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