Commit | Line | Data |
---|---|---|
9542cb1f | 1 | /* Coding system handler (conversion, detection, etc). |
aaef169d | 2 | Copyright (C) 2001, 2002, 2003, 2004, 2005, |
76b6f707 | 3 | 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
7976eda0 | 4 | Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
76b6f707 | 5 | 2005, 2006, 2007, 2008, 2009 |
ce03bf76 KH |
6 | National Institute of Advanced Industrial Science and Technology (AIST) |
7 | Registration Number H14PRO021 | |
8f924df7 | 8 | Copyright (C) 2003 |
df7492f9 KH |
9 | National Institute of Advanced Industrial Science and Technology (AIST) |
10 | Registration Number H13PRO009 | |
4ed46869 | 11 | |
369314dc KH |
12 | This file is part of GNU Emacs. |
13 | ||
9ec0b715 | 14 | GNU Emacs is free software: you can redistribute it and/or modify |
369314dc | 15 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
16 | the Free Software Foundation, either version 3 of the License, or |
17 | (at your option) any later version. | |
4ed46869 | 18 | |
369314dc KH |
19 | GNU Emacs is distributed in the hope that it will be useful, |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
4ed46869 | 23 | |
369314dc | 24 | You should have received a copy of the GNU General Public License |
9ec0b715 | 25 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ed46869 KH |
26 | |
27 | /*** TABLE OF CONTENTS *** | |
28 | ||
b73bfc1c | 29 | 0. General comments |
4ed46869 | 30 | 1. Preamble |
df7492f9 KH |
31 | 2. Emacs' internal format (emacs-utf-8) handlers |
32 | 3. UTF-8 handlers | |
33 | 4. UTF-16 handlers | |
34 | 5. Charset-base coding systems handlers | |
35 | 6. emacs-mule (old Emacs' internal format) handlers | |
36 | 7. ISO2022 handlers | |
37 | 8. Shift-JIS and BIG5 handlers | |
38 | 9. CCL handlers | |
39 | 10. C library functions | |
40 | 11. Emacs Lisp library functions | |
41 | 12. Postamble | |
4ed46869 KH |
42 | |
43 | */ | |
44 | ||
df7492f9 | 45 | /*** 0. General comments *** |
b73bfc1c KH |
46 | |
47 | ||
df7492f9 | 48 | CODING SYSTEM |
4ed46869 | 49 | |
5bad0796 DL |
50 | A coding system is an object for an encoding mechanism that contains |
51 | information about how to convert byte sequences to character | |
e19c3639 KH |
52 | sequences and vice versa. When we say "decode", it means converting |
53 | a byte sequence of a specific coding system into a character | |
54 | sequence that is represented by Emacs' internal coding system | |
55 | `emacs-utf-8', and when we say "encode", it means converting a | |
56 | character sequence of emacs-utf-8 to a byte sequence of a specific | |
0ef69138 | 57 | coding system. |
4ed46869 | 58 | |
e19c3639 KH |
59 | In Emacs Lisp, a coding system is represented by a Lisp symbol. In |
60 | C level, a coding system is represented by a vector of attributes | |
5bad0796 | 61 | stored in the hash table Vcharset_hash_table. The conversion from |
e19c3639 KH |
62 | coding system symbol to attributes vector is done by looking up |
63 | Vcharset_hash_table by the symbol. | |
4ed46869 | 64 | |
e19c3639 | 65 | Coding systems are classified into the following types depending on |
5bad0796 | 66 | the encoding mechanism. Here's a brief description of the types. |
4ed46869 | 67 | |
df7492f9 KH |
68 | o UTF-8 |
69 | ||
70 | o UTF-16 | |
71 | ||
72 | o Charset-base coding system | |
73 | ||
74 | A coding system defined by one or more (coded) character sets. | |
5bad0796 | 75 | Decoding and encoding are done by a code converter defined for each |
df7492f9 KH |
76 | character set. |
77 | ||
5bad0796 | 78 | o Old Emacs internal format (emacs-mule) |
df7492f9 | 79 | |
5bad0796 | 80 | The coding system adopted by old versions of Emacs (20 and 21). |
4ed46869 | 81 | |
df7492f9 | 82 | o ISO2022-base coding system |
4ed46869 KH |
83 | |
84 | The most famous coding system for multiple character sets. X's | |
df7492f9 KH |
85 | Compound Text, various EUCs (Extended Unix Code), and coding systems |
86 | used in the Internet communication such as ISO-2022-JP are all | |
87 | variants of ISO2022. | |
4ed46869 | 88 | |
df7492f9 | 89 | o SJIS (or Shift-JIS or MS-Kanji-Code) |
93dec019 | 90 | |
4ed46869 KH |
91 | A coding system to encode character sets: ASCII, JISX0201, and |
92 | JISX0208. Widely used for PC's in Japan. Details are described in | |
df7492f9 | 93 | section 8. |
4ed46869 | 94 | |
df7492f9 | 95 | o BIG5 |
4ed46869 | 96 | |
df7492f9 | 97 | A coding system to encode character sets: ASCII and Big5. Widely |
cfb43547 | 98 | used for Chinese (mainly in Taiwan and Hong Kong). Details are |
df7492f9 KH |
99 | described in section 8. In this file, when we write "big5" (all |
100 | lowercase), we mean the coding system, and when we write "Big5" | |
101 | (capitalized), we mean the character set. | |
4ed46869 | 102 | |
df7492f9 | 103 | o CCL |
27901516 | 104 | |
5bad0796 | 105 | If a user wants to decode/encode text encoded in a coding system |
df7492f9 KH |
106 | not listed above, he can supply a decoder and an encoder for it in |
107 | CCL (Code Conversion Language) programs. Emacs executes the CCL | |
108 | program while decoding/encoding. | |
27901516 | 109 | |
df7492f9 | 110 | o Raw-text |
4ed46869 | 111 | |
5a936b46 | 112 | A coding system for text containing raw eight-bit data. Emacs |
5bad0796 | 113 | treats each byte of source text as a character (except for |
df7492f9 | 114 | end-of-line conversion). |
4ed46869 | 115 | |
df7492f9 KH |
116 | o No-conversion |
117 | ||
118 | Like raw text, but don't do end-of-line conversion. | |
4ed46869 | 119 | |
4ed46869 | 120 | |
df7492f9 | 121 | END-OF-LINE FORMAT |
4ed46869 | 122 | |
5bad0796 | 123 | How text end-of-line is encoded depends on operating system. For |
df7492f9 | 124 | instance, Unix's format is just one byte of LF (line-feed) code, |
f4dee582 | 125 | whereas DOS's format is two-byte sequence of `carriage-return' and |
d46c5b12 KH |
126 | `line-feed' codes. MacOS's format is usually one byte of |
127 | `carriage-return'. | |
4ed46869 | 128 | |
cfb43547 | 129 | Since text character encoding and end-of-line encoding are |
df7492f9 KH |
130 | independent, any coding system described above can take any format |
131 | of end-of-line (except for no-conversion). | |
4ed46869 | 132 | |
e19c3639 KH |
133 | STRUCT CODING_SYSTEM |
134 | ||
135 | Before using a coding system for code conversion (i.e. decoding and | |
136 | encoding), we setup a structure of type `struct coding_system'. | |
137 | This structure keeps various information about a specific code | |
5bad0796 | 138 | conversion (e.g. the location of source and destination data). |
4ed46869 KH |
139 | |
140 | */ | |
141 | ||
df7492f9 KH |
142 | /* COMMON MACROS */ |
143 | ||
144 | ||
4ed46869 KH |
145 | /*** GENERAL NOTES on `detect_coding_XXX ()' functions *** |
146 | ||
df7492f9 | 147 | These functions check if a byte sequence specified as a source in |
ff0dacd7 KH |
148 | CODING conforms to the format of XXX, and update the members of |
149 | DETECT_INFO. | |
df7492f9 | 150 | |
ff0dacd7 | 151 | Return 1 if the byte sequence conforms to XXX, otherwise return 0. |
df7492f9 KH |
152 | |
153 | Below is the template of these functions. */ | |
154 | ||
4ed46869 | 155 | #if 0 |
df7492f9 | 156 | static int |
ff0dacd7 | 157 | detect_coding_XXX (coding, detect_info) |
df7492f9 | 158 | struct coding_system *coding; |
ff0dacd7 | 159 | struct coding_detection_info *detect_info; |
4ed46869 | 160 | { |
f1d34bca MB |
161 | const unsigned char *src = coding->source; |
162 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 163 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 164 | int consumed_chars = 0; |
df7492f9 KH |
165 | int found = 0; |
166 | ...; | |
167 | ||
168 | while (1) | |
169 | { | |
170 | /* Get one byte from the source. If the souce is exausted, jump | |
171 | to no_more_source:. */ | |
172 | ONE_MORE_BYTE (c); | |
ff0dacd7 KH |
173 | |
174 | if (! __C_conforms_to_XXX___ (c)) | |
175 | break; | |
176 | if (! __C_strongly_suggests_XXX__ (c)) | |
177 | found = CATEGORY_MASK_XXX; | |
df7492f9 | 178 | } |
ff0dacd7 KH |
179 | /* The byte sequence is invalid for XXX. */ |
180 | detect_info->rejected |= CATEGORY_MASK_XXX; | |
df7492f9 | 181 | return 0; |
ff0dacd7 | 182 | |
df7492f9 | 183 | no_more_source: |
ff0dacd7 KH |
184 | /* The source exausted successfully. */ |
185 | detect_info->found |= found; | |
df7492f9 | 186 | return 1; |
4ed46869 KH |
187 | } |
188 | #endif | |
189 | ||
190 | /*** GENERAL NOTES on `decode_coding_XXX ()' functions *** | |
191 | ||
df7492f9 KH |
192 | These functions decode a byte sequence specified as a source by |
193 | CODING. The resulting multibyte text goes to a place pointed to by | |
194 | CODING->charbuf, the length of which should not exceed | |
195 | CODING->charbuf_size; | |
d46c5b12 | 196 | |
df7492f9 KH |
197 | These functions set the information of original and decoded texts in |
198 | CODING->consumed, CODING->consumed_char, and CODING->charbuf_used. | |
199 | They also set CODING->result to one of CODING_RESULT_XXX indicating | |
200 | how the decoding is finished. | |
d46c5b12 | 201 | |
df7492f9 | 202 | Below is the template of these functions. */ |
d46c5b12 | 203 | |
4ed46869 | 204 | #if 0 |
b73bfc1c | 205 | static void |
df7492f9 | 206 | decode_coding_XXXX (coding) |
4ed46869 | 207 | struct coding_system *coding; |
4ed46869 | 208 | { |
f1d34bca MB |
209 | const unsigned char *src = coding->source + coding->consumed; |
210 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
211 | /* SRC_BASE remembers the start position in source in each loop. |
212 | The loop will be exited when there's not enough source code, or | |
213 | when there's no room in CHARBUF for a decoded character. */ | |
f1d34bca | 214 | const unsigned char *src_base; |
df7492f9 | 215 | /* A buffer to produce decoded characters. */ |
69a80ea3 KH |
216 | int *charbuf = coding->charbuf + coding->charbuf_used; |
217 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
218 | int multibytep = coding->src_multibyte; |
219 | ||
220 | while (1) | |
221 | { | |
222 | src_base = src; | |
223 | if (charbuf < charbuf_end) | |
224 | /* No more room to produce a decoded character. */ | |
225 | break; | |
226 | ONE_MORE_BYTE (c); | |
227 | /* Decode it. */ | |
228 | } | |
229 | ||
230 | no_more_source: | |
231 | if (src_base < src_end | |
232 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
233 | /* If the source ends by partial bytes to construct a character, | |
234 | treat them as eight-bit raw data. */ | |
235 | while (src_base < src_end && charbuf < charbuf_end) | |
236 | *charbuf++ = *src_base++; | |
237 | /* Remember how many bytes and characters we consumed. If the | |
238 | source is multibyte, the bytes and chars are not identical. */ | |
239 | coding->consumed = coding->consumed_char = src_base - coding->source; | |
240 | /* Remember how many characters we produced. */ | |
241 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
242 | } |
243 | #endif | |
244 | ||
245 | /*** GENERAL NOTES on `encode_coding_XXX ()' functions *** | |
246 | ||
df7492f9 KH |
247 | These functions encode SRC_BYTES length text at SOURCE of Emacs' |
248 | internal multibyte format by CODING. The resulting byte sequence | |
b73bfc1c KH |
249 | goes to a place pointed to by DESTINATION, the length of which |
250 | should not exceed DST_BYTES. | |
d46c5b12 | 251 | |
df7492f9 KH |
252 | These functions set the information of original and encoded texts in |
253 | the members produced, produced_char, consumed, and consumed_char of | |
254 | the structure *CODING. They also set the member result to one of | |
255 | CODING_RESULT_XXX indicating how the encoding finished. | |
d46c5b12 | 256 | |
df7492f9 KH |
257 | DST_BYTES zero means that source area and destination area are |
258 | overlapped, which means that we can produce a encoded text until it | |
259 | reaches at the head of not-yet-encoded source text. | |
d46c5b12 | 260 | |
df7492f9 | 261 | Below is a template of these functions. */ |
4ed46869 | 262 | #if 0 |
b73bfc1c | 263 | static void |
df7492f9 | 264 | encode_coding_XXX (coding) |
4ed46869 | 265 | struct coding_system *coding; |
4ed46869 | 266 | { |
df7492f9 KH |
267 | int multibytep = coding->dst_multibyte; |
268 | int *charbuf = coding->charbuf; | |
269 | int *charbuf_end = charbuf->charbuf + coding->charbuf_used; | |
270 | unsigned char *dst = coding->destination + coding->produced; | |
271 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
272 | unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_; | |
273 | int produced_chars = 0; | |
274 | ||
275 | for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++) | |
276 | { | |
277 | int c = *charbuf; | |
278 | /* Encode C into DST, and increment DST. */ | |
279 | } | |
280 | label_no_more_destination: | |
281 | /* How many chars and bytes we produced. */ | |
282 | coding->produced_char += produced_chars; | |
283 | coding->produced = dst - coding->destination; | |
4ed46869 KH |
284 | } |
285 | #endif | |
286 | ||
4ed46869 KH |
287 | \f |
288 | /*** 1. Preamble ***/ | |
289 | ||
68c45bf0 | 290 | #include <config.h> |
4ed46869 KH |
291 | #include <stdio.h> |
292 | ||
4ed46869 KH |
293 | #include "lisp.h" |
294 | #include "buffer.h" | |
df7492f9 | 295 | #include "character.h" |
4ed46869 KH |
296 | #include "charset.h" |
297 | #include "ccl.h" | |
df7492f9 | 298 | #include "composite.h" |
4ed46869 KH |
299 | #include "coding.h" |
300 | #include "window.h" | |
b8299c66 KL |
301 | #include "frame.h" |
302 | #include "termhooks.h" | |
4ed46869 | 303 | |
df7492f9 | 304 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 305 | |
df7492f9 | 306 | Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type; |
1965cb73 DL |
307 | Lisp_Object Qunix, Qdos; |
308 | extern Lisp_Object Qmac; /* frame.c */ | |
4ed46869 KH |
309 | Lisp_Object Qbuffer_file_coding_system; |
310 | Lisp_Object Qpost_read_conversion, Qpre_write_conversion; | |
df7492f9 | 311 | Lisp_Object Qdefault_char; |
27901516 | 312 | Lisp_Object Qno_conversion, Qundecided; |
df7492f9 | 313 | Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5; |
b49a1807 | 314 | Lisp_Object Qbig, Qlittle; |
bb0115a2 | 315 | Lisp_Object Qcoding_system_history; |
1397dc18 | 316 | Lisp_Object Qvalid_codes; |
2133e2d1 | 317 | Lisp_Object QCcategory, QCmnemonic, QCdefault_char; |
a6f87d34 KH |
318 | Lisp_Object QCdecode_translation_table, QCencode_translation_table; |
319 | Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
35befdaa | 320 | Lisp_Object QCascii_compatible_p; |
4ed46869 KH |
321 | |
322 | extern Lisp_Object Qinsert_file_contents, Qwrite_region; | |
387f6ba5 | 323 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 KH |
324 | Lisp_Object Qstart_process, Qopen_network_stream; |
325 | Lisp_Object Qtarget_idx; | |
326 | ||
065e3595 KH |
327 | Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source; |
328 | Lisp_Object Qinterrupted, Qinsufficient_memory; | |
329 | ||
c7183fb8 GM |
330 | extern Lisp_Object Qcompletion_ignore_case; |
331 | ||
44e8490d KH |
332 | /* If a symbol has this property, evaluate the value to define the |
333 | symbol as a coding system. */ | |
334 | static Lisp_Object Qcoding_system_define_form; | |
335 | ||
5d5bf4d8 KH |
336 | int coding_system_require_warning; |
337 | ||
d46c5b12 KH |
338 | Lisp_Object Vselect_safe_coding_system_function; |
339 | ||
7722baf9 EZ |
340 | /* Mnemonic string for each format of end-of-line. */ |
341 | Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac; | |
342 | /* Mnemonic string to indicate format of end-of-line is not yet | |
4ed46869 | 343 | decided. */ |
7722baf9 | 344 | Lisp_Object eol_mnemonic_undecided; |
4ed46869 | 345 | |
fcbcfb64 KH |
346 | /* Format of end-of-line decided by system. This is Qunix on |
347 | Unix and Mac, Qdos on DOS/Windows. | |
348 | This has an effect only for external encoding (i.e. for output to | |
349 | file and process), not for in-buffer or Lisp string encoding. */ | |
350 | static Lisp_Object system_eol_type; | |
351 | ||
4ed46869 KH |
352 | #ifdef emacs |
353 | ||
4608c386 KH |
354 | Lisp_Object Vcoding_system_list, Vcoding_system_alist; |
355 | ||
356 | Lisp_Object Qcoding_system_p, Qcoding_system_error; | |
4ed46869 | 357 | |
d46c5b12 KH |
358 | /* Coding system emacs-mule and raw-text are for converting only |
359 | end-of-line format. */ | |
360 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 361 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 362 | |
4ed46869 KH |
363 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
364 | routines by the following three variables. */ | |
365 | /* Coding-system for reading files and receiving data from process. */ | |
366 | Lisp_Object Vcoding_system_for_read; | |
367 | /* Coding-system for writing files and sending data to process. */ | |
368 | Lisp_Object Vcoding_system_for_write; | |
369 | /* Coding-system actually used in the latest I/O. */ | |
370 | Lisp_Object Vlast_coding_system_used; | |
065e3595 KH |
371 | /* Set to non-nil when an error is detected while code conversion. */ |
372 | Lisp_Object Vlast_code_conversion_error; | |
c4825358 | 373 | /* A vector of length 256 which contains information about special |
94487c4e | 374 | Latin codes (especially for dealing with Microsoft codes). */ |
3f003981 | 375 | Lisp_Object Vlatin_extra_code_table; |
c4825358 | 376 | |
9ce27fde KH |
377 | /* Flag to inhibit code conversion of end-of-line format. */ |
378 | int inhibit_eol_conversion; | |
379 | ||
74383408 KH |
380 | /* Flag to inhibit ISO2022 escape sequence detection. */ |
381 | int inhibit_iso_escape_detection; | |
382 | ||
97b1b294 EZ |
383 | /* Flag to inhibit detection of binary files through null bytes. */ |
384 | int inhibit_null_byte_detection; | |
385 | ||
ed29121d EZ |
386 | /* Flag to make buffer-file-coding-system inherit from process-coding. */ |
387 | int inherit_process_coding_system; | |
388 | ||
c4825358 KH |
389 | /* Coding system to be used to encode text for terminal display when |
390 | terminal coding system is nil. */ | |
391 | struct coding_system safe_terminal_coding; | |
392 | ||
02ba4723 KH |
393 | Lisp_Object Vfile_coding_system_alist; |
394 | Lisp_Object Vprocess_coding_system_alist; | |
395 | Lisp_Object Vnetwork_coding_system_alist; | |
4ed46869 | 396 | |
68c45bf0 PE |
397 | Lisp_Object Vlocale_coding_system; |
398 | ||
4ed46869 KH |
399 | #endif /* emacs */ |
400 | ||
f967223b KH |
401 | /* Flag to tell if we look up translation table on character code |
402 | conversion. */ | |
84fbb8a0 | 403 | Lisp_Object Venable_character_translation; |
f967223b KH |
404 | /* Standard translation table to look up on decoding (reading). */ |
405 | Lisp_Object Vstandard_translation_table_for_decode; | |
406 | /* Standard translation table to look up on encoding (writing). */ | |
407 | Lisp_Object Vstandard_translation_table_for_encode; | |
84fbb8a0 | 408 | |
f967223b KH |
409 | Lisp_Object Qtranslation_table; |
410 | Lisp_Object Qtranslation_table_id; | |
411 | Lisp_Object Qtranslation_table_for_decode; | |
412 | Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 KH |
413 | |
414 | /* Alist of charsets vs revision number. */ | |
df7492f9 | 415 | static Lisp_Object Vcharset_revision_table; |
4ed46869 | 416 | |
02ba4723 KH |
417 | /* Default coding systems used for process I/O. */ |
418 | Lisp_Object Vdefault_process_coding_system; | |
419 | ||
002fdb44 DL |
420 | /* Char table for translating Quail and self-inserting input. */ |
421 | Lisp_Object Vtranslation_table_for_input; | |
422 | ||
df7492f9 KH |
423 | /* Two special coding systems. */ |
424 | Lisp_Object Vsjis_coding_system; | |
425 | Lisp_Object Vbig5_coding_system; | |
426 | ||
df7492f9 KH |
427 | /* ISO2022 section */ |
428 | ||
429 | #define CODING_ISO_INITIAL(coding, reg) \ | |
430 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
431 | coding_attr_iso_initial), \ | |
432 | reg))) | |
433 | ||
434 | ||
1b3b981b AS |
435 | #define CODING_ISO_REQUEST(coding, charset_id) \ |
436 | (((charset_id) <= (coding)->max_charset_id \ | |
437 | ? ((coding)->safe_charsets[charset_id] != 255 \ | |
438 | ? (coding)->safe_charsets[charset_id] \ | |
439 | : -1) \ | |
df7492f9 KH |
440 | : -1)) |
441 | ||
442 | ||
443 | #define CODING_ISO_FLAGS(coding) \ | |
444 | ((coding)->spec.iso_2022.flags) | |
445 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
446 | ((coding)->spec.iso_2022.current_designation[reg]) | |
447 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
448 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
449 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
450 | ((coding)->spec.iso_2022.single_shifting) | |
451 | #define CODING_ISO_BOL(coding) \ | |
452 | ((coding)->spec.iso_2022.bol) | |
453 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
454 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
e951386e KH |
455 | #define CODING_ISO_CMP_STATUS(coding) \ |
456 | (&(coding)->spec.iso_2022.cmp_status) | |
457 | #define CODING_ISO_EXTSEGMENT_LEN(coding) \ | |
458 | ((coding)->spec.iso_2022.ctext_extended_segment_len) | |
459 | #define CODING_ISO_EMBEDDED_UTF_8(coding) \ | |
460 | ((coding)->spec.iso_2022.embedded_utf_8) | |
df7492f9 KH |
461 | |
462 | /* Control characters of ISO2022. */ | |
463 | /* code */ /* function */ | |
464 | #define ISO_CODE_LF 0x0A /* line-feed */ | |
465 | #define ISO_CODE_CR 0x0D /* carriage-return */ | |
466 | #define ISO_CODE_SO 0x0E /* shift-out */ | |
467 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
468 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
469 | #define ISO_CODE_ESC 0x1B /* escape */ | |
470 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
471 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
472 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
473 | ||
474 | /* All code (1-byte) of ISO2022 is classified into one of the | |
475 | followings. */ | |
476 | enum iso_code_class_type | |
477 | { | |
478 | ISO_control_0, /* Control codes in the range | |
479 | 0x00..0x1F and 0x7F, except for the | |
480 | following 5 codes. */ | |
df7492f9 KH |
481 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
482 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
483 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
484 | ISO_escape, /* ISO_CODE_SO (0x1B) */ | |
485 | ISO_control_1, /* Control codes in the range | |
486 | 0x80..0x9F, except for the | |
487 | following 3 codes. */ | |
488 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
489 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
490 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
491 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
492 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
493 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
494 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
495 | }; | |
05e6f5dc | 496 | |
df7492f9 KH |
497 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
498 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 499 | |
df7492f9 KH |
500 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
501 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
502 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 503 | |
df7492f9 KH |
504 | /* If set, reset graphic planes and registers at end-of-line to the |
505 | initial state. */ | |
506 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 507 | |
df7492f9 KH |
508 | /* If set, reset graphic planes and registers before any control |
509 | characters to the initial state. */ | |
510 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 511 | |
df7492f9 KH |
512 | /* If set, encode by 7-bit environment. */ |
513 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 514 | |
df7492f9 KH |
515 | /* If set, use locking-shift function. */ |
516 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 517 | |
df7492f9 KH |
518 | /* If set, use single-shift function. Overwrite |
519 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
520 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 521 | |
df7492f9 KH |
522 | /* If set, use designation escape sequence. */ |
523 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 524 | |
df7492f9 KH |
525 | /* If set, produce revision number sequence. */ |
526 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 527 | |
df7492f9 KH |
528 | /* If set, produce ISO6429's direction specifying sequence. */ |
529 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 530 | |
df7492f9 KH |
531 | /* If set, assume designation states are reset at beginning of line on |
532 | output. */ | |
533 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 534 | |
df7492f9 KH |
535 | /* If set, designation sequence should be placed at beginning of line |
536 | on output. */ | |
537 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 538 | |
df7492f9 KH |
539 | /* If set, do not encode unsafe charactes on output. */ |
540 | #define CODING_ISO_FLAG_SAFE 0x0800 | |
aa72b389 | 541 | |
df7492f9 KH |
542 | /* If set, extra latin codes (128..159) are accepted as a valid code |
543 | on input. */ | |
544 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 545 | |
df7492f9 | 546 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 547 | |
df7492f9 | 548 | #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 |
aa72b389 | 549 | |
bf16eb23 | 550 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 551 | |
bf16eb23 | 552 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 553 | |
bf16eb23 | 554 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 555 | |
df7492f9 KH |
556 | /* A character to be produced on output if encoding of the original |
557 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
558 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 559 | |
a470d443 KH |
560 | /* UTF-8 section */ |
561 | #define CODING_UTF_8_BOM(coding) \ | |
562 | ((coding)->spec.utf_8_bom) | |
4ed46869 | 563 | |
df7492f9 KH |
564 | /* UTF-16 section */ |
565 | #define CODING_UTF_16_BOM(coding) \ | |
566 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 567 | |
df7492f9 KH |
568 | #define CODING_UTF_16_ENDIAN(coding) \ |
569 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 570 | |
df7492f9 KH |
571 | #define CODING_UTF_16_SURROGATE(coding) \ |
572 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 573 | |
4ed46869 | 574 | |
df7492f9 KH |
575 | /* CCL section */ |
576 | #define CODING_CCL_DECODER(coding) \ | |
577 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
578 | #define CODING_CCL_ENCODER(coding) \ | |
579 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
580 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 581 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 582 | |
5a936b46 | 583 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 584 | |
df7492f9 KH |
585 | enum coding_category |
586 | { | |
587 | coding_category_iso_7, | |
588 | coding_category_iso_7_tight, | |
589 | coding_category_iso_8_1, | |
590 | coding_category_iso_8_2, | |
591 | coding_category_iso_7_else, | |
592 | coding_category_iso_8_else, | |
a470d443 KH |
593 | coding_category_utf_8_auto, |
594 | coding_category_utf_8_nosig, | |
595 | coding_category_utf_8_sig, | |
df7492f9 KH |
596 | coding_category_utf_16_auto, |
597 | coding_category_utf_16_be, | |
598 | coding_category_utf_16_le, | |
599 | coding_category_utf_16_be_nosig, | |
600 | coding_category_utf_16_le_nosig, | |
601 | coding_category_charset, | |
602 | coding_category_sjis, | |
603 | coding_category_big5, | |
604 | coding_category_ccl, | |
605 | coding_category_emacs_mule, | |
606 | /* All above are targets of code detection. */ | |
607 | coding_category_raw_text, | |
608 | coding_category_undecided, | |
609 | coding_category_max | |
610 | }; | |
611 | ||
612 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
613 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
614 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
615 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
616 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
617 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
618 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
a470d443 KH |
619 | #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto) |
620 | #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig) | |
621 | #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig) | |
b49a1807 | 622 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
623 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
624 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
625 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
626 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
627 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
628 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
629 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
630 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
631 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 632 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
633 | |
634 | /* This value is returned if detect_coding_mask () find nothing other | |
635 | than ASCII characters. */ | |
636 | #define CATEGORY_MASK_ANY \ | |
637 | (CATEGORY_MASK_ISO_7 \ | |
638 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
639 | | CATEGORY_MASK_ISO_8_1 \ | |
640 | | CATEGORY_MASK_ISO_8_2 \ | |
641 | | CATEGORY_MASK_ISO_7_ELSE \ | |
642 | | CATEGORY_MASK_ISO_8_ELSE \ | |
a470d443 KH |
643 | | CATEGORY_MASK_UTF_8_AUTO \ |
644 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
645 | | CATEGORY_MASK_UTF_8_SIG \ | |
2f3cbb32 | 646 | | CATEGORY_MASK_UTF_16_AUTO \ |
df7492f9 KH |
647 | | CATEGORY_MASK_UTF_16_BE \ |
648 | | CATEGORY_MASK_UTF_16_LE \ | |
649 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
650 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
651 | | CATEGORY_MASK_CHARSET \ | |
652 | | CATEGORY_MASK_SJIS \ | |
653 | | CATEGORY_MASK_BIG5 \ | |
654 | | CATEGORY_MASK_CCL \ | |
655 | | CATEGORY_MASK_EMACS_MULE) | |
656 | ||
657 | ||
658 | #define CATEGORY_MASK_ISO_7BIT \ | |
659 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
660 | ||
661 | #define CATEGORY_MASK_ISO_8BIT \ | |
662 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
663 | ||
664 | #define CATEGORY_MASK_ISO_ELSE \ | |
665 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
666 | ||
667 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
668 | (CATEGORY_MASK_ISO_7 \ | |
669 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
670 | | CATEGORY_MASK_ISO_7_ELSE \ | |
671 | | CATEGORY_MASK_ISO_8_ELSE) | |
672 | ||
673 | #define CATEGORY_MASK_ISO \ | |
674 | ( CATEGORY_MASK_ISO_7BIT \ | |
675 | | CATEGORY_MASK_ISO_8BIT \ | |
676 | | CATEGORY_MASK_ISO_ELSE) | |
677 | ||
678 | #define CATEGORY_MASK_UTF_16 \ | |
2f3cbb32 KH |
679 | (CATEGORY_MASK_UTF_16_AUTO \ |
680 | | CATEGORY_MASK_UTF_16_BE \ | |
df7492f9 KH |
681 | | CATEGORY_MASK_UTF_16_LE \ |
682 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
683 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
684 | ||
a470d443 KH |
685 | #define CATEGORY_MASK_UTF_8 \ |
686 | (CATEGORY_MASK_UTF_8_AUTO \ | |
687 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
688 | | CATEGORY_MASK_UTF_8_SIG) | |
df7492f9 KH |
689 | |
690 | /* List of symbols `coding-category-xxx' ordered by priority. This | |
691 | variable is exposed to Emacs Lisp. */ | |
692 | static Lisp_Object Vcoding_category_list; | |
693 | ||
694 | /* Table of coding categories (Lisp symbols). This variable is for | |
695 | internal use oly. */ | |
696 | static Lisp_Object Vcoding_category_table; | |
697 | ||
698 | /* Table of coding-categories ordered by priority. */ | |
699 | static enum coding_category coding_priorities[coding_category_max]; | |
700 | ||
701 | /* Nth element is a coding context for the coding system bound to the | |
702 | Nth coding category. */ | |
703 | static struct coding_system coding_categories[coding_category_max]; | |
704 | ||
df7492f9 KH |
705 | /*** Commonly used macros and functions ***/ |
706 | ||
707 | #ifndef min | |
708 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
709 | #endif | |
710 | #ifndef max | |
711 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
712 | #endif | |
4ed46869 | 713 | |
24a73b0a KH |
714 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
715 | do { \ | |
716 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
717 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 718 | } while (0) |
4ed46869 | 719 | |
4ed46869 | 720 | |
df7492f9 KH |
721 | /* Safely get one byte from the source text pointed by SRC which ends |
722 | at SRC_END, and set C to that byte. If there are not enough bytes | |
065e3595 KH |
723 | in the source, it jumps to `no_more_source'. If multibytep is |
724 | nonzero, and a multibyte character is found at SRC, set C to the | |
725 | negative value of the character code. The caller should declare | |
726 | and set these variables appropriately in advance: | |
727 | src, src_end, multibytep */ | |
aa72b389 | 728 | |
065e3595 KH |
729 | #define ONE_MORE_BYTE(c) \ |
730 | do { \ | |
731 | if (src == src_end) \ | |
732 | { \ | |
733 | if (src_base < src) \ | |
734 | record_conversion_result \ | |
735 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
736 | goto no_more_source; \ | |
737 | } \ | |
738 | c = *src++; \ | |
739 | if (multibytep && (c & 0x80)) \ | |
740 | { \ | |
741 | if ((c & 0xFE) == 0xC0) \ | |
742 | c = ((c & 1) << 6) | *src++; \ | |
743 | else \ | |
744 | { \ | |
35befdaa KH |
745 | src--; \ |
746 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
747 | record_conversion_result \ |
748 | (coding, CODING_RESULT_INVALID_SRC); \ | |
749 | } \ | |
750 | } \ | |
751 | consumed_chars++; \ | |
aa72b389 KH |
752 | } while (0) |
753 | ||
f56a4450 | 754 | /* Safely get two bytes from the source text pointed by SRC which ends |
220eeac9 KH |
755 | at SRC_END, and set C1 and C2 to those bytes while skipping the |
756 | heading multibyte characters. If there are not enough bytes in the | |
757 | source, it jumps to `no_more_source'. If multibytep is nonzero and | |
758 | a multibyte character is found for C2, set C2 to the negative value | |
759 | of the character code. The caller should declare and set these | |
760 | variables appropriately in advance: | |
f56a4450 KH |
761 | src, src_end, multibytep |
762 | It is intended that this macro is used in detect_coding_utf_16. */ | |
763 | ||
220eeac9 KH |
764 | #define TWO_MORE_BYTES(c1, c2) \ |
765 | do { \ | |
766 | do { \ | |
767 | if (src == src_end) \ | |
768 | goto no_more_source; \ | |
769 | c1 = *src++; \ | |
770 | if (multibytep && (c1 & 0x80)) \ | |
771 | { \ | |
772 | if ((c1 & 0xFE) == 0xC0) \ | |
773 | c1 = ((c1 & 1) << 6) | *src++; \ | |
774 | else \ | |
775 | { \ | |
776 | src += BYTES_BY_CHAR_HEAD (c1) - 1; \ | |
777 | c1 = -1; \ | |
778 | } \ | |
779 | } \ | |
780 | } while (c1 < 0); \ | |
781 | if (src == src_end) \ | |
782 | goto no_more_source; \ | |
783 | c2 = *src++; \ | |
784 | if (multibytep && (c2 & 0x80)) \ | |
785 | { \ | |
786 | if ((c2 & 0xFE) == 0xC0) \ | |
787 | c2 = ((c2 & 1) << 6) | *src++; \ | |
788 | else \ | |
789 | c2 = -1; \ | |
790 | } \ | |
f56a4450 KH |
791 | } while (0) |
792 | ||
aa72b389 | 793 | |
065e3595 KH |
794 | #define ONE_MORE_BYTE_NO_CHECK(c) \ |
795 | do { \ | |
796 | c = *src++; \ | |
797 | if (multibytep && (c & 0x80)) \ | |
798 | { \ | |
799 | if ((c & 0xFE) == 0xC0) \ | |
800 | c = ((c & 1) << 6) | *src++; \ | |
801 | else \ | |
802 | { \ | |
35befdaa KH |
803 | src--; \ |
804 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
805 | record_conversion_result \ |
806 | (coding, CODING_RESULT_INVALID_SRC); \ | |
807 | } \ | |
808 | } \ | |
809 | consumed_chars++; \ | |
aa72b389 KH |
810 | } while (0) |
811 | ||
aa72b389 | 812 | |
df7492f9 KH |
813 | /* Store a byte C in the place pointed by DST and increment DST to the |
814 | next free point, and increment PRODUCED_CHARS. The caller should | |
815 | assure that C is 0..127, and declare and set the variable `dst' | |
816 | appropriately in advance. | |
817 | */ | |
aa72b389 KH |
818 | |
819 | ||
df7492f9 KH |
820 | #define EMIT_ONE_ASCII_BYTE(c) \ |
821 | do { \ | |
822 | produced_chars++; \ | |
823 | *dst++ = (c); \ | |
b6871cc7 | 824 | } while (0) |
aa72b389 KH |
825 | |
826 | ||
df7492f9 | 827 | /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */ |
aa72b389 | 828 | |
df7492f9 KH |
829 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
830 | do { \ | |
831 | produced_chars += 2; \ | |
832 | *dst++ = (c1), *dst++ = (c2); \ | |
833 | } while (0) | |
aa72b389 KH |
834 | |
835 | ||
df7492f9 KH |
836 | /* Store a byte C in the place pointed by DST and increment DST to the |
837 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is | |
838 | nonzero, store in an appropriate multibyte from. The caller should | |
839 | declare and set the variables `dst' and `multibytep' appropriately | |
840 | in advance. */ | |
841 | ||
842 | #define EMIT_ONE_BYTE(c) \ | |
843 | do { \ | |
844 | produced_chars++; \ | |
845 | if (multibytep) \ | |
846 | { \ | |
847 | int ch = (c); \ | |
848 | if (ch >= 0x80) \ | |
849 | ch = BYTE8_TO_CHAR (ch); \ | |
850 | CHAR_STRING_ADVANCE (ch, dst); \ | |
851 | } \ | |
852 | else \ | |
853 | *dst++ = (c); \ | |
aa72b389 | 854 | } while (0) |
aa72b389 | 855 | |
aa72b389 | 856 | |
df7492f9 | 857 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 858 | |
e19c3639 KH |
859 | #define EMIT_TWO_BYTES(c1, c2) \ |
860 | do { \ | |
861 | produced_chars += 2; \ | |
862 | if (multibytep) \ | |
863 | { \ | |
864 | int ch; \ | |
865 | \ | |
866 | ch = (c1); \ | |
867 | if (ch >= 0x80) \ | |
868 | ch = BYTE8_TO_CHAR (ch); \ | |
869 | CHAR_STRING_ADVANCE (ch, dst); \ | |
870 | ch = (c2); \ | |
871 | if (ch >= 0x80) \ | |
872 | ch = BYTE8_TO_CHAR (ch); \ | |
873 | CHAR_STRING_ADVANCE (ch, dst); \ | |
874 | } \ | |
875 | else \ | |
876 | { \ | |
877 | *dst++ = (c1); \ | |
878 | *dst++ = (c2); \ | |
879 | } \ | |
aa72b389 KH |
880 | } while (0) |
881 | ||
882 | ||
df7492f9 KH |
883 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
884 | do { \ | |
885 | EMIT_ONE_BYTE (c1); \ | |
886 | EMIT_TWO_BYTES (c2, c3); \ | |
887 | } while (0) | |
aa72b389 | 888 | |
aa72b389 | 889 | |
df7492f9 KH |
890 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
891 | do { \ | |
892 | EMIT_TWO_BYTES (c1, c2); \ | |
893 | EMIT_TWO_BYTES (c3, c4); \ | |
894 | } while (0) | |
aa72b389 | 895 | |
aa72b389 | 896 | |
f6cbaf43 KH |
897 | /* Prototypes for static functions. */ |
898 | static void record_conversion_result P_ ((struct coding_system *coding, | |
899 | enum coding_result_code result)); | |
900 | static int detect_coding_utf_8 P_ ((struct coding_system *, | |
901 | struct coding_detection_info *info)); | |
902 | static void decode_coding_utf_8 P_ ((struct coding_system *)); | |
903 | static int encode_coding_utf_8 P_ ((struct coding_system *)); | |
904 | ||
905 | static int detect_coding_utf_16 P_ ((struct coding_system *, | |
906 | struct coding_detection_info *info)); | |
907 | static void decode_coding_utf_16 P_ ((struct coding_system *)); | |
908 | static int encode_coding_utf_16 P_ ((struct coding_system *)); | |
909 | ||
910 | static int detect_coding_iso_2022 P_ ((struct coding_system *, | |
911 | struct coding_detection_info *info)); | |
912 | static void decode_coding_iso_2022 P_ ((struct coding_system *)); | |
913 | static int encode_coding_iso_2022 P_ ((struct coding_system *)); | |
914 | ||
915 | static int detect_coding_emacs_mule P_ ((struct coding_system *, | |
916 | struct coding_detection_info *info)); | |
917 | static void decode_coding_emacs_mule P_ ((struct coding_system *)); | |
918 | static int encode_coding_emacs_mule P_ ((struct coding_system *)); | |
919 | ||
920 | static int detect_coding_sjis P_ ((struct coding_system *, | |
921 | struct coding_detection_info *info)); | |
922 | static void decode_coding_sjis P_ ((struct coding_system *)); | |
923 | static int encode_coding_sjis P_ ((struct coding_system *)); | |
924 | ||
925 | static int detect_coding_big5 P_ ((struct coding_system *, | |
926 | struct coding_detection_info *info)); | |
927 | static void decode_coding_big5 P_ ((struct coding_system *)); | |
928 | static int encode_coding_big5 P_ ((struct coding_system *)); | |
929 | ||
930 | static int detect_coding_ccl P_ ((struct coding_system *, | |
931 | struct coding_detection_info *info)); | |
932 | static void decode_coding_ccl P_ ((struct coding_system *)); | |
933 | static int encode_coding_ccl P_ ((struct coding_system *)); | |
934 | ||
935 | static void decode_coding_raw_text P_ ((struct coding_system *)); | |
936 | static int encode_coding_raw_text P_ ((struct coding_system *)); | |
937 | ||
938 | static void coding_set_source P_ ((struct coding_system *)); | |
939 | static void coding_set_destination P_ ((struct coding_system *)); | |
940 | static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT)); | |
941 | static void coding_alloc_by_making_gap P_ ((struct coding_system *, | |
287c57d7 | 942 | EMACS_INT, EMACS_INT)); |
f6cbaf43 KH |
943 | static unsigned char *alloc_destination P_ ((struct coding_system *, |
944 | EMACS_INT, unsigned char *)); | |
945 | static void setup_iso_safe_charsets P_ ((Lisp_Object)); | |
946 | static unsigned char *encode_designation_at_bol P_ ((struct coding_system *, | |
947 | int *, int *, | |
948 | unsigned char *)); | |
949 | static int detect_eol P_ ((const unsigned char *, | |
950 | EMACS_INT, enum coding_category)); | |
951 | static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int)); | |
952 | static void decode_eol P_ ((struct coding_system *)); | |
953 | static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *)); | |
e951386e | 954 | static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *)); |
f6cbaf43 | 955 | static int produce_chars P_ ((struct coding_system *, Lisp_Object, int)); |
f6cbaf43 KH |
956 | static INLINE void produce_charset P_ ((struct coding_system *, int *, |
957 | EMACS_INT)); | |
958 | static void produce_annotation P_ ((struct coding_system *, EMACS_INT)); | |
959 | static int decode_coding P_ ((struct coding_system *)); | |
960 | static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT, | |
3ed051d4 | 961 | struct coding_system *, |
f6cbaf43 KH |
962 | int *, EMACS_INT *)); |
963 | static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT, | |
964 | struct coding_system *, | |
965 | int *, EMACS_INT *)); | |
966 | static void consume_chars P_ ((struct coding_system *, Lisp_Object, int)); | |
967 | static int encode_coding P_ ((struct coding_system *)); | |
968 | static Lisp_Object make_conversion_work_buffer P_ ((int)); | |
969 | static Lisp_Object code_conversion_restore P_ ((Lisp_Object)); | |
970 | static INLINE int char_encodable_p P_ ((int, Lisp_Object)); | |
971 | static Lisp_Object make_subsidiaries P_ ((Lisp_Object)); | |
972 | ||
065e3595 KH |
973 | static void |
974 | record_conversion_result (struct coding_system *coding, | |
975 | enum coding_result_code result) | |
976 | { | |
977 | coding->result = result; | |
978 | switch (result) | |
979 | { | |
980 | case CODING_RESULT_INSUFFICIENT_SRC: | |
981 | Vlast_code_conversion_error = Qinsufficient_source; | |
982 | break; | |
983 | case CODING_RESULT_INCONSISTENT_EOL: | |
984 | Vlast_code_conversion_error = Qinconsistent_eol; | |
985 | break; | |
986 | case CODING_RESULT_INVALID_SRC: | |
987 | Vlast_code_conversion_error = Qinvalid_source; | |
988 | break; | |
989 | case CODING_RESULT_INTERRUPT: | |
990 | Vlast_code_conversion_error = Qinterrupted; | |
991 | break; | |
992 | case CODING_RESULT_INSUFFICIENT_MEM: | |
993 | Vlast_code_conversion_error = Qinsufficient_memory; | |
994 | break; | |
35befdaa KH |
995 | default: |
996 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
997 | } |
998 | } | |
999 | ||
df7492f9 KH |
1000 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
1001 | do { \ | |
1002 | charset_map_loaded = 0; \ | |
1003 | c = DECODE_CHAR (charset, code); \ | |
1004 | if (charset_map_loaded) \ | |
1005 | { \ | |
8f924df7 | 1006 | const unsigned char *orig = coding->source; \ |
df7492f9 KH |
1007 | EMACS_INT offset; \ |
1008 | \ | |
1009 | coding_set_source (coding); \ | |
1010 | offset = coding->source - orig; \ | |
1011 | src += offset; \ | |
1012 | src_base += offset; \ | |
1013 | src_end += offset; \ | |
1014 | } \ | |
aa72b389 KH |
1015 | } while (0) |
1016 | ||
1017 | ||
119852e7 KH |
1018 | /* If there are at least BYTES length of room at dst, allocate memory |
1019 | for coding->destination and update dst and dst_end. We don't have | |
1020 | to take care of coding->source which will be relocated. It is | |
1021 | handled by calling coding_set_source in encode_coding. */ | |
1022 | ||
df7492f9 KH |
1023 | #define ASSURE_DESTINATION(bytes) \ |
1024 | do { \ | |
1025 | if (dst + (bytes) >= dst_end) \ | |
1026 | { \ | |
1027 | int more_bytes = charbuf_end - charbuf + (bytes); \ | |
1028 | \ | |
1029 | dst = alloc_destination (coding, more_bytes, dst); \ | |
1030 | dst_end = coding->destination + coding->dst_bytes; \ | |
1031 | } \ | |
1032 | } while (0) | |
aa72b389 | 1033 | |
aa72b389 | 1034 | |
db274c7a KH |
1035 | /* Store multibyte form of the character C in P, and advance P to the |
1036 | end of the multibyte form. This is like CHAR_STRING_ADVANCE but it | |
1037 | never calls MAYBE_UNIFY_CHAR. */ | |
1038 | ||
1039 | #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \ | |
1040 | do { \ | |
1041 | if ((c) <= MAX_1_BYTE_CHAR) \ | |
1042 | *(p)++ = (c); \ | |
1043 | else if ((c) <= MAX_2_BYTE_CHAR) \ | |
1044 | *(p)++ = (0xC0 | ((c) >> 6)), \ | |
1045 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
1046 | else if ((c) <= MAX_3_BYTE_CHAR) \ | |
1047 | *(p)++ = (0xE0 | ((c) >> 12)), \ | |
1048 | *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \ | |
1049 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
1050 | else if ((c) <= MAX_4_BYTE_CHAR) \ | |
1051 | *(p)++ = (0xF0 | (c >> 18)), \ | |
1052 | *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \ | |
1053 | *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \ | |
1054 | *(p)++ = (0x80 | (c & 0x3F)); \ | |
1055 | else if ((c) <= MAX_5_BYTE_CHAR) \ | |
1056 | *(p)++ = 0xF8, \ | |
1057 | *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \ | |
1058 | *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \ | |
1059 | *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \ | |
1060 | *(p)++ = (0x80 | (c & 0x3F)); \ | |
1061 | else \ | |
1062 | (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \ | |
1063 | } while (0) | |
1064 | ||
1065 | ||
1066 | /* Return the character code of character whose multibyte form is at | |
1067 | P, and advance P to the end of the multibyte form. This is like | |
1068 | STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */ | |
1069 | ||
1070 | #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \ | |
1071 | (!((p)[0] & 0x80) \ | |
1072 | ? *(p)++ \ | |
1073 | : ! ((p)[0] & 0x20) \ | |
1074 | ? ((p) += 2, \ | |
1075 | ((((p)[-2] & 0x1F) << 6) \ | |
1076 | | ((p)[-1] & 0x3F) \ | |
1077 | | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \ | |
1078 | : ! ((p)[0] & 0x10) \ | |
1079 | ? ((p) += 3, \ | |
1080 | ((((p)[-3] & 0x0F) << 12) \ | |
1081 | | (((p)[-2] & 0x3F) << 6) \ | |
1082 | | ((p)[-1] & 0x3F))) \ | |
1083 | : ! ((p)[0] & 0x08) \ | |
1084 | ? ((p) += 4, \ | |
1085 | ((((p)[-4] & 0xF) << 18) \ | |
1086 | | (((p)[-3] & 0x3F) << 12) \ | |
1087 | | (((p)[-2] & 0x3F) << 6) \ | |
1088 | | ((p)[-1] & 0x3F))) \ | |
1089 | : ((p) += 5, \ | |
1090 | ((((p)[-4] & 0x3F) << 18) \ | |
1091 | | (((p)[-3] & 0x3F) << 12) \ | |
1092 | | (((p)[-2] & 0x3F) << 6) \ | |
1093 | | ((p)[-1] & 0x3F)))) | |
1094 | ||
aa72b389 | 1095 | |
df7492f9 KH |
1096 | static void |
1097 | coding_set_source (coding) | |
aa72b389 | 1098 | struct coding_system *coding; |
aa72b389 | 1099 | { |
df7492f9 KH |
1100 | if (BUFFERP (coding->src_object)) |
1101 | { | |
2cb26057 | 1102 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 1103 | |
df7492f9 | 1104 | if (coding->src_pos < 0) |
2cb26057 | 1105 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 1106 | else |
2cb26057 | 1107 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 1108 | } |
df7492f9 | 1109 | else if (STRINGP (coding->src_object)) |
aa72b389 | 1110 | { |
8f924df7 | 1111 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 1112 | } |
df7492f9 KH |
1113 | else |
1114 | /* Otherwise, the source is C string and is never relocated | |
1115 | automatically. Thus we don't have to update anything. */ | |
1116 | ; | |
1117 | } | |
aa72b389 | 1118 | |
df7492f9 KH |
1119 | static void |
1120 | coding_set_destination (coding) | |
1121 | struct coding_system *coding; | |
1122 | { | |
1123 | if (BUFFERP (coding->dst_object)) | |
aa72b389 | 1124 | { |
df7492f9 | 1125 | if (coding->src_pos < 0) |
aa72b389 | 1126 | { |
13818c30 | 1127 | coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE; |
28f67a95 KH |
1128 | coding->dst_bytes = (GAP_END_ADDR |
1129 | - (coding->src_bytes - coding->consumed) | |
1130 | - coding->destination); | |
aa72b389 | 1131 | } |
df7492f9 | 1132 | else |
28f67a95 KH |
1133 | { |
1134 | /* We are sure that coding->dst_pos_byte is before the gap | |
1135 | of the buffer. */ | |
1136 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
13818c30 | 1137 | + coding->dst_pos_byte - BEG_BYTE); |
28f67a95 KH |
1138 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) |
1139 | - coding->destination); | |
1140 | } | |
df7492f9 KH |
1141 | } |
1142 | else | |
1143 | /* Otherwise, the destination is C string and is never relocated | |
1144 | automatically. Thus we don't have to update anything. */ | |
1145 | ; | |
1146 | } | |
1147 | ||
1148 | ||
1149 | static void | |
1150 | coding_alloc_by_realloc (coding, bytes) | |
1151 | struct coding_system *coding; | |
1152 | EMACS_INT bytes; | |
1153 | { | |
1154 | coding->destination = (unsigned char *) xrealloc (coding->destination, | |
1155 | coding->dst_bytes + bytes); | |
1156 | coding->dst_bytes += bytes; | |
1157 | } | |
1158 | ||
1159 | static void | |
db274c7a | 1160 | coding_alloc_by_making_gap (coding, gap_head_used, bytes) |
df7492f9 | 1161 | struct coding_system *coding; |
db274c7a | 1162 | EMACS_INT gap_head_used, bytes; |
df7492f9 | 1163 | { |
db274c7a | 1164 | if (EQ (coding->src_object, coding->dst_object)) |
df7492f9 | 1165 | { |
db274c7a KH |
1166 | /* The gap may contain the produced data at the head and not-yet |
1167 | consumed data at the tail. To preserve those data, we at | |
1168 | first make the gap size to zero, then increase the gap | |
1169 | size. */ | |
1170 | EMACS_INT add = GAP_SIZE; | |
1171 | ||
1172 | GPT += gap_head_used, GPT_BYTE += gap_head_used; | |
1173 | GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
df7492f9 KH |
1174 | make_gap (bytes); |
1175 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
db274c7a | 1176 | GPT -= gap_head_used, GPT_BYTE -= gap_head_used; |
df7492f9 | 1177 | } |
730fff51 | 1178 | else |
df7492f9 | 1179 | { |
2c78b7e1 KH |
1180 | Lisp_Object this_buffer; |
1181 | ||
1182 | this_buffer = Fcurrent_buffer (); | |
df7492f9 KH |
1183 | set_buffer_internal (XBUFFER (coding->dst_object)); |
1184 | make_gap (bytes); | |
1185 | set_buffer_internal (XBUFFER (this_buffer)); | |
aa72b389 | 1186 | } |
df7492f9 | 1187 | } |
8f924df7 | 1188 | |
df7492f9 KH |
1189 | |
1190 | static unsigned char * | |
1191 | alloc_destination (coding, nbytes, dst) | |
1192 | struct coding_system *coding; | |
3e139625 | 1193 | EMACS_INT nbytes; |
df7492f9 KH |
1194 | unsigned char *dst; |
1195 | { | |
1196 | EMACS_INT offset = dst - coding->destination; | |
1197 | ||
1198 | if (BUFFERP (coding->dst_object)) | |
db274c7a KH |
1199 | { |
1200 | struct buffer *buf = XBUFFER (coding->dst_object); | |
1201 | ||
1202 | coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes); | |
1203 | } | |
aa72b389 | 1204 | else |
df7492f9 | 1205 | coding_alloc_by_realloc (coding, nbytes); |
065e3595 | 1206 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1207 | coding_set_destination (coding); |
1208 | dst = coding->destination + offset; | |
1209 | return dst; | |
1210 | } | |
aa72b389 | 1211 | |
ff0dacd7 KH |
1212 | /** Macros for annotations. */ |
1213 | ||
ff0dacd7 KH |
1214 | /* An annotation data is stored in the array coding->charbuf in this |
1215 | format: | |
69a80ea3 | 1216 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1217 | LENGTH is the number of elements in the annotation. |
1218 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1219 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1220 | |
1221 | The format of the following elements depend on ANNOTATION_MASK. | |
1222 | ||
1223 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1224 | follows: | |
e951386e KH |
1225 | ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ] |
1226 | ||
1227 | NBYTES is the number of bytes specified in the header part of | |
1228 | old-style emacs-mule encoding, or 0 for the other kind of | |
1229 | composition. | |
1230 | ||
ff0dacd7 | 1231 | METHOD is one of enum composition_method. |
e951386e | 1232 | |
ff0dacd7 KH |
1233 | Optionnal COMPOSITION-COMPONENTS are characters and composition |
1234 | rules. | |
1235 | ||
1236 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
e951386e KH |
1237 | follows. |
1238 | ||
1239 | If ANNOTATION_MASK is 0, this annotation is just a space holder to | |
1240 | recover from an invalid annotation, and should be skipped by | |
1241 | produce_annotation. */ | |
1242 | ||
1243 | /* Maximum length of the header of annotation data. */ | |
1244 | #define MAX_ANNOTATION_LENGTH 5 | |
ff0dacd7 | 1245 | |
69a80ea3 | 1246 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1247 | do { \ |
1248 | *(buf)++ = -(len); \ | |
1249 | *(buf)++ = (mask); \ | |
69a80ea3 | 1250 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1251 | coding->annotated = 1; \ |
1252 | } while (0); | |
1253 | ||
e951386e | 1254 | #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \ |
69a80ea3 | 1255 | do { \ |
e951386e KH |
1256 | ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ |
1257 | *buf++ = nbytes; \ | |
69a80ea3 | 1258 | *buf++ = method; \ |
ff0dacd7 KH |
1259 | } while (0) |
1260 | ||
1261 | ||
69a80ea3 KH |
1262 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1263 | do { \ | |
1264 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1265 | *buf++ = id; \ | |
ff0dacd7 KH |
1266 | } while (0) |
1267 | ||
df7492f9 KH |
1268 | \f |
1269 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1270 | ||
1271 | ||
1272 | ||
1273 | \f | |
1274 | /*** 3. UTF-8 ***/ | |
1275 | ||
1276 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1277 | Check if a text is encoded in UTF-8. If it is, return 1, else |
1278 | return 0. */ | |
df7492f9 KH |
1279 | |
1280 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1281 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1282 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1283 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1284 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1285 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1286 | ||
a470d443 KH |
1287 | #define UTF_BOM 0xFEFF |
1288 | #define UTF_8_BOM_1 0xEF | |
1289 | #define UTF_8_BOM_2 0xBB | |
1290 | #define UTF_8_BOM_3 0xBF | |
1291 | ||
df7492f9 | 1292 | static int |
ff0dacd7 | 1293 | detect_coding_utf_8 (coding, detect_info) |
df7492f9 | 1294 | struct coding_system *coding; |
ff0dacd7 | 1295 | struct coding_detection_info *detect_info; |
df7492f9 | 1296 | { |
065e3595 | 1297 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1298 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1299 | int multibytep = coding->src_multibyte; |
1300 | int consumed_chars = 0; | |
a470d443 | 1301 | int bom_found = 0; |
df7492f9 KH |
1302 | int found = 0; |
1303 | ||
ff0dacd7 | 1304 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1305 | /* A coding system of this category is always ASCII compatible. */ |
1306 | src += coding->head_ascii; | |
1307 | ||
1308 | while (1) | |
aa72b389 | 1309 | { |
df7492f9 | 1310 | int c, c1, c2, c3, c4; |
aa72b389 | 1311 | |
065e3595 | 1312 | src_base = src; |
df7492f9 | 1313 | ONE_MORE_BYTE (c); |
065e3595 | 1314 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
df7492f9 KH |
1315 | continue; |
1316 | ONE_MORE_BYTE (c1); | |
065e3595 | 1317 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1318 | break; |
1319 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1320 | { |
a470d443 | 1321 | found = 1; |
df7492f9 | 1322 | continue; |
aa72b389 | 1323 | } |
df7492f9 | 1324 | ONE_MORE_BYTE (c2); |
065e3595 | 1325 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1326 | break; |
1327 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1328 | { |
a470d443 KH |
1329 | found = 1; |
1330 | if (src_base == coding->source | |
1331 | && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3) | |
1332 | bom_found = 1; | |
df7492f9 | 1333 | continue; |
aa72b389 | 1334 | } |
df7492f9 | 1335 | ONE_MORE_BYTE (c3); |
065e3595 | 1336 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1337 | break; |
1338 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1339 | { |
a470d443 | 1340 | found = 1; |
df7492f9 KH |
1341 | continue; |
1342 | } | |
1343 | ONE_MORE_BYTE (c4); | |
065e3595 | 1344 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1345 | break; |
1346 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1347 | { | |
a470d443 | 1348 | found = 1; |
df7492f9 KH |
1349 | continue; |
1350 | } | |
1351 | break; | |
aa72b389 | 1352 | } |
ff0dacd7 | 1353 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1354 | return 0; |
aa72b389 | 1355 | |
df7492f9 | 1356 | no_more_source: |
065e3595 | 1357 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1358 | { |
ff0dacd7 | 1359 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1360 | return 0; |
aa72b389 | 1361 | } |
a470d443 KH |
1362 | if (bom_found) |
1363 | { | |
1364 | /* The first character 0xFFFE doesn't necessarily mean a BOM. */ | |
1365 | detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG; | |
1366 | } | |
1367 | else | |
1368 | { | |
1369 | detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG; | |
0e17387a KH |
1370 | if (found) |
1371 | detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG; | |
a470d443 | 1372 | } |
ff0dacd7 | 1373 | return 1; |
aa72b389 KH |
1374 | } |
1375 | ||
4ed46869 | 1376 | |
b73bfc1c | 1377 | static void |
df7492f9 | 1378 | decode_coding_utf_8 (coding) |
b73bfc1c | 1379 | struct coding_system *coding; |
b73bfc1c | 1380 | { |
8f924df7 KH |
1381 | const unsigned char *src = coding->source + coding->consumed; |
1382 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1383 | const unsigned char *src_base; | |
69a80ea3 KH |
1384 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1385 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
453b38f0 | 1386 | int consumed_chars = 0, consumed_chars_base = 0; |
df7492f9 | 1387 | int multibytep = coding->src_multibyte; |
a470d443 | 1388 | enum utf_bom_type bom = CODING_UTF_8_BOM (coding); |
24a73b0a | 1389 | Lisp_Object attr, charset_list; |
0a9564cb EZ |
1390 | int eol_crlf = |
1391 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 1392 | int byte_after_cr = -1; |
4ed46869 | 1393 | |
24a73b0a | 1394 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1395 | |
a470d443 KH |
1396 | if (bom != utf_without_bom) |
1397 | { | |
1398 | int c1, c2, c3; | |
1399 | ||
1400 | src_base = src; | |
1401 | ONE_MORE_BYTE (c1); | |
1402 | if (! UTF_8_3_OCTET_LEADING_P (c1)) | |
1403 | src = src_base; | |
1404 | else | |
1405 | { | |
159bd5a2 | 1406 | ONE_MORE_BYTE (c2); |
a470d443 KH |
1407 | if (! UTF_8_EXTRA_OCTET_P (c2)) |
1408 | src = src_base; | |
1409 | else | |
1410 | { | |
159bd5a2 | 1411 | ONE_MORE_BYTE (c3); |
a470d443 KH |
1412 | if (! UTF_8_EXTRA_OCTET_P (c3)) |
1413 | src = src_base; | |
1414 | else | |
1415 | { | |
1416 | if ((c1 != UTF_8_BOM_1) | |
1417 | || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3)) | |
1418 | src = src_base; | |
1419 | else | |
1420 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1421 | } | |
1422 | } | |
1423 | } | |
1424 | } | |
1425 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1426 | ||
1427 | ||
1428 | ||
df7492f9 | 1429 | while (1) |
b73bfc1c | 1430 | { |
df7492f9 | 1431 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1432 | |
df7492f9 KH |
1433 | src_base = src; |
1434 | consumed_chars_base = consumed_chars; | |
4af310db | 1435 | |
df7492f9 | 1436 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1437 | { |
1438 | if (byte_after_cr >= 0) | |
1439 | src_base--; | |
1440 | break; | |
1441 | } | |
df7492f9 | 1442 | |
119852e7 KH |
1443 | if (byte_after_cr >= 0) |
1444 | c1 = byte_after_cr, byte_after_cr = -1; | |
1445 | else | |
1446 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1447 | if (c1 < 0) |
1448 | { | |
1449 | c = - c1; | |
1450 | } | |
1451 | else if (UTF_8_1_OCTET_P(c1)) | |
df7492f9 | 1452 | { |
119852e7 KH |
1453 | if (eol_crlf && c1 == '\r') |
1454 | ONE_MORE_BYTE (byte_after_cr); | |
df7492f9 | 1455 | c = c1; |
4af310db | 1456 | } |
df7492f9 | 1457 | else |
4af310db | 1458 | { |
df7492f9 | 1459 | ONE_MORE_BYTE (c2); |
065e3595 | 1460 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1461 | goto invalid_code; |
1462 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1463 | { |
b0edb2c5 DL |
1464 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1465 | /* Reject overlong sequences here and below. Encoders | |
1466 | producing them are incorrect, they can be misleading, | |
1467 | and they mess up read/write invariance. */ | |
1468 | if (c < 128) | |
1469 | goto invalid_code; | |
4af310db | 1470 | } |
df7492f9 | 1471 | else |
aa72b389 | 1472 | { |
df7492f9 | 1473 | ONE_MORE_BYTE (c3); |
065e3595 | 1474 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1475 | goto invalid_code; |
1476 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1477 | { |
1478 | c = (((c1 & 0xF) << 12) | |
1479 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1480 | if (c < 0x800 |
1481 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1482 | goto invalid_code; |
1483 | } | |
df7492f9 KH |
1484 | else |
1485 | { | |
1486 | ONE_MORE_BYTE (c4); | |
065e3595 | 1487 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1488 | goto invalid_code; |
1489 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1490 | { |
df7492f9 KH |
1491 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1492 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1493 | if (c < 0x10000) |
1494 | goto invalid_code; | |
1495 | } | |
df7492f9 KH |
1496 | else |
1497 | { | |
1498 | ONE_MORE_BYTE (c5); | |
065e3595 | 1499 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1500 | goto invalid_code; |
1501 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1502 | { | |
1503 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1504 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1505 | | (c5 & 0x3F)); | |
b0edb2c5 | 1506 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1507 | goto invalid_code; |
1508 | } | |
1509 | else | |
1510 | goto invalid_code; | |
1511 | } | |
1512 | } | |
aa72b389 | 1513 | } |
b73bfc1c | 1514 | } |
df7492f9 KH |
1515 | |
1516 | *charbuf++ = c; | |
1517 | continue; | |
1518 | ||
1519 | invalid_code: | |
1520 | src = src_base; | |
1521 | consumed_chars = consumed_chars_base; | |
1522 | ONE_MORE_BYTE (c); | |
1523 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1524 | coding->errors++; | |
aa72b389 KH |
1525 | } |
1526 | ||
df7492f9 KH |
1527 | no_more_source: |
1528 | coding->consumed_char += consumed_chars_base; | |
1529 | coding->consumed = src_base - coding->source; | |
1530 | coding->charbuf_used = charbuf - coding->charbuf; | |
1531 | } | |
1532 | ||
1533 | ||
1534 | static int | |
1535 | encode_coding_utf_8 (coding) | |
1536 | struct coding_system *coding; | |
1537 | { | |
1538 | int multibytep = coding->dst_multibyte; | |
1539 | int *charbuf = coding->charbuf; | |
1540 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1541 | unsigned char *dst = coding->destination + coding->produced; | |
1542 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
e19c3639 | 1543 | int produced_chars = 0; |
df7492f9 KH |
1544 | int c; |
1545 | ||
a470d443 KH |
1546 | if (CODING_UTF_8_BOM (coding) == utf_with_bom) |
1547 | { | |
1548 | ASSURE_DESTINATION (3); | |
1549 | EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3); | |
1550 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1551 | } | |
1552 | ||
df7492f9 | 1553 | if (multibytep) |
aa72b389 | 1554 | { |
df7492f9 KH |
1555 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1556 | ||
1557 | while (charbuf < charbuf_end) | |
b73bfc1c | 1558 | { |
df7492f9 | 1559 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1560 | |
df7492f9 KH |
1561 | ASSURE_DESTINATION (safe_room); |
1562 | c = *charbuf++; | |
28f67a95 KH |
1563 | if (CHAR_BYTE8_P (c)) |
1564 | { | |
1565 | c = CHAR_TO_BYTE8 (c); | |
1566 | EMIT_ONE_BYTE (c); | |
1567 | } | |
1568 | else | |
1569 | { | |
db274c7a | 1570 | CHAR_STRING_ADVANCE_NO_UNIFY (c, pend); |
28f67a95 KH |
1571 | for (p = str; p < pend; p++) |
1572 | EMIT_ONE_BYTE (*p); | |
1573 | } | |
b73bfc1c | 1574 | } |
aa72b389 | 1575 | } |
df7492f9 KH |
1576 | else |
1577 | { | |
1578 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1579 | ||
1580 | while (charbuf < charbuf_end) | |
b73bfc1c | 1581 | { |
df7492f9 KH |
1582 | ASSURE_DESTINATION (safe_room); |
1583 | c = *charbuf++; | |
f03caae0 KH |
1584 | if (CHAR_BYTE8_P (c)) |
1585 | *dst++ = CHAR_TO_BYTE8 (c); | |
1586 | else | |
db274c7a | 1587 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
df7492f9 | 1588 | produced_chars++; |
4ed46869 KH |
1589 | } |
1590 | } | |
065e3595 | 1591 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1592 | coding->produced_char += produced_chars; |
1593 | coding->produced = dst - coding->destination; | |
1594 | return 0; | |
4ed46869 KH |
1595 | } |
1596 | ||
b73bfc1c | 1597 | |
df7492f9 | 1598 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
1599 | Check if a text is encoded in one of UTF-16 based coding systems. |
1600 | If it is, return 1, else return 0. */ | |
aa72b389 | 1601 | |
df7492f9 KH |
1602 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1603 | (((val) & 0xFC00) == 0xD800) | |
1604 | ||
1605 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1606 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1607 | |
df7492f9 KH |
1608 | #define UTF_16_INVALID_P(val) \ |
1609 | (((val) == 0xFFFE) \ | |
1610 | || ((val) == 0xFFFF) \ | |
1611 | || UTF_16_LOW_SURROGATE_P (val)) | |
aa72b389 | 1612 | |
aa72b389 | 1613 | |
df7492f9 | 1614 | static int |
ff0dacd7 | 1615 | detect_coding_utf_16 (coding, detect_info) |
aa72b389 | 1616 | struct coding_system *coding; |
ff0dacd7 | 1617 | struct coding_detection_info *detect_info; |
aa72b389 | 1618 | { |
8f924df7 KH |
1619 | const unsigned char *src = coding->source, *src_base = src; |
1620 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
1621 | int multibytep = coding->src_multibyte; |
1622 | int consumed_chars = 0; | |
1623 | int c1, c2; | |
aa72b389 | 1624 | |
ff0dacd7 | 1625 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1626 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1627 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1628 | { |
1629 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1630 | return 0; | |
1631 | } | |
24a73b0a | 1632 | |
f56a4450 | 1633 | TWO_MORE_BYTES (c1, c2); |
df7492f9 | 1634 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1635 | { |
b49a1807 KH |
1636 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1637 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1638 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1639 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1640 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1641 | } |
df7492f9 | 1642 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1643 | { |
b49a1807 KH |
1644 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1645 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1646 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1647 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1648 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1649 | } | |
220eeac9 | 1650 | else if (c2 < 0) |
f56a4450 KH |
1651 | { |
1652 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1653 | return 0; | |
1654 | } | |
2f3cbb32 | 1655 | else |
24a73b0a | 1656 | { |
2f3cbb32 KH |
1657 | /* We check the dispersion of Eth and Oth bytes where E is even and |
1658 | O is odd. If both are high, we assume binary data.*/ | |
1659 | unsigned char e[256], o[256]; | |
1660 | unsigned e_num = 1, o_num = 1; | |
1661 | ||
1662 | memset (e, 0, 256); | |
1663 | memset (o, 0, 256); | |
1664 | e[c1] = 1; | |
1665 | o[c2] = 1; | |
1666 | ||
24a73b0a KH |
1667 | detect_info->rejected |
1668 | |= (CATEGORY_MASK_UTF_16_BE | CATEGORY_MASK_UTF_16_LE); | |
2f3cbb32 KH |
1669 | |
1670 | while (1) | |
1671 | { | |
f56a4450 | 1672 | TWO_MORE_BYTES (c1, c2); |
220eeac9 | 1673 | if (c2 < 0) |
f56a4450 | 1674 | break; |
2f3cbb32 KH |
1675 | if (! e[c1]) |
1676 | { | |
1677 | e[c1] = 1; | |
1678 | e_num++; | |
1679 | if (e_num >= 128) | |
1680 | break; | |
1681 | } | |
1682 | if (! o[c2]) | |
1683 | { | |
1684 | o[c1] = 1; | |
1685 | o_num++; | |
1686 | if (o_num >= 128) | |
1687 | break; | |
1688 | } | |
1689 | } | |
1690 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1691 | return 0; | |
ff0dacd7 | 1692 | } |
2f3cbb32 | 1693 | |
df7492f9 | 1694 | no_more_source: |
ff0dacd7 | 1695 | return 1; |
df7492f9 | 1696 | } |
aa72b389 | 1697 | |
df7492f9 KH |
1698 | static void |
1699 | decode_coding_utf_16 (coding) | |
1700 | struct coding_system *coding; | |
1701 | { | |
8f924df7 KH |
1702 | const unsigned char *src = coding->source + coding->consumed; |
1703 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1704 | const unsigned char *src_base; | |
69a80ea3 KH |
1705 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1706 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
3a8406e1 | 1707 | int consumed_chars = 0, consumed_chars_base = 0; |
df7492f9 | 1708 | int multibytep = coding->src_multibyte; |
a470d443 | 1709 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1710 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); |
1711 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
24a73b0a | 1712 | Lisp_Object attr, charset_list; |
0a9564cb EZ |
1713 | int eol_crlf = |
1714 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 1715 | int byte_after_cr1 = -1, byte_after_cr2 = -1; |
df7492f9 | 1716 | |
24a73b0a | 1717 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1718 | |
a470d443 | 1719 | if (bom == utf_with_bom) |
aa72b389 | 1720 | { |
df7492f9 | 1721 | int c, c1, c2; |
4af310db | 1722 | |
aa72b389 | 1723 | src_base = src; |
df7492f9 KH |
1724 | ONE_MORE_BYTE (c1); |
1725 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1726 | c = (c1 << 8) | c2; |
aa72b389 | 1727 | |
b49a1807 KH |
1728 | if (endian == utf_16_big_endian |
1729 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1730 | { |
b49a1807 KH |
1731 | /* The first two bytes are not BOM. Treat them as bytes |
1732 | for a normal character. */ | |
1733 | src = src_base; | |
1734 | coding->errors++; | |
aa72b389 | 1735 | } |
a470d443 | 1736 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
b49a1807 | 1737 | } |
a470d443 | 1738 | else if (bom == utf_detect_bom) |
b49a1807 KH |
1739 | { |
1740 | /* We have already tried to detect BOM and failed in | |
1741 | detect_coding. */ | |
a470d443 | 1742 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 | 1743 | } |
aa72b389 | 1744 | |
df7492f9 KH |
1745 | while (1) |
1746 | { | |
1747 | int c, c1, c2; | |
1748 | ||
1749 | src_base = src; | |
1750 | consumed_chars_base = consumed_chars; | |
1751 | ||
1752 | if (charbuf + 2 >= charbuf_end) | |
b71f6f73 KH |
1753 | { |
1754 | if (byte_after_cr1 >= 0) | |
1755 | src_base -= 2; | |
1756 | break; | |
1757 | } | |
df7492f9 | 1758 | |
119852e7 KH |
1759 | if (byte_after_cr1 >= 0) |
1760 | c1 = byte_after_cr1, byte_after_cr1 = -1; | |
1761 | else | |
1762 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1763 | if (c1 < 0) |
1764 | { | |
1765 | *charbuf++ = -c1; | |
1766 | continue; | |
1767 | } | |
119852e7 KH |
1768 | if (byte_after_cr2 >= 0) |
1769 | c2 = byte_after_cr2, byte_after_cr2 = -1; | |
1770 | else | |
1771 | ONE_MORE_BYTE (c2); | |
065e3595 KH |
1772 | if (c2 < 0) |
1773 | { | |
1774 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1775 | *charbuf++ = -c2; | |
1776 | continue; | |
1777 | } | |
df7492f9 | 1778 | c = (endian == utf_16_big_endian |
e19c3639 | 1779 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
119852e7 | 1780 | |
df7492f9 | 1781 | if (surrogate) |
fd3ae0b9 | 1782 | { |
df7492f9 | 1783 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1784 | { |
df7492f9 KH |
1785 | if (endian == utf_16_big_endian) |
1786 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1787 | else | |
1788 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1789 | *charbuf++ = c1; | |
1790 | *charbuf++ = c2; | |
1791 | coding->errors++; | |
1792 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1793 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1794 | else |
df7492f9 | 1795 | *charbuf++ = c; |
fd3ae0b9 KH |
1796 | } |
1797 | else | |
df7492f9 KH |
1798 | { |
1799 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1800 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1801 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1802 | } |
fd3ae0b9 | 1803 | } |
aa72b389 | 1804 | else |
df7492f9 KH |
1805 | { |
1806 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1807 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1808 | else | |
119852e7 KH |
1809 | { |
1810 | if (eol_crlf && c == '\r') | |
1811 | { | |
1812 | ONE_MORE_BYTE (byte_after_cr1); | |
1813 | ONE_MORE_BYTE (byte_after_cr2); | |
1814 | } | |
1815 | *charbuf++ = c; | |
1816 | } | |
8f924df7 | 1817 | } |
aa72b389 | 1818 | } |
df7492f9 KH |
1819 | |
1820 | no_more_source: | |
1821 | coding->consumed_char += consumed_chars_base; | |
1822 | coding->consumed = src_base - coding->source; | |
1823 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1824 | } |
b73bfc1c | 1825 | |
df7492f9 KH |
1826 | static int |
1827 | encode_coding_utf_16 (coding) | |
1828 | struct coding_system *coding; | |
1829 | { | |
1830 | int multibytep = coding->dst_multibyte; | |
1831 | int *charbuf = coding->charbuf; | |
1832 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1833 | unsigned char *dst = coding->destination + coding->produced; | |
1834 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1835 | int safe_room = 8; | |
a470d443 | 1836 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1837 | int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; |
1838 | int produced_chars = 0; | |
24a73b0a | 1839 | Lisp_Object attrs, charset_list; |
df7492f9 | 1840 | int c; |
4ed46869 | 1841 | |
24a73b0a | 1842 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 1843 | |
a470d443 | 1844 | if (bom != utf_without_bom) |
df7492f9 KH |
1845 | { |
1846 | ASSURE_DESTINATION (safe_room); | |
1847 | if (big_endian) | |
df7492f9 | 1848 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1849 | else |
1850 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
a470d443 | 1851 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 KH |
1852 | } |
1853 | ||
1854 | while (charbuf < charbuf_end) | |
1855 | { | |
1856 | ASSURE_DESTINATION (safe_room); | |
1857 | c = *charbuf++; | |
e19c3639 KH |
1858 | if (c >= MAX_UNICODE_CHAR) |
1859 | c = coding->default_char; | |
df7492f9 KH |
1860 | |
1861 | if (c < 0x10000) | |
1862 | { | |
1863 | if (big_endian) | |
1864 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1865 | else | |
1866 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1867 | } | |
1868 | else | |
1869 | { | |
1870 | int c1, c2; | |
1871 | ||
1872 | c -= 0x10000; | |
1873 | c1 = (c >> 10) + 0xD800; | |
1874 | c2 = (c & 0x3FF) + 0xDC00; | |
1875 | if (big_endian) | |
1876 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1877 | else | |
1878 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1879 | } | |
1880 | } | |
065e3595 | 1881 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1882 | coding->produced = dst - coding->destination; |
1883 | coding->produced_char += produced_chars; | |
1884 | return 0; | |
1885 | } | |
1886 | ||
1887 | \f | |
1888 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1889 | ||
1890 | /* Emacs' internal format for representation of multiple character | |
1891 | sets is a kind of multi-byte encoding, i.e. characters are | |
1892 | represented by variable-length sequences of one-byte codes. | |
1893 | ||
1894 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1895 | represented by one-byte sequences which are their ASCII codes, in | |
1896 | the range 0x00 through 0x7F. | |
1897 | ||
1898 | 8-bit characters of the range 0x80..0x9F are represented by | |
1899 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1900 | code + 0x20). | |
1901 | ||
1902 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1903 | one-byte sequences which are their 8-bit code. | |
1904 | ||
1905 | The other characters are represented by a sequence of `base | |
1906 | leading-code', optional `extended leading-code', and one or two | |
1907 | `position-code's. The length of the sequence is determined by the | |
1908 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1909 | whereas extended leading-code and position-code take the range 0xA0 | |
1910 | through 0xFF. See `charset.h' for more details about leading-code | |
1911 | and position-code. | |
1912 | ||
1913 | --- CODE RANGE of Emacs' internal format --- | |
1914 | character set range | |
1915 | ------------- ----- | |
1916 | ascii 0x00..0x7F | |
1917 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1918 | eight-bit-graphic 0xA0..0xBF | |
1919 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1920 | --------------------------------------------- | |
1921 | ||
1922 | As this is the internal character representation, the format is | |
1923 | usually not used externally (i.e. in a file or in a data sent to a | |
1924 | process). But, it is possible to have a text externally in this | |
1925 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1926 | ||
1927 | In that case, a sequence of one-byte codes has a slightly different | |
1928 | form. | |
1929 | ||
1930 | At first, all characters in eight-bit-control are represented by | |
1931 | one-byte sequences which are their 8-bit code. | |
1932 | ||
1933 | Next, character composition data are represented by the byte | |
1934 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1935 | where, | |
e951386e | 1936 | METHOD is 0xF2 plus one of composition method (enum |
df7492f9 KH |
1937 | composition_method), |
1938 | ||
1939 | BYTES is 0xA0 plus a byte length of this composition data, | |
1940 | ||
e951386e | 1941 | CHARS is 0xA0 plus a number of characters composed by this |
df7492f9 KH |
1942 | data, |
1943 | ||
1944 | COMPONENTs are characters of multibye form or composition | |
1945 | rules encoded by two-byte of ASCII codes. | |
1946 | ||
1947 | In addition, for backward compatibility, the following formats are | |
1948 | also recognized as composition data on decoding. | |
1949 | ||
1950 | 0x80 MSEQ ... | |
1951 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1952 | ||
1953 | Here, | |
1954 | MSEQ is a multibyte form but in these special format: | |
1955 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1956 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1957 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1958 | represents a composition rule. | |
1959 | */ | |
1960 | ||
1961 | char emacs_mule_bytes[256]; | |
1962 | ||
e951386e KH |
1963 | |
1964 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
1965 | Check if a text is encoded in `emacs-mule'. If it is, return 1, | |
1966 | else return 0. */ | |
1967 | ||
1968 | static int | |
1969 | detect_coding_emacs_mule (coding, detect_info) | |
1970 | struct coding_system *coding; | |
1971 | struct coding_detection_info *detect_info; | |
1972 | { | |
1973 | const unsigned char *src = coding->source, *src_base; | |
1974 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1975 | int multibytep = coding->src_multibyte; | |
1976 | int consumed_chars = 0; | |
1977 | int c; | |
1978 | int found = 0; | |
1979 | ||
1980 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; | |
1981 | /* A coding system of this category is always ASCII compatible. */ | |
1982 | src += coding->head_ascii; | |
1983 | ||
1984 | while (1) | |
1985 | { | |
1986 | src_base = src; | |
1987 | ONE_MORE_BYTE (c); | |
1988 | if (c < 0) | |
1989 | continue; | |
1990 | if (c == 0x80) | |
1991 | { | |
1992 | /* Perhaps the start of composite character. We simply skip | |
1993 | it because analyzing it is too heavy for detecting. But, | |
1994 | at least, we check that the composite character | |
1995 | constitutes of more than 4 bytes. */ | |
1996 | const unsigned char *src_base; | |
1997 | ||
1998 | repeat: | |
1999 | src_base = src; | |
2000 | do | |
2001 | { | |
2002 | ONE_MORE_BYTE (c); | |
2003 | } | |
2004 | while (c >= 0xA0); | |
2005 | ||
2006 | if (src - src_base <= 4) | |
2007 | break; | |
2008 | found = CATEGORY_MASK_EMACS_MULE; | |
2009 | if (c == 0x80) | |
2010 | goto repeat; | |
2011 | } | |
2012 | ||
2013 | if (c < 0x80) | |
2014 | { | |
2015 | if (c < 0x20 | |
2016 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
2017 | break; | |
2018 | } | |
2019 | else | |
2020 | { | |
2021 | int more_bytes = emacs_mule_bytes[*src_base] - 1; | |
2022 | ||
2023 | while (more_bytes > 0) | |
2024 | { | |
2025 | ONE_MORE_BYTE (c); | |
2026 | if (c < 0xA0) | |
2027 | { | |
2028 | src--; /* Unread the last byte. */ | |
2029 | break; | |
2030 | } | |
2031 | more_bytes--; | |
2032 | } | |
2033 | if (more_bytes != 0) | |
2034 | break; | |
2035 | found = CATEGORY_MASK_EMACS_MULE; | |
2036 | } | |
2037 | } | |
2038 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
2039 | return 0; | |
2040 | ||
2041 | no_more_source: | |
2042 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) | |
2043 | { | |
2044 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
2045 | return 0; | |
2046 | } | |
2047 | detect_info->found |= found; | |
2048 | return 1; | |
2049 | } | |
2050 | ||
2051 | ||
2052 | /* Parse emacs-mule multibyte sequence at SRC and return the decoded | |
2053 | character. If CMP_STATUS indicates that we must expect MSEQ or | |
2054 | RULE described above, decode it and return the negative value of | |
2055 | the deocded character or rule. If an invalid byte is found, return | |
2056 | -1. If SRC is too short, return -2. */ | |
2057 | ||
df7492f9 | 2058 | int |
e951386e | 2059 | emacs_mule_char (coding, src, nbytes, nchars, id, cmp_status) |
df7492f9 | 2060 | struct coding_system *coding; |
065e3595 | 2061 | const unsigned char *src; |
ff0dacd7 | 2062 | int *nbytes, *nchars, *id; |
e951386e | 2063 | struct composition_status *cmp_status; |
df7492f9 | 2064 | { |
8f924df7 KH |
2065 | const unsigned char *src_end = coding->source + coding->src_bytes; |
2066 | const unsigned char *src_base = src; | |
df7492f9 | 2067 | int multibytep = coding->src_multibyte; |
df7492f9 KH |
2068 | struct charset *charset; |
2069 | unsigned code; | |
2070 | int c; | |
2071 | int consumed_chars = 0; | |
e951386e | 2072 | int mseq_found = 0; |
df7492f9 KH |
2073 | |
2074 | ONE_MORE_BYTE (c); | |
065e3595 | 2075 | if (c < 0) |
df7492f9 | 2076 | { |
065e3595 KH |
2077 | c = -c; |
2078 | charset = emacs_mule_charset[0]; | |
2079 | } | |
2080 | else | |
2081 | { | |
4d41e8b7 KH |
2082 | if (c >= 0xA0) |
2083 | { | |
e951386e KH |
2084 | if (cmp_status->state != COMPOSING_NO |
2085 | && cmp_status->old_form) | |
4d41e8b7 | 2086 | { |
e951386e KH |
2087 | if (cmp_status->state == COMPOSING_CHAR) |
2088 | { | |
2089 | if (c == 0xA0) | |
2090 | { | |
2091 | ONE_MORE_BYTE (c); | |
2092 | c -= 0x80; | |
2093 | if (c < 0) | |
2094 | goto invalid_code; | |
2095 | } | |
2096 | else | |
2097 | c -= 0x20; | |
2098 | mseq_found = 1; | |
2099 | } | |
2100 | else | |
2101 | { | |
2102 | *nbytes = src - src_base; | |
2103 | *nchars = consumed_chars; | |
2104 | return -c; | |
2105 | } | |
4d41e8b7 KH |
2106 | } |
2107 | else | |
e951386e | 2108 | goto invalid_code; |
4d41e8b7 KH |
2109 | } |
2110 | ||
065e3595 | 2111 | switch (emacs_mule_bytes[c]) |
b73bfc1c | 2112 | { |
065e3595 | 2113 | case 2: |
df7492f9 KH |
2114 | if (! (charset = emacs_mule_charset[c])) |
2115 | goto invalid_code; | |
2116 | ONE_MORE_BYTE (c); | |
9ffd559c | 2117 | if (c < 0xA0) |
065e3595 | 2118 | goto invalid_code; |
df7492f9 | 2119 | code = c & 0x7F; |
065e3595 KH |
2120 | break; |
2121 | ||
2122 | case 3: | |
2123 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
2124 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
2125 | { | |
2126 | ONE_MORE_BYTE (c); | |
9ffd559c | 2127 | if (c < 0xA0 || ! (charset = emacs_mule_charset[c])) |
065e3595 KH |
2128 | goto invalid_code; |
2129 | ONE_MORE_BYTE (c); | |
9ffd559c | 2130 | if (c < 0xA0) |
065e3595 KH |
2131 | goto invalid_code; |
2132 | code = c & 0x7F; | |
2133 | } | |
2134 | else | |
2135 | { | |
2136 | if (! (charset = emacs_mule_charset[c])) | |
2137 | goto invalid_code; | |
2138 | ONE_MORE_BYTE (c); | |
9ffd559c | 2139 | if (c < 0xA0) |
065e3595 KH |
2140 | goto invalid_code; |
2141 | code = (c & 0x7F) << 8; | |
2142 | ONE_MORE_BYTE (c); | |
9ffd559c | 2143 | if (c < 0xA0) |
065e3595 KH |
2144 | goto invalid_code; |
2145 | code |= c & 0x7F; | |
2146 | } | |
2147 | break; | |
2148 | ||
2149 | case 4: | |
2150 | ONE_MORE_BYTE (c); | |
2151 | if (c < 0 || ! (charset = emacs_mule_charset[c])) | |
df7492f9 KH |
2152 | goto invalid_code; |
2153 | ONE_MORE_BYTE (c); | |
9ffd559c | 2154 | if (c < 0xA0) |
065e3595 | 2155 | goto invalid_code; |
781d7a48 | 2156 | code = (c & 0x7F) << 8; |
df7492f9 | 2157 | ONE_MORE_BYTE (c); |
9ffd559c | 2158 | if (c < 0xA0) |
065e3595 | 2159 | goto invalid_code; |
df7492f9 | 2160 | code |= c & 0x7F; |
065e3595 | 2161 | break; |
df7492f9 | 2162 | |
065e3595 KH |
2163 | case 1: |
2164 | code = c; | |
2165 | charset = CHARSET_FROM_ID (ASCII_BYTE_P (code) | |
2166 | ? charset_ascii : charset_eight_bit); | |
2167 | break; | |
df7492f9 | 2168 | |
065e3595 KH |
2169 | default: |
2170 | abort (); | |
2171 | } | |
2172 | c = DECODE_CHAR (charset, code); | |
2173 | if (c < 0) | |
2174 | goto invalid_code; | |
df7492f9 | 2175 | } |
df7492f9 KH |
2176 | *nbytes = src - src_base; |
2177 | *nchars = consumed_chars; | |
ff0dacd7 KH |
2178 | if (id) |
2179 | *id = charset->id; | |
e951386e | 2180 | return (mseq_found ? -c : c); |
df7492f9 KH |
2181 | |
2182 | no_more_source: | |
2183 | return -2; | |
2184 | ||
2185 | invalid_code: | |
2186 | return -1; | |
2187 | } | |
2188 | ||
2189 | ||
e951386e | 2190 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
df7492f9 | 2191 | |
e951386e KH |
2192 | /* Handle these composition sequence ('|': the end of header elements, |
2193 | BYTES and CHARS >= 0xA0): | |
df7492f9 | 2194 | |
e951386e KH |
2195 | (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ... |
2196 | (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ... | |
2197 | (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ... | |
df7492f9 | 2198 | |
e951386e KH |
2199 | and these old form: |
2200 | ||
2201 | (4) relative composition: 0x80 | MSEQ ... MSEQ | |
2202 | (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ | |
df7492f9 | 2203 | |
e951386e KH |
2204 | When the starter 0x80 and the following header elements are found, |
2205 | this annotation header is produced. | |
df7492f9 | 2206 | |
e951386e | 2207 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ] |
df7492f9 | 2208 | |
e951386e KH |
2209 | NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5). |
2210 | NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5). | |
df7492f9 | 2211 | |
e951386e KH |
2212 | Then, upon reading the following elements, these codes are produced |
2213 | until the composition end is found: | |
df7492f9 | 2214 | |
e951386e KH |
2215 | (1) CHAR ... CHAR |
2216 | (2) ALT ... ALT CHAR ... CHAR | |
2217 | (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR | |
2218 | (4) CHAR ... CHAR | |
2219 | (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
4ed46869 | 2220 | |
e951386e KH |
2221 | When the composition end is found, LENGTH and NCHARS in the |
2222 | annotation header is updated as below: | |
b73bfc1c | 2223 | |
e951386e KH |
2224 | (1) LENGTH: unchanged, NCHARS: unchanged |
2225 | (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2226 | (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2227 | (4) LENGTH: unchanged, NCHARS: number of CHARs | |
2228 | (5) LENGTH: unchanged, NCHARS: number of CHARs | |
df7492f9 | 2229 | |
e951386e KH |
2230 | If an error is found while composing, the annotation header is |
2231 | changed to the original composition header (plus filler -1s) as | |
2232 | below: | |
2233 | ||
2234 | (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ] | |
2235 | (5) [ 0x80 0xFF -1 -1- -1 ] | |
2236 | ||
2237 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
2238 | byte sequence as below: | |
2239 | o the original byte sequence is B: [ B -1 ] | |
2240 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
2241 | ||
2242 | Most of the routines are implemented by macros because many | |
2243 | variables and labels in the caller decode_coding_emacs_mule must be | |
2244 | accessible, and they are usually called just once (thus doesn't | |
2245 | increase the size of compiled object). */ | |
2246 | ||
2247 | /* Decode a composition rule represented by C as a component of | |
2248 | composition sequence of Emacs 20 style. Set RULE to the decoded | |
2249 | rule. */ | |
2250 | ||
2251 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \ | |
df7492f9 | 2252 | do { \ |
e951386e KH |
2253 | int gref, nref; \ |
2254 | \ | |
4d41e8b7 | 2255 | c -= 0xA0; \ |
df7492f9 KH |
2256 | if (c < 0 || c >= 81) \ |
2257 | goto invalid_code; \ | |
df7492f9 | 2258 | gref = c / 9, nref = c % 9; \ |
e951386e KH |
2259 | if (gref == 4) gref = 10; \ |
2260 | if (nref == 4) nref = 10; \ | |
2261 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
df7492f9 KH |
2262 | } while (0) |
2263 | ||
2264 | ||
e951386e KH |
2265 | /* Decode a composition rule represented by C and the following byte |
2266 | at SRC as a component of composition sequence of Emacs 21 style. | |
2267 | Set RULE to the decoded rule. */ | |
781d7a48 | 2268 | |
e951386e | 2269 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \ |
781d7a48 KH |
2270 | do { \ |
2271 | int gref, nref; \ | |
e951386e KH |
2272 | \ |
2273 | gref = c - 0x20; \ | |
2274 | if (gref < 0 || gref >= 81) \ | |
781d7a48 | 2275 | goto invalid_code; \ |
e951386e KH |
2276 | ONE_MORE_BYTE (c); \ |
2277 | nref = c - 0x20; \ | |
2278 | if (nref < 0 || nref >= 81) \ | |
781d7a48 | 2279 | goto invalid_code; \ |
e951386e | 2280 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ |
781d7a48 KH |
2281 | } while (0) |
2282 | ||
2283 | ||
e951386e KH |
2284 | /* Start of Emacs 21 style format. The first three bytes at SRC are |
2285 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the | |
2286 | byte length of this composition information, CHARS is the number of | |
2287 | characters composed by this composition. */ | |
2288 | ||
2289 | #define DECODE_EMACS_MULE_21_COMPOSITION() \ | |
aa72b389 | 2290 | do { \ |
781d7a48 KH |
2291 | enum composition_method method = c - 0xF2; \ |
2292 | int *charbuf_base = charbuf; \ | |
df7492f9 | 2293 | int nbytes, nchars; \ |
e951386e | 2294 | \ |
df7492f9 | 2295 | ONE_MORE_BYTE (c); \ |
065e3595 KH |
2296 | if (c < 0) \ |
2297 | goto invalid_code; \ | |
df7492f9 | 2298 | nbytes = c - 0xA0; \ |
e951386e | 2299 | if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \ |
df7492f9 KH |
2300 | goto invalid_code; \ |
2301 | ONE_MORE_BYTE (c); \ | |
2302 | nchars = c - 0xA0; \ | |
e951386e KH |
2303 | if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \ |
2304 | goto invalid_code; \ | |
2305 | cmp_status->old_form = 0; \ | |
2306 | cmp_status->method = method; \ | |
2307 | if (method == COMPOSITION_RELATIVE) \ | |
2308 | cmp_status->state = COMPOSING_CHAR; \ | |
2309 | else \ | |
2310 | cmp_status->state = COMPOSING_COMPONENT_CHAR; \ | |
2311 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2312 | cmp_status->nchars = nchars; \ | |
2313 | cmp_status->ncomps = nbytes - 4; \ | |
2314 | ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \ | |
aa72b389 | 2315 | } while (0) |
93dec019 | 2316 | |
aa72b389 | 2317 | |
e951386e KH |
2318 | /* Start of Emacs 20 style format for relative composition. */ |
2319 | ||
2320 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \ | |
2321 | do { \ | |
2322 | cmp_status->old_form = 1; \ | |
2323 | cmp_status->method = COMPOSITION_RELATIVE; \ | |
2324 | cmp_status->state = COMPOSING_CHAR; \ | |
2325 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2326 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2327 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
2328 | } while (0) | |
2329 | ||
2330 | ||
2331 | /* Start of Emacs 20 style format for rule-base composition. */ | |
2332 | ||
2333 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \ | |
2334 | do { \ | |
2335 | cmp_status->old_form = 1; \ | |
2336 | cmp_status->method = COMPOSITION_WITH_RULE; \ | |
2337 | cmp_status->state = COMPOSING_CHAR; \ | |
2338 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2339 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2340 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
df7492f9 KH |
2341 | } while (0) |
2342 | ||
2343 | ||
e951386e KH |
2344 | #define DECODE_EMACS_MULE_COMPOSITION_START() \ |
2345 | do { \ | |
2346 | const unsigned char *current_src = src; \ | |
2347 | \ | |
2348 | ONE_MORE_BYTE (c); \ | |
2349 | if (c < 0) \ | |
2350 | goto invalid_code; \ | |
2351 | if (c - 0xF2 >= COMPOSITION_RELATIVE \ | |
2352 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \ | |
2353 | DECODE_EMACS_MULE_21_COMPOSITION (); \ | |
2354 | else if (c < 0xA0) \ | |
2355 | goto invalid_code; \ | |
2356 | else if (c < 0xC0) \ | |
2357 | { \ | |
2358 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \ | |
2359 | /* Re-read C as a composition component. */ \ | |
2360 | src = current_src; \ | |
2361 | } \ | |
2362 | else if (c == 0xFF) \ | |
2363 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \ | |
2364 | else \ | |
2365 | goto invalid_code; \ | |
2366 | } while (0) | |
2367 | ||
2368 | #define EMACS_MULE_COMPOSITION_END() \ | |
df7492f9 | 2369 | do { \ |
e951386e | 2370 | int idx = - cmp_status->length; \ |
4d41e8b7 | 2371 | \ |
e951386e KH |
2372 | if (cmp_status->old_form) \ |
2373 | charbuf[idx + 2] = cmp_status->nchars; \ | |
2374 | else if (cmp_status->method > COMPOSITION_RELATIVE) \ | |
2375 | charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \ | |
2376 | cmp_status->state = COMPOSING_NO; \ | |
2377 | } while (0) | |
2378 | ||
2379 | ||
2380 | static int | |
2381 | emacs_mule_finish_composition (charbuf, cmp_status) | |
2382 | int *charbuf; | |
2383 | struct composition_status *cmp_status; | |
2384 | { | |
2385 | int idx = - cmp_status->length; | |
2386 | int new_chars; | |
2387 | ||
2388 | if (cmp_status->old_form && cmp_status->nchars > 0) | |
2389 | { | |
2390 | charbuf[idx + 2] = cmp_status->nchars; | |
2391 | new_chars = 0; | |
2392 | if (cmp_status->method == COMPOSITION_WITH_RULE | |
2393 | && cmp_status->state == COMPOSING_CHAR) | |
2394 | { | |
2395 | /* The last rule was invalid. */ | |
2396 | int rule = charbuf[-1] + 0xA0; | |
2397 | ||
2398 | charbuf[-2] = BYTE8_TO_CHAR (rule); | |
2399 | charbuf[-1] = -1; | |
2400 | new_chars = 1; | |
2401 | } | |
2402 | } | |
2403 | else | |
2404 | { | |
2405 | charbuf[idx++] = BYTE8_TO_CHAR (0x80); | |
2406 | ||
2407 | if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2408 | { | |
2409 | charbuf[idx++] = BYTE8_TO_CHAR (0xFF); | |
2410 | charbuf[idx++] = -3; | |
2411 | charbuf[idx++] = 0; | |
2412 | new_chars = 1; | |
2413 | } | |
2414 | else | |
2415 | { | |
2416 | int nchars = charbuf[idx + 1] + 0xA0; | |
2417 | int nbytes = charbuf[idx + 2] + 0xA0; | |
2418 | ||
2419 | charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method); | |
2420 | charbuf[idx++] = BYTE8_TO_CHAR (nbytes); | |
2421 | charbuf[idx++] = BYTE8_TO_CHAR (nchars); | |
2422 | charbuf[idx++] = -1; | |
2423 | new_chars = 4; | |
2424 | } | |
2425 | } | |
2426 | cmp_status->state = COMPOSING_NO; | |
2427 | return new_chars; | |
2428 | } | |
2429 | ||
2430 | #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \ | |
2431 | do { \ | |
2432 | if (cmp_status->state != COMPOSING_NO) \ | |
2433 | char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \ | |
df7492f9 KH |
2434 | } while (0) |
2435 | ||
aa72b389 KH |
2436 | |
2437 | static void | |
df7492f9 | 2438 | decode_coding_emacs_mule (coding) |
aa72b389 | 2439 | struct coding_system *coding; |
aa72b389 | 2440 | { |
8f924df7 KH |
2441 | const unsigned char *src = coding->source + coding->consumed; |
2442 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2443 | const unsigned char *src_base; | |
69a80ea3 KH |
2444 | int *charbuf = coding->charbuf + coding->charbuf_used; |
2445 | int *charbuf_end | |
2446 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 | 2447 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2448 | int multibytep = coding->src_multibyte; |
24a73b0a | 2449 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2450 | int char_offset = coding->produced_char; |
2451 | int last_offset = char_offset; | |
2452 | int last_id = charset_ascii; | |
0a9564cb EZ |
2453 | int eol_crlf = |
2454 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 2455 | int byte_after_cr = -1; |
e951386e | 2456 | struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status; |
aa72b389 | 2457 | |
24a73b0a | 2458 | CODING_GET_INFO (coding, attrs, charset_list); |
aa72b389 | 2459 | |
e951386e KH |
2460 | if (cmp_status->state != COMPOSING_NO) |
2461 | { | |
2462 | int i; | |
2463 | ||
2464 | for (i = 0; i < cmp_status->length; i++) | |
2465 | *charbuf++ = cmp_status->carryover[i]; | |
2466 | coding->annotated = 1; | |
2467 | } | |
2468 | ||
aa72b389 KH |
2469 | while (1) |
2470 | { | |
e951386e | 2471 | int c, id; |
df7492f9 | 2472 | |
aa72b389 | 2473 | src_base = src; |
df7492f9 KH |
2474 | consumed_chars_base = consumed_chars; |
2475 | ||
2476 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
2477 | { |
2478 | if (byte_after_cr >= 0) | |
2479 | src_base--; | |
2480 | break; | |
2481 | } | |
aa72b389 | 2482 | |
119852e7 KH |
2483 | if (byte_after_cr >= 0) |
2484 | c = byte_after_cr, byte_after_cr = -1; | |
2485 | else | |
2486 | ONE_MORE_BYTE (c); | |
e951386e KH |
2487 | |
2488 | if (c < 0 || c == 0x80) | |
065e3595 | 2489 | { |
e951386e KH |
2490 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
2491 | if (c < 0) | |
2492 | { | |
2493 | *charbuf++ = -c; | |
2494 | char_offset++; | |
2495 | } | |
2496 | else | |
2497 | DECODE_EMACS_MULE_COMPOSITION_START (); | |
2498 | continue; | |
065e3595 | 2499 | } |
e951386e KH |
2500 | |
2501 | if (c < 0x80) | |
aa72b389 | 2502 | { |
119852e7 KH |
2503 | if (eol_crlf && c == '\r') |
2504 | ONE_MORE_BYTE (byte_after_cr); | |
e951386e KH |
2505 | id = charset_ascii; |
2506 | if (cmp_status->state != COMPOSING_NO) | |
2507 | { | |
2508 | if (cmp_status->old_form) | |
2509 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2510 | else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2511 | cmp_status->ncomps--; | |
2512 | } | |
2513 | } | |
2514 | else | |
2515 | { | |
2516 | int nchars, nbytes; | |
2517 | ||
2518 | c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id, | |
2519 | cmp_status); | |
2520 | if (c < 0) | |
2521 | { | |
2522 | if (c == -1) | |
2523 | goto invalid_code; | |
2524 | if (c == -2) | |
2525 | break; | |
2526 | } | |
2527 | src = src_base + nbytes; | |
2528 | consumed_chars = consumed_chars_base + nchars; | |
2529 | if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2530 | cmp_status->ncomps -= nchars; | |
2531 | } | |
2532 | ||
2533 | /* Now if C >= 0, we found a normally encoded characer, if C < | |
2534 | 0, we found an old-style composition component character or | |
2535 | rule. */ | |
2536 | ||
2537 | if (cmp_status->state == COMPOSING_NO) | |
2538 | { | |
2539 | if (last_id != id) | |
2540 | { | |
2541 | if (last_id != charset_ascii) | |
2542 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, | |
2543 | last_id); | |
2544 | last_id = id; | |
2545 | last_offset = char_offset; | |
2546 | } | |
df7492f9 KH |
2547 | *charbuf++ = c; |
2548 | char_offset++; | |
aa72b389 | 2549 | } |
e951386e | 2550 | else if (cmp_status->state == COMPOSING_CHAR) |
df7492f9 | 2551 | { |
e951386e KH |
2552 | if (cmp_status->old_form) |
2553 | { | |
2554 | if (c >= 0) | |
2555 | { | |
2556 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2557 | *charbuf++ = c; | |
2558 | char_offset++; | |
2559 | } | |
2560 | else | |
2561 | { | |
2562 | *charbuf++ = -c; | |
2563 | cmp_status->nchars++; | |
2564 | cmp_status->length++; | |
2565 | if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS) | |
2566 | EMACS_MULE_COMPOSITION_END (); | |
2567 | else if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2568 | cmp_status->state = COMPOSING_RULE; | |
2569 | } | |
2570 | } | |
df7492f9 | 2571 | else |
e951386e KH |
2572 | { |
2573 | *charbuf++ = c; | |
2574 | cmp_status->length++; | |
2575 | cmp_status->nchars--; | |
2576 | if (cmp_status->nchars == 0) | |
2577 | EMACS_MULE_COMPOSITION_END (); | |
2578 | } | |
df7492f9 | 2579 | } |
e951386e | 2580 | else if (cmp_status->state == COMPOSING_RULE) |
df7492f9 | 2581 | { |
e951386e | 2582 | int rule; |
ff0dacd7 | 2583 | |
e951386e | 2584 | if (c >= 0) |
df7492f9 | 2585 | { |
e951386e KH |
2586 | EMACS_MULE_COMPOSITION_END (); |
2587 | *charbuf++ = c; | |
2588 | char_offset++; | |
df7492f9 | 2589 | } |
e951386e | 2590 | else |
ff0dacd7 | 2591 | { |
e951386e KH |
2592 | c = -c; |
2593 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule); | |
2594 | if (rule < 0) | |
2595 | goto invalid_code; | |
2596 | *charbuf++ = -2; | |
2597 | *charbuf++ = rule; | |
2598 | cmp_status->length += 2; | |
2599 | cmp_status->state = COMPOSING_CHAR; | |
ff0dacd7 | 2600 | } |
e951386e KH |
2601 | } |
2602 | else if (cmp_status->state == COMPOSING_COMPONENT_CHAR) | |
2603 | { | |
df7492f9 | 2604 | *charbuf++ = c; |
e951386e KH |
2605 | cmp_status->length++; |
2606 | if (cmp_status->ncomps == 0) | |
2607 | cmp_status->state = COMPOSING_CHAR; | |
2608 | else if (cmp_status->ncomps > 0) | |
2609 | { | |
2610 | if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) | |
2611 | cmp_status->state = COMPOSING_COMPONENT_RULE; | |
2612 | } | |
2613 | else | |
2614 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
df7492f9 | 2615 | } |
e951386e KH |
2616 | else /* COMPOSING_COMPONENT_RULE */ |
2617 | { | |
2618 | int rule; | |
2619 | ||
2620 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule); | |
2621 | if (rule < 0) | |
2622 | goto invalid_code; | |
2623 | *charbuf++ = -2; | |
2624 | *charbuf++ = rule; | |
2625 | cmp_status->length += 2; | |
2626 | cmp_status->ncomps--; | |
2627 | if (cmp_status->ncomps > 0) | |
2628 | cmp_status->state = COMPOSING_COMPONENT_CHAR; | |
2629 | else | |
2630 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2631 | } | |
2632 | continue; | |
2633 | ||
2634 | retry: | |
2635 | src = src_base; | |
2636 | consumed_chars = consumed_chars_base; | |
df7492f9 KH |
2637 | continue; |
2638 | ||
2639 | invalid_code: | |
e951386e | 2640 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
df7492f9 KH |
2641 | src = src_base; |
2642 | consumed_chars = consumed_chars_base; | |
2643 | ONE_MORE_BYTE (c); | |
2644 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2645 | char_offset++; |
df7492f9 KH |
2646 | coding->errors++; |
2647 | } | |
2648 | ||
2649 | no_more_source: | |
e951386e KH |
2650 | if (cmp_status->state != COMPOSING_NO) |
2651 | { | |
2652 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
2653 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2654 | else | |
2655 | { | |
2656 | int i; | |
2657 | ||
2658 | charbuf -= cmp_status->length; | |
2659 | for (i = 0; i < cmp_status->length; i++) | |
2660 | cmp_status->carryover[i] = charbuf[i]; | |
2661 | } | |
2662 | } | |
ff0dacd7 | 2663 | if (last_id != charset_ascii) |
69a80ea3 | 2664 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2665 | coding->consumed_char += consumed_chars_base; |
2666 | coding->consumed = src_base - coding->source; | |
2667 | coding->charbuf_used = charbuf - coding->charbuf; | |
2668 | } | |
2669 | ||
2670 | ||
2671 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2672 | do { \ | |
2673 | if (id < 0xA0) \ | |
2674 | codes[0] = id, codes[1] = 0; \ | |
2675 | else if (id < 0xE0) \ | |
2676 | codes[0] = 0x9A, codes[1] = id; \ | |
2677 | else if (id < 0xF0) \ | |
2678 | codes[0] = 0x9B, codes[1] = id; \ | |
2679 | else if (id < 0xF5) \ | |
2680 | codes[0] = 0x9C, codes[1] = id; \ | |
2681 | else \ | |
2682 | codes[0] = 0x9D, codes[1] = id; \ | |
2683 | } while (0); | |
2684 | ||
aa72b389 | 2685 | |
df7492f9 KH |
2686 | static int |
2687 | encode_coding_emacs_mule (coding) | |
2688 | struct coding_system *coding; | |
2689 | { | |
2690 | int multibytep = coding->dst_multibyte; | |
2691 | int *charbuf = coding->charbuf; | |
2692 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2693 | unsigned char *dst = coding->destination + coding->produced; | |
2694 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2695 | int safe_room = 8; | |
df7492f9 | 2696 | int produced_chars = 0; |
24a73b0a | 2697 | Lisp_Object attrs, charset_list; |
df7492f9 | 2698 | int c; |
ff0dacd7 | 2699 | int preferred_charset_id = -1; |
df7492f9 | 2700 | |
24a73b0a | 2701 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2702 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2703 | { | |
2704 | CODING_ATTR_CHARSET_LIST (attrs) | |
2705 | = charset_list = Vemacs_mule_charset_list; | |
2706 | } | |
df7492f9 KH |
2707 | |
2708 | while (charbuf < charbuf_end) | |
2709 | { | |
2710 | ASSURE_DESTINATION (safe_room); | |
2711 | c = *charbuf++; | |
ff0dacd7 KH |
2712 | |
2713 | if (c < 0) | |
2714 | { | |
2715 | /* Handle an annotation. */ | |
2716 | switch (*charbuf) | |
2717 | { | |
2718 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2719 | /* Not yet implemented. */ | |
2720 | break; | |
2721 | case CODING_ANNOTATE_CHARSET_MASK: | |
2722 | preferred_charset_id = charbuf[3]; | |
2723 | if (preferred_charset_id >= 0 | |
2724 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2725 | charset_list))) | |
2726 | preferred_charset_id = -1; | |
2727 | break; | |
2728 | default: | |
2729 | abort (); | |
2730 | } | |
2731 | charbuf += -c - 1; | |
2732 | continue; | |
2733 | } | |
2734 | ||
df7492f9 KH |
2735 | if (ASCII_CHAR_P (c)) |
2736 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2737 | else if (CHAR_BYTE8_P (c)) |
2738 | { | |
2739 | c = CHAR_TO_BYTE8 (c); | |
2740 | EMIT_ONE_BYTE (c); | |
2741 | } | |
df7492f9 | 2742 | else |
aa72b389 | 2743 | { |
df7492f9 KH |
2744 | struct charset *charset; |
2745 | unsigned code; | |
2746 | int dimension; | |
2747 | int emacs_mule_id; | |
2748 | unsigned char leading_codes[2]; | |
2749 | ||
ff0dacd7 KH |
2750 | if (preferred_charset_id >= 0) |
2751 | { | |
2752 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
905ca9d2 KH |
2753 | if (CHAR_CHARSET_P (c, charset)) |
2754 | code = ENCODE_CHAR (charset, c); | |
2755 | else | |
2756 | charset = char_charset (c, charset_list, &code); | |
ff0dacd7 KH |
2757 | } |
2758 | else | |
2759 | charset = char_charset (c, charset_list, &code); | |
df7492f9 KH |
2760 | if (! charset) |
2761 | { | |
2762 | c = coding->default_char; | |
2763 | if (ASCII_CHAR_P (c)) | |
2764 | { | |
2765 | EMIT_ONE_ASCII_BYTE (c); | |
2766 | continue; | |
2767 | } | |
2768 | charset = char_charset (c, charset_list, &code); | |
2769 | } | |
2770 | dimension = CHARSET_DIMENSION (charset); | |
2771 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2772 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2773 | EMIT_ONE_BYTE (leading_codes[0]); | |
2774 | if (leading_codes[1]) | |
2775 | EMIT_ONE_BYTE (leading_codes[1]); | |
2776 | if (dimension == 1) | |
1fa663f9 | 2777 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2778 | else |
df7492f9 | 2779 | { |
1fa663f9 | 2780 | code |= 0x8080; |
df7492f9 KH |
2781 | EMIT_ONE_BYTE (code >> 8); |
2782 | EMIT_ONE_BYTE (code & 0xFF); | |
2783 | } | |
aa72b389 | 2784 | } |
aa72b389 | 2785 | } |
065e3595 | 2786 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2787 | coding->produced_char += produced_chars; |
2788 | coding->produced = dst - coding->destination; | |
2789 | return 0; | |
aa72b389 | 2790 | } |
b73bfc1c | 2791 | |
4ed46869 | 2792 | \f |
df7492f9 | 2793 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2794 | |
2795 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2796 | Since the intention of this note is to help understand the |
5a936b46 | 2797 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2798 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2799 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2800 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2801 | |
2802 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2803 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2804 | is encoded using bytes less than 128. This may make the encoded |
2805 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2806 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2807 | Significant Bit). |
b73bfc1c | 2808 | |
cfb43547 DL |
2809 | There are two kinds of character sets: control character sets and |
2810 | graphic character sets. The former contain control characters such | |
4ed46869 | 2811 | as `newline' and `escape' to provide control functions (control |
39787efd | 2812 | functions are also provided by escape sequences). The latter |
cfb43547 | 2813 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2814 | two control character sets and many graphic character sets. |
2815 | ||
2816 | Graphic character sets are classified into one of the following | |
39787efd KH |
2817 | four classes, according to the number of bytes (DIMENSION) and |
2818 | number of characters in one dimension (CHARS) of the set: | |
2819 | - DIMENSION1_CHARS94 | |
2820 | - DIMENSION1_CHARS96 | |
2821 | - DIMENSION2_CHARS94 | |
2822 | - DIMENSION2_CHARS96 | |
2823 | ||
2824 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2825 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2826 | hereafter). The <F> of each character set is decided by ECMA(*) |
2827 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2828 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2829 | |
2830 | Note (*): ECMA = European Computer Manufacturers Association | |
2831 | ||
cfb43547 | 2832 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2833 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2834 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2835 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2836 | o DIMENSION2_CHARS96 -- none for the moment | |
2837 | ||
39787efd | 2838 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2839 | C0 [0x00..0x1F] -- control character plane 0 |
2840 | GL [0x20..0x7F] -- graphic character plane 0 | |
2841 | C1 [0x80..0x9F] -- control character plane 1 | |
2842 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2843 | ||
2844 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2845 | C1 by an escape sequence. The most common case is that: |
2846 | - ISO646's control character set is designated/invoked to C0, and | |
2847 | - ISO6429's control character set is designated/invoked to C1, | |
2848 | and usually these designations/invocations are omitted in encoded | |
2849 | text. In a 7-bit environment, only C0 can be used, and a control | |
2850 | character for C1 is encoded by an appropriate escape sequence to | |
2851 | fit into the environment. All control characters for C1 are | |
2852 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2853 | |
2854 | A graphic character set is at first designated to one of four | |
2855 | graphic registers (G0 through G3), then these graphic registers are | |
2856 | invoked to GL or GR. These designations and invocations can be | |
2857 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2858 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2859 | these invocations and designations are omitted in encoded text. | |
2860 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2861 | |
39787efd KH |
2862 | When a graphic character set of CHARS94 is invoked to GL, codes |
2863 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2864 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2865 | be used. | |
4ed46869 KH |
2866 | |
2867 | There are two ways of invocation: locking-shift and single-shift. | |
2868 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2869 | invocation, whereas with single-shift, the invocation affects the |
2870 | following character only and doesn't affect the locking-shift | |
2871 | state. Invocations are done by the following control characters or | |
2872 | escape sequences: | |
4ed46869 KH |
2873 | |
2874 | ---------------------------------------------------------------------- | |
39787efd | 2875 | abbrev function cntrl escape seq description |
4ed46869 | 2876 | ---------------------------------------------------------------------- |
39787efd KH |
2877 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2878 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2879 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2880 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2881 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2882 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2883 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2884 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2885 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2886 | ---------------------------------------------------------------------- |
39787efd KH |
2887 | (*) These are not used by any known coding system. |
2888 | ||
2889 | Control characters for these functions are defined by macros | |
2890 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2891 | |
39787efd | 2892 | Designations are done by the following escape sequences: |
4ed46869 KH |
2893 | ---------------------------------------------------------------------- |
2894 | escape sequence description | |
2895 | ---------------------------------------------------------------------- | |
2896 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2897 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2898 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2899 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2900 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2901 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2902 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2903 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2904 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2905 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2906 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2907 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2908 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2909 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2910 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2911 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2912 | ---------------------------------------------------------------------- | |
2913 | ||
2914 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2915 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2916 | |
2917 | Note (*): Although these designations are not allowed in ISO2022, | |
2918 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2919 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2920 | 7-bit environment, non-locking-shift, and non-single-shift. |
2921 | ||
2922 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2923 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2924 | |
cfb43547 | 2925 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2926 | same multilingual text in ISO2022. Actually, there exist many |
2927 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2928 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2929 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2930 | localized platforms), and all of these are variants of ISO2022. |
2931 | ||
2932 | In addition to the above, Emacs handles two more kinds of escape | |
2933 | sequences: ISO6429's direction specification and Emacs' private | |
2934 | sequence for specifying character composition. | |
2935 | ||
39787efd | 2936 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2937 | o CSI ']' -- end of the current direction |
2938 | o CSI '0' ']' -- end of the current direction | |
2939 | o CSI '1' ']' -- start of left-to-right text | |
2940 | o CSI '2' ']' -- start of right-to-left text | |
2941 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2942 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2943 | ||
2944 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2945 | o ESC '0' -- start relative composition |
2946 | o ESC '1' -- end composition | |
2947 | o ESC '2' -- start rule-base composition (*) | |
2948 | o ESC '3' -- start relative composition with alternate chars (**) | |
2949 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2950 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2951 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2952 | |
5a936b46 DL |
2953 | (*) This form is used only in Emacs 20.7 and older versions, |
2954 | but newer versions can safely decode it. | |
cfb43547 | 2955 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2956 | and older versions can't decode it. |
ec6d2bb8 | 2957 | |
cfb43547 | 2958 | Here's a list of example usages of these composition escape |
b73bfc1c | 2959 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2960 | |
b73bfc1c | 2961 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2962 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2963 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2964 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2965 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2966 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2967 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2968 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 KH |
2969 | |
2970 | enum iso_code_class_type iso_code_class[256]; | |
2971 | ||
df7492f9 KH |
2972 | #define SAFE_CHARSET_P(coding, id) \ |
2973 | ((id) <= (coding)->max_charset_id \ | |
1b3b981b | 2974 | && (coding)->safe_charsets[id] != 255) |
df7492f9 KH |
2975 | |
2976 | ||
2977 | #define SHIFT_OUT_OK(category) \ | |
2978 | (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0) | |
2979 | ||
2980 | static void | |
f0064e1f DL |
2981 | setup_iso_safe_charsets (attrs) |
2982 | Lisp_Object attrs; | |
df7492f9 KH |
2983 | { |
2984 | Lisp_Object charset_list, safe_charsets; | |
2985 | Lisp_Object request; | |
2986 | Lisp_Object reg_usage; | |
2987 | Lisp_Object tail; | |
2988 | int reg94, reg96; | |
2989 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
2990 | int max_charset_id; | |
2991 | ||
2992 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2993 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2994 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2995 | { | |
2996 | CODING_ATTR_CHARSET_LIST (attrs) | |
2997 | = charset_list = Viso_2022_charset_list; | |
2998 | ASET (attrs, coding_attr_safe_charsets, Qnil); | |
2999 | } | |
3000 | ||
3001 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
3002 | return; | |
3003 | ||
3004 | max_charset_id = 0; | |
3005 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
3006 | { | |
3007 | int id = XINT (XCAR (tail)); | |
3008 | if (max_charset_id < id) | |
3009 | max_charset_id = id; | |
3010 | } | |
d46c5b12 | 3011 | |
1b3b981b AS |
3012 | safe_charsets = make_uninit_string (max_charset_id + 1); |
3013 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
3014 | request = AREF (attrs, coding_attr_iso_request); |
3015 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
3016 | reg94 = XINT (XCAR (reg_usage)); | |
3017 | reg96 = XINT (XCDR (reg_usage)); | |
3018 | ||
3019 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
3020 | { | |
3021 | Lisp_Object id; | |
3022 | Lisp_Object reg; | |
3023 | struct charset *charset; | |
3024 | ||
3025 | id = XCAR (tail); | |
3026 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 3027 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 3028 | if (! NILP (reg)) |
8f924df7 | 3029 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
3030 | else if (charset->iso_chars_96) |
3031 | { | |
3032 | if (reg96 < 4) | |
8f924df7 | 3033 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
3034 | } |
3035 | else | |
3036 | { | |
3037 | if (reg94 < 4) | |
8f924df7 | 3038 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
3039 | } |
3040 | } | |
3041 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
3042 | } | |
d46c5b12 | 3043 | |
b6871cc7 | 3044 | |
4ed46869 | 3045 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
3046 | Check if a text is encoded in one of ISO-2022 based codig systems. |
3047 | If it is, return 1, else return 0. */ | |
4ed46869 | 3048 | |
0a28aafb | 3049 | static int |
ff0dacd7 | 3050 | detect_coding_iso_2022 (coding, detect_info) |
df7492f9 | 3051 | struct coding_system *coding; |
ff0dacd7 | 3052 | struct coding_detection_info *detect_info; |
4ed46869 | 3053 | { |
8f924df7 KH |
3054 | const unsigned char *src = coding->source, *src_base = src; |
3055 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 3056 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 3057 | int single_shifting = 0; |
df7492f9 KH |
3058 | int id; |
3059 | int c, c1; | |
3060 | int consumed_chars = 0; | |
3061 | int i; | |
ff0dacd7 KH |
3062 | int rejected = 0; |
3063 | int found = 0; | |
cee53ed4 | 3064 | int composition_count = -1; |
ff0dacd7 KH |
3065 | |
3066 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
3067 | |
3068 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
3069 | { | |
3070 | struct coding_system *this = &(coding_categories[i]); | |
3071 | Lisp_Object attrs, val; | |
3072 | ||
c6b278e7 KH |
3073 | if (this->id < 0) |
3074 | continue; | |
df7492f9 KH |
3075 | attrs = CODING_ID_ATTRS (this->id); |
3076 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
1b3b981b | 3077 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list)) |
df7492f9 KH |
3078 | setup_iso_safe_charsets (attrs); |
3079 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 3080 | this->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 3081 | this->safe_charsets = SDATA (val); |
df7492f9 KH |
3082 | } |
3083 | ||
3084 | /* A coding system of this category is always ASCII compatible. */ | |
3085 | src += coding->head_ascii; | |
3f003981 | 3086 | |
ff0dacd7 | 3087 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 3088 | { |
065e3595 | 3089 | src_base = src; |
df7492f9 | 3090 | ONE_MORE_BYTE (c); |
4ed46869 KH |
3091 | switch (c) |
3092 | { | |
3093 | case ISO_CODE_ESC: | |
74383408 KH |
3094 | if (inhibit_iso_escape_detection) |
3095 | break; | |
f46869e4 | 3096 | single_shifting = 0; |
df7492f9 | 3097 | ONE_MORE_BYTE (c); |
d46c5b12 | 3098 | if (c >= '(' && c <= '/') |
4ed46869 | 3099 | { |
bf9cdd4e | 3100 | /* Designation sequence for a charset of dimension 1. */ |
df7492f9 | 3101 | ONE_MORE_BYTE (c1); |
d46c5b12 | 3102 | if (c1 < ' ' || c1 >= 0x80 |
df7492f9 | 3103 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) |
d46c5b12 KH |
3104 | /* Invalid designation sequence. Just ignore. */ |
3105 | break; | |
bf9cdd4e KH |
3106 | } |
3107 | else if (c == '$') | |
3108 | { | |
3109 | /* Designation sequence for a charset of dimension 2. */ | |
df7492f9 | 3110 | ONE_MORE_BYTE (c); |
bf9cdd4e KH |
3111 | if (c >= '@' && c <= 'B') |
3112 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
ff0dacd7 | 3113 | id = iso_charset_table[1][0][c]; |
bf9cdd4e | 3114 | else if (c >= '(' && c <= '/') |
bcf26d6a | 3115 | { |
df7492f9 | 3116 | ONE_MORE_BYTE (c1); |
d46c5b12 | 3117 | if (c1 < ' ' || c1 >= 0x80 |
df7492f9 | 3118 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) |
d46c5b12 KH |
3119 | /* Invalid designation sequence. Just ignore. */ |
3120 | break; | |
bcf26d6a | 3121 | } |
bf9cdd4e | 3122 | else |
ff0dacd7 | 3123 | /* Invalid designation sequence. Just ignore it. */ |
d46c5b12 KH |
3124 | break; |
3125 | } | |
ae9ff118 | 3126 | else if (c == 'N' || c == 'O') |
d46c5b12 | 3127 | { |
ae9ff118 | 3128 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
3129 | single_shifting = 1; |
3130 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
d46c5b12 | 3131 | break; |
4ed46869 | 3132 | } |
cee53ed4 KH |
3133 | else if (c == '1') |
3134 | { | |
3135 | /* End of composition. */ | |
3136 | if (composition_count < 0 | |
3137 | || composition_count > MAX_COMPOSITION_COMPONENTS) | |
3138 | /* Invalid */ | |
3139 | break; | |
3140 | composition_count = -1; | |
3141 | found |= CATEGORY_MASK_ISO; | |
3142 | } | |
ec6d2bb8 KH |
3143 | else if (c >= '0' && c <= '4') |
3144 | { | |
3145 | /* ESC <Fp> for start/end composition. */ | |
cee53ed4 | 3146 | composition_count = 0; |
ec6d2bb8 KH |
3147 | break; |
3148 | } | |
bf9cdd4e | 3149 | else |
df7492f9 | 3150 | { |
ff0dacd7 | 3151 | /* Invalid escape sequence. Just ignore it. */ |
df7492f9 KH |
3152 | break; |
3153 | } | |
d46c5b12 KH |
3154 | |
3155 | /* We found a valid designation sequence for CHARSET. */ | |
ff0dacd7 | 3156 | rejected |= CATEGORY_MASK_ISO_8BIT; |
df7492f9 KH |
3157 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], |
3158 | id)) | |
ff0dacd7 | 3159 | found |= CATEGORY_MASK_ISO_7; |
d46c5b12 | 3160 | else |
ff0dacd7 | 3161 | rejected |= CATEGORY_MASK_ISO_7; |
df7492f9 KH |
3162 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], |
3163 | id)) | |
ff0dacd7 | 3164 | found |= CATEGORY_MASK_ISO_7_TIGHT; |
d46c5b12 | 3165 | else |
ff0dacd7 | 3166 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; |
df7492f9 KH |
3167 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], |
3168 | id)) | |
ff0dacd7 | 3169 | found |= CATEGORY_MASK_ISO_7_ELSE; |
ae9ff118 | 3170 | else |
ff0dacd7 | 3171 | rejected |= CATEGORY_MASK_ISO_7_ELSE; |
df7492f9 KH |
3172 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], |
3173 | id)) | |
ff0dacd7 | 3174 | found |= CATEGORY_MASK_ISO_8_ELSE; |
ae9ff118 | 3175 | else |
ff0dacd7 | 3176 | rejected |= CATEGORY_MASK_ISO_8_ELSE; |
4ed46869 KH |
3177 | break; |
3178 | ||
4ed46869 | 3179 | case ISO_CODE_SO: |
d46c5b12 | 3180 | case ISO_CODE_SI: |
ff0dacd7 | 3181 | /* Locking shift out/in. */ |
74383408 KH |
3182 | if (inhibit_iso_escape_detection) |
3183 | break; | |
f46869e4 | 3184 | single_shifting = 0; |
ff0dacd7 | 3185 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
d46c5b12 KH |
3186 | break; |
3187 | ||
4ed46869 | 3188 | case ISO_CODE_CSI: |
ff0dacd7 | 3189 | /* Control sequence introducer. */ |
f46869e4 | 3190 | single_shifting = 0; |
ff0dacd7 KH |
3191 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3192 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3193 | goto check_extra_latin; | |
3194 | ||
4ed46869 KH |
3195 | case ISO_CODE_SS2: |
3196 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
3197 | /* Single shift. */ |
3198 | if (inhibit_iso_escape_detection) | |
3199 | break; | |
75e2a253 | 3200 | single_shifting = 0; |
ff0dacd7 KH |
3201 | rejected |= CATEGORY_MASK_ISO_7BIT; |
3202 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3203 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
75e2a253 | 3204 | found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1; |
ff0dacd7 KH |
3205 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
3206 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
75e2a253 KH |
3207 | found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1; |
3208 | if (single_shifting) | |
3209 | break; | |
ff0dacd7 | 3210 | goto check_extra_latin; |
4ed46869 KH |
3211 | |
3212 | default: | |
065e3595 KH |
3213 | if (c < 0) |
3214 | continue; | |
4ed46869 | 3215 | if (c < 0x80) |
f46869e4 | 3216 | { |
cee53ed4 KH |
3217 | if (composition_count >= 0) |
3218 | composition_count++; | |
f46869e4 KH |
3219 | single_shifting = 0; |
3220 | break; | |
3221 | } | |
ff0dacd7 | 3222 | if (c >= 0xA0) |
c4825358 | 3223 | { |
ff0dacd7 KH |
3224 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3225 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 3226 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
3227 | 0xA0..0FF. If the byte length is even, we include |
3228 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
3229 | only when we are not single shifting. */ | |
3230 | if (! single_shifting | |
3231 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 3232 | { |
e17de821 | 3233 | int i = 1; |
b73bfc1c KH |
3234 | while (src < src_end) |
3235 | { | |
df7492f9 | 3236 | ONE_MORE_BYTE (c); |
b73bfc1c KH |
3237 | if (c < 0xA0) |
3238 | break; | |
3239 | i++; | |
3240 | } | |
3241 | ||
3242 | if (i & 1 && src < src_end) | |
cee53ed4 KH |
3243 | { |
3244 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3245 | if (composition_count >= 0) | |
3246 | composition_count += i; | |
3247 | } | |
f46869e4 | 3248 | else |
cee53ed4 KH |
3249 | { |
3250 | found |= CATEGORY_MASK_ISO_8_2; | |
3251 | if (composition_count >= 0) | |
3252 | composition_count += i / 2; | |
3253 | } | |
f46869e4 | 3254 | } |
ff0dacd7 | 3255 | break; |
4ed46869 | 3256 | } |
ff0dacd7 KH |
3257 | check_extra_latin: |
3258 | single_shifting = 0; | |
3259 | if (! VECTORP (Vlatin_extra_code_table) | |
3260 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
3261 | { | |
3262 | rejected = CATEGORY_MASK_ISO; | |
3263 | break; | |
3264 | } | |
3265 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3266 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
3267 | found |= CATEGORY_MASK_ISO_8_1; | |
3268 | else | |
3269 | rejected |= CATEGORY_MASK_ISO_8_1; | |
75e2a253 | 3270 | rejected |= CATEGORY_MASK_ISO_8_2; |
4ed46869 KH |
3271 | } |
3272 | } | |
ff0dacd7 KH |
3273 | detect_info->rejected |= CATEGORY_MASK_ISO; |
3274 | return 0; | |
4ed46869 | 3275 | |
df7492f9 | 3276 | no_more_source: |
ff0dacd7 KH |
3277 | detect_info->rejected |= rejected; |
3278 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 3279 | return 1; |
4ed46869 | 3280 | } |
ec6d2bb8 | 3281 | |
4ed46869 | 3282 | |
134b9549 KH |
3283 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
3284 | escape sequence should be kept. */ | |
df7492f9 KH |
3285 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
3286 | do { \ | |
3287 | int id, prev; \ | |
3288 | \ | |
3289 | if (final < '0' || final >= 128 \ | |
3290 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
3291 | || !SAFE_CHARSET_P (coding, id)) \ | |
3292 | { \ | |
3293 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
3294 | chars_96 = -1; \ |
3295 | break; \ | |
df7492f9 KH |
3296 | } \ |
3297 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
3298 | if (id == charset_jisx0201_roman) \ |
3299 | { \ | |
3300 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3301 | id = charset_ascii; \ | |
3302 | } \ | |
3303 | else if (id == charset_jisx0208_1978) \ | |
3304 | { \ | |
3305 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3306 | id = charset_jisx0208; \ | |
3307 | } \ | |
df7492f9 KH |
3308 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
3309 | /* If there was an invalid designation to REG previously, and this \ | |
3310 | designation is ASCII to REG, we should keep this designation \ | |
3311 | sequence. */ \ | |
3312 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 3313 | chars_96 = -1; \ |
4ed46869 KH |
3314 | } while (0) |
3315 | ||
d46c5b12 | 3316 | |
e951386e KH |
3317 | /* Handle these composition sequence (ALT: alternate char): |
3318 | ||
3319 | (1) relative composition: ESC 0 CHAR ... ESC 1 | |
3320 | (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
3321 | (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1 | |
3322 | (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1 | |
3323 | ||
3324 | When the start sequence (ESC 0/2/3/4) is found, this annotation | |
3325 | header is produced. | |
3326 | ||
3327 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ] | |
3328 | ||
3329 | Then, upon reading CHAR or RULE (one or two bytes), these codes are | |
3330 | produced until the end sequence (ESC 1) is found: | |
3331 | ||
3332 | (1) CHAR ... CHAR | |
3333 | (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
3334 | (3) ALT ... ALT -1 -1 CHAR ... CHAR | |
3335 | (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR | |
3336 | ||
3337 | When the end sequence (ESC 1) is found, LENGTH and NCHARS in the | |
3338 | annotation header is updated as below: | |
3339 | ||
3340 | (1) LENGTH: unchanged, NCHARS: number of CHARs | |
3341 | (2) LENGTH: unchanged, NCHARS: number of CHARs | |
3342 | (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs | |
3343 | (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs | |
3344 | ||
3345 | If an error is found while composing, the annotation header is | |
3346 | changed to: | |
3347 | ||
3348 | [ ESC '0'/'2'/'3'/'4' -2 0 ] | |
3349 | ||
3350 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
3351 | byte sequence as below: | |
3352 | o the original byte sequence is B: [ B -1 ] | |
3353 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
3354 | and the sequence [ -1 -1 ] is changed to the original byte | |
3355 | sequence: | |
3356 | [ ESC '0' ] | |
3357 | */ | |
3358 | ||
3359 | /* Decode a composition rule C1 and maybe one more byte from the | |
3360 | source, and set RULE to the encoded composition rule, NBYTES to the | |
3361 | length of the composition rule. If the rule is invalid, set RULE | |
3362 | to some negative value. */ | |
3363 | ||
3364 | #define DECODE_COMPOSITION_RULE(rule, nbytes) \ | |
3365 | do { \ | |
3366 | rule = c1 - 32; \ | |
3367 | if (rule < 0) \ | |
3368 | break; \ | |
3369 | if (rule < 81) /* old format (before ver.21) */ \ | |
3370 | { \ | |
3371 | int gref = (rule) / 9; \ | |
3372 | int nref = (rule) % 9; \ | |
3373 | if (gref == 4) gref = 10; \ | |
3374 | if (nref == 4) nref = 10; \ | |
3375 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
3376 | nbytes = 1; \ | |
3377 | } \ | |
3378 | else /* new format (after ver.21) */ \ | |
3379 | { \ | |
3380 | int c; \ | |
3381 | \ | |
3382 | ONE_MORE_BYTE (c); \ | |
3383 | rule = COMPOSITION_ENCODE_RULE (rule - 81, c - 32); \ | |
3384 | if (rule >= 0) \ | |
3385 | rule += 0x100; /* to destinguish it from the old format */ \ | |
3386 | nbytes = 2; \ | |
3387 | } \ | |
3388 | } while (0) | |
3389 | ||
3390 | #define ENCODE_COMPOSITION_RULE(rule) \ | |
df7492f9 | 3391 | do { \ |
e951386e KH |
3392 | int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \ |
3393 | \ | |
3394 | if (rule < 0x100) /* old format */ \ | |
df7492f9 | 3395 | { \ |
e951386e KH |
3396 | if (gref == 10) gref = 4; \ |
3397 | if (nref == 10) nref = 4; \ | |
3398 | charbuf[idx] = 32 + gref * 9 + nref; \ | |
3399 | charbuf[idx + 1] = -1; \ | |
3400 | new_chars++; \ | |
df7492f9 | 3401 | } \ |
e951386e | 3402 | else /* new format */ \ |
df7492f9 | 3403 | { \ |
e951386e KH |
3404 | charbuf[idx] = 32 + 81 + gref; \ |
3405 | charbuf[idx + 1] = 32 + nref; \ | |
3406 | new_chars += 2; \ | |
df7492f9 KH |
3407 | } \ |
3408 | } while (0) | |
3409 | ||
e951386e KH |
3410 | /* Finish the current composition as invalid. */ |
3411 | ||
3412 | static int finish_composition P_ ((int *, struct composition_status *)); | |
3413 | ||
3414 | static int | |
3415 | finish_composition (charbuf, cmp_status) | |
3416 | int *charbuf; | |
3417 | struct composition_status *cmp_status; | |
3418 | { | |
3419 | int idx = - cmp_status->length; | |
3420 | int new_chars; | |
3421 | ||
3422 | /* Recover the original ESC sequence */ | |
3423 | charbuf[idx++] = ISO_CODE_ESC; | |
3424 | charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0' | |
3425 | : cmp_status->method == COMPOSITION_WITH_RULE ? '2' | |
3426 | : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3' | |
3427 | /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */ | |
3428 | : '4'); | |
3429 | charbuf[idx++] = -2; | |
3430 | charbuf[idx++] = 0; | |
3431 | charbuf[idx++] = -1; | |
3432 | new_chars = cmp_status->nchars; | |
3433 | if (cmp_status->method >= COMPOSITION_WITH_RULE) | |
3434 | for (; idx < 0; idx++) | |
3435 | { | |
3436 | int elt = charbuf[idx]; | |
3437 | ||
3438 | if (elt == -2) | |
3439 | { | |
3440 | ENCODE_COMPOSITION_RULE (charbuf[idx + 1]); | |
3441 | idx++; | |
3442 | } | |
3443 | else if (elt == -1) | |
3444 | { | |
3445 | charbuf[idx++] = ISO_CODE_ESC; | |
3446 | charbuf[idx] = '0'; | |
3447 | new_chars += 2; | |
3448 | } | |
3449 | } | |
3450 | cmp_status->state = COMPOSING_NO; | |
3451 | return new_chars; | |
3452 | } | |
3453 | ||
3454 | /* If characers are under composition, finish the composition. */ | |
3455 | #define MAYBE_FINISH_COMPOSITION() \ | |
3456 | do { \ | |
3457 | if (cmp_status->state != COMPOSING_NO) \ | |
3458 | char_offset += finish_composition (charbuf, cmp_status); \ | |
3459 | } while (0) | |
d46c5b12 | 3460 | |
aa72b389 | 3461 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
e951386e | 3462 | |
aa72b389 KH |
3463 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 |
3464 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
3465 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
3466 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
ec6d2bb8 | 3467 | |
e951386e KH |
3468 | Produce this annotation sequence now: |
3469 | ||
3470 | [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ] | |
3471 | */ | |
3472 | ||
3473 | #define DECODE_COMPOSITION_START(c1) \ | |
3474 | do { \ | |
3475 | if (c1 == '0' \ | |
3476 | && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \ | |
3477 | && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ | |
3478 | || (cmp_status->state == COMPOSING_COMPONENT_RULE \ | |
3479 | && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \ | |
3480 | { \ | |
3481 | *charbuf++ = -1; \ | |
3482 | *charbuf++= -1; \ | |
3483 | cmp_status->state = COMPOSING_CHAR; \ | |
3484 | cmp_status->length += 2; \ | |
3485 | } \ | |
3486 | else \ | |
3487 | { \ | |
3488 | MAYBE_FINISH_COMPOSITION (); \ | |
3489 | cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
3490 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
3491 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
3492 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
3493 | cmp_status->state \ | |
3494 | = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \ | |
3495 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
3496 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
3497 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
3498 | coding->annotated = 1; \ | |
3499 | } \ | |
ec6d2bb8 KH |
3500 | } while (0) |
3501 | ||
ec6d2bb8 | 3502 | |
e951386e | 3503 | /* Handle composition end sequence ESC 1. */ |
df7492f9 KH |
3504 | |
3505 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 3506 | do { \ |
e951386e KH |
3507 | if (cmp_status->nchars == 0 \ |
3508 | || ((cmp_status->state == COMPOSING_CHAR) \ | |
3509 | == (cmp_status->method == COMPOSITION_WITH_RULE))) \ | |
ec6d2bb8 | 3510 | { \ |
e951386e KH |
3511 | MAYBE_FINISH_COMPOSITION (); \ |
3512 | goto invalid_code; \ | |
ec6d2bb8 | 3513 | } \ |
e951386e KH |
3514 | if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ |
3515 | charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \ | |
3516 | else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \ | |
3517 | charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \ | |
3518 | charbuf[- cmp_status->length + 2] = cmp_status->nchars; \ | |
3519 | char_offset += cmp_status->nchars; \ | |
3520 | cmp_status->state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
3521 | } while (0) |
3522 | ||
e951386e | 3523 | /* Store a composition rule RULE in charbuf, and update cmp_status. */ |
df7492f9 | 3524 | |
e951386e KH |
3525 | #define STORE_COMPOSITION_RULE(rule) \ |
3526 | do { \ | |
3527 | *charbuf++ = -2; \ | |
3528 | *charbuf++ = rule; \ | |
3529 | cmp_status->length += 2; \ | |
3530 | cmp_status->state--; \ | |
3531 | } while (0) | |
ec6d2bb8 | 3532 | |
e951386e KH |
3533 | /* Store a composed char or a component char C in charbuf, and update |
3534 | cmp_status. */ | |
3535 | ||
3536 | #define STORE_COMPOSITION_CHAR(c) \ | |
ec6d2bb8 | 3537 | do { \ |
e951386e KH |
3538 | *charbuf++ = (c); \ |
3539 | cmp_status->length++; \ | |
3540 | if (cmp_status->state == COMPOSING_CHAR) \ | |
3541 | cmp_status->nchars++; \ | |
df7492f9 | 3542 | else \ |
e951386e KH |
3543 | cmp_status->ncomps++; \ |
3544 | if (cmp_status->method == COMPOSITION_WITH_RULE \ | |
3545 | || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \ | |
3546 | && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \ | |
3547 | cmp_status->state++; \ | |
ec6d2bb8 | 3548 | } while (0) |
88993dfd | 3549 | |
d46c5b12 | 3550 | |
4ed46869 KH |
3551 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
3552 | ||
b73bfc1c | 3553 | static void |
df7492f9 | 3554 | decode_coding_iso_2022 (coding) |
4ed46869 | 3555 | struct coding_system *coding; |
4ed46869 | 3556 | { |
8f924df7 KH |
3557 | const unsigned char *src = coding->source + coding->consumed; |
3558 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
3559 | const unsigned char *src_base; | |
69a80ea3 | 3560 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ff0dacd7 | 3561 | int *charbuf_end |
e951386e | 3562 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; |
df7492f9 | 3563 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 3564 | int multibytep = coding->src_multibyte; |
4ed46869 | 3565 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
3566 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
3567 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 3568 | int charset_id_2, charset_id_3; |
df7492f9 KH |
3569 | struct charset *charset; |
3570 | int c; | |
e951386e | 3571 | struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding); |
24a73b0a | 3572 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
3573 | int char_offset = coding->produced_char; |
3574 | int last_offset = char_offset; | |
3575 | int last_id = charset_ascii; | |
0a9564cb EZ |
3576 | int eol_crlf = |
3577 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 3578 | int byte_after_cr = -1; |
e951386e | 3579 | int i; |
df7492f9 | 3580 | |
24a73b0a | 3581 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 3582 | setup_iso_safe_charsets (attrs); |
287c57d7 KH |
3583 | /* Charset list may have been changed. */ |
3584 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
1b3b981b | 3585 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
b73bfc1c | 3586 | |
e951386e KH |
3587 | if (cmp_status->state != COMPOSING_NO) |
3588 | { | |
3589 | for (i = 0; i < cmp_status->length; i++) | |
3590 | *charbuf++ = cmp_status->carryover[i]; | |
3591 | coding->annotated = 1; | |
3592 | } | |
3593 | ||
b73bfc1c | 3594 | while (1) |
4ed46869 | 3595 | { |
463f5630 | 3596 | int c1, c2; |
b73bfc1c KH |
3597 | |
3598 | src_base = src; | |
df7492f9 KH |
3599 | consumed_chars_base = consumed_chars; |
3600 | ||
3601 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
3602 | { |
3603 | if (byte_after_cr >= 0) | |
3604 | src_base--; | |
3605 | break; | |
3606 | } | |
df7492f9 | 3607 | |
119852e7 KH |
3608 | if (byte_after_cr >= 0) |
3609 | c1 = byte_after_cr, byte_after_cr = -1; | |
3610 | else | |
3611 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
3612 | if (c1 < 0) |
3613 | goto invalid_code; | |
4ed46869 | 3614 | |
e951386e | 3615 | if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0) |
4ed46869 | 3616 | { |
e951386e KH |
3617 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
3618 | char_offset++; | |
3619 | CODING_ISO_EXTSEGMENT_LEN (coding)--; | |
3620 | continue; | |
3621 | } | |
3622 | ||
3623 | if (CODING_ISO_EMBEDDED_UTF_8 (coding)) | |
3624 | { | |
3625 | if (c1 == ISO_CODE_ESC) | |
ec6d2bb8 | 3626 | { |
e951386e KH |
3627 | if (src + 1 >= src_end) |
3628 | goto no_more_source; | |
3629 | *charbuf++ = ISO_CODE_ESC; | |
3630 | char_offset++; | |
3631 | if (src[0] == '%' && src[1] == '@') | |
df7492f9 | 3632 | { |
e951386e KH |
3633 | src += 2; |
3634 | consumed_chars += 2; | |
3635 | char_offset += 2; | |
3636 | /* We are sure charbuf can contain two more chars. */ | |
3637 | *charbuf++ = '%'; | |
3638 | *charbuf++ = '@'; | |
3639 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
df7492f9 | 3640 | } |
4ed46869 | 3641 | } |
e951386e KH |
3642 | else |
3643 | { | |
3644 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
3645 | char_offset++; | |
3646 | } | |
3647 | continue; | |
3648 | } | |
3649 | ||
3650 | if ((cmp_status->state == COMPOSING_RULE | |
3651 | || cmp_status->state == COMPOSING_COMPONENT_RULE) | |
3652 | && c1 != ISO_CODE_ESC) | |
3653 | { | |
3654 | int rule, nbytes; | |
3655 | ||
3656 | DECODE_COMPOSITION_RULE (rule, nbytes); | |
3657 | if (rule < 0) | |
3658 | goto invalid_code; | |
3659 | STORE_COMPOSITION_RULE (rule); | |
3660 | continue; | |
3661 | } | |
3662 | ||
3663 | /* We produce at most one character. */ | |
3664 | switch (iso_code_class [c1]) | |
3665 | { | |
3666 | case ISO_0x20_or_0x7F: | |
df7492f9 KH |
3667 | if (charset_id_0 < 0 |
3668 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
3669 | /* This is SPACE or DEL. */ |
3670 | charset = CHARSET_FROM_ID (charset_ascii); | |
3671 | else | |
3672 | charset = CHARSET_FROM_ID (charset_id_0); | |
3673 | break; | |
4ed46869 KH |
3674 | |
3675 | case ISO_graphic_plane_0: | |
134b9549 KH |
3676 | if (charset_id_0 < 0) |
3677 | charset = CHARSET_FROM_ID (charset_ascii); | |
3678 | else | |
3679 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
3680 | break; |
3681 | ||
3682 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
3683 | if (charset_id_1 < 0 |
3684 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
3685 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
3686 | goto invalid_code; | |
4ed46869 KH |
3687 | /* This is a graphic character, we fall down ... */ |
3688 | ||
3689 | case ISO_graphic_plane_1: | |
df7492f9 KH |
3690 | if (charset_id_1 < 0) |
3691 | goto invalid_code; | |
3692 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
3693 | break; |
3694 | ||
df7492f9 | 3695 | case ISO_control_0: |
119852e7 KH |
3696 | if (eol_crlf && c1 == '\r') |
3697 | ONE_MORE_BYTE (byte_after_cr); | |
df7492f9 KH |
3698 | MAYBE_FINISH_COMPOSITION (); |
3699 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
3700 | break; |
3701 | ||
df7492f9 | 3702 | case ISO_control_1: |
df7492f9 KH |
3703 | goto invalid_code; |
3704 | ||
4ed46869 | 3705 | case ISO_shift_out: |
df7492f9 KH |
3706 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3707 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3708 | goto invalid_code; | |
3709 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3710 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3711 | continue; |
4ed46869 KH |
3712 | |
3713 | case ISO_shift_in: | |
df7492f9 KH |
3714 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3715 | goto invalid_code; | |
3716 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3717 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3718 | continue; |
4ed46869 KH |
3719 | |
3720 | case ISO_single_shift_2_7: | |
3721 | case ISO_single_shift_2: | |
df7492f9 KH |
3722 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3723 | goto invalid_code; | |
4ed46869 KH |
3724 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3725 | c1 = 'N'; | |
3726 | goto label_escape_sequence; | |
3727 | ||
3728 | case ISO_single_shift_3: | |
df7492f9 KH |
3729 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3730 | goto invalid_code; | |
4ed46869 KH |
3731 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3732 | c1 = 'O'; | |
3733 | goto label_escape_sequence; | |
3734 | ||
3735 | case ISO_control_sequence_introducer: | |
3736 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3737 | c1 = '['; | |
3738 | goto label_escape_sequence; | |
3739 | ||
3740 | case ISO_escape: | |
3741 | ONE_MORE_BYTE (c1); | |
3742 | label_escape_sequence: | |
df7492f9 | 3743 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3744 | designation, direction specification, and character |
3745 | composition specification. */ | |
3746 | switch (c1) | |
3747 | { | |
3748 | case '&': /* revision of following character set */ | |
3749 | ONE_MORE_BYTE (c1); | |
3750 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3751 | goto invalid_code; |
4ed46869 KH |
3752 | ONE_MORE_BYTE (c1); |
3753 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3754 | goto invalid_code; |
4ed46869 KH |
3755 | ONE_MORE_BYTE (c1); |
3756 | goto label_escape_sequence; | |
3757 | ||
3758 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3759 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3760 | goto invalid_code; | |
134b9549 KH |
3761 | { |
3762 | int reg, chars96; | |
3763 | ||
3764 | ONE_MORE_BYTE (c1); | |
3765 | if (c1 >= '@' && c1 <= 'B') | |
3766 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3767 | or JISX0208.1980 */ |
134b9549 KH |
3768 | reg = 0, chars96 = 0; |
3769 | } | |
3770 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3771 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3772 | reg = c1 - 0x28, chars96 = 0; | |
3773 | ONE_MORE_BYTE (c1); | |
3774 | } | |
3775 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3776 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3777 | reg = c1 - 0x2C, chars96 = 1; | |
3778 | ONE_MORE_BYTE (c1); | |
3779 | } | |
3780 | else | |
3781 | goto invalid_code; | |
3782 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3783 | /* We must update these variables now. */ | |
3784 | if (reg == 0) | |
3785 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3786 | else if (reg == 1) | |
3787 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3788 | if (chars96 < 0) | |
3789 | goto invalid_code; | |
3790 | } | |
b73bfc1c | 3791 | continue; |
4ed46869 KH |
3792 | |
3793 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3794 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3795 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3796 | goto invalid_code; | |
3797 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3798 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3799 | continue; |
4ed46869 KH |
3800 | |
3801 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3802 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3803 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3804 | goto invalid_code; | |
3805 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3806 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3807 | continue; |
4ed46869 KH |
3808 | |
3809 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3810 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3811 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3812 | goto invalid_code; | |
134b9549 KH |
3813 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3814 | if (charset_id_2 < 0) | |
3815 | charset = CHARSET_FROM_ID (charset_ascii); | |
3816 | else | |
3817 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3818 | ONE_MORE_BYTE (c1); |
e7046a18 | 3819 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3820 | goto invalid_code; |
4ed46869 KH |
3821 | break; |
3822 | ||
3823 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3824 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3825 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3826 | goto invalid_code; | |
134b9549 KH |
3827 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3828 | if (charset_id_3 < 0) | |
3829 | charset = CHARSET_FROM_ID (charset_ascii); | |
3830 | else | |
3831 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3832 | ONE_MORE_BYTE (c1); |
e7046a18 | 3833 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3834 | goto invalid_code; |
4ed46869 KH |
3835 | break; |
3836 | ||
ec6d2bb8 | 3837 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3838 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3839 | goto invalid_code; | |
e951386e KH |
3840 | if (last_id != charset_ascii) |
3841 | { | |
3842 | ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id); | |
3843 | last_id = charset_ascii; | |
3844 | last_offset = char_offset; | |
3845 | } | |
ec6d2bb8 | 3846 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3847 | continue; |
4ed46869 | 3848 | |
ec6d2bb8 | 3849 | case '1': /* end composition */ |
e951386e | 3850 | if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3851 | goto invalid_code; |
3852 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3853 | continue; |
4ed46869 KH |
3854 | |
3855 | case '[': /* specification of direction */ | |
df7492f9 KH |
3856 | if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION) |
3857 | goto invalid_code; | |
4ed46869 | 3858 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3859 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
df7492f9 | 3860 | left-to-right, and nozero means right-to-left. */ |
4ed46869 KH |
3861 | ONE_MORE_BYTE (c1); |
3862 | switch (c1) | |
3863 | { | |
3864 | case ']': /* end of the current direction */ | |
d46c5b12 | 3865 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3866 | |
3867 | case '0': /* end of the current direction */ | |
3868 | case '1': /* start of left-to-right direction */ | |
3869 | ONE_MORE_BYTE (c1); | |
3870 | if (c1 == ']') | |
d46c5b12 | 3871 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3872 | else |
df7492f9 | 3873 | goto invalid_code; |
4ed46869 KH |
3874 | break; |
3875 | ||
3876 | case '2': /* start of right-to-left direction */ | |
3877 | ONE_MORE_BYTE (c1); | |
3878 | if (c1 == ']') | |
d46c5b12 | 3879 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3880 | else |
df7492f9 | 3881 | goto invalid_code; |
4ed46869 KH |
3882 | break; |
3883 | ||
3884 | default: | |
df7492f9 | 3885 | goto invalid_code; |
4ed46869 | 3886 | } |
b73bfc1c | 3887 | continue; |
4ed46869 | 3888 | |
103e0180 | 3889 | case '%': |
103e0180 KH |
3890 | ONE_MORE_BYTE (c1); |
3891 | if (c1 == '/') | |
3892 | { | |
3893 | /* CTEXT extended segment: | |
3894 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3895 | We keep these bytes as is for the moment. | |
3896 | They may be decoded by post-read-conversion. */ | |
3897 | int dim, M, L; | |
4776e638 | 3898 | int size; |
8f924df7 | 3899 | |
103e0180 | 3900 | ONE_MORE_BYTE (dim); |
e951386e KH |
3901 | if (dim < 0 || dim > 4) |
3902 | goto invalid_code; | |
103e0180 | 3903 | ONE_MORE_BYTE (M); |
e951386e KH |
3904 | if (M < 128) |
3905 | goto invalid_code; | |
103e0180 | 3906 | ONE_MORE_BYTE (L); |
e951386e KH |
3907 | if (L < 128) |
3908 | goto invalid_code; | |
103e0180 | 3909 | size = ((M - 128) * 128) + (L - 128); |
e951386e | 3910 | if (charbuf + 6 > charbuf_end) |
4776e638 KH |
3911 | goto break_loop; |
3912 | *charbuf++ = ISO_CODE_ESC; | |
3913 | *charbuf++ = '%'; | |
3914 | *charbuf++ = '/'; | |
3915 | *charbuf++ = dim; | |
3916 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3917 | *charbuf++ = BYTE8_TO_CHAR (L); | |
e951386e | 3918 | CODING_ISO_EXTSEGMENT_LEN (coding) = size; |
103e0180 KH |
3919 | } |
3920 | else if (c1 == 'G') | |
3921 | { | |
103e0180 KH |
3922 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3923 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3924 | We keep these bytes as is for the moment. | |
3925 | They may be decoded by post-read-conversion. */ | |
e951386e | 3926 | if (charbuf + 3 > charbuf_end) |
4776e638 | 3927 | goto break_loop; |
e951386e KH |
3928 | *charbuf++ = ISO_CODE_ESC; |
3929 | *charbuf++ = '%'; | |
3930 | *charbuf++ = 'G'; | |
3931 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 1; | |
103e0180 KH |
3932 | } |
3933 | else | |
4776e638 | 3934 | goto invalid_code; |
103e0180 | 3935 | continue; |
4776e638 | 3936 | break; |
103e0180 | 3937 | |
4ed46869 | 3938 | default: |
df7492f9 KH |
3939 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3940 | goto invalid_code; | |
134b9549 KH |
3941 | { |
3942 | int reg, chars96; | |
3943 | ||
3944 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3945 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3946 | reg = c1 - 0x28, chars96 = 0; | |
3947 | ONE_MORE_BYTE (c1); | |
3948 | } | |
3949 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3950 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3951 | reg = c1 - 0x2C, chars96 = 1; | |
3952 | ONE_MORE_BYTE (c1); | |
3953 | } | |
3954 | else | |
3955 | goto invalid_code; | |
3956 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3957 | /* We must update these variables now. */ | |
3958 | if (reg == 0) | |
3959 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3960 | else if (reg == 1) | |
3961 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3962 | if (chars96 < 0) | |
3963 | goto invalid_code; | |
3964 | } | |
b73bfc1c | 3965 | continue; |
4ed46869 | 3966 | } |
b73bfc1c | 3967 | } |
4ed46869 | 3968 | |
e951386e KH |
3969 | if (cmp_status->state == COMPOSING_NO |
3970 | && charset->id != charset_ascii | |
ff0dacd7 KH |
3971 | && last_id != charset->id) |
3972 | { | |
3973 | if (last_id != charset_ascii) | |
69a80ea3 | 3974 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3975 | last_id = charset->id; |
3976 | last_offset = char_offset; | |
3977 | } | |
3978 | ||
b73bfc1c | 3979 | /* Now we know CHARSET and 1st position code C1 of a character. |
df7492f9 KH |
3980 | Produce a decoded character while getting 2nd position code |
3981 | C2 if necessary. */ | |
3982 | c1 &= 0x7F; | |
3983 | if (CHARSET_DIMENSION (charset) > 1) | |
b73bfc1c KH |
3984 | { |
3985 | ONE_MORE_BYTE (c2); | |
df7492f9 | 3986 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)) |
b73bfc1c | 3987 | /* C2 is not in a valid range. */ |
df7492f9 KH |
3988 | goto invalid_code; |
3989 | c1 = (c1 << 8) | (c2 & 0x7F); | |
3990 | if (CHARSET_DIMENSION (charset) > 2) | |
3991 | { | |
3992 | ONE_MORE_BYTE (c2); | |
3993 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)) | |
3994 | /* C2 is not in a valid range. */ | |
3995 | goto invalid_code; | |
3996 | c1 = (c1 << 8) | (c2 & 0x7F); | |
3997 | } | |
3998 | } | |
3999 | ||
4000 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); | |
4001 | if (c < 0) | |
4002 | { | |
4003 | MAYBE_FINISH_COMPOSITION (); | |
4004 | for (; src_base < src; src_base++, char_offset++) | |
4005 | { | |
4006 | if (ASCII_BYTE_P (*src_base)) | |
4007 | *charbuf++ = *src_base; | |
4008 | else | |
4009 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
4010 | } | |
4011 | } | |
e951386e | 4012 | else if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
4013 | { |
4014 | *charbuf++ = c; | |
4015 | char_offset++; | |
4ed46869 | 4016 | } |
e951386e KH |
4017 | else if ((cmp_status->state == COMPOSING_CHAR |
4018 | ? cmp_status->nchars | |
4019 | : cmp_status->ncomps) | |
4020 | >= MAX_COMPOSITION_COMPONENTS) | |
781d7a48 | 4021 | { |
e951386e KH |
4022 | /* Too long composition. */ |
4023 | MAYBE_FINISH_COMPOSITION (); | |
4024 | *charbuf++ = c; | |
4025 | char_offset++; | |
4ed46869 | 4026 | } |
e951386e KH |
4027 | else |
4028 | STORE_COMPOSITION_CHAR (c); | |
4ed46869 KH |
4029 | continue; |
4030 | ||
df7492f9 KH |
4031 | invalid_code: |
4032 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 4033 | src = src_base; |
df7492f9 KH |
4034 | consumed_chars = consumed_chars_base; |
4035 | ONE_MORE_BYTE (c); | |
065e3595 | 4036 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4037 | char_offset++; |
df7492f9 | 4038 | coding->errors++; |
4776e638 KH |
4039 | continue; |
4040 | ||
4041 | break_loop: | |
4042 | break; | |
4ed46869 | 4043 | } |
fb88bf2d | 4044 | |
df7492f9 | 4045 | no_more_source: |
e951386e KH |
4046 | if (cmp_status->state != COMPOSING_NO) |
4047 | { | |
4048 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
4049 | MAYBE_FINISH_COMPOSITION (); | |
4050 | else | |
4051 | { | |
4052 | charbuf -= cmp_status->length; | |
4053 | for (i = 0; i < cmp_status->length; i++) | |
4054 | cmp_status->carryover[i] = charbuf[i]; | |
4055 | } | |
4056 | } | |
4057 | else if (last_id != charset_ascii) | |
69a80ea3 | 4058 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4059 | coding->consumed_char += consumed_chars_base; |
4060 | coding->consumed = src_base - coding->source; | |
4061 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4062 | } |
4063 | ||
b73bfc1c | 4064 | |
f4dee582 | 4065 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
4066 | |
4067 | /* | |
f4dee582 | 4068 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 4069 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 4070 | variant has the following specifications: |
df7492f9 | 4071 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
4072 | 2. Allows short-form designation? |
4073 | 3. ASCII should be designated to G0 before control characters? | |
4074 | 4. ASCII should be designated to G0 at end of line? | |
4075 | 5. 7-bit environment or 8-bit environment? | |
4076 | 6. Use locking-shift? | |
4077 | 7. Use Single-shift? | |
4078 | And the following two are only for Japanese: | |
4079 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
4080 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
4081 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
4082 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 4083 | details. |
4ed46869 KH |
4084 | */ |
4085 | ||
4086 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
4087 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
4088 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
4089 | designation sequence of short-form. */ | |
4ed46869 KH |
4090 | |
4091 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
4092 | do { \ | |
df7492f9 | 4093 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
4ed46869 KH |
4094 | char *intermediate_char_94 = "()*+"; \ |
4095 | char *intermediate_char_96 = ",-./"; \ | |
df7492f9 KH |
4096 | int revision = -1; \ |
4097 | int c; \ | |
4098 | \ | |
4099 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 4100 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
4101 | \ |
4102 | if (revision >= 0) \ | |
70c22245 | 4103 | { \ |
df7492f9 KH |
4104 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
4105 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 4106 | } \ |
df7492f9 | 4107 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
4108 | if (CHARSET_DIMENSION (charset) == 1) \ |
4109 | { \ | |
df7492f9 KH |
4110 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
4111 | c = intermediate_char_94[reg]; \ | |
4ed46869 | 4112 | else \ |
df7492f9 KH |
4113 | c = intermediate_char_96[reg]; \ |
4114 | EMIT_ONE_ASCII_BYTE (c); \ | |
4ed46869 KH |
4115 | } \ |
4116 | else \ | |
4117 | { \ | |
df7492f9 KH |
4118 | EMIT_ONE_ASCII_BYTE ('$'); \ |
4119 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 4120 | { \ |
df7492f9 | 4121 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
4122 | || reg != 0 \ |
4123 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 4124 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
4125 | } \ |
4126 | else \ | |
df7492f9 | 4127 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 4128 | } \ |
df7492f9 KH |
4129 | EMIT_ONE_ASCII_BYTE (final_char); \ |
4130 | \ | |
4131 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
4132 | } while (0) |
4133 | ||
df7492f9 | 4134 | |
4ed46869 KH |
4135 | /* The following two macros produce codes (control character or escape |
4136 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
4137 | single-shift-3). */ | |
4138 | ||
df7492f9 KH |
4139 | #define ENCODE_SINGLE_SHIFT_2 \ |
4140 | do { \ | |
4141 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4142 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
4143 | else \ | |
4144 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
4145 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4146 | } while (0) |
4147 | ||
df7492f9 KH |
4148 | |
4149 | #define ENCODE_SINGLE_SHIFT_3 \ | |
4150 | do { \ | |
4151 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4152 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
4153 | else \ | |
4154 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
4155 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4156 | } while (0) |
4157 | ||
df7492f9 | 4158 | |
4ed46869 KH |
4159 | /* The following four macros produce codes (control character or |
4160 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
4161 | shift-out, locking-shift-2, and locking-shift-3). */ | |
4162 | ||
df7492f9 KH |
4163 | #define ENCODE_SHIFT_IN \ |
4164 | do { \ | |
4165 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
4166 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
4167 | } while (0) |
4168 | ||
df7492f9 KH |
4169 | |
4170 | #define ENCODE_SHIFT_OUT \ | |
4171 | do { \ | |
4172 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
4173 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
4174 | } while (0) |
4175 | ||
df7492f9 KH |
4176 | |
4177 | #define ENCODE_LOCKING_SHIFT_2 \ | |
4178 | do { \ | |
4179 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4180 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
4181 | } while (0) |
4182 | ||
df7492f9 KH |
4183 | |
4184 | #define ENCODE_LOCKING_SHIFT_3 \ | |
4185 | do { \ | |
4186 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4187 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
4188 | } while (0) |
4189 | ||
df7492f9 | 4190 | |
f4dee582 RS |
4191 | /* Produce codes for a DIMENSION1 character whose character set is |
4192 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
4193 | sequences are also produced in advance if necessary. */ |
4194 | ||
6e85d753 KH |
4195 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
4196 | do { \ | |
df7492f9 | 4197 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4198 | \ |
4199 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
4200 | && id == charset_ascii) \ | |
4201 | { \ | |
4202 | id = charset_jisx0201_roman; \ | |
4203 | charset = CHARSET_FROM_ID (id); \ | |
4204 | } \ | |
4205 | \ | |
df7492f9 | 4206 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4207 | { \ |
df7492f9 KH |
4208 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4209 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 4210 | else \ |
df7492f9 KH |
4211 | EMIT_ONE_BYTE (c1 | 0x80); \ |
4212 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4213 | break; \ |
4214 | } \ | |
df7492f9 | 4215 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4216 | { \ |
df7492f9 | 4217 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
4218 | break; \ |
4219 | } \ | |
df7492f9 | 4220 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4221 | { \ |
df7492f9 | 4222 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
4223 | break; \ |
4224 | } \ | |
6e85d753 KH |
4225 | else \ |
4226 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4227 | must invoke it, or, at first, designate it to some graphic \ | |
4228 | register. Then repeat the loop to actually produce the \ | |
4229 | character. */ \ | |
df7492f9 KH |
4230 | dst = encode_invocation_designation (charset, coding, dst, \ |
4231 | &produced_chars); \ | |
4ed46869 KH |
4232 | } while (1) |
4233 | ||
df7492f9 | 4234 | |
f4dee582 RS |
4235 | /* Produce codes for a DIMENSION2 character whose character set is |
4236 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
4237 | invocation codes are also produced in advance if necessary. */ |
4238 | ||
6e85d753 KH |
4239 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
4240 | do { \ | |
df7492f9 | 4241 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4242 | \ |
4243 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
4244 | && id == charset_jisx0208) \ | |
4245 | { \ | |
4246 | id = charset_jisx0208_1978; \ | |
4247 | charset = CHARSET_FROM_ID (id); \ | |
4248 | } \ | |
4249 | \ | |
df7492f9 | 4250 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4251 | { \ |
df7492f9 KH |
4252 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4253 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 4254 | else \ |
df7492f9 KH |
4255 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
4256 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4257 | break; \ |
4258 | } \ | |
df7492f9 | 4259 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4260 | { \ |
df7492f9 | 4261 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
4262 | break; \ |
4263 | } \ | |
df7492f9 | 4264 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4265 | { \ |
df7492f9 | 4266 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
4267 | break; \ |
4268 | } \ | |
6e85d753 KH |
4269 | else \ |
4270 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4271 | must invoke it, or, at first, designate it to some graphic \ | |
4272 | register. Then repeat the loop to actually produce the \ | |
4273 | character. */ \ | |
df7492f9 KH |
4274 | dst = encode_invocation_designation (charset, coding, dst, \ |
4275 | &produced_chars); \ | |
4ed46869 KH |
4276 | } while (1) |
4277 | ||
05e6f5dc | 4278 | |
df7492f9 KH |
4279 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
4280 | do { \ | |
4281 | int code = ENCODE_CHAR ((charset),(c)); \ | |
4282 | \ | |
4283 | if (CHARSET_DIMENSION (charset) == 1) \ | |
4284 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
4285 | else \ | |
4286 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 4287 | } while (0) |
bdd9fb48 | 4288 | |
05e6f5dc | 4289 | |
4ed46869 | 4290 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 4291 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
4292 | Return new DST. */ |
4293 | ||
4294 | unsigned char * | |
df7492f9 KH |
4295 | encode_invocation_designation (charset, coding, dst, p_nchars) |
4296 | struct charset *charset; | |
4ed46869 KH |
4297 | struct coding_system *coding; |
4298 | unsigned char *dst; | |
df7492f9 | 4299 | int *p_nchars; |
4ed46869 | 4300 | { |
df7492f9 KH |
4301 | int multibytep = coding->dst_multibyte; |
4302 | int produced_chars = *p_nchars; | |
4ed46869 | 4303 | int reg; /* graphic register number */ |
df7492f9 | 4304 | int id = CHARSET_ID (charset); |
4ed46869 KH |
4305 | |
4306 | /* At first, check designations. */ | |
4307 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 4308 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
4309 | break; |
4310 | ||
4311 | if (reg >= 4) | |
4312 | { | |
4313 | /* CHARSET is not yet designated to any graphic registers. */ | |
4314 | /* At first check the requested designation. */ | |
df7492f9 KH |
4315 | reg = CODING_ISO_REQUEST (coding, id); |
4316 | if (reg < 0) | |
1ba9e4ab KH |
4317 | /* Since CHARSET requests no special designation, designate it |
4318 | to graphic register 0. */ | |
4ed46869 KH |
4319 | reg = 0; |
4320 | ||
4321 | ENCODE_DESIGNATION (charset, reg, coding); | |
4322 | } | |
4323 | ||
df7492f9 KH |
4324 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
4325 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
4326 | { |
4327 | /* Since the graphic register REG is not invoked to any graphic | |
4328 | planes, invoke it to graphic plane 0. */ | |
4329 | switch (reg) | |
4330 | { | |
4331 | case 0: /* graphic register 0 */ | |
4332 | ENCODE_SHIFT_IN; | |
4333 | break; | |
4334 | ||
4335 | case 1: /* graphic register 1 */ | |
4336 | ENCODE_SHIFT_OUT; | |
4337 | break; | |
4338 | ||
4339 | case 2: /* graphic register 2 */ | |
df7492f9 | 4340 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4341 | ENCODE_SINGLE_SHIFT_2; |
4342 | else | |
4343 | ENCODE_LOCKING_SHIFT_2; | |
4344 | break; | |
4345 | ||
4346 | case 3: /* graphic register 3 */ | |
df7492f9 | 4347 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4348 | ENCODE_SINGLE_SHIFT_3; |
4349 | else | |
4350 | ENCODE_LOCKING_SHIFT_3; | |
4351 | break; | |
4352 | } | |
4353 | } | |
b73bfc1c | 4354 | |
df7492f9 | 4355 | *p_nchars = produced_chars; |
4ed46869 KH |
4356 | return dst; |
4357 | } | |
4358 | ||
df7492f9 KH |
4359 | /* The following three macros produce codes for indicating direction |
4360 | of text. */ | |
4361 | #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \ | |
ec6d2bb8 | 4362 | do { \ |
df7492f9 KH |
4363 | if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \ |
4364 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \ | |
ec6d2bb8 | 4365 | else \ |
df7492f9 | 4366 | EMIT_ONE_BYTE (ISO_CODE_CSI); \ |
ec6d2bb8 KH |
4367 | } while (0) |
4368 | ||
ec6d2bb8 | 4369 | |
df7492f9 KH |
4370 | #define ENCODE_DIRECTION_R2L() \ |
4371 | do { \ | |
4372 | ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \ | |
4373 | EMIT_TWO_ASCII_BYTES ('2', ']'); \ | |
ec6d2bb8 KH |
4374 | } while (0) |
4375 | ||
ec6d2bb8 | 4376 | |
df7492f9 | 4377 | #define ENCODE_DIRECTION_L2R() \ |
ec6d2bb8 | 4378 | do { \ |
df7492f9 KH |
4379 | ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \ |
4380 | EMIT_TWO_ASCII_BYTES ('0', ']'); \ | |
ec6d2bb8 | 4381 | } while (0) |
4ed46869 | 4382 | |
4ed46869 KH |
4383 | |
4384 | /* Produce codes for designation and invocation to reset the graphic | |
4385 | planes and registers to initial state. */ | |
df7492f9 KH |
4386 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
4387 | do { \ | |
4388 | int reg; \ | |
4389 | struct charset *charset; \ | |
4390 | \ | |
4391 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
4392 | ENCODE_SHIFT_IN; \ | |
4393 | for (reg = 0; reg < 4; reg++) \ | |
4394 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
4395 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
4396 | != CODING_ISO_INITIAL (coding, reg))) \ | |
4397 | { \ | |
4398 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
4399 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
4400 | } \ | |
4ed46869 KH |
4401 | } while (0) |
4402 | ||
df7492f9 | 4403 | |
bdd9fb48 | 4404 | /* Produce designation sequences of charsets in the line started from |
b73bfc1c | 4405 | SRC to a place pointed by DST, and return updated DST. |
bdd9fb48 KH |
4406 | |
4407 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
4408 | find all the necessary designations. */ |
4409 | ||
b73bfc1c | 4410 | static unsigned char * |
df7492f9 | 4411 | encode_designation_at_bol (coding, charbuf, charbuf_end, dst) |
e0e989f6 | 4412 | struct coding_system *coding; |
df7492f9 KH |
4413 | int *charbuf, *charbuf_end; |
4414 | unsigned char *dst; | |
e0e989f6 | 4415 | { |
df7492f9 | 4416 | struct charset *charset; |
bdd9fb48 KH |
4417 | /* Table of charsets to be designated to each graphic register. */ |
4418 | int r[4]; | |
df7492f9 KH |
4419 | int c, found = 0, reg; |
4420 | int produced_chars = 0; | |
4421 | int multibytep = coding->dst_multibyte; | |
4422 | Lisp_Object attrs; | |
4423 | Lisp_Object charset_list; | |
4424 | ||
4425 | attrs = CODING_ID_ATTRS (coding->id); | |
4426 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
4427 | if (EQ (charset_list, Qiso_2022)) | |
4428 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
4429 | |
4430 | for (reg = 0; reg < 4; reg++) | |
4431 | r[reg] = -1; | |
4432 | ||
b73bfc1c | 4433 | while (found < 4) |
e0e989f6 | 4434 | { |
df7492f9 KH |
4435 | int id; |
4436 | ||
4437 | c = *charbuf++; | |
b73bfc1c KH |
4438 | if (c == '\n') |
4439 | break; | |
df7492f9 KH |
4440 | charset = char_charset (c, charset_list, NULL); |
4441 | id = CHARSET_ID (charset); | |
4442 | reg = CODING_ISO_REQUEST (coding, id); | |
4443 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
4444 | { |
4445 | found++; | |
df7492f9 | 4446 | r[reg] = id; |
bdd9fb48 | 4447 | } |
bdd9fb48 KH |
4448 | } |
4449 | ||
4450 | if (found) | |
4451 | { | |
4452 | for (reg = 0; reg < 4; reg++) | |
4453 | if (r[reg] >= 0 | |
df7492f9 KH |
4454 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
4455 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 4456 | } |
b73bfc1c KH |
4457 | |
4458 | return dst; | |
e0e989f6 KH |
4459 | } |
4460 | ||
4ed46869 KH |
4461 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
4462 | ||
df7492f9 KH |
4463 | static int |
4464 | encode_coding_iso_2022 (coding) | |
4ed46869 | 4465 | struct coding_system *coding; |
4ed46869 | 4466 | { |
df7492f9 KH |
4467 | int multibytep = coding->dst_multibyte; |
4468 | int *charbuf = coding->charbuf; | |
4469 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4470 | unsigned char *dst = coding->destination + coding->produced; | |
4471 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4472 | int safe_room = 16; | |
4473 | int bol_designation | |
4474 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL | |
4475 | && CODING_ISO_BOL (coding)); | |
4476 | int produced_chars = 0; | |
4477 | Lisp_Object attrs, eol_type, charset_list; | |
4478 | int ascii_compatible; | |
b73bfc1c | 4479 | int c; |
ff0dacd7 | 4480 | int preferred_charset_id = -1; |
05e6f5dc | 4481 | |
24a73b0a | 4482 | CODING_GET_INFO (coding, attrs, charset_list); |
0a9564cb | 4483 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
24a73b0a KH |
4484 | if (VECTORP (eol_type)) |
4485 | eol_type = Qunix; | |
4486 | ||
004068e4 | 4487 | setup_iso_safe_charsets (attrs); |
ff0dacd7 | 4488 | /* Charset list may have been changed. */ |
287c57d7 | 4489 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); |
1b3b981b | 4490 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
0eecad43 | 4491 | |
df7492f9 | 4492 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
bdd9fb48 | 4493 | |
df7492f9 | 4494 | while (charbuf < charbuf_end) |
4ed46869 | 4495 | { |
df7492f9 | 4496 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 4497 | |
df7492f9 | 4498 | if (bol_designation) |
b73bfc1c | 4499 | { |
df7492f9 | 4500 | unsigned char *dst_prev = dst; |
4ed46869 | 4501 | |
bdd9fb48 | 4502 | /* We have to produce designation sequences if any now. */ |
df7492f9 KH |
4503 | dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst); |
4504 | bol_designation = 0; | |
4505 | /* We are sure that designation sequences are all ASCII bytes. */ | |
4506 | produced_chars += dst - dst_prev; | |
e0e989f6 KH |
4507 | } |
4508 | ||
df7492f9 | 4509 | c = *charbuf++; |
ec6d2bb8 | 4510 | |
ff0dacd7 KH |
4511 | if (c < 0) |
4512 | { | |
4513 | /* Handle an annotation. */ | |
4514 | switch (*charbuf) | |
ec6d2bb8 | 4515 | { |
ff0dacd7 KH |
4516 | case CODING_ANNOTATE_COMPOSITION_MASK: |
4517 | /* Not yet implemented. */ | |
4518 | break; | |
4519 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 4520 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
4521 | if (preferred_charset_id >= 0 |
4522 | && NILP (Fmemq (make_number (preferred_charset_id), | |
4523 | charset_list))) | |
4524 | preferred_charset_id = -1; | |
4525 | break; | |
4526 | default: | |
4527 | abort (); | |
4ed46869 | 4528 | } |
ff0dacd7 KH |
4529 | charbuf += -c - 1; |
4530 | continue; | |
4ed46869 | 4531 | } |
ec6d2bb8 | 4532 | |
b73bfc1c KH |
4533 | /* Now encode the character C. */ |
4534 | if (c < 0x20 || c == 0x7F) | |
4535 | { | |
df7492f9 KH |
4536 | if (c == '\n' |
4537 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 4538 | { |
df7492f9 KH |
4539 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
4540 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4541 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 4542 | { |
df7492f9 KH |
4543 | int i; |
4544 | ||
4545 | for (i = 0; i < 4; i++) | |
4546 | CODING_ISO_DESIGNATION (coding, i) | |
4547 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 4548 | } |
df7492f9 KH |
4549 | bol_designation |
4550 | = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL; | |
19a8d9e0 | 4551 | } |
df7492f9 KH |
4552 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
4553 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4554 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 4555 | } |
df7492f9 | 4556 | else if (ASCII_CHAR_P (c)) |
88993dfd | 4557 | { |
df7492f9 KH |
4558 | if (ascii_compatible) |
4559 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 4560 | else |
19a8d9e0 | 4561 | { |
bf16eb23 KH |
4562 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
4563 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 4564 | } |
4ed46869 | 4565 | } |
16eafb5d | 4566 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 4567 | { |
16eafb5d KH |
4568 | c = CHAR_TO_BYTE8 (c); |
4569 | EMIT_ONE_BYTE (c); | |
88993dfd | 4570 | } |
b73bfc1c | 4571 | else |
df7492f9 | 4572 | { |
ff0dacd7 | 4573 | struct charset *charset; |
b73bfc1c | 4574 | |
ff0dacd7 KH |
4575 | if (preferred_charset_id >= 0) |
4576 | { | |
4577 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
4578 | if (! CHAR_CHARSET_P (c, charset)) | |
4579 | charset = char_charset (c, charset_list, NULL); | |
4580 | } | |
4581 | else | |
4582 | charset = char_charset (c, charset_list, NULL); | |
df7492f9 KH |
4583 | if (!charset) |
4584 | { | |
41cbe562 KH |
4585 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
4586 | { | |
4587 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4588 | charset = CHARSET_FROM_ID (charset_ascii); | |
4589 | } | |
4590 | else | |
4591 | { | |
4592 | c = coding->default_char; | |
4593 | charset = char_charset (c, charset_list, NULL); | |
4594 | } | |
df7492f9 KH |
4595 | } |
4596 | ENCODE_ISO_CHARACTER (charset, c); | |
4597 | } | |
84fbb8a0 | 4598 | } |
b73bfc1c | 4599 | |
df7492f9 KH |
4600 | if (coding->mode & CODING_MODE_LAST_BLOCK |
4601 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
4602 | { | |
4603 | ASSURE_DESTINATION (safe_room); | |
4604 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4605 | } | |
065e3595 | 4606 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4607 | CODING_ISO_BOL (coding) = bol_designation; |
4608 | coding->produced_char += produced_chars; | |
4609 | coding->produced = dst - coding->destination; | |
4610 | return 0; | |
4ed46869 KH |
4611 | } |
4612 | ||
4613 | \f | |
df7492f9 | 4614 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 4615 | |
df7492f9 | 4616 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
4617 | quite widely. So, for the moment, Emacs supports them in the bare |
4618 | C code. But, in the future, they may be supported only by CCL. */ | |
4619 | ||
4620 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
4621 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
4622 | as is. A character of charset katakana-jisx0201 is encoded by | |
4623 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
4624 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 4625 | so that it fit in the range below. |
4ed46869 KH |
4626 | |
4627 | --- CODE RANGE of SJIS --- | |
4628 | (character set) (range) | |
4629 | ASCII 0x00 .. 0x7F | |
df7492f9 | 4630 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 4631 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 4632 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
4633 | ------------------------------- |
4634 | ||
4635 | */ | |
4636 | ||
4637 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
4638 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 4639 | character set and is encoded in two-byte. |
4ed46869 KH |
4640 | |
4641 | --- CODE RANGE of BIG5 --- | |
4642 | (character set) (range) | |
4643 | ASCII 0x00 .. 0x7F | |
4644 | Big5 (1st byte) 0xA1 .. 0xFE | |
4645 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
4646 | -------------------------- | |
4647 | ||
df7492f9 | 4648 | */ |
4ed46869 KH |
4649 | |
4650 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4651 | Check if a text is encoded in SJIS. If it is, return | |
df7492f9 | 4652 | CATEGORY_MASK_SJIS, else return 0. */ |
4ed46869 | 4653 | |
0a28aafb | 4654 | static int |
ff0dacd7 | 4655 | detect_coding_sjis (coding, detect_info) |
df7492f9 | 4656 | struct coding_system *coding; |
ff0dacd7 | 4657 | struct coding_detection_info *detect_info; |
4ed46869 | 4658 | { |
065e3595 | 4659 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4660 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4661 | int multibytep = coding->src_multibyte; |
4662 | int consumed_chars = 0; | |
4663 | int found = 0; | |
b73bfc1c | 4664 | int c; |
df7492f9 | 4665 | |
ff0dacd7 | 4666 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4667 | /* A coding system of this category is always ASCII compatible. */ |
4668 | src += coding->head_ascii; | |
4ed46869 | 4669 | |
b73bfc1c | 4670 | while (1) |
4ed46869 | 4671 | { |
065e3595 | 4672 | src_base = src; |
df7492f9 | 4673 | ONE_MORE_BYTE (c); |
682169fe KH |
4674 | if (c < 0x80) |
4675 | continue; | |
df7492f9 | 4676 | if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF)) |
4ed46869 | 4677 | { |
df7492f9 | 4678 | ONE_MORE_BYTE (c); |
682169fe | 4679 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 4680 | break; |
ff0dacd7 | 4681 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 4682 | } |
df7492f9 | 4683 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 4684 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
4685 | else |
4686 | break; | |
4ed46869 | 4687 | } |
ff0dacd7 | 4688 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4689 | return 0; |
4690 | ||
4691 | no_more_source: | |
065e3595 | 4692 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4693 | { |
ff0dacd7 | 4694 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 4695 | return 0; |
4ed46869 | 4696 | } |
ff0dacd7 KH |
4697 | detect_info->found |= found; |
4698 | return 1; | |
4ed46869 KH |
4699 | } |
4700 | ||
4701 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4702 | Check if a text is encoded in BIG5. If it is, return | |
df7492f9 | 4703 | CATEGORY_MASK_BIG5, else return 0. */ |
4ed46869 | 4704 | |
0a28aafb | 4705 | static int |
ff0dacd7 | 4706 | detect_coding_big5 (coding, detect_info) |
df7492f9 | 4707 | struct coding_system *coding; |
ff0dacd7 | 4708 | struct coding_detection_info *detect_info; |
4ed46869 | 4709 | { |
065e3595 | 4710 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4711 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4712 | int multibytep = coding->src_multibyte; |
4713 | int consumed_chars = 0; | |
4714 | int found = 0; | |
b73bfc1c | 4715 | int c; |
fa42c37f | 4716 | |
ff0dacd7 | 4717 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4718 | /* A coding system of this category is always ASCII compatible. */ |
4719 | src += coding->head_ascii; | |
fa42c37f | 4720 | |
b73bfc1c | 4721 | while (1) |
fa42c37f | 4722 | { |
065e3595 | 4723 | src_base = src; |
df7492f9 KH |
4724 | ONE_MORE_BYTE (c); |
4725 | if (c < 0x80) | |
fa42c37f | 4726 | continue; |
df7492f9 | 4727 | if (c >= 0xA1) |
fa42c37f | 4728 | { |
df7492f9 KH |
4729 | ONE_MORE_BYTE (c); |
4730 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4731 | return 0; |
ff0dacd7 | 4732 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4733 | } |
df7492f9 KH |
4734 | else |
4735 | break; | |
fa42c37f | 4736 | } |
ff0dacd7 | 4737 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4738 | return 0; |
fa42c37f | 4739 | |
df7492f9 | 4740 | no_more_source: |
065e3595 | 4741 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4742 | { |
ff0dacd7 | 4743 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4744 | return 0; |
4745 | } | |
ff0dacd7 KH |
4746 | detect_info->found |= found; |
4747 | return 1; | |
fa42c37f KH |
4748 | } |
4749 | ||
4ed46869 KH |
4750 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". |
4751 | If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ | |
fa42c37f | 4752 | |
b73bfc1c | 4753 | static void |
df7492f9 | 4754 | decode_coding_sjis (coding) |
4ed46869 | 4755 | struct coding_system *coding; |
4ed46869 | 4756 | { |
8f924df7 KH |
4757 | const unsigned char *src = coding->source + coding->consumed; |
4758 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4759 | const unsigned char *src_base; | |
69a80ea3 KH |
4760 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4761 | int *charbuf_end | |
4762 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4763 | int consumed_chars = 0, consumed_chars_base; |
4764 | int multibytep = coding->src_multibyte; | |
4765 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4766 | struct charset *charset_kanji2; |
24a73b0a | 4767 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4768 | int char_offset = coding->produced_char; |
4769 | int last_offset = char_offset; | |
4770 | int last_id = charset_ascii; | |
0a9564cb EZ |
4771 | int eol_crlf = |
4772 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 4773 | int byte_after_cr = -1; |
a5d301df | 4774 | |
24a73b0a | 4775 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4776 | |
4777 | val = charset_list; | |
4778 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4779 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4780 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4781 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4782 | |
b73bfc1c | 4783 | while (1) |
4ed46869 | 4784 | { |
df7492f9 | 4785 | int c, c1; |
24a73b0a | 4786 | struct charset *charset; |
fa42c37f | 4787 | |
b73bfc1c | 4788 | src_base = src; |
df7492f9 | 4789 | consumed_chars_base = consumed_chars; |
fa42c37f | 4790 | |
df7492f9 | 4791 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
4792 | { |
4793 | if (byte_after_cr >= 0) | |
4794 | src_base--; | |
4795 | break; | |
4796 | } | |
df7492f9 | 4797 | |
119852e7 KH |
4798 | if (byte_after_cr >= 0) |
4799 | c = byte_after_cr, byte_after_cr = -1; | |
4800 | else | |
4801 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4802 | if (c < 0) |
4803 | goto invalid_code; | |
24a73b0a | 4804 | if (c < 0x80) |
119852e7 KH |
4805 | { |
4806 | if (eol_crlf && c == '\r') | |
4807 | ONE_MORE_BYTE (byte_after_cr); | |
4808 | charset = charset_roman; | |
4809 | } | |
57a47f8a | 4810 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4811 | goto invalid_code; |
57a47f8a KH |
4812 | else if (c >= 0xA1 && c <= 0xDF) |
4813 | { | |
4814 | /* SJIS -> JISX0201-Kana */ | |
4815 | c &= 0x7F; | |
4816 | charset = charset_kana; | |
4817 | } | |
4818 | else if (c <= 0xEF) | |
df7492f9 | 4819 | { |
57a47f8a KH |
4820 | /* SJIS -> JISX0208 */ |
4821 | ONE_MORE_BYTE (c1); | |
4822 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4823 | goto invalid_code; |
57a47f8a KH |
4824 | c = (c << 8) | c1; |
4825 | SJIS_TO_JIS (c); | |
4826 | charset = charset_kanji; | |
4827 | } | |
4828 | else if (c <= 0xFC && charset_kanji2) | |
4829 | { | |
c6876370 | 4830 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4831 | ONE_MORE_BYTE (c1); |
4832 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4833 | goto invalid_code; |
57a47f8a KH |
4834 | c = (c << 8) | c1; |
4835 | SJIS_TO_JIS2 (c); | |
4836 | charset = charset_kanji2; | |
df7492f9 | 4837 | } |
57a47f8a KH |
4838 | else |
4839 | goto invalid_code; | |
24a73b0a KH |
4840 | if (charset->id != charset_ascii |
4841 | && last_id != charset->id) | |
4842 | { | |
4843 | if (last_id != charset_ascii) | |
69a80ea3 | 4844 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4845 | last_id = charset->id; |
4846 | last_offset = char_offset; | |
4847 | } | |
4848 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4849 | *charbuf++ = c; |
ff0dacd7 | 4850 | char_offset++; |
df7492f9 | 4851 | continue; |
b73bfc1c | 4852 | |
df7492f9 KH |
4853 | invalid_code: |
4854 | src = src_base; | |
4855 | consumed_chars = consumed_chars_base; | |
4856 | ONE_MORE_BYTE (c); | |
065e3595 | 4857 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4858 | char_offset++; |
df7492f9 KH |
4859 | coding->errors++; |
4860 | } | |
fa42c37f | 4861 | |
df7492f9 | 4862 | no_more_source: |
ff0dacd7 | 4863 | if (last_id != charset_ascii) |
69a80ea3 | 4864 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4865 | coding->consumed_char += consumed_chars_base; |
4866 | coding->consumed = src_base - coding->source; | |
4867 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4868 | } |
4869 | ||
b73bfc1c | 4870 | static void |
df7492f9 | 4871 | decode_coding_big5 (coding) |
4ed46869 | 4872 | struct coding_system *coding; |
4ed46869 | 4873 | { |
8f924df7 KH |
4874 | const unsigned char *src = coding->source + coding->consumed; |
4875 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4876 | const unsigned char *src_base; | |
69a80ea3 KH |
4877 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4878 | int *charbuf_end | |
4879 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4880 | int consumed_chars = 0, consumed_chars_base; |
4881 | int multibytep = coding->src_multibyte; | |
4882 | struct charset *charset_roman, *charset_big5; | |
24a73b0a | 4883 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4884 | int char_offset = coding->produced_char; |
4885 | int last_offset = char_offset; | |
4886 | int last_id = charset_ascii; | |
0a9564cb EZ |
4887 | int eol_crlf = |
4888 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 4889 | int byte_after_cr = -1; |
df7492f9 | 4890 | |
24a73b0a | 4891 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4892 | val = charset_list; |
4893 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4894 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4895 | |
b73bfc1c | 4896 | while (1) |
4ed46869 | 4897 | { |
df7492f9 | 4898 | int c, c1; |
24a73b0a | 4899 | struct charset *charset; |
b73bfc1c KH |
4900 | |
4901 | src_base = src; | |
df7492f9 KH |
4902 | consumed_chars_base = consumed_chars; |
4903 | ||
4904 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
4905 | { |
4906 | if (byte_after_cr >= 0) | |
4907 | src_base--; | |
4908 | break; | |
4909 | } | |
df7492f9 | 4910 | |
119852e7 | 4911 | if (byte_after_cr >= 0) |
14daee73 | 4912 | c = byte_after_cr, byte_after_cr = -1; |
119852e7 KH |
4913 | else |
4914 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4915 | |
065e3595 KH |
4916 | if (c < 0) |
4917 | goto invalid_code; | |
24a73b0a | 4918 | if (c < 0x80) |
119852e7 | 4919 | { |
14daee73 | 4920 | if (eol_crlf && c == '\r') |
119852e7 KH |
4921 | ONE_MORE_BYTE (byte_after_cr); |
4922 | charset = charset_roman; | |
4923 | } | |
24a73b0a | 4924 | else |
4ed46869 | 4925 | { |
24a73b0a KH |
4926 | /* BIG5 -> Big5 */ |
4927 | if (c < 0xA1 || c > 0xFE) | |
4928 | goto invalid_code; | |
4929 | ONE_MORE_BYTE (c1); | |
4930 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4931 | goto invalid_code; | |
4932 | c = c << 8 | c1; | |
4933 | charset = charset_big5; | |
4ed46869 | 4934 | } |
24a73b0a KH |
4935 | if (charset->id != charset_ascii |
4936 | && last_id != charset->id) | |
df7492f9 | 4937 | { |
24a73b0a | 4938 | if (last_id != charset_ascii) |
69a80ea3 | 4939 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4940 | last_id = charset->id; |
4941 | last_offset = char_offset; | |
4ed46869 | 4942 | } |
24a73b0a | 4943 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4944 | *charbuf++ = c; |
ff0dacd7 | 4945 | char_offset++; |
fb88bf2d KH |
4946 | continue; |
4947 | ||
df7492f9 | 4948 | invalid_code: |
4ed46869 | 4949 | src = src_base; |
df7492f9 KH |
4950 | consumed_chars = consumed_chars_base; |
4951 | ONE_MORE_BYTE (c); | |
065e3595 | 4952 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4953 | char_offset++; |
df7492f9 | 4954 | coding->errors++; |
fb88bf2d | 4955 | } |
d46c5b12 | 4956 | |
df7492f9 | 4957 | no_more_source: |
ff0dacd7 | 4958 | if (last_id != charset_ascii) |
69a80ea3 | 4959 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4960 | coding->consumed_char += consumed_chars_base; |
4961 | coding->consumed = src_base - coding->source; | |
4962 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4963 | } |
4964 | ||
4965 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4966 | This function can encode charsets `ascii', `katakana-jisx0201', |
4967 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4968 | are sure that all these charsets are registered as official charset | |
4ed46869 KH |
4969 | (i.e. do not have extended leading-codes). Characters of other |
4970 | charsets are produced without any encoding. If SJIS_P is 1, encode | |
4971 | SJIS text, else encode BIG5 text. */ | |
4972 | ||
df7492f9 KH |
4973 | static int |
4974 | encode_coding_sjis (coding) | |
4ed46869 | 4975 | struct coding_system *coding; |
4ed46869 | 4976 | { |
df7492f9 KH |
4977 | int multibytep = coding->dst_multibyte; |
4978 | int *charbuf = coding->charbuf; | |
4979 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4980 | unsigned char *dst = coding->destination + coding->produced; | |
4981 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4982 | int safe_room = 4; | |
4983 | int produced_chars = 0; | |
24a73b0a | 4984 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4985 | int ascii_compatible; |
4986 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4987 | struct charset *charset_kanji2; |
df7492f9 | 4988 | int c; |
a5d301df | 4989 | |
24a73b0a | 4990 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4991 | val = charset_list; |
4992 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4993 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
57a47f8a KH |
4994 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4995 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4996 | |
df7492f9 | 4997 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4998 | |
df7492f9 KH |
4999 | while (charbuf < charbuf_end) |
5000 | { | |
5001 | ASSURE_DESTINATION (safe_room); | |
5002 | c = *charbuf++; | |
b73bfc1c | 5003 | /* Now encode the character C. */ |
df7492f9 KH |
5004 | if (ASCII_CHAR_P (c) && ascii_compatible) |
5005 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
5006 | else if (CHAR_BYTE8_P (c)) |
5007 | { | |
5008 | c = CHAR_TO_BYTE8 (c); | |
5009 | EMIT_ONE_BYTE (c); | |
5010 | } | |
df7492f9 | 5011 | else |
b73bfc1c | 5012 | { |
df7492f9 KH |
5013 | unsigned code; |
5014 | struct charset *charset = char_charset (c, charset_list, &code); | |
5015 | ||
5016 | if (!charset) | |
4ed46869 | 5017 | { |
41cbe562 | 5018 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 5019 | { |
41cbe562 KH |
5020 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
5021 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 5022 | } |
41cbe562 | 5023 | else |
b73bfc1c | 5024 | { |
41cbe562 KH |
5025 | c = coding->default_char; |
5026 | charset = char_charset (c, charset_list, &code); | |
b73bfc1c | 5027 | } |
b73bfc1c | 5028 | } |
df7492f9 KH |
5029 | if (code == CHARSET_INVALID_CODE (charset)) |
5030 | abort (); | |
5031 | if (charset == charset_kanji) | |
5032 | { | |
5033 | int c1, c2; | |
5034 | JIS_TO_SJIS (code); | |
5035 | c1 = code >> 8, c2 = code & 0xFF; | |
5036 | EMIT_TWO_BYTES (c1, c2); | |
5037 | } | |
5038 | else if (charset == charset_kana) | |
5039 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
5040 | else if (charset_kanji2 && charset == charset_kanji2) |
5041 | { | |
5042 | int c1, c2; | |
5043 | ||
5044 | c1 = code >> 8; | |
5045 | if (c1 == 0x21 || (c1 >= 0x23 && c1 < 0x25) | |
5046 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) | |
5047 | { | |
5048 | JIS_TO_SJIS2 (code); | |
5049 | c1 = code >> 8, c2 = code & 0xFF; | |
5050 | EMIT_TWO_BYTES (c1, c2); | |
5051 | } | |
5052 | else | |
5053 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
5054 | } | |
df7492f9 KH |
5055 | else |
5056 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
5057 | } | |
5058 | } | |
065e3595 | 5059 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5060 | coding->produced_char += produced_chars; |
5061 | coding->produced = dst - coding->destination; | |
5062 | return 0; | |
5063 | } | |
5064 | ||
5065 | static int | |
5066 | encode_coding_big5 (coding) | |
5067 | struct coding_system *coding; | |
5068 | { | |
5069 | int multibytep = coding->dst_multibyte; | |
5070 | int *charbuf = coding->charbuf; | |
5071 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5072 | unsigned char *dst = coding->destination + coding->produced; | |
5073 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5074 | int safe_room = 4; | |
5075 | int produced_chars = 0; | |
24a73b0a | 5076 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
5077 | int ascii_compatible; |
5078 | struct charset *charset_roman, *charset_big5; | |
5079 | int c; | |
5080 | ||
24a73b0a | 5081 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
5082 | val = charset_list; |
5083 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
5084 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
5085 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
5086 | ||
5087 | while (charbuf < charbuf_end) | |
5088 | { | |
5089 | ASSURE_DESTINATION (safe_room); | |
5090 | c = *charbuf++; | |
5091 | /* Now encode the character C. */ | |
5092 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
5093 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
5094 | else if (CHAR_BYTE8_P (c)) |
5095 | { | |
5096 | c = CHAR_TO_BYTE8 (c); | |
5097 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
5098 | } |
5099 | else | |
5100 | { | |
df7492f9 KH |
5101 | unsigned code; |
5102 | struct charset *charset = char_charset (c, charset_list, &code); | |
5103 | ||
5104 | if (! charset) | |
b73bfc1c | 5105 | { |
41cbe562 | 5106 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 5107 | { |
41cbe562 KH |
5108 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
5109 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 5110 | } |
41cbe562 | 5111 | else |
0eecad43 | 5112 | { |
41cbe562 KH |
5113 | c = coding->default_char; |
5114 | charset = char_charset (c, charset_list, &code); | |
0eecad43 | 5115 | } |
4ed46869 | 5116 | } |
df7492f9 KH |
5117 | if (code == CHARSET_INVALID_CODE (charset)) |
5118 | abort (); | |
5119 | if (charset == charset_big5) | |
b73bfc1c | 5120 | { |
df7492f9 KH |
5121 | int c1, c2; |
5122 | ||
5123 | c1 = code >> 8, c2 = code & 0xFF; | |
5124 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 5125 | } |
df7492f9 KH |
5126 | else |
5127 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 5128 | } |
4ed46869 | 5129 | } |
065e3595 | 5130 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5131 | coding->produced_char += produced_chars; |
5132 | coding->produced = dst - coding->destination; | |
5133 | return 0; | |
4ed46869 KH |
5134 | } |
5135 | ||
5136 | \f | |
df7492f9 | 5137 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
5138 | |
5139 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
5140 | Check if a text is encoded in a coding system of which | |
5141 | encoder/decoder are written in CCL program. If it is, return | |
df7492f9 | 5142 | CATEGORY_MASK_CCL, else return 0. */ |
1397dc18 | 5143 | |
0a28aafb | 5144 | static int |
ff0dacd7 | 5145 | detect_coding_ccl (coding, detect_info) |
df7492f9 | 5146 | struct coding_system *coding; |
ff0dacd7 | 5147 | struct coding_detection_info *detect_info; |
1397dc18 | 5148 | { |
065e3595 | 5149 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5150 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
5151 | int multibytep = coding->src_multibyte; |
5152 | int consumed_chars = 0; | |
5153 | int found = 0; | |
0e219d54 | 5154 | unsigned char *valids; |
df7492f9 KH |
5155 | int head_ascii = coding->head_ascii; |
5156 | Lisp_Object attrs; | |
5157 | ||
ff0dacd7 KH |
5158 | detect_info->checked |= CATEGORY_MASK_CCL; |
5159 | ||
df7492f9 | 5160 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 5161 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
5162 | attrs = CODING_ID_ATTRS (coding->id); |
5163 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
5164 | src += head_ascii; | |
1397dc18 | 5165 | |
b73bfc1c | 5166 | while (1) |
1397dc18 | 5167 | { |
df7492f9 | 5168 | int c; |
065e3595 KH |
5169 | |
5170 | src_base = src; | |
df7492f9 | 5171 | ONE_MORE_BYTE (c); |
065e3595 | 5172 | if (c < 0 || ! valids[c]) |
df7492f9 | 5173 | break; |
ff0dacd7 KH |
5174 | if ((valids[c] > 1)) |
5175 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 5176 | } |
ff0dacd7 | 5177 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
5178 | return 0; |
5179 | ||
5180 | no_more_source: | |
ff0dacd7 KH |
5181 | detect_info->found |= found; |
5182 | return 1; | |
df7492f9 KH |
5183 | } |
5184 | ||
5185 | static void | |
5186 | decode_coding_ccl (coding) | |
5187 | struct coding_system *coding; | |
5188 | { | |
7c78e542 | 5189 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 5190 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
5191 | int *charbuf = coding->charbuf + coding->charbuf_used; |
5192 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
5193 | int consumed_chars = 0; |
5194 | int multibytep = coding->src_multibyte; | |
5195 | struct ccl_program ccl; | |
5196 | int source_charbuf[1024]; | |
5197 | int source_byteidx[1024]; | |
24a73b0a | 5198 | Lisp_Object attrs, charset_list; |
df7492f9 | 5199 | |
24a73b0a | 5200 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
5201 | setup_ccl_program (&ccl, CODING_CCL_DECODER (coding)); |
5202 | ||
5203 | while (src < src_end) | |
5204 | { | |
7c78e542 | 5205 | const unsigned char *p = src; |
df7492f9 KH |
5206 | int *source, *source_end; |
5207 | int i = 0; | |
5208 | ||
5209 | if (multibytep) | |
5210 | while (i < 1024 && p < src_end) | |
5211 | { | |
5212 | source_byteidx[i] = p - src; | |
5213 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
5214 | } | |
5215 | else | |
5216 | while (i < 1024 && p < src_end) | |
5217 | source_charbuf[i++] = *p++; | |
8f924df7 | 5218 | |
df7492f9 KH |
5219 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
5220 | ccl.last_block = 1; | |
5221 | ||
5222 | source = source_charbuf; | |
5223 | source_end = source + i; | |
5224 | while (source < source_end) | |
5225 | { | |
5226 | ccl_driver (&ccl, source, charbuf, | |
8dcbea82 KH |
5227 | source_end - source, charbuf_end - charbuf, |
5228 | charset_list); | |
df7492f9 KH |
5229 | source += ccl.consumed; |
5230 | charbuf += ccl.produced; | |
5231 | if (ccl.status != CCL_STAT_SUSPEND_BY_DST) | |
5232 | break; | |
5233 | } | |
5234 | if (source < source_end) | |
5235 | src += source_byteidx[source - source_charbuf]; | |
5236 | else | |
5237 | src = p; | |
5238 | consumed_chars += source - source_charbuf; | |
5239 | ||
5240 | if (ccl.status != CCL_STAT_SUSPEND_BY_SRC | |
5241 | && ccl.status != CODING_RESULT_INSUFFICIENT_SRC) | |
5242 | break; | |
5243 | } | |
5244 | ||
5245 | switch (ccl.status) | |
5246 | { | |
5247 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5248 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5249 | break; |
5250 | case CCL_STAT_SUSPEND_BY_DST: | |
5251 | break; | |
5252 | case CCL_STAT_QUIT: | |
5253 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5254 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5255 | break; |
5256 | default: | |
065e3595 | 5257 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5258 | break; |
5259 | } | |
5260 | coding->consumed_char += consumed_chars; | |
5261 | coding->consumed = src - coding->source; | |
5262 | coding->charbuf_used = charbuf - coding->charbuf; | |
5263 | } | |
5264 | ||
5265 | static int | |
5266 | encode_coding_ccl (coding) | |
5267 | struct coding_system *coding; | |
5268 | { | |
5269 | struct ccl_program ccl; | |
5270 | int multibytep = coding->dst_multibyte; | |
5271 | int *charbuf = coding->charbuf; | |
5272 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5273 | unsigned char *dst = coding->destination + coding->produced; | |
5274 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
df7492f9 KH |
5275 | int destination_charbuf[1024]; |
5276 | int i, produced_chars = 0; | |
24a73b0a | 5277 | Lisp_Object attrs, charset_list; |
df7492f9 | 5278 | |
24a73b0a | 5279 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
5280 | setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding)); |
5281 | ||
5282 | ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK; | |
5283 | ccl.dst_multibyte = coding->dst_multibyte; | |
5284 | ||
8cffd3e7 | 5285 | while (charbuf < charbuf_end) |
df7492f9 | 5286 | { |
df7492f9 | 5287 | ccl_driver (&ccl, charbuf, destination_charbuf, |
8cffd3e7 | 5288 | charbuf_end - charbuf, 1024, charset_list); |
df7492f9 | 5289 | if (multibytep) |
8cffd3e7 KH |
5290 | { |
5291 | ASSURE_DESTINATION (ccl.produced * 2); | |
5292 | for (i = 0; i < ccl.produced; i++) | |
5293 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); | |
5294 | } | |
df7492f9 KH |
5295 | else |
5296 | { | |
8cffd3e7 | 5297 | ASSURE_DESTINATION (ccl.produced); |
3ed051d4 | 5298 | for (i = 0; i < ccl.produced; i++) |
df7492f9 KH |
5299 | *dst++ = destination_charbuf[i] & 0xFF; |
5300 | produced_chars += ccl.produced; | |
5301 | } | |
8cffd3e7 KH |
5302 | charbuf += ccl.consumed; |
5303 | if (ccl.status == CCL_STAT_QUIT | |
5304 | || ccl.status == CCL_STAT_INVALID_CMD) | |
5305 | break; | |
df7492f9 KH |
5306 | } |
5307 | ||
5308 | switch (ccl.status) | |
5309 | { | |
5310 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5311 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5312 | break; |
5313 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 5314 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5315 | break; |
5316 | case CCL_STAT_QUIT: | |
5317 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5318 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5319 | break; |
5320 | default: | |
065e3595 | 5321 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 5322 | break; |
1397dc18 | 5323 | } |
df7492f9 KH |
5324 | |
5325 | coding->produced_char += produced_chars; | |
5326 | coding->produced = dst - coding->destination; | |
5327 | return 0; | |
1397dc18 KH |
5328 | } |
5329 | ||
df7492f9 | 5330 | |
1397dc18 | 5331 | \f |
df7492f9 | 5332 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 5333 | |
b73bfc1c | 5334 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 5335 | |
b73bfc1c | 5336 | static void |
df7492f9 | 5337 | decode_coding_raw_text (coding) |
4ed46869 | 5338 | struct coding_system *coding; |
4ed46869 | 5339 | { |
0a9564cb EZ |
5340 | int eol_crlf = |
5341 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 5342 | |
df7492f9 | 5343 | coding->chars_at_source = 1; |
119852e7 KH |
5344 | coding->consumed_char = coding->src_chars; |
5345 | coding->consumed = coding->src_bytes; | |
5346 | if (eol_crlf && coding->source[coding->src_bytes - 1] == '\r') | |
5347 | { | |
5348 | coding->consumed_char--; | |
5349 | coding->consumed--; | |
5350 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
5351 | } | |
5352 | else | |
5353 | record_conversion_result (coding, CODING_RESULT_SUCCESS); | |
df7492f9 | 5354 | } |
4ed46869 | 5355 | |
df7492f9 KH |
5356 | static int |
5357 | encode_coding_raw_text (coding) | |
5358 | struct coding_system *coding; | |
5359 | { | |
5360 | int multibytep = coding->dst_multibyte; | |
5361 | int *charbuf = coding->charbuf; | |
5362 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
5363 | unsigned char *dst = coding->destination + coding->produced; | |
5364 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
a0ed9b27 | 5365 | int produced_chars = 0; |
b73bfc1c KH |
5366 | int c; |
5367 | ||
df7492f9 | 5368 | if (multibytep) |
b73bfc1c | 5369 | { |
df7492f9 | 5370 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 5371 | |
df7492f9 KH |
5372 | if (coding->src_multibyte) |
5373 | while (charbuf < charbuf_end) | |
5374 | { | |
5375 | ASSURE_DESTINATION (safe_room); | |
5376 | c = *charbuf++; | |
5377 | if (ASCII_CHAR_P (c)) | |
5378 | EMIT_ONE_ASCII_BYTE (c); | |
5379 | else if (CHAR_BYTE8_P (c)) | |
5380 | { | |
5381 | c = CHAR_TO_BYTE8 (c); | |
5382 | EMIT_ONE_BYTE (c); | |
5383 | } | |
5384 | else | |
5385 | { | |
5386 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 5387 | |
df7492f9 KH |
5388 | CHAR_STRING_ADVANCE (c, p1); |
5389 | while (p0 < p1) | |
9d123124 KH |
5390 | { |
5391 | EMIT_ONE_BYTE (*p0); | |
5392 | p0++; | |
5393 | } | |
df7492f9 KH |
5394 | } |
5395 | } | |
b73bfc1c | 5396 | else |
df7492f9 KH |
5397 | while (charbuf < charbuf_end) |
5398 | { | |
5399 | ASSURE_DESTINATION (safe_room); | |
5400 | c = *charbuf++; | |
5401 | EMIT_ONE_BYTE (c); | |
5402 | } | |
5403 | } | |
5404 | else | |
4ed46869 | 5405 | { |
df7492f9 | 5406 | if (coding->src_multibyte) |
d46c5b12 | 5407 | { |
df7492f9 KH |
5408 | int safe_room = MAX_MULTIBYTE_LENGTH; |
5409 | ||
5410 | while (charbuf < charbuf_end) | |
d46c5b12 | 5411 | { |
df7492f9 KH |
5412 | ASSURE_DESTINATION (safe_room); |
5413 | c = *charbuf++; | |
5414 | if (ASCII_CHAR_P (c)) | |
5415 | *dst++ = c; | |
5416 | else if (CHAR_BYTE8_P (c)) | |
5417 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 5418 | else |
df7492f9 | 5419 | CHAR_STRING_ADVANCE (c, dst); |
d46c5b12 KH |
5420 | } |
5421 | } | |
df7492f9 KH |
5422 | else |
5423 | { | |
5424 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
5425 | while (charbuf < charbuf_end && dst < dst_end) | |
5426 | *dst++ = *charbuf++; | |
8f924df7 | 5427 | } |
319a3947 | 5428 | produced_chars = dst - (coding->destination + coding->produced); |
4ed46869 | 5429 | } |
065e3595 | 5430 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
a0ed9b27 | 5431 | coding->produced_char += produced_chars; |
df7492f9 KH |
5432 | coding->produced = dst - coding->destination; |
5433 | return 0; | |
4ed46869 KH |
5434 | } |
5435 | ||
ff0dacd7 KH |
5436 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
5437 | Check if a text is encoded in a charset-based coding system. If it | |
5438 | is, return 1, else return 0. */ | |
5439 | ||
0a28aafb | 5440 | static int |
ff0dacd7 | 5441 | detect_coding_charset (coding, detect_info) |
df7492f9 | 5442 | struct coding_system *coding; |
ff0dacd7 | 5443 | struct coding_detection_info *detect_info; |
1397dc18 | 5444 | { |
065e3595 | 5445 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5446 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
5447 | int multibytep = coding->src_multibyte; |
5448 | int consumed_chars = 0; | |
07295713 | 5449 | Lisp_Object attrs, valids, name; |
584948ac | 5450 | int found = 0; |
716b3fa0 | 5451 | int head_ascii = coding->head_ascii; |
07295713 | 5452 | int check_latin_extra = 0; |
1397dc18 | 5453 | |
ff0dacd7 KH |
5454 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
5455 | ||
df7492f9 KH |
5456 | coding = &coding_categories[coding_category_charset]; |
5457 | attrs = CODING_ID_ATTRS (coding->id); | |
5458 | valids = AREF (attrs, coding_attr_charset_valids); | |
07295713 | 5459 | name = CODING_ID_NAME (coding->id); |
237aabf4 JR |
5460 | if (strncmp ((char *) SDATA (SYMBOL_NAME (name)), |
5461 | "iso-8859-", sizeof ("iso-8859-") - 1) == 0 | |
5462 | || strncmp ((char *) SDATA (SYMBOL_NAME (name)), | |
5463 | "iso-latin-", sizeof ("iso-latin-") - 1) == 0) | |
07295713 | 5464 | check_latin_extra = 1; |
237aabf4 | 5465 | |
df7492f9 | 5466 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
716b3fa0 | 5467 | src += head_ascii; |
1397dc18 | 5468 | |
b73bfc1c | 5469 | while (1) |
1397dc18 | 5470 | { |
df7492f9 | 5471 | int c; |
716b3fa0 KH |
5472 | Lisp_Object val; |
5473 | struct charset *charset; | |
5474 | int dim, idx; | |
1397dc18 | 5475 | |
065e3595 | 5476 | src_base = src; |
df7492f9 | 5477 | ONE_MORE_BYTE (c); |
065e3595 KH |
5478 | if (c < 0) |
5479 | continue; | |
716b3fa0 KH |
5480 | val = AREF (valids, c); |
5481 | if (NILP (val)) | |
df7492f9 | 5482 | break; |
584948ac | 5483 | if (c >= 0x80) |
07295713 KH |
5484 | { |
5485 | if (c < 0xA0 | |
237aabf4 JR |
5486 | && check_latin_extra |
5487 | && (!VECTORP (Vlatin_extra_code_table) | |
9f0526cb | 5488 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))) |
07295713 KH |
5489 | break; |
5490 | found = CATEGORY_MASK_CHARSET; | |
5491 | } | |
716b3fa0 KH |
5492 | if (INTEGERP (val)) |
5493 | { | |
5494 | charset = CHARSET_FROM_ID (XFASTINT (val)); | |
5495 | dim = CHARSET_DIMENSION (charset); | |
5496 | for (idx = 1; idx < dim; idx++) | |
5497 | { | |
5498 | if (src == src_end) | |
5499 | goto too_short; | |
5500 | ONE_MORE_BYTE (c); | |
3ed051d4 | 5501 | if (c < charset->code_space[(dim - 1 - idx) * 2] |
716b3fa0 KH |
5502 | || c > charset->code_space[(dim - 1 - idx) * 2 + 1]) |
5503 | break; | |
5504 | } | |
5505 | if (idx < dim) | |
5506 | break; | |
5507 | } | |
5508 | else | |
5509 | { | |
5510 | idx = 1; | |
5511 | for (; CONSP (val); val = XCDR (val)) | |
5512 | { | |
5513 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); | |
5514 | dim = CHARSET_DIMENSION (charset); | |
5515 | while (idx < dim) | |
5516 | { | |
5517 | if (src == src_end) | |
5518 | goto too_short; | |
5519 | ONE_MORE_BYTE (c); | |
5520 | if (c < charset->code_space[(dim - 1 - idx) * 4] | |
5521 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
5522 | break; | |
5523 | idx++; | |
5524 | } | |
5525 | if (idx == dim) | |
5526 | { | |
5527 | val = Qnil; | |
5528 | break; | |
5529 | } | |
5530 | } | |
5531 | if (CONSP (val)) | |
5532 | break; | |
5533 | } | |
df7492f9 | 5534 | } |
716b3fa0 | 5535 | too_short: |
ff0dacd7 | 5536 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 5537 | return 0; |
4ed46869 | 5538 | |
df7492f9 | 5539 | no_more_source: |
ff0dacd7 KH |
5540 | detect_info->found |= found; |
5541 | return 1; | |
df7492f9 | 5542 | } |
b73bfc1c | 5543 | |
b73bfc1c | 5544 | static void |
df7492f9 | 5545 | decode_coding_charset (coding) |
4ed46869 | 5546 | struct coding_system *coding; |
4ed46869 | 5547 | { |
8f924df7 KH |
5548 | const unsigned char *src = coding->source + coding->consumed; |
5549 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
5550 | const unsigned char *src_base; | |
69a80ea3 KH |
5551 | int *charbuf = coding->charbuf + coding->charbuf_used; |
5552 | int *charbuf_end | |
5553 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
5554 | int consumed_chars = 0, consumed_chars_base; |
5555 | int multibytep = coding->src_multibyte; | |
24a73b0a | 5556 | Lisp_Object attrs, charset_list, valids; |
ff0dacd7 KH |
5557 | int char_offset = coding->produced_char; |
5558 | int last_offset = char_offset; | |
5559 | int last_id = charset_ascii; | |
0a9564cb EZ |
5560 | int eol_crlf = |
5561 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 5562 | int byte_after_cr = -1; |
df7492f9 | 5563 | |
24a73b0a | 5564 | CODING_GET_INFO (coding, attrs, charset_list); |
4eb6d3f1 | 5565 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 5566 | |
df7492f9 | 5567 | while (1) |
4ed46869 | 5568 | { |
4eb6d3f1 | 5569 | int c; |
24a73b0a KH |
5570 | Lisp_Object val; |
5571 | struct charset *charset; | |
5572 | int dim; | |
5573 | int len = 1; | |
5574 | unsigned code; | |
df7492f9 KH |
5575 | |
5576 | src_base = src; | |
5577 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 5578 | |
df7492f9 | 5579 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
5580 | { |
5581 | if (byte_after_cr >= 0) | |
5582 | src_base--; | |
5583 | break; | |
5584 | } | |
df7492f9 | 5585 | |
119852e7 KH |
5586 | if (byte_after_cr >= 0) |
5587 | { | |
5588 | c = byte_after_cr; | |
5589 | byte_after_cr = -1; | |
5590 | } | |
5591 | else | |
5592 | { | |
5593 | ONE_MORE_BYTE (c); | |
5594 | if (eol_crlf && c == '\r') | |
5595 | ONE_MORE_BYTE (byte_after_cr); | |
5596 | } | |
065e3595 KH |
5597 | if (c < 0) |
5598 | goto invalid_code; | |
24a73b0a KH |
5599 | code = c; |
5600 | ||
5601 | val = AREF (valids, c); | |
1b17adfd | 5602 | if (! INTEGERP (val) && ! CONSP (val)) |
24a73b0a KH |
5603 | goto invalid_code; |
5604 | if (INTEGERP (val)) | |
d46c5b12 | 5605 | { |
24a73b0a KH |
5606 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
5607 | dim = CHARSET_DIMENSION (charset); | |
5608 | while (len < dim) | |
b73bfc1c | 5609 | { |
24a73b0a KH |
5610 | ONE_MORE_BYTE (c); |
5611 | code = (code << 8) | c; | |
5612 | len++; | |
b73bfc1c | 5613 | } |
24a73b0a KH |
5614 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
5615 | charset, code, c); | |
d46c5b12 | 5616 | } |
df7492f9 | 5617 | else |
d46c5b12 | 5618 | { |
24a73b0a KH |
5619 | /* VAL is a list of charset IDs. It is assured that the |
5620 | list is sorted by charset dimensions (smaller one | |
5621 | comes first). */ | |
5622 | while (CONSP (val)) | |
4eb6d3f1 | 5623 | { |
24a73b0a | 5624 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 5625 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 5626 | while (len < dim) |
4eb6d3f1 | 5627 | { |
acb2a965 KH |
5628 | ONE_MORE_BYTE (c); |
5629 | code = (code << 8) | c; | |
f9d71dcd | 5630 | len++; |
4eb6d3f1 | 5631 | } |
24a73b0a KH |
5632 | CODING_DECODE_CHAR (coding, src, src_base, |
5633 | src_end, charset, code, c); | |
5634 | if (c >= 0) | |
5635 | break; | |
5636 | val = XCDR (val); | |
ff0dacd7 | 5637 | } |
d46c5b12 | 5638 | } |
24a73b0a KH |
5639 | if (c < 0) |
5640 | goto invalid_code; | |
5641 | if (charset->id != charset_ascii | |
5642 | && last_id != charset->id) | |
5643 | { | |
5644 | if (last_id != charset_ascii) | |
69a80ea3 | 5645 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
5646 | last_id = charset->id; |
5647 | last_offset = char_offset; | |
5648 | } | |
5649 | ||
df7492f9 | 5650 | *charbuf++ = c; |
ff0dacd7 | 5651 | char_offset++; |
df7492f9 KH |
5652 | continue; |
5653 | ||
5654 | invalid_code: | |
5655 | src = src_base; | |
5656 | consumed_chars = consumed_chars_base; | |
5657 | ONE_MORE_BYTE (c); | |
065e3595 | 5658 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 5659 | char_offset++; |
df7492f9 | 5660 | coding->errors++; |
4ed46869 KH |
5661 | } |
5662 | ||
df7492f9 | 5663 | no_more_source: |
ff0dacd7 | 5664 | if (last_id != charset_ascii) |
69a80ea3 | 5665 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
5666 | coding->consumed_char += consumed_chars_base; |
5667 | coding->consumed = src_base - coding->source; | |
5668 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
5669 | } |
5670 | ||
df7492f9 KH |
5671 | static int |
5672 | encode_coding_charset (coding) | |
4ed46869 | 5673 | struct coding_system *coding; |
4ed46869 | 5674 | { |
df7492f9 KH |
5675 | int multibytep = coding->dst_multibyte; |
5676 | int *charbuf = coding->charbuf; | |
5677 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5678 | unsigned char *dst = coding->destination + coding->produced; | |
5679 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5680 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
5681 | int produced_chars = 0; | |
24a73b0a | 5682 | Lisp_Object attrs, charset_list; |
df7492f9 | 5683 | int ascii_compatible; |
b73bfc1c | 5684 | int c; |
b73bfc1c | 5685 | |
24a73b0a | 5686 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5687 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 5688 | |
df7492f9 | 5689 | while (charbuf < charbuf_end) |
4ed46869 | 5690 | { |
4eb6d3f1 | 5691 | struct charset *charset; |
df7492f9 | 5692 | unsigned code; |
8f924df7 | 5693 | |
df7492f9 KH |
5694 | ASSURE_DESTINATION (safe_room); |
5695 | c = *charbuf++; | |
5696 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
5697 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 5698 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 5699 | { |
16eafb5d KH |
5700 | c = CHAR_TO_BYTE8 (c); |
5701 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 5702 | } |
d46c5b12 | 5703 | else |
b73bfc1c | 5704 | { |
4eb6d3f1 KH |
5705 | charset = char_charset (c, charset_list, &code); |
5706 | if (charset) | |
5707 | { | |
5708 | if (CHARSET_DIMENSION (charset) == 1) | |
5709 | EMIT_ONE_BYTE (code); | |
5710 | else if (CHARSET_DIMENSION (charset) == 2) | |
5711 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
5712 | else if (CHARSET_DIMENSION (charset) == 3) | |
5713 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
5714 | else | |
5715 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
5716 | (code >> 8) & 0xFF, code & 0xFF); | |
5717 | } | |
5718 | else | |
41cbe562 KH |
5719 | { |
5720 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
5721 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
5722 | else | |
5723 | c = coding->default_char; | |
5724 | EMIT_ONE_BYTE (c); | |
5725 | } | |
4ed46869 | 5726 | } |
4ed46869 KH |
5727 | } |
5728 | ||
065e3595 | 5729 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5730 | coding->produced_char += produced_chars; |
5731 | coding->produced = dst - coding->destination; | |
5732 | return 0; | |
4ed46869 KH |
5733 | } |
5734 | ||
5735 | \f | |
1397dc18 | 5736 | /*** 7. C library functions ***/ |
4ed46869 | 5737 | |
df7492f9 KH |
5738 | /* Setup coding context CODING from information about CODING_SYSTEM. |
5739 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
5740 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 5741 | |
ec6d2bb8 | 5742 | void |
e0e989f6 KH |
5743 | setup_coding_system (coding_system, coding) |
5744 | Lisp_Object coding_system; | |
4ed46869 KH |
5745 | struct coding_system *coding; |
5746 | { | |
df7492f9 KH |
5747 | Lisp_Object attrs; |
5748 | Lisp_Object eol_type; | |
5749 | Lisp_Object coding_type; | |
4608c386 | 5750 | Lisp_Object val; |
4ed46869 | 5751 | |
df7492f9 | 5752 | if (NILP (coding_system)) |
ae6f73fa | 5753 | coding_system = Qundecided; |
c07c8e12 | 5754 | |
df7492f9 | 5755 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 5756 | |
df7492f9 | 5757 | attrs = CODING_ID_ATTRS (coding->id); |
0a9564cb | 5758 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
1f5dbf34 | 5759 | |
df7492f9 KH |
5760 | coding->mode = 0; |
5761 | coding->head_ascii = -1; | |
4a015c45 KH |
5762 | if (VECTORP (eol_type)) |
5763 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5764 | | CODING_REQUIRE_DETECTION_MASK); | |
5765 | else if (! EQ (eol_type, Qunix)) | |
5766 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5767 | | CODING_REQUIRE_ENCODING_MASK); | |
5768 | else | |
5769 | coding->common_flags = 0; | |
5e5c78be KH |
5770 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
5771 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5772 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
5773 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
5774 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
5775 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 5776 | |
df7492f9 | 5777 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 | 5778 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5779 | coding->safe_charsets = SDATA (val); |
df7492f9 | 5780 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
4608c386 | 5781 | |
df7492f9 KH |
5782 | coding_type = CODING_ATTR_TYPE (attrs); |
5783 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 5784 | { |
df7492f9 KH |
5785 | coding->detector = NULL; |
5786 | coding->decoder = decode_coding_raw_text; | |
5787 | coding->encoder = encode_coding_raw_text; | |
5788 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5789 | } |
df7492f9 | 5790 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 5791 | { |
df7492f9 KH |
5792 | int i; |
5793 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5794 | ||
5795 | /* Invoke graphic register 0 to plane 0. */ | |
5796 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
5797 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
5798 | CODING_ISO_INVOCATION (coding, 1) | |
5799 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
5800 | /* Setup the initial status of designation. */ | |
5801 | for (i = 0; i < 4; i++) | |
5802 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
5803 | /* Not single shifting initially. */ | |
5804 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
5805 | /* Beginning of buffer should also be regarded as bol. */ | |
5806 | CODING_ISO_BOL (coding) = 1; | |
5807 | coding->detector = detect_coding_iso_2022; | |
5808 | coding->decoder = decode_coding_iso_2022; | |
5809 | coding->encoder = encode_coding_iso_2022; | |
5810 | if (flags & CODING_ISO_FLAG_SAFE) | |
5811 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 5812 | coding->common_flags |
df7492f9 KH |
5813 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5814 | | CODING_REQUIRE_FLUSHING_MASK); | |
5815 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
5816 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
5817 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
5818 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
5819 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
5820 | { | |
5821 | setup_iso_safe_charsets (attrs); | |
5822 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 5823 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5824 | coding->safe_charsets = SDATA (val); |
df7492f9 KH |
5825 | } |
5826 | CODING_ISO_FLAGS (coding) = flags; | |
e951386e KH |
5827 | CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO; |
5828 | CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO; | |
5829 | CODING_ISO_EXTSEGMENT_LEN (coding) = 0; | |
5830 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
d46c5b12 | 5831 | } |
df7492f9 | 5832 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 5833 | { |
df7492f9 KH |
5834 | coding->detector = detect_coding_charset; |
5835 | coding->decoder = decode_coding_charset; | |
5836 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 5837 | coding->common_flags |
df7492f9 | 5838 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 5839 | } |
df7492f9 | 5840 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5841 | { |
a470d443 KH |
5842 | val = AREF (attrs, coding_attr_utf_bom); |
5843 | CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5844 | : EQ (val, Qt) ? utf_with_bom | |
5845 | : utf_without_bom); | |
df7492f9 KH |
5846 | coding->detector = detect_coding_utf_8; |
5847 | coding->decoder = decode_coding_utf_8; | |
5848 | coding->encoder = encode_coding_utf_8; | |
5849 | coding->common_flags | |
5850 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 KH |
5851 | if (CODING_UTF_8_BOM (coding) == utf_detect_bom) |
5852 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
df7492f9 KH |
5853 | } |
5854 | else if (EQ (coding_type, Qutf_16)) | |
5855 | { | |
a470d443 KH |
5856 | val = AREF (attrs, coding_attr_utf_bom); |
5857 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5858 | : EQ (val, Qt) ? utf_with_bom | |
5859 | : utf_without_bom); | |
df7492f9 | 5860 | val = AREF (attrs, coding_attr_utf_16_endian); |
b49a1807 | 5861 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5862 | : utf_16_little_endian); |
e19c3639 | 5863 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5864 | coding->detector = detect_coding_utf_16; |
5865 | coding->decoder = decode_coding_utf_16; | |
5866 | coding->encoder = encode_coding_utf_16; | |
5867 | coding->common_flags | |
5868 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 | 5869 | if (CODING_UTF_16_BOM (coding) == utf_detect_bom) |
b49a1807 | 5870 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; |
d46c5b12 | 5871 | } |
df7492f9 | 5872 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5873 | { |
df7492f9 KH |
5874 | coding->detector = detect_coding_ccl; |
5875 | coding->decoder = decode_coding_ccl; | |
5876 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5877 | coding->common_flags |
df7492f9 KH |
5878 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5879 | | CODING_REQUIRE_FLUSHING_MASK); | |
5880 | } | |
5881 | else if (EQ (coding_type, Qemacs_mule)) | |
5882 | { | |
5883 | coding->detector = detect_coding_emacs_mule; | |
5884 | coding->decoder = decode_coding_emacs_mule; | |
5885 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5886 | coding->common_flags |
df7492f9 | 5887 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
e951386e | 5888 | coding->spec.emacs_mule.full_support = 1; |
df7492f9 KH |
5889 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) |
5890 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5891 | { | |
5892 | Lisp_Object tail, safe_charsets; | |
5893 | int max_charset_id = 0; | |
5894 | ||
5895 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5896 | tail = XCDR (tail)) | |
5897 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5898 | max_charset_id = XFASTINT (XCAR (tail)); | |
1b3b981b AS |
5899 | safe_charsets = make_uninit_string (max_charset_id + 1); |
5900 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
5901 | for (tail = Vemacs_mule_charset_list; CONSP (tail); |
5902 | tail = XCDR (tail)) | |
8f924df7 | 5903 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5904 | coding->max_charset_id = max_charset_id; |
1b3b981b | 5905 | coding->safe_charsets = SDATA (safe_charsets); |
e951386e | 5906 | coding->spec.emacs_mule.full_support = 1; |
df7492f9 | 5907 | } |
e951386e KH |
5908 | coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO; |
5909 | coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO; | |
df7492f9 KH |
5910 | } |
5911 | else if (EQ (coding_type, Qshift_jis)) | |
5912 | { | |
5913 | coding->detector = detect_coding_sjis; | |
5914 | coding->decoder = decode_coding_sjis; | |
5915 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5916 | coding->common_flags |
df7492f9 KH |
5917 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5918 | } | |
5919 | else if (EQ (coding_type, Qbig5)) | |
5920 | { | |
5921 | coding->detector = detect_coding_big5; | |
5922 | coding->decoder = decode_coding_big5; | |
5923 | coding->encoder = encode_coding_big5; | |
c952af22 | 5924 | coding->common_flags |
df7492f9 KH |
5925 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5926 | } | |
5927 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5928 | { |
df7492f9 KH |
5929 | coding->detector = NULL; |
5930 | coding->decoder = decode_coding_raw_text; | |
5931 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5932 | if (! EQ (eol_type, Qunix)) |
5933 | { | |
5934 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5935 | if (! VECTORP (eol_type)) | |
5936 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5937 | } | |
5938 | ||
4ed46869 | 5939 | } |
4ed46869 | 5940 | |
df7492f9 | 5941 | return; |
4ed46869 KH |
5942 | } |
5943 | ||
0ff61e78 KH |
5944 | /* Return a list of charsets supported by CODING. */ |
5945 | ||
5946 | Lisp_Object | |
5947 | coding_charset_list (coding) | |
5948 | struct coding_system *coding; | |
5949 | { | |
35befdaa | 5950 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5951 | |
5952 | CODING_GET_INFO (coding, attrs, charset_list); | |
5953 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5954 | { | |
5955 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5956 | ||
5957 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5958 | charset_list = Viso_2022_charset_list; | |
5959 | } | |
5960 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5961 | { | |
5962 | charset_list = Vemacs_mule_charset_list; | |
5963 | } | |
5964 | return charset_list; | |
5965 | } | |
5966 | ||
5967 | ||
e9f91ece KH |
5968 | /* Return a list of charsets supported by CODING-SYSTEM. */ |
5969 | ||
5970 | Lisp_Object | |
5971 | coding_system_charset_list (coding_system) | |
5972 | Lisp_Object coding_system; | |
5973 | { | |
5974 | int id; | |
5975 | Lisp_Object attrs, charset_list; | |
5976 | ||
5977 | CHECK_CODING_SYSTEM_GET_ID (coding_system, id); | |
5978 | attrs = CODING_ID_ATTRS (id); | |
5979 | ||
5980 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5981 | { | |
5982 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5983 | ||
5984 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5985 | charset_list = Viso_2022_charset_list; | |
5986 | else | |
5987 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5988 | } | |
5989 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5990 | { | |
5991 | charset_list = Vemacs_mule_charset_list; | |
5992 | } | |
5993 | else | |
5994 | { | |
5995 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5996 | } | |
5997 | return charset_list; | |
5998 | } | |
5999 | ||
6000 | ||
df7492f9 KH |
6001 | /* Return raw-text or one of its subsidiaries that has the same |
6002 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 6003 | |
df7492f9 KH |
6004 | Lisp_Object |
6005 | raw_text_coding_system (coding_system) | |
6006 | Lisp_Object coding_system; | |
ec6d2bb8 | 6007 | { |
0be8721c | 6008 | Lisp_Object spec, attrs; |
df7492f9 | 6009 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 6010 | |
d3e4cb56 KH |
6011 | if (NILP (coding_system)) |
6012 | return Qraw_text; | |
df7492f9 KH |
6013 | spec = CODING_SYSTEM_SPEC (coding_system); |
6014 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 6015 | |
df7492f9 KH |
6016 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
6017 | return coding_system; | |
ec6d2bb8 | 6018 | |
df7492f9 KH |
6019 | eol_type = AREF (spec, 2); |
6020 | if (VECTORP (eol_type)) | |
6021 | return Qraw_text; | |
6022 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
6023 | raw_text_eol_type = AREF (spec, 2); | |
6024 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
6025 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
6026 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
6027 | } |
6028 | ||
54f78171 | 6029 | |
df7492f9 KH |
6030 | /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT |
6031 | does, return one of the subsidiary that has the same eol-spec as | |
fcbcfb64 KH |
6032 | PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil, |
6033 | inherit end-of-line format from the system's setting | |
6034 | (system_eol_type). */ | |
df7492f9 KH |
6035 | |
6036 | Lisp_Object | |
6037 | coding_inherit_eol_type (coding_system, parent) | |
b74e4686 | 6038 | Lisp_Object coding_system, parent; |
54f78171 | 6039 | { |
3e139625 | 6040 | Lisp_Object spec, eol_type; |
54f78171 | 6041 | |
d3e4cb56 KH |
6042 | if (NILP (coding_system)) |
6043 | coding_system = Qraw_text; | |
df7492f9 | 6044 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 6045 | eol_type = AREF (spec, 2); |
fcbcfb64 | 6046 | if (VECTORP (eol_type)) |
df7492f9 | 6047 | { |
df7492f9 KH |
6048 | Lisp_Object parent_eol_type; |
6049 | ||
fcbcfb64 KH |
6050 | if (! NILP (parent)) |
6051 | { | |
6052 | Lisp_Object parent_spec; | |
6053 | ||
4a015c45 | 6054 | parent_spec = CODING_SYSTEM_SPEC (parent); |
fcbcfb64 KH |
6055 | parent_eol_type = AREF (parent_spec, 2); |
6056 | } | |
6057 | else | |
6058 | parent_eol_type = system_eol_type; | |
df7492f9 KH |
6059 | if (EQ (parent_eol_type, Qunix)) |
6060 | coding_system = AREF (eol_type, 0); | |
6061 | else if (EQ (parent_eol_type, Qdos)) | |
6062 | coding_system = AREF (eol_type, 1); | |
6063 | else if (EQ (parent_eol_type, Qmac)) | |
6064 | coding_system = AREF (eol_type, 2); | |
54f78171 | 6065 | } |
df7492f9 | 6066 | return coding_system; |
54f78171 KH |
6067 | } |
6068 | ||
4ed46869 KH |
6069 | /* Emacs has a mechanism to automatically detect a coding system if it |
6070 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
6071 | it's impossible to distinguish some coding systems accurately | |
6072 | because they use the same range of codes. So, at first, coding | |
6073 | systems are categorized into 7, those are: | |
6074 | ||
0ef69138 | 6075 | o coding-category-emacs-mule |
4ed46869 KH |
6076 | |
6077 | The category for a coding system which has the same code range | |
6078 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 6079 | symbol) `emacs-mule' by default. |
4ed46869 KH |
6080 | |
6081 | o coding-category-sjis | |
6082 | ||
6083 | The category for a coding system which has the same code range | |
6084 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 6085 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
6086 | |
6087 | o coding-category-iso-7 | |
6088 | ||
6089 | The category for a coding system which has the same code range | |
7717c392 | 6090 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
6091 | shift and single shift functions. This can encode/decode all |
6092 | charsets. Assigned the coding-system (Lisp symbol) | |
6093 | `iso-2022-7bit' by default. | |
6094 | ||
6095 | o coding-category-iso-7-tight | |
6096 | ||
6097 | Same as coding-category-iso-7 except that this can | |
6098 | encode/decode only the specified charsets. | |
4ed46869 KH |
6099 | |
6100 | o coding-category-iso-8-1 | |
6101 | ||
6102 | The category for a coding system which has the same code range | |
6103 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6104 | for DIMENSION1 charset. This doesn't use any locking shift |
6105 | and single shift functions. Assigned the coding-system (Lisp | |
6106 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
6107 | |
6108 | o coding-category-iso-8-2 | |
6109 | ||
6110 | The category for a coding system which has the same code range | |
6111 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6112 | for DIMENSION2 charset. This doesn't use any locking shift |
6113 | and single shift functions. Assigned the coding-system (Lisp | |
6114 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 6115 | |
7717c392 | 6116 | o coding-category-iso-7-else |
4ed46869 KH |
6117 | |
6118 | The category for a coding system which has the same code range | |
df7492f9 | 6119 | as ISO2022 of 7-bit environemnt but uses locking shift or |
7717c392 KH |
6120 | single shift functions. Assigned the coding-system (Lisp |
6121 | symbol) `iso-2022-7bit-lock' by default. | |
6122 | ||
6123 | o coding-category-iso-8-else | |
6124 | ||
6125 | The category for a coding system which has the same code range | |
df7492f9 | 6126 | as ISO2022 of 8-bit environemnt but uses locking shift or |
7717c392 KH |
6127 | single shift functions. Assigned the coding-system (Lisp |
6128 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
6129 | |
6130 | o coding-category-big5 | |
6131 | ||
6132 | The category for a coding system which has the same code range | |
6133 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6134 | `cn-big5' by default. |
4ed46869 | 6135 | |
fa42c37f KH |
6136 | o coding-category-utf-8 |
6137 | ||
6138 | The category for a coding system which has the same code range | |
6e76ae91 | 6139 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
6140 | symbol) `utf-8' by default. |
6141 | ||
6142 | o coding-category-utf-16-be | |
6143 | ||
6144 | The category for a coding system in which a text has an | |
6145 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
6146 | endian at the head. Assigned the coding-system (Lisp symbol) | |
6147 | `utf-16-be' by default. | |
6148 | ||
6149 | o coding-category-utf-16-le | |
6150 | ||
6151 | The category for a coding system in which a text has an | |
6152 | Unicode signature (cf. Unicode Standard) in the order of | |
6153 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
6154 | symbol) `utf-16-le' by default. | |
6155 | ||
1397dc18 KH |
6156 | o coding-category-ccl |
6157 | ||
6158 | The category for a coding system of which encoder/decoder is | |
6159 | written in CCL programs. The default value is nil, i.e., no | |
6160 | coding system is assigned. | |
6161 | ||
4ed46869 KH |
6162 | o coding-category-binary |
6163 | ||
6164 | The category for a coding system not categorized in any of the | |
6165 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6166 | `no-conversion' by default. |
4ed46869 KH |
6167 | |
6168 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 6169 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
6170 | What Emacs does actually is to detect a category of coding system. |
6171 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 6172 | decide only one possible category, it selects a category of the |
4ed46869 KH |
6173 | highest priority. Priorities of categories are also specified by a |
6174 | user in a Lisp variable `coding-category-list'. | |
6175 | ||
6176 | */ | |
6177 | ||
df7492f9 KH |
6178 | #define EOL_SEEN_NONE 0 |
6179 | #define EOL_SEEN_LF 1 | |
6180 | #define EOL_SEEN_CR 2 | |
6181 | #define EOL_SEEN_CRLF 4 | |
66cfb530 | 6182 | |
ff0dacd7 KH |
6183 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
6184 | SOURCE is encoded. If CATEGORY is one of | |
6185 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
6186 | two-byte, else they are encoded by one-byte. | |
6187 | ||
6188 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 6189 | |
bc4bc72a | 6190 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
6191 | |
6192 | static int | |
89528eb3 | 6193 | detect_eol (source, src_bytes, category) |
f6cbaf43 | 6194 | const unsigned char *source; |
df7492f9 | 6195 | EMACS_INT src_bytes; |
89528eb3 | 6196 | enum coding_category category; |
4ed46869 | 6197 | { |
f6cbaf43 | 6198 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 6199 | unsigned char c; |
df7492f9 KH |
6200 | int total = 0; |
6201 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 6202 | |
89528eb3 | 6203 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 6204 | { |
df7492f9 | 6205 | int msb, lsb; |
fa42c37f | 6206 | |
89528eb3 KH |
6207 | msb = category == (coding_category_utf_16_le |
6208 | | coding_category_utf_16_le_nosig); | |
df7492f9 | 6209 | lsb = 1 - msb; |
fa42c37f | 6210 | |
df7492f9 | 6211 | while (src + 1 < src_end) |
fa42c37f | 6212 | { |
df7492f9 KH |
6213 | c = src[lsb]; |
6214 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 6215 | { |
df7492f9 KH |
6216 | int this_eol; |
6217 | ||
6218 | if (c == '\n') | |
6219 | this_eol = EOL_SEEN_LF; | |
6220 | else if (src + 3 >= src_end | |
6221 | || src[msb + 2] != 0 | |
6222 | || src[lsb + 2] != '\n') | |
6223 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 6224 | else |
75f4f1ac EZ |
6225 | { |
6226 | this_eol = EOL_SEEN_CRLF; | |
6227 | src += 2; | |
6228 | } | |
df7492f9 KH |
6229 | |
6230 | if (eol_seen == EOL_SEEN_NONE) | |
6231 | /* This is the first end-of-line. */ | |
6232 | eol_seen = this_eol; | |
6233 | else if (eol_seen != this_eol) | |
fa42c37f | 6234 | { |
75f4f1ac EZ |
6235 | /* The found type is different from what found before. |
6236 | Allow for stray ^M characters in DOS EOL files. */ | |
6237 | if (eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF | |
6238 | || eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR) | |
6239 | eol_seen = EOL_SEEN_CRLF; | |
6240 | else | |
6241 | { | |
6242 | eol_seen = EOL_SEEN_LF; | |
6243 | break; | |
6244 | } | |
fa42c37f | 6245 | } |
df7492f9 KH |
6246 | if (++total == MAX_EOL_CHECK_COUNT) |
6247 | break; | |
fa42c37f | 6248 | } |
df7492f9 | 6249 | src += 2; |
fa42c37f | 6250 | } |
bcf26d6a | 6251 | } |
d46c5b12 | 6252 | else |
c4825358 | 6253 | { |
df7492f9 | 6254 | while (src < src_end) |
27901516 | 6255 | { |
df7492f9 KH |
6256 | c = *src++; |
6257 | if (c == '\n' || c == '\r') | |
6258 | { | |
6259 | int this_eol; | |
d46c5b12 | 6260 | |
df7492f9 KH |
6261 | if (c == '\n') |
6262 | this_eol = EOL_SEEN_LF; | |
6263 | else if (src >= src_end || *src != '\n') | |
6264 | this_eol = EOL_SEEN_CR; | |
6265 | else | |
6266 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 6267 | |
df7492f9 KH |
6268 | if (eol_seen == EOL_SEEN_NONE) |
6269 | /* This is the first end-of-line. */ | |
6270 | eol_seen = this_eol; | |
6271 | else if (eol_seen != this_eol) | |
6272 | { | |
75f4f1ac EZ |
6273 | /* The found type is different from what found before. |
6274 | Allow for stray ^M characters in DOS EOL files. */ | |
6275 | if (eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF | |
6276 | || eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR) | |
6277 | eol_seen = EOL_SEEN_CRLF; | |
6278 | else | |
6279 | { | |
6280 | eol_seen = EOL_SEEN_LF; | |
6281 | break; | |
6282 | } | |
df7492f9 KH |
6283 | } |
6284 | if (++total == MAX_EOL_CHECK_COUNT) | |
6285 | break; | |
6286 | } | |
6287 | } | |
73be902c | 6288 | } |
df7492f9 | 6289 | return eol_seen; |
73be902c KH |
6290 | } |
6291 | ||
df7492f9 | 6292 | |
24a73b0a | 6293 | static Lisp_Object |
df7492f9 KH |
6294 | adjust_coding_eol_type (coding, eol_seen) |
6295 | struct coding_system *coding; | |
6296 | int eol_seen; | |
73be902c | 6297 | { |
0be8721c | 6298 | Lisp_Object eol_type; |
8f924df7 | 6299 | |
df7492f9 KH |
6300 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
6301 | if (eol_seen & EOL_SEEN_LF) | |
24a73b0a KH |
6302 | { |
6303 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
6304 | eol_type = Qunix; | |
6305 | } | |
6f197c07 | 6306 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
6307 | { |
6308 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
6309 | eol_type = Qdos; | |
6310 | } | |
6f197c07 | 6311 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
6312 | { |
6313 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
6314 | eol_type = Qmac; | |
6315 | } | |
6316 | return eol_type; | |
d46c5b12 | 6317 | } |
4ed46869 | 6318 | |
df7492f9 KH |
6319 | /* Detect how a text specified in CODING is encoded. If a coding |
6320 | system is detected, update fields of CODING by the detected coding | |
6321 | system. */ | |
0a28aafb | 6322 | |
df7492f9 KH |
6323 | void |
6324 | detect_coding (coding) | |
d46c5b12 | 6325 | struct coding_system *coding; |
d46c5b12 | 6326 | { |
8f924df7 | 6327 | const unsigned char *src, *src_end; |
73cce38d | 6328 | int saved_mode = coding->mode; |
d46c5b12 | 6329 | |
df7492f9 KH |
6330 | coding->consumed = coding->consumed_char = 0; |
6331 | coding->produced = coding->produced_char = 0; | |
6332 | coding_set_source (coding); | |
1c3478b0 | 6333 | |
df7492f9 | 6334 | src_end = coding->source + coding->src_bytes; |
c0e16b14 | 6335 | coding->head_ascii = 0; |
1c3478b0 | 6336 | |
df7492f9 KH |
6337 | /* If we have not yet decided the text encoding type, detect it |
6338 | now. */ | |
6339 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 6340 | { |
df7492f9 | 6341 | int c, i; |
6cb21a4f | 6342 | struct coding_detection_info detect_info; |
2f3cbb32 | 6343 | int null_byte_found = 0, eight_bit_found = 0; |
df7492f9 | 6344 | |
6cb21a4f | 6345 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
2f3cbb32 | 6346 | for (src = coding->source; src < src_end; src++) |
d46c5b12 | 6347 | { |
df7492f9 | 6348 | c = *src; |
6cb21a4f | 6349 | if (c & 0x80) |
6cb21a4f | 6350 | { |
2f3cbb32 | 6351 | eight_bit_found = 1; |
2f3cbb32 KH |
6352 | if (null_byte_found) |
6353 | break; | |
6354 | } | |
6355 | else if (c < 0x20) | |
6356 | { | |
6357 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
6358 | && ! inhibit_iso_escape_detection | |
6359 | && ! detect_info.checked) | |
6cb21a4f | 6360 | { |
2f3cbb32 KH |
6361 | if (detect_coding_iso_2022 (coding, &detect_info)) |
6362 | { | |
6363 | /* We have scanned the whole data. */ | |
6364 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
6365 | { |
6366 | /* We didn't find an 8-bit code. We may | |
6367 | have found a null-byte, but it's very | |
6368 | rare that a binary file confirm to | |
6369 | ISO-2022. */ | |
6370 | src = src_end; | |
6371 | coding->head_ascii = src - coding->source; | |
6372 | } | |
6373 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
6374 | break; |
6375 | } | |
6376 | } | |
97b1b294 | 6377 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
6378 | { |
6379 | null_byte_found = 1; | |
6380 | if (eight_bit_found) | |
6381 | break; | |
6cb21a4f | 6382 | } |
c006c0c8 KH |
6383 | if (! eight_bit_found) |
6384 | coding->head_ascii++; | |
6cb21a4f | 6385 | } |
c006c0c8 | 6386 | else if (! eight_bit_found) |
c0e16b14 | 6387 | coding->head_ascii++; |
d46c5b12 | 6388 | } |
df7492f9 | 6389 | |
2f3cbb32 KH |
6390 | if (null_byte_found || eight_bit_found |
6391 | || coding->head_ascii < coding->src_bytes | |
6cb21a4f | 6392 | || detect_info.found) |
d46c5b12 | 6393 | { |
ff0dacd7 KH |
6394 | enum coding_category category; |
6395 | struct coding_system *this; | |
df7492f9 | 6396 | |
6cb21a4f KH |
6397 | if (coding->head_ascii == coding->src_bytes) |
6398 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
6399 | for (i = 0; i < coding_category_raw_text; i++) | |
6400 | { | |
6401 | category = coding_priorities[i]; | |
6402 | this = coding_categories + category; | |
6403 | if (detect_info.found & (1 << category)) | |
24a73b0a | 6404 | break; |
6cb21a4f KH |
6405 | } |
6406 | else | |
2f3cbb32 KH |
6407 | { |
6408 | if (null_byte_found) | |
ff0dacd7 | 6409 | { |
2f3cbb32 KH |
6410 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; |
6411 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
ff0dacd7 | 6412 | } |
2f3cbb32 KH |
6413 | for (i = 0; i < coding_category_raw_text; i++) |
6414 | { | |
6415 | category = coding_priorities[i]; | |
6416 | this = coding_categories + category; | |
6417 | if (this->id < 0) | |
6418 | { | |
6419 | /* No coding system of this category is defined. */ | |
6420 | detect_info.rejected |= (1 << category); | |
6421 | } | |
6422 | else if (category >= coding_category_raw_text) | |
6423 | continue; | |
6424 | else if (detect_info.checked & (1 << category)) | |
6425 | { | |
6426 | if (detect_info.found & (1 << category)) | |
6427 | break; | |
6428 | } | |
6429 | else if ((*(this->detector)) (coding, &detect_info) | |
6430 | && detect_info.found & (1 << category)) | |
6431 | { | |
6432 | if (category == coding_category_utf_16_auto) | |
6433 | { | |
6434 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6435 | category = coding_category_utf_16_le; | |
6436 | else | |
6437 | category = coding_category_utf_16_be; | |
6438 | } | |
6439 | break; | |
6440 | } | |
6441 | } | |
2f3cbb32 | 6442 | } |
c0e16b14 KH |
6443 | |
6444 | if (i < coding_category_raw_text) | |
6445 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6446 | else if (null_byte_found) | |
6447 | setup_coding_system (Qno_conversion, coding); | |
6448 | else if ((detect_info.rejected & CATEGORY_MASK_ANY) | |
6449 | == CATEGORY_MASK_ANY) | |
6450 | setup_coding_system (Qraw_text, coding); | |
6451 | else if (detect_info.rejected) | |
6452 | for (i = 0; i < coding_category_raw_text; i++) | |
6453 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
6454 | { | |
6455 | this = coding_categories + coding_priorities[i]; | |
6456 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6457 | break; | |
6458 | } | |
d46c5b12 | 6459 | } |
b73bfc1c | 6460 | } |
a470d443 KH |
6461 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6462 | == coding_category_utf_8_auto) | |
6463 | { | |
6464 | Lisp_Object coding_systems; | |
6465 | struct coding_detection_info detect_info; | |
6466 | ||
6467 | coding_systems | |
6468 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); | |
6469 | detect_info.found = detect_info.rejected = 0; | |
6470 | coding->head_ascii = 0; | |
6471 | if (CONSP (coding_systems) | |
6472 | && detect_coding_utf_8 (coding, &detect_info)) | |
6473 | { | |
6474 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
6475 | setup_coding_system (XCAR (coding_systems), coding); | |
6476 | else | |
6477 | setup_coding_system (XCDR (coding_systems), coding); | |
6478 | } | |
6479 | } | |
24a73b0a KH |
6480 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6481 | == coding_category_utf_16_auto) | |
b49a1807 KH |
6482 | { |
6483 | Lisp_Object coding_systems; | |
6484 | struct coding_detection_info detect_info; | |
6485 | ||
6486 | coding_systems | |
a470d443 | 6487 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); |
b49a1807 | 6488 | detect_info.found = detect_info.rejected = 0; |
a470d443 | 6489 | coding->head_ascii = 0; |
b49a1807 | 6490 | if (CONSP (coding_systems) |
24a73b0a | 6491 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
6492 | { |
6493 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6494 | setup_coding_system (XCAR (coding_systems), coding); | |
24a73b0a | 6495 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
b49a1807 KH |
6496 | setup_coding_system (XCDR (coding_systems), coding); |
6497 | } | |
6498 | } | |
73cce38d | 6499 | coding->mode = saved_mode; |
4ed46869 | 6500 | } |
4ed46869 | 6501 | |
d46c5b12 | 6502 | |
aaaf0b1e | 6503 | static void |
df7492f9 | 6504 | decode_eol (coding) |
aaaf0b1e | 6505 | struct coding_system *coding; |
aaaf0b1e | 6506 | { |
24a73b0a KH |
6507 | Lisp_Object eol_type; |
6508 | unsigned char *p, *pbeg, *pend; | |
3ed051d4 | 6509 | |
24a73b0a | 6510 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
0a9564cb | 6511 | if (EQ (eol_type, Qunix) || inhibit_eol_conversion) |
24a73b0a KH |
6512 | return; |
6513 | ||
6514 | if (NILP (coding->dst_object)) | |
6515 | pbeg = coding->destination; | |
6516 | else | |
6517 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
6518 | pend = pbeg + coding->produced; | |
6519 | ||
6520 | if (VECTORP (eol_type)) | |
aaaf0b1e | 6521 | { |
df7492f9 | 6522 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 6523 | |
24a73b0a | 6524 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 6525 | { |
df7492f9 KH |
6526 | if (*p == '\n') |
6527 | eol_seen |= EOL_SEEN_LF; | |
6528 | else if (*p == '\r') | |
aaaf0b1e | 6529 | { |
df7492f9 | 6530 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 6531 | { |
df7492f9 KH |
6532 | eol_seen |= EOL_SEEN_CRLF; |
6533 | p++; | |
aaaf0b1e | 6534 | } |
aaaf0b1e | 6535 | else |
df7492f9 | 6536 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 6537 | } |
aaaf0b1e | 6538 | } |
75f4f1ac EZ |
6539 | /* Handle DOS-style EOLs in a file with stray ^M characters. */ |
6540 | if ((eol_seen & EOL_SEEN_CRLF) != 0 | |
6541 | && (eol_seen & EOL_SEEN_CR) != 0 | |
6542 | && (eol_seen & EOL_SEEN_LF) == 0) | |
6543 | eol_seen = EOL_SEEN_CRLF; | |
6544 | else if (eol_seen != EOL_SEEN_NONE | |
24a73b0a KH |
6545 | && eol_seen != EOL_SEEN_LF |
6546 | && eol_seen != EOL_SEEN_CRLF | |
6547 | && eol_seen != EOL_SEEN_CR) | |
6548 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 6549 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 6550 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 6551 | } |
d46c5b12 | 6552 | |
24a73b0a | 6553 | if (EQ (eol_type, Qmac)) |
27901516 | 6554 | { |
24a73b0a | 6555 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
6556 | if (*p == '\r') |
6557 | *p = '\n'; | |
4ed46869 | 6558 | } |
24a73b0a | 6559 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 6560 | { |
24a73b0a | 6561 | int n = 0; |
b73bfc1c | 6562 | |
24a73b0a KH |
6563 | if (NILP (coding->dst_object)) |
6564 | { | |
4347441b KH |
6565 | /* Start deleting '\r' from the tail to minimize the memory |
6566 | movement. */ | |
24a73b0a KH |
6567 | for (p = pend - 2; p >= pbeg; p--) |
6568 | if (*p == '\r') | |
6569 | { | |
6570 | safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1); | |
6571 | n++; | |
6572 | } | |
6573 | } | |
6574 | else | |
6575 | { | |
4347441b KH |
6576 | int pos_byte = coding->dst_pos_byte; |
6577 | int pos = coding->dst_pos; | |
6578 | int pos_end = pos + coding->produced_char - 1; | |
6579 | ||
6580 | while (pos < pos_end) | |
6581 | { | |
6582 | p = BYTE_POS_ADDR (pos_byte); | |
6583 | if (*p == '\r' && p[1] == '\n') | |
6584 | { | |
6585 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
6586 | n++; | |
6587 | pos_end--; | |
6588 | } | |
6589 | pos++; | |
69b8522d KH |
6590 | if (coding->dst_multibyte) |
6591 | pos_byte += BYTES_BY_CHAR_HEAD (*p); | |
6592 | else | |
6593 | pos_byte++; | |
4347441b | 6594 | } |
24a73b0a KH |
6595 | } |
6596 | coding->produced -= n; | |
6597 | coding->produced_char -= n; | |
aaaf0b1e | 6598 | } |
4ed46869 KH |
6599 | } |
6600 | ||
7d64c6ad | 6601 | |
a6f87d34 KH |
6602 | /* Return a translation table (or list of them) from coding system |
6603 | attribute vector ATTRS for encoding (ENCODEP is nonzero) or | |
6604 | decoding (ENCODEP is zero). */ | |
7d64c6ad | 6605 | |
e6a54062 | 6606 | static Lisp_Object |
09ee6fdd KH |
6607 | get_translation_table (attrs, encodep, max_lookup) |
6608 | Lisp_Object attrs; | |
6609 | int encodep, *max_lookup; | |
7d64c6ad KH |
6610 | { |
6611 | Lisp_Object standard, translation_table; | |
09ee6fdd | 6612 | Lisp_Object val; |
7d64c6ad KH |
6613 | |
6614 | if (encodep) | |
6615 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
6616 | standard = Vstandard_translation_table_for_encode; | |
6617 | else | |
6618 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
6619 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 6620 | if (NILP (translation_table)) |
09ee6fdd KH |
6621 | translation_table = standard; |
6622 | else | |
a6f87d34 | 6623 | { |
09ee6fdd KH |
6624 | if (SYMBOLP (translation_table)) |
6625 | translation_table = Fget (translation_table, Qtranslation_table); | |
6626 | else if (CONSP (translation_table)) | |
6627 | { | |
6628 | translation_table = Fcopy_sequence (translation_table); | |
6629 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
6630 | if (SYMBOLP (XCAR (val))) | |
6631 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
6632 | } | |
6633 | if (CHAR_TABLE_P (standard)) | |
6634 | { | |
6635 | if (CONSP (translation_table)) | |
6636 | translation_table = nconc2 (translation_table, | |
6637 | Fcons (standard, Qnil)); | |
6638 | else | |
6639 | translation_table = Fcons (translation_table, | |
6640 | Fcons (standard, Qnil)); | |
6641 | } | |
a6f87d34 | 6642 | } |
2170c8f0 KH |
6643 | |
6644 | if (max_lookup) | |
09ee6fdd | 6645 | { |
2170c8f0 KH |
6646 | *max_lookup = 1; |
6647 | if (CHAR_TABLE_P (translation_table) | |
6648 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
6649 | { | |
6650 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
6651 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
6652 | *max_lookup = XFASTINT (val); | |
6653 | } | |
6654 | else if (CONSP (translation_table)) | |
6655 | { | |
6656 | Lisp_Object tail, val; | |
09ee6fdd | 6657 | |
2170c8f0 KH |
6658 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
6659 | if (CHAR_TABLE_P (XCAR (tail)) | |
6660 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
6661 | { | |
6662 | val = XCHAR_TABLE (XCAR (tail))->extras[1]; | |
6663 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
6664 | *max_lookup = XFASTINT (val); | |
6665 | } | |
6666 | } | |
a6f87d34 | 6667 | } |
7d64c6ad KH |
6668 | return translation_table; |
6669 | } | |
6670 | ||
09ee6fdd KH |
6671 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
6672 | do { \ | |
6673 | trans = Qnil; \ | |
6674 | if (CHAR_TABLE_P (table)) \ | |
6675 | { \ | |
6676 | trans = CHAR_TABLE_REF (table, c); \ | |
6677 | if (CHARACTERP (trans)) \ | |
6678 | c = XFASTINT (trans), trans = Qnil; \ | |
6679 | } \ | |
6680 | else if (CONSP (table)) \ | |
6681 | { \ | |
6682 | Lisp_Object tail; \ | |
6683 | \ | |
6684 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
6685 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
6686 | { \ | |
6687 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
6688 | if (CHARACTERP (trans)) \ | |
6689 | c = XFASTINT (trans), trans = Qnil; \ | |
6690 | else if (! NILP (trans)) \ | |
6691 | break; \ | |
6692 | } \ | |
6693 | } \ | |
e6a54062 KH |
6694 | } while (0) |
6695 | ||
7d64c6ad | 6696 | |
e951386e KH |
6697 | /* Return a translation of character(s) at BUF according to TRANS. |
6698 | TRANS is TO-CHAR or ((FROM . TO) ...) where | |
6699 | FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...]. | |
6700 | The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a | |
6701 | translation is found, and Qnil if not found.. | |
6702 | If BUF is too short to lookup characters in FROM, return Qt. */ | |
6703 | ||
69a80ea3 | 6704 | static Lisp_Object |
e951386e KH |
6705 | get_translation (trans, buf, buf_end) |
6706 | Lisp_Object trans; | |
69a80ea3 | 6707 | int *buf, *buf_end; |
69a80ea3 | 6708 | { |
e951386e KH |
6709 | |
6710 | if (INTEGERP (trans)) | |
6711 | return trans; | |
6712 | for (; CONSP (trans); trans = XCDR (trans)) | |
69a80ea3 | 6713 | { |
e951386e KH |
6714 | Lisp_Object val = XCAR (trans); |
6715 | Lisp_Object from = XCAR (val); | |
6716 | int len = ASIZE (from); | |
6717 | int i; | |
69a80ea3 | 6718 | |
e951386e | 6719 | for (i = 0; i < len; i++) |
69a80ea3 | 6720 | { |
e951386e KH |
6721 | if (buf + i == buf_end) |
6722 | return Qt; | |
6723 | if (XINT (AREF (from, i)) != buf[i]) | |
6724 | break; | |
69a80ea3 | 6725 | } |
e951386e KH |
6726 | if (i == len) |
6727 | return val; | |
69a80ea3 | 6728 | } |
e951386e | 6729 | return Qnil; |
69a80ea3 KH |
6730 | } |
6731 | ||
6732 | ||
d46c5b12 | 6733 | static int |
69a80ea3 | 6734 | produce_chars (coding, translation_table, last_block) |
df7492f9 | 6735 | struct coding_system *coding; |
69a80ea3 KH |
6736 | Lisp_Object translation_table; |
6737 | int last_block; | |
4ed46869 | 6738 | { |
df7492f9 KH |
6739 | unsigned char *dst = coding->destination + coding->produced; |
6740 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
119852e7 KH |
6741 | EMACS_INT produced; |
6742 | EMACS_INT produced_chars = 0; | |
69a80ea3 | 6743 | int carryover = 0; |
4ed46869 | 6744 | |
df7492f9 | 6745 | if (! coding->chars_at_source) |
4ed46869 | 6746 | { |
119852e7 | 6747 | /* Source characters are in coding->charbuf. */ |
fba4576f AS |
6748 | int *buf = coding->charbuf; |
6749 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 6750 | |
db274c7a KH |
6751 | if (EQ (coding->src_object, coding->dst_object)) |
6752 | { | |
6753 | coding_set_source (coding); | |
6754 | dst_end = ((unsigned char *) coding->source) + coding->consumed; | |
6755 | } | |
4ed46869 | 6756 | |
df7492f9 | 6757 | while (buf < buf_end) |
4ed46869 | 6758 | { |
69a80ea3 | 6759 | int c = *buf, i; |
bc4bc72a | 6760 | |
df7492f9 KH |
6761 | if (c >= 0) |
6762 | { | |
69a80ea3 KH |
6763 | int from_nchars = 1, to_nchars = 1; |
6764 | Lisp_Object trans = Qnil; | |
6765 | ||
09ee6fdd | 6766 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 6767 | if (! NILP (trans)) |
69a80ea3 | 6768 | { |
e951386e KH |
6769 | trans = get_translation (trans, buf, buf_end); |
6770 | if (INTEGERP (trans)) | |
6771 | c = XINT (trans); | |
6772 | else if (CONSP (trans)) | |
6773 | { | |
6774 | from_nchars = ASIZE (XCAR (trans)); | |
6775 | trans = XCDR (trans); | |
6776 | if (INTEGERP (trans)) | |
6777 | c = XINT (trans); | |
6778 | else | |
6779 | { | |
6780 | to_nchars = ASIZE (trans); | |
6781 | c = XINT (AREF (trans, 0)); | |
6782 | } | |
6783 | } | |
6784 | else if (EQ (trans, Qt) && ! last_block) | |
69a80ea3 | 6785 | break; |
69a80ea3 KH |
6786 | } |
6787 | ||
6788 | if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end) | |
6789 | { | |
6790 | dst = alloc_destination (coding, | |
6791 | buf_end - buf | |
6792 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
6793 | dst); | |
db274c7a KH |
6794 | if (EQ (coding->src_object, coding->dst_object)) |
6795 | { | |
6796 | coding_set_source (coding); | |
e951386e KH |
6797 | dst_end = (((unsigned char *) coding->source) |
6798 | + coding->consumed); | |
db274c7a KH |
6799 | } |
6800 | else | |
6801 | dst_end = coding->destination + coding->dst_bytes; | |
69a80ea3 KH |
6802 | } |
6803 | ||
433f7f87 | 6804 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 6805 | { |
433f7f87 KH |
6806 | if (i > 0) |
6807 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
6808 | if (coding->dst_multibyte |
6809 | || ! CHAR_BYTE8_P (c)) | |
db274c7a | 6810 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
69a80ea3 KH |
6811 | else |
6812 | *dst++ = CHAR_TO_BYTE8 (c); | |
6813 | } | |
6814 | produced_chars += to_nchars; | |
e951386e | 6815 | buf += from_nchars; |
d46c5b12 | 6816 | } |
df7492f9 | 6817 | else |
69a80ea3 KH |
6818 | /* This is an annotation datum. (-C) is the length. */ |
6819 | buf += -c; | |
4ed46869 | 6820 | } |
69a80ea3 | 6821 | carryover = buf_end - buf; |
4ed46869 | 6822 | } |
fa42c37f | 6823 | else |
fa42c37f | 6824 | { |
119852e7 | 6825 | /* Source characters are at coding->source. */ |
8f924df7 | 6826 | const unsigned char *src = coding->source; |
119852e7 | 6827 | const unsigned char *src_end = src + coding->consumed; |
4ed46869 | 6828 | |
db274c7a KH |
6829 | if (EQ (coding->dst_object, coding->src_object)) |
6830 | dst_end = (unsigned char *) src; | |
df7492f9 | 6831 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 6832 | { |
df7492f9 | 6833 | if (coding->src_multibyte) |
fa42c37f | 6834 | { |
71c81426 | 6835 | int multibytep = 1; |
4533845d | 6836 | EMACS_INT consumed_chars = 0; |
d46c5b12 | 6837 | |
df7492f9 KH |
6838 | while (1) |
6839 | { | |
8f924df7 | 6840 | const unsigned char *src_base = src; |
df7492f9 | 6841 | int c; |
b73bfc1c | 6842 | |
df7492f9 | 6843 | ONE_MORE_BYTE (c); |
119852e7 | 6844 | if (dst == dst_end) |
df7492f9 | 6845 | { |
119852e7 KH |
6846 | if (EQ (coding->src_object, coding->dst_object)) |
6847 | dst_end = (unsigned char *) src; | |
6848 | if (dst == dst_end) | |
df7492f9 | 6849 | { |
119852e7 KH |
6850 | EMACS_INT offset = src - coding->source; |
6851 | ||
6852 | dst = alloc_destination (coding, src_end - src + 1, | |
6853 | dst); | |
6854 | dst_end = coding->destination + coding->dst_bytes; | |
6855 | coding_set_source (coding); | |
6856 | src = coding->source + offset; | |
6857 | src_end = coding->source + coding->src_bytes; | |
db274c7a KH |
6858 | if (EQ (coding->src_object, coding->dst_object)) |
6859 | dst_end = (unsigned char *) src; | |
df7492f9 | 6860 | } |
df7492f9 KH |
6861 | } |
6862 | *dst++ = c; | |
6863 | produced_chars++; | |
6864 | } | |
6865 | no_more_source: | |
6866 | ; | |
fa42c37f KH |
6867 | } |
6868 | else | |
df7492f9 KH |
6869 | while (src < src_end) |
6870 | { | |
71c81426 | 6871 | int multibytep = 1; |
df7492f9 | 6872 | int c = *src++; |
b73bfc1c | 6873 | |
df7492f9 KH |
6874 | if (dst >= dst_end - 1) |
6875 | { | |
2c78b7e1 | 6876 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 6877 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
6878 | if (dst >= dst_end - 1) |
6879 | { | |
119852e7 | 6880 | EMACS_INT offset = src - coding->source; |
db274c7a | 6881 | EMACS_INT more_bytes; |
119852e7 | 6882 | |
db274c7a KH |
6883 | if (EQ (coding->src_object, coding->dst_object)) |
6884 | more_bytes = ((src_end - src) / 2) + 2; | |
6885 | else | |
6886 | more_bytes = src_end - src + 2; | |
6887 | dst = alloc_destination (coding, more_bytes, dst); | |
2c78b7e1 KH |
6888 | dst_end = coding->destination + coding->dst_bytes; |
6889 | coding_set_source (coding); | |
119852e7 | 6890 | src = coding->source + offset; |
2c78b7e1 | 6891 | src_end = coding->source + coding->src_bytes; |
db274c7a KH |
6892 | if (EQ (coding->src_object, coding->dst_object)) |
6893 | dst_end = (unsigned char *) src; | |
2c78b7e1 | 6894 | } |
df7492f9 KH |
6895 | } |
6896 | EMIT_ONE_BYTE (c); | |
6897 | } | |
d46c5b12 | 6898 | } |
df7492f9 KH |
6899 | else |
6900 | { | |
6901 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 6902 | { |
119852e7 | 6903 | EMACS_INT require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 6904 | |
df7492f9 | 6905 | if (require > 0) |
fa42c37f | 6906 | { |
df7492f9 KH |
6907 | EMACS_INT offset = src - coding->source; |
6908 | ||
6909 | dst = alloc_destination (coding, require, dst); | |
6910 | coding_set_source (coding); | |
6911 | src = coding->source + offset; | |
6912 | src_end = coding->source + coding->src_bytes; | |
fa42c37f KH |
6913 | } |
6914 | } | |
119852e7 | 6915 | produced_chars = coding->consumed_char; |
df7492f9 | 6916 | while (src < src_end) |
14daee73 | 6917 | *dst++ = *src++; |
fa42c37f KH |
6918 | } |
6919 | } | |
6920 | ||
df7492f9 | 6921 | produced = dst - (coding->destination + coding->produced); |
284201e4 | 6922 | if (BUFFERP (coding->dst_object) && produced_chars > 0) |
df7492f9 KH |
6923 | insert_from_gap (produced_chars, produced); |
6924 | coding->produced += produced; | |
6925 | coding->produced_char += produced_chars; | |
69a80ea3 | 6926 | return carryover; |
fa42c37f KH |
6927 | } |
6928 | ||
ff0dacd7 KH |
6929 | /* Compose text in CODING->object according to the annotation data at |
6930 | CHARBUF. CHARBUF is an array: | |
e951386e | 6931 | [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ] |
df7492f9 | 6932 | */ |
4ed46869 | 6933 | |
df7492f9 | 6934 | static INLINE void |
69a80ea3 | 6935 | produce_composition (coding, charbuf, pos) |
4ed46869 | 6936 | struct coding_system *coding; |
df7492f9 | 6937 | int *charbuf; |
69a80ea3 | 6938 | EMACS_INT pos; |
4ed46869 | 6939 | { |
df7492f9 | 6940 | int len; |
69a80ea3 | 6941 | EMACS_INT to; |
df7492f9 | 6942 | enum composition_method method; |
df7492f9 | 6943 | Lisp_Object components; |
fa42c37f | 6944 | |
e951386e | 6945 | len = -charbuf[0] - MAX_ANNOTATION_LENGTH; |
69a80ea3 | 6946 | to = pos + charbuf[2]; |
e951386e | 6947 | method = (enum composition_method) (charbuf[4]); |
d46c5b12 | 6948 | |
df7492f9 KH |
6949 | if (method == COMPOSITION_RELATIVE) |
6950 | components = Qnil; | |
e951386e | 6951 | else |
d46c5b12 | 6952 | { |
df7492f9 | 6953 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
e951386e | 6954 | int i, j; |
b73bfc1c | 6955 | |
e951386e KH |
6956 | if (method == COMPOSITION_WITH_RULE) |
6957 | len = charbuf[2] * 3 - 2; | |
6958 | charbuf += MAX_ANNOTATION_LENGTH; | |
6959 | /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */ | |
6960 | for (i = j = 0; i < len && charbuf[i] != -1; i++, j++) | |
9ffd559c | 6961 | { |
e951386e KH |
6962 | if (charbuf[i] >= 0) |
6963 | args[j] = make_number (charbuf[i]); | |
6964 | else | |
6965 | { | |
6966 | i++; | |
6967 | args[j] = make_number (charbuf[i] % 0x100); | |
6968 | } | |
9ffd559c | 6969 | } |
e951386e | 6970 | components = (i == j ? Fstring (j, args) : Fvector (j, args)); |
d46c5b12 | 6971 | } |
69a80ea3 | 6972 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
6973 | } |
6974 | ||
d46c5b12 | 6975 | |
ff0dacd7 KH |
6976 | /* Put `charset' property on text in CODING->object according to |
6977 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 6978 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 6979 | */ |
d46c5b12 | 6980 | |
ff0dacd7 | 6981 | static INLINE void |
69a80ea3 | 6982 | produce_charset (coding, charbuf, pos) |
d46c5b12 | 6983 | struct coding_system *coding; |
ff0dacd7 | 6984 | int *charbuf; |
69a80ea3 | 6985 | EMACS_INT pos; |
d46c5b12 | 6986 | { |
69a80ea3 KH |
6987 | EMACS_INT from = pos - charbuf[2]; |
6988 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); | |
b73bfc1c | 6989 | |
69a80ea3 | 6990 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
6991 | Qcharset, CHARSET_NAME (charset), |
6992 | coding->dst_object); | |
d46c5b12 KH |
6993 | } |
6994 | ||
d46c5b12 | 6995 | |
df7492f9 KH |
6996 | #define CHARBUF_SIZE 0x4000 |
6997 | ||
6998 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
6999 | do { \ | |
8510724d | 7000 | int size = CHARBUF_SIZE; \ |
df7492f9 KH |
7001 | \ |
7002 | coding->charbuf = NULL; \ | |
7003 | while (size > 1024) \ | |
7004 | { \ | |
7005 | coding->charbuf = (int *) alloca (sizeof (int) * size); \ | |
7006 | if (coding->charbuf) \ | |
7007 | break; \ | |
7008 | size >>= 1; \ | |
7009 | } \ | |
7010 | if (! coding->charbuf) \ | |
7011 | { \ | |
065e3595 | 7012 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \ |
df7492f9 KH |
7013 | return coding->result; \ |
7014 | } \ | |
7015 | coding->charbuf_size = size; \ | |
7016 | } while (0) | |
4ed46869 | 7017 | |
d46c5b12 KH |
7018 | |
7019 | static void | |
69a80ea3 | 7020 | produce_annotation (coding, pos) |
d46c5b12 | 7021 | struct coding_system *coding; |
69a80ea3 | 7022 | EMACS_INT pos; |
d46c5b12 | 7023 | { |
df7492f9 KH |
7024 | int *charbuf = coding->charbuf; |
7025 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 7026 | |
ff0dacd7 KH |
7027 | if (NILP (coding->dst_object)) |
7028 | return; | |
d46c5b12 | 7029 | |
df7492f9 | 7030 | while (charbuf < charbuf_end) |
a84f1519 | 7031 | { |
df7492f9 | 7032 | if (*charbuf >= 0) |
e951386e | 7033 | pos++, charbuf++; |
d46c5b12 | 7034 | else |
d46c5b12 | 7035 | { |
df7492f9 | 7036 | int len = -*charbuf; |
e951386e KH |
7037 | |
7038 | if (len > 2) | |
7039 | switch (charbuf[1]) | |
7040 | { | |
7041 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
7042 | produce_composition (coding, charbuf, pos); | |
7043 | break; | |
7044 | case CODING_ANNOTATE_CHARSET_MASK: | |
7045 | produce_charset (coding, charbuf, pos); | |
7046 | break; | |
7047 | } | |
df7492f9 | 7048 | charbuf += len; |
d46c5b12 | 7049 | } |
a84f1519 | 7050 | } |
d46c5b12 KH |
7051 | } |
7052 | ||
df7492f9 KH |
7053 | /* Decode the data at CODING->src_object into CODING->dst_object. |
7054 | CODING->src_object is a buffer, a string, or nil. | |
7055 | CODING->dst_object is a buffer. | |
d46c5b12 | 7056 | |
df7492f9 KH |
7057 | If CODING->src_object is a buffer, it must be the current buffer. |
7058 | In this case, if CODING->src_pos is positive, it is a position of | |
7059 | the source text in the buffer, otherwise, the source text is in the | |
7060 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
7061 | the text from GPT (which must be the same as PT). If this is the | |
7062 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7063 | negative. | |
d46c5b12 | 7064 | |
b6828792 | 7065 | If CODING->src_object is a string, CODING->src_pos is an index to |
df7492f9 | 7066 | that string. |
d46c5b12 | 7067 | |
df7492f9 KH |
7068 | If CODING->src_object is nil, CODING->source must already point to |
7069 | the non-relocatable memory area. In this case, CODING->src_pos is | |
7070 | an offset from CODING->source. | |
73be902c | 7071 | |
df7492f9 KH |
7072 | The decoded data is inserted at the current point of the buffer |
7073 | CODING->dst_object. | |
7074 | */ | |
d46c5b12 | 7075 | |
df7492f9 KH |
7076 | static int |
7077 | decode_coding (coding) | |
d46c5b12 | 7078 | struct coding_system *coding; |
d46c5b12 | 7079 | { |
df7492f9 | 7080 | Lisp_Object attrs; |
24a73b0a | 7081 | Lisp_Object undo_list; |
7d64c6ad | 7082 | Lisp_Object translation_table; |
69a80ea3 KH |
7083 | int carryover; |
7084 | int i; | |
d46c5b12 | 7085 | |
df7492f9 KH |
7086 | if (BUFFERP (coding->src_object) |
7087 | && coding->src_pos > 0 | |
7088 | && coding->src_pos < GPT | |
7089 | && coding->src_pos + coding->src_chars > GPT) | |
7090 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 7091 | |
24a73b0a | 7092 | undo_list = Qt; |
df7492f9 | 7093 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 7094 | { |
df7492f9 KH |
7095 | if (current_buffer != XBUFFER (coding->dst_object)) |
7096 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
7097 | if (GPT != PT) | |
7098 | move_gap_both (PT, PT_BYTE); | |
24a73b0a KH |
7099 | undo_list = current_buffer->undo_list; |
7100 | current_buffer->undo_list = Qt; | |
1c3478b0 KH |
7101 | } |
7102 | ||
df7492f9 KH |
7103 | coding->consumed = coding->consumed_char = 0; |
7104 | coding->produced = coding->produced_char = 0; | |
7105 | coding->chars_at_source = 0; | |
065e3595 | 7106 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 7107 | coding->errors = 0; |
1c3478b0 | 7108 | |
df7492f9 KH |
7109 | ALLOC_CONVERSION_WORK_AREA (coding); |
7110 | ||
7111 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 7112 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 7113 | |
69a80ea3 | 7114 | carryover = 0; |
df7492f9 | 7115 | do |
b73bfc1c | 7116 | { |
69a80ea3 KH |
7117 | EMACS_INT pos = coding->dst_pos + coding->produced_char; |
7118 | ||
df7492f9 KH |
7119 | coding_set_source (coding); |
7120 | coding->annotated = 0; | |
69a80ea3 | 7121 | coding->charbuf_used = carryover; |
df7492f9 | 7122 | (*(coding->decoder)) (coding); |
df7492f9 | 7123 | coding_set_destination (coding); |
69a80ea3 | 7124 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 7125 | if (coding->annotated) |
69a80ea3 KH |
7126 | produce_annotation (coding, pos); |
7127 | for (i = 0; i < carryover; i++) | |
7128 | coding->charbuf[i] | |
7129 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 7130 | } |
df7492f9 | 7131 | while (coding->consumed < coding->src_bytes |
54b367bb KH |
7132 | && (coding->result == CODING_RESULT_SUCCESS |
7133 | || coding->result == CODING_RESULT_INVALID_SRC)); | |
d46c5b12 | 7134 | |
69a80ea3 KH |
7135 | if (carryover > 0) |
7136 | { | |
7137 | coding_set_destination (coding); | |
7138 | coding->charbuf_used = carryover; | |
7139 | produce_chars (coding, translation_table, 1); | |
7140 | } | |
7141 | ||
df7492f9 KH |
7142 | coding->carryover_bytes = 0; |
7143 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 7144 | { |
df7492f9 | 7145 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 7146 | const unsigned char *src; |
df7492f9 KH |
7147 | |
7148 | coding_set_source (coding); | |
7149 | coding_set_destination (coding); | |
7150 | src = coding->source + coding->consumed; | |
7151 | ||
7152 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 7153 | { |
df7492f9 KH |
7154 | /* Flush out unprocessed data as binary chars. We are sure |
7155 | that the number of data is less than the size of | |
7156 | coding->charbuf. */ | |
065e3595 | 7157 | coding->charbuf_used = 0; |
b2dab6c8 JR |
7158 | coding->chars_at_source = 0; |
7159 | ||
df7492f9 | 7160 | while (nbytes-- > 0) |
1c3478b0 | 7161 | { |
df7492f9 | 7162 | int c = *src++; |
98725083 | 7163 | |
1c91457d KH |
7164 | if (c & 0x80) |
7165 | c = BYTE8_TO_CHAR (c); | |
7166 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 7167 | } |
f6cbaf43 | 7168 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 7169 | } |
d46c5b12 | 7170 | else |
df7492f9 KH |
7171 | { |
7172 | /* Record unprocessed bytes in coding->carryover. We are | |
7173 | sure that the number of data is less than the size of | |
7174 | coding->carryover. */ | |
7175 | unsigned char *p = coding->carryover; | |
7176 | ||
f289d375 KH |
7177 | if (nbytes > sizeof coding->carryover) |
7178 | nbytes = sizeof coding->carryover; | |
df7492f9 KH |
7179 | coding->carryover_bytes = nbytes; |
7180 | while (nbytes-- > 0) | |
7181 | *p++ = *src++; | |
1c3478b0 | 7182 | } |
df7492f9 | 7183 | coding->consumed = coding->src_bytes; |
b73bfc1c | 7184 | } |
69f76525 | 7185 | |
0a9564cb EZ |
7186 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix) |
7187 | && !inhibit_eol_conversion) | |
4347441b | 7188 | decode_eol (coding); |
24a73b0a KH |
7189 | if (BUFFERP (coding->dst_object)) |
7190 | { | |
7191 | current_buffer->undo_list = undo_list; | |
7192 | record_insert (coding->dst_pos, coding->produced_char); | |
7193 | } | |
73be902c | 7194 | return coding->result; |
4ed46869 KH |
7195 | } |
7196 | ||
aaaf0b1e | 7197 | |
e1c23804 | 7198 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
7199 | ending before LIMIT of CODING->src_object (buffer or string), store |
7200 | the data in BUF, set *STOP to a starting position of the next | |
7201 | composition (if any) or to LIMIT, and return the address of the | |
7202 | next element of BUF. | |
7203 | ||
7204 | If such an annotation is not found, set *STOP to a starting | |
7205 | position of a composition after POS (if any) or to LIMIT, and | |
7206 | return BUF. */ | |
7207 | ||
7208 | static INLINE int * | |
7209 | handle_composition_annotation (pos, limit, coding, buf, stop) | |
7210 | EMACS_INT pos, limit; | |
aaaf0b1e | 7211 | struct coding_system *coding; |
ff0dacd7 KH |
7212 | int *buf; |
7213 | EMACS_INT *stop; | |
aaaf0b1e | 7214 | { |
ff0dacd7 KH |
7215 | EMACS_INT start, end; |
7216 | Lisp_Object prop; | |
aaaf0b1e | 7217 | |
ff0dacd7 KH |
7218 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
7219 | || end > limit) | |
7220 | *stop = limit; | |
7221 | else if (start > pos) | |
7222 | *stop = start; | |
7223 | else | |
aaaf0b1e | 7224 | { |
ff0dacd7 | 7225 | if (start == pos) |
aaaf0b1e | 7226 | { |
ff0dacd7 KH |
7227 | /* We found a composition. Store the corresponding |
7228 | annotation data in BUF. */ | |
7229 | int *head = buf; | |
7230 | enum composition_method method = COMPOSITION_METHOD (prop); | |
7231 | int nchars = COMPOSITION_LENGTH (prop); | |
7232 | ||
e951386e | 7233 | ADD_COMPOSITION_DATA (buf, nchars, 0, method); |
ff0dacd7 | 7234 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 7235 | { |
ff0dacd7 KH |
7236 | Lisp_Object components; |
7237 | int len, i, i_byte; | |
7238 | ||
7239 | components = COMPOSITION_COMPONENTS (prop); | |
7240 | if (VECTORP (components)) | |
aaaf0b1e | 7241 | { |
ff0dacd7 KH |
7242 | len = XVECTOR (components)->size; |
7243 | for (i = 0; i < len; i++) | |
7244 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 7245 | } |
ff0dacd7 | 7246 | else if (STRINGP (components)) |
aaaf0b1e | 7247 | { |
8f924df7 | 7248 | len = SCHARS (components); |
ff0dacd7 KH |
7249 | i = i_byte = 0; |
7250 | while (i < len) | |
7251 | { | |
7252 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
7253 | buf++; | |
7254 | } | |
7255 | } | |
7256 | else if (INTEGERP (components)) | |
7257 | { | |
7258 | len = 1; | |
7259 | *buf++ = XINT (components); | |
7260 | } | |
7261 | else if (CONSP (components)) | |
7262 | { | |
7263 | for (len = 0; CONSP (components); | |
7264 | len++, components = XCDR (components)) | |
7265 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 7266 | } |
aaaf0b1e | 7267 | else |
ff0dacd7 KH |
7268 | abort (); |
7269 | *head -= len; | |
aaaf0b1e | 7270 | } |
aaaf0b1e | 7271 | } |
ff0dacd7 KH |
7272 | |
7273 | if (find_composition (end, limit, &start, &end, &prop, | |
7274 | coding->src_object) | |
7275 | && end <= limit) | |
7276 | *stop = start; | |
7277 | else | |
7278 | *stop = limit; | |
aaaf0b1e | 7279 | } |
ff0dacd7 KH |
7280 | return buf; |
7281 | } | |
7282 | ||
7283 | ||
e1c23804 | 7284 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
7285 | CODING->src_object (buffer of string), store the data in BUF, set |
7286 | *STOP to the position where the value of `charset' property changes | |
7287 | (limiting by LIMIT), and return the address of the next element of | |
7288 | BUF. | |
7289 | ||
7290 | If the property value is nil, set *STOP to the position where the | |
7291 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
7292 | ||
7293 | static INLINE int * | |
7294 | handle_charset_annotation (pos, limit, coding, buf, stop) | |
7295 | EMACS_INT pos, limit; | |
7296 | struct coding_system *coding; | |
7297 | int *buf; | |
7298 | EMACS_INT *stop; | |
7299 | { | |
7300 | Lisp_Object val, next; | |
7301 | int id; | |
7302 | ||
7303 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
7304 | if (! NILP (val) && CHARSETP (val)) | |
7305 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
7306 | else | |
7307 | id = -1; | |
69a80ea3 | 7308 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
7309 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
7310 | coding->src_object, | |
7311 | make_number (limit)); | |
7312 | *stop = XINT (next); | |
7313 | return buf; | |
7314 | } | |
7315 | ||
7316 | ||
df7492f9 | 7317 | static void |
09ee6fdd | 7318 | consume_chars (coding, translation_table, max_lookup) |
df7492f9 | 7319 | struct coding_system *coding; |
433f7f87 | 7320 | Lisp_Object translation_table; |
09ee6fdd | 7321 | int max_lookup; |
df7492f9 KH |
7322 | { |
7323 | int *buf = coding->charbuf; | |
ff0dacd7 | 7324 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 7325 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 7326 | const unsigned char *src_end = coding->source + coding->src_bytes; |
ff0dacd7 KH |
7327 | EMACS_INT pos = coding->src_pos + coding->consumed_char; |
7328 | EMACS_INT end_pos = coding->src_pos + coding->src_chars; | |
df7492f9 KH |
7329 | int multibytep = coding->src_multibyte; |
7330 | Lisp_Object eol_type; | |
7331 | int c; | |
ff0dacd7 | 7332 | EMACS_INT stop, stop_composition, stop_charset; |
09ee6fdd | 7333 | int *lookup_buf = NULL; |
433f7f87 KH |
7334 | |
7335 | if (! NILP (translation_table)) | |
09ee6fdd | 7336 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 7337 | |
0a9564cb | 7338 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
df7492f9 KH |
7339 | if (VECTORP (eol_type)) |
7340 | eol_type = Qunix; | |
88993dfd | 7341 | |
df7492f9 KH |
7342 | /* Note: composition handling is not yet implemented. */ |
7343 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 7344 | |
0b5670c9 KH |
7345 | if (NILP (coding->src_object)) |
7346 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 7347 | else |
0b5670c9 KH |
7348 | { |
7349 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
7350 | stop = stop_composition = pos; | |
7351 | else | |
7352 | stop = stop_composition = end_pos; | |
7353 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
7354 | stop = stop_charset = pos; | |
7355 | else | |
7356 | stop_charset = end_pos; | |
7357 | } | |
ec6d2bb8 | 7358 | |
24a73b0a | 7359 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 7360 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 7361 | while (buf < buf_end) |
aaaf0b1e | 7362 | { |
433f7f87 KH |
7363 | Lisp_Object trans; |
7364 | ||
df7492f9 | 7365 | if (pos == stop) |
ec6d2bb8 | 7366 | { |
df7492f9 KH |
7367 | if (pos == end_pos) |
7368 | break; | |
ff0dacd7 KH |
7369 | if (pos == stop_composition) |
7370 | buf = handle_composition_annotation (pos, end_pos, coding, | |
7371 | buf, &stop_composition); | |
7372 | if (pos == stop_charset) | |
7373 | buf = handle_charset_annotation (pos, end_pos, coding, | |
7374 | buf, &stop_charset); | |
7375 | stop = (stop_composition < stop_charset | |
7376 | ? stop_composition : stop_charset); | |
df7492f9 KH |
7377 | } |
7378 | ||
7379 | if (! multibytep) | |
4776e638 | 7380 | { |
d3e4cb56 | 7381 | EMACS_INT bytes; |
aaaf0b1e | 7382 | |
ea29edf2 KH |
7383 | if (coding->encoder == encode_coding_raw_text) |
7384 | c = *src++, pos++; | |
7385 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
db274c7a | 7386 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes; |
4776e638 | 7387 | else |
f03caae0 | 7388 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 7389 | } |
df7492f9 | 7390 | else |
db274c7a | 7391 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++; |
df7492f9 KH |
7392 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
7393 | c = '\n'; | |
7394 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 7395 | { |
df7492f9 | 7396 | if (c == '\n') |
aaaf0b1e | 7397 | { |
df7492f9 KH |
7398 | if (EQ (eol_type, Qdos)) |
7399 | *buf++ = '\r'; | |
7400 | else | |
7401 | c = '\r'; | |
aaaf0b1e KH |
7402 | } |
7403 | } | |
433f7f87 | 7404 | |
e6a54062 | 7405 | trans = Qnil; |
09ee6fdd | 7406 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 7407 | if (NILP (trans)) |
433f7f87 KH |
7408 | *buf++ = c; |
7409 | else | |
7410 | { | |
7411 | int from_nchars = 1, to_nchars = 1; | |
7412 | int *lookup_buf_end; | |
7413 | const unsigned char *p = src; | |
7414 | int i; | |
7415 | ||
7416 | lookup_buf[0] = c; | |
7417 | for (i = 1; i < max_lookup && p < src_end; i++) | |
7418 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
7419 | lookup_buf_end = lookup_buf + i; | |
e951386e KH |
7420 | trans = get_translation (trans, lookup_buf, lookup_buf_end); |
7421 | if (INTEGERP (trans)) | |
7422 | c = XINT (trans); | |
7423 | else if (CONSP (trans)) | |
7424 | { | |
7425 | from_nchars = ASIZE (XCAR (trans)); | |
7426 | trans = XCDR (trans); | |
7427 | if (INTEGERP (trans)) | |
7428 | c = XINT (trans); | |
7429 | else | |
7430 | { | |
7431 | to_nchars = ASIZE (trans); | |
7432 | if (buf + to_nchars > buf_end) | |
7433 | break; | |
7434 | c = XINT (AREF (trans, 0)); | |
7435 | } | |
7436 | } | |
7437 | else | |
433f7f87 | 7438 | break; |
e951386e | 7439 | *buf++ = c; |
433f7f87 KH |
7440 | for (i = 1; i < to_nchars; i++) |
7441 | *buf++ = XINT (AREF (trans, i)); | |
7442 | for (i = 1; i < from_nchars; i++, pos++) | |
7443 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
7444 | } | |
aaaf0b1e | 7445 | } |
ec6d2bb8 | 7446 | |
df7492f9 KH |
7447 | coding->consumed = src - coding->source; |
7448 | coding->consumed_char = pos - coding->src_pos; | |
7449 | coding->charbuf_used = buf - coding->charbuf; | |
7450 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
7451 | } |
7452 | ||
4ed46869 | 7453 | |
df7492f9 KH |
7454 | /* Encode the text at CODING->src_object into CODING->dst_object. |
7455 | CODING->src_object is a buffer or a string. | |
7456 | CODING->dst_object is a buffer or nil. | |
7457 | ||
7458 | If CODING->src_object is a buffer, it must be the current buffer. | |
7459 | In this case, if CODING->src_pos is positive, it is a position of | |
7460 | the source text in the buffer, otherwise. the source text is in the | |
7461 | gap area of the buffer, and coding->src_pos specifies the offset of | |
7462 | the text from GPT (which must be the same as PT). If this is the | |
7463 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7464 | negative and CODING should not have `pre-write-conversion'. | |
7465 | ||
7466 | If CODING->src_object is a string, CODING should not have | |
7467 | `pre-write-conversion'. | |
7468 | ||
7469 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
7470 | the current point of that buffer. | |
7471 | ||
7472 | If CODING->dst_object is nil, the encoded data is placed at the | |
7473 | memory area specified by CODING->destination. */ | |
7474 | ||
7475 | static int | |
7476 | encode_coding (coding) | |
4ed46869 | 7477 | struct coding_system *coding; |
4ed46869 | 7478 | { |
df7492f9 | 7479 | Lisp_Object attrs; |
7d64c6ad | 7480 | Lisp_Object translation_table; |
09ee6fdd | 7481 | int max_lookup; |
9861e777 | 7482 | |
df7492f9 | 7483 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
7484 | if (coding->encoder == encode_coding_raw_text) |
7485 | translation_table = Qnil, max_lookup = 0; | |
7486 | else | |
7487 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 7488 | |
df7492f9 | 7489 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 7490 | { |
df7492f9 KH |
7491 | set_buffer_internal (XBUFFER (coding->dst_object)); |
7492 | coding->dst_multibyte | |
7493 | = ! NILP (current_buffer->enable_multibyte_characters); | |
8844fa83 | 7494 | } |
4ed46869 | 7495 | |
b73bfc1c | 7496 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 7497 | coding->produced = coding->produced_char = 0; |
065e3595 | 7498 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 7499 | coding->errors = 0; |
b73bfc1c | 7500 | |
df7492f9 | 7501 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 7502 | |
df7492f9 KH |
7503 | do { |
7504 | coding_set_source (coding); | |
09ee6fdd | 7505 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
7506 | coding_set_destination (coding); |
7507 | (*(coding->encoder)) (coding); | |
7508 | } while (coding->consumed_char < coding->src_chars); | |
7509 | ||
284201e4 | 7510 | if (BUFFERP (coding->dst_object) && coding->produced_char > 0) |
df7492f9 KH |
7511 | insert_from_gap (coding->produced_char, coding->produced); |
7512 | ||
7513 | return (coding->result); | |
ec6d2bb8 KH |
7514 | } |
7515 | ||
fb88bf2d | 7516 | |
24a73b0a KH |
7517 | /* Name (or base name) of work buffer for code conversion. */ |
7518 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 7519 | |
24a73b0a KH |
7520 | /* A working buffer used by the top level conversion. Once it is |
7521 | created, it is never destroyed. It has the name | |
7522 | Vcode_conversion_workbuf_name. The other working buffers are | |
7523 | destroyed after the use is finished, and their names are modified | |
7524 | versions of Vcode_conversion_workbuf_name. */ | |
7525 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 7526 | |
24a73b0a KH |
7527 | /* 1 iff Vcode_conversion_reused_workbuf is already in use. */ |
7528 | static int reused_workbuf_in_use; | |
4ed46869 | 7529 | |
24a73b0a KH |
7530 | |
7531 | /* Return a working buffer of code convesion. MULTIBYTE specifies the | |
7532 | multibyteness of returning buffer. */ | |
b73bfc1c | 7533 | |
f6cbaf43 | 7534 | static Lisp_Object |
24a73b0a | 7535 | make_conversion_work_buffer (multibyte) |
f6cbaf43 | 7536 | int multibyte; |
df7492f9 | 7537 | { |
24a73b0a KH |
7538 | Lisp_Object name, workbuf; |
7539 | struct buffer *current; | |
4ed46869 | 7540 | |
24a73b0a | 7541 | if (reused_workbuf_in_use++) |
065e3595 KH |
7542 | { |
7543 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
7544 | workbuf = Fget_buffer_create (name); | |
7545 | } | |
df7492f9 | 7546 | else |
065e3595 | 7547 | { |
159bd5a2 | 7548 | if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf))) |
a993c7a1 KH |
7549 | Vcode_conversion_reused_workbuf |
7550 | = Fget_buffer_create (Vcode_conversion_workbuf_name); | |
7551 | workbuf = Vcode_conversion_reused_workbuf; | |
065e3595 | 7552 | } |
24a73b0a KH |
7553 | current = current_buffer; |
7554 | set_buffer_internal (XBUFFER (workbuf)); | |
df36ff1f CY |
7555 | /* We can't allow modification hooks to run in the work buffer. For |
7556 | instance, directory_files_internal assumes that file decoding | |
7557 | doesn't compile new regexps. */ | |
7558 | Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt); | |
3ed051d4 | 7559 | Ferase_buffer (); |
df7492f9 | 7560 | current_buffer->undo_list = Qt; |
24a73b0a | 7561 | current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil; |
df7492f9 | 7562 | set_buffer_internal (current); |
24a73b0a | 7563 | return workbuf; |
df7492f9 | 7564 | } |
d46c5b12 | 7565 | |
24a73b0a | 7566 | |
4776e638 | 7567 | static Lisp_Object |
24a73b0a KH |
7568 | code_conversion_restore (arg) |
7569 | Lisp_Object arg; | |
4776e638 | 7570 | { |
24a73b0a | 7571 | Lisp_Object current, workbuf; |
948bdcf3 | 7572 | struct gcpro gcpro1; |
24a73b0a | 7573 | |
948bdcf3 | 7574 | GCPRO1 (arg); |
24a73b0a KH |
7575 | current = XCAR (arg); |
7576 | workbuf = XCDR (arg); | |
7577 | if (! NILP (workbuf)) | |
7578 | { | |
7579 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
7580 | reused_workbuf_in_use = 0; | |
7581 | else if (! NILP (Fbuffer_live_p (workbuf))) | |
7582 | Fkill_buffer (workbuf); | |
7583 | } | |
7584 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 7585 | UNGCPRO; |
4776e638 KH |
7586 | return Qnil; |
7587 | } | |
b73bfc1c | 7588 | |
24a73b0a KH |
7589 | Lisp_Object |
7590 | code_conversion_save (with_work_buf, multibyte) | |
4776e638 | 7591 | int with_work_buf, multibyte; |
df7492f9 | 7592 | { |
24a73b0a | 7593 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 7594 | |
4776e638 | 7595 | if (with_work_buf) |
24a73b0a KH |
7596 | workbuf = make_conversion_work_buffer (multibyte); |
7597 | record_unwind_protect (code_conversion_restore, | |
7598 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 7599 | return workbuf; |
df7492f9 | 7600 | } |
d46c5b12 | 7601 | |
df7492f9 KH |
7602 | int |
7603 | decode_coding_gap (coding, chars, bytes) | |
7604 | struct coding_system *coding; | |
7605 | EMACS_INT chars, bytes; | |
7606 | { | |
7607 | int count = specpdl_ptr - specpdl; | |
5e5c78be | 7608 | Lisp_Object attrs; |
fb88bf2d | 7609 | |
24a73b0a | 7610 | code_conversion_save (0, 0); |
ec6d2bb8 | 7611 | |
24a73b0a | 7612 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7613 | coding->src_chars = chars; |
7614 | coding->src_bytes = bytes; | |
7615 | coding->src_pos = -chars; | |
7616 | coding->src_pos_byte = -bytes; | |
7617 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 7618 | coding->dst_object = coding->src_object; |
df7492f9 KH |
7619 | coding->dst_pos = PT; |
7620 | coding->dst_pos_byte = PT_BYTE; | |
71c81426 | 7621 | coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters); |
4ed46869 | 7622 | |
df7492f9 KH |
7623 | if (CODING_REQUIRE_DETECTION (coding)) |
7624 | detect_coding (coding); | |
8f924df7 | 7625 | |
9286b333 | 7626 | coding->mode |= CODING_MODE_LAST_BLOCK; |
287c57d7 | 7627 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 | 7628 | decode_coding (coding); |
287c57d7 | 7629 | current_buffer->text->inhibit_shrinking = 0; |
d46c5b12 | 7630 | |
5e5c78be KH |
7631 | attrs = CODING_ID_ATTRS (coding->id); |
7632 | if (! NILP (CODING_ATTR_POST_READ (attrs))) | |
b73bfc1c | 7633 | { |
5e5c78be KH |
7634 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
7635 | Lisp_Object val; | |
7636 | ||
7637 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
7638 | val = call1 (CODING_ATTR_POST_READ (attrs), |
7639 | make_number (coding->produced_char)); | |
5e5c78be KH |
7640 | CHECK_NATNUM (val); |
7641 | coding->produced_char += Z - prev_Z; | |
7642 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 7643 | } |
4ed46869 | 7644 | |
df7492f9 | 7645 | unbind_to (count, Qnil); |
b73bfc1c KH |
7646 | return coding->result; |
7647 | } | |
52d41803 | 7648 | |
4ed46869 | 7649 | int |
df7492f9 | 7650 | encode_coding_gap (coding, chars, bytes) |
4ed46869 | 7651 | struct coding_system *coding; |
df7492f9 | 7652 | EMACS_INT chars, bytes; |
4ed46869 | 7653 | { |
df7492f9 | 7654 | int count = specpdl_ptr - specpdl; |
4ed46869 | 7655 | |
24a73b0a | 7656 | code_conversion_save (0, 0); |
4ed46869 | 7657 | |
24a73b0a | 7658 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7659 | coding->src_chars = chars; |
7660 | coding->src_bytes = bytes; | |
7661 | coding->src_pos = -chars; | |
7662 | coding->src_pos_byte = -bytes; | |
7663 | coding->src_multibyte = chars < bytes; | |
7664 | coding->dst_object = coding->src_object; | |
7665 | coding->dst_pos = PT; | |
7666 | coding->dst_pos_byte = PT_BYTE; | |
4ed46869 | 7667 | |
df7492f9 | 7668 | encode_coding (coding); |
b73bfc1c | 7669 | |
df7492f9 KH |
7670 | unbind_to (count, Qnil); |
7671 | return coding->result; | |
7672 | } | |
4ed46869 | 7673 | |
d46c5b12 | 7674 | |
df7492f9 KH |
7675 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
7676 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 7677 | |
df7492f9 | 7678 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 7679 | |
df7492f9 KH |
7680 | If it is a buffer, the text is at point of the buffer. FROM and TO |
7681 | are positions in the buffer. | |
b73bfc1c | 7682 | |
df7492f9 KH |
7683 | If it is a string, the text is at the beginning of the string. |
7684 | FROM and TO are indices to the string. | |
4ed46869 | 7685 | |
df7492f9 KH |
7686 | If it is nil, the text is at coding->source. FROM and TO are |
7687 | indices to coding->source. | |
bb10be8b | 7688 | |
df7492f9 | 7689 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 7690 | |
df7492f9 KH |
7691 | If it is a buffer, the decoded text is inserted at point of the |
7692 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
7693 | is deleted. | |
4ed46869 | 7694 | |
df7492f9 KH |
7695 | If it is Qt, a string is made from the decoded text, and |
7696 | set in CODING->dst_object. | |
d46c5b12 | 7697 | |
df7492f9 | 7698 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 7699 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
7700 | CODING->destination by xmalloc. If the decoded text is longer than |
7701 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
7702 | */ | |
d46c5b12 | 7703 | |
df7492f9 KH |
7704 | void |
7705 | decode_coding_object (coding, src_object, from, from_byte, to, to_byte, | |
7706 | dst_object) | |
d46c5b12 | 7707 | struct coding_system *coding; |
df7492f9 KH |
7708 | Lisp_Object src_object; |
7709 | EMACS_INT from, from_byte, to, to_byte; | |
7710 | Lisp_Object dst_object; | |
d46c5b12 | 7711 | { |
df7492f9 KH |
7712 | int count = specpdl_ptr - specpdl; |
7713 | unsigned char *destination; | |
7714 | EMACS_INT dst_bytes; | |
7715 | EMACS_INT chars = to - from; | |
7716 | EMACS_INT bytes = to_byte - from_byte; | |
7717 | Lisp_Object attrs; | |
4776e638 | 7718 | int saved_pt = -1, saved_pt_byte; |
64cedb0c | 7719 | int need_marker_adjustment = 0; |
b3bfad50 | 7720 | Lisp_Object old_deactivate_mark; |
d46c5b12 | 7721 | |
b3bfad50 | 7722 | old_deactivate_mark = Vdeactivate_mark; |
93dec019 | 7723 | |
df7492f9 | 7724 | if (NILP (dst_object)) |
d46c5b12 | 7725 | { |
df7492f9 KH |
7726 | destination = coding->destination; |
7727 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 7728 | } |
93dec019 | 7729 | |
df7492f9 KH |
7730 | coding->src_object = src_object; |
7731 | coding->src_chars = chars; | |
7732 | coding->src_bytes = bytes; | |
7733 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 7734 | |
df7492f9 | 7735 | if (STRINGP (src_object)) |
d46c5b12 | 7736 | { |
df7492f9 KH |
7737 | coding->src_pos = from; |
7738 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7739 | } |
df7492f9 | 7740 | else if (BUFFERP (src_object)) |
88993dfd | 7741 | { |
df7492f9 KH |
7742 | set_buffer_internal (XBUFFER (src_object)); |
7743 | if (from != GPT) | |
7744 | move_gap_both (from, from_byte); | |
7745 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 7746 | { |
64cedb0c KH |
7747 | struct Lisp_Marker *tail; |
7748 | ||
7749 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7750 | { | |
7751 | tail->need_adjustment | |
7752 | = tail->charpos == (tail->insertion_type ? from : to); | |
7753 | need_marker_adjustment |= tail->need_adjustment; | |
7754 | } | |
4776e638 | 7755 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 | 7756 | TEMP_SET_PT_BOTH (from, from_byte); |
f4a3cc44 | 7757 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 KH |
7758 | del_range_both (from, from_byte, to, to_byte, 1); |
7759 | coding->src_pos = -chars; | |
7760 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 7761 | } |
df7492f9 | 7762 | else |
fb88bf2d | 7763 | { |
df7492f9 KH |
7764 | coding->src_pos = from; |
7765 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 7766 | } |
88993dfd KH |
7767 | } |
7768 | ||
df7492f9 KH |
7769 | if (CODING_REQUIRE_DETECTION (coding)) |
7770 | detect_coding (coding); | |
7771 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 7772 | |
2cb26057 KH |
7773 | if (EQ (dst_object, Qt) |
7774 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
7775 | && NILP (dst_object))) | |
b73bfc1c | 7776 | { |
a1567c45 SM |
7777 | coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding); |
7778 | coding->dst_object = code_conversion_save (1, coding->dst_multibyte); | |
df7492f9 KH |
7779 | coding->dst_pos = BEG; |
7780 | coding->dst_pos_byte = BEG_BYTE; | |
b73bfc1c | 7781 | } |
df7492f9 | 7782 | else if (BUFFERP (dst_object)) |
d46c5b12 | 7783 | { |
24a73b0a | 7784 | code_conversion_save (0, 0); |
df7492f9 KH |
7785 | coding->dst_object = dst_object; |
7786 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
7787 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
7788 | coding->dst_multibyte | |
7789 | = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters); | |
d46c5b12 KH |
7790 | } |
7791 | else | |
7792 | { | |
24a73b0a | 7793 | code_conversion_save (0, 0); |
df7492f9 | 7794 | coding->dst_object = Qnil; |
0154725e SM |
7795 | /* Most callers presume this will return a multibyte result, and they |
7796 | won't use `binary' or `raw-text' anyway, so let's not worry about | |
7797 | CODING_FOR_UNIBYTE. */ | |
bb555731 | 7798 | coding->dst_multibyte = 1; |
d46c5b12 KH |
7799 | } |
7800 | ||
df7492f9 | 7801 | decode_coding (coding); |
fa46990e | 7802 | |
df7492f9 KH |
7803 | if (BUFFERP (coding->dst_object)) |
7804 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 7805 | |
df7492f9 | 7806 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 7807 | { |
b3bfad50 | 7808 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
df7492f9 | 7809 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
2b4f9037 | 7810 | Lisp_Object val; |
d46c5b12 | 7811 | |
c0cc7f7f | 7812 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
b3bfad50 KH |
7813 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7814 | old_deactivate_mark); | |
d4850d67 KH |
7815 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
7816 | make_number (coding->produced_char)); | |
df7492f9 KH |
7817 | UNGCPRO; |
7818 | CHECK_NATNUM (val); | |
7819 | coding->produced_char += Z - prev_Z; | |
7820 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 7821 | } |
de79a6a5 | 7822 | |
df7492f9 | 7823 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 7824 | { |
df7492f9 KH |
7825 | coding->dst_object = Fbuffer_string (); |
7826 | } | |
7827 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
7828 | { | |
7829 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
7830 | if (dst_bytes < coding->produced) | |
7831 | { | |
b3bfad50 | 7832 | destination = xrealloc (destination, coding->produced); |
df7492f9 KH |
7833 | if (! destination) |
7834 | { | |
065e3595 KH |
7835 | record_conversion_result (coding, |
7836 | CODING_RESULT_INSUFFICIENT_DST); | |
df7492f9 KH |
7837 | unbind_to (count, Qnil); |
7838 | return; | |
7839 | } | |
7840 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) | |
7841 | move_gap_both (BEGV, BEGV_BYTE); | |
7842 | bcopy (BEGV_ADDR, destination, coding->produced); | |
7843 | coding->destination = destination; | |
d46c5b12 | 7844 | } |
ec6d2bb8 | 7845 | } |
b73bfc1c | 7846 | |
4776e638 KH |
7847 | if (saved_pt >= 0) |
7848 | { | |
7849 | /* This is the case of: | |
7850 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7851 | As we have moved PT while replacing the original buffer | |
7852 | contents, we must recover it now. */ | |
7853 | set_buffer_internal (XBUFFER (src_object)); | |
f4a3cc44 | 7854 | current_buffer->text->inhibit_shrinking = 0; |
4776e638 KH |
7855 | if (saved_pt < from) |
7856 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7857 | else if (saved_pt < from + chars) | |
7858 | TEMP_SET_PT_BOTH (from, from_byte); | |
7859 | else if (! NILP (current_buffer->enable_multibyte_characters)) | |
7860 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), | |
7861 | saved_pt_byte + (coding->produced - bytes)); | |
7862 | else | |
7863 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
7864 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7865 | |
7866 | if (need_marker_adjustment) | |
7867 | { | |
7868 | struct Lisp_Marker *tail; | |
7869 | ||
7870 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7871 | if (tail->need_adjustment) | |
7872 | { | |
7873 | tail->need_adjustment = 0; | |
7874 | if (tail->insertion_type) | |
7875 | { | |
7876 | tail->bytepos = from_byte; | |
7877 | tail->charpos = from; | |
7878 | } | |
7879 | else | |
7880 | { | |
7881 | tail->bytepos = from_byte + coding->produced; | |
7882 | tail->charpos | |
7883 | = (NILP (current_buffer->enable_multibyte_characters) | |
7884 | ? tail->bytepos : from + coding->produced_char); | |
7885 | } | |
7886 | } | |
7887 | } | |
d46c5b12 | 7888 | } |
4776e638 | 7889 | |
b3bfad50 | 7890 | Vdeactivate_mark = old_deactivate_mark; |
065e3595 | 7891 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
7892 | } |
7893 | ||
d46c5b12 | 7894 | |
df7492f9 KH |
7895 | void |
7896 | encode_coding_object (coding, src_object, from, from_byte, to, to_byte, | |
7897 | dst_object) | |
d46c5b12 | 7898 | struct coding_system *coding; |
df7492f9 KH |
7899 | Lisp_Object src_object; |
7900 | EMACS_INT from, from_byte, to, to_byte; | |
7901 | Lisp_Object dst_object; | |
d46c5b12 | 7902 | { |
b73bfc1c | 7903 | int count = specpdl_ptr - specpdl; |
df7492f9 KH |
7904 | EMACS_INT chars = to - from; |
7905 | EMACS_INT bytes = to_byte - from_byte; | |
7906 | Lisp_Object attrs; | |
4776e638 | 7907 | int saved_pt = -1, saved_pt_byte; |
64cedb0c | 7908 | int need_marker_adjustment = 0; |
c02d943b | 7909 | int kill_src_buffer = 0; |
b3bfad50 | 7910 | Lisp_Object old_deactivate_mark; |
df7492f9 | 7911 | |
b3bfad50 | 7912 | old_deactivate_mark = Vdeactivate_mark; |
df7492f9 KH |
7913 | |
7914 | coding->src_object = src_object; | |
7915 | coding->src_chars = chars; | |
7916 | coding->src_bytes = bytes; | |
7917 | coding->src_multibyte = chars < bytes; | |
7918 | ||
7919 | attrs = CODING_ID_ATTRS (coding->id); | |
7920 | ||
64cedb0c KH |
7921 | if (EQ (src_object, dst_object)) |
7922 | { | |
7923 | struct Lisp_Marker *tail; | |
7924 | ||
7925 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7926 | { | |
7927 | tail->need_adjustment | |
7928 | = tail->charpos == (tail->insertion_type ? from : to); | |
7929 | need_marker_adjustment |= tail->need_adjustment; | |
7930 | } | |
7931 | } | |
7932 | ||
df7492f9 | 7933 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) |
6bac5b12 | 7934 | { |
24a73b0a | 7935 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
7936 | set_buffer_internal (XBUFFER (coding->src_object)); |
7937 | if (STRINGP (src_object)) | |
7938 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
7939 | else if (BUFFERP (src_object)) | |
7940 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
7941 | else | |
7942 | insert_1_both (coding->source + from, chars, bytes, 0, 0, 0); | |
d46c5b12 | 7943 | |
df7492f9 KH |
7944 | if (EQ (src_object, dst_object)) |
7945 | { | |
7946 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 7947 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
7948 | del_range_both (from, from_byte, to, to_byte, 1); |
7949 | set_buffer_internal (XBUFFER (coding->src_object)); | |
7950 | } | |
7951 | ||
d4850d67 KH |
7952 | { |
7953 | Lisp_Object args[3]; | |
b3bfad50 | 7954 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d4850d67 | 7955 | |
b3bfad50 KH |
7956 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7957 | old_deactivate_mark); | |
d4850d67 KH |
7958 | args[0] = CODING_ATTR_PRE_WRITE (attrs); |
7959 | args[1] = make_number (BEG); | |
7960 | args[2] = make_number (Z); | |
7961 | safe_call (3, args); | |
b3bfad50 | 7962 | UNGCPRO; |
d4850d67 | 7963 | } |
c02d943b KH |
7964 | if (XBUFFER (coding->src_object) != current_buffer) |
7965 | kill_src_buffer = 1; | |
ac87bbef | 7966 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7967 | if (BEG != GPT) |
7968 | move_gap_both (BEG, BEG_BYTE); | |
7969 | coding->src_chars = Z - BEG; | |
7970 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
7971 | coding->src_pos = BEG; | |
7972 | coding->src_pos_byte = BEG_BYTE; | |
7973 | coding->src_multibyte = Z < Z_BYTE; | |
7974 | } | |
7975 | else if (STRINGP (src_object)) | |
d46c5b12 | 7976 | { |
24a73b0a | 7977 | code_conversion_save (0, 0); |
df7492f9 KH |
7978 | coding->src_pos = from; |
7979 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 7980 | } |
df7492f9 | 7981 | else if (BUFFERP (src_object)) |
b73bfc1c | 7982 | { |
24a73b0a | 7983 | code_conversion_save (0, 0); |
df7492f9 | 7984 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 7985 | if (EQ (src_object, dst_object)) |
d46c5b12 | 7986 | { |
4776e638 | 7987 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
7988 | coding->src_object = del_range_1 (from, to, 1, 1); |
7989 | coding->src_pos = 0; | |
7990 | coding->src_pos_byte = 0; | |
d46c5b12 | 7991 | } |
df7492f9 | 7992 | else |
d46c5b12 | 7993 | { |
ff0dacd7 KH |
7994 | if (from < GPT && to >= GPT) |
7995 | move_gap_both (from, from_byte); | |
df7492f9 KH |
7996 | coding->src_pos = from; |
7997 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7998 | } |
d46c5b12 | 7999 | } |
4776e638 | 8000 | else |
24a73b0a | 8001 | code_conversion_save (0, 0); |
d46c5b12 | 8002 | |
df7492f9 | 8003 | if (BUFFERP (dst_object)) |
88993dfd | 8004 | { |
df7492f9 | 8005 | coding->dst_object = dst_object; |
28f67a95 KH |
8006 | if (EQ (src_object, dst_object)) |
8007 | { | |
8008 | coding->dst_pos = from; | |
8009 | coding->dst_pos_byte = from_byte; | |
8010 | } | |
8011 | else | |
8012 | { | |
319a3947 KH |
8013 | struct buffer *current = current_buffer; |
8014 | ||
8015 | set_buffer_temp (XBUFFER (dst_object)); | |
8016 | coding->dst_pos = PT; | |
8017 | coding->dst_pos_byte = PT_BYTE; | |
8018 | move_gap_both (coding->dst_pos, coding->dst_pos_byte); | |
8019 | set_buffer_temp (current); | |
28f67a95 | 8020 | } |
df7492f9 KH |
8021 | coding->dst_multibyte |
8022 | = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters); | |
88993dfd | 8023 | } |
df7492f9 | 8024 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 8025 | { |
df7492f9 | 8026 | coding->dst_object = Qnil; |
df7492f9 | 8027 | coding->dst_bytes = coding->src_chars; |
ac87bbef KH |
8028 | if (coding->dst_bytes == 0) |
8029 | coding->dst_bytes = 1; | |
8030 | coding->destination = (unsigned char *) xmalloc (coding->dst_bytes); | |
df7492f9 | 8031 | coding->dst_multibyte = 0; |
d46c5b12 KH |
8032 | } |
8033 | else | |
8034 | { | |
df7492f9 KH |
8035 | coding->dst_object = Qnil; |
8036 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
8037 | } |
8038 | ||
df7492f9 | 8039 | encode_coding (coding); |
d46c5b12 | 8040 | |
df7492f9 | 8041 | if (EQ (dst_object, Qt)) |
d46c5b12 | 8042 | { |
df7492f9 KH |
8043 | if (BUFFERP (coding->dst_object)) |
8044 | coding->dst_object = Fbuffer_string (); | |
8045 | else | |
d46c5b12 | 8046 | { |
df7492f9 KH |
8047 | coding->dst_object |
8048 | = make_unibyte_string ((char *) coding->destination, | |
8049 | coding->produced); | |
8050 | xfree (coding->destination); | |
d46c5b12 | 8051 | } |
4ed46869 | 8052 | } |
d46c5b12 | 8053 | |
4776e638 KH |
8054 | if (saved_pt >= 0) |
8055 | { | |
8056 | /* This is the case of: | |
8057 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
8058 | As we have moved PT while replacing the original buffer | |
8059 | contents, we must recover it now. */ | |
8060 | set_buffer_internal (XBUFFER (src_object)); | |
8061 | if (saved_pt < from) | |
8062 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
8063 | else if (saved_pt < from + chars) | |
8064 | TEMP_SET_PT_BOTH (from, from_byte); | |
8065 | else if (! NILP (current_buffer->enable_multibyte_characters)) | |
8066 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), | |
8067 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 8068 | else |
4776e638 KH |
8069 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
8070 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
8071 | |
8072 | if (need_marker_adjustment) | |
8073 | { | |
8074 | struct Lisp_Marker *tail; | |
8075 | ||
8076 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
8077 | if (tail->need_adjustment) | |
8078 | { | |
8079 | tail->need_adjustment = 0; | |
8080 | if (tail->insertion_type) | |
8081 | { | |
8082 | tail->bytepos = from_byte; | |
8083 | tail->charpos = from; | |
8084 | } | |
8085 | else | |
8086 | { | |
8087 | tail->bytepos = from_byte + coding->produced; | |
8088 | tail->charpos | |
8089 | = (NILP (current_buffer->enable_multibyte_characters) | |
8090 | ? tail->bytepos : from + coding->produced_char); | |
8091 | } | |
8092 | } | |
8093 | } | |
4776e638 KH |
8094 | } |
8095 | ||
c02d943b KH |
8096 | if (kill_src_buffer) |
8097 | Fkill_buffer (coding->src_object); | |
b3bfad50 KH |
8098 | |
8099 | Vdeactivate_mark = old_deactivate_mark; | |
df7492f9 | 8100 | unbind_to (count, Qnil); |
b73bfc1c KH |
8101 | } |
8102 | ||
df7492f9 | 8103 | |
b73bfc1c | 8104 | Lisp_Object |
df7492f9 | 8105 | preferred_coding_system () |
b73bfc1c | 8106 | { |
df7492f9 | 8107 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 8108 | |
df7492f9 | 8109 | return CODING_ID_NAME (id); |
4ed46869 KH |
8110 | } |
8111 | ||
8112 | \f | |
8113 | #ifdef emacs | |
1397dc18 | 8114 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 8115 | |
4ed46869 | 8116 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 8117 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 8118 | See the documentation of `define-coding-system' for information |
48b0f3ae | 8119 | about coding-system objects. */) |
d4a1d553 JB |
8120 | (object) |
8121 | Lisp_Object object; | |
4ed46869 | 8122 | { |
d4a1d553 JB |
8123 | if (NILP (object) |
8124 | || CODING_SYSTEM_ID (object) >= 0) | |
44e8490d | 8125 | return Qt; |
d4a1d553 JB |
8126 | if (! SYMBOLP (object) |
8127 | || NILP (Fget (object, Qcoding_system_define_form))) | |
44e8490d KH |
8128 | return Qnil; |
8129 | return Qt; | |
4ed46869 KH |
8130 | } |
8131 | ||
9d991de8 RS |
8132 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
8133 | Sread_non_nil_coding_system, 1, 1, 0, | |
48b0f3ae PJ |
8134 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
8135 | (prompt) | |
4ed46869 KH |
8136 | Lisp_Object prompt; |
8137 | { | |
e0e989f6 | 8138 | Lisp_Object val; |
9d991de8 RS |
8139 | do |
8140 | { | |
4608c386 KH |
8141 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
8142 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 8143 | } |
8f924df7 | 8144 | while (SCHARS (val) == 0); |
e0e989f6 | 8145 | return (Fintern (val, Qnil)); |
4ed46869 KH |
8146 | } |
8147 | ||
9b787f3e | 8148 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae | 8149 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
c7183fb8 GM |
8150 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. |
8151 | Ignores case when completing coding systems (all Emacs coding systems | |
8152 | are lower-case). */) | |
48b0f3ae | 8153 | (prompt, default_coding_system) |
9b787f3e | 8154 | Lisp_Object prompt, default_coding_system; |
4ed46869 | 8155 | { |
f44d27ce | 8156 | Lisp_Object val; |
c7183fb8 GM |
8157 | int count = SPECPDL_INDEX (); |
8158 | ||
9b787f3e | 8159 | if (SYMBOLP (default_coding_system)) |
57d25e6f | 8160 | default_coding_system = SYMBOL_NAME (default_coding_system); |
c7183fb8 | 8161 | specbind (Qcompletion_ignore_case, Qt); |
4608c386 | 8162 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
8163 | Qt, Qnil, Qcoding_system_history, |
8164 | default_coding_system, Qnil); | |
c7183fb8 | 8165 | unbind_to (count, Qnil); |
8f924df7 | 8166 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
8167 | } |
8168 | ||
8169 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, | |
8170 | 1, 1, 0, | |
48b0f3ae | 8171 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
8172 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
8173 | It is valid if it is nil or a symbol defined as a coding system by the | |
8174 | function `define-coding-system'. */) | |
df7492f9 | 8175 | (coding_system) |
4ed46869 KH |
8176 | Lisp_Object coding_system; |
8177 | { | |
44e8490d KH |
8178 | Lisp_Object define_form; |
8179 | ||
8180 | define_form = Fget (coding_system, Qcoding_system_define_form); | |
8181 | if (! NILP (define_form)) | |
8182 | { | |
8183 | Fput (coding_system, Qcoding_system_define_form, Qnil); | |
8184 | safe_eval (define_form); | |
8185 | } | |
4ed46869 KH |
8186 | if (!NILP (Fcoding_system_p (coding_system))) |
8187 | return coding_system; | |
fcad4ec4 | 8188 | xsignal1 (Qcoding_system_error, coding_system); |
4ed46869 | 8189 | } |
df7492f9 | 8190 | |
3a73fa5d | 8191 | \f |
89528eb3 KH |
8192 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
8193 | HIGHEST is nonzero, return the coding system of the highest | |
8194 | priority among the detected coding systems. Otherwize return a | |
8195 | list of detected coding systems sorted by their priorities. If | |
8196 | MULTIBYTEP is nonzero, it is assumed that the bytes are in correct | |
8197 | multibyte form but contains only ASCII and eight-bit chars. | |
8198 | Otherwise, the bytes are raw bytes. | |
8199 | ||
8200 | CODING-SYSTEM controls the detection as below: | |
8201 | ||
8202 | If it is nil, detect both text-format and eol-format. If the | |
8203 | text-format part of CODING-SYSTEM is already specified | |
8204 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
8205 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
8206 | detect only text-format. */ | |
8207 | ||
d46c5b12 | 8208 | Lisp_Object |
24a73b0a KH |
8209 | detect_coding_system (src, src_chars, src_bytes, highest, multibytep, |
8210 | coding_system) | |
8f924df7 | 8211 | const unsigned char *src; |
13818c30 SM |
8212 | EMACS_INT src_chars, src_bytes; |
8213 | int highest; | |
0a28aafb | 8214 | int multibytep; |
df7492f9 | 8215 | Lisp_Object coding_system; |
4ed46869 | 8216 | { |
8f924df7 | 8217 | const unsigned char *src_end = src + src_bytes; |
df7492f9 | 8218 | Lisp_Object attrs, eol_type; |
4533845d | 8219 | Lisp_Object val = Qnil; |
df7492f9 | 8220 | struct coding_system coding; |
89528eb3 | 8221 | int id; |
ff0dacd7 | 8222 | struct coding_detection_info detect_info; |
24a73b0a | 8223 | enum coding_category base_category; |
2f3cbb32 | 8224 | int null_byte_found = 0, eight_bit_found = 0; |
b73bfc1c | 8225 | |
df7492f9 KH |
8226 | if (NILP (coding_system)) |
8227 | coding_system = Qundecided; | |
8228 | setup_coding_system (coding_system, &coding); | |
8229 | attrs = CODING_ID_ATTRS (coding.id); | |
8230 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 8231 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 8232 | |
df7492f9 | 8233 | coding.source = src; |
24a73b0a | 8234 | coding.src_chars = src_chars; |
df7492f9 KH |
8235 | coding.src_bytes = src_bytes; |
8236 | coding.src_multibyte = multibytep; | |
8237 | coding.consumed = 0; | |
89528eb3 | 8238 | coding.mode |= CODING_MODE_LAST_BLOCK; |
c0e16b14 | 8239 | coding.head_ascii = 0; |
d46c5b12 | 8240 | |
ff0dacd7 | 8241 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 8242 | |
89528eb3 | 8243 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
8244 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
8245 | if (base_category == coding_category_undecided) | |
4ed46869 | 8246 | { |
ff0dacd7 KH |
8247 | enum coding_category category; |
8248 | struct coding_system *this; | |
8249 | int c, i; | |
88993dfd | 8250 | |
24a73b0a | 8251 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
2f3cbb32 | 8252 | for (; src < src_end; src++) |
4ed46869 | 8253 | { |
df7492f9 | 8254 | c = *src; |
6cb21a4f | 8255 | if (c & 0x80) |
6cb21a4f | 8256 | { |
2f3cbb32 | 8257 | eight_bit_found = 1; |
2f3cbb32 KH |
8258 | if (null_byte_found) |
8259 | break; | |
8260 | } | |
c0e16b14 | 8261 | else if (c < 0x20) |
2f3cbb32 KH |
8262 | { |
8263 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
8264 | && ! inhibit_iso_escape_detection | |
8265 | && ! detect_info.checked) | |
6cb21a4f | 8266 | { |
2f3cbb32 KH |
8267 | if (detect_coding_iso_2022 (&coding, &detect_info)) |
8268 | { | |
8269 | /* We have scanned the whole data. */ | |
8270 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
8271 | { |
8272 | /* We didn't find an 8-bit code. We may | |
8273 | have found a null-byte, but it's very | |
8274 | rare that a binary file confirm to | |
8275 | ISO-2022. */ | |
8276 | src = src_end; | |
8277 | coding.head_ascii = src - coding.source; | |
8278 | } | |
8279 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
8280 | break; |
8281 | } | |
8282 | } | |
97b1b294 | 8283 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
8284 | { |
8285 | null_byte_found = 1; | |
8286 | if (eight_bit_found) | |
8287 | break; | |
6cb21a4f | 8288 | } |
c006c0c8 KH |
8289 | if (! eight_bit_found) |
8290 | coding.head_ascii++; | |
6cb21a4f | 8291 | } |
c006c0c8 | 8292 | else if (! eight_bit_found) |
c0e16b14 | 8293 | coding.head_ascii++; |
4ed46869 | 8294 | } |
88993dfd | 8295 | |
2f3cbb32 KH |
8296 | if (null_byte_found || eight_bit_found |
8297 | || coding.head_ascii < coding.src_bytes | |
6cb21a4f KH |
8298 | || detect_info.found) |
8299 | { | |
2f3cbb32 | 8300 | if (coding.head_ascii == coding.src_bytes) |
6cb21a4f KH |
8301 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ |
8302 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 8303 | { |
6cb21a4f | 8304 | category = coding_priorities[i]; |
c7266f4a | 8305 | this = coding_categories + category; |
6cb21a4f | 8306 | if (detect_info.found & (1 << category)) |
ff0dacd7 KH |
8307 | break; |
8308 | } | |
6cb21a4f | 8309 | else |
2f3cbb32 KH |
8310 | { |
8311 | if (null_byte_found) | |
8312 | { | |
8313 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; | |
8314 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
8315 | } | |
8316 | for (i = 0; i < coding_category_raw_text; i++) | |
8317 | { | |
8318 | category = coding_priorities[i]; | |
8319 | this = coding_categories + category; | |
6cb21a4f | 8320 | |
2f3cbb32 KH |
8321 | if (this->id < 0) |
8322 | { | |
8323 | /* No coding system of this category is defined. */ | |
8324 | detect_info.rejected |= (1 << category); | |
8325 | } | |
8326 | else if (category >= coding_category_raw_text) | |
8327 | continue; | |
8328 | else if (detect_info.checked & (1 << category)) | |
8329 | { | |
8330 | if (highest | |
8331 | && (detect_info.found & (1 << category))) | |
6cb21a4f | 8332 | break; |
2f3cbb32 KH |
8333 | } |
8334 | else if ((*(this->detector)) (&coding, &detect_info) | |
8335 | && highest | |
8336 | && (detect_info.found & (1 << category))) | |
8337 | { | |
8338 | if (category == coding_category_utf_16_auto) | |
8339 | { | |
8340 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8341 | category = coding_category_utf_16_le; | |
8342 | else | |
8343 | category = coding_category_utf_16_be; | |
8344 | } | |
8345 | break; | |
8346 | } | |
8347 | } | |
8348 | } | |
6cb21a4f | 8349 | } |
ec6d2bb8 | 8350 | |
4cddb209 KH |
8351 | if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY |
8352 | || null_byte_found) | |
ec6d2bb8 | 8353 | { |
ff0dacd7 | 8354 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
4cddb209 | 8355 | id = CODING_SYSTEM_ID (Qno_conversion); |
89528eb3 KH |
8356 | val = Fcons (make_number (id), Qnil); |
8357 | } | |
ff0dacd7 | 8358 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 8359 | { |
ff0dacd7 | 8360 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 KH |
8361 | id = coding_categories[coding_category_undecided].id; |
8362 | val = Fcons (make_number (id), Qnil); | |
8363 | } | |
8364 | else if (highest) | |
8365 | { | |
ff0dacd7 | 8366 | if (detect_info.found) |
ec6d2bb8 | 8367 | { |
ff0dacd7 KH |
8368 | detect_info.found = 1 << category; |
8369 | val = Fcons (make_number (this->id), Qnil); | |
8370 | } | |
8371 | else | |
8372 | for (i = 0; i < coding_category_raw_text; i++) | |
8373 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
8374 | { | |
8375 | detect_info.found = 1 << coding_priorities[i]; | |
8376 | id = coding_categories[coding_priorities[i]].id; | |
8377 | val = Fcons (make_number (id), Qnil); | |
8378 | break; | |
8379 | } | |
8380 | } | |
89528eb3 KH |
8381 | else |
8382 | { | |
ff0dacd7 KH |
8383 | int mask = detect_info.rejected | detect_info.found; |
8384 | int found = 0; | |
ec6d2bb8 | 8385 | |
89528eb3 | 8386 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
8387 | { |
8388 | category = coding_priorities[i]; | |
8389 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 8390 | { |
ff0dacd7 KH |
8391 | found |= 1 << category; |
8392 | id = coding_categories[category].id; | |
c7266f4a KH |
8393 | if (id >= 0) |
8394 | val = Fcons (make_number (id), val); | |
ff0dacd7 KH |
8395 | } |
8396 | } | |
8397 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
8398 | { | |
8399 | category = coding_priorities[i]; | |
8400 | if (detect_info.found & (1 << category)) | |
8401 | { | |
8402 | id = coding_categories[category].id; | |
8403 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 8404 | } |
ec6d2bb8 | 8405 | } |
ff0dacd7 | 8406 | detect_info.found |= found; |
ec6d2bb8 | 8407 | } |
ec6d2bb8 | 8408 | } |
a470d443 KH |
8409 | else if (base_category == coding_category_utf_8_auto) |
8410 | { | |
8411 | if (detect_coding_utf_8 (&coding, &detect_info)) | |
8412 | { | |
8413 | struct coding_system *this; | |
8414 | ||
8415 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
8416 | this = coding_categories + coding_category_utf_8_sig; | |
8417 | else | |
8418 | this = coding_categories + coding_category_utf_8_nosig; | |
8419 | val = Fcons (make_number (this->id), Qnil); | |
8420 | } | |
8421 | } | |
24a73b0a KH |
8422 | else if (base_category == coding_category_utf_16_auto) |
8423 | { | |
8424 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
8425 | { | |
24a73b0a KH |
8426 | struct coding_system *this; |
8427 | ||
8428 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8429 | this = coding_categories + coding_category_utf_16_le; | |
8430 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
8431 | this = coding_categories + coding_category_utf_16_be; | |
8432 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
8433 | this = coding_categories + coding_category_utf_16_be_nosig; | |
8434 | else | |
8435 | this = coding_categories + coding_category_utf_16_le_nosig; | |
8436 | val = Fcons (make_number (this->id), Qnil); | |
8437 | } | |
8438 | } | |
df7492f9 KH |
8439 | else |
8440 | { | |
ff0dacd7 | 8441 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
89528eb3 | 8442 | val = Fcons (make_number (coding.id), Qnil); |
4ed46869 | 8443 | } |
df7492f9 | 8444 | |
89528eb3 | 8445 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 8446 | { |
4533845d | 8447 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1; |
df7492f9 KH |
8448 | Lisp_Object tail; |
8449 | ||
89528eb3 KH |
8450 | if (VECTORP (eol_type)) |
8451 | { | |
ff0dacd7 | 8452 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
2f3cbb32 KH |
8453 | { |
8454 | if (null_byte_found) | |
8455 | normal_eol = EOL_SEEN_LF; | |
8456 | else | |
8457 | normal_eol = detect_eol (coding.source, src_bytes, | |
8458 | coding_category_raw_text); | |
8459 | } | |
ff0dacd7 KH |
8460 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
8461 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
8462 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
8463 | coding_category_utf_16_be); | |
ff0dacd7 KH |
8464 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
8465 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
8466 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
8467 | coding_category_utf_16_le); | |
8468 | } | |
8469 | else | |
8470 | { | |
8471 | if (EQ (eol_type, Qunix)) | |
8472 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
8473 | else if (EQ (eol_type, Qdos)) | |
8474 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
8475 | else | |
8476 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
8477 | } | |
8478 | ||
df7492f9 KH |
8479 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
8480 | { | |
89528eb3 | 8481 | enum coding_category category; |
df7492f9 | 8482 | int this_eol; |
89528eb3 KH |
8483 | |
8484 | id = XINT (XCAR (tail)); | |
8485 | attrs = CODING_ID_ATTRS (id); | |
8486 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8487 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
8488 | if (VECTORP (eol_type)) |
8489 | { | |
89528eb3 KH |
8490 | if (category == coding_category_utf_16_be |
8491 | || category == coding_category_utf_16_be_nosig) | |
8492 | this_eol = utf_16_be_eol; | |
8493 | else if (category == coding_category_utf_16_le | |
8494 | || category == coding_category_utf_16_le_nosig) | |
8495 | this_eol = utf_16_le_eol; | |
df7492f9 | 8496 | else |
89528eb3 KH |
8497 | this_eol = normal_eol; |
8498 | ||
df7492f9 KH |
8499 | if (this_eol == EOL_SEEN_LF) |
8500 | XSETCAR (tail, AREF (eol_type, 0)); | |
8501 | else if (this_eol == EOL_SEEN_CRLF) | |
8502 | XSETCAR (tail, AREF (eol_type, 1)); | |
8503 | else if (this_eol == EOL_SEEN_CR) | |
8504 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
8505 | else |
8506 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 8507 | } |
89528eb3 KH |
8508 | else |
8509 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
8510 | } |
8511 | } | |
ec6d2bb8 | 8512 | |
4533845d | 8513 | return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val); |
ec6d2bb8 KH |
8514 | } |
8515 | ||
ec6d2bb8 | 8516 | |
d46c5b12 KH |
8517 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
8518 | 2, 3, 0, | |
48b0f3ae PJ |
8519 | doc: /* Detect coding system of the text in the region between START and END. |
8520 | Return a list of possible coding systems ordered by priority. | |
ec6d2bb8 | 8521 | |
12e0131a | 8522 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8523 | characters as ESC), it returns a list of single element `undecided' |
8524 | or its subsidiary coding system according to a detected end-of-line | |
8525 | format. | |
ec6d2bb8 | 8526 | |
48b0f3ae PJ |
8527 | If optional argument HIGHEST is non-nil, return the coding system of |
8528 | highest priority. */) | |
8529 | (start, end, highest) | |
d46c5b12 KH |
8530 | Lisp_Object start, end, highest; |
8531 | { | |
8532 | int from, to; | |
8533 | int from_byte, to_byte; | |
ec6d2bb8 | 8534 | |
b7826503 PJ |
8535 | CHECK_NUMBER_COERCE_MARKER (start); |
8536 | CHECK_NUMBER_COERCE_MARKER (end); | |
ec6d2bb8 | 8537 | |
d46c5b12 KH |
8538 | validate_region (&start, &end); |
8539 | from = XINT (start), to = XINT (end); | |
8540 | from_byte = CHAR_TO_BYTE (from); | |
8541 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 8542 | |
d46c5b12 KH |
8543 | if (from < GPT && to >= GPT) |
8544 | move_gap_both (to, to_byte); | |
c210f766 | 8545 | |
d46c5b12 | 8546 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 8547 | to - from, to_byte - from_byte, |
0a28aafb KH |
8548 | !NILP (highest), |
8549 | !NILP (current_buffer | |
df7492f9 KH |
8550 | ->enable_multibyte_characters), |
8551 | Qnil); | |
ec6d2bb8 KH |
8552 | } |
8553 | ||
d46c5b12 KH |
8554 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
8555 | 1, 2, 0, | |
48b0f3ae PJ |
8556 | doc: /* Detect coding system of the text in STRING. |
8557 | Return a list of possible coding systems ordered by priority. | |
fb88bf2d | 8558 | |
12e0131a | 8559 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8560 | characters as ESC), it returns a list of single element `undecided' |
8561 | or its subsidiary coding system according to a detected end-of-line | |
8562 | format. | |
d46c5b12 | 8563 | |
48b0f3ae PJ |
8564 | If optional argument HIGHEST is non-nil, return the coding system of |
8565 | highest priority. */) | |
8566 | (string, highest) | |
d46c5b12 KH |
8567 | Lisp_Object string, highest; |
8568 | { | |
b7826503 | 8569 | CHECK_STRING (string); |
b73bfc1c | 8570 | |
24a73b0a KH |
8571 | return detect_coding_system (SDATA (string), |
8572 | SCHARS (string), SBYTES (string), | |
8f924df7 | 8573 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 8574 | Qnil); |
4ed46869 | 8575 | } |
4ed46869 | 8576 | |
b73bfc1c | 8577 | |
df7492f9 KH |
8578 | static INLINE int |
8579 | char_encodable_p (c, attrs) | |
8580 | int c; | |
8581 | Lisp_Object attrs; | |
05e6f5dc | 8582 | { |
df7492f9 | 8583 | Lisp_Object tail; |
df7492f9 | 8584 | struct charset *charset; |
7d64c6ad | 8585 | Lisp_Object translation_table; |
d46c5b12 | 8586 | |
7d64c6ad | 8587 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 8588 | if (! NILP (translation_table)) |
7d64c6ad | 8589 | c = translate_char (translation_table, c); |
df7492f9 KH |
8590 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
8591 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 8592 | { |
df7492f9 KH |
8593 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
8594 | if (CHAR_CHARSET_P (c, charset)) | |
8595 | break; | |
e133c8fa | 8596 | } |
df7492f9 | 8597 | return (! NILP (tail)); |
05e6f5dc | 8598 | } |
83fa074f | 8599 | |
fb88bf2d | 8600 | |
df7492f9 KH |
8601 | /* Return a list of coding systems that safely encode the text between |
8602 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
8603 | systems not to check. The returned list doesn't contain any such | |
48468dac | 8604 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 8605 | unibyte, return t. */ |
e077cc80 | 8606 | |
df7492f9 KH |
8607 | DEFUN ("find-coding-systems-region-internal", |
8608 | Ffind_coding_systems_region_internal, | |
8609 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
8610 | doc: /* Internal use only. */) | |
8611 | (start, end, exclude) | |
8612 | Lisp_Object start, end, exclude; | |
8613 | { | |
8614 | Lisp_Object coding_attrs_list, safe_codings; | |
8615 | EMACS_INT start_byte, end_byte; | |
7c78e542 | 8616 | const unsigned char *p, *pbeg, *pend; |
df7492f9 KH |
8617 | int c; |
8618 | Lisp_Object tail, elt; | |
d46c5b12 | 8619 | |
df7492f9 KH |
8620 | if (STRINGP (start)) |
8621 | { | |
8622 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 8623 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8624 | return Qt; |
8625 | start_byte = 0; | |
8f924df7 | 8626 | end_byte = SBYTES (start); |
df7492f9 KH |
8627 | } |
8628 | else | |
d46c5b12 | 8629 | { |
df7492f9 KH |
8630 | CHECK_NUMBER_COERCE_MARKER (start); |
8631 | CHECK_NUMBER_COERCE_MARKER (end); | |
8632 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
8633 | args_out_of_range (start, end); | |
8634 | if (NILP (current_buffer->enable_multibyte_characters)) | |
8635 | return Qt; | |
8636 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8637 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8638 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
8639 | return Qt; | |
d46c5b12 | 8640 | |
e1c23804 | 8641 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 8642 | { |
e1c23804 DL |
8643 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8644 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8645 | else |
e1c23804 | 8646 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
8647 | } |
8648 | } | |
8649 | ||
df7492f9 KH |
8650 | coding_attrs_list = Qnil; |
8651 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
8652 | if (NILP (exclude) | |
8653 | || NILP (Fmemq (XCAR (tail), exclude))) | |
8654 | { | |
8655 | Lisp_Object attrs; | |
d46c5b12 | 8656 | |
df7492f9 KH |
8657 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
8658 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)) | |
8659 | && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
7d64c6ad KH |
8660 | { |
8661 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 8662 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
8663 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
8664 | } | |
df7492f9 | 8665 | } |
d46c5b12 | 8666 | |
df7492f9 | 8667 | if (STRINGP (start)) |
8f924df7 | 8668 | p = pbeg = SDATA (start); |
df7492f9 KH |
8669 | else |
8670 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
8671 | pend = p + (end_byte - start_byte); | |
b843d1ae | 8672 | |
df7492f9 KH |
8673 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
8674 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 8675 | |
05e6f5dc | 8676 | while (p < pend) |
72d1a715 | 8677 | { |
df7492f9 KH |
8678 | if (ASCII_BYTE_P (*p)) |
8679 | p++; | |
72d1a715 RS |
8680 | else |
8681 | { | |
df7492f9 | 8682 | c = STRING_CHAR_ADVANCE (p); |
12410ef1 | 8683 | |
df7492f9 KH |
8684 | charset_map_loaded = 0; |
8685 | for (tail = coding_attrs_list; CONSP (tail);) | |
8686 | { | |
8687 | elt = XCAR (tail); | |
8688 | if (NILP (elt)) | |
8689 | tail = XCDR (tail); | |
8690 | else if (char_encodable_p (c, elt)) | |
8691 | tail = XCDR (tail); | |
8692 | else if (CONSP (XCDR (tail))) | |
8693 | { | |
8694 | XSETCAR (tail, XCAR (XCDR (tail))); | |
8695 | XSETCDR (tail, XCDR (XCDR (tail))); | |
8696 | } | |
8697 | else | |
8698 | { | |
8699 | XSETCAR (tail, Qnil); | |
8700 | tail = XCDR (tail); | |
8701 | } | |
8702 | } | |
8703 | if (charset_map_loaded) | |
8704 | { | |
8705 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
05e6f5dc | 8706 | |
df7492f9 | 8707 | if (STRINGP (start)) |
8f924df7 | 8708 | pbeg = SDATA (start); |
df7492f9 KH |
8709 | else |
8710 | pbeg = BYTE_POS_ADDR (start_byte); | |
8711 | p = pbeg + p_offset; | |
8712 | pend = pbeg + pend_offset; | |
8713 | } | |
8714 | } | |
ec6d2bb8 | 8715 | } |
fb88bf2d | 8716 | |
988b3759 | 8717 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
8718 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
8719 | if (! NILP (XCAR (tail))) | |
8720 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 8721 | |
05e6f5dc KH |
8722 | return safe_codings; |
8723 | } | |
4956c225 | 8724 | |
d46c5b12 | 8725 | |
8f924df7 KH |
8726 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
8727 | Sunencodable_char_position, 3, 5, 0, | |
8728 | doc: /* | |
8729 | Return position of first un-encodable character in a region. | |
d4a1d553 | 8730 | START and END specify the region and CODING-SYSTEM specifies the |
8f924df7 | 8731 | encoding to check. Return nil if CODING-SYSTEM does encode the region. |
d46c5b12 | 8732 | |
8f924df7 KH |
8733 | If optional 4th argument COUNT is non-nil, it specifies at most how |
8734 | many un-encodable characters to search. In this case, the value is a | |
8735 | list of positions. | |
d46c5b12 | 8736 | |
8f924df7 KH |
8737 | If optional 5th argument STRING is non-nil, it is a string to search |
8738 | for un-encodable characters. In that case, START and END are indexes | |
8739 | to the string. */) | |
8740 | (start, end, coding_system, count, string) | |
8741 | Lisp_Object start, end, coding_system, count, string; | |
8742 | { | |
8743 | int n; | |
8744 | struct coding_system coding; | |
7d64c6ad | 8745 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 KH |
8746 | Lisp_Object positions; |
8747 | int from, to; | |
8748 | const unsigned char *p, *stop, *pend; | |
8749 | int ascii_compatible; | |
fb88bf2d | 8750 | |
8f924df7 KH |
8751 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
8752 | attrs = CODING_ID_ATTRS (coding.id); | |
8753 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
8754 | return Qnil; | |
8755 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
8756 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 8757 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 8758 | |
8f924df7 KH |
8759 | if (NILP (string)) |
8760 | { | |
8761 | validate_region (&start, &end); | |
8762 | from = XINT (start); | |
8763 | to = XINT (end); | |
8764 | if (NILP (current_buffer->enable_multibyte_characters) | |
8765 | || (ascii_compatible | |
8766 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
8767 | return Qnil; | |
8768 | p = CHAR_POS_ADDR (from); | |
8769 | pend = CHAR_POS_ADDR (to); | |
8770 | if (from < GPT && to >= GPT) | |
8771 | stop = GPT_ADDR; | |
8772 | else | |
8773 | stop = pend; | |
8774 | } | |
8775 | else | |
8776 | { | |
8777 | CHECK_STRING (string); | |
8778 | CHECK_NATNUM (start); | |
8779 | CHECK_NATNUM (end); | |
8780 | from = XINT (start); | |
8781 | to = XINT (end); | |
8782 | if (from > to | |
8783 | || to > SCHARS (string)) | |
8784 | args_out_of_range_3 (string, start, end); | |
8785 | if (! STRING_MULTIBYTE (string)) | |
8786 | return Qnil; | |
8787 | p = SDATA (string) + string_char_to_byte (string, from); | |
8788 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
8789 | if (ascii_compatible && (to - from) == (pend - p)) | |
8790 | return Qnil; | |
8791 | } | |
f2558efd | 8792 | |
8f924df7 KH |
8793 | if (NILP (count)) |
8794 | n = 1; | |
8795 | else | |
b73bfc1c | 8796 | { |
8f924df7 KH |
8797 | CHECK_NATNUM (count); |
8798 | n = XINT (count); | |
b73bfc1c KH |
8799 | } |
8800 | ||
8f924df7 KH |
8801 | positions = Qnil; |
8802 | while (1) | |
d46c5b12 | 8803 | { |
8f924df7 | 8804 | int c; |
ec6d2bb8 | 8805 | |
8f924df7 KH |
8806 | if (ascii_compatible) |
8807 | while (p < stop && ASCII_BYTE_P (*p)) | |
8808 | p++, from++; | |
8809 | if (p >= stop) | |
0e79d667 | 8810 | { |
8f924df7 KH |
8811 | if (p >= pend) |
8812 | break; | |
8813 | stop = pend; | |
8814 | p = GAP_END_ADDR; | |
0e79d667 | 8815 | } |
ec6d2bb8 | 8816 | |
8f924df7 KH |
8817 | c = STRING_CHAR_ADVANCE (p); |
8818 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
8819 | && ! char_charset (translate_char (translation_table, c), |
8820 | charset_list, NULL)) | |
ec6d2bb8 | 8821 | { |
8f924df7 KH |
8822 | positions = Fcons (make_number (from), positions); |
8823 | n--; | |
8824 | if (n == 0) | |
8825 | break; | |
ec6d2bb8 KH |
8826 | } |
8827 | ||
8f924df7 KH |
8828 | from++; |
8829 | } | |
d46c5b12 | 8830 | |
8f924df7 KH |
8831 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
8832 | } | |
d46c5b12 | 8833 | |
d46c5b12 | 8834 | |
df7492f9 KH |
8835 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
8836 | Scheck_coding_systems_region, 3, 3, 0, | |
8837 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 8838 | |
df7492f9 KH |
8839 | START and END are buffer positions specifying the region. |
8840 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 8841 | |
df7492f9 | 8842 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
d4a1d553 | 8843 | CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the |
df7492f9 KH |
8844 | whole region, POS0, POS1, ... are buffer positions where non-encodable |
8845 | characters are found. | |
93dec019 | 8846 | |
df7492f9 KH |
8847 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
8848 | value is nil. | |
93dec019 | 8849 | |
df7492f9 KH |
8850 | START may be a string. In that case, check if the string is |
8851 | encodable, and the value contains indices to the string instead of | |
5704f39a KH |
8852 | buffer positions. END is ignored. |
8853 | ||
4c1958f4 | 8854 | If the current buffer (or START if it is a string) is unibyte, the value |
5704f39a | 8855 | is nil. */) |
df7492f9 KH |
8856 | (start, end, coding_system_list) |
8857 | Lisp_Object start, end, coding_system_list; | |
05e6f5dc | 8858 | { |
df7492f9 KH |
8859 | Lisp_Object list; |
8860 | EMACS_INT start_byte, end_byte; | |
8861 | int pos; | |
7c78e542 | 8862 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8863 | int c; |
7d64c6ad | 8864 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 8865 | |
05e6f5dc KH |
8866 | if (STRINGP (start)) |
8867 | { | |
df7492f9 | 8868 | if (!STRING_MULTIBYTE (start) |
4c1958f4 | 8869 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8870 | return Qnil; |
8871 | start_byte = 0; | |
8f924df7 | 8872 | end_byte = SBYTES (start); |
df7492f9 | 8873 | pos = 0; |
d46c5b12 | 8874 | } |
05e6f5dc | 8875 | else |
b73bfc1c | 8876 | { |
b7826503 PJ |
8877 | CHECK_NUMBER_COERCE_MARKER (start); |
8878 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
8879 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
8880 | args_out_of_range (start, end); | |
8881 | if (NILP (current_buffer->enable_multibyte_characters)) | |
df7492f9 KH |
8882 | return Qnil; |
8883 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8884 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8885 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
5704f39a | 8886 | return Qnil; |
df7492f9 | 8887 | |
e1c23804 | 8888 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 8889 | { |
e1c23804 DL |
8890 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8891 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8892 | else |
e1c23804 | 8893 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 8894 | } |
e1c23804 | 8895 | pos = XINT (start); |
b73bfc1c | 8896 | } |
7553d0e1 | 8897 | |
df7492f9 KH |
8898 | list = Qnil; |
8899 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 8900 | { |
df7492f9 | 8901 | elt = XCAR (tail); |
7d64c6ad | 8902 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
8903 | ASET (attrs, coding_attr_trans_tbl, |
8904 | get_translation_table (attrs, 1, NULL)); | |
7d64c6ad | 8905 | list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list); |
12410ef1 KH |
8906 | } |
8907 | ||
df7492f9 | 8908 | if (STRINGP (start)) |
8f924df7 | 8909 | p = pbeg = SDATA (start); |
72d1a715 | 8910 | else |
df7492f9 KH |
8911 | p = pbeg = BYTE_POS_ADDR (start_byte); |
8912 | pend = p + (end_byte - start_byte); | |
4ed46869 | 8913 | |
df7492f9 KH |
8914 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
8915 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 8916 | |
df7492f9 | 8917 | while (p < pend) |
d46c5b12 | 8918 | { |
df7492f9 KH |
8919 | if (ASCII_BYTE_P (*p)) |
8920 | p++; | |
e133c8fa | 8921 | else |
05e6f5dc | 8922 | { |
df7492f9 KH |
8923 | c = STRING_CHAR_ADVANCE (p); |
8924 | ||
8925 | charset_map_loaded = 0; | |
8926 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
8927 | { | |
8928 | elt = XCDR (XCAR (tail)); | |
8929 | if (! char_encodable_p (c, XCAR (elt))) | |
8930 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
8931 | } | |
8932 | if (charset_map_loaded) | |
8933 | { | |
8934 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
8935 | ||
8936 | if (STRINGP (start)) | |
8f924df7 | 8937 | pbeg = SDATA (start); |
df7492f9 KH |
8938 | else |
8939 | pbeg = BYTE_POS_ADDR (start_byte); | |
8940 | p = pbeg + p_offset; | |
8941 | pend = pbeg + pend_offset; | |
8942 | } | |
05e6f5dc | 8943 | } |
df7492f9 | 8944 | pos++; |
d46c5b12 | 8945 | } |
4ed46869 | 8946 | |
df7492f9 KH |
8947 | tail = list; |
8948 | list = Qnil; | |
8949 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 8950 | { |
df7492f9 KH |
8951 | elt = XCAR (tail); |
8952 | if (CONSP (XCDR (XCDR (elt)))) | |
8953 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
8954 | list); | |
ec6d2bb8 | 8955 | } |
2b4f9037 | 8956 | |
df7492f9 | 8957 | return list; |
d46c5b12 KH |
8958 | } |
8959 | ||
3fd9494b | 8960 | |
b73bfc1c | 8961 | Lisp_Object |
df7492f9 KH |
8962 | code_convert_region (start, end, coding_system, dst_object, encodep, norecord) |
8963 | Lisp_Object start, end, coding_system, dst_object; | |
8964 | int encodep, norecord; | |
4ed46869 | 8965 | { |
3a73fa5d | 8966 | struct coding_system coding; |
df7492f9 KH |
8967 | EMACS_INT from, from_byte, to, to_byte; |
8968 | Lisp_Object src_object; | |
4ed46869 | 8969 | |
b7826503 PJ |
8970 | CHECK_NUMBER_COERCE_MARKER (start); |
8971 | CHECK_NUMBER_COERCE_MARKER (end); | |
df7492f9 KH |
8972 | if (NILP (coding_system)) |
8973 | coding_system = Qno_conversion; | |
8974 | else | |
8975 | CHECK_CODING_SYSTEM (coding_system); | |
8976 | src_object = Fcurrent_buffer (); | |
8977 | if (NILP (dst_object)) | |
8978 | dst_object = src_object; | |
8979 | else if (! EQ (dst_object, Qt)) | |
8980 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 8981 | |
d46c5b12 KH |
8982 | validate_region (&start, &end); |
8983 | from = XFASTINT (start); | |
df7492f9 | 8984 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 8985 | to = XFASTINT (end); |
df7492f9 | 8986 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 8987 | |
df7492f9 KH |
8988 | setup_coding_system (coding_system, &coding); |
8989 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 8990 | |
df7492f9 KH |
8991 | if (encodep) |
8992 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8993 | dst_object); | |
8994 | else | |
8995 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8996 | dst_object); | |
8997 | if (! norecord) | |
8998 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 8999 | |
df7492f9 KH |
9000 | return (BUFFERP (dst_object) |
9001 | ? make_number (coding.produced_char) | |
9002 | : coding.dst_object); | |
4031e2bf | 9003 | } |
78108bcd | 9004 | |
4ed46869 | 9005 | |
4031e2bf | 9006 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 9007 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 9008 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
9009 | When called from a program, takes four arguments: |
9010 | START, END, CODING-SYSTEM, and DESTINATION. | |
9011 | START and END are buffer positions. | |
8844fa83 | 9012 | |
df7492f9 | 9013 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
2354b80b | 9014 | If nil, the region between START and END is replaced by the decoded text. |
1560f91a EZ |
9015 | If buffer, the decoded text is inserted in that buffer after point (point |
9016 | does not move). | |
446dcd75 | 9017 | In those cases, the length of the decoded text is returned. |
319a3947 | 9018 | If DESTINATION is t, the decoded text is returned. |
8844fa83 | 9019 | |
48b0f3ae PJ |
9020 | This function sets `last-coding-system-used' to the precise coding system |
9021 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 9022 | not fully specified.) */) |
df7492f9 KH |
9023 | (start, end, coding_system, destination) |
9024 | Lisp_Object start, end, coding_system, destination; | |
4031e2bf | 9025 | { |
df7492f9 | 9026 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 9027 | } |
8844fa83 | 9028 | |
3a73fa5d | 9029 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
9030 | 3, 4, "r\nzCoding system: ", |
9031 | doc: /* Encode the current region by specified coding system. | |
d4a1d553 JB |
9032 | When called from a program, takes four arguments: |
9033 | START, END, CODING-SYSTEM and DESTINATION. | |
9034 | START and END are buffer positions. | |
d46c5b12 | 9035 | |
df7492f9 KH |
9036 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
9037 | If nil, the region between START and END is replace by the encoded text. | |
1560f91a EZ |
9038 | If buffer, the encoded text is inserted in that buffer after point (point |
9039 | does not move). | |
446dcd75 | 9040 | In those cases, the length of the encoded text is returned. |
319a3947 | 9041 | If DESTINATION is t, the encoded text is returned. |
2391eaa4 | 9042 | |
48b0f3ae PJ |
9043 | This function sets `last-coding-system-used' to the precise coding system |
9044 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 9045 | not fully specified.) */) |
df7492f9 KH |
9046 | (start, end, coding_system, destination) |
9047 | Lisp_Object start, end, coding_system, destination; | |
3a73fa5d | 9048 | { |
df7492f9 | 9049 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
9050 | } |
9051 | ||
9052 | Lisp_Object | |
df7492f9 KH |
9053 | code_convert_string (string, coding_system, dst_object, |
9054 | encodep, nocopy, norecord) | |
9055 | Lisp_Object string, coding_system, dst_object; | |
9056 | int encodep, nocopy, norecord; | |
b73bfc1c | 9057 | { |
4031e2bf | 9058 | struct coding_system coding; |
df7492f9 | 9059 | EMACS_INT chars, bytes; |
ec6d2bb8 | 9060 | |
b7826503 | 9061 | CHECK_STRING (string); |
d46c5b12 | 9062 | if (NILP (coding_system)) |
4956c225 | 9063 | { |
df7492f9 KH |
9064 | if (! norecord) |
9065 | Vlast_coding_system_used = Qno_conversion; | |
9066 | if (NILP (dst_object)) | |
9067 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 9068 | } |
b73bfc1c | 9069 | |
df7492f9 KH |
9070 | if (NILP (coding_system)) |
9071 | coding_system = Qno_conversion; | |
9072 | else | |
9073 | CHECK_CODING_SYSTEM (coding_system); | |
9074 | if (NILP (dst_object)) | |
9075 | dst_object = Qt; | |
9076 | else if (! EQ (dst_object, Qt)) | |
9077 | CHECK_BUFFER (dst_object); | |
73be902c | 9078 | |
df7492f9 | 9079 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 9080 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
9081 | chars = SCHARS (string); |
9082 | bytes = SBYTES (string); | |
df7492f9 KH |
9083 | if (encodep) |
9084 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
9085 | else | |
9086 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
9087 | if (! norecord) | |
9088 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 9089 | |
df7492f9 KH |
9090 | return (BUFFERP (dst_object) |
9091 | ? make_number (coding.produced_char) | |
9092 | : coding.dst_object); | |
4ed46869 | 9093 | } |
73be902c | 9094 | |
b73bfc1c | 9095 | |
ecec61c1 | 9096 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 9097 | Do not set Vlast_coding_system_used. |
4ed46869 | 9098 | |
ec6d2bb8 KH |
9099 | This function is called only from macros DECODE_FILE and |
9100 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 9101 | |
ecec61c1 KH |
9102 | Lisp_Object |
9103 | code_convert_string_norecord (string, coding_system, encodep) | |
9104 | Lisp_Object string, coding_system; | |
9105 | int encodep; | |
4ed46869 | 9106 | { |
0be8721c | 9107 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
9108 | } |
9109 | ||
4ed46869 | 9110 | |
df7492f9 KH |
9111 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
9112 | 2, 4, 0, | |
9113 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
9114 | ||
9115 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
9116 | if the decoding operation is trivial. | |
ecec61c1 | 9117 | |
d4a1d553 | 9118 | Optional fourth arg BUFFER non-nil means that the decoded text is |
1560f91a EZ |
9119 | inserted in that buffer after point (point does not move). In this |
9120 | case, the return value is the length of the decoded text. | |
ecec61c1 | 9121 | |
df7492f9 KH |
9122 | This function sets `last-coding-system-used' to the precise coding system |
9123 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
d4a1d553 | 9124 | not fully specified.) */) |
df7492f9 KH |
9125 | (string, coding_system, nocopy, buffer) |
9126 | Lisp_Object string, coding_system, nocopy, buffer; | |
4ed46869 | 9127 | { |
df7492f9 KH |
9128 | return code_convert_string (string, coding_system, buffer, |
9129 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
9130 | } |
9131 | ||
df7492f9 KH |
9132 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
9133 | 2, 4, 0, | |
9134 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
9135 | ||
9136 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
9137 | itself if the encoding operation is trivial. | |
9138 | ||
d4a1d553 | 9139 | Optional fourth arg BUFFER non-nil means that the encoded text is |
1560f91a EZ |
9140 | inserted in that buffer after point (point does not move). In this |
9141 | case, the return value is the length of the encoded text. | |
df7492f9 KH |
9142 | |
9143 | This function sets `last-coding-system-used' to the precise coding system | |
9144 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
9145 | not fully specified.) */) | |
9146 | (string, coding_system, nocopy, buffer) | |
9147 | Lisp_Object string, coding_system, nocopy, buffer; | |
4ed46869 | 9148 | { |
df7492f9 | 9149 | return code_convert_string (string, coding_system, buffer, |
c197f191 | 9150 | 1, ! NILP (nocopy), 1); |
4ed46869 | 9151 | } |
df7492f9 | 9152 | |
3a73fa5d | 9153 | \f |
4ed46869 | 9154 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
9155 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
9156 | Return the corresponding character. */) | |
9157 | (code) | |
4ed46869 | 9158 | Lisp_Object code; |
4ed46869 | 9159 | { |
df7492f9 KH |
9160 | Lisp_Object spec, attrs, val; |
9161 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
9162 | int c; | |
4ed46869 | 9163 | |
df7492f9 KH |
9164 | CHECK_NATNUM (code); |
9165 | c = XFASTINT (code); | |
9166 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9167 | attrs = AREF (spec, 0); | |
4ed46869 | 9168 | |
df7492f9 KH |
9169 | if (ASCII_BYTE_P (c) |
9170 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9171 | return code; | |
4ed46869 | 9172 | |
df7492f9 KH |
9173 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9174 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
9175 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
9176 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 9177 | |
df7492f9 KH |
9178 | if (c <= 0x7F) |
9179 | charset = charset_roman; | |
9180 | else if (c >= 0xA0 && c < 0xDF) | |
55ab7be3 | 9181 | { |
df7492f9 KH |
9182 | charset = charset_kana; |
9183 | c -= 0x80; | |
4ed46869 | 9184 | } |
55ab7be3 | 9185 | else |
4ed46869 | 9186 | { |
004068e4 | 9187 | int s1 = c >> 8, s2 = c & 0xFF; |
df7492f9 KH |
9188 | |
9189 | if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF | |
9190 | || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC) | |
9191 | error ("Invalid code: %d", code); | |
9192 | SJIS_TO_JIS (c); | |
9193 | charset = charset_kanji; | |
4ed46869 | 9194 | } |
df7492f9 KH |
9195 | c = DECODE_CHAR (charset, c); |
9196 | if (c < 0) | |
9197 | error ("Invalid code: %d", code); | |
9198 | return make_number (c); | |
93dec019 | 9199 | } |
4ed46869 | 9200 | |
48b0f3ae | 9201 | |
4ed46869 | 9202 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
8acf0c0e | 9203 | doc: /* Encode a Japanese character CH to shift_jis encoding. |
48b0f3ae PJ |
9204 | Return the corresponding code in SJIS. */) |
9205 | (ch) | |
df7492f9 | 9206 | Lisp_Object ch; |
4ed46869 | 9207 | { |
df7492f9 KH |
9208 | Lisp_Object spec, attrs, charset_list; |
9209 | int c; | |
9210 | struct charset *charset; | |
9211 | unsigned code; | |
48b0f3ae | 9212 | |
df7492f9 KH |
9213 | CHECK_CHARACTER (ch); |
9214 | c = XFASTINT (ch); | |
9215 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9216 | attrs = AREF (spec, 0); | |
9217 | ||
9218 | if (ASCII_CHAR_P (c) | |
9219 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9220 | return ch; | |
9221 | ||
9222 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9223 | charset = char_charset (c, charset_list, &code); | |
9224 | if (code == CHARSET_INVALID_CODE (charset)) | |
9225 | error ("Can't encode by shift_jis encoding: %d", c); | |
9226 | JIS_TO_SJIS (code); | |
9227 | ||
9228 | return make_number (code); | |
4ed46869 KH |
9229 | } |
9230 | ||
9231 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
9232 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
9233 | Return the corresponding character. */) | |
9234 | (code) | |
4ed46869 | 9235 | Lisp_Object code; |
d46c5b12 | 9236 | { |
df7492f9 KH |
9237 | Lisp_Object spec, attrs, val; |
9238 | struct charset *charset_roman, *charset_big5, *charset; | |
9239 | int c; | |
6289dd10 | 9240 | |
df7492f9 KH |
9241 | CHECK_NATNUM (code); |
9242 | c = XFASTINT (code); | |
9243 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9244 | attrs = AREF (spec, 0); | |
4ed46869 | 9245 | |
df7492f9 KH |
9246 | if (ASCII_BYTE_P (c) |
9247 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9248 | return code; | |
6289dd10 | 9249 | |
df7492f9 KH |
9250 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9251 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
9252 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 9253 | |
df7492f9 KH |
9254 | if (c <= 0x7F) |
9255 | charset = charset_roman; | |
c28a9453 KH |
9256 | else |
9257 | { | |
df7492f9 KH |
9258 | int b1 = c >> 8, b2 = c & 0x7F; |
9259 | if (b1 < 0xA1 || b1 > 0xFE | |
9260 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
9261 | error ("Invalid code: %d", code); | |
9262 | charset = charset_big5; | |
c28a9453 | 9263 | } |
df7492f9 KH |
9264 | c = DECODE_CHAR (charset, (unsigned )c); |
9265 | if (c < 0) | |
9266 | error ("Invalid code: %d", code); | |
9267 | return make_number (c); | |
d46c5b12 | 9268 | } |
6289dd10 | 9269 | |
4ed46869 | 9270 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
8acf0c0e | 9271 | doc: /* Encode the Big5 character CH to BIG5 coding system. |
48b0f3ae PJ |
9272 | Return the corresponding character code in Big5. */) |
9273 | (ch) | |
4ed46869 KH |
9274 | Lisp_Object ch; |
9275 | { | |
df7492f9 KH |
9276 | Lisp_Object spec, attrs, charset_list; |
9277 | struct charset *charset; | |
9278 | int c; | |
9279 | unsigned code; | |
9280 | ||
9281 | CHECK_CHARACTER (ch); | |
9282 | c = XFASTINT (ch); | |
9283 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9284 | attrs = AREF (spec, 0); | |
9285 | if (ASCII_CHAR_P (c) | |
9286 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9287 | return ch; | |
9288 | ||
9289 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9290 | charset = char_charset (c, charset_list, &code); | |
9291 | if (code == CHARSET_INVALID_CODE (charset)) | |
9292 | error ("Can't encode by Big5 encoding: %d", c); | |
9293 | ||
9294 | return make_number (code); | |
4ed46869 | 9295 | } |
48b0f3ae | 9296 | |
3a73fa5d | 9297 | \f |
002fdb44 | 9298 | DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal, |
68bba4e4 | 9299 | Sset_terminal_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9300 | doc: /* Internal use only. */) |
6ed8eeff | 9301 | (coding_system, terminal) |
b74e4686 | 9302 | Lisp_Object coding_system; |
6ed8eeff | 9303 | Lisp_Object terminal; |
4ed46869 | 9304 | { |
6ed8eeff | 9305 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); |
b7826503 | 9306 | CHECK_SYMBOL (coding_system); |
b8299c66 | 9307 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); |
70c22245 | 9308 | /* We had better not send unsafe characters to terminal. */ |
c73bd236 | 9309 | terminal_coding->mode |= CODING_MODE_SAFE_ENCODING; |
df7492f9 | 9310 | /* Characer composition should be disabled. */ |
c73bd236 | 9311 | terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b8299c66 KL |
9312 | terminal_coding->src_multibyte = 1; |
9313 | terminal_coding->dst_multibyte = 0; | |
4ed46869 KH |
9314 | return Qnil; |
9315 | } | |
9316 | ||
c4825358 KH |
9317 | DEFUN ("set-safe-terminal-coding-system-internal", |
9318 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 9319 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 9320 | doc: /* Internal use only. */) |
48b0f3ae | 9321 | (coding_system) |
b74e4686 | 9322 | Lisp_Object coding_system; |
d46c5b12 | 9323 | { |
b7826503 | 9324 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
9325 | setup_coding_system (Fcheck_coding_system (coding_system), |
9326 | &safe_terminal_coding); | |
df7492f9 KH |
9327 | /* Characer composition should be disabled. */ |
9328 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
b73bfc1c KH |
9329 | safe_terminal_coding.src_multibyte = 1; |
9330 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
9331 | return Qnil; |
9332 | } | |
4ed46869 | 9333 | |
002fdb44 | 9334 | DEFUN ("terminal-coding-system", Fterminal_coding_system, |
68bba4e4 | 9335 | Sterminal_coding_system, 0, 1, 0, |
6ed8eeff KL |
9336 | doc: /* Return coding system specified for terminal output on the given terminal. |
9337 | TERMINAL may be a terminal id, a frame, or nil for the selected | |
9338 | frame's terminal device. */) | |
9339 | (terminal) | |
9340 | Lisp_Object terminal; | |
4ed46869 | 9341 | { |
985773c9 MB |
9342 | struct coding_system *terminal_coding |
9343 | = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); | |
9344 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); | |
ae6f73fa | 9345 | |
ae6f73fa | 9346 | /* For backward compatibility, return nil if it is `undecided'. */ |
f75c90a9 | 9347 | return (! EQ (coding_system, Qundecided) ? coding_system : Qnil); |
4ed46869 KH |
9348 | } |
9349 | ||
002fdb44 | 9350 | DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal, |
68bba4e4 | 9351 | Sset_keyboard_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9352 | doc: /* Internal use only. */) |
6ed8eeff | 9353 | (coding_system, terminal) |
4ed46869 | 9354 | Lisp_Object coding_system; |
6ed8eeff | 9355 | Lisp_Object terminal; |
4ed46869 | 9356 | { |
6ed8eeff | 9357 | struct terminal *t = get_terminal (terminal, 1); |
b7826503 | 9358 | CHECK_SYMBOL (coding_system); |
df7492f9 | 9359 | setup_coding_system (Fcheck_coding_system (coding_system), |
c73bd236 | 9360 | TERMINAL_KEYBOARD_CODING (t)); |
df7492f9 | 9361 | /* Characer composition should be disabled. */ |
c73bd236 MB |
9362 | TERMINAL_KEYBOARD_CODING (t)->common_flags |
9363 | &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
9364 | return Qnil; |
9365 | } | |
9366 | ||
9367 | DEFUN ("keyboard-coding-system", | |
985773c9 | 9368 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0, |
48b0f3ae | 9369 | doc: /* Return coding system specified for decoding keyboard input. */) |
985773c9 MB |
9370 | (terminal) |
9371 | Lisp_Object terminal; | |
4ed46869 | 9372 | { |
985773c9 MB |
9373 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING |
9374 | (get_terminal (terminal, 1))->id); | |
4ed46869 KH |
9375 | } |
9376 | ||
4ed46869 | 9377 | \f |
a5d301df KH |
9378 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
9379 | Sfind_operation_coding_system, 1, MANY, 0, | |
48b0f3ae PJ |
9380 | doc: /* Choose a coding system for an operation based on the target name. |
9381 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
9382 | DECODING-SYSTEM is the coding system to use for decoding | |
9383 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
9384 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 9385 | |
48b0f3ae PJ |
9386 | The first argument OPERATION specifies an I/O primitive: |
9387 | For file I/O, `insert-file-contents' or `write-region'. | |
9388 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
9389 | For network I/O, `open-network-stream'. | |
05e6f5dc | 9390 | |
48b0f3ae PJ |
9391 | The remaining arguments should be the same arguments that were passed |
9392 | to the primitive. Depending on which primitive, one of those arguments | |
9393 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
9394 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 9395 | |
48b0f3ae | 9396 | TARGET has a meaning which depends on OPERATION: |
b883cdb2 | 9397 | For file I/O, TARGET is a file name (except for the special case below). |
48b0f3ae | 9398 | For process I/O, TARGET is a process name. |
d4a1d553 | 9399 | For network I/O, TARGET is a service name or a port number. |
05e6f5dc | 9400 | |
d4a1d553 | 9401 | This function looks up what is specified for TARGET in |
48b0f3ae PJ |
9402 | `file-coding-system-alist', `process-coding-system-alist', |
9403 | or `network-coding-system-alist' depending on OPERATION. | |
9404 | They may specify a coding system, a cons of coding systems, | |
9405 | or a function symbol to call. | |
9406 | In the last case, we call the function with one argument, | |
9407 | which is a list of all the arguments given to this function. | |
1011c487 MB |
9408 | If the function can't decide a coding system, it can return |
9409 | `undecided' so that the normal code-detection is performed. | |
48b0f3ae | 9410 | |
b883cdb2 MB |
9411 | If OPERATION is `insert-file-contents', the argument corresponding to |
9412 | TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a | |
9413 | file name to look up, and BUFFER is a buffer that contains the file's | |
9414 | contents (not yet decoded). If `file-coding-system-alist' specifies a | |
9415 | function to call for FILENAME, that function should examine the | |
9416 | contents of BUFFER instead of reading the file. | |
9417 | ||
d918f936 | 9418 | usage: (find-operation-coding-system OPERATION ARGUMENTS...) */) |
48b0f3ae | 9419 | (nargs, args) |
4ed46869 KH |
9420 | int nargs; |
9421 | Lisp_Object *args; | |
6b89e3aa | 9422 | { |
4ed46869 KH |
9423 | Lisp_Object operation, target_idx, target, val; |
9424 | register Lisp_Object chain; | |
177c0ea7 | 9425 | |
4ed46869 KH |
9426 | if (nargs < 2) |
9427 | error ("Too few arguments"); | |
9428 | operation = args[0]; | |
9429 | if (!SYMBOLP (operation) | |
9430 | || !INTEGERP (target_idx = Fget (operation, Qtarget_idx))) | |
3ed051d4 | 9431 | error ("Invalid first argument"); |
4ed46869 KH |
9432 | if (nargs < 1 + XINT (target_idx)) |
9433 | error ("Too few arguments for operation: %s", | |
8f924df7 | 9434 | SDATA (SYMBOL_NAME (operation))); |
4ed46869 KH |
9435 | target = args[XINT (target_idx) + 1]; |
9436 | if (!(STRINGP (target) | |
091a0ff0 KH |
9437 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
9438 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | |
4ed46869 | 9439 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
df7492f9 | 9440 | error ("Invalid %dth argument", XINT (target_idx) + 1); |
091a0ff0 KH |
9441 | if (CONSP (target)) |
9442 | target = XCAR (target); | |
4ed46869 | 9443 | |
2e34157c RS |
9444 | chain = ((EQ (operation, Qinsert_file_contents) |
9445 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 9446 | ? Vfile_coding_system_alist |
2e34157c | 9447 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
9448 | ? Vnetwork_coding_system_alist |
9449 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
9450 | if (NILP (chain)) |
9451 | return Qnil; | |
9452 | ||
03699b14 | 9453 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 9454 | { |
f44d27ce | 9455 | Lisp_Object elt; |
6b89e3aa | 9456 | |
df7492f9 | 9457 | elt = XCAR (chain); |
4ed46869 KH |
9458 | if (CONSP (elt) |
9459 | && ((STRINGP (target) | |
03699b14 KR |
9460 | && STRINGP (XCAR (elt)) |
9461 | && fast_string_match (XCAR (elt), target) >= 0) | |
9462 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 9463 | { |
03699b14 | 9464 | val = XCDR (elt); |
b19fd4c5 KH |
9465 | /* Here, if VAL is both a valid coding system and a valid |
9466 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
9467 | if (CONSP (val)) |
9468 | return val; | |
9469 | if (! SYMBOLP (val)) | |
9470 | return Qnil; | |
9471 | if (! NILP (Fcoding_system_p (val))) | |
9472 | return Fcons (val, val); | |
b19fd4c5 | 9473 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 9474 | { |
e2b97060 MB |
9475 | /* We use call1 rather than safe_call1 |
9476 | so as to get bug reports about functions called here | |
9477 | which don't handle the current interface. */ | |
9478 | val = call1 (val, Flist (nargs, args)); | |
b19fd4c5 KH |
9479 | if (CONSP (val)) |
9480 | return val; | |
9481 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
9482 | return Fcons (val, val); | |
6b89e3aa | 9483 | } |
02ba4723 | 9484 | return Qnil; |
6b89e3aa KH |
9485 | } |
9486 | } | |
4ed46869 | 9487 | return Qnil; |
6b89e3aa KH |
9488 | } |
9489 | ||
df7492f9 | 9490 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 9491 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 9492 | doc: /* Assign higher priority to the coding systems given as arguments. |
d4a1d553 | 9493 | If multiple coding systems belong to the same category, |
a3181084 DL |
9494 | all but the first one are ignored. |
9495 | ||
d4a1d553 | 9496 | usage: (set-coding-system-priority &rest coding-systems) */) |
df7492f9 KH |
9497 | (nargs, args) |
9498 | int nargs; | |
9499 | Lisp_Object *args; | |
9500 | { | |
9501 | int i, j; | |
9502 | int changed[coding_category_max]; | |
9503 | enum coding_category priorities[coding_category_max]; | |
9504 | ||
9505 | bzero (changed, sizeof changed); | |
6b89e3aa | 9506 | |
df7492f9 | 9507 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 9508 | { |
df7492f9 KH |
9509 | enum coding_category category; |
9510 | Lisp_Object spec, attrs; | |
6b89e3aa | 9511 | |
df7492f9 KH |
9512 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
9513 | attrs = AREF (spec, 0); | |
9514 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
9515 | if (changed[category]) | |
9516 | /* Ignore this coding system because a coding system of the | |
9517 | same category already had a higher priority. */ | |
9518 | continue; | |
9519 | changed[category] = 1; | |
9520 | priorities[j++] = category; | |
9521 | if (coding_categories[category].id >= 0 | |
9522 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
9523 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 9524 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 9525 | } |
6b89e3aa | 9526 | |
df7492f9 KH |
9527 | /* Now we have decided top J priorities. Reflect the order of the |
9528 | original priorities to the remaining priorities. */ | |
6b89e3aa | 9529 | |
df7492f9 | 9530 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 9531 | { |
df7492f9 KH |
9532 | while (j < coding_category_max |
9533 | && changed[coding_priorities[j]]) | |
9534 | j++; | |
9535 | if (j == coding_category_max) | |
9536 | abort (); | |
9537 | priorities[i] = coding_priorities[j]; | |
9538 | } | |
6b89e3aa | 9539 | |
df7492f9 | 9540 | bcopy (priorities, coding_priorities, sizeof priorities); |
177c0ea7 | 9541 | |
ff563fce KH |
9542 | /* Update `coding-category-list'. */ |
9543 | Vcoding_category_list = Qnil; | |
9544 | for (i = coding_category_max - 1; i >= 0; i--) | |
9545 | Vcoding_category_list | |
9546 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
9547 | Vcoding_category_list); | |
6b89e3aa | 9548 | |
df7492f9 | 9549 | return Qnil; |
6b89e3aa KH |
9550 | } |
9551 | ||
df7492f9 KH |
9552 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
9553 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 DL |
9554 | doc: /* Return a list of coding systems ordered by their priorities. |
9555 | HIGHESTP non-nil means just return the highest priority one. */) | |
df7492f9 KH |
9556 | (highestp) |
9557 | Lisp_Object highestp; | |
d46c5b12 KH |
9558 | { |
9559 | int i; | |
df7492f9 | 9560 | Lisp_Object val; |
6b89e3aa | 9561 | |
df7492f9 | 9562 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 9563 | { |
df7492f9 KH |
9564 | enum coding_category category = coding_priorities[i]; |
9565 | int id = coding_categories[category].id; | |
9566 | Lisp_Object attrs; | |
068a9dbd | 9567 | |
df7492f9 KH |
9568 | if (id < 0) |
9569 | continue; | |
9570 | attrs = CODING_ID_ATTRS (id); | |
9571 | if (! NILP (highestp)) | |
9572 | return CODING_ATTR_BASE_NAME (attrs); | |
9573 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
9574 | } | |
9575 | return Fnreverse (val); | |
9576 | } | |
068a9dbd | 9577 | |
f0064e1f | 9578 | static char *suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
9579 | |
9580 | static Lisp_Object | |
df7492f9 KH |
9581 | make_subsidiaries (base) |
9582 | Lisp_Object base; | |
068a9dbd | 9583 | { |
df7492f9 | 9584 | Lisp_Object subsidiaries; |
8f924df7 | 9585 | int base_name_len = SBYTES (SYMBOL_NAME (base)); |
df7492f9 KH |
9586 | char *buf = (char *) alloca (base_name_len + 6); |
9587 | int i; | |
068a9dbd | 9588 | |
8f924df7 | 9589 | bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len); |
df7492f9 KH |
9590 | subsidiaries = Fmake_vector (make_number (3), Qnil); |
9591 | for (i = 0; i < 3; i++) | |
068a9dbd | 9592 | { |
df7492f9 KH |
9593 | bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1); |
9594 | ASET (subsidiaries, i, intern (buf)); | |
068a9dbd | 9595 | } |
df7492f9 | 9596 | return subsidiaries; |
068a9dbd KH |
9597 | } |
9598 | ||
9599 | ||
df7492f9 KH |
9600 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
9601 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
9602 | doc: /* For internal use only. |
9603 | usage: (define-coding-system-internal ...) */) | |
df7492f9 KH |
9604 | (nargs, args) |
9605 | int nargs; | |
9606 | Lisp_Object *args; | |
068a9dbd | 9607 | { |
df7492f9 KH |
9608 | Lisp_Object name; |
9609 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
9610 | Lisp_Object attrs; /* Vector of attributes. */ | |
9611 | Lisp_Object eol_type; | |
9612 | Lisp_Object aliases; | |
9613 | Lisp_Object coding_type, charset_list, safe_charsets; | |
9614 | enum coding_category category; | |
9615 | Lisp_Object tail, val; | |
9616 | int max_charset_id = 0; | |
9617 | int i; | |
068a9dbd | 9618 | |
df7492f9 KH |
9619 | if (nargs < coding_arg_max) |
9620 | goto short_args; | |
068a9dbd | 9621 | |
df7492f9 | 9622 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 9623 | |
df7492f9 KH |
9624 | name = args[coding_arg_name]; |
9625 | CHECK_SYMBOL (name); | |
9626 | CODING_ATTR_BASE_NAME (attrs) = name; | |
068a9dbd | 9627 | |
df7492f9 KH |
9628 | val = args[coding_arg_mnemonic]; |
9629 | if (! STRINGP (val)) | |
9630 | CHECK_CHARACTER (val); | |
9631 | CODING_ATTR_MNEMONIC (attrs) = val; | |
068a9dbd | 9632 | |
df7492f9 KH |
9633 | coding_type = args[coding_arg_coding_type]; |
9634 | CHECK_SYMBOL (coding_type); | |
9635 | CODING_ATTR_TYPE (attrs) = coding_type; | |
068a9dbd | 9636 | |
df7492f9 KH |
9637 | charset_list = args[coding_arg_charset_list]; |
9638 | if (SYMBOLP (charset_list)) | |
9639 | { | |
9640 | if (EQ (charset_list, Qiso_2022)) | |
9641 | { | |
9642 | if (! EQ (coding_type, Qiso_2022)) | |
9643 | error ("Invalid charset-list"); | |
9644 | charset_list = Viso_2022_charset_list; | |
9645 | } | |
9646 | else if (EQ (charset_list, Qemacs_mule)) | |
9647 | { | |
9648 | if (! EQ (coding_type, Qemacs_mule)) | |
9649 | error ("Invalid charset-list"); | |
9650 | charset_list = Vemacs_mule_charset_list; | |
9651 | } | |
9652 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
9653 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
9654 | max_charset_id = XFASTINT (XCAR (tail)); | |
9655 | } | |
068a9dbd KH |
9656 | else |
9657 | { | |
df7492f9 | 9658 | charset_list = Fcopy_sequence (charset_list); |
985773c9 | 9659 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
068a9dbd | 9660 | { |
df7492f9 KH |
9661 | struct charset *charset; |
9662 | ||
985773c9 | 9663 | val = XCAR (tail); |
df7492f9 KH |
9664 | CHECK_CHARSET_GET_CHARSET (val, charset); |
9665 | if (EQ (coding_type, Qiso_2022) | |
9666 | ? CHARSET_ISO_FINAL (charset) < 0 | |
9667 | : EQ (coding_type, Qemacs_mule) | |
9668 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
9669 | : 0) | |
9670 | error ("Can't handle charset `%s'", | |
8f924df7 | 9671 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 9672 | |
8f924df7 | 9673 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
9674 | if (max_charset_id < charset->id) |
9675 | max_charset_id = charset->id; | |
068a9dbd KH |
9676 | } |
9677 | } | |
df7492f9 | 9678 | CODING_ATTR_CHARSET_LIST (attrs) = charset_list; |
068a9dbd | 9679 | |
1b3b981b AS |
9680 | safe_charsets = make_uninit_string (max_charset_id + 1); |
9681 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 | 9682 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
8f924df7 | 9683 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 9684 | CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets; |
068a9dbd | 9685 | |
584948ac | 9686 | CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p]; |
3a73fa5d | 9687 | |
df7492f9 | 9688 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 9689 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9690 | CHECK_SYMBOL (val); |
df7492f9 | 9691 | CODING_ATTR_DECODE_TBL (attrs) = val; |
3a73fa5d | 9692 | |
df7492f9 | 9693 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 9694 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9695 | CHECK_SYMBOL (val); |
df7492f9 | 9696 | CODING_ATTR_ENCODE_TBL (attrs) = val; |
d46c5b12 | 9697 | |
df7492f9 KH |
9698 | val = args[coding_arg_post_read_conversion]; |
9699 | CHECK_SYMBOL (val); | |
9700 | CODING_ATTR_POST_READ (attrs) = val; | |
d46c5b12 | 9701 | |
df7492f9 KH |
9702 | val = args[coding_arg_pre_write_conversion]; |
9703 | CHECK_SYMBOL (val); | |
9704 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
3a73fa5d | 9705 | |
df7492f9 KH |
9706 | val = args[coding_arg_default_char]; |
9707 | if (NILP (val)) | |
9708 | CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' '); | |
9709 | else | |
9710 | { | |
8f924df7 | 9711 | CHECK_CHARACTER (val); |
df7492f9 KH |
9712 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; |
9713 | } | |
4031e2bf | 9714 | |
8f924df7 KH |
9715 | val = args[coding_arg_for_unibyte]; |
9716 | CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt; | |
3a73fa5d | 9717 | |
df7492f9 KH |
9718 | val = args[coding_arg_plist]; |
9719 | CHECK_LIST (val); | |
9720 | CODING_ATTR_PLIST (attrs) = val; | |
3a73fa5d | 9721 | |
df7492f9 KH |
9722 | if (EQ (coding_type, Qcharset)) |
9723 | { | |
c7c66a95 KH |
9724 | /* Generate a lisp vector of 256 elements. Each element is nil, |
9725 | integer, or a list of charset IDs. | |
3a73fa5d | 9726 | |
c7c66a95 KH |
9727 | If Nth element is nil, the byte code N is invalid in this |
9728 | coding system. | |
4ed46869 | 9729 | |
c7c66a95 KH |
9730 | If Nth element is a number NUM, N is the first byte of a |
9731 | charset whose ID is NUM. | |
4ed46869 | 9732 | |
c7c66a95 KH |
9733 | If Nth element is a list of charset IDs, N is the first byte |
9734 | of one of them. The list is sorted by dimensions of the | |
2bc515e4 | 9735 | charsets. A charset of smaller dimension comes firtst. */ |
df7492f9 | 9736 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 9737 | |
5c99c2e6 | 9738 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 9739 | { |
c7c66a95 KH |
9740 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
9741 | int dim = CHARSET_DIMENSION (charset); | |
9742 | int idx = (dim - 1) * 4; | |
4ed46869 | 9743 | |
5c99c2e6 | 9744 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
584948ac | 9745 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
4031e2bf | 9746 | |
15d143f7 KH |
9747 | for (i = charset->code_space[idx]; |
9748 | i <= charset->code_space[idx + 1]; i++) | |
9749 | { | |
c7c66a95 KH |
9750 | Lisp_Object tmp, tmp2; |
9751 | int dim2; | |
ec6d2bb8 | 9752 | |
c7c66a95 KH |
9753 | tmp = AREF (val, i); |
9754 | if (NILP (tmp)) | |
9755 | tmp = XCAR (tail); | |
9756 | else if (NUMBERP (tmp)) | |
9757 | { | |
9758 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
9759 | if (dim < dim2) | |
c7c66a95 | 9760 | tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil)); |
f9d71dcd KH |
9761 | else |
9762 | tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil)); | |
c7c66a95 | 9763 | } |
15d143f7 | 9764 | else |
c7c66a95 KH |
9765 | { |
9766 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
9767 | { | |
9768 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
9769 | if (dim < dim2) | |
9770 | break; | |
9771 | } | |
9772 | if (NILP (tmp2)) | |
9773 | tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil)); | |
9774 | else | |
9775 | { | |
9776 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
9777 | XSETCAR (tmp2, XCAR (tail)); | |
9778 | } | |
9779 | } | |
9780 | ASET (val, i, tmp); | |
15d143f7 | 9781 | } |
df7492f9 KH |
9782 | } |
9783 | ASET (attrs, coding_attr_charset_valids, val); | |
9784 | category = coding_category_charset; | |
9785 | } | |
9786 | else if (EQ (coding_type, Qccl)) | |
9787 | { | |
9788 | Lisp_Object valids; | |
ecec61c1 | 9789 | |
df7492f9 KH |
9790 | if (nargs < coding_arg_ccl_max) |
9791 | goto short_args; | |
ecec61c1 | 9792 | |
df7492f9 KH |
9793 | val = args[coding_arg_ccl_decoder]; |
9794 | CHECK_CCL_PROGRAM (val); | |
9795 | if (VECTORP (val)) | |
9796 | val = Fcopy_sequence (val); | |
9797 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 9798 | |
df7492f9 KH |
9799 | val = args[coding_arg_ccl_encoder]; |
9800 | CHECK_CCL_PROGRAM (val); | |
9801 | if (VECTORP (val)) | |
9802 | val = Fcopy_sequence (val); | |
9803 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 9804 | |
df7492f9 KH |
9805 | val = args[coding_arg_ccl_valids]; |
9806 | valids = Fmake_string (make_number (256), make_number (0)); | |
9807 | for (tail = val; !NILP (tail); tail = Fcdr (tail)) | |
9808 | { | |
8dcbea82 | 9809 | int from, to; |
ecec61c1 | 9810 | |
df7492f9 KH |
9811 | val = Fcar (tail); |
9812 | if (INTEGERP (val)) | |
8dcbea82 KH |
9813 | { |
9814 | from = to = XINT (val); | |
9815 | if (from < 0 || from > 255) | |
9816 | args_out_of_range_3 (val, make_number (0), make_number (255)); | |
9817 | } | |
df7492f9 KH |
9818 | else |
9819 | { | |
df7492f9 | 9820 | CHECK_CONS (val); |
8f924df7 KH |
9821 | CHECK_NATNUM_CAR (val); |
9822 | CHECK_NATNUM_CDR (val); | |
df7492f9 | 9823 | from = XINT (XCAR (val)); |
8f924df7 | 9824 | if (from > 255) |
8dcbea82 KH |
9825 | args_out_of_range_3 (XCAR (val), |
9826 | make_number (0), make_number (255)); | |
df7492f9 | 9827 | to = XINT (XCDR (val)); |
8dcbea82 KH |
9828 | if (to < from || to > 255) |
9829 | args_out_of_range_3 (XCDR (val), | |
9830 | XCAR (val), make_number (255)); | |
df7492f9 | 9831 | } |
8dcbea82 | 9832 | for (i = from; i <= to; i++) |
8f924df7 | 9833 | SSET (valids, i, 1); |
df7492f9 KH |
9834 | } |
9835 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 9836 | |
df7492f9 | 9837 | category = coding_category_ccl; |
55ab7be3 | 9838 | } |
df7492f9 | 9839 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 9840 | { |
df7492f9 | 9841 | Lisp_Object bom, endian; |
4ed46869 | 9842 | |
584948ac | 9843 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; |
4ed46869 | 9844 | |
df7492f9 KH |
9845 | if (nargs < coding_arg_utf16_max) |
9846 | goto short_args; | |
4ed46869 | 9847 | |
df7492f9 KH |
9848 | bom = args[coding_arg_utf16_bom]; |
9849 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
9850 | { | |
9851 | CHECK_CONS (bom); | |
8f924df7 KH |
9852 | val = XCAR (bom); |
9853 | CHECK_CODING_SYSTEM (val); | |
9854 | val = XCDR (bom); | |
9855 | CHECK_CODING_SYSTEM (val); | |
df7492f9 | 9856 | } |
a470d443 | 9857 | ASET (attrs, coding_attr_utf_bom, bom); |
df7492f9 KH |
9858 | |
9859 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
9860 | CHECK_SYMBOL (endian); |
9861 | if (NILP (endian)) | |
9862 | endian = Qbig; | |
9863 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 9864 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
9865 | ASET (attrs, coding_attr_utf_16_endian, endian); |
9866 | ||
9867 | category = (CONSP (bom) | |
9868 | ? coding_category_utf_16_auto | |
9869 | : NILP (bom) | |
b49a1807 | 9870 | ? (EQ (endian, Qbig) |
df7492f9 KH |
9871 | ? coding_category_utf_16_be_nosig |
9872 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 9873 | : (EQ (endian, Qbig) |
df7492f9 KH |
9874 | ? coding_category_utf_16_be |
9875 | : coding_category_utf_16_le)); | |
9876 | } | |
9877 | else if (EQ (coding_type, Qiso_2022)) | |
9878 | { | |
9879 | Lisp_Object initial, reg_usage, request, flags; | |
4776e638 | 9880 | int i; |
1397dc18 | 9881 | |
df7492f9 KH |
9882 | if (nargs < coding_arg_iso2022_max) |
9883 | goto short_args; | |
9884 | ||
9885 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
9886 | CHECK_VECTOR (initial); | |
9887 | for (i = 0; i < 4; i++) | |
9888 | { | |
9889 | val = Faref (initial, make_number (i)); | |
9890 | if (! NILP (val)) | |
9891 | { | |
584948ac KH |
9892 | struct charset *charset; |
9893 | ||
9894 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
9895 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
9896 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
9897 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9898 | } |
9899 | else | |
9900 | ASET (initial, i, make_number (-1)); | |
9901 | } | |
9902 | ||
9903 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
9904 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
9905 | CHECK_NUMBER_CAR (reg_usage); |
9906 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
9907 | |
9908 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
9909 | for (tail = request; ! NILP (tail); tail = Fcdr (tail)) | |
1397dc18 | 9910 | { |
df7492f9 | 9911 | int id; |
8f924df7 | 9912 | Lisp_Object tmp; |
df7492f9 KH |
9913 | |
9914 | val = Fcar (tail); | |
9915 | CHECK_CONS (val); | |
8f924df7 KH |
9916 | tmp = XCAR (val); |
9917 | CHECK_CHARSET_GET_ID (tmp, id); | |
9918 | CHECK_NATNUM_CDR (val); | |
df7492f9 KH |
9919 | if (XINT (XCDR (val)) >= 4) |
9920 | error ("Invalid graphic register number: %d", XINT (XCDR (val))); | |
8f924df7 | 9921 | XSETCAR (val, make_number (id)); |
1397dc18 | 9922 | } |
4ed46869 | 9923 | |
df7492f9 KH |
9924 | flags = args[coding_arg_iso2022_flags]; |
9925 | CHECK_NATNUM (flags); | |
9926 | i = XINT (flags); | |
9927 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) | |
9928 | flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT); | |
9929 | ||
9930 | ASET (attrs, coding_attr_iso_initial, initial); | |
9931 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
9932 | ASET (attrs, coding_attr_iso_request, request); | |
9933 | ASET (attrs, coding_attr_iso_flags, flags); | |
9934 | setup_iso_safe_charsets (attrs); | |
9935 | ||
9936 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
9937 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
9938 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
9939 | ? coding_category_iso_7_else | |
9940 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
9941 | ? coding_category_iso_7 | |
9942 | : coding_category_iso_7_tight); | |
9943 | else | |
9944 | { | |
9945 | int id = XINT (AREF (initial, 1)); | |
9946 | ||
c6fb6e98 | 9947 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
9948 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
9949 | || id < 0) | |
9950 | ? coding_category_iso_8_else | |
9951 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
9952 | ? coding_category_iso_8_1 | |
9953 | : coding_category_iso_8_2); | |
9954 | } | |
0ce7886f KH |
9955 | if (category != coding_category_iso_8_1 |
9956 | && category != coding_category_iso_8_2) | |
9957 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; | |
df7492f9 KH |
9958 | } |
9959 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 9960 | { |
df7492f9 KH |
9961 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
9962 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
584948ac | 9963 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
df7492f9 | 9964 | category = coding_category_emacs_mule; |
c28a9453 | 9965 | } |
df7492f9 | 9966 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 9967 | { |
df7492f9 KH |
9968 | |
9969 | struct charset *charset; | |
9970 | ||
7d64c6ad | 9971 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 9972 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 9973 | error ("There should be three or four charsets"); |
df7492f9 KH |
9974 | |
9975 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9976 | if (CHARSET_DIMENSION (charset) != 1) | |
9977 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9978 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
9979 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
9980 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9981 | |
9982 | charset_list = XCDR (charset_list); | |
9983 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9984 | if (CHARSET_DIMENSION (charset) != 1) | |
9985 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9986 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
9987 | |
9988 | charset_list = XCDR (charset_list); | |
9989 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9990 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
9991 | error ("Dimension of charset %s is not two", |
9992 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9993 | ||
9994 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
9995 | if (! NILP (charset_list)) |
9996 | { | |
9997 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9998 | if (CHARSET_DIMENSION (charset) != 2) | |
9999 | error ("Dimension of charset %s is not two", | |
10000 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
10001 | } | |
df7492f9 KH |
10002 | |
10003 | category = coding_category_sjis; | |
10004 | Vsjis_coding_system = name; | |
c28a9453 | 10005 | } |
df7492f9 KH |
10006 | else if (EQ (coding_type, Qbig5)) |
10007 | { | |
10008 | struct charset *charset; | |
4ed46869 | 10009 | |
df7492f9 KH |
10010 | if (XINT (Flength (charset_list)) != 2) |
10011 | error ("There should be just two charsets"); | |
10012 | ||
10013 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10014 | if (CHARSET_DIMENSION (charset) != 1) | |
10015 | error ("Dimension of charset %s is not one", | |
8f924df7 | 10016 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
10017 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
10018 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
10019 | |
10020 | charset_list = XCDR (charset_list); | |
10021 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10022 | if (CHARSET_DIMENSION (charset) != 2) | |
10023 | error ("Dimension of charset %s is not two", | |
8f924df7 | 10024 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 10025 | |
df7492f9 KH |
10026 | category = coding_category_big5; |
10027 | Vbig5_coding_system = name; | |
10028 | } | |
10029 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 10030 | { |
584948ac KH |
10031 | category = coding_category_raw_text; |
10032 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
c28a9453 | 10033 | } |
df7492f9 | 10034 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 10035 | { |
a470d443 KH |
10036 | Lisp_Object bom; |
10037 | ||
584948ac | 10038 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
a470d443 KH |
10039 | |
10040 | if (nargs < coding_arg_utf8_max) | |
10041 | goto short_args; | |
10042 | ||
10043 | bom = args[coding_arg_utf8_bom]; | |
10044 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
10045 | { | |
10046 | CHECK_CONS (bom); | |
10047 | val = XCAR (bom); | |
10048 | CHECK_CODING_SYSTEM (val); | |
10049 | val = XCDR (bom); | |
10050 | CHECK_CODING_SYSTEM (val); | |
10051 | } | |
10052 | ASET (attrs, coding_attr_utf_bom, bom); | |
10053 | ||
10054 | category = (CONSP (bom) ? coding_category_utf_8_auto | |
10055 | : NILP (bom) ? coding_category_utf_8_nosig | |
10056 | : coding_category_utf_8_sig); | |
4ed46869 | 10057 | } |
df7492f9 KH |
10058 | else if (EQ (coding_type, Qundecided)) |
10059 | category = coding_category_undecided; | |
4ed46869 | 10060 | else |
df7492f9 | 10061 | error ("Invalid coding system type: %s", |
8f924df7 | 10062 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 10063 | |
df7492f9 | 10064 | CODING_ATTR_CATEGORY (attrs) = make_number (category); |
01378f49 KH |
10065 | CODING_ATTR_PLIST (attrs) |
10066 | = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category), | |
10067 | CODING_ATTR_PLIST (attrs))); | |
35befdaa | 10068 | CODING_ATTR_PLIST (attrs) |
3ed051d4 | 10069 | = Fcons (QCascii_compatible_p, |
35befdaa KH |
10070 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), |
10071 | CODING_ATTR_PLIST (attrs))); | |
c4825358 | 10072 | |
df7492f9 KH |
10073 | eol_type = args[coding_arg_eol_type]; |
10074 | if (! NILP (eol_type) | |
10075 | && ! EQ (eol_type, Qunix) | |
10076 | && ! EQ (eol_type, Qdos) | |
10077 | && ! EQ (eol_type, Qmac)) | |
10078 | error ("Invalid eol-type"); | |
4ed46869 | 10079 | |
df7492f9 | 10080 | aliases = Fcons (name, Qnil); |
4ed46869 | 10081 | |
df7492f9 KH |
10082 | if (NILP (eol_type)) |
10083 | { | |
10084 | eol_type = make_subsidiaries (name); | |
10085 | for (i = 0; i < 3; i++) | |
1397dc18 | 10086 | { |
df7492f9 KH |
10087 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
10088 | ||
10089 | this_name = AREF (eol_type, i); | |
10090 | this_aliases = Fcons (this_name, Qnil); | |
10091 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); | |
10092 | this_spec = Fmake_vector (make_number (3), attrs); | |
10093 | ASET (this_spec, 1, this_aliases); | |
10094 | ASET (this_spec, 2, this_eol_type); | |
10095 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
10096 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
583f71ca KH |
10097 | val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist); |
10098 | if (NILP (val)) | |
10099 | Vcoding_system_alist | |
10100 | = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
10101 | Vcoding_system_alist); | |
1397dc18 | 10102 | } |
d46c5b12 | 10103 | } |
4ed46869 | 10104 | |
df7492f9 KH |
10105 | spec_vec = Fmake_vector (make_number (3), attrs); |
10106 | ASET (spec_vec, 1, aliases); | |
10107 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 10108 | |
df7492f9 KH |
10109 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
10110 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
583f71ca KH |
10111 | val = Fassoc (Fsymbol_name (name), Vcoding_system_alist); |
10112 | if (NILP (val)) | |
10113 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
10114 | Vcoding_system_alist); | |
48b0f3ae | 10115 | |
df7492f9 KH |
10116 | { |
10117 | int id = coding_categories[category].id; | |
48b0f3ae | 10118 | |
df7492f9 KH |
10119 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
10120 | setup_coding_system (name, &coding_categories[category]); | |
10121 | } | |
48b0f3ae | 10122 | |
d46c5b12 | 10123 | return Qnil; |
48b0f3ae | 10124 | |
df7492f9 KH |
10125 | short_args: |
10126 | return Fsignal (Qwrong_number_of_arguments, | |
10127 | Fcons (intern ("define-coding-system-internal"), | |
10128 | make_number (nargs))); | |
d46c5b12 | 10129 | } |
4ed46869 | 10130 | |
d6925f38 | 10131 | |
a6f87d34 KH |
10132 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
10133 | 3, 3, 0, | |
10134 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
10135 | (coding_system, prop, val) | |
10136 | Lisp_Object coding_system, prop, val; | |
10137 | { | |
3dbe7859 | 10138 | Lisp_Object spec, attrs; |
a6f87d34 KH |
10139 | |
10140 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10141 | attrs = AREF (spec, 0); | |
10142 | if (EQ (prop, QCmnemonic)) | |
10143 | { | |
10144 | if (! STRINGP (val)) | |
10145 | CHECK_CHARACTER (val); | |
10146 | CODING_ATTR_MNEMONIC (attrs) = val; | |
10147 | } | |
2133e2d1 | 10148 | else if (EQ (prop, QCdefault_char)) |
a6f87d34 KH |
10149 | { |
10150 | if (NILP (val)) | |
10151 | val = make_number (' '); | |
10152 | else | |
10153 | CHECK_CHARACTER (val); | |
10154 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; | |
10155 | } | |
10156 | else if (EQ (prop, QCdecode_translation_table)) | |
10157 | { | |
10158 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10159 | CHECK_SYMBOL (val); | |
10160 | CODING_ATTR_DECODE_TBL (attrs) = val; | |
10161 | } | |
10162 | else if (EQ (prop, QCencode_translation_table)) | |
10163 | { | |
10164 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10165 | CHECK_SYMBOL (val); | |
10166 | CODING_ATTR_ENCODE_TBL (attrs) = val; | |
10167 | } | |
10168 | else if (EQ (prop, QCpost_read_conversion)) | |
10169 | { | |
10170 | CHECK_SYMBOL (val); | |
10171 | CODING_ATTR_POST_READ (attrs) = val; | |
10172 | } | |
10173 | else if (EQ (prop, QCpre_write_conversion)) | |
10174 | { | |
10175 | CHECK_SYMBOL (val); | |
10176 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
10177 | } | |
35befdaa KH |
10178 | else if (EQ (prop, QCascii_compatible_p)) |
10179 | { | |
10180 | CODING_ATTR_ASCII_COMPAT (attrs) = val; | |
10181 | } | |
a6f87d34 KH |
10182 | |
10183 | CODING_ATTR_PLIST (attrs) | |
10184 | = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val); | |
10185 | return val; | |
10186 | } | |
10187 | ||
10188 | ||
df7492f9 KH |
10189 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
10190 | Sdefine_coding_system_alias, 2, 2, 0, | |
10191 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
10192 | (alias, coding_system) | |
10193 | Lisp_Object alias, coding_system; | |
66cfb530 | 10194 | { |
583f71ca | 10195 | Lisp_Object spec, aliases, eol_type, val; |
4ed46869 | 10196 | |
df7492f9 KH |
10197 | CHECK_SYMBOL (alias); |
10198 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10199 | aliases = AREF (spec, 1); | |
d4a1d553 | 10200 | /* ALIASES should be a list of length more than zero, and the first |
d6925f38 KH |
10201 | element is a base coding system. Append ALIAS at the tail of the |
10202 | list. */ | |
df7492f9 KH |
10203 | while (!NILP (XCDR (aliases))) |
10204 | aliases = XCDR (aliases); | |
8f924df7 | 10205 | XSETCDR (aliases, Fcons (alias, Qnil)); |
4ed46869 | 10206 | |
df7492f9 KH |
10207 | eol_type = AREF (spec, 2); |
10208 | if (VECTORP (eol_type)) | |
4ed46869 | 10209 | { |
df7492f9 KH |
10210 | Lisp_Object subsidiaries; |
10211 | int i; | |
4ed46869 | 10212 | |
df7492f9 KH |
10213 | subsidiaries = make_subsidiaries (alias); |
10214 | for (i = 0; i < 3; i++) | |
10215 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
10216 | AREF (eol_type, i)); | |
4ed46869 | 10217 | } |
df7492f9 KH |
10218 | |
10219 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 10220 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
583f71ca KH |
10221 | val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist); |
10222 | if (NILP (val)) | |
10223 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), | |
10224 | Vcoding_system_alist); | |
66cfb530 | 10225 | |
4ed46869 KH |
10226 | return Qnil; |
10227 | } | |
10228 | ||
df7492f9 KH |
10229 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
10230 | 1, 1, 0, | |
10231 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 10232 | Any alias or subsidiary coding system is not a base coding system. */) |
df7492f9 KH |
10233 | (coding_system) |
10234 | Lisp_Object coding_system; | |
d46c5b12 | 10235 | { |
df7492f9 | 10236 | Lisp_Object spec, attrs; |
d46c5b12 | 10237 | |
df7492f9 KH |
10238 | if (NILP (coding_system)) |
10239 | return (Qno_conversion); | |
10240 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10241 | attrs = AREF (spec, 0); | |
10242 | return CODING_ATTR_BASE_NAME (attrs); | |
10243 | } | |
1397dc18 | 10244 | |
df7492f9 KH |
10245 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
10246 | 1, 1, 0, | |
10247 | doc: "Return the property list of CODING-SYSTEM.") | |
10248 | (coding_system) | |
10249 | Lisp_Object coding_system; | |
10250 | { | |
10251 | Lisp_Object spec, attrs; | |
1397dc18 | 10252 | |
df7492f9 KH |
10253 | if (NILP (coding_system)) |
10254 | coding_system = Qno_conversion; | |
10255 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10256 | attrs = AREF (spec, 0); | |
10257 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
10258 | } |
10259 | ||
df7492f9 KH |
10260 | |
10261 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
10262 | 1, 1, 0, | |
da7db224 | 10263 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
df7492f9 KH |
10264 | (coding_system) |
10265 | Lisp_Object coding_system; | |
66cfb530 | 10266 | { |
df7492f9 | 10267 | Lisp_Object spec; |
84d60297 | 10268 | |
df7492f9 KH |
10269 | if (NILP (coding_system)) |
10270 | coding_system = Qno_conversion; | |
10271 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 10272 | return AREF (spec, 1); |
df7492f9 | 10273 | } |
66cfb530 | 10274 | |
df7492f9 KH |
10275 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
10276 | Scoding_system_eol_type, 1, 1, 0, | |
10277 | doc: /* Return eol-type of CODING-SYSTEM. | |
d4a1d553 | 10278 | An eol-type is an integer 0, 1, 2, or a vector of coding systems. |
66cfb530 | 10279 | |
df7492f9 KH |
10280 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
10281 | and CR respectively. | |
66cfb530 | 10282 | |
df7492f9 KH |
10283 | A vector value indicates that a format of end-of-line should be |
10284 | detected automatically. Nth element of the vector is the subsidiary | |
10285 | coding system whose eol-type is N. */) | |
6b89e3aa KH |
10286 | (coding_system) |
10287 | Lisp_Object coding_system; | |
10288 | { | |
df7492f9 KH |
10289 | Lisp_Object spec, eol_type; |
10290 | int n; | |
6b89e3aa | 10291 | |
df7492f9 KH |
10292 | if (NILP (coding_system)) |
10293 | coding_system = Qno_conversion; | |
10294 | if (! CODING_SYSTEM_P (coding_system)) | |
10295 | return Qnil; | |
10296 | spec = CODING_SYSTEM_SPEC (coding_system); | |
10297 | eol_type = AREF (spec, 2); | |
10298 | if (VECTORP (eol_type)) | |
10299 | return Fcopy_sequence (eol_type); | |
10300 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
10301 | return make_number (n); | |
6b89e3aa KH |
10302 | } |
10303 | ||
4ed46869 KH |
10304 | #endif /* emacs */ |
10305 | ||
10306 | \f | |
1397dc18 | 10307 | /*** 9. Post-amble ***/ |
4ed46869 | 10308 | |
dfcf069d | 10309 | void |
4ed46869 KH |
10310 | init_coding_once () |
10311 | { | |
10312 | int i; | |
10313 | ||
df7492f9 KH |
10314 | for (i = 0; i < coding_category_max; i++) |
10315 | { | |
10316 | coding_categories[i].id = -1; | |
10317 | coding_priorities[i] = i; | |
10318 | } | |
4ed46869 KH |
10319 | |
10320 | /* ISO2022 specific initialize routine. */ | |
10321 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 10322 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
10323 | for (i = 0x21; i < 0x7F; i++) |
10324 | iso_code_class[i] = ISO_graphic_plane_0; | |
10325 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 10326 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
10327 | for (i = 0xA1; i < 0xFF; i++) |
10328 | iso_code_class[i] = ISO_graphic_plane_1; | |
10329 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
10330 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
10331 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
10332 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
10333 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
10334 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
10335 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
10336 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
10337 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
10338 | ||
df7492f9 KH |
10339 | for (i = 0; i < 256; i++) |
10340 | { | |
10341 | emacs_mule_bytes[i] = 1; | |
10342 | } | |
7c78e542 KH |
10343 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
10344 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
10345 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
10346 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
10347 | } |
10348 | ||
10349 | #ifdef emacs | |
10350 | ||
dfcf069d | 10351 | void |
e0e989f6 KH |
10352 | syms_of_coding () |
10353 | { | |
df7492f9 | 10354 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
10355 | { |
10356 | Lisp_Object args[2]; | |
10357 | args[0] = QCtest; | |
10358 | args[1] = Qeq; | |
10359 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
10360 | } | |
df7492f9 KH |
10361 | |
10362 | staticpro (&Vsjis_coding_system); | |
10363 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 10364 | |
df7492f9 KH |
10365 | staticpro (&Vbig5_coding_system); |
10366 | Vbig5_coding_system = Qnil; | |
10367 | ||
24a73b0a KH |
10368 | staticpro (&Vcode_conversion_reused_workbuf); |
10369 | Vcode_conversion_reused_workbuf = Qnil; | |
10370 | ||
10371 | staticpro (&Vcode_conversion_workbuf_name); | |
10372 | Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*"); | |
e0e989f6 | 10373 | |
24a73b0a | 10374 | reused_workbuf_in_use = 0; |
df7492f9 KH |
10375 | |
10376 | DEFSYM (Qcharset, "charset"); | |
10377 | DEFSYM (Qtarget_idx, "target-idx"); | |
10378 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
10379 | Fset (Qcoding_system_history, Qnil); |
10380 | ||
9ce27fde | 10381 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 10382 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 10383 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
10384 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
10385 | ||
df7492f9 | 10386 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 10387 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
10388 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
10389 | ||
df7492f9 | 10390 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 10391 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10392 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
10393 | ||
df7492f9 | 10394 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 10395 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10396 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
10397 | ||
df7492f9 | 10398 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 10399 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
10400 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
10401 | ||
df7492f9 KH |
10402 | DEFSYM (Qcoding_system, "coding-system"); |
10403 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 10404 | |
df7492f9 KH |
10405 | DEFSYM (Qeol_type, "eol-type"); |
10406 | DEFSYM (Qunix, "unix"); | |
10407 | DEFSYM (Qdos, "dos"); | |
4ed46869 | 10408 | |
df7492f9 KH |
10409 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
10410 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
10411 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
10412 | DEFSYM (Qdefault_char, "default-char"); | |
10413 | DEFSYM (Qundecided, "undecided"); | |
10414 | DEFSYM (Qno_conversion, "no-conversion"); | |
10415 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 10416 | |
df7492f9 | 10417 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 10418 | |
df7492f9 | 10419 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 10420 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 10421 | |
df7492f9 | 10422 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
10423 | DEFSYM (Qbig, "big"); |
10424 | DEFSYM (Qlittle, "little"); | |
27901516 | 10425 | |
df7492f9 KH |
10426 | DEFSYM (Qshift_jis, "shift-jis"); |
10427 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 10428 | |
df7492f9 | 10429 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 10430 | |
df7492f9 | 10431 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 KH |
10432 | Fput (Qcoding_system_error, Qerror_conditions, |
10433 | Fcons (Qcoding_system_error, Fcons (Qerror, Qnil))); | |
10434 | Fput (Qcoding_system_error, Qerror_message, | |
9ce27fde | 10435 | build_string ("Invalid coding system")); |
4ed46869 | 10436 | |
05e6f5dc KH |
10437 | /* Intern this now in case it isn't already done. |
10438 | Setting this variable twice is harmless. | |
10439 | But don't staticpro it here--that is done in alloc.c. */ | |
10440 | Qchar_table_extra_slots = intern ("char-table-extra-slots"); | |
70c22245 | 10441 | |
df7492f9 | 10442 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 10443 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
10444 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
10445 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
10446 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 10447 | |
df7492f9 | 10448 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 10449 | |
df7492f9 | 10450 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 10451 | |
01378f49 | 10452 | DEFSYM (QCcategory, ":category"); |
a6f87d34 | 10453 | DEFSYM (QCmnemonic, ":mnemonic"); |
2133e2d1 | 10454 | DEFSYM (QCdefault_char, ":default-char"); |
a6f87d34 KH |
10455 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); |
10456 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
10457 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
10458 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 10459 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 10460 | |
df7492f9 KH |
10461 | Vcoding_category_table |
10462 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
10463 | staticpro (&Vcoding_category_table); | |
10464 | /* Followings are target of code detection. */ | |
10465 | ASET (Vcoding_category_table, coding_category_iso_7, | |
10466 | intern ("coding-category-iso-7")); | |
10467 | ASET (Vcoding_category_table, coding_category_iso_7_tight, | |
10468 | intern ("coding-category-iso-7-tight")); | |
10469 | ASET (Vcoding_category_table, coding_category_iso_8_1, | |
10470 | intern ("coding-category-iso-8-1")); | |
10471 | ASET (Vcoding_category_table, coding_category_iso_8_2, | |
10472 | intern ("coding-category-iso-8-2")); | |
10473 | ASET (Vcoding_category_table, coding_category_iso_7_else, | |
10474 | intern ("coding-category-iso-7-else")); | |
10475 | ASET (Vcoding_category_table, coding_category_iso_8_else, | |
10476 | intern ("coding-category-iso-8-else")); | |
a470d443 KH |
10477 | ASET (Vcoding_category_table, coding_category_utf_8_auto, |
10478 | intern ("coding-category-utf-8-auto")); | |
10479 | ASET (Vcoding_category_table, coding_category_utf_8_nosig, | |
df7492f9 | 10480 | intern ("coding-category-utf-8")); |
a470d443 KH |
10481 | ASET (Vcoding_category_table, coding_category_utf_8_sig, |
10482 | intern ("coding-category-utf-8-sig")); | |
df7492f9 KH |
10483 | ASET (Vcoding_category_table, coding_category_utf_16_be, |
10484 | intern ("coding-category-utf-16-be")); | |
ff563fce KH |
10485 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
10486 | intern ("coding-category-utf-16-auto")); | |
df7492f9 KH |
10487 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
10488 | intern ("coding-category-utf-16-le")); | |
10489 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, | |
10490 | intern ("coding-category-utf-16-be-nosig")); | |
10491 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, | |
10492 | intern ("coding-category-utf-16-le-nosig")); | |
10493 | ASET (Vcoding_category_table, coding_category_charset, | |
10494 | intern ("coding-category-charset")); | |
10495 | ASET (Vcoding_category_table, coding_category_sjis, | |
10496 | intern ("coding-category-sjis")); | |
10497 | ASET (Vcoding_category_table, coding_category_big5, | |
10498 | intern ("coding-category-big5")); | |
10499 | ASET (Vcoding_category_table, coding_category_ccl, | |
10500 | intern ("coding-category-ccl")); | |
10501 | ASET (Vcoding_category_table, coding_category_emacs_mule, | |
10502 | intern ("coding-category-emacs-mule")); | |
10503 | /* Followings are NOT target of code detection. */ | |
10504 | ASET (Vcoding_category_table, coding_category_raw_text, | |
10505 | intern ("coding-category-raw-text")); | |
10506 | ASET (Vcoding_category_table, coding_category_undecided, | |
10507 | intern ("coding-category-undecided")); | |
ecf488bc | 10508 | |
065e3595 KH |
10509 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
10510 | DEFSYM (Qinconsistent_eol, "inconsistent-eol"); | |
10511 | DEFSYM (Qinvalid_source, "invalid-source"); | |
10512 | DEFSYM (Qinterrupted, "interrupted"); | |
10513 | DEFSYM (Qinsufficient_memory, "insufficient-memory"); | |
44e8490d | 10514 | DEFSYM (Qcoding_system_define_form, "coding-system-define-form"); |
065e3595 | 10515 | |
4ed46869 KH |
10516 | defsubr (&Scoding_system_p); |
10517 | defsubr (&Sread_coding_system); | |
10518 | defsubr (&Sread_non_nil_coding_system); | |
10519 | defsubr (&Scheck_coding_system); | |
10520 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 10521 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 10522 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 10523 | defsubr (&Sunencodable_char_position); |
df7492f9 | 10524 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
10525 | defsubr (&Sdecode_coding_region); |
10526 | defsubr (&Sencode_coding_region); | |
10527 | defsubr (&Sdecode_coding_string); | |
10528 | defsubr (&Sencode_coding_string); | |
10529 | defsubr (&Sdecode_sjis_char); | |
10530 | defsubr (&Sencode_sjis_char); | |
10531 | defsubr (&Sdecode_big5_char); | |
10532 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 10533 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 10534 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 10535 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 10536 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 10537 | defsubr (&Skeyboard_coding_system); |
a5d301df | 10538 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 10539 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 10540 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 10541 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 10542 | defsubr (&Scoding_system_put); |
df7492f9 KH |
10543 | defsubr (&Scoding_system_base); |
10544 | defsubr (&Scoding_system_plist); | |
10545 | defsubr (&Scoding_system_aliases); | |
10546 | defsubr (&Scoding_system_eol_type); | |
10547 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 10548 | |
4608c386 | 10549 | DEFVAR_LISP ("coding-system-list", &Vcoding_system_list, |
48b0f3ae PJ |
10550 | doc: /* List of coding systems. |
10551 | ||
10552 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 10553 | updated by the functions `define-coding-system' and |
48b0f3ae | 10554 | `define-coding-system-alias'. */); |
4608c386 KH |
10555 | Vcoding_system_list = Qnil; |
10556 | ||
10557 | DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist, | |
48b0f3ae PJ |
10558 | doc: /* Alist of coding system names. |
10559 | Each element is one element list of coding system name. | |
446dcd75 | 10560 | This variable is given to `completing-read' as COLLECTION argument. |
48b0f3ae PJ |
10561 | |
10562 | Do not alter the value of this variable manually. This variable should be | |
10563 | updated by the functions `make-coding-system' and | |
10564 | `define-coding-system-alias'. */); | |
4608c386 KH |
10565 | Vcoding_system_alist = Qnil; |
10566 | ||
4ed46869 | 10567 | DEFVAR_LISP ("coding-category-list", &Vcoding_category_list, |
48b0f3ae PJ |
10568 | doc: /* List of coding-categories (symbols) ordered by priority. |
10569 | ||
10570 | On detecting a coding system, Emacs tries code detection algorithms | |
10571 | associated with each coding-category one by one in this order. When | |
10572 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
10573 | system bound to the corresponding coding-category is selected. |
10574 | ||
42205607 | 10575 | Don't modify this variable directly, but use `set-coding-priority'. */); |
4ed46869 KH |
10576 | { |
10577 | int i; | |
10578 | ||
10579 | Vcoding_category_list = Qnil; | |
df7492f9 | 10580 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 10581 | Vcoding_category_list |
d46c5b12 KH |
10582 | = Fcons (XVECTOR (Vcoding_category_table)->contents[i], |
10583 | Vcoding_category_list); | |
4ed46869 KH |
10584 | } |
10585 | ||
10586 | DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read, | |
48b0f3ae PJ |
10587 | doc: /* Specify the coding system for read operations. |
10588 | It is useful to bind this variable with `let', but do not set it globally. | |
10589 | If the value is a coding system, it is used for decoding on read operation. | |
446dcd75 JB |
10590 | If not, an appropriate element is used from one of the coding system alists. |
10591 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae | 10592 | `process-coding-system-alist', and `network-coding-system-alist'. */); |
4ed46869 KH |
10593 | Vcoding_system_for_read = Qnil; |
10594 | ||
10595 | DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write, | |
48b0f3ae PJ |
10596 | doc: /* Specify the coding system for write operations. |
10597 | Programs bind this variable with `let', but you should not set it globally. | |
10598 | If the value is a coding system, it is used for encoding of output, | |
10599 | when writing it to a file and when sending it to a file or subprocess. | |
10600 | ||
10601 | If this does not specify a coding system, an appropriate element | |
446dcd75 JB |
10602 | is used from one of the coding system alists. |
10603 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae PJ |
10604 | `process-coding-system-alist', and `network-coding-system-alist'. |
10605 | For output to files, if the above procedure does not specify a coding system, | |
10606 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
10607 | Vcoding_system_for_write = Qnil; |
10608 | ||
10609 | DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used, | |
df7492f9 KH |
10610 | doc: /* |
10611 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
10612 | Vlast_coding_system_used = Qnil; |
10613 | ||
065e3595 KH |
10614 | DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error, |
10615 | doc: /* | |
10616 | Error status of the last code conversion. | |
10617 | ||
10618 | When an error was detected in the last code conversion, this variable | |
10619 | is set to one of the following symbols. | |
10620 | `insufficient-source' | |
10621 | `inconsistent-eol' | |
10622 | `invalid-source' | |
10623 | `interrupted' | |
10624 | `insufficient-memory' | |
10625 | When no error was detected, the value doesn't change. So, to check | |
10626 | the error status of a code conversion by this variable, you must | |
10627 | explicitly set this variable to nil before performing code | |
10628 | conversion. */); | |
10629 | Vlast_code_conversion_error = Qnil; | |
10630 | ||
9ce27fde | 10631 | DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion, |
df7492f9 KH |
10632 | doc: /* |
10633 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
10634 | See info node `Coding Systems' and info node `Text and Binary' concerning |
10635 | such conversion. */); | |
9ce27fde KH |
10636 | inhibit_eol_conversion = 0; |
10637 | ||
ed29121d | 10638 | DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system, |
df7492f9 KH |
10639 | doc: /* |
10640 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
10641 | Bind it to t if the process output is to be treated as if it were a file |
10642 | read from some filesystem. */); | |
ed29121d EZ |
10643 | inherit_process_coding_system = 0; |
10644 | ||
02ba4723 | 10645 | DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist, |
df7492f9 KH |
10646 | doc: /* |
10647 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
10648 | The format is ((PATTERN . VAL) ...), |
10649 | where PATTERN is a regular expression matching a file name, | |
10650 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10651 | If VAL is a coding system, it is used for both decoding and encoding | |
10652 | the file contents. | |
10653 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10654 | and the cdr part is used for encoding. | |
10655 | If VAL is a function symbol, the function must return a coding system | |
2c53e699 KH |
10656 | or a cons of coding systems which are used as above. The function is |
10657 | called with an argument that is a list of the arguments with which | |
5a0bbd9a KH |
10658 | `find-operation-coding-system' was called. If the function can't decide |
10659 | a coding system, it can return `undecided' so that the normal | |
10660 | code-detection is performed. | |
48b0f3ae PJ |
10661 | |
10662 | See also the function `find-operation-coding-system' | |
10663 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
10664 | Vfile_coding_system_alist = Qnil; |
10665 | ||
10666 | DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist, | |
df7492f9 KH |
10667 | doc: /* |
10668 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
10669 | The format is ((PATTERN . VAL) ...), |
10670 | where PATTERN is a regular expression matching a program name, | |
10671 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10672 | If VAL is a coding system, it is used for both decoding what received | |
10673 | from the program and encoding what sent to the program. | |
10674 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10675 | and the cdr part is used for encoding. | |
10676 | If VAL is a function symbol, the function must return a coding system | |
10677 | or a cons of coding systems which are used as above. | |
10678 | ||
10679 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
10680 | Vprocess_coding_system_alist = Qnil; |
10681 | ||
10682 | DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist, | |
df7492f9 KH |
10683 | doc: /* |
10684 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
10685 | The format is ((PATTERN . VAL) ...), |
10686 | where PATTERN is a regular expression matching a network service name | |
10687 | or is a port number to connect to, | |
10688 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10689 | If VAL is a coding system, it is used for both decoding what received | |
10690 | from the network stream and encoding what sent to the network stream. | |
10691 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10692 | and the cdr part is used for encoding. | |
10693 | If VAL is a function symbol, the function must return a coding system | |
10694 | or a cons of coding systems which are used as above. | |
10695 | ||
10696 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 10697 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 10698 | |
68c45bf0 | 10699 | DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system, |
75205970 RS |
10700 | doc: /* Coding system to use with system messages. |
10701 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
10702 | Vlocale_coding_system = Qnil; |
10703 | ||
005f0d35 | 10704 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
7722baf9 | 10705 | DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix, |
df7492f9 KH |
10706 | doc: /* |
10707 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
7722baf9 | 10708 | eol_mnemonic_unix = build_string (":"); |
4ed46869 | 10709 | |
7722baf9 | 10710 | DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos, |
df7492f9 KH |
10711 | doc: /* |
10712 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
7722baf9 | 10713 | eol_mnemonic_dos = build_string ("\\"); |
4ed46869 | 10714 | |
7722baf9 | 10715 | DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac, |
df7492f9 KH |
10716 | doc: /* |
10717 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
7722baf9 | 10718 | eol_mnemonic_mac = build_string ("/"); |
4ed46869 | 10719 | |
7722baf9 | 10720 | DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided, |
df7492f9 KH |
10721 | doc: /* |
10722 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
7722baf9 | 10723 | eol_mnemonic_undecided = build_string (":"); |
4ed46869 | 10724 | |
84fbb8a0 | 10725 | DEFVAR_LISP ("enable-character-translation", &Venable_character_translation, |
df7492f9 KH |
10726 | doc: /* |
10727 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 10728 | Venable_character_translation = Qt; |
bdd9fb48 | 10729 | |
f967223b | 10730 | DEFVAR_LISP ("standard-translation-table-for-decode", |
48b0f3ae PJ |
10731 | &Vstandard_translation_table_for_decode, |
10732 | doc: /* Table for translating characters while decoding. */); | |
f967223b | 10733 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 10734 | |
f967223b | 10735 | DEFVAR_LISP ("standard-translation-table-for-encode", |
48b0f3ae PJ |
10736 | &Vstandard_translation_table_for_encode, |
10737 | doc: /* Table for translating characters while encoding. */); | |
f967223b | 10738 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 10739 | |
df7492f9 | 10740 | DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table, |
48b0f3ae PJ |
10741 | doc: /* Alist of charsets vs revision numbers. |
10742 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
10743 | designate it with the escape sequence identifying revision (cdr part |
10744 | of the element). */); | |
10745 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
10746 | |
10747 | DEFVAR_LISP ("default-process-coding-system", | |
10748 | &Vdefault_process_coding_system, | |
48b0f3ae PJ |
10749 | doc: /* Cons of coding systems used for process I/O by default. |
10750 | The car part is used for decoding a process output, | |
10751 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 10752 | Vdefault_process_coding_system = Qnil; |
c4825358 | 10753 | |
3f003981 | 10754 | DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table, |
df7492f9 KH |
10755 | doc: /* |
10756 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
10757 | This is a vector of length 256. |
10758 | If Nth element is non-nil, the existence of code N in a file | |
10759 | \(or output of subprocess) doesn't prevent it to be detected as | |
10760 | a coding system of ISO 2022 variant which has a flag | |
10761 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
10762 | or reading output of a subprocess. | |
446dcd75 | 10763 | Only 128th through 159th elements have a meaning. */); |
3f003981 | 10764 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
10765 | |
10766 | DEFVAR_LISP ("select-safe-coding-system-function", | |
10767 | &Vselect_safe_coding_system_function, | |
df7492f9 KH |
10768 | doc: /* |
10769 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
10770 | |
10771 | If set, this function is called to force a user to select a proper | |
10772 | coding system which can encode the text in the case that a default | |
fdecf907 GM |
10773 | coding system used in each operation can't encode the text. The |
10774 | function should take care that the buffer is not modified while | |
10775 | the coding system is being selected. | |
48b0f3ae PJ |
10776 | |
10777 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
10778 | Vselect_safe_coding_system_function = Qnil; |
10779 | ||
5d5bf4d8 KH |
10780 | DEFVAR_BOOL ("coding-system-require-warning", |
10781 | &coding_system_require_warning, | |
10782 | doc: /* Internal use only. | |
6b89e3aa KH |
10783 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
10784 | called even if `coding-system-for-write' is non-nil. The command | |
10785 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
10786 | coding_system_require_warning = 0; |
10787 | ||
10788 | ||
22ab2303 | 10789 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
74383408 | 10790 | &inhibit_iso_escape_detection, |
df7492f9 | 10791 | doc: /* |
97b1b294 | 10792 | If non-nil, Emacs ignores ISO-2022 escape sequences during code detection. |
48b0f3ae | 10793 | |
97b1b294 EZ |
10794 | When Emacs reads text, it tries to detect how the text is encoded. |
10795 | This code detection is sensitive to escape sequences. If Emacs sees | |
10796 | a valid ISO-2022 escape sequence, it assumes the text is encoded in one | |
10797 | of the ISO2022 encodings, and decodes text by the corresponding coding | |
10798 | system (e.g. `iso-2022-7bit'). | |
48b0f3ae PJ |
10799 | |
10800 | However, there may be a case that you want to read escape sequences in | |
10801 | a file as is. In such a case, you can set this variable to non-nil. | |
97b1b294 EZ |
10802 | Then the code detection will ignore any escape sequences, and no text is |
10803 | detected as encoded in some ISO-2022 encoding. The result is that all | |
48b0f3ae PJ |
10804 | escape sequences become visible in a buffer. |
10805 | ||
10806 | The default value is nil, and it is strongly recommended not to change | |
10807 | it. That is because many Emacs Lisp source files that contain | |
10808 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
10809 | in Emacs's distribution, and they won't be decoded correctly on | |
10810 | reading if you suppress escape sequence detection. | |
10811 | ||
10812 | The other way to read escape sequences in a file without decoding is | |
97b1b294 | 10813 | to explicitly specify some coding system that doesn't use ISO-2022 |
48b0f3ae | 10814 | escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */); |
74383408 | 10815 | inhibit_iso_escape_detection = 0; |
002fdb44 | 10816 | |
97b1b294 EZ |
10817 | DEFVAR_BOOL ("inhibit-null-byte-detection", |
10818 | &inhibit_null_byte_detection, | |
10819 | doc: /* If non-nil, Emacs ignores null bytes on code detection. | |
10820 | By default, Emacs treats it as binary data, and does not attempt to | |
10821 | decode it. The effect is as if you specified `no-conversion' for | |
10822 | reading that text. | |
10823 | ||
10824 | Set this to non-nil when a regular text happens to include null bytes. | |
10825 | Examples are Index nodes of Info files and null-byte delimited output | |
10826 | from GNU Find and GNU Grep. Emacs will then ignore the null bytes and | |
10827 | decode text as usual. */); | |
10828 | inhibit_null_byte_detection = 0; | |
10829 | ||
002fdb44 | 10830 | DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input, |
15c8f9d1 | 10831 | doc: /* Char table for translating self-inserting characters. |
446dcd75 | 10832 | This is applied to the result of input methods, not their input. |
8434d0b8 EZ |
10833 | See also `keyboard-translate-table'. |
10834 | ||
10835 | Use of this variable for character code unification was rendered | |
10836 | obsolete in Emacs 23.1 and later, since Unicode is now the basis of | |
10837 | internal character representation. */); | |
002fdb44 | 10838 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 10839 | |
2c78b7e1 KH |
10840 | { |
10841 | Lisp_Object args[coding_arg_max]; | |
8f924df7 | 10842 | Lisp_Object plist[16]; |
2c78b7e1 KH |
10843 | int i; |
10844 | ||
10845 | for (i = 0; i < coding_arg_max; i++) | |
10846 | args[i] = Qnil; | |
10847 | ||
10848 | plist[0] = intern (":name"); | |
10849 | plist[1] = args[coding_arg_name] = Qno_conversion; | |
10850 | plist[2] = intern (":mnemonic"); | |
10851 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); | |
10852 | plist[4] = intern (":coding-type"); | |
10853 | plist[5] = args[coding_arg_coding_type] = Qraw_text; | |
10854 | plist[6] = intern (":ascii-compatible-p"); | |
10855 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; | |
10856 | plist[8] = intern (":default-char"); | |
10857 | plist[9] = args[coding_arg_default_char] = make_number (0); | |
8f924df7 KH |
10858 | plist[10] = intern (":for-unibyte"); |
10859 | plist[11] = args[coding_arg_for_unibyte] = Qt; | |
10860 | plist[12] = intern (":docstring"); | |
10861 | plist[13] = build_string ("Do no conversion.\n\ | |
2c78b7e1 KH |
10862 | \n\ |
10863 | When you visit a file with this coding, the file is read into a\n\ | |
10864 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
10865 | character."); | |
8f924df7 KH |
10866 | plist[14] = intern (":eol-type"); |
10867 | plist[15] = args[coding_arg_eol_type] = Qunix; | |
10868 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 10869 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
10870 | |
10871 | plist[1] = args[coding_arg_name] = Qundecided; | |
10872 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
10873 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
10874 | /* This is already set. | |
35befdaa | 10875 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
ae6f73fa KH |
10876 | plist[8] = intern (":charset-list"); |
10877 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); | |
10878 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
10879 | plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding."); | |
10880 | plist[15] = args[coding_arg_eol_type] = Qnil; | |
10881 | args[coding_arg_plist] = Flist (16, plist); | |
10882 | Fdefine_coding_system_internal (coding_arg_max, args); | |
2c78b7e1 KH |
10883 | } |
10884 | ||
2c78b7e1 | 10885 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
10886 | |
10887 | { | |
10888 | int i; | |
10889 | ||
10890 | for (i = 0; i < coding_category_max; i++) | |
10891 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
10892 | } | |
fcbcfb64 KH |
10893 | #if defined (MSDOS) || defined (WINDOWSNT) |
10894 | system_eol_type = Qdos; | |
10895 | #else | |
10896 | system_eol_type = Qunix; | |
10897 | #endif | |
10898 | staticpro (&system_eol_type); | |
4ed46869 KH |
10899 | } |
10900 | ||
68c45bf0 PE |
10901 | char * |
10902 | emacs_strerror (error_number) | |
10903 | int error_number; | |
10904 | { | |
10905 | char *str; | |
10906 | ||
ca9c0567 | 10907 | synchronize_system_messages_locale (); |
68c45bf0 PE |
10908 | str = strerror (error_number); |
10909 | ||
10910 | if (! NILP (Vlocale_coding_system)) | |
10911 | { | |
10912 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
10913 | Vlocale_coding_system, | |
10914 | 0); | |
d5db4077 | 10915 | str = (char *) SDATA (dec); |
68c45bf0 PE |
10916 | } |
10917 | ||
10918 | return str; | |
10919 | } | |
10920 | ||
4ed46869 | 10921 | #endif /* emacs */ |
9ffd559c KH |
10922 | |
10923 | /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d | |
10924 | (do not change this comment) */ |