1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2015 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
16 (at 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 <http://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. MacOS'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 a 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
)
1011 if (STRING_BYTES_BOUND
- coding
->dst_bytes
< bytes
)
1013 coding
->destination
= xrealloc (coding
->destination
,
1014 coding
->dst_bytes
+ bytes
);
1015 coding
->dst_bytes
+= bytes
;
1019 coding_alloc_by_making_gap (struct coding_system
*coding
,
1020 ptrdiff_t gap_head_used
, ptrdiff_t bytes
)
1022 if (EQ (coding
->src_object
, coding
->dst_object
))
1024 /* The gap may contain the produced data at the head and not-yet
1025 consumed data at the tail. To preserve those data, we at
1026 first make the gap size to zero, then increase the gap
1028 ptrdiff_t add
= GAP_SIZE
;
1030 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1031 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1033 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1034 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1037 make_gap_1 (XBUFFER (coding
->dst_object
), bytes
);
1041 static unsigned char *
1042 alloc_destination (struct coding_system
*coding
, ptrdiff_t nbytes
,
1045 ptrdiff_t offset
= dst
- coding
->destination
;
1047 if (BUFFERP (coding
->dst_object
))
1049 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1051 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1054 coding_alloc_by_realloc (coding
, nbytes
);
1055 coding_set_destination (coding
);
1056 dst
= coding
->destination
+ offset
;
1060 /** Macros for annotations. */
1062 /* An annotation data is stored in the array coding->charbuf in this
1064 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1065 LENGTH is the number of elements in the annotation.
1066 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1067 NCHARS is the number of characters in the text annotated.
1069 The format of the following elements depend on ANNOTATION_MASK.
1071 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1073 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1075 NBYTES is the number of bytes specified in the header part of
1076 old-style emacs-mule encoding, or 0 for the other kind of
1079 METHOD is one of enum composition_method.
1081 Optional COMPOSITION-COMPONENTS are characters and composition
1084 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1087 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1088 recover from an invalid annotation, and should be skipped by
1089 produce_annotation. */
1091 /* Maximum length of the header of annotation data. */
1092 #define MAX_ANNOTATION_LENGTH 5
1094 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1096 *(buf)++ = -(len); \
1097 *(buf)++ = (mask); \
1098 *(buf)++ = (nchars); \
1099 coding->annotated = 1; \
1102 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1104 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1110 #define ADD_CHARSET_DATA(buf, nchars, id) \
1112 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1117 /* Bitmasks for coding->eol_seen. */
1119 #define EOL_SEEN_NONE 0
1120 #define EOL_SEEN_LF 1
1121 #define EOL_SEEN_CR 2
1122 #define EOL_SEEN_CRLF 4
1125 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1132 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1133 Return true if a text is encoded in UTF-8. */
1135 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1136 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1137 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1138 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1139 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1140 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1142 #define UTF_8_BOM_1 0xEF
1143 #define UTF_8_BOM_2 0xBB
1144 #define UTF_8_BOM_3 0xBF
1146 /* Unlike the other detect_coding_XXX, this function counts the number
1147 of characters and checks the EOL format. */
1150 detect_coding_utf_8 (struct coding_system
*coding
,
1151 struct coding_detection_info
*detect_info
)
1153 const unsigned char *src
= coding
->source
, *src_base
;
1154 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1155 bool multibytep
= coding
->src_multibyte
;
1156 ptrdiff_t consumed_chars
= 0;
1158 ptrdiff_t nchars
= coding
->head_ascii
;
1159 int eol_seen
= coding
->eol_seen
;
1161 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1162 /* A coding system of this category is always ASCII compatible. */
1165 if (src
== coding
->source
/* BOM should be at the head. */
1166 && src
+ 3 < src_end
/* BOM is 3-byte long. */
1167 && src
[0] == UTF_8_BOM_1
1168 && src
[1] == UTF_8_BOM_2
1169 && src
[2] == UTF_8_BOM_3
)
1178 int c
, c1
, c2
, c3
, c4
;
1182 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1187 if (src
< src_end
&& *src
== '\n')
1189 eol_seen
|= EOL_SEEN_CRLF
;
1194 eol_seen
|= EOL_SEEN_CR
;
1197 eol_seen
|= EOL_SEEN_LF
;
1201 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1203 if (UTF_8_2_OCTET_LEADING_P (c
))
1209 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1211 if (UTF_8_3_OCTET_LEADING_P (c
))
1217 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1219 if (UTF_8_4_OCTET_LEADING_P (c
))
1225 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1227 if (UTF_8_5_OCTET_LEADING_P (c
))
1234 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1238 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1240 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1245 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1246 detect_info
->found
|= CATEGORY_MASK_UTF_8_AUTO
| CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1250 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1251 if (nchars
< src_end
- coding
->source
)
1252 /* The found characters are less than source bytes, which
1253 means that we found a valid non-ASCII characters. */
1254 detect_info
->found
|= CATEGORY_MASK_UTF_8_AUTO
| CATEGORY_MASK_UTF_8_NOSIG
;
1256 coding
->detected_utf8_bytes
= src_base
- coding
->source
;
1257 coding
->detected_utf8_chars
= nchars
;
1263 decode_coding_utf_8 (struct coding_system
*coding
)
1265 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1266 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1267 const unsigned char *src_base
;
1268 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1269 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1270 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1271 bool multibytep
= coding
->src_multibyte
;
1272 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1274 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1275 int byte_after_cr
= -1;
1277 if (bom
!= utf_without_bom
)
1283 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1288 if (! UTF_8_EXTRA_OCTET_P (c2
))
1293 if (! UTF_8_EXTRA_OCTET_P (c3
))
1297 if ((c1
!= UTF_8_BOM_1
)
1298 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1301 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1306 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1310 int c
, c1
, c2
, c3
, c4
, c5
;
1313 consumed_chars_base
= consumed_chars
;
1315 if (charbuf
>= charbuf_end
)
1317 if (byte_after_cr
>= 0)
1322 /* In the simple case, rapidly handle ordinary characters */
1323 if (multibytep
&& ! eol_dos
1324 && charbuf
< charbuf_end
- 6 && src
< src_end
- 6)
1326 while (charbuf
< charbuf_end
- 6 && src
< src_end
- 6)
1356 /* If we handled at least one character, restart the main loop. */
1357 if (src
!= src_base
)
1361 if (byte_after_cr
>= 0)
1362 c1
= byte_after_cr
, byte_after_cr
= -1;
1369 else if (UTF_8_1_OCTET_P (c1
))
1371 if (eol_dos
&& c1
== '\r')
1372 ONE_MORE_BYTE (byte_after_cr
);
1378 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1380 if (UTF_8_2_OCTET_LEADING_P (c1
))
1382 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1383 /* Reject overlong sequences here and below. Encoders
1384 producing them are incorrect, they can be misleading,
1385 and they mess up read/write invariance. */
1392 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1394 if (UTF_8_3_OCTET_LEADING_P (c1
))
1396 c
= (((c1
& 0xF) << 12)
1397 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1399 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1405 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1407 if (UTF_8_4_OCTET_LEADING_P (c1
))
1409 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1410 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1417 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1419 if (UTF_8_5_OCTET_LEADING_P (c1
))
1421 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1422 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1424 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1439 consumed_chars
= consumed_chars_base
;
1441 *charbuf
++ = ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1445 coding
->consumed_char
+= consumed_chars_base
;
1446 coding
->consumed
= src_base
- coding
->source
;
1447 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1452 encode_coding_utf_8 (struct coding_system
*coding
)
1454 bool multibytep
= coding
->dst_multibyte
;
1455 int *charbuf
= coding
->charbuf
;
1456 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1457 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1458 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1459 ptrdiff_t produced_chars
= 0;
1462 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1464 ASSURE_DESTINATION (3);
1465 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1466 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1471 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1473 while (charbuf
< charbuf_end
)
1475 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1477 ASSURE_DESTINATION (safe_room
);
1479 if (CHAR_BYTE8_P (c
))
1481 c
= CHAR_TO_BYTE8 (c
);
1486 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1487 for (p
= str
; p
< pend
; p
++)
1494 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1496 while (charbuf
< charbuf_end
)
1498 ASSURE_DESTINATION (safe_room
);
1500 if (CHAR_BYTE8_P (c
))
1501 *dst
++ = CHAR_TO_BYTE8 (c
);
1503 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1505 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
1507 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1508 coding
->produced_char
+= produced_chars
;
1509 coding
->produced
= dst
- coding
->destination
;
1514 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1515 Return true if a text is encoded in one of UTF-16 based coding systems. */
1517 #define UTF_16_HIGH_SURROGATE_P(val) \
1518 (((val) & 0xFC00) == 0xD800)
1520 #define UTF_16_LOW_SURROGATE_P(val) \
1521 (((val) & 0xFC00) == 0xDC00)
1525 detect_coding_utf_16 (struct coding_system
*coding
,
1526 struct coding_detection_info
*detect_info
)
1528 const unsigned char *src
= coding
->source
;
1529 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1530 bool multibytep
= coding
->src_multibyte
;
1533 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1534 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1535 && (coding
->src_chars
& 1))
1537 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1541 TWO_MORE_BYTES (c1
, c2
);
1542 if ((c1
== 0xFF) && (c2
== 0xFE))
1544 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1545 | CATEGORY_MASK_UTF_16_AUTO
);
1546 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1547 | CATEGORY_MASK_UTF_16_BE_NOSIG
1548 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1550 else if ((c1
== 0xFE) && (c2
== 0xFF))
1552 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1553 | CATEGORY_MASK_UTF_16_AUTO
);
1554 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1555 | CATEGORY_MASK_UTF_16_BE_NOSIG
1556 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1560 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1565 /* We check the dispersion of Eth and Oth bytes where E is even and
1566 O is odd. If both are high, we assume binary data.*/
1567 unsigned char e
[256], o
[256];
1568 unsigned e_num
= 1, o_num
= 1;
1575 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1576 |CATEGORY_MASK_UTF_16_BE
1577 | CATEGORY_MASK_UTF_16_LE
);
1579 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1580 != CATEGORY_MASK_UTF_16
)
1582 TWO_MORE_BYTES (c1
, c2
);
1590 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1597 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1608 decode_coding_utf_16 (struct coding_system
*coding
)
1610 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1611 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1612 const unsigned char *src_base
;
1613 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1614 /* We may produces at most 3 chars in one loop. */
1615 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1616 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1617 bool multibytep
= coding
->src_multibyte
;
1618 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1619 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1620 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1622 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1623 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1625 if (bom
== utf_with_bom
)
1634 if (endian
== utf_16_big_endian
1635 ? c
!= 0xFEFF : c
!= 0xFFFE)
1637 /* The first two bytes are not BOM. Treat them as bytes
1638 for a normal character. */
1641 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1643 else if (bom
== utf_detect_bom
)
1645 /* We have already tried to detect BOM and failed in
1647 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1655 consumed_chars_base
= consumed_chars
;
1657 if (charbuf
>= charbuf_end
)
1659 if (byte_after_cr1
>= 0)
1664 if (byte_after_cr1
>= 0)
1665 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1673 if (byte_after_cr2
>= 0)
1674 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1679 *charbuf
++ = ASCII_CHAR_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1683 c
= (endian
== utf_16_big_endian
1684 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1688 if (! UTF_16_LOW_SURROGATE_P (c
))
1690 if (endian
== utf_16_big_endian
)
1691 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1693 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1696 if (UTF_16_HIGH_SURROGATE_P (c
))
1697 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1703 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1704 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1705 *charbuf
++ = 0x10000 + c
;
1710 if (UTF_16_HIGH_SURROGATE_P (c
))
1711 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1714 if (eol_dos
&& c
== '\r')
1716 ONE_MORE_BYTE (byte_after_cr1
);
1717 ONE_MORE_BYTE (byte_after_cr2
);
1725 coding
->consumed_char
+= consumed_chars_base
;
1726 coding
->consumed
= src_base
- coding
->source
;
1727 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1731 encode_coding_utf_16 (struct coding_system
*coding
)
1733 bool multibytep
= coding
->dst_multibyte
;
1734 int *charbuf
= coding
->charbuf
;
1735 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1736 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1737 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1739 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1740 bool big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1741 ptrdiff_t produced_chars
= 0;
1744 if (bom
!= utf_without_bom
)
1746 ASSURE_DESTINATION (safe_room
);
1748 EMIT_TWO_BYTES (0xFE, 0xFF);
1750 EMIT_TWO_BYTES (0xFF, 0xFE);
1751 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1754 while (charbuf
< charbuf_end
)
1756 ASSURE_DESTINATION (safe_room
);
1758 if (c
> MAX_UNICODE_CHAR
)
1759 c
= coding
->default_char
;
1764 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1766 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1773 c1
= (c
>> 10) + 0xD800;
1774 c2
= (c
& 0x3FF) + 0xDC00;
1776 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1778 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1781 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1782 coding
->produced
= dst
- coding
->destination
;
1783 coding
->produced_char
+= produced_chars
;
1788 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1790 /* Emacs' internal format for representation of multiple character
1791 sets is a kind of multi-byte encoding, i.e. characters are
1792 represented by variable-length sequences of one-byte codes.
1794 ASCII characters and control characters (e.g. `tab', `newline') are
1795 represented by one-byte sequences which are their ASCII codes, in
1796 the range 0x00 through 0x7F.
1798 8-bit characters of the range 0x80..0x9F are represented by
1799 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1802 8-bit characters of the range 0xA0..0xFF are represented by
1803 one-byte sequences which are their 8-bit code.
1805 The other characters are represented by a sequence of `base
1806 leading-code', optional `extended leading-code', and one or two
1807 `position-code's. The length of the sequence is determined by the
1808 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1809 whereas extended leading-code and position-code take the range 0xA0
1810 through 0xFF. See `charset.h' for more details about leading-code
1813 --- CODE RANGE of Emacs' internal format ---
1817 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1818 eight-bit-graphic 0xA0..0xBF
1819 ELSE 0x81..0x9D + [0xA0..0xFF]+
1820 ---------------------------------------------
1822 As this is the internal character representation, the format is
1823 usually not used externally (i.e. in a file or in a data sent to a
1824 process). But, it is possible to have a text externally in this
1825 format (i.e. by encoding by the coding system `emacs-mule').
1827 In that case, a sequence of one-byte codes has a slightly different
1830 At first, all characters in eight-bit-control are represented by
1831 one-byte sequences which are their 8-bit code.
1833 Next, character composition data are represented by the byte
1834 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1836 METHOD is 0xF2 plus one of composition method (enum
1837 composition_method),
1839 BYTES is 0xA0 plus a byte length of this composition data,
1841 CHARS is 0xA0 plus a number of characters composed by this
1844 COMPONENTs are characters of multibyte form or composition
1845 rules encoded by two-byte of ASCII codes.
1847 In addition, for backward compatibility, the following formats are
1848 also recognized as composition data on decoding.
1851 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1854 MSEQ is a multibyte form but in these special format:
1855 ASCII: 0xA0 ASCII_CODE+0x80,
1856 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1857 RULE is a one byte code of the range 0xA0..0xF0 that
1858 represents a composition rule.
1861 char emacs_mule_bytes
[256];
1864 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1865 Return true if a text is encoded in 'emacs-mule'. */
1868 detect_coding_emacs_mule (struct coding_system
*coding
,
1869 struct coding_detection_info
*detect_info
)
1871 const unsigned char *src
= coding
->source
, *src_base
;
1872 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1873 bool multibytep
= coding
->src_multibyte
;
1874 ptrdiff_t consumed_chars
= 0;
1878 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1879 /* A coding system of this category is always ASCII compatible. */
1880 src
+= coding
->head_ascii
;
1890 /* Perhaps the start of composite character. We simply skip
1891 it because analyzing it is too heavy for detecting. But,
1892 at least, we check that the composite character
1893 constitutes of more than 4 bytes. */
1894 const unsigned char *src_start
;
1904 if (src
- src_start
<= 4)
1906 found
= CATEGORY_MASK_EMACS_MULE
;
1914 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1919 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1921 while (more_bytes
> 0)
1926 src
--; /* Unread the last byte. */
1931 if (more_bytes
!= 0)
1933 found
= CATEGORY_MASK_EMACS_MULE
;
1936 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1940 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1942 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1945 detect_info
->found
|= found
;
1950 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1951 character. If CMP_STATUS indicates that we must expect MSEQ or
1952 RULE described above, decode it and return the negative value of
1953 the decoded character or rule. If an invalid byte is found, return
1954 -1. If SRC is too short, return -2. */
1957 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1958 int *nbytes
, int *nchars
, int *id
,
1959 struct composition_status
*cmp_status
)
1961 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1962 const unsigned char *src_base
= src
;
1963 bool multibytep
= coding
->src_multibyte
;
1967 ptrdiff_t consumed_chars
= 0;
1968 bool mseq_found
= 0;
1974 charset_ID
= emacs_mule_charset
[0];
1980 if (cmp_status
->state
!= COMPOSING_NO
1981 && cmp_status
->old_form
)
1983 if (cmp_status
->state
== COMPOSING_CHAR
)
1998 *nbytes
= src
- src_base
;
1999 *nchars
= consumed_chars
;
2007 switch (emacs_mule_bytes
[c
])
2010 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2019 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2020 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2023 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2032 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2037 code
= (c
& 0x7F) << 8;
2047 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2052 code
= (c
& 0x7F) << 8;
2061 charset_ID
= ASCII_CHAR_P (code
) ? charset_ascii
: charset_eight_bit
;
2067 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2068 CHARSET_FROM_ID (charset_ID
), code
, c
);
2072 *nbytes
= src
- src_base
;
2073 *nchars
= consumed_chars
;
2076 return (mseq_found
? -c
: c
);
2086 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2088 /* Handle these composition sequence ('|': the end of header elements,
2089 BYTES and CHARS >= 0xA0):
2091 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2092 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2093 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2097 (4) relative composition: 0x80 | MSEQ ... MSEQ
2098 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2100 When the starter 0x80 and the following header elements are found,
2101 this annotation header is produced.
2103 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2105 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2106 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2108 Then, upon reading the following elements, these codes are produced
2109 until the composition end is found:
2112 (2) ALT ... ALT CHAR ... CHAR
2113 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2115 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2117 When the composition end is found, LENGTH and NCHARS in the
2118 annotation header is updated as below:
2120 (1) LENGTH: unchanged, NCHARS: unchanged
2121 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2122 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2123 (4) LENGTH: unchanged, NCHARS: number of CHARs
2124 (5) LENGTH: unchanged, NCHARS: number of CHARs
2126 If an error is found while composing, the annotation header is
2127 changed to the original composition header (plus filler -1s) as
2130 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2131 (5) [ 0x80 0xFF -1 -1- -1 ]
2133 and the sequence [ -2 DECODED-RULE ] is changed to the original
2134 byte sequence as below:
2135 o the original byte sequence is B: [ B -1 ]
2136 o the original byte sequence is B1 B2: [ B1 B2 ]
2138 Most of the routines are implemented by macros because many
2139 variables and labels in the caller decode_coding_emacs_mule must be
2140 accessible, and they are usually called just once (thus doesn't
2141 increase the size of compiled object). */
2143 /* Decode a composition rule represented by C as a component of
2144 composition sequence of Emacs 20 style. Set RULE to the decoded
2147 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2152 if (c < 0 || c >= 81) \
2153 goto invalid_code; \
2154 gref = c / 9, nref = c % 9; \
2155 if (gref == 4) gref = 10; \
2156 if (nref == 4) nref = 10; \
2157 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2161 /* Decode a composition rule represented by C and the following byte
2162 at SRC as a component of composition sequence of Emacs 21 style.
2163 Set RULE to the decoded rule. */
2165 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2170 if (gref < 0 || gref >= 81) \
2171 goto invalid_code; \
2172 ONE_MORE_BYTE (c); \
2174 if (nref < 0 || nref >= 81) \
2175 goto invalid_code; \
2176 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2180 /* Start of Emacs 21 style format. The first three bytes at SRC are
2181 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2182 byte length of this composition information, CHARS is the number of
2183 characters composed by this composition. */
2185 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2187 enum composition_method method = c - 0xF2; \
2188 int nbytes, nchars; \
2190 ONE_MORE_BYTE (c); \
2192 goto invalid_code; \
2193 nbytes = c - 0xA0; \
2194 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2195 goto invalid_code; \
2196 ONE_MORE_BYTE (c); \
2197 nchars = c - 0xA0; \
2198 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2199 goto invalid_code; \
2200 cmp_status->old_form = 0; \
2201 cmp_status->method = method; \
2202 if (method == COMPOSITION_RELATIVE) \
2203 cmp_status->state = COMPOSING_CHAR; \
2205 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2206 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2207 cmp_status->nchars = nchars; \
2208 cmp_status->ncomps = nbytes - 4; \
2209 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2213 /* Start of Emacs 20 style format for relative composition. */
2215 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2217 cmp_status->old_form = 1; \
2218 cmp_status->method = COMPOSITION_RELATIVE; \
2219 cmp_status->state = COMPOSING_CHAR; \
2220 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2221 cmp_status->nchars = cmp_status->ncomps = 0; \
2222 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2226 /* Start of Emacs 20 style format for rule-base composition. */
2228 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2230 cmp_status->old_form = 1; \
2231 cmp_status->method = COMPOSITION_WITH_RULE; \
2232 cmp_status->state = COMPOSING_CHAR; \
2233 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2234 cmp_status->nchars = cmp_status->ncomps = 0; \
2235 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2239 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2241 const unsigned char *current_src = src; \
2243 ONE_MORE_BYTE (c); \
2245 goto invalid_code; \
2246 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2247 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2248 DECODE_EMACS_MULE_21_COMPOSITION (); \
2249 else if (c < 0xA0) \
2250 goto invalid_code; \
2251 else if (c < 0xC0) \
2253 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2254 /* Re-read C as a composition component. */ \
2255 src = current_src; \
2257 else if (c == 0xFF) \
2258 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2260 goto invalid_code; \
2263 #define EMACS_MULE_COMPOSITION_END() \
2265 int idx = - cmp_status->length; \
2267 if (cmp_status->old_form) \
2268 charbuf[idx + 2] = cmp_status->nchars; \
2269 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2270 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2271 cmp_status->state = COMPOSING_NO; \
2276 emacs_mule_finish_composition (int *charbuf
,
2277 struct composition_status
*cmp_status
)
2279 int idx
= - cmp_status
->length
;
2282 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2284 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2286 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2287 && cmp_status
->state
== COMPOSING_CHAR
)
2289 /* The last rule was invalid. */
2290 int rule
= charbuf
[-1] + 0xA0;
2292 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2299 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2301 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2303 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2304 charbuf
[idx
++] = -3;
2310 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2311 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2313 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2314 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2315 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2316 charbuf
[idx
++] = -1;
2320 cmp_status
->state
= COMPOSING_NO
;
2324 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2326 if (cmp_status->state != COMPOSING_NO) \
2327 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2332 decode_coding_emacs_mule (struct coding_system
*coding
)
2334 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2335 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2336 const unsigned char *src_base
;
2337 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2338 /* We may produce two annotations (charset and composition) in one
2339 loop and one more charset annotation at the end. */
2341 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2342 /* We can produce up to 2 characters in a loop. */
2344 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
2345 bool multibytep
= coding
->src_multibyte
;
2346 ptrdiff_t char_offset
= coding
->produced_char
;
2347 ptrdiff_t last_offset
= char_offset
;
2348 int last_id
= charset_ascii
;
2350 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2351 int byte_after_cr
= -1;
2352 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2354 if (cmp_status
->state
!= COMPOSING_NO
)
2358 if (charbuf_end
- charbuf
< cmp_status
->length
)
2360 for (i
= 0; i
< cmp_status
->length
; i
++)
2361 *charbuf
++ = cmp_status
->carryover
[i
];
2362 coding
->annotated
= 1;
2367 int c
, id
IF_LINT (= 0);
2370 consumed_chars_base
= consumed_chars
;
2372 if (charbuf
>= charbuf_end
)
2374 if (byte_after_cr
>= 0)
2379 if (byte_after_cr
>= 0)
2380 c
= byte_after_cr
, byte_after_cr
= -1;
2384 if (c
< 0 || c
== 0x80)
2386 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2393 DECODE_EMACS_MULE_COMPOSITION_START ();
2399 if (eol_dos
&& c
== '\r')
2400 ONE_MORE_BYTE (byte_after_cr
);
2402 if (cmp_status
->state
!= COMPOSING_NO
)
2404 if (cmp_status
->old_form
)
2405 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2406 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2407 cmp_status
->ncomps
--;
2412 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2413 /* emacs_mule_char can load a charset map from a file, which
2414 allocates a large structure and might cause buffer text
2415 to be relocated as result. Thus, we need to remember the
2416 original pointer to buffer text, and fix up all related
2417 pointers after the call. */
2418 const unsigned char *orig
= coding
->source
;
2421 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2423 offset
= coding
->source
- orig
;
2437 src
= src_base
+ nbytes
;
2438 consumed_chars
= consumed_chars_base
+ nchars
;
2439 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2440 cmp_status
->ncomps
-= nchars
;
2443 /* Now if C >= 0, we found a normally encoded character, if C <
2444 0, we found an old-style composition component character or
2447 if (cmp_status
->state
== COMPOSING_NO
)
2451 if (last_id
!= charset_ascii
)
2452 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2455 last_offset
= char_offset
;
2460 else if (cmp_status
->state
== COMPOSING_CHAR
)
2462 if (cmp_status
->old_form
)
2466 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2473 cmp_status
->nchars
++;
2474 cmp_status
->length
++;
2475 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2476 EMACS_MULE_COMPOSITION_END ();
2477 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2478 cmp_status
->state
= COMPOSING_RULE
;
2484 cmp_status
->length
++;
2485 cmp_status
->nchars
--;
2486 if (cmp_status
->nchars
== 0)
2487 EMACS_MULE_COMPOSITION_END ();
2490 else if (cmp_status
->state
== COMPOSING_RULE
)
2496 EMACS_MULE_COMPOSITION_END ();
2503 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2508 cmp_status
->length
+= 2;
2509 cmp_status
->state
= COMPOSING_CHAR
;
2512 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2515 cmp_status
->length
++;
2516 if (cmp_status
->ncomps
== 0)
2517 cmp_status
->state
= COMPOSING_CHAR
;
2518 else if (cmp_status
->ncomps
> 0)
2520 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2521 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2524 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2526 else /* COMPOSING_COMPONENT_RULE */
2530 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2535 cmp_status
->length
+= 2;
2536 cmp_status
->ncomps
--;
2537 if (cmp_status
->ncomps
> 0)
2538 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2540 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2545 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2547 consumed_chars
= consumed_chars_base
;
2549 *charbuf
++ = ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2554 if (cmp_status
->state
!= COMPOSING_NO
)
2556 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2557 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2562 charbuf
-= cmp_status
->length
;
2563 for (i
= 0; i
< cmp_status
->length
; i
++)
2564 cmp_status
->carryover
[i
] = charbuf
[i
];
2567 if (last_id
!= charset_ascii
)
2568 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2569 coding
->consumed_char
+= consumed_chars_base
;
2570 coding
->consumed
= src_base
- coding
->source
;
2571 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2575 #define EMACS_MULE_LEADING_CODES(id, codes) \
2578 codes[0] = id, codes[1] = 0; \
2579 else if (id < 0xE0) \
2580 codes[0] = 0x9A, codes[1] = id; \
2581 else if (id < 0xF0) \
2582 codes[0] = 0x9B, codes[1] = id; \
2583 else if (id < 0xF5) \
2584 codes[0] = 0x9C, codes[1] = id; \
2586 codes[0] = 0x9D, codes[1] = id; \
2591 encode_coding_emacs_mule (struct coding_system
*coding
)
2593 bool multibytep
= coding
->dst_multibyte
;
2594 int *charbuf
= coding
->charbuf
;
2595 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2596 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2597 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2599 ptrdiff_t produced_chars
= 0;
2600 Lisp_Object attrs
, charset_list
;
2602 int preferred_charset_id
= -1;
2604 CODING_GET_INFO (coding
, attrs
, charset_list
);
2605 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2607 charset_list
= Vemacs_mule_charset_list
;
2608 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2611 while (charbuf
< charbuf_end
)
2613 ASSURE_DESTINATION (safe_room
);
2618 /* Handle an annotation. */
2621 case CODING_ANNOTATE_COMPOSITION_MASK
:
2622 /* Not yet implemented. */
2624 case CODING_ANNOTATE_CHARSET_MASK
:
2625 preferred_charset_id
= charbuf
[3];
2626 if (preferred_charset_id
>= 0
2627 && NILP (Fmemq (make_number (preferred_charset_id
),
2629 preferred_charset_id
= -1;
2638 if (ASCII_CHAR_P (c
))
2639 EMIT_ONE_ASCII_BYTE (c
);
2640 else if (CHAR_BYTE8_P (c
))
2642 c
= CHAR_TO_BYTE8 (c
);
2647 struct charset
*charset
;
2651 unsigned char leading_codes
[2];
2653 if (preferred_charset_id
>= 0)
2657 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2658 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2660 code
= ENCODE_CHAR (charset
, c
);
2662 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2666 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2670 c
= coding
->default_char
;
2671 if (ASCII_CHAR_P (c
))
2673 EMIT_ONE_ASCII_BYTE (c
);
2676 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2679 dimension
= CHARSET_DIMENSION (charset
);
2680 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2681 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2682 EMIT_ONE_BYTE (leading_codes
[0]);
2683 if (leading_codes
[1])
2684 EMIT_ONE_BYTE (leading_codes
[1]);
2686 EMIT_ONE_BYTE (code
| 0x80);
2690 EMIT_ONE_BYTE (code
>> 8);
2691 EMIT_ONE_BYTE (code
& 0xFF);
2695 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2696 coding
->produced_char
+= produced_chars
;
2697 coding
->produced
= dst
- coding
->destination
;
2702 /*** 7. ISO2022 handlers ***/
2704 /* The following note describes the coding system ISO2022 briefly.
2705 Since the intention of this note is to help understand the
2706 functions in this file, some parts are NOT ACCURATE or are OVERLY
2707 SIMPLIFIED. For thorough understanding, please refer to the
2708 original document of ISO2022. This is equivalent to the standard
2709 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2711 ISO2022 provides many mechanisms to encode several character sets
2712 in 7-bit and 8-bit environments. For 7-bit environments, all text
2713 is encoded using bytes less than 128. This may make the encoded
2714 text a little bit longer, but the text passes more easily through
2715 several types of gateway, some of which strip off the MSB (Most
2718 There are two kinds of character sets: control character sets and
2719 graphic character sets. The former contain control characters such
2720 as `newline' and `escape' to provide control functions (control
2721 functions are also provided by escape sequences). The latter
2722 contain graphic characters such as 'A' and '-'. Emacs recognizes
2723 two control character sets and many graphic character sets.
2725 Graphic character sets are classified into one of the following
2726 four classes, according to the number of bytes (DIMENSION) and
2727 number of characters in one dimension (CHARS) of the set:
2728 - DIMENSION1_CHARS94
2729 - DIMENSION1_CHARS96
2730 - DIMENSION2_CHARS94
2731 - DIMENSION2_CHARS96
2733 In addition, each character set is assigned an identification tag,
2734 unique for each set, called the "final character" (denoted as <F>
2735 hereafter). The <F> of each character set is decided by ECMA(*)
2736 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2737 (0x30..0x3F are for private use only).
2739 Note (*): ECMA = European Computer Manufacturers Association
2741 Here are examples of graphic character sets [NAME(<F>)]:
2742 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2743 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2744 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2745 o DIMENSION2_CHARS96 -- none for the moment
2747 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2748 C0 [0x00..0x1F] -- control character plane 0
2749 GL [0x20..0x7F] -- graphic character plane 0
2750 C1 [0x80..0x9F] -- control character plane 1
2751 GR [0xA0..0xFF] -- graphic character plane 1
2753 A control character set is directly designated and invoked to C0 or
2754 C1 by an escape sequence. The most common case is that:
2755 - ISO646's control character set is designated/invoked to C0, and
2756 - ISO6429's control character set is designated/invoked to C1,
2757 and usually these designations/invocations are omitted in encoded
2758 text. In a 7-bit environment, only C0 can be used, and a control
2759 character for C1 is encoded by an appropriate escape sequence to
2760 fit into the environment. All control characters for C1 are
2761 defined to have corresponding escape sequences.
2763 A graphic character set is at first designated to one of four
2764 graphic registers (G0 through G3), then these graphic registers are
2765 invoked to GL or GR. These designations and invocations can be
2766 done independently. The most common case is that G0 is invoked to
2767 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2768 these invocations and designations are omitted in encoded text.
2769 In a 7-bit environment, only GL can be used.
2771 When a graphic character set of CHARS94 is invoked to GL, codes
2772 0x20 and 0x7F of the GL area work as control characters SPACE and
2773 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2776 There are two ways of invocation: locking-shift and single-shift.
2777 With locking-shift, the invocation lasts until the next different
2778 invocation, whereas with single-shift, the invocation affects the
2779 following character only and doesn't affect the locking-shift
2780 state. Invocations are done by the following control characters or
2783 ----------------------------------------------------------------------
2784 abbrev function cntrl escape seq description
2785 ----------------------------------------------------------------------
2786 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2787 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2788 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2789 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2790 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2791 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2792 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2793 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2794 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2795 ----------------------------------------------------------------------
2796 (*) These are not used by any known coding system.
2798 Control characters for these functions are defined by macros
2799 ISO_CODE_XXX in `coding.h'.
2801 Designations are done by the following escape sequences:
2802 ----------------------------------------------------------------------
2803 escape sequence description
2804 ----------------------------------------------------------------------
2805 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2806 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2807 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2808 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2809 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2810 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2811 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2812 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2813 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2814 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2815 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2816 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2817 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2818 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2819 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2820 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2821 ----------------------------------------------------------------------
2823 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2824 of dimension 1, chars 94, and final character <F>, etc...
2826 Note (*): Although these designations are not allowed in ISO2022,
2827 Emacs accepts them on decoding, and produces them on encoding
2828 CHARS96 character sets in a coding system which is characterized as
2829 7-bit environment, non-locking-shift, and non-single-shift.
2831 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2832 '(' must be omitted. We refer to this as "short-form" hereafter.
2834 Now you may notice that there are a lot of ways of encoding the
2835 same multilingual text in ISO2022. Actually, there exist many
2836 coding systems such as Compound Text (used in X11's inter client
2837 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2838 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2839 localized platforms), and all of these are variants of ISO2022.
2841 In addition to the above, Emacs handles two more kinds of escape
2842 sequences: ISO6429's direction specification and Emacs' private
2843 sequence for specifying character composition.
2845 ISO6429's direction specification takes the following form:
2846 o CSI ']' -- end of the current direction
2847 o CSI '0' ']' -- end of the current direction
2848 o CSI '1' ']' -- start of left-to-right text
2849 o CSI '2' ']' -- start of right-to-left text
2850 The control character CSI (0x9B: control sequence introducer) is
2851 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2853 Character composition specification takes the following form:
2854 o ESC '0' -- start relative composition
2855 o ESC '1' -- end composition
2856 o ESC '2' -- start rule-base composition (*)
2857 o ESC '3' -- start relative composition with alternate chars (**)
2858 o ESC '4' -- start rule-base composition with alternate chars (**)
2859 Since these are not standard escape sequences of any ISO standard,
2860 the use of them with these meanings is restricted to Emacs only.
2862 (*) This form is used only in Emacs 20.7 and older versions,
2863 but newer versions can safely decode it.
2864 (**) This form is used only in Emacs 21.1 and newer versions,
2865 and older versions can't decode it.
2867 Here's a list of example usages of these composition escape
2868 sequences (categorized by `enum composition_method').
2870 COMPOSITION_RELATIVE:
2871 ESC 0 CHAR [ CHAR ] ESC 1
2872 COMPOSITION_WITH_RULE:
2873 ESC 2 CHAR [ RULE CHAR ] ESC 1
2874 COMPOSITION_WITH_ALTCHARS:
2875 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2876 COMPOSITION_WITH_RULE_ALTCHARS:
2877 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2879 static enum iso_code_class_type iso_code_class
[256];
2881 #define SAFE_CHARSET_P(coding, id) \
2882 ((id) <= (coding)->max_charset_id \
2883 && (coding)->safe_charsets[id] != 255)
2886 setup_iso_safe_charsets (Lisp_Object attrs
)
2888 Lisp_Object charset_list
, safe_charsets
;
2889 Lisp_Object request
;
2890 Lisp_Object reg_usage
;
2892 EMACS_INT reg94
, reg96
;
2893 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2896 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2897 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2898 && ! EQ (charset_list
, Viso_2022_charset_list
))
2900 charset_list
= Viso_2022_charset_list
;
2901 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2902 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2905 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2909 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2911 int id
= XINT (XCAR (tail
));
2912 if (max_charset_id
< id
)
2913 max_charset_id
= id
;
2916 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2917 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2918 request
= AREF (attrs
, coding_attr_iso_request
);
2919 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2920 reg94
= XINT (XCAR (reg_usage
));
2921 reg96
= XINT (XCDR (reg_usage
));
2923 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2927 struct charset
*charset
;
2930 charset
= CHARSET_FROM_ID (XINT (id
));
2931 reg
= Fcdr (Fassq (id
, request
));
2933 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2934 else if (charset
->iso_chars_96
)
2937 SSET (safe_charsets
, XINT (id
), reg96
);
2942 SSET (safe_charsets
, XINT (id
), reg94
);
2945 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2949 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2950 Return true if a text is encoded in one of ISO-2022 based coding
2954 detect_coding_iso_2022 (struct coding_system
*coding
,
2955 struct coding_detection_info
*detect_info
)
2957 const unsigned char *src
= coding
->source
, *src_base
= src
;
2958 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2959 bool multibytep
= coding
->src_multibyte
;
2960 bool single_shifting
= 0;
2963 ptrdiff_t consumed_chars
= 0;
2967 int composition_count
= -1;
2969 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2971 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2973 struct coding_system
*this = &(coding_categories
[i
]);
2974 Lisp_Object attrs
, val
;
2978 attrs
= CODING_ID_ATTRS (this->id
);
2979 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2980 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2981 setup_iso_safe_charsets (attrs
);
2982 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2983 this->max_charset_id
= SCHARS (val
) - 1;
2984 this->safe_charsets
= SDATA (val
);
2987 /* A coding system of this category is always ASCII compatible. */
2988 src
+= coding
->head_ascii
;
2990 while (rejected
!= CATEGORY_MASK_ISO
)
2997 if (inhibit_iso_escape_detection
)
2999 single_shifting
= 0;
3001 if (c
== 'N' || c
== 'O')
3003 /* ESC <Fe> for SS2 or SS3. */
3004 single_shifting
= 1;
3005 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3009 /* End of composition. */
3010 if (composition_count
< 0
3011 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3014 composition_count
= -1;
3015 found
|= CATEGORY_MASK_ISO
;
3017 else if (c
>= '0' && c
<= '4')
3019 /* ESC <Fp> for start/end composition. */
3020 composition_count
= 0;
3024 if (c
>= '(' && c
<= '/')
3026 /* Designation sequence for a charset of dimension 1. */
3028 if (c1
< ' ' || c1
>= 0x80
3029 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3031 /* Invalid designation sequence. Just ignore. */
3033 rejected
|= (CATEGORY_MASK_ISO_7BIT
3034 | CATEGORY_MASK_ISO_7_ELSE
);
3040 /* Designation sequence for a charset of dimension 2. */
3042 if (c
>= '@' && c
<= 'B')
3043 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3044 id
= iso_charset_table
[1][0][c
];
3045 else if (c
>= '(' && c
<= '/')
3048 if (c1
< ' ' || c1
>= 0x80
3049 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3051 /* Invalid designation sequence. Just ignore. */
3053 rejected
|= (CATEGORY_MASK_ISO_7BIT
3054 | CATEGORY_MASK_ISO_7_ELSE
);
3060 /* Invalid designation sequence. Just ignore it. */
3062 rejected
|= (CATEGORY_MASK_ISO_7BIT
3063 | CATEGORY_MASK_ISO_7_ELSE
);
3069 /* Invalid escape sequence. Just ignore it. */
3071 rejected
|= (CATEGORY_MASK_ISO_7BIT
3072 | CATEGORY_MASK_ISO_7_ELSE
);
3076 /* We found a valid designation sequence for CHARSET. */
3077 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3078 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3080 found
|= CATEGORY_MASK_ISO_7
;
3082 rejected
|= CATEGORY_MASK_ISO_7
;
3083 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3085 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3087 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3088 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3090 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3092 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3093 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3095 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3097 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3103 /* Locking shift out/in. */
3104 if (inhibit_iso_escape_detection
)
3106 single_shifting
= 0;
3107 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3111 /* Control sequence introducer. */
3112 single_shifting
= 0;
3113 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3114 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3115 goto check_extra_latin
;
3120 if (inhibit_iso_escape_detection
)
3122 single_shifting
= 0;
3123 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3124 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3125 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3127 found
|= CATEGORY_MASK_ISO_8_1
;
3128 single_shifting
= 1;
3130 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3131 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3133 found
|= CATEGORY_MASK_ISO_8_2
;
3134 single_shifting
= 1;
3136 if (single_shifting
)
3138 goto check_extra_latin
;
3145 if (composition_count
>= 0)
3146 composition_count
++;
3147 single_shifting
= 0;
3150 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3153 found
|= CATEGORY_MASK_ISO_8_1
;
3154 /* Check the length of succeeding codes of the range
3155 0xA0..0FF. If the byte length is even, we include
3156 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3157 only when we are not single shifting. */
3158 if (! single_shifting
3159 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3162 while (src
< src_end
)
3174 if (len
& 1 && src
< src_end
)
3176 rejected
|= CATEGORY_MASK_ISO_8_2
;
3177 if (composition_count
>= 0)
3178 composition_count
+= len
;
3182 found
|= CATEGORY_MASK_ISO_8_2
;
3183 if (composition_count
>= 0)
3184 composition_count
+= len
/ 2;
3190 if (! VECTORP (Vlatin_extra_code_table
)
3191 || NILP (AREF (Vlatin_extra_code_table
, c
)))
3193 rejected
= CATEGORY_MASK_ISO
;
3196 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3197 & CODING_ISO_FLAG_LATIN_EXTRA
)
3198 found
|= CATEGORY_MASK_ISO_8_1
;
3200 rejected
|= CATEGORY_MASK_ISO_8_1
;
3201 rejected
|= CATEGORY_MASK_ISO_8_2
;
3205 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3209 detect_info
->rejected
|= rejected
;
3210 detect_info
->found
|= (found
& ~rejected
);
3215 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3216 escape sequence should be kept. */
3217 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3221 if (final < '0' || final >= 128 \
3222 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3223 || !SAFE_CHARSET_P (coding, id)) \
3225 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3229 prev = CODING_ISO_DESIGNATION (coding, reg); \
3230 if (id == charset_jisx0201_roman) \
3232 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3233 id = charset_ascii; \
3235 else if (id == charset_jisx0208_1978) \
3237 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3238 id = charset_jisx0208; \
3240 CODING_ISO_DESIGNATION (coding, reg) = id; \
3241 /* If there was an invalid designation to REG previously, and this \
3242 designation is ASCII to REG, we should keep this designation \
3244 if (prev == -2 && id == charset_ascii) \
3249 /* Handle these composition sequence (ALT: alternate char):
3251 (1) relative composition: ESC 0 CHAR ... ESC 1
3252 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3253 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3254 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3256 When the start sequence (ESC 0/2/3/4) is found, this annotation
3259 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3261 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3262 produced until the end sequence (ESC 1) is found:
3265 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3266 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3267 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3269 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3270 annotation header is updated as below:
3272 (1) LENGTH: unchanged, NCHARS: number of CHARs
3273 (2) LENGTH: unchanged, NCHARS: number of CHARs
3274 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3275 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3277 If an error is found while composing, the annotation header is
3280 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3282 and the sequence [ -2 DECODED-RULE ] is changed to the original
3283 byte sequence as below:
3284 o the original byte sequence is B: [ B -1 ]
3285 o the original byte sequence is B1 B2: [ B1 B2 ]
3286 and the sequence [ -1 -1 ] is changed to the original byte
3291 /* Decode a composition rule C1 and maybe one more byte from the
3292 source, and set RULE to the encoded composition rule. If the rule
3293 is invalid, goto invalid_code. */
3295 #define DECODE_COMPOSITION_RULE(rule) \
3299 goto invalid_code; \
3300 if (rule < 81) /* old format (before ver.21) */ \
3302 int gref = (rule) / 9; \
3303 int nref = (rule) % 9; \
3304 if (gref == 4) gref = 10; \
3305 if (nref == 4) nref = 10; \
3306 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3308 else /* new format (after ver.21) */ \
3312 ONE_MORE_BYTE (b); \
3313 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3314 goto invalid_code; \
3315 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3316 rule += 0x100; /* Distinguish it from the old format. */ \
3320 #define ENCODE_COMPOSITION_RULE(rule) \
3322 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3324 if (rule < 0x100) /* old format */ \
3326 if (gref == 10) gref = 4; \
3327 if (nref == 10) nref = 4; \
3328 charbuf[idx] = 32 + gref * 9 + nref; \
3329 charbuf[idx + 1] = -1; \
3332 else /* new format */ \
3334 charbuf[idx] = 32 + 81 + gref; \
3335 charbuf[idx + 1] = 32 + nref; \
3340 /* Finish the current composition as invalid. */
3343 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3345 int idx
= - cmp_status
->length
;
3348 /* Recover the original ESC sequence */
3349 charbuf
[idx
++] = ISO_CODE_ESC
;
3350 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3351 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3352 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3353 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3355 charbuf
[idx
++] = -2;
3357 charbuf
[idx
++] = -1;
3358 new_chars
= cmp_status
->nchars
;
3359 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3360 for (; idx
< 0; idx
++)
3362 int elt
= charbuf
[idx
];
3366 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3371 charbuf
[idx
++] = ISO_CODE_ESC
;
3376 cmp_status
->state
= COMPOSING_NO
;
3380 /* If characters are under composition, finish the composition. */
3381 #define MAYBE_FINISH_COMPOSITION() \
3383 if (cmp_status->state != COMPOSING_NO) \
3384 char_offset += finish_composition (charbuf, cmp_status); \
3387 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3389 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3390 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3391 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3392 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3394 Produce this annotation sequence now:
3396 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3399 #define DECODE_COMPOSITION_START(c1) \
3402 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3403 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3404 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3405 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3409 cmp_status->state = COMPOSING_CHAR; \
3410 cmp_status->length += 2; \
3414 MAYBE_FINISH_COMPOSITION (); \
3415 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3416 : c1 == '2' ? COMPOSITION_WITH_RULE \
3417 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3418 : COMPOSITION_WITH_RULE_ALTCHARS); \
3420 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3421 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3422 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3423 cmp_status->nchars = cmp_status->ncomps = 0; \
3424 coding->annotated = 1; \
3429 /* Handle composition end sequence ESC 1. */
3431 #define DECODE_COMPOSITION_END() \
3433 if (cmp_status->nchars == 0 \
3434 || ((cmp_status->state == COMPOSING_CHAR) \
3435 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3437 MAYBE_FINISH_COMPOSITION (); \
3438 goto invalid_code; \
3440 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3441 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3442 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3443 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3444 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3445 char_offset += cmp_status->nchars; \
3446 cmp_status->state = COMPOSING_NO; \
3449 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3451 #define STORE_COMPOSITION_RULE(rule) \
3454 *charbuf++ = rule; \
3455 cmp_status->length += 2; \
3456 cmp_status->state--; \
3459 /* Store a composed char or a component char C in charbuf, and update
3462 #define STORE_COMPOSITION_CHAR(c) \
3465 cmp_status->length++; \
3466 if (cmp_status->state == COMPOSING_CHAR) \
3467 cmp_status->nchars++; \
3469 cmp_status->ncomps++; \
3470 if (cmp_status->method == COMPOSITION_WITH_RULE \
3471 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3472 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3473 cmp_status->state++; \
3477 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3480 decode_coding_iso_2022 (struct coding_system
*coding
)
3482 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3483 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3484 const unsigned char *src_base
;
3485 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3486 /* We may produce two annotations (charset and composition) in one
3487 loop and one more charset annotation at the end. */
3489 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3490 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
3491 bool multibytep
= coding
->src_multibyte
;
3492 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3493 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3494 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3495 int charset_id_2
, charset_id_3
;
3496 struct charset
*charset
;
3498 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3499 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3500 ptrdiff_t char_offset
= coding
->produced_char
;
3501 ptrdiff_t last_offset
= char_offset
;
3502 int last_id
= charset_ascii
;
3504 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3505 int byte_after_cr
= -1;
3508 setup_iso_safe_charsets (attrs
);
3509 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3511 if (cmp_status
->state
!= COMPOSING_NO
)
3513 if (charbuf_end
- charbuf
< cmp_status
->length
)
3515 for (i
= 0; i
< cmp_status
->length
; i
++)
3516 *charbuf
++ = cmp_status
->carryover
[i
];
3517 coding
->annotated
= 1;
3525 consumed_chars_base
= consumed_chars
;
3527 if (charbuf
>= charbuf_end
)
3529 if (byte_after_cr
>= 0)
3534 if (byte_after_cr
>= 0)
3535 c1
= byte_after_cr
, byte_after_cr
= -1;
3541 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3543 *charbuf
++ = ASCII_CHAR_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3545 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3549 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3551 if (c1
== ISO_CODE_ESC
)
3553 if (src
+ 1 >= src_end
)
3554 goto no_more_source
;
3555 *charbuf
++ = ISO_CODE_ESC
;
3557 if (src
[0] == '%' && src
[1] == '@')
3560 consumed_chars
+= 2;
3562 /* We are sure charbuf can contain two more chars. */
3565 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3570 *charbuf
++ = ASCII_CHAR_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3576 if ((cmp_status
->state
== COMPOSING_RULE
3577 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3578 && c1
!= ISO_CODE_ESC
)
3582 DECODE_COMPOSITION_RULE (rule
);
3583 STORE_COMPOSITION_RULE (rule
);
3587 /* We produce at most one character. */
3588 switch (iso_code_class
[c1
])
3590 case ISO_0x20_or_0x7F
:
3591 if (charset_id_0
< 0
3592 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3593 /* This is SPACE or DEL. */
3594 charset
= CHARSET_FROM_ID (charset_ascii
);
3596 charset
= CHARSET_FROM_ID (charset_id_0
);
3599 case ISO_graphic_plane_0
:
3600 if (charset_id_0
< 0)
3601 charset
= CHARSET_FROM_ID (charset_ascii
);
3603 charset
= CHARSET_FROM_ID (charset_id_0
);
3606 case ISO_0xA0_or_0xFF
:
3607 if (charset_id_1
< 0
3608 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3609 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3611 /* This is a graphic character, we fall down ... */
3613 case ISO_graphic_plane_1
:
3614 if (charset_id_1
< 0)
3616 charset
= CHARSET_FROM_ID (charset_id_1
);
3620 if (eol_dos
&& c1
== '\r')
3621 ONE_MORE_BYTE (byte_after_cr
);
3622 MAYBE_FINISH_COMPOSITION ();
3623 charset
= CHARSET_FROM_ID (charset_ascii
);
3630 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3631 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3633 CODING_ISO_INVOCATION (coding
, 0) = 1;
3634 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3638 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3640 CODING_ISO_INVOCATION (coding
, 0) = 0;
3641 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3644 case ISO_single_shift_2_7
:
3645 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3647 case ISO_single_shift_2
:
3648 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3650 /* SS2 is handled as an escape sequence of ESC 'N' */
3652 goto label_escape_sequence
;
3654 case ISO_single_shift_3
:
3655 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3657 /* SS2 is handled as an escape sequence of ESC 'O' */
3659 goto label_escape_sequence
;
3661 case ISO_control_sequence_introducer
:
3662 /* CSI is handled as an escape sequence of ESC '[' ... */
3664 goto label_escape_sequence
;
3668 label_escape_sequence
:
3669 /* Escape sequences handled here are invocation,
3670 designation, direction specification, and character
3671 composition specification. */
3674 case '&': /* revision of following character set */
3676 if (!(c1
>= '@' && c1
<= '~'))
3679 if (c1
!= ISO_CODE_ESC
)
3682 goto label_escape_sequence
;
3684 case '$': /* designation of 2-byte character set */
3685 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3691 if (c1
>= '@' && c1
<= 'B')
3692 { /* designation of JISX0208.1978, GB2312.1980,
3694 reg
= 0, chars96
= 0;
3696 else if (c1
>= 0x28 && c1
<= 0x2B)
3697 { /* designation of DIMENSION2_CHARS94 character set */
3698 reg
= c1
- 0x28, chars96
= 0;
3701 else if (c1
>= 0x2C && c1
<= 0x2F)
3702 { /* designation of DIMENSION2_CHARS96 character set */
3703 reg
= c1
- 0x2C, chars96
= 1;
3708 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3709 /* We must update these variables now. */
3711 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3713 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3719 case 'n': /* invocation of locking-shift-2 */
3720 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3721 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3723 CODING_ISO_INVOCATION (coding
, 0) = 2;
3724 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3727 case 'o': /* invocation of locking-shift-3 */
3728 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3729 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3731 CODING_ISO_INVOCATION (coding
, 0) = 3;
3732 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3735 case 'N': /* invocation of single-shift-2 */
3736 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3737 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3739 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3740 if (charset_id_2
< 0)
3741 charset
= CHARSET_FROM_ID (charset_ascii
);
3743 charset
= CHARSET_FROM_ID (charset_id_2
);
3745 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0)
3746 || (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3747 && ((CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LEVEL_4
)
3748 ? c1
>= 0x80 : c1
< 0x80)))
3752 case 'O': /* invocation of single-shift-3 */
3753 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3754 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3756 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3757 if (charset_id_3
< 0)
3758 charset
= CHARSET_FROM_ID (charset_ascii
);
3760 charset
= CHARSET_FROM_ID (charset_id_3
);
3762 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0)
3763 || (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3764 && ((CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LEVEL_4
)
3765 ? c1
>= 0x80 : c1
< 0x80)))
3769 case '0': case '2': case '3': case '4': /* start composition */
3770 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3772 if (last_id
!= charset_ascii
)
3774 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3775 last_id
= charset_ascii
;
3776 last_offset
= char_offset
;
3778 DECODE_COMPOSITION_START (c1
);
3781 case '1': /* end composition */
3782 if (cmp_status
->state
== COMPOSING_NO
)
3784 DECODE_COMPOSITION_END ();
3787 case '[': /* specification of direction */
3788 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3790 /* For the moment, nested direction is not supported.
3791 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3792 left-to-right, and nonzero means right-to-left. */
3796 case ']': /* end of the current direction */
3797 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3799 case '0': /* end of the current direction */
3800 case '1': /* start of left-to-right direction */
3803 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3808 case '2': /* start of right-to-left direction */
3811 coding
->mode
|= CODING_MODE_DIRECTION
;
3825 /* CTEXT extended segment:
3826 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3827 We keep these bytes as is for the moment.
3828 They may be decoded by post-read-conversion. */
3832 ONE_MORE_BYTE (dim
);
3833 if (dim
< '0' || dim
> '4')
3841 size
= ((M
- 128) * 128) + (L
- 128);
3842 if (charbuf
+ 6 > charbuf_end
)
3844 *charbuf
++ = ISO_CODE_ESC
;
3848 *charbuf
++ = BYTE8_TO_CHAR (M
);
3849 *charbuf
++ = BYTE8_TO_CHAR (L
);
3850 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3854 /* XFree86 extension for embedding UTF-8 in CTEXT:
3855 ESC % G --UTF-8-BYTES-- ESC % @
3856 We keep these bytes as is for the moment.
3857 They may be decoded by post-read-conversion. */
3858 if (charbuf
+ 3 > charbuf_end
)
3860 *charbuf
++ = ISO_CODE_ESC
;
3863 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3871 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3876 if (c1
>= 0x28 && c1
<= 0x2B)
3877 { /* designation of DIMENSION1_CHARS94 character set */
3878 reg
= c1
- 0x28, chars96
= 0;
3881 else if (c1
>= 0x2C && c1
<= 0x2F)
3882 { /* designation of DIMENSION1_CHARS96 character set */
3883 reg
= c1
- 0x2C, chars96
= 1;
3888 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3889 /* We must update these variables now. */
3891 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3893 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3905 if (cmp_status
->state
== COMPOSING_NO
3906 && charset
->id
!= charset_ascii
3907 && last_id
!= charset
->id
)
3909 if (last_id
!= charset_ascii
)
3910 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3911 last_id
= charset
->id
;
3912 last_offset
= char_offset
;
3915 /* Now we know CHARSET and 1st position code C1 of a character.
3916 Produce a decoded character while getting 2nd and 3rd
3917 position codes C2, C3 if necessary. */
3918 if (CHARSET_DIMENSION (charset
) > 1)
3921 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3922 || ((c1
& 0x80) != (c2
& 0x80)))
3923 /* C2 is not in a valid range. */
3925 if (CHARSET_DIMENSION (charset
) == 2)
3926 c1
= (c1
<< 8) | c2
;
3930 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3931 || ((c1
& 0x80) != (c3
& 0x80)))
3932 /* C3 is not in a valid range. */
3934 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3938 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3941 MAYBE_FINISH_COMPOSITION ();
3942 for (; src_base
< src
; src_base
++, char_offset
++)
3944 if (ASCII_CHAR_P (*src_base
))
3945 *charbuf
++ = *src_base
;
3947 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3950 else if (cmp_status
->state
== COMPOSING_NO
)
3955 else if ((cmp_status
->state
== COMPOSING_CHAR
3956 ? cmp_status
->nchars
3957 : cmp_status
->ncomps
)
3958 >= MAX_COMPOSITION_COMPONENTS
)
3960 /* Too long composition. */
3961 MAYBE_FINISH_COMPOSITION ();
3966 STORE_COMPOSITION_CHAR (c
);
3970 MAYBE_FINISH_COMPOSITION ();
3972 consumed_chars
= consumed_chars_base
;
3974 *charbuf
++ = c
< 0 ? -c
: ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3976 /* Reset the invocation and designation status to the safest
3977 one; i.e. designate ASCII to the graphic register 0, and
3978 invoke that register to the graphic plane 0. This typically
3979 helps the case that an designation sequence for ASCII "ESC (
3980 B" is somehow broken (e.g. broken by a newline). */
3981 CODING_ISO_INVOCATION (coding
, 0) = 0;
3982 CODING_ISO_DESIGNATION (coding
, 0) = charset_ascii
;
3983 charset_id_0
= charset_ascii
;
3991 if (cmp_status
->state
!= COMPOSING_NO
)
3993 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3994 MAYBE_FINISH_COMPOSITION ();
3997 charbuf
-= cmp_status
->length
;
3998 for (i
= 0; i
< cmp_status
->length
; i
++)
3999 cmp_status
->carryover
[i
] = charbuf
[i
];
4002 else if (last_id
!= charset_ascii
)
4003 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4004 coding
->consumed_char
+= consumed_chars_base
;
4005 coding
->consumed
= src_base
- coding
->source
;
4006 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4010 /* ISO2022 encoding stuff. */
4013 It is not enough to say just "ISO2022" on encoding, we have to
4014 specify more details. In Emacs, each coding system of ISO2022
4015 variant has the following specifications:
4016 1. Initial designation to G0 thru G3.
4017 2. Allows short-form designation?
4018 3. ASCII should be designated to G0 before control characters?
4019 4. ASCII should be designated to G0 at end of line?
4020 5. 7-bit environment or 8-bit environment?
4021 6. Use locking-shift?
4022 7. Use Single-shift?
4023 And the following two are only for Japanese:
4024 8. Use ASCII in place of JIS0201-1976-Roman?
4025 9. Use JISX0208-1983 in place of JISX0208-1978?
4026 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4027 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4031 /* Produce codes (escape sequence) for designating CHARSET to graphic
4032 register REG at DST, and increment DST. If <final-char> of CHARSET is
4033 '@', 'A', or 'B' and the coding system CODING allows, produce
4034 designation sequence of short-form. */
4036 #define ENCODE_DESIGNATION(charset, reg, coding) \
4038 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4039 const char *intermediate_char_94 = "()*+"; \
4040 const char *intermediate_char_96 = ",-./"; \
4041 int revision = -1; \
4043 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4044 revision = CHARSET_ISO_REVISION (charset); \
4046 if (revision >= 0) \
4048 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4049 EMIT_ONE_BYTE ('@' + revision); \
4051 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4052 if (CHARSET_DIMENSION (charset) == 1) \
4055 if (! CHARSET_ISO_CHARS_96 (charset)) \
4056 b = intermediate_char_94[reg]; \
4058 b = intermediate_char_96[reg]; \
4059 EMIT_ONE_ASCII_BYTE (b); \
4063 EMIT_ONE_ASCII_BYTE ('$'); \
4064 if (! CHARSET_ISO_CHARS_96 (charset)) \
4066 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4068 || final_char < '@' || final_char > 'B') \
4069 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4072 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4074 EMIT_ONE_ASCII_BYTE (final_char); \
4076 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4080 /* The following two macros produce codes (control character or escape
4081 sequence) for ISO2022 single-shift functions (single-shift-2 and
4084 #define ENCODE_SINGLE_SHIFT_2 \
4086 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4087 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4089 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4090 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4094 #define ENCODE_SINGLE_SHIFT_3 \
4096 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4097 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4099 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4100 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4104 /* The following four macros produce codes (control character or
4105 escape sequence) for ISO2022 locking-shift functions (shift-in,
4106 shift-out, locking-shift-2, and locking-shift-3). */
4108 #define ENCODE_SHIFT_IN \
4110 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4111 CODING_ISO_INVOCATION (coding, 0) = 0; \
4115 #define ENCODE_SHIFT_OUT \
4117 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4118 CODING_ISO_INVOCATION (coding, 0) = 1; \
4122 #define ENCODE_LOCKING_SHIFT_2 \
4124 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4125 CODING_ISO_INVOCATION (coding, 0) = 2; \
4129 #define ENCODE_LOCKING_SHIFT_3 \
4131 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4132 CODING_ISO_INVOCATION (coding, 0) = 3; \
4136 /* Produce codes for a DIMENSION1 character whose character set is
4137 CHARSET and whose position-code is C1. Designation and invocation
4138 sequences are also produced in advance if necessary. */
4140 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4142 int id = CHARSET_ID (charset); \
4144 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4145 && id == charset_ascii) \
4147 id = charset_jisx0201_roman; \
4148 charset = CHARSET_FROM_ID (id); \
4151 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4153 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4154 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4156 EMIT_ONE_BYTE (c1 | 0x80); \
4157 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4160 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4162 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4165 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4167 EMIT_ONE_BYTE (c1 | 0x80); \
4171 /* Since CHARSET is not yet invoked to any graphic planes, we \
4172 must invoke it, or, at first, designate it to some graphic \
4173 register. Then repeat the loop to actually produce the \
4175 dst = encode_invocation_designation (charset, coding, dst, \
4180 /* Produce codes for a DIMENSION2 character whose character set is
4181 CHARSET and whose position-codes are C1 and C2. Designation and
4182 invocation codes are also produced in advance if necessary. */
4184 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4186 int id = CHARSET_ID (charset); \
4188 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4189 && id == charset_jisx0208) \
4191 id = charset_jisx0208_1978; \
4192 charset = CHARSET_FROM_ID (id); \
4195 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4197 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4198 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4200 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4201 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4204 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4206 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4209 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4211 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4215 /* Since CHARSET is not yet invoked to any graphic planes, we \
4216 must invoke it, or, at first, designate it to some graphic \
4217 register. Then repeat the loop to actually produce the \
4219 dst = encode_invocation_designation (charset, coding, dst, \
4224 #define ENCODE_ISO_CHARACTER(charset, c) \
4227 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4229 if (CHARSET_DIMENSION (charset) == 1) \
4230 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4232 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4236 /* Produce designation and invocation codes at a place pointed by DST
4237 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4240 static unsigned char *
4241 encode_invocation_designation (struct charset
*charset
,
4242 struct coding_system
*coding
,
4243 unsigned char *dst
, ptrdiff_t *p_nchars
)
4245 bool multibytep
= coding
->dst_multibyte
;
4246 ptrdiff_t produced_chars
= *p_nchars
;
4247 int reg
; /* graphic register number */
4248 int id
= CHARSET_ID (charset
);
4250 /* At first, check designations. */
4251 for (reg
= 0; reg
< 4; reg
++)
4252 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4257 /* CHARSET is not yet designated to any graphic registers. */
4258 /* At first check the requested designation. */
4259 reg
= CODING_ISO_REQUEST (coding
, id
);
4261 /* Since CHARSET requests no special designation, designate it
4262 to graphic register 0. */
4265 ENCODE_DESIGNATION (charset
, reg
, coding
);
4268 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4269 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4271 /* Since the graphic register REG is not invoked to any graphic
4272 planes, invoke it to graphic plane 0. */
4275 case 0: /* graphic register 0 */
4279 case 1: /* graphic register 1 */
4283 case 2: /* graphic register 2 */
4284 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4285 ENCODE_SINGLE_SHIFT_2
;
4287 ENCODE_LOCKING_SHIFT_2
;
4290 case 3: /* graphic register 3 */
4291 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4292 ENCODE_SINGLE_SHIFT_3
;
4294 ENCODE_LOCKING_SHIFT_3
;
4302 *p_nchars
= produced_chars
;
4307 /* Produce codes for designation and invocation to reset the graphic
4308 planes and registers to initial state. */
4309 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4312 struct charset *charset; \
4314 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4316 for (reg = 0; reg < 4; reg++) \
4317 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4318 && (CODING_ISO_DESIGNATION (coding, reg) \
4319 != CODING_ISO_INITIAL (coding, reg))) \
4321 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4322 ENCODE_DESIGNATION (charset, reg, coding); \
4327 /* Produce designation sequences of charsets in the line started from
4328 CHARBUF to a place pointed by DST, and return the number of
4329 produced bytes. DST should not directly point a buffer text area
4330 which may be relocated by char_charset call.
4332 If the current block ends before any end-of-line, we may fail to
4333 find all the necessary designations. */
4336 encode_designation_at_bol (struct coding_system
*coding
,
4337 int *charbuf
, int *charbuf_end
,
4340 unsigned char *orig
= dst
;
4341 struct charset
*charset
;
4342 /* Table of charsets to be designated to each graphic register. */
4344 int c
, found
= 0, reg
;
4345 ptrdiff_t produced_chars
= 0;
4346 bool multibytep
= coding
->dst_multibyte
;
4348 Lisp_Object charset_list
;
4350 attrs
= CODING_ID_ATTRS (coding
->id
);
4351 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4352 if (EQ (charset_list
, Qiso_2022
))
4353 charset_list
= Viso_2022_charset_list
;
4355 for (reg
= 0; reg
< 4; reg
++)
4358 while (charbuf
< charbuf_end
&& found
< 4)
4365 charset
= char_charset (c
, charset_list
, NULL
);
4366 id
= CHARSET_ID (charset
);
4367 reg
= CODING_ISO_REQUEST (coding
, id
);
4368 if (reg
>= 0 && r
[reg
] < 0)
4377 for (reg
= 0; reg
< 4; reg
++)
4379 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4380 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4386 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4389 encode_coding_iso_2022 (struct coding_system
*coding
)
4391 bool multibytep
= coding
->dst_multibyte
;
4392 int *charbuf
= coding
->charbuf
;
4393 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4394 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4395 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4397 bool bol_designation
4398 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4399 && CODING_ISO_BOL (coding
));
4400 ptrdiff_t produced_chars
= 0;
4401 Lisp_Object attrs
, eol_type
, charset_list
;
4402 bool ascii_compatible
;
4404 int preferred_charset_id
= -1;
4406 CODING_GET_INFO (coding
, attrs
, charset_list
);
4407 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4408 if (VECTORP (eol_type
))
4411 setup_iso_safe_charsets (attrs
);
4412 /* Charset list may have been changed. */
4413 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4414 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4417 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4418 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4419 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4421 while (charbuf
< charbuf_end
)
4423 ASSURE_DESTINATION (safe_room
);
4425 if (bol_designation
)
4427 /* We have to produce designation sequences if any now. */
4428 unsigned char desig_buf
[16];
4432 charset_map_loaded
= 0;
4433 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4435 if (charset_map_loaded
4436 && (offset
= coding_change_destination (coding
)))
4441 memcpy (dst
, desig_buf
, nbytes
);
4443 /* We are sure that designation sequences are all ASCII bytes. */
4444 produced_chars
+= nbytes
;
4445 bol_designation
= 0;
4446 ASSURE_DESTINATION (safe_room
);
4453 /* Handle an annotation. */
4456 case CODING_ANNOTATE_COMPOSITION_MASK
:
4457 /* Not yet implemented. */
4459 case CODING_ANNOTATE_CHARSET_MASK
:
4460 preferred_charset_id
= charbuf
[2];
4461 if (preferred_charset_id
>= 0
4462 && NILP (Fmemq (make_number (preferred_charset_id
),
4464 preferred_charset_id
= -1;
4473 /* Now encode the character C. */
4474 if (c
< 0x20 || c
== 0x7F)
4477 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4479 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4480 ENCODE_RESET_PLANE_AND_REGISTER ();
4481 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4485 for (i
= 0; i
< 4; i
++)
4486 CODING_ISO_DESIGNATION (coding
, i
)
4487 = CODING_ISO_INITIAL (coding
, i
);
4489 bol_designation
= ((CODING_ISO_FLAGS (coding
)
4490 & CODING_ISO_FLAG_DESIGNATE_AT_BOL
)
4493 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4494 ENCODE_RESET_PLANE_AND_REGISTER ();
4495 EMIT_ONE_ASCII_BYTE (c
);
4497 else if (ASCII_CHAR_P (c
))
4499 if (ascii_compatible
)
4500 EMIT_ONE_ASCII_BYTE (c
);
4503 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4504 ENCODE_ISO_CHARACTER (charset
, c
);
4507 else if (CHAR_BYTE8_P (c
))
4509 c
= CHAR_TO_BYTE8 (c
);
4514 struct charset
*charset
;
4516 if (preferred_charset_id
>= 0)
4520 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4521 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4523 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4527 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4531 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4533 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4534 charset
= CHARSET_FROM_ID (charset_ascii
);
4538 c
= coding
->default_char
;
4539 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4540 charset_list
, NULL
, charset
);
4543 ENCODE_ISO_CHARACTER (charset
, c
);
4547 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4548 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4550 ASSURE_DESTINATION (safe_room
);
4551 ENCODE_RESET_PLANE_AND_REGISTER ();
4553 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4554 CODING_ISO_BOL (coding
) = bol_designation
;
4555 coding
->produced_char
+= produced_chars
;
4556 coding
->produced
= dst
- coding
->destination
;
4561 /*** 8,9. SJIS and BIG5 handlers ***/
4563 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4564 quite widely. So, for the moment, Emacs supports them in the bare
4565 C code. But, in the future, they may be supported only by CCL. */
4567 /* SJIS is a coding system encoding three character sets: ASCII, right
4568 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4569 as is. A character of charset katakana-jisx0201 is encoded by
4570 "position-code + 0x80". A character of charset japanese-jisx0208
4571 is encoded in 2-byte but two position-codes are divided and shifted
4572 so that it fit in the range below.
4574 --- CODE RANGE of SJIS ---
4575 (character set) (range)
4577 KATAKANA-JISX0201 0xA0 .. 0xDF
4578 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4579 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4580 -------------------------------
4584 /* BIG5 is a coding system encoding two character sets: ASCII and
4585 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4586 character set and is encoded in two-byte.
4588 --- CODE RANGE of BIG5 ---
4589 (character set) (range)
4591 Big5 (1st byte) 0xA1 .. 0xFE
4592 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4593 --------------------------
4597 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4598 Return true if a text is encoded in SJIS. */
4601 detect_coding_sjis (struct coding_system
*coding
,
4602 struct coding_detection_info
*detect_info
)
4604 const unsigned char *src
= coding
->source
, *src_base
;
4605 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4606 bool multibytep
= coding
->src_multibyte
;
4607 ptrdiff_t consumed_chars
= 0;
4610 Lisp_Object attrs
, charset_list
;
4611 int max_first_byte_of_2_byte_code
;
4613 CODING_GET_INFO (coding
, attrs
, charset_list
);
4614 max_first_byte_of_2_byte_code
4615 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4617 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4618 /* A coding system of this category is always ASCII compatible. */
4619 src
+= coding
->head_ascii
;
4627 if ((c
>= 0x81 && c
<= 0x9F)
4628 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4631 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4633 found
= CATEGORY_MASK_SJIS
;
4635 else if (c
>= 0xA0 && c
< 0xE0)
4636 found
= CATEGORY_MASK_SJIS
;
4640 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4644 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4646 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4649 detect_info
->found
|= found
;
4653 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4654 Return true if a text is encoded in BIG5. */
4657 detect_coding_big5 (struct coding_system
*coding
,
4658 struct coding_detection_info
*detect_info
)
4660 const unsigned char *src
= coding
->source
, *src_base
;
4661 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4662 bool multibytep
= coding
->src_multibyte
;
4663 ptrdiff_t consumed_chars
= 0;
4667 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4668 /* A coding system of this category is always ASCII compatible. */
4669 src
+= coding
->head_ascii
;
4680 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4682 found
= CATEGORY_MASK_BIG5
;
4687 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4691 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4693 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4696 detect_info
->found
|= found
;
4700 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4703 decode_coding_sjis (struct coding_system
*coding
)
4705 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4706 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4707 const unsigned char *src_base
;
4708 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4709 /* We may produce one charset annotation in one loop and one more at
4712 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4713 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4714 bool multibytep
= coding
->src_multibyte
;
4715 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4716 struct charset
*charset_kanji2
;
4717 Lisp_Object attrs
, charset_list
, val
;
4718 ptrdiff_t char_offset
= coding
->produced_char
;
4719 ptrdiff_t last_offset
= char_offset
;
4720 int last_id
= charset_ascii
;
4722 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4723 int byte_after_cr
= -1;
4725 CODING_GET_INFO (coding
, attrs
, charset_list
);
4728 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4729 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4730 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4731 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4736 struct charset
*charset
;
4739 consumed_chars_base
= consumed_chars
;
4741 if (charbuf
>= charbuf_end
)
4743 if (byte_after_cr
>= 0)
4748 if (byte_after_cr
>= 0)
4749 c
= byte_after_cr
, byte_after_cr
= -1;
4756 if (eol_dos
&& c
== '\r')
4757 ONE_MORE_BYTE (byte_after_cr
);
4758 charset
= charset_roman
;
4760 else if (c
== 0x80 || c
== 0xA0)
4762 else if (c
>= 0xA1 && c
<= 0xDF)
4764 /* SJIS -> JISX0201-Kana */
4766 charset
= charset_kana
;
4770 /* SJIS -> JISX0208 */
4772 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4776 charset
= charset_kanji
;
4778 else if (c
<= 0xFC && charset_kanji2
)
4780 /* SJIS -> JISX0213-2 */
4782 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4786 charset
= charset_kanji2
;
4790 if (charset
->id
!= charset_ascii
4791 && last_id
!= charset
->id
)
4793 if (last_id
!= charset_ascii
)
4794 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4795 last_id
= charset
->id
;
4796 last_offset
= char_offset
;
4798 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4805 consumed_chars
= consumed_chars_base
;
4807 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4812 if (last_id
!= charset_ascii
)
4813 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4814 coding
->consumed_char
+= consumed_chars_base
;
4815 coding
->consumed
= src_base
- coding
->source
;
4816 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4820 decode_coding_big5 (struct coding_system
*coding
)
4822 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4823 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4824 const unsigned char *src_base
;
4825 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4826 /* We may produce one charset annotation in one loop and one more at
4829 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4830 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4831 bool multibytep
= coding
->src_multibyte
;
4832 struct charset
*charset_roman
, *charset_big5
;
4833 Lisp_Object attrs
, charset_list
, val
;
4834 ptrdiff_t char_offset
= coding
->produced_char
;
4835 ptrdiff_t last_offset
= char_offset
;
4836 int last_id
= charset_ascii
;
4838 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4839 int byte_after_cr
= -1;
4841 CODING_GET_INFO (coding
, attrs
, charset_list
);
4843 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4844 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4849 struct charset
*charset
;
4852 consumed_chars_base
= consumed_chars
;
4854 if (charbuf
>= charbuf_end
)
4856 if (byte_after_cr
>= 0)
4861 if (byte_after_cr
>= 0)
4862 c
= byte_after_cr
, byte_after_cr
= -1;
4870 if (eol_dos
&& c
== '\r')
4871 ONE_MORE_BYTE (byte_after_cr
);
4872 charset
= charset_roman
;
4877 if (c
< 0xA1 || c
> 0xFE)
4880 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4883 charset
= charset_big5
;
4885 if (charset
->id
!= charset_ascii
4886 && last_id
!= charset
->id
)
4888 if (last_id
!= charset_ascii
)
4889 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4890 last_id
= charset
->id
;
4891 last_offset
= char_offset
;
4893 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4900 consumed_chars
= consumed_chars_base
;
4902 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4907 if (last_id
!= charset_ascii
)
4908 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4909 coding
->consumed_char
+= consumed_chars_base
;
4910 coding
->consumed
= src_base
- coding
->source
;
4911 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4914 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4915 This function can encode charsets `ascii', `katakana-jisx0201',
4916 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4917 are sure that all these charsets are registered as official charset
4918 (i.e. do not have extended leading-codes). Characters of other
4919 charsets are produced without any encoding. */
4922 encode_coding_sjis (struct coding_system
*coding
)
4924 bool multibytep
= coding
->dst_multibyte
;
4925 int *charbuf
= coding
->charbuf
;
4926 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4927 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4928 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4930 ptrdiff_t produced_chars
= 0;
4931 Lisp_Object attrs
, charset_list
, val
;
4932 bool ascii_compatible
;
4933 struct charset
*charset_kanji
, *charset_kana
;
4934 struct charset
*charset_kanji2
;
4937 CODING_GET_INFO (coding
, attrs
, charset_list
);
4938 val
= XCDR (charset_list
);
4939 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4940 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4941 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4943 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4945 while (charbuf
< charbuf_end
)
4947 ASSURE_DESTINATION (safe_room
);
4949 /* Now encode the character C. */
4950 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4951 EMIT_ONE_ASCII_BYTE (c
);
4952 else if (CHAR_BYTE8_P (c
))
4954 c
= CHAR_TO_BYTE8 (c
);
4960 struct charset
*charset
;
4961 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4966 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4968 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4969 charset
= CHARSET_FROM_ID (charset_ascii
);
4973 c
= coding
->default_char
;
4974 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4975 charset_list
, &code
, charset
);
4978 if (code
== CHARSET_INVALID_CODE (charset
))
4980 if (charset
== charset_kanji
)
4984 c1
= code
>> 8, c2
= code
& 0xFF;
4985 EMIT_TWO_BYTES (c1
, c2
);
4987 else if (charset
== charset_kana
)
4988 EMIT_ONE_BYTE (code
| 0x80);
4989 else if (charset_kanji2
&& charset
== charset_kanji2
)
4994 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4996 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4998 JIS_TO_SJIS2 (code
);
4999 c1
= code
>> 8, c2
= code
& 0xFF;
5000 EMIT_TWO_BYTES (c1
, c2
);
5003 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5006 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5009 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5010 coding
->produced_char
+= produced_chars
;
5011 coding
->produced
= dst
- coding
->destination
;
5016 encode_coding_big5 (struct coding_system
*coding
)
5018 bool multibytep
= coding
->dst_multibyte
;
5019 int *charbuf
= coding
->charbuf
;
5020 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5021 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5022 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5024 ptrdiff_t produced_chars
= 0;
5025 Lisp_Object attrs
, charset_list
, val
;
5026 bool ascii_compatible
;
5027 struct charset
*charset_big5
;
5030 CODING_GET_INFO (coding
, attrs
, charset_list
);
5031 val
= XCDR (charset_list
);
5032 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5033 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5035 while (charbuf
< charbuf_end
)
5037 ASSURE_DESTINATION (safe_room
);
5039 /* Now encode the character C. */
5040 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5041 EMIT_ONE_ASCII_BYTE (c
);
5042 else if (CHAR_BYTE8_P (c
))
5044 c
= CHAR_TO_BYTE8 (c
);
5050 struct charset
*charset
;
5051 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5056 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5058 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5059 charset
= CHARSET_FROM_ID (charset_ascii
);
5063 c
= coding
->default_char
;
5064 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
5065 charset_list
, &code
, charset
);
5068 if (code
== CHARSET_INVALID_CODE (charset
))
5070 if (charset
== charset_big5
)
5074 c1
= code
>> 8, c2
= code
& 0xFF;
5075 EMIT_TWO_BYTES (c1
, c2
);
5078 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5081 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5082 coding
->produced_char
+= produced_chars
;
5083 coding
->produced
= dst
- coding
->destination
;
5088 /*** 10. CCL handlers ***/
5090 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5091 Return true if a text is encoded in a coding system of which
5092 encoder/decoder are written in CCL program. */
5095 detect_coding_ccl (struct coding_system
*coding
,
5096 struct coding_detection_info
*detect_info
)
5098 const unsigned char *src
= coding
->source
, *src_base
;
5099 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5100 bool multibytep
= coding
->src_multibyte
;
5101 ptrdiff_t consumed_chars
= 0;
5103 unsigned char *valids
;
5104 ptrdiff_t head_ascii
= coding
->head_ascii
;
5107 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5109 coding
= &coding_categories
[coding_category_ccl
];
5110 valids
= CODING_CCL_VALIDS (coding
);
5111 attrs
= CODING_ID_ATTRS (coding
->id
);
5112 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5121 if (c
< 0 || ! valids
[c
])
5123 if ((valids
[c
] > 1))
5124 found
= CATEGORY_MASK_CCL
;
5126 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5130 detect_info
->found
|= found
;
5135 decode_coding_ccl (struct coding_system
*coding
)
5137 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5138 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5139 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5140 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5141 ptrdiff_t consumed_chars
= 0;
5142 bool multibytep
= coding
->src_multibyte
;
5143 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5144 int source_charbuf
[1024];
5145 int source_byteidx
[1025];
5146 Lisp_Object attrs
, charset_list
;
5148 CODING_GET_INFO (coding
, attrs
, charset_list
);
5152 const unsigned char *p
= src
;
5158 while (i
< 1024 && p
< src_end
)
5160 source_byteidx
[i
] = p
- src
;
5161 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5163 source_byteidx
[i
] = p
- src
;
5166 while (i
< 1024 && p
< src_end
)
5167 source_charbuf
[i
++] = *p
++;
5169 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5170 ccl
->last_block
= true;
5171 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5172 charset_map_loaded
= 0;
5173 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5175 if (charset_map_loaded
5176 && (offset
= coding_change_source (coding
)))
5182 charbuf
+= ccl
->produced
;
5184 src
+= source_byteidx
[ccl
->consumed
];
5186 src
+= ccl
->consumed
;
5187 consumed_chars
+= ccl
->consumed
;
5188 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5192 switch (ccl
->status
)
5194 case CCL_STAT_SUSPEND_BY_SRC
:
5195 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5197 case CCL_STAT_SUSPEND_BY_DST
:
5198 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5201 case CCL_STAT_INVALID_CMD
:
5202 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5205 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5208 coding
->consumed_char
+= consumed_chars
;
5209 coding
->consumed
= src
- coding
->source
;
5210 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5214 encode_coding_ccl (struct coding_system
*coding
)
5216 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5217 bool multibytep
= coding
->dst_multibyte
;
5218 int *charbuf
= coding
->charbuf
;
5219 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5220 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5221 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5222 int destination_charbuf
[1024];
5223 ptrdiff_t produced_chars
= 0;
5225 Lisp_Object attrs
, charset_list
;
5227 CODING_GET_INFO (coding
, attrs
, charset_list
);
5228 if (coding
->consumed_char
== coding
->src_chars
5229 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5230 ccl
->last_block
= true;
5236 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5237 charset_map_loaded
= 0;
5238 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5239 charbuf_end
- charbuf
, 1024, charset_list
);
5240 if (charset_map_loaded
5241 && (offset
= coding_change_destination (coding
)))
5245 ASSURE_DESTINATION (ccl
->produced
* 2);
5246 for (i
= 0; i
< ccl
->produced
; i
++)
5247 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5251 ASSURE_DESTINATION (ccl
->produced
);
5252 for (i
= 0; i
< ccl
->produced
; i
++)
5253 *dst
++ = destination_charbuf
[i
] & 0xFF;
5254 produced_chars
+= ccl
->produced
;
5256 charbuf
+= ccl
->consumed
;
5257 if (ccl
->status
== CCL_STAT_QUIT
5258 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5261 while (charbuf
< charbuf_end
);
5263 switch (ccl
->status
)
5265 case CCL_STAT_SUSPEND_BY_SRC
:
5266 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5268 case CCL_STAT_SUSPEND_BY_DST
:
5269 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5272 case CCL_STAT_INVALID_CMD
:
5273 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5276 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5280 coding
->produced_char
+= produced_chars
;
5281 coding
->produced
= dst
- coding
->destination
;
5286 /*** 10, 11. no-conversion handlers ***/
5288 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5291 decode_coding_raw_text (struct coding_system
*coding
)
5294 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5296 coding
->chars_at_source
= 1;
5297 coding
->consumed_char
= coding
->src_chars
;
5298 coding
->consumed
= coding
->src_bytes
;
5299 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5301 coding
->consumed_char
--;
5303 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5306 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5310 encode_coding_raw_text (struct coding_system
*coding
)
5312 bool multibytep
= coding
->dst_multibyte
;
5313 int *charbuf
= coding
->charbuf
;
5314 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5315 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5316 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5317 ptrdiff_t produced_chars
= 0;
5322 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5324 if (coding
->src_multibyte
)
5325 while (charbuf
< charbuf_end
)
5327 ASSURE_DESTINATION (safe_room
);
5329 if (ASCII_CHAR_P (c
))
5330 EMIT_ONE_ASCII_BYTE (c
);
5331 else if (CHAR_BYTE8_P (c
))
5333 c
= CHAR_TO_BYTE8 (c
);
5338 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5340 CHAR_STRING_ADVANCE (c
, p1
);
5343 EMIT_ONE_BYTE (*p0
);
5350 while (charbuf
< charbuf_end
)
5352 ASSURE_DESTINATION (safe_room
);
5359 if (coding
->src_multibyte
)
5361 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5363 while (charbuf
< charbuf_end
)
5365 ASSURE_DESTINATION (safe_room
);
5367 if (ASCII_CHAR_P (c
))
5369 else if (CHAR_BYTE8_P (c
))
5370 *dst
++ = CHAR_TO_BYTE8 (c
);
5372 CHAR_STRING_ADVANCE (c
, dst
);
5377 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5378 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5379 *dst
++ = *charbuf
++;
5381 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5383 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5384 coding
->produced_char
+= produced_chars
;
5385 coding
->produced
= dst
- coding
->destination
;
5389 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5390 Return true if a text is encoded in a charset-based coding system. */
5393 detect_coding_charset (struct coding_system
*coding
,
5394 struct coding_detection_info
*detect_info
)
5396 const unsigned char *src
= coding
->source
, *src_base
;
5397 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5398 bool multibytep
= coding
->src_multibyte
;
5399 ptrdiff_t consumed_chars
= 0;
5400 Lisp_Object attrs
, valids
, name
;
5402 ptrdiff_t head_ascii
= coding
->head_ascii
;
5403 bool check_latin_extra
= 0;
5405 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5407 coding
= &coding_categories
[coding_category_charset
];
5408 attrs
= CODING_ID_ATTRS (coding
->id
);
5409 valids
= AREF (attrs
, coding_attr_charset_valids
);
5410 name
= CODING_ID_NAME (coding
->id
);
5411 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5412 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5413 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5414 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5415 check_latin_extra
= 1;
5417 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5424 struct charset
*charset
;
5431 val
= AREF (valids
, c
);
5437 && check_latin_extra
5438 && (!VECTORP (Vlatin_extra_code_table
)
5439 || NILP (AREF (Vlatin_extra_code_table
, c
))))
5441 found
= CATEGORY_MASK_CHARSET
;
5445 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5446 dim
= CHARSET_DIMENSION (charset
);
5447 for (idx
= 1; idx
< dim
; idx
++)
5452 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5453 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5462 for (; CONSP (val
); val
= XCDR (val
))
5464 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5465 dim
= CHARSET_DIMENSION (charset
);
5471 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5472 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5487 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5491 detect_info
->found
|= found
;
5496 decode_coding_charset (struct coding_system
*coding
)
5498 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5499 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5500 const unsigned char *src_base
;
5501 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5502 /* We may produce one charset annotation in one loop and one more at
5505 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5506 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
5507 bool multibytep
= coding
->src_multibyte
;
5508 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5510 ptrdiff_t char_offset
= coding
->produced_char
;
5511 ptrdiff_t last_offset
= char_offset
;
5512 int last_id
= charset_ascii
;
5514 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5515 int byte_after_cr
= -1;
5517 valids
= AREF (attrs
, coding_attr_charset_valids
);
5523 struct charset
*charset
;
5529 consumed_chars_base
= consumed_chars
;
5531 if (charbuf
>= charbuf_end
)
5533 if (byte_after_cr
>= 0)
5538 if (byte_after_cr
>= 0)
5546 if (eol_dos
&& c
== '\r')
5547 ONE_MORE_BYTE (byte_after_cr
);
5553 val
= AREF (valids
, c
);
5554 if (! INTEGERP (val
) && ! CONSP (val
))
5558 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5559 dim
= CHARSET_DIMENSION (charset
);
5563 code
= (code
<< 8) | c
;
5566 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5571 /* VAL is a list of charset IDs. It is assured that the
5572 list is sorted by charset dimensions (smaller one
5576 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5577 dim
= CHARSET_DIMENSION (charset
);
5581 code
= (code
<< 8) | c
;
5584 CODING_DECODE_CHAR (coding
, src
, src_base
,
5585 src_end
, charset
, code
, c
);
5593 if (charset
->id
!= charset_ascii
5594 && last_id
!= charset
->id
)
5596 if (last_id
!= charset_ascii
)
5597 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5598 last_id
= charset
->id
;
5599 last_offset
= char_offset
;
5608 consumed_chars
= consumed_chars_base
;
5610 *charbuf
++ = c
< 0 ? -c
: ASCII_CHAR_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5615 if (last_id
!= charset_ascii
)
5616 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5617 coding
->consumed_char
+= consumed_chars_base
;
5618 coding
->consumed
= src_base
- coding
->source
;
5619 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5623 encode_coding_charset (struct coding_system
*coding
)
5625 bool multibytep
= coding
->dst_multibyte
;
5626 int *charbuf
= coding
->charbuf
;
5627 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5628 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5629 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5630 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5631 ptrdiff_t produced_chars
= 0;
5632 Lisp_Object attrs
, charset_list
;
5633 bool ascii_compatible
;
5636 CODING_GET_INFO (coding
, attrs
, charset_list
);
5637 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5639 while (charbuf
< charbuf_end
)
5641 struct charset
*charset
;
5644 ASSURE_DESTINATION (safe_room
);
5646 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5647 EMIT_ONE_ASCII_BYTE (c
);
5648 else if (CHAR_BYTE8_P (c
))
5650 c
= CHAR_TO_BYTE8 (c
);
5655 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5660 if (CHARSET_DIMENSION (charset
) == 1)
5661 EMIT_ONE_BYTE (code
);
5662 else if (CHARSET_DIMENSION (charset
) == 2)
5663 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5664 else if (CHARSET_DIMENSION (charset
) == 3)
5665 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5667 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5668 (code
>> 8) & 0xFF, code
& 0xFF);
5672 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5673 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5675 c
= coding
->default_char
;
5681 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5682 coding
->produced_char
+= produced_chars
;
5683 coding
->produced
= dst
- coding
->destination
;
5688 /*** 7. C library functions ***/
5690 /* Setup coding context CODING from information about CODING_SYSTEM.
5691 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5692 CODING_SYSTEM is invalid, signal an error. */
5695 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5698 Lisp_Object eol_type
;
5699 Lisp_Object coding_type
;
5702 if (NILP (coding_system
))
5703 coding_system
= Qundecided
;
5705 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5707 attrs
= CODING_ID_ATTRS (coding
->id
);
5708 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5711 if (VECTORP (eol_type
))
5712 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5713 | CODING_REQUIRE_DETECTION_MASK
);
5714 else if (! EQ (eol_type
, Qunix
))
5715 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5716 | CODING_REQUIRE_ENCODING_MASK
);
5718 coding
->common_flags
= 0;
5719 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5720 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5721 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5722 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5723 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5724 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5726 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5727 coding
->max_charset_id
= SCHARS (val
) - 1;
5728 coding
->safe_charsets
= SDATA (val
);
5729 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5730 coding
->carryover_bytes
= 0;
5731 coding
->raw_destination
= 0;
5733 coding_type
= CODING_ATTR_TYPE (attrs
);
5734 if (EQ (coding_type
, Qundecided
))
5736 coding
->detector
= NULL
;
5737 coding
->decoder
= decode_coding_raw_text
;
5738 coding
->encoder
= encode_coding_raw_text
;
5739 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5740 coding
->spec
.undecided
.inhibit_nbd
5741 = (encode_inhibit_flag
5742 (AREF (attrs
, coding_attr_undecided_inhibit_null_byte_detection
)));
5743 coding
->spec
.undecided
.inhibit_ied
5744 = (encode_inhibit_flag
5745 (AREF (attrs
, coding_attr_undecided_inhibit_iso_escape_detection
)));
5746 coding
->spec
.undecided
.prefer_utf_8
5747 = ! NILP (AREF (attrs
, coding_attr_undecided_prefer_utf_8
));
5749 else if (EQ (coding_type
, Qiso_2022
))
5752 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5754 /* Invoke graphic register 0 to plane 0. */
5755 CODING_ISO_INVOCATION (coding
, 0) = 0;
5756 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5757 CODING_ISO_INVOCATION (coding
, 1)
5758 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5759 /* Setup the initial status of designation. */
5760 for (i
= 0; i
< 4; i
++)
5761 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5762 /* Not single shifting initially. */
5763 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5764 /* Beginning of buffer should also be regarded as bol. */
5765 CODING_ISO_BOL (coding
) = 1;
5766 coding
->detector
= detect_coding_iso_2022
;
5767 coding
->decoder
= decode_coding_iso_2022
;
5768 coding
->encoder
= encode_coding_iso_2022
;
5769 if (flags
& CODING_ISO_FLAG_SAFE
)
5770 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5771 coding
->common_flags
5772 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5773 | CODING_REQUIRE_FLUSHING_MASK
);
5774 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5775 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5776 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5777 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5778 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5780 setup_iso_safe_charsets (attrs
);
5781 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5782 coding
->max_charset_id
= SCHARS (val
) - 1;
5783 coding
->safe_charsets
= SDATA (val
);
5785 CODING_ISO_FLAGS (coding
) = flags
;
5786 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5787 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5788 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5789 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5791 else if (EQ (coding_type
, Qcharset
))
5793 coding
->detector
= detect_coding_charset
;
5794 coding
->decoder
= decode_coding_charset
;
5795 coding
->encoder
= encode_coding_charset
;
5796 coding
->common_flags
5797 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5799 else if (EQ (coding_type
, Qutf_8
))
5801 val
= AREF (attrs
, coding_attr_utf_bom
);
5802 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5803 : EQ (val
, Qt
) ? utf_with_bom
5805 coding
->detector
= detect_coding_utf_8
;
5806 coding
->decoder
= decode_coding_utf_8
;
5807 coding
->encoder
= encode_coding_utf_8
;
5808 coding
->common_flags
5809 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5810 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5811 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5813 else if (EQ (coding_type
, Qutf_16
))
5815 val
= AREF (attrs
, coding_attr_utf_bom
);
5816 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5817 : EQ (val
, Qt
) ? utf_with_bom
5819 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5820 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5821 : utf_16_little_endian
);
5822 CODING_UTF_16_SURROGATE (coding
) = 0;
5823 coding
->detector
= detect_coding_utf_16
;
5824 coding
->decoder
= decode_coding_utf_16
;
5825 coding
->encoder
= encode_coding_utf_16
;
5826 coding
->common_flags
5827 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5828 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5829 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5831 else if (EQ (coding_type
, Qccl
))
5833 coding
->detector
= detect_coding_ccl
;
5834 coding
->decoder
= decode_coding_ccl
;
5835 coding
->encoder
= encode_coding_ccl
;
5836 coding
->common_flags
5837 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5838 | CODING_REQUIRE_FLUSHING_MASK
);
5840 else if (EQ (coding_type
, Qemacs_mule
))
5842 coding
->detector
= detect_coding_emacs_mule
;
5843 coding
->decoder
= decode_coding_emacs_mule
;
5844 coding
->encoder
= encode_coding_emacs_mule
;
5845 coding
->common_flags
5846 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5847 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5848 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5850 Lisp_Object tail
, safe_charsets
;
5851 int max_charset_id
= 0;
5853 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5855 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5856 max_charset_id
= XFASTINT (XCAR (tail
));
5857 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5858 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5859 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5861 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5862 coding
->max_charset_id
= max_charset_id
;
5863 coding
->safe_charsets
= SDATA (safe_charsets
);
5865 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5866 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5868 else if (EQ (coding_type
, Qshift_jis
))
5870 coding
->detector
= detect_coding_sjis
;
5871 coding
->decoder
= decode_coding_sjis
;
5872 coding
->encoder
= encode_coding_sjis
;
5873 coding
->common_flags
5874 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5876 else if (EQ (coding_type
, Qbig5
))
5878 coding
->detector
= detect_coding_big5
;
5879 coding
->decoder
= decode_coding_big5
;
5880 coding
->encoder
= encode_coding_big5
;
5881 coding
->common_flags
5882 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5884 else /* EQ (coding_type, Qraw_text) */
5886 coding
->detector
= NULL
;
5887 coding
->decoder
= decode_coding_raw_text
;
5888 coding
->encoder
= encode_coding_raw_text
;
5889 if (! EQ (eol_type
, Qunix
))
5891 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5892 if (! VECTORP (eol_type
))
5893 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5901 /* Return a list of charsets supported by CODING. */
5904 coding_charset_list (struct coding_system
*coding
)
5906 Lisp_Object attrs
, charset_list
;
5908 CODING_GET_INFO (coding
, attrs
, charset_list
);
5909 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5911 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5913 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5914 charset_list
= Viso_2022_charset_list
;
5916 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5918 charset_list
= Vemacs_mule_charset_list
;
5920 return charset_list
;
5924 /* Return a list of charsets supported by CODING-SYSTEM. */
5927 coding_system_charset_list (Lisp_Object coding_system
)
5930 Lisp_Object attrs
, charset_list
;
5932 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5933 attrs
= CODING_ID_ATTRS (id
);
5935 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5937 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5939 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5940 charset_list
= Viso_2022_charset_list
;
5942 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5944 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5946 charset_list
= Vemacs_mule_charset_list
;
5950 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5952 return charset_list
;
5956 /* Return raw-text or one of its subsidiaries that has the same
5957 eol_type as CODING-SYSTEM. */
5960 raw_text_coding_system (Lisp_Object coding_system
)
5962 Lisp_Object spec
, attrs
;
5963 Lisp_Object eol_type
, raw_text_eol_type
;
5965 if (NILP (coding_system
))
5967 spec
= CODING_SYSTEM_SPEC (coding_system
);
5968 attrs
= AREF (spec
, 0);
5970 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5971 return coding_system
;
5973 eol_type
= AREF (spec
, 2);
5974 if (VECTORP (eol_type
))
5976 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5977 raw_text_eol_type
= AREF (spec
, 2);
5978 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5979 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5980 : AREF (raw_text_eol_type
, 2));
5983 /* Return true if CODING corresponds to raw-text coding-system. */
5986 raw_text_coding_system_p (struct coding_system
*coding
)
5988 return (coding
->decoder
== decode_coding_raw_text
5989 && coding
->encoder
== encode_coding_raw_text
) ? true : false;
5993 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5994 the subsidiary that has the same eol-spec as PARENT (if it is not
5995 nil and specifies end-of-line format) or the system's setting
5996 (system_eol_type). */
5999 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
6001 Lisp_Object spec
, eol_type
;
6003 if (NILP (coding_system
))
6004 coding_system
= Qraw_text
;
6006 CHECK_CODING_SYSTEM (coding_system
);
6007 spec
= CODING_SYSTEM_SPEC (coding_system
);
6008 eol_type
= AREF (spec
, 2);
6009 if (VECTORP (eol_type
))
6011 Lisp_Object parent_eol_type
;
6013 if (! NILP (parent
))
6015 Lisp_Object parent_spec
;
6017 CHECK_CODING_SYSTEM (parent
);
6018 parent_spec
= CODING_SYSTEM_SPEC (parent
);
6019 parent_eol_type
= AREF (parent_spec
, 2);
6020 if (VECTORP (parent_eol_type
))
6021 parent_eol_type
= system_eol_type
;
6024 parent_eol_type
= system_eol_type
;
6025 if (EQ (parent_eol_type
, Qunix
))
6026 coding_system
= AREF (eol_type
, 0);
6027 else if (EQ (parent_eol_type
, Qdos
))
6028 coding_system
= AREF (eol_type
, 1);
6029 else if (EQ (parent_eol_type
, Qmac
))
6030 coding_system
= AREF (eol_type
, 2);
6032 return coding_system
;
6036 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
6037 decided for writing to a process. If not, complement them, and
6038 return a new coding system. */
6041 complement_process_encoding_system (Lisp_Object coding_system
)
6043 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
6044 Lisp_Object spec
, attrs
;
6047 for (i
= 0; i
< 3; i
++)
6050 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
6052 coding_system
= preferred_coding_system ();
6053 spec
= CODING_SYSTEM_SPEC (coding_system
);
6056 attrs
= AREF (spec
, 0);
6057 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
6058 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
6059 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
6060 eol_base
= coding_system
;
6061 if (! NILP (coding_base
) && ! NILP (eol_base
))
6066 /* The original CODING_SYSTEM didn't specify text-conversion or
6067 eol-conversion. Be sure that we return a fully complemented
6069 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
6070 return coding_system
;
6074 /* Emacs has a mechanism to automatically detect a coding system if it
6075 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6076 it's impossible to distinguish some coding systems accurately
6077 because they use the same range of codes. So, at first, coding
6078 systems are categorized into 7, those are:
6080 o coding-category-emacs-mule
6082 The category for a coding system which has the same code range
6083 as Emacs' internal format. Assigned the coding-system (Lisp
6084 symbol) `emacs-mule' by default.
6086 o coding-category-sjis
6088 The category for a coding system which has the same code range
6089 as SJIS. Assigned the coding-system (Lisp
6090 symbol) `japanese-shift-jis' by default.
6092 o coding-category-iso-7
6094 The category for a coding system which has the same code range
6095 as ISO2022 of 7-bit environment. This doesn't use any locking
6096 shift and single shift functions. This can encode/decode all
6097 charsets. Assigned the coding-system (Lisp symbol)
6098 `iso-2022-7bit' by default.
6100 o coding-category-iso-7-tight
6102 Same as coding-category-iso-7 except that this can
6103 encode/decode only the specified charsets.
6105 o coding-category-iso-8-1
6107 The category for a coding system which has the same code range
6108 as ISO2022 of 8-bit environment and graphic plane 1 used only
6109 for DIMENSION1 charset. This doesn't use any locking shift
6110 and single shift functions. Assigned the coding-system (Lisp
6111 symbol) `iso-latin-1' by default.
6113 o coding-category-iso-8-2
6115 The category for a coding system which has the same code range
6116 as ISO2022 of 8-bit environment and graphic plane 1 used only
6117 for DIMENSION2 charset. This doesn't use any locking shift
6118 and single shift functions. Assigned the coding-system (Lisp
6119 symbol) `japanese-iso-8bit' by default.
6121 o coding-category-iso-7-else
6123 The category for a coding system which has the same code range
6124 as ISO2022 of 7-bit environment but uses locking shift or
6125 single shift functions. Assigned the coding-system (Lisp
6126 symbol) `iso-2022-7bit-lock' by default.
6128 o coding-category-iso-8-else
6130 The category for a coding system which has the same code range
6131 as ISO2022 of 8-bit environment but uses locking shift or
6132 single shift functions. Assigned the coding-system (Lisp
6133 symbol) `iso-2022-8bit-ss2' by default.
6135 o coding-category-big5
6137 The category for a coding system which has the same code range
6138 as BIG5. Assigned the coding-system (Lisp symbol)
6139 `cn-big5' by default.
6141 o coding-category-utf-8
6143 The category for a coding system which has the same code range
6144 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6145 symbol) `utf-8' by default.
6147 o coding-category-utf-16-be
6149 The category for a coding system in which a text has an
6150 Unicode signature (cf. Unicode Standard) in the order of BIG
6151 endian at the head. Assigned the coding-system (Lisp symbol)
6152 `utf-16-be' by default.
6154 o coding-category-utf-16-le
6156 The category for a coding system in which a text has an
6157 Unicode signature (cf. Unicode Standard) in the order of
6158 LITTLE endian at the head. Assigned the coding-system (Lisp
6159 symbol) `utf-16-le' by default.
6161 o coding-category-ccl
6163 The category for a coding system of which encoder/decoder is
6164 written in CCL programs. The default value is nil, i.e., no
6165 coding system is assigned.
6167 o coding-category-binary
6169 The category for a coding system not categorized in any of the
6170 above. Assigned the coding-system (Lisp symbol)
6171 `no-conversion' by default.
6173 Each of them is a Lisp symbol and the value is an actual
6174 `coding-system's (this is also a Lisp symbol) assigned by a user.
6175 What Emacs does actually is to detect a category of coding system.
6176 Then, it uses a `coding-system' assigned to it. If Emacs can't
6177 decide only one possible category, it selects a category of the
6178 highest priority. Priorities of categories are also specified by a
6179 user in a Lisp variable `coding-category-list'.
6183 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*coding
,
6187 /* Return the number of ASCII characters at the head of the source.
6188 By side effects, set coding->head_ascii and update
6189 coding->eol_seen. The value of coding->eol_seen is "logical or" of
6190 EOL_SEEN_LF, EOL_SEEN_CR, and EOL_SEEN_CRLF, but the value is
6191 reliable only when all the source bytes are ASCII. */
6194 check_ascii (struct coding_system
*coding
)
6196 const unsigned char *src
, *end
;
6197 Lisp_Object eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6198 int eol_seen
= coding
->eol_seen
;
6200 coding_set_source (coding
);
6201 src
= coding
->source
;
6202 end
= src
+ coding
->src_bytes
;
6204 if (inhibit_eol_conversion
6205 || SYMBOLP (eol_type
))
6207 /* We don't have to check EOL format. */
6208 while (src
< end
&& !( *src
& 0x80))
6211 eol_seen
|= EOL_SEEN_LF
;
6216 end
--; /* We look ahead one byte for "CR LF". */
6228 eol_seen
|= EOL_SEEN_CRLF
;
6232 eol_seen
|= EOL_SEEN_CR
;
6235 eol_seen
|= EOL_SEEN_LF
;
6241 /* All bytes but the last one C are ASCII. */
6245 eol_seen
|= EOL_SEEN_CR
;
6247 eol_seen
|= EOL_SEEN_LF
;
6252 coding
->head_ascii
= src
- coding
->source
;
6253 coding
->eol_seen
= eol_seen
;
6254 return (coding
->head_ascii
);
6258 /* Return the number of characters at the source if all the bytes are
6259 valid UTF-8 (of Unicode range). Otherwise, return -1. By side
6260 effects, update coding->eol_seen. The value of coding->eol_seen is
6261 "logical or" of EOL_SEEN_LF, EOL_SEEN_CR, and EOL_SEEN_CRLF, but
6262 the value is reliable only when all the source bytes are valid
6266 check_utf_8 (struct coding_system
*coding
)
6268 const unsigned char *src
, *end
;
6270 ptrdiff_t nchars
= coding
->head_ascii
;
6272 if (coding
->head_ascii
< 0)
6273 check_ascii (coding
);
6275 coding_set_source (coding
);
6276 src
= coding
->source
+ coding
->head_ascii
;
6277 /* We look ahead one byte for CR LF. */
6278 end
= coding
->source
+ coding
->src_bytes
- 1;
6279 eol_seen
= coding
->eol_seen
;
6284 if (UTF_8_1_OCTET_P (*src
))
6293 eol_seen
|= EOL_SEEN_CRLF
;
6298 eol_seen
|= EOL_SEEN_CR
;
6301 eol_seen
|= EOL_SEEN_LF
;
6304 else if (UTF_8_2_OCTET_LEADING_P (c
))
6306 if (c
< 0xC2 /* overlong sequence */
6308 || ! UTF_8_EXTRA_OCTET_P (src
[1]))
6312 else if (UTF_8_3_OCTET_LEADING_P (c
))
6315 || ! (UTF_8_EXTRA_OCTET_P (src
[1])
6316 && UTF_8_EXTRA_OCTET_P (src
[2])))
6318 c
= (((c
& 0xF) << 12)
6319 | ((src
[1] & 0x3F) << 6) | (src
[2] & 0x3F));
6320 if (c
< 0x800 /* overlong sequence */
6321 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
6325 else if (UTF_8_4_OCTET_LEADING_P (c
))
6328 || ! (UTF_8_EXTRA_OCTET_P (src
[1])
6329 && UTF_8_EXTRA_OCTET_P (src
[2])
6330 && UTF_8_EXTRA_OCTET_P (src
[3])))
6332 c
= (((c
& 0x7) << 18) | ((src
[1] & 0x3F) << 12)
6333 | ((src
[2] & 0x3F) << 6) | (src
[3] & 0x3F));
6334 if (c
< 0x10000 /* overlong sequence */
6335 || c
>= 0x110000) /* non-Unicode character */
6346 if (! UTF_8_1_OCTET_P (*src
))
6350 eol_seen
|= EOL_SEEN_CR
;
6351 else if (*src
== '\n')
6352 eol_seen
|= EOL_SEEN_LF
;
6354 coding
->eol_seen
= eol_seen
;
6359 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6360 SOURCE is encoded. If CATEGORY is one of
6361 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6362 two-byte, else they are encoded by one-byte.
6364 Return one of EOL_SEEN_XXX. */
6366 #define MAX_EOL_CHECK_COUNT 3
6369 detect_eol (const unsigned char *source
, ptrdiff_t src_bytes
,
6370 enum coding_category category
)
6372 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6375 int eol_seen
= EOL_SEEN_NONE
;
6377 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6379 bool msb
= category
== (coding_category_utf_16_le
6380 | coding_category_utf_16_le_nosig
);
6383 while (src
+ 1 < src_end
)
6386 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6391 this_eol
= EOL_SEEN_LF
;
6392 else if (src
+ 3 >= src_end
6393 || src
[msb
+ 2] != 0
6394 || src
[lsb
+ 2] != '\n')
6395 this_eol
= EOL_SEEN_CR
;
6398 this_eol
= EOL_SEEN_CRLF
;
6402 if (eol_seen
== EOL_SEEN_NONE
)
6403 /* This is the first end-of-line. */
6404 eol_seen
= this_eol
;
6405 else if (eol_seen
!= this_eol
)
6407 /* The found type is different from what found before.
6408 Allow for stray ^M characters in DOS EOL files. */
6409 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6410 || (eol_seen
== EOL_SEEN_CRLF
6411 && this_eol
== EOL_SEEN_CR
))
6412 eol_seen
= EOL_SEEN_CRLF
;
6415 eol_seen
= EOL_SEEN_LF
;
6419 if (++total
== MAX_EOL_CHECK_COUNT
)
6426 while (src
< src_end
)
6429 if (c
== '\n' || c
== '\r')
6434 this_eol
= EOL_SEEN_LF
;
6435 else if (src
>= src_end
|| *src
!= '\n')
6436 this_eol
= EOL_SEEN_CR
;
6438 this_eol
= EOL_SEEN_CRLF
, src
++;
6440 if (eol_seen
== EOL_SEEN_NONE
)
6441 /* This is the first end-of-line. */
6442 eol_seen
= this_eol
;
6443 else if (eol_seen
!= this_eol
)
6445 /* The found type is different from what found before.
6446 Allow for stray ^M characters in DOS EOL files. */
6447 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6448 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6449 eol_seen
= EOL_SEEN_CRLF
;
6452 eol_seen
= EOL_SEEN_LF
;
6456 if (++total
== MAX_EOL_CHECK_COUNT
)
6465 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6467 Lisp_Object eol_type
;
6469 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6470 if (! VECTORP (eol_type
))
6471 /* Already adjusted. */
6473 if (eol_seen
& EOL_SEEN_LF
)
6475 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6478 else if (eol_seen
& EOL_SEEN_CRLF
)
6480 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6483 else if (eol_seen
& EOL_SEEN_CR
)
6485 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6491 /* Detect how a text specified in CODING is encoded. If a coding
6492 system is detected, update fields of CODING by the detected coding
6496 detect_coding (struct coding_system
*coding
)
6498 const unsigned char *src
, *src_end
;
6499 unsigned int saved_mode
= coding
->mode
;
6500 Lisp_Object found
= Qnil
;
6501 Lisp_Object eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6503 coding
->consumed
= coding
->consumed_char
= 0;
6504 coding
->produced
= coding
->produced_char
= 0;
6505 coding_set_source (coding
);
6507 src_end
= coding
->source
+ coding
->src_bytes
;
6509 coding
->eol_seen
= EOL_SEEN_NONE
;
6510 /* If we have not yet decided the text encoding type, detect it
6512 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6515 struct coding_detection_info detect_info
;
6516 bool null_byte_found
= 0, eight_bit_found
= 0;
6517 bool inhibit_nbd
= inhibit_flag (coding
->spec
.undecided
.inhibit_nbd
,
6518 inhibit_null_byte_detection
);
6519 bool inhibit_ied
= inhibit_flag (coding
->spec
.undecided
.inhibit_ied
,
6520 inhibit_iso_escape_detection
);
6521 bool prefer_utf_8
= coding
->spec
.undecided
.prefer_utf_8
;
6523 coding
->head_ascii
= 0;
6524 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6525 for (src
= coding
->source
; src
< src_end
; src
++)
6530 eight_bit_found
= 1;
6531 if (null_byte_found
)
6536 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6538 && ! detect_info
.checked
)
6540 if (detect_coding_iso_2022 (coding
, &detect_info
))
6542 /* We have scanned the whole data. */
6543 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6545 /* We didn't find an 8-bit code. We may
6546 have found a null-byte, but it's very
6547 rare that a binary file conforms to
6550 coding
->head_ascii
= src
- coding
->source
;
6552 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6556 else if (! c
&& !inhibit_nbd
)
6558 null_byte_found
= 1;
6559 if (eight_bit_found
)
6562 else if (! disable_ascii_optimization
6563 && ! inhibit_eol_conversion
)
6567 if (src
< src_end
&& src
[1] == '\n')
6569 coding
->eol_seen
|= EOL_SEEN_CRLF
;
6571 if (! eight_bit_found
)
6572 coding
->head_ascii
++;
6575 coding
->eol_seen
|= EOL_SEEN_CR
;
6579 coding
->eol_seen
|= EOL_SEEN_LF
;
6583 if (! eight_bit_found
)
6584 coding
->head_ascii
++;
6586 else if (! eight_bit_found
)
6587 coding
->head_ascii
++;
6590 if (null_byte_found
|| eight_bit_found
6591 || coding
->head_ascii
< coding
->src_bytes
6592 || detect_info
.found
)
6594 enum coding_category category
;
6595 struct coding_system
*this;
6597 if (coding
->head_ascii
== coding
->src_bytes
)
6598 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6599 for (i
= 0; i
< coding_category_raw_text
; i
++)
6601 category
= coding_priorities
[i
];
6602 this = coding_categories
+ category
;
6603 if (detect_info
.found
& (1 << category
))
6608 if (null_byte_found
)
6610 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6611 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6613 else if (prefer_utf_8
6614 && detect_coding_utf_8 (coding
, &detect_info
))
6616 detect_info
.checked
|= ~CATEGORY_MASK_UTF_8
;
6617 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_8
;
6619 for (i
= 0; i
< coding_category_raw_text
; i
++)
6621 category
= coding_priorities
[i
];
6622 this = coding_categories
+ category
;
6623 /* Some of this->detector (e.g. detect_coding_sjis)
6624 require this information. */
6625 coding
->id
= this->id
;
6628 /* No coding system of this category is defined. */
6629 detect_info
.rejected
|= (1 << category
);
6631 else if (category
>= coding_category_raw_text
)
6633 else if (detect_info
.checked
& (1 << category
))
6635 if (detect_info
.found
& (1 << category
))
6638 else if ((*(this->detector
)) (coding
, &detect_info
)
6639 && detect_info
.found
& (1 << category
))
6644 if (i
< coding_category_raw_text
)
6646 if (category
== coding_category_utf_8_auto
)
6648 Lisp_Object coding_systems
;
6650 coding_systems
= AREF (CODING_ID_ATTRS (this->id
),
6651 coding_attr_utf_bom
);
6652 if (CONSP (coding_systems
))
6654 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6655 found
= XCAR (coding_systems
);
6657 found
= XCDR (coding_systems
);
6660 found
= CODING_ID_NAME (this->id
);
6662 else if (category
== coding_category_utf_16_auto
)
6664 Lisp_Object coding_systems
;
6666 coding_systems
= AREF (CODING_ID_ATTRS (this->id
),
6667 coding_attr_utf_bom
);
6668 if (CONSP (coding_systems
))
6670 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6671 found
= XCAR (coding_systems
);
6672 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6673 found
= XCDR (coding_systems
);
6676 found
= CODING_ID_NAME (this->id
);
6679 found
= CODING_ID_NAME (this->id
);
6681 else if (null_byte_found
)
6682 found
= Qno_conversion
;
6683 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6684 == CATEGORY_MASK_ANY
)
6686 else if (detect_info
.rejected
)
6687 for (i
= 0; i
< coding_category_raw_text
; i
++)
6688 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6690 this = coding_categories
+ coding_priorities
[i
];
6691 found
= CODING_ID_NAME (this->id
);
6696 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6697 == coding_category_utf_8_auto
)
6699 Lisp_Object coding_systems
;
6700 struct coding_detection_info detect_info
;
6703 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6704 detect_info
.found
= detect_info
.rejected
= 0;
6705 if (check_ascii (coding
) == coding
->src_bytes
)
6707 if (CONSP (coding_systems
))
6708 found
= XCDR (coding_systems
);
6712 if (CONSP (coding_systems
)
6713 && detect_coding_utf_8 (coding
, &detect_info
))
6715 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6716 found
= XCAR (coding_systems
);
6718 found
= XCDR (coding_systems
);
6722 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6723 == coding_category_utf_16_auto
)
6725 Lisp_Object coding_systems
;
6726 struct coding_detection_info detect_info
;
6729 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6730 detect_info
.found
= detect_info
.rejected
= 0;
6731 coding
->head_ascii
= 0;
6732 if (CONSP (coding_systems
)
6733 && detect_coding_utf_16 (coding
, &detect_info
))
6735 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6736 found
= XCAR (coding_systems
);
6737 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6738 found
= XCDR (coding_systems
);
6744 int specified_eol
= (VECTORP (eol_type
) ? EOL_SEEN_NONE
6745 : EQ (eol_type
, Qdos
) ? EOL_SEEN_CRLF
6746 : EQ (eol_type
, Qmac
) ? EOL_SEEN_CR
6749 setup_coding_system (found
, coding
);
6750 if (specified_eol
!= EOL_SEEN_NONE
)
6751 adjust_coding_eol_type (coding
, specified_eol
);
6754 coding
->mode
= saved_mode
;
6759 decode_eol (struct coding_system
*coding
)
6761 Lisp_Object eol_type
;
6762 unsigned char *p
, *pbeg
, *pend
;
6764 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6765 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6768 if (NILP (coding
->dst_object
))
6769 pbeg
= coding
->destination
;
6771 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6772 pend
= pbeg
+ coding
->produced
;
6774 if (VECTORP (eol_type
))
6776 int eol_seen
= EOL_SEEN_NONE
;
6778 for (p
= pbeg
; p
< pend
; p
++)
6781 eol_seen
|= EOL_SEEN_LF
;
6782 else if (*p
== '\r')
6784 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6786 eol_seen
|= EOL_SEEN_CRLF
;
6790 eol_seen
|= EOL_SEEN_CR
;
6793 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6794 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6795 && (eol_seen
& EOL_SEEN_CR
) != 0
6796 && (eol_seen
& EOL_SEEN_LF
) == 0)
6797 eol_seen
= EOL_SEEN_CRLF
;
6798 else if (eol_seen
!= EOL_SEEN_NONE
6799 && eol_seen
!= EOL_SEEN_LF
6800 && eol_seen
!= EOL_SEEN_CRLF
6801 && eol_seen
!= EOL_SEEN_CR
)
6802 eol_seen
= EOL_SEEN_LF
;
6803 if (eol_seen
!= EOL_SEEN_NONE
)
6804 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6807 if (EQ (eol_type
, Qmac
))
6809 for (p
= pbeg
; p
< pend
; p
++)
6813 else if (EQ (eol_type
, Qdos
))
6817 if (NILP (coding
->dst_object
))
6819 /* Start deleting '\r' from the tail to minimize the memory
6821 for (p
= pend
- 2; p
>= pbeg
; p
--)
6824 memmove (p
, p
+ 1, pend
-- - p
- 1);
6830 ptrdiff_t pos_byte
= coding
->dst_pos_byte
;
6831 ptrdiff_t pos
= coding
->dst_pos
;
6832 ptrdiff_t pos_end
= pos
+ coding
->produced_char
- 1;
6834 while (pos
< pos_end
)
6836 p
= BYTE_POS_ADDR (pos_byte
);
6837 if (*p
== '\r' && p
[1] == '\n')
6839 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6844 if (coding
->dst_multibyte
)
6845 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6850 coding
->produced
-= n
;
6851 coding
->produced_char
-= n
;
6856 /* MAX_LOOKUP's maximum value. MAX_LOOKUP is an int and so cannot
6857 exceed INT_MAX. Also, MAX_LOOKUP is multiplied by sizeof (int) for
6858 alloca, so it cannot exceed MAX_ALLOCA / sizeof (int). */
6859 enum { MAX_LOOKUP_MAX
= min (INT_MAX
, MAX_ALLOCA
/ sizeof (int)) };
6861 /* Return a translation table (or list of them) from coding system
6862 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6866 get_translation_table (Lisp_Object attrs
, bool encodep
, int *max_lookup
)
6868 Lisp_Object standard
, translation_table
;
6871 if (NILP (Venable_character_translation
))
6878 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6879 standard
= Vstandard_translation_table_for_encode
;
6881 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6882 standard
= Vstandard_translation_table_for_decode
;
6883 if (NILP (translation_table
))
6884 translation_table
= standard
;
6887 if (SYMBOLP (translation_table
))
6888 translation_table
= Fget (translation_table
, Qtranslation_table
);
6889 else if (CONSP (translation_table
))
6891 translation_table
= Fcopy_sequence (translation_table
);
6892 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6893 if (SYMBOLP (XCAR (val
)))
6894 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6896 if (CHAR_TABLE_P (standard
))
6898 if (CONSP (translation_table
))
6899 translation_table
= nconc2 (translation_table
, list1 (standard
));
6901 translation_table
= list2 (translation_table
, standard
);
6908 if (CHAR_TABLE_P (translation_table
)
6909 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6911 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6912 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6913 *max_lookup
= min (XFASTINT (val
), MAX_LOOKUP_MAX
);
6915 else if (CONSP (translation_table
))
6919 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6920 if (CHAR_TABLE_P (XCAR (tail
))
6921 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6923 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6924 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6925 *max_lookup
= min (XFASTINT (tailval
), MAX_LOOKUP_MAX
);
6929 return translation_table
;
6932 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6935 if (CHAR_TABLE_P (table)) \
6937 trans = CHAR_TABLE_REF (table, c); \
6938 if (CHARACTERP (trans)) \
6939 c = XFASTINT (trans), trans = Qnil; \
6941 else if (CONSP (table)) \
6945 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6946 if (CHAR_TABLE_P (XCAR (tail))) \
6948 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6949 if (CHARACTERP (trans)) \
6950 c = XFASTINT (trans), trans = Qnil; \
6951 else if (! NILP (trans)) \
6958 /* Return a translation of character(s) at BUF according to TRANS.
6959 TRANS is TO-CHAR or ((FROM . TO) ...) where
6960 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6961 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6962 translation is found, and Qnil if not found..
6963 If BUF is too short to lookup characters in FROM, return Qt. */
6966 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6969 if (INTEGERP (trans
))
6971 for (; CONSP (trans
); trans
= XCDR (trans
))
6973 Lisp_Object val
= XCAR (trans
);
6974 Lisp_Object from
= XCAR (val
);
6975 ptrdiff_t len
= ASIZE (from
);
6978 for (i
= 0; i
< len
; i
++)
6980 if (buf
+ i
== buf_end
)
6982 if (XINT (AREF (from
, i
)) != buf
[i
])
6993 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6996 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6997 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6999 ptrdiff_t produced_chars
= 0;
7002 if (! coding
->chars_at_source
)
7004 /* Source characters are in coding->charbuf. */
7005 int *buf
= coding
->charbuf
;
7006 int *buf_end
= buf
+ coding
->charbuf_used
;
7008 if (EQ (coding
->src_object
, coding
->dst_object
)
7009 && ! NILP (coding
->dst_object
))
7011 eassert (growable_destination (coding
));
7012 coding_set_source (coding
);
7013 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
7016 while (buf
< buf_end
)
7023 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7024 Lisp_Object trans
= Qnil
;
7026 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7029 trans
= get_translation (trans
, buf
, buf_end
);
7030 if (INTEGERP (trans
))
7032 else if (CONSP (trans
))
7034 from_nchars
= ASIZE (XCAR (trans
));
7035 trans
= XCDR (trans
);
7036 if (INTEGERP (trans
))
7040 to_nchars
= ASIZE (trans
);
7041 c
= XINT (AREF (trans
, 0));
7044 else if (EQ (trans
, Qt
) && ! last_block
)
7048 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
7050 eassert (growable_destination (coding
));
7051 if (((min (PTRDIFF_MAX
, SIZE_MAX
) - (buf_end
- buf
))
7052 / MAX_MULTIBYTE_LENGTH
)
7054 memory_full (SIZE_MAX
);
7055 dst
= alloc_destination (coding
,
7057 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
7059 if (EQ (coding
->src_object
, coding
->dst_object
))
7061 coding_set_source (coding
);
7062 dst_end
= (((unsigned char *) coding
->source
)
7063 + coding
->consumed
);
7066 dst_end
= coding
->destination
+ coding
->dst_bytes
;
7069 for (i
= 0; i
< to_nchars
; i
++)
7072 c
= XINT (AREF (trans
, i
));
7073 if (coding
->dst_multibyte
7074 || ! CHAR_BYTE8_P (c
))
7075 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
7077 *dst
++ = CHAR_TO_BYTE8 (c
);
7079 produced_chars
+= to_nchars
;
7083 /* This is an annotation datum. (-C) is the length. */
7086 carryover
= buf_end
- buf
;
7090 /* Source characters are at coding->source. */
7091 const unsigned char *src
= coding
->source
;
7092 const unsigned char *src_end
= src
+ coding
->consumed
;
7094 if (EQ (coding
->dst_object
, coding
->src_object
))
7096 eassert (growable_destination (coding
));
7097 dst_end
= (unsigned char *) src
;
7099 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
7101 if (coding
->src_multibyte
)
7103 bool multibytep
= 1;
7104 ptrdiff_t consumed_chars
= 0;
7108 const unsigned char *src_base
= src
;
7114 eassert (growable_destination (coding
));
7115 if (EQ (coding
->src_object
, coding
->dst_object
))
7116 dst_end
= (unsigned char *) src
;
7119 ptrdiff_t offset
= src
- coding
->source
;
7121 dst
= alloc_destination (coding
, src_end
- src
+ 1,
7123 dst_end
= coding
->destination
+ coding
->dst_bytes
;
7124 coding_set_source (coding
);
7125 src
= coding
->source
+ offset
;
7126 src_end
= coding
->source
+ coding
->consumed
;
7127 if (EQ (coding
->src_object
, coding
->dst_object
))
7128 dst_end
= (unsigned char *) src
;
7138 while (src
< src_end
)
7140 bool multibytep
= 1;
7143 if (dst
>= dst_end
- 1)
7145 eassert (growable_destination (coding
));
7146 if (EQ (coding
->src_object
, coding
->dst_object
))
7147 dst_end
= (unsigned char *) src
;
7148 if (dst
>= dst_end
- 1)
7150 ptrdiff_t offset
= src
- coding
->source
;
7151 ptrdiff_t more_bytes
;
7153 if (EQ (coding
->src_object
, coding
->dst_object
))
7154 more_bytes
= ((src_end
- src
) / 2) + 2;
7156 more_bytes
= src_end
- src
+ 2;
7157 dst
= alloc_destination (coding
, more_bytes
, dst
);
7158 dst_end
= coding
->destination
+ coding
->dst_bytes
;
7159 coding_set_source (coding
);
7160 src
= coding
->source
+ offset
;
7161 src_end
= coding
->source
+ coding
->consumed
;
7162 if (EQ (coding
->src_object
, coding
->dst_object
))
7163 dst_end
= (unsigned char *) src
;
7171 if (!EQ (coding
->src_object
, coding
->dst_object
))
7173 ptrdiff_t require
= coding
->src_bytes
- coding
->dst_bytes
;
7177 ptrdiff_t offset
= src
- coding
->source
;
7179 dst
= alloc_destination (coding
, require
, dst
);
7180 coding_set_source (coding
);
7181 src
= coding
->source
+ offset
;
7182 src_end
= coding
->source
+ coding
->consumed
;
7185 produced_chars
= coding
->consumed_char
;
7186 while (src
< src_end
)
7191 produced
= dst
- (coding
->destination
+ coding
->produced
);
7192 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
7193 insert_from_gap (produced_chars
, produced
, 0);
7194 coding
->produced
+= produced
;
7195 coding
->produced_char
+= produced_chars
;
7199 /* Compose text in CODING->object according to the annotation data at
7200 CHARBUF. CHARBUF is an array:
7201 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
7205 produce_composition (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
7209 enum composition_method method
;
7210 Lisp_Object components
;
7212 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
7213 to
= pos
+ charbuf
[2];
7214 method
= (enum composition_method
) (charbuf
[4]);
7216 if (method
== COMPOSITION_RELATIVE
)
7220 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
7223 if (method
== COMPOSITION_WITH_RULE
)
7224 len
= charbuf
[2] * 3 - 2;
7225 charbuf
+= MAX_ANNOTATION_LENGTH
;
7226 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
7227 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
7229 if (charbuf
[i
] >= 0)
7230 args
[j
] = make_number (charbuf
[i
]);
7234 args
[j
] = make_number (charbuf
[i
] % 0x100);
7237 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
7239 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
7243 /* Put `charset' property on text in CODING->object according to
7244 the annotation data at CHARBUF. CHARBUF is an array:
7245 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
7249 produce_charset (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
7251 ptrdiff_t from
= pos
- charbuf
[2];
7252 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
7254 Fput_text_property (make_number (from
), make_number (pos
),
7255 Qcharset
, CHARSET_NAME (charset
),
7256 coding
->dst_object
);
7259 #define MAX_CHARBUF_SIZE 0x4000
7260 /* How many units decoding functions expect in coding->charbuf at
7261 most. Currently, decode_coding_emacs_mule expects the following
7262 size, and that is the largest value. */
7263 #define MAX_CHARBUF_EXTRA_SIZE ((MAX_ANNOTATION_LENGTH * 3) + 1)
7265 #define ALLOC_CONVERSION_WORK_AREA(coding, size) \
7267 ptrdiff_t units = min ((size) + MAX_CHARBUF_EXTRA_SIZE, \
7268 MAX_CHARBUF_SIZE); \
7269 coding->charbuf = SAFE_ALLOCA (units * sizeof (int)); \
7270 coding->charbuf_size = units; \
7274 produce_annotation (struct coding_system
*coding
, ptrdiff_t pos
)
7276 int *charbuf
= coding
->charbuf
;
7277 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
7279 if (NILP (coding
->dst_object
))
7282 while (charbuf
< charbuf_end
)
7288 int len
= -*charbuf
;
7293 case CODING_ANNOTATE_COMPOSITION_MASK
:
7294 produce_composition (coding
, charbuf
, pos
);
7296 case CODING_ANNOTATE_CHARSET_MASK
:
7297 produce_charset (coding
, charbuf
, pos
);
7307 /* Decode the data at CODING->src_object into CODING->dst_object.
7308 CODING->src_object is a buffer, a string, or nil.
7309 CODING->dst_object is a buffer.
7311 If CODING->src_object is a buffer, it must be the current buffer.
7312 In this case, if CODING->src_pos is positive, it is a position of
7313 the source text in the buffer, otherwise, the source text is in the
7314 gap area of the buffer, and CODING->src_pos specifies the offset of
7315 the text from GPT (which must be the same as PT). If this is the
7316 same buffer as CODING->dst_object, CODING->src_pos must be
7319 If CODING->src_object is a string, CODING->src_pos is an index to
7322 If CODING->src_object is nil, CODING->source must already point to
7323 the non-relocatable memory area. In this case, CODING->src_pos is
7324 an offset from CODING->source.
7326 The decoded data is inserted at the current point of the buffer
7331 decode_coding (struct coding_system
*coding
)
7334 Lisp_Object undo_list
;
7335 Lisp_Object translation_table
;
7336 struct ccl_spec cclspec
;
7342 if (BUFFERP (coding
->src_object
)
7343 && coding
->src_pos
> 0
7344 && coding
->src_pos
< GPT
7345 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7346 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7349 if (BUFFERP (coding
->dst_object
))
7351 set_buffer_internal (XBUFFER (coding
->dst_object
));
7353 move_gap_both (PT
, PT_BYTE
);
7355 /* We must disable undo_list in order to record the whole insert
7356 transaction via record_insert at the end. But doing so also
7357 disables the recording of the first change to the undo_list.
7358 Therefore we check for first change here and record it via
7359 record_first_change if needed. */
7360 if (MODIFF
<= SAVE_MODIFF
)
7361 record_first_change ();
7363 undo_list
= BVAR (current_buffer
, undo_list
);
7364 bset_undo_list (current_buffer
, Qt
);
7367 coding
->consumed
= coding
->consumed_char
= 0;
7368 coding
->produced
= coding
->produced_char
= 0;
7369 coding
->chars_at_source
= 0;
7370 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7372 ALLOC_CONVERSION_WORK_AREA (coding
, coding
->src_bytes
);
7374 attrs
= CODING_ID_ATTRS (coding
->id
);
7375 translation_table
= get_translation_table (attrs
, 0, NULL
);
7378 if (coding
->decoder
== decode_coding_ccl
)
7380 coding
->spec
.ccl
= &cclspec
;
7381 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7385 ptrdiff_t pos
= coding
->dst_pos
+ coding
->produced_char
;
7387 coding_set_source (coding
);
7388 coding
->annotated
= 0;
7389 coding
->charbuf_used
= carryover
;
7390 (*(coding
->decoder
)) (coding
);
7391 coding_set_destination (coding
);
7392 carryover
= produce_chars (coding
, translation_table
, 0);
7393 if (coding
->annotated
)
7394 produce_annotation (coding
, pos
);
7395 for (i
= 0; i
< carryover
; i
++)
7397 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7399 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7400 || (coding
->consumed
< coding
->src_bytes
7401 && (coding
->result
== CODING_RESULT_SUCCESS
7402 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7406 coding_set_destination (coding
);
7407 coding
->charbuf_used
= carryover
;
7408 produce_chars (coding
, translation_table
, 1);
7411 coding
->carryover_bytes
= 0;
7412 if (coding
->consumed
< coding
->src_bytes
)
7414 ptrdiff_t nbytes
= coding
->src_bytes
- coding
->consumed
;
7415 const unsigned char *src
;
7417 coding_set_source (coding
);
7418 coding_set_destination (coding
);
7419 src
= coding
->source
+ coding
->consumed
;
7421 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7423 /* Flush out unprocessed data as binary chars. We are sure
7424 that the number of data is less than the size of
7426 coding
->charbuf_used
= 0;
7427 coding
->chars_at_source
= 0;
7429 while (nbytes
-- > 0)
7434 c
= BYTE8_TO_CHAR (c
);
7435 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7437 produce_chars (coding
, Qnil
, 1);
7441 /* Record unprocessed bytes in coding->carryover. We are
7442 sure that the number of data is less than the size of
7443 coding->carryover. */
7444 unsigned char *p
= coding
->carryover
;
7446 if (nbytes
> sizeof coding
->carryover
)
7447 nbytes
= sizeof coding
->carryover
;
7448 coding
->carryover_bytes
= nbytes
;
7449 while (nbytes
-- > 0)
7452 coding
->consumed
= coding
->src_bytes
;
7455 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7456 && !inhibit_eol_conversion
)
7457 decode_eol (coding
);
7458 if (BUFFERP (coding
->dst_object
))
7460 bset_undo_list (current_buffer
, undo_list
);
7461 record_insert (coding
->dst_pos
, coding
->produced_char
);
7468 /* Extract an annotation datum from a composition starting at POS and
7469 ending before LIMIT of CODING->src_object (buffer or string), store
7470 the data in BUF, set *STOP to a starting position of the next
7471 composition (if any) or to LIMIT, and return the address of the
7472 next element of BUF.
7474 If such an annotation is not found, set *STOP to a starting
7475 position of a composition after POS (if any) or to LIMIT, and
7479 handle_composition_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7480 struct coding_system
*coding
, int *buf
,
7483 ptrdiff_t start
, end
;
7486 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7489 else if (start
> pos
)
7495 /* We found a composition. Store the corresponding
7496 annotation data in BUF. */
7498 enum composition_method method
= composition_method (prop
);
7499 int nchars
= COMPOSITION_LENGTH (prop
);
7501 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7502 if (method
!= COMPOSITION_RELATIVE
)
7504 Lisp_Object components
;
7505 ptrdiff_t i
, len
, i_byte
;
7507 components
= COMPOSITION_COMPONENTS (prop
);
7508 if (VECTORP (components
))
7510 len
= ASIZE (components
);
7511 for (i
= 0; i
< len
; i
++)
7512 *buf
++ = XINT (AREF (components
, i
));
7514 else if (STRINGP (components
))
7516 len
= SCHARS (components
);
7520 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7524 else if (INTEGERP (components
))
7527 *buf
++ = XINT (components
);
7529 else if (CONSP (components
))
7531 for (len
= 0; CONSP (components
);
7532 len
++, components
= XCDR (components
))
7533 *buf
++ = XINT (XCAR (components
));
7541 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7552 /* Extract an annotation datum from a text property `charset' at POS of
7553 CODING->src_object (buffer of string), store the data in BUF, set
7554 *STOP to the position where the value of `charset' property changes
7555 (limiting by LIMIT), and return the address of the next element of
7558 If the property value is nil, set *STOP to the position where the
7559 property value is non-nil (limiting by LIMIT), and return BUF. */
7562 handle_charset_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7563 struct coding_system
*coding
, int *buf
,
7566 Lisp_Object val
, next
;
7569 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7570 if (! NILP (val
) && CHARSETP (val
))
7571 id
= XINT (CHARSET_SYMBOL_ID (val
));
7574 ADD_CHARSET_DATA (buf
, 0, id
);
7575 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7577 make_number (limit
));
7578 *stop
= XINT (next
);
7584 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7587 int *buf
= coding
->charbuf
;
7588 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7589 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7590 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7591 ptrdiff_t pos
= coding
->src_pos
+ coding
->consumed_char
;
7592 ptrdiff_t end_pos
= coding
->src_pos
+ coding
->src_chars
;
7593 bool multibytep
= coding
->src_multibyte
;
7594 Lisp_Object eol_type
;
7596 ptrdiff_t stop
, stop_composition
, stop_charset
;
7597 int *lookup_buf
= NULL
;
7599 if (! NILP (translation_table
))
7600 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7602 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7603 if (VECTORP (eol_type
))
7606 /* Note: composition handling is not yet implemented. */
7607 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7609 if (NILP (coding
->src_object
))
7610 stop
= stop_composition
= stop_charset
= end_pos
;
7613 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7614 stop
= stop_composition
= pos
;
7616 stop
= stop_composition
= end_pos
;
7617 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7618 stop
= stop_charset
= pos
;
7620 stop_charset
= end_pos
;
7623 /* Compensate for CRLF and conversion. */
7624 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7625 while (buf
< buf_end
)
7633 if (pos
== stop_composition
)
7634 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7635 buf
, &stop_composition
);
7636 if (pos
== stop_charset
)
7637 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7638 buf
, &stop_charset
);
7639 stop
= (stop_composition
< stop_charset
7640 ? stop_composition
: stop_charset
);
7647 if (coding
->encoder
== encode_coding_raw_text
7648 || coding
->encoder
== encode_coding_ccl
)
7650 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7651 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7653 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7656 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7657 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7659 if (! EQ (eol_type
, Qunix
))
7663 if (EQ (eol_type
, Qdos
))
7671 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7676 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7677 int *lookup_buf_end
;
7678 const unsigned char *p
= src
;
7682 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7683 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7684 lookup_buf_end
= lookup_buf
+ i
;
7685 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7686 if (INTEGERP (trans
))
7688 else if (CONSP (trans
))
7690 from_nchars
= ASIZE (XCAR (trans
));
7691 trans
= XCDR (trans
);
7692 if (INTEGERP (trans
))
7696 to_nchars
= ASIZE (trans
);
7697 if (buf_end
- buf
< to_nchars
)
7699 c
= XINT (AREF (trans
, 0));
7705 for (i
= 1; i
< to_nchars
; i
++)
7706 *buf
++ = XINT (AREF (trans
, i
));
7707 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7708 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7712 coding
->consumed
= src
- coding
->source
;
7713 coding
->consumed_char
= pos
- coding
->src_pos
;
7714 coding
->charbuf_used
= buf
- coding
->charbuf
;
7715 coding
->chars_at_source
= 0;
7719 /* Encode the text at CODING->src_object into CODING->dst_object.
7720 CODING->src_object is a buffer or a string.
7721 CODING->dst_object is a buffer or nil.
7723 If CODING->src_object is a buffer, it must be the current buffer.
7724 In this case, if CODING->src_pos is positive, it is a position of
7725 the source text in the buffer, otherwise. the source text is in the
7726 gap area of the buffer, and coding->src_pos specifies the offset of
7727 the text from GPT (which must be the same as PT). If this is the
7728 same buffer as CODING->dst_object, CODING->src_pos must be
7729 negative and CODING should not have `pre-write-conversion'.
7731 If CODING->src_object is a string, CODING should not have
7732 `pre-write-conversion'.
7734 If CODING->dst_object is a buffer, the encoded data is inserted at
7735 the current point of that buffer.
7737 If CODING->dst_object is nil, the encoded data is placed at the
7738 memory area specified by CODING->destination. */
7741 encode_coding (struct coding_system
*coding
)
7744 Lisp_Object translation_table
;
7746 struct ccl_spec cclspec
;
7750 attrs
= CODING_ID_ATTRS (coding
->id
);
7751 if (coding
->encoder
== encode_coding_raw_text
)
7752 translation_table
= Qnil
, max_lookup
= 0;
7754 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7756 if (BUFFERP (coding
->dst_object
))
7758 set_buffer_internal (XBUFFER (coding
->dst_object
));
7759 coding
->dst_multibyte
7760 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7763 coding
->consumed
= coding
->consumed_char
= 0;
7764 coding
->produced
= coding
->produced_char
= 0;
7765 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7767 ALLOC_CONVERSION_WORK_AREA (coding
, coding
->src_chars
);
7769 if (coding
->encoder
== encode_coding_ccl
)
7771 coding
->spec
.ccl
= &cclspec
;
7772 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7775 coding_set_source (coding
);
7776 consume_chars (coding
, translation_table
, max_lookup
);
7777 coding_set_destination (coding
);
7778 (*(coding
->encoder
)) (coding
);
7779 } while (coding
->consumed_char
< coding
->src_chars
);
7781 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7782 insert_from_gap (coding
->produced_char
, coding
->produced
, 0);
7788 /* Name (or base name) of work buffer for code conversion. */
7789 static Lisp_Object Vcode_conversion_workbuf_name
;
7791 /* A working buffer used by the top level conversion. Once it is
7792 created, it is never destroyed. It has the name
7793 Vcode_conversion_workbuf_name. The other working buffers are
7794 destroyed after the use is finished, and their names are modified
7795 versions of Vcode_conversion_workbuf_name. */
7796 static Lisp_Object Vcode_conversion_reused_workbuf
;
7798 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7799 static bool reused_workbuf_in_use
;
7802 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7803 multibyteness of returning buffer. */
7806 make_conversion_work_buffer (bool multibyte
)
7808 Lisp_Object name
, workbuf
;
7809 struct buffer
*current
;
7811 if (reused_workbuf_in_use
)
7813 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7814 workbuf
= Fget_buffer_create (name
);
7818 reused_workbuf_in_use
= 1;
7819 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7820 Vcode_conversion_reused_workbuf
7821 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7822 workbuf
= Vcode_conversion_reused_workbuf
;
7824 current
= current_buffer
;
7825 set_buffer_internal (XBUFFER (workbuf
));
7826 /* We can't allow modification hooks to run in the work buffer. For
7827 instance, directory_files_internal assumes that file decoding
7828 doesn't compile new regexps. */
7829 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7831 bset_undo_list (current_buffer
, Qt
);
7832 bset_enable_multibyte_characters (current_buffer
, multibyte
? Qt
: Qnil
);
7833 set_buffer_internal (current
);
7839 code_conversion_restore (Lisp_Object arg
)
7841 Lisp_Object current
, workbuf
;
7843 current
= XCAR (arg
);
7844 workbuf
= XCDR (arg
);
7845 if (! NILP (workbuf
))
7847 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7848 reused_workbuf_in_use
= 0;
7850 Fkill_buffer (workbuf
);
7852 set_buffer_internal (XBUFFER (current
));
7856 code_conversion_save (bool with_work_buf
, bool multibyte
)
7858 Lisp_Object workbuf
= Qnil
;
7861 workbuf
= make_conversion_work_buffer (multibyte
);
7862 record_unwind_protect (code_conversion_restore
,
7863 Fcons (Fcurrent_buffer (), workbuf
));
7868 decode_coding_gap (struct coding_system
*coding
,
7869 ptrdiff_t chars
, ptrdiff_t bytes
)
7871 ptrdiff_t count
= SPECPDL_INDEX ();
7874 coding
->src_object
= Fcurrent_buffer ();
7875 coding
->src_chars
= chars
;
7876 coding
->src_bytes
= bytes
;
7877 coding
->src_pos
= -chars
;
7878 coding
->src_pos_byte
= -bytes
;
7879 coding
->src_multibyte
= chars
< bytes
;
7880 coding
->dst_object
= coding
->src_object
;
7881 coding
->dst_pos
= PT
;
7882 coding
->dst_pos_byte
= PT_BYTE
;
7883 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7885 coding
->head_ascii
= -1;
7886 coding
->detected_utf8_bytes
= coding
->detected_utf8_chars
= -1;
7887 coding
->eol_seen
= EOL_SEEN_NONE
;
7888 if (CODING_REQUIRE_DETECTION (coding
))
7889 detect_coding (coding
);
7890 attrs
= CODING_ID_ATTRS (coding
->id
);
7891 if (! disable_ascii_optimization
7892 && ! coding
->src_multibyte
7893 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
7894 && NILP (CODING_ATTR_POST_READ (attrs
))
7895 && NILP (get_translation_table (attrs
, 0, NULL
)))
7897 chars
= coding
->head_ascii
;
7899 chars
= check_ascii (coding
);
7902 /* There exists a non-ASCII byte. */
7903 if (EQ (CODING_ATTR_TYPE (attrs
), Qutf_8
)
7904 && coding
->detected_utf8_bytes
== coding
->src_bytes
)
7906 if (coding
->detected_utf8_chars
>= 0)
7907 chars
= coding
->detected_utf8_chars
;
7909 chars
= check_utf_8 (coding
);
7910 if (CODING_UTF_8_BOM (coding
) != utf_without_bom
7911 && coding
->head_ascii
== 0
7912 && coding
->source
[0] == UTF_8_BOM_1
7913 && coding
->source
[1] == UTF_8_BOM_2
7914 && coding
->source
[2] == UTF_8_BOM_3
)
7918 coding
->src_bytes
-= 3;
7926 Lisp_Object eol_type
;
7928 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
7929 if (VECTORP (eol_type
))
7931 if (coding
->eol_seen
!= EOL_SEEN_NONE
)
7932 eol_type
= adjust_coding_eol_type (coding
, coding
->eol_seen
);
7934 if (EQ (eol_type
, Qmac
))
7936 unsigned char *src_end
= GAP_END_ADDR
;
7937 unsigned char *src
= src_end
- coding
->src_bytes
;
7939 while (src
< src_end
)
7945 else if (EQ (eol_type
, Qdos
))
7947 unsigned char *src
= GAP_END_ADDR
;
7948 unsigned char *src_beg
= src
- coding
->src_bytes
;
7949 unsigned char *dst
= src
;
7952 while (src_beg
< src
)
7955 if (*src
== '\n' && src
> src_beg
&& src
[-1] == '\r')
7962 coding
->produced
= bytes
;
7963 coding
->produced_char
= chars
;
7964 insert_from_gap (chars
, bytes
, 1);
7968 code_conversion_save (0, 0);
7970 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7971 current_buffer
->text
->inhibit_shrinking
= 1;
7972 decode_coding (coding
);
7973 current_buffer
->text
->inhibit_shrinking
= 0;
7975 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7977 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7980 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7981 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7982 make_number (coding
->produced_char
));
7984 coding
->produced_char
+= Z
- prev_Z
;
7985 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7988 unbind_to (count
, Qnil
);
7992 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7993 SRC_OBJECT into DST_OBJECT by coding context CODING.
7995 SRC_OBJECT is a buffer, a string, or Qnil.
7997 If it is a buffer, the text is at point of the buffer. FROM and TO
7998 are positions in the buffer.
8000 If it is a string, the text is at the beginning of the string.
8001 FROM and TO are indices to the string.
8003 If it is nil, the text is at coding->source. FROM and TO are
8004 indices to coding->source.
8006 DST_OBJECT is a buffer, Qt, or Qnil.
8008 If it is a buffer, the decoded text is inserted at point of the
8009 buffer. If the buffer is the same as SRC_OBJECT, the source text
8012 If it is Qt, a string is made from the decoded text, and
8013 set in CODING->dst_object.
8015 If it is Qnil, the decoded text is stored at CODING->destination.
8016 The caller must allocate CODING->dst_bytes bytes at
8017 CODING->destination by xmalloc. If the decoded text is longer than
8018 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
8022 decode_coding_object (struct coding_system
*coding
,
8023 Lisp_Object src_object
,
8024 ptrdiff_t from
, ptrdiff_t from_byte
,
8025 ptrdiff_t to
, ptrdiff_t to_byte
,
8026 Lisp_Object dst_object
)
8028 ptrdiff_t count
= SPECPDL_INDEX ();
8029 unsigned char *destination
IF_LINT (= NULL
);
8030 ptrdiff_t dst_bytes
IF_LINT (= 0);
8031 ptrdiff_t chars
= to
- from
;
8032 ptrdiff_t bytes
= to_byte
- from_byte
;
8034 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
8035 bool need_marker_adjustment
= 0;
8036 Lisp_Object old_deactivate_mark
;
8038 old_deactivate_mark
= Vdeactivate_mark
;
8040 if (NILP (dst_object
))
8042 destination
= coding
->destination
;
8043 dst_bytes
= coding
->dst_bytes
;
8046 coding
->src_object
= src_object
;
8047 coding
->src_chars
= chars
;
8048 coding
->src_bytes
= bytes
;
8049 coding
->src_multibyte
= chars
< bytes
;
8051 if (STRINGP (src_object
))
8053 coding
->src_pos
= from
;
8054 coding
->src_pos_byte
= from_byte
;
8056 else if (BUFFERP (src_object
))
8058 set_buffer_internal (XBUFFER (src_object
));
8060 move_gap_both (from
, from_byte
);
8061 if (EQ (src_object
, dst_object
))
8063 struct Lisp_Marker
*tail
;
8065 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8067 tail
->need_adjustment
8068 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
8069 need_marker_adjustment
|= tail
->need_adjustment
;
8071 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8072 TEMP_SET_PT_BOTH (from
, from_byte
);
8073 current_buffer
->text
->inhibit_shrinking
= 1;
8074 del_range_both (from
, from_byte
, to
, to_byte
, 1);
8075 coding
->src_pos
= -chars
;
8076 coding
->src_pos_byte
= -bytes
;
8080 coding
->src_pos
= from
;
8081 coding
->src_pos_byte
= from_byte
;
8085 if (CODING_REQUIRE_DETECTION (coding
))
8086 detect_coding (coding
);
8087 attrs
= CODING_ID_ATTRS (coding
->id
);
8089 if (EQ (dst_object
, Qt
)
8090 || (! NILP (CODING_ATTR_POST_READ (attrs
))
8091 && NILP (dst_object
)))
8093 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
8094 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
8095 coding
->dst_pos
= BEG
;
8096 coding
->dst_pos_byte
= BEG_BYTE
;
8098 else if (BUFFERP (dst_object
))
8100 code_conversion_save (0, 0);
8101 coding
->dst_object
= dst_object
;
8102 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
8103 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
8104 coding
->dst_multibyte
8105 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
8109 code_conversion_save (0, 0);
8110 coding
->dst_object
= Qnil
;
8111 /* Most callers presume this will return a multibyte result, and they
8112 won't use `binary' or `raw-text' anyway, so let's not worry about
8113 CODING_FOR_UNIBYTE. */
8114 coding
->dst_multibyte
= 1;
8117 decode_coding (coding
);
8119 if (BUFFERP (coding
->dst_object
))
8120 set_buffer_internal (XBUFFER (coding
->dst_object
));
8122 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
8124 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
8127 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
8128 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
8129 make_number (coding
->produced_char
));
8131 coding
->produced_char
+= Z
- prev_Z
;
8132 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
8135 if (EQ (dst_object
, Qt
))
8137 coding
->dst_object
= Fbuffer_string ();
8139 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
8141 set_buffer_internal (XBUFFER (coding
->dst_object
));
8142 if (dst_bytes
< coding
->produced
)
8144 eassert (coding
->produced
> 0);
8145 destination
= xrealloc (destination
, coding
->produced
);
8146 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
8147 move_gap_both (BEGV
, BEGV_BYTE
);
8148 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
8149 coding
->destination
= destination
;
8155 /* This is the case of:
8156 (BUFFERP (src_object) && EQ (src_object, dst_object))
8157 As we have moved PT while replacing the original buffer
8158 contents, we must recover it now. */
8159 set_buffer_internal (XBUFFER (src_object
));
8160 current_buffer
->text
->inhibit_shrinking
= 0;
8161 if (saved_pt
< from
)
8162 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8163 else if (saved_pt
< from
+ chars
)
8164 TEMP_SET_PT_BOTH (from
, from_byte
);
8165 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8166 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8167 saved_pt_byte
+ (coding
->produced
- bytes
));
8169 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8170 saved_pt_byte
+ (coding
->produced
- bytes
));
8172 if (need_marker_adjustment
)
8174 struct Lisp_Marker
*tail
;
8176 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8177 if (tail
->need_adjustment
)
8179 tail
->need_adjustment
= 0;
8180 if (tail
->insertion_type
)
8182 tail
->bytepos
= from_byte
;
8183 tail
->charpos
= from
;
8187 tail
->bytepos
= from_byte
+ coding
->produced
;
8189 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8190 ? tail
->bytepos
: from
+ coding
->produced_char
);
8196 Vdeactivate_mark
= old_deactivate_mark
;
8197 unbind_to (count
, coding
->dst_object
);
8202 encode_coding_object (struct coding_system
*coding
,
8203 Lisp_Object src_object
,
8204 ptrdiff_t from
, ptrdiff_t from_byte
,
8205 ptrdiff_t to
, ptrdiff_t to_byte
,
8206 Lisp_Object dst_object
)
8208 ptrdiff_t count
= SPECPDL_INDEX ();
8209 ptrdiff_t chars
= to
- from
;
8210 ptrdiff_t bytes
= to_byte
- from_byte
;
8212 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
8213 bool need_marker_adjustment
= 0;
8214 bool kill_src_buffer
= 0;
8215 Lisp_Object old_deactivate_mark
;
8217 old_deactivate_mark
= Vdeactivate_mark
;
8219 coding
->src_object
= src_object
;
8220 coding
->src_chars
= chars
;
8221 coding
->src_bytes
= bytes
;
8222 coding
->src_multibyte
= chars
< bytes
;
8224 attrs
= CODING_ID_ATTRS (coding
->id
);
8226 if (EQ (src_object
, dst_object
))
8228 struct Lisp_Marker
*tail
;
8230 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8232 tail
->need_adjustment
8233 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
8234 need_marker_adjustment
|= tail
->need_adjustment
;
8238 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
8240 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
8241 set_buffer_internal (XBUFFER (coding
->src_object
));
8242 if (STRINGP (src_object
))
8243 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
8244 else if (BUFFERP (src_object
))
8245 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
8247 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
8249 if (EQ (src_object
, dst_object
))
8251 set_buffer_internal (XBUFFER (src_object
));
8252 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8253 del_range_both (from
, from_byte
, to
, to_byte
, 1);
8254 set_buffer_internal (XBUFFER (coding
->src_object
));
8257 safe_call2 (CODING_ATTR_PRE_WRITE (attrs
),
8258 make_number (BEG
), make_number (Z
));
8259 if (XBUFFER (coding
->src_object
) != current_buffer
)
8260 kill_src_buffer
= 1;
8261 coding
->src_object
= Fcurrent_buffer ();
8263 move_gap_both (BEG
, BEG_BYTE
);
8264 coding
->src_chars
= Z
- BEG
;
8265 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
8266 coding
->src_pos
= BEG
;
8267 coding
->src_pos_byte
= BEG_BYTE
;
8268 coding
->src_multibyte
= Z
< Z_BYTE
;
8270 else if (STRINGP (src_object
))
8272 code_conversion_save (0, 0);
8273 coding
->src_pos
= from
;
8274 coding
->src_pos_byte
= from_byte
;
8276 else if (BUFFERP (src_object
))
8278 code_conversion_save (0, 0);
8279 set_buffer_internal (XBUFFER (src_object
));
8280 if (EQ (src_object
, dst_object
))
8282 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8283 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
8284 coding
->src_pos
= 0;
8285 coding
->src_pos_byte
= 0;
8289 if (from
< GPT
&& to
>= GPT
)
8290 move_gap_both (from
, from_byte
);
8291 coding
->src_pos
= from
;
8292 coding
->src_pos_byte
= from_byte
;
8297 code_conversion_save (0, 0);
8298 coding
->src_pos
= from
;
8299 coding
->src_pos_byte
= from_byte
;
8302 if (BUFFERP (dst_object
))
8304 coding
->dst_object
= dst_object
;
8305 if (EQ (src_object
, dst_object
))
8307 coding
->dst_pos
= from
;
8308 coding
->dst_pos_byte
= from_byte
;
8312 struct buffer
*current
= current_buffer
;
8314 set_buffer_temp (XBUFFER (dst_object
));
8315 coding
->dst_pos
= PT
;
8316 coding
->dst_pos_byte
= PT_BYTE
;
8317 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
8318 set_buffer_temp (current
);
8320 coding
->dst_multibyte
8321 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
8323 else if (EQ (dst_object
, Qt
))
8325 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
8326 coding
->dst_object
= Qnil
;
8327 coding
->destination
= xmalloc (dst_bytes
);
8328 coding
->dst_bytes
= dst_bytes
;
8329 coding
->dst_multibyte
= 0;
8333 coding
->dst_object
= Qnil
;
8334 coding
->dst_multibyte
= 0;
8337 encode_coding (coding
);
8339 if (EQ (dst_object
, Qt
))
8341 if (BUFFERP (coding
->dst_object
))
8342 coding
->dst_object
= Fbuffer_string ();
8343 else if (coding
->raw_destination
)
8344 /* This is used to avoid creating huge Lisp string.
8345 NOTE: caller who sets `raw_destination' is also
8346 responsible for freeing `destination' buffer. */
8347 coding
->dst_object
= Qnil
;
8351 = make_unibyte_string ((char *) coding
->destination
,
8353 xfree (coding
->destination
);
8359 /* This is the case of:
8360 (BUFFERP (src_object) && EQ (src_object, dst_object))
8361 As we have moved PT while replacing the original buffer
8362 contents, we must recover it now. */
8363 set_buffer_internal (XBUFFER (src_object
));
8364 if (saved_pt
< from
)
8365 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8366 else if (saved_pt
< from
+ chars
)
8367 TEMP_SET_PT_BOTH (from
, from_byte
);
8368 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8369 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8370 saved_pt_byte
+ (coding
->produced
- bytes
));
8372 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8373 saved_pt_byte
+ (coding
->produced
- bytes
));
8375 if (need_marker_adjustment
)
8377 struct Lisp_Marker
*tail
;
8379 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8380 if (tail
->need_adjustment
)
8382 tail
->need_adjustment
= 0;
8383 if (tail
->insertion_type
)
8385 tail
->bytepos
= from_byte
;
8386 tail
->charpos
= from
;
8390 tail
->bytepos
= from_byte
+ coding
->produced
;
8392 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8393 ? tail
->bytepos
: from
+ coding
->produced_char
);
8399 if (kill_src_buffer
)
8400 Fkill_buffer (coding
->src_object
);
8402 Vdeactivate_mark
= old_deactivate_mark
;
8403 unbind_to (count
, Qnil
);
8408 preferred_coding_system (void)
8410 int id
= coding_categories
[coding_priorities
[0]].id
;
8412 return CODING_ID_NAME (id
);
8415 #if defined (WINDOWSNT) || defined (CYGWIN)
8418 from_unicode (Lisp_Object str
)
8421 if (!STRING_MULTIBYTE (str
) &&
8424 str
= Fsubstring (str
, make_number (0), make_number (-1));
8427 return code_convert_string_norecord (str
, Qutf_16le
, 0);
8431 from_unicode_buffer (const wchar_t *wstr
)
8433 return from_unicode (
8434 make_unibyte_string (
8436 /* we get one of the two final 0 bytes for free. */
8437 1 + sizeof (wchar_t) * wcslen (wstr
)));
8441 to_unicode (Lisp_Object str
, Lisp_Object
*buf
)
8443 *buf
= code_convert_string_norecord (str
, Qutf_16le
, 1);
8444 /* We need to make another copy (in addition to the one made by
8445 code_convert_string_norecord) to ensure that the final string is
8446 _doubly_ zero terminated --- that is, that the string is
8447 terminated by two zero bytes and one utf-16le null character.
8448 Because strings are already terminated with a single zero byte,
8449 we just add one additional zero. */
8450 str
= make_uninit_string (SBYTES (*buf
) + 1);
8451 memcpy (SDATA (str
), SDATA (*buf
), SBYTES (*buf
));
8452 SDATA (str
) [SBYTES (*buf
)] = '\0';
8454 return WCSDATA (*buf
);
8457 #endif /* WINDOWSNT || CYGWIN */
8461 /*** 8. Emacs Lisp library functions ***/
8463 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8464 doc
: /* Return t if OBJECT is nil or a coding-system.
8465 See the documentation of `define-coding-system' for information
8466 about coding-system objects. */)
8467 (Lisp_Object object
)
8470 || CODING_SYSTEM_ID (object
) >= 0)
8472 if (! SYMBOLP (object
)
8473 || NILP (Fget (object
, Qcoding_system_define_form
)))
8478 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8479 Sread_non_nil_coding_system
, 1, 1, 0,
8480 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8481 (Lisp_Object prompt
)
8486 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8487 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8489 while (SCHARS (val
) == 0);
8490 return (Fintern (val
, Qnil
));
8493 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8494 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8495 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8496 Ignores case when completing coding systems (all Emacs coding systems
8497 are lower-case). */)
8498 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8501 ptrdiff_t count
= SPECPDL_INDEX ();
8503 if (SYMBOLP (default_coding_system
))
8504 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8505 specbind (Qcompletion_ignore_case
, Qt
);
8506 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8507 Qt
, Qnil
, Qcoding_system_history
,
8508 default_coding_system
, Qnil
);
8509 unbind_to (count
, Qnil
);
8510 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8513 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8515 doc
: /* Check validity of CODING-SYSTEM.
8516 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8517 It is valid if it is nil or a symbol defined as a coding system by the
8518 function `define-coding-system'. */)
8519 (Lisp_Object coding_system
)
8521 Lisp_Object define_form
;
8523 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8524 if (! NILP (define_form
))
8526 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8527 safe_eval (define_form
);
8529 if (!NILP (Fcoding_system_p (coding_system
)))
8530 return coding_system
;
8531 xsignal1 (Qcoding_system_error
, coding_system
);
8535 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8536 HIGHEST, return the coding system of the highest
8537 priority among the detected coding systems. Otherwise return a
8538 list of detected coding systems sorted by their priorities. If
8539 MULTIBYTEP, it is assumed that the bytes are in correct
8540 multibyte form but contains only ASCII and eight-bit chars.
8541 Otherwise, the bytes are raw bytes.
8543 CODING-SYSTEM controls the detection as below:
8545 If it is nil, detect both text-format and eol-format. If the
8546 text-format part of CODING-SYSTEM is already specified
8547 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8548 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8549 detect only text-format. */
8552 detect_coding_system (const unsigned char *src
,
8553 ptrdiff_t src_chars
, ptrdiff_t src_bytes
,
8554 bool highest
, bool multibytep
,
8555 Lisp_Object coding_system
)
8557 const unsigned char *src_end
= src
+ src_bytes
;
8558 Lisp_Object attrs
, eol_type
;
8559 Lisp_Object val
= Qnil
;
8560 struct coding_system coding
;
8562 struct coding_detection_info detect_info
;
8563 enum coding_category base_category
;
8564 bool null_byte_found
= 0, eight_bit_found
= 0;
8566 if (NILP (coding_system
))
8567 coding_system
= Qundecided
;
8568 setup_coding_system (coding_system
, &coding
);
8569 attrs
= CODING_ID_ATTRS (coding
.id
);
8570 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8571 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8573 coding
.source
= src
;
8574 coding
.src_chars
= src_chars
;
8575 coding
.src_bytes
= src_bytes
;
8576 coding
.src_multibyte
= multibytep
;
8577 coding
.consumed
= 0;
8578 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8579 coding
.head_ascii
= 0;
8581 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8583 /* At first, detect text-format if necessary. */
8584 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8585 if (base_category
== coding_category_undecided
)
8587 enum coding_category category
IF_LINT (= 0);
8588 struct coding_system
*this IF_LINT (= NULL
);
8590 bool inhibit_nbd
= inhibit_flag (coding
.spec
.undecided
.inhibit_nbd
,
8591 inhibit_null_byte_detection
);
8592 bool inhibit_ied
= inhibit_flag (coding
.spec
.undecided
.inhibit_ied
,
8593 inhibit_iso_escape_detection
);
8594 bool prefer_utf_8
= coding
.spec
.undecided
.prefer_utf_8
;
8596 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8597 for (; src
< src_end
; src
++)
8602 eight_bit_found
= 1;
8603 if (null_byte_found
)
8608 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8610 && ! detect_info
.checked
)
8612 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8614 /* We have scanned the whole data. */
8615 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8617 /* We didn't find an 8-bit code. We may
8618 have found a null-byte, but it's very
8619 rare that a binary file confirm to
8622 coding
.head_ascii
= src
- coding
.source
;
8624 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8628 else if (! c
&& !inhibit_nbd
)
8630 null_byte_found
= 1;
8631 if (eight_bit_found
)
8634 if (! eight_bit_found
)
8635 coding
.head_ascii
++;
8637 else if (! eight_bit_found
)
8638 coding
.head_ascii
++;
8641 if (null_byte_found
|| eight_bit_found
8642 || coding
.head_ascii
< coding
.src_bytes
8643 || detect_info
.found
)
8645 if (coding
.head_ascii
== coding
.src_bytes
)
8646 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8647 for (i
= 0; i
< coding_category_raw_text
; i
++)
8649 category
= coding_priorities
[i
];
8650 this = coding_categories
+ category
;
8651 if (detect_info
.found
& (1 << category
))
8656 if (null_byte_found
)
8658 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8659 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8661 else if (prefer_utf_8
8662 && detect_coding_utf_8 (&coding
, &detect_info
))
8664 detect_info
.checked
|= ~CATEGORY_MASK_UTF_8
;
8665 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_8
;
8667 for (i
= 0; i
< coding_category_raw_text
; i
++)
8669 category
= coding_priorities
[i
];
8670 this = coding_categories
+ category
;
8674 /* No coding system of this category is defined. */
8675 detect_info
.rejected
|= (1 << category
);
8677 else if (category
>= coding_category_raw_text
)
8679 else if (detect_info
.checked
& (1 << category
))
8682 && (detect_info
.found
& (1 << category
)))
8685 else if ((*(this->detector
)) (&coding
, &detect_info
)
8687 && (detect_info
.found
& (1 << category
)))
8689 if (category
== coding_category_utf_16_auto
)
8691 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8692 category
= coding_category_utf_16_le
;
8694 category
= coding_category_utf_16_be
;
8702 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8705 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8706 id
= CODING_SYSTEM_ID (Qno_conversion
);
8707 val
= list1 (make_number (id
));
8709 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8711 detect_info
.found
= CATEGORY_MASK_ANY
;
8712 id
= coding_categories
[coding_category_undecided
].id
;
8713 val
= list1 (make_number (id
));
8717 if (detect_info
.found
)
8719 detect_info
.found
= 1 << category
;
8720 val
= list1 (make_number (this->id
));
8723 for (i
= 0; i
< coding_category_raw_text
; i
++)
8724 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8726 detect_info
.found
= 1 << coding_priorities
[i
];
8727 id
= coding_categories
[coding_priorities
[i
]].id
;
8728 val
= list1 (make_number (id
));
8734 int mask
= detect_info
.rejected
| detect_info
.found
;
8737 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8739 category
= coding_priorities
[i
];
8740 if (! (mask
& (1 << category
)))
8742 found
|= 1 << category
;
8743 id
= coding_categories
[category
].id
;
8745 val
= list1 (make_number (id
));
8748 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8750 category
= coding_priorities
[i
];
8751 if (detect_info
.found
& (1 << category
))
8753 id
= coding_categories
[category
].id
;
8754 val
= Fcons (make_number (id
), val
);
8757 detect_info
.found
|= found
;
8760 else if (base_category
== coding_category_utf_8_auto
)
8762 if (detect_coding_utf_8 (&coding
, &detect_info
))
8764 struct coding_system
*this;
8766 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8767 this = coding_categories
+ coding_category_utf_8_sig
;
8769 this = coding_categories
+ coding_category_utf_8_nosig
;
8770 val
= list1 (make_number (this->id
));
8773 else if (base_category
== coding_category_utf_16_auto
)
8775 if (detect_coding_utf_16 (&coding
, &detect_info
))
8777 struct coding_system
*this;
8779 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8780 this = coding_categories
+ coding_category_utf_16_le
;
8781 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8782 this = coding_categories
+ coding_category_utf_16_be
;
8783 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8784 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8786 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8787 val
= list1 (make_number (this->id
));
8792 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8793 val
= list1 (make_number (coding
.id
));
8796 /* Then, detect eol-format if necessary. */
8798 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8801 if (VECTORP (eol_type
))
8803 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8805 if (null_byte_found
)
8806 normal_eol
= EOL_SEEN_LF
;
8808 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8809 coding_category_raw_text
);
8811 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8812 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8813 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8814 coding_category_utf_16_be
);
8815 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8816 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8817 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8818 coding_category_utf_16_le
);
8822 if (EQ (eol_type
, Qunix
))
8823 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8824 else if (EQ (eol_type
, Qdos
))
8825 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8827 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8830 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8832 enum coding_category category
;
8835 id
= XINT (XCAR (tail
));
8836 attrs
= CODING_ID_ATTRS (id
);
8837 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8838 eol_type
= CODING_ID_EOL_TYPE (id
);
8839 if (VECTORP (eol_type
))
8841 if (category
== coding_category_utf_16_be
8842 || category
== coding_category_utf_16_be_nosig
)
8843 this_eol
= utf_16_be_eol
;
8844 else if (category
== coding_category_utf_16_le
8845 || category
== coding_category_utf_16_le_nosig
)
8846 this_eol
= utf_16_le_eol
;
8848 this_eol
= normal_eol
;
8850 if (this_eol
== EOL_SEEN_LF
)
8851 XSETCAR (tail
, AREF (eol_type
, 0));
8852 else if (this_eol
== EOL_SEEN_CRLF
)
8853 XSETCAR (tail
, AREF (eol_type
, 1));
8854 else if (this_eol
== EOL_SEEN_CR
)
8855 XSETCAR (tail
, AREF (eol_type
, 2));
8857 XSETCAR (tail
, CODING_ID_NAME (id
));
8860 XSETCAR (tail
, CODING_ID_NAME (id
));
8864 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8868 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8870 doc
: /* Detect coding system of the text in the region between START and END.
8871 Return a list of possible coding systems ordered by priority.
8872 The coding systems to try and their priorities follows what
8873 the function `coding-system-priority-list' (which see) returns.
8875 If only ASCII characters are found (except for such ISO-2022 control
8876 characters as ESC), it returns a list of single element `undecided'
8877 or its subsidiary coding system according to a detected end-of-line
8880 If optional argument HIGHEST is non-nil, return the coding system of
8881 highest priority. */)
8882 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8885 ptrdiff_t from_byte
, to_byte
;
8887 validate_region (&start
, &end
);
8888 from
= XINT (start
), to
= XINT (end
);
8889 from_byte
= CHAR_TO_BYTE (from
);
8890 to_byte
= CHAR_TO_BYTE (to
);
8892 if (from
< GPT
&& to
>= GPT
)
8893 move_gap_both (to
, to_byte
);
8895 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8896 to
- from
, to_byte
- from_byte
,
8898 !NILP (BVAR (current_buffer
8899 , enable_multibyte_characters
)),
8903 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8905 doc
: /* Detect coding system of the text in STRING.
8906 Return a list of possible coding systems ordered by priority.
8907 The coding systems to try and their priorities follows what
8908 the function `coding-system-priority-list' (which see) returns.
8910 If only ASCII characters are found (except for such ISO-2022 control
8911 characters as ESC), it returns a list of single element `undecided'
8912 or its subsidiary coding system according to a detected end-of-line
8915 If optional argument HIGHEST is non-nil, return the coding system of
8916 highest priority. */)
8917 (Lisp_Object string
, Lisp_Object highest
)
8919 CHECK_STRING (string
);
8921 return detect_coding_system (SDATA (string
),
8922 SCHARS (string
), SBYTES (string
),
8923 !NILP (highest
), STRING_MULTIBYTE (string
),
8929 char_encodable_p (int c
, Lisp_Object attrs
)
8932 struct charset
*charset
;
8933 Lisp_Object translation_table
;
8935 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8936 if (! NILP (translation_table
))
8937 c
= translate_char (translation_table
, c
);
8938 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8939 CONSP (tail
); tail
= XCDR (tail
))
8941 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8942 if (CHAR_CHARSET_P (c
, charset
))
8945 return (! NILP (tail
));
8949 /* Return a list of coding systems that safely encode the text between
8950 START and END. If EXCLUDE is non-nil, it is a list of coding
8951 systems not to check. The returned list doesn't contain any such
8952 coding systems. In any case, if the text contains only ASCII or is
8953 unibyte, return t. */
8955 DEFUN ("find-coding-systems-region-internal",
8956 Ffind_coding_systems_region_internal
,
8957 Sfind_coding_systems_region_internal
, 2, 3, 0,
8958 doc
: /* Internal use only. */)
8959 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8961 Lisp_Object coding_attrs_list
, safe_codings
;
8962 ptrdiff_t start_byte
, end_byte
;
8963 const unsigned char *p
, *pbeg
, *pend
;
8965 Lisp_Object tail
, elt
, work_table
;
8967 if (STRINGP (start
))
8969 if (!STRING_MULTIBYTE (start
)
8970 || SCHARS (start
) == SBYTES (start
))
8973 end_byte
= SBYTES (start
);
8977 CHECK_NUMBER_COERCE_MARKER (start
);
8978 CHECK_NUMBER_COERCE_MARKER (end
);
8979 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8980 args_out_of_range (start
, end
);
8981 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8983 start_byte
= CHAR_TO_BYTE (XINT (start
));
8984 end_byte
= CHAR_TO_BYTE (XINT (end
));
8985 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8988 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8990 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8991 move_gap_both (XINT (start
), start_byte
);
8993 move_gap_both (XINT (end
), end_byte
);
8997 coding_attrs_list
= Qnil
;
8998 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
9000 || NILP (Fmemq (XCAR (tail
), exclude
)))
9004 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
9005 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
)))
9007 ASET (attrs
, coding_attr_trans_tbl
,
9008 get_translation_table (attrs
, 1, NULL
));
9009 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
9013 if (STRINGP (start
))
9014 p
= pbeg
= SDATA (start
);
9016 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
9017 pend
= p
+ (end_byte
- start_byte
);
9019 while (p
< pend
&& ASCII_CHAR_P (*p
)) p
++;
9020 while (p
< pend
&& ASCII_CHAR_P (*(pend
- 1))) pend
--;
9022 work_table
= Fmake_char_table (Qnil
, Qnil
);
9025 if (ASCII_CHAR_P (*p
))
9029 c
= STRING_CHAR_ADVANCE (p
);
9030 if (!NILP (char_table_ref (work_table
, c
)))
9031 /* This character was already checked. Ignore it. */
9034 charset_map_loaded
= 0;
9035 for (tail
= coding_attrs_list
; CONSP (tail
);)
9040 else if (char_encodable_p (c
, elt
))
9042 else if (CONSP (XCDR (tail
)))
9044 XSETCAR (tail
, XCAR (XCDR (tail
)));
9045 XSETCDR (tail
, XCDR (XCDR (tail
)));
9049 XSETCAR (tail
, Qnil
);
9053 if (charset_map_loaded
)
9055 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
9057 if (STRINGP (start
))
9058 pbeg
= SDATA (start
);
9060 pbeg
= BYTE_POS_ADDR (start_byte
);
9061 p
= pbeg
+ p_offset
;
9062 pend
= pbeg
+ pend_offset
;
9064 char_table_set (work_table
, c
, Qt
);
9068 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
9069 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
9070 if (! NILP (XCAR (tail
)))
9071 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
9073 return safe_codings
;
9077 DEFUN ("unencodable-char-position", Funencodable_char_position
,
9078 Sunencodable_char_position
, 3, 5, 0,
9079 doc
: /* Return position of first un-encodable character in a region.
9080 START and END specify the region and CODING-SYSTEM specifies the
9081 encoding to check. Return nil if CODING-SYSTEM does encode the region.
9083 If optional 4th argument COUNT is non-nil, it specifies at most how
9084 many un-encodable characters to search. In this case, the value is a
9087 If optional 5th argument STRING is non-nil, it is a string to search
9088 for un-encodable characters. In that case, START and END are indexes
9089 to the string and treated as in `substring'. */)
9090 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
,
9091 Lisp_Object count
, Lisp_Object string
)
9094 struct coding_system coding
;
9095 Lisp_Object attrs
, charset_list
, translation_table
;
9096 Lisp_Object positions
;
9098 const unsigned char *p
, *stop
, *pend
;
9099 bool ascii_compatible
;
9101 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
9102 attrs
= CODING_ID_ATTRS (coding
.id
);
9103 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
9105 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
9106 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9107 translation_table
= get_translation_table (attrs
, 1, NULL
);
9111 validate_region (&start
, &end
);
9112 from
= XINT (start
);
9114 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
9115 || (ascii_compatible
9116 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
9118 p
= CHAR_POS_ADDR (from
);
9119 pend
= CHAR_POS_ADDR (to
);
9120 if (from
< GPT
&& to
>= GPT
)
9127 CHECK_STRING (string
);
9128 validate_subarray (string
, start
, end
, SCHARS (string
), &from
, &to
);
9129 if (! STRING_MULTIBYTE (string
))
9131 p
= SDATA (string
) + string_char_to_byte (string
, from
);
9132 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
9133 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
9141 CHECK_NATNUM (count
);
9146 charset_map_loaded
= 0;
9151 if (ascii_compatible
)
9152 while (p
< stop
&& ASCII_CHAR_P (*p
))
9162 c
= STRING_CHAR_ADVANCE (p
);
9163 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
9164 && ! char_charset (translate_char (translation_table
, c
),
9165 charset_list
, NULL
))
9167 positions
= Fcons (make_number (from
), positions
);
9174 if (charset_map_loaded
&& NILP (string
))
9176 p
= CHAR_POS_ADDR (from
);
9177 pend
= CHAR_POS_ADDR (to
);
9178 if (from
< GPT
&& to
>= GPT
)
9182 charset_map_loaded
= 0;
9186 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
9190 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
9191 Scheck_coding_systems_region
, 3, 3, 0,
9192 doc
: /* Check if the region is encodable by coding systems.
9194 START and END are buffer positions specifying the region.
9195 CODING-SYSTEM-LIST is a list of coding systems to check.
9197 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
9198 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
9199 whole region, POS0, POS1, ... are buffer positions where non-encodable
9200 characters are found.
9202 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
9205 START may be a string. In that case, check if the string is
9206 encodable, and the value contains indices to the string instead of
9207 buffer positions. END is ignored.
9209 If the current buffer (or START if it is a string) is unibyte, the value
9211 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
9214 ptrdiff_t start_byte
, end_byte
;
9216 const unsigned char *p
, *pbeg
, *pend
;
9218 Lisp_Object tail
, elt
, attrs
;
9220 if (STRINGP (start
))
9222 if (!STRING_MULTIBYTE (start
)
9223 || SCHARS (start
) == SBYTES (start
))
9226 end_byte
= SBYTES (start
);
9231 CHECK_NUMBER_COERCE_MARKER (start
);
9232 CHECK_NUMBER_COERCE_MARKER (end
);
9233 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
9234 args_out_of_range (start
, end
);
9235 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
9237 start_byte
= CHAR_TO_BYTE (XINT (start
));
9238 end_byte
= CHAR_TO_BYTE (XINT (end
));
9239 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
9242 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
9244 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
9245 move_gap_both (XINT (start
), start_byte
);
9247 move_gap_both (XINT (end
), end_byte
);
9253 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
9256 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
9257 ASET (attrs
, coding_attr_trans_tbl
,
9258 get_translation_table (attrs
, 1, NULL
));
9259 list
= Fcons (list2 (elt
, attrs
), list
);
9262 if (STRINGP (start
))
9263 p
= pbeg
= SDATA (start
);
9265 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
9266 pend
= p
+ (end_byte
- start_byte
);
9268 while (p
< pend
&& ASCII_CHAR_P (*p
)) p
++, pos
++;
9269 while (p
< pend
&& ASCII_CHAR_P (*(pend
- 1))) pend
--;
9273 if (ASCII_CHAR_P (*p
))
9277 c
= STRING_CHAR_ADVANCE (p
);
9279 charset_map_loaded
= 0;
9280 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
9282 elt
= XCDR (XCAR (tail
));
9283 if (! char_encodable_p (c
, XCAR (elt
)))
9284 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
9286 if (charset_map_loaded
)
9288 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
9290 if (STRINGP (start
))
9291 pbeg
= SDATA (start
);
9293 pbeg
= BYTE_POS_ADDR (start_byte
);
9294 p
= pbeg
+ p_offset
;
9295 pend
= pbeg
+ pend_offset
;
9303 for (; CONSP (tail
); tail
= XCDR (tail
))
9306 if (CONSP (XCDR (XCDR (elt
))))
9307 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
9316 code_convert_region (Lisp_Object start
, Lisp_Object end
,
9317 Lisp_Object coding_system
, Lisp_Object dst_object
,
9318 bool encodep
, bool norecord
)
9320 struct coding_system coding
;
9321 ptrdiff_t from
, from_byte
, to
, to_byte
;
9322 Lisp_Object src_object
;
9324 if (NILP (coding_system
))
9325 coding_system
= Qno_conversion
;
9327 CHECK_CODING_SYSTEM (coding_system
);
9328 src_object
= Fcurrent_buffer ();
9329 if (NILP (dst_object
))
9330 dst_object
= src_object
;
9331 else if (! EQ (dst_object
, Qt
))
9332 CHECK_BUFFER (dst_object
);
9334 validate_region (&start
, &end
);
9335 from
= XFASTINT (start
);
9336 from_byte
= CHAR_TO_BYTE (from
);
9337 to
= XFASTINT (end
);
9338 to_byte
= CHAR_TO_BYTE (to
);
9340 setup_coding_system (coding_system
, &coding
);
9341 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9343 if (BUFFERP (dst_object
) && !EQ (dst_object
, src_object
))
9345 struct buffer
*buf
= XBUFFER (dst_object
);
9346 ptrdiff_t buf_pt
= BUF_PT (buf
);
9348 invalidate_buffer_caches (buf
, buf_pt
, buf_pt
);
9352 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
9355 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
9358 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9360 return (BUFFERP (dst_object
)
9361 ? make_number (coding
.produced_char
)
9362 : coding
.dst_object
);
9366 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
9367 3, 4, "r\nzCoding system: ",
9368 doc
: /* Decode the current region from the specified coding system.
9369 When called from a program, takes four arguments:
9370 START, END, CODING-SYSTEM, and DESTINATION.
9371 START and END are buffer positions.
9373 Optional 4th arguments DESTINATION specifies where the decoded text goes.
9374 If nil, the region between START and END is replaced by the decoded text.
9375 If buffer, the decoded text is inserted in that buffer after point (point
9377 In those cases, the length of the decoded text is returned.
9378 If DESTINATION is t, the decoded text is returned.
9380 This function sets `last-coding-system-used' to the precise coding system
9381 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9382 not fully specified.) */)
9383 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
9385 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
9388 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
9389 3, 4, "r\nzCoding system: ",
9390 doc
: /* Encode the current region by 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 encoded text goes.
9396 If nil, the region between START and END is replace by the encoded text.
9397 If buffer, the encoded text is inserted in that buffer after point (point
9399 In those cases, the length of the encoded text is returned.
9400 If DESTINATION is t, the encoded 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
, 1, 0);
9411 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
9412 Lisp_Object dst_object
, bool encodep
, bool nocopy
,
9415 struct coding_system coding
;
9416 ptrdiff_t chars
, bytes
;
9418 CHECK_STRING (string
);
9419 if (NILP (coding_system
))
9422 Vlast_coding_system_used
= Qno_conversion
;
9423 if (NILP (dst_object
))
9424 return (nocopy
? Fcopy_sequence (string
) : string
);
9427 if (NILP (coding_system
))
9428 coding_system
= Qno_conversion
;
9430 CHECK_CODING_SYSTEM (coding_system
);
9431 if (NILP (dst_object
))
9433 else if (! EQ (dst_object
, Qt
))
9434 CHECK_BUFFER (dst_object
);
9436 setup_coding_system (coding_system
, &coding
);
9437 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9438 chars
= SCHARS (string
);
9439 bytes
= SBYTES (string
);
9441 if (BUFFERP (dst_object
))
9443 struct buffer
*buf
= XBUFFER (dst_object
);
9444 ptrdiff_t buf_pt
= BUF_PT (buf
);
9446 invalidate_buffer_caches (buf
, buf_pt
, buf_pt
);
9450 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9452 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9454 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9456 return (BUFFERP (dst_object
)
9457 ? make_number (coding
.produced_char
)
9458 : coding
.dst_object
);
9462 /* Encode or decode STRING according to CODING_SYSTEM.
9463 Do not set Vlast_coding_system_used.
9465 This function is called only from macros DECODE_FILE and
9466 ENCODE_FILE, thus we ignore character composition. */
9469 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9472 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9475 /* Encode or decode a file name, to or from a unibyte string suitable
9476 for passing to C library functions. */
9478 decode_file_name (Lisp_Object fname
)
9481 /* The w32 build pretends to use UTF-8 for file-name encoding, and
9482 converts the file names either to UTF-16LE or to the system ANSI
9483 codepage internally, depending on the underlying OS; see w32.c. */
9484 if (! NILP (Fcoding_system_p (Qutf_8
)))
9485 return code_convert_string_norecord (fname
, Qutf_8
, 0);
9487 #else /* !WINDOWSNT */
9488 if (! NILP (Vfile_name_coding_system
))
9489 return code_convert_string_norecord (fname
, Vfile_name_coding_system
, 0);
9490 else if (! NILP (Vdefault_file_name_coding_system
))
9491 return code_convert_string_norecord (fname
,
9492 Vdefault_file_name_coding_system
, 0);
9499 encode_file_name (Lisp_Object fname
)
9501 /* This is especially important during bootstrap and dumping, when
9502 file-name encoding is not yet known, and therefore any non-ASCII
9503 file names are unibyte strings, and could only be thrashed if we
9504 try to encode them. */
9505 if (!STRING_MULTIBYTE (fname
))
9508 /* The w32 build pretends to use UTF-8 for file-name encoding, and
9509 converts the file names either to UTF-16LE or to the system ANSI
9510 codepage internally, depending on the underlying OS; see w32.c. */
9511 if (! NILP (Fcoding_system_p (Qutf_8
)))
9512 return code_convert_string_norecord (fname
, Qutf_8
, 1);
9514 #else /* !WINDOWSNT */
9515 if (! NILP (Vfile_name_coding_system
))
9516 return code_convert_string_norecord (fname
, Vfile_name_coding_system
, 1);
9517 else if (! NILP (Vdefault_file_name_coding_system
))
9518 return code_convert_string_norecord (fname
,
9519 Vdefault_file_name_coding_system
, 1);
9525 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9527 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9529 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9530 if the decoding operation is trivial.
9532 Optional fourth arg BUFFER non-nil means that the decoded text is
9533 inserted in that buffer after point (point does not move). In this
9534 case, the return value is the length of the decoded text.
9536 This function sets `last-coding-system-used' to the precise coding system
9537 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9538 not fully specified.) */)
9539 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9541 return code_convert_string (string
, coding_system
, buffer
,
9542 0, ! NILP (nocopy
), 0);
9545 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9547 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9549 Optional third arg NOCOPY non-nil means it is OK to return STRING
9550 itself if the encoding operation is trivial.
9552 Optional fourth arg BUFFER non-nil means that the encoded text is
9553 inserted in that buffer after point (point does not move). In this
9554 case, the return value is the length of the encoded text.
9556 This function sets `last-coding-system-used' to the precise coding system
9557 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9558 not fully specified.) */)
9559 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9561 return code_convert_string (string
, coding_system
, buffer
,
9562 1, ! NILP (nocopy
), 0);
9566 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9567 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9568 Return the corresponding character. */)
9571 Lisp_Object spec
, attrs
, val
;
9572 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9576 CHECK_NATNUM (code
);
9577 ch
= XFASTINT (code
);
9578 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9579 attrs
= AREF (spec
, 0);
9581 if (ASCII_CHAR_P (ch
)
9582 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9585 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9586 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9587 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9588 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9593 charset
= charset_roman
;
9595 else if (ch
>= 0xA0 && ch
< 0xDF)
9598 charset
= charset_kana
;
9602 EMACS_INT c1
= ch
>> 8;
9605 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9606 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9607 error ("Invalid code: %"pI
"d", ch
);
9610 charset
= charset_kanji
;
9612 c
= DECODE_CHAR (charset
, c
);
9614 error ("Invalid code: %"pI
"d", ch
);
9615 return make_number (c
);
9619 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9620 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9621 Return the corresponding code in SJIS. */)
9624 Lisp_Object spec
, attrs
, charset_list
;
9626 struct charset
*charset
;
9629 CHECK_CHARACTER (ch
);
9631 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9632 attrs
= AREF (spec
, 0);
9634 if (ASCII_CHAR_P (c
)
9635 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9638 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9639 charset
= char_charset (c
, charset_list
, &code
);
9640 if (code
== CHARSET_INVALID_CODE (charset
))
9641 error ("Can't encode by shift_jis encoding: %c", c
);
9644 return make_number (code
);
9647 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9648 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9649 Return the corresponding character. */)
9652 Lisp_Object spec
, attrs
, val
;
9653 struct charset
*charset_roman
, *charset_big5
, *charset
;
9657 CHECK_NATNUM (code
);
9658 ch
= XFASTINT (code
);
9659 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9660 attrs
= AREF (spec
, 0);
9662 if (ASCII_CHAR_P (ch
)
9663 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9666 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9667 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9668 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9673 charset
= charset_roman
;
9677 EMACS_INT b1
= ch
>> 8;
9679 if (b1
< 0xA1 || b1
> 0xFE
9680 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9681 error ("Invalid code: %"pI
"d", ch
);
9683 charset
= charset_big5
;
9685 c
= DECODE_CHAR (charset
, c
);
9687 error ("Invalid code: %"pI
"d", ch
);
9688 return make_number (c
);
9691 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9692 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9693 Return the corresponding character code in Big5. */)
9696 Lisp_Object spec
, attrs
, charset_list
;
9697 struct charset
*charset
;
9701 CHECK_CHARACTER (ch
);
9703 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9704 attrs
= AREF (spec
, 0);
9705 if (ASCII_CHAR_P (c
)
9706 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9709 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9710 charset
= char_charset (c
, charset_list
, &code
);
9711 if (code
== CHARSET_INVALID_CODE (charset
))
9712 error ("Can't encode by Big5 encoding: %c", c
);
9714 return make_number (code
);
9718 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9719 Sset_terminal_coding_system_internal
, 1, 2, 0,
9720 doc
: /* Internal use only. */)
9721 (Lisp_Object coding_system
, Lisp_Object terminal
)
9723 struct terminal
*term
= decode_live_terminal (terminal
);
9724 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9725 CHECK_SYMBOL (coding_system
);
9726 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9727 /* We had better not send unsafe characters to terminal. */
9728 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9729 /* Character composition should be disabled. */
9730 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9731 terminal_coding
->src_multibyte
= 1;
9732 terminal_coding
->dst_multibyte
= 0;
9734 (term
, (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
9735 ? coding_charset_list (terminal_coding
)
9736 : list1 (make_number (charset_ascii
))));
9740 DEFUN ("set-safe-terminal-coding-system-internal",
9741 Fset_safe_terminal_coding_system_internal
,
9742 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9743 doc
: /* Internal use only. */)
9744 (Lisp_Object coding_system
)
9746 CHECK_SYMBOL (coding_system
);
9747 setup_coding_system (Fcheck_coding_system (coding_system
),
9748 &safe_terminal_coding
);
9749 /* Character composition should be disabled. */
9750 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9751 safe_terminal_coding
.src_multibyte
= 1;
9752 safe_terminal_coding
.dst_multibyte
= 0;
9756 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9757 Sterminal_coding_system
, 0, 1, 0,
9758 doc
: /* Return coding system specified for terminal output on the given terminal.
9759 TERMINAL may be a terminal object, a frame, or nil for the selected
9760 frame's terminal device. */)
9761 (Lisp_Object terminal
)
9763 struct coding_system
*terminal_coding
9764 = TERMINAL_TERMINAL_CODING (decode_live_terminal (terminal
));
9765 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9767 /* For backward compatibility, return nil if it is `undecided'. */
9768 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9771 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9772 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9773 doc
: /* Internal use only. */)
9774 (Lisp_Object coding_system
, Lisp_Object terminal
)
9776 struct terminal
*t
= decode_live_terminal (terminal
);
9777 CHECK_SYMBOL (coding_system
);
9778 if (NILP (coding_system
))
9779 coding_system
= Qno_conversion
;
9781 Fcheck_coding_system (coding_system
);
9782 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9783 /* Character composition should be disabled. */
9784 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9785 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9789 DEFUN ("keyboard-coding-system",
9790 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9791 doc
: /* Return coding system specified for decoding keyboard input. */)
9792 (Lisp_Object terminal
)
9794 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9795 (decode_live_terminal (terminal
))->id
);
9799 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9800 Sfind_operation_coding_system
, 1, MANY
, 0,
9801 doc
: /* Choose a coding system for an operation based on the target name.
9802 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9803 DECODING-SYSTEM is the coding system to use for decoding
9804 (in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9805 for encoding (in case OPERATION does encoding).
9807 The first argument OPERATION specifies an I/O primitive:
9808 For file I/O, `insert-file-contents' or `write-region'.
9809 For process I/O, `call-process', `call-process-region', or `start-process'.
9810 For network I/O, `open-network-stream'.
9812 The remaining arguments should be the same arguments that were passed
9813 to the primitive. Depending on which primitive, one of those arguments
9814 is selected as the TARGET. For example, if OPERATION does file I/O,
9815 whichever argument specifies the file name is TARGET.
9817 TARGET has a meaning which depends on OPERATION:
9818 For file I/O, TARGET is a file name (except for the special case below).
9819 For process I/O, TARGET is a process name.
9820 For network I/O, TARGET is a service name or a port number.
9822 This function looks up what is specified for TARGET in
9823 `file-coding-system-alist', `process-coding-system-alist',
9824 or `network-coding-system-alist' depending on OPERATION.
9825 They may specify a coding system, a cons of coding systems,
9826 or a function symbol to call.
9827 In the last case, we call the function with one argument,
9828 which is a list of all the arguments given to this function.
9829 If the function can't decide a coding system, it can return
9830 `undecided' so that the normal code-detection is performed.
9832 If OPERATION is `insert-file-contents', the argument corresponding to
9833 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9834 file name to look up, and BUFFER is a buffer that contains the file's
9835 contents (not yet decoded). If `file-coding-system-alist' specifies a
9836 function to call for FILENAME, that function should examine the
9837 contents of BUFFER instead of reading the file.
9839 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9840 (ptrdiff_t nargs
, Lisp_Object
*args
)
9842 Lisp_Object operation
, target_idx
, target
, val
;
9843 register Lisp_Object chain
;
9846 error ("Too few arguments");
9847 operation
= args
[0];
9848 if (!SYMBOLP (operation
)
9849 || (target_idx
= Fget (operation
, Qtarget_idx
), !NATNUMP (target_idx
)))
9850 error ("Invalid first argument");
9851 if (nargs
<= 1 + XFASTINT (target_idx
))
9852 error ("Too few arguments for operation `%s'",
9853 SDATA (SYMBOL_NAME (operation
)));
9854 target
= args
[XFASTINT (target_idx
) + 1];
9855 if (!(STRINGP (target
)
9856 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9857 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9858 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9859 error ("Invalid argument %"pI
"d of operation `%s'",
9860 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9862 target
= XCAR (target
);
9864 chain
= ((EQ (operation
, Qinsert_file_contents
)
9865 || EQ (operation
, Qwrite_region
))
9866 ? Vfile_coding_system_alist
9867 : (EQ (operation
, Qopen_network_stream
)
9868 ? Vnetwork_coding_system_alist
9869 : Vprocess_coding_system_alist
));
9873 for (; CONSP (chain
); chain
= XCDR (chain
))
9879 && ((STRINGP (target
)
9880 && STRINGP (XCAR (elt
))
9881 && fast_string_match (XCAR (elt
), target
) >= 0)
9882 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9885 /* Here, if VAL is both a valid coding system and a valid
9886 function symbol, we return VAL as a coding system. */
9889 if (! SYMBOLP (val
))
9891 if (! NILP (Fcoding_system_p (val
)))
9892 return Fcons (val
, val
);
9893 if (! NILP (Ffboundp (val
)))
9895 /* We use call1 rather than safe_call1
9896 so as to get bug reports about functions called here
9897 which don't handle the current interface. */
9898 val
= call1 (val
, Flist (nargs
, args
));
9901 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9902 return Fcons (val
, val
);
9910 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9911 Sset_coding_system_priority
, 0, MANY
, 0,
9912 doc
: /* Assign higher priority to the coding systems given as arguments.
9913 If multiple coding systems belong to the same category,
9914 all but the first one are ignored.
9916 usage: (set-coding-system-priority &rest coding-systems) */)
9917 (ptrdiff_t nargs
, Lisp_Object
*args
)
9920 bool changed
[coding_category_max
];
9921 enum coding_category priorities
[coding_category_max
];
9923 memset (changed
, 0, sizeof changed
);
9925 for (i
= j
= 0; i
< nargs
; i
++)
9927 enum coding_category category
;
9928 Lisp_Object spec
, attrs
;
9930 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9931 attrs
= AREF (spec
, 0);
9932 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9933 if (changed
[category
])
9934 /* Ignore this coding system because a coding system of the
9935 same category already had a higher priority. */
9937 changed
[category
] = 1;
9938 priorities
[j
++] = category
;
9939 if (coding_categories
[category
].id
>= 0
9940 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9941 setup_coding_system (args
[i
], &coding_categories
[category
]);
9942 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9945 /* Now we have decided top J priorities. Reflect the order of the
9946 original priorities to the remaining priorities. */
9948 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9950 while (j
< coding_category_max
9951 && changed
[coding_priorities
[j
]])
9953 if (j
== coding_category_max
)
9955 priorities
[i
] = coding_priorities
[j
];
9958 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9960 /* Update `coding-category-list'. */
9961 Vcoding_category_list
= Qnil
;
9962 for (i
= coding_category_max
; i
-- > 0; )
9963 Vcoding_category_list
9964 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9965 Vcoding_category_list
);
9970 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9971 Scoding_system_priority_list
, 0, 1, 0,
9972 doc
: /* Return a list of coding systems ordered by their priorities.
9973 The list contains a subset of coding systems; i.e. coding systems
9974 assigned to each coding category (see `coding-category-list').
9976 HIGHESTP non-nil means just return the highest priority one. */)
9977 (Lisp_Object highestp
)
9982 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9984 enum coding_category category
= coding_priorities
[i
];
9985 int id
= coding_categories
[category
].id
;
9990 attrs
= CODING_ID_ATTRS (id
);
9991 if (! NILP (highestp
))
9992 return CODING_ATTR_BASE_NAME (attrs
);
9993 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9995 return Fnreverse (val
);
9998 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
10001 make_subsidiaries (Lisp_Object base
)
10003 Lisp_Object subsidiaries
;
10004 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
10006 char *buf
= SAFE_ALLOCA (base_name_len
+ 6);
10009 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
10010 subsidiaries
= make_uninit_vector (3);
10011 for (i
= 0; i
< 3; i
++)
10013 strcpy (buf
+ base_name_len
, suffixes
[i
]);
10014 ASET (subsidiaries
, i
, intern (buf
));
10017 return subsidiaries
;
10021 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
10022 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
10023 doc
: /* For internal use only.
10024 usage: (define-coding-system-internal ...) */)
10025 (ptrdiff_t nargs
, Lisp_Object
*args
)
10028 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
10029 Lisp_Object attrs
; /* Vector of attributes. */
10030 Lisp_Object eol_type
;
10031 Lisp_Object aliases
;
10032 Lisp_Object coding_type
, charset_list
, safe_charsets
;
10033 enum coding_category category
;
10034 Lisp_Object tail
, val
;
10035 int max_charset_id
= 0;
10038 if (nargs
< coding_arg_max
)
10041 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
10043 name
= args
[coding_arg_name
];
10044 CHECK_SYMBOL (name
);
10045 ASET (attrs
, coding_attr_base_name
, name
);
10047 val
= args
[coding_arg_mnemonic
];
10048 if (! STRINGP (val
))
10049 CHECK_CHARACTER (val
);
10050 ASET (attrs
, coding_attr_mnemonic
, val
);
10052 coding_type
= args
[coding_arg_coding_type
];
10053 CHECK_SYMBOL (coding_type
);
10054 ASET (attrs
, coding_attr_type
, coding_type
);
10056 charset_list
= args
[coding_arg_charset_list
];
10057 if (SYMBOLP (charset_list
))
10059 if (EQ (charset_list
, Qiso_2022
))
10061 if (! EQ (coding_type
, Qiso_2022
))
10062 error ("Invalid charset-list");
10063 charset_list
= Viso_2022_charset_list
;
10065 else if (EQ (charset_list
, Qemacs_mule
))
10067 if (! EQ (coding_type
, Qemacs_mule
))
10068 error ("Invalid charset-list");
10069 charset_list
= Vemacs_mule_charset_list
;
10071 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10073 if (! RANGED_INTEGERP (0, XCAR (tail
), INT_MAX
- 1))
10074 error ("Invalid charset-list");
10075 if (max_charset_id
< XFASTINT (XCAR (tail
)))
10076 max_charset_id
= XFASTINT (XCAR (tail
));
10081 charset_list
= Fcopy_sequence (charset_list
);
10082 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10084 struct charset
*charset
;
10087 CHECK_CHARSET_GET_CHARSET (val
, charset
);
10088 if (EQ (coding_type
, Qiso_2022
)
10089 ? CHARSET_ISO_FINAL (charset
) < 0
10090 : EQ (coding_type
, Qemacs_mule
)
10091 ? CHARSET_EMACS_MULE_ID (charset
) < 0
10093 error ("Can't handle charset `%s'",
10094 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10096 XSETCAR (tail
, make_number (charset
->id
));
10097 if (max_charset_id
< charset
->id
)
10098 max_charset_id
= charset
->id
;
10101 ASET (attrs
, coding_attr_charset_list
, charset_list
);
10103 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
10104 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
10105 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10106 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
10107 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
10109 ASET (attrs
, coding_attr_ascii_compat
, args
[coding_arg_ascii_compatible_p
]);
10111 val
= args
[coding_arg_decode_translation_table
];
10112 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10113 CHECK_SYMBOL (val
);
10114 ASET (attrs
, coding_attr_decode_tbl
, val
);
10116 val
= args
[coding_arg_encode_translation_table
];
10117 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10118 CHECK_SYMBOL (val
);
10119 ASET (attrs
, coding_attr_encode_tbl
, val
);
10121 val
= args
[coding_arg_post_read_conversion
];
10122 CHECK_SYMBOL (val
);
10123 ASET (attrs
, coding_attr_post_read
, val
);
10125 val
= args
[coding_arg_pre_write_conversion
];
10126 CHECK_SYMBOL (val
);
10127 ASET (attrs
, coding_attr_pre_write
, val
);
10129 val
= args
[coding_arg_default_char
];
10131 ASET (attrs
, coding_attr_default_char
, make_number (' '));
10134 CHECK_CHARACTER (val
);
10135 ASET (attrs
, coding_attr_default_char
, val
);
10138 val
= args
[coding_arg_for_unibyte
];
10139 ASET (attrs
, coding_attr_for_unibyte
, NILP (val
) ? Qnil
: Qt
);
10141 val
= args
[coding_arg_plist
];
10143 ASET (attrs
, coding_attr_plist
, val
);
10145 if (EQ (coding_type
, Qcharset
))
10147 /* Generate a lisp vector of 256 elements. Each element is nil,
10148 integer, or a list of charset IDs.
10150 If Nth element is nil, the byte code N is invalid in this
10153 If Nth element is a number NUM, N is the first byte of a
10154 charset whose ID is NUM.
10156 If Nth element is a list of charset IDs, N is the first byte
10157 of one of them. The list is sorted by dimensions of the
10158 charsets. A charset of smaller dimension comes first. */
10159 val
= Fmake_vector (make_number (256), Qnil
);
10161 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
10163 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
10164 int dim
= CHARSET_DIMENSION (charset
);
10165 int idx
= (dim
- 1) * 4;
10167 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10168 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10170 for (i
= charset
->code_space
[idx
];
10171 i
<= charset
->code_space
[idx
+ 1]; i
++)
10173 Lisp_Object tmp
, tmp2
;
10176 tmp
= AREF (val
, i
);
10179 else if (NUMBERP (tmp
))
10181 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
10183 tmp
= list2 (XCAR (tail
), tmp
);
10185 tmp
= list2 (tmp
, XCAR (tail
));
10189 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
10191 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
10196 tmp
= nconc2 (tmp
, list1 (XCAR (tail
)));
10199 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
10200 XSETCAR (tmp2
, XCAR (tail
));
10203 ASET (val
, i
, tmp
);
10206 ASET (attrs
, coding_attr_charset_valids
, val
);
10207 category
= coding_category_charset
;
10209 else if (EQ (coding_type
, Qccl
))
10211 Lisp_Object valids
;
10213 if (nargs
< coding_arg_ccl_max
)
10216 val
= args
[coding_arg_ccl_decoder
];
10217 CHECK_CCL_PROGRAM (val
);
10219 val
= Fcopy_sequence (val
);
10220 ASET (attrs
, coding_attr_ccl_decoder
, val
);
10222 val
= args
[coding_arg_ccl_encoder
];
10223 CHECK_CCL_PROGRAM (val
);
10225 val
= Fcopy_sequence (val
);
10226 ASET (attrs
, coding_attr_ccl_encoder
, val
);
10228 val
= args
[coding_arg_ccl_valids
];
10229 valids
= Fmake_string (make_number (256), make_number (0));
10230 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
10235 if (INTEGERP (val
))
10237 if (! (0 <= XINT (val
) && XINT (val
) <= 255))
10238 args_out_of_range_3 (val
, make_number (0), make_number (255));
10239 from
= to
= XINT (val
);
10244 CHECK_NATNUM_CAR (val
);
10245 CHECK_NUMBER_CDR (val
);
10246 if (XINT (XCAR (val
)) > 255)
10247 args_out_of_range_3 (XCAR (val
),
10248 make_number (0), make_number (255));
10249 from
= XINT (XCAR (val
));
10250 if (! (from
<= XINT (XCDR (val
)) && XINT (XCDR (val
)) <= 255))
10251 args_out_of_range_3 (XCDR (val
),
10252 XCAR (val
), make_number (255));
10253 to
= XINT (XCDR (val
));
10255 for (i
= from
; i
<= to
; i
++)
10256 SSET (valids
, i
, 1);
10258 ASET (attrs
, coding_attr_ccl_valids
, valids
);
10260 category
= coding_category_ccl
;
10262 else if (EQ (coding_type
, Qutf_16
))
10264 Lisp_Object bom
, endian
;
10266 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
10268 if (nargs
< coding_arg_utf16_max
)
10271 bom
= args
[coding_arg_utf16_bom
];
10272 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10276 CHECK_CODING_SYSTEM (val
);
10278 CHECK_CODING_SYSTEM (val
);
10280 ASET (attrs
, coding_attr_utf_bom
, bom
);
10282 endian
= args
[coding_arg_utf16_endian
];
10283 CHECK_SYMBOL (endian
);
10286 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
10287 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
10288 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
10290 category
= (CONSP (bom
)
10291 ? coding_category_utf_16_auto
10293 ? (EQ (endian
, Qbig
)
10294 ? coding_category_utf_16_be_nosig
10295 : coding_category_utf_16_le_nosig
)
10296 : (EQ (endian
, Qbig
)
10297 ? coding_category_utf_16_be
10298 : coding_category_utf_16_le
));
10300 else if (EQ (coding_type
, Qiso_2022
))
10302 Lisp_Object initial
, reg_usage
, request
, flags
;
10304 if (nargs
< coding_arg_iso2022_max
)
10307 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
10308 CHECK_VECTOR (initial
);
10309 for (i
= 0; i
< 4; i
++)
10311 val
= AREF (initial
, i
);
10314 struct charset
*charset
;
10316 CHECK_CHARSET_GET_CHARSET (val
, charset
);
10317 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
10318 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
10319 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10322 ASET (initial
, i
, make_number (-1));
10325 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
10326 CHECK_CONS (reg_usage
);
10327 CHECK_NUMBER_CAR (reg_usage
);
10328 CHECK_NUMBER_CDR (reg_usage
);
10330 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
10331 for (tail
= request
; CONSP (tail
); tail
= XCDR (tail
))
10339 CHECK_CHARSET_GET_ID (tmp1
, id
);
10340 CHECK_NATNUM_CDR (val
);
10341 if (XINT (XCDR (val
)) >= 4)
10342 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
10343 XSETCAR (val
, make_number (id
));
10346 flags
= args
[coding_arg_iso2022_flags
];
10347 CHECK_NATNUM (flags
);
10348 i
= XINT (flags
) & INT_MAX
;
10349 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
10350 i
|= CODING_ISO_FLAG_FULL_SUPPORT
;
10351 flags
= make_number (i
);
10353 ASET (attrs
, coding_attr_iso_initial
, initial
);
10354 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
10355 ASET (attrs
, coding_attr_iso_request
, request
);
10356 ASET (attrs
, coding_attr_iso_flags
, flags
);
10357 setup_iso_safe_charsets (attrs
);
10359 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
10360 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
10361 | CODING_ISO_FLAG_SINGLE_SHIFT
))
10362 ? coding_category_iso_7_else
10363 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
10364 ? coding_category_iso_7
10365 : coding_category_iso_7_tight
);
10368 int id
= XINT (AREF (initial
, 1));
10370 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
10371 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
10373 ? coding_category_iso_8_else
10374 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
10375 ? coding_category_iso_8_1
10376 : coding_category_iso_8_2
);
10378 if (category
!= coding_category_iso_8_1
10379 && category
!= coding_category_iso_8_2
)
10380 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
10382 else if (EQ (coding_type
, Qemacs_mule
))
10384 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
10385 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
10386 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10387 category
= coding_category_emacs_mule
;
10389 else if (EQ (coding_type
, Qshift_jis
))
10392 struct charset
*charset
;
10394 if (XINT (Flength (charset_list
)) != 3
10395 && XINT (Flength (charset_list
)) != 4)
10396 error ("There should be three or four charsets");
10398 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10399 if (CHARSET_DIMENSION (charset
) != 1)
10400 error ("Dimension of charset %s is not one",
10401 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10402 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10403 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10405 charset_list
= XCDR (charset_list
);
10406 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10407 if (CHARSET_DIMENSION (charset
) != 1)
10408 error ("Dimension of charset %s is not one",
10409 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10411 charset_list
= XCDR (charset_list
);
10412 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10413 if (CHARSET_DIMENSION (charset
) != 2)
10414 error ("Dimension of charset %s is not two",
10415 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10417 charset_list
= XCDR (charset_list
);
10418 if (! NILP (charset_list
))
10420 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10421 if (CHARSET_DIMENSION (charset
) != 2)
10422 error ("Dimension of charset %s is not two",
10423 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10426 category
= coding_category_sjis
;
10427 Vsjis_coding_system
= name
;
10429 else if (EQ (coding_type
, Qbig5
))
10431 struct charset
*charset
;
10433 if (XINT (Flength (charset_list
)) != 2)
10434 error ("There should be just two charsets");
10436 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10437 if (CHARSET_DIMENSION (charset
) != 1)
10438 error ("Dimension of charset %s is not one",
10439 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10440 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10441 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10443 charset_list
= XCDR (charset_list
);
10444 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10445 if (CHARSET_DIMENSION (charset
) != 2)
10446 error ("Dimension of charset %s is not two",
10447 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10449 category
= coding_category_big5
;
10450 Vbig5_coding_system
= name
;
10452 else if (EQ (coding_type
, Qraw_text
))
10454 category
= coding_category_raw_text
;
10455 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10457 else if (EQ (coding_type
, Qutf_8
))
10461 if (nargs
< coding_arg_utf8_max
)
10464 bom
= args
[coding_arg_utf8_bom
];
10465 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10469 CHECK_CODING_SYSTEM (val
);
10471 CHECK_CODING_SYSTEM (val
);
10473 ASET (attrs
, coding_attr_utf_bom
, bom
);
10475 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
10477 category
= (CONSP (bom
) ? coding_category_utf_8_auto
10478 : NILP (bom
) ? coding_category_utf_8_nosig
10479 : coding_category_utf_8_sig
);
10481 else if (EQ (coding_type
, Qundecided
))
10483 if (nargs
< coding_arg_undecided_max
)
10485 ASET (attrs
, coding_attr_undecided_inhibit_null_byte_detection
,
10486 args
[coding_arg_undecided_inhibit_null_byte_detection
]);
10487 ASET (attrs
, coding_attr_undecided_inhibit_iso_escape_detection
,
10488 args
[coding_arg_undecided_inhibit_iso_escape_detection
]);
10489 ASET (attrs
, coding_attr_undecided_prefer_utf_8
,
10490 args
[coding_arg_undecided_prefer_utf_8
]);
10491 category
= coding_category_undecided
;
10494 error ("Invalid coding system type: %s",
10495 SDATA (SYMBOL_NAME (coding_type
)));
10497 ASET (attrs
, coding_attr_category
, make_number (category
));
10498 ASET (attrs
, coding_attr_plist
,
10500 Fcons (AREF (Vcoding_category_table
, category
),
10501 CODING_ATTR_PLIST (attrs
))));
10502 ASET (attrs
, coding_attr_plist
,
10503 Fcons (QCascii_compatible_p
,
10504 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
10505 CODING_ATTR_PLIST (attrs
))));
10507 eol_type
= args
[coding_arg_eol_type
];
10508 if (! NILP (eol_type
)
10509 && ! EQ (eol_type
, Qunix
)
10510 && ! EQ (eol_type
, Qdos
)
10511 && ! EQ (eol_type
, Qmac
))
10512 error ("Invalid eol-type");
10514 aliases
= list1 (name
);
10516 if (NILP (eol_type
))
10518 eol_type
= make_subsidiaries (name
);
10519 for (i
= 0; i
< 3; i
++)
10521 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10523 this_name
= AREF (eol_type
, i
);
10524 this_aliases
= list1 (this_name
);
10525 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10526 this_spec
= make_uninit_vector (3);
10527 ASET (this_spec
, 0, attrs
);
10528 ASET (this_spec
, 1, this_aliases
);
10529 ASET (this_spec
, 2, this_eol_type
);
10530 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10531 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10532 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10534 Vcoding_system_alist
10535 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10536 Vcoding_system_alist
);
10540 spec_vec
= make_uninit_vector (3);
10541 ASET (spec_vec
, 0, attrs
);
10542 ASET (spec_vec
, 1, aliases
);
10543 ASET (spec_vec
, 2, eol_type
);
10545 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10546 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10547 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10549 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10550 Vcoding_system_alist
);
10553 int id
= coding_categories
[category
].id
;
10555 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10556 setup_coding_system (name
, &coding_categories
[category
]);
10562 return Fsignal (Qwrong_number_of_arguments
,
10563 Fcons (intern ("define-coding-system-internal"),
10564 make_number (nargs
)));
10568 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10570 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10571 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10573 Lisp_Object spec
, attrs
;
10575 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10576 attrs
= AREF (spec
, 0);
10577 if (EQ (prop
, QCmnemonic
))
10579 if (! STRINGP (val
))
10580 CHECK_CHARACTER (val
);
10581 ASET (attrs
, coding_attr_mnemonic
, val
);
10583 else if (EQ (prop
, QCdefault_char
))
10586 val
= make_number (' ');
10588 CHECK_CHARACTER (val
);
10589 ASET (attrs
, coding_attr_default_char
, val
);
10591 else if (EQ (prop
, QCdecode_translation_table
))
10593 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10594 CHECK_SYMBOL (val
);
10595 ASET (attrs
, coding_attr_decode_tbl
, val
);
10597 else if (EQ (prop
, QCencode_translation_table
))
10599 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10600 CHECK_SYMBOL (val
);
10601 ASET (attrs
, coding_attr_encode_tbl
, val
);
10603 else if (EQ (prop
, QCpost_read_conversion
))
10605 CHECK_SYMBOL (val
);
10606 ASET (attrs
, coding_attr_post_read
, val
);
10608 else if (EQ (prop
, QCpre_write_conversion
))
10610 CHECK_SYMBOL (val
);
10611 ASET (attrs
, coding_attr_pre_write
, val
);
10613 else if (EQ (prop
, QCascii_compatible_p
))
10615 ASET (attrs
, coding_attr_ascii_compat
, val
);
10618 ASET (attrs
, coding_attr_plist
,
10619 Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
));
10624 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10625 Sdefine_coding_system_alias
, 2, 2, 0,
10626 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10627 (Lisp_Object alias
, Lisp_Object coding_system
)
10629 Lisp_Object spec
, aliases
, eol_type
, val
;
10631 CHECK_SYMBOL (alias
);
10632 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10633 aliases
= AREF (spec
, 1);
10634 /* ALIASES should be a list of length more than zero, and the first
10635 element is a base coding system. Append ALIAS at the tail of the
10637 while (!NILP (XCDR (aliases
)))
10638 aliases
= XCDR (aliases
);
10639 XSETCDR (aliases
, list1 (alias
));
10641 eol_type
= AREF (spec
, 2);
10642 if (VECTORP (eol_type
))
10644 Lisp_Object subsidiaries
;
10647 subsidiaries
= make_subsidiaries (alias
);
10648 for (i
= 0; i
< 3; i
++)
10649 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10650 AREF (eol_type
, i
));
10653 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10654 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10655 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10657 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10658 Vcoding_system_alist
);
10663 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10665 doc
: /* Return the base of CODING-SYSTEM.
10666 Any alias or subsidiary coding system is not a base coding system. */)
10667 (Lisp_Object coding_system
)
10669 Lisp_Object spec
, attrs
;
10671 if (NILP (coding_system
))
10672 return (Qno_conversion
);
10673 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10674 attrs
= AREF (spec
, 0);
10675 return CODING_ATTR_BASE_NAME (attrs
);
10678 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10680 doc
: /* Return the property list of CODING-SYSTEM. */)
10681 (Lisp_Object coding_system
)
10683 Lisp_Object spec
, attrs
;
10685 if (NILP (coding_system
))
10686 coding_system
= Qno_conversion
;
10687 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10688 attrs
= AREF (spec
, 0);
10689 return CODING_ATTR_PLIST (attrs
);
10693 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10695 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10696 (Lisp_Object coding_system
)
10700 if (NILP (coding_system
))
10701 coding_system
= Qno_conversion
;
10702 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10703 return AREF (spec
, 1);
10706 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10707 Scoding_system_eol_type
, 1, 1, 0,
10708 doc
: /* Return eol-type of CODING-SYSTEM.
10709 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10711 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10712 and CR respectively.
10714 A vector value indicates that a format of end-of-line should be
10715 detected automatically. Nth element of the vector is the subsidiary
10716 coding system whose eol-type is N. */)
10717 (Lisp_Object coding_system
)
10719 Lisp_Object spec
, eol_type
;
10722 if (NILP (coding_system
))
10723 coding_system
= Qno_conversion
;
10724 if (! CODING_SYSTEM_P (coding_system
))
10726 spec
= CODING_SYSTEM_SPEC (coding_system
);
10727 eol_type
= AREF (spec
, 2);
10728 if (VECTORP (eol_type
))
10729 return Fcopy_sequence (eol_type
);
10730 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10731 return make_number (n
);
10737 /*** 9. Post-amble ***/
10740 init_coding_once (void)
10744 for (i
= 0; i
< coding_category_max
; i
++)
10746 coding_categories
[i
].id
= -1;
10747 coding_priorities
[i
] = i
;
10750 /* ISO2022 specific initialize routine. */
10751 for (i
= 0; i
< 0x20; i
++)
10752 iso_code_class
[i
] = ISO_control_0
;
10753 for (i
= 0x21; i
< 0x7F; i
++)
10754 iso_code_class
[i
] = ISO_graphic_plane_0
;
10755 for (i
= 0x80; i
< 0xA0; i
++)
10756 iso_code_class
[i
] = ISO_control_1
;
10757 for (i
= 0xA1; i
< 0xFF; i
++)
10758 iso_code_class
[i
] = ISO_graphic_plane_1
;
10759 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10760 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10761 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10762 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10763 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10764 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10765 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10766 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10767 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10769 for (i
= 0; i
< 256; i
++)
10771 emacs_mule_bytes
[i
] = 1;
10773 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10774 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10775 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10776 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10782 syms_of_coding (void)
10784 staticpro (&Vcoding_system_hash_table
);
10785 Vcoding_system_hash_table
= CALLN (Fmake_hash_table
, QCtest
, Qeq
);
10787 staticpro (&Vsjis_coding_system
);
10788 Vsjis_coding_system
= Qnil
;
10790 staticpro (&Vbig5_coding_system
);
10791 Vbig5_coding_system
= Qnil
;
10793 staticpro (&Vcode_conversion_reused_workbuf
);
10794 Vcode_conversion_reused_workbuf
= Qnil
;
10796 staticpro (&Vcode_conversion_workbuf_name
);
10797 Vcode_conversion_workbuf_name
= build_pure_c_string (" *code-conversion-work*");
10799 reused_workbuf_in_use
= 0;
10801 DEFSYM (Qcharset
, "charset");
10802 DEFSYM (Qtarget_idx
, "target-idx");
10803 DEFSYM (Qcoding_system_history
, "coding-system-history");
10804 Fset (Qcoding_system_history
, Qnil
);
10806 /* Target FILENAME is the first argument. */
10807 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10808 /* Target FILENAME is the third argument. */
10809 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10811 DEFSYM (Qcall_process
, "call-process");
10812 /* Target PROGRAM is the first argument. */
10813 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10815 DEFSYM (Qcall_process_region
, "call-process-region");
10816 /* Target PROGRAM is the third argument. */
10817 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10819 DEFSYM (Qstart_process
, "start-process");
10820 /* Target PROGRAM is the third argument. */
10821 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10823 DEFSYM (Qopen_network_stream
, "open-network-stream");
10824 /* Target SERVICE is the fourth argument. */
10825 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10827 DEFSYM (Qunix
, "unix");
10828 DEFSYM (Qdos
, "dos");
10829 DEFSYM (Qmac
, "mac");
10831 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10832 DEFSYM (Qundecided
, "undecided");
10833 DEFSYM (Qno_conversion
, "no-conversion");
10834 DEFSYM (Qraw_text
, "raw-text");
10836 DEFSYM (Qiso_2022
, "iso-2022");
10838 DEFSYM (Qutf_8
, "utf-8");
10839 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10841 #if defined (WINDOWSNT) || defined (CYGWIN)
10842 /* No, not utf-16-le: that one has a BOM. */
10843 DEFSYM (Qutf_16le
, "utf-16le");
10846 DEFSYM (Qutf_16
, "utf-16");
10847 DEFSYM (Qbig
, "big");
10848 DEFSYM (Qlittle
, "little");
10850 DEFSYM (Qshift_jis
, "shift-jis");
10851 DEFSYM (Qbig5
, "big5");
10853 DEFSYM (Qcoding_system_p
, "coding-system-p");
10855 /* Error signaled when there's a problem with detecting a coding system. */
10856 DEFSYM (Qcoding_system_error
, "coding-system-error");
10857 Fput (Qcoding_system_error
, Qerror_conditions
,
10858 listn (CONSTYPE_PURE
, 2, Qcoding_system_error
, Qerror
));
10859 Fput (Qcoding_system_error
, Qerror_message
,
10860 build_pure_c_string ("Invalid coding system"));
10862 DEFSYM (Qtranslation_table
, "translation-table");
10863 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10864 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10866 /* Coding system emacs-mule and raw-text are for converting only
10867 end-of-line format. */
10868 DEFSYM (Qemacs_mule
, "emacs-mule");
10870 DEFSYM (QCcategory
, ":category");
10871 DEFSYM (QCmnemonic
, ":mnemonic");
10872 DEFSYM (QCdefault_char
, ":default-char");
10873 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10874 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10875 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10876 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10877 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10879 Vcoding_category_table
10880 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10881 staticpro (&Vcoding_category_table
);
10882 /* Followings are target of code detection. */
10883 ASET (Vcoding_category_table
, coding_category_iso_7
,
10884 intern_c_string ("coding-category-iso-7"));
10885 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10886 intern_c_string ("coding-category-iso-7-tight"));
10887 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10888 intern_c_string ("coding-category-iso-8-1"));
10889 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10890 intern_c_string ("coding-category-iso-8-2"));
10891 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10892 intern_c_string ("coding-category-iso-7-else"));
10893 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10894 intern_c_string ("coding-category-iso-8-else"));
10895 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10896 intern_c_string ("coding-category-utf-8-auto"));
10897 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10898 intern_c_string ("coding-category-utf-8"));
10899 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10900 intern_c_string ("coding-category-utf-8-sig"));
10901 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10902 intern_c_string ("coding-category-utf-16-be"));
10903 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10904 intern_c_string ("coding-category-utf-16-auto"));
10905 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10906 intern_c_string ("coding-category-utf-16-le"));
10907 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10908 intern_c_string ("coding-category-utf-16-be-nosig"));
10909 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10910 intern_c_string ("coding-category-utf-16-le-nosig"));
10911 ASET (Vcoding_category_table
, coding_category_charset
,
10912 intern_c_string ("coding-category-charset"));
10913 ASET (Vcoding_category_table
, coding_category_sjis
,
10914 intern_c_string ("coding-category-sjis"));
10915 ASET (Vcoding_category_table
, coding_category_big5
,
10916 intern_c_string ("coding-category-big5"));
10917 ASET (Vcoding_category_table
, coding_category_ccl
,
10918 intern_c_string ("coding-category-ccl"));
10919 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10920 intern_c_string ("coding-category-emacs-mule"));
10921 /* Followings are NOT target of code detection. */
10922 ASET (Vcoding_category_table
, coding_category_raw_text
,
10923 intern_c_string ("coding-category-raw-text"));
10924 ASET (Vcoding_category_table
, coding_category_undecided
,
10925 intern_c_string ("coding-category-undecided"));
10927 DEFSYM (Qinsufficient_source
, "insufficient-source");
10928 DEFSYM (Qinvalid_source
, "invalid-source");
10929 DEFSYM (Qinterrupted
, "interrupted");
10931 /* If a symbol has this property, evaluate the value to define the
10932 symbol as a coding system. */
10933 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10935 defsubr (&Scoding_system_p
);
10936 defsubr (&Sread_coding_system
);
10937 defsubr (&Sread_non_nil_coding_system
);
10938 defsubr (&Scheck_coding_system
);
10939 defsubr (&Sdetect_coding_region
);
10940 defsubr (&Sdetect_coding_string
);
10941 defsubr (&Sfind_coding_systems_region_internal
);
10942 defsubr (&Sunencodable_char_position
);
10943 defsubr (&Scheck_coding_systems_region
);
10944 defsubr (&Sdecode_coding_region
);
10945 defsubr (&Sencode_coding_region
);
10946 defsubr (&Sdecode_coding_string
);
10947 defsubr (&Sencode_coding_string
);
10948 defsubr (&Sdecode_sjis_char
);
10949 defsubr (&Sencode_sjis_char
);
10950 defsubr (&Sdecode_big5_char
);
10951 defsubr (&Sencode_big5_char
);
10952 defsubr (&Sset_terminal_coding_system_internal
);
10953 defsubr (&Sset_safe_terminal_coding_system_internal
);
10954 defsubr (&Sterminal_coding_system
);
10955 defsubr (&Sset_keyboard_coding_system_internal
);
10956 defsubr (&Skeyboard_coding_system
);
10957 defsubr (&Sfind_operation_coding_system
);
10958 defsubr (&Sset_coding_system_priority
);
10959 defsubr (&Sdefine_coding_system_internal
);
10960 defsubr (&Sdefine_coding_system_alias
);
10961 defsubr (&Scoding_system_put
);
10962 defsubr (&Scoding_system_base
);
10963 defsubr (&Scoding_system_plist
);
10964 defsubr (&Scoding_system_aliases
);
10965 defsubr (&Scoding_system_eol_type
);
10966 defsubr (&Scoding_system_priority_list
);
10968 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10969 doc
: /* List of coding systems.
10971 Do not alter the value of this variable manually. This variable should be
10972 updated by the functions `define-coding-system' and
10973 `define-coding-system-alias'. */);
10974 Vcoding_system_list
= Qnil
;
10976 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10977 doc
: /* Alist of coding system names.
10978 Each element is one element list of coding system name.
10979 This variable is given to `completing-read' as COLLECTION argument.
10981 Do not alter the value of this variable manually. This variable should be
10982 updated by the functions `make-coding-system' and
10983 `define-coding-system-alias'. */);
10984 Vcoding_system_alist
= Qnil
;
10986 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10987 doc
: /* List of coding-categories (symbols) ordered by priority.
10989 On detecting a coding system, Emacs tries code detection algorithms
10990 associated with each coding-category one by one in this order. When
10991 one algorithm agrees with a byte sequence of source text, the coding
10992 system bound to the corresponding coding-category is selected.
10994 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10998 Vcoding_category_list
= Qnil
;
10999 for (i
= coding_category_max
- 1; i
>= 0; i
--)
11000 Vcoding_category_list
11001 = Fcons (AREF (Vcoding_category_table
, i
),
11002 Vcoding_category_list
);
11005 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
11006 doc
: /* Specify the coding system for read operations.
11007 It is useful to bind this variable with `let', but do not set it globally.
11008 If the value is a coding system, it is used for decoding on read operation.
11009 If not, an appropriate element is used from one of the coding system alists.
11010 There are three such tables: `file-coding-system-alist',
11011 `process-coding-system-alist', and `network-coding-system-alist'. */);
11012 Vcoding_system_for_read
= Qnil
;
11014 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
11015 doc
: /* Specify the coding system for write operations.
11016 Programs bind this variable with `let', but you should not set it globally.
11017 If the value is a coding system, it is used for encoding of output,
11018 when writing it to a file and when sending it to a file or subprocess.
11020 If this does not specify a coding system, an appropriate element
11021 is used from one of the coding system alists.
11022 There are three such tables: `file-coding-system-alist',
11023 `process-coding-system-alist', and `network-coding-system-alist'.
11024 For output to files, if the above procedure does not specify a coding system,
11025 the value of `buffer-file-coding-system' is used. */);
11026 Vcoding_system_for_write
= Qnil
;
11028 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
11030 Coding system used in the latest file or process I/O. */);
11031 Vlast_coding_system_used
= Qnil
;
11033 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
11035 Error status of the last code conversion.
11037 When an error was detected in the last code conversion, this variable
11038 is set to one of the following symbols.
11039 `insufficient-source'
11043 `insufficient-memory'
11044 When no error was detected, the value doesn't change. So, to check
11045 the error status of a code conversion by this variable, you must
11046 explicitly set this variable to nil before performing code
11048 Vlast_code_conversion_error
= Qnil
;
11050 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
11052 Non-nil means always inhibit code conversion of end-of-line format.
11053 See info node `Coding Systems' and info node `Text and Binary' concerning
11054 such conversion. */);
11055 inhibit_eol_conversion
= 0;
11057 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
11059 Non-nil means process buffer inherits coding system of process output.
11060 Bind it to t if the process output is to be treated as if it were a file
11061 read from some filesystem. */);
11062 inherit_process_coding_system
= 0;
11064 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
11066 Alist to decide a coding system to use for a file I/O operation.
11067 The format is ((PATTERN . VAL) ...),
11068 where PATTERN is a regular expression matching a file name,
11069 VAL is a coding system, a cons of coding systems, or a function symbol.
11070 If VAL is a coding system, it is used for both decoding and encoding
11072 If VAL is a cons of coding systems, the car part is used for decoding,
11073 and the cdr part is used for encoding.
11074 If VAL is a function symbol, the function must return a coding system
11075 or a cons of coding systems which are used as above. The function is
11076 called with an argument that is a list of the arguments with which
11077 `find-operation-coding-system' was called. If the function can't decide
11078 a coding system, it can return `undecided' so that the normal
11079 code-detection is performed.
11081 See also the function `find-operation-coding-system'
11082 and the variable `auto-coding-alist'. */);
11083 Vfile_coding_system_alist
= Qnil
;
11085 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
11087 Alist to decide a coding system to use for a process I/O operation.
11088 The format is ((PATTERN . VAL) ...),
11089 where PATTERN is a regular expression matching a program name,
11090 VAL is a coding system, a cons of coding systems, or a function symbol.
11091 If VAL is a coding system, it is used for both decoding what received
11092 from the program and encoding what sent to the program.
11093 If VAL is a cons of coding systems, the car part is used for decoding,
11094 and the cdr part is used for encoding.
11095 If VAL is a function symbol, the function must return a coding system
11096 or a cons of coding systems which are used as above.
11098 See also the function `find-operation-coding-system'. */);
11099 Vprocess_coding_system_alist
= Qnil
;
11101 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
11103 Alist to decide a coding system to use for a network I/O operation.
11104 The format is ((PATTERN . VAL) ...),
11105 where PATTERN is a regular expression matching a network service name
11106 or is a port number to connect to,
11107 VAL is a coding system, a cons of coding systems, or a function symbol.
11108 If VAL is a coding system, it is used for both decoding what received
11109 from the network stream and encoding what sent to the network stream.
11110 If VAL is a cons of coding systems, the car part is used for decoding,
11111 and the cdr part is used for encoding.
11112 If VAL is a function symbol, the function must return a coding system
11113 or a cons of coding systems which are used as above.
11115 See also the function `find-operation-coding-system'. */);
11116 Vnetwork_coding_system_alist
= Qnil
;
11118 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
11119 doc
: /* Coding system to use with system messages.
11120 Also used for decoding keyboard input on X Window system, and for
11121 encoding standard output and error streams. */);
11122 Vlocale_coding_system
= Qnil
;
11124 /* The eol mnemonics are reset in startup.el system-dependently. */
11125 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
11127 String displayed in mode line for UNIX-like (LF) end-of-line format. */);
11128 eol_mnemonic_unix
= build_pure_c_string (":");
11130 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
11132 String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
11133 eol_mnemonic_dos
= build_pure_c_string ("\\");
11135 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
11137 String displayed in mode line for MAC-like (CR) end-of-line format. */);
11138 eol_mnemonic_mac
= build_pure_c_string ("/");
11140 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
11142 String displayed in mode line when end-of-line format is not yet determined. */);
11143 eol_mnemonic_undecided
= build_pure_c_string (":");
11145 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
11147 Non-nil enables character translation while encoding and decoding. */);
11148 Venable_character_translation
= Qt
;
11150 DEFVAR_LISP ("standard-translation-table-for-decode",
11151 Vstandard_translation_table_for_decode
,
11152 doc
: /* Table for translating characters while decoding. */);
11153 Vstandard_translation_table_for_decode
= Qnil
;
11155 DEFVAR_LISP ("standard-translation-table-for-encode",
11156 Vstandard_translation_table_for_encode
,
11157 doc
: /* Table for translating characters while encoding. */);
11158 Vstandard_translation_table_for_encode
= Qnil
;
11160 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
11161 doc
: /* Alist of charsets vs revision numbers.
11162 While encoding, if a charset (car part of an element) is found,
11163 designate it with the escape sequence identifying revision (cdr part
11164 of the element). */);
11165 Vcharset_revision_table
= Qnil
;
11167 DEFVAR_LISP ("default-process-coding-system",
11168 Vdefault_process_coding_system
,
11169 doc
: /* Cons of coding systems used for process I/O by default.
11170 The car part is used for decoding a process output,
11171 the cdr part is used for encoding a text to be sent to a process. */);
11172 Vdefault_process_coding_system
= Qnil
;
11174 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
11176 Table of extra Latin codes in the range 128..159 (inclusive).
11177 This is a vector of length 256.
11178 If Nth element is non-nil, the existence of code N in a file
11179 (or output of subprocess) doesn't prevent it to be detected as
11180 a coding system of ISO 2022 variant which has a flag
11181 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
11182 or reading output of a subprocess.
11183 Only 128th through 159th elements have a meaning. */);
11184 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
11186 DEFVAR_LISP ("select-safe-coding-system-function",
11187 Vselect_safe_coding_system_function
,
11189 Function to call to select safe coding system for encoding a text.
11191 If set, this function is called to force a user to select a proper
11192 coding system which can encode the text in the case that a default
11193 coding system used in each operation can't encode the text. The
11194 function should take care that the buffer is not modified while
11195 the coding system is being selected.
11197 The default value is `select-safe-coding-system' (which see). */);
11198 Vselect_safe_coding_system_function
= Qnil
;
11200 DEFVAR_BOOL ("coding-system-require-warning",
11201 coding_system_require_warning
,
11202 doc
: /* Internal use only.
11203 If non-nil, on writing a file, `select-safe-coding-system-function' is
11204 called even if `coding-system-for-write' is non-nil. The command
11205 `universal-coding-system-argument' binds this variable to t temporarily. */);
11206 coding_system_require_warning
= 0;
11209 DEFVAR_BOOL ("inhibit-iso-escape-detection",
11210 inhibit_iso_escape_detection
,
11212 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
11214 When Emacs reads text, it tries to detect how the text is encoded.
11215 This code detection is sensitive to escape sequences. If Emacs sees
11216 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
11217 of the ISO2022 encodings, and decodes text by the corresponding coding
11218 system (e.g. `iso-2022-7bit').
11220 However, there may be a case that you want to read escape sequences in
11221 a file as is. In such a case, you can set this variable to non-nil.
11222 Then the code detection will ignore any escape sequences, and no text is
11223 detected as encoded in some ISO-2022 encoding. The result is that all
11224 escape sequences become visible in a buffer.
11226 The default value is nil, and it is strongly recommended not to change
11227 it. That is because many Emacs Lisp source files that contain
11228 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
11229 in Emacs's distribution, and they won't be decoded correctly on
11230 reading if you suppress escape sequence detection.
11232 The other way to read escape sequences in a file without decoding is
11233 to explicitly specify some coding system that doesn't use ISO-2022
11234 escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */);
11235 inhibit_iso_escape_detection
= 0;
11237 DEFVAR_BOOL ("inhibit-null-byte-detection",
11238 inhibit_null_byte_detection
,
11239 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
11240 By default, Emacs treats it as binary data, and does not attempt to
11241 decode it. The effect is as if you specified `no-conversion' for
11244 Set this to non-nil when a regular text happens to include null bytes.
11245 Examples are Index nodes of Info files and null-byte delimited output
11246 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
11247 decode text as usual. */);
11248 inhibit_null_byte_detection
= 0;
11250 DEFVAR_BOOL ("disable-ascii-optimization", disable_ascii_optimization
,
11251 doc
: /* If non-nil, Emacs does not optimize code decoder for ASCII files.
11252 Internal use only. Remove after the experimental optimizer becomes stable. */);
11253 disable_ascii_optimization
= 0;
11255 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
11256 doc
: /* Char table for translating self-inserting characters.
11257 This is applied to the result of input methods, not their input.
11258 See also `keyboard-translate-table'.
11260 Use of this variable for character code unification was rendered
11261 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
11262 internal character representation. */);
11263 Vtranslation_table_for_input
= Qnil
;
11265 Lisp_Object args
[coding_arg_undecided_max
];
11266 memclear (args
, sizeof args
);
11268 Lisp_Object plist
[] =
11271 args
[coding_arg_name
] = Qno_conversion
,
11273 args
[coding_arg_mnemonic
] = make_number ('='),
11274 intern_c_string (":coding-type"),
11275 args
[coding_arg_coding_type
] = Qraw_text
,
11276 QCascii_compatible_p
,
11277 args
[coding_arg_ascii_compatible_p
] = Qt
,
11279 args
[coding_arg_default_char
] = make_number (0),
11280 intern_c_string (":for-unibyte"),
11281 args
[coding_arg_for_unibyte
] = Qt
,
11282 intern_c_string (":docstring"),
11283 (build_pure_c_string
11284 ("Do no conversion.\n"
11286 "When you visit a file with this coding, the file is read into a\n"
11287 "unibyte buffer as is, thus each byte of a file is treated as a\n"
11289 intern_c_string (":eol-type"),
11290 args
[coding_arg_eol_type
] = Qunix
,
11292 args
[coding_arg_plist
] = CALLMANY (Flist
, plist
);
11293 Fdefine_coding_system_internal (coding_arg_max
, args
);
11295 plist
[1] = args
[coding_arg_name
] = Qundecided
;
11296 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
11297 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
11298 /* This is already set.
11299 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
11300 plist
[8] = intern_c_string (":charset-list");
11301 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
11302 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
11303 plist
[13] = build_pure_c_string ("No conversion on encoding, "
11304 "automatic conversion on decoding.");
11305 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
11306 args
[coding_arg_plist
] = CALLMANY (Flist
, plist
);
11307 args
[coding_arg_undecided_inhibit_null_byte_detection
] = make_number (0);
11308 args
[coding_arg_undecided_inhibit_iso_escape_detection
] = make_number (0);
11309 Fdefine_coding_system_internal (coding_arg_undecided_max
, args
);
11311 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
11313 for (int i
= 0; i
< coding_category_max
; i
++)
11314 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
11316 #if defined (DOS_NT)
11317 system_eol_type
= Qdos
;
11319 system_eol_type
= Qunix
;
11321 staticpro (&system_eol_type
);
11325 emacs_strerror (int error_number
)
11329 synchronize_system_messages_locale ();
11330 str
= strerror (error_number
);
11332 if (! NILP (Vlocale_coding_system
))
11334 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
11335 Vlocale_coding_system
,
11337 str
= SSDATA (dec
);