Commit | Line | Data |
---|---|---|
9542cb1f | 1 | /* Coding system handler (conversion, detection, etc). |
0b5538bd | 2 | Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
ce03bf76 KH |
3 | Copyright (C) 1995, 1997, 1998, 2002, 2003, 2004, 2005 |
4 | National Institute of Advanced Industrial Science and Technology (AIST) | |
5 | Registration Number H14PRO021 | |
8f924df7 | 6 | Copyright (C) 2003 |
df7492f9 KH |
7 | National Institute of Advanced Industrial Science and Technology (AIST) |
8 | Registration Number H13PRO009 | |
4ed46869 | 9 | |
369314dc KH |
10 | This file is part of GNU Emacs. |
11 | ||
12 | GNU Emacs is free software; you can redistribute it and/or modify | |
13 | it under the terms of the GNU General Public License as published by | |
14 | the Free Software Foundation; either version 2, or (at your option) | |
15 | any later version. | |
4ed46869 | 16 | |
369314dc KH |
17 | GNU Emacs is distributed in the hope that it will be useful, |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | GNU General Public License for more details. | |
4ed46869 | 21 | |
369314dc KH |
22 | You should have received a copy of the GNU General Public License |
23 | along with GNU Emacs; see the file COPYING. If not, write to | |
4fc5845f LK |
24 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | Boston, MA 02110-1301, USA. */ | |
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" | |
4ed46869 | 301 | |
df7492f9 | 302 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 303 | |
df7492f9 | 304 | Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type; |
1965cb73 DL |
305 | Lisp_Object Qunix, Qdos; |
306 | extern Lisp_Object Qmac; /* frame.c */ | |
4ed46869 KH |
307 | Lisp_Object Qbuffer_file_coding_system; |
308 | Lisp_Object Qpost_read_conversion, Qpre_write_conversion; | |
df7492f9 | 309 | Lisp_Object Qdefault_char; |
27901516 | 310 | Lisp_Object Qno_conversion, Qundecided; |
df7492f9 | 311 | Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5; |
b49a1807 | 312 | Lisp_Object Qbig, Qlittle; |
bb0115a2 | 313 | Lisp_Object Qcoding_system_history; |
1397dc18 | 314 | Lisp_Object Qvalid_codes; |
a6f87d34 KH |
315 | Lisp_Object QCcategory, QCmnemonic, QCdefalut_char; |
316 | Lisp_Object QCdecode_translation_table, QCencode_translation_table; | |
317 | Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
35befdaa | 318 | Lisp_Object QCascii_compatible_p; |
4ed46869 KH |
319 | |
320 | extern Lisp_Object Qinsert_file_contents, Qwrite_region; | |
387f6ba5 | 321 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 KH |
322 | Lisp_Object Qstart_process, Qopen_network_stream; |
323 | Lisp_Object Qtarget_idx; | |
324 | ||
065e3595 KH |
325 | Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source; |
326 | Lisp_Object Qinterrupted, Qinsufficient_memory; | |
327 | ||
5d5bf4d8 KH |
328 | int coding_system_require_warning; |
329 | ||
d46c5b12 KH |
330 | Lisp_Object Vselect_safe_coding_system_function; |
331 | ||
7722baf9 EZ |
332 | /* Mnemonic string for each format of end-of-line. */ |
333 | Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac; | |
334 | /* Mnemonic string to indicate format of end-of-line is not yet | |
4ed46869 | 335 | decided. */ |
7722baf9 | 336 | Lisp_Object eol_mnemonic_undecided; |
4ed46869 KH |
337 | |
338 | #ifdef emacs | |
339 | ||
4608c386 KH |
340 | Lisp_Object Vcoding_system_list, Vcoding_system_alist; |
341 | ||
342 | Lisp_Object Qcoding_system_p, Qcoding_system_error; | |
4ed46869 | 343 | |
d46c5b12 KH |
344 | /* Coding system emacs-mule and raw-text are for converting only |
345 | end-of-line format. */ | |
346 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 347 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 348 | |
4ed46869 KH |
349 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
350 | routines by the following three variables. */ | |
351 | /* Coding-system for reading files and receiving data from process. */ | |
352 | Lisp_Object Vcoding_system_for_read; | |
353 | /* Coding-system for writing files and sending data to process. */ | |
354 | Lisp_Object Vcoding_system_for_write; | |
355 | /* Coding-system actually used in the latest I/O. */ | |
356 | Lisp_Object Vlast_coding_system_used; | |
065e3595 KH |
357 | /* Set to non-nil when an error is detected while code conversion. */ |
358 | Lisp_Object Vlast_code_conversion_error; | |
c4825358 | 359 | /* A vector of length 256 which contains information about special |
94487c4e | 360 | Latin codes (especially for dealing with Microsoft codes). */ |
3f003981 | 361 | Lisp_Object Vlatin_extra_code_table; |
c4825358 | 362 | |
9ce27fde KH |
363 | /* Flag to inhibit code conversion of end-of-line format. */ |
364 | int inhibit_eol_conversion; | |
365 | ||
74383408 KH |
366 | /* Flag to inhibit ISO2022 escape sequence detection. */ |
367 | int inhibit_iso_escape_detection; | |
368 | ||
ed29121d EZ |
369 | /* Flag to make buffer-file-coding-system inherit from process-coding. */ |
370 | int inherit_process_coding_system; | |
371 | ||
c4825358 | 372 | /* Coding system to be used to encode text for terminal display. */ |
4ed46869 KH |
373 | struct coding_system terminal_coding; |
374 | ||
c4825358 KH |
375 | /* Coding system to be used to encode text for terminal display when |
376 | terminal coding system is nil. */ | |
377 | struct coding_system safe_terminal_coding; | |
378 | ||
379 | /* Coding system of what is sent from terminal keyboard. */ | |
4ed46869 KH |
380 | struct coding_system keyboard_coding; |
381 | ||
02ba4723 KH |
382 | Lisp_Object Vfile_coding_system_alist; |
383 | Lisp_Object Vprocess_coding_system_alist; | |
384 | Lisp_Object Vnetwork_coding_system_alist; | |
4ed46869 | 385 | |
68c45bf0 PE |
386 | Lisp_Object Vlocale_coding_system; |
387 | ||
4ed46869 KH |
388 | #endif /* emacs */ |
389 | ||
f967223b KH |
390 | /* Flag to tell if we look up translation table on character code |
391 | conversion. */ | |
84fbb8a0 | 392 | Lisp_Object Venable_character_translation; |
f967223b KH |
393 | /* Standard translation table to look up on decoding (reading). */ |
394 | Lisp_Object Vstandard_translation_table_for_decode; | |
395 | /* Standard translation table to look up on encoding (writing). */ | |
396 | Lisp_Object Vstandard_translation_table_for_encode; | |
84fbb8a0 | 397 | |
f967223b KH |
398 | Lisp_Object Qtranslation_table; |
399 | Lisp_Object Qtranslation_table_id; | |
400 | Lisp_Object Qtranslation_table_for_decode; | |
401 | Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 KH |
402 | |
403 | /* Alist of charsets vs revision number. */ | |
df7492f9 | 404 | static Lisp_Object Vcharset_revision_table; |
4ed46869 | 405 | |
02ba4723 KH |
406 | /* Default coding systems used for process I/O. */ |
407 | Lisp_Object Vdefault_process_coding_system; | |
408 | ||
002fdb44 DL |
409 | /* Char table for translating Quail and self-inserting input. */ |
410 | Lisp_Object Vtranslation_table_for_input; | |
411 | ||
df7492f9 KH |
412 | /* Two special coding systems. */ |
413 | Lisp_Object Vsjis_coding_system; | |
414 | Lisp_Object Vbig5_coding_system; | |
415 | ||
df7492f9 KH |
416 | /* ISO2022 section */ |
417 | ||
418 | #define CODING_ISO_INITIAL(coding, reg) \ | |
419 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
420 | coding_attr_iso_initial), \ | |
421 | reg))) | |
422 | ||
423 | ||
424 | #define CODING_ISO_REQUEST(coding, charset_id) \ | |
425 | ((charset_id <= (coding)->max_charset_id \ | |
426 | ? (coding)->safe_charsets[charset_id] \ | |
427 | : -1)) | |
428 | ||
429 | ||
430 | #define CODING_ISO_FLAGS(coding) \ | |
431 | ((coding)->spec.iso_2022.flags) | |
432 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
433 | ((coding)->spec.iso_2022.current_designation[reg]) | |
434 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
435 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
436 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
437 | ((coding)->spec.iso_2022.single_shifting) | |
438 | #define CODING_ISO_BOL(coding) \ | |
439 | ((coding)->spec.iso_2022.bol) | |
440 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
441 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
442 | ||
443 | /* Control characters of ISO2022. */ | |
444 | /* code */ /* function */ | |
445 | #define ISO_CODE_LF 0x0A /* line-feed */ | |
446 | #define ISO_CODE_CR 0x0D /* carriage-return */ | |
447 | #define ISO_CODE_SO 0x0E /* shift-out */ | |
448 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
449 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
450 | #define ISO_CODE_ESC 0x1B /* escape */ | |
451 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
452 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
453 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
454 | ||
455 | /* All code (1-byte) of ISO2022 is classified into one of the | |
456 | followings. */ | |
457 | enum iso_code_class_type | |
458 | { | |
459 | ISO_control_0, /* Control codes in the range | |
460 | 0x00..0x1F and 0x7F, except for the | |
461 | following 5 codes. */ | |
df7492f9 KH |
462 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
463 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
464 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
465 | ISO_escape, /* ISO_CODE_SO (0x1B) */ | |
466 | ISO_control_1, /* Control codes in the range | |
467 | 0x80..0x9F, except for the | |
468 | following 3 codes. */ | |
469 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
470 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
471 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
472 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
473 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
474 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
475 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
476 | }; | |
05e6f5dc | 477 | |
df7492f9 KH |
478 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
479 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 480 | |
df7492f9 KH |
481 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
482 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
483 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 484 | |
df7492f9 KH |
485 | /* If set, reset graphic planes and registers at end-of-line to the |
486 | initial state. */ | |
487 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 488 | |
df7492f9 KH |
489 | /* If set, reset graphic planes and registers before any control |
490 | characters to the initial state. */ | |
491 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 492 | |
df7492f9 KH |
493 | /* If set, encode by 7-bit environment. */ |
494 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 495 | |
df7492f9 KH |
496 | /* If set, use locking-shift function. */ |
497 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 498 | |
df7492f9 KH |
499 | /* If set, use single-shift function. Overwrite |
500 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
501 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 502 | |
df7492f9 KH |
503 | /* If set, use designation escape sequence. */ |
504 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 505 | |
df7492f9 KH |
506 | /* If set, produce revision number sequence. */ |
507 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 508 | |
df7492f9 KH |
509 | /* If set, produce ISO6429's direction specifying sequence. */ |
510 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 511 | |
df7492f9 KH |
512 | /* If set, assume designation states are reset at beginning of line on |
513 | output. */ | |
514 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 515 | |
df7492f9 KH |
516 | /* If set, designation sequence should be placed at beginning of line |
517 | on output. */ | |
518 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 519 | |
df7492f9 KH |
520 | /* If set, do not encode unsafe charactes on output. */ |
521 | #define CODING_ISO_FLAG_SAFE 0x0800 | |
aa72b389 | 522 | |
df7492f9 KH |
523 | /* If set, extra latin codes (128..159) are accepted as a valid code |
524 | on input. */ | |
525 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 526 | |
df7492f9 | 527 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 528 | |
df7492f9 | 529 | #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 |
aa72b389 | 530 | |
bf16eb23 | 531 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 532 | |
bf16eb23 | 533 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 534 | |
bf16eb23 | 535 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 536 | |
df7492f9 KH |
537 | /* A character to be produced on output if encoding of the original |
538 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
539 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 540 | |
4ed46869 | 541 | |
df7492f9 KH |
542 | /* UTF-16 section */ |
543 | #define CODING_UTF_16_BOM(coding) \ | |
544 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 545 | |
df7492f9 KH |
546 | #define CODING_UTF_16_ENDIAN(coding) \ |
547 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 548 | |
df7492f9 KH |
549 | #define CODING_UTF_16_SURROGATE(coding) \ |
550 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 551 | |
4ed46869 | 552 | |
df7492f9 KH |
553 | /* CCL section */ |
554 | #define CODING_CCL_DECODER(coding) \ | |
555 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
556 | #define CODING_CCL_ENCODER(coding) \ | |
557 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
558 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 559 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 560 | |
5a936b46 | 561 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 562 | |
df7492f9 KH |
563 | enum coding_category |
564 | { | |
565 | coding_category_iso_7, | |
566 | coding_category_iso_7_tight, | |
567 | coding_category_iso_8_1, | |
568 | coding_category_iso_8_2, | |
569 | coding_category_iso_7_else, | |
570 | coding_category_iso_8_else, | |
571 | coding_category_utf_8, | |
572 | coding_category_utf_16_auto, | |
573 | coding_category_utf_16_be, | |
574 | coding_category_utf_16_le, | |
575 | coding_category_utf_16_be_nosig, | |
576 | coding_category_utf_16_le_nosig, | |
577 | coding_category_charset, | |
578 | coding_category_sjis, | |
579 | coding_category_big5, | |
580 | coding_category_ccl, | |
581 | coding_category_emacs_mule, | |
582 | /* All above are targets of code detection. */ | |
583 | coding_category_raw_text, | |
584 | coding_category_undecided, | |
585 | coding_category_max | |
586 | }; | |
587 | ||
588 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
589 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
590 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
591 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
592 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
593 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
594 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
595 | #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8) | |
b49a1807 | 596 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
597 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
598 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
599 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
600 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
601 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
602 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
603 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
604 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
605 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 606 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
607 | |
608 | /* This value is returned if detect_coding_mask () find nothing other | |
609 | than ASCII characters. */ | |
610 | #define CATEGORY_MASK_ANY \ | |
611 | (CATEGORY_MASK_ISO_7 \ | |
612 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
613 | | CATEGORY_MASK_ISO_8_1 \ | |
614 | | CATEGORY_MASK_ISO_8_2 \ | |
615 | | CATEGORY_MASK_ISO_7_ELSE \ | |
616 | | CATEGORY_MASK_ISO_8_ELSE \ | |
617 | | CATEGORY_MASK_UTF_8 \ | |
618 | | CATEGORY_MASK_UTF_16_BE \ | |
619 | | CATEGORY_MASK_UTF_16_LE \ | |
620 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
621 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
622 | | CATEGORY_MASK_CHARSET \ | |
623 | | CATEGORY_MASK_SJIS \ | |
624 | | CATEGORY_MASK_BIG5 \ | |
625 | | CATEGORY_MASK_CCL \ | |
626 | | CATEGORY_MASK_EMACS_MULE) | |
627 | ||
628 | ||
629 | #define CATEGORY_MASK_ISO_7BIT \ | |
630 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
631 | ||
632 | #define CATEGORY_MASK_ISO_8BIT \ | |
633 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
634 | ||
635 | #define CATEGORY_MASK_ISO_ELSE \ | |
636 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
637 | ||
638 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
639 | (CATEGORY_MASK_ISO_7 \ | |
640 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
641 | | CATEGORY_MASK_ISO_7_ELSE \ | |
642 | | CATEGORY_MASK_ISO_8_ELSE) | |
643 | ||
644 | #define CATEGORY_MASK_ISO \ | |
645 | ( CATEGORY_MASK_ISO_7BIT \ | |
646 | | CATEGORY_MASK_ISO_8BIT \ | |
647 | | CATEGORY_MASK_ISO_ELSE) | |
648 | ||
649 | #define CATEGORY_MASK_UTF_16 \ | |
650 | (CATEGORY_MASK_UTF_16_BE \ | |
651 | | CATEGORY_MASK_UTF_16_LE \ | |
652 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
653 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
654 | ||
655 | ||
656 | /* List of symbols `coding-category-xxx' ordered by priority. This | |
657 | variable is exposed to Emacs Lisp. */ | |
658 | static Lisp_Object Vcoding_category_list; | |
659 | ||
660 | /* Table of coding categories (Lisp symbols). This variable is for | |
661 | internal use oly. */ | |
662 | static Lisp_Object Vcoding_category_table; | |
663 | ||
664 | /* Table of coding-categories ordered by priority. */ | |
665 | static enum coding_category coding_priorities[coding_category_max]; | |
666 | ||
667 | /* Nth element is a coding context for the coding system bound to the | |
668 | Nth coding category. */ | |
669 | static struct coding_system coding_categories[coding_category_max]; | |
670 | ||
df7492f9 KH |
671 | /*** Commonly used macros and functions ***/ |
672 | ||
673 | #ifndef min | |
674 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
675 | #endif | |
676 | #ifndef max | |
677 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
678 | #endif | |
4ed46869 | 679 | |
24a73b0a KH |
680 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
681 | do { \ | |
682 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
683 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 684 | } while (0) |
4ed46869 | 685 | |
4ed46869 | 686 | |
df7492f9 KH |
687 | /* Safely get one byte from the source text pointed by SRC which ends |
688 | at SRC_END, and set C to that byte. If there are not enough bytes | |
065e3595 KH |
689 | in the source, it jumps to `no_more_source'. If multibytep is |
690 | nonzero, and a multibyte character is found at SRC, set C to the | |
691 | negative value of the character code. The caller should declare | |
692 | and set these variables appropriately in advance: | |
693 | src, src_end, multibytep */ | |
aa72b389 | 694 | |
065e3595 KH |
695 | #define ONE_MORE_BYTE(c) \ |
696 | do { \ | |
697 | if (src == src_end) \ | |
698 | { \ | |
699 | if (src_base < src) \ | |
700 | record_conversion_result \ | |
701 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
702 | goto no_more_source; \ | |
703 | } \ | |
704 | c = *src++; \ | |
705 | if (multibytep && (c & 0x80)) \ | |
706 | { \ | |
707 | if ((c & 0xFE) == 0xC0) \ | |
708 | c = ((c & 1) << 6) | *src++; \ | |
709 | else \ | |
710 | { \ | |
35befdaa KH |
711 | src--; \ |
712 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
713 | record_conversion_result \ |
714 | (coding, CODING_RESULT_INVALID_SRC); \ | |
715 | } \ | |
716 | } \ | |
717 | consumed_chars++; \ | |
aa72b389 KH |
718 | } while (0) |
719 | ||
aa72b389 | 720 | |
065e3595 KH |
721 | #define ONE_MORE_BYTE_NO_CHECK(c) \ |
722 | do { \ | |
723 | c = *src++; \ | |
724 | if (multibytep && (c & 0x80)) \ | |
725 | { \ | |
726 | if ((c & 0xFE) == 0xC0) \ | |
727 | c = ((c & 1) << 6) | *src++; \ | |
728 | else \ | |
729 | { \ | |
35befdaa KH |
730 | src--; \ |
731 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
732 | record_conversion_result \ |
733 | (coding, CODING_RESULT_INVALID_SRC); \ | |
734 | } \ | |
735 | } \ | |
736 | consumed_chars++; \ | |
aa72b389 KH |
737 | } while (0) |
738 | ||
aa72b389 | 739 | |
df7492f9 KH |
740 | /* Store a byte C in the place pointed by DST and increment DST to the |
741 | next free point, and increment PRODUCED_CHARS. The caller should | |
742 | assure that C is 0..127, and declare and set the variable `dst' | |
743 | appropriately in advance. | |
744 | */ | |
aa72b389 KH |
745 | |
746 | ||
df7492f9 KH |
747 | #define EMIT_ONE_ASCII_BYTE(c) \ |
748 | do { \ | |
749 | produced_chars++; \ | |
750 | *dst++ = (c); \ | |
b6871cc7 | 751 | } while (0) |
aa72b389 KH |
752 | |
753 | ||
df7492f9 | 754 | /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */ |
aa72b389 | 755 | |
df7492f9 KH |
756 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
757 | do { \ | |
758 | produced_chars += 2; \ | |
759 | *dst++ = (c1), *dst++ = (c2); \ | |
760 | } while (0) | |
aa72b389 KH |
761 | |
762 | ||
df7492f9 KH |
763 | /* Store a byte C in the place pointed by DST and increment DST to the |
764 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is | |
765 | nonzero, store in an appropriate multibyte from. The caller should | |
766 | declare and set the variables `dst' and `multibytep' appropriately | |
767 | in advance. */ | |
768 | ||
769 | #define EMIT_ONE_BYTE(c) \ | |
770 | do { \ | |
771 | produced_chars++; \ | |
772 | if (multibytep) \ | |
773 | { \ | |
774 | int ch = (c); \ | |
775 | if (ch >= 0x80) \ | |
776 | ch = BYTE8_TO_CHAR (ch); \ | |
777 | CHAR_STRING_ADVANCE (ch, dst); \ | |
778 | } \ | |
779 | else \ | |
780 | *dst++ = (c); \ | |
aa72b389 | 781 | } while (0) |
aa72b389 | 782 | |
aa72b389 | 783 | |
df7492f9 | 784 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 785 | |
e19c3639 KH |
786 | #define EMIT_TWO_BYTES(c1, c2) \ |
787 | do { \ | |
788 | produced_chars += 2; \ | |
789 | if (multibytep) \ | |
790 | { \ | |
791 | int ch; \ | |
792 | \ | |
793 | ch = (c1); \ | |
794 | if (ch >= 0x80) \ | |
795 | ch = BYTE8_TO_CHAR (ch); \ | |
796 | CHAR_STRING_ADVANCE (ch, dst); \ | |
797 | ch = (c2); \ | |
798 | if (ch >= 0x80) \ | |
799 | ch = BYTE8_TO_CHAR (ch); \ | |
800 | CHAR_STRING_ADVANCE (ch, dst); \ | |
801 | } \ | |
802 | else \ | |
803 | { \ | |
804 | *dst++ = (c1); \ | |
805 | *dst++ = (c2); \ | |
806 | } \ | |
aa72b389 KH |
807 | } while (0) |
808 | ||
809 | ||
df7492f9 KH |
810 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
811 | do { \ | |
812 | EMIT_ONE_BYTE (c1); \ | |
813 | EMIT_TWO_BYTES (c2, c3); \ | |
814 | } while (0) | |
aa72b389 | 815 | |
aa72b389 | 816 | |
df7492f9 KH |
817 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
818 | do { \ | |
819 | EMIT_TWO_BYTES (c1, c2); \ | |
820 | EMIT_TWO_BYTES (c3, c4); \ | |
821 | } while (0) | |
aa72b389 | 822 | |
aa72b389 | 823 | |
f6cbaf43 KH |
824 | /* Prototypes for static functions. */ |
825 | static void record_conversion_result P_ ((struct coding_system *coding, | |
826 | enum coding_result_code result)); | |
827 | static int detect_coding_utf_8 P_ ((struct coding_system *, | |
828 | struct coding_detection_info *info)); | |
829 | static void decode_coding_utf_8 P_ ((struct coding_system *)); | |
830 | static int encode_coding_utf_8 P_ ((struct coding_system *)); | |
831 | ||
832 | static int detect_coding_utf_16 P_ ((struct coding_system *, | |
833 | struct coding_detection_info *info)); | |
834 | static void decode_coding_utf_16 P_ ((struct coding_system *)); | |
835 | static int encode_coding_utf_16 P_ ((struct coding_system *)); | |
836 | ||
837 | static int detect_coding_iso_2022 P_ ((struct coding_system *, | |
838 | struct coding_detection_info *info)); | |
839 | static void decode_coding_iso_2022 P_ ((struct coding_system *)); | |
840 | static int encode_coding_iso_2022 P_ ((struct coding_system *)); | |
841 | ||
842 | static int detect_coding_emacs_mule P_ ((struct coding_system *, | |
843 | struct coding_detection_info *info)); | |
844 | static void decode_coding_emacs_mule P_ ((struct coding_system *)); | |
845 | static int encode_coding_emacs_mule P_ ((struct coding_system *)); | |
846 | ||
847 | static int detect_coding_sjis P_ ((struct coding_system *, | |
848 | struct coding_detection_info *info)); | |
849 | static void decode_coding_sjis P_ ((struct coding_system *)); | |
850 | static int encode_coding_sjis P_ ((struct coding_system *)); | |
851 | ||
852 | static int detect_coding_big5 P_ ((struct coding_system *, | |
853 | struct coding_detection_info *info)); | |
854 | static void decode_coding_big5 P_ ((struct coding_system *)); | |
855 | static int encode_coding_big5 P_ ((struct coding_system *)); | |
856 | ||
857 | static int detect_coding_ccl P_ ((struct coding_system *, | |
858 | struct coding_detection_info *info)); | |
859 | static void decode_coding_ccl P_ ((struct coding_system *)); | |
860 | static int encode_coding_ccl P_ ((struct coding_system *)); | |
861 | ||
862 | static void decode_coding_raw_text P_ ((struct coding_system *)); | |
863 | static int encode_coding_raw_text P_ ((struct coding_system *)); | |
864 | ||
865 | static void coding_set_source P_ ((struct coding_system *)); | |
866 | static void coding_set_destination P_ ((struct coding_system *)); | |
867 | static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT)); | |
868 | static void coding_alloc_by_making_gap P_ ((struct coding_system *, | |
869 | EMACS_INT)); | |
870 | static unsigned char *alloc_destination P_ ((struct coding_system *, | |
871 | EMACS_INT, unsigned char *)); | |
872 | static void setup_iso_safe_charsets P_ ((Lisp_Object)); | |
873 | static unsigned char *encode_designation_at_bol P_ ((struct coding_system *, | |
874 | int *, int *, | |
875 | unsigned char *)); | |
876 | static int detect_eol P_ ((const unsigned char *, | |
877 | EMACS_INT, enum coding_category)); | |
878 | static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int)); | |
879 | static void decode_eol P_ ((struct coding_system *)); | |
880 | static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *)); | |
881 | static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *, | |
882 | int, int *, int *)); | |
883 | static int produce_chars P_ ((struct coding_system *, Lisp_Object, int)); | |
884 | static INLINE void produce_composition P_ ((struct coding_system *, int *, | |
885 | EMACS_INT)); | |
886 | static INLINE void produce_charset P_ ((struct coding_system *, int *, | |
887 | EMACS_INT)); | |
888 | static void produce_annotation P_ ((struct coding_system *, EMACS_INT)); | |
889 | static int decode_coding P_ ((struct coding_system *)); | |
890 | static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT, | |
891 | struct coding_system *, | |
892 | int *, EMACS_INT *)); | |
893 | static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT, | |
894 | struct coding_system *, | |
895 | int *, EMACS_INT *)); | |
896 | static void consume_chars P_ ((struct coding_system *, Lisp_Object, int)); | |
897 | static int encode_coding P_ ((struct coding_system *)); | |
898 | static Lisp_Object make_conversion_work_buffer P_ ((int)); | |
899 | static Lisp_Object code_conversion_restore P_ ((Lisp_Object)); | |
900 | static INLINE int char_encodable_p P_ ((int, Lisp_Object)); | |
901 | static Lisp_Object make_subsidiaries P_ ((Lisp_Object)); | |
902 | ||
065e3595 KH |
903 | static void |
904 | record_conversion_result (struct coding_system *coding, | |
905 | enum coding_result_code result) | |
906 | { | |
907 | coding->result = result; | |
908 | switch (result) | |
909 | { | |
910 | case CODING_RESULT_INSUFFICIENT_SRC: | |
911 | Vlast_code_conversion_error = Qinsufficient_source; | |
912 | break; | |
913 | case CODING_RESULT_INCONSISTENT_EOL: | |
914 | Vlast_code_conversion_error = Qinconsistent_eol; | |
915 | break; | |
916 | case CODING_RESULT_INVALID_SRC: | |
917 | Vlast_code_conversion_error = Qinvalid_source; | |
918 | break; | |
919 | case CODING_RESULT_INTERRUPT: | |
920 | Vlast_code_conversion_error = Qinterrupted; | |
921 | break; | |
922 | case CODING_RESULT_INSUFFICIENT_MEM: | |
923 | Vlast_code_conversion_error = Qinsufficient_memory; | |
924 | break; | |
35befdaa KH |
925 | default: |
926 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
927 | } |
928 | } | |
929 | ||
df7492f9 KH |
930 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
931 | do { \ | |
932 | charset_map_loaded = 0; \ | |
933 | c = DECODE_CHAR (charset, code); \ | |
934 | if (charset_map_loaded) \ | |
935 | { \ | |
8f924df7 | 936 | const unsigned char *orig = coding->source; \ |
df7492f9 KH |
937 | EMACS_INT offset; \ |
938 | \ | |
939 | coding_set_source (coding); \ | |
940 | offset = coding->source - orig; \ | |
941 | src += offset; \ | |
942 | src_base += offset; \ | |
943 | src_end += offset; \ | |
944 | } \ | |
aa72b389 KH |
945 | } while (0) |
946 | ||
947 | ||
df7492f9 KH |
948 | #define ASSURE_DESTINATION(bytes) \ |
949 | do { \ | |
950 | if (dst + (bytes) >= dst_end) \ | |
951 | { \ | |
952 | int more_bytes = charbuf_end - charbuf + (bytes); \ | |
953 | \ | |
954 | dst = alloc_destination (coding, more_bytes, dst); \ | |
955 | dst_end = coding->destination + coding->dst_bytes; \ | |
956 | } \ | |
957 | } while (0) | |
aa72b389 | 958 | |
aa72b389 | 959 | |
aa72b389 | 960 | |
df7492f9 KH |
961 | static void |
962 | coding_set_source (coding) | |
aa72b389 | 963 | struct coding_system *coding; |
aa72b389 | 964 | { |
df7492f9 KH |
965 | if (BUFFERP (coding->src_object)) |
966 | { | |
2cb26057 | 967 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 968 | |
df7492f9 | 969 | if (coding->src_pos < 0) |
2cb26057 | 970 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 971 | else |
2cb26057 | 972 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 973 | } |
df7492f9 | 974 | else if (STRINGP (coding->src_object)) |
aa72b389 | 975 | { |
8f924df7 | 976 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 977 | } |
df7492f9 KH |
978 | else |
979 | /* Otherwise, the source is C string and is never relocated | |
980 | automatically. Thus we don't have to update anything. */ | |
981 | ; | |
982 | } | |
aa72b389 | 983 | |
df7492f9 KH |
984 | static void |
985 | coding_set_destination (coding) | |
986 | struct coding_system *coding; | |
987 | { | |
988 | if (BUFFERP (coding->dst_object)) | |
aa72b389 | 989 | { |
df7492f9 | 990 | if (coding->src_pos < 0) |
aa72b389 | 991 | { |
28f67a95 KH |
992 | coding->destination = BEG_ADDR + coding->dst_pos_byte - 1; |
993 | coding->dst_bytes = (GAP_END_ADDR | |
994 | - (coding->src_bytes - coding->consumed) | |
995 | - coding->destination); | |
aa72b389 | 996 | } |
df7492f9 | 997 | else |
28f67a95 KH |
998 | { |
999 | /* We are sure that coding->dst_pos_byte is before the gap | |
1000 | of the buffer. */ | |
1001 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
1002 | + coding->dst_pos_byte - 1); | |
1003 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) | |
1004 | - coding->destination); | |
1005 | } | |
df7492f9 KH |
1006 | } |
1007 | else | |
1008 | /* Otherwise, the destination is C string and is never relocated | |
1009 | automatically. Thus we don't have to update anything. */ | |
1010 | ; | |
1011 | } | |
1012 | ||
1013 | ||
1014 | static void | |
1015 | coding_alloc_by_realloc (coding, bytes) | |
1016 | struct coding_system *coding; | |
1017 | EMACS_INT bytes; | |
1018 | { | |
1019 | coding->destination = (unsigned char *) xrealloc (coding->destination, | |
1020 | coding->dst_bytes + bytes); | |
1021 | coding->dst_bytes += bytes; | |
1022 | } | |
1023 | ||
1024 | static void | |
1025 | coding_alloc_by_making_gap (coding, bytes) | |
1026 | struct coding_system *coding; | |
1027 | EMACS_INT bytes; | |
1028 | { | |
2c78b7e1 KH |
1029 | if (BUFFERP (coding->dst_object) |
1030 | && EQ (coding->src_object, coding->dst_object)) | |
df7492f9 KH |
1031 | { |
1032 | EMACS_INT add = coding->src_bytes - coding->consumed; | |
1033 | ||
1034 | GAP_SIZE -= add; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
1035 | make_gap (bytes); | |
1036 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
1037 | } | |
1038 | else | |
1039 | { | |
2c78b7e1 KH |
1040 | Lisp_Object this_buffer; |
1041 | ||
1042 | this_buffer = Fcurrent_buffer (); | |
df7492f9 KH |
1043 | set_buffer_internal (XBUFFER (coding->dst_object)); |
1044 | make_gap (bytes); | |
1045 | set_buffer_internal (XBUFFER (this_buffer)); | |
aa72b389 | 1046 | } |
df7492f9 | 1047 | } |
8f924df7 | 1048 | |
df7492f9 KH |
1049 | |
1050 | static unsigned char * | |
1051 | alloc_destination (coding, nbytes, dst) | |
1052 | struct coding_system *coding; | |
3e139625 | 1053 | EMACS_INT nbytes; |
df7492f9 KH |
1054 | unsigned char *dst; |
1055 | { | |
1056 | EMACS_INT offset = dst - coding->destination; | |
1057 | ||
1058 | if (BUFFERP (coding->dst_object)) | |
1059 | coding_alloc_by_making_gap (coding, nbytes); | |
aa72b389 | 1060 | else |
df7492f9 | 1061 | coding_alloc_by_realloc (coding, nbytes); |
065e3595 | 1062 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1063 | coding_set_destination (coding); |
1064 | dst = coding->destination + offset; | |
1065 | return dst; | |
1066 | } | |
aa72b389 | 1067 | |
ff0dacd7 KH |
1068 | /** Macros for annotations. */ |
1069 | ||
1070 | /* Maximum length of annotation data (sum of annotations for | |
1071 | composition and charset). */ | |
69a80ea3 | 1072 | #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4) |
ff0dacd7 KH |
1073 | |
1074 | /* An annotation data is stored in the array coding->charbuf in this | |
1075 | format: | |
69a80ea3 | 1076 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1077 | LENGTH is the number of elements in the annotation. |
1078 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1079 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1080 | |
1081 | The format of the following elements depend on ANNOTATION_MASK. | |
1082 | ||
1083 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1084 | follows: | |
1085 | ... METHOD [ COMPOSITION-COMPONENTS ... ] | |
1086 | METHOD is one of enum composition_method. | |
1087 | Optionnal COMPOSITION-COMPONENTS are characters and composition | |
1088 | rules. | |
1089 | ||
1090 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
1091 | follows. */ | |
1092 | ||
69a80ea3 | 1093 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1094 | do { \ |
1095 | *(buf)++ = -(len); \ | |
1096 | *(buf)++ = (mask); \ | |
69a80ea3 | 1097 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1098 | coding->annotated = 1; \ |
1099 | } while (0); | |
1100 | ||
69a80ea3 KH |
1101 | #define ADD_COMPOSITION_DATA(buf, nchars, method) \ |
1102 | do { \ | |
1103 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ | |
1104 | *buf++ = method; \ | |
ff0dacd7 KH |
1105 | } while (0) |
1106 | ||
1107 | ||
69a80ea3 KH |
1108 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1109 | do { \ | |
1110 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1111 | *buf++ = id; \ | |
ff0dacd7 KH |
1112 | } while (0) |
1113 | ||
df7492f9 KH |
1114 | \f |
1115 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1116 | ||
1117 | ||
1118 | ||
1119 | \f | |
1120 | /*** 3. UTF-8 ***/ | |
1121 | ||
1122 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1123 | Check if a text is encoded in UTF-8. If it is, return 1, else |
1124 | return 0. */ | |
df7492f9 KH |
1125 | |
1126 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1127 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1128 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1129 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1130 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1131 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1132 | ||
1133 | static int | |
ff0dacd7 | 1134 | detect_coding_utf_8 (coding, detect_info) |
df7492f9 | 1135 | struct coding_system *coding; |
ff0dacd7 | 1136 | struct coding_detection_info *detect_info; |
df7492f9 | 1137 | { |
065e3595 | 1138 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1139 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1140 | int multibytep = coding->src_multibyte; |
1141 | int consumed_chars = 0; | |
1142 | int found = 0; | |
1143 | ||
ff0dacd7 | 1144 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1145 | /* A coding system of this category is always ASCII compatible. */ |
1146 | src += coding->head_ascii; | |
1147 | ||
1148 | while (1) | |
aa72b389 | 1149 | { |
df7492f9 | 1150 | int c, c1, c2, c3, c4; |
aa72b389 | 1151 | |
065e3595 | 1152 | src_base = src; |
df7492f9 | 1153 | ONE_MORE_BYTE (c); |
065e3595 | 1154 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
df7492f9 KH |
1155 | continue; |
1156 | ONE_MORE_BYTE (c1); | |
065e3595 | 1157 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1158 | break; |
1159 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1160 | { |
ff0dacd7 | 1161 | found = CATEGORY_MASK_UTF_8; |
df7492f9 | 1162 | continue; |
aa72b389 | 1163 | } |
df7492f9 | 1164 | ONE_MORE_BYTE (c2); |
065e3595 | 1165 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1166 | break; |
1167 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1168 | { |
ff0dacd7 | 1169 | found = CATEGORY_MASK_UTF_8; |
df7492f9 | 1170 | continue; |
aa72b389 | 1171 | } |
df7492f9 | 1172 | ONE_MORE_BYTE (c3); |
065e3595 | 1173 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1174 | break; |
1175 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1176 | { |
ff0dacd7 | 1177 | found = CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1178 | continue; |
1179 | } | |
1180 | ONE_MORE_BYTE (c4); | |
065e3595 | 1181 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1182 | break; |
1183 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1184 | { | |
ff0dacd7 | 1185 | found = CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1186 | continue; |
1187 | } | |
1188 | break; | |
aa72b389 | 1189 | } |
ff0dacd7 | 1190 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1191 | return 0; |
aa72b389 | 1192 | |
df7492f9 | 1193 | no_more_source: |
065e3595 | 1194 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1195 | { |
ff0dacd7 | 1196 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1197 | return 0; |
aa72b389 | 1198 | } |
ff0dacd7 KH |
1199 | detect_info->found |= found; |
1200 | return 1; | |
aa72b389 KH |
1201 | } |
1202 | ||
4ed46869 | 1203 | |
b73bfc1c | 1204 | static void |
df7492f9 | 1205 | decode_coding_utf_8 (coding) |
b73bfc1c | 1206 | struct coding_system *coding; |
b73bfc1c | 1207 | { |
8f924df7 KH |
1208 | const unsigned char *src = coding->source + coding->consumed; |
1209 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1210 | const unsigned char *src_base; | |
69a80ea3 KH |
1211 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1212 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
1213 | int consumed_chars = 0, consumed_chars_base; |
1214 | int multibytep = coding->src_multibyte; | |
24a73b0a | 1215 | Lisp_Object attr, charset_list; |
4ed46869 | 1216 | |
24a73b0a | 1217 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 KH |
1218 | |
1219 | while (1) | |
b73bfc1c | 1220 | { |
df7492f9 | 1221 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1222 | |
df7492f9 KH |
1223 | src_base = src; |
1224 | consumed_chars_base = consumed_chars; | |
4af310db | 1225 | |
df7492f9 KH |
1226 | if (charbuf >= charbuf_end) |
1227 | break; | |
1228 | ||
1229 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1230 | if (c1 < 0) |
1231 | { | |
1232 | c = - c1; | |
1233 | } | |
1234 | else if (UTF_8_1_OCTET_P(c1)) | |
df7492f9 KH |
1235 | { |
1236 | c = c1; | |
4af310db | 1237 | } |
df7492f9 | 1238 | else |
4af310db | 1239 | { |
df7492f9 | 1240 | ONE_MORE_BYTE (c2); |
065e3595 | 1241 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1242 | goto invalid_code; |
1243 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1244 | { |
b0edb2c5 DL |
1245 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1246 | /* Reject overlong sequences here and below. Encoders | |
1247 | producing them are incorrect, they can be misleading, | |
1248 | and they mess up read/write invariance. */ | |
1249 | if (c < 128) | |
1250 | goto invalid_code; | |
4af310db | 1251 | } |
df7492f9 | 1252 | else |
aa72b389 | 1253 | { |
df7492f9 | 1254 | ONE_MORE_BYTE (c3); |
065e3595 | 1255 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1256 | goto invalid_code; |
1257 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1258 | { |
1259 | c = (((c1 & 0xF) << 12) | |
1260 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1261 | if (c < 0x800 |
1262 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1263 | goto invalid_code; |
1264 | } | |
df7492f9 KH |
1265 | else |
1266 | { | |
1267 | ONE_MORE_BYTE (c4); | |
065e3595 | 1268 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1269 | goto invalid_code; |
1270 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1271 | { |
df7492f9 KH |
1272 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1273 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1274 | if (c < 0x10000) |
1275 | goto invalid_code; | |
1276 | } | |
df7492f9 KH |
1277 | else |
1278 | { | |
1279 | ONE_MORE_BYTE (c5); | |
065e3595 | 1280 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1281 | goto invalid_code; |
1282 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1283 | { | |
1284 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1285 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1286 | | (c5 & 0x3F)); | |
b0edb2c5 | 1287 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1288 | goto invalid_code; |
1289 | } | |
1290 | else | |
1291 | goto invalid_code; | |
1292 | } | |
1293 | } | |
aa72b389 | 1294 | } |
b73bfc1c | 1295 | } |
df7492f9 KH |
1296 | |
1297 | *charbuf++ = c; | |
1298 | continue; | |
1299 | ||
1300 | invalid_code: | |
1301 | src = src_base; | |
1302 | consumed_chars = consumed_chars_base; | |
1303 | ONE_MORE_BYTE (c); | |
1304 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1305 | coding->errors++; | |
aa72b389 KH |
1306 | } |
1307 | ||
df7492f9 KH |
1308 | no_more_source: |
1309 | coding->consumed_char += consumed_chars_base; | |
1310 | coding->consumed = src_base - coding->source; | |
1311 | coding->charbuf_used = charbuf - coding->charbuf; | |
1312 | } | |
1313 | ||
1314 | ||
1315 | static int | |
1316 | encode_coding_utf_8 (coding) | |
1317 | struct coding_system *coding; | |
1318 | { | |
1319 | int multibytep = coding->dst_multibyte; | |
1320 | int *charbuf = coding->charbuf; | |
1321 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1322 | unsigned char *dst = coding->destination + coding->produced; | |
1323 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
e19c3639 | 1324 | int produced_chars = 0; |
df7492f9 KH |
1325 | int c; |
1326 | ||
1327 | if (multibytep) | |
aa72b389 | 1328 | { |
df7492f9 KH |
1329 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1330 | ||
1331 | while (charbuf < charbuf_end) | |
b73bfc1c | 1332 | { |
df7492f9 | 1333 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1334 | |
df7492f9 KH |
1335 | ASSURE_DESTINATION (safe_room); |
1336 | c = *charbuf++; | |
28f67a95 KH |
1337 | if (CHAR_BYTE8_P (c)) |
1338 | { | |
1339 | c = CHAR_TO_BYTE8 (c); | |
1340 | EMIT_ONE_BYTE (c); | |
1341 | } | |
1342 | else | |
1343 | { | |
1344 | CHAR_STRING_ADVANCE (c, pend); | |
1345 | for (p = str; p < pend; p++) | |
1346 | EMIT_ONE_BYTE (*p); | |
1347 | } | |
b73bfc1c | 1348 | } |
aa72b389 | 1349 | } |
df7492f9 KH |
1350 | else |
1351 | { | |
1352 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1353 | ||
1354 | while (charbuf < charbuf_end) | |
b73bfc1c | 1355 | { |
df7492f9 KH |
1356 | ASSURE_DESTINATION (safe_room); |
1357 | c = *charbuf++; | |
f03caae0 KH |
1358 | if (CHAR_BYTE8_P (c)) |
1359 | *dst++ = CHAR_TO_BYTE8 (c); | |
1360 | else | |
1361 | dst += CHAR_STRING (c, dst); | |
df7492f9 | 1362 | produced_chars++; |
4ed46869 KH |
1363 | } |
1364 | } | |
065e3595 | 1365 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1366 | coding->produced_char += produced_chars; |
1367 | coding->produced = dst - coding->destination; | |
1368 | return 0; | |
4ed46869 KH |
1369 | } |
1370 | ||
b73bfc1c | 1371 | |
df7492f9 | 1372 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
1373 | Check if a text is encoded in one of UTF-16 based coding systems. |
1374 | If it is, return 1, else return 0. */ | |
aa72b389 | 1375 | |
df7492f9 KH |
1376 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1377 | (((val) & 0xFC00) == 0xD800) | |
1378 | ||
1379 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1380 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1381 | |
df7492f9 KH |
1382 | #define UTF_16_INVALID_P(val) \ |
1383 | (((val) == 0xFFFE) \ | |
1384 | || ((val) == 0xFFFF) \ | |
1385 | || UTF_16_LOW_SURROGATE_P (val)) | |
aa72b389 | 1386 | |
aa72b389 | 1387 | |
df7492f9 | 1388 | static int |
ff0dacd7 | 1389 | detect_coding_utf_16 (coding, detect_info) |
aa72b389 | 1390 | struct coding_system *coding; |
ff0dacd7 | 1391 | struct coding_detection_info *detect_info; |
aa72b389 | 1392 | { |
8f924df7 KH |
1393 | const unsigned char *src = coding->source, *src_base = src; |
1394 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
1395 | int multibytep = coding->src_multibyte; |
1396 | int consumed_chars = 0; | |
1397 | int c1, c2; | |
aa72b389 | 1398 | |
ff0dacd7 | 1399 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1400 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1401 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1402 | { |
1403 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1404 | return 0; | |
1405 | } | |
24a73b0a | 1406 | |
df7492f9 KH |
1407 | ONE_MORE_BYTE (c1); |
1408 | ONE_MORE_BYTE (c2); | |
df7492f9 | 1409 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1410 | { |
b49a1807 KH |
1411 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1412 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1413 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1414 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1415 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1416 | } |
df7492f9 | 1417 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1418 | { |
b49a1807 KH |
1419 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1420 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1421 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1422 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1423 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1424 | } | |
065e3595 | 1425 | else if (c1 >= 0 && c2 >= 0) |
24a73b0a | 1426 | { |
24a73b0a KH |
1427 | detect_info->rejected |
1428 | |= (CATEGORY_MASK_UTF_16_BE | CATEGORY_MASK_UTF_16_LE); | |
ff0dacd7 | 1429 | } |
df7492f9 | 1430 | no_more_source: |
ff0dacd7 | 1431 | return 1; |
df7492f9 | 1432 | } |
aa72b389 | 1433 | |
df7492f9 KH |
1434 | static void |
1435 | decode_coding_utf_16 (coding) | |
1436 | struct coding_system *coding; | |
1437 | { | |
8f924df7 KH |
1438 | const unsigned char *src = coding->source + coding->consumed; |
1439 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1440 | const unsigned char *src_base; | |
69a80ea3 KH |
1441 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1442 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
1443 | int consumed_chars = 0, consumed_chars_base; |
1444 | int multibytep = coding->src_multibyte; | |
1445 | enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding); | |
1446 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); | |
1447 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
24a73b0a | 1448 | Lisp_Object attr, charset_list; |
df7492f9 | 1449 | |
24a73b0a | 1450 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1451 | |
b49a1807 | 1452 | if (bom == utf_16_with_bom) |
aa72b389 | 1453 | { |
df7492f9 | 1454 | int c, c1, c2; |
4af310db | 1455 | |
aa72b389 | 1456 | src_base = src; |
df7492f9 KH |
1457 | ONE_MORE_BYTE (c1); |
1458 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1459 | c = (c1 << 8) | c2; |
aa72b389 | 1460 | |
b49a1807 KH |
1461 | if (endian == utf_16_big_endian |
1462 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1463 | { |
b49a1807 KH |
1464 | /* The first two bytes are not BOM. Treat them as bytes |
1465 | for a normal character. */ | |
1466 | src = src_base; | |
1467 | coding->errors++; | |
aa72b389 | 1468 | } |
b49a1807 KH |
1469 | CODING_UTF_16_BOM (coding) = utf_16_without_bom; |
1470 | } | |
1471 | else if (bom == utf_16_detect_bom) | |
1472 | { | |
1473 | /* We have already tried to detect BOM and failed in | |
1474 | detect_coding. */ | |
1475 | CODING_UTF_16_BOM (coding) = utf_16_without_bom; | |
df7492f9 | 1476 | } |
aa72b389 | 1477 | |
df7492f9 KH |
1478 | while (1) |
1479 | { | |
1480 | int c, c1, c2; | |
1481 | ||
1482 | src_base = src; | |
1483 | consumed_chars_base = consumed_chars; | |
1484 | ||
1485 | if (charbuf + 2 >= charbuf_end) | |
1486 | break; | |
1487 | ||
1488 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1489 | if (c1 < 0) |
1490 | { | |
1491 | *charbuf++ = -c1; | |
1492 | continue; | |
1493 | } | |
df7492f9 | 1494 | ONE_MORE_BYTE (c2); |
065e3595 KH |
1495 | if (c2 < 0) |
1496 | { | |
1497 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1498 | *charbuf++ = -c2; | |
1499 | continue; | |
1500 | } | |
df7492f9 | 1501 | c = (endian == utf_16_big_endian |
e19c3639 | 1502 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
df7492f9 | 1503 | if (surrogate) |
fd3ae0b9 | 1504 | { |
df7492f9 | 1505 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1506 | { |
df7492f9 KH |
1507 | if (endian == utf_16_big_endian) |
1508 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1509 | else | |
1510 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1511 | *charbuf++ = c1; | |
1512 | *charbuf++ = c2; | |
1513 | coding->errors++; | |
1514 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1515 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1516 | else |
df7492f9 | 1517 | *charbuf++ = c; |
fd3ae0b9 KH |
1518 | } |
1519 | else | |
df7492f9 KH |
1520 | { |
1521 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1522 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1523 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1524 | } |
fd3ae0b9 | 1525 | } |
aa72b389 | 1526 | else |
df7492f9 KH |
1527 | { |
1528 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1529 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1530 | else | |
1531 | *charbuf++ = c; | |
8f924df7 | 1532 | } |
aa72b389 | 1533 | } |
df7492f9 KH |
1534 | |
1535 | no_more_source: | |
1536 | coding->consumed_char += consumed_chars_base; | |
1537 | coding->consumed = src_base - coding->source; | |
1538 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1539 | } |
b73bfc1c | 1540 | |
df7492f9 KH |
1541 | static int |
1542 | encode_coding_utf_16 (coding) | |
1543 | struct coding_system *coding; | |
1544 | { | |
1545 | int multibytep = coding->dst_multibyte; | |
1546 | int *charbuf = coding->charbuf; | |
1547 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1548 | unsigned char *dst = coding->destination + coding->produced; | |
1549 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1550 | int safe_room = 8; | |
1551 | enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding); | |
1552 | int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; | |
1553 | int produced_chars = 0; | |
24a73b0a | 1554 | Lisp_Object attrs, charset_list; |
df7492f9 | 1555 | int c; |
4ed46869 | 1556 | |
24a73b0a | 1557 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 1558 | |
b49a1807 | 1559 | if (bom != utf_16_without_bom) |
df7492f9 KH |
1560 | { |
1561 | ASSURE_DESTINATION (safe_room); | |
1562 | if (big_endian) | |
df7492f9 | 1563 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1564 | else |
1565 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
df7492f9 KH |
1566 | CODING_UTF_16_BOM (coding) = utf_16_without_bom; |
1567 | } | |
1568 | ||
1569 | while (charbuf < charbuf_end) | |
1570 | { | |
1571 | ASSURE_DESTINATION (safe_room); | |
1572 | c = *charbuf++; | |
e19c3639 KH |
1573 | if (c >= MAX_UNICODE_CHAR) |
1574 | c = coding->default_char; | |
df7492f9 KH |
1575 | |
1576 | if (c < 0x10000) | |
1577 | { | |
1578 | if (big_endian) | |
1579 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1580 | else | |
1581 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1582 | } | |
1583 | else | |
1584 | { | |
1585 | int c1, c2; | |
1586 | ||
1587 | c -= 0x10000; | |
1588 | c1 = (c >> 10) + 0xD800; | |
1589 | c2 = (c & 0x3FF) + 0xDC00; | |
1590 | if (big_endian) | |
1591 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1592 | else | |
1593 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1594 | } | |
1595 | } | |
065e3595 | 1596 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1597 | coding->produced = dst - coding->destination; |
1598 | coding->produced_char += produced_chars; | |
1599 | return 0; | |
1600 | } | |
1601 | ||
1602 | \f | |
1603 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1604 | ||
1605 | /* Emacs' internal format for representation of multiple character | |
1606 | sets is a kind of multi-byte encoding, i.e. characters are | |
1607 | represented by variable-length sequences of one-byte codes. | |
1608 | ||
1609 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1610 | represented by one-byte sequences which are their ASCII codes, in | |
1611 | the range 0x00 through 0x7F. | |
1612 | ||
1613 | 8-bit characters of the range 0x80..0x9F are represented by | |
1614 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1615 | code + 0x20). | |
1616 | ||
1617 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1618 | one-byte sequences which are their 8-bit code. | |
1619 | ||
1620 | The other characters are represented by a sequence of `base | |
1621 | leading-code', optional `extended leading-code', and one or two | |
1622 | `position-code's. The length of the sequence is determined by the | |
1623 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1624 | whereas extended leading-code and position-code take the range 0xA0 | |
1625 | through 0xFF. See `charset.h' for more details about leading-code | |
1626 | and position-code. | |
1627 | ||
1628 | --- CODE RANGE of Emacs' internal format --- | |
1629 | character set range | |
1630 | ------------- ----- | |
1631 | ascii 0x00..0x7F | |
1632 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1633 | eight-bit-graphic 0xA0..0xBF | |
1634 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1635 | --------------------------------------------- | |
1636 | ||
1637 | As this is the internal character representation, the format is | |
1638 | usually not used externally (i.e. in a file or in a data sent to a | |
1639 | process). But, it is possible to have a text externally in this | |
1640 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1641 | ||
1642 | In that case, a sequence of one-byte codes has a slightly different | |
1643 | form. | |
1644 | ||
1645 | At first, all characters in eight-bit-control are represented by | |
1646 | one-byte sequences which are their 8-bit code. | |
1647 | ||
1648 | Next, character composition data are represented by the byte | |
1649 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1650 | where, | |
1651 | METHOD is 0xF0 plus one of composition method (enum | |
1652 | composition_method), | |
1653 | ||
1654 | BYTES is 0xA0 plus a byte length of this composition data, | |
1655 | ||
1656 | CHARS is 0x20 plus a number of characters composed by this | |
1657 | data, | |
1658 | ||
1659 | COMPONENTs are characters of multibye form or composition | |
1660 | rules encoded by two-byte of ASCII codes. | |
1661 | ||
1662 | In addition, for backward compatibility, the following formats are | |
1663 | also recognized as composition data on decoding. | |
1664 | ||
1665 | 0x80 MSEQ ... | |
1666 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1667 | ||
1668 | Here, | |
1669 | MSEQ is a multibyte form but in these special format: | |
1670 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1671 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1672 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1673 | represents a composition rule. | |
1674 | */ | |
1675 | ||
1676 | char emacs_mule_bytes[256]; | |
1677 | ||
df7492f9 | 1678 | int |
ff0dacd7 | 1679 | emacs_mule_char (coding, src, nbytes, nchars, id) |
df7492f9 | 1680 | struct coding_system *coding; |
065e3595 | 1681 | const unsigned char *src; |
ff0dacd7 | 1682 | int *nbytes, *nchars, *id; |
df7492f9 | 1683 | { |
8f924df7 KH |
1684 | const unsigned char *src_end = coding->source + coding->src_bytes; |
1685 | const unsigned char *src_base = src; | |
df7492f9 | 1686 | int multibytep = coding->src_multibyte; |
df7492f9 KH |
1687 | struct charset *charset; |
1688 | unsigned code; | |
1689 | int c; | |
1690 | int consumed_chars = 0; | |
1691 | ||
1692 | ONE_MORE_BYTE (c); | |
065e3595 | 1693 | if (c < 0) |
df7492f9 | 1694 | { |
065e3595 KH |
1695 | c = -c; |
1696 | charset = emacs_mule_charset[0]; | |
1697 | } | |
1698 | else | |
1699 | { | |
1700 | switch (emacs_mule_bytes[c]) | |
b73bfc1c | 1701 | { |
065e3595 | 1702 | case 2: |
df7492f9 KH |
1703 | if (! (charset = emacs_mule_charset[c])) |
1704 | goto invalid_code; | |
1705 | ONE_MORE_BYTE (c); | |
9ffd559c | 1706 | if (c < 0xA0) |
065e3595 | 1707 | goto invalid_code; |
df7492f9 | 1708 | code = c & 0x7F; |
065e3595 KH |
1709 | break; |
1710 | ||
1711 | case 3: | |
1712 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
1713 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
1714 | { | |
1715 | ONE_MORE_BYTE (c); | |
9ffd559c | 1716 | if (c < 0xA0 || ! (charset = emacs_mule_charset[c])) |
065e3595 KH |
1717 | goto invalid_code; |
1718 | ONE_MORE_BYTE (c); | |
9ffd559c | 1719 | if (c < 0xA0) |
065e3595 KH |
1720 | goto invalid_code; |
1721 | code = c & 0x7F; | |
1722 | } | |
1723 | else | |
1724 | { | |
1725 | if (! (charset = emacs_mule_charset[c])) | |
1726 | goto invalid_code; | |
1727 | ONE_MORE_BYTE (c); | |
9ffd559c | 1728 | if (c < 0xA0) |
065e3595 KH |
1729 | goto invalid_code; |
1730 | code = (c & 0x7F) << 8; | |
1731 | ONE_MORE_BYTE (c); | |
9ffd559c | 1732 | if (c < 0xA0) |
065e3595 KH |
1733 | goto invalid_code; |
1734 | code |= c & 0x7F; | |
1735 | } | |
1736 | break; | |
1737 | ||
1738 | case 4: | |
1739 | ONE_MORE_BYTE (c); | |
1740 | if (c < 0 || ! (charset = emacs_mule_charset[c])) | |
df7492f9 KH |
1741 | goto invalid_code; |
1742 | ONE_MORE_BYTE (c); | |
9ffd559c | 1743 | if (c < 0xA0) |
065e3595 | 1744 | goto invalid_code; |
781d7a48 | 1745 | code = (c & 0x7F) << 8; |
df7492f9 | 1746 | ONE_MORE_BYTE (c); |
9ffd559c | 1747 | if (c < 0xA0) |
065e3595 | 1748 | goto invalid_code; |
df7492f9 | 1749 | code |= c & 0x7F; |
065e3595 | 1750 | break; |
df7492f9 | 1751 | |
065e3595 KH |
1752 | case 1: |
1753 | code = c; | |
1754 | charset = CHARSET_FROM_ID (ASCII_BYTE_P (code) | |
1755 | ? charset_ascii : charset_eight_bit); | |
1756 | break; | |
df7492f9 | 1757 | |
065e3595 KH |
1758 | default: |
1759 | abort (); | |
1760 | } | |
1761 | c = DECODE_CHAR (charset, code); | |
1762 | if (c < 0) | |
1763 | goto invalid_code; | |
df7492f9 | 1764 | } |
df7492f9 KH |
1765 | *nbytes = src - src_base; |
1766 | *nchars = consumed_chars; | |
ff0dacd7 KH |
1767 | if (id) |
1768 | *id = charset->id; | |
df7492f9 KH |
1769 | return c; |
1770 | ||
1771 | no_more_source: | |
1772 | return -2; | |
1773 | ||
1774 | invalid_code: | |
1775 | return -1; | |
1776 | } | |
1777 | ||
1778 | ||
1779 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1780 | Check if a text is encoded in `emacs-mule'. If it is, return 1, |
1781 | else return 0. */ | |
df7492f9 KH |
1782 | |
1783 | static int | |
ff0dacd7 | 1784 | detect_coding_emacs_mule (coding, detect_info) |
df7492f9 | 1785 | struct coding_system *coding; |
ff0dacd7 | 1786 | struct coding_detection_info *detect_info; |
df7492f9 | 1787 | { |
065e3595 | 1788 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1789 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1790 | int multibytep = coding->src_multibyte; |
1791 | int consumed_chars = 0; | |
1792 | int c; | |
1793 | int found = 0; | |
1794 | ||
ff0dacd7 | 1795 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; |
df7492f9 KH |
1796 | /* A coding system of this category is always ASCII compatible. */ |
1797 | src += coding->head_ascii; | |
1798 | ||
1799 | while (1) | |
1800 | { | |
065e3595 | 1801 | src_base = src; |
df7492f9 | 1802 | ONE_MORE_BYTE (c); |
065e3595 KH |
1803 | if (c < 0) |
1804 | continue; | |
df7492f9 KH |
1805 | if (c == 0x80) |
1806 | { | |
1807 | /* Perhaps the start of composite character. We simple skip | |
1808 | it because analyzing it is too heavy for detecting. But, | |
1809 | at least, we check that the composite character | |
1810 | constitues of more than 4 bytes. */ | |
8f924df7 | 1811 | const unsigned char *src_base; |
df7492f9 KH |
1812 | |
1813 | repeat: | |
1814 | src_base = src; | |
1815 | do | |
1816 | { | |
1817 | ONE_MORE_BYTE (c); | |
1818 | } | |
1819 | while (c >= 0xA0); | |
1820 | ||
1821 | if (src - src_base <= 4) | |
1822 | break; | |
ff0dacd7 | 1823 | found = CATEGORY_MASK_EMACS_MULE; |
df7492f9 KH |
1824 | if (c == 0x80) |
1825 | goto repeat; | |
b73bfc1c | 1826 | } |
df7492f9 KH |
1827 | |
1828 | if (c < 0x80) | |
b73bfc1c | 1829 | { |
df7492f9 KH |
1830 | if (c < 0x20 |
1831 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
1832 | break; | |
1833 | } | |
1834 | else | |
1835 | { | |
0e219d54 | 1836 | int more_bytes = emacs_mule_bytes[*src_base] - 1; |
df7492f9 | 1837 | |
0e219d54 | 1838 | while (more_bytes > 0) |
df7492f9 KH |
1839 | { |
1840 | ONE_MORE_BYTE (c); | |
0e219d54 KH |
1841 | if (c < 0xA0) |
1842 | { | |
1843 | src--; /* Unread the last byte. */ | |
1844 | break; | |
1845 | } | |
1846 | more_bytes--; | |
df7492f9 | 1847 | } |
0e219d54 | 1848 | if (more_bytes != 0) |
df7492f9 | 1849 | break; |
ff0dacd7 | 1850 | found = CATEGORY_MASK_EMACS_MULE; |
4ed46869 KH |
1851 | } |
1852 | } | |
ff0dacd7 | 1853 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; |
df7492f9 KH |
1854 | return 0; |
1855 | ||
1856 | no_more_source: | |
065e3595 | 1857 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 1858 | { |
ff0dacd7 | 1859 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; |
89528eb3 KH |
1860 | return 0; |
1861 | } | |
ff0dacd7 KH |
1862 | detect_info->found |= found; |
1863 | return 1; | |
4ed46869 KH |
1864 | } |
1865 | ||
b73bfc1c | 1866 | |
df7492f9 KH |
1867 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
1868 | ||
1869 | /* Decode a character represented as a component of composition | |
1870 | sequence of Emacs 20/21 style at SRC. Set C to that character and | |
1871 | update SRC to the head of next character (or an encoded composition | |
1872 | rule). If SRC doesn't points a composition component, set C to -1. | |
1873 | If SRC points an invalid byte sequence, global exit by a return | |
1874 | value 0. */ | |
1875 | ||
1876 | #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \ | |
1877 | if (1) \ | |
1878 | { \ | |
1879 | int c; \ | |
1880 | int nbytes, nchars; \ | |
1881 | \ | |
1882 | if (src == src_end) \ | |
1883 | break; \ | |
ff0dacd7 | 1884 | c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\ |
df7492f9 KH |
1885 | if (c < 0) \ |
1886 | { \ | |
1887 | if (c == -2) \ | |
1888 | break; \ | |
1889 | goto invalid_code; \ | |
1890 | } \ | |
1891 | *buf++ = c; \ | |
1892 | src += nbytes; \ | |
1893 | consumed_chars += nchars; \ | |
1894 | } \ | |
1895 | else | |
1896 | ||
1897 | ||
1898 | /* Decode a composition rule represented as a component of composition | |
781d7a48 KH |
1899 | sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF, |
1900 | and increment BUF. If SRC points an invalid byte sequence, set C | |
1901 | to -1. */ | |
df7492f9 | 1902 | |
781d7a48 | 1903 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \ |
df7492f9 KH |
1904 | do { \ |
1905 | int c, gref, nref; \ | |
1906 | \ | |
781d7a48 | 1907 | if (src >= src_end) \ |
df7492f9 KH |
1908 | goto invalid_code; \ |
1909 | ONE_MORE_BYTE_NO_CHECK (c); \ | |
781d7a48 | 1910 | c -= 0x20; \ |
df7492f9 KH |
1911 | if (c < 0 || c >= 81) \ |
1912 | goto invalid_code; \ | |
1913 | \ | |
1914 | gref = c / 9, nref = c % 9; \ | |
1915 | *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
1916 | } while (0) | |
1917 | ||
1918 | ||
781d7a48 KH |
1919 | /* Decode a composition rule represented as a component of composition |
1920 | sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF, | |
1921 | and increment BUF. If SRC points an invalid byte sequence, set C | |
1922 | to -1. */ | |
1923 | ||
1924 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \ | |
1925 | do { \ | |
1926 | int gref, nref; \ | |
1927 | \ | |
1928 | if (src + 1>= src_end) \ | |
1929 | goto invalid_code; \ | |
1930 | ONE_MORE_BYTE_NO_CHECK (gref); \ | |
1931 | gref -= 0x20; \ | |
1932 | ONE_MORE_BYTE_NO_CHECK (nref); \ | |
1933 | nref -= 0x20; \ | |
1934 | if (gref < 0 || gref >= 81 \ | |
1935 | || nref < 0 || nref >= 81) \ | |
1936 | goto invalid_code; \ | |
1937 | *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
1938 | } while (0) | |
1939 | ||
1940 | ||
df7492f9 | 1941 | #define DECODE_EMACS_MULE_21_COMPOSITION(c) \ |
aa72b389 | 1942 | do { \ |
df7492f9 | 1943 | /* Emacs 21 style format. The first three bytes at SRC are \ |
781d7a48 | 1944 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \ |
df7492f9 KH |
1945 | the byte length of this composition information, CHARS is the \ |
1946 | number of characters composed by this composition. */ \ | |
781d7a48 KH |
1947 | enum composition_method method = c - 0xF2; \ |
1948 | int *charbuf_base = charbuf; \ | |
df7492f9 KH |
1949 | int consumed_chars_limit; \ |
1950 | int nbytes, nchars; \ | |
1951 | \ | |
1952 | ONE_MORE_BYTE (c); \ | |
065e3595 KH |
1953 | if (c < 0) \ |
1954 | goto invalid_code; \ | |
df7492f9 KH |
1955 | nbytes = c - 0xA0; \ |
1956 | if (nbytes < 3) \ | |
1957 | goto invalid_code; \ | |
1958 | ONE_MORE_BYTE (c); \ | |
065e3595 KH |
1959 | if (c < 0) \ |
1960 | goto invalid_code; \ | |
df7492f9 | 1961 | nchars = c - 0xA0; \ |
69a80ea3 | 1962 | ADD_COMPOSITION_DATA (charbuf, nchars, method); \ |
df7492f9 KH |
1963 | consumed_chars_limit = consumed_chars_base + nbytes; \ |
1964 | if (method != COMPOSITION_RELATIVE) \ | |
aa72b389 | 1965 | { \ |
df7492f9 KH |
1966 | int i = 0; \ |
1967 | while (consumed_chars < consumed_chars_limit) \ | |
aa72b389 | 1968 | { \ |
df7492f9 | 1969 | if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \ |
781d7a48 | 1970 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \ |
df7492f9 KH |
1971 | else \ |
1972 | DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \ | |
781d7a48 | 1973 | i++; \ |
aa72b389 | 1974 | } \ |
df7492f9 KH |
1975 | if (consumed_chars < consumed_chars_limit) \ |
1976 | goto invalid_code; \ | |
781d7a48 | 1977 | charbuf_base[0] -= i; \ |
aa72b389 KH |
1978 | } \ |
1979 | } while (0) | |
93dec019 | 1980 | |
aa72b389 | 1981 | |
df7492f9 KH |
1982 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \ |
1983 | do { \ | |
1984 | /* Emacs 20 style format for relative composition. */ \ | |
1985 | /* Store multibyte form of characters to be composed. */ \ | |
ff0dacd7 | 1986 | enum composition_method method = COMPOSITION_RELATIVE; \ |
df7492f9 KH |
1987 | int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \ |
1988 | int *buf = components; \ | |
1989 | int i, j; \ | |
1990 | \ | |
1991 | src = src_base; \ | |
1992 | ONE_MORE_BYTE (c); /* skip 0x80 */ \ | |
1993 | for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \ | |
1994 | DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \ | |
1995 | if (i < 2) \ | |
1996 | goto invalid_code; \ | |
69a80ea3 | 1997 | ADD_COMPOSITION_DATA (charbuf, i, method); \ |
df7492f9 KH |
1998 | for (j = 0; j < i; j++) \ |
1999 | *charbuf++ = components[j]; \ | |
2000 | } while (0) | |
2001 | ||
2002 | ||
2003 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \ | |
2004 | do { \ | |
2005 | /* Emacs 20 style format for rule-base composition. */ \ | |
2006 | /* Store multibyte form of characters to be composed. */ \ | |
ff0dacd7 | 2007 | enum composition_method method = COMPOSITION_WITH_RULE; \ |
df7492f9 KH |
2008 | int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \ |
2009 | int *buf = components; \ | |
2010 | int i, j; \ | |
2011 | \ | |
2012 | DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \ | |
2013 | for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \ | |
2014 | { \ | |
781d7a48 | 2015 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \ |
df7492f9 KH |
2016 | DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \ |
2017 | } \ | |
2018 | if (i < 1 || (buf - components) % 2 == 0) \ | |
2019 | goto invalid_code; \ | |
2020 | if (charbuf + i + (i / 2) + 1 < charbuf_end) \ | |
2021 | goto no_more_source; \ | |
69a80ea3 | 2022 | ADD_COMPOSITION_DATA (buf, i, method); \ |
df7492f9 KH |
2023 | for (j = 0; j < i; j++) \ |
2024 | *charbuf++ = components[j]; \ | |
2025 | for (j = 0; j < i; j += 2) \ | |
2026 | *charbuf++ = components[j]; \ | |
2027 | } while (0) | |
2028 | ||
aa72b389 KH |
2029 | |
2030 | static void | |
df7492f9 | 2031 | decode_coding_emacs_mule (coding) |
aa72b389 | 2032 | struct coding_system *coding; |
aa72b389 | 2033 | { |
8f924df7 KH |
2034 | const unsigned char *src = coding->source + coding->consumed; |
2035 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2036 | const unsigned char *src_base; | |
69a80ea3 KH |
2037 | int *charbuf = coding->charbuf + coding->charbuf_used; |
2038 | int *charbuf_end | |
2039 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 | 2040 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2041 | int multibytep = coding->src_multibyte; |
24a73b0a | 2042 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2043 | int char_offset = coding->produced_char; |
2044 | int last_offset = char_offset; | |
2045 | int last_id = charset_ascii; | |
aa72b389 | 2046 | |
24a73b0a | 2047 | CODING_GET_INFO (coding, attrs, charset_list); |
aa72b389 | 2048 | |
aa72b389 KH |
2049 | while (1) |
2050 | { | |
df7492f9 KH |
2051 | int c; |
2052 | ||
aa72b389 | 2053 | src_base = src; |
df7492f9 KH |
2054 | consumed_chars_base = consumed_chars; |
2055 | ||
2056 | if (charbuf >= charbuf_end) | |
2057 | break; | |
aa72b389 | 2058 | |
df7492f9 | 2059 | ONE_MORE_BYTE (c); |
065e3595 KH |
2060 | if (c < 0) |
2061 | { | |
2062 | *charbuf++ = -c; | |
2063 | char_offset++; | |
2064 | } | |
2065 | else if (c < 0x80) | |
aa72b389 | 2066 | { |
df7492f9 KH |
2067 | *charbuf++ = c; |
2068 | char_offset++; | |
aa72b389 | 2069 | } |
df7492f9 KH |
2070 | else if (c == 0x80) |
2071 | { | |
df7492f9 | 2072 | ONE_MORE_BYTE (c); |
065e3595 KH |
2073 | if (c < 0) |
2074 | goto invalid_code; | |
781d7a48 KH |
2075 | if (c - 0xF2 >= COMPOSITION_RELATIVE |
2076 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) | |
df7492f9 KH |
2077 | DECODE_EMACS_MULE_21_COMPOSITION (c); |
2078 | else if (c < 0xC0) | |
2079 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c); | |
2080 | else if (c == 0xFF) | |
2081 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c); | |
2082 | else | |
2083 | goto invalid_code; | |
2084 | } | |
2085 | else if (c < 0xA0 && emacs_mule_bytes[c] > 1) | |
2086 | { | |
2087 | int nbytes, nchars; | |
ff0dacd7 KH |
2088 | int id; |
2089 | ||
781d7a48 KH |
2090 | src = src_base; |
2091 | consumed_chars = consumed_chars_base; | |
ff0dacd7 | 2092 | c = emacs_mule_char (coding, src, &nbytes, &nchars, &id); |
df7492f9 KH |
2093 | if (c < 0) |
2094 | { | |
2095 | if (c == -2) | |
2096 | break; | |
2097 | goto invalid_code; | |
2098 | } | |
ff0dacd7 KH |
2099 | if (last_id != id) |
2100 | { | |
2101 | if (last_id != charset_ascii) | |
69a80ea3 | 2102 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
2103 | last_id = id; |
2104 | last_offset = char_offset; | |
2105 | } | |
df7492f9 | 2106 | *charbuf++ = c; |
781d7a48 KH |
2107 | src += nbytes; |
2108 | consumed_chars += nchars; | |
df7492f9 KH |
2109 | char_offset++; |
2110 | } | |
2111 | continue; | |
2112 | ||
2113 | invalid_code: | |
2114 | src = src_base; | |
2115 | consumed_chars = consumed_chars_base; | |
2116 | ONE_MORE_BYTE (c); | |
2117 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2118 | char_offset++; |
df7492f9 KH |
2119 | coding->errors++; |
2120 | } | |
2121 | ||
2122 | no_more_source: | |
ff0dacd7 | 2123 | if (last_id != charset_ascii) |
69a80ea3 | 2124 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2125 | coding->consumed_char += consumed_chars_base; |
2126 | coding->consumed = src_base - coding->source; | |
2127 | coding->charbuf_used = charbuf - coding->charbuf; | |
2128 | } | |
2129 | ||
2130 | ||
2131 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2132 | do { \ | |
2133 | if (id < 0xA0) \ | |
2134 | codes[0] = id, codes[1] = 0; \ | |
2135 | else if (id < 0xE0) \ | |
2136 | codes[0] = 0x9A, codes[1] = id; \ | |
2137 | else if (id < 0xF0) \ | |
2138 | codes[0] = 0x9B, codes[1] = id; \ | |
2139 | else if (id < 0xF5) \ | |
2140 | codes[0] = 0x9C, codes[1] = id; \ | |
2141 | else \ | |
2142 | codes[0] = 0x9D, codes[1] = id; \ | |
2143 | } while (0); | |
2144 | ||
aa72b389 | 2145 | |
df7492f9 KH |
2146 | static int |
2147 | encode_coding_emacs_mule (coding) | |
2148 | struct coding_system *coding; | |
2149 | { | |
2150 | int multibytep = coding->dst_multibyte; | |
2151 | int *charbuf = coding->charbuf; | |
2152 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2153 | unsigned char *dst = coding->destination + coding->produced; | |
2154 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2155 | int safe_room = 8; | |
df7492f9 | 2156 | int produced_chars = 0; |
24a73b0a | 2157 | Lisp_Object attrs, charset_list; |
df7492f9 | 2158 | int c; |
ff0dacd7 | 2159 | int preferred_charset_id = -1; |
df7492f9 | 2160 | |
24a73b0a | 2161 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2162 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2163 | { | |
2164 | CODING_ATTR_CHARSET_LIST (attrs) | |
2165 | = charset_list = Vemacs_mule_charset_list; | |
2166 | } | |
df7492f9 KH |
2167 | |
2168 | while (charbuf < charbuf_end) | |
2169 | { | |
2170 | ASSURE_DESTINATION (safe_room); | |
2171 | c = *charbuf++; | |
ff0dacd7 KH |
2172 | |
2173 | if (c < 0) | |
2174 | { | |
2175 | /* Handle an annotation. */ | |
2176 | switch (*charbuf) | |
2177 | { | |
2178 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2179 | /* Not yet implemented. */ | |
2180 | break; | |
2181 | case CODING_ANNOTATE_CHARSET_MASK: | |
2182 | preferred_charset_id = charbuf[3]; | |
2183 | if (preferred_charset_id >= 0 | |
2184 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2185 | charset_list))) | |
2186 | preferred_charset_id = -1; | |
2187 | break; | |
2188 | default: | |
2189 | abort (); | |
2190 | } | |
2191 | charbuf += -c - 1; | |
2192 | continue; | |
2193 | } | |
2194 | ||
df7492f9 KH |
2195 | if (ASCII_CHAR_P (c)) |
2196 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2197 | else if (CHAR_BYTE8_P (c)) |
2198 | { | |
2199 | c = CHAR_TO_BYTE8 (c); | |
2200 | EMIT_ONE_BYTE (c); | |
2201 | } | |
df7492f9 | 2202 | else |
aa72b389 | 2203 | { |
df7492f9 KH |
2204 | struct charset *charset; |
2205 | unsigned code; | |
2206 | int dimension; | |
2207 | int emacs_mule_id; | |
2208 | unsigned char leading_codes[2]; | |
2209 | ||
ff0dacd7 KH |
2210 | if (preferred_charset_id >= 0) |
2211 | { | |
2212 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
2213 | if (! CHAR_CHARSET_P (c, charset)) | |
2214 | charset = char_charset (c, charset_list, NULL); | |
2215 | } | |
2216 | else | |
2217 | charset = char_charset (c, charset_list, &code); | |
df7492f9 KH |
2218 | if (! charset) |
2219 | { | |
2220 | c = coding->default_char; | |
2221 | if (ASCII_CHAR_P (c)) | |
2222 | { | |
2223 | EMIT_ONE_ASCII_BYTE (c); | |
2224 | continue; | |
2225 | } | |
2226 | charset = char_charset (c, charset_list, &code); | |
2227 | } | |
2228 | dimension = CHARSET_DIMENSION (charset); | |
2229 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2230 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2231 | EMIT_ONE_BYTE (leading_codes[0]); | |
2232 | if (leading_codes[1]) | |
2233 | EMIT_ONE_BYTE (leading_codes[1]); | |
2234 | if (dimension == 1) | |
1fa663f9 | 2235 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2236 | else |
df7492f9 | 2237 | { |
1fa663f9 | 2238 | code |= 0x8080; |
df7492f9 KH |
2239 | EMIT_ONE_BYTE (code >> 8); |
2240 | EMIT_ONE_BYTE (code & 0xFF); | |
2241 | } | |
aa72b389 | 2242 | } |
aa72b389 | 2243 | } |
065e3595 | 2244 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2245 | coding->produced_char += produced_chars; |
2246 | coding->produced = dst - coding->destination; | |
2247 | return 0; | |
aa72b389 | 2248 | } |
b73bfc1c | 2249 | |
4ed46869 | 2250 | \f |
df7492f9 | 2251 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2252 | |
2253 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2254 | Since the intention of this note is to help understand the |
5a936b46 | 2255 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2256 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2257 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2258 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2259 | |
2260 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2261 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2262 | is encoded using bytes less than 128. This may make the encoded |
2263 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2264 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2265 | Significant Bit). |
b73bfc1c | 2266 | |
cfb43547 DL |
2267 | There are two kinds of character sets: control character sets and |
2268 | graphic character sets. The former contain control characters such | |
4ed46869 | 2269 | as `newline' and `escape' to provide control functions (control |
39787efd | 2270 | functions are also provided by escape sequences). The latter |
cfb43547 | 2271 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2272 | two control character sets and many graphic character sets. |
2273 | ||
2274 | Graphic character sets are classified into one of the following | |
39787efd KH |
2275 | four classes, according to the number of bytes (DIMENSION) and |
2276 | number of characters in one dimension (CHARS) of the set: | |
2277 | - DIMENSION1_CHARS94 | |
2278 | - DIMENSION1_CHARS96 | |
2279 | - DIMENSION2_CHARS94 | |
2280 | - DIMENSION2_CHARS96 | |
2281 | ||
2282 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2283 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2284 | hereafter). The <F> of each character set is decided by ECMA(*) |
2285 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2286 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2287 | |
2288 | Note (*): ECMA = European Computer Manufacturers Association | |
2289 | ||
cfb43547 | 2290 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2291 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2292 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2293 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2294 | o DIMENSION2_CHARS96 -- none for the moment | |
2295 | ||
39787efd | 2296 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2297 | C0 [0x00..0x1F] -- control character plane 0 |
2298 | GL [0x20..0x7F] -- graphic character plane 0 | |
2299 | C1 [0x80..0x9F] -- control character plane 1 | |
2300 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2301 | ||
2302 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2303 | C1 by an escape sequence. The most common case is that: |
2304 | - ISO646's control character set is designated/invoked to C0, and | |
2305 | - ISO6429's control character set is designated/invoked to C1, | |
2306 | and usually these designations/invocations are omitted in encoded | |
2307 | text. In a 7-bit environment, only C0 can be used, and a control | |
2308 | character for C1 is encoded by an appropriate escape sequence to | |
2309 | fit into the environment. All control characters for C1 are | |
2310 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2311 | |
2312 | A graphic character set is at first designated to one of four | |
2313 | graphic registers (G0 through G3), then these graphic registers are | |
2314 | invoked to GL or GR. These designations and invocations can be | |
2315 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2316 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2317 | these invocations and designations are omitted in encoded text. | |
2318 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2319 | |
39787efd KH |
2320 | When a graphic character set of CHARS94 is invoked to GL, codes |
2321 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2322 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2323 | be used. | |
4ed46869 KH |
2324 | |
2325 | There are two ways of invocation: locking-shift and single-shift. | |
2326 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2327 | invocation, whereas with single-shift, the invocation affects the |
2328 | following character only and doesn't affect the locking-shift | |
2329 | state. Invocations are done by the following control characters or | |
2330 | escape sequences: | |
4ed46869 KH |
2331 | |
2332 | ---------------------------------------------------------------------- | |
39787efd | 2333 | abbrev function cntrl escape seq description |
4ed46869 | 2334 | ---------------------------------------------------------------------- |
39787efd KH |
2335 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2336 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2337 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2338 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2339 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2340 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2341 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2342 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2343 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2344 | ---------------------------------------------------------------------- |
39787efd KH |
2345 | (*) These are not used by any known coding system. |
2346 | ||
2347 | Control characters for these functions are defined by macros | |
2348 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2349 | |
39787efd | 2350 | Designations are done by the following escape sequences: |
4ed46869 KH |
2351 | ---------------------------------------------------------------------- |
2352 | escape sequence description | |
2353 | ---------------------------------------------------------------------- | |
2354 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2355 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2356 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2357 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2358 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2359 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2360 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2361 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2362 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2363 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2364 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2365 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2366 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2367 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2368 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2369 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2370 | ---------------------------------------------------------------------- | |
2371 | ||
2372 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2373 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2374 | |
2375 | Note (*): Although these designations are not allowed in ISO2022, | |
2376 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2377 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2378 | 7-bit environment, non-locking-shift, and non-single-shift. |
2379 | ||
2380 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2381 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2382 | |
cfb43547 | 2383 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2384 | same multilingual text in ISO2022. Actually, there exist many |
2385 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2386 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2387 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2388 | localized platforms), and all of these are variants of ISO2022. |
2389 | ||
2390 | In addition to the above, Emacs handles two more kinds of escape | |
2391 | sequences: ISO6429's direction specification and Emacs' private | |
2392 | sequence for specifying character composition. | |
2393 | ||
39787efd | 2394 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2395 | o CSI ']' -- end of the current direction |
2396 | o CSI '0' ']' -- end of the current direction | |
2397 | o CSI '1' ']' -- start of left-to-right text | |
2398 | o CSI '2' ']' -- start of right-to-left text | |
2399 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2400 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2401 | ||
2402 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2403 | o ESC '0' -- start relative composition |
2404 | o ESC '1' -- end composition | |
2405 | o ESC '2' -- start rule-base composition (*) | |
2406 | o ESC '3' -- start relative composition with alternate chars (**) | |
2407 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2408 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2409 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2410 | |
5a936b46 DL |
2411 | (*) This form is used only in Emacs 20.7 and older versions, |
2412 | but newer versions can safely decode it. | |
cfb43547 | 2413 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2414 | and older versions can't decode it. |
ec6d2bb8 | 2415 | |
cfb43547 | 2416 | Here's a list of example usages of these composition escape |
b73bfc1c | 2417 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2418 | |
b73bfc1c | 2419 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2420 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2421 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2422 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2423 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2424 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2425 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2426 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 KH |
2427 | |
2428 | enum iso_code_class_type iso_code_class[256]; | |
2429 | ||
df7492f9 KH |
2430 | #define SAFE_CHARSET_P(coding, id) \ |
2431 | ((id) <= (coding)->max_charset_id \ | |
2432 | && (coding)->safe_charsets[id] >= 0) | |
2433 | ||
2434 | ||
2435 | #define SHIFT_OUT_OK(category) \ | |
2436 | (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0) | |
2437 | ||
2438 | static void | |
f0064e1f DL |
2439 | setup_iso_safe_charsets (attrs) |
2440 | Lisp_Object attrs; | |
df7492f9 KH |
2441 | { |
2442 | Lisp_Object charset_list, safe_charsets; | |
2443 | Lisp_Object request; | |
2444 | Lisp_Object reg_usage; | |
2445 | Lisp_Object tail; | |
2446 | int reg94, reg96; | |
2447 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
2448 | int max_charset_id; | |
2449 | ||
2450 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2451 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2452 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2453 | { | |
2454 | CODING_ATTR_CHARSET_LIST (attrs) | |
2455 | = charset_list = Viso_2022_charset_list; | |
2456 | ASET (attrs, coding_attr_safe_charsets, Qnil); | |
2457 | } | |
2458 | ||
2459 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
2460 | return; | |
2461 | ||
2462 | max_charset_id = 0; | |
2463 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2464 | { | |
2465 | int id = XINT (XCAR (tail)); | |
2466 | if (max_charset_id < id) | |
2467 | max_charset_id = id; | |
2468 | } | |
d46c5b12 | 2469 | |
df7492f9 KH |
2470 | safe_charsets = Fmake_string (make_number (max_charset_id + 1), |
2471 | make_number (255)); | |
2472 | request = AREF (attrs, coding_attr_iso_request); | |
2473 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
2474 | reg94 = XINT (XCAR (reg_usage)); | |
2475 | reg96 = XINT (XCDR (reg_usage)); | |
2476 | ||
2477 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2478 | { | |
2479 | Lisp_Object id; | |
2480 | Lisp_Object reg; | |
2481 | struct charset *charset; | |
2482 | ||
2483 | id = XCAR (tail); | |
2484 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 2485 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 2486 | if (! NILP (reg)) |
8f924df7 | 2487 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
2488 | else if (charset->iso_chars_96) |
2489 | { | |
2490 | if (reg96 < 4) | |
8f924df7 | 2491 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
2492 | } |
2493 | else | |
2494 | { | |
2495 | if (reg94 < 4) | |
8f924df7 | 2496 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
2497 | } |
2498 | } | |
2499 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
2500 | } | |
d46c5b12 | 2501 | |
b6871cc7 | 2502 | |
4ed46869 | 2503 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
2504 | Check if a text is encoded in one of ISO-2022 based codig systems. |
2505 | If it is, return 1, else return 0. */ | |
4ed46869 | 2506 | |
0a28aafb | 2507 | static int |
ff0dacd7 | 2508 | detect_coding_iso_2022 (coding, detect_info) |
df7492f9 | 2509 | struct coding_system *coding; |
ff0dacd7 | 2510 | struct coding_detection_info *detect_info; |
4ed46869 | 2511 | { |
8f924df7 KH |
2512 | const unsigned char *src = coding->source, *src_base = src; |
2513 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 2514 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 2515 | int single_shifting = 0; |
df7492f9 KH |
2516 | int id; |
2517 | int c, c1; | |
2518 | int consumed_chars = 0; | |
2519 | int i; | |
ff0dacd7 KH |
2520 | int rejected = 0; |
2521 | int found = 0; | |
2522 | ||
2523 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
2524 | |
2525 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
2526 | { | |
2527 | struct coding_system *this = &(coding_categories[i]); | |
2528 | Lisp_Object attrs, val; | |
2529 | ||
2530 | attrs = CODING_ID_ATTRS (this->id); | |
2531 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
2532 | && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs), Viso_2022_charset_list)) | |
2533 | setup_iso_safe_charsets (attrs); | |
2534 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 KH |
2535 | this->max_charset_id = SCHARS (val) - 1; |
2536 | this->safe_charsets = (char *) SDATA (val); | |
df7492f9 KH |
2537 | } |
2538 | ||
2539 | /* A coding system of this category is always ASCII compatible. */ | |
2540 | src += coding->head_ascii; | |
3f003981 | 2541 | |
ff0dacd7 | 2542 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 2543 | { |
065e3595 | 2544 | src_base = src; |
df7492f9 | 2545 | ONE_MORE_BYTE (c); |
4ed46869 KH |
2546 | switch (c) |
2547 | { | |
2548 | case ISO_CODE_ESC: | |
74383408 KH |
2549 | if (inhibit_iso_escape_detection) |
2550 | break; | |
f46869e4 | 2551 | single_shifting = 0; |
df7492f9 | 2552 | ONE_MORE_BYTE (c); |
d46c5b12 | 2553 | if (c >= '(' && c <= '/') |
4ed46869 | 2554 | { |
bf9cdd4e | 2555 | /* Designation sequence for a charset of dimension 1. */ |
df7492f9 | 2556 | ONE_MORE_BYTE (c1); |
d46c5b12 | 2557 | if (c1 < ' ' || c1 >= 0x80 |
df7492f9 | 2558 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) |
d46c5b12 KH |
2559 | /* Invalid designation sequence. Just ignore. */ |
2560 | break; | |
bf9cdd4e KH |
2561 | } |
2562 | else if (c == '$') | |
2563 | { | |
2564 | /* Designation sequence for a charset of dimension 2. */ | |
df7492f9 | 2565 | ONE_MORE_BYTE (c); |
bf9cdd4e KH |
2566 | if (c >= '@' && c <= 'B') |
2567 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
ff0dacd7 | 2568 | id = iso_charset_table[1][0][c]; |
bf9cdd4e | 2569 | else if (c >= '(' && c <= '/') |
bcf26d6a | 2570 | { |
df7492f9 | 2571 | ONE_MORE_BYTE (c1); |
d46c5b12 | 2572 | if (c1 < ' ' || c1 >= 0x80 |
df7492f9 | 2573 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) |
d46c5b12 KH |
2574 | /* Invalid designation sequence. Just ignore. */ |
2575 | break; | |
bcf26d6a | 2576 | } |
bf9cdd4e | 2577 | else |
ff0dacd7 | 2578 | /* Invalid designation sequence. Just ignore it. */ |
d46c5b12 KH |
2579 | break; |
2580 | } | |
ae9ff118 | 2581 | else if (c == 'N' || c == 'O') |
d46c5b12 | 2582 | { |
ae9ff118 | 2583 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
2584 | single_shifting = 1; |
2585 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
d46c5b12 | 2586 | break; |
4ed46869 | 2587 | } |
ec6d2bb8 KH |
2588 | else if (c >= '0' && c <= '4') |
2589 | { | |
2590 | /* ESC <Fp> for start/end composition. */ | |
ff0dacd7 | 2591 | found |= CATEGORY_MASK_ISO; |
ec6d2bb8 KH |
2592 | break; |
2593 | } | |
bf9cdd4e | 2594 | else |
df7492f9 | 2595 | { |
ff0dacd7 | 2596 | /* Invalid escape sequence. Just ignore it. */ |
df7492f9 KH |
2597 | break; |
2598 | } | |
d46c5b12 KH |
2599 | |
2600 | /* We found a valid designation sequence for CHARSET. */ | |
ff0dacd7 | 2601 | rejected |= CATEGORY_MASK_ISO_8BIT; |
df7492f9 KH |
2602 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], |
2603 | id)) | |
ff0dacd7 | 2604 | found |= CATEGORY_MASK_ISO_7; |
d46c5b12 | 2605 | else |
ff0dacd7 | 2606 | rejected |= CATEGORY_MASK_ISO_7; |
df7492f9 KH |
2607 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], |
2608 | id)) | |
ff0dacd7 | 2609 | found |= CATEGORY_MASK_ISO_7_TIGHT; |
d46c5b12 | 2610 | else |
ff0dacd7 | 2611 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; |
df7492f9 KH |
2612 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], |
2613 | id)) | |
ff0dacd7 | 2614 | found |= CATEGORY_MASK_ISO_7_ELSE; |
ae9ff118 | 2615 | else |
ff0dacd7 | 2616 | rejected |= CATEGORY_MASK_ISO_7_ELSE; |
df7492f9 KH |
2617 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], |
2618 | id)) | |
ff0dacd7 | 2619 | found |= CATEGORY_MASK_ISO_8_ELSE; |
ae9ff118 | 2620 | else |
ff0dacd7 | 2621 | rejected |= CATEGORY_MASK_ISO_8_ELSE; |
4ed46869 KH |
2622 | break; |
2623 | ||
4ed46869 | 2624 | case ISO_CODE_SO: |
d46c5b12 | 2625 | case ISO_CODE_SI: |
ff0dacd7 | 2626 | /* Locking shift out/in. */ |
74383408 KH |
2627 | if (inhibit_iso_escape_detection) |
2628 | break; | |
f46869e4 | 2629 | single_shifting = 0; |
ff0dacd7 KH |
2630 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
2631 | found |= CATEGORY_MASK_ISO_ELSE; | |
d46c5b12 KH |
2632 | break; |
2633 | ||
4ed46869 | 2634 | case ISO_CODE_CSI: |
ff0dacd7 | 2635 | /* Control sequence introducer. */ |
f46869e4 | 2636 | single_shifting = 0; |
ff0dacd7 KH |
2637 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
2638 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
2639 | goto check_extra_latin; | |
2640 | ||
4ed46869 KH |
2641 | case ISO_CODE_SS2: |
2642 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
2643 | /* Single shift. */ |
2644 | if (inhibit_iso_escape_detection) | |
2645 | break; | |
75e2a253 | 2646 | single_shifting = 0; |
ff0dacd7 KH |
2647 | rejected |= CATEGORY_MASK_ISO_7BIT; |
2648 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
2649 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
75e2a253 | 2650 | found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1; |
ff0dacd7 KH |
2651 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
2652 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
75e2a253 KH |
2653 | found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1; |
2654 | if (single_shifting) | |
2655 | break; | |
ff0dacd7 | 2656 | goto check_extra_latin; |
4ed46869 KH |
2657 | |
2658 | default: | |
065e3595 KH |
2659 | if (c < 0) |
2660 | continue; | |
4ed46869 | 2661 | if (c < 0x80) |
f46869e4 KH |
2662 | { |
2663 | single_shifting = 0; | |
2664 | break; | |
2665 | } | |
ff0dacd7 | 2666 | if (c >= 0xA0) |
c4825358 | 2667 | { |
ff0dacd7 KH |
2668 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
2669 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 2670 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
2671 | 0xA0..0FF. If the byte length is even, we include |
2672 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
2673 | only when we are not single shifting. */ | |
2674 | if (! single_shifting | |
2675 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 2676 | { |
e17de821 | 2677 | int i = 1; |
b73bfc1c KH |
2678 | while (src < src_end) |
2679 | { | |
df7492f9 | 2680 | ONE_MORE_BYTE (c); |
b73bfc1c KH |
2681 | if (c < 0xA0) |
2682 | break; | |
2683 | i++; | |
2684 | } | |
2685 | ||
2686 | if (i & 1 && src < src_end) | |
ff0dacd7 | 2687 | rejected |= CATEGORY_MASK_ISO_8_2; |
f46869e4 | 2688 | else |
ff0dacd7 | 2689 | found |= CATEGORY_MASK_ISO_8_2; |
f46869e4 | 2690 | } |
ff0dacd7 | 2691 | break; |
4ed46869 | 2692 | } |
ff0dacd7 KH |
2693 | check_extra_latin: |
2694 | single_shifting = 0; | |
2695 | if (! VECTORP (Vlatin_extra_code_table) | |
2696 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
2697 | { | |
2698 | rejected = CATEGORY_MASK_ISO; | |
2699 | break; | |
2700 | } | |
2701 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
2702 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
2703 | found |= CATEGORY_MASK_ISO_8_1; | |
2704 | else | |
2705 | rejected |= CATEGORY_MASK_ISO_8_1; | |
75e2a253 | 2706 | rejected |= CATEGORY_MASK_ISO_8_2; |
4ed46869 KH |
2707 | } |
2708 | } | |
ff0dacd7 KH |
2709 | detect_info->rejected |= CATEGORY_MASK_ISO; |
2710 | return 0; | |
4ed46869 | 2711 | |
df7492f9 | 2712 | no_more_source: |
ff0dacd7 KH |
2713 | detect_info->rejected |= rejected; |
2714 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 2715 | return 1; |
4ed46869 | 2716 | } |
ec6d2bb8 | 2717 | |
4ed46869 | 2718 | |
134b9549 KH |
2719 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
2720 | escape sequence should be kept. */ | |
df7492f9 KH |
2721 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
2722 | do { \ | |
2723 | int id, prev; \ | |
2724 | \ | |
2725 | if (final < '0' || final >= 128 \ | |
2726 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
2727 | || !SAFE_CHARSET_P (coding, id)) \ | |
2728 | { \ | |
2729 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
2730 | chars_96 = -1; \ |
2731 | break; \ | |
df7492f9 KH |
2732 | } \ |
2733 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
2734 | if (id == charset_jisx0201_roman) \ |
2735 | { \ | |
2736 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
2737 | id = charset_ascii; \ | |
2738 | } \ | |
2739 | else if (id == charset_jisx0208_1978) \ | |
2740 | { \ | |
2741 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
2742 | id = charset_jisx0208; \ | |
2743 | } \ | |
df7492f9 KH |
2744 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
2745 | /* If there was an invalid designation to REG previously, and this \ | |
2746 | designation is ASCII to REG, we should keep this designation \ | |
2747 | sequence. */ \ | |
2748 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 2749 | chars_96 = -1; \ |
4ed46869 KH |
2750 | } while (0) |
2751 | ||
d46c5b12 | 2752 | |
df7492f9 KH |
2753 | #define MAYBE_FINISH_COMPOSITION() \ |
2754 | do { \ | |
2755 | int i; \ | |
2756 | if (composition_state == COMPOSING_NO) \ | |
2757 | break; \ | |
2758 | /* It is assured that we have enough room for producing \ | |
2759 | characters stored in the table `components'. */ \ | |
2760 | if (charbuf + component_idx > charbuf_end) \ | |
2761 | goto no_more_source; \ | |
2762 | composition_state = COMPOSING_NO; \ | |
2763 | if (method == COMPOSITION_RELATIVE \ | |
2764 | || method == COMPOSITION_WITH_ALTCHARS) \ | |
2765 | { \ | |
2766 | for (i = 0; i < component_idx; i++) \ | |
2767 | *charbuf++ = components[i]; \ | |
2768 | char_offset += component_idx; \ | |
2769 | } \ | |
2770 | else \ | |
2771 | { \ | |
2772 | for (i = 0; i < component_idx; i += 2) \ | |
2773 | *charbuf++ = components[i]; \ | |
2774 | char_offset += (component_idx / 2) + 1; \ | |
2775 | } \ | |
2776 | } while (0) | |
2777 | ||
d46c5b12 | 2778 | |
aa72b389 KH |
2779 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
2780 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 | |
2781 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
2782 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
2783 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
aa72b389 | 2784 | */ |
ec6d2bb8 | 2785 | |
df7492f9 KH |
2786 | #define DECODE_COMPOSITION_START(c1) \ |
2787 | do { \ | |
2788 | if (c1 == '0' \ | |
781d7a48 | 2789 | && composition_state == COMPOSING_COMPONENT_RULE) \ |
df7492f9 KH |
2790 | { \ |
2791 | component_len = component_idx; \ | |
2792 | composition_state = COMPOSING_CHAR; \ | |
2793 | } \ | |
2794 | else \ | |
2795 | { \ | |
8f924df7 | 2796 | const unsigned char *p; \ |
df7492f9 KH |
2797 | \ |
2798 | MAYBE_FINISH_COMPOSITION (); \ | |
2799 | if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \ | |
2800 | goto no_more_source; \ | |
2801 | for (p = src; p < src_end - 1; p++) \ | |
2802 | if (*p == ISO_CODE_ESC && p[1] == '1') \ | |
2803 | break; \ | |
2804 | if (p == src_end - 1) \ | |
2805 | { \ | |
2806 | if (coding->mode & CODING_MODE_LAST_BLOCK) \ | |
2807 | goto invalid_code; \ | |
2808 | goto no_more_source; \ | |
2809 | } \ | |
2810 | \ | |
2811 | /* This is surely the start of a composition. */ \ | |
2812 | method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
2813 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
2814 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
2815 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
2816 | composition_state = (c1 <= '2' ? COMPOSING_CHAR \ | |
2817 | : COMPOSING_COMPONENT_CHAR); \ | |
2818 | component_idx = component_len = 0; \ | |
2819 | } \ | |
ec6d2bb8 KH |
2820 | } while (0) |
2821 | ||
ec6d2bb8 | 2822 | |
df7492f9 KH |
2823 | /* Handle compositoin end sequence ESC 1. */ |
2824 | ||
2825 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 2826 | do { \ |
df7492f9 KH |
2827 | int nchars = (component_len > 0 ? component_idx - component_len \ |
2828 | : method == COMPOSITION_RELATIVE ? component_idx \ | |
2829 | : (component_idx + 1) / 2); \ | |
2830 | int i; \ | |
2831 | int *saved_charbuf = charbuf; \ | |
2832 | \ | |
69a80ea3 | 2833 | ADD_COMPOSITION_DATA (charbuf, nchars, method); \ |
df7492f9 | 2834 | if (method != COMPOSITION_RELATIVE) \ |
ec6d2bb8 | 2835 | { \ |
df7492f9 KH |
2836 | if (component_len == 0) \ |
2837 | for (i = 0; i < component_idx; i++) \ | |
2838 | *charbuf++ = components[i]; \ | |
2839 | else \ | |
2840 | for (i = 0; i < component_len; i++) \ | |
2841 | *charbuf++ = components[i]; \ | |
2842 | *saved_charbuf = saved_charbuf - charbuf; \ | |
ec6d2bb8 | 2843 | } \ |
df7492f9 KH |
2844 | if (method == COMPOSITION_WITH_RULE) \ |
2845 | for (i = 0; i < component_idx; i += 2, char_offset++) \ | |
2846 | *charbuf++ = components[i]; \ | |
ec6d2bb8 | 2847 | else \ |
df7492f9 KH |
2848 | for (i = component_len; i < component_idx; i++, char_offset++) \ |
2849 | *charbuf++ = components[i]; \ | |
2850 | coding->annotated = 1; \ | |
2851 | composition_state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
2852 | } while (0) |
2853 | ||
df7492f9 | 2854 | |
ec6d2bb8 KH |
2855 | /* Decode a composition rule from the byte C1 (and maybe one more byte |
2856 | from SRC) and store one encoded composition rule in | |
2857 | coding->cmp_data. */ | |
2858 | ||
2859 | #define DECODE_COMPOSITION_RULE(c1) \ | |
2860 | do { \ | |
ec6d2bb8 KH |
2861 | (c1) -= 32; \ |
2862 | if (c1 < 81) /* old format (before ver.21) */ \ | |
2863 | { \ | |
2864 | int gref = (c1) / 9; \ | |
2865 | int nref = (c1) % 9; \ | |
2866 | if (gref == 4) gref = 10; \ | |
2867 | if (nref == 4) nref = 10; \ | |
df7492f9 | 2868 | c1 = COMPOSITION_ENCODE_RULE (gref, nref); \ |
ec6d2bb8 | 2869 | } \ |
b73bfc1c | 2870 | else if (c1 < 93) /* new format (after ver.21) */ \ |
ec6d2bb8 KH |
2871 | { \ |
2872 | ONE_MORE_BYTE (c2); \ | |
df7492f9 | 2873 | c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \ |
ec6d2bb8 | 2874 | } \ |
df7492f9 KH |
2875 | else \ |
2876 | c1 = 0; \ | |
ec6d2bb8 | 2877 | } while (0) |
88993dfd | 2878 | |
d46c5b12 | 2879 | |
4ed46869 KH |
2880 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
2881 | ||
b73bfc1c | 2882 | static void |
df7492f9 | 2883 | decode_coding_iso_2022 (coding) |
4ed46869 | 2884 | struct coding_system *coding; |
4ed46869 | 2885 | { |
8f924df7 KH |
2886 | const unsigned char *src = coding->source + coding->consumed; |
2887 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2888 | const unsigned char *src_base; | |
69a80ea3 | 2889 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ff0dacd7 | 2890 | int *charbuf_end |
69a80ea3 | 2891 | = coding->charbuf + coding->charbuf_size - 4 - MAX_ANNOTATION_LENGTH; |
df7492f9 | 2892 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2893 | int multibytep = coding->src_multibyte; |
4ed46869 | 2894 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
2895 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
2896 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 2897 | int charset_id_2, charset_id_3; |
df7492f9 KH |
2898 | struct charset *charset; |
2899 | int c; | |
2900 | /* For handling composition sequence. */ | |
2901 | #define COMPOSING_NO 0 | |
2902 | #define COMPOSING_CHAR 1 | |
2903 | #define COMPOSING_RULE 2 | |
2904 | #define COMPOSING_COMPONENT_CHAR 3 | |
2905 | #define COMPOSING_COMPONENT_RULE 4 | |
2906 | ||
2907 | int composition_state = COMPOSING_NO; | |
2908 | enum composition_method method; | |
2909 | int components[MAX_COMPOSITION_COMPONENTS * 2 + 1]; | |
2910 | int component_idx; | |
2911 | int component_len; | |
24a73b0a | 2912 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2913 | int char_offset = coding->produced_char; |
2914 | int last_offset = char_offset; | |
2915 | int last_id = charset_ascii; | |
df7492f9 | 2916 | |
24a73b0a | 2917 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 2918 | setup_iso_safe_charsets (attrs); |
b73bfc1c KH |
2919 | |
2920 | while (1) | |
4ed46869 | 2921 | { |
463f5630 | 2922 | int c1, c2; |
b73bfc1c KH |
2923 | |
2924 | src_base = src; | |
df7492f9 KH |
2925 | consumed_chars_base = consumed_chars; |
2926 | ||
2927 | if (charbuf >= charbuf_end) | |
2928 | break; | |
2929 | ||
b73bfc1c | 2930 | ONE_MORE_BYTE (c1); |
065e3595 KH |
2931 | if (c1 < 0) |
2932 | goto invalid_code; | |
4ed46869 | 2933 | |
98725083 | 2934 | /* We produce at most one character. */ |
4ed46869 KH |
2935 | switch (iso_code_class [c1]) |
2936 | { | |
2937 | case ISO_0x20_or_0x7F: | |
df7492f9 | 2938 | if (composition_state != COMPOSING_NO) |
ec6d2bb8 | 2939 | { |
df7492f9 KH |
2940 | if (composition_state == COMPOSING_RULE |
2941 | || composition_state == COMPOSING_COMPONENT_RULE) | |
2942 | { | |
2943 | DECODE_COMPOSITION_RULE (c1); | |
2944 | components[component_idx++] = c1; | |
2945 | composition_state--; | |
2946 | continue; | |
2947 | } | |
4ed46869 | 2948 | } |
df7492f9 KH |
2949 | if (charset_id_0 < 0 |
2950 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
2951 | /* This is SPACE or DEL. */ |
2952 | charset = CHARSET_FROM_ID (charset_ascii); | |
2953 | else | |
2954 | charset = CHARSET_FROM_ID (charset_id_0); | |
2955 | break; | |
4ed46869 KH |
2956 | |
2957 | case ISO_graphic_plane_0: | |
781d7a48 | 2958 | if (composition_state != COMPOSING_NO) |
b73bfc1c | 2959 | { |
781d7a48 KH |
2960 | if (composition_state == COMPOSING_RULE |
2961 | || composition_state == COMPOSING_COMPONENT_RULE) | |
2962 | { | |
2963 | DECODE_COMPOSITION_RULE (c1); | |
2964 | components[component_idx++] = c1; | |
2965 | composition_state--; | |
2966 | continue; | |
2967 | } | |
b73bfc1c | 2968 | } |
134b9549 KH |
2969 | if (charset_id_0 < 0) |
2970 | charset = CHARSET_FROM_ID (charset_ascii); | |
2971 | else | |
2972 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
2973 | break; |
2974 | ||
2975 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
2976 | if (charset_id_1 < 0 |
2977 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
2978 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
2979 | goto invalid_code; | |
4ed46869 KH |
2980 | /* This is a graphic character, we fall down ... */ |
2981 | ||
2982 | case ISO_graphic_plane_1: | |
df7492f9 KH |
2983 | if (charset_id_1 < 0) |
2984 | goto invalid_code; | |
2985 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
2986 | break; |
2987 | ||
df7492f9 KH |
2988 | case ISO_control_0: |
2989 | MAYBE_FINISH_COMPOSITION (); | |
2990 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
2991 | break; |
2992 | ||
df7492f9 KH |
2993 | case ISO_control_1: |
2994 | MAYBE_FINISH_COMPOSITION (); | |
2995 | goto invalid_code; | |
2996 | ||
4ed46869 | 2997 | case ISO_shift_out: |
df7492f9 KH |
2998 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
2999 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3000 | goto invalid_code; | |
3001 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3002 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3003 | continue; |
4ed46869 KH |
3004 | |
3005 | case ISO_shift_in: | |
df7492f9 KH |
3006 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3007 | goto invalid_code; | |
3008 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3009 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3010 | continue; |
4ed46869 KH |
3011 | |
3012 | case ISO_single_shift_2_7: | |
3013 | case ISO_single_shift_2: | |
df7492f9 KH |
3014 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3015 | goto invalid_code; | |
4ed46869 KH |
3016 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3017 | c1 = 'N'; | |
3018 | goto label_escape_sequence; | |
3019 | ||
3020 | case ISO_single_shift_3: | |
df7492f9 KH |
3021 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3022 | goto invalid_code; | |
4ed46869 KH |
3023 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3024 | c1 = 'O'; | |
3025 | goto label_escape_sequence; | |
3026 | ||
3027 | case ISO_control_sequence_introducer: | |
3028 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3029 | c1 = '['; | |
3030 | goto label_escape_sequence; | |
3031 | ||
3032 | case ISO_escape: | |
3033 | ONE_MORE_BYTE (c1); | |
3034 | label_escape_sequence: | |
df7492f9 | 3035 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3036 | designation, direction specification, and character |
3037 | composition specification. */ | |
3038 | switch (c1) | |
3039 | { | |
3040 | case '&': /* revision of following character set */ | |
3041 | ONE_MORE_BYTE (c1); | |
3042 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3043 | goto invalid_code; |
4ed46869 KH |
3044 | ONE_MORE_BYTE (c1); |
3045 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3046 | goto invalid_code; |
4ed46869 KH |
3047 | ONE_MORE_BYTE (c1); |
3048 | goto label_escape_sequence; | |
3049 | ||
3050 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3051 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3052 | goto invalid_code; | |
134b9549 KH |
3053 | { |
3054 | int reg, chars96; | |
3055 | ||
3056 | ONE_MORE_BYTE (c1); | |
3057 | if (c1 >= '@' && c1 <= 'B') | |
3058 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3059 | or JISX0208.1980 */ |
134b9549 KH |
3060 | reg = 0, chars96 = 0; |
3061 | } | |
3062 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3063 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3064 | reg = c1 - 0x28, chars96 = 0; | |
3065 | ONE_MORE_BYTE (c1); | |
3066 | } | |
3067 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3068 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3069 | reg = c1 - 0x2C, chars96 = 1; | |
3070 | ONE_MORE_BYTE (c1); | |
3071 | } | |
3072 | else | |
3073 | goto invalid_code; | |
3074 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3075 | /* We must update these variables now. */ | |
3076 | if (reg == 0) | |
3077 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3078 | else if (reg == 1) | |
3079 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3080 | if (chars96 < 0) | |
3081 | goto invalid_code; | |
3082 | } | |
b73bfc1c | 3083 | continue; |
4ed46869 KH |
3084 | |
3085 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3086 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3087 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3088 | goto invalid_code; | |
3089 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3090 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3091 | continue; |
4ed46869 KH |
3092 | |
3093 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3094 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3095 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3096 | goto invalid_code; | |
3097 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3098 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3099 | continue; |
4ed46869 KH |
3100 | |
3101 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3102 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3103 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3104 | goto invalid_code; | |
134b9549 KH |
3105 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3106 | if (charset_id_2 < 0) | |
3107 | charset = CHARSET_FROM_ID (charset_ascii); | |
3108 | else | |
3109 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3110 | ONE_MORE_BYTE (c1); |
e7046a18 | 3111 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3112 | goto invalid_code; |
4ed46869 KH |
3113 | break; |
3114 | ||
3115 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3116 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3117 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3118 | goto invalid_code; | |
134b9549 KH |
3119 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3120 | if (charset_id_3 < 0) | |
3121 | charset = CHARSET_FROM_ID (charset_ascii); | |
3122 | else | |
3123 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3124 | ONE_MORE_BYTE (c1); |
e7046a18 | 3125 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3126 | goto invalid_code; |
4ed46869 KH |
3127 | break; |
3128 | ||
ec6d2bb8 | 3129 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3130 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3131 | goto invalid_code; | |
ec6d2bb8 | 3132 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3133 | continue; |
4ed46869 | 3134 | |
ec6d2bb8 | 3135 | case '1': /* end composition */ |
df7492f9 KH |
3136 | if (composition_state == COMPOSING_NO) |
3137 | goto invalid_code; | |
3138 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3139 | continue; |
4ed46869 KH |
3140 | |
3141 | case '[': /* specification of direction */ | |
df7492f9 KH |
3142 | if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION) |
3143 | goto invalid_code; | |
4ed46869 | 3144 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3145 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
df7492f9 | 3146 | left-to-right, and nozero means right-to-left. */ |
4ed46869 KH |
3147 | ONE_MORE_BYTE (c1); |
3148 | switch (c1) | |
3149 | { | |
3150 | case ']': /* end of the current direction */ | |
d46c5b12 | 3151 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3152 | |
3153 | case '0': /* end of the current direction */ | |
3154 | case '1': /* start of left-to-right direction */ | |
3155 | ONE_MORE_BYTE (c1); | |
3156 | if (c1 == ']') | |
d46c5b12 | 3157 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3158 | else |
df7492f9 | 3159 | goto invalid_code; |
4ed46869 KH |
3160 | break; |
3161 | ||
3162 | case '2': /* start of right-to-left direction */ | |
3163 | ONE_MORE_BYTE (c1); | |
3164 | if (c1 == ']') | |
d46c5b12 | 3165 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3166 | else |
df7492f9 | 3167 | goto invalid_code; |
4ed46869 KH |
3168 | break; |
3169 | ||
3170 | default: | |
df7492f9 | 3171 | goto invalid_code; |
4ed46869 | 3172 | } |
b73bfc1c | 3173 | continue; |
4ed46869 | 3174 | |
103e0180 | 3175 | case '%': |
103e0180 KH |
3176 | ONE_MORE_BYTE (c1); |
3177 | if (c1 == '/') | |
3178 | { | |
3179 | /* CTEXT extended segment: | |
3180 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3181 | We keep these bytes as is for the moment. | |
3182 | They may be decoded by post-read-conversion. */ | |
3183 | int dim, M, L; | |
4776e638 | 3184 | int size; |
8f924df7 | 3185 | |
103e0180 KH |
3186 | ONE_MORE_BYTE (dim); |
3187 | ONE_MORE_BYTE (M); | |
3188 | ONE_MORE_BYTE (L); | |
3189 | size = ((M - 128) * 128) + (L - 128); | |
4776e638 KH |
3190 | if (charbuf + 8 + size > charbuf_end) |
3191 | goto break_loop; | |
3192 | *charbuf++ = ISO_CODE_ESC; | |
3193 | *charbuf++ = '%'; | |
3194 | *charbuf++ = '/'; | |
3195 | *charbuf++ = dim; | |
3196 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3197 | *charbuf++ = BYTE8_TO_CHAR (L); | |
103e0180 KH |
3198 | while (size-- > 0) |
3199 | { | |
3200 | ONE_MORE_BYTE (c1); | |
4776e638 | 3201 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
103e0180 | 3202 | } |
103e0180 KH |
3203 | } |
3204 | else if (c1 == 'G') | |
3205 | { | |
103e0180 KH |
3206 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3207 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3208 | We keep these bytes as is for the moment. | |
3209 | They may be decoded by post-read-conversion. */ | |
4776e638 KH |
3210 | int *p = charbuf; |
3211 | ||
3212 | if (p + 6 > charbuf_end) | |
3213 | goto break_loop; | |
3214 | *p++ = ISO_CODE_ESC; | |
3215 | *p++ = '%'; | |
3216 | *p++ = 'G'; | |
3217 | while (p < charbuf_end) | |
103e0180 KH |
3218 | { |
3219 | ONE_MORE_BYTE (c1); | |
3220 | if (c1 == ISO_CODE_ESC | |
3221 | && src + 1 < src_end | |
3222 | && src[0] == '%' | |
3223 | && src[1] == '@') | |
9ffd559c KH |
3224 | { |
3225 | src += 2; | |
3226 | break; | |
3227 | } | |
4776e638 | 3228 | *p++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
103e0180 | 3229 | } |
4776e638 KH |
3230 | if (p + 3 > charbuf_end) |
3231 | goto break_loop; | |
3232 | *p++ = ISO_CODE_ESC; | |
3233 | *p++ = '%'; | |
3234 | *p++ = '@'; | |
3235 | charbuf = p; | |
103e0180 KH |
3236 | } |
3237 | else | |
4776e638 | 3238 | goto invalid_code; |
103e0180 | 3239 | continue; |
4776e638 | 3240 | break; |
103e0180 | 3241 | |
4ed46869 | 3242 | default: |
df7492f9 KH |
3243 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3244 | goto invalid_code; | |
134b9549 KH |
3245 | { |
3246 | int reg, chars96; | |
3247 | ||
3248 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3249 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3250 | reg = c1 - 0x28, chars96 = 0; | |
3251 | ONE_MORE_BYTE (c1); | |
3252 | } | |
3253 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3254 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3255 | reg = c1 - 0x2C, chars96 = 1; | |
3256 | ONE_MORE_BYTE (c1); | |
3257 | } | |
3258 | else | |
3259 | goto invalid_code; | |
3260 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3261 | /* We must update these variables now. */ | |
3262 | if (reg == 0) | |
3263 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3264 | else if (reg == 1) | |
3265 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3266 | if (chars96 < 0) | |
3267 | goto invalid_code; | |
3268 | } | |
b73bfc1c | 3269 | continue; |
4ed46869 | 3270 | } |
b73bfc1c | 3271 | } |
4ed46869 | 3272 | |
ff0dacd7 KH |
3273 | if (charset->id != charset_ascii |
3274 | && last_id != charset->id) | |
3275 | { | |
3276 | if (last_id != charset_ascii) | |
69a80ea3 | 3277 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3278 | last_id = charset->id; |
3279 | last_offset = char_offset; | |
3280 | } | |
3281 | ||
b73bfc1c | 3282 | /* Now we know CHARSET and 1st position code C1 of a character. |
df7492f9 KH |
3283 | Produce a decoded character while getting 2nd position code |
3284 | C2 if necessary. */ | |
3285 | c1 &= 0x7F; | |
3286 | if (CHARSET_DIMENSION (charset) > 1) | |
b73bfc1c KH |
3287 | { |
3288 | ONE_MORE_BYTE (c2); | |
df7492f9 | 3289 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)) |
b73bfc1c | 3290 | /* C2 is not in a valid range. */ |
df7492f9 KH |
3291 | goto invalid_code; |
3292 | c1 = (c1 << 8) | (c2 & 0x7F); | |
3293 | if (CHARSET_DIMENSION (charset) > 2) | |
3294 | { | |
3295 | ONE_MORE_BYTE (c2); | |
3296 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)) | |
3297 | /* C2 is not in a valid range. */ | |
3298 | goto invalid_code; | |
3299 | c1 = (c1 << 8) | (c2 & 0x7F); | |
3300 | } | |
3301 | } | |
3302 | ||
3303 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); | |
3304 | if (c < 0) | |
3305 | { | |
3306 | MAYBE_FINISH_COMPOSITION (); | |
3307 | for (; src_base < src; src_base++, char_offset++) | |
3308 | { | |
3309 | if (ASCII_BYTE_P (*src_base)) | |
3310 | *charbuf++ = *src_base; | |
3311 | else | |
3312 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
3313 | } | |
3314 | } | |
3315 | else if (composition_state == COMPOSING_NO) | |
3316 | { | |
3317 | *charbuf++ = c; | |
3318 | char_offset++; | |
4ed46869 | 3319 | } |
df7492f9 | 3320 | else |
781d7a48 KH |
3321 | { |
3322 | components[component_idx++] = c; | |
3323 | if (method == COMPOSITION_WITH_RULE | |
3324 | || (method == COMPOSITION_WITH_RULE_ALTCHARS | |
3325 | && composition_state == COMPOSING_COMPONENT_CHAR)) | |
3326 | composition_state++; | |
4ed46869 KH |
3327 | } |
3328 | continue; | |
3329 | ||
df7492f9 KH |
3330 | invalid_code: |
3331 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 3332 | src = src_base; |
df7492f9 KH |
3333 | consumed_chars = consumed_chars_base; |
3334 | ONE_MORE_BYTE (c); | |
065e3595 | 3335 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 3336 | char_offset++; |
df7492f9 | 3337 | coding->errors++; |
4776e638 KH |
3338 | continue; |
3339 | ||
3340 | break_loop: | |
3341 | break; | |
4ed46869 | 3342 | } |
fb88bf2d | 3343 | |
df7492f9 | 3344 | no_more_source: |
ff0dacd7 | 3345 | if (last_id != charset_ascii) |
69a80ea3 | 3346 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
3347 | coding->consumed_char += consumed_chars_base; |
3348 | coding->consumed = src_base - coding->source; | |
3349 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
3350 | } |
3351 | ||
b73bfc1c | 3352 | |
f4dee582 | 3353 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
3354 | |
3355 | /* | |
f4dee582 | 3356 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 3357 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 3358 | variant has the following specifications: |
df7492f9 | 3359 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
3360 | 2. Allows short-form designation? |
3361 | 3. ASCII should be designated to G0 before control characters? | |
3362 | 4. ASCII should be designated to G0 at end of line? | |
3363 | 5. 7-bit environment or 8-bit environment? | |
3364 | 6. Use locking-shift? | |
3365 | 7. Use Single-shift? | |
3366 | And the following two are only for Japanese: | |
3367 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
3368 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
3369 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
3370 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 3371 | details. |
4ed46869 KH |
3372 | */ |
3373 | ||
3374 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
3375 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
3376 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
3377 | designation sequence of short-form. */ | |
4ed46869 KH |
3378 | |
3379 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
3380 | do { \ | |
df7492f9 | 3381 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
4ed46869 KH |
3382 | char *intermediate_char_94 = "()*+"; \ |
3383 | char *intermediate_char_96 = ",-./"; \ | |
df7492f9 KH |
3384 | int revision = -1; \ |
3385 | int c; \ | |
3386 | \ | |
3387 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 3388 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
3389 | \ |
3390 | if (revision >= 0) \ | |
70c22245 | 3391 | { \ |
df7492f9 KH |
3392 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
3393 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 3394 | } \ |
df7492f9 | 3395 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
3396 | if (CHARSET_DIMENSION (charset) == 1) \ |
3397 | { \ | |
df7492f9 KH |
3398 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
3399 | c = intermediate_char_94[reg]; \ | |
4ed46869 | 3400 | else \ |
df7492f9 KH |
3401 | c = intermediate_char_96[reg]; \ |
3402 | EMIT_ONE_ASCII_BYTE (c); \ | |
4ed46869 KH |
3403 | } \ |
3404 | else \ | |
3405 | { \ | |
df7492f9 KH |
3406 | EMIT_ONE_ASCII_BYTE ('$'); \ |
3407 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 3408 | { \ |
df7492f9 | 3409 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
3410 | || reg != 0 \ |
3411 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 3412 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
3413 | } \ |
3414 | else \ | |
df7492f9 | 3415 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 3416 | } \ |
df7492f9 KH |
3417 | EMIT_ONE_ASCII_BYTE (final_char); \ |
3418 | \ | |
3419 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
3420 | } while (0) |
3421 | ||
df7492f9 | 3422 | |
4ed46869 KH |
3423 | /* The following two macros produce codes (control character or escape |
3424 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
3425 | single-shift-3). */ | |
3426 | ||
df7492f9 KH |
3427 | #define ENCODE_SINGLE_SHIFT_2 \ |
3428 | do { \ | |
3429 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
3430 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
3431 | else \ | |
3432 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
3433 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
3434 | } while (0) |
3435 | ||
df7492f9 KH |
3436 | |
3437 | #define ENCODE_SINGLE_SHIFT_3 \ | |
3438 | do { \ | |
3439 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
3440 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
3441 | else \ | |
3442 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
3443 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
3444 | } while (0) |
3445 | ||
df7492f9 | 3446 | |
4ed46869 KH |
3447 | /* The following four macros produce codes (control character or |
3448 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
3449 | shift-out, locking-shift-2, and locking-shift-3). */ | |
3450 | ||
df7492f9 KH |
3451 | #define ENCODE_SHIFT_IN \ |
3452 | do { \ | |
3453 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
3454 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
3455 | } while (0) |
3456 | ||
df7492f9 KH |
3457 | |
3458 | #define ENCODE_SHIFT_OUT \ | |
3459 | do { \ | |
3460 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
3461 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
3462 | } while (0) |
3463 | ||
df7492f9 KH |
3464 | |
3465 | #define ENCODE_LOCKING_SHIFT_2 \ | |
3466 | do { \ | |
3467 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
3468 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
3469 | } while (0) |
3470 | ||
df7492f9 KH |
3471 | |
3472 | #define ENCODE_LOCKING_SHIFT_3 \ | |
3473 | do { \ | |
3474 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
3475 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
3476 | } while (0) |
3477 | ||
df7492f9 | 3478 | |
f4dee582 RS |
3479 | /* Produce codes for a DIMENSION1 character whose character set is |
3480 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
3481 | sequences are also produced in advance if necessary. */ |
3482 | ||
6e85d753 KH |
3483 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
3484 | do { \ | |
df7492f9 | 3485 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
3486 | \ |
3487 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3488 | && id == charset_ascii) \ | |
3489 | { \ | |
3490 | id = charset_jisx0201_roman; \ | |
3491 | charset = CHARSET_FROM_ID (id); \ | |
3492 | } \ | |
3493 | \ | |
df7492f9 | 3494 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 3495 | { \ |
df7492f9 KH |
3496 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
3497 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 3498 | else \ |
df7492f9 KH |
3499 | EMIT_ONE_BYTE (c1 | 0x80); \ |
3500 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
3501 | break; \ |
3502 | } \ | |
df7492f9 | 3503 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 3504 | { \ |
df7492f9 | 3505 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
3506 | break; \ |
3507 | } \ | |
df7492f9 | 3508 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 3509 | { \ |
df7492f9 | 3510 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
3511 | break; \ |
3512 | } \ | |
6e85d753 KH |
3513 | else \ |
3514 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
3515 | must invoke it, or, at first, designate it to some graphic \ | |
3516 | register. Then repeat the loop to actually produce the \ | |
3517 | character. */ \ | |
df7492f9 KH |
3518 | dst = encode_invocation_designation (charset, coding, dst, \ |
3519 | &produced_chars); \ | |
4ed46869 KH |
3520 | } while (1) |
3521 | ||
df7492f9 | 3522 | |
f4dee582 RS |
3523 | /* Produce codes for a DIMENSION2 character whose character set is |
3524 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
3525 | invocation codes are also produced in advance if necessary. */ |
3526 | ||
6e85d753 KH |
3527 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
3528 | do { \ | |
df7492f9 | 3529 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
3530 | \ |
3531 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3532 | && id == charset_jisx0208) \ | |
3533 | { \ | |
3534 | id = charset_jisx0208_1978; \ | |
3535 | charset = CHARSET_FROM_ID (id); \ | |
3536 | } \ | |
3537 | \ | |
df7492f9 | 3538 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 3539 | { \ |
df7492f9 KH |
3540 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
3541 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 3542 | else \ |
df7492f9 KH |
3543 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
3544 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
3545 | break; \ |
3546 | } \ | |
df7492f9 | 3547 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 3548 | { \ |
df7492f9 | 3549 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
3550 | break; \ |
3551 | } \ | |
df7492f9 | 3552 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 3553 | { \ |
df7492f9 | 3554 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
3555 | break; \ |
3556 | } \ | |
6e85d753 KH |
3557 | else \ |
3558 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
3559 | must invoke it, or, at first, designate it to some graphic \ | |
3560 | register. Then repeat the loop to actually produce the \ | |
3561 | character. */ \ | |
df7492f9 KH |
3562 | dst = encode_invocation_designation (charset, coding, dst, \ |
3563 | &produced_chars); \ | |
4ed46869 KH |
3564 | } while (1) |
3565 | ||
05e6f5dc | 3566 | |
df7492f9 KH |
3567 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
3568 | do { \ | |
3569 | int code = ENCODE_CHAR ((charset),(c)); \ | |
3570 | \ | |
3571 | if (CHARSET_DIMENSION (charset) == 1) \ | |
3572 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
3573 | else \ | |
3574 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 3575 | } while (0) |
bdd9fb48 | 3576 | |
05e6f5dc | 3577 | |
4ed46869 | 3578 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 3579 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
3580 | Return new DST. */ |
3581 | ||
3582 | unsigned char * | |
df7492f9 KH |
3583 | encode_invocation_designation (charset, coding, dst, p_nchars) |
3584 | struct charset *charset; | |
4ed46869 KH |
3585 | struct coding_system *coding; |
3586 | unsigned char *dst; | |
df7492f9 | 3587 | int *p_nchars; |
4ed46869 | 3588 | { |
df7492f9 KH |
3589 | int multibytep = coding->dst_multibyte; |
3590 | int produced_chars = *p_nchars; | |
4ed46869 | 3591 | int reg; /* graphic register number */ |
df7492f9 | 3592 | int id = CHARSET_ID (charset); |
4ed46869 KH |
3593 | |
3594 | /* At first, check designations. */ | |
3595 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 3596 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
3597 | break; |
3598 | ||
3599 | if (reg >= 4) | |
3600 | { | |
3601 | /* CHARSET is not yet designated to any graphic registers. */ | |
3602 | /* At first check the requested designation. */ | |
df7492f9 KH |
3603 | reg = CODING_ISO_REQUEST (coding, id); |
3604 | if (reg < 0) | |
1ba9e4ab KH |
3605 | /* Since CHARSET requests no special designation, designate it |
3606 | to graphic register 0. */ | |
4ed46869 KH |
3607 | reg = 0; |
3608 | ||
3609 | ENCODE_DESIGNATION (charset, reg, coding); | |
3610 | } | |
3611 | ||
df7492f9 KH |
3612 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
3613 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
3614 | { |
3615 | /* Since the graphic register REG is not invoked to any graphic | |
3616 | planes, invoke it to graphic plane 0. */ | |
3617 | switch (reg) | |
3618 | { | |
3619 | case 0: /* graphic register 0 */ | |
3620 | ENCODE_SHIFT_IN; | |
3621 | break; | |
3622 | ||
3623 | case 1: /* graphic register 1 */ | |
3624 | ENCODE_SHIFT_OUT; | |
3625 | break; | |
3626 | ||
3627 | case 2: /* graphic register 2 */ | |
df7492f9 | 3628 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
3629 | ENCODE_SINGLE_SHIFT_2; |
3630 | else | |
3631 | ENCODE_LOCKING_SHIFT_2; | |
3632 | break; | |
3633 | ||
3634 | case 3: /* graphic register 3 */ | |
df7492f9 | 3635 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
3636 | ENCODE_SINGLE_SHIFT_3; |
3637 | else | |
3638 | ENCODE_LOCKING_SHIFT_3; | |
3639 | break; | |
3640 | } | |
3641 | } | |
b73bfc1c | 3642 | |
df7492f9 | 3643 | *p_nchars = produced_chars; |
4ed46869 KH |
3644 | return dst; |
3645 | } | |
3646 | ||
df7492f9 KH |
3647 | /* The following three macros produce codes for indicating direction |
3648 | of text. */ | |
3649 | #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \ | |
ec6d2bb8 | 3650 | do { \ |
df7492f9 KH |
3651 | if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \ |
3652 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \ | |
ec6d2bb8 | 3653 | else \ |
df7492f9 | 3654 | EMIT_ONE_BYTE (ISO_CODE_CSI); \ |
ec6d2bb8 KH |
3655 | } while (0) |
3656 | ||
ec6d2bb8 | 3657 | |
df7492f9 KH |
3658 | #define ENCODE_DIRECTION_R2L() \ |
3659 | do { \ | |
3660 | ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \ | |
3661 | EMIT_TWO_ASCII_BYTES ('2', ']'); \ | |
ec6d2bb8 KH |
3662 | } while (0) |
3663 | ||
ec6d2bb8 | 3664 | |
df7492f9 | 3665 | #define ENCODE_DIRECTION_L2R() \ |
ec6d2bb8 | 3666 | do { \ |
df7492f9 KH |
3667 | ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \ |
3668 | EMIT_TWO_ASCII_BYTES ('0', ']'); \ | |
ec6d2bb8 | 3669 | } while (0) |
4ed46869 | 3670 | |
4ed46869 KH |
3671 | |
3672 | /* Produce codes for designation and invocation to reset the graphic | |
3673 | planes and registers to initial state. */ | |
df7492f9 KH |
3674 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
3675 | do { \ | |
3676 | int reg; \ | |
3677 | struct charset *charset; \ | |
3678 | \ | |
3679 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
3680 | ENCODE_SHIFT_IN; \ | |
3681 | for (reg = 0; reg < 4; reg++) \ | |
3682 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
3683 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
3684 | != CODING_ISO_INITIAL (coding, reg))) \ | |
3685 | { \ | |
3686 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
3687 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
3688 | } \ | |
4ed46869 KH |
3689 | } while (0) |
3690 | ||
df7492f9 | 3691 | |
bdd9fb48 | 3692 | /* Produce designation sequences of charsets in the line started from |
b73bfc1c | 3693 | SRC to a place pointed by DST, and return updated DST. |
bdd9fb48 KH |
3694 | |
3695 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
3696 | find all the necessary designations. */ |
3697 | ||
b73bfc1c | 3698 | static unsigned char * |
df7492f9 | 3699 | encode_designation_at_bol (coding, charbuf, charbuf_end, dst) |
e0e989f6 | 3700 | struct coding_system *coding; |
df7492f9 KH |
3701 | int *charbuf, *charbuf_end; |
3702 | unsigned char *dst; | |
e0e989f6 | 3703 | { |
df7492f9 | 3704 | struct charset *charset; |
bdd9fb48 KH |
3705 | /* Table of charsets to be designated to each graphic register. */ |
3706 | int r[4]; | |
df7492f9 KH |
3707 | int c, found = 0, reg; |
3708 | int produced_chars = 0; | |
3709 | int multibytep = coding->dst_multibyte; | |
3710 | Lisp_Object attrs; | |
3711 | Lisp_Object charset_list; | |
3712 | ||
3713 | attrs = CODING_ID_ATTRS (coding->id); | |
3714 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
3715 | if (EQ (charset_list, Qiso_2022)) | |
3716 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
3717 | |
3718 | for (reg = 0; reg < 4; reg++) | |
3719 | r[reg] = -1; | |
3720 | ||
b73bfc1c | 3721 | while (found < 4) |
e0e989f6 | 3722 | { |
df7492f9 KH |
3723 | int id; |
3724 | ||
3725 | c = *charbuf++; | |
b73bfc1c KH |
3726 | if (c == '\n') |
3727 | break; | |
df7492f9 KH |
3728 | charset = char_charset (c, charset_list, NULL); |
3729 | id = CHARSET_ID (charset); | |
3730 | reg = CODING_ISO_REQUEST (coding, id); | |
3731 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
3732 | { |
3733 | found++; | |
df7492f9 | 3734 | r[reg] = id; |
bdd9fb48 | 3735 | } |
bdd9fb48 KH |
3736 | } |
3737 | ||
3738 | if (found) | |
3739 | { | |
3740 | for (reg = 0; reg < 4; reg++) | |
3741 | if (r[reg] >= 0 | |
df7492f9 KH |
3742 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
3743 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 3744 | } |
b73bfc1c KH |
3745 | |
3746 | return dst; | |
e0e989f6 KH |
3747 | } |
3748 | ||
4ed46869 KH |
3749 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
3750 | ||
df7492f9 KH |
3751 | static int |
3752 | encode_coding_iso_2022 (coding) | |
4ed46869 | 3753 | struct coding_system *coding; |
4ed46869 | 3754 | { |
df7492f9 KH |
3755 | int multibytep = coding->dst_multibyte; |
3756 | int *charbuf = coding->charbuf; | |
3757 | int *charbuf_end = charbuf + coding->charbuf_used; | |
3758 | unsigned char *dst = coding->destination + coding->produced; | |
3759 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
3760 | int safe_room = 16; | |
3761 | int bol_designation | |
3762 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL | |
3763 | && CODING_ISO_BOL (coding)); | |
3764 | int produced_chars = 0; | |
3765 | Lisp_Object attrs, eol_type, charset_list; | |
3766 | int ascii_compatible; | |
b73bfc1c | 3767 | int c; |
ff0dacd7 | 3768 | int preferred_charset_id = -1; |
05e6f5dc | 3769 | |
24a73b0a KH |
3770 | CODING_GET_INFO (coding, attrs, charset_list); |
3771 | eol_type = CODING_ID_EOL_TYPE (coding->id); | |
3772 | if (VECTORP (eol_type)) | |
3773 | eol_type = Qunix; | |
3774 | ||
004068e4 | 3775 | setup_iso_safe_charsets (attrs); |
ff0dacd7 KH |
3776 | /* Charset list may have been changed. */ |
3777 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); \ | |
8f924df7 | 3778 | coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs)); |
0eecad43 | 3779 | |
df7492f9 | 3780 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
bdd9fb48 | 3781 | |
df7492f9 | 3782 | while (charbuf < charbuf_end) |
4ed46869 | 3783 | { |
df7492f9 | 3784 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 3785 | |
df7492f9 | 3786 | if (bol_designation) |
b73bfc1c | 3787 | { |
df7492f9 | 3788 | unsigned char *dst_prev = dst; |
4ed46869 | 3789 | |
bdd9fb48 | 3790 | /* We have to produce designation sequences if any now. */ |
df7492f9 KH |
3791 | dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst); |
3792 | bol_designation = 0; | |
3793 | /* We are sure that designation sequences are all ASCII bytes. */ | |
3794 | produced_chars += dst - dst_prev; | |
e0e989f6 KH |
3795 | } |
3796 | ||
df7492f9 | 3797 | c = *charbuf++; |
ec6d2bb8 | 3798 | |
ff0dacd7 KH |
3799 | if (c < 0) |
3800 | { | |
3801 | /* Handle an annotation. */ | |
3802 | switch (*charbuf) | |
ec6d2bb8 | 3803 | { |
ff0dacd7 KH |
3804 | case CODING_ANNOTATE_COMPOSITION_MASK: |
3805 | /* Not yet implemented. */ | |
3806 | break; | |
3807 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 3808 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
3809 | if (preferred_charset_id >= 0 |
3810 | && NILP (Fmemq (make_number (preferred_charset_id), | |
3811 | charset_list))) | |
3812 | preferred_charset_id = -1; | |
3813 | break; | |
3814 | default: | |
3815 | abort (); | |
4ed46869 | 3816 | } |
ff0dacd7 KH |
3817 | charbuf += -c - 1; |
3818 | continue; | |
4ed46869 | 3819 | } |
ec6d2bb8 | 3820 | |
b73bfc1c KH |
3821 | /* Now encode the character C. */ |
3822 | if (c < 0x20 || c == 0x7F) | |
3823 | { | |
df7492f9 KH |
3824 | if (c == '\n' |
3825 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 3826 | { |
df7492f9 KH |
3827 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
3828 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
3829 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 3830 | { |
df7492f9 KH |
3831 | int i; |
3832 | ||
3833 | for (i = 0; i < 4; i++) | |
3834 | CODING_ISO_DESIGNATION (coding, i) | |
3835 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 3836 | } |
df7492f9 KH |
3837 | bol_designation |
3838 | = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL; | |
19a8d9e0 | 3839 | } |
df7492f9 KH |
3840 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
3841 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
3842 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 3843 | } |
df7492f9 | 3844 | else if (ASCII_CHAR_P (c)) |
88993dfd | 3845 | { |
df7492f9 KH |
3846 | if (ascii_compatible) |
3847 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 3848 | else |
19a8d9e0 | 3849 | { |
bf16eb23 KH |
3850 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
3851 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 3852 | } |
4ed46869 | 3853 | } |
16eafb5d | 3854 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 3855 | { |
16eafb5d KH |
3856 | c = CHAR_TO_BYTE8 (c); |
3857 | EMIT_ONE_BYTE (c); | |
88993dfd | 3858 | } |
b73bfc1c | 3859 | else |
df7492f9 | 3860 | { |
ff0dacd7 | 3861 | struct charset *charset; |
b73bfc1c | 3862 | |
ff0dacd7 KH |
3863 | if (preferred_charset_id >= 0) |
3864 | { | |
3865 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
3866 | if (! CHAR_CHARSET_P (c, charset)) | |
3867 | charset = char_charset (c, charset_list, NULL); | |
3868 | } | |
3869 | else | |
3870 | charset = char_charset (c, charset_list, NULL); | |
df7492f9 KH |
3871 | if (!charset) |
3872 | { | |
41cbe562 KH |
3873 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
3874 | { | |
3875 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
3876 | charset = CHARSET_FROM_ID (charset_ascii); | |
3877 | } | |
3878 | else | |
3879 | { | |
3880 | c = coding->default_char; | |
3881 | charset = char_charset (c, charset_list, NULL); | |
3882 | } | |
df7492f9 KH |
3883 | } |
3884 | ENCODE_ISO_CHARACTER (charset, c); | |
3885 | } | |
84fbb8a0 | 3886 | } |
b73bfc1c | 3887 | |
df7492f9 KH |
3888 | if (coding->mode & CODING_MODE_LAST_BLOCK |
3889 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
3890 | { | |
3891 | ASSURE_DESTINATION (safe_room); | |
3892 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
3893 | } | |
065e3595 | 3894 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
3895 | CODING_ISO_BOL (coding) = bol_designation; |
3896 | coding->produced_char += produced_chars; | |
3897 | coding->produced = dst - coding->destination; | |
3898 | return 0; | |
4ed46869 KH |
3899 | } |
3900 | ||
3901 | \f | |
df7492f9 | 3902 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 3903 | |
df7492f9 | 3904 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
3905 | quite widely. So, for the moment, Emacs supports them in the bare |
3906 | C code. But, in the future, they may be supported only by CCL. */ | |
3907 | ||
3908 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
3909 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
3910 | as is. A character of charset katakana-jisx0201 is encoded by | |
3911 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
3912 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 3913 | so that it fit in the range below. |
4ed46869 KH |
3914 | |
3915 | --- CODE RANGE of SJIS --- | |
3916 | (character set) (range) | |
3917 | ASCII 0x00 .. 0x7F | |
df7492f9 | 3918 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 3919 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 3920 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
3921 | ------------------------------- |
3922 | ||
3923 | */ | |
3924 | ||
3925 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
3926 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 3927 | character set and is encoded in two-byte. |
4ed46869 KH |
3928 | |
3929 | --- CODE RANGE of BIG5 --- | |
3930 | (character set) (range) | |
3931 | ASCII 0x00 .. 0x7F | |
3932 | Big5 (1st byte) 0xA1 .. 0xFE | |
3933 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
3934 | -------------------------- | |
3935 | ||
df7492f9 | 3936 | */ |
4ed46869 KH |
3937 | |
3938 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
3939 | Check if a text is encoded in SJIS. If it is, return | |
df7492f9 | 3940 | CATEGORY_MASK_SJIS, else return 0. */ |
4ed46869 | 3941 | |
0a28aafb | 3942 | static int |
ff0dacd7 | 3943 | detect_coding_sjis (coding, detect_info) |
df7492f9 | 3944 | struct coding_system *coding; |
ff0dacd7 | 3945 | struct coding_detection_info *detect_info; |
4ed46869 | 3946 | { |
065e3595 | 3947 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 3948 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
3949 | int multibytep = coding->src_multibyte; |
3950 | int consumed_chars = 0; | |
3951 | int found = 0; | |
b73bfc1c | 3952 | int c; |
df7492f9 | 3953 | |
ff0dacd7 | 3954 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
3955 | /* A coding system of this category is always ASCII compatible. */ |
3956 | src += coding->head_ascii; | |
4ed46869 | 3957 | |
b73bfc1c | 3958 | while (1) |
4ed46869 | 3959 | { |
065e3595 | 3960 | src_base = src; |
df7492f9 | 3961 | ONE_MORE_BYTE (c); |
682169fe KH |
3962 | if (c < 0x80) |
3963 | continue; | |
df7492f9 | 3964 | if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF)) |
4ed46869 | 3965 | { |
df7492f9 | 3966 | ONE_MORE_BYTE (c); |
682169fe | 3967 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 3968 | break; |
ff0dacd7 | 3969 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 3970 | } |
df7492f9 | 3971 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 3972 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
3973 | else |
3974 | break; | |
4ed46869 | 3975 | } |
ff0dacd7 | 3976 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
3977 | return 0; |
3978 | ||
3979 | no_more_source: | |
065e3595 | 3980 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 3981 | { |
ff0dacd7 | 3982 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 3983 | return 0; |
4ed46869 | 3984 | } |
ff0dacd7 KH |
3985 | detect_info->found |= found; |
3986 | return 1; | |
4ed46869 KH |
3987 | } |
3988 | ||
3989 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
3990 | Check if a text is encoded in BIG5. If it is, return | |
df7492f9 | 3991 | CATEGORY_MASK_BIG5, else return 0. */ |
4ed46869 | 3992 | |
0a28aafb | 3993 | static int |
ff0dacd7 | 3994 | detect_coding_big5 (coding, detect_info) |
df7492f9 | 3995 | struct coding_system *coding; |
ff0dacd7 | 3996 | struct coding_detection_info *detect_info; |
4ed46869 | 3997 | { |
065e3595 | 3998 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 3999 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4000 | int multibytep = coding->src_multibyte; |
4001 | int consumed_chars = 0; | |
4002 | int found = 0; | |
b73bfc1c | 4003 | int c; |
fa42c37f | 4004 | |
ff0dacd7 | 4005 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4006 | /* A coding system of this category is always ASCII compatible. */ |
4007 | src += coding->head_ascii; | |
fa42c37f | 4008 | |
b73bfc1c | 4009 | while (1) |
fa42c37f | 4010 | { |
065e3595 | 4011 | src_base = src; |
df7492f9 KH |
4012 | ONE_MORE_BYTE (c); |
4013 | if (c < 0x80) | |
fa42c37f | 4014 | continue; |
df7492f9 | 4015 | if (c >= 0xA1) |
fa42c37f | 4016 | { |
df7492f9 KH |
4017 | ONE_MORE_BYTE (c); |
4018 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4019 | return 0; |
ff0dacd7 | 4020 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4021 | } |
df7492f9 KH |
4022 | else |
4023 | break; | |
fa42c37f | 4024 | } |
ff0dacd7 | 4025 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4026 | return 0; |
fa42c37f | 4027 | |
df7492f9 | 4028 | no_more_source: |
065e3595 | 4029 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4030 | { |
ff0dacd7 | 4031 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4032 | return 0; |
4033 | } | |
ff0dacd7 KH |
4034 | detect_info->found |= found; |
4035 | return 1; | |
fa42c37f KH |
4036 | } |
4037 | ||
4ed46869 KH |
4038 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". |
4039 | If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ | |
fa42c37f | 4040 | |
b73bfc1c | 4041 | static void |
df7492f9 | 4042 | decode_coding_sjis (coding) |
4ed46869 | 4043 | struct coding_system *coding; |
4ed46869 | 4044 | { |
8f924df7 KH |
4045 | const unsigned char *src = coding->source + coding->consumed; |
4046 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4047 | const unsigned char *src_base; | |
69a80ea3 KH |
4048 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4049 | int *charbuf_end | |
4050 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4051 | int consumed_chars = 0, consumed_chars_base; |
4052 | int multibytep = coding->src_multibyte; | |
4053 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4054 | struct charset *charset_kanji2; |
24a73b0a | 4055 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4056 | int char_offset = coding->produced_char; |
4057 | int last_offset = char_offset; | |
4058 | int last_id = charset_ascii; | |
a5d301df | 4059 | |
24a73b0a | 4060 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4061 | |
4062 | val = charset_list; | |
4063 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4064 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4065 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4066 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4067 | |
b73bfc1c | 4068 | while (1) |
4ed46869 | 4069 | { |
df7492f9 | 4070 | int c, c1; |
24a73b0a | 4071 | struct charset *charset; |
fa42c37f | 4072 | |
b73bfc1c | 4073 | src_base = src; |
df7492f9 | 4074 | consumed_chars_base = consumed_chars; |
fa42c37f | 4075 | |
df7492f9 KH |
4076 | if (charbuf >= charbuf_end) |
4077 | break; | |
4078 | ||
4079 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4080 | if (c < 0) |
4081 | goto invalid_code; | |
24a73b0a KH |
4082 | if (c < 0x80) |
4083 | charset = charset_roman; | |
57a47f8a | 4084 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4085 | goto invalid_code; |
57a47f8a KH |
4086 | else if (c >= 0xA1 && c <= 0xDF) |
4087 | { | |
4088 | /* SJIS -> JISX0201-Kana */ | |
4089 | c &= 0x7F; | |
4090 | charset = charset_kana; | |
4091 | } | |
4092 | else if (c <= 0xEF) | |
df7492f9 | 4093 | { |
57a47f8a KH |
4094 | /* SJIS -> JISX0208 */ |
4095 | ONE_MORE_BYTE (c1); | |
4096 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4097 | goto invalid_code; |
57a47f8a KH |
4098 | c = (c << 8) | c1; |
4099 | SJIS_TO_JIS (c); | |
4100 | charset = charset_kanji; | |
4101 | } | |
4102 | else if (c <= 0xFC && charset_kanji2) | |
4103 | { | |
c6876370 | 4104 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4105 | ONE_MORE_BYTE (c1); |
4106 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4107 | goto invalid_code; |
57a47f8a KH |
4108 | c = (c << 8) | c1; |
4109 | SJIS_TO_JIS2 (c); | |
4110 | charset = charset_kanji2; | |
df7492f9 | 4111 | } |
57a47f8a KH |
4112 | else |
4113 | goto invalid_code; | |
24a73b0a KH |
4114 | if (charset->id != charset_ascii |
4115 | && last_id != charset->id) | |
4116 | { | |
4117 | if (last_id != charset_ascii) | |
69a80ea3 | 4118 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4119 | last_id = charset->id; |
4120 | last_offset = char_offset; | |
4121 | } | |
4122 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4123 | *charbuf++ = c; |
ff0dacd7 | 4124 | char_offset++; |
df7492f9 | 4125 | continue; |
b73bfc1c | 4126 | |
df7492f9 KH |
4127 | invalid_code: |
4128 | src = src_base; | |
4129 | consumed_chars = consumed_chars_base; | |
4130 | ONE_MORE_BYTE (c); | |
065e3595 | 4131 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4132 | char_offset++; |
df7492f9 KH |
4133 | coding->errors++; |
4134 | } | |
fa42c37f | 4135 | |
df7492f9 | 4136 | no_more_source: |
ff0dacd7 | 4137 | if (last_id != charset_ascii) |
69a80ea3 | 4138 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4139 | coding->consumed_char += consumed_chars_base; |
4140 | coding->consumed = src_base - coding->source; | |
4141 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4142 | } |
4143 | ||
b73bfc1c | 4144 | static void |
df7492f9 | 4145 | decode_coding_big5 (coding) |
4ed46869 | 4146 | struct coding_system *coding; |
4ed46869 | 4147 | { |
8f924df7 KH |
4148 | const unsigned char *src = coding->source + coding->consumed; |
4149 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4150 | const unsigned char *src_base; | |
69a80ea3 KH |
4151 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4152 | int *charbuf_end | |
4153 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4154 | int consumed_chars = 0, consumed_chars_base; |
4155 | int multibytep = coding->src_multibyte; | |
4156 | struct charset *charset_roman, *charset_big5; | |
24a73b0a | 4157 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4158 | int char_offset = coding->produced_char; |
4159 | int last_offset = char_offset; | |
4160 | int last_id = charset_ascii; | |
df7492f9 | 4161 | |
24a73b0a | 4162 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4163 | val = charset_list; |
4164 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4165 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4166 | |
b73bfc1c | 4167 | while (1) |
4ed46869 | 4168 | { |
df7492f9 | 4169 | int c, c1; |
24a73b0a | 4170 | struct charset *charset; |
b73bfc1c KH |
4171 | |
4172 | src_base = src; | |
df7492f9 KH |
4173 | consumed_chars_base = consumed_chars; |
4174 | ||
4175 | if (charbuf >= charbuf_end) | |
4176 | break; | |
4177 | ||
4178 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4179 | |
065e3595 KH |
4180 | if (c < 0) |
4181 | goto invalid_code; | |
24a73b0a KH |
4182 | if (c < 0x80) |
4183 | charset = charset_roman; | |
4184 | else | |
4ed46869 | 4185 | { |
24a73b0a KH |
4186 | /* BIG5 -> Big5 */ |
4187 | if (c < 0xA1 || c > 0xFE) | |
4188 | goto invalid_code; | |
4189 | ONE_MORE_BYTE (c1); | |
4190 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4191 | goto invalid_code; | |
4192 | c = c << 8 | c1; | |
4193 | charset = charset_big5; | |
4ed46869 | 4194 | } |
24a73b0a KH |
4195 | if (charset->id != charset_ascii |
4196 | && last_id != charset->id) | |
df7492f9 | 4197 | { |
24a73b0a | 4198 | if (last_id != charset_ascii) |
69a80ea3 | 4199 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4200 | last_id = charset->id; |
4201 | last_offset = char_offset; | |
4ed46869 | 4202 | } |
24a73b0a | 4203 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4204 | *charbuf++ = c; |
ff0dacd7 | 4205 | char_offset++; |
fb88bf2d KH |
4206 | continue; |
4207 | ||
df7492f9 | 4208 | invalid_code: |
4ed46869 | 4209 | src = src_base; |
df7492f9 KH |
4210 | consumed_chars = consumed_chars_base; |
4211 | ONE_MORE_BYTE (c); | |
065e3595 | 4212 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4213 | char_offset++; |
df7492f9 | 4214 | coding->errors++; |
fb88bf2d | 4215 | } |
d46c5b12 | 4216 | |
df7492f9 | 4217 | no_more_source: |
ff0dacd7 | 4218 | if (last_id != charset_ascii) |
69a80ea3 | 4219 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4220 | coding->consumed_char += consumed_chars_base; |
4221 | coding->consumed = src_base - coding->source; | |
4222 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4223 | } |
4224 | ||
4225 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4226 | This function can encode charsets `ascii', `katakana-jisx0201', |
4227 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4228 | are sure that all these charsets are registered as official charset | |
4ed46869 KH |
4229 | (i.e. do not have extended leading-codes). Characters of other |
4230 | charsets are produced without any encoding. If SJIS_P is 1, encode | |
4231 | SJIS text, else encode BIG5 text. */ | |
4232 | ||
df7492f9 KH |
4233 | static int |
4234 | encode_coding_sjis (coding) | |
4ed46869 | 4235 | struct coding_system *coding; |
4ed46869 | 4236 | { |
df7492f9 KH |
4237 | int multibytep = coding->dst_multibyte; |
4238 | int *charbuf = coding->charbuf; | |
4239 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4240 | unsigned char *dst = coding->destination + coding->produced; | |
4241 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4242 | int safe_room = 4; | |
4243 | int produced_chars = 0; | |
24a73b0a | 4244 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4245 | int ascii_compatible; |
4246 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4247 | struct charset *charset_kanji2; |
df7492f9 | 4248 | int c; |
a5d301df | 4249 | |
24a73b0a | 4250 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4251 | val = charset_list; |
4252 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4253 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
57a47f8a KH |
4254 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4255 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4256 | |
df7492f9 | 4257 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4258 | |
df7492f9 KH |
4259 | while (charbuf < charbuf_end) |
4260 | { | |
4261 | ASSURE_DESTINATION (safe_room); | |
4262 | c = *charbuf++; | |
b73bfc1c | 4263 | /* Now encode the character C. */ |
df7492f9 KH |
4264 | if (ASCII_CHAR_P (c) && ascii_compatible) |
4265 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4266 | else if (CHAR_BYTE8_P (c)) |
4267 | { | |
4268 | c = CHAR_TO_BYTE8 (c); | |
4269 | EMIT_ONE_BYTE (c); | |
4270 | } | |
df7492f9 | 4271 | else |
b73bfc1c | 4272 | { |
df7492f9 KH |
4273 | unsigned code; |
4274 | struct charset *charset = char_charset (c, charset_list, &code); | |
4275 | ||
4276 | if (!charset) | |
4ed46869 | 4277 | { |
41cbe562 | 4278 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4279 | { |
41cbe562 KH |
4280 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4281 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4282 | } |
41cbe562 | 4283 | else |
b73bfc1c | 4284 | { |
41cbe562 KH |
4285 | c = coding->default_char; |
4286 | charset = char_charset (c, charset_list, &code); | |
b73bfc1c | 4287 | } |
b73bfc1c | 4288 | } |
df7492f9 KH |
4289 | if (code == CHARSET_INVALID_CODE (charset)) |
4290 | abort (); | |
4291 | if (charset == charset_kanji) | |
4292 | { | |
4293 | int c1, c2; | |
4294 | JIS_TO_SJIS (code); | |
4295 | c1 = code >> 8, c2 = code & 0xFF; | |
4296 | EMIT_TWO_BYTES (c1, c2); | |
4297 | } | |
4298 | else if (charset == charset_kana) | |
4299 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
4300 | else if (charset_kanji2 && charset == charset_kanji2) |
4301 | { | |
4302 | int c1, c2; | |
4303 | ||
4304 | c1 = code >> 8; | |
4305 | if (c1 == 0x21 || (c1 >= 0x23 && c1 < 0x25) | |
4306 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) | |
4307 | { | |
4308 | JIS_TO_SJIS2 (code); | |
4309 | c1 = code >> 8, c2 = code & 0xFF; | |
4310 | EMIT_TWO_BYTES (c1, c2); | |
4311 | } | |
4312 | else | |
4313 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4314 | } | |
df7492f9 KH |
4315 | else |
4316 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4317 | } | |
4318 | } | |
065e3595 | 4319 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4320 | coding->produced_char += produced_chars; |
4321 | coding->produced = dst - coding->destination; | |
4322 | return 0; | |
4323 | } | |
4324 | ||
4325 | static int | |
4326 | encode_coding_big5 (coding) | |
4327 | struct coding_system *coding; | |
4328 | { | |
4329 | int multibytep = coding->dst_multibyte; | |
4330 | int *charbuf = coding->charbuf; | |
4331 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4332 | unsigned char *dst = coding->destination + coding->produced; | |
4333 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4334 | int safe_room = 4; | |
4335 | int produced_chars = 0; | |
24a73b0a | 4336 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4337 | int ascii_compatible; |
4338 | struct charset *charset_roman, *charset_big5; | |
4339 | int c; | |
4340 | ||
24a73b0a | 4341 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4342 | val = charset_list; |
4343 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4344 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4345 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
4346 | ||
4347 | while (charbuf < charbuf_end) | |
4348 | { | |
4349 | ASSURE_DESTINATION (safe_room); | |
4350 | c = *charbuf++; | |
4351 | /* Now encode the character C. */ | |
4352 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
4353 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4354 | else if (CHAR_BYTE8_P (c)) |
4355 | { | |
4356 | c = CHAR_TO_BYTE8 (c); | |
4357 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
4358 | } |
4359 | else | |
4360 | { | |
df7492f9 KH |
4361 | unsigned code; |
4362 | struct charset *charset = char_charset (c, charset_list, &code); | |
4363 | ||
4364 | if (! charset) | |
b73bfc1c | 4365 | { |
41cbe562 | 4366 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4367 | { |
41cbe562 KH |
4368 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4369 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4370 | } |
41cbe562 | 4371 | else |
0eecad43 | 4372 | { |
41cbe562 KH |
4373 | c = coding->default_char; |
4374 | charset = char_charset (c, charset_list, &code); | |
0eecad43 | 4375 | } |
4ed46869 | 4376 | } |
df7492f9 KH |
4377 | if (code == CHARSET_INVALID_CODE (charset)) |
4378 | abort (); | |
4379 | if (charset == charset_big5) | |
b73bfc1c | 4380 | { |
df7492f9 KH |
4381 | int c1, c2; |
4382 | ||
4383 | c1 = code >> 8, c2 = code & 0xFF; | |
4384 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 4385 | } |
df7492f9 KH |
4386 | else |
4387 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 4388 | } |
4ed46869 | 4389 | } |
065e3595 | 4390 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4391 | coding->produced_char += produced_chars; |
4392 | coding->produced = dst - coding->destination; | |
4393 | return 0; | |
4ed46869 KH |
4394 | } |
4395 | ||
4396 | \f | |
df7492f9 | 4397 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
4398 | |
4399 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4400 | Check if a text is encoded in a coding system of which | |
4401 | encoder/decoder are written in CCL program. If it is, return | |
df7492f9 | 4402 | CATEGORY_MASK_CCL, else return 0. */ |
1397dc18 | 4403 | |
0a28aafb | 4404 | static int |
ff0dacd7 | 4405 | detect_coding_ccl (coding, detect_info) |
df7492f9 | 4406 | struct coding_system *coding; |
ff0dacd7 | 4407 | struct coding_detection_info *detect_info; |
1397dc18 | 4408 | { |
065e3595 | 4409 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4410 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4411 | int multibytep = coding->src_multibyte; |
4412 | int consumed_chars = 0; | |
4413 | int found = 0; | |
0e219d54 | 4414 | unsigned char *valids; |
df7492f9 KH |
4415 | int head_ascii = coding->head_ascii; |
4416 | Lisp_Object attrs; | |
4417 | ||
ff0dacd7 KH |
4418 | detect_info->checked |= CATEGORY_MASK_CCL; |
4419 | ||
df7492f9 | 4420 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 4421 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
4422 | attrs = CODING_ID_ATTRS (coding->id); |
4423 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
4424 | src += head_ascii; | |
1397dc18 | 4425 | |
b73bfc1c | 4426 | while (1) |
1397dc18 | 4427 | { |
df7492f9 | 4428 | int c; |
065e3595 KH |
4429 | |
4430 | src_base = src; | |
df7492f9 | 4431 | ONE_MORE_BYTE (c); |
065e3595 | 4432 | if (c < 0 || ! valids[c]) |
df7492f9 | 4433 | break; |
ff0dacd7 KH |
4434 | if ((valids[c] > 1)) |
4435 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 4436 | } |
ff0dacd7 | 4437 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
4438 | return 0; |
4439 | ||
4440 | no_more_source: | |
ff0dacd7 KH |
4441 | detect_info->found |= found; |
4442 | return 1; | |
df7492f9 KH |
4443 | } |
4444 | ||
4445 | static void | |
4446 | decode_coding_ccl (coding) | |
4447 | struct coding_system *coding; | |
4448 | { | |
7c78e542 | 4449 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 4450 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
4451 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4452 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
4453 | int consumed_chars = 0; |
4454 | int multibytep = coding->src_multibyte; | |
4455 | struct ccl_program ccl; | |
4456 | int source_charbuf[1024]; | |
4457 | int source_byteidx[1024]; | |
24a73b0a | 4458 | Lisp_Object attrs, charset_list; |
df7492f9 | 4459 | |
24a73b0a | 4460 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4461 | setup_ccl_program (&ccl, CODING_CCL_DECODER (coding)); |
4462 | ||
4463 | while (src < src_end) | |
4464 | { | |
7c78e542 | 4465 | const unsigned char *p = src; |
df7492f9 KH |
4466 | int *source, *source_end; |
4467 | int i = 0; | |
4468 | ||
4469 | if (multibytep) | |
4470 | while (i < 1024 && p < src_end) | |
4471 | { | |
4472 | source_byteidx[i] = p - src; | |
4473 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
4474 | } | |
4475 | else | |
4476 | while (i < 1024 && p < src_end) | |
4477 | source_charbuf[i++] = *p++; | |
8f924df7 | 4478 | |
df7492f9 KH |
4479 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
4480 | ccl.last_block = 1; | |
4481 | ||
4482 | source = source_charbuf; | |
4483 | source_end = source + i; | |
4484 | while (source < source_end) | |
4485 | { | |
4486 | ccl_driver (&ccl, source, charbuf, | |
8dcbea82 KH |
4487 | source_end - source, charbuf_end - charbuf, |
4488 | charset_list); | |
df7492f9 KH |
4489 | source += ccl.consumed; |
4490 | charbuf += ccl.produced; | |
4491 | if (ccl.status != CCL_STAT_SUSPEND_BY_DST) | |
4492 | break; | |
4493 | } | |
4494 | if (source < source_end) | |
4495 | src += source_byteidx[source - source_charbuf]; | |
4496 | else | |
4497 | src = p; | |
4498 | consumed_chars += source - source_charbuf; | |
4499 | ||
4500 | if (ccl.status != CCL_STAT_SUSPEND_BY_SRC | |
4501 | && ccl.status != CODING_RESULT_INSUFFICIENT_SRC) | |
4502 | break; | |
4503 | } | |
4504 | ||
4505 | switch (ccl.status) | |
4506 | { | |
4507 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 4508 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
4509 | break; |
4510 | case CCL_STAT_SUSPEND_BY_DST: | |
4511 | break; | |
4512 | case CCL_STAT_QUIT: | |
4513 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 4514 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
4515 | break; |
4516 | default: | |
065e3595 | 4517 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4518 | break; |
4519 | } | |
4520 | coding->consumed_char += consumed_chars; | |
4521 | coding->consumed = src - coding->source; | |
4522 | coding->charbuf_used = charbuf - coding->charbuf; | |
4523 | } | |
4524 | ||
4525 | static int | |
4526 | encode_coding_ccl (coding) | |
4527 | struct coding_system *coding; | |
4528 | { | |
4529 | struct ccl_program ccl; | |
4530 | int multibytep = coding->dst_multibyte; | |
4531 | int *charbuf = coding->charbuf; | |
4532 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4533 | unsigned char *dst = coding->destination + coding->produced; | |
4534 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4535 | unsigned char *adjusted_dst_end = dst_end - 1; | |
4536 | int destination_charbuf[1024]; | |
4537 | int i, produced_chars = 0; | |
24a73b0a | 4538 | Lisp_Object attrs, charset_list; |
df7492f9 | 4539 | |
24a73b0a | 4540 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4541 | setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding)); |
4542 | ||
4543 | ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK; | |
4544 | ccl.dst_multibyte = coding->dst_multibyte; | |
4545 | ||
4546 | while (charbuf < charbuf_end && dst < adjusted_dst_end) | |
4547 | { | |
4548 | int dst_bytes = dst_end - dst; | |
4549 | if (dst_bytes > 1024) | |
4550 | dst_bytes = 1024; | |
4551 | ||
4552 | ccl_driver (&ccl, charbuf, destination_charbuf, | |
8dcbea82 | 4553 | charbuf_end - charbuf, dst_bytes, charset_list); |
df7492f9 KH |
4554 | charbuf += ccl.consumed; |
4555 | if (multibytep) | |
4556 | for (i = 0; i < ccl.produced; i++) | |
4557 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); | |
4558 | else | |
4559 | { | |
4560 | for (i = 0; i < ccl.produced; i++) | |
4561 | *dst++ = destination_charbuf[i] & 0xFF; | |
4562 | produced_chars += ccl.produced; | |
4563 | } | |
4564 | } | |
4565 | ||
4566 | switch (ccl.status) | |
4567 | { | |
4568 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 4569 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
4570 | break; |
4571 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 4572 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
4573 | break; |
4574 | case CCL_STAT_QUIT: | |
4575 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 4576 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
4577 | break; |
4578 | default: | |
065e3595 | 4579 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 4580 | break; |
1397dc18 | 4581 | } |
df7492f9 KH |
4582 | |
4583 | coding->produced_char += produced_chars; | |
4584 | coding->produced = dst - coding->destination; | |
4585 | return 0; | |
1397dc18 KH |
4586 | } |
4587 | ||
df7492f9 | 4588 | |
1397dc18 | 4589 | \f |
df7492f9 | 4590 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 4591 | |
b73bfc1c | 4592 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 4593 | |
b73bfc1c | 4594 | static void |
df7492f9 | 4595 | decode_coding_raw_text (coding) |
4ed46869 | 4596 | struct coding_system *coding; |
4ed46869 | 4597 | { |
df7492f9 | 4598 | coding->chars_at_source = 1; |
2c78b7e1 KH |
4599 | coding->consumed_char = 0; |
4600 | coding->consumed = 0; | |
065e3595 | 4601 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 4602 | } |
4ed46869 | 4603 | |
df7492f9 KH |
4604 | static int |
4605 | encode_coding_raw_text (coding) | |
4606 | struct coding_system *coding; | |
4607 | { | |
4608 | int multibytep = coding->dst_multibyte; | |
4609 | int *charbuf = coding->charbuf; | |
4610 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
4611 | unsigned char *dst = coding->destination + coding->produced; | |
4612 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4613 | int produced_chars = 0; | |
b73bfc1c KH |
4614 | int c; |
4615 | ||
df7492f9 | 4616 | if (multibytep) |
b73bfc1c | 4617 | { |
df7492f9 | 4618 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 4619 | |
df7492f9 KH |
4620 | if (coding->src_multibyte) |
4621 | while (charbuf < charbuf_end) | |
4622 | { | |
4623 | ASSURE_DESTINATION (safe_room); | |
4624 | c = *charbuf++; | |
4625 | if (ASCII_CHAR_P (c)) | |
4626 | EMIT_ONE_ASCII_BYTE (c); | |
4627 | else if (CHAR_BYTE8_P (c)) | |
4628 | { | |
4629 | c = CHAR_TO_BYTE8 (c); | |
4630 | EMIT_ONE_BYTE (c); | |
4631 | } | |
4632 | else | |
4633 | { | |
4634 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 4635 | |
df7492f9 KH |
4636 | CHAR_STRING_ADVANCE (c, p1); |
4637 | while (p0 < p1) | |
9d123124 KH |
4638 | { |
4639 | EMIT_ONE_BYTE (*p0); | |
4640 | p0++; | |
4641 | } | |
df7492f9 KH |
4642 | } |
4643 | } | |
b73bfc1c | 4644 | else |
df7492f9 KH |
4645 | while (charbuf < charbuf_end) |
4646 | { | |
4647 | ASSURE_DESTINATION (safe_room); | |
4648 | c = *charbuf++; | |
4649 | EMIT_ONE_BYTE (c); | |
4650 | } | |
4651 | } | |
4652 | else | |
4ed46869 | 4653 | { |
df7492f9 | 4654 | if (coding->src_multibyte) |
d46c5b12 | 4655 | { |
df7492f9 KH |
4656 | int safe_room = MAX_MULTIBYTE_LENGTH; |
4657 | ||
4658 | while (charbuf < charbuf_end) | |
d46c5b12 | 4659 | { |
df7492f9 KH |
4660 | ASSURE_DESTINATION (safe_room); |
4661 | c = *charbuf++; | |
4662 | if (ASCII_CHAR_P (c)) | |
4663 | *dst++ = c; | |
4664 | else if (CHAR_BYTE8_P (c)) | |
4665 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 4666 | else |
df7492f9 KH |
4667 | CHAR_STRING_ADVANCE (c, dst); |
4668 | produced_chars++; | |
d46c5b12 KH |
4669 | } |
4670 | } | |
df7492f9 KH |
4671 | else |
4672 | { | |
4673 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
4674 | while (charbuf < charbuf_end && dst < dst_end) | |
4675 | *dst++ = *charbuf++; | |
4676 | produced_chars = dst - (coding->destination + coding->dst_bytes); | |
8f924df7 | 4677 | } |
4ed46869 | 4678 | } |
065e3595 | 4679 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4680 | coding->produced_char += produced_chars; |
4681 | coding->produced = dst - coding->destination; | |
4682 | return 0; | |
4ed46869 KH |
4683 | } |
4684 | ||
ff0dacd7 KH |
4685 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
4686 | Check if a text is encoded in a charset-based coding system. If it | |
4687 | is, return 1, else return 0. */ | |
4688 | ||
0a28aafb | 4689 | static int |
ff0dacd7 | 4690 | detect_coding_charset (coding, detect_info) |
df7492f9 | 4691 | struct coding_system *coding; |
ff0dacd7 | 4692 | struct coding_detection_info *detect_info; |
1397dc18 | 4693 | { |
065e3595 | 4694 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4695 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4696 | int multibytep = coding->src_multibyte; |
4697 | int consumed_chars = 0; | |
4698 | Lisp_Object attrs, valids; | |
584948ac | 4699 | int found = 0; |
1397dc18 | 4700 | |
ff0dacd7 KH |
4701 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
4702 | ||
df7492f9 KH |
4703 | coding = &coding_categories[coding_category_charset]; |
4704 | attrs = CODING_ID_ATTRS (coding->id); | |
4705 | valids = AREF (attrs, coding_attr_charset_valids); | |
4706 | ||
4707 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
4708 | src += coding->head_ascii; | |
1397dc18 | 4709 | |
b73bfc1c | 4710 | while (1) |
1397dc18 | 4711 | { |
df7492f9 | 4712 | int c; |
1397dc18 | 4713 | |
065e3595 | 4714 | src_base = src; |
df7492f9 | 4715 | ONE_MORE_BYTE (c); |
065e3595 KH |
4716 | if (c < 0) |
4717 | continue; | |
df7492f9 KH |
4718 | if (NILP (AREF (valids, c))) |
4719 | break; | |
584948ac | 4720 | if (c >= 0x80) |
ff0dacd7 | 4721 | found = CATEGORY_MASK_CHARSET; |
df7492f9 | 4722 | } |
ff0dacd7 | 4723 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 4724 | return 0; |
4ed46869 | 4725 | |
df7492f9 | 4726 | no_more_source: |
ff0dacd7 KH |
4727 | detect_info->found |= found; |
4728 | return 1; | |
df7492f9 | 4729 | } |
b73bfc1c | 4730 | |
b73bfc1c | 4731 | static void |
df7492f9 | 4732 | decode_coding_charset (coding) |
4ed46869 | 4733 | struct coding_system *coding; |
4ed46869 | 4734 | { |
8f924df7 KH |
4735 | const unsigned char *src = coding->source + coding->consumed; |
4736 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4737 | const unsigned char *src_base; | |
69a80ea3 KH |
4738 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4739 | int *charbuf_end | |
4740 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4741 | int consumed_chars = 0, consumed_chars_base; |
4742 | int multibytep = coding->src_multibyte; | |
24a73b0a | 4743 | Lisp_Object attrs, charset_list, valids; |
ff0dacd7 KH |
4744 | int char_offset = coding->produced_char; |
4745 | int last_offset = char_offset; | |
4746 | int last_id = charset_ascii; | |
df7492f9 | 4747 | |
24a73b0a | 4748 | CODING_GET_INFO (coding, attrs, charset_list); |
4eb6d3f1 | 4749 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 4750 | |
df7492f9 | 4751 | while (1) |
4ed46869 | 4752 | { |
4eb6d3f1 | 4753 | int c; |
24a73b0a KH |
4754 | Lisp_Object val; |
4755 | struct charset *charset; | |
4756 | int dim; | |
4757 | int len = 1; | |
4758 | unsigned code; | |
df7492f9 KH |
4759 | |
4760 | src_base = src; | |
4761 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 4762 | |
df7492f9 KH |
4763 | if (charbuf >= charbuf_end) |
4764 | break; | |
4765 | ||
4eb6d3f1 | 4766 | ONE_MORE_BYTE (c); |
065e3595 KH |
4767 | if (c < 0) |
4768 | goto invalid_code; | |
24a73b0a KH |
4769 | code = c; |
4770 | ||
4771 | val = AREF (valids, c); | |
4772 | if (NILP (val)) | |
4773 | goto invalid_code; | |
4774 | if (INTEGERP (val)) | |
d46c5b12 | 4775 | { |
24a73b0a KH |
4776 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
4777 | dim = CHARSET_DIMENSION (charset); | |
4778 | while (len < dim) | |
b73bfc1c | 4779 | { |
24a73b0a KH |
4780 | ONE_MORE_BYTE (c); |
4781 | code = (code << 8) | c; | |
4782 | len++; | |
b73bfc1c | 4783 | } |
24a73b0a KH |
4784 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
4785 | charset, code, c); | |
d46c5b12 | 4786 | } |
df7492f9 | 4787 | else |
d46c5b12 | 4788 | { |
24a73b0a KH |
4789 | /* VAL is a list of charset IDs. It is assured that the |
4790 | list is sorted by charset dimensions (smaller one | |
4791 | comes first). */ | |
4792 | while (CONSP (val)) | |
4eb6d3f1 | 4793 | { |
24a73b0a | 4794 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 4795 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 4796 | while (len < dim) |
4eb6d3f1 | 4797 | { |
acb2a965 KH |
4798 | ONE_MORE_BYTE (c); |
4799 | code = (code << 8) | c; | |
f9d71dcd | 4800 | len++; |
4eb6d3f1 | 4801 | } |
24a73b0a KH |
4802 | CODING_DECODE_CHAR (coding, src, src_base, |
4803 | src_end, charset, code, c); | |
4804 | if (c >= 0) | |
4805 | break; | |
4806 | val = XCDR (val); | |
ff0dacd7 | 4807 | } |
d46c5b12 | 4808 | } |
24a73b0a KH |
4809 | if (c < 0) |
4810 | goto invalid_code; | |
4811 | if (charset->id != charset_ascii | |
4812 | && last_id != charset->id) | |
4813 | { | |
4814 | if (last_id != charset_ascii) | |
69a80ea3 | 4815 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4816 | last_id = charset->id; |
4817 | last_offset = char_offset; | |
4818 | } | |
4819 | ||
df7492f9 | 4820 | *charbuf++ = c; |
ff0dacd7 | 4821 | char_offset++; |
df7492f9 KH |
4822 | continue; |
4823 | ||
4824 | invalid_code: | |
4825 | src = src_base; | |
4826 | consumed_chars = consumed_chars_base; | |
4827 | ONE_MORE_BYTE (c); | |
065e3595 | 4828 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4829 | char_offset++; |
df7492f9 | 4830 | coding->errors++; |
4ed46869 KH |
4831 | } |
4832 | ||
df7492f9 | 4833 | no_more_source: |
ff0dacd7 | 4834 | if (last_id != charset_ascii) |
69a80ea3 | 4835 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4836 | coding->consumed_char += consumed_chars_base; |
4837 | coding->consumed = src_base - coding->source; | |
4838 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4839 | } |
4840 | ||
df7492f9 KH |
4841 | static int |
4842 | encode_coding_charset (coding) | |
4ed46869 | 4843 | struct coding_system *coding; |
4ed46869 | 4844 | { |
df7492f9 KH |
4845 | int multibytep = coding->dst_multibyte; |
4846 | int *charbuf = coding->charbuf; | |
4847 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4848 | unsigned char *dst = coding->destination + coding->produced; | |
4849 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4850 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
4851 | int produced_chars = 0; | |
24a73b0a | 4852 | Lisp_Object attrs, charset_list; |
df7492f9 | 4853 | int ascii_compatible; |
b73bfc1c | 4854 | int c; |
b73bfc1c | 4855 | |
24a73b0a | 4856 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 4857 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 4858 | |
df7492f9 | 4859 | while (charbuf < charbuf_end) |
4ed46869 | 4860 | { |
4eb6d3f1 | 4861 | struct charset *charset; |
df7492f9 | 4862 | unsigned code; |
8f924df7 | 4863 | |
df7492f9 KH |
4864 | ASSURE_DESTINATION (safe_room); |
4865 | c = *charbuf++; | |
4866 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
4867 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 4868 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 4869 | { |
16eafb5d KH |
4870 | c = CHAR_TO_BYTE8 (c); |
4871 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 4872 | } |
d46c5b12 | 4873 | else |
b73bfc1c | 4874 | { |
4eb6d3f1 KH |
4875 | charset = char_charset (c, charset_list, &code); |
4876 | if (charset) | |
4877 | { | |
4878 | if (CHARSET_DIMENSION (charset) == 1) | |
4879 | EMIT_ONE_BYTE (code); | |
4880 | else if (CHARSET_DIMENSION (charset) == 2) | |
4881 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
4882 | else if (CHARSET_DIMENSION (charset) == 3) | |
4883 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
4884 | else | |
4885 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
4886 | (code >> 8) & 0xFF, code & 0xFF); | |
4887 | } | |
4888 | else | |
41cbe562 KH |
4889 | { |
4890 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
4891 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4892 | else | |
4893 | c = coding->default_char; | |
4894 | EMIT_ONE_BYTE (c); | |
4895 | } | |
4ed46869 | 4896 | } |
4ed46869 KH |
4897 | } |
4898 | ||
065e3595 | 4899 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4900 | coding->produced_char += produced_chars; |
4901 | coding->produced = dst - coding->destination; | |
4902 | return 0; | |
4ed46869 KH |
4903 | } |
4904 | ||
4905 | \f | |
1397dc18 | 4906 | /*** 7. C library functions ***/ |
4ed46869 | 4907 | |
df7492f9 KH |
4908 | /* Setup coding context CODING from information about CODING_SYSTEM. |
4909 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
4910 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 4911 | |
ec6d2bb8 | 4912 | void |
e0e989f6 KH |
4913 | setup_coding_system (coding_system, coding) |
4914 | Lisp_Object coding_system; | |
4ed46869 KH |
4915 | struct coding_system *coding; |
4916 | { | |
df7492f9 KH |
4917 | Lisp_Object attrs; |
4918 | Lisp_Object eol_type; | |
4919 | Lisp_Object coding_type; | |
4608c386 | 4920 | Lisp_Object val; |
4ed46869 | 4921 | |
df7492f9 | 4922 | if (NILP (coding_system)) |
ae6f73fa | 4923 | coding_system = Qundecided; |
c07c8e12 | 4924 | |
df7492f9 | 4925 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 4926 | |
df7492f9 KH |
4927 | attrs = CODING_ID_ATTRS (coding->id); |
4928 | eol_type = CODING_ID_EOL_TYPE (coding->id); | |
1f5dbf34 | 4929 | |
df7492f9 KH |
4930 | coding->mode = 0; |
4931 | coding->head_ascii = -1; | |
4932 | coding->common_flags | |
4933 | = (VECTORP (eol_type) ? CODING_REQUIRE_DETECTION_MASK : 0); | |
5e5c78be KH |
4934 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
4935 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
4936 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
4937 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
4938 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
4939 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 4940 | |
df7492f9 | 4941 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 KH |
4942 | coding->max_charset_id = SCHARS (val) - 1; |
4943 | coding->safe_charsets = (char *) SDATA (val); | |
df7492f9 | 4944 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
4608c386 | 4945 | |
df7492f9 KH |
4946 | coding_type = CODING_ATTR_TYPE (attrs); |
4947 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 4948 | { |
df7492f9 KH |
4949 | coding->detector = NULL; |
4950 | coding->decoder = decode_coding_raw_text; | |
4951 | coding->encoder = encode_coding_raw_text; | |
4952 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 4953 | } |
df7492f9 | 4954 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 4955 | { |
df7492f9 KH |
4956 | int i; |
4957 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
4958 | ||
4959 | /* Invoke graphic register 0 to plane 0. */ | |
4960 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
4961 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
4962 | CODING_ISO_INVOCATION (coding, 1) | |
4963 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
4964 | /* Setup the initial status of designation. */ | |
4965 | for (i = 0; i < 4; i++) | |
4966 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
4967 | /* Not single shifting initially. */ | |
4968 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
4969 | /* Beginning of buffer should also be regarded as bol. */ | |
4970 | CODING_ISO_BOL (coding) = 1; | |
4971 | coding->detector = detect_coding_iso_2022; | |
4972 | coding->decoder = decode_coding_iso_2022; | |
4973 | coding->encoder = encode_coding_iso_2022; | |
4974 | if (flags & CODING_ISO_FLAG_SAFE) | |
4975 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 4976 | coding->common_flags |
df7492f9 KH |
4977 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
4978 | | CODING_REQUIRE_FLUSHING_MASK); | |
4979 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
4980 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
4981 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
4982 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
4983 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
4984 | { | |
4985 | setup_iso_safe_charsets (attrs); | |
4986 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 KH |
4987 | coding->max_charset_id = SCHARS (val) - 1; |
4988 | coding->safe_charsets = (char *) SDATA (val); | |
df7492f9 KH |
4989 | } |
4990 | CODING_ISO_FLAGS (coding) = flags; | |
d46c5b12 | 4991 | } |
df7492f9 | 4992 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 4993 | { |
df7492f9 KH |
4994 | coding->detector = detect_coding_charset; |
4995 | coding->decoder = decode_coding_charset; | |
4996 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 4997 | coding->common_flags |
df7492f9 | 4998 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 4999 | } |
df7492f9 | 5000 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5001 | { |
df7492f9 KH |
5002 | coding->detector = detect_coding_utf_8; |
5003 | coding->decoder = decode_coding_utf_8; | |
5004 | coding->encoder = encode_coding_utf_8; | |
5005 | coding->common_flags | |
5006 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
5007 | } | |
5008 | else if (EQ (coding_type, Qutf_16)) | |
5009 | { | |
5010 | val = AREF (attrs, coding_attr_utf_16_bom); | |
5011 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom | |
5012 | : EQ (val, Qt) ? utf_16_with_bom | |
5013 | : utf_16_without_bom); | |
5014 | val = AREF (attrs, coding_attr_utf_16_endian); | |
b49a1807 | 5015 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5016 | : utf_16_little_endian); |
e19c3639 | 5017 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5018 | coding->detector = detect_coding_utf_16; |
5019 | coding->decoder = decode_coding_utf_16; | |
5020 | coding->encoder = encode_coding_utf_16; | |
5021 | coding->common_flags | |
5022 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
b49a1807 KH |
5023 | if (CODING_UTF_16_BOM (coding) == utf_16_detect_bom) |
5024 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5025 | } |
df7492f9 | 5026 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5027 | { |
df7492f9 KH |
5028 | coding->detector = detect_coding_ccl; |
5029 | coding->decoder = decode_coding_ccl; | |
5030 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5031 | coding->common_flags |
df7492f9 KH |
5032 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5033 | | CODING_REQUIRE_FLUSHING_MASK); | |
5034 | } | |
5035 | else if (EQ (coding_type, Qemacs_mule)) | |
5036 | { | |
5037 | coding->detector = detect_coding_emacs_mule; | |
5038 | coding->decoder = decode_coding_emacs_mule; | |
5039 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5040 | coding->common_flags |
df7492f9 KH |
5041 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5042 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) | |
5043 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5044 | { | |
5045 | Lisp_Object tail, safe_charsets; | |
5046 | int max_charset_id = 0; | |
5047 | ||
5048 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5049 | tail = XCDR (tail)) | |
5050 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5051 | max_charset_id = XFASTINT (XCAR (tail)); | |
5052 | safe_charsets = Fmake_string (make_number (max_charset_id + 1), | |
5053 | make_number (255)); | |
5054 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5055 | tail = XCDR (tail)) | |
8f924df7 | 5056 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5057 | coding->max_charset_id = max_charset_id; |
8f924df7 | 5058 | coding->safe_charsets = (char *) SDATA (safe_charsets); |
df7492f9 KH |
5059 | } |
5060 | } | |
5061 | else if (EQ (coding_type, Qshift_jis)) | |
5062 | { | |
5063 | coding->detector = detect_coding_sjis; | |
5064 | coding->decoder = decode_coding_sjis; | |
5065 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5066 | coding->common_flags |
df7492f9 KH |
5067 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5068 | } | |
5069 | else if (EQ (coding_type, Qbig5)) | |
5070 | { | |
5071 | coding->detector = detect_coding_big5; | |
5072 | coding->decoder = decode_coding_big5; | |
5073 | coding->encoder = encode_coding_big5; | |
c952af22 | 5074 | coding->common_flags |
df7492f9 KH |
5075 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5076 | } | |
5077 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5078 | { |
df7492f9 KH |
5079 | coding->detector = NULL; |
5080 | coding->decoder = decode_coding_raw_text; | |
5081 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5082 | if (! EQ (eol_type, Qunix)) |
5083 | { | |
5084 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5085 | if (! VECTORP (eol_type)) | |
5086 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5087 | } | |
5088 | ||
4ed46869 | 5089 | } |
4ed46869 | 5090 | |
df7492f9 | 5091 | return; |
4ed46869 KH |
5092 | } |
5093 | ||
0ff61e78 KH |
5094 | /* Return a list of charsets supported by CODING. */ |
5095 | ||
5096 | Lisp_Object | |
5097 | coding_charset_list (coding) | |
5098 | struct coding_system *coding; | |
5099 | { | |
35befdaa | 5100 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5101 | |
5102 | CODING_GET_INFO (coding, attrs, charset_list); | |
5103 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5104 | { | |
5105 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5106 | ||
5107 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5108 | charset_list = Viso_2022_charset_list; | |
5109 | } | |
5110 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5111 | { | |
5112 | charset_list = Vemacs_mule_charset_list; | |
5113 | } | |
5114 | return charset_list; | |
5115 | } | |
5116 | ||
5117 | ||
df7492f9 KH |
5118 | /* Return raw-text or one of its subsidiaries that has the same |
5119 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 5120 | |
df7492f9 KH |
5121 | Lisp_Object |
5122 | raw_text_coding_system (coding_system) | |
5123 | Lisp_Object coding_system; | |
ec6d2bb8 | 5124 | { |
0be8721c | 5125 | Lisp_Object spec, attrs; |
df7492f9 | 5126 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 5127 | |
d3e4cb56 KH |
5128 | if (NILP (coding_system)) |
5129 | return Qraw_text; | |
df7492f9 KH |
5130 | spec = CODING_SYSTEM_SPEC (coding_system); |
5131 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 5132 | |
df7492f9 KH |
5133 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
5134 | return coding_system; | |
ec6d2bb8 | 5135 | |
df7492f9 KH |
5136 | eol_type = AREF (spec, 2); |
5137 | if (VECTORP (eol_type)) | |
5138 | return Qraw_text; | |
5139 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
5140 | raw_text_eol_type = AREF (spec, 2); | |
5141 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
5142 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
5143 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
5144 | } |
5145 | ||
54f78171 | 5146 | |
df7492f9 KH |
5147 | /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT |
5148 | does, return one of the subsidiary that has the same eol-spec as | |
5149 | PARENT. Otherwise, return CODING_SYSTEM. */ | |
5150 | ||
5151 | Lisp_Object | |
5152 | coding_inherit_eol_type (coding_system, parent) | |
b74e4686 | 5153 | Lisp_Object coding_system, parent; |
54f78171 | 5154 | { |
3e139625 | 5155 | Lisp_Object spec, eol_type; |
54f78171 | 5156 | |
d3e4cb56 KH |
5157 | if (NILP (coding_system)) |
5158 | coding_system = Qraw_text; | |
df7492f9 | 5159 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 5160 | eol_type = AREF (spec, 2); |
d3e4cb56 KH |
5161 | if (VECTORP (eol_type) |
5162 | && ! NILP (parent)) | |
df7492f9 KH |
5163 | { |
5164 | Lisp_Object parent_spec; | |
df7492f9 KH |
5165 | Lisp_Object parent_eol_type; |
5166 | ||
5167 | parent_spec | |
5168 | = CODING_SYSTEM_SPEC (buffer_defaults.buffer_file_coding_system); | |
5169 | parent_eol_type = AREF (parent_spec, 2); | |
5170 | if (EQ (parent_eol_type, Qunix)) | |
5171 | coding_system = AREF (eol_type, 0); | |
5172 | else if (EQ (parent_eol_type, Qdos)) | |
5173 | coding_system = AREF (eol_type, 1); | |
5174 | else if (EQ (parent_eol_type, Qmac)) | |
5175 | coding_system = AREF (eol_type, 2); | |
54f78171 | 5176 | } |
df7492f9 | 5177 | return coding_system; |
54f78171 KH |
5178 | } |
5179 | ||
4ed46869 KH |
5180 | /* Emacs has a mechanism to automatically detect a coding system if it |
5181 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
5182 | it's impossible to distinguish some coding systems accurately | |
5183 | because they use the same range of codes. So, at first, coding | |
5184 | systems are categorized into 7, those are: | |
5185 | ||
0ef69138 | 5186 | o coding-category-emacs-mule |
4ed46869 KH |
5187 | |
5188 | The category for a coding system which has the same code range | |
5189 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 5190 | symbol) `emacs-mule' by default. |
4ed46869 KH |
5191 | |
5192 | o coding-category-sjis | |
5193 | ||
5194 | The category for a coding system which has the same code range | |
5195 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 5196 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
5197 | |
5198 | o coding-category-iso-7 | |
5199 | ||
5200 | The category for a coding system which has the same code range | |
7717c392 | 5201 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
5202 | shift and single shift functions. This can encode/decode all |
5203 | charsets. Assigned the coding-system (Lisp symbol) | |
5204 | `iso-2022-7bit' by default. | |
5205 | ||
5206 | o coding-category-iso-7-tight | |
5207 | ||
5208 | Same as coding-category-iso-7 except that this can | |
5209 | encode/decode only the specified charsets. | |
4ed46869 KH |
5210 | |
5211 | o coding-category-iso-8-1 | |
5212 | ||
5213 | The category for a coding system which has the same code range | |
5214 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
5215 | for DIMENSION1 charset. This doesn't use any locking shift |
5216 | and single shift functions. Assigned the coding-system (Lisp | |
5217 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
5218 | |
5219 | o coding-category-iso-8-2 | |
5220 | ||
5221 | The category for a coding system which has the same code range | |
5222 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
5223 | for DIMENSION2 charset. This doesn't use any locking shift |
5224 | and single shift functions. Assigned the coding-system (Lisp | |
5225 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 5226 | |
7717c392 | 5227 | o coding-category-iso-7-else |
4ed46869 KH |
5228 | |
5229 | The category for a coding system which has the same code range | |
df7492f9 | 5230 | as ISO2022 of 7-bit environemnt but uses locking shift or |
7717c392 KH |
5231 | single shift functions. Assigned the coding-system (Lisp |
5232 | symbol) `iso-2022-7bit-lock' by default. | |
5233 | ||
5234 | o coding-category-iso-8-else | |
5235 | ||
5236 | The category for a coding system which has the same code range | |
df7492f9 | 5237 | as ISO2022 of 8-bit environemnt but uses locking shift or |
7717c392 KH |
5238 | single shift functions. Assigned the coding-system (Lisp |
5239 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
5240 | |
5241 | o coding-category-big5 | |
5242 | ||
5243 | The category for a coding system which has the same code range | |
5244 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 5245 | `cn-big5' by default. |
4ed46869 | 5246 | |
fa42c37f KH |
5247 | o coding-category-utf-8 |
5248 | ||
5249 | The category for a coding system which has the same code range | |
6e76ae91 | 5250 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
5251 | symbol) `utf-8' by default. |
5252 | ||
5253 | o coding-category-utf-16-be | |
5254 | ||
5255 | The category for a coding system in which a text has an | |
5256 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
5257 | endian at the head. Assigned the coding-system (Lisp symbol) | |
5258 | `utf-16-be' by default. | |
5259 | ||
5260 | o coding-category-utf-16-le | |
5261 | ||
5262 | The category for a coding system in which a text has an | |
5263 | Unicode signature (cf. Unicode Standard) in the order of | |
5264 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
5265 | symbol) `utf-16-le' by default. | |
5266 | ||
1397dc18 KH |
5267 | o coding-category-ccl |
5268 | ||
5269 | The category for a coding system of which encoder/decoder is | |
5270 | written in CCL programs. The default value is nil, i.e., no | |
5271 | coding system is assigned. | |
5272 | ||
4ed46869 KH |
5273 | o coding-category-binary |
5274 | ||
5275 | The category for a coding system not categorized in any of the | |
5276 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 5277 | `no-conversion' by default. |
4ed46869 KH |
5278 | |
5279 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 5280 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
5281 | What Emacs does actually is to detect a category of coding system. |
5282 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 5283 | decide only one possible category, it selects a category of the |
4ed46869 KH |
5284 | highest priority. Priorities of categories are also specified by a |
5285 | user in a Lisp variable `coding-category-list'. | |
5286 | ||
5287 | */ | |
5288 | ||
df7492f9 KH |
5289 | #define EOL_SEEN_NONE 0 |
5290 | #define EOL_SEEN_LF 1 | |
5291 | #define EOL_SEEN_CR 2 | |
5292 | #define EOL_SEEN_CRLF 4 | |
66cfb530 | 5293 | |
ff0dacd7 KH |
5294 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
5295 | SOURCE is encoded. If CATEGORY is one of | |
5296 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
5297 | two-byte, else they are encoded by one-byte. | |
5298 | ||
5299 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 5300 | |
bc4bc72a | 5301 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
5302 | |
5303 | static int | |
89528eb3 | 5304 | detect_eol (source, src_bytes, category) |
f6cbaf43 | 5305 | const unsigned char *source; |
df7492f9 | 5306 | EMACS_INT src_bytes; |
89528eb3 | 5307 | enum coding_category category; |
4ed46869 | 5308 | { |
f6cbaf43 | 5309 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 5310 | unsigned char c; |
df7492f9 KH |
5311 | int total = 0; |
5312 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 5313 | |
89528eb3 | 5314 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 5315 | { |
df7492f9 | 5316 | int msb, lsb; |
fa42c37f | 5317 | |
89528eb3 KH |
5318 | msb = category == (coding_category_utf_16_le |
5319 | | coding_category_utf_16_le_nosig); | |
df7492f9 | 5320 | lsb = 1 - msb; |
fa42c37f | 5321 | |
df7492f9 | 5322 | while (src + 1 < src_end) |
fa42c37f | 5323 | { |
df7492f9 KH |
5324 | c = src[lsb]; |
5325 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 5326 | { |
df7492f9 KH |
5327 | int this_eol; |
5328 | ||
5329 | if (c == '\n') | |
5330 | this_eol = EOL_SEEN_LF; | |
5331 | else if (src + 3 >= src_end | |
5332 | || src[msb + 2] != 0 | |
5333 | || src[lsb + 2] != '\n') | |
5334 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 5335 | else |
8f924df7 | 5336 | this_eol = EOL_SEEN_CRLF; |
df7492f9 KH |
5337 | |
5338 | if (eol_seen == EOL_SEEN_NONE) | |
5339 | /* This is the first end-of-line. */ | |
5340 | eol_seen = this_eol; | |
5341 | else if (eol_seen != this_eol) | |
fa42c37f | 5342 | { |
df7492f9 KH |
5343 | /* The found type is different from what found before. */ |
5344 | eol_seen = EOL_SEEN_LF; | |
5345 | break; | |
fa42c37f | 5346 | } |
df7492f9 KH |
5347 | if (++total == MAX_EOL_CHECK_COUNT) |
5348 | break; | |
fa42c37f | 5349 | } |
df7492f9 | 5350 | src += 2; |
fa42c37f | 5351 | } |
bcf26d6a | 5352 | } |
d46c5b12 | 5353 | else |
c4825358 | 5354 | { |
df7492f9 | 5355 | while (src < src_end) |
27901516 | 5356 | { |
df7492f9 KH |
5357 | c = *src++; |
5358 | if (c == '\n' || c == '\r') | |
5359 | { | |
5360 | int this_eol; | |
d46c5b12 | 5361 | |
df7492f9 KH |
5362 | if (c == '\n') |
5363 | this_eol = EOL_SEEN_LF; | |
5364 | else if (src >= src_end || *src != '\n') | |
5365 | this_eol = EOL_SEEN_CR; | |
5366 | else | |
5367 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 5368 | |
df7492f9 KH |
5369 | if (eol_seen == EOL_SEEN_NONE) |
5370 | /* This is the first end-of-line. */ | |
5371 | eol_seen = this_eol; | |
5372 | else if (eol_seen != this_eol) | |
5373 | { | |
5374 | /* The found type is different from what found before. */ | |
5375 | eol_seen = EOL_SEEN_LF; | |
5376 | break; | |
5377 | } | |
5378 | if (++total == MAX_EOL_CHECK_COUNT) | |
5379 | break; | |
5380 | } | |
5381 | } | |
73be902c | 5382 | } |
df7492f9 | 5383 | return eol_seen; |
73be902c KH |
5384 | } |
5385 | ||
df7492f9 | 5386 | |
24a73b0a | 5387 | static Lisp_Object |
df7492f9 KH |
5388 | adjust_coding_eol_type (coding, eol_seen) |
5389 | struct coding_system *coding; | |
5390 | int eol_seen; | |
73be902c | 5391 | { |
0be8721c | 5392 | Lisp_Object eol_type; |
8f924df7 | 5393 | |
df7492f9 KH |
5394 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
5395 | if (eol_seen & EOL_SEEN_LF) | |
24a73b0a KH |
5396 | { |
5397 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
5398 | eol_type = Qunix; | |
5399 | } | |
6f197c07 | 5400 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
5401 | { |
5402 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
5403 | eol_type = Qdos; | |
5404 | } | |
6f197c07 | 5405 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
5406 | { |
5407 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
5408 | eol_type = Qmac; | |
5409 | } | |
5410 | return eol_type; | |
d46c5b12 | 5411 | } |
4ed46869 | 5412 | |
df7492f9 KH |
5413 | /* Detect how a text specified in CODING is encoded. If a coding |
5414 | system is detected, update fields of CODING by the detected coding | |
5415 | system. */ | |
0a28aafb | 5416 | |
df7492f9 KH |
5417 | void |
5418 | detect_coding (coding) | |
d46c5b12 | 5419 | struct coding_system *coding; |
d46c5b12 | 5420 | { |
8f924df7 | 5421 | const unsigned char *src, *src_end; |
d46c5b12 | 5422 | |
df7492f9 KH |
5423 | coding->consumed = coding->consumed_char = 0; |
5424 | coding->produced = coding->produced_char = 0; | |
5425 | coding_set_source (coding); | |
1c3478b0 | 5426 | |
df7492f9 | 5427 | src_end = coding->source + coding->src_bytes; |
1c3478b0 | 5428 | |
df7492f9 KH |
5429 | /* If we have not yet decided the text encoding type, detect it |
5430 | now. */ | |
5431 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 5432 | { |
df7492f9 | 5433 | int c, i; |
6cb21a4f | 5434 | struct coding_detection_info detect_info; |
df7492f9 | 5435 | |
6cb21a4f | 5436 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
24a73b0a | 5437 | for (i = 0, src = coding->source; src < src_end; i++, src++) |
d46c5b12 | 5438 | { |
df7492f9 | 5439 | c = *src; |
6cb21a4f | 5440 | if (c & 0x80) |
df7492f9 | 5441 | break; |
6cb21a4f KH |
5442 | if (c < 0x20 |
5443 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
5444 | && ! inhibit_iso_escape_detection | |
5445 | && ! detect_info.checked) | |
5446 | { | |
5447 | coding->head_ascii = src - (coding->source + coding->consumed); | |
5448 | if (detect_coding_iso_2022 (coding, &detect_info)) | |
5449 | { | |
5450 | /* We have scanned the whole data. */ | |
5451 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
5452 | /* We didn't find an 8-bit code. */ | |
5453 | src = src_end; | |
5454 | break; | |
5455 | } | |
5456 | } | |
d46c5b12 | 5457 | } |
df7492f9 KH |
5458 | coding->head_ascii = src - (coding->source + coding->consumed); |
5459 | ||
3aef54f3 | 5460 | if (coding->head_ascii < coding->src_bytes |
6cb21a4f | 5461 | || detect_info.found) |
d46c5b12 | 5462 | { |
ff0dacd7 KH |
5463 | enum coding_category category; |
5464 | struct coding_system *this; | |
df7492f9 | 5465 | |
6cb21a4f KH |
5466 | if (coding->head_ascii == coding->src_bytes) |
5467 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
5468 | for (i = 0; i < coding_category_raw_text; i++) | |
5469 | { | |
5470 | category = coding_priorities[i]; | |
5471 | this = coding_categories + category; | |
5472 | if (detect_info.found & (1 << category)) | |
24a73b0a | 5473 | break; |
6cb21a4f KH |
5474 | } |
5475 | else | |
5476 | for (i = 0; i < coding_category_raw_text; i++) | |
5477 | { | |
5478 | category = coding_priorities[i]; | |
5479 | this = coding_categories + category; | |
5480 | if (this->id < 0) | |
5481 | { | |
5482 | /* No coding system of this category is defined. */ | |
5483 | detect_info.rejected |= (1 << category); | |
5484 | } | |
5485 | else if (category >= coding_category_raw_text) | |
5486 | continue; | |
5487 | else if (detect_info.checked & (1 << category)) | |
5488 | { | |
5489 | if (detect_info.found & (1 << category)) | |
5490 | break; | |
5491 | } | |
5492 | else if ((*(this->detector)) (coding, &detect_info) | |
5493 | && detect_info.found & (1 << category)) | |
5494 | { | |
5495 | if (category == coding_category_utf_16_auto) | |
5496 | { | |
5497 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
5498 | category = coding_category_utf_16_le; | |
5499 | else | |
5500 | category = coding_category_utf_16_be; | |
5501 | } | |
5502 | break; | |
5503 | } | |
5504 | } | |
5505 | ||
ff0dacd7 KH |
5506 | if (i < coding_category_raw_text) |
5507 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
5508 | else if (detect_info.rejected == CATEGORY_MASK_ANY) | |
df7492f9 | 5509 | setup_coding_system (Qraw_text, coding); |
ff0dacd7 | 5510 | else if (detect_info.rejected) |
df7492f9 | 5511 | for (i = 0; i < coding_category_raw_text; i++) |
ff0dacd7 KH |
5512 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) |
5513 | { | |
5514 | this = coding_categories + coding_priorities[i]; | |
5515 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
5516 | break; | |
5517 | } | |
d46c5b12 | 5518 | } |
b73bfc1c | 5519 | } |
24a73b0a KH |
5520 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
5521 | == coding_category_utf_16_auto) | |
b49a1807 KH |
5522 | { |
5523 | Lisp_Object coding_systems; | |
5524 | struct coding_detection_info detect_info; | |
5525 | ||
5526 | coding_systems | |
5527 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_16_bom); | |
5528 | detect_info.found = detect_info.rejected = 0; | |
5529 | if (CONSP (coding_systems) | |
24a73b0a | 5530 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
5531 | { |
5532 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
5533 | setup_coding_system (XCAR (coding_systems), coding); | |
24a73b0a | 5534 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
b49a1807 KH |
5535 | setup_coding_system (XCDR (coding_systems), coding); |
5536 | } | |
5537 | } | |
4ed46869 | 5538 | } |
4ed46869 | 5539 | |
d46c5b12 | 5540 | |
aaaf0b1e | 5541 | static void |
df7492f9 | 5542 | decode_eol (coding) |
aaaf0b1e | 5543 | struct coding_system *coding; |
aaaf0b1e | 5544 | { |
24a73b0a KH |
5545 | Lisp_Object eol_type; |
5546 | unsigned char *p, *pbeg, *pend; | |
5547 | ||
5548 | eol_type = CODING_ID_EOL_TYPE (coding->id); | |
5549 | if (EQ (eol_type, Qunix)) | |
5550 | return; | |
5551 | ||
5552 | if (NILP (coding->dst_object)) | |
5553 | pbeg = coding->destination; | |
5554 | else | |
5555 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
5556 | pend = pbeg + coding->produced; | |
5557 | ||
5558 | if (VECTORP (eol_type)) | |
aaaf0b1e | 5559 | { |
df7492f9 | 5560 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 5561 | |
24a73b0a | 5562 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 5563 | { |
df7492f9 KH |
5564 | if (*p == '\n') |
5565 | eol_seen |= EOL_SEEN_LF; | |
5566 | else if (*p == '\r') | |
aaaf0b1e | 5567 | { |
df7492f9 | 5568 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 5569 | { |
df7492f9 KH |
5570 | eol_seen |= EOL_SEEN_CRLF; |
5571 | p++; | |
aaaf0b1e | 5572 | } |
aaaf0b1e | 5573 | else |
df7492f9 | 5574 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 5575 | } |
aaaf0b1e | 5576 | } |
24a73b0a KH |
5577 | if (eol_seen != EOL_SEEN_NONE |
5578 | && eol_seen != EOL_SEEN_LF | |
5579 | && eol_seen != EOL_SEEN_CRLF | |
5580 | && eol_seen != EOL_SEEN_CR) | |
5581 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 5582 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 5583 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 5584 | } |
d46c5b12 | 5585 | |
24a73b0a | 5586 | if (EQ (eol_type, Qmac)) |
27901516 | 5587 | { |
24a73b0a | 5588 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
5589 | if (*p == '\r') |
5590 | *p = '\n'; | |
4ed46869 | 5591 | } |
24a73b0a | 5592 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 5593 | { |
24a73b0a | 5594 | int n = 0; |
b73bfc1c | 5595 | |
24a73b0a KH |
5596 | if (NILP (coding->dst_object)) |
5597 | { | |
5598 | for (p = pend - 2; p >= pbeg; p--) | |
5599 | if (*p == '\r') | |
5600 | { | |
5601 | safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1); | |
5602 | n++; | |
5603 | } | |
5604 | } | |
5605 | else | |
5606 | { | |
5607 | for (p = pend - 2; p >= pbeg; p--) | |
5608 | if (*p == '\r') | |
5609 | { | |
5610 | int pos_byte = coding->dst_pos_byte + (p - pbeg); | |
5611 | int pos = BYTE_TO_CHAR (pos_byte); | |
5612 | ||
5613 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
5614 | n++; | |
5615 | } | |
5616 | } | |
5617 | coding->produced -= n; | |
5618 | coding->produced_char -= n; | |
aaaf0b1e | 5619 | } |
4ed46869 KH |
5620 | } |
5621 | ||
7d64c6ad | 5622 | |
a6f87d34 KH |
5623 | /* Return a translation table (or list of them) from coding system |
5624 | attribute vector ATTRS for encoding (ENCODEP is nonzero) or | |
5625 | decoding (ENCODEP is zero). */ | |
7d64c6ad | 5626 | |
e6a54062 | 5627 | static Lisp_Object |
09ee6fdd KH |
5628 | get_translation_table (attrs, encodep, max_lookup) |
5629 | Lisp_Object attrs; | |
5630 | int encodep, *max_lookup; | |
7d64c6ad KH |
5631 | { |
5632 | Lisp_Object standard, translation_table; | |
09ee6fdd | 5633 | Lisp_Object val; |
7d64c6ad KH |
5634 | |
5635 | if (encodep) | |
5636 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
5637 | standard = Vstandard_translation_table_for_encode; | |
5638 | else | |
5639 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
5640 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 5641 | if (NILP (translation_table)) |
09ee6fdd KH |
5642 | translation_table = standard; |
5643 | else | |
a6f87d34 | 5644 | { |
09ee6fdd KH |
5645 | if (SYMBOLP (translation_table)) |
5646 | translation_table = Fget (translation_table, Qtranslation_table); | |
5647 | else if (CONSP (translation_table)) | |
5648 | { | |
5649 | translation_table = Fcopy_sequence (translation_table); | |
5650 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
5651 | if (SYMBOLP (XCAR (val))) | |
5652 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
5653 | } | |
5654 | if (CHAR_TABLE_P (standard)) | |
5655 | { | |
5656 | if (CONSP (translation_table)) | |
5657 | translation_table = nconc2 (translation_table, | |
5658 | Fcons (standard, Qnil)); | |
5659 | else | |
5660 | translation_table = Fcons (translation_table, | |
5661 | Fcons (standard, Qnil)); | |
5662 | } | |
a6f87d34 | 5663 | } |
2170c8f0 KH |
5664 | |
5665 | if (max_lookup) | |
09ee6fdd | 5666 | { |
2170c8f0 KH |
5667 | *max_lookup = 1; |
5668 | if (CHAR_TABLE_P (translation_table) | |
5669 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
5670 | { | |
5671 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
5672 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
5673 | *max_lookup = XFASTINT (val); | |
5674 | } | |
5675 | else if (CONSP (translation_table)) | |
5676 | { | |
5677 | Lisp_Object tail, val; | |
09ee6fdd | 5678 | |
2170c8f0 KH |
5679 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
5680 | if (CHAR_TABLE_P (XCAR (tail)) | |
5681 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
5682 | { | |
5683 | val = XCHAR_TABLE (XCAR (tail))->extras[1]; | |
5684 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
5685 | *max_lookup = XFASTINT (val); | |
5686 | } | |
5687 | } | |
a6f87d34 | 5688 | } |
7d64c6ad KH |
5689 | return translation_table; |
5690 | } | |
5691 | ||
09ee6fdd KH |
5692 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
5693 | do { \ | |
5694 | trans = Qnil; \ | |
5695 | if (CHAR_TABLE_P (table)) \ | |
5696 | { \ | |
5697 | trans = CHAR_TABLE_REF (table, c); \ | |
5698 | if (CHARACTERP (trans)) \ | |
5699 | c = XFASTINT (trans), trans = Qnil; \ | |
5700 | } \ | |
5701 | else if (CONSP (table)) \ | |
5702 | { \ | |
5703 | Lisp_Object tail; \ | |
5704 | \ | |
5705 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
5706 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
5707 | { \ | |
5708 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
5709 | if (CHARACTERP (trans)) \ | |
5710 | c = XFASTINT (trans), trans = Qnil; \ | |
5711 | else if (! NILP (trans)) \ | |
5712 | break; \ | |
5713 | } \ | |
5714 | } \ | |
e6a54062 KH |
5715 | } while (0) |
5716 | ||
7d64c6ad | 5717 | |
69a80ea3 KH |
5718 | static Lisp_Object |
5719 | get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars) | |
5720 | Lisp_Object val; | |
5721 | int *buf, *buf_end; | |
5722 | int last_block; | |
5723 | int *from_nchars, *to_nchars; | |
5724 | { | |
433f7f87 KH |
5725 | /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or |
5726 | [TO-CHAR ...]. */ | |
69a80ea3 KH |
5727 | if (CONSP (val)) |
5728 | { | |
433f7f87 | 5729 | Lisp_Object from, tail; |
69a80ea3 KH |
5730 | int i, len; |
5731 | ||
433f7f87 | 5732 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
69a80ea3 | 5733 | { |
433f7f87 KH |
5734 | val = XCAR (tail); |
5735 | from = XCAR (val); | |
5736 | len = ASIZE (from); | |
5737 | for (i = 0; i < len; i++) | |
5738 | { | |
5739 | if (buf + i == buf_end) | |
5740 | { | |
5741 | if (! last_block) | |
5742 | return Qt; | |
5743 | break; | |
5744 | } | |
5745 | if (XINT (AREF (from, i)) != buf[i]) | |
5746 | break; | |
5747 | } | |
5748 | if (i == len) | |
5749 | { | |
5750 | val = XCDR (val); | |
5751 | *from_nchars = len; | |
5752 | break; | |
5753 | } | |
69a80ea3 | 5754 | } |
433f7f87 KH |
5755 | if (! CONSP (tail)) |
5756 | return Qnil; | |
69a80ea3 KH |
5757 | } |
5758 | if (VECTORP (val)) | |
5759 | *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val); | |
5760 | else | |
5761 | *buf = XINT (val); | |
5762 | return val; | |
5763 | } | |
5764 | ||
5765 | ||
d46c5b12 | 5766 | static int |
69a80ea3 | 5767 | produce_chars (coding, translation_table, last_block) |
df7492f9 | 5768 | struct coding_system *coding; |
69a80ea3 KH |
5769 | Lisp_Object translation_table; |
5770 | int last_block; | |
4ed46869 | 5771 | { |
df7492f9 KH |
5772 | unsigned char *dst = coding->destination + coding->produced; |
5773 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5774 | int produced; | |
5775 | int produced_chars = 0; | |
69a80ea3 | 5776 | int carryover = 0; |
4ed46869 | 5777 | |
df7492f9 | 5778 | if (! coding->chars_at_source) |
4ed46869 | 5779 | { |
df7492f9 | 5780 | /* Characters are in coding->charbuf. */ |
fba4576f AS |
5781 | int *buf = coding->charbuf; |
5782 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 5783 | |
df7492f9 KH |
5784 | if (BUFFERP (coding->src_object) |
5785 | && EQ (coding->src_object, coding->dst_object)) | |
8f924df7 | 5786 | dst_end = ((unsigned char *) coding->source) + coding->consumed; |
4ed46869 | 5787 | |
df7492f9 | 5788 | while (buf < buf_end) |
4ed46869 | 5789 | { |
69a80ea3 | 5790 | int c = *buf, i; |
bc4bc72a | 5791 | |
df7492f9 KH |
5792 | if (c >= 0) |
5793 | { | |
69a80ea3 KH |
5794 | int from_nchars = 1, to_nchars = 1; |
5795 | Lisp_Object trans = Qnil; | |
5796 | ||
09ee6fdd | 5797 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 5798 | if (! NILP (trans)) |
69a80ea3 KH |
5799 | { |
5800 | trans = get_translation (trans, buf, buf_end, last_block, | |
5801 | &from_nchars, &to_nchars); | |
5802 | if (EQ (trans, Qt)) | |
5803 | break; | |
5804 | c = *buf; | |
5805 | } | |
5806 | ||
5807 | if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end) | |
5808 | { | |
5809 | dst = alloc_destination (coding, | |
5810 | buf_end - buf | |
5811 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
5812 | dst); | |
5813 | dst_end = coding->destination + coding->dst_bytes; | |
5814 | } | |
5815 | ||
433f7f87 | 5816 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 5817 | { |
433f7f87 KH |
5818 | if (i > 0) |
5819 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
5820 | if (coding->dst_multibyte |
5821 | || ! CHAR_BYTE8_P (c)) | |
5822 | CHAR_STRING_ADVANCE (c, dst); | |
5823 | else | |
5824 | *dst++ = CHAR_TO_BYTE8 (c); | |
5825 | } | |
5826 | produced_chars += to_nchars; | |
5827 | *buf++ = to_nchars; | |
5828 | while (--from_nchars > 0) | |
5829 | *buf++ = 0; | |
d46c5b12 | 5830 | } |
df7492f9 | 5831 | else |
69a80ea3 KH |
5832 | /* This is an annotation datum. (-C) is the length. */ |
5833 | buf += -c; | |
4ed46869 | 5834 | } |
69a80ea3 | 5835 | carryover = buf_end - buf; |
4ed46869 | 5836 | } |
fa42c37f | 5837 | else |
fa42c37f | 5838 | { |
8f924df7 KH |
5839 | const unsigned char *src = coding->source; |
5840 | const unsigned char *src_end = src + coding->src_bytes; | |
df7492f9 | 5841 | Lisp_Object eol_type; |
fa42c37f | 5842 | |
df7492f9 | 5843 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
4ed46869 | 5844 | |
df7492f9 | 5845 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 5846 | { |
df7492f9 | 5847 | if (coding->src_multibyte) |
fa42c37f | 5848 | { |
71c81426 | 5849 | int multibytep = 1; |
df7492f9 | 5850 | int consumed_chars; |
d46c5b12 | 5851 | |
df7492f9 KH |
5852 | while (1) |
5853 | { | |
8f924df7 | 5854 | const unsigned char *src_base = src; |
df7492f9 | 5855 | int c; |
b73bfc1c | 5856 | |
df7492f9 KH |
5857 | ONE_MORE_BYTE (c); |
5858 | if (c == '\r') | |
5859 | { | |
5860 | if (EQ (eol_type, Qdos)) | |
5861 | { | |
98725083 KH |
5862 | if (src == src_end) |
5863 | { | |
065e3595 KH |
5864 | record_conversion_result |
5865 | (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
98725083 KH |
5866 | goto no_more_source; |
5867 | } | |
5868 | if (*src == '\n') | |
df7492f9 KH |
5869 | c = *src++; |
5870 | } | |
5871 | else if (EQ (eol_type, Qmac)) | |
5872 | c = '\n'; | |
5873 | } | |
5874 | if (dst == dst_end) | |
5875 | { | |
2c78b7e1 | 5876 | coding->consumed = src - coding->source; |
b73bfc1c | 5877 | |
2c78b7e1 | 5878 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 5879 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
5880 | if (dst == dst_end) |
5881 | { | |
5882 | dst = alloc_destination (coding, src_end - src + 1, | |
5883 | dst); | |
5884 | dst_end = coding->destination + coding->dst_bytes; | |
5885 | coding_set_source (coding); | |
5886 | src = coding->source + coding->consumed; | |
5887 | src_end = coding->source + coding->src_bytes; | |
5888 | } | |
df7492f9 KH |
5889 | } |
5890 | *dst++ = c; | |
5891 | produced_chars++; | |
5892 | } | |
5893 | no_more_source: | |
5894 | ; | |
fa42c37f KH |
5895 | } |
5896 | else | |
df7492f9 KH |
5897 | while (src < src_end) |
5898 | { | |
71c81426 | 5899 | int multibytep = 1; |
df7492f9 | 5900 | int c = *src++; |
b73bfc1c | 5901 | |
df7492f9 KH |
5902 | if (c == '\r') |
5903 | { | |
5904 | if (EQ (eol_type, Qdos)) | |
5905 | { | |
5906 | if (src < src_end | |
5907 | && *src == '\n') | |
5908 | c = *src++; | |
5909 | } | |
5910 | else if (EQ (eol_type, Qmac)) | |
5911 | c = '\n'; | |
5912 | } | |
5913 | if (dst >= dst_end - 1) | |
5914 | { | |
2c78b7e1 | 5915 | coding->consumed = src - coding->source; |
df7492f9 | 5916 | |
2c78b7e1 | 5917 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 5918 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
5919 | if (dst >= dst_end - 1) |
5920 | { | |
5921 | dst = alloc_destination (coding, src_end - src + 2, | |
5922 | dst); | |
5923 | dst_end = coding->destination + coding->dst_bytes; | |
5924 | coding_set_source (coding); | |
5925 | src = coding->source + coding->consumed; | |
5926 | src_end = coding->source + coding->src_bytes; | |
5927 | } | |
df7492f9 KH |
5928 | } |
5929 | EMIT_ONE_BYTE (c); | |
5930 | } | |
d46c5b12 | 5931 | } |
df7492f9 KH |
5932 | else |
5933 | { | |
5934 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 5935 | { |
df7492f9 | 5936 | int require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 5937 | |
df7492f9 | 5938 | if (require > 0) |
fa42c37f | 5939 | { |
df7492f9 KH |
5940 | EMACS_INT offset = src - coding->source; |
5941 | ||
5942 | dst = alloc_destination (coding, require, dst); | |
5943 | coding_set_source (coding); | |
5944 | src = coding->source + offset; | |
5945 | src_end = coding->source + coding->src_bytes; | |
fa42c37f KH |
5946 | } |
5947 | } | |
df7492f9 KH |
5948 | produced_chars = coding->src_chars; |
5949 | while (src < src_end) | |
fa42c37f | 5950 | { |
df7492f9 KH |
5951 | int c = *src++; |
5952 | ||
5953 | if (c == '\r') | |
5954 | { | |
5955 | if (EQ (eol_type, Qdos)) | |
5956 | { | |
5957 | if (src < src_end | |
5958 | && *src == '\n') | |
5959 | c = *src++; | |
5960 | produced_chars--; | |
5961 | } | |
5962 | else if (EQ (eol_type, Qmac)) | |
5963 | c = '\n'; | |
5964 | } | |
5965 | *dst++ = c; | |
fa42c37f KH |
5966 | } |
5967 | } | |
2c78b7e1 KH |
5968 | coding->consumed = coding->src_bytes; |
5969 | coding->consumed_char = coding->src_chars; | |
fa42c37f KH |
5970 | } |
5971 | ||
df7492f9 KH |
5972 | produced = dst - (coding->destination + coding->produced); |
5973 | if (BUFFERP (coding->dst_object)) | |
5974 | insert_from_gap (produced_chars, produced); | |
5975 | coding->produced += produced; | |
5976 | coding->produced_char += produced_chars; | |
69a80ea3 | 5977 | return carryover; |
fa42c37f KH |
5978 | } |
5979 | ||
ff0dacd7 KH |
5980 | /* Compose text in CODING->object according to the annotation data at |
5981 | CHARBUF. CHARBUF is an array: | |
5982 | [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ] | |
df7492f9 | 5983 | */ |
4ed46869 | 5984 | |
df7492f9 | 5985 | static INLINE void |
69a80ea3 | 5986 | produce_composition (coding, charbuf, pos) |
4ed46869 | 5987 | struct coding_system *coding; |
df7492f9 | 5988 | int *charbuf; |
69a80ea3 | 5989 | EMACS_INT pos; |
4ed46869 | 5990 | { |
df7492f9 | 5991 | int len; |
69a80ea3 | 5992 | EMACS_INT to; |
df7492f9 | 5993 | enum composition_method method; |
df7492f9 | 5994 | Lisp_Object components; |
fa42c37f | 5995 | |
df7492f9 | 5996 | len = -charbuf[0]; |
69a80ea3 | 5997 | to = pos + charbuf[2]; |
9ffd559c KH |
5998 | if (to <= pos) |
5999 | return; | |
69a80ea3 | 6000 | method = (enum composition_method) (charbuf[3]); |
d46c5b12 | 6001 | |
df7492f9 KH |
6002 | if (method == COMPOSITION_RELATIVE) |
6003 | components = Qnil; | |
9ffd559c KH |
6004 | else if (method >= COMPOSITION_WITH_RULE |
6005 | && method <= COMPOSITION_WITH_RULE_ALTCHARS) | |
d46c5b12 | 6006 | { |
df7492f9 KH |
6007 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
6008 | int i; | |
b73bfc1c | 6009 | |
69a80ea3 KH |
6010 | len -= 4; |
6011 | charbuf += 4; | |
df7492f9 | 6012 | for (i = 0; i < len; i++) |
9ffd559c KH |
6013 | { |
6014 | args[i] = make_number (charbuf[i]); | |
6015 | if (args[i] < 0) | |
6016 | return; | |
6017 | } | |
df7492f9 KH |
6018 | components = (method == COMPOSITION_WITH_ALTCHARS |
6019 | ? Fstring (len, args) : Fvector (len, args)); | |
d46c5b12 | 6020 | } |
9ffd559c KH |
6021 | else |
6022 | return; | |
69a80ea3 | 6023 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
6024 | } |
6025 | ||
d46c5b12 | 6026 | |
ff0dacd7 KH |
6027 | /* Put `charset' property on text in CODING->object according to |
6028 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 6029 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 6030 | */ |
d46c5b12 | 6031 | |
ff0dacd7 | 6032 | static INLINE void |
69a80ea3 | 6033 | produce_charset (coding, charbuf, pos) |
d46c5b12 | 6034 | struct coding_system *coding; |
ff0dacd7 | 6035 | int *charbuf; |
69a80ea3 | 6036 | EMACS_INT pos; |
d46c5b12 | 6037 | { |
69a80ea3 KH |
6038 | EMACS_INT from = pos - charbuf[2]; |
6039 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); | |
b73bfc1c | 6040 | |
69a80ea3 | 6041 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
6042 | Qcharset, CHARSET_NAME (charset), |
6043 | coding->dst_object); | |
d46c5b12 KH |
6044 | } |
6045 | ||
d46c5b12 | 6046 | |
df7492f9 KH |
6047 | #define CHARBUF_SIZE 0x4000 |
6048 | ||
6049 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
6050 | do { \ | |
6051 | int size = CHARBUF_SIZE;; \ | |
6052 | \ | |
6053 | coding->charbuf = NULL; \ | |
6054 | while (size > 1024) \ | |
6055 | { \ | |
6056 | coding->charbuf = (int *) alloca (sizeof (int) * size); \ | |
6057 | if (coding->charbuf) \ | |
6058 | break; \ | |
6059 | size >>= 1; \ | |
6060 | } \ | |
6061 | if (! coding->charbuf) \ | |
6062 | { \ | |
065e3595 | 6063 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \ |
df7492f9 KH |
6064 | return coding->result; \ |
6065 | } \ | |
6066 | coding->charbuf_size = size; \ | |
6067 | } while (0) | |
4ed46869 | 6068 | |
d46c5b12 KH |
6069 | |
6070 | static void | |
69a80ea3 | 6071 | produce_annotation (coding, pos) |
d46c5b12 | 6072 | struct coding_system *coding; |
69a80ea3 | 6073 | EMACS_INT pos; |
d46c5b12 | 6074 | { |
df7492f9 KH |
6075 | int *charbuf = coding->charbuf; |
6076 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 6077 | |
ff0dacd7 KH |
6078 | if (NILP (coding->dst_object)) |
6079 | return; | |
d46c5b12 | 6080 | |
df7492f9 | 6081 | while (charbuf < charbuf_end) |
a84f1519 | 6082 | { |
df7492f9 | 6083 | if (*charbuf >= 0) |
69a80ea3 | 6084 | pos += *charbuf++; |
d46c5b12 | 6085 | else |
d46c5b12 | 6086 | { |
df7492f9 | 6087 | int len = -*charbuf; |
ff0dacd7 | 6088 | switch (charbuf[1]) |
df7492f9 KH |
6089 | { |
6090 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
69a80ea3 | 6091 | produce_composition (coding, charbuf, pos); |
df7492f9 | 6092 | break; |
ff0dacd7 | 6093 | case CODING_ANNOTATE_CHARSET_MASK: |
69a80ea3 | 6094 | produce_charset (coding, charbuf, pos); |
ff0dacd7 | 6095 | break; |
df7492f9 KH |
6096 | default: |
6097 | abort (); | |
6098 | } | |
6099 | charbuf += len; | |
d46c5b12 | 6100 | } |
a84f1519 | 6101 | } |
d46c5b12 KH |
6102 | } |
6103 | ||
df7492f9 KH |
6104 | /* Decode the data at CODING->src_object into CODING->dst_object. |
6105 | CODING->src_object is a buffer, a string, or nil. | |
6106 | CODING->dst_object is a buffer. | |
d46c5b12 | 6107 | |
df7492f9 KH |
6108 | If CODING->src_object is a buffer, it must be the current buffer. |
6109 | In this case, if CODING->src_pos is positive, it is a position of | |
6110 | the source text in the buffer, otherwise, the source text is in the | |
6111 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
6112 | the text from GPT (which must be the same as PT). If this is the | |
6113 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6114 | negative. | |
d46c5b12 | 6115 | |
df7492f9 KH |
6116 | If CODING->src_object is a string, CODING->src_pos in an index to |
6117 | that string. | |
d46c5b12 | 6118 | |
df7492f9 KH |
6119 | If CODING->src_object is nil, CODING->source must already point to |
6120 | the non-relocatable memory area. In this case, CODING->src_pos is | |
6121 | an offset from CODING->source. | |
73be902c | 6122 | |
df7492f9 KH |
6123 | The decoded data is inserted at the current point of the buffer |
6124 | CODING->dst_object. | |
6125 | */ | |
d46c5b12 | 6126 | |
df7492f9 KH |
6127 | static int |
6128 | decode_coding (coding) | |
d46c5b12 | 6129 | struct coding_system *coding; |
d46c5b12 | 6130 | { |
df7492f9 | 6131 | Lisp_Object attrs; |
24a73b0a | 6132 | Lisp_Object undo_list; |
7d64c6ad | 6133 | Lisp_Object translation_table; |
69a80ea3 KH |
6134 | int carryover; |
6135 | int i; | |
d46c5b12 | 6136 | |
df7492f9 KH |
6137 | if (BUFFERP (coding->src_object) |
6138 | && coding->src_pos > 0 | |
6139 | && coding->src_pos < GPT | |
6140 | && coding->src_pos + coding->src_chars > GPT) | |
6141 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 6142 | |
24a73b0a | 6143 | undo_list = Qt; |
df7492f9 | 6144 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 6145 | { |
df7492f9 KH |
6146 | if (current_buffer != XBUFFER (coding->dst_object)) |
6147 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
6148 | if (GPT != PT) | |
6149 | move_gap_both (PT, PT_BYTE); | |
24a73b0a KH |
6150 | undo_list = current_buffer->undo_list; |
6151 | current_buffer->undo_list = Qt; | |
1c3478b0 KH |
6152 | } |
6153 | ||
df7492f9 KH |
6154 | coding->consumed = coding->consumed_char = 0; |
6155 | coding->produced = coding->produced_char = 0; | |
6156 | coding->chars_at_source = 0; | |
065e3595 | 6157 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 6158 | coding->errors = 0; |
1c3478b0 | 6159 | |
df7492f9 KH |
6160 | ALLOC_CONVERSION_WORK_AREA (coding); |
6161 | ||
6162 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 6163 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 6164 | |
69a80ea3 | 6165 | carryover = 0; |
df7492f9 | 6166 | do |
b73bfc1c | 6167 | { |
69a80ea3 KH |
6168 | EMACS_INT pos = coding->dst_pos + coding->produced_char; |
6169 | ||
df7492f9 KH |
6170 | coding_set_source (coding); |
6171 | coding->annotated = 0; | |
69a80ea3 | 6172 | coding->charbuf_used = carryover; |
df7492f9 | 6173 | (*(coding->decoder)) (coding); |
df7492f9 | 6174 | coding_set_destination (coding); |
69a80ea3 | 6175 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 6176 | if (coding->annotated) |
69a80ea3 KH |
6177 | produce_annotation (coding, pos); |
6178 | for (i = 0; i < carryover; i++) | |
6179 | coding->charbuf[i] | |
6180 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 6181 | } |
df7492f9 KH |
6182 | while (coding->consumed < coding->src_bytes |
6183 | && ! coding->result); | |
d46c5b12 | 6184 | |
69a80ea3 KH |
6185 | if (carryover > 0) |
6186 | { | |
6187 | coding_set_destination (coding); | |
6188 | coding->charbuf_used = carryover; | |
6189 | produce_chars (coding, translation_table, 1); | |
6190 | } | |
6191 | ||
df7492f9 KH |
6192 | coding->carryover_bytes = 0; |
6193 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 6194 | { |
df7492f9 | 6195 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 6196 | const unsigned char *src; |
df7492f9 KH |
6197 | |
6198 | coding_set_source (coding); | |
6199 | coding_set_destination (coding); | |
6200 | src = coding->source + coding->consumed; | |
6201 | ||
6202 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 6203 | { |
df7492f9 KH |
6204 | /* Flush out unprocessed data as binary chars. We are sure |
6205 | that the number of data is less than the size of | |
6206 | coding->charbuf. */ | |
065e3595 | 6207 | coding->charbuf_used = 0; |
df7492f9 | 6208 | while (nbytes-- > 0) |
1c3478b0 | 6209 | { |
df7492f9 | 6210 | int c = *src++; |
98725083 | 6211 | |
1c91457d KH |
6212 | if (c & 0x80) |
6213 | c = BYTE8_TO_CHAR (c); | |
6214 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 6215 | } |
f6cbaf43 | 6216 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 6217 | } |
d46c5b12 | 6218 | else |
df7492f9 KH |
6219 | { |
6220 | /* Record unprocessed bytes in coding->carryover. We are | |
6221 | sure that the number of data is less than the size of | |
6222 | coding->carryover. */ | |
6223 | unsigned char *p = coding->carryover; | |
6224 | ||
6225 | coding->carryover_bytes = nbytes; | |
6226 | while (nbytes-- > 0) | |
6227 | *p++ = *src++; | |
1c3478b0 | 6228 | } |
df7492f9 | 6229 | coding->consumed = coding->src_bytes; |
b73bfc1c | 6230 | } |
69f76525 | 6231 | |
24a73b0a KH |
6232 | if (BUFFERP (coding->dst_object)) |
6233 | { | |
6234 | current_buffer->undo_list = undo_list; | |
6235 | record_insert (coding->dst_pos, coding->produced_char); | |
6236 | } | |
6237 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix)) | |
6238 | decode_eol (coding); | |
73be902c | 6239 | return coding->result; |
4ed46869 KH |
6240 | } |
6241 | ||
aaaf0b1e | 6242 | |
e1c23804 | 6243 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
6244 | ending before LIMIT of CODING->src_object (buffer or string), store |
6245 | the data in BUF, set *STOP to a starting position of the next | |
6246 | composition (if any) or to LIMIT, and return the address of the | |
6247 | next element of BUF. | |
6248 | ||
6249 | If such an annotation is not found, set *STOP to a starting | |
6250 | position of a composition after POS (if any) or to LIMIT, and | |
6251 | return BUF. */ | |
6252 | ||
6253 | static INLINE int * | |
6254 | handle_composition_annotation (pos, limit, coding, buf, stop) | |
6255 | EMACS_INT pos, limit; | |
aaaf0b1e | 6256 | struct coding_system *coding; |
ff0dacd7 KH |
6257 | int *buf; |
6258 | EMACS_INT *stop; | |
aaaf0b1e | 6259 | { |
ff0dacd7 KH |
6260 | EMACS_INT start, end; |
6261 | Lisp_Object prop; | |
aaaf0b1e | 6262 | |
ff0dacd7 KH |
6263 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
6264 | || end > limit) | |
6265 | *stop = limit; | |
6266 | else if (start > pos) | |
6267 | *stop = start; | |
6268 | else | |
aaaf0b1e | 6269 | { |
ff0dacd7 | 6270 | if (start == pos) |
aaaf0b1e | 6271 | { |
ff0dacd7 KH |
6272 | /* We found a composition. Store the corresponding |
6273 | annotation data in BUF. */ | |
6274 | int *head = buf; | |
6275 | enum composition_method method = COMPOSITION_METHOD (prop); | |
6276 | int nchars = COMPOSITION_LENGTH (prop); | |
6277 | ||
69a80ea3 | 6278 | ADD_COMPOSITION_DATA (buf, nchars, method); |
ff0dacd7 | 6279 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 6280 | { |
ff0dacd7 KH |
6281 | Lisp_Object components; |
6282 | int len, i, i_byte; | |
6283 | ||
6284 | components = COMPOSITION_COMPONENTS (prop); | |
6285 | if (VECTORP (components)) | |
aaaf0b1e | 6286 | { |
ff0dacd7 KH |
6287 | len = XVECTOR (components)->size; |
6288 | for (i = 0; i < len; i++) | |
6289 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 6290 | } |
ff0dacd7 | 6291 | else if (STRINGP (components)) |
aaaf0b1e | 6292 | { |
8f924df7 | 6293 | len = SCHARS (components); |
ff0dacd7 KH |
6294 | i = i_byte = 0; |
6295 | while (i < len) | |
6296 | { | |
6297 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
6298 | buf++; | |
6299 | } | |
6300 | } | |
6301 | else if (INTEGERP (components)) | |
6302 | { | |
6303 | len = 1; | |
6304 | *buf++ = XINT (components); | |
6305 | } | |
6306 | else if (CONSP (components)) | |
6307 | { | |
6308 | for (len = 0; CONSP (components); | |
6309 | len++, components = XCDR (components)) | |
6310 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 6311 | } |
aaaf0b1e | 6312 | else |
ff0dacd7 KH |
6313 | abort (); |
6314 | *head -= len; | |
aaaf0b1e | 6315 | } |
aaaf0b1e | 6316 | } |
ff0dacd7 KH |
6317 | |
6318 | if (find_composition (end, limit, &start, &end, &prop, | |
6319 | coding->src_object) | |
6320 | && end <= limit) | |
6321 | *stop = start; | |
6322 | else | |
6323 | *stop = limit; | |
aaaf0b1e | 6324 | } |
ff0dacd7 KH |
6325 | return buf; |
6326 | } | |
6327 | ||
6328 | ||
e1c23804 | 6329 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
6330 | CODING->src_object (buffer of string), store the data in BUF, set |
6331 | *STOP to the position where the value of `charset' property changes | |
6332 | (limiting by LIMIT), and return the address of the next element of | |
6333 | BUF. | |
6334 | ||
6335 | If the property value is nil, set *STOP to the position where the | |
6336 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
6337 | ||
6338 | static INLINE int * | |
6339 | handle_charset_annotation (pos, limit, coding, buf, stop) | |
6340 | EMACS_INT pos, limit; | |
6341 | struct coding_system *coding; | |
6342 | int *buf; | |
6343 | EMACS_INT *stop; | |
6344 | { | |
6345 | Lisp_Object val, next; | |
6346 | int id; | |
6347 | ||
6348 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
6349 | if (! NILP (val) && CHARSETP (val)) | |
6350 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
6351 | else | |
6352 | id = -1; | |
69a80ea3 | 6353 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
6354 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
6355 | coding->src_object, | |
6356 | make_number (limit)); | |
6357 | *stop = XINT (next); | |
6358 | return buf; | |
6359 | } | |
6360 | ||
6361 | ||
df7492f9 | 6362 | static void |
09ee6fdd | 6363 | consume_chars (coding, translation_table, max_lookup) |
df7492f9 | 6364 | struct coding_system *coding; |
433f7f87 | 6365 | Lisp_Object translation_table; |
09ee6fdd | 6366 | int max_lookup; |
df7492f9 KH |
6367 | { |
6368 | int *buf = coding->charbuf; | |
ff0dacd7 | 6369 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 6370 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 6371 | const unsigned char *src_end = coding->source + coding->src_bytes; |
ff0dacd7 KH |
6372 | EMACS_INT pos = coding->src_pos + coding->consumed_char; |
6373 | EMACS_INT end_pos = coding->src_pos + coding->src_chars; | |
df7492f9 KH |
6374 | int multibytep = coding->src_multibyte; |
6375 | Lisp_Object eol_type; | |
6376 | int c; | |
ff0dacd7 | 6377 | EMACS_INT stop, stop_composition, stop_charset; |
09ee6fdd | 6378 | int *lookup_buf = NULL; |
433f7f87 KH |
6379 | |
6380 | if (! NILP (translation_table)) | |
09ee6fdd | 6381 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 6382 | |
df7492f9 KH |
6383 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
6384 | if (VECTORP (eol_type)) | |
6385 | eol_type = Qunix; | |
88993dfd | 6386 | |
df7492f9 KH |
6387 | /* Note: composition handling is not yet implemented. */ |
6388 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 6389 | |
0b5670c9 KH |
6390 | if (NILP (coding->src_object)) |
6391 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 6392 | else |
0b5670c9 KH |
6393 | { |
6394 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
6395 | stop = stop_composition = pos; | |
6396 | else | |
6397 | stop = stop_composition = end_pos; | |
6398 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
6399 | stop = stop_charset = pos; | |
6400 | else | |
6401 | stop_charset = end_pos; | |
6402 | } | |
ec6d2bb8 | 6403 | |
24a73b0a | 6404 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 6405 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 6406 | while (buf < buf_end) |
aaaf0b1e | 6407 | { |
433f7f87 KH |
6408 | Lisp_Object trans; |
6409 | ||
df7492f9 | 6410 | if (pos == stop) |
ec6d2bb8 | 6411 | { |
df7492f9 KH |
6412 | if (pos == end_pos) |
6413 | break; | |
ff0dacd7 KH |
6414 | if (pos == stop_composition) |
6415 | buf = handle_composition_annotation (pos, end_pos, coding, | |
6416 | buf, &stop_composition); | |
6417 | if (pos == stop_charset) | |
6418 | buf = handle_charset_annotation (pos, end_pos, coding, | |
6419 | buf, &stop_charset); | |
6420 | stop = (stop_composition < stop_charset | |
6421 | ? stop_composition : stop_charset); | |
df7492f9 KH |
6422 | } |
6423 | ||
6424 | if (! multibytep) | |
4776e638 | 6425 | { |
d3e4cb56 | 6426 | EMACS_INT bytes; |
aaaf0b1e | 6427 | |
ea29edf2 KH |
6428 | if (coding->encoder == encode_coding_raw_text) |
6429 | c = *src++, pos++; | |
6430 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
4776e638 KH |
6431 | c = STRING_CHAR_ADVANCE (src), pos += bytes; |
6432 | else | |
f03caae0 | 6433 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 6434 | } |
df7492f9 | 6435 | else |
4776e638 | 6436 | c = STRING_CHAR_ADVANCE (src), pos++; |
df7492f9 KH |
6437 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
6438 | c = '\n'; | |
6439 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 6440 | { |
df7492f9 | 6441 | if (c == '\n') |
aaaf0b1e | 6442 | { |
df7492f9 KH |
6443 | if (EQ (eol_type, Qdos)) |
6444 | *buf++ = '\r'; | |
6445 | else | |
6446 | c = '\r'; | |
aaaf0b1e KH |
6447 | } |
6448 | } | |
433f7f87 | 6449 | |
e6a54062 | 6450 | trans = Qnil; |
09ee6fdd | 6451 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 6452 | if (NILP (trans)) |
433f7f87 KH |
6453 | *buf++ = c; |
6454 | else | |
6455 | { | |
6456 | int from_nchars = 1, to_nchars = 1; | |
6457 | int *lookup_buf_end; | |
6458 | const unsigned char *p = src; | |
6459 | int i; | |
6460 | ||
6461 | lookup_buf[0] = c; | |
6462 | for (i = 1; i < max_lookup && p < src_end; i++) | |
6463 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
6464 | lookup_buf_end = lookup_buf + i; | |
6465 | trans = get_translation (trans, lookup_buf, lookup_buf_end, 1, | |
6466 | &from_nchars, &to_nchars); | |
6467 | if (EQ (trans, Qt) | |
6468 | || buf + to_nchars > buf_end) | |
6469 | break; | |
6470 | *buf++ = *lookup_buf; | |
6471 | for (i = 1; i < to_nchars; i++) | |
6472 | *buf++ = XINT (AREF (trans, i)); | |
6473 | for (i = 1; i < from_nchars; i++, pos++) | |
6474 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
6475 | } | |
aaaf0b1e | 6476 | } |
ec6d2bb8 | 6477 | |
df7492f9 KH |
6478 | coding->consumed = src - coding->source; |
6479 | coding->consumed_char = pos - coding->src_pos; | |
6480 | coding->charbuf_used = buf - coding->charbuf; | |
6481 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
6482 | } |
6483 | ||
4ed46869 | 6484 | |
df7492f9 KH |
6485 | /* Encode the text at CODING->src_object into CODING->dst_object. |
6486 | CODING->src_object is a buffer or a string. | |
6487 | CODING->dst_object is a buffer or nil. | |
6488 | ||
6489 | If CODING->src_object is a buffer, it must be the current buffer. | |
6490 | In this case, if CODING->src_pos is positive, it is a position of | |
6491 | the source text in the buffer, otherwise. the source text is in the | |
6492 | gap area of the buffer, and coding->src_pos specifies the offset of | |
6493 | the text from GPT (which must be the same as PT). If this is the | |
6494 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6495 | negative and CODING should not have `pre-write-conversion'. | |
6496 | ||
6497 | If CODING->src_object is a string, CODING should not have | |
6498 | `pre-write-conversion'. | |
6499 | ||
6500 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
6501 | the current point of that buffer. | |
6502 | ||
6503 | If CODING->dst_object is nil, the encoded data is placed at the | |
6504 | memory area specified by CODING->destination. */ | |
6505 | ||
6506 | static int | |
6507 | encode_coding (coding) | |
4ed46869 | 6508 | struct coding_system *coding; |
4ed46869 | 6509 | { |
df7492f9 | 6510 | Lisp_Object attrs; |
7d64c6ad | 6511 | Lisp_Object translation_table; |
09ee6fdd | 6512 | int max_lookup; |
9861e777 | 6513 | |
df7492f9 | 6514 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
6515 | if (coding->encoder == encode_coding_raw_text) |
6516 | translation_table = Qnil, max_lookup = 0; | |
6517 | else | |
6518 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 6519 | |
df7492f9 | 6520 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 6521 | { |
df7492f9 KH |
6522 | set_buffer_internal (XBUFFER (coding->dst_object)); |
6523 | coding->dst_multibyte | |
6524 | = ! NILP (current_buffer->enable_multibyte_characters); | |
8844fa83 | 6525 | } |
4ed46869 | 6526 | |
b73bfc1c | 6527 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 6528 | coding->produced = coding->produced_char = 0; |
065e3595 | 6529 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 6530 | coding->errors = 0; |
b73bfc1c | 6531 | |
df7492f9 | 6532 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 6533 | |
df7492f9 KH |
6534 | do { |
6535 | coding_set_source (coding); | |
09ee6fdd | 6536 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
6537 | coding_set_destination (coding); |
6538 | (*(coding->encoder)) (coding); | |
6539 | } while (coding->consumed_char < coding->src_chars); | |
6540 | ||
6541 | if (BUFFERP (coding->dst_object)) | |
6542 | insert_from_gap (coding->produced_char, coding->produced); | |
6543 | ||
6544 | return (coding->result); | |
ec6d2bb8 KH |
6545 | } |
6546 | ||
fb88bf2d | 6547 | |
24a73b0a KH |
6548 | /* Name (or base name) of work buffer for code conversion. */ |
6549 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 6550 | |
24a73b0a KH |
6551 | /* A working buffer used by the top level conversion. Once it is |
6552 | created, it is never destroyed. It has the name | |
6553 | Vcode_conversion_workbuf_name. The other working buffers are | |
6554 | destroyed after the use is finished, and their names are modified | |
6555 | versions of Vcode_conversion_workbuf_name. */ | |
6556 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 6557 | |
24a73b0a KH |
6558 | /* 1 iff Vcode_conversion_reused_workbuf is already in use. */ |
6559 | static int reused_workbuf_in_use; | |
4ed46869 | 6560 | |
24a73b0a KH |
6561 | |
6562 | /* Return a working buffer of code convesion. MULTIBYTE specifies the | |
6563 | multibyteness of returning buffer. */ | |
b73bfc1c | 6564 | |
f6cbaf43 | 6565 | static Lisp_Object |
24a73b0a | 6566 | make_conversion_work_buffer (multibyte) |
f6cbaf43 | 6567 | int multibyte; |
df7492f9 | 6568 | { |
24a73b0a KH |
6569 | Lisp_Object name, workbuf; |
6570 | struct buffer *current; | |
4ed46869 | 6571 | |
24a73b0a | 6572 | if (reused_workbuf_in_use++) |
065e3595 KH |
6573 | { |
6574 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
6575 | workbuf = Fget_buffer_create (name); | |
6576 | } | |
df7492f9 | 6577 | else |
065e3595 KH |
6578 | { |
6579 | name = Vcode_conversion_workbuf_name; | |
6580 | workbuf = Fget_buffer_create (name); | |
6581 | if (NILP (Vcode_conversion_reused_workbuf)) | |
6582 | Vcode_conversion_reused_workbuf = workbuf; | |
6583 | } | |
24a73b0a KH |
6584 | current = current_buffer; |
6585 | set_buffer_internal (XBUFFER (workbuf)); | |
6586 | Ferase_buffer (); | |
df7492f9 | 6587 | current_buffer->undo_list = Qt; |
24a73b0a | 6588 | current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil; |
df7492f9 | 6589 | set_buffer_internal (current); |
24a73b0a | 6590 | return workbuf; |
df7492f9 | 6591 | } |
d46c5b12 | 6592 | |
24a73b0a | 6593 | |
4776e638 | 6594 | static Lisp_Object |
24a73b0a KH |
6595 | code_conversion_restore (arg) |
6596 | Lisp_Object arg; | |
4776e638 | 6597 | { |
24a73b0a | 6598 | Lisp_Object current, workbuf; |
948bdcf3 | 6599 | struct gcpro gcpro1; |
24a73b0a | 6600 | |
948bdcf3 | 6601 | GCPRO1 (arg); |
24a73b0a KH |
6602 | current = XCAR (arg); |
6603 | workbuf = XCDR (arg); | |
6604 | if (! NILP (workbuf)) | |
6605 | { | |
6606 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
6607 | reused_workbuf_in_use = 0; | |
6608 | else if (! NILP (Fbuffer_live_p (workbuf))) | |
6609 | Fkill_buffer (workbuf); | |
6610 | } | |
6611 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 6612 | UNGCPRO; |
4776e638 KH |
6613 | return Qnil; |
6614 | } | |
b73bfc1c | 6615 | |
24a73b0a KH |
6616 | Lisp_Object |
6617 | code_conversion_save (with_work_buf, multibyte) | |
4776e638 | 6618 | int with_work_buf, multibyte; |
df7492f9 | 6619 | { |
24a73b0a | 6620 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 6621 | |
4776e638 | 6622 | if (with_work_buf) |
24a73b0a KH |
6623 | workbuf = make_conversion_work_buffer (multibyte); |
6624 | record_unwind_protect (code_conversion_restore, | |
6625 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 6626 | return workbuf; |
df7492f9 | 6627 | } |
d46c5b12 | 6628 | |
df7492f9 KH |
6629 | int |
6630 | decode_coding_gap (coding, chars, bytes) | |
6631 | struct coding_system *coding; | |
6632 | EMACS_INT chars, bytes; | |
6633 | { | |
6634 | int count = specpdl_ptr - specpdl; | |
5e5c78be | 6635 | Lisp_Object attrs; |
fb88bf2d | 6636 | |
24a73b0a | 6637 | code_conversion_save (0, 0); |
ec6d2bb8 | 6638 | |
24a73b0a | 6639 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
6640 | coding->src_chars = chars; |
6641 | coding->src_bytes = bytes; | |
6642 | coding->src_pos = -chars; | |
6643 | coding->src_pos_byte = -bytes; | |
6644 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 6645 | coding->dst_object = coding->src_object; |
df7492f9 KH |
6646 | coding->dst_pos = PT; |
6647 | coding->dst_pos_byte = PT_BYTE; | |
71c81426 | 6648 | coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters); |
98725083 | 6649 | coding->mode |= CODING_MODE_LAST_BLOCK; |
4ed46869 | 6650 | |
df7492f9 KH |
6651 | if (CODING_REQUIRE_DETECTION (coding)) |
6652 | detect_coding (coding); | |
8f924df7 | 6653 | |
df7492f9 | 6654 | decode_coding (coding); |
d46c5b12 | 6655 | |
5e5c78be KH |
6656 | attrs = CODING_ID_ATTRS (coding->id); |
6657 | if (! NILP (CODING_ATTR_POST_READ (attrs))) | |
b73bfc1c | 6658 | { |
5e5c78be KH |
6659 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
6660 | Lisp_Object val; | |
6661 | ||
6662 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
6663 | val = call1 (CODING_ATTR_POST_READ (attrs), |
6664 | make_number (coding->produced_char)); | |
5e5c78be KH |
6665 | CHECK_NATNUM (val); |
6666 | coding->produced_char += Z - prev_Z; | |
6667 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 6668 | } |
4ed46869 | 6669 | |
df7492f9 | 6670 | unbind_to (count, Qnil); |
b73bfc1c KH |
6671 | return coding->result; |
6672 | } | |
52d41803 | 6673 | |
4ed46869 | 6674 | int |
df7492f9 | 6675 | encode_coding_gap (coding, chars, bytes) |
4ed46869 | 6676 | struct coding_system *coding; |
df7492f9 | 6677 | EMACS_INT chars, bytes; |
4ed46869 | 6678 | { |
df7492f9 | 6679 | int count = specpdl_ptr - specpdl; |
4ed46869 | 6680 | |
24a73b0a | 6681 | code_conversion_save (0, 0); |
4ed46869 | 6682 | |
24a73b0a | 6683 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
6684 | coding->src_chars = chars; |
6685 | coding->src_bytes = bytes; | |
6686 | coding->src_pos = -chars; | |
6687 | coding->src_pos_byte = -bytes; | |
6688 | coding->src_multibyte = chars < bytes; | |
6689 | coding->dst_object = coding->src_object; | |
6690 | coding->dst_pos = PT; | |
6691 | coding->dst_pos_byte = PT_BYTE; | |
4ed46869 | 6692 | |
df7492f9 | 6693 | encode_coding (coding); |
b73bfc1c | 6694 | |
df7492f9 KH |
6695 | unbind_to (count, Qnil); |
6696 | return coding->result; | |
6697 | } | |
4ed46869 | 6698 | |
d46c5b12 | 6699 | |
df7492f9 KH |
6700 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
6701 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 6702 | |
df7492f9 | 6703 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 6704 | |
df7492f9 KH |
6705 | If it is a buffer, the text is at point of the buffer. FROM and TO |
6706 | are positions in the buffer. | |
b73bfc1c | 6707 | |
df7492f9 KH |
6708 | If it is a string, the text is at the beginning of the string. |
6709 | FROM and TO are indices to the string. | |
4ed46869 | 6710 | |
df7492f9 KH |
6711 | If it is nil, the text is at coding->source. FROM and TO are |
6712 | indices to coding->source. | |
bb10be8b | 6713 | |
df7492f9 | 6714 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 6715 | |
df7492f9 KH |
6716 | If it is a buffer, the decoded text is inserted at point of the |
6717 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
6718 | is deleted. | |
4ed46869 | 6719 | |
df7492f9 KH |
6720 | If it is Qt, a string is made from the decoded text, and |
6721 | set in CODING->dst_object. | |
d46c5b12 | 6722 | |
df7492f9 | 6723 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 6724 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
6725 | CODING->destination by xmalloc. If the decoded text is longer than |
6726 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
6727 | */ | |
d46c5b12 | 6728 | |
df7492f9 KH |
6729 | void |
6730 | decode_coding_object (coding, src_object, from, from_byte, to, to_byte, | |
6731 | dst_object) | |
d46c5b12 | 6732 | struct coding_system *coding; |
df7492f9 KH |
6733 | Lisp_Object src_object; |
6734 | EMACS_INT from, from_byte, to, to_byte; | |
6735 | Lisp_Object dst_object; | |
d46c5b12 | 6736 | { |
df7492f9 KH |
6737 | int count = specpdl_ptr - specpdl; |
6738 | unsigned char *destination; | |
6739 | EMACS_INT dst_bytes; | |
6740 | EMACS_INT chars = to - from; | |
6741 | EMACS_INT bytes = to_byte - from_byte; | |
6742 | Lisp_Object attrs; | |
4776e638 KH |
6743 | Lisp_Object buffer; |
6744 | int saved_pt = -1, saved_pt_byte; | |
d46c5b12 | 6745 | |
4776e638 | 6746 | buffer = Fcurrent_buffer (); |
93dec019 | 6747 | |
df7492f9 | 6748 | if (NILP (dst_object)) |
d46c5b12 | 6749 | { |
df7492f9 KH |
6750 | destination = coding->destination; |
6751 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 6752 | } |
93dec019 | 6753 | |
df7492f9 KH |
6754 | coding->src_object = src_object; |
6755 | coding->src_chars = chars; | |
6756 | coding->src_bytes = bytes; | |
6757 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 6758 | |
df7492f9 | 6759 | if (STRINGP (src_object)) |
d46c5b12 | 6760 | { |
df7492f9 KH |
6761 | coding->src_pos = from; |
6762 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 6763 | } |
df7492f9 | 6764 | else if (BUFFERP (src_object)) |
88993dfd | 6765 | { |
df7492f9 KH |
6766 | set_buffer_internal (XBUFFER (src_object)); |
6767 | if (from != GPT) | |
6768 | move_gap_both (from, from_byte); | |
6769 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 6770 | { |
4776e638 | 6771 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
6772 | TEMP_SET_PT_BOTH (from, from_byte); |
6773 | del_range_both (from, from_byte, to, to_byte, 1); | |
6774 | coding->src_pos = -chars; | |
6775 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 6776 | } |
df7492f9 | 6777 | else |
fb88bf2d | 6778 | { |
df7492f9 KH |
6779 | coding->src_pos = from; |
6780 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 6781 | } |
88993dfd KH |
6782 | } |
6783 | ||
df7492f9 KH |
6784 | if (CODING_REQUIRE_DETECTION (coding)) |
6785 | detect_coding (coding); | |
6786 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 6787 | |
2cb26057 KH |
6788 | if (EQ (dst_object, Qt) |
6789 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
6790 | && NILP (dst_object))) | |
b73bfc1c | 6791 | { |
24a73b0a | 6792 | coding->dst_object = code_conversion_save (1, 1); |
df7492f9 KH |
6793 | coding->dst_pos = BEG; |
6794 | coding->dst_pos_byte = BEG_BYTE; | |
6795 | coding->dst_multibyte = 1; | |
b73bfc1c | 6796 | } |
df7492f9 | 6797 | else if (BUFFERP (dst_object)) |
d46c5b12 | 6798 | { |
24a73b0a | 6799 | code_conversion_save (0, 0); |
df7492f9 KH |
6800 | coding->dst_object = dst_object; |
6801 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
6802 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
6803 | coding->dst_multibyte | |
6804 | = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters); | |
d46c5b12 KH |
6805 | } |
6806 | else | |
6807 | { | |
24a73b0a | 6808 | code_conversion_save (0, 0); |
df7492f9 KH |
6809 | coding->dst_object = Qnil; |
6810 | coding->dst_multibyte = 1; | |
d46c5b12 KH |
6811 | } |
6812 | ||
df7492f9 | 6813 | decode_coding (coding); |
fa46990e | 6814 | |
df7492f9 KH |
6815 | if (BUFFERP (coding->dst_object)) |
6816 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 6817 | |
df7492f9 | 6818 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 6819 | { |
df7492f9 KH |
6820 | struct gcpro gcpro1, gcpro2; |
6821 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; | |
2b4f9037 | 6822 | Lisp_Object val; |
d46c5b12 | 6823 | |
c0cc7f7f | 6824 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
df7492f9 | 6825 | GCPRO2 (coding->src_object, coding->dst_object); |
d4850d67 KH |
6826 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
6827 | make_number (coding->produced_char)); | |
df7492f9 KH |
6828 | UNGCPRO; |
6829 | CHECK_NATNUM (val); | |
6830 | coding->produced_char += Z - prev_Z; | |
6831 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 6832 | } |
de79a6a5 | 6833 | |
df7492f9 | 6834 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 6835 | { |
df7492f9 KH |
6836 | coding->dst_object = Fbuffer_string (); |
6837 | } | |
6838 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
6839 | { | |
6840 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
6841 | if (dst_bytes < coding->produced) | |
6842 | { | |
6843 | destination | |
6844 | = (unsigned char *) xrealloc (destination, coding->produced); | |
6845 | if (! destination) | |
6846 | { | |
065e3595 KH |
6847 | record_conversion_result (coding, |
6848 | CODING_RESULT_INSUFFICIENT_DST); | |
df7492f9 KH |
6849 | unbind_to (count, Qnil); |
6850 | return; | |
6851 | } | |
6852 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) | |
6853 | move_gap_both (BEGV, BEGV_BYTE); | |
6854 | bcopy (BEGV_ADDR, destination, coding->produced); | |
6855 | coding->destination = destination; | |
d46c5b12 | 6856 | } |
ec6d2bb8 | 6857 | } |
b73bfc1c | 6858 | |
4776e638 KH |
6859 | if (saved_pt >= 0) |
6860 | { | |
6861 | /* This is the case of: | |
6862 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
6863 | As we have moved PT while replacing the original buffer | |
6864 | contents, we must recover it now. */ | |
6865 | set_buffer_internal (XBUFFER (src_object)); | |
6866 | if (saved_pt < from) | |
6867 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
6868 | else if (saved_pt < from + chars) | |
6869 | TEMP_SET_PT_BOTH (from, from_byte); | |
6870 | else if (! NILP (current_buffer->enable_multibyte_characters)) | |
6871 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), | |
6872 | saved_pt_byte + (coding->produced - bytes)); | |
6873 | else | |
6874 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
6875 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 6876 | } |
4776e638 | 6877 | |
065e3595 | 6878 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
6879 | } |
6880 | ||
d46c5b12 | 6881 | |
df7492f9 KH |
6882 | void |
6883 | encode_coding_object (coding, src_object, from, from_byte, to, to_byte, | |
6884 | dst_object) | |
d46c5b12 | 6885 | struct coding_system *coding; |
df7492f9 KH |
6886 | Lisp_Object src_object; |
6887 | EMACS_INT from, from_byte, to, to_byte; | |
6888 | Lisp_Object dst_object; | |
d46c5b12 | 6889 | { |
b73bfc1c | 6890 | int count = specpdl_ptr - specpdl; |
df7492f9 KH |
6891 | EMACS_INT chars = to - from; |
6892 | EMACS_INT bytes = to_byte - from_byte; | |
6893 | Lisp_Object attrs; | |
4776e638 KH |
6894 | Lisp_Object buffer; |
6895 | int saved_pt = -1, saved_pt_byte; | |
c02d943b | 6896 | int kill_src_buffer = 0; |
df7492f9 | 6897 | |
4776e638 | 6898 | buffer = Fcurrent_buffer (); |
df7492f9 KH |
6899 | |
6900 | coding->src_object = src_object; | |
6901 | coding->src_chars = chars; | |
6902 | coding->src_bytes = bytes; | |
6903 | coding->src_multibyte = chars < bytes; | |
6904 | ||
6905 | attrs = CODING_ID_ATTRS (coding->id); | |
6906 | ||
6907 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
6bac5b12 | 6908 | { |
24a73b0a | 6909 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
6910 | set_buffer_internal (XBUFFER (coding->src_object)); |
6911 | if (STRINGP (src_object)) | |
6912 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
6913 | else if (BUFFERP (src_object)) | |
6914 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
6915 | else | |
6916 | insert_1_both (coding->source + from, chars, bytes, 0, 0, 0); | |
d46c5b12 | 6917 | |
df7492f9 KH |
6918 | if (EQ (src_object, dst_object)) |
6919 | { | |
6920 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 6921 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
6922 | del_range_both (from, from_byte, to, to_byte, 1); |
6923 | set_buffer_internal (XBUFFER (coding->src_object)); | |
6924 | } | |
6925 | ||
d4850d67 KH |
6926 | { |
6927 | Lisp_Object args[3]; | |
6928 | ||
6929 | args[0] = CODING_ATTR_PRE_WRITE (attrs); | |
6930 | args[1] = make_number (BEG); | |
6931 | args[2] = make_number (Z); | |
6932 | safe_call (3, args); | |
6933 | } | |
c02d943b KH |
6934 | if (XBUFFER (coding->src_object) != current_buffer) |
6935 | kill_src_buffer = 1; | |
ac87bbef | 6936 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
6937 | if (BEG != GPT) |
6938 | move_gap_both (BEG, BEG_BYTE); | |
6939 | coding->src_chars = Z - BEG; | |
6940 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
6941 | coding->src_pos = BEG; | |
6942 | coding->src_pos_byte = BEG_BYTE; | |
6943 | coding->src_multibyte = Z < Z_BYTE; | |
6944 | } | |
6945 | else if (STRINGP (src_object)) | |
d46c5b12 | 6946 | { |
24a73b0a | 6947 | code_conversion_save (0, 0); |
df7492f9 KH |
6948 | coding->src_pos = from; |
6949 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 6950 | } |
df7492f9 | 6951 | else if (BUFFERP (src_object)) |
b73bfc1c | 6952 | { |
24a73b0a | 6953 | code_conversion_save (0, 0); |
df7492f9 | 6954 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 6955 | if (EQ (src_object, dst_object)) |
d46c5b12 | 6956 | { |
4776e638 | 6957 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
6958 | coding->src_object = del_range_1 (from, to, 1, 1); |
6959 | coding->src_pos = 0; | |
6960 | coding->src_pos_byte = 0; | |
d46c5b12 | 6961 | } |
df7492f9 | 6962 | else |
d46c5b12 | 6963 | { |
ff0dacd7 KH |
6964 | if (from < GPT && to >= GPT) |
6965 | move_gap_both (from, from_byte); | |
df7492f9 KH |
6966 | coding->src_pos = from; |
6967 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 6968 | } |
d46c5b12 | 6969 | } |
4776e638 | 6970 | else |
24a73b0a | 6971 | code_conversion_save (0, 0); |
d46c5b12 | 6972 | |
df7492f9 | 6973 | if (BUFFERP (dst_object)) |
88993dfd | 6974 | { |
df7492f9 | 6975 | coding->dst_object = dst_object; |
28f67a95 KH |
6976 | if (EQ (src_object, dst_object)) |
6977 | { | |
6978 | coding->dst_pos = from; | |
6979 | coding->dst_pos_byte = from_byte; | |
6980 | } | |
6981 | else | |
6982 | { | |
6983 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
6984 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
6985 | } | |
df7492f9 KH |
6986 | coding->dst_multibyte |
6987 | = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters); | |
88993dfd | 6988 | } |
df7492f9 | 6989 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 6990 | { |
df7492f9 | 6991 | coding->dst_object = Qnil; |
df7492f9 | 6992 | coding->dst_bytes = coding->src_chars; |
ac87bbef KH |
6993 | if (coding->dst_bytes == 0) |
6994 | coding->dst_bytes = 1; | |
6995 | coding->destination = (unsigned char *) xmalloc (coding->dst_bytes); | |
df7492f9 | 6996 | coding->dst_multibyte = 0; |
d46c5b12 KH |
6997 | } |
6998 | else | |
6999 | { | |
df7492f9 KH |
7000 | coding->dst_object = Qnil; |
7001 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
7002 | } |
7003 | ||
df7492f9 | 7004 | encode_coding (coding); |
d46c5b12 | 7005 | |
df7492f9 | 7006 | if (EQ (dst_object, Qt)) |
d46c5b12 | 7007 | { |
df7492f9 KH |
7008 | if (BUFFERP (coding->dst_object)) |
7009 | coding->dst_object = Fbuffer_string (); | |
7010 | else | |
d46c5b12 | 7011 | { |
df7492f9 KH |
7012 | coding->dst_object |
7013 | = make_unibyte_string ((char *) coding->destination, | |
7014 | coding->produced); | |
7015 | xfree (coding->destination); | |
d46c5b12 | 7016 | } |
4ed46869 | 7017 | } |
d46c5b12 | 7018 | |
4776e638 KH |
7019 | if (saved_pt >= 0) |
7020 | { | |
7021 | /* This is the case of: | |
7022 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7023 | As we have moved PT while replacing the original buffer | |
7024 | contents, we must recover it now. */ | |
7025 | set_buffer_internal (XBUFFER (src_object)); | |
7026 | if (saved_pt < from) | |
7027 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7028 | else if (saved_pt < from + chars) | |
7029 | TEMP_SET_PT_BOTH (from, from_byte); | |
7030 | else if (! NILP (current_buffer->enable_multibyte_characters)) | |
7031 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), | |
7032 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 7033 | else |
4776e638 KH |
7034 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
7035 | saved_pt_byte + (coding->produced - bytes)); | |
7036 | } | |
7037 | ||
c02d943b KH |
7038 | if (kill_src_buffer) |
7039 | Fkill_buffer (coding->src_object); | |
df7492f9 | 7040 | unbind_to (count, Qnil); |
b73bfc1c KH |
7041 | } |
7042 | ||
df7492f9 | 7043 | |
b73bfc1c | 7044 | Lisp_Object |
df7492f9 | 7045 | preferred_coding_system () |
b73bfc1c | 7046 | { |
df7492f9 | 7047 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 7048 | |
df7492f9 | 7049 | return CODING_ID_NAME (id); |
4ed46869 KH |
7050 | } |
7051 | ||
7052 | \f | |
7053 | #ifdef emacs | |
1397dc18 | 7054 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 7055 | |
4ed46869 | 7056 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 7057 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 7058 | See the documentation of `define-coding-system' for information |
48b0f3ae PJ |
7059 | about coding-system objects. */) |
7060 | (obj) | |
4ed46869 KH |
7061 | Lisp_Object obj; |
7062 | { | |
df7492f9 | 7063 | return ((NILP (obj) || CODING_SYSTEM_P (obj)) ? Qt : Qnil); |
4ed46869 KH |
7064 | } |
7065 | ||
9d991de8 RS |
7066 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
7067 | Sread_non_nil_coding_system, 1, 1, 0, | |
48b0f3ae PJ |
7068 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
7069 | (prompt) | |
4ed46869 KH |
7070 | Lisp_Object prompt; |
7071 | { | |
e0e989f6 | 7072 | Lisp_Object val; |
9d991de8 RS |
7073 | do |
7074 | { | |
4608c386 KH |
7075 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
7076 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 7077 | } |
8f924df7 | 7078 | while (SCHARS (val) == 0); |
e0e989f6 | 7079 | return (Fintern (val, Qnil)); |
4ed46869 KH |
7080 | } |
7081 | ||
9b787f3e | 7082 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae PJ |
7083 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
7084 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */) | |
7085 | (prompt, default_coding_system) | |
9b787f3e | 7086 | Lisp_Object prompt, default_coding_system; |
4ed46869 | 7087 | { |
f44d27ce | 7088 | Lisp_Object val; |
9b787f3e | 7089 | if (SYMBOLP (default_coding_system)) |
a3181084 | 7090 | XSETSTRING (default_coding_system, XPNTR (SYMBOL_NAME (default_coding_system))); |
4608c386 | 7091 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
7092 | Qt, Qnil, Qcoding_system_history, |
7093 | default_coding_system, Qnil); | |
8f924df7 | 7094 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
7095 | } |
7096 | ||
7097 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, | |
7098 | 1, 1, 0, | |
48b0f3ae | 7099 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
7100 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
7101 | It is valid if it is nil or a symbol defined as a coding system by the | |
7102 | function `define-coding-system'. */) | |
df7492f9 | 7103 | (coding_system) |
4ed46869 KH |
7104 | Lisp_Object coding_system; |
7105 | { | |
b7826503 | 7106 | CHECK_SYMBOL (coding_system); |
4ed46869 KH |
7107 | if (!NILP (Fcoding_system_p (coding_system))) |
7108 | return coding_system; | |
7109 | while (1) | |
02ba4723 | 7110 | Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil)); |
4ed46869 | 7111 | } |
df7492f9 | 7112 | |
3a73fa5d | 7113 | \f |
89528eb3 KH |
7114 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
7115 | HIGHEST is nonzero, return the coding system of the highest | |
7116 | priority among the detected coding systems. Otherwize return a | |
7117 | list of detected coding systems sorted by their priorities. If | |
7118 | MULTIBYTEP is nonzero, it is assumed that the bytes are in correct | |
7119 | multibyte form but contains only ASCII and eight-bit chars. | |
7120 | Otherwise, the bytes are raw bytes. | |
7121 | ||
7122 | CODING-SYSTEM controls the detection as below: | |
7123 | ||
7124 | If it is nil, detect both text-format and eol-format. If the | |
7125 | text-format part of CODING-SYSTEM is already specified | |
7126 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
7127 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
7128 | detect only text-format. */ | |
7129 | ||
d46c5b12 | 7130 | Lisp_Object |
24a73b0a KH |
7131 | detect_coding_system (src, src_chars, src_bytes, highest, multibytep, |
7132 | coding_system) | |
8f924df7 | 7133 | const unsigned char *src; |
24a73b0a | 7134 | int src_chars, src_bytes, highest; |
0a28aafb | 7135 | int multibytep; |
df7492f9 | 7136 | Lisp_Object coding_system; |
4ed46869 | 7137 | { |
8f924df7 | 7138 | const unsigned char *src_end = src + src_bytes; |
df7492f9 KH |
7139 | Lisp_Object attrs, eol_type; |
7140 | Lisp_Object val; | |
7141 | struct coding_system coding; | |
89528eb3 | 7142 | int id; |
ff0dacd7 | 7143 | struct coding_detection_info detect_info; |
24a73b0a | 7144 | enum coding_category base_category; |
b73bfc1c | 7145 | |
df7492f9 KH |
7146 | if (NILP (coding_system)) |
7147 | coding_system = Qundecided; | |
7148 | setup_coding_system (coding_system, &coding); | |
7149 | attrs = CODING_ID_ATTRS (coding.id); | |
7150 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 7151 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 7152 | |
df7492f9 | 7153 | coding.source = src; |
24a73b0a | 7154 | coding.src_chars = src_chars; |
df7492f9 KH |
7155 | coding.src_bytes = src_bytes; |
7156 | coding.src_multibyte = multibytep; | |
7157 | coding.consumed = 0; | |
89528eb3 | 7158 | coding.mode |= CODING_MODE_LAST_BLOCK; |
d46c5b12 | 7159 | |
ff0dacd7 | 7160 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 7161 | |
89528eb3 | 7162 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
7163 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
7164 | if (base_category == coding_category_undecided) | |
4ed46869 | 7165 | { |
ff0dacd7 KH |
7166 | enum coding_category category; |
7167 | struct coding_system *this; | |
7168 | int c, i; | |
88993dfd | 7169 | |
24a73b0a KH |
7170 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
7171 | for (i = 0; src < src_end; i++, src++) | |
4ed46869 | 7172 | { |
df7492f9 | 7173 | c = *src; |
6cb21a4f | 7174 | if (c & 0x80) |
d46c5b12 | 7175 | break; |
6cb21a4f KH |
7176 | if (c < 0x20 |
7177 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
7178 | && inhibit_iso_escape_detection) | |
7179 | { | |
7180 | coding.head_ascii = src - coding.source; | |
7181 | if (detect_coding_iso_2022 (&coding, &detect_info)) | |
7182 | { | |
7183 | /* We have scanned the whole data. */ | |
7184 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
7185 | /* We didn't find an 8-bit code. */ | |
7186 | src = src_end; | |
7187 | break; | |
7188 | } | |
7189 | } | |
4ed46869 | 7190 | } |
df7492f9 | 7191 | coding.head_ascii = src - coding.source; |
88993dfd | 7192 | |
6cb21a4f KH |
7193 | if (src < src_end |
7194 | || detect_info.found) | |
7195 | { | |
7196 | if (src == src_end) | |
7197 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
7198 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 7199 | { |
6cb21a4f KH |
7200 | category = coding_priorities[i]; |
7201 | if (detect_info.found & (1 << category)) | |
ff0dacd7 KH |
7202 | break; |
7203 | } | |
6cb21a4f KH |
7204 | else |
7205 | for (i = 0; i < coding_category_raw_text; i++) | |
df7492f9 | 7206 | { |
6cb21a4f KH |
7207 | category = coding_priorities[i]; |
7208 | this = coding_categories + category; | |
7209 | ||
7210 | if (this->id < 0) | |
24a73b0a | 7211 | { |
6cb21a4f KH |
7212 | /* No coding system of this category is defined. */ |
7213 | detect_info.rejected |= (1 << category); | |
7214 | } | |
7215 | else if (category >= coding_category_raw_text) | |
7216 | continue; | |
7217 | else if (detect_info.checked & (1 << category)) | |
7218 | { | |
7219 | if (highest | |
7220 | && (detect_info.found & (1 << category))) | |
7221 | break; | |
7222 | } | |
7223 | else | |
7224 | { | |
7225 | if ((*(this->detector)) (&coding, &detect_info) | |
7226 | && highest | |
7227 | && (detect_info.found & (1 << category))) | |
24a73b0a | 7228 | { |
6cb21a4f KH |
7229 | if (category == coding_category_utf_16_auto) |
7230 | { | |
7231 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
7232 | category = coding_category_utf_16_le; | |
7233 | else | |
7234 | category = coding_category_utf_16_be; | |
7235 | } | |
7236 | break; | |
24a73b0a | 7237 | } |
24a73b0a | 7238 | } |
df7492f9 | 7239 | } |
6cb21a4f | 7240 | } |
ec6d2bb8 | 7241 | |
ff0dacd7 | 7242 | if (detect_info.rejected == CATEGORY_MASK_ANY) |
ec6d2bb8 | 7243 | { |
ff0dacd7 | 7244 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
89528eb3 KH |
7245 | id = coding_categories[coding_category_raw_text].id; |
7246 | val = Fcons (make_number (id), Qnil); | |
7247 | } | |
ff0dacd7 | 7248 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 7249 | { |
ff0dacd7 | 7250 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 KH |
7251 | id = coding_categories[coding_category_undecided].id; |
7252 | val = Fcons (make_number (id), Qnil); | |
7253 | } | |
7254 | else if (highest) | |
7255 | { | |
ff0dacd7 | 7256 | if (detect_info.found) |
ec6d2bb8 | 7257 | { |
ff0dacd7 KH |
7258 | detect_info.found = 1 << category; |
7259 | val = Fcons (make_number (this->id), Qnil); | |
7260 | } | |
7261 | else | |
7262 | for (i = 0; i < coding_category_raw_text; i++) | |
7263 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
7264 | { | |
7265 | detect_info.found = 1 << coding_priorities[i]; | |
7266 | id = coding_categories[coding_priorities[i]].id; | |
7267 | val = Fcons (make_number (id), Qnil); | |
7268 | break; | |
7269 | } | |
7270 | } | |
89528eb3 KH |
7271 | else |
7272 | { | |
ff0dacd7 KH |
7273 | int mask = detect_info.rejected | detect_info.found; |
7274 | int found = 0; | |
89528eb3 | 7275 | val = Qnil; |
ec6d2bb8 | 7276 | |
89528eb3 | 7277 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
7278 | { |
7279 | category = coding_priorities[i]; | |
7280 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 7281 | { |
ff0dacd7 KH |
7282 | found |= 1 << category; |
7283 | id = coding_categories[category].id; | |
7284 | val = Fcons (make_number (id), val); | |
7285 | } | |
7286 | } | |
7287 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
7288 | { | |
7289 | category = coding_priorities[i]; | |
7290 | if (detect_info.found & (1 << category)) | |
7291 | { | |
7292 | id = coding_categories[category].id; | |
7293 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 7294 | } |
ec6d2bb8 | 7295 | } |
ff0dacd7 | 7296 | detect_info.found |= found; |
ec6d2bb8 | 7297 | } |
ec6d2bb8 | 7298 | } |
24a73b0a KH |
7299 | else if (base_category == coding_category_utf_16_auto) |
7300 | { | |
7301 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
7302 | { | |
24a73b0a KH |
7303 | struct coding_system *this; |
7304 | ||
7305 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
7306 | this = coding_categories + coding_category_utf_16_le; | |
7307 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
7308 | this = coding_categories + coding_category_utf_16_be; | |
7309 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
7310 | this = coding_categories + coding_category_utf_16_be_nosig; | |
7311 | else | |
7312 | this = coding_categories + coding_category_utf_16_le_nosig; | |
7313 | val = Fcons (make_number (this->id), Qnil); | |
7314 | } | |
7315 | } | |
df7492f9 KH |
7316 | else |
7317 | { | |
ff0dacd7 | 7318 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
89528eb3 | 7319 | val = Fcons (make_number (coding.id), Qnil); |
4ed46869 | 7320 | } |
df7492f9 | 7321 | |
89528eb3 | 7322 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 7323 | { |
89528eb3 | 7324 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol; |
df7492f9 KH |
7325 | Lisp_Object tail; |
7326 | ||
89528eb3 KH |
7327 | if (VECTORP (eol_type)) |
7328 | { | |
ff0dacd7 | 7329 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
89528eb3 KH |
7330 | normal_eol = detect_eol (coding.source, src_bytes, |
7331 | coding_category_raw_text); | |
ff0dacd7 KH |
7332 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
7333 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
7334 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
7335 | coding_category_utf_16_be); | |
ff0dacd7 KH |
7336 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
7337 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
7338 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
7339 | coding_category_utf_16_le); | |
7340 | } | |
7341 | else | |
7342 | { | |
7343 | if (EQ (eol_type, Qunix)) | |
7344 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
7345 | else if (EQ (eol_type, Qdos)) | |
7346 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
7347 | else | |
7348 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
7349 | } | |
7350 | ||
df7492f9 KH |
7351 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
7352 | { | |
89528eb3 | 7353 | enum coding_category category; |
df7492f9 | 7354 | int this_eol; |
89528eb3 KH |
7355 | |
7356 | id = XINT (XCAR (tail)); | |
7357 | attrs = CODING_ID_ATTRS (id); | |
7358 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
7359 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
7360 | if (VECTORP (eol_type)) |
7361 | { | |
89528eb3 KH |
7362 | if (category == coding_category_utf_16_be |
7363 | || category == coding_category_utf_16_be_nosig) | |
7364 | this_eol = utf_16_be_eol; | |
7365 | else if (category == coding_category_utf_16_le | |
7366 | || category == coding_category_utf_16_le_nosig) | |
7367 | this_eol = utf_16_le_eol; | |
df7492f9 | 7368 | else |
89528eb3 KH |
7369 | this_eol = normal_eol; |
7370 | ||
df7492f9 KH |
7371 | if (this_eol == EOL_SEEN_LF) |
7372 | XSETCAR (tail, AREF (eol_type, 0)); | |
7373 | else if (this_eol == EOL_SEEN_CRLF) | |
7374 | XSETCAR (tail, AREF (eol_type, 1)); | |
7375 | else if (this_eol == EOL_SEEN_CR) | |
7376 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
7377 | else |
7378 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 7379 | } |
89528eb3 KH |
7380 | else |
7381 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
7382 | } |
7383 | } | |
ec6d2bb8 | 7384 | |
03699b14 | 7385 | return (highest ? XCAR (val) : val); |
ec6d2bb8 KH |
7386 | } |
7387 | ||
ec6d2bb8 | 7388 | |
d46c5b12 KH |
7389 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
7390 | 2, 3, 0, | |
48b0f3ae PJ |
7391 | doc: /* Detect coding system of the text in the region between START and END. |
7392 | Return a list of possible coding systems ordered by priority. | |
ec6d2bb8 | 7393 | |
48b0f3ae PJ |
7394 | If only ASCII characters are found, it returns a list of single element |
7395 | `undecided' or its subsidiary coding system according to a detected | |
7396 | end-of-line format. | |
ec6d2bb8 | 7397 | |
48b0f3ae PJ |
7398 | If optional argument HIGHEST is non-nil, return the coding system of |
7399 | highest priority. */) | |
7400 | (start, end, highest) | |
d46c5b12 KH |
7401 | Lisp_Object start, end, highest; |
7402 | { | |
7403 | int from, to; | |
7404 | int from_byte, to_byte; | |
ec6d2bb8 | 7405 | |
b7826503 PJ |
7406 | CHECK_NUMBER_COERCE_MARKER (start); |
7407 | CHECK_NUMBER_COERCE_MARKER (end); | |
ec6d2bb8 | 7408 | |
d46c5b12 KH |
7409 | validate_region (&start, &end); |
7410 | from = XINT (start), to = XINT (end); | |
7411 | from_byte = CHAR_TO_BYTE (from); | |
7412 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 7413 | |
d46c5b12 KH |
7414 | if (from < GPT && to >= GPT) |
7415 | move_gap_both (to, to_byte); | |
c210f766 | 7416 | |
d46c5b12 | 7417 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 7418 | to - from, to_byte - from_byte, |
0a28aafb KH |
7419 | !NILP (highest), |
7420 | !NILP (current_buffer | |
df7492f9 KH |
7421 | ->enable_multibyte_characters), |
7422 | Qnil); | |
ec6d2bb8 KH |
7423 | } |
7424 | ||
d46c5b12 KH |
7425 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
7426 | 1, 2, 0, | |
48b0f3ae PJ |
7427 | doc: /* Detect coding system of the text in STRING. |
7428 | Return a list of possible coding systems ordered by priority. | |
fb88bf2d | 7429 | |
48b0f3ae PJ |
7430 | If only ASCII characters are found, it returns a list of single element |
7431 | `undecided' or its subsidiary coding system according to a detected | |
7432 | end-of-line format. | |
d46c5b12 | 7433 | |
48b0f3ae PJ |
7434 | If optional argument HIGHEST is non-nil, return the coding system of |
7435 | highest priority. */) | |
7436 | (string, highest) | |
d46c5b12 KH |
7437 | Lisp_Object string, highest; |
7438 | { | |
b7826503 | 7439 | CHECK_STRING (string); |
b73bfc1c | 7440 | |
24a73b0a KH |
7441 | return detect_coding_system (SDATA (string), |
7442 | SCHARS (string), SBYTES (string), | |
8f924df7 | 7443 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 7444 | Qnil); |
4ed46869 | 7445 | } |
4ed46869 | 7446 | |
b73bfc1c | 7447 | |
df7492f9 KH |
7448 | static INLINE int |
7449 | char_encodable_p (c, attrs) | |
7450 | int c; | |
7451 | Lisp_Object attrs; | |
05e6f5dc | 7452 | { |
df7492f9 | 7453 | Lisp_Object tail; |
df7492f9 | 7454 | struct charset *charset; |
7d64c6ad | 7455 | Lisp_Object translation_table; |
d46c5b12 | 7456 | |
7d64c6ad | 7457 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 7458 | if (! NILP (translation_table)) |
7d64c6ad | 7459 | c = translate_char (translation_table, c); |
df7492f9 KH |
7460 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
7461 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 7462 | { |
df7492f9 KH |
7463 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
7464 | if (CHAR_CHARSET_P (c, charset)) | |
7465 | break; | |
e133c8fa | 7466 | } |
df7492f9 | 7467 | return (! NILP (tail)); |
05e6f5dc | 7468 | } |
83fa074f | 7469 | |
fb88bf2d | 7470 | |
df7492f9 KH |
7471 | /* Return a list of coding systems that safely encode the text between |
7472 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
7473 | systems not to check. The returned list doesn't contain any such | |
48468dac | 7474 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 7475 | unibyte, return t. */ |
e077cc80 | 7476 | |
df7492f9 KH |
7477 | DEFUN ("find-coding-systems-region-internal", |
7478 | Ffind_coding_systems_region_internal, | |
7479 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
7480 | doc: /* Internal use only. */) | |
7481 | (start, end, exclude) | |
7482 | Lisp_Object start, end, exclude; | |
7483 | { | |
7484 | Lisp_Object coding_attrs_list, safe_codings; | |
7485 | EMACS_INT start_byte, end_byte; | |
7c78e542 | 7486 | const unsigned char *p, *pbeg, *pend; |
df7492f9 KH |
7487 | int c; |
7488 | Lisp_Object tail, elt; | |
d46c5b12 | 7489 | |
df7492f9 KH |
7490 | if (STRINGP (start)) |
7491 | { | |
7492 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 7493 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
7494 | return Qt; |
7495 | start_byte = 0; | |
8f924df7 | 7496 | end_byte = SBYTES (start); |
df7492f9 KH |
7497 | } |
7498 | else | |
d46c5b12 | 7499 | { |
df7492f9 KH |
7500 | CHECK_NUMBER_COERCE_MARKER (start); |
7501 | CHECK_NUMBER_COERCE_MARKER (end); | |
7502 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
7503 | args_out_of_range (start, end); | |
7504 | if (NILP (current_buffer->enable_multibyte_characters)) | |
7505 | return Qt; | |
7506 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
7507 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
7508 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
7509 | return Qt; | |
d46c5b12 | 7510 | |
e1c23804 | 7511 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 7512 | { |
e1c23804 DL |
7513 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
7514 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 7515 | else |
e1c23804 | 7516 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
7517 | } |
7518 | } | |
7519 | ||
df7492f9 KH |
7520 | coding_attrs_list = Qnil; |
7521 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
7522 | if (NILP (exclude) | |
7523 | || NILP (Fmemq (XCAR (tail), exclude))) | |
7524 | { | |
7525 | Lisp_Object attrs; | |
d46c5b12 | 7526 | |
df7492f9 KH |
7527 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
7528 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)) | |
7529 | && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
7d64c6ad KH |
7530 | { |
7531 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 7532 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
7533 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
7534 | } | |
df7492f9 | 7535 | } |
d46c5b12 | 7536 | |
df7492f9 | 7537 | if (STRINGP (start)) |
8f924df7 | 7538 | p = pbeg = SDATA (start); |
df7492f9 KH |
7539 | else |
7540 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
7541 | pend = p + (end_byte - start_byte); | |
b843d1ae | 7542 | |
df7492f9 KH |
7543 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
7544 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 7545 | |
05e6f5dc | 7546 | while (p < pend) |
72d1a715 | 7547 | { |
df7492f9 KH |
7548 | if (ASCII_BYTE_P (*p)) |
7549 | p++; | |
72d1a715 RS |
7550 | else |
7551 | { | |
df7492f9 | 7552 | c = STRING_CHAR_ADVANCE (p); |
12410ef1 | 7553 | |
df7492f9 KH |
7554 | charset_map_loaded = 0; |
7555 | for (tail = coding_attrs_list; CONSP (tail);) | |
7556 | { | |
7557 | elt = XCAR (tail); | |
7558 | if (NILP (elt)) | |
7559 | tail = XCDR (tail); | |
7560 | else if (char_encodable_p (c, elt)) | |
7561 | tail = XCDR (tail); | |
7562 | else if (CONSP (XCDR (tail))) | |
7563 | { | |
7564 | XSETCAR (tail, XCAR (XCDR (tail))); | |
7565 | XSETCDR (tail, XCDR (XCDR (tail))); | |
7566 | } | |
7567 | else | |
7568 | { | |
7569 | XSETCAR (tail, Qnil); | |
7570 | tail = XCDR (tail); | |
7571 | } | |
7572 | } | |
7573 | if (charset_map_loaded) | |
7574 | { | |
7575 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
05e6f5dc | 7576 | |
df7492f9 | 7577 | if (STRINGP (start)) |
8f924df7 | 7578 | pbeg = SDATA (start); |
df7492f9 KH |
7579 | else |
7580 | pbeg = BYTE_POS_ADDR (start_byte); | |
7581 | p = pbeg + p_offset; | |
7582 | pend = pbeg + pend_offset; | |
7583 | } | |
7584 | } | |
ec6d2bb8 | 7585 | } |
fb88bf2d | 7586 | |
988b3759 | 7587 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
7588 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
7589 | if (! NILP (XCAR (tail))) | |
7590 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 7591 | |
05e6f5dc KH |
7592 | return safe_codings; |
7593 | } | |
4956c225 | 7594 | |
d46c5b12 | 7595 | |
8f924df7 KH |
7596 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
7597 | Sunencodable_char_position, 3, 5, 0, | |
7598 | doc: /* | |
7599 | Return position of first un-encodable character in a region. | |
7600 | START and END specfiy the region and CODING-SYSTEM specifies the | |
7601 | encoding to check. Return nil if CODING-SYSTEM does encode the region. | |
d46c5b12 | 7602 | |
8f924df7 KH |
7603 | If optional 4th argument COUNT is non-nil, it specifies at most how |
7604 | many un-encodable characters to search. In this case, the value is a | |
7605 | list of positions. | |
d46c5b12 | 7606 | |
8f924df7 KH |
7607 | If optional 5th argument STRING is non-nil, it is a string to search |
7608 | for un-encodable characters. In that case, START and END are indexes | |
7609 | to the string. */) | |
7610 | (start, end, coding_system, count, string) | |
7611 | Lisp_Object start, end, coding_system, count, string; | |
7612 | { | |
7613 | int n; | |
7614 | struct coding_system coding; | |
7d64c6ad | 7615 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 KH |
7616 | Lisp_Object positions; |
7617 | int from, to; | |
7618 | const unsigned char *p, *stop, *pend; | |
7619 | int ascii_compatible; | |
fb88bf2d | 7620 | |
8f924df7 KH |
7621 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
7622 | attrs = CODING_ID_ATTRS (coding.id); | |
7623 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
7624 | return Qnil; | |
7625 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
7626 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 7627 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 7628 | |
8f924df7 KH |
7629 | if (NILP (string)) |
7630 | { | |
7631 | validate_region (&start, &end); | |
7632 | from = XINT (start); | |
7633 | to = XINT (end); | |
7634 | if (NILP (current_buffer->enable_multibyte_characters) | |
7635 | || (ascii_compatible | |
7636 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
7637 | return Qnil; | |
7638 | p = CHAR_POS_ADDR (from); | |
7639 | pend = CHAR_POS_ADDR (to); | |
7640 | if (from < GPT && to >= GPT) | |
7641 | stop = GPT_ADDR; | |
7642 | else | |
7643 | stop = pend; | |
7644 | } | |
7645 | else | |
7646 | { | |
7647 | CHECK_STRING (string); | |
7648 | CHECK_NATNUM (start); | |
7649 | CHECK_NATNUM (end); | |
7650 | from = XINT (start); | |
7651 | to = XINT (end); | |
7652 | if (from > to | |
7653 | || to > SCHARS (string)) | |
7654 | args_out_of_range_3 (string, start, end); | |
7655 | if (! STRING_MULTIBYTE (string)) | |
7656 | return Qnil; | |
7657 | p = SDATA (string) + string_char_to_byte (string, from); | |
7658 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
7659 | if (ascii_compatible && (to - from) == (pend - p)) | |
7660 | return Qnil; | |
7661 | } | |
f2558efd | 7662 | |
8f924df7 KH |
7663 | if (NILP (count)) |
7664 | n = 1; | |
7665 | else | |
b73bfc1c | 7666 | { |
8f924df7 KH |
7667 | CHECK_NATNUM (count); |
7668 | n = XINT (count); | |
b73bfc1c KH |
7669 | } |
7670 | ||
8f924df7 KH |
7671 | positions = Qnil; |
7672 | while (1) | |
d46c5b12 | 7673 | { |
8f924df7 | 7674 | int c; |
ec6d2bb8 | 7675 | |
8f924df7 KH |
7676 | if (ascii_compatible) |
7677 | while (p < stop && ASCII_BYTE_P (*p)) | |
7678 | p++, from++; | |
7679 | if (p >= stop) | |
0e79d667 | 7680 | { |
8f924df7 KH |
7681 | if (p >= pend) |
7682 | break; | |
7683 | stop = pend; | |
7684 | p = GAP_END_ADDR; | |
0e79d667 | 7685 | } |
ec6d2bb8 | 7686 | |
8f924df7 KH |
7687 | c = STRING_CHAR_ADVANCE (p); |
7688 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
7689 | && ! char_charset (translate_char (translation_table, c), |
7690 | charset_list, NULL)) | |
ec6d2bb8 | 7691 | { |
8f924df7 KH |
7692 | positions = Fcons (make_number (from), positions); |
7693 | n--; | |
7694 | if (n == 0) | |
7695 | break; | |
ec6d2bb8 KH |
7696 | } |
7697 | ||
8f924df7 KH |
7698 | from++; |
7699 | } | |
d46c5b12 | 7700 | |
8f924df7 KH |
7701 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
7702 | } | |
d46c5b12 | 7703 | |
d46c5b12 | 7704 | |
df7492f9 KH |
7705 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
7706 | Scheck_coding_systems_region, 3, 3, 0, | |
7707 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 7708 | |
df7492f9 KH |
7709 | START and END are buffer positions specifying the region. |
7710 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 7711 | |
df7492f9 KH |
7712 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
7713 | CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the | |
7714 | whole region, POS0, POS1, ... are buffer positions where non-encodable | |
7715 | characters are found. | |
93dec019 | 7716 | |
df7492f9 KH |
7717 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
7718 | value is nil. | |
93dec019 | 7719 | |
df7492f9 KH |
7720 | START may be a string. In that case, check if the string is |
7721 | encodable, and the value contains indices to the string instead of | |
7722 | buffer positions. END is ignored. */) | |
7723 | (start, end, coding_system_list) | |
7724 | Lisp_Object start, end, coding_system_list; | |
05e6f5dc | 7725 | { |
df7492f9 KH |
7726 | Lisp_Object list; |
7727 | EMACS_INT start_byte, end_byte; | |
7728 | int pos; | |
7c78e542 | 7729 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 7730 | int c; |
7d64c6ad | 7731 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 7732 | |
05e6f5dc KH |
7733 | if (STRINGP (start)) |
7734 | { | |
df7492f9 | 7735 | if (!STRING_MULTIBYTE (start) |
8f924df7 | 7736 | && SCHARS (start) != SBYTES (start)) |
df7492f9 KH |
7737 | return Qnil; |
7738 | start_byte = 0; | |
8f924df7 | 7739 | end_byte = SBYTES (start); |
df7492f9 | 7740 | pos = 0; |
d46c5b12 | 7741 | } |
05e6f5dc | 7742 | else |
b73bfc1c | 7743 | { |
b7826503 PJ |
7744 | CHECK_NUMBER_COERCE_MARKER (start); |
7745 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
7746 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
7747 | args_out_of_range (start, end); | |
7748 | if (NILP (current_buffer->enable_multibyte_characters)) | |
df7492f9 KH |
7749 | return Qnil; |
7750 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
7751 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
7752 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
05e6f5dc | 7753 | return Qt; |
df7492f9 | 7754 | |
e1c23804 | 7755 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 7756 | { |
e1c23804 DL |
7757 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
7758 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 7759 | else |
e1c23804 | 7760 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 7761 | } |
e1c23804 | 7762 | pos = XINT (start); |
b73bfc1c | 7763 | } |
7553d0e1 | 7764 | |
df7492f9 KH |
7765 | list = Qnil; |
7766 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 7767 | { |
df7492f9 | 7768 | elt = XCAR (tail); |
7d64c6ad | 7769 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
7770 | ASET (attrs, coding_attr_trans_tbl, |
7771 | get_translation_table (attrs, 1, NULL)); | |
7d64c6ad | 7772 | list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list); |
12410ef1 KH |
7773 | } |
7774 | ||
df7492f9 | 7775 | if (STRINGP (start)) |
8f924df7 | 7776 | p = pbeg = SDATA (start); |
72d1a715 | 7777 | else |
df7492f9 KH |
7778 | p = pbeg = BYTE_POS_ADDR (start_byte); |
7779 | pend = p + (end_byte - start_byte); | |
4ed46869 | 7780 | |
df7492f9 KH |
7781 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
7782 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 7783 | |
df7492f9 | 7784 | while (p < pend) |
d46c5b12 | 7785 | { |
df7492f9 KH |
7786 | if (ASCII_BYTE_P (*p)) |
7787 | p++; | |
e133c8fa | 7788 | else |
05e6f5dc | 7789 | { |
df7492f9 KH |
7790 | c = STRING_CHAR_ADVANCE (p); |
7791 | ||
7792 | charset_map_loaded = 0; | |
7793 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
7794 | { | |
7795 | elt = XCDR (XCAR (tail)); | |
7796 | if (! char_encodable_p (c, XCAR (elt))) | |
7797 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
7798 | } | |
7799 | if (charset_map_loaded) | |
7800 | { | |
7801 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
7802 | ||
7803 | if (STRINGP (start)) | |
8f924df7 | 7804 | pbeg = SDATA (start); |
df7492f9 KH |
7805 | else |
7806 | pbeg = BYTE_POS_ADDR (start_byte); | |
7807 | p = pbeg + p_offset; | |
7808 | pend = pbeg + pend_offset; | |
7809 | } | |
05e6f5dc | 7810 | } |
df7492f9 | 7811 | pos++; |
d46c5b12 | 7812 | } |
4ed46869 | 7813 | |
df7492f9 KH |
7814 | tail = list; |
7815 | list = Qnil; | |
7816 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 7817 | { |
df7492f9 KH |
7818 | elt = XCAR (tail); |
7819 | if (CONSP (XCDR (XCDR (elt)))) | |
7820 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
7821 | list); | |
ec6d2bb8 | 7822 | } |
2b4f9037 | 7823 | |
df7492f9 | 7824 | return list; |
d46c5b12 KH |
7825 | } |
7826 | ||
3fd9494b | 7827 | |
b73bfc1c | 7828 | Lisp_Object |
df7492f9 KH |
7829 | code_convert_region (start, end, coding_system, dst_object, encodep, norecord) |
7830 | Lisp_Object start, end, coding_system, dst_object; | |
7831 | int encodep, norecord; | |
4ed46869 | 7832 | { |
3a73fa5d | 7833 | struct coding_system coding; |
df7492f9 KH |
7834 | EMACS_INT from, from_byte, to, to_byte; |
7835 | Lisp_Object src_object; | |
4ed46869 | 7836 | |
b7826503 PJ |
7837 | CHECK_NUMBER_COERCE_MARKER (start); |
7838 | CHECK_NUMBER_COERCE_MARKER (end); | |
df7492f9 KH |
7839 | if (NILP (coding_system)) |
7840 | coding_system = Qno_conversion; | |
7841 | else | |
7842 | CHECK_CODING_SYSTEM (coding_system); | |
7843 | src_object = Fcurrent_buffer (); | |
7844 | if (NILP (dst_object)) | |
7845 | dst_object = src_object; | |
7846 | else if (! EQ (dst_object, Qt)) | |
7847 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 7848 | |
d46c5b12 KH |
7849 | validate_region (&start, &end); |
7850 | from = XFASTINT (start); | |
df7492f9 | 7851 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 7852 | to = XFASTINT (end); |
df7492f9 | 7853 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 7854 | |
df7492f9 KH |
7855 | setup_coding_system (coding_system, &coding); |
7856 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 7857 | |
df7492f9 KH |
7858 | if (encodep) |
7859 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
7860 | dst_object); | |
7861 | else | |
7862 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
7863 | dst_object); | |
7864 | if (! norecord) | |
7865 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 7866 | |
df7492f9 KH |
7867 | return (BUFFERP (dst_object) |
7868 | ? make_number (coding.produced_char) | |
7869 | : coding.dst_object); | |
4031e2bf | 7870 | } |
78108bcd | 7871 | |
4ed46869 | 7872 | |
4031e2bf | 7873 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 7874 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 7875 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
7876 | When called from a program, takes four arguments: |
7877 | START, END, CODING-SYSTEM, and DESTINATION. | |
7878 | START and END are buffer positions. | |
8844fa83 | 7879 | |
df7492f9 KH |
7880 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
7881 | If nil, the region between START and END is replace by the decoded text. | |
7882 | If buffer, the decoded text is inserted in the buffer. | |
7883 | If t, the decoded text is returned. | |
8844fa83 | 7884 | |
48b0f3ae PJ |
7885 | This function sets `last-coding-system-used' to the precise coding system |
7886 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
7887 | not fully specified.) | |
7888 | It returns the length of the decoded text. */) | |
df7492f9 KH |
7889 | (start, end, coding_system, destination) |
7890 | Lisp_Object start, end, coding_system, destination; | |
4031e2bf | 7891 | { |
df7492f9 | 7892 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 7893 | } |
8844fa83 | 7894 | |
3a73fa5d | 7895 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
7896 | 3, 4, "r\nzCoding system: ", |
7897 | doc: /* Encode the current region by specified coding system. | |
48b0f3ae PJ |
7898 | When called from a program, takes three arguments: |
7899 | START, END, and CODING-SYSTEM. START and END are buffer positions. | |
d46c5b12 | 7900 | |
df7492f9 KH |
7901 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
7902 | If nil, the region between START and END is replace by the encoded text. | |
7903 | If buffer, the encoded text is inserted in the buffer. | |
7904 | If t, the encoded text is returned. | |
2391eaa4 | 7905 | |
48b0f3ae PJ |
7906 | This function sets `last-coding-system-used' to the precise coding system |
7907 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
7908 | not fully specified.) | |
7909 | It returns the length of the encoded text. */) | |
df7492f9 KH |
7910 | (start, end, coding_system, destination) |
7911 | Lisp_Object start, end, coding_system, destination; | |
3a73fa5d | 7912 | { |
df7492f9 | 7913 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
7914 | } |
7915 | ||
7916 | Lisp_Object | |
df7492f9 KH |
7917 | code_convert_string (string, coding_system, dst_object, |
7918 | encodep, nocopy, norecord) | |
7919 | Lisp_Object string, coding_system, dst_object; | |
7920 | int encodep, nocopy, norecord; | |
b73bfc1c | 7921 | { |
4031e2bf | 7922 | struct coding_system coding; |
df7492f9 | 7923 | EMACS_INT chars, bytes; |
ec6d2bb8 | 7924 | |
b7826503 | 7925 | CHECK_STRING (string); |
d46c5b12 | 7926 | if (NILP (coding_system)) |
4956c225 | 7927 | { |
df7492f9 KH |
7928 | if (! norecord) |
7929 | Vlast_coding_system_used = Qno_conversion; | |
7930 | if (NILP (dst_object)) | |
7931 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 7932 | } |
b73bfc1c | 7933 | |
df7492f9 KH |
7934 | if (NILP (coding_system)) |
7935 | coding_system = Qno_conversion; | |
7936 | else | |
7937 | CHECK_CODING_SYSTEM (coding_system); | |
7938 | if (NILP (dst_object)) | |
7939 | dst_object = Qt; | |
7940 | else if (! EQ (dst_object, Qt)) | |
7941 | CHECK_BUFFER (dst_object); | |
73be902c | 7942 | |
df7492f9 | 7943 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 7944 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
7945 | chars = SCHARS (string); |
7946 | bytes = SBYTES (string); | |
df7492f9 KH |
7947 | if (encodep) |
7948 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
7949 | else | |
7950 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
7951 | if (! norecord) | |
7952 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 7953 | |
df7492f9 KH |
7954 | return (BUFFERP (dst_object) |
7955 | ? make_number (coding.produced_char) | |
7956 | : coding.dst_object); | |
4ed46869 | 7957 | } |
73be902c | 7958 | |
b73bfc1c | 7959 | |
ecec61c1 | 7960 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 7961 | Do not set Vlast_coding_system_used. |
4ed46869 | 7962 | |
ec6d2bb8 KH |
7963 | This function is called only from macros DECODE_FILE and |
7964 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 7965 | |
ecec61c1 KH |
7966 | Lisp_Object |
7967 | code_convert_string_norecord (string, coding_system, encodep) | |
7968 | Lisp_Object string, coding_system; | |
7969 | int encodep; | |
4ed46869 | 7970 | { |
0be8721c | 7971 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
7972 | } |
7973 | ||
4ed46869 | 7974 | |
df7492f9 KH |
7975 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
7976 | 2, 4, 0, | |
7977 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
7978 | ||
7979 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
7980 | if the decoding operation is trivial. | |
ecec61c1 | 7981 | |
df7492f9 | 7982 | Optional fourth arg BUFFER non-nil meant that the decoded text is |
a3f6ee6d | 7983 | inserted in BUFFER instead of returned as a string. In this case, |
df7492f9 | 7984 | the return value is BUFFER. |
ecec61c1 | 7985 | |
df7492f9 KH |
7986 | This function sets `last-coding-system-used' to the precise coding system |
7987 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
7988 | not fully specified. */) | |
7989 | (string, coding_system, nocopy, buffer) | |
7990 | Lisp_Object string, coding_system, nocopy, buffer; | |
4ed46869 | 7991 | { |
df7492f9 KH |
7992 | return code_convert_string (string, coding_system, buffer, |
7993 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
7994 | } |
7995 | ||
df7492f9 KH |
7996 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
7997 | 2, 4, 0, | |
7998 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
7999 | ||
8000 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
8001 | itself if the encoding operation is trivial. | |
8002 | ||
8003 | Optional fourth arg BUFFER non-nil meant that the encoded text is | |
a3f6ee6d | 8004 | inserted in BUFFER instead of returned as a string. In this case, |
df7492f9 KH |
8005 | the return value is BUFFER. |
8006 | ||
8007 | This function sets `last-coding-system-used' to the precise coding system | |
8008 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
8009 | not fully specified.) */) | |
8010 | (string, coding_system, nocopy, buffer) | |
8011 | Lisp_Object string, coding_system, nocopy, buffer; | |
4ed46869 | 8012 | { |
df7492f9 | 8013 | return code_convert_string (string, coding_system, buffer, |
c197f191 | 8014 | 1, ! NILP (nocopy), 1); |
4ed46869 | 8015 | } |
df7492f9 | 8016 | |
3a73fa5d | 8017 | \f |
4ed46869 | 8018 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
8019 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
8020 | Return the corresponding character. */) | |
8021 | (code) | |
4ed46869 | 8022 | Lisp_Object code; |
4ed46869 | 8023 | { |
df7492f9 KH |
8024 | Lisp_Object spec, attrs, val; |
8025 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
8026 | int c; | |
4ed46869 | 8027 | |
df7492f9 KH |
8028 | CHECK_NATNUM (code); |
8029 | c = XFASTINT (code); | |
8030 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
8031 | attrs = AREF (spec, 0); | |
4ed46869 | 8032 | |
df7492f9 KH |
8033 | if (ASCII_BYTE_P (c) |
8034 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8035 | return code; | |
4ed46869 | 8036 | |
df7492f9 KH |
8037 | val = CODING_ATTR_CHARSET_LIST (attrs); |
8038 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
8039 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
8040 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 8041 | |
df7492f9 KH |
8042 | if (c <= 0x7F) |
8043 | charset = charset_roman; | |
8044 | else if (c >= 0xA0 && c < 0xDF) | |
55ab7be3 | 8045 | { |
df7492f9 KH |
8046 | charset = charset_kana; |
8047 | c -= 0x80; | |
4ed46869 | 8048 | } |
55ab7be3 | 8049 | else |
4ed46869 | 8050 | { |
004068e4 | 8051 | int s1 = c >> 8, s2 = c & 0xFF; |
df7492f9 KH |
8052 | |
8053 | if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF | |
8054 | || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC) | |
8055 | error ("Invalid code: %d", code); | |
8056 | SJIS_TO_JIS (c); | |
8057 | charset = charset_kanji; | |
4ed46869 | 8058 | } |
df7492f9 KH |
8059 | c = DECODE_CHAR (charset, c); |
8060 | if (c < 0) | |
8061 | error ("Invalid code: %d", code); | |
8062 | return make_number (c); | |
93dec019 | 8063 | } |
4ed46869 | 8064 | |
48b0f3ae | 8065 | |
4ed46869 | 8066 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
8067 | doc: /* Encode a Japanese character CHAR to shift_jis encoding. |
8068 | Return the corresponding code in SJIS. */) | |
8069 | (ch) | |
df7492f9 | 8070 | Lisp_Object ch; |
4ed46869 | 8071 | { |
df7492f9 KH |
8072 | Lisp_Object spec, attrs, charset_list; |
8073 | int c; | |
8074 | struct charset *charset; | |
8075 | unsigned code; | |
48b0f3ae | 8076 | |
df7492f9 KH |
8077 | CHECK_CHARACTER (ch); |
8078 | c = XFASTINT (ch); | |
8079 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
8080 | attrs = AREF (spec, 0); | |
8081 | ||
8082 | if (ASCII_CHAR_P (c) | |
8083 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8084 | return ch; | |
8085 | ||
8086 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
8087 | charset = char_charset (c, charset_list, &code); | |
8088 | if (code == CHARSET_INVALID_CODE (charset)) | |
8089 | error ("Can't encode by shift_jis encoding: %d", c); | |
8090 | JIS_TO_SJIS (code); | |
8091 | ||
8092 | return make_number (code); | |
4ed46869 KH |
8093 | } |
8094 | ||
8095 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
8096 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
8097 | Return the corresponding character. */) | |
8098 | (code) | |
4ed46869 | 8099 | Lisp_Object code; |
d46c5b12 | 8100 | { |
df7492f9 KH |
8101 | Lisp_Object spec, attrs, val; |
8102 | struct charset *charset_roman, *charset_big5, *charset; | |
8103 | int c; | |
6289dd10 | 8104 | |
df7492f9 KH |
8105 | CHECK_NATNUM (code); |
8106 | c = XFASTINT (code); | |
8107 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
8108 | attrs = AREF (spec, 0); | |
4ed46869 | 8109 | |
df7492f9 KH |
8110 | if (ASCII_BYTE_P (c) |
8111 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8112 | return code; | |
6289dd10 | 8113 | |
df7492f9 KH |
8114 | val = CODING_ATTR_CHARSET_LIST (attrs); |
8115 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
8116 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 8117 | |
df7492f9 KH |
8118 | if (c <= 0x7F) |
8119 | charset = charset_roman; | |
c28a9453 KH |
8120 | else |
8121 | { | |
df7492f9 KH |
8122 | int b1 = c >> 8, b2 = c & 0x7F; |
8123 | if (b1 < 0xA1 || b1 > 0xFE | |
8124 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
8125 | error ("Invalid code: %d", code); | |
8126 | charset = charset_big5; | |
c28a9453 | 8127 | } |
df7492f9 KH |
8128 | c = DECODE_CHAR (charset, (unsigned )c); |
8129 | if (c < 0) | |
8130 | error ("Invalid code: %d", code); | |
8131 | return make_number (c); | |
d46c5b12 | 8132 | } |
6289dd10 | 8133 | |
4ed46869 | 8134 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
48b0f3ae PJ |
8135 | doc: /* Encode the Big5 character CHAR to BIG5 coding system. |
8136 | Return the corresponding character code in Big5. */) | |
8137 | (ch) | |
4ed46869 KH |
8138 | Lisp_Object ch; |
8139 | { | |
df7492f9 KH |
8140 | Lisp_Object spec, attrs, charset_list; |
8141 | struct charset *charset; | |
8142 | int c; | |
8143 | unsigned code; | |
8144 | ||
8145 | CHECK_CHARACTER (ch); | |
8146 | c = XFASTINT (ch); | |
8147 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
8148 | attrs = AREF (spec, 0); | |
8149 | if (ASCII_CHAR_P (c) | |
8150 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8151 | return ch; | |
8152 | ||
8153 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
8154 | charset = char_charset (c, charset_list, &code); | |
8155 | if (code == CHARSET_INVALID_CODE (charset)) | |
8156 | error ("Can't encode by Big5 encoding: %d", c); | |
8157 | ||
8158 | return make_number (code); | |
4ed46869 | 8159 | } |
48b0f3ae | 8160 | |
3a73fa5d | 8161 | \f |
1ba9e4ab KH |
8162 | DEFUN ("set-terminal-coding-system-internal", |
8163 | Fset_terminal_coding_system_internal, | |
48b0f3ae PJ |
8164 | Sset_terminal_coding_system_internal, 1, 1, 0, |
8165 | doc: /* Internal use only. */) | |
8166 | (coding_system) | |
b74e4686 | 8167 | Lisp_Object coding_system; |
4ed46869 | 8168 | { |
b7826503 | 8169 | CHECK_SYMBOL (coding_system); |
df7492f9 KH |
8170 | setup_coding_system (Fcheck_coding_system (coding_system), |
8171 | &terminal_coding); | |
48b0f3ae | 8172 | |
70c22245 | 8173 | /* We had better not send unsafe characters to terminal. */ |
df7492f9 KH |
8174 | terminal_coding.mode |= CODING_MODE_SAFE_ENCODING; |
8175 | /* Characer composition should be disabled. */ | |
8176 | terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
b73bfc1c KH |
8177 | terminal_coding.src_multibyte = 1; |
8178 | terminal_coding.dst_multibyte = 0; | |
4ed46869 KH |
8179 | return Qnil; |
8180 | } | |
8181 | ||
c4825358 KH |
8182 | DEFUN ("set-safe-terminal-coding-system-internal", |
8183 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 8184 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 8185 | doc: /* Internal use only. */) |
48b0f3ae | 8186 | (coding_system) |
b74e4686 | 8187 | Lisp_Object coding_system; |
d46c5b12 | 8188 | { |
b7826503 | 8189 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
8190 | setup_coding_system (Fcheck_coding_system (coding_system), |
8191 | &safe_terminal_coding); | |
df7492f9 KH |
8192 | /* Characer composition should be disabled. */ |
8193 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
b73bfc1c KH |
8194 | safe_terminal_coding.src_multibyte = 1; |
8195 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
8196 | return Qnil; |
8197 | } | |
4ed46869 | 8198 | |
4ed46869 KH |
8199 | DEFUN ("terminal-coding-system", |
8200 | Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0, | |
48b0f3ae PJ |
8201 | doc: /* Return coding system specified for terminal output. */) |
8202 | () | |
4ed46869 | 8203 | { |
ae6f73fa KH |
8204 | Lisp_Object coding_system; |
8205 | ||
8206 | coding_system = CODING_ID_NAME (terminal_coding.id); | |
8207 | /* For backward compatibility, return nil if it is `undecided'. */ | |
8208 | return (coding_system != Qundecided ? coding_system : Qnil); | |
4ed46869 KH |
8209 | } |
8210 | ||
1ba9e4ab KH |
8211 | DEFUN ("set-keyboard-coding-system-internal", |
8212 | Fset_keyboard_coding_system_internal, | |
48b0f3ae PJ |
8213 | Sset_keyboard_coding_system_internal, 1, 1, 0, |
8214 | doc: /* Internal use only. */) | |
8215 | (coding_system) | |
4ed46869 KH |
8216 | Lisp_Object coding_system; |
8217 | { | |
b7826503 | 8218 | CHECK_SYMBOL (coding_system); |
df7492f9 KH |
8219 | setup_coding_system (Fcheck_coding_system (coding_system), |
8220 | &keyboard_coding); | |
8221 | /* Characer composition should be disabled. */ | |
8222 | keyboard_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
8223 | return Qnil; |
8224 | } | |
8225 | ||
8226 | DEFUN ("keyboard-coding-system", | |
8227 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0, | |
48b0f3ae PJ |
8228 | doc: /* Return coding system specified for decoding keyboard input. */) |
8229 | () | |
4ed46869 | 8230 | { |
df7492f9 | 8231 | return CODING_ID_NAME (keyboard_coding.id); |
4ed46869 KH |
8232 | } |
8233 | ||
4ed46869 | 8234 | \f |
a5d301df KH |
8235 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
8236 | Sfind_operation_coding_system, 1, MANY, 0, | |
48b0f3ae PJ |
8237 | doc: /* Choose a coding system for an operation based on the target name. |
8238 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
8239 | DECODING-SYSTEM is the coding system to use for decoding | |
8240 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
8241 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 8242 | |
48b0f3ae PJ |
8243 | The first argument OPERATION specifies an I/O primitive: |
8244 | For file I/O, `insert-file-contents' or `write-region'. | |
8245 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
8246 | For network I/O, `open-network-stream'. | |
05e6f5dc | 8247 | |
48b0f3ae PJ |
8248 | The remaining arguments should be the same arguments that were passed |
8249 | to the primitive. Depending on which primitive, one of those arguments | |
8250 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
8251 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 8252 | |
48b0f3ae PJ |
8253 | TARGET has a meaning which depends on OPERATION: |
8254 | For file I/O, TARGET is a file name. | |
8255 | For process I/O, TARGET is a process name. | |
8256 | For network I/O, TARGET is a service name or a port number | |
05e6f5dc | 8257 | |
48b0f3ae PJ |
8258 | This function looks up what specified for TARGET in, |
8259 | `file-coding-system-alist', `process-coding-system-alist', | |
8260 | or `network-coding-system-alist' depending on OPERATION. | |
8261 | They may specify a coding system, a cons of coding systems, | |
8262 | or a function symbol to call. | |
8263 | In the last case, we call the function with one argument, | |
8264 | which is a list of all the arguments given to this function. | |
8265 | ||
8266 | usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */) | |
8267 | (nargs, args) | |
4ed46869 KH |
8268 | int nargs; |
8269 | Lisp_Object *args; | |
6b89e3aa | 8270 | { |
4ed46869 KH |
8271 | Lisp_Object operation, target_idx, target, val; |
8272 | register Lisp_Object chain; | |
177c0ea7 | 8273 | |
4ed46869 KH |
8274 | if (nargs < 2) |
8275 | error ("Too few arguments"); | |
8276 | operation = args[0]; | |
8277 | if (!SYMBOLP (operation) | |
8278 | || !INTEGERP (target_idx = Fget (operation, Qtarget_idx))) | |
df7492f9 | 8279 | error ("Invalid first arguement"); |
4ed46869 KH |
8280 | if (nargs < 1 + XINT (target_idx)) |
8281 | error ("Too few arguments for operation: %s", | |
8f924df7 | 8282 | SDATA (SYMBOL_NAME (operation))); |
4ed46869 KH |
8283 | target = args[XINT (target_idx) + 1]; |
8284 | if (!(STRINGP (target) | |
8285 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) | |
df7492f9 | 8286 | error ("Invalid %dth argument", XINT (target_idx) + 1); |
4ed46869 | 8287 | |
2e34157c RS |
8288 | chain = ((EQ (operation, Qinsert_file_contents) |
8289 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 8290 | ? Vfile_coding_system_alist |
2e34157c | 8291 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
8292 | ? Vnetwork_coding_system_alist |
8293 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
8294 | if (NILP (chain)) |
8295 | return Qnil; | |
8296 | ||
03699b14 | 8297 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 8298 | { |
f44d27ce | 8299 | Lisp_Object elt; |
6b89e3aa | 8300 | |
df7492f9 | 8301 | elt = XCAR (chain); |
4ed46869 KH |
8302 | if (CONSP (elt) |
8303 | && ((STRINGP (target) | |
03699b14 KR |
8304 | && STRINGP (XCAR (elt)) |
8305 | && fast_string_match (XCAR (elt), target) >= 0) | |
8306 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 8307 | { |
03699b14 | 8308 | val = XCDR (elt); |
b19fd4c5 KH |
8309 | /* Here, if VAL is both a valid coding system and a valid |
8310 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
8311 | if (CONSP (val)) |
8312 | return val; | |
8313 | if (! SYMBOLP (val)) | |
8314 | return Qnil; | |
8315 | if (! NILP (Fcoding_system_p (val))) | |
8316 | return Fcons (val, val); | |
b19fd4c5 | 8317 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 8318 | { |
b19fd4c5 KH |
8319 | val = call1 (val, Flist (nargs, args)); |
8320 | if (CONSP (val)) | |
8321 | return val; | |
8322 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
8323 | return Fcons (val, val); | |
6b89e3aa | 8324 | } |
02ba4723 | 8325 | return Qnil; |
6b89e3aa KH |
8326 | } |
8327 | } | |
4ed46869 | 8328 | return Qnil; |
6b89e3aa KH |
8329 | } |
8330 | ||
df7492f9 | 8331 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 8332 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 8333 | doc: /* Assign higher priority to the coding systems given as arguments. |
ff563fce | 8334 | If multiple coding systems belongs to the same category, |
a3181084 DL |
8335 | all but the first one are ignored. |
8336 | ||
8337 | usage: (set-coding-system-priority ...) */) | |
df7492f9 KH |
8338 | (nargs, args) |
8339 | int nargs; | |
8340 | Lisp_Object *args; | |
8341 | { | |
8342 | int i, j; | |
8343 | int changed[coding_category_max]; | |
8344 | enum coding_category priorities[coding_category_max]; | |
8345 | ||
8346 | bzero (changed, sizeof changed); | |
6b89e3aa | 8347 | |
df7492f9 | 8348 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 8349 | { |
df7492f9 KH |
8350 | enum coding_category category; |
8351 | Lisp_Object spec, attrs; | |
6b89e3aa | 8352 | |
df7492f9 KH |
8353 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
8354 | attrs = AREF (spec, 0); | |
8355 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8356 | if (changed[category]) | |
8357 | /* Ignore this coding system because a coding system of the | |
8358 | same category already had a higher priority. */ | |
8359 | continue; | |
8360 | changed[category] = 1; | |
8361 | priorities[j++] = category; | |
8362 | if (coding_categories[category].id >= 0 | |
8363 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
8364 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 8365 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 8366 | } |
6b89e3aa | 8367 | |
df7492f9 KH |
8368 | /* Now we have decided top J priorities. Reflect the order of the |
8369 | original priorities to the remaining priorities. */ | |
6b89e3aa | 8370 | |
df7492f9 | 8371 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 8372 | { |
df7492f9 KH |
8373 | while (j < coding_category_max |
8374 | && changed[coding_priorities[j]]) | |
8375 | j++; | |
8376 | if (j == coding_category_max) | |
8377 | abort (); | |
8378 | priorities[i] = coding_priorities[j]; | |
8379 | } | |
6b89e3aa | 8380 | |
df7492f9 | 8381 | bcopy (priorities, coding_priorities, sizeof priorities); |
177c0ea7 | 8382 | |
ff563fce KH |
8383 | /* Update `coding-category-list'. */ |
8384 | Vcoding_category_list = Qnil; | |
8385 | for (i = coding_category_max - 1; i >= 0; i--) | |
8386 | Vcoding_category_list | |
8387 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
8388 | Vcoding_category_list); | |
6b89e3aa | 8389 | |
df7492f9 | 8390 | return Qnil; |
6b89e3aa KH |
8391 | } |
8392 | ||
df7492f9 KH |
8393 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
8394 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 DL |
8395 | doc: /* Return a list of coding systems ordered by their priorities. |
8396 | HIGHESTP non-nil means just return the highest priority one. */) | |
df7492f9 KH |
8397 | (highestp) |
8398 | Lisp_Object highestp; | |
d46c5b12 KH |
8399 | { |
8400 | int i; | |
df7492f9 | 8401 | Lisp_Object val; |
6b89e3aa | 8402 | |
df7492f9 | 8403 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 8404 | { |
df7492f9 KH |
8405 | enum coding_category category = coding_priorities[i]; |
8406 | int id = coding_categories[category].id; | |
8407 | Lisp_Object attrs; | |
068a9dbd | 8408 | |
df7492f9 KH |
8409 | if (id < 0) |
8410 | continue; | |
8411 | attrs = CODING_ID_ATTRS (id); | |
8412 | if (! NILP (highestp)) | |
8413 | return CODING_ATTR_BASE_NAME (attrs); | |
8414 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
8415 | } | |
8416 | return Fnreverse (val); | |
8417 | } | |
068a9dbd | 8418 | |
f0064e1f | 8419 | static char *suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
8420 | |
8421 | static Lisp_Object | |
df7492f9 KH |
8422 | make_subsidiaries (base) |
8423 | Lisp_Object base; | |
068a9dbd | 8424 | { |
df7492f9 | 8425 | Lisp_Object subsidiaries; |
8f924df7 | 8426 | int base_name_len = SBYTES (SYMBOL_NAME (base)); |
df7492f9 KH |
8427 | char *buf = (char *) alloca (base_name_len + 6); |
8428 | int i; | |
068a9dbd | 8429 | |
8f924df7 | 8430 | bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len); |
df7492f9 KH |
8431 | subsidiaries = Fmake_vector (make_number (3), Qnil); |
8432 | for (i = 0; i < 3; i++) | |
068a9dbd | 8433 | { |
df7492f9 KH |
8434 | bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1); |
8435 | ASET (subsidiaries, i, intern (buf)); | |
068a9dbd | 8436 | } |
df7492f9 | 8437 | return subsidiaries; |
068a9dbd KH |
8438 | } |
8439 | ||
8440 | ||
df7492f9 KH |
8441 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
8442 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
8443 | doc: /* For internal use only. |
8444 | usage: (define-coding-system-internal ...) */) | |
df7492f9 KH |
8445 | (nargs, args) |
8446 | int nargs; | |
8447 | Lisp_Object *args; | |
068a9dbd | 8448 | { |
df7492f9 KH |
8449 | Lisp_Object name; |
8450 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
8451 | Lisp_Object attrs; /* Vector of attributes. */ | |
8452 | Lisp_Object eol_type; | |
8453 | Lisp_Object aliases; | |
8454 | Lisp_Object coding_type, charset_list, safe_charsets; | |
8455 | enum coding_category category; | |
8456 | Lisp_Object tail, val; | |
8457 | int max_charset_id = 0; | |
8458 | int i; | |
068a9dbd | 8459 | |
df7492f9 KH |
8460 | if (nargs < coding_arg_max) |
8461 | goto short_args; | |
068a9dbd | 8462 | |
df7492f9 | 8463 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 8464 | |
df7492f9 KH |
8465 | name = args[coding_arg_name]; |
8466 | CHECK_SYMBOL (name); | |
8467 | CODING_ATTR_BASE_NAME (attrs) = name; | |
068a9dbd | 8468 | |
df7492f9 KH |
8469 | val = args[coding_arg_mnemonic]; |
8470 | if (! STRINGP (val)) | |
8471 | CHECK_CHARACTER (val); | |
8472 | CODING_ATTR_MNEMONIC (attrs) = val; | |
068a9dbd | 8473 | |
df7492f9 KH |
8474 | coding_type = args[coding_arg_coding_type]; |
8475 | CHECK_SYMBOL (coding_type); | |
8476 | CODING_ATTR_TYPE (attrs) = coding_type; | |
068a9dbd | 8477 | |
df7492f9 KH |
8478 | charset_list = args[coding_arg_charset_list]; |
8479 | if (SYMBOLP (charset_list)) | |
8480 | { | |
8481 | if (EQ (charset_list, Qiso_2022)) | |
8482 | { | |
8483 | if (! EQ (coding_type, Qiso_2022)) | |
8484 | error ("Invalid charset-list"); | |
8485 | charset_list = Viso_2022_charset_list; | |
8486 | } | |
8487 | else if (EQ (charset_list, Qemacs_mule)) | |
8488 | { | |
8489 | if (! EQ (coding_type, Qemacs_mule)) | |
8490 | error ("Invalid charset-list"); | |
8491 | charset_list = Vemacs_mule_charset_list; | |
8492 | } | |
8493 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
8494 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
8495 | max_charset_id = XFASTINT (XCAR (tail)); | |
8496 | } | |
068a9dbd KH |
8497 | else |
8498 | { | |
df7492f9 KH |
8499 | charset_list = Fcopy_sequence (charset_list); |
8500 | for (tail = charset_list; !NILP (tail); tail = Fcdr (tail)) | |
068a9dbd | 8501 | { |
df7492f9 KH |
8502 | struct charset *charset; |
8503 | ||
8504 | val = Fcar (tail); | |
8505 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
8506 | if (EQ (coding_type, Qiso_2022) | |
8507 | ? CHARSET_ISO_FINAL (charset) < 0 | |
8508 | : EQ (coding_type, Qemacs_mule) | |
8509 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
8510 | : 0) | |
8511 | error ("Can't handle charset `%s'", | |
8f924df7 | 8512 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 8513 | |
8f924df7 | 8514 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
8515 | if (max_charset_id < charset->id) |
8516 | max_charset_id = charset->id; | |
068a9dbd KH |
8517 | } |
8518 | } | |
df7492f9 | 8519 | CODING_ATTR_CHARSET_LIST (attrs) = charset_list; |
068a9dbd | 8520 | |
df7492f9 KH |
8521 | safe_charsets = Fmake_string (make_number (max_charset_id + 1), |
8522 | make_number (255)); | |
8523 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
8f924df7 | 8524 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 8525 | CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets; |
068a9dbd | 8526 | |
584948ac | 8527 | CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p]; |
3a73fa5d | 8528 | |
df7492f9 | 8529 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 8530 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 8531 | CHECK_SYMBOL (val); |
df7492f9 | 8532 | CODING_ATTR_DECODE_TBL (attrs) = val; |
3a73fa5d | 8533 | |
df7492f9 | 8534 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 8535 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 8536 | CHECK_SYMBOL (val); |
df7492f9 | 8537 | CODING_ATTR_ENCODE_TBL (attrs) = val; |
d46c5b12 | 8538 | |
df7492f9 KH |
8539 | val = args[coding_arg_post_read_conversion]; |
8540 | CHECK_SYMBOL (val); | |
8541 | CODING_ATTR_POST_READ (attrs) = val; | |
d46c5b12 | 8542 | |
df7492f9 KH |
8543 | val = args[coding_arg_pre_write_conversion]; |
8544 | CHECK_SYMBOL (val); | |
8545 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
3a73fa5d | 8546 | |
df7492f9 KH |
8547 | val = args[coding_arg_default_char]; |
8548 | if (NILP (val)) | |
8549 | CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' '); | |
8550 | else | |
8551 | { | |
8f924df7 | 8552 | CHECK_CHARACTER (val); |
df7492f9 KH |
8553 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; |
8554 | } | |
4031e2bf | 8555 | |
8f924df7 KH |
8556 | val = args[coding_arg_for_unibyte]; |
8557 | CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt; | |
3a73fa5d | 8558 | |
df7492f9 KH |
8559 | val = args[coding_arg_plist]; |
8560 | CHECK_LIST (val); | |
8561 | CODING_ATTR_PLIST (attrs) = val; | |
3a73fa5d | 8562 | |
df7492f9 KH |
8563 | if (EQ (coding_type, Qcharset)) |
8564 | { | |
c7c66a95 KH |
8565 | /* Generate a lisp vector of 256 elements. Each element is nil, |
8566 | integer, or a list of charset IDs. | |
3a73fa5d | 8567 | |
c7c66a95 KH |
8568 | If Nth element is nil, the byte code N is invalid in this |
8569 | coding system. | |
4ed46869 | 8570 | |
c7c66a95 KH |
8571 | If Nth element is a number NUM, N is the first byte of a |
8572 | charset whose ID is NUM. | |
4ed46869 | 8573 | |
c7c66a95 KH |
8574 | If Nth element is a list of charset IDs, N is the first byte |
8575 | of one of them. The list is sorted by dimensions of the | |
2bc515e4 | 8576 | charsets. A charset of smaller dimension comes firtst. */ |
df7492f9 | 8577 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 8578 | |
5c99c2e6 | 8579 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 8580 | { |
c7c66a95 KH |
8581 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
8582 | int dim = CHARSET_DIMENSION (charset); | |
8583 | int idx = (dim - 1) * 4; | |
4ed46869 | 8584 | |
5c99c2e6 | 8585 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
584948ac | 8586 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
4031e2bf | 8587 | |
15d143f7 KH |
8588 | for (i = charset->code_space[idx]; |
8589 | i <= charset->code_space[idx + 1]; i++) | |
8590 | { | |
c7c66a95 KH |
8591 | Lisp_Object tmp, tmp2; |
8592 | int dim2; | |
ec6d2bb8 | 8593 | |
c7c66a95 KH |
8594 | tmp = AREF (val, i); |
8595 | if (NILP (tmp)) | |
8596 | tmp = XCAR (tail); | |
8597 | else if (NUMBERP (tmp)) | |
8598 | { | |
8599 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
8600 | if (dim < dim2) | |
c7c66a95 | 8601 | tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil)); |
f9d71dcd KH |
8602 | else |
8603 | tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil)); | |
c7c66a95 | 8604 | } |
15d143f7 | 8605 | else |
c7c66a95 KH |
8606 | { |
8607 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
8608 | { | |
8609 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
8610 | if (dim < dim2) | |
8611 | break; | |
8612 | } | |
8613 | if (NILP (tmp2)) | |
8614 | tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil)); | |
8615 | else | |
8616 | { | |
8617 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
8618 | XSETCAR (tmp2, XCAR (tail)); | |
8619 | } | |
8620 | } | |
8621 | ASET (val, i, tmp); | |
15d143f7 | 8622 | } |
df7492f9 KH |
8623 | } |
8624 | ASET (attrs, coding_attr_charset_valids, val); | |
8625 | category = coding_category_charset; | |
8626 | } | |
8627 | else if (EQ (coding_type, Qccl)) | |
8628 | { | |
8629 | Lisp_Object valids; | |
ecec61c1 | 8630 | |
df7492f9 KH |
8631 | if (nargs < coding_arg_ccl_max) |
8632 | goto short_args; | |
ecec61c1 | 8633 | |
df7492f9 KH |
8634 | val = args[coding_arg_ccl_decoder]; |
8635 | CHECK_CCL_PROGRAM (val); | |
8636 | if (VECTORP (val)) | |
8637 | val = Fcopy_sequence (val); | |
8638 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 8639 | |
df7492f9 KH |
8640 | val = args[coding_arg_ccl_encoder]; |
8641 | CHECK_CCL_PROGRAM (val); | |
8642 | if (VECTORP (val)) | |
8643 | val = Fcopy_sequence (val); | |
8644 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 8645 | |
df7492f9 KH |
8646 | val = args[coding_arg_ccl_valids]; |
8647 | valids = Fmake_string (make_number (256), make_number (0)); | |
8648 | for (tail = val; !NILP (tail); tail = Fcdr (tail)) | |
8649 | { | |
8dcbea82 | 8650 | int from, to; |
ecec61c1 | 8651 | |
df7492f9 KH |
8652 | val = Fcar (tail); |
8653 | if (INTEGERP (val)) | |
8dcbea82 KH |
8654 | { |
8655 | from = to = XINT (val); | |
8656 | if (from < 0 || from > 255) | |
8657 | args_out_of_range_3 (val, make_number (0), make_number (255)); | |
8658 | } | |
df7492f9 KH |
8659 | else |
8660 | { | |
df7492f9 | 8661 | CHECK_CONS (val); |
8f924df7 KH |
8662 | CHECK_NATNUM_CAR (val); |
8663 | CHECK_NATNUM_CDR (val); | |
df7492f9 | 8664 | from = XINT (XCAR (val)); |
8f924df7 | 8665 | if (from > 255) |
8dcbea82 KH |
8666 | args_out_of_range_3 (XCAR (val), |
8667 | make_number (0), make_number (255)); | |
df7492f9 | 8668 | to = XINT (XCDR (val)); |
8dcbea82 KH |
8669 | if (to < from || to > 255) |
8670 | args_out_of_range_3 (XCDR (val), | |
8671 | XCAR (val), make_number (255)); | |
df7492f9 | 8672 | } |
8dcbea82 | 8673 | for (i = from; i <= to; i++) |
8f924df7 | 8674 | SSET (valids, i, 1); |
df7492f9 KH |
8675 | } |
8676 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 8677 | |
df7492f9 | 8678 | category = coding_category_ccl; |
55ab7be3 | 8679 | } |
df7492f9 | 8680 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 8681 | { |
df7492f9 | 8682 | Lisp_Object bom, endian; |
4ed46869 | 8683 | |
584948ac | 8684 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; |
4ed46869 | 8685 | |
df7492f9 KH |
8686 | if (nargs < coding_arg_utf16_max) |
8687 | goto short_args; | |
4ed46869 | 8688 | |
df7492f9 KH |
8689 | bom = args[coding_arg_utf16_bom]; |
8690 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
8691 | { | |
8692 | CHECK_CONS (bom); | |
8f924df7 KH |
8693 | val = XCAR (bom); |
8694 | CHECK_CODING_SYSTEM (val); | |
8695 | val = XCDR (bom); | |
8696 | CHECK_CODING_SYSTEM (val); | |
df7492f9 KH |
8697 | } |
8698 | ASET (attrs, coding_attr_utf_16_bom, bom); | |
8699 | ||
8700 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
8701 | CHECK_SYMBOL (endian); |
8702 | if (NILP (endian)) | |
8703 | endian = Qbig; | |
8704 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 8705 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
8706 | ASET (attrs, coding_attr_utf_16_endian, endian); |
8707 | ||
8708 | category = (CONSP (bom) | |
8709 | ? coding_category_utf_16_auto | |
8710 | : NILP (bom) | |
b49a1807 | 8711 | ? (EQ (endian, Qbig) |
df7492f9 KH |
8712 | ? coding_category_utf_16_be_nosig |
8713 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 8714 | : (EQ (endian, Qbig) |
df7492f9 KH |
8715 | ? coding_category_utf_16_be |
8716 | : coding_category_utf_16_le)); | |
8717 | } | |
8718 | else if (EQ (coding_type, Qiso_2022)) | |
8719 | { | |
8720 | Lisp_Object initial, reg_usage, request, flags; | |
4776e638 | 8721 | int i; |
1397dc18 | 8722 | |
df7492f9 KH |
8723 | if (nargs < coding_arg_iso2022_max) |
8724 | goto short_args; | |
8725 | ||
8726 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
8727 | CHECK_VECTOR (initial); | |
8728 | for (i = 0; i < 4; i++) | |
8729 | { | |
8730 | val = Faref (initial, make_number (i)); | |
8731 | if (! NILP (val)) | |
8732 | { | |
584948ac KH |
8733 | struct charset *charset; |
8734 | ||
8735 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
8736 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
8737 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
8738 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
8739 | } |
8740 | else | |
8741 | ASET (initial, i, make_number (-1)); | |
8742 | } | |
8743 | ||
8744 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
8745 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
8746 | CHECK_NUMBER_CAR (reg_usage); |
8747 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
8748 | |
8749 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
8750 | for (tail = request; ! NILP (tail); tail = Fcdr (tail)) | |
1397dc18 | 8751 | { |
df7492f9 | 8752 | int id; |
8f924df7 | 8753 | Lisp_Object tmp; |
df7492f9 KH |
8754 | |
8755 | val = Fcar (tail); | |
8756 | CHECK_CONS (val); | |
8f924df7 KH |
8757 | tmp = XCAR (val); |
8758 | CHECK_CHARSET_GET_ID (tmp, id); | |
8759 | CHECK_NATNUM_CDR (val); | |
df7492f9 KH |
8760 | if (XINT (XCDR (val)) >= 4) |
8761 | error ("Invalid graphic register number: %d", XINT (XCDR (val))); | |
8f924df7 | 8762 | XSETCAR (val, make_number (id)); |
1397dc18 | 8763 | } |
4ed46869 | 8764 | |
df7492f9 KH |
8765 | flags = args[coding_arg_iso2022_flags]; |
8766 | CHECK_NATNUM (flags); | |
8767 | i = XINT (flags); | |
8768 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) | |
8769 | flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT); | |
8770 | ||
8771 | ASET (attrs, coding_attr_iso_initial, initial); | |
8772 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
8773 | ASET (attrs, coding_attr_iso_request, request); | |
8774 | ASET (attrs, coding_attr_iso_flags, flags); | |
8775 | setup_iso_safe_charsets (attrs); | |
8776 | ||
8777 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
8778 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
8779 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
8780 | ? coding_category_iso_7_else | |
8781 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
8782 | ? coding_category_iso_7 | |
8783 | : coding_category_iso_7_tight); | |
8784 | else | |
8785 | { | |
8786 | int id = XINT (AREF (initial, 1)); | |
8787 | ||
c6fb6e98 | 8788 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
8789 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
8790 | || id < 0) | |
8791 | ? coding_category_iso_8_else | |
8792 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
8793 | ? coding_category_iso_8_1 | |
8794 | : coding_category_iso_8_2); | |
8795 | } | |
0ce7886f KH |
8796 | if (category != coding_category_iso_8_1 |
8797 | && category != coding_category_iso_8_2) | |
8798 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; | |
df7492f9 KH |
8799 | } |
8800 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 8801 | { |
df7492f9 KH |
8802 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
8803 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
584948ac | 8804 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
df7492f9 | 8805 | category = coding_category_emacs_mule; |
c28a9453 | 8806 | } |
df7492f9 | 8807 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 8808 | { |
df7492f9 KH |
8809 | |
8810 | struct charset *charset; | |
8811 | ||
7d64c6ad | 8812 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 8813 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 8814 | error ("There should be three or four charsets"); |
df7492f9 KH |
8815 | |
8816 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8817 | if (CHARSET_DIMENSION (charset) != 1) | |
8818 | error ("Dimension of charset %s is not one", | |
8f924df7 | 8819 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
8820 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
8821 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
8822 | |
8823 | charset_list = XCDR (charset_list); | |
8824 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8825 | if (CHARSET_DIMENSION (charset) != 1) | |
8826 | error ("Dimension of charset %s is not one", | |
8f924df7 | 8827 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
8828 | |
8829 | charset_list = XCDR (charset_list); | |
8830 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8831 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
8832 | error ("Dimension of charset %s is not two", |
8833 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
8834 | ||
8835 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
8836 | if (! NILP (charset_list)) |
8837 | { | |
8838 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8839 | if (CHARSET_DIMENSION (charset) != 2) | |
8840 | error ("Dimension of charset %s is not two", | |
8841 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
8842 | } | |
df7492f9 KH |
8843 | |
8844 | category = coding_category_sjis; | |
8845 | Vsjis_coding_system = name; | |
c28a9453 | 8846 | } |
df7492f9 KH |
8847 | else if (EQ (coding_type, Qbig5)) |
8848 | { | |
8849 | struct charset *charset; | |
4ed46869 | 8850 | |
df7492f9 KH |
8851 | if (XINT (Flength (charset_list)) != 2) |
8852 | error ("There should be just two charsets"); | |
8853 | ||
8854 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8855 | if (CHARSET_DIMENSION (charset) != 1) | |
8856 | error ("Dimension of charset %s is not one", | |
8f924df7 | 8857 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
8858 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
8859 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
8860 | |
8861 | charset_list = XCDR (charset_list); | |
8862 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8863 | if (CHARSET_DIMENSION (charset) != 2) | |
8864 | error ("Dimension of charset %s is not two", | |
8f924df7 | 8865 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 8866 | |
df7492f9 KH |
8867 | category = coding_category_big5; |
8868 | Vbig5_coding_system = name; | |
8869 | } | |
8870 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 8871 | { |
584948ac KH |
8872 | category = coding_category_raw_text; |
8873 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
c28a9453 | 8874 | } |
df7492f9 | 8875 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 8876 | { |
584948ac KH |
8877 | category = coding_category_utf_8; |
8878 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
4ed46869 | 8879 | } |
df7492f9 KH |
8880 | else if (EQ (coding_type, Qundecided)) |
8881 | category = coding_category_undecided; | |
4ed46869 | 8882 | else |
df7492f9 | 8883 | error ("Invalid coding system type: %s", |
8f924df7 | 8884 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 8885 | |
df7492f9 | 8886 | CODING_ATTR_CATEGORY (attrs) = make_number (category); |
01378f49 KH |
8887 | CODING_ATTR_PLIST (attrs) |
8888 | = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category), | |
8889 | CODING_ATTR_PLIST (attrs))); | |
35befdaa KH |
8890 | CODING_ATTR_PLIST (attrs) |
8891 | = Fcons (QCascii_compatible_p, | |
8892 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), | |
8893 | CODING_ATTR_PLIST (attrs))); | |
c4825358 | 8894 | |
df7492f9 KH |
8895 | eol_type = args[coding_arg_eol_type]; |
8896 | if (! NILP (eol_type) | |
8897 | && ! EQ (eol_type, Qunix) | |
8898 | && ! EQ (eol_type, Qdos) | |
8899 | && ! EQ (eol_type, Qmac)) | |
8900 | error ("Invalid eol-type"); | |
4ed46869 | 8901 | |
df7492f9 | 8902 | aliases = Fcons (name, Qnil); |
4ed46869 | 8903 | |
df7492f9 KH |
8904 | if (NILP (eol_type)) |
8905 | { | |
8906 | eol_type = make_subsidiaries (name); | |
8907 | for (i = 0; i < 3; i++) | |
1397dc18 | 8908 | { |
df7492f9 KH |
8909 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
8910 | ||
8911 | this_name = AREF (eol_type, i); | |
8912 | this_aliases = Fcons (this_name, Qnil); | |
8913 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); | |
8914 | this_spec = Fmake_vector (make_number (3), attrs); | |
8915 | ASET (this_spec, 1, this_aliases); | |
8916 | ASET (this_spec, 2, this_eol_type); | |
8917 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
8918 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
8919 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
8920 | Vcoding_system_alist); | |
1397dc18 | 8921 | } |
d46c5b12 | 8922 | } |
4ed46869 | 8923 | |
df7492f9 KH |
8924 | spec_vec = Fmake_vector (make_number (3), attrs); |
8925 | ASET (spec_vec, 1, aliases); | |
8926 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 8927 | |
df7492f9 KH |
8928 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
8929 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
8930 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
8931 | Vcoding_system_alist); | |
48b0f3ae | 8932 | |
df7492f9 KH |
8933 | { |
8934 | int id = coding_categories[category].id; | |
48b0f3ae | 8935 | |
df7492f9 KH |
8936 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
8937 | setup_coding_system (name, &coding_categories[category]); | |
8938 | } | |
48b0f3ae | 8939 | |
d46c5b12 | 8940 | return Qnil; |
48b0f3ae | 8941 | |
df7492f9 KH |
8942 | short_args: |
8943 | return Fsignal (Qwrong_number_of_arguments, | |
8944 | Fcons (intern ("define-coding-system-internal"), | |
8945 | make_number (nargs))); | |
d46c5b12 | 8946 | } |
4ed46869 | 8947 | |
d6925f38 | 8948 | |
a6f87d34 KH |
8949 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
8950 | 3, 3, 0, | |
8951 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
8952 | (coding_system, prop, val) | |
8953 | Lisp_Object coding_system, prop, val; | |
8954 | { | |
3dbe7859 | 8955 | Lisp_Object spec, attrs; |
a6f87d34 KH |
8956 | |
8957 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
8958 | attrs = AREF (spec, 0); | |
8959 | if (EQ (prop, QCmnemonic)) | |
8960 | { | |
8961 | if (! STRINGP (val)) | |
8962 | CHECK_CHARACTER (val); | |
8963 | CODING_ATTR_MNEMONIC (attrs) = val; | |
8964 | } | |
8965 | else if (EQ (prop, QCdefalut_char)) | |
8966 | { | |
8967 | if (NILP (val)) | |
8968 | val = make_number (' '); | |
8969 | else | |
8970 | CHECK_CHARACTER (val); | |
8971 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; | |
8972 | } | |
8973 | else if (EQ (prop, QCdecode_translation_table)) | |
8974 | { | |
8975 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
8976 | CHECK_SYMBOL (val); | |
8977 | CODING_ATTR_DECODE_TBL (attrs) = val; | |
8978 | } | |
8979 | else if (EQ (prop, QCencode_translation_table)) | |
8980 | { | |
8981 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
8982 | CHECK_SYMBOL (val); | |
8983 | CODING_ATTR_ENCODE_TBL (attrs) = val; | |
8984 | } | |
8985 | else if (EQ (prop, QCpost_read_conversion)) | |
8986 | { | |
8987 | CHECK_SYMBOL (val); | |
8988 | CODING_ATTR_POST_READ (attrs) = val; | |
8989 | } | |
8990 | else if (EQ (prop, QCpre_write_conversion)) | |
8991 | { | |
8992 | CHECK_SYMBOL (val); | |
8993 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
8994 | } | |
35befdaa KH |
8995 | else if (EQ (prop, QCascii_compatible_p)) |
8996 | { | |
8997 | CODING_ATTR_ASCII_COMPAT (attrs) = val; | |
8998 | } | |
a6f87d34 KH |
8999 | |
9000 | CODING_ATTR_PLIST (attrs) | |
9001 | = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val); | |
9002 | return val; | |
9003 | } | |
9004 | ||
9005 | ||
df7492f9 KH |
9006 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
9007 | Sdefine_coding_system_alias, 2, 2, 0, | |
9008 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
9009 | (alias, coding_system) | |
9010 | Lisp_Object alias, coding_system; | |
66cfb530 | 9011 | { |
df7492f9 | 9012 | Lisp_Object spec, aliases, eol_type; |
4ed46869 | 9013 | |
df7492f9 KH |
9014 | CHECK_SYMBOL (alias); |
9015 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9016 | aliases = AREF (spec, 1); | |
d6925f38 KH |
9017 | /* ALISES should be a list of length more than zero, and the first |
9018 | element is a base coding system. Append ALIAS at the tail of the | |
9019 | list. */ | |
df7492f9 KH |
9020 | while (!NILP (XCDR (aliases))) |
9021 | aliases = XCDR (aliases); | |
8f924df7 | 9022 | XSETCDR (aliases, Fcons (alias, Qnil)); |
4ed46869 | 9023 | |
df7492f9 KH |
9024 | eol_type = AREF (spec, 2); |
9025 | if (VECTORP (eol_type)) | |
4ed46869 | 9026 | { |
df7492f9 KH |
9027 | Lisp_Object subsidiaries; |
9028 | int i; | |
4ed46869 | 9029 | |
df7492f9 KH |
9030 | subsidiaries = make_subsidiaries (alias); |
9031 | for (i = 0; i < 3; i++) | |
9032 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
9033 | AREF (eol_type, i)); | |
4ed46869 | 9034 | } |
df7492f9 KH |
9035 | |
9036 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 9037 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
5bad0796 DL |
9038 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), |
9039 | Vcoding_system_alist); | |
66cfb530 | 9040 | |
4ed46869 KH |
9041 | return Qnil; |
9042 | } | |
9043 | ||
df7492f9 KH |
9044 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
9045 | 1, 1, 0, | |
9046 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 9047 | Any alias or subsidiary coding system is not a base coding system. */) |
df7492f9 KH |
9048 | (coding_system) |
9049 | Lisp_Object coding_system; | |
d46c5b12 | 9050 | { |
df7492f9 | 9051 | Lisp_Object spec, attrs; |
d46c5b12 | 9052 | |
df7492f9 KH |
9053 | if (NILP (coding_system)) |
9054 | return (Qno_conversion); | |
9055 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9056 | attrs = AREF (spec, 0); | |
9057 | return CODING_ATTR_BASE_NAME (attrs); | |
9058 | } | |
1397dc18 | 9059 | |
df7492f9 KH |
9060 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
9061 | 1, 1, 0, | |
9062 | doc: "Return the property list of CODING-SYSTEM.") | |
9063 | (coding_system) | |
9064 | Lisp_Object coding_system; | |
9065 | { | |
9066 | Lisp_Object spec, attrs; | |
1397dc18 | 9067 | |
df7492f9 KH |
9068 | if (NILP (coding_system)) |
9069 | coding_system = Qno_conversion; | |
9070 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9071 | attrs = AREF (spec, 0); | |
9072 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
9073 | } |
9074 | ||
df7492f9 KH |
9075 | |
9076 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
9077 | 1, 1, 0, | |
da7db224 | 9078 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
df7492f9 KH |
9079 | (coding_system) |
9080 | Lisp_Object coding_system; | |
66cfb530 | 9081 | { |
df7492f9 | 9082 | Lisp_Object spec; |
84d60297 | 9083 | |
df7492f9 KH |
9084 | if (NILP (coding_system)) |
9085 | coding_system = Qno_conversion; | |
9086 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 9087 | return AREF (spec, 1); |
df7492f9 | 9088 | } |
66cfb530 | 9089 | |
df7492f9 KH |
9090 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
9091 | Scoding_system_eol_type, 1, 1, 0, | |
9092 | doc: /* Return eol-type of CODING-SYSTEM. | |
9093 | An eol-type is integer 0, 1, 2, or a vector of coding systems. | |
66cfb530 | 9094 | |
df7492f9 KH |
9095 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
9096 | and CR respectively. | |
66cfb530 | 9097 | |
df7492f9 KH |
9098 | A vector value indicates that a format of end-of-line should be |
9099 | detected automatically. Nth element of the vector is the subsidiary | |
9100 | coding system whose eol-type is N. */) | |
6b89e3aa KH |
9101 | (coding_system) |
9102 | Lisp_Object coding_system; | |
9103 | { | |
df7492f9 KH |
9104 | Lisp_Object spec, eol_type; |
9105 | int n; | |
6b89e3aa | 9106 | |
df7492f9 KH |
9107 | if (NILP (coding_system)) |
9108 | coding_system = Qno_conversion; | |
9109 | if (! CODING_SYSTEM_P (coding_system)) | |
9110 | return Qnil; | |
9111 | spec = CODING_SYSTEM_SPEC (coding_system); | |
9112 | eol_type = AREF (spec, 2); | |
9113 | if (VECTORP (eol_type)) | |
9114 | return Fcopy_sequence (eol_type); | |
9115 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
9116 | return make_number (n); | |
6b89e3aa KH |
9117 | } |
9118 | ||
4ed46869 KH |
9119 | #endif /* emacs */ |
9120 | ||
9121 | \f | |
1397dc18 | 9122 | /*** 9. Post-amble ***/ |
4ed46869 | 9123 | |
dfcf069d | 9124 | void |
4ed46869 KH |
9125 | init_coding_once () |
9126 | { | |
9127 | int i; | |
9128 | ||
df7492f9 KH |
9129 | for (i = 0; i < coding_category_max; i++) |
9130 | { | |
9131 | coding_categories[i].id = -1; | |
9132 | coding_priorities[i] = i; | |
9133 | } | |
4ed46869 KH |
9134 | |
9135 | /* ISO2022 specific initialize routine. */ | |
9136 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 9137 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
9138 | for (i = 0x21; i < 0x7F; i++) |
9139 | iso_code_class[i] = ISO_graphic_plane_0; | |
9140 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 9141 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
9142 | for (i = 0xA1; i < 0xFF; i++) |
9143 | iso_code_class[i] = ISO_graphic_plane_1; | |
9144 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
9145 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
9146 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
9147 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
9148 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
9149 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
9150 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
9151 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
9152 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
9153 | ||
df7492f9 KH |
9154 | for (i = 0; i < 256; i++) |
9155 | { | |
9156 | emacs_mule_bytes[i] = 1; | |
9157 | } | |
7c78e542 KH |
9158 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
9159 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
9160 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
9161 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
9162 | } |
9163 | ||
9164 | #ifdef emacs | |
9165 | ||
dfcf069d | 9166 | void |
e0e989f6 KH |
9167 | syms_of_coding () |
9168 | { | |
df7492f9 | 9169 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
9170 | { |
9171 | Lisp_Object args[2]; | |
9172 | args[0] = QCtest; | |
9173 | args[1] = Qeq; | |
9174 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
9175 | } | |
df7492f9 KH |
9176 | |
9177 | staticpro (&Vsjis_coding_system); | |
9178 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 9179 | |
df7492f9 KH |
9180 | staticpro (&Vbig5_coding_system); |
9181 | Vbig5_coding_system = Qnil; | |
9182 | ||
24a73b0a KH |
9183 | staticpro (&Vcode_conversion_reused_workbuf); |
9184 | Vcode_conversion_reused_workbuf = Qnil; | |
9185 | ||
9186 | staticpro (&Vcode_conversion_workbuf_name); | |
9187 | Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*"); | |
e0e989f6 | 9188 | |
24a73b0a | 9189 | reused_workbuf_in_use = 0; |
df7492f9 KH |
9190 | |
9191 | DEFSYM (Qcharset, "charset"); | |
9192 | DEFSYM (Qtarget_idx, "target-idx"); | |
9193 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
9194 | Fset (Qcoding_system_history, Qnil); |
9195 | ||
9ce27fde | 9196 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 9197 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 9198 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
9199 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
9200 | ||
df7492f9 | 9201 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 9202 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
9203 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
9204 | ||
df7492f9 | 9205 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 9206 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
9207 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
9208 | ||
df7492f9 | 9209 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 9210 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
9211 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
9212 | ||
df7492f9 | 9213 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 9214 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
9215 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
9216 | ||
df7492f9 KH |
9217 | DEFSYM (Qcoding_system, "coding-system"); |
9218 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 9219 | |
df7492f9 KH |
9220 | DEFSYM (Qeol_type, "eol-type"); |
9221 | DEFSYM (Qunix, "unix"); | |
9222 | DEFSYM (Qdos, "dos"); | |
4ed46869 | 9223 | |
df7492f9 KH |
9224 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
9225 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
9226 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
9227 | DEFSYM (Qdefault_char, "default-char"); | |
9228 | DEFSYM (Qundecided, "undecided"); | |
9229 | DEFSYM (Qno_conversion, "no-conversion"); | |
9230 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 9231 | |
df7492f9 | 9232 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 9233 | |
df7492f9 | 9234 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 9235 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 9236 | |
df7492f9 | 9237 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
9238 | DEFSYM (Qbig, "big"); |
9239 | DEFSYM (Qlittle, "little"); | |
27901516 | 9240 | |
df7492f9 KH |
9241 | DEFSYM (Qshift_jis, "shift-jis"); |
9242 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 9243 | |
df7492f9 | 9244 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 9245 | |
df7492f9 | 9246 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 KH |
9247 | Fput (Qcoding_system_error, Qerror_conditions, |
9248 | Fcons (Qcoding_system_error, Fcons (Qerror, Qnil))); | |
9249 | Fput (Qcoding_system_error, Qerror_message, | |
9ce27fde | 9250 | build_string ("Invalid coding system")); |
4ed46869 | 9251 | |
05e6f5dc KH |
9252 | /* Intern this now in case it isn't already done. |
9253 | Setting this variable twice is harmless. | |
9254 | But don't staticpro it here--that is done in alloc.c. */ | |
9255 | Qchar_table_extra_slots = intern ("char-table-extra-slots"); | |
70c22245 | 9256 | |
df7492f9 | 9257 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 9258 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
9259 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
9260 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
9261 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 9262 | |
df7492f9 | 9263 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 9264 | |
df7492f9 | 9265 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 9266 | |
01378f49 | 9267 | DEFSYM (QCcategory, ":category"); |
a6f87d34 KH |
9268 | DEFSYM (QCmnemonic, ":mnemonic"); |
9269 | DEFSYM (QCdefalut_char, ":default-char"); | |
9270 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); | |
9271 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
9272 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
9273 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 9274 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 9275 | |
df7492f9 KH |
9276 | Vcoding_category_table |
9277 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
9278 | staticpro (&Vcoding_category_table); | |
9279 | /* Followings are target of code detection. */ | |
9280 | ASET (Vcoding_category_table, coding_category_iso_7, | |
9281 | intern ("coding-category-iso-7")); | |
9282 | ASET (Vcoding_category_table, coding_category_iso_7_tight, | |
9283 | intern ("coding-category-iso-7-tight")); | |
9284 | ASET (Vcoding_category_table, coding_category_iso_8_1, | |
9285 | intern ("coding-category-iso-8-1")); | |
9286 | ASET (Vcoding_category_table, coding_category_iso_8_2, | |
9287 | intern ("coding-category-iso-8-2")); | |
9288 | ASET (Vcoding_category_table, coding_category_iso_7_else, | |
9289 | intern ("coding-category-iso-7-else")); | |
9290 | ASET (Vcoding_category_table, coding_category_iso_8_else, | |
9291 | intern ("coding-category-iso-8-else")); | |
9292 | ASET (Vcoding_category_table, coding_category_utf_8, | |
9293 | intern ("coding-category-utf-8")); | |
9294 | ASET (Vcoding_category_table, coding_category_utf_16_be, | |
9295 | intern ("coding-category-utf-16-be")); | |
ff563fce KH |
9296 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
9297 | intern ("coding-category-utf-16-auto")); | |
df7492f9 KH |
9298 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
9299 | intern ("coding-category-utf-16-le")); | |
9300 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, | |
9301 | intern ("coding-category-utf-16-be-nosig")); | |
9302 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, | |
9303 | intern ("coding-category-utf-16-le-nosig")); | |
9304 | ASET (Vcoding_category_table, coding_category_charset, | |
9305 | intern ("coding-category-charset")); | |
9306 | ASET (Vcoding_category_table, coding_category_sjis, | |
9307 | intern ("coding-category-sjis")); | |
9308 | ASET (Vcoding_category_table, coding_category_big5, | |
9309 | intern ("coding-category-big5")); | |
9310 | ASET (Vcoding_category_table, coding_category_ccl, | |
9311 | intern ("coding-category-ccl")); | |
9312 | ASET (Vcoding_category_table, coding_category_emacs_mule, | |
9313 | intern ("coding-category-emacs-mule")); | |
9314 | /* Followings are NOT target of code detection. */ | |
9315 | ASET (Vcoding_category_table, coding_category_raw_text, | |
9316 | intern ("coding-category-raw-text")); | |
9317 | ASET (Vcoding_category_table, coding_category_undecided, | |
9318 | intern ("coding-category-undecided")); | |
ecf488bc | 9319 | |
065e3595 KH |
9320 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
9321 | DEFSYM (Qinconsistent_eol, "inconsistent-eol"); | |
9322 | DEFSYM (Qinvalid_source, "invalid-source"); | |
9323 | DEFSYM (Qinterrupted, "interrupted"); | |
9324 | DEFSYM (Qinsufficient_memory, "insufficient-memory"); | |
9325 | ||
4ed46869 KH |
9326 | defsubr (&Scoding_system_p); |
9327 | defsubr (&Sread_coding_system); | |
9328 | defsubr (&Sread_non_nil_coding_system); | |
9329 | defsubr (&Scheck_coding_system); | |
9330 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 9331 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 9332 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 9333 | defsubr (&Sunencodable_char_position); |
df7492f9 | 9334 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
9335 | defsubr (&Sdecode_coding_region); |
9336 | defsubr (&Sencode_coding_region); | |
9337 | defsubr (&Sdecode_coding_string); | |
9338 | defsubr (&Sencode_coding_string); | |
9339 | defsubr (&Sdecode_sjis_char); | |
9340 | defsubr (&Sencode_sjis_char); | |
9341 | defsubr (&Sdecode_big5_char); | |
9342 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 9343 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 9344 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 9345 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 9346 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 9347 | defsubr (&Skeyboard_coding_system); |
a5d301df | 9348 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 9349 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 9350 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 9351 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 9352 | defsubr (&Scoding_system_put); |
df7492f9 KH |
9353 | defsubr (&Scoding_system_base); |
9354 | defsubr (&Scoding_system_plist); | |
9355 | defsubr (&Scoding_system_aliases); | |
9356 | defsubr (&Scoding_system_eol_type); | |
9357 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 9358 | |
4608c386 | 9359 | DEFVAR_LISP ("coding-system-list", &Vcoding_system_list, |
48b0f3ae PJ |
9360 | doc: /* List of coding systems. |
9361 | ||
9362 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 9363 | updated by the functions `define-coding-system' and |
48b0f3ae | 9364 | `define-coding-system-alias'. */); |
4608c386 KH |
9365 | Vcoding_system_list = Qnil; |
9366 | ||
9367 | DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist, | |
48b0f3ae PJ |
9368 | doc: /* Alist of coding system names. |
9369 | Each element is one element list of coding system name. | |
9370 | This variable is given to `completing-read' as TABLE argument. | |
9371 | ||
9372 | Do not alter the value of this variable manually. This variable should be | |
9373 | updated by the functions `make-coding-system' and | |
9374 | `define-coding-system-alias'. */); | |
4608c386 KH |
9375 | Vcoding_system_alist = Qnil; |
9376 | ||
4ed46869 | 9377 | DEFVAR_LISP ("coding-category-list", &Vcoding_category_list, |
48b0f3ae PJ |
9378 | doc: /* List of coding-categories (symbols) ordered by priority. |
9379 | ||
9380 | On detecting a coding system, Emacs tries code detection algorithms | |
9381 | associated with each coding-category one by one in this order. When | |
9382 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
9383 | system bound to the corresponding coding-category is selected. |
9384 | ||
42205607 | 9385 | Don't modify this variable directly, but use `set-coding-priority'. */); |
4ed46869 KH |
9386 | { |
9387 | int i; | |
9388 | ||
9389 | Vcoding_category_list = Qnil; | |
df7492f9 | 9390 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 9391 | Vcoding_category_list |
d46c5b12 KH |
9392 | = Fcons (XVECTOR (Vcoding_category_table)->contents[i], |
9393 | Vcoding_category_list); | |
4ed46869 KH |
9394 | } |
9395 | ||
9396 | DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read, | |
48b0f3ae PJ |
9397 | doc: /* Specify the coding system for read operations. |
9398 | It is useful to bind this variable with `let', but do not set it globally. | |
9399 | If the value is a coding system, it is used for decoding on read operation. | |
9400 | If not, an appropriate element is used from one of the coding system alists: | |
9401 | There are three such tables, `file-coding-system-alist', | |
9402 | `process-coding-system-alist', and `network-coding-system-alist'. */); | |
4ed46869 KH |
9403 | Vcoding_system_for_read = Qnil; |
9404 | ||
9405 | DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write, | |
48b0f3ae PJ |
9406 | doc: /* Specify the coding system for write operations. |
9407 | Programs bind this variable with `let', but you should not set it globally. | |
9408 | If the value is a coding system, it is used for encoding of output, | |
9409 | when writing it to a file and when sending it to a file or subprocess. | |
9410 | ||
9411 | If this does not specify a coding system, an appropriate element | |
9412 | is used from one of the coding system alists: | |
9413 | There are three such tables, `file-coding-system-alist', | |
9414 | `process-coding-system-alist', and `network-coding-system-alist'. | |
9415 | For output to files, if the above procedure does not specify a coding system, | |
9416 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
9417 | Vcoding_system_for_write = Qnil; |
9418 | ||
9419 | DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used, | |
df7492f9 KH |
9420 | doc: /* |
9421 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
9422 | Vlast_coding_system_used = Qnil; |
9423 | ||
065e3595 KH |
9424 | DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error, |
9425 | doc: /* | |
9426 | Error status of the last code conversion. | |
9427 | ||
9428 | When an error was detected in the last code conversion, this variable | |
9429 | is set to one of the following symbols. | |
9430 | `insufficient-source' | |
9431 | `inconsistent-eol' | |
9432 | `invalid-source' | |
9433 | `interrupted' | |
9434 | `insufficient-memory' | |
9435 | When no error was detected, the value doesn't change. So, to check | |
9436 | the error status of a code conversion by this variable, you must | |
9437 | explicitly set this variable to nil before performing code | |
9438 | conversion. */); | |
9439 | Vlast_code_conversion_error = Qnil; | |
9440 | ||
9ce27fde | 9441 | DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion, |
df7492f9 KH |
9442 | doc: /* |
9443 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
9444 | See info node `Coding Systems' and info node `Text and Binary' concerning |
9445 | such conversion. */); | |
9ce27fde KH |
9446 | inhibit_eol_conversion = 0; |
9447 | ||
ed29121d | 9448 | DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system, |
df7492f9 KH |
9449 | doc: /* |
9450 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
9451 | Bind it to t if the process output is to be treated as if it were a file |
9452 | read from some filesystem. */); | |
ed29121d EZ |
9453 | inherit_process_coding_system = 0; |
9454 | ||
02ba4723 | 9455 | DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist, |
df7492f9 KH |
9456 | doc: /* |
9457 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
9458 | The format is ((PATTERN . VAL) ...), |
9459 | where PATTERN is a regular expression matching a file name, | |
9460 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
9461 | If VAL is a coding system, it is used for both decoding and encoding | |
9462 | the file contents. | |
9463 | If VAL is a cons of coding systems, the car part is used for decoding, | |
9464 | and the cdr part is used for encoding. | |
9465 | If VAL is a function symbol, the function must return a coding system | |
0192762c DL |
9466 | or a cons of coding systems which are used as above. The function gets |
9467 | the arguments with which `find-operation-coding-systems' was called. | |
48b0f3ae PJ |
9468 | |
9469 | See also the function `find-operation-coding-system' | |
9470 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
9471 | Vfile_coding_system_alist = Qnil; |
9472 | ||
9473 | DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist, | |
df7492f9 KH |
9474 | doc: /* |
9475 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
9476 | The format is ((PATTERN . VAL) ...), |
9477 | where PATTERN is a regular expression matching a program name, | |
9478 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
9479 | If VAL is a coding system, it is used for both decoding what received | |
9480 | from the program and encoding what sent to the program. | |
9481 | If VAL is a cons of coding systems, the car part is used for decoding, | |
9482 | and the cdr part is used for encoding. | |
9483 | If VAL is a function symbol, the function must return a coding system | |
9484 | or a cons of coding systems which are used as above. | |
9485 | ||
9486 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
9487 | Vprocess_coding_system_alist = Qnil; |
9488 | ||
9489 | DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist, | |
df7492f9 KH |
9490 | doc: /* |
9491 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
9492 | The format is ((PATTERN . VAL) ...), |
9493 | where PATTERN is a regular expression matching a network service name | |
9494 | or is a port number to connect to, | |
9495 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
9496 | If VAL is a coding system, it is used for both decoding what received | |
9497 | from the network stream and encoding what sent to the network stream. | |
9498 | If VAL is a cons of coding systems, the car part is used for decoding, | |
9499 | and the cdr part is used for encoding. | |
9500 | If VAL is a function symbol, the function must return a coding system | |
9501 | or a cons of coding systems which are used as above. | |
9502 | ||
9503 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 9504 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 9505 | |
68c45bf0 | 9506 | DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system, |
75205970 RS |
9507 | doc: /* Coding system to use with system messages. |
9508 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
9509 | Vlocale_coding_system = Qnil; |
9510 | ||
005f0d35 | 9511 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
7722baf9 | 9512 | DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix, |
df7492f9 KH |
9513 | doc: /* |
9514 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
7722baf9 | 9515 | eol_mnemonic_unix = build_string (":"); |
4ed46869 | 9516 | |
7722baf9 | 9517 | DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos, |
df7492f9 KH |
9518 | doc: /* |
9519 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
7722baf9 | 9520 | eol_mnemonic_dos = build_string ("\\"); |
4ed46869 | 9521 | |
7722baf9 | 9522 | DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac, |
df7492f9 KH |
9523 | doc: /* |
9524 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
7722baf9 | 9525 | eol_mnemonic_mac = build_string ("/"); |
4ed46869 | 9526 | |
7722baf9 | 9527 | DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided, |
df7492f9 KH |
9528 | doc: /* |
9529 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
7722baf9 | 9530 | eol_mnemonic_undecided = build_string (":"); |
4ed46869 | 9531 | |
84fbb8a0 | 9532 | DEFVAR_LISP ("enable-character-translation", &Venable_character_translation, |
df7492f9 KH |
9533 | doc: /* |
9534 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 9535 | Venable_character_translation = Qt; |
bdd9fb48 | 9536 | |
f967223b | 9537 | DEFVAR_LISP ("standard-translation-table-for-decode", |
48b0f3ae PJ |
9538 | &Vstandard_translation_table_for_decode, |
9539 | doc: /* Table for translating characters while decoding. */); | |
f967223b | 9540 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 9541 | |
f967223b | 9542 | DEFVAR_LISP ("standard-translation-table-for-encode", |
48b0f3ae PJ |
9543 | &Vstandard_translation_table_for_encode, |
9544 | doc: /* Table for translating characters while encoding. */); | |
f967223b | 9545 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 9546 | |
df7492f9 | 9547 | DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table, |
48b0f3ae PJ |
9548 | doc: /* Alist of charsets vs revision numbers. |
9549 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
9550 | designate it with the escape sequence identifying revision (cdr part |
9551 | of the element). */); | |
9552 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
9553 | |
9554 | DEFVAR_LISP ("default-process-coding-system", | |
9555 | &Vdefault_process_coding_system, | |
48b0f3ae PJ |
9556 | doc: /* Cons of coding systems used for process I/O by default. |
9557 | The car part is used for decoding a process output, | |
9558 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 9559 | Vdefault_process_coding_system = Qnil; |
c4825358 | 9560 | |
3f003981 | 9561 | DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table, |
df7492f9 KH |
9562 | doc: /* |
9563 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
9564 | This is a vector of length 256. |
9565 | If Nth element is non-nil, the existence of code N in a file | |
9566 | \(or output of subprocess) doesn't prevent it to be detected as | |
9567 | a coding system of ISO 2022 variant which has a flag | |
9568 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
9569 | or reading output of a subprocess. | |
9570 | Only 128th through 159th elements has a meaning. */); | |
3f003981 | 9571 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
9572 | |
9573 | DEFVAR_LISP ("select-safe-coding-system-function", | |
9574 | &Vselect_safe_coding_system_function, | |
df7492f9 KH |
9575 | doc: /* |
9576 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
9577 | |
9578 | If set, this function is called to force a user to select a proper | |
9579 | coding system which can encode the text in the case that a default | |
9580 | coding system used in each operation can't encode the text. | |
9581 | ||
9582 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
9583 | Vselect_safe_coding_system_function = Qnil; |
9584 | ||
5d5bf4d8 KH |
9585 | DEFVAR_BOOL ("coding-system-require-warning", |
9586 | &coding_system_require_warning, | |
9587 | doc: /* Internal use only. | |
6b89e3aa KH |
9588 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
9589 | called even if `coding-system-for-write' is non-nil. The command | |
9590 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
9591 | coding_system_require_warning = 0; |
9592 | ||
9593 | ||
22ab2303 | 9594 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
74383408 | 9595 | &inhibit_iso_escape_detection, |
df7492f9 KH |
9596 | doc: /* |
9597 | If non-nil, Emacs ignores ISO2022's escape sequence on code detection. | |
48b0f3ae PJ |
9598 | |
9599 | By default, on reading a file, Emacs tries to detect how the text is | |
9600 | encoded. This code detection is sensitive to escape sequences. If | |
9601 | the sequence is valid as ISO2022, the code is determined as one of | |
9602 | the ISO2022 encodings, and the file is decoded by the corresponding | |
9603 | coding system (e.g. `iso-2022-7bit'). | |
9604 | ||
9605 | However, there may be a case that you want to read escape sequences in | |
9606 | a file as is. In such a case, you can set this variable to non-nil. | |
9607 | Then, as the code detection ignores any escape sequences, no file is | |
9608 | detected as encoded in some ISO2022 encoding. The result is that all | |
9609 | escape sequences become visible in a buffer. | |
9610 | ||
9611 | The default value is nil, and it is strongly recommended not to change | |
9612 | it. That is because many Emacs Lisp source files that contain | |
9613 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
9614 | in Emacs's distribution, and they won't be decoded correctly on | |
9615 | reading if you suppress escape sequence detection. | |
9616 | ||
9617 | The other way to read escape sequences in a file without decoding is | |
9618 | to explicitly specify some coding system that doesn't use ISO2022's | |
9619 | escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */); | |
74383408 | 9620 | inhibit_iso_escape_detection = 0; |
002fdb44 DL |
9621 | |
9622 | DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input, | |
15c8f9d1 DL |
9623 | doc: /* Char table for translating self-inserting characters. |
9624 | This is applied to the result of input methods, not their input. See also | |
9625 | `keyboard-translate-table'. */); | |
002fdb44 | 9626 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 9627 | |
2c78b7e1 KH |
9628 | { |
9629 | Lisp_Object args[coding_arg_max]; | |
8f924df7 | 9630 | Lisp_Object plist[16]; |
2c78b7e1 KH |
9631 | int i; |
9632 | ||
9633 | for (i = 0; i < coding_arg_max; i++) | |
9634 | args[i] = Qnil; | |
9635 | ||
9636 | plist[0] = intern (":name"); | |
9637 | plist[1] = args[coding_arg_name] = Qno_conversion; | |
9638 | plist[2] = intern (":mnemonic"); | |
9639 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); | |
9640 | plist[4] = intern (":coding-type"); | |
9641 | plist[5] = args[coding_arg_coding_type] = Qraw_text; | |
9642 | plist[6] = intern (":ascii-compatible-p"); | |
9643 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; | |
9644 | plist[8] = intern (":default-char"); | |
9645 | plist[9] = args[coding_arg_default_char] = make_number (0); | |
8f924df7 KH |
9646 | plist[10] = intern (":for-unibyte"); |
9647 | plist[11] = args[coding_arg_for_unibyte] = Qt; | |
9648 | plist[12] = intern (":docstring"); | |
9649 | plist[13] = build_string ("Do no conversion.\n\ | |
2c78b7e1 KH |
9650 | \n\ |
9651 | When you visit a file with this coding, the file is read into a\n\ | |
9652 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
9653 | character."); | |
8f924df7 KH |
9654 | plist[14] = intern (":eol-type"); |
9655 | plist[15] = args[coding_arg_eol_type] = Qunix; | |
9656 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 9657 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
9658 | |
9659 | plist[1] = args[coding_arg_name] = Qundecided; | |
9660 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
9661 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
9662 | /* This is already set. | |
35befdaa | 9663 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
ae6f73fa KH |
9664 | plist[8] = intern (":charset-list"); |
9665 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); | |
9666 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
9667 | plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding."); | |
9668 | plist[15] = args[coding_arg_eol_type] = Qnil; | |
9669 | args[coding_arg_plist] = Flist (16, plist); | |
9670 | Fdefine_coding_system_internal (coding_arg_max, args); | |
2c78b7e1 KH |
9671 | } |
9672 | ||
9673 | setup_coding_system (Qno_conversion, &keyboard_coding); | |
ae6f73fa | 9674 | setup_coding_system (Qundecided, &terminal_coding); |
2c78b7e1 | 9675 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
9676 | |
9677 | { | |
9678 | int i; | |
9679 | ||
9680 | for (i = 0; i < coding_category_max; i++) | |
9681 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
9682 | } | |
4ed46869 KH |
9683 | } |
9684 | ||
68c45bf0 PE |
9685 | char * |
9686 | emacs_strerror (error_number) | |
9687 | int error_number; | |
9688 | { | |
9689 | char *str; | |
9690 | ||
ca9c0567 | 9691 | synchronize_system_messages_locale (); |
68c45bf0 PE |
9692 | str = strerror (error_number); |
9693 | ||
9694 | if (! NILP (Vlocale_coding_system)) | |
9695 | { | |
9696 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
9697 | Vlocale_coding_system, | |
9698 | 0); | |
d5db4077 | 9699 | str = (char *) SDATA (dec); |
68c45bf0 PE |
9700 | } |
9701 | ||
9702 | return str; | |
9703 | } | |
9704 | ||
4ed46869 | 9705 | #endif /* emacs */ |
9ffd559c KH |
9706 | |
9707 | /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d | |
9708 | (do not change this comment) */ |