Commit | Line | Data |
---|---|---|
9542cb1f | 1 | /* Coding system handler (conversion, detection, etc). |
aaef169d | 2 | Copyright (C) 2001, 2002, 2003, 2004, 2005, |
4e6835db | 3 | 2006, 2007 Free Software Foundation, Inc. |
7976eda0 | 4 | Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
4e6835db | 5 | 2005, 2006, 2007 |
ce03bf76 KH |
6 | National Institute of Advanced Industrial Science and Technology (AIST) |
7 | Registration Number H14PRO021 | |
8f924df7 | 8 | Copyright (C) 2003 |
df7492f9 KH |
9 | National Institute of Advanced Industrial Science and Technology (AIST) |
10 | Registration Number H13PRO009 | |
4ed46869 | 11 | |
369314dc KH |
12 | This file is part of GNU Emacs. |
13 | ||
14 | GNU Emacs is free software; you can redistribute it and/or modify | |
15 | it under the terms of the GNU General Public License as published by | |
16 | the Free Software Foundation; either version 2, or (at your option) | |
17 | any later version. | |
4ed46869 | 18 | |
369314dc KH |
19 | GNU Emacs is distributed in the hope that it will be useful, |
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | GNU General Public License for more details. | |
4ed46869 | 23 | |
369314dc KH |
24 | You should have received a copy of the GNU General Public License |
25 | along with GNU Emacs; see the file COPYING. If not, write to | |
4fc5845f LK |
26 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
27 | Boston, MA 02110-1301, USA. */ | |
4ed46869 KH |
28 | |
29 | /*** TABLE OF CONTENTS *** | |
30 | ||
b73bfc1c | 31 | 0. General comments |
4ed46869 | 32 | 1. Preamble |
df7492f9 KH |
33 | 2. Emacs' internal format (emacs-utf-8) handlers |
34 | 3. UTF-8 handlers | |
35 | 4. UTF-16 handlers | |
36 | 5. Charset-base coding systems handlers | |
37 | 6. emacs-mule (old Emacs' internal format) handlers | |
38 | 7. ISO2022 handlers | |
39 | 8. Shift-JIS and BIG5 handlers | |
40 | 9. CCL handlers | |
41 | 10. C library functions | |
42 | 11. Emacs Lisp library functions | |
43 | 12. Postamble | |
4ed46869 KH |
44 | |
45 | */ | |
46 | ||
df7492f9 | 47 | /*** 0. General comments *** |
b73bfc1c KH |
48 | |
49 | ||
df7492f9 | 50 | CODING SYSTEM |
4ed46869 | 51 | |
5bad0796 DL |
52 | A coding system is an object for an encoding mechanism that contains |
53 | information about how to convert byte sequences to character | |
e19c3639 KH |
54 | sequences and vice versa. When we say "decode", it means converting |
55 | a byte sequence of a specific coding system into a character | |
56 | sequence that is represented by Emacs' internal coding system | |
57 | `emacs-utf-8', and when we say "encode", it means converting a | |
58 | character sequence of emacs-utf-8 to a byte sequence of a specific | |
0ef69138 | 59 | coding system. |
4ed46869 | 60 | |
e19c3639 KH |
61 | In Emacs Lisp, a coding system is represented by a Lisp symbol. In |
62 | C level, a coding system is represented by a vector of attributes | |
5bad0796 | 63 | stored in the hash table Vcharset_hash_table. The conversion from |
e19c3639 KH |
64 | coding system symbol to attributes vector is done by looking up |
65 | Vcharset_hash_table by the symbol. | |
4ed46869 | 66 | |
e19c3639 | 67 | Coding systems are classified into the following types depending on |
5bad0796 | 68 | the encoding mechanism. Here's a brief description of the types. |
4ed46869 | 69 | |
df7492f9 KH |
70 | o UTF-8 |
71 | ||
72 | o UTF-16 | |
73 | ||
74 | o Charset-base coding system | |
75 | ||
76 | A coding system defined by one or more (coded) character sets. | |
5bad0796 | 77 | Decoding and encoding are done by a code converter defined for each |
df7492f9 KH |
78 | character set. |
79 | ||
5bad0796 | 80 | o Old Emacs internal format (emacs-mule) |
df7492f9 | 81 | |
5bad0796 | 82 | The coding system adopted by old versions of Emacs (20 and 21). |
4ed46869 | 83 | |
df7492f9 | 84 | o ISO2022-base coding system |
4ed46869 KH |
85 | |
86 | The most famous coding system for multiple character sets. X's | |
df7492f9 KH |
87 | Compound Text, various EUCs (Extended Unix Code), and coding systems |
88 | used in the Internet communication such as ISO-2022-JP are all | |
89 | variants of ISO2022. | |
4ed46869 | 90 | |
df7492f9 | 91 | o SJIS (or Shift-JIS or MS-Kanji-Code) |
93dec019 | 92 | |
4ed46869 KH |
93 | A coding system to encode character sets: ASCII, JISX0201, and |
94 | JISX0208. Widely used for PC's in Japan. Details are described in | |
df7492f9 | 95 | section 8. |
4ed46869 | 96 | |
df7492f9 | 97 | o BIG5 |
4ed46869 | 98 | |
df7492f9 | 99 | A coding system to encode character sets: ASCII and Big5. Widely |
cfb43547 | 100 | used for Chinese (mainly in Taiwan and Hong Kong). Details are |
df7492f9 KH |
101 | described in section 8. In this file, when we write "big5" (all |
102 | lowercase), we mean the coding system, and when we write "Big5" | |
103 | (capitalized), we mean the character set. | |
4ed46869 | 104 | |
df7492f9 | 105 | o CCL |
27901516 | 106 | |
5bad0796 | 107 | If a user wants to decode/encode text encoded in a coding system |
df7492f9 KH |
108 | not listed above, he can supply a decoder and an encoder for it in |
109 | CCL (Code Conversion Language) programs. Emacs executes the CCL | |
110 | program while decoding/encoding. | |
27901516 | 111 | |
df7492f9 | 112 | o Raw-text |
4ed46869 | 113 | |
5a936b46 | 114 | A coding system for text containing raw eight-bit data. Emacs |
5bad0796 | 115 | treats each byte of source text as a character (except for |
df7492f9 | 116 | end-of-line conversion). |
4ed46869 | 117 | |
df7492f9 KH |
118 | o No-conversion |
119 | ||
120 | Like raw text, but don't do end-of-line conversion. | |
4ed46869 | 121 | |
4ed46869 | 122 | |
df7492f9 | 123 | END-OF-LINE FORMAT |
4ed46869 | 124 | |
5bad0796 | 125 | How text end-of-line is encoded depends on operating system. For |
df7492f9 | 126 | instance, Unix's format is just one byte of LF (line-feed) code, |
f4dee582 | 127 | whereas DOS's format is two-byte sequence of `carriage-return' and |
d46c5b12 KH |
128 | `line-feed' codes. MacOS's format is usually one byte of |
129 | `carriage-return'. | |
4ed46869 | 130 | |
cfb43547 | 131 | Since text character encoding and end-of-line encoding are |
df7492f9 KH |
132 | independent, any coding system described above can take any format |
133 | of end-of-line (except for no-conversion). | |
4ed46869 | 134 | |
e19c3639 KH |
135 | STRUCT CODING_SYSTEM |
136 | ||
137 | Before using a coding system for code conversion (i.e. decoding and | |
138 | encoding), we setup a structure of type `struct coding_system'. | |
139 | This structure keeps various information about a specific code | |
5bad0796 | 140 | conversion (e.g. the location of source and destination data). |
4ed46869 KH |
141 | |
142 | */ | |
143 | ||
df7492f9 KH |
144 | /* COMMON MACROS */ |
145 | ||
146 | ||
4ed46869 KH |
147 | /*** GENERAL NOTES on `detect_coding_XXX ()' functions *** |
148 | ||
df7492f9 | 149 | These functions check if a byte sequence specified as a source in |
ff0dacd7 KH |
150 | CODING conforms to the format of XXX, and update the members of |
151 | DETECT_INFO. | |
df7492f9 | 152 | |
ff0dacd7 | 153 | Return 1 if the byte sequence conforms to XXX, otherwise return 0. |
df7492f9 KH |
154 | |
155 | Below is the template of these functions. */ | |
156 | ||
4ed46869 | 157 | #if 0 |
df7492f9 | 158 | static int |
ff0dacd7 | 159 | detect_coding_XXX (coding, detect_info) |
df7492f9 | 160 | struct coding_system *coding; |
ff0dacd7 | 161 | struct coding_detection_info *detect_info; |
4ed46869 | 162 | { |
f1d34bca MB |
163 | const unsigned char *src = coding->source; |
164 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 165 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 166 | int consumed_chars = 0; |
df7492f9 KH |
167 | int found = 0; |
168 | ...; | |
169 | ||
170 | while (1) | |
171 | { | |
172 | /* Get one byte from the source. If the souce is exausted, jump | |
173 | to no_more_source:. */ | |
174 | ONE_MORE_BYTE (c); | |
ff0dacd7 KH |
175 | |
176 | if (! __C_conforms_to_XXX___ (c)) | |
177 | break; | |
178 | if (! __C_strongly_suggests_XXX__ (c)) | |
179 | found = CATEGORY_MASK_XXX; | |
df7492f9 | 180 | } |
ff0dacd7 KH |
181 | /* The byte sequence is invalid for XXX. */ |
182 | detect_info->rejected |= CATEGORY_MASK_XXX; | |
df7492f9 | 183 | return 0; |
ff0dacd7 | 184 | |
df7492f9 | 185 | no_more_source: |
ff0dacd7 KH |
186 | /* The source exausted successfully. */ |
187 | detect_info->found |= found; | |
df7492f9 | 188 | return 1; |
4ed46869 KH |
189 | } |
190 | #endif | |
191 | ||
192 | /*** GENERAL NOTES on `decode_coding_XXX ()' functions *** | |
193 | ||
df7492f9 KH |
194 | These functions decode a byte sequence specified as a source by |
195 | CODING. The resulting multibyte text goes to a place pointed to by | |
196 | CODING->charbuf, the length of which should not exceed | |
197 | CODING->charbuf_size; | |
d46c5b12 | 198 | |
df7492f9 KH |
199 | These functions set the information of original and decoded texts in |
200 | CODING->consumed, CODING->consumed_char, and CODING->charbuf_used. | |
201 | They also set CODING->result to one of CODING_RESULT_XXX indicating | |
202 | how the decoding is finished. | |
d46c5b12 | 203 | |
df7492f9 | 204 | Below is the template of these functions. */ |
d46c5b12 | 205 | |
4ed46869 | 206 | #if 0 |
b73bfc1c | 207 | static void |
df7492f9 | 208 | decode_coding_XXXX (coding) |
4ed46869 | 209 | struct coding_system *coding; |
4ed46869 | 210 | { |
f1d34bca MB |
211 | const unsigned char *src = coding->source + coding->consumed; |
212 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
213 | /* SRC_BASE remembers the start position in source in each loop. |
214 | The loop will be exited when there's not enough source code, or | |
215 | when there's no room in CHARBUF for a decoded character. */ | |
f1d34bca | 216 | const unsigned char *src_base; |
df7492f9 | 217 | /* A buffer to produce decoded characters. */ |
69a80ea3 KH |
218 | int *charbuf = coding->charbuf + coding->charbuf_used; |
219 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
220 | int multibytep = coding->src_multibyte; |
221 | ||
222 | while (1) | |
223 | { | |
224 | src_base = src; | |
225 | if (charbuf < charbuf_end) | |
226 | /* No more room to produce a decoded character. */ | |
227 | break; | |
228 | ONE_MORE_BYTE (c); | |
229 | /* Decode it. */ | |
230 | } | |
231 | ||
232 | no_more_source: | |
233 | if (src_base < src_end | |
234 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
235 | /* If the source ends by partial bytes to construct a character, | |
236 | treat them as eight-bit raw data. */ | |
237 | while (src_base < src_end && charbuf < charbuf_end) | |
238 | *charbuf++ = *src_base++; | |
239 | /* Remember how many bytes and characters we consumed. If the | |
240 | source is multibyte, the bytes and chars are not identical. */ | |
241 | coding->consumed = coding->consumed_char = src_base - coding->source; | |
242 | /* Remember how many characters we produced. */ | |
243 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
244 | } |
245 | #endif | |
246 | ||
247 | /*** GENERAL NOTES on `encode_coding_XXX ()' functions *** | |
248 | ||
df7492f9 KH |
249 | These functions encode SRC_BYTES length text at SOURCE of Emacs' |
250 | internal multibyte format by CODING. The resulting byte sequence | |
b73bfc1c KH |
251 | goes to a place pointed to by DESTINATION, the length of which |
252 | should not exceed DST_BYTES. | |
d46c5b12 | 253 | |
df7492f9 KH |
254 | These functions set the information of original and encoded texts in |
255 | the members produced, produced_char, consumed, and consumed_char of | |
256 | the structure *CODING. They also set the member result to one of | |
257 | CODING_RESULT_XXX indicating how the encoding finished. | |
d46c5b12 | 258 | |
df7492f9 KH |
259 | DST_BYTES zero means that source area and destination area are |
260 | overlapped, which means that we can produce a encoded text until it | |
261 | reaches at the head of not-yet-encoded source text. | |
d46c5b12 | 262 | |
df7492f9 | 263 | Below is a template of these functions. */ |
4ed46869 | 264 | #if 0 |
b73bfc1c | 265 | static void |
df7492f9 | 266 | encode_coding_XXX (coding) |
4ed46869 | 267 | struct coding_system *coding; |
4ed46869 | 268 | { |
df7492f9 KH |
269 | int multibytep = coding->dst_multibyte; |
270 | int *charbuf = coding->charbuf; | |
271 | int *charbuf_end = charbuf->charbuf + coding->charbuf_used; | |
272 | unsigned char *dst = coding->destination + coding->produced; | |
273 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
274 | unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_; | |
275 | int produced_chars = 0; | |
276 | ||
277 | for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++) | |
278 | { | |
279 | int c = *charbuf; | |
280 | /* Encode C into DST, and increment DST. */ | |
281 | } | |
282 | label_no_more_destination: | |
283 | /* How many chars and bytes we produced. */ | |
284 | coding->produced_char += produced_chars; | |
285 | coding->produced = dst - coding->destination; | |
4ed46869 KH |
286 | } |
287 | #endif | |
288 | ||
4ed46869 KH |
289 | \f |
290 | /*** 1. Preamble ***/ | |
291 | ||
68c45bf0 | 292 | #include <config.h> |
4ed46869 KH |
293 | #include <stdio.h> |
294 | ||
4ed46869 KH |
295 | #include "lisp.h" |
296 | #include "buffer.h" | |
df7492f9 | 297 | #include "character.h" |
4ed46869 KH |
298 | #include "charset.h" |
299 | #include "ccl.h" | |
df7492f9 | 300 | #include "composite.h" |
4ed46869 KH |
301 | #include "coding.h" |
302 | #include "window.h" | |
4ed46869 | 303 | |
df7492f9 | 304 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 305 | |
df7492f9 | 306 | Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type; |
1965cb73 DL |
307 | Lisp_Object Qunix, Qdos; |
308 | extern Lisp_Object Qmac; /* frame.c */ | |
4ed46869 KH |
309 | Lisp_Object Qbuffer_file_coding_system; |
310 | Lisp_Object Qpost_read_conversion, Qpre_write_conversion; | |
df7492f9 | 311 | Lisp_Object Qdefault_char; |
27901516 | 312 | Lisp_Object Qno_conversion, Qundecided; |
df7492f9 | 313 | Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5; |
b49a1807 | 314 | Lisp_Object Qbig, Qlittle; |
bb0115a2 | 315 | Lisp_Object Qcoding_system_history; |
1397dc18 | 316 | Lisp_Object Qvalid_codes; |
a6f87d34 KH |
317 | Lisp_Object QCcategory, QCmnemonic, QCdefalut_char; |
318 | Lisp_Object QCdecode_translation_table, QCencode_translation_table; | |
319 | Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
35befdaa | 320 | Lisp_Object QCascii_compatible_p; |
4ed46869 KH |
321 | |
322 | extern Lisp_Object Qinsert_file_contents, Qwrite_region; | |
387f6ba5 | 323 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 KH |
324 | Lisp_Object Qstart_process, Qopen_network_stream; |
325 | Lisp_Object Qtarget_idx; | |
326 | ||
065e3595 KH |
327 | Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source; |
328 | Lisp_Object Qinterrupted, Qinsufficient_memory; | |
329 | ||
44e8490d KH |
330 | /* If a symbol has this property, evaluate the value to define the |
331 | symbol as a coding system. */ | |
332 | static Lisp_Object Qcoding_system_define_form; | |
333 | ||
5d5bf4d8 KH |
334 | int coding_system_require_warning; |
335 | ||
d46c5b12 KH |
336 | Lisp_Object Vselect_safe_coding_system_function; |
337 | ||
7722baf9 EZ |
338 | /* Mnemonic string for each format of end-of-line. */ |
339 | Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac; | |
340 | /* Mnemonic string to indicate format of end-of-line is not yet | |
4ed46869 | 341 | decided. */ |
7722baf9 | 342 | Lisp_Object eol_mnemonic_undecided; |
4ed46869 | 343 | |
fcbcfb64 KH |
344 | /* Format of end-of-line decided by system. This is Qunix on |
345 | Unix and Mac, Qdos on DOS/Windows. | |
346 | This has an effect only for external encoding (i.e. for output to | |
347 | file and process), not for in-buffer or Lisp string encoding. */ | |
348 | static Lisp_Object system_eol_type; | |
349 | ||
4ed46869 KH |
350 | #ifdef emacs |
351 | ||
4608c386 KH |
352 | Lisp_Object Vcoding_system_list, Vcoding_system_alist; |
353 | ||
354 | Lisp_Object Qcoding_system_p, Qcoding_system_error; | |
4ed46869 | 355 | |
d46c5b12 KH |
356 | /* Coding system emacs-mule and raw-text are for converting only |
357 | end-of-line format. */ | |
358 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 359 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 360 | |
4ed46869 KH |
361 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
362 | routines by the following three variables. */ | |
363 | /* Coding-system for reading files and receiving data from process. */ | |
364 | Lisp_Object Vcoding_system_for_read; | |
365 | /* Coding-system for writing files and sending data to process. */ | |
366 | Lisp_Object Vcoding_system_for_write; | |
367 | /* Coding-system actually used in the latest I/O. */ | |
368 | Lisp_Object Vlast_coding_system_used; | |
065e3595 KH |
369 | /* Set to non-nil when an error is detected while code conversion. */ |
370 | Lisp_Object Vlast_code_conversion_error; | |
c4825358 | 371 | /* A vector of length 256 which contains information about special |
94487c4e | 372 | Latin codes (especially for dealing with Microsoft codes). */ |
3f003981 | 373 | Lisp_Object Vlatin_extra_code_table; |
c4825358 | 374 | |
9ce27fde KH |
375 | /* Flag to inhibit code conversion of end-of-line format. */ |
376 | int inhibit_eol_conversion; | |
377 | ||
74383408 KH |
378 | /* Flag to inhibit ISO2022 escape sequence detection. */ |
379 | int inhibit_iso_escape_detection; | |
380 | ||
ed29121d EZ |
381 | /* Flag to make buffer-file-coding-system inherit from process-coding. */ |
382 | int inherit_process_coding_system; | |
383 | ||
c4825358 | 384 | /* Coding system to be used to encode text for terminal display. */ |
4ed46869 KH |
385 | struct coding_system terminal_coding; |
386 | ||
c4825358 KH |
387 | /* Coding system to be used to encode text for terminal display when |
388 | terminal coding system is nil. */ | |
389 | struct coding_system safe_terminal_coding; | |
390 | ||
391 | /* Coding system of what is sent from terminal keyboard. */ | |
4ed46869 KH |
392 | struct coding_system keyboard_coding; |
393 | ||
02ba4723 KH |
394 | Lisp_Object Vfile_coding_system_alist; |
395 | Lisp_Object Vprocess_coding_system_alist; | |
396 | Lisp_Object Vnetwork_coding_system_alist; | |
4ed46869 | 397 | |
68c45bf0 PE |
398 | Lisp_Object Vlocale_coding_system; |
399 | ||
4ed46869 KH |
400 | #endif /* emacs */ |
401 | ||
f967223b KH |
402 | /* Flag to tell if we look up translation table on character code |
403 | conversion. */ | |
84fbb8a0 | 404 | Lisp_Object Venable_character_translation; |
f967223b KH |
405 | /* Standard translation table to look up on decoding (reading). */ |
406 | Lisp_Object Vstandard_translation_table_for_decode; | |
407 | /* Standard translation table to look up on encoding (writing). */ | |
408 | Lisp_Object Vstandard_translation_table_for_encode; | |
84fbb8a0 | 409 | |
f967223b KH |
410 | Lisp_Object Qtranslation_table; |
411 | Lisp_Object Qtranslation_table_id; | |
412 | Lisp_Object Qtranslation_table_for_decode; | |
413 | Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 KH |
414 | |
415 | /* Alist of charsets vs revision number. */ | |
df7492f9 | 416 | static Lisp_Object Vcharset_revision_table; |
4ed46869 | 417 | |
02ba4723 KH |
418 | /* Default coding systems used for process I/O. */ |
419 | Lisp_Object Vdefault_process_coding_system; | |
420 | ||
002fdb44 DL |
421 | /* Char table for translating Quail and self-inserting input. */ |
422 | Lisp_Object Vtranslation_table_for_input; | |
423 | ||
df7492f9 KH |
424 | /* Two special coding systems. */ |
425 | Lisp_Object Vsjis_coding_system; | |
426 | Lisp_Object Vbig5_coding_system; | |
427 | ||
df7492f9 KH |
428 | /* ISO2022 section */ |
429 | ||
430 | #define CODING_ISO_INITIAL(coding, reg) \ | |
431 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
432 | coding_attr_iso_initial), \ | |
433 | reg))) | |
434 | ||
435 | ||
436 | #define CODING_ISO_REQUEST(coding, charset_id) \ | |
437 | ((charset_id <= (coding)->max_charset_id \ | |
438 | ? (coding)->safe_charsets[charset_id] \ | |
439 | : -1)) | |
440 | ||
441 | ||
442 | #define CODING_ISO_FLAGS(coding) \ | |
443 | ((coding)->spec.iso_2022.flags) | |
444 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
445 | ((coding)->spec.iso_2022.current_designation[reg]) | |
446 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
447 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
448 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
449 | ((coding)->spec.iso_2022.single_shifting) | |
450 | #define CODING_ISO_BOL(coding) \ | |
451 | ((coding)->spec.iso_2022.bol) | |
452 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
453 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
454 | ||
455 | /* Control characters of ISO2022. */ | |
456 | /* code */ /* function */ | |
457 | #define ISO_CODE_LF 0x0A /* line-feed */ | |
458 | #define ISO_CODE_CR 0x0D /* carriage-return */ | |
459 | #define ISO_CODE_SO 0x0E /* shift-out */ | |
460 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
461 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
462 | #define ISO_CODE_ESC 0x1B /* escape */ | |
463 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
464 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
465 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
466 | ||
467 | /* All code (1-byte) of ISO2022 is classified into one of the | |
468 | followings. */ | |
469 | enum iso_code_class_type | |
470 | { | |
471 | ISO_control_0, /* Control codes in the range | |
472 | 0x00..0x1F and 0x7F, except for the | |
473 | following 5 codes. */ | |
df7492f9 KH |
474 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
475 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
476 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
477 | ISO_escape, /* ISO_CODE_SO (0x1B) */ | |
478 | ISO_control_1, /* Control codes in the range | |
479 | 0x80..0x9F, except for the | |
480 | following 3 codes. */ | |
481 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
482 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
483 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
484 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
485 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
486 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
487 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
488 | }; | |
05e6f5dc | 489 | |
df7492f9 KH |
490 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
491 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 492 | |
df7492f9 KH |
493 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
494 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
495 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 496 | |
df7492f9 KH |
497 | /* If set, reset graphic planes and registers at end-of-line to the |
498 | initial state. */ | |
499 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 500 | |
df7492f9 KH |
501 | /* If set, reset graphic planes and registers before any control |
502 | characters to the initial state. */ | |
503 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 504 | |
df7492f9 KH |
505 | /* If set, encode by 7-bit environment. */ |
506 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 507 | |
df7492f9 KH |
508 | /* If set, use locking-shift function. */ |
509 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 510 | |
df7492f9 KH |
511 | /* If set, use single-shift function. Overwrite |
512 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
513 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 514 | |
df7492f9 KH |
515 | /* If set, use designation escape sequence. */ |
516 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 517 | |
df7492f9 KH |
518 | /* If set, produce revision number sequence. */ |
519 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 520 | |
df7492f9 KH |
521 | /* If set, produce ISO6429's direction specifying sequence. */ |
522 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 523 | |
df7492f9 KH |
524 | /* If set, assume designation states are reset at beginning of line on |
525 | output. */ | |
526 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 527 | |
df7492f9 KH |
528 | /* If set, designation sequence should be placed at beginning of line |
529 | on output. */ | |
530 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 531 | |
df7492f9 KH |
532 | /* If set, do not encode unsafe charactes on output. */ |
533 | #define CODING_ISO_FLAG_SAFE 0x0800 | |
aa72b389 | 534 | |
df7492f9 KH |
535 | /* If set, extra latin codes (128..159) are accepted as a valid code |
536 | on input. */ | |
537 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 538 | |
df7492f9 | 539 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 540 | |
df7492f9 | 541 | #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 |
aa72b389 | 542 | |
bf16eb23 | 543 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 544 | |
bf16eb23 | 545 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 546 | |
bf16eb23 | 547 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 548 | |
df7492f9 KH |
549 | /* A character to be produced on output if encoding of the original |
550 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
551 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 552 | |
4ed46869 | 553 | |
df7492f9 KH |
554 | /* UTF-16 section */ |
555 | #define CODING_UTF_16_BOM(coding) \ | |
556 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 557 | |
df7492f9 KH |
558 | #define CODING_UTF_16_ENDIAN(coding) \ |
559 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 560 | |
df7492f9 KH |
561 | #define CODING_UTF_16_SURROGATE(coding) \ |
562 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 563 | |
4ed46869 | 564 | |
df7492f9 KH |
565 | /* CCL section */ |
566 | #define CODING_CCL_DECODER(coding) \ | |
567 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
568 | #define CODING_CCL_ENCODER(coding) \ | |
569 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
570 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 571 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 572 | |
5a936b46 | 573 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 574 | |
df7492f9 KH |
575 | enum coding_category |
576 | { | |
577 | coding_category_iso_7, | |
578 | coding_category_iso_7_tight, | |
579 | coding_category_iso_8_1, | |
580 | coding_category_iso_8_2, | |
581 | coding_category_iso_7_else, | |
582 | coding_category_iso_8_else, | |
583 | coding_category_utf_8, | |
584 | coding_category_utf_16_auto, | |
585 | coding_category_utf_16_be, | |
586 | coding_category_utf_16_le, | |
587 | coding_category_utf_16_be_nosig, | |
588 | coding_category_utf_16_le_nosig, | |
589 | coding_category_charset, | |
590 | coding_category_sjis, | |
591 | coding_category_big5, | |
592 | coding_category_ccl, | |
593 | coding_category_emacs_mule, | |
594 | /* All above are targets of code detection. */ | |
595 | coding_category_raw_text, | |
596 | coding_category_undecided, | |
597 | coding_category_max | |
598 | }; | |
599 | ||
600 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
601 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
602 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
603 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
604 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
605 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
606 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
607 | #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8) | |
b49a1807 | 608 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
609 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
610 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
611 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
612 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
613 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
614 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
615 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
616 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
617 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 618 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
619 | |
620 | /* This value is returned if detect_coding_mask () find nothing other | |
621 | than ASCII characters. */ | |
622 | #define CATEGORY_MASK_ANY \ | |
623 | (CATEGORY_MASK_ISO_7 \ | |
624 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
625 | | CATEGORY_MASK_ISO_8_1 \ | |
626 | | CATEGORY_MASK_ISO_8_2 \ | |
627 | | CATEGORY_MASK_ISO_7_ELSE \ | |
628 | | CATEGORY_MASK_ISO_8_ELSE \ | |
629 | | CATEGORY_MASK_UTF_8 \ | |
630 | | CATEGORY_MASK_UTF_16_BE \ | |
631 | | CATEGORY_MASK_UTF_16_LE \ | |
632 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
633 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
634 | | CATEGORY_MASK_CHARSET \ | |
635 | | CATEGORY_MASK_SJIS \ | |
636 | | CATEGORY_MASK_BIG5 \ | |
637 | | CATEGORY_MASK_CCL \ | |
638 | | CATEGORY_MASK_EMACS_MULE) | |
639 | ||
640 | ||
641 | #define CATEGORY_MASK_ISO_7BIT \ | |
642 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
643 | ||
644 | #define CATEGORY_MASK_ISO_8BIT \ | |
645 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
646 | ||
647 | #define CATEGORY_MASK_ISO_ELSE \ | |
648 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
649 | ||
650 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
651 | (CATEGORY_MASK_ISO_7 \ | |
652 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
653 | | CATEGORY_MASK_ISO_7_ELSE \ | |
654 | | CATEGORY_MASK_ISO_8_ELSE) | |
655 | ||
656 | #define CATEGORY_MASK_ISO \ | |
657 | ( CATEGORY_MASK_ISO_7BIT \ | |
658 | | CATEGORY_MASK_ISO_8BIT \ | |
659 | | CATEGORY_MASK_ISO_ELSE) | |
660 | ||
661 | #define CATEGORY_MASK_UTF_16 \ | |
662 | (CATEGORY_MASK_UTF_16_BE \ | |
663 | | CATEGORY_MASK_UTF_16_LE \ | |
664 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
665 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
666 | ||
667 | ||
668 | /* List of symbols `coding-category-xxx' ordered by priority. This | |
669 | variable is exposed to Emacs Lisp. */ | |
670 | static Lisp_Object Vcoding_category_list; | |
671 | ||
672 | /* Table of coding categories (Lisp symbols). This variable is for | |
673 | internal use oly. */ | |
674 | static Lisp_Object Vcoding_category_table; | |
675 | ||
676 | /* Table of coding-categories ordered by priority. */ | |
677 | static enum coding_category coding_priorities[coding_category_max]; | |
678 | ||
679 | /* Nth element is a coding context for the coding system bound to the | |
680 | Nth coding category. */ | |
681 | static struct coding_system coding_categories[coding_category_max]; | |
682 | ||
df7492f9 KH |
683 | /*** Commonly used macros and functions ***/ |
684 | ||
685 | #ifndef min | |
686 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
687 | #endif | |
688 | #ifndef max | |
689 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
690 | #endif | |
4ed46869 | 691 | |
24a73b0a KH |
692 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
693 | do { \ | |
694 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
695 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 696 | } while (0) |
4ed46869 | 697 | |
4ed46869 | 698 | |
df7492f9 KH |
699 | /* Safely get one byte from the source text pointed by SRC which ends |
700 | at SRC_END, and set C to that byte. If there are not enough bytes | |
065e3595 KH |
701 | in the source, it jumps to `no_more_source'. If multibytep is |
702 | nonzero, and a multibyte character is found at SRC, set C to the | |
703 | negative value of the character code. The caller should declare | |
704 | and set these variables appropriately in advance: | |
705 | src, src_end, multibytep */ | |
aa72b389 | 706 | |
065e3595 KH |
707 | #define ONE_MORE_BYTE(c) \ |
708 | do { \ | |
709 | if (src == src_end) \ | |
710 | { \ | |
711 | if (src_base < src) \ | |
712 | record_conversion_result \ | |
713 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
714 | goto no_more_source; \ | |
715 | } \ | |
716 | c = *src++; \ | |
717 | if (multibytep && (c & 0x80)) \ | |
718 | { \ | |
719 | if ((c & 0xFE) == 0xC0) \ | |
720 | c = ((c & 1) << 6) | *src++; \ | |
721 | else \ | |
722 | { \ | |
35befdaa KH |
723 | src--; \ |
724 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
725 | record_conversion_result \ |
726 | (coding, CODING_RESULT_INVALID_SRC); \ | |
727 | } \ | |
728 | } \ | |
729 | consumed_chars++; \ | |
aa72b389 KH |
730 | } while (0) |
731 | ||
aa72b389 | 732 | |
065e3595 KH |
733 | #define ONE_MORE_BYTE_NO_CHECK(c) \ |
734 | do { \ | |
735 | c = *src++; \ | |
736 | if (multibytep && (c & 0x80)) \ | |
737 | { \ | |
738 | if ((c & 0xFE) == 0xC0) \ | |
739 | c = ((c & 1) << 6) | *src++; \ | |
740 | else \ | |
741 | { \ | |
35befdaa KH |
742 | src--; \ |
743 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
744 | record_conversion_result \ |
745 | (coding, CODING_RESULT_INVALID_SRC); \ | |
746 | } \ | |
747 | } \ | |
748 | consumed_chars++; \ | |
aa72b389 KH |
749 | } while (0) |
750 | ||
aa72b389 | 751 | |
df7492f9 KH |
752 | /* Store a byte C in the place pointed by DST and increment DST to the |
753 | next free point, and increment PRODUCED_CHARS. The caller should | |
754 | assure that C is 0..127, and declare and set the variable `dst' | |
755 | appropriately in advance. | |
756 | */ | |
aa72b389 KH |
757 | |
758 | ||
df7492f9 KH |
759 | #define EMIT_ONE_ASCII_BYTE(c) \ |
760 | do { \ | |
761 | produced_chars++; \ | |
762 | *dst++ = (c); \ | |
b6871cc7 | 763 | } while (0) |
aa72b389 KH |
764 | |
765 | ||
df7492f9 | 766 | /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */ |
aa72b389 | 767 | |
df7492f9 KH |
768 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
769 | do { \ | |
770 | produced_chars += 2; \ | |
771 | *dst++ = (c1), *dst++ = (c2); \ | |
772 | } while (0) | |
aa72b389 KH |
773 | |
774 | ||
df7492f9 KH |
775 | /* Store a byte C in the place pointed by DST and increment DST to the |
776 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is | |
777 | nonzero, store in an appropriate multibyte from. The caller should | |
778 | declare and set the variables `dst' and `multibytep' appropriately | |
779 | in advance. */ | |
780 | ||
781 | #define EMIT_ONE_BYTE(c) \ | |
782 | do { \ | |
783 | produced_chars++; \ | |
784 | if (multibytep) \ | |
785 | { \ | |
786 | int ch = (c); \ | |
787 | if (ch >= 0x80) \ | |
788 | ch = BYTE8_TO_CHAR (ch); \ | |
789 | CHAR_STRING_ADVANCE (ch, dst); \ | |
790 | } \ | |
791 | else \ | |
792 | *dst++ = (c); \ | |
aa72b389 | 793 | } while (0) |
aa72b389 | 794 | |
aa72b389 | 795 | |
df7492f9 | 796 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 797 | |
e19c3639 KH |
798 | #define EMIT_TWO_BYTES(c1, c2) \ |
799 | do { \ | |
800 | produced_chars += 2; \ | |
801 | if (multibytep) \ | |
802 | { \ | |
803 | int ch; \ | |
804 | \ | |
805 | ch = (c1); \ | |
806 | if (ch >= 0x80) \ | |
807 | ch = BYTE8_TO_CHAR (ch); \ | |
808 | CHAR_STRING_ADVANCE (ch, dst); \ | |
809 | ch = (c2); \ | |
810 | if (ch >= 0x80) \ | |
811 | ch = BYTE8_TO_CHAR (ch); \ | |
812 | CHAR_STRING_ADVANCE (ch, dst); \ | |
813 | } \ | |
814 | else \ | |
815 | { \ | |
816 | *dst++ = (c1); \ | |
817 | *dst++ = (c2); \ | |
818 | } \ | |
aa72b389 KH |
819 | } while (0) |
820 | ||
821 | ||
df7492f9 KH |
822 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
823 | do { \ | |
824 | EMIT_ONE_BYTE (c1); \ | |
825 | EMIT_TWO_BYTES (c2, c3); \ | |
826 | } while (0) | |
aa72b389 | 827 | |
aa72b389 | 828 | |
df7492f9 KH |
829 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
830 | do { \ | |
831 | EMIT_TWO_BYTES (c1, c2); \ | |
832 | EMIT_TWO_BYTES (c3, c4); \ | |
833 | } while (0) | |
aa72b389 | 834 | |
aa72b389 | 835 | |
f6cbaf43 KH |
836 | /* Prototypes for static functions. */ |
837 | static void record_conversion_result P_ ((struct coding_system *coding, | |
838 | enum coding_result_code result)); | |
839 | static int detect_coding_utf_8 P_ ((struct coding_system *, | |
840 | struct coding_detection_info *info)); | |
841 | static void decode_coding_utf_8 P_ ((struct coding_system *)); | |
842 | static int encode_coding_utf_8 P_ ((struct coding_system *)); | |
843 | ||
844 | static int detect_coding_utf_16 P_ ((struct coding_system *, | |
845 | struct coding_detection_info *info)); | |
846 | static void decode_coding_utf_16 P_ ((struct coding_system *)); | |
847 | static int encode_coding_utf_16 P_ ((struct coding_system *)); | |
848 | ||
849 | static int detect_coding_iso_2022 P_ ((struct coding_system *, | |
850 | struct coding_detection_info *info)); | |
851 | static void decode_coding_iso_2022 P_ ((struct coding_system *)); | |
852 | static int encode_coding_iso_2022 P_ ((struct coding_system *)); | |
853 | ||
854 | static int detect_coding_emacs_mule P_ ((struct coding_system *, | |
855 | struct coding_detection_info *info)); | |
856 | static void decode_coding_emacs_mule P_ ((struct coding_system *)); | |
857 | static int encode_coding_emacs_mule P_ ((struct coding_system *)); | |
858 | ||
859 | static int detect_coding_sjis P_ ((struct coding_system *, | |
860 | struct coding_detection_info *info)); | |
861 | static void decode_coding_sjis P_ ((struct coding_system *)); | |
862 | static int encode_coding_sjis P_ ((struct coding_system *)); | |
863 | ||
864 | static int detect_coding_big5 P_ ((struct coding_system *, | |
865 | struct coding_detection_info *info)); | |
866 | static void decode_coding_big5 P_ ((struct coding_system *)); | |
867 | static int encode_coding_big5 P_ ((struct coding_system *)); | |
868 | ||
869 | static int detect_coding_ccl P_ ((struct coding_system *, | |
870 | struct coding_detection_info *info)); | |
871 | static void decode_coding_ccl P_ ((struct coding_system *)); | |
872 | static int encode_coding_ccl P_ ((struct coding_system *)); | |
873 | ||
874 | static void decode_coding_raw_text P_ ((struct coding_system *)); | |
875 | static int encode_coding_raw_text P_ ((struct coding_system *)); | |
876 | ||
877 | static void coding_set_source P_ ((struct coding_system *)); | |
878 | static void coding_set_destination P_ ((struct coding_system *)); | |
879 | static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT)); | |
880 | static void coding_alloc_by_making_gap P_ ((struct coding_system *, | |
287c57d7 | 881 | EMACS_INT, EMACS_INT)); |
f6cbaf43 KH |
882 | static unsigned char *alloc_destination P_ ((struct coding_system *, |
883 | EMACS_INT, unsigned char *)); | |
884 | static void setup_iso_safe_charsets P_ ((Lisp_Object)); | |
885 | static unsigned char *encode_designation_at_bol P_ ((struct coding_system *, | |
886 | int *, int *, | |
887 | unsigned char *)); | |
888 | static int detect_eol P_ ((const unsigned char *, | |
889 | EMACS_INT, enum coding_category)); | |
890 | static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int)); | |
891 | static void decode_eol P_ ((struct coding_system *)); | |
892 | static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *)); | |
893 | static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *, | |
894 | int, int *, int *)); | |
895 | static int produce_chars P_ ((struct coding_system *, Lisp_Object, int)); | |
896 | static INLINE void produce_composition P_ ((struct coding_system *, int *, | |
897 | EMACS_INT)); | |
898 | static INLINE void produce_charset P_ ((struct coding_system *, int *, | |
899 | EMACS_INT)); | |
900 | static void produce_annotation P_ ((struct coding_system *, EMACS_INT)); | |
901 | static int decode_coding P_ ((struct coding_system *)); | |
902 | static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT, | |
903 | struct coding_system *, | |
904 | int *, EMACS_INT *)); | |
905 | static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT, | |
906 | struct coding_system *, | |
907 | int *, EMACS_INT *)); | |
908 | static void consume_chars P_ ((struct coding_system *, Lisp_Object, int)); | |
909 | static int encode_coding P_ ((struct coding_system *)); | |
910 | static Lisp_Object make_conversion_work_buffer P_ ((int)); | |
911 | static Lisp_Object code_conversion_restore P_ ((Lisp_Object)); | |
912 | static INLINE int char_encodable_p P_ ((int, Lisp_Object)); | |
913 | static Lisp_Object make_subsidiaries P_ ((Lisp_Object)); | |
914 | ||
065e3595 KH |
915 | static void |
916 | record_conversion_result (struct coding_system *coding, | |
917 | enum coding_result_code result) | |
918 | { | |
919 | coding->result = result; | |
920 | switch (result) | |
921 | { | |
922 | case CODING_RESULT_INSUFFICIENT_SRC: | |
923 | Vlast_code_conversion_error = Qinsufficient_source; | |
924 | break; | |
925 | case CODING_RESULT_INCONSISTENT_EOL: | |
926 | Vlast_code_conversion_error = Qinconsistent_eol; | |
927 | break; | |
928 | case CODING_RESULT_INVALID_SRC: | |
929 | Vlast_code_conversion_error = Qinvalid_source; | |
930 | break; | |
931 | case CODING_RESULT_INTERRUPT: | |
932 | Vlast_code_conversion_error = Qinterrupted; | |
933 | break; | |
934 | case CODING_RESULT_INSUFFICIENT_MEM: | |
935 | Vlast_code_conversion_error = Qinsufficient_memory; | |
936 | break; | |
35befdaa KH |
937 | default: |
938 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
939 | } |
940 | } | |
941 | ||
df7492f9 KH |
942 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
943 | do { \ | |
944 | charset_map_loaded = 0; \ | |
945 | c = DECODE_CHAR (charset, code); \ | |
946 | if (charset_map_loaded) \ | |
947 | { \ | |
8f924df7 | 948 | const unsigned char *orig = coding->source; \ |
df7492f9 KH |
949 | EMACS_INT offset; \ |
950 | \ | |
951 | coding_set_source (coding); \ | |
952 | offset = coding->source - orig; \ | |
953 | src += offset; \ | |
954 | src_base += offset; \ | |
955 | src_end += offset; \ | |
956 | } \ | |
aa72b389 KH |
957 | } while (0) |
958 | ||
959 | ||
df7492f9 KH |
960 | #define ASSURE_DESTINATION(bytes) \ |
961 | do { \ | |
962 | if (dst + (bytes) >= dst_end) \ | |
963 | { \ | |
964 | int more_bytes = charbuf_end - charbuf + (bytes); \ | |
965 | \ | |
966 | dst = alloc_destination (coding, more_bytes, dst); \ | |
967 | dst_end = coding->destination + coding->dst_bytes; \ | |
968 | } \ | |
969 | } while (0) | |
aa72b389 | 970 | |
aa72b389 | 971 | |
aa72b389 | 972 | |
df7492f9 KH |
973 | static void |
974 | coding_set_source (coding) | |
aa72b389 | 975 | struct coding_system *coding; |
aa72b389 | 976 | { |
df7492f9 KH |
977 | if (BUFFERP (coding->src_object)) |
978 | { | |
2cb26057 | 979 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 980 | |
df7492f9 | 981 | if (coding->src_pos < 0) |
2cb26057 | 982 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 983 | else |
2cb26057 | 984 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 985 | } |
df7492f9 | 986 | else if (STRINGP (coding->src_object)) |
aa72b389 | 987 | { |
8f924df7 | 988 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 989 | } |
df7492f9 KH |
990 | else |
991 | /* Otherwise, the source is C string and is never relocated | |
992 | automatically. Thus we don't have to update anything. */ | |
993 | ; | |
994 | } | |
aa72b389 | 995 | |
df7492f9 KH |
996 | static void |
997 | coding_set_destination (coding) | |
998 | struct coding_system *coding; | |
999 | { | |
1000 | if (BUFFERP (coding->dst_object)) | |
aa72b389 | 1001 | { |
df7492f9 | 1002 | if (coding->src_pos < 0) |
aa72b389 | 1003 | { |
28f67a95 KH |
1004 | coding->destination = BEG_ADDR + coding->dst_pos_byte - 1; |
1005 | coding->dst_bytes = (GAP_END_ADDR | |
1006 | - (coding->src_bytes - coding->consumed) | |
1007 | - coding->destination); | |
aa72b389 | 1008 | } |
df7492f9 | 1009 | else |
28f67a95 KH |
1010 | { |
1011 | /* We are sure that coding->dst_pos_byte is before the gap | |
1012 | of the buffer. */ | |
1013 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
1014 | + coding->dst_pos_byte - 1); | |
1015 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) | |
1016 | - coding->destination); | |
1017 | } | |
df7492f9 KH |
1018 | } |
1019 | else | |
1020 | /* Otherwise, the destination is C string and is never relocated | |
1021 | automatically. Thus we don't have to update anything. */ | |
1022 | ; | |
1023 | } | |
1024 | ||
1025 | ||
1026 | static void | |
1027 | coding_alloc_by_realloc (coding, bytes) | |
1028 | struct coding_system *coding; | |
1029 | EMACS_INT bytes; | |
1030 | { | |
1031 | coding->destination = (unsigned char *) xrealloc (coding->destination, | |
1032 | coding->dst_bytes + bytes); | |
1033 | coding->dst_bytes += bytes; | |
1034 | } | |
1035 | ||
1036 | static void | |
287c57d7 | 1037 | coding_alloc_by_making_gap (coding, offset, bytes) |
df7492f9 | 1038 | struct coding_system *coding; |
287c57d7 | 1039 | EMACS_INT offset, bytes; |
df7492f9 | 1040 | { |
2c78b7e1 KH |
1041 | if (BUFFERP (coding->dst_object) |
1042 | && EQ (coding->src_object, coding->dst_object)) | |
df7492f9 | 1043 | { |
287c57d7 | 1044 | EMACS_INT add = offset + (coding->src_bytes - coding->consumed); |
df7492f9 | 1045 | |
287c57d7 | 1046 | GPT += offset, GPT_BYTE += offset; |
df7492f9 KH |
1047 | GAP_SIZE -= add; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; |
1048 | make_gap (bytes); | |
1049 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
287c57d7 | 1050 | GPT -= offset, GPT_BYTE -= offset; |
df7492f9 | 1051 | } |
730fff51 | 1052 | else |
df7492f9 | 1053 | { |
2c78b7e1 KH |
1054 | Lisp_Object this_buffer; |
1055 | ||
1056 | this_buffer = Fcurrent_buffer (); | |
df7492f9 KH |
1057 | set_buffer_internal (XBUFFER (coding->dst_object)); |
1058 | make_gap (bytes); | |
1059 | set_buffer_internal (XBUFFER (this_buffer)); | |
aa72b389 | 1060 | } |
df7492f9 | 1061 | } |
8f924df7 | 1062 | |
df7492f9 KH |
1063 | |
1064 | static unsigned char * | |
1065 | alloc_destination (coding, nbytes, dst) | |
1066 | struct coding_system *coding; | |
3e139625 | 1067 | EMACS_INT nbytes; |
df7492f9 KH |
1068 | unsigned char *dst; |
1069 | { | |
1070 | EMACS_INT offset = dst - coding->destination; | |
1071 | ||
1072 | if (BUFFERP (coding->dst_object)) | |
287c57d7 | 1073 | coding_alloc_by_making_gap (coding, offset, nbytes); |
aa72b389 | 1074 | else |
df7492f9 | 1075 | coding_alloc_by_realloc (coding, nbytes); |
065e3595 | 1076 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1077 | coding_set_destination (coding); |
1078 | dst = coding->destination + offset; | |
1079 | return dst; | |
1080 | } | |
aa72b389 | 1081 | |
ff0dacd7 KH |
1082 | /** Macros for annotations. */ |
1083 | ||
1084 | /* Maximum length of annotation data (sum of annotations for | |
1085 | composition and charset). */ | |
69a80ea3 | 1086 | #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4) |
ff0dacd7 KH |
1087 | |
1088 | /* An annotation data is stored in the array coding->charbuf in this | |
1089 | format: | |
69a80ea3 | 1090 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1091 | LENGTH is the number of elements in the annotation. |
1092 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1093 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1094 | |
1095 | The format of the following elements depend on ANNOTATION_MASK. | |
1096 | ||
1097 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1098 | follows: | |
1099 | ... METHOD [ COMPOSITION-COMPONENTS ... ] | |
1100 | METHOD is one of enum composition_method. | |
1101 | Optionnal COMPOSITION-COMPONENTS are characters and composition | |
1102 | rules. | |
1103 | ||
1104 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
1105 | follows. */ | |
1106 | ||
69a80ea3 | 1107 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1108 | do { \ |
1109 | *(buf)++ = -(len); \ | |
1110 | *(buf)++ = (mask); \ | |
69a80ea3 | 1111 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1112 | coding->annotated = 1; \ |
1113 | } while (0); | |
1114 | ||
69a80ea3 KH |
1115 | #define ADD_COMPOSITION_DATA(buf, nchars, method) \ |
1116 | do { \ | |
1117 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ | |
1118 | *buf++ = method; \ | |
ff0dacd7 KH |
1119 | } while (0) |
1120 | ||
1121 | ||
69a80ea3 KH |
1122 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1123 | do { \ | |
1124 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1125 | *buf++ = id; \ | |
ff0dacd7 KH |
1126 | } while (0) |
1127 | ||
df7492f9 KH |
1128 | \f |
1129 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1130 | ||
1131 | ||
1132 | ||
1133 | \f | |
1134 | /*** 3. UTF-8 ***/ | |
1135 | ||
1136 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1137 | Check if a text is encoded in UTF-8. If it is, return 1, else |
1138 | return 0. */ | |
df7492f9 KH |
1139 | |
1140 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1141 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1142 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1143 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1144 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1145 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1146 | ||
1147 | static int | |
ff0dacd7 | 1148 | detect_coding_utf_8 (coding, detect_info) |
df7492f9 | 1149 | struct coding_system *coding; |
ff0dacd7 | 1150 | struct coding_detection_info *detect_info; |
df7492f9 | 1151 | { |
065e3595 | 1152 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1153 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1154 | int multibytep = coding->src_multibyte; |
1155 | int consumed_chars = 0; | |
1156 | int found = 0; | |
1157 | ||
ff0dacd7 | 1158 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1159 | /* A coding system of this category is always ASCII compatible. */ |
1160 | src += coding->head_ascii; | |
1161 | ||
1162 | while (1) | |
aa72b389 | 1163 | { |
df7492f9 | 1164 | int c, c1, c2, c3, c4; |
aa72b389 | 1165 | |
065e3595 | 1166 | src_base = src; |
df7492f9 | 1167 | ONE_MORE_BYTE (c); |
065e3595 | 1168 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
df7492f9 KH |
1169 | continue; |
1170 | ONE_MORE_BYTE (c1); | |
065e3595 | 1171 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1172 | break; |
1173 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1174 | { |
ff0dacd7 | 1175 | found = CATEGORY_MASK_UTF_8; |
df7492f9 | 1176 | continue; |
aa72b389 | 1177 | } |
df7492f9 | 1178 | ONE_MORE_BYTE (c2); |
065e3595 | 1179 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1180 | break; |
1181 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1182 | { |
ff0dacd7 | 1183 | found = CATEGORY_MASK_UTF_8; |
df7492f9 | 1184 | continue; |
aa72b389 | 1185 | } |
df7492f9 | 1186 | ONE_MORE_BYTE (c3); |
065e3595 | 1187 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1188 | break; |
1189 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1190 | { |
ff0dacd7 | 1191 | found = CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1192 | continue; |
1193 | } | |
1194 | ONE_MORE_BYTE (c4); | |
065e3595 | 1195 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1196 | break; |
1197 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1198 | { | |
ff0dacd7 | 1199 | found = CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1200 | continue; |
1201 | } | |
1202 | break; | |
aa72b389 | 1203 | } |
ff0dacd7 | 1204 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1205 | return 0; |
aa72b389 | 1206 | |
df7492f9 | 1207 | no_more_source: |
065e3595 | 1208 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1209 | { |
ff0dacd7 | 1210 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1211 | return 0; |
aa72b389 | 1212 | } |
ff0dacd7 KH |
1213 | detect_info->found |= found; |
1214 | return 1; | |
aa72b389 KH |
1215 | } |
1216 | ||
4ed46869 | 1217 | |
b73bfc1c | 1218 | static void |
df7492f9 | 1219 | decode_coding_utf_8 (coding) |
b73bfc1c | 1220 | struct coding_system *coding; |
b73bfc1c | 1221 | { |
8f924df7 KH |
1222 | const unsigned char *src = coding->source + coding->consumed; |
1223 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1224 | const unsigned char *src_base; | |
69a80ea3 KH |
1225 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1226 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
1227 | int consumed_chars = 0, consumed_chars_base; |
1228 | int multibytep = coding->src_multibyte; | |
24a73b0a | 1229 | Lisp_Object attr, charset_list; |
4ed46869 | 1230 | |
24a73b0a | 1231 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 KH |
1232 | |
1233 | while (1) | |
b73bfc1c | 1234 | { |
df7492f9 | 1235 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1236 | |
df7492f9 KH |
1237 | src_base = src; |
1238 | consumed_chars_base = consumed_chars; | |
4af310db | 1239 | |
df7492f9 KH |
1240 | if (charbuf >= charbuf_end) |
1241 | break; | |
1242 | ||
1243 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1244 | if (c1 < 0) |
1245 | { | |
1246 | c = - c1; | |
1247 | } | |
1248 | else if (UTF_8_1_OCTET_P(c1)) | |
df7492f9 KH |
1249 | { |
1250 | c = c1; | |
4af310db | 1251 | } |
df7492f9 | 1252 | else |
4af310db | 1253 | { |
df7492f9 | 1254 | ONE_MORE_BYTE (c2); |
065e3595 | 1255 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1256 | goto invalid_code; |
1257 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1258 | { |
b0edb2c5 DL |
1259 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1260 | /* Reject overlong sequences here and below. Encoders | |
1261 | producing them are incorrect, they can be misleading, | |
1262 | and they mess up read/write invariance. */ | |
1263 | if (c < 128) | |
1264 | goto invalid_code; | |
4af310db | 1265 | } |
df7492f9 | 1266 | else |
aa72b389 | 1267 | { |
df7492f9 | 1268 | ONE_MORE_BYTE (c3); |
065e3595 | 1269 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1270 | goto invalid_code; |
1271 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1272 | { |
1273 | c = (((c1 & 0xF) << 12) | |
1274 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1275 | if (c < 0x800 |
1276 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1277 | goto invalid_code; |
1278 | } | |
df7492f9 KH |
1279 | else |
1280 | { | |
1281 | ONE_MORE_BYTE (c4); | |
065e3595 | 1282 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1283 | goto invalid_code; |
1284 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1285 | { |
df7492f9 KH |
1286 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1287 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1288 | if (c < 0x10000) |
1289 | goto invalid_code; | |
1290 | } | |
df7492f9 KH |
1291 | else |
1292 | { | |
1293 | ONE_MORE_BYTE (c5); | |
065e3595 | 1294 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1295 | goto invalid_code; |
1296 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1297 | { | |
1298 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1299 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1300 | | (c5 & 0x3F)); | |
b0edb2c5 | 1301 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1302 | goto invalid_code; |
1303 | } | |
1304 | else | |
1305 | goto invalid_code; | |
1306 | } | |
1307 | } | |
aa72b389 | 1308 | } |
b73bfc1c | 1309 | } |
df7492f9 KH |
1310 | |
1311 | *charbuf++ = c; | |
1312 | continue; | |
1313 | ||
1314 | invalid_code: | |
1315 | src = src_base; | |
1316 | consumed_chars = consumed_chars_base; | |
1317 | ONE_MORE_BYTE (c); | |
1318 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1319 | coding->errors++; | |
aa72b389 KH |
1320 | } |
1321 | ||
df7492f9 KH |
1322 | no_more_source: |
1323 | coding->consumed_char += consumed_chars_base; | |
1324 | coding->consumed = src_base - coding->source; | |
1325 | coding->charbuf_used = charbuf - coding->charbuf; | |
1326 | } | |
1327 | ||
1328 | ||
1329 | static int | |
1330 | encode_coding_utf_8 (coding) | |
1331 | struct coding_system *coding; | |
1332 | { | |
1333 | int multibytep = coding->dst_multibyte; | |
1334 | int *charbuf = coding->charbuf; | |
1335 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1336 | unsigned char *dst = coding->destination + coding->produced; | |
1337 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
e19c3639 | 1338 | int produced_chars = 0; |
df7492f9 KH |
1339 | int c; |
1340 | ||
1341 | if (multibytep) | |
aa72b389 | 1342 | { |
df7492f9 KH |
1343 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1344 | ||
1345 | while (charbuf < charbuf_end) | |
b73bfc1c | 1346 | { |
df7492f9 | 1347 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1348 | |
df7492f9 KH |
1349 | ASSURE_DESTINATION (safe_room); |
1350 | c = *charbuf++; | |
28f67a95 KH |
1351 | if (CHAR_BYTE8_P (c)) |
1352 | { | |
1353 | c = CHAR_TO_BYTE8 (c); | |
1354 | EMIT_ONE_BYTE (c); | |
1355 | } | |
1356 | else | |
1357 | { | |
1358 | CHAR_STRING_ADVANCE (c, pend); | |
1359 | for (p = str; p < pend; p++) | |
1360 | EMIT_ONE_BYTE (*p); | |
1361 | } | |
b73bfc1c | 1362 | } |
aa72b389 | 1363 | } |
df7492f9 KH |
1364 | else |
1365 | { | |
1366 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1367 | ||
1368 | while (charbuf < charbuf_end) | |
b73bfc1c | 1369 | { |
df7492f9 KH |
1370 | ASSURE_DESTINATION (safe_room); |
1371 | c = *charbuf++; | |
f03caae0 KH |
1372 | if (CHAR_BYTE8_P (c)) |
1373 | *dst++ = CHAR_TO_BYTE8 (c); | |
1374 | else | |
1375 | dst += CHAR_STRING (c, dst); | |
df7492f9 | 1376 | produced_chars++; |
4ed46869 KH |
1377 | } |
1378 | } | |
065e3595 | 1379 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1380 | coding->produced_char += produced_chars; |
1381 | coding->produced = dst - coding->destination; | |
1382 | return 0; | |
4ed46869 KH |
1383 | } |
1384 | ||
b73bfc1c | 1385 | |
df7492f9 | 1386 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
1387 | Check if a text is encoded in one of UTF-16 based coding systems. |
1388 | If it is, return 1, else return 0. */ | |
aa72b389 | 1389 | |
df7492f9 KH |
1390 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1391 | (((val) & 0xFC00) == 0xD800) | |
1392 | ||
1393 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1394 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1395 | |
df7492f9 KH |
1396 | #define UTF_16_INVALID_P(val) \ |
1397 | (((val) == 0xFFFE) \ | |
1398 | || ((val) == 0xFFFF) \ | |
1399 | || UTF_16_LOW_SURROGATE_P (val)) | |
aa72b389 | 1400 | |
aa72b389 | 1401 | |
df7492f9 | 1402 | static int |
ff0dacd7 | 1403 | detect_coding_utf_16 (coding, detect_info) |
aa72b389 | 1404 | struct coding_system *coding; |
ff0dacd7 | 1405 | struct coding_detection_info *detect_info; |
aa72b389 | 1406 | { |
8f924df7 KH |
1407 | const unsigned char *src = coding->source, *src_base = src; |
1408 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
1409 | int multibytep = coding->src_multibyte; |
1410 | int consumed_chars = 0; | |
1411 | int c1, c2; | |
aa72b389 | 1412 | |
ff0dacd7 | 1413 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1414 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1415 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1416 | { |
1417 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1418 | return 0; | |
1419 | } | |
24a73b0a | 1420 | |
df7492f9 KH |
1421 | ONE_MORE_BYTE (c1); |
1422 | ONE_MORE_BYTE (c2); | |
df7492f9 | 1423 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1424 | { |
b49a1807 KH |
1425 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1426 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1427 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1428 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1429 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1430 | } |
df7492f9 | 1431 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1432 | { |
b49a1807 KH |
1433 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1434 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1435 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1436 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1437 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1438 | } | |
065e3595 | 1439 | else if (c1 >= 0 && c2 >= 0) |
24a73b0a | 1440 | { |
24a73b0a KH |
1441 | detect_info->rejected |
1442 | |= (CATEGORY_MASK_UTF_16_BE | CATEGORY_MASK_UTF_16_LE); | |
ff0dacd7 | 1443 | } |
df7492f9 | 1444 | no_more_source: |
ff0dacd7 | 1445 | return 1; |
df7492f9 | 1446 | } |
aa72b389 | 1447 | |
df7492f9 KH |
1448 | static void |
1449 | decode_coding_utf_16 (coding) | |
1450 | struct coding_system *coding; | |
1451 | { | |
8f924df7 KH |
1452 | const unsigned char *src = coding->source + coding->consumed; |
1453 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1454 | const unsigned char *src_base; | |
69a80ea3 KH |
1455 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1456 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
1457 | int consumed_chars = 0, consumed_chars_base; |
1458 | int multibytep = coding->src_multibyte; | |
1459 | enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding); | |
1460 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); | |
1461 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
24a73b0a | 1462 | Lisp_Object attr, charset_list; |
df7492f9 | 1463 | |
24a73b0a | 1464 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1465 | |
b49a1807 | 1466 | if (bom == utf_16_with_bom) |
aa72b389 | 1467 | { |
df7492f9 | 1468 | int c, c1, c2; |
4af310db | 1469 | |
aa72b389 | 1470 | src_base = src; |
df7492f9 KH |
1471 | ONE_MORE_BYTE (c1); |
1472 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1473 | c = (c1 << 8) | c2; |
aa72b389 | 1474 | |
b49a1807 KH |
1475 | if (endian == utf_16_big_endian |
1476 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1477 | { |
b49a1807 KH |
1478 | /* The first two bytes are not BOM. Treat them as bytes |
1479 | for a normal character. */ | |
1480 | src = src_base; | |
1481 | coding->errors++; | |
aa72b389 | 1482 | } |
b49a1807 KH |
1483 | CODING_UTF_16_BOM (coding) = utf_16_without_bom; |
1484 | } | |
1485 | else if (bom == utf_16_detect_bom) | |
1486 | { | |
1487 | /* We have already tried to detect BOM and failed in | |
1488 | detect_coding. */ | |
1489 | CODING_UTF_16_BOM (coding) = utf_16_without_bom; | |
df7492f9 | 1490 | } |
aa72b389 | 1491 | |
df7492f9 KH |
1492 | while (1) |
1493 | { | |
1494 | int c, c1, c2; | |
1495 | ||
1496 | src_base = src; | |
1497 | consumed_chars_base = consumed_chars; | |
1498 | ||
1499 | if (charbuf + 2 >= charbuf_end) | |
1500 | break; | |
1501 | ||
1502 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1503 | if (c1 < 0) |
1504 | { | |
1505 | *charbuf++ = -c1; | |
1506 | continue; | |
1507 | } | |
df7492f9 | 1508 | ONE_MORE_BYTE (c2); |
065e3595 KH |
1509 | if (c2 < 0) |
1510 | { | |
1511 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1512 | *charbuf++ = -c2; | |
1513 | continue; | |
1514 | } | |
df7492f9 | 1515 | c = (endian == utf_16_big_endian |
e19c3639 | 1516 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
df7492f9 | 1517 | if (surrogate) |
fd3ae0b9 | 1518 | { |
df7492f9 | 1519 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1520 | { |
df7492f9 KH |
1521 | if (endian == utf_16_big_endian) |
1522 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1523 | else | |
1524 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1525 | *charbuf++ = c1; | |
1526 | *charbuf++ = c2; | |
1527 | coding->errors++; | |
1528 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1529 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1530 | else |
df7492f9 | 1531 | *charbuf++ = c; |
fd3ae0b9 KH |
1532 | } |
1533 | else | |
df7492f9 KH |
1534 | { |
1535 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1536 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1537 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1538 | } |
fd3ae0b9 | 1539 | } |
aa72b389 | 1540 | else |
df7492f9 KH |
1541 | { |
1542 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1543 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1544 | else | |
1545 | *charbuf++ = c; | |
8f924df7 | 1546 | } |
aa72b389 | 1547 | } |
df7492f9 KH |
1548 | |
1549 | no_more_source: | |
1550 | coding->consumed_char += consumed_chars_base; | |
1551 | coding->consumed = src_base - coding->source; | |
1552 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1553 | } |
b73bfc1c | 1554 | |
df7492f9 KH |
1555 | static int |
1556 | encode_coding_utf_16 (coding) | |
1557 | struct coding_system *coding; | |
1558 | { | |
1559 | int multibytep = coding->dst_multibyte; | |
1560 | int *charbuf = coding->charbuf; | |
1561 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1562 | unsigned char *dst = coding->destination + coding->produced; | |
1563 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1564 | int safe_room = 8; | |
1565 | enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding); | |
1566 | int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; | |
1567 | int produced_chars = 0; | |
24a73b0a | 1568 | Lisp_Object attrs, charset_list; |
df7492f9 | 1569 | int c; |
4ed46869 | 1570 | |
24a73b0a | 1571 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 1572 | |
b49a1807 | 1573 | if (bom != utf_16_without_bom) |
df7492f9 KH |
1574 | { |
1575 | ASSURE_DESTINATION (safe_room); | |
1576 | if (big_endian) | |
df7492f9 | 1577 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1578 | else |
1579 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
df7492f9 KH |
1580 | CODING_UTF_16_BOM (coding) = utf_16_without_bom; |
1581 | } | |
1582 | ||
1583 | while (charbuf < charbuf_end) | |
1584 | { | |
1585 | ASSURE_DESTINATION (safe_room); | |
1586 | c = *charbuf++; | |
e19c3639 KH |
1587 | if (c >= MAX_UNICODE_CHAR) |
1588 | c = coding->default_char; | |
df7492f9 KH |
1589 | |
1590 | if (c < 0x10000) | |
1591 | { | |
1592 | if (big_endian) | |
1593 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1594 | else | |
1595 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1596 | } | |
1597 | else | |
1598 | { | |
1599 | int c1, c2; | |
1600 | ||
1601 | c -= 0x10000; | |
1602 | c1 = (c >> 10) + 0xD800; | |
1603 | c2 = (c & 0x3FF) + 0xDC00; | |
1604 | if (big_endian) | |
1605 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1606 | else | |
1607 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1608 | } | |
1609 | } | |
065e3595 | 1610 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1611 | coding->produced = dst - coding->destination; |
1612 | coding->produced_char += produced_chars; | |
1613 | return 0; | |
1614 | } | |
1615 | ||
1616 | \f | |
1617 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1618 | ||
1619 | /* Emacs' internal format for representation of multiple character | |
1620 | sets is a kind of multi-byte encoding, i.e. characters are | |
1621 | represented by variable-length sequences of one-byte codes. | |
1622 | ||
1623 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1624 | represented by one-byte sequences which are their ASCII codes, in | |
1625 | the range 0x00 through 0x7F. | |
1626 | ||
1627 | 8-bit characters of the range 0x80..0x9F are represented by | |
1628 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1629 | code + 0x20). | |
1630 | ||
1631 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1632 | one-byte sequences which are their 8-bit code. | |
1633 | ||
1634 | The other characters are represented by a sequence of `base | |
1635 | leading-code', optional `extended leading-code', and one or two | |
1636 | `position-code's. The length of the sequence is determined by the | |
1637 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1638 | whereas extended leading-code and position-code take the range 0xA0 | |
1639 | through 0xFF. See `charset.h' for more details about leading-code | |
1640 | and position-code. | |
1641 | ||
1642 | --- CODE RANGE of Emacs' internal format --- | |
1643 | character set range | |
1644 | ------------- ----- | |
1645 | ascii 0x00..0x7F | |
1646 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1647 | eight-bit-graphic 0xA0..0xBF | |
1648 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1649 | --------------------------------------------- | |
1650 | ||
1651 | As this is the internal character representation, the format is | |
1652 | usually not used externally (i.e. in a file or in a data sent to a | |
1653 | process). But, it is possible to have a text externally in this | |
1654 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1655 | ||
1656 | In that case, a sequence of one-byte codes has a slightly different | |
1657 | form. | |
1658 | ||
1659 | At first, all characters in eight-bit-control are represented by | |
1660 | one-byte sequences which are their 8-bit code. | |
1661 | ||
1662 | Next, character composition data are represented by the byte | |
1663 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1664 | where, | |
1665 | METHOD is 0xF0 plus one of composition method (enum | |
1666 | composition_method), | |
1667 | ||
1668 | BYTES is 0xA0 plus a byte length of this composition data, | |
1669 | ||
1670 | CHARS is 0x20 plus a number of characters composed by this | |
1671 | data, | |
1672 | ||
1673 | COMPONENTs are characters of multibye form or composition | |
1674 | rules encoded by two-byte of ASCII codes. | |
1675 | ||
1676 | In addition, for backward compatibility, the following formats are | |
1677 | also recognized as composition data on decoding. | |
1678 | ||
1679 | 0x80 MSEQ ... | |
1680 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1681 | ||
1682 | Here, | |
1683 | MSEQ is a multibyte form but in these special format: | |
1684 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1685 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1686 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1687 | represents a composition rule. | |
1688 | */ | |
1689 | ||
1690 | char emacs_mule_bytes[256]; | |
1691 | ||
df7492f9 | 1692 | int |
ff0dacd7 | 1693 | emacs_mule_char (coding, src, nbytes, nchars, id) |
df7492f9 | 1694 | struct coding_system *coding; |
065e3595 | 1695 | const unsigned char *src; |
ff0dacd7 | 1696 | int *nbytes, *nchars, *id; |
df7492f9 | 1697 | { |
8f924df7 KH |
1698 | const unsigned char *src_end = coding->source + coding->src_bytes; |
1699 | const unsigned char *src_base = src; | |
df7492f9 | 1700 | int multibytep = coding->src_multibyte; |
df7492f9 KH |
1701 | struct charset *charset; |
1702 | unsigned code; | |
1703 | int c; | |
1704 | int consumed_chars = 0; | |
1705 | ||
1706 | ONE_MORE_BYTE (c); | |
065e3595 | 1707 | if (c < 0) |
df7492f9 | 1708 | { |
065e3595 KH |
1709 | c = -c; |
1710 | charset = emacs_mule_charset[0]; | |
1711 | } | |
1712 | else | |
1713 | { | |
4d41e8b7 KH |
1714 | if (c >= 0xA0) |
1715 | { | |
1716 | /* Old style component character of a compostion. */ | |
1717 | if (c == 0xA0) | |
1718 | { | |
1719 | ONE_MORE_BYTE (c); | |
1720 | c -= 0x80; | |
1721 | } | |
1722 | else | |
1723 | c -= 0x20; | |
1724 | } | |
1725 | ||
065e3595 | 1726 | switch (emacs_mule_bytes[c]) |
b73bfc1c | 1727 | { |
065e3595 | 1728 | case 2: |
df7492f9 KH |
1729 | if (! (charset = emacs_mule_charset[c])) |
1730 | goto invalid_code; | |
1731 | ONE_MORE_BYTE (c); | |
9ffd559c | 1732 | if (c < 0xA0) |
065e3595 | 1733 | goto invalid_code; |
df7492f9 | 1734 | code = c & 0x7F; |
065e3595 KH |
1735 | break; |
1736 | ||
1737 | case 3: | |
1738 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
1739 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
1740 | { | |
1741 | ONE_MORE_BYTE (c); | |
9ffd559c | 1742 | if (c < 0xA0 || ! (charset = emacs_mule_charset[c])) |
065e3595 KH |
1743 | goto invalid_code; |
1744 | ONE_MORE_BYTE (c); | |
9ffd559c | 1745 | if (c < 0xA0) |
065e3595 KH |
1746 | goto invalid_code; |
1747 | code = c & 0x7F; | |
1748 | } | |
1749 | else | |
1750 | { | |
1751 | if (! (charset = emacs_mule_charset[c])) | |
1752 | goto invalid_code; | |
1753 | ONE_MORE_BYTE (c); | |
9ffd559c | 1754 | if (c < 0xA0) |
065e3595 KH |
1755 | goto invalid_code; |
1756 | code = (c & 0x7F) << 8; | |
1757 | ONE_MORE_BYTE (c); | |
9ffd559c | 1758 | if (c < 0xA0) |
065e3595 KH |
1759 | goto invalid_code; |
1760 | code |= c & 0x7F; | |
1761 | } | |
1762 | break; | |
1763 | ||
1764 | case 4: | |
1765 | ONE_MORE_BYTE (c); | |
1766 | if (c < 0 || ! (charset = emacs_mule_charset[c])) | |
df7492f9 KH |
1767 | goto invalid_code; |
1768 | ONE_MORE_BYTE (c); | |
9ffd559c | 1769 | if (c < 0xA0) |
065e3595 | 1770 | goto invalid_code; |
781d7a48 | 1771 | code = (c & 0x7F) << 8; |
df7492f9 | 1772 | ONE_MORE_BYTE (c); |
9ffd559c | 1773 | if (c < 0xA0) |
065e3595 | 1774 | goto invalid_code; |
df7492f9 | 1775 | code |= c & 0x7F; |
065e3595 | 1776 | break; |
df7492f9 | 1777 | |
065e3595 KH |
1778 | case 1: |
1779 | code = c; | |
1780 | charset = CHARSET_FROM_ID (ASCII_BYTE_P (code) | |
1781 | ? charset_ascii : charset_eight_bit); | |
1782 | break; | |
df7492f9 | 1783 | |
065e3595 KH |
1784 | default: |
1785 | abort (); | |
1786 | } | |
1787 | c = DECODE_CHAR (charset, code); | |
1788 | if (c < 0) | |
1789 | goto invalid_code; | |
df7492f9 | 1790 | } |
df7492f9 KH |
1791 | *nbytes = src - src_base; |
1792 | *nchars = consumed_chars; | |
ff0dacd7 KH |
1793 | if (id) |
1794 | *id = charset->id; | |
df7492f9 KH |
1795 | return c; |
1796 | ||
1797 | no_more_source: | |
1798 | return -2; | |
1799 | ||
1800 | invalid_code: | |
1801 | return -1; | |
1802 | } | |
1803 | ||
1804 | ||
1805 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1806 | Check if a text is encoded in `emacs-mule'. If it is, return 1, |
1807 | else return 0. */ | |
df7492f9 KH |
1808 | |
1809 | static int | |
ff0dacd7 | 1810 | detect_coding_emacs_mule (coding, detect_info) |
df7492f9 | 1811 | struct coding_system *coding; |
ff0dacd7 | 1812 | struct coding_detection_info *detect_info; |
df7492f9 | 1813 | { |
065e3595 | 1814 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1815 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1816 | int multibytep = coding->src_multibyte; |
1817 | int consumed_chars = 0; | |
1818 | int c; | |
1819 | int found = 0; | |
1820 | ||
ff0dacd7 | 1821 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; |
df7492f9 KH |
1822 | /* A coding system of this category is always ASCII compatible. */ |
1823 | src += coding->head_ascii; | |
1824 | ||
1825 | while (1) | |
1826 | { | |
065e3595 | 1827 | src_base = src; |
df7492f9 | 1828 | ONE_MORE_BYTE (c); |
065e3595 KH |
1829 | if (c < 0) |
1830 | continue; | |
df7492f9 KH |
1831 | if (c == 0x80) |
1832 | { | |
1833 | /* Perhaps the start of composite character. We simple skip | |
1834 | it because analyzing it is too heavy for detecting. But, | |
1835 | at least, we check that the composite character | |
1836 | constitues of more than 4 bytes. */ | |
8f924df7 | 1837 | const unsigned char *src_base; |
df7492f9 KH |
1838 | |
1839 | repeat: | |
1840 | src_base = src; | |
1841 | do | |
1842 | { | |
1843 | ONE_MORE_BYTE (c); | |
1844 | } | |
1845 | while (c >= 0xA0); | |
1846 | ||
1847 | if (src - src_base <= 4) | |
1848 | break; | |
ff0dacd7 | 1849 | found = CATEGORY_MASK_EMACS_MULE; |
df7492f9 KH |
1850 | if (c == 0x80) |
1851 | goto repeat; | |
b73bfc1c | 1852 | } |
df7492f9 KH |
1853 | |
1854 | if (c < 0x80) | |
b73bfc1c | 1855 | { |
df7492f9 KH |
1856 | if (c < 0x20 |
1857 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
1858 | break; | |
1859 | } | |
1860 | else | |
1861 | { | |
0e219d54 | 1862 | int more_bytes = emacs_mule_bytes[*src_base] - 1; |
df7492f9 | 1863 | |
0e219d54 | 1864 | while (more_bytes > 0) |
df7492f9 KH |
1865 | { |
1866 | ONE_MORE_BYTE (c); | |
0e219d54 KH |
1867 | if (c < 0xA0) |
1868 | { | |
1869 | src--; /* Unread the last byte. */ | |
1870 | break; | |
1871 | } | |
1872 | more_bytes--; | |
df7492f9 | 1873 | } |
0e219d54 | 1874 | if (more_bytes != 0) |
df7492f9 | 1875 | break; |
ff0dacd7 | 1876 | found = CATEGORY_MASK_EMACS_MULE; |
4ed46869 KH |
1877 | } |
1878 | } | |
ff0dacd7 | 1879 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; |
df7492f9 KH |
1880 | return 0; |
1881 | ||
1882 | no_more_source: | |
065e3595 | 1883 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 1884 | { |
ff0dacd7 | 1885 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; |
89528eb3 KH |
1886 | return 0; |
1887 | } | |
ff0dacd7 KH |
1888 | detect_info->found |= found; |
1889 | return 1; | |
4ed46869 KH |
1890 | } |
1891 | ||
b73bfc1c | 1892 | |
df7492f9 KH |
1893 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
1894 | ||
1895 | /* Decode a character represented as a component of composition | |
1896 | sequence of Emacs 20/21 style at SRC. Set C to that character and | |
1897 | update SRC to the head of next character (or an encoded composition | |
1898 | rule). If SRC doesn't points a composition component, set C to -1. | |
1899 | If SRC points an invalid byte sequence, global exit by a return | |
1900 | value 0. */ | |
1901 | ||
1902 | #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \ | |
1903 | if (1) \ | |
1904 | { \ | |
1905 | int c; \ | |
1906 | int nbytes, nchars; \ | |
1907 | \ | |
1908 | if (src == src_end) \ | |
1909 | break; \ | |
ff0dacd7 | 1910 | c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\ |
df7492f9 KH |
1911 | if (c < 0) \ |
1912 | { \ | |
1913 | if (c == -2) \ | |
1914 | break; \ | |
1915 | goto invalid_code; \ | |
1916 | } \ | |
1917 | *buf++ = c; \ | |
1918 | src += nbytes; \ | |
1919 | consumed_chars += nchars; \ | |
1920 | } \ | |
1921 | else | |
1922 | ||
1923 | ||
1924 | /* Decode a composition rule represented as a component of composition | |
781d7a48 KH |
1925 | sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF, |
1926 | and increment BUF. If SRC points an invalid byte sequence, set C | |
1927 | to -1. */ | |
df7492f9 | 1928 | |
781d7a48 | 1929 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \ |
df7492f9 KH |
1930 | do { \ |
1931 | int c, gref, nref; \ | |
1932 | \ | |
781d7a48 | 1933 | if (src >= src_end) \ |
df7492f9 KH |
1934 | goto invalid_code; \ |
1935 | ONE_MORE_BYTE_NO_CHECK (c); \ | |
4d41e8b7 | 1936 | c -= 0xA0; \ |
df7492f9 KH |
1937 | if (c < 0 || c >= 81) \ |
1938 | goto invalid_code; \ | |
1939 | \ | |
1940 | gref = c / 9, nref = c % 9; \ | |
1941 | *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
1942 | } while (0) | |
1943 | ||
1944 | ||
781d7a48 KH |
1945 | /* Decode a composition rule represented as a component of composition |
1946 | sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF, | |
1947 | and increment BUF. If SRC points an invalid byte sequence, set C | |
1948 | to -1. */ | |
1949 | ||
1950 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \ | |
1951 | do { \ | |
1952 | int gref, nref; \ | |
1953 | \ | |
1954 | if (src + 1>= src_end) \ | |
1955 | goto invalid_code; \ | |
1956 | ONE_MORE_BYTE_NO_CHECK (gref); \ | |
1957 | gref -= 0x20; \ | |
1958 | ONE_MORE_BYTE_NO_CHECK (nref); \ | |
1959 | nref -= 0x20; \ | |
1960 | if (gref < 0 || gref >= 81 \ | |
1961 | || nref < 0 || nref >= 81) \ | |
1962 | goto invalid_code; \ | |
1963 | *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
1964 | } while (0) | |
1965 | ||
1966 | ||
df7492f9 | 1967 | #define DECODE_EMACS_MULE_21_COMPOSITION(c) \ |
aa72b389 | 1968 | do { \ |
df7492f9 | 1969 | /* Emacs 21 style format. The first three bytes at SRC are \ |
781d7a48 | 1970 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \ |
df7492f9 KH |
1971 | the byte length of this composition information, CHARS is the \ |
1972 | number of characters composed by this composition. */ \ | |
781d7a48 KH |
1973 | enum composition_method method = c - 0xF2; \ |
1974 | int *charbuf_base = charbuf; \ | |
df7492f9 KH |
1975 | int consumed_chars_limit; \ |
1976 | int nbytes, nchars; \ | |
1977 | \ | |
1978 | ONE_MORE_BYTE (c); \ | |
065e3595 KH |
1979 | if (c < 0) \ |
1980 | goto invalid_code; \ | |
df7492f9 KH |
1981 | nbytes = c - 0xA0; \ |
1982 | if (nbytes < 3) \ | |
1983 | goto invalid_code; \ | |
1984 | ONE_MORE_BYTE (c); \ | |
065e3595 KH |
1985 | if (c < 0) \ |
1986 | goto invalid_code; \ | |
df7492f9 | 1987 | nchars = c - 0xA0; \ |
69a80ea3 | 1988 | ADD_COMPOSITION_DATA (charbuf, nchars, method); \ |
df7492f9 KH |
1989 | consumed_chars_limit = consumed_chars_base + nbytes; \ |
1990 | if (method != COMPOSITION_RELATIVE) \ | |
aa72b389 | 1991 | { \ |
df7492f9 KH |
1992 | int i = 0; \ |
1993 | while (consumed_chars < consumed_chars_limit) \ | |
aa72b389 | 1994 | { \ |
df7492f9 | 1995 | if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \ |
781d7a48 | 1996 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \ |
df7492f9 KH |
1997 | else \ |
1998 | DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \ | |
781d7a48 | 1999 | i++; \ |
aa72b389 | 2000 | } \ |
df7492f9 KH |
2001 | if (consumed_chars < consumed_chars_limit) \ |
2002 | goto invalid_code; \ | |
781d7a48 | 2003 | charbuf_base[0] -= i; \ |
aa72b389 KH |
2004 | } \ |
2005 | } while (0) | |
93dec019 | 2006 | |
aa72b389 | 2007 | |
d959f512 KH |
2008 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \ |
2009 | do { \ | |
2010 | /* Emacs 20 style format for relative composition. */ \ | |
2011 | /* Store multibyte form of characters to be composed. */ \ | |
2012 | enum composition_method method = COMPOSITION_RELATIVE; \ | |
2013 | int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \ | |
2014 | int *buf = components; \ | |
2015 | int i, j; \ | |
2016 | \ | |
2017 | src = src_base; \ | |
2018 | ONE_MORE_BYTE (c); /* skip 0x80 */ \ | |
2019 | for (i = 0; *src >= 0xA0 && i < MAX_COMPOSITION_COMPONENTS; i++) \ | |
2020 | DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \ | |
2021 | if (i < 2) \ | |
2022 | goto invalid_code; \ | |
2023 | ADD_COMPOSITION_DATA (charbuf, i, method); \ | |
2024 | for (j = 0; j < i; j++) \ | |
2025 | *charbuf++ = components[j]; \ | |
df7492f9 KH |
2026 | } while (0) |
2027 | ||
2028 | ||
2029 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \ | |
2030 | do { \ | |
2031 | /* Emacs 20 style format for rule-base composition. */ \ | |
2032 | /* Store multibyte form of characters to be composed. */ \ | |
ff0dacd7 | 2033 | enum composition_method method = COMPOSITION_WITH_RULE; \ |
4d41e8b7 | 2034 | int *charbuf_base = charbuf; \ |
df7492f9 KH |
2035 | int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \ |
2036 | int *buf = components; \ | |
2037 | int i, j; \ | |
4d41e8b7 | 2038 | \ |
df7492f9 | 2039 | DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \ |
4d41e8b7 | 2040 | for (i = 1; i < MAX_COMPOSITION_COMPONENTS; i++) \ |
df7492f9 | 2041 | { \ |
4d41e8b7 KH |
2042 | if (*src < 0xA0) \ |
2043 | break; \ | |
781d7a48 | 2044 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \ |
df7492f9 KH |
2045 | DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \ |
2046 | } \ | |
4d41e8b7 | 2047 | if (i <= 1 || (buf - components) % 2 == 0) \ |
df7492f9 | 2048 | goto invalid_code; \ |
4d41e8b7 | 2049 | if (charbuf + i + (i / 2) + 1 >= charbuf_end) \ |
df7492f9 | 2050 | goto no_more_source; \ |
4d41e8b7 KH |
2051 | ADD_COMPOSITION_DATA (charbuf, i, method); \ |
2052 | i = i * 2 - 1; \ | |
df7492f9 KH |
2053 | for (j = 0; j < i; j++) \ |
2054 | *charbuf++ = components[j]; \ | |
4d41e8b7 | 2055 | charbuf_base[0] -= i; \ |
df7492f9 KH |
2056 | for (j = 0; j < i; j += 2) \ |
2057 | *charbuf++ = components[j]; \ | |
2058 | } while (0) | |
2059 | ||
aa72b389 KH |
2060 | |
2061 | static void | |
df7492f9 | 2062 | decode_coding_emacs_mule (coding) |
aa72b389 | 2063 | struct coding_system *coding; |
aa72b389 | 2064 | { |
8f924df7 KH |
2065 | const unsigned char *src = coding->source + coding->consumed; |
2066 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2067 | const unsigned char *src_base; | |
69a80ea3 KH |
2068 | int *charbuf = coding->charbuf + coding->charbuf_used; |
2069 | int *charbuf_end | |
2070 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 | 2071 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2072 | int multibytep = coding->src_multibyte; |
24a73b0a | 2073 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2074 | int char_offset = coding->produced_char; |
2075 | int last_offset = char_offset; | |
2076 | int last_id = charset_ascii; | |
aa72b389 | 2077 | |
24a73b0a | 2078 | CODING_GET_INFO (coding, attrs, charset_list); |
aa72b389 | 2079 | |
aa72b389 KH |
2080 | while (1) |
2081 | { | |
df7492f9 KH |
2082 | int c; |
2083 | ||
aa72b389 | 2084 | src_base = src; |
df7492f9 KH |
2085 | consumed_chars_base = consumed_chars; |
2086 | ||
2087 | if (charbuf >= charbuf_end) | |
2088 | break; | |
aa72b389 | 2089 | |
df7492f9 | 2090 | ONE_MORE_BYTE (c); |
065e3595 KH |
2091 | if (c < 0) |
2092 | { | |
2093 | *charbuf++ = -c; | |
2094 | char_offset++; | |
2095 | } | |
2096 | else if (c < 0x80) | |
aa72b389 | 2097 | { |
df7492f9 KH |
2098 | *charbuf++ = c; |
2099 | char_offset++; | |
aa72b389 | 2100 | } |
df7492f9 KH |
2101 | else if (c == 0x80) |
2102 | { | |
df7492f9 | 2103 | ONE_MORE_BYTE (c); |
065e3595 KH |
2104 | if (c < 0) |
2105 | goto invalid_code; | |
781d7a48 KH |
2106 | if (c - 0xF2 >= COMPOSITION_RELATIVE |
2107 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) | |
df7492f9 KH |
2108 | DECODE_EMACS_MULE_21_COMPOSITION (c); |
2109 | else if (c < 0xC0) | |
2110 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c); | |
2111 | else if (c == 0xFF) | |
2112 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c); | |
2113 | else | |
2114 | goto invalid_code; | |
2115 | } | |
2116 | else if (c < 0xA0 && emacs_mule_bytes[c] > 1) | |
2117 | { | |
2118 | int nbytes, nchars; | |
ff0dacd7 KH |
2119 | int id; |
2120 | ||
781d7a48 KH |
2121 | src = src_base; |
2122 | consumed_chars = consumed_chars_base; | |
ff0dacd7 | 2123 | c = emacs_mule_char (coding, src, &nbytes, &nchars, &id); |
df7492f9 KH |
2124 | if (c < 0) |
2125 | { | |
2126 | if (c == -2) | |
2127 | break; | |
2128 | goto invalid_code; | |
2129 | } | |
ff0dacd7 KH |
2130 | if (last_id != id) |
2131 | { | |
2132 | if (last_id != charset_ascii) | |
69a80ea3 | 2133 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
2134 | last_id = id; |
2135 | last_offset = char_offset; | |
2136 | } | |
df7492f9 | 2137 | *charbuf++ = c; |
781d7a48 KH |
2138 | src += nbytes; |
2139 | consumed_chars += nchars; | |
df7492f9 KH |
2140 | char_offset++; |
2141 | } | |
4d41e8b7 KH |
2142 | else |
2143 | goto invalid_code; | |
df7492f9 KH |
2144 | continue; |
2145 | ||
2146 | invalid_code: | |
2147 | src = src_base; | |
2148 | consumed_chars = consumed_chars_base; | |
2149 | ONE_MORE_BYTE (c); | |
2150 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2151 | char_offset++; |
df7492f9 KH |
2152 | coding->errors++; |
2153 | } | |
2154 | ||
2155 | no_more_source: | |
ff0dacd7 | 2156 | if (last_id != charset_ascii) |
69a80ea3 | 2157 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2158 | coding->consumed_char += consumed_chars_base; |
2159 | coding->consumed = src_base - coding->source; | |
2160 | coding->charbuf_used = charbuf - coding->charbuf; | |
2161 | } | |
2162 | ||
2163 | ||
2164 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2165 | do { \ | |
2166 | if (id < 0xA0) \ | |
2167 | codes[0] = id, codes[1] = 0; \ | |
2168 | else if (id < 0xE0) \ | |
2169 | codes[0] = 0x9A, codes[1] = id; \ | |
2170 | else if (id < 0xF0) \ | |
2171 | codes[0] = 0x9B, codes[1] = id; \ | |
2172 | else if (id < 0xF5) \ | |
2173 | codes[0] = 0x9C, codes[1] = id; \ | |
2174 | else \ | |
2175 | codes[0] = 0x9D, codes[1] = id; \ | |
2176 | } while (0); | |
2177 | ||
aa72b389 | 2178 | |
df7492f9 KH |
2179 | static int |
2180 | encode_coding_emacs_mule (coding) | |
2181 | struct coding_system *coding; | |
2182 | { | |
2183 | int multibytep = coding->dst_multibyte; | |
2184 | int *charbuf = coding->charbuf; | |
2185 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2186 | unsigned char *dst = coding->destination + coding->produced; | |
2187 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2188 | int safe_room = 8; | |
df7492f9 | 2189 | int produced_chars = 0; |
24a73b0a | 2190 | Lisp_Object attrs, charset_list; |
df7492f9 | 2191 | int c; |
ff0dacd7 | 2192 | int preferred_charset_id = -1; |
df7492f9 | 2193 | |
24a73b0a | 2194 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2195 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2196 | { | |
2197 | CODING_ATTR_CHARSET_LIST (attrs) | |
2198 | = charset_list = Vemacs_mule_charset_list; | |
2199 | } | |
df7492f9 KH |
2200 | |
2201 | while (charbuf < charbuf_end) | |
2202 | { | |
2203 | ASSURE_DESTINATION (safe_room); | |
2204 | c = *charbuf++; | |
ff0dacd7 KH |
2205 | |
2206 | if (c < 0) | |
2207 | { | |
2208 | /* Handle an annotation. */ | |
2209 | switch (*charbuf) | |
2210 | { | |
2211 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2212 | /* Not yet implemented. */ | |
2213 | break; | |
2214 | case CODING_ANNOTATE_CHARSET_MASK: | |
2215 | preferred_charset_id = charbuf[3]; | |
2216 | if (preferred_charset_id >= 0 | |
2217 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2218 | charset_list))) | |
2219 | preferred_charset_id = -1; | |
2220 | break; | |
2221 | default: | |
2222 | abort (); | |
2223 | } | |
2224 | charbuf += -c - 1; | |
2225 | continue; | |
2226 | } | |
2227 | ||
df7492f9 KH |
2228 | if (ASCII_CHAR_P (c)) |
2229 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2230 | else if (CHAR_BYTE8_P (c)) |
2231 | { | |
2232 | c = CHAR_TO_BYTE8 (c); | |
2233 | EMIT_ONE_BYTE (c); | |
2234 | } | |
df7492f9 | 2235 | else |
aa72b389 | 2236 | { |
df7492f9 KH |
2237 | struct charset *charset; |
2238 | unsigned code; | |
2239 | int dimension; | |
2240 | int emacs_mule_id; | |
2241 | unsigned char leading_codes[2]; | |
2242 | ||
ff0dacd7 KH |
2243 | if (preferred_charset_id >= 0) |
2244 | { | |
2245 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
2246 | if (! CHAR_CHARSET_P (c, charset)) | |
2247 | charset = char_charset (c, charset_list, NULL); | |
2248 | } | |
2249 | else | |
2250 | charset = char_charset (c, charset_list, &code); | |
df7492f9 KH |
2251 | if (! charset) |
2252 | { | |
2253 | c = coding->default_char; | |
2254 | if (ASCII_CHAR_P (c)) | |
2255 | { | |
2256 | EMIT_ONE_ASCII_BYTE (c); | |
2257 | continue; | |
2258 | } | |
2259 | charset = char_charset (c, charset_list, &code); | |
2260 | } | |
2261 | dimension = CHARSET_DIMENSION (charset); | |
2262 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2263 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2264 | EMIT_ONE_BYTE (leading_codes[0]); | |
2265 | if (leading_codes[1]) | |
2266 | EMIT_ONE_BYTE (leading_codes[1]); | |
2267 | if (dimension == 1) | |
1fa663f9 | 2268 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2269 | else |
df7492f9 | 2270 | { |
1fa663f9 | 2271 | code |= 0x8080; |
df7492f9 KH |
2272 | EMIT_ONE_BYTE (code >> 8); |
2273 | EMIT_ONE_BYTE (code & 0xFF); | |
2274 | } | |
aa72b389 | 2275 | } |
aa72b389 | 2276 | } |
065e3595 | 2277 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2278 | coding->produced_char += produced_chars; |
2279 | coding->produced = dst - coding->destination; | |
2280 | return 0; | |
aa72b389 | 2281 | } |
b73bfc1c | 2282 | |
4ed46869 | 2283 | \f |
df7492f9 | 2284 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2285 | |
2286 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2287 | Since the intention of this note is to help understand the |
5a936b46 | 2288 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2289 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2290 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2291 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2292 | |
2293 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2294 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2295 | is encoded using bytes less than 128. This may make the encoded |
2296 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2297 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2298 | Significant Bit). |
b73bfc1c | 2299 | |
cfb43547 DL |
2300 | There are two kinds of character sets: control character sets and |
2301 | graphic character sets. The former contain control characters such | |
4ed46869 | 2302 | as `newline' and `escape' to provide control functions (control |
39787efd | 2303 | functions are also provided by escape sequences). The latter |
cfb43547 | 2304 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2305 | two control character sets and many graphic character sets. |
2306 | ||
2307 | Graphic character sets are classified into one of the following | |
39787efd KH |
2308 | four classes, according to the number of bytes (DIMENSION) and |
2309 | number of characters in one dimension (CHARS) of the set: | |
2310 | - DIMENSION1_CHARS94 | |
2311 | - DIMENSION1_CHARS96 | |
2312 | - DIMENSION2_CHARS94 | |
2313 | - DIMENSION2_CHARS96 | |
2314 | ||
2315 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2316 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2317 | hereafter). The <F> of each character set is decided by ECMA(*) |
2318 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2319 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2320 | |
2321 | Note (*): ECMA = European Computer Manufacturers Association | |
2322 | ||
cfb43547 | 2323 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2324 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2325 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2326 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2327 | o DIMENSION2_CHARS96 -- none for the moment | |
2328 | ||
39787efd | 2329 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2330 | C0 [0x00..0x1F] -- control character plane 0 |
2331 | GL [0x20..0x7F] -- graphic character plane 0 | |
2332 | C1 [0x80..0x9F] -- control character plane 1 | |
2333 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2334 | ||
2335 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2336 | C1 by an escape sequence. The most common case is that: |
2337 | - ISO646's control character set is designated/invoked to C0, and | |
2338 | - ISO6429's control character set is designated/invoked to C1, | |
2339 | and usually these designations/invocations are omitted in encoded | |
2340 | text. In a 7-bit environment, only C0 can be used, and a control | |
2341 | character for C1 is encoded by an appropriate escape sequence to | |
2342 | fit into the environment. All control characters for C1 are | |
2343 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2344 | |
2345 | A graphic character set is at first designated to one of four | |
2346 | graphic registers (G0 through G3), then these graphic registers are | |
2347 | invoked to GL or GR. These designations and invocations can be | |
2348 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2349 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2350 | these invocations and designations are omitted in encoded text. | |
2351 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2352 | |
39787efd KH |
2353 | When a graphic character set of CHARS94 is invoked to GL, codes |
2354 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2355 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2356 | be used. | |
4ed46869 KH |
2357 | |
2358 | There are two ways of invocation: locking-shift and single-shift. | |
2359 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2360 | invocation, whereas with single-shift, the invocation affects the |
2361 | following character only and doesn't affect the locking-shift | |
2362 | state. Invocations are done by the following control characters or | |
2363 | escape sequences: | |
4ed46869 KH |
2364 | |
2365 | ---------------------------------------------------------------------- | |
39787efd | 2366 | abbrev function cntrl escape seq description |
4ed46869 | 2367 | ---------------------------------------------------------------------- |
39787efd KH |
2368 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2369 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2370 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2371 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2372 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2373 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2374 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2375 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2376 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2377 | ---------------------------------------------------------------------- |
39787efd KH |
2378 | (*) These are not used by any known coding system. |
2379 | ||
2380 | Control characters for these functions are defined by macros | |
2381 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2382 | |
39787efd | 2383 | Designations are done by the following escape sequences: |
4ed46869 KH |
2384 | ---------------------------------------------------------------------- |
2385 | escape sequence description | |
2386 | ---------------------------------------------------------------------- | |
2387 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2388 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2389 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2390 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2391 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2392 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2393 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2394 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2395 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2396 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2397 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2398 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2399 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2400 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2401 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2402 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2403 | ---------------------------------------------------------------------- | |
2404 | ||
2405 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2406 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2407 | |
2408 | Note (*): Although these designations are not allowed in ISO2022, | |
2409 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2410 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2411 | 7-bit environment, non-locking-shift, and non-single-shift. |
2412 | ||
2413 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2414 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2415 | |
cfb43547 | 2416 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2417 | same multilingual text in ISO2022. Actually, there exist many |
2418 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2419 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2420 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2421 | localized platforms), and all of these are variants of ISO2022. |
2422 | ||
2423 | In addition to the above, Emacs handles two more kinds of escape | |
2424 | sequences: ISO6429's direction specification and Emacs' private | |
2425 | sequence for specifying character composition. | |
2426 | ||
39787efd | 2427 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2428 | o CSI ']' -- end of the current direction |
2429 | o CSI '0' ']' -- end of the current direction | |
2430 | o CSI '1' ']' -- start of left-to-right text | |
2431 | o CSI '2' ']' -- start of right-to-left text | |
2432 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2433 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2434 | ||
2435 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2436 | o ESC '0' -- start relative composition |
2437 | o ESC '1' -- end composition | |
2438 | o ESC '2' -- start rule-base composition (*) | |
2439 | o ESC '3' -- start relative composition with alternate chars (**) | |
2440 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2441 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2442 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2443 | |
5a936b46 DL |
2444 | (*) This form is used only in Emacs 20.7 and older versions, |
2445 | but newer versions can safely decode it. | |
cfb43547 | 2446 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2447 | and older versions can't decode it. |
ec6d2bb8 | 2448 | |
cfb43547 | 2449 | Here's a list of example usages of these composition escape |
b73bfc1c | 2450 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2451 | |
b73bfc1c | 2452 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2453 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2454 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2455 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2456 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2457 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2458 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2459 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 KH |
2460 | |
2461 | enum iso_code_class_type iso_code_class[256]; | |
2462 | ||
df7492f9 KH |
2463 | #define SAFE_CHARSET_P(coding, id) \ |
2464 | ((id) <= (coding)->max_charset_id \ | |
2465 | && (coding)->safe_charsets[id] >= 0) | |
2466 | ||
2467 | ||
2468 | #define SHIFT_OUT_OK(category) \ | |
2469 | (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0) | |
2470 | ||
2471 | static void | |
f0064e1f DL |
2472 | setup_iso_safe_charsets (attrs) |
2473 | Lisp_Object attrs; | |
df7492f9 KH |
2474 | { |
2475 | Lisp_Object charset_list, safe_charsets; | |
2476 | Lisp_Object request; | |
2477 | Lisp_Object reg_usage; | |
2478 | Lisp_Object tail; | |
2479 | int reg94, reg96; | |
2480 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
2481 | int max_charset_id; | |
2482 | ||
2483 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2484 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2485 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2486 | { | |
2487 | CODING_ATTR_CHARSET_LIST (attrs) | |
2488 | = charset_list = Viso_2022_charset_list; | |
2489 | ASET (attrs, coding_attr_safe_charsets, Qnil); | |
2490 | } | |
2491 | ||
2492 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
2493 | return; | |
2494 | ||
2495 | max_charset_id = 0; | |
2496 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2497 | { | |
2498 | int id = XINT (XCAR (tail)); | |
2499 | if (max_charset_id < id) | |
2500 | max_charset_id = id; | |
2501 | } | |
d46c5b12 | 2502 | |
df7492f9 KH |
2503 | safe_charsets = Fmake_string (make_number (max_charset_id + 1), |
2504 | make_number (255)); | |
2505 | request = AREF (attrs, coding_attr_iso_request); | |
2506 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
2507 | reg94 = XINT (XCAR (reg_usage)); | |
2508 | reg96 = XINT (XCDR (reg_usage)); | |
2509 | ||
2510 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2511 | { | |
2512 | Lisp_Object id; | |
2513 | Lisp_Object reg; | |
2514 | struct charset *charset; | |
2515 | ||
2516 | id = XCAR (tail); | |
2517 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 2518 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 2519 | if (! NILP (reg)) |
8f924df7 | 2520 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
2521 | else if (charset->iso_chars_96) |
2522 | { | |
2523 | if (reg96 < 4) | |
8f924df7 | 2524 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
2525 | } |
2526 | else | |
2527 | { | |
2528 | if (reg94 < 4) | |
8f924df7 | 2529 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
2530 | } |
2531 | } | |
2532 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
2533 | } | |
d46c5b12 | 2534 | |
b6871cc7 | 2535 | |
4ed46869 | 2536 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
2537 | Check if a text is encoded in one of ISO-2022 based codig systems. |
2538 | If it is, return 1, else return 0. */ | |
4ed46869 | 2539 | |
0a28aafb | 2540 | static int |
ff0dacd7 | 2541 | detect_coding_iso_2022 (coding, detect_info) |
df7492f9 | 2542 | struct coding_system *coding; |
ff0dacd7 | 2543 | struct coding_detection_info *detect_info; |
4ed46869 | 2544 | { |
8f924df7 KH |
2545 | const unsigned char *src = coding->source, *src_base = src; |
2546 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 2547 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 2548 | int single_shifting = 0; |
df7492f9 KH |
2549 | int id; |
2550 | int c, c1; | |
2551 | int consumed_chars = 0; | |
2552 | int i; | |
ff0dacd7 KH |
2553 | int rejected = 0; |
2554 | int found = 0; | |
2555 | ||
2556 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
2557 | |
2558 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
2559 | { | |
2560 | struct coding_system *this = &(coding_categories[i]); | |
2561 | Lisp_Object attrs, val; | |
2562 | ||
2563 | attrs = CODING_ID_ATTRS (this->id); | |
2564 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
2565 | && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs), Viso_2022_charset_list)) | |
2566 | setup_iso_safe_charsets (attrs); | |
2567 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 KH |
2568 | this->max_charset_id = SCHARS (val) - 1; |
2569 | this->safe_charsets = (char *) SDATA (val); | |
df7492f9 KH |
2570 | } |
2571 | ||
2572 | /* A coding system of this category is always ASCII compatible. */ | |
2573 | src += coding->head_ascii; | |
3f003981 | 2574 | |
ff0dacd7 | 2575 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 2576 | { |
065e3595 | 2577 | src_base = src; |
df7492f9 | 2578 | ONE_MORE_BYTE (c); |
4ed46869 KH |
2579 | switch (c) |
2580 | { | |
2581 | case ISO_CODE_ESC: | |
74383408 KH |
2582 | if (inhibit_iso_escape_detection) |
2583 | break; | |
f46869e4 | 2584 | single_shifting = 0; |
df7492f9 | 2585 | ONE_MORE_BYTE (c); |
d46c5b12 | 2586 | if (c >= '(' && c <= '/') |
4ed46869 | 2587 | { |
bf9cdd4e | 2588 | /* Designation sequence for a charset of dimension 1. */ |
df7492f9 | 2589 | ONE_MORE_BYTE (c1); |
d46c5b12 | 2590 | if (c1 < ' ' || c1 >= 0x80 |
df7492f9 | 2591 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) |
d46c5b12 KH |
2592 | /* Invalid designation sequence. Just ignore. */ |
2593 | break; | |
bf9cdd4e KH |
2594 | } |
2595 | else if (c == '$') | |
2596 | { | |
2597 | /* Designation sequence for a charset of dimension 2. */ | |
df7492f9 | 2598 | ONE_MORE_BYTE (c); |
bf9cdd4e KH |
2599 | if (c >= '@' && c <= 'B') |
2600 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
ff0dacd7 | 2601 | id = iso_charset_table[1][0][c]; |
bf9cdd4e | 2602 | else if (c >= '(' && c <= '/') |
bcf26d6a | 2603 | { |
df7492f9 | 2604 | ONE_MORE_BYTE (c1); |
d46c5b12 | 2605 | if (c1 < ' ' || c1 >= 0x80 |
df7492f9 | 2606 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) |
d46c5b12 KH |
2607 | /* Invalid designation sequence. Just ignore. */ |
2608 | break; | |
bcf26d6a | 2609 | } |
bf9cdd4e | 2610 | else |
ff0dacd7 | 2611 | /* Invalid designation sequence. Just ignore it. */ |
d46c5b12 KH |
2612 | break; |
2613 | } | |
ae9ff118 | 2614 | else if (c == 'N' || c == 'O') |
d46c5b12 | 2615 | { |
ae9ff118 | 2616 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
2617 | single_shifting = 1; |
2618 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
d46c5b12 | 2619 | break; |
4ed46869 | 2620 | } |
ec6d2bb8 KH |
2621 | else if (c >= '0' && c <= '4') |
2622 | { | |
2623 | /* ESC <Fp> for start/end composition. */ | |
ff0dacd7 | 2624 | found |= CATEGORY_MASK_ISO; |
ec6d2bb8 KH |
2625 | break; |
2626 | } | |
bf9cdd4e | 2627 | else |
df7492f9 | 2628 | { |
ff0dacd7 | 2629 | /* Invalid escape sequence. Just ignore it. */ |
df7492f9 KH |
2630 | break; |
2631 | } | |
d46c5b12 KH |
2632 | |
2633 | /* We found a valid designation sequence for CHARSET. */ | |
ff0dacd7 | 2634 | rejected |= CATEGORY_MASK_ISO_8BIT; |
df7492f9 KH |
2635 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], |
2636 | id)) | |
ff0dacd7 | 2637 | found |= CATEGORY_MASK_ISO_7; |
d46c5b12 | 2638 | else |
ff0dacd7 | 2639 | rejected |= CATEGORY_MASK_ISO_7; |
df7492f9 KH |
2640 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], |
2641 | id)) | |
ff0dacd7 | 2642 | found |= CATEGORY_MASK_ISO_7_TIGHT; |
d46c5b12 | 2643 | else |
ff0dacd7 | 2644 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; |
df7492f9 KH |
2645 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], |
2646 | id)) | |
ff0dacd7 | 2647 | found |= CATEGORY_MASK_ISO_7_ELSE; |
ae9ff118 | 2648 | else |
ff0dacd7 | 2649 | rejected |= CATEGORY_MASK_ISO_7_ELSE; |
df7492f9 KH |
2650 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], |
2651 | id)) | |
ff0dacd7 | 2652 | found |= CATEGORY_MASK_ISO_8_ELSE; |
ae9ff118 | 2653 | else |
ff0dacd7 | 2654 | rejected |= CATEGORY_MASK_ISO_8_ELSE; |
4ed46869 KH |
2655 | break; |
2656 | ||
4ed46869 | 2657 | case ISO_CODE_SO: |
d46c5b12 | 2658 | case ISO_CODE_SI: |
ff0dacd7 | 2659 | /* Locking shift out/in. */ |
74383408 KH |
2660 | if (inhibit_iso_escape_detection) |
2661 | break; | |
f46869e4 | 2662 | single_shifting = 0; |
ff0dacd7 KH |
2663 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
2664 | found |= CATEGORY_MASK_ISO_ELSE; | |
d46c5b12 KH |
2665 | break; |
2666 | ||
4ed46869 | 2667 | case ISO_CODE_CSI: |
ff0dacd7 | 2668 | /* Control sequence introducer. */ |
f46869e4 | 2669 | single_shifting = 0; |
ff0dacd7 KH |
2670 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
2671 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
2672 | goto check_extra_latin; | |
2673 | ||
4ed46869 KH |
2674 | case ISO_CODE_SS2: |
2675 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
2676 | /* Single shift. */ |
2677 | if (inhibit_iso_escape_detection) | |
2678 | break; | |
75e2a253 | 2679 | single_shifting = 0; |
ff0dacd7 KH |
2680 | rejected |= CATEGORY_MASK_ISO_7BIT; |
2681 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
2682 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
75e2a253 | 2683 | found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1; |
ff0dacd7 KH |
2684 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
2685 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
75e2a253 KH |
2686 | found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1; |
2687 | if (single_shifting) | |
2688 | break; | |
ff0dacd7 | 2689 | goto check_extra_latin; |
4ed46869 KH |
2690 | |
2691 | default: | |
065e3595 KH |
2692 | if (c < 0) |
2693 | continue; | |
4ed46869 | 2694 | if (c < 0x80) |
f46869e4 KH |
2695 | { |
2696 | single_shifting = 0; | |
2697 | break; | |
2698 | } | |
ff0dacd7 | 2699 | if (c >= 0xA0) |
c4825358 | 2700 | { |
ff0dacd7 KH |
2701 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
2702 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 2703 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
2704 | 0xA0..0FF. If the byte length is even, we include |
2705 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
2706 | only when we are not single shifting. */ | |
2707 | if (! single_shifting | |
2708 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 2709 | { |
e17de821 | 2710 | int i = 1; |
b73bfc1c KH |
2711 | while (src < src_end) |
2712 | { | |
df7492f9 | 2713 | ONE_MORE_BYTE (c); |
b73bfc1c KH |
2714 | if (c < 0xA0) |
2715 | break; | |
2716 | i++; | |
2717 | } | |
2718 | ||
2719 | if (i & 1 && src < src_end) | |
ff0dacd7 | 2720 | rejected |= CATEGORY_MASK_ISO_8_2; |
f46869e4 | 2721 | else |
ff0dacd7 | 2722 | found |= CATEGORY_MASK_ISO_8_2; |
f46869e4 | 2723 | } |
ff0dacd7 | 2724 | break; |
4ed46869 | 2725 | } |
ff0dacd7 KH |
2726 | check_extra_latin: |
2727 | single_shifting = 0; | |
2728 | if (! VECTORP (Vlatin_extra_code_table) | |
2729 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
2730 | { | |
2731 | rejected = CATEGORY_MASK_ISO; | |
2732 | break; | |
2733 | } | |
2734 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
2735 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
2736 | found |= CATEGORY_MASK_ISO_8_1; | |
2737 | else | |
2738 | rejected |= CATEGORY_MASK_ISO_8_1; | |
75e2a253 | 2739 | rejected |= CATEGORY_MASK_ISO_8_2; |
4ed46869 KH |
2740 | } |
2741 | } | |
ff0dacd7 KH |
2742 | detect_info->rejected |= CATEGORY_MASK_ISO; |
2743 | return 0; | |
4ed46869 | 2744 | |
df7492f9 | 2745 | no_more_source: |
ff0dacd7 KH |
2746 | detect_info->rejected |= rejected; |
2747 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 2748 | return 1; |
4ed46869 | 2749 | } |
ec6d2bb8 | 2750 | |
4ed46869 | 2751 | |
134b9549 KH |
2752 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
2753 | escape sequence should be kept. */ | |
df7492f9 KH |
2754 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
2755 | do { \ | |
2756 | int id, prev; \ | |
2757 | \ | |
2758 | if (final < '0' || final >= 128 \ | |
2759 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
2760 | || !SAFE_CHARSET_P (coding, id)) \ | |
2761 | { \ | |
2762 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
2763 | chars_96 = -1; \ |
2764 | break; \ | |
df7492f9 KH |
2765 | } \ |
2766 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
2767 | if (id == charset_jisx0201_roman) \ |
2768 | { \ | |
2769 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
2770 | id = charset_ascii; \ | |
2771 | } \ | |
2772 | else if (id == charset_jisx0208_1978) \ | |
2773 | { \ | |
2774 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
2775 | id = charset_jisx0208; \ | |
2776 | } \ | |
df7492f9 KH |
2777 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
2778 | /* If there was an invalid designation to REG previously, and this \ | |
2779 | designation is ASCII to REG, we should keep this designation \ | |
2780 | sequence. */ \ | |
2781 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 2782 | chars_96 = -1; \ |
4ed46869 KH |
2783 | } while (0) |
2784 | ||
d46c5b12 | 2785 | |
df7492f9 KH |
2786 | #define MAYBE_FINISH_COMPOSITION() \ |
2787 | do { \ | |
2788 | int i; \ | |
2789 | if (composition_state == COMPOSING_NO) \ | |
2790 | break; \ | |
2791 | /* It is assured that we have enough room for producing \ | |
2792 | characters stored in the table `components'. */ \ | |
2793 | if (charbuf + component_idx > charbuf_end) \ | |
2794 | goto no_more_source; \ | |
2795 | composition_state = COMPOSING_NO; \ | |
2796 | if (method == COMPOSITION_RELATIVE \ | |
2797 | || method == COMPOSITION_WITH_ALTCHARS) \ | |
2798 | { \ | |
2799 | for (i = 0; i < component_idx; i++) \ | |
2800 | *charbuf++ = components[i]; \ | |
2801 | char_offset += component_idx; \ | |
2802 | } \ | |
2803 | else \ | |
2804 | { \ | |
2805 | for (i = 0; i < component_idx; i += 2) \ | |
2806 | *charbuf++ = components[i]; \ | |
2807 | char_offset += (component_idx / 2) + 1; \ | |
2808 | } \ | |
2809 | } while (0) | |
2810 | ||
d46c5b12 | 2811 | |
aa72b389 KH |
2812 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
2813 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 | |
2814 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
2815 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
2816 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
aa72b389 | 2817 | */ |
ec6d2bb8 | 2818 | |
df7492f9 KH |
2819 | #define DECODE_COMPOSITION_START(c1) \ |
2820 | do { \ | |
2821 | if (c1 == '0' \ | |
781d7a48 | 2822 | && composition_state == COMPOSING_COMPONENT_RULE) \ |
df7492f9 KH |
2823 | { \ |
2824 | component_len = component_idx; \ | |
2825 | composition_state = COMPOSING_CHAR; \ | |
2826 | } \ | |
2827 | else \ | |
2828 | { \ | |
8f924df7 | 2829 | const unsigned char *p; \ |
df7492f9 KH |
2830 | \ |
2831 | MAYBE_FINISH_COMPOSITION (); \ | |
2832 | if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \ | |
2833 | goto no_more_source; \ | |
2834 | for (p = src; p < src_end - 1; p++) \ | |
2835 | if (*p == ISO_CODE_ESC && p[1] == '1') \ | |
2836 | break; \ | |
2837 | if (p == src_end - 1) \ | |
2838 | { \ | |
9286b333 KH |
2839 | /* The current composition doesn't end in the current \ |
2840 | source. */ \ | |
2841 | record_conversion_result \ | |
2842 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
df7492f9 KH |
2843 | goto no_more_source; \ |
2844 | } \ | |
2845 | \ | |
2846 | /* This is surely the start of a composition. */ \ | |
2847 | method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
2848 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
2849 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
2850 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
2851 | composition_state = (c1 <= '2' ? COMPOSING_CHAR \ | |
2852 | : COMPOSING_COMPONENT_CHAR); \ | |
2853 | component_idx = component_len = 0; \ | |
2854 | } \ | |
ec6d2bb8 KH |
2855 | } while (0) |
2856 | ||
ec6d2bb8 | 2857 | |
df7492f9 KH |
2858 | /* Handle compositoin end sequence ESC 1. */ |
2859 | ||
2860 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 2861 | do { \ |
df7492f9 KH |
2862 | int nchars = (component_len > 0 ? component_idx - component_len \ |
2863 | : method == COMPOSITION_RELATIVE ? component_idx \ | |
2864 | : (component_idx + 1) / 2); \ | |
2865 | int i; \ | |
2866 | int *saved_charbuf = charbuf; \ | |
2867 | \ | |
69a80ea3 | 2868 | ADD_COMPOSITION_DATA (charbuf, nchars, method); \ |
df7492f9 | 2869 | if (method != COMPOSITION_RELATIVE) \ |
ec6d2bb8 | 2870 | { \ |
df7492f9 KH |
2871 | if (component_len == 0) \ |
2872 | for (i = 0; i < component_idx; i++) \ | |
2873 | *charbuf++ = components[i]; \ | |
2874 | else \ | |
2875 | for (i = 0; i < component_len; i++) \ | |
2876 | *charbuf++ = components[i]; \ | |
2877 | *saved_charbuf = saved_charbuf - charbuf; \ | |
ec6d2bb8 | 2878 | } \ |
df7492f9 KH |
2879 | if (method == COMPOSITION_WITH_RULE) \ |
2880 | for (i = 0; i < component_idx; i += 2, char_offset++) \ | |
2881 | *charbuf++ = components[i]; \ | |
ec6d2bb8 | 2882 | else \ |
df7492f9 KH |
2883 | for (i = component_len; i < component_idx; i++, char_offset++) \ |
2884 | *charbuf++ = components[i]; \ | |
2885 | coding->annotated = 1; \ | |
2886 | composition_state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
2887 | } while (0) |
2888 | ||
df7492f9 | 2889 | |
ec6d2bb8 KH |
2890 | /* Decode a composition rule from the byte C1 (and maybe one more byte |
2891 | from SRC) and store one encoded composition rule in | |
2892 | coding->cmp_data. */ | |
2893 | ||
2894 | #define DECODE_COMPOSITION_RULE(c1) \ | |
2895 | do { \ | |
ec6d2bb8 KH |
2896 | (c1) -= 32; \ |
2897 | if (c1 < 81) /* old format (before ver.21) */ \ | |
2898 | { \ | |
2899 | int gref = (c1) / 9; \ | |
2900 | int nref = (c1) % 9; \ | |
2901 | if (gref == 4) gref = 10; \ | |
2902 | if (nref == 4) nref = 10; \ | |
df7492f9 | 2903 | c1 = COMPOSITION_ENCODE_RULE (gref, nref); \ |
ec6d2bb8 | 2904 | } \ |
b73bfc1c | 2905 | else if (c1 < 93) /* new format (after ver.21) */ \ |
ec6d2bb8 KH |
2906 | { \ |
2907 | ONE_MORE_BYTE (c2); \ | |
df7492f9 | 2908 | c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \ |
ec6d2bb8 | 2909 | } \ |
df7492f9 KH |
2910 | else \ |
2911 | c1 = 0; \ | |
ec6d2bb8 | 2912 | } while (0) |
88993dfd | 2913 | |
d46c5b12 | 2914 | |
4ed46869 KH |
2915 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
2916 | ||
b73bfc1c | 2917 | static void |
df7492f9 | 2918 | decode_coding_iso_2022 (coding) |
4ed46869 | 2919 | struct coding_system *coding; |
4ed46869 | 2920 | { |
8f924df7 KH |
2921 | const unsigned char *src = coding->source + coding->consumed; |
2922 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2923 | const unsigned char *src_base; | |
69a80ea3 | 2924 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ff0dacd7 | 2925 | int *charbuf_end |
69a80ea3 | 2926 | = coding->charbuf + coding->charbuf_size - 4 - MAX_ANNOTATION_LENGTH; |
df7492f9 | 2927 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2928 | int multibytep = coding->src_multibyte; |
4ed46869 | 2929 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
2930 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
2931 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 2932 | int charset_id_2, charset_id_3; |
df7492f9 KH |
2933 | struct charset *charset; |
2934 | int c; | |
2935 | /* For handling composition sequence. */ | |
2936 | #define COMPOSING_NO 0 | |
2937 | #define COMPOSING_CHAR 1 | |
2938 | #define COMPOSING_RULE 2 | |
2939 | #define COMPOSING_COMPONENT_CHAR 3 | |
2940 | #define COMPOSING_COMPONENT_RULE 4 | |
2941 | ||
2942 | int composition_state = COMPOSING_NO; | |
2943 | enum composition_method method; | |
2944 | int components[MAX_COMPOSITION_COMPONENTS * 2 + 1]; | |
2945 | int component_idx; | |
2946 | int component_len; | |
24a73b0a | 2947 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2948 | int char_offset = coding->produced_char; |
2949 | int last_offset = char_offset; | |
2950 | int last_id = charset_ascii; | |
df7492f9 | 2951 | |
24a73b0a | 2952 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 2953 | setup_iso_safe_charsets (attrs); |
287c57d7 KH |
2954 | /* Charset list may have been changed. */ |
2955 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2956 | coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs)); | |
b73bfc1c KH |
2957 | |
2958 | while (1) | |
4ed46869 | 2959 | { |
463f5630 | 2960 | int c1, c2; |
b73bfc1c KH |
2961 | |
2962 | src_base = src; | |
df7492f9 KH |
2963 | consumed_chars_base = consumed_chars; |
2964 | ||
2965 | if (charbuf >= charbuf_end) | |
2966 | break; | |
2967 | ||
b73bfc1c | 2968 | ONE_MORE_BYTE (c1); |
065e3595 KH |
2969 | if (c1 < 0) |
2970 | goto invalid_code; | |
4ed46869 | 2971 | |
98725083 | 2972 | /* We produce at most one character. */ |
4ed46869 KH |
2973 | switch (iso_code_class [c1]) |
2974 | { | |
2975 | case ISO_0x20_or_0x7F: | |
df7492f9 | 2976 | if (composition_state != COMPOSING_NO) |
ec6d2bb8 | 2977 | { |
df7492f9 KH |
2978 | if (composition_state == COMPOSING_RULE |
2979 | || composition_state == COMPOSING_COMPONENT_RULE) | |
2980 | { | |
2981 | DECODE_COMPOSITION_RULE (c1); | |
2982 | components[component_idx++] = c1; | |
2983 | composition_state--; | |
2984 | continue; | |
2985 | } | |
4ed46869 | 2986 | } |
df7492f9 KH |
2987 | if (charset_id_0 < 0 |
2988 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
2989 | /* This is SPACE or DEL. */ |
2990 | charset = CHARSET_FROM_ID (charset_ascii); | |
2991 | else | |
2992 | charset = CHARSET_FROM_ID (charset_id_0); | |
2993 | break; | |
4ed46869 KH |
2994 | |
2995 | case ISO_graphic_plane_0: | |
781d7a48 | 2996 | if (composition_state != COMPOSING_NO) |
b73bfc1c | 2997 | { |
781d7a48 KH |
2998 | if (composition_state == COMPOSING_RULE |
2999 | || composition_state == COMPOSING_COMPONENT_RULE) | |
3000 | { | |
3001 | DECODE_COMPOSITION_RULE (c1); | |
3002 | components[component_idx++] = c1; | |
3003 | composition_state--; | |
3004 | continue; | |
3005 | } | |
b73bfc1c | 3006 | } |
134b9549 KH |
3007 | if (charset_id_0 < 0) |
3008 | charset = CHARSET_FROM_ID (charset_ascii); | |
3009 | else | |
3010 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
3011 | break; |
3012 | ||
3013 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
3014 | if (charset_id_1 < 0 |
3015 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
3016 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
3017 | goto invalid_code; | |
4ed46869 KH |
3018 | /* This is a graphic character, we fall down ... */ |
3019 | ||
3020 | case ISO_graphic_plane_1: | |
df7492f9 KH |
3021 | if (charset_id_1 < 0) |
3022 | goto invalid_code; | |
3023 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
3024 | break; |
3025 | ||
df7492f9 KH |
3026 | case ISO_control_0: |
3027 | MAYBE_FINISH_COMPOSITION (); | |
3028 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
3029 | break; |
3030 | ||
df7492f9 KH |
3031 | case ISO_control_1: |
3032 | MAYBE_FINISH_COMPOSITION (); | |
3033 | goto invalid_code; | |
3034 | ||
4ed46869 | 3035 | case ISO_shift_out: |
df7492f9 KH |
3036 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3037 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3038 | goto invalid_code; | |
3039 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3040 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3041 | continue; |
4ed46869 KH |
3042 | |
3043 | case ISO_shift_in: | |
df7492f9 KH |
3044 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3045 | goto invalid_code; | |
3046 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3047 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3048 | continue; |
4ed46869 KH |
3049 | |
3050 | case ISO_single_shift_2_7: | |
3051 | case ISO_single_shift_2: | |
df7492f9 KH |
3052 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3053 | goto invalid_code; | |
4ed46869 KH |
3054 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3055 | c1 = 'N'; | |
3056 | goto label_escape_sequence; | |
3057 | ||
3058 | case ISO_single_shift_3: | |
df7492f9 KH |
3059 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3060 | goto invalid_code; | |
4ed46869 KH |
3061 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3062 | c1 = 'O'; | |
3063 | goto label_escape_sequence; | |
3064 | ||
3065 | case ISO_control_sequence_introducer: | |
3066 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3067 | c1 = '['; | |
3068 | goto label_escape_sequence; | |
3069 | ||
3070 | case ISO_escape: | |
3071 | ONE_MORE_BYTE (c1); | |
3072 | label_escape_sequence: | |
df7492f9 | 3073 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3074 | designation, direction specification, and character |
3075 | composition specification. */ | |
3076 | switch (c1) | |
3077 | { | |
3078 | case '&': /* revision of following character set */ | |
3079 | ONE_MORE_BYTE (c1); | |
3080 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3081 | goto invalid_code; |
4ed46869 KH |
3082 | ONE_MORE_BYTE (c1); |
3083 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3084 | goto invalid_code; |
4ed46869 KH |
3085 | ONE_MORE_BYTE (c1); |
3086 | goto label_escape_sequence; | |
3087 | ||
3088 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3089 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3090 | goto invalid_code; | |
134b9549 KH |
3091 | { |
3092 | int reg, chars96; | |
3093 | ||
3094 | ONE_MORE_BYTE (c1); | |
3095 | if (c1 >= '@' && c1 <= 'B') | |
3096 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3097 | or JISX0208.1980 */ |
134b9549 KH |
3098 | reg = 0, chars96 = 0; |
3099 | } | |
3100 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3101 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3102 | reg = c1 - 0x28, chars96 = 0; | |
3103 | ONE_MORE_BYTE (c1); | |
3104 | } | |
3105 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3106 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3107 | reg = c1 - 0x2C, chars96 = 1; | |
3108 | ONE_MORE_BYTE (c1); | |
3109 | } | |
3110 | else | |
3111 | goto invalid_code; | |
3112 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3113 | /* We must update these variables now. */ | |
3114 | if (reg == 0) | |
3115 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3116 | else if (reg == 1) | |
3117 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3118 | if (chars96 < 0) | |
3119 | goto invalid_code; | |
3120 | } | |
b73bfc1c | 3121 | continue; |
4ed46869 KH |
3122 | |
3123 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3124 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3125 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3126 | goto invalid_code; | |
3127 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3128 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3129 | continue; |
4ed46869 KH |
3130 | |
3131 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3132 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3133 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3134 | goto invalid_code; | |
3135 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3136 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3137 | continue; |
4ed46869 KH |
3138 | |
3139 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3140 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3141 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3142 | goto invalid_code; | |
134b9549 KH |
3143 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3144 | if (charset_id_2 < 0) | |
3145 | charset = CHARSET_FROM_ID (charset_ascii); | |
3146 | else | |
3147 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3148 | ONE_MORE_BYTE (c1); |
e7046a18 | 3149 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3150 | goto invalid_code; |
4ed46869 KH |
3151 | break; |
3152 | ||
3153 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3154 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3155 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3156 | goto invalid_code; | |
134b9549 KH |
3157 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3158 | if (charset_id_3 < 0) | |
3159 | charset = CHARSET_FROM_ID (charset_ascii); | |
3160 | else | |
3161 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3162 | ONE_MORE_BYTE (c1); |
e7046a18 | 3163 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3164 | goto invalid_code; |
4ed46869 KH |
3165 | break; |
3166 | ||
ec6d2bb8 | 3167 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3168 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3169 | goto invalid_code; | |
ec6d2bb8 | 3170 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3171 | continue; |
4ed46869 | 3172 | |
ec6d2bb8 | 3173 | case '1': /* end composition */ |
df7492f9 KH |
3174 | if (composition_state == COMPOSING_NO) |
3175 | goto invalid_code; | |
3176 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3177 | continue; |
4ed46869 KH |
3178 | |
3179 | case '[': /* specification of direction */ | |
df7492f9 KH |
3180 | if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION) |
3181 | goto invalid_code; | |
4ed46869 | 3182 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3183 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
df7492f9 | 3184 | left-to-right, and nozero means right-to-left. */ |
4ed46869 KH |
3185 | ONE_MORE_BYTE (c1); |
3186 | switch (c1) | |
3187 | { | |
3188 | case ']': /* end of the current direction */ | |
d46c5b12 | 3189 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3190 | |
3191 | case '0': /* end of the current direction */ | |
3192 | case '1': /* start of left-to-right direction */ | |
3193 | ONE_MORE_BYTE (c1); | |
3194 | if (c1 == ']') | |
d46c5b12 | 3195 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3196 | else |
df7492f9 | 3197 | goto invalid_code; |
4ed46869 KH |
3198 | break; |
3199 | ||
3200 | case '2': /* start of right-to-left direction */ | |
3201 | ONE_MORE_BYTE (c1); | |
3202 | if (c1 == ']') | |
d46c5b12 | 3203 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3204 | else |
df7492f9 | 3205 | goto invalid_code; |
4ed46869 KH |
3206 | break; |
3207 | ||
3208 | default: | |
df7492f9 | 3209 | goto invalid_code; |
4ed46869 | 3210 | } |
b73bfc1c | 3211 | continue; |
4ed46869 | 3212 | |
103e0180 | 3213 | case '%': |
103e0180 KH |
3214 | ONE_MORE_BYTE (c1); |
3215 | if (c1 == '/') | |
3216 | { | |
3217 | /* CTEXT extended segment: | |
3218 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3219 | We keep these bytes as is for the moment. | |
3220 | They may be decoded by post-read-conversion. */ | |
3221 | int dim, M, L; | |
4776e638 | 3222 | int size; |
8f924df7 | 3223 | |
103e0180 KH |
3224 | ONE_MORE_BYTE (dim); |
3225 | ONE_MORE_BYTE (M); | |
3226 | ONE_MORE_BYTE (L); | |
3227 | size = ((M - 128) * 128) + (L - 128); | |
4776e638 KH |
3228 | if (charbuf + 8 + size > charbuf_end) |
3229 | goto break_loop; | |
3230 | *charbuf++ = ISO_CODE_ESC; | |
3231 | *charbuf++ = '%'; | |
3232 | *charbuf++ = '/'; | |
3233 | *charbuf++ = dim; | |
3234 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3235 | *charbuf++ = BYTE8_TO_CHAR (L); | |
103e0180 KH |
3236 | while (size-- > 0) |
3237 | { | |
3238 | ONE_MORE_BYTE (c1); | |
4776e638 | 3239 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
103e0180 | 3240 | } |
103e0180 KH |
3241 | } |
3242 | else if (c1 == 'G') | |
3243 | { | |
103e0180 KH |
3244 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3245 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3246 | We keep these bytes as is for the moment. | |
3247 | They may be decoded by post-read-conversion. */ | |
4776e638 KH |
3248 | int *p = charbuf; |
3249 | ||
3250 | if (p + 6 > charbuf_end) | |
3251 | goto break_loop; | |
3252 | *p++ = ISO_CODE_ESC; | |
3253 | *p++ = '%'; | |
3254 | *p++ = 'G'; | |
3255 | while (p < charbuf_end) | |
103e0180 KH |
3256 | { |
3257 | ONE_MORE_BYTE (c1); | |
3258 | if (c1 == ISO_CODE_ESC | |
3259 | && src + 1 < src_end | |
3260 | && src[0] == '%' | |
3261 | && src[1] == '@') | |
9ffd559c KH |
3262 | { |
3263 | src += 2; | |
3264 | break; | |
3265 | } | |
4776e638 | 3266 | *p++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
103e0180 | 3267 | } |
4776e638 KH |
3268 | if (p + 3 > charbuf_end) |
3269 | goto break_loop; | |
3270 | *p++ = ISO_CODE_ESC; | |
3271 | *p++ = '%'; | |
3272 | *p++ = '@'; | |
3273 | charbuf = p; | |
103e0180 KH |
3274 | } |
3275 | else | |
4776e638 | 3276 | goto invalid_code; |
103e0180 | 3277 | continue; |
4776e638 | 3278 | break; |
103e0180 | 3279 | |
4ed46869 | 3280 | default: |
df7492f9 KH |
3281 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3282 | goto invalid_code; | |
134b9549 KH |
3283 | { |
3284 | int reg, chars96; | |
3285 | ||
3286 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3287 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3288 | reg = c1 - 0x28, chars96 = 0; | |
3289 | ONE_MORE_BYTE (c1); | |
3290 | } | |
3291 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3292 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3293 | reg = c1 - 0x2C, chars96 = 1; | |
3294 | ONE_MORE_BYTE (c1); | |
3295 | } | |
3296 | else | |
3297 | goto invalid_code; | |
3298 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3299 | /* We must update these variables now. */ | |
3300 | if (reg == 0) | |
3301 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3302 | else if (reg == 1) | |
3303 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3304 | if (chars96 < 0) | |
3305 | goto invalid_code; | |
3306 | } | |
b73bfc1c | 3307 | continue; |
4ed46869 | 3308 | } |
b73bfc1c | 3309 | } |
4ed46869 | 3310 | |
ff0dacd7 KH |
3311 | if (charset->id != charset_ascii |
3312 | && last_id != charset->id) | |
3313 | { | |
3314 | if (last_id != charset_ascii) | |
69a80ea3 | 3315 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3316 | last_id = charset->id; |
3317 | last_offset = char_offset; | |
3318 | } | |
3319 | ||
b73bfc1c | 3320 | /* Now we know CHARSET and 1st position code C1 of a character. |
df7492f9 KH |
3321 | Produce a decoded character while getting 2nd position code |
3322 | C2 if necessary. */ | |
3323 | c1 &= 0x7F; | |
3324 | if (CHARSET_DIMENSION (charset) > 1) | |
b73bfc1c KH |
3325 | { |
3326 | ONE_MORE_BYTE (c2); | |
df7492f9 | 3327 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)) |
b73bfc1c | 3328 | /* C2 is not in a valid range. */ |
df7492f9 KH |
3329 | goto invalid_code; |
3330 | c1 = (c1 << 8) | (c2 & 0x7F); | |
3331 | if (CHARSET_DIMENSION (charset) > 2) | |
3332 | { | |
3333 | ONE_MORE_BYTE (c2); | |
3334 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0)) | |
3335 | /* C2 is not in a valid range. */ | |
3336 | goto invalid_code; | |
3337 | c1 = (c1 << 8) | (c2 & 0x7F); | |
3338 | } | |
3339 | } | |
3340 | ||
3341 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); | |
3342 | if (c < 0) | |
3343 | { | |
3344 | MAYBE_FINISH_COMPOSITION (); | |
3345 | for (; src_base < src; src_base++, char_offset++) | |
3346 | { | |
3347 | if (ASCII_BYTE_P (*src_base)) | |
3348 | *charbuf++ = *src_base; | |
3349 | else | |
3350 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
3351 | } | |
3352 | } | |
3353 | else if (composition_state == COMPOSING_NO) | |
3354 | { | |
3355 | *charbuf++ = c; | |
3356 | char_offset++; | |
4ed46869 | 3357 | } |
df7492f9 | 3358 | else |
781d7a48 KH |
3359 | { |
3360 | components[component_idx++] = c; | |
3361 | if (method == COMPOSITION_WITH_RULE | |
3362 | || (method == COMPOSITION_WITH_RULE_ALTCHARS | |
3363 | && composition_state == COMPOSING_COMPONENT_CHAR)) | |
3364 | composition_state++; | |
4ed46869 KH |
3365 | } |
3366 | continue; | |
3367 | ||
df7492f9 KH |
3368 | invalid_code: |
3369 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 3370 | src = src_base; |
df7492f9 KH |
3371 | consumed_chars = consumed_chars_base; |
3372 | ONE_MORE_BYTE (c); | |
065e3595 | 3373 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 3374 | char_offset++; |
df7492f9 | 3375 | coding->errors++; |
4776e638 KH |
3376 | continue; |
3377 | ||
3378 | break_loop: | |
3379 | break; | |
4ed46869 | 3380 | } |
fb88bf2d | 3381 | |
df7492f9 | 3382 | no_more_source: |
ff0dacd7 | 3383 | if (last_id != charset_ascii) |
69a80ea3 | 3384 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
3385 | coding->consumed_char += consumed_chars_base; |
3386 | coding->consumed = src_base - coding->source; | |
3387 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
3388 | } |
3389 | ||
b73bfc1c | 3390 | |
f4dee582 | 3391 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
3392 | |
3393 | /* | |
f4dee582 | 3394 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 3395 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 3396 | variant has the following specifications: |
df7492f9 | 3397 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
3398 | 2. Allows short-form designation? |
3399 | 3. ASCII should be designated to G0 before control characters? | |
3400 | 4. ASCII should be designated to G0 at end of line? | |
3401 | 5. 7-bit environment or 8-bit environment? | |
3402 | 6. Use locking-shift? | |
3403 | 7. Use Single-shift? | |
3404 | And the following two are only for Japanese: | |
3405 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
3406 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
3407 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
3408 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 3409 | details. |
4ed46869 KH |
3410 | */ |
3411 | ||
3412 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
3413 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
3414 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
3415 | designation sequence of short-form. */ | |
4ed46869 KH |
3416 | |
3417 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
3418 | do { \ | |
df7492f9 | 3419 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
4ed46869 KH |
3420 | char *intermediate_char_94 = "()*+"; \ |
3421 | char *intermediate_char_96 = ",-./"; \ | |
df7492f9 KH |
3422 | int revision = -1; \ |
3423 | int c; \ | |
3424 | \ | |
3425 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 3426 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
3427 | \ |
3428 | if (revision >= 0) \ | |
70c22245 | 3429 | { \ |
df7492f9 KH |
3430 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
3431 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 3432 | } \ |
df7492f9 | 3433 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
3434 | if (CHARSET_DIMENSION (charset) == 1) \ |
3435 | { \ | |
df7492f9 KH |
3436 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
3437 | c = intermediate_char_94[reg]; \ | |
4ed46869 | 3438 | else \ |
df7492f9 KH |
3439 | c = intermediate_char_96[reg]; \ |
3440 | EMIT_ONE_ASCII_BYTE (c); \ | |
4ed46869 KH |
3441 | } \ |
3442 | else \ | |
3443 | { \ | |
df7492f9 KH |
3444 | EMIT_ONE_ASCII_BYTE ('$'); \ |
3445 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 3446 | { \ |
df7492f9 | 3447 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
3448 | || reg != 0 \ |
3449 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 3450 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
3451 | } \ |
3452 | else \ | |
df7492f9 | 3453 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 3454 | } \ |
df7492f9 KH |
3455 | EMIT_ONE_ASCII_BYTE (final_char); \ |
3456 | \ | |
3457 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
3458 | } while (0) |
3459 | ||
df7492f9 | 3460 | |
4ed46869 KH |
3461 | /* The following two macros produce codes (control character or escape |
3462 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
3463 | single-shift-3). */ | |
3464 | ||
df7492f9 KH |
3465 | #define ENCODE_SINGLE_SHIFT_2 \ |
3466 | do { \ | |
3467 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
3468 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
3469 | else \ | |
3470 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
3471 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
3472 | } while (0) |
3473 | ||
df7492f9 KH |
3474 | |
3475 | #define ENCODE_SINGLE_SHIFT_3 \ | |
3476 | do { \ | |
3477 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
3478 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
3479 | else \ | |
3480 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
3481 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
3482 | } while (0) |
3483 | ||
df7492f9 | 3484 | |
4ed46869 KH |
3485 | /* The following four macros produce codes (control character or |
3486 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
3487 | shift-out, locking-shift-2, and locking-shift-3). */ | |
3488 | ||
df7492f9 KH |
3489 | #define ENCODE_SHIFT_IN \ |
3490 | do { \ | |
3491 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
3492 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
3493 | } while (0) |
3494 | ||
df7492f9 KH |
3495 | |
3496 | #define ENCODE_SHIFT_OUT \ | |
3497 | do { \ | |
3498 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
3499 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
3500 | } while (0) |
3501 | ||
df7492f9 KH |
3502 | |
3503 | #define ENCODE_LOCKING_SHIFT_2 \ | |
3504 | do { \ | |
3505 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
3506 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
3507 | } while (0) |
3508 | ||
df7492f9 KH |
3509 | |
3510 | #define ENCODE_LOCKING_SHIFT_3 \ | |
3511 | do { \ | |
3512 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
3513 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
3514 | } while (0) |
3515 | ||
df7492f9 | 3516 | |
f4dee582 RS |
3517 | /* Produce codes for a DIMENSION1 character whose character set is |
3518 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
3519 | sequences are also produced in advance if necessary. */ |
3520 | ||
6e85d753 KH |
3521 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
3522 | do { \ | |
df7492f9 | 3523 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
3524 | \ |
3525 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3526 | && id == charset_ascii) \ | |
3527 | { \ | |
3528 | id = charset_jisx0201_roman; \ | |
3529 | charset = CHARSET_FROM_ID (id); \ | |
3530 | } \ | |
3531 | \ | |
df7492f9 | 3532 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 3533 | { \ |
df7492f9 KH |
3534 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
3535 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 3536 | else \ |
df7492f9 KH |
3537 | EMIT_ONE_BYTE (c1 | 0x80); \ |
3538 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
3539 | break; \ |
3540 | } \ | |
df7492f9 | 3541 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 3542 | { \ |
df7492f9 | 3543 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
3544 | break; \ |
3545 | } \ | |
df7492f9 | 3546 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 3547 | { \ |
df7492f9 | 3548 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
3549 | break; \ |
3550 | } \ | |
6e85d753 KH |
3551 | else \ |
3552 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
3553 | must invoke it, or, at first, designate it to some graphic \ | |
3554 | register. Then repeat the loop to actually produce the \ | |
3555 | character. */ \ | |
df7492f9 KH |
3556 | dst = encode_invocation_designation (charset, coding, dst, \ |
3557 | &produced_chars); \ | |
4ed46869 KH |
3558 | } while (1) |
3559 | ||
df7492f9 | 3560 | |
f4dee582 RS |
3561 | /* Produce codes for a DIMENSION2 character whose character set is |
3562 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
3563 | invocation codes are also produced in advance if necessary. */ |
3564 | ||
6e85d753 KH |
3565 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
3566 | do { \ | |
df7492f9 | 3567 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
3568 | \ |
3569 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3570 | && id == charset_jisx0208) \ | |
3571 | { \ | |
3572 | id = charset_jisx0208_1978; \ | |
3573 | charset = CHARSET_FROM_ID (id); \ | |
3574 | } \ | |
3575 | \ | |
df7492f9 | 3576 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 3577 | { \ |
df7492f9 KH |
3578 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
3579 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 3580 | else \ |
df7492f9 KH |
3581 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
3582 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
3583 | break; \ |
3584 | } \ | |
df7492f9 | 3585 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 3586 | { \ |
df7492f9 | 3587 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
3588 | break; \ |
3589 | } \ | |
df7492f9 | 3590 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 3591 | { \ |
df7492f9 | 3592 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
3593 | break; \ |
3594 | } \ | |
6e85d753 KH |
3595 | else \ |
3596 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
3597 | must invoke it, or, at first, designate it to some graphic \ | |
3598 | register. Then repeat the loop to actually produce the \ | |
3599 | character. */ \ | |
df7492f9 KH |
3600 | dst = encode_invocation_designation (charset, coding, dst, \ |
3601 | &produced_chars); \ | |
4ed46869 KH |
3602 | } while (1) |
3603 | ||
05e6f5dc | 3604 | |
df7492f9 KH |
3605 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
3606 | do { \ | |
3607 | int code = ENCODE_CHAR ((charset),(c)); \ | |
3608 | \ | |
3609 | if (CHARSET_DIMENSION (charset) == 1) \ | |
3610 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
3611 | else \ | |
3612 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 3613 | } while (0) |
bdd9fb48 | 3614 | |
05e6f5dc | 3615 | |
4ed46869 | 3616 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 3617 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
3618 | Return new DST. */ |
3619 | ||
3620 | unsigned char * | |
df7492f9 KH |
3621 | encode_invocation_designation (charset, coding, dst, p_nchars) |
3622 | struct charset *charset; | |
4ed46869 KH |
3623 | struct coding_system *coding; |
3624 | unsigned char *dst; | |
df7492f9 | 3625 | int *p_nchars; |
4ed46869 | 3626 | { |
df7492f9 KH |
3627 | int multibytep = coding->dst_multibyte; |
3628 | int produced_chars = *p_nchars; | |
4ed46869 | 3629 | int reg; /* graphic register number */ |
df7492f9 | 3630 | int id = CHARSET_ID (charset); |
4ed46869 KH |
3631 | |
3632 | /* At first, check designations. */ | |
3633 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 3634 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
3635 | break; |
3636 | ||
3637 | if (reg >= 4) | |
3638 | { | |
3639 | /* CHARSET is not yet designated to any graphic registers. */ | |
3640 | /* At first check the requested designation. */ | |
df7492f9 KH |
3641 | reg = CODING_ISO_REQUEST (coding, id); |
3642 | if (reg < 0) | |
1ba9e4ab KH |
3643 | /* Since CHARSET requests no special designation, designate it |
3644 | to graphic register 0. */ | |
4ed46869 KH |
3645 | reg = 0; |
3646 | ||
3647 | ENCODE_DESIGNATION (charset, reg, coding); | |
3648 | } | |
3649 | ||
df7492f9 KH |
3650 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
3651 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
3652 | { |
3653 | /* Since the graphic register REG is not invoked to any graphic | |
3654 | planes, invoke it to graphic plane 0. */ | |
3655 | switch (reg) | |
3656 | { | |
3657 | case 0: /* graphic register 0 */ | |
3658 | ENCODE_SHIFT_IN; | |
3659 | break; | |
3660 | ||
3661 | case 1: /* graphic register 1 */ | |
3662 | ENCODE_SHIFT_OUT; | |
3663 | break; | |
3664 | ||
3665 | case 2: /* graphic register 2 */ | |
df7492f9 | 3666 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
3667 | ENCODE_SINGLE_SHIFT_2; |
3668 | else | |
3669 | ENCODE_LOCKING_SHIFT_2; | |
3670 | break; | |
3671 | ||
3672 | case 3: /* graphic register 3 */ | |
df7492f9 | 3673 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
3674 | ENCODE_SINGLE_SHIFT_3; |
3675 | else | |
3676 | ENCODE_LOCKING_SHIFT_3; | |
3677 | break; | |
3678 | } | |
3679 | } | |
b73bfc1c | 3680 | |
df7492f9 | 3681 | *p_nchars = produced_chars; |
4ed46869 KH |
3682 | return dst; |
3683 | } | |
3684 | ||
df7492f9 KH |
3685 | /* The following three macros produce codes for indicating direction |
3686 | of text. */ | |
3687 | #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \ | |
ec6d2bb8 | 3688 | do { \ |
df7492f9 KH |
3689 | if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \ |
3690 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \ | |
ec6d2bb8 | 3691 | else \ |
df7492f9 | 3692 | EMIT_ONE_BYTE (ISO_CODE_CSI); \ |
ec6d2bb8 KH |
3693 | } while (0) |
3694 | ||
ec6d2bb8 | 3695 | |
df7492f9 KH |
3696 | #define ENCODE_DIRECTION_R2L() \ |
3697 | do { \ | |
3698 | ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \ | |
3699 | EMIT_TWO_ASCII_BYTES ('2', ']'); \ | |
ec6d2bb8 KH |
3700 | } while (0) |
3701 | ||
ec6d2bb8 | 3702 | |
df7492f9 | 3703 | #define ENCODE_DIRECTION_L2R() \ |
ec6d2bb8 | 3704 | do { \ |
df7492f9 KH |
3705 | ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \ |
3706 | EMIT_TWO_ASCII_BYTES ('0', ']'); \ | |
ec6d2bb8 | 3707 | } while (0) |
4ed46869 | 3708 | |
4ed46869 KH |
3709 | |
3710 | /* Produce codes for designation and invocation to reset the graphic | |
3711 | planes and registers to initial state. */ | |
df7492f9 KH |
3712 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
3713 | do { \ | |
3714 | int reg; \ | |
3715 | struct charset *charset; \ | |
3716 | \ | |
3717 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
3718 | ENCODE_SHIFT_IN; \ | |
3719 | for (reg = 0; reg < 4; reg++) \ | |
3720 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
3721 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
3722 | != CODING_ISO_INITIAL (coding, reg))) \ | |
3723 | { \ | |
3724 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
3725 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
3726 | } \ | |
4ed46869 KH |
3727 | } while (0) |
3728 | ||
df7492f9 | 3729 | |
bdd9fb48 | 3730 | /* Produce designation sequences of charsets in the line started from |
b73bfc1c | 3731 | SRC to a place pointed by DST, and return updated DST. |
bdd9fb48 KH |
3732 | |
3733 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
3734 | find all the necessary designations. */ |
3735 | ||
b73bfc1c | 3736 | static unsigned char * |
df7492f9 | 3737 | encode_designation_at_bol (coding, charbuf, charbuf_end, dst) |
e0e989f6 | 3738 | struct coding_system *coding; |
df7492f9 KH |
3739 | int *charbuf, *charbuf_end; |
3740 | unsigned char *dst; | |
e0e989f6 | 3741 | { |
df7492f9 | 3742 | struct charset *charset; |
bdd9fb48 KH |
3743 | /* Table of charsets to be designated to each graphic register. */ |
3744 | int r[4]; | |
df7492f9 KH |
3745 | int c, found = 0, reg; |
3746 | int produced_chars = 0; | |
3747 | int multibytep = coding->dst_multibyte; | |
3748 | Lisp_Object attrs; | |
3749 | Lisp_Object charset_list; | |
3750 | ||
3751 | attrs = CODING_ID_ATTRS (coding->id); | |
3752 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
3753 | if (EQ (charset_list, Qiso_2022)) | |
3754 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
3755 | |
3756 | for (reg = 0; reg < 4; reg++) | |
3757 | r[reg] = -1; | |
3758 | ||
b73bfc1c | 3759 | while (found < 4) |
e0e989f6 | 3760 | { |
df7492f9 KH |
3761 | int id; |
3762 | ||
3763 | c = *charbuf++; | |
b73bfc1c KH |
3764 | if (c == '\n') |
3765 | break; | |
df7492f9 KH |
3766 | charset = char_charset (c, charset_list, NULL); |
3767 | id = CHARSET_ID (charset); | |
3768 | reg = CODING_ISO_REQUEST (coding, id); | |
3769 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
3770 | { |
3771 | found++; | |
df7492f9 | 3772 | r[reg] = id; |
bdd9fb48 | 3773 | } |
bdd9fb48 KH |
3774 | } |
3775 | ||
3776 | if (found) | |
3777 | { | |
3778 | for (reg = 0; reg < 4; reg++) | |
3779 | if (r[reg] >= 0 | |
df7492f9 KH |
3780 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
3781 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 3782 | } |
b73bfc1c KH |
3783 | |
3784 | return dst; | |
e0e989f6 KH |
3785 | } |
3786 | ||
4ed46869 KH |
3787 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
3788 | ||
df7492f9 KH |
3789 | static int |
3790 | encode_coding_iso_2022 (coding) | |
4ed46869 | 3791 | struct coding_system *coding; |
4ed46869 | 3792 | { |
df7492f9 KH |
3793 | int multibytep = coding->dst_multibyte; |
3794 | int *charbuf = coding->charbuf; | |
3795 | int *charbuf_end = charbuf + coding->charbuf_used; | |
3796 | unsigned char *dst = coding->destination + coding->produced; | |
3797 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
3798 | int safe_room = 16; | |
3799 | int bol_designation | |
3800 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL | |
3801 | && CODING_ISO_BOL (coding)); | |
3802 | int produced_chars = 0; | |
3803 | Lisp_Object attrs, eol_type, charset_list; | |
3804 | int ascii_compatible; | |
b73bfc1c | 3805 | int c; |
ff0dacd7 | 3806 | int preferred_charset_id = -1; |
05e6f5dc | 3807 | |
24a73b0a KH |
3808 | CODING_GET_INFO (coding, attrs, charset_list); |
3809 | eol_type = CODING_ID_EOL_TYPE (coding->id); | |
3810 | if (VECTORP (eol_type)) | |
3811 | eol_type = Qunix; | |
3812 | ||
004068e4 | 3813 | setup_iso_safe_charsets (attrs); |
ff0dacd7 | 3814 | /* Charset list may have been changed. */ |
287c57d7 | 3815 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); |
8f924df7 | 3816 | coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs)); |
0eecad43 | 3817 | |
df7492f9 | 3818 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
bdd9fb48 | 3819 | |
df7492f9 | 3820 | while (charbuf < charbuf_end) |
4ed46869 | 3821 | { |
df7492f9 | 3822 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 3823 | |
df7492f9 | 3824 | if (bol_designation) |
b73bfc1c | 3825 | { |
df7492f9 | 3826 | unsigned char *dst_prev = dst; |
4ed46869 | 3827 | |
bdd9fb48 | 3828 | /* We have to produce designation sequences if any now. */ |
df7492f9 KH |
3829 | dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst); |
3830 | bol_designation = 0; | |
3831 | /* We are sure that designation sequences are all ASCII bytes. */ | |
3832 | produced_chars += dst - dst_prev; | |
e0e989f6 KH |
3833 | } |
3834 | ||
df7492f9 | 3835 | c = *charbuf++; |
ec6d2bb8 | 3836 | |
ff0dacd7 KH |
3837 | if (c < 0) |
3838 | { | |
3839 | /* Handle an annotation. */ | |
3840 | switch (*charbuf) | |
ec6d2bb8 | 3841 | { |
ff0dacd7 KH |
3842 | case CODING_ANNOTATE_COMPOSITION_MASK: |
3843 | /* Not yet implemented. */ | |
3844 | break; | |
3845 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 3846 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
3847 | if (preferred_charset_id >= 0 |
3848 | && NILP (Fmemq (make_number (preferred_charset_id), | |
3849 | charset_list))) | |
3850 | preferred_charset_id = -1; | |
3851 | break; | |
3852 | default: | |
3853 | abort (); | |
4ed46869 | 3854 | } |
ff0dacd7 KH |
3855 | charbuf += -c - 1; |
3856 | continue; | |
4ed46869 | 3857 | } |
ec6d2bb8 | 3858 | |
b73bfc1c KH |
3859 | /* Now encode the character C. */ |
3860 | if (c < 0x20 || c == 0x7F) | |
3861 | { | |
df7492f9 KH |
3862 | if (c == '\n' |
3863 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 3864 | { |
df7492f9 KH |
3865 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
3866 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
3867 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 3868 | { |
df7492f9 KH |
3869 | int i; |
3870 | ||
3871 | for (i = 0; i < 4; i++) | |
3872 | CODING_ISO_DESIGNATION (coding, i) | |
3873 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 3874 | } |
df7492f9 KH |
3875 | bol_designation |
3876 | = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL; | |
19a8d9e0 | 3877 | } |
df7492f9 KH |
3878 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
3879 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
3880 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 3881 | } |
df7492f9 | 3882 | else if (ASCII_CHAR_P (c)) |
88993dfd | 3883 | { |
df7492f9 KH |
3884 | if (ascii_compatible) |
3885 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 3886 | else |
19a8d9e0 | 3887 | { |
bf16eb23 KH |
3888 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
3889 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 3890 | } |
4ed46869 | 3891 | } |
16eafb5d | 3892 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 3893 | { |
16eafb5d KH |
3894 | c = CHAR_TO_BYTE8 (c); |
3895 | EMIT_ONE_BYTE (c); | |
88993dfd | 3896 | } |
b73bfc1c | 3897 | else |
df7492f9 | 3898 | { |
ff0dacd7 | 3899 | struct charset *charset; |
b73bfc1c | 3900 | |
ff0dacd7 KH |
3901 | if (preferred_charset_id >= 0) |
3902 | { | |
3903 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
3904 | if (! CHAR_CHARSET_P (c, charset)) | |
3905 | charset = char_charset (c, charset_list, NULL); | |
3906 | } | |
3907 | else | |
3908 | charset = char_charset (c, charset_list, NULL); | |
df7492f9 KH |
3909 | if (!charset) |
3910 | { | |
41cbe562 KH |
3911 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
3912 | { | |
3913 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
3914 | charset = CHARSET_FROM_ID (charset_ascii); | |
3915 | } | |
3916 | else | |
3917 | { | |
3918 | c = coding->default_char; | |
3919 | charset = char_charset (c, charset_list, NULL); | |
3920 | } | |
df7492f9 KH |
3921 | } |
3922 | ENCODE_ISO_CHARACTER (charset, c); | |
3923 | } | |
84fbb8a0 | 3924 | } |
b73bfc1c | 3925 | |
df7492f9 KH |
3926 | if (coding->mode & CODING_MODE_LAST_BLOCK |
3927 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
3928 | { | |
3929 | ASSURE_DESTINATION (safe_room); | |
3930 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
3931 | } | |
065e3595 | 3932 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
3933 | CODING_ISO_BOL (coding) = bol_designation; |
3934 | coding->produced_char += produced_chars; | |
3935 | coding->produced = dst - coding->destination; | |
3936 | return 0; | |
4ed46869 KH |
3937 | } |
3938 | ||
3939 | \f | |
df7492f9 | 3940 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 3941 | |
df7492f9 | 3942 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
3943 | quite widely. So, for the moment, Emacs supports them in the bare |
3944 | C code. But, in the future, they may be supported only by CCL. */ | |
3945 | ||
3946 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
3947 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
3948 | as is. A character of charset katakana-jisx0201 is encoded by | |
3949 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
3950 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 3951 | so that it fit in the range below. |
4ed46869 KH |
3952 | |
3953 | --- CODE RANGE of SJIS --- | |
3954 | (character set) (range) | |
3955 | ASCII 0x00 .. 0x7F | |
df7492f9 | 3956 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 3957 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 3958 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
3959 | ------------------------------- |
3960 | ||
3961 | */ | |
3962 | ||
3963 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
3964 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 3965 | character set and is encoded in two-byte. |
4ed46869 KH |
3966 | |
3967 | --- CODE RANGE of BIG5 --- | |
3968 | (character set) (range) | |
3969 | ASCII 0x00 .. 0x7F | |
3970 | Big5 (1st byte) 0xA1 .. 0xFE | |
3971 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
3972 | -------------------------- | |
3973 | ||
df7492f9 | 3974 | */ |
4ed46869 KH |
3975 | |
3976 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
3977 | Check if a text is encoded in SJIS. If it is, return | |
df7492f9 | 3978 | CATEGORY_MASK_SJIS, else return 0. */ |
4ed46869 | 3979 | |
0a28aafb | 3980 | static int |
ff0dacd7 | 3981 | detect_coding_sjis (coding, detect_info) |
df7492f9 | 3982 | struct coding_system *coding; |
ff0dacd7 | 3983 | struct coding_detection_info *detect_info; |
4ed46869 | 3984 | { |
065e3595 | 3985 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 3986 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
3987 | int multibytep = coding->src_multibyte; |
3988 | int consumed_chars = 0; | |
3989 | int found = 0; | |
b73bfc1c | 3990 | int c; |
df7492f9 | 3991 | |
ff0dacd7 | 3992 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
3993 | /* A coding system of this category is always ASCII compatible. */ |
3994 | src += coding->head_ascii; | |
4ed46869 | 3995 | |
b73bfc1c | 3996 | while (1) |
4ed46869 | 3997 | { |
065e3595 | 3998 | src_base = src; |
df7492f9 | 3999 | ONE_MORE_BYTE (c); |
682169fe KH |
4000 | if (c < 0x80) |
4001 | continue; | |
df7492f9 | 4002 | if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF)) |
4ed46869 | 4003 | { |
df7492f9 | 4004 | ONE_MORE_BYTE (c); |
682169fe | 4005 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 4006 | break; |
ff0dacd7 | 4007 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 4008 | } |
df7492f9 | 4009 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 4010 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
4011 | else |
4012 | break; | |
4ed46869 | 4013 | } |
ff0dacd7 | 4014 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4015 | return 0; |
4016 | ||
4017 | no_more_source: | |
065e3595 | 4018 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4019 | { |
ff0dacd7 | 4020 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 4021 | return 0; |
4ed46869 | 4022 | } |
ff0dacd7 KH |
4023 | detect_info->found |= found; |
4024 | return 1; | |
4ed46869 KH |
4025 | } |
4026 | ||
4027 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4028 | Check if a text is encoded in BIG5. If it is, return | |
df7492f9 | 4029 | CATEGORY_MASK_BIG5, else return 0. */ |
4ed46869 | 4030 | |
0a28aafb | 4031 | static int |
ff0dacd7 | 4032 | detect_coding_big5 (coding, detect_info) |
df7492f9 | 4033 | struct coding_system *coding; |
ff0dacd7 | 4034 | struct coding_detection_info *detect_info; |
4ed46869 | 4035 | { |
065e3595 | 4036 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4037 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4038 | int multibytep = coding->src_multibyte; |
4039 | int consumed_chars = 0; | |
4040 | int found = 0; | |
b73bfc1c | 4041 | int c; |
fa42c37f | 4042 | |
ff0dacd7 | 4043 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4044 | /* A coding system of this category is always ASCII compatible. */ |
4045 | src += coding->head_ascii; | |
fa42c37f | 4046 | |
b73bfc1c | 4047 | while (1) |
fa42c37f | 4048 | { |
065e3595 | 4049 | src_base = src; |
df7492f9 KH |
4050 | ONE_MORE_BYTE (c); |
4051 | if (c < 0x80) | |
fa42c37f | 4052 | continue; |
df7492f9 | 4053 | if (c >= 0xA1) |
fa42c37f | 4054 | { |
df7492f9 KH |
4055 | ONE_MORE_BYTE (c); |
4056 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4057 | return 0; |
ff0dacd7 | 4058 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4059 | } |
df7492f9 KH |
4060 | else |
4061 | break; | |
fa42c37f | 4062 | } |
ff0dacd7 | 4063 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4064 | return 0; |
fa42c37f | 4065 | |
df7492f9 | 4066 | no_more_source: |
065e3595 | 4067 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4068 | { |
ff0dacd7 | 4069 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4070 | return 0; |
4071 | } | |
ff0dacd7 KH |
4072 | detect_info->found |= found; |
4073 | return 1; | |
fa42c37f KH |
4074 | } |
4075 | ||
4ed46869 KH |
4076 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". |
4077 | If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ | |
fa42c37f | 4078 | |
b73bfc1c | 4079 | static void |
df7492f9 | 4080 | decode_coding_sjis (coding) |
4ed46869 | 4081 | struct coding_system *coding; |
4ed46869 | 4082 | { |
8f924df7 KH |
4083 | const unsigned char *src = coding->source + coding->consumed; |
4084 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4085 | const unsigned char *src_base; | |
69a80ea3 KH |
4086 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4087 | int *charbuf_end | |
4088 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4089 | int consumed_chars = 0, consumed_chars_base; |
4090 | int multibytep = coding->src_multibyte; | |
4091 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4092 | struct charset *charset_kanji2; |
24a73b0a | 4093 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4094 | int char_offset = coding->produced_char; |
4095 | int last_offset = char_offset; | |
4096 | int last_id = charset_ascii; | |
a5d301df | 4097 | |
24a73b0a | 4098 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4099 | |
4100 | val = charset_list; | |
4101 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4102 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4103 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4104 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4105 | |
b73bfc1c | 4106 | while (1) |
4ed46869 | 4107 | { |
df7492f9 | 4108 | int c, c1; |
24a73b0a | 4109 | struct charset *charset; |
fa42c37f | 4110 | |
b73bfc1c | 4111 | src_base = src; |
df7492f9 | 4112 | consumed_chars_base = consumed_chars; |
fa42c37f | 4113 | |
df7492f9 KH |
4114 | if (charbuf >= charbuf_end) |
4115 | break; | |
4116 | ||
4117 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4118 | if (c < 0) |
4119 | goto invalid_code; | |
24a73b0a KH |
4120 | if (c < 0x80) |
4121 | charset = charset_roman; | |
57a47f8a | 4122 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4123 | goto invalid_code; |
57a47f8a KH |
4124 | else if (c >= 0xA1 && c <= 0xDF) |
4125 | { | |
4126 | /* SJIS -> JISX0201-Kana */ | |
4127 | c &= 0x7F; | |
4128 | charset = charset_kana; | |
4129 | } | |
4130 | else if (c <= 0xEF) | |
df7492f9 | 4131 | { |
57a47f8a KH |
4132 | /* SJIS -> JISX0208 */ |
4133 | ONE_MORE_BYTE (c1); | |
4134 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4135 | goto invalid_code; |
57a47f8a KH |
4136 | c = (c << 8) | c1; |
4137 | SJIS_TO_JIS (c); | |
4138 | charset = charset_kanji; | |
4139 | } | |
4140 | else if (c <= 0xFC && charset_kanji2) | |
4141 | { | |
c6876370 | 4142 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4143 | ONE_MORE_BYTE (c1); |
4144 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4145 | goto invalid_code; |
57a47f8a KH |
4146 | c = (c << 8) | c1; |
4147 | SJIS_TO_JIS2 (c); | |
4148 | charset = charset_kanji2; | |
df7492f9 | 4149 | } |
57a47f8a KH |
4150 | else |
4151 | goto invalid_code; | |
24a73b0a KH |
4152 | if (charset->id != charset_ascii |
4153 | && last_id != charset->id) | |
4154 | { | |
4155 | if (last_id != charset_ascii) | |
69a80ea3 | 4156 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4157 | last_id = charset->id; |
4158 | last_offset = char_offset; | |
4159 | } | |
4160 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4161 | *charbuf++ = c; |
ff0dacd7 | 4162 | char_offset++; |
df7492f9 | 4163 | continue; |
b73bfc1c | 4164 | |
df7492f9 KH |
4165 | invalid_code: |
4166 | src = src_base; | |
4167 | consumed_chars = consumed_chars_base; | |
4168 | ONE_MORE_BYTE (c); | |
065e3595 | 4169 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4170 | char_offset++; |
df7492f9 KH |
4171 | coding->errors++; |
4172 | } | |
fa42c37f | 4173 | |
df7492f9 | 4174 | no_more_source: |
ff0dacd7 | 4175 | if (last_id != charset_ascii) |
69a80ea3 | 4176 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4177 | coding->consumed_char += consumed_chars_base; |
4178 | coding->consumed = src_base - coding->source; | |
4179 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4180 | } |
4181 | ||
b73bfc1c | 4182 | static void |
df7492f9 | 4183 | decode_coding_big5 (coding) |
4ed46869 | 4184 | struct coding_system *coding; |
4ed46869 | 4185 | { |
8f924df7 KH |
4186 | const unsigned char *src = coding->source + coding->consumed; |
4187 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4188 | const unsigned char *src_base; | |
69a80ea3 KH |
4189 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4190 | int *charbuf_end | |
4191 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4192 | int consumed_chars = 0, consumed_chars_base; |
4193 | int multibytep = coding->src_multibyte; | |
4194 | struct charset *charset_roman, *charset_big5; | |
24a73b0a | 4195 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4196 | int char_offset = coding->produced_char; |
4197 | int last_offset = char_offset; | |
4198 | int last_id = charset_ascii; | |
df7492f9 | 4199 | |
24a73b0a | 4200 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4201 | val = charset_list; |
4202 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4203 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4204 | |
b73bfc1c | 4205 | while (1) |
4ed46869 | 4206 | { |
df7492f9 | 4207 | int c, c1; |
24a73b0a | 4208 | struct charset *charset; |
b73bfc1c KH |
4209 | |
4210 | src_base = src; | |
df7492f9 KH |
4211 | consumed_chars_base = consumed_chars; |
4212 | ||
4213 | if (charbuf >= charbuf_end) | |
4214 | break; | |
4215 | ||
4216 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4217 | |
065e3595 KH |
4218 | if (c < 0) |
4219 | goto invalid_code; | |
24a73b0a KH |
4220 | if (c < 0x80) |
4221 | charset = charset_roman; | |
4222 | else | |
4ed46869 | 4223 | { |
24a73b0a KH |
4224 | /* BIG5 -> Big5 */ |
4225 | if (c < 0xA1 || c > 0xFE) | |
4226 | goto invalid_code; | |
4227 | ONE_MORE_BYTE (c1); | |
4228 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4229 | goto invalid_code; | |
4230 | c = c << 8 | c1; | |
4231 | charset = charset_big5; | |
4ed46869 | 4232 | } |
24a73b0a KH |
4233 | if (charset->id != charset_ascii |
4234 | && last_id != charset->id) | |
df7492f9 | 4235 | { |
24a73b0a | 4236 | if (last_id != charset_ascii) |
69a80ea3 | 4237 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4238 | last_id = charset->id; |
4239 | last_offset = char_offset; | |
4ed46869 | 4240 | } |
24a73b0a | 4241 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4242 | *charbuf++ = c; |
ff0dacd7 | 4243 | char_offset++; |
fb88bf2d KH |
4244 | continue; |
4245 | ||
df7492f9 | 4246 | invalid_code: |
4ed46869 | 4247 | src = src_base; |
df7492f9 KH |
4248 | consumed_chars = consumed_chars_base; |
4249 | ONE_MORE_BYTE (c); | |
065e3595 | 4250 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4251 | char_offset++; |
df7492f9 | 4252 | coding->errors++; |
fb88bf2d | 4253 | } |
d46c5b12 | 4254 | |
df7492f9 | 4255 | no_more_source: |
ff0dacd7 | 4256 | if (last_id != charset_ascii) |
69a80ea3 | 4257 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4258 | coding->consumed_char += consumed_chars_base; |
4259 | coding->consumed = src_base - coding->source; | |
4260 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4261 | } |
4262 | ||
4263 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4264 | This function can encode charsets `ascii', `katakana-jisx0201', |
4265 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4266 | are sure that all these charsets are registered as official charset | |
4ed46869 KH |
4267 | (i.e. do not have extended leading-codes). Characters of other |
4268 | charsets are produced without any encoding. If SJIS_P is 1, encode | |
4269 | SJIS text, else encode BIG5 text. */ | |
4270 | ||
df7492f9 KH |
4271 | static int |
4272 | encode_coding_sjis (coding) | |
4ed46869 | 4273 | struct coding_system *coding; |
4ed46869 | 4274 | { |
df7492f9 KH |
4275 | int multibytep = coding->dst_multibyte; |
4276 | int *charbuf = coding->charbuf; | |
4277 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4278 | unsigned char *dst = coding->destination + coding->produced; | |
4279 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4280 | int safe_room = 4; | |
4281 | int produced_chars = 0; | |
24a73b0a | 4282 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4283 | int ascii_compatible; |
4284 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4285 | struct charset *charset_kanji2; |
df7492f9 | 4286 | int c; |
a5d301df | 4287 | |
24a73b0a | 4288 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4289 | val = charset_list; |
4290 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4291 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
57a47f8a KH |
4292 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4293 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4294 | |
df7492f9 | 4295 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4296 | |
df7492f9 KH |
4297 | while (charbuf < charbuf_end) |
4298 | { | |
4299 | ASSURE_DESTINATION (safe_room); | |
4300 | c = *charbuf++; | |
b73bfc1c | 4301 | /* Now encode the character C. */ |
df7492f9 KH |
4302 | if (ASCII_CHAR_P (c) && ascii_compatible) |
4303 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4304 | else if (CHAR_BYTE8_P (c)) |
4305 | { | |
4306 | c = CHAR_TO_BYTE8 (c); | |
4307 | EMIT_ONE_BYTE (c); | |
4308 | } | |
df7492f9 | 4309 | else |
b73bfc1c | 4310 | { |
df7492f9 KH |
4311 | unsigned code; |
4312 | struct charset *charset = char_charset (c, charset_list, &code); | |
4313 | ||
4314 | if (!charset) | |
4ed46869 | 4315 | { |
41cbe562 | 4316 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4317 | { |
41cbe562 KH |
4318 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4319 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4320 | } |
41cbe562 | 4321 | else |
b73bfc1c | 4322 | { |
41cbe562 KH |
4323 | c = coding->default_char; |
4324 | charset = char_charset (c, charset_list, &code); | |
b73bfc1c | 4325 | } |
b73bfc1c | 4326 | } |
df7492f9 KH |
4327 | if (code == CHARSET_INVALID_CODE (charset)) |
4328 | abort (); | |
4329 | if (charset == charset_kanji) | |
4330 | { | |
4331 | int c1, c2; | |
4332 | JIS_TO_SJIS (code); | |
4333 | c1 = code >> 8, c2 = code & 0xFF; | |
4334 | EMIT_TWO_BYTES (c1, c2); | |
4335 | } | |
4336 | else if (charset == charset_kana) | |
4337 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
4338 | else if (charset_kanji2 && charset == charset_kanji2) |
4339 | { | |
4340 | int c1, c2; | |
4341 | ||
4342 | c1 = code >> 8; | |
4343 | if (c1 == 0x21 || (c1 >= 0x23 && c1 < 0x25) | |
4344 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) | |
4345 | { | |
4346 | JIS_TO_SJIS2 (code); | |
4347 | c1 = code >> 8, c2 = code & 0xFF; | |
4348 | EMIT_TWO_BYTES (c1, c2); | |
4349 | } | |
4350 | else | |
4351 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4352 | } | |
df7492f9 KH |
4353 | else |
4354 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4355 | } | |
4356 | } | |
065e3595 | 4357 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4358 | coding->produced_char += produced_chars; |
4359 | coding->produced = dst - coding->destination; | |
4360 | return 0; | |
4361 | } | |
4362 | ||
4363 | static int | |
4364 | encode_coding_big5 (coding) | |
4365 | struct coding_system *coding; | |
4366 | { | |
4367 | int multibytep = coding->dst_multibyte; | |
4368 | int *charbuf = coding->charbuf; | |
4369 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4370 | unsigned char *dst = coding->destination + coding->produced; | |
4371 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4372 | int safe_room = 4; | |
4373 | int produced_chars = 0; | |
24a73b0a | 4374 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4375 | int ascii_compatible; |
4376 | struct charset *charset_roman, *charset_big5; | |
4377 | int c; | |
4378 | ||
24a73b0a | 4379 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4380 | val = charset_list; |
4381 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4382 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4383 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
4384 | ||
4385 | while (charbuf < charbuf_end) | |
4386 | { | |
4387 | ASSURE_DESTINATION (safe_room); | |
4388 | c = *charbuf++; | |
4389 | /* Now encode the character C. */ | |
4390 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
4391 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4392 | else if (CHAR_BYTE8_P (c)) |
4393 | { | |
4394 | c = CHAR_TO_BYTE8 (c); | |
4395 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
4396 | } |
4397 | else | |
4398 | { | |
df7492f9 KH |
4399 | unsigned code; |
4400 | struct charset *charset = char_charset (c, charset_list, &code); | |
4401 | ||
4402 | if (! charset) | |
b73bfc1c | 4403 | { |
41cbe562 | 4404 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4405 | { |
41cbe562 KH |
4406 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4407 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4408 | } |
41cbe562 | 4409 | else |
0eecad43 | 4410 | { |
41cbe562 KH |
4411 | c = coding->default_char; |
4412 | charset = char_charset (c, charset_list, &code); | |
0eecad43 | 4413 | } |
4ed46869 | 4414 | } |
df7492f9 KH |
4415 | if (code == CHARSET_INVALID_CODE (charset)) |
4416 | abort (); | |
4417 | if (charset == charset_big5) | |
b73bfc1c | 4418 | { |
df7492f9 KH |
4419 | int c1, c2; |
4420 | ||
4421 | c1 = code >> 8, c2 = code & 0xFF; | |
4422 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 4423 | } |
df7492f9 KH |
4424 | else |
4425 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 4426 | } |
4ed46869 | 4427 | } |
065e3595 | 4428 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4429 | coding->produced_char += produced_chars; |
4430 | coding->produced = dst - coding->destination; | |
4431 | return 0; | |
4ed46869 KH |
4432 | } |
4433 | ||
4434 | \f | |
df7492f9 | 4435 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
4436 | |
4437 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4438 | Check if a text is encoded in a coding system of which | |
4439 | encoder/decoder are written in CCL program. If it is, return | |
df7492f9 | 4440 | CATEGORY_MASK_CCL, else return 0. */ |
1397dc18 | 4441 | |
0a28aafb | 4442 | static int |
ff0dacd7 | 4443 | detect_coding_ccl (coding, detect_info) |
df7492f9 | 4444 | struct coding_system *coding; |
ff0dacd7 | 4445 | struct coding_detection_info *detect_info; |
1397dc18 | 4446 | { |
065e3595 | 4447 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4448 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4449 | int multibytep = coding->src_multibyte; |
4450 | int consumed_chars = 0; | |
4451 | int found = 0; | |
0e219d54 | 4452 | unsigned char *valids; |
df7492f9 KH |
4453 | int head_ascii = coding->head_ascii; |
4454 | Lisp_Object attrs; | |
4455 | ||
ff0dacd7 KH |
4456 | detect_info->checked |= CATEGORY_MASK_CCL; |
4457 | ||
df7492f9 | 4458 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 4459 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
4460 | attrs = CODING_ID_ATTRS (coding->id); |
4461 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
4462 | src += head_ascii; | |
1397dc18 | 4463 | |
b73bfc1c | 4464 | while (1) |
1397dc18 | 4465 | { |
df7492f9 | 4466 | int c; |
065e3595 KH |
4467 | |
4468 | src_base = src; | |
df7492f9 | 4469 | ONE_MORE_BYTE (c); |
065e3595 | 4470 | if (c < 0 || ! valids[c]) |
df7492f9 | 4471 | break; |
ff0dacd7 KH |
4472 | if ((valids[c] > 1)) |
4473 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 4474 | } |
ff0dacd7 | 4475 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
4476 | return 0; |
4477 | ||
4478 | no_more_source: | |
ff0dacd7 KH |
4479 | detect_info->found |= found; |
4480 | return 1; | |
df7492f9 KH |
4481 | } |
4482 | ||
4483 | static void | |
4484 | decode_coding_ccl (coding) | |
4485 | struct coding_system *coding; | |
4486 | { | |
7c78e542 | 4487 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 4488 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
4489 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4490 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
4491 | int consumed_chars = 0; |
4492 | int multibytep = coding->src_multibyte; | |
4493 | struct ccl_program ccl; | |
4494 | int source_charbuf[1024]; | |
4495 | int source_byteidx[1024]; | |
24a73b0a | 4496 | Lisp_Object attrs, charset_list; |
df7492f9 | 4497 | |
24a73b0a | 4498 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4499 | setup_ccl_program (&ccl, CODING_CCL_DECODER (coding)); |
4500 | ||
4501 | while (src < src_end) | |
4502 | { | |
7c78e542 | 4503 | const unsigned char *p = src; |
df7492f9 KH |
4504 | int *source, *source_end; |
4505 | int i = 0; | |
4506 | ||
4507 | if (multibytep) | |
4508 | while (i < 1024 && p < src_end) | |
4509 | { | |
4510 | source_byteidx[i] = p - src; | |
4511 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
4512 | } | |
4513 | else | |
4514 | while (i < 1024 && p < src_end) | |
4515 | source_charbuf[i++] = *p++; | |
8f924df7 | 4516 | |
df7492f9 KH |
4517 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
4518 | ccl.last_block = 1; | |
4519 | ||
4520 | source = source_charbuf; | |
4521 | source_end = source + i; | |
4522 | while (source < source_end) | |
4523 | { | |
4524 | ccl_driver (&ccl, source, charbuf, | |
8dcbea82 KH |
4525 | source_end - source, charbuf_end - charbuf, |
4526 | charset_list); | |
df7492f9 KH |
4527 | source += ccl.consumed; |
4528 | charbuf += ccl.produced; | |
4529 | if (ccl.status != CCL_STAT_SUSPEND_BY_DST) | |
4530 | break; | |
4531 | } | |
4532 | if (source < source_end) | |
4533 | src += source_byteidx[source - source_charbuf]; | |
4534 | else | |
4535 | src = p; | |
4536 | consumed_chars += source - source_charbuf; | |
4537 | ||
4538 | if (ccl.status != CCL_STAT_SUSPEND_BY_SRC | |
4539 | && ccl.status != CODING_RESULT_INSUFFICIENT_SRC) | |
4540 | break; | |
4541 | } | |
4542 | ||
4543 | switch (ccl.status) | |
4544 | { | |
4545 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 4546 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
4547 | break; |
4548 | case CCL_STAT_SUSPEND_BY_DST: | |
4549 | break; | |
4550 | case CCL_STAT_QUIT: | |
4551 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 4552 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
4553 | break; |
4554 | default: | |
065e3595 | 4555 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4556 | break; |
4557 | } | |
4558 | coding->consumed_char += consumed_chars; | |
4559 | coding->consumed = src - coding->source; | |
4560 | coding->charbuf_used = charbuf - coding->charbuf; | |
4561 | } | |
4562 | ||
4563 | static int | |
4564 | encode_coding_ccl (coding) | |
4565 | struct coding_system *coding; | |
4566 | { | |
4567 | struct ccl_program ccl; | |
4568 | int multibytep = coding->dst_multibyte; | |
4569 | int *charbuf = coding->charbuf; | |
4570 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4571 | unsigned char *dst = coding->destination + coding->produced; | |
4572 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
df7492f9 KH |
4573 | int destination_charbuf[1024]; |
4574 | int i, produced_chars = 0; | |
24a73b0a | 4575 | Lisp_Object attrs, charset_list; |
df7492f9 | 4576 | |
24a73b0a | 4577 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4578 | setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding)); |
4579 | ||
4580 | ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK; | |
4581 | ccl.dst_multibyte = coding->dst_multibyte; | |
4582 | ||
8cffd3e7 | 4583 | while (charbuf < charbuf_end) |
df7492f9 | 4584 | { |
df7492f9 | 4585 | ccl_driver (&ccl, charbuf, destination_charbuf, |
8cffd3e7 | 4586 | charbuf_end - charbuf, 1024, charset_list); |
df7492f9 | 4587 | if (multibytep) |
8cffd3e7 KH |
4588 | { |
4589 | ASSURE_DESTINATION (ccl.produced * 2); | |
4590 | for (i = 0; i < ccl.produced; i++) | |
4591 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); | |
4592 | } | |
df7492f9 KH |
4593 | else |
4594 | { | |
8cffd3e7 | 4595 | ASSURE_DESTINATION (ccl.produced); |
df7492f9 KH |
4596 | for (i = 0; i < ccl.produced; i++) |
4597 | *dst++ = destination_charbuf[i] & 0xFF; | |
4598 | produced_chars += ccl.produced; | |
4599 | } | |
8cffd3e7 KH |
4600 | charbuf += ccl.consumed; |
4601 | if (ccl.status == CCL_STAT_QUIT | |
4602 | || ccl.status == CCL_STAT_INVALID_CMD) | |
4603 | break; | |
df7492f9 KH |
4604 | } |
4605 | ||
4606 | switch (ccl.status) | |
4607 | { | |
4608 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 4609 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
4610 | break; |
4611 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 4612 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
4613 | break; |
4614 | case CCL_STAT_QUIT: | |
4615 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 4616 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
4617 | break; |
4618 | default: | |
065e3595 | 4619 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 4620 | break; |
1397dc18 | 4621 | } |
df7492f9 KH |
4622 | |
4623 | coding->produced_char += produced_chars; | |
4624 | coding->produced = dst - coding->destination; | |
4625 | return 0; | |
1397dc18 KH |
4626 | } |
4627 | ||
df7492f9 | 4628 | |
1397dc18 | 4629 | \f |
df7492f9 | 4630 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 4631 | |
b73bfc1c | 4632 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 4633 | |
b73bfc1c | 4634 | static void |
df7492f9 | 4635 | decode_coding_raw_text (coding) |
4ed46869 | 4636 | struct coding_system *coding; |
4ed46869 | 4637 | { |
df7492f9 | 4638 | coding->chars_at_source = 1; |
2c78b7e1 KH |
4639 | coding->consumed_char = 0; |
4640 | coding->consumed = 0; | |
065e3595 | 4641 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 4642 | } |
4ed46869 | 4643 | |
df7492f9 KH |
4644 | static int |
4645 | encode_coding_raw_text (coding) | |
4646 | struct coding_system *coding; | |
4647 | { | |
4648 | int multibytep = coding->dst_multibyte; | |
4649 | int *charbuf = coding->charbuf; | |
4650 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
4651 | unsigned char *dst = coding->destination + coding->produced; | |
4652 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4653 | int produced_chars = 0; | |
b73bfc1c KH |
4654 | int c; |
4655 | ||
df7492f9 | 4656 | if (multibytep) |
b73bfc1c | 4657 | { |
df7492f9 | 4658 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 4659 | |
df7492f9 KH |
4660 | if (coding->src_multibyte) |
4661 | while (charbuf < charbuf_end) | |
4662 | { | |
4663 | ASSURE_DESTINATION (safe_room); | |
4664 | c = *charbuf++; | |
4665 | if (ASCII_CHAR_P (c)) | |
4666 | EMIT_ONE_ASCII_BYTE (c); | |
4667 | else if (CHAR_BYTE8_P (c)) | |
4668 | { | |
4669 | c = CHAR_TO_BYTE8 (c); | |
4670 | EMIT_ONE_BYTE (c); | |
4671 | } | |
4672 | else | |
4673 | { | |
4674 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 4675 | |
df7492f9 KH |
4676 | CHAR_STRING_ADVANCE (c, p1); |
4677 | while (p0 < p1) | |
9d123124 KH |
4678 | { |
4679 | EMIT_ONE_BYTE (*p0); | |
4680 | p0++; | |
4681 | } | |
df7492f9 KH |
4682 | } |
4683 | } | |
b73bfc1c | 4684 | else |
df7492f9 KH |
4685 | while (charbuf < charbuf_end) |
4686 | { | |
4687 | ASSURE_DESTINATION (safe_room); | |
4688 | c = *charbuf++; | |
4689 | EMIT_ONE_BYTE (c); | |
4690 | } | |
4691 | } | |
4692 | else | |
4ed46869 | 4693 | { |
df7492f9 | 4694 | if (coding->src_multibyte) |
d46c5b12 | 4695 | { |
df7492f9 KH |
4696 | int safe_room = MAX_MULTIBYTE_LENGTH; |
4697 | ||
4698 | while (charbuf < charbuf_end) | |
d46c5b12 | 4699 | { |
df7492f9 KH |
4700 | ASSURE_DESTINATION (safe_room); |
4701 | c = *charbuf++; | |
4702 | if (ASCII_CHAR_P (c)) | |
4703 | *dst++ = c; | |
4704 | else if (CHAR_BYTE8_P (c)) | |
4705 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 4706 | else |
df7492f9 KH |
4707 | CHAR_STRING_ADVANCE (c, dst); |
4708 | produced_chars++; | |
d46c5b12 KH |
4709 | } |
4710 | } | |
df7492f9 KH |
4711 | else |
4712 | { | |
4713 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
4714 | while (charbuf < charbuf_end && dst < dst_end) | |
4715 | *dst++ = *charbuf++; | |
4716 | produced_chars = dst - (coding->destination + coding->dst_bytes); | |
8f924df7 | 4717 | } |
4ed46869 | 4718 | } |
065e3595 | 4719 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4720 | coding->produced_char += produced_chars; |
4721 | coding->produced = dst - coding->destination; | |
4722 | return 0; | |
4ed46869 KH |
4723 | } |
4724 | ||
ff0dacd7 KH |
4725 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
4726 | Check if a text is encoded in a charset-based coding system. If it | |
4727 | is, return 1, else return 0. */ | |
4728 | ||
0a28aafb | 4729 | static int |
ff0dacd7 | 4730 | detect_coding_charset (coding, detect_info) |
df7492f9 | 4731 | struct coding_system *coding; |
ff0dacd7 | 4732 | struct coding_detection_info *detect_info; |
1397dc18 | 4733 | { |
065e3595 | 4734 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4735 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4736 | int multibytep = coding->src_multibyte; |
4737 | int consumed_chars = 0; | |
4738 | Lisp_Object attrs, valids; | |
584948ac | 4739 | int found = 0; |
716b3fa0 | 4740 | int head_ascii = coding->head_ascii; |
1397dc18 | 4741 | |
ff0dacd7 KH |
4742 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
4743 | ||
df7492f9 KH |
4744 | coding = &coding_categories[coding_category_charset]; |
4745 | attrs = CODING_ID_ATTRS (coding->id); | |
4746 | valids = AREF (attrs, coding_attr_charset_valids); | |
4747 | ||
4748 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
716b3fa0 | 4749 | src += head_ascii; |
1397dc18 | 4750 | |
b73bfc1c | 4751 | while (1) |
1397dc18 | 4752 | { |
df7492f9 | 4753 | int c; |
716b3fa0 KH |
4754 | Lisp_Object val; |
4755 | struct charset *charset; | |
4756 | int dim, idx; | |
1397dc18 | 4757 | |
065e3595 | 4758 | src_base = src; |
df7492f9 | 4759 | ONE_MORE_BYTE (c); |
065e3595 KH |
4760 | if (c < 0) |
4761 | continue; | |
716b3fa0 KH |
4762 | val = AREF (valids, c); |
4763 | if (NILP (val)) | |
df7492f9 | 4764 | break; |
584948ac | 4765 | if (c >= 0x80) |
ff0dacd7 | 4766 | found = CATEGORY_MASK_CHARSET; |
716b3fa0 KH |
4767 | if (INTEGERP (val)) |
4768 | { | |
4769 | charset = CHARSET_FROM_ID (XFASTINT (val)); | |
4770 | dim = CHARSET_DIMENSION (charset); | |
4771 | for (idx = 1; idx < dim; idx++) | |
4772 | { | |
4773 | if (src == src_end) | |
4774 | goto too_short; | |
4775 | ONE_MORE_BYTE (c); | |
4776 | if (c < charset->code_space[(dim - 1 - idx) * 2] | |
4777 | || c > charset->code_space[(dim - 1 - idx) * 2 + 1]) | |
4778 | break; | |
4779 | } | |
4780 | if (idx < dim) | |
4781 | break; | |
4782 | } | |
4783 | else | |
4784 | { | |
4785 | idx = 1; | |
4786 | for (; CONSP (val); val = XCDR (val)) | |
4787 | { | |
4788 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); | |
4789 | dim = CHARSET_DIMENSION (charset); | |
4790 | while (idx < dim) | |
4791 | { | |
4792 | if (src == src_end) | |
4793 | goto too_short; | |
4794 | ONE_MORE_BYTE (c); | |
4795 | if (c < charset->code_space[(dim - 1 - idx) * 4] | |
4796 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
4797 | break; | |
4798 | idx++; | |
4799 | } | |
4800 | if (idx == dim) | |
4801 | { | |
4802 | val = Qnil; | |
4803 | break; | |
4804 | } | |
4805 | } | |
4806 | if (CONSP (val)) | |
4807 | break; | |
4808 | } | |
df7492f9 | 4809 | } |
716b3fa0 | 4810 | too_short: |
ff0dacd7 | 4811 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 4812 | return 0; |
4ed46869 | 4813 | |
df7492f9 | 4814 | no_more_source: |
ff0dacd7 KH |
4815 | detect_info->found |= found; |
4816 | return 1; | |
df7492f9 | 4817 | } |
b73bfc1c | 4818 | |
b73bfc1c | 4819 | static void |
df7492f9 | 4820 | decode_coding_charset (coding) |
4ed46869 | 4821 | struct coding_system *coding; |
4ed46869 | 4822 | { |
8f924df7 KH |
4823 | const unsigned char *src = coding->source + coding->consumed; |
4824 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4825 | const unsigned char *src_base; | |
69a80ea3 KH |
4826 | int *charbuf = coding->charbuf + coding->charbuf_used; |
4827 | int *charbuf_end | |
4828 | = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH; | |
df7492f9 KH |
4829 | int consumed_chars = 0, consumed_chars_base; |
4830 | int multibytep = coding->src_multibyte; | |
24a73b0a | 4831 | Lisp_Object attrs, charset_list, valids; |
ff0dacd7 KH |
4832 | int char_offset = coding->produced_char; |
4833 | int last_offset = char_offset; | |
4834 | int last_id = charset_ascii; | |
df7492f9 | 4835 | |
24a73b0a | 4836 | CODING_GET_INFO (coding, attrs, charset_list); |
4eb6d3f1 | 4837 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 4838 | |
df7492f9 | 4839 | while (1) |
4ed46869 | 4840 | { |
4eb6d3f1 | 4841 | int c; |
24a73b0a KH |
4842 | Lisp_Object val; |
4843 | struct charset *charset; | |
4844 | int dim; | |
4845 | int len = 1; | |
4846 | unsigned code; | |
df7492f9 KH |
4847 | |
4848 | src_base = src; | |
4849 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 4850 | |
df7492f9 KH |
4851 | if (charbuf >= charbuf_end) |
4852 | break; | |
4853 | ||
4eb6d3f1 | 4854 | ONE_MORE_BYTE (c); |
065e3595 KH |
4855 | if (c < 0) |
4856 | goto invalid_code; | |
24a73b0a KH |
4857 | code = c; |
4858 | ||
4859 | val = AREF (valids, c); | |
4860 | if (NILP (val)) | |
4861 | goto invalid_code; | |
4862 | if (INTEGERP (val)) | |
d46c5b12 | 4863 | { |
24a73b0a KH |
4864 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
4865 | dim = CHARSET_DIMENSION (charset); | |
4866 | while (len < dim) | |
b73bfc1c | 4867 | { |
24a73b0a KH |
4868 | ONE_MORE_BYTE (c); |
4869 | code = (code << 8) | c; | |
4870 | len++; | |
b73bfc1c | 4871 | } |
24a73b0a KH |
4872 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
4873 | charset, code, c); | |
d46c5b12 | 4874 | } |
df7492f9 | 4875 | else |
d46c5b12 | 4876 | { |
24a73b0a KH |
4877 | /* VAL is a list of charset IDs. It is assured that the |
4878 | list is sorted by charset dimensions (smaller one | |
4879 | comes first). */ | |
4880 | while (CONSP (val)) | |
4eb6d3f1 | 4881 | { |
24a73b0a | 4882 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 4883 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 4884 | while (len < dim) |
4eb6d3f1 | 4885 | { |
acb2a965 KH |
4886 | ONE_MORE_BYTE (c); |
4887 | code = (code << 8) | c; | |
f9d71dcd | 4888 | len++; |
4eb6d3f1 | 4889 | } |
24a73b0a KH |
4890 | CODING_DECODE_CHAR (coding, src, src_base, |
4891 | src_end, charset, code, c); | |
4892 | if (c >= 0) | |
4893 | break; | |
4894 | val = XCDR (val); | |
ff0dacd7 | 4895 | } |
d46c5b12 | 4896 | } |
24a73b0a KH |
4897 | if (c < 0) |
4898 | goto invalid_code; | |
4899 | if (charset->id != charset_ascii | |
4900 | && last_id != charset->id) | |
4901 | { | |
4902 | if (last_id != charset_ascii) | |
69a80ea3 | 4903 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4904 | last_id = charset->id; |
4905 | last_offset = char_offset; | |
4906 | } | |
4907 | ||
df7492f9 | 4908 | *charbuf++ = c; |
ff0dacd7 | 4909 | char_offset++; |
df7492f9 KH |
4910 | continue; |
4911 | ||
4912 | invalid_code: | |
4913 | src = src_base; | |
4914 | consumed_chars = consumed_chars_base; | |
4915 | ONE_MORE_BYTE (c); | |
065e3595 | 4916 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4917 | char_offset++; |
df7492f9 | 4918 | coding->errors++; |
4ed46869 KH |
4919 | } |
4920 | ||
df7492f9 | 4921 | no_more_source: |
ff0dacd7 | 4922 | if (last_id != charset_ascii) |
69a80ea3 | 4923 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4924 | coding->consumed_char += consumed_chars_base; |
4925 | coding->consumed = src_base - coding->source; | |
4926 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4927 | } |
4928 | ||
df7492f9 KH |
4929 | static int |
4930 | encode_coding_charset (coding) | |
4ed46869 | 4931 | struct coding_system *coding; |
4ed46869 | 4932 | { |
df7492f9 KH |
4933 | int multibytep = coding->dst_multibyte; |
4934 | int *charbuf = coding->charbuf; | |
4935 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4936 | unsigned char *dst = coding->destination + coding->produced; | |
4937 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4938 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
4939 | int produced_chars = 0; | |
24a73b0a | 4940 | Lisp_Object attrs, charset_list; |
df7492f9 | 4941 | int ascii_compatible; |
b73bfc1c | 4942 | int c; |
b73bfc1c | 4943 | |
24a73b0a | 4944 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 4945 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 4946 | |
df7492f9 | 4947 | while (charbuf < charbuf_end) |
4ed46869 | 4948 | { |
4eb6d3f1 | 4949 | struct charset *charset; |
df7492f9 | 4950 | unsigned code; |
8f924df7 | 4951 | |
df7492f9 KH |
4952 | ASSURE_DESTINATION (safe_room); |
4953 | c = *charbuf++; | |
4954 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
4955 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 4956 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 4957 | { |
16eafb5d KH |
4958 | c = CHAR_TO_BYTE8 (c); |
4959 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 4960 | } |
d46c5b12 | 4961 | else |
b73bfc1c | 4962 | { |
4eb6d3f1 KH |
4963 | charset = char_charset (c, charset_list, &code); |
4964 | if (charset) | |
4965 | { | |
4966 | if (CHARSET_DIMENSION (charset) == 1) | |
4967 | EMIT_ONE_BYTE (code); | |
4968 | else if (CHARSET_DIMENSION (charset) == 2) | |
4969 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
4970 | else if (CHARSET_DIMENSION (charset) == 3) | |
4971 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
4972 | else | |
4973 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
4974 | (code >> 8) & 0xFF, code & 0xFF); | |
4975 | } | |
4976 | else | |
41cbe562 KH |
4977 | { |
4978 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
4979 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4980 | else | |
4981 | c = coding->default_char; | |
4982 | EMIT_ONE_BYTE (c); | |
4983 | } | |
4ed46869 | 4984 | } |
4ed46869 KH |
4985 | } |
4986 | ||
065e3595 | 4987 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4988 | coding->produced_char += produced_chars; |
4989 | coding->produced = dst - coding->destination; | |
4990 | return 0; | |
4ed46869 KH |
4991 | } |
4992 | ||
4993 | \f | |
1397dc18 | 4994 | /*** 7. C library functions ***/ |
4ed46869 | 4995 | |
df7492f9 KH |
4996 | /* Setup coding context CODING from information about CODING_SYSTEM. |
4997 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
4998 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 4999 | |
ec6d2bb8 | 5000 | void |
e0e989f6 KH |
5001 | setup_coding_system (coding_system, coding) |
5002 | Lisp_Object coding_system; | |
4ed46869 KH |
5003 | struct coding_system *coding; |
5004 | { | |
df7492f9 KH |
5005 | Lisp_Object attrs; |
5006 | Lisp_Object eol_type; | |
5007 | Lisp_Object coding_type; | |
4608c386 | 5008 | Lisp_Object val; |
4ed46869 | 5009 | |
df7492f9 | 5010 | if (NILP (coding_system)) |
ae6f73fa | 5011 | coding_system = Qundecided; |
c07c8e12 | 5012 | |
df7492f9 | 5013 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 5014 | |
df7492f9 KH |
5015 | attrs = CODING_ID_ATTRS (coding->id); |
5016 | eol_type = CODING_ID_EOL_TYPE (coding->id); | |
1f5dbf34 | 5017 | |
df7492f9 KH |
5018 | coding->mode = 0; |
5019 | coding->head_ascii = -1; | |
4a015c45 KH |
5020 | if (VECTORP (eol_type)) |
5021 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5022 | | CODING_REQUIRE_DETECTION_MASK); | |
5023 | else if (! EQ (eol_type, Qunix)) | |
5024 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5025 | | CODING_REQUIRE_ENCODING_MASK); | |
5026 | else | |
5027 | coding->common_flags = 0; | |
5e5c78be KH |
5028 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
5029 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5030 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
5031 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
5032 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
5033 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 5034 | |
df7492f9 | 5035 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 KH |
5036 | coding->max_charset_id = SCHARS (val) - 1; |
5037 | coding->safe_charsets = (char *) SDATA (val); | |
df7492f9 | 5038 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
4608c386 | 5039 | |
df7492f9 KH |
5040 | coding_type = CODING_ATTR_TYPE (attrs); |
5041 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 5042 | { |
df7492f9 KH |
5043 | coding->detector = NULL; |
5044 | coding->decoder = decode_coding_raw_text; | |
5045 | coding->encoder = encode_coding_raw_text; | |
5046 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5047 | } |
df7492f9 | 5048 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 5049 | { |
df7492f9 KH |
5050 | int i; |
5051 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5052 | ||
5053 | /* Invoke graphic register 0 to plane 0. */ | |
5054 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
5055 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
5056 | CODING_ISO_INVOCATION (coding, 1) | |
5057 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
5058 | /* Setup the initial status of designation. */ | |
5059 | for (i = 0; i < 4; i++) | |
5060 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
5061 | /* Not single shifting initially. */ | |
5062 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
5063 | /* Beginning of buffer should also be regarded as bol. */ | |
5064 | CODING_ISO_BOL (coding) = 1; | |
5065 | coding->detector = detect_coding_iso_2022; | |
5066 | coding->decoder = decode_coding_iso_2022; | |
5067 | coding->encoder = encode_coding_iso_2022; | |
5068 | if (flags & CODING_ISO_FLAG_SAFE) | |
5069 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 5070 | coding->common_flags |
df7492f9 KH |
5071 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5072 | | CODING_REQUIRE_FLUSHING_MASK); | |
5073 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
5074 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
5075 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
5076 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
5077 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
5078 | { | |
5079 | setup_iso_safe_charsets (attrs); | |
5080 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 KH |
5081 | coding->max_charset_id = SCHARS (val) - 1; |
5082 | coding->safe_charsets = (char *) SDATA (val); | |
df7492f9 KH |
5083 | } |
5084 | CODING_ISO_FLAGS (coding) = flags; | |
d46c5b12 | 5085 | } |
df7492f9 | 5086 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 5087 | { |
df7492f9 KH |
5088 | coding->detector = detect_coding_charset; |
5089 | coding->decoder = decode_coding_charset; | |
5090 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 5091 | coding->common_flags |
df7492f9 | 5092 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 5093 | } |
df7492f9 | 5094 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5095 | { |
df7492f9 KH |
5096 | coding->detector = detect_coding_utf_8; |
5097 | coding->decoder = decode_coding_utf_8; | |
5098 | coding->encoder = encode_coding_utf_8; | |
5099 | coding->common_flags | |
5100 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
5101 | } | |
5102 | else if (EQ (coding_type, Qutf_16)) | |
5103 | { | |
5104 | val = AREF (attrs, coding_attr_utf_16_bom); | |
5105 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom | |
5106 | : EQ (val, Qt) ? utf_16_with_bom | |
5107 | : utf_16_without_bom); | |
5108 | val = AREF (attrs, coding_attr_utf_16_endian); | |
b49a1807 | 5109 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5110 | : utf_16_little_endian); |
e19c3639 | 5111 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5112 | coding->detector = detect_coding_utf_16; |
5113 | coding->decoder = decode_coding_utf_16; | |
5114 | coding->encoder = encode_coding_utf_16; | |
5115 | coding->common_flags | |
5116 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
b49a1807 KH |
5117 | if (CODING_UTF_16_BOM (coding) == utf_16_detect_bom) |
5118 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5119 | } |
df7492f9 | 5120 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5121 | { |
df7492f9 KH |
5122 | coding->detector = detect_coding_ccl; |
5123 | coding->decoder = decode_coding_ccl; | |
5124 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5125 | coding->common_flags |
df7492f9 KH |
5126 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5127 | | CODING_REQUIRE_FLUSHING_MASK); | |
5128 | } | |
5129 | else if (EQ (coding_type, Qemacs_mule)) | |
5130 | { | |
5131 | coding->detector = detect_coding_emacs_mule; | |
5132 | coding->decoder = decode_coding_emacs_mule; | |
5133 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5134 | coding->common_flags |
df7492f9 KH |
5135 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5136 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) | |
5137 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5138 | { | |
5139 | Lisp_Object tail, safe_charsets; | |
5140 | int max_charset_id = 0; | |
5141 | ||
5142 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5143 | tail = XCDR (tail)) | |
5144 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5145 | max_charset_id = XFASTINT (XCAR (tail)); | |
5146 | safe_charsets = Fmake_string (make_number (max_charset_id + 1), | |
5147 | make_number (255)); | |
5148 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5149 | tail = XCDR (tail)) | |
8f924df7 | 5150 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5151 | coding->max_charset_id = max_charset_id; |
8f924df7 | 5152 | coding->safe_charsets = (char *) SDATA (safe_charsets); |
df7492f9 KH |
5153 | } |
5154 | } | |
5155 | else if (EQ (coding_type, Qshift_jis)) | |
5156 | { | |
5157 | coding->detector = detect_coding_sjis; | |
5158 | coding->decoder = decode_coding_sjis; | |
5159 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5160 | coding->common_flags |
df7492f9 KH |
5161 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5162 | } | |
5163 | else if (EQ (coding_type, Qbig5)) | |
5164 | { | |
5165 | coding->detector = detect_coding_big5; | |
5166 | coding->decoder = decode_coding_big5; | |
5167 | coding->encoder = encode_coding_big5; | |
c952af22 | 5168 | coding->common_flags |
df7492f9 KH |
5169 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5170 | } | |
5171 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5172 | { |
df7492f9 KH |
5173 | coding->detector = NULL; |
5174 | coding->decoder = decode_coding_raw_text; | |
5175 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5176 | if (! EQ (eol_type, Qunix)) |
5177 | { | |
5178 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5179 | if (! VECTORP (eol_type)) | |
5180 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5181 | } | |
5182 | ||
4ed46869 | 5183 | } |
4ed46869 | 5184 | |
df7492f9 | 5185 | return; |
4ed46869 KH |
5186 | } |
5187 | ||
0ff61e78 KH |
5188 | /* Return a list of charsets supported by CODING. */ |
5189 | ||
5190 | Lisp_Object | |
5191 | coding_charset_list (coding) | |
5192 | struct coding_system *coding; | |
5193 | { | |
35befdaa | 5194 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5195 | |
5196 | CODING_GET_INFO (coding, attrs, charset_list); | |
5197 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5198 | { | |
5199 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5200 | ||
5201 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5202 | charset_list = Viso_2022_charset_list; | |
5203 | } | |
5204 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5205 | { | |
5206 | charset_list = Vemacs_mule_charset_list; | |
5207 | } | |
5208 | return charset_list; | |
5209 | } | |
5210 | ||
5211 | ||
df7492f9 KH |
5212 | /* Return raw-text or one of its subsidiaries that has the same |
5213 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 5214 | |
df7492f9 KH |
5215 | Lisp_Object |
5216 | raw_text_coding_system (coding_system) | |
5217 | Lisp_Object coding_system; | |
ec6d2bb8 | 5218 | { |
0be8721c | 5219 | Lisp_Object spec, attrs; |
df7492f9 | 5220 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 5221 | |
d3e4cb56 KH |
5222 | if (NILP (coding_system)) |
5223 | return Qraw_text; | |
df7492f9 KH |
5224 | spec = CODING_SYSTEM_SPEC (coding_system); |
5225 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 5226 | |
df7492f9 KH |
5227 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
5228 | return coding_system; | |
ec6d2bb8 | 5229 | |
df7492f9 KH |
5230 | eol_type = AREF (spec, 2); |
5231 | if (VECTORP (eol_type)) | |
5232 | return Qraw_text; | |
5233 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
5234 | raw_text_eol_type = AREF (spec, 2); | |
5235 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
5236 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
5237 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
5238 | } |
5239 | ||
54f78171 | 5240 | |
df7492f9 KH |
5241 | /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT |
5242 | does, return one of the subsidiary that has the same eol-spec as | |
fcbcfb64 KH |
5243 | PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil, |
5244 | inherit end-of-line format from the system's setting | |
5245 | (system_eol_type). */ | |
df7492f9 KH |
5246 | |
5247 | Lisp_Object | |
5248 | coding_inherit_eol_type (coding_system, parent) | |
b74e4686 | 5249 | Lisp_Object coding_system, parent; |
54f78171 | 5250 | { |
3e139625 | 5251 | Lisp_Object spec, eol_type; |
54f78171 | 5252 | |
d3e4cb56 KH |
5253 | if (NILP (coding_system)) |
5254 | coding_system = Qraw_text; | |
df7492f9 | 5255 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 5256 | eol_type = AREF (spec, 2); |
fcbcfb64 | 5257 | if (VECTORP (eol_type)) |
df7492f9 | 5258 | { |
df7492f9 KH |
5259 | Lisp_Object parent_eol_type; |
5260 | ||
fcbcfb64 KH |
5261 | if (! NILP (parent)) |
5262 | { | |
5263 | Lisp_Object parent_spec; | |
5264 | ||
4a015c45 | 5265 | parent_spec = CODING_SYSTEM_SPEC (parent); |
fcbcfb64 KH |
5266 | parent_eol_type = AREF (parent_spec, 2); |
5267 | } | |
5268 | else | |
5269 | parent_eol_type = system_eol_type; | |
df7492f9 KH |
5270 | if (EQ (parent_eol_type, Qunix)) |
5271 | coding_system = AREF (eol_type, 0); | |
5272 | else if (EQ (parent_eol_type, Qdos)) | |
5273 | coding_system = AREF (eol_type, 1); | |
5274 | else if (EQ (parent_eol_type, Qmac)) | |
5275 | coding_system = AREF (eol_type, 2); | |
54f78171 | 5276 | } |
df7492f9 | 5277 | return coding_system; |
54f78171 KH |
5278 | } |
5279 | ||
4ed46869 KH |
5280 | /* Emacs has a mechanism to automatically detect a coding system if it |
5281 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
5282 | it's impossible to distinguish some coding systems accurately | |
5283 | because they use the same range of codes. So, at first, coding | |
5284 | systems are categorized into 7, those are: | |
5285 | ||
0ef69138 | 5286 | o coding-category-emacs-mule |
4ed46869 KH |
5287 | |
5288 | The category for a coding system which has the same code range | |
5289 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 5290 | symbol) `emacs-mule' by default. |
4ed46869 KH |
5291 | |
5292 | o coding-category-sjis | |
5293 | ||
5294 | The category for a coding system which has the same code range | |
5295 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 5296 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
5297 | |
5298 | o coding-category-iso-7 | |
5299 | ||
5300 | The category for a coding system which has the same code range | |
7717c392 | 5301 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
5302 | shift and single shift functions. This can encode/decode all |
5303 | charsets. Assigned the coding-system (Lisp symbol) | |
5304 | `iso-2022-7bit' by default. | |
5305 | ||
5306 | o coding-category-iso-7-tight | |
5307 | ||
5308 | Same as coding-category-iso-7 except that this can | |
5309 | encode/decode only the specified charsets. | |
4ed46869 KH |
5310 | |
5311 | o coding-category-iso-8-1 | |
5312 | ||
5313 | The category for a coding system which has the same code range | |
5314 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
5315 | for DIMENSION1 charset. This doesn't use any locking shift |
5316 | and single shift functions. Assigned the coding-system (Lisp | |
5317 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
5318 | |
5319 | o coding-category-iso-8-2 | |
5320 | ||
5321 | The category for a coding system which has the same code range | |
5322 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
5323 | for DIMENSION2 charset. This doesn't use any locking shift |
5324 | and single shift functions. Assigned the coding-system (Lisp | |
5325 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 5326 | |
7717c392 | 5327 | o coding-category-iso-7-else |
4ed46869 KH |
5328 | |
5329 | The category for a coding system which has the same code range | |
df7492f9 | 5330 | as ISO2022 of 7-bit environemnt but uses locking shift or |
7717c392 KH |
5331 | single shift functions. Assigned the coding-system (Lisp |
5332 | symbol) `iso-2022-7bit-lock' by default. | |
5333 | ||
5334 | o coding-category-iso-8-else | |
5335 | ||
5336 | The category for a coding system which has the same code range | |
df7492f9 | 5337 | as ISO2022 of 8-bit environemnt but uses locking shift or |
7717c392 KH |
5338 | single shift functions. Assigned the coding-system (Lisp |
5339 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
5340 | |
5341 | o coding-category-big5 | |
5342 | ||
5343 | The category for a coding system which has the same code range | |
5344 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 5345 | `cn-big5' by default. |
4ed46869 | 5346 | |
fa42c37f KH |
5347 | o coding-category-utf-8 |
5348 | ||
5349 | The category for a coding system which has the same code range | |
6e76ae91 | 5350 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
5351 | symbol) `utf-8' by default. |
5352 | ||
5353 | o coding-category-utf-16-be | |
5354 | ||
5355 | The category for a coding system in which a text has an | |
5356 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
5357 | endian at the head. Assigned the coding-system (Lisp symbol) | |
5358 | `utf-16-be' by default. | |
5359 | ||
5360 | o coding-category-utf-16-le | |
5361 | ||
5362 | The category for a coding system in which a text has an | |
5363 | Unicode signature (cf. Unicode Standard) in the order of | |
5364 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
5365 | symbol) `utf-16-le' by default. | |
5366 | ||
1397dc18 KH |
5367 | o coding-category-ccl |
5368 | ||
5369 | The category for a coding system of which encoder/decoder is | |
5370 | written in CCL programs. The default value is nil, i.e., no | |
5371 | coding system is assigned. | |
5372 | ||
4ed46869 KH |
5373 | o coding-category-binary |
5374 | ||
5375 | The category for a coding system not categorized in any of the | |
5376 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 5377 | `no-conversion' by default. |
4ed46869 KH |
5378 | |
5379 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 5380 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
5381 | What Emacs does actually is to detect a category of coding system. |
5382 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 5383 | decide only one possible category, it selects a category of the |
4ed46869 KH |
5384 | highest priority. Priorities of categories are also specified by a |
5385 | user in a Lisp variable `coding-category-list'. | |
5386 | ||
5387 | */ | |
5388 | ||
df7492f9 KH |
5389 | #define EOL_SEEN_NONE 0 |
5390 | #define EOL_SEEN_LF 1 | |
5391 | #define EOL_SEEN_CR 2 | |
5392 | #define EOL_SEEN_CRLF 4 | |
66cfb530 | 5393 | |
ff0dacd7 KH |
5394 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
5395 | SOURCE is encoded. If CATEGORY is one of | |
5396 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
5397 | two-byte, else they are encoded by one-byte. | |
5398 | ||
5399 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 5400 | |
bc4bc72a | 5401 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
5402 | |
5403 | static int | |
89528eb3 | 5404 | detect_eol (source, src_bytes, category) |
f6cbaf43 | 5405 | const unsigned char *source; |
df7492f9 | 5406 | EMACS_INT src_bytes; |
89528eb3 | 5407 | enum coding_category category; |
4ed46869 | 5408 | { |
f6cbaf43 | 5409 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 5410 | unsigned char c; |
df7492f9 KH |
5411 | int total = 0; |
5412 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 5413 | |
89528eb3 | 5414 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 5415 | { |
df7492f9 | 5416 | int msb, lsb; |
fa42c37f | 5417 | |
89528eb3 KH |
5418 | msb = category == (coding_category_utf_16_le |
5419 | | coding_category_utf_16_le_nosig); | |
df7492f9 | 5420 | lsb = 1 - msb; |
fa42c37f | 5421 | |
df7492f9 | 5422 | while (src + 1 < src_end) |
fa42c37f | 5423 | { |
df7492f9 KH |
5424 | c = src[lsb]; |
5425 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 5426 | { |
df7492f9 KH |
5427 | int this_eol; |
5428 | ||
5429 | if (c == '\n') | |
5430 | this_eol = EOL_SEEN_LF; | |
5431 | else if (src + 3 >= src_end | |
5432 | || src[msb + 2] != 0 | |
5433 | || src[lsb + 2] != '\n') | |
5434 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 5435 | else |
8f924df7 | 5436 | this_eol = EOL_SEEN_CRLF; |
df7492f9 KH |
5437 | |
5438 | if (eol_seen == EOL_SEEN_NONE) | |
5439 | /* This is the first end-of-line. */ | |
5440 | eol_seen = this_eol; | |
5441 | else if (eol_seen != this_eol) | |
fa42c37f | 5442 | { |
df7492f9 KH |
5443 | /* The found type is different from what found before. */ |
5444 | eol_seen = EOL_SEEN_LF; | |
5445 | break; | |
fa42c37f | 5446 | } |
df7492f9 KH |
5447 | if (++total == MAX_EOL_CHECK_COUNT) |
5448 | break; | |
fa42c37f | 5449 | } |
df7492f9 | 5450 | src += 2; |
fa42c37f | 5451 | } |
bcf26d6a | 5452 | } |
d46c5b12 | 5453 | else |
c4825358 | 5454 | { |
df7492f9 | 5455 | while (src < src_end) |
27901516 | 5456 | { |
df7492f9 KH |
5457 | c = *src++; |
5458 | if (c == '\n' || c == '\r') | |
5459 | { | |
5460 | int this_eol; | |
d46c5b12 | 5461 | |
df7492f9 KH |
5462 | if (c == '\n') |
5463 | this_eol = EOL_SEEN_LF; | |
5464 | else if (src >= src_end || *src != '\n') | |
5465 | this_eol = EOL_SEEN_CR; | |
5466 | else | |
5467 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 5468 | |
df7492f9 KH |
5469 | if (eol_seen == EOL_SEEN_NONE) |
5470 | /* This is the first end-of-line. */ | |
5471 | eol_seen = this_eol; | |
5472 | else if (eol_seen != this_eol) | |
5473 | { | |
5474 | /* The found type is different from what found before. */ | |
5475 | eol_seen = EOL_SEEN_LF; | |
5476 | break; | |
5477 | } | |
5478 | if (++total == MAX_EOL_CHECK_COUNT) | |
5479 | break; | |
5480 | } | |
5481 | } | |
73be902c | 5482 | } |
df7492f9 | 5483 | return eol_seen; |
73be902c KH |
5484 | } |
5485 | ||
df7492f9 | 5486 | |
24a73b0a | 5487 | static Lisp_Object |
df7492f9 KH |
5488 | adjust_coding_eol_type (coding, eol_seen) |
5489 | struct coding_system *coding; | |
5490 | int eol_seen; | |
73be902c | 5491 | { |
0be8721c | 5492 | Lisp_Object eol_type; |
8f924df7 | 5493 | |
df7492f9 KH |
5494 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
5495 | if (eol_seen & EOL_SEEN_LF) | |
24a73b0a KH |
5496 | { |
5497 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
5498 | eol_type = Qunix; | |
5499 | } | |
6f197c07 | 5500 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
5501 | { |
5502 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
5503 | eol_type = Qdos; | |
5504 | } | |
6f197c07 | 5505 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
5506 | { |
5507 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
5508 | eol_type = Qmac; | |
5509 | } | |
5510 | return eol_type; | |
d46c5b12 | 5511 | } |
4ed46869 | 5512 | |
df7492f9 KH |
5513 | /* Detect how a text specified in CODING is encoded. If a coding |
5514 | system is detected, update fields of CODING by the detected coding | |
5515 | system. */ | |
0a28aafb | 5516 | |
df7492f9 KH |
5517 | void |
5518 | detect_coding (coding) | |
d46c5b12 | 5519 | struct coding_system *coding; |
d46c5b12 | 5520 | { |
8f924df7 | 5521 | const unsigned char *src, *src_end; |
d46c5b12 | 5522 | |
df7492f9 KH |
5523 | coding->consumed = coding->consumed_char = 0; |
5524 | coding->produced = coding->produced_char = 0; | |
5525 | coding_set_source (coding); | |
1c3478b0 | 5526 | |
df7492f9 | 5527 | src_end = coding->source + coding->src_bytes; |
1c3478b0 | 5528 | |
df7492f9 KH |
5529 | /* If we have not yet decided the text encoding type, detect it |
5530 | now. */ | |
5531 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 5532 | { |
df7492f9 | 5533 | int c, i; |
6cb21a4f | 5534 | struct coding_detection_info detect_info; |
df7492f9 | 5535 | |
6cb21a4f | 5536 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
24a73b0a | 5537 | for (i = 0, src = coding->source; src < src_end; i++, src++) |
d46c5b12 | 5538 | { |
df7492f9 | 5539 | c = *src; |
6cb21a4f | 5540 | if (c & 0x80) |
df7492f9 | 5541 | break; |
6cb21a4f KH |
5542 | if (c < 0x20 |
5543 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
5544 | && ! inhibit_iso_escape_detection | |
5545 | && ! detect_info.checked) | |
5546 | { | |
5547 | coding->head_ascii = src - (coding->source + coding->consumed); | |
5548 | if (detect_coding_iso_2022 (coding, &detect_info)) | |
5549 | { | |
5550 | /* We have scanned the whole data. */ | |
5551 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
5552 | /* We didn't find an 8-bit code. */ | |
5553 | src = src_end; | |
5554 | break; | |
5555 | } | |
5556 | } | |
d46c5b12 | 5557 | } |
df7492f9 KH |
5558 | coding->head_ascii = src - (coding->source + coding->consumed); |
5559 | ||
3aef54f3 | 5560 | if (coding->head_ascii < coding->src_bytes |
6cb21a4f | 5561 | || detect_info.found) |
d46c5b12 | 5562 | { |
ff0dacd7 KH |
5563 | enum coding_category category; |
5564 | struct coding_system *this; | |
df7492f9 | 5565 | |
6cb21a4f KH |
5566 | if (coding->head_ascii == coding->src_bytes) |
5567 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
5568 | for (i = 0; i < coding_category_raw_text; i++) | |
5569 | { | |
5570 | category = coding_priorities[i]; | |
5571 | this = coding_categories + category; | |
5572 | if (detect_info.found & (1 << category)) | |
24a73b0a | 5573 | break; |
6cb21a4f KH |
5574 | } |
5575 | else | |
5576 | for (i = 0; i < coding_category_raw_text; i++) | |
5577 | { | |
5578 | category = coding_priorities[i]; | |
5579 | this = coding_categories + category; | |
5580 | if (this->id < 0) | |
5581 | { | |
5582 | /* No coding system of this category is defined. */ | |
5583 | detect_info.rejected |= (1 << category); | |
5584 | } | |
5585 | else if (category >= coding_category_raw_text) | |
5586 | continue; | |
5587 | else if (detect_info.checked & (1 << category)) | |
5588 | { | |
5589 | if (detect_info.found & (1 << category)) | |
5590 | break; | |
5591 | } | |
5592 | else if ((*(this->detector)) (coding, &detect_info) | |
5593 | && detect_info.found & (1 << category)) | |
5594 | { | |
5595 | if (category == coding_category_utf_16_auto) | |
5596 | { | |
5597 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
5598 | category = coding_category_utf_16_le; | |
5599 | else | |
5600 | category = coding_category_utf_16_be; | |
5601 | } | |
5602 | break; | |
5603 | } | |
5604 | } | |
5605 | ||
ff0dacd7 KH |
5606 | if (i < coding_category_raw_text) |
5607 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
5608 | else if (detect_info.rejected == CATEGORY_MASK_ANY) | |
df7492f9 | 5609 | setup_coding_system (Qraw_text, coding); |
ff0dacd7 | 5610 | else if (detect_info.rejected) |
df7492f9 | 5611 | for (i = 0; i < coding_category_raw_text; i++) |
ff0dacd7 KH |
5612 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) |
5613 | { | |
5614 | this = coding_categories + coding_priorities[i]; | |
5615 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
5616 | break; | |
5617 | } | |
d46c5b12 | 5618 | } |
b73bfc1c | 5619 | } |
24a73b0a KH |
5620 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
5621 | == coding_category_utf_16_auto) | |
b49a1807 KH |
5622 | { |
5623 | Lisp_Object coding_systems; | |
5624 | struct coding_detection_info detect_info; | |
5625 | ||
5626 | coding_systems | |
5627 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_16_bom); | |
5628 | detect_info.found = detect_info.rejected = 0; | |
5629 | if (CONSP (coding_systems) | |
24a73b0a | 5630 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
5631 | { |
5632 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
5633 | setup_coding_system (XCAR (coding_systems), coding); | |
24a73b0a | 5634 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
b49a1807 KH |
5635 | setup_coding_system (XCDR (coding_systems), coding); |
5636 | } | |
5637 | } | |
4ed46869 | 5638 | } |
4ed46869 | 5639 | |
d46c5b12 | 5640 | |
aaaf0b1e | 5641 | static void |
df7492f9 | 5642 | decode_eol (coding) |
aaaf0b1e | 5643 | struct coding_system *coding; |
aaaf0b1e | 5644 | { |
24a73b0a KH |
5645 | Lisp_Object eol_type; |
5646 | unsigned char *p, *pbeg, *pend; | |
5647 | ||
5648 | eol_type = CODING_ID_EOL_TYPE (coding->id); | |
5649 | if (EQ (eol_type, Qunix)) | |
5650 | return; | |
5651 | ||
5652 | if (NILP (coding->dst_object)) | |
5653 | pbeg = coding->destination; | |
5654 | else | |
5655 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
5656 | pend = pbeg + coding->produced; | |
5657 | ||
5658 | if (VECTORP (eol_type)) | |
aaaf0b1e | 5659 | { |
df7492f9 | 5660 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 5661 | |
24a73b0a | 5662 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 5663 | { |
df7492f9 KH |
5664 | if (*p == '\n') |
5665 | eol_seen |= EOL_SEEN_LF; | |
5666 | else if (*p == '\r') | |
aaaf0b1e | 5667 | { |
df7492f9 | 5668 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 5669 | { |
df7492f9 KH |
5670 | eol_seen |= EOL_SEEN_CRLF; |
5671 | p++; | |
aaaf0b1e | 5672 | } |
aaaf0b1e | 5673 | else |
df7492f9 | 5674 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 5675 | } |
aaaf0b1e | 5676 | } |
24a73b0a KH |
5677 | if (eol_seen != EOL_SEEN_NONE |
5678 | && eol_seen != EOL_SEEN_LF | |
5679 | && eol_seen != EOL_SEEN_CRLF | |
5680 | && eol_seen != EOL_SEEN_CR) | |
5681 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 5682 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 5683 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 5684 | } |
d46c5b12 | 5685 | |
24a73b0a | 5686 | if (EQ (eol_type, Qmac)) |
27901516 | 5687 | { |
24a73b0a | 5688 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
5689 | if (*p == '\r') |
5690 | *p = '\n'; | |
4ed46869 | 5691 | } |
24a73b0a | 5692 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 5693 | { |
24a73b0a | 5694 | int n = 0; |
b73bfc1c | 5695 | |
24a73b0a KH |
5696 | if (NILP (coding->dst_object)) |
5697 | { | |
4347441b KH |
5698 | /* Start deleting '\r' from the tail to minimize the memory |
5699 | movement. */ | |
24a73b0a KH |
5700 | for (p = pend - 2; p >= pbeg; p--) |
5701 | if (*p == '\r') | |
5702 | { | |
5703 | safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1); | |
5704 | n++; | |
5705 | } | |
5706 | } | |
5707 | else | |
5708 | { | |
4347441b KH |
5709 | int pos_byte = coding->dst_pos_byte; |
5710 | int pos = coding->dst_pos; | |
5711 | int pos_end = pos + coding->produced_char - 1; | |
5712 | ||
5713 | while (pos < pos_end) | |
5714 | { | |
5715 | p = BYTE_POS_ADDR (pos_byte); | |
5716 | if (*p == '\r' && p[1] == '\n') | |
5717 | { | |
5718 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
5719 | n++; | |
5720 | pos_end--; | |
5721 | } | |
5722 | pos++; | |
5723 | pos_byte += BYTES_BY_CHAR_HEAD (*p); | |
5724 | } | |
24a73b0a KH |
5725 | } |
5726 | coding->produced -= n; | |
5727 | coding->produced_char -= n; | |
aaaf0b1e | 5728 | } |
4ed46869 KH |
5729 | } |
5730 | ||
7d64c6ad | 5731 | |
a6f87d34 KH |
5732 | /* Return a translation table (or list of them) from coding system |
5733 | attribute vector ATTRS for encoding (ENCODEP is nonzero) or | |
5734 | decoding (ENCODEP is zero). */ | |
7d64c6ad | 5735 | |
e6a54062 | 5736 | static Lisp_Object |
09ee6fdd KH |
5737 | get_translation_table (attrs, encodep, max_lookup) |
5738 | Lisp_Object attrs; | |
5739 | int encodep, *max_lookup; | |
7d64c6ad KH |
5740 | { |
5741 | Lisp_Object standard, translation_table; | |
09ee6fdd | 5742 | Lisp_Object val; |
7d64c6ad KH |
5743 | |
5744 | if (encodep) | |
5745 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
5746 | standard = Vstandard_translation_table_for_encode; | |
5747 | else | |
5748 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
5749 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 5750 | if (NILP (translation_table)) |
09ee6fdd KH |
5751 | translation_table = standard; |
5752 | else | |
a6f87d34 | 5753 | { |
09ee6fdd KH |
5754 | if (SYMBOLP (translation_table)) |
5755 | translation_table = Fget (translation_table, Qtranslation_table); | |
5756 | else if (CONSP (translation_table)) | |
5757 | { | |
5758 | translation_table = Fcopy_sequence (translation_table); | |
5759 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
5760 | if (SYMBOLP (XCAR (val))) | |
5761 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
5762 | } | |
5763 | if (CHAR_TABLE_P (standard)) | |
5764 | { | |
5765 | if (CONSP (translation_table)) | |
5766 | translation_table = nconc2 (translation_table, | |
5767 | Fcons (standard, Qnil)); | |
5768 | else | |
5769 | translation_table = Fcons (translation_table, | |
5770 | Fcons (standard, Qnil)); | |
5771 | } | |
a6f87d34 | 5772 | } |
2170c8f0 KH |
5773 | |
5774 | if (max_lookup) | |
09ee6fdd | 5775 | { |
2170c8f0 KH |
5776 | *max_lookup = 1; |
5777 | if (CHAR_TABLE_P (translation_table) | |
5778 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
5779 | { | |
5780 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
5781 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
5782 | *max_lookup = XFASTINT (val); | |
5783 | } | |
5784 | else if (CONSP (translation_table)) | |
5785 | { | |
5786 | Lisp_Object tail, val; | |
09ee6fdd | 5787 | |
2170c8f0 KH |
5788 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
5789 | if (CHAR_TABLE_P (XCAR (tail)) | |
5790 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
5791 | { | |
5792 | val = XCHAR_TABLE (XCAR (tail))->extras[1]; | |
5793 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
5794 | *max_lookup = XFASTINT (val); | |
5795 | } | |
5796 | } | |
a6f87d34 | 5797 | } |
7d64c6ad KH |
5798 | return translation_table; |
5799 | } | |
5800 | ||
09ee6fdd KH |
5801 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
5802 | do { \ | |
5803 | trans = Qnil; \ | |
5804 | if (CHAR_TABLE_P (table)) \ | |
5805 | { \ | |
5806 | trans = CHAR_TABLE_REF (table, c); \ | |
5807 | if (CHARACTERP (trans)) \ | |
5808 | c = XFASTINT (trans), trans = Qnil; \ | |
5809 | } \ | |
5810 | else if (CONSP (table)) \ | |
5811 | { \ | |
5812 | Lisp_Object tail; \ | |
5813 | \ | |
5814 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
5815 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
5816 | { \ | |
5817 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
5818 | if (CHARACTERP (trans)) \ | |
5819 | c = XFASTINT (trans), trans = Qnil; \ | |
5820 | else if (! NILP (trans)) \ | |
5821 | break; \ | |
5822 | } \ | |
5823 | } \ | |
e6a54062 KH |
5824 | } while (0) |
5825 | ||
7d64c6ad | 5826 | |
69a80ea3 KH |
5827 | static Lisp_Object |
5828 | get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars) | |
5829 | Lisp_Object val; | |
5830 | int *buf, *buf_end; | |
5831 | int last_block; | |
5832 | int *from_nchars, *to_nchars; | |
5833 | { | |
433f7f87 KH |
5834 | /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or |
5835 | [TO-CHAR ...]. */ | |
69a80ea3 KH |
5836 | if (CONSP (val)) |
5837 | { | |
433f7f87 | 5838 | Lisp_Object from, tail; |
69a80ea3 KH |
5839 | int i, len; |
5840 | ||
433f7f87 | 5841 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
69a80ea3 | 5842 | { |
433f7f87 KH |
5843 | val = XCAR (tail); |
5844 | from = XCAR (val); | |
5845 | len = ASIZE (from); | |
5846 | for (i = 0; i < len; i++) | |
5847 | { | |
5848 | if (buf + i == buf_end) | |
5849 | { | |
5850 | if (! last_block) | |
5851 | return Qt; | |
5852 | break; | |
5853 | } | |
5854 | if (XINT (AREF (from, i)) != buf[i]) | |
5855 | break; | |
5856 | } | |
5857 | if (i == len) | |
5858 | { | |
5859 | val = XCDR (val); | |
5860 | *from_nchars = len; | |
5861 | break; | |
5862 | } | |
69a80ea3 | 5863 | } |
433f7f87 KH |
5864 | if (! CONSP (tail)) |
5865 | return Qnil; | |
69a80ea3 KH |
5866 | } |
5867 | if (VECTORP (val)) | |
5868 | *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val); | |
5869 | else | |
5870 | *buf = XINT (val); | |
5871 | return val; | |
5872 | } | |
5873 | ||
5874 | ||
d46c5b12 | 5875 | static int |
69a80ea3 | 5876 | produce_chars (coding, translation_table, last_block) |
df7492f9 | 5877 | struct coding_system *coding; |
69a80ea3 KH |
5878 | Lisp_Object translation_table; |
5879 | int last_block; | |
4ed46869 | 5880 | { |
df7492f9 KH |
5881 | unsigned char *dst = coding->destination + coding->produced; |
5882 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5883 | int produced; | |
5884 | int produced_chars = 0; | |
69a80ea3 | 5885 | int carryover = 0; |
4ed46869 | 5886 | |
df7492f9 | 5887 | if (! coding->chars_at_source) |
4ed46869 | 5888 | { |
df7492f9 | 5889 | /* Characters are in coding->charbuf. */ |
fba4576f AS |
5890 | int *buf = coding->charbuf; |
5891 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 5892 | |
df7492f9 KH |
5893 | if (BUFFERP (coding->src_object) |
5894 | && EQ (coding->src_object, coding->dst_object)) | |
8f924df7 | 5895 | dst_end = ((unsigned char *) coding->source) + coding->consumed; |
4ed46869 | 5896 | |
df7492f9 | 5897 | while (buf < buf_end) |
4ed46869 | 5898 | { |
69a80ea3 | 5899 | int c = *buf, i; |
bc4bc72a | 5900 | |
df7492f9 KH |
5901 | if (c >= 0) |
5902 | { | |
69a80ea3 KH |
5903 | int from_nchars = 1, to_nchars = 1; |
5904 | Lisp_Object trans = Qnil; | |
5905 | ||
09ee6fdd | 5906 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 5907 | if (! NILP (trans)) |
69a80ea3 KH |
5908 | { |
5909 | trans = get_translation (trans, buf, buf_end, last_block, | |
5910 | &from_nchars, &to_nchars); | |
5911 | if (EQ (trans, Qt)) | |
5912 | break; | |
5913 | c = *buf; | |
5914 | } | |
5915 | ||
5916 | if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end) | |
5917 | { | |
5918 | dst = alloc_destination (coding, | |
5919 | buf_end - buf | |
5920 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
5921 | dst); | |
5922 | dst_end = coding->destination + coding->dst_bytes; | |
5923 | } | |
5924 | ||
433f7f87 | 5925 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 5926 | { |
433f7f87 KH |
5927 | if (i > 0) |
5928 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
5929 | if (coding->dst_multibyte |
5930 | || ! CHAR_BYTE8_P (c)) | |
5931 | CHAR_STRING_ADVANCE (c, dst); | |
5932 | else | |
5933 | *dst++ = CHAR_TO_BYTE8 (c); | |
5934 | } | |
5935 | produced_chars += to_nchars; | |
5936 | *buf++ = to_nchars; | |
5937 | while (--from_nchars > 0) | |
5938 | *buf++ = 0; | |
d46c5b12 | 5939 | } |
df7492f9 | 5940 | else |
69a80ea3 KH |
5941 | /* This is an annotation datum. (-C) is the length. */ |
5942 | buf += -c; | |
4ed46869 | 5943 | } |
69a80ea3 | 5944 | carryover = buf_end - buf; |
4ed46869 | 5945 | } |
fa42c37f | 5946 | else |
fa42c37f | 5947 | { |
8f924df7 KH |
5948 | const unsigned char *src = coding->source; |
5949 | const unsigned char *src_end = src + coding->src_bytes; | |
df7492f9 | 5950 | Lisp_Object eol_type; |
fa42c37f | 5951 | |
df7492f9 | 5952 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
4ed46869 | 5953 | |
df7492f9 | 5954 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 5955 | { |
df7492f9 | 5956 | if (coding->src_multibyte) |
fa42c37f | 5957 | { |
71c81426 | 5958 | int multibytep = 1; |
df7492f9 | 5959 | int consumed_chars; |
d46c5b12 | 5960 | |
df7492f9 KH |
5961 | while (1) |
5962 | { | |
8f924df7 | 5963 | const unsigned char *src_base = src; |
df7492f9 | 5964 | int c; |
b73bfc1c | 5965 | |
df7492f9 KH |
5966 | ONE_MORE_BYTE (c); |
5967 | if (c == '\r') | |
5968 | { | |
5969 | if (EQ (eol_type, Qdos)) | |
5970 | { | |
98725083 KH |
5971 | if (src == src_end) |
5972 | { | |
065e3595 KH |
5973 | record_conversion_result |
5974 | (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
98725083 KH |
5975 | goto no_more_source; |
5976 | } | |
5977 | if (*src == '\n') | |
df7492f9 KH |
5978 | c = *src++; |
5979 | } | |
5980 | else if (EQ (eol_type, Qmac)) | |
5981 | c = '\n'; | |
5982 | } | |
5983 | if (dst == dst_end) | |
5984 | { | |
2c78b7e1 | 5985 | coding->consumed = src - coding->source; |
b73bfc1c | 5986 | |
2c78b7e1 | 5987 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 5988 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
5989 | if (dst == dst_end) |
5990 | { | |
5991 | dst = alloc_destination (coding, src_end - src + 1, | |
5992 | dst); | |
5993 | dst_end = coding->destination + coding->dst_bytes; | |
5994 | coding_set_source (coding); | |
5995 | src = coding->source + coding->consumed; | |
5996 | src_end = coding->source + coding->src_bytes; | |
5997 | } | |
df7492f9 KH |
5998 | } |
5999 | *dst++ = c; | |
6000 | produced_chars++; | |
6001 | } | |
6002 | no_more_source: | |
6003 | ; | |
fa42c37f KH |
6004 | } |
6005 | else | |
df7492f9 KH |
6006 | while (src < src_end) |
6007 | { | |
71c81426 | 6008 | int multibytep = 1; |
df7492f9 | 6009 | int c = *src++; |
b73bfc1c | 6010 | |
df7492f9 KH |
6011 | if (c == '\r') |
6012 | { | |
6013 | if (EQ (eol_type, Qdos)) | |
6014 | { | |
6015 | if (src < src_end | |
6016 | && *src == '\n') | |
6017 | c = *src++; | |
6018 | } | |
6019 | else if (EQ (eol_type, Qmac)) | |
6020 | c = '\n'; | |
6021 | } | |
6022 | if (dst >= dst_end - 1) | |
6023 | { | |
2c78b7e1 | 6024 | coding->consumed = src - coding->source; |
df7492f9 | 6025 | |
2c78b7e1 | 6026 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 6027 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
6028 | if (dst >= dst_end - 1) |
6029 | { | |
6030 | dst = alloc_destination (coding, src_end - src + 2, | |
6031 | dst); | |
6032 | dst_end = coding->destination + coding->dst_bytes; | |
6033 | coding_set_source (coding); | |
6034 | src = coding->source + coding->consumed; | |
6035 | src_end = coding->source + coding->src_bytes; | |
6036 | } | |
df7492f9 KH |
6037 | } |
6038 | EMIT_ONE_BYTE (c); | |
6039 | } | |
d46c5b12 | 6040 | } |
df7492f9 KH |
6041 | else |
6042 | { | |
6043 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 6044 | { |
df7492f9 | 6045 | int require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 6046 | |
df7492f9 | 6047 | if (require > 0) |
fa42c37f | 6048 | { |
df7492f9 KH |
6049 | EMACS_INT offset = src - coding->source; |
6050 | ||
6051 | dst = alloc_destination (coding, require, dst); | |
6052 | coding_set_source (coding); | |
6053 | src = coding->source + offset; | |
6054 | src_end = coding->source + coding->src_bytes; | |
fa42c37f KH |
6055 | } |
6056 | } | |
df7492f9 KH |
6057 | produced_chars = coding->src_chars; |
6058 | while (src < src_end) | |
fa42c37f | 6059 | { |
df7492f9 KH |
6060 | int c = *src++; |
6061 | ||
6062 | if (c == '\r') | |
6063 | { | |
6064 | if (EQ (eol_type, Qdos)) | |
6065 | { | |
6066 | if (src < src_end | |
6067 | && *src == '\n') | |
6068 | c = *src++; | |
6069 | produced_chars--; | |
6070 | } | |
6071 | else if (EQ (eol_type, Qmac)) | |
6072 | c = '\n'; | |
6073 | } | |
6074 | *dst++ = c; | |
fa42c37f KH |
6075 | } |
6076 | } | |
2c78b7e1 KH |
6077 | coding->consumed = coding->src_bytes; |
6078 | coding->consumed_char = coding->src_chars; | |
fa42c37f KH |
6079 | } |
6080 | ||
df7492f9 | 6081 | produced = dst - (coding->destination + coding->produced); |
284201e4 | 6082 | if (BUFFERP (coding->dst_object) && produced_chars > 0) |
df7492f9 KH |
6083 | insert_from_gap (produced_chars, produced); |
6084 | coding->produced += produced; | |
6085 | coding->produced_char += produced_chars; | |
69a80ea3 | 6086 | return carryover; |
fa42c37f KH |
6087 | } |
6088 | ||
ff0dacd7 KH |
6089 | /* Compose text in CODING->object according to the annotation data at |
6090 | CHARBUF. CHARBUF is an array: | |
6091 | [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ] | |
df7492f9 | 6092 | */ |
4ed46869 | 6093 | |
df7492f9 | 6094 | static INLINE void |
69a80ea3 | 6095 | produce_composition (coding, charbuf, pos) |
4ed46869 | 6096 | struct coding_system *coding; |
df7492f9 | 6097 | int *charbuf; |
69a80ea3 | 6098 | EMACS_INT pos; |
4ed46869 | 6099 | { |
df7492f9 | 6100 | int len; |
69a80ea3 | 6101 | EMACS_INT to; |
df7492f9 | 6102 | enum composition_method method; |
df7492f9 | 6103 | Lisp_Object components; |
fa42c37f | 6104 | |
df7492f9 | 6105 | len = -charbuf[0]; |
69a80ea3 | 6106 | to = pos + charbuf[2]; |
9ffd559c KH |
6107 | if (to <= pos) |
6108 | return; | |
69a80ea3 | 6109 | method = (enum composition_method) (charbuf[3]); |
d46c5b12 | 6110 | |
df7492f9 KH |
6111 | if (method == COMPOSITION_RELATIVE) |
6112 | components = Qnil; | |
9ffd559c KH |
6113 | else if (method >= COMPOSITION_WITH_RULE |
6114 | && method <= COMPOSITION_WITH_RULE_ALTCHARS) | |
d46c5b12 | 6115 | { |
df7492f9 KH |
6116 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
6117 | int i; | |
b73bfc1c | 6118 | |
69a80ea3 KH |
6119 | len -= 4; |
6120 | charbuf += 4; | |
df7492f9 | 6121 | for (i = 0; i < len; i++) |
9ffd559c KH |
6122 | { |
6123 | args[i] = make_number (charbuf[i]); | |
f75c90a9 | 6124 | if (charbuf[i] < 0) |
9ffd559c KH |
6125 | return; |
6126 | } | |
df7492f9 KH |
6127 | components = (method == COMPOSITION_WITH_ALTCHARS |
6128 | ? Fstring (len, args) : Fvector (len, args)); | |
d46c5b12 | 6129 | } |
9ffd559c KH |
6130 | else |
6131 | return; | |
69a80ea3 | 6132 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
6133 | } |
6134 | ||
d46c5b12 | 6135 | |
ff0dacd7 KH |
6136 | /* Put `charset' property on text in CODING->object according to |
6137 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 6138 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 6139 | */ |
d46c5b12 | 6140 | |
ff0dacd7 | 6141 | static INLINE void |
69a80ea3 | 6142 | produce_charset (coding, charbuf, pos) |
d46c5b12 | 6143 | struct coding_system *coding; |
ff0dacd7 | 6144 | int *charbuf; |
69a80ea3 | 6145 | EMACS_INT pos; |
d46c5b12 | 6146 | { |
69a80ea3 KH |
6147 | EMACS_INT from = pos - charbuf[2]; |
6148 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); | |
b73bfc1c | 6149 | |
69a80ea3 | 6150 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
6151 | Qcharset, CHARSET_NAME (charset), |
6152 | coding->dst_object); | |
d46c5b12 KH |
6153 | } |
6154 | ||
d46c5b12 | 6155 | |
df7492f9 KH |
6156 | #define CHARBUF_SIZE 0x4000 |
6157 | ||
6158 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
6159 | do { \ | |
6160 | int size = CHARBUF_SIZE;; \ | |
6161 | \ | |
6162 | coding->charbuf = NULL; \ | |
6163 | while (size > 1024) \ | |
6164 | { \ | |
6165 | coding->charbuf = (int *) alloca (sizeof (int) * size); \ | |
6166 | if (coding->charbuf) \ | |
6167 | break; \ | |
6168 | size >>= 1; \ | |
6169 | } \ | |
6170 | if (! coding->charbuf) \ | |
6171 | { \ | |
065e3595 | 6172 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \ |
df7492f9 KH |
6173 | return coding->result; \ |
6174 | } \ | |
6175 | coding->charbuf_size = size; \ | |
6176 | } while (0) | |
4ed46869 | 6177 | |
d46c5b12 KH |
6178 | |
6179 | static void | |
69a80ea3 | 6180 | produce_annotation (coding, pos) |
d46c5b12 | 6181 | struct coding_system *coding; |
69a80ea3 | 6182 | EMACS_INT pos; |
d46c5b12 | 6183 | { |
df7492f9 KH |
6184 | int *charbuf = coding->charbuf; |
6185 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 6186 | |
ff0dacd7 KH |
6187 | if (NILP (coding->dst_object)) |
6188 | return; | |
d46c5b12 | 6189 | |
df7492f9 | 6190 | while (charbuf < charbuf_end) |
a84f1519 | 6191 | { |
df7492f9 | 6192 | if (*charbuf >= 0) |
69a80ea3 | 6193 | pos += *charbuf++; |
d46c5b12 | 6194 | else |
d46c5b12 | 6195 | { |
df7492f9 | 6196 | int len = -*charbuf; |
ff0dacd7 | 6197 | switch (charbuf[1]) |
df7492f9 KH |
6198 | { |
6199 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
69a80ea3 | 6200 | produce_composition (coding, charbuf, pos); |
df7492f9 | 6201 | break; |
ff0dacd7 | 6202 | case CODING_ANNOTATE_CHARSET_MASK: |
69a80ea3 | 6203 | produce_charset (coding, charbuf, pos); |
ff0dacd7 | 6204 | break; |
df7492f9 KH |
6205 | default: |
6206 | abort (); | |
6207 | } | |
6208 | charbuf += len; | |
d46c5b12 | 6209 | } |
a84f1519 | 6210 | } |
d46c5b12 KH |
6211 | } |
6212 | ||
df7492f9 KH |
6213 | /* Decode the data at CODING->src_object into CODING->dst_object. |
6214 | CODING->src_object is a buffer, a string, or nil. | |
6215 | CODING->dst_object is a buffer. | |
d46c5b12 | 6216 | |
df7492f9 KH |
6217 | If CODING->src_object is a buffer, it must be the current buffer. |
6218 | In this case, if CODING->src_pos is positive, it is a position of | |
6219 | the source text in the buffer, otherwise, the source text is in the | |
6220 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
6221 | the text from GPT (which must be the same as PT). If this is the | |
6222 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6223 | negative. | |
d46c5b12 | 6224 | |
b6828792 | 6225 | If CODING->src_object is a string, CODING->src_pos is an index to |
df7492f9 | 6226 | that string. |
d46c5b12 | 6227 | |
df7492f9 KH |
6228 | If CODING->src_object is nil, CODING->source must already point to |
6229 | the non-relocatable memory area. In this case, CODING->src_pos is | |
6230 | an offset from CODING->source. | |
73be902c | 6231 | |
df7492f9 KH |
6232 | The decoded data is inserted at the current point of the buffer |
6233 | CODING->dst_object. | |
6234 | */ | |
d46c5b12 | 6235 | |
df7492f9 KH |
6236 | static int |
6237 | decode_coding (coding) | |
d46c5b12 | 6238 | struct coding_system *coding; |
d46c5b12 | 6239 | { |
df7492f9 | 6240 | Lisp_Object attrs; |
24a73b0a | 6241 | Lisp_Object undo_list; |
7d64c6ad | 6242 | Lisp_Object translation_table; |
69a80ea3 KH |
6243 | int carryover; |
6244 | int i; | |
d46c5b12 | 6245 | |
df7492f9 KH |
6246 | if (BUFFERP (coding->src_object) |
6247 | && coding->src_pos > 0 | |
6248 | && coding->src_pos < GPT | |
6249 | && coding->src_pos + coding->src_chars > GPT) | |
6250 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 6251 | |
24a73b0a | 6252 | undo_list = Qt; |
df7492f9 | 6253 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 6254 | { |
df7492f9 KH |
6255 | if (current_buffer != XBUFFER (coding->dst_object)) |
6256 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
6257 | if (GPT != PT) | |
6258 | move_gap_both (PT, PT_BYTE); | |
24a73b0a KH |
6259 | undo_list = current_buffer->undo_list; |
6260 | current_buffer->undo_list = Qt; | |
1c3478b0 KH |
6261 | } |
6262 | ||
df7492f9 KH |
6263 | coding->consumed = coding->consumed_char = 0; |
6264 | coding->produced = coding->produced_char = 0; | |
6265 | coding->chars_at_source = 0; | |
065e3595 | 6266 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 6267 | coding->errors = 0; |
1c3478b0 | 6268 | |
df7492f9 KH |
6269 | ALLOC_CONVERSION_WORK_AREA (coding); |
6270 | ||
6271 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 6272 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 6273 | |
69a80ea3 | 6274 | carryover = 0; |
df7492f9 | 6275 | do |
b73bfc1c | 6276 | { |
69a80ea3 KH |
6277 | EMACS_INT pos = coding->dst_pos + coding->produced_char; |
6278 | ||
df7492f9 KH |
6279 | coding_set_source (coding); |
6280 | coding->annotated = 0; | |
69a80ea3 | 6281 | coding->charbuf_used = carryover; |
df7492f9 | 6282 | (*(coding->decoder)) (coding); |
df7492f9 | 6283 | coding_set_destination (coding); |
69a80ea3 | 6284 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 6285 | if (coding->annotated) |
69a80ea3 KH |
6286 | produce_annotation (coding, pos); |
6287 | for (i = 0; i < carryover; i++) | |
6288 | coding->charbuf[i] | |
6289 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 6290 | } |
df7492f9 | 6291 | while (coding->consumed < coding->src_bytes |
54b367bb KH |
6292 | && (coding->result == CODING_RESULT_SUCCESS |
6293 | || coding->result == CODING_RESULT_INVALID_SRC)); | |
d46c5b12 | 6294 | |
69a80ea3 KH |
6295 | if (carryover > 0) |
6296 | { | |
6297 | coding_set_destination (coding); | |
6298 | coding->charbuf_used = carryover; | |
6299 | produce_chars (coding, translation_table, 1); | |
6300 | } | |
6301 | ||
df7492f9 KH |
6302 | coding->carryover_bytes = 0; |
6303 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 6304 | { |
df7492f9 | 6305 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 6306 | const unsigned char *src; |
df7492f9 KH |
6307 | |
6308 | coding_set_source (coding); | |
6309 | coding_set_destination (coding); | |
6310 | src = coding->source + coding->consumed; | |
6311 | ||
6312 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 6313 | { |
df7492f9 KH |
6314 | /* Flush out unprocessed data as binary chars. We are sure |
6315 | that the number of data is less than the size of | |
6316 | coding->charbuf. */ | |
065e3595 | 6317 | coding->charbuf_used = 0; |
df7492f9 | 6318 | while (nbytes-- > 0) |
1c3478b0 | 6319 | { |
df7492f9 | 6320 | int c = *src++; |
98725083 | 6321 | |
1c91457d KH |
6322 | if (c & 0x80) |
6323 | c = BYTE8_TO_CHAR (c); | |
6324 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 6325 | } |
f6cbaf43 | 6326 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 6327 | } |
d46c5b12 | 6328 | else |
df7492f9 KH |
6329 | { |
6330 | /* Record unprocessed bytes in coding->carryover. We are | |
6331 | sure that the number of data is less than the size of | |
6332 | coding->carryover. */ | |
6333 | unsigned char *p = coding->carryover; | |
6334 | ||
6335 | coding->carryover_bytes = nbytes; | |
6336 | while (nbytes-- > 0) | |
6337 | *p++ = *src++; | |
1c3478b0 | 6338 | } |
df7492f9 | 6339 | coding->consumed = coding->src_bytes; |
b73bfc1c | 6340 | } |
69f76525 | 6341 | |
4347441b KH |
6342 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix)) |
6343 | decode_eol (coding); | |
24a73b0a KH |
6344 | if (BUFFERP (coding->dst_object)) |
6345 | { | |
6346 | current_buffer->undo_list = undo_list; | |
6347 | record_insert (coding->dst_pos, coding->produced_char); | |
6348 | } | |
73be902c | 6349 | return coding->result; |
4ed46869 KH |
6350 | } |
6351 | ||
aaaf0b1e | 6352 | |
e1c23804 | 6353 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
6354 | ending before LIMIT of CODING->src_object (buffer or string), store |
6355 | the data in BUF, set *STOP to a starting position of the next | |
6356 | composition (if any) or to LIMIT, and return the address of the | |
6357 | next element of BUF. | |
6358 | ||
6359 | If such an annotation is not found, set *STOP to a starting | |
6360 | position of a composition after POS (if any) or to LIMIT, and | |
6361 | return BUF. */ | |
6362 | ||
6363 | static INLINE int * | |
6364 | handle_composition_annotation (pos, limit, coding, buf, stop) | |
6365 | EMACS_INT pos, limit; | |
aaaf0b1e | 6366 | struct coding_system *coding; |
ff0dacd7 KH |
6367 | int *buf; |
6368 | EMACS_INT *stop; | |
aaaf0b1e | 6369 | { |
ff0dacd7 KH |
6370 | EMACS_INT start, end; |
6371 | Lisp_Object prop; | |
aaaf0b1e | 6372 | |
ff0dacd7 KH |
6373 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
6374 | || end > limit) | |
6375 | *stop = limit; | |
6376 | else if (start > pos) | |
6377 | *stop = start; | |
6378 | else | |
aaaf0b1e | 6379 | { |
ff0dacd7 | 6380 | if (start == pos) |
aaaf0b1e | 6381 | { |
ff0dacd7 KH |
6382 | /* We found a composition. Store the corresponding |
6383 | annotation data in BUF. */ | |
6384 | int *head = buf; | |
6385 | enum composition_method method = COMPOSITION_METHOD (prop); | |
6386 | int nchars = COMPOSITION_LENGTH (prop); | |
6387 | ||
69a80ea3 | 6388 | ADD_COMPOSITION_DATA (buf, nchars, method); |
ff0dacd7 | 6389 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 6390 | { |
ff0dacd7 KH |
6391 | Lisp_Object components; |
6392 | int len, i, i_byte; | |
6393 | ||
6394 | components = COMPOSITION_COMPONENTS (prop); | |
6395 | if (VECTORP (components)) | |
aaaf0b1e | 6396 | { |
ff0dacd7 KH |
6397 | len = XVECTOR (components)->size; |
6398 | for (i = 0; i < len; i++) | |
6399 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 6400 | } |
ff0dacd7 | 6401 | else if (STRINGP (components)) |
aaaf0b1e | 6402 | { |
8f924df7 | 6403 | len = SCHARS (components); |
ff0dacd7 KH |
6404 | i = i_byte = 0; |
6405 | while (i < len) | |
6406 | { | |
6407 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
6408 | buf++; | |
6409 | } | |
6410 | } | |
6411 | else if (INTEGERP (components)) | |
6412 | { | |
6413 | len = 1; | |
6414 | *buf++ = XINT (components); | |
6415 | } | |
6416 | else if (CONSP (components)) | |
6417 | { | |
6418 | for (len = 0; CONSP (components); | |
6419 | len++, components = XCDR (components)) | |
6420 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 6421 | } |
aaaf0b1e | 6422 | else |
ff0dacd7 KH |
6423 | abort (); |
6424 | *head -= len; | |
aaaf0b1e | 6425 | } |
aaaf0b1e | 6426 | } |
ff0dacd7 KH |
6427 | |
6428 | if (find_composition (end, limit, &start, &end, &prop, | |
6429 | coding->src_object) | |
6430 | && end <= limit) | |
6431 | *stop = start; | |
6432 | else | |
6433 | *stop = limit; | |
aaaf0b1e | 6434 | } |
ff0dacd7 KH |
6435 | return buf; |
6436 | } | |
6437 | ||
6438 | ||
e1c23804 | 6439 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
6440 | CODING->src_object (buffer of string), store the data in BUF, set |
6441 | *STOP to the position where the value of `charset' property changes | |
6442 | (limiting by LIMIT), and return the address of the next element of | |
6443 | BUF. | |
6444 | ||
6445 | If the property value is nil, set *STOP to the position where the | |
6446 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
6447 | ||
6448 | static INLINE int * | |
6449 | handle_charset_annotation (pos, limit, coding, buf, stop) | |
6450 | EMACS_INT pos, limit; | |
6451 | struct coding_system *coding; | |
6452 | int *buf; | |
6453 | EMACS_INT *stop; | |
6454 | { | |
6455 | Lisp_Object val, next; | |
6456 | int id; | |
6457 | ||
6458 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
6459 | if (! NILP (val) && CHARSETP (val)) | |
6460 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
6461 | else | |
6462 | id = -1; | |
69a80ea3 | 6463 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
6464 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
6465 | coding->src_object, | |
6466 | make_number (limit)); | |
6467 | *stop = XINT (next); | |
6468 | return buf; | |
6469 | } | |
6470 | ||
6471 | ||
df7492f9 | 6472 | static void |
09ee6fdd | 6473 | consume_chars (coding, translation_table, max_lookup) |
df7492f9 | 6474 | struct coding_system *coding; |
433f7f87 | 6475 | Lisp_Object translation_table; |
09ee6fdd | 6476 | int max_lookup; |
df7492f9 KH |
6477 | { |
6478 | int *buf = coding->charbuf; | |
ff0dacd7 | 6479 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 6480 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 6481 | const unsigned char *src_end = coding->source + coding->src_bytes; |
ff0dacd7 KH |
6482 | EMACS_INT pos = coding->src_pos + coding->consumed_char; |
6483 | EMACS_INT end_pos = coding->src_pos + coding->src_chars; | |
df7492f9 KH |
6484 | int multibytep = coding->src_multibyte; |
6485 | Lisp_Object eol_type; | |
6486 | int c; | |
ff0dacd7 | 6487 | EMACS_INT stop, stop_composition, stop_charset; |
09ee6fdd | 6488 | int *lookup_buf = NULL; |
433f7f87 KH |
6489 | |
6490 | if (! NILP (translation_table)) | |
09ee6fdd | 6491 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 6492 | |
df7492f9 KH |
6493 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
6494 | if (VECTORP (eol_type)) | |
6495 | eol_type = Qunix; | |
88993dfd | 6496 | |
df7492f9 KH |
6497 | /* Note: composition handling is not yet implemented. */ |
6498 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 6499 | |
0b5670c9 KH |
6500 | if (NILP (coding->src_object)) |
6501 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 6502 | else |
0b5670c9 KH |
6503 | { |
6504 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
6505 | stop = stop_composition = pos; | |
6506 | else | |
6507 | stop = stop_composition = end_pos; | |
6508 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
6509 | stop = stop_charset = pos; | |
6510 | else | |
6511 | stop_charset = end_pos; | |
6512 | } | |
ec6d2bb8 | 6513 | |
24a73b0a | 6514 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 6515 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 6516 | while (buf < buf_end) |
aaaf0b1e | 6517 | { |
433f7f87 KH |
6518 | Lisp_Object trans; |
6519 | ||
df7492f9 | 6520 | if (pos == stop) |
ec6d2bb8 | 6521 | { |
df7492f9 KH |
6522 | if (pos == end_pos) |
6523 | break; | |
ff0dacd7 KH |
6524 | if (pos == stop_composition) |
6525 | buf = handle_composition_annotation (pos, end_pos, coding, | |
6526 | buf, &stop_composition); | |
6527 | if (pos == stop_charset) | |
6528 | buf = handle_charset_annotation (pos, end_pos, coding, | |
6529 | buf, &stop_charset); | |
6530 | stop = (stop_composition < stop_charset | |
6531 | ? stop_composition : stop_charset); | |
df7492f9 KH |
6532 | } |
6533 | ||
6534 | if (! multibytep) | |
4776e638 | 6535 | { |
d3e4cb56 | 6536 | EMACS_INT bytes; |
aaaf0b1e | 6537 | |
ea29edf2 KH |
6538 | if (coding->encoder == encode_coding_raw_text) |
6539 | c = *src++, pos++; | |
6540 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
4776e638 KH |
6541 | c = STRING_CHAR_ADVANCE (src), pos += bytes; |
6542 | else | |
f03caae0 | 6543 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 6544 | } |
df7492f9 | 6545 | else |
4776e638 | 6546 | c = STRING_CHAR_ADVANCE (src), pos++; |
df7492f9 KH |
6547 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
6548 | c = '\n'; | |
6549 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 6550 | { |
df7492f9 | 6551 | if (c == '\n') |
aaaf0b1e | 6552 | { |
df7492f9 KH |
6553 | if (EQ (eol_type, Qdos)) |
6554 | *buf++ = '\r'; | |
6555 | else | |
6556 | c = '\r'; | |
aaaf0b1e KH |
6557 | } |
6558 | } | |
433f7f87 | 6559 | |
e6a54062 | 6560 | trans = Qnil; |
09ee6fdd | 6561 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 6562 | if (NILP (trans)) |
433f7f87 KH |
6563 | *buf++ = c; |
6564 | else | |
6565 | { | |
6566 | int from_nchars = 1, to_nchars = 1; | |
6567 | int *lookup_buf_end; | |
6568 | const unsigned char *p = src; | |
6569 | int i; | |
6570 | ||
6571 | lookup_buf[0] = c; | |
6572 | for (i = 1; i < max_lookup && p < src_end; i++) | |
6573 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
6574 | lookup_buf_end = lookup_buf + i; | |
6575 | trans = get_translation (trans, lookup_buf, lookup_buf_end, 1, | |
6576 | &from_nchars, &to_nchars); | |
6577 | if (EQ (trans, Qt) | |
6578 | || buf + to_nchars > buf_end) | |
6579 | break; | |
6580 | *buf++ = *lookup_buf; | |
6581 | for (i = 1; i < to_nchars; i++) | |
6582 | *buf++ = XINT (AREF (trans, i)); | |
6583 | for (i = 1; i < from_nchars; i++, pos++) | |
6584 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
6585 | } | |
aaaf0b1e | 6586 | } |
ec6d2bb8 | 6587 | |
df7492f9 KH |
6588 | coding->consumed = src - coding->source; |
6589 | coding->consumed_char = pos - coding->src_pos; | |
6590 | coding->charbuf_used = buf - coding->charbuf; | |
6591 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
6592 | } |
6593 | ||
4ed46869 | 6594 | |
df7492f9 KH |
6595 | /* Encode the text at CODING->src_object into CODING->dst_object. |
6596 | CODING->src_object is a buffer or a string. | |
6597 | CODING->dst_object is a buffer or nil. | |
6598 | ||
6599 | If CODING->src_object is a buffer, it must be the current buffer. | |
6600 | In this case, if CODING->src_pos is positive, it is a position of | |
6601 | the source text in the buffer, otherwise. the source text is in the | |
6602 | gap area of the buffer, and coding->src_pos specifies the offset of | |
6603 | the text from GPT (which must be the same as PT). If this is the | |
6604 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6605 | negative and CODING should not have `pre-write-conversion'. | |
6606 | ||
6607 | If CODING->src_object is a string, CODING should not have | |
6608 | `pre-write-conversion'. | |
6609 | ||
6610 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
6611 | the current point of that buffer. | |
6612 | ||
6613 | If CODING->dst_object is nil, the encoded data is placed at the | |
6614 | memory area specified by CODING->destination. */ | |
6615 | ||
6616 | static int | |
6617 | encode_coding (coding) | |
4ed46869 | 6618 | struct coding_system *coding; |
4ed46869 | 6619 | { |
df7492f9 | 6620 | Lisp_Object attrs; |
7d64c6ad | 6621 | Lisp_Object translation_table; |
09ee6fdd | 6622 | int max_lookup; |
9861e777 | 6623 | |
df7492f9 | 6624 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
6625 | if (coding->encoder == encode_coding_raw_text) |
6626 | translation_table = Qnil, max_lookup = 0; | |
6627 | else | |
6628 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 6629 | |
df7492f9 | 6630 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 6631 | { |
df7492f9 KH |
6632 | set_buffer_internal (XBUFFER (coding->dst_object)); |
6633 | coding->dst_multibyte | |
6634 | = ! NILP (current_buffer->enable_multibyte_characters); | |
8844fa83 | 6635 | } |
4ed46869 | 6636 | |
b73bfc1c | 6637 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 6638 | coding->produced = coding->produced_char = 0; |
065e3595 | 6639 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 6640 | coding->errors = 0; |
b73bfc1c | 6641 | |
df7492f9 | 6642 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 6643 | |
df7492f9 KH |
6644 | do { |
6645 | coding_set_source (coding); | |
09ee6fdd | 6646 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
6647 | coding_set_destination (coding); |
6648 | (*(coding->encoder)) (coding); | |
6649 | } while (coding->consumed_char < coding->src_chars); | |
6650 | ||
284201e4 | 6651 | if (BUFFERP (coding->dst_object) && coding->produced_char > 0) |
df7492f9 KH |
6652 | insert_from_gap (coding->produced_char, coding->produced); |
6653 | ||
6654 | return (coding->result); | |
ec6d2bb8 KH |
6655 | } |
6656 | ||
fb88bf2d | 6657 | |
24a73b0a KH |
6658 | /* Name (or base name) of work buffer for code conversion. */ |
6659 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 6660 | |
24a73b0a KH |
6661 | /* A working buffer used by the top level conversion. Once it is |
6662 | created, it is never destroyed. It has the name | |
6663 | Vcode_conversion_workbuf_name. The other working buffers are | |
6664 | destroyed after the use is finished, and their names are modified | |
6665 | versions of Vcode_conversion_workbuf_name. */ | |
6666 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 6667 | |
24a73b0a KH |
6668 | /* 1 iff Vcode_conversion_reused_workbuf is already in use. */ |
6669 | static int reused_workbuf_in_use; | |
4ed46869 | 6670 | |
24a73b0a KH |
6671 | |
6672 | /* Return a working buffer of code convesion. MULTIBYTE specifies the | |
6673 | multibyteness of returning buffer. */ | |
b73bfc1c | 6674 | |
f6cbaf43 | 6675 | static Lisp_Object |
24a73b0a | 6676 | make_conversion_work_buffer (multibyte) |
f6cbaf43 | 6677 | int multibyte; |
df7492f9 | 6678 | { |
24a73b0a KH |
6679 | Lisp_Object name, workbuf; |
6680 | struct buffer *current; | |
4ed46869 | 6681 | |
24a73b0a | 6682 | if (reused_workbuf_in_use++) |
065e3595 KH |
6683 | { |
6684 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
6685 | workbuf = Fget_buffer_create (name); | |
6686 | } | |
df7492f9 | 6687 | else |
065e3595 KH |
6688 | { |
6689 | name = Vcode_conversion_workbuf_name; | |
6690 | workbuf = Fget_buffer_create (name); | |
6691 | if (NILP (Vcode_conversion_reused_workbuf)) | |
6692 | Vcode_conversion_reused_workbuf = workbuf; | |
6693 | } | |
24a73b0a KH |
6694 | current = current_buffer; |
6695 | set_buffer_internal (XBUFFER (workbuf)); | |
6696 | Ferase_buffer (); | |
df7492f9 | 6697 | current_buffer->undo_list = Qt; |
24a73b0a | 6698 | current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil; |
df7492f9 | 6699 | set_buffer_internal (current); |
24a73b0a | 6700 | return workbuf; |
df7492f9 | 6701 | } |
d46c5b12 | 6702 | |
24a73b0a | 6703 | |
4776e638 | 6704 | static Lisp_Object |
24a73b0a KH |
6705 | code_conversion_restore (arg) |
6706 | Lisp_Object arg; | |
4776e638 | 6707 | { |
24a73b0a | 6708 | Lisp_Object current, workbuf; |
948bdcf3 | 6709 | struct gcpro gcpro1; |
24a73b0a | 6710 | |
948bdcf3 | 6711 | GCPRO1 (arg); |
24a73b0a KH |
6712 | current = XCAR (arg); |
6713 | workbuf = XCDR (arg); | |
6714 | if (! NILP (workbuf)) | |
6715 | { | |
6716 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
6717 | reused_workbuf_in_use = 0; | |
6718 | else if (! NILP (Fbuffer_live_p (workbuf))) | |
6719 | Fkill_buffer (workbuf); | |
6720 | } | |
6721 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 6722 | UNGCPRO; |
4776e638 KH |
6723 | return Qnil; |
6724 | } | |
b73bfc1c | 6725 | |
24a73b0a KH |
6726 | Lisp_Object |
6727 | code_conversion_save (with_work_buf, multibyte) | |
4776e638 | 6728 | int with_work_buf, multibyte; |
df7492f9 | 6729 | { |
24a73b0a | 6730 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 6731 | |
4776e638 | 6732 | if (with_work_buf) |
24a73b0a KH |
6733 | workbuf = make_conversion_work_buffer (multibyte); |
6734 | record_unwind_protect (code_conversion_restore, | |
6735 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 6736 | return workbuf; |
df7492f9 | 6737 | } |
d46c5b12 | 6738 | |
df7492f9 KH |
6739 | int |
6740 | decode_coding_gap (coding, chars, bytes) | |
6741 | struct coding_system *coding; | |
6742 | EMACS_INT chars, bytes; | |
6743 | { | |
6744 | int count = specpdl_ptr - specpdl; | |
5e5c78be | 6745 | Lisp_Object attrs; |
fb88bf2d | 6746 | |
24a73b0a | 6747 | code_conversion_save (0, 0); |
ec6d2bb8 | 6748 | |
24a73b0a | 6749 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
6750 | coding->src_chars = chars; |
6751 | coding->src_bytes = bytes; | |
6752 | coding->src_pos = -chars; | |
6753 | coding->src_pos_byte = -bytes; | |
6754 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 6755 | coding->dst_object = coding->src_object; |
df7492f9 KH |
6756 | coding->dst_pos = PT; |
6757 | coding->dst_pos_byte = PT_BYTE; | |
71c81426 | 6758 | coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters); |
4ed46869 | 6759 | |
df7492f9 KH |
6760 | if (CODING_REQUIRE_DETECTION (coding)) |
6761 | detect_coding (coding); | |
8f924df7 | 6762 | |
9286b333 | 6763 | coding->mode |= CODING_MODE_LAST_BLOCK; |
287c57d7 | 6764 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 | 6765 | decode_coding (coding); |
287c57d7 | 6766 | current_buffer->text->inhibit_shrinking = 0; |
d46c5b12 | 6767 | |
5e5c78be KH |
6768 | attrs = CODING_ID_ATTRS (coding->id); |
6769 | if (! NILP (CODING_ATTR_POST_READ (attrs))) | |
b73bfc1c | 6770 | { |
5e5c78be KH |
6771 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
6772 | Lisp_Object val; | |
6773 | ||
6774 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
6775 | val = call1 (CODING_ATTR_POST_READ (attrs), |
6776 | make_number (coding->produced_char)); | |
5e5c78be KH |
6777 | CHECK_NATNUM (val); |
6778 | coding->produced_char += Z - prev_Z; | |
6779 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 6780 | } |
4ed46869 | 6781 | |
df7492f9 | 6782 | unbind_to (count, Qnil); |
b73bfc1c KH |
6783 | return coding->result; |
6784 | } | |
52d41803 | 6785 | |
4ed46869 | 6786 | int |
df7492f9 | 6787 | encode_coding_gap (coding, chars, bytes) |
4ed46869 | 6788 | struct coding_system *coding; |
df7492f9 | 6789 | EMACS_INT chars, bytes; |
4ed46869 | 6790 | { |
df7492f9 | 6791 | int count = specpdl_ptr - specpdl; |
4ed46869 | 6792 | |
24a73b0a | 6793 | code_conversion_save (0, 0); |
4ed46869 | 6794 | |
24a73b0a | 6795 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
6796 | coding->src_chars = chars; |
6797 | coding->src_bytes = bytes; | |
6798 | coding->src_pos = -chars; | |
6799 | coding->src_pos_byte = -bytes; | |
6800 | coding->src_multibyte = chars < bytes; | |
6801 | coding->dst_object = coding->src_object; | |
6802 | coding->dst_pos = PT; | |
6803 | coding->dst_pos_byte = PT_BYTE; | |
4ed46869 | 6804 | |
df7492f9 | 6805 | encode_coding (coding); |
b73bfc1c | 6806 | |
df7492f9 KH |
6807 | unbind_to (count, Qnil); |
6808 | return coding->result; | |
6809 | } | |
4ed46869 | 6810 | |
d46c5b12 | 6811 | |
df7492f9 KH |
6812 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
6813 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 6814 | |
df7492f9 | 6815 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 6816 | |
df7492f9 KH |
6817 | If it is a buffer, the text is at point of the buffer. FROM and TO |
6818 | are positions in the buffer. | |
b73bfc1c | 6819 | |
df7492f9 KH |
6820 | If it is a string, the text is at the beginning of the string. |
6821 | FROM and TO are indices to the string. | |
4ed46869 | 6822 | |
df7492f9 KH |
6823 | If it is nil, the text is at coding->source. FROM and TO are |
6824 | indices to coding->source. | |
bb10be8b | 6825 | |
df7492f9 | 6826 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 6827 | |
df7492f9 KH |
6828 | If it is a buffer, the decoded text is inserted at point of the |
6829 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
6830 | is deleted. | |
4ed46869 | 6831 | |
df7492f9 KH |
6832 | If it is Qt, a string is made from the decoded text, and |
6833 | set in CODING->dst_object. | |
d46c5b12 | 6834 | |
df7492f9 | 6835 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 6836 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
6837 | CODING->destination by xmalloc. If the decoded text is longer than |
6838 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
6839 | */ | |
d46c5b12 | 6840 | |
df7492f9 KH |
6841 | void |
6842 | decode_coding_object (coding, src_object, from, from_byte, to, to_byte, | |
6843 | dst_object) | |
d46c5b12 | 6844 | struct coding_system *coding; |
df7492f9 KH |
6845 | Lisp_Object src_object; |
6846 | EMACS_INT from, from_byte, to, to_byte; | |
6847 | Lisp_Object dst_object; | |
d46c5b12 | 6848 | { |
df7492f9 KH |
6849 | int count = specpdl_ptr - specpdl; |
6850 | unsigned char *destination; | |
6851 | EMACS_INT dst_bytes; | |
6852 | EMACS_INT chars = to - from; | |
6853 | EMACS_INT bytes = to_byte - from_byte; | |
6854 | Lisp_Object attrs; | |
4776e638 KH |
6855 | Lisp_Object buffer; |
6856 | int saved_pt = -1, saved_pt_byte; | |
d46c5b12 | 6857 | |
4776e638 | 6858 | buffer = Fcurrent_buffer (); |
93dec019 | 6859 | |
df7492f9 | 6860 | if (NILP (dst_object)) |
d46c5b12 | 6861 | { |
df7492f9 KH |
6862 | destination = coding->destination; |
6863 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 6864 | } |
93dec019 | 6865 | |
df7492f9 KH |
6866 | coding->src_object = src_object; |
6867 | coding->src_chars = chars; | |
6868 | coding->src_bytes = bytes; | |
6869 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 6870 | |
df7492f9 | 6871 | if (STRINGP (src_object)) |
d46c5b12 | 6872 | { |
df7492f9 KH |
6873 | coding->src_pos = from; |
6874 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 6875 | } |
df7492f9 | 6876 | else if (BUFFERP (src_object)) |
88993dfd | 6877 | { |
df7492f9 KH |
6878 | set_buffer_internal (XBUFFER (src_object)); |
6879 | if (from != GPT) | |
6880 | move_gap_both (from, from_byte); | |
6881 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 6882 | { |
4776e638 | 6883 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
6884 | TEMP_SET_PT_BOTH (from, from_byte); |
6885 | del_range_both (from, from_byte, to, to_byte, 1); | |
6886 | coding->src_pos = -chars; | |
6887 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 6888 | } |
df7492f9 | 6889 | else |
fb88bf2d | 6890 | { |
df7492f9 KH |
6891 | coding->src_pos = from; |
6892 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 6893 | } |
88993dfd KH |
6894 | } |
6895 | ||
df7492f9 KH |
6896 | if (CODING_REQUIRE_DETECTION (coding)) |
6897 | detect_coding (coding); | |
6898 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 6899 | |
2cb26057 KH |
6900 | if (EQ (dst_object, Qt) |
6901 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
6902 | && NILP (dst_object))) | |
b73bfc1c | 6903 | { |
24a73b0a | 6904 | coding->dst_object = code_conversion_save (1, 1); |
df7492f9 KH |
6905 | coding->dst_pos = BEG; |
6906 | coding->dst_pos_byte = BEG_BYTE; | |
6907 | coding->dst_multibyte = 1; | |
b73bfc1c | 6908 | } |
df7492f9 | 6909 | else if (BUFFERP (dst_object)) |
d46c5b12 | 6910 | { |
24a73b0a | 6911 | code_conversion_save (0, 0); |
df7492f9 KH |
6912 | coding->dst_object = dst_object; |
6913 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
6914 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
6915 | coding->dst_multibyte | |
6916 | = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters); | |
d46c5b12 KH |
6917 | } |
6918 | else | |
6919 | { | |
24a73b0a | 6920 | code_conversion_save (0, 0); |
df7492f9 KH |
6921 | coding->dst_object = Qnil; |
6922 | coding->dst_multibyte = 1; | |
d46c5b12 KH |
6923 | } |
6924 | ||
df7492f9 | 6925 | decode_coding (coding); |
fa46990e | 6926 | |
df7492f9 KH |
6927 | if (BUFFERP (coding->dst_object)) |
6928 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 6929 | |
df7492f9 | 6930 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 6931 | { |
df7492f9 KH |
6932 | struct gcpro gcpro1, gcpro2; |
6933 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; | |
2b4f9037 | 6934 | Lisp_Object val; |
d46c5b12 | 6935 | |
c0cc7f7f | 6936 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
df7492f9 | 6937 | GCPRO2 (coding->src_object, coding->dst_object); |
d4850d67 KH |
6938 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
6939 | make_number (coding->produced_char)); | |
df7492f9 KH |
6940 | UNGCPRO; |
6941 | CHECK_NATNUM (val); | |
6942 | coding->produced_char += Z - prev_Z; | |
6943 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 6944 | } |
de79a6a5 | 6945 | |
df7492f9 | 6946 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 6947 | { |
df7492f9 KH |
6948 | coding->dst_object = Fbuffer_string (); |
6949 | } | |
6950 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
6951 | { | |
6952 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
6953 | if (dst_bytes < coding->produced) | |
6954 | { | |
6955 | destination | |
6956 | = (unsigned char *) xrealloc (destination, coding->produced); | |
6957 | if (! destination) | |
6958 | { | |
065e3595 KH |
6959 | record_conversion_result (coding, |
6960 | CODING_RESULT_INSUFFICIENT_DST); | |
df7492f9 KH |
6961 | unbind_to (count, Qnil); |
6962 | return; | |
6963 | } | |
6964 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) | |
6965 | move_gap_both (BEGV, BEGV_BYTE); | |
6966 | bcopy (BEGV_ADDR, destination, coding->produced); | |
6967 | coding->destination = destination; | |
d46c5b12 | 6968 | } |
ec6d2bb8 | 6969 | } |
b73bfc1c | 6970 | |
4776e638 KH |
6971 | if (saved_pt >= 0) |
6972 | { | |
6973 | /* This is the case of: | |
6974 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
6975 | As we have moved PT while replacing the original buffer | |
6976 | contents, we must recover it now. */ | |
6977 | set_buffer_internal (XBUFFER (src_object)); | |
6978 | if (saved_pt < from) | |
6979 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
6980 | else if (saved_pt < from + chars) | |
6981 | TEMP_SET_PT_BOTH (from, from_byte); | |
6982 | else if (! NILP (current_buffer->enable_multibyte_characters)) | |
6983 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), | |
6984 | saved_pt_byte + (coding->produced - bytes)); | |
6985 | else | |
6986 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
6987 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 6988 | } |
4776e638 | 6989 | |
065e3595 | 6990 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
6991 | } |
6992 | ||
d46c5b12 | 6993 | |
df7492f9 KH |
6994 | void |
6995 | encode_coding_object (coding, src_object, from, from_byte, to, to_byte, | |
6996 | dst_object) | |
d46c5b12 | 6997 | struct coding_system *coding; |
df7492f9 KH |
6998 | Lisp_Object src_object; |
6999 | EMACS_INT from, from_byte, to, to_byte; | |
7000 | Lisp_Object dst_object; | |
d46c5b12 | 7001 | { |
b73bfc1c | 7002 | int count = specpdl_ptr - specpdl; |
df7492f9 KH |
7003 | EMACS_INT chars = to - from; |
7004 | EMACS_INT bytes = to_byte - from_byte; | |
7005 | Lisp_Object attrs; | |
4776e638 KH |
7006 | Lisp_Object buffer; |
7007 | int saved_pt = -1, saved_pt_byte; | |
c02d943b | 7008 | int kill_src_buffer = 0; |
df7492f9 | 7009 | |
4776e638 | 7010 | buffer = Fcurrent_buffer (); |
df7492f9 KH |
7011 | |
7012 | coding->src_object = src_object; | |
7013 | coding->src_chars = chars; | |
7014 | coding->src_bytes = bytes; | |
7015 | coding->src_multibyte = chars < bytes; | |
7016 | ||
7017 | attrs = CODING_ID_ATTRS (coding->id); | |
7018 | ||
7019 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
6bac5b12 | 7020 | { |
24a73b0a | 7021 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
7022 | set_buffer_internal (XBUFFER (coding->src_object)); |
7023 | if (STRINGP (src_object)) | |
7024 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
7025 | else if (BUFFERP (src_object)) | |
7026 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
7027 | else | |
7028 | insert_1_both (coding->source + from, chars, bytes, 0, 0, 0); | |
d46c5b12 | 7029 | |
df7492f9 KH |
7030 | if (EQ (src_object, dst_object)) |
7031 | { | |
7032 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 7033 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
7034 | del_range_both (from, from_byte, to, to_byte, 1); |
7035 | set_buffer_internal (XBUFFER (coding->src_object)); | |
7036 | } | |
7037 | ||
d4850d67 KH |
7038 | { |
7039 | Lisp_Object args[3]; | |
7040 | ||
7041 | args[0] = CODING_ATTR_PRE_WRITE (attrs); | |
7042 | args[1] = make_number (BEG); | |
7043 | args[2] = make_number (Z); | |
7044 | safe_call (3, args); | |
7045 | } | |
c02d943b KH |
7046 | if (XBUFFER (coding->src_object) != current_buffer) |
7047 | kill_src_buffer = 1; | |
ac87bbef | 7048 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7049 | if (BEG != GPT) |
7050 | move_gap_both (BEG, BEG_BYTE); | |
7051 | coding->src_chars = Z - BEG; | |
7052 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
7053 | coding->src_pos = BEG; | |
7054 | coding->src_pos_byte = BEG_BYTE; | |
7055 | coding->src_multibyte = Z < Z_BYTE; | |
7056 | } | |
7057 | else if (STRINGP (src_object)) | |
d46c5b12 | 7058 | { |
24a73b0a | 7059 | code_conversion_save (0, 0); |
df7492f9 KH |
7060 | coding->src_pos = from; |
7061 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 7062 | } |
df7492f9 | 7063 | else if (BUFFERP (src_object)) |
b73bfc1c | 7064 | { |
24a73b0a | 7065 | code_conversion_save (0, 0); |
df7492f9 | 7066 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 7067 | if (EQ (src_object, dst_object)) |
d46c5b12 | 7068 | { |
4776e638 | 7069 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
7070 | coding->src_object = del_range_1 (from, to, 1, 1); |
7071 | coding->src_pos = 0; | |
7072 | coding->src_pos_byte = 0; | |
d46c5b12 | 7073 | } |
df7492f9 | 7074 | else |
d46c5b12 | 7075 | { |
ff0dacd7 KH |
7076 | if (from < GPT && to >= GPT) |
7077 | move_gap_both (from, from_byte); | |
df7492f9 KH |
7078 | coding->src_pos = from; |
7079 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7080 | } |
d46c5b12 | 7081 | } |
4776e638 | 7082 | else |
24a73b0a | 7083 | code_conversion_save (0, 0); |
d46c5b12 | 7084 | |
df7492f9 | 7085 | if (BUFFERP (dst_object)) |
88993dfd | 7086 | { |
df7492f9 | 7087 | coding->dst_object = dst_object; |
28f67a95 KH |
7088 | if (EQ (src_object, dst_object)) |
7089 | { | |
7090 | coding->dst_pos = from; | |
7091 | coding->dst_pos_byte = from_byte; | |
7092 | } | |
7093 | else | |
7094 | { | |
7095 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
7096 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
7097 | } | |
df7492f9 KH |
7098 | coding->dst_multibyte |
7099 | = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters); | |
88993dfd | 7100 | } |
df7492f9 | 7101 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 7102 | { |
df7492f9 | 7103 | coding->dst_object = Qnil; |
df7492f9 | 7104 | coding->dst_bytes = coding->src_chars; |
ac87bbef KH |
7105 | if (coding->dst_bytes == 0) |
7106 | coding->dst_bytes = 1; | |
7107 | coding->destination = (unsigned char *) xmalloc (coding->dst_bytes); | |
df7492f9 | 7108 | coding->dst_multibyte = 0; |
d46c5b12 KH |
7109 | } |
7110 | else | |
7111 | { | |
df7492f9 KH |
7112 | coding->dst_object = Qnil; |
7113 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
7114 | } |
7115 | ||
df7492f9 | 7116 | encode_coding (coding); |
d46c5b12 | 7117 | |
df7492f9 | 7118 | if (EQ (dst_object, Qt)) |
d46c5b12 | 7119 | { |
df7492f9 KH |
7120 | if (BUFFERP (coding->dst_object)) |
7121 | coding->dst_object = Fbuffer_string (); | |
7122 | else | |
d46c5b12 | 7123 | { |
df7492f9 KH |
7124 | coding->dst_object |
7125 | = make_unibyte_string ((char *) coding->destination, | |
7126 | coding->produced); | |
7127 | xfree (coding->destination); | |
d46c5b12 | 7128 | } |
4ed46869 | 7129 | } |
d46c5b12 | 7130 | |
4776e638 KH |
7131 | if (saved_pt >= 0) |
7132 | { | |
7133 | /* This is the case of: | |
7134 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7135 | As we have moved PT while replacing the original buffer | |
7136 | contents, we must recover it now. */ | |
7137 | set_buffer_internal (XBUFFER (src_object)); | |
7138 | if (saved_pt < from) | |
7139 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7140 | else if (saved_pt < from + chars) | |
7141 | TEMP_SET_PT_BOTH (from, from_byte); | |
7142 | else if (! NILP (current_buffer->enable_multibyte_characters)) | |
7143 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), | |
7144 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 7145 | else |
4776e638 KH |
7146 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
7147 | saved_pt_byte + (coding->produced - bytes)); | |
7148 | } | |
7149 | ||
c02d943b KH |
7150 | if (kill_src_buffer) |
7151 | Fkill_buffer (coding->src_object); | |
df7492f9 | 7152 | unbind_to (count, Qnil); |
b73bfc1c KH |
7153 | } |
7154 | ||
df7492f9 | 7155 | |
b73bfc1c | 7156 | Lisp_Object |
df7492f9 | 7157 | preferred_coding_system () |
b73bfc1c | 7158 | { |
df7492f9 | 7159 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 7160 | |
df7492f9 | 7161 | return CODING_ID_NAME (id); |
4ed46869 KH |
7162 | } |
7163 | ||
7164 | \f | |
7165 | #ifdef emacs | |
1397dc18 | 7166 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 7167 | |
4ed46869 | 7168 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 7169 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 7170 | See the documentation of `define-coding-system' for information |
48b0f3ae PJ |
7171 | about coding-system objects. */) |
7172 | (obj) | |
4ed46869 KH |
7173 | Lisp_Object obj; |
7174 | { | |
44e8490d KH |
7175 | if (NILP (obj) |
7176 | || CODING_SYSTEM_ID (obj) >= 0) | |
7177 | return Qt; | |
7178 | if (! SYMBOLP (obj) | |
7179 | || NILP (Fget (obj, Qcoding_system_define_form))) | |
7180 | return Qnil; | |
7181 | return Qt; | |
4ed46869 KH |
7182 | } |
7183 | ||
9d991de8 RS |
7184 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
7185 | Sread_non_nil_coding_system, 1, 1, 0, | |
48b0f3ae PJ |
7186 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
7187 | (prompt) | |
4ed46869 KH |
7188 | Lisp_Object prompt; |
7189 | { | |
e0e989f6 | 7190 | Lisp_Object val; |
9d991de8 RS |
7191 | do |
7192 | { | |
4608c386 KH |
7193 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
7194 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 7195 | } |
8f924df7 | 7196 | while (SCHARS (val) == 0); |
e0e989f6 | 7197 | return (Fintern (val, Qnil)); |
4ed46869 KH |
7198 | } |
7199 | ||
9b787f3e | 7200 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae PJ |
7201 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
7202 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */) | |
7203 | (prompt, default_coding_system) | |
9b787f3e | 7204 | Lisp_Object prompt, default_coding_system; |
4ed46869 | 7205 | { |
f44d27ce | 7206 | Lisp_Object val; |
9b787f3e | 7207 | if (SYMBOLP (default_coding_system)) |
a3181084 | 7208 | XSETSTRING (default_coding_system, XPNTR (SYMBOL_NAME (default_coding_system))); |
4608c386 | 7209 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
7210 | Qt, Qnil, Qcoding_system_history, |
7211 | default_coding_system, Qnil); | |
8f924df7 | 7212 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
7213 | } |
7214 | ||
7215 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, | |
7216 | 1, 1, 0, | |
48b0f3ae | 7217 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
7218 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
7219 | It is valid if it is nil or a symbol defined as a coding system by the | |
7220 | function `define-coding-system'. */) | |
df7492f9 | 7221 | (coding_system) |
4ed46869 KH |
7222 | Lisp_Object coding_system; |
7223 | { | |
44e8490d KH |
7224 | Lisp_Object define_form; |
7225 | ||
7226 | define_form = Fget (coding_system, Qcoding_system_define_form); | |
7227 | if (! NILP (define_form)) | |
7228 | { | |
7229 | Fput (coding_system, Qcoding_system_define_form, Qnil); | |
7230 | safe_eval (define_form); | |
7231 | } | |
4ed46869 KH |
7232 | if (!NILP (Fcoding_system_p (coding_system))) |
7233 | return coding_system; | |
fcad4ec4 | 7234 | xsignal1 (Qcoding_system_error, coding_system); |
4ed46869 | 7235 | } |
df7492f9 | 7236 | |
3a73fa5d | 7237 | \f |
89528eb3 KH |
7238 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
7239 | HIGHEST is nonzero, return the coding system of the highest | |
7240 | priority among the detected coding systems. Otherwize return a | |
7241 | list of detected coding systems sorted by their priorities. If | |
7242 | MULTIBYTEP is nonzero, it is assumed that the bytes are in correct | |
7243 | multibyte form but contains only ASCII and eight-bit chars. | |
7244 | Otherwise, the bytes are raw bytes. | |
7245 | ||
7246 | CODING-SYSTEM controls the detection as below: | |
7247 | ||
7248 | If it is nil, detect both text-format and eol-format. If the | |
7249 | text-format part of CODING-SYSTEM is already specified | |
7250 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
7251 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
7252 | detect only text-format. */ | |
7253 | ||
d46c5b12 | 7254 | Lisp_Object |
24a73b0a KH |
7255 | detect_coding_system (src, src_chars, src_bytes, highest, multibytep, |
7256 | coding_system) | |
8f924df7 | 7257 | const unsigned char *src; |
24a73b0a | 7258 | int src_chars, src_bytes, highest; |
0a28aafb | 7259 | int multibytep; |
df7492f9 | 7260 | Lisp_Object coding_system; |
4ed46869 | 7261 | { |
8f924df7 | 7262 | const unsigned char *src_end = src + src_bytes; |
df7492f9 KH |
7263 | Lisp_Object attrs, eol_type; |
7264 | Lisp_Object val; | |
7265 | struct coding_system coding; | |
89528eb3 | 7266 | int id; |
ff0dacd7 | 7267 | struct coding_detection_info detect_info; |
24a73b0a | 7268 | enum coding_category base_category; |
b73bfc1c | 7269 | |
df7492f9 KH |
7270 | if (NILP (coding_system)) |
7271 | coding_system = Qundecided; | |
7272 | setup_coding_system (coding_system, &coding); | |
7273 | attrs = CODING_ID_ATTRS (coding.id); | |
7274 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 7275 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 7276 | |
df7492f9 | 7277 | coding.source = src; |
24a73b0a | 7278 | coding.src_chars = src_chars; |
df7492f9 KH |
7279 | coding.src_bytes = src_bytes; |
7280 | coding.src_multibyte = multibytep; | |
7281 | coding.consumed = 0; | |
89528eb3 | 7282 | coding.mode |= CODING_MODE_LAST_BLOCK; |
d46c5b12 | 7283 | |
ff0dacd7 | 7284 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 7285 | |
89528eb3 | 7286 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
7287 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
7288 | if (base_category == coding_category_undecided) | |
4ed46869 | 7289 | { |
ff0dacd7 KH |
7290 | enum coding_category category; |
7291 | struct coding_system *this; | |
7292 | int c, i; | |
88993dfd | 7293 | |
24a73b0a KH |
7294 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
7295 | for (i = 0; src < src_end; i++, src++) | |
4ed46869 | 7296 | { |
df7492f9 | 7297 | c = *src; |
6cb21a4f | 7298 | if (c & 0x80) |
d46c5b12 | 7299 | break; |
6cb21a4f KH |
7300 | if (c < 0x20 |
7301 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
c7266f4a | 7302 | && ! inhibit_iso_escape_detection) |
6cb21a4f KH |
7303 | { |
7304 | coding.head_ascii = src - coding.source; | |
7305 | if (detect_coding_iso_2022 (&coding, &detect_info)) | |
7306 | { | |
7307 | /* We have scanned the whole data. */ | |
7308 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
7309 | /* We didn't find an 8-bit code. */ | |
7310 | src = src_end; | |
7311 | break; | |
7312 | } | |
7313 | } | |
4ed46869 | 7314 | } |
df7492f9 | 7315 | coding.head_ascii = src - coding.source; |
88993dfd | 7316 | |
6cb21a4f KH |
7317 | if (src < src_end |
7318 | || detect_info.found) | |
7319 | { | |
7320 | if (src == src_end) | |
7321 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
7322 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 7323 | { |
6cb21a4f | 7324 | category = coding_priorities[i]; |
c7266f4a | 7325 | this = coding_categories + category; |
6cb21a4f | 7326 | if (detect_info.found & (1 << category)) |
ff0dacd7 KH |
7327 | break; |
7328 | } | |
6cb21a4f KH |
7329 | else |
7330 | for (i = 0; i < coding_category_raw_text; i++) | |
df7492f9 | 7331 | { |
6cb21a4f KH |
7332 | category = coding_priorities[i]; |
7333 | this = coding_categories + category; | |
7334 | ||
7335 | if (this->id < 0) | |
24a73b0a | 7336 | { |
6cb21a4f KH |
7337 | /* No coding system of this category is defined. */ |
7338 | detect_info.rejected |= (1 << category); | |
7339 | } | |
7340 | else if (category >= coding_category_raw_text) | |
7341 | continue; | |
7342 | else if (detect_info.checked & (1 << category)) | |
7343 | { | |
7344 | if (highest | |
7345 | && (detect_info.found & (1 << category))) | |
7346 | break; | |
7347 | } | |
7348 | else | |
7349 | { | |
7350 | if ((*(this->detector)) (&coding, &detect_info) | |
7351 | && highest | |
7352 | && (detect_info.found & (1 << category))) | |
24a73b0a | 7353 | { |
6cb21a4f KH |
7354 | if (category == coding_category_utf_16_auto) |
7355 | { | |
7356 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
7357 | category = coding_category_utf_16_le; | |
7358 | else | |
7359 | category = coding_category_utf_16_be; | |
7360 | } | |
7361 | break; | |
24a73b0a | 7362 | } |
24a73b0a | 7363 | } |
df7492f9 | 7364 | } |
6cb21a4f | 7365 | } |
ec6d2bb8 | 7366 | |
ff0dacd7 | 7367 | if (detect_info.rejected == CATEGORY_MASK_ANY) |
ec6d2bb8 | 7368 | { |
ff0dacd7 | 7369 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
89528eb3 KH |
7370 | id = coding_categories[coding_category_raw_text].id; |
7371 | val = Fcons (make_number (id), Qnil); | |
7372 | } | |
ff0dacd7 | 7373 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 7374 | { |
ff0dacd7 | 7375 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 KH |
7376 | id = coding_categories[coding_category_undecided].id; |
7377 | val = Fcons (make_number (id), Qnil); | |
7378 | } | |
7379 | else if (highest) | |
7380 | { | |
ff0dacd7 | 7381 | if (detect_info.found) |
ec6d2bb8 | 7382 | { |
ff0dacd7 KH |
7383 | detect_info.found = 1 << category; |
7384 | val = Fcons (make_number (this->id), Qnil); | |
7385 | } | |
7386 | else | |
7387 | for (i = 0; i < coding_category_raw_text; i++) | |
7388 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
7389 | { | |
7390 | detect_info.found = 1 << coding_priorities[i]; | |
7391 | id = coding_categories[coding_priorities[i]].id; | |
7392 | val = Fcons (make_number (id), Qnil); | |
7393 | break; | |
7394 | } | |
7395 | } | |
89528eb3 KH |
7396 | else |
7397 | { | |
ff0dacd7 KH |
7398 | int mask = detect_info.rejected | detect_info.found; |
7399 | int found = 0; | |
89528eb3 | 7400 | val = Qnil; |
ec6d2bb8 | 7401 | |
89528eb3 | 7402 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
7403 | { |
7404 | category = coding_priorities[i]; | |
7405 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 7406 | { |
ff0dacd7 KH |
7407 | found |= 1 << category; |
7408 | id = coding_categories[category].id; | |
c7266f4a KH |
7409 | if (id >= 0) |
7410 | val = Fcons (make_number (id), val); | |
ff0dacd7 KH |
7411 | } |
7412 | } | |
7413 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
7414 | { | |
7415 | category = coding_priorities[i]; | |
7416 | if (detect_info.found & (1 << category)) | |
7417 | { | |
7418 | id = coding_categories[category].id; | |
7419 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 7420 | } |
ec6d2bb8 | 7421 | } |
ff0dacd7 | 7422 | detect_info.found |= found; |
ec6d2bb8 | 7423 | } |
ec6d2bb8 | 7424 | } |
24a73b0a KH |
7425 | else if (base_category == coding_category_utf_16_auto) |
7426 | { | |
7427 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
7428 | { | |
24a73b0a KH |
7429 | struct coding_system *this; |
7430 | ||
7431 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
7432 | this = coding_categories + coding_category_utf_16_le; | |
7433 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
7434 | this = coding_categories + coding_category_utf_16_be; | |
7435 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
7436 | this = coding_categories + coding_category_utf_16_be_nosig; | |
7437 | else | |
7438 | this = coding_categories + coding_category_utf_16_le_nosig; | |
7439 | val = Fcons (make_number (this->id), Qnil); | |
7440 | } | |
7441 | } | |
df7492f9 KH |
7442 | else |
7443 | { | |
ff0dacd7 | 7444 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
89528eb3 | 7445 | val = Fcons (make_number (coding.id), Qnil); |
4ed46869 | 7446 | } |
df7492f9 | 7447 | |
89528eb3 | 7448 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 7449 | { |
89528eb3 | 7450 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol; |
df7492f9 KH |
7451 | Lisp_Object tail; |
7452 | ||
89528eb3 KH |
7453 | if (VECTORP (eol_type)) |
7454 | { | |
ff0dacd7 | 7455 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
89528eb3 KH |
7456 | normal_eol = detect_eol (coding.source, src_bytes, |
7457 | coding_category_raw_text); | |
ff0dacd7 KH |
7458 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
7459 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
7460 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
7461 | coding_category_utf_16_be); | |
ff0dacd7 KH |
7462 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
7463 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
7464 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
7465 | coding_category_utf_16_le); | |
7466 | } | |
7467 | else | |
7468 | { | |
7469 | if (EQ (eol_type, Qunix)) | |
7470 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
7471 | else if (EQ (eol_type, Qdos)) | |
7472 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
7473 | else | |
7474 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
7475 | } | |
7476 | ||
df7492f9 KH |
7477 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
7478 | { | |
89528eb3 | 7479 | enum coding_category category; |
df7492f9 | 7480 | int this_eol; |
89528eb3 KH |
7481 | |
7482 | id = XINT (XCAR (tail)); | |
7483 | attrs = CODING_ID_ATTRS (id); | |
7484 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
7485 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
7486 | if (VECTORP (eol_type)) |
7487 | { | |
89528eb3 KH |
7488 | if (category == coding_category_utf_16_be |
7489 | || category == coding_category_utf_16_be_nosig) | |
7490 | this_eol = utf_16_be_eol; | |
7491 | else if (category == coding_category_utf_16_le | |
7492 | || category == coding_category_utf_16_le_nosig) | |
7493 | this_eol = utf_16_le_eol; | |
df7492f9 | 7494 | else |
89528eb3 KH |
7495 | this_eol = normal_eol; |
7496 | ||
df7492f9 KH |
7497 | if (this_eol == EOL_SEEN_LF) |
7498 | XSETCAR (tail, AREF (eol_type, 0)); | |
7499 | else if (this_eol == EOL_SEEN_CRLF) | |
7500 | XSETCAR (tail, AREF (eol_type, 1)); | |
7501 | else if (this_eol == EOL_SEEN_CR) | |
7502 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
7503 | else |
7504 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 7505 | } |
89528eb3 KH |
7506 | else |
7507 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
7508 | } |
7509 | } | |
ec6d2bb8 | 7510 | |
03699b14 | 7511 | return (highest ? XCAR (val) : val); |
ec6d2bb8 KH |
7512 | } |
7513 | ||
ec6d2bb8 | 7514 | |
d46c5b12 KH |
7515 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
7516 | 2, 3, 0, | |
48b0f3ae PJ |
7517 | doc: /* Detect coding system of the text in the region between START and END. |
7518 | Return a list of possible coding systems ordered by priority. | |
ec6d2bb8 | 7519 | |
12e0131a KH |
7520 | If only ASCII characters are found (except for such ISO-2022 control |
7521 | characters ISO-2022 as ESC), it returns a list of single element | |
48b0f3ae PJ |
7522 | `undecided' or its subsidiary coding system according to a detected |
7523 | end-of-line format. | |
ec6d2bb8 | 7524 | |
48b0f3ae PJ |
7525 | If optional argument HIGHEST is non-nil, return the coding system of |
7526 | highest priority. */) | |
7527 | (start, end, highest) | |
d46c5b12 KH |
7528 | Lisp_Object start, end, highest; |
7529 | { | |
7530 | int from, to; | |
7531 | int from_byte, to_byte; | |
ec6d2bb8 | 7532 | |
b7826503 PJ |
7533 | CHECK_NUMBER_COERCE_MARKER (start); |
7534 | CHECK_NUMBER_COERCE_MARKER (end); | |
ec6d2bb8 | 7535 | |
d46c5b12 KH |
7536 | validate_region (&start, &end); |
7537 | from = XINT (start), to = XINT (end); | |
7538 | from_byte = CHAR_TO_BYTE (from); | |
7539 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 7540 | |
d46c5b12 KH |
7541 | if (from < GPT && to >= GPT) |
7542 | move_gap_both (to, to_byte); | |
c210f766 | 7543 | |
d46c5b12 | 7544 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 7545 | to - from, to_byte - from_byte, |
0a28aafb KH |
7546 | !NILP (highest), |
7547 | !NILP (current_buffer | |
df7492f9 KH |
7548 | ->enable_multibyte_characters), |
7549 | Qnil); | |
ec6d2bb8 KH |
7550 | } |
7551 | ||
d46c5b12 KH |
7552 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
7553 | 1, 2, 0, | |
48b0f3ae PJ |
7554 | doc: /* Detect coding system of the text in STRING. |
7555 | Return a list of possible coding systems ordered by priority. | |
fb88bf2d | 7556 | |
12e0131a KH |
7557 | If only ASCII characters are found (except for such ISO-2022 control |
7558 | characters ISO-2022 as ESC), it returns a list of single element | |
48b0f3ae PJ |
7559 | `undecided' or its subsidiary coding system according to a detected |
7560 | end-of-line format. | |
d46c5b12 | 7561 | |
48b0f3ae PJ |
7562 | If optional argument HIGHEST is non-nil, return the coding system of |
7563 | highest priority. */) | |
7564 | (string, highest) | |
d46c5b12 KH |
7565 | Lisp_Object string, highest; |
7566 | { | |
b7826503 | 7567 | CHECK_STRING (string); |
b73bfc1c | 7568 | |
24a73b0a KH |
7569 | return detect_coding_system (SDATA (string), |
7570 | SCHARS (string), SBYTES (string), | |
8f924df7 | 7571 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 7572 | Qnil); |
4ed46869 | 7573 | } |
4ed46869 | 7574 | |
b73bfc1c | 7575 | |
df7492f9 KH |
7576 | static INLINE int |
7577 | char_encodable_p (c, attrs) | |
7578 | int c; | |
7579 | Lisp_Object attrs; | |
05e6f5dc | 7580 | { |
df7492f9 | 7581 | Lisp_Object tail; |
df7492f9 | 7582 | struct charset *charset; |
7d64c6ad | 7583 | Lisp_Object translation_table; |
d46c5b12 | 7584 | |
7d64c6ad | 7585 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 7586 | if (! NILP (translation_table)) |
7d64c6ad | 7587 | c = translate_char (translation_table, c); |
df7492f9 KH |
7588 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
7589 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 7590 | { |
df7492f9 KH |
7591 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
7592 | if (CHAR_CHARSET_P (c, charset)) | |
7593 | break; | |
e133c8fa | 7594 | } |
df7492f9 | 7595 | return (! NILP (tail)); |
05e6f5dc | 7596 | } |
83fa074f | 7597 | |
fb88bf2d | 7598 | |
df7492f9 KH |
7599 | /* Return a list of coding systems that safely encode the text between |
7600 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
7601 | systems not to check. The returned list doesn't contain any such | |
48468dac | 7602 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 7603 | unibyte, return t. */ |
e077cc80 | 7604 | |
df7492f9 KH |
7605 | DEFUN ("find-coding-systems-region-internal", |
7606 | Ffind_coding_systems_region_internal, | |
7607 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
7608 | doc: /* Internal use only. */) | |
7609 | (start, end, exclude) | |
7610 | Lisp_Object start, end, exclude; | |
7611 | { | |
7612 | Lisp_Object coding_attrs_list, safe_codings; | |
7613 | EMACS_INT start_byte, end_byte; | |
7c78e542 | 7614 | const unsigned char *p, *pbeg, *pend; |
df7492f9 KH |
7615 | int c; |
7616 | Lisp_Object tail, elt; | |
d46c5b12 | 7617 | |
df7492f9 KH |
7618 | if (STRINGP (start)) |
7619 | { | |
7620 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 7621 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
7622 | return Qt; |
7623 | start_byte = 0; | |
8f924df7 | 7624 | end_byte = SBYTES (start); |
df7492f9 KH |
7625 | } |
7626 | else | |
d46c5b12 | 7627 | { |
df7492f9 KH |
7628 | CHECK_NUMBER_COERCE_MARKER (start); |
7629 | CHECK_NUMBER_COERCE_MARKER (end); | |
7630 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
7631 | args_out_of_range (start, end); | |
7632 | if (NILP (current_buffer->enable_multibyte_characters)) | |
7633 | return Qt; | |
7634 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
7635 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
7636 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
7637 | return Qt; | |
d46c5b12 | 7638 | |
e1c23804 | 7639 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 7640 | { |
e1c23804 DL |
7641 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
7642 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 7643 | else |
e1c23804 | 7644 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
7645 | } |
7646 | } | |
7647 | ||
df7492f9 KH |
7648 | coding_attrs_list = Qnil; |
7649 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
7650 | if (NILP (exclude) | |
7651 | || NILP (Fmemq (XCAR (tail), exclude))) | |
7652 | { | |
7653 | Lisp_Object attrs; | |
d46c5b12 | 7654 | |
df7492f9 KH |
7655 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
7656 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)) | |
7657 | && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
7d64c6ad KH |
7658 | { |
7659 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 7660 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
7661 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
7662 | } | |
df7492f9 | 7663 | } |
d46c5b12 | 7664 | |
df7492f9 | 7665 | if (STRINGP (start)) |
8f924df7 | 7666 | p = pbeg = SDATA (start); |
df7492f9 KH |
7667 | else |
7668 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
7669 | pend = p + (end_byte - start_byte); | |
b843d1ae | 7670 | |
df7492f9 KH |
7671 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
7672 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 7673 | |
05e6f5dc | 7674 | while (p < pend) |
72d1a715 | 7675 | { |
df7492f9 KH |
7676 | if (ASCII_BYTE_P (*p)) |
7677 | p++; | |
72d1a715 RS |
7678 | else |
7679 | { | |
df7492f9 | 7680 | c = STRING_CHAR_ADVANCE (p); |
12410ef1 | 7681 | |
df7492f9 KH |
7682 | charset_map_loaded = 0; |
7683 | for (tail = coding_attrs_list; CONSP (tail);) | |
7684 | { | |
7685 | elt = XCAR (tail); | |
7686 | if (NILP (elt)) | |
7687 | tail = XCDR (tail); | |
7688 | else if (char_encodable_p (c, elt)) | |
7689 | tail = XCDR (tail); | |
7690 | else if (CONSP (XCDR (tail))) | |
7691 | { | |
7692 | XSETCAR (tail, XCAR (XCDR (tail))); | |
7693 | XSETCDR (tail, XCDR (XCDR (tail))); | |
7694 | } | |
7695 | else | |
7696 | { | |
7697 | XSETCAR (tail, Qnil); | |
7698 | tail = XCDR (tail); | |
7699 | } | |
7700 | } | |
7701 | if (charset_map_loaded) | |
7702 | { | |
7703 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
05e6f5dc | 7704 | |
df7492f9 | 7705 | if (STRINGP (start)) |
8f924df7 | 7706 | pbeg = SDATA (start); |
df7492f9 KH |
7707 | else |
7708 | pbeg = BYTE_POS_ADDR (start_byte); | |
7709 | p = pbeg + p_offset; | |
7710 | pend = pbeg + pend_offset; | |
7711 | } | |
7712 | } | |
ec6d2bb8 | 7713 | } |
fb88bf2d | 7714 | |
988b3759 | 7715 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
7716 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
7717 | if (! NILP (XCAR (tail))) | |
7718 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 7719 | |
05e6f5dc KH |
7720 | return safe_codings; |
7721 | } | |
4956c225 | 7722 | |
d46c5b12 | 7723 | |
8f924df7 KH |
7724 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
7725 | Sunencodable_char_position, 3, 5, 0, | |
7726 | doc: /* | |
7727 | Return position of first un-encodable character in a region. | |
7728 | START and END specfiy the region and CODING-SYSTEM specifies the | |
7729 | encoding to check. Return nil if CODING-SYSTEM does encode the region. | |
d46c5b12 | 7730 | |
8f924df7 KH |
7731 | If optional 4th argument COUNT is non-nil, it specifies at most how |
7732 | many un-encodable characters to search. In this case, the value is a | |
7733 | list of positions. | |
d46c5b12 | 7734 | |
8f924df7 KH |
7735 | If optional 5th argument STRING is non-nil, it is a string to search |
7736 | for un-encodable characters. In that case, START and END are indexes | |
7737 | to the string. */) | |
7738 | (start, end, coding_system, count, string) | |
7739 | Lisp_Object start, end, coding_system, count, string; | |
7740 | { | |
7741 | int n; | |
7742 | struct coding_system coding; | |
7d64c6ad | 7743 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 KH |
7744 | Lisp_Object positions; |
7745 | int from, to; | |
7746 | const unsigned char *p, *stop, *pend; | |
7747 | int ascii_compatible; | |
fb88bf2d | 7748 | |
8f924df7 KH |
7749 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
7750 | attrs = CODING_ID_ATTRS (coding.id); | |
7751 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
7752 | return Qnil; | |
7753 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
7754 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 7755 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 7756 | |
8f924df7 KH |
7757 | if (NILP (string)) |
7758 | { | |
7759 | validate_region (&start, &end); | |
7760 | from = XINT (start); | |
7761 | to = XINT (end); | |
7762 | if (NILP (current_buffer->enable_multibyte_characters) | |
7763 | || (ascii_compatible | |
7764 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
7765 | return Qnil; | |
7766 | p = CHAR_POS_ADDR (from); | |
7767 | pend = CHAR_POS_ADDR (to); | |
7768 | if (from < GPT && to >= GPT) | |
7769 | stop = GPT_ADDR; | |
7770 | else | |
7771 | stop = pend; | |
7772 | } | |
7773 | else | |
7774 | { | |
7775 | CHECK_STRING (string); | |
7776 | CHECK_NATNUM (start); | |
7777 | CHECK_NATNUM (end); | |
7778 | from = XINT (start); | |
7779 | to = XINT (end); | |
7780 | if (from > to | |
7781 | || to > SCHARS (string)) | |
7782 | args_out_of_range_3 (string, start, end); | |
7783 | if (! STRING_MULTIBYTE (string)) | |
7784 | return Qnil; | |
7785 | p = SDATA (string) + string_char_to_byte (string, from); | |
7786 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
7787 | if (ascii_compatible && (to - from) == (pend - p)) | |
7788 | return Qnil; | |
7789 | } | |
f2558efd | 7790 | |
8f924df7 KH |
7791 | if (NILP (count)) |
7792 | n = 1; | |
7793 | else | |
b73bfc1c | 7794 | { |
8f924df7 KH |
7795 | CHECK_NATNUM (count); |
7796 | n = XINT (count); | |
b73bfc1c KH |
7797 | } |
7798 | ||
8f924df7 KH |
7799 | positions = Qnil; |
7800 | while (1) | |
d46c5b12 | 7801 | { |
8f924df7 | 7802 | int c; |
ec6d2bb8 | 7803 | |
8f924df7 KH |
7804 | if (ascii_compatible) |
7805 | while (p < stop && ASCII_BYTE_P (*p)) | |
7806 | p++, from++; | |
7807 | if (p >= stop) | |
0e79d667 | 7808 | { |
8f924df7 KH |
7809 | if (p >= pend) |
7810 | break; | |
7811 | stop = pend; | |
7812 | p = GAP_END_ADDR; | |
0e79d667 | 7813 | } |
ec6d2bb8 | 7814 | |
8f924df7 KH |
7815 | c = STRING_CHAR_ADVANCE (p); |
7816 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
7817 | && ! char_charset (translate_char (translation_table, c), |
7818 | charset_list, NULL)) | |
ec6d2bb8 | 7819 | { |
8f924df7 KH |
7820 | positions = Fcons (make_number (from), positions); |
7821 | n--; | |
7822 | if (n == 0) | |
7823 | break; | |
ec6d2bb8 KH |
7824 | } |
7825 | ||
8f924df7 KH |
7826 | from++; |
7827 | } | |
d46c5b12 | 7828 | |
8f924df7 KH |
7829 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
7830 | } | |
d46c5b12 | 7831 | |
d46c5b12 | 7832 | |
df7492f9 KH |
7833 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
7834 | Scheck_coding_systems_region, 3, 3, 0, | |
7835 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 7836 | |
df7492f9 KH |
7837 | START and END are buffer positions specifying the region. |
7838 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 7839 | |
df7492f9 KH |
7840 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
7841 | CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the | |
7842 | whole region, POS0, POS1, ... are buffer positions where non-encodable | |
7843 | characters are found. | |
93dec019 | 7844 | |
df7492f9 KH |
7845 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
7846 | value is nil. | |
93dec019 | 7847 | |
df7492f9 KH |
7848 | START may be a string. In that case, check if the string is |
7849 | encodable, and the value contains indices to the string instead of | |
7850 | buffer positions. END is ignored. */) | |
7851 | (start, end, coding_system_list) | |
7852 | Lisp_Object start, end, coding_system_list; | |
05e6f5dc | 7853 | { |
df7492f9 KH |
7854 | Lisp_Object list; |
7855 | EMACS_INT start_byte, end_byte; | |
7856 | int pos; | |
7c78e542 | 7857 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 7858 | int c; |
7d64c6ad | 7859 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 7860 | |
05e6f5dc KH |
7861 | if (STRINGP (start)) |
7862 | { | |
df7492f9 | 7863 | if (!STRING_MULTIBYTE (start) |
8f924df7 | 7864 | && SCHARS (start) != SBYTES (start)) |
df7492f9 KH |
7865 | return Qnil; |
7866 | start_byte = 0; | |
8f924df7 | 7867 | end_byte = SBYTES (start); |
df7492f9 | 7868 | pos = 0; |
d46c5b12 | 7869 | } |
05e6f5dc | 7870 | else |
b73bfc1c | 7871 | { |
b7826503 PJ |
7872 | CHECK_NUMBER_COERCE_MARKER (start); |
7873 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
7874 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
7875 | args_out_of_range (start, end); | |
7876 | if (NILP (current_buffer->enable_multibyte_characters)) | |
df7492f9 KH |
7877 | return Qnil; |
7878 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
7879 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
7880 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
05e6f5dc | 7881 | return Qt; |
df7492f9 | 7882 | |
e1c23804 | 7883 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 7884 | { |
e1c23804 DL |
7885 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
7886 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 7887 | else |
e1c23804 | 7888 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 7889 | } |
e1c23804 | 7890 | pos = XINT (start); |
b73bfc1c | 7891 | } |
7553d0e1 | 7892 | |
df7492f9 KH |
7893 | list = Qnil; |
7894 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 7895 | { |
df7492f9 | 7896 | elt = XCAR (tail); |
7d64c6ad | 7897 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
7898 | ASET (attrs, coding_attr_trans_tbl, |
7899 | get_translation_table (attrs, 1, NULL)); | |
7d64c6ad | 7900 | list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list); |
12410ef1 KH |
7901 | } |
7902 | ||
df7492f9 | 7903 | if (STRINGP (start)) |
8f924df7 | 7904 | p = pbeg = SDATA (start); |
72d1a715 | 7905 | else |
df7492f9 KH |
7906 | p = pbeg = BYTE_POS_ADDR (start_byte); |
7907 | pend = p + (end_byte - start_byte); | |
4ed46869 | 7908 | |
df7492f9 KH |
7909 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
7910 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 7911 | |
df7492f9 | 7912 | while (p < pend) |
d46c5b12 | 7913 | { |
df7492f9 KH |
7914 | if (ASCII_BYTE_P (*p)) |
7915 | p++; | |
e133c8fa | 7916 | else |
05e6f5dc | 7917 | { |
df7492f9 KH |
7918 | c = STRING_CHAR_ADVANCE (p); |
7919 | ||
7920 | charset_map_loaded = 0; | |
7921 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
7922 | { | |
7923 | elt = XCDR (XCAR (tail)); | |
7924 | if (! char_encodable_p (c, XCAR (elt))) | |
7925 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
7926 | } | |
7927 | if (charset_map_loaded) | |
7928 | { | |
7929 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
7930 | ||
7931 | if (STRINGP (start)) | |
8f924df7 | 7932 | pbeg = SDATA (start); |
df7492f9 KH |
7933 | else |
7934 | pbeg = BYTE_POS_ADDR (start_byte); | |
7935 | p = pbeg + p_offset; | |
7936 | pend = pbeg + pend_offset; | |
7937 | } | |
05e6f5dc | 7938 | } |
df7492f9 | 7939 | pos++; |
d46c5b12 | 7940 | } |
4ed46869 | 7941 | |
df7492f9 KH |
7942 | tail = list; |
7943 | list = Qnil; | |
7944 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 7945 | { |
df7492f9 KH |
7946 | elt = XCAR (tail); |
7947 | if (CONSP (XCDR (XCDR (elt)))) | |
7948 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
7949 | list); | |
ec6d2bb8 | 7950 | } |
2b4f9037 | 7951 | |
df7492f9 | 7952 | return list; |
d46c5b12 KH |
7953 | } |
7954 | ||
3fd9494b | 7955 | |
b73bfc1c | 7956 | Lisp_Object |
df7492f9 KH |
7957 | code_convert_region (start, end, coding_system, dst_object, encodep, norecord) |
7958 | Lisp_Object start, end, coding_system, dst_object; | |
7959 | int encodep, norecord; | |
4ed46869 | 7960 | { |
3a73fa5d | 7961 | struct coding_system coding; |
df7492f9 KH |
7962 | EMACS_INT from, from_byte, to, to_byte; |
7963 | Lisp_Object src_object; | |
4ed46869 | 7964 | |
b7826503 PJ |
7965 | CHECK_NUMBER_COERCE_MARKER (start); |
7966 | CHECK_NUMBER_COERCE_MARKER (end); | |
df7492f9 KH |
7967 | if (NILP (coding_system)) |
7968 | coding_system = Qno_conversion; | |
7969 | else | |
7970 | CHECK_CODING_SYSTEM (coding_system); | |
7971 | src_object = Fcurrent_buffer (); | |
7972 | if (NILP (dst_object)) | |
7973 | dst_object = src_object; | |
7974 | else if (! EQ (dst_object, Qt)) | |
7975 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 7976 | |
d46c5b12 KH |
7977 | validate_region (&start, &end); |
7978 | from = XFASTINT (start); | |
df7492f9 | 7979 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 7980 | to = XFASTINT (end); |
df7492f9 | 7981 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 7982 | |
df7492f9 KH |
7983 | setup_coding_system (coding_system, &coding); |
7984 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 7985 | |
df7492f9 KH |
7986 | if (encodep) |
7987 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
7988 | dst_object); | |
7989 | else | |
7990 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
7991 | dst_object); | |
7992 | if (! norecord) | |
7993 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 7994 | |
df7492f9 KH |
7995 | return (BUFFERP (dst_object) |
7996 | ? make_number (coding.produced_char) | |
7997 | : coding.dst_object); | |
4031e2bf | 7998 | } |
78108bcd | 7999 | |
4ed46869 | 8000 | |
4031e2bf | 8001 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 8002 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 8003 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
8004 | When called from a program, takes four arguments: |
8005 | START, END, CODING-SYSTEM, and DESTINATION. | |
8006 | START and END are buffer positions. | |
8844fa83 | 8007 | |
df7492f9 KH |
8008 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
8009 | If nil, the region between START and END is replace by the decoded text. | |
8010 | If buffer, the decoded text is inserted in the buffer. | |
8011 | If t, the decoded text is returned. | |
8844fa83 | 8012 | |
48b0f3ae PJ |
8013 | This function sets `last-coding-system-used' to the precise coding system |
8014 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
8015 | not fully specified.) | |
8016 | It returns the length of the decoded text. */) | |
df7492f9 KH |
8017 | (start, end, coding_system, destination) |
8018 | Lisp_Object start, end, coding_system, destination; | |
4031e2bf | 8019 | { |
df7492f9 | 8020 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 8021 | } |
8844fa83 | 8022 | |
3a73fa5d | 8023 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
8024 | 3, 4, "r\nzCoding system: ", |
8025 | doc: /* Encode the current region by specified coding system. | |
48b0f3ae PJ |
8026 | When called from a program, takes three arguments: |
8027 | START, END, and CODING-SYSTEM. START and END are buffer positions. | |
d46c5b12 | 8028 | |
df7492f9 KH |
8029 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
8030 | If nil, the region between START and END is replace by the encoded text. | |
8031 | If buffer, the encoded text is inserted in the buffer. | |
8032 | If t, the encoded text is returned. | |
2391eaa4 | 8033 | |
48b0f3ae PJ |
8034 | This function sets `last-coding-system-used' to the precise coding system |
8035 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
8036 | not fully specified.) | |
8037 | It returns the length of the encoded text. */) | |
df7492f9 KH |
8038 | (start, end, coding_system, destination) |
8039 | Lisp_Object start, end, coding_system, destination; | |
3a73fa5d | 8040 | { |
df7492f9 | 8041 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
8042 | } |
8043 | ||
8044 | Lisp_Object | |
df7492f9 KH |
8045 | code_convert_string (string, coding_system, dst_object, |
8046 | encodep, nocopy, norecord) | |
8047 | Lisp_Object string, coding_system, dst_object; | |
8048 | int encodep, nocopy, norecord; | |
b73bfc1c | 8049 | { |
4031e2bf | 8050 | struct coding_system coding; |
df7492f9 | 8051 | EMACS_INT chars, bytes; |
ec6d2bb8 | 8052 | |
b7826503 | 8053 | CHECK_STRING (string); |
d46c5b12 | 8054 | if (NILP (coding_system)) |
4956c225 | 8055 | { |
df7492f9 KH |
8056 | if (! norecord) |
8057 | Vlast_coding_system_used = Qno_conversion; | |
8058 | if (NILP (dst_object)) | |
8059 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 8060 | } |
b73bfc1c | 8061 | |
df7492f9 KH |
8062 | if (NILP (coding_system)) |
8063 | coding_system = Qno_conversion; | |
8064 | else | |
8065 | CHECK_CODING_SYSTEM (coding_system); | |
8066 | if (NILP (dst_object)) | |
8067 | dst_object = Qt; | |
8068 | else if (! EQ (dst_object, Qt)) | |
8069 | CHECK_BUFFER (dst_object); | |
73be902c | 8070 | |
df7492f9 | 8071 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 8072 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
8073 | chars = SCHARS (string); |
8074 | bytes = SBYTES (string); | |
df7492f9 KH |
8075 | if (encodep) |
8076 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8077 | else | |
8078 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8079 | if (! norecord) | |
8080 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 8081 | |
df7492f9 KH |
8082 | return (BUFFERP (dst_object) |
8083 | ? make_number (coding.produced_char) | |
8084 | : coding.dst_object); | |
4ed46869 | 8085 | } |
73be902c | 8086 | |
b73bfc1c | 8087 | |
ecec61c1 | 8088 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 8089 | Do not set Vlast_coding_system_used. |
4ed46869 | 8090 | |
ec6d2bb8 KH |
8091 | This function is called only from macros DECODE_FILE and |
8092 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 8093 | |
ecec61c1 KH |
8094 | Lisp_Object |
8095 | code_convert_string_norecord (string, coding_system, encodep) | |
8096 | Lisp_Object string, coding_system; | |
8097 | int encodep; | |
4ed46869 | 8098 | { |
0be8721c | 8099 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
8100 | } |
8101 | ||
4ed46869 | 8102 | |
df7492f9 KH |
8103 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
8104 | 2, 4, 0, | |
8105 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
8106 | ||
8107 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
8108 | if the decoding operation is trivial. | |
ecec61c1 | 8109 | |
df7492f9 | 8110 | Optional fourth arg BUFFER non-nil meant that the decoded text is |
a3f6ee6d | 8111 | inserted in BUFFER instead of returned as a string. In this case, |
df7492f9 | 8112 | the return value is BUFFER. |
ecec61c1 | 8113 | |
df7492f9 KH |
8114 | This function sets `last-coding-system-used' to the precise coding system |
8115 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
8116 | not fully specified. */) | |
8117 | (string, coding_system, nocopy, buffer) | |
8118 | Lisp_Object string, coding_system, nocopy, buffer; | |
4ed46869 | 8119 | { |
df7492f9 KH |
8120 | return code_convert_string (string, coding_system, buffer, |
8121 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
8122 | } |
8123 | ||
df7492f9 KH |
8124 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
8125 | 2, 4, 0, | |
8126 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
8127 | ||
8128 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
8129 | itself if the encoding operation is trivial. | |
8130 | ||
8131 | Optional fourth arg BUFFER non-nil meant that the encoded text is | |
a3f6ee6d | 8132 | inserted in BUFFER instead of returned as a string. In this case, |
df7492f9 KH |
8133 | the return value is BUFFER. |
8134 | ||
8135 | This function sets `last-coding-system-used' to the precise coding system | |
8136 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
8137 | not fully specified.) */) | |
8138 | (string, coding_system, nocopy, buffer) | |
8139 | Lisp_Object string, coding_system, nocopy, buffer; | |
4ed46869 | 8140 | { |
df7492f9 | 8141 | return code_convert_string (string, coding_system, buffer, |
c197f191 | 8142 | 1, ! NILP (nocopy), 1); |
4ed46869 | 8143 | } |
df7492f9 | 8144 | |
3a73fa5d | 8145 | \f |
4ed46869 | 8146 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
8147 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
8148 | Return the corresponding character. */) | |
8149 | (code) | |
4ed46869 | 8150 | Lisp_Object code; |
4ed46869 | 8151 | { |
df7492f9 KH |
8152 | Lisp_Object spec, attrs, val; |
8153 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
8154 | int c; | |
4ed46869 | 8155 | |
df7492f9 KH |
8156 | CHECK_NATNUM (code); |
8157 | c = XFASTINT (code); | |
8158 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
8159 | attrs = AREF (spec, 0); | |
4ed46869 | 8160 | |
df7492f9 KH |
8161 | if (ASCII_BYTE_P (c) |
8162 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8163 | return code; | |
4ed46869 | 8164 | |
df7492f9 KH |
8165 | val = CODING_ATTR_CHARSET_LIST (attrs); |
8166 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
8167 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
8168 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 8169 | |
df7492f9 KH |
8170 | if (c <= 0x7F) |
8171 | charset = charset_roman; | |
8172 | else if (c >= 0xA0 && c < 0xDF) | |
55ab7be3 | 8173 | { |
df7492f9 KH |
8174 | charset = charset_kana; |
8175 | c -= 0x80; | |
4ed46869 | 8176 | } |
55ab7be3 | 8177 | else |
4ed46869 | 8178 | { |
004068e4 | 8179 | int s1 = c >> 8, s2 = c & 0xFF; |
df7492f9 KH |
8180 | |
8181 | if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF | |
8182 | || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC) | |
8183 | error ("Invalid code: %d", code); | |
8184 | SJIS_TO_JIS (c); | |
8185 | charset = charset_kanji; | |
4ed46869 | 8186 | } |
df7492f9 KH |
8187 | c = DECODE_CHAR (charset, c); |
8188 | if (c < 0) | |
8189 | error ("Invalid code: %d", code); | |
8190 | return make_number (c); | |
93dec019 | 8191 | } |
4ed46869 | 8192 | |
48b0f3ae | 8193 | |
4ed46869 | 8194 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
8acf0c0e | 8195 | doc: /* Encode a Japanese character CH to shift_jis encoding. |
48b0f3ae PJ |
8196 | Return the corresponding code in SJIS. */) |
8197 | (ch) | |
df7492f9 | 8198 | Lisp_Object ch; |
4ed46869 | 8199 | { |
df7492f9 KH |
8200 | Lisp_Object spec, attrs, charset_list; |
8201 | int c; | |
8202 | struct charset *charset; | |
8203 | unsigned code; | |
48b0f3ae | 8204 | |
df7492f9 KH |
8205 | CHECK_CHARACTER (ch); |
8206 | c = XFASTINT (ch); | |
8207 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
8208 | attrs = AREF (spec, 0); | |
8209 | ||
8210 | if (ASCII_CHAR_P (c) | |
8211 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8212 | return ch; | |
8213 | ||
8214 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
8215 | charset = char_charset (c, charset_list, &code); | |
8216 | if (code == CHARSET_INVALID_CODE (charset)) | |
8217 | error ("Can't encode by shift_jis encoding: %d", c); | |
8218 | JIS_TO_SJIS (code); | |
8219 | ||
8220 | return make_number (code); | |
4ed46869 KH |
8221 | } |
8222 | ||
8223 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
8224 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
8225 | Return the corresponding character. */) | |
8226 | (code) | |
4ed46869 | 8227 | Lisp_Object code; |
d46c5b12 | 8228 | { |
df7492f9 KH |
8229 | Lisp_Object spec, attrs, val; |
8230 | struct charset *charset_roman, *charset_big5, *charset; | |
8231 | int c; | |
6289dd10 | 8232 | |
df7492f9 KH |
8233 | CHECK_NATNUM (code); |
8234 | c = XFASTINT (code); | |
8235 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
8236 | attrs = AREF (spec, 0); | |
4ed46869 | 8237 | |
df7492f9 KH |
8238 | if (ASCII_BYTE_P (c) |
8239 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8240 | return code; | |
6289dd10 | 8241 | |
df7492f9 KH |
8242 | val = CODING_ATTR_CHARSET_LIST (attrs); |
8243 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
8244 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 8245 | |
df7492f9 KH |
8246 | if (c <= 0x7F) |
8247 | charset = charset_roman; | |
c28a9453 KH |
8248 | else |
8249 | { | |
df7492f9 KH |
8250 | int b1 = c >> 8, b2 = c & 0x7F; |
8251 | if (b1 < 0xA1 || b1 > 0xFE | |
8252 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
8253 | error ("Invalid code: %d", code); | |
8254 | charset = charset_big5; | |
c28a9453 | 8255 | } |
df7492f9 KH |
8256 | c = DECODE_CHAR (charset, (unsigned )c); |
8257 | if (c < 0) | |
8258 | error ("Invalid code: %d", code); | |
8259 | return make_number (c); | |
d46c5b12 | 8260 | } |
6289dd10 | 8261 | |
4ed46869 | 8262 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
8acf0c0e | 8263 | doc: /* Encode the Big5 character CH to BIG5 coding system. |
48b0f3ae PJ |
8264 | Return the corresponding character code in Big5. */) |
8265 | (ch) | |
4ed46869 KH |
8266 | Lisp_Object ch; |
8267 | { | |
df7492f9 KH |
8268 | Lisp_Object spec, attrs, charset_list; |
8269 | struct charset *charset; | |
8270 | int c; | |
8271 | unsigned code; | |
8272 | ||
8273 | CHECK_CHARACTER (ch); | |
8274 | c = XFASTINT (ch); | |
8275 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
8276 | attrs = AREF (spec, 0); | |
8277 | if (ASCII_CHAR_P (c) | |
8278 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
8279 | return ch; | |
8280 | ||
8281 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
8282 | charset = char_charset (c, charset_list, &code); | |
8283 | if (code == CHARSET_INVALID_CODE (charset)) | |
8284 | error ("Can't encode by Big5 encoding: %d", c); | |
8285 | ||
8286 | return make_number (code); | |
4ed46869 | 8287 | } |
48b0f3ae | 8288 | |
3a73fa5d | 8289 | \f |
1ba9e4ab KH |
8290 | DEFUN ("set-terminal-coding-system-internal", |
8291 | Fset_terminal_coding_system_internal, | |
48b0f3ae PJ |
8292 | Sset_terminal_coding_system_internal, 1, 1, 0, |
8293 | doc: /* Internal use only. */) | |
8294 | (coding_system) | |
b74e4686 | 8295 | Lisp_Object coding_system; |
4ed46869 | 8296 | { |
b7826503 | 8297 | CHECK_SYMBOL (coding_system); |
df7492f9 KH |
8298 | setup_coding_system (Fcheck_coding_system (coding_system), |
8299 | &terminal_coding); | |
48b0f3ae | 8300 | |
70c22245 | 8301 | /* We had better not send unsafe characters to terminal. */ |
df7492f9 KH |
8302 | terminal_coding.mode |= CODING_MODE_SAFE_ENCODING; |
8303 | /* Characer composition should be disabled. */ | |
8304 | terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
b73bfc1c KH |
8305 | terminal_coding.src_multibyte = 1; |
8306 | terminal_coding.dst_multibyte = 0; | |
4ed46869 KH |
8307 | return Qnil; |
8308 | } | |
8309 | ||
c4825358 KH |
8310 | DEFUN ("set-safe-terminal-coding-system-internal", |
8311 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 8312 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 8313 | doc: /* Internal use only. */) |
48b0f3ae | 8314 | (coding_system) |
b74e4686 | 8315 | Lisp_Object coding_system; |
d46c5b12 | 8316 | { |
b7826503 | 8317 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
8318 | setup_coding_system (Fcheck_coding_system (coding_system), |
8319 | &safe_terminal_coding); | |
df7492f9 KH |
8320 | /* Characer composition should be disabled. */ |
8321 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
b73bfc1c KH |
8322 | safe_terminal_coding.src_multibyte = 1; |
8323 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
8324 | return Qnil; |
8325 | } | |
4ed46869 | 8326 | |
4ed46869 KH |
8327 | DEFUN ("terminal-coding-system", |
8328 | Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0, | |
48b0f3ae PJ |
8329 | doc: /* Return coding system specified for terminal output. */) |
8330 | () | |
4ed46869 | 8331 | { |
ae6f73fa KH |
8332 | Lisp_Object coding_system; |
8333 | ||
8334 | coding_system = CODING_ID_NAME (terminal_coding.id); | |
8335 | /* For backward compatibility, return nil if it is `undecided'. */ | |
f75c90a9 | 8336 | return (! EQ (coding_system, Qundecided) ? coding_system : Qnil); |
4ed46869 KH |
8337 | } |
8338 | ||
1ba9e4ab KH |
8339 | DEFUN ("set-keyboard-coding-system-internal", |
8340 | Fset_keyboard_coding_system_internal, | |
48b0f3ae PJ |
8341 | Sset_keyboard_coding_system_internal, 1, 1, 0, |
8342 | doc: /* Internal use only. */) | |
8343 | (coding_system) | |
4ed46869 KH |
8344 | Lisp_Object coding_system; |
8345 | { | |
b7826503 | 8346 | CHECK_SYMBOL (coding_system); |
df7492f9 KH |
8347 | setup_coding_system (Fcheck_coding_system (coding_system), |
8348 | &keyboard_coding); | |
8349 | /* Characer composition should be disabled. */ | |
8350 | keyboard_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
8351 | return Qnil; |
8352 | } | |
8353 | ||
8354 | DEFUN ("keyboard-coding-system", | |
8355 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0, | |
48b0f3ae PJ |
8356 | doc: /* Return coding system specified for decoding keyboard input. */) |
8357 | () | |
4ed46869 | 8358 | { |
df7492f9 | 8359 | return CODING_ID_NAME (keyboard_coding.id); |
4ed46869 KH |
8360 | } |
8361 | ||
4ed46869 | 8362 | \f |
a5d301df KH |
8363 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
8364 | Sfind_operation_coding_system, 1, MANY, 0, | |
48b0f3ae PJ |
8365 | doc: /* Choose a coding system for an operation based on the target name. |
8366 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
8367 | DECODING-SYSTEM is the coding system to use for decoding | |
8368 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
8369 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 8370 | |
48b0f3ae PJ |
8371 | The first argument OPERATION specifies an I/O primitive: |
8372 | For file I/O, `insert-file-contents' or `write-region'. | |
8373 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
8374 | For network I/O, `open-network-stream'. | |
05e6f5dc | 8375 | |
48b0f3ae PJ |
8376 | The remaining arguments should be the same arguments that were passed |
8377 | to the primitive. Depending on which primitive, one of those arguments | |
8378 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
8379 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 8380 | |
48b0f3ae | 8381 | TARGET has a meaning which depends on OPERATION: |
b883cdb2 | 8382 | For file I/O, TARGET is a file name (except for the special case below). |
48b0f3ae PJ |
8383 | For process I/O, TARGET is a process name. |
8384 | For network I/O, TARGET is a service name or a port number | |
05e6f5dc | 8385 | |
48b0f3ae PJ |
8386 | This function looks up what specified for TARGET in, |
8387 | `file-coding-system-alist', `process-coding-system-alist', | |
8388 | or `network-coding-system-alist' depending on OPERATION. | |
8389 | They may specify a coding system, a cons of coding systems, | |
8390 | or a function symbol to call. | |
8391 | In the last case, we call the function with one argument, | |
8392 | which is a list of all the arguments given to this function. | |
8393 | ||
b883cdb2 MB |
8394 | If OPERATION is `insert-file-contents', the argument corresponding to |
8395 | TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a | |
8396 | file name to look up, and BUFFER is a buffer that contains the file's | |
8397 | contents (not yet decoded). If `file-coding-system-alist' specifies a | |
8398 | function to call for FILENAME, that function should examine the | |
8399 | contents of BUFFER instead of reading the file. | |
8400 | ||
48b0f3ae PJ |
8401 | usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */) |
8402 | (nargs, args) | |
4ed46869 KH |
8403 | int nargs; |
8404 | Lisp_Object *args; | |
6b89e3aa | 8405 | { |
4ed46869 KH |
8406 | Lisp_Object operation, target_idx, target, val; |
8407 | register Lisp_Object chain; | |
177c0ea7 | 8408 | |
4ed46869 KH |
8409 | if (nargs < 2) |
8410 | error ("Too few arguments"); | |
8411 | operation = args[0]; | |
8412 | if (!SYMBOLP (operation) | |
8413 | || !INTEGERP (target_idx = Fget (operation, Qtarget_idx))) | |
df7492f9 | 8414 | error ("Invalid first arguement"); |
4ed46869 KH |
8415 | if (nargs < 1 + XINT (target_idx)) |
8416 | error ("Too few arguments for operation: %s", | |
8f924df7 | 8417 | SDATA (SYMBOL_NAME (operation))); |
4ed46869 KH |
8418 | target = args[XINT (target_idx) + 1]; |
8419 | if (!(STRINGP (target) | |
091a0ff0 KH |
8420 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
8421 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | |
4ed46869 | 8422 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
df7492f9 | 8423 | error ("Invalid %dth argument", XINT (target_idx) + 1); |
091a0ff0 KH |
8424 | if (CONSP (target)) |
8425 | target = XCAR (target); | |
4ed46869 | 8426 | |
2e34157c RS |
8427 | chain = ((EQ (operation, Qinsert_file_contents) |
8428 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 8429 | ? Vfile_coding_system_alist |
2e34157c | 8430 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
8431 | ? Vnetwork_coding_system_alist |
8432 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
8433 | if (NILP (chain)) |
8434 | return Qnil; | |
8435 | ||
03699b14 | 8436 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 8437 | { |
f44d27ce | 8438 | Lisp_Object elt; |
6b89e3aa | 8439 | |
df7492f9 | 8440 | elt = XCAR (chain); |
4ed46869 KH |
8441 | if (CONSP (elt) |
8442 | && ((STRINGP (target) | |
03699b14 KR |
8443 | && STRINGP (XCAR (elt)) |
8444 | && fast_string_match (XCAR (elt), target) >= 0) | |
8445 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 8446 | { |
03699b14 | 8447 | val = XCDR (elt); |
b19fd4c5 KH |
8448 | /* Here, if VAL is both a valid coding system and a valid |
8449 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
8450 | if (CONSP (val)) |
8451 | return val; | |
8452 | if (! SYMBOLP (val)) | |
8453 | return Qnil; | |
8454 | if (! NILP (Fcoding_system_p (val))) | |
8455 | return Fcons (val, val); | |
b19fd4c5 | 8456 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 8457 | { |
e2b97060 MB |
8458 | /* We use call1 rather than safe_call1 |
8459 | so as to get bug reports about functions called here | |
8460 | which don't handle the current interface. */ | |
8461 | val = call1 (val, Flist (nargs, args)); | |
b19fd4c5 KH |
8462 | if (CONSP (val)) |
8463 | return val; | |
8464 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
8465 | return Fcons (val, val); | |
6b89e3aa | 8466 | } |
02ba4723 | 8467 | return Qnil; |
6b89e3aa KH |
8468 | } |
8469 | } | |
4ed46869 | 8470 | return Qnil; |
6b89e3aa KH |
8471 | } |
8472 | ||
df7492f9 | 8473 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 8474 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 8475 | doc: /* Assign higher priority to the coding systems given as arguments. |
ff563fce | 8476 | If multiple coding systems belongs to the same category, |
a3181084 DL |
8477 | all but the first one are ignored. |
8478 | ||
8479 | usage: (set-coding-system-priority ...) */) | |
df7492f9 KH |
8480 | (nargs, args) |
8481 | int nargs; | |
8482 | Lisp_Object *args; | |
8483 | { | |
8484 | int i, j; | |
8485 | int changed[coding_category_max]; | |
8486 | enum coding_category priorities[coding_category_max]; | |
8487 | ||
8488 | bzero (changed, sizeof changed); | |
6b89e3aa | 8489 | |
df7492f9 | 8490 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 8491 | { |
df7492f9 KH |
8492 | enum coding_category category; |
8493 | Lisp_Object spec, attrs; | |
6b89e3aa | 8494 | |
df7492f9 KH |
8495 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
8496 | attrs = AREF (spec, 0); | |
8497 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8498 | if (changed[category]) | |
8499 | /* Ignore this coding system because a coding system of the | |
8500 | same category already had a higher priority. */ | |
8501 | continue; | |
8502 | changed[category] = 1; | |
8503 | priorities[j++] = category; | |
8504 | if (coding_categories[category].id >= 0 | |
8505 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
8506 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 8507 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 8508 | } |
6b89e3aa | 8509 | |
df7492f9 KH |
8510 | /* Now we have decided top J priorities. Reflect the order of the |
8511 | original priorities to the remaining priorities. */ | |
6b89e3aa | 8512 | |
df7492f9 | 8513 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 8514 | { |
df7492f9 KH |
8515 | while (j < coding_category_max |
8516 | && changed[coding_priorities[j]]) | |
8517 | j++; | |
8518 | if (j == coding_category_max) | |
8519 | abort (); | |
8520 | priorities[i] = coding_priorities[j]; | |
8521 | } | |
6b89e3aa | 8522 | |
df7492f9 | 8523 | bcopy (priorities, coding_priorities, sizeof priorities); |
177c0ea7 | 8524 | |
ff563fce KH |
8525 | /* Update `coding-category-list'. */ |
8526 | Vcoding_category_list = Qnil; | |
8527 | for (i = coding_category_max - 1; i >= 0; i--) | |
8528 | Vcoding_category_list | |
8529 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
8530 | Vcoding_category_list); | |
6b89e3aa | 8531 | |
df7492f9 | 8532 | return Qnil; |
6b89e3aa KH |
8533 | } |
8534 | ||
df7492f9 KH |
8535 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
8536 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 DL |
8537 | doc: /* Return a list of coding systems ordered by their priorities. |
8538 | HIGHESTP non-nil means just return the highest priority one. */) | |
df7492f9 KH |
8539 | (highestp) |
8540 | Lisp_Object highestp; | |
d46c5b12 KH |
8541 | { |
8542 | int i; | |
df7492f9 | 8543 | Lisp_Object val; |
6b89e3aa | 8544 | |
df7492f9 | 8545 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 8546 | { |
df7492f9 KH |
8547 | enum coding_category category = coding_priorities[i]; |
8548 | int id = coding_categories[category].id; | |
8549 | Lisp_Object attrs; | |
068a9dbd | 8550 | |
df7492f9 KH |
8551 | if (id < 0) |
8552 | continue; | |
8553 | attrs = CODING_ID_ATTRS (id); | |
8554 | if (! NILP (highestp)) | |
8555 | return CODING_ATTR_BASE_NAME (attrs); | |
8556 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
8557 | } | |
8558 | return Fnreverse (val); | |
8559 | } | |
068a9dbd | 8560 | |
f0064e1f | 8561 | static char *suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
8562 | |
8563 | static Lisp_Object | |
df7492f9 KH |
8564 | make_subsidiaries (base) |
8565 | Lisp_Object base; | |
068a9dbd | 8566 | { |
df7492f9 | 8567 | Lisp_Object subsidiaries; |
8f924df7 | 8568 | int base_name_len = SBYTES (SYMBOL_NAME (base)); |
df7492f9 KH |
8569 | char *buf = (char *) alloca (base_name_len + 6); |
8570 | int i; | |
068a9dbd | 8571 | |
8f924df7 | 8572 | bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len); |
df7492f9 KH |
8573 | subsidiaries = Fmake_vector (make_number (3), Qnil); |
8574 | for (i = 0; i < 3; i++) | |
068a9dbd | 8575 | { |
df7492f9 KH |
8576 | bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1); |
8577 | ASET (subsidiaries, i, intern (buf)); | |
068a9dbd | 8578 | } |
df7492f9 | 8579 | return subsidiaries; |
068a9dbd KH |
8580 | } |
8581 | ||
8582 | ||
df7492f9 KH |
8583 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
8584 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
8585 | doc: /* For internal use only. |
8586 | usage: (define-coding-system-internal ...) */) | |
df7492f9 KH |
8587 | (nargs, args) |
8588 | int nargs; | |
8589 | Lisp_Object *args; | |
068a9dbd | 8590 | { |
df7492f9 KH |
8591 | Lisp_Object name; |
8592 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
8593 | Lisp_Object attrs; /* Vector of attributes. */ | |
8594 | Lisp_Object eol_type; | |
8595 | Lisp_Object aliases; | |
8596 | Lisp_Object coding_type, charset_list, safe_charsets; | |
8597 | enum coding_category category; | |
8598 | Lisp_Object tail, val; | |
8599 | int max_charset_id = 0; | |
8600 | int i; | |
068a9dbd | 8601 | |
df7492f9 KH |
8602 | if (nargs < coding_arg_max) |
8603 | goto short_args; | |
068a9dbd | 8604 | |
df7492f9 | 8605 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 8606 | |
df7492f9 KH |
8607 | name = args[coding_arg_name]; |
8608 | CHECK_SYMBOL (name); | |
8609 | CODING_ATTR_BASE_NAME (attrs) = name; | |
068a9dbd | 8610 | |
df7492f9 KH |
8611 | val = args[coding_arg_mnemonic]; |
8612 | if (! STRINGP (val)) | |
8613 | CHECK_CHARACTER (val); | |
8614 | CODING_ATTR_MNEMONIC (attrs) = val; | |
068a9dbd | 8615 | |
df7492f9 KH |
8616 | coding_type = args[coding_arg_coding_type]; |
8617 | CHECK_SYMBOL (coding_type); | |
8618 | CODING_ATTR_TYPE (attrs) = coding_type; | |
068a9dbd | 8619 | |
df7492f9 KH |
8620 | charset_list = args[coding_arg_charset_list]; |
8621 | if (SYMBOLP (charset_list)) | |
8622 | { | |
8623 | if (EQ (charset_list, Qiso_2022)) | |
8624 | { | |
8625 | if (! EQ (coding_type, Qiso_2022)) | |
8626 | error ("Invalid charset-list"); | |
8627 | charset_list = Viso_2022_charset_list; | |
8628 | } | |
8629 | else if (EQ (charset_list, Qemacs_mule)) | |
8630 | { | |
8631 | if (! EQ (coding_type, Qemacs_mule)) | |
8632 | error ("Invalid charset-list"); | |
8633 | charset_list = Vemacs_mule_charset_list; | |
8634 | } | |
8635 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
8636 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
8637 | max_charset_id = XFASTINT (XCAR (tail)); | |
8638 | } | |
068a9dbd KH |
8639 | else |
8640 | { | |
df7492f9 KH |
8641 | charset_list = Fcopy_sequence (charset_list); |
8642 | for (tail = charset_list; !NILP (tail); tail = Fcdr (tail)) | |
068a9dbd | 8643 | { |
df7492f9 KH |
8644 | struct charset *charset; |
8645 | ||
8646 | val = Fcar (tail); | |
8647 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
8648 | if (EQ (coding_type, Qiso_2022) | |
8649 | ? CHARSET_ISO_FINAL (charset) < 0 | |
8650 | : EQ (coding_type, Qemacs_mule) | |
8651 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
8652 | : 0) | |
8653 | error ("Can't handle charset `%s'", | |
8f924df7 | 8654 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 8655 | |
8f924df7 | 8656 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
8657 | if (max_charset_id < charset->id) |
8658 | max_charset_id = charset->id; | |
068a9dbd KH |
8659 | } |
8660 | } | |
df7492f9 | 8661 | CODING_ATTR_CHARSET_LIST (attrs) = charset_list; |
068a9dbd | 8662 | |
df7492f9 KH |
8663 | safe_charsets = Fmake_string (make_number (max_charset_id + 1), |
8664 | make_number (255)); | |
8665 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
8f924df7 | 8666 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 8667 | CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets; |
068a9dbd | 8668 | |
584948ac | 8669 | CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p]; |
3a73fa5d | 8670 | |
df7492f9 | 8671 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 8672 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 8673 | CHECK_SYMBOL (val); |
df7492f9 | 8674 | CODING_ATTR_DECODE_TBL (attrs) = val; |
3a73fa5d | 8675 | |
df7492f9 | 8676 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 8677 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 8678 | CHECK_SYMBOL (val); |
df7492f9 | 8679 | CODING_ATTR_ENCODE_TBL (attrs) = val; |
d46c5b12 | 8680 | |
df7492f9 KH |
8681 | val = args[coding_arg_post_read_conversion]; |
8682 | CHECK_SYMBOL (val); | |
8683 | CODING_ATTR_POST_READ (attrs) = val; | |
d46c5b12 | 8684 | |
df7492f9 KH |
8685 | val = args[coding_arg_pre_write_conversion]; |
8686 | CHECK_SYMBOL (val); | |
8687 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
3a73fa5d | 8688 | |
df7492f9 KH |
8689 | val = args[coding_arg_default_char]; |
8690 | if (NILP (val)) | |
8691 | CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' '); | |
8692 | else | |
8693 | { | |
8f924df7 | 8694 | CHECK_CHARACTER (val); |
df7492f9 KH |
8695 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; |
8696 | } | |
4031e2bf | 8697 | |
8f924df7 KH |
8698 | val = args[coding_arg_for_unibyte]; |
8699 | CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt; | |
3a73fa5d | 8700 | |
df7492f9 KH |
8701 | val = args[coding_arg_plist]; |
8702 | CHECK_LIST (val); | |
8703 | CODING_ATTR_PLIST (attrs) = val; | |
3a73fa5d | 8704 | |
df7492f9 KH |
8705 | if (EQ (coding_type, Qcharset)) |
8706 | { | |
c7c66a95 KH |
8707 | /* Generate a lisp vector of 256 elements. Each element is nil, |
8708 | integer, or a list of charset IDs. | |
3a73fa5d | 8709 | |
c7c66a95 KH |
8710 | If Nth element is nil, the byte code N is invalid in this |
8711 | coding system. | |
4ed46869 | 8712 | |
c7c66a95 KH |
8713 | If Nth element is a number NUM, N is the first byte of a |
8714 | charset whose ID is NUM. | |
4ed46869 | 8715 | |
c7c66a95 KH |
8716 | If Nth element is a list of charset IDs, N is the first byte |
8717 | of one of them. The list is sorted by dimensions of the | |
2bc515e4 | 8718 | charsets. A charset of smaller dimension comes firtst. */ |
df7492f9 | 8719 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 8720 | |
5c99c2e6 | 8721 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 8722 | { |
c7c66a95 KH |
8723 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
8724 | int dim = CHARSET_DIMENSION (charset); | |
8725 | int idx = (dim - 1) * 4; | |
4ed46869 | 8726 | |
5c99c2e6 | 8727 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
584948ac | 8728 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
4031e2bf | 8729 | |
15d143f7 KH |
8730 | for (i = charset->code_space[idx]; |
8731 | i <= charset->code_space[idx + 1]; i++) | |
8732 | { | |
c7c66a95 KH |
8733 | Lisp_Object tmp, tmp2; |
8734 | int dim2; | |
ec6d2bb8 | 8735 | |
c7c66a95 KH |
8736 | tmp = AREF (val, i); |
8737 | if (NILP (tmp)) | |
8738 | tmp = XCAR (tail); | |
8739 | else if (NUMBERP (tmp)) | |
8740 | { | |
8741 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
8742 | if (dim < dim2) | |
c7c66a95 | 8743 | tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil)); |
f9d71dcd KH |
8744 | else |
8745 | tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil)); | |
c7c66a95 | 8746 | } |
15d143f7 | 8747 | else |
c7c66a95 KH |
8748 | { |
8749 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
8750 | { | |
8751 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
8752 | if (dim < dim2) | |
8753 | break; | |
8754 | } | |
8755 | if (NILP (tmp2)) | |
8756 | tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil)); | |
8757 | else | |
8758 | { | |
8759 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
8760 | XSETCAR (tmp2, XCAR (tail)); | |
8761 | } | |
8762 | } | |
8763 | ASET (val, i, tmp); | |
15d143f7 | 8764 | } |
df7492f9 KH |
8765 | } |
8766 | ASET (attrs, coding_attr_charset_valids, val); | |
8767 | category = coding_category_charset; | |
8768 | } | |
8769 | else if (EQ (coding_type, Qccl)) | |
8770 | { | |
8771 | Lisp_Object valids; | |
ecec61c1 | 8772 | |
df7492f9 KH |
8773 | if (nargs < coding_arg_ccl_max) |
8774 | goto short_args; | |
ecec61c1 | 8775 | |
df7492f9 KH |
8776 | val = args[coding_arg_ccl_decoder]; |
8777 | CHECK_CCL_PROGRAM (val); | |
8778 | if (VECTORP (val)) | |
8779 | val = Fcopy_sequence (val); | |
8780 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 8781 | |
df7492f9 KH |
8782 | val = args[coding_arg_ccl_encoder]; |
8783 | CHECK_CCL_PROGRAM (val); | |
8784 | if (VECTORP (val)) | |
8785 | val = Fcopy_sequence (val); | |
8786 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 8787 | |
df7492f9 KH |
8788 | val = args[coding_arg_ccl_valids]; |
8789 | valids = Fmake_string (make_number (256), make_number (0)); | |
8790 | for (tail = val; !NILP (tail); tail = Fcdr (tail)) | |
8791 | { | |
8dcbea82 | 8792 | int from, to; |
ecec61c1 | 8793 | |
df7492f9 KH |
8794 | val = Fcar (tail); |
8795 | if (INTEGERP (val)) | |
8dcbea82 KH |
8796 | { |
8797 | from = to = XINT (val); | |
8798 | if (from < 0 || from > 255) | |
8799 | args_out_of_range_3 (val, make_number (0), make_number (255)); | |
8800 | } | |
df7492f9 KH |
8801 | else |
8802 | { | |
df7492f9 | 8803 | CHECK_CONS (val); |
8f924df7 KH |
8804 | CHECK_NATNUM_CAR (val); |
8805 | CHECK_NATNUM_CDR (val); | |
df7492f9 | 8806 | from = XINT (XCAR (val)); |
8f924df7 | 8807 | if (from > 255) |
8dcbea82 KH |
8808 | args_out_of_range_3 (XCAR (val), |
8809 | make_number (0), make_number (255)); | |
df7492f9 | 8810 | to = XINT (XCDR (val)); |
8dcbea82 KH |
8811 | if (to < from || to > 255) |
8812 | args_out_of_range_3 (XCDR (val), | |
8813 | XCAR (val), make_number (255)); | |
df7492f9 | 8814 | } |
8dcbea82 | 8815 | for (i = from; i <= to; i++) |
8f924df7 | 8816 | SSET (valids, i, 1); |
df7492f9 KH |
8817 | } |
8818 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 8819 | |
df7492f9 | 8820 | category = coding_category_ccl; |
55ab7be3 | 8821 | } |
df7492f9 | 8822 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 8823 | { |
df7492f9 | 8824 | Lisp_Object bom, endian; |
4ed46869 | 8825 | |
584948ac | 8826 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; |
4ed46869 | 8827 | |
df7492f9 KH |
8828 | if (nargs < coding_arg_utf16_max) |
8829 | goto short_args; | |
4ed46869 | 8830 | |
df7492f9 KH |
8831 | bom = args[coding_arg_utf16_bom]; |
8832 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
8833 | { | |
8834 | CHECK_CONS (bom); | |
8f924df7 KH |
8835 | val = XCAR (bom); |
8836 | CHECK_CODING_SYSTEM (val); | |
8837 | val = XCDR (bom); | |
8838 | CHECK_CODING_SYSTEM (val); | |
df7492f9 KH |
8839 | } |
8840 | ASET (attrs, coding_attr_utf_16_bom, bom); | |
8841 | ||
8842 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
8843 | CHECK_SYMBOL (endian); |
8844 | if (NILP (endian)) | |
8845 | endian = Qbig; | |
8846 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 8847 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
8848 | ASET (attrs, coding_attr_utf_16_endian, endian); |
8849 | ||
8850 | category = (CONSP (bom) | |
8851 | ? coding_category_utf_16_auto | |
8852 | : NILP (bom) | |
b49a1807 | 8853 | ? (EQ (endian, Qbig) |
df7492f9 KH |
8854 | ? coding_category_utf_16_be_nosig |
8855 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 8856 | : (EQ (endian, Qbig) |
df7492f9 KH |
8857 | ? coding_category_utf_16_be |
8858 | : coding_category_utf_16_le)); | |
8859 | } | |
8860 | else if (EQ (coding_type, Qiso_2022)) | |
8861 | { | |
8862 | Lisp_Object initial, reg_usage, request, flags; | |
4776e638 | 8863 | int i; |
1397dc18 | 8864 | |
df7492f9 KH |
8865 | if (nargs < coding_arg_iso2022_max) |
8866 | goto short_args; | |
8867 | ||
8868 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
8869 | CHECK_VECTOR (initial); | |
8870 | for (i = 0; i < 4; i++) | |
8871 | { | |
8872 | val = Faref (initial, make_number (i)); | |
8873 | if (! NILP (val)) | |
8874 | { | |
584948ac KH |
8875 | struct charset *charset; |
8876 | ||
8877 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
8878 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
8879 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
8880 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
8881 | } |
8882 | else | |
8883 | ASET (initial, i, make_number (-1)); | |
8884 | } | |
8885 | ||
8886 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
8887 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
8888 | CHECK_NUMBER_CAR (reg_usage); |
8889 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
8890 | |
8891 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
8892 | for (tail = request; ! NILP (tail); tail = Fcdr (tail)) | |
1397dc18 | 8893 | { |
df7492f9 | 8894 | int id; |
8f924df7 | 8895 | Lisp_Object tmp; |
df7492f9 KH |
8896 | |
8897 | val = Fcar (tail); | |
8898 | CHECK_CONS (val); | |
8f924df7 KH |
8899 | tmp = XCAR (val); |
8900 | CHECK_CHARSET_GET_ID (tmp, id); | |
8901 | CHECK_NATNUM_CDR (val); | |
df7492f9 KH |
8902 | if (XINT (XCDR (val)) >= 4) |
8903 | error ("Invalid graphic register number: %d", XINT (XCDR (val))); | |
8f924df7 | 8904 | XSETCAR (val, make_number (id)); |
1397dc18 | 8905 | } |
4ed46869 | 8906 | |
df7492f9 KH |
8907 | flags = args[coding_arg_iso2022_flags]; |
8908 | CHECK_NATNUM (flags); | |
8909 | i = XINT (flags); | |
8910 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) | |
8911 | flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT); | |
8912 | ||
8913 | ASET (attrs, coding_attr_iso_initial, initial); | |
8914 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
8915 | ASET (attrs, coding_attr_iso_request, request); | |
8916 | ASET (attrs, coding_attr_iso_flags, flags); | |
8917 | setup_iso_safe_charsets (attrs); | |
8918 | ||
8919 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
8920 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
8921 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
8922 | ? coding_category_iso_7_else | |
8923 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
8924 | ? coding_category_iso_7 | |
8925 | : coding_category_iso_7_tight); | |
8926 | else | |
8927 | { | |
8928 | int id = XINT (AREF (initial, 1)); | |
8929 | ||
c6fb6e98 | 8930 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
8931 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
8932 | || id < 0) | |
8933 | ? coding_category_iso_8_else | |
8934 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
8935 | ? coding_category_iso_8_1 | |
8936 | : coding_category_iso_8_2); | |
8937 | } | |
0ce7886f KH |
8938 | if (category != coding_category_iso_8_1 |
8939 | && category != coding_category_iso_8_2) | |
8940 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; | |
df7492f9 KH |
8941 | } |
8942 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 8943 | { |
df7492f9 KH |
8944 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
8945 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
584948ac | 8946 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
df7492f9 | 8947 | category = coding_category_emacs_mule; |
c28a9453 | 8948 | } |
df7492f9 | 8949 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 8950 | { |
df7492f9 KH |
8951 | |
8952 | struct charset *charset; | |
8953 | ||
7d64c6ad | 8954 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 8955 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 8956 | error ("There should be three or four charsets"); |
df7492f9 KH |
8957 | |
8958 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8959 | if (CHARSET_DIMENSION (charset) != 1) | |
8960 | error ("Dimension of charset %s is not one", | |
8f924df7 | 8961 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
8962 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
8963 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
8964 | |
8965 | charset_list = XCDR (charset_list); | |
8966 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8967 | if (CHARSET_DIMENSION (charset) != 1) | |
8968 | error ("Dimension of charset %s is not one", | |
8f924df7 | 8969 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
8970 | |
8971 | charset_list = XCDR (charset_list); | |
8972 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8973 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
8974 | error ("Dimension of charset %s is not two", |
8975 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
8976 | ||
8977 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
8978 | if (! NILP (charset_list)) |
8979 | { | |
8980 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8981 | if (CHARSET_DIMENSION (charset) != 2) | |
8982 | error ("Dimension of charset %s is not two", | |
8983 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
8984 | } | |
df7492f9 KH |
8985 | |
8986 | category = coding_category_sjis; | |
8987 | Vsjis_coding_system = name; | |
c28a9453 | 8988 | } |
df7492f9 KH |
8989 | else if (EQ (coding_type, Qbig5)) |
8990 | { | |
8991 | struct charset *charset; | |
4ed46869 | 8992 | |
df7492f9 KH |
8993 | if (XINT (Flength (charset_list)) != 2) |
8994 | error ("There should be just two charsets"); | |
8995 | ||
8996 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
8997 | if (CHARSET_DIMENSION (charset) != 1) | |
8998 | error ("Dimension of charset %s is not one", | |
8f924df7 | 8999 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
9000 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
9001 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9002 | |
9003 | charset_list = XCDR (charset_list); | |
9004 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9005 | if (CHARSET_DIMENSION (charset) != 2) | |
9006 | error ("Dimension of charset %s is not two", | |
8f924df7 | 9007 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 9008 | |
df7492f9 KH |
9009 | category = coding_category_big5; |
9010 | Vbig5_coding_system = name; | |
9011 | } | |
9012 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 9013 | { |
584948ac KH |
9014 | category = coding_category_raw_text; |
9015 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
c28a9453 | 9016 | } |
df7492f9 | 9017 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 9018 | { |
584948ac KH |
9019 | category = coding_category_utf_8; |
9020 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
4ed46869 | 9021 | } |
df7492f9 KH |
9022 | else if (EQ (coding_type, Qundecided)) |
9023 | category = coding_category_undecided; | |
4ed46869 | 9024 | else |
df7492f9 | 9025 | error ("Invalid coding system type: %s", |
8f924df7 | 9026 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 9027 | |
df7492f9 | 9028 | CODING_ATTR_CATEGORY (attrs) = make_number (category); |
01378f49 KH |
9029 | CODING_ATTR_PLIST (attrs) |
9030 | = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category), | |
9031 | CODING_ATTR_PLIST (attrs))); | |
35befdaa KH |
9032 | CODING_ATTR_PLIST (attrs) |
9033 | = Fcons (QCascii_compatible_p, | |
9034 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), | |
9035 | CODING_ATTR_PLIST (attrs))); | |
c4825358 | 9036 | |
df7492f9 KH |
9037 | eol_type = args[coding_arg_eol_type]; |
9038 | if (! NILP (eol_type) | |
9039 | && ! EQ (eol_type, Qunix) | |
9040 | && ! EQ (eol_type, Qdos) | |
9041 | && ! EQ (eol_type, Qmac)) | |
9042 | error ("Invalid eol-type"); | |
4ed46869 | 9043 | |
df7492f9 | 9044 | aliases = Fcons (name, Qnil); |
4ed46869 | 9045 | |
df7492f9 KH |
9046 | if (NILP (eol_type)) |
9047 | { | |
9048 | eol_type = make_subsidiaries (name); | |
9049 | for (i = 0; i < 3; i++) | |
1397dc18 | 9050 | { |
df7492f9 KH |
9051 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
9052 | ||
9053 | this_name = AREF (eol_type, i); | |
9054 | this_aliases = Fcons (this_name, Qnil); | |
9055 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); | |
9056 | this_spec = Fmake_vector (make_number (3), attrs); | |
9057 | ASET (this_spec, 1, this_aliases); | |
9058 | ASET (this_spec, 2, this_eol_type); | |
9059 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
9060 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
583f71ca KH |
9061 | val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist); |
9062 | if (NILP (val)) | |
9063 | Vcoding_system_alist | |
9064 | = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
9065 | Vcoding_system_alist); | |
1397dc18 | 9066 | } |
d46c5b12 | 9067 | } |
4ed46869 | 9068 | |
df7492f9 KH |
9069 | spec_vec = Fmake_vector (make_number (3), attrs); |
9070 | ASET (spec_vec, 1, aliases); | |
9071 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 9072 | |
df7492f9 KH |
9073 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
9074 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
583f71ca KH |
9075 | val = Fassoc (Fsymbol_name (name), Vcoding_system_alist); |
9076 | if (NILP (val)) | |
9077 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
9078 | Vcoding_system_alist); | |
48b0f3ae | 9079 | |
df7492f9 KH |
9080 | { |
9081 | int id = coding_categories[category].id; | |
48b0f3ae | 9082 | |
df7492f9 KH |
9083 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
9084 | setup_coding_system (name, &coding_categories[category]); | |
9085 | } | |
48b0f3ae | 9086 | |
d46c5b12 | 9087 | return Qnil; |
48b0f3ae | 9088 | |
df7492f9 KH |
9089 | short_args: |
9090 | return Fsignal (Qwrong_number_of_arguments, | |
9091 | Fcons (intern ("define-coding-system-internal"), | |
9092 | make_number (nargs))); | |
d46c5b12 | 9093 | } |
4ed46869 | 9094 | |
d6925f38 | 9095 | |
a6f87d34 KH |
9096 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
9097 | 3, 3, 0, | |
9098 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
9099 | (coding_system, prop, val) | |
9100 | Lisp_Object coding_system, prop, val; | |
9101 | { | |
3dbe7859 | 9102 | Lisp_Object spec, attrs; |
a6f87d34 KH |
9103 | |
9104 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9105 | attrs = AREF (spec, 0); | |
9106 | if (EQ (prop, QCmnemonic)) | |
9107 | { | |
9108 | if (! STRINGP (val)) | |
9109 | CHECK_CHARACTER (val); | |
9110 | CODING_ATTR_MNEMONIC (attrs) = val; | |
9111 | } | |
9112 | else if (EQ (prop, QCdefalut_char)) | |
9113 | { | |
9114 | if (NILP (val)) | |
9115 | val = make_number (' '); | |
9116 | else | |
9117 | CHECK_CHARACTER (val); | |
9118 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; | |
9119 | } | |
9120 | else if (EQ (prop, QCdecode_translation_table)) | |
9121 | { | |
9122 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
9123 | CHECK_SYMBOL (val); | |
9124 | CODING_ATTR_DECODE_TBL (attrs) = val; | |
9125 | } | |
9126 | else if (EQ (prop, QCencode_translation_table)) | |
9127 | { | |
9128 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
9129 | CHECK_SYMBOL (val); | |
9130 | CODING_ATTR_ENCODE_TBL (attrs) = val; | |
9131 | } | |
9132 | else if (EQ (prop, QCpost_read_conversion)) | |
9133 | { | |
9134 | CHECK_SYMBOL (val); | |
9135 | CODING_ATTR_POST_READ (attrs) = val; | |
9136 | } | |
9137 | else if (EQ (prop, QCpre_write_conversion)) | |
9138 | { | |
9139 | CHECK_SYMBOL (val); | |
9140 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
9141 | } | |
35befdaa KH |
9142 | else if (EQ (prop, QCascii_compatible_p)) |
9143 | { | |
9144 | CODING_ATTR_ASCII_COMPAT (attrs) = val; | |
9145 | } | |
a6f87d34 KH |
9146 | |
9147 | CODING_ATTR_PLIST (attrs) | |
9148 | = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val); | |
9149 | return val; | |
9150 | } | |
9151 | ||
9152 | ||
df7492f9 KH |
9153 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
9154 | Sdefine_coding_system_alias, 2, 2, 0, | |
9155 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
9156 | (alias, coding_system) | |
9157 | Lisp_Object alias, coding_system; | |
66cfb530 | 9158 | { |
583f71ca | 9159 | Lisp_Object spec, aliases, eol_type, val; |
4ed46869 | 9160 | |
df7492f9 KH |
9161 | CHECK_SYMBOL (alias); |
9162 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9163 | aliases = AREF (spec, 1); | |
d6925f38 KH |
9164 | /* ALISES should be a list of length more than zero, and the first |
9165 | element is a base coding system. Append ALIAS at the tail of the | |
9166 | list. */ | |
df7492f9 KH |
9167 | while (!NILP (XCDR (aliases))) |
9168 | aliases = XCDR (aliases); | |
8f924df7 | 9169 | XSETCDR (aliases, Fcons (alias, Qnil)); |
4ed46869 | 9170 | |
df7492f9 KH |
9171 | eol_type = AREF (spec, 2); |
9172 | if (VECTORP (eol_type)) | |
4ed46869 | 9173 | { |
df7492f9 KH |
9174 | Lisp_Object subsidiaries; |
9175 | int i; | |
4ed46869 | 9176 | |
df7492f9 KH |
9177 | subsidiaries = make_subsidiaries (alias); |
9178 | for (i = 0; i < 3; i++) | |
9179 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
9180 | AREF (eol_type, i)); | |
4ed46869 | 9181 | } |
df7492f9 KH |
9182 | |
9183 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 9184 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
583f71ca KH |
9185 | val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist); |
9186 | if (NILP (val)) | |
9187 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), | |
9188 | Vcoding_system_alist); | |
66cfb530 | 9189 | |
4ed46869 KH |
9190 | return Qnil; |
9191 | } | |
9192 | ||
df7492f9 KH |
9193 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
9194 | 1, 1, 0, | |
9195 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 9196 | Any alias or subsidiary coding system is not a base coding system. */) |
df7492f9 KH |
9197 | (coding_system) |
9198 | Lisp_Object coding_system; | |
d46c5b12 | 9199 | { |
df7492f9 | 9200 | Lisp_Object spec, attrs; |
d46c5b12 | 9201 | |
df7492f9 KH |
9202 | if (NILP (coding_system)) |
9203 | return (Qno_conversion); | |
9204 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9205 | attrs = AREF (spec, 0); | |
9206 | return CODING_ATTR_BASE_NAME (attrs); | |
9207 | } | |
1397dc18 | 9208 | |
df7492f9 KH |
9209 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
9210 | 1, 1, 0, | |
9211 | doc: "Return the property list of CODING-SYSTEM.") | |
9212 | (coding_system) | |
9213 | Lisp_Object coding_system; | |
9214 | { | |
9215 | Lisp_Object spec, attrs; | |
1397dc18 | 9216 | |
df7492f9 KH |
9217 | if (NILP (coding_system)) |
9218 | coding_system = Qno_conversion; | |
9219 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
9220 | attrs = AREF (spec, 0); | |
9221 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
9222 | } |
9223 | ||
df7492f9 KH |
9224 | |
9225 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
9226 | 1, 1, 0, | |
da7db224 | 9227 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
df7492f9 KH |
9228 | (coding_system) |
9229 | Lisp_Object coding_system; | |
66cfb530 | 9230 | { |
df7492f9 | 9231 | Lisp_Object spec; |
84d60297 | 9232 | |
df7492f9 KH |
9233 | if (NILP (coding_system)) |
9234 | coding_system = Qno_conversion; | |
9235 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 9236 | return AREF (spec, 1); |
df7492f9 | 9237 | } |
66cfb530 | 9238 | |
df7492f9 KH |
9239 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
9240 | Scoding_system_eol_type, 1, 1, 0, | |
9241 | doc: /* Return eol-type of CODING-SYSTEM. | |
9242 | An eol-type is integer 0, 1, 2, or a vector of coding systems. | |
66cfb530 | 9243 | |
df7492f9 KH |
9244 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
9245 | and CR respectively. | |
66cfb530 | 9246 | |
df7492f9 KH |
9247 | A vector value indicates that a format of end-of-line should be |
9248 | detected automatically. Nth element of the vector is the subsidiary | |
9249 | coding system whose eol-type is N. */) | |
6b89e3aa KH |
9250 | (coding_system) |
9251 | Lisp_Object coding_system; | |
9252 | { | |
df7492f9 KH |
9253 | Lisp_Object spec, eol_type; |
9254 | int n; | |
6b89e3aa | 9255 | |
df7492f9 KH |
9256 | if (NILP (coding_system)) |
9257 | coding_system = Qno_conversion; | |
9258 | if (! CODING_SYSTEM_P (coding_system)) | |
9259 | return Qnil; | |
9260 | spec = CODING_SYSTEM_SPEC (coding_system); | |
9261 | eol_type = AREF (spec, 2); | |
9262 | if (VECTORP (eol_type)) | |
9263 | return Fcopy_sequence (eol_type); | |
9264 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
9265 | return make_number (n); | |
6b89e3aa KH |
9266 | } |
9267 | ||
4ed46869 KH |
9268 | #endif /* emacs */ |
9269 | ||
9270 | \f | |
1397dc18 | 9271 | /*** 9. Post-amble ***/ |
4ed46869 | 9272 | |
dfcf069d | 9273 | void |
4ed46869 KH |
9274 | init_coding_once () |
9275 | { | |
9276 | int i; | |
9277 | ||
df7492f9 KH |
9278 | for (i = 0; i < coding_category_max; i++) |
9279 | { | |
9280 | coding_categories[i].id = -1; | |
9281 | coding_priorities[i] = i; | |
9282 | } | |
4ed46869 KH |
9283 | |
9284 | /* ISO2022 specific initialize routine. */ | |
9285 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 9286 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
9287 | for (i = 0x21; i < 0x7F; i++) |
9288 | iso_code_class[i] = ISO_graphic_plane_0; | |
9289 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 9290 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
9291 | for (i = 0xA1; i < 0xFF; i++) |
9292 | iso_code_class[i] = ISO_graphic_plane_1; | |
9293 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
9294 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
9295 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
9296 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
9297 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
9298 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
9299 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
9300 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
9301 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
9302 | ||
df7492f9 KH |
9303 | for (i = 0; i < 256; i++) |
9304 | { | |
9305 | emacs_mule_bytes[i] = 1; | |
9306 | } | |
7c78e542 KH |
9307 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
9308 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
9309 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
9310 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
9311 | } |
9312 | ||
9313 | #ifdef emacs | |
9314 | ||
dfcf069d | 9315 | void |
e0e989f6 KH |
9316 | syms_of_coding () |
9317 | { | |
df7492f9 | 9318 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
9319 | { |
9320 | Lisp_Object args[2]; | |
9321 | args[0] = QCtest; | |
9322 | args[1] = Qeq; | |
9323 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
9324 | } | |
df7492f9 KH |
9325 | |
9326 | staticpro (&Vsjis_coding_system); | |
9327 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 9328 | |
df7492f9 KH |
9329 | staticpro (&Vbig5_coding_system); |
9330 | Vbig5_coding_system = Qnil; | |
9331 | ||
24a73b0a KH |
9332 | staticpro (&Vcode_conversion_reused_workbuf); |
9333 | Vcode_conversion_reused_workbuf = Qnil; | |
9334 | ||
9335 | staticpro (&Vcode_conversion_workbuf_name); | |
9336 | Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*"); | |
e0e989f6 | 9337 | |
24a73b0a | 9338 | reused_workbuf_in_use = 0; |
df7492f9 KH |
9339 | |
9340 | DEFSYM (Qcharset, "charset"); | |
9341 | DEFSYM (Qtarget_idx, "target-idx"); | |
9342 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
9343 | Fset (Qcoding_system_history, Qnil); |
9344 | ||
9ce27fde | 9345 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 9346 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 9347 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
9348 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
9349 | ||
df7492f9 | 9350 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 9351 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
9352 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
9353 | ||
df7492f9 | 9354 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 9355 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
9356 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
9357 | ||
df7492f9 | 9358 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 9359 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
9360 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
9361 | ||
df7492f9 | 9362 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 9363 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
9364 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
9365 | ||
df7492f9 KH |
9366 | DEFSYM (Qcoding_system, "coding-system"); |
9367 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 9368 | |
df7492f9 KH |
9369 | DEFSYM (Qeol_type, "eol-type"); |
9370 | DEFSYM (Qunix, "unix"); | |
9371 | DEFSYM (Qdos, "dos"); | |
4ed46869 | 9372 | |
df7492f9 KH |
9373 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
9374 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
9375 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
9376 | DEFSYM (Qdefault_char, "default-char"); | |
9377 | DEFSYM (Qundecided, "undecided"); | |
9378 | DEFSYM (Qno_conversion, "no-conversion"); | |
9379 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 9380 | |
df7492f9 | 9381 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 9382 | |
df7492f9 | 9383 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 9384 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 9385 | |
df7492f9 | 9386 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
9387 | DEFSYM (Qbig, "big"); |
9388 | DEFSYM (Qlittle, "little"); | |
27901516 | 9389 | |
df7492f9 KH |
9390 | DEFSYM (Qshift_jis, "shift-jis"); |
9391 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 9392 | |
df7492f9 | 9393 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 9394 | |
df7492f9 | 9395 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 KH |
9396 | Fput (Qcoding_system_error, Qerror_conditions, |
9397 | Fcons (Qcoding_system_error, Fcons (Qerror, Qnil))); | |
9398 | Fput (Qcoding_system_error, Qerror_message, | |
9ce27fde | 9399 | build_string ("Invalid coding system")); |
4ed46869 | 9400 | |
05e6f5dc KH |
9401 | /* Intern this now in case it isn't already done. |
9402 | Setting this variable twice is harmless. | |
9403 | But don't staticpro it here--that is done in alloc.c. */ | |
9404 | Qchar_table_extra_slots = intern ("char-table-extra-slots"); | |
70c22245 | 9405 | |
df7492f9 | 9406 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 9407 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
9408 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
9409 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
9410 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 9411 | |
df7492f9 | 9412 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 9413 | |
df7492f9 | 9414 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 9415 | |
01378f49 | 9416 | DEFSYM (QCcategory, ":category"); |
a6f87d34 KH |
9417 | DEFSYM (QCmnemonic, ":mnemonic"); |
9418 | DEFSYM (QCdefalut_char, ":default-char"); | |
9419 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); | |
9420 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
9421 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
9422 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 9423 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 9424 | |
df7492f9 KH |
9425 | Vcoding_category_table |
9426 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
9427 | staticpro (&Vcoding_category_table); | |
9428 | /* Followings are target of code detection. */ | |
9429 | ASET (Vcoding_category_table, coding_category_iso_7, | |
9430 | intern ("coding-category-iso-7")); | |
9431 | ASET (Vcoding_category_table, coding_category_iso_7_tight, | |
9432 | intern ("coding-category-iso-7-tight")); | |
9433 | ASET (Vcoding_category_table, coding_category_iso_8_1, | |
9434 | intern ("coding-category-iso-8-1")); | |
9435 | ASET (Vcoding_category_table, coding_category_iso_8_2, | |
9436 | intern ("coding-category-iso-8-2")); | |
9437 | ASET (Vcoding_category_table, coding_category_iso_7_else, | |
9438 | intern ("coding-category-iso-7-else")); | |
9439 | ASET (Vcoding_category_table, coding_category_iso_8_else, | |
9440 | intern ("coding-category-iso-8-else")); | |
9441 | ASET (Vcoding_category_table, coding_category_utf_8, | |
9442 | intern ("coding-category-utf-8")); | |
9443 | ASET (Vcoding_category_table, coding_category_utf_16_be, | |
9444 | intern ("coding-category-utf-16-be")); | |
ff563fce KH |
9445 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
9446 | intern ("coding-category-utf-16-auto")); | |
df7492f9 KH |
9447 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
9448 | intern ("coding-category-utf-16-le")); | |
9449 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, | |
9450 | intern ("coding-category-utf-16-be-nosig")); | |
9451 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, | |
9452 | intern ("coding-category-utf-16-le-nosig")); | |
9453 | ASET (Vcoding_category_table, coding_category_charset, | |
9454 | intern ("coding-category-charset")); | |
9455 | ASET (Vcoding_category_table, coding_category_sjis, | |
9456 | intern ("coding-category-sjis")); | |
9457 | ASET (Vcoding_category_table, coding_category_big5, | |
9458 | intern ("coding-category-big5")); | |
9459 | ASET (Vcoding_category_table, coding_category_ccl, | |
9460 | intern ("coding-category-ccl")); | |
9461 | ASET (Vcoding_category_table, coding_category_emacs_mule, | |
9462 | intern ("coding-category-emacs-mule")); | |
9463 | /* Followings are NOT target of code detection. */ | |
9464 | ASET (Vcoding_category_table, coding_category_raw_text, | |
9465 | intern ("coding-category-raw-text")); | |
9466 | ASET (Vcoding_category_table, coding_category_undecided, | |
9467 | intern ("coding-category-undecided")); | |
ecf488bc | 9468 | |
065e3595 KH |
9469 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
9470 | DEFSYM (Qinconsistent_eol, "inconsistent-eol"); | |
9471 | DEFSYM (Qinvalid_source, "invalid-source"); | |
9472 | DEFSYM (Qinterrupted, "interrupted"); | |
9473 | DEFSYM (Qinsufficient_memory, "insufficient-memory"); | |
44e8490d | 9474 | DEFSYM (Qcoding_system_define_form, "coding-system-define-form"); |
065e3595 | 9475 | |
4ed46869 KH |
9476 | defsubr (&Scoding_system_p); |
9477 | defsubr (&Sread_coding_system); | |
9478 | defsubr (&Sread_non_nil_coding_system); | |
9479 | defsubr (&Scheck_coding_system); | |
9480 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 9481 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 9482 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 9483 | defsubr (&Sunencodable_char_position); |
df7492f9 | 9484 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
9485 | defsubr (&Sdecode_coding_region); |
9486 | defsubr (&Sencode_coding_region); | |
9487 | defsubr (&Sdecode_coding_string); | |
9488 | defsubr (&Sencode_coding_string); | |
9489 | defsubr (&Sdecode_sjis_char); | |
9490 | defsubr (&Sencode_sjis_char); | |
9491 | defsubr (&Sdecode_big5_char); | |
9492 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 9493 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 9494 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 9495 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 9496 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 9497 | defsubr (&Skeyboard_coding_system); |
a5d301df | 9498 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 9499 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 9500 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 9501 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 9502 | defsubr (&Scoding_system_put); |
df7492f9 KH |
9503 | defsubr (&Scoding_system_base); |
9504 | defsubr (&Scoding_system_plist); | |
9505 | defsubr (&Scoding_system_aliases); | |
9506 | defsubr (&Scoding_system_eol_type); | |
9507 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 9508 | |
4608c386 | 9509 | DEFVAR_LISP ("coding-system-list", &Vcoding_system_list, |
48b0f3ae PJ |
9510 | doc: /* List of coding systems. |
9511 | ||
9512 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 9513 | updated by the functions `define-coding-system' and |
48b0f3ae | 9514 | `define-coding-system-alias'. */); |
4608c386 KH |
9515 | Vcoding_system_list = Qnil; |
9516 | ||
9517 | DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist, | |
48b0f3ae PJ |
9518 | doc: /* Alist of coding system names. |
9519 | Each element is one element list of coding system name. | |
9520 | This variable is given to `completing-read' as TABLE argument. | |
9521 | ||
9522 | Do not alter the value of this variable manually. This variable should be | |
9523 | updated by the functions `make-coding-system' and | |
9524 | `define-coding-system-alias'. */); | |
4608c386 KH |
9525 | Vcoding_system_alist = Qnil; |
9526 | ||
4ed46869 | 9527 | DEFVAR_LISP ("coding-category-list", &Vcoding_category_list, |
48b0f3ae PJ |
9528 | doc: /* List of coding-categories (symbols) ordered by priority. |
9529 | ||
9530 | On detecting a coding system, Emacs tries code detection algorithms | |
9531 | associated with each coding-category one by one in this order. When | |
9532 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
9533 | system bound to the corresponding coding-category is selected. |
9534 | ||
42205607 | 9535 | Don't modify this variable directly, but use `set-coding-priority'. */); |
4ed46869 KH |
9536 | { |
9537 | int i; | |
9538 | ||
9539 | Vcoding_category_list = Qnil; | |
df7492f9 | 9540 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 9541 | Vcoding_category_list |
d46c5b12 KH |
9542 | = Fcons (XVECTOR (Vcoding_category_table)->contents[i], |
9543 | Vcoding_category_list); | |
4ed46869 KH |
9544 | } |
9545 | ||
9546 | DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read, | |
48b0f3ae PJ |
9547 | doc: /* Specify the coding system for read operations. |
9548 | It is useful to bind this variable with `let', but do not set it globally. | |
9549 | If the value is a coding system, it is used for decoding on read operation. | |
9550 | If not, an appropriate element is used from one of the coding system alists: | |
9551 | There are three such tables, `file-coding-system-alist', | |
9552 | `process-coding-system-alist', and `network-coding-system-alist'. */); | |
4ed46869 KH |
9553 | Vcoding_system_for_read = Qnil; |
9554 | ||
9555 | DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write, | |
48b0f3ae PJ |
9556 | doc: /* Specify the coding system for write operations. |
9557 | Programs bind this variable with `let', but you should not set it globally. | |
9558 | If the value is a coding system, it is used for encoding of output, | |
9559 | when writing it to a file and when sending it to a file or subprocess. | |
9560 | ||
9561 | If this does not specify a coding system, an appropriate element | |
9562 | is used from one of the coding system alists: | |
9563 | There are three such tables, `file-coding-system-alist', | |
9564 | `process-coding-system-alist', and `network-coding-system-alist'. | |
9565 | For output to files, if the above procedure does not specify a coding system, | |
9566 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
9567 | Vcoding_system_for_write = Qnil; |
9568 | ||
9569 | DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used, | |
df7492f9 KH |
9570 | doc: /* |
9571 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
9572 | Vlast_coding_system_used = Qnil; |
9573 | ||
065e3595 KH |
9574 | DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error, |
9575 | doc: /* | |
9576 | Error status of the last code conversion. | |
9577 | ||
9578 | When an error was detected in the last code conversion, this variable | |
9579 | is set to one of the following symbols. | |
9580 | `insufficient-source' | |
9581 | `inconsistent-eol' | |
9582 | `invalid-source' | |
9583 | `interrupted' | |
9584 | `insufficient-memory' | |
9585 | When no error was detected, the value doesn't change. So, to check | |
9586 | the error status of a code conversion by this variable, you must | |
9587 | explicitly set this variable to nil before performing code | |
9588 | conversion. */); | |
9589 | Vlast_code_conversion_error = Qnil; | |
9590 | ||
9ce27fde | 9591 | DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion, |
df7492f9 KH |
9592 | doc: /* |
9593 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
9594 | See info node `Coding Systems' and info node `Text and Binary' concerning |
9595 | such conversion. */); | |
9ce27fde KH |
9596 | inhibit_eol_conversion = 0; |
9597 | ||
ed29121d | 9598 | DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system, |
df7492f9 KH |
9599 | doc: /* |
9600 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
9601 | Bind it to t if the process output is to be treated as if it were a file |
9602 | read from some filesystem. */); | |
ed29121d EZ |
9603 | inherit_process_coding_system = 0; |
9604 | ||
02ba4723 | 9605 | DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist, |
df7492f9 KH |
9606 | doc: /* |
9607 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
9608 | The format is ((PATTERN . VAL) ...), |
9609 | where PATTERN is a regular expression matching a file name, | |
9610 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
9611 | If VAL is a coding system, it is used for both decoding and encoding | |
9612 | the file contents. | |
9613 | If VAL is a cons of coding systems, the car part is used for decoding, | |
9614 | and the cdr part is used for encoding. | |
9615 | If VAL is a function symbol, the function must return a coding system | |
0192762c DL |
9616 | or a cons of coding systems which are used as above. The function gets |
9617 | the arguments with which `find-operation-coding-systems' was called. | |
48b0f3ae PJ |
9618 | |
9619 | See also the function `find-operation-coding-system' | |
9620 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
9621 | Vfile_coding_system_alist = Qnil; |
9622 | ||
9623 | DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist, | |
df7492f9 KH |
9624 | doc: /* |
9625 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
9626 | The format is ((PATTERN . VAL) ...), |
9627 | where PATTERN is a regular expression matching a program name, | |
9628 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
9629 | If VAL is a coding system, it is used for both decoding what received | |
9630 | from the program and encoding what sent to the program. | |
9631 | If VAL is a cons of coding systems, the car part is used for decoding, | |
9632 | and the cdr part is used for encoding. | |
9633 | If VAL is a function symbol, the function must return a coding system | |
9634 | or a cons of coding systems which are used as above. | |
9635 | ||
9636 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
9637 | Vprocess_coding_system_alist = Qnil; |
9638 | ||
9639 | DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist, | |
df7492f9 KH |
9640 | doc: /* |
9641 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
9642 | The format is ((PATTERN . VAL) ...), |
9643 | where PATTERN is a regular expression matching a network service name | |
9644 | or is a port number to connect to, | |
9645 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
9646 | If VAL is a coding system, it is used for both decoding what received | |
9647 | from the network stream and encoding what sent to the network stream. | |
9648 | If VAL is a cons of coding systems, the car part is used for decoding, | |
9649 | and the cdr part is used for encoding. | |
9650 | If VAL is a function symbol, the function must return a coding system | |
9651 | or a cons of coding systems which are used as above. | |
9652 | ||
9653 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 9654 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 9655 | |
68c45bf0 | 9656 | DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system, |
75205970 RS |
9657 | doc: /* Coding system to use with system messages. |
9658 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
9659 | Vlocale_coding_system = Qnil; |
9660 | ||
005f0d35 | 9661 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
7722baf9 | 9662 | DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix, |
df7492f9 KH |
9663 | doc: /* |
9664 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
7722baf9 | 9665 | eol_mnemonic_unix = build_string (":"); |
4ed46869 | 9666 | |
7722baf9 | 9667 | DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos, |
df7492f9 KH |
9668 | doc: /* |
9669 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
7722baf9 | 9670 | eol_mnemonic_dos = build_string ("\\"); |
4ed46869 | 9671 | |
7722baf9 | 9672 | DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac, |
df7492f9 KH |
9673 | doc: /* |
9674 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
7722baf9 | 9675 | eol_mnemonic_mac = build_string ("/"); |
4ed46869 | 9676 | |
7722baf9 | 9677 | DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided, |
df7492f9 KH |
9678 | doc: /* |
9679 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
7722baf9 | 9680 | eol_mnemonic_undecided = build_string (":"); |
4ed46869 | 9681 | |
84fbb8a0 | 9682 | DEFVAR_LISP ("enable-character-translation", &Venable_character_translation, |
df7492f9 KH |
9683 | doc: /* |
9684 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 9685 | Venable_character_translation = Qt; |
bdd9fb48 | 9686 | |
f967223b | 9687 | DEFVAR_LISP ("standard-translation-table-for-decode", |
48b0f3ae PJ |
9688 | &Vstandard_translation_table_for_decode, |
9689 | doc: /* Table for translating characters while decoding. */); | |
f967223b | 9690 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 9691 | |
f967223b | 9692 | DEFVAR_LISP ("standard-translation-table-for-encode", |
48b0f3ae PJ |
9693 | &Vstandard_translation_table_for_encode, |
9694 | doc: /* Table for translating characters while encoding. */); | |
f967223b | 9695 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 9696 | |
df7492f9 | 9697 | DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table, |
48b0f3ae PJ |
9698 | doc: /* Alist of charsets vs revision numbers. |
9699 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
9700 | designate it with the escape sequence identifying revision (cdr part |
9701 | of the element). */); | |
9702 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
9703 | |
9704 | DEFVAR_LISP ("default-process-coding-system", | |
9705 | &Vdefault_process_coding_system, | |
48b0f3ae PJ |
9706 | doc: /* Cons of coding systems used for process I/O by default. |
9707 | The car part is used for decoding a process output, | |
9708 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 9709 | Vdefault_process_coding_system = Qnil; |
c4825358 | 9710 | |
3f003981 | 9711 | DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table, |
df7492f9 KH |
9712 | doc: /* |
9713 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
9714 | This is a vector of length 256. |
9715 | If Nth element is non-nil, the existence of code N in a file | |
9716 | \(or output of subprocess) doesn't prevent it to be detected as | |
9717 | a coding system of ISO 2022 variant which has a flag | |
9718 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
9719 | or reading output of a subprocess. | |
9720 | Only 128th through 159th elements has a meaning. */); | |
3f003981 | 9721 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
9722 | |
9723 | DEFVAR_LISP ("select-safe-coding-system-function", | |
9724 | &Vselect_safe_coding_system_function, | |
df7492f9 KH |
9725 | doc: /* |
9726 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
9727 | |
9728 | If set, this function is called to force a user to select a proper | |
9729 | coding system which can encode the text in the case that a default | |
9730 | coding system used in each operation can't encode the text. | |
9731 | ||
9732 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
9733 | Vselect_safe_coding_system_function = Qnil; |
9734 | ||
5d5bf4d8 KH |
9735 | DEFVAR_BOOL ("coding-system-require-warning", |
9736 | &coding_system_require_warning, | |
9737 | doc: /* Internal use only. | |
6b89e3aa KH |
9738 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
9739 | called even if `coding-system-for-write' is non-nil. The command | |
9740 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
9741 | coding_system_require_warning = 0; |
9742 | ||
9743 | ||
22ab2303 | 9744 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
74383408 | 9745 | &inhibit_iso_escape_detection, |
df7492f9 KH |
9746 | doc: /* |
9747 | If non-nil, Emacs ignores ISO2022's escape sequence on code detection. | |
48b0f3ae PJ |
9748 | |
9749 | By default, on reading a file, Emacs tries to detect how the text is | |
9750 | encoded. This code detection is sensitive to escape sequences. If | |
9751 | the sequence is valid as ISO2022, the code is determined as one of | |
9752 | the ISO2022 encodings, and the file is decoded by the corresponding | |
9753 | coding system (e.g. `iso-2022-7bit'). | |
9754 | ||
9755 | However, there may be a case that you want to read escape sequences in | |
9756 | a file as is. In such a case, you can set this variable to non-nil. | |
9757 | Then, as the code detection ignores any escape sequences, no file is | |
9758 | detected as encoded in some ISO2022 encoding. The result is that all | |
9759 | escape sequences become visible in a buffer. | |
9760 | ||
9761 | The default value is nil, and it is strongly recommended not to change | |
9762 | it. That is because many Emacs Lisp source files that contain | |
9763 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
9764 | in Emacs's distribution, and they won't be decoded correctly on | |
9765 | reading if you suppress escape sequence detection. | |
9766 | ||
9767 | The other way to read escape sequences in a file without decoding is | |
9768 | to explicitly specify some coding system that doesn't use ISO2022's | |
9769 | escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */); | |
74383408 | 9770 | inhibit_iso_escape_detection = 0; |
002fdb44 DL |
9771 | |
9772 | DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input, | |
15c8f9d1 DL |
9773 | doc: /* Char table for translating self-inserting characters. |
9774 | This is applied to the result of input methods, not their input. See also | |
9775 | `keyboard-translate-table'. */); | |
002fdb44 | 9776 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 9777 | |
2c78b7e1 KH |
9778 | { |
9779 | Lisp_Object args[coding_arg_max]; | |
8f924df7 | 9780 | Lisp_Object plist[16]; |
2c78b7e1 KH |
9781 | int i; |
9782 | ||
9783 | for (i = 0; i < coding_arg_max; i++) | |
9784 | args[i] = Qnil; | |
9785 | ||
9786 | plist[0] = intern (":name"); | |
9787 | plist[1] = args[coding_arg_name] = Qno_conversion; | |
9788 | plist[2] = intern (":mnemonic"); | |
9789 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); | |
9790 | plist[4] = intern (":coding-type"); | |
9791 | plist[5] = args[coding_arg_coding_type] = Qraw_text; | |
9792 | plist[6] = intern (":ascii-compatible-p"); | |
9793 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; | |
9794 | plist[8] = intern (":default-char"); | |
9795 | plist[9] = args[coding_arg_default_char] = make_number (0); | |
8f924df7 KH |
9796 | plist[10] = intern (":for-unibyte"); |
9797 | plist[11] = args[coding_arg_for_unibyte] = Qt; | |
9798 | plist[12] = intern (":docstring"); | |
9799 | plist[13] = build_string ("Do no conversion.\n\ | |
2c78b7e1 KH |
9800 | \n\ |
9801 | When you visit a file with this coding, the file is read into a\n\ | |
9802 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
9803 | character."); | |
8f924df7 KH |
9804 | plist[14] = intern (":eol-type"); |
9805 | plist[15] = args[coding_arg_eol_type] = Qunix; | |
9806 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 9807 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
9808 | |
9809 | plist[1] = args[coding_arg_name] = Qundecided; | |
9810 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
9811 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
9812 | /* This is already set. | |
35befdaa | 9813 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
ae6f73fa KH |
9814 | plist[8] = intern (":charset-list"); |
9815 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); | |
9816 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
9817 | plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding."); | |
9818 | plist[15] = args[coding_arg_eol_type] = Qnil; | |
9819 | args[coding_arg_plist] = Flist (16, plist); | |
9820 | Fdefine_coding_system_internal (coding_arg_max, args); | |
2c78b7e1 KH |
9821 | } |
9822 | ||
9823 | setup_coding_system (Qno_conversion, &keyboard_coding); | |
ae6f73fa | 9824 | setup_coding_system (Qundecided, &terminal_coding); |
2c78b7e1 | 9825 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
9826 | |
9827 | { | |
9828 | int i; | |
9829 | ||
9830 | for (i = 0; i < coding_category_max; i++) | |
9831 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
9832 | } | |
fcbcfb64 KH |
9833 | #if defined (MSDOS) || defined (WINDOWSNT) |
9834 | system_eol_type = Qdos; | |
9835 | #else | |
9836 | system_eol_type = Qunix; | |
9837 | #endif | |
9838 | staticpro (&system_eol_type); | |
4ed46869 KH |
9839 | } |
9840 | ||
68c45bf0 PE |
9841 | char * |
9842 | emacs_strerror (error_number) | |
9843 | int error_number; | |
9844 | { | |
9845 | char *str; | |
9846 | ||
ca9c0567 | 9847 | synchronize_system_messages_locale (); |
68c45bf0 PE |
9848 | str = strerror (error_number); |
9849 | ||
9850 | if (! NILP (Vlocale_coding_system)) | |
9851 | { | |
9852 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
9853 | Vlocale_coding_system, | |
9854 | 0); | |
d5db4077 | 9855 | str = (char *) SDATA (dec); |
68c45bf0 PE |
9856 | } |
9857 | ||
9858 | return str; | |
9859 | } | |
9860 | ||
4ed46869 | 9861 | #endif /* emacs */ |
9ffd559c KH |
9862 | |
9863 | /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d | |
9864 | (do not change this comment) */ |