1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2013 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 ***/
290 #include "character.h"
294 #include "composite.h"
298 #include "termhooks.h"
300 Lisp_Object Vcoding_system_hash_table
;
302 static Lisp_Object Qcoding_system
, Qeol_type
;
303 static Lisp_Object Qcoding_aliases
;
304 Lisp_Object Qunix
, Qdos
;
305 static Lisp_Object Qmac
;
306 Lisp_Object Qbuffer_file_coding_system
;
307 static Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
308 static Lisp_Object Qdefault_char
;
309 Lisp_Object Qno_conversion
, Qundecided
;
310 Lisp_Object Qcharset
, Qutf_8
;
311 static Lisp_Object Qiso_2022
;
312 static Lisp_Object Qutf_16
, Qshift_jis
, Qbig5
;
313 static Lisp_Object Qbig
, Qlittle
;
314 static Lisp_Object Qcoding_system_history
;
315 static Lisp_Object Qvalid_codes
;
316 static Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
317 static Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
318 static Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
319 static Lisp_Object QCascii_compatible_p
;
321 Lisp_Object Qcall_process
, Qcall_process_region
;
322 Lisp_Object Qstart_process
, Qopen_network_stream
;
323 static Lisp_Object Qtarget_idx
;
325 static Lisp_Object Qinsufficient_source
, Qinvalid_source
, Qinterrupted
;
327 /* If a symbol has this property, evaluate the value to define the
328 symbol as a coding system. */
329 static Lisp_Object Qcoding_system_define_form
;
331 /* Format of end-of-line decided by system. This is Qunix on
332 Unix and Mac, Qdos on DOS/Windows.
333 This has an effect only for external encoding (i.e. for output to
334 file and process), not for in-buffer or Lisp string encoding. */
335 static Lisp_Object system_eol_type
;
339 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
341 /* Coding system emacs-mule and raw-text are for converting only
342 end-of-line format. */
343 Lisp_Object Qemacs_mule
, Qraw_text
;
344 Lisp_Object Qutf_8_emacs
;
346 #if defined (WINDOWSNT) || defined (CYGWIN)
347 static Lisp_Object Qutf_16le
;
350 /* Coding-systems are handed between Emacs Lisp programs and C internal
351 routines by the following three variables. */
352 /* Coding system to be used to encode text for terminal display when
353 terminal coding system is nil. */
354 struct coding_system safe_terminal_coding
;
358 Lisp_Object Qtranslation_table
;
359 Lisp_Object Qtranslation_table_id
;
360 static Lisp_Object Qtranslation_table_for_decode
;
361 static Lisp_Object Qtranslation_table_for_encode
;
363 /* Two special coding systems. */
364 static Lisp_Object Vsjis_coding_system
;
365 static Lisp_Object Vbig5_coding_system
;
367 /* ISO2022 section */
369 #define CODING_ISO_INITIAL(coding, reg) \
370 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
371 coding_attr_iso_initial), \
375 #define CODING_ISO_REQUEST(coding, charset_id) \
376 (((charset_id) <= (coding)->max_charset_id \
377 ? ((coding)->safe_charsets[charset_id] != 255 \
378 ? (coding)->safe_charsets[charset_id] \
383 #define CODING_ISO_FLAGS(coding) \
384 ((coding)->spec.iso_2022.flags)
385 #define CODING_ISO_DESIGNATION(coding, reg) \
386 ((coding)->spec.iso_2022.current_designation[reg])
387 #define CODING_ISO_INVOCATION(coding, plane) \
388 ((coding)->spec.iso_2022.current_invocation[plane])
389 #define CODING_ISO_SINGLE_SHIFTING(coding) \
390 ((coding)->spec.iso_2022.single_shifting)
391 #define CODING_ISO_BOL(coding) \
392 ((coding)->spec.iso_2022.bol)
393 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
394 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
395 #define CODING_ISO_CMP_STATUS(coding) \
396 (&(coding)->spec.iso_2022.cmp_status)
397 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
398 ((coding)->spec.iso_2022.ctext_extended_segment_len)
399 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
400 ((coding)->spec.iso_2022.embedded_utf_8)
402 /* Control characters of ISO2022. */
403 /* code */ /* function */
404 #define ISO_CODE_SO 0x0E /* shift-out */
405 #define ISO_CODE_SI 0x0F /* shift-in */
406 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
407 #define ISO_CODE_ESC 0x1B /* escape */
408 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
409 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
410 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
412 /* All code (1-byte) of ISO2022 is classified into one of the
414 enum iso_code_class_type
416 ISO_control_0
, /* Control codes in the range
417 0x00..0x1F and 0x7F, except for the
418 following 5 codes. */
419 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
420 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
421 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
422 ISO_escape
, /* ISO_CODE_ESC (0x1B) */
423 ISO_control_1
, /* Control codes in the range
424 0x80..0x9F, except for the
425 following 3 codes. */
426 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
427 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
428 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
429 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
430 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
431 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
432 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
435 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
436 `iso-flags' attribute of an iso2022 coding system. */
438 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
439 instead of the correct short-form sequence (e.g. ESC $ A). */
440 #define CODING_ISO_FLAG_LONG_FORM 0x0001
442 /* If set, reset graphic planes and registers at end-of-line to the
444 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
446 /* If set, reset graphic planes and registers before any control
447 characters to the initial state. */
448 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
450 /* If set, encode by 7-bit environment. */
451 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
453 /* If set, use locking-shift function. */
454 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
456 /* If set, use single-shift function. Overwrite
457 CODING_ISO_FLAG_LOCKING_SHIFT. */
458 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
460 /* If set, use designation escape sequence. */
461 #define CODING_ISO_FLAG_DESIGNATION 0x0040
463 /* If set, produce revision number sequence. */
464 #define CODING_ISO_FLAG_REVISION 0x0080
466 /* If set, produce ISO6429's direction specifying sequence. */
467 #define CODING_ISO_FLAG_DIRECTION 0x0100
469 /* If set, assume designation states are reset at beginning of line on
471 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
473 /* If set, designation sequence should be placed at beginning of line
475 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
477 /* If set, do not encode unsafe characters on output. */
478 #define CODING_ISO_FLAG_SAFE 0x0800
480 /* If set, extra latin codes (128..159) are accepted as a valid code
482 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
484 #define CODING_ISO_FLAG_COMPOSITION 0x2000
486 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
488 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
490 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
492 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
494 /* A character to be produced on output if encoding of the original
495 character is prohibited by CODING_ISO_FLAG_SAFE. */
496 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
499 #define CODING_UTF_8_BOM(coding) \
500 ((coding)->spec.utf_8_bom)
503 #define CODING_UTF_16_BOM(coding) \
504 ((coding)->spec.utf_16.bom)
506 #define CODING_UTF_16_ENDIAN(coding) \
507 ((coding)->spec.utf_16.endian)
509 #define CODING_UTF_16_SURROGATE(coding) \
510 ((coding)->spec.utf_16.surrogate)
514 #define CODING_CCL_DECODER(coding) \
515 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
516 #define CODING_CCL_ENCODER(coding) \
517 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
518 #define CODING_CCL_VALIDS(coding) \
519 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
521 /* Index for each coding category in `coding_categories' */
525 coding_category_iso_7
,
526 coding_category_iso_7_tight
,
527 coding_category_iso_8_1
,
528 coding_category_iso_8_2
,
529 coding_category_iso_7_else
,
530 coding_category_iso_8_else
,
531 coding_category_utf_8_auto
,
532 coding_category_utf_8_nosig
,
533 coding_category_utf_8_sig
,
534 coding_category_utf_16_auto
,
535 coding_category_utf_16_be
,
536 coding_category_utf_16_le
,
537 coding_category_utf_16_be_nosig
,
538 coding_category_utf_16_le_nosig
,
539 coding_category_charset
,
540 coding_category_sjis
,
541 coding_category_big5
,
543 coding_category_emacs_mule
,
544 /* All above are targets of code detection. */
545 coding_category_raw_text
,
546 coding_category_undecided
,
550 /* Definitions of flag bits used in detect_coding_XXXX. */
551 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
552 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
553 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
554 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
555 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
556 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
557 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
558 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
559 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
560 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
561 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
562 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
563 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
564 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
565 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
566 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
567 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
568 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
569 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
570 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
572 /* This value is returned if detect_coding_mask () find nothing other
573 than ASCII characters. */
574 #define CATEGORY_MASK_ANY \
575 (CATEGORY_MASK_ISO_7 \
576 | CATEGORY_MASK_ISO_7_TIGHT \
577 | CATEGORY_MASK_ISO_8_1 \
578 | CATEGORY_MASK_ISO_8_2 \
579 | CATEGORY_MASK_ISO_7_ELSE \
580 | CATEGORY_MASK_ISO_8_ELSE \
581 | CATEGORY_MASK_UTF_8_AUTO \
582 | CATEGORY_MASK_UTF_8_NOSIG \
583 | CATEGORY_MASK_UTF_8_SIG \
584 | CATEGORY_MASK_UTF_16_AUTO \
585 | CATEGORY_MASK_UTF_16_BE \
586 | CATEGORY_MASK_UTF_16_LE \
587 | CATEGORY_MASK_UTF_16_BE_NOSIG \
588 | CATEGORY_MASK_UTF_16_LE_NOSIG \
589 | CATEGORY_MASK_CHARSET \
590 | CATEGORY_MASK_SJIS \
591 | CATEGORY_MASK_BIG5 \
592 | CATEGORY_MASK_CCL \
593 | CATEGORY_MASK_EMACS_MULE)
596 #define CATEGORY_MASK_ISO_7BIT \
597 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
599 #define CATEGORY_MASK_ISO_8BIT \
600 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
602 #define CATEGORY_MASK_ISO_ELSE \
603 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
605 #define CATEGORY_MASK_ISO_ESCAPE \
606 (CATEGORY_MASK_ISO_7 \
607 | CATEGORY_MASK_ISO_7_TIGHT \
608 | CATEGORY_MASK_ISO_7_ELSE \
609 | CATEGORY_MASK_ISO_8_ELSE)
611 #define CATEGORY_MASK_ISO \
612 ( CATEGORY_MASK_ISO_7BIT \
613 | CATEGORY_MASK_ISO_8BIT \
614 | CATEGORY_MASK_ISO_ELSE)
616 #define CATEGORY_MASK_UTF_16 \
617 (CATEGORY_MASK_UTF_16_AUTO \
618 | CATEGORY_MASK_UTF_16_BE \
619 | CATEGORY_MASK_UTF_16_LE \
620 | CATEGORY_MASK_UTF_16_BE_NOSIG \
621 | CATEGORY_MASK_UTF_16_LE_NOSIG)
623 #define CATEGORY_MASK_UTF_8 \
624 (CATEGORY_MASK_UTF_8_AUTO \
625 | CATEGORY_MASK_UTF_8_NOSIG \
626 | CATEGORY_MASK_UTF_8_SIG)
628 /* Table of coding categories (Lisp symbols). This variable is for
629 internal use only. */
630 static Lisp_Object Vcoding_category_table
;
632 /* Table of coding-categories ordered by priority. */
633 static enum coding_category coding_priorities
[coding_category_max
];
635 /* Nth element is a coding context for the coding system bound to the
636 Nth coding category. */
637 static struct coding_system coding_categories
[coding_category_max
];
639 /*** Commonly used macros and functions ***/
642 #define min(a, b) ((a) < (b) ? (a) : (b))
645 #define max(a, b) ((a) > (b) ? (a) : (b))
648 #define CODING_GET_INFO(coding, attrs, charset_list) \
650 (attrs) = CODING_ID_ATTRS ((coding)->id); \
651 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
655 /* Safely get one byte from the source text pointed by SRC which ends
656 at SRC_END, and set C to that byte. If there are not enough bytes
657 in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
658 and a multibyte character is found at SRC, set C to the
659 negative value of the character code. The caller should declare
660 and set these variables appropriately in advance:
661 src, src_end, multibytep */
663 #define ONE_MORE_BYTE(c) \
665 if (src == src_end) \
667 if (src_base < src) \
668 record_conversion_result \
669 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
670 goto no_more_source; \
673 if (multibytep && (c & 0x80)) \
675 if ((c & 0xFE) == 0xC0) \
676 c = ((c & 1) << 6) | *src++; \
680 c = - string_char (src, &src, NULL); \
681 record_conversion_result \
682 (coding, CODING_RESULT_INVALID_SRC); \
688 /* Safely get two bytes from the source text pointed by SRC which ends
689 at SRC_END, and set C1 and C2 to those bytes while skipping the
690 heading multibyte characters. If there are not enough bytes in the
691 source, it jumps to 'no_more_source'. If MULTIBYTEP and
692 a multibyte character is found for C2, set C2 to the negative value
693 of the character code. The caller should declare and set these
694 variables appropriately in advance:
695 src, src_end, multibytep
696 It is intended that this macro is used in detect_coding_utf_16. */
698 #define TWO_MORE_BYTES(c1, c2) \
701 if (src == src_end) \
702 goto no_more_source; \
704 if (multibytep && (c1 & 0x80)) \
706 if ((c1 & 0xFE) == 0xC0) \
707 c1 = ((c1 & 1) << 6) | *src++; \
710 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
715 if (src == src_end) \
716 goto no_more_source; \
718 if (multibytep && (c2 & 0x80)) \
720 if ((c2 & 0xFE) == 0xC0) \
721 c2 = ((c2 & 1) << 6) | *src++; \
728 /* Store a byte C in the place pointed by DST and increment DST to the
729 next free point, and increment PRODUCED_CHARS. The caller should
730 assure that C is 0..127, and declare and set the variable `dst'
731 appropriately in advance.
735 #define EMIT_ONE_ASCII_BYTE(c) \
742 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
744 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
746 produced_chars += 2; \
747 *dst++ = (c1), *dst++ = (c2); \
751 /* Store a byte C in the place pointed by DST and increment DST to the
752 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
753 store in an appropriate multibyte form. The caller should
754 declare and set the variables `dst' and `multibytep' appropriately
757 #define EMIT_ONE_BYTE(c) \
764 ch = BYTE8_TO_CHAR (ch); \
765 CHAR_STRING_ADVANCE (ch, dst); \
772 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
774 #define EMIT_TWO_BYTES(c1, c2) \
776 produced_chars += 2; \
783 ch = BYTE8_TO_CHAR (ch); \
784 CHAR_STRING_ADVANCE (ch, dst); \
787 ch = BYTE8_TO_CHAR (ch); \
788 CHAR_STRING_ADVANCE (ch, dst); \
798 #define EMIT_THREE_BYTES(c1, c2, c3) \
800 EMIT_ONE_BYTE (c1); \
801 EMIT_TWO_BYTES (c2, c3); \
805 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
807 EMIT_TWO_BYTES (c1, c2); \
808 EMIT_TWO_BYTES (c3, c4); \
813 record_conversion_result (struct coding_system
*coding
,
814 enum coding_result_code result
)
816 coding
->result
= result
;
819 case CODING_RESULT_INSUFFICIENT_SRC
:
820 Vlast_code_conversion_error
= Qinsufficient_source
;
822 case CODING_RESULT_INVALID_SRC
:
823 Vlast_code_conversion_error
= Qinvalid_source
;
825 case CODING_RESULT_INTERRUPT
:
826 Vlast_code_conversion_error
= Qinterrupted
;
828 case CODING_RESULT_INSUFFICIENT_DST
:
829 /* Don't record this error in Vlast_code_conversion_error
830 because it happens just temporarily and is resolved when the
831 whole conversion is finished. */
833 case CODING_RESULT_SUCCESS
:
836 Vlast_code_conversion_error
= intern ("Unknown error");
840 /* These wrapper macros are used to preserve validity of pointers into
841 buffer text across calls to decode_char, encode_char, etc, which
842 could cause relocation of buffers if it loads a charset map,
843 because loading a charset map allocates large structures. */
845 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
849 charset_map_loaded = 0; \
850 c = DECODE_CHAR (charset, code); \
851 if (charset_map_loaded \
852 && (offset = coding_change_source (coding))) \
855 src_base += offset; \
860 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
864 charset_map_loaded = 0; \
865 code = ENCODE_CHAR (charset, c); \
866 if (charset_map_loaded \
867 && (offset = coding_change_destination (coding))) \
874 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
878 charset_map_loaded = 0; \
879 charset = char_charset (c, charset_list, code_return); \
880 if (charset_map_loaded \
881 && (offset = coding_change_destination (coding))) \
888 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
892 charset_map_loaded = 0; \
893 result = CHAR_CHARSET_P (c, charset); \
894 if (charset_map_loaded \
895 && (offset = coding_change_destination (coding))) \
903 /* If there are at least BYTES length of room at dst, allocate memory
904 for coding->destination and update dst and dst_end. We don't have
905 to take care of coding->source which will be relocated. It is
906 handled by calling coding_set_source in encode_coding. */
908 #define ASSURE_DESTINATION(bytes) \
910 if (dst + (bytes) >= dst_end) \
912 ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
914 dst = alloc_destination (coding, more_bytes, dst); \
915 dst_end = coding->destination + coding->dst_bytes; \
920 /* Store multibyte form of the character C in P, and advance P to the
921 end of the multibyte form. This used to be like CHAR_STRING_ADVANCE
922 without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call
923 MAYBE_UNIFY_CHAR in CHAR_STRING_ADVANCE. */
925 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) CHAR_STRING_ADVANCE(c, p)
927 /* Return the character code of character whose multibyte form is at
928 P, and advance P to the end of the multibyte form. This used to be
929 like STRING_CHAR_ADVANCE without ever calling MAYBE_UNIFY_CHAR, but
930 nowadays STRING_CHAR_ADVANCE doesn't call MAYBE_UNIFY_CHAR. */
932 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) STRING_CHAR_ADVANCE(p)
934 /* Set coding->source from coding->src_object. */
937 coding_set_source (struct coding_system
*coding
)
939 if (BUFFERP (coding
->src_object
))
941 struct buffer
*buf
= XBUFFER (coding
->src_object
);
943 if (coding
->src_pos
< 0)
944 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
946 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
948 else if (STRINGP (coding
->src_object
))
950 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
954 /* Otherwise, the source is C string and is never relocated
955 automatically. Thus we don't have to update anything. */
960 /* Set coding->source from coding->src_object, and return how many
961 bytes coding->source was changed. */
964 coding_change_source (struct coding_system
*coding
)
966 const unsigned char *orig
= coding
->source
;
967 coding_set_source (coding
);
968 return coding
->source
- orig
;
972 /* Set coding->destination from coding->dst_object. */
975 coding_set_destination (struct coding_system
*coding
)
977 if (BUFFERP (coding
->dst_object
))
979 if (BUFFERP (coding
->src_object
) && coding
->src_pos
< 0)
981 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
982 coding
->dst_bytes
= (GAP_END_ADDR
983 - (coding
->src_bytes
- coding
->consumed
)
984 - coding
->destination
);
988 /* We are sure that coding->dst_pos_byte is before the gap
990 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
991 + coding
->dst_pos_byte
- BEG_BYTE
);
992 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
993 - coding
->destination
);
998 /* Otherwise, the destination is C string and is never relocated
999 automatically. Thus we don't have to update anything. */
1004 /* Set coding->destination from coding->dst_object, and return how
1005 many bytes coding->destination was changed. */
1008 coding_change_destination (struct coding_system
*coding
)
1010 const unsigned char *orig
= coding
->destination
;
1011 coding_set_destination (coding
);
1012 return coding
->destination
- orig
;
1017 coding_alloc_by_realloc (struct coding_system
*coding
, ptrdiff_t bytes
)
1019 if (STRING_BYTES_BOUND
- coding
->dst_bytes
< bytes
)
1021 coding
->destination
= xrealloc (coding
->destination
,
1022 coding
->dst_bytes
+ bytes
);
1023 coding
->dst_bytes
+= bytes
;
1027 coding_alloc_by_making_gap (struct coding_system
*coding
,
1028 ptrdiff_t gap_head_used
, ptrdiff_t bytes
)
1030 if (EQ (coding
->src_object
, coding
->dst_object
))
1032 /* The gap may contain the produced data at the head and not-yet
1033 consumed data at the tail. To preserve those data, we at
1034 first make the gap size to zero, then increase the gap
1036 ptrdiff_t add
= GAP_SIZE
;
1038 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1039 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1041 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1042 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1045 make_gap_1 (XBUFFER (coding
->dst_object
), bytes
);
1049 static unsigned char *
1050 alloc_destination (struct coding_system
*coding
, ptrdiff_t nbytes
,
1053 ptrdiff_t offset
= dst
- coding
->destination
;
1055 if (BUFFERP (coding
->dst_object
))
1057 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1059 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1062 coding_alloc_by_realloc (coding
, nbytes
);
1063 coding_set_destination (coding
);
1064 dst
= coding
->destination
+ offset
;
1068 /** Macros for annotations. */
1070 /* An annotation data is stored in the array coding->charbuf in this
1072 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1073 LENGTH is the number of elements in the annotation.
1074 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1075 NCHARS is the number of characters in the text annotated.
1077 The format of the following elements depend on ANNOTATION_MASK.
1079 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1081 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1083 NBYTES is the number of bytes specified in the header part of
1084 old-style emacs-mule encoding, or 0 for the other kind of
1087 METHOD is one of enum composition_method.
1089 Optional COMPOSITION-COMPONENTS are characters and composition
1092 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1095 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1096 recover from an invalid annotation, and should be skipped by
1097 produce_annotation. */
1099 /* Maximum length of the header of annotation data. */
1100 #define MAX_ANNOTATION_LENGTH 5
1102 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1104 *(buf)++ = -(len); \
1105 *(buf)++ = (mask); \
1106 *(buf)++ = (nchars); \
1107 coding->annotated = 1; \
1110 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1112 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1118 #define ADD_CHARSET_DATA(buf, nchars, id) \
1120 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
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
1147 detect_coding_utf_8 (struct coding_system
*coding
,
1148 struct coding_detection_info
*detect_info
)
1150 const unsigned char *src
= coding
->source
, *src_base
;
1151 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1152 bool multibytep
= coding
->src_multibyte
;
1153 ptrdiff_t consumed_chars
= 0;
1157 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1158 /* A coding system of this category is always ASCII compatible. */
1159 src
+= coding
->head_ascii
;
1163 int c
, c1
, c2
, c3
, c4
;
1167 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1170 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1172 if (UTF_8_2_OCTET_LEADING_P (c
))
1178 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1180 if (UTF_8_3_OCTET_LEADING_P (c
))
1183 if (src_base
== coding
->source
1184 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1189 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1191 if (UTF_8_4_OCTET_LEADING_P (c
))
1197 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1199 if (UTF_8_5_OCTET_LEADING_P (c
))
1206 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1210 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1212 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1217 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1218 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1222 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1224 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1231 decode_coding_utf_8 (struct coding_system
*coding
)
1233 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1234 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1235 const unsigned char *src_base
;
1236 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1237 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1238 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1239 bool multibytep
= coding
->src_multibyte
;
1240 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1242 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1243 int byte_after_cr
= -1;
1245 if (bom
!= utf_without_bom
)
1251 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1256 if (! UTF_8_EXTRA_OCTET_P (c2
))
1261 if (! UTF_8_EXTRA_OCTET_P (c3
))
1265 if ((c1
!= UTF_8_BOM_1
)
1266 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1269 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1274 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1278 int c
, c1
, c2
, c3
, c4
, c5
;
1281 consumed_chars_base
= consumed_chars
;
1283 if (charbuf
>= charbuf_end
)
1285 if (byte_after_cr
>= 0)
1290 if (byte_after_cr
>= 0)
1291 c1
= byte_after_cr
, byte_after_cr
= -1;
1298 else if (UTF_8_1_OCTET_P (c1
))
1300 if (eol_dos
&& c1
== '\r')
1301 ONE_MORE_BYTE (byte_after_cr
);
1307 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1309 if (UTF_8_2_OCTET_LEADING_P (c1
))
1311 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1312 /* Reject overlong sequences here and below. Encoders
1313 producing them are incorrect, they can be misleading,
1314 and they mess up read/write invariance. */
1321 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1323 if (UTF_8_3_OCTET_LEADING_P (c1
))
1325 c
= (((c1
& 0xF) << 12)
1326 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1328 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1334 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1336 if (UTF_8_4_OCTET_LEADING_P (c1
))
1338 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1339 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1346 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1348 if (UTF_8_5_OCTET_LEADING_P (c1
))
1350 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1351 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1353 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1368 consumed_chars
= consumed_chars_base
;
1370 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1375 coding
->consumed_char
+= consumed_chars_base
;
1376 coding
->consumed
= src_base
- coding
->source
;
1377 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1382 encode_coding_utf_8 (struct coding_system
*coding
)
1384 bool multibytep
= coding
->dst_multibyte
;
1385 int *charbuf
= coding
->charbuf
;
1386 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1387 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1388 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1389 ptrdiff_t produced_chars
= 0;
1392 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1394 ASSURE_DESTINATION (3);
1395 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1396 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1401 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1403 while (charbuf
< charbuf_end
)
1405 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1407 ASSURE_DESTINATION (safe_room
);
1409 if (CHAR_BYTE8_P (c
))
1411 c
= CHAR_TO_BYTE8 (c
);
1416 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1417 for (p
= str
; p
< pend
; p
++)
1424 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1426 while (charbuf
< charbuf_end
)
1428 ASSURE_DESTINATION (safe_room
);
1430 if (CHAR_BYTE8_P (c
))
1431 *dst
++ = CHAR_TO_BYTE8 (c
);
1433 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1437 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1438 coding
->produced_char
+= produced_chars
;
1439 coding
->produced
= dst
- coding
->destination
;
1444 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1445 Return true if a text is encoded in one of UTF-16 based coding systems. */
1447 #define UTF_16_HIGH_SURROGATE_P(val) \
1448 (((val) & 0xFC00) == 0xD800)
1450 #define UTF_16_LOW_SURROGATE_P(val) \
1451 (((val) & 0xFC00) == 0xDC00)
1455 detect_coding_utf_16 (struct coding_system
*coding
,
1456 struct coding_detection_info
*detect_info
)
1458 const unsigned char *src
= coding
->source
;
1459 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1460 bool multibytep
= coding
->src_multibyte
;
1463 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1464 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1465 && (coding
->src_chars
& 1))
1467 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1471 TWO_MORE_BYTES (c1
, c2
);
1472 if ((c1
== 0xFF) && (c2
== 0xFE))
1474 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1475 | CATEGORY_MASK_UTF_16_AUTO
);
1476 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1477 | CATEGORY_MASK_UTF_16_BE_NOSIG
1478 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1480 else if ((c1
== 0xFE) && (c2
== 0xFF))
1482 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1483 | CATEGORY_MASK_UTF_16_AUTO
);
1484 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1485 | CATEGORY_MASK_UTF_16_BE_NOSIG
1486 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1490 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1495 /* We check the dispersion of Eth and Oth bytes where E is even and
1496 O is odd. If both are high, we assume binary data.*/
1497 unsigned char e
[256], o
[256];
1498 unsigned e_num
= 1, o_num
= 1;
1505 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1506 |CATEGORY_MASK_UTF_16_BE
1507 | CATEGORY_MASK_UTF_16_LE
);
1509 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1510 != CATEGORY_MASK_UTF_16
)
1512 TWO_MORE_BYTES (c1
, c2
);
1520 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1527 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1538 decode_coding_utf_16 (struct coding_system
*coding
)
1540 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1541 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1542 const unsigned char *src_base
;
1543 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1544 /* We may produces at most 3 chars in one loop. */
1545 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1546 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1547 bool multibytep
= coding
->src_multibyte
;
1548 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1549 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1550 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1552 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1553 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1555 if (bom
== utf_with_bom
)
1564 if (endian
== utf_16_big_endian
1565 ? c
!= 0xFEFF : c
!= 0xFFFE)
1567 /* The first two bytes are not BOM. Treat them as bytes
1568 for a normal character. */
1572 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1574 else if (bom
== utf_detect_bom
)
1576 /* We have already tried to detect BOM and failed in
1578 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1586 consumed_chars_base
= consumed_chars
;
1588 if (charbuf
>= charbuf_end
)
1590 if (byte_after_cr1
>= 0)
1595 if (byte_after_cr1
>= 0)
1596 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1604 if (byte_after_cr2
>= 0)
1605 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1610 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1614 c
= (endian
== utf_16_big_endian
1615 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1619 if (! UTF_16_LOW_SURROGATE_P (c
))
1621 if (endian
== utf_16_big_endian
)
1622 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1624 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1628 if (UTF_16_HIGH_SURROGATE_P (c
))
1629 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1635 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1636 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1637 *charbuf
++ = 0x10000 + c
;
1642 if (UTF_16_HIGH_SURROGATE_P (c
))
1643 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1646 if (eol_dos
&& c
== '\r')
1648 ONE_MORE_BYTE (byte_after_cr1
);
1649 ONE_MORE_BYTE (byte_after_cr2
);
1657 coding
->consumed_char
+= consumed_chars_base
;
1658 coding
->consumed
= src_base
- coding
->source
;
1659 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1663 encode_coding_utf_16 (struct coding_system
*coding
)
1665 bool multibytep
= coding
->dst_multibyte
;
1666 int *charbuf
= coding
->charbuf
;
1667 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1668 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1669 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1671 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1672 bool big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1673 ptrdiff_t produced_chars
= 0;
1676 if (bom
!= utf_without_bom
)
1678 ASSURE_DESTINATION (safe_room
);
1680 EMIT_TWO_BYTES (0xFE, 0xFF);
1682 EMIT_TWO_BYTES (0xFF, 0xFE);
1683 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1686 while (charbuf
< charbuf_end
)
1688 ASSURE_DESTINATION (safe_room
);
1690 if (c
> MAX_UNICODE_CHAR
)
1691 c
= coding
->default_char
;
1696 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1698 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1705 c1
= (c
>> 10) + 0xD800;
1706 c2
= (c
& 0x3FF) + 0xDC00;
1708 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1710 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1713 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1714 coding
->produced
= dst
- coding
->destination
;
1715 coding
->produced_char
+= produced_chars
;
1720 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1722 /* Emacs' internal format for representation of multiple character
1723 sets is a kind of multi-byte encoding, i.e. characters are
1724 represented by variable-length sequences of one-byte codes.
1726 ASCII characters and control characters (e.g. `tab', `newline') are
1727 represented by one-byte sequences which are their ASCII codes, in
1728 the range 0x00 through 0x7F.
1730 8-bit characters of the range 0x80..0x9F are represented by
1731 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1734 8-bit characters of the range 0xA0..0xFF are represented by
1735 one-byte sequences which are their 8-bit code.
1737 The other characters are represented by a sequence of `base
1738 leading-code', optional `extended leading-code', and one or two
1739 `position-code's. The length of the sequence is determined by the
1740 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1741 whereas extended leading-code and position-code take the range 0xA0
1742 through 0xFF. See `charset.h' for more details about leading-code
1745 --- CODE RANGE of Emacs' internal format ---
1749 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1750 eight-bit-graphic 0xA0..0xBF
1751 ELSE 0x81..0x9D + [0xA0..0xFF]+
1752 ---------------------------------------------
1754 As this is the internal character representation, the format is
1755 usually not used externally (i.e. in a file or in a data sent to a
1756 process). But, it is possible to have a text externally in this
1757 format (i.e. by encoding by the coding system `emacs-mule').
1759 In that case, a sequence of one-byte codes has a slightly different
1762 At first, all characters in eight-bit-control are represented by
1763 one-byte sequences which are their 8-bit code.
1765 Next, character composition data are represented by the byte
1766 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1768 METHOD is 0xF2 plus one of composition method (enum
1769 composition_method),
1771 BYTES is 0xA0 plus a byte length of this composition data,
1773 CHARS is 0xA0 plus a number of characters composed by this
1776 COMPONENTs are characters of multibyte form or composition
1777 rules encoded by two-byte of ASCII codes.
1779 In addition, for backward compatibility, the following formats are
1780 also recognized as composition data on decoding.
1783 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1786 MSEQ is a multibyte form but in these special format:
1787 ASCII: 0xA0 ASCII_CODE+0x80,
1788 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1789 RULE is a one byte code of the range 0xA0..0xF0 that
1790 represents a composition rule.
1793 char emacs_mule_bytes
[256];
1796 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1797 Return true if a text is encoded in 'emacs-mule'. */
1800 detect_coding_emacs_mule (struct coding_system
*coding
,
1801 struct coding_detection_info
*detect_info
)
1803 const unsigned char *src
= coding
->source
, *src_base
;
1804 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1805 bool multibytep
= coding
->src_multibyte
;
1806 ptrdiff_t consumed_chars
= 0;
1810 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1811 /* A coding system of this category is always ASCII compatible. */
1812 src
+= coding
->head_ascii
;
1822 /* Perhaps the start of composite character. We simply skip
1823 it because analyzing it is too heavy for detecting. But,
1824 at least, we check that the composite character
1825 constitutes of more than 4 bytes. */
1826 const unsigned char *src_start
;
1836 if (src
- src_start
<= 4)
1838 found
= CATEGORY_MASK_EMACS_MULE
;
1846 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1851 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1853 while (more_bytes
> 0)
1858 src
--; /* Unread the last byte. */
1863 if (more_bytes
!= 0)
1865 found
= CATEGORY_MASK_EMACS_MULE
;
1868 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1872 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1874 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1877 detect_info
->found
|= found
;
1882 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1883 character. If CMP_STATUS indicates that we must expect MSEQ or
1884 RULE described above, decode it and return the negative value of
1885 the decoded character or rule. If an invalid byte is found, return
1886 -1. If SRC is too short, return -2. */
1889 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1890 int *nbytes
, int *nchars
, int *id
,
1891 struct composition_status
*cmp_status
)
1893 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1894 const unsigned char *src_base
= src
;
1895 bool multibytep
= coding
->src_multibyte
;
1899 int consumed_chars
= 0;
1900 bool mseq_found
= 0;
1906 charset_ID
= emacs_mule_charset
[0];
1912 if (cmp_status
->state
!= COMPOSING_NO
1913 && cmp_status
->old_form
)
1915 if (cmp_status
->state
== COMPOSING_CHAR
)
1930 *nbytes
= src
- src_base
;
1931 *nchars
= consumed_chars
;
1939 switch (emacs_mule_bytes
[c
])
1942 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
1951 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1952 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1955 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
1964 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
1969 code
= (c
& 0x7F) << 8;
1979 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
1984 code
= (c
& 0x7F) << 8;
1993 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
1999 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2000 CHARSET_FROM_ID (charset_ID
), code
, c
);
2004 *nbytes
= src
- src_base
;
2005 *nchars
= consumed_chars
;
2008 return (mseq_found
? -c
: c
);
2018 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2020 /* Handle these composition sequence ('|': the end of header elements,
2021 BYTES and CHARS >= 0xA0):
2023 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2024 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2025 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2029 (4) relative composition: 0x80 | MSEQ ... MSEQ
2030 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2032 When the starter 0x80 and the following header elements are found,
2033 this annotation header is produced.
2035 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2037 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2038 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2040 Then, upon reading the following elements, these codes are produced
2041 until the composition end is found:
2044 (2) ALT ... ALT CHAR ... CHAR
2045 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2047 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2049 When the composition end is found, LENGTH and NCHARS in the
2050 annotation header is updated as below:
2052 (1) LENGTH: unchanged, NCHARS: unchanged
2053 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2054 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2055 (4) LENGTH: unchanged, NCHARS: number of CHARs
2056 (5) LENGTH: unchanged, NCHARS: number of CHARs
2058 If an error is found while composing, the annotation header is
2059 changed to the original composition header (plus filler -1s) as
2062 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2063 (5) [ 0x80 0xFF -1 -1- -1 ]
2065 and the sequence [ -2 DECODED-RULE ] is changed to the original
2066 byte sequence as below:
2067 o the original byte sequence is B: [ B -1 ]
2068 o the original byte sequence is B1 B2: [ B1 B2 ]
2070 Most of the routines are implemented by macros because many
2071 variables and labels in the caller decode_coding_emacs_mule must be
2072 accessible, and they are usually called just once (thus doesn't
2073 increase the size of compiled object). */
2075 /* Decode a composition rule represented by C as a component of
2076 composition sequence of Emacs 20 style. Set RULE to the decoded
2079 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2084 if (c < 0 || c >= 81) \
2085 goto invalid_code; \
2086 gref = c / 9, nref = c % 9; \
2087 if (gref == 4) gref = 10; \
2088 if (nref == 4) nref = 10; \
2089 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2093 /* Decode a composition rule represented by C and the following byte
2094 at SRC as a component of composition sequence of Emacs 21 style.
2095 Set RULE to the decoded rule. */
2097 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2102 if (gref < 0 || gref >= 81) \
2103 goto invalid_code; \
2104 ONE_MORE_BYTE (c); \
2106 if (nref < 0 || nref >= 81) \
2107 goto invalid_code; \
2108 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2112 /* Start of Emacs 21 style format. The first three bytes at SRC are
2113 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2114 byte length of this composition information, CHARS is the number of
2115 characters composed by this composition. */
2117 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2119 enum composition_method method = c - 0xF2; \
2120 int nbytes, nchars; \
2122 ONE_MORE_BYTE (c); \
2124 goto invalid_code; \
2125 nbytes = c - 0xA0; \
2126 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2127 goto invalid_code; \
2128 ONE_MORE_BYTE (c); \
2129 nchars = c - 0xA0; \
2130 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2131 goto invalid_code; \
2132 cmp_status->old_form = 0; \
2133 cmp_status->method = method; \
2134 if (method == COMPOSITION_RELATIVE) \
2135 cmp_status->state = COMPOSING_CHAR; \
2137 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2138 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2139 cmp_status->nchars = nchars; \
2140 cmp_status->ncomps = nbytes - 4; \
2141 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2145 /* Start of Emacs 20 style format for relative composition. */
2147 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2149 cmp_status->old_form = 1; \
2150 cmp_status->method = COMPOSITION_RELATIVE; \
2151 cmp_status->state = COMPOSING_CHAR; \
2152 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2153 cmp_status->nchars = cmp_status->ncomps = 0; \
2154 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2158 /* Start of Emacs 20 style format for rule-base composition. */
2160 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2162 cmp_status->old_form = 1; \
2163 cmp_status->method = COMPOSITION_WITH_RULE; \
2164 cmp_status->state = COMPOSING_CHAR; \
2165 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2166 cmp_status->nchars = cmp_status->ncomps = 0; \
2167 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2171 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2173 const unsigned char *current_src = src; \
2175 ONE_MORE_BYTE (c); \
2177 goto invalid_code; \
2178 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2179 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2180 DECODE_EMACS_MULE_21_COMPOSITION (); \
2181 else if (c < 0xA0) \
2182 goto invalid_code; \
2183 else if (c < 0xC0) \
2185 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2186 /* Re-read C as a composition component. */ \
2187 src = current_src; \
2189 else if (c == 0xFF) \
2190 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2192 goto invalid_code; \
2195 #define EMACS_MULE_COMPOSITION_END() \
2197 int idx = - cmp_status->length; \
2199 if (cmp_status->old_form) \
2200 charbuf[idx + 2] = cmp_status->nchars; \
2201 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2202 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2203 cmp_status->state = COMPOSING_NO; \
2208 emacs_mule_finish_composition (int *charbuf
,
2209 struct composition_status
*cmp_status
)
2211 int idx
= - cmp_status
->length
;
2214 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2216 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2218 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2219 && cmp_status
->state
== COMPOSING_CHAR
)
2221 /* The last rule was invalid. */
2222 int rule
= charbuf
[-1] + 0xA0;
2224 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2231 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2233 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2235 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2236 charbuf
[idx
++] = -3;
2242 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2243 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2245 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2246 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2247 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2248 charbuf
[idx
++] = -1;
2252 cmp_status
->state
= COMPOSING_NO
;
2256 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2258 if (cmp_status->state != COMPOSING_NO) \
2259 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2264 decode_coding_emacs_mule (struct coding_system
*coding
)
2266 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2267 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2268 const unsigned char *src_base
;
2269 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2270 /* We may produce two annotations (charset and composition) in one
2271 loop and one more charset annotation at the end. */
2273 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2274 /* We can produce up to 2 characters in a loop. */
2276 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
2277 bool multibytep
= coding
->src_multibyte
;
2278 ptrdiff_t char_offset
= coding
->produced_char
;
2279 ptrdiff_t last_offset
= char_offset
;
2280 int last_id
= charset_ascii
;
2282 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2283 int byte_after_cr
= -1;
2284 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2286 if (cmp_status
->state
!= COMPOSING_NO
)
2290 if (charbuf_end
- charbuf
< cmp_status
->length
)
2292 for (i
= 0; i
< cmp_status
->length
; i
++)
2293 *charbuf
++ = cmp_status
->carryover
[i
];
2294 coding
->annotated
= 1;
2299 int c
, id
IF_LINT (= 0);
2302 consumed_chars_base
= consumed_chars
;
2304 if (charbuf
>= charbuf_end
)
2306 if (byte_after_cr
>= 0)
2311 if (byte_after_cr
>= 0)
2312 c
= byte_after_cr
, byte_after_cr
= -1;
2316 if (c
< 0 || c
== 0x80)
2318 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2325 DECODE_EMACS_MULE_COMPOSITION_START ();
2331 if (eol_dos
&& c
== '\r')
2332 ONE_MORE_BYTE (byte_after_cr
);
2334 if (cmp_status
->state
!= COMPOSING_NO
)
2336 if (cmp_status
->old_form
)
2337 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2338 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2339 cmp_status
->ncomps
--;
2344 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2345 /* emacs_mule_char can load a charset map from a file, which
2346 allocates a large structure and might cause buffer text
2347 to be relocated as result. Thus, we need to remember the
2348 original pointer to buffer text, and fix up all related
2349 pointers after the call. */
2350 const unsigned char *orig
= coding
->source
;
2353 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2355 offset
= coding
->source
- orig
;
2369 src
= src_base
+ nbytes
;
2370 consumed_chars
= consumed_chars_base
+ nchars
;
2371 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2372 cmp_status
->ncomps
-= nchars
;
2375 /* Now if C >= 0, we found a normally encoded character, if C <
2376 0, we found an old-style composition component character or
2379 if (cmp_status
->state
== COMPOSING_NO
)
2383 if (last_id
!= charset_ascii
)
2384 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2387 last_offset
= char_offset
;
2392 else if (cmp_status
->state
== COMPOSING_CHAR
)
2394 if (cmp_status
->old_form
)
2398 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2405 cmp_status
->nchars
++;
2406 cmp_status
->length
++;
2407 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2408 EMACS_MULE_COMPOSITION_END ();
2409 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2410 cmp_status
->state
= COMPOSING_RULE
;
2416 cmp_status
->length
++;
2417 cmp_status
->nchars
--;
2418 if (cmp_status
->nchars
== 0)
2419 EMACS_MULE_COMPOSITION_END ();
2422 else if (cmp_status
->state
== COMPOSING_RULE
)
2428 EMACS_MULE_COMPOSITION_END ();
2435 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2440 cmp_status
->length
+= 2;
2441 cmp_status
->state
= COMPOSING_CHAR
;
2444 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2447 cmp_status
->length
++;
2448 if (cmp_status
->ncomps
== 0)
2449 cmp_status
->state
= COMPOSING_CHAR
;
2450 else if (cmp_status
->ncomps
> 0)
2452 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2453 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2456 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2458 else /* COMPOSING_COMPONENT_RULE */
2462 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2467 cmp_status
->length
+= 2;
2468 cmp_status
->ncomps
--;
2469 if (cmp_status
->ncomps
> 0)
2470 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2472 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2477 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2479 consumed_chars
= consumed_chars_base
;
2481 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2487 if (cmp_status
->state
!= COMPOSING_NO
)
2489 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2490 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2495 charbuf
-= cmp_status
->length
;
2496 for (i
= 0; i
< cmp_status
->length
; i
++)
2497 cmp_status
->carryover
[i
] = charbuf
[i
];
2500 if (last_id
!= charset_ascii
)
2501 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2502 coding
->consumed_char
+= consumed_chars_base
;
2503 coding
->consumed
= src_base
- coding
->source
;
2504 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2508 #define EMACS_MULE_LEADING_CODES(id, codes) \
2511 codes[0] = id, codes[1] = 0; \
2512 else if (id < 0xE0) \
2513 codes[0] = 0x9A, codes[1] = id; \
2514 else if (id < 0xF0) \
2515 codes[0] = 0x9B, codes[1] = id; \
2516 else if (id < 0xF5) \
2517 codes[0] = 0x9C, codes[1] = id; \
2519 codes[0] = 0x9D, codes[1] = id; \
2524 encode_coding_emacs_mule (struct coding_system
*coding
)
2526 bool multibytep
= coding
->dst_multibyte
;
2527 int *charbuf
= coding
->charbuf
;
2528 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2529 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2530 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2532 ptrdiff_t produced_chars
= 0;
2533 Lisp_Object attrs
, charset_list
;
2535 int preferred_charset_id
= -1;
2537 CODING_GET_INFO (coding
, attrs
, charset_list
);
2538 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2540 charset_list
= Vemacs_mule_charset_list
;
2541 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2544 while (charbuf
< charbuf_end
)
2546 ASSURE_DESTINATION (safe_room
);
2551 /* Handle an annotation. */
2554 case CODING_ANNOTATE_COMPOSITION_MASK
:
2555 /* Not yet implemented. */
2557 case CODING_ANNOTATE_CHARSET_MASK
:
2558 preferred_charset_id
= charbuf
[3];
2559 if (preferred_charset_id
>= 0
2560 && NILP (Fmemq (make_number (preferred_charset_id
),
2562 preferred_charset_id
= -1;
2571 if (ASCII_CHAR_P (c
))
2572 EMIT_ONE_ASCII_BYTE (c
);
2573 else if (CHAR_BYTE8_P (c
))
2575 c
= CHAR_TO_BYTE8 (c
);
2580 struct charset
*charset
;
2584 unsigned char leading_codes
[2];
2586 if (preferred_charset_id
>= 0)
2590 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2591 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2593 code
= ENCODE_CHAR (charset
, c
);
2595 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2599 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2603 c
= coding
->default_char
;
2604 if (ASCII_CHAR_P (c
))
2606 EMIT_ONE_ASCII_BYTE (c
);
2609 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2612 dimension
= CHARSET_DIMENSION (charset
);
2613 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2614 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2615 EMIT_ONE_BYTE (leading_codes
[0]);
2616 if (leading_codes
[1])
2617 EMIT_ONE_BYTE (leading_codes
[1]);
2619 EMIT_ONE_BYTE (code
| 0x80);
2623 EMIT_ONE_BYTE (code
>> 8);
2624 EMIT_ONE_BYTE (code
& 0xFF);
2628 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2629 coding
->produced_char
+= produced_chars
;
2630 coding
->produced
= dst
- coding
->destination
;
2635 /*** 7. ISO2022 handlers ***/
2637 /* The following note describes the coding system ISO2022 briefly.
2638 Since the intention of this note is to help understand the
2639 functions in this file, some parts are NOT ACCURATE or are OVERLY
2640 SIMPLIFIED. For thorough understanding, please refer to the
2641 original document of ISO2022. This is equivalent to the standard
2642 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2644 ISO2022 provides many mechanisms to encode several character sets
2645 in 7-bit and 8-bit environments. For 7-bit environments, all text
2646 is encoded using bytes less than 128. This may make the encoded
2647 text a little bit longer, but the text passes more easily through
2648 several types of gateway, some of which strip off the MSB (Most
2651 There are two kinds of character sets: control character sets and
2652 graphic character sets. The former contain control characters such
2653 as `newline' and `escape' to provide control functions (control
2654 functions are also provided by escape sequences). The latter
2655 contain graphic characters such as 'A' and '-'. Emacs recognizes
2656 two control character sets and many graphic character sets.
2658 Graphic character sets are classified into one of the following
2659 four classes, according to the number of bytes (DIMENSION) and
2660 number of characters in one dimension (CHARS) of the set:
2661 - DIMENSION1_CHARS94
2662 - DIMENSION1_CHARS96
2663 - DIMENSION2_CHARS94
2664 - DIMENSION2_CHARS96
2666 In addition, each character set is assigned an identification tag,
2667 unique for each set, called the "final character" (denoted as <F>
2668 hereafter). The <F> of each character set is decided by ECMA(*)
2669 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2670 (0x30..0x3F are for private use only).
2672 Note (*): ECMA = European Computer Manufacturers Association
2674 Here are examples of graphic character sets [NAME(<F>)]:
2675 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2676 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2677 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2678 o DIMENSION2_CHARS96 -- none for the moment
2680 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2681 C0 [0x00..0x1F] -- control character plane 0
2682 GL [0x20..0x7F] -- graphic character plane 0
2683 C1 [0x80..0x9F] -- control character plane 1
2684 GR [0xA0..0xFF] -- graphic character plane 1
2686 A control character set is directly designated and invoked to C0 or
2687 C1 by an escape sequence. The most common case is that:
2688 - ISO646's control character set is designated/invoked to C0, and
2689 - ISO6429's control character set is designated/invoked to C1,
2690 and usually these designations/invocations are omitted in encoded
2691 text. In a 7-bit environment, only C0 can be used, and a control
2692 character for C1 is encoded by an appropriate escape sequence to
2693 fit into the environment. All control characters for C1 are
2694 defined to have corresponding escape sequences.
2696 A graphic character set is at first designated to one of four
2697 graphic registers (G0 through G3), then these graphic registers are
2698 invoked to GL or GR. These designations and invocations can be
2699 done independently. The most common case is that G0 is invoked to
2700 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2701 these invocations and designations are omitted in encoded text.
2702 In a 7-bit environment, only GL can be used.
2704 When a graphic character set of CHARS94 is invoked to GL, codes
2705 0x20 and 0x7F of the GL area work as control characters SPACE and
2706 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2709 There are two ways of invocation: locking-shift and single-shift.
2710 With locking-shift, the invocation lasts until the next different
2711 invocation, whereas with single-shift, the invocation affects the
2712 following character only and doesn't affect the locking-shift
2713 state. Invocations are done by the following control characters or
2716 ----------------------------------------------------------------------
2717 abbrev function cntrl escape seq description
2718 ----------------------------------------------------------------------
2719 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2720 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2721 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2722 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2723 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2724 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2725 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2726 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2727 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2728 ----------------------------------------------------------------------
2729 (*) These are not used by any known coding system.
2731 Control characters for these functions are defined by macros
2732 ISO_CODE_XXX in `coding.h'.
2734 Designations are done by the following escape sequences:
2735 ----------------------------------------------------------------------
2736 escape sequence description
2737 ----------------------------------------------------------------------
2738 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2739 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2740 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2741 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2742 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2743 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2744 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2745 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2746 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2747 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2748 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2749 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2750 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2751 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2752 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2753 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2754 ----------------------------------------------------------------------
2756 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2757 of dimension 1, chars 94, and final character <F>, etc...
2759 Note (*): Although these designations are not allowed in ISO2022,
2760 Emacs accepts them on decoding, and produces them on encoding
2761 CHARS96 character sets in a coding system which is characterized as
2762 7-bit environment, non-locking-shift, and non-single-shift.
2764 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2765 '(' must be omitted. We refer to this as "short-form" hereafter.
2767 Now you may notice that there are a lot of ways of encoding the
2768 same multilingual text in ISO2022. Actually, there exist many
2769 coding systems such as Compound Text (used in X11's inter client
2770 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2771 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2772 localized platforms), and all of these are variants of ISO2022.
2774 In addition to the above, Emacs handles two more kinds of escape
2775 sequences: ISO6429's direction specification and Emacs' private
2776 sequence for specifying character composition.
2778 ISO6429's direction specification takes the following form:
2779 o CSI ']' -- end of the current direction
2780 o CSI '0' ']' -- end of the current direction
2781 o CSI '1' ']' -- start of left-to-right text
2782 o CSI '2' ']' -- start of right-to-left text
2783 The control character CSI (0x9B: control sequence introducer) is
2784 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2786 Character composition specification takes the following form:
2787 o ESC '0' -- start relative composition
2788 o ESC '1' -- end composition
2789 o ESC '2' -- start rule-base composition (*)
2790 o ESC '3' -- start relative composition with alternate chars (**)
2791 o ESC '4' -- start rule-base composition with alternate chars (**)
2792 Since these are not standard escape sequences of any ISO standard,
2793 the use of them with these meanings is restricted to Emacs only.
2795 (*) This form is used only in Emacs 20.7 and older versions,
2796 but newer versions can safely decode it.
2797 (**) This form is used only in Emacs 21.1 and newer versions,
2798 and older versions can't decode it.
2800 Here's a list of example usages of these composition escape
2801 sequences (categorized by `enum composition_method').
2803 COMPOSITION_RELATIVE:
2804 ESC 0 CHAR [ CHAR ] ESC 1
2805 COMPOSITION_WITH_RULE:
2806 ESC 2 CHAR [ RULE CHAR ] ESC 1
2807 COMPOSITION_WITH_ALTCHARS:
2808 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2809 COMPOSITION_WITH_RULE_ALTCHARS:
2810 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2812 static enum iso_code_class_type iso_code_class
[256];
2814 #define SAFE_CHARSET_P(coding, id) \
2815 ((id) <= (coding)->max_charset_id \
2816 && (coding)->safe_charsets[id] != 255)
2819 setup_iso_safe_charsets (Lisp_Object attrs
)
2821 Lisp_Object charset_list
, safe_charsets
;
2822 Lisp_Object request
;
2823 Lisp_Object reg_usage
;
2825 EMACS_INT reg94
, reg96
;
2826 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2829 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2830 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2831 && ! EQ (charset_list
, Viso_2022_charset_list
))
2833 charset_list
= Viso_2022_charset_list
;
2834 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2835 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2838 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2842 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2844 int id
= XINT (XCAR (tail
));
2845 if (max_charset_id
< id
)
2846 max_charset_id
= id
;
2849 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2850 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2851 request
= AREF (attrs
, coding_attr_iso_request
);
2852 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2853 reg94
= XINT (XCAR (reg_usage
));
2854 reg96
= XINT (XCDR (reg_usage
));
2856 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2860 struct charset
*charset
;
2863 charset
= CHARSET_FROM_ID (XINT (id
));
2864 reg
= Fcdr (Fassq (id
, request
));
2866 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2867 else if (charset
->iso_chars_96
)
2870 SSET (safe_charsets
, XINT (id
), reg96
);
2875 SSET (safe_charsets
, XINT (id
), reg94
);
2878 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2882 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2883 Return true if a text is encoded in one of ISO-2022 based coding
2887 detect_coding_iso_2022 (struct coding_system
*coding
,
2888 struct coding_detection_info
*detect_info
)
2890 const unsigned char *src
= coding
->source
, *src_base
= src
;
2891 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2892 bool multibytep
= coding
->src_multibyte
;
2893 bool single_shifting
= 0;
2896 ptrdiff_t consumed_chars
= 0;
2900 int composition_count
= -1;
2902 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2904 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2906 struct coding_system
*this = &(coding_categories
[i
]);
2907 Lisp_Object attrs
, val
;
2911 attrs
= CODING_ID_ATTRS (this->id
);
2912 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2913 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2914 setup_iso_safe_charsets (attrs
);
2915 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2916 this->max_charset_id
= SCHARS (val
) - 1;
2917 this->safe_charsets
= SDATA (val
);
2920 /* A coding system of this category is always ASCII compatible. */
2921 src
+= coding
->head_ascii
;
2923 while (rejected
!= CATEGORY_MASK_ISO
)
2930 if (inhibit_iso_escape_detection
)
2932 single_shifting
= 0;
2934 if (c
== 'N' || c
== 'O')
2936 /* ESC <Fe> for SS2 or SS3. */
2937 single_shifting
= 1;
2938 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2942 /* End of composition. */
2943 if (composition_count
< 0
2944 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
2947 composition_count
= -1;
2948 found
|= CATEGORY_MASK_ISO
;
2950 else if (c
>= '0' && c
<= '4')
2952 /* ESC <Fp> for start/end composition. */
2953 composition_count
= 0;
2957 if (c
>= '(' && c
<= '/')
2959 /* Designation sequence for a charset of dimension 1. */
2961 if (c1
< ' ' || c1
>= 0x80
2962 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2963 /* Invalid designation sequence. Just ignore. */
2968 /* Designation sequence for a charset of dimension 2. */
2970 if (c
>= '@' && c
<= 'B')
2971 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2972 id
= iso_charset_table
[1][0][c
];
2973 else if (c
>= '(' && c
<= '/')
2976 if (c1
< ' ' || c1
>= 0x80
2977 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2978 /* Invalid designation sequence. Just ignore. */
2982 /* Invalid designation sequence. Just ignore it. */
2987 /* Invalid escape sequence. Just ignore it. */
2991 /* We found a valid designation sequence for CHARSET. */
2992 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2993 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2995 found
|= CATEGORY_MASK_ISO_7
;
2997 rejected
|= CATEGORY_MASK_ISO_7
;
2998 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3000 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3002 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3003 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3005 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3007 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3008 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3010 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3012 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3018 /* Locking shift out/in. */
3019 if (inhibit_iso_escape_detection
)
3021 single_shifting
= 0;
3022 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3026 /* Control sequence introducer. */
3027 single_shifting
= 0;
3028 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3029 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3030 goto check_extra_latin
;
3035 if (inhibit_iso_escape_detection
)
3037 single_shifting
= 0;
3038 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3039 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3040 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3042 found
|= CATEGORY_MASK_ISO_8_1
;
3043 single_shifting
= 1;
3045 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3046 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3048 found
|= CATEGORY_MASK_ISO_8_2
;
3049 single_shifting
= 1;
3051 if (single_shifting
)
3053 goto check_extra_latin
;
3060 if (composition_count
>= 0)
3061 composition_count
++;
3062 single_shifting
= 0;
3067 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3068 found
|= CATEGORY_MASK_ISO_8_1
;
3069 /* Check the length of succeeding codes of the range
3070 0xA0..0FF. If the byte length is even, we include
3071 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3072 only when we are not single shifting. */
3073 if (! single_shifting
3074 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3077 while (src
< src_end
)
3089 if (len
& 1 && src
< src_end
)
3091 rejected
|= CATEGORY_MASK_ISO_8_2
;
3092 if (composition_count
>= 0)
3093 composition_count
+= len
;
3097 found
|= CATEGORY_MASK_ISO_8_2
;
3098 if (composition_count
>= 0)
3099 composition_count
+= len
/ 2;
3105 if (! VECTORP (Vlatin_extra_code_table
)
3106 || NILP (AREF (Vlatin_extra_code_table
, c
)))
3108 rejected
= CATEGORY_MASK_ISO
;
3111 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3112 & CODING_ISO_FLAG_LATIN_EXTRA
)
3113 found
|= CATEGORY_MASK_ISO_8_1
;
3115 rejected
|= CATEGORY_MASK_ISO_8_1
;
3116 rejected
|= CATEGORY_MASK_ISO_8_2
;
3120 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3124 detect_info
->rejected
|= rejected
;
3125 detect_info
->found
|= (found
& ~rejected
);
3130 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3131 escape sequence should be kept. */
3132 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3136 if (final < '0' || final >= 128 \
3137 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3138 || !SAFE_CHARSET_P (coding, id)) \
3140 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3144 prev = CODING_ISO_DESIGNATION (coding, reg); \
3145 if (id == charset_jisx0201_roman) \
3147 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3148 id = charset_ascii; \
3150 else if (id == charset_jisx0208_1978) \
3152 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3153 id = charset_jisx0208; \
3155 CODING_ISO_DESIGNATION (coding, reg) = id; \
3156 /* If there was an invalid designation to REG previously, and this \
3157 designation is ASCII to REG, we should keep this designation \
3159 if (prev == -2 && id == charset_ascii) \
3164 /* Handle these composition sequence (ALT: alternate char):
3166 (1) relative composition: ESC 0 CHAR ... ESC 1
3167 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3168 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3169 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3171 When the start sequence (ESC 0/2/3/4) is found, this annotation
3174 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3176 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3177 produced until the end sequence (ESC 1) is found:
3180 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3181 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3182 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3184 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3185 annotation header is updated as below:
3187 (1) LENGTH: unchanged, NCHARS: number of CHARs
3188 (2) LENGTH: unchanged, NCHARS: number of CHARs
3189 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3190 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3192 If an error is found while composing, the annotation header is
3195 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3197 and the sequence [ -2 DECODED-RULE ] is changed to the original
3198 byte sequence as below:
3199 o the original byte sequence is B: [ B -1 ]
3200 o the original byte sequence is B1 B2: [ B1 B2 ]
3201 and the sequence [ -1 -1 ] is changed to the original byte
3206 /* Decode a composition rule C1 and maybe one more byte from the
3207 source, and set RULE to the encoded composition rule. If the rule
3208 is invalid, goto invalid_code. */
3210 #define DECODE_COMPOSITION_RULE(rule) \
3214 goto invalid_code; \
3215 if (rule < 81) /* old format (before ver.21) */ \
3217 int gref = (rule) / 9; \
3218 int nref = (rule) % 9; \
3219 if (gref == 4) gref = 10; \
3220 if (nref == 4) nref = 10; \
3221 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3223 else /* new format (after ver.21) */ \
3227 ONE_MORE_BYTE (b); \
3228 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3229 goto invalid_code; \
3230 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3231 rule += 0x100; /* Distinguish it from the old format. */ \
3235 #define ENCODE_COMPOSITION_RULE(rule) \
3237 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3239 if (rule < 0x100) /* old format */ \
3241 if (gref == 10) gref = 4; \
3242 if (nref == 10) nref = 4; \
3243 charbuf[idx] = 32 + gref * 9 + nref; \
3244 charbuf[idx + 1] = -1; \
3247 else /* new format */ \
3249 charbuf[idx] = 32 + 81 + gref; \
3250 charbuf[idx + 1] = 32 + nref; \
3255 /* Finish the current composition as invalid. */
3258 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3260 int idx
= - cmp_status
->length
;
3263 /* Recover the original ESC sequence */
3264 charbuf
[idx
++] = ISO_CODE_ESC
;
3265 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3266 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3267 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3268 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3270 charbuf
[idx
++] = -2;
3272 charbuf
[idx
++] = -1;
3273 new_chars
= cmp_status
->nchars
;
3274 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3275 for (; idx
< 0; idx
++)
3277 int elt
= charbuf
[idx
];
3281 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3286 charbuf
[idx
++] = ISO_CODE_ESC
;
3291 cmp_status
->state
= COMPOSING_NO
;
3295 /* If characters are under composition, finish the composition. */
3296 #define MAYBE_FINISH_COMPOSITION() \
3298 if (cmp_status->state != COMPOSING_NO) \
3299 char_offset += finish_composition (charbuf, cmp_status); \
3302 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3304 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3305 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3306 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3307 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3309 Produce this annotation sequence now:
3311 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3314 #define DECODE_COMPOSITION_START(c1) \
3317 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3318 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3319 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3320 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3324 cmp_status->state = COMPOSING_CHAR; \
3325 cmp_status->length += 2; \
3329 MAYBE_FINISH_COMPOSITION (); \
3330 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3331 : c1 == '2' ? COMPOSITION_WITH_RULE \
3332 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3333 : COMPOSITION_WITH_RULE_ALTCHARS); \
3335 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3336 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3337 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3338 cmp_status->nchars = cmp_status->ncomps = 0; \
3339 coding->annotated = 1; \
3344 /* Handle composition end sequence ESC 1. */
3346 #define DECODE_COMPOSITION_END() \
3348 if (cmp_status->nchars == 0 \
3349 || ((cmp_status->state == COMPOSING_CHAR) \
3350 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3352 MAYBE_FINISH_COMPOSITION (); \
3353 goto invalid_code; \
3355 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3356 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3357 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3358 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3359 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3360 char_offset += cmp_status->nchars; \
3361 cmp_status->state = COMPOSING_NO; \
3364 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3366 #define STORE_COMPOSITION_RULE(rule) \
3369 *charbuf++ = rule; \
3370 cmp_status->length += 2; \
3371 cmp_status->state--; \
3374 /* Store a composed char or a component char C in charbuf, and update
3377 #define STORE_COMPOSITION_CHAR(c) \
3380 cmp_status->length++; \
3381 if (cmp_status->state == COMPOSING_CHAR) \
3382 cmp_status->nchars++; \
3384 cmp_status->ncomps++; \
3385 if (cmp_status->method == COMPOSITION_WITH_RULE \
3386 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3387 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3388 cmp_status->state++; \
3392 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3395 decode_coding_iso_2022 (struct coding_system
*coding
)
3397 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3398 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3399 const unsigned char *src_base
;
3400 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3401 /* We may produce two annotations (charset and composition) in one
3402 loop and one more charset annotation at the end. */
3404 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3405 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
3406 bool multibytep
= coding
->src_multibyte
;
3407 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3408 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3409 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3410 int charset_id_2
, charset_id_3
;
3411 struct charset
*charset
;
3413 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3414 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3415 ptrdiff_t char_offset
= coding
->produced_char
;
3416 ptrdiff_t last_offset
= char_offset
;
3417 int last_id
= charset_ascii
;
3419 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3420 int byte_after_cr
= -1;
3423 setup_iso_safe_charsets (attrs
);
3424 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3426 if (cmp_status
->state
!= COMPOSING_NO
)
3428 if (charbuf_end
- charbuf
< cmp_status
->length
)
3430 for (i
= 0; i
< cmp_status
->length
; i
++)
3431 *charbuf
++ = cmp_status
->carryover
[i
];
3432 coding
->annotated
= 1;
3440 consumed_chars_base
= consumed_chars
;
3442 if (charbuf
>= charbuf_end
)
3444 if (byte_after_cr
>= 0)
3449 if (byte_after_cr
>= 0)
3450 c1
= byte_after_cr
, byte_after_cr
= -1;
3456 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3458 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3460 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3464 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3466 if (c1
== ISO_CODE_ESC
)
3468 if (src
+ 1 >= src_end
)
3469 goto no_more_source
;
3470 *charbuf
++ = ISO_CODE_ESC
;
3472 if (src
[0] == '%' && src
[1] == '@')
3475 consumed_chars
+= 2;
3477 /* We are sure charbuf can contain two more chars. */
3480 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3485 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3491 if ((cmp_status
->state
== COMPOSING_RULE
3492 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3493 && c1
!= ISO_CODE_ESC
)
3497 DECODE_COMPOSITION_RULE (rule
);
3498 STORE_COMPOSITION_RULE (rule
);
3502 /* We produce at most one character. */
3503 switch (iso_code_class
[c1
])
3505 case ISO_0x20_or_0x7F
:
3506 if (charset_id_0
< 0
3507 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3508 /* This is SPACE or DEL. */
3509 charset
= CHARSET_FROM_ID (charset_ascii
);
3511 charset
= CHARSET_FROM_ID (charset_id_0
);
3514 case ISO_graphic_plane_0
:
3515 if (charset_id_0
< 0)
3516 charset
= CHARSET_FROM_ID (charset_ascii
);
3518 charset
= CHARSET_FROM_ID (charset_id_0
);
3521 case ISO_0xA0_or_0xFF
:
3522 if (charset_id_1
< 0
3523 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3524 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3526 /* This is a graphic character, we fall down ... */
3528 case ISO_graphic_plane_1
:
3529 if (charset_id_1
< 0)
3531 charset
= CHARSET_FROM_ID (charset_id_1
);
3535 if (eol_dos
&& c1
== '\r')
3536 ONE_MORE_BYTE (byte_after_cr
);
3537 MAYBE_FINISH_COMPOSITION ();
3538 charset
= CHARSET_FROM_ID (charset_ascii
);
3545 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3546 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3548 CODING_ISO_INVOCATION (coding
, 0) = 1;
3549 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3553 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3555 CODING_ISO_INVOCATION (coding
, 0) = 0;
3556 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3559 case ISO_single_shift_2_7
:
3560 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3562 case ISO_single_shift_2
:
3563 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3565 /* SS2 is handled as an escape sequence of ESC 'N' */
3567 goto label_escape_sequence
;
3569 case ISO_single_shift_3
:
3570 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3572 /* SS2 is handled as an escape sequence of ESC 'O' */
3574 goto label_escape_sequence
;
3576 case ISO_control_sequence_introducer
:
3577 /* CSI is handled as an escape sequence of ESC '[' ... */
3579 goto label_escape_sequence
;
3583 label_escape_sequence
:
3584 /* Escape sequences handled here are invocation,
3585 designation, direction specification, and character
3586 composition specification. */
3589 case '&': /* revision of following character set */
3591 if (!(c1
>= '@' && c1
<= '~'))
3594 if (c1
!= ISO_CODE_ESC
)
3597 goto label_escape_sequence
;
3599 case '$': /* designation of 2-byte character set */
3600 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3606 if (c1
>= '@' && c1
<= 'B')
3607 { /* designation of JISX0208.1978, GB2312.1980,
3609 reg
= 0, chars96
= 0;
3611 else if (c1
>= 0x28 && c1
<= 0x2B)
3612 { /* designation of DIMENSION2_CHARS94 character set */
3613 reg
= c1
- 0x28, chars96
= 0;
3616 else if (c1
>= 0x2C && c1
<= 0x2F)
3617 { /* designation of DIMENSION2_CHARS96 character set */
3618 reg
= c1
- 0x2C, chars96
= 1;
3623 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3624 /* We must update these variables now. */
3626 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3628 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3634 case 'n': /* invocation of locking-shift-2 */
3635 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3636 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3638 CODING_ISO_INVOCATION (coding
, 0) = 2;
3639 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3642 case 'o': /* invocation of locking-shift-3 */
3643 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3644 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3646 CODING_ISO_INVOCATION (coding
, 0) = 3;
3647 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3650 case 'N': /* invocation of single-shift-2 */
3651 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3652 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3654 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3655 if (charset_id_2
< 0)
3656 charset
= CHARSET_FROM_ID (charset_ascii
);
3658 charset
= CHARSET_FROM_ID (charset_id_2
);
3660 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3664 case 'O': /* invocation of single-shift-3 */
3665 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3666 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3668 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3669 if (charset_id_3
< 0)
3670 charset
= CHARSET_FROM_ID (charset_ascii
);
3672 charset
= CHARSET_FROM_ID (charset_id_3
);
3674 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3678 case '0': case '2': case '3': case '4': /* start composition */
3679 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3681 if (last_id
!= charset_ascii
)
3683 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3684 last_id
= charset_ascii
;
3685 last_offset
= char_offset
;
3687 DECODE_COMPOSITION_START (c1
);
3690 case '1': /* end composition */
3691 if (cmp_status
->state
== COMPOSING_NO
)
3693 DECODE_COMPOSITION_END ();
3696 case '[': /* specification of direction */
3697 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3699 /* For the moment, nested direction is not supported.
3700 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3701 left-to-right, and nonzero means right-to-left. */
3705 case ']': /* end of the current direction */
3706 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3708 case '0': /* end of the current direction */
3709 case '1': /* start of left-to-right direction */
3712 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3717 case '2': /* start of right-to-left direction */
3720 coding
->mode
|= CODING_MODE_DIRECTION
;
3734 /* CTEXT extended segment:
3735 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3736 We keep these bytes as is for the moment.
3737 They may be decoded by post-read-conversion. */
3741 ONE_MORE_BYTE (dim
);
3742 if (dim
< '0' || dim
> '4')
3750 size
= ((M
- 128) * 128) + (L
- 128);
3751 if (charbuf
+ 6 > charbuf_end
)
3753 *charbuf
++ = ISO_CODE_ESC
;
3757 *charbuf
++ = BYTE8_TO_CHAR (M
);
3758 *charbuf
++ = BYTE8_TO_CHAR (L
);
3759 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3763 /* XFree86 extension for embedding UTF-8 in CTEXT:
3764 ESC % G --UTF-8-BYTES-- ESC % @
3765 We keep these bytes as is for the moment.
3766 They may be decoded by post-read-conversion. */
3767 if (charbuf
+ 3 > charbuf_end
)
3769 *charbuf
++ = ISO_CODE_ESC
;
3772 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3780 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3785 if (c1
>= 0x28 && c1
<= 0x2B)
3786 { /* designation of DIMENSION1_CHARS94 character set */
3787 reg
= c1
- 0x28, chars96
= 0;
3790 else if (c1
>= 0x2C && c1
<= 0x2F)
3791 { /* designation of DIMENSION1_CHARS96 character set */
3792 reg
= c1
- 0x2C, chars96
= 1;
3797 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3798 /* We must update these variables now. */
3800 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3802 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3814 if (cmp_status
->state
== COMPOSING_NO
3815 && charset
->id
!= charset_ascii
3816 && last_id
!= charset
->id
)
3818 if (last_id
!= charset_ascii
)
3819 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3820 last_id
= charset
->id
;
3821 last_offset
= char_offset
;
3824 /* Now we know CHARSET and 1st position code C1 of a character.
3825 Produce a decoded character while getting 2nd and 3rd
3826 position codes C2, C3 if necessary. */
3827 if (CHARSET_DIMENSION (charset
) > 1)
3830 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3831 || ((c1
& 0x80) != (c2
& 0x80)))
3832 /* C2 is not in a valid range. */
3834 if (CHARSET_DIMENSION (charset
) == 2)
3835 c1
= (c1
<< 8) | c2
;
3839 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3840 || ((c1
& 0x80) != (c3
& 0x80)))
3841 /* C3 is not in a valid range. */
3843 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3847 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3850 MAYBE_FINISH_COMPOSITION ();
3851 for (; src_base
< src
; src_base
++, char_offset
++)
3853 if (ASCII_BYTE_P (*src_base
))
3854 *charbuf
++ = *src_base
;
3856 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3859 else if (cmp_status
->state
== COMPOSING_NO
)
3864 else if ((cmp_status
->state
== COMPOSING_CHAR
3865 ? cmp_status
->nchars
3866 : cmp_status
->ncomps
)
3867 >= MAX_COMPOSITION_COMPONENTS
)
3869 /* Too long composition. */
3870 MAYBE_FINISH_COMPOSITION ();
3875 STORE_COMPOSITION_CHAR (c
);
3879 MAYBE_FINISH_COMPOSITION ();
3881 consumed_chars
= consumed_chars_base
;
3883 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3893 if (cmp_status
->state
!= COMPOSING_NO
)
3895 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3896 MAYBE_FINISH_COMPOSITION ();
3899 charbuf
-= cmp_status
->length
;
3900 for (i
= 0; i
< cmp_status
->length
; i
++)
3901 cmp_status
->carryover
[i
] = charbuf
[i
];
3904 else if (last_id
!= charset_ascii
)
3905 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3906 coding
->consumed_char
+= consumed_chars_base
;
3907 coding
->consumed
= src_base
- coding
->source
;
3908 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3912 /* ISO2022 encoding stuff. */
3915 It is not enough to say just "ISO2022" on encoding, we have to
3916 specify more details. In Emacs, each coding system of ISO2022
3917 variant has the following specifications:
3918 1. Initial designation to G0 thru G3.
3919 2. Allows short-form designation?
3920 3. ASCII should be designated to G0 before control characters?
3921 4. ASCII should be designated to G0 at end of line?
3922 5. 7-bit environment or 8-bit environment?
3923 6. Use locking-shift?
3924 7. Use Single-shift?
3925 And the following two are only for Japanese:
3926 8. Use ASCII in place of JIS0201-1976-Roman?
3927 9. Use JISX0208-1983 in place of JISX0208-1978?
3928 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3929 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3933 /* Produce codes (escape sequence) for designating CHARSET to graphic
3934 register REG at DST, and increment DST. If <final-char> of CHARSET is
3935 '@', 'A', or 'B' and the coding system CODING allows, produce
3936 designation sequence of short-form. */
3938 #define ENCODE_DESIGNATION(charset, reg, coding) \
3940 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3941 const char *intermediate_char_94 = "()*+"; \
3942 const char *intermediate_char_96 = ",-./"; \
3943 int revision = -1; \
3945 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3946 revision = CHARSET_ISO_REVISION (charset); \
3948 if (revision >= 0) \
3950 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3951 EMIT_ONE_BYTE ('@' + revision); \
3953 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3954 if (CHARSET_DIMENSION (charset) == 1) \
3957 if (! CHARSET_ISO_CHARS_96 (charset)) \
3958 b = intermediate_char_94[reg]; \
3960 b = intermediate_char_96[reg]; \
3961 EMIT_ONE_ASCII_BYTE (b); \
3965 EMIT_ONE_ASCII_BYTE ('$'); \
3966 if (! CHARSET_ISO_CHARS_96 (charset)) \
3968 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3970 || final_char < '@' || final_char > 'B') \
3971 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3974 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3976 EMIT_ONE_ASCII_BYTE (final_char); \
3978 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3982 /* The following two macros produce codes (control character or escape
3983 sequence) for ISO2022 single-shift functions (single-shift-2 and
3986 #define ENCODE_SINGLE_SHIFT_2 \
3988 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3989 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3991 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3992 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3996 #define ENCODE_SINGLE_SHIFT_3 \
3998 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3999 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4001 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4002 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4006 /* The following four macros produce codes (control character or
4007 escape sequence) for ISO2022 locking-shift functions (shift-in,
4008 shift-out, locking-shift-2, and locking-shift-3). */
4010 #define ENCODE_SHIFT_IN \
4012 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4013 CODING_ISO_INVOCATION (coding, 0) = 0; \
4017 #define ENCODE_SHIFT_OUT \
4019 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4020 CODING_ISO_INVOCATION (coding, 0) = 1; \
4024 #define ENCODE_LOCKING_SHIFT_2 \
4026 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4027 CODING_ISO_INVOCATION (coding, 0) = 2; \
4031 #define ENCODE_LOCKING_SHIFT_3 \
4033 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4034 CODING_ISO_INVOCATION (coding, 0) = 3; \
4038 /* Produce codes for a DIMENSION1 character whose character set is
4039 CHARSET and whose position-code is C1. Designation and invocation
4040 sequences are also produced in advance if necessary. */
4042 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4044 int id = CHARSET_ID (charset); \
4046 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4047 && id == charset_ascii) \
4049 id = charset_jisx0201_roman; \
4050 charset = CHARSET_FROM_ID (id); \
4053 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4055 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4056 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4058 EMIT_ONE_BYTE (c1 | 0x80); \
4059 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4062 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4064 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4067 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4069 EMIT_ONE_BYTE (c1 | 0x80); \
4073 /* Since CHARSET is not yet invoked to any graphic planes, we \
4074 must invoke it, or, at first, designate it to some graphic \
4075 register. Then repeat the loop to actually produce the \
4077 dst = encode_invocation_designation (charset, coding, dst, \
4082 /* Produce codes for a DIMENSION2 character whose character set is
4083 CHARSET and whose position-codes are C1 and C2. Designation and
4084 invocation codes are also produced in advance if necessary. */
4086 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4088 int id = CHARSET_ID (charset); \
4090 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4091 && id == charset_jisx0208) \
4093 id = charset_jisx0208_1978; \
4094 charset = CHARSET_FROM_ID (id); \
4097 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4099 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4100 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4102 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4103 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4106 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4108 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4111 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4113 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4117 /* Since CHARSET is not yet invoked to any graphic planes, we \
4118 must invoke it, or, at first, designate it to some graphic \
4119 register. Then repeat the loop to actually produce the \
4121 dst = encode_invocation_designation (charset, coding, dst, \
4126 #define ENCODE_ISO_CHARACTER(charset, c) \
4129 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4131 if (CHARSET_DIMENSION (charset) == 1) \
4132 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4134 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4138 /* Produce designation and invocation codes at a place pointed by DST
4139 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4142 static unsigned char *
4143 encode_invocation_designation (struct charset
*charset
,
4144 struct coding_system
*coding
,
4145 unsigned char *dst
, ptrdiff_t *p_nchars
)
4147 bool multibytep
= coding
->dst_multibyte
;
4148 ptrdiff_t produced_chars
= *p_nchars
;
4149 int reg
; /* graphic register number */
4150 int id
= CHARSET_ID (charset
);
4152 /* At first, check designations. */
4153 for (reg
= 0; reg
< 4; reg
++)
4154 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4159 /* CHARSET is not yet designated to any graphic registers. */
4160 /* At first check the requested designation. */
4161 reg
= CODING_ISO_REQUEST (coding
, id
);
4163 /* Since CHARSET requests no special designation, designate it
4164 to graphic register 0. */
4167 ENCODE_DESIGNATION (charset
, reg
, coding
);
4170 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4171 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4173 /* Since the graphic register REG is not invoked to any graphic
4174 planes, invoke it to graphic plane 0. */
4177 case 0: /* graphic register 0 */
4181 case 1: /* graphic register 1 */
4185 case 2: /* graphic register 2 */
4186 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4187 ENCODE_SINGLE_SHIFT_2
;
4189 ENCODE_LOCKING_SHIFT_2
;
4192 case 3: /* graphic register 3 */
4193 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4194 ENCODE_SINGLE_SHIFT_3
;
4196 ENCODE_LOCKING_SHIFT_3
;
4201 *p_nchars
= produced_chars
;
4206 /* Produce codes for designation and invocation to reset the graphic
4207 planes and registers to initial state. */
4208 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4211 struct charset *charset; \
4213 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4215 for (reg = 0; reg < 4; reg++) \
4216 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4217 && (CODING_ISO_DESIGNATION (coding, reg) \
4218 != CODING_ISO_INITIAL (coding, reg))) \
4220 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4221 ENCODE_DESIGNATION (charset, reg, coding); \
4226 /* Produce designation sequences of charsets in the line started from
4227 CHARBUF to a place pointed by DST, and return the number of
4228 produced bytes. DST should not directly point a buffer text area
4229 which may be relocated by char_charset call.
4231 If the current block ends before any end-of-line, we may fail to
4232 find all the necessary designations. */
4235 encode_designation_at_bol (struct coding_system
*coding
,
4236 int *charbuf
, int *charbuf_end
,
4239 unsigned char *orig
= dst
;
4240 struct charset
*charset
;
4241 /* Table of charsets to be designated to each graphic register. */
4243 int c
, found
= 0, reg
;
4244 ptrdiff_t produced_chars
= 0;
4245 bool multibytep
= coding
->dst_multibyte
;
4247 Lisp_Object charset_list
;
4249 attrs
= CODING_ID_ATTRS (coding
->id
);
4250 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4251 if (EQ (charset_list
, Qiso_2022
))
4252 charset_list
= Viso_2022_charset_list
;
4254 for (reg
= 0; reg
< 4; reg
++)
4257 while (charbuf
< charbuf_end
&& found
< 4)
4264 charset
= char_charset (c
, charset_list
, NULL
);
4265 id
= CHARSET_ID (charset
);
4266 reg
= CODING_ISO_REQUEST (coding
, id
);
4267 if (reg
>= 0 && r
[reg
] < 0)
4276 for (reg
= 0; reg
< 4; reg
++)
4278 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4279 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4285 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4288 encode_coding_iso_2022 (struct coding_system
*coding
)
4290 bool multibytep
= coding
->dst_multibyte
;
4291 int *charbuf
= coding
->charbuf
;
4292 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4293 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4294 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4296 bool bol_designation
4297 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4298 && CODING_ISO_BOL (coding
));
4299 ptrdiff_t produced_chars
= 0;
4300 Lisp_Object attrs
, eol_type
, charset_list
;
4301 bool ascii_compatible
;
4303 int preferred_charset_id
= -1;
4305 CODING_GET_INFO (coding
, attrs
, charset_list
);
4306 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4307 if (VECTORP (eol_type
))
4310 setup_iso_safe_charsets (attrs
);
4311 /* Charset list may have been changed. */
4312 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4313 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4316 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4317 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4318 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4320 while (charbuf
< charbuf_end
)
4322 ASSURE_DESTINATION (safe_room
);
4324 if (bol_designation
)
4326 /* We have to produce designation sequences if any now. */
4327 unsigned char desig_buf
[16];
4331 charset_map_loaded
= 0;
4332 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4334 if (charset_map_loaded
4335 && (offset
= coding_change_destination (coding
)))
4340 memcpy (dst
, desig_buf
, nbytes
);
4342 /* We are sure that designation sequences are all ASCII bytes. */
4343 produced_chars
+= nbytes
;
4344 bol_designation
= 0;
4345 ASSURE_DESTINATION (safe_room
);
4352 /* Handle an annotation. */
4355 case CODING_ANNOTATE_COMPOSITION_MASK
:
4356 /* Not yet implemented. */
4358 case CODING_ANNOTATE_CHARSET_MASK
:
4359 preferred_charset_id
= charbuf
[2];
4360 if (preferred_charset_id
>= 0
4361 && NILP (Fmemq (make_number (preferred_charset_id
),
4363 preferred_charset_id
= -1;
4372 /* Now encode the character C. */
4373 if (c
< 0x20 || c
== 0x7F)
4376 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4378 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4379 ENCODE_RESET_PLANE_AND_REGISTER ();
4380 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4384 for (i
= 0; i
< 4; i
++)
4385 CODING_ISO_DESIGNATION (coding
, i
)
4386 = CODING_ISO_INITIAL (coding
, i
);
4388 bol_designation
= ((CODING_ISO_FLAGS (coding
)
4389 & CODING_ISO_FLAG_DESIGNATE_AT_BOL
)
4392 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4393 ENCODE_RESET_PLANE_AND_REGISTER ();
4394 EMIT_ONE_ASCII_BYTE (c
);
4396 else if (ASCII_CHAR_P (c
))
4398 if (ascii_compatible
)
4399 EMIT_ONE_ASCII_BYTE (c
);
4402 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4403 ENCODE_ISO_CHARACTER (charset
, c
);
4406 else if (CHAR_BYTE8_P (c
))
4408 c
= CHAR_TO_BYTE8 (c
);
4413 struct charset
*charset
;
4415 if (preferred_charset_id
>= 0)
4419 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4420 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4422 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4426 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4430 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4432 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4433 charset
= CHARSET_FROM_ID (charset_ascii
);
4437 c
= coding
->default_char
;
4438 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4439 charset_list
, NULL
, charset
);
4442 ENCODE_ISO_CHARACTER (charset
, c
);
4446 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4447 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4449 ASSURE_DESTINATION (safe_room
);
4450 ENCODE_RESET_PLANE_AND_REGISTER ();
4452 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4453 CODING_ISO_BOL (coding
) = bol_designation
;
4454 coding
->produced_char
+= produced_chars
;
4455 coding
->produced
= dst
- coding
->destination
;
4460 /*** 8,9. SJIS and BIG5 handlers ***/
4462 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4463 quite widely. So, for the moment, Emacs supports them in the bare
4464 C code. But, in the future, they may be supported only by CCL. */
4466 /* SJIS is a coding system encoding three character sets: ASCII, right
4467 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4468 as is. A character of charset katakana-jisx0201 is encoded by
4469 "position-code + 0x80". A character of charset japanese-jisx0208
4470 is encoded in 2-byte but two position-codes are divided and shifted
4471 so that it fit in the range below.
4473 --- CODE RANGE of SJIS ---
4474 (character set) (range)
4476 KATAKANA-JISX0201 0xA0 .. 0xDF
4477 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4478 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4479 -------------------------------
4483 /* BIG5 is a coding system encoding two character sets: ASCII and
4484 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4485 character set and is encoded in two-byte.
4487 --- CODE RANGE of BIG5 ---
4488 (character set) (range)
4490 Big5 (1st byte) 0xA1 .. 0xFE
4491 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4492 --------------------------
4496 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4497 Return true if a text is encoded in SJIS. */
4500 detect_coding_sjis (struct coding_system
*coding
,
4501 struct coding_detection_info
*detect_info
)
4503 const unsigned char *src
= coding
->source
, *src_base
;
4504 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4505 bool multibytep
= coding
->src_multibyte
;
4506 ptrdiff_t consumed_chars
= 0;
4509 Lisp_Object attrs
, charset_list
;
4510 int max_first_byte_of_2_byte_code
;
4512 CODING_GET_INFO (coding
, attrs
, charset_list
);
4513 max_first_byte_of_2_byte_code
4514 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4516 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4517 /* A coding system of this category is always ASCII compatible. */
4518 src
+= coding
->head_ascii
;
4526 if ((c
>= 0x81 && c
<= 0x9F)
4527 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4530 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4532 found
= CATEGORY_MASK_SJIS
;
4534 else if (c
>= 0xA0 && c
< 0xE0)
4535 found
= CATEGORY_MASK_SJIS
;
4539 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4543 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4545 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4548 detect_info
->found
|= found
;
4552 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4553 Return true if a text is encoded in BIG5. */
4556 detect_coding_big5 (struct coding_system
*coding
,
4557 struct coding_detection_info
*detect_info
)
4559 const unsigned char *src
= coding
->source
, *src_base
;
4560 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4561 bool multibytep
= coding
->src_multibyte
;
4562 ptrdiff_t consumed_chars
= 0;
4566 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4567 /* A coding system of this category is always ASCII compatible. */
4568 src
+= coding
->head_ascii
;
4579 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4581 found
= CATEGORY_MASK_BIG5
;
4586 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4590 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4592 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4595 detect_info
->found
|= found
;
4599 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4602 decode_coding_sjis (struct coding_system
*coding
)
4604 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4605 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4606 const unsigned char *src_base
;
4607 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4608 /* We may produce one charset annotation in one loop and one more at
4611 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4612 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4613 bool multibytep
= coding
->src_multibyte
;
4614 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4615 struct charset
*charset_kanji2
;
4616 Lisp_Object attrs
, charset_list
, val
;
4617 ptrdiff_t char_offset
= coding
->produced_char
;
4618 ptrdiff_t last_offset
= char_offset
;
4619 int last_id
= charset_ascii
;
4621 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4622 int byte_after_cr
= -1;
4624 CODING_GET_INFO (coding
, attrs
, charset_list
);
4627 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4628 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4629 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4630 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4635 struct charset
*charset
;
4638 consumed_chars_base
= consumed_chars
;
4640 if (charbuf
>= charbuf_end
)
4642 if (byte_after_cr
>= 0)
4647 if (byte_after_cr
>= 0)
4648 c
= byte_after_cr
, byte_after_cr
= -1;
4655 if (eol_dos
&& c
== '\r')
4656 ONE_MORE_BYTE (byte_after_cr
);
4657 charset
= charset_roman
;
4659 else if (c
== 0x80 || c
== 0xA0)
4661 else if (c
>= 0xA1 && c
<= 0xDF)
4663 /* SJIS -> JISX0201-Kana */
4665 charset
= charset_kana
;
4669 /* SJIS -> JISX0208 */
4671 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4675 charset
= charset_kanji
;
4677 else if (c
<= 0xFC && charset_kanji2
)
4679 /* SJIS -> JISX0213-2 */
4681 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4685 charset
= charset_kanji2
;
4689 if (charset
->id
!= charset_ascii
4690 && last_id
!= charset
->id
)
4692 if (last_id
!= charset_ascii
)
4693 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4694 last_id
= charset
->id
;
4695 last_offset
= char_offset
;
4697 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4704 consumed_chars
= consumed_chars_base
;
4706 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4712 if (last_id
!= charset_ascii
)
4713 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4714 coding
->consumed_char
+= consumed_chars_base
;
4715 coding
->consumed
= src_base
- coding
->source
;
4716 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4720 decode_coding_big5 (struct coding_system
*coding
)
4722 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4723 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4724 const unsigned char *src_base
;
4725 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4726 /* We may produce one charset annotation in one loop and one more at
4729 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4730 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4731 bool multibytep
= coding
->src_multibyte
;
4732 struct charset
*charset_roman
, *charset_big5
;
4733 Lisp_Object attrs
, charset_list
, val
;
4734 ptrdiff_t char_offset
= coding
->produced_char
;
4735 ptrdiff_t last_offset
= char_offset
;
4736 int last_id
= charset_ascii
;
4738 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4739 int byte_after_cr
= -1;
4741 CODING_GET_INFO (coding
, attrs
, charset_list
);
4743 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4744 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4749 struct charset
*charset
;
4752 consumed_chars_base
= consumed_chars
;
4754 if (charbuf
>= charbuf_end
)
4756 if (byte_after_cr
>= 0)
4761 if (byte_after_cr
>= 0)
4762 c
= byte_after_cr
, byte_after_cr
= -1;
4770 if (eol_dos
&& c
== '\r')
4771 ONE_MORE_BYTE (byte_after_cr
);
4772 charset
= charset_roman
;
4777 if (c
< 0xA1 || c
> 0xFE)
4780 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4783 charset
= charset_big5
;
4785 if (charset
->id
!= charset_ascii
4786 && last_id
!= charset
->id
)
4788 if (last_id
!= charset_ascii
)
4789 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4790 last_id
= charset
->id
;
4791 last_offset
= char_offset
;
4793 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4800 consumed_chars
= consumed_chars_base
;
4802 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4808 if (last_id
!= charset_ascii
)
4809 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4810 coding
->consumed_char
+= consumed_chars_base
;
4811 coding
->consumed
= src_base
- coding
->source
;
4812 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4815 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4816 This function can encode charsets `ascii', `katakana-jisx0201',
4817 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4818 are sure that all these charsets are registered as official charset
4819 (i.e. do not have extended leading-codes). Characters of other
4820 charsets are produced without any encoding. */
4823 encode_coding_sjis (struct coding_system
*coding
)
4825 bool multibytep
= coding
->dst_multibyte
;
4826 int *charbuf
= coding
->charbuf
;
4827 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4828 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4829 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4831 ptrdiff_t produced_chars
= 0;
4832 Lisp_Object attrs
, charset_list
, val
;
4833 bool ascii_compatible
;
4834 struct charset
*charset_kanji
, *charset_kana
;
4835 struct charset
*charset_kanji2
;
4838 CODING_GET_INFO (coding
, attrs
, charset_list
);
4839 val
= XCDR (charset_list
);
4840 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4841 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4842 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4844 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4846 while (charbuf
< charbuf_end
)
4848 ASSURE_DESTINATION (safe_room
);
4850 /* Now encode the character C. */
4851 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4852 EMIT_ONE_ASCII_BYTE (c
);
4853 else if (CHAR_BYTE8_P (c
))
4855 c
= CHAR_TO_BYTE8 (c
);
4861 struct charset
*charset
;
4862 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4867 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4869 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4870 charset
= CHARSET_FROM_ID (charset_ascii
);
4874 c
= coding
->default_char
;
4875 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4876 charset_list
, &code
, charset
);
4879 if (code
== CHARSET_INVALID_CODE (charset
))
4881 if (charset
== charset_kanji
)
4885 c1
= code
>> 8, c2
= code
& 0xFF;
4886 EMIT_TWO_BYTES (c1
, c2
);
4888 else if (charset
== charset_kana
)
4889 EMIT_ONE_BYTE (code
| 0x80);
4890 else if (charset_kanji2
&& charset
== charset_kanji2
)
4895 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4897 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4899 JIS_TO_SJIS2 (code
);
4900 c1
= code
>> 8, c2
= code
& 0xFF;
4901 EMIT_TWO_BYTES (c1
, c2
);
4904 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4907 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4910 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4911 coding
->produced_char
+= produced_chars
;
4912 coding
->produced
= dst
- coding
->destination
;
4917 encode_coding_big5 (struct coding_system
*coding
)
4919 bool multibytep
= coding
->dst_multibyte
;
4920 int *charbuf
= coding
->charbuf
;
4921 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4922 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4923 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4925 ptrdiff_t produced_chars
= 0;
4926 Lisp_Object attrs
, charset_list
, val
;
4927 bool ascii_compatible
;
4928 struct charset
*charset_big5
;
4931 CODING_GET_INFO (coding
, attrs
, charset_list
);
4932 val
= XCDR (charset_list
);
4933 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4934 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4936 while (charbuf
< charbuf_end
)
4938 ASSURE_DESTINATION (safe_room
);
4940 /* Now encode the character C. */
4941 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4942 EMIT_ONE_ASCII_BYTE (c
);
4943 else if (CHAR_BYTE8_P (c
))
4945 c
= CHAR_TO_BYTE8 (c
);
4951 struct charset
*charset
;
4952 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4957 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4959 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4960 charset
= CHARSET_FROM_ID (charset_ascii
);
4964 c
= coding
->default_char
;
4965 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4966 charset_list
, &code
, charset
);
4969 if (code
== CHARSET_INVALID_CODE (charset
))
4971 if (charset
== charset_big5
)
4975 c1
= code
>> 8, c2
= code
& 0xFF;
4976 EMIT_TWO_BYTES (c1
, c2
);
4979 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4982 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4983 coding
->produced_char
+= produced_chars
;
4984 coding
->produced
= dst
- coding
->destination
;
4989 /*** 10. CCL handlers ***/
4991 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4992 Return true if a text is encoded in a coding system of which
4993 encoder/decoder are written in CCL program. */
4996 detect_coding_ccl (struct coding_system
*coding
,
4997 struct coding_detection_info
*detect_info
)
4999 const unsigned char *src
= coding
->source
, *src_base
;
5000 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5001 bool multibytep
= coding
->src_multibyte
;
5002 ptrdiff_t consumed_chars
= 0;
5004 unsigned char *valids
;
5005 ptrdiff_t head_ascii
= coding
->head_ascii
;
5008 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5010 coding
= &coding_categories
[coding_category_ccl
];
5011 valids
= CODING_CCL_VALIDS (coding
);
5012 attrs
= CODING_ID_ATTRS (coding
->id
);
5013 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5022 if (c
< 0 || ! valids
[c
])
5024 if ((valids
[c
] > 1))
5025 found
= CATEGORY_MASK_CCL
;
5027 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5031 detect_info
->found
|= found
;
5036 decode_coding_ccl (struct coding_system
*coding
)
5038 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5039 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5040 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5041 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5042 ptrdiff_t consumed_chars
= 0;
5043 bool multibytep
= coding
->src_multibyte
;
5044 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5045 int source_charbuf
[1024];
5046 int source_byteidx
[1025];
5047 Lisp_Object attrs
, charset_list
;
5049 CODING_GET_INFO (coding
, attrs
, charset_list
);
5053 const unsigned char *p
= src
;
5059 while (i
< 1024 && p
< src_end
)
5061 source_byteidx
[i
] = p
- src
;
5062 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5064 source_byteidx
[i
] = p
- src
;
5067 while (i
< 1024 && p
< src_end
)
5068 source_charbuf
[i
++] = *p
++;
5070 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5071 ccl
->last_block
= 1;
5072 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5073 charset_map_loaded
= 0;
5074 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5076 if (charset_map_loaded
5077 && (offset
= coding_change_source (coding
)))
5083 charbuf
+= ccl
->produced
;
5085 src
+= source_byteidx
[ccl
->consumed
];
5087 src
+= ccl
->consumed
;
5088 consumed_chars
+= ccl
->consumed
;
5089 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5093 switch (ccl
->status
)
5095 case CCL_STAT_SUSPEND_BY_SRC
:
5096 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5098 case CCL_STAT_SUSPEND_BY_DST
:
5099 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5102 case CCL_STAT_INVALID_CMD
:
5103 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5106 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5109 coding
->consumed_char
+= consumed_chars
;
5110 coding
->consumed
= src
- coding
->source
;
5111 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5115 encode_coding_ccl (struct coding_system
*coding
)
5117 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5118 bool multibytep
= coding
->dst_multibyte
;
5119 int *charbuf
= coding
->charbuf
;
5120 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5121 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5122 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5123 int destination_charbuf
[1024];
5124 ptrdiff_t produced_chars
= 0;
5126 Lisp_Object attrs
, charset_list
;
5128 CODING_GET_INFO (coding
, attrs
, charset_list
);
5129 if (coding
->consumed_char
== coding
->src_chars
5130 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5131 ccl
->last_block
= 1;
5137 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5138 charset_map_loaded
= 0;
5139 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5140 charbuf_end
- charbuf
, 1024, charset_list
);
5141 if (charset_map_loaded
5142 && (offset
= coding_change_destination (coding
)))
5146 ASSURE_DESTINATION (ccl
->produced
* 2);
5147 for (i
= 0; i
< ccl
->produced
; i
++)
5148 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5152 ASSURE_DESTINATION (ccl
->produced
);
5153 for (i
= 0; i
< ccl
->produced
; i
++)
5154 *dst
++ = destination_charbuf
[i
] & 0xFF;
5155 produced_chars
+= ccl
->produced
;
5157 charbuf
+= ccl
->consumed
;
5158 if (ccl
->status
== CCL_STAT_QUIT
5159 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5162 while (charbuf
< charbuf_end
);
5164 switch (ccl
->status
)
5166 case CCL_STAT_SUSPEND_BY_SRC
:
5167 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5169 case CCL_STAT_SUSPEND_BY_DST
:
5170 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5173 case CCL_STAT_INVALID_CMD
:
5174 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5177 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5181 coding
->produced_char
+= produced_chars
;
5182 coding
->produced
= dst
- coding
->destination
;
5187 /*** 10, 11. no-conversion handlers ***/
5189 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5192 decode_coding_raw_text (struct coding_system
*coding
)
5195 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5197 coding
->chars_at_source
= 1;
5198 coding
->consumed_char
= coding
->src_chars
;
5199 coding
->consumed
= coding
->src_bytes
;
5200 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5202 coding
->consumed_char
--;
5204 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5207 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5211 encode_coding_raw_text (struct coding_system
*coding
)
5213 bool multibytep
= coding
->dst_multibyte
;
5214 int *charbuf
= coding
->charbuf
;
5215 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5216 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5217 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5218 ptrdiff_t produced_chars
= 0;
5223 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5225 if (coding
->src_multibyte
)
5226 while (charbuf
< charbuf_end
)
5228 ASSURE_DESTINATION (safe_room
);
5230 if (ASCII_CHAR_P (c
))
5231 EMIT_ONE_ASCII_BYTE (c
);
5232 else if (CHAR_BYTE8_P (c
))
5234 c
= CHAR_TO_BYTE8 (c
);
5239 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5241 CHAR_STRING_ADVANCE (c
, p1
);
5244 EMIT_ONE_BYTE (*p0
);
5251 while (charbuf
< charbuf_end
)
5253 ASSURE_DESTINATION (safe_room
);
5260 if (coding
->src_multibyte
)
5262 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5264 while (charbuf
< charbuf_end
)
5266 ASSURE_DESTINATION (safe_room
);
5268 if (ASCII_CHAR_P (c
))
5270 else if (CHAR_BYTE8_P (c
))
5271 *dst
++ = CHAR_TO_BYTE8 (c
);
5273 CHAR_STRING_ADVANCE (c
, dst
);
5278 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5279 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5280 *dst
++ = *charbuf
++;
5282 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5284 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5285 coding
->produced_char
+= produced_chars
;
5286 coding
->produced
= dst
- coding
->destination
;
5290 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5291 Return true if a text is encoded in a charset-based coding system. */
5294 detect_coding_charset (struct coding_system
*coding
,
5295 struct coding_detection_info
*detect_info
)
5297 const unsigned char *src
= coding
->source
, *src_base
;
5298 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5299 bool multibytep
= coding
->src_multibyte
;
5300 ptrdiff_t consumed_chars
= 0;
5301 Lisp_Object attrs
, valids
, name
;
5303 ptrdiff_t head_ascii
= coding
->head_ascii
;
5304 bool check_latin_extra
= 0;
5306 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5308 coding
= &coding_categories
[coding_category_charset
];
5309 attrs
= CODING_ID_ATTRS (coding
->id
);
5310 valids
= AREF (attrs
, coding_attr_charset_valids
);
5311 name
= CODING_ID_NAME (coding
->id
);
5312 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5313 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5314 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5315 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5316 check_latin_extra
= 1;
5318 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5325 struct charset
*charset
;
5332 val
= AREF (valids
, c
);
5338 && check_latin_extra
5339 && (!VECTORP (Vlatin_extra_code_table
)
5340 || NILP (AREF (Vlatin_extra_code_table
, c
))))
5342 found
= CATEGORY_MASK_CHARSET
;
5346 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5347 dim
= CHARSET_DIMENSION (charset
);
5348 for (idx
= 1; idx
< dim
; idx
++)
5353 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5354 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5363 for (; CONSP (val
); val
= XCDR (val
))
5365 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5366 dim
= CHARSET_DIMENSION (charset
);
5372 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5373 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5388 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5392 detect_info
->found
|= found
;
5397 decode_coding_charset (struct coding_system
*coding
)
5399 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5400 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5401 const unsigned char *src_base
;
5402 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5403 /* We may produce one charset annotation in one loop and one more at
5406 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5407 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
5408 bool multibytep
= coding
->src_multibyte
;
5409 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5411 ptrdiff_t char_offset
= coding
->produced_char
;
5412 ptrdiff_t last_offset
= char_offset
;
5413 int last_id
= charset_ascii
;
5415 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5416 int byte_after_cr
= -1;
5418 valids
= AREF (attrs
, coding_attr_charset_valids
);
5424 struct charset
*charset
;
5430 consumed_chars_base
= consumed_chars
;
5432 if (charbuf
>= charbuf_end
)
5434 if (byte_after_cr
>= 0)
5439 if (byte_after_cr
>= 0)
5447 if (eol_dos
&& c
== '\r')
5448 ONE_MORE_BYTE (byte_after_cr
);
5454 val
= AREF (valids
, c
);
5455 if (! INTEGERP (val
) && ! CONSP (val
))
5459 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5460 dim
= CHARSET_DIMENSION (charset
);
5464 code
= (code
<< 8) | c
;
5467 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5472 /* VAL is a list of charset IDs. It is assured that the
5473 list is sorted by charset dimensions (smaller one
5477 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5478 dim
= CHARSET_DIMENSION (charset
);
5482 code
= (code
<< 8) | c
;
5485 CODING_DECODE_CHAR (coding
, src
, src_base
,
5486 src_end
, charset
, code
, c
);
5494 if (charset
->id
!= charset_ascii
5495 && last_id
!= charset
->id
)
5497 if (last_id
!= charset_ascii
)
5498 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5499 last_id
= charset
->id
;
5500 last_offset
= char_offset
;
5509 consumed_chars
= consumed_chars_base
;
5511 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5517 if (last_id
!= charset_ascii
)
5518 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5519 coding
->consumed_char
+= consumed_chars_base
;
5520 coding
->consumed
= src_base
- coding
->source
;
5521 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5525 encode_coding_charset (struct coding_system
*coding
)
5527 bool multibytep
= coding
->dst_multibyte
;
5528 int *charbuf
= coding
->charbuf
;
5529 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5530 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5531 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5532 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5533 ptrdiff_t produced_chars
= 0;
5534 Lisp_Object attrs
, charset_list
;
5535 bool ascii_compatible
;
5538 CODING_GET_INFO (coding
, attrs
, charset_list
);
5539 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5541 while (charbuf
< charbuf_end
)
5543 struct charset
*charset
;
5546 ASSURE_DESTINATION (safe_room
);
5548 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5549 EMIT_ONE_ASCII_BYTE (c
);
5550 else if (CHAR_BYTE8_P (c
))
5552 c
= CHAR_TO_BYTE8 (c
);
5557 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5562 if (CHARSET_DIMENSION (charset
) == 1)
5563 EMIT_ONE_BYTE (code
);
5564 else if (CHARSET_DIMENSION (charset
) == 2)
5565 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5566 else if (CHARSET_DIMENSION (charset
) == 3)
5567 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5569 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5570 (code
>> 8) & 0xFF, code
& 0xFF);
5574 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5575 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5577 c
= coding
->default_char
;
5583 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5584 coding
->produced_char
+= produced_chars
;
5585 coding
->produced
= dst
- coding
->destination
;
5590 /*** 7. C library functions ***/
5592 /* Setup coding context CODING from information about CODING_SYSTEM.
5593 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5594 CODING_SYSTEM is invalid, signal an error. */
5597 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5600 Lisp_Object eol_type
;
5601 Lisp_Object coding_type
;
5604 if (NILP (coding_system
))
5605 coding_system
= Qundecided
;
5607 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5609 attrs
= CODING_ID_ATTRS (coding
->id
);
5610 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5613 coding
->head_ascii
= -1;
5614 if (VECTORP (eol_type
))
5615 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5616 | CODING_REQUIRE_DETECTION_MASK
);
5617 else if (! EQ (eol_type
, Qunix
))
5618 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5619 | CODING_REQUIRE_ENCODING_MASK
);
5621 coding
->common_flags
= 0;
5622 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5623 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5624 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5625 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5626 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5627 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5629 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5630 coding
->max_charset_id
= SCHARS (val
) - 1;
5631 coding
->safe_charsets
= SDATA (val
);
5632 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5633 coding
->carryover_bytes
= 0;
5635 coding_type
= CODING_ATTR_TYPE (attrs
);
5636 if (EQ (coding_type
, Qundecided
))
5638 coding
->detector
= NULL
;
5639 coding
->decoder
= decode_coding_raw_text
;
5640 coding
->encoder
= encode_coding_raw_text
;
5641 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5643 else if (EQ (coding_type
, Qiso_2022
))
5646 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5648 /* Invoke graphic register 0 to plane 0. */
5649 CODING_ISO_INVOCATION (coding
, 0) = 0;
5650 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5651 CODING_ISO_INVOCATION (coding
, 1)
5652 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5653 /* Setup the initial status of designation. */
5654 for (i
= 0; i
< 4; i
++)
5655 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5656 /* Not single shifting initially. */
5657 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5658 /* Beginning of buffer should also be regarded as bol. */
5659 CODING_ISO_BOL (coding
) = 1;
5660 coding
->detector
= detect_coding_iso_2022
;
5661 coding
->decoder
= decode_coding_iso_2022
;
5662 coding
->encoder
= encode_coding_iso_2022
;
5663 if (flags
& CODING_ISO_FLAG_SAFE
)
5664 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5665 coding
->common_flags
5666 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5667 | CODING_REQUIRE_FLUSHING_MASK
);
5668 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5669 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5670 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5671 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5672 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5674 setup_iso_safe_charsets (attrs
);
5675 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5676 coding
->max_charset_id
= SCHARS (val
) - 1;
5677 coding
->safe_charsets
= SDATA (val
);
5679 CODING_ISO_FLAGS (coding
) = flags
;
5680 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5681 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5682 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5683 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5685 else if (EQ (coding_type
, Qcharset
))
5687 coding
->detector
= detect_coding_charset
;
5688 coding
->decoder
= decode_coding_charset
;
5689 coding
->encoder
= encode_coding_charset
;
5690 coding
->common_flags
5691 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5693 else if (EQ (coding_type
, Qutf_8
))
5695 val
= AREF (attrs
, coding_attr_utf_bom
);
5696 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5697 : EQ (val
, Qt
) ? utf_with_bom
5699 coding
->detector
= detect_coding_utf_8
;
5700 coding
->decoder
= decode_coding_utf_8
;
5701 coding
->encoder
= encode_coding_utf_8
;
5702 coding
->common_flags
5703 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5704 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5705 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5707 else if (EQ (coding_type
, Qutf_16
))
5709 val
= AREF (attrs
, coding_attr_utf_bom
);
5710 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5711 : EQ (val
, Qt
) ? utf_with_bom
5713 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5714 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5715 : utf_16_little_endian
);
5716 CODING_UTF_16_SURROGATE (coding
) = 0;
5717 coding
->detector
= detect_coding_utf_16
;
5718 coding
->decoder
= decode_coding_utf_16
;
5719 coding
->encoder
= encode_coding_utf_16
;
5720 coding
->common_flags
5721 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5722 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5723 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5725 else if (EQ (coding_type
, Qccl
))
5727 coding
->detector
= detect_coding_ccl
;
5728 coding
->decoder
= decode_coding_ccl
;
5729 coding
->encoder
= encode_coding_ccl
;
5730 coding
->common_flags
5731 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5732 | CODING_REQUIRE_FLUSHING_MASK
);
5734 else if (EQ (coding_type
, Qemacs_mule
))
5736 coding
->detector
= detect_coding_emacs_mule
;
5737 coding
->decoder
= decode_coding_emacs_mule
;
5738 coding
->encoder
= encode_coding_emacs_mule
;
5739 coding
->common_flags
5740 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5741 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5742 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5744 Lisp_Object tail
, safe_charsets
;
5745 int max_charset_id
= 0;
5747 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5749 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5750 max_charset_id
= XFASTINT (XCAR (tail
));
5751 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5752 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5753 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5755 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5756 coding
->max_charset_id
= max_charset_id
;
5757 coding
->safe_charsets
= SDATA (safe_charsets
);
5759 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5760 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5762 else if (EQ (coding_type
, Qshift_jis
))
5764 coding
->detector
= detect_coding_sjis
;
5765 coding
->decoder
= decode_coding_sjis
;
5766 coding
->encoder
= encode_coding_sjis
;
5767 coding
->common_flags
5768 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5770 else if (EQ (coding_type
, Qbig5
))
5772 coding
->detector
= detect_coding_big5
;
5773 coding
->decoder
= decode_coding_big5
;
5774 coding
->encoder
= encode_coding_big5
;
5775 coding
->common_flags
5776 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5778 else /* EQ (coding_type, Qraw_text) */
5780 coding
->detector
= NULL
;
5781 coding
->decoder
= decode_coding_raw_text
;
5782 coding
->encoder
= encode_coding_raw_text
;
5783 if (! EQ (eol_type
, Qunix
))
5785 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5786 if (! VECTORP (eol_type
))
5787 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5795 /* Return a list of charsets supported by CODING. */
5798 coding_charset_list (struct coding_system
*coding
)
5800 Lisp_Object attrs
, charset_list
;
5802 CODING_GET_INFO (coding
, attrs
, charset_list
);
5803 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5805 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5807 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5808 charset_list
= Viso_2022_charset_list
;
5810 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5812 charset_list
= Vemacs_mule_charset_list
;
5814 return charset_list
;
5818 /* Return a list of charsets supported by CODING-SYSTEM. */
5821 coding_system_charset_list (Lisp_Object coding_system
)
5824 Lisp_Object attrs
, charset_list
;
5826 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5827 attrs
= CODING_ID_ATTRS (id
);
5829 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5831 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5833 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5834 charset_list
= Viso_2022_charset_list
;
5836 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5838 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5840 charset_list
= Vemacs_mule_charset_list
;
5844 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5846 return charset_list
;
5850 /* Return raw-text or one of its subsidiaries that has the same
5851 eol_type as CODING-SYSTEM. */
5854 raw_text_coding_system (Lisp_Object coding_system
)
5856 Lisp_Object spec
, attrs
;
5857 Lisp_Object eol_type
, raw_text_eol_type
;
5859 if (NILP (coding_system
))
5861 spec
= CODING_SYSTEM_SPEC (coding_system
);
5862 attrs
= AREF (spec
, 0);
5864 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5865 return coding_system
;
5867 eol_type
= AREF (spec
, 2);
5868 if (VECTORP (eol_type
))
5870 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5871 raw_text_eol_type
= AREF (spec
, 2);
5872 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5873 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5874 : AREF (raw_text_eol_type
, 2));
5878 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5879 the subsidiary that has the same eol-spec as PARENT (if it is not
5880 nil and specifies end-of-line format) or the system's setting
5881 (system_eol_type). */
5884 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5886 Lisp_Object spec
, eol_type
;
5888 if (NILP (coding_system
))
5889 coding_system
= Qraw_text
;
5890 spec
= CODING_SYSTEM_SPEC (coding_system
);
5891 eol_type
= AREF (spec
, 2);
5892 if (VECTORP (eol_type
))
5894 Lisp_Object parent_eol_type
;
5896 if (! NILP (parent
))
5898 Lisp_Object parent_spec
;
5900 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5901 parent_eol_type
= AREF (parent_spec
, 2);
5902 if (VECTORP (parent_eol_type
))
5903 parent_eol_type
= system_eol_type
;
5906 parent_eol_type
= system_eol_type
;
5907 if (EQ (parent_eol_type
, Qunix
))
5908 coding_system
= AREF (eol_type
, 0);
5909 else if (EQ (parent_eol_type
, Qdos
))
5910 coding_system
= AREF (eol_type
, 1);
5911 else if (EQ (parent_eol_type
, Qmac
))
5912 coding_system
= AREF (eol_type
, 2);
5914 return coding_system
;
5918 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5919 decided for writing to a process. If not, complement them, and
5920 return a new coding system. */
5923 complement_process_encoding_system (Lisp_Object coding_system
)
5925 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5926 Lisp_Object spec
, attrs
;
5929 for (i
= 0; i
< 3; i
++)
5932 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
5934 coding_system
= preferred_coding_system ();
5935 spec
= CODING_SYSTEM_SPEC (coding_system
);
5938 attrs
= AREF (spec
, 0);
5939 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
5940 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
5941 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
5942 eol_base
= coding_system
;
5943 if (! NILP (coding_base
) && ! NILP (eol_base
))
5948 /* The original CODING_SYSTEM didn't specify text-conversion or
5949 eol-conversion. Be sure that we return a fully complemented
5951 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
5952 return coding_system
;
5956 /* Emacs has a mechanism to automatically detect a coding system if it
5957 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5958 it's impossible to distinguish some coding systems accurately
5959 because they use the same range of codes. So, at first, coding
5960 systems are categorized into 7, those are:
5962 o coding-category-emacs-mule
5964 The category for a coding system which has the same code range
5965 as Emacs' internal format. Assigned the coding-system (Lisp
5966 symbol) `emacs-mule' by default.
5968 o coding-category-sjis
5970 The category for a coding system which has the same code range
5971 as SJIS. Assigned the coding-system (Lisp
5972 symbol) `japanese-shift-jis' by default.
5974 o coding-category-iso-7
5976 The category for a coding system which has the same code range
5977 as ISO2022 of 7-bit environment. This doesn't use any locking
5978 shift and single shift functions. This can encode/decode all
5979 charsets. Assigned the coding-system (Lisp symbol)
5980 `iso-2022-7bit' by default.
5982 o coding-category-iso-7-tight
5984 Same as coding-category-iso-7 except that this can
5985 encode/decode only the specified charsets.
5987 o coding-category-iso-8-1
5989 The category for a coding system which has the same code range
5990 as ISO2022 of 8-bit environment and graphic plane 1 used only
5991 for DIMENSION1 charset. This doesn't use any locking shift
5992 and single shift functions. Assigned the coding-system (Lisp
5993 symbol) `iso-latin-1' by default.
5995 o coding-category-iso-8-2
5997 The category for a coding system which has the same code range
5998 as ISO2022 of 8-bit environment and graphic plane 1 used only
5999 for DIMENSION2 charset. This doesn't use any locking shift
6000 and single shift functions. Assigned the coding-system (Lisp
6001 symbol) `japanese-iso-8bit' by default.
6003 o coding-category-iso-7-else
6005 The category for a coding system which has the same code range
6006 as ISO2022 of 7-bit environment but uses locking shift or
6007 single shift functions. Assigned the coding-system (Lisp
6008 symbol) `iso-2022-7bit-lock' by default.
6010 o coding-category-iso-8-else
6012 The category for a coding system which has the same code range
6013 as ISO2022 of 8-bit environment but uses locking shift or
6014 single shift functions. Assigned the coding-system (Lisp
6015 symbol) `iso-2022-8bit-ss2' by default.
6017 o coding-category-big5
6019 The category for a coding system which has the same code range
6020 as BIG5. Assigned the coding-system (Lisp symbol)
6021 `cn-big5' by default.
6023 o coding-category-utf-8
6025 The category for a coding system which has the same code range
6026 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6027 symbol) `utf-8' by default.
6029 o coding-category-utf-16-be
6031 The category for a coding system in which a text has an
6032 Unicode signature (cf. Unicode Standard) in the order of BIG
6033 endian at the head. Assigned the coding-system (Lisp symbol)
6034 `utf-16-be' by default.
6036 o coding-category-utf-16-le
6038 The category for a coding system in which a text has an
6039 Unicode signature (cf. Unicode Standard) in the order of
6040 LITTLE endian at the head. Assigned the coding-system (Lisp
6041 symbol) `utf-16-le' by default.
6043 o coding-category-ccl
6045 The category for a coding system of which encoder/decoder is
6046 written in CCL programs. The default value is nil, i.e., no
6047 coding system is assigned.
6049 o coding-category-binary
6051 The category for a coding system not categorized in any of the
6052 above. Assigned the coding-system (Lisp symbol)
6053 `no-conversion' by default.
6055 Each of them is a Lisp symbol and the value is an actual
6056 `coding-system's (this is also a Lisp symbol) assigned by a user.
6057 What Emacs does actually is to detect a category of coding system.
6058 Then, it uses a `coding-system' assigned to it. If Emacs can't
6059 decide only one possible category, it selects a category of the
6060 highest priority. Priorities of categories are also specified by a
6061 user in a Lisp variable `coding-category-list'.
6065 #define EOL_SEEN_NONE 0
6066 #define EOL_SEEN_LF 1
6067 #define EOL_SEEN_CR 2
6068 #define EOL_SEEN_CRLF 4
6070 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6071 SOURCE is encoded. If CATEGORY is one of
6072 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6073 two-byte, else they are encoded by one-byte.
6075 Return one of EOL_SEEN_XXX. */
6077 #define MAX_EOL_CHECK_COUNT 3
6080 detect_eol (const unsigned char *source
, ptrdiff_t src_bytes
,
6081 enum coding_category category
)
6083 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6086 int eol_seen
= EOL_SEEN_NONE
;
6088 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6090 bool msb
= category
== (coding_category_utf_16_le
6091 | coding_category_utf_16_le_nosig
);
6094 while (src
+ 1 < src_end
)
6097 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6102 this_eol
= EOL_SEEN_LF
;
6103 else if (src
+ 3 >= src_end
6104 || src
[msb
+ 2] != 0
6105 || src
[lsb
+ 2] != '\n')
6106 this_eol
= EOL_SEEN_CR
;
6109 this_eol
= EOL_SEEN_CRLF
;
6113 if (eol_seen
== EOL_SEEN_NONE
)
6114 /* This is the first end-of-line. */
6115 eol_seen
= this_eol
;
6116 else if (eol_seen
!= this_eol
)
6118 /* The found type is different from what found before.
6119 Allow for stray ^M characters in DOS EOL files. */
6120 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6121 || (eol_seen
== EOL_SEEN_CRLF
6122 && this_eol
== EOL_SEEN_CR
))
6123 eol_seen
= EOL_SEEN_CRLF
;
6126 eol_seen
= EOL_SEEN_LF
;
6130 if (++total
== MAX_EOL_CHECK_COUNT
)
6137 while (src
< src_end
)
6140 if (c
== '\n' || c
== '\r')
6145 this_eol
= EOL_SEEN_LF
;
6146 else if (src
>= src_end
|| *src
!= '\n')
6147 this_eol
= EOL_SEEN_CR
;
6149 this_eol
= EOL_SEEN_CRLF
, src
++;
6151 if (eol_seen
== EOL_SEEN_NONE
)
6152 /* This is the first end-of-line. */
6153 eol_seen
= this_eol
;
6154 else if (eol_seen
!= this_eol
)
6156 /* The found type is different from what found before.
6157 Allow for stray ^M characters in DOS EOL files. */
6158 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6159 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6160 eol_seen
= EOL_SEEN_CRLF
;
6163 eol_seen
= EOL_SEEN_LF
;
6167 if (++total
== MAX_EOL_CHECK_COUNT
)
6176 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6178 Lisp_Object eol_type
;
6180 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6181 if (eol_seen
& EOL_SEEN_LF
)
6183 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6186 else if (eol_seen
& EOL_SEEN_CRLF
)
6188 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6191 else if (eol_seen
& EOL_SEEN_CR
)
6193 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6199 /* Detect how a text specified in CODING is encoded. If a coding
6200 system is detected, update fields of CODING by the detected coding
6204 detect_coding (struct coding_system
*coding
)
6206 const unsigned char *src
, *src_end
;
6207 unsigned int saved_mode
= coding
->mode
;
6209 coding
->consumed
= coding
->consumed_char
= 0;
6210 coding
->produced
= coding
->produced_char
= 0;
6211 coding_set_source (coding
);
6213 src_end
= coding
->source
+ coding
->src_bytes
;
6214 coding
->head_ascii
= 0;
6216 /* If we have not yet decided the text encoding type, detect it
6218 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6221 struct coding_detection_info detect_info
;
6222 bool null_byte_found
= 0, eight_bit_found
= 0;
6224 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6225 for (src
= coding
->source
; src
< src_end
; src
++)
6230 eight_bit_found
= 1;
6231 if (null_byte_found
)
6236 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6237 && ! inhibit_iso_escape_detection
6238 && ! detect_info
.checked
)
6240 if (detect_coding_iso_2022 (coding
, &detect_info
))
6242 /* We have scanned the whole data. */
6243 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6245 /* We didn't find an 8-bit code. We may
6246 have found a null-byte, but it's very
6247 rare that a binary file conforms to
6250 coding
->head_ascii
= src
- coding
->source
;
6252 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6256 else if (! c
&& !inhibit_null_byte_detection
)
6258 null_byte_found
= 1;
6259 if (eight_bit_found
)
6262 if (! eight_bit_found
)
6263 coding
->head_ascii
++;
6265 else if (! eight_bit_found
)
6266 coding
->head_ascii
++;
6269 if (null_byte_found
|| eight_bit_found
6270 || coding
->head_ascii
< coding
->src_bytes
6271 || detect_info
.found
)
6273 enum coding_category category
;
6274 struct coding_system
*this;
6276 if (coding
->head_ascii
== coding
->src_bytes
)
6277 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6278 for (i
= 0; i
< coding_category_raw_text
; i
++)
6280 category
= coding_priorities
[i
];
6281 this = coding_categories
+ category
;
6282 if (detect_info
.found
& (1 << category
))
6287 if (null_byte_found
)
6289 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6290 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6292 for (i
= 0; i
< coding_category_raw_text
; i
++)
6294 category
= coding_priorities
[i
];
6295 this = coding_categories
+ category
;
6296 /* Some of this->detector (e.g. detect_coding_sjis)
6297 require this information. */
6298 coding
->id
= this->id
;
6301 /* No coding system of this category is defined. */
6302 detect_info
.rejected
|= (1 << category
);
6304 else if (category
>= coding_category_raw_text
)
6306 else if (detect_info
.checked
& (1 << category
))
6308 if (detect_info
.found
& (1 << category
))
6311 else if ((*(this->detector
)) (coding
, &detect_info
)
6312 && detect_info
.found
& (1 << category
))
6314 if (category
== coding_category_utf_16_auto
)
6316 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6317 category
= coding_category_utf_16_le
;
6319 category
= coding_category_utf_16_be
;
6326 if (i
< coding_category_raw_text
)
6327 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6328 else if (null_byte_found
)
6329 setup_coding_system (Qno_conversion
, coding
);
6330 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6331 == CATEGORY_MASK_ANY
)
6332 setup_coding_system (Qraw_text
, coding
);
6333 else if (detect_info
.rejected
)
6334 for (i
= 0; i
< coding_category_raw_text
; i
++)
6335 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6337 this = coding_categories
+ coding_priorities
[i
];
6338 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6343 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6344 == coding_category_utf_8_auto
)
6346 Lisp_Object coding_systems
;
6347 struct coding_detection_info detect_info
;
6350 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6351 detect_info
.found
= detect_info
.rejected
= 0;
6352 for (src
= coding
->source
; src
< src_end
; src
++)
6357 coding
->head_ascii
= src
- coding
->source
;
6358 if (CONSP (coding_systems
)
6359 && detect_coding_utf_8 (coding
, &detect_info
))
6361 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6362 setup_coding_system (XCAR (coding_systems
), coding
);
6364 setup_coding_system (XCDR (coding_systems
), coding
);
6367 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6368 == coding_category_utf_16_auto
)
6370 Lisp_Object coding_systems
;
6371 struct coding_detection_info detect_info
;
6374 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6375 detect_info
.found
= detect_info
.rejected
= 0;
6376 coding
->head_ascii
= 0;
6377 if (CONSP (coding_systems
)
6378 && detect_coding_utf_16 (coding
, &detect_info
))
6380 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6381 setup_coding_system (XCAR (coding_systems
), coding
);
6382 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6383 setup_coding_system (XCDR (coding_systems
), coding
);
6386 coding
->mode
= saved_mode
;
6391 decode_eol (struct coding_system
*coding
)
6393 Lisp_Object eol_type
;
6394 unsigned char *p
, *pbeg
, *pend
;
6396 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6397 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6400 if (NILP (coding
->dst_object
))
6401 pbeg
= coding
->destination
;
6403 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6404 pend
= pbeg
+ coding
->produced
;
6406 if (VECTORP (eol_type
))
6408 int eol_seen
= EOL_SEEN_NONE
;
6410 for (p
= pbeg
; p
< pend
; p
++)
6413 eol_seen
|= EOL_SEEN_LF
;
6414 else if (*p
== '\r')
6416 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6418 eol_seen
|= EOL_SEEN_CRLF
;
6422 eol_seen
|= EOL_SEEN_CR
;
6425 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6426 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6427 && (eol_seen
& EOL_SEEN_CR
) != 0
6428 && (eol_seen
& EOL_SEEN_LF
) == 0)
6429 eol_seen
= EOL_SEEN_CRLF
;
6430 else if (eol_seen
!= EOL_SEEN_NONE
6431 && eol_seen
!= EOL_SEEN_LF
6432 && eol_seen
!= EOL_SEEN_CRLF
6433 && eol_seen
!= EOL_SEEN_CR
)
6434 eol_seen
= EOL_SEEN_LF
;
6435 if (eol_seen
!= EOL_SEEN_NONE
)
6436 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6439 if (EQ (eol_type
, Qmac
))
6441 for (p
= pbeg
; p
< pend
; p
++)
6445 else if (EQ (eol_type
, Qdos
))
6449 if (NILP (coding
->dst_object
))
6451 /* Start deleting '\r' from the tail to minimize the memory
6453 for (p
= pend
- 2; p
>= pbeg
; p
--)
6456 memmove (p
, p
+ 1, pend
-- - p
- 1);
6462 ptrdiff_t pos_byte
= coding
->dst_pos_byte
;
6463 ptrdiff_t pos
= coding
->dst_pos
;
6464 ptrdiff_t pos_end
= pos
+ coding
->produced_char
- 1;
6466 while (pos
< pos_end
)
6468 p
= BYTE_POS_ADDR (pos_byte
);
6469 if (*p
== '\r' && p
[1] == '\n')
6471 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6476 if (coding
->dst_multibyte
)
6477 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6482 coding
->produced
-= n
;
6483 coding
->produced_char
-= n
;
6488 /* Return a translation table (or list of them) from coding system
6489 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6493 get_translation_table (Lisp_Object attrs
, bool encodep
, int *max_lookup
)
6495 Lisp_Object standard
, translation_table
;
6498 if (NILP (Venable_character_translation
))
6505 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6506 standard
= Vstandard_translation_table_for_encode
;
6508 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6509 standard
= Vstandard_translation_table_for_decode
;
6510 if (NILP (translation_table
))
6511 translation_table
= standard
;
6514 if (SYMBOLP (translation_table
))
6515 translation_table
= Fget (translation_table
, Qtranslation_table
);
6516 else if (CONSP (translation_table
))
6518 translation_table
= Fcopy_sequence (translation_table
);
6519 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6520 if (SYMBOLP (XCAR (val
)))
6521 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6523 if (CHAR_TABLE_P (standard
))
6525 if (CONSP (translation_table
))
6526 translation_table
= nconc2 (translation_table
,
6527 Fcons (standard
, Qnil
));
6529 translation_table
= Fcons (translation_table
,
6530 Fcons (standard
, Qnil
));
6537 if (CHAR_TABLE_P (translation_table
)
6538 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6540 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6541 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6542 *max_lookup
= XFASTINT (val
);
6544 else if (CONSP (translation_table
))
6548 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6549 if (CHAR_TABLE_P (XCAR (tail
))
6550 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6552 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6553 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6554 *max_lookup
= XFASTINT (tailval
);
6558 return translation_table
;
6561 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6564 if (CHAR_TABLE_P (table)) \
6566 trans = CHAR_TABLE_REF (table, c); \
6567 if (CHARACTERP (trans)) \
6568 c = XFASTINT (trans), trans = Qnil; \
6570 else if (CONSP (table)) \
6574 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6575 if (CHAR_TABLE_P (XCAR (tail))) \
6577 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6578 if (CHARACTERP (trans)) \
6579 c = XFASTINT (trans), trans = Qnil; \
6580 else if (! NILP (trans)) \
6587 /* Return a translation of character(s) at BUF according to TRANS.
6588 TRANS is TO-CHAR or ((FROM . TO) ...) where
6589 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6590 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6591 translation is found, and Qnil if not found..
6592 If BUF is too short to lookup characters in FROM, return Qt. */
6595 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6598 if (INTEGERP (trans
))
6600 for (; CONSP (trans
); trans
= XCDR (trans
))
6602 Lisp_Object val
= XCAR (trans
);
6603 Lisp_Object from
= XCAR (val
);
6604 ptrdiff_t len
= ASIZE (from
);
6607 for (i
= 0; i
< len
; i
++)
6609 if (buf
+ i
== buf_end
)
6611 if (XINT (AREF (from
, i
)) != buf
[i
])
6622 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6625 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6626 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6628 ptrdiff_t produced_chars
= 0;
6631 if (! coding
->chars_at_source
)
6633 /* Source characters are in coding->charbuf. */
6634 int *buf
= coding
->charbuf
;
6635 int *buf_end
= buf
+ coding
->charbuf_used
;
6637 if (EQ (coding
->src_object
, coding
->dst_object
))
6639 coding_set_source (coding
);
6640 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6643 while (buf
< buf_end
)
6650 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
6651 Lisp_Object trans
= Qnil
;
6653 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6656 trans
= get_translation (trans
, buf
, buf_end
);
6657 if (INTEGERP (trans
))
6659 else if (CONSP (trans
))
6661 from_nchars
= ASIZE (XCAR (trans
));
6662 trans
= XCDR (trans
);
6663 if (INTEGERP (trans
))
6667 to_nchars
= ASIZE (trans
);
6668 c
= XINT (AREF (trans
, 0));
6671 else if (EQ (trans
, Qt
) && ! last_block
)
6675 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
6677 if (((min (PTRDIFF_MAX
, SIZE_MAX
) - (buf_end
- buf
))
6678 / MAX_MULTIBYTE_LENGTH
)
6680 memory_full (SIZE_MAX
);
6681 dst
= alloc_destination (coding
,
6683 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6685 if (EQ (coding
->src_object
, coding
->dst_object
))
6687 coding_set_source (coding
);
6688 dst_end
= (((unsigned char *) coding
->source
)
6689 + coding
->consumed
);
6692 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6695 for (i
= 0; i
< to_nchars
; i
++)
6698 c
= XINT (AREF (trans
, i
));
6699 if (coding
->dst_multibyte
6700 || ! CHAR_BYTE8_P (c
))
6701 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6703 *dst
++ = CHAR_TO_BYTE8 (c
);
6705 produced_chars
+= to_nchars
;
6709 /* This is an annotation datum. (-C) is the length. */
6712 carryover
= buf_end
- buf
;
6716 /* Source characters are at coding->source. */
6717 const unsigned char *src
= coding
->source
;
6718 const unsigned char *src_end
= src
+ coding
->consumed
;
6720 if (EQ (coding
->dst_object
, coding
->src_object
))
6721 dst_end
= (unsigned char *) src
;
6722 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6724 if (coding
->src_multibyte
)
6726 bool multibytep
= 1;
6727 ptrdiff_t consumed_chars
= 0;
6731 const unsigned char *src_base
= src
;
6737 if (EQ (coding
->src_object
, coding
->dst_object
))
6738 dst_end
= (unsigned char *) src
;
6741 ptrdiff_t offset
= src
- coding
->source
;
6743 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6745 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6746 coding_set_source (coding
);
6747 src
= coding
->source
+ offset
;
6748 src_end
= coding
->source
+ coding
->consumed
;
6749 if (EQ (coding
->src_object
, coding
->dst_object
))
6750 dst_end
= (unsigned char *) src
;
6760 while (src
< src_end
)
6762 bool multibytep
= 1;
6765 if (dst
>= dst_end
- 1)
6767 if (EQ (coding
->src_object
, coding
->dst_object
))
6768 dst_end
= (unsigned char *) src
;
6769 if (dst
>= dst_end
- 1)
6771 ptrdiff_t offset
= src
- coding
->source
;
6772 ptrdiff_t more_bytes
;
6774 if (EQ (coding
->src_object
, coding
->dst_object
))
6775 more_bytes
= ((src_end
- src
) / 2) + 2;
6777 more_bytes
= src_end
- src
+ 2;
6778 dst
= alloc_destination (coding
, more_bytes
, dst
);
6779 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6780 coding_set_source (coding
);
6781 src
= coding
->source
+ offset
;
6782 src_end
= coding
->source
+ coding
->consumed
;
6783 if (EQ (coding
->src_object
, coding
->dst_object
))
6784 dst_end
= (unsigned char *) src
;
6792 if (!EQ (coding
->src_object
, coding
->dst_object
))
6794 ptrdiff_t require
= coding
->src_bytes
- coding
->dst_bytes
;
6798 ptrdiff_t offset
= src
- coding
->source
;
6800 dst
= alloc_destination (coding
, require
, dst
);
6801 coding_set_source (coding
);
6802 src
= coding
->source
+ offset
;
6803 src_end
= coding
->source
+ coding
->consumed
;
6806 produced_chars
= coding
->consumed_char
;
6807 while (src
< src_end
)
6812 produced
= dst
- (coding
->destination
+ coding
->produced
);
6813 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6814 insert_from_gap (produced_chars
, produced
);
6815 coding
->produced
+= produced
;
6816 coding
->produced_char
+= produced_chars
;
6820 /* Compose text in CODING->object according to the annotation data at
6821 CHARBUF. CHARBUF is an array:
6822 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6826 produce_composition (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6830 enum composition_method method
;
6831 Lisp_Object components
;
6833 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6834 to
= pos
+ charbuf
[2];
6835 method
= (enum composition_method
) (charbuf
[4]);
6837 if (method
== COMPOSITION_RELATIVE
)
6841 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6844 if (method
== COMPOSITION_WITH_RULE
)
6845 len
= charbuf
[2] * 3 - 2;
6846 charbuf
+= MAX_ANNOTATION_LENGTH
;
6847 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6848 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6850 if (charbuf
[i
] >= 0)
6851 args
[j
] = make_number (charbuf
[i
]);
6855 args
[j
] = make_number (charbuf
[i
] % 0x100);
6858 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6860 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6864 /* Put `charset' property on text in CODING->object according to
6865 the annotation data at CHARBUF. CHARBUF is an array:
6866 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6870 produce_charset (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6872 ptrdiff_t from
= pos
- charbuf
[2];
6873 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6875 Fput_text_property (make_number (from
), make_number (pos
),
6876 Qcharset
, CHARSET_NAME (charset
),
6877 coding
->dst_object
);
6881 #define CHARBUF_SIZE 0x4000
6883 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6885 coding->charbuf = SAFE_ALLOCA (CHARBUF_SIZE * sizeof (int)); \
6886 coding->charbuf_size = CHARBUF_SIZE; \
6891 produce_annotation (struct coding_system
*coding
, ptrdiff_t pos
)
6893 int *charbuf
= coding
->charbuf
;
6894 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6896 if (NILP (coding
->dst_object
))
6899 while (charbuf
< charbuf_end
)
6905 int len
= -*charbuf
;
6910 case CODING_ANNOTATE_COMPOSITION_MASK
:
6911 produce_composition (coding
, charbuf
, pos
);
6913 case CODING_ANNOTATE_CHARSET_MASK
:
6914 produce_charset (coding
, charbuf
, pos
);
6922 /* Decode the data at CODING->src_object into CODING->dst_object.
6923 CODING->src_object is a buffer, a string, or nil.
6924 CODING->dst_object is a buffer.
6926 If CODING->src_object is a buffer, it must be the current buffer.
6927 In this case, if CODING->src_pos is positive, it is a position of
6928 the source text in the buffer, otherwise, the source text is in the
6929 gap area of the buffer, and CODING->src_pos specifies the offset of
6930 the text from GPT (which must be the same as PT). If this is the
6931 same buffer as CODING->dst_object, CODING->src_pos must be
6934 If CODING->src_object is a string, CODING->src_pos is an index to
6937 If CODING->src_object is nil, CODING->source must already point to
6938 the non-relocatable memory area. In this case, CODING->src_pos is
6939 an offset from CODING->source.
6941 The decoded data is inserted at the current point of the buffer
6946 decode_coding (struct coding_system
*coding
)
6949 Lisp_Object undo_list
;
6950 Lisp_Object translation_table
;
6951 struct ccl_spec cclspec
;
6957 if (BUFFERP (coding
->src_object
)
6958 && coding
->src_pos
> 0
6959 && coding
->src_pos
< GPT
6960 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6961 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6964 if (BUFFERP (coding
->dst_object
))
6966 set_buffer_internal (XBUFFER (coding
->dst_object
));
6968 move_gap_both (PT
, PT_BYTE
);
6970 /* We must disable undo_list in order to record the whole insert
6971 transaction via record_insert at the end. But doing so also
6972 disables the recording of the first change to the undo_list.
6973 Therefore we check for first change here and record it via
6974 record_first_change if needed. */
6975 if (MODIFF
<= SAVE_MODIFF
)
6976 record_first_change ();
6978 undo_list
= BVAR (current_buffer
, undo_list
);
6979 bset_undo_list (current_buffer
, Qt
);
6982 coding
->consumed
= coding
->consumed_char
= 0;
6983 coding
->produced
= coding
->produced_char
= 0;
6984 coding
->chars_at_source
= 0;
6985 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6988 ALLOC_CONVERSION_WORK_AREA (coding
);
6990 attrs
= CODING_ID_ATTRS (coding
->id
);
6991 translation_table
= get_translation_table (attrs
, 0, NULL
);
6994 if (coding
->decoder
== decode_coding_ccl
)
6996 coding
->spec
.ccl
= &cclspec
;
6997 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7001 ptrdiff_t pos
= coding
->dst_pos
+ coding
->produced_char
;
7003 coding_set_source (coding
);
7004 coding
->annotated
= 0;
7005 coding
->charbuf_used
= carryover
;
7006 (*(coding
->decoder
)) (coding
);
7007 coding_set_destination (coding
);
7008 carryover
= produce_chars (coding
, translation_table
, 0);
7009 if (coding
->annotated
)
7010 produce_annotation (coding
, pos
);
7011 for (i
= 0; i
< carryover
; i
++)
7013 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7015 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7016 || (coding
->consumed
< coding
->src_bytes
7017 && (coding
->result
== CODING_RESULT_SUCCESS
7018 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7022 coding_set_destination (coding
);
7023 coding
->charbuf_used
= carryover
;
7024 produce_chars (coding
, translation_table
, 1);
7027 coding
->carryover_bytes
= 0;
7028 if (coding
->consumed
< coding
->src_bytes
)
7030 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7031 const unsigned char *src
;
7033 coding_set_source (coding
);
7034 coding_set_destination (coding
);
7035 src
= coding
->source
+ coding
->consumed
;
7037 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7039 /* Flush out unprocessed data as binary chars. We are sure
7040 that the number of data is less than the size of
7042 coding
->charbuf_used
= 0;
7043 coding
->chars_at_source
= 0;
7045 while (nbytes
-- > 0)
7050 c
= BYTE8_TO_CHAR (c
);
7051 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7053 produce_chars (coding
, Qnil
, 1);
7057 /* Record unprocessed bytes in coding->carryover. We are
7058 sure that the number of data is less than the size of
7059 coding->carryover. */
7060 unsigned char *p
= coding
->carryover
;
7062 if (nbytes
> sizeof coding
->carryover
)
7063 nbytes
= sizeof coding
->carryover
;
7064 coding
->carryover_bytes
= nbytes
;
7065 while (nbytes
-- > 0)
7068 coding
->consumed
= coding
->src_bytes
;
7071 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7072 && !inhibit_eol_conversion
)
7073 decode_eol (coding
);
7074 if (BUFFERP (coding
->dst_object
))
7076 bset_undo_list (current_buffer
, undo_list
);
7077 record_insert (coding
->dst_pos
, coding
->produced_char
);
7084 /* Extract an annotation datum from a composition starting at POS and
7085 ending before LIMIT of CODING->src_object (buffer or string), store
7086 the data in BUF, set *STOP to a starting position of the next
7087 composition (if any) or to LIMIT, and return the address of the
7088 next element of BUF.
7090 If such an annotation is not found, set *STOP to a starting
7091 position of a composition after POS (if any) or to LIMIT, and
7095 handle_composition_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7096 struct coding_system
*coding
, int *buf
,
7099 ptrdiff_t start
, end
;
7102 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7105 else if (start
> pos
)
7111 /* We found a composition. Store the corresponding
7112 annotation data in BUF. */
7114 enum composition_method method
= COMPOSITION_METHOD (prop
);
7115 int nchars
= COMPOSITION_LENGTH (prop
);
7117 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7118 if (method
!= COMPOSITION_RELATIVE
)
7120 Lisp_Object components
;
7121 ptrdiff_t i
, len
, i_byte
;
7123 components
= COMPOSITION_COMPONENTS (prop
);
7124 if (VECTORP (components
))
7126 len
= ASIZE (components
);
7127 for (i
= 0; i
< len
; i
++)
7128 *buf
++ = XINT (AREF (components
, i
));
7130 else if (STRINGP (components
))
7132 len
= SCHARS (components
);
7136 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7140 else if (INTEGERP (components
))
7143 *buf
++ = XINT (components
);
7145 else if (CONSP (components
))
7147 for (len
= 0; CONSP (components
);
7148 len
++, components
= XCDR (components
))
7149 *buf
++ = XINT (XCAR (components
));
7157 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7168 /* Extract an annotation datum from a text property `charset' at POS of
7169 CODING->src_object (buffer of string), store the data in BUF, set
7170 *STOP to the position where the value of `charset' property changes
7171 (limiting by LIMIT), and return the address of the next element of
7174 If the property value is nil, set *STOP to the position where the
7175 property value is non-nil (limiting by LIMIT), and return BUF. */
7178 handle_charset_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7179 struct coding_system
*coding
, int *buf
,
7182 Lisp_Object val
, next
;
7185 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7186 if (! NILP (val
) && CHARSETP (val
))
7187 id
= XINT (CHARSET_SYMBOL_ID (val
));
7190 ADD_CHARSET_DATA (buf
, 0, id
);
7191 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7193 make_number (limit
));
7194 *stop
= XINT (next
);
7200 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7203 int *buf
= coding
->charbuf
;
7204 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7205 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7206 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7207 ptrdiff_t pos
= coding
->src_pos
+ coding
->consumed_char
;
7208 ptrdiff_t end_pos
= coding
->src_pos
+ coding
->src_chars
;
7209 bool multibytep
= coding
->src_multibyte
;
7210 Lisp_Object eol_type
;
7212 ptrdiff_t stop
, stop_composition
, stop_charset
;
7213 int *lookup_buf
= NULL
;
7215 if (! NILP (translation_table
))
7216 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7218 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7219 if (VECTORP (eol_type
))
7222 /* Note: composition handling is not yet implemented. */
7223 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7225 if (NILP (coding
->src_object
))
7226 stop
= stop_composition
= stop_charset
= end_pos
;
7229 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7230 stop
= stop_composition
= pos
;
7232 stop
= stop_composition
= end_pos
;
7233 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7234 stop
= stop_charset
= pos
;
7236 stop_charset
= end_pos
;
7239 /* Compensate for CRLF and conversion. */
7240 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7241 while (buf
< buf_end
)
7249 if (pos
== stop_composition
)
7250 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7251 buf
, &stop_composition
);
7252 if (pos
== stop_charset
)
7253 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7254 buf
, &stop_charset
);
7255 stop
= (stop_composition
< stop_charset
7256 ? stop_composition
: stop_charset
);
7263 if (coding
->encoder
== encode_coding_raw_text
7264 || coding
->encoder
== encode_coding_ccl
)
7266 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7267 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7269 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7272 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7273 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7275 if (! EQ (eol_type
, Qunix
))
7279 if (EQ (eol_type
, Qdos
))
7287 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7292 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7293 int *lookup_buf_end
;
7294 const unsigned char *p
= src
;
7298 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7299 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7300 lookup_buf_end
= lookup_buf
+ i
;
7301 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7302 if (INTEGERP (trans
))
7304 else if (CONSP (trans
))
7306 from_nchars
= ASIZE (XCAR (trans
));
7307 trans
= XCDR (trans
);
7308 if (INTEGERP (trans
))
7312 to_nchars
= ASIZE (trans
);
7313 if (buf_end
- buf
< to_nchars
)
7315 c
= XINT (AREF (trans
, 0));
7321 for (i
= 1; i
< to_nchars
; i
++)
7322 *buf
++ = XINT (AREF (trans
, i
));
7323 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7324 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7328 coding
->consumed
= src
- coding
->source
;
7329 coding
->consumed_char
= pos
- coding
->src_pos
;
7330 coding
->charbuf_used
= buf
- coding
->charbuf
;
7331 coding
->chars_at_source
= 0;
7335 /* Encode the text at CODING->src_object into CODING->dst_object.
7336 CODING->src_object is a buffer or a string.
7337 CODING->dst_object is a buffer or nil.
7339 If CODING->src_object is a buffer, it must be the current buffer.
7340 In this case, if CODING->src_pos is positive, it is a position of
7341 the source text in the buffer, otherwise. the source text is in the
7342 gap area of the buffer, and coding->src_pos specifies the offset of
7343 the text from GPT (which must be the same as PT). If this is the
7344 same buffer as CODING->dst_object, CODING->src_pos must be
7345 negative and CODING should not have `pre-write-conversion'.
7347 If CODING->src_object is a string, CODING should not have
7348 `pre-write-conversion'.
7350 If CODING->dst_object is a buffer, the encoded data is inserted at
7351 the current point of that buffer.
7353 If CODING->dst_object is nil, the encoded data is placed at the
7354 memory area specified by CODING->destination. */
7357 encode_coding (struct coding_system
*coding
)
7360 Lisp_Object translation_table
;
7362 struct ccl_spec cclspec
;
7366 attrs
= CODING_ID_ATTRS (coding
->id
);
7367 if (coding
->encoder
== encode_coding_raw_text
)
7368 translation_table
= Qnil
, max_lookup
= 0;
7370 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7372 if (BUFFERP (coding
->dst_object
))
7374 set_buffer_internal (XBUFFER (coding
->dst_object
));
7375 coding
->dst_multibyte
7376 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7379 coding
->consumed
= coding
->consumed_char
= 0;
7380 coding
->produced
= coding
->produced_char
= 0;
7381 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7384 ALLOC_CONVERSION_WORK_AREA (coding
);
7386 if (coding
->encoder
== encode_coding_ccl
)
7388 coding
->spec
.ccl
= &cclspec
;
7389 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7392 coding_set_source (coding
);
7393 consume_chars (coding
, translation_table
, max_lookup
);
7394 coding_set_destination (coding
);
7395 (*(coding
->encoder
)) (coding
);
7396 } while (coding
->consumed_char
< coding
->src_chars
);
7398 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7399 insert_from_gap (coding
->produced_char
, coding
->produced
);
7405 /* Name (or base name) of work buffer for code conversion. */
7406 static Lisp_Object Vcode_conversion_workbuf_name
;
7408 /* A working buffer used by the top level conversion. Once it is
7409 created, it is never destroyed. It has the name
7410 Vcode_conversion_workbuf_name. The other working buffers are
7411 destroyed after the use is finished, and their names are modified
7412 versions of Vcode_conversion_workbuf_name. */
7413 static Lisp_Object Vcode_conversion_reused_workbuf
;
7415 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7416 static bool reused_workbuf_in_use
;
7419 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7420 multibyteness of returning buffer. */
7423 make_conversion_work_buffer (bool multibyte
)
7425 Lisp_Object name
, workbuf
;
7426 struct buffer
*current
;
7428 if (reused_workbuf_in_use
)
7430 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7431 workbuf
= Fget_buffer_create (name
);
7435 reused_workbuf_in_use
= 1;
7436 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7437 Vcode_conversion_reused_workbuf
7438 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7439 workbuf
= Vcode_conversion_reused_workbuf
;
7441 current
= current_buffer
;
7442 set_buffer_internal (XBUFFER (workbuf
));
7443 /* We can't allow modification hooks to run in the work buffer. For
7444 instance, directory_files_internal assumes that file decoding
7445 doesn't compile new regexps. */
7446 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7448 bset_undo_list (current_buffer
, Qt
);
7449 bset_enable_multibyte_characters (current_buffer
, multibyte
? Qt
: Qnil
);
7450 set_buffer_internal (current
);
7456 code_conversion_restore (Lisp_Object arg
)
7458 Lisp_Object current
, workbuf
;
7459 struct gcpro gcpro1
;
7462 current
= XCAR (arg
);
7463 workbuf
= XCDR (arg
);
7464 if (! NILP (workbuf
))
7466 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7467 reused_workbuf_in_use
= 0;
7469 Fkill_buffer (workbuf
);
7471 set_buffer_internal (XBUFFER (current
));
7477 code_conversion_save (bool with_work_buf
, bool multibyte
)
7479 Lisp_Object workbuf
= Qnil
;
7482 workbuf
= make_conversion_work_buffer (multibyte
);
7483 record_unwind_protect (code_conversion_restore
,
7484 Fcons (Fcurrent_buffer (), workbuf
));
7489 decode_coding_gap (struct coding_system
*coding
,
7490 ptrdiff_t chars
, ptrdiff_t bytes
)
7492 ptrdiff_t count
= SPECPDL_INDEX ();
7495 coding
->src_object
= Fcurrent_buffer ();
7496 coding
->src_chars
= chars
;
7497 coding
->src_bytes
= bytes
;
7498 coding
->src_pos
= -chars
;
7499 coding
->src_pos_byte
= -bytes
;
7500 coding
->src_multibyte
= chars
< bytes
;
7501 coding
->dst_object
= coding
->src_object
;
7502 coding
->dst_pos
= PT
;
7503 coding
->dst_pos_byte
= PT_BYTE
;
7504 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7506 if (CODING_REQUIRE_DETECTION (coding
))
7507 detect_coding (coding
);
7508 attrs
= CODING_ID_ATTRS (coding
->id
);
7509 #ifndef CODING_DISABLE_ASCII_OPTIMIZATION
7510 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
7511 && NILP (CODING_ATTR_POST_READ (attrs
))
7512 && NILP (get_translation_table (attrs
, 0, NULL
))
7513 && (inhibit_eol_conversion
7514 || EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)))
7516 /* We can skip the conversion if all source bytes are ASCII. */
7517 if (coding
->head_ascii
< 0)
7519 /* We have not yet counted the number of ASCII bytes at the
7520 head of the source. Do it now. */
7521 const unsigned char *src
, *src_end
;
7523 coding_set_source (coding
);
7524 src_end
= coding
->source
+ coding
->src_bytes
;
7525 for (src
= coding
->source
; src
< src_end
; src
++)
7530 coding
->head_ascii
= src
- coding
->source
;
7532 if (coding
->src_bytes
== coding
->head_ascii
)
7534 /* No need of conversion. Use the data in the gap as is. */
7535 coding
->produced_char
= chars
;
7536 coding
->produced
= bytes
;
7537 adjust_after_replace (PT
, PT_BYTE
, Qnil
, chars
, bytes
, 1);
7541 #endif /* not CODING_DISABLE_ASCII_OPTIMIZATION */
7542 code_conversion_save (0, 0);
7544 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7545 current_buffer
->text
->inhibit_shrinking
= 1;
7546 decode_coding (coding
);
7547 current_buffer
->text
->inhibit_shrinking
= 0;
7549 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7551 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7554 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7555 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7556 make_number (coding
->produced_char
));
7558 coding
->produced_char
+= Z
- prev_Z
;
7559 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7562 unbind_to (count
, Qnil
);
7566 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7567 SRC_OBJECT into DST_OBJECT by coding context CODING.
7569 SRC_OBJECT is a buffer, a string, or Qnil.
7571 If it is a buffer, the text is at point of the buffer. FROM and TO
7572 are positions in the buffer.
7574 If it is a string, the text is at the beginning of the string.
7575 FROM and TO are indices to the string.
7577 If it is nil, the text is at coding->source. FROM and TO are
7578 indices to coding->source.
7580 DST_OBJECT is a buffer, Qt, or Qnil.
7582 If it is a buffer, the decoded text is inserted at point of the
7583 buffer. If the buffer is the same as SRC_OBJECT, the source text
7586 If it is Qt, a string is made from the decoded text, and
7587 set in CODING->dst_object.
7589 If it is Qnil, the decoded text is stored at CODING->destination.
7590 The caller must allocate CODING->dst_bytes bytes at
7591 CODING->destination by xmalloc. If the decoded text is longer than
7592 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7596 decode_coding_object (struct coding_system
*coding
,
7597 Lisp_Object src_object
,
7598 ptrdiff_t from
, ptrdiff_t from_byte
,
7599 ptrdiff_t to
, ptrdiff_t to_byte
,
7600 Lisp_Object dst_object
)
7602 ptrdiff_t count
= SPECPDL_INDEX ();
7603 unsigned char *destination
IF_LINT (= NULL
);
7604 ptrdiff_t dst_bytes
IF_LINT (= 0);
7605 ptrdiff_t chars
= to
- from
;
7606 ptrdiff_t bytes
= to_byte
- from_byte
;
7608 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7609 bool need_marker_adjustment
= 0;
7610 Lisp_Object old_deactivate_mark
;
7612 old_deactivate_mark
= Vdeactivate_mark
;
7614 if (NILP (dst_object
))
7616 destination
= coding
->destination
;
7617 dst_bytes
= coding
->dst_bytes
;
7620 coding
->src_object
= src_object
;
7621 coding
->src_chars
= chars
;
7622 coding
->src_bytes
= bytes
;
7623 coding
->src_multibyte
= chars
< bytes
;
7625 if (STRINGP (src_object
))
7627 coding
->src_pos
= from
;
7628 coding
->src_pos_byte
= from_byte
;
7630 else if (BUFFERP (src_object
))
7632 set_buffer_internal (XBUFFER (src_object
));
7634 move_gap_both (from
, from_byte
);
7635 if (EQ (src_object
, dst_object
))
7637 struct Lisp_Marker
*tail
;
7639 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7641 tail
->need_adjustment
7642 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7643 need_marker_adjustment
|= tail
->need_adjustment
;
7645 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7646 TEMP_SET_PT_BOTH (from
, from_byte
);
7647 current_buffer
->text
->inhibit_shrinking
= 1;
7648 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7649 coding
->src_pos
= -chars
;
7650 coding
->src_pos_byte
= -bytes
;
7654 coding
->src_pos
= from
;
7655 coding
->src_pos_byte
= from_byte
;
7659 if (CODING_REQUIRE_DETECTION (coding
))
7660 detect_coding (coding
);
7661 attrs
= CODING_ID_ATTRS (coding
->id
);
7663 if (EQ (dst_object
, Qt
)
7664 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7665 && NILP (dst_object
)))
7667 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7668 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7669 coding
->dst_pos
= BEG
;
7670 coding
->dst_pos_byte
= BEG_BYTE
;
7672 else if (BUFFERP (dst_object
))
7674 code_conversion_save (0, 0);
7675 coding
->dst_object
= dst_object
;
7676 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7677 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7678 coding
->dst_multibyte
7679 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7683 code_conversion_save (0, 0);
7684 coding
->dst_object
= Qnil
;
7685 /* Most callers presume this will return a multibyte result, and they
7686 won't use `binary' or `raw-text' anyway, so let's not worry about
7687 CODING_FOR_UNIBYTE. */
7688 coding
->dst_multibyte
= 1;
7691 decode_coding (coding
);
7693 if (BUFFERP (coding
->dst_object
))
7694 set_buffer_internal (XBUFFER (coding
->dst_object
));
7696 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7698 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7699 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7702 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7703 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7704 old_deactivate_mark
);
7705 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7706 make_number (coding
->produced_char
));
7709 coding
->produced_char
+= Z
- prev_Z
;
7710 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7713 if (EQ (dst_object
, Qt
))
7715 coding
->dst_object
= Fbuffer_string ();
7717 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7719 set_buffer_internal (XBUFFER (coding
->dst_object
));
7720 if (dst_bytes
< coding
->produced
)
7722 eassert (coding
->produced
> 0);
7723 destination
= xrealloc (destination
, coding
->produced
);
7724 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7725 move_gap_both (BEGV
, BEGV_BYTE
);
7726 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7727 coding
->destination
= destination
;
7733 /* This is the case of:
7734 (BUFFERP (src_object) && EQ (src_object, dst_object))
7735 As we have moved PT while replacing the original buffer
7736 contents, we must recover it now. */
7737 set_buffer_internal (XBUFFER (src_object
));
7738 current_buffer
->text
->inhibit_shrinking
= 0;
7739 if (saved_pt
< from
)
7740 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7741 else if (saved_pt
< from
+ chars
)
7742 TEMP_SET_PT_BOTH (from
, from_byte
);
7743 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7744 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7745 saved_pt_byte
+ (coding
->produced
- bytes
));
7747 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7748 saved_pt_byte
+ (coding
->produced
- bytes
));
7750 if (need_marker_adjustment
)
7752 struct Lisp_Marker
*tail
;
7754 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7755 if (tail
->need_adjustment
)
7757 tail
->need_adjustment
= 0;
7758 if (tail
->insertion_type
)
7760 tail
->bytepos
= from_byte
;
7761 tail
->charpos
= from
;
7765 tail
->bytepos
= from_byte
+ coding
->produced
;
7767 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7768 ? tail
->bytepos
: from
+ coding
->produced_char
);
7774 Vdeactivate_mark
= old_deactivate_mark
;
7775 unbind_to (count
, coding
->dst_object
);
7780 encode_coding_object (struct coding_system
*coding
,
7781 Lisp_Object src_object
,
7782 ptrdiff_t from
, ptrdiff_t from_byte
,
7783 ptrdiff_t to
, ptrdiff_t to_byte
,
7784 Lisp_Object dst_object
)
7786 ptrdiff_t count
= SPECPDL_INDEX ();
7787 ptrdiff_t chars
= to
- from
;
7788 ptrdiff_t bytes
= to_byte
- from_byte
;
7790 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7791 bool need_marker_adjustment
= 0;
7792 bool kill_src_buffer
= 0;
7793 Lisp_Object old_deactivate_mark
;
7795 old_deactivate_mark
= Vdeactivate_mark
;
7797 coding
->src_object
= src_object
;
7798 coding
->src_chars
= chars
;
7799 coding
->src_bytes
= bytes
;
7800 coding
->src_multibyte
= chars
< bytes
;
7802 attrs
= CODING_ID_ATTRS (coding
->id
);
7804 if (EQ (src_object
, dst_object
))
7806 struct Lisp_Marker
*tail
;
7808 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7810 tail
->need_adjustment
7811 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7812 need_marker_adjustment
|= tail
->need_adjustment
;
7816 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7818 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7819 set_buffer_internal (XBUFFER (coding
->src_object
));
7820 if (STRINGP (src_object
))
7821 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7822 else if (BUFFERP (src_object
))
7823 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7825 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7827 if (EQ (src_object
, dst_object
))
7829 set_buffer_internal (XBUFFER (src_object
));
7830 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7831 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7832 set_buffer_internal (XBUFFER (coding
->src_object
));
7836 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7838 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7839 old_deactivate_mark
);
7840 safe_call2 (CODING_ATTR_PRE_WRITE (attrs
),
7841 make_number (BEG
), make_number (Z
));
7844 if (XBUFFER (coding
->src_object
) != current_buffer
)
7845 kill_src_buffer
= 1;
7846 coding
->src_object
= Fcurrent_buffer ();
7848 move_gap_both (BEG
, BEG_BYTE
);
7849 coding
->src_chars
= Z
- BEG
;
7850 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7851 coding
->src_pos
= BEG
;
7852 coding
->src_pos_byte
= BEG_BYTE
;
7853 coding
->src_multibyte
= Z
< Z_BYTE
;
7855 else if (STRINGP (src_object
))
7857 code_conversion_save (0, 0);
7858 coding
->src_pos
= from
;
7859 coding
->src_pos_byte
= from_byte
;
7861 else if (BUFFERP (src_object
))
7863 code_conversion_save (0, 0);
7864 set_buffer_internal (XBUFFER (src_object
));
7865 if (EQ (src_object
, dst_object
))
7867 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7868 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7869 coding
->src_pos
= 0;
7870 coding
->src_pos_byte
= 0;
7874 if (from
< GPT
&& to
>= GPT
)
7875 move_gap_both (from
, from_byte
);
7876 coding
->src_pos
= from
;
7877 coding
->src_pos_byte
= from_byte
;
7881 code_conversion_save (0, 0);
7883 if (BUFFERP (dst_object
))
7885 coding
->dst_object
= dst_object
;
7886 if (EQ (src_object
, dst_object
))
7888 coding
->dst_pos
= from
;
7889 coding
->dst_pos_byte
= from_byte
;
7893 struct buffer
*current
= current_buffer
;
7895 set_buffer_temp (XBUFFER (dst_object
));
7896 coding
->dst_pos
= PT
;
7897 coding
->dst_pos_byte
= PT_BYTE
;
7898 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7899 set_buffer_temp (current
);
7901 coding
->dst_multibyte
7902 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7904 else if (EQ (dst_object
, Qt
))
7906 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
7907 coding
->dst_object
= Qnil
;
7908 coding
->destination
= xmalloc (dst_bytes
);
7909 coding
->dst_bytes
= dst_bytes
;
7910 coding
->dst_multibyte
= 0;
7914 coding
->dst_object
= Qnil
;
7915 coding
->dst_multibyte
= 0;
7918 encode_coding (coding
);
7920 if (EQ (dst_object
, Qt
))
7922 if (BUFFERP (coding
->dst_object
))
7923 coding
->dst_object
= Fbuffer_string ();
7927 = make_unibyte_string ((char *) coding
->destination
,
7929 xfree (coding
->destination
);
7935 /* This is the case of:
7936 (BUFFERP (src_object) && EQ (src_object, dst_object))
7937 As we have moved PT while replacing the original buffer
7938 contents, we must recover it now. */
7939 set_buffer_internal (XBUFFER (src_object
));
7940 if (saved_pt
< from
)
7941 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7942 else if (saved_pt
< from
+ chars
)
7943 TEMP_SET_PT_BOTH (from
, from_byte
);
7944 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7945 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7946 saved_pt_byte
+ (coding
->produced
- bytes
));
7948 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7949 saved_pt_byte
+ (coding
->produced
- bytes
));
7951 if (need_marker_adjustment
)
7953 struct Lisp_Marker
*tail
;
7955 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7956 if (tail
->need_adjustment
)
7958 tail
->need_adjustment
= 0;
7959 if (tail
->insertion_type
)
7961 tail
->bytepos
= from_byte
;
7962 tail
->charpos
= from
;
7966 tail
->bytepos
= from_byte
+ coding
->produced
;
7968 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7969 ? tail
->bytepos
: from
+ coding
->produced_char
);
7975 if (kill_src_buffer
)
7976 Fkill_buffer (coding
->src_object
);
7978 Vdeactivate_mark
= old_deactivate_mark
;
7979 unbind_to (count
, Qnil
);
7984 preferred_coding_system (void)
7986 int id
= coding_categories
[coding_priorities
[0]].id
;
7988 return CODING_ID_NAME (id
);
7991 #if defined (WINDOWSNT) || defined (CYGWIN)
7994 from_unicode (Lisp_Object str
)
7997 if (!STRING_MULTIBYTE (str
) &&
8000 str
= Fsubstring (str
, make_number (0), make_number (-1));
8003 return code_convert_string_norecord (str
, Qutf_16le
, 0);
8007 to_unicode (Lisp_Object str
, Lisp_Object
*buf
)
8009 *buf
= code_convert_string_norecord (str
, Qutf_16le
, 1);
8010 /* We need to make another copy (in addition to the one made by
8011 code_convert_string_norecord) to ensure that the final string is
8012 _doubly_ zero terminated --- that is, that the string is
8013 terminated by two zero bytes and one utf-16le null character.
8014 Because strings are already terminated with a single zero byte,
8015 we just add one additional zero. */
8016 str
= make_uninit_string (SBYTES (*buf
) + 1);
8017 memcpy (SDATA (str
), SDATA (*buf
), SBYTES (*buf
));
8018 SDATA (str
) [SBYTES (*buf
)] = '\0';
8020 return WCSDATA (*buf
);
8023 #endif /* WINDOWSNT || CYGWIN */
8027 /*** 8. Emacs Lisp library functions ***/
8029 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8030 doc
: /* Return t if OBJECT is nil or a coding-system.
8031 See the documentation of `define-coding-system' for information
8032 about coding-system objects. */)
8033 (Lisp_Object object
)
8036 || CODING_SYSTEM_ID (object
) >= 0)
8038 if (! SYMBOLP (object
)
8039 || NILP (Fget (object
, Qcoding_system_define_form
)))
8044 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8045 Sread_non_nil_coding_system
, 1, 1, 0,
8046 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8047 (Lisp_Object prompt
)
8052 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8053 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8055 while (SCHARS (val
) == 0);
8056 return (Fintern (val
, Qnil
));
8059 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8060 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8061 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8062 Ignores case when completing coding systems (all Emacs coding systems
8063 are lower-case). */)
8064 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8067 ptrdiff_t count
= SPECPDL_INDEX ();
8069 if (SYMBOLP (default_coding_system
))
8070 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8071 specbind (Qcompletion_ignore_case
, Qt
);
8072 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8073 Qt
, Qnil
, Qcoding_system_history
,
8074 default_coding_system
, Qnil
);
8075 unbind_to (count
, Qnil
);
8076 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8079 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8081 doc
: /* Check validity of CODING-SYSTEM.
8082 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8083 It is valid if it is nil or a symbol defined as a coding system by the
8084 function `define-coding-system'. */)
8085 (Lisp_Object coding_system
)
8087 Lisp_Object define_form
;
8089 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8090 if (! NILP (define_form
))
8092 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8093 safe_eval (define_form
);
8095 if (!NILP (Fcoding_system_p (coding_system
)))
8096 return coding_system
;
8097 xsignal1 (Qcoding_system_error
, coding_system
);
8101 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8102 HIGHEST, return the coding system of the highest
8103 priority among the detected coding systems. Otherwise return a
8104 list of detected coding systems sorted by their priorities. If
8105 MULTIBYTEP, it is assumed that the bytes are in correct
8106 multibyte form but contains only ASCII and eight-bit chars.
8107 Otherwise, the bytes are raw bytes.
8109 CODING-SYSTEM controls the detection as below:
8111 If it is nil, detect both text-format and eol-format. If the
8112 text-format part of CODING-SYSTEM is already specified
8113 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8114 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8115 detect only text-format. */
8118 detect_coding_system (const unsigned char *src
,
8119 ptrdiff_t src_chars
, ptrdiff_t src_bytes
,
8120 bool highest
, bool multibytep
,
8121 Lisp_Object coding_system
)
8123 const unsigned char *src_end
= src
+ src_bytes
;
8124 Lisp_Object attrs
, eol_type
;
8125 Lisp_Object val
= Qnil
;
8126 struct coding_system coding
;
8128 struct coding_detection_info detect_info
;
8129 enum coding_category base_category
;
8130 bool null_byte_found
= 0, eight_bit_found
= 0;
8132 if (NILP (coding_system
))
8133 coding_system
= Qundecided
;
8134 setup_coding_system (coding_system
, &coding
);
8135 attrs
= CODING_ID_ATTRS (coding
.id
);
8136 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8137 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8139 coding
.source
= src
;
8140 coding
.src_chars
= src_chars
;
8141 coding
.src_bytes
= src_bytes
;
8142 coding
.src_multibyte
= multibytep
;
8143 coding
.consumed
= 0;
8144 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8145 coding
.head_ascii
= 0;
8147 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8149 /* At first, detect text-format if necessary. */
8150 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8151 if (base_category
== coding_category_undecided
)
8153 enum coding_category category
IF_LINT (= 0);
8154 struct coding_system
*this IF_LINT (= NULL
);
8157 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8158 for (; src
< src_end
; src
++)
8163 eight_bit_found
= 1;
8164 if (null_byte_found
)
8169 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8170 && ! inhibit_iso_escape_detection
8171 && ! detect_info
.checked
)
8173 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8175 /* We have scanned the whole data. */
8176 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8178 /* We didn't find an 8-bit code. We may
8179 have found a null-byte, but it's very
8180 rare that a binary file confirm to
8183 coding
.head_ascii
= src
- coding
.source
;
8185 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8189 else if (! c
&& !inhibit_null_byte_detection
)
8191 null_byte_found
= 1;
8192 if (eight_bit_found
)
8195 if (! eight_bit_found
)
8196 coding
.head_ascii
++;
8198 else if (! eight_bit_found
)
8199 coding
.head_ascii
++;
8202 if (null_byte_found
|| eight_bit_found
8203 || coding
.head_ascii
< coding
.src_bytes
8204 || detect_info
.found
)
8206 if (coding
.head_ascii
== coding
.src_bytes
)
8207 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8208 for (i
= 0; i
< coding_category_raw_text
; i
++)
8210 category
= coding_priorities
[i
];
8211 this = coding_categories
+ category
;
8212 if (detect_info
.found
& (1 << category
))
8217 if (null_byte_found
)
8219 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8220 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8222 for (i
= 0; i
< coding_category_raw_text
; i
++)
8224 category
= coding_priorities
[i
];
8225 this = coding_categories
+ category
;
8229 /* No coding system of this category is defined. */
8230 detect_info
.rejected
|= (1 << category
);
8232 else if (category
>= coding_category_raw_text
)
8234 else if (detect_info
.checked
& (1 << category
))
8237 && (detect_info
.found
& (1 << category
)))
8240 else if ((*(this->detector
)) (&coding
, &detect_info
)
8242 && (detect_info
.found
& (1 << category
)))
8244 if (category
== coding_category_utf_16_auto
)
8246 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8247 category
= coding_category_utf_16_le
;
8249 category
= coding_category_utf_16_be
;
8257 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8260 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8261 id
= CODING_SYSTEM_ID (Qno_conversion
);
8262 val
= Fcons (make_number (id
), Qnil
);
8264 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8266 detect_info
.found
= CATEGORY_MASK_ANY
;
8267 id
= coding_categories
[coding_category_undecided
].id
;
8268 val
= Fcons (make_number (id
), Qnil
);
8272 if (detect_info
.found
)
8274 detect_info
.found
= 1 << category
;
8275 val
= Fcons (make_number (this->id
), Qnil
);
8278 for (i
= 0; i
< coding_category_raw_text
; i
++)
8279 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8281 detect_info
.found
= 1 << coding_priorities
[i
];
8282 id
= coding_categories
[coding_priorities
[i
]].id
;
8283 val
= Fcons (make_number (id
), Qnil
);
8289 int mask
= detect_info
.rejected
| detect_info
.found
;
8292 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8294 category
= coding_priorities
[i
];
8295 if (! (mask
& (1 << category
)))
8297 found
|= 1 << category
;
8298 id
= coding_categories
[category
].id
;
8300 val
= Fcons (make_number (id
), val
);
8303 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8305 category
= coding_priorities
[i
];
8306 if (detect_info
.found
& (1 << category
))
8308 id
= coding_categories
[category
].id
;
8309 val
= Fcons (make_number (id
), val
);
8312 detect_info
.found
|= found
;
8315 else if (base_category
== coding_category_utf_8_auto
)
8317 if (detect_coding_utf_8 (&coding
, &detect_info
))
8319 struct coding_system
*this;
8321 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8322 this = coding_categories
+ coding_category_utf_8_sig
;
8324 this = coding_categories
+ coding_category_utf_8_nosig
;
8325 val
= Fcons (make_number (this->id
), Qnil
);
8328 else if (base_category
== coding_category_utf_16_auto
)
8330 if (detect_coding_utf_16 (&coding
, &detect_info
))
8332 struct coding_system
*this;
8334 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8335 this = coding_categories
+ coding_category_utf_16_le
;
8336 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8337 this = coding_categories
+ coding_category_utf_16_be
;
8338 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8339 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8341 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8342 val
= Fcons (make_number (this->id
), Qnil
);
8347 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8348 val
= Fcons (make_number (coding
.id
), Qnil
);
8351 /* Then, detect eol-format if necessary. */
8353 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8356 if (VECTORP (eol_type
))
8358 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8360 if (null_byte_found
)
8361 normal_eol
= EOL_SEEN_LF
;
8363 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8364 coding_category_raw_text
);
8366 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8367 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8368 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8369 coding_category_utf_16_be
);
8370 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8371 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8372 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8373 coding_category_utf_16_le
);
8377 if (EQ (eol_type
, Qunix
))
8378 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8379 else if (EQ (eol_type
, Qdos
))
8380 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8382 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8385 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8387 enum coding_category category
;
8390 id
= XINT (XCAR (tail
));
8391 attrs
= CODING_ID_ATTRS (id
);
8392 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8393 eol_type
= CODING_ID_EOL_TYPE (id
);
8394 if (VECTORP (eol_type
))
8396 if (category
== coding_category_utf_16_be
8397 || category
== coding_category_utf_16_be_nosig
)
8398 this_eol
= utf_16_be_eol
;
8399 else if (category
== coding_category_utf_16_le
8400 || category
== coding_category_utf_16_le_nosig
)
8401 this_eol
= utf_16_le_eol
;
8403 this_eol
= normal_eol
;
8405 if (this_eol
== EOL_SEEN_LF
)
8406 XSETCAR (tail
, AREF (eol_type
, 0));
8407 else if (this_eol
== EOL_SEEN_CRLF
)
8408 XSETCAR (tail
, AREF (eol_type
, 1));
8409 else if (this_eol
== EOL_SEEN_CR
)
8410 XSETCAR (tail
, AREF (eol_type
, 2));
8412 XSETCAR (tail
, CODING_ID_NAME (id
));
8415 XSETCAR (tail
, CODING_ID_NAME (id
));
8419 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8423 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8425 doc
: /* Detect coding system of the text in the region between START and END.
8426 Return a list of possible coding systems ordered by priority.
8427 The coding systems to try and their priorities follows what
8428 the function `coding-system-priority-list' (which see) returns.
8430 If only ASCII characters are found (except for such ISO-2022 control
8431 characters as ESC), it returns a list of single element `undecided'
8432 or its subsidiary coding system according to a detected end-of-line
8435 If optional argument HIGHEST is non-nil, return the coding system of
8436 highest priority. */)
8437 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8440 ptrdiff_t from_byte
, to_byte
;
8442 validate_region (&start
, &end
);
8443 from
= XINT (start
), to
= XINT (end
);
8444 from_byte
= CHAR_TO_BYTE (from
);
8445 to_byte
= CHAR_TO_BYTE (to
);
8447 if (from
< GPT
&& to
>= GPT
)
8448 move_gap_both (to
, to_byte
);
8450 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8451 to
- from
, to_byte
- from_byte
,
8453 !NILP (BVAR (current_buffer
8454 , enable_multibyte_characters
)),
8458 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8460 doc
: /* Detect coding system of the text in STRING.
8461 Return a list of possible coding systems ordered by priority.
8462 The coding systems to try and their priorities follows what
8463 the function `coding-system-priority-list' (which see) returns.
8465 If only ASCII characters are found (except for such ISO-2022 control
8466 characters as ESC), it returns a list of single element `undecided'
8467 or its subsidiary coding system according to a detected end-of-line
8470 If optional argument HIGHEST is non-nil, return the coding system of
8471 highest priority. */)
8472 (Lisp_Object string
, Lisp_Object highest
)
8474 CHECK_STRING (string
);
8476 return detect_coding_system (SDATA (string
),
8477 SCHARS (string
), SBYTES (string
),
8478 !NILP (highest
), STRING_MULTIBYTE (string
),
8484 char_encodable_p (int c
, Lisp_Object attrs
)
8487 struct charset
*charset
;
8488 Lisp_Object translation_table
;
8490 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8491 if (! NILP (translation_table
))
8492 c
= translate_char (translation_table
, c
);
8493 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8494 CONSP (tail
); tail
= XCDR (tail
))
8496 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8497 if (CHAR_CHARSET_P (c
, charset
))
8500 return (! NILP (tail
));
8504 /* Return a list of coding systems that safely encode the text between
8505 START and END. If EXCLUDE is non-nil, it is a list of coding
8506 systems not to check. The returned list doesn't contain any such
8507 coding systems. In any case, if the text contains only ASCII or is
8508 unibyte, return t. */
8510 DEFUN ("find-coding-systems-region-internal",
8511 Ffind_coding_systems_region_internal
,
8512 Sfind_coding_systems_region_internal
, 2, 3, 0,
8513 doc
: /* Internal use only. */)
8514 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8516 Lisp_Object coding_attrs_list
, safe_codings
;
8517 ptrdiff_t start_byte
, end_byte
;
8518 const unsigned char *p
, *pbeg
, *pend
;
8520 Lisp_Object tail
, elt
, work_table
;
8522 if (STRINGP (start
))
8524 if (!STRING_MULTIBYTE (start
)
8525 || SCHARS (start
) == SBYTES (start
))
8528 end_byte
= SBYTES (start
);
8532 CHECK_NUMBER_COERCE_MARKER (start
);
8533 CHECK_NUMBER_COERCE_MARKER (end
);
8534 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8535 args_out_of_range (start
, end
);
8536 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8538 start_byte
= CHAR_TO_BYTE (XINT (start
));
8539 end_byte
= CHAR_TO_BYTE (XINT (end
));
8540 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8543 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8545 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8546 move_gap_both (XINT (start
), start_byte
);
8548 move_gap_both (XINT (end
), end_byte
);
8552 coding_attrs_list
= Qnil
;
8553 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8555 || NILP (Fmemq (XCAR (tail
), exclude
)))
8559 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8560 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8561 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8563 ASET (attrs
, coding_attr_trans_tbl
,
8564 get_translation_table (attrs
, 1, NULL
));
8565 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8569 if (STRINGP (start
))
8570 p
= pbeg
= SDATA (start
);
8572 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8573 pend
= p
+ (end_byte
- start_byte
);
8575 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8576 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8578 work_table
= Fmake_char_table (Qnil
, Qnil
);
8581 if (ASCII_BYTE_P (*p
))
8585 c
= STRING_CHAR_ADVANCE (p
);
8586 if (!NILP (char_table_ref (work_table
, c
)))
8587 /* This character was already checked. Ignore it. */
8590 charset_map_loaded
= 0;
8591 for (tail
= coding_attrs_list
; CONSP (tail
);)
8596 else if (char_encodable_p (c
, elt
))
8598 else if (CONSP (XCDR (tail
)))
8600 XSETCAR (tail
, XCAR (XCDR (tail
)));
8601 XSETCDR (tail
, XCDR (XCDR (tail
)));
8605 XSETCAR (tail
, Qnil
);
8609 if (charset_map_loaded
)
8611 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8613 if (STRINGP (start
))
8614 pbeg
= SDATA (start
);
8616 pbeg
= BYTE_POS_ADDR (start_byte
);
8617 p
= pbeg
+ p_offset
;
8618 pend
= pbeg
+ pend_offset
;
8620 char_table_set (work_table
, c
, Qt
);
8624 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8625 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8626 if (! NILP (XCAR (tail
)))
8627 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8629 return safe_codings
;
8633 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8634 Sunencodable_char_position
, 3, 5, 0,
8636 Return position of first un-encodable character in a region.
8637 START and END specify the region and CODING-SYSTEM specifies the
8638 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8640 If optional 4th argument COUNT is non-nil, it specifies at most how
8641 many un-encodable characters to search. In this case, the value is a
8644 If optional 5th argument STRING is non-nil, it is a string to search
8645 for un-encodable characters. In that case, START and END are indexes
8647 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8650 struct coding_system coding
;
8651 Lisp_Object attrs
, charset_list
, translation_table
;
8652 Lisp_Object positions
;
8654 const unsigned char *p
, *stop
, *pend
;
8655 bool ascii_compatible
;
8657 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8658 attrs
= CODING_ID_ATTRS (coding
.id
);
8659 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8661 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8662 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8663 translation_table
= get_translation_table (attrs
, 1, NULL
);
8667 validate_region (&start
, &end
);
8668 from
= XINT (start
);
8670 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8671 || (ascii_compatible
8672 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8674 p
= CHAR_POS_ADDR (from
);
8675 pend
= CHAR_POS_ADDR (to
);
8676 if (from
< GPT
&& to
>= GPT
)
8683 CHECK_STRING (string
);
8684 CHECK_NATNUM (start
);
8686 if (! (XINT (start
) <= XINT (end
) && XINT (end
) <= SCHARS (string
)))
8687 args_out_of_range_3 (string
, start
, end
);
8688 from
= XINT (start
);
8690 if (! STRING_MULTIBYTE (string
))
8692 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8693 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8694 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8702 CHECK_NATNUM (count
);
8707 charset_map_loaded
= 0;
8712 if (ascii_compatible
)
8713 while (p
< stop
&& ASCII_BYTE_P (*p
))
8723 c
= STRING_CHAR_ADVANCE (p
);
8724 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8725 && ! char_charset (translate_char (translation_table
, c
),
8726 charset_list
, NULL
))
8728 positions
= Fcons (make_number (from
), positions
);
8735 if (charset_map_loaded
&& NILP (string
))
8737 p
= CHAR_POS_ADDR (from
);
8738 pend
= CHAR_POS_ADDR (to
);
8739 if (from
< GPT
&& to
>= GPT
)
8743 charset_map_loaded
= 0;
8747 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8751 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8752 Scheck_coding_systems_region
, 3, 3, 0,
8753 doc
: /* Check if the region is encodable by coding systems.
8755 START and END are buffer positions specifying the region.
8756 CODING-SYSTEM-LIST is a list of coding systems to check.
8758 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8759 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8760 whole region, POS0, POS1, ... are buffer positions where non-encodable
8761 characters are found.
8763 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8766 START may be a string. In that case, check if the string is
8767 encodable, and the value contains indices to the string instead of
8768 buffer positions. END is ignored.
8770 If the current buffer (or START if it is a string) is unibyte, the value
8772 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8775 ptrdiff_t start_byte
, end_byte
;
8777 const unsigned char *p
, *pbeg
, *pend
;
8779 Lisp_Object tail
, elt
, attrs
;
8781 if (STRINGP (start
))
8783 if (!STRING_MULTIBYTE (start
)
8784 || SCHARS (start
) == SBYTES (start
))
8787 end_byte
= SBYTES (start
);
8792 CHECK_NUMBER_COERCE_MARKER (start
);
8793 CHECK_NUMBER_COERCE_MARKER (end
);
8794 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8795 args_out_of_range (start
, end
);
8796 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8798 start_byte
= CHAR_TO_BYTE (XINT (start
));
8799 end_byte
= CHAR_TO_BYTE (XINT (end
));
8800 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8803 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8805 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8806 move_gap_both (XINT (start
), start_byte
);
8808 move_gap_both (XINT (end
), end_byte
);
8814 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8817 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8818 ASET (attrs
, coding_attr_trans_tbl
,
8819 get_translation_table (attrs
, 1, NULL
));
8820 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8823 if (STRINGP (start
))
8824 p
= pbeg
= SDATA (start
);
8826 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8827 pend
= p
+ (end_byte
- start_byte
);
8829 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8830 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8834 if (ASCII_BYTE_P (*p
))
8838 c
= STRING_CHAR_ADVANCE (p
);
8840 charset_map_loaded
= 0;
8841 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8843 elt
= XCDR (XCAR (tail
));
8844 if (! char_encodable_p (c
, XCAR (elt
)))
8845 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8847 if (charset_map_loaded
)
8849 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8851 if (STRINGP (start
))
8852 pbeg
= SDATA (start
);
8854 pbeg
= BYTE_POS_ADDR (start_byte
);
8855 p
= pbeg
+ p_offset
;
8856 pend
= pbeg
+ pend_offset
;
8864 for (; CONSP (tail
); tail
= XCDR (tail
))
8867 if (CONSP (XCDR (XCDR (elt
))))
8868 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8877 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8878 Lisp_Object coding_system
, Lisp_Object dst_object
,
8879 bool encodep
, bool norecord
)
8881 struct coding_system coding
;
8882 ptrdiff_t from
, from_byte
, to
, to_byte
;
8883 Lisp_Object src_object
;
8885 if (NILP (coding_system
))
8886 coding_system
= Qno_conversion
;
8888 CHECK_CODING_SYSTEM (coding_system
);
8889 src_object
= Fcurrent_buffer ();
8890 if (NILP (dst_object
))
8891 dst_object
= src_object
;
8892 else if (! EQ (dst_object
, Qt
))
8893 CHECK_BUFFER (dst_object
);
8895 validate_region (&start
, &end
);
8896 from
= XFASTINT (start
);
8897 from_byte
= CHAR_TO_BYTE (from
);
8898 to
= XFASTINT (end
);
8899 to_byte
= CHAR_TO_BYTE (to
);
8901 setup_coding_system (coding_system
, &coding
);
8902 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8905 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8908 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8911 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8913 return (BUFFERP (dst_object
)
8914 ? make_number (coding
.produced_char
)
8915 : coding
.dst_object
);
8919 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8920 3, 4, "r\nzCoding system: ",
8921 doc
: /* Decode the current region from the specified coding system.
8922 When called from a program, takes four arguments:
8923 START, END, CODING-SYSTEM, and DESTINATION.
8924 START and END are buffer positions.
8926 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8927 If nil, the region between START and END is replaced by the decoded text.
8928 If buffer, the decoded text is inserted in that buffer after point (point
8930 In those cases, the length of the decoded text is returned.
8931 If DESTINATION is t, the decoded text is returned.
8933 This function sets `last-coding-system-used' to the precise coding system
8934 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8935 not fully specified.) */)
8936 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8938 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8941 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8942 3, 4, "r\nzCoding system: ",
8943 doc
: /* Encode the current region by specified coding system.
8944 When called from a program, takes four arguments:
8945 START, END, CODING-SYSTEM and DESTINATION.
8946 START and END are buffer positions.
8948 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8949 If nil, the region between START and END is replace by the encoded text.
8950 If buffer, the encoded text is inserted in that buffer after point (point
8952 In those cases, the length of the encoded text is returned.
8953 If DESTINATION is t, the encoded text is returned.
8955 This function sets `last-coding-system-used' to the precise coding system
8956 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8957 not fully specified.) */)
8958 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8960 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8964 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8965 Lisp_Object dst_object
, bool encodep
, bool nocopy
,
8968 struct coding_system coding
;
8969 ptrdiff_t chars
, bytes
;
8971 CHECK_STRING (string
);
8972 if (NILP (coding_system
))
8975 Vlast_coding_system_used
= Qno_conversion
;
8976 if (NILP (dst_object
))
8977 return (nocopy
? Fcopy_sequence (string
) : string
);
8980 if (NILP (coding_system
))
8981 coding_system
= Qno_conversion
;
8983 CHECK_CODING_SYSTEM (coding_system
);
8984 if (NILP (dst_object
))
8986 else if (! EQ (dst_object
, Qt
))
8987 CHECK_BUFFER (dst_object
);
8989 setup_coding_system (coding_system
, &coding
);
8990 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8991 chars
= SCHARS (string
);
8992 bytes
= SBYTES (string
);
8994 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8996 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8998 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9000 return (BUFFERP (dst_object
)
9001 ? make_number (coding
.produced_char
)
9002 : coding
.dst_object
);
9006 /* Encode or decode STRING according to CODING_SYSTEM.
9007 Do not set Vlast_coding_system_used.
9009 This function is called only from macros DECODE_FILE and
9010 ENCODE_FILE, thus we ignore character composition. */
9013 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9016 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9020 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9022 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9024 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9025 if the decoding operation is trivial.
9027 Optional fourth arg BUFFER non-nil means that the decoded text is
9028 inserted in that buffer after point (point does not move). In this
9029 case, the return value is the length of the decoded text.
9031 This function sets `last-coding-system-used' to the precise coding system
9032 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9033 not fully specified.) */)
9034 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9036 return code_convert_string (string
, coding_system
, buffer
,
9037 0, ! NILP (nocopy
), 0);
9040 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9042 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9044 Optional third arg NOCOPY non-nil means it is OK to return STRING
9045 itself if the encoding operation is trivial.
9047 Optional fourth arg BUFFER non-nil means that the encoded text is
9048 inserted in that buffer after point (point does not move). In this
9049 case, the return value is the length of the encoded text.
9051 This function sets `last-coding-system-used' to the precise coding system
9052 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9053 not fully specified.) */)
9054 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9056 return code_convert_string (string
, coding_system
, buffer
,
9057 1, ! NILP (nocopy
), 0);
9061 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9062 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9063 Return the corresponding character. */)
9066 Lisp_Object spec
, attrs
, val
;
9067 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9071 CHECK_NATNUM (code
);
9072 ch
= XFASTINT (code
);
9073 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9074 attrs
= AREF (spec
, 0);
9076 if (ASCII_BYTE_P (ch
)
9077 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9080 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9081 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9082 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9083 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9088 charset
= charset_roman
;
9090 else if (ch
>= 0xA0 && ch
< 0xDF)
9093 charset
= charset_kana
;
9097 EMACS_INT c1
= ch
>> 8;
9100 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9101 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9102 error ("Invalid code: %"pI
"d", ch
);
9105 charset
= charset_kanji
;
9107 c
= DECODE_CHAR (charset
, c
);
9109 error ("Invalid code: %"pI
"d", ch
);
9110 return make_number (c
);
9114 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9115 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9116 Return the corresponding code in SJIS. */)
9119 Lisp_Object spec
, attrs
, charset_list
;
9121 struct charset
*charset
;
9124 CHECK_CHARACTER (ch
);
9126 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9127 attrs
= AREF (spec
, 0);
9129 if (ASCII_CHAR_P (c
)
9130 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9133 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9134 charset
= char_charset (c
, charset_list
, &code
);
9135 if (code
== CHARSET_INVALID_CODE (charset
))
9136 error ("Can't encode by shift_jis encoding: %c", c
);
9139 return make_number (code
);
9142 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9143 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9144 Return the corresponding character. */)
9147 Lisp_Object spec
, attrs
, val
;
9148 struct charset
*charset_roman
, *charset_big5
, *charset
;
9152 CHECK_NATNUM (code
);
9153 ch
= XFASTINT (code
);
9154 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9155 attrs
= AREF (spec
, 0);
9157 if (ASCII_BYTE_P (ch
)
9158 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9161 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9162 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9163 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9168 charset
= charset_roman
;
9172 EMACS_INT b1
= ch
>> 8;
9174 if (b1
< 0xA1 || b1
> 0xFE
9175 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9176 error ("Invalid code: %"pI
"d", ch
);
9178 charset
= charset_big5
;
9180 c
= DECODE_CHAR (charset
, c
);
9182 error ("Invalid code: %"pI
"d", ch
);
9183 return make_number (c
);
9186 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9187 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9188 Return the corresponding character code in Big5. */)
9191 Lisp_Object spec
, attrs
, charset_list
;
9192 struct charset
*charset
;
9196 CHECK_CHARACTER (ch
);
9198 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9199 attrs
= AREF (spec
, 0);
9200 if (ASCII_CHAR_P (c
)
9201 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9204 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9205 charset
= char_charset (c
, charset_list
, &code
);
9206 if (code
== CHARSET_INVALID_CODE (charset
))
9207 error ("Can't encode by Big5 encoding: %c", c
);
9209 return make_number (code
);
9213 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9214 Sset_terminal_coding_system_internal
, 1, 2, 0,
9215 doc
: /* Internal use only. */)
9216 (Lisp_Object coding_system
, Lisp_Object terminal
)
9218 struct terminal
*term
= get_terminal (terminal
, 1);
9219 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9220 CHECK_SYMBOL (coding_system
);
9221 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9222 /* We had better not send unsafe characters to terminal. */
9223 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9224 /* Character composition should be disabled. */
9225 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9226 terminal_coding
->src_multibyte
= 1;
9227 terminal_coding
->dst_multibyte
= 0;
9229 (term
, (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
9230 ? coding_charset_list (terminal_coding
)
9231 : Fcons (make_number (charset_ascii
), Qnil
)));
9235 DEFUN ("set-safe-terminal-coding-system-internal",
9236 Fset_safe_terminal_coding_system_internal
,
9237 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9238 doc
: /* Internal use only. */)
9239 (Lisp_Object coding_system
)
9241 CHECK_SYMBOL (coding_system
);
9242 setup_coding_system (Fcheck_coding_system (coding_system
),
9243 &safe_terminal_coding
);
9244 /* Character composition should be disabled. */
9245 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9246 safe_terminal_coding
.src_multibyte
= 1;
9247 safe_terminal_coding
.dst_multibyte
= 0;
9251 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9252 Sterminal_coding_system
, 0, 1, 0,
9253 doc
: /* Return coding system specified for terminal output on the given terminal.
9254 TERMINAL may be a terminal object, a frame, or nil for the selected
9255 frame's terminal device. */)
9256 (Lisp_Object terminal
)
9258 struct coding_system
*terminal_coding
9259 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9260 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9262 /* For backward compatibility, return nil if it is `undecided'. */
9263 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9266 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9267 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9268 doc
: /* Internal use only. */)
9269 (Lisp_Object coding_system
, Lisp_Object terminal
)
9271 struct terminal
*t
= get_terminal (terminal
, 1);
9272 CHECK_SYMBOL (coding_system
);
9273 if (NILP (coding_system
))
9274 coding_system
= Qno_conversion
;
9276 Fcheck_coding_system (coding_system
);
9277 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9278 /* Character composition should be disabled. */
9279 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9280 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9284 DEFUN ("keyboard-coding-system",
9285 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9286 doc
: /* Return coding system specified for decoding keyboard input. */)
9287 (Lisp_Object terminal
)
9289 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9290 (get_terminal (terminal
, 1))->id
);
9294 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9295 Sfind_operation_coding_system
, 1, MANY
, 0,
9296 doc
: /* Choose a coding system for an operation based on the target name.
9297 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9298 DECODING-SYSTEM is the coding system to use for decoding
9299 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9300 for encoding (in case OPERATION does encoding).
9302 The first argument OPERATION specifies an I/O primitive:
9303 For file I/O, `insert-file-contents' or `write-region'.
9304 For process I/O, `call-process', `call-process-region', or `start-process'.
9305 For network I/O, `open-network-stream'.
9307 The remaining arguments should be the same arguments that were passed
9308 to the primitive. Depending on which primitive, one of those arguments
9309 is selected as the TARGET. For example, if OPERATION does file I/O,
9310 whichever argument specifies the file name is TARGET.
9312 TARGET has a meaning which depends on OPERATION:
9313 For file I/O, TARGET is a file name (except for the special case below).
9314 For process I/O, TARGET is a process name.
9315 For network I/O, TARGET is a service name or a port number.
9317 This function looks up what is specified for TARGET in
9318 `file-coding-system-alist', `process-coding-system-alist',
9319 or `network-coding-system-alist' depending on OPERATION.
9320 They may specify a coding system, a cons of coding systems,
9321 or a function symbol to call.
9322 In the last case, we call the function with one argument,
9323 which is a list of all the arguments given to this function.
9324 If the function can't decide a coding system, it can return
9325 `undecided' so that the normal code-detection is performed.
9327 If OPERATION is `insert-file-contents', the argument corresponding to
9328 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9329 file name to look up, and BUFFER is a buffer that contains the file's
9330 contents (not yet decoded). If `file-coding-system-alist' specifies a
9331 function to call for FILENAME, that function should examine the
9332 contents of BUFFER instead of reading the file.
9334 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9335 (ptrdiff_t nargs
, Lisp_Object
*args
)
9337 Lisp_Object operation
, target_idx
, target
, val
;
9338 register Lisp_Object chain
;
9341 error ("Too few arguments");
9342 operation
= args
[0];
9343 if (!SYMBOLP (operation
)
9344 || (target_idx
= Fget (operation
, Qtarget_idx
), !NATNUMP (target_idx
)))
9345 error ("Invalid first argument");
9346 if (nargs
<= 1 + XFASTINT (target_idx
))
9347 error ("Too few arguments for operation `%s'",
9348 SDATA (SYMBOL_NAME (operation
)));
9349 target
= args
[XFASTINT (target_idx
) + 1];
9350 if (!(STRINGP (target
)
9351 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9352 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9353 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9354 error ("Invalid argument %"pI
"d of operation `%s'",
9355 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9357 target
= XCAR (target
);
9359 chain
= ((EQ (operation
, Qinsert_file_contents
)
9360 || EQ (operation
, Qwrite_region
))
9361 ? Vfile_coding_system_alist
9362 : (EQ (operation
, Qopen_network_stream
)
9363 ? Vnetwork_coding_system_alist
9364 : Vprocess_coding_system_alist
));
9368 for (; CONSP (chain
); chain
= XCDR (chain
))
9374 && ((STRINGP (target
)
9375 && STRINGP (XCAR (elt
))
9376 && fast_string_match (XCAR (elt
), target
) >= 0)
9377 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9380 /* Here, if VAL is both a valid coding system and a valid
9381 function symbol, we return VAL as a coding system. */
9384 if (! SYMBOLP (val
))
9386 if (! NILP (Fcoding_system_p (val
)))
9387 return Fcons (val
, val
);
9388 if (! NILP (Ffboundp (val
)))
9390 /* We use call1 rather than safe_call1
9391 so as to get bug reports about functions called here
9392 which don't handle the current interface. */
9393 val
= call1 (val
, Flist (nargs
, args
));
9396 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9397 return Fcons (val
, val
);
9405 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9406 Sset_coding_system_priority
, 0, MANY
, 0,
9407 doc
: /* Assign higher priority to the coding systems given as arguments.
9408 If multiple coding systems belong to the same category,
9409 all but the first one are ignored.
9411 usage: (set-coding-system-priority &rest coding-systems) */)
9412 (ptrdiff_t nargs
, Lisp_Object
*args
)
9415 bool changed
[coding_category_max
];
9416 enum coding_category priorities
[coding_category_max
];
9418 memset (changed
, 0, sizeof changed
);
9420 for (i
= j
= 0; i
< nargs
; i
++)
9422 enum coding_category category
;
9423 Lisp_Object spec
, attrs
;
9425 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9426 attrs
= AREF (spec
, 0);
9427 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9428 if (changed
[category
])
9429 /* Ignore this coding system because a coding system of the
9430 same category already had a higher priority. */
9432 changed
[category
] = 1;
9433 priorities
[j
++] = category
;
9434 if (coding_categories
[category
].id
>= 0
9435 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9436 setup_coding_system (args
[i
], &coding_categories
[category
]);
9437 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9440 /* Now we have decided top J priorities. Reflect the order of the
9441 original priorities to the remaining priorities. */
9443 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9445 while (j
< coding_category_max
9446 && changed
[coding_priorities
[j
]])
9448 if (j
== coding_category_max
)
9450 priorities
[i
] = coding_priorities
[j
];
9453 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9455 /* Update `coding-category-list'. */
9456 Vcoding_category_list
= Qnil
;
9457 for (i
= coding_category_max
; i
-- > 0; )
9458 Vcoding_category_list
9459 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9460 Vcoding_category_list
);
9465 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9466 Scoding_system_priority_list
, 0, 1, 0,
9467 doc
: /* Return a list of coding systems ordered by their priorities.
9468 The list contains a subset of coding systems; i.e. coding systems
9469 assigned to each coding category (see `coding-category-list').
9471 HIGHESTP non-nil means just return the highest priority one. */)
9472 (Lisp_Object highestp
)
9477 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9479 enum coding_category category
= coding_priorities
[i
];
9480 int id
= coding_categories
[category
].id
;
9485 attrs
= CODING_ID_ATTRS (id
);
9486 if (! NILP (highestp
))
9487 return CODING_ATTR_BASE_NAME (attrs
);
9488 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9490 return Fnreverse (val
);
9493 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9496 make_subsidiaries (Lisp_Object base
)
9498 Lisp_Object subsidiaries
;
9499 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
9500 char *buf
= alloca (base_name_len
+ 6);
9503 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9504 subsidiaries
= make_uninit_vector (3);
9505 for (i
= 0; i
< 3; i
++)
9507 strcpy (buf
+ base_name_len
, suffixes
[i
]);
9508 ASET (subsidiaries
, i
, intern (buf
));
9510 return subsidiaries
;
9514 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9515 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9516 doc
: /* For internal use only.
9517 usage: (define-coding-system-internal ...) */)
9518 (ptrdiff_t nargs
, Lisp_Object
*args
)
9521 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9522 Lisp_Object attrs
; /* Vector of attributes. */
9523 Lisp_Object eol_type
;
9524 Lisp_Object aliases
;
9525 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9526 enum coding_category category
;
9527 Lisp_Object tail
, val
;
9528 int max_charset_id
= 0;
9531 if (nargs
< coding_arg_max
)
9534 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9536 name
= args
[coding_arg_name
];
9537 CHECK_SYMBOL (name
);
9538 ASET (attrs
, coding_attr_base_name
, name
);
9540 val
= args
[coding_arg_mnemonic
];
9541 if (! STRINGP (val
))
9542 CHECK_CHARACTER (val
);
9543 ASET (attrs
, coding_attr_mnemonic
, val
);
9545 coding_type
= args
[coding_arg_coding_type
];
9546 CHECK_SYMBOL (coding_type
);
9547 ASET (attrs
, coding_attr_type
, coding_type
);
9549 charset_list
= args
[coding_arg_charset_list
];
9550 if (SYMBOLP (charset_list
))
9552 if (EQ (charset_list
, Qiso_2022
))
9554 if (! EQ (coding_type
, Qiso_2022
))
9555 error ("Invalid charset-list");
9556 charset_list
= Viso_2022_charset_list
;
9558 else if (EQ (charset_list
, Qemacs_mule
))
9560 if (! EQ (coding_type
, Qemacs_mule
))
9561 error ("Invalid charset-list");
9562 charset_list
= Vemacs_mule_charset_list
;
9564 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9566 if (! RANGED_INTEGERP (0, XCAR (tail
), INT_MAX
- 1))
9567 error ("Invalid charset-list");
9568 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9569 max_charset_id
= XFASTINT (XCAR (tail
));
9574 charset_list
= Fcopy_sequence (charset_list
);
9575 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9577 struct charset
*charset
;
9580 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9581 if (EQ (coding_type
, Qiso_2022
)
9582 ? CHARSET_ISO_FINAL (charset
) < 0
9583 : EQ (coding_type
, Qemacs_mule
)
9584 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9586 error ("Can't handle charset `%s'",
9587 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9589 XSETCAR (tail
, make_number (charset
->id
));
9590 if (max_charset_id
< charset
->id
)
9591 max_charset_id
= charset
->id
;
9594 ASET (attrs
, coding_attr_charset_list
, charset_list
);
9596 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9597 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9598 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9599 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9600 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
9602 ASET (attrs
, coding_attr_ascii_compat
, args
[coding_arg_ascii_compatible_p
]);
9604 val
= args
[coding_arg_decode_translation_table
];
9605 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9607 ASET (attrs
, coding_attr_decode_tbl
, val
);
9609 val
= args
[coding_arg_encode_translation_table
];
9610 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9612 ASET (attrs
, coding_attr_encode_tbl
, val
);
9614 val
= args
[coding_arg_post_read_conversion
];
9616 ASET (attrs
, coding_attr_post_read
, val
);
9618 val
= args
[coding_arg_pre_write_conversion
];
9620 ASET (attrs
, coding_attr_pre_write
, val
);
9622 val
= args
[coding_arg_default_char
];
9624 ASET (attrs
, coding_attr_default_char
, make_number (' '));
9627 CHECK_CHARACTER (val
);
9628 ASET (attrs
, coding_attr_default_char
, val
);
9631 val
= args
[coding_arg_for_unibyte
];
9632 ASET (attrs
, coding_attr_for_unibyte
, NILP (val
) ? Qnil
: Qt
);
9634 val
= args
[coding_arg_plist
];
9636 ASET (attrs
, coding_attr_plist
, val
);
9638 if (EQ (coding_type
, Qcharset
))
9640 /* Generate a lisp vector of 256 elements. Each element is nil,
9641 integer, or a list of charset IDs.
9643 If Nth element is nil, the byte code N is invalid in this
9646 If Nth element is a number NUM, N is the first byte of a
9647 charset whose ID is NUM.
9649 If Nth element is a list of charset IDs, N is the first byte
9650 of one of them. The list is sorted by dimensions of the
9651 charsets. A charset of smaller dimension comes first. */
9652 val
= Fmake_vector (make_number (256), Qnil
);
9654 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9656 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9657 int dim
= CHARSET_DIMENSION (charset
);
9658 int idx
= (dim
- 1) * 4;
9660 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9661 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9663 for (i
= charset
->code_space
[idx
];
9664 i
<= charset
->code_space
[idx
+ 1]; i
++)
9666 Lisp_Object tmp
, tmp2
;
9669 tmp
= AREF (val
, i
);
9672 else if (NUMBERP (tmp
))
9674 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9676 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9678 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9682 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9684 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9689 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9692 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9693 XSETCAR (tmp2
, XCAR (tail
));
9699 ASET (attrs
, coding_attr_charset_valids
, val
);
9700 category
= coding_category_charset
;
9702 else if (EQ (coding_type
, Qccl
))
9706 if (nargs
< coding_arg_ccl_max
)
9709 val
= args
[coding_arg_ccl_decoder
];
9710 CHECK_CCL_PROGRAM (val
);
9712 val
= Fcopy_sequence (val
);
9713 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9715 val
= args
[coding_arg_ccl_encoder
];
9716 CHECK_CCL_PROGRAM (val
);
9718 val
= Fcopy_sequence (val
);
9719 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9721 val
= args
[coding_arg_ccl_valids
];
9722 valids
= Fmake_string (make_number (256), make_number (0));
9723 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
9730 if (! (0 <= XINT (val
) && XINT (val
) <= 255))
9731 args_out_of_range_3 (val
, make_number (0), make_number (255));
9732 from
= to
= XINT (val
);
9737 CHECK_NATNUM_CAR (val
);
9738 CHECK_NUMBER_CDR (val
);
9739 if (XINT (XCAR (val
)) > 255)
9740 args_out_of_range_3 (XCAR (val
),
9741 make_number (0), make_number (255));
9742 from
= XINT (XCAR (val
));
9743 if (! (from
<= XINT (XCDR (val
)) && XINT (XCDR (val
)) <= 255))
9744 args_out_of_range_3 (XCDR (val
),
9745 XCAR (val
), make_number (255));
9746 to
= XINT (XCDR (val
));
9748 for (i
= from
; i
<= to
; i
++)
9749 SSET (valids
, i
, 1);
9751 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9753 category
= coding_category_ccl
;
9755 else if (EQ (coding_type
, Qutf_16
))
9757 Lisp_Object bom
, endian
;
9759 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9761 if (nargs
< coding_arg_utf16_max
)
9764 bom
= args
[coding_arg_utf16_bom
];
9765 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9769 CHECK_CODING_SYSTEM (val
);
9771 CHECK_CODING_SYSTEM (val
);
9773 ASET (attrs
, coding_attr_utf_bom
, bom
);
9775 endian
= args
[coding_arg_utf16_endian
];
9776 CHECK_SYMBOL (endian
);
9779 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9780 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9781 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9783 category
= (CONSP (bom
)
9784 ? coding_category_utf_16_auto
9786 ? (EQ (endian
, Qbig
)
9787 ? coding_category_utf_16_be_nosig
9788 : coding_category_utf_16_le_nosig
)
9789 : (EQ (endian
, Qbig
)
9790 ? coding_category_utf_16_be
9791 : coding_category_utf_16_le
));
9793 else if (EQ (coding_type
, Qiso_2022
))
9795 Lisp_Object initial
, reg_usage
, request
, flags
;
9797 if (nargs
< coding_arg_iso2022_max
)
9800 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9801 CHECK_VECTOR (initial
);
9802 for (i
= 0; i
< 4; i
++)
9804 val
= AREF (initial
, i
);
9807 struct charset
*charset
;
9809 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9810 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9811 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9812 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9815 ASET (initial
, i
, make_number (-1));
9818 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9819 CHECK_CONS (reg_usage
);
9820 CHECK_NUMBER_CAR (reg_usage
);
9821 CHECK_NUMBER_CDR (reg_usage
);
9823 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9824 for (tail
= request
; CONSP (tail
); tail
= XCDR (tail
))
9832 CHECK_CHARSET_GET_ID (tmp1
, id
);
9833 CHECK_NATNUM_CDR (val
);
9834 if (XINT (XCDR (val
)) >= 4)
9835 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
9836 XSETCAR (val
, make_number (id
));
9839 flags
= args
[coding_arg_iso2022_flags
];
9840 CHECK_NATNUM (flags
);
9841 i
= XINT (flags
) & INT_MAX
;
9842 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9843 i
|= CODING_ISO_FLAG_FULL_SUPPORT
;
9844 flags
= make_number (i
);
9846 ASET (attrs
, coding_attr_iso_initial
, initial
);
9847 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9848 ASET (attrs
, coding_attr_iso_request
, request
);
9849 ASET (attrs
, coding_attr_iso_flags
, flags
);
9850 setup_iso_safe_charsets (attrs
);
9852 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9853 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9854 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9855 ? coding_category_iso_7_else
9856 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9857 ? coding_category_iso_7
9858 : coding_category_iso_7_tight
);
9861 int id
= XINT (AREF (initial
, 1));
9863 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9864 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9866 ? coding_category_iso_8_else
9867 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9868 ? coding_category_iso_8_1
9869 : coding_category_iso_8_2
);
9871 if (category
!= coding_category_iso_8_1
9872 && category
!= coding_category_iso_8_2
)
9873 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9875 else if (EQ (coding_type
, Qemacs_mule
))
9877 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9878 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9879 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9880 category
= coding_category_emacs_mule
;
9882 else if (EQ (coding_type
, Qshift_jis
))
9885 struct charset
*charset
;
9887 if (XINT (Flength (charset_list
)) != 3
9888 && XINT (Flength (charset_list
)) != 4)
9889 error ("There should be three or four charsets");
9891 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9892 if (CHARSET_DIMENSION (charset
) != 1)
9893 error ("Dimension of charset %s is not one",
9894 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9895 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9896 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9898 charset_list
= XCDR (charset_list
);
9899 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9900 if (CHARSET_DIMENSION (charset
) != 1)
9901 error ("Dimension of charset %s is not one",
9902 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9904 charset_list
= XCDR (charset_list
);
9905 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9906 if (CHARSET_DIMENSION (charset
) != 2)
9907 error ("Dimension of charset %s is not two",
9908 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9910 charset_list
= XCDR (charset_list
);
9911 if (! NILP (charset_list
))
9913 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9914 if (CHARSET_DIMENSION (charset
) != 2)
9915 error ("Dimension of charset %s is not two",
9916 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9919 category
= coding_category_sjis
;
9920 Vsjis_coding_system
= name
;
9922 else if (EQ (coding_type
, Qbig5
))
9924 struct charset
*charset
;
9926 if (XINT (Flength (charset_list
)) != 2)
9927 error ("There should be just two charsets");
9929 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9930 if (CHARSET_DIMENSION (charset
) != 1)
9931 error ("Dimension of charset %s is not one",
9932 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9933 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9934 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9936 charset_list
= XCDR (charset_list
);
9937 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9938 if (CHARSET_DIMENSION (charset
) != 2)
9939 error ("Dimension of charset %s is not two",
9940 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9942 category
= coding_category_big5
;
9943 Vbig5_coding_system
= name
;
9945 else if (EQ (coding_type
, Qraw_text
))
9947 category
= coding_category_raw_text
;
9948 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9950 else if (EQ (coding_type
, Qutf_8
))
9954 if (nargs
< coding_arg_utf8_max
)
9957 bom
= args
[coding_arg_utf8_bom
];
9958 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9962 CHECK_CODING_SYSTEM (val
);
9964 CHECK_CODING_SYSTEM (val
);
9966 ASET (attrs
, coding_attr_utf_bom
, bom
);
9968 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9970 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9971 : NILP (bom
) ? coding_category_utf_8_nosig
9972 : coding_category_utf_8_sig
);
9974 else if (EQ (coding_type
, Qundecided
))
9975 category
= coding_category_undecided
;
9977 error ("Invalid coding system type: %s",
9978 SDATA (SYMBOL_NAME (coding_type
)));
9980 ASET (attrs
, coding_attr_category
, make_number (category
));
9981 ASET (attrs
, coding_attr_plist
,
9983 Fcons (AREF (Vcoding_category_table
, category
),
9984 CODING_ATTR_PLIST (attrs
))));
9985 ASET (attrs
, coding_attr_plist
,
9986 Fcons (QCascii_compatible_p
,
9987 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9988 CODING_ATTR_PLIST (attrs
))));
9990 eol_type
= args
[coding_arg_eol_type
];
9991 if (! NILP (eol_type
)
9992 && ! EQ (eol_type
, Qunix
)
9993 && ! EQ (eol_type
, Qdos
)
9994 && ! EQ (eol_type
, Qmac
))
9995 error ("Invalid eol-type");
9997 aliases
= Fcons (name
, Qnil
);
9999 if (NILP (eol_type
))
10001 eol_type
= make_subsidiaries (name
);
10002 for (i
= 0; i
< 3; i
++)
10004 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10006 this_name
= AREF (eol_type
, i
);
10007 this_aliases
= Fcons (this_name
, Qnil
);
10008 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10009 this_spec
= make_uninit_vector (3);
10010 ASET (this_spec
, 0, attrs
);
10011 ASET (this_spec
, 1, this_aliases
);
10012 ASET (this_spec
, 2, this_eol_type
);
10013 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10014 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10015 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10017 Vcoding_system_alist
10018 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10019 Vcoding_system_alist
);
10023 spec_vec
= make_uninit_vector (3);
10024 ASET (spec_vec
, 0, attrs
);
10025 ASET (spec_vec
, 1, aliases
);
10026 ASET (spec_vec
, 2, eol_type
);
10028 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10029 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10030 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10032 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10033 Vcoding_system_alist
);
10036 int id
= coding_categories
[category
].id
;
10038 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10039 setup_coding_system (name
, &coding_categories
[category
]);
10045 return Fsignal (Qwrong_number_of_arguments
,
10046 Fcons (intern ("define-coding-system-internal"),
10047 make_number (nargs
)));
10051 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10053 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10054 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10056 Lisp_Object spec
, attrs
;
10058 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10059 attrs
= AREF (spec
, 0);
10060 if (EQ (prop
, QCmnemonic
))
10062 if (! STRINGP (val
))
10063 CHECK_CHARACTER (val
);
10064 ASET (attrs
, coding_attr_mnemonic
, val
);
10066 else if (EQ (prop
, QCdefault_char
))
10069 val
= make_number (' ');
10071 CHECK_CHARACTER (val
);
10072 ASET (attrs
, coding_attr_default_char
, val
);
10074 else if (EQ (prop
, QCdecode_translation_table
))
10076 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10077 CHECK_SYMBOL (val
);
10078 ASET (attrs
, coding_attr_decode_tbl
, val
);
10080 else if (EQ (prop
, QCencode_translation_table
))
10082 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10083 CHECK_SYMBOL (val
);
10084 ASET (attrs
, coding_attr_encode_tbl
, val
);
10086 else if (EQ (prop
, QCpost_read_conversion
))
10088 CHECK_SYMBOL (val
);
10089 ASET (attrs
, coding_attr_post_read
, val
);
10091 else if (EQ (prop
, QCpre_write_conversion
))
10093 CHECK_SYMBOL (val
);
10094 ASET (attrs
, coding_attr_pre_write
, val
);
10096 else if (EQ (prop
, QCascii_compatible_p
))
10098 ASET (attrs
, coding_attr_ascii_compat
, val
);
10101 ASET (attrs
, coding_attr_plist
,
10102 Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
));
10107 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10108 Sdefine_coding_system_alias
, 2, 2, 0,
10109 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10110 (Lisp_Object alias
, Lisp_Object coding_system
)
10112 Lisp_Object spec
, aliases
, eol_type
, val
;
10114 CHECK_SYMBOL (alias
);
10115 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10116 aliases
= AREF (spec
, 1);
10117 /* ALIASES should be a list of length more than zero, and the first
10118 element is a base coding system. Append ALIAS at the tail of the
10120 while (!NILP (XCDR (aliases
)))
10121 aliases
= XCDR (aliases
);
10122 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10124 eol_type
= AREF (spec
, 2);
10125 if (VECTORP (eol_type
))
10127 Lisp_Object subsidiaries
;
10130 subsidiaries
= make_subsidiaries (alias
);
10131 for (i
= 0; i
< 3; i
++)
10132 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10133 AREF (eol_type
, i
));
10136 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10137 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10138 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10140 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10141 Vcoding_system_alist
);
10146 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10148 doc
: /* Return the base of CODING-SYSTEM.
10149 Any alias or subsidiary coding system is not a base coding system. */)
10150 (Lisp_Object coding_system
)
10152 Lisp_Object spec
, attrs
;
10154 if (NILP (coding_system
))
10155 return (Qno_conversion
);
10156 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10157 attrs
= AREF (spec
, 0);
10158 return CODING_ATTR_BASE_NAME (attrs
);
10161 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10163 doc
: "Return the property list of CODING-SYSTEM.")
10164 (Lisp_Object coding_system
)
10166 Lisp_Object spec
, attrs
;
10168 if (NILP (coding_system
))
10169 coding_system
= Qno_conversion
;
10170 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10171 attrs
= AREF (spec
, 0);
10172 return CODING_ATTR_PLIST (attrs
);
10176 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10178 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10179 (Lisp_Object coding_system
)
10183 if (NILP (coding_system
))
10184 coding_system
= Qno_conversion
;
10185 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10186 return AREF (spec
, 1);
10189 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10190 Scoding_system_eol_type
, 1, 1, 0,
10191 doc
: /* Return eol-type of CODING-SYSTEM.
10192 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10194 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10195 and CR respectively.
10197 A vector value indicates that a format of end-of-line should be
10198 detected automatically. Nth element of the vector is the subsidiary
10199 coding system whose eol-type is N. */)
10200 (Lisp_Object coding_system
)
10202 Lisp_Object spec
, eol_type
;
10205 if (NILP (coding_system
))
10206 coding_system
= Qno_conversion
;
10207 if (! CODING_SYSTEM_P (coding_system
))
10209 spec
= CODING_SYSTEM_SPEC (coding_system
);
10210 eol_type
= AREF (spec
, 2);
10211 if (VECTORP (eol_type
))
10212 return Fcopy_sequence (eol_type
);
10213 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10214 return make_number (n
);
10220 /*** 9. Post-amble ***/
10223 init_coding_once (void)
10227 for (i
= 0; i
< coding_category_max
; i
++)
10229 coding_categories
[i
].id
= -1;
10230 coding_priorities
[i
] = i
;
10233 /* ISO2022 specific initialize routine. */
10234 for (i
= 0; i
< 0x20; i
++)
10235 iso_code_class
[i
] = ISO_control_0
;
10236 for (i
= 0x21; i
< 0x7F; i
++)
10237 iso_code_class
[i
] = ISO_graphic_plane_0
;
10238 for (i
= 0x80; i
< 0xA0; i
++)
10239 iso_code_class
[i
] = ISO_control_1
;
10240 for (i
= 0xA1; i
< 0xFF; i
++)
10241 iso_code_class
[i
] = ISO_graphic_plane_1
;
10242 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10243 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10244 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10245 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10246 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10247 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10248 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10249 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10250 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10252 for (i
= 0; i
< 256; i
++)
10254 emacs_mule_bytes
[i
] = 1;
10256 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10257 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10258 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10259 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10265 syms_of_coding (void)
10267 staticpro (&Vcoding_system_hash_table
);
10269 Lisp_Object args
[2];
10272 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10275 staticpro (&Vsjis_coding_system
);
10276 Vsjis_coding_system
= Qnil
;
10278 staticpro (&Vbig5_coding_system
);
10279 Vbig5_coding_system
= Qnil
;
10281 staticpro (&Vcode_conversion_reused_workbuf
);
10282 Vcode_conversion_reused_workbuf
= Qnil
;
10284 staticpro (&Vcode_conversion_workbuf_name
);
10285 Vcode_conversion_workbuf_name
= build_pure_c_string (" *code-conversion-work*");
10287 reused_workbuf_in_use
= 0;
10289 DEFSYM (Qcharset
, "charset");
10290 DEFSYM (Qtarget_idx
, "target-idx");
10291 DEFSYM (Qcoding_system_history
, "coding-system-history");
10292 Fset (Qcoding_system_history
, Qnil
);
10294 /* Target FILENAME is the first argument. */
10295 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10296 /* Target FILENAME is the third argument. */
10297 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10299 DEFSYM (Qcall_process
, "call-process");
10300 /* Target PROGRAM is the first argument. */
10301 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10303 DEFSYM (Qcall_process_region
, "call-process-region");
10304 /* Target PROGRAM is the third argument. */
10305 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10307 DEFSYM (Qstart_process
, "start-process");
10308 /* Target PROGRAM is the third argument. */
10309 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10311 DEFSYM (Qopen_network_stream
, "open-network-stream");
10312 /* Target SERVICE is the fourth argument. */
10313 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10315 DEFSYM (Qcoding_system
, "coding-system");
10316 DEFSYM (Qcoding_aliases
, "coding-aliases");
10318 DEFSYM (Qeol_type
, "eol-type");
10319 DEFSYM (Qunix
, "unix");
10320 DEFSYM (Qdos
, "dos");
10321 DEFSYM (Qmac
, "mac");
10323 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10324 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10325 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10326 DEFSYM (Qdefault_char
, "default-char");
10327 DEFSYM (Qundecided
, "undecided");
10328 DEFSYM (Qno_conversion
, "no-conversion");
10329 DEFSYM (Qraw_text
, "raw-text");
10331 DEFSYM (Qiso_2022
, "iso-2022");
10333 DEFSYM (Qutf_8
, "utf-8");
10334 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10336 #if defined (WINDOWSNT) || defined (CYGWIN)
10337 /* No, not utf-16-le: that one has a BOM. */
10338 DEFSYM (Qutf_16le
, "utf-16le");
10341 DEFSYM (Qutf_16
, "utf-16");
10342 DEFSYM (Qbig
, "big");
10343 DEFSYM (Qlittle
, "little");
10345 DEFSYM (Qshift_jis
, "shift-jis");
10346 DEFSYM (Qbig5
, "big5");
10348 DEFSYM (Qcoding_system_p
, "coding-system-p");
10350 DEFSYM (Qcoding_system_error
, "coding-system-error");
10351 Fput (Qcoding_system_error
, Qerror_conditions
,
10352 listn (CONSTYPE_PURE
, 2, Qcoding_system_error
, Qerror
));
10353 Fput (Qcoding_system_error
, Qerror_message
,
10354 build_pure_c_string ("Invalid coding system"));
10356 /* Intern this now in case it isn't already done.
10357 Setting this variable twice is harmless.
10358 But don't staticpro it here--that is done in alloc.c. */
10359 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10361 DEFSYM (Qtranslation_table
, "translation-table");
10362 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10363 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10364 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10365 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10367 DEFSYM (Qvalid_codes
, "valid-codes");
10369 DEFSYM (Qemacs_mule
, "emacs-mule");
10371 DEFSYM (QCcategory
, ":category");
10372 DEFSYM (QCmnemonic
, ":mnemonic");
10373 DEFSYM (QCdefault_char
, ":default-char");
10374 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10375 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10376 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10377 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10378 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10380 Vcoding_category_table
10381 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10382 staticpro (&Vcoding_category_table
);
10383 /* Followings are target of code detection. */
10384 ASET (Vcoding_category_table
, coding_category_iso_7
,
10385 intern_c_string ("coding-category-iso-7"));
10386 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10387 intern_c_string ("coding-category-iso-7-tight"));
10388 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10389 intern_c_string ("coding-category-iso-8-1"));
10390 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10391 intern_c_string ("coding-category-iso-8-2"));
10392 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10393 intern_c_string ("coding-category-iso-7-else"));
10394 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10395 intern_c_string ("coding-category-iso-8-else"));
10396 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10397 intern_c_string ("coding-category-utf-8-auto"));
10398 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10399 intern_c_string ("coding-category-utf-8"));
10400 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10401 intern_c_string ("coding-category-utf-8-sig"));
10402 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10403 intern_c_string ("coding-category-utf-16-be"));
10404 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10405 intern_c_string ("coding-category-utf-16-auto"));
10406 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10407 intern_c_string ("coding-category-utf-16-le"));
10408 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10409 intern_c_string ("coding-category-utf-16-be-nosig"));
10410 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10411 intern_c_string ("coding-category-utf-16-le-nosig"));
10412 ASET (Vcoding_category_table
, coding_category_charset
,
10413 intern_c_string ("coding-category-charset"));
10414 ASET (Vcoding_category_table
, coding_category_sjis
,
10415 intern_c_string ("coding-category-sjis"));
10416 ASET (Vcoding_category_table
, coding_category_big5
,
10417 intern_c_string ("coding-category-big5"));
10418 ASET (Vcoding_category_table
, coding_category_ccl
,
10419 intern_c_string ("coding-category-ccl"));
10420 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10421 intern_c_string ("coding-category-emacs-mule"));
10422 /* Followings are NOT target of code detection. */
10423 ASET (Vcoding_category_table
, coding_category_raw_text
,
10424 intern_c_string ("coding-category-raw-text"));
10425 ASET (Vcoding_category_table
, coding_category_undecided
,
10426 intern_c_string ("coding-category-undecided"));
10428 DEFSYM (Qinsufficient_source
, "insufficient-source");
10429 DEFSYM (Qinvalid_source
, "invalid-source");
10430 DEFSYM (Qinterrupted
, "interrupted");
10431 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10433 defsubr (&Scoding_system_p
);
10434 defsubr (&Sread_coding_system
);
10435 defsubr (&Sread_non_nil_coding_system
);
10436 defsubr (&Scheck_coding_system
);
10437 defsubr (&Sdetect_coding_region
);
10438 defsubr (&Sdetect_coding_string
);
10439 defsubr (&Sfind_coding_systems_region_internal
);
10440 defsubr (&Sunencodable_char_position
);
10441 defsubr (&Scheck_coding_systems_region
);
10442 defsubr (&Sdecode_coding_region
);
10443 defsubr (&Sencode_coding_region
);
10444 defsubr (&Sdecode_coding_string
);
10445 defsubr (&Sencode_coding_string
);
10446 defsubr (&Sdecode_sjis_char
);
10447 defsubr (&Sencode_sjis_char
);
10448 defsubr (&Sdecode_big5_char
);
10449 defsubr (&Sencode_big5_char
);
10450 defsubr (&Sset_terminal_coding_system_internal
);
10451 defsubr (&Sset_safe_terminal_coding_system_internal
);
10452 defsubr (&Sterminal_coding_system
);
10453 defsubr (&Sset_keyboard_coding_system_internal
);
10454 defsubr (&Skeyboard_coding_system
);
10455 defsubr (&Sfind_operation_coding_system
);
10456 defsubr (&Sset_coding_system_priority
);
10457 defsubr (&Sdefine_coding_system_internal
);
10458 defsubr (&Sdefine_coding_system_alias
);
10459 defsubr (&Scoding_system_put
);
10460 defsubr (&Scoding_system_base
);
10461 defsubr (&Scoding_system_plist
);
10462 defsubr (&Scoding_system_aliases
);
10463 defsubr (&Scoding_system_eol_type
);
10464 defsubr (&Scoding_system_priority_list
);
10466 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10467 doc
: /* List of coding systems.
10469 Do not alter the value of this variable manually. This variable should be
10470 updated by the functions `define-coding-system' and
10471 `define-coding-system-alias'. */);
10472 Vcoding_system_list
= Qnil
;
10474 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10475 doc
: /* Alist of coding system names.
10476 Each element is one element list of coding system name.
10477 This variable is given to `completing-read' as COLLECTION argument.
10479 Do not alter the value of this variable manually. This variable should be
10480 updated by the functions `make-coding-system' and
10481 `define-coding-system-alias'. */);
10482 Vcoding_system_alist
= Qnil
;
10484 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10485 doc
: /* List of coding-categories (symbols) ordered by priority.
10487 On detecting a coding system, Emacs tries code detection algorithms
10488 associated with each coding-category one by one in this order. When
10489 one algorithm agrees with a byte sequence of source text, the coding
10490 system bound to the corresponding coding-category is selected.
10492 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10496 Vcoding_category_list
= Qnil
;
10497 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10498 Vcoding_category_list
10499 = Fcons (AREF (Vcoding_category_table
, i
),
10500 Vcoding_category_list
);
10503 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10504 doc
: /* Specify the coding system for read operations.
10505 It is useful to bind this variable with `let', but do not set it globally.
10506 If the value is a coding system, it is used for decoding on read operation.
10507 If not, an appropriate element is used from one of the coding system alists.
10508 There are three such tables: `file-coding-system-alist',
10509 `process-coding-system-alist', and `network-coding-system-alist'. */);
10510 Vcoding_system_for_read
= Qnil
;
10512 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10513 doc
: /* Specify the coding system for write operations.
10514 Programs bind this variable with `let', but you should not set it globally.
10515 If the value is a coding system, it is used for encoding of output,
10516 when writing it to a file and when sending it to a file or subprocess.
10518 If this does not specify a coding system, an appropriate element
10519 is used from one of the coding system alists.
10520 There are three such tables: `file-coding-system-alist',
10521 `process-coding-system-alist', and `network-coding-system-alist'.
10522 For output to files, if the above procedure does not specify a coding system,
10523 the value of `buffer-file-coding-system' is used. */);
10524 Vcoding_system_for_write
= Qnil
;
10526 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10528 Coding system used in the latest file or process I/O. */);
10529 Vlast_coding_system_used
= Qnil
;
10531 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10533 Error status of the last code conversion.
10535 When an error was detected in the last code conversion, this variable
10536 is set to one of the following symbols.
10537 `insufficient-source'
10541 `insufficient-memory'
10542 When no error was detected, the value doesn't change. So, to check
10543 the error status of a code conversion by this variable, you must
10544 explicitly set this variable to nil before performing code
10546 Vlast_code_conversion_error
= Qnil
;
10548 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10550 *Non-nil means always inhibit code conversion of end-of-line format.
10551 See info node `Coding Systems' and info node `Text and Binary' concerning
10552 such conversion. */);
10553 inhibit_eol_conversion
= 0;
10555 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10557 Non-nil means process buffer inherits coding system of process output.
10558 Bind it to t if the process output is to be treated as if it were a file
10559 read from some filesystem. */);
10560 inherit_process_coding_system
= 0;
10562 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10564 Alist to decide a coding system to use for a file I/O operation.
10565 The format is ((PATTERN . VAL) ...),
10566 where PATTERN is a regular expression matching a file name,
10567 VAL is a coding system, a cons of coding systems, or a function symbol.
10568 If VAL is a coding system, it is used for both decoding and encoding
10570 If VAL is a cons of coding systems, the car part is used for decoding,
10571 and the cdr part is used for encoding.
10572 If VAL is a function symbol, the function must return a coding system
10573 or a cons of coding systems which are used as above. The function is
10574 called with an argument that is a list of the arguments with which
10575 `find-operation-coding-system' was called. If the function can't decide
10576 a coding system, it can return `undecided' so that the normal
10577 code-detection is performed.
10579 See also the function `find-operation-coding-system'
10580 and the variable `auto-coding-alist'. */);
10581 Vfile_coding_system_alist
= Qnil
;
10583 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10585 Alist to decide a coding system to use for a process I/O operation.
10586 The format is ((PATTERN . VAL) ...),
10587 where PATTERN is a regular expression matching a program name,
10588 VAL is a coding system, a cons of coding systems, or a function symbol.
10589 If VAL is a coding system, it is used for both decoding what received
10590 from the program and encoding what sent to the program.
10591 If VAL is a cons of coding systems, the car part is used for decoding,
10592 and the cdr part is used for encoding.
10593 If VAL is a function symbol, the function must return a coding system
10594 or a cons of coding systems which are used as above.
10596 See also the function `find-operation-coding-system'. */);
10597 Vprocess_coding_system_alist
= Qnil
;
10599 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10601 Alist to decide a coding system to use for a network I/O operation.
10602 The format is ((PATTERN . VAL) ...),
10603 where PATTERN is a regular expression matching a network service name
10604 or is a port number to connect to,
10605 VAL is a coding system, a cons of coding systems, or a function symbol.
10606 If VAL is a coding system, it is used for both decoding what received
10607 from the network stream and encoding what sent to the network stream.
10608 If VAL is a cons of coding systems, the car part is used for decoding,
10609 and the cdr part is used for encoding.
10610 If VAL is a function symbol, the function must return a coding system
10611 or a cons of coding systems which are used as above.
10613 See also the function `find-operation-coding-system'. */);
10614 Vnetwork_coding_system_alist
= Qnil
;
10616 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10617 doc
: /* Coding system to use with system messages.
10618 Also used for decoding keyboard input on X Window system. */);
10619 Vlocale_coding_system
= Qnil
;
10621 /* The eol mnemonics are reset in startup.el system-dependently. */
10622 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10624 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10625 eol_mnemonic_unix
= build_pure_c_string (":");
10627 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10629 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10630 eol_mnemonic_dos
= build_pure_c_string ("\\");
10632 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10634 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10635 eol_mnemonic_mac
= build_pure_c_string ("/");
10637 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10639 *String displayed in mode line when end-of-line format is not yet determined. */);
10640 eol_mnemonic_undecided
= build_pure_c_string (":");
10642 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10644 *Non-nil enables character translation while encoding and decoding. */);
10645 Venable_character_translation
= Qt
;
10647 DEFVAR_LISP ("standard-translation-table-for-decode",
10648 Vstandard_translation_table_for_decode
,
10649 doc
: /* Table for translating characters while decoding. */);
10650 Vstandard_translation_table_for_decode
= Qnil
;
10652 DEFVAR_LISP ("standard-translation-table-for-encode",
10653 Vstandard_translation_table_for_encode
,
10654 doc
: /* Table for translating characters while encoding. */);
10655 Vstandard_translation_table_for_encode
= Qnil
;
10657 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10658 doc
: /* Alist of charsets vs revision numbers.
10659 While encoding, if a charset (car part of an element) is found,
10660 designate it with the escape sequence identifying revision (cdr part
10661 of the element). */);
10662 Vcharset_revision_table
= Qnil
;
10664 DEFVAR_LISP ("default-process-coding-system",
10665 Vdefault_process_coding_system
,
10666 doc
: /* Cons of coding systems used for process I/O by default.
10667 The car part is used for decoding a process output,
10668 the cdr part is used for encoding a text to be sent to a process. */);
10669 Vdefault_process_coding_system
= Qnil
;
10671 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10673 Table of extra Latin codes in the range 128..159 (inclusive).
10674 This is a vector of length 256.
10675 If Nth element is non-nil, the existence of code N in a file
10676 \(or output of subprocess) doesn't prevent it to be detected as
10677 a coding system of ISO 2022 variant which has a flag
10678 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10679 or reading output of a subprocess.
10680 Only 128th through 159th elements have a meaning. */);
10681 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10683 DEFVAR_LISP ("select-safe-coding-system-function",
10684 Vselect_safe_coding_system_function
,
10686 Function to call to select safe coding system for encoding a text.
10688 If set, this function is called to force a user to select a proper
10689 coding system which can encode the text in the case that a default
10690 coding system used in each operation can't encode the text. The
10691 function should take care that the buffer is not modified while
10692 the coding system is being selected.
10694 The default value is `select-safe-coding-system' (which see). */);
10695 Vselect_safe_coding_system_function
= Qnil
;
10697 DEFVAR_BOOL ("coding-system-require-warning",
10698 coding_system_require_warning
,
10699 doc
: /* Internal use only.
10700 If non-nil, on writing a file, `select-safe-coding-system-function' is
10701 called even if `coding-system-for-write' is non-nil. The command
10702 `universal-coding-system-argument' binds this variable to t temporarily. */);
10703 coding_system_require_warning
= 0;
10706 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10707 inhibit_iso_escape_detection
,
10709 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10711 When Emacs reads text, it tries to detect how the text is encoded.
10712 This code detection is sensitive to escape sequences. If Emacs sees
10713 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10714 of the ISO2022 encodings, and decodes text by the corresponding coding
10715 system (e.g. `iso-2022-7bit').
10717 However, there may be a case that you want to read escape sequences in
10718 a file as is. In such a case, you can set this variable to non-nil.
10719 Then the code detection will ignore any escape sequences, and no text is
10720 detected as encoded in some ISO-2022 encoding. The result is that all
10721 escape sequences become visible in a buffer.
10723 The default value is nil, and it is strongly recommended not to change
10724 it. That is because many Emacs Lisp source files that contain
10725 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10726 in Emacs's distribution, and they won't be decoded correctly on
10727 reading if you suppress escape sequence detection.
10729 The other way to read escape sequences in a file without decoding is
10730 to explicitly specify some coding system that doesn't use ISO-2022
10731 escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */);
10732 inhibit_iso_escape_detection
= 0;
10734 DEFVAR_BOOL ("inhibit-null-byte-detection",
10735 inhibit_null_byte_detection
,
10736 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10737 By default, Emacs treats it as binary data, and does not attempt to
10738 decode it. The effect is as if you specified `no-conversion' for
10741 Set this to non-nil when a regular text happens to include null bytes.
10742 Examples are Index nodes of Info files and null-byte delimited output
10743 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10744 decode text as usual. */);
10745 inhibit_null_byte_detection
= 0;
10747 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10748 doc
: /* Char table for translating self-inserting characters.
10749 This is applied to the result of input methods, not their input.
10750 See also `keyboard-translate-table'.
10752 Use of this variable for character code unification was rendered
10753 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10754 internal character representation. */);
10755 Vtranslation_table_for_input
= Qnil
;
10758 Lisp_Object args
[coding_arg_max
];
10759 Lisp_Object plist
[16];
10762 for (i
= 0; i
< coding_arg_max
; i
++)
10765 plist
[0] = intern_c_string (":name");
10766 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10767 plist
[2] = intern_c_string (":mnemonic");
10768 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10769 plist
[4] = intern_c_string (":coding-type");
10770 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10771 plist
[6] = intern_c_string (":ascii-compatible-p");
10772 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10773 plist
[8] = intern_c_string (":default-char");
10774 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10775 plist
[10] = intern_c_string (":for-unibyte");
10776 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10777 plist
[12] = intern_c_string (":docstring");
10778 plist
[13] = build_pure_c_string ("Do no conversion.\n\
10780 When you visit a file with this coding, the file is read into a\n\
10781 unibyte buffer as is, thus each byte of a file is treated as a\n\
10783 plist
[14] = intern_c_string (":eol-type");
10784 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10785 args
[coding_arg_plist
] = Flist (16, plist
);
10786 Fdefine_coding_system_internal (coding_arg_max
, args
);
10788 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10789 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10790 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10791 /* This is already set.
10792 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10793 plist
[8] = intern_c_string (":charset-list");
10794 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10795 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10796 plist
[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10797 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10798 args
[coding_arg_plist
] = Flist (16, plist
);
10799 Fdefine_coding_system_internal (coding_arg_max
, args
);
10802 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10807 for (i
= 0; i
< coding_category_max
; i
++)
10808 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10810 #if defined (DOS_NT)
10811 system_eol_type
= Qdos
;
10813 system_eol_type
= Qunix
;
10815 staticpro (&system_eol_type
);
10819 emacs_strerror (int error_number
)
10823 synchronize_system_messages_locale ();
10824 str
= strerror (error_number
);
10826 if (! NILP (Vlocale_coding_system
))
10828 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10829 Vlocale_coding_system
,
10831 str
= SSDATA (dec
);