Commit | Line | Data |
---|---|---|
4ed46869 | 1 | /* Header for coding system handler. |
acaf905b | 2 | Copyright (C) 2001-2012 Free Software Foundation, Inc. |
7976eda0 | 3 | Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
ce03bf76 KH |
5 | National Institute of Advanced Industrial Science and Technology (AIST) |
6 | Registration Number H14PRO021 | |
8f924df7 | 7 | Copyright (C) 2003 |
df7492f9 KH |
8 | National Institute of Advanced Industrial Science and Technology (AIST) |
9 | Registration Number H13PRO009 | |
4ed46869 | 10 | |
369314dc KH |
11 | This file is part of GNU Emacs. |
12 | ||
b9b1cc14 | 13 | GNU Emacs is free software: you can redistribute it and/or modify |
369314dc | 14 | it under the terms of the GNU General Public License as published by |
b9b1cc14 GM |
15 | the Free Software Foundation, either version 3 of the License, or |
16 | (at your option) any later version. | |
4ed46869 | 17 | |
369314dc KH |
18 | GNU Emacs is distributed in the hope that it will be useful, |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
4ed46869 | 22 | |
369314dc | 23 | You should have received a copy of the GNU General Public License |
b9b1cc14 | 24 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ed46869 | 25 | |
6f776e81 KH |
26 | #ifndef EMACS_CODING_H |
27 | #define EMACS_CODING_H | |
4ed46869 | 28 | |
df7492f9 | 29 | /* Index to arguments of Fdefine_coding_system_internal. */ |
4ed46869 | 30 | |
df7492f9 KH |
31 | enum define_coding_system_arg_index |
32 | { | |
33 | coding_arg_name, | |
34 | coding_arg_mnemonic, | |
35 | coding_arg_coding_type, | |
36 | coding_arg_charset_list, | |
37 | coding_arg_ascii_compatible_p, | |
38 | coding_arg_decode_translation_table, | |
39 | coding_arg_encode_translation_table, | |
40 | coding_arg_post_read_conversion, | |
41 | coding_arg_pre_write_conversion, | |
42 | coding_arg_default_char, | |
8f924df7 | 43 | coding_arg_for_unibyte, |
df7492f9 KH |
44 | coding_arg_plist, |
45 | coding_arg_eol_type, | |
46 | coding_arg_max | |
47 | }; | |
4ed46869 | 48 | |
df7492f9 | 49 | enum define_coding_iso2022_arg_index |
4ed46869 | 50 | { |
df7492f9 KH |
51 | coding_arg_iso2022_initial = coding_arg_max, |
52 | coding_arg_iso2022_reg_usage, | |
53 | coding_arg_iso2022_request, | |
54 | coding_arg_iso2022_flags, | |
55 | coding_arg_iso2022_max | |
4ed46869 KH |
56 | }; |
57 | ||
e4215ddd KH |
58 | enum define_coding_utf8_arg_index |
59 | { | |
60 | coding_arg_utf8_bom = coding_arg_max, | |
61 | coding_arg_utf8_max | |
62 | }; | |
63 | ||
df7492f9 | 64 | enum define_coding_utf16_arg_index |
4ed46869 | 65 | { |
df7492f9 KH |
66 | coding_arg_utf16_bom = coding_arg_max, |
67 | coding_arg_utf16_endian, | |
68 | coding_arg_utf16_max | |
4ed46869 KH |
69 | }; |
70 | ||
df7492f9 KH |
71 | enum define_coding_ccl_arg_index |
72 | { | |
35d47d18 | 73 | coding_arg_ccl_decoder = coding_arg_max, |
df7492f9 KH |
74 | coding_arg_ccl_encoder, |
75 | coding_arg_ccl_valids, | |
76 | coding_arg_ccl_max | |
77 | }; | |
4ed46869 | 78 | |
933373ed KH |
79 | /* Hash table for all coding systems. Keys are coding system symbols |
80 | and values are spec vectors of the corresponding coding system. A | |
81 | spec vector has the form [ ATTRS ALIASES EOL-TYPE ]. ATTRS is a | |
82 | vector of attribute of the coding system. ALIASES is a list of | |
83 | aliases (symbols) of the coding system. EOL-TYPE is `unix', `dos', | |
84 | `mac' or a vector of coding systems (symbols). */ | |
85 | ||
df7492f9 | 86 | extern Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 87 | |
933373ed | 88 | |
df7492f9 | 89 | /* Enumeration of coding system type. */ |
4ed46869 | 90 | |
df7492f9 KH |
91 | enum coding_system_type |
92 | { | |
93 | coding_type_charset, | |
94 | coding_type_utf_8, | |
95 | coding_type_utf_16, | |
96 | coding_type_iso_2022, | |
97 | coding_type_emacs_mule, | |
98 | coding_type_sjis, | |
99 | coding_type_ccl, | |
100 | coding_type_raw_text, | |
101 | coding_type_undecided, | |
102 | coding_type_max | |
103 | }; | |
4ed46869 | 104 | |
4ed46869 | 105 | |
df7492f9 | 106 | /* Enumeration of end-of-line format type. */ |
4ed46869 | 107 | |
df7492f9 KH |
108 | enum end_of_line_type |
109 | { | |
110 | eol_lf, /* Line-feed only, same as Emacs' internal | |
111 | format. */ | |
112 | eol_crlf, /* Sequence of carriage-return and | |
113 | line-feed. */ | |
114 | eol_cr, /* Carriage-return only. */ | |
115 | eol_any, /* Accept any of above. Produce line-feed | |
116 | only. */ | |
117 | eol_undecided, /* This value is used to denote that the | |
118 | eol-type is not yet undecided. */ | |
119 | eol_type_max | |
120 | }; | |
4ed46869 | 121 | |
df7492f9 | 122 | /* Enumeration of index to an attribute vector of a coding system. */ |
4ed46869 | 123 | |
df7492f9 KH |
124 | enum coding_attr_index |
125 | { | |
126 | coding_attr_base_name, | |
127 | coding_attr_docstring, | |
128 | coding_attr_mnemonic, | |
129 | coding_attr_type, | |
130 | coding_attr_charset_list, | |
131 | coding_attr_ascii_compat, | |
132 | coding_attr_decode_tbl, | |
133 | coding_attr_encode_tbl, | |
404202e7 | 134 | coding_attr_trans_tbl, |
df7492f9 KH |
135 | coding_attr_post_read, |
136 | coding_attr_pre_write, | |
137 | coding_attr_default_char, | |
8f924df7 | 138 | coding_attr_for_unibyte, |
df7492f9 KH |
139 | coding_attr_plist, |
140 | ||
141 | coding_attr_category, | |
142 | coding_attr_safe_charsets, | |
143 | ||
144 | /* The followings are extra attributes for each type. */ | |
145 | coding_attr_charset_valids, | |
146 | ||
147 | coding_attr_ccl_decoder, | |
148 | coding_attr_ccl_encoder, | |
149 | coding_attr_ccl_valids, | |
150 | ||
151 | coding_attr_iso_initial, | |
152 | coding_attr_iso_usage, | |
153 | coding_attr_iso_request, | |
154 | coding_attr_iso_flags, | |
155 | ||
e4215ddd | 156 | coding_attr_utf_bom, |
df7492f9 KH |
157 | coding_attr_utf_16_endian, |
158 | ||
159 | coding_attr_emacs_mule_full, | |
160 | ||
161 | coding_attr_last_index | |
162 | }; | |
4ed46869 | 163 | |
4ed46869 | 164 | |
933373ed KH |
165 | /* Macros to access an element of an attribute vector. */ |
166 | ||
04e05596 JB |
167 | #define CODING_ATTR_BASE_NAME(attrs) AREF (attrs, coding_attr_base_name) |
168 | #define CODING_ATTR_TYPE(attrs) AREF (attrs, coding_attr_type) | |
169 | #define CODING_ATTR_CHARSET_LIST(attrs) AREF (attrs, coding_attr_charset_list) | |
170 | #define CODING_ATTR_MNEMONIC(attrs) AREF (attrs, coding_attr_mnemonic) | |
171 | #define CODING_ATTR_DOCSTRING(attrs) AREF (attrs, coding_attr_docstring) | |
172 | #define CODING_ATTR_ASCII_COMPAT(attrs) AREF (attrs, coding_attr_ascii_compat) | |
173 | #define CODING_ATTR_DECODE_TBL(attrs) AREF (attrs, coding_attr_decode_tbl) | |
174 | #define CODING_ATTR_ENCODE_TBL(attrs) AREF (attrs, coding_attr_encode_tbl) | |
175 | #define CODING_ATTR_TRANS_TBL(attrs) AREF (attrs, coding_attr_trans_tbl) | |
176 | #define CODING_ATTR_POST_READ(attrs) AREF (attrs, coding_attr_post_read) | |
177 | #define CODING_ATTR_PRE_WRITE(attrs) AREF (attrs, coding_attr_pre_write) | |
178 | #define CODING_ATTR_DEFAULT_CHAR(attrs) AREF (attrs, coding_attr_default_char) | |
179 | #define CODING_ATTR_FOR_UNIBYTE(attrs) AREF (attrs, coding_attr_for_unibyte) | |
04e05596 JB |
180 | #define CODING_ATTR_PLIST(attrs) AREF (attrs, coding_attr_plist) |
181 | #define CODING_ATTR_CATEGORY(attrs) AREF (attrs, coding_attr_category) | |
182 | #define CODING_ATTR_SAFE_CHARSETS(attrs)AREF (attrs, coding_attr_safe_charsets) | |
8ddb35b2 | 183 | |
8ddb35b2 | 184 | |
933373ed KH |
185 | /* Return the name of a coding system specified by ID. */ |
186 | #define CODING_ID_NAME(id) \ | |
187 | (HASH_KEY (XHASH_TABLE (Vcoding_system_hash_table), id)) | |
188 | ||
189 | /* Return the attribute vector of a coding system specified by ID. */ | |
190 | ||
df7492f9 KH |
191 | #define CODING_ID_ATTRS(id) \ |
192 | (AREF (HASH_VALUE (XHASH_TABLE (Vcoding_system_hash_table), id), 0)) | |
fbaa2ed9 | 193 | |
933373ed KH |
194 | /* Return the list of aliases of a coding system specified by ID. */ |
195 | ||
df7492f9 KH |
196 | #define CODING_ID_ALIASES(id) \ |
197 | (AREF (HASH_VALUE (XHASH_TABLE (Vcoding_system_hash_table), id), 1)) | |
c0c69d45 | 198 | |
933373ed KH |
199 | /* Return the eol-type of a coding system specified by ID. */ |
200 | ||
df7492f9 KH |
201 | #define CODING_ID_EOL_TYPE(id) \ |
202 | (AREF (HASH_VALUE (XHASH_TABLE (Vcoding_system_hash_table), id), 2)) | |
658cc252 | 203 | |
933373ed KH |
204 | |
205 | /* Return the spec vector of CODING_SYSTEM_SYMBOL. */ | |
fbaa2ed9 | 206 | |
df7492f9 KH |
207 | #define CODING_SYSTEM_SPEC(coding_system_symbol) \ |
208 | (Fgethash (coding_system_symbol, Vcoding_system_hash_table, Qnil)) | |
4ed46869 | 209 | |
933373ed KH |
210 | |
211 | /* Return the ID of CODING_SYSTEM_SYMBOL. */ | |
212 | ||
df7492f9 KH |
213 | #define CODING_SYSTEM_ID(coding_system_symbol) \ |
214 | hash_lookup (XHASH_TABLE (Vcoding_system_hash_table), \ | |
215 | coding_system_symbol, NULL) | |
4ed46869 | 216 | |
b2e6b10f | 217 | /* Return 1 if CODING_SYSTEM_SYMBOL is a coding system. */ |
933373ed | 218 | |
7c00e33d KH |
219 | #define CODING_SYSTEM_P(coding_system_symbol) \ |
220 | (CODING_SYSTEM_ID (coding_system_symbol) >= 0 \ | |
221 | || (! NILP (coding_system_symbol) \ | |
222 | && ! NILP (Fcoding_system_p (coding_system_symbol)))) | |
4ed46869 | 223 | |
933373ed KH |
224 | /* Check if X is a coding system or not. */ |
225 | ||
8f924df7 | 226 | #define CHECK_CODING_SYSTEM(x) \ |
df7492f9 | 227 | do { \ |
7c00e33d KH |
228 | if (CODING_SYSTEM_ID (x) < 0 \ |
229 | && NILP (Fcheck_coding_system (x))) \ | |
8f924df7 | 230 | wrong_type_argument (Qcoding_system_p, (x)); \ |
df7492f9 | 231 | } while (0) |
658cc252 | 232 | |
4ed46869 | 233 | |
933373ed KH |
234 | /* Check if X is a coding system or not. If it is, set SEPC to the |
235 | spec vector of the coding system. */ | |
236 | ||
df7492f9 KH |
237 | #define CHECK_CODING_SYSTEM_GET_SPEC(x, spec) \ |
238 | do { \ | |
239 | spec = CODING_SYSTEM_SPEC (x); \ | |
7c00e33d KH |
240 | if (NILP (spec)) \ |
241 | { \ | |
242 | Fcheck_coding_system (x); \ | |
243 | spec = CODING_SYSTEM_SPEC (x); \ | |
244 | } \ | |
df7492f9 | 245 | if (NILP (spec)) \ |
02dfeba8 | 246 | wrong_type_argument (Qcoding_system_p, (x)); \ |
df7492f9 | 247 | } while (0) |
e6de76f8 | 248 | |
8ddb35b2 | 249 | |
933373ed KH |
250 | /* Check if X is a coding system or not. If it is, set ID to the |
251 | ID of the coding system. */ | |
252 | ||
df7492f9 KH |
253 | #define CHECK_CODING_SYSTEM_GET_ID(x, id) \ |
254 | do \ | |
255 | { \ | |
256 | id = CODING_SYSTEM_ID (x); \ | |
7c00e33d KH |
257 | if (id < 0) \ |
258 | { \ | |
259 | Fcheck_coding_system (x); \ | |
260 | id = CODING_SYSTEM_ID (x); \ | |
261 | } \ | |
df7492f9 | 262 | if (id < 0) \ |
02dfeba8 | 263 | wrong_type_argument (Qcoding_system_p, (x)); \ |
df7492f9 | 264 | } while (0) |
4ed46869 | 265 | |
4ed46869 KH |
266 | |
267 | /*** GENERAL section ***/ | |
268 | ||
df7492f9 KH |
269 | /* Enumeration of result code of code conversion. */ |
270 | enum coding_result_code | |
4ed46869 | 271 | { |
df7492f9 KH |
272 | CODING_RESULT_SUCCESS, |
273 | CODING_RESULT_INSUFFICIENT_SRC, | |
274 | CODING_RESULT_INSUFFICIENT_DST, | |
275 | CODING_RESULT_INCONSISTENT_EOL, | |
63e11478 | 276 | CODING_RESULT_INVALID_SRC, |
df7492f9 KH |
277 | CODING_RESULT_INTERRUPT, |
278 | CODING_RESULT_INSUFFICIENT_MEM | |
4ed46869 KH |
279 | }; |
280 | ||
658cc252 | 281 | |
3b2d77fe | 282 | /* Macros used for the member `mode' of the struct coding_system. */ |
658cc252 KH |
283 | |
284 | /* If set, recover the original CR or LF of the already decoded text | |
285 | when the decoding routine encounters an inconsistent eol format. */ | |
286 | #define CODING_MODE_INHIBIT_INCONSISTENT_EOL 0x01 | |
287 | ||
288 | /* If set, the decoding/encoding routines treat the current data as | |
5998373a | 289 | the last block of the whole text to be converted, and do the |
55496054 | 290 | appropriate finishing job. */ |
658cc252 KH |
291 | #define CODING_MODE_LAST_BLOCK 0x02 |
292 | ||
293 | /* If set, it means that the current source text is in a buffer which | |
294 | enables selective display. */ | |
295 | #define CODING_MODE_SELECTIVE_DISPLAY 0x04 | |
296 | ||
297 | /* This flag is used by the decoding/encoding routines on the fly. If | |
298 | set, it means that right-to-left text is being processed. */ | |
299 | #define CODING_MODE_DIRECTION 0x08 | |
300 | ||
df7492f9 KH |
301 | #define CODING_MODE_FIXED_DESTINATION 0x10 |
302 | ||
933373ed KH |
303 | /* If set, it means that the encoding routines produces some safe |
304 | ASCII characters (usually '?') for unsupported characters. */ | |
df7492f9 KH |
305 | #define CODING_MODE_SAFE_ENCODING 0x20 |
306 | ||
825d0875 KH |
307 | /* For handling composition sequence. */ |
308 | #include "composite.h" | |
309 | ||
310 | enum composition_state | |
311 | { | |
312 | COMPOSING_NO, | |
313 | COMPOSING_CHAR, | |
314 | COMPOSING_RULE, | |
315 | COMPOSING_COMPONENT_CHAR, | |
316 | COMPOSING_COMPONENT_RULE | |
317 | }; | |
318 | ||
319 | /* Structure for the current composition status. */ | |
320 | struct composition_status | |
321 | { | |
322 | enum composition_state state; | |
323 | enum composition_method method; | |
324 | int old_form; /* 0:pre-21 form, 1:post-21 form */ | |
325 | int length; /* number of elements produced in charbuf */ | |
326 | int nchars; /* number of characters composed */ | |
327 | int ncomps; /* number of composition components */ | |
328 | /* Maximum carryover is for the case of COMPOSITION_WITH_RULE_ALTCHARS. | |
329 | See the comment in coding.c. */ | |
330 | int carryover[4 /* annotation header */ | |
331 | + MAX_COMPOSITION_COMPONENTS * 3 - 2 /* ALTs and RULEs */ | |
332 | + 2 /* intermediate -1 -1 */ | |
333 | + MAX_COMPOSITION_COMPONENTS /* CHARs */ | |
334 | ]; | |
335 | }; | |
336 | ||
337 | ||
df7492f9 KH |
338 | /* Structure of the field `spec.iso_2022' in the structure |
339 | `coding_system'. */ | |
340 | struct iso_2022_spec | |
4ed46869 | 341 | { |
2ec49574 | 342 | /* Bit-wise-or of CODING_ISO_FLAG_XXX. */ |
df7492f9 | 343 | unsigned flags; |
4ed46869 | 344 | |
df7492f9 KH |
345 | /* The current graphic register invoked to each graphic plane. */ |
346 | int current_invocation[2]; | |
658cc252 | 347 | |
df7492f9 KH |
348 | /* The current charset designated to each graphic register. The |
349 | value -1 means that not charset is designated, -2 means that | |
350 | there was an invalid designation previously. */ | |
351 | int current_designation[4]; | |
4ed46869 | 352 | |
df7492f9 KH |
353 | /* Set to 1 temporarily only when graphic register 2 or 3 is invoked |
354 | by single-shift while encoding. */ | |
355 | int single_shifting; | |
4ed46869 | 356 | |
df7492f9 KH |
357 | /* Set to 1 temporarily only when processing at beginning of line. */ |
358 | int bol; | |
825d0875 KH |
359 | |
360 | /* If positive, we are now scanning CTEXT extended segment. */ | |
361 | int ctext_extended_segment_len; | |
362 | ||
363 | /* If nonzero, we are now scanning embedded UTF-8 sequence. */ | |
364 | int embedded_utf_8; | |
365 | ||
366 | /* The current composition. */ | |
367 | struct composition_status cmp_status; | |
368 | }; | |
369 | ||
370 | struct emacs_mule_spec | |
371 | { | |
372 | int full_support; | |
373 | struct composition_status cmp_status; | |
df7492f9 | 374 | }; |
4ed46869 | 375 | |
df7492f9 | 376 | struct ccl_spec; |
4ed46869 | 377 | |
e4215ddd | 378 | enum utf_bom_type |
df7492f9 | 379 | { |
e4215ddd KH |
380 | utf_detect_bom, |
381 | utf_without_bom, | |
382 | utf_with_bom | |
df7492f9 | 383 | }; |
279d9f7b | 384 | |
df7492f9 KH |
385 | enum utf_16_endian_type |
386 | { | |
387 | utf_16_big_endian, | |
388 | utf_16_little_endian | |
389 | }; | |
279d9f7b | 390 | |
df7492f9 KH |
391 | struct utf_16_spec |
392 | { | |
e4215ddd | 393 | enum utf_bom_type bom; |
df7492f9 KH |
394 | enum utf_16_endian_type endian; |
395 | int surrogate; | |
396 | }; | |
279d9f7b | 397 | |
4fecac5c KH |
398 | struct coding_detection_info |
399 | { | |
400 | /* Values of these members are bitwise-OR of CATEGORY_MASK_XXXs. */ | |
401 | /* Which categories are already checked. */ | |
402 | int checked; | |
403 | /* Which categories are strongly found. */ | |
404 | int found; | |
405 | /* Which categories are rejected. */ | |
406 | int rejected; | |
407 | }; | |
279d9f7b | 408 | |
279d9f7b | 409 | |
df7492f9 KH |
410 | struct coding_system |
411 | { | |
412 | /* ID number of the coding system. This is an index to | |
413 | Vcoding_system_hash_table. This value is set by | |
414 | setup_coding_system. At the early stage of building time, this | |
415 | value is -1 in the array coding_categories to indicate that no | |
416 | coding-system of that category is yet defined. */ | |
d3411f89 | 417 | ptrdiff_t id; |
df7492f9 KH |
418 | |
419 | /* Flag bits of the coding system. The meaning of each bit is common | |
420 | to all types of coding systems. */ | |
421 | int common_flags; | |
422 | ||
423 | /* Mode bits of the coding system. See the comments of the macros | |
424 | CODING_MODE_XXX. */ | |
425 | unsigned int mode; | |
450c60a5 | 426 | |
4ed46869 | 427 | /* Detailed information specific to each type of coding system. */ |
df7492f9 | 428 | union |
4ed46869 | 429 | { |
df7492f9 KH |
430 | struct iso_2022_spec iso_2022; |
431 | struct ccl_spec *ccl; /* Defined in ccl.h. */ | |
432 | struct utf_16_spec utf_16; | |
e4215ddd | 433 | enum utf_bom_type utf_8_bom; |
825d0875 | 434 | struct emacs_mule_spec emacs_mule; |
4ed46869 KH |
435 | } spec; |
436 | ||
df7492f9 | 437 | int max_charset_id; |
1b3b981b | 438 | unsigned char *safe_charsets; |
658cc252 | 439 | |
df7492f9 KH |
440 | /* The following two members specify how binary 8-bit code 128..255 |
441 | are represented in source and destination text respectively. 1 | |
442 | means they are represented by 2-byte sequence, 0 means they are | |
443 | represented by 1-byte as is (see the comment in character.h). */ | |
811ea086 KH |
444 | unsigned src_multibyte : 1; |
445 | unsigned dst_multibyte : 1; | |
446 | ||
a137bb00 KH |
447 | /* How may heading bytes we can skip for decoding. This is set to |
448 | -1 in setup_coding_system, and updated by detect_coding. So, | |
449 | when this is equal to the byte length of the text being | |
450 | converted, we can skip the actual conversion process. */ | |
d311d28c | 451 | ptrdiff_t head_ascii; |
658cc252 KH |
452 | |
453 | /* The following members are set by encoding/decoding routine. */ | |
d311d28c | 454 | ptrdiff_t produced, produced_char, consumed, consumed_char; |
658cc252 | 455 | |
811ea086 KH |
456 | /* Number of error source data found in a decoding routine. */ |
457 | int errors; | |
458 | ||
6d5eb5b0 | 459 | /* Store the positions of error source data. */ |
d311d28c | 460 | ptrdiff_t *error_positions; |
e6a9a0bc | 461 | |
df7492f9 KH |
462 | /* Finish status of code conversion. */ |
463 | enum coding_result_code result; | |
6041c9ce | 464 | |
d311d28c | 465 | ptrdiff_t src_pos, src_pos_byte, src_chars, src_bytes; |
df7492f9 | 466 | Lisp_Object src_object; |
8f924df7 | 467 | const unsigned char *source; |
4ed46869 | 468 | |
d311d28c | 469 | ptrdiff_t dst_pos, dst_pos_byte, dst_bytes; |
df7492f9 KH |
470 | Lisp_Object dst_object; |
471 | unsigned char *destination; | |
4ed46869 | 472 | |
b2e6b10f | 473 | /* Set to 1 if the source of conversion is not in the member |
933373ed | 474 | `charbuf', but at `src_object'. */ |
df7492f9 | 475 | int chars_at_source; |
4ed46869 | 476 | |
df7492f9 KH |
477 | /* If an element is non-negative, it is a character code. |
478 | ||
479 | If it is in the range -128..-1, it is a 8-bit character code | |
480 | minus 256. | |
481 | ||
482 | If it is less than -128, it specifies the start of an annotation | |
483 | chunk. The length of the chunk is -128 minus the value of the | |
484 | element. The following elements are OFFSET, ANNOTATION-TYPE, and | |
485 | a sequence of actual data for the annotation. OFFSET is a | |
486 | character position offset from dst_pos or src_pos, | |
22bcf204 | 487 | ANNOTATION-TYPE specifies the meaning of the annotation and how to |
df7492f9 KH |
488 | handle the following data.. */ |
489 | int *charbuf; | |
490 | int charbuf_size, charbuf_used; | |
491 | ||
492 | /* Set to 1 if charbuf contains an annotation. */ | |
493 | int annotated; | |
4ed46869 | 494 | |
df7492f9 KH |
495 | unsigned char carryover[64]; |
496 | int carryover_bytes; | |
a5ee738b | 497 | |
df7492f9 KH |
498 | int default_char; |
499 | ||
383e0970 J |
500 | int (*detector) (struct coding_system *, |
501 | struct coding_detection_info *); | |
502 | void (*decoder) (struct coding_system *); | |
503 | int (*encoder) (struct coding_system *); | |
df7492f9 KH |
504 | }; |
505 | ||
506 | /* Meanings of bits in the member `common_flags' of the structure | |
507 | coding_system. The lowest 8 bits are reserved for various kind of | |
508 | annotations (currently two of them are used). */ | |
509 | #define CODING_ANNOTATION_MASK 0x00FF | |
510 | #define CODING_ANNOTATE_COMPOSITION_MASK 0x0001 | |
511 | #define CODING_ANNOTATE_DIRECTION_MASK 0x0002 | |
4fecac5c | 512 | #define CODING_ANNOTATE_CHARSET_MASK 0x0003 |
df7492f9 KH |
513 | #define CODING_FOR_UNIBYTE_MASK 0x0100 |
514 | #define CODING_REQUIRE_FLUSHING_MASK 0x0200 | |
515 | #define CODING_REQUIRE_DECODING_MASK 0x0400 | |
516 | #define CODING_REQUIRE_ENCODING_MASK 0x0800 | |
517 | #define CODING_REQUIRE_DETECTION_MASK 0x1000 | |
518 | #define CODING_RESET_AT_BOL_MASK 0x2000 | |
519 | ||
91af3942 | 520 | /* Return 1 if the coding context CODING requires annotation |
df7492f9 KH |
521 | handling. */ |
522 | #define CODING_REQUIRE_ANNOTATION(coding) \ | |
523 | ((coding)->common_flags & CODING_ANNOTATION_MASK) | |
524 | ||
525 | /* Return 1 if the coding context CODING prefers decoding into unibyte. */ | |
526 | #define CODING_FOR_UNIBYTE(coding) \ | |
527 | ((coding)->common_flags & CODING_FOR_UNIBYTE_MASK) | |
528 | ||
529 | /* Return 1 if the coding context CODING requires specific code to be | |
a5ee738b KH |
530 | attached at the tail of converted text. */ |
531 | #define CODING_REQUIRE_FLUSHING(coding) \ | |
532 | ((coding)->common_flags & CODING_REQUIRE_FLUSHING_MASK) | |
533 | ||
df7492f9 | 534 | /* Return 1 if the coding context CODING requires code conversion on |
a5ee738b KH |
535 | decoding. */ |
536 | #define CODING_REQUIRE_DECODING(coding) \ | |
811ea086 KH |
537 | ((coding)->dst_multibyte \ |
538 | || (coding)->common_flags & CODING_REQUIRE_DECODING_MASK) | |
a5ee738b | 539 | |
df7492f9 KH |
540 | |
541 | /* Return 1 if the coding context CODING requires code conversion on | |
c198294f KH |
542 | encoding. |
543 | The non-multibyte part of the condition is to support encoding of | |
544 | unibyte strings/buffers generated by string-as-unibyte or | |
545 | (set-buffer-multibyte nil) from multibyte strings/buffers. */ | |
df7492f9 KH |
546 | #define CODING_REQUIRE_ENCODING(coding) \ |
547 | ((coding)->src_multibyte \ | |
548 | || (coding)->common_flags & CODING_REQUIRE_ENCODING_MASK \ | |
549 | || (coding)->mode & CODING_MODE_SELECTIVE_DISPLAY) | |
550 | ||
a5ee738b | 551 | |
df7492f9 | 552 | /* Return 1 if the coding context CODING requires some kind of code |
a5ee738b KH |
553 | detection. */ |
554 | #define CODING_REQUIRE_DETECTION(coding) \ | |
555 | ((coding)->common_flags & CODING_REQUIRE_DETECTION_MASK) | |
556 | ||
df7492f9 | 557 | /* Return 1 if the coding context CODING requires code conversion on |
811ea086 | 558 | decoding or some kind of code detection. */ |
658cc252 | 559 | #define CODING_MAY_REQUIRE_DECODING(coding) \ |
811ea086 KH |
560 | (CODING_REQUIRE_DECODING (coding) \ |
561 | || CODING_REQUIRE_DETECTION (coding)) | |
4ed46869 | 562 | |
4ed46869 KH |
563 | /* Macros to decode or encode a character of JISX0208 in SJIS. S1 and |
564 | S2 are the 1st and 2nd position-codes of JISX0208 in SJIS coding | |
565 | system. C1 and C2 are the 1st and 2nd position codes of Emacs' | |
566 | internal format. */ | |
567 | ||
df7492f9 KH |
568 | #define SJIS_TO_JIS(code) \ |
569 | do { \ | |
570 | int s1, s2, j1, j2; \ | |
571 | \ | |
572 | s1 = (code) >> 8, s2 = (code) & 0xFF; \ | |
573 | \ | |
574 | if (s2 >= 0x9F) \ | |
575 | (j1 = s1 * 2 - (s1 >= 0xE0 ? 0x160 : 0xE0), \ | |
576 | j2 = s2 - 0x7E); \ | |
577 | else \ | |
578 | (j1 = s1 * 2 - ((s1 >= 0xE0) ? 0x161 : 0xE1), \ | |
579 | j2 = s2 - ((s2 >= 0x7F) ? 0x20 : 0x1F)); \ | |
580 | (code) = (j1 << 8) | j2; \ | |
4ed46869 KH |
581 | } while (0) |
582 | ||
6e58724e KH |
583 | #define SJIS_TO_JIS2(code) \ |
584 | do { \ | |
585 | int s1, s2, j1, j2; \ | |
586 | \ | |
587 | s1 = (code) >> 8, s2 = (code) & 0xFF; \ | |
588 | \ | |
589 | if (s2 >= 0x9F) \ | |
590 | { \ | |
591 | j1 = (s1 == 0xF0 ? 0x28 \ | |
592 | : s1 == 0xF1 ? 0x24 \ | |
593 | : s1 == 0xF2 ? 0x2C \ | |
594 | : s1 == 0xF3 ? 0x2E \ | |
595 | : 0x6E + (s1 - 0xF4) * 2); \ | |
596 | j2 = s2 - 0x7E; \ | |
597 | } \ | |
598 | else \ | |
599 | { \ | |
600 | j1 = (s1 <= 0xF2 ? 0x21 + (s1 - 0xF0) * 2 \ | |
601 | : s1 <= 0xF4 ? 0x2D + (s1 - 0xF3) * 2 \ | |
602 | : 0x6F + (s1 - 0xF5) * 2); \ | |
603 | j2 = s2 - ((s2 >= 0x7F ? 0x20 : 0x1F)); \ | |
604 | } \ | |
605 | (code) = (j1 << 8) | j2; \ | |
606 | } while (0) | |
607 | ||
df7492f9 KH |
608 | |
609 | #define JIS_TO_SJIS(code) \ | |
4ed46869 | 610 | do { \ |
df7492f9 KH |
611 | int s1, s2, j1, j2; \ |
612 | \ | |
613 | j1 = (code) >> 8, j2 = (code) & 0xFF; \ | |
614 | if (j1 & 1) \ | |
615 | (s1 = j1 / 2 + ((j1 < 0x5F) ? 0x71 : 0xB1), \ | |
616 | s2 = j2 + ((j2 >= 0x60) ? 0x20 : 0x1F)); \ | |
4ed46869 | 617 | else \ |
df7492f9 KH |
618 | (s1 = j1 / 2 + ((j1 < 0x5F) ? 0x70 : 0xB0), \ |
619 | s2 = j2 + 0x7E); \ | |
5afaefc1 | 620 | (code) = (s1 << 8) | s2; \ |
4ed46869 KH |
621 | } while (0) |
622 | ||
6e58724e KH |
623 | #define JIS_TO_SJIS2(code) \ |
624 | do { \ | |
625 | int s1, s2, j1, j2; \ | |
626 | \ | |
627 | j1 = (code) >> 8, j2 = (code) & 0xFF; \ | |
628 | if (j1 & 1) \ | |
629 | { \ | |
630 | s1 = (j1 <= 0x25 ? 0xF0 + (j1 - 0x21) / 2 \ | |
edb61b39 | 631 | : j1 <= 0x2F ? 0xF3 + (j1 - 0x2D) / 2 \ |
6e58724e KH |
632 | : 0xF5 + (j1 - 0x6F) / 2); \ |
633 | s2 = j2 + ((j2 >= 0x60) ? 0x20 : 0x1F); \ | |
634 | } \ | |
635 | else \ | |
636 | { \ | |
637 | s1 = (j1 == 0x28 ? 0xF0 \ | |
638 | : j1 == 0x24 ? 0xF1 \ | |
639 | : j1 == 0x2C ? 0xF2 \ | |
640 | : j1 == 0x2E ? 0xF3 \ | |
641 | : 0xF4 + (j1 - 0x6E) / 2); \ | |
642 | s2 = j2 + 0x7E; \ | |
643 | } \ | |
644 | (code) = (s1 << 8) | s2; \ | |
645 | } while (0) | |
df7492f9 | 646 | |
290591c8 KH |
647 | /* Encode the file name NAME using the specified coding system |
648 | for file names, if any. */ | |
649 | #define ENCODE_FILE(name) \ | |
650 | (! NILP (Vfile_name_coding_system) \ | |
5e92b1ca | 651 | && !EQ (Vfile_name_coding_system, make_number (0)) \ |
afee9150 | 652 | ? code_convert_string_norecord (name, Vfile_name_coding_system, 1) \ |
290591c8 | 653 | : (! NILP (Vdefault_file_name_coding_system) \ |
5e92b1ca | 654 | && !EQ (Vdefault_file_name_coding_system, make_number (0)) \ |
afee9150 | 655 | ? code_convert_string_norecord (name, Vdefault_file_name_coding_system, 1) \ |
290591c8 KH |
656 | : name)) |
657 | ||
df7492f9 | 658 | |
290591c8 KH |
659 | /* Decode the file name NAME using the specified coding system |
660 | for file names, if any. */ | |
661 | #define DECODE_FILE(name) \ | |
662 | (! NILP (Vfile_name_coding_system) \ | |
5e92b1ca | 663 | && !EQ (Vfile_name_coding_system, make_number (0)) \ |
afee9150 | 664 | ? code_convert_string_norecord (name, Vfile_name_coding_system, 0) \ |
290591c8 | 665 | : (! NILP (Vdefault_file_name_coding_system) \ |
5e92b1ca | 666 | && !EQ (Vdefault_file_name_coding_system, make_number (0)) \ |
afee9150 | 667 | ? code_convert_string_norecord (name, Vdefault_file_name_coding_system, 0) \ |
290591c8 KH |
668 | : name)) |
669 | ||
df7492f9 | 670 | |
2dfda962 | 671 | /* Encode the string STR using the specified coding system |
53eda481 | 672 | for system functions, if any. */ |
2dfda962 | 673 | #define ENCODE_SYSTEM(str) \ |
9b58c683 | 674 | (! NILP (Vlocale_coding_system) \ |
5e92b1ca | 675 | && !EQ (Vlocale_coding_system, make_number (0)) \ |
9b58c683 | 676 | ? code_convert_string_norecord (str, Vlocale_coding_system, 1) \ |
2dfda962 JR |
677 | : str) |
678 | ||
679 | /* Decode the string STR using the specified coding system | |
53eda481 | 680 | for system functions, if any. */ |
581e7427 | 681 | #define DECODE_SYSTEM(str) \ |
9b58c683 | 682 | (! NILP (Vlocale_coding_system) \ |
5e92b1ca | 683 | && !EQ (Vlocale_coding_system, make_number (0)) \ |
9b58c683 | 684 | ? code_convert_string_norecord (str, Vlocale_coding_system, 0) \ |
2dfda962 | 685 | : str) |
cf29bf99 | 686 | |
5bbb4727 | 687 | /* Note that this encodes utf-8, not utf-8-emacs, so it's not a no-op. */ |
b3a208b0 DL |
688 | #define ENCODE_UTF_8(str) code_convert_string_norecord (str, Qutf_8, 1) |
689 | ||
4ed46869 | 690 | /* Extern declarations. */ |
383e0970 J |
691 | extern Lisp_Object code_conversion_save (int, int); |
692 | extern int decoding_buffer_size (struct coding_system *, int); | |
693 | extern int encoding_buffer_size (struct coding_system *, int); | |
694 | extern void setup_coding_system (Lisp_Object, struct coding_system *); | |
695 | extern Lisp_Object coding_charset_list (struct coding_system *); | |
696 | extern Lisp_Object coding_system_charset_list (Lisp_Object); | |
383e0970 J |
697 | extern Lisp_Object code_convert_string (Lisp_Object, Lisp_Object, |
698 | Lisp_Object, int, int, int); | |
699 | extern Lisp_Object code_convert_string_norecord (Lisp_Object, Lisp_Object, | |
700 | int); | |
701 | extern Lisp_Object raw_text_coding_system (Lisp_Object); | |
702 | extern Lisp_Object coding_inherit_eol_type (Lisp_Object, Lisp_Object); | |
4628bef1 | 703 | extern Lisp_Object complement_process_encoding_system (Lisp_Object); |
383e0970 J |
704 | |
705 | extern int decode_coding_gap (struct coding_system *, | |
d311d28c | 706 | ptrdiff_t, ptrdiff_t); |
383e0970 | 707 | extern void decode_coding_object (struct coding_system *, |
d311d28c PE |
708 | Lisp_Object, ptrdiff_t, ptrdiff_t, |
709 | ptrdiff_t, ptrdiff_t, Lisp_Object); | |
383e0970 | 710 | extern void encode_coding_object (struct coding_system *, |
d311d28c PE |
711 | Lisp_Object, ptrdiff_t, ptrdiff_t, |
712 | ptrdiff_t, ptrdiff_t, Lisp_Object); | |
df7492f9 | 713 | |
933373ed KH |
714 | /* Macros for backward compatibility. */ |
715 | ||
df7492f9 KH |
716 | #define decode_coding_region(coding, from, to) \ |
717 | decode_coding_object (coding, Fcurrent_buffer (), \ | |
718 | from, CHAR_TO_BYTE (from), \ | |
719 | to, CHAR_TO_BYTE (to), Fcurrent_buffer ()) | |
720 | ||
721 | ||
722 | #define encode_coding_region(coding, from, to) \ | |
723 | encode_coding_object (coding, Fcurrent_buffer (), \ | |
724 | from, CHAR_TO_BYTE (from), \ | |
725 | to, CHAR_TO_BYTE (to), Fcurrent_buffer ()) | |
726 | ||
727 | ||
728 | #define decode_coding_string(coding, string, nocopy) \ | |
8890e5f5 | 729 | decode_coding_object (coding, string, 0, 0, SCHARS (string), \ |
f0bed503 | 730 | SBYTES (string), Qt) |
df7492f9 KH |
731 | |
732 | #define encode_coding_string(coding, string, nocopy) \ | |
729eadda EZ |
733 | (STRING_MULTIBYTE(string) ? \ |
734 | (encode_coding_object (coding, string, 0, 0, SCHARS (string), \ | |
735 | SBYTES (string), Qt), \ | |
736 | (coding)->dst_object) : (string)) | |
df7492f9 KH |
737 | |
738 | ||
739 | #define decode_coding_c_string(coding, src, bytes, dst_object) \ | |
740 | do { \ | |
741 | (coding)->source = (src); \ | |
742 | (coding)->src_chars = (coding)->src_bytes = (bytes); \ | |
743 | decode_coding_object ((coding), Qnil, 0, 0, (bytes), (bytes), \ | |
744 | (dst_object)); \ | |
745 | } while (0) | |
746 | ||
747 | ||
c532d349 | 748 | extern Lisp_Object preferred_coding_system (void); |
df7492f9 KH |
749 | |
750 | ||
8f924df7 KH |
751 | extern Lisp_Object Qutf_8, Qutf_8_emacs; |
752 | ||
955cbe7b | 753 | extern Lisp_Object Qcoding_category_index; |
df7492f9 KH |
754 | extern Lisp_Object Qcoding_system_p; |
755 | extern Lisp_Object Qraw_text, Qemacs_mule, Qno_conversion, Qundecided; | |
4ed46869 | 756 | extern Lisp_Object Qbuffer_file_coding_system; |
df7492f9 KH |
757 | |
758 | extern Lisp_Object Qunix, Qdos, Qmac; | |
4ed46869 | 759 | |
f967223b KH |
760 | extern Lisp_Object Qtranslation_table; |
761 | extern Lisp_Object Qtranslation_table_id; | |
ab45712c | 762 | |
4ed46869 KH |
763 | #ifdef emacs |
764 | extern Lisp_Object Qfile_coding_system; | |
387f6ba5 | 765 | extern Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 | 766 | extern Lisp_Object Qstart_process, Qopen_network_stream; |
d008a7cc | 767 | extern Lisp_Object Qwrite_region; |
4ed46869 | 768 | |
383e0970 | 769 | extern char *emacs_strerror (int); |
68c45bf0 | 770 | |
fbaa2ed9 KH |
771 | /* Coding system to be used to encode text for terminal display when |
772 | terminal coding system is nil. */ | |
773 | extern struct coding_system safe_terminal_coding; | |
774 | ||
4ed46869 KH |
775 | #endif |
776 | ||
d008a7cc GM |
777 | /* Error signaled when there's a problem with detecting coding system */ |
778 | extern Lisp_Object Qcoding_system_error; | |
779 | ||
df7492f9 | 780 | extern char emacs_mule_bytes[256]; |
383e0970 | 781 | extern int emacs_mule_string_char (unsigned char *); |
df7492f9 | 782 | |
6f776e81 | 783 | #endif /* EMACS_CODING_H */ |