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
, Qinconsistent_eol
, Qinvalid_source
;
326 static Lisp_Object Qinterrupted
, Qinsufficient_memory
;
328 /* If a symbol has this property, evaluate the value to define the
329 symbol as a coding system. */
330 static Lisp_Object Qcoding_system_define_form
;
332 /* Format of end-of-line decided by system. This is Qunix on
333 Unix and Mac, Qdos on DOS/Windows.
334 This has an effect only for external encoding (i.e. for output to
335 file and process), not for in-buffer or Lisp string encoding. */
336 static Lisp_Object system_eol_type
;
340 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
342 /* Coding system emacs-mule and raw-text are for converting only
343 end-of-line format. */
344 Lisp_Object Qemacs_mule
, Qraw_text
;
345 Lisp_Object Qutf_8_emacs
;
347 #if defined (WINDOWSNT) || defined (CYGWIN)
348 static Lisp_Object Qutf_16le
;
351 /* Coding-systems are handed between Emacs Lisp programs and C internal
352 routines by the following three variables. */
353 /* Coding system to be used to encode text for terminal display when
354 terminal coding system is nil. */
355 struct coding_system safe_terminal_coding
;
359 Lisp_Object Qtranslation_table
;
360 Lisp_Object Qtranslation_table_id
;
361 static Lisp_Object Qtranslation_table_for_decode
;
362 static Lisp_Object Qtranslation_table_for_encode
;
364 /* Two special coding systems. */
365 static Lisp_Object Vsjis_coding_system
;
366 static Lisp_Object Vbig5_coding_system
;
368 /* ISO2022 section */
370 #define CODING_ISO_INITIAL(coding, reg) \
371 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
372 coding_attr_iso_initial), \
376 #define CODING_ISO_REQUEST(coding, charset_id) \
377 (((charset_id) <= (coding)->max_charset_id \
378 ? ((coding)->safe_charsets[charset_id] != 255 \
379 ? (coding)->safe_charsets[charset_id] \
384 #define CODING_ISO_FLAGS(coding) \
385 ((coding)->spec.iso_2022.flags)
386 #define CODING_ISO_DESIGNATION(coding, reg) \
387 ((coding)->spec.iso_2022.current_designation[reg])
388 #define CODING_ISO_INVOCATION(coding, plane) \
389 ((coding)->spec.iso_2022.current_invocation[plane])
390 #define CODING_ISO_SINGLE_SHIFTING(coding) \
391 ((coding)->spec.iso_2022.single_shifting)
392 #define CODING_ISO_BOL(coding) \
393 ((coding)->spec.iso_2022.bol)
394 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
395 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
396 #define CODING_ISO_CMP_STATUS(coding) \
397 (&(coding)->spec.iso_2022.cmp_status)
398 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
399 ((coding)->spec.iso_2022.ctext_extended_segment_len)
400 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
401 ((coding)->spec.iso_2022.embedded_utf_8)
403 /* Control characters of ISO2022. */
404 /* code */ /* function */
405 #define ISO_CODE_SO 0x0E /* shift-out */
406 #define ISO_CODE_SI 0x0F /* shift-in */
407 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
408 #define ISO_CODE_ESC 0x1B /* escape */
409 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
410 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
411 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
413 /* All code (1-byte) of ISO2022 is classified into one of the
415 enum iso_code_class_type
417 ISO_control_0
, /* Control codes in the range
418 0x00..0x1F and 0x7F, except for the
419 following 5 codes. */
420 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
421 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
422 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
423 ISO_escape
, /* ISO_CODE_ESC (0x1B) */
424 ISO_control_1
, /* Control codes in the range
425 0x80..0x9F, except for the
426 following 3 codes. */
427 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
428 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
429 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
430 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
431 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
432 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
433 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
436 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
437 `iso-flags' attribute of an iso2022 coding system. */
439 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
440 instead of the correct short-form sequence (e.g. ESC $ A). */
441 #define CODING_ISO_FLAG_LONG_FORM 0x0001
443 /* If set, reset graphic planes and registers at end-of-line to the
445 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
447 /* If set, reset graphic planes and registers before any control
448 characters to the initial state. */
449 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
451 /* If set, encode by 7-bit environment. */
452 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
454 /* If set, use locking-shift function. */
455 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
457 /* If set, use single-shift function. Overwrite
458 CODING_ISO_FLAG_LOCKING_SHIFT. */
459 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
461 /* If set, use designation escape sequence. */
462 #define CODING_ISO_FLAG_DESIGNATION 0x0040
464 /* If set, produce revision number sequence. */
465 #define CODING_ISO_FLAG_REVISION 0x0080
467 /* If set, produce ISO6429's direction specifying sequence. */
468 #define CODING_ISO_FLAG_DIRECTION 0x0100
470 /* If set, assume designation states are reset at beginning of line on
472 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
474 /* If set, designation sequence should be placed at beginning of line
476 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
478 /* If set, do not encode unsafe characters on output. */
479 #define CODING_ISO_FLAG_SAFE 0x0800
481 /* If set, extra latin codes (128..159) are accepted as a valid code
483 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
485 #define CODING_ISO_FLAG_COMPOSITION 0x2000
487 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
489 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
491 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
493 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
495 /* A character to be produced on output if encoding of the original
496 character is prohibited by CODING_ISO_FLAG_SAFE. */
497 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
500 #define CODING_UTF_8_BOM(coding) \
501 ((coding)->spec.utf_8_bom)
504 #define CODING_UTF_16_BOM(coding) \
505 ((coding)->spec.utf_16.bom)
507 #define CODING_UTF_16_ENDIAN(coding) \
508 ((coding)->spec.utf_16.endian)
510 #define CODING_UTF_16_SURROGATE(coding) \
511 ((coding)->spec.utf_16.surrogate)
515 #define CODING_CCL_DECODER(coding) \
516 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
517 #define CODING_CCL_ENCODER(coding) \
518 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
519 #define CODING_CCL_VALIDS(coding) \
520 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
522 /* Index for each coding category in `coding_categories' */
526 coding_category_iso_7
,
527 coding_category_iso_7_tight
,
528 coding_category_iso_8_1
,
529 coding_category_iso_8_2
,
530 coding_category_iso_7_else
,
531 coding_category_iso_8_else
,
532 coding_category_utf_8_auto
,
533 coding_category_utf_8_nosig
,
534 coding_category_utf_8_sig
,
535 coding_category_utf_16_auto
,
536 coding_category_utf_16_be
,
537 coding_category_utf_16_le
,
538 coding_category_utf_16_be_nosig
,
539 coding_category_utf_16_le_nosig
,
540 coding_category_charset
,
541 coding_category_sjis
,
542 coding_category_big5
,
544 coding_category_emacs_mule
,
545 /* All above are targets of code detection. */
546 coding_category_raw_text
,
547 coding_category_undecided
,
551 /* Definitions of flag bits used in detect_coding_XXXX. */
552 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
553 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
554 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
555 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
556 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
557 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
558 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
559 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
560 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
561 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
562 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
563 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
564 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
565 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
566 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
567 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
568 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
569 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
570 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
571 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
573 /* This value is returned if detect_coding_mask () find nothing other
574 than ASCII characters. */
575 #define CATEGORY_MASK_ANY \
576 (CATEGORY_MASK_ISO_7 \
577 | CATEGORY_MASK_ISO_7_TIGHT \
578 | CATEGORY_MASK_ISO_8_1 \
579 | CATEGORY_MASK_ISO_8_2 \
580 | CATEGORY_MASK_ISO_7_ELSE \
581 | CATEGORY_MASK_ISO_8_ELSE \
582 | CATEGORY_MASK_UTF_8_AUTO \
583 | CATEGORY_MASK_UTF_8_NOSIG \
584 | CATEGORY_MASK_UTF_8_SIG \
585 | CATEGORY_MASK_UTF_16_AUTO \
586 | CATEGORY_MASK_UTF_16_BE \
587 | CATEGORY_MASK_UTF_16_LE \
588 | CATEGORY_MASK_UTF_16_BE_NOSIG \
589 | CATEGORY_MASK_UTF_16_LE_NOSIG \
590 | CATEGORY_MASK_CHARSET \
591 | CATEGORY_MASK_SJIS \
592 | CATEGORY_MASK_BIG5 \
593 | CATEGORY_MASK_CCL \
594 | CATEGORY_MASK_EMACS_MULE)
597 #define CATEGORY_MASK_ISO_7BIT \
598 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
600 #define CATEGORY_MASK_ISO_8BIT \
601 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
603 #define CATEGORY_MASK_ISO_ELSE \
604 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
606 #define CATEGORY_MASK_ISO_ESCAPE \
607 (CATEGORY_MASK_ISO_7 \
608 | CATEGORY_MASK_ISO_7_TIGHT \
609 | CATEGORY_MASK_ISO_7_ELSE \
610 | CATEGORY_MASK_ISO_8_ELSE)
612 #define CATEGORY_MASK_ISO \
613 ( CATEGORY_MASK_ISO_7BIT \
614 | CATEGORY_MASK_ISO_8BIT \
615 | CATEGORY_MASK_ISO_ELSE)
617 #define CATEGORY_MASK_UTF_16 \
618 (CATEGORY_MASK_UTF_16_AUTO \
619 | CATEGORY_MASK_UTF_16_BE \
620 | CATEGORY_MASK_UTF_16_LE \
621 | CATEGORY_MASK_UTF_16_BE_NOSIG \
622 | CATEGORY_MASK_UTF_16_LE_NOSIG)
624 #define CATEGORY_MASK_UTF_8 \
625 (CATEGORY_MASK_UTF_8_AUTO \
626 | CATEGORY_MASK_UTF_8_NOSIG \
627 | CATEGORY_MASK_UTF_8_SIG)
629 /* Table of coding categories (Lisp symbols). This variable is for
630 internal use only. */
631 static Lisp_Object Vcoding_category_table
;
633 /* Table of coding-categories ordered by priority. */
634 static enum coding_category coding_priorities
[coding_category_max
];
636 /* Nth element is a coding context for the coding system bound to the
637 Nth coding category. */
638 static struct coding_system coding_categories
[coding_category_max
];
640 /*** Commonly used macros and functions ***/
643 #define min(a, b) ((a) < (b) ? (a) : (b))
646 #define max(a, b) ((a) > (b) ? (a) : (b))
649 #define CODING_GET_INFO(coding, attrs, charset_list) \
651 (attrs) = CODING_ID_ATTRS ((coding)->id); \
652 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
656 /* Safely get one byte from the source text pointed by SRC which ends
657 at SRC_END, and set C to that byte. If there are not enough bytes
658 in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
659 and a multibyte character is found at SRC, set C to the
660 negative value of the character code. The caller should declare
661 and set these variables appropriately in advance:
662 src, src_end, multibytep */
664 #define ONE_MORE_BYTE(c) \
666 if (src == src_end) \
668 if (src_base < src) \
669 record_conversion_result \
670 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
671 goto no_more_source; \
674 if (multibytep && (c & 0x80)) \
676 if ((c & 0xFE) == 0xC0) \
677 c = ((c & 1) << 6) | *src++; \
681 c = - string_char (src, &src, NULL); \
682 record_conversion_result \
683 (coding, CODING_RESULT_INVALID_SRC); \
689 /* Safely get two bytes from the source text pointed by SRC which ends
690 at SRC_END, and set C1 and C2 to those bytes while skipping the
691 heading multibyte characters. If there are not enough bytes in the
692 source, it jumps to 'no_more_source'. If MULTIBYTEP and
693 a multibyte character is found for C2, set C2 to the negative value
694 of the character code. The caller should declare and set these
695 variables appropriately in advance:
696 src, src_end, multibytep
697 It is intended that this macro is used in detect_coding_utf_16. */
699 #define TWO_MORE_BYTES(c1, c2) \
702 if (src == src_end) \
703 goto no_more_source; \
705 if (multibytep && (c1 & 0x80)) \
707 if ((c1 & 0xFE) == 0xC0) \
708 c1 = ((c1 & 1) << 6) | *src++; \
711 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
716 if (src == src_end) \
717 goto no_more_source; \
719 if (multibytep && (c2 & 0x80)) \
721 if ((c2 & 0xFE) == 0xC0) \
722 c2 = ((c2 & 1) << 6) | *src++; \
729 /* Store a byte C in the place pointed by DST and increment DST to the
730 next free point, and increment PRODUCED_CHARS. The caller should
731 assure that C is 0..127, and declare and set the variable `dst'
732 appropriately in advance.
736 #define EMIT_ONE_ASCII_BYTE(c) \
743 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
745 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
747 produced_chars += 2; \
748 *dst++ = (c1), *dst++ = (c2); \
752 /* Store a byte C in the place pointed by DST and increment DST to the
753 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
754 store in an appropriate multibyte form. The caller should
755 declare and set the variables `dst' and `multibytep' appropriately
758 #define EMIT_ONE_BYTE(c) \
765 ch = BYTE8_TO_CHAR (ch); \
766 CHAR_STRING_ADVANCE (ch, dst); \
773 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
775 #define EMIT_TWO_BYTES(c1, c2) \
777 produced_chars += 2; \
784 ch = BYTE8_TO_CHAR (ch); \
785 CHAR_STRING_ADVANCE (ch, dst); \
788 ch = BYTE8_TO_CHAR (ch); \
789 CHAR_STRING_ADVANCE (ch, dst); \
799 #define EMIT_THREE_BYTES(c1, c2, c3) \
801 EMIT_ONE_BYTE (c1); \
802 EMIT_TWO_BYTES (c2, c3); \
806 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
808 EMIT_TWO_BYTES (c1, c2); \
809 EMIT_TWO_BYTES (c3, c4); \
814 record_conversion_result (struct coding_system
*coding
,
815 enum coding_result_code result
)
817 coding
->result
= result
;
820 case CODING_RESULT_INSUFFICIENT_SRC
:
821 Vlast_code_conversion_error
= Qinsufficient_source
;
823 case CODING_RESULT_INCONSISTENT_EOL
:
824 Vlast_code_conversion_error
= Qinconsistent_eol
;
826 case CODING_RESULT_INVALID_SRC
:
827 Vlast_code_conversion_error
= Qinvalid_source
;
829 case CODING_RESULT_INTERRUPT
:
830 Vlast_code_conversion_error
= Qinterrupted
;
832 case CODING_RESULT_INSUFFICIENT_MEM
:
833 Vlast_code_conversion_error
= Qinsufficient_memory
;
835 case CODING_RESULT_INSUFFICIENT_DST
:
836 /* Don't record this error in Vlast_code_conversion_error
837 because it happens just temporarily and is resolved when the
838 whole conversion is finished. */
840 case CODING_RESULT_SUCCESS
:
843 Vlast_code_conversion_error
= intern ("Unknown error");
847 /* These wrapper macros are used to preserve validity of pointers into
848 buffer text across calls to decode_char, encode_char, etc, which
849 could cause relocation of buffers if it loads a charset map,
850 because loading a charset map allocates large structures. */
852 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
856 charset_map_loaded = 0; \
857 c = DECODE_CHAR (charset, code); \
858 if (charset_map_loaded \
859 && (offset = coding_change_source (coding))) \
862 src_base += offset; \
867 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
871 charset_map_loaded = 0; \
872 code = ENCODE_CHAR (charset, c); \
873 if (charset_map_loaded \
874 && (offset = coding_change_destination (coding))) \
881 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
885 charset_map_loaded = 0; \
886 charset = char_charset (c, charset_list, code_return); \
887 if (charset_map_loaded \
888 && (offset = coding_change_destination (coding))) \
895 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
899 charset_map_loaded = 0; \
900 result = CHAR_CHARSET_P (c, charset); \
901 if (charset_map_loaded \
902 && (offset = coding_change_destination (coding))) \
910 /* If there are at least BYTES length of room at dst, allocate memory
911 for coding->destination and update dst and dst_end. We don't have
912 to take care of coding->source which will be relocated. It is
913 handled by calling coding_set_source in encode_coding. */
915 #define ASSURE_DESTINATION(bytes) \
917 if (dst + (bytes) >= dst_end) \
919 ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
921 dst = alloc_destination (coding, more_bytes, dst); \
922 dst_end = coding->destination + coding->dst_bytes; \
927 /* Store multibyte form of the character C in P, and advance P to the
928 end of the multibyte form. This used to be like CHAR_STRING_ADVANCE
929 without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call
930 MAYBE_UNIFY_CHAR in CHAR_STRING_ADVANCE. */
932 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) CHAR_STRING_ADVANCE(c, p)
934 /* Return the character code of character whose multibyte form is at
935 P, and advance P to the end of the multibyte form. This used to be
936 like STRING_CHAR_ADVANCE without ever calling MAYBE_UNIFY_CHAR, but
937 nowadays STRING_CHAR_ADVANCE doesn't call MAYBE_UNIFY_CHAR. */
939 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) STRING_CHAR_ADVANCE(p)
941 /* Set coding->source from coding->src_object. */
944 coding_set_source (struct coding_system
*coding
)
946 if (BUFFERP (coding
->src_object
))
948 struct buffer
*buf
= XBUFFER (coding
->src_object
);
950 if (coding
->src_pos
< 0)
951 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
953 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
955 else if (STRINGP (coding
->src_object
))
957 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
961 /* Otherwise, the source is C string and is never relocated
962 automatically. Thus we don't have to update anything. */
967 /* Set coding->source from coding->src_object, and return how many
968 bytes coding->source was changed. */
971 coding_change_source (struct coding_system
*coding
)
973 const unsigned char *orig
= coding
->source
;
974 coding_set_source (coding
);
975 return coding
->source
- orig
;
979 /* Set coding->destination from coding->dst_object. */
982 coding_set_destination (struct coding_system
*coding
)
984 if (BUFFERP (coding
->dst_object
))
986 if (BUFFERP (coding
->src_object
) && coding
->src_pos
< 0)
988 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
989 coding
->dst_bytes
= (GAP_END_ADDR
990 - (coding
->src_bytes
- coding
->consumed
)
991 - coding
->destination
);
995 /* We are sure that coding->dst_pos_byte is before the gap
997 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
998 + coding
->dst_pos_byte
- BEG_BYTE
);
999 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1000 - coding
->destination
);
1005 /* Otherwise, the destination is C string and is never relocated
1006 automatically. Thus we don't have to update anything. */
1011 /* Set coding->destination from coding->dst_object, and return how
1012 many bytes coding->destination was changed. */
1015 coding_change_destination (struct coding_system
*coding
)
1017 const unsigned char *orig
= coding
->destination
;
1018 coding_set_destination (coding
);
1019 return coding
->destination
- orig
;
1024 coding_alloc_by_realloc (struct coding_system
*coding
, ptrdiff_t bytes
)
1026 if (STRING_BYTES_BOUND
- coding
->dst_bytes
< bytes
)
1028 coding
->destination
= xrealloc (coding
->destination
,
1029 coding
->dst_bytes
+ bytes
);
1030 coding
->dst_bytes
+= bytes
;
1034 coding_alloc_by_making_gap (struct coding_system
*coding
,
1035 ptrdiff_t gap_head_used
, ptrdiff_t bytes
)
1037 if (EQ (coding
->src_object
, coding
->dst_object
))
1039 /* The gap may contain the produced data at the head and not-yet
1040 consumed data at the tail. To preserve those data, we at
1041 first make the gap size to zero, then increase the gap
1043 ptrdiff_t add
= GAP_SIZE
;
1045 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1046 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1048 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1049 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1052 make_gap_1 (XBUFFER (coding
->dst_object
), bytes
);
1056 static unsigned char *
1057 alloc_destination (struct coding_system
*coding
, ptrdiff_t nbytes
,
1060 ptrdiff_t offset
= dst
- coding
->destination
;
1062 if (BUFFERP (coding
->dst_object
))
1064 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1066 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1069 coding_alloc_by_realloc (coding
, nbytes
);
1070 coding_set_destination (coding
);
1071 dst
= coding
->destination
+ offset
;
1075 /** Macros for annotations. */
1077 /* An annotation data is stored in the array coding->charbuf in this
1079 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1080 LENGTH is the number of elements in the annotation.
1081 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1082 NCHARS is the number of characters in the text annotated.
1084 The format of the following elements depend on ANNOTATION_MASK.
1086 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1088 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1090 NBYTES is the number of bytes specified in the header part of
1091 old-style emacs-mule encoding, or 0 for the other kind of
1094 METHOD is one of enum composition_method.
1096 Optional COMPOSITION-COMPONENTS are characters and composition
1099 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1102 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1103 recover from an invalid annotation, and should be skipped by
1104 produce_annotation. */
1106 /* Maximum length of the header of annotation data. */
1107 #define MAX_ANNOTATION_LENGTH 5
1109 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1111 *(buf)++ = -(len); \
1112 *(buf)++ = (mask); \
1113 *(buf)++ = (nchars); \
1114 coding->annotated = 1; \
1117 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1119 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1125 #define ADD_CHARSET_DATA(buf, nchars, id) \
1127 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1132 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1139 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1140 Return true if a text is encoded in UTF-8. */
1142 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1143 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1144 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1145 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1146 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1147 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1149 #define UTF_8_BOM_1 0xEF
1150 #define UTF_8_BOM_2 0xBB
1151 #define UTF_8_BOM_3 0xBF
1154 detect_coding_utf_8 (struct coding_system
*coding
,
1155 struct coding_detection_info
*detect_info
)
1157 const unsigned char *src
= coding
->source
, *src_base
;
1158 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1159 bool multibytep
= coding
->src_multibyte
;
1160 ptrdiff_t consumed_chars
= 0;
1164 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1165 /* A coding system of this category is always ASCII compatible. */
1166 src
+= coding
->head_ascii
;
1170 int c
, c1
, c2
, c3
, c4
;
1174 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1177 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1179 if (UTF_8_2_OCTET_LEADING_P (c
))
1185 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1187 if (UTF_8_3_OCTET_LEADING_P (c
))
1190 if (src_base
== coding
->source
1191 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1196 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1198 if (UTF_8_4_OCTET_LEADING_P (c
))
1204 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1206 if (UTF_8_5_OCTET_LEADING_P (c
))
1213 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1217 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1219 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1224 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1225 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1229 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1231 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1238 decode_coding_utf_8 (struct coding_system
*coding
)
1240 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1241 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1242 const unsigned char *src_base
;
1243 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1244 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1245 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1246 bool multibytep
= coding
->src_multibyte
;
1247 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1249 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1250 int byte_after_cr
= -1;
1252 if (bom
!= utf_without_bom
)
1258 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1263 if (! UTF_8_EXTRA_OCTET_P (c2
))
1268 if (! UTF_8_EXTRA_OCTET_P (c3
))
1272 if ((c1
!= UTF_8_BOM_1
)
1273 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1276 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1281 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1285 int c
, c1
, c2
, c3
, c4
, c5
;
1288 consumed_chars_base
= consumed_chars
;
1290 if (charbuf
>= charbuf_end
)
1292 if (byte_after_cr
>= 0)
1297 if (byte_after_cr
>= 0)
1298 c1
= byte_after_cr
, byte_after_cr
= -1;
1305 else if (UTF_8_1_OCTET_P (c1
))
1307 if (eol_dos
&& c1
== '\r')
1308 ONE_MORE_BYTE (byte_after_cr
);
1314 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1316 if (UTF_8_2_OCTET_LEADING_P (c1
))
1318 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1319 /* Reject overlong sequences here and below. Encoders
1320 producing them are incorrect, they can be misleading,
1321 and they mess up read/write invariance. */
1328 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1330 if (UTF_8_3_OCTET_LEADING_P (c1
))
1332 c
= (((c1
& 0xF) << 12)
1333 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1335 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1341 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1343 if (UTF_8_4_OCTET_LEADING_P (c1
))
1345 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1346 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1353 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1355 if (UTF_8_5_OCTET_LEADING_P (c1
))
1357 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1358 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1360 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1375 consumed_chars
= consumed_chars_base
;
1377 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1382 coding
->consumed_char
+= consumed_chars_base
;
1383 coding
->consumed
= src_base
- coding
->source
;
1384 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1389 encode_coding_utf_8 (struct coding_system
*coding
)
1391 bool multibytep
= coding
->dst_multibyte
;
1392 int *charbuf
= coding
->charbuf
;
1393 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1394 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1395 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1396 ptrdiff_t produced_chars
= 0;
1399 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1401 ASSURE_DESTINATION (3);
1402 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1403 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1408 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1410 while (charbuf
< charbuf_end
)
1412 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1414 ASSURE_DESTINATION (safe_room
);
1416 if (CHAR_BYTE8_P (c
))
1418 c
= CHAR_TO_BYTE8 (c
);
1423 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1424 for (p
= str
; p
< pend
; p
++)
1431 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1433 while (charbuf
< charbuf_end
)
1435 ASSURE_DESTINATION (safe_room
);
1437 if (CHAR_BYTE8_P (c
))
1438 *dst
++ = CHAR_TO_BYTE8 (c
);
1440 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1444 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1445 coding
->produced_char
+= produced_chars
;
1446 coding
->produced
= dst
- coding
->destination
;
1451 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1452 Return true if a text is encoded in one of UTF-16 based coding systems. */
1454 #define UTF_16_HIGH_SURROGATE_P(val) \
1455 (((val) & 0xFC00) == 0xD800)
1457 #define UTF_16_LOW_SURROGATE_P(val) \
1458 (((val) & 0xFC00) == 0xDC00)
1462 detect_coding_utf_16 (struct coding_system
*coding
,
1463 struct coding_detection_info
*detect_info
)
1465 const unsigned char *src
= coding
->source
;
1466 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1467 bool multibytep
= coding
->src_multibyte
;
1470 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1471 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1472 && (coding
->src_chars
& 1))
1474 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1478 TWO_MORE_BYTES (c1
, c2
);
1479 if ((c1
== 0xFF) && (c2
== 0xFE))
1481 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1482 | CATEGORY_MASK_UTF_16_AUTO
);
1483 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1484 | CATEGORY_MASK_UTF_16_BE_NOSIG
1485 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1487 else if ((c1
== 0xFE) && (c2
== 0xFF))
1489 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1490 | CATEGORY_MASK_UTF_16_AUTO
);
1491 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1492 | CATEGORY_MASK_UTF_16_BE_NOSIG
1493 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1497 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1502 /* We check the dispersion of Eth and Oth bytes where E is even and
1503 O is odd. If both are high, we assume binary data.*/
1504 unsigned char e
[256], o
[256];
1505 unsigned e_num
= 1, o_num
= 1;
1512 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1513 |CATEGORY_MASK_UTF_16_BE
1514 | CATEGORY_MASK_UTF_16_LE
);
1516 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1517 != CATEGORY_MASK_UTF_16
)
1519 TWO_MORE_BYTES (c1
, c2
);
1527 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1534 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1545 decode_coding_utf_16 (struct coding_system
*coding
)
1547 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1548 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1549 const unsigned char *src_base
;
1550 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1551 /* We may produces at most 3 chars in one loop. */
1552 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1553 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1554 bool multibytep
= coding
->src_multibyte
;
1555 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1556 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1557 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1559 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1560 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1562 if (bom
== utf_with_bom
)
1571 if (endian
== utf_16_big_endian
1572 ? c
!= 0xFEFF : c
!= 0xFFFE)
1574 /* The first two bytes are not BOM. Treat them as bytes
1575 for a normal character. */
1579 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1581 else if (bom
== utf_detect_bom
)
1583 /* We have already tried to detect BOM and failed in
1585 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1593 consumed_chars_base
= consumed_chars
;
1595 if (charbuf
>= charbuf_end
)
1597 if (byte_after_cr1
>= 0)
1602 if (byte_after_cr1
>= 0)
1603 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1611 if (byte_after_cr2
>= 0)
1612 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1617 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1621 c
= (endian
== utf_16_big_endian
1622 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1626 if (! UTF_16_LOW_SURROGATE_P (c
))
1628 if (endian
== utf_16_big_endian
)
1629 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1631 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1635 if (UTF_16_HIGH_SURROGATE_P (c
))
1636 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1642 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1643 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1644 *charbuf
++ = 0x10000 + c
;
1649 if (UTF_16_HIGH_SURROGATE_P (c
))
1650 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1653 if (eol_dos
&& c
== '\r')
1655 ONE_MORE_BYTE (byte_after_cr1
);
1656 ONE_MORE_BYTE (byte_after_cr2
);
1664 coding
->consumed_char
+= consumed_chars_base
;
1665 coding
->consumed
= src_base
- coding
->source
;
1666 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1670 encode_coding_utf_16 (struct coding_system
*coding
)
1672 bool multibytep
= coding
->dst_multibyte
;
1673 int *charbuf
= coding
->charbuf
;
1674 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1675 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1676 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1678 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1679 bool big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1680 ptrdiff_t produced_chars
= 0;
1683 if (bom
!= utf_without_bom
)
1685 ASSURE_DESTINATION (safe_room
);
1687 EMIT_TWO_BYTES (0xFE, 0xFF);
1689 EMIT_TWO_BYTES (0xFF, 0xFE);
1690 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1693 while (charbuf
< charbuf_end
)
1695 ASSURE_DESTINATION (safe_room
);
1697 if (c
> MAX_UNICODE_CHAR
)
1698 c
= coding
->default_char
;
1703 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1705 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1712 c1
= (c
>> 10) + 0xD800;
1713 c2
= (c
& 0x3FF) + 0xDC00;
1715 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1717 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1720 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1721 coding
->produced
= dst
- coding
->destination
;
1722 coding
->produced_char
+= produced_chars
;
1727 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1729 /* Emacs' internal format for representation of multiple character
1730 sets is a kind of multi-byte encoding, i.e. characters are
1731 represented by variable-length sequences of one-byte codes.
1733 ASCII characters and control characters (e.g. `tab', `newline') are
1734 represented by one-byte sequences which are their ASCII codes, in
1735 the range 0x00 through 0x7F.
1737 8-bit characters of the range 0x80..0x9F are represented by
1738 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1741 8-bit characters of the range 0xA0..0xFF are represented by
1742 one-byte sequences which are their 8-bit code.
1744 The other characters are represented by a sequence of `base
1745 leading-code', optional `extended leading-code', and one or two
1746 `position-code's. The length of the sequence is determined by the
1747 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1748 whereas extended leading-code and position-code take the range 0xA0
1749 through 0xFF. See `charset.h' for more details about leading-code
1752 --- CODE RANGE of Emacs' internal format ---
1756 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1757 eight-bit-graphic 0xA0..0xBF
1758 ELSE 0x81..0x9D + [0xA0..0xFF]+
1759 ---------------------------------------------
1761 As this is the internal character representation, the format is
1762 usually not used externally (i.e. in a file or in a data sent to a
1763 process). But, it is possible to have a text externally in this
1764 format (i.e. by encoding by the coding system `emacs-mule').
1766 In that case, a sequence of one-byte codes has a slightly different
1769 At first, all characters in eight-bit-control are represented by
1770 one-byte sequences which are their 8-bit code.
1772 Next, character composition data are represented by the byte
1773 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1775 METHOD is 0xF2 plus one of composition method (enum
1776 composition_method),
1778 BYTES is 0xA0 plus a byte length of this composition data,
1780 CHARS is 0xA0 plus a number of characters composed by this
1783 COMPONENTs are characters of multibyte form or composition
1784 rules encoded by two-byte of ASCII codes.
1786 In addition, for backward compatibility, the following formats are
1787 also recognized as composition data on decoding.
1790 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1793 MSEQ is a multibyte form but in these special format:
1794 ASCII: 0xA0 ASCII_CODE+0x80,
1795 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1796 RULE is a one byte code of the range 0xA0..0xF0 that
1797 represents a composition rule.
1800 char emacs_mule_bytes
[256];
1803 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1804 Return true if a text is encoded in 'emacs-mule'. */
1807 detect_coding_emacs_mule (struct coding_system
*coding
,
1808 struct coding_detection_info
*detect_info
)
1810 const unsigned char *src
= coding
->source
, *src_base
;
1811 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1812 bool multibytep
= coding
->src_multibyte
;
1813 ptrdiff_t consumed_chars
= 0;
1817 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1818 /* A coding system of this category is always ASCII compatible. */
1819 src
+= coding
->head_ascii
;
1829 /* Perhaps the start of composite character. We simply skip
1830 it because analyzing it is too heavy for detecting. But,
1831 at least, we check that the composite character
1832 constitutes of more than 4 bytes. */
1833 const unsigned char *src_start
;
1843 if (src
- src_start
<= 4)
1845 found
= CATEGORY_MASK_EMACS_MULE
;
1853 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1858 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1860 while (more_bytes
> 0)
1865 src
--; /* Unread the last byte. */
1870 if (more_bytes
!= 0)
1872 found
= CATEGORY_MASK_EMACS_MULE
;
1875 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1879 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1881 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1884 detect_info
->found
|= found
;
1889 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1890 character. If CMP_STATUS indicates that we must expect MSEQ or
1891 RULE described above, decode it and return the negative value of
1892 the decoded character or rule. If an invalid byte is found, return
1893 -1. If SRC is too short, return -2. */
1896 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1897 int *nbytes
, int *nchars
, int *id
,
1898 struct composition_status
*cmp_status
)
1900 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1901 const unsigned char *src_base
= src
;
1902 bool multibytep
= coding
->src_multibyte
;
1906 int consumed_chars
= 0;
1907 bool mseq_found
= 0;
1913 charset_ID
= emacs_mule_charset
[0];
1919 if (cmp_status
->state
!= COMPOSING_NO
1920 && cmp_status
->old_form
)
1922 if (cmp_status
->state
== COMPOSING_CHAR
)
1937 *nbytes
= src
- src_base
;
1938 *nchars
= consumed_chars
;
1946 switch (emacs_mule_bytes
[c
])
1949 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
1958 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1959 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1962 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
1971 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
1976 code
= (c
& 0x7F) << 8;
1986 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
1991 code
= (c
& 0x7F) << 8;
2000 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2006 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2007 CHARSET_FROM_ID (charset_ID
), code
, c
);
2011 *nbytes
= src
- src_base
;
2012 *nchars
= consumed_chars
;
2015 return (mseq_found
? -c
: c
);
2025 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2027 /* Handle these composition sequence ('|': the end of header elements,
2028 BYTES and CHARS >= 0xA0):
2030 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2031 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2032 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2036 (4) relative composition: 0x80 | MSEQ ... MSEQ
2037 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2039 When the starter 0x80 and the following header elements are found,
2040 this annotation header is produced.
2042 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2044 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2045 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2047 Then, upon reading the following elements, these codes are produced
2048 until the composition end is found:
2051 (2) ALT ... ALT CHAR ... CHAR
2052 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2054 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2056 When the composition end is found, LENGTH and NCHARS in the
2057 annotation header is updated as below:
2059 (1) LENGTH: unchanged, NCHARS: unchanged
2060 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2061 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2062 (4) LENGTH: unchanged, NCHARS: number of CHARs
2063 (5) LENGTH: unchanged, NCHARS: number of CHARs
2065 If an error is found while composing, the annotation header is
2066 changed to the original composition header (plus filler -1s) as
2069 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2070 (5) [ 0x80 0xFF -1 -1- -1 ]
2072 and the sequence [ -2 DECODED-RULE ] is changed to the original
2073 byte sequence as below:
2074 o the original byte sequence is B: [ B -1 ]
2075 o the original byte sequence is B1 B2: [ B1 B2 ]
2077 Most of the routines are implemented by macros because many
2078 variables and labels in the caller decode_coding_emacs_mule must be
2079 accessible, and they are usually called just once (thus doesn't
2080 increase the size of compiled object). */
2082 /* Decode a composition rule represented by C as a component of
2083 composition sequence of Emacs 20 style. Set RULE to the decoded
2086 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2091 if (c < 0 || c >= 81) \
2092 goto invalid_code; \
2093 gref = c / 9, nref = c % 9; \
2094 if (gref == 4) gref = 10; \
2095 if (nref == 4) nref = 10; \
2096 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2100 /* Decode a composition rule represented by C and the following byte
2101 at SRC as a component of composition sequence of Emacs 21 style.
2102 Set RULE to the decoded rule. */
2104 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2109 if (gref < 0 || gref >= 81) \
2110 goto invalid_code; \
2111 ONE_MORE_BYTE (c); \
2113 if (nref < 0 || nref >= 81) \
2114 goto invalid_code; \
2115 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2119 /* Start of Emacs 21 style format. The first three bytes at SRC are
2120 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2121 byte length of this composition information, CHARS is the number of
2122 characters composed by this composition. */
2124 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2126 enum composition_method method = c - 0xF2; \
2127 int nbytes, nchars; \
2129 ONE_MORE_BYTE (c); \
2131 goto invalid_code; \
2132 nbytes = c - 0xA0; \
2133 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2134 goto invalid_code; \
2135 ONE_MORE_BYTE (c); \
2136 nchars = c - 0xA0; \
2137 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2138 goto invalid_code; \
2139 cmp_status->old_form = 0; \
2140 cmp_status->method = method; \
2141 if (method == COMPOSITION_RELATIVE) \
2142 cmp_status->state = COMPOSING_CHAR; \
2144 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2145 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2146 cmp_status->nchars = nchars; \
2147 cmp_status->ncomps = nbytes - 4; \
2148 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2152 /* Start of Emacs 20 style format for relative composition. */
2154 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2156 cmp_status->old_form = 1; \
2157 cmp_status->method = COMPOSITION_RELATIVE; \
2158 cmp_status->state = COMPOSING_CHAR; \
2159 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2160 cmp_status->nchars = cmp_status->ncomps = 0; \
2161 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2165 /* Start of Emacs 20 style format for rule-base composition. */
2167 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2169 cmp_status->old_form = 1; \
2170 cmp_status->method = COMPOSITION_WITH_RULE; \
2171 cmp_status->state = COMPOSING_CHAR; \
2172 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2173 cmp_status->nchars = cmp_status->ncomps = 0; \
2174 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2178 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2180 const unsigned char *current_src = src; \
2182 ONE_MORE_BYTE (c); \
2184 goto invalid_code; \
2185 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2186 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2187 DECODE_EMACS_MULE_21_COMPOSITION (); \
2188 else if (c < 0xA0) \
2189 goto invalid_code; \
2190 else if (c < 0xC0) \
2192 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2193 /* Re-read C as a composition component. */ \
2194 src = current_src; \
2196 else if (c == 0xFF) \
2197 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2199 goto invalid_code; \
2202 #define EMACS_MULE_COMPOSITION_END() \
2204 int idx = - cmp_status->length; \
2206 if (cmp_status->old_form) \
2207 charbuf[idx + 2] = cmp_status->nchars; \
2208 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2209 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2210 cmp_status->state = COMPOSING_NO; \
2215 emacs_mule_finish_composition (int *charbuf
,
2216 struct composition_status
*cmp_status
)
2218 int idx
= - cmp_status
->length
;
2221 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2223 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2225 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2226 && cmp_status
->state
== COMPOSING_CHAR
)
2228 /* The last rule was invalid. */
2229 int rule
= charbuf
[-1] + 0xA0;
2231 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2238 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2240 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2242 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2243 charbuf
[idx
++] = -3;
2249 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2250 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2252 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2253 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2254 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2255 charbuf
[idx
++] = -1;
2259 cmp_status
->state
= COMPOSING_NO
;
2263 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2265 if (cmp_status->state != COMPOSING_NO) \
2266 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2271 decode_coding_emacs_mule (struct coding_system
*coding
)
2273 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2274 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2275 const unsigned char *src_base
;
2276 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2277 /* We may produce two annotations (charset and composition) in one
2278 loop and one more charset annotation at the end. */
2280 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2281 /* We can produce up to 2 characters in a loop. */
2283 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
2284 bool multibytep
= coding
->src_multibyte
;
2285 ptrdiff_t char_offset
= coding
->produced_char
;
2286 ptrdiff_t last_offset
= char_offset
;
2287 int last_id
= charset_ascii
;
2289 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2290 int byte_after_cr
= -1;
2291 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2293 if (cmp_status
->state
!= COMPOSING_NO
)
2297 if (charbuf_end
- charbuf
< cmp_status
->length
)
2299 for (i
= 0; i
< cmp_status
->length
; i
++)
2300 *charbuf
++ = cmp_status
->carryover
[i
];
2301 coding
->annotated
= 1;
2306 int c
, id
IF_LINT (= 0);
2309 consumed_chars_base
= consumed_chars
;
2311 if (charbuf
>= charbuf_end
)
2313 if (byte_after_cr
>= 0)
2318 if (byte_after_cr
>= 0)
2319 c
= byte_after_cr
, byte_after_cr
= -1;
2323 if (c
< 0 || c
== 0x80)
2325 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2332 DECODE_EMACS_MULE_COMPOSITION_START ();
2338 if (eol_dos
&& c
== '\r')
2339 ONE_MORE_BYTE (byte_after_cr
);
2341 if (cmp_status
->state
!= COMPOSING_NO
)
2343 if (cmp_status
->old_form
)
2344 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2345 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2346 cmp_status
->ncomps
--;
2351 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2352 /* emacs_mule_char can load a charset map from a file, which
2353 allocates a large structure and might cause buffer text
2354 to be relocated as result. Thus, we need to remember the
2355 original pointer to buffer text, and fix up all related
2356 pointers after the call. */
2357 const unsigned char *orig
= coding
->source
;
2360 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2362 offset
= coding
->source
- orig
;
2376 src
= src_base
+ nbytes
;
2377 consumed_chars
= consumed_chars_base
+ nchars
;
2378 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2379 cmp_status
->ncomps
-= nchars
;
2382 /* Now if C >= 0, we found a normally encoded character, if C <
2383 0, we found an old-style composition component character or
2386 if (cmp_status
->state
== COMPOSING_NO
)
2390 if (last_id
!= charset_ascii
)
2391 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2394 last_offset
= char_offset
;
2399 else if (cmp_status
->state
== COMPOSING_CHAR
)
2401 if (cmp_status
->old_form
)
2405 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2412 cmp_status
->nchars
++;
2413 cmp_status
->length
++;
2414 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2415 EMACS_MULE_COMPOSITION_END ();
2416 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2417 cmp_status
->state
= COMPOSING_RULE
;
2423 cmp_status
->length
++;
2424 cmp_status
->nchars
--;
2425 if (cmp_status
->nchars
== 0)
2426 EMACS_MULE_COMPOSITION_END ();
2429 else if (cmp_status
->state
== COMPOSING_RULE
)
2435 EMACS_MULE_COMPOSITION_END ();
2442 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2447 cmp_status
->length
+= 2;
2448 cmp_status
->state
= COMPOSING_CHAR
;
2451 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2454 cmp_status
->length
++;
2455 if (cmp_status
->ncomps
== 0)
2456 cmp_status
->state
= COMPOSING_CHAR
;
2457 else if (cmp_status
->ncomps
> 0)
2459 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2460 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2463 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2465 else /* COMPOSING_COMPONENT_RULE */
2469 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2474 cmp_status
->length
+= 2;
2475 cmp_status
->ncomps
--;
2476 if (cmp_status
->ncomps
> 0)
2477 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2479 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2484 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2486 consumed_chars
= consumed_chars_base
;
2488 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2494 if (cmp_status
->state
!= COMPOSING_NO
)
2496 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2497 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2502 charbuf
-= cmp_status
->length
;
2503 for (i
= 0; i
< cmp_status
->length
; i
++)
2504 cmp_status
->carryover
[i
] = charbuf
[i
];
2507 if (last_id
!= charset_ascii
)
2508 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2509 coding
->consumed_char
+= consumed_chars_base
;
2510 coding
->consumed
= src_base
- coding
->source
;
2511 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2515 #define EMACS_MULE_LEADING_CODES(id, codes) \
2518 codes[0] = id, codes[1] = 0; \
2519 else if (id < 0xE0) \
2520 codes[0] = 0x9A, codes[1] = id; \
2521 else if (id < 0xF0) \
2522 codes[0] = 0x9B, codes[1] = id; \
2523 else if (id < 0xF5) \
2524 codes[0] = 0x9C, codes[1] = id; \
2526 codes[0] = 0x9D, codes[1] = id; \
2531 encode_coding_emacs_mule (struct coding_system
*coding
)
2533 bool multibytep
= coding
->dst_multibyte
;
2534 int *charbuf
= coding
->charbuf
;
2535 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2536 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2537 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2539 ptrdiff_t produced_chars
= 0;
2540 Lisp_Object attrs
, charset_list
;
2542 int preferred_charset_id
= -1;
2544 CODING_GET_INFO (coding
, attrs
, charset_list
);
2545 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2547 charset_list
= Vemacs_mule_charset_list
;
2548 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2551 while (charbuf
< charbuf_end
)
2553 ASSURE_DESTINATION (safe_room
);
2558 /* Handle an annotation. */
2561 case CODING_ANNOTATE_COMPOSITION_MASK
:
2562 /* Not yet implemented. */
2564 case CODING_ANNOTATE_CHARSET_MASK
:
2565 preferred_charset_id
= charbuf
[3];
2566 if (preferred_charset_id
>= 0
2567 && NILP (Fmemq (make_number (preferred_charset_id
),
2569 preferred_charset_id
= -1;
2578 if (ASCII_CHAR_P (c
))
2579 EMIT_ONE_ASCII_BYTE (c
);
2580 else if (CHAR_BYTE8_P (c
))
2582 c
= CHAR_TO_BYTE8 (c
);
2587 struct charset
*charset
;
2591 unsigned char leading_codes
[2];
2593 if (preferred_charset_id
>= 0)
2597 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2598 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2600 code
= ENCODE_CHAR (charset
, c
);
2602 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2606 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2610 c
= coding
->default_char
;
2611 if (ASCII_CHAR_P (c
))
2613 EMIT_ONE_ASCII_BYTE (c
);
2616 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2619 dimension
= CHARSET_DIMENSION (charset
);
2620 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2621 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2622 EMIT_ONE_BYTE (leading_codes
[0]);
2623 if (leading_codes
[1])
2624 EMIT_ONE_BYTE (leading_codes
[1]);
2626 EMIT_ONE_BYTE (code
| 0x80);
2630 EMIT_ONE_BYTE (code
>> 8);
2631 EMIT_ONE_BYTE (code
& 0xFF);
2635 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2636 coding
->produced_char
+= produced_chars
;
2637 coding
->produced
= dst
- coding
->destination
;
2642 /*** 7. ISO2022 handlers ***/
2644 /* The following note describes the coding system ISO2022 briefly.
2645 Since the intention of this note is to help understand the
2646 functions in this file, some parts are NOT ACCURATE or are OVERLY
2647 SIMPLIFIED. For thorough understanding, please refer to the
2648 original document of ISO2022. This is equivalent to the standard
2649 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2651 ISO2022 provides many mechanisms to encode several character sets
2652 in 7-bit and 8-bit environments. For 7-bit environments, all text
2653 is encoded using bytes less than 128. This may make the encoded
2654 text a little bit longer, but the text passes more easily through
2655 several types of gateway, some of which strip off the MSB (Most
2658 There are two kinds of character sets: control character sets and
2659 graphic character sets. The former contain control characters such
2660 as `newline' and `escape' to provide control functions (control
2661 functions are also provided by escape sequences). The latter
2662 contain graphic characters such as 'A' and '-'. Emacs recognizes
2663 two control character sets and many graphic character sets.
2665 Graphic character sets are classified into one of the following
2666 four classes, according to the number of bytes (DIMENSION) and
2667 number of characters in one dimension (CHARS) of the set:
2668 - DIMENSION1_CHARS94
2669 - DIMENSION1_CHARS96
2670 - DIMENSION2_CHARS94
2671 - DIMENSION2_CHARS96
2673 In addition, each character set is assigned an identification tag,
2674 unique for each set, called the "final character" (denoted as <F>
2675 hereafter). The <F> of each character set is decided by ECMA(*)
2676 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2677 (0x30..0x3F are for private use only).
2679 Note (*): ECMA = European Computer Manufacturers Association
2681 Here are examples of graphic character sets [NAME(<F>)]:
2682 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2683 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2684 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2685 o DIMENSION2_CHARS96 -- none for the moment
2687 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2688 C0 [0x00..0x1F] -- control character plane 0
2689 GL [0x20..0x7F] -- graphic character plane 0
2690 C1 [0x80..0x9F] -- control character plane 1
2691 GR [0xA0..0xFF] -- graphic character plane 1
2693 A control character set is directly designated and invoked to C0 or
2694 C1 by an escape sequence. The most common case is that:
2695 - ISO646's control character set is designated/invoked to C0, and
2696 - ISO6429's control character set is designated/invoked to C1,
2697 and usually these designations/invocations are omitted in encoded
2698 text. In a 7-bit environment, only C0 can be used, and a control
2699 character for C1 is encoded by an appropriate escape sequence to
2700 fit into the environment. All control characters for C1 are
2701 defined to have corresponding escape sequences.
2703 A graphic character set is at first designated to one of four
2704 graphic registers (G0 through G3), then these graphic registers are
2705 invoked to GL or GR. These designations and invocations can be
2706 done independently. The most common case is that G0 is invoked to
2707 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2708 these invocations and designations are omitted in encoded text.
2709 In a 7-bit environment, only GL can be used.
2711 When a graphic character set of CHARS94 is invoked to GL, codes
2712 0x20 and 0x7F of the GL area work as control characters SPACE and
2713 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2716 There are two ways of invocation: locking-shift and single-shift.
2717 With locking-shift, the invocation lasts until the next different
2718 invocation, whereas with single-shift, the invocation affects the
2719 following character only and doesn't affect the locking-shift
2720 state. Invocations are done by the following control characters or
2723 ----------------------------------------------------------------------
2724 abbrev function cntrl escape seq description
2725 ----------------------------------------------------------------------
2726 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2727 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2728 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2729 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2730 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2731 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2732 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2733 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2734 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2735 ----------------------------------------------------------------------
2736 (*) These are not used by any known coding system.
2738 Control characters for these functions are defined by macros
2739 ISO_CODE_XXX in `coding.h'.
2741 Designations are done by the following escape sequences:
2742 ----------------------------------------------------------------------
2743 escape sequence description
2744 ----------------------------------------------------------------------
2745 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2746 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2747 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2748 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2749 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2750 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2751 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2752 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2753 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2754 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2755 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2756 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2757 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2758 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2759 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2760 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2761 ----------------------------------------------------------------------
2763 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2764 of dimension 1, chars 94, and final character <F>, etc...
2766 Note (*): Although these designations are not allowed in ISO2022,
2767 Emacs accepts them on decoding, and produces them on encoding
2768 CHARS96 character sets in a coding system which is characterized as
2769 7-bit environment, non-locking-shift, and non-single-shift.
2771 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2772 '(' must be omitted. We refer to this as "short-form" hereafter.
2774 Now you may notice that there are a lot of ways of encoding the
2775 same multilingual text in ISO2022. Actually, there exist many
2776 coding systems such as Compound Text (used in X11's inter client
2777 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2778 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2779 localized platforms), and all of these are variants of ISO2022.
2781 In addition to the above, Emacs handles two more kinds of escape
2782 sequences: ISO6429's direction specification and Emacs' private
2783 sequence for specifying character composition.
2785 ISO6429's direction specification takes the following form:
2786 o CSI ']' -- end of the current direction
2787 o CSI '0' ']' -- end of the current direction
2788 o CSI '1' ']' -- start of left-to-right text
2789 o CSI '2' ']' -- start of right-to-left text
2790 The control character CSI (0x9B: control sequence introducer) is
2791 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2793 Character composition specification takes the following form:
2794 o ESC '0' -- start relative composition
2795 o ESC '1' -- end composition
2796 o ESC '2' -- start rule-base composition (*)
2797 o ESC '3' -- start relative composition with alternate chars (**)
2798 o ESC '4' -- start rule-base composition with alternate chars (**)
2799 Since these are not standard escape sequences of any ISO standard,
2800 the use of them with these meanings is restricted to Emacs only.
2802 (*) This form is used only in Emacs 20.7 and older versions,
2803 but newer versions can safely decode it.
2804 (**) This form is used only in Emacs 21.1 and newer versions,
2805 and older versions can't decode it.
2807 Here's a list of example usages of these composition escape
2808 sequences (categorized by `enum composition_method').
2810 COMPOSITION_RELATIVE:
2811 ESC 0 CHAR [ CHAR ] ESC 1
2812 COMPOSITION_WITH_RULE:
2813 ESC 2 CHAR [ RULE CHAR ] ESC 1
2814 COMPOSITION_WITH_ALTCHARS:
2815 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2816 COMPOSITION_WITH_RULE_ALTCHARS:
2817 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2819 static enum iso_code_class_type iso_code_class
[256];
2821 #define SAFE_CHARSET_P(coding, id) \
2822 ((id) <= (coding)->max_charset_id \
2823 && (coding)->safe_charsets[id] != 255)
2826 setup_iso_safe_charsets (Lisp_Object attrs
)
2828 Lisp_Object charset_list
, safe_charsets
;
2829 Lisp_Object request
;
2830 Lisp_Object reg_usage
;
2832 EMACS_INT reg94
, reg96
;
2833 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2836 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2837 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2838 && ! EQ (charset_list
, Viso_2022_charset_list
))
2840 charset_list
= Viso_2022_charset_list
;
2841 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2842 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2845 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2849 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2851 int id
= XINT (XCAR (tail
));
2852 if (max_charset_id
< id
)
2853 max_charset_id
= id
;
2856 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2857 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2858 request
= AREF (attrs
, coding_attr_iso_request
);
2859 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2860 reg94
= XINT (XCAR (reg_usage
));
2861 reg96
= XINT (XCDR (reg_usage
));
2863 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2867 struct charset
*charset
;
2870 charset
= CHARSET_FROM_ID (XINT (id
));
2871 reg
= Fcdr (Fassq (id
, request
));
2873 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2874 else if (charset
->iso_chars_96
)
2877 SSET (safe_charsets
, XINT (id
), reg96
);
2882 SSET (safe_charsets
, XINT (id
), reg94
);
2885 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2889 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2890 Return true if a text is encoded in one of ISO-2022 based coding
2894 detect_coding_iso_2022 (struct coding_system
*coding
,
2895 struct coding_detection_info
*detect_info
)
2897 const unsigned char *src
= coding
->source
, *src_base
= src
;
2898 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2899 bool multibytep
= coding
->src_multibyte
;
2900 bool single_shifting
= 0;
2903 ptrdiff_t consumed_chars
= 0;
2907 int composition_count
= -1;
2909 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2911 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2913 struct coding_system
*this = &(coding_categories
[i
]);
2914 Lisp_Object attrs
, val
;
2918 attrs
= CODING_ID_ATTRS (this->id
);
2919 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2920 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2921 setup_iso_safe_charsets (attrs
);
2922 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2923 this->max_charset_id
= SCHARS (val
) - 1;
2924 this->safe_charsets
= SDATA (val
);
2927 /* A coding system of this category is always ASCII compatible. */
2928 src
+= coding
->head_ascii
;
2930 while (rejected
!= CATEGORY_MASK_ISO
)
2937 if (inhibit_iso_escape_detection
)
2939 single_shifting
= 0;
2941 if (c
== 'N' || c
== 'O')
2943 /* ESC <Fe> for SS2 or SS3. */
2944 single_shifting
= 1;
2945 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2949 /* End of composition. */
2950 if (composition_count
< 0
2951 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
2954 composition_count
= -1;
2955 found
|= CATEGORY_MASK_ISO
;
2957 else if (c
>= '0' && c
<= '4')
2959 /* ESC <Fp> for start/end composition. */
2960 composition_count
= 0;
2964 if (c
>= '(' && c
<= '/')
2966 /* Designation sequence for a charset of dimension 1. */
2968 if (c1
< ' ' || c1
>= 0x80
2969 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2970 /* Invalid designation sequence. Just ignore. */
2975 /* Designation sequence for a charset of dimension 2. */
2977 if (c
>= '@' && c
<= 'B')
2978 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2979 id
= iso_charset_table
[1][0][c
];
2980 else if (c
>= '(' && c
<= '/')
2983 if (c1
< ' ' || c1
>= 0x80
2984 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2985 /* Invalid designation sequence. Just ignore. */
2989 /* Invalid designation sequence. Just ignore it. */
2994 /* Invalid escape sequence. Just ignore it. */
2998 /* We found a valid designation sequence for CHARSET. */
2999 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3000 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3002 found
|= CATEGORY_MASK_ISO_7
;
3004 rejected
|= CATEGORY_MASK_ISO_7
;
3005 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3007 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3009 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3010 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3012 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3014 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3015 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3017 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3019 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3025 /* Locking shift out/in. */
3026 if (inhibit_iso_escape_detection
)
3028 single_shifting
= 0;
3029 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3033 /* Control sequence introducer. */
3034 single_shifting
= 0;
3035 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3036 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3037 goto check_extra_latin
;
3042 if (inhibit_iso_escape_detection
)
3044 single_shifting
= 0;
3045 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3046 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3047 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3049 found
|= CATEGORY_MASK_ISO_8_1
;
3050 single_shifting
= 1;
3052 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3053 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3055 found
|= CATEGORY_MASK_ISO_8_2
;
3056 single_shifting
= 1;
3058 if (single_shifting
)
3060 goto check_extra_latin
;
3067 if (composition_count
>= 0)
3068 composition_count
++;
3069 single_shifting
= 0;
3074 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3075 found
|= CATEGORY_MASK_ISO_8_1
;
3076 /* Check the length of succeeding codes of the range
3077 0xA0..0FF. If the byte length is even, we include
3078 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3079 only when we are not single shifting. */
3080 if (! single_shifting
3081 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3084 while (src
< src_end
)
3096 if (len
& 1 && src
< src_end
)
3098 rejected
|= CATEGORY_MASK_ISO_8_2
;
3099 if (composition_count
>= 0)
3100 composition_count
+= len
;
3104 found
|= CATEGORY_MASK_ISO_8_2
;
3105 if (composition_count
>= 0)
3106 composition_count
+= len
/ 2;
3112 if (! VECTORP (Vlatin_extra_code_table
)
3113 || NILP (AREF (Vlatin_extra_code_table
, c
)))
3115 rejected
= CATEGORY_MASK_ISO
;
3118 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3119 & CODING_ISO_FLAG_LATIN_EXTRA
)
3120 found
|= CATEGORY_MASK_ISO_8_1
;
3122 rejected
|= CATEGORY_MASK_ISO_8_1
;
3123 rejected
|= CATEGORY_MASK_ISO_8_2
;
3127 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3131 detect_info
->rejected
|= rejected
;
3132 detect_info
->found
|= (found
& ~rejected
);
3137 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3138 escape sequence should be kept. */
3139 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3143 if (final < '0' || final >= 128 \
3144 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3145 || !SAFE_CHARSET_P (coding, id)) \
3147 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3151 prev = CODING_ISO_DESIGNATION (coding, reg); \
3152 if (id == charset_jisx0201_roman) \
3154 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3155 id = charset_ascii; \
3157 else if (id == charset_jisx0208_1978) \
3159 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3160 id = charset_jisx0208; \
3162 CODING_ISO_DESIGNATION (coding, reg) = id; \
3163 /* If there was an invalid designation to REG previously, and this \
3164 designation is ASCII to REG, we should keep this designation \
3166 if (prev == -2 && id == charset_ascii) \
3171 /* Handle these composition sequence (ALT: alternate char):
3173 (1) relative composition: ESC 0 CHAR ... ESC 1
3174 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3175 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3176 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3178 When the start sequence (ESC 0/2/3/4) is found, this annotation
3181 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3183 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3184 produced until the end sequence (ESC 1) is found:
3187 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3188 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3189 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3191 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3192 annotation header is updated as below:
3194 (1) LENGTH: unchanged, NCHARS: number of CHARs
3195 (2) LENGTH: unchanged, NCHARS: number of CHARs
3196 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3197 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3199 If an error is found while composing, the annotation header is
3202 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3204 and the sequence [ -2 DECODED-RULE ] is changed to the original
3205 byte sequence as below:
3206 o the original byte sequence is B: [ B -1 ]
3207 o the original byte sequence is B1 B2: [ B1 B2 ]
3208 and the sequence [ -1 -1 ] is changed to the original byte
3213 /* Decode a composition rule C1 and maybe one more byte from the
3214 source, and set RULE to the encoded composition rule. If the rule
3215 is invalid, goto invalid_code. */
3217 #define DECODE_COMPOSITION_RULE(rule) \
3221 goto invalid_code; \
3222 if (rule < 81) /* old format (before ver.21) */ \
3224 int gref = (rule) / 9; \
3225 int nref = (rule) % 9; \
3226 if (gref == 4) gref = 10; \
3227 if (nref == 4) nref = 10; \
3228 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3230 else /* new format (after ver.21) */ \
3234 ONE_MORE_BYTE (b); \
3235 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3236 goto invalid_code; \
3237 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3238 rule += 0x100; /* Distinguish it from the old format. */ \
3242 #define ENCODE_COMPOSITION_RULE(rule) \
3244 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3246 if (rule < 0x100) /* old format */ \
3248 if (gref == 10) gref = 4; \
3249 if (nref == 10) nref = 4; \
3250 charbuf[idx] = 32 + gref * 9 + nref; \
3251 charbuf[idx + 1] = -1; \
3254 else /* new format */ \
3256 charbuf[idx] = 32 + 81 + gref; \
3257 charbuf[idx + 1] = 32 + nref; \
3262 /* Finish the current composition as invalid. */
3265 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3267 int idx
= - cmp_status
->length
;
3270 /* Recover the original ESC sequence */
3271 charbuf
[idx
++] = ISO_CODE_ESC
;
3272 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3273 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3274 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3275 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3277 charbuf
[idx
++] = -2;
3279 charbuf
[idx
++] = -1;
3280 new_chars
= cmp_status
->nchars
;
3281 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3282 for (; idx
< 0; idx
++)
3284 int elt
= charbuf
[idx
];
3288 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3293 charbuf
[idx
++] = ISO_CODE_ESC
;
3298 cmp_status
->state
= COMPOSING_NO
;
3302 /* If characters are under composition, finish the composition. */
3303 #define MAYBE_FINISH_COMPOSITION() \
3305 if (cmp_status->state != COMPOSING_NO) \
3306 char_offset += finish_composition (charbuf, cmp_status); \
3309 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3311 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3312 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3313 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3314 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3316 Produce this annotation sequence now:
3318 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3321 #define DECODE_COMPOSITION_START(c1) \
3324 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3325 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3326 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3327 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3331 cmp_status->state = COMPOSING_CHAR; \
3332 cmp_status->length += 2; \
3336 MAYBE_FINISH_COMPOSITION (); \
3337 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3338 : c1 == '2' ? COMPOSITION_WITH_RULE \
3339 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3340 : COMPOSITION_WITH_RULE_ALTCHARS); \
3342 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3343 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3344 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3345 cmp_status->nchars = cmp_status->ncomps = 0; \
3346 coding->annotated = 1; \
3351 /* Handle composition end sequence ESC 1. */
3353 #define DECODE_COMPOSITION_END() \
3355 if (cmp_status->nchars == 0 \
3356 || ((cmp_status->state == COMPOSING_CHAR) \
3357 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3359 MAYBE_FINISH_COMPOSITION (); \
3360 goto invalid_code; \
3362 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3363 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3364 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3365 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3366 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3367 char_offset += cmp_status->nchars; \
3368 cmp_status->state = COMPOSING_NO; \
3371 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3373 #define STORE_COMPOSITION_RULE(rule) \
3376 *charbuf++ = rule; \
3377 cmp_status->length += 2; \
3378 cmp_status->state--; \
3381 /* Store a composed char or a component char C in charbuf, and update
3384 #define STORE_COMPOSITION_CHAR(c) \
3387 cmp_status->length++; \
3388 if (cmp_status->state == COMPOSING_CHAR) \
3389 cmp_status->nchars++; \
3391 cmp_status->ncomps++; \
3392 if (cmp_status->method == COMPOSITION_WITH_RULE \
3393 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3394 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3395 cmp_status->state++; \
3399 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3402 decode_coding_iso_2022 (struct coding_system
*coding
)
3404 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3405 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3406 const unsigned char *src_base
;
3407 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3408 /* We may produce two annotations (charset and composition) in one
3409 loop and one more charset annotation at the end. */
3411 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3412 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
3413 bool multibytep
= coding
->src_multibyte
;
3414 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3415 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3416 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3417 int charset_id_2
, charset_id_3
;
3418 struct charset
*charset
;
3420 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3421 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3422 ptrdiff_t char_offset
= coding
->produced_char
;
3423 ptrdiff_t last_offset
= char_offset
;
3424 int last_id
= charset_ascii
;
3426 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3427 int byte_after_cr
= -1;
3430 setup_iso_safe_charsets (attrs
);
3431 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3433 if (cmp_status
->state
!= COMPOSING_NO
)
3435 if (charbuf_end
- charbuf
< cmp_status
->length
)
3437 for (i
= 0; i
< cmp_status
->length
; i
++)
3438 *charbuf
++ = cmp_status
->carryover
[i
];
3439 coding
->annotated
= 1;
3447 consumed_chars_base
= consumed_chars
;
3449 if (charbuf
>= charbuf_end
)
3451 if (byte_after_cr
>= 0)
3456 if (byte_after_cr
>= 0)
3457 c1
= byte_after_cr
, byte_after_cr
= -1;
3463 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3465 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3467 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3471 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3473 if (c1
== ISO_CODE_ESC
)
3475 if (src
+ 1 >= src_end
)
3476 goto no_more_source
;
3477 *charbuf
++ = ISO_CODE_ESC
;
3479 if (src
[0] == '%' && src
[1] == '@')
3482 consumed_chars
+= 2;
3484 /* We are sure charbuf can contain two more chars. */
3487 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3492 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3498 if ((cmp_status
->state
== COMPOSING_RULE
3499 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3500 && c1
!= ISO_CODE_ESC
)
3504 DECODE_COMPOSITION_RULE (rule
);
3505 STORE_COMPOSITION_RULE (rule
);
3509 /* We produce at most one character. */
3510 switch (iso_code_class
[c1
])
3512 case ISO_0x20_or_0x7F
:
3513 if (charset_id_0
< 0
3514 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3515 /* This is SPACE or DEL. */
3516 charset
= CHARSET_FROM_ID (charset_ascii
);
3518 charset
= CHARSET_FROM_ID (charset_id_0
);
3521 case ISO_graphic_plane_0
:
3522 if (charset_id_0
< 0)
3523 charset
= CHARSET_FROM_ID (charset_ascii
);
3525 charset
= CHARSET_FROM_ID (charset_id_0
);
3528 case ISO_0xA0_or_0xFF
:
3529 if (charset_id_1
< 0
3530 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3531 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3533 /* This is a graphic character, we fall down ... */
3535 case ISO_graphic_plane_1
:
3536 if (charset_id_1
< 0)
3538 charset
= CHARSET_FROM_ID (charset_id_1
);
3542 if (eol_dos
&& c1
== '\r')
3543 ONE_MORE_BYTE (byte_after_cr
);
3544 MAYBE_FINISH_COMPOSITION ();
3545 charset
= CHARSET_FROM_ID (charset_ascii
);
3552 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3553 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3555 CODING_ISO_INVOCATION (coding
, 0) = 1;
3556 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3560 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3562 CODING_ISO_INVOCATION (coding
, 0) = 0;
3563 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3566 case ISO_single_shift_2_7
:
3567 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3569 case ISO_single_shift_2
:
3570 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3572 /* SS2 is handled as an escape sequence of ESC 'N' */
3574 goto label_escape_sequence
;
3576 case ISO_single_shift_3
:
3577 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3579 /* SS2 is handled as an escape sequence of ESC 'O' */
3581 goto label_escape_sequence
;
3583 case ISO_control_sequence_introducer
:
3584 /* CSI is handled as an escape sequence of ESC '[' ... */
3586 goto label_escape_sequence
;
3590 label_escape_sequence
:
3591 /* Escape sequences handled here are invocation,
3592 designation, direction specification, and character
3593 composition specification. */
3596 case '&': /* revision of following character set */
3598 if (!(c1
>= '@' && c1
<= '~'))
3601 if (c1
!= ISO_CODE_ESC
)
3604 goto label_escape_sequence
;
3606 case '$': /* designation of 2-byte character set */
3607 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3613 if (c1
>= '@' && c1
<= 'B')
3614 { /* designation of JISX0208.1978, GB2312.1980,
3616 reg
= 0, chars96
= 0;
3618 else if (c1
>= 0x28 && c1
<= 0x2B)
3619 { /* designation of DIMENSION2_CHARS94 character set */
3620 reg
= c1
- 0x28, chars96
= 0;
3623 else if (c1
>= 0x2C && c1
<= 0x2F)
3624 { /* designation of DIMENSION2_CHARS96 character set */
3625 reg
= c1
- 0x2C, chars96
= 1;
3630 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3631 /* We must update these variables now. */
3633 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3635 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3641 case 'n': /* invocation of locking-shift-2 */
3642 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3643 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3645 CODING_ISO_INVOCATION (coding
, 0) = 2;
3646 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3649 case 'o': /* invocation of locking-shift-3 */
3650 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3651 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3653 CODING_ISO_INVOCATION (coding
, 0) = 3;
3654 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3657 case 'N': /* invocation of single-shift-2 */
3658 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3659 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3661 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3662 if (charset_id_2
< 0)
3663 charset
= CHARSET_FROM_ID (charset_ascii
);
3665 charset
= CHARSET_FROM_ID (charset_id_2
);
3667 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3671 case 'O': /* invocation of single-shift-3 */
3672 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3673 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3675 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3676 if (charset_id_3
< 0)
3677 charset
= CHARSET_FROM_ID (charset_ascii
);
3679 charset
= CHARSET_FROM_ID (charset_id_3
);
3681 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3685 case '0': case '2': case '3': case '4': /* start composition */
3686 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3688 if (last_id
!= charset_ascii
)
3690 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3691 last_id
= charset_ascii
;
3692 last_offset
= char_offset
;
3694 DECODE_COMPOSITION_START (c1
);
3697 case '1': /* end composition */
3698 if (cmp_status
->state
== COMPOSING_NO
)
3700 DECODE_COMPOSITION_END ();
3703 case '[': /* specification of direction */
3704 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3706 /* For the moment, nested direction is not supported.
3707 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3708 left-to-right, and nonzero means right-to-left. */
3712 case ']': /* end of the current direction */
3713 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3715 case '0': /* end of the current direction */
3716 case '1': /* start of left-to-right direction */
3719 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3724 case '2': /* start of right-to-left direction */
3727 coding
->mode
|= CODING_MODE_DIRECTION
;
3741 /* CTEXT extended segment:
3742 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3743 We keep these bytes as is for the moment.
3744 They may be decoded by post-read-conversion. */
3748 ONE_MORE_BYTE (dim
);
3749 if (dim
< '0' || dim
> '4')
3757 size
= ((M
- 128) * 128) + (L
- 128);
3758 if (charbuf
+ 6 > charbuf_end
)
3760 *charbuf
++ = ISO_CODE_ESC
;
3764 *charbuf
++ = BYTE8_TO_CHAR (M
);
3765 *charbuf
++ = BYTE8_TO_CHAR (L
);
3766 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3770 /* XFree86 extension for embedding UTF-8 in CTEXT:
3771 ESC % G --UTF-8-BYTES-- ESC % @
3772 We keep these bytes as is for the moment.
3773 They may be decoded by post-read-conversion. */
3774 if (charbuf
+ 3 > charbuf_end
)
3776 *charbuf
++ = ISO_CODE_ESC
;
3779 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3787 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3792 if (c1
>= 0x28 && c1
<= 0x2B)
3793 { /* designation of DIMENSION1_CHARS94 character set */
3794 reg
= c1
- 0x28, chars96
= 0;
3797 else if (c1
>= 0x2C && c1
<= 0x2F)
3798 { /* designation of DIMENSION1_CHARS96 character set */
3799 reg
= c1
- 0x2C, chars96
= 1;
3804 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3805 /* We must update these variables now. */
3807 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3809 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3821 if (cmp_status
->state
== COMPOSING_NO
3822 && charset
->id
!= charset_ascii
3823 && last_id
!= charset
->id
)
3825 if (last_id
!= charset_ascii
)
3826 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3827 last_id
= charset
->id
;
3828 last_offset
= char_offset
;
3831 /* Now we know CHARSET and 1st position code C1 of a character.
3832 Produce a decoded character while getting 2nd and 3rd
3833 position codes C2, C3 if necessary. */
3834 if (CHARSET_DIMENSION (charset
) > 1)
3837 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3838 || ((c1
& 0x80) != (c2
& 0x80)))
3839 /* C2 is not in a valid range. */
3841 if (CHARSET_DIMENSION (charset
) == 2)
3842 c1
= (c1
<< 8) | c2
;
3846 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3847 || ((c1
& 0x80) != (c3
& 0x80)))
3848 /* C3 is not in a valid range. */
3850 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3854 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3857 MAYBE_FINISH_COMPOSITION ();
3858 for (; src_base
< src
; src_base
++, char_offset
++)
3860 if (ASCII_BYTE_P (*src_base
))
3861 *charbuf
++ = *src_base
;
3863 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3866 else if (cmp_status
->state
== COMPOSING_NO
)
3871 else if ((cmp_status
->state
== COMPOSING_CHAR
3872 ? cmp_status
->nchars
3873 : cmp_status
->ncomps
)
3874 >= MAX_COMPOSITION_COMPONENTS
)
3876 /* Too long composition. */
3877 MAYBE_FINISH_COMPOSITION ();
3882 STORE_COMPOSITION_CHAR (c
);
3886 MAYBE_FINISH_COMPOSITION ();
3888 consumed_chars
= consumed_chars_base
;
3890 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3900 if (cmp_status
->state
!= COMPOSING_NO
)
3902 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3903 MAYBE_FINISH_COMPOSITION ();
3906 charbuf
-= cmp_status
->length
;
3907 for (i
= 0; i
< cmp_status
->length
; i
++)
3908 cmp_status
->carryover
[i
] = charbuf
[i
];
3911 else if (last_id
!= charset_ascii
)
3912 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3913 coding
->consumed_char
+= consumed_chars_base
;
3914 coding
->consumed
= src_base
- coding
->source
;
3915 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3919 /* ISO2022 encoding stuff. */
3922 It is not enough to say just "ISO2022" on encoding, we have to
3923 specify more details. In Emacs, each coding system of ISO2022
3924 variant has the following specifications:
3925 1. Initial designation to G0 thru G3.
3926 2. Allows short-form designation?
3927 3. ASCII should be designated to G0 before control characters?
3928 4. ASCII should be designated to G0 at end of line?
3929 5. 7-bit environment or 8-bit environment?
3930 6. Use locking-shift?
3931 7. Use Single-shift?
3932 And the following two are only for Japanese:
3933 8. Use ASCII in place of JIS0201-1976-Roman?
3934 9. Use JISX0208-1983 in place of JISX0208-1978?
3935 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3936 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3940 /* Produce codes (escape sequence) for designating CHARSET to graphic
3941 register REG at DST, and increment DST. If <final-char> of CHARSET is
3942 '@', 'A', or 'B' and the coding system CODING allows, produce
3943 designation sequence of short-form. */
3945 #define ENCODE_DESIGNATION(charset, reg, coding) \
3947 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3948 const char *intermediate_char_94 = "()*+"; \
3949 const char *intermediate_char_96 = ",-./"; \
3950 int revision = -1; \
3952 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3953 revision = CHARSET_ISO_REVISION (charset); \
3955 if (revision >= 0) \
3957 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3958 EMIT_ONE_BYTE ('@' + revision); \
3960 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3961 if (CHARSET_DIMENSION (charset) == 1) \
3964 if (! CHARSET_ISO_CHARS_96 (charset)) \
3965 b = intermediate_char_94[reg]; \
3967 b = intermediate_char_96[reg]; \
3968 EMIT_ONE_ASCII_BYTE (b); \
3972 EMIT_ONE_ASCII_BYTE ('$'); \
3973 if (! CHARSET_ISO_CHARS_96 (charset)) \
3975 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3977 || final_char < '@' || final_char > 'B') \
3978 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3981 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3983 EMIT_ONE_ASCII_BYTE (final_char); \
3985 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3989 /* The following two macros produce codes (control character or escape
3990 sequence) for ISO2022 single-shift functions (single-shift-2 and
3993 #define ENCODE_SINGLE_SHIFT_2 \
3995 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3996 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3998 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3999 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4003 #define ENCODE_SINGLE_SHIFT_3 \
4005 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4006 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4008 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4009 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4013 /* The following four macros produce codes (control character or
4014 escape sequence) for ISO2022 locking-shift functions (shift-in,
4015 shift-out, locking-shift-2, and locking-shift-3). */
4017 #define ENCODE_SHIFT_IN \
4019 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4020 CODING_ISO_INVOCATION (coding, 0) = 0; \
4024 #define ENCODE_SHIFT_OUT \
4026 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4027 CODING_ISO_INVOCATION (coding, 0) = 1; \
4031 #define ENCODE_LOCKING_SHIFT_2 \
4033 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4034 CODING_ISO_INVOCATION (coding, 0) = 2; \
4038 #define ENCODE_LOCKING_SHIFT_3 \
4040 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4041 CODING_ISO_INVOCATION (coding, 0) = 3; \
4045 /* Produce codes for a DIMENSION1 character whose character set is
4046 CHARSET and whose position-code is C1. Designation and invocation
4047 sequences are also produced in advance if necessary. */
4049 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4051 int id = CHARSET_ID (charset); \
4053 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4054 && id == charset_ascii) \
4056 id = charset_jisx0201_roman; \
4057 charset = CHARSET_FROM_ID (id); \
4060 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4062 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4063 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4065 EMIT_ONE_BYTE (c1 | 0x80); \
4066 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4069 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4071 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4074 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4076 EMIT_ONE_BYTE (c1 | 0x80); \
4080 /* Since CHARSET is not yet invoked to any graphic planes, we \
4081 must invoke it, or, at first, designate it to some graphic \
4082 register. Then repeat the loop to actually produce the \
4084 dst = encode_invocation_designation (charset, coding, dst, \
4089 /* Produce codes for a DIMENSION2 character whose character set is
4090 CHARSET and whose position-codes are C1 and C2. Designation and
4091 invocation codes are also produced in advance if necessary. */
4093 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4095 int id = CHARSET_ID (charset); \
4097 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4098 && id == charset_jisx0208) \
4100 id = charset_jisx0208_1978; \
4101 charset = CHARSET_FROM_ID (id); \
4104 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4106 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4107 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4109 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4110 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4113 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4115 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4118 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4120 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4124 /* Since CHARSET is not yet invoked to any graphic planes, we \
4125 must invoke it, or, at first, designate it to some graphic \
4126 register. Then repeat the loop to actually produce the \
4128 dst = encode_invocation_designation (charset, coding, dst, \
4133 #define ENCODE_ISO_CHARACTER(charset, c) \
4136 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4138 if (CHARSET_DIMENSION (charset) == 1) \
4139 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4141 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4145 /* Produce designation and invocation codes at a place pointed by DST
4146 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4149 static unsigned char *
4150 encode_invocation_designation (struct charset
*charset
,
4151 struct coding_system
*coding
,
4152 unsigned char *dst
, ptrdiff_t *p_nchars
)
4154 bool multibytep
= coding
->dst_multibyte
;
4155 ptrdiff_t produced_chars
= *p_nchars
;
4156 int reg
; /* graphic register number */
4157 int id
= CHARSET_ID (charset
);
4159 /* At first, check designations. */
4160 for (reg
= 0; reg
< 4; reg
++)
4161 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4166 /* CHARSET is not yet designated to any graphic registers. */
4167 /* At first check the requested designation. */
4168 reg
= CODING_ISO_REQUEST (coding
, id
);
4170 /* Since CHARSET requests no special designation, designate it
4171 to graphic register 0. */
4174 ENCODE_DESIGNATION (charset
, reg
, coding
);
4177 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4178 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4180 /* Since the graphic register REG is not invoked to any graphic
4181 planes, invoke it to graphic plane 0. */
4184 case 0: /* graphic register 0 */
4188 case 1: /* graphic register 1 */
4192 case 2: /* graphic register 2 */
4193 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4194 ENCODE_SINGLE_SHIFT_2
;
4196 ENCODE_LOCKING_SHIFT_2
;
4199 case 3: /* graphic register 3 */
4200 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4201 ENCODE_SINGLE_SHIFT_3
;
4203 ENCODE_LOCKING_SHIFT_3
;
4208 *p_nchars
= produced_chars
;
4213 /* Produce codes for designation and invocation to reset the graphic
4214 planes and registers to initial state. */
4215 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4218 struct charset *charset; \
4220 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4222 for (reg = 0; reg < 4; reg++) \
4223 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4224 && (CODING_ISO_DESIGNATION (coding, reg) \
4225 != CODING_ISO_INITIAL (coding, reg))) \
4227 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4228 ENCODE_DESIGNATION (charset, reg, coding); \
4233 /* Produce designation sequences of charsets in the line started from
4234 CHARBUF to a place pointed by DST, and return the number of
4235 produced bytes. DST should not directly point a buffer text area
4236 which may be relocated by char_charset call.
4238 If the current block ends before any end-of-line, we may fail to
4239 find all the necessary designations. */
4242 encode_designation_at_bol (struct coding_system
*coding
,
4243 int *charbuf
, int *charbuf_end
,
4246 unsigned char *orig
= dst
;
4247 struct charset
*charset
;
4248 /* Table of charsets to be designated to each graphic register. */
4250 int c
, found
= 0, reg
;
4251 ptrdiff_t produced_chars
= 0;
4252 bool multibytep
= coding
->dst_multibyte
;
4254 Lisp_Object charset_list
;
4256 attrs
= CODING_ID_ATTRS (coding
->id
);
4257 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4258 if (EQ (charset_list
, Qiso_2022
))
4259 charset_list
= Viso_2022_charset_list
;
4261 for (reg
= 0; reg
< 4; reg
++)
4264 while (charbuf
< charbuf_end
&& found
< 4)
4271 charset
= char_charset (c
, charset_list
, NULL
);
4272 id
= CHARSET_ID (charset
);
4273 reg
= CODING_ISO_REQUEST (coding
, id
);
4274 if (reg
>= 0 && r
[reg
] < 0)
4283 for (reg
= 0; reg
< 4; reg
++)
4285 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4286 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4292 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4295 encode_coding_iso_2022 (struct coding_system
*coding
)
4297 bool multibytep
= coding
->dst_multibyte
;
4298 int *charbuf
= coding
->charbuf
;
4299 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4300 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4301 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4303 bool bol_designation
4304 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4305 && CODING_ISO_BOL (coding
));
4306 ptrdiff_t produced_chars
= 0;
4307 Lisp_Object attrs
, eol_type
, charset_list
;
4308 bool ascii_compatible
;
4310 int preferred_charset_id
= -1;
4312 CODING_GET_INFO (coding
, attrs
, charset_list
);
4313 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4314 if (VECTORP (eol_type
))
4317 setup_iso_safe_charsets (attrs
);
4318 /* Charset list may have been changed. */
4319 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4320 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4323 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4324 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4325 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4327 while (charbuf
< charbuf_end
)
4329 ASSURE_DESTINATION (safe_room
);
4331 if (bol_designation
)
4333 /* We have to produce designation sequences if any now. */
4334 unsigned char desig_buf
[16];
4338 charset_map_loaded
= 0;
4339 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4341 if (charset_map_loaded
4342 && (offset
= coding_change_destination (coding
)))
4347 memcpy (dst
, desig_buf
, nbytes
);
4349 /* We are sure that designation sequences are all ASCII bytes. */
4350 produced_chars
+= nbytes
;
4351 bol_designation
= 0;
4352 ASSURE_DESTINATION (safe_room
);
4359 /* Handle an annotation. */
4362 case CODING_ANNOTATE_COMPOSITION_MASK
:
4363 /* Not yet implemented. */
4365 case CODING_ANNOTATE_CHARSET_MASK
:
4366 preferred_charset_id
= charbuf
[2];
4367 if (preferred_charset_id
>= 0
4368 && NILP (Fmemq (make_number (preferred_charset_id
),
4370 preferred_charset_id
= -1;
4379 /* Now encode the character C. */
4380 if (c
< 0x20 || c
== 0x7F)
4383 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4385 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4386 ENCODE_RESET_PLANE_AND_REGISTER ();
4387 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4391 for (i
= 0; i
< 4; i
++)
4392 CODING_ISO_DESIGNATION (coding
, i
)
4393 = CODING_ISO_INITIAL (coding
, i
);
4395 bol_designation
= ((CODING_ISO_FLAGS (coding
)
4396 & CODING_ISO_FLAG_DESIGNATE_AT_BOL
)
4399 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4400 ENCODE_RESET_PLANE_AND_REGISTER ();
4401 EMIT_ONE_ASCII_BYTE (c
);
4403 else if (ASCII_CHAR_P (c
))
4405 if (ascii_compatible
)
4406 EMIT_ONE_ASCII_BYTE (c
);
4409 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4410 ENCODE_ISO_CHARACTER (charset
, c
);
4413 else if (CHAR_BYTE8_P (c
))
4415 c
= CHAR_TO_BYTE8 (c
);
4420 struct charset
*charset
;
4422 if (preferred_charset_id
>= 0)
4426 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4427 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4429 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4433 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4437 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4439 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4440 charset
= CHARSET_FROM_ID (charset_ascii
);
4444 c
= coding
->default_char
;
4445 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4446 charset_list
, NULL
, charset
);
4449 ENCODE_ISO_CHARACTER (charset
, c
);
4453 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4454 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4456 ASSURE_DESTINATION (safe_room
);
4457 ENCODE_RESET_PLANE_AND_REGISTER ();
4459 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4460 CODING_ISO_BOL (coding
) = bol_designation
;
4461 coding
->produced_char
+= produced_chars
;
4462 coding
->produced
= dst
- coding
->destination
;
4467 /*** 8,9. SJIS and BIG5 handlers ***/
4469 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4470 quite widely. So, for the moment, Emacs supports them in the bare
4471 C code. But, in the future, they may be supported only by CCL. */
4473 /* SJIS is a coding system encoding three character sets: ASCII, right
4474 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4475 as is. A character of charset katakana-jisx0201 is encoded by
4476 "position-code + 0x80". A character of charset japanese-jisx0208
4477 is encoded in 2-byte but two position-codes are divided and shifted
4478 so that it fit in the range below.
4480 --- CODE RANGE of SJIS ---
4481 (character set) (range)
4483 KATAKANA-JISX0201 0xA0 .. 0xDF
4484 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4485 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4486 -------------------------------
4490 /* BIG5 is a coding system encoding two character sets: ASCII and
4491 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4492 character set and is encoded in two-byte.
4494 --- CODE RANGE of BIG5 ---
4495 (character set) (range)
4497 Big5 (1st byte) 0xA1 .. 0xFE
4498 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4499 --------------------------
4503 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4504 Return true if a text is encoded in SJIS. */
4507 detect_coding_sjis (struct coding_system
*coding
,
4508 struct coding_detection_info
*detect_info
)
4510 const unsigned char *src
= coding
->source
, *src_base
;
4511 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4512 bool multibytep
= coding
->src_multibyte
;
4513 ptrdiff_t consumed_chars
= 0;
4516 Lisp_Object attrs
, charset_list
;
4517 int max_first_byte_of_2_byte_code
;
4519 CODING_GET_INFO (coding
, attrs
, charset_list
);
4520 max_first_byte_of_2_byte_code
4521 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4523 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4524 /* A coding system of this category is always ASCII compatible. */
4525 src
+= coding
->head_ascii
;
4533 if ((c
>= 0x81 && c
<= 0x9F)
4534 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4537 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4539 found
= CATEGORY_MASK_SJIS
;
4541 else if (c
>= 0xA0 && c
< 0xE0)
4542 found
= CATEGORY_MASK_SJIS
;
4546 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4550 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4552 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4555 detect_info
->found
|= found
;
4559 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4560 Return true if a text is encoded in BIG5. */
4563 detect_coding_big5 (struct coding_system
*coding
,
4564 struct coding_detection_info
*detect_info
)
4566 const unsigned char *src
= coding
->source
, *src_base
;
4567 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4568 bool multibytep
= coding
->src_multibyte
;
4569 ptrdiff_t consumed_chars
= 0;
4573 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4574 /* A coding system of this category is always ASCII compatible. */
4575 src
+= coding
->head_ascii
;
4586 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4588 found
= CATEGORY_MASK_BIG5
;
4593 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4597 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4599 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4602 detect_info
->found
|= found
;
4606 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4609 decode_coding_sjis (struct coding_system
*coding
)
4611 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4612 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4613 const unsigned char *src_base
;
4614 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4615 /* We may produce one charset annotation in one loop and one more at
4618 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4619 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4620 bool multibytep
= coding
->src_multibyte
;
4621 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4622 struct charset
*charset_kanji2
;
4623 Lisp_Object attrs
, charset_list
, val
;
4624 ptrdiff_t char_offset
= coding
->produced_char
;
4625 ptrdiff_t last_offset
= char_offset
;
4626 int last_id
= charset_ascii
;
4628 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4629 int byte_after_cr
= -1;
4631 CODING_GET_INFO (coding
, attrs
, charset_list
);
4634 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4635 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4636 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4637 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4642 struct charset
*charset
;
4645 consumed_chars_base
= consumed_chars
;
4647 if (charbuf
>= charbuf_end
)
4649 if (byte_after_cr
>= 0)
4654 if (byte_after_cr
>= 0)
4655 c
= byte_after_cr
, byte_after_cr
= -1;
4662 if (eol_dos
&& c
== '\r')
4663 ONE_MORE_BYTE (byte_after_cr
);
4664 charset
= charset_roman
;
4666 else if (c
== 0x80 || c
== 0xA0)
4668 else if (c
>= 0xA1 && c
<= 0xDF)
4670 /* SJIS -> JISX0201-Kana */
4672 charset
= charset_kana
;
4676 /* SJIS -> JISX0208 */
4678 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4682 charset
= charset_kanji
;
4684 else if (c
<= 0xFC && charset_kanji2
)
4686 /* SJIS -> JISX0213-2 */
4688 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4692 charset
= charset_kanji2
;
4696 if (charset
->id
!= charset_ascii
4697 && last_id
!= charset
->id
)
4699 if (last_id
!= charset_ascii
)
4700 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4701 last_id
= charset
->id
;
4702 last_offset
= char_offset
;
4704 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4711 consumed_chars
= consumed_chars_base
;
4713 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4719 if (last_id
!= charset_ascii
)
4720 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4721 coding
->consumed_char
+= consumed_chars_base
;
4722 coding
->consumed
= src_base
- coding
->source
;
4723 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4727 decode_coding_big5 (struct coding_system
*coding
)
4729 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4730 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4731 const unsigned char *src_base
;
4732 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4733 /* We may produce one charset annotation in one loop and one more at
4736 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4737 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4738 bool multibytep
= coding
->src_multibyte
;
4739 struct charset
*charset_roman
, *charset_big5
;
4740 Lisp_Object attrs
, charset_list
, val
;
4741 ptrdiff_t char_offset
= coding
->produced_char
;
4742 ptrdiff_t last_offset
= char_offset
;
4743 int last_id
= charset_ascii
;
4745 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4746 int byte_after_cr
= -1;
4748 CODING_GET_INFO (coding
, attrs
, charset_list
);
4750 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4751 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4756 struct charset
*charset
;
4759 consumed_chars_base
= consumed_chars
;
4761 if (charbuf
>= charbuf_end
)
4763 if (byte_after_cr
>= 0)
4768 if (byte_after_cr
>= 0)
4769 c
= byte_after_cr
, byte_after_cr
= -1;
4777 if (eol_dos
&& c
== '\r')
4778 ONE_MORE_BYTE (byte_after_cr
);
4779 charset
= charset_roman
;
4784 if (c
< 0xA1 || c
> 0xFE)
4787 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4790 charset
= charset_big5
;
4792 if (charset
->id
!= charset_ascii
4793 && last_id
!= charset
->id
)
4795 if (last_id
!= charset_ascii
)
4796 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4797 last_id
= charset
->id
;
4798 last_offset
= char_offset
;
4800 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4807 consumed_chars
= consumed_chars_base
;
4809 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4815 if (last_id
!= charset_ascii
)
4816 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4817 coding
->consumed_char
+= consumed_chars_base
;
4818 coding
->consumed
= src_base
- coding
->source
;
4819 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4822 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4823 This function can encode charsets `ascii', `katakana-jisx0201',
4824 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4825 are sure that all these charsets are registered as official charset
4826 (i.e. do not have extended leading-codes). Characters of other
4827 charsets are produced without any encoding. */
4830 encode_coding_sjis (struct coding_system
*coding
)
4832 bool multibytep
= coding
->dst_multibyte
;
4833 int *charbuf
= coding
->charbuf
;
4834 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4835 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4836 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4838 ptrdiff_t produced_chars
= 0;
4839 Lisp_Object attrs
, charset_list
, val
;
4840 bool ascii_compatible
;
4841 struct charset
*charset_kanji
, *charset_kana
;
4842 struct charset
*charset_kanji2
;
4845 CODING_GET_INFO (coding
, attrs
, charset_list
);
4846 val
= XCDR (charset_list
);
4847 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4848 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4849 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4851 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4853 while (charbuf
< charbuf_end
)
4855 ASSURE_DESTINATION (safe_room
);
4857 /* Now encode the character C. */
4858 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4859 EMIT_ONE_ASCII_BYTE (c
);
4860 else if (CHAR_BYTE8_P (c
))
4862 c
= CHAR_TO_BYTE8 (c
);
4868 struct charset
*charset
;
4869 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4874 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4876 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4877 charset
= CHARSET_FROM_ID (charset_ascii
);
4881 c
= coding
->default_char
;
4882 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4883 charset_list
, &code
, charset
);
4886 if (code
== CHARSET_INVALID_CODE (charset
))
4888 if (charset
== charset_kanji
)
4892 c1
= code
>> 8, c2
= code
& 0xFF;
4893 EMIT_TWO_BYTES (c1
, c2
);
4895 else if (charset
== charset_kana
)
4896 EMIT_ONE_BYTE (code
| 0x80);
4897 else if (charset_kanji2
&& charset
== charset_kanji2
)
4902 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4904 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4906 JIS_TO_SJIS2 (code
);
4907 c1
= code
>> 8, c2
= code
& 0xFF;
4908 EMIT_TWO_BYTES (c1
, c2
);
4911 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4914 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4917 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4918 coding
->produced_char
+= produced_chars
;
4919 coding
->produced
= dst
- coding
->destination
;
4924 encode_coding_big5 (struct coding_system
*coding
)
4926 bool multibytep
= coding
->dst_multibyte
;
4927 int *charbuf
= coding
->charbuf
;
4928 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4929 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4930 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4932 ptrdiff_t produced_chars
= 0;
4933 Lisp_Object attrs
, charset_list
, val
;
4934 bool ascii_compatible
;
4935 struct charset
*charset_big5
;
4938 CODING_GET_INFO (coding
, attrs
, charset_list
);
4939 val
= XCDR (charset_list
);
4940 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4941 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4943 while (charbuf
< charbuf_end
)
4945 ASSURE_DESTINATION (safe_room
);
4947 /* Now encode the character C. */
4948 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4949 EMIT_ONE_ASCII_BYTE (c
);
4950 else if (CHAR_BYTE8_P (c
))
4952 c
= CHAR_TO_BYTE8 (c
);
4958 struct charset
*charset
;
4959 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4964 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4966 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4967 charset
= CHARSET_FROM_ID (charset_ascii
);
4971 c
= coding
->default_char
;
4972 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4973 charset_list
, &code
, charset
);
4976 if (code
== CHARSET_INVALID_CODE (charset
))
4978 if (charset
== charset_big5
)
4982 c1
= code
>> 8, c2
= code
& 0xFF;
4983 EMIT_TWO_BYTES (c1
, c2
);
4986 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4989 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4990 coding
->produced_char
+= produced_chars
;
4991 coding
->produced
= dst
- coding
->destination
;
4996 /*** 10. CCL handlers ***/
4998 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4999 Return true if a text is encoded in a coding system of which
5000 encoder/decoder are written in CCL program. */
5003 detect_coding_ccl (struct coding_system
*coding
,
5004 struct coding_detection_info
*detect_info
)
5006 const unsigned char *src
= coding
->source
, *src_base
;
5007 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5008 bool multibytep
= coding
->src_multibyte
;
5009 ptrdiff_t consumed_chars
= 0;
5011 unsigned char *valids
;
5012 ptrdiff_t head_ascii
= coding
->head_ascii
;
5015 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5017 coding
= &coding_categories
[coding_category_ccl
];
5018 valids
= CODING_CCL_VALIDS (coding
);
5019 attrs
= CODING_ID_ATTRS (coding
->id
);
5020 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5029 if (c
< 0 || ! valids
[c
])
5031 if ((valids
[c
] > 1))
5032 found
= CATEGORY_MASK_CCL
;
5034 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5038 detect_info
->found
|= found
;
5043 decode_coding_ccl (struct coding_system
*coding
)
5045 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5046 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5047 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5048 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5049 ptrdiff_t consumed_chars
= 0;
5050 bool multibytep
= coding
->src_multibyte
;
5051 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5052 int source_charbuf
[1024];
5053 int source_byteidx
[1025];
5054 Lisp_Object attrs
, charset_list
;
5056 CODING_GET_INFO (coding
, attrs
, charset_list
);
5060 const unsigned char *p
= src
;
5066 while (i
< 1024 && p
< src_end
)
5068 source_byteidx
[i
] = p
- src
;
5069 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5071 source_byteidx
[i
] = p
- src
;
5074 while (i
< 1024 && p
< src_end
)
5075 source_charbuf
[i
++] = *p
++;
5077 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5078 ccl
->last_block
= 1;
5079 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5080 charset_map_loaded
= 0;
5081 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5083 if (charset_map_loaded
5084 && (offset
= coding_change_source (coding
)))
5090 charbuf
+= ccl
->produced
;
5092 src
+= source_byteidx
[ccl
->consumed
];
5094 src
+= ccl
->consumed
;
5095 consumed_chars
+= ccl
->consumed
;
5096 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5100 switch (ccl
->status
)
5102 case CCL_STAT_SUSPEND_BY_SRC
:
5103 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5105 case CCL_STAT_SUSPEND_BY_DST
:
5106 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5109 case CCL_STAT_INVALID_CMD
:
5110 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5113 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5116 coding
->consumed_char
+= consumed_chars
;
5117 coding
->consumed
= src
- coding
->source
;
5118 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5122 encode_coding_ccl (struct coding_system
*coding
)
5124 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5125 bool multibytep
= coding
->dst_multibyte
;
5126 int *charbuf
= coding
->charbuf
;
5127 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5128 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5129 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5130 int destination_charbuf
[1024];
5131 ptrdiff_t produced_chars
= 0;
5133 Lisp_Object attrs
, charset_list
;
5135 CODING_GET_INFO (coding
, attrs
, charset_list
);
5136 if (coding
->consumed_char
== coding
->src_chars
5137 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5138 ccl
->last_block
= 1;
5144 /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */
5145 charset_map_loaded
= 0;
5146 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5147 charbuf_end
- charbuf
, 1024, charset_list
);
5148 if (charset_map_loaded
5149 && (offset
= coding_change_destination (coding
)))
5153 ASSURE_DESTINATION (ccl
->produced
* 2);
5154 for (i
= 0; i
< ccl
->produced
; i
++)
5155 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5159 ASSURE_DESTINATION (ccl
->produced
);
5160 for (i
= 0; i
< ccl
->produced
; i
++)
5161 *dst
++ = destination_charbuf
[i
] & 0xFF;
5162 produced_chars
+= ccl
->produced
;
5164 charbuf
+= ccl
->consumed
;
5165 if (ccl
->status
== CCL_STAT_QUIT
5166 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5169 while (charbuf
< charbuf_end
);
5171 switch (ccl
->status
)
5173 case CCL_STAT_SUSPEND_BY_SRC
:
5174 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5176 case CCL_STAT_SUSPEND_BY_DST
:
5177 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5180 case CCL_STAT_INVALID_CMD
:
5181 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5184 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5188 coding
->produced_char
+= produced_chars
;
5189 coding
->produced
= dst
- coding
->destination
;
5194 /*** 10, 11. no-conversion handlers ***/
5196 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5199 decode_coding_raw_text (struct coding_system
*coding
)
5202 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5204 coding
->chars_at_source
= 1;
5205 coding
->consumed_char
= coding
->src_chars
;
5206 coding
->consumed
= coding
->src_bytes
;
5207 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5209 coding
->consumed_char
--;
5211 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5214 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5218 encode_coding_raw_text (struct coding_system
*coding
)
5220 bool multibytep
= coding
->dst_multibyte
;
5221 int *charbuf
= coding
->charbuf
;
5222 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5223 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5224 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5225 ptrdiff_t produced_chars
= 0;
5230 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5232 if (coding
->src_multibyte
)
5233 while (charbuf
< charbuf_end
)
5235 ASSURE_DESTINATION (safe_room
);
5237 if (ASCII_CHAR_P (c
))
5238 EMIT_ONE_ASCII_BYTE (c
);
5239 else if (CHAR_BYTE8_P (c
))
5241 c
= CHAR_TO_BYTE8 (c
);
5246 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5248 CHAR_STRING_ADVANCE (c
, p1
);
5251 EMIT_ONE_BYTE (*p0
);
5258 while (charbuf
< charbuf_end
)
5260 ASSURE_DESTINATION (safe_room
);
5267 if (coding
->src_multibyte
)
5269 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5271 while (charbuf
< charbuf_end
)
5273 ASSURE_DESTINATION (safe_room
);
5275 if (ASCII_CHAR_P (c
))
5277 else if (CHAR_BYTE8_P (c
))
5278 *dst
++ = CHAR_TO_BYTE8 (c
);
5280 CHAR_STRING_ADVANCE (c
, dst
);
5285 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5286 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5287 *dst
++ = *charbuf
++;
5289 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5291 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5292 coding
->produced_char
+= produced_chars
;
5293 coding
->produced
= dst
- coding
->destination
;
5297 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5298 Return true if a text is encoded in a charset-based coding system. */
5301 detect_coding_charset (struct coding_system
*coding
,
5302 struct coding_detection_info
*detect_info
)
5304 const unsigned char *src
= coding
->source
, *src_base
;
5305 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5306 bool multibytep
= coding
->src_multibyte
;
5307 ptrdiff_t consumed_chars
= 0;
5308 Lisp_Object attrs
, valids
, name
;
5310 ptrdiff_t head_ascii
= coding
->head_ascii
;
5311 bool check_latin_extra
= 0;
5313 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5315 coding
= &coding_categories
[coding_category_charset
];
5316 attrs
= CODING_ID_ATTRS (coding
->id
);
5317 valids
= AREF (attrs
, coding_attr_charset_valids
);
5318 name
= CODING_ID_NAME (coding
->id
);
5319 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5320 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5321 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5322 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5323 check_latin_extra
= 1;
5325 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5332 struct charset
*charset
;
5339 val
= AREF (valids
, c
);
5345 && check_latin_extra
5346 && (!VECTORP (Vlatin_extra_code_table
)
5347 || NILP (AREF (Vlatin_extra_code_table
, c
))))
5349 found
= CATEGORY_MASK_CHARSET
;
5353 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5354 dim
= CHARSET_DIMENSION (charset
);
5355 for (idx
= 1; idx
< dim
; idx
++)
5360 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5361 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5370 for (; CONSP (val
); val
= XCDR (val
))
5372 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5373 dim
= CHARSET_DIMENSION (charset
);
5379 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5380 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5395 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5399 detect_info
->found
|= found
;
5404 decode_coding_charset (struct coding_system
*coding
)
5406 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5407 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5408 const unsigned char *src_base
;
5409 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5410 /* We may produce one charset annotation in one loop and one more at
5413 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5414 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
5415 bool multibytep
= coding
->src_multibyte
;
5416 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5418 ptrdiff_t char_offset
= coding
->produced_char
;
5419 ptrdiff_t last_offset
= char_offset
;
5420 int last_id
= charset_ascii
;
5422 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5423 int byte_after_cr
= -1;
5425 valids
= AREF (attrs
, coding_attr_charset_valids
);
5431 struct charset
*charset
;
5437 consumed_chars_base
= consumed_chars
;
5439 if (charbuf
>= charbuf_end
)
5441 if (byte_after_cr
>= 0)
5446 if (byte_after_cr
>= 0)
5454 if (eol_dos
&& c
== '\r')
5455 ONE_MORE_BYTE (byte_after_cr
);
5461 val
= AREF (valids
, c
);
5462 if (! INTEGERP (val
) && ! CONSP (val
))
5466 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5467 dim
= CHARSET_DIMENSION (charset
);
5471 code
= (code
<< 8) | c
;
5474 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5479 /* VAL is a list of charset IDs. It is assured that the
5480 list is sorted by charset dimensions (smaller one
5484 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5485 dim
= CHARSET_DIMENSION (charset
);
5489 code
= (code
<< 8) | c
;
5492 CODING_DECODE_CHAR (coding
, src
, src_base
,
5493 src_end
, charset
, code
, c
);
5501 if (charset
->id
!= charset_ascii
5502 && last_id
!= charset
->id
)
5504 if (last_id
!= charset_ascii
)
5505 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5506 last_id
= charset
->id
;
5507 last_offset
= char_offset
;
5516 consumed_chars
= consumed_chars_base
;
5518 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5524 if (last_id
!= charset_ascii
)
5525 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5526 coding
->consumed_char
+= consumed_chars_base
;
5527 coding
->consumed
= src_base
- coding
->source
;
5528 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5532 encode_coding_charset (struct coding_system
*coding
)
5534 bool multibytep
= coding
->dst_multibyte
;
5535 int *charbuf
= coding
->charbuf
;
5536 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5537 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5538 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5539 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5540 ptrdiff_t produced_chars
= 0;
5541 Lisp_Object attrs
, charset_list
;
5542 bool ascii_compatible
;
5545 CODING_GET_INFO (coding
, attrs
, charset_list
);
5546 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5548 while (charbuf
< charbuf_end
)
5550 struct charset
*charset
;
5553 ASSURE_DESTINATION (safe_room
);
5555 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5556 EMIT_ONE_ASCII_BYTE (c
);
5557 else if (CHAR_BYTE8_P (c
))
5559 c
= CHAR_TO_BYTE8 (c
);
5564 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5569 if (CHARSET_DIMENSION (charset
) == 1)
5570 EMIT_ONE_BYTE (code
);
5571 else if (CHARSET_DIMENSION (charset
) == 2)
5572 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5573 else if (CHARSET_DIMENSION (charset
) == 3)
5574 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5576 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5577 (code
>> 8) & 0xFF, code
& 0xFF);
5581 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5582 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5584 c
= coding
->default_char
;
5590 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5591 coding
->produced_char
+= produced_chars
;
5592 coding
->produced
= dst
- coding
->destination
;
5597 /*** 7. C library functions ***/
5599 /* Setup coding context CODING from information about CODING_SYSTEM.
5600 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5601 CODING_SYSTEM is invalid, signal an error. */
5604 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5607 Lisp_Object eol_type
;
5608 Lisp_Object coding_type
;
5611 if (NILP (coding_system
))
5612 coding_system
= Qundecided
;
5614 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5616 attrs
= CODING_ID_ATTRS (coding
->id
);
5617 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5620 coding
->head_ascii
= -1;
5621 if (VECTORP (eol_type
))
5622 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5623 | CODING_REQUIRE_DETECTION_MASK
);
5624 else if (! EQ (eol_type
, Qunix
))
5625 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5626 | CODING_REQUIRE_ENCODING_MASK
);
5628 coding
->common_flags
= 0;
5629 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5630 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5631 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5632 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5633 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5634 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5636 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5637 coding
->max_charset_id
= SCHARS (val
) - 1;
5638 coding
->safe_charsets
= SDATA (val
);
5639 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5640 coding
->carryover_bytes
= 0;
5642 coding_type
= CODING_ATTR_TYPE (attrs
);
5643 if (EQ (coding_type
, Qundecided
))
5645 coding
->detector
= NULL
;
5646 coding
->decoder
= decode_coding_raw_text
;
5647 coding
->encoder
= encode_coding_raw_text
;
5648 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5650 else if (EQ (coding_type
, Qiso_2022
))
5653 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5655 /* Invoke graphic register 0 to plane 0. */
5656 CODING_ISO_INVOCATION (coding
, 0) = 0;
5657 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5658 CODING_ISO_INVOCATION (coding
, 1)
5659 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5660 /* Setup the initial status of designation. */
5661 for (i
= 0; i
< 4; i
++)
5662 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5663 /* Not single shifting initially. */
5664 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5665 /* Beginning of buffer should also be regarded as bol. */
5666 CODING_ISO_BOL (coding
) = 1;
5667 coding
->detector
= detect_coding_iso_2022
;
5668 coding
->decoder
= decode_coding_iso_2022
;
5669 coding
->encoder
= encode_coding_iso_2022
;
5670 if (flags
& CODING_ISO_FLAG_SAFE
)
5671 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5672 coding
->common_flags
5673 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5674 | CODING_REQUIRE_FLUSHING_MASK
);
5675 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5676 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5677 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5678 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5679 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5681 setup_iso_safe_charsets (attrs
);
5682 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5683 coding
->max_charset_id
= SCHARS (val
) - 1;
5684 coding
->safe_charsets
= SDATA (val
);
5686 CODING_ISO_FLAGS (coding
) = flags
;
5687 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5688 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5689 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5690 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5692 else if (EQ (coding_type
, Qcharset
))
5694 coding
->detector
= detect_coding_charset
;
5695 coding
->decoder
= decode_coding_charset
;
5696 coding
->encoder
= encode_coding_charset
;
5697 coding
->common_flags
5698 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5700 else if (EQ (coding_type
, Qutf_8
))
5702 val
= AREF (attrs
, coding_attr_utf_bom
);
5703 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5704 : EQ (val
, Qt
) ? utf_with_bom
5706 coding
->detector
= detect_coding_utf_8
;
5707 coding
->decoder
= decode_coding_utf_8
;
5708 coding
->encoder
= encode_coding_utf_8
;
5709 coding
->common_flags
5710 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5711 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5712 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5714 else if (EQ (coding_type
, Qutf_16
))
5716 val
= AREF (attrs
, coding_attr_utf_bom
);
5717 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5718 : EQ (val
, Qt
) ? utf_with_bom
5720 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5721 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5722 : utf_16_little_endian
);
5723 CODING_UTF_16_SURROGATE (coding
) = 0;
5724 coding
->detector
= detect_coding_utf_16
;
5725 coding
->decoder
= decode_coding_utf_16
;
5726 coding
->encoder
= encode_coding_utf_16
;
5727 coding
->common_flags
5728 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5729 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5730 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5732 else if (EQ (coding_type
, Qccl
))
5734 coding
->detector
= detect_coding_ccl
;
5735 coding
->decoder
= decode_coding_ccl
;
5736 coding
->encoder
= encode_coding_ccl
;
5737 coding
->common_flags
5738 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5739 | CODING_REQUIRE_FLUSHING_MASK
);
5741 else if (EQ (coding_type
, Qemacs_mule
))
5743 coding
->detector
= detect_coding_emacs_mule
;
5744 coding
->decoder
= decode_coding_emacs_mule
;
5745 coding
->encoder
= encode_coding_emacs_mule
;
5746 coding
->common_flags
5747 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5748 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5749 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5751 Lisp_Object tail
, safe_charsets
;
5752 int max_charset_id
= 0;
5754 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5756 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5757 max_charset_id
= XFASTINT (XCAR (tail
));
5758 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5759 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5760 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5762 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5763 coding
->max_charset_id
= max_charset_id
;
5764 coding
->safe_charsets
= SDATA (safe_charsets
);
5766 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5767 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5769 else if (EQ (coding_type
, Qshift_jis
))
5771 coding
->detector
= detect_coding_sjis
;
5772 coding
->decoder
= decode_coding_sjis
;
5773 coding
->encoder
= encode_coding_sjis
;
5774 coding
->common_flags
5775 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5777 else if (EQ (coding_type
, Qbig5
))
5779 coding
->detector
= detect_coding_big5
;
5780 coding
->decoder
= decode_coding_big5
;
5781 coding
->encoder
= encode_coding_big5
;
5782 coding
->common_flags
5783 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5785 else /* EQ (coding_type, Qraw_text) */
5787 coding
->detector
= NULL
;
5788 coding
->decoder
= decode_coding_raw_text
;
5789 coding
->encoder
= encode_coding_raw_text
;
5790 if (! EQ (eol_type
, Qunix
))
5792 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5793 if (! VECTORP (eol_type
))
5794 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5802 /* Return a list of charsets supported by CODING. */
5805 coding_charset_list (struct coding_system
*coding
)
5807 Lisp_Object attrs
, charset_list
;
5809 CODING_GET_INFO (coding
, attrs
, charset_list
);
5810 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5812 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5814 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5815 charset_list
= Viso_2022_charset_list
;
5817 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5819 charset_list
= Vemacs_mule_charset_list
;
5821 return charset_list
;
5825 /* Return a list of charsets supported by CODING-SYSTEM. */
5828 coding_system_charset_list (Lisp_Object coding_system
)
5831 Lisp_Object attrs
, charset_list
;
5833 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5834 attrs
= CODING_ID_ATTRS (id
);
5836 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5838 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5840 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5841 charset_list
= Viso_2022_charset_list
;
5843 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5845 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5847 charset_list
= Vemacs_mule_charset_list
;
5851 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5853 return charset_list
;
5857 /* Return raw-text or one of its subsidiaries that has the same
5858 eol_type as CODING-SYSTEM. */
5861 raw_text_coding_system (Lisp_Object coding_system
)
5863 Lisp_Object spec
, attrs
;
5864 Lisp_Object eol_type
, raw_text_eol_type
;
5866 if (NILP (coding_system
))
5868 spec
= CODING_SYSTEM_SPEC (coding_system
);
5869 attrs
= AREF (spec
, 0);
5871 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5872 return coding_system
;
5874 eol_type
= AREF (spec
, 2);
5875 if (VECTORP (eol_type
))
5877 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5878 raw_text_eol_type
= AREF (spec
, 2);
5879 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5880 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5881 : AREF (raw_text_eol_type
, 2));
5885 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5886 the subsidiary that has the same eol-spec as PARENT (if it is not
5887 nil and specifies end-of-line format) or the system's setting
5888 (system_eol_type). */
5891 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5893 Lisp_Object spec
, eol_type
;
5895 if (NILP (coding_system
))
5896 coding_system
= Qraw_text
;
5897 spec
= CODING_SYSTEM_SPEC (coding_system
);
5898 eol_type
= AREF (spec
, 2);
5899 if (VECTORP (eol_type
))
5901 Lisp_Object parent_eol_type
;
5903 if (! NILP (parent
))
5905 Lisp_Object parent_spec
;
5907 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5908 parent_eol_type
= AREF (parent_spec
, 2);
5909 if (VECTORP (parent_eol_type
))
5910 parent_eol_type
= system_eol_type
;
5913 parent_eol_type
= system_eol_type
;
5914 if (EQ (parent_eol_type
, Qunix
))
5915 coding_system
= AREF (eol_type
, 0);
5916 else if (EQ (parent_eol_type
, Qdos
))
5917 coding_system
= AREF (eol_type
, 1);
5918 else if (EQ (parent_eol_type
, Qmac
))
5919 coding_system
= AREF (eol_type
, 2);
5921 return coding_system
;
5925 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5926 decided for writing to a process. If not, complement them, and
5927 return a new coding system. */
5930 complement_process_encoding_system (Lisp_Object coding_system
)
5932 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5933 Lisp_Object spec
, attrs
;
5936 for (i
= 0; i
< 3; i
++)
5939 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
5941 coding_system
= preferred_coding_system ();
5942 spec
= CODING_SYSTEM_SPEC (coding_system
);
5945 attrs
= AREF (spec
, 0);
5946 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
5947 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
5948 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
5949 eol_base
= coding_system
;
5950 if (! NILP (coding_base
) && ! NILP (eol_base
))
5955 /* The original CODING_SYSTEM didn't specify text-conversion or
5956 eol-conversion. Be sure that we return a fully complemented
5958 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
5959 return coding_system
;
5963 /* Emacs has a mechanism to automatically detect a coding system if it
5964 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5965 it's impossible to distinguish some coding systems accurately
5966 because they use the same range of codes. So, at first, coding
5967 systems are categorized into 7, those are:
5969 o coding-category-emacs-mule
5971 The category for a coding system which has the same code range
5972 as Emacs' internal format. Assigned the coding-system (Lisp
5973 symbol) `emacs-mule' by default.
5975 o coding-category-sjis
5977 The category for a coding system which has the same code range
5978 as SJIS. Assigned the coding-system (Lisp
5979 symbol) `japanese-shift-jis' by default.
5981 o coding-category-iso-7
5983 The category for a coding system which has the same code range
5984 as ISO2022 of 7-bit environment. This doesn't use any locking
5985 shift and single shift functions. This can encode/decode all
5986 charsets. Assigned the coding-system (Lisp symbol)
5987 `iso-2022-7bit' by default.
5989 o coding-category-iso-7-tight
5991 Same as coding-category-iso-7 except that this can
5992 encode/decode only the specified charsets.
5994 o coding-category-iso-8-1
5996 The category for a coding system which has the same code range
5997 as ISO2022 of 8-bit environment and graphic plane 1 used only
5998 for DIMENSION1 charset. This doesn't use any locking shift
5999 and single shift functions. Assigned the coding-system (Lisp
6000 symbol) `iso-latin-1' by default.
6002 o coding-category-iso-8-2
6004 The category for a coding system which has the same code range
6005 as ISO2022 of 8-bit environment and graphic plane 1 used only
6006 for DIMENSION2 charset. This doesn't use any locking shift
6007 and single shift functions. Assigned the coding-system (Lisp
6008 symbol) `japanese-iso-8bit' by default.
6010 o coding-category-iso-7-else
6012 The category for a coding system which has the same code range
6013 as ISO2022 of 7-bit environment but uses locking shift or
6014 single shift functions. Assigned the coding-system (Lisp
6015 symbol) `iso-2022-7bit-lock' by default.
6017 o coding-category-iso-8-else
6019 The category for a coding system which has the same code range
6020 as ISO2022 of 8-bit environment but uses locking shift or
6021 single shift functions. Assigned the coding-system (Lisp
6022 symbol) `iso-2022-8bit-ss2' by default.
6024 o coding-category-big5
6026 The category for a coding system which has the same code range
6027 as BIG5. Assigned the coding-system (Lisp symbol)
6028 `cn-big5' by default.
6030 o coding-category-utf-8
6032 The category for a coding system which has the same code range
6033 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6034 symbol) `utf-8' by default.
6036 o coding-category-utf-16-be
6038 The category for a coding system in which a text has an
6039 Unicode signature (cf. Unicode Standard) in the order of BIG
6040 endian at the head. Assigned the coding-system (Lisp symbol)
6041 `utf-16-be' by default.
6043 o coding-category-utf-16-le
6045 The category for a coding system in which a text has an
6046 Unicode signature (cf. Unicode Standard) in the order of
6047 LITTLE endian at the head. Assigned the coding-system (Lisp
6048 symbol) `utf-16-le' by default.
6050 o coding-category-ccl
6052 The category for a coding system of which encoder/decoder is
6053 written in CCL programs. The default value is nil, i.e., no
6054 coding system is assigned.
6056 o coding-category-binary
6058 The category for a coding system not categorized in any of the
6059 above. Assigned the coding-system (Lisp symbol)
6060 `no-conversion' by default.
6062 Each of them is a Lisp symbol and the value is an actual
6063 `coding-system's (this is also a Lisp symbol) assigned by a user.
6064 What Emacs does actually is to detect a category of coding system.
6065 Then, it uses a `coding-system' assigned to it. If Emacs can't
6066 decide only one possible category, it selects a category of the
6067 highest priority. Priorities of categories are also specified by a
6068 user in a Lisp variable `coding-category-list'.
6072 #define EOL_SEEN_NONE 0
6073 #define EOL_SEEN_LF 1
6074 #define EOL_SEEN_CR 2
6075 #define EOL_SEEN_CRLF 4
6077 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6078 SOURCE is encoded. If CATEGORY is one of
6079 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6080 two-byte, else they are encoded by one-byte.
6082 Return one of EOL_SEEN_XXX. */
6084 #define MAX_EOL_CHECK_COUNT 3
6087 detect_eol (const unsigned char *source
, ptrdiff_t src_bytes
,
6088 enum coding_category category
)
6090 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6093 int eol_seen
= EOL_SEEN_NONE
;
6095 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6097 bool msb
= category
== (coding_category_utf_16_le
6098 | coding_category_utf_16_le_nosig
);
6101 while (src
+ 1 < src_end
)
6104 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6109 this_eol
= EOL_SEEN_LF
;
6110 else if (src
+ 3 >= src_end
6111 || src
[msb
+ 2] != 0
6112 || src
[lsb
+ 2] != '\n')
6113 this_eol
= EOL_SEEN_CR
;
6116 this_eol
= EOL_SEEN_CRLF
;
6120 if (eol_seen
== EOL_SEEN_NONE
)
6121 /* This is the first end-of-line. */
6122 eol_seen
= this_eol
;
6123 else if (eol_seen
!= this_eol
)
6125 /* The found type is different from what found before.
6126 Allow for stray ^M characters in DOS EOL files. */
6127 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6128 || (eol_seen
== EOL_SEEN_CRLF
6129 && this_eol
== EOL_SEEN_CR
))
6130 eol_seen
= EOL_SEEN_CRLF
;
6133 eol_seen
= EOL_SEEN_LF
;
6137 if (++total
== MAX_EOL_CHECK_COUNT
)
6144 while (src
< src_end
)
6147 if (c
== '\n' || c
== '\r')
6152 this_eol
= EOL_SEEN_LF
;
6153 else if (src
>= src_end
|| *src
!= '\n')
6154 this_eol
= EOL_SEEN_CR
;
6156 this_eol
= EOL_SEEN_CRLF
, src
++;
6158 if (eol_seen
== EOL_SEEN_NONE
)
6159 /* This is the first end-of-line. */
6160 eol_seen
= this_eol
;
6161 else if (eol_seen
!= this_eol
)
6163 /* The found type is different from what found before.
6164 Allow for stray ^M characters in DOS EOL files. */
6165 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6166 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6167 eol_seen
= EOL_SEEN_CRLF
;
6170 eol_seen
= EOL_SEEN_LF
;
6174 if (++total
== MAX_EOL_CHECK_COUNT
)
6183 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6185 Lisp_Object eol_type
;
6187 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6188 if (eol_seen
& EOL_SEEN_LF
)
6190 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6193 else if (eol_seen
& EOL_SEEN_CRLF
)
6195 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6198 else if (eol_seen
& EOL_SEEN_CR
)
6200 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6206 /* Detect how a text specified in CODING is encoded. If a coding
6207 system is detected, update fields of CODING by the detected coding
6211 detect_coding (struct coding_system
*coding
)
6213 const unsigned char *src
, *src_end
;
6214 unsigned int saved_mode
= coding
->mode
;
6216 coding
->consumed
= coding
->consumed_char
= 0;
6217 coding
->produced
= coding
->produced_char
= 0;
6218 coding_set_source (coding
);
6220 src_end
= coding
->source
+ coding
->src_bytes
;
6221 coding
->head_ascii
= 0;
6223 /* If we have not yet decided the text encoding type, detect it
6225 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6228 struct coding_detection_info detect_info
;
6229 bool null_byte_found
= 0, eight_bit_found
= 0;
6231 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6232 for (src
= coding
->source
; src
< src_end
; src
++)
6237 eight_bit_found
= 1;
6238 if (null_byte_found
)
6243 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6244 && ! inhibit_iso_escape_detection
6245 && ! detect_info
.checked
)
6247 if (detect_coding_iso_2022 (coding
, &detect_info
))
6249 /* We have scanned the whole data. */
6250 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6252 /* We didn't find an 8-bit code. We may
6253 have found a null-byte, but it's very
6254 rare that a binary file conforms to
6257 coding
->head_ascii
= src
- coding
->source
;
6259 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6263 else if (! c
&& !inhibit_null_byte_detection
)
6265 null_byte_found
= 1;
6266 if (eight_bit_found
)
6269 if (! eight_bit_found
)
6270 coding
->head_ascii
++;
6272 else if (! eight_bit_found
)
6273 coding
->head_ascii
++;
6276 if (null_byte_found
|| eight_bit_found
6277 || coding
->head_ascii
< coding
->src_bytes
6278 || detect_info
.found
)
6280 enum coding_category category
;
6281 struct coding_system
*this;
6283 if (coding
->head_ascii
== coding
->src_bytes
)
6284 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6285 for (i
= 0; i
< coding_category_raw_text
; i
++)
6287 category
= coding_priorities
[i
];
6288 this = coding_categories
+ category
;
6289 if (detect_info
.found
& (1 << category
))
6294 if (null_byte_found
)
6296 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6297 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6299 for (i
= 0; i
< coding_category_raw_text
; i
++)
6301 category
= coding_priorities
[i
];
6302 this = coding_categories
+ category
;
6303 /* Some of this->detector (e.g. detect_coding_sjis)
6304 require this information. */
6305 coding
->id
= this->id
;
6308 /* No coding system of this category is defined. */
6309 detect_info
.rejected
|= (1 << category
);
6311 else if (category
>= coding_category_raw_text
)
6313 else if (detect_info
.checked
& (1 << category
))
6315 if (detect_info
.found
& (1 << category
))
6318 else if ((*(this->detector
)) (coding
, &detect_info
)
6319 && detect_info
.found
& (1 << category
))
6321 if (category
== coding_category_utf_16_auto
)
6323 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6324 category
= coding_category_utf_16_le
;
6326 category
= coding_category_utf_16_be
;
6333 if (i
< coding_category_raw_text
)
6334 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6335 else if (null_byte_found
)
6336 setup_coding_system (Qno_conversion
, coding
);
6337 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6338 == CATEGORY_MASK_ANY
)
6339 setup_coding_system (Qraw_text
, coding
);
6340 else if (detect_info
.rejected
)
6341 for (i
= 0; i
< coding_category_raw_text
; i
++)
6342 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6344 this = coding_categories
+ coding_priorities
[i
];
6345 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6350 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6351 == coding_category_utf_8_auto
)
6353 Lisp_Object coding_systems
;
6354 struct coding_detection_info detect_info
;
6357 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6358 detect_info
.found
= detect_info
.rejected
= 0;
6359 coding
->head_ascii
= 0;
6360 if (CONSP (coding_systems
)
6361 && detect_coding_utf_8 (coding
, &detect_info
))
6363 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6364 setup_coding_system (XCAR (coding_systems
), coding
);
6366 setup_coding_system (XCDR (coding_systems
), coding
);
6369 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6370 == coding_category_utf_16_auto
)
6372 Lisp_Object coding_systems
;
6373 struct coding_detection_info detect_info
;
6376 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6377 detect_info
.found
= detect_info
.rejected
= 0;
6378 coding
->head_ascii
= 0;
6379 if (CONSP (coding_systems
)
6380 && detect_coding_utf_16 (coding
, &detect_info
))
6382 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6383 setup_coding_system (XCAR (coding_systems
), coding
);
6384 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6385 setup_coding_system (XCDR (coding_systems
), coding
);
6388 coding
->mode
= saved_mode
;
6393 decode_eol (struct coding_system
*coding
)
6395 Lisp_Object eol_type
;
6396 unsigned char *p
, *pbeg
, *pend
;
6398 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6399 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6402 if (NILP (coding
->dst_object
))
6403 pbeg
= coding
->destination
;
6405 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6406 pend
= pbeg
+ coding
->produced
;
6408 if (VECTORP (eol_type
))
6410 int eol_seen
= EOL_SEEN_NONE
;
6412 for (p
= pbeg
; p
< pend
; p
++)
6415 eol_seen
|= EOL_SEEN_LF
;
6416 else if (*p
== '\r')
6418 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6420 eol_seen
|= EOL_SEEN_CRLF
;
6424 eol_seen
|= EOL_SEEN_CR
;
6427 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6428 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6429 && (eol_seen
& EOL_SEEN_CR
) != 0
6430 && (eol_seen
& EOL_SEEN_LF
) == 0)
6431 eol_seen
= EOL_SEEN_CRLF
;
6432 else if (eol_seen
!= EOL_SEEN_NONE
6433 && eol_seen
!= EOL_SEEN_LF
6434 && eol_seen
!= EOL_SEEN_CRLF
6435 && eol_seen
!= EOL_SEEN_CR
)
6436 eol_seen
= EOL_SEEN_LF
;
6437 if (eol_seen
!= EOL_SEEN_NONE
)
6438 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6441 if (EQ (eol_type
, Qmac
))
6443 for (p
= pbeg
; p
< pend
; p
++)
6447 else if (EQ (eol_type
, Qdos
))
6451 if (NILP (coding
->dst_object
))
6453 /* Start deleting '\r' from the tail to minimize the memory
6455 for (p
= pend
- 2; p
>= pbeg
; p
--)
6458 memmove (p
, p
+ 1, pend
-- - p
- 1);
6464 ptrdiff_t pos_byte
= coding
->dst_pos_byte
;
6465 ptrdiff_t pos
= coding
->dst_pos
;
6466 ptrdiff_t pos_end
= pos
+ coding
->produced_char
- 1;
6468 while (pos
< pos_end
)
6470 p
= BYTE_POS_ADDR (pos_byte
);
6471 if (*p
== '\r' && p
[1] == '\n')
6473 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6478 if (coding
->dst_multibyte
)
6479 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6484 coding
->produced
-= n
;
6485 coding
->produced_char
-= n
;
6490 /* Return a translation table (or list of them) from coding system
6491 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6495 get_translation_table (Lisp_Object attrs
, bool encodep
, int *max_lookup
)
6497 Lisp_Object standard
, translation_table
;
6500 if (NILP (Venable_character_translation
))
6507 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6508 standard
= Vstandard_translation_table_for_encode
;
6510 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6511 standard
= Vstandard_translation_table_for_decode
;
6512 if (NILP (translation_table
))
6513 translation_table
= standard
;
6516 if (SYMBOLP (translation_table
))
6517 translation_table
= Fget (translation_table
, Qtranslation_table
);
6518 else if (CONSP (translation_table
))
6520 translation_table
= Fcopy_sequence (translation_table
);
6521 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6522 if (SYMBOLP (XCAR (val
)))
6523 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6525 if (CHAR_TABLE_P (standard
))
6527 if (CONSP (translation_table
))
6528 translation_table
= nconc2 (translation_table
,
6529 Fcons (standard
, Qnil
));
6531 translation_table
= Fcons (translation_table
,
6532 Fcons (standard
, Qnil
));
6539 if (CHAR_TABLE_P (translation_table
)
6540 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6542 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6543 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6544 *max_lookup
= XFASTINT (val
);
6546 else if (CONSP (translation_table
))
6550 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6551 if (CHAR_TABLE_P (XCAR (tail
))
6552 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6554 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6555 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6556 *max_lookup
= XFASTINT (tailval
);
6560 return translation_table
;
6563 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6566 if (CHAR_TABLE_P (table)) \
6568 trans = CHAR_TABLE_REF (table, c); \
6569 if (CHARACTERP (trans)) \
6570 c = XFASTINT (trans), trans = Qnil; \
6572 else if (CONSP (table)) \
6576 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6577 if (CHAR_TABLE_P (XCAR (tail))) \
6579 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6580 if (CHARACTERP (trans)) \
6581 c = XFASTINT (trans), trans = Qnil; \
6582 else if (! NILP (trans)) \
6589 /* Return a translation of character(s) at BUF according to TRANS.
6590 TRANS is TO-CHAR or ((FROM . TO) ...) where
6591 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6592 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6593 translation is found, and Qnil if not found..
6594 If BUF is too short to lookup characters in FROM, return Qt. */
6597 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6600 if (INTEGERP (trans
))
6602 for (; CONSP (trans
); trans
= XCDR (trans
))
6604 Lisp_Object val
= XCAR (trans
);
6605 Lisp_Object from
= XCAR (val
);
6606 ptrdiff_t len
= ASIZE (from
);
6609 for (i
= 0; i
< len
; i
++)
6611 if (buf
+ i
== buf_end
)
6613 if (XINT (AREF (from
, i
)) != buf
[i
])
6624 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6627 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6628 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6630 ptrdiff_t produced_chars
= 0;
6633 if (! coding
->chars_at_source
)
6635 /* Source characters are in coding->charbuf. */
6636 int *buf
= coding
->charbuf
;
6637 int *buf_end
= buf
+ coding
->charbuf_used
;
6639 if (EQ (coding
->src_object
, coding
->dst_object
))
6641 coding_set_source (coding
);
6642 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6645 while (buf
< buf_end
)
6652 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
6653 Lisp_Object trans
= Qnil
;
6655 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6658 trans
= get_translation (trans
, buf
, buf_end
);
6659 if (INTEGERP (trans
))
6661 else if (CONSP (trans
))
6663 from_nchars
= ASIZE (XCAR (trans
));
6664 trans
= XCDR (trans
);
6665 if (INTEGERP (trans
))
6669 to_nchars
= ASIZE (trans
);
6670 c
= XINT (AREF (trans
, 0));
6673 else if (EQ (trans
, Qt
) && ! last_block
)
6677 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
6679 if (((min (PTRDIFF_MAX
, SIZE_MAX
) - (buf_end
- buf
))
6680 / MAX_MULTIBYTE_LENGTH
)
6682 memory_full (SIZE_MAX
);
6683 dst
= alloc_destination (coding
,
6685 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6687 if (EQ (coding
->src_object
, coding
->dst_object
))
6689 coding_set_source (coding
);
6690 dst_end
= (((unsigned char *) coding
->source
)
6691 + coding
->consumed
);
6694 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6697 for (i
= 0; i
< to_nchars
; i
++)
6700 c
= XINT (AREF (trans
, i
));
6701 if (coding
->dst_multibyte
6702 || ! CHAR_BYTE8_P (c
))
6703 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6705 *dst
++ = CHAR_TO_BYTE8 (c
);
6707 produced_chars
+= to_nchars
;
6711 /* This is an annotation datum. (-C) is the length. */
6714 carryover
= buf_end
- buf
;
6718 /* Source characters are at coding->source. */
6719 const unsigned char *src
= coding
->source
;
6720 const unsigned char *src_end
= src
+ coding
->consumed
;
6722 if (EQ (coding
->dst_object
, coding
->src_object
))
6723 dst_end
= (unsigned char *) src
;
6724 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6726 if (coding
->src_multibyte
)
6728 bool multibytep
= 1;
6729 ptrdiff_t consumed_chars
= 0;
6733 const unsigned char *src_base
= src
;
6739 if (EQ (coding
->src_object
, coding
->dst_object
))
6740 dst_end
= (unsigned char *) src
;
6743 ptrdiff_t offset
= src
- coding
->source
;
6745 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6747 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6748 coding_set_source (coding
);
6749 src
= coding
->source
+ offset
;
6750 src_end
= coding
->source
+ coding
->consumed
;
6751 if (EQ (coding
->src_object
, coding
->dst_object
))
6752 dst_end
= (unsigned char *) src
;
6762 while (src
< src_end
)
6764 bool multibytep
= 1;
6767 if (dst
>= dst_end
- 1)
6769 if (EQ (coding
->src_object
, coding
->dst_object
))
6770 dst_end
= (unsigned char *) src
;
6771 if (dst
>= dst_end
- 1)
6773 ptrdiff_t offset
= src
- coding
->source
;
6774 ptrdiff_t more_bytes
;
6776 if (EQ (coding
->src_object
, coding
->dst_object
))
6777 more_bytes
= ((src_end
- src
) / 2) + 2;
6779 more_bytes
= src_end
- src
+ 2;
6780 dst
= alloc_destination (coding
, more_bytes
, dst
);
6781 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6782 coding_set_source (coding
);
6783 src
= coding
->source
+ offset
;
6784 src_end
= coding
->source
+ coding
->consumed
;
6785 if (EQ (coding
->src_object
, coding
->dst_object
))
6786 dst_end
= (unsigned char *) src
;
6794 if (!EQ (coding
->src_object
, coding
->dst_object
))
6796 ptrdiff_t require
= coding
->src_bytes
- coding
->dst_bytes
;
6800 ptrdiff_t offset
= src
- coding
->source
;
6802 dst
= alloc_destination (coding
, require
, dst
);
6803 coding_set_source (coding
);
6804 src
= coding
->source
+ offset
;
6805 src_end
= coding
->source
+ coding
->consumed
;
6808 produced_chars
= coding
->consumed_char
;
6809 while (src
< src_end
)
6814 produced
= dst
- (coding
->destination
+ coding
->produced
);
6815 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6816 insert_from_gap (produced_chars
, produced
);
6817 coding
->produced
+= produced
;
6818 coding
->produced_char
+= produced_chars
;
6822 /* Compose text in CODING->object according to the annotation data at
6823 CHARBUF. CHARBUF is an array:
6824 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6828 produce_composition (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6832 enum composition_method method
;
6833 Lisp_Object components
;
6835 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6836 to
= pos
+ charbuf
[2];
6837 method
= (enum composition_method
) (charbuf
[4]);
6839 if (method
== COMPOSITION_RELATIVE
)
6843 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6846 if (method
== COMPOSITION_WITH_RULE
)
6847 len
= charbuf
[2] * 3 - 2;
6848 charbuf
+= MAX_ANNOTATION_LENGTH
;
6849 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6850 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6852 if (charbuf
[i
] >= 0)
6853 args
[j
] = make_number (charbuf
[i
]);
6857 args
[j
] = make_number (charbuf
[i
] % 0x100);
6860 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6862 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6866 /* Put `charset' property on text in CODING->object according to
6867 the annotation data at CHARBUF. CHARBUF is an array:
6868 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6872 produce_charset (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6874 ptrdiff_t from
= pos
- charbuf
[2];
6875 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6877 Fput_text_property (make_number (from
), make_number (pos
),
6878 Qcharset
, CHARSET_NAME (charset
),
6879 coding
->dst_object
);
6883 #define CHARBUF_SIZE 0x4000
6885 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6887 int size = CHARBUF_SIZE; \
6889 coding->charbuf = NULL; \
6890 while (size > 1024) \
6892 coding->charbuf = alloca (sizeof (int) * size); \
6893 if (coding->charbuf) \
6897 if (! coding->charbuf) \
6899 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6902 coding->charbuf_size = size; \
6907 produce_annotation (struct coding_system
*coding
, ptrdiff_t pos
)
6909 int *charbuf
= coding
->charbuf
;
6910 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6912 if (NILP (coding
->dst_object
))
6915 while (charbuf
< charbuf_end
)
6921 int len
= -*charbuf
;
6926 case CODING_ANNOTATE_COMPOSITION_MASK
:
6927 produce_composition (coding
, charbuf
, pos
);
6929 case CODING_ANNOTATE_CHARSET_MASK
:
6930 produce_charset (coding
, charbuf
, pos
);
6938 /* Decode the data at CODING->src_object into CODING->dst_object.
6939 CODING->src_object is a buffer, a string, or nil.
6940 CODING->dst_object is a buffer.
6942 If CODING->src_object is a buffer, it must be the current buffer.
6943 In this case, if CODING->src_pos is positive, it is a position of
6944 the source text in the buffer, otherwise, the source text is in the
6945 gap area of the buffer, and CODING->src_pos specifies the offset of
6946 the text from GPT (which must be the same as PT). If this is the
6947 same buffer as CODING->dst_object, CODING->src_pos must be
6950 If CODING->src_object is a string, CODING->src_pos is an index to
6953 If CODING->src_object is nil, CODING->source must already point to
6954 the non-relocatable memory area. In this case, CODING->src_pos is
6955 an offset from CODING->source.
6957 The decoded data is inserted at the current point of the buffer
6962 decode_coding (struct coding_system
*coding
)
6965 Lisp_Object undo_list
;
6966 Lisp_Object translation_table
;
6967 struct ccl_spec cclspec
;
6971 if (BUFFERP (coding
->src_object
)
6972 && coding
->src_pos
> 0
6973 && coding
->src_pos
< GPT
6974 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6975 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6978 if (BUFFERP (coding
->dst_object
))
6980 set_buffer_internal (XBUFFER (coding
->dst_object
));
6982 move_gap_both (PT
, PT_BYTE
);
6984 /* We must disable undo_list in order to record the whole insert
6985 transaction via record_insert at the end. But doing so also
6986 disables the recording of the first change to the undo_list.
6987 Therefore we check for first change here and record it via
6988 record_first_change if needed. */
6989 if (MODIFF
<= SAVE_MODIFF
)
6990 record_first_change ();
6992 undo_list
= BVAR (current_buffer
, undo_list
);
6993 bset_undo_list (current_buffer
, Qt
);
6996 coding
->consumed
= coding
->consumed_char
= 0;
6997 coding
->produced
= coding
->produced_char
= 0;
6998 coding
->chars_at_source
= 0;
6999 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7002 ALLOC_CONVERSION_WORK_AREA (coding
);
7004 attrs
= CODING_ID_ATTRS (coding
->id
);
7005 translation_table
= get_translation_table (attrs
, 0, NULL
);
7008 if (coding
->decoder
== decode_coding_ccl
)
7010 coding
->spec
.ccl
= &cclspec
;
7011 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7015 ptrdiff_t pos
= coding
->dst_pos
+ coding
->produced_char
;
7017 coding_set_source (coding
);
7018 coding
->annotated
= 0;
7019 coding
->charbuf_used
= carryover
;
7020 (*(coding
->decoder
)) (coding
);
7021 coding_set_destination (coding
);
7022 carryover
= produce_chars (coding
, translation_table
, 0);
7023 if (coding
->annotated
)
7024 produce_annotation (coding
, pos
);
7025 for (i
= 0; i
< carryover
; i
++)
7027 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7029 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7030 || (coding
->consumed
< coding
->src_bytes
7031 && (coding
->result
== CODING_RESULT_SUCCESS
7032 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7036 coding_set_destination (coding
);
7037 coding
->charbuf_used
= carryover
;
7038 produce_chars (coding
, translation_table
, 1);
7041 coding
->carryover_bytes
= 0;
7042 if (coding
->consumed
< coding
->src_bytes
)
7044 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7045 const unsigned char *src
;
7047 coding_set_source (coding
);
7048 coding_set_destination (coding
);
7049 src
= coding
->source
+ coding
->consumed
;
7051 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7053 /* Flush out unprocessed data as binary chars. We are sure
7054 that the number of data is less than the size of
7056 coding
->charbuf_used
= 0;
7057 coding
->chars_at_source
= 0;
7059 while (nbytes
-- > 0)
7064 c
= BYTE8_TO_CHAR (c
);
7065 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7067 produce_chars (coding
, Qnil
, 1);
7071 /* Record unprocessed bytes in coding->carryover. We are
7072 sure that the number of data is less than the size of
7073 coding->carryover. */
7074 unsigned char *p
= coding
->carryover
;
7076 if (nbytes
> sizeof coding
->carryover
)
7077 nbytes
= sizeof coding
->carryover
;
7078 coding
->carryover_bytes
= nbytes
;
7079 while (nbytes
-- > 0)
7082 coding
->consumed
= coding
->src_bytes
;
7085 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7086 && !inhibit_eol_conversion
)
7087 decode_eol (coding
);
7088 if (BUFFERP (coding
->dst_object
))
7090 bset_undo_list (current_buffer
, undo_list
);
7091 record_insert (coding
->dst_pos
, coding
->produced_char
);
7096 /* Extract an annotation datum from a composition starting at POS and
7097 ending before LIMIT of CODING->src_object (buffer or string), store
7098 the data in BUF, set *STOP to a starting position of the next
7099 composition (if any) or to LIMIT, and return the address of the
7100 next element of BUF.
7102 If such an annotation is not found, set *STOP to a starting
7103 position of a composition after POS (if any) or to LIMIT, and
7107 handle_composition_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7108 struct coding_system
*coding
, int *buf
,
7111 ptrdiff_t start
, end
;
7114 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7117 else if (start
> pos
)
7123 /* We found a composition. Store the corresponding
7124 annotation data in BUF. */
7126 enum composition_method method
= COMPOSITION_METHOD (prop
);
7127 int nchars
= COMPOSITION_LENGTH (prop
);
7129 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7130 if (method
!= COMPOSITION_RELATIVE
)
7132 Lisp_Object components
;
7133 ptrdiff_t i
, len
, i_byte
;
7135 components
= COMPOSITION_COMPONENTS (prop
);
7136 if (VECTORP (components
))
7138 len
= ASIZE (components
);
7139 for (i
= 0; i
< len
; i
++)
7140 *buf
++ = XINT (AREF (components
, i
));
7142 else if (STRINGP (components
))
7144 len
= SCHARS (components
);
7148 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7152 else if (INTEGERP (components
))
7155 *buf
++ = XINT (components
);
7157 else if (CONSP (components
))
7159 for (len
= 0; CONSP (components
);
7160 len
++, components
= XCDR (components
))
7161 *buf
++ = XINT (XCAR (components
));
7169 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7180 /* Extract an annotation datum from a text property `charset' at POS of
7181 CODING->src_object (buffer of string), store the data in BUF, set
7182 *STOP to the position where the value of `charset' property changes
7183 (limiting by LIMIT), and return the address of the next element of
7186 If the property value is nil, set *STOP to the position where the
7187 property value is non-nil (limiting by LIMIT), and return BUF. */
7190 handle_charset_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7191 struct coding_system
*coding
, int *buf
,
7194 Lisp_Object val
, next
;
7197 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7198 if (! NILP (val
) && CHARSETP (val
))
7199 id
= XINT (CHARSET_SYMBOL_ID (val
));
7202 ADD_CHARSET_DATA (buf
, 0, id
);
7203 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7205 make_number (limit
));
7206 *stop
= XINT (next
);
7212 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7215 int *buf
= coding
->charbuf
;
7216 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7217 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7218 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7219 ptrdiff_t pos
= coding
->src_pos
+ coding
->consumed_char
;
7220 ptrdiff_t end_pos
= coding
->src_pos
+ coding
->src_chars
;
7221 bool multibytep
= coding
->src_multibyte
;
7222 Lisp_Object eol_type
;
7224 ptrdiff_t stop
, stop_composition
, stop_charset
;
7225 int *lookup_buf
= NULL
;
7227 if (! NILP (translation_table
))
7228 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7230 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7231 if (VECTORP (eol_type
))
7234 /* Note: composition handling is not yet implemented. */
7235 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7237 if (NILP (coding
->src_object
))
7238 stop
= stop_composition
= stop_charset
= end_pos
;
7241 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7242 stop
= stop_composition
= pos
;
7244 stop
= stop_composition
= end_pos
;
7245 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7246 stop
= stop_charset
= pos
;
7248 stop_charset
= end_pos
;
7251 /* Compensate for CRLF and conversion. */
7252 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7253 while (buf
< buf_end
)
7261 if (pos
== stop_composition
)
7262 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7263 buf
, &stop_composition
);
7264 if (pos
== stop_charset
)
7265 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7266 buf
, &stop_charset
);
7267 stop
= (stop_composition
< stop_charset
7268 ? stop_composition
: stop_charset
);
7275 if (coding
->encoder
== encode_coding_raw_text
7276 || coding
->encoder
== encode_coding_ccl
)
7278 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7279 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7281 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7284 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7285 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7287 if (! EQ (eol_type
, Qunix
))
7291 if (EQ (eol_type
, Qdos
))
7299 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7304 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7305 int *lookup_buf_end
;
7306 const unsigned char *p
= src
;
7310 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7311 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7312 lookup_buf_end
= lookup_buf
+ i
;
7313 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7314 if (INTEGERP (trans
))
7316 else if (CONSP (trans
))
7318 from_nchars
= ASIZE (XCAR (trans
));
7319 trans
= XCDR (trans
);
7320 if (INTEGERP (trans
))
7324 to_nchars
= ASIZE (trans
);
7325 if (buf_end
- buf
< to_nchars
)
7327 c
= XINT (AREF (trans
, 0));
7333 for (i
= 1; i
< to_nchars
; i
++)
7334 *buf
++ = XINT (AREF (trans
, i
));
7335 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7336 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7340 coding
->consumed
= src
- coding
->source
;
7341 coding
->consumed_char
= pos
- coding
->src_pos
;
7342 coding
->charbuf_used
= buf
- coding
->charbuf
;
7343 coding
->chars_at_source
= 0;
7347 /* Encode the text at CODING->src_object into CODING->dst_object.
7348 CODING->src_object is a buffer or a string.
7349 CODING->dst_object is a buffer or nil.
7351 If CODING->src_object is a buffer, it must be the current buffer.
7352 In this case, if CODING->src_pos is positive, it is a position of
7353 the source text in the buffer, otherwise. the source text is in the
7354 gap area of the buffer, and coding->src_pos specifies the offset of
7355 the text from GPT (which must be the same as PT). If this is the
7356 same buffer as CODING->dst_object, CODING->src_pos must be
7357 negative and CODING should not have `pre-write-conversion'.
7359 If CODING->src_object is a string, CODING should not have
7360 `pre-write-conversion'.
7362 If CODING->dst_object is a buffer, the encoded data is inserted at
7363 the current point of that buffer.
7365 If CODING->dst_object is nil, the encoded data is placed at the
7366 memory area specified by CODING->destination. */
7369 encode_coding (struct coding_system
*coding
)
7372 Lisp_Object translation_table
;
7374 struct ccl_spec cclspec
;
7376 attrs
= CODING_ID_ATTRS (coding
->id
);
7377 if (coding
->encoder
== encode_coding_raw_text
)
7378 translation_table
= Qnil
, max_lookup
= 0;
7380 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7382 if (BUFFERP (coding
->dst_object
))
7384 set_buffer_internal (XBUFFER (coding
->dst_object
));
7385 coding
->dst_multibyte
7386 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7389 coding
->consumed
= coding
->consumed_char
= 0;
7390 coding
->produced
= coding
->produced_char
= 0;
7391 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7394 ALLOC_CONVERSION_WORK_AREA (coding
);
7396 if (coding
->encoder
== encode_coding_ccl
)
7398 coding
->spec
.ccl
= &cclspec
;
7399 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7402 coding_set_source (coding
);
7403 consume_chars (coding
, translation_table
, max_lookup
);
7404 coding_set_destination (coding
);
7405 (*(coding
->encoder
)) (coding
);
7406 } while (coding
->consumed_char
< coding
->src_chars
);
7408 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7409 insert_from_gap (coding
->produced_char
, coding
->produced
);
7413 /* Name (or base name) of work buffer for code conversion. */
7414 static Lisp_Object Vcode_conversion_workbuf_name
;
7416 /* A working buffer used by the top level conversion. Once it is
7417 created, it is never destroyed. It has the name
7418 Vcode_conversion_workbuf_name. The other working buffers are
7419 destroyed after the use is finished, and their names are modified
7420 versions of Vcode_conversion_workbuf_name. */
7421 static Lisp_Object Vcode_conversion_reused_workbuf
;
7423 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7424 static bool reused_workbuf_in_use
;
7427 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7428 multibyteness of returning buffer. */
7431 make_conversion_work_buffer (bool multibyte
)
7433 Lisp_Object name
, workbuf
;
7434 struct buffer
*current
;
7436 if (reused_workbuf_in_use
)
7438 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7439 workbuf
= Fget_buffer_create (name
);
7443 reused_workbuf_in_use
= 1;
7444 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7445 Vcode_conversion_reused_workbuf
7446 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7447 workbuf
= Vcode_conversion_reused_workbuf
;
7449 current
= current_buffer
;
7450 set_buffer_internal (XBUFFER (workbuf
));
7451 /* We can't allow modification hooks to run in the work buffer. For
7452 instance, directory_files_internal assumes that file decoding
7453 doesn't compile new regexps. */
7454 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7456 bset_undo_list (current_buffer
, Qt
);
7457 bset_enable_multibyte_characters (current_buffer
, multibyte
? Qt
: Qnil
);
7458 set_buffer_internal (current
);
7464 code_conversion_restore (Lisp_Object arg
)
7466 Lisp_Object current
, workbuf
;
7467 struct gcpro gcpro1
;
7470 current
= XCAR (arg
);
7471 workbuf
= XCDR (arg
);
7472 if (! NILP (workbuf
))
7474 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7475 reused_workbuf_in_use
= 0;
7477 Fkill_buffer (workbuf
);
7479 set_buffer_internal (XBUFFER (current
));
7485 code_conversion_save (bool with_work_buf
, bool multibyte
)
7487 Lisp_Object workbuf
= Qnil
;
7490 workbuf
= make_conversion_work_buffer (multibyte
);
7491 record_unwind_protect (code_conversion_restore
,
7492 Fcons (Fcurrent_buffer (), workbuf
));
7497 decode_coding_gap (struct coding_system
*coding
,
7498 ptrdiff_t chars
, ptrdiff_t bytes
)
7500 ptrdiff_t count
= SPECPDL_INDEX ();
7503 code_conversion_save (0, 0);
7505 coding
->src_object
= Fcurrent_buffer ();
7506 coding
->src_chars
= chars
;
7507 coding
->src_bytes
= bytes
;
7508 coding
->src_pos
= -chars
;
7509 coding
->src_pos_byte
= -bytes
;
7510 coding
->src_multibyte
= chars
< bytes
;
7511 coding
->dst_object
= coding
->src_object
;
7512 coding
->dst_pos
= PT
;
7513 coding
->dst_pos_byte
= PT_BYTE
;
7514 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7516 if (CODING_REQUIRE_DETECTION (coding
))
7517 detect_coding (coding
);
7519 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7520 current_buffer
->text
->inhibit_shrinking
= 1;
7521 decode_coding (coding
);
7522 current_buffer
->text
->inhibit_shrinking
= 0;
7524 attrs
= CODING_ID_ATTRS (coding
->id
);
7525 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7527 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7530 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7531 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7532 make_number (coding
->produced_char
));
7534 coding
->produced_char
+= Z
- prev_Z
;
7535 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7538 unbind_to (count
, Qnil
);
7542 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7543 SRC_OBJECT into DST_OBJECT by coding context CODING.
7545 SRC_OBJECT is a buffer, a string, or Qnil.
7547 If it is a buffer, the text is at point of the buffer. FROM and TO
7548 are positions in the buffer.
7550 If it is a string, the text is at the beginning of the string.
7551 FROM and TO are indices to the string.
7553 If it is nil, the text is at coding->source. FROM and TO are
7554 indices to coding->source.
7556 DST_OBJECT is a buffer, Qt, or Qnil.
7558 If it is a buffer, the decoded text is inserted at point of the
7559 buffer. If the buffer is the same as SRC_OBJECT, the source text
7562 If it is Qt, a string is made from the decoded text, and
7563 set in CODING->dst_object.
7565 If it is Qnil, the decoded text is stored at CODING->destination.
7566 The caller must allocate CODING->dst_bytes bytes at
7567 CODING->destination by xmalloc. If the decoded text is longer than
7568 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7572 decode_coding_object (struct coding_system
*coding
,
7573 Lisp_Object src_object
,
7574 ptrdiff_t from
, ptrdiff_t from_byte
,
7575 ptrdiff_t to
, ptrdiff_t to_byte
,
7576 Lisp_Object dst_object
)
7578 ptrdiff_t count
= SPECPDL_INDEX ();
7579 unsigned char *destination
IF_LINT (= NULL
);
7580 ptrdiff_t dst_bytes
IF_LINT (= 0);
7581 ptrdiff_t chars
= to
- from
;
7582 ptrdiff_t bytes
= to_byte
- from_byte
;
7584 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7585 bool need_marker_adjustment
= 0;
7586 Lisp_Object old_deactivate_mark
;
7588 old_deactivate_mark
= Vdeactivate_mark
;
7590 if (NILP (dst_object
))
7592 destination
= coding
->destination
;
7593 dst_bytes
= coding
->dst_bytes
;
7596 coding
->src_object
= src_object
;
7597 coding
->src_chars
= chars
;
7598 coding
->src_bytes
= bytes
;
7599 coding
->src_multibyte
= chars
< bytes
;
7601 if (STRINGP (src_object
))
7603 coding
->src_pos
= from
;
7604 coding
->src_pos_byte
= from_byte
;
7606 else if (BUFFERP (src_object
))
7608 set_buffer_internal (XBUFFER (src_object
));
7610 move_gap_both (from
, from_byte
);
7611 if (EQ (src_object
, dst_object
))
7613 struct Lisp_Marker
*tail
;
7615 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7617 tail
->need_adjustment
7618 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7619 need_marker_adjustment
|= tail
->need_adjustment
;
7621 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7622 TEMP_SET_PT_BOTH (from
, from_byte
);
7623 current_buffer
->text
->inhibit_shrinking
= 1;
7624 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7625 coding
->src_pos
= -chars
;
7626 coding
->src_pos_byte
= -bytes
;
7630 coding
->src_pos
= from
;
7631 coding
->src_pos_byte
= from_byte
;
7635 if (CODING_REQUIRE_DETECTION (coding
))
7636 detect_coding (coding
);
7637 attrs
= CODING_ID_ATTRS (coding
->id
);
7639 if (EQ (dst_object
, Qt
)
7640 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7641 && NILP (dst_object
)))
7643 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7644 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7645 coding
->dst_pos
= BEG
;
7646 coding
->dst_pos_byte
= BEG_BYTE
;
7648 else if (BUFFERP (dst_object
))
7650 code_conversion_save (0, 0);
7651 coding
->dst_object
= dst_object
;
7652 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7653 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7654 coding
->dst_multibyte
7655 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7659 code_conversion_save (0, 0);
7660 coding
->dst_object
= Qnil
;
7661 /* Most callers presume this will return a multibyte result, and they
7662 won't use `binary' or `raw-text' anyway, so let's not worry about
7663 CODING_FOR_UNIBYTE. */
7664 coding
->dst_multibyte
= 1;
7667 decode_coding (coding
);
7669 if (BUFFERP (coding
->dst_object
))
7670 set_buffer_internal (XBUFFER (coding
->dst_object
));
7672 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7674 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7675 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7678 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7679 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7680 old_deactivate_mark
);
7681 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7682 make_number (coding
->produced_char
));
7685 coding
->produced_char
+= Z
- prev_Z
;
7686 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7689 if (EQ (dst_object
, Qt
))
7691 coding
->dst_object
= Fbuffer_string ();
7693 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7695 set_buffer_internal (XBUFFER (coding
->dst_object
));
7696 if (dst_bytes
< coding
->produced
)
7698 destination
= xrealloc (destination
, coding
->produced
);
7701 record_conversion_result (coding
,
7702 CODING_RESULT_INSUFFICIENT_MEM
);
7703 unbind_to (count
, Qnil
);
7706 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7707 move_gap_both (BEGV
, BEGV_BYTE
);
7708 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7709 coding
->destination
= destination
;
7715 /* This is the case of:
7716 (BUFFERP (src_object) && EQ (src_object, dst_object))
7717 As we have moved PT while replacing the original buffer
7718 contents, we must recover it now. */
7719 set_buffer_internal (XBUFFER (src_object
));
7720 current_buffer
->text
->inhibit_shrinking
= 0;
7721 if (saved_pt
< from
)
7722 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7723 else if (saved_pt
< from
+ chars
)
7724 TEMP_SET_PT_BOTH (from
, from_byte
);
7725 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7726 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7727 saved_pt_byte
+ (coding
->produced
- bytes
));
7729 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7730 saved_pt_byte
+ (coding
->produced
- bytes
));
7732 if (need_marker_adjustment
)
7734 struct Lisp_Marker
*tail
;
7736 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7737 if (tail
->need_adjustment
)
7739 tail
->need_adjustment
= 0;
7740 if (tail
->insertion_type
)
7742 tail
->bytepos
= from_byte
;
7743 tail
->charpos
= from
;
7747 tail
->bytepos
= from_byte
+ coding
->produced
;
7749 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7750 ? tail
->bytepos
: from
+ coding
->produced_char
);
7756 Vdeactivate_mark
= old_deactivate_mark
;
7757 unbind_to (count
, coding
->dst_object
);
7762 encode_coding_object (struct coding_system
*coding
,
7763 Lisp_Object src_object
,
7764 ptrdiff_t from
, ptrdiff_t from_byte
,
7765 ptrdiff_t to
, ptrdiff_t to_byte
,
7766 Lisp_Object dst_object
)
7768 ptrdiff_t count
= SPECPDL_INDEX ();
7769 ptrdiff_t chars
= to
- from
;
7770 ptrdiff_t bytes
= to_byte
- from_byte
;
7772 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7773 bool need_marker_adjustment
= 0;
7774 bool kill_src_buffer
= 0;
7775 Lisp_Object old_deactivate_mark
;
7777 old_deactivate_mark
= Vdeactivate_mark
;
7779 coding
->src_object
= src_object
;
7780 coding
->src_chars
= chars
;
7781 coding
->src_bytes
= bytes
;
7782 coding
->src_multibyte
= chars
< bytes
;
7784 attrs
= CODING_ID_ATTRS (coding
->id
);
7786 if (EQ (src_object
, dst_object
))
7788 struct Lisp_Marker
*tail
;
7790 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7792 tail
->need_adjustment
7793 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7794 need_marker_adjustment
|= tail
->need_adjustment
;
7798 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7800 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7801 set_buffer_internal (XBUFFER (coding
->src_object
));
7802 if (STRINGP (src_object
))
7803 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7804 else if (BUFFERP (src_object
))
7805 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7807 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7809 if (EQ (src_object
, dst_object
))
7811 set_buffer_internal (XBUFFER (src_object
));
7812 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7813 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7814 set_buffer_internal (XBUFFER (coding
->src_object
));
7818 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7820 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7821 old_deactivate_mark
);
7822 safe_call2 (CODING_ATTR_PRE_WRITE (attrs
),
7823 make_number (BEG
), make_number (Z
));
7826 if (XBUFFER (coding
->src_object
) != current_buffer
)
7827 kill_src_buffer
= 1;
7828 coding
->src_object
= Fcurrent_buffer ();
7830 move_gap_both (BEG
, BEG_BYTE
);
7831 coding
->src_chars
= Z
- BEG
;
7832 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7833 coding
->src_pos
= BEG
;
7834 coding
->src_pos_byte
= BEG_BYTE
;
7835 coding
->src_multibyte
= Z
< Z_BYTE
;
7837 else if (STRINGP (src_object
))
7839 code_conversion_save (0, 0);
7840 coding
->src_pos
= from
;
7841 coding
->src_pos_byte
= from_byte
;
7843 else if (BUFFERP (src_object
))
7845 code_conversion_save (0, 0);
7846 set_buffer_internal (XBUFFER (src_object
));
7847 if (EQ (src_object
, dst_object
))
7849 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7850 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7851 coding
->src_pos
= 0;
7852 coding
->src_pos_byte
= 0;
7856 if (from
< GPT
&& to
>= GPT
)
7857 move_gap_both (from
, from_byte
);
7858 coding
->src_pos
= from
;
7859 coding
->src_pos_byte
= from_byte
;
7863 code_conversion_save (0, 0);
7865 if (BUFFERP (dst_object
))
7867 coding
->dst_object
= dst_object
;
7868 if (EQ (src_object
, dst_object
))
7870 coding
->dst_pos
= from
;
7871 coding
->dst_pos_byte
= from_byte
;
7875 struct buffer
*current
= current_buffer
;
7877 set_buffer_temp (XBUFFER (dst_object
));
7878 coding
->dst_pos
= PT
;
7879 coding
->dst_pos_byte
= PT_BYTE
;
7880 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7881 set_buffer_temp (current
);
7883 coding
->dst_multibyte
7884 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7886 else if (EQ (dst_object
, Qt
))
7888 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
7889 coding
->dst_object
= Qnil
;
7890 coding
->destination
= xmalloc (dst_bytes
);
7891 coding
->dst_bytes
= dst_bytes
;
7892 coding
->dst_multibyte
= 0;
7896 coding
->dst_object
= Qnil
;
7897 coding
->dst_multibyte
= 0;
7900 encode_coding (coding
);
7902 if (EQ (dst_object
, Qt
))
7904 if (BUFFERP (coding
->dst_object
))
7905 coding
->dst_object
= Fbuffer_string ();
7909 = make_unibyte_string ((char *) coding
->destination
,
7911 xfree (coding
->destination
);
7917 /* This is the case of:
7918 (BUFFERP (src_object) && EQ (src_object, dst_object))
7919 As we have moved PT while replacing the original buffer
7920 contents, we must recover it now. */
7921 set_buffer_internal (XBUFFER (src_object
));
7922 if (saved_pt
< from
)
7923 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7924 else if (saved_pt
< from
+ chars
)
7925 TEMP_SET_PT_BOTH (from
, from_byte
);
7926 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7927 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7928 saved_pt_byte
+ (coding
->produced
- bytes
));
7930 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7931 saved_pt_byte
+ (coding
->produced
- bytes
));
7933 if (need_marker_adjustment
)
7935 struct Lisp_Marker
*tail
;
7937 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7938 if (tail
->need_adjustment
)
7940 tail
->need_adjustment
= 0;
7941 if (tail
->insertion_type
)
7943 tail
->bytepos
= from_byte
;
7944 tail
->charpos
= from
;
7948 tail
->bytepos
= from_byte
+ coding
->produced
;
7950 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7951 ? tail
->bytepos
: from
+ coding
->produced_char
);
7957 if (kill_src_buffer
)
7958 Fkill_buffer (coding
->src_object
);
7960 Vdeactivate_mark
= old_deactivate_mark
;
7961 unbind_to (count
, Qnil
);
7966 preferred_coding_system (void)
7968 int id
= coding_categories
[coding_priorities
[0]].id
;
7970 return CODING_ID_NAME (id
);
7973 #if defined (WINDOWSNT) || defined (CYGWIN)
7976 from_unicode (Lisp_Object str
)
7979 if (!STRING_MULTIBYTE (str
) &&
7982 str
= Fsubstring (str
, make_number (0), make_number (-1));
7985 return code_convert_string_norecord (str
, Qutf_16le
, 0);
7989 to_unicode (Lisp_Object str
, Lisp_Object
*buf
)
7991 *buf
= code_convert_string_norecord (str
, Qutf_16le
, 1);
7992 /* We need to make a another copy (in addition to the one made by
7993 code_convert_string_norecord) to ensure that the final string is
7994 _doubly_ zero terminated --- that is, that the string is
7995 terminated by two zero bytes and one utf-16le null character.
7996 Because strings are already terminated with a single zero byte,
7997 we just add one additional zero. */
7998 str
= make_uninit_string (SBYTES (*buf
) + 1);
7999 memcpy (SDATA (str
), SDATA (*buf
), SBYTES (*buf
));
8000 SDATA (str
) [SBYTES (*buf
)] = '\0';
8002 return WCSDATA (*buf
);
8005 #endif /* WINDOWSNT || CYGWIN */
8009 /*** 8. Emacs Lisp library functions ***/
8011 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8012 doc
: /* Return t if OBJECT is nil or a coding-system.
8013 See the documentation of `define-coding-system' for information
8014 about coding-system objects. */)
8015 (Lisp_Object object
)
8018 || CODING_SYSTEM_ID (object
) >= 0)
8020 if (! SYMBOLP (object
)
8021 || NILP (Fget (object
, Qcoding_system_define_form
)))
8026 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8027 Sread_non_nil_coding_system
, 1, 1, 0,
8028 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8029 (Lisp_Object prompt
)
8034 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8035 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8037 while (SCHARS (val
) == 0);
8038 return (Fintern (val
, Qnil
));
8041 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8042 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8043 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8044 Ignores case when completing coding systems (all Emacs coding systems
8045 are lower-case). */)
8046 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8049 ptrdiff_t count
= SPECPDL_INDEX ();
8051 if (SYMBOLP (default_coding_system
))
8052 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8053 specbind (Qcompletion_ignore_case
, Qt
);
8054 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8055 Qt
, Qnil
, Qcoding_system_history
,
8056 default_coding_system
, Qnil
);
8057 unbind_to (count
, Qnil
);
8058 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8061 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8063 doc
: /* Check validity of CODING-SYSTEM.
8064 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8065 It is valid if it is nil or a symbol defined as a coding system by the
8066 function `define-coding-system'. */)
8067 (Lisp_Object coding_system
)
8069 Lisp_Object define_form
;
8071 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8072 if (! NILP (define_form
))
8074 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8075 safe_eval (define_form
);
8077 if (!NILP (Fcoding_system_p (coding_system
)))
8078 return coding_system
;
8079 xsignal1 (Qcoding_system_error
, coding_system
);
8083 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8084 HIGHEST, return the coding system of the highest
8085 priority among the detected coding systems. Otherwise return a
8086 list of detected coding systems sorted by their priorities. If
8087 MULTIBYTEP, it is assumed that the bytes are in correct
8088 multibyte form but contains only ASCII and eight-bit chars.
8089 Otherwise, the bytes are raw bytes.
8091 CODING-SYSTEM controls the detection as below:
8093 If it is nil, detect both text-format and eol-format. If the
8094 text-format part of CODING-SYSTEM is already specified
8095 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8096 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8097 detect only text-format. */
8100 detect_coding_system (const unsigned char *src
,
8101 ptrdiff_t src_chars
, ptrdiff_t src_bytes
,
8102 bool highest
, bool multibytep
,
8103 Lisp_Object coding_system
)
8105 const unsigned char *src_end
= src
+ src_bytes
;
8106 Lisp_Object attrs
, eol_type
;
8107 Lisp_Object val
= Qnil
;
8108 struct coding_system coding
;
8110 struct coding_detection_info detect_info
;
8111 enum coding_category base_category
;
8112 bool null_byte_found
= 0, eight_bit_found
= 0;
8114 if (NILP (coding_system
))
8115 coding_system
= Qundecided
;
8116 setup_coding_system (coding_system
, &coding
);
8117 attrs
= CODING_ID_ATTRS (coding
.id
);
8118 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8119 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8121 coding
.source
= src
;
8122 coding
.src_chars
= src_chars
;
8123 coding
.src_bytes
= src_bytes
;
8124 coding
.src_multibyte
= multibytep
;
8125 coding
.consumed
= 0;
8126 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8127 coding
.head_ascii
= 0;
8129 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8131 /* At first, detect text-format if necessary. */
8132 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8133 if (base_category
== coding_category_undecided
)
8135 enum coding_category category
IF_LINT (= 0);
8136 struct coding_system
*this IF_LINT (= NULL
);
8139 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8140 for (; src
< src_end
; src
++)
8145 eight_bit_found
= 1;
8146 if (null_byte_found
)
8151 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8152 && ! inhibit_iso_escape_detection
8153 && ! detect_info
.checked
)
8155 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8157 /* We have scanned the whole data. */
8158 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8160 /* We didn't find an 8-bit code. We may
8161 have found a null-byte, but it's very
8162 rare that a binary file confirm to
8165 coding
.head_ascii
= src
- coding
.source
;
8167 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8171 else if (! c
&& !inhibit_null_byte_detection
)
8173 null_byte_found
= 1;
8174 if (eight_bit_found
)
8177 if (! eight_bit_found
)
8178 coding
.head_ascii
++;
8180 else if (! eight_bit_found
)
8181 coding
.head_ascii
++;
8184 if (null_byte_found
|| eight_bit_found
8185 || coding
.head_ascii
< coding
.src_bytes
8186 || detect_info
.found
)
8188 if (coding
.head_ascii
== coding
.src_bytes
)
8189 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8190 for (i
= 0; i
< coding_category_raw_text
; i
++)
8192 category
= coding_priorities
[i
];
8193 this = coding_categories
+ category
;
8194 if (detect_info
.found
& (1 << category
))
8199 if (null_byte_found
)
8201 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8202 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8204 for (i
= 0; i
< coding_category_raw_text
; i
++)
8206 category
= coding_priorities
[i
];
8207 this = coding_categories
+ category
;
8211 /* No coding system of this category is defined. */
8212 detect_info
.rejected
|= (1 << category
);
8214 else if (category
>= coding_category_raw_text
)
8216 else if (detect_info
.checked
& (1 << category
))
8219 && (detect_info
.found
& (1 << category
)))
8222 else if ((*(this->detector
)) (&coding
, &detect_info
)
8224 && (detect_info
.found
& (1 << category
)))
8226 if (category
== coding_category_utf_16_auto
)
8228 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8229 category
= coding_category_utf_16_le
;
8231 category
= coding_category_utf_16_be
;
8239 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8242 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8243 id
= CODING_SYSTEM_ID (Qno_conversion
);
8244 val
= Fcons (make_number (id
), Qnil
);
8246 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8248 detect_info
.found
= CATEGORY_MASK_ANY
;
8249 id
= coding_categories
[coding_category_undecided
].id
;
8250 val
= Fcons (make_number (id
), Qnil
);
8254 if (detect_info
.found
)
8256 detect_info
.found
= 1 << category
;
8257 val
= Fcons (make_number (this->id
), Qnil
);
8260 for (i
= 0; i
< coding_category_raw_text
; i
++)
8261 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8263 detect_info
.found
= 1 << coding_priorities
[i
];
8264 id
= coding_categories
[coding_priorities
[i
]].id
;
8265 val
= Fcons (make_number (id
), Qnil
);
8271 int mask
= detect_info
.rejected
| detect_info
.found
;
8274 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8276 category
= coding_priorities
[i
];
8277 if (! (mask
& (1 << category
)))
8279 found
|= 1 << category
;
8280 id
= coding_categories
[category
].id
;
8282 val
= Fcons (make_number (id
), val
);
8285 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8287 category
= coding_priorities
[i
];
8288 if (detect_info
.found
& (1 << category
))
8290 id
= coding_categories
[category
].id
;
8291 val
= Fcons (make_number (id
), val
);
8294 detect_info
.found
|= found
;
8297 else if (base_category
== coding_category_utf_8_auto
)
8299 if (detect_coding_utf_8 (&coding
, &detect_info
))
8301 struct coding_system
*this;
8303 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8304 this = coding_categories
+ coding_category_utf_8_sig
;
8306 this = coding_categories
+ coding_category_utf_8_nosig
;
8307 val
= Fcons (make_number (this->id
), Qnil
);
8310 else if (base_category
== coding_category_utf_16_auto
)
8312 if (detect_coding_utf_16 (&coding
, &detect_info
))
8314 struct coding_system
*this;
8316 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8317 this = coding_categories
+ coding_category_utf_16_le
;
8318 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8319 this = coding_categories
+ coding_category_utf_16_be
;
8320 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8321 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8323 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8324 val
= Fcons (make_number (this->id
), Qnil
);
8329 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8330 val
= Fcons (make_number (coding
.id
), Qnil
);
8333 /* Then, detect eol-format if necessary. */
8335 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8338 if (VECTORP (eol_type
))
8340 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8342 if (null_byte_found
)
8343 normal_eol
= EOL_SEEN_LF
;
8345 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8346 coding_category_raw_text
);
8348 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8349 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8350 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8351 coding_category_utf_16_be
);
8352 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8353 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8354 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8355 coding_category_utf_16_le
);
8359 if (EQ (eol_type
, Qunix
))
8360 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8361 else if (EQ (eol_type
, Qdos
))
8362 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8364 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8367 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8369 enum coding_category category
;
8372 id
= XINT (XCAR (tail
));
8373 attrs
= CODING_ID_ATTRS (id
);
8374 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8375 eol_type
= CODING_ID_EOL_TYPE (id
);
8376 if (VECTORP (eol_type
))
8378 if (category
== coding_category_utf_16_be
8379 || category
== coding_category_utf_16_be_nosig
)
8380 this_eol
= utf_16_be_eol
;
8381 else if (category
== coding_category_utf_16_le
8382 || category
== coding_category_utf_16_le_nosig
)
8383 this_eol
= utf_16_le_eol
;
8385 this_eol
= normal_eol
;
8387 if (this_eol
== EOL_SEEN_LF
)
8388 XSETCAR (tail
, AREF (eol_type
, 0));
8389 else if (this_eol
== EOL_SEEN_CRLF
)
8390 XSETCAR (tail
, AREF (eol_type
, 1));
8391 else if (this_eol
== EOL_SEEN_CR
)
8392 XSETCAR (tail
, AREF (eol_type
, 2));
8394 XSETCAR (tail
, CODING_ID_NAME (id
));
8397 XSETCAR (tail
, CODING_ID_NAME (id
));
8401 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8405 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8407 doc
: /* Detect coding system of the text in the region between START and END.
8408 Return a list of possible coding systems ordered by priority.
8409 The coding systems to try and their priorities follows what
8410 the function `coding-system-priority-list' (which see) returns.
8412 If only ASCII characters are found (except for such ISO-2022 control
8413 characters as ESC), it returns a list of single element `undecided'
8414 or its subsidiary coding system according to a detected end-of-line
8417 If optional argument HIGHEST is non-nil, return the coding system of
8418 highest priority. */)
8419 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8422 ptrdiff_t from_byte
, to_byte
;
8424 validate_region (&start
, &end
);
8425 from
= XINT (start
), to
= XINT (end
);
8426 from_byte
= CHAR_TO_BYTE (from
);
8427 to_byte
= CHAR_TO_BYTE (to
);
8429 if (from
< GPT
&& to
>= GPT
)
8430 move_gap_both (to
, to_byte
);
8432 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8433 to
- from
, to_byte
- from_byte
,
8435 !NILP (BVAR (current_buffer
8436 , enable_multibyte_characters
)),
8440 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8442 doc
: /* Detect coding system of the text in STRING.
8443 Return a list of possible coding systems ordered by priority.
8444 The coding systems to try and their priorities follows what
8445 the function `coding-system-priority-list' (which see) returns.
8447 If only ASCII characters are found (except for such ISO-2022 control
8448 characters as ESC), it returns a list of single element `undecided'
8449 or its subsidiary coding system according to a detected end-of-line
8452 If optional argument HIGHEST is non-nil, return the coding system of
8453 highest priority. */)
8454 (Lisp_Object string
, Lisp_Object highest
)
8456 CHECK_STRING (string
);
8458 return detect_coding_system (SDATA (string
),
8459 SCHARS (string
), SBYTES (string
),
8460 !NILP (highest
), STRING_MULTIBYTE (string
),
8466 char_encodable_p (int c
, Lisp_Object attrs
)
8469 struct charset
*charset
;
8470 Lisp_Object translation_table
;
8472 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8473 if (! NILP (translation_table
))
8474 c
= translate_char (translation_table
, c
);
8475 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8476 CONSP (tail
); tail
= XCDR (tail
))
8478 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8479 if (CHAR_CHARSET_P (c
, charset
))
8482 return (! NILP (tail
));
8486 /* Return a list of coding systems that safely encode the text between
8487 START and END. If EXCLUDE is non-nil, it is a list of coding
8488 systems not to check. The returned list doesn't contain any such
8489 coding systems. In any case, if the text contains only ASCII or is
8490 unibyte, return t. */
8492 DEFUN ("find-coding-systems-region-internal",
8493 Ffind_coding_systems_region_internal
,
8494 Sfind_coding_systems_region_internal
, 2, 3, 0,
8495 doc
: /* Internal use only. */)
8496 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8498 Lisp_Object coding_attrs_list
, safe_codings
;
8499 ptrdiff_t start_byte
, end_byte
;
8500 const unsigned char *p
, *pbeg
, *pend
;
8502 Lisp_Object tail
, elt
, work_table
;
8504 if (STRINGP (start
))
8506 if (!STRING_MULTIBYTE (start
)
8507 || SCHARS (start
) == SBYTES (start
))
8510 end_byte
= SBYTES (start
);
8514 CHECK_NUMBER_COERCE_MARKER (start
);
8515 CHECK_NUMBER_COERCE_MARKER (end
);
8516 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8517 args_out_of_range (start
, end
);
8518 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8520 start_byte
= CHAR_TO_BYTE (XINT (start
));
8521 end_byte
= CHAR_TO_BYTE (XINT (end
));
8522 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8525 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8527 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8528 move_gap_both (XINT (start
), start_byte
);
8530 move_gap_both (XINT (end
), end_byte
);
8534 coding_attrs_list
= Qnil
;
8535 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8537 || NILP (Fmemq (XCAR (tail
), exclude
)))
8541 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8542 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8543 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8545 ASET (attrs
, coding_attr_trans_tbl
,
8546 get_translation_table (attrs
, 1, NULL
));
8547 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8551 if (STRINGP (start
))
8552 p
= pbeg
= SDATA (start
);
8554 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8555 pend
= p
+ (end_byte
- start_byte
);
8557 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8558 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8560 work_table
= Fmake_char_table (Qnil
, Qnil
);
8563 if (ASCII_BYTE_P (*p
))
8567 c
= STRING_CHAR_ADVANCE (p
);
8568 if (!NILP (char_table_ref (work_table
, c
)))
8569 /* This character was already checked. Ignore it. */
8572 charset_map_loaded
= 0;
8573 for (tail
= coding_attrs_list
; CONSP (tail
);)
8578 else if (char_encodable_p (c
, elt
))
8580 else if (CONSP (XCDR (tail
)))
8582 XSETCAR (tail
, XCAR (XCDR (tail
)));
8583 XSETCDR (tail
, XCDR (XCDR (tail
)));
8587 XSETCAR (tail
, Qnil
);
8591 if (charset_map_loaded
)
8593 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8595 if (STRINGP (start
))
8596 pbeg
= SDATA (start
);
8598 pbeg
= BYTE_POS_ADDR (start_byte
);
8599 p
= pbeg
+ p_offset
;
8600 pend
= pbeg
+ pend_offset
;
8602 char_table_set (work_table
, c
, Qt
);
8606 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8607 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8608 if (! NILP (XCAR (tail
)))
8609 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8611 return safe_codings
;
8615 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8616 Sunencodable_char_position
, 3, 5, 0,
8618 Return position of first un-encodable character in a region.
8619 START and END specify the region and CODING-SYSTEM specifies the
8620 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8622 If optional 4th argument COUNT is non-nil, it specifies at most how
8623 many un-encodable characters to search. In this case, the value is a
8626 If optional 5th argument STRING is non-nil, it is a string to search
8627 for un-encodable characters. In that case, START and END are indexes
8629 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8632 struct coding_system coding
;
8633 Lisp_Object attrs
, charset_list
, translation_table
;
8634 Lisp_Object positions
;
8636 const unsigned char *p
, *stop
, *pend
;
8637 bool ascii_compatible
;
8639 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8640 attrs
= CODING_ID_ATTRS (coding
.id
);
8641 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8643 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8644 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8645 translation_table
= get_translation_table (attrs
, 1, NULL
);
8649 validate_region (&start
, &end
);
8650 from
= XINT (start
);
8652 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8653 || (ascii_compatible
8654 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8656 p
= CHAR_POS_ADDR (from
);
8657 pend
= CHAR_POS_ADDR (to
);
8658 if (from
< GPT
&& to
>= GPT
)
8665 CHECK_STRING (string
);
8666 CHECK_NATNUM (start
);
8668 if (! (XINT (start
) <= XINT (end
) && XINT (end
) <= SCHARS (string
)))
8669 args_out_of_range_3 (string
, start
, end
);
8670 from
= XINT (start
);
8672 if (! STRING_MULTIBYTE (string
))
8674 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8675 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8676 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8684 CHECK_NATNUM (count
);
8689 charset_map_loaded
= 0;
8694 if (ascii_compatible
)
8695 while (p
< stop
&& ASCII_BYTE_P (*p
))
8705 c
= STRING_CHAR_ADVANCE (p
);
8706 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8707 && ! char_charset (translate_char (translation_table
, c
),
8708 charset_list
, NULL
))
8710 positions
= Fcons (make_number (from
), positions
);
8717 if (charset_map_loaded
&& NILP (string
))
8719 p
= CHAR_POS_ADDR (from
);
8720 pend
= CHAR_POS_ADDR (to
);
8721 if (from
< GPT
&& to
>= GPT
)
8725 charset_map_loaded
= 0;
8729 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8733 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8734 Scheck_coding_systems_region
, 3, 3, 0,
8735 doc
: /* Check if the region is encodable by coding systems.
8737 START and END are buffer positions specifying the region.
8738 CODING-SYSTEM-LIST is a list of coding systems to check.
8740 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8741 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8742 whole region, POS0, POS1, ... are buffer positions where non-encodable
8743 characters are found.
8745 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8748 START may be a string. In that case, check if the string is
8749 encodable, and the value contains indices to the string instead of
8750 buffer positions. END is ignored.
8752 If the current buffer (or START if it is a string) is unibyte, the value
8754 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8757 ptrdiff_t start_byte
, end_byte
;
8759 const unsigned char *p
, *pbeg
, *pend
;
8761 Lisp_Object tail
, elt
, attrs
;
8763 if (STRINGP (start
))
8765 if (!STRING_MULTIBYTE (start
)
8766 || SCHARS (start
) == SBYTES (start
))
8769 end_byte
= SBYTES (start
);
8774 CHECK_NUMBER_COERCE_MARKER (start
);
8775 CHECK_NUMBER_COERCE_MARKER (end
);
8776 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8777 args_out_of_range (start
, end
);
8778 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8780 start_byte
= CHAR_TO_BYTE (XINT (start
));
8781 end_byte
= CHAR_TO_BYTE (XINT (end
));
8782 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8785 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8787 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8788 move_gap_both (XINT (start
), start_byte
);
8790 move_gap_both (XINT (end
), end_byte
);
8796 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8799 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8800 ASET (attrs
, coding_attr_trans_tbl
,
8801 get_translation_table (attrs
, 1, NULL
));
8802 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8805 if (STRINGP (start
))
8806 p
= pbeg
= SDATA (start
);
8808 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8809 pend
= p
+ (end_byte
- start_byte
);
8811 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8812 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8816 if (ASCII_BYTE_P (*p
))
8820 c
= STRING_CHAR_ADVANCE (p
);
8822 charset_map_loaded
= 0;
8823 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8825 elt
= XCDR (XCAR (tail
));
8826 if (! char_encodable_p (c
, XCAR (elt
)))
8827 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8829 if (charset_map_loaded
)
8831 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8833 if (STRINGP (start
))
8834 pbeg
= SDATA (start
);
8836 pbeg
= BYTE_POS_ADDR (start_byte
);
8837 p
= pbeg
+ p_offset
;
8838 pend
= pbeg
+ pend_offset
;
8846 for (; CONSP (tail
); tail
= XCDR (tail
))
8849 if (CONSP (XCDR (XCDR (elt
))))
8850 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8859 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8860 Lisp_Object coding_system
, Lisp_Object dst_object
,
8861 bool encodep
, bool norecord
)
8863 struct coding_system coding
;
8864 ptrdiff_t from
, from_byte
, to
, to_byte
;
8865 Lisp_Object src_object
;
8867 if (NILP (coding_system
))
8868 coding_system
= Qno_conversion
;
8870 CHECK_CODING_SYSTEM (coding_system
);
8871 src_object
= Fcurrent_buffer ();
8872 if (NILP (dst_object
))
8873 dst_object
= src_object
;
8874 else if (! EQ (dst_object
, Qt
))
8875 CHECK_BUFFER (dst_object
);
8877 validate_region (&start
, &end
);
8878 from
= XFASTINT (start
);
8879 from_byte
= CHAR_TO_BYTE (from
);
8880 to
= XFASTINT (end
);
8881 to_byte
= CHAR_TO_BYTE (to
);
8883 setup_coding_system (coding_system
, &coding
);
8884 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8887 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8890 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8893 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8895 return (BUFFERP (dst_object
)
8896 ? make_number (coding
.produced_char
)
8897 : coding
.dst_object
);
8901 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8902 3, 4, "r\nzCoding system: ",
8903 doc
: /* Decode the current region from the specified coding system.
8904 When called from a program, takes four arguments:
8905 START, END, CODING-SYSTEM, and DESTINATION.
8906 START and END are buffer positions.
8908 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8909 If nil, the region between START and END is replaced by the decoded text.
8910 If buffer, the decoded text is inserted in that buffer after point (point
8912 In those cases, the length of the decoded text is returned.
8913 If DESTINATION is t, the decoded text is returned.
8915 This function sets `last-coding-system-used' to the precise coding system
8916 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8917 not fully specified.) */)
8918 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8920 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8923 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8924 3, 4, "r\nzCoding system: ",
8925 doc
: /* Encode the current region by specified coding system.
8926 When called from a program, takes four arguments:
8927 START, END, CODING-SYSTEM and DESTINATION.
8928 START and END are buffer positions.
8930 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8931 If nil, the region between START and END is replace by the encoded text.
8932 If buffer, the encoded text is inserted in that buffer after point (point
8934 In those cases, the length of the encoded text is returned.
8935 If DESTINATION is t, the encoded text is returned.
8937 This function sets `last-coding-system-used' to the precise coding system
8938 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8939 not fully specified.) */)
8940 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8942 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8946 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8947 Lisp_Object dst_object
, bool encodep
, bool nocopy
,
8950 struct coding_system coding
;
8951 ptrdiff_t chars
, bytes
;
8953 CHECK_STRING (string
);
8954 if (NILP (coding_system
))
8957 Vlast_coding_system_used
= Qno_conversion
;
8958 if (NILP (dst_object
))
8959 return (nocopy
? Fcopy_sequence (string
) : string
);
8962 if (NILP (coding_system
))
8963 coding_system
= Qno_conversion
;
8965 CHECK_CODING_SYSTEM (coding_system
);
8966 if (NILP (dst_object
))
8968 else if (! EQ (dst_object
, Qt
))
8969 CHECK_BUFFER (dst_object
);
8971 setup_coding_system (coding_system
, &coding
);
8972 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8973 chars
= SCHARS (string
);
8974 bytes
= SBYTES (string
);
8976 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8978 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8980 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8982 return (BUFFERP (dst_object
)
8983 ? make_number (coding
.produced_char
)
8984 : coding
.dst_object
);
8988 /* Encode or decode STRING according to CODING_SYSTEM.
8989 Do not set Vlast_coding_system_used.
8991 This function is called only from macros DECODE_FILE and
8992 ENCODE_FILE, thus we ignore character composition. */
8995 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
8998 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9002 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9004 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9006 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9007 if the decoding operation is trivial.
9009 Optional fourth arg BUFFER non-nil means that the decoded text is
9010 inserted in that buffer after point (point does not move). In this
9011 case, the return value is the length of the decoded text.
9013 This function sets `last-coding-system-used' to the precise coding system
9014 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9015 not fully specified.) */)
9016 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9018 return code_convert_string (string
, coding_system
, buffer
,
9019 0, ! NILP (nocopy
), 0);
9022 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9024 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9026 Optional third arg NOCOPY non-nil means it is OK to return STRING
9027 itself if the encoding operation is trivial.
9029 Optional fourth arg BUFFER non-nil means that the encoded text is
9030 inserted in that buffer after point (point does not move). In this
9031 case, the return value is the length of the encoded text.
9033 This function sets `last-coding-system-used' to the precise coding system
9034 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9035 not fully specified.) */)
9036 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9038 return code_convert_string (string
, coding_system
, buffer
,
9039 1, ! NILP (nocopy
), 0);
9043 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9044 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9045 Return the corresponding character. */)
9048 Lisp_Object spec
, attrs
, val
;
9049 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9053 CHECK_NATNUM (code
);
9054 ch
= XFASTINT (code
);
9055 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9056 attrs
= AREF (spec
, 0);
9058 if (ASCII_BYTE_P (ch
)
9059 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9062 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9063 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9064 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9065 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9070 charset
= charset_roman
;
9072 else if (ch
>= 0xA0 && ch
< 0xDF)
9075 charset
= charset_kana
;
9079 EMACS_INT c1
= ch
>> 8;
9082 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9083 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9084 error ("Invalid code: %"pI
"d", ch
);
9087 charset
= charset_kanji
;
9089 c
= DECODE_CHAR (charset
, c
);
9091 error ("Invalid code: %"pI
"d", ch
);
9092 return make_number (c
);
9096 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9097 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9098 Return the corresponding code in SJIS. */)
9101 Lisp_Object spec
, attrs
, charset_list
;
9103 struct charset
*charset
;
9106 CHECK_CHARACTER (ch
);
9108 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9109 attrs
= AREF (spec
, 0);
9111 if (ASCII_CHAR_P (c
)
9112 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9115 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9116 charset
= char_charset (c
, charset_list
, &code
);
9117 if (code
== CHARSET_INVALID_CODE (charset
))
9118 error ("Can't encode by shift_jis encoding: %c", c
);
9121 return make_number (code
);
9124 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9125 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9126 Return the corresponding character. */)
9129 Lisp_Object spec
, attrs
, val
;
9130 struct charset
*charset_roman
, *charset_big5
, *charset
;
9134 CHECK_NATNUM (code
);
9135 ch
= XFASTINT (code
);
9136 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9137 attrs
= AREF (spec
, 0);
9139 if (ASCII_BYTE_P (ch
)
9140 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9143 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9144 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9145 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9150 charset
= charset_roman
;
9154 EMACS_INT b1
= ch
>> 8;
9156 if (b1
< 0xA1 || b1
> 0xFE
9157 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9158 error ("Invalid code: %"pI
"d", ch
);
9160 charset
= charset_big5
;
9162 c
= DECODE_CHAR (charset
, c
);
9164 error ("Invalid code: %"pI
"d", ch
);
9165 return make_number (c
);
9168 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9169 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9170 Return the corresponding character code in Big5. */)
9173 Lisp_Object spec
, attrs
, charset_list
;
9174 struct charset
*charset
;
9178 CHECK_CHARACTER (ch
);
9180 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9181 attrs
= AREF (spec
, 0);
9182 if (ASCII_CHAR_P (c
)
9183 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9186 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9187 charset
= char_charset (c
, charset_list
, &code
);
9188 if (code
== CHARSET_INVALID_CODE (charset
))
9189 error ("Can't encode by Big5 encoding: %c", c
);
9191 return make_number (code
);
9195 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9196 Sset_terminal_coding_system_internal
, 1, 2, 0,
9197 doc
: /* Internal use only. */)
9198 (Lisp_Object coding_system
, Lisp_Object terminal
)
9200 struct terminal
*term
= get_terminal (terminal
, 1);
9201 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9202 CHECK_SYMBOL (coding_system
);
9203 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9204 /* We had better not send unsafe characters to terminal. */
9205 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9206 /* Character composition should be disabled. */
9207 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9208 terminal_coding
->src_multibyte
= 1;
9209 terminal_coding
->dst_multibyte
= 0;
9211 (term
, (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
9212 ? coding_charset_list (terminal_coding
)
9213 : Fcons (make_number (charset_ascii
), Qnil
)));
9217 DEFUN ("set-safe-terminal-coding-system-internal",
9218 Fset_safe_terminal_coding_system_internal
,
9219 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9220 doc
: /* Internal use only. */)
9221 (Lisp_Object coding_system
)
9223 CHECK_SYMBOL (coding_system
);
9224 setup_coding_system (Fcheck_coding_system (coding_system
),
9225 &safe_terminal_coding
);
9226 /* Character composition should be disabled. */
9227 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9228 safe_terminal_coding
.src_multibyte
= 1;
9229 safe_terminal_coding
.dst_multibyte
= 0;
9233 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9234 Sterminal_coding_system
, 0, 1, 0,
9235 doc
: /* Return coding system specified for terminal output on the given terminal.
9236 TERMINAL may be a terminal object, a frame, or nil for the selected
9237 frame's terminal device. */)
9238 (Lisp_Object terminal
)
9240 struct coding_system
*terminal_coding
9241 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9242 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9244 /* For backward compatibility, return nil if it is `undecided'. */
9245 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9248 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9249 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9250 doc
: /* Internal use only. */)
9251 (Lisp_Object coding_system
, Lisp_Object terminal
)
9253 struct terminal
*t
= get_terminal (terminal
, 1);
9254 CHECK_SYMBOL (coding_system
);
9255 if (NILP (coding_system
))
9256 coding_system
= Qno_conversion
;
9258 Fcheck_coding_system (coding_system
);
9259 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9260 /* Character composition should be disabled. */
9261 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9262 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9266 DEFUN ("keyboard-coding-system",
9267 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9268 doc
: /* Return coding system specified for decoding keyboard input. */)
9269 (Lisp_Object terminal
)
9271 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9272 (get_terminal (terminal
, 1))->id
);
9276 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9277 Sfind_operation_coding_system
, 1, MANY
, 0,
9278 doc
: /* Choose a coding system for an operation based on the target name.
9279 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9280 DECODING-SYSTEM is the coding system to use for decoding
9281 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9282 for encoding (in case OPERATION does encoding).
9284 The first argument OPERATION specifies an I/O primitive:
9285 For file I/O, `insert-file-contents' or `write-region'.
9286 For process I/O, `call-process', `call-process-region', or `start-process'.
9287 For network I/O, `open-network-stream'.
9289 The remaining arguments should be the same arguments that were passed
9290 to the primitive. Depending on which primitive, one of those arguments
9291 is selected as the TARGET. For example, if OPERATION does file I/O,
9292 whichever argument specifies the file name is TARGET.
9294 TARGET has a meaning which depends on OPERATION:
9295 For file I/O, TARGET is a file name (except for the special case below).
9296 For process I/O, TARGET is a process name.
9297 For network I/O, TARGET is a service name or a port number.
9299 This function looks up what is specified for TARGET in
9300 `file-coding-system-alist', `process-coding-system-alist',
9301 or `network-coding-system-alist' depending on OPERATION.
9302 They may specify a coding system, a cons of coding systems,
9303 or a function symbol to call.
9304 In the last case, we call the function with one argument,
9305 which is a list of all the arguments given to this function.
9306 If the function can't decide a coding system, it can return
9307 `undecided' so that the normal code-detection is performed.
9309 If OPERATION is `insert-file-contents', the argument corresponding to
9310 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9311 file name to look up, and BUFFER is a buffer that contains the file's
9312 contents (not yet decoded). If `file-coding-system-alist' specifies a
9313 function to call for FILENAME, that function should examine the
9314 contents of BUFFER instead of reading the file.
9316 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9317 (ptrdiff_t nargs
, Lisp_Object
*args
)
9319 Lisp_Object operation
, target_idx
, target
, val
;
9320 register Lisp_Object chain
;
9323 error ("Too few arguments");
9324 operation
= args
[0];
9325 if (!SYMBOLP (operation
)
9326 || (target_idx
= Fget (operation
, Qtarget_idx
), !NATNUMP (target_idx
)))
9327 error ("Invalid first argument");
9328 if (nargs
<= 1 + XFASTINT (target_idx
))
9329 error ("Too few arguments for operation `%s'",
9330 SDATA (SYMBOL_NAME (operation
)));
9331 target
= args
[XFASTINT (target_idx
) + 1];
9332 if (!(STRINGP (target
)
9333 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9334 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9335 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9336 error ("Invalid argument %"pI
"d of operation `%s'",
9337 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9339 target
= XCAR (target
);
9341 chain
= ((EQ (operation
, Qinsert_file_contents
)
9342 || EQ (operation
, Qwrite_region
))
9343 ? Vfile_coding_system_alist
9344 : (EQ (operation
, Qopen_network_stream
)
9345 ? Vnetwork_coding_system_alist
9346 : Vprocess_coding_system_alist
));
9350 for (; CONSP (chain
); chain
= XCDR (chain
))
9356 && ((STRINGP (target
)
9357 && STRINGP (XCAR (elt
))
9358 && fast_string_match (XCAR (elt
), target
) >= 0)
9359 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9362 /* Here, if VAL is both a valid coding system and a valid
9363 function symbol, we return VAL as a coding system. */
9366 if (! SYMBOLP (val
))
9368 if (! NILP (Fcoding_system_p (val
)))
9369 return Fcons (val
, val
);
9370 if (! NILP (Ffboundp (val
)))
9372 /* We use call1 rather than safe_call1
9373 so as to get bug reports about functions called here
9374 which don't handle the current interface. */
9375 val
= call1 (val
, Flist (nargs
, args
));
9378 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9379 return Fcons (val
, val
);
9387 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9388 Sset_coding_system_priority
, 0, MANY
, 0,
9389 doc
: /* Assign higher priority to the coding systems given as arguments.
9390 If multiple coding systems belong to the same category,
9391 all but the first one are ignored.
9393 usage: (set-coding-system-priority &rest coding-systems) */)
9394 (ptrdiff_t nargs
, Lisp_Object
*args
)
9397 bool changed
[coding_category_max
];
9398 enum coding_category priorities
[coding_category_max
];
9400 memset (changed
, 0, sizeof changed
);
9402 for (i
= j
= 0; i
< nargs
; i
++)
9404 enum coding_category category
;
9405 Lisp_Object spec
, attrs
;
9407 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9408 attrs
= AREF (spec
, 0);
9409 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9410 if (changed
[category
])
9411 /* Ignore this coding system because a coding system of the
9412 same category already had a higher priority. */
9414 changed
[category
] = 1;
9415 priorities
[j
++] = category
;
9416 if (coding_categories
[category
].id
>= 0
9417 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9418 setup_coding_system (args
[i
], &coding_categories
[category
]);
9419 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9422 /* Now we have decided top J priorities. Reflect the order of the
9423 original priorities to the remaining priorities. */
9425 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9427 while (j
< coding_category_max
9428 && changed
[coding_priorities
[j
]])
9430 if (j
== coding_category_max
)
9432 priorities
[i
] = coding_priorities
[j
];
9435 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9437 /* Update `coding-category-list'. */
9438 Vcoding_category_list
= Qnil
;
9439 for (i
= coding_category_max
; i
-- > 0; )
9440 Vcoding_category_list
9441 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9442 Vcoding_category_list
);
9447 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9448 Scoding_system_priority_list
, 0, 1, 0,
9449 doc
: /* Return a list of coding systems ordered by their priorities.
9450 The list contains a subset of coding systems; i.e. coding systems
9451 assigned to each coding category (see `coding-category-list').
9453 HIGHESTP non-nil means just return the highest priority one. */)
9454 (Lisp_Object highestp
)
9459 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9461 enum coding_category category
= coding_priorities
[i
];
9462 int id
= coding_categories
[category
].id
;
9467 attrs
= CODING_ID_ATTRS (id
);
9468 if (! NILP (highestp
))
9469 return CODING_ATTR_BASE_NAME (attrs
);
9470 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9472 return Fnreverse (val
);
9475 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9478 make_subsidiaries (Lisp_Object base
)
9480 Lisp_Object subsidiaries
;
9481 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
9482 char *buf
= alloca (base_name_len
+ 6);
9485 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9486 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9487 for (i
= 0; i
< 3; i
++)
9489 strcpy (buf
+ base_name_len
, suffixes
[i
]);
9490 ASET (subsidiaries
, i
, intern (buf
));
9492 return subsidiaries
;
9496 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9497 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9498 doc
: /* For internal use only.
9499 usage: (define-coding-system-internal ...) */)
9500 (ptrdiff_t nargs
, Lisp_Object
*args
)
9503 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9504 Lisp_Object attrs
; /* Vector of attributes. */
9505 Lisp_Object eol_type
;
9506 Lisp_Object aliases
;
9507 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9508 enum coding_category category
;
9509 Lisp_Object tail
, val
;
9510 int max_charset_id
= 0;
9513 if (nargs
< coding_arg_max
)
9516 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9518 name
= args
[coding_arg_name
];
9519 CHECK_SYMBOL (name
);
9520 ASET (attrs
, coding_attr_base_name
, name
);
9522 val
= args
[coding_arg_mnemonic
];
9523 if (! STRINGP (val
))
9524 CHECK_CHARACTER (val
);
9525 ASET (attrs
, coding_attr_mnemonic
, val
);
9527 coding_type
= args
[coding_arg_coding_type
];
9528 CHECK_SYMBOL (coding_type
);
9529 ASET (attrs
, coding_attr_type
, coding_type
);
9531 charset_list
= args
[coding_arg_charset_list
];
9532 if (SYMBOLP (charset_list
))
9534 if (EQ (charset_list
, Qiso_2022
))
9536 if (! EQ (coding_type
, Qiso_2022
))
9537 error ("Invalid charset-list");
9538 charset_list
= Viso_2022_charset_list
;
9540 else if (EQ (charset_list
, Qemacs_mule
))
9542 if (! EQ (coding_type
, Qemacs_mule
))
9543 error ("Invalid charset-list");
9544 charset_list
= Vemacs_mule_charset_list
;
9546 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9548 if (! RANGED_INTEGERP (0, XCAR (tail
), INT_MAX
- 1))
9549 error ("Invalid charset-list");
9550 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9551 max_charset_id
= XFASTINT (XCAR (tail
));
9556 charset_list
= Fcopy_sequence (charset_list
);
9557 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9559 struct charset
*charset
;
9562 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9563 if (EQ (coding_type
, Qiso_2022
)
9564 ? CHARSET_ISO_FINAL (charset
) < 0
9565 : EQ (coding_type
, Qemacs_mule
)
9566 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9568 error ("Can't handle charset `%s'",
9569 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9571 XSETCAR (tail
, make_number (charset
->id
));
9572 if (max_charset_id
< charset
->id
)
9573 max_charset_id
= charset
->id
;
9576 ASET (attrs
, coding_attr_charset_list
, charset_list
);
9578 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9579 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9580 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9581 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9582 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
9584 ASET (attrs
, coding_attr_ascii_compat
, args
[coding_arg_ascii_compatible_p
]);
9586 val
= args
[coding_arg_decode_translation_table
];
9587 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9589 ASET (attrs
, coding_attr_decode_tbl
, val
);
9591 val
= args
[coding_arg_encode_translation_table
];
9592 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9594 ASET (attrs
, coding_attr_encode_tbl
, val
);
9596 val
= args
[coding_arg_post_read_conversion
];
9598 ASET (attrs
, coding_attr_post_read
, val
);
9600 val
= args
[coding_arg_pre_write_conversion
];
9602 ASET (attrs
, coding_attr_pre_write
, val
);
9604 val
= args
[coding_arg_default_char
];
9606 ASET (attrs
, coding_attr_default_char
, make_number (' '));
9609 CHECK_CHARACTER (val
);
9610 ASET (attrs
, coding_attr_default_char
, val
);
9613 val
= args
[coding_arg_for_unibyte
];
9614 ASET (attrs
, coding_attr_for_unibyte
, NILP (val
) ? Qnil
: Qt
);
9616 val
= args
[coding_arg_plist
];
9618 ASET (attrs
, coding_attr_plist
, val
);
9620 if (EQ (coding_type
, Qcharset
))
9622 /* Generate a lisp vector of 256 elements. Each element is nil,
9623 integer, or a list of charset IDs.
9625 If Nth element is nil, the byte code N is invalid in this
9628 If Nth element is a number NUM, N is the first byte of a
9629 charset whose ID is NUM.
9631 If Nth element is a list of charset IDs, N is the first byte
9632 of one of them. The list is sorted by dimensions of the
9633 charsets. A charset of smaller dimension comes first. */
9634 val
= Fmake_vector (make_number (256), Qnil
);
9636 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9638 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9639 int dim
= CHARSET_DIMENSION (charset
);
9640 int idx
= (dim
- 1) * 4;
9642 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9643 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9645 for (i
= charset
->code_space
[idx
];
9646 i
<= charset
->code_space
[idx
+ 1]; i
++)
9648 Lisp_Object tmp
, tmp2
;
9651 tmp
= AREF (val
, i
);
9654 else if (NUMBERP (tmp
))
9656 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9658 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9660 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9664 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9666 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9671 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9674 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9675 XSETCAR (tmp2
, XCAR (tail
));
9681 ASET (attrs
, coding_attr_charset_valids
, val
);
9682 category
= coding_category_charset
;
9684 else if (EQ (coding_type
, Qccl
))
9688 if (nargs
< coding_arg_ccl_max
)
9691 val
= args
[coding_arg_ccl_decoder
];
9692 CHECK_CCL_PROGRAM (val
);
9694 val
= Fcopy_sequence (val
);
9695 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9697 val
= args
[coding_arg_ccl_encoder
];
9698 CHECK_CCL_PROGRAM (val
);
9700 val
= Fcopy_sequence (val
);
9701 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9703 val
= args
[coding_arg_ccl_valids
];
9704 valids
= Fmake_string (make_number (256), make_number (0));
9705 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
9712 if (! (0 <= XINT (val
) && XINT (val
) <= 255))
9713 args_out_of_range_3 (val
, make_number (0), make_number (255));
9714 from
= to
= XINT (val
);
9719 CHECK_NATNUM_CAR (val
);
9720 CHECK_NUMBER_CDR (val
);
9721 if (XINT (XCAR (val
)) > 255)
9722 args_out_of_range_3 (XCAR (val
),
9723 make_number (0), make_number (255));
9724 from
= XINT (XCAR (val
));
9725 if (! (from
<= XINT (XCDR (val
)) && XINT (XCDR (val
)) <= 255))
9726 args_out_of_range_3 (XCDR (val
),
9727 XCAR (val
), make_number (255));
9728 to
= XINT (XCDR (val
));
9730 for (i
= from
; i
<= to
; i
++)
9731 SSET (valids
, i
, 1);
9733 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9735 category
= coding_category_ccl
;
9737 else if (EQ (coding_type
, Qutf_16
))
9739 Lisp_Object bom
, endian
;
9741 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9743 if (nargs
< coding_arg_utf16_max
)
9746 bom
= args
[coding_arg_utf16_bom
];
9747 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9751 CHECK_CODING_SYSTEM (val
);
9753 CHECK_CODING_SYSTEM (val
);
9755 ASET (attrs
, coding_attr_utf_bom
, bom
);
9757 endian
= args
[coding_arg_utf16_endian
];
9758 CHECK_SYMBOL (endian
);
9761 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9762 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9763 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9765 category
= (CONSP (bom
)
9766 ? coding_category_utf_16_auto
9768 ? (EQ (endian
, Qbig
)
9769 ? coding_category_utf_16_be_nosig
9770 : coding_category_utf_16_le_nosig
)
9771 : (EQ (endian
, Qbig
)
9772 ? coding_category_utf_16_be
9773 : coding_category_utf_16_le
));
9775 else if (EQ (coding_type
, Qiso_2022
))
9777 Lisp_Object initial
, reg_usage
, request
, flags
;
9779 if (nargs
< coding_arg_iso2022_max
)
9782 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9783 CHECK_VECTOR (initial
);
9784 for (i
= 0; i
< 4; i
++)
9786 val
= Faref (initial
, make_number (i
));
9789 struct charset
*charset
;
9791 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9792 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9793 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9794 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9797 ASET (initial
, i
, make_number (-1));
9800 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9801 CHECK_CONS (reg_usage
);
9802 CHECK_NUMBER_CAR (reg_usage
);
9803 CHECK_NUMBER_CDR (reg_usage
);
9805 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9806 for (tail
= request
; CONSP (tail
); tail
= XCDR (tail
))
9814 CHECK_CHARSET_GET_ID (tmp1
, id
);
9815 CHECK_NATNUM_CDR (val
);
9816 if (XINT (XCDR (val
)) >= 4)
9817 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
9818 XSETCAR (val
, make_number (id
));
9821 flags
= args
[coding_arg_iso2022_flags
];
9822 CHECK_NATNUM (flags
);
9823 i
= XINT (flags
) & INT_MAX
;
9824 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9825 i
|= CODING_ISO_FLAG_FULL_SUPPORT
;
9826 flags
= make_number (i
);
9828 ASET (attrs
, coding_attr_iso_initial
, initial
);
9829 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9830 ASET (attrs
, coding_attr_iso_request
, request
);
9831 ASET (attrs
, coding_attr_iso_flags
, flags
);
9832 setup_iso_safe_charsets (attrs
);
9834 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9835 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9836 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9837 ? coding_category_iso_7_else
9838 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9839 ? coding_category_iso_7
9840 : coding_category_iso_7_tight
);
9843 int id
= XINT (AREF (initial
, 1));
9845 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9846 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9848 ? coding_category_iso_8_else
9849 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9850 ? coding_category_iso_8_1
9851 : coding_category_iso_8_2
);
9853 if (category
!= coding_category_iso_8_1
9854 && category
!= coding_category_iso_8_2
)
9855 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9857 else if (EQ (coding_type
, Qemacs_mule
))
9859 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9860 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9861 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9862 category
= coding_category_emacs_mule
;
9864 else if (EQ (coding_type
, Qshift_jis
))
9867 struct charset
*charset
;
9869 if (XINT (Flength (charset_list
)) != 3
9870 && XINT (Flength (charset_list
)) != 4)
9871 error ("There should be three or four charsets");
9873 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9874 if (CHARSET_DIMENSION (charset
) != 1)
9875 error ("Dimension of charset %s is not one",
9876 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9877 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9878 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9880 charset_list
= XCDR (charset_list
);
9881 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9882 if (CHARSET_DIMENSION (charset
) != 1)
9883 error ("Dimension of charset %s is not one",
9884 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9886 charset_list
= XCDR (charset_list
);
9887 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9888 if (CHARSET_DIMENSION (charset
) != 2)
9889 error ("Dimension of charset %s is not two",
9890 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9892 charset_list
= XCDR (charset_list
);
9893 if (! NILP (charset_list
))
9895 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9896 if (CHARSET_DIMENSION (charset
) != 2)
9897 error ("Dimension of charset %s is not two",
9898 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9901 category
= coding_category_sjis
;
9902 Vsjis_coding_system
= name
;
9904 else if (EQ (coding_type
, Qbig5
))
9906 struct charset
*charset
;
9908 if (XINT (Flength (charset_list
)) != 2)
9909 error ("There should be just two charsets");
9911 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9912 if (CHARSET_DIMENSION (charset
) != 1)
9913 error ("Dimension of charset %s is not one",
9914 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9915 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9916 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9918 charset_list
= XCDR (charset_list
);
9919 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9920 if (CHARSET_DIMENSION (charset
) != 2)
9921 error ("Dimension of charset %s is not two",
9922 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9924 category
= coding_category_big5
;
9925 Vbig5_coding_system
= name
;
9927 else if (EQ (coding_type
, Qraw_text
))
9929 category
= coding_category_raw_text
;
9930 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9932 else if (EQ (coding_type
, Qutf_8
))
9936 if (nargs
< coding_arg_utf8_max
)
9939 bom
= args
[coding_arg_utf8_bom
];
9940 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9944 CHECK_CODING_SYSTEM (val
);
9946 CHECK_CODING_SYSTEM (val
);
9948 ASET (attrs
, coding_attr_utf_bom
, bom
);
9950 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9952 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9953 : NILP (bom
) ? coding_category_utf_8_nosig
9954 : coding_category_utf_8_sig
);
9956 else if (EQ (coding_type
, Qundecided
))
9957 category
= coding_category_undecided
;
9959 error ("Invalid coding system type: %s",
9960 SDATA (SYMBOL_NAME (coding_type
)));
9962 ASET (attrs
, coding_attr_category
, make_number (category
));
9963 ASET (attrs
, coding_attr_plist
,
9965 Fcons (AREF (Vcoding_category_table
, category
),
9966 CODING_ATTR_PLIST (attrs
))));
9967 ASET (attrs
, coding_attr_plist
,
9968 Fcons (QCascii_compatible_p
,
9969 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9970 CODING_ATTR_PLIST (attrs
))));
9972 eol_type
= args
[coding_arg_eol_type
];
9973 if (! NILP (eol_type
)
9974 && ! EQ (eol_type
, Qunix
)
9975 && ! EQ (eol_type
, Qdos
)
9976 && ! EQ (eol_type
, Qmac
))
9977 error ("Invalid eol-type");
9979 aliases
= Fcons (name
, Qnil
);
9981 if (NILP (eol_type
))
9983 eol_type
= make_subsidiaries (name
);
9984 for (i
= 0; i
< 3; i
++)
9986 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9988 this_name
= AREF (eol_type
, i
);
9989 this_aliases
= Fcons (this_name
, Qnil
);
9990 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9991 this_spec
= Fmake_vector (make_number (3), attrs
);
9992 ASET (this_spec
, 1, this_aliases
);
9993 ASET (this_spec
, 2, this_eol_type
);
9994 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9995 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9996 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9998 Vcoding_system_alist
9999 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10000 Vcoding_system_alist
);
10004 spec_vec
= Fmake_vector (make_number (3), attrs
);
10005 ASET (spec_vec
, 1, aliases
);
10006 ASET (spec_vec
, 2, eol_type
);
10008 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10009 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10010 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10012 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10013 Vcoding_system_alist
);
10016 int id
= coding_categories
[category
].id
;
10018 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10019 setup_coding_system (name
, &coding_categories
[category
]);
10025 return Fsignal (Qwrong_number_of_arguments
,
10026 Fcons (intern ("define-coding-system-internal"),
10027 make_number (nargs
)));
10031 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10033 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10034 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10036 Lisp_Object spec
, attrs
;
10038 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10039 attrs
= AREF (spec
, 0);
10040 if (EQ (prop
, QCmnemonic
))
10042 if (! STRINGP (val
))
10043 CHECK_CHARACTER (val
);
10044 ASET (attrs
, coding_attr_mnemonic
, val
);
10046 else if (EQ (prop
, QCdefault_char
))
10049 val
= make_number (' ');
10051 CHECK_CHARACTER (val
);
10052 ASET (attrs
, coding_attr_default_char
, val
);
10054 else if (EQ (prop
, QCdecode_translation_table
))
10056 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10057 CHECK_SYMBOL (val
);
10058 ASET (attrs
, coding_attr_decode_tbl
, val
);
10060 else if (EQ (prop
, QCencode_translation_table
))
10062 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10063 CHECK_SYMBOL (val
);
10064 ASET (attrs
, coding_attr_encode_tbl
, val
);
10066 else if (EQ (prop
, QCpost_read_conversion
))
10068 CHECK_SYMBOL (val
);
10069 ASET (attrs
, coding_attr_post_read
, val
);
10071 else if (EQ (prop
, QCpre_write_conversion
))
10073 CHECK_SYMBOL (val
);
10074 ASET (attrs
, coding_attr_pre_write
, val
);
10076 else if (EQ (prop
, QCascii_compatible_p
))
10078 ASET (attrs
, coding_attr_ascii_compat
, val
);
10081 ASET (attrs
, coding_attr_plist
,
10082 Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
));
10087 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10088 Sdefine_coding_system_alias
, 2, 2, 0,
10089 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10090 (Lisp_Object alias
, Lisp_Object coding_system
)
10092 Lisp_Object spec
, aliases
, eol_type
, val
;
10094 CHECK_SYMBOL (alias
);
10095 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10096 aliases
= AREF (spec
, 1);
10097 /* ALIASES should be a list of length more than zero, and the first
10098 element is a base coding system. Append ALIAS at the tail of the
10100 while (!NILP (XCDR (aliases
)))
10101 aliases
= XCDR (aliases
);
10102 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10104 eol_type
= AREF (spec
, 2);
10105 if (VECTORP (eol_type
))
10107 Lisp_Object subsidiaries
;
10110 subsidiaries
= make_subsidiaries (alias
);
10111 for (i
= 0; i
< 3; i
++)
10112 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10113 AREF (eol_type
, i
));
10116 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10117 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10118 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10120 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10121 Vcoding_system_alist
);
10126 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10128 doc
: /* Return the base of CODING-SYSTEM.
10129 Any alias or subsidiary coding system is not a base coding system. */)
10130 (Lisp_Object coding_system
)
10132 Lisp_Object spec
, attrs
;
10134 if (NILP (coding_system
))
10135 return (Qno_conversion
);
10136 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10137 attrs
= AREF (spec
, 0);
10138 return CODING_ATTR_BASE_NAME (attrs
);
10141 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10143 doc
: "Return the property list of CODING-SYSTEM.")
10144 (Lisp_Object coding_system
)
10146 Lisp_Object spec
, attrs
;
10148 if (NILP (coding_system
))
10149 coding_system
= Qno_conversion
;
10150 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10151 attrs
= AREF (spec
, 0);
10152 return CODING_ATTR_PLIST (attrs
);
10156 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10158 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10159 (Lisp_Object coding_system
)
10163 if (NILP (coding_system
))
10164 coding_system
= Qno_conversion
;
10165 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10166 return AREF (spec
, 1);
10169 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10170 Scoding_system_eol_type
, 1, 1, 0,
10171 doc
: /* Return eol-type of CODING-SYSTEM.
10172 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10174 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10175 and CR respectively.
10177 A vector value indicates that a format of end-of-line should be
10178 detected automatically. Nth element of the vector is the subsidiary
10179 coding system whose eol-type is N. */)
10180 (Lisp_Object coding_system
)
10182 Lisp_Object spec
, eol_type
;
10185 if (NILP (coding_system
))
10186 coding_system
= Qno_conversion
;
10187 if (! CODING_SYSTEM_P (coding_system
))
10189 spec
= CODING_SYSTEM_SPEC (coding_system
);
10190 eol_type
= AREF (spec
, 2);
10191 if (VECTORP (eol_type
))
10192 return Fcopy_sequence (eol_type
);
10193 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10194 return make_number (n
);
10200 /*** 9. Post-amble ***/
10203 init_coding_once (void)
10207 for (i
= 0; i
< coding_category_max
; i
++)
10209 coding_categories
[i
].id
= -1;
10210 coding_priorities
[i
] = i
;
10213 /* ISO2022 specific initialize routine. */
10214 for (i
= 0; i
< 0x20; i
++)
10215 iso_code_class
[i
] = ISO_control_0
;
10216 for (i
= 0x21; i
< 0x7F; i
++)
10217 iso_code_class
[i
] = ISO_graphic_plane_0
;
10218 for (i
= 0x80; i
< 0xA0; i
++)
10219 iso_code_class
[i
] = ISO_control_1
;
10220 for (i
= 0xA1; i
< 0xFF; i
++)
10221 iso_code_class
[i
] = ISO_graphic_plane_1
;
10222 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10223 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10224 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10225 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10226 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10227 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10228 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10229 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10230 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10232 for (i
= 0; i
< 256; i
++)
10234 emacs_mule_bytes
[i
] = 1;
10236 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10237 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10238 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10239 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10245 syms_of_coding (void)
10247 staticpro (&Vcoding_system_hash_table
);
10249 Lisp_Object args
[2];
10252 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10255 staticpro (&Vsjis_coding_system
);
10256 Vsjis_coding_system
= Qnil
;
10258 staticpro (&Vbig5_coding_system
);
10259 Vbig5_coding_system
= Qnil
;
10261 staticpro (&Vcode_conversion_reused_workbuf
);
10262 Vcode_conversion_reused_workbuf
= Qnil
;
10264 staticpro (&Vcode_conversion_workbuf_name
);
10265 Vcode_conversion_workbuf_name
= build_pure_c_string (" *code-conversion-work*");
10267 reused_workbuf_in_use
= 0;
10269 DEFSYM (Qcharset
, "charset");
10270 DEFSYM (Qtarget_idx
, "target-idx");
10271 DEFSYM (Qcoding_system_history
, "coding-system-history");
10272 Fset (Qcoding_system_history
, Qnil
);
10274 /* Target FILENAME is the first argument. */
10275 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10276 /* Target FILENAME is the third argument. */
10277 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10279 DEFSYM (Qcall_process
, "call-process");
10280 /* Target PROGRAM is the first argument. */
10281 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10283 DEFSYM (Qcall_process_region
, "call-process-region");
10284 /* Target PROGRAM is the third argument. */
10285 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10287 DEFSYM (Qstart_process
, "start-process");
10288 /* Target PROGRAM is the third argument. */
10289 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10291 DEFSYM (Qopen_network_stream
, "open-network-stream");
10292 /* Target SERVICE is the fourth argument. */
10293 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10295 DEFSYM (Qcoding_system
, "coding-system");
10296 DEFSYM (Qcoding_aliases
, "coding-aliases");
10298 DEFSYM (Qeol_type
, "eol-type");
10299 DEFSYM (Qunix
, "unix");
10300 DEFSYM (Qdos
, "dos");
10301 DEFSYM (Qmac
, "mac");
10303 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10304 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10305 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10306 DEFSYM (Qdefault_char
, "default-char");
10307 DEFSYM (Qundecided
, "undecided");
10308 DEFSYM (Qno_conversion
, "no-conversion");
10309 DEFSYM (Qraw_text
, "raw-text");
10311 DEFSYM (Qiso_2022
, "iso-2022");
10313 DEFSYM (Qutf_8
, "utf-8");
10314 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10316 #if defined (WINDOWSNT) || defined (CYGWIN)
10317 /* No, not utf-16-le: that one has a BOM. */
10318 DEFSYM (Qutf_16le
, "utf-16le");
10321 DEFSYM (Qutf_16
, "utf-16");
10322 DEFSYM (Qbig
, "big");
10323 DEFSYM (Qlittle
, "little");
10325 DEFSYM (Qshift_jis
, "shift-jis");
10326 DEFSYM (Qbig5
, "big5");
10328 DEFSYM (Qcoding_system_p
, "coding-system-p");
10330 DEFSYM (Qcoding_system_error
, "coding-system-error");
10331 Fput (Qcoding_system_error
, Qerror_conditions
,
10332 listn (CONSTYPE_PURE
, 2, Qcoding_system_error
, Qerror
));
10333 Fput (Qcoding_system_error
, Qerror_message
,
10334 build_pure_c_string ("Invalid coding system"));
10336 /* Intern this now in case it isn't already done.
10337 Setting this variable twice is harmless.
10338 But don't staticpro it here--that is done in alloc.c. */
10339 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10341 DEFSYM (Qtranslation_table
, "translation-table");
10342 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10343 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10344 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10345 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10347 DEFSYM (Qvalid_codes
, "valid-codes");
10349 DEFSYM (Qemacs_mule
, "emacs-mule");
10351 DEFSYM (QCcategory
, ":category");
10352 DEFSYM (QCmnemonic
, ":mnemonic");
10353 DEFSYM (QCdefault_char
, ":default-char");
10354 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10355 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10356 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10357 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10358 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10360 Vcoding_category_table
10361 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10362 staticpro (&Vcoding_category_table
);
10363 /* Followings are target of code detection. */
10364 ASET (Vcoding_category_table
, coding_category_iso_7
,
10365 intern_c_string ("coding-category-iso-7"));
10366 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10367 intern_c_string ("coding-category-iso-7-tight"));
10368 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10369 intern_c_string ("coding-category-iso-8-1"));
10370 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10371 intern_c_string ("coding-category-iso-8-2"));
10372 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10373 intern_c_string ("coding-category-iso-7-else"));
10374 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10375 intern_c_string ("coding-category-iso-8-else"));
10376 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10377 intern_c_string ("coding-category-utf-8-auto"));
10378 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10379 intern_c_string ("coding-category-utf-8"));
10380 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10381 intern_c_string ("coding-category-utf-8-sig"));
10382 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10383 intern_c_string ("coding-category-utf-16-be"));
10384 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10385 intern_c_string ("coding-category-utf-16-auto"));
10386 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10387 intern_c_string ("coding-category-utf-16-le"));
10388 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10389 intern_c_string ("coding-category-utf-16-be-nosig"));
10390 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10391 intern_c_string ("coding-category-utf-16-le-nosig"));
10392 ASET (Vcoding_category_table
, coding_category_charset
,
10393 intern_c_string ("coding-category-charset"));
10394 ASET (Vcoding_category_table
, coding_category_sjis
,
10395 intern_c_string ("coding-category-sjis"));
10396 ASET (Vcoding_category_table
, coding_category_big5
,
10397 intern_c_string ("coding-category-big5"));
10398 ASET (Vcoding_category_table
, coding_category_ccl
,
10399 intern_c_string ("coding-category-ccl"));
10400 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10401 intern_c_string ("coding-category-emacs-mule"));
10402 /* Followings are NOT target of code detection. */
10403 ASET (Vcoding_category_table
, coding_category_raw_text
,
10404 intern_c_string ("coding-category-raw-text"));
10405 ASET (Vcoding_category_table
, coding_category_undecided
,
10406 intern_c_string ("coding-category-undecided"));
10408 DEFSYM (Qinsufficient_source
, "insufficient-source");
10409 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10410 DEFSYM (Qinvalid_source
, "invalid-source");
10411 DEFSYM (Qinterrupted
, "interrupted");
10412 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10413 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10415 defsubr (&Scoding_system_p
);
10416 defsubr (&Sread_coding_system
);
10417 defsubr (&Sread_non_nil_coding_system
);
10418 defsubr (&Scheck_coding_system
);
10419 defsubr (&Sdetect_coding_region
);
10420 defsubr (&Sdetect_coding_string
);
10421 defsubr (&Sfind_coding_systems_region_internal
);
10422 defsubr (&Sunencodable_char_position
);
10423 defsubr (&Scheck_coding_systems_region
);
10424 defsubr (&Sdecode_coding_region
);
10425 defsubr (&Sencode_coding_region
);
10426 defsubr (&Sdecode_coding_string
);
10427 defsubr (&Sencode_coding_string
);
10428 defsubr (&Sdecode_sjis_char
);
10429 defsubr (&Sencode_sjis_char
);
10430 defsubr (&Sdecode_big5_char
);
10431 defsubr (&Sencode_big5_char
);
10432 defsubr (&Sset_terminal_coding_system_internal
);
10433 defsubr (&Sset_safe_terminal_coding_system_internal
);
10434 defsubr (&Sterminal_coding_system
);
10435 defsubr (&Sset_keyboard_coding_system_internal
);
10436 defsubr (&Skeyboard_coding_system
);
10437 defsubr (&Sfind_operation_coding_system
);
10438 defsubr (&Sset_coding_system_priority
);
10439 defsubr (&Sdefine_coding_system_internal
);
10440 defsubr (&Sdefine_coding_system_alias
);
10441 defsubr (&Scoding_system_put
);
10442 defsubr (&Scoding_system_base
);
10443 defsubr (&Scoding_system_plist
);
10444 defsubr (&Scoding_system_aliases
);
10445 defsubr (&Scoding_system_eol_type
);
10446 defsubr (&Scoding_system_priority_list
);
10448 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10449 doc
: /* List of coding systems.
10451 Do not alter the value of this variable manually. This variable should be
10452 updated by the functions `define-coding-system' and
10453 `define-coding-system-alias'. */);
10454 Vcoding_system_list
= Qnil
;
10456 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10457 doc
: /* Alist of coding system names.
10458 Each element is one element list of coding system name.
10459 This variable is given to `completing-read' as COLLECTION argument.
10461 Do not alter the value of this variable manually. This variable should be
10462 updated by the functions `make-coding-system' and
10463 `define-coding-system-alias'. */);
10464 Vcoding_system_alist
= Qnil
;
10466 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10467 doc
: /* List of coding-categories (symbols) ordered by priority.
10469 On detecting a coding system, Emacs tries code detection algorithms
10470 associated with each coding-category one by one in this order. When
10471 one algorithm agrees with a byte sequence of source text, the coding
10472 system bound to the corresponding coding-category is selected.
10474 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10478 Vcoding_category_list
= Qnil
;
10479 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10480 Vcoding_category_list
10481 = Fcons (AREF (Vcoding_category_table
, i
),
10482 Vcoding_category_list
);
10485 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10486 doc
: /* Specify the coding system for read operations.
10487 It is useful to bind this variable with `let', but do not set it globally.
10488 If the value is a coding system, it is used for decoding on read operation.
10489 If not, an appropriate element is used from one of the coding system alists.
10490 There are three such tables: `file-coding-system-alist',
10491 `process-coding-system-alist', and `network-coding-system-alist'. */);
10492 Vcoding_system_for_read
= Qnil
;
10494 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10495 doc
: /* Specify the coding system for write operations.
10496 Programs bind this variable with `let', but you should not set it globally.
10497 If the value is a coding system, it is used for encoding of output,
10498 when writing it to a file and when sending it to a file or subprocess.
10500 If this does not specify a coding system, an appropriate element
10501 is used from one of the coding system alists.
10502 There are three such tables: `file-coding-system-alist',
10503 `process-coding-system-alist', and `network-coding-system-alist'.
10504 For output to files, if the above procedure does not specify a coding system,
10505 the value of `buffer-file-coding-system' is used. */);
10506 Vcoding_system_for_write
= Qnil
;
10508 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10510 Coding system used in the latest file or process I/O. */);
10511 Vlast_coding_system_used
= Qnil
;
10513 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10515 Error status of the last code conversion.
10517 When an error was detected in the last code conversion, this variable
10518 is set to one of the following symbols.
10519 `insufficient-source'
10523 `insufficient-memory'
10524 When no error was detected, the value doesn't change. So, to check
10525 the error status of a code conversion by this variable, you must
10526 explicitly set this variable to nil before performing code
10528 Vlast_code_conversion_error
= Qnil
;
10530 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10532 *Non-nil means always inhibit code conversion of end-of-line format.
10533 See info node `Coding Systems' and info node `Text and Binary' concerning
10534 such conversion. */);
10535 inhibit_eol_conversion
= 0;
10537 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10539 Non-nil means process buffer inherits coding system of process output.
10540 Bind it to t if the process output is to be treated as if it were a file
10541 read from some filesystem. */);
10542 inherit_process_coding_system
= 0;
10544 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10546 Alist to decide a coding system to use for a file I/O operation.
10547 The format is ((PATTERN . VAL) ...),
10548 where PATTERN is a regular expression matching a file name,
10549 VAL is a coding system, a cons of coding systems, or a function symbol.
10550 If VAL is a coding system, it is used for both decoding and encoding
10552 If VAL is a cons of coding systems, the car part is used for decoding,
10553 and the cdr part is used for encoding.
10554 If VAL is a function symbol, the function must return a coding system
10555 or a cons of coding systems which are used as above. The function is
10556 called with an argument that is a list of the arguments with which
10557 `find-operation-coding-system' was called. If the function can't decide
10558 a coding system, it can return `undecided' so that the normal
10559 code-detection is performed.
10561 See also the function `find-operation-coding-system'
10562 and the variable `auto-coding-alist'. */);
10563 Vfile_coding_system_alist
= Qnil
;
10565 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10567 Alist to decide a coding system to use for a process I/O operation.
10568 The format is ((PATTERN . VAL) ...),
10569 where PATTERN is a regular expression matching a program name,
10570 VAL is a coding system, a cons of coding systems, or a function symbol.
10571 If VAL is a coding system, it is used for both decoding what received
10572 from the program and encoding what sent to the program.
10573 If VAL is a cons of coding systems, the car part is used for decoding,
10574 and the cdr part is used for encoding.
10575 If VAL is a function symbol, the function must return a coding system
10576 or a cons of coding systems which are used as above.
10578 See also the function `find-operation-coding-system'. */);
10579 Vprocess_coding_system_alist
= Qnil
;
10581 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10583 Alist to decide a coding system to use for a network I/O operation.
10584 The format is ((PATTERN . VAL) ...),
10585 where PATTERN is a regular expression matching a network service name
10586 or is a port number to connect to,
10587 VAL is a coding system, a cons of coding systems, or a function symbol.
10588 If VAL is a coding system, it is used for both decoding what received
10589 from the network stream and encoding what sent to the network stream.
10590 If VAL is a cons of coding systems, the car part is used for decoding,
10591 and the cdr part is used for encoding.
10592 If VAL is a function symbol, the function must return a coding system
10593 or a cons of coding systems which are used as above.
10595 See also the function `find-operation-coding-system'. */);
10596 Vnetwork_coding_system_alist
= Qnil
;
10598 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10599 doc
: /* Coding system to use with system messages.
10600 Also used for decoding keyboard input on X Window system. */);
10601 Vlocale_coding_system
= Qnil
;
10603 /* The eol mnemonics are reset in startup.el system-dependently. */
10604 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10606 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10607 eol_mnemonic_unix
= build_pure_c_string (":");
10609 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10611 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10612 eol_mnemonic_dos
= build_pure_c_string ("\\");
10614 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10616 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10617 eol_mnemonic_mac
= build_pure_c_string ("/");
10619 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10621 *String displayed in mode line when end-of-line format is not yet determined. */);
10622 eol_mnemonic_undecided
= build_pure_c_string (":");
10624 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10626 *Non-nil enables character translation while encoding and decoding. */);
10627 Venable_character_translation
= Qt
;
10629 DEFVAR_LISP ("standard-translation-table-for-decode",
10630 Vstandard_translation_table_for_decode
,
10631 doc
: /* Table for translating characters while decoding. */);
10632 Vstandard_translation_table_for_decode
= Qnil
;
10634 DEFVAR_LISP ("standard-translation-table-for-encode",
10635 Vstandard_translation_table_for_encode
,
10636 doc
: /* Table for translating characters while encoding. */);
10637 Vstandard_translation_table_for_encode
= Qnil
;
10639 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10640 doc
: /* Alist of charsets vs revision numbers.
10641 While encoding, if a charset (car part of an element) is found,
10642 designate it with the escape sequence identifying revision (cdr part
10643 of the element). */);
10644 Vcharset_revision_table
= Qnil
;
10646 DEFVAR_LISP ("default-process-coding-system",
10647 Vdefault_process_coding_system
,
10648 doc
: /* Cons of coding systems used for process I/O by default.
10649 The car part is used for decoding a process output,
10650 the cdr part is used for encoding a text to be sent to a process. */);
10651 Vdefault_process_coding_system
= Qnil
;
10653 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10655 Table of extra Latin codes in the range 128..159 (inclusive).
10656 This is a vector of length 256.
10657 If Nth element is non-nil, the existence of code N in a file
10658 \(or output of subprocess) doesn't prevent it to be detected as
10659 a coding system of ISO 2022 variant which has a flag
10660 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10661 or reading output of a subprocess.
10662 Only 128th through 159th elements have a meaning. */);
10663 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10665 DEFVAR_LISP ("select-safe-coding-system-function",
10666 Vselect_safe_coding_system_function
,
10668 Function to call to select safe coding system for encoding a text.
10670 If set, this function is called to force a user to select a proper
10671 coding system which can encode the text in the case that a default
10672 coding system used in each operation can't encode the text. The
10673 function should take care that the buffer is not modified while
10674 the coding system is being selected.
10676 The default value is `select-safe-coding-system' (which see). */);
10677 Vselect_safe_coding_system_function
= Qnil
;
10679 DEFVAR_BOOL ("coding-system-require-warning",
10680 coding_system_require_warning
,
10681 doc
: /* Internal use only.
10682 If non-nil, on writing a file, `select-safe-coding-system-function' is
10683 called even if `coding-system-for-write' is non-nil. The command
10684 `universal-coding-system-argument' binds this variable to t temporarily. */);
10685 coding_system_require_warning
= 0;
10688 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10689 inhibit_iso_escape_detection
,
10691 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10693 When Emacs reads text, it tries to detect how the text is encoded.
10694 This code detection is sensitive to escape sequences. If Emacs sees
10695 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10696 of the ISO2022 encodings, and decodes text by the corresponding coding
10697 system (e.g. `iso-2022-7bit').
10699 However, there may be a case that you want to read escape sequences in
10700 a file as is. In such a case, you can set this variable to non-nil.
10701 Then the code detection will ignore any escape sequences, and no text is
10702 detected as encoded in some ISO-2022 encoding. The result is that all
10703 escape sequences become visible in a buffer.
10705 The default value is nil, and it is strongly recommended not to change
10706 it. That is because many Emacs Lisp source files that contain
10707 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10708 in Emacs's distribution, and they won't be decoded correctly on
10709 reading if you suppress escape sequence detection.
10711 The other way to read escape sequences in a file without decoding is
10712 to explicitly specify some coding system that doesn't use ISO-2022
10713 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10714 inhibit_iso_escape_detection
= 0;
10716 DEFVAR_BOOL ("inhibit-null-byte-detection",
10717 inhibit_null_byte_detection
,
10718 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10719 By default, Emacs treats it as binary data, and does not attempt to
10720 decode it. The effect is as if you specified `no-conversion' for
10723 Set this to non-nil when a regular text happens to include null bytes.
10724 Examples are Index nodes of Info files and null-byte delimited output
10725 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10726 decode text as usual. */);
10727 inhibit_null_byte_detection
= 0;
10729 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10730 doc
: /* Char table for translating self-inserting characters.
10731 This is applied to the result of input methods, not their input.
10732 See also `keyboard-translate-table'.
10734 Use of this variable for character code unification was rendered
10735 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10736 internal character representation. */);
10737 Vtranslation_table_for_input
= Qnil
;
10740 Lisp_Object args
[coding_arg_max
];
10741 Lisp_Object plist
[16];
10744 for (i
= 0; i
< coding_arg_max
; i
++)
10747 plist
[0] = intern_c_string (":name");
10748 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10749 plist
[2] = intern_c_string (":mnemonic");
10750 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10751 plist
[4] = intern_c_string (":coding-type");
10752 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10753 plist
[6] = intern_c_string (":ascii-compatible-p");
10754 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10755 plist
[8] = intern_c_string (":default-char");
10756 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10757 plist
[10] = intern_c_string (":for-unibyte");
10758 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10759 plist
[12] = intern_c_string (":docstring");
10760 plist
[13] = build_pure_c_string ("Do no conversion.\n\
10762 When you visit a file with this coding, the file is read into a\n\
10763 unibyte buffer as is, thus each byte of a file is treated as a\n\
10765 plist
[14] = intern_c_string (":eol-type");
10766 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10767 args
[coding_arg_plist
] = Flist (16, plist
);
10768 Fdefine_coding_system_internal (coding_arg_max
, args
);
10770 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10771 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10772 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10773 /* This is already set.
10774 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10775 plist
[8] = intern_c_string (":charset-list");
10776 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10777 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10778 plist
[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10779 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10780 args
[coding_arg_plist
] = Flist (16, plist
);
10781 Fdefine_coding_system_internal (coding_arg_max
, args
);
10784 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10789 for (i
= 0; i
< coding_category_max
; i
++)
10790 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10792 #if defined (DOS_NT)
10793 system_eol_type
= Qdos
;
10795 system_eol_type
= Qunix
;
10797 staticpro (&system_eol_type
);
10801 emacs_strerror (int error_number
)
10805 synchronize_system_messages_locale ();
10806 str
= strerror (error_number
);
10808 if (! NILP (Vlocale_coding_system
))
10810 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10811 Vlocale_coding_system
,
10813 str
= SSDATA (dec
);