1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2018 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
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
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 (at
16 your option) any later version.
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.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
26 /*** TABLE OF CONTENTS ***
30 2. Emacs' internal format (emacs-utf-8) handlers
33 5. Charset-base coding systems handlers
34 6. emacs-mule (old Emacs' internal format) handlers
36 8. Shift-JIS and BIG5 handlers
38 10. C library functions
39 11. Emacs Lisp library functions
44 /*** 0. General comments ***
49 A coding system is an object for an encoding mechanism that contains
50 information about how to convert byte sequences to character
51 sequences and vice versa. When we say "decode", it means converting
52 a byte sequence of a specific coding system into a character
53 sequence that is represented by Emacs' internal coding system
54 `emacs-utf-8', and when we say "encode", it means converting a
55 character sequence of emacs-utf-8 to a byte sequence of a specific
58 In Emacs Lisp, a coding system is represented by a Lisp symbol. On
59 the C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
71 o Charset-base coding system
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
77 o Old Emacs internal format (emacs-mule)
79 The coding system adopted by old versions of Emacs (20 and 21).
81 o ISO2022-base coding system
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
117 Like raw text, but don't do end-of-line conversion.
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. Classic Mac OS's format is usually one byte of
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
150 Return true if the byte sequence conforms to XXX.
152 Below is the template of these functions. */
156 detect_coding_XXX (struct coding_system
*coding
,
157 struct coding_detection_info
*detect_info
)
159 const unsigned char *src
= coding
->source
;
160 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
161 bool multibytep
= coding
->src_multibyte
;
162 ptrdiff_t consumed_chars
= 0;
168 /* Get one byte from the source. If the source is exhausted, jump
169 to no_more_source:. */
172 if (! __C_conforms_to_XXX___ (c
))
174 if (! __C_strongly_suggests_XXX__ (c
))
175 found
= CATEGORY_MASK_XXX
;
177 /* The byte sequence is invalid for XXX. */
178 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
182 /* The source exhausted successfully. */
183 detect_info
->found
|= found
;
188 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
190 These functions decode a byte sequence specified as a source by
191 CODING. The resulting multibyte text goes to a place pointed to by
192 CODING->charbuf, the length of which should not exceed
193 CODING->charbuf_size;
195 These functions set the information of original and decoded texts in
196 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
197 They also set CODING->result to one of CODING_RESULT_XXX indicating
198 how the decoding is finished.
200 Below is the template of these functions. */
204 decode_coding_XXXX (struct coding_system
*coding
)
206 const unsigned char *src
= coding
->source
+ coding
->consumed
;
207 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
208 /* SRC_BASE remembers the start position in source in each loop.
209 The loop will be exited when there's not enough source code, or
210 when there's no room in CHARBUF for a decoded character. */
211 const unsigned char *src_base
;
212 /* A buffer to produce decoded characters. */
213 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
214 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
215 bool multibytep
= coding
->src_multibyte
;
220 if (charbuf
< charbuf_end
)
221 /* No more room to produce a decoded character. */
228 if (src_base
< src_end
229 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
230 /* If the source ends by partial bytes to construct a character,
231 treat them as eight-bit raw data. */
232 while (src_base
< src_end
&& charbuf
< charbuf_end
)
233 *charbuf
++ = *src_base
++;
234 /* Remember how many bytes and characters we consumed. If the
235 source is multibyte, the bytes and chars are not identical. */
236 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
237 /* Remember how many characters we produced. */
238 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
242 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
244 These functions encode SRC_BYTES length text at SOURCE of Emacs'
245 internal multibyte format by CODING. The resulting byte sequence
246 goes to a place pointed to by DESTINATION, the length of which
247 should not exceed DST_BYTES.
249 These functions set the information of original and encoded texts in
250 the members produced, produced_char, consumed, and consumed_char of
251 the structure *CODING. They also set the member result to one of
252 CODING_RESULT_XXX indicating how the encoding finished.
254 DST_BYTES zero means that source area and destination area are
255 overlapped, which means that we can produce an encoded text until it
256 reaches at the head of not-yet-encoded source text.
258 Below is a template of these functions. */
261 encode_coding_XXX (struct coding_system
*coding
)
263 bool multibytep
= coding
->dst_multibyte
;
264 int *charbuf
= coding
->charbuf
;
265 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
266 unsigned char *dst
= coding
->destination
+ coding
->produced
;
267 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
268 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
269 ptrdiff_t produced_chars
= 0;
271 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
274 /* Encode C into DST, and increment DST. */
276 label_no_more_destination
:
277 /* How many chars and bytes we produced. */
278 coding
->produced_char
+= produced_chars
;
279 coding
->produced
= dst
- coding
->destination
;
284 /*** 1. Preamble ***/
291 #endif /* HAVE_WCHAR_H */
294 #include "character.h"
298 #include "composite.h"
300 #include "termhooks.h"
302 Lisp_Object Vcoding_system_hash_table
;
304 /* Format of end-of-line decided by system. This is Qunix on
305 Unix and Mac, Qdos on DOS/Windows.
306 This has an effect only for external encoding (i.e. for output to
307 file and process), not for in-buffer or Lisp string encoding. */
308 static Lisp_Object system_eol_type
;
312 /* Coding-systems are handed between Emacs Lisp programs and C internal
313 routines by the following three variables. */
314 /* Coding system to be used to encode text for terminal display when
315 terminal coding system is nil. */
316 struct coding_system safe_terminal_coding
;
320 /* Two special coding systems. */
321 static Lisp_Object Vsjis_coding_system
;
322 static Lisp_Object Vbig5_coding_system
;
324 /* ISO2022 section */
326 #define CODING_ISO_INITIAL(coding, reg) \
327 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
328 coding_attr_iso_initial), \
332 #define CODING_ISO_REQUEST(coding, charset_id) \
333 (((charset_id) <= (coding)->max_charset_id \
334 ? ((coding)->safe_charsets[charset_id] != 255 \
335 ? (coding)->safe_charsets[charset_id] \
340 #define CODING_ISO_FLAGS(coding) \
341 ((coding)->spec.iso_2022.flags)
342 #define CODING_ISO_DESIGNATION(coding, reg) \
343 ((coding)->spec.iso_2022.current_designation[reg])
344 #define CODING_ISO_INVOCATION(coding, plane) \
345 ((coding)->spec.iso_2022.current_invocation[plane])
346 #define CODING_ISO_SINGLE_SHIFTING(coding) \
347 ((coding)->spec.iso_2022.single_shifting)
348 #define CODING_ISO_BOL(coding) \
349 ((coding)->spec.iso_2022.bol)
350 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
351 (CODING_ISO_INVOCATION (coding, plane) < 0 ? -1 \
352 : CODING_ISO_DESIGNATION (coding, CODING_ISO_INVOCATION (coding, plane)))
353 #define CODING_ISO_CMP_STATUS(coding) \
354 (&(coding)->spec.iso_2022.cmp_status)
355 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
356 ((coding)->spec.iso_2022.ctext_extended_segment_len)
357 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
358 ((coding)->spec.iso_2022.embedded_utf_8)
360 /* Control characters of ISO2022. */
361 /* code */ /* function */
362 #define ISO_CODE_SO 0x0E /* shift-out */
363 #define ISO_CODE_SI 0x0F /* shift-in */
364 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
365 #define ISO_CODE_ESC 0x1B /* escape */
366 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
367 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
368 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
370 /* All code (1-byte) of ISO2022 is classified into one of the
372 enum iso_code_class_type
374 ISO_control_0
, /* Control codes in the range
375 0x00..0x1F and 0x7F, except for the
376 following 5 codes. */
377 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
378 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
379 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
380 ISO_escape
, /* ISO_CODE_ESC (0x1B) */
381 ISO_control_1
, /* Control codes in the range
382 0x80..0x9F, except for the
383 following 3 codes. */
384 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
385 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
386 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
387 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
388 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
389 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
390 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
393 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
394 `iso-flags' attribute of an iso2022 coding system. */
396 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
397 instead of the correct short-form sequence (e.g. ESC $ A). */
398 #define CODING_ISO_FLAG_LONG_FORM 0x0001
400 /* If set, reset graphic planes and registers at end-of-line to the
402 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
404 /* If set, reset graphic planes and registers before any control
405 characters to the initial state. */
406 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
408 /* If set, encode by 7-bit environment. */
409 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
411 /* If set, use locking-shift function. */
412 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
414 /* If set, use single-shift function. Overwrite
415 CODING_ISO_FLAG_LOCKING_SHIFT. */
416 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
418 /* If set, use designation escape sequence. */
419 #define CODING_ISO_FLAG_DESIGNATION 0x0040
421 /* If set, produce revision number sequence. */
422 #define CODING_ISO_FLAG_REVISION 0x0080
424 /* If set, produce ISO6429's direction specifying sequence. */
425 #define CODING_ISO_FLAG_DIRECTION 0x0100
427 /* If set, assume designation states are reset at beginning of line on
429 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
431 /* If set, designation sequence should be placed at beginning of line
433 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
435 /* If set, do not encode unsafe characters on output. */
436 #define CODING_ISO_FLAG_SAFE 0x0800
438 /* If set, extra latin codes (128..159) are accepted as a valid code
440 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
442 #define CODING_ISO_FLAG_COMPOSITION 0x2000
444 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
446 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
448 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
450 #define CODING_ISO_FLAG_LEVEL_4 0x20000
452 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
454 /* A character to be produced on output if encoding of the original
455 character is prohibited by CODING_ISO_FLAG_SAFE. */
456 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
459 #define CODING_UTF_8_BOM(coding) \
460 ((coding)->spec.utf_8_bom)
463 #define CODING_UTF_16_BOM(coding) \
464 ((coding)->spec.utf_16.bom)
466 #define CODING_UTF_16_ENDIAN(coding) \
467 ((coding)->spec.utf_16.endian)
469 #define CODING_UTF_16_SURROGATE(coding) \
470 ((coding)->spec.utf_16.surrogate)
474 #define CODING_CCL_DECODER(coding) \
475 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
476 #define CODING_CCL_ENCODER(coding) \
477 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
478 #define CODING_CCL_VALIDS(coding) \
479 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
481 /* Index for each coding category in `coding_categories' */
485 coding_category_iso_7
,
486 coding_category_iso_7_tight
,
487 coding_category_iso_8_1
,
488 coding_category_iso_8_2
,
489 coding_category_iso_7_else
,
490 coding_category_iso_8_else
,
491 coding_category_utf_8_auto
,
492 coding_category_utf_8_nosig
,
493 coding_category_utf_8_sig
,
494 coding_category_utf_16_auto
,
495 coding_category_utf_16_be
,
496 coding_category_utf_16_le
,
497 coding_category_utf_16_be_nosig
,
498 coding_category_utf_16_le_nosig
,
499 coding_category_charset
,
500 coding_category_sjis
,
501 coding_category_big5
,
503 coding_category_emacs_mule
,
504 /* All above are targets of code detection. */
505 coding_category_raw_text
,
506 coding_category_undecided
,
510 /* Definitions of flag bits used in detect_coding_XXXX. */
511 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
512 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
513 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
514 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
515 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
516 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
517 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
518 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
519 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
520 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
521 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
522 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
523 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
524 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
525 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
526 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
527 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
528 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
529 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
530 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
532 /* This value is returned if detect_coding_mask () find nothing other
533 than ASCII characters. */
534 #define CATEGORY_MASK_ANY \
535 (CATEGORY_MASK_ISO_7 \
536 | CATEGORY_MASK_ISO_7_TIGHT \
537 | CATEGORY_MASK_ISO_8_1 \
538 | CATEGORY_MASK_ISO_8_2 \
539 | CATEGORY_MASK_ISO_7_ELSE \
540 | CATEGORY_MASK_ISO_8_ELSE \
541 | CATEGORY_MASK_UTF_8_AUTO \
542 | CATEGORY_MASK_UTF_8_NOSIG \
543 | CATEGORY_MASK_UTF_8_SIG \
544 | CATEGORY_MASK_UTF_16_AUTO \
545 | CATEGORY_MASK_UTF_16_BE \
546 | CATEGORY_MASK_UTF_16_LE \
547 | CATEGORY_MASK_UTF_16_BE_NOSIG \
548 | CATEGORY_MASK_UTF_16_LE_NOSIG \
549 | CATEGORY_MASK_CHARSET \
550 | CATEGORY_MASK_SJIS \
551 | CATEGORY_MASK_BIG5 \
552 | CATEGORY_MASK_CCL \
553 | CATEGORY_MASK_EMACS_MULE)
556 #define CATEGORY_MASK_ISO_7BIT \
557 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
559 #define CATEGORY_MASK_ISO_8BIT \
560 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
562 #define CATEGORY_MASK_ISO_ELSE \
563 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
565 #define CATEGORY_MASK_ISO_ESCAPE \
566 (CATEGORY_MASK_ISO_7 \
567 | CATEGORY_MASK_ISO_7_TIGHT \
568 | CATEGORY_MASK_ISO_7_ELSE \
569 | CATEGORY_MASK_ISO_8_ELSE)
571 #define CATEGORY_MASK_ISO \
572 ( CATEGORY_MASK_ISO_7BIT \
573 | CATEGORY_MASK_ISO_8BIT \
574 | CATEGORY_MASK_ISO_ELSE)
576 #define CATEGORY_MASK_UTF_16 \
577 (CATEGORY_MASK_UTF_16_AUTO \
578 | CATEGORY_MASK_UTF_16_BE \
579 | CATEGORY_MASK_UTF_16_LE \
580 | CATEGORY_MASK_UTF_16_BE_NOSIG \
581 | CATEGORY_MASK_UTF_16_LE_NOSIG)
583 #define CATEGORY_MASK_UTF_8 \
584 (CATEGORY_MASK_UTF_8_AUTO \
585 | CATEGORY_MASK_UTF_8_NOSIG \
586 | CATEGORY_MASK_UTF_8_SIG)
588 /* Table of coding categories (Lisp symbols). This variable is for
589 internal use only. */
590 static Lisp_Object Vcoding_category_table
;
592 /* Table of coding-categories ordered by priority. */
593 static enum coding_category coding_priorities
[coding_category_max
];
595 /* Nth element is a coding context for the coding system bound to the
596 Nth coding category. */
597 static struct coding_system coding_categories
[coding_category_max
];
599 /* Encode a flag that can be nil, something else, or t as -1, 0, 1. */
602 encode_inhibit_flag (Lisp_Object flag
)
604 return NILP (flag
) ? -1 : EQ (flag
, Qt
);
607 /* True if the value of ENCODED_FLAG says a flag should be treated as set.
608 1 means yes, -1 means no, 0 means ask the user variable VAR. */
611 inhibit_flag (int encoded_flag
, bool var
)
613 return 0 < encoded_flag
+ var
;
616 #define CODING_GET_INFO(coding, attrs, charset_list) \
618 (attrs) = CODING_ID_ATTRS ((coding)->id); \
619 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
623 CHECK_NATNUM_CAR (Lisp_Object x
)
625 Lisp_Object tmp
= XCAR (x
);
631 CHECK_NATNUM_CDR (Lisp_Object x
)
633 Lisp_Object tmp
= XCDR (x
);
638 /* True if CODING's destination can be grown. */
641 growable_destination (struct coding_system
*coding
)
643 return STRINGP (coding
->dst_object
) || BUFFERP (coding
->dst_object
);
647 /* Safely get one byte from the source text pointed by SRC which ends
648 at SRC_END, and set C to that byte. If there are not enough bytes
649 in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
650 and a multibyte character is found at SRC, set C to the
651 negative value of the character code. The caller should declare
652 and set these variables appropriately in advance:
653 src, src_end, multibytep */
655 #define ONE_MORE_BYTE(c) \
657 if (src == src_end) \
659 if (src_base < src) \
660 record_conversion_result \
661 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
662 goto no_more_source; \
665 if (multibytep && (c & 0x80)) \
667 if ((c & 0xFE) == 0xC0) \
668 c = ((c & 1) << 6) | *src++; \
672 c = - string_char (src, &src, NULL); \
673 record_conversion_result \
674 (coding, CODING_RESULT_INVALID_SRC); \
680 /* Safely get two bytes from the source text pointed by SRC which ends
681 at SRC_END, and set C1 and C2 to those bytes while skipping the
682 heading multibyte characters. If there are not enough bytes in the
683 source, it jumps to 'no_more_source'. If MULTIBYTEP and
684 a multibyte character is found for C2, set C2 to the negative value
685 of the character code. The caller should declare and set these
686 variables appropriately in advance:
687 src, src_end, multibytep
688 It is intended that this macro is used in detect_coding_utf_16. */
690 #define TWO_MORE_BYTES(c1, c2) \
693 if (src == src_end) \
694 goto no_more_source; \
696 if (multibytep && (c1 & 0x80)) \
698 if ((c1 & 0xFE) == 0xC0) \
699 c1 = ((c1 & 1) << 6) | *src++; \
702 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
707 if (src == src_end) \
708 goto no_more_source; \
710 if (multibytep && (c2 & 0x80)) \
712 if ((c2 & 0xFE) == 0xC0) \
713 c2 = ((c2 & 1) << 6) | *src++; \
720 /* Store a byte C in the place pointed by DST and increment DST to the
721 next free point, and increment PRODUCED_CHARS. The caller should
722 assure that C is 0..127, and declare and set the variable `dst'
723 appropriately in advance.
727 #define EMIT_ONE_ASCII_BYTE(c) \
734 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
736 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
738 produced_chars += 2; \
739 *dst++ = (c1), *dst++ = (c2); \
743 /* Store a byte C in the place pointed by DST and increment DST to the
744 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
745 store in an appropriate multibyte form. The caller should
746 declare and set the variables `dst' and `multibytep' appropriately
749 #define EMIT_ONE_BYTE(c) \
756 ch = BYTE8_TO_CHAR (ch); \
757 CHAR_STRING_ADVANCE (ch, dst); \
764 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
766 #define EMIT_TWO_BYTES(c1, c2) \
768 produced_chars += 2; \
775 ch = BYTE8_TO_CHAR (ch); \
776 CHAR_STRING_ADVANCE (ch, dst); \
779 ch = BYTE8_TO_CHAR (ch); \
780 CHAR_STRING_ADVANCE (ch, dst); \
790 #define EMIT_THREE_BYTES(c1, c2, c3) \
792 EMIT_ONE_BYTE (c1); \
793 EMIT_TWO_BYTES (c2, c3); \
797 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
799 EMIT_TWO_BYTES (c1, c2); \
800 EMIT_TWO_BYTES (c3, c4); \
805 record_conversion_result (struct coding_system
*coding
,
806 enum coding_result_code result
)
808 coding
->result
= result
;
811 case CODING_RESULT_INSUFFICIENT_SRC
:
812 Vlast_code_conversion_error
= Qinsufficient_source
;
814 case CODING_RESULT_INVALID_SRC
:
815 Vlast_code_conversion_error
= Qinvalid_source
;
817 case CODING_RESULT_INTERRUPT
:
818 Vlast_code_conversion_error
= Qinterrupted
;
820 case CODING_RESULT_INSUFFICIENT_DST
:
821 /* Don't record this error in Vlast_code_conversion_error
822 because it happens just temporarily and is resolved when the
823 whole conversion is finished. */
825 case CODING_RESULT_SUCCESS
:
828 Vlast_code_conversion_error
= intern ("Unknown error");
832 /* These wrapper macros are used to preserve validity of pointers into
833 buffer text across calls to decode_char, encode_char, etc, which
834 could cause relocation of buffers if it loads a charset map,
835 because loading a charset map allocates large structures. */
837 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
841 charset_map_loaded = 0; \
842 c = DECODE_CHAR (charset, code); \
843 if (charset_map_loaded \
844 && (offset = coding_change_source (coding))) \
847 src_base += offset; \
852 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
856 charset_map_loaded = 0; \
857 code = ENCODE_CHAR (charset, c); \
858 if (charset_map_loaded \
859 && (offset = coding_change_destination (coding))) \
866 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
870 charset_map_loaded = 0; \
871 charset = char_charset (c, charset_list, code_return); \
872 if (charset_map_loaded \
873 && (offset = coding_change_destination (coding))) \
880 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
884 charset_map_loaded = 0; \
885 result = CHAR_CHARSET_P (c, charset); \
886 if (charset_map_loaded \
887 && (offset = coding_change_destination (coding))) \
895 /* If there are at least BYTES length of room at dst, allocate memory
896 for coding->destination and update dst and dst_end. We don't have
897 to take care of coding->source which will be relocated. It is
898 handled by calling coding_set_source in encode_coding. */
900 #define ASSURE_DESTINATION(bytes) \
902 if (dst + (bytes) >= dst_end) \
904 ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
906 dst = alloc_destination (coding, more_bytes, dst); \
907 dst_end = coding->destination + coding->dst_bytes; \
912 /* Store multibyte form of the character C in P, and advance P to the
913 end of the multibyte form. This used to be like CHAR_STRING_ADVANCE
914 without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call
915 MAYBE_UNIFY_CHAR in CHAR_STRING_ADVANCE. */
917 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) CHAR_STRING_ADVANCE(c, p)
919 /* Return the character code of character whose multibyte form is at
920 P, and advance P to the end of the multibyte form. This used to be
921 like STRING_CHAR_ADVANCE without ever calling MAYBE_UNIFY_CHAR, but
922 nowadays STRING_CHAR_ADVANCE doesn't call MAYBE_UNIFY_CHAR. */
924 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) STRING_CHAR_ADVANCE(p)
926 /* Set coding->source from coding->src_object. */
929 coding_set_source (struct coding_system
*coding
)
931 if (BUFFERP (coding
->src_object
))
933 struct buffer
*buf
= XBUFFER (coding
->src_object
);
935 if (coding
->src_pos
< 0)
936 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
938 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
940 else if (STRINGP (coding
->src_object
))
942 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
946 /* Otherwise, the source is C string and is never relocated
947 automatically. Thus we don't have to update anything. */
952 /* Set coding->source from coding->src_object, and return how many
953 bytes coding->source was changed. */
956 coding_change_source (struct coding_system
*coding
)
958 const unsigned char *orig
= coding
->source
;
959 coding_set_source (coding
);
960 return coding
->source
- orig
;
964 /* Set coding->destination from coding->dst_object. */
967 coding_set_destination (struct coding_system
*coding
)
969 if (BUFFERP (coding
->dst_object
))
971 if (BUFFERP (coding
->src_object
) && coding
->src_pos
< 0)
973 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
974 coding
->dst_bytes
= (GAP_END_ADDR
975 - (coding
->src_bytes
- coding
->consumed
)
976 - coding
->destination
);
980 /* We are sure that coding->dst_pos_byte is before the gap
982 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
983 + coding
->dst_pos_byte
- BEG_BYTE
);
984 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
985 - coding
->destination
);
990 /* Otherwise, the destination is C string and is never relocated
991 automatically. Thus we don't have to update anything. */
996 /* Set coding->destination from coding->dst_object, and return how
997 many bytes coding->destination was changed. */
1000 coding_change_destination (struct coding_system
*coding
)
1002 const unsigned char *orig
= coding
->destination
;
1003 coding_set_destination (coding
);
1004 return coding
->destination
- orig
;
1009 coding_alloc_by_realloc (struct coding_system
*coding
, ptrdiff_t bytes
)
1012 if (INT_ADD_WRAPV (coding
->dst_bytes
, bytes
, &newbytes
)
1013 || SIZE_MAX
< newbytes
)
1015 coding
->destination
= xrealloc (coding
->destination
, newbytes
);
1016 coding
->dst_bytes
= newbytes
;
1020 coding_alloc_by_making_gap (struct coding_system
*coding
,
1021 ptrdiff_t gap_head_used
, ptrdiff_t bytes
)
1023 if (EQ (coding
->src_object
, coding
->dst_object
))
1025 /* The gap may contain the produced data at the head and not-yet
1026 consumed data at the tail. To preserve those data, we at
1027 first make the gap size to zero, then increase the gap
1029 ptrdiff_t add
= GAP_SIZE
;
1031 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1032 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1034 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1035 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1038 make_gap_1 (XBUFFER (coding
->dst_object
), bytes
);
1042 static unsigned char *
1043 alloc_destination (struct coding_system
*coding
, ptrdiff_t nbytes
,
1046 ptrdiff_t offset
= dst
- coding
->destination
;
1048 if (BUFFERP (coding
->dst_object
))
1050 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1052 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1055 coding_alloc_by_realloc (coding
, nbytes
);
1056 coding_set_destination (coding
);
1057 dst
= coding
->destination
+ offset
;
1061 /** Macros for annotations. */
1063 /* An annotation data is stored in the array coding->charbuf in this
1065 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1066 LENGTH is the number of elements in the annotation.
1067 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1068 NCHARS is the number of characters in the text annotated.
1070 The format of the following elements depend on ANNOTATION_MASK.
1072 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1074 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1076 NBYTES is the number of bytes specified in the header part of
1077 old-style emacs-mule encoding, or 0 for the other kind of
1080 METHOD is one of enum composition_method.
1082 Optional COMPOSITION-COMPONENTS are characters and composition
1085 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1088 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1089 recover from an invalid annotation, and should be skipped by
1090 produce_annotation. */
1092 /* Maximum length of the header of annotation data. */
1093 #define MAX_ANNOTATION_LENGTH 5
1095 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1097 *(buf)++ = -(len); \
1098 *(buf)++ = (mask); \
1099 *(buf)++ = (nchars); \
1100 coding->annotated = 1; \
1103 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1105 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1111 #define ADD_CHARSET_DATA(buf, nchars, id) \
1113 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1118 /* Bitmasks for coding->eol_seen. */
1120 #define EOL_SEEN_NONE 0
1121 #define EOL_SEEN_LF 1
1122 #define EOL_SEEN_CR 2
1123 #define EOL_SEEN_CRLF 4
1126 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1133 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1134 Return true if a text is encoded in UTF-8. */
1136 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1137 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1138 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1139 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1140 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1141 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1143 #define UTF_8_BOM_1 0xEF
1144 #define UTF_8_BOM_2 0xBB
1145 #define UTF_8_BOM_3 0xBF
1147 /* Unlike the other detect_coding_XXX, this function counts the number
1148 of characters and checks the EOL format. */
1151 detect_coding_utf_8 (struct coding_system
*coding
,
1152 struct coding_detection_info
*detect_info
)
1154 const unsigned char *src
= coding
->source
, *src_base
;
1155 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1156 bool multibytep
= coding
->src_multibyte
;
1157 ptrdiff_t consumed_chars
= 0;
1159 ptrdiff_t nchars
= coding
->head_ascii
;
1160 int eol_seen
= coding
->eol_seen
;
1162 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1163 /* A coding system of this category is always ASCII compatible. */
1166 if (src
== coding
->source
/* BOM should be at the head. */
1167 && src
+ 3 < src_end
/* BOM is 3-byte long. */
1168 && src
[0] == UTF_8_BOM_1
1169 && src
[1] == UTF_8_BOM_2
1170 && src
[2] == UTF_8_BOM_3
)
1179 int c
, c1
, c2
, c3
, c4
;
1183 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1188 if (src
< src_end
&& *src
== '\n')
1190 eol_seen
|= EOL_SEEN_CRLF
;
1195 eol_seen
|= EOL_SEEN_CR
;
1198 eol_seen
|= EOL_SEEN_LF
;
1202 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1204 if (UTF_8_2_OCTET_LEADING_P (c
))
1210 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1212 if (UTF_8_3_OCTET_LEADING_P (c
))
1218 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1220 if (UTF_8_4_OCTET_LEADING_P (c
))
1226 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1228 if (UTF_8_5_OCTET_LEADING_P (c
))
1235 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1239 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1241 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1246 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1247 detect_info
->found
|= CATEGORY_MASK_UTF_8_AUTO
| CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1251 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1252 if (nchars
< src_end
- coding
->source
)
1253 /* The found characters are less than source bytes, which
1254 means that we found a valid non-ASCII characters. */
1255 detect_info
->found
|= CATEGORY_MASK_UTF_8_AUTO
| CATEGORY_MASK_UTF_8_NOSIG
;
1257 coding
->detected_utf8_bytes
= src_base
- coding
->source
;
1258 coding
->detected_utf8_chars
= nchars
;
1264 decode_coding_utf_8 (struct coding_system
*coding
)
1266 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1267 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1268 const unsigned char *src_base
;
1269 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1270 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1271 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1272 bool multibytep
= coding
->src_multibyte
;
1273 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1275 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1276 int byte_after_cr
= -1;
1278 if (bom
!= utf_without_bom
)
1284 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1289 if (! UTF_8_EXTRA_OCTET_P (c2
))
1294 if (! UTF_8_EXTRA_OCTET_P (c3
))
1298 if ((c1
!= UTF_8_BOM_1
)
1299 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1302 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1307 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1311 int c
, c1
, c2
, c3
, c4
, c5
;
1314 consumed_chars_base
= consumed_chars
;
1316 if (charbuf
>= charbuf_end
)
1318 if (byte_after_cr
>= 0)
1323 /* In the simple case, rapidly handle ordinary characters */
1324 if (multibytep
&& ! eol_dos
1325 && charbuf
< charbuf_end
- 6 && src
< src_end
- 6)
1327 while (charbuf
< charbuf_end
- 6 && src
< src_end
- 6)
1357 /* If we handled at least one character, restart the main loop. */
1358 if (src
!= src_base
)
1362 if (byte_after_cr
>= 0)
1363 c1
= byte_after_cr
, byte_after_cr
= -1;
1370 else if (UTF_8_1_OCTET_P (c1
))
1372 if (eol_dos
&& c1
== '\r')
1373 ONE_MORE_BYTE (byte_after_cr
);
1379 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1381 if (UTF_8_2_OCTET_LEADING_P (c1
))
1383 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1384 /* Reject overlong sequences here and below. Encoders
1385 producing them are incorrect, they can be misleading,
1386 and they mess up read/write invariance. */
1393 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1395 if (UTF_8_3_OCTET_LEADING_P (c1
))
1397 c
= (((c1
& 0xF) << 12)
1398 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1400 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1406 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1408 if (UTF_8_4_OCTET_LEADING_P (c1
))
1410 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1411 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1418 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1420 if (UTF_8_5_OCTET_LEADING_P (c1
))
1422 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1423 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1425 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1440 consumed_chars
= consumed_chars_base
;
1442 *charbuf
++ = ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1446 coding
->consumed_char
+= consumed_chars_base
;
1447 coding
->consumed
= src_base
- coding
->source
;
1448 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1453 encode_coding_utf_8 (struct coding_system
*coding
)
1455 bool multibytep
= coding
->dst_multibyte
;
1456 int *charbuf
= coding
->charbuf
;
1457 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1458 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1459 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1460 ptrdiff_t produced_chars
= 0;
1463 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1465 ASSURE_DESTINATION (3);
1466 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1467 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1472 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1474 while (charbuf
< charbuf_end
)
1476 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1478 ASSURE_DESTINATION (safe_room
);
1480 if (CHAR_BYTE8_P (c
))
1482 c
= CHAR_TO_BYTE8 (c
);
1487 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1488 for (p
= str
; p
< pend
; p
++)
1495 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1497 while (charbuf
< charbuf_end
)
1499 ASSURE_DESTINATION (safe_room
);
1501 if (CHAR_BYTE8_P (c
))
1502 *dst
++ = CHAR_TO_BYTE8 (c
);
1504 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1506 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
1508 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1509 coding
->produced_char
+= produced_chars
;
1510 coding
->produced
= dst
- coding
->destination
;
1515 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1516 Return true if a text is encoded in one of UTF-16 based coding systems. */
1518 #define UTF_16_HIGH_SURROGATE_P(val) \
1519 (((val) & 0xFC00) == 0xD800)
1521 #define UTF_16_LOW_SURROGATE_P(val) \
1522 (((val) & 0xFC00) == 0xDC00)
1526 detect_coding_utf_16 (struct coding_system
*coding
,
1527 struct coding_detection_info
*detect_info
)
1529 const unsigned char *src
= coding
->source
;
1530 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1531 bool multibytep
= coding
->src_multibyte
;
1534 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1535 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1536 && (coding
->src_chars
& 1))
1538 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1542 TWO_MORE_BYTES (c1
, c2
);
1543 if ((c1
== 0xFF) && (c2
== 0xFE))
1545 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1546 | CATEGORY_MASK_UTF_16_AUTO
);
1547 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1548 | CATEGORY_MASK_UTF_16_BE_NOSIG
1549 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1551 else if ((c1
== 0xFE) && (c2
== 0xFF))
1553 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1554 | CATEGORY_MASK_UTF_16_AUTO
);
1555 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1556 | CATEGORY_MASK_UTF_16_BE_NOSIG
1557 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1561 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1566 /* We check the dispersion of Eth and Oth bytes where E is even and
1567 O is odd. If both are high, we assume binary data.*/
1568 unsigned char e
[256], o
[256];
1569 unsigned e_num
= 1, o_num
= 1;
1576 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1577 |CATEGORY_MASK_UTF_16_BE
1578 | CATEGORY_MASK_UTF_16_LE
);
1580 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1581 != CATEGORY_MASK_UTF_16
)
1583 TWO_MORE_BYTES (c1
, c2
);
1591 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1598 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1609 decode_coding_utf_16 (struct coding_system
*coding
)
1611 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1612 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1613 const unsigned char *src_base
;
1614 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1615 /* We may produces at most 3 chars in one loop. */
1616 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1617 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1618 bool multibytep
= coding
->src_multibyte
;
1619 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1620 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1621 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1623 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1624 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1626 if (bom
== utf_with_bom
)
1635 if (endian
== utf_16_big_endian
1636 ? c
!= 0xFEFF : c
!= 0xFFFE)
1638 /* The first two bytes are not BOM. Treat them as bytes
1639 for a normal character. */
1642 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1644 else if (bom
== utf_detect_bom
)
1646 /* We have already tried to detect BOM and failed in
1648 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1656 consumed_chars_base
= consumed_chars
;
1658 if (charbuf
>= charbuf_end
)
1660 if (byte_after_cr1
>= 0)
1665 if (byte_after_cr1
>= 0)
1666 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1674 if (byte_after_cr2
>= 0)
1675 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1680 *charbuf
++ = ASCII_CHAR_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1684 c
= (endian
== utf_16_big_endian
1685 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1689 if (! UTF_16_LOW_SURROGATE_P (c
))
1691 if (endian
== utf_16_big_endian
)
1692 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1694 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1697 if (UTF_16_HIGH_SURROGATE_P (c
))
1698 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1704 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1705 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1706 *charbuf
++ = 0x10000 + c
;
1711 if (UTF_16_HIGH_SURROGATE_P (c
))
1712 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1715 if (eol_dos
&& c
== '\r')
1717 ONE_MORE_BYTE (byte_after_cr1
);
1718 ONE_MORE_BYTE (byte_after_cr2
);
1726 coding
->consumed_char
+= consumed_chars_base
;
1727 coding
->consumed
= src_base
- coding
->source
;
1728 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1732 encode_coding_utf_16 (struct coding_system
*coding
)
1734 bool multibytep
= coding
->dst_multibyte
;
1735 int *charbuf
= coding
->charbuf
;
1736 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1737 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1738 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1740 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1741 bool big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1742 ptrdiff_t produced_chars
= 0;
1745 if (bom
!= utf_without_bom
)
1747 ASSURE_DESTINATION (safe_room
);
1749 EMIT_TWO_BYTES (0xFE, 0xFF);
1751 EMIT_TWO_BYTES (0xFF, 0xFE);
1752 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1755 while (charbuf
< charbuf_end
)
1757 ASSURE_DESTINATION (safe_room
);
1759 if (c
> MAX_UNICODE_CHAR
)
1760 c
= coding
->default_char
;
1765 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1767 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1774 c1
= (c
>> 10) + 0xD800;
1775 c2
= (c
& 0x3FF) + 0xDC00;
1777 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1779 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1782 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1783 coding
->produced
= dst
- coding
->destination
;
1784 coding
->produced_char
+= produced_chars
;
1789 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1791 /* Emacs' internal format for representation of multiple character
1792 sets is a kind of multi-byte encoding, i.e. characters are
1793 represented by variable-length sequences of one-byte codes.
1795 ASCII characters and control characters (e.g. `tab', `newline') are
1796 represented by one-byte sequences which are their ASCII codes, in
1797 the range 0x00 through 0x7F.
1799 8-bit characters of the range 0x80..0x9F are represented by
1800 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1803 8-bit characters of the range 0xA0..0xFF are represented by
1804 one-byte sequences which are their 8-bit code.
1806 The other characters are represented by a sequence of `base
1807 leading-code', optional `extended leading-code', and one or two
1808 `position-code's. The length of the sequence is determined by the
1809 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1810 whereas extended leading-code and position-code take the range 0xA0
1811 through 0xFF. See `charset.h' for more details about leading-code
1814 --- CODE RANGE of Emacs' internal format ---
1818 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1819 eight-bit-graphic 0xA0..0xBF
1820 ELSE 0x81..0x9D + [0xA0..0xFF]+
1821 ---------------------------------------------
1823 As this is the internal character representation, the format is
1824 usually not used externally (i.e. in a file or in a data sent to a
1825 process). But, it is possible to have a text externally in this
1826 format (i.e. by encoding by the coding system `emacs-mule').
1828 In that case, a sequence of one-byte codes has a slightly different
1831 At first, all characters in eight-bit-control are represented by
1832 one-byte sequences which are their 8-bit code.
1834 Next, character composition data are represented by the byte
1835 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1837 METHOD is 0xF2 plus one of composition method (enum
1838 composition_method),
1840 BYTES is 0xA0 plus a byte length of this composition data,
1842 CHARS is 0xA0 plus a number of characters composed by this
1845 COMPONENTs are characters of multibyte form or composition
1846 rules encoded by two-byte of ASCII codes.
1848 In addition, for backward compatibility, the following formats are
1849 also recognized as composition data on decoding.
1852 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1855 MSEQ is a multibyte form but in these special format:
1856 ASCII: 0xA0 ASCII_CODE+0x80,
1857 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1858 RULE is a one byte code of the range 0xA0..0xF0 that
1859 represents a composition rule.
1862 char emacs_mule_bytes
[256];
1865 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1866 Return true if a text is encoded in 'emacs-mule'. */
1869 detect_coding_emacs_mule (struct coding_system
*coding
,
1870 struct coding_detection_info
*detect_info
)
1872 const unsigned char *src
= coding
->source
, *src_base
;
1873 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1874 bool multibytep
= coding
->src_multibyte
;
1875 ptrdiff_t consumed_chars
= 0;
1879 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1880 /* A coding system of this category is always ASCII compatible. */
1881 src
+= coding
->head_ascii
;
1891 /* Perhaps the start of composite character. We simply skip
1892 it because analyzing it is too heavy for detecting. But,
1893 at least, we check that the composite character
1894 constitutes of more than 4 bytes. */
1895 const unsigned char *src_start
;
1905 if (src
- src_start
<= 4)
1907 found
= CATEGORY_MASK_EMACS_MULE
;
1915 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1920 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1922 while (more_bytes
> 0)
1927 src
--; /* Unread the last byte. */
1932 if (more_bytes
!= 0)
1934 found
= CATEGORY_MASK_EMACS_MULE
;
1937 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1941 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1943 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1946 detect_info
->found
|= found
;
1951 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1952 character. If CMP_STATUS indicates that we must expect MSEQ or
1953 RULE described above, decode it and return the negative value of
1954 the decoded character or rule. If an invalid byte is found, return
1955 -1. If SRC is too short, return -2. */
1958 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1959 int *nbytes
, int *nchars
, int *id
,
1960 struct composition_status
*cmp_status
)
1962 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1963 const unsigned char *src_base
= src
;
1964 bool multibytep
= coding
->src_multibyte
;
1968 ptrdiff_t consumed_chars
= 0;
1969 bool mseq_found
= 0;
1975 charset_ID
= emacs_mule_charset
[0];
1981 if (cmp_status
->state
!= COMPOSING_NO
1982 && cmp_status
->old_form
)
1984 if (cmp_status
->state
== COMPOSING_CHAR
)
1999 *nbytes
= src
- src_base
;
2000 *nchars
= consumed_chars
;
2008 switch (emacs_mule_bytes
[c
])
2011 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2020 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2021 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2024 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2033 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2038 code
= (c
& 0x7F) << 8;
2048 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2053 code
= (c
& 0x7F) << 8;
2062 charset_ID
= ASCII_CHAR_P (code
) ? charset_ascii
: charset_eight_bit
;
2068 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2069 CHARSET_FROM_ID (charset_ID
), code
, c
);
2073 *nbytes
= src
- src_base
;
2074 *nchars
= consumed_chars
;
2077 return (mseq_found
? -c
: c
);
2087 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2089 /* Handle these composition sequence ('|': the end of header elements,
2090 BYTES and CHARS >= 0xA0):
2092 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2093 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2094 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2098 (4) relative composition: 0x80 | MSEQ ... MSEQ
2099 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2101 When the starter 0x80 and the following header elements are found,
2102 this annotation header is produced.
2104 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2106 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2107 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2109 Then, upon reading the following elements, these codes are produced
2110 until the composition end is found:
2113 (2) ALT ... ALT CHAR ... CHAR
2114 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2116 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2118 When the composition end is found, LENGTH and NCHARS in the
2119 annotation header is updated as below:
2121 (1) LENGTH: unchanged, NCHARS: unchanged
2122 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2123 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2124 (4) LENGTH: unchanged, NCHARS: number of CHARs
2125 (5) LENGTH: unchanged, NCHARS: number of CHARs
2127 If an error is found while composing, the annotation header is
2128 changed to the original composition header (plus filler -1s) as
2131 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2132 (5) [ 0x80 0xFF -1 -1- -1 ]
2134 and the sequence [ -2 DECODED-RULE ] is changed to the original
2135 byte sequence as below:
2136 o the original byte sequence is B: [ B -1 ]
2137 o the original byte sequence is B1 B2: [ B1 B2 ]
2139 Most of the routines are implemented by macros because many
2140 variables and labels in the caller decode_coding_emacs_mule must be
2141 accessible, and they are usually called just once (thus doesn't
2142 increase the size of compiled object). */
2144 /* Decode a composition rule represented by C as a component of
2145 composition sequence of Emacs 20 style. Set RULE to the decoded
2148 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2153 if (c < 0 || c >= 81) \
2154 goto invalid_code; \
2155 gref = c / 9, nref = c % 9; \
2156 if (gref == 4) gref = 10; \
2157 if (nref == 4) nref = 10; \
2158 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2162 /* Decode a composition rule represented by C and the following byte
2163 at SRC as a component of composition sequence of Emacs 21 style.
2164 Set RULE to the decoded rule. */
2166 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2171 if (gref < 0 || gref >= 81) \
2172 goto invalid_code; \
2173 ONE_MORE_BYTE (c); \
2175 if (nref < 0 || nref >= 81) \
2176 goto invalid_code; \
2177 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2181 /* Start of Emacs 21 style format. The first three bytes at SRC are
2182 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2183 byte length of this composition information, CHARS is the number of
2184 characters composed by this composition. */
2186 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2188 enum composition_method method = c - 0xF2; \
2189 int nbytes, nchars; \
2191 ONE_MORE_BYTE (c); \
2193 goto invalid_code; \
2194 nbytes = c - 0xA0; \
2195 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2196 goto invalid_code; \
2197 ONE_MORE_BYTE (c); \
2198 nchars = c - 0xA0; \
2199 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2200 goto invalid_code; \
2201 cmp_status->old_form = 0; \
2202 cmp_status->method = method; \
2203 if (method == COMPOSITION_RELATIVE) \
2204 cmp_status->state = COMPOSING_CHAR; \
2206 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2207 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2208 cmp_status->nchars = nchars; \
2209 cmp_status->ncomps = nbytes - 4; \
2210 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2214 /* Start of Emacs 20 style format for relative composition. */
2216 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2218 cmp_status->old_form = 1; \
2219 cmp_status->method = COMPOSITION_RELATIVE; \
2220 cmp_status->state = COMPOSING_CHAR; \
2221 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2222 cmp_status->nchars = cmp_status->ncomps = 0; \
2223 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2227 /* Start of Emacs 20 style format for rule-base composition. */
2229 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2231 cmp_status->old_form = 1; \
2232 cmp_status->method = COMPOSITION_WITH_RULE; \
2233 cmp_status->state = COMPOSING_CHAR; \
2234 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2235 cmp_status->nchars = cmp_status->ncomps = 0; \
2236 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2240 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2242 const unsigned char *current_src = src; \
2244 ONE_MORE_BYTE (c); \
2246 goto invalid_code; \
2247 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2248 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2249 DECODE_EMACS_MULE_21_COMPOSITION (); \
2250 else if (c < 0xA0) \
2251 goto invalid_code; \
2252 else if (c < 0xC0) \
2254 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2255 /* Re-read C as a composition component. */ \
2256 src = current_src; \
2258 else if (c == 0xFF) \
2259 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2261 goto invalid_code; \
2264 #define EMACS_MULE_COMPOSITION_END() \
2266 int idx = - cmp_status->length; \
2268 if (cmp_status->old_form) \
2269 charbuf[idx + 2] = cmp_status->nchars; \
2270 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2271 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2272 cmp_status->state = COMPOSING_NO; \
2277 emacs_mule_finish_composition (int *charbuf
,
2278 struct composition_status
*cmp_status
)
2280 int idx
= - cmp_status
->length
;
2283 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2285 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2287 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2288 && cmp_status
->state
== COMPOSING_CHAR
)
2290 /* The last rule was invalid. */
2291 int rule
= charbuf
[-1] + 0xA0;
2293 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2300 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2302 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2304 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2305 charbuf
[idx
++] = -3;
2311 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2312 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2314 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2315 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2316 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2317 charbuf
[idx
++] = -1;
2321 cmp_status
->state
= COMPOSING_NO
;
2325 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2327 if (cmp_status->state != COMPOSING_NO) \
2328 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2333 decode_coding_emacs_mule (struct coding_system
*coding
)
2335 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2336 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2337 const unsigned char *src_base
;
2338 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2339 /* We may produce two annotations (charset and composition) in one
2340 loop and one more charset annotation at the end. */
2342 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2343 /* We can produce up to 2 characters in a loop. */
2345 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
2346 bool multibytep
= coding
->src_multibyte
;
2347 ptrdiff_t char_offset
= coding
->produced_char
;
2348 ptrdiff_t last_offset
= char_offset
;
2349 int last_id
= charset_ascii
;
2351 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2352 int byte_after_cr
= -1;
2353 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2355 if (cmp_status
->state
!= COMPOSING_NO
)
2359 if (charbuf_end
- charbuf
< cmp_status
->length
)
2361 for (i
= 0; i
< cmp_status
->length
; i
++)
2362 *charbuf
++ = cmp_status
->carryover
[i
];
2363 coding
->annotated
= 1;
2372 consumed_chars_base
= consumed_chars
;
2374 if (charbuf
>= charbuf_end
)
2376 if (byte_after_cr
>= 0)
2381 if (byte_after_cr
>= 0)
2382 c
= byte_after_cr
, byte_after_cr
= -1;
2386 if (c
< 0 || c
== 0x80)
2388 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2395 DECODE_EMACS_MULE_COMPOSITION_START ();
2401 if (eol_dos
&& c
== '\r')
2402 ONE_MORE_BYTE (byte_after_cr
);
2404 if (cmp_status
->state
!= COMPOSING_NO
)
2406 if (cmp_status
->old_form
)
2407 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2408 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2409 cmp_status
->ncomps
--;
2414 int nchars UNINIT
, nbytes UNINIT
;
2415 /* emacs_mule_char can load a charset map from a file, which
2416 allocates a large structure and might cause buffer text
2417 to be relocated as result. Thus, we need to remember the
2418 original pointer to buffer text, and fix up all related
2419 pointers after the call. */
2420 const unsigned char *orig
= coding
->source
;
2423 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2425 offset
= coding
->source
- orig
;
2439 src
= src_base
+ nbytes
;
2440 consumed_chars
= consumed_chars_base
+ nchars
;
2441 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2442 cmp_status
->ncomps
-= nchars
;
2445 /* Now if C >= 0, we found a normally encoded character, if C <
2446 0, we found an old-style composition component character or
2449 if (cmp_status
->state
== COMPOSING_NO
)
2453 if (last_id
!= charset_ascii
)
2454 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2457 last_offset
= char_offset
;
2462 else if (cmp_status
->state
== COMPOSING_CHAR
)
2464 if (cmp_status
->old_form
)
2468 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2475 cmp_status
->nchars
++;
2476 cmp_status
->length
++;
2477 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2478 EMACS_MULE_COMPOSITION_END ();
2479 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2480 cmp_status
->state
= COMPOSING_RULE
;
2486 cmp_status
->length
++;
2487 cmp_status
->nchars
--;
2488 if (cmp_status
->nchars
== 0)
2489 EMACS_MULE_COMPOSITION_END ();
2492 else if (cmp_status
->state
== COMPOSING_RULE
)
2498 EMACS_MULE_COMPOSITION_END ();
2505 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2510 cmp_status
->length
+= 2;
2511 cmp_status
->state
= COMPOSING_CHAR
;
2514 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2517 cmp_status
->length
++;
2518 if (cmp_status
->ncomps
== 0)
2519 cmp_status
->state
= COMPOSING_CHAR
;
2520 else if (cmp_status
->ncomps
> 0)
2522 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2523 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2526 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2528 else /* COMPOSING_COMPONENT_RULE */
2532 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2537 cmp_status
->length
+= 2;
2538 cmp_status
->ncomps
--;
2539 if (cmp_status
->ncomps
> 0)
2540 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2542 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2547 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2549 consumed_chars
= consumed_chars_base
;
2551 *charbuf
++ = ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2556 if (cmp_status
->state
!= COMPOSING_NO
)
2558 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2559 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2564 charbuf
-= cmp_status
->length
;
2565 for (i
= 0; i
< cmp_status
->length
; i
++)
2566 cmp_status
->carryover
[i
] = charbuf
[i
];
2569 if (last_id
!= charset_ascii
)
2570 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2571 coding
->consumed_char
+= consumed_chars_base
;
2572 coding
->consumed
= src_base
- coding
->source
;
2573 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2577 #define EMACS_MULE_LEADING_CODES(id, codes) \
2580 codes[0] = id, codes[1] = 0; \
2581 else if (id < 0xE0) \
2582 codes[0] = 0x9A, codes[1] = id; \
2583 else if (id < 0xF0) \
2584 codes[0] = 0x9B, codes[1] = id; \
2585 else if (id < 0xF5) \
2586 codes[0] = 0x9C, codes[1] = id; \
2588 codes[0] = 0x9D, codes[1] = id; \
2593 encode_coding_emacs_mule (struct coding_system
*coding
)
2595 bool multibytep
= coding
->dst_multibyte
;
2596 int *charbuf
= coding
->charbuf
;
2597 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2598 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2599 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2601 ptrdiff_t produced_chars
= 0;
2602 Lisp_Object attrs
, charset_list
;
2604 int preferred_charset_id
= -1;
2606 CODING_GET_INFO (coding
, attrs
, charset_list
);
2607 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2609 charset_list
= Vemacs_mule_charset_list
;
2610 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2613 while (charbuf
< charbuf_end
)
2615 ASSURE_DESTINATION (safe_room
);
2620 /* Handle an annotation. */
2623 case CODING_ANNOTATE_COMPOSITION_MASK
:
2624 /* Not yet implemented. */
2626 case CODING_ANNOTATE_CHARSET_MASK
:
2627 preferred_charset_id
= charbuf
[3];
2628 if (preferred_charset_id
>= 0
2629 && NILP (Fmemq (make_number (preferred_charset_id
),
2631 preferred_charset_id
= -1;
2640 if (ASCII_CHAR_P (c
))
2641 EMIT_ONE_ASCII_BYTE (c
);
2642 else if (CHAR_BYTE8_P (c
))
2644 c
= CHAR_TO_BYTE8 (c
);
2649 struct charset
*charset
;
2653 unsigned char leading_codes
[2];
2655 if (preferred_charset_id
>= 0)
2659 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2660 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2662 code
= ENCODE_CHAR (charset
, c
);
2664 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2668 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2672 c
= coding
->default_char
;
2673 if (ASCII_CHAR_P (c
))
2675 EMIT_ONE_ASCII_BYTE (c
);
2678 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2681 dimension
= CHARSET_DIMENSION (charset
);
2682 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2683 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2684 EMIT_ONE_BYTE (leading_codes
[0]);
2685 if (leading_codes
[1])
2686 EMIT_ONE_BYTE (leading_codes
[1]);
2688 EMIT_ONE_BYTE (code
| 0x80);
2692 EMIT_ONE_BYTE (code
>> 8);
2693 EMIT_ONE_BYTE (code
& 0xFF);
2697 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2698 coding
->produced_char
+= produced_chars
;
2699 coding
->produced
= dst
- coding
->destination
;
2704 /*** 7. ISO2022 handlers ***/
2706 /* The following note describes the coding system ISO2022 briefly.
2707 Since the intention of this note is to help understand the
2708 functions in this file, some parts are NOT ACCURATE or are OVERLY
2709 SIMPLIFIED. For thorough understanding, please refer to the
2710 original document of ISO2022. This is equivalent to the standard
2711 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2713 ISO2022 provides many mechanisms to encode several character sets
2714 in 7-bit and 8-bit environments. For 7-bit environments, all text
2715 is encoded using bytes less than 128. This may make the encoded
2716 text a little bit longer, but the text passes more easily through
2717 several types of gateway, some of which strip off the MSB (Most
2720 There are two kinds of character sets: control character sets and
2721 graphic character sets. The former contain control characters such
2722 as `newline' and `escape' to provide control functions (control
2723 functions are also provided by escape sequences). The latter
2724 contain graphic characters such as 'A' and '-'. Emacs recognizes
2725 two control character sets and many graphic character sets.
2727 Graphic character sets are classified into one of the following
2728 four classes, according to the number of bytes (DIMENSION) and
2729 number of characters in one dimension (CHARS) of the set:
2730 - DIMENSION1_CHARS94
2731 - DIMENSION1_CHARS96
2732 - DIMENSION2_CHARS94
2733 - DIMENSION2_CHARS96
2735 In addition, each character set is assigned an identification tag,
2736 unique for each set, called the "final character" (denoted as <F>
2737 hereafter). The <F> of each character set is decided by ECMA(*)
2738 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2739 (0x30..0x3F are for private use only).
2741 Note (*): ECMA = European Computer Manufacturers Association
2743 Here are examples of graphic character sets [NAME(<F>)]:
2744 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2745 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2746 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2747 o DIMENSION2_CHARS96 -- none for the moment
2749 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2750 C0 [0x00..0x1F] -- control character plane 0
2751 GL [0x20..0x7F] -- graphic character plane 0
2752 C1 [0x80..0x9F] -- control character plane 1
2753 GR [0xA0..0xFF] -- graphic character plane 1
2755 A control character set is directly designated and invoked to C0 or
2756 C1 by an escape sequence. The most common case is that:
2757 - ISO646's control character set is designated/invoked to C0, and
2758 - ISO6429's control character set is designated/invoked to C1,
2759 and usually these designations/invocations are omitted in encoded
2760 text. In a 7-bit environment, only C0 can be used, and a control
2761 character for C1 is encoded by an appropriate escape sequence to
2762 fit into the environment. All control characters for C1 are
2763 defined to have corresponding escape sequences.
2765 A graphic character set is at first designated to one of four
2766 graphic registers (G0 through G3), then these graphic registers are
2767 invoked to GL or GR. These designations and invocations can be
2768 done independently. The most common case is that G0 is invoked to
2769 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2770 these invocations and designations are omitted in encoded text.
2771 In a 7-bit environment, only GL can be used.
2773 When a graphic character set of CHARS94 is invoked to GL, codes
2774 0x20 and 0x7F of the GL area work as control characters SPACE and
2775 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2778 There are two ways of invocation: locking-shift and single-shift.
2779 With locking-shift, the invocation lasts until the next different
2780 invocation, whereas with single-shift, the invocation affects the
2781 following character only and doesn't affect the locking-shift
2782 state. Invocations are done by the following control characters or
2785 ----------------------------------------------------------------------
2786 abbrev function cntrl escape seq description
2787 ----------------------------------------------------------------------
2788 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2789 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2790 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2791 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2792 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2793 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2794 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2795 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2796 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2797 ----------------------------------------------------------------------
2798 (*) These are not used by any known coding system.
2800 Control characters for these functions are defined by macros
2801 ISO_CODE_XXX in `coding.h'.
2803 Designations are done by the following escape sequences:
2804 ----------------------------------------------------------------------
2805 escape sequence description
2806 ----------------------------------------------------------------------
2807 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2808 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2809 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2810 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2811 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2812 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2813 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2814 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2815 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2816 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2817 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2818 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2819 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2820 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2821 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2822 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2823 ----------------------------------------------------------------------
2825 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2826 of dimension 1, chars 94, and final character <F>, etc...
2828 Note (*): Although these designations are not allowed in ISO2022,
2829 Emacs accepts them on decoding, and produces them on encoding
2830 CHARS96 character sets in a coding system which is characterized as
2831 7-bit environment, non-locking-shift, and non-single-shift.
2833 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2834 '(' must be omitted. We refer to this as "short-form" hereafter.
2836 Now you may notice that there are a lot of ways of encoding the
2837 same multilingual text in ISO2022. Actually, there exist many
2838 coding systems such as Compound Text (used in X11's inter client
2839 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2840 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2841 localized platforms), and all of these are variants of ISO2022.
2843 In addition to the above, Emacs handles two more kinds of escape
2844 sequences: ISO6429's direction specification and Emacs' private
2845 sequence for specifying character composition.
2847 ISO6429's direction specification takes the following form:
2848 o CSI ']' -- end of the current direction
2849 o CSI '0' ']' -- end of the current direction
2850 o CSI '1' ']' -- start of left-to-right text
2851 o CSI '2' ']' -- start of right-to-left text
2852 The control character CSI (0x9B: control sequence introducer) is
2853 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2855 Character composition specification takes the following form:
2856 o ESC '0' -- start relative composition
2857 o ESC '1' -- end composition
2858 o ESC '2' -- start rule-base composition (*)
2859 o ESC '3' -- start relative composition with alternate chars (**)
2860 o ESC '4' -- start rule-base composition with alternate chars (**)
2861 Since these are not standard escape sequences of any ISO standard,
2862 the use of them with these meanings is restricted to Emacs only.
2864 (*) This form is used only in Emacs 20.7 and older versions,
2865 but newer versions can safely decode it.
2866 (**) This form is used only in Emacs 21.1 and newer versions,
2867 and older versions can't decode it.
2869 Here's a list of example usages of these composition escape
2870 sequences (categorized by `enum composition_method').
2872 COMPOSITION_RELATIVE:
2873 ESC 0 CHAR [ CHAR ] ESC 1
2874 COMPOSITION_WITH_RULE:
2875 ESC 2 CHAR [ RULE CHAR ] ESC 1
2876 COMPOSITION_WITH_ALTCHARS:
2877 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2878 COMPOSITION_WITH_RULE_ALTCHARS:
2879 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2881 static enum iso_code_class_type iso_code_class
[256];
2883 #define SAFE_CHARSET_P(coding, id) \
2884 ((id) <= (coding)->max_charset_id \
2885 && (coding)->safe_charsets[id] != 255)
2888 setup_iso_safe_charsets (Lisp_Object attrs
)
2890 Lisp_Object charset_list
, safe_charsets
;
2891 Lisp_Object request
;
2892 Lisp_Object reg_usage
;
2894 EMACS_INT reg94
, reg96
;
2895 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2898 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2899 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2900 && ! EQ (charset_list
, Viso_2022_charset_list
))
2902 charset_list
= Viso_2022_charset_list
;
2903 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2904 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2907 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2911 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2913 int id
= XINT (XCAR (tail
));
2914 if (max_charset_id
< id
)
2915 max_charset_id
= id
;
2918 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2919 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2920 request
= AREF (attrs
, coding_attr_iso_request
);
2921 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2922 reg94
= XINT (XCAR (reg_usage
));
2923 reg96
= XINT (XCDR (reg_usage
));
2925 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2929 struct charset
*charset
;
2932 charset
= CHARSET_FROM_ID (XINT (id
));
2933 reg
= Fcdr (Fassq (id
, request
));
2935 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2936 else if (charset
->iso_chars_96
)
2939 SSET (safe_charsets
, XINT (id
), reg96
);
2944 SSET (safe_charsets
, XINT (id
), reg94
);
2947 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2951 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2952 Return true if a text is encoded in one of ISO-2022 based coding
2956 detect_coding_iso_2022 (struct coding_system
*coding
,
2957 struct coding_detection_info
*detect_info
)
2959 const unsigned char *src
= coding
->source
, *src_base
= src
;
2960 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2961 bool multibytep
= coding
->src_multibyte
;
2962 bool single_shifting
= 0;
2965 ptrdiff_t consumed_chars
= 0;
2969 int composition_count
= -1;
2971 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2973 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2975 struct coding_system
*this = &(coding_categories
[i
]);
2976 Lisp_Object attrs
, val
;
2980 attrs
= CODING_ID_ATTRS (this->id
);
2981 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2982 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2983 setup_iso_safe_charsets (attrs
);
2984 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2985 this->max_charset_id
= SCHARS (val
) - 1;
2986 this->safe_charsets
= SDATA (val
);
2989 /* A coding system of this category is always ASCII compatible. */
2990 src
+= coding
->head_ascii
;
2992 while (rejected
!= CATEGORY_MASK_ISO
)
2999 if (inhibit_iso_escape_detection
)
3001 single_shifting
= 0;
3003 if (c
== 'N' || c
== 'O')
3005 /* ESC <Fe> for SS2 or SS3. */
3006 single_shifting
= 1;
3007 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3011 /* End of composition. */
3012 if (composition_count
< 0
3013 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3016 composition_count
= -1;
3017 found
|= CATEGORY_MASK_ISO
;
3019 else if (c
>= '0' && c
<= '4')
3021 /* ESC <Fp> for start/end composition. */
3022 composition_count
= 0;
3026 if (c
>= '(' && c
<= '/')
3028 /* Designation sequence for a charset of dimension 1. */
3030 if (c1
< ' ' || c1
>= 0x80
3031 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3033 /* Invalid designation sequence. Just ignore. */
3035 rejected
|= (CATEGORY_MASK_ISO_7BIT
3036 | CATEGORY_MASK_ISO_7_ELSE
);
3042 /* Designation sequence for a charset of dimension 2. */
3044 if (c
>= '@' && c
<= 'B')
3045 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3046 id
= iso_charset_table
[1][0][c
];
3047 else if (c
>= '(' && c
<= '/')
3050 if (c1
< ' ' || c1
>= 0x80
3051 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3053 /* Invalid designation sequence. Just ignore. */
3055 rejected
|= (CATEGORY_MASK_ISO_7BIT
3056 | CATEGORY_MASK_ISO_7_ELSE
);
3062 /* Invalid designation sequence. Just ignore it. */
3064 rejected
|= (CATEGORY_MASK_ISO_7BIT
3065 | CATEGORY_MASK_ISO_7_ELSE
);
3071 /* Invalid escape sequence. Just ignore it. */
3073 rejected
|= (CATEGORY_MASK_ISO_7BIT
3074 | CATEGORY_MASK_ISO_7_ELSE
);
3078 /* We found a valid designation sequence for CHARSET. */
3079 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3080 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3082 found
|= CATEGORY_MASK_ISO_7
;
3084 rejected
|= CATEGORY_MASK_ISO_7
;
3085 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3087 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3089 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3090 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3092 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3094 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3095 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3097 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3099 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3105 /* Locking shift out/in. */
3106 if (inhibit_iso_escape_detection
)
3108 single_shifting
= 0;
3109 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3113 /* Control sequence introducer. */
3114 single_shifting
= 0;
3115 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3116 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3117 goto check_extra_latin
;
3122 if (inhibit_iso_escape_detection
)
3124 single_shifting
= 0;
3125 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3126 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3127 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3129 found
|= CATEGORY_MASK_ISO_8_1
;
3130 single_shifting
= 1;
3132 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3133 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3135 found
|= CATEGORY_MASK_ISO_8_2
;
3136 single_shifting
= 1;
3138 if (single_shifting
)
3140 goto check_extra_latin
;
3147 if (composition_count
>= 0)
3148 composition_count
++;
3149 single_shifting
= 0;
3152 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3155 found
|= CATEGORY_MASK_ISO_8_1
;
3156 /* Check the length of succeeding codes of the range
3157 0xA0..0FF. If the byte length is even, we include
3158 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3159 only when we are not single shifting. */
3160 if (! single_shifting
3161 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3164 while (src
< src_end
)
3176 if (len
& 1 && src
< src_end
)
3178 rejected
|= CATEGORY_MASK_ISO_8_2
;
3179 if (composition_count
>= 0)
3180 composition_count
+= len
;
3184 found
|= CATEGORY_MASK_ISO_8_2
;
3185 if (composition_count
>= 0)
3186 composition_count
+= len
/ 2;
3192 if (! VECTORP (Vlatin_extra_code_table
)
3193 || NILP (AREF (Vlatin_extra_code_table
, c
)))
3195 rejected
= CATEGORY_MASK_ISO
;
3198 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3199 & CODING_ISO_FLAG_LATIN_EXTRA
)
3200 found
|= CATEGORY_MASK_ISO_8_1
;
3202 rejected
|= CATEGORY_MASK_ISO_8_1
;
3203 rejected
|= CATEGORY_MASK_ISO_8_2
;
3207 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3211 detect_info
->rejected
|= rejected
;
3212 detect_info
->found
|= (found
& ~rejected
);
3217 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3218 escape sequence should be kept. */
3219 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3223 if (final < '0' || final >= 128 \
3224 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3225 || !SAFE_CHARSET_P (coding, id)) \
3227 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3231 prev = CODING_ISO_DESIGNATION (coding, reg); \
3232 if (id == charset_jisx0201_roman) \
3234 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3235 id = charset_ascii; \
3237 else if (id == charset_jisx0208_1978) \
3239 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3240 id = charset_jisx0208; \
3242 CODING_ISO_DESIGNATION (coding, reg) = id; \
3243 /* If there was an invalid designation to REG previously, and this \
3244 designation is ASCII to REG, we should keep this designation \
3246 if (prev == -2 && id == charset_ascii) \
3251 /* Handle these composition sequence (ALT: alternate char):
3253 (1) relative composition: ESC 0 CHAR ... ESC 1
3254 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3255 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3256 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3258 When the start sequence (ESC 0/2/3/4) is found, this annotation
3261 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3263 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3264 produced until the end sequence (ESC 1) is found:
3267 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3268 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3269 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3271 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3272 annotation header is updated as below:
3274 (1) LENGTH: unchanged, NCHARS: number of CHARs
3275 (2) LENGTH: unchanged, NCHARS: number of CHARs
3276 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3277 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3279 If an error is found while composing, the annotation header is
3282 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3284 and the sequence [ -2 DECODED-RULE ] is changed to the original
3285 byte sequence as below:
3286 o the original byte sequence is B: [ B -1 ]
3287 o the original byte sequence is B1 B2: [ B1 B2 ]
3288 and the sequence [ -1 -1 ] is changed to the original byte
3293 /* Decode a composition rule C1 and maybe one more byte from the
3294 source, and set RULE to the encoded composition rule. If the rule
3295 is invalid, goto invalid_code. */
3297 #define DECODE_COMPOSITION_RULE(rule) \
3301 goto invalid_code; \
3302 if (rule < 81) /* old format (before ver.21) */ \
3304 int gref = (rule) / 9; \
3305 int nref = (rule) % 9; \
3306 if (gref == 4) gref = 10; \
3307 if (nref == 4) nref = 10; \
3308 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3310 else /* new format (after ver.21) */ \
3314 ONE_MORE_BYTE (b); \
3315 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3316 goto invalid_code; \
3317 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3318 rule += 0x100; /* Distinguish it from the old format. */ \
3322 #define ENCODE_COMPOSITION_RULE(rule) \
3324 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3326 if (rule < 0x100) /* old format */ \
3328 if (gref == 10) gref = 4; \
3329 if (nref == 10) nref = 4; \
3330 charbuf[idx] = 32 + gref * 9 + nref; \
3331 charbuf[idx + 1] = -1; \
3334 else /* new format */ \
3336 charbuf[idx] = 32 + 81 + gref; \
3337 charbuf[idx + 1] = 32 + nref; \
3342 /* Finish the current composition as invalid. */
3345 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3347 int idx
= - cmp_status
->length
;
3350 /* Recover the original ESC sequence */
3351 charbuf
[idx
++] = ISO_CODE_ESC
;
3352 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3353 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3354 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3355 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3357 charbuf
[idx
++] = -2;
3359 charbuf
[idx
++] = -1;
3360 new_chars
= cmp_status
->nchars
;
3361 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3362 for (; idx
< 0; idx
++)
3364 int elt
= charbuf
[idx
];
3368 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3373 charbuf
[idx
++] = ISO_CODE_ESC
;
3378 cmp_status
->state
= COMPOSING_NO
;
3382 /* If characters are under composition, finish the composition. */
3383 #define MAYBE_FINISH_COMPOSITION() \
3385 if (cmp_status->state != COMPOSING_NO) \
3386 char_offset += finish_composition (charbuf, cmp_status); \
3389 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3391 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3392 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3393 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3394 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3396 Produce this annotation sequence now:
3398 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3401 #define DECODE_COMPOSITION_START(c1) \
3404 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3405 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3406 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3407 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3411 cmp_status->state = COMPOSING_CHAR; \
3412 cmp_status->length += 2; \
3416 MAYBE_FINISH_COMPOSITION (); \
3417 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3418 : c1 == '2' ? COMPOSITION_WITH_RULE \
3419 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3420 : COMPOSITION_WITH_RULE_ALTCHARS); \
3422 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3423 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3424 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3425 cmp_status->nchars = cmp_status->ncomps = 0; \
3426 coding->annotated = 1; \
3431 /* Handle composition end sequence ESC 1. */
3433 #define DECODE_COMPOSITION_END() \
3435 if (cmp_status->nchars == 0 \
3436 || ((cmp_status->state == COMPOSING_CHAR) \
3437 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3439 MAYBE_FINISH_COMPOSITION (); \
3440 goto invalid_code; \
3442 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3443 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3444 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3445 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3446 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3447 char_offset += cmp_status->nchars; \
3448 cmp_status->state = COMPOSING_NO; \
3451 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3453 #define STORE_COMPOSITION_RULE(rule) \
3456 *charbuf++ = rule; \
3457 cmp_status->length += 2; \
3458 cmp_status->state--; \
3461 /* Store a composed char or a component char C in charbuf, and update
3464 #define STORE_COMPOSITION_CHAR(c) \
3467 cmp_status->length++; \
3468 if (cmp_status->state == COMPOSING_CHAR) \
3469 cmp_status->nchars++; \
3471 cmp_status->ncomps++; \
3472 if (cmp_status->method == COMPOSITION_WITH_RULE \
3473 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3474 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3475 cmp_status->state++; \
3479 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3482 decode_coding_iso_2022 (struct coding_system
*coding
)
3484 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3485 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3486 const unsigned char *src_base
;
3487 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3488 /* We may produce two annotations (charset and composition) in one
3489 loop and one more charset annotation at the end. */
3491 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3492 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
3493 bool multibytep
= coding
->src_multibyte
;
3494 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3495 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3496 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3497 int charset_id_2
, charset_id_3
;
3498 struct charset
*charset
;
3500 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3501 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3502 ptrdiff_t char_offset
= coding
->produced_char
;
3503 ptrdiff_t last_offset
= char_offset
;
3504 int last_id
= charset_ascii
;
3506 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3507 int byte_after_cr
= -1;
3510 setup_iso_safe_charsets (attrs
);
3511 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3513 if (cmp_status
->state
!= COMPOSING_NO
)
3515 if (charbuf_end
- charbuf
< cmp_status
->length
)
3517 for (i
= 0; i
< cmp_status
->length
; i
++)
3518 *charbuf
++ = cmp_status
->carryover
[i
];
3519 coding
->annotated
= 1;
3527 consumed_chars_base
= consumed_chars
;
3529 if (charbuf
>= charbuf_end
)
3531 if (byte_after_cr
>= 0)
3536 if (byte_after_cr
>= 0)
3537 c1
= byte_after_cr
, byte_after_cr
= -1;
3543 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3545 *charbuf
++ = ASCII_CHAR_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3547 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3551 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3553 if (c1
== ISO_CODE_ESC
)
3555 if (src
+ 1 >= src_end
)
3556 goto no_more_source
;
3557 *charbuf
++ = ISO_CODE_ESC
;
3559 if (src
[0] == '%' && src
[1] == '@')
3562 consumed_chars
+= 2;
3564 /* We are sure charbuf can contain two more chars. */
3567 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3572 *charbuf
++ = ASCII_CHAR_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3578 if ((cmp_status
->state
== COMPOSING_RULE
3579 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3580 && c1
!= ISO_CODE_ESC
)
3584 DECODE_COMPOSITION_RULE (rule
);
3585 STORE_COMPOSITION_RULE (rule
);
3589 /* We produce at most one character. */
3590 switch (iso_code_class
[c1
])
3592 case ISO_0x20_or_0x7F
:
3593 if (charset_id_0
< 0
3594 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3595 /* This is SPACE or DEL. */
3596 charset
= CHARSET_FROM_ID (charset_ascii
);
3598 charset
= CHARSET_FROM_ID (charset_id_0
);
3601 case ISO_graphic_plane_0
:
3602 if (charset_id_0
< 0)
3603 charset
= CHARSET_FROM_ID (charset_ascii
);
3605 charset
= CHARSET_FROM_ID (charset_id_0
);
3608 case ISO_0xA0_or_0xFF
:
3609 if (charset_id_1
< 0
3610 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3611 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3613 /* This is a graphic character, we fall down ... */
3615 case ISO_graphic_plane_1
:
3616 if (charset_id_1
< 0)
3618 charset
= CHARSET_FROM_ID (charset_id_1
);
3622 if (eol_dos
&& c1
== '\r')
3623 ONE_MORE_BYTE (byte_after_cr
);
3624 MAYBE_FINISH_COMPOSITION ();
3625 charset
= CHARSET_FROM_ID (charset_ascii
);
3632 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3633 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3635 CODING_ISO_INVOCATION (coding
, 0) = 1;
3636 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3640 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3642 CODING_ISO_INVOCATION (coding
, 0) = 0;
3643 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3646 case ISO_single_shift_2_7
:
3647 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3650 case ISO_single_shift_2
:
3651 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3653 /* SS2 is handled as an escape sequence of ESC 'N' */
3655 goto label_escape_sequence
;
3657 case ISO_single_shift_3
:
3658 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3660 /* SS2 is handled as an escape sequence of ESC 'O' */
3662 goto label_escape_sequence
;
3664 case ISO_control_sequence_introducer
:
3665 /* CSI is handled as an escape sequence of ESC '[' ... */
3667 goto label_escape_sequence
;
3671 label_escape_sequence
:
3672 /* Escape sequences handled here are invocation,
3673 designation, direction specification, and character
3674 composition specification. */
3677 case '&': /* revision of following character set */
3679 if (!(c1
>= '@' && c1
<= '~'))
3682 if (c1
!= ISO_CODE_ESC
)
3685 goto label_escape_sequence
;
3687 case '$': /* designation of 2-byte character set */
3688 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3694 if (c1
>= '@' && c1
<= 'B')
3695 { /* designation of JISX0208.1978, GB2312.1980,
3697 reg
= 0, chars96
= 0;
3699 else if (c1
>= 0x28 && c1
<= 0x2B)
3700 { /* designation of DIMENSION2_CHARS94 character set */
3701 reg
= c1
- 0x28, chars96
= 0;
3704 else if (c1
>= 0x2C && c1
<= 0x2F)
3705 { /* designation of DIMENSION2_CHARS96 character set */
3706 reg
= c1
- 0x2C, chars96
= 1;
3711 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3712 /* We must update these variables now. */
3714 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3716 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3722 case 'n': /* invocation of locking-shift-2 */
3723 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3724 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3726 CODING_ISO_INVOCATION (coding
, 0) = 2;
3727 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3730 case 'o': /* invocation of locking-shift-3 */
3731 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3732 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3734 CODING_ISO_INVOCATION (coding
, 0) = 3;
3735 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3738 case 'N': /* invocation of single-shift-2 */
3739 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3740 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3742 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3743 if (charset_id_2
< 0)
3744 charset
= CHARSET_FROM_ID (charset_ascii
);
3746 charset
= CHARSET_FROM_ID (charset_id_2
);
3748 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0)
3749 || (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3750 && ((CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LEVEL_4
)
3751 ? c1
>= 0x80 : c1
< 0x80)))
3755 case 'O': /* invocation of single-shift-3 */
3756 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3757 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3759 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3760 if (charset_id_3
< 0)
3761 charset
= CHARSET_FROM_ID (charset_ascii
);
3763 charset
= CHARSET_FROM_ID (charset_id_3
);
3765 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0)
3766 || (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3767 && ((CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LEVEL_4
)
3768 ? c1
>= 0x80 : c1
< 0x80)))
3772 case '0': case '2': case '3': case '4': /* start composition */
3773 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3775 if (last_id
!= charset_ascii
)
3777 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3778 last_id
= charset_ascii
;
3779 last_offset
= char_offset
;
3781 DECODE_COMPOSITION_START (c1
);
3784 case '1': /* end composition */
3785 if (cmp_status
->state
== COMPOSING_NO
)
3787 DECODE_COMPOSITION_END ();
3790 case '[': /* specification of direction */
3791 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3793 /* For the moment, nested direction is not supported.
3794 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3795 left-to-right, and nonzero means right-to-left. */
3799 case ']': /* end of the current direction */
3800 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3803 case '0': /* end of the current direction */
3804 case '1': /* start of left-to-right direction */
3807 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3812 case '2': /* start of right-to-left direction */
3815 coding
->mode
|= CODING_MODE_DIRECTION
;
3829 /* CTEXT extended segment:
3830 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3831 We keep these bytes as is for the moment.
3832 They may be decoded by post-read-conversion. */
3836 ONE_MORE_BYTE (dim
);
3837 if (dim
< '0' || dim
> '4')
3845 size
= ((M
- 128) * 128) + (L
- 128);
3846 if (charbuf
+ 6 > charbuf_end
)
3848 *charbuf
++ = ISO_CODE_ESC
;
3852 *charbuf
++ = BYTE8_TO_CHAR (M
);
3853 *charbuf
++ = BYTE8_TO_CHAR (L
);
3854 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3858 /* XFree86 extension for embedding UTF-8 in CTEXT:
3859 ESC % G --UTF-8-BYTES-- ESC % @
3860 We keep these bytes as is for the moment.
3861 They may be decoded by post-read-conversion. */
3862 if (charbuf
+ 3 > charbuf_end
)
3864 *charbuf
++ = ISO_CODE_ESC
;
3867 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3875 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3880 if (c1
>= 0x28 && c1
<= 0x2B)
3881 { /* designation of DIMENSION1_CHARS94 character set */
3882 reg
= c1
- 0x28, chars96
= 0;
3885 else if (c1
>= 0x2C && c1
<= 0x2F)
3886 { /* designation of DIMENSION1_CHARS96 character set */
3887 reg
= c1
- 0x2C, chars96
= 1;
3892 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3893 /* We must update these variables now. */
3895 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3897 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3909 if (cmp_status
->state
== COMPOSING_NO
3910 && charset
->id
!= charset_ascii
3911 && last_id
!= charset
->id
)
3913 if (last_id
!= charset_ascii
)
3914 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3915 last_id
= charset
->id
;
3916 last_offset
= char_offset
;
3919 /* Now we know CHARSET and 1st position code C1 of a character.
3920 Produce a decoded character while getting 2nd and 3rd
3921 position codes C2, C3 if necessary. */
3922 if (CHARSET_DIMENSION (charset
) > 1)
3925 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3926 || ((c1
& 0x80) != (c2
& 0x80)))
3927 /* C2 is not in a valid range. */
3929 if (CHARSET_DIMENSION (charset
) == 2)
3930 c1
= (c1
<< 8) | c2
;
3934 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3935 || ((c1
& 0x80) != (c3
& 0x80)))
3936 /* C3 is not in a valid range. */
3938 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3942 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3945 MAYBE_FINISH_COMPOSITION ();
3946 for (; src_base
< src
; src_base
++, char_offset
++)
3948 if (ASCII_CHAR_P (*src_base
))
3949 *charbuf
++ = *src_base
;
3951 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3954 else if (cmp_status
->state
== COMPOSING_NO
)
3959 else if ((cmp_status
->state
== COMPOSING_CHAR
3960 ? cmp_status
->nchars
3961 : cmp_status
->ncomps
)
3962 >= MAX_COMPOSITION_COMPONENTS
)
3964 /* Too long composition. */
3965 MAYBE_FINISH_COMPOSITION ();
3970 STORE_COMPOSITION_CHAR (c
);
3974 MAYBE_FINISH_COMPOSITION ();
3976 consumed_chars
= consumed_chars_base
;
3978 *charbuf
++ = c
< 0 ? -c
: ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3980 /* Reset the invocation and designation status to the safest
3981 one; i.e. designate ASCII to the graphic register 0, and
3982 invoke that register to the graphic plane 0. This typically
3983 helps the case that a designation sequence for ASCII "ESC (
3984 B" is somehow broken (e.g. broken by a newline). */
3985 CODING_ISO_INVOCATION (coding
, 0) = 0;
3986 CODING_ISO_DESIGNATION (coding
, 0) = charset_ascii
;
3987 charset_id_0
= charset_ascii
;
3995 if (cmp_status
->state
!= COMPOSING_NO
)
3997 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3998 MAYBE_FINISH_COMPOSITION ();
4001 charbuf
-= cmp_status
->length
;
4002 for (i
= 0; i
< cmp_status
->length
; i
++)
4003 cmp_status
->carryover
[i
] = charbuf
[i
];
4006 else if (last_id
!= charset_ascii
)
4007 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4008 coding
->consumed_char
+= consumed_chars_base
;
4009 coding
->consumed
= src_base
- coding
->source
;
4010 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4014 /* ISO2022 encoding stuff. */
4017 It is not enough to say just "ISO2022" on encoding, we have to
4018 specify more details. In Emacs, each coding system of ISO2022
4019 variant has the following specifications:
4020 1. Initial designation to G0 thru G3.
4021 2. Allows short-form designation?
4022 3. ASCII should be designated to G0 before control characters?
4023 4. ASCII should be designated to G0 at end of line?
4024 5. 7-bit environment or 8-bit environment?
4025 6. Use locking-shift?
4026 7. Use Single-shift?
4027 And the following two are only for Japanese:
4028 8. Use ASCII in place of JIS0201-1976-Roman?
4029 9. Use JISX0208-1983 in place of JISX0208-1978?
4030 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4031 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4035 /* Produce codes (escape sequence) for designating CHARSET to graphic
4036 register REG at DST, and increment DST. If <final-char> of CHARSET is
4037 '@', 'A', or 'B' and the coding system CODING allows, produce
4038 designation sequence of short-form. */
4040 #define ENCODE_DESIGNATION(charset, reg, coding) \
4042 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4043 const char *intermediate_char_94 = "()*+"; \
4044 const char *intermediate_char_96 = ",-./"; \
4045 int revision = -1; \
4047 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4048 revision = CHARSET_ISO_REVISION (charset); \
4050 if (revision >= 0) \
4052 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4053 EMIT_ONE_BYTE ('@' + revision); \
4055 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4056 if (CHARSET_DIMENSION (charset) == 1) \
4059 if (! CHARSET_ISO_CHARS_96 (charset)) \
4060 b = intermediate_char_94[reg]; \
4062 b = intermediate_char_96[reg]; \
4063 EMIT_ONE_ASCII_BYTE (b); \
4067 EMIT_ONE_ASCII_BYTE ('$'); \
4068 if (! CHARSET_ISO_CHARS_96 (charset)) \
4070 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4072 || final_char < '@' || final_char > 'B') \
4073 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4076 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4078 EMIT_ONE_ASCII_BYTE (final_char); \
4080 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4084 /* The following two macros produce codes (control character or escape
4085 sequence) for ISO2022 single-shift functions (single-shift-2 and
4088 #define ENCODE_SINGLE_SHIFT_2 \
4090 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4091 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4093 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4094 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4098 #define ENCODE_SINGLE_SHIFT_3 \
4100 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4101 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4103 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4104 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4108 /* The following four macros produce codes (control character or
4109 escape sequence) for ISO2022 locking-shift functions (shift-in,
4110 shift-out, locking-shift-2, and locking-shift-3). */
4112 #define ENCODE_SHIFT_IN \
4114 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4115 CODING_ISO_INVOCATION (coding, 0) = 0; \
4119 #define ENCODE_SHIFT_OUT \
4121 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4122 CODING_ISO_INVOCATION (coding, 0) = 1; \
4126 #define ENCODE_LOCKING_SHIFT_2 \
4128 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4129 CODING_ISO_INVOCATION (coding, 0) = 2; \
4133 #define ENCODE_LOCKING_SHIFT_3 \
4135 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4136 CODING_ISO_INVOCATION (coding, 0) = 3; \
4140 /* Produce codes for a DIMENSION1 character whose character set is
4141 CHARSET and whose position-code is C1. Designation and invocation
4142 sequences are also produced in advance if necessary. */
4144 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4146 int id = CHARSET_ID (charset); \
4148 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4149 && id == charset_ascii) \
4151 id = charset_jisx0201_roman; \
4152 charset = CHARSET_FROM_ID (id); \
4155 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4157 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4158 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4160 EMIT_ONE_BYTE (c1 | 0x80); \
4161 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4164 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4166 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4169 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4171 EMIT_ONE_BYTE (c1 | 0x80); \
4175 /* Since CHARSET is not yet invoked to any graphic planes, we \
4176 must invoke it, or, at first, designate it to some graphic \
4177 register. Then repeat the loop to actually produce the \
4179 dst = encode_invocation_designation (charset, coding, dst, \
4184 /* Produce codes for a DIMENSION2 character whose character set is
4185 CHARSET and whose position-codes are C1 and C2. Designation and
4186 invocation codes are also produced in advance if necessary. */
4188 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4190 int id = CHARSET_ID (charset); \
4192 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4193 && id == charset_jisx0208) \
4195 id = charset_jisx0208_1978; \
4196 charset = CHARSET_FROM_ID (id); \
4199 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4201 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4202 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4204 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4205 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4208 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4210 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4213 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4215 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4219 /* Since CHARSET is not yet invoked to any graphic planes, we \
4220 must invoke it, or, at first, designate it to some graphic \
4221 register. Then repeat the loop to actually produce the \
4223 dst = encode_invocation_designation (charset, coding, dst, \
4228 #define ENCODE_ISO_CHARACTER(charset, c) \
4231 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4233 if (CHARSET_DIMENSION (charset) == 1) \
4234 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4236 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4240 /* Produce designation and invocation codes at a place pointed by DST
4241 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4244 static unsigned char *
4245 encode_invocation_designation (struct charset
*charset
,
4246 struct coding_system
*coding
,
4247 unsigned char *dst
, ptrdiff_t *p_nchars
)
4249 bool multibytep
= coding
->dst_multibyte
;
4250 ptrdiff_t produced_chars
= *p_nchars
;
4251 int reg
; /* graphic register number */
4252 int id
= CHARSET_ID (charset
);
4254 /* At first, check designations. */
4255 for (reg
= 0; reg
< 4; reg
++)
4256 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4261 /* CHARSET is not yet designated to any graphic registers. */
4262 /* At first check the requested designation. */
4263 reg
= CODING_ISO_REQUEST (coding
, id
);
4265 /* Since CHARSET requests no special designation, designate it
4266 to graphic register 0. */
4269 ENCODE_DESIGNATION (charset
, reg
, coding
);
4272 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4273 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4275 /* Since the graphic register REG is not invoked to any graphic
4276 planes, invoke it to graphic plane 0. */
4279 case 0: /* graphic register 0 */
4283 case 1: /* graphic register 1 */
4287 case 2: /* graphic register 2 */
4288 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4289 ENCODE_SINGLE_SHIFT_2
;
4291 ENCODE_LOCKING_SHIFT_2
;
4294 case 3: /* graphic register 3 */
4295 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4296 ENCODE_SINGLE_SHIFT_3
;
4298 ENCODE_LOCKING_SHIFT_3
;
4306 *p_nchars
= produced_chars
;
4311 /* Produce codes for designation and invocation to reset the graphic
4312 planes and registers to initial state. */
4313 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4316 struct charset *charset; \
4318 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4320 for (reg = 0; reg < 4; reg++) \
4321 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4322 && (CODING_ISO_DESIGNATION (coding, reg) \
4323 != CODING_ISO_INITIAL (coding, reg))) \
4325 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4326 ENCODE_DESIGNATION (charset, reg, coding); \
4331 /* Produce designation sequences of charsets in the line started from
4332 CHARBUF to a place pointed by DST, and return the number of
4333 produced bytes. DST should not directly point a buffer text area
4334 which may be relocated by char_charset call.
4336 If the current block ends before any end-of-line, we may fail to
4337 find all the necessary designations. */
4340 encode_designation_at_bol (struct coding_system
*coding
,
4341 int *charbuf
, int *charbuf_end
,
4344 unsigned char *orig
= dst
;
4345 struct charset
*charset
;
4346 /* Table of charsets to be designated to each graphic register. */
4348 int c
, found
= 0, reg
;
4349 ptrdiff_t produced_chars
= 0;
4350 bool multibytep
= coding
->dst_multibyte
;
4352 Lisp_Object charset_list
;
4354 attrs
= CODING_ID_ATTRS (coding
->id
);
4355 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4356 if (EQ (charset_list
, Qiso_2022
))
4357 charset_list
= Viso_2022_charset_list
;
4359 for (reg
= 0; reg
< 4; reg
++)
4362 while (charbuf
< charbuf_end
&& found
< 4)
4369 charset
= char_charset (c
, charset_list
, NULL
);
4370 id
= CHARSET_ID (charset
);
4371 reg
= CODING_ISO_REQUEST (coding
, id
);
4372 if (reg
>= 0 && r
[reg
] < 0)
4381 for (reg
= 0; reg
< 4; reg
++)
4383 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4384 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4390 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4393 encode_coding_iso_2022 (struct coding_system
*coding
)
4395 bool multibytep
= coding
->dst_multibyte
;
4396 int *charbuf
= coding
->charbuf
;
4397 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4398 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4399 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4401 bool bol_designation
4402 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4403 && CODING_ISO_BOL (coding
));
4404 ptrdiff_t produced_chars
= 0;
4405 Lisp_Object attrs
, eol_type
, charset_list
;
4406 bool ascii_compatible
;
4408 int preferred_charset_id
= -1;
4410 CODING_GET_INFO (coding
, attrs
, charset_list
);
4411 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4412 if (VECTORP (eol_type
))
4415 setup_iso_safe_charsets (attrs
);
4416 /* Charset list may have been changed. */
4417 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4418 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4421 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4422 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4423 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4425 while (charbuf
< charbuf_end
)
4427 ASSURE_DESTINATION (safe_room
);
4429 if (bol_designation
)
4431 /* We have to produce designation sequences if any now. */
4432 unsigned char desig_buf
[16];
4436 charset_map_loaded
= 0;
4437 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4439 if (charset_map_loaded
4440 && (offset
= coding_change_destination (coding
)))
4445 memcpy (dst
, desig_buf
, nbytes
);
4447 /* We are sure that designation sequences are all ASCII bytes. */
4448 produced_chars
+= nbytes
;
4449 bol_designation
= 0;
4450 ASSURE_DESTINATION (safe_room
);
4457 /* Handle an annotation. */
4460 case CODING_ANNOTATE_COMPOSITION_MASK
:
4461 /* Not yet implemented. */
4463 case CODING_ANNOTATE_CHARSET_MASK
:
4464 preferred_charset_id
= charbuf
[2];
4465 if (preferred_charset_id
>= 0
4466 && NILP (Fmemq (make_number (preferred_charset_id
),
4468 preferred_charset_id
= -1;
4477 /* Now encode the character C. */
4478 if (c
< 0x20 || c
== 0x7F)
4481 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4483 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4484 ENCODE_RESET_PLANE_AND_REGISTER ();
4485 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4489 for (i
= 0; i
< 4; i
++)
4490 CODING_ISO_DESIGNATION (coding
, i
)
4491 = CODING_ISO_INITIAL (coding
, i
);
4493 bol_designation
= ((CODING_ISO_FLAGS (coding
)
4494 & CODING_ISO_FLAG_DESIGNATE_AT_BOL
)
4497 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4498 ENCODE_RESET_PLANE_AND_REGISTER ();
4499 EMIT_ONE_ASCII_BYTE (c
);
4501 else if (ASCII_CHAR_P (c
))
4503 if (ascii_compatible
)
4504 EMIT_ONE_ASCII_BYTE (c
);
4507 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4508 ENCODE_ISO_CHARACTER (charset
, c
);
4511 else if (CHAR_BYTE8_P (c
))
4513 c
= CHAR_TO_BYTE8 (c
);
4518 struct charset
*charset
;
4520 if (preferred_charset_id
>= 0)
4524 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4525 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4527 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4531 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4535 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4537 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4538 charset
= CHARSET_FROM_ID (charset_ascii
);
4542 c
= coding
->default_char
;
4543 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4544 charset_list
, NULL
, charset
);
4547 ENCODE_ISO_CHARACTER (charset
, c
);
4551 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4552 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4554 ASSURE_DESTINATION (safe_room
);
4555 ENCODE_RESET_PLANE_AND_REGISTER ();
4557 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4558 CODING_ISO_BOL (coding
) = bol_designation
;
4559 coding
->produced_char
+= produced_chars
;
4560 coding
->produced
= dst
- coding
->destination
;
4565 /*** 8,9. SJIS and BIG5 handlers ***/
4567 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4568 quite widely. So, for the moment, Emacs supports them in the bare
4569 C code. But, in the future, they may be supported only by CCL. */
4571 /* SJIS is a coding system encoding three character sets: ASCII, right
4572 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4573 as is. A character of charset katakana-jisx0201 is encoded by
4574 "position-code + 0x80". A character of charset japanese-jisx0208
4575 is encoded in 2-byte but two position-codes are divided and shifted
4576 so that it fit in the range below.
4578 --- CODE RANGE of SJIS ---
4579 (character set) (range)
4581 KATAKANA-JISX0201 0xA0 .. 0xDF
4582 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4583 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4584 -------------------------------
4588 /* BIG5 is a coding system encoding two character sets: ASCII and
4589 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4590 character set and is encoded in two-byte.
4592 --- CODE RANGE of BIG5 ---
4593 (character set) (range)
4595 Big5 (1st byte) 0xA1 .. 0xFE
4596 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4597 --------------------------
4601 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4602 Return true if a text is encoded in SJIS. */
4605 detect_coding_sjis (struct coding_system
*coding
,
4606 struct coding_detection_info
*detect_info
)
4608 const unsigned char *src
= coding
->source
, *src_base
;
4609 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4610 bool multibytep
= coding
->src_multibyte
;
4611 ptrdiff_t consumed_chars
= 0;
4614 Lisp_Object attrs
, charset_list
;
4615 int max_first_byte_of_2_byte_code
;
4617 CODING_GET_INFO (coding
, attrs
, charset_list
);
4618 max_first_byte_of_2_byte_code
4619 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4621 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4622 /* A coding system of this category is always ASCII compatible. */
4623 src
+= coding
->head_ascii
;
4631 if ((c
>= 0x81 && c
<= 0x9F)
4632 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4635 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4637 found
= CATEGORY_MASK_SJIS
;
4639 else if (c
>= 0xA0 && c
< 0xE0)
4640 found
= CATEGORY_MASK_SJIS
;
4644 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4648 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4650 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4653 detect_info
->found
|= found
;
4657 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4658 Return true if a text is encoded in BIG5. */
4661 detect_coding_big5 (struct coding_system
*coding
,
4662 struct coding_detection_info
*detect_info
)
4664 const unsigned char *src
= coding
->source
, *src_base
;
4665 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4666 bool multibytep
= coding
->src_multibyte
;
4667 ptrdiff_t consumed_chars
= 0;
4671 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4672 /* A coding system of this category is always ASCII compatible. */
4673 src
+= coding
->head_ascii
;
4684 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4686 found
= CATEGORY_MASK_BIG5
;
4691 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4695 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4697 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4700 detect_info
->found
|= found
;
4704 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4707 decode_coding_sjis (struct coding_system
*coding
)
4709 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4710 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4711 const unsigned char *src_base
;
4712 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4713 /* We may produce one charset annotation in one loop and one more at
4716 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4717 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4718 bool multibytep
= coding
->src_multibyte
;
4719 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4720 struct charset
*charset_kanji2
;
4721 Lisp_Object attrs
, charset_list
, val
;
4722 ptrdiff_t char_offset
= coding
->produced_char
;
4723 ptrdiff_t last_offset
= char_offset
;
4724 int last_id
= charset_ascii
;
4726 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4727 int byte_after_cr
= -1;
4729 CODING_GET_INFO (coding
, attrs
, charset_list
);
4732 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4733 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4734 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4735 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4740 struct charset
*charset
;
4743 consumed_chars_base
= consumed_chars
;
4745 if (charbuf
>= charbuf_end
)
4747 if (byte_after_cr
>= 0)
4752 if (byte_after_cr
>= 0)
4753 c
= byte_after_cr
, byte_after_cr
= -1;
4760 if (eol_dos
&& c
== '\r')
4761 ONE_MORE_BYTE (byte_after_cr
);
4762 charset
= charset_roman
;
4764 else if (c
== 0x80 || c
== 0xA0)
4766 else if (c
>= 0xA1 && c
<= 0xDF)
4768 /* SJIS -> JISX0201-Kana */
4770 charset
= charset_kana
;
4774 /* SJIS -> JISX0208 */
4776 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4780 charset
= charset_kanji
;
4782 else if (c
<= 0xFC && charset_kanji2
)
4784 /* SJIS -> JISX0213-2 */
4786 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4790 charset
= charset_kanji2
;
4794 if (charset
->id
!= charset_ascii
4795 && last_id
!= charset
->id
)
4797 if (last_id
!= charset_ascii
)
4798 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4799 last_id
= charset
->id
;
4800 last_offset
= char_offset
;
4802 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4809 consumed_chars
= consumed_chars_base
;
4811 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4816 if (last_id
!= charset_ascii
)
4817 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4818 coding
->consumed_char
+= consumed_chars_base
;
4819 coding
->consumed
= src_base
- coding
->source
;
4820 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4824 decode_coding_big5 (struct coding_system
*coding
)
4826 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4827 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4828 const unsigned char *src_base
;
4829 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4830 /* We may produce one charset annotation in one loop and one more at
4833 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4834 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4835 bool multibytep
= coding
->src_multibyte
;
4836 struct charset
*charset_roman
, *charset_big5
;
4837 Lisp_Object attrs
, charset_list
, val
;
4838 ptrdiff_t char_offset
= coding
->produced_char
;
4839 ptrdiff_t last_offset
= char_offset
;
4840 int last_id
= charset_ascii
;
4842 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4843 int byte_after_cr
= -1;
4845 CODING_GET_INFO (coding
, attrs
, charset_list
);
4847 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4848 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4853 struct charset
*charset
;
4856 consumed_chars_base
= consumed_chars
;
4858 if (charbuf
>= charbuf_end
)
4860 if (byte_after_cr
>= 0)
4865 if (byte_after_cr
>= 0)
4866 c
= byte_after_cr
, byte_after_cr
= -1;
4874 if (eol_dos
&& c
== '\r')
4875 ONE_MORE_BYTE (byte_after_cr
);
4876 charset
= charset_roman
;
4881 if (c
< 0xA1 || c
> 0xFE)
4884 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4887 charset
= charset_big5
;
4889 if (charset
->id
!= charset_ascii
4890 && last_id
!= charset
->id
)
4892 if (last_id
!= charset_ascii
)
4893 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4894 last_id
= charset
->id
;
4895 last_offset
= char_offset
;
4897 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4904 consumed_chars
= consumed_chars_base
;
4906 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4911 if (last_id
!= charset_ascii
)
4912 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4913 coding
->consumed_char
+= consumed_chars_base
;
4914 coding
->consumed
= src_base
- coding
->source
;
4915 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4918 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4919 This function can encode charsets `ascii', `katakana-jisx0201',
4920 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4921 are sure that all these charsets are registered as official charset
4922 (i.e. do not have extended leading-codes). Characters of other
4923 charsets are produced without any encoding. */
4926 encode_coding_sjis (struct coding_system
*coding
)
4928 bool multibytep
= coding
->dst_multibyte
;
4929 int *charbuf
= coding
->charbuf
;
4930 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4931 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4932 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4934 ptrdiff_t produced_chars
= 0;
4935 Lisp_Object attrs
, charset_list
, val
;
4936 bool ascii_compatible
;
4937 struct charset
*charset_kanji
, *charset_kana
;
4938 struct charset
*charset_kanji2
;
4941 CODING_GET_INFO (coding
, attrs
, charset_list
);
4942 val
= XCDR (charset_list
);
4943 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4944 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4945 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4947 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4949 while (charbuf
< charbuf_end
)
4951 ASSURE_DESTINATION (safe_room
);
4953 /* Now encode the character C. */
4954 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4955 EMIT_ONE_ASCII_BYTE (c
);
4956 else if (CHAR_BYTE8_P (c
))
4958 c
= CHAR_TO_BYTE8 (c
);
4964 struct charset
*charset
;
4965 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4970 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4972 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4973 charset
= CHARSET_FROM_ID (charset_ascii
);
4977 c
= coding
->default_char
;
4978 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4979 charset_list
, &code
, charset
);
4982 if (code
== CHARSET_INVALID_CODE (charset
))
4984 if (charset
== charset_kanji
)
4988 c1
= code
>> 8, c2
= code
& 0xFF;
4989 EMIT_TWO_BYTES (c1
, c2
);
4991 else if (charset
== charset_kana
)
4992 EMIT_ONE_BYTE (code
| 0x80);
4993 else if (charset_kanji2
&& charset
== charset_kanji2
)
4998 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
5000 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
5002 JIS_TO_SJIS2 (code
);
5003 c1
= code
>> 8, c2
= code
& 0xFF;
5004 EMIT_TWO_BYTES (c1
, c2
);
5007 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5010 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5013 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5014 coding
->produced_char
+= produced_chars
;
5015 coding
->produced
= dst
- coding
->destination
;
5020 encode_coding_big5 (struct coding_system
*coding
)
5022 bool multibytep
= coding
->dst_multibyte
;
5023 int *charbuf
= coding
->charbuf
;
5024 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5025 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5026 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5028 ptrdiff_t produced_chars
= 0;
5029 Lisp_Object attrs
, charset_list
, val
;
5030 bool ascii_compatible
;
5031 struct charset
*charset_big5
;
5034 CODING_GET_INFO (coding
, attrs
, charset_list
);
5035 val
= XCDR (charset_list
);
5036 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5037 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5039 while (charbuf
< charbuf_end
)
5041 ASSURE_DESTINATION (safe_room
);
5043 /* Now encode the character C. */
5044 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5045 EMIT_ONE_ASCII_BYTE (c
);
5046 else if (CHAR_BYTE8_P (c
))
5048 c
= CHAR_TO_BYTE8 (c
);
5054 struct charset
*charset
;
5055 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5060 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5062 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5063 charset
= CHARSET_FROM_ID (charset_ascii
);
5067 c
= coding
->default_char
;
5068 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
5069 charset_list
, &code
, charset
);
5072 if (code
== CHARSET_INVALID_CODE (charset
))
5074 if (charset
== charset_big5
)
5078 c1
= code
>> 8, c2
= code
& 0xFF;
5079 EMIT_TWO_BYTES (c1
, c2
);
5082 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5085 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5086 coding
->produced_char
+= produced_chars
;
5087 coding
->produced
= dst
- coding
->destination
;
5092 /*** 10. CCL handlers ***/
5094 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5095 Return true if a text is encoded in a coding system of which
5096 encoder/decoder are written in CCL program. */
5099 detect_coding_ccl (struct coding_system
*coding
,
5100 struct coding_detection_info
*detect_info
)
5102 const unsigned char *src
= coding
->source
, *src_base
;
5103 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5104 bool multibytep
= coding
->src_multibyte
;
5105 ptrdiff_t consumed_chars
= 0;
5107 unsigned char *valids
;
5108 ptrdiff_t head_ascii
= coding
->head_ascii
;
5111 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5113 coding
= &coding_categories
[coding_category_ccl
];
5114 valids
= CODING_CCL_VALIDS (coding
);
5115 attrs
= CODING_ID_ATTRS (coding
->id
);
5116 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5125 if (c
< 0 || ! valids
[c
])
5127 if ((valids
[c
] > 1))
5128 found
= CATEGORY_MASK_CCL
;
5130 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5134 detect_info
->found
|= found
;
5139 decode_coding_ccl (struct coding_system
*coding
)
5141 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5142 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5143 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5144 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5145 ptrdiff_t consumed_chars
= 0;
5146 bool multibytep
= coding
->src_multibyte
;
5147 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5148 int source_charbuf
[1024];
5149 int source_byteidx
[1025];
5150 Lisp_Object attrs
, charset_list
;
5152 CODING_GET_INFO (coding
, attrs
, charset_list
);
5156 const unsigned char *p
= src
;
5162 while (i
< 1024 && p
< src_end
)
5164 source_byteidx
[i
] = p
- src
;
5165 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5167 source_byteidx
[i
] = p
- src
;
5170 while (i
< 1024 && p
< src_end
)
5171 source_charbuf
[i
++] = *p
++;
5173 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5174 ccl
->last_block
= true;
5175 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5176 charset_map_loaded
= 0;
5177 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5179 if (charset_map_loaded
5180 && (offset
= coding_change_source (coding
)))
5186 charbuf
+= ccl
->produced
;
5188 src
+= source_byteidx
[ccl
->consumed
];
5190 src
+= ccl
->consumed
;
5191 consumed_chars
+= ccl
->consumed
;
5192 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5196 switch (ccl
->status
)
5198 case CCL_STAT_SUSPEND_BY_SRC
:
5199 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5201 case CCL_STAT_SUSPEND_BY_DST
:
5202 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5205 case CCL_STAT_INVALID_CMD
:
5206 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5209 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5212 coding
->consumed_char
+= consumed_chars
;
5213 coding
->consumed
= src
- coding
->source
;
5214 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5218 encode_coding_ccl (struct coding_system
*coding
)
5220 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5221 bool multibytep
= coding
->dst_multibyte
;
5222 int *charbuf
= coding
->charbuf
;
5223 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5224 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5225 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5226 int destination_charbuf
[1024];
5227 ptrdiff_t produced_chars
= 0;
5229 Lisp_Object attrs
, charset_list
;
5231 CODING_GET_INFO (coding
, attrs
, charset_list
);
5232 if (coding
->consumed_char
== coding
->src_chars
5233 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5234 ccl
->last_block
= true;
5240 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5241 charset_map_loaded
= 0;
5242 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5243 charbuf_end
- charbuf
, 1024, charset_list
);
5244 if (charset_map_loaded
5245 && (offset
= coding_change_destination (coding
)))
5249 ASSURE_DESTINATION (ccl
->produced
* 2);
5250 for (i
= 0; i
< ccl
->produced
; i
++)
5251 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5255 ASSURE_DESTINATION (ccl
->produced
);
5256 for (i
= 0; i
< ccl
->produced
; i
++)
5257 *dst
++ = destination_charbuf
[i
] & 0xFF;
5258 produced_chars
+= ccl
->produced
;
5260 charbuf
+= ccl
->consumed
;
5261 if (ccl
->status
== CCL_STAT_QUIT
5262 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5265 while (charbuf
< charbuf_end
);
5267 switch (ccl
->status
)
5269 case CCL_STAT_SUSPEND_BY_SRC
:
5270 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5272 case CCL_STAT_SUSPEND_BY_DST
:
5273 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5276 case CCL_STAT_INVALID_CMD
:
5277 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5280 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5284 coding
->produced_char
+= produced_chars
;
5285 coding
->produced
= dst
- coding
->destination
;
5290 /*** 10, 11. no-conversion handlers ***/
5292 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5295 decode_coding_raw_text (struct coding_system
*coding
)
5298 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5300 coding
->chars_at_source
= 1;
5301 coding
->consumed_char
= coding
->src_chars
;
5302 coding
->consumed
= coding
->src_bytes
;
5303 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5305 coding
->consumed_char
--;
5307 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5310 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5314 encode_coding_raw_text (struct coding_system
*coding
)
5316 bool multibytep
= coding
->dst_multibyte
;
5317 int *charbuf
= coding
->charbuf
;
5318 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5319 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5320 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5321 ptrdiff_t produced_chars
= 0;
5326 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5328 if (coding
->src_multibyte
)
5329 while (charbuf
< charbuf_end
)
5331 ASSURE_DESTINATION (safe_room
);
5333 if (ASCII_CHAR_P (c
))
5334 EMIT_ONE_ASCII_BYTE (c
);
5335 else if (CHAR_BYTE8_P (c
))
5337 c
= CHAR_TO_BYTE8 (c
);
5342 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5344 CHAR_STRING_ADVANCE (c
, p1
);
5347 EMIT_ONE_BYTE (*p0
);
5354 while (charbuf
< charbuf_end
)
5356 ASSURE_DESTINATION (safe_room
);
5363 if (coding
->src_multibyte
)
5365 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5367 while (charbuf
< charbuf_end
)
5369 ASSURE_DESTINATION (safe_room
);
5371 if (ASCII_CHAR_P (c
))
5373 else if (CHAR_BYTE8_P (c
))
5374 *dst
++ = CHAR_TO_BYTE8 (c
);
5376 CHAR_STRING_ADVANCE (c
, dst
);
5381 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5382 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5383 *dst
++ = *charbuf
++;
5385 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5387 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5388 coding
->produced_char
+= produced_chars
;
5389 coding
->produced
= dst
- coding
->destination
;
5393 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5394 Return true if a text is encoded in a charset-based coding system. */
5397 detect_coding_charset (struct coding_system
*coding
,
5398 struct coding_detection_info
*detect_info
)
5400 const unsigned char *src
= coding
->source
, *src_base
;
5401 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5402 bool multibytep
= coding
->src_multibyte
;
5403 ptrdiff_t consumed_chars
= 0;
5404 Lisp_Object attrs
, valids
, name
;
5406 ptrdiff_t head_ascii
= coding
->head_ascii
;
5407 bool check_latin_extra
= 0;
5409 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5411 coding
= &coding_categories
[coding_category_charset
];
5412 attrs
= CODING_ID_ATTRS (coding
->id
);
5413 valids
= AREF (attrs
, coding_attr_charset_valids
);
5414 name
= CODING_ID_NAME (coding
->id
);
5415 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5416 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5417 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5418 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5419 check_latin_extra
= 1;
5421 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5428 struct charset
*charset
;
5435 val
= AREF (valids
, c
);
5441 && check_latin_extra
5442 && (!VECTORP (Vlatin_extra_code_table
)
5443 || NILP (AREF (Vlatin_extra_code_table
, c
))))
5445 found
= CATEGORY_MASK_CHARSET
;
5449 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5450 dim
= CHARSET_DIMENSION (charset
);
5451 for (idx
= 1; idx
< dim
; idx
++)
5456 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5457 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5466 for (; CONSP (val
); val
= XCDR (val
))
5468 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5469 dim
= CHARSET_DIMENSION (charset
);
5475 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5476 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5491 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5495 detect_info
->found
|= found
;
5500 decode_coding_charset (struct coding_system
*coding
)
5502 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5503 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5504 const unsigned char *src_base
;
5505 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5506 /* We may produce one charset annotation in one loop and one more at
5509 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5510 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
5511 bool multibytep
= coding
->src_multibyte
;
5512 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5514 ptrdiff_t char_offset
= coding
->produced_char
;
5515 ptrdiff_t last_offset
= char_offset
;
5516 int last_id
= charset_ascii
;
5518 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5519 int byte_after_cr
= -1;
5521 valids
= AREF (attrs
, coding_attr_charset_valids
);
5527 struct charset
*charset
;
5533 consumed_chars_base
= consumed_chars
;
5535 if (charbuf
>= charbuf_end
)
5537 if (byte_after_cr
>= 0)
5542 if (byte_after_cr
>= 0)
5550 if (eol_dos
&& c
== '\r')
5551 ONE_MORE_BYTE (byte_after_cr
);
5557 val
= AREF (valids
, c
);
5558 if (! INTEGERP (val
) && ! CONSP (val
))
5562 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5563 dim
= CHARSET_DIMENSION (charset
);
5567 code
= (code
<< 8) | c
;
5570 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5575 /* VAL is a list of charset IDs. It is assured that the
5576 list is sorted by charset dimensions (smaller one
5580 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5581 dim
= CHARSET_DIMENSION (charset
);
5585 code
= (code
<< 8) | c
;
5588 CODING_DECODE_CHAR (coding
, src
, src_base
,
5589 src_end
, charset
, code
, c
);
5597 if (charset
->id
!= charset_ascii
5598 && last_id
!= charset
->id
)
5600 if (last_id
!= charset_ascii
)
5601 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5602 last_id
= charset
->id
;
5603 last_offset
= char_offset
;
5612 consumed_chars
= consumed_chars_base
;
5614 *charbuf
++ = c
< 0 ? -c
: ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5619 if (last_id
!= charset_ascii
)
5620 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5621 coding
->consumed_char
+= consumed_chars_base
;
5622 coding
->consumed
= src_base
- coding
->source
;
5623 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5627 encode_coding_charset (struct coding_system
*coding
)
5629 bool multibytep
= coding
->dst_multibyte
;
5630 int *charbuf
= coding
->charbuf
;
5631 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5632 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5633 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5634 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5635 ptrdiff_t produced_chars
= 0;
5636 Lisp_Object attrs
, charset_list
;
5637 bool ascii_compatible
;
5640 CODING_GET_INFO (coding
, attrs
, charset_list
);
5641 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5643 while (charbuf
< charbuf_end
)
5645 struct charset
*charset
;
5648 ASSURE_DESTINATION (safe_room
);
5650 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5651 EMIT_ONE_ASCII_BYTE (c
);
5652 else if (CHAR_BYTE8_P (c
))
5654 c
= CHAR_TO_BYTE8 (c
);
5659 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5664 if (CHARSET_DIMENSION (charset
) == 1)
5665 EMIT_ONE_BYTE (code
);
5666 else if (CHARSET_DIMENSION (charset
) == 2)
5667 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5668 else if (CHARSET_DIMENSION (charset
) == 3)
5669 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5671 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5672 (code
>> 8) & 0xFF, code
& 0xFF);
5676 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5677 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5679 c
= coding
->default_char
;
5685 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5686 coding
->produced_char
+= produced_chars
;
5687 coding
->produced
= dst
- coding
->destination
;
5692 /*** 7. C library functions ***/
5694 /* Setup coding context CODING from information about CODING_SYSTEM.
5695 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5696 CODING_SYSTEM is invalid, signal an error. */
5699 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5702 Lisp_Object eol_type
;
5703 Lisp_Object coding_type
;
5706 if (NILP (coding_system
))
5707 coding_system
= Qundecided
;
5709 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5711 attrs
= CODING_ID_ATTRS (coding
->id
);
5712 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5715 if (VECTORP (eol_type
))
5716 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5717 | CODING_REQUIRE_DETECTION_MASK
);
5718 else if (! EQ (eol_type
, Qunix
))
5719 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5720 | CODING_REQUIRE_ENCODING_MASK
);
5722 coding
->common_flags
= 0;
5723 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5724 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5725 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5726 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5727 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5728 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5730 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5731 coding
->max_charset_id
= SCHARS (val
) - 1;
5732 coding
->safe_charsets
= SDATA (val
);
5733 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5734 coding
->carryover_bytes
= 0;
5735 coding
->raw_destination
= 0;
5737 coding_type
= CODING_ATTR_TYPE (attrs
);
5738 if (EQ (coding_type
, Qundecided
))
5740 coding
->detector
= NULL
;
5741 coding
->decoder
= decode_coding_raw_text
;
5742 coding
->encoder
= encode_coding_raw_text
;
5743 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5744 coding
->spec
.undecided
.inhibit_nbd
5745 = (encode_inhibit_flag
5746 (AREF (attrs
, coding_attr_undecided_inhibit_null_byte_detection
)));
5747 coding
->spec
.undecided
.inhibit_ied
5748 = (encode_inhibit_flag
5749 (AREF (attrs
, coding_attr_undecided_inhibit_iso_escape_detection
)));
5750 coding
->spec
.undecided
.prefer_utf_8
5751 = ! NILP (AREF (attrs
, coding_attr_undecided_prefer_utf_8
));
5753 else if (EQ (coding_type
, Qiso_2022
))
5756 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5758 /* Invoke graphic register 0 to plane 0. */
5759 CODING_ISO_INVOCATION (coding
, 0) = 0;
5760 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5761 CODING_ISO_INVOCATION (coding
, 1)
5762 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5763 /* Setup the initial status of designation. */
5764 for (i
= 0; i
< 4; i
++)
5765 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5766 /* Not single shifting initially. */
5767 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5768 /* Beginning of buffer should also be regarded as bol. */
5769 CODING_ISO_BOL (coding
) = 1;
5770 coding
->detector
= detect_coding_iso_2022
;
5771 coding
->decoder
= decode_coding_iso_2022
;
5772 coding
->encoder
= encode_coding_iso_2022
;
5773 if (flags
& CODING_ISO_FLAG_SAFE
)
5774 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5775 coding
->common_flags
5776 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5777 | CODING_REQUIRE_FLUSHING_MASK
);
5778 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5779 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5780 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5781 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5782 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5784 setup_iso_safe_charsets (attrs
);
5785 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5786 coding
->max_charset_id
= SCHARS (val
) - 1;
5787 coding
->safe_charsets
= SDATA (val
);
5789 CODING_ISO_FLAGS (coding
) = flags
;
5790 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5791 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5792 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5793 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5795 else if (EQ (coding_type
, Qcharset
))
5797 coding
->detector
= detect_coding_charset
;
5798 coding
->decoder
= decode_coding_charset
;
5799 coding
->encoder
= encode_coding_charset
;
5800 coding
->common_flags
5801 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5803 else if (EQ (coding_type
, Qutf_8
))
5805 val
= AREF (attrs
, coding_attr_utf_bom
);
5806 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5807 : EQ (val
, Qt
) ? utf_with_bom
5809 coding
->detector
= detect_coding_utf_8
;
5810 coding
->decoder
= decode_coding_utf_8
;
5811 coding
->encoder
= encode_coding_utf_8
;
5812 coding
->common_flags
5813 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5814 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5815 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5817 else if (EQ (coding_type
, Qutf_16
))
5819 val
= AREF (attrs
, coding_attr_utf_bom
);
5820 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5821 : EQ (val
, Qt
) ? utf_with_bom
5823 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5824 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5825 : utf_16_little_endian
);
5826 CODING_UTF_16_SURROGATE (coding
) = 0;
5827 coding
->detector
= detect_coding_utf_16
;
5828 coding
->decoder
= decode_coding_utf_16
;
5829 coding
->encoder
= encode_coding_utf_16
;
5830 coding
->common_flags
5831 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5832 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5833 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5835 else if (EQ (coding_type
, Qccl
))
5837 coding
->detector
= detect_coding_ccl
;
5838 coding
->decoder
= decode_coding_ccl
;
5839 coding
->encoder
= encode_coding_ccl
;
5840 coding
->common_flags
5841 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5842 | CODING_REQUIRE_FLUSHING_MASK
);
5844 else if (EQ (coding_type
, Qemacs_mule
))
5846 coding
->detector
= detect_coding_emacs_mule
;
5847 coding
->decoder
= decode_coding_emacs_mule
;
5848 coding
->encoder
= encode_coding_emacs_mule
;
5849 coding
->common_flags
5850 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5851 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5852 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5854 Lisp_Object tail
, safe_charsets
;
5855 int max_charset_id
= 0;
5857 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5859 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5860 max_charset_id
= XFASTINT (XCAR (tail
));
5861 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5862 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5863 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5865 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5866 coding
->max_charset_id
= max_charset_id
;
5867 coding
->safe_charsets
= SDATA (safe_charsets
);
5869 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5870 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5872 else if (EQ (coding_type
, Qshift_jis
))
5874 coding
->detector
= detect_coding_sjis
;
5875 coding
->decoder
= decode_coding_sjis
;
5876 coding
->encoder
= encode_coding_sjis
;
5877 coding
->common_flags
5878 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5880 else if (EQ (coding_type
, Qbig5
))
5882 coding
->detector
= detect_coding_big5
;
5883 coding
->decoder
= decode_coding_big5
;
5884 coding
->encoder
= encode_coding_big5
;
5885 coding
->common_flags
5886 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5888 else /* EQ (coding_type, Qraw_text) */
5890 coding
->detector
= NULL
;
5891 coding
->decoder
= decode_coding_raw_text
;
5892 coding
->encoder
= encode_coding_raw_text
;
5893 if (! EQ (eol_type
, Qunix
))
5895 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5896 if (! VECTORP (eol_type
))
5897 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5905 /* Return a list of charsets supported by CODING. */
5908 coding_charset_list (struct coding_system
*coding
)
5910 Lisp_Object attrs
, charset_list
;
5912 CODING_GET_INFO (coding
, attrs
, charset_list
);
5913 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5915 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5917 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5918 charset_list
= Viso_2022_charset_list
;
5920 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5922 charset_list
= Vemacs_mule_charset_list
;
5924 return charset_list
;
5928 /* Return a list of charsets supported by CODING-SYSTEM. */
5931 coding_system_charset_list (Lisp_Object coding_system
)
5934 Lisp_Object attrs
, charset_list
;
5936 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5937 attrs
= CODING_ID_ATTRS (id
);
5939 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5941 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5943 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5944 charset_list
= Viso_2022_charset_list
;
5946 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5948 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5950 charset_list
= Vemacs_mule_charset_list
;
5954 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5956 return charset_list
;
5960 /* Return raw-text or one of its subsidiaries that has the same
5961 eol_type as CODING-SYSTEM. */
5964 raw_text_coding_system (Lisp_Object coding_system
)
5966 Lisp_Object spec
, attrs
;
5967 Lisp_Object eol_type
, raw_text_eol_type
;
5969 if (NILP (coding_system
))
5971 spec
= CODING_SYSTEM_SPEC (coding_system
);
5972 attrs
= AREF (spec
, 0);
5974 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5975 return coding_system
;
5977 eol_type
= AREF (spec
, 2);
5978 if (VECTORP (eol_type
))
5980 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5981 raw_text_eol_type
= AREF (spec
, 2);
5982 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5983 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5984 : AREF (raw_text_eol_type
, 2));
5987 /* Return true if CODING corresponds to raw-text coding-system. */
5990 raw_text_coding_system_p (struct coding_system
*coding
)
5992 return (coding
->decoder
== decode_coding_raw_text
5993 && coding
->encoder
== encode_coding_raw_text
) ? true : false;
5997 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5998 the subsidiary that has the same eol-spec as PARENT (if it is not
5999 nil and specifies end-of-line format) or the system's setting
6000 (system_eol_type). */
6003 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
6005 Lisp_Object spec
, eol_type
;
6007 if (NILP (coding_system
))
6008 coding_system
= Qraw_text
;
6010 CHECK_CODING_SYSTEM (coding_system
);
6011 spec
= CODING_SYSTEM_SPEC (coding_system
);
6012 eol_type
= AREF (spec
, 2);
6013 if (VECTORP (eol_type
))
6015 Lisp_Object parent_eol_type
;
6017 if (! NILP (parent
))
6019 Lisp_Object parent_spec
;
6021 CHECK_CODING_SYSTEM (parent
);
6022 parent_spec
= CODING_SYSTEM_SPEC (parent
);
6023 parent_eol_type
= AREF (parent_spec
, 2);
6024 if (VECTORP (parent_eol_type
))
6025 parent_eol_type
= system_eol_type
;
6028 parent_eol_type
= system_eol_type
;
6029 if (EQ (parent_eol_type
, Qunix
))
6030 coding_system
= AREF (eol_type
, 0);
6031 else if (EQ (parent_eol_type
, Qdos
))
6032 coding_system
= AREF (eol_type
, 1);
6033 else if (EQ (parent_eol_type
, Qmac
))
6034 coding_system
= AREF (eol_type
, 2);
6036 return coding_system
;
6040 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
6041 decided for writing to a process. If not, complement them, and
6042 return a new coding system. */
6045 complement_process_encoding_system (Lisp_Object coding_system
)
6047 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
6048 Lisp_Object spec
, attrs
;
6051 for (i
= 0; i
< 3; i
++)
6054 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
6056 coding_system
= preferred_coding_system ();
6057 spec
= CODING_SYSTEM_SPEC (coding_system
);
6060 attrs
= AREF (spec
, 0);
6061 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
6062 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
6063 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
6064 eol_base
= coding_system
;
6065 if (! NILP (coding_base
) && ! NILP (eol_base
))
6070 /* The original CODING_SYSTEM didn't specify text-conversion or
6071 eol-conversion. Be sure that we return a fully complemented
6073 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
6074 return coding_system
;
6078 /* Emacs has a mechanism to automatically detect a coding system if it
6079 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6080 it's impossible to distinguish some coding systems accurately
6081 because they use the same range of codes. So, at first, coding
6082 systems are categorized into 7, those are:
6084 o coding-category-emacs-mule
6086 The category for a coding system which has the same code range
6087 as Emacs' internal format. Assigned the coding-system (Lisp
6088 symbol) `emacs-mule' by default.
6090 o coding-category-sjis
6092 The category for a coding system which has the same code range
6093 as SJIS. Assigned the coding-system (Lisp
6094 symbol) `japanese-shift-jis' by default.
6096 o coding-category-iso-7
6098 The category for a coding system which has the same code range
6099 as ISO2022 of 7-bit environment. This doesn't use any locking
6100 shift and single shift functions. This can encode/decode all
6101 charsets. Assigned the coding-system (Lisp symbol)
6102 `iso-2022-7bit' by default.
6104 o coding-category-iso-7-tight
6106 Same as coding-category-iso-7 except that this can
6107 encode/decode only the specified charsets.
6109 o coding-category-iso-8-1
6111 The category for a coding system which has the same code range
6112 as ISO2022 of 8-bit environment and graphic plane 1 used only
6113 for DIMENSION1 charset. This doesn't use any locking shift
6114 and single shift functions. Assigned the coding-system (Lisp
6115 symbol) `iso-latin-1' by default.
6117 o coding-category-iso-8-2
6119 The category for a coding system which has the same code range
6120 as ISO2022 of 8-bit environment and graphic plane 1 used only
6121 for DIMENSION2 charset. This doesn't use any locking shift
6122 and single shift functions. Assigned the coding-system (Lisp
6123 symbol) `japanese-iso-8bit' by default.
6125 o coding-category-iso-7-else
6127 The category for a coding system which has the same code range
6128 as ISO2022 of 7-bit environment but uses locking shift or
6129 single shift functions. Assigned the coding-system (Lisp
6130 symbol) `iso-2022-7bit-lock' by default.
6132 o coding-category-iso-8-else
6134 The category for a coding system which has the same code range
6135 as ISO2022 of 8-bit environment but uses locking shift or
6136 single shift functions. Assigned the coding-system (Lisp
6137 symbol) `iso-2022-8bit-ss2' by default.
6139 o coding-category-big5
6141 The category for a coding system which has the same code range
6142 as BIG5. Assigned the coding-system (Lisp symbol)
6143 `cn-big5' by default.
6145 o coding-category-utf-8
6147 The category for a coding system which has the same code range
6148 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6149 symbol) `utf-8' by default.
6151 o coding-category-utf-16-be
6153 The category for a coding system in which a text has an
6154 Unicode signature (cf. Unicode Standard) in the order of BIG
6155 endian at the head. Assigned the coding-system (Lisp symbol)
6156 `utf-16-be' by default.
6158 o coding-category-utf-16-le
6160 The category for a coding system in which a text has an
6161 Unicode signature (cf. Unicode Standard) in the order of
6162 LITTLE endian at the head. Assigned the coding-system (Lisp
6163 symbol) `utf-16-le' by default.
6165 o coding-category-ccl
6167 The category for a coding system of which encoder/decoder is
6168 written in CCL programs. The default value is nil, i.e., no
6169 coding system is assigned.
6171 o coding-category-binary
6173 The category for a coding system not categorized in any of the
6174 above. Assigned the coding-system (Lisp symbol)
6175 `no-conversion' by default.
6177 Each of them is a Lisp symbol and the value is an actual
6178 `coding-system's (this is also a Lisp symbol) assigned by a user.
6179 What Emacs does actually is to detect a category of coding system.
6180 Then, it uses a `coding-system' assigned to it. If Emacs can't
6181 decide only one possible category, it selects a category of the
6182 highest priority. Priorities of categories are also specified by a
6183 user in a Lisp variable `coding-category-list'.
6187 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*coding
,
6191 /* Return the number of ASCII characters at the head of the source.
6192 By side effects, set coding->head_ascii and update
6193 coding->eol_seen. The value of coding->eol_seen is "logical or" of
6194 EOL_SEEN_LF, EOL_SEEN_CR, and EOL_SEEN_CRLF, but the value is
6195 reliable only when all the source bytes are ASCII. */
6198 check_ascii (struct coding_system
*coding
)
6200 const unsigned char *src
, *end
;
6201 Lisp_Object eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6202 int eol_seen
= coding
->eol_seen
;
6204 coding_set_source (coding
);
6205 src
= coding
->source
;
6206 end
= src
+ coding
->src_bytes
;
6208 if (inhibit_eol_conversion
6209 || SYMBOLP (eol_type
))
6211 /* We don't have to check EOL format. */
6212 while (src
< end
&& !( *src
& 0x80))
6215 eol_seen
|= EOL_SEEN_LF
;
6220 end
--; /* We look ahead one byte for "CR LF". */
6232 eol_seen
|= EOL_SEEN_CRLF
;
6236 eol_seen
|= EOL_SEEN_CR
;
6239 eol_seen
|= EOL_SEEN_LF
;
6245 /* All bytes but the last one C are ASCII. */
6249 eol_seen
|= EOL_SEEN_CR
;
6251 eol_seen
|= EOL_SEEN_LF
;
6256 coding
->head_ascii
= src
- coding
->source
;
6257 coding
->eol_seen
= eol_seen
;
6258 return (coding
->head_ascii
);
6262 /* Return the number of characters at the source if all the bytes are
6263 valid UTF-8 (of Unicode range). Otherwise, return -1. By side
6264 effects, update coding->eol_seen. The value of coding->eol_seen is
6265 "logical or" of EOL_SEEN_LF, EOL_SEEN_CR, and EOL_SEEN_CRLF, but
6266 the value is reliable only when all the source bytes are valid
6270 check_utf_8 (struct coding_system
*coding
)
6272 const unsigned char *src
, *end
;
6274 ptrdiff_t nchars
= coding
->head_ascii
;
6276 if (coding
->head_ascii
< 0)
6277 check_ascii (coding
);
6279 coding_set_source (coding
);
6280 src
= coding
->source
+ coding
->head_ascii
;
6281 /* We look ahead one byte for CR LF. */
6282 end
= coding
->source
+ coding
->src_bytes
- 1;
6283 eol_seen
= coding
->eol_seen
;
6288 if (UTF_8_1_OCTET_P (*src
))
6297 eol_seen
|= EOL_SEEN_CRLF
;
6302 eol_seen
|= EOL_SEEN_CR
;
6305 eol_seen
|= EOL_SEEN_LF
;
6308 else if (UTF_8_2_OCTET_LEADING_P (c
))
6310 if (c
< 0xC2 /* overlong sequence */
6312 || ! UTF_8_EXTRA_OCTET_P (src
[1]))
6316 else if (UTF_8_3_OCTET_LEADING_P (c
))
6319 || ! (UTF_8_EXTRA_OCTET_P (src
[1])
6320 && UTF_8_EXTRA_OCTET_P (src
[2])))
6322 c
= (((c
& 0xF) << 12)
6323 | ((src
[1] & 0x3F) << 6) | (src
[2] & 0x3F));
6324 if (c
< 0x800 /* overlong sequence */
6325 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
6329 else if (UTF_8_4_OCTET_LEADING_P (c
))
6332 || ! (UTF_8_EXTRA_OCTET_P (src
[1])
6333 && UTF_8_EXTRA_OCTET_P (src
[2])
6334 && UTF_8_EXTRA_OCTET_P (src
[3])))
6336 c
= (((c
& 0x7) << 18) | ((src
[1] & 0x3F) << 12)
6337 | ((src
[2] & 0x3F) << 6) | (src
[3] & 0x3F));
6338 if (c
< 0x10000 /* overlong sequence */
6339 || c
>= 0x110000) /* non-Unicode character */
6350 if (! UTF_8_1_OCTET_P (*src
))
6354 eol_seen
|= EOL_SEEN_CR
;
6355 else if (*src
== '\n')
6356 eol_seen
|= EOL_SEEN_LF
;
6358 coding
->eol_seen
= eol_seen
;
6363 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6364 SOURCE is encoded. If CATEGORY is one of
6365 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6366 two-byte, else they are encoded by one-byte.
6368 Return one of EOL_SEEN_XXX. */
6370 #define MAX_EOL_CHECK_COUNT 3
6373 detect_eol (const unsigned char *source
, ptrdiff_t src_bytes
,
6374 enum coding_category category
)
6376 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6379 int eol_seen
= EOL_SEEN_NONE
;
6381 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6383 bool msb
= category
== (coding_category_utf_16_le
6384 | coding_category_utf_16_le_nosig
);
6387 while (src
+ 1 < src_end
)
6390 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6395 this_eol
= EOL_SEEN_LF
;
6396 else if (src
+ 3 >= src_end
6397 || src
[msb
+ 2] != 0
6398 || src
[lsb
+ 2] != '\n')
6399 this_eol
= EOL_SEEN_CR
;
6402 this_eol
= EOL_SEEN_CRLF
;
6406 if (eol_seen
== EOL_SEEN_NONE
)
6407 /* This is the first end-of-line. */
6408 eol_seen
= this_eol
;
6409 else if (eol_seen
!= this_eol
)
6411 /* The found type is different from what found before.
6412 Allow for stray ^M characters in DOS EOL files. */
6413 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6414 || (eol_seen
== EOL_SEEN_CRLF
6415 && this_eol
== EOL_SEEN_CR
))
6416 eol_seen
= EOL_SEEN_CRLF
;
6419 eol_seen
= EOL_SEEN_LF
;
6423 if (++total
== MAX_EOL_CHECK_COUNT
)
6430 while (src
< src_end
)
6433 if (c
== '\n' || c
== '\r')
6438 this_eol
= EOL_SEEN_LF
;
6439 else if (src
>= src_end
|| *src
!= '\n')
6440 this_eol
= EOL_SEEN_CR
;
6442 this_eol
= EOL_SEEN_CRLF
, src
++;
6444 if (eol_seen
== EOL_SEEN_NONE
)
6445 /* This is the first end-of-line. */
6446 eol_seen
= this_eol
;
6447 else if (eol_seen
!= this_eol
)
6449 /* The found type is different from what found before.
6450 Allow for stray ^M characters in DOS EOL files. */
6451 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6452 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6453 eol_seen
= EOL_SEEN_CRLF
;
6456 eol_seen
= EOL_SEEN_LF
;
6460 if (++total
== MAX_EOL_CHECK_COUNT
)
6469 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6471 Lisp_Object eol_type
;
6473 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6474 if (! VECTORP (eol_type
))
6475 /* Already adjusted. */
6477 if (eol_seen
& EOL_SEEN_LF
)
6479 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6482 else if (eol_seen
& EOL_SEEN_CRLF
)
6484 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6487 else if (eol_seen
& EOL_SEEN_CR
)
6489 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6495 /* Detect how a text specified in CODING is encoded. If a coding
6496 system is detected, update fields of CODING by the detected coding
6500 detect_coding (struct coding_system
*coding
)
6502 const unsigned char *src
, *src_end
;
6503 unsigned int saved_mode
= coding
->mode
;
6504 Lisp_Object found
= Qnil
;
6505 Lisp_Object eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6507 coding
->consumed
= coding
->consumed_char
= 0;
6508 coding
->produced
= coding
->produced_char
= 0;
6509 coding_set_source (coding
);
6511 src_end
= coding
->source
+ coding
->src_bytes
;
6513 coding
->eol_seen
= EOL_SEEN_NONE
;
6514 /* If we have not yet decided the text encoding type, detect it
6516 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6519 struct coding_detection_info detect_info
;
6520 bool null_byte_found
= 0, eight_bit_found
= 0;
6521 bool inhibit_nbd
= inhibit_flag (coding
->spec
.undecided
.inhibit_nbd
,
6522 inhibit_null_byte_detection
);
6523 bool inhibit_ied
= inhibit_flag (coding
->spec
.undecided
.inhibit_ied
,
6524 inhibit_iso_escape_detection
);
6525 bool prefer_utf_8
= coding
->spec
.undecided
.prefer_utf_8
;
6527 coding
->head_ascii
= 0;
6528 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6529 for (src
= coding
->source
; src
< src_end
; src
++)
6534 eight_bit_found
= 1;
6535 if (null_byte_found
)
6540 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6542 && ! detect_info
.checked
)
6544 if (detect_coding_iso_2022 (coding
, &detect_info
))
6546 /* We have scanned the whole data. */
6547 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6549 /* We didn't find an 8-bit code. We may
6550 have found a null-byte, but it's very
6551 rare that a binary file conforms to
6554 coding
->head_ascii
= src
- coding
->source
;
6556 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6560 else if (! c
&& !inhibit_nbd
)
6562 null_byte_found
= 1;
6563 if (eight_bit_found
)
6566 else if (! disable_ascii_optimization
6567 && ! inhibit_eol_conversion
)
6571 if (src
< src_end
&& src
[1] == '\n')
6573 coding
->eol_seen
|= EOL_SEEN_CRLF
;
6575 if (! eight_bit_found
)
6576 coding
->head_ascii
++;
6579 coding
->eol_seen
|= EOL_SEEN_CR
;
6583 coding
->eol_seen
|= EOL_SEEN_LF
;
6587 if (! eight_bit_found
)
6588 coding
->head_ascii
++;
6590 else if (! eight_bit_found
)
6591 coding
->head_ascii
++;
6594 if (null_byte_found
|| eight_bit_found
6595 || coding
->head_ascii
< coding
->src_bytes
6596 || detect_info
.found
)
6598 enum coding_category category
;
6599 struct coding_system
*this;
6601 if (coding
->head_ascii
== coding
->src_bytes
)
6602 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6603 for (i
= 0; i
< coding_category_raw_text
; i
++)
6605 category
= coding_priorities
[i
];
6606 this = coding_categories
+ category
;
6607 if (detect_info
.found
& (1 << category
))
6612 if (null_byte_found
)
6614 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6615 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6617 else if (prefer_utf_8
6618 && detect_coding_utf_8 (coding
, &detect_info
))
6620 detect_info
.checked
|= ~CATEGORY_MASK_UTF_8
;
6621 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_8
;
6623 for (i
= 0; i
< coding_category_raw_text
; i
++)
6625 category
= coding_priorities
[i
];
6626 this = coding_categories
+ category
;
6627 /* Some of this->detector (e.g. detect_coding_sjis)
6628 require this information. */
6629 coding
->id
= this->id
;
6632 /* No coding system of this category is defined. */
6633 detect_info
.rejected
|= (1 << category
);
6635 else if (category
>= coding_category_raw_text
)
6637 else if (detect_info
.checked
& (1 << category
))
6639 if (detect_info
.found
& (1 << category
))
6642 else if ((*(this->detector
)) (coding
, &detect_info
)
6643 && detect_info
.found
& (1 << category
))
6648 if (i
< coding_category_raw_text
)
6650 if (category
== coding_category_utf_8_auto
)
6652 Lisp_Object coding_systems
;
6654 coding_systems
= AREF (CODING_ID_ATTRS (this->id
),
6655 coding_attr_utf_bom
);
6656 if (CONSP (coding_systems
))
6658 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6659 found
= XCAR (coding_systems
);
6661 found
= XCDR (coding_systems
);
6664 found
= CODING_ID_NAME (this->id
);
6666 else if (category
== coding_category_utf_16_auto
)
6668 Lisp_Object coding_systems
;
6670 coding_systems
= AREF (CODING_ID_ATTRS (this->id
),
6671 coding_attr_utf_bom
);
6672 if (CONSP (coding_systems
))
6674 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6675 found
= XCAR (coding_systems
);
6676 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6677 found
= XCDR (coding_systems
);
6680 found
= CODING_ID_NAME (this->id
);
6683 found
= CODING_ID_NAME (this->id
);
6685 else if (null_byte_found
)
6686 found
= Qno_conversion
;
6687 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6688 == CATEGORY_MASK_ANY
)
6690 else if (detect_info
.rejected
)
6691 for (i
= 0; i
< coding_category_raw_text
; i
++)
6692 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6694 this = coding_categories
+ coding_priorities
[i
];
6695 found
= CODING_ID_NAME (this->id
);
6700 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6701 == coding_category_utf_8_auto
)
6703 Lisp_Object coding_systems
;
6704 struct coding_detection_info detect_info
;
6707 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6708 detect_info
.found
= detect_info
.rejected
= 0;
6709 if (check_ascii (coding
) == coding
->src_bytes
)
6711 if (CONSP (coding_systems
))
6712 found
= XCDR (coding_systems
);
6716 if (CONSP (coding_systems
)
6717 && detect_coding_utf_8 (coding
, &detect_info
))
6719 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6720 found
= XCAR (coding_systems
);
6722 found
= XCDR (coding_systems
);
6726 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6727 == coding_category_utf_16_auto
)
6729 Lisp_Object coding_systems
;
6730 struct coding_detection_info detect_info
;
6733 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6734 detect_info
.found
= detect_info
.rejected
= 0;
6735 coding
->head_ascii
= 0;
6736 if (CONSP (coding_systems
)
6737 && detect_coding_utf_16 (coding
, &detect_info
))
6739 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6740 found
= XCAR (coding_systems
);
6741 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6742 found
= XCDR (coding_systems
);
6748 int specified_eol
= (VECTORP (eol_type
) ? EOL_SEEN_NONE
6749 : EQ (eol_type
, Qdos
) ? EOL_SEEN_CRLF
6750 : EQ (eol_type
, Qmac
) ? EOL_SEEN_CR
6753 setup_coding_system (found
, coding
);
6754 if (specified_eol
!= EOL_SEEN_NONE
)
6755 adjust_coding_eol_type (coding
, specified_eol
);
6758 coding
->mode
= saved_mode
;
6763 decode_eol (struct coding_system
*coding
)
6765 Lisp_Object eol_type
;
6766 unsigned char *p
, *pbeg
, *pend
;
6768 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6769 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6772 if (NILP (coding
->dst_object
))
6773 pbeg
= coding
->destination
;
6775 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6776 pend
= pbeg
+ coding
->produced
;
6778 if (VECTORP (eol_type
))
6780 int eol_seen
= EOL_SEEN_NONE
;
6782 for (p
= pbeg
; p
< pend
; p
++)
6785 eol_seen
|= EOL_SEEN_LF
;
6786 else if (*p
== '\r')
6788 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6790 eol_seen
|= EOL_SEEN_CRLF
;
6794 eol_seen
|= EOL_SEEN_CR
;
6797 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6798 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6799 && (eol_seen
& EOL_SEEN_CR
) != 0
6800 && (eol_seen
& EOL_SEEN_LF
) == 0)
6801 eol_seen
= EOL_SEEN_CRLF
;
6802 else if (eol_seen
!= EOL_SEEN_NONE
6803 && eol_seen
!= EOL_SEEN_LF
6804 && eol_seen
!= EOL_SEEN_CRLF
6805 && eol_seen
!= EOL_SEEN_CR
)
6806 eol_seen
= EOL_SEEN_LF
;
6807 if (eol_seen
!= EOL_SEEN_NONE
)
6808 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6811 if (EQ (eol_type
, Qmac
))
6813 for (p
= pbeg
; p
< pend
; p
++)
6817 else if (EQ (eol_type
, Qdos
))
6820 ptrdiff_t pos
= coding
->dst_pos
;
6821 ptrdiff_t pos_byte
= coding
->dst_pos_byte
;
6822 ptrdiff_t pos_end
= pos_byte
+ coding
->produced
- 1;
6824 /* This assertion is here instead of code, now deleted, that
6825 handled the NILP case, which no longer happens with the
6826 current codebase. */
6827 eassert (!NILP (coding
->dst_object
));
6829 while (pos_byte
< pos_end
)
6833 p
= BYTE_POS_ADDR (pos_byte
);
6834 if (coding
->dst_multibyte
)
6835 incr
= BYTES_BY_CHAR_HEAD (*p
);
6839 if (*p
== '\r' && p
[1] == '\n')
6841 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6848 coding
->produced
-= n
;
6849 coding
->produced_char
-= n
;
6854 /* MAX_LOOKUP's maximum value. MAX_LOOKUP is an int and so cannot
6855 exceed INT_MAX. Also, MAX_LOOKUP is multiplied by sizeof (int) for
6856 alloca, so it cannot exceed MAX_ALLOCA / sizeof (int). */
6857 enum { MAX_LOOKUP_MAX
= min (INT_MAX
, MAX_ALLOCA
/ sizeof (int)) };
6859 /* Return a translation table (or list of them) from coding system
6860 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6864 get_translation_table (Lisp_Object attrs
, bool encodep
, int *max_lookup
)
6866 Lisp_Object standard
, translation_table
;
6869 if (NILP (Venable_character_translation
))
6876 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6877 standard
= Vstandard_translation_table_for_encode
;
6879 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6880 standard
= Vstandard_translation_table_for_decode
;
6881 if (NILP (translation_table
))
6882 translation_table
= standard
;
6885 if (SYMBOLP (translation_table
))
6886 translation_table
= Fget (translation_table
, Qtranslation_table
);
6887 else if (CONSP (translation_table
))
6889 translation_table
= Fcopy_sequence (translation_table
);
6890 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6891 if (SYMBOLP (XCAR (val
)))
6892 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6894 if (CHAR_TABLE_P (standard
))
6896 if (CONSP (translation_table
))
6897 translation_table
= nconc2 (translation_table
, list1 (standard
));
6899 translation_table
= list2 (translation_table
, standard
);
6906 if (CHAR_TABLE_P (translation_table
)
6907 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6909 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6910 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6911 *max_lookup
= min (XFASTINT (val
), MAX_LOOKUP_MAX
);
6913 else if (CONSP (translation_table
))
6917 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6918 if (CHAR_TABLE_P (XCAR (tail
))
6919 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6921 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6922 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6923 *max_lookup
= min (XFASTINT (tailval
), MAX_LOOKUP_MAX
);
6927 return translation_table
;
6930 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6933 if (CHAR_TABLE_P (table)) \
6935 trans = CHAR_TABLE_REF (table, c); \
6936 if (CHARACTERP (trans)) \
6937 c = XFASTINT (trans), trans = Qnil; \
6939 else if (CONSP (table)) \
6943 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6944 if (CHAR_TABLE_P (XCAR (tail))) \
6946 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6947 if (CHARACTERP (trans)) \
6948 c = XFASTINT (trans), trans = Qnil; \
6949 else if (! NILP (trans)) \
6956 /* Return a translation of character(s) at BUF according to TRANS.
6957 TRANS is TO-CHAR, [TO-CHAR ...], or ((FROM . TO) ...) where FROM =
6958 [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...]. The return value
6959 is TO-CHAR or [TO-CHAR ...] if a translation is found, Qnil if not
6960 found, or Qt if BUF is too short to lookup characters in FROM. As
6961 a side effect, if a translation is found, *NCHARS is set to the
6962 number of characters being translated. */
6965 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
, ptrdiff_t *nchars
)
6967 if (INTEGERP (trans
) || VECTORP (trans
))
6972 for (; CONSP (trans
); trans
= XCDR (trans
))
6974 Lisp_Object val
= XCAR (trans
);
6975 Lisp_Object from
= XCAR (val
);
6976 ptrdiff_t len
= ASIZE (from
);
6979 for (i
= 0; i
< len
; i
++)
6981 if (buf
+ i
== buf_end
)
6983 if (XINT (AREF (from
, i
)) != buf
[i
])
6997 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7000 unsigned char *dst
= coding
->destination
+ coding
->produced
;
7001 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
7003 ptrdiff_t produced_chars
= 0;
7006 if (! coding
->chars_at_source
)
7008 /* Source characters are in coding->charbuf. */
7009 int *buf
= coding
->charbuf
;
7010 int *buf_end
= buf
+ coding
->charbuf_used
;
7012 if (EQ (coding
->src_object
, coding
->dst_object
)
7013 && ! NILP (coding
->dst_object
))
7015 eassert (growable_destination (coding
));
7016 coding_set_source (coding
);
7017 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
7020 while (buf
< buf_end
)
7027 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7028 Lisp_Object trans
= Qnil
;
7030 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7033 trans
= get_translation (trans
, buf
, buf_end
, &from_nchars
);
7034 if (INTEGERP (trans
))
7036 else if (VECTORP (trans
))
7038 to_nchars
= ASIZE (trans
);
7039 c
= XINT (AREF (trans
, 0));
7041 else if (EQ (trans
, Qt
) && ! last_block
)
7045 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
7047 eassert (growable_destination (coding
));
7049 if (INT_MULTIPLY_WRAPV (to_nchars
, MAX_MULTIBYTE_LENGTH
,
7051 || INT_ADD_WRAPV (buf_end
- buf
, dst_size
, &dst_size
))
7052 memory_full (SIZE_MAX
);
7053 dst
= alloc_destination (coding
, dst_size
, dst
);
7054 if (EQ (coding
->src_object
, coding
->dst_object
))
7056 coding_set_source (coding
);
7057 dst_end
= (((unsigned char *) coding
->source
)
7058 + coding
->consumed
);
7061 dst_end
= coding
->destination
+ coding
->dst_bytes
;
7064 for (i
= 0; i
< to_nchars
; i
++)
7067 c
= XINT (AREF (trans
, i
));
7068 if (coding
->dst_multibyte
7069 || ! CHAR_BYTE8_P (c
))
7070 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
7072 *dst
++ = CHAR_TO_BYTE8 (c
);
7074 produced_chars
+= to_nchars
;
7078 /* This is an annotation datum. (-C) is the length. */
7081 carryover
= buf_end
- buf
;
7085 /* Source characters are at coding->source. */
7086 const unsigned char *src
= coding
->source
;
7087 const unsigned char *src_end
= src
+ coding
->consumed
;
7089 if (EQ (coding
->dst_object
, coding
->src_object
))
7091 eassert (growable_destination (coding
));
7092 dst_end
= (unsigned char *) src
;
7094 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
7096 if (coding
->src_multibyte
)
7098 bool multibytep
= 1;
7099 ptrdiff_t consumed_chars
= 0;
7103 const unsigned char *src_base
= src
;
7109 eassert (growable_destination (coding
));
7110 if (EQ (coding
->src_object
, coding
->dst_object
))
7111 dst_end
= (unsigned char *) src
;
7114 ptrdiff_t offset
= src
- coding
->source
;
7116 dst
= alloc_destination (coding
, src_end
- src
+ 1,
7118 dst_end
= coding
->destination
+ coding
->dst_bytes
;
7119 coding_set_source (coding
);
7120 src
= coding
->source
+ offset
;
7121 src_end
= coding
->source
+ coding
->consumed
;
7122 if (EQ (coding
->src_object
, coding
->dst_object
))
7123 dst_end
= (unsigned char *) src
;
7133 while (src
< src_end
)
7135 bool multibytep
= 1;
7138 if (dst
>= dst_end
- 1)
7140 eassert (growable_destination (coding
));
7141 if (EQ (coding
->src_object
, coding
->dst_object
))
7142 dst_end
= (unsigned char *) src
;
7143 if (dst
>= dst_end
- 1)
7145 ptrdiff_t offset
= src
- coding
->source
;
7146 ptrdiff_t more_bytes
;
7148 if (EQ (coding
->src_object
, coding
->dst_object
))
7149 more_bytes
= ((src_end
- src
) / 2) + 2;
7151 more_bytes
= src_end
- src
+ 2;
7152 dst
= alloc_destination (coding
, more_bytes
, dst
);
7153 dst_end
= coding
->destination
+ coding
->dst_bytes
;
7154 coding_set_source (coding
);
7155 src
= coding
->source
+ offset
;
7156 src_end
= coding
->source
+ coding
->consumed
;
7157 if (EQ (coding
->src_object
, coding
->dst_object
))
7158 dst_end
= (unsigned char *) src
;
7166 if (!EQ (coding
->src_object
, coding
->dst_object
))
7168 ptrdiff_t require
= coding
->src_bytes
- coding
->dst_bytes
;
7172 ptrdiff_t offset
= src
- coding
->source
;
7174 dst
= alloc_destination (coding
, require
, dst
);
7175 coding_set_source (coding
);
7176 src
= coding
->source
+ offset
;
7177 src_end
= coding
->source
+ coding
->consumed
;
7180 produced_chars
= coding
->consumed_char
;
7181 while (src
< src_end
)
7186 produced
= dst
- (coding
->destination
+ coding
->produced
);
7187 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
7188 insert_from_gap (produced_chars
, produced
, 0);
7189 coding
->produced
+= produced
;
7190 coding
->produced_char
+= produced_chars
;
7194 /* Compose text in CODING->object according to the annotation data at
7195 CHARBUF. CHARBUF is an array:
7196 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
7200 produce_composition (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
7204 enum composition_method method
;
7205 Lisp_Object components
;
7207 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
7208 to
= pos
+ charbuf
[2];
7209 method
= (enum composition_method
) (charbuf
[4]);
7211 if (method
== COMPOSITION_RELATIVE
)
7215 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
7218 if (method
== COMPOSITION_WITH_RULE
)
7219 len
= charbuf
[2] * 3 - 2;
7220 charbuf
+= MAX_ANNOTATION_LENGTH
;
7221 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
7222 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
7224 if (charbuf
[i
] >= 0)
7225 args
[j
] = make_number (charbuf
[i
]);
7229 args
[j
] = make_number (charbuf
[i
] % 0x100);
7232 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
7234 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
7238 /* Put `charset' property on text in CODING->object according to
7239 the annotation data at CHARBUF. CHARBUF is an array:
7240 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
7244 produce_charset (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
7246 ptrdiff_t from
= pos
- charbuf
[2];
7247 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
7249 Fput_text_property (make_number (from
), make_number (pos
),
7250 Qcharset
, CHARSET_NAME (charset
),
7251 coding
->dst_object
);
7254 #define MAX_CHARBUF_SIZE 0x4000
7255 /* How many units decoding functions expect in coding->charbuf at
7256 most. Currently, decode_coding_emacs_mule expects the following
7257 size, and that is the largest value. */
7258 #define MAX_CHARBUF_EXTRA_SIZE ((MAX_ANNOTATION_LENGTH * 3) + 1)
7260 #define ALLOC_CONVERSION_WORK_AREA(coding, size) \
7262 ptrdiff_t units = min ((size) + MAX_CHARBUF_EXTRA_SIZE, \
7263 MAX_CHARBUF_SIZE); \
7264 coding->charbuf = SAFE_ALLOCA (units * sizeof (int)); \
7265 coding->charbuf_size = units; \
7269 produce_annotation (struct coding_system
*coding
, ptrdiff_t pos
)
7271 int *charbuf
= coding
->charbuf
;
7272 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
7274 if (NILP (coding
->dst_object
))
7277 while (charbuf
< charbuf_end
)
7283 int len
= -*charbuf
;
7288 case CODING_ANNOTATE_COMPOSITION_MASK
:
7289 produce_composition (coding
, charbuf
, pos
);
7291 case CODING_ANNOTATE_CHARSET_MASK
:
7292 produce_charset (coding
, charbuf
, pos
);
7302 /* Decode the data at CODING->src_object into CODING->dst_object.
7303 CODING->src_object is a buffer, a string, or nil.
7304 CODING->dst_object is a buffer.
7306 If CODING->src_object is a buffer, it must be the current buffer.
7307 In this case, if CODING->src_pos is positive, it is a position of
7308 the source text in the buffer, otherwise, the source text is in the
7309 gap area of the buffer, and CODING->src_pos specifies the offset of
7310 the text from GPT (which must be the same as PT). If this is the
7311 same buffer as CODING->dst_object, CODING->src_pos must be
7314 If CODING->src_object is a string, CODING->src_pos is an index to
7317 If CODING->src_object is nil, CODING->source must already point to
7318 the non-relocatable memory area. In this case, CODING->src_pos is
7319 an offset from CODING->source.
7321 The decoded data is inserted at the current point of the buffer
7326 decode_coding (struct coding_system
*coding
)
7329 Lisp_Object undo_list
;
7330 Lisp_Object translation_table
;
7331 struct ccl_spec cclspec
;
7337 if (BUFFERP (coding
->src_object
)
7338 && coding
->src_pos
> 0
7339 && coding
->src_pos
< GPT
7340 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7341 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7344 if (BUFFERP (coding
->dst_object
))
7346 set_buffer_internal (XBUFFER (coding
->dst_object
));
7348 move_gap_both (PT
, PT_BYTE
);
7350 /* We must disable undo_list in order to record the whole insert
7351 transaction via record_insert at the end. But doing so also
7352 disables the recording of the first change to the undo_list.
7353 Therefore we check for first change here and record it via
7354 record_first_change if needed. */
7355 if (MODIFF
<= SAVE_MODIFF
)
7356 record_first_change ();
7358 undo_list
= BVAR (current_buffer
, undo_list
);
7359 bset_undo_list (current_buffer
, Qt
);
7362 coding
->consumed
= coding
->consumed_char
= 0;
7363 coding
->produced
= coding
->produced_char
= 0;
7364 coding
->chars_at_source
= 0;
7365 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7367 ALLOC_CONVERSION_WORK_AREA (coding
, coding
->src_bytes
);
7369 attrs
= CODING_ID_ATTRS (coding
->id
);
7370 translation_table
= get_translation_table (attrs
, 0, NULL
);
7373 if (coding
->decoder
== decode_coding_ccl
)
7375 coding
->spec
.ccl
= &cclspec
;
7376 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7380 ptrdiff_t pos
= coding
->dst_pos
+ coding
->produced_char
;
7382 coding_set_source (coding
);
7383 coding
->annotated
= 0;
7384 coding
->charbuf_used
= carryover
;
7385 (*(coding
->decoder
)) (coding
);
7386 coding_set_destination (coding
);
7387 carryover
= produce_chars (coding
, translation_table
, 0);
7388 if (coding
->annotated
)
7389 produce_annotation (coding
, pos
);
7390 for (i
= 0; i
< carryover
; i
++)
7392 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7394 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7395 || (coding
->consumed
< coding
->src_bytes
7396 && (coding
->result
== CODING_RESULT_SUCCESS
7397 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7401 coding_set_destination (coding
);
7402 coding
->charbuf_used
= carryover
;
7403 produce_chars (coding
, translation_table
, 1);
7406 coding
->carryover_bytes
= 0;
7407 if (coding
->consumed
< coding
->src_bytes
)
7409 ptrdiff_t nbytes
= coding
->src_bytes
- coding
->consumed
;
7410 const unsigned char *src
;
7412 coding_set_source (coding
);
7413 coding_set_destination (coding
);
7414 src
= coding
->source
+ coding
->consumed
;
7416 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7418 /* Flush out unprocessed data as binary chars. We are sure
7419 that the number of data is less than the size of
7421 coding
->charbuf_used
= 0;
7422 coding
->chars_at_source
= 0;
7424 while (nbytes
-- > 0)
7428 /* Copy raw bytes in their 2-byte forms from multibyte
7429 text as single characters. */
7430 if (coding
->src_multibyte
7431 && CHAR_BYTE8_HEAD_P (*src
) && nbytes
> 0)
7433 c
= STRING_CHAR_ADVANCE (src
);
7441 c
= BYTE8_TO_CHAR (c
);
7443 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7445 produce_chars (coding
, Qnil
, 1);
7449 /* Record unprocessed bytes in coding->carryover. We are
7450 sure that the number of data is less than the size of
7451 coding->carryover. */
7452 unsigned char *p
= coding
->carryover
;
7454 if (nbytes
> sizeof coding
->carryover
)
7455 nbytes
= sizeof coding
->carryover
;
7456 coding
->carryover_bytes
= nbytes
;
7457 while (nbytes
-- > 0)
7460 coding
->consumed
= coding
->src_bytes
;
7463 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7464 && !inhibit_eol_conversion
)
7465 decode_eol (coding
);
7466 if (BUFFERP (coding
->dst_object
))
7468 bset_undo_list (current_buffer
, undo_list
);
7469 record_insert (coding
->dst_pos
, coding
->produced_char
);
7476 /* Extract an annotation datum from a composition starting at POS and
7477 ending before LIMIT of CODING->src_object (buffer or string), store
7478 the data in BUF, set *STOP to a starting position of the next
7479 composition (if any) or to LIMIT, and return the address of the
7480 next element of BUF.
7482 If such an annotation is not found, set *STOP to a starting
7483 position of a composition after POS (if any) or to LIMIT, and
7487 handle_composition_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7488 struct coding_system
*coding
, int *buf
,
7491 ptrdiff_t start
, end
;
7494 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7497 else if (start
> pos
)
7503 /* We found a composition. Store the corresponding
7504 annotation data in BUF. */
7506 enum composition_method method
= composition_method (prop
);
7507 int nchars
= COMPOSITION_LENGTH (prop
);
7509 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7510 if (method
!= COMPOSITION_RELATIVE
)
7512 Lisp_Object components
;
7513 ptrdiff_t i
, len
, i_byte
;
7515 components
= COMPOSITION_COMPONENTS (prop
);
7516 if (VECTORP (components
))
7518 len
= ASIZE (components
);
7519 for (i
= 0; i
< len
; i
++)
7520 *buf
++ = XINT (AREF (components
, i
));
7522 else if (STRINGP (components
))
7524 len
= SCHARS (components
);
7528 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7532 else if (INTEGERP (components
))
7535 *buf
++ = XINT (components
);
7537 else if (CONSP (components
))
7539 for (len
= 0; CONSP (components
);
7540 len
++, components
= XCDR (components
))
7541 *buf
++ = XINT (XCAR (components
));
7549 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7560 /* Extract an annotation datum from a text property `charset' at POS of
7561 CODING->src_object (buffer of string), store the data in BUF, set
7562 *STOP to the position where the value of `charset' property changes
7563 (limiting by LIMIT), and return the address of the next element of
7566 If the property value is nil, set *STOP to the position where the
7567 property value is non-nil (limiting by LIMIT), and return BUF. */
7570 handle_charset_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7571 struct coding_system
*coding
, int *buf
,
7574 Lisp_Object val
, next
;
7577 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7578 if (! NILP (val
) && CHARSETP (val
))
7579 id
= XINT (CHARSET_SYMBOL_ID (val
));
7582 ADD_CHARSET_DATA (buf
, 0, id
);
7583 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7585 make_number (limit
));
7586 *stop
= XINT (next
);
7592 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7595 int *buf
= coding
->charbuf
;
7596 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7597 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7598 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7599 ptrdiff_t pos
= coding
->src_pos
+ coding
->consumed_char
;
7600 ptrdiff_t end_pos
= coding
->src_pos
+ coding
->src_chars
;
7601 bool multibytep
= coding
->src_multibyte
;
7602 Lisp_Object eol_type
;
7604 ptrdiff_t stop
, stop_composition
, stop_charset
;
7605 int *lookup_buf
= NULL
;
7607 if (! NILP (translation_table
))
7608 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7610 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7611 if (VECTORP (eol_type
))
7614 /* Note: composition handling is not yet implemented. */
7615 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7617 if (NILP (coding
->src_object
))
7618 stop
= stop_composition
= stop_charset
= end_pos
;
7621 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7622 stop
= stop_composition
= pos
;
7624 stop
= stop_composition
= end_pos
;
7625 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7626 stop
= stop_charset
= pos
;
7628 stop_charset
= end_pos
;
7631 /* Compensate for CRLF and conversion. */
7632 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7633 while (buf
< buf_end
)
7641 if (pos
== stop_composition
)
7642 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7643 buf
, &stop_composition
);
7644 if (pos
== stop_charset
)
7645 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7646 buf
, &stop_charset
);
7647 stop
= (stop_composition
< stop_charset
7648 ? stop_composition
: stop_charset
);
7655 if (coding
->encoder
== encode_coding_raw_text
7656 || coding
->encoder
== encode_coding_ccl
)
7658 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7659 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7661 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7664 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7665 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7667 if (! EQ (eol_type
, Qunix
))
7671 if (EQ (eol_type
, Qdos
))
7679 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7684 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7685 int *lookup_buf_end
;
7686 const unsigned char *p
= src
;
7690 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7691 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7692 lookup_buf_end
= lookup_buf
+ i
;
7693 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
,
7695 if (INTEGERP (trans
))
7697 else if (VECTORP (trans
))
7699 to_nchars
= ASIZE (trans
);
7700 if (buf_end
- buf
< to_nchars
)
7702 c
= XINT (AREF (trans
, 0));
7707 for (i
= 1; i
< to_nchars
; i
++)
7708 *buf
++ = XINT (AREF (trans
, i
));
7709 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7710 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7714 coding
->consumed
= src
- coding
->source
;
7715 coding
->consumed_char
= pos
- coding
->src_pos
;
7716 coding
->charbuf_used
= buf
- coding
->charbuf
;
7717 coding
->chars_at_source
= 0;
7721 /* Encode the text at CODING->src_object into CODING->dst_object.
7722 CODING->src_object is a buffer or a string.
7723 CODING->dst_object is a buffer or nil.
7725 If CODING->src_object is a buffer, it must be the current buffer.
7726 In this case, if CODING->src_pos is positive, it is a position of
7727 the source text in the buffer, otherwise. the source text is in the
7728 gap area of the buffer, and coding->src_pos specifies the offset of
7729 the text from GPT (which must be the same as PT). If this is the
7730 same buffer as CODING->dst_object, CODING->src_pos must be
7731 negative and CODING should not have `pre-write-conversion'.
7733 If CODING->src_object is a string, CODING should not have
7734 `pre-write-conversion'.
7736 If CODING->dst_object is a buffer, the encoded data is inserted at
7737 the current point of that buffer.
7739 If CODING->dst_object is nil, the encoded data is placed at the
7740 memory area specified by CODING->destination. */
7743 encode_coding (struct coding_system
*coding
)
7746 Lisp_Object translation_table
;
7748 struct ccl_spec cclspec
;
7752 attrs
= CODING_ID_ATTRS (coding
->id
);
7753 if (coding
->encoder
== encode_coding_raw_text
)
7754 translation_table
= Qnil
, max_lookup
= 0;
7756 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7758 if (BUFFERP (coding
->dst_object
))
7760 set_buffer_internal (XBUFFER (coding
->dst_object
));
7761 coding
->dst_multibyte
7762 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7765 coding
->consumed
= coding
->consumed_char
= 0;
7766 coding
->produced
= coding
->produced_char
= 0;
7767 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7769 ALLOC_CONVERSION_WORK_AREA (coding
, coding
->src_chars
);
7771 if (coding
->encoder
== encode_coding_ccl
)
7773 coding
->spec
.ccl
= &cclspec
;
7774 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7777 coding_set_source (coding
);
7778 consume_chars (coding
, translation_table
, max_lookup
);
7779 coding_set_destination (coding
);
7780 (*(coding
->encoder
)) (coding
);
7781 } while (coding
->consumed_char
< coding
->src_chars
);
7783 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7784 insert_from_gap (coding
->produced_char
, coding
->produced
, 0);
7790 /* Name (or base name) of work buffer for code conversion. */
7791 static Lisp_Object Vcode_conversion_workbuf_name
;
7793 /* A working buffer used by the top level conversion. Once it is
7794 created, it is never destroyed. It has the name
7795 Vcode_conversion_workbuf_name. The other working buffers are
7796 destroyed after the use is finished, and their names are modified
7797 versions of Vcode_conversion_workbuf_name. */
7798 static Lisp_Object Vcode_conversion_reused_workbuf
;
7800 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7801 static bool reused_workbuf_in_use
;
7804 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7805 multibyteness of returning buffer. */
7808 make_conversion_work_buffer (bool multibyte
)
7810 Lisp_Object name
, workbuf
;
7811 struct buffer
*current
;
7813 if (reused_workbuf_in_use
)
7815 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7816 workbuf
= Fget_buffer_create (name
);
7820 reused_workbuf_in_use
= 1;
7821 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7822 Vcode_conversion_reused_workbuf
7823 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7824 workbuf
= Vcode_conversion_reused_workbuf
;
7826 current
= current_buffer
;
7827 set_buffer_internal (XBUFFER (workbuf
));
7828 /* We can't allow modification hooks to run in the work buffer. For
7829 instance, directory_files_internal assumes that file decoding
7830 doesn't compile new regexps. */
7831 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7833 bset_undo_list (current_buffer
, Qt
);
7834 bset_enable_multibyte_characters (current_buffer
, multibyte
? Qt
: Qnil
);
7835 set_buffer_internal (current
);
7841 code_conversion_restore (Lisp_Object arg
)
7843 Lisp_Object current
, workbuf
;
7845 current
= XCAR (arg
);
7846 workbuf
= XCDR (arg
);
7847 if (! NILP (workbuf
))
7849 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7850 reused_workbuf_in_use
= 0;
7852 Fkill_buffer (workbuf
);
7854 set_buffer_internal (XBUFFER (current
));
7858 code_conversion_save (bool with_work_buf
, bool multibyte
)
7860 Lisp_Object workbuf
= Qnil
;
7863 workbuf
= make_conversion_work_buffer (multibyte
);
7864 record_unwind_protect (code_conversion_restore
,
7865 Fcons (Fcurrent_buffer (), workbuf
));
7870 coding_restore_undo_list (Lisp_Object arg
)
7872 Lisp_Object undo_list
= XCAR (arg
);
7873 struct buffer
*buf
= XBUFFER (XCDR (arg
));
7875 bset_undo_list (buf
, undo_list
);
7879 decode_coding_gap (struct coding_system
*coding
,
7880 ptrdiff_t chars
, ptrdiff_t bytes
)
7882 ptrdiff_t count
= SPECPDL_INDEX ();
7885 coding
->src_object
= Fcurrent_buffer ();
7886 coding
->src_chars
= chars
;
7887 coding
->src_bytes
= bytes
;
7888 coding
->src_pos
= -chars
;
7889 coding
->src_pos_byte
= -bytes
;
7890 coding
->src_multibyte
= chars
< bytes
;
7891 coding
->dst_object
= coding
->src_object
;
7892 coding
->dst_pos
= PT
;
7893 coding
->dst_pos_byte
= PT_BYTE
;
7894 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7896 coding
->head_ascii
= -1;
7897 coding
->detected_utf8_bytes
= coding
->detected_utf8_chars
= -1;
7898 coding
->eol_seen
= EOL_SEEN_NONE
;
7899 if (CODING_REQUIRE_DETECTION (coding
))
7900 detect_coding (coding
);
7901 attrs
= CODING_ID_ATTRS (coding
->id
);
7902 if (! disable_ascii_optimization
7903 && ! coding
->src_multibyte
7904 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
7905 && NILP (CODING_ATTR_POST_READ (attrs
))
7906 && NILP (get_translation_table (attrs
, 0, NULL
)))
7908 chars
= coding
->head_ascii
;
7910 chars
= check_ascii (coding
);
7913 /* There exists a non-ASCII byte. */
7914 if (EQ (CODING_ATTR_TYPE (attrs
), Qutf_8
)
7915 && coding
->detected_utf8_bytes
== coding
->src_bytes
)
7917 if (coding
->detected_utf8_chars
>= 0)
7918 chars
= coding
->detected_utf8_chars
;
7920 chars
= check_utf_8 (coding
);
7921 if (CODING_UTF_8_BOM (coding
) != utf_without_bom
7922 && coding
->head_ascii
== 0
7923 && coding
->source
[0] == UTF_8_BOM_1
7924 && coding
->source
[1] == UTF_8_BOM_2
7925 && coding
->source
[2] == UTF_8_BOM_3
)
7929 coding
->src_bytes
-= 3;
7937 Lisp_Object eol_type
;
7939 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
7940 if (VECTORP (eol_type
))
7942 if (coding
->eol_seen
!= EOL_SEEN_NONE
)
7943 eol_type
= adjust_coding_eol_type (coding
, coding
->eol_seen
);
7945 if (EQ (eol_type
, Qmac
))
7947 unsigned char *src_end
= GAP_END_ADDR
;
7948 unsigned char *src
= src_end
- coding
->src_bytes
;
7950 while (src
< src_end
)
7956 else if (EQ (eol_type
, Qdos
))
7958 unsigned char *src
= GAP_END_ADDR
;
7959 unsigned char *src_beg
= src
- coding
->src_bytes
;
7960 unsigned char *dst
= src
;
7963 while (src_beg
< src
)
7966 if (*src
== '\n' && src
> src_beg
&& src
[-1] == '\r')
7973 coding
->produced
= bytes
;
7974 coding
->produced_char
= chars
;
7975 insert_from_gap (chars
, bytes
, 1);
7979 code_conversion_save (0, 0);
7981 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7982 current_buffer
->text
->inhibit_shrinking
= 1;
7983 decode_coding (coding
);
7984 current_buffer
->text
->inhibit_shrinking
= 0;
7986 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7988 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7990 Lisp_Object undo_list
= BVAR (current_buffer
, undo_list
);
7991 ptrdiff_t count1
= SPECPDL_INDEX ();
7993 record_unwind_protect (coding_restore_undo_list
,
7994 Fcons (undo_list
, Fcurrent_buffer ()));
7995 bset_undo_list (current_buffer
, Qt
);
7996 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7997 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7998 make_number (coding
->produced_char
));
8000 coding
->produced_char
+= Z
- prev_Z
;
8001 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
8002 unbind_to (count1
, Qnil
);
8005 unbind_to (count
, Qnil
);
8009 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
8010 SRC_OBJECT into DST_OBJECT by coding context CODING.
8012 SRC_OBJECT is a buffer, a string, or Qnil.
8014 If it is a buffer, the text is at point of the buffer. FROM and TO
8015 are positions in the buffer.
8017 If it is a string, the text is at the beginning of the string.
8018 FROM and TO are indices to the string.
8020 If it is nil, the text is at coding->source. FROM and TO are
8021 indices to coding->source.
8023 DST_OBJECT is a buffer, Qt, or Qnil.
8025 If it is a buffer, the decoded text is inserted at point of the
8026 buffer. If the buffer is the same as SRC_OBJECT, the source text
8029 If it is Qt, a string is made from the decoded text, and
8030 set in CODING->dst_object.
8032 If it is Qnil, the decoded text is stored at CODING->destination.
8033 The caller must allocate CODING->dst_bytes bytes at
8034 CODING->destination by xmalloc. If the decoded text is longer than
8035 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
8039 decode_coding_object (struct coding_system
*coding
,
8040 Lisp_Object src_object
,
8041 ptrdiff_t from
, ptrdiff_t from_byte
,
8042 ptrdiff_t to
, ptrdiff_t to_byte
,
8043 Lisp_Object dst_object
)
8045 ptrdiff_t count
= SPECPDL_INDEX ();
8046 unsigned char *destination UNINIT
;
8047 ptrdiff_t dst_bytes UNINIT
;
8048 ptrdiff_t chars
= to
- from
;
8049 ptrdiff_t bytes
= to_byte
- from_byte
;
8051 ptrdiff_t saved_pt
= -1, saved_pt_byte UNINIT
;
8052 bool need_marker_adjustment
= 0;
8053 Lisp_Object old_deactivate_mark
;
8055 old_deactivate_mark
= Vdeactivate_mark
;
8057 if (NILP (dst_object
))
8059 destination
= coding
->destination
;
8060 dst_bytes
= coding
->dst_bytes
;
8063 coding
->src_object
= src_object
;
8064 coding
->src_chars
= chars
;
8065 coding
->src_bytes
= bytes
;
8066 coding
->src_multibyte
= chars
< bytes
;
8068 if (STRINGP (src_object
))
8070 coding
->src_pos
= from
;
8071 coding
->src_pos_byte
= from_byte
;
8073 else if (BUFFERP (src_object
))
8075 set_buffer_internal (XBUFFER (src_object
));
8077 move_gap_both (from
, from_byte
);
8078 if (EQ (src_object
, dst_object
))
8080 struct Lisp_Marker
*tail
;
8082 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8084 tail
->need_adjustment
8085 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
8086 need_marker_adjustment
|= tail
->need_adjustment
;
8088 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8089 TEMP_SET_PT_BOTH (from
, from_byte
);
8090 current_buffer
->text
->inhibit_shrinking
= 1;
8091 del_range_both (from
, from_byte
, to
, to_byte
, 1);
8092 coding
->src_pos
= -chars
;
8093 coding
->src_pos_byte
= -bytes
;
8097 coding
->src_pos
= from
;
8098 coding
->src_pos_byte
= from_byte
;
8102 if (CODING_REQUIRE_DETECTION (coding
))
8103 detect_coding (coding
);
8104 attrs
= CODING_ID_ATTRS (coding
->id
);
8106 if (EQ (dst_object
, Qt
)
8107 || (! NILP (CODING_ATTR_POST_READ (attrs
))
8108 && NILP (dst_object
)))
8110 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
8111 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
8112 coding
->dst_pos
= BEG
;
8113 coding
->dst_pos_byte
= BEG_BYTE
;
8115 else if (BUFFERP (dst_object
))
8117 code_conversion_save (0, 0);
8118 coding
->dst_object
= dst_object
;
8119 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
8120 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
8121 coding
->dst_multibyte
8122 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
8126 code_conversion_save (0, 0);
8127 coding
->dst_object
= Qnil
;
8128 /* Most callers presume this will return a multibyte result, and they
8129 won't use `binary' or `raw-text' anyway, so let's not worry about
8130 CODING_FOR_UNIBYTE. */
8131 coding
->dst_multibyte
= 1;
8134 decode_coding (coding
);
8136 if (BUFFERP (coding
->dst_object
))
8137 set_buffer_internal (XBUFFER (coding
->dst_object
));
8139 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
8141 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
8143 Lisp_Object undo_list
= BVAR (current_buffer
, undo_list
);
8144 ptrdiff_t count1
= SPECPDL_INDEX ();
8146 record_unwind_protect (coding_restore_undo_list
,
8147 Fcons (undo_list
, Fcurrent_buffer ()));
8148 bset_undo_list (current_buffer
, Qt
);
8149 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
8150 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
8151 make_number (coding
->produced_char
));
8153 coding
->produced_char
+= Z
- prev_Z
;
8154 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
8155 unbind_to (count1
, Qnil
);
8158 if (EQ (dst_object
, Qt
))
8160 coding
->dst_object
= Fbuffer_string ();
8162 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
8164 set_buffer_internal (XBUFFER (coding
->dst_object
));
8165 if (dst_bytes
< coding
->produced
)
8167 eassert (coding
->produced
> 0);
8168 destination
= xrealloc (destination
, coding
->produced
);
8169 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
8170 move_gap_both (BEGV
, BEGV_BYTE
);
8171 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
8172 coding
->destination
= destination
;
8178 /* This is the case of:
8179 (BUFFERP (src_object) && EQ (src_object, dst_object))
8180 As we have moved PT while replacing the original buffer
8181 contents, we must recover it now. */
8182 set_buffer_internal (XBUFFER (src_object
));
8183 current_buffer
->text
->inhibit_shrinking
= 0;
8184 if (saved_pt
< from
)
8185 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8186 else if (saved_pt
< from
+ chars
)
8187 TEMP_SET_PT_BOTH (from
, from_byte
);
8188 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8189 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8190 saved_pt_byte
+ (coding
->produced
- bytes
));
8192 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8193 saved_pt_byte
+ (coding
->produced
- bytes
));
8195 if (need_marker_adjustment
)
8197 struct Lisp_Marker
*tail
;
8199 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8200 if (tail
->need_adjustment
)
8202 tail
->need_adjustment
= 0;
8203 if (tail
->insertion_type
)
8205 tail
->bytepos
= from_byte
;
8206 tail
->charpos
= from
;
8210 tail
->bytepos
= from_byte
+ coding
->produced
;
8212 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8213 ? tail
->bytepos
: from
+ coding
->produced_char
);
8219 Vdeactivate_mark
= old_deactivate_mark
;
8220 unbind_to (count
, coding
->dst_object
);
8225 encode_coding_object (struct coding_system
*coding
,
8226 Lisp_Object src_object
,
8227 ptrdiff_t from
, ptrdiff_t from_byte
,
8228 ptrdiff_t to
, ptrdiff_t to_byte
,
8229 Lisp_Object dst_object
)
8231 ptrdiff_t count
= SPECPDL_INDEX ();
8232 ptrdiff_t chars
= to
- from
;
8233 ptrdiff_t bytes
= to_byte
- from_byte
;
8235 ptrdiff_t saved_pt
= -1, saved_pt_byte
;
8236 bool need_marker_adjustment
= 0;
8237 bool kill_src_buffer
= 0;
8238 Lisp_Object old_deactivate_mark
;
8240 old_deactivate_mark
= Vdeactivate_mark
;
8242 coding
->src_object
= src_object
;
8243 coding
->src_chars
= chars
;
8244 coding
->src_bytes
= bytes
;
8245 coding
->src_multibyte
= chars
< bytes
;
8247 attrs
= CODING_ID_ATTRS (coding
->id
);
8249 if (EQ (src_object
, dst_object
))
8251 struct Lisp_Marker
*tail
;
8253 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8255 tail
->need_adjustment
8256 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
8257 need_marker_adjustment
|= tail
->need_adjustment
;
8261 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
8263 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
8264 set_buffer_internal (XBUFFER (coding
->src_object
));
8265 if (STRINGP (src_object
))
8266 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
8267 else if (BUFFERP (src_object
))
8268 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
8270 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
8272 if (EQ (src_object
, dst_object
))
8274 set_buffer_internal (XBUFFER (src_object
));
8275 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8276 del_range_both (from
, from_byte
, to
, to_byte
, 1);
8277 set_buffer_internal (XBUFFER (coding
->src_object
));
8280 safe_call2 (CODING_ATTR_PRE_WRITE (attrs
),
8281 make_number (BEG
), make_number (Z
));
8282 if (XBUFFER (coding
->src_object
) != current_buffer
)
8283 kill_src_buffer
= 1;
8284 coding
->src_object
= Fcurrent_buffer ();
8286 move_gap_both (BEG
, BEG_BYTE
);
8287 coding
->src_chars
= Z
- BEG
;
8288 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
8289 coding
->src_pos
= BEG
;
8290 coding
->src_pos_byte
= BEG_BYTE
;
8291 coding
->src_multibyte
= Z
< Z_BYTE
;
8293 else if (STRINGP (src_object
))
8295 code_conversion_save (0, 0);
8296 coding
->src_pos
= from
;
8297 coding
->src_pos_byte
= from_byte
;
8299 else if (BUFFERP (src_object
))
8301 code_conversion_save (0, 0);
8302 set_buffer_internal (XBUFFER (src_object
));
8303 if (EQ (src_object
, dst_object
))
8305 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8306 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
8307 coding
->src_pos
= 0;
8308 coding
->src_pos_byte
= 0;
8312 if (from
< GPT
&& to
>= GPT
)
8313 move_gap_both (from
, from_byte
);
8314 coding
->src_pos
= from
;
8315 coding
->src_pos_byte
= from_byte
;
8320 code_conversion_save (0, 0);
8321 coding
->src_pos
= from
;
8322 coding
->src_pos_byte
= from_byte
;
8325 if (BUFFERP (dst_object
))
8327 coding
->dst_object
= dst_object
;
8328 if (EQ (src_object
, dst_object
))
8330 coding
->dst_pos
= from
;
8331 coding
->dst_pos_byte
= from_byte
;
8335 struct buffer
*current
= current_buffer
;
8337 set_buffer_temp (XBUFFER (dst_object
));
8338 coding
->dst_pos
= PT
;
8339 coding
->dst_pos_byte
= PT_BYTE
;
8340 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
8341 set_buffer_temp (current
);
8343 coding
->dst_multibyte
8344 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
8346 else if (EQ (dst_object
, Qt
))
8348 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
8349 coding
->dst_object
= Qnil
;
8350 coding
->destination
= xmalloc (dst_bytes
);
8351 coding
->dst_bytes
= dst_bytes
;
8352 coding
->dst_multibyte
= 0;
8356 coding
->dst_object
= Qnil
;
8357 coding
->dst_multibyte
= 0;
8360 encode_coding (coding
);
8362 if (EQ (dst_object
, Qt
))
8364 if (BUFFERP (coding
->dst_object
))
8365 coding
->dst_object
= Fbuffer_string ();
8366 else if (coding
->raw_destination
)
8367 /* This is used to avoid creating huge Lisp string.
8368 NOTE: caller who sets `raw_destination' is also
8369 responsible for freeing `destination' buffer. */
8370 coding
->dst_object
= Qnil
;
8374 = make_unibyte_string ((char *) coding
->destination
,
8376 xfree (coding
->destination
);
8382 /* This is the case of:
8383 (BUFFERP (src_object) && EQ (src_object, dst_object))
8384 As we have moved PT while replacing the original buffer
8385 contents, we must recover it now. */
8386 set_buffer_internal (XBUFFER (src_object
));
8387 if (saved_pt
< from
)
8388 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8389 else if (saved_pt
< from
+ chars
)
8390 TEMP_SET_PT_BOTH (from
, from_byte
);
8391 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8392 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8393 saved_pt_byte
+ (coding
->produced
- bytes
));
8395 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8396 saved_pt_byte
+ (coding
->produced
- bytes
));
8398 if (need_marker_adjustment
)
8400 struct Lisp_Marker
*tail
;
8402 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8403 if (tail
->need_adjustment
)
8405 tail
->need_adjustment
= 0;
8406 if (tail
->insertion_type
)
8408 tail
->bytepos
= from_byte
;
8409 tail
->charpos
= from
;
8413 tail
->bytepos
= from_byte
+ coding
->produced
;
8415 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8416 ? tail
->bytepos
: from
+ coding
->produced_char
);
8422 if (kill_src_buffer
)
8423 Fkill_buffer (coding
->src_object
);
8425 Vdeactivate_mark
= old_deactivate_mark
;
8426 unbind_to (count
, Qnil
);
8431 preferred_coding_system (void)
8433 int id
= coding_categories
[coding_priorities
[0]].id
;
8435 return CODING_ID_NAME (id
);
8438 #if defined (WINDOWSNT) || defined (CYGWIN)
8441 from_unicode (Lisp_Object str
)
8444 if (!STRING_MULTIBYTE (str
) &&
8447 str
= Fsubstring (str
, make_number (0), make_number (-1));
8450 return code_convert_string_norecord (str
, Qutf_16le
, 0);
8454 from_unicode_buffer (const wchar_t *wstr
)
8456 /* We get one of the two final null bytes for free. */
8457 ptrdiff_t len
= 1 + sizeof (wchar_t) * wcslen (wstr
);
8458 AUTO_STRING_WITH_LEN (str
, (char *) wstr
, len
);
8459 return from_unicode (str
);
8463 to_unicode (Lisp_Object str
, Lisp_Object
*buf
)
8465 *buf
= code_convert_string_norecord (str
, Qutf_16le
, 1);
8466 /* We need to make another copy (in addition to the one made by
8467 code_convert_string_norecord) to ensure that the final string is
8468 _doubly_ zero terminated --- that is, that the string is
8469 terminated by two zero bytes and one utf-16le null character.
8470 Because strings are already terminated with a single zero byte,
8471 we just add one additional zero. */
8472 str
= make_uninit_string (SBYTES (*buf
) + 1);
8473 memcpy (SDATA (str
), SDATA (*buf
), SBYTES (*buf
));
8474 SDATA (str
) [SBYTES (*buf
)] = '\0';
8476 return WCSDATA (*buf
);
8479 #endif /* WINDOWSNT || CYGWIN */
8483 /*** 8. Emacs Lisp library functions ***/
8485 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8486 doc
: /* Return t if OBJECT is nil or a coding-system.
8487 See the documentation of `define-coding-system' for information
8488 about coding-system objects. */)
8489 (Lisp_Object object
)
8492 || CODING_SYSTEM_ID (object
) >= 0)
8494 if (! SYMBOLP (object
)
8495 || NILP (Fget (object
, Qcoding_system_define_form
)))
8500 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8501 Sread_non_nil_coding_system
, 1, 1, 0,
8502 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8503 (Lisp_Object prompt
)
8508 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8509 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8511 while (SCHARS (val
) == 0);
8512 return (Fintern (val
, Qnil
));
8515 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8516 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8517 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8518 Ignores case when completing coding systems (all Emacs coding systems
8519 are lower-case). */)
8520 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8523 ptrdiff_t count
= SPECPDL_INDEX ();
8525 if (SYMBOLP (default_coding_system
))
8526 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8527 specbind (Qcompletion_ignore_case
, Qt
);
8528 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8529 Qt
, Qnil
, Qcoding_system_history
,
8530 default_coding_system
, Qnil
);
8531 unbind_to (count
, Qnil
);
8532 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8535 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8537 doc
: /* Check validity of CODING-SYSTEM.
8538 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8539 It is valid if it is nil or a symbol defined as a coding system by the
8540 function `define-coding-system'. */)
8541 (Lisp_Object coding_system
)
8543 Lisp_Object define_form
;
8545 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8546 if (! NILP (define_form
))
8548 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8549 safe_eval (define_form
);
8551 if (!NILP (Fcoding_system_p (coding_system
)))
8552 return coding_system
;
8553 xsignal1 (Qcoding_system_error
, coding_system
);
8557 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8558 HIGHEST, return the coding system of the highest
8559 priority among the detected coding systems. Otherwise return a
8560 list of detected coding systems sorted by their priorities. If
8561 MULTIBYTEP, it is assumed that the bytes are in correct
8562 multibyte form but contains only ASCII and eight-bit chars.
8563 Otherwise, the bytes are raw bytes.
8565 CODING-SYSTEM controls the detection as below:
8567 If it is nil, detect both text-format and eol-format. If the
8568 text-format part of CODING-SYSTEM is already specified
8569 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8570 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8571 detect only text-format. */
8574 detect_coding_system (const unsigned char *src
,
8575 ptrdiff_t src_chars
, ptrdiff_t src_bytes
,
8576 bool highest
, bool multibytep
,
8577 Lisp_Object coding_system
)
8579 const unsigned char *src_end
= src
+ src_bytes
;
8580 Lisp_Object attrs
, eol_type
;
8581 Lisp_Object val
= Qnil
;
8582 struct coding_system coding
;
8584 struct coding_detection_info detect_info
;
8585 enum coding_category base_category
;
8586 bool null_byte_found
= 0, eight_bit_found
= 0;
8588 if (NILP (coding_system
))
8589 coding_system
= Qundecided
;
8590 setup_coding_system (coding_system
, &coding
);
8591 attrs
= CODING_ID_ATTRS (coding
.id
);
8592 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8593 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8595 coding
.source
= src
;
8596 coding
.src_chars
= src_chars
;
8597 coding
.src_bytes
= src_bytes
;
8598 coding
.src_multibyte
= multibytep
;
8599 coding
.consumed
= 0;
8600 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8601 coding
.head_ascii
= 0;
8603 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8605 /* At first, detect text-format if necessary. */
8606 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8607 if (base_category
== coding_category_undecided
)
8609 enum coding_category category UNINIT
;
8610 struct coding_system
*this UNINIT
;
8612 bool inhibit_nbd
= inhibit_flag (coding
.spec
.undecided
.inhibit_nbd
,
8613 inhibit_null_byte_detection
);
8614 bool inhibit_ied
= inhibit_flag (coding
.spec
.undecided
.inhibit_ied
,
8615 inhibit_iso_escape_detection
);
8616 bool prefer_utf_8
= coding
.spec
.undecided
.prefer_utf_8
;
8618 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8619 for (; src
< src_end
; src
++)
8624 eight_bit_found
= 1;
8625 if (null_byte_found
)
8630 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8632 && ! detect_info
.checked
)
8634 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8636 /* We have scanned the whole data. */
8637 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8639 /* We didn't find an 8-bit code. We may
8640 have found a null-byte, but it's very
8641 rare that a binary file confirm to
8644 coding
.head_ascii
= src
- coding
.source
;
8646 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8650 else if (! c
&& !inhibit_nbd
)
8652 null_byte_found
= 1;
8653 if (eight_bit_found
)
8656 if (! eight_bit_found
)
8657 coding
.head_ascii
++;
8659 else if (! eight_bit_found
)
8660 coding
.head_ascii
++;
8663 if (null_byte_found
|| eight_bit_found
8664 || coding
.head_ascii
< coding
.src_bytes
8665 || detect_info
.found
)
8667 if (coding
.head_ascii
== coding
.src_bytes
)
8668 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8669 for (i
= 0; i
< coding_category_raw_text
; i
++)
8671 category
= coding_priorities
[i
];
8672 this = coding_categories
+ category
;
8673 if (detect_info
.found
& (1 << category
))
8678 if (null_byte_found
)
8680 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8681 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8683 else if (prefer_utf_8
8684 && detect_coding_utf_8 (&coding
, &detect_info
))
8686 detect_info
.checked
|= ~CATEGORY_MASK_UTF_8
;
8687 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_8
;
8689 for (i
= 0; i
< coding_category_raw_text
; i
++)
8691 category
= coding_priorities
[i
];
8692 this = coding_categories
+ category
;
8696 /* No coding system of this category is defined. */
8697 detect_info
.rejected
|= (1 << category
);
8699 else if (category
>= coding_category_raw_text
)
8701 else if (detect_info
.checked
& (1 << category
))
8704 && (detect_info
.found
& (1 << category
)))
8707 else if ((*(this->detector
)) (&coding
, &detect_info
)
8709 && (detect_info
.found
& (1 << category
)))
8711 if (category
== coding_category_utf_16_auto
)
8713 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8714 category
= coding_category_utf_16_le
;
8716 category
= coding_category_utf_16_be
;
8724 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8727 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8728 id
= CODING_SYSTEM_ID (Qno_conversion
);
8729 val
= list1 (make_number (id
));
8731 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8733 detect_info
.found
= CATEGORY_MASK_ANY
;
8734 id
= coding_categories
[coding_category_undecided
].id
;
8735 val
= list1 (make_number (id
));
8739 if (detect_info
.found
)
8741 detect_info
.found
= 1 << category
;
8742 val
= list1 (make_number (this->id
));
8745 for (i
= 0; i
< coding_category_raw_text
; i
++)
8746 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8748 detect_info
.found
= 1 << coding_priorities
[i
];
8749 id
= coding_categories
[coding_priorities
[i
]].id
;
8750 val
= list1 (make_number (id
));
8756 int mask
= detect_info
.rejected
| detect_info
.found
;
8759 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8761 category
= coding_priorities
[i
];
8762 if (! (mask
& (1 << category
)))
8764 found
|= 1 << category
;
8765 id
= coding_categories
[category
].id
;
8767 val
= list1 (make_number (id
));
8770 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8772 category
= coding_priorities
[i
];
8773 if (detect_info
.found
& (1 << category
))
8775 id
= coding_categories
[category
].id
;
8776 val
= Fcons (make_number (id
), val
);
8779 detect_info
.found
|= found
;
8782 else if (base_category
== coding_category_utf_8_auto
)
8784 if (detect_coding_utf_8 (&coding
, &detect_info
))
8786 struct coding_system
*this;
8788 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8789 this = coding_categories
+ coding_category_utf_8_sig
;
8791 this = coding_categories
+ coding_category_utf_8_nosig
;
8792 val
= list1 (make_number (this->id
));
8795 else if (base_category
== coding_category_utf_16_auto
)
8797 if (detect_coding_utf_16 (&coding
, &detect_info
))
8799 struct coding_system
*this;
8801 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8802 this = coding_categories
+ coding_category_utf_16_le
;
8803 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8804 this = coding_categories
+ coding_category_utf_16_be
;
8805 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8806 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8808 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8809 val
= list1 (make_number (this->id
));
8814 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8815 val
= list1 (make_number (coding
.id
));
8818 /* Then, detect eol-format if necessary. */
8820 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8823 if (VECTORP (eol_type
))
8825 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8827 if (null_byte_found
)
8828 normal_eol
= EOL_SEEN_LF
;
8830 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8831 coding_category_raw_text
);
8833 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8834 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8835 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8836 coding_category_utf_16_be
);
8837 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8838 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8839 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8840 coding_category_utf_16_le
);
8844 if (EQ (eol_type
, Qunix
))
8845 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8846 else if (EQ (eol_type
, Qdos
))
8847 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8849 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8852 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8854 enum coding_category category
;
8857 id
= XINT (XCAR (tail
));
8858 attrs
= CODING_ID_ATTRS (id
);
8859 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8860 eol_type
= CODING_ID_EOL_TYPE (id
);
8861 if (VECTORP (eol_type
))
8863 if (category
== coding_category_utf_16_be
8864 || category
== coding_category_utf_16_be_nosig
)
8865 this_eol
= utf_16_be_eol
;
8866 else if (category
== coding_category_utf_16_le
8867 || category
== coding_category_utf_16_le_nosig
)
8868 this_eol
= utf_16_le_eol
;
8870 this_eol
= normal_eol
;
8872 if (this_eol
== EOL_SEEN_LF
)
8873 XSETCAR (tail
, AREF (eol_type
, 0));
8874 else if (this_eol
== EOL_SEEN_CRLF
)
8875 XSETCAR (tail
, AREF (eol_type
, 1));
8876 else if (this_eol
== EOL_SEEN_CR
)
8877 XSETCAR (tail
, AREF (eol_type
, 2));
8879 XSETCAR (tail
, CODING_ID_NAME (id
));
8882 XSETCAR (tail
, CODING_ID_NAME (id
));
8886 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8890 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8892 doc
: /* Detect coding system of the text in the region between START and END.
8893 Return a list of possible coding systems ordered by priority.
8894 The coding systems to try and their priorities follows what
8895 the function `coding-system-priority-list' (which see) returns.
8897 If only ASCII characters are found (except for such ISO-2022 control
8898 characters as ESC), it returns a list of single element `undecided'
8899 or its subsidiary coding system according to a detected end-of-line
8902 If optional argument HIGHEST is non-nil, return the coding system of
8903 highest priority. */)
8904 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8907 ptrdiff_t from_byte
, to_byte
;
8909 validate_region (&start
, &end
);
8910 from
= XINT (start
), to
= XINT (end
);
8911 from_byte
= CHAR_TO_BYTE (from
);
8912 to_byte
= CHAR_TO_BYTE (to
);
8914 if (from
< GPT
&& to
>= GPT
)
8915 move_gap_both (to
, to_byte
);
8917 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8918 to
- from
, to_byte
- from_byte
,
8920 !NILP (BVAR (current_buffer
8921 , enable_multibyte_characters
)),
8925 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8927 doc
: /* Detect coding system of the text in STRING.
8928 Return a list of possible coding systems ordered by priority.
8929 The coding systems to try and their priorities follows what
8930 the function `coding-system-priority-list' (which see) returns.
8932 If only ASCII characters are found (except for such ISO-2022 control
8933 characters as ESC), it returns a list of single element `undecided'
8934 or its subsidiary coding system according to a detected end-of-line
8937 If optional argument HIGHEST is non-nil, return the coding system of
8938 highest priority. */)
8939 (Lisp_Object string
, Lisp_Object highest
)
8941 CHECK_STRING (string
);
8943 return detect_coding_system (SDATA (string
),
8944 SCHARS (string
), SBYTES (string
),
8945 !NILP (highest
), STRING_MULTIBYTE (string
),
8951 char_encodable_p (int c
, Lisp_Object attrs
)
8954 struct charset
*charset
;
8955 Lisp_Object translation_table
;
8957 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8958 if (! NILP (translation_table
))
8959 c
= translate_char (translation_table
, c
);
8960 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8961 CONSP (tail
); tail
= XCDR (tail
))
8963 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8964 if (CHAR_CHARSET_P (c
, charset
))
8967 return (! NILP (tail
));
8971 /* Return a list of coding systems that safely encode the text between
8972 START and END. If EXCLUDE is non-nil, it is a list of coding
8973 systems not to check. The returned list doesn't contain any such
8974 coding systems. In any case, if the text contains only ASCII or is
8975 unibyte, return t. */
8977 DEFUN ("find-coding-systems-region-internal",
8978 Ffind_coding_systems_region_internal
,
8979 Sfind_coding_systems_region_internal
, 2, 3, 0,
8980 doc
: /* Internal use only. */)
8981 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8983 Lisp_Object coding_attrs_list
, safe_codings
;
8984 ptrdiff_t start_byte
, end_byte
;
8985 const unsigned char *p
, *pbeg
, *pend
;
8987 Lisp_Object tail
, elt
, work_table
;
8989 if (STRINGP (start
))
8991 if (!STRING_MULTIBYTE (start
)
8992 || SCHARS (start
) == SBYTES (start
))
8995 end_byte
= SBYTES (start
);
8999 CHECK_NUMBER_COERCE_MARKER (start
);
9000 CHECK_NUMBER_COERCE_MARKER (end
);
9001 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
9002 args_out_of_range (start
, end
);
9003 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
9005 start_byte
= CHAR_TO_BYTE (XINT (start
));
9006 end_byte
= CHAR_TO_BYTE (XINT (end
));
9007 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
9010 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
9012 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
9013 move_gap_both (XINT (start
), start_byte
);
9015 move_gap_both (XINT (end
), end_byte
);
9019 coding_attrs_list
= Qnil
;
9020 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
9022 || NILP (Fmemq (XCAR (tail
), exclude
)))
9026 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
9027 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
)))
9029 ASET (attrs
, coding_attr_trans_tbl
,
9030 get_translation_table (attrs
, 1, NULL
));
9031 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
9035 if (STRINGP (start
))
9036 p
= pbeg
= SDATA (start
);
9038 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
9039 pend
= p
+ (end_byte
- start_byte
);
9041 while (p
< pend
&& ASCII_CHAR_P (*p
)) p
++;
9042 while (p
< pend
&& ASCII_CHAR_P (*(pend
- 1))) pend
--;
9044 work_table
= Fmake_char_table (Qnil
, Qnil
);
9047 if (ASCII_CHAR_P (*p
))
9051 c
= STRING_CHAR_ADVANCE (p
);
9052 if (!NILP (char_table_ref (work_table
, c
)))
9053 /* This character was already checked. Ignore it. */
9056 charset_map_loaded
= 0;
9057 for (tail
= coding_attrs_list
; CONSP (tail
);)
9062 else if (char_encodable_p (c
, elt
))
9064 else if (CONSP (XCDR (tail
)))
9066 XSETCAR (tail
, XCAR (XCDR (tail
)));
9067 XSETCDR (tail
, XCDR (XCDR (tail
)));
9071 XSETCAR (tail
, Qnil
);
9075 if (charset_map_loaded
)
9077 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
9079 if (STRINGP (start
))
9080 pbeg
= SDATA (start
);
9082 pbeg
= BYTE_POS_ADDR (start_byte
);
9083 p
= pbeg
+ p_offset
;
9084 pend
= pbeg
+ pend_offset
;
9086 char_table_set (work_table
, c
, Qt
);
9090 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
9091 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
9092 if (! NILP (XCAR (tail
)))
9093 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
9095 return safe_codings
;
9099 DEFUN ("unencodable-char-position", Funencodable_char_position
,
9100 Sunencodable_char_position
, 3, 5, 0,
9101 doc
: /* Return position of first un-encodable character in a region.
9102 START and END specify the region and CODING-SYSTEM specifies the
9103 encoding to check. Return nil if CODING-SYSTEM does encode the region.
9105 If optional 4th argument COUNT is non-nil, it specifies at most how
9106 many un-encodable characters to search. In this case, the value is a
9109 If optional 5th argument STRING is non-nil, it is a string to search
9110 for un-encodable characters. In that case, START and END are indexes
9111 to the string and treated as in `substring'. */)
9112 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
,
9113 Lisp_Object count
, Lisp_Object string
)
9116 struct coding_system coding
;
9117 Lisp_Object attrs
, charset_list
, translation_table
;
9118 Lisp_Object positions
;
9120 const unsigned char *p
, *stop
, *pend
;
9121 bool ascii_compatible
;
9123 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
9124 attrs
= CODING_ID_ATTRS (coding
.id
);
9125 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
9127 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
9128 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9129 translation_table
= get_translation_table (attrs
, 1, NULL
);
9133 validate_region (&start
, &end
);
9134 from
= XINT (start
);
9136 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
9137 || (ascii_compatible
9138 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
9140 p
= CHAR_POS_ADDR (from
);
9141 pend
= CHAR_POS_ADDR (to
);
9142 if (from
< GPT
&& to
>= GPT
)
9149 CHECK_STRING (string
);
9150 validate_subarray (string
, start
, end
, SCHARS (string
), &from
, &to
);
9151 if (! STRING_MULTIBYTE (string
))
9153 p
= SDATA (string
) + string_char_to_byte (string
, from
);
9154 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
9155 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
9163 CHECK_NATNUM (count
);
9168 charset_map_loaded
= 0;
9173 if (ascii_compatible
)
9174 while (p
< stop
&& ASCII_CHAR_P (*p
))
9184 c
= STRING_CHAR_ADVANCE (p
);
9185 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
9186 && ! char_charset (translate_char (translation_table
, c
),
9187 charset_list
, NULL
))
9189 positions
= Fcons (make_number (from
), positions
);
9196 if (charset_map_loaded
&& NILP (string
))
9198 p
= CHAR_POS_ADDR (from
);
9199 pend
= CHAR_POS_ADDR (to
);
9200 if (from
< GPT
&& to
>= GPT
)
9204 charset_map_loaded
= 0;
9208 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
9212 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
9213 Scheck_coding_systems_region
, 3, 3, 0,
9214 doc
: /* Check if the region is encodable by coding systems.
9216 START and END are buffer positions specifying the region.
9217 CODING-SYSTEM-LIST is a list of coding systems to check.
9219 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
9220 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
9221 whole region, POS0, POS1, ... are buffer positions where non-encodable
9222 characters are found.
9224 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
9227 START may be a string. In that case, check if the string is
9228 encodable, and the value contains indices to the string instead of
9229 buffer positions. END is ignored.
9231 If the current buffer (or START if it is a string) is unibyte, the value
9233 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
9236 ptrdiff_t start_byte
, end_byte
;
9238 const unsigned char *p
, *pbeg
, *pend
;
9240 Lisp_Object tail
, elt
, attrs
;
9242 if (STRINGP (start
))
9244 if (!STRING_MULTIBYTE (start
)
9245 || SCHARS (start
) == SBYTES (start
))
9248 end_byte
= SBYTES (start
);
9253 CHECK_NUMBER_COERCE_MARKER (start
);
9254 CHECK_NUMBER_COERCE_MARKER (end
);
9255 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
9256 args_out_of_range (start
, end
);
9257 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
9259 start_byte
= CHAR_TO_BYTE (XINT (start
));
9260 end_byte
= CHAR_TO_BYTE (XINT (end
));
9261 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
9264 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
9266 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
9267 move_gap_both (XINT (start
), start_byte
);
9269 move_gap_both (XINT (end
), end_byte
);
9275 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
9278 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
9279 ASET (attrs
, coding_attr_trans_tbl
,
9280 get_translation_table (attrs
, 1, NULL
));
9281 list
= Fcons (list2 (elt
, attrs
), list
);
9284 if (STRINGP (start
))
9285 p
= pbeg
= SDATA (start
);
9287 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
9288 pend
= p
+ (end_byte
- start_byte
);
9290 while (p
< pend
&& ASCII_CHAR_P (*p
)) p
++, pos
++;
9291 while (p
< pend
&& ASCII_CHAR_P (*(pend
- 1))) pend
--;
9295 if (ASCII_CHAR_P (*p
))
9299 c
= STRING_CHAR_ADVANCE (p
);
9301 charset_map_loaded
= 0;
9302 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
9304 elt
= XCDR (XCAR (tail
));
9305 if (! char_encodable_p (c
, XCAR (elt
)))
9306 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
9308 if (charset_map_loaded
)
9310 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
9312 if (STRINGP (start
))
9313 pbeg
= SDATA (start
);
9315 pbeg
= BYTE_POS_ADDR (start_byte
);
9316 p
= pbeg
+ p_offset
;
9317 pend
= pbeg
+ pend_offset
;
9325 for (; CONSP (tail
); tail
= XCDR (tail
))
9328 if (CONSP (XCDR (XCDR (elt
))))
9329 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
9338 code_convert_region (Lisp_Object start
, Lisp_Object end
,
9339 Lisp_Object coding_system
, Lisp_Object dst_object
,
9340 bool encodep
, bool norecord
)
9342 struct coding_system coding
;
9343 ptrdiff_t from
, from_byte
, to
, to_byte
;
9344 Lisp_Object src_object
;
9346 if (NILP (coding_system
))
9347 coding_system
= Qno_conversion
;
9349 CHECK_CODING_SYSTEM (coding_system
);
9350 src_object
= Fcurrent_buffer ();
9351 if (NILP (dst_object
))
9352 dst_object
= src_object
;
9353 else if (! EQ (dst_object
, Qt
))
9354 CHECK_BUFFER (dst_object
);
9356 validate_region (&start
, &end
);
9357 from
= XFASTINT (start
);
9358 from_byte
= CHAR_TO_BYTE (from
);
9359 to
= XFASTINT (end
);
9360 to_byte
= CHAR_TO_BYTE (to
);
9362 setup_coding_system (coding_system
, &coding
);
9363 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9365 if (BUFFERP (dst_object
) && !EQ (dst_object
, src_object
))
9367 struct buffer
*buf
= XBUFFER (dst_object
);
9368 ptrdiff_t buf_pt
= BUF_PT (buf
);
9370 invalidate_buffer_caches (buf
, buf_pt
, buf_pt
);
9374 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
9377 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
9380 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9382 return (BUFFERP (dst_object
)
9383 ? make_number (coding
.produced_char
)
9384 : coding
.dst_object
);
9388 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
9389 3, 4, "r\nzCoding system: ",
9390 doc
: /* Decode the current region from the specified coding system.
9391 When called from a program, takes four arguments:
9392 START, END, CODING-SYSTEM, and DESTINATION.
9393 START and END are buffer positions.
9395 Optional 4th arguments DESTINATION specifies where the decoded text goes.
9396 If nil, the region between START and END is replaced by the decoded text.
9397 If buffer, the decoded text is inserted in that buffer after point (point
9399 In those cases, the length of the decoded text is returned.
9400 If DESTINATION is t, the decoded text is returned.
9402 This function sets `last-coding-system-used' to the precise coding system
9403 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9404 not fully specified.) */)
9405 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
9407 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
9410 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
9411 3, 4, "r\nzCoding system: ",
9412 doc
: /* Encode the current region by specified coding system.
9413 When called from a program, takes four arguments:
9414 START, END, CODING-SYSTEM and DESTINATION.
9415 START and END are buffer positions.
9417 Optional 4th argument DESTINATION specifies where the encoded text goes.
9418 If nil, the region between START and END is replaced by the encoded text.
9419 If buffer, the encoded text is inserted in that buffer after point (point
9421 In those cases, the length of the encoded text is returned.
9422 If DESTINATION is t, the encoded text is returned.
9424 This function sets `last-coding-system-used' to the precise coding system
9425 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9426 not fully specified.) */)
9427 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
9429 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
9433 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
9434 Lisp_Object dst_object
, bool encodep
, bool nocopy
,
9437 struct coding_system coding
;
9438 ptrdiff_t chars
, bytes
;
9440 CHECK_STRING (string
);
9441 if (NILP (coding_system
))
9444 Vlast_coding_system_used
= Qno_conversion
;
9445 if (NILP (dst_object
))
9446 return (nocopy
? Fcopy_sequence (string
) : string
);
9449 if (NILP (coding_system
))
9450 coding_system
= Qno_conversion
;
9452 CHECK_CODING_SYSTEM (coding_system
);
9453 if (NILP (dst_object
))
9455 else if (! EQ (dst_object
, Qt
))
9456 CHECK_BUFFER (dst_object
);
9458 setup_coding_system (coding_system
, &coding
);
9459 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9460 chars
= SCHARS (string
);
9461 bytes
= SBYTES (string
);
9463 if (BUFFERP (dst_object
))
9465 struct buffer
*buf
= XBUFFER (dst_object
);
9466 ptrdiff_t buf_pt
= BUF_PT (buf
);
9468 invalidate_buffer_caches (buf
, buf_pt
, buf_pt
);
9472 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9474 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9476 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9478 return (BUFFERP (dst_object
)
9479 ? make_number (coding
.produced_char
)
9480 : coding
.dst_object
);
9484 /* Encode or decode STRING according to CODING_SYSTEM.
9485 Do not set Vlast_coding_system_used.
9487 This function is called only from macros DECODE_FILE and
9488 ENCODE_FILE, thus we ignore character composition. */
9491 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9494 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9497 /* Encode or decode a file name, to or from a unibyte string suitable
9498 for passing to C library functions. */
9500 decode_file_name (Lisp_Object fname
)
9503 /* The w32 build pretends to use UTF-8 for file-name encoding, and
9504 converts the file names either to UTF-16LE or to the system ANSI
9505 codepage internally, depending on the underlying OS; see w32.c. */
9506 if (! NILP (Fcoding_system_p (Qutf_8
)))
9507 return code_convert_string_norecord (fname
, Qutf_8
, 0);
9509 #else /* !WINDOWSNT */
9510 if (! NILP (Vfile_name_coding_system
))
9511 return code_convert_string_norecord (fname
, Vfile_name_coding_system
, 0);
9512 else if (! NILP (Vdefault_file_name_coding_system
))
9513 return code_convert_string_norecord (fname
,
9514 Vdefault_file_name_coding_system
, 0);
9521 encode_file_name (Lisp_Object fname
)
9523 /* This is especially important during bootstrap and dumping, when
9524 file-name encoding is not yet known, and therefore any non-ASCII
9525 file names are unibyte strings, and could only be thrashed if we
9526 try to encode them. */
9527 if (!STRING_MULTIBYTE (fname
))
9530 /* The w32 build pretends to use UTF-8 for file-name encoding, and
9531 converts the file names either to UTF-16LE or to the system ANSI
9532 codepage internally, depending on the underlying OS; see w32.c. */
9533 if (! NILP (Fcoding_system_p (Qutf_8
)))
9534 return code_convert_string_norecord (fname
, Qutf_8
, 1);
9536 #else /* !WINDOWSNT */
9537 if (! NILP (Vfile_name_coding_system
))
9538 return code_convert_string_norecord (fname
, Vfile_name_coding_system
, 1);
9539 else if (! NILP (Vdefault_file_name_coding_system
))
9540 return code_convert_string_norecord (fname
,
9541 Vdefault_file_name_coding_system
, 1);
9547 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9549 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9551 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9552 if the decoding operation is trivial.
9554 Optional fourth arg BUFFER non-nil means that the decoded text is
9555 inserted in that buffer after point (point does not move). In this
9556 case, the return value is the length of the decoded text.
9558 This function sets `last-coding-system-used' to the precise coding system
9559 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9560 not fully specified.) */)
9561 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9563 return code_convert_string (string
, coding_system
, buffer
,
9564 0, ! NILP (nocopy
), 0);
9567 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9569 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9571 Optional third arg NOCOPY non-nil means it is OK to return STRING
9572 itself if the encoding operation is trivial.
9574 Optional fourth arg BUFFER non-nil means that the encoded text is
9575 inserted in that buffer after point (point does not move). In this
9576 case, the return value is the length of the encoded text.
9578 This function sets `last-coding-system-used' to the precise coding system
9579 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9580 not fully specified.) */)
9581 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9583 return code_convert_string (string
, coding_system
, buffer
,
9584 1, ! NILP (nocopy
), 0);
9588 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9589 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9590 Return the corresponding character. */)
9593 Lisp_Object spec
, attrs
, val
;
9594 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9598 CHECK_NATNUM (code
);
9599 ch
= XFASTINT (code
);
9600 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9601 attrs
= AREF (spec
, 0);
9603 if (ASCII_CHAR_P (ch
)
9604 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9607 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9608 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9609 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9610 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9615 charset
= charset_roman
;
9617 else if (ch
>= 0xA0 && ch
< 0xDF)
9620 charset
= charset_kana
;
9624 EMACS_INT c1
= ch
>> 8;
9627 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9628 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9629 error ("Invalid code: %"pI
"d", ch
);
9632 charset
= charset_kanji
;
9634 c
= DECODE_CHAR (charset
, c
);
9636 error ("Invalid code: %"pI
"d", ch
);
9637 return make_number (c
);
9641 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9642 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9643 Return the corresponding code in SJIS. */)
9646 Lisp_Object spec
, attrs
, charset_list
;
9648 struct charset
*charset
;
9651 CHECK_CHARACTER (ch
);
9653 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9654 attrs
= AREF (spec
, 0);
9656 if (ASCII_CHAR_P (c
)
9657 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9660 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9661 charset
= char_charset (c
, charset_list
, &code
);
9662 if (code
== CHARSET_INVALID_CODE (charset
))
9663 error ("Can't encode by shift_jis encoding: %c", c
);
9666 return make_number (code
);
9669 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9670 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9671 Return the corresponding character. */)
9674 Lisp_Object spec
, attrs
, val
;
9675 struct charset
*charset_roman
, *charset_big5
, *charset
;
9679 CHECK_NATNUM (code
);
9680 ch
= XFASTINT (code
);
9681 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9682 attrs
= AREF (spec
, 0);
9684 if (ASCII_CHAR_P (ch
)
9685 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9688 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9689 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9690 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9695 charset
= charset_roman
;
9699 EMACS_INT b1
= ch
>> 8;
9701 if (b1
< 0xA1 || b1
> 0xFE
9702 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9703 error ("Invalid code: %"pI
"d", ch
);
9705 charset
= charset_big5
;
9707 c
= DECODE_CHAR (charset
, c
);
9709 error ("Invalid code: %"pI
"d", ch
);
9710 return make_number (c
);
9713 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9714 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9715 Return the corresponding character code in Big5. */)
9718 Lisp_Object spec
, attrs
, charset_list
;
9719 struct charset
*charset
;
9723 CHECK_CHARACTER (ch
);
9725 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9726 attrs
= AREF (spec
, 0);
9727 if (ASCII_CHAR_P (c
)
9728 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9731 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9732 charset
= char_charset (c
, charset_list
, &code
);
9733 if (code
== CHARSET_INVALID_CODE (charset
))
9734 error ("Can't encode by Big5 encoding: %c", c
);
9736 return make_number (code
);
9740 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9741 Sset_terminal_coding_system_internal
, 1, 2, 0,
9742 doc
: /* Internal use only. */)
9743 (Lisp_Object coding_system
, Lisp_Object terminal
)
9745 struct terminal
*term
= decode_live_terminal (terminal
);
9746 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9747 CHECK_SYMBOL (coding_system
);
9748 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9749 /* We had better not send unsafe characters to terminal. */
9750 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9751 /* Character composition should be disabled. */
9752 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9753 terminal_coding
->src_multibyte
= 1;
9754 terminal_coding
->dst_multibyte
= 0;
9756 (term
, (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
9757 ? coding_charset_list (terminal_coding
)
9758 : list1 (make_number (charset_ascii
))));
9762 DEFUN ("set-safe-terminal-coding-system-internal",
9763 Fset_safe_terminal_coding_system_internal
,
9764 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9765 doc
: /* Internal use only. */)
9766 (Lisp_Object coding_system
)
9768 CHECK_SYMBOL (coding_system
);
9769 setup_coding_system (Fcheck_coding_system (coding_system
),
9770 &safe_terminal_coding
);
9771 /* Character composition should be disabled. */
9772 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9773 safe_terminal_coding
.src_multibyte
= 1;
9774 safe_terminal_coding
.dst_multibyte
= 0;
9778 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9779 Sterminal_coding_system
, 0, 1, 0,
9780 doc
: /* Return coding system specified for terminal output on the given terminal.
9781 TERMINAL may be a terminal object, a frame, or nil for the selected
9782 frame's terminal device. */)
9783 (Lisp_Object terminal
)
9785 struct coding_system
*terminal_coding
9786 = TERMINAL_TERMINAL_CODING (decode_live_terminal (terminal
));
9787 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9789 /* For backward compatibility, return nil if it is `undecided'. */
9790 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9793 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9794 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9795 doc
: /* Internal use only. */)
9796 (Lisp_Object coding_system
, Lisp_Object terminal
)
9798 struct terminal
*t
= decode_live_terminal (terminal
);
9799 CHECK_SYMBOL (coding_system
);
9800 if (NILP (coding_system
))
9801 coding_system
= Qno_conversion
;
9803 Fcheck_coding_system (coding_system
);
9804 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9805 /* Character composition should be disabled. */
9806 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9807 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9811 DEFUN ("keyboard-coding-system",
9812 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9813 doc
: /* Return coding system specified for decoding keyboard input. */)
9814 (Lisp_Object terminal
)
9816 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9817 (decode_live_terminal (terminal
))->id
);
9821 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9822 Sfind_operation_coding_system
, 1, MANY
, 0,
9823 doc
: /* Choose a coding system for an operation based on the target name.
9824 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9825 DECODING-SYSTEM is the coding system to use for decoding
9826 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9827 for encoding (in case OPERATION does encoding).
9829 The first argument OPERATION specifies an I/O primitive:
9830 For file I/O, `insert-file-contents' or `write-region'.
9831 For process I/O, `call-process', `call-process-region', or `start-process'.
9832 For network I/O, `open-network-stream'.
9834 The remaining arguments should be the same arguments that were passed
9835 to the primitive. Depending on which primitive, one of those arguments
9836 is selected as the TARGET. For example, if OPERATION does file I/O,
9837 whichever argument specifies the file name is TARGET.
9839 TARGET has a meaning which depends on OPERATION:
9840 For file I/O, TARGET is a file name (except for the special case below).
9841 For process I/O, TARGET is a process name.
9842 For network I/O, TARGET is a service name or a port number.
9844 This function looks up what is specified for TARGET in
9845 `file-coding-system-alist', `process-coding-system-alist',
9846 or `network-coding-system-alist' depending on OPERATION.
9847 They may specify a coding system, a cons of coding systems,
9848 or a function symbol to call.
9849 In the last case, we call the function with one argument,
9850 which is a list of all the arguments given to this function.
9851 If the function can't decide a coding system, it can return
9852 `undecided' so that the normal code-detection is performed.
9854 If OPERATION is `insert-file-contents', the argument corresponding to
9855 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9856 file name to look up, and BUFFER is a buffer that contains the file's
9857 contents (not yet decoded). If `file-coding-system-alist' specifies a
9858 function to call for FILENAME, that function should examine the
9859 contents of BUFFER instead of reading the file.
9861 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9862 (ptrdiff_t nargs
, Lisp_Object
*args
)
9864 Lisp_Object operation
, target_idx
, target
, val
;
9865 register Lisp_Object chain
;
9868 error ("Too few arguments");
9869 operation
= args
[0];
9870 if (!SYMBOLP (operation
)
9871 || (target_idx
= Fget (operation
, Qtarget_idx
), !NATNUMP (target_idx
)))
9872 error ("Invalid first argument");
9873 if (nargs
<= 1 + XFASTINT (target_idx
))
9874 error ("Too few arguments for operation `%s'",
9875 SDATA (SYMBOL_NAME (operation
)));
9876 target
= args
[XFASTINT (target_idx
) + 1];
9877 if (!(STRINGP (target
)
9878 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9879 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9880 || (EQ (operation
, Qopen_network_stream
)
9881 && (INTEGERP (target
) || EQ (target
, Qt
)))))
9882 error ("Invalid argument %"pI
"d of operation `%s'",
9883 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9885 target
= XCAR (target
);
9887 chain
= ((EQ (operation
, Qinsert_file_contents
)
9888 || EQ (operation
, Qwrite_region
))
9889 ? Vfile_coding_system_alist
9890 : (EQ (operation
, Qopen_network_stream
)
9891 ? Vnetwork_coding_system_alist
9892 : Vprocess_coding_system_alist
));
9896 for (; CONSP (chain
); chain
= XCDR (chain
))
9902 && ((STRINGP (target
)
9903 && STRINGP (XCAR (elt
))
9904 && fast_string_match (XCAR (elt
), target
) >= 0)
9905 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9908 /* Here, if VAL is both a valid coding system and a valid
9909 function symbol, we return VAL as a coding system. */
9912 if (! SYMBOLP (val
))
9914 if (! NILP (Fcoding_system_p (val
)))
9915 return Fcons (val
, val
);
9916 if (! NILP (Ffboundp (val
)))
9918 /* We use call1 rather than safe_call1
9919 so as to get bug reports about functions called here
9920 which don't handle the current interface. */
9921 val
= call1 (val
, Flist (nargs
, args
));
9924 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9925 return Fcons (val
, val
);
9933 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9934 Sset_coding_system_priority
, 0, MANY
, 0,
9935 doc
: /* Assign higher priority to the coding systems given as arguments.
9936 If multiple coding systems belong to the same category,
9937 all but the first one are ignored.
9939 usage: (set-coding-system-priority &rest coding-systems) */)
9940 (ptrdiff_t nargs
, Lisp_Object
*args
)
9943 bool changed
[coding_category_max
];
9944 enum coding_category priorities
[coding_category_max
];
9946 memset (changed
, 0, sizeof changed
);
9948 for (i
= j
= 0; i
< nargs
; i
++)
9950 enum coding_category category
;
9951 Lisp_Object spec
, attrs
;
9953 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9954 attrs
= AREF (spec
, 0);
9955 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9956 if (changed
[category
])
9957 /* Ignore this coding system because a coding system of the
9958 same category already had a higher priority. */
9960 changed
[category
] = 1;
9961 priorities
[j
++] = category
;
9962 if (coding_categories
[category
].id
>= 0
9963 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9964 setup_coding_system (args
[i
], &coding_categories
[category
]);
9965 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9968 /* Now we have decided top J priorities. Reflect the order of the
9969 original priorities to the remaining priorities. */
9971 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9973 while (j
< coding_category_max
9974 && changed
[coding_priorities
[j
]])
9976 if (j
== coding_category_max
)
9978 priorities
[i
] = coding_priorities
[j
];
9981 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9983 /* Update `coding-category-list'. */
9984 Vcoding_category_list
= Qnil
;
9985 for (i
= coding_category_max
; i
-- > 0; )
9986 Vcoding_category_list
9987 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9988 Vcoding_category_list
);
9993 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9994 Scoding_system_priority_list
, 0, 1, 0,
9995 doc
: /* Return a list of coding systems ordered by their priorities.
9996 The list contains a subset of coding systems; i.e. coding systems
9997 assigned to each coding category (see `coding-category-list').
9999 HIGHESTP non-nil means just return the highest priority one. */)
10000 (Lisp_Object highestp
)
10005 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
10007 enum coding_category category
= coding_priorities
[i
];
10008 int id
= coding_categories
[category
].id
;
10013 attrs
= CODING_ID_ATTRS (id
);
10014 if (! NILP (highestp
))
10015 return CODING_ATTR_BASE_NAME (attrs
);
10016 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
10018 return Fnreverse (val
);
10021 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
10024 make_subsidiaries (Lisp_Object base
)
10026 Lisp_Object subsidiaries
;
10027 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
10029 char *buf
= SAFE_ALLOCA (base_name_len
+ 6);
10032 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
10033 subsidiaries
= make_uninit_vector (3);
10034 for (i
= 0; i
< 3; i
++)
10036 strcpy (buf
+ base_name_len
, suffixes
[i
]);
10037 ASET (subsidiaries
, i
, intern (buf
));
10040 return subsidiaries
;
10044 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
10045 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
10046 doc
: /* For internal use only.
10047 usage: (define-coding-system-internal ...) */)
10048 (ptrdiff_t nargs
, Lisp_Object
*args
)
10051 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
10052 Lisp_Object attrs
; /* Vector of attributes. */
10053 Lisp_Object eol_type
;
10054 Lisp_Object aliases
;
10055 Lisp_Object coding_type
, charset_list
, safe_charsets
;
10056 enum coding_category category
;
10057 Lisp_Object tail
, val
;
10058 int max_charset_id
= 0;
10061 if (nargs
< coding_arg_max
)
10064 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
10066 name
= args
[coding_arg_name
];
10067 CHECK_SYMBOL (name
);
10068 ASET (attrs
, coding_attr_base_name
, name
);
10070 val
= args
[coding_arg_mnemonic
];
10071 if (! STRINGP (val
))
10072 CHECK_CHARACTER (val
);
10073 ASET (attrs
, coding_attr_mnemonic
, val
);
10075 coding_type
= args
[coding_arg_coding_type
];
10076 CHECK_SYMBOL (coding_type
);
10077 ASET (attrs
, coding_attr_type
, coding_type
);
10079 charset_list
= args
[coding_arg_charset_list
];
10080 if (SYMBOLP (charset_list
))
10082 if (EQ (charset_list
, Qiso_2022
))
10084 if (! EQ (coding_type
, Qiso_2022
))
10085 error ("Invalid charset-list");
10086 charset_list
= Viso_2022_charset_list
;
10088 else if (EQ (charset_list
, Qemacs_mule
))
10090 if (! EQ (coding_type
, Qemacs_mule
))
10091 error ("Invalid charset-list");
10092 charset_list
= Vemacs_mule_charset_list
;
10094 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10096 if (! RANGED_INTEGERP (0, XCAR (tail
), INT_MAX
- 1))
10097 error ("Invalid charset-list");
10098 if (max_charset_id
< XFASTINT (XCAR (tail
)))
10099 max_charset_id
= XFASTINT (XCAR (tail
));
10104 charset_list
= Fcopy_sequence (charset_list
);
10105 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10107 struct charset
*charset
;
10110 CHECK_CHARSET_GET_CHARSET (val
, charset
);
10111 if (EQ (coding_type
, Qiso_2022
)
10112 ? CHARSET_ISO_FINAL (charset
) < 0
10113 : EQ (coding_type
, Qemacs_mule
)
10114 ? CHARSET_EMACS_MULE_ID (charset
) < 0
10116 error ("Can't handle charset `%s'",
10117 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10119 XSETCAR (tail
, make_number (charset
->id
));
10120 if (max_charset_id
< charset
->id
)
10121 max_charset_id
= charset
->id
;
10124 ASET (attrs
, coding_attr_charset_list
, charset_list
);
10126 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
10127 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
10128 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10129 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
10130 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
10132 ASET (attrs
, coding_attr_ascii_compat
, args
[coding_arg_ascii_compatible_p
]);
10134 val
= args
[coding_arg_decode_translation_table
];
10135 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10136 CHECK_SYMBOL (val
);
10137 ASET (attrs
, coding_attr_decode_tbl
, val
);
10139 val
= args
[coding_arg_encode_translation_table
];
10140 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10141 CHECK_SYMBOL (val
);
10142 ASET (attrs
, coding_attr_encode_tbl
, val
);
10144 val
= args
[coding_arg_post_read_conversion
];
10145 CHECK_SYMBOL (val
);
10146 ASET (attrs
, coding_attr_post_read
, val
);
10148 val
= args
[coding_arg_pre_write_conversion
];
10149 CHECK_SYMBOL (val
);
10150 ASET (attrs
, coding_attr_pre_write
, val
);
10152 val
= args
[coding_arg_default_char
];
10154 ASET (attrs
, coding_attr_default_char
, make_number (' '));
10157 CHECK_CHARACTER (val
);
10158 ASET (attrs
, coding_attr_default_char
, val
);
10161 val
= args
[coding_arg_for_unibyte
];
10162 ASET (attrs
, coding_attr_for_unibyte
, NILP (val
) ? Qnil
: Qt
);
10164 val
= args
[coding_arg_plist
];
10166 ASET (attrs
, coding_attr_plist
, val
);
10168 if (EQ (coding_type
, Qcharset
))
10170 /* Generate a lisp vector of 256 elements. Each element is nil,
10171 integer, or a list of charset IDs.
10173 If Nth element is nil, the byte code N is invalid in this
10176 If Nth element is a number NUM, N is the first byte of a
10177 charset whose ID is NUM.
10179 If Nth element is a list of charset IDs, N is the first byte
10180 of one of them. The list is sorted by dimensions of the
10181 charsets. A charset of smaller dimension comes first. */
10182 val
= Fmake_vector (make_number (256), Qnil
);
10184 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10186 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
10187 int dim
= CHARSET_DIMENSION (charset
);
10188 int idx
= (dim
- 1) * 4;
10190 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10191 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10193 for (i
= charset
->code_space
[idx
];
10194 i
<= charset
->code_space
[idx
+ 1]; i
++)
10196 Lisp_Object tmp
, tmp2
;
10199 tmp
= AREF (val
, i
);
10202 else if (NUMBERP (tmp
))
10204 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
10206 tmp
= list2 (XCAR (tail
), tmp
);
10208 tmp
= list2 (tmp
, XCAR (tail
));
10212 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
10214 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
10219 tmp
= nconc2 (tmp
, list1 (XCAR (tail
)));
10222 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
10223 XSETCAR (tmp2
, XCAR (tail
));
10226 ASET (val
, i
, tmp
);
10229 ASET (attrs
, coding_attr_charset_valids
, val
);
10230 category
= coding_category_charset
;
10232 else if (EQ (coding_type
, Qccl
))
10234 Lisp_Object valids
;
10236 if (nargs
< coding_arg_ccl_max
)
10239 val
= args
[coding_arg_ccl_decoder
];
10240 CHECK_CCL_PROGRAM (val
);
10242 val
= Fcopy_sequence (val
);
10243 ASET (attrs
, coding_attr_ccl_decoder
, val
);
10245 val
= args
[coding_arg_ccl_encoder
];
10246 CHECK_CCL_PROGRAM (val
);
10248 val
= Fcopy_sequence (val
);
10249 ASET (attrs
, coding_attr_ccl_encoder
, val
);
10251 val
= args
[coding_arg_ccl_valids
];
10252 valids
= Fmake_string (make_number (256), make_number (0));
10253 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
10258 if (INTEGERP (val
))
10260 if (! (0 <= XINT (val
) && XINT (val
) <= 255))
10261 args_out_of_range_3 (val
, make_number (0), make_number (255));
10262 from
= to
= XINT (val
);
10267 CHECK_NATNUM_CAR (val
);
10268 CHECK_NUMBER_CDR (val
);
10269 if (XINT (XCAR (val
)) > 255)
10270 args_out_of_range_3 (XCAR (val
),
10271 make_number (0), make_number (255));
10272 from
= XINT (XCAR (val
));
10273 if (! (from
<= XINT (XCDR (val
)) && XINT (XCDR (val
)) <= 255))
10274 args_out_of_range_3 (XCDR (val
),
10275 XCAR (val
), make_number (255));
10276 to
= XINT (XCDR (val
));
10278 for (i
= from
; i
<= to
; i
++)
10279 SSET (valids
, i
, 1);
10281 ASET (attrs
, coding_attr_ccl_valids
, valids
);
10283 category
= coding_category_ccl
;
10285 else if (EQ (coding_type
, Qutf_16
))
10287 Lisp_Object bom
, endian
;
10289 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
10291 if (nargs
< coding_arg_utf16_max
)
10294 bom
= args
[coding_arg_utf16_bom
];
10295 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10299 CHECK_CODING_SYSTEM (val
);
10301 CHECK_CODING_SYSTEM (val
);
10303 ASET (attrs
, coding_attr_utf_bom
, bom
);
10305 endian
= args
[coding_arg_utf16_endian
];
10306 CHECK_SYMBOL (endian
);
10309 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
10310 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
10311 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
10313 category
= (CONSP (bom
)
10314 ? coding_category_utf_16_auto
10316 ? (EQ (endian
, Qbig
)
10317 ? coding_category_utf_16_be_nosig
10318 : coding_category_utf_16_le_nosig
)
10319 : (EQ (endian
, Qbig
)
10320 ? coding_category_utf_16_be
10321 : coding_category_utf_16_le
));
10323 else if (EQ (coding_type
, Qiso_2022
))
10325 Lisp_Object initial
, reg_usage
, request
, flags
;
10327 if (nargs
< coding_arg_iso2022_max
)
10330 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
10331 CHECK_VECTOR (initial
);
10332 for (i
= 0; i
< 4; i
++)
10334 val
= AREF (initial
, i
);
10337 struct charset
*charset
;
10339 CHECK_CHARSET_GET_CHARSET (val
, charset
);
10340 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
10341 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
10342 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10345 ASET (initial
, i
, make_number (-1));
10348 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
10349 CHECK_CONS (reg_usage
);
10350 CHECK_NUMBER_CAR (reg_usage
);
10351 CHECK_NUMBER_CDR (reg_usage
);
10353 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
10354 for (tail
= request
; CONSP (tail
); tail
= XCDR (tail
))
10362 CHECK_CHARSET_GET_ID (tmp1
, id
);
10363 CHECK_NATNUM_CDR (val
);
10364 if (XINT (XCDR (val
)) >= 4)
10365 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
10366 XSETCAR (val
, make_number (id
));
10369 flags
= args
[coding_arg_iso2022_flags
];
10370 CHECK_NATNUM (flags
);
10371 i
= XINT (flags
) & INT_MAX
;
10372 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
10373 i
|= CODING_ISO_FLAG_FULL_SUPPORT
;
10374 flags
= make_number (i
);
10376 ASET (attrs
, coding_attr_iso_initial
, initial
);
10377 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
10378 ASET (attrs
, coding_attr_iso_request
, request
);
10379 ASET (attrs
, coding_attr_iso_flags
, flags
);
10380 setup_iso_safe_charsets (attrs
);
10382 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
10383 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
10384 | CODING_ISO_FLAG_SINGLE_SHIFT
))
10385 ? coding_category_iso_7_else
10386 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
10387 ? coding_category_iso_7
10388 : coding_category_iso_7_tight
);
10391 int id
= XINT (AREF (initial
, 1));
10393 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
10394 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
10396 ? coding_category_iso_8_else
10397 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
10398 ? coding_category_iso_8_1
10399 : coding_category_iso_8_2
);
10401 if (category
!= coding_category_iso_8_1
10402 && category
!= coding_category_iso_8_2
)
10403 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
10405 else if (EQ (coding_type
, Qemacs_mule
))
10407 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
10408 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
10409 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10410 category
= coding_category_emacs_mule
;
10412 else if (EQ (coding_type
, Qshift_jis
))
10415 struct charset
*charset
;
10417 if (XINT (Flength (charset_list
)) != 3
10418 && XINT (Flength (charset_list
)) != 4)
10419 error ("There should be three or four charsets");
10421 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10422 if (CHARSET_DIMENSION (charset
) != 1)
10423 error ("Dimension of charset %s is not one",
10424 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10425 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10426 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10428 charset_list
= XCDR (charset_list
);
10429 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10430 if (CHARSET_DIMENSION (charset
) != 1)
10431 error ("Dimension of charset %s is not one",
10432 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10434 charset_list
= XCDR (charset_list
);
10435 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10436 if (CHARSET_DIMENSION (charset
) != 2)
10437 error ("Dimension of charset %s is not two",
10438 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10440 charset_list
= XCDR (charset_list
);
10441 if (! NILP (charset_list
))
10443 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10444 if (CHARSET_DIMENSION (charset
) != 2)
10445 error ("Dimension of charset %s is not two",
10446 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10449 category
= coding_category_sjis
;
10450 Vsjis_coding_system
= name
;
10452 else if (EQ (coding_type
, Qbig5
))
10454 struct charset
*charset
;
10456 if (XINT (Flength (charset_list
)) != 2)
10457 error ("There should be just two charsets");
10459 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10460 if (CHARSET_DIMENSION (charset
) != 1)
10461 error ("Dimension of charset %s is not one",
10462 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10463 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10464 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10466 charset_list
= XCDR (charset_list
);
10467 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10468 if (CHARSET_DIMENSION (charset
) != 2)
10469 error ("Dimension of charset %s is not two",
10470 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10472 category
= coding_category_big5
;
10473 Vbig5_coding_system
= name
;
10475 else if (EQ (coding_type
, Qraw_text
))
10477 category
= coding_category_raw_text
;
10478 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10480 else if (EQ (coding_type
, Qutf_8
))
10484 if (nargs
< coding_arg_utf8_max
)
10487 bom
= args
[coding_arg_utf8_bom
];
10488 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10492 CHECK_CODING_SYSTEM (val
);
10494 CHECK_CODING_SYSTEM (val
);
10496 ASET (attrs
, coding_attr_utf_bom
, bom
);
10498 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10500 category
= (CONSP (bom
) ? coding_category_utf_8_auto
10501 : NILP (bom
) ? coding_category_utf_8_nosig
10502 : coding_category_utf_8_sig
);
10504 else if (EQ (coding_type
, Qundecided
))
10506 if (nargs
< coding_arg_undecided_max
)
10508 ASET (attrs
, coding_attr_undecided_inhibit_null_byte_detection
,
10509 args
[coding_arg_undecided_inhibit_null_byte_detection
]);
10510 ASET (attrs
, coding_attr_undecided_inhibit_iso_escape_detection
,
10511 args
[coding_arg_undecided_inhibit_iso_escape_detection
]);
10512 ASET (attrs
, coding_attr_undecided_prefer_utf_8
,
10513 args
[coding_arg_undecided_prefer_utf_8
]);
10514 category
= coding_category_undecided
;
10517 error ("Invalid coding system type: %s",
10518 SDATA (SYMBOL_NAME (coding_type
)));
10520 ASET (attrs
, coding_attr_category
, make_number (category
));
10521 ASET (attrs
, coding_attr_plist
,
10523 Fcons (AREF (Vcoding_category_table
, category
),
10524 CODING_ATTR_PLIST (attrs
))));
10525 ASET (attrs
, coding_attr_plist
,
10526 Fcons (QCascii_compatible_p
,
10527 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
10528 CODING_ATTR_PLIST (attrs
))));
10530 eol_type
= args
[coding_arg_eol_type
];
10531 if (! NILP (eol_type
)
10532 && ! EQ (eol_type
, Qunix
)
10533 && ! EQ (eol_type
, Qdos
)
10534 && ! EQ (eol_type
, Qmac
))
10535 error ("Invalid eol-type");
10537 aliases
= list1 (name
);
10539 if (NILP (eol_type
))
10541 eol_type
= make_subsidiaries (name
);
10542 for (i
= 0; i
< 3; i
++)
10544 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10546 this_name
= AREF (eol_type
, i
);
10547 this_aliases
= list1 (this_name
);
10548 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10549 this_spec
= make_uninit_vector (3);
10550 ASET (this_spec
, 0, attrs
);
10551 ASET (this_spec
, 1, this_aliases
);
10552 ASET (this_spec
, 2, this_eol_type
);
10553 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10554 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10555 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
, Qnil
);
10557 Vcoding_system_alist
10558 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10559 Vcoding_system_alist
);
10563 spec_vec
= make_uninit_vector (3);
10564 ASET (spec_vec
, 0, attrs
);
10565 ASET (spec_vec
, 1, aliases
);
10566 ASET (spec_vec
, 2, eol_type
);
10568 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10569 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10570 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
, Qnil
);
10572 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10573 Vcoding_system_alist
);
10576 int id
= coding_categories
[category
].id
;
10578 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10579 setup_coding_system (name
, &coding_categories
[category
]);
10585 Fsignal (Qwrong_number_of_arguments
,
10586 Fcons (intern ("define-coding-system-internal"),
10587 make_number (nargs
)));
10591 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10593 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10594 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10596 Lisp_Object spec
, attrs
;
10598 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10599 attrs
= AREF (spec
, 0);
10600 if (EQ (prop
, QCmnemonic
))
10602 if (! STRINGP (val
))
10603 CHECK_CHARACTER (val
);
10604 ASET (attrs
, coding_attr_mnemonic
, val
);
10606 else if (EQ (prop
, QCdefault_char
))
10609 val
= make_number (' ');
10611 CHECK_CHARACTER (val
);
10612 ASET (attrs
, coding_attr_default_char
, val
);
10614 else if (EQ (prop
, QCdecode_translation_table
))
10616 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10617 CHECK_SYMBOL (val
);
10618 ASET (attrs
, coding_attr_decode_tbl
, val
);
10620 else if (EQ (prop
, QCencode_translation_table
))
10622 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10623 CHECK_SYMBOL (val
);
10624 ASET (attrs
, coding_attr_encode_tbl
, val
);
10626 else if (EQ (prop
, QCpost_read_conversion
))
10628 CHECK_SYMBOL (val
);
10629 ASET (attrs
, coding_attr_post_read
, val
);
10631 else if (EQ (prop
, QCpre_write_conversion
))
10633 CHECK_SYMBOL (val
);
10634 ASET (attrs
, coding_attr_pre_write
, val
);
10636 else if (EQ (prop
, QCascii_compatible_p
))
10638 ASET (attrs
, coding_attr_ascii_compat
, val
);
10641 ASET (attrs
, coding_attr_plist
,
10642 Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
));
10647 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10648 Sdefine_coding_system_alias
, 2, 2, 0,
10649 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10650 (Lisp_Object alias
, Lisp_Object coding_system
)
10652 Lisp_Object spec
, aliases
, eol_type
, val
;
10654 CHECK_SYMBOL (alias
);
10655 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10656 aliases
= AREF (spec
, 1);
10657 /* ALIASES should be a list of length more than zero, and the first
10658 element is a base coding system. Append ALIAS at the tail of the
10660 while (!NILP (XCDR (aliases
)))
10661 aliases
= XCDR (aliases
);
10662 XSETCDR (aliases
, list1 (alias
));
10664 eol_type
= AREF (spec
, 2);
10665 if (VECTORP (eol_type
))
10667 Lisp_Object subsidiaries
;
10670 subsidiaries
= make_subsidiaries (alias
);
10671 for (i
= 0; i
< 3; i
++)
10672 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10673 AREF (eol_type
, i
));
10676 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10677 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10678 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
, Qnil
);
10680 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10681 Vcoding_system_alist
);
10686 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10688 doc
: /* Return the base of CODING-SYSTEM.
10689 Any alias or subsidiary coding system is not a base coding system. */)
10690 (Lisp_Object coding_system
)
10692 Lisp_Object spec
, attrs
;
10694 if (NILP (coding_system
))
10695 return (Qno_conversion
);
10696 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10697 attrs
= AREF (spec
, 0);
10698 return CODING_ATTR_BASE_NAME (attrs
);
10701 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10703 doc
: /* Return the property list of CODING-SYSTEM. */)
10704 (Lisp_Object coding_system
)
10706 Lisp_Object spec
, attrs
;
10708 if (NILP (coding_system
))
10709 coding_system
= Qno_conversion
;
10710 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10711 attrs
= AREF (spec
, 0);
10712 return CODING_ATTR_PLIST (attrs
);
10716 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10718 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10719 (Lisp_Object coding_system
)
10723 if (NILP (coding_system
))
10724 coding_system
= Qno_conversion
;
10725 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10726 return AREF (spec
, 1);
10729 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10730 Scoding_system_eol_type
, 1, 1, 0,
10731 doc
: /* Return eol-type of CODING-SYSTEM.
10732 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10734 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10735 and CR respectively.
10737 A vector value indicates that a format of end-of-line should be
10738 detected automatically. Nth element of the vector is the subsidiary
10739 coding system whose eol-type is N. */)
10740 (Lisp_Object coding_system
)
10742 Lisp_Object spec
, eol_type
;
10745 if (NILP (coding_system
))
10746 coding_system
= Qno_conversion
;
10747 if (! CODING_SYSTEM_P (coding_system
))
10749 spec
= CODING_SYSTEM_SPEC (coding_system
);
10750 eol_type
= AREF (spec
, 2);
10751 if (VECTORP (eol_type
))
10752 return Fcopy_sequence (eol_type
);
10753 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10754 return make_number (n
);
10760 /*** 9. Post-amble ***/
10763 init_coding_once (void)
10767 for (i
= 0; i
< coding_category_max
; i
++)
10769 coding_categories
[i
].id
= -1;
10770 coding_priorities
[i
] = i
;
10773 /* ISO2022 specific initialize routine. */
10774 for (i
= 0; i
< 0x20; i
++)
10775 iso_code_class
[i
] = ISO_control_0
;
10776 for (i
= 0x21; i
< 0x7F; i
++)
10777 iso_code_class
[i
] = ISO_graphic_plane_0
;
10778 for (i
= 0x80; i
< 0xA0; i
++)
10779 iso_code_class
[i
] = ISO_control_1
;
10780 for (i
= 0xA1; i
< 0xFF; i
++)
10781 iso_code_class
[i
] = ISO_graphic_plane_1
;
10782 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10783 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10784 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10785 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10786 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10787 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10788 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10789 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10790 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10792 for (i
= 0; i
< 256; i
++)
10794 emacs_mule_bytes
[i
] = 1;
10796 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10797 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10798 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10799 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10805 syms_of_coding (void)
10807 staticpro (&Vcoding_system_hash_table
);
10808 Vcoding_system_hash_table
= CALLN (Fmake_hash_table
, QCtest
, Qeq
);
10810 staticpro (&Vsjis_coding_system
);
10811 Vsjis_coding_system
= Qnil
;
10813 staticpro (&Vbig5_coding_system
);
10814 Vbig5_coding_system
= Qnil
;
10816 staticpro (&Vcode_conversion_reused_workbuf
);
10817 Vcode_conversion_reused_workbuf
= Qnil
;
10819 staticpro (&Vcode_conversion_workbuf_name
);
10820 Vcode_conversion_workbuf_name
= build_pure_c_string (" *code-conversion-work*");
10822 reused_workbuf_in_use
= 0;
10824 DEFSYM (Qcharset
, "charset");
10825 DEFSYM (Qtarget_idx
, "target-idx");
10826 DEFSYM (Qcoding_system_history
, "coding-system-history");
10827 Fset (Qcoding_system_history
, Qnil
);
10829 /* Target FILENAME is the first argument. */
10830 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10831 /* Target FILENAME is the third argument. */
10832 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10834 DEFSYM (Qcall_process
, "call-process");
10835 /* Target PROGRAM is the first argument. */
10836 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10838 DEFSYM (Qcall_process_region
, "call-process-region");
10839 /* Target PROGRAM is the third argument. */
10840 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10842 DEFSYM (Qstart_process
, "start-process");
10843 /* Target PROGRAM is the third argument. */
10844 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10846 DEFSYM (Qopen_network_stream
, "open-network-stream");
10847 /* Target SERVICE is the fourth argument. */
10848 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10850 DEFSYM (Qunix
, "unix");
10851 DEFSYM (Qdos
, "dos");
10852 DEFSYM (Qmac
, "mac");
10854 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10855 DEFSYM (Qundecided
, "undecided");
10856 DEFSYM (Qno_conversion
, "no-conversion");
10857 DEFSYM (Qraw_text
, "raw-text");
10859 DEFSYM (Qiso_2022
, "iso-2022");
10861 DEFSYM (Qutf_8
, "utf-8");
10862 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10864 #if defined (WINDOWSNT) || defined (CYGWIN)
10865 /* No, not utf-16-le: that one has a BOM. */
10866 DEFSYM (Qutf_16le
, "utf-16le");
10869 DEFSYM (Qutf_16
, "utf-16");
10870 DEFSYM (Qbig
, "big");
10871 DEFSYM (Qlittle
, "little");
10873 DEFSYM (Qshift_jis
, "shift-jis");
10874 DEFSYM (Qbig5
, "big5");
10876 DEFSYM (Qcoding_system_p
, "coding-system-p");
10878 /* Error signaled when there's a problem with detecting a coding system. */
10879 DEFSYM (Qcoding_system_error
, "coding-system-error");
10880 Fput (Qcoding_system_error
, Qerror_conditions
,
10881 listn (CONSTYPE_PURE
, 2, Qcoding_system_error
, Qerror
));
10882 Fput (Qcoding_system_error
, Qerror_message
,
10883 build_pure_c_string ("Invalid coding system"));
10885 DEFSYM (Qtranslation_table
, "translation-table");
10886 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10887 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10889 /* Coding system emacs-mule and raw-text are for converting only
10890 end-of-line format. */
10891 DEFSYM (Qemacs_mule
, "emacs-mule");
10893 DEFSYM (QCcategory
, ":category");
10894 DEFSYM (QCmnemonic
, ":mnemonic");
10895 DEFSYM (QCdefault_char
, ":default-char");
10896 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10897 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10898 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10899 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10900 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10902 Vcoding_category_table
10903 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10904 staticpro (&Vcoding_category_table
);
10905 /* Followings are target of code detection. */
10906 ASET (Vcoding_category_table
, coding_category_iso_7
,
10907 intern_c_string ("coding-category-iso-7"));
10908 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10909 intern_c_string ("coding-category-iso-7-tight"));
10910 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10911 intern_c_string ("coding-category-iso-8-1"));
10912 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10913 intern_c_string ("coding-category-iso-8-2"));
10914 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10915 intern_c_string ("coding-category-iso-7-else"));
10916 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10917 intern_c_string ("coding-category-iso-8-else"));
10918 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10919 intern_c_string ("coding-category-utf-8-auto"));
10920 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10921 intern_c_string ("coding-category-utf-8"));
10922 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10923 intern_c_string ("coding-category-utf-8-sig"));
10924 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10925 intern_c_string ("coding-category-utf-16-be"));
10926 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10927 intern_c_string ("coding-category-utf-16-auto"));
10928 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10929 intern_c_string ("coding-category-utf-16-le"));
10930 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10931 intern_c_string ("coding-category-utf-16-be-nosig"));
10932 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10933 intern_c_string ("coding-category-utf-16-le-nosig"));
10934 ASET (Vcoding_category_table
, coding_category_charset
,
10935 intern_c_string ("coding-category-charset"));
10936 ASET (Vcoding_category_table
, coding_category_sjis
,
10937 intern_c_string ("coding-category-sjis"));
10938 ASET (Vcoding_category_table
, coding_category_big5
,
10939 intern_c_string ("coding-category-big5"));
10940 ASET (Vcoding_category_table
, coding_category_ccl
,
10941 intern_c_string ("coding-category-ccl"));
10942 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10943 intern_c_string ("coding-category-emacs-mule"));
10944 /* Followings are NOT target of code detection. */
10945 ASET (Vcoding_category_table
, coding_category_raw_text
,
10946 intern_c_string ("coding-category-raw-text"));
10947 ASET (Vcoding_category_table
, coding_category_undecided
,
10948 intern_c_string ("coding-category-undecided"));
10950 DEFSYM (Qinsufficient_source
, "insufficient-source");
10951 DEFSYM (Qinvalid_source
, "invalid-source");
10952 DEFSYM (Qinterrupted
, "interrupted");
10954 /* If a symbol has this property, evaluate the value to define the
10955 symbol as a coding system. */
10956 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10958 defsubr (&Scoding_system_p
);
10959 defsubr (&Sread_coding_system
);
10960 defsubr (&Sread_non_nil_coding_system
);
10961 defsubr (&Scheck_coding_system
);
10962 defsubr (&Sdetect_coding_region
);
10963 defsubr (&Sdetect_coding_string
);
10964 defsubr (&Sfind_coding_systems_region_internal
);
10965 defsubr (&Sunencodable_char_position
);
10966 defsubr (&Scheck_coding_systems_region
);
10967 defsubr (&Sdecode_coding_region
);
10968 defsubr (&Sencode_coding_region
);
10969 defsubr (&Sdecode_coding_string
);
10970 defsubr (&Sencode_coding_string
);
10971 defsubr (&Sdecode_sjis_char
);
10972 defsubr (&Sencode_sjis_char
);
10973 defsubr (&Sdecode_big5_char
);
10974 defsubr (&Sencode_big5_char
);
10975 defsubr (&Sset_terminal_coding_system_internal
);
10976 defsubr (&Sset_safe_terminal_coding_system_internal
);
10977 defsubr (&Sterminal_coding_system
);
10978 defsubr (&Sset_keyboard_coding_system_internal
);
10979 defsubr (&Skeyboard_coding_system
);
10980 defsubr (&Sfind_operation_coding_system
);
10981 defsubr (&Sset_coding_system_priority
);
10982 defsubr (&Sdefine_coding_system_internal
);
10983 defsubr (&Sdefine_coding_system_alias
);
10984 defsubr (&Scoding_system_put
);
10985 defsubr (&Scoding_system_base
);
10986 defsubr (&Scoding_system_plist
);
10987 defsubr (&Scoding_system_aliases
);
10988 defsubr (&Scoding_system_eol_type
);
10989 defsubr (&Scoding_system_priority_list
);
10991 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10992 doc
: /* List of coding systems.
10994 Do not alter the value of this variable manually. This variable should be
10995 updated by the functions `define-coding-system' and
10996 `define-coding-system-alias'. */);
10997 Vcoding_system_list
= Qnil
;
10999 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
11000 doc
: /* Alist of coding system names.
11001 Each element is one element list of coding system name.
11002 This variable is given to `completing-read' as COLLECTION argument.
11004 Do not alter the value of this variable manually. This variable should be
11005 updated by the functions `make-coding-system' and
11006 `define-coding-system-alias'. */);
11007 Vcoding_system_alist
= Qnil
;
11009 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
11010 doc
: /* List of coding-categories (symbols) ordered by priority.
11012 On detecting a coding system, Emacs tries code detection algorithms
11013 associated with each coding-category one by one in this order. When
11014 one algorithm agrees with a byte sequence of source text, the coding
11015 system bound to the corresponding coding-category is selected.
11017 Don't modify this variable directly, but use `set-coding-system-priority'. */);
11021 Vcoding_category_list
= Qnil
;
11022 for (i
= coding_category_max
- 1; i
>= 0; i
--)
11023 Vcoding_category_list
11024 = Fcons (AREF (Vcoding_category_table
, i
),
11025 Vcoding_category_list
);
11028 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
11029 doc
: /* Specify the coding system for read operations.
11030 It is useful to bind this variable with `let', but do not set it globally.
11031 If the value is a coding system, it is used for decoding on read operation.
11032 If not, an appropriate element is used from one of the coding system alists.
11033 There are three such tables: `file-coding-system-alist',
11034 `process-coding-system-alist', and `network-coding-system-alist'. */);
11035 Vcoding_system_for_read
= Qnil
;
11037 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
11038 doc
: /* Specify the coding system for write operations.
11039 Programs bind this variable with `let', but you should not set it globally.
11040 If the value is a coding system, it is used for encoding of output,
11041 when writing it to a file and when sending it to a file or subprocess.
11043 If this does not specify a coding system, an appropriate element
11044 is used from one of the coding system alists.
11045 There are three such tables: `file-coding-system-alist',
11046 `process-coding-system-alist', and `network-coding-system-alist'.
11047 For output to files, if the above procedure does not specify a coding system,
11048 the value of `buffer-file-coding-system' is used. */);
11049 Vcoding_system_for_write
= Qnil
;
11051 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
11053 Coding system used in the latest file or process I/O. */);
11054 Vlast_coding_system_used
= Qnil
;
11056 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
11058 Error status of the last code conversion.
11060 When an error was detected in the last code conversion, this variable
11061 is set to one of the following symbols.
11062 `insufficient-source'
11066 `insufficient-memory'
11067 When no error was detected, the value doesn't change. So, to check
11068 the error status of a code conversion by this variable, you must
11069 explicitly set this variable to nil before performing code
11071 Vlast_code_conversion_error
= Qnil
;
11073 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
11075 Non-nil means always inhibit code conversion of end-of-line format.
11076 See info node `Coding Systems' and info node `Text and Binary' concerning
11077 such conversion. */);
11078 inhibit_eol_conversion
= 0;
11080 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
11082 Non-nil means process buffer inherits coding system of process output.
11083 Bind it to t if the process output is to be treated as if it were a file
11084 read from some filesystem. */);
11085 inherit_process_coding_system
= 0;
11087 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
11089 Alist to decide a coding system to use for a file I/O operation.
11090 The format is ((PATTERN . VAL) ...),
11091 where PATTERN is a regular expression matching a file name,
11092 VAL is a coding system, a cons of coding systems, or a function symbol.
11093 If VAL is a coding system, it is used for both decoding and encoding
11095 If VAL is a cons of coding systems, the car part is used for decoding,
11096 and the cdr part is used for encoding.
11097 If VAL is a function symbol, the function must return a coding system
11098 or a cons of coding systems which are used as above. The function is
11099 called with an argument that is a list of the arguments with which
11100 `find-operation-coding-system' was called. If the function can't decide
11101 a coding system, it can return `undecided' so that the normal
11102 code-detection is performed.
11104 See also the function `find-operation-coding-system'
11105 and the variable `auto-coding-alist'. */);
11106 Vfile_coding_system_alist
= Qnil
;
11108 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
11110 Alist to decide a coding system to use for a process I/O operation.
11111 The format is ((PATTERN . VAL) ...),
11112 where PATTERN is a regular expression matching a program name,
11113 VAL is a coding system, a cons of coding systems, or a function symbol.
11114 If VAL is a coding system, it is used for both decoding what received
11115 from the program and encoding what sent to the program.
11116 If VAL is a cons of coding systems, the car part is used for decoding,
11117 and the cdr part is used for encoding.
11118 If VAL is a function symbol, the function must return a coding system
11119 or a cons of coding systems which are used as above.
11121 See also the function `find-operation-coding-system'. */);
11122 Vprocess_coding_system_alist
= Qnil
;
11124 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
11126 Alist to decide a coding system to use for a network I/O operation.
11127 The format is ((PATTERN . VAL) ...),
11128 where PATTERN is a regular expression matching a network service name
11129 or is a port number to connect to,
11130 VAL is a coding system, a cons of coding systems, or a function symbol.
11131 If VAL is a coding system, it is used for both decoding what received
11132 from the network stream and encoding what sent to the network stream.
11133 If VAL is a cons of coding systems, the car part is used for decoding,
11134 and the cdr part is used for encoding.
11135 If VAL is a function symbol, the function must return a coding system
11136 or a cons of coding systems which are used as above.
11138 See also the function `find-operation-coding-system'. */);
11139 Vnetwork_coding_system_alist
= Qnil
;
11141 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
11142 doc
: /* Coding system to use with system messages.
11143 Also used for decoding keyboard input on X Window system, and for
11144 encoding standard output and error streams. */);
11145 Vlocale_coding_system
= Qnil
;
11147 /* The eol mnemonics are reset in startup.el system-dependently. */
11148 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
11150 String displayed in mode line for UNIX-like (LF) end-of-line format. */);
11151 eol_mnemonic_unix
= build_pure_c_string (":");
11153 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
11155 String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
11156 eol_mnemonic_dos
= build_pure_c_string ("\\");
11158 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
11160 String displayed in mode line for MAC-like (CR) end-of-line format. */);
11161 eol_mnemonic_mac
= build_pure_c_string ("/");
11163 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
11165 String displayed in mode line when end-of-line format is not yet determined. */);
11166 eol_mnemonic_undecided
= build_pure_c_string (":");
11168 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
11170 Non-nil enables character translation while encoding and decoding. */);
11171 Venable_character_translation
= Qt
;
11173 DEFVAR_LISP ("standard-translation-table-for-decode",
11174 Vstandard_translation_table_for_decode
,
11175 doc
: /* Table for translating characters while decoding. */);
11176 Vstandard_translation_table_for_decode
= Qnil
;
11178 DEFVAR_LISP ("standard-translation-table-for-encode",
11179 Vstandard_translation_table_for_encode
,
11180 doc
: /* Table for translating characters while encoding. */);
11181 Vstandard_translation_table_for_encode
= Qnil
;
11183 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
11184 doc
: /* Alist of charsets vs revision numbers.
11185 While encoding, if a charset (car part of an element) is found,
11186 designate it with the escape sequence identifying revision (cdr part
11187 of the element). */);
11188 Vcharset_revision_table
= Qnil
;
11190 DEFVAR_LISP ("default-process-coding-system",
11191 Vdefault_process_coding_system
,
11192 doc
: /* Cons of coding systems used for process I/O by default.
11193 The car part is used for decoding a process output,
11194 the cdr part is used for encoding a text to be sent to a process. */);
11195 Vdefault_process_coding_system
= Qnil
;
11197 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
11199 Table of extra Latin codes in the range 128..159 (inclusive).
11200 This is a vector of length 256.
11201 If Nth element is non-nil, the existence of code N in a file
11202 \(or output of subprocess) doesn't prevent it to be detected as
11203 a coding system of ISO 2022 variant which has a flag
11204 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
11205 or reading output of a subprocess.
11206 Only 128th through 159th elements have a meaning. */);
11207 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
11209 DEFVAR_LISP ("select-safe-coding-system-function",
11210 Vselect_safe_coding_system_function
,
11212 Function to call to select safe coding system for encoding a text.
11214 If set, this function is called to force a user to select a proper
11215 coding system which can encode the text in the case that a default
11216 coding system used in each operation can't encode the text. The
11217 function should take care that the buffer is not modified while
11218 the coding system is being selected.
11220 The default value is `select-safe-coding-system' (which see). */);
11221 Vselect_safe_coding_system_function
= Qnil
;
11223 DEFVAR_BOOL ("coding-system-require-warning",
11224 coding_system_require_warning
,
11225 doc
: /* Internal use only.
11226 If non-nil, on writing a file, `select-safe-coding-system-function' is
11227 called even if `coding-system-for-write' is non-nil. The command
11228 `universal-coding-system-argument' binds this variable to t temporarily. */);
11229 coding_system_require_warning
= 0;
11232 DEFVAR_BOOL ("inhibit-iso-escape-detection",
11233 inhibit_iso_escape_detection
,
11235 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
11237 When Emacs reads text, it tries to detect how the text is encoded.
11238 This code detection is sensitive to escape sequences. If Emacs sees
11239 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
11240 of the ISO2022 encodings, and decodes text by the corresponding coding
11241 system (e.g. `iso-2022-7bit').
11243 However, there may be a case that you want to read escape sequences in
11244 a file as is. In such a case, you can set this variable to non-nil.
11245 Then the code detection will ignore any escape sequences, and no text is
11246 detected as encoded in some ISO-2022 encoding. The result is that all
11247 escape sequences become visible in a buffer.
11249 The default value is nil, and it is strongly recommended not to change
11250 it. That is because many Emacs Lisp source files that contain
11251 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
11252 in Emacs's distribution, and they won't be decoded correctly on
11253 reading if you suppress escape sequence detection.
11255 The other way to read escape sequences in a file without decoding is
11256 to explicitly specify some coding system that doesn't use ISO-2022
11257 escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */);
11258 inhibit_iso_escape_detection
= 0;
11260 DEFVAR_BOOL ("inhibit-null-byte-detection",
11261 inhibit_null_byte_detection
,
11262 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
11263 By default, Emacs treats it as binary data, and does not attempt to
11264 decode it. The effect is as if you specified `no-conversion' for
11267 Set this to non-nil when a regular text happens to include null bytes.
11268 Examples are Index nodes of Info files and null-byte delimited output
11269 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
11270 decode text as usual. */);
11271 inhibit_null_byte_detection
= 0;
11273 DEFVAR_BOOL ("disable-ascii-optimization", disable_ascii_optimization
,
11274 doc
: /* If non-nil, Emacs does not optimize code decoder for ASCII files.
11275 Internal use only. Remove after the experimental optimizer becomes stable. */);
11276 disable_ascii_optimization
= 0;
11278 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
11279 doc
: /* Char table for translating self-inserting characters.
11280 This is applied to the result of input methods, not their input.
11281 See also `keyboard-translate-table'.
11283 Use of this variable for character code unification was rendered
11284 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
11285 internal character representation. */);
11286 Vtranslation_table_for_input
= Qnil
;
11288 Lisp_Object args
[coding_arg_undecided_max
];
11289 memclear (args
, sizeof args
);
11291 Lisp_Object plist
[] =
11294 args
[coding_arg_name
] = Qno_conversion
,
11296 args
[coding_arg_mnemonic
] = make_number ('='),
11297 intern_c_string (":coding-type"),
11298 args
[coding_arg_coding_type
] = Qraw_text
,
11299 QCascii_compatible_p
,
11300 args
[coding_arg_ascii_compatible_p
] = Qt
,
11302 args
[coding_arg_default_char
] = make_number (0),
11303 intern_c_string (":for-unibyte"),
11304 args
[coding_arg_for_unibyte
] = Qt
,
11305 intern_c_string (":docstring"),
11306 (build_pure_c_string
11307 ("Do no conversion.\n"
11309 "When you visit a file with this coding, the file is read into a\n"
11310 "unibyte buffer as is, thus each byte of a file is treated as a\n"
11312 intern_c_string (":eol-type"),
11313 args
[coding_arg_eol_type
] = Qunix
,
11315 args
[coding_arg_plist
] = CALLMANY (Flist
, plist
);
11316 Fdefine_coding_system_internal (coding_arg_max
, args
);
11318 plist
[1] = args
[coding_arg_name
] = Qundecided
;
11319 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
11320 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
11321 /* This is already set.
11322 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
11323 plist
[8] = intern_c_string (":charset-list");
11324 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
11325 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
11326 plist
[13] = build_pure_c_string ("No conversion on encoding, "
11327 "automatic conversion on decoding.");
11328 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
11329 args
[coding_arg_plist
] = CALLMANY (Flist
, plist
);
11330 args
[coding_arg_undecided_inhibit_null_byte_detection
] = make_number (0);
11331 args
[coding_arg_undecided_inhibit_iso_escape_detection
] = make_number (0);
11332 Fdefine_coding_system_internal (coding_arg_undecided_max
, args
);
11334 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
11336 for (int i
= 0; i
< coding_category_max
; i
++)
11337 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
11339 #if defined (DOS_NT)
11340 system_eol_type
= Qdos
;
11342 system_eol_type
= Qunix
;
11344 staticpro (&system_eol_type
);