1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2012 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
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 Lisp_Object Qbuffer_file_coding_system
;
306 static Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
307 static Lisp_Object Qdefault_char
;
308 Lisp_Object Qno_conversion
, Qundecided
;
309 Lisp_Object Qcharset
, Qutf_8
;
310 static Lisp_Object Qiso_2022
;
311 static Lisp_Object Qutf_16
, Qshift_jis
, Qbig5
;
312 static Lisp_Object Qbig
, Qlittle
;
313 static Lisp_Object Qcoding_system_history
;
314 static Lisp_Object Qvalid_codes
;
315 static Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
316 static Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
317 static Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
318 static Lisp_Object QCascii_compatible_p
;
320 Lisp_Object Qcall_process
, Qcall_process_region
;
321 Lisp_Object Qstart_process
, Qopen_network_stream
;
322 static Lisp_Object Qtarget_idx
;
324 static Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
325 static Lisp_Object Qinterrupted
, Qinsufficient_memory
;
327 /* If a symbol has this property, evaluate the value to define the
328 symbol as a coding system. */
329 static Lisp_Object Qcoding_system_define_form
;
331 /* Format of end-of-line decided by system. This is Qunix on
332 Unix and Mac, Qdos on DOS/Windows.
333 This has an effect only for external encoding (i.e. for output to
334 file and process), not for in-buffer or Lisp string encoding. */
335 static Lisp_Object system_eol_type
;
339 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
341 /* Coding system emacs-mule and raw-text are for converting only
342 end-of-line format. */
343 Lisp_Object Qemacs_mule
, Qraw_text
;
344 Lisp_Object Qutf_8_emacs
;
346 /* Coding-systems are handed between Emacs Lisp programs and C internal
347 routines by the following three variables. */
348 /* Coding system to be used to encode text for terminal display when
349 terminal coding system is nil. */
350 struct coding_system safe_terminal_coding
;
354 Lisp_Object Qtranslation_table
;
355 Lisp_Object Qtranslation_table_id
;
356 static Lisp_Object Qtranslation_table_for_decode
;
357 static Lisp_Object Qtranslation_table_for_encode
;
359 /* Two special coding systems. */
360 static Lisp_Object Vsjis_coding_system
;
361 static Lisp_Object Vbig5_coding_system
;
363 /* ISO2022 section */
365 #define CODING_ISO_INITIAL(coding, reg) \
366 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
367 coding_attr_iso_initial), \
371 #define CODING_ISO_REQUEST(coding, charset_id) \
372 (((charset_id) <= (coding)->max_charset_id \
373 ? ((coding)->safe_charsets[charset_id] != 255 \
374 ? (coding)->safe_charsets[charset_id] \
379 #define CODING_ISO_FLAGS(coding) \
380 ((coding)->spec.iso_2022.flags)
381 #define CODING_ISO_DESIGNATION(coding, reg) \
382 ((coding)->spec.iso_2022.current_designation[reg])
383 #define CODING_ISO_INVOCATION(coding, plane) \
384 ((coding)->spec.iso_2022.current_invocation[plane])
385 #define CODING_ISO_SINGLE_SHIFTING(coding) \
386 ((coding)->spec.iso_2022.single_shifting)
387 #define CODING_ISO_BOL(coding) \
388 ((coding)->spec.iso_2022.bol)
389 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
390 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
391 #define CODING_ISO_CMP_STATUS(coding) \
392 (&(coding)->spec.iso_2022.cmp_status)
393 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
394 ((coding)->spec.iso_2022.ctext_extended_segment_len)
395 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
396 ((coding)->spec.iso_2022.embedded_utf_8)
398 /* Control characters of ISO2022. */
399 /* code */ /* function */
400 #define ISO_CODE_SO 0x0E /* shift-out */
401 #define ISO_CODE_SI 0x0F /* shift-in */
402 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
403 #define ISO_CODE_ESC 0x1B /* escape */
404 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
405 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
406 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
408 /* All code (1-byte) of ISO2022 is classified into one of the
410 enum iso_code_class_type
412 ISO_control_0
, /* Control codes in the range
413 0x00..0x1F and 0x7F, except for the
414 following 5 codes. */
415 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
416 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
417 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
418 ISO_escape
, /* ISO_CODE_SO (0x1B) */
419 ISO_control_1
, /* Control codes in the range
420 0x80..0x9F, except for the
421 following 3 codes. */
422 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
423 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
424 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
425 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
426 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
427 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
428 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
431 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
432 `iso-flags' attribute of an iso2022 coding system. */
434 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
435 instead of the correct short-form sequence (e.g. ESC $ A). */
436 #define CODING_ISO_FLAG_LONG_FORM 0x0001
438 /* If set, reset graphic planes and registers at end-of-line to the
440 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
442 /* If set, reset graphic planes and registers before any control
443 characters to the initial state. */
444 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
446 /* If set, encode by 7-bit environment. */
447 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
449 /* If set, use locking-shift function. */
450 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
452 /* If set, use single-shift function. Overwrite
453 CODING_ISO_FLAG_LOCKING_SHIFT. */
454 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
456 /* If set, use designation escape sequence. */
457 #define CODING_ISO_FLAG_DESIGNATION 0x0040
459 /* If set, produce revision number sequence. */
460 #define CODING_ISO_FLAG_REVISION 0x0080
462 /* If set, produce ISO6429's direction specifying sequence. */
463 #define CODING_ISO_FLAG_DIRECTION 0x0100
465 /* If set, assume designation states are reset at beginning of line on
467 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
469 /* If set, designation sequence should be placed at beginning of line
471 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
473 /* If set, do not encode unsafe characters on output. */
474 #define CODING_ISO_FLAG_SAFE 0x0800
476 /* If set, extra latin codes (128..159) are accepted as a valid code
478 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
480 #define CODING_ISO_FLAG_COMPOSITION 0x2000
482 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
484 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
486 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
488 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
490 /* A character to be produced on output if encoding of the original
491 character is prohibited by CODING_ISO_FLAG_SAFE. */
492 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
495 #define CODING_UTF_8_BOM(coding) \
496 ((coding)->spec.utf_8_bom)
499 #define CODING_UTF_16_BOM(coding) \
500 ((coding)->spec.utf_16.bom)
502 #define CODING_UTF_16_ENDIAN(coding) \
503 ((coding)->spec.utf_16.endian)
505 #define CODING_UTF_16_SURROGATE(coding) \
506 ((coding)->spec.utf_16.surrogate)
510 #define CODING_CCL_DECODER(coding) \
511 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
512 #define CODING_CCL_ENCODER(coding) \
513 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
514 #define CODING_CCL_VALIDS(coding) \
515 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
517 /* Index for each coding category in `coding_categories' */
521 coding_category_iso_7
,
522 coding_category_iso_7_tight
,
523 coding_category_iso_8_1
,
524 coding_category_iso_8_2
,
525 coding_category_iso_7_else
,
526 coding_category_iso_8_else
,
527 coding_category_utf_8_auto
,
528 coding_category_utf_8_nosig
,
529 coding_category_utf_8_sig
,
530 coding_category_utf_16_auto
,
531 coding_category_utf_16_be
,
532 coding_category_utf_16_le
,
533 coding_category_utf_16_be_nosig
,
534 coding_category_utf_16_le_nosig
,
535 coding_category_charset
,
536 coding_category_sjis
,
537 coding_category_big5
,
539 coding_category_emacs_mule
,
540 /* All above are targets of code detection. */
541 coding_category_raw_text
,
542 coding_category_undecided
,
546 /* Definitions of flag bits used in detect_coding_XXXX. */
547 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
548 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
549 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
550 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
551 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
552 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
553 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
554 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
555 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
556 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
557 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
558 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
559 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
560 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
561 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
562 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
563 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
564 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
565 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
566 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
568 /* This value is returned if detect_coding_mask () find nothing other
569 than ASCII characters. */
570 #define CATEGORY_MASK_ANY \
571 (CATEGORY_MASK_ISO_7 \
572 | CATEGORY_MASK_ISO_7_TIGHT \
573 | CATEGORY_MASK_ISO_8_1 \
574 | CATEGORY_MASK_ISO_8_2 \
575 | CATEGORY_MASK_ISO_7_ELSE \
576 | CATEGORY_MASK_ISO_8_ELSE \
577 | CATEGORY_MASK_UTF_8_AUTO \
578 | CATEGORY_MASK_UTF_8_NOSIG \
579 | CATEGORY_MASK_UTF_8_SIG \
580 | CATEGORY_MASK_UTF_16_AUTO \
581 | CATEGORY_MASK_UTF_16_BE \
582 | CATEGORY_MASK_UTF_16_LE \
583 | CATEGORY_MASK_UTF_16_BE_NOSIG \
584 | CATEGORY_MASK_UTF_16_LE_NOSIG \
585 | CATEGORY_MASK_CHARSET \
586 | CATEGORY_MASK_SJIS \
587 | CATEGORY_MASK_BIG5 \
588 | CATEGORY_MASK_CCL \
589 | CATEGORY_MASK_EMACS_MULE)
592 #define CATEGORY_MASK_ISO_7BIT \
593 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
595 #define CATEGORY_MASK_ISO_8BIT \
596 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
598 #define CATEGORY_MASK_ISO_ELSE \
599 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
601 #define CATEGORY_MASK_ISO_ESCAPE \
602 (CATEGORY_MASK_ISO_7 \
603 | CATEGORY_MASK_ISO_7_TIGHT \
604 | CATEGORY_MASK_ISO_7_ELSE \
605 | CATEGORY_MASK_ISO_8_ELSE)
607 #define CATEGORY_MASK_ISO \
608 ( CATEGORY_MASK_ISO_7BIT \
609 | CATEGORY_MASK_ISO_8BIT \
610 | CATEGORY_MASK_ISO_ELSE)
612 #define CATEGORY_MASK_UTF_16 \
613 (CATEGORY_MASK_UTF_16_AUTO \
614 | CATEGORY_MASK_UTF_16_BE \
615 | CATEGORY_MASK_UTF_16_LE \
616 | CATEGORY_MASK_UTF_16_BE_NOSIG \
617 | CATEGORY_MASK_UTF_16_LE_NOSIG)
619 #define CATEGORY_MASK_UTF_8 \
620 (CATEGORY_MASK_UTF_8_AUTO \
621 | CATEGORY_MASK_UTF_8_NOSIG \
622 | CATEGORY_MASK_UTF_8_SIG)
624 /* Table of coding categories (Lisp symbols). This variable is for
625 internal use only. */
626 static Lisp_Object Vcoding_category_table
;
628 /* Table of coding-categories ordered by priority. */
629 static enum coding_category coding_priorities
[coding_category_max
];
631 /* Nth element is a coding context for the coding system bound to the
632 Nth coding category. */
633 static struct coding_system coding_categories
[coding_category_max
];
635 /*** Commonly used macros and functions ***/
638 #define min(a, b) ((a) < (b) ? (a) : (b))
641 #define max(a, b) ((a) > (b) ? (a) : (b))
644 #define CODING_GET_INFO(coding, attrs, charset_list) \
646 (attrs) = CODING_ID_ATTRS ((coding)->id); \
647 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
651 /* Safely get one byte from the source text pointed by SRC which ends
652 at SRC_END, and set C to that byte. If there are not enough bytes
653 in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
654 and a multibyte character is found at SRC, set C to the
655 negative value of the character code. The caller should declare
656 and set these variables appropriately in advance:
657 src, src_end, multibytep */
659 #define ONE_MORE_BYTE(c) \
661 if (src == src_end) \
663 if (src_base < src) \
664 record_conversion_result \
665 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
666 goto no_more_source; \
669 if (multibytep && (c & 0x80)) \
671 if ((c & 0xFE) == 0xC0) \
672 c = ((c & 1) << 6) | *src++; \
676 c = - string_char (src, &src, NULL); \
677 record_conversion_result \
678 (coding, CODING_RESULT_INVALID_SRC); \
684 /* Safely get two bytes from the source text pointed by SRC which ends
685 at SRC_END, and set C1 and C2 to those bytes while skipping the
686 heading multibyte characters. If there are not enough bytes in the
687 source, it jumps to 'no_more_source'. If MULTIBYTEP and
688 a multibyte character is found for C2, set C2 to the negative value
689 of the character code. The caller should declare and set these
690 variables appropriately in advance:
691 src, src_end, multibytep
692 It is intended that this macro is used in detect_coding_utf_16. */
694 #define TWO_MORE_BYTES(c1, c2) \
697 if (src == src_end) \
698 goto no_more_source; \
700 if (multibytep && (c1 & 0x80)) \
702 if ((c1 & 0xFE) == 0xC0) \
703 c1 = ((c1 & 1) << 6) | *src++; \
706 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
711 if (src == src_end) \
712 goto no_more_source; \
714 if (multibytep && (c2 & 0x80)) \
716 if ((c2 & 0xFE) == 0xC0) \
717 c2 = ((c2 & 1) << 6) | *src++; \
724 /* Store a byte C in the place pointed by DST and increment DST to the
725 next free point, and increment PRODUCED_CHARS. The caller should
726 assure that C is 0..127, and declare and set the variable `dst'
727 appropriately in advance.
731 #define EMIT_ONE_ASCII_BYTE(c) \
738 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
740 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
742 produced_chars += 2; \
743 *dst++ = (c1), *dst++ = (c2); \
747 /* Store a byte C in the place pointed by DST and increment DST to the
748 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
749 store in an appropriate multibyte form. The caller should
750 declare and set the variables `dst' and `multibytep' appropriately
753 #define EMIT_ONE_BYTE(c) \
760 ch = BYTE8_TO_CHAR (ch); \
761 CHAR_STRING_ADVANCE (ch, dst); \
768 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
770 #define EMIT_TWO_BYTES(c1, c2) \
772 produced_chars += 2; \
779 ch = BYTE8_TO_CHAR (ch); \
780 CHAR_STRING_ADVANCE (ch, dst); \
783 ch = BYTE8_TO_CHAR (ch); \
784 CHAR_STRING_ADVANCE (ch, dst); \
794 #define EMIT_THREE_BYTES(c1, c2, c3) \
796 EMIT_ONE_BYTE (c1); \
797 EMIT_TWO_BYTES (c2, c3); \
801 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
803 EMIT_TWO_BYTES (c1, c2); \
804 EMIT_TWO_BYTES (c3, c4); \
809 record_conversion_result (struct coding_system
*coding
,
810 enum coding_result_code result
)
812 coding
->result
= result
;
815 case CODING_RESULT_INSUFFICIENT_SRC
:
816 Vlast_code_conversion_error
= Qinsufficient_source
;
818 case CODING_RESULT_INCONSISTENT_EOL
:
819 Vlast_code_conversion_error
= Qinconsistent_eol
;
821 case CODING_RESULT_INVALID_SRC
:
822 Vlast_code_conversion_error
= Qinvalid_source
;
824 case CODING_RESULT_INTERRUPT
:
825 Vlast_code_conversion_error
= Qinterrupted
;
827 case CODING_RESULT_INSUFFICIENT_MEM
:
828 Vlast_code_conversion_error
= Qinsufficient_memory
;
830 case CODING_RESULT_INSUFFICIENT_DST
:
831 /* Don't record this error in Vlast_code_conversion_error
832 because it happens just temporarily and is resolved when the
833 whole conversion is finished. */
835 case CODING_RESULT_SUCCESS
:
838 Vlast_code_conversion_error
= intern ("Unknown error");
842 /* These wrapper macros are used to preserve validity of pointers into
843 buffer text across calls to decode_char, encode_char, etc, which
844 could cause relocation of buffers if it loads a charset map,
845 because loading a charset map allocates large structures. */
847 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
851 charset_map_loaded = 0; \
852 c = DECODE_CHAR (charset, code); \
853 if (charset_map_loaded \
854 && (offset = coding_change_source (coding))) \
857 src_base += offset; \
862 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
866 charset_map_loaded = 0; \
867 code = ENCODE_CHAR (charset, c); \
868 if (charset_map_loaded \
869 && (offset = coding_change_destination (coding))) \
876 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
880 charset_map_loaded = 0; \
881 charset = char_charset (c, charset_list, code_return); \
882 if (charset_map_loaded \
883 && (offset = coding_change_destination (coding))) \
890 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
894 charset_map_loaded = 0; \
895 result = CHAR_CHARSET_P (c, charset); \
896 if (charset_map_loaded \
897 && (offset = coding_change_destination (coding))) \
905 /* If there are at least BYTES length of room at dst, allocate memory
906 for coding->destination and update dst and dst_end. We don't have
907 to take care of coding->source which will be relocated. It is
908 handled by calling coding_set_source in encode_coding. */
910 #define ASSURE_DESTINATION(bytes) \
912 if (dst + (bytes) >= dst_end) \
914 ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
916 dst = alloc_destination (coding, more_bytes, dst); \
917 dst_end = coding->destination + coding->dst_bytes; \
922 /* Store multibyte form of the character C in P, and advance P to the
923 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
924 never calls MAYBE_UNIFY_CHAR. */
926 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
928 if ((c) <= MAX_1_BYTE_CHAR) \
930 else if ((c) <= MAX_2_BYTE_CHAR) \
931 *(p)++ = (0xC0 | ((c) >> 6)), \
932 *(p)++ = (0x80 | ((c) & 0x3F)); \
933 else if ((c) <= MAX_3_BYTE_CHAR) \
934 *(p)++ = (0xE0 | ((c) >> 12)), \
935 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
936 *(p)++ = (0x80 | ((c) & 0x3F)); \
937 else if ((c) <= MAX_4_BYTE_CHAR) \
938 *(p)++ = (0xF0 | (c >> 18)), \
939 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
940 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
941 *(p)++ = (0x80 | (c & 0x3F)); \
942 else if ((c) <= MAX_5_BYTE_CHAR) \
944 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
945 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
946 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
947 *(p)++ = (0x80 | (c & 0x3F)); \
949 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
953 /* Return the character code of character whose multibyte form is at
954 P, and advance P to the end of the multibyte form. This is like
955 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
957 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
960 : ! ((p)[0] & 0x20) \
962 ((((p)[-2] & 0x1F) << 6) \
964 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
965 : ! ((p)[0] & 0x10) \
967 ((((p)[-3] & 0x0F) << 12) \
968 | (((p)[-2] & 0x3F) << 6) \
969 | ((p)[-1] & 0x3F))) \
970 : ! ((p)[0] & 0x08) \
972 ((((p)[-4] & 0xF) << 18) \
973 | (((p)[-3] & 0x3F) << 12) \
974 | (((p)[-2] & 0x3F) << 6) \
975 | ((p)[-1] & 0x3F))) \
977 ((((p)[-4] & 0x3F) << 18) \
978 | (((p)[-3] & 0x3F) << 12) \
979 | (((p)[-2] & 0x3F) << 6) \
980 | ((p)[-1] & 0x3F))))
983 /* Set coding->source from coding->src_object. */
986 coding_set_source (struct coding_system
*coding
)
988 if (BUFFERP (coding
->src_object
))
990 struct buffer
*buf
= XBUFFER (coding
->src_object
);
992 if (coding
->src_pos
< 0)
993 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
995 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
997 else if (STRINGP (coding
->src_object
))
999 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1003 /* Otherwise, the source is C string and is never relocated
1004 automatically. Thus we don't have to update anything. */
1009 /* Set coding->source from coding->src_object, and return how many
1010 bytes coding->source was changed. */
1013 coding_change_source (struct coding_system
*coding
)
1015 const unsigned char *orig
= coding
->source
;
1016 coding_set_source (coding
);
1017 return coding
->source
- orig
;
1021 /* Set coding->destination from coding->dst_object. */
1024 coding_set_destination (struct coding_system
*coding
)
1026 if (BUFFERP (coding
->dst_object
))
1028 if (BUFFERP (coding
->src_object
) && coding
->src_pos
< 0)
1030 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1031 coding
->dst_bytes
= (GAP_END_ADDR
1032 - (coding
->src_bytes
- coding
->consumed
)
1033 - coding
->destination
);
1037 /* We are sure that coding->dst_pos_byte is before the gap
1039 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1040 + coding
->dst_pos_byte
- BEG_BYTE
);
1041 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1042 - coding
->destination
);
1047 /* Otherwise, the destination is C string and is never relocated
1048 automatically. Thus we don't have to update anything. */
1053 /* Set coding->destination from coding->dst_object, and return how
1054 many bytes coding->destination was changed. */
1057 coding_change_destination (struct coding_system
*coding
)
1059 const unsigned char *orig
= coding
->destination
;
1060 coding_set_destination (coding
);
1061 return coding
->destination
- orig
;
1066 coding_alloc_by_realloc (struct coding_system
*coding
, ptrdiff_t bytes
)
1068 if (STRING_BYTES_BOUND
- coding
->dst_bytes
< bytes
)
1070 coding
->destination
= xrealloc (coding
->destination
,
1071 coding
->dst_bytes
+ bytes
);
1072 coding
->dst_bytes
+= bytes
;
1076 coding_alloc_by_making_gap (struct coding_system
*coding
,
1077 ptrdiff_t gap_head_used
, ptrdiff_t bytes
)
1079 if (EQ (coding
->src_object
, coding
->dst_object
))
1081 /* The gap may contain the produced data at the head and not-yet
1082 consumed data at the tail. To preserve those data, we at
1083 first make the gap size to zero, then increase the gap
1085 ptrdiff_t add
= GAP_SIZE
;
1087 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1088 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1090 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1091 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1095 Lisp_Object this_buffer
;
1097 this_buffer
= Fcurrent_buffer ();
1098 set_buffer_internal (XBUFFER (coding
->dst_object
));
1100 set_buffer_internal (XBUFFER (this_buffer
));
1105 static unsigned char *
1106 alloc_destination (struct coding_system
*coding
, ptrdiff_t nbytes
,
1109 ptrdiff_t offset
= dst
- coding
->destination
;
1111 if (BUFFERP (coding
->dst_object
))
1113 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1115 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1118 coding_alloc_by_realloc (coding
, nbytes
);
1119 coding_set_destination (coding
);
1120 dst
= coding
->destination
+ offset
;
1124 /** Macros for annotations. */
1126 /* An annotation data is stored in the array coding->charbuf in this
1128 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1129 LENGTH is the number of elements in the annotation.
1130 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1131 NCHARS is the number of characters in the text annotated.
1133 The format of the following elements depend on ANNOTATION_MASK.
1135 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1137 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1139 NBYTES is the number of bytes specified in the header part of
1140 old-style emacs-mule encoding, or 0 for the other kind of
1143 METHOD is one of enum composition_method.
1145 Optional COMPOSITION-COMPONENTS are characters and composition
1148 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1151 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1152 recover from an invalid annotation, and should be skipped by
1153 produce_annotation. */
1155 /* Maximum length of the header of annotation data. */
1156 #define MAX_ANNOTATION_LENGTH 5
1158 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1160 *(buf)++ = -(len); \
1161 *(buf)++ = (mask); \
1162 *(buf)++ = (nchars); \
1163 coding->annotated = 1; \
1166 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1168 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1174 #define ADD_CHARSET_DATA(buf, nchars, id) \
1176 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1181 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1188 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1189 Return true if a text is encoded in UTF-8. */
1191 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1192 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1193 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1194 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1195 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1196 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1198 #define UTF_8_BOM_1 0xEF
1199 #define UTF_8_BOM_2 0xBB
1200 #define UTF_8_BOM_3 0xBF
1203 detect_coding_utf_8 (struct coding_system
*coding
,
1204 struct coding_detection_info
*detect_info
)
1206 const unsigned char *src
= coding
->source
, *src_base
;
1207 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1208 bool multibytep
= coding
->src_multibyte
;
1209 ptrdiff_t consumed_chars
= 0;
1213 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1214 /* A coding system of this category is always ASCII compatible. */
1215 src
+= coding
->head_ascii
;
1219 int c
, c1
, c2
, c3
, c4
;
1223 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1226 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1228 if (UTF_8_2_OCTET_LEADING_P (c
))
1234 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1236 if (UTF_8_3_OCTET_LEADING_P (c
))
1239 if (src_base
== coding
->source
1240 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1245 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1247 if (UTF_8_4_OCTET_LEADING_P (c
))
1253 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1255 if (UTF_8_5_OCTET_LEADING_P (c
))
1262 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1266 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1268 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1273 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1274 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1278 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1280 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1287 decode_coding_utf_8 (struct coding_system
*coding
)
1289 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1290 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1291 const unsigned char *src_base
;
1292 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1293 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1294 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1295 bool multibytep
= coding
->src_multibyte
;
1296 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1298 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1299 int byte_after_cr
= -1;
1301 if (bom
!= utf_without_bom
)
1307 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1312 if (! UTF_8_EXTRA_OCTET_P (c2
))
1317 if (! UTF_8_EXTRA_OCTET_P (c3
))
1321 if ((c1
!= UTF_8_BOM_1
)
1322 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1325 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1330 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1334 int c
, c1
, c2
, c3
, c4
, c5
;
1337 consumed_chars_base
= consumed_chars
;
1339 if (charbuf
>= charbuf_end
)
1341 if (byte_after_cr
>= 0)
1346 if (byte_after_cr
>= 0)
1347 c1
= byte_after_cr
, byte_after_cr
= -1;
1354 else if (UTF_8_1_OCTET_P (c1
))
1356 if (eol_dos
&& c1
== '\r')
1357 ONE_MORE_BYTE (byte_after_cr
);
1363 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1365 if (UTF_8_2_OCTET_LEADING_P (c1
))
1367 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1368 /* Reject overlong sequences here and below. Encoders
1369 producing them are incorrect, they can be misleading,
1370 and they mess up read/write invariance. */
1377 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1379 if (UTF_8_3_OCTET_LEADING_P (c1
))
1381 c
= (((c1
& 0xF) << 12)
1382 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1384 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1390 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1392 if (UTF_8_4_OCTET_LEADING_P (c1
))
1394 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1395 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1402 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1404 if (UTF_8_5_OCTET_LEADING_P (c1
))
1406 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1407 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1409 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1424 consumed_chars
= consumed_chars_base
;
1426 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1431 coding
->consumed_char
+= consumed_chars_base
;
1432 coding
->consumed
= src_base
- coding
->source
;
1433 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1438 encode_coding_utf_8 (struct coding_system
*coding
)
1440 bool multibytep
= coding
->dst_multibyte
;
1441 int *charbuf
= coding
->charbuf
;
1442 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1443 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1444 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1445 ptrdiff_t produced_chars
= 0;
1448 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1450 ASSURE_DESTINATION (3);
1451 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1452 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1457 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1459 while (charbuf
< charbuf_end
)
1461 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1463 ASSURE_DESTINATION (safe_room
);
1465 if (CHAR_BYTE8_P (c
))
1467 c
= CHAR_TO_BYTE8 (c
);
1472 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1473 for (p
= str
; p
< pend
; p
++)
1480 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1482 while (charbuf
< charbuf_end
)
1484 ASSURE_DESTINATION (safe_room
);
1486 if (CHAR_BYTE8_P (c
))
1487 *dst
++ = CHAR_TO_BYTE8 (c
);
1489 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1493 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1494 coding
->produced_char
+= produced_chars
;
1495 coding
->produced
= dst
- coding
->destination
;
1500 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1501 Return true if a text is encoded in one of UTF-16 based coding systems. */
1503 #define UTF_16_HIGH_SURROGATE_P(val) \
1504 (((val) & 0xFC00) == 0xD800)
1506 #define UTF_16_LOW_SURROGATE_P(val) \
1507 (((val) & 0xFC00) == 0xDC00)
1511 detect_coding_utf_16 (struct coding_system
*coding
,
1512 struct coding_detection_info
*detect_info
)
1514 const unsigned char *src
= coding
->source
;
1515 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1516 bool multibytep
= coding
->src_multibyte
;
1519 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1520 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1521 && (coding
->src_chars
& 1))
1523 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1527 TWO_MORE_BYTES (c1
, c2
);
1528 if ((c1
== 0xFF) && (c2
== 0xFE))
1530 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1531 | CATEGORY_MASK_UTF_16_AUTO
);
1532 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1533 | CATEGORY_MASK_UTF_16_BE_NOSIG
1534 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1536 else if ((c1
== 0xFE) && (c2
== 0xFF))
1538 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1539 | CATEGORY_MASK_UTF_16_AUTO
);
1540 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1541 | CATEGORY_MASK_UTF_16_BE_NOSIG
1542 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1546 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1551 /* We check the dispersion of Eth and Oth bytes where E is even and
1552 O is odd. If both are high, we assume binary data.*/
1553 unsigned char e
[256], o
[256];
1554 unsigned e_num
= 1, o_num
= 1;
1561 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1562 |CATEGORY_MASK_UTF_16_BE
1563 | CATEGORY_MASK_UTF_16_LE
);
1565 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1566 != CATEGORY_MASK_UTF_16
)
1568 TWO_MORE_BYTES (c1
, c2
);
1576 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1583 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1594 decode_coding_utf_16 (struct coding_system
*coding
)
1596 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1597 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1598 const unsigned char *src_base
;
1599 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1600 /* We may produces at most 3 chars in one loop. */
1601 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1602 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1603 bool multibytep
= coding
->src_multibyte
;
1604 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1605 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1606 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1608 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1609 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1611 if (bom
== utf_with_bom
)
1620 if (endian
== utf_16_big_endian
1621 ? c
!= 0xFEFF : c
!= 0xFFFE)
1623 /* The first two bytes are not BOM. Treat them as bytes
1624 for a normal character. */
1628 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1630 else if (bom
== utf_detect_bom
)
1632 /* We have already tried to detect BOM and failed in
1634 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1642 consumed_chars_base
= consumed_chars
;
1644 if (charbuf
>= charbuf_end
)
1646 if (byte_after_cr1
>= 0)
1651 if (byte_after_cr1
>= 0)
1652 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1660 if (byte_after_cr2
>= 0)
1661 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1666 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1670 c
= (endian
== utf_16_big_endian
1671 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1675 if (! UTF_16_LOW_SURROGATE_P (c
))
1677 if (endian
== utf_16_big_endian
)
1678 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1680 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1684 if (UTF_16_HIGH_SURROGATE_P (c
))
1685 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1691 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1692 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1693 *charbuf
++ = 0x10000 + c
;
1698 if (UTF_16_HIGH_SURROGATE_P (c
))
1699 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1702 if (eol_dos
&& c
== '\r')
1704 ONE_MORE_BYTE (byte_after_cr1
);
1705 ONE_MORE_BYTE (byte_after_cr2
);
1713 coding
->consumed_char
+= consumed_chars_base
;
1714 coding
->consumed
= src_base
- coding
->source
;
1715 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1719 encode_coding_utf_16 (struct coding_system
*coding
)
1721 bool multibytep
= coding
->dst_multibyte
;
1722 int *charbuf
= coding
->charbuf
;
1723 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1724 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1725 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1727 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1728 bool big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1729 ptrdiff_t produced_chars
= 0;
1732 if (bom
!= utf_without_bom
)
1734 ASSURE_DESTINATION (safe_room
);
1736 EMIT_TWO_BYTES (0xFE, 0xFF);
1738 EMIT_TWO_BYTES (0xFF, 0xFE);
1739 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1742 while (charbuf
< charbuf_end
)
1744 ASSURE_DESTINATION (safe_room
);
1746 if (c
> MAX_UNICODE_CHAR
)
1747 c
= coding
->default_char
;
1752 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1754 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1761 c1
= (c
>> 10) + 0xD800;
1762 c2
= (c
& 0x3FF) + 0xDC00;
1764 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1766 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1769 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1770 coding
->produced
= dst
- coding
->destination
;
1771 coding
->produced_char
+= produced_chars
;
1776 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1778 /* Emacs' internal format for representation of multiple character
1779 sets is a kind of multi-byte encoding, i.e. characters are
1780 represented by variable-length sequences of one-byte codes.
1782 ASCII characters and control characters (e.g. `tab', `newline') are
1783 represented by one-byte sequences which are their ASCII codes, in
1784 the range 0x00 through 0x7F.
1786 8-bit characters of the range 0x80..0x9F are represented by
1787 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1790 8-bit characters of the range 0xA0..0xFF are represented by
1791 one-byte sequences which are their 8-bit code.
1793 The other characters are represented by a sequence of `base
1794 leading-code', optional `extended leading-code', and one or two
1795 `position-code's. The length of the sequence is determined by the
1796 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1797 whereas extended leading-code and position-code take the range 0xA0
1798 through 0xFF. See `charset.h' for more details about leading-code
1801 --- CODE RANGE of Emacs' internal format ---
1805 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1806 eight-bit-graphic 0xA0..0xBF
1807 ELSE 0x81..0x9D + [0xA0..0xFF]+
1808 ---------------------------------------------
1810 As this is the internal character representation, the format is
1811 usually not used externally (i.e. in a file or in a data sent to a
1812 process). But, it is possible to have a text externally in this
1813 format (i.e. by encoding by the coding system `emacs-mule').
1815 In that case, a sequence of one-byte codes has a slightly different
1818 At first, all characters in eight-bit-control are represented by
1819 one-byte sequences which are their 8-bit code.
1821 Next, character composition data are represented by the byte
1822 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1824 METHOD is 0xF2 plus one of composition method (enum
1825 composition_method),
1827 BYTES is 0xA0 plus a byte length of this composition data,
1829 CHARS is 0xA0 plus a number of characters composed by this
1832 COMPONENTs are characters of multibyte form or composition
1833 rules encoded by two-byte of ASCII codes.
1835 In addition, for backward compatibility, the following formats are
1836 also recognized as composition data on decoding.
1839 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1842 MSEQ is a multibyte form but in these special format:
1843 ASCII: 0xA0 ASCII_CODE+0x80,
1844 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1845 RULE is a one byte code of the range 0xA0..0xF0 that
1846 represents a composition rule.
1849 char emacs_mule_bytes
[256];
1852 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1853 Return true if a text is encoded in 'emacs-mule'. */
1856 detect_coding_emacs_mule (struct coding_system
*coding
,
1857 struct coding_detection_info
*detect_info
)
1859 const unsigned char *src
= coding
->source
, *src_base
;
1860 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1861 bool multibytep
= coding
->src_multibyte
;
1862 ptrdiff_t consumed_chars
= 0;
1866 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1867 /* A coding system of this category is always ASCII compatible. */
1868 src
+= coding
->head_ascii
;
1878 /* Perhaps the start of composite character. We simply skip
1879 it because analyzing it is too heavy for detecting. But,
1880 at least, we check that the composite character
1881 constitutes of more than 4 bytes. */
1882 const unsigned char *src_start
;
1892 if (src
- src_start
<= 4)
1894 found
= CATEGORY_MASK_EMACS_MULE
;
1902 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1907 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1909 while (more_bytes
> 0)
1914 src
--; /* Unread the last byte. */
1919 if (more_bytes
!= 0)
1921 found
= CATEGORY_MASK_EMACS_MULE
;
1924 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1928 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1930 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1933 detect_info
->found
|= found
;
1938 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1939 character. If CMP_STATUS indicates that we must expect MSEQ or
1940 RULE described above, decode it and return the negative value of
1941 the decoded character or rule. If an invalid byte is found, return
1942 -1. If SRC is too short, return -2. */
1945 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1946 int *nbytes
, int *nchars
, int *id
,
1947 struct composition_status
*cmp_status
)
1949 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1950 const unsigned char *src_base
= src
;
1951 bool multibytep
= coding
->src_multibyte
;
1955 int consumed_chars
= 0;
1956 bool mseq_found
= 0;
1962 charset_ID
= emacs_mule_charset
[0];
1968 if (cmp_status
->state
!= COMPOSING_NO
1969 && cmp_status
->old_form
)
1971 if (cmp_status
->state
== COMPOSING_CHAR
)
1986 *nbytes
= src
- src_base
;
1987 *nchars
= consumed_chars
;
1995 switch (emacs_mule_bytes
[c
])
1998 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2007 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2008 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2011 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2020 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2025 code
= (c
& 0x7F) << 8;
2035 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2040 code
= (c
& 0x7F) << 8;
2049 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2055 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2056 CHARSET_FROM_ID (charset_ID
), code
, c
);
2060 *nbytes
= src
- src_base
;
2061 *nchars
= consumed_chars
;
2064 return (mseq_found
? -c
: c
);
2074 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2076 /* Handle these composition sequence ('|': the end of header elements,
2077 BYTES and CHARS >= 0xA0):
2079 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2080 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2081 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2085 (4) relative composition: 0x80 | MSEQ ... MSEQ
2086 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2088 When the starter 0x80 and the following header elements are found,
2089 this annotation header is produced.
2091 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2093 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2094 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2096 Then, upon reading the following elements, these codes are produced
2097 until the composition end is found:
2100 (2) ALT ... ALT CHAR ... CHAR
2101 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2103 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2105 When the composition end is found, LENGTH and NCHARS in the
2106 annotation header is updated as below:
2108 (1) LENGTH: unchanged, NCHARS: unchanged
2109 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2110 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2111 (4) LENGTH: unchanged, NCHARS: number of CHARs
2112 (5) LENGTH: unchanged, NCHARS: number of CHARs
2114 If an error is found while composing, the annotation header is
2115 changed to the original composition header (plus filler -1s) as
2118 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2119 (5) [ 0x80 0xFF -1 -1- -1 ]
2121 and the sequence [ -2 DECODED-RULE ] is changed to the original
2122 byte sequence as below:
2123 o the original byte sequence is B: [ B -1 ]
2124 o the original byte sequence is B1 B2: [ B1 B2 ]
2126 Most of the routines are implemented by macros because many
2127 variables and labels in the caller decode_coding_emacs_mule must be
2128 accessible, and they are usually called just once (thus doesn't
2129 increase the size of compiled object). */
2131 /* Decode a composition rule represented by C as a component of
2132 composition sequence of Emacs 20 style. Set RULE to the decoded
2135 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2140 if (c < 0 || c >= 81) \
2141 goto invalid_code; \
2142 gref = c / 9, nref = c % 9; \
2143 if (gref == 4) gref = 10; \
2144 if (nref == 4) nref = 10; \
2145 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2149 /* Decode a composition rule represented by C and the following byte
2150 at SRC as a component of composition sequence of Emacs 21 style.
2151 Set RULE to the decoded rule. */
2153 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2158 if (gref < 0 || gref >= 81) \
2159 goto invalid_code; \
2160 ONE_MORE_BYTE (c); \
2162 if (nref < 0 || nref >= 81) \
2163 goto invalid_code; \
2164 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2168 /* Start of Emacs 21 style format. The first three bytes at SRC are
2169 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2170 byte length of this composition information, CHARS is the number of
2171 characters composed by this composition. */
2173 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2175 enum composition_method method = c - 0xF2; \
2176 int nbytes, nchars; \
2178 ONE_MORE_BYTE (c); \
2180 goto invalid_code; \
2181 nbytes = c - 0xA0; \
2182 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2183 goto invalid_code; \
2184 ONE_MORE_BYTE (c); \
2185 nchars = c - 0xA0; \
2186 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2187 goto invalid_code; \
2188 cmp_status->old_form = 0; \
2189 cmp_status->method = method; \
2190 if (method == COMPOSITION_RELATIVE) \
2191 cmp_status->state = COMPOSING_CHAR; \
2193 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2194 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2195 cmp_status->nchars = nchars; \
2196 cmp_status->ncomps = nbytes - 4; \
2197 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2201 /* Start of Emacs 20 style format for relative composition. */
2203 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2205 cmp_status->old_form = 1; \
2206 cmp_status->method = COMPOSITION_RELATIVE; \
2207 cmp_status->state = COMPOSING_CHAR; \
2208 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2209 cmp_status->nchars = cmp_status->ncomps = 0; \
2210 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2214 /* Start of Emacs 20 style format for rule-base composition. */
2216 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2218 cmp_status->old_form = 1; \
2219 cmp_status->method = COMPOSITION_WITH_RULE; \
2220 cmp_status->state = COMPOSING_CHAR; \
2221 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2222 cmp_status->nchars = cmp_status->ncomps = 0; \
2223 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2227 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2229 const unsigned char *current_src = src; \
2231 ONE_MORE_BYTE (c); \
2233 goto invalid_code; \
2234 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2235 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2236 DECODE_EMACS_MULE_21_COMPOSITION (); \
2237 else if (c < 0xA0) \
2238 goto invalid_code; \
2239 else if (c < 0xC0) \
2241 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2242 /* Re-read C as a composition component. */ \
2243 src = current_src; \
2245 else if (c == 0xFF) \
2246 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2248 goto invalid_code; \
2251 #define EMACS_MULE_COMPOSITION_END() \
2253 int idx = - cmp_status->length; \
2255 if (cmp_status->old_form) \
2256 charbuf[idx + 2] = cmp_status->nchars; \
2257 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2258 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2259 cmp_status->state = COMPOSING_NO; \
2264 emacs_mule_finish_composition (int *charbuf
,
2265 struct composition_status
*cmp_status
)
2267 int idx
= - cmp_status
->length
;
2270 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2272 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2274 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2275 && cmp_status
->state
== COMPOSING_CHAR
)
2277 /* The last rule was invalid. */
2278 int rule
= charbuf
[-1] + 0xA0;
2280 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2287 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2289 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2291 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2292 charbuf
[idx
++] = -3;
2298 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2299 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2301 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2302 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2303 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2304 charbuf
[idx
++] = -1;
2308 cmp_status
->state
= COMPOSING_NO
;
2312 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2314 if (cmp_status->state != COMPOSING_NO) \
2315 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2320 decode_coding_emacs_mule (struct coding_system
*coding
)
2322 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2323 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2324 const unsigned char *src_base
;
2325 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2326 /* We may produce two annotations (charset and composition) in one
2327 loop and one more charset annotation at the end. */
2329 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2330 /* We can produce up to 2 characters in a loop. */
2332 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
2333 bool multibytep
= coding
->src_multibyte
;
2334 ptrdiff_t char_offset
= coding
->produced_char
;
2335 ptrdiff_t last_offset
= char_offset
;
2336 int last_id
= charset_ascii
;
2338 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2339 int byte_after_cr
= -1;
2340 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2342 if (cmp_status
->state
!= COMPOSING_NO
)
2346 if (charbuf_end
- charbuf
< cmp_status
->length
)
2348 for (i
= 0; i
< cmp_status
->length
; i
++)
2349 *charbuf
++ = cmp_status
->carryover
[i
];
2350 coding
->annotated
= 1;
2355 int c
, id
IF_LINT (= 0);
2358 consumed_chars_base
= consumed_chars
;
2360 if (charbuf
>= charbuf_end
)
2362 if (byte_after_cr
>= 0)
2367 if (byte_after_cr
>= 0)
2368 c
= byte_after_cr
, byte_after_cr
= -1;
2372 if (c
< 0 || c
== 0x80)
2374 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2381 DECODE_EMACS_MULE_COMPOSITION_START ();
2387 if (eol_dos
&& c
== '\r')
2388 ONE_MORE_BYTE (byte_after_cr
);
2390 if (cmp_status
->state
!= COMPOSING_NO
)
2392 if (cmp_status
->old_form
)
2393 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2394 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2395 cmp_status
->ncomps
--;
2400 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2401 /* emacs_mule_char can load a charset map from a file, which
2402 allocates a large structure and might cause buffer text
2403 to be relocated as result. Thus, we need to remember the
2404 original pointer to buffer text, and fix up all related
2405 pointers after the call. */
2406 const unsigned char *orig
= coding
->source
;
2409 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2411 offset
= coding
->source
- orig
;
2425 src
= src_base
+ nbytes
;
2426 consumed_chars
= consumed_chars_base
+ nchars
;
2427 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2428 cmp_status
->ncomps
-= nchars
;
2431 /* Now if C >= 0, we found a normally encoded character, if C <
2432 0, we found an old-style composition component character or
2435 if (cmp_status
->state
== COMPOSING_NO
)
2439 if (last_id
!= charset_ascii
)
2440 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2443 last_offset
= char_offset
;
2448 else if (cmp_status
->state
== COMPOSING_CHAR
)
2450 if (cmp_status
->old_form
)
2454 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2461 cmp_status
->nchars
++;
2462 cmp_status
->length
++;
2463 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2464 EMACS_MULE_COMPOSITION_END ();
2465 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2466 cmp_status
->state
= COMPOSING_RULE
;
2472 cmp_status
->length
++;
2473 cmp_status
->nchars
--;
2474 if (cmp_status
->nchars
== 0)
2475 EMACS_MULE_COMPOSITION_END ();
2478 else if (cmp_status
->state
== COMPOSING_RULE
)
2484 EMACS_MULE_COMPOSITION_END ();
2491 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2496 cmp_status
->length
+= 2;
2497 cmp_status
->state
= COMPOSING_CHAR
;
2500 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2503 cmp_status
->length
++;
2504 if (cmp_status
->ncomps
== 0)
2505 cmp_status
->state
= COMPOSING_CHAR
;
2506 else if (cmp_status
->ncomps
> 0)
2508 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2509 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2512 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2514 else /* COMPOSING_COMPONENT_RULE */
2518 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2523 cmp_status
->length
+= 2;
2524 cmp_status
->ncomps
--;
2525 if (cmp_status
->ncomps
> 0)
2526 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2528 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2533 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2535 consumed_chars
= consumed_chars_base
;
2537 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2543 if (cmp_status
->state
!= COMPOSING_NO
)
2545 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2546 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2551 charbuf
-= cmp_status
->length
;
2552 for (i
= 0; i
< cmp_status
->length
; i
++)
2553 cmp_status
->carryover
[i
] = charbuf
[i
];
2556 if (last_id
!= charset_ascii
)
2557 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2558 coding
->consumed_char
+= consumed_chars_base
;
2559 coding
->consumed
= src_base
- coding
->source
;
2560 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2564 #define EMACS_MULE_LEADING_CODES(id, codes) \
2567 codes[0] = id, codes[1] = 0; \
2568 else if (id < 0xE0) \
2569 codes[0] = 0x9A, codes[1] = id; \
2570 else if (id < 0xF0) \
2571 codes[0] = 0x9B, codes[1] = id; \
2572 else if (id < 0xF5) \
2573 codes[0] = 0x9C, codes[1] = id; \
2575 codes[0] = 0x9D, codes[1] = id; \
2580 encode_coding_emacs_mule (struct coding_system
*coding
)
2582 bool multibytep
= coding
->dst_multibyte
;
2583 int *charbuf
= coding
->charbuf
;
2584 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2585 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2586 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2588 ptrdiff_t produced_chars
= 0;
2589 Lisp_Object attrs
, charset_list
;
2591 int preferred_charset_id
= -1;
2593 CODING_GET_INFO (coding
, attrs
, charset_list
);
2594 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2596 charset_list
= Vemacs_mule_charset_list
;
2597 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2600 while (charbuf
< charbuf_end
)
2602 ASSURE_DESTINATION (safe_room
);
2607 /* Handle an annotation. */
2610 case CODING_ANNOTATE_COMPOSITION_MASK
:
2611 /* Not yet implemented. */
2613 case CODING_ANNOTATE_CHARSET_MASK
:
2614 preferred_charset_id
= charbuf
[3];
2615 if (preferred_charset_id
>= 0
2616 && NILP (Fmemq (make_number (preferred_charset_id
),
2618 preferred_charset_id
= -1;
2627 if (ASCII_CHAR_P (c
))
2628 EMIT_ONE_ASCII_BYTE (c
);
2629 else if (CHAR_BYTE8_P (c
))
2631 c
= CHAR_TO_BYTE8 (c
);
2636 struct charset
*charset
;
2640 unsigned char leading_codes
[2];
2642 if (preferred_charset_id
>= 0)
2646 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2647 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2649 code
= ENCODE_CHAR (charset
, c
);
2651 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2655 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2659 c
= coding
->default_char
;
2660 if (ASCII_CHAR_P (c
))
2662 EMIT_ONE_ASCII_BYTE (c
);
2665 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2668 dimension
= CHARSET_DIMENSION (charset
);
2669 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2670 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2671 EMIT_ONE_BYTE (leading_codes
[0]);
2672 if (leading_codes
[1])
2673 EMIT_ONE_BYTE (leading_codes
[1]);
2675 EMIT_ONE_BYTE (code
| 0x80);
2679 EMIT_ONE_BYTE (code
>> 8);
2680 EMIT_ONE_BYTE (code
& 0xFF);
2684 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2685 coding
->produced_char
+= produced_chars
;
2686 coding
->produced
= dst
- coding
->destination
;
2691 /*** 7. ISO2022 handlers ***/
2693 /* The following note describes the coding system ISO2022 briefly.
2694 Since the intention of this note is to help understand the
2695 functions in this file, some parts are NOT ACCURATE or are OVERLY
2696 SIMPLIFIED. For thorough understanding, please refer to the
2697 original document of ISO2022. This is equivalent to the standard
2698 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2700 ISO2022 provides many mechanisms to encode several character sets
2701 in 7-bit and 8-bit environments. For 7-bit environments, all text
2702 is encoded using bytes less than 128. This may make the encoded
2703 text a little bit longer, but the text passes more easily through
2704 several types of gateway, some of which strip off the MSB (Most
2707 There are two kinds of character sets: control character sets and
2708 graphic character sets. The former contain control characters such
2709 as `newline' and `escape' to provide control functions (control
2710 functions are also provided by escape sequences). The latter
2711 contain graphic characters such as 'A' and '-'. Emacs recognizes
2712 two control character sets and many graphic character sets.
2714 Graphic character sets are classified into one of the following
2715 four classes, according to the number of bytes (DIMENSION) and
2716 number of characters in one dimension (CHARS) of the set:
2717 - DIMENSION1_CHARS94
2718 - DIMENSION1_CHARS96
2719 - DIMENSION2_CHARS94
2720 - DIMENSION2_CHARS96
2722 In addition, each character set is assigned an identification tag,
2723 unique for each set, called the "final character" (denoted as <F>
2724 hereafter). The <F> of each character set is decided by ECMA(*)
2725 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2726 (0x30..0x3F are for private use only).
2728 Note (*): ECMA = European Computer Manufacturers Association
2730 Here are examples of graphic character sets [NAME(<F>)]:
2731 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2732 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2733 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2734 o DIMENSION2_CHARS96 -- none for the moment
2736 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2737 C0 [0x00..0x1F] -- control character plane 0
2738 GL [0x20..0x7F] -- graphic character plane 0
2739 C1 [0x80..0x9F] -- control character plane 1
2740 GR [0xA0..0xFF] -- graphic character plane 1
2742 A control character set is directly designated and invoked to C0 or
2743 C1 by an escape sequence. The most common case is that:
2744 - ISO646's control character set is designated/invoked to C0, and
2745 - ISO6429's control character set is designated/invoked to C1,
2746 and usually these designations/invocations are omitted in encoded
2747 text. In a 7-bit environment, only C0 can be used, and a control
2748 character for C1 is encoded by an appropriate escape sequence to
2749 fit into the environment. All control characters for C1 are
2750 defined to have corresponding escape sequences.
2752 A graphic character set is at first designated to one of four
2753 graphic registers (G0 through G3), then these graphic registers are
2754 invoked to GL or GR. These designations and invocations can be
2755 done independently. The most common case is that G0 is invoked to
2756 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2757 these invocations and designations are omitted in encoded text.
2758 In a 7-bit environment, only GL can be used.
2760 When a graphic character set of CHARS94 is invoked to GL, codes
2761 0x20 and 0x7F of the GL area work as control characters SPACE and
2762 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2765 There are two ways of invocation: locking-shift and single-shift.
2766 With locking-shift, the invocation lasts until the next different
2767 invocation, whereas with single-shift, the invocation affects the
2768 following character only and doesn't affect the locking-shift
2769 state. Invocations are done by the following control characters or
2772 ----------------------------------------------------------------------
2773 abbrev function cntrl escape seq description
2774 ----------------------------------------------------------------------
2775 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2776 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2777 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2778 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2779 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2780 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2781 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2782 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2783 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2784 ----------------------------------------------------------------------
2785 (*) These are not used by any known coding system.
2787 Control characters for these functions are defined by macros
2788 ISO_CODE_XXX in `coding.h'.
2790 Designations are done by the following escape sequences:
2791 ----------------------------------------------------------------------
2792 escape sequence description
2793 ----------------------------------------------------------------------
2794 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2795 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2796 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2797 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2798 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2799 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2800 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2801 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2802 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2803 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2804 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2805 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2806 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2807 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2808 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2809 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2810 ----------------------------------------------------------------------
2812 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2813 of dimension 1, chars 94, and final character <F>, etc...
2815 Note (*): Although these designations are not allowed in ISO2022,
2816 Emacs accepts them on decoding, and produces them on encoding
2817 CHARS96 character sets in a coding system which is characterized as
2818 7-bit environment, non-locking-shift, and non-single-shift.
2820 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2821 '(' must be omitted. We refer to this as "short-form" hereafter.
2823 Now you may notice that there are a lot of ways of encoding the
2824 same multilingual text in ISO2022. Actually, there exist many
2825 coding systems such as Compound Text (used in X11's inter client
2826 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2827 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2828 localized platforms), and all of these are variants of ISO2022.
2830 In addition to the above, Emacs handles two more kinds of escape
2831 sequences: ISO6429's direction specification and Emacs' private
2832 sequence for specifying character composition.
2834 ISO6429's direction specification takes the following form:
2835 o CSI ']' -- end of the current direction
2836 o CSI '0' ']' -- end of the current direction
2837 o CSI '1' ']' -- start of left-to-right text
2838 o CSI '2' ']' -- start of right-to-left text
2839 The control character CSI (0x9B: control sequence introducer) is
2840 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2842 Character composition specification takes the following form:
2843 o ESC '0' -- start relative composition
2844 o ESC '1' -- end composition
2845 o ESC '2' -- start rule-base composition (*)
2846 o ESC '3' -- start relative composition with alternate chars (**)
2847 o ESC '4' -- start rule-base composition with alternate chars (**)
2848 Since these are not standard escape sequences of any ISO standard,
2849 the use of them with these meanings is restricted to Emacs only.
2851 (*) This form is used only in Emacs 20.7 and older versions,
2852 but newer versions can safely decode it.
2853 (**) This form is used only in Emacs 21.1 and newer versions,
2854 and older versions can't decode it.
2856 Here's a list of example usages of these composition escape
2857 sequences (categorized by `enum composition_method').
2859 COMPOSITION_RELATIVE:
2860 ESC 0 CHAR [ CHAR ] ESC 1
2861 COMPOSITION_WITH_RULE:
2862 ESC 2 CHAR [ RULE CHAR ] ESC 1
2863 COMPOSITION_WITH_ALTCHARS:
2864 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2865 COMPOSITION_WITH_RULE_ALTCHARS:
2866 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2868 static enum iso_code_class_type iso_code_class
[256];
2870 #define SAFE_CHARSET_P(coding, id) \
2871 ((id) <= (coding)->max_charset_id \
2872 && (coding)->safe_charsets[id] != 255)
2875 setup_iso_safe_charsets (Lisp_Object attrs
)
2877 Lisp_Object charset_list
, safe_charsets
;
2878 Lisp_Object request
;
2879 Lisp_Object reg_usage
;
2881 EMACS_INT reg94
, reg96
;
2882 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2885 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2886 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2887 && ! EQ (charset_list
, Viso_2022_charset_list
))
2889 charset_list
= Viso_2022_charset_list
;
2890 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2891 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2894 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2898 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2900 int id
= XINT (XCAR (tail
));
2901 if (max_charset_id
< id
)
2902 max_charset_id
= id
;
2905 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2906 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2907 request
= AREF (attrs
, coding_attr_iso_request
);
2908 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2909 reg94
= XINT (XCAR (reg_usage
));
2910 reg96
= XINT (XCDR (reg_usage
));
2912 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2916 struct charset
*charset
;
2919 charset
= CHARSET_FROM_ID (XINT (id
));
2920 reg
= Fcdr (Fassq (id
, request
));
2922 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2923 else if (charset
->iso_chars_96
)
2926 SSET (safe_charsets
, XINT (id
), reg96
);
2931 SSET (safe_charsets
, XINT (id
), reg94
);
2934 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2938 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2939 Return true if a text is encoded in one of ISO-2022 based coding
2943 detect_coding_iso_2022 (struct coding_system
*coding
,
2944 struct coding_detection_info
*detect_info
)
2946 const unsigned char *src
= coding
->source
, *src_base
= src
;
2947 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2948 bool multibytep
= coding
->src_multibyte
;
2949 bool single_shifting
= 0;
2952 ptrdiff_t consumed_chars
= 0;
2956 int composition_count
= -1;
2958 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2960 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2962 struct coding_system
*this = &(coding_categories
[i
]);
2963 Lisp_Object attrs
, val
;
2967 attrs
= CODING_ID_ATTRS (this->id
);
2968 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2969 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2970 setup_iso_safe_charsets (attrs
);
2971 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2972 this->max_charset_id
= SCHARS (val
) - 1;
2973 this->safe_charsets
= SDATA (val
);
2976 /* A coding system of this category is always ASCII compatible. */
2977 src
+= coding
->head_ascii
;
2979 while (rejected
!= CATEGORY_MASK_ISO
)
2986 if (inhibit_iso_escape_detection
)
2988 single_shifting
= 0;
2990 if (c
== 'N' || c
== 'O')
2992 /* ESC <Fe> for SS2 or SS3. */
2993 single_shifting
= 1;
2994 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2998 /* End of composition. */
2999 if (composition_count
< 0
3000 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3003 composition_count
= -1;
3004 found
|= CATEGORY_MASK_ISO
;
3006 else if (c
>= '0' && c
<= '4')
3008 /* ESC <Fp> for start/end composition. */
3009 composition_count
= 0;
3013 if (c
>= '(' && c
<= '/')
3015 /* Designation sequence for a charset of dimension 1. */
3017 if (c1
< ' ' || c1
>= 0x80
3018 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3019 /* Invalid designation sequence. Just ignore. */
3024 /* Designation sequence for a charset of dimension 2. */
3026 if (c
>= '@' && c
<= 'B')
3027 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3028 id
= iso_charset_table
[1][0][c
];
3029 else if (c
>= '(' && c
<= '/')
3032 if (c1
< ' ' || c1
>= 0x80
3033 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3034 /* Invalid designation sequence. Just ignore. */
3038 /* Invalid designation sequence. Just ignore it. */
3043 /* Invalid escape sequence. Just ignore it. */
3047 /* We found a valid designation sequence for CHARSET. */
3048 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3049 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3051 found
|= CATEGORY_MASK_ISO_7
;
3053 rejected
|= CATEGORY_MASK_ISO_7
;
3054 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3056 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3058 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3059 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3061 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3063 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3064 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3066 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3068 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3074 /* Locking shift out/in. */
3075 if (inhibit_iso_escape_detection
)
3077 single_shifting
= 0;
3078 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3082 /* Control sequence introducer. */
3083 single_shifting
= 0;
3084 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3085 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3086 goto check_extra_latin
;
3091 if (inhibit_iso_escape_detection
)
3093 single_shifting
= 0;
3094 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3095 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3096 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3098 found
|= CATEGORY_MASK_ISO_8_1
;
3099 single_shifting
= 1;
3101 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3102 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3104 found
|= CATEGORY_MASK_ISO_8_2
;
3105 single_shifting
= 1;
3107 if (single_shifting
)
3110 if (! VECTORP (Vlatin_extra_code_table
)
3111 || NILP (AREF (Vlatin_extra_code_table
, c
)))
3113 rejected
= CATEGORY_MASK_ISO
;
3116 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3117 & CODING_ISO_FLAG_LATIN_EXTRA
)
3118 found
|= CATEGORY_MASK_ISO_8_1
;
3120 rejected
|= CATEGORY_MASK_ISO_8_1
;
3121 rejected
|= CATEGORY_MASK_ISO_8_2
;
3129 if (composition_count
>= 0)
3130 composition_count
++;
3131 single_shifting
= 0;
3136 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3137 found
|= CATEGORY_MASK_ISO_8_1
;
3138 /* Check the length of succeeding codes of the range
3139 0xA0..0FF. If the byte length is even, we include
3140 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3141 only when we are not single shifting. */
3142 if (! single_shifting
3143 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3146 while (src
< src_end
)
3158 if (len
& 1 && src
< src_end
)
3160 rejected
|= CATEGORY_MASK_ISO_8_2
;
3161 if (composition_count
>= 0)
3162 composition_count
+= len
;
3166 found
|= CATEGORY_MASK_ISO_8_2
;
3167 if (composition_count
>= 0)
3168 composition_count
+= len
/ 2;
3175 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3179 detect_info
->rejected
|= rejected
;
3180 detect_info
->found
|= (found
& ~rejected
);
3185 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3186 escape sequence should be kept. */
3187 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3191 if (final < '0' || final >= 128 \
3192 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3193 || !SAFE_CHARSET_P (coding, id)) \
3195 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3199 prev = CODING_ISO_DESIGNATION (coding, reg); \
3200 if (id == charset_jisx0201_roman) \
3202 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3203 id = charset_ascii; \
3205 else if (id == charset_jisx0208_1978) \
3207 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3208 id = charset_jisx0208; \
3210 CODING_ISO_DESIGNATION (coding, reg) = id; \
3211 /* If there was an invalid designation to REG previously, and this \
3212 designation is ASCII to REG, we should keep this designation \
3214 if (prev == -2 && id == charset_ascii) \
3219 /* Handle these composition sequence (ALT: alternate char):
3221 (1) relative composition: ESC 0 CHAR ... ESC 1
3222 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3223 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3224 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3226 When the start sequence (ESC 0/2/3/4) is found, this annotation
3229 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3231 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3232 produced until the end sequence (ESC 1) is found:
3235 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3236 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3237 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3239 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3240 annotation header is updated as below:
3242 (1) LENGTH: unchanged, NCHARS: number of CHARs
3243 (2) LENGTH: unchanged, NCHARS: number of CHARs
3244 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3245 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3247 If an error is found while composing, the annotation header is
3250 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3252 and the sequence [ -2 DECODED-RULE ] is changed to the original
3253 byte sequence as below:
3254 o the original byte sequence is B: [ B -1 ]
3255 o the original byte sequence is B1 B2: [ B1 B2 ]
3256 and the sequence [ -1 -1 ] is changed to the original byte
3261 /* Decode a composition rule C1 and maybe one more byte from the
3262 source, and set RULE to the encoded composition rule. If the rule
3263 is invalid, goto invalid_code. */
3265 #define DECODE_COMPOSITION_RULE(rule) \
3269 goto invalid_code; \
3270 if (rule < 81) /* old format (before ver.21) */ \
3272 int gref = (rule) / 9; \
3273 int nref = (rule) % 9; \
3274 if (gref == 4) gref = 10; \
3275 if (nref == 4) nref = 10; \
3276 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3278 else /* new format (after ver.21) */ \
3282 ONE_MORE_BYTE (b); \
3283 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3284 goto invalid_code; \
3285 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3286 rule += 0x100; /* Distinguish it from the old format. */ \
3290 #define ENCODE_COMPOSITION_RULE(rule) \
3292 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3294 if (rule < 0x100) /* old format */ \
3296 if (gref == 10) gref = 4; \
3297 if (nref == 10) nref = 4; \
3298 charbuf[idx] = 32 + gref * 9 + nref; \
3299 charbuf[idx + 1] = -1; \
3302 else /* new format */ \
3304 charbuf[idx] = 32 + 81 + gref; \
3305 charbuf[idx + 1] = 32 + nref; \
3310 /* Finish the current composition as invalid. */
3313 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3315 int idx
= - cmp_status
->length
;
3318 /* Recover the original ESC sequence */
3319 charbuf
[idx
++] = ISO_CODE_ESC
;
3320 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3321 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3322 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3323 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3325 charbuf
[idx
++] = -2;
3327 charbuf
[idx
++] = -1;
3328 new_chars
= cmp_status
->nchars
;
3329 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3330 for (; idx
< 0; idx
++)
3332 int elt
= charbuf
[idx
];
3336 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3341 charbuf
[idx
++] = ISO_CODE_ESC
;
3346 cmp_status
->state
= COMPOSING_NO
;
3350 /* If characters are under composition, finish the composition. */
3351 #define MAYBE_FINISH_COMPOSITION() \
3353 if (cmp_status->state != COMPOSING_NO) \
3354 char_offset += finish_composition (charbuf, cmp_status); \
3357 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3359 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3360 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3361 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3362 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3364 Produce this annotation sequence now:
3366 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3369 #define DECODE_COMPOSITION_START(c1) \
3372 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3373 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3374 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3375 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3379 cmp_status->state = COMPOSING_CHAR; \
3380 cmp_status->length += 2; \
3384 MAYBE_FINISH_COMPOSITION (); \
3385 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3386 : c1 == '2' ? COMPOSITION_WITH_RULE \
3387 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3388 : COMPOSITION_WITH_RULE_ALTCHARS); \
3390 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3391 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3392 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3393 cmp_status->nchars = cmp_status->ncomps = 0; \
3394 coding->annotated = 1; \
3399 /* Handle composition end sequence ESC 1. */
3401 #define DECODE_COMPOSITION_END() \
3403 if (cmp_status->nchars == 0 \
3404 || ((cmp_status->state == COMPOSING_CHAR) \
3405 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3407 MAYBE_FINISH_COMPOSITION (); \
3408 goto invalid_code; \
3410 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3411 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3412 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3413 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3414 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3415 char_offset += cmp_status->nchars; \
3416 cmp_status->state = COMPOSING_NO; \
3419 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3421 #define STORE_COMPOSITION_RULE(rule) \
3424 *charbuf++ = rule; \
3425 cmp_status->length += 2; \
3426 cmp_status->state--; \
3429 /* Store a composed char or a component char C in charbuf, and update
3432 #define STORE_COMPOSITION_CHAR(c) \
3435 cmp_status->length++; \
3436 if (cmp_status->state == COMPOSING_CHAR) \
3437 cmp_status->nchars++; \
3439 cmp_status->ncomps++; \
3440 if (cmp_status->method == COMPOSITION_WITH_RULE \
3441 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3442 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3443 cmp_status->state++; \
3447 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3450 decode_coding_iso_2022 (struct coding_system
*coding
)
3452 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3453 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3454 const unsigned char *src_base
;
3455 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3456 /* We may produce two annotations (charset and composition) in one
3457 loop and one more charset annotation at the end. */
3459 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3460 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
3461 bool multibytep
= coding
->src_multibyte
;
3462 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3463 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3464 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3465 int charset_id_2
, charset_id_3
;
3466 struct charset
*charset
;
3468 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3469 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3470 ptrdiff_t char_offset
= coding
->produced_char
;
3471 ptrdiff_t last_offset
= char_offset
;
3472 int last_id
= charset_ascii
;
3474 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3475 int byte_after_cr
= -1;
3478 setup_iso_safe_charsets (attrs
);
3479 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3481 if (cmp_status
->state
!= COMPOSING_NO
)
3483 if (charbuf_end
- charbuf
< cmp_status
->length
)
3485 for (i
= 0; i
< cmp_status
->length
; i
++)
3486 *charbuf
++ = cmp_status
->carryover
[i
];
3487 coding
->annotated
= 1;
3495 consumed_chars_base
= consumed_chars
;
3497 if (charbuf
>= charbuf_end
)
3499 if (byte_after_cr
>= 0)
3504 if (byte_after_cr
>= 0)
3505 c1
= byte_after_cr
, byte_after_cr
= -1;
3511 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3513 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3515 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3519 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3521 if (c1
== ISO_CODE_ESC
)
3523 if (src
+ 1 >= src_end
)
3524 goto no_more_source
;
3525 *charbuf
++ = ISO_CODE_ESC
;
3527 if (src
[0] == '%' && src
[1] == '@')
3530 consumed_chars
+= 2;
3532 /* We are sure charbuf can contain two more chars. */
3535 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3540 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3546 if ((cmp_status
->state
== COMPOSING_RULE
3547 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3548 && c1
!= ISO_CODE_ESC
)
3552 DECODE_COMPOSITION_RULE (rule
);
3553 STORE_COMPOSITION_RULE (rule
);
3557 /* We produce at most one character. */
3558 switch (iso_code_class
[c1
])
3560 case ISO_0x20_or_0x7F
:
3561 if (charset_id_0
< 0
3562 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3563 /* This is SPACE or DEL. */
3564 charset
= CHARSET_FROM_ID (charset_ascii
);
3566 charset
= CHARSET_FROM_ID (charset_id_0
);
3569 case ISO_graphic_plane_0
:
3570 if (charset_id_0
< 0)
3571 charset
= CHARSET_FROM_ID (charset_ascii
);
3573 charset
= CHARSET_FROM_ID (charset_id_0
);
3576 case ISO_0xA0_or_0xFF
:
3577 if (charset_id_1
< 0
3578 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3579 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3581 /* This is a graphic character, we fall down ... */
3583 case ISO_graphic_plane_1
:
3584 if (charset_id_1
< 0)
3586 charset
= CHARSET_FROM_ID (charset_id_1
);
3590 if (eol_dos
&& c1
== '\r')
3591 ONE_MORE_BYTE (byte_after_cr
);
3592 MAYBE_FINISH_COMPOSITION ();
3593 charset
= CHARSET_FROM_ID (charset_ascii
);
3600 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3601 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3603 CODING_ISO_INVOCATION (coding
, 0) = 1;
3604 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3608 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3610 CODING_ISO_INVOCATION (coding
, 0) = 0;
3611 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3614 case ISO_single_shift_2_7
:
3615 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3617 case ISO_single_shift_2
:
3618 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3620 /* SS2 is handled as an escape sequence of ESC 'N' */
3622 goto label_escape_sequence
;
3624 case ISO_single_shift_3
:
3625 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3627 /* SS2 is handled as an escape sequence of ESC 'O' */
3629 goto label_escape_sequence
;
3631 case ISO_control_sequence_introducer
:
3632 /* CSI is handled as an escape sequence of ESC '[' ... */
3634 goto label_escape_sequence
;
3638 label_escape_sequence
:
3639 /* Escape sequences handled here are invocation,
3640 designation, direction specification, and character
3641 composition specification. */
3644 case '&': /* revision of following character set */
3646 if (!(c1
>= '@' && c1
<= '~'))
3649 if (c1
!= ISO_CODE_ESC
)
3652 goto label_escape_sequence
;
3654 case '$': /* designation of 2-byte character set */
3655 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3661 if (c1
>= '@' && c1
<= 'B')
3662 { /* designation of JISX0208.1978, GB2312.1980,
3664 reg
= 0, chars96
= 0;
3666 else if (c1
>= 0x28 && c1
<= 0x2B)
3667 { /* designation of DIMENSION2_CHARS94 character set */
3668 reg
= c1
- 0x28, chars96
= 0;
3671 else if (c1
>= 0x2C && c1
<= 0x2F)
3672 { /* designation of DIMENSION2_CHARS96 character set */
3673 reg
= c1
- 0x2C, chars96
= 1;
3678 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3679 /* We must update these variables now. */
3681 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3683 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3689 case 'n': /* invocation of locking-shift-2 */
3690 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3691 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3693 CODING_ISO_INVOCATION (coding
, 0) = 2;
3694 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3697 case 'o': /* invocation of locking-shift-3 */
3698 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3699 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3701 CODING_ISO_INVOCATION (coding
, 0) = 3;
3702 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3705 case 'N': /* invocation of single-shift-2 */
3706 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3707 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3709 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3710 if (charset_id_2
< 0)
3711 charset
= CHARSET_FROM_ID (charset_ascii
);
3713 charset
= CHARSET_FROM_ID (charset_id_2
);
3715 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3719 case 'O': /* invocation of single-shift-3 */
3720 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3721 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3723 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3724 if (charset_id_3
< 0)
3725 charset
= CHARSET_FROM_ID (charset_ascii
);
3727 charset
= CHARSET_FROM_ID (charset_id_3
);
3729 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3733 case '0': case '2': case '3': case '4': /* start composition */
3734 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3736 if (last_id
!= charset_ascii
)
3738 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3739 last_id
= charset_ascii
;
3740 last_offset
= char_offset
;
3742 DECODE_COMPOSITION_START (c1
);
3745 case '1': /* end composition */
3746 if (cmp_status
->state
== COMPOSING_NO
)
3748 DECODE_COMPOSITION_END ();
3751 case '[': /* specification of direction */
3752 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3754 /* For the moment, nested direction is not supported.
3755 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3756 left-to-right, and nonzero means right-to-left. */
3760 case ']': /* end of the current direction */
3761 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3763 case '0': /* end of the current direction */
3764 case '1': /* start of left-to-right direction */
3767 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3772 case '2': /* start of right-to-left direction */
3775 coding
->mode
|= CODING_MODE_DIRECTION
;
3789 /* CTEXT extended segment:
3790 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3791 We keep these bytes as is for the moment.
3792 They may be decoded by post-read-conversion. */
3796 ONE_MORE_BYTE (dim
);
3797 if (dim
< '0' || dim
> '4')
3805 size
= ((M
- 128) * 128) + (L
- 128);
3806 if (charbuf
+ 6 > charbuf_end
)
3808 *charbuf
++ = ISO_CODE_ESC
;
3812 *charbuf
++ = BYTE8_TO_CHAR (M
);
3813 *charbuf
++ = BYTE8_TO_CHAR (L
);
3814 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3818 /* XFree86 extension for embedding UTF-8 in CTEXT:
3819 ESC % G --UTF-8-BYTES-- ESC % @
3820 We keep these bytes as is for the moment.
3821 They may be decoded by post-read-conversion. */
3822 if (charbuf
+ 3 > charbuf_end
)
3824 *charbuf
++ = ISO_CODE_ESC
;
3827 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3835 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3840 if (c1
>= 0x28 && c1
<= 0x2B)
3841 { /* designation of DIMENSION1_CHARS94 character set */
3842 reg
= c1
- 0x28, chars96
= 0;
3845 else if (c1
>= 0x2C && c1
<= 0x2F)
3846 { /* designation of DIMENSION1_CHARS96 character set */
3847 reg
= c1
- 0x2C, chars96
= 1;
3852 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3853 /* We must update these variables now. */
3855 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3857 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3869 if (cmp_status
->state
== COMPOSING_NO
3870 && charset
->id
!= charset_ascii
3871 && last_id
!= charset
->id
)
3873 if (last_id
!= charset_ascii
)
3874 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3875 last_id
= charset
->id
;
3876 last_offset
= char_offset
;
3879 /* Now we know CHARSET and 1st position code C1 of a character.
3880 Produce a decoded character while getting 2nd and 3rd
3881 position codes C2, C3 if necessary. */
3882 if (CHARSET_DIMENSION (charset
) > 1)
3885 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3886 || ((c1
& 0x80) != (c2
& 0x80)))
3887 /* C2 is not in a valid range. */
3889 if (CHARSET_DIMENSION (charset
) == 2)
3890 c1
= (c1
<< 8) | c2
;
3894 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3895 || ((c1
& 0x80) != (c3
& 0x80)))
3896 /* C3 is not in a valid range. */
3898 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3902 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3905 MAYBE_FINISH_COMPOSITION ();
3906 for (; src_base
< src
; src_base
++, char_offset
++)
3908 if (ASCII_BYTE_P (*src_base
))
3909 *charbuf
++ = *src_base
;
3911 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3914 else if (cmp_status
->state
== COMPOSING_NO
)
3919 else if ((cmp_status
->state
== COMPOSING_CHAR
3920 ? cmp_status
->nchars
3921 : cmp_status
->ncomps
)
3922 >= MAX_COMPOSITION_COMPONENTS
)
3924 /* Too long composition. */
3925 MAYBE_FINISH_COMPOSITION ();
3930 STORE_COMPOSITION_CHAR (c
);
3934 MAYBE_FINISH_COMPOSITION ();
3936 consumed_chars
= consumed_chars_base
;
3938 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3948 if (cmp_status
->state
!= COMPOSING_NO
)
3950 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3951 MAYBE_FINISH_COMPOSITION ();
3954 charbuf
-= cmp_status
->length
;
3955 for (i
= 0; i
< cmp_status
->length
; i
++)
3956 cmp_status
->carryover
[i
] = charbuf
[i
];
3959 else if (last_id
!= charset_ascii
)
3960 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3961 coding
->consumed_char
+= consumed_chars_base
;
3962 coding
->consumed
= src_base
- coding
->source
;
3963 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3967 /* ISO2022 encoding stuff. */
3970 It is not enough to say just "ISO2022" on encoding, we have to
3971 specify more details. In Emacs, each coding system of ISO2022
3972 variant has the following specifications:
3973 1. Initial designation to G0 thru G3.
3974 2. Allows short-form designation?
3975 3. ASCII should be designated to G0 before control characters?
3976 4. ASCII should be designated to G0 at end of line?
3977 5. 7-bit environment or 8-bit environment?
3978 6. Use locking-shift?
3979 7. Use Single-shift?
3980 And the following two are only for Japanese:
3981 8. Use ASCII in place of JIS0201-1976-Roman?
3982 9. Use JISX0208-1983 in place of JISX0208-1978?
3983 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3984 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3988 /* Produce codes (escape sequence) for designating CHARSET to graphic
3989 register REG at DST, and increment DST. If <final-char> of CHARSET is
3990 '@', 'A', or 'B' and the coding system CODING allows, produce
3991 designation sequence of short-form. */
3993 #define ENCODE_DESIGNATION(charset, reg, coding) \
3995 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3996 const char *intermediate_char_94 = "()*+"; \
3997 const char *intermediate_char_96 = ",-./"; \
3998 int revision = -1; \
4000 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4001 revision = CHARSET_ISO_REVISION (charset); \
4003 if (revision >= 0) \
4005 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4006 EMIT_ONE_BYTE ('@' + revision); \
4008 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4009 if (CHARSET_DIMENSION (charset) == 1) \
4012 if (! CHARSET_ISO_CHARS_96 (charset)) \
4013 b = intermediate_char_94[reg]; \
4015 b = intermediate_char_96[reg]; \
4016 EMIT_ONE_ASCII_BYTE (b); \
4020 EMIT_ONE_ASCII_BYTE ('$'); \
4021 if (! CHARSET_ISO_CHARS_96 (charset)) \
4023 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4025 || final_char < '@' || final_char > 'B') \
4026 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4029 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4031 EMIT_ONE_ASCII_BYTE (final_char); \
4033 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4037 /* The following two macros produce codes (control character or escape
4038 sequence) for ISO2022 single-shift functions (single-shift-2 and
4041 #define ENCODE_SINGLE_SHIFT_2 \
4043 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4044 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4046 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4047 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4051 #define ENCODE_SINGLE_SHIFT_3 \
4053 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4054 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4056 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4057 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4061 /* The following four macros produce codes (control character or
4062 escape sequence) for ISO2022 locking-shift functions (shift-in,
4063 shift-out, locking-shift-2, and locking-shift-3). */
4065 #define ENCODE_SHIFT_IN \
4067 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4068 CODING_ISO_INVOCATION (coding, 0) = 0; \
4072 #define ENCODE_SHIFT_OUT \
4074 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4075 CODING_ISO_INVOCATION (coding, 0) = 1; \
4079 #define ENCODE_LOCKING_SHIFT_2 \
4081 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4082 CODING_ISO_INVOCATION (coding, 0) = 2; \
4086 #define ENCODE_LOCKING_SHIFT_3 \
4088 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4089 CODING_ISO_INVOCATION (coding, 0) = 3; \
4093 /* Produce codes for a DIMENSION1 character whose character set is
4094 CHARSET and whose position-code is C1. Designation and invocation
4095 sequences are also produced in advance if necessary. */
4097 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4099 int id = CHARSET_ID (charset); \
4101 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4102 && id == charset_ascii) \
4104 id = charset_jisx0201_roman; \
4105 charset = CHARSET_FROM_ID (id); \
4108 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4110 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4111 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4113 EMIT_ONE_BYTE (c1 | 0x80); \
4114 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4117 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4119 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4122 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4124 EMIT_ONE_BYTE (c1 | 0x80); \
4128 /* Since CHARSET is not yet invoked to any graphic planes, we \
4129 must invoke it, or, at first, designate it to some graphic \
4130 register. Then repeat the loop to actually produce the \
4132 dst = encode_invocation_designation (charset, coding, dst, \
4137 /* Produce codes for a DIMENSION2 character whose character set is
4138 CHARSET and whose position-codes are C1 and C2. Designation and
4139 invocation codes are also produced in advance if necessary. */
4141 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4143 int id = CHARSET_ID (charset); \
4145 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4146 && id == charset_jisx0208) \
4148 id = charset_jisx0208_1978; \
4149 charset = CHARSET_FROM_ID (id); \
4152 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4154 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4155 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4157 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4158 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4161 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4163 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4166 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4168 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4172 /* Since CHARSET is not yet invoked to any graphic planes, we \
4173 must invoke it, or, at first, designate it to some graphic \
4174 register. Then repeat the loop to actually produce the \
4176 dst = encode_invocation_designation (charset, coding, dst, \
4181 #define ENCODE_ISO_CHARACTER(charset, c) \
4184 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4186 if (CHARSET_DIMENSION (charset) == 1) \
4187 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4189 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4193 /* Produce designation and invocation codes at a place pointed by DST
4194 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4197 static unsigned char *
4198 encode_invocation_designation (struct charset
*charset
,
4199 struct coding_system
*coding
,
4200 unsigned char *dst
, ptrdiff_t *p_nchars
)
4202 bool multibytep
= coding
->dst_multibyte
;
4203 ptrdiff_t produced_chars
= *p_nchars
;
4204 int reg
; /* graphic register number */
4205 int id
= CHARSET_ID (charset
);
4207 /* At first, check designations. */
4208 for (reg
= 0; reg
< 4; reg
++)
4209 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4214 /* CHARSET is not yet designated to any graphic registers. */
4215 /* At first check the requested designation. */
4216 reg
= CODING_ISO_REQUEST (coding
, id
);
4218 /* Since CHARSET requests no special designation, designate it
4219 to graphic register 0. */
4222 ENCODE_DESIGNATION (charset
, reg
, coding
);
4225 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4226 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4228 /* Since the graphic register REG is not invoked to any graphic
4229 planes, invoke it to graphic plane 0. */
4232 case 0: /* graphic register 0 */
4236 case 1: /* graphic register 1 */
4240 case 2: /* graphic register 2 */
4241 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4242 ENCODE_SINGLE_SHIFT_2
;
4244 ENCODE_LOCKING_SHIFT_2
;
4247 case 3: /* graphic register 3 */
4248 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4249 ENCODE_SINGLE_SHIFT_3
;
4251 ENCODE_LOCKING_SHIFT_3
;
4256 *p_nchars
= produced_chars
;
4261 /* Produce codes for designation and invocation to reset the graphic
4262 planes and registers to initial state. */
4263 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4266 struct charset *charset; \
4268 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4270 for (reg = 0; reg < 4; reg++) \
4271 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4272 && (CODING_ISO_DESIGNATION (coding, reg) \
4273 != CODING_ISO_INITIAL (coding, reg))) \
4275 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4276 ENCODE_DESIGNATION (charset, reg, coding); \
4281 /* Produce designation sequences of charsets in the line started from
4282 CHARBUF to a place pointed by DST, and return the number of
4283 produced bytes. DST should not directly point a buffer text area
4284 which may be relocated by char_charset call.
4286 If the current block ends before any end-of-line, we may fail to
4287 find all the necessary designations. */
4290 encode_designation_at_bol (struct coding_system
*coding
,
4291 int *charbuf
, int *charbuf_end
,
4294 unsigned char *orig
= dst
;
4295 struct charset
*charset
;
4296 /* Table of charsets to be designated to each graphic register. */
4298 int c
, found
= 0, reg
;
4299 ptrdiff_t produced_chars
= 0;
4300 bool multibytep
= coding
->dst_multibyte
;
4302 Lisp_Object charset_list
;
4304 attrs
= CODING_ID_ATTRS (coding
->id
);
4305 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4306 if (EQ (charset_list
, Qiso_2022
))
4307 charset_list
= Viso_2022_charset_list
;
4309 for (reg
= 0; reg
< 4; reg
++)
4312 while (charbuf
< charbuf_end
&& found
< 4)
4319 charset
= char_charset (c
, charset_list
, NULL
);
4320 id
= CHARSET_ID (charset
);
4321 reg
= CODING_ISO_REQUEST (coding
, id
);
4322 if (reg
>= 0 && r
[reg
] < 0)
4331 for (reg
= 0; reg
< 4; reg
++)
4333 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4334 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4340 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4343 encode_coding_iso_2022 (struct coding_system
*coding
)
4345 bool multibytep
= coding
->dst_multibyte
;
4346 int *charbuf
= coding
->charbuf
;
4347 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4348 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4349 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4351 bool bol_designation
4352 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4353 && CODING_ISO_BOL (coding
));
4354 ptrdiff_t produced_chars
= 0;
4355 Lisp_Object attrs
, eol_type
, charset_list
;
4356 bool ascii_compatible
;
4358 int preferred_charset_id
= -1;
4360 CODING_GET_INFO (coding
, attrs
, charset_list
);
4361 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4362 if (VECTORP (eol_type
))
4365 setup_iso_safe_charsets (attrs
);
4366 /* Charset list may have been changed. */
4367 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4368 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4371 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4372 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4373 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4375 while (charbuf
< charbuf_end
)
4377 ASSURE_DESTINATION (safe_room
);
4379 if (bol_designation
)
4381 /* We have to produce designation sequences if any now. */
4382 unsigned char desig_buf
[16];
4386 charset_map_loaded
= 0;
4387 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4389 if (charset_map_loaded
4390 && (offset
= coding_change_destination (coding
)))
4395 memcpy (dst
, desig_buf
, nbytes
);
4397 /* We are sure that designation sequences are all ASCII bytes. */
4398 produced_chars
+= nbytes
;
4399 bol_designation
= 0;
4400 ASSURE_DESTINATION (safe_room
);
4407 /* Handle an annotation. */
4410 case CODING_ANNOTATE_COMPOSITION_MASK
:
4411 /* Not yet implemented. */
4413 case CODING_ANNOTATE_CHARSET_MASK
:
4414 preferred_charset_id
= charbuf
[2];
4415 if (preferred_charset_id
>= 0
4416 && NILP (Fmemq (make_number (preferred_charset_id
),
4418 preferred_charset_id
= -1;
4427 /* Now encode the character C. */
4428 if (c
< 0x20 || c
== 0x7F)
4431 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4433 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4434 ENCODE_RESET_PLANE_AND_REGISTER ();
4435 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4439 for (i
= 0; i
< 4; i
++)
4440 CODING_ISO_DESIGNATION (coding
, i
)
4441 = CODING_ISO_INITIAL (coding
, i
);
4443 bol_designation
= ((CODING_ISO_FLAGS (coding
)
4444 & CODING_ISO_FLAG_DESIGNATE_AT_BOL
)
4447 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4448 ENCODE_RESET_PLANE_AND_REGISTER ();
4449 EMIT_ONE_ASCII_BYTE (c
);
4451 else if (ASCII_CHAR_P (c
))
4453 if (ascii_compatible
)
4454 EMIT_ONE_ASCII_BYTE (c
);
4457 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4458 ENCODE_ISO_CHARACTER (charset
, c
);
4461 else if (CHAR_BYTE8_P (c
))
4463 c
= CHAR_TO_BYTE8 (c
);
4468 struct charset
*charset
;
4470 if (preferred_charset_id
>= 0)
4474 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4475 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4477 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4481 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4485 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4487 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4488 charset
= CHARSET_FROM_ID (charset_ascii
);
4492 c
= coding
->default_char
;
4493 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4494 charset_list
, NULL
, charset
);
4497 ENCODE_ISO_CHARACTER (charset
, c
);
4501 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4502 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4504 ASSURE_DESTINATION (safe_room
);
4505 ENCODE_RESET_PLANE_AND_REGISTER ();
4507 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4508 CODING_ISO_BOL (coding
) = bol_designation
;
4509 coding
->produced_char
+= produced_chars
;
4510 coding
->produced
= dst
- coding
->destination
;
4515 /*** 8,9. SJIS and BIG5 handlers ***/
4517 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4518 quite widely. So, for the moment, Emacs supports them in the bare
4519 C code. But, in the future, they may be supported only by CCL. */
4521 /* SJIS is a coding system encoding three character sets: ASCII, right
4522 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4523 as is. A character of charset katakana-jisx0201 is encoded by
4524 "position-code + 0x80". A character of charset japanese-jisx0208
4525 is encoded in 2-byte but two position-codes are divided and shifted
4526 so that it fit in the range below.
4528 --- CODE RANGE of SJIS ---
4529 (character set) (range)
4531 KATAKANA-JISX0201 0xA0 .. 0xDF
4532 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4533 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4534 -------------------------------
4538 /* BIG5 is a coding system encoding two character sets: ASCII and
4539 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4540 character set and is encoded in two-byte.
4542 --- CODE RANGE of BIG5 ---
4543 (character set) (range)
4545 Big5 (1st byte) 0xA1 .. 0xFE
4546 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4547 --------------------------
4551 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4552 Return true if a text is encoded in SJIS. */
4555 detect_coding_sjis (struct coding_system
*coding
,
4556 struct coding_detection_info
*detect_info
)
4558 const unsigned char *src
= coding
->source
, *src_base
;
4559 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4560 bool multibytep
= coding
->src_multibyte
;
4561 ptrdiff_t consumed_chars
= 0;
4564 Lisp_Object attrs
, charset_list
;
4565 int max_first_byte_of_2_byte_code
;
4567 CODING_GET_INFO (coding
, attrs
, charset_list
);
4568 max_first_byte_of_2_byte_code
4569 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4571 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4572 /* A coding system of this category is always ASCII compatible. */
4573 src
+= coding
->head_ascii
;
4581 if ((c
>= 0x81 && c
<= 0x9F)
4582 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4585 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4587 found
= CATEGORY_MASK_SJIS
;
4589 else if (c
>= 0xA0 && c
< 0xE0)
4590 found
= CATEGORY_MASK_SJIS
;
4594 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4598 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4600 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4603 detect_info
->found
|= found
;
4607 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4608 Return true if a text is encoded in BIG5. */
4611 detect_coding_big5 (struct coding_system
*coding
,
4612 struct coding_detection_info
*detect_info
)
4614 const unsigned char *src
= coding
->source
, *src_base
;
4615 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4616 bool multibytep
= coding
->src_multibyte
;
4617 ptrdiff_t consumed_chars
= 0;
4621 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4622 /* A coding system of this category is always ASCII compatible. */
4623 src
+= coding
->head_ascii
;
4634 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4636 found
= CATEGORY_MASK_BIG5
;
4641 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4645 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4647 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4650 detect_info
->found
|= found
;
4654 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4657 decode_coding_sjis (struct coding_system
*coding
)
4659 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4660 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4661 const unsigned char *src_base
;
4662 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4663 /* We may produce one charset annotation in one loop and one more at
4666 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4667 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4668 bool multibytep
= coding
->src_multibyte
;
4669 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4670 struct charset
*charset_kanji2
;
4671 Lisp_Object attrs
, charset_list
, val
;
4672 ptrdiff_t char_offset
= coding
->produced_char
;
4673 ptrdiff_t last_offset
= char_offset
;
4674 int last_id
= charset_ascii
;
4676 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4677 int byte_after_cr
= -1;
4679 CODING_GET_INFO (coding
, attrs
, charset_list
);
4682 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4683 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4684 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4685 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4690 struct charset
*charset
;
4693 consumed_chars_base
= consumed_chars
;
4695 if (charbuf
>= charbuf_end
)
4697 if (byte_after_cr
>= 0)
4702 if (byte_after_cr
>= 0)
4703 c
= byte_after_cr
, byte_after_cr
= -1;
4710 if (eol_dos
&& c
== '\r')
4711 ONE_MORE_BYTE (byte_after_cr
);
4712 charset
= charset_roman
;
4714 else if (c
== 0x80 || c
== 0xA0)
4716 else if (c
>= 0xA1 && c
<= 0xDF)
4718 /* SJIS -> JISX0201-Kana */
4720 charset
= charset_kana
;
4724 /* SJIS -> JISX0208 */
4726 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4730 charset
= charset_kanji
;
4732 else if (c
<= 0xFC && charset_kanji2
)
4734 /* SJIS -> JISX0213-2 */
4736 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4740 charset
= charset_kanji2
;
4744 if (charset
->id
!= charset_ascii
4745 && last_id
!= charset
->id
)
4747 if (last_id
!= charset_ascii
)
4748 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4749 last_id
= charset
->id
;
4750 last_offset
= char_offset
;
4752 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4759 consumed_chars
= consumed_chars_base
;
4761 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4767 if (last_id
!= charset_ascii
)
4768 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4769 coding
->consumed_char
+= consumed_chars_base
;
4770 coding
->consumed
= src_base
- coding
->source
;
4771 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4775 decode_coding_big5 (struct coding_system
*coding
)
4777 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4778 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4779 const unsigned char *src_base
;
4780 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4781 /* We may produce one charset annotation in one loop and one more at
4784 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4785 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4786 bool multibytep
= coding
->src_multibyte
;
4787 struct charset
*charset_roman
, *charset_big5
;
4788 Lisp_Object attrs
, charset_list
, val
;
4789 ptrdiff_t char_offset
= coding
->produced_char
;
4790 ptrdiff_t last_offset
= char_offset
;
4791 int last_id
= charset_ascii
;
4793 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4794 int byte_after_cr
= -1;
4796 CODING_GET_INFO (coding
, attrs
, charset_list
);
4798 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4799 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4804 struct charset
*charset
;
4807 consumed_chars_base
= consumed_chars
;
4809 if (charbuf
>= charbuf_end
)
4811 if (byte_after_cr
>= 0)
4816 if (byte_after_cr
>= 0)
4817 c
= byte_after_cr
, byte_after_cr
= -1;
4825 if (eol_dos
&& c
== '\r')
4826 ONE_MORE_BYTE (byte_after_cr
);
4827 charset
= charset_roman
;
4832 if (c
< 0xA1 || c
> 0xFE)
4835 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4838 charset
= charset_big5
;
4840 if (charset
->id
!= charset_ascii
4841 && last_id
!= charset
->id
)
4843 if (last_id
!= charset_ascii
)
4844 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4845 last_id
= charset
->id
;
4846 last_offset
= char_offset
;
4848 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4855 consumed_chars
= consumed_chars_base
;
4857 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4863 if (last_id
!= charset_ascii
)
4864 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4865 coding
->consumed_char
+= consumed_chars_base
;
4866 coding
->consumed
= src_base
- coding
->source
;
4867 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4870 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4871 This function can encode charsets `ascii', `katakana-jisx0201',
4872 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4873 are sure that all these charsets are registered as official charset
4874 (i.e. do not have extended leading-codes). Characters of other
4875 charsets are produced without any encoding. */
4878 encode_coding_sjis (struct coding_system
*coding
)
4880 bool multibytep
= coding
->dst_multibyte
;
4881 int *charbuf
= coding
->charbuf
;
4882 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4883 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4884 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4886 ptrdiff_t produced_chars
= 0;
4887 Lisp_Object attrs
, charset_list
, val
;
4888 bool ascii_compatible
;
4889 struct charset
*charset_kanji
, *charset_kana
;
4890 struct charset
*charset_kanji2
;
4893 CODING_GET_INFO (coding
, attrs
, charset_list
);
4894 val
= XCDR (charset_list
);
4895 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4896 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4897 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4899 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4901 while (charbuf
< charbuf_end
)
4903 ASSURE_DESTINATION (safe_room
);
4905 /* Now encode the character C. */
4906 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4907 EMIT_ONE_ASCII_BYTE (c
);
4908 else if (CHAR_BYTE8_P (c
))
4910 c
= CHAR_TO_BYTE8 (c
);
4916 struct charset
*charset
;
4917 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4922 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4924 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4925 charset
= CHARSET_FROM_ID (charset_ascii
);
4929 c
= coding
->default_char
;
4930 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4931 charset_list
, &code
, charset
);
4934 if (code
== CHARSET_INVALID_CODE (charset
))
4936 if (charset
== charset_kanji
)
4940 c1
= code
>> 8, c2
= code
& 0xFF;
4941 EMIT_TWO_BYTES (c1
, c2
);
4943 else if (charset
== charset_kana
)
4944 EMIT_ONE_BYTE (code
| 0x80);
4945 else if (charset_kanji2
&& charset
== charset_kanji2
)
4950 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4952 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4954 JIS_TO_SJIS2 (code
);
4955 c1
= code
>> 8, c2
= code
& 0xFF;
4956 EMIT_TWO_BYTES (c1
, c2
);
4959 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4962 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4965 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4966 coding
->produced_char
+= produced_chars
;
4967 coding
->produced
= dst
- coding
->destination
;
4972 encode_coding_big5 (struct coding_system
*coding
)
4974 bool multibytep
= coding
->dst_multibyte
;
4975 int *charbuf
= coding
->charbuf
;
4976 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4977 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4978 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4980 ptrdiff_t produced_chars
= 0;
4981 Lisp_Object attrs
, charset_list
, val
;
4982 bool ascii_compatible
;
4983 struct charset
*charset_big5
;
4986 CODING_GET_INFO (coding
, attrs
, charset_list
);
4987 val
= XCDR (charset_list
);
4988 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4989 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4991 while (charbuf
< charbuf_end
)
4993 ASSURE_DESTINATION (safe_room
);
4995 /* Now encode the character C. */
4996 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4997 EMIT_ONE_ASCII_BYTE (c
);
4998 else if (CHAR_BYTE8_P (c
))
5000 c
= CHAR_TO_BYTE8 (c
);
5006 struct charset
*charset
;
5007 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5012 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5014 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5015 charset
= CHARSET_FROM_ID (charset_ascii
);
5019 c
= coding
->default_char
;
5020 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
5021 charset_list
, &code
, charset
);
5024 if (code
== CHARSET_INVALID_CODE (charset
))
5026 if (charset
== charset_big5
)
5030 c1
= code
>> 8, c2
= code
& 0xFF;
5031 EMIT_TWO_BYTES (c1
, c2
);
5034 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5037 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5038 coding
->produced_char
+= produced_chars
;
5039 coding
->produced
= dst
- coding
->destination
;
5044 /*** 10. CCL handlers ***/
5046 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5047 Return true if a text is encoded in a coding system of which
5048 encoder/decoder are written in CCL program. */
5051 detect_coding_ccl (struct coding_system
*coding
,
5052 struct coding_detection_info
*detect_info
)
5054 const unsigned char *src
= coding
->source
, *src_base
;
5055 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5056 bool multibytep
= coding
->src_multibyte
;
5057 ptrdiff_t consumed_chars
= 0;
5059 unsigned char *valids
;
5060 ptrdiff_t head_ascii
= coding
->head_ascii
;
5063 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5065 coding
= &coding_categories
[coding_category_ccl
];
5066 valids
= CODING_CCL_VALIDS (coding
);
5067 attrs
= CODING_ID_ATTRS (coding
->id
);
5068 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5077 if (c
< 0 || ! valids
[c
])
5079 if ((valids
[c
] > 1))
5080 found
= CATEGORY_MASK_CCL
;
5082 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5086 detect_info
->found
|= found
;
5091 decode_coding_ccl (struct coding_system
*coding
)
5093 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5094 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5095 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5096 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5097 ptrdiff_t consumed_chars
= 0;
5098 bool multibytep
= coding
->src_multibyte
;
5099 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5100 int source_charbuf
[1024];
5101 int source_byteidx
[1025];
5102 Lisp_Object attrs
, charset_list
;
5104 CODING_GET_INFO (coding
, attrs
, charset_list
);
5108 const unsigned char *p
= src
;
5113 while (i
< 1024 && p
< src_end
)
5115 source_byteidx
[i
] = p
- src
;
5116 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5118 source_byteidx
[i
] = p
- src
;
5121 while (i
< 1024 && p
< src_end
)
5122 source_charbuf
[i
++] = *p
++;
5124 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5125 ccl
->last_block
= 1;
5126 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5128 charbuf
+= ccl
->produced
;
5130 src
+= source_byteidx
[ccl
->consumed
];
5132 src
+= ccl
->consumed
;
5133 consumed_chars
+= ccl
->consumed
;
5134 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5138 switch (ccl
->status
)
5140 case CCL_STAT_SUSPEND_BY_SRC
:
5141 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5143 case CCL_STAT_SUSPEND_BY_DST
:
5144 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5147 case CCL_STAT_INVALID_CMD
:
5148 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5151 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5154 coding
->consumed_char
+= consumed_chars
;
5155 coding
->consumed
= src
- coding
->source
;
5156 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5160 encode_coding_ccl (struct coding_system
*coding
)
5162 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5163 bool multibytep
= coding
->dst_multibyte
;
5164 int *charbuf
= coding
->charbuf
;
5165 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5166 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5167 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5168 int destination_charbuf
[1024];
5169 ptrdiff_t produced_chars
= 0;
5171 Lisp_Object attrs
, charset_list
;
5173 CODING_GET_INFO (coding
, attrs
, charset_list
);
5174 if (coding
->consumed_char
== coding
->src_chars
5175 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5176 ccl
->last_block
= 1;
5180 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5181 charbuf_end
- charbuf
, 1024, charset_list
);
5184 ASSURE_DESTINATION (ccl
->produced
* 2);
5185 for (i
= 0; i
< ccl
->produced
; i
++)
5186 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5190 ASSURE_DESTINATION (ccl
->produced
);
5191 for (i
= 0; i
< ccl
->produced
; i
++)
5192 *dst
++ = destination_charbuf
[i
] & 0xFF;
5193 produced_chars
+= ccl
->produced
;
5195 charbuf
+= ccl
->consumed
;
5196 if (ccl
->status
== CCL_STAT_QUIT
5197 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5200 while (charbuf
< charbuf_end
);
5202 switch (ccl
->status
)
5204 case CCL_STAT_SUSPEND_BY_SRC
:
5205 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5207 case CCL_STAT_SUSPEND_BY_DST
:
5208 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5211 case CCL_STAT_INVALID_CMD
:
5212 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5215 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5219 coding
->produced_char
+= produced_chars
;
5220 coding
->produced
= dst
- coding
->destination
;
5225 /*** 10, 11. no-conversion handlers ***/
5227 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5230 decode_coding_raw_text (struct coding_system
*coding
)
5233 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5235 coding
->chars_at_source
= 1;
5236 coding
->consumed_char
= coding
->src_chars
;
5237 coding
->consumed
= coding
->src_bytes
;
5238 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5240 coding
->consumed_char
--;
5242 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5245 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5249 encode_coding_raw_text (struct coding_system
*coding
)
5251 bool multibytep
= coding
->dst_multibyte
;
5252 int *charbuf
= coding
->charbuf
;
5253 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5254 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5255 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5256 ptrdiff_t produced_chars
= 0;
5261 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5263 if (coding
->src_multibyte
)
5264 while (charbuf
< charbuf_end
)
5266 ASSURE_DESTINATION (safe_room
);
5268 if (ASCII_CHAR_P (c
))
5269 EMIT_ONE_ASCII_BYTE (c
);
5270 else if (CHAR_BYTE8_P (c
))
5272 c
= CHAR_TO_BYTE8 (c
);
5277 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5279 CHAR_STRING_ADVANCE (c
, p1
);
5282 EMIT_ONE_BYTE (*p0
);
5289 while (charbuf
< charbuf_end
)
5291 ASSURE_DESTINATION (safe_room
);
5298 if (coding
->src_multibyte
)
5300 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5302 while (charbuf
< charbuf_end
)
5304 ASSURE_DESTINATION (safe_room
);
5306 if (ASCII_CHAR_P (c
))
5308 else if (CHAR_BYTE8_P (c
))
5309 *dst
++ = CHAR_TO_BYTE8 (c
);
5311 CHAR_STRING_ADVANCE (c
, dst
);
5316 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5317 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5318 *dst
++ = *charbuf
++;
5320 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5322 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5323 coding
->produced_char
+= produced_chars
;
5324 coding
->produced
= dst
- coding
->destination
;
5328 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5329 Return true if a text is encoded in a charset-based coding system. */
5332 detect_coding_charset (struct coding_system
*coding
,
5333 struct coding_detection_info
*detect_info
)
5335 const unsigned char *src
= coding
->source
, *src_base
;
5336 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5337 bool multibytep
= coding
->src_multibyte
;
5338 ptrdiff_t consumed_chars
= 0;
5339 Lisp_Object attrs
, valids
, name
;
5341 ptrdiff_t head_ascii
= coding
->head_ascii
;
5342 bool check_latin_extra
= 0;
5344 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5346 coding
= &coding_categories
[coding_category_charset
];
5347 attrs
= CODING_ID_ATTRS (coding
->id
);
5348 valids
= AREF (attrs
, coding_attr_charset_valids
);
5349 name
= CODING_ID_NAME (coding
->id
);
5350 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5351 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5352 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5353 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5354 check_latin_extra
= 1;
5356 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5363 struct charset
*charset
;
5370 val
= AREF (valids
, c
);
5376 && check_latin_extra
5377 && (!VECTORP (Vlatin_extra_code_table
)
5378 || NILP (AREF (Vlatin_extra_code_table
, c
))))
5380 found
= CATEGORY_MASK_CHARSET
;
5384 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5385 dim
= CHARSET_DIMENSION (charset
);
5386 for (idx
= 1; idx
< dim
; idx
++)
5391 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5392 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5401 for (; CONSP (val
); val
= XCDR (val
))
5403 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5404 dim
= CHARSET_DIMENSION (charset
);
5410 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5411 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5426 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5430 detect_info
->found
|= found
;
5435 decode_coding_charset (struct coding_system
*coding
)
5437 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5438 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5439 const unsigned char *src_base
;
5440 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5441 /* We may produce one charset annotation in one loop and one more at
5444 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5445 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
5446 bool multibytep
= coding
->src_multibyte
;
5447 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5449 ptrdiff_t char_offset
= coding
->produced_char
;
5450 ptrdiff_t last_offset
= char_offset
;
5451 int last_id
= charset_ascii
;
5453 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5454 int byte_after_cr
= -1;
5456 valids
= AREF (attrs
, coding_attr_charset_valids
);
5462 struct charset
*charset
;
5468 consumed_chars_base
= consumed_chars
;
5470 if (charbuf
>= charbuf_end
)
5472 if (byte_after_cr
>= 0)
5477 if (byte_after_cr
>= 0)
5485 if (eol_dos
&& c
== '\r')
5486 ONE_MORE_BYTE (byte_after_cr
);
5492 val
= AREF (valids
, c
);
5493 if (! INTEGERP (val
) && ! CONSP (val
))
5497 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5498 dim
= CHARSET_DIMENSION (charset
);
5502 code
= (code
<< 8) | c
;
5505 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5510 /* VAL is a list of charset IDs. It is assured that the
5511 list is sorted by charset dimensions (smaller one
5515 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5516 dim
= CHARSET_DIMENSION (charset
);
5520 code
= (code
<< 8) | c
;
5523 CODING_DECODE_CHAR (coding
, src
, src_base
,
5524 src_end
, charset
, code
, c
);
5532 if (charset
->id
!= charset_ascii
5533 && last_id
!= charset
->id
)
5535 if (last_id
!= charset_ascii
)
5536 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5537 last_id
= charset
->id
;
5538 last_offset
= char_offset
;
5547 consumed_chars
= consumed_chars_base
;
5549 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5555 if (last_id
!= charset_ascii
)
5556 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5557 coding
->consumed_char
+= consumed_chars_base
;
5558 coding
->consumed
= src_base
- coding
->source
;
5559 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5563 encode_coding_charset (struct coding_system
*coding
)
5565 bool multibytep
= coding
->dst_multibyte
;
5566 int *charbuf
= coding
->charbuf
;
5567 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5568 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5569 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5570 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5571 ptrdiff_t produced_chars
= 0;
5572 Lisp_Object attrs
, charset_list
;
5573 bool ascii_compatible
;
5576 CODING_GET_INFO (coding
, attrs
, charset_list
);
5577 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5579 while (charbuf
< charbuf_end
)
5581 struct charset
*charset
;
5584 ASSURE_DESTINATION (safe_room
);
5586 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5587 EMIT_ONE_ASCII_BYTE (c
);
5588 else if (CHAR_BYTE8_P (c
))
5590 c
= CHAR_TO_BYTE8 (c
);
5595 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5600 if (CHARSET_DIMENSION (charset
) == 1)
5601 EMIT_ONE_BYTE (code
);
5602 else if (CHARSET_DIMENSION (charset
) == 2)
5603 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5604 else if (CHARSET_DIMENSION (charset
) == 3)
5605 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5607 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5608 (code
>> 8) & 0xFF, code
& 0xFF);
5612 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5613 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5615 c
= coding
->default_char
;
5621 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5622 coding
->produced_char
+= produced_chars
;
5623 coding
->produced
= dst
- coding
->destination
;
5628 /*** 7. C library functions ***/
5630 /* Setup coding context CODING from information about CODING_SYSTEM.
5631 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5632 CODING_SYSTEM is invalid, signal an error. */
5635 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5638 Lisp_Object eol_type
;
5639 Lisp_Object coding_type
;
5642 if (NILP (coding_system
))
5643 coding_system
= Qundecided
;
5645 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5647 attrs
= CODING_ID_ATTRS (coding
->id
);
5648 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5651 coding
->head_ascii
= -1;
5652 if (VECTORP (eol_type
))
5653 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5654 | CODING_REQUIRE_DETECTION_MASK
);
5655 else if (! EQ (eol_type
, Qunix
))
5656 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5657 | CODING_REQUIRE_ENCODING_MASK
);
5659 coding
->common_flags
= 0;
5660 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5661 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5662 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5663 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5664 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5665 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5667 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5668 coding
->max_charset_id
= SCHARS (val
) - 1;
5669 coding
->safe_charsets
= SDATA (val
);
5670 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5671 coding
->carryover_bytes
= 0;
5673 coding_type
= CODING_ATTR_TYPE (attrs
);
5674 if (EQ (coding_type
, Qundecided
))
5676 coding
->detector
= NULL
;
5677 coding
->decoder
= decode_coding_raw_text
;
5678 coding
->encoder
= encode_coding_raw_text
;
5679 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5681 else if (EQ (coding_type
, Qiso_2022
))
5684 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5686 /* Invoke graphic register 0 to plane 0. */
5687 CODING_ISO_INVOCATION (coding
, 0) = 0;
5688 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5689 CODING_ISO_INVOCATION (coding
, 1)
5690 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5691 /* Setup the initial status of designation. */
5692 for (i
= 0; i
< 4; i
++)
5693 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5694 /* Not single shifting initially. */
5695 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5696 /* Beginning of buffer should also be regarded as bol. */
5697 CODING_ISO_BOL (coding
) = 1;
5698 coding
->detector
= detect_coding_iso_2022
;
5699 coding
->decoder
= decode_coding_iso_2022
;
5700 coding
->encoder
= encode_coding_iso_2022
;
5701 if (flags
& CODING_ISO_FLAG_SAFE
)
5702 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5703 coding
->common_flags
5704 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5705 | CODING_REQUIRE_FLUSHING_MASK
);
5706 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5707 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5708 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5709 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5710 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5712 setup_iso_safe_charsets (attrs
);
5713 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5714 coding
->max_charset_id
= SCHARS (val
) - 1;
5715 coding
->safe_charsets
= SDATA (val
);
5717 CODING_ISO_FLAGS (coding
) = flags
;
5718 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5719 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5720 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5721 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5723 else if (EQ (coding_type
, Qcharset
))
5725 coding
->detector
= detect_coding_charset
;
5726 coding
->decoder
= decode_coding_charset
;
5727 coding
->encoder
= encode_coding_charset
;
5728 coding
->common_flags
5729 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5731 else if (EQ (coding_type
, Qutf_8
))
5733 val
= AREF (attrs
, coding_attr_utf_bom
);
5734 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5735 : EQ (val
, Qt
) ? utf_with_bom
5737 coding
->detector
= detect_coding_utf_8
;
5738 coding
->decoder
= decode_coding_utf_8
;
5739 coding
->encoder
= encode_coding_utf_8
;
5740 coding
->common_flags
5741 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5742 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5743 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5745 else if (EQ (coding_type
, Qutf_16
))
5747 val
= AREF (attrs
, coding_attr_utf_bom
);
5748 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5749 : EQ (val
, Qt
) ? utf_with_bom
5751 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5752 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5753 : utf_16_little_endian
);
5754 CODING_UTF_16_SURROGATE (coding
) = 0;
5755 coding
->detector
= detect_coding_utf_16
;
5756 coding
->decoder
= decode_coding_utf_16
;
5757 coding
->encoder
= encode_coding_utf_16
;
5758 coding
->common_flags
5759 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5760 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5761 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5763 else if (EQ (coding_type
, Qccl
))
5765 coding
->detector
= detect_coding_ccl
;
5766 coding
->decoder
= decode_coding_ccl
;
5767 coding
->encoder
= encode_coding_ccl
;
5768 coding
->common_flags
5769 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5770 | CODING_REQUIRE_FLUSHING_MASK
);
5772 else if (EQ (coding_type
, Qemacs_mule
))
5774 coding
->detector
= detect_coding_emacs_mule
;
5775 coding
->decoder
= decode_coding_emacs_mule
;
5776 coding
->encoder
= encode_coding_emacs_mule
;
5777 coding
->common_flags
5778 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5779 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5780 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5782 Lisp_Object tail
, safe_charsets
;
5783 int max_charset_id
= 0;
5785 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5787 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5788 max_charset_id
= XFASTINT (XCAR (tail
));
5789 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5790 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5791 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5793 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5794 coding
->max_charset_id
= max_charset_id
;
5795 coding
->safe_charsets
= SDATA (safe_charsets
);
5797 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5798 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5800 else if (EQ (coding_type
, Qshift_jis
))
5802 coding
->detector
= detect_coding_sjis
;
5803 coding
->decoder
= decode_coding_sjis
;
5804 coding
->encoder
= encode_coding_sjis
;
5805 coding
->common_flags
5806 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5808 else if (EQ (coding_type
, Qbig5
))
5810 coding
->detector
= detect_coding_big5
;
5811 coding
->decoder
= decode_coding_big5
;
5812 coding
->encoder
= encode_coding_big5
;
5813 coding
->common_flags
5814 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5816 else /* EQ (coding_type, Qraw_text) */
5818 coding
->detector
= NULL
;
5819 coding
->decoder
= decode_coding_raw_text
;
5820 coding
->encoder
= encode_coding_raw_text
;
5821 if (! EQ (eol_type
, Qunix
))
5823 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5824 if (! VECTORP (eol_type
))
5825 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5833 /* Return a list of charsets supported by CODING. */
5836 coding_charset_list (struct coding_system
*coding
)
5838 Lisp_Object attrs
, charset_list
;
5840 CODING_GET_INFO (coding
, attrs
, charset_list
);
5841 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5843 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5845 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5846 charset_list
= Viso_2022_charset_list
;
5848 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5850 charset_list
= Vemacs_mule_charset_list
;
5852 return charset_list
;
5856 /* Return a list of charsets supported by CODING-SYSTEM. */
5859 coding_system_charset_list (Lisp_Object coding_system
)
5862 Lisp_Object attrs
, charset_list
;
5864 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5865 attrs
= CODING_ID_ATTRS (id
);
5867 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5869 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5871 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5872 charset_list
= Viso_2022_charset_list
;
5874 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5876 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5878 charset_list
= Vemacs_mule_charset_list
;
5882 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5884 return charset_list
;
5888 /* Return raw-text or one of its subsidiaries that has the same
5889 eol_type as CODING-SYSTEM. */
5892 raw_text_coding_system (Lisp_Object coding_system
)
5894 Lisp_Object spec
, attrs
;
5895 Lisp_Object eol_type
, raw_text_eol_type
;
5897 if (NILP (coding_system
))
5899 spec
= CODING_SYSTEM_SPEC (coding_system
);
5900 attrs
= AREF (spec
, 0);
5902 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5903 return coding_system
;
5905 eol_type
= AREF (spec
, 2);
5906 if (VECTORP (eol_type
))
5908 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5909 raw_text_eol_type
= AREF (spec
, 2);
5910 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5911 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5912 : AREF (raw_text_eol_type
, 2));
5916 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5917 the subsidiary that has the same eol-spec as PARENT (if it is not
5918 nil and specifies end-of-line format) or the system's setting
5919 (system_eol_type). */
5922 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5924 Lisp_Object spec
, eol_type
;
5926 if (NILP (coding_system
))
5927 coding_system
= Qraw_text
;
5928 spec
= CODING_SYSTEM_SPEC (coding_system
);
5929 eol_type
= AREF (spec
, 2);
5930 if (VECTORP (eol_type
))
5932 Lisp_Object parent_eol_type
;
5934 if (! NILP (parent
))
5936 Lisp_Object parent_spec
;
5938 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5939 parent_eol_type
= AREF (parent_spec
, 2);
5940 if (VECTORP (parent_eol_type
))
5941 parent_eol_type
= system_eol_type
;
5944 parent_eol_type
= system_eol_type
;
5945 if (EQ (parent_eol_type
, Qunix
))
5946 coding_system
= AREF (eol_type
, 0);
5947 else if (EQ (parent_eol_type
, Qdos
))
5948 coding_system
= AREF (eol_type
, 1);
5949 else if (EQ (parent_eol_type
, Qmac
))
5950 coding_system
= AREF (eol_type
, 2);
5952 return coding_system
;
5956 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5957 decided for writing to a process. If not, complement them, and
5958 return a new coding system. */
5961 complement_process_encoding_system (Lisp_Object coding_system
)
5963 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5964 Lisp_Object spec
, attrs
;
5967 for (i
= 0; i
< 3; i
++)
5970 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
5972 coding_system
= preferred_coding_system ();
5973 spec
= CODING_SYSTEM_SPEC (coding_system
);
5976 attrs
= AREF (spec
, 0);
5977 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
5978 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
5979 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
5980 eol_base
= coding_system
;
5981 if (! NILP (coding_base
) && ! NILP (eol_base
))
5986 /* The original CODING_SYSTEM didn't specify text-conversion or
5987 eol-conversion. Be sure that we return a fully complemented
5989 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
5990 return coding_system
;
5994 /* Emacs has a mechanism to automatically detect a coding system if it
5995 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5996 it's impossible to distinguish some coding systems accurately
5997 because they use the same range of codes. So, at first, coding
5998 systems are categorized into 7, those are:
6000 o coding-category-emacs-mule
6002 The category for a coding system which has the same code range
6003 as Emacs' internal format. Assigned the coding-system (Lisp
6004 symbol) `emacs-mule' by default.
6006 o coding-category-sjis
6008 The category for a coding system which has the same code range
6009 as SJIS. Assigned the coding-system (Lisp
6010 symbol) `japanese-shift-jis' by default.
6012 o coding-category-iso-7
6014 The category for a coding system which has the same code range
6015 as ISO2022 of 7-bit environment. This doesn't use any locking
6016 shift and single shift functions. This can encode/decode all
6017 charsets. Assigned the coding-system (Lisp symbol)
6018 `iso-2022-7bit' by default.
6020 o coding-category-iso-7-tight
6022 Same as coding-category-iso-7 except that this can
6023 encode/decode only the specified charsets.
6025 o coding-category-iso-8-1
6027 The category for a coding system which has the same code range
6028 as ISO2022 of 8-bit environment and graphic plane 1 used only
6029 for DIMENSION1 charset. This doesn't use any locking shift
6030 and single shift functions. Assigned the coding-system (Lisp
6031 symbol) `iso-latin-1' by default.
6033 o coding-category-iso-8-2
6035 The category for a coding system which has the same code range
6036 as ISO2022 of 8-bit environment and graphic plane 1 used only
6037 for DIMENSION2 charset. This doesn't use any locking shift
6038 and single shift functions. Assigned the coding-system (Lisp
6039 symbol) `japanese-iso-8bit' by default.
6041 o coding-category-iso-7-else
6043 The category for a coding system which has the same code range
6044 as ISO2022 of 7-bit environment but uses locking shift or
6045 single shift functions. Assigned the coding-system (Lisp
6046 symbol) `iso-2022-7bit-lock' by default.
6048 o coding-category-iso-8-else
6050 The category for a coding system which has the same code range
6051 as ISO2022 of 8-bit environment but uses locking shift or
6052 single shift functions. Assigned the coding-system (Lisp
6053 symbol) `iso-2022-8bit-ss2' by default.
6055 o coding-category-big5
6057 The category for a coding system which has the same code range
6058 as BIG5. Assigned the coding-system (Lisp symbol)
6059 `cn-big5' by default.
6061 o coding-category-utf-8
6063 The category for a coding system which has the same code range
6064 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6065 symbol) `utf-8' by default.
6067 o coding-category-utf-16-be
6069 The category for a coding system in which a text has an
6070 Unicode signature (cf. Unicode Standard) in the order of BIG
6071 endian at the head. Assigned the coding-system (Lisp symbol)
6072 `utf-16-be' by default.
6074 o coding-category-utf-16-le
6076 The category for a coding system in which a text has an
6077 Unicode signature (cf. Unicode Standard) in the order of
6078 LITTLE endian at the head. Assigned the coding-system (Lisp
6079 symbol) `utf-16-le' by default.
6081 o coding-category-ccl
6083 The category for a coding system of which encoder/decoder is
6084 written in CCL programs. The default value is nil, i.e., no
6085 coding system is assigned.
6087 o coding-category-binary
6089 The category for a coding system not categorized in any of the
6090 above. Assigned the coding-system (Lisp symbol)
6091 `no-conversion' by default.
6093 Each of them is a Lisp symbol and the value is an actual
6094 `coding-system's (this is also a Lisp symbol) assigned by a user.
6095 What Emacs does actually is to detect a category of coding system.
6096 Then, it uses a `coding-system' assigned to it. If Emacs can't
6097 decide only one possible category, it selects a category of the
6098 highest priority. Priorities of categories are also specified by a
6099 user in a Lisp variable `coding-category-list'.
6103 #define EOL_SEEN_NONE 0
6104 #define EOL_SEEN_LF 1
6105 #define EOL_SEEN_CR 2
6106 #define EOL_SEEN_CRLF 4
6108 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6109 SOURCE is encoded. If CATEGORY is one of
6110 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6111 two-byte, else they are encoded by one-byte.
6113 Return one of EOL_SEEN_XXX. */
6115 #define MAX_EOL_CHECK_COUNT 3
6118 detect_eol (const unsigned char *source
, ptrdiff_t src_bytes
,
6119 enum coding_category category
)
6121 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6124 int eol_seen
= EOL_SEEN_NONE
;
6126 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6128 bool msb
= category
== (coding_category_utf_16_le
6129 | coding_category_utf_16_le_nosig
);
6132 while (src
+ 1 < src_end
)
6135 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6140 this_eol
= EOL_SEEN_LF
;
6141 else if (src
+ 3 >= src_end
6142 || src
[msb
+ 2] != 0
6143 || src
[lsb
+ 2] != '\n')
6144 this_eol
= EOL_SEEN_CR
;
6147 this_eol
= EOL_SEEN_CRLF
;
6151 if (eol_seen
== EOL_SEEN_NONE
)
6152 /* This is the first end-of-line. */
6153 eol_seen
= this_eol
;
6154 else if (eol_seen
!= this_eol
)
6156 /* The found type is different from what found before.
6157 Allow for stray ^M characters in DOS EOL files. */
6158 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6159 || (eol_seen
== EOL_SEEN_CRLF
6160 && this_eol
== EOL_SEEN_CR
))
6161 eol_seen
= EOL_SEEN_CRLF
;
6164 eol_seen
= EOL_SEEN_LF
;
6168 if (++total
== MAX_EOL_CHECK_COUNT
)
6175 while (src
< src_end
)
6178 if (c
== '\n' || c
== '\r')
6183 this_eol
= EOL_SEEN_LF
;
6184 else if (src
>= src_end
|| *src
!= '\n')
6185 this_eol
= EOL_SEEN_CR
;
6187 this_eol
= EOL_SEEN_CRLF
, src
++;
6189 if (eol_seen
== EOL_SEEN_NONE
)
6190 /* This is the first end-of-line. */
6191 eol_seen
= this_eol
;
6192 else if (eol_seen
!= this_eol
)
6194 /* The found type is different from what found before.
6195 Allow for stray ^M characters in DOS EOL files. */
6196 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6197 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6198 eol_seen
= EOL_SEEN_CRLF
;
6201 eol_seen
= EOL_SEEN_LF
;
6205 if (++total
== MAX_EOL_CHECK_COUNT
)
6214 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6216 Lisp_Object eol_type
;
6218 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6219 if (eol_seen
& EOL_SEEN_LF
)
6221 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6224 else if (eol_seen
& EOL_SEEN_CRLF
)
6226 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6229 else if (eol_seen
& EOL_SEEN_CR
)
6231 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6237 /* Detect how a text specified in CODING is encoded. If a coding
6238 system is detected, update fields of CODING by the detected coding
6242 detect_coding (struct coding_system
*coding
)
6244 const unsigned char *src
, *src_end
;
6245 unsigned int saved_mode
= coding
->mode
;
6247 coding
->consumed
= coding
->consumed_char
= 0;
6248 coding
->produced
= coding
->produced_char
= 0;
6249 coding_set_source (coding
);
6251 src_end
= coding
->source
+ coding
->src_bytes
;
6252 coding
->head_ascii
= 0;
6254 /* If we have not yet decided the text encoding type, detect it
6256 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6259 struct coding_detection_info detect_info
;
6260 bool null_byte_found
= 0, eight_bit_found
= 0;
6262 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6263 for (src
= coding
->source
; src
< src_end
; src
++)
6268 eight_bit_found
= 1;
6269 if (null_byte_found
)
6274 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6275 && ! inhibit_iso_escape_detection
6276 && ! detect_info
.checked
)
6278 if (detect_coding_iso_2022 (coding
, &detect_info
))
6280 /* We have scanned the whole data. */
6281 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6283 /* We didn't find an 8-bit code. We may
6284 have found a null-byte, but it's very
6285 rare that a binary file conforms to
6288 coding
->head_ascii
= src
- coding
->source
;
6290 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6294 else if (! c
&& !inhibit_null_byte_detection
)
6296 null_byte_found
= 1;
6297 if (eight_bit_found
)
6300 if (! eight_bit_found
)
6301 coding
->head_ascii
++;
6303 else if (! eight_bit_found
)
6304 coding
->head_ascii
++;
6307 if (null_byte_found
|| eight_bit_found
6308 || coding
->head_ascii
< coding
->src_bytes
6309 || detect_info
.found
)
6311 enum coding_category category
;
6312 struct coding_system
*this;
6314 if (coding
->head_ascii
== coding
->src_bytes
)
6315 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6316 for (i
= 0; i
< coding_category_raw_text
; i
++)
6318 category
= coding_priorities
[i
];
6319 this = coding_categories
+ category
;
6320 if (detect_info
.found
& (1 << category
))
6325 if (null_byte_found
)
6327 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6328 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6330 for (i
= 0; i
< coding_category_raw_text
; i
++)
6332 category
= coding_priorities
[i
];
6333 this = coding_categories
+ category
;
6336 /* No coding system of this category is defined. */
6337 detect_info
.rejected
|= (1 << category
);
6339 else if (category
>= coding_category_raw_text
)
6341 else if (detect_info
.checked
& (1 << category
))
6343 if (detect_info
.found
& (1 << category
))
6346 else if ((*(this->detector
)) (coding
, &detect_info
)
6347 && detect_info
.found
& (1 << category
))
6349 if (category
== coding_category_utf_16_auto
)
6351 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6352 category
= coding_category_utf_16_le
;
6354 category
= coding_category_utf_16_be
;
6361 if (i
< coding_category_raw_text
)
6362 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6363 else if (null_byte_found
)
6364 setup_coding_system (Qno_conversion
, coding
);
6365 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6366 == CATEGORY_MASK_ANY
)
6367 setup_coding_system (Qraw_text
, coding
);
6368 else if (detect_info
.rejected
)
6369 for (i
= 0; i
< coding_category_raw_text
; i
++)
6370 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6372 this = coding_categories
+ coding_priorities
[i
];
6373 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6378 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6379 == coding_category_utf_8_auto
)
6381 Lisp_Object coding_systems
;
6382 struct coding_detection_info detect_info
;
6385 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6386 detect_info
.found
= detect_info
.rejected
= 0;
6387 coding
->head_ascii
= 0;
6388 if (CONSP (coding_systems
)
6389 && detect_coding_utf_8 (coding
, &detect_info
))
6391 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6392 setup_coding_system (XCAR (coding_systems
), coding
);
6394 setup_coding_system (XCDR (coding_systems
), coding
);
6397 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6398 == coding_category_utf_16_auto
)
6400 Lisp_Object coding_systems
;
6401 struct coding_detection_info detect_info
;
6404 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6405 detect_info
.found
= detect_info
.rejected
= 0;
6406 coding
->head_ascii
= 0;
6407 if (CONSP (coding_systems
)
6408 && detect_coding_utf_16 (coding
, &detect_info
))
6410 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6411 setup_coding_system (XCAR (coding_systems
), coding
);
6412 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6413 setup_coding_system (XCDR (coding_systems
), coding
);
6416 coding
->mode
= saved_mode
;
6421 decode_eol (struct coding_system
*coding
)
6423 Lisp_Object eol_type
;
6424 unsigned char *p
, *pbeg
, *pend
;
6426 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6427 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6430 if (NILP (coding
->dst_object
))
6431 pbeg
= coding
->destination
;
6433 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6434 pend
= pbeg
+ coding
->produced
;
6436 if (VECTORP (eol_type
))
6438 int eol_seen
= EOL_SEEN_NONE
;
6440 for (p
= pbeg
; p
< pend
; p
++)
6443 eol_seen
|= EOL_SEEN_LF
;
6444 else if (*p
== '\r')
6446 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6448 eol_seen
|= EOL_SEEN_CRLF
;
6452 eol_seen
|= EOL_SEEN_CR
;
6455 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6456 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6457 && (eol_seen
& EOL_SEEN_CR
) != 0
6458 && (eol_seen
& EOL_SEEN_LF
) == 0)
6459 eol_seen
= EOL_SEEN_CRLF
;
6460 else if (eol_seen
!= EOL_SEEN_NONE
6461 && eol_seen
!= EOL_SEEN_LF
6462 && eol_seen
!= EOL_SEEN_CRLF
6463 && eol_seen
!= EOL_SEEN_CR
)
6464 eol_seen
= EOL_SEEN_LF
;
6465 if (eol_seen
!= EOL_SEEN_NONE
)
6466 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6469 if (EQ (eol_type
, Qmac
))
6471 for (p
= pbeg
; p
< pend
; p
++)
6475 else if (EQ (eol_type
, Qdos
))
6479 if (NILP (coding
->dst_object
))
6481 /* Start deleting '\r' from the tail to minimize the memory
6483 for (p
= pend
- 2; p
>= pbeg
; p
--)
6486 memmove (p
, p
+ 1, pend
-- - p
- 1);
6492 ptrdiff_t pos_byte
= coding
->dst_pos_byte
;
6493 ptrdiff_t pos
= coding
->dst_pos
;
6494 ptrdiff_t pos_end
= pos
+ coding
->produced_char
- 1;
6496 while (pos
< pos_end
)
6498 p
= BYTE_POS_ADDR (pos_byte
);
6499 if (*p
== '\r' && p
[1] == '\n')
6501 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6506 if (coding
->dst_multibyte
)
6507 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6512 coding
->produced
-= n
;
6513 coding
->produced_char
-= n
;
6518 /* Return a translation table (or list of them) from coding system
6519 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6523 get_translation_table (Lisp_Object attrs
, bool encodep
, int *max_lookup
)
6525 Lisp_Object standard
, translation_table
;
6528 if (NILP (Venable_character_translation
))
6535 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6536 standard
= Vstandard_translation_table_for_encode
;
6538 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6539 standard
= Vstandard_translation_table_for_decode
;
6540 if (NILP (translation_table
))
6541 translation_table
= standard
;
6544 if (SYMBOLP (translation_table
))
6545 translation_table
= Fget (translation_table
, Qtranslation_table
);
6546 else if (CONSP (translation_table
))
6548 translation_table
= Fcopy_sequence (translation_table
);
6549 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6550 if (SYMBOLP (XCAR (val
)))
6551 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6553 if (CHAR_TABLE_P (standard
))
6555 if (CONSP (translation_table
))
6556 translation_table
= nconc2 (translation_table
,
6557 Fcons (standard
, Qnil
));
6559 translation_table
= Fcons (translation_table
,
6560 Fcons (standard
, Qnil
));
6567 if (CHAR_TABLE_P (translation_table
)
6568 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6570 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6571 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6572 *max_lookup
= XFASTINT (val
);
6574 else if (CONSP (translation_table
))
6578 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6579 if (CHAR_TABLE_P (XCAR (tail
))
6580 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6582 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6583 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6584 *max_lookup
= XFASTINT (tailval
);
6588 return translation_table
;
6591 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6594 if (CHAR_TABLE_P (table)) \
6596 trans = CHAR_TABLE_REF (table, c); \
6597 if (CHARACTERP (trans)) \
6598 c = XFASTINT (trans), trans = Qnil; \
6600 else if (CONSP (table)) \
6604 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6605 if (CHAR_TABLE_P (XCAR (tail))) \
6607 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6608 if (CHARACTERP (trans)) \
6609 c = XFASTINT (trans), trans = Qnil; \
6610 else if (! NILP (trans)) \
6617 /* Return a translation of character(s) at BUF according to TRANS.
6618 TRANS is TO-CHAR or ((FROM . TO) ...) where
6619 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6620 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6621 translation is found, and Qnil if not found..
6622 If BUF is too short to lookup characters in FROM, return Qt. */
6625 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6628 if (INTEGERP (trans
))
6630 for (; CONSP (trans
); trans
= XCDR (trans
))
6632 Lisp_Object val
= XCAR (trans
);
6633 Lisp_Object from
= XCAR (val
);
6634 ptrdiff_t len
= ASIZE (from
);
6637 for (i
= 0; i
< len
; i
++)
6639 if (buf
+ i
== buf_end
)
6641 if (XINT (AREF (from
, i
)) != buf
[i
])
6652 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6655 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6656 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6658 ptrdiff_t produced_chars
= 0;
6661 if (! coding
->chars_at_source
)
6663 /* Source characters are in coding->charbuf. */
6664 int *buf
= coding
->charbuf
;
6665 int *buf_end
= buf
+ coding
->charbuf_used
;
6667 if (EQ (coding
->src_object
, coding
->dst_object
))
6669 coding_set_source (coding
);
6670 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6673 while (buf
< buf_end
)
6680 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
6681 Lisp_Object trans
= Qnil
;
6683 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6686 trans
= get_translation (trans
, buf
, buf_end
);
6687 if (INTEGERP (trans
))
6689 else if (CONSP (trans
))
6691 from_nchars
= ASIZE (XCAR (trans
));
6692 trans
= XCDR (trans
);
6693 if (INTEGERP (trans
))
6697 to_nchars
= ASIZE (trans
);
6698 c
= XINT (AREF (trans
, 0));
6701 else if (EQ (trans
, Qt
) && ! last_block
)
6705 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
6707 if (((min (PTRDIFF_MAX
, SIZE_MAX
) - (buf_end
- buf
))
6708 / MAX_MULTIBYTE_LENGTH
)
6710 memory_full (SIZE_MAX
);
6711 dst
= alloc_destination (coding
,
6713 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6715 if (EQ (coding
->src_object
, coding
->dst_object
))
6717 coding_set_source (coding
);
6718 dst_end
= (((unsigned char *) coding
->source
)
6719 + coding
->consumed
);
6722 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6725 for (i
= 0; i
< to_nchars
; i
++)
6728 c
= XINT (AREF (trans
, i
));
6729 if (coding
->dst_multibyte
6730 || ! CHAR_BYTE8_P (c
))
6731 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6733 *dst
++ = CHAR_TO_BYTE8 (c
);
6735 produced_chars
+= to_nchars
;
6739 /* This is an annotation datum. (-C) is the length. */
6742 carryover
= buf_end
- buf
;
6746 /* Source characters are at coding->source. */
6747 const unsigned char *src
= coding
->source
;
6748 const unsigned char *src_end
= src
+ coding
->consumed
;
6750 if (EQ (coding
->dst_object
, coding
->src_object
))
6751 dst_end
= (unsigned char *) src
;
6752 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6754 if (coding
->src_multibyte
)
6756 bool multibytep
= 1;
6757 ptrdiff_t consumed_chars
= 0;
6761 const unsigned char *src_base
= src
;
6767 if (EQ (coding
->src_object
, coding
->dst_object
))
6768 dst_end
= (unsigned char *) src
;
6771 ptrdiff_t offset
= src
- coding
->source
;
6773 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6775 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6776 coding_set_source (coding
);
6777 src
= coding
->source
+ offset
;
6778 src_end
= coding
->source
+ coding
->consumed
;
6779 if (EQ (coding
->src_object
, coding
->dst_object
))
6780 dst_end
= (unsigned char *) src
;
6790 while (src
< src_end
)
6792 bool multibytep
= 1;
6795 if (dst
>= dst_end
- 1)
6797 if (EQ (coding
->src_object
, coding
->dst_object
))
6798 dst_end
= (unsigned char *) src
;
6799 if (dst
>= dst_end
- 1)
6801 ptrdiff_t offset
= src
- coding
->source
;
6802 ptrdiff_t more_bytes
;
6804 if (EQ (coding
->src_object
, coding
->dst_object
))
6805 more_bytes
= ((src_end
- src
) / 2) + 2;
6807 more_bytes
= src_end
- src
+ 2;
6808 dst
= alloc_destination (coding
, more_bytes
, dst
);
6809 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6810 coding_set_source (coding
);
6811 src
= coding
->source
+ offset
;
6812 src_end
= coding
->source
+ coding
->consumed
;
6813 if (EQ (coding
->src_object
, coding
->dst_object
))
6814 dst_end
= (unsigned char *) src
;
6822 if (!EQ (coding
->src_object
, coding
->dst_object
))
6824 ptrdiff_t require
= coding
->src_bytes
- coding
->dst_bytes
;
6828 ptrdiff_t offset
= src
- coding
->source
;
6830 dst
= alloc_destination (coding
, require
, dst
);
6831 coding_set_source (coding
);
6832 src
= coding
->source
+ offset
;
6833 src_end
= coding
->source
+ coding
->consumed
;
6836 produced_chars
= coding
->consumed_char
;
6837 while (src
< src_end
)
6842 produced
= dst
- (coding
->destination
+ coding
->produced
);
6843 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6844 insert_from_gap (produced_chars
, produced
);
6845 coding
->produced
+= produced
;
6846 coding
->produced_char
+= produced_chars
;
6850 /* Compose text in CODING->object according to the annotation data at
6851 CHARBUF. CHARBUF is an array:
6852 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6856 produce_composition (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6860 enum composition_method method
;
6861 Lisp_Object components
;
6863 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6864 to
= pos
+ charbuf
[2];
6865 method
= (enum composition_method
) (charbuf
[4]);
6867 if (method
== COMPOSITION_RELATIVE
)
6871 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6874 if (method
== COMPOSITION_WITH_RULE
)
6875 len
= charbuf
[2] * 3 - 2;
6876 charbuf
+= MAX_ANNOTATION_LENGTH
;
6877 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6878 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6880 if (charbuf
[i
] >= 0)
6881 args
[j
] = make_number (charbuf
[i
]);
6885 args
[j
] = make_number (charbuf
[i
] % 0x100);
6888 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6890 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6894 /* Put `charset' property on text in CODING->object according to
6895 the annotation data at CHARBUF. CHARBUF is an array:
6896 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6900 produce_charset (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6902 ptrdiff_t from
= pos
- charbuf
[2];
6903 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6905 Fput_text_property (make_number (from
), make_number (pos
),
6906 Qcharset
, CHARSET_NAME (charset
),
6907 coding
->dst_object
);
6911 #define CHARBUF_SIZE 0x4000
6913 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6915 int size = CHARBUF_SIZE; \
6917 coding->charbuf = NULL; \
6918 while (size > 1024) \
6920 coding->charbuf = alloca (sizeof (int) * size); \
6921 if (coding->charbuf) \
6925 if (! coding->charbuf) \
6927 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6930 coding->charbuf_size = size; \
6935 produce_annotation (struct coding_system
*coding
, ptrdiff_t pos
)
6937 int *charbuf
= coding
->charbuf
;
6938 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6940 if (NILP (coding
->dst_object
))
6943 while (charbuf
< charbuf_end
)
6949 int len
= -*charbuf
;
6954 case CODING_ANNOTATE_COMPOSITION_MASK
:
6955 produce_composition (coding
, charbuf
, pos
);
6957 case CODING_ANNOTATE_CHARSET_MASK
:
6958 produce_charset (coding
, charbuf
, pos
);
6966 /* Decode the data at CODING->src_object into CODING->dst_object.
6967 CODING->src_object is a buffer, a string, or nil.
6968 CODING->dst_object is a buffer.
6970 If CODING->src_object is a buffer, it must be the current buffer.
6971 In this case, if CODING->src_pos is positive, it is a position of
6972 the source text in the buffer, otherwise, the source text is in the
6973 gap area of the buffer, and CODING->src_pos specifies the offset of
6974 the text from GPT (which must be the same as PT). If this is the
6975 same buffer as CODING->dst_object, CODING->src_pos must be
6978 If CODING->src_object is a string, CODING->src_pos is an index to
6981 If CODING->src_object is nil, CODING->source must already point to
6982 the non-relocatable memory area. In this case, CODING->src_pos is
6983 an offset from CODING->source.
6985 The decoded data is inserted at the current point of the buffer
6990 decode_coding (struct coding_system
*coding
)
6993 Lisp_Object undo_list
;
6994 Lisp_Object translation_table
;
6995 struct ccl_spec cclspec
;
6999 if (BUFFERP (coding
->src_object
)
7000 && coding
->src_pos
> 0
7001 && coding
->src_pos
< GPT
7002 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7003 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7006 if (BUFFERP (coding
->dst_object
))
7008 set_buffer_internal (XBUFFER (coding
->dst_object
));
7010 move_gap_both (PT
, PT_BYTE
);
7012 /* We must disable undo_list in order to record the whole insert
7013 transaction via record_insert at the end. But doing so also
7014 disables the recording of the first change to the undo_list.
7015 Therefore we check for first change here and record it via
7016 record_first_change if needed. */
7017 if (MODIFF
<= SAVE_MODIFF
)
7018 record_first_change ();
7020 undo_list
= BVAR (current_buffer
, undo_list
);
7021 bset_undo_list (current_buffer
, Qt
);
7024 coding
->consumed
= coding
->consumed_char
= 0;
7025 coding
->produced
= coding
->produced_char
= 0;
7026 coding
->chars_at_source
= 0;
7027 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7030 ALLOC_CONVERSION_WORK_AREA (coding
);
7032 attrs
= CODING_ID_ATTRS (coding
->id
);
7033 translation_table
= get_translation_table (attrs
, 0, NULL
);
7036 if (coding
->decoder
== decode_coding_ccl
)
7038 coding
->spec
.ccl
= &cclspec
;
7039 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7043 ptrdiff_t pos
= coding
->dst_pos
+ coding
->produced_char
;
7045 coding_set_source (coding
);
7046 coding
->annotated
= 0;
7047 coding
->charbuf_used
= carryover
;
7048 (*(coding
->decoder
)) (coding
);
7049 coding_set_destination (coding
);
7050 carryover
= produce_chars (coding
, translation_table
, 0);
7051 if (coding
->annotated
)
7052 produce_annotation (coding
, pos
);
7053 for (i
= 0; i
< carryover
; i
++)
7055 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7057 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7058 || (coding
->consumed
< coding
->src_bytes
7059 && (coding
->result
== CODING_RESULT_SUCCESS
7060 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7064 coding_set_destination (coding
);
7065 coding
->charbuf_used
= carryover
;
7066 produce_chars (coding
, translation_table
, 1);
7069 coding
->carryover_bytes
= 0;
7070 if (coding
->consumed
< coding
->src_bytes
)
7072 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7073 const unsigned char *src
;
7075 coding_set_source (coding
);
7076 coding_set_destination (coding
);
7077 src
= coding
->source
+ coding
->consumed
;
7079 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7081 /* Flush out unprocessed data as binary chars. We are sure
7082 that the number of data is less than the size of
7084 coding
->charbuf_used
= 0;
7085 coding
->chars_at_source
= 0;
7087 while (nbytes
-- > 0)
7092 c
= BYTE8_TO_CHAR (c
);
7093 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7095 produce_chars (coding
, Qnil
, 1);
7099 /* Record unprocessed bytes in coding->carryover. We are
7100 sure that the number of data is less than the size of
7101 coding->carryover. */
7102 unsigned char *p
= coding
->carryover
;
7104 if (nbytes
> sizeof coding
->carryover
)
7105 nbytes
= sizeof coding
->carryover
;
7106 coding
->carryover_bytes
= nbytes
;
7107 while (nbytes
-- > 0)
7110 coding
->consumed
= coding
->src_bytes
;
7113 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7114 && !inhibit_eol_conversion
)
7115 decode_eol (coding
);
7116 if (BUFFERP (coding
->dst_object
))
7118 bset_undo_list (current_buffer
, undo_list
);
7119 record_insert (coding
->dst_pos
, coding
->produced_char
);
7124 /* Extract an annotation datum from a composition starting at POS and
7125 ending before LIMIT of CODING->src_object (buffer or string), store
7126 the data in BUF, set *STOP to a starting position of the next
7127 composition (if any) or to LIMIT, and return the address of the
7128 next element of BUF.
7130 If such an annotation is not found, set *STOP to a starting
7131 position of a composition after POS (if any) or to LIMIT, and
7135 handle_composition_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7136 struct coding_system
*coding
, int *buf
,
7139 ptrdiff_t start
, end
;
7142 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7145 else if (start
> pos
)
7151 /* We found a composition. Store the corresponding
7152 annotation data in BUF. */
7154 enum composition_method method
= COMPOSITION_METHOD (prop
);
7155 int nchars
= COMPOSITION_LENGTH (prop
);
7157 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7158 if (method
!= COMPOSITION_RELATIVE
)
7160 Lisp_Object components
;
7161 ptrdiff_t i
, len
, i_byte
;
7163 components
= COMPOSITION_COMPONENTS (prop
);
7164 if (VECTORP (components
))
7166 len
= ASIZE (components
);
7167 for (i
= 0; i
< len
; i
++)
7168 *buf
++ = XINT (AREF (components
, i
));
7170 else if (STRINGP (components
))
7172 len
= SCHARS (components
);
7176 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7180 else if (INTEGERP (components
))
7183 *buf
++ = XINT (components
);
7185 else if (CONSP (components
))
7187 for (len
= 0; CONSP (components
);
7188 len
++, components
= XCDR (components
))
7189 *buf
++ = XINT (XCAR (components
));
7197 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7208 /* Extract an annotation datum from a text property `charset' at POS of
7209 CODING->src_object (buffer of string), store the data in BUF, set
7210 *STOP to the position where the value of `charset' property changes
7211 (limiting by LIMIT), and return the address of the next element of
7214 If the property value is nil, set *STOP to the position where the
7215 property value is non-nil (limiting by LIMIT), and return BUF. */
7218 handle_charset_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7219 struct coding_system
*coding
, int *buf
,
7222 Lisp_Object val
, next
;
7225 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7226 if (! NILP (val
) && CHARSETP (val
))
7227 id
= XINT (CHARSET_SYMBOL_ID (val
));
7230 ADD_CHARSET_DATA (buf
, 0, id
);
7231 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7233 make_number (limit
));
7234 *stop
= XINT (next
);
7240 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7243 int *buf
= coding
->charbuf
;
7244 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7245 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7246 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7247 ptrdiff_t pos
= coding
->src_pos
+ coding
->consumed_char
;
7248 ptrdiff_t end_pos
= coding
->src_pos
+ coding
->src_chars
;
7249 bool multibytep
= coding
->src_multibyte
;
7250 Lisp_Object eol_type
;
7252 ptrdiff_t stop
, stop_composition
, stop_charset
;
7253 int *lookup_buf
= NULL
;
7255 if (! NILP (translation_table
))
7256 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7258 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7259 if (VECTORP (eol_type
))
7262 /* Note: composition handling is not yet implemented. */
7263 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7265 if (NILP (coding
->src_object
))
7266 stop
= stop_composition
= stop_charset
= end_pos
;
7269 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7270 stop
= stop_composition
= pos
;
7272 stop
= stop_composition
= end_pos
;
7273 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7274 stop
= stop_charset
= pos
;
7276 stop_charset
= end_pos
;
7279 /* Compensate for CRLF and conversion. */
7280 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7281 while (buf
< buf_end
)
7289 if (pos
== stop_composition
)
7290 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7291 buf
, &stop_composition
);
7292 if (pos
== stop_charset
)
7293 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7294 buf
, &stop_charset
);
7295 stop
= (stop_composition
< stop_charset
7296 ? stop_composition
: stop_charset
);
7303 if (coding
->encoder
== encode_coding_raw_text
7304 || coding
->encoder
== encode_coding_ccl
)
7306 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7307 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7309 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7312 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7313 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7315 if (! EQ (eol_type
, Qunix
))
7319 if (EQ (eol_type
, Qdos
))
7327 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7332 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7333 int *lookup_buf_end
;
7334 const unsigned char *p
= src
;
7338 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7339 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7340 lookup_buf_end
= lookup_buf
+ i
;
7341 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7342 if (INTEGERP (trans
))
7344 else if (CONSP (trans
))
7346 from_nchars
= ASIZE (XCAR (trans
));
7347 trans
= XCDR (trans
);
7348 if (INTEGERP (trans
))
7352 to_nchars
= ASIZE (trans
);
7353 if (buf_end
- buf
< to_nchars
)
7355 c
= XINT (AREF (trans
, 0));
7361 for (i
= 1; i
< to_nchars
; i
++)
7362 *buf
++ = XINT (AREF (trans
, i
));
7363 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7364 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7368 coding
->consumed
= src
- coding
->source
;
7369 coding
->consumed_char
= pos
- coding
->src_pos
;
7370 coding
->charbuf_used
= buf
- coding
->charbuf
;
7371 coding
->chars_at_source
= 0;
7375 /* Encode the text at CODING->src_object into CODING->dst_object.
7376 CODING->src_object is a buffer or a string.
7377 CODING->dst_object is a buffer or nil.
7379 If CODING->src_object is a buffer, it must be the current buffer.
7380 In this case, if CODING->src_pos is positive, it is a position of
7381 the source text in the buffer, otherwise. the source text is in the
7382 gap area of the buffer, and coding->src_pos specifies the offset of
7383 the text from GPT (which must be the same as PT). If this is the
7384 same buffer as CODING->dst_object, CODING->src_pos must be
7385 negative and CODING should not have `pre-write-conversion'.
7387 If CODING->src_object is a string, CODING should not have
7388 `pre-write-conversion'.
7390 If CODING->dst_object is a buffer, the encoded data is inserted at
7391 the current point of that buffer.
7393 If CODING->dst_object is nil, the encoded data is placed at the
7394 memory area specified by CODING->destination. */
7397 encode_coding (struct coding_system
*coding
)
7400 Lisp_Object translation_table
;
7402 struct ccl_spec cclspec
;
7404 attrs
= CODING_ID_ATTRS (coding
->id
);
7405 if (coding
->encoder
== encode_coding_raw_text
)
7406 translation_table
= Qnil
, max_lookup
= 0;
7408 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7410 if (BUFFERP (coding
->dst_object
))
7412 set_buffer_internal (XBUFFER (coding
->dst_object
));
7413 coding
->dst_multibyte
7414 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7417 coding
->consumed
= coding
->consumed_char
= 0;
7418 coding
->produced
= coding
->produced_char
= 0;
7419 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7422 ALLOC_CONVERSION_WORK_AREA (coding
);
7424 if (coding
->encoder
== encode_coding_ccl
)
7426 coding
->spec
.ccl
= &cclspec
;
7427 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7430 coding_set_source (coding
);
7431 consume_chars (coding
, translation_table
, max_lookup
);
7432 coding_set_destination (coding
);
7433 (*(coding
->encoder
)) (coding
);
7434 } while (coding
->consumed_char
< coding
->src_chars
);
7436 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7437 insert_from_gap (coding
->produced_char
, coding
->produced
);
7441 /* Name (or base name) of work buffer for code conversion. */
7442 static Lisp_Object Vcode_conversion_workbuf_name
;
7444 /* A working buffer used by the top level conversion. Once it is
7445 created, it is never destroyed. It has the name
7446 Vcode_conversion_workbuf_name. The other working buffers are
7447 destroyed after the use is finished, and their names are modified
7448 versions of Vcode_conversion_workbuf_name. */
7449 static Lisp_Object Vcode_conversion_reused_workbuf
;
7451 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7452 static bool reused_workbuf_in_use
;
7455 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7456 multibyteness of returning buffer. */
7459 make_conversion_work_buffer (bool multibyte
)
7461 Lisp_Object name
, workbuf
;
7462 struct buffer
*current
;
7464 if (reused_workbuf_in_use
)
7466 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7467 workbuf
= Fget_buffer_create (name
);
7471 reused_workbuf_in_use
= 1;
7472 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7473 Vcode_conversion_reused_workbuf
7474 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7475 workbuf
= Vcode_conversion_reused_workbuf
;
7477 current
= current_buffer
;
7478 set_buffer_internal (XBUFFER (workbuf
));
7479 /* We can't allow modification hooks to run in the work buffer. For
7480 instance, directory_files_internal assumes that file decoding
7481 doesn't compile new regexps. */
7482 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7484 bset_undo_list (current_buffer
, Qt
);
7485 bset_enable_multibyte_characters (current_buffer
, multibyte
? Qt
: Qnil
);
7486 set_buffer_internal (current
);
7492 code_conversion_restore (Lisp_Object arg
)
7494 Lisp_Object current
, workbuf
;
7495 struct gcpro gcpro1
;
7498 current
= XCAR (arg
);
7499 workbuf
= XCDR (arg
);
7500 if (! NILP (workbuf
))
7502 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7503 reused_workbuf_in_use
= 0;
7505 Fkill_buffer (workbuf
);
7507 set_buffer_internal (XBUFFER (current
));
7513 code_conversion_save (bool with_work_buf
, bool multibyte
)
7515 Lisp_Object workbuf
= Qnil
;
7518 workbuf
= make_conversion_work_buffer (multibyte
);
7519 record_unwind_protect (code_conversion_restore
,
7520 Fcons (Fcurrent_buffer (), workbuf
));
7525 decode_coding_gap (struct coding_system
*coding
,
7526 ptrdiff_t chars
, ptrdiff_t bytes
)
7528 ptrdiff_t count
= SPECPDL_INDEX ();
7531 code_conversion_save (0, 0);
7533 coding
->src_object
= Fcurrent_buffer ();
7534 coding
->src_chars
= chars
;
7535 coding
->src_bytes
= bytes
;
7536 coding
->src_pos
= -chars
;
7537 coding
->src_pos_byte
= -bytes
;
7538 coding
->src_multibyte
= chars
< bytes
;
7539 coding
->dst_object
= coding
->src_object
;
7540 coding
->dst_pos
= PT
;
7541 coding
->dst_pos_byte
= PT_BYTE
;
7542 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7544 if (CODING_REQUIRE_DETECTION (coding
))
7545 detect_coding (coding
);
7547 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7548 current_buffer
->text
->inhibit_shrinking
= 1;
7549 decode_coding (coding
);
7550 current_buffer
->text
->inhibit_shrinking
= 0;
7552 attrs
= CODING_ID_ATTRS (coding
->id
);
7553 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7555 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7558 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7559 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7560 make_number (coding
->produced_char
));
7562 coding
->produced_char
+= Z
- prev_Z
;
7563 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7566 unbind_to (count
, Qnil
);
7570 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7571 SRC_OBJECT into DST_OBJECT by coding context CODING.
7573 SRC_OBJECT is a buffer, a string, or Qnil.
7575 If it is a buffer, the text is at point of the buffer. FROM and TO
7576 are positions in the buffer.
7578 If it is a string, the text is at the beginning of the string.
7579 FROM and TO are indices to the string.
7581 If it is nil, the text is at coding->source. FROM and TO are
7582 indices to coding->source.
7584 DST_OBJECT is a buffer, Qt, or Qnil.
7586 If it is a buffer, the decoded text is inserted at point of the
7587 buffer. If the buffer is the same as SRC_OBJECT, the source text
7590 If it is Qt, a string is made from the decoded text, and
7591 set in CODING->dst_object.
7593 If it is Qnil, the decoded text is stored at CODING->destination.
7594 The caller must allocate CODING->dst_bytes bytes at
7595 CODING->destination by xmalloc. If the decoded text is longer than
7596 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7600 decode_coding_object (struct coding_system
*coding
,
7601 Lisp_Object src_object
,
7602 ptrdiff_t from
, ptrdiff_t from_byte
,
7603 ptrdiff_t to
, ptrdiff_t to_byte
,
7604 Lisp_Object dst_object
)
7606 ptrdiff_t count
= SPECPDL_INDEX ();
7607 unsigned char *destination
IF_LINT (= NULL
);
7608 ptrdiff_t dst_bytes
IF_LINT (= 0);
7609 ptrdiff_t chars
= to
- from
;
7610 ptrdiff_t bytes
= to_byte
- from_byte
;
7612 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7613 bool need_marker_adjustment
= 0;
7614 Lisp_Object old_deactivate_mark
;
7616 old_deactivate_mark
= Vdeactivate_mark
;
7618 if (NILP (dst_object
))
7620 destination
= coding
->destination
;
7621 dst_bytes
= coding
->dst_bytes
;
7624 coding
->src_object
= src_object
;
7625 coding
->src_chars
= chars
;
7626 coding
->src_bytes
= bytes
;
7627 coding
->src_multibyte
= chars
< bytes
;
7629 if (STRINGP (src_object
))
7631 coding
->src_pos
= from
;
7632 coding
->src_pos_byte
= from_byte
;
7634 else if (BUFFERP (src_object
))
7636 set_buffer_internal (XBUFFER (src_object
));
7638 move_gap_both (from
, from_byte
);
7639 if (EQ (src_object
, dst_object
))
7641 struct Lisp_Marker
*tail
;
7643 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7645 tail
->need_adjustment
7646 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7647 need_marker_adjustment
|= tail
->need_adjustment
;
7649 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7650 TEMP_SET_PT_BOTH (from
, from_byte
);
7651 current_buffer
->text
->inhibit_shrinking
= 1;
7652 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7653 coding
->src_pos
= -chars
;
7654 coding
->src_pos_byte
= -bytes
;
7658 coding
->src_pos
= from
;
7659 coding
->src_pos_byte
= from_byte
;
7663 if (CODING_REQUIRE_DETECTION (coding
))
7664 detect_coding (coding
);
7665 attrs
= CODING_ID_ATTRS (coding
->id
);
7667 if (EQ (dst_object
, Qt
)
7668 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7669 && NILP (dst_object
)))
7671 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7672 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7673 coding
->dst_pos
= BEG
;
7674 coding
->dst_pos_byte
= BEG_BYTE
;
7676 else if (BUFFERP (dst_object
))
7678 code_conversion_save (0, 0);
7679 coding
->dst_object
= dst_object
;
7680 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7681 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7682 coding
->dst_multibyte
7683 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7687 code_conversion_save (0, 0);
7688 coding
->dst_object
= Qnil
;
7689 /* Most callers presume this will return a multibyte result, and they
7690 won't use `binary' or `raw-text' anyway, so let's not worry about
7691 CODING_FOR_UNIBYTE. */
7692 coding
->dst_multibyte
= 1;
7695 decode_coding (coding
);
7697 if (BUFFERP (coding
->dst_object
))
7698 set_buffer_internal (XBUFFER (coding
->dst_object
));
7700 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7702 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7703 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7706 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7707 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7708 old_deactivate_mark
);
7709 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7710 make_number (coding
->produced_char
));
7713 coding
->produced_char
+= Z
- prev_Z
;
7714 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7717 if (EQ (dst_object
, Qt
))
7719 coding
->dst_object
= Fbuffer_string ();
7721 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7723 set_buffer_internal (XBUFFER (coding
->dst_object
));
7724 if (dst_bytes
< coding
->produced
)
7726 destination
= xrealloc (destination
, coding
->produced
);
7729 record_conversion_result (coding
,
7730 CODING_RESULT_INSUFFICIENT_MEM
);
7731 unbind_to (count
, Qnil
);
7734 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7735 move_gap_both (BEGV
, BEGV_BYTE
);
7736 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7737 coding
->destination
= destination
;
7743 /* This is the case of:
7744 (BUFFERP (src_object) && EQ (src_object, dst_object))
7745 As we have moved PT while replacing the original buffer
7746 contents, we must recover it now. */
7747 set_buffer_internal (XBUFFER (src_object
));
7748 current_buffer
->text
->inhibit_shrinking
= 0;
7749 if (saved_pt
< from
)
7750 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7751 else if (saved_pt
< from
+ chars
)
7752 TEMP_SET_PT_BOTH (from
, from_byte
);
7753 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7754 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7755 saved_pt_byte
+ (coding
->produced
- bytes
));
7757 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7758 saved_pt_byte
+ (coding
->produced
- bytes
));
7760 if (need_marker_adjustment
)
7762 struct Lisp_Marker
*tail
;
7764 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7765 if (tail
->need_adjustment
)
7767 tail
->need_adjustment
= 0;
7768 if (tail
->insertion_type
)
7770 tail
->bytepos
= from_byte
;
7771 tail
->charpos
= from
;
7775 tail
->bytepos
= from_byte
+ coding
->produced
;
7777 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7778 ? tail
->bytepos
: from
+ coding
->produced_char
);
7784 Vdeactivate_mark
= old_deactivate_mark
;
7785 unbind_to (count
, coding
->dst_object
);
7790 encode_coding_object (struct coding_system
*coding
,
7791 Lisp_Object src_object
,
7792 ptrdiff_t from
, ptrdiff_t from_byte
,
7793 ptrdiff_t to
, ptrdiff_t to_byte
,
7794 Lisp_Object dst_object
)
7796 ptrdiff_t count
= SPECPDL_INDEX ();
7797 ptrdiff_t chars
= to
- from
;
7798 ptrdiff_t bytes
= to_byte
- from_byte
;
7800 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7801 bool need_marker_adjustment
= 0;
7802 bool kill_src_buffer
= 0;
7803 Lisp_Object old_deactivate_mark
;
7805 old_deactivate_mark
= Vdeactivate_mark
;
7807 coding
->src_object
= src_object
;
7808 coding
->src_chars
= chars
;
7809 coding
->src_bytes
= bytes
;
7810 coding
->src_multibyte
= chars
< bytes
;
7812 attrs
= CODING_ID_ATTRS (coding
->id
);
7814 if (EQ (src_object
, dst_object
))
7816 struct Lisp_Marker
*tail
;
7818 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7820 tail
->need_adjustment
7821 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7822 need_marker_adjustment
|= tail
->need_adjustment
;
7826 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7828 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7829 set_buffer_internal (XBUFFER (coding
->src_object
));
7830 if (STRINGP (src_object
))
7831 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7832 else if (BUFFERP (src_object
))
7833 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7835 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7837 if (EQ (src_object
, dst_object
))
7839 set_buffer_internal (XBUFFER (src_object
));
7840 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7841 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7842 set_buffer_internal (XBUFFER (coding
->src_object
));
7846 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7848 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7849 old_deactivate_mark
);
7850 safe_call2 (CODING_ATTR_PRE_WRITE (attrs
),
7851 make_number (BEG
), make_number (Z
));
7854 if (XBUFFER (coding
->src_object
) != current_buffer
)
7855 kill_src_buffer
= 1;
7856 coding
->src_object
= Fcurrent_buffer ();
7858 move_gap_both (BEG
, BEG_BYTE
);
7859 coding
->src_chars
= Z
- BEG
;
7860 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7861 coding
->src_pos
= BEG
;
7862 coding
->src_pos_byte
= BEG_BYTE
;
7863 coding
->src_multibyte
= Z
< Z_BYTE
;
7865 else if (STRINGP (src_object
))
7867 code_conversion_save (0, 0);
7868 coding
->src_pos
= from
;
7869 coding
->src_pos_byte
= from_byte
;
7871 else if (BUFFERP (src_object
))
7873 code_conversion_save (0, 0);
7874 set_buffer_internal (XBUFFER (src_object
));
7875 if (EQ (src_object
, dst_object
))
7877 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7878 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7879 coding
->src_pos
= 0;
7880 coding
->src_pos_byte
= 0;
7884 if (from
< GPT
&& to
>= GPT
)
7885 move_gap_both (from
, from_byte
);
7886 coding
->src_pos
= from
;
7887 coding
->src_pos_byte
= from_byte
;
7891 code_conversion_save (0, 0);
7893 if (BUFFERP (dst_object
))
7895 coding
->dst_object
= dst_object
;
7896 if (EQ (src_object
, dst_object
))
7898 coding
->dst_pos
= from
;
7899 coding
->dst_pos_byte
= from_byte
;
7903 struct buffer
*current
= current_buffer
;
7905 set_buffer_temp (XBUFFER (dst_object
));
7906 coding
->dst_pos
= PT
;
7907 coding
->dst_pos_byte
= PT_BYTE
;
7908 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7909 set_buffer_temp (current
);
7911 coding
->dst_multibyte
7912 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7914 else if (EQ (dst_object
, Qt
))
7916 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
7917 coding
->dst_object
= Qnil
;
7918 coding
->destination
= xmalloc (dst_bytes
);
7919 coding
->dst_bytes
= dst_bytes
;
7920 coding
->dst_multibyte
= 0;
7924 coding
->dst_object
= Qnil
;
7925 coding
->dst_multibyte
= 0;
7928 encode_coding (coding
);
7930 if (EQ (dst_object
, Qt
))
7932 if (BUFFERP (coding
->dst_object
))
7933 coding
->dst_object
= Fbuffer_string ();
7937 = make_unibyte_string ((char *) coding
->destination
,
7939 xfree (coding
->destination
);
7945 /* This is the case of:
7946 (BUFFERP (src_object) && EQ (src_object, dst_object))
7947 As we have moved PT while replacing the original buffer
7948 contents, we must recover it now. */
7949 set_buffer_internal (XBUFFER (src_object
));
7950 if (saved_pt
< from
)
7951 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7952 else if (saved_pt
< from
+ chars
)
7953 TEMP_SET_PT_BOTH (from
, from_byte
);
7954 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7955 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7956 saved_pt_byte
+ (coding
->produced
- bytes
));
7958 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7959 saved_pt_byte
+ (coding
->produced
- bytes
));
7961 if (need_marker_adjustment
)
7963 struct Lisp_Marker
*tail
;
7965 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7966 if (tail
->need_adjustment
)
7968 tail
->need_adjustment
= 0;
7969 if (tail
->insertion_type
)
7971 tail
->bytepos
= from_byte
;
7972 tail
->charpos
= from
;
7976 tail
->bytepos
= from_byte
+ coding
->produced
;
7978 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7979 ? tail
->bytepos
: from
+ coding
->produced_char
);
7985 if (kill_src_buffer
)
7986 Fkill_buffer (coding
->src_object
);
7988 Vdeactivate_mark
= old_deactivate_mark
;
7989 unbind_to (count
, Qnil
);
7994 preferred_coding_system (void)
7996 int id
= coding_categories
[coding_priorities
[0]].id
;
7998 return CODING_ID_NAME (id
);
8003 /*** 8. Emacs Lisp library functions ***/
8005 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8006 doc
: /* Return t if OBJECT is nil or a coding-system.
8007 See the documentation of `define-coding-system' for information
8008 about coding-system objects. */)
8009 (Lisp_Object object
)
8012 || CODING_SYSTEM_ID (object
) >= 0)
8014 if (! SYMBOLP (object
)
8015 || NILP (Fget (object
, Qcoding_system_define_form
)))
8020 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8021 Sread_non_nil_coding_system
, 1, 1, 0,
8022 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8023 (Lisp_Object prompt
)
8028 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8029 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8031 while (SCHARS (val
) == 0);
8032 return (Fintern (val
, Qnil
));
8035 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8036 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8037 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8038 Ignores case when completing coding systems (all Emacs coding systems
8039 are lower-case). */)
8040 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8043 ptrdiff_t count
= SPECPDL_INDEX ();
8045 if (SYMBOLP (default_coding_system
))
8046 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8047 specbind (Qcompletion_ignore_case
, Qt
);
8048 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8049 Qt
, Qnil
, Qcoding_system_history
,
8050 default_coding_system
, Qnil
);
8051 unbind_to (count
, Qnil
);
8052 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8055 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8057 doc
: /* Check validity of CODING-SYSTEM.
8058 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8059 It is valid if it is nil or a symbol defined as a coding system by the
8060 function `define-coding-system'. */)
8061 (Lisp_Object coding_system
)
8063 Lisp_Object define_form
;
8065 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8066 if (! NILP (define_form
))
8068 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8069 safe_eval (define_form
);
8071 if (!NILP (Fcoding_system_p (coding_system
)))
8072 return coding_system
;
8073 xsignal1 (Qcoding_system_error
, coding_system
);
8077 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8078 HIGHEST, return the coding system of the highest
8079 priority among the detected coding systems. Otherwise return a
8080 list of detected coding systems sorted by their priorities. If
8081 MULTIBYTEP, it is assumed that the bytes are in correct
8082 multibyte form but contains only ASCII and eight-bit chars.
8083 Otherwise, the bytes are raw bytes.
8085 CODING-SYSTEM controls the detection as below:
8087 If it is nil, detect both text-format and eol-format. If the
8088 text-format part of CODING-SYSTEM is already specified
8089 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8090 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8091 detect only text-format. */
8094 detect_coding_system (const unsigned char *src
,
8095 ptrdiff_t src_chars
, ptrdiff_t src_bytes
,
8096 bool highest
, bool multibytep
,
8097 Lisp_Object coding_system
)
8099 const unsigned char *src_end
= src
+ src_bytes
;
8100 Lisp_Object attrs
, eol_type
;
8101 Lisp_Object val
= Qnil
;
8102 struct coding_system coding
;
8104 struct coding_detection_info detect_info
;
8105 enum coding_category base_category
;
8106 bool null_byte_found
= 0, eight_bit_found
= 0;
8108 if (NILP (coding_system
))
8109 coding_system
= Qundecided
;
8110 setup_coding_system (coding_system
, &coding
);
8111 attrs
= CODING_ID_ATTRS (coding
.id
);
8112 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8113 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8115 coding
.source
= src
;
8116 coding
.src_chars
= src_chars
;
8117 coding
.src_bytes
= src_bytes
;
8118 coding
.src_multibyte
= multibytep
;
8119 coding
.consumed
= 0;
8120 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8121 coding
.head_ascii
= 0;
8123 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8125 /* At first, detect text-format if necessary. */
8126 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8127 if (base_category
== coding_category_undecided
)
8129 enum coding_category category
IF_LINT (= 0);
8130 struct coding_system
*this IF_LINT (= NULL
);
8133 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8134 for (; src
< src_end
; src
++)
8139 eight_bit_found
= 1;
8140 if (null_byte_found
)
8145 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8146 && ! inhibit_iso_escape_detection
8147 && ! detect_info
.checked
)
8149 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8151 /* We have scanned the whole data. */
8152 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8154 /* We didn't find an 8-bit code. We may
8155 have found a null-byte, but it's very
8156 rare that a binary file confirm to
8159 coding
.head_ascii
= src
- coding
.source
;
8161 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8165 else if (! c
&& !inhibit_null_byte_detection
)
8167 null_byte_found
= 1;
8168 if (eight_bit_found
)
8171 if (! eight_bit_found
)
8172 coding
.head_ascii
++;
8174 else if (! eight_bit_found
)
8175 coding
.head_ascii
++;
8178 if (null_byte_found
|| eight_bit_found
8179 || coding
.head_ascii
< coding
.src_bytes
8180 || detect_info
.found
)
8182 if (coding
.head_ascii
== coding
.src_bytes
)
8183 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8184 for (i
= 0; i
< coding_category_raw_text
; i
++)
8186 category
= coding_priorities
[i
];
8187 this = coding_categories
+ category
;
8188 if (detect_info
.found
& (1 << category
))
8193 if (null_byte_found
)
8195 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8196 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8198 for (i
= 0; i
< coding_category_raw_text
; i
++)
8200 category
= coding_priorities
[i
];
8201 this = coding_categories
+ category
;
8205 /* No coding system of this category is defined. */
8206 detect_info
.rejected
|= (1 << category
);
8208 else if (category
>= coding_category_raw_text
)
8210 else if (detect_info
.checked
& (1 << category
))
8213 && (detect_info
.found
& (1 << category
)))
8216 else if ((*(this->detector
)) (&coding
, &detect_info
)
8218 && (detect_info
.found
& (1 << category
)))
8220 if (category
== coding_category_utf_16_auto
)
8222 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8223 category
= coding_category_utf_16_le
;
8225 category
= coding_category_utf_16_be
;
8233 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8236 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8237 id
= CODING_SYSTEM_ID (Qno_conversion
);
8238 val
= Fcons (make_number (id
), Qnil
);
8240 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8242 detect_info
.found
= CATEGORY_MASK_ANY
;
8243 id
= coding_categories
[coding_category_undecided
].id
;
8244 val
= Fcons (make_number (id
), Qnil
);
8248 if (detect_info
.found
)
8250 detect_info
.found
= 1 << category
;
8251 val
= Fcons (make_number (this->id
), Qnil
);
8254 for (i
= 0; i
< coding_category_raw_text
; i
++)
8255 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8257 detect_info
.found
= 1 << coding_priorities
[i
];
8258 id
= coding_categories
[coding_priorities
[i
]].id
;
8259 val
= Fcons (make_number (id
), Qnil
);
8265 int mask
= detect_info
.rejected
| detect_info
.found
;
8268 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8270 category
= coding_priorities
[i
];
8271 if (! (mask
& (1 << category
)))
8273 found
|= 1 << category
;
8274 id
= coding_categories
[category
].id
;
8276 val
= Fcons (make_number (id
), val
);
8279 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8281 category
= coding_priorities
[i
];
8282 if (detect_info
.found
& (1 << category
))
8284 id
= coding_categories
[category
].id
;
8285 val
= Fcons (make_number (id
), val
);
8288 detect_info
.found
|= found
;
8291 else if (base_category
== coding_category_utf_8_auto
)
8293 if (detect_coding_utf_8 (&coding
, &detect_info
))
8295 struct coding_system
*this;
8297 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8298 this = coding_categories
+ coding_category_utf_8_sig
;
8300 this = coding_categories
+ coding_category_utf_8_nosig
;
8301 val
= Fcons (make_number (this->id
), Qnil
);
8304 else if (base_category
== coding_category_utf_16_auto
)
8306 if (detect_coding_utf_16 (&coding
, &detect_info
))
8308 struct coding_system
*this;
8310 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8311 this = coding_categories
+ coding_category_utf_16_le
;
8312 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8313 this = coding_categories
+ coding_category_utf_16_be
;
8314 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8315 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8317 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8318 val
= Fcons (make_number (this->id
), Qnil
);
8323 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8324 val
= Fcons (make_number (coding
.id
), Qnil
);
8327 /* Then, detect eol-format if necessary. */
8329 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8332 if (VECTORP (eol_type
))
8334 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8336 if (null_byte_found
)
8337 normal_eol
= EOL_SEEN_LF
;
8339 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8340 coding_category_raw_text
);
8342 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8343 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8344 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8345 coding_category_utf_16_be
);
8346 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8347 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8348 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8349 coding_category_utf_16_le
);
8353 if (EQ (eol_type
, Qunix
))
8354 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8355 else if (EQ (eol_type
, Qdos
))
8356 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8358 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8361 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8363 enum coding_category category
;
8366 id
= XINT (XCAR (tail
));
8367 attrs
= CODING_ID_ATTRS (id
);
8368 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8369 eol_type
= CODING_ID_EOL_TYPE (id
);
8370 if (VECTORP (eol_type
))
8372 if (category
== coding_category_utf_16_be
8373 || category
== coding_category_utf_16_be_nosig
)
8374 this_eol
= utf_16_be_eol
;
8375 else if (category
== coding_category_utf_16_le
8376 || category
== coding_category_utf_16_le_nosig
)
8377 this_eol
= utf_16_le_eol
;
8379 this_eol
= normal_eol
;
8381 if (this_eol
== EOL_SEEN_LF
)
8382 XSETCAR (tail
, AREF (eol_type
, 0));
8383 else if (this_eol
== EOL_SEEN_CRLF
)
8384 XSETCAR (tail
, AREF (eol_type
, 1));
8385 else if (this_eol
== EOL_SEEN_CR
)
8386 XSETCAR (tail
, AREF (eol_type
, 2));
8388 XSETCAR (tail
, CODING_ID_NAME (id
));
8391 XSETCAR (tail
, CODING_ID_NAME (id
));
8395 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8399 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8401 doc
: /* Detect coding system of the text in the region between START and END.
8402 Return a list of possible coding systems ordered by priority.
8403 The coding systems to try and their priorities follows what
8404 the function `coding-system-priority-list' (which see) returns.
8406 If only ASCII characters are found (except for such ISO-2022 control
8407 characters as ESC), it returns a list of single element `undecided'
8408 or its subsidiary coding system according to a detected end-of-line
8411 If optional argument HIGHEST is non-nil, return the coding system of
8412 highest priority. */)
8413 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8416 ptrdiff_t from_byte
, to_byte
;
8418 CHECK_NUMBER_COERCE_MARKER (start
);
8419 CHECK_NUMBER_COERCE_MARKER (end
);
8421 validate_region (&start
, &end
);
8422 from
= XINT (start
), to
= XINT (end
);
8423 from_byte
= CHAR_TO_BYTE (from
);
8424 to_byte
= CHAR_TO_BYTE (to
);
8426 if (from
< GPT
&& to
>= GPT
)
8427 move_gap_both (to
, to_byte
);
8429 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8430 to
- from
, to_byte
- from_byte
,
8432 !NILP (BVAR (current_buffer
8433 , enable_multibyte_characters
)),
8437 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8439 doc
: /* Detect coding system of the text in STRING.
8440 Return a list of possible coding systems ordered by priority.
8441 The coding systems to try and their priorities follows what
8442 the function `coding-system-priority-list' (which see) returns.
8444 If only ASCII characters are found (except for such ISO-2022 control
8445 characters as ESC), it returns a list of single element `undecided'
8446 or its subsidiary coding system according to a detected end-of-line
8449 If optional argument HIGHEST is non-nil, return the coding system of
8450 highest priority. */)
8451 (Lisp_Object string
, Lisp_Object highest
)
8453 CHECK_STRING (string
);
8455 return detect_coding_system (SDATA (string
),
8456 SCHARS (string
), SBYTES (string
),
8457 !NILP (highest
), STRING_MULTIBYTE (string
),
8463 char_encodable_p (int c
, Lisp_Object attrs
)
8466 struct charset
*charset
;
8467 Lisp_Object translation_table
;
8469 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8470 if (! NILP (translation_table
))
8471 c
= translate_char (translation_table
, c
);
8472 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8473 CONSP (tail
); tail
= XCDR (tail
))
8475 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8476 if (CHAR_CHARSET_P (c
, charset
))
8479 return (! NILP (tail
));
8483 /* Return a list of coding systems that safely encode the text between
8484 START and END. If EXCLUDE is non-nil, it is a list of coding
8485 systems not to check. The returned list doesn't contain any such
8486 coding systems. In any case, if the text contains only ASCII or is
8487 unibyte, return t. */
8489 DEFUN ("find-coding-systems-region-internal",
8490 Ffind_coding_systems_region_internal
,
8491 Sfind_coding_systems_region_internal
, 2, 3, 0,
8492 doc
: /* Internal use only. */)
8493 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8495 Lisp_Object coding_attrs_list
, safe_codings
;
8496 ptrdiff_t start_byte
, end_byte
;
8497 const unsigned char *p
, *pbeg
, *pend
;
8499 Lisp_Object tail
, elt
, work_table
;
8501 if (STRINGP (start
))
8503 if (!STRING_MULTIBYTE (start
)
8504 || SCHARS (start
) == SBYTES (start
))
8507 end_byte
= SBYTES (start
);
8511 CHECK_NUMBER_COERCE_MARKER (start
);
8512 CHECK_NUMBER_COERCE_MARKER (end
);
8513 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8514 args_out_of_range (start
, end
);
8515 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8517 start_byte
= CHAR_TO_BYTE (XINT (start
));
8518 end_byte
= CHAR_TO_BYTE (XINT (end
));
8519 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8522 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8524 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8525 move_gap_both (XINT (start
), start_byte
);
8527 move_gap_both (XINT (end
), end_byte
);
8531 coding_attrs_list
= Qnil
;
8532 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8534 || NILP (Fmemq (XCAR (tail
), exclude
)))
8538 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8539 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8540 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8542 ASET (attrs
, coding_attr_trans_tbl
,
8543 get_translation_table (attrs
, 1, NULL
));
8544 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8548 if (STRINGP (start
))
8549 p
= pbeg
= SDATA (start
);
8551 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8552 pend
= p
+ (end_byte
- start_byte
);
8554 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8555 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8557 work_table
= Fmake_char_table (Qnil
, Qnil
);
8560 if (ASCII_BYTE_P (*p
))
8564 c
= STRING_CHAR_ADVANCE (p
);
8565 if (!NILP (char_table_ref (work_table
, c
)))
8566 /* This character was already checked. Ignore it. */
8569 charset_map_loaded
= 0;
8570 for (tail
= coding_attrs_list
; CONSP (tail
);)
8575 else if (char_encodable_p (c
, elt
))
8577 else if (CONSP (XCDR (tail
)))
8579 XSETCAR (tail
, XCAR (XCDR (tail
)));
8580 XSETCDR (tail
, XCDR (XCDR (tail
)));
8584 XSETCAR (tail
, Qnil
);
8588 if (charset_map_loaded
)
8590 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8592 if (STRINGP (start
))
8593 pbeg
= SDATA (start
);
8595 pbeg
= BYTE_POS_ADDR (start_byte
);
8596 p
= pbeg
+ p_offset
;
8597 pend
= pbeg
+ pend_offset
;
8599 char_table_set (work_table
, c
, Qt
);
8603 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8604 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8605 if (! NILP (XCAR (tail
)))
8606 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8608 return safe_codings
;
8612 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8613 Sunencodable_char_position
, 3, 5, 0,
8615 Return position of first un-encodable character in a region.
8616 START and END specify the region and CODING-SYSTEM specifies the
8617 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8619 If optional 4th argument COUNT is non-nil, it specifies at most how
8620 many un-encodable characters to search. In this case, the value is a
8623 If optional 5th argument STRING is non-nil, it is a string to search
8624 for un-encodable characters. In that case, START and END are indexes
8626 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8629 struct coding_system coding
;
8630 Lisp_Object attrs
, charset_list
, translation_table
;
8631 Lisp_Object positions
;
8633 const unsigned char *p
, *stop
, *pend
;
8634 bool ascii_compatible
;
8636 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8637 attrs
= CODING_ID_ATTRS (coding
.id
);
8638 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8640 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8641 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8642 translation_table
= get_translation_table (attrs
, 1, NULL
);
8646 validate_region (&start
, &end
);
8647 from
= XINT (start
);
8649 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8650 || (ascii_compatible
8651 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8653 p
= CHAR_POS_ADDR (from
);
8654 pend
= CHAR_POS_ADDR (to
);
8655 if (from
< GPT
&& to
>= GPT
)
8662 CHECK_STRING (string
);
8663 CHECK_NATNUM (start
);
8665 if (! (XINT (start
) <= XINT (end
) && XINT (end
) <= SCHARS (string
)))
8666 args_out_of_range_3 (string
, start
, end
);
8667 from
= XINT (start
);
8669 if (! STRING_MULTIBYTE (string
))
8671 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8672 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8673 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8681 CHECK_NATNUM (count
);
8686 charset_map_loaded
= 0;
8691 if (ascii_compatible
)
8692 while (p
< stop
&& ASCII_BYTE_P (*p
))
8702 c
= STRING_CHAR_ADVANCE (p
);
8703 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8704 && ! char_charset (translate_char (translation_table
, c
),
8705 charset_list
, NULL
))
8707 positions
= Fcons (make_number (from
), positions
);
8714 if (charset_map_loaded
&& NILP (string
))
8716 p
= CHAR_POS_ADDR (from
);
8717 pend
= CHAR_POS_ADDR (to
);
8718 if (from
< GPT
&& to
>= GPT
)
8722 charset_map_loaded
= 0;
8726 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8730 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8731 Scheck_coding_systems_region
, 3, 3, 0,
8732 doc
: /* Check if the region is encodable by coding systems.
8734 START and END are buffer positions specifying the region.
8735 CODING-SYSTEM-LIST is a list of coding systems to check.
8737 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8738 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8739 whole region, POS0, POS1, ... are buffer positions where non-encodable
8740 characters are found.
8742 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8745 START may be a string. In that case, check if the string is
8746 encodable, and the value contains indices to the string instead of
8747 buffer positions. END is ignored.
8749 If the current buffer (or START if it is a string) is unibyte, the value
8751 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8754 ptrdiff_t start_byte
, end_byte
;
8756 const unsigned char *p
, *pbeg
, *pend
;
8758 Lisp_Object tail
, elt
, attrs
;
8760 if (STRINGP (start
))
8762 if (!STRING_MULTIBYTE (start
)
8763 || SCHARS (start
) == SBYTES (start
))
8766 end_byte
= SBYTES (start
);
8771 CHECK_NUMBER_COERCE_MARKER (start
);
8772 CHECK_NUMBER_COERCE_MARKER (end
);
8773 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8774 args_out_of_range (start
, end
);
8775 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8777 start_byte
= CHAR_TO_BYTE (XINT (start
));
8778 end_byte
= CHAR_TO_BYTE (XINT (end
));
8779 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8782 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8784 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8785 move_gap_both (XINT (start
), start_byte
);
8787 move_gap_both (XINT (end
), end_byte
);
8793 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8796 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8797 ASET (attrs
, coding_attr_trans_tbl
,
8798 get_translation_table (attrs
, 1, NULL
));
8799 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8802 if (STRINGP (start
))
8803 p
= pbeg
= SDATA (start
);
8805 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8806 pend
= p
+ (end_byte
- start_byte
);
8808 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8809 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8813 if (ASCII_BYTE_P (*p
))
8817 c
= STRING_CHAR_ADVANCE (p
);
8819 charset_map_loaded
= 0;
8820 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8822 elt
= XCDR (XCAR (tail
));
8823 if (! char_encodable_p (c
, XCAR (elt
)))
8824 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8826 if (charset_map_loaded
)
8828 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8830 if (STRINGP (start
))
8831 pbeg
= SDATA (start
);
8833 pbeg
= BYTE_POS_ADDR (start_byte
);
8834 p
= pbeg
+ p_offset
;
8835 pend
= pbeg
+ pend_offset
;
8843 for (; CONSP (tail
); tail
= XCDR (tail
))
8846 if (CONSP (XCDR (XCDR (elt
))))
8847 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8856 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8857 Lisp_Object coding_system
, Lisp_Object dst_object
,
8858 bool encodep
, bool norecord
)
8860 struct coding_system coding
;
8861 ptrdiff_t from
, from_byte
, to
, to_byte
;
8862 Lisp_Object src_object
;
8864 CHECK_NUMBER_COERCE_MARKER (start
);
8865 CHECK_NUMBER_COERCE_MARKER (end
);
8866 if (NILP (coding_system
))
8867 coding_system
= Qno_conversion
;
8869 CHECK_CODING_SYSTEM (coding_system
);
8870 src_object
= Fcurrent_buffer ();
8871 if (NILP (dst_object
))
8872 dst_object
= src_object
;
8873 else if (! EQ (dst_object
, Qt
))
8874 CHECK_BUFFER (dst_object
);
8876 validate_region (&start
, &end
);
8877 from
= XFASTINT (start
);
8878 from_byte
= CHAR_TO_BYTE (from
);
8879 to
= XFASTINT (end
);
8880 to_byte
= CHAR_TO_BYTE (to
);
8882 setup_coding_system (coding_system
, &coding
);
8883 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8886 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8889 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8892 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8894 return (BUFFERP (dst_object
)
8895 ? make_number (coding
.produced_char
)
8896 : coding
.dst_object
);
8900 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8901 3, 4, "r\nzCoding system: ",
8902 doc
: /* Decode the current region from the specified coding system.
8903 When called from a program, takes four arguments:
8904 START, END, CODING-SYSTEM, and DESTINATION.
8905 START and END are buffer positions.
8907 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8908 If nil, the region between START and END is replaced by the decoded text.
8909 If buffer, the decoded text is inserted in that buffer after point (point
8911 In those cases, the length of the decoded text is returned.
8912 If DESTINATION is t, the decoded text is returned.
8914 This function sets `last-coding-system-used' to the precise coding system
8915 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8916 not fully specified.) */)
8917 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8919 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8922 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8923 3, 4, "r\nzCoding system: ",
8924 doc
: /* Encode the current region by specified coding system.
8925 When called from a program, takes four arguments:
8926 START, END, CODING-SYSTEM and DESTINATION.
8927 START and END are buffer positions.
8929 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8930 If nil, the region between START and END is replace by the encoded text.
8931 If buffer, the encoded text is inserted in that buffer after point (point
8933 In those cases, the length of the encoded text is returned.
8934 If DESTINATION is t, the encoded text is returned.
8936 This function sets `last-coding-system-used' to the precise coding system
8937 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8938 not fully specified.) */)
8939 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8941 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8945 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8946 Lisp_Object dst_object
, bool encodep
, bool nocopy
,
8949 struct coding_system coding
;
8950 ptrdiff_t chars
, bytes
;
8952 CHECK_STRING (string
);
8953 if (NILP (coding_system
))
8956 Vlast_coding_system_used
= Qno_conversion
;
8957 if (NILP (dst_object
))
8958 return (nocopy
? Fcopy_sequence (string
) : string
);
8961 if (NILP (coding_system
))
8962 coding_system
= Qno_conversion
;
8964 CHECK_CODING_SYSTEM (coding_system
);
8965 if (NILP (dst_object
))
8967 else if (! EQ (dst_object
, Qt
))
8968 CHECK_BUFFER (dst_object
);
8970 setup_coding_system (coding_system
, &coding
);
8971 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8972 chars
= SCHARS (string
);
8973 bytes
= SBYTES (string
);
8975 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8977 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8979 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8981 return (BUFFERP (dst_object
)
8982 ? make_number (coding
.produced_char
)
8983 : coding
.dst_object
);
8987 /* Encode or decode STRING according to CODING_SYSTEM.
8988 Do not set Vlast_coding_system_used.
8990 This function is called only from macros DECODE_FILE and
8991 ENCODE_FILE, thus we ignore character composition. */
8994 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
8997 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9001 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9003 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9005 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9006 if the decoding operation is trivial.
9008 Optional fourth arg BUFFER non-nil means that the decoded text is
9009 inserted in that buffer after point (point does not move). In this
9010 case, the return value is the length of the decoded text.
9012 This function sets `last-coding-system-used' to the precise coding system
9013 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9014 not fully specified.) */)
9015 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9017 return code_convert_string (string
, coding_system
, buffer
,
9018 0, ! NILP (nocopy
), 0);
9021 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9023 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9025 Optional third arg NOCOPY non-nil means it is OK to return STRING
9026 itself if the encoding operation is trivial.
9028 Optional fourth arg BUFFER non-nil means that the encoded text is
9029 inserted in that buffer after point (point does not move). In this
9030 case, the return value is the length of the encoded text.
9032 This function sets `last-coding-system-used' to the precise coding system
9033 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9034 not fully specified.) */)
9035 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9037 return code_convert_string (string
, coding_system
, buffer
,
9038 1, ! NILP (nocopy
), 0);
9042 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9043 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9044 Return the corresponding character. */)
9047 Lisp_Object spec
, attrs
, val
;
9048 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9052 CHECK_NATNUM (code
);
9053 ch
= XFASTINT (code
);
9054 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9055 attrs
= AREF (spec
, 0);
9057 if (ASCII_BYTE_P (ch
)
9058 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9061 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9062 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9063 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9064 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9069 charset
= charset_roman
;
9071 else if (ch
>= 0xA0 && ch
< 0xDF)
9074 charset
= charset_kana
;
9078 EMACS_INT c1
= ch
>> 8;
9081 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9082 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9083 error ("Invalid code: %"pI
"d", ch
);
9086 charset
= charset_kanji
;
9088 c
= DECODE_CHAR (charset
, c
);
9090 error ("Invalid code: %"pI
"d", ch
);
9091 return make_number (c
);
9095 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9096 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9097 Return the corresponding code in SJIS. */)
9100 Lisp_Object spec
, attrs
, charset_list
;
9102 struct charset
*charset
;
9105 CHECK_CHARACTER (ch
);
9107 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9108 attrs
= AREF (spec
, 0);
9110 if (ASCII_CHAR_P (c
)
9111 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9114 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9115 charset
= char_charset (c
, charset_list
, &code
);
9116 if (code
== CHARSET_INVALID_CODE (charset
))
9117 error ("Can't encode by shift_jis encoding: %c", c
);
9120 return make_number (code
);
9123 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9124 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9125 Return the corresponding character. */)
9128 Lisp_Object spec
, attrs
, val
;
9129 struct charset
*charset_roman
, *charset_big5
, *charset
;
9133 CHECK_NATNUM (code
);
9134 ch
= XFASTINT (code
);
9135 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9136 attrs
= AREF (spec
, 0);
9138 if (ASCII_BYTE_P (ch
)
9139 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9142 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9143 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9144 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9149 charset
= charset_roman
;
9153 EMACS_INT b1
= ch
>> 8;
9155 if (b1
< 0xA1 || b1
> 0xFE
9156 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9157 error ("Invalid code: %"pI
"d", ch
);
9159 charset
= charset_big5
;
9161 c
= DECODE_CHAR (charset
, c
);
9163 error ("Invalid code: %"pI
"d", ch
);
9164 return make_number (c
);
9167 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9168 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9169 Return the corresponding character code in Big5. */)
9172 Lisp_Object spec
, attrs
, charset_list
;
9173 struct charset
*charset
;
9177 CHECK_CHARACTER (ch
);
9179 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9180 attrs
= AREF (spec
, 0);
9181 if (ASCII_CHAR_P (c
)
9182 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9185 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9186 charset
= char_charset (c
, charset_list
, &code
);
9187 if (code
== CHARSET_INVALID_CODE (charset
))
9188 error ("Can't encode by Big5 encoding: %c", c
);
9190 return make_number (code
);
9194 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9195 Sset_terminal_coding_system_internal
, 1, 2, 0,
9196 doc
: /* Internal use only. */)
9197 (Lisp_Object coding_system
, Lisp_Object terminal
)
9199 struct terminal
*term
= get_terminal (terminal
, 1);
9200 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9201 CHECK_SYMBOL (coding_system
);
9202 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9203 /* We had better not send unsafe characters to terminal. */
9204 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9205 /* Character composition should be disabled. */
9206 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9207 terminal_coding
->src_multibyte
= 1;
9208 terminal_coding
->dst_multibyte
= 0;
9210 (term
, (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
9211 ? coding_charset_list (terminal_coding
)
9212 : Fcons (make_number (charset_ascii
), Qnil
)));
9216 DEFUN ("set-safe-terminal-coding-system-internal",
9217 Fset_safe_terminal_coding_system_internal
,
9218 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9219 doc
: /* Internal use only. */)
9220 (Lisp_Object coding_system
)
9222 CHECK_SYMBOL (coding_system
);
9223 setup_coding_system (Fcheck_coding_system (coding_system
),
9224 &safe_terminal_coding
);
9225 /* Character composition should be disabled. */
9226 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9227 safe_terminal_coding
.src_multibyte
= 1;
9228 safe_terminal_coding
.dst_multibyte
= 0;
9232 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9233 Sterminal_coding_system
, 0, 1, 0,
9234 doc
: /* Return coding system specified for terminal output on the given terminal.
9235 TERMINAL may be a terminal object, a frame, or nil for the selected
9236 frame's terminal device. */)
9237 (Lisp_Object terminal
)
9239 struct coding_system
*terminal_coding
9240 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9241 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9243 /* For backward compatibility, return nil if it is `undecided'. */
9244 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9247 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9248 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9249 doc
: /* Internal use only. */)
9250 (Lisp_Object coding_system
, Lisp_Object terminal
)
9252 struct terminal
*t
= get_terminal (terminal
, 1);
9253 CHECK_SYMBOL (coding_system
);
9254 if (NILP (coding_system
))
9255 coding_system
= Qno_conversion
;
9257 Fcheck_coding_system (coding_system
);
9258 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9259 /* Character composition should be disabled. */
9260 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9261 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9265 DEFUN ("keyboard-coding-system",
9266 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9267 doc
: /* Return coding system specified for decoding keyboard input. */)
9268 (Lisp_Object terminal
)
9270 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9271 (get_terminal (terminal
, 1))->id
);
9275 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9276 Sfind_operation_coding_system
, 1, MANY
, 0,
9277 doc
: /* Choose a coding system for an operation based on the target name.
9278 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9279 DECODING-SYSTEM is the coding system to use for decoding
9280 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9281 for encoding (in case OPERATION does encoding).
9283 The first argument OPERATION specifies an I/O primitive:
9284 For file I/O, `insert-file-contents' or `write-region'.
9285 For process I/O, `call-process', `call-process-region', or `start-process'.
9286 For network I/O, `open-network-stream'.
9288 The remaining arguments should be the same arguments that were passed
9289 to the primitive. Depending on which primitive, one of those arguments
9290 is selected as the TARGET. For example, if OPERATION does file I/O,
9291 whichever argument specifies the file name is TARGET.
9293 TARGET has a meaning which depends on OPERATION:
9294 For file I/O, TARGET is a file name (except for the special case below).
9295 For process I/O, TARGET is a process name.
9296 For network I/O, TARGET is a service name or a port number.
9298 This function looks up what is specified for TARGET in
9299 `file-coding-system-alist', `process-coding-system-alist',
9300 or `network-coding-system-alist' depending on OPERATION.
9301 They may specify a coding system, a cons of coding systems,
9302 or a function symbol to call.
9303 In the last case, we call the function with one argument,
9304 which is a list of all the arguments given to this function.
9305 If the function can't decide a coding system, it can return
9306 `undecided' so that the normal code-detection is performed.
9308 If OPERATION is `insert-file-contents', the argument corresponding to
9309 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9310 file name to look up, and BUFFER is a buffer that contains the file's
9311 contents (not yet decoded). If `file-coding-system-alist' specifies a
9312 function to call for FILENAME, that function should examine the
9313 contents of BUFFER instead of reading the file.
9315 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9316 (ptrdiff_t nargs
, Lisp_Object
*args
)
9318 Lisp_Object operation
, target_idx
, target
, val
;
9319 register Lisp_Object chain
;
9322 error ("Too few arguments");
9323 operation
= args
[0];
9324 if (!SYMBOLP (operation
)
9325 || (target_idx
= Fget (operation
, Qtarget_idx
), !NATNUMP (target_idx
)))
9326 error ("Invalid first argument");
9327 if (nargs
<= 1 + XFASTINT (target_idx
))
9328 error ("Too few arguments for operation `%s'",
9329 SDATA (SYMBOL_NAME (operation
)));
9330 target
= args
[XFASTINT (target_idx
) + 1];
9331 if (!(STRINGP (target
)
9332 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9333 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9334 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9335 error ("Invalid argument %"pI
"d of operation `%s'",
9336 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9338 target
= XCAR (target
);
9340 chain
= ((EQ (operation
, Qinsert_file_contents
)
9341 || EQ (operation
, Qwrite_region
))
9342 ? Vfile_coding_system_alist
9343 : (EQ (operation
, Qopen_network_stream
)
9344 ? Vnetwork_coding_system_alist
9345 : Vprocess_coding_system_alist
));
9349 for (; CONSP (chain
); chain
= XCDR (chain
))
9355 && ((STRINGP (target
)
9356 && STRINGP (XCAR (elt
))
9357 && fast_string_match (XCAR (elt
), target
) >= 0)
9358 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9361 /* Here, if VAL is both a valid coding system and a valid
9362 function symbol, we return VAL as a coding system. */
9365 if (! SYMBOLP (val
))
9367 if (! NILP (Fcoding_system_p (val
)))
9368 return Fcons (val
, val
);
9369 if (! NILP (Ffboundp (val
)))
9371 /* We use call1 rather than safe_call1
9372 so as to get bug reports about functions called here
9373 which don't handle the current interface. */
9374 val
= call1 (val
, Flist (nargs
, args
));
9377 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9378 return Fcons (val
, val
);
9386 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9387 Sset_coding_system_priority
, 0, MANY
, 0,
9388 doc
: /* Assign higher priority to the coding systems given as arguments.
9389 If multiple coding systems belong to the same category,
9390 all but the first one are ignored.
9392 usage: (set-coding-system-priority &rest coding-systems) */)
9393 (ptrdiff_t nargs
, Lisp_Object
*args
)
9396 bool changed
[coding_category_max
];
9397 enum coding_category priorities
[coding_category_max
];
9399 memset (changed
, 0, sizeof changed
);
9401 for (i
= j
= 0; i
< nargs
; i
++)
9403 enum coding_category category
;
9404 Lisp_Object spec
, attrs
;
9406 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9407 attrs
= AREF (spec
, 0);
9408 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9409 if (changed
[category
])
9410 /* Ignore this coding system because a coding system of the
9411 same category already had a higher priority. */
9413 changed
[category
] = 1;
9414 priorities
[j
++] = category
;
9415 if (coding_categories
[category
].id
>= 0
9416 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9417 setup_coding_system (args
[i
], &coding_categories
[category
]);
9418 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9421 /* Now we have decided top J priorities. Reflect the order of the
9422 original priorities to the remaining priorities. */
9424 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9426 while (j
< coding_category_max
9427 && changed
[coding_priorities
[j
]])
9429 if (j
== coding_category_max
)
9431 priorities
[i
] = coding_priorities
[j
];
9434 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9436 /* Update `coding-category-list'. */
9437 Vcoding_category_list
= Qnil
;
9438 for (i
= coding_category_max
; i
-- > 0; )
9439 Vcoding_category_list
9440 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9441 Vcoding_category_list
);
9446 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9447 Scoding_system_priority_list
, 0, 1, 0,
9448 doc
: /* Return a list of coding systems ordered by their priorities.
9449 The list contains a subset of coding systems; i.e. coding systems
9450 assigned to each coding category (see `coding-category-list').
9452 HIGHESTP non-nil means just return the highest priority one. */)
9453 (Lisp_Object highestp
)
9458 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9460 enum coding_category category
= coding_priorities
[i
];
9461 int id
= coding_categories
[category
].id
;
9466 attrs
= CODING_ID_ATTRS (id
);
9467 if (! NILP (highestp
))
9468 return CODING_ATTR_BASE_NAME (attrs
);
9469 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9471 return Fnreverse (val
);
9474 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9477 make_subsidiaries (Lisp_Object base
)
9479 Lisp_Object subsidiaries
;
9480 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
9481 char *buf
= alloca (base_name_len
+ 6);
9484 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9485 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9486 for (i
= 0; i
< 3; i
++)
9488 strcpy (buf
+ base_name_len
, suffixes
[i
]);
9489 ASET (subsidiaries
, i
, intern (buf
));
9491 return subsidiaries
;
9495 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9496 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9497 doc
: /* For internal use only.
9498 usage: (define-coding-system-internal ...) */)
9499 (ptrdiff_t nargs
, Lisp_Object
*args
)
9502 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9503 Lisp_Object attrs
; /* Vector of attributes. */
9504 Lisp_Object eol_type
;
9505 Lisp_Object aliases
;
9506 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9507 enum coding_category category
;
9508 Lisp_Object tail
, val
;
9509 int max_charset_id
= 0;
9512 if (nargs
< coding_arg_max
)
9515 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9517 name
= args
[coding_arg_name
];
9518 CHECK_SYMBOL (name
);
9519 ASET (attrs
, coding_attr_base_name
, name
);
9521 val
= args
[coding_arg_mnemonic
];
9522 if (! STRINGP (val
))
9523 CHECK_CHARACTER (val
);
9524 ASET (attrs
, coding_attr_mnemonic
, val
);
9526 coding_type
= args
[coding_arg_coding_type
];
9527 CHECK_SYMBOL (coding_type
);
9528 ASET (attrs
, coding_attr_type
, coding_type
);
9530 charset_list
= args
[coding_arg_charset_list
];
9531 if (SYMBOLP (charset_list
))
9533 if (EQ (charset_list
, Qiso_2022
))
9535 if (! EQ (coding_type
, Qiso_2022
))
9536 error ("Invalid charset-list");
9537 charset_list
= Viso_2022_charset_list
;
9539 else if (EQ (charset_list
, Qemacs_mule
))
9541 if (! EQ (coding_type
, Qemacs_mule
))
9542 error ("Invalid charset-list");
9543 charset_list
= Vemacs_mule_charset_list
;
9545 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9547 if (! RANGED_INTEGERP (0, XCAR (tail
), INT_MAX
- 1))
9548 error ("Invalid charset-list");
9549 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9550 max_charset_id
= XFASTINT (XCAR (tail
));
9555 charset_list
= Fcopy_sequence (charset_list
);
9556 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9558 struct charset
*charset
;
9561 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9562 if (EQ (coding_type
, Qiso_2022
)
9563 ? CHARSET_ISO_FINAL (charset
) < 0
9564 : EQ (coding_type
, Qemacs_mule
)
9565 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9567 error ("Can't handle charset `%s'",
9568 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9570 XSETCAR (tail
, make_number (charset
->id
));
9571 if (max_charset_id
< charset
->id
)
9572 max_charset_id
= charset
->id
;
9575 ASET (attrs
, coding_attr_charset_list
, charset_list
);
9577 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9578 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9579 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9580 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9581 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
9583 ASET (attrs
, coding_attr_ascii_compat
, args
[coding_arg_ascii_compatible_p
]);
9585 val
= args
[coding_arg_decode_translation_table
];
9586 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9588 ASET (attrs
, coding_attr_decode_tbl
, val
);
9590 val
= args
[coding_arg_encode_translation_table
];
9591 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9593 ASET (attrs
, coding_attr_encode_tbl
, val
);
9595 val
= args
[coding_arg_post_read_conversion
];
9597 ASET (attrs
, coding_attr_post_read
, val
);
9599 val
= args
[coding_arg_pre_write_conversion
];
9601 ASET (attrs
, coding_attr_pre_write
, val
);
9603 val
= args
[coding_arg_default_char
];
9605 ASET (attrs
, coding_attr_default_char
, make_number (' '));
9608 CHECK_CHARACTER (val
);
9609 ASET (attrs
, coding_attr_default_char
, val
);
9612 val
= args
[coding_arg_for_unibyte
];
9613 ASET (attrs
, coding_attr_for_unibyte
, NILP (val
) ? Qnil
: Qt
);
9615 val
= args
[coding_arg_plist
];
9617 ASET (attrs
, coding_attr_plist
, val
);
9619 if (EQ (coding_type
, Qcharset
))
9621 /* Generate a lisp vector of 256 elements. Each element is nil,
9622 integer, or a list of charset IDs.
9624 If Nth element is nil, the byte code N is invalid in this
9627 If Nth element is a number NUM, N is the first byte of a
9628 charset whose ID is NUM.
9630 If Nth element is a list of charset IDs, N is the first byte
9631 of one of them. The list is sorted by dimensions of the
9632 charsets. A charset of smaller dimension comes first. */
9633 val
= Fmake_vector (make_number (256), Qnil
);
9635 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9637 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9638 int dim
= CHARSET_DIMENSION (charset
);
9639 int idx
= (dim
- 1) * 4;
9641 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9642 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9644 for (i
= charset
->code_space
[idx
];
9645 i
<= charset
->code_space
[idx
+ 1]; i
++)
9647 Lisp_Object tmp
, tmp2
;
9650 tmp
= AREF (val
, i
);
9653 else if (NUMBERP (tmp
))
9655 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9657 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9659 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9663 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9665 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9670 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9673 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9674 XSETCAR (tmp2
, XCAR (tail
));
9680 ASET (attrs
, coding_attr_charset_valids
, val
);
9681 category
= coding_category_charset
;
9683 else if (EQ (coding_type
, Qccl
))
9687 if (nargs
< coding_arg_ccl_max
)
9690 val
= args
[coding_arg_ccl_decoder
];
9691 CHECK_CCL_PROGRAM (val
);
9693 val
= Fcopy_sequence (val
);
9694 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9696 val
= args
[coding_arg_ccl_encoder
];
9697 CHECK_CCL_PROGRAM (val
);
9699 val
= Fcopy_sequence (val
);
9700 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9702 val
= args
[coding_arg_ccl_valids
];
9703 valids
= Fmake_string (make_number (256), make_number (0));
9704 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
9711 if (! (0 <= XINT (val
) && XINT (val
) <= 255))
9712 args_out_of_range_3 (val
, make_number (0), make_number (255));
9713 from
= to
= XINT (val
);
9718 CHECK_NATNUM_CAR (val
);
9719 CHECK_NUMBER_CDR (val
);
9720 if (XINT (XCAR (val
)) > 255)
9721 args_out_of_range_3 (XCAR (val
),
9722 make_number (0), make_number (255));
9723 from
= XINT (XCAR (val
));
9724 if (! (from
<= XINT (XCDR (val
)) && XINT (XCDR (val
)) <= 255))
9725 args_out_of_range_3 (XCDR (val
),
9726 XCAR (val
), make_number (255));
9727 to
= XINT (XCDR (val
));
9729 for (i
= from
; i
<= to
; i
++)
9730 SSET (valids
, i
, 1);
9732 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9734 category
= coding_category_ccl
;
9736 else if (EQ (coding_type
, Qutf_16
))
9738 Lisp_Object bom
, endian
;
9740 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9742 if (nargs
< coding_arg_utf16_max
)
9745 bom
= args
[coding_arg_utf16_bom
];
9746 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9750 CHECK_CODING_SYSTEM (val
);
9752 CHECK_CODING_SYSTEM (val
);
9754 ASET (attrs
, coding_attr_utf_bom
, bom
);
9756 endian
= args
[coding_arg_utf16_endian
];
9757 CHECK_SYMBOL (endian
);
9760 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9761 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9762 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9764 category
= (CONSP (bom
)
9765 ? coding_category_utf_16_auto
9767 ? (EQ (endian
, Qbig
)
9768 ? coding_category_utf_16_be_nosig
9769 : coding_category_utf_16_le_nosig
)
9770 : (EQ (endian
, Qbig
)
9771 ? coding_category_utf_16_be
9772 : coding_category_utf_16_le
));
9774 else if (EQ (coding_type
, Qiso_2022
))
9776 Lisp_Object initial
, reg_usage
, request
, flags
;
9778 if (nargs
< coding_arg_iso2022_max
)
9781 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9782 CHECK_VECTOR (initial
);
9783 for (i
= 0; i
< 4; i
++)
9785 val
= Faref (initial
, make_number (i
));
9788 struct charset
*charset
;
9790 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9791 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9792 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9793 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9796 ASET (initial
, i
, make_number (-1));
9799 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9800 CHECK_CONS (reg_usage
);
9801 CHECK_NUMBER_CAR (reg_usage
);
9802 CHECK_NUMBER_CDR (reg_usage
);
9804 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9805 for (tail
= request
; CONSP (tail
); tail
= XCDR (tail
))
9813 CHECK_CHARSET_GET_ID (tmp1
, id
);
9814 CHECK_NATNUM_CDR (val
);
9815 if (XINT (XCDR (val
)) >= 4)
9816 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
9817 XSETCAR (val
, make_number (id
));
9820 flags
= args
[coding_arg_iso2022_flags
];
9821 CHECK_NATNUM (flags
);
9822 i
= XINT (flags
) & INT_MAX
;
9823 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9824 i
|= CODING_ISO_FLAG_FULL_SUPPORT
;
9825 flags
= make_number (i
);
9827 ASET (attrs
, coding_attr_iso_initial
, initial
);
9828 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9829 ASET (attrs
, coding_attr_iso_request
, request
);
9830 ASET (attrs
, coding_attr_iso_flags
, flags
);
9831 setup_iso_safe_charsets (attrs
);
9833 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9834 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9835 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9836 ? coding_category_iso_7_else
9837 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9838 ? coding_category_iso_7
9839 : coding_category_iso_7_tight
);
9842 int id
= XINT (AREF (initial
, 1));
9844 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9845 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9847 ? coding_category_iso_8_else
9848 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9849 ? coding_category_iso_8_1
9850 : coding_category_iso_8_2
);
9852 if (category
!= coding_category_iso_8_1
9853 && category
!= coding_category_iso_8_2
)
9854 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9856 else if (EQ (coding_type
, Qemacs_mule
))
9858 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9859 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9860 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9861 category
= coding_category_emacs_mule
;
9863 else if (EQ (coding_type
, Qshift_jis
))
9866 struct charset
*charset
;
9868 if (XINT (Flength (charset_list
)) != 3
9869 && XINT (Flength (charset_list
)) != 4)
9870 error ("There should be three or four charsets");
9872 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9873 if (CHARSET_DIMENSION (charset
) != 1)
9874 error ("Dimension of charset %s is not one",
9875 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9876 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9877 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9879 charset_list
= XCDR (charset_list
);
9880 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9881 if (CHARSET_DIMENSION (charset
) != 1)
9882 error ("Dimension of charset %s is not one",
9883 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9885 charset_list
= XCDR (charset_list
);
9886 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9887 if (CHARSET_DIMENSION (charset
) != 2)
9888 error ("Dimension of charset %s is not two",
9889 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9891 charset_list
= XCDR (charset_list
);
9892 if (! NILP (charset_list
))
9894 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9895 if (CHARSET_DIMENSION (charset
) != 2)
9896 error ("Dimension of charset %s is not two",
9897 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9900 category
= coding_category_sjis
;
9901 Vsjis_coding_system
= name
;
9903 else if (EQ (coding_type
, Qbig5
))
9905 struct charset
*charset
;
9907 if (XINT (Flength (charset_list
)) != 2)
9908 error ("There should be just two charsets");
9910 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9911 if (CHARSET_DIMENSION (charset
) != 1)
9912 error ("Dimension of charset %s is not one",
9913 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9914 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9915 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9917 charset_list
= XCDR (charset_list
);
9918 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9919 if (CHARSET_DIMENSION (charset
) != 2)
9920 error ("Dimension of charset %s is not two",
9921 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9923 category
= coding_category_big5
;
9924 Vbig5_coding_system
= name
;
9926 else if (EQ (coding_type
, Qraw_text
))
9928 category
= coding_category_raw_text
;
9929 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9931 else if (EQ (coding_type
, Qutf_8
))
9935 if (nargs
< coding_arg_utf8_max
)
9938 bom
= args
[coding_arg_utf8_bom
];
9939 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9943 CHECK_CODING_SYSTEM (val
);
9945 CHECK_CODING_SYSTEM (val
);
9947 ASET (attrs
, coding_attr_utf_bom
, bom
);
9949 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9951 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9952 : NILP (bom
) ? coding_category_utf_8_nosig
9953 : coding_category_utf_8_sig
);
9955 else if (EQ (coding_type
, Qundecided
))
9956 category
= coding_category_undecided
;
9958 error ("Invalid coding system type: %s",
9959 SDATA (SYMBOL_NAME (coding_type
)));
9961 ASET (attrs
, coding_attr_category
, make_number (category
));
9962 ASET (attrs
, coding_attr_plist
,
9964 Fcons (AREF (Vcoding_category_table
, category
),
9965 CODING_ATTR_PLIST (attrs
))));
9966 ASET (attrs
, coding_attr_plist
,
9967 Fcons (QCascii_compatible_p
,
9968 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9969 CODING_ATTR_PLIST (attrs
))));
9971 eol_type
= args
[coding_arg_eol_type
];
9972 if (! NILP (eol_type
)
9973 && ! EQ (eol_type
, Qunix
)
9974 && ! EQ (eol_type
, Qdos
)
9975 && ! EQ (eol_type
, Qmac
))
9976 error ("Invalid eol-type");
9978 aliases
= Fcons (name
, Qnil
);
9980 if (NILP (eol_type
))
9982 eol_type
= make_subsidiaries (name
);
9983 for (i
= 0; i
< 3; i
++)
9985 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9987 this_name
= AREF (eol_type
, i
);
9988 this_aliases
= Fcons (this_name
, Qnil
);
9989 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9990 this_spec
= Fmake_vector (make_number (3), attrs
);
9991 ASET (this_spec
, 1, this_aliases
);
9992 ASET (this_spec
, 2, this_eol_type
);
9993 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9994 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9995 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9997 Vcoding_system_alist
9998 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9999 Vcoding_system_alist
);
10003 spec_vec
= Fmake_vector (make_number (3), attrs
);
10004 ASET (spec_vec
, 1, aliases
);
10005 ASET (spec_vec
, 2, eol_type
);
10007 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10008 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10009 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10011 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10012 Vcoding_system_alist
);
10015 int id
= coding_categories
[category
].id
;
10017 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10018 setup_coding_system (name
, &coding_categories
[category
]);
10024 return Fsignal (Qwrong_number_of_arguments
,
10025 Fcons (intern ("define-coding-system-internal"),
10026 make_number (nargs
)));
10030 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10032 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10033 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10035 Lisp_Object spec
, attrs
;
10037 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10038 attrs
= AREF (spec
, 0);
10039 if (EQ (prop
, QCmnemonic
))
10041 if (! STRINGP (val
))
10042 CHECK_CHARACTER (val
);
10043 ASET (attrs
, coding_attr_mnemonic
, val
);
10045 else if (EQ (prop
, QCdefault_char
))
10048 val
= make_number (' ');
10050 CHECK_CHARACTER (val
);
10051 ASET (attrs
, coding_attr_default_char
, val
);
10053 else if (EQ (prop
, QCdecode_translation_table
))
10055 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10056 CHECK_SYMBOL (val
);
10057 ASET (attrs
, coding_attr_decode_tbl
, val
);
10059 else if (EQ (prop
, QCencode_translation_table
))
10061 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10062 CHECK_SYMBOL (val
);
10063 ASET (attrs
, coding_attr_encode_tbl
, val
);
10065 else if (EQ (prop
, QCpost_read_conversion
))
10067 CHECK_SYMBOL (val
);
10068 ASET (attrs
, coding_attr_post_read
, val
);
10070 else if (EQ (prop
, QCpre_write_conversion
))
10072 CHECK_SYMBOL (val
);
10073 ASET (attrs
, coding_attr_pre_write
, val
);
10075 else if (EQ (prop
, QCascii_compatible_p
))
10077 ASET (attrs
, coding_attr_ascii_compat
, val
);
10080 ASET (attrs
, coding_attr_plist
,
10081 Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
));
10086 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10087 Sdefine_coding_system_alias
, 2, 2, 0,
10088 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10089 (Lisp_Object alias
, Lisp_Object coding_system
)
10091 Lisp_Object spec
, aliases
, eol_type
, val
;
10093 CHECK_SYMBOL (alias
);
10094 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10095 aliases
= AREF (spec
, 1);
10096 /* ALIASES should be a list of length more than zero, and the first
10097 element is a base coding system. Append ALIAS at the tail of the
10099 while (!NILP (XCDR (aliases
)))
10100 aliases
= XCDR (aliases
);
10101 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10103 eol_type
= AREF (spec
, 2);
10104 if (VECTORP (eol_type
))
10106 Lisp_Object subsidiaries
;
10109 subsidiaries
= make_subsidiaries (alias
);
10110 for (i
= 0; i
< 3; i
++)
10111 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10112 AREF (eol_type
, i
));
10115 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10116 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10117 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10119 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10120 Vcoding_system_alist
);
10125 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10127 doc
: /* Return the base of CODING-SYSTEM.
10128 Any alias or subsidiary coding system is not a base coding system. */)
10129 (Lisp_Object coding_system
)
10131 Lisp_Object spec
, attrs
;
10133 if (NILP (coding_system
))
10134 return (Qno_conversion
);
10135 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10136 attrs
= AREF (spec
, 0);
10137 return CODING_ATTR_BASE_NAME (attrs
);
10140 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10142 doc
: "Return the property list of CODING-SYSTEM.")
10143 (Lisp_Object coding_system
)
10145 Lisp_Object spec
, attrs
;
10147 if (NILP (coding_system
))
10148 coding_system
= Qno_conversion
;
10149 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10150 attrs
= AREF (spec
, 0);
10151 return CODING_ATTR_PLIST (attrs
);
10155 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10157 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10158 (Lisp_Object coding_system
)
10162 if (NILP (coding_system
))
10163 coding_system
= Qno_conversion
;
10164 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10165 return AREF (spec
, 1);
10168 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10169 Scoding_system_eol_type
, 1, 1, 0,
10170 doc
: /* Return eol-type of CODING-SYSTEM.
10171 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10173 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10174 and CR respectively.
10176 A vector value indicates that a format of end-of-line should be
10177 detected automatically. Nth element of the vector is the subsidiary
10178 coding system whose eol-type is N. */)
10179 (Lisp_Object coding_system
)
10181 Lisp_Object spec
, eol_type
;
10184 if (NILP (coding_system
))
10185 coding_system
= Qno_conversion
;
10186 if (! CODING_SYSTEM_P (coding_system
))
10188 spec
= CODING_SYSTEM_SPEC (coding_system
);
10189 eol_type
= AREF (spec
, 2);
10190 if (VECTORP (eol_type
))
10191 return Fcopy_sequence (eol_type
);
10192 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10193 return make_number (n
);
10199 /*** 9. Post-amble ***/
10202 init_coding_once (void)
10206 for (i
= 0; i
< coding_category_max
; i
++)
10208 coding_categories
[i
].id
= -1;
10209 coding_priorities
[i
] = i
;
10212 /* ISO2022 specific initialize routine. */
10213 for (i
= 0; i
< 0x20; i
++)
10214 iso_code_class
[i
] = ISO_control_0
;
10215 for (i
= 0x21; i
< 0x7F; i
++)
10216 iso_code_class
[i
] = ISO_graphic_plane_0
;
10217 for (i
= 0x80; i
< 0xA0; i
++)
10218 iso_code_class
[i
] = ISO_control_1
;
10219 for (i
= 0xA1; i
< 0xFF; i
++)
10220 iso_code_class
[i
] = ISO_graphic_plane_1
;
10221 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10222 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10223 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10224 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10225 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10226 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10227 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10228 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10229 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10231 for (i
= 0; i
< 256; i
++)
10233 emacs_mule_bytes
[i
] = 1;
10235 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10236 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10237 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10238 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10244 syms_of_coding (void)
10246 staticpro (&Vcoding_system_hash_table
);
10248 Lisp_Object args
[2];
10251 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10254 staticpro (&Vsjis_coding_system
);
10255 Vsjis_coding_system
= Qnil
;
10257 staticpro (&Vbig5_coding_system
);
10258 Vbig5_coding_system
= Qnil
;
10260 staticpro (&Vcode_conversion_reused_workbuf
);
10261 Vcode_conversion_reused_workbuf
= Qnil
;
10263 staticpro (&Vcode_conversion_workbuf_name
);
10264 Vcode_conversion_workbuf_name
= build_pure_c_string (" *code-conversion-work*");
10266 reused_workbuf_in_use
= 0;
10268 DEFSYM (Qcharset
, "charset");
10269 DEFSYM (Qtarget_idx
, "target-idx");
10270 DEFSYM (Qcoding_system_history
, "coding-system-history");
10271 Fset (Qcoding_system_history
, Qnil
);
10273 /* Target FILENAME is the first argument. */
10274 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10275 /* Target FILENAME is the third argument. */
10276 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10278 DEFSYM (Qcall_process
, "call-process");
10279 /* Target PROGRAM is the first argument. */
10280 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10282 DEFSYM (Qcall_process_region
, "call-process-region");
10283 /* Target PROGRAM is the third argument. */
10284 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10286 DEFSYM (Qstart_process
, "start-process");
10287 /* Target PROGRAM is the third argument. */
10288 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10290 DEFSYM (Qopen_network_stream
, "open-network-stream");
10291 /* Target SERVICE is the fourth argument. */
10292 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10294 DEFSYM (Qcoding_system
, "coding-system");
10295 DEFSYM (Qcoding_aliases
, "coding-aliases");
10297 DEFSYM (Qeol_type
, "eol-type");
10298 DEFSYM (Qunix
, "unix");
10299 DEFSYM (Qdos
, "dos");
10301 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10302 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10303 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10304 DEFSYM (Qdefault_char
, "default-char");
10305 DEFSYM (Qundecided
, "undecided");
10306 DEFSYM (Qno_conversion
, "no-conversion");
10307 DEFSYM (Qraw_text
, "raw-text");
10309 DEFSYM (Qiso_2022
, "iso-2022");
10311 DEFSYM (Qutf_8
, "utf-8");
10312 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10314 DEFSYM (Qutf_16
, "utf-16");
10315 DEFSYM (Qbig
, "big");
10316 DEFSYM (Qlittle
, "little");
10318 DEFSYM (Qshift_jis
, "shift-jis");
10319 DEFSYM (Qbig5
, "big5");
10321 DEFSYM (Qcoding_system_p
, "coding-system-p");
10323 DEFSYM (Qcoding_system_error
, "coding-system-error");
10324 Fput (Qcoding_system_error
, Qerror_conditions
,
10325 listn (CONSTYPE_PURE
, 2, Qcoding_system_error
, Qerror
));
10326 Fput (Qcoding_system_error
, Qerror_message
,
10327 build_pure_c_string ("Invalid coding system"));
10329 /* Intern this now in case it isn't already done.
10330 Setting this variable twice is harmless.
10331 But don't staticpro it here--that is done in alloc.c. */
10332 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10334 DEFSYM (Qtranslation_table
, "translation-table");
10335 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10336 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10337 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10338 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10340 DEFSYM (Qvalid_codes
, "valid-codes");
10342 DEFSYM (Qemacs_mule
, "emacs-mule");
10344 DEFSYM (QCcategory
, ":category");
10345 DEFSYM (QCmnemonic
, ":mnemonic");
10346 DEFSYM (QCdefault_char
, ":default-char");
10347 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10348 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10349 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10350 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10351 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10353 Vcoding_category_table
10354 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10355 staticpro (&Vcoding_category_table
);
10356 /* Followings are target of code detection. */
10357 ASET (Vcoding_category_table
, coding_category_iso_7
,
10358 intern_c_string ("coding-category-iso-7"));
10359 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10360 intern_c_string ("coding-category-iso-7-tight"));
10361 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10362 intern_c_string ("coding-category-iso-8-1"));
10363 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10364 intern_c_string ("coding-category-iso-8-2"));
10365 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10366 intern_c_string ("coding-category-iso-7-else"));
10367 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10368 intern_c_string ("coding-category-iso-8-else"));
10369 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10370 intern_c_string ("coding-category-utf-8-auto"));
10371 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10372 intern_c_string ("coding-category-utf-8"));
10373 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10374 intern_c_string ("coding-category-utf-8-sig"));
10375 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10376 intern_c_string ("coding-category-utf-16-be"));
10377 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10378 intern_c_string ("coding-category-utf-16-auto"));
10379 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10380 intern_c_string ("coding-category-utf-16-le"));
10381 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10382 intern_c_string ("coding-category-utf-16-be-nosig"));
10383 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10384 intern_c_string ("coding-category-utf-16-le-nosig"));
10385 ASET (Vcoding_category_table
, coding_category_charset
,
10386 intern_c_string ("coding-category-charset"));
10387 ASET (Vcoding_category_table
, coding_category_sjis
,
10388 intern_c_string ("coding-category-sjis"));
10389 ASET (Vcoding_category_table
, coding_category_big5
,
10390 intern_c_string ("coding-category-big5"));
10391 ASET (Vcoding_category_table
, coding_category_ccl
,
10392 intern_c_string ("coding-category-ccl"));
10393 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10394 intern_c_string ("coding-category-emacs-mule"));
10395 /* Followings are NOT target of code detection. */
10396 ASET (Vcoding_category_table
, coding_category_raw_text
,
10397 intern_c_string ("coding-category-raw-text"));
10398 ASET (Vcoding_category_table
, coding_category_undecided
,
10399 intern_c_string ("coding-category-undecided"));
10401 DEFSYM (Qinsufficient_source
, "insufficient-source");
10402 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10403 DEFSYM (Qinvalid_source
, "invalid-source");
10404 DEFSYM (Qinterrupted
, "interrupted");
10405 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10406 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10408 defsubr (&Scoding_system_p
);
10409 defsubr (&Sread_coding_system
);
10410 defsubr (&Sread_non_nil_coding_system
);
10411 defsubr (&Scheck_coding_system
);
10412 defsubr (&Sdetect_coding_region
);
10413 defsubr (&Sdetect_coding_string
);
10414 defsubr (&Sfind_coding_systems_region_internal
);
10415 defsubr (&Sunencodable_char_position
);
10416 defsubr (&Scheck_coding_systems_region
);
10417 defsubr (&Sdecode_coding_region
);
10418 defsubr (&Sencode_coding_region
);
10419 defsubr (&Sdecode_coding_string
);
10420 defsubr (&Sencode_coding_string
);
10421 defsubr (&Sdecode_sjis_char
);
10422 defsubr (&Sencode_sjis_char
);
10423 defsubr (&Sdecode_big5_char
);
10424 defsubr (&Sencode_big5_char
);
10425 defsubr (&Sset_terminal_coding_system_internal
);
10426 defsubr (&Sset_safe_terminal_coding_system_internal
);
10427 defsubr (&Sterminal_coding_system
);
10428 defsubr (&Sset_keyboard_coding_system_internal
);
10429 defsubr (&Skeyboard_coding_system
);
10430 defsubr (&Sfind_operation_coding_system
);
10431 defsubr (&Sset_coding_system_priority
);
10432 defsubr (&Sdefine_coding_system_internal
);
10433 defsubr (&Sdefine_coding_system_alias
);
10434 defsubr (&Scoding_system_put
);
10435 defsubr (&Scoding_system_base
);
10436 defsubr (&Scoding_system_plist
);
10437 defsubr (&Scoding_system_aliases
);
10438 defsubr (&Scoding_system_eol_type
);
10439 defsubr (&Scoding_system_priority_list
);
10441 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10442 doc
: /* List of coding systems.
10444 Do not alter the value of this variable manually. This variable should be
10445 updated by the functions `define-coding-system' and
10446 `define-coding-system-alias'. */);
10447 Vcoding_system_list
= Qnil
;
10449 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10450 doc
: /* Alist of coding system names.
10451 Each element is one element list of coding system name.
10452 This variable is given to `completing-read' as COLLECTION argument.
10454 Do not alter the value of this variable manually. This variable should be
10455 updated by the functions `make-coding-system' and
10456 `define-coding-system-alias'. */);
10457 Vcoding_system_alist
= Qnil
;
10459 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10460 doc
: /* List of coding-categories (symbols) ordered by priority.
10462 On detecting a coding system, Emacs tries code detection algorithms
10463 associated with each coding-category one by one in this order. When
10464 one algorithm agrees with a byte sequence of source text, the coding
10465 system bound to the corresponding coding-category is selected.
10467 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10471 Vcoding_category_list
= Qnil
;
10472 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10473 Vcoding_category_list
10474 = Fcons (AREF (Vcoding_category_table
, i
),
10475 Vcoding_category_list
);
10478 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10479 doc
: /* Specify the coding system for read operations.
10480 It is useful to bind this variable with `let', but do not set it globally.
10481 If the value is a coding system, it is used for decoding on read operation.
10482 If not, an appropriate element is used from one of the coding system alists.
10483 There are three such tables: `file-coding-system-alist',
10484 `process-coding-system-alist', and `network-coding-system-alist'. */);
10485 Vcoding_system_for_read
= Qnil
;
10487 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10488 doc
: /* Specify the coding system for write operations.
10489 Programs bind this variable with `let', but you should not set it globally.
10490 If the value is a coding system, it is used for encoding of output,
10491 when writing it to a file and when sending it to a file or subprocess.
10493 If this does not specify a coding system, an appropriate element
10494 is used from one of the coding system alists.
10495 There are three such tables: `file-coding-system-alist',
10496 `process-coding-system-alist', and `network-coding-system-alist'.
10497 For output to files, if the above procedure does not specify a coding system,
10498 the value of `buffer-file-coding-system' is used. */);
10499 Vcoding_system_for_write
= Qnil
;
10501 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10503 Coding system used in the latest file or process I/O. */);
10504 Vlast_coding_system_used
= Qnil
;
10506 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10508 Error status of the last code conversion.
10510 When an error was detected in the last code conversion, this variable
10511 is set to one of the following symbols.
10512 `insufficient-source'
10516 `insufficient-memory'
10517 When no error was detected, the value doesn't change. So, to check
10518 the error status of a code conversion by this variable, you must
10519 explicitly set this variable to nil before performing code
10521 Vlast_code_conversion_error
= Qnil
;
10523 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10525 *Non-nil means always inhibit code conversion of end-of-line format.
10526 See info node `Coding Systems' and info node `Text and Binary' concerning
10527 such conversion. */);
10528 inhibit_eol_conversion
= 0;
10530 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10532 Non-nil means process buffer inherits coding system of process output.
10533 Bind it to t if the process output is to be treated as if it were a file
10534 read from some filesystem. */);
10535 inherit_process_coding_system
= 0;
10537 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10539 Alist to decide a coding system to use for a file I/O operation.
10540 The format is ((PATTERN . VAL) ...),
10541 where PATTERN is a regular expression matching a file name,
10542 VAL is a coding system, a cons of coding systems, or a function symbol.
10543 If VAL is a coding system, it is used for both decoding and encoding
10545 If VAL is a cons of coding systems, the car part is used for decoding,
10546 and the cdr part is used for encoding.
10547 If VAL is a function symbol, the function must return a coding system
10548 or a cons of coding systems which are used as above. The function is
10549 called with an argument that is a list of the arguments with which
10550 `find-operation-coding-system' was called. If the function can't decide
10551 a coding system, it can return `undecided' so that the normal
10552 code-detection is performed.
10554 See also the function `find-operation-coding-system'
10555 and the variable `auto-coding-alist'. */);
10556 Vfile_coding_system_alist
= Qnil
;
10558 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10560 Alist to decide a coding system to use for a process I/O operation.
10561 The format is ((PATTERN . VAL) ...),
10562 where PATTERN is a regular expression matching a program name,
10563 VAL is a coding system, a cons of coding systems, or a function symbol.
10564 If VAL is a coding system, it is used for both decoding what received
10565 from the program and encoding what sent to the program.
10566 If VAL is a cons of coding systems, the car part is used for decoding,
10567 and the cdr part is used for encoding.
10568 If VAL is a function symbol, the function must return a coding system
10569 or a cons of coding systems which are used as above.
10571 See also the function `find-operation-coding-system'. */);
10572 Vprocess_coding_system_alist
= Qnil
;
10574 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10576 Alist to decide a coding system to use for a network I/O operation.
10577 The format is ((PATTERN . VAL) ...),
10578 where PATTERN is a regular expression matching a network service name
10579 or is a port number to connect to,
10580 VAL is a coding system, a cons of coding systems, or a function symbol.
10581 If VAL is a coding system, it is used for both decoding what received
10582 from the network stream and encoding what sent to the network stream.
10583 If VAL is a cons of coding systems, the car part is used for decoding,
10584 and the cdr part is used for encoding.
10585 If VAL is a function symbol, the function must return a coding system
10586 or a cons of coding systems which are used as above.
10588 See also the function `find-operation-coding-system'. */);
10589 Vnetwork_coding_system_alist
= Qnil
;
10591 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10592 doc
: /* Coding system to use with system messages.
10593 Also used for decoding keyboard input on X Window system. */);
10594 Vlocale_coding_system
= Qnil
;
10596 /* The eol mnemonics are reset in startup.el system-dependently. */
10597 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10599 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10600 eol_mnemonic_unix
= build_pure_c_string (":");
10602 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10604 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10605 eol_mnemonic_dos
= build_pure_c_string ("\\");
10607 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10609 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10610 eol_mnemonic_mac
= build_pure_c_string ("/");
10612 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10614 *String displayed in mode line when end-of-line format is not yet determined. */);
10615 eol_mnemonic_undecided
= build_pure_c_string (":");
10617 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10619 *Non-nil enables character translation while encoding and decoding. */);
10620 Venable_character_translation
= Qt
;
10622 DEFVAR_LISP ("standard-translation-table-for-decode",
10623 Vstandard_translation_table_for_decode
,
10624 doc
: /* Table for translating characters while decoding. */);
10625 Vstandard_translation_table_for_decode
= Qnil
;
10627 DEFVAR_LISP ("standard-translation-table-for-encode",
10628 Vstandard_translation_table_for_encode
,
10629 doc
: /* Table for translating characters while encoding. */);
10630 Vstandard_translation_table_for_encode
= Qnil
;
10632 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10633 doc
: /* Alist of charsets vs revision numbers.
10634 While encoding, if a charset (car part of an element) is found,
10635 designate it with the escape sequence identifying revision (cdr part
10636 of the element). */);
10637 Vcharset_revision_table
= Qnil
;
10639 DEFVAR_LISP ("default-process-coding-system",
10640 Vdefault_process_coding_system
,
10641 doc
: /* Cons of coding systems used for process I/O by default.
10642 The car part is used for decoding a process output,
10643 the cdr part is used for encoding a text to be sent to a process. */);
10644 Vdefault_process_coding_system
= Qnil
;
10646 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10648 Table of extra Latin codes in the range 128..159 (inclusive).
10649 This is a vector of length 256.
10650 If Nth element is non-nil, the existence of code N in a file
10651 \(or output of subprocess) doesn't prevent it to be detected as
10652 a coding system of ISO 2022 variant which has a flag
10653 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10654 or reading output of a subprocess.
10655 Only 128th through 159th elements have a meaning. */);
10656 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10658 DEFVAR_LISP ("select-safe-coding-system-function",
10659 Vselect_safe_coding_system_function
,
10661 Function to call to select safe coding system for encoding a text.
10663 If set, this function is called to force a user to select a proper
10664 coding system which can encode the text in the case that a default
10665 coding system used in each operation can't encode the text. The
10666 function should take care that the buffer is not modified while
10667 the coding system is being selected.
10669 The default value is `select-safe-coding-system' (which see). */);
10670 Vselect_safe_coding_system_function
= Qnil
;
10672 DEFVAR_BOOL ("coding-system-require-warning",
10673 coding_system_require_warning
,
10674 doc
: /* Internal use only.
10675 If non-nil, on writing a file, `select-safe-coding-system-function' is
10676 called even if `coding-system-for-write' is non-nil. The command
10677 `universal-coding-system-argument' binds this variable to t temporarily. */);
10678 coding_system_require_warning
= 0;
10681 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10682 inhibit_iso_escape_detection
,
10684 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10686 When Emacs reads text, it tries to detect how the text is encoded.
10687 This code detection is sensitive to escape sequences. If Emacs sees
10688 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10689 of the ISO2022 encodings, and decodes text by the corresponding coding
10690 system (e.g. `iso-2022-7bit').
10692 However, there may be a case that you want to read escape sequences in
10693 a file as is. In such a case, you can set this variable to non-nil.
10694 Then the code detection will ignore any escape sequences, and no text is
10695 detected as encoded in some ISO-2022 encoding. The result is that all
10696 escape sequences become visible in a buffer.
10698 The default value is nil, and it is strongly recommended not to change
10699 it. That is because many Emacs Lisp source files that contain
10700 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10701 in Emacs's distribution, and they won't be decoded correctly on
10702 reading if you suppress escape sequence detection.
10704 The other way to read escape sequences in a file without decoding is
10705 to explicitly specify some coding system that doesn't use ISO-2022
10706 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10707 inhibit_iso_escape_detection
= 0;
10709 DEFVAR_BOOL ("inhibit-null-byte-detection",
10710 inhibit_null_byte_detection
,
10711 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10712 By default, Emacs treats it as binary data, and does not attempt to
10713 decode it. The effect is as if you specified `no-conversion' for
10716 Set this to non-nil when a regular text happens to include null bytes.
10717 Examples are Index nodes of Info files and null-byte delimited output
10718 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10719 decode text as usual. */);
10720 inhibit_null_byte_detection
= 0;
10722 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10723 doc
: /* Char table for translating self-inserting characters.
10724 This is applied to the result of input methods, not their input.
10725 See also `keyboard-translate-table'.
10727 Use of this variable for character code unification was rendered
10728 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10729 internal character representation. */);
10730 Vtranslation_table_for_input
= Qnil
;
10733 Lisp_Object args
[coding_arg_max
];
10734 Lisp_Object plist
[16];
10737 for (i
= 0; i
< coding_arg_max
; i
++)
10740 plist
[0] = intern_c_string (":name");
10741 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10742 plist
[2] = intern_c_string (":mnemonic");
10743 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10744 plist
[4] = intern_c_string (":coding-type");
10745 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10746 plist
[6] = intern_c_string (":ascii-compatible-p");
10747 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10748 plist
[8] = intern_c_string (":default-char");
10749 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10750 plist
[10] = intern_c_string (":for-unibyte");
10751 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10752 plist
[12] = intern_c_string (":docstring");
10753 plist
[13] = build_pure_c_string ("Do no conversion.\n\
10755 When you visit a file with this coding, the file is read into a\n\
10756 unibyte buffer as is, thus each byte of a file is treated as a\n\
10758 plist
[14] = intern_c_string (":eol-type");
10759 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10760 args
[coding_arg_plist
] = Flist (16, plist
);
10761 Fdefine_coding_system_internal (coding_arg_max
, args
);
10763 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10764 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10765 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10766 /* This is already set.
10767 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10768 plist
[8] = intern_c_string (":charset-list");
10769 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10770 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10771 plist
[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10772 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10773 args
[coding_arg_plist
] = Flist (16, plist
);
10774 Fdefine_coding_system_internal (coding_arg_max
, args
);
10777 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10782 for (i
= 0; i
< coding_category_max
; i
++)
10783 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10785 #if defined (DOS_NT)
10786 system_eol_type
= Qdos
;
10788 system_eol_type
= Qunix
;
10790 staticpro (&system_eol_type
);
10794 emacs_strerror (int error_number
)
10798 synchronize_system_messages_locale ();
10799 str
= strerror (error_number
);
10801 if (! NILP (Vlocale_coding_system
))
10803 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10804 Vlocale_coding_system
,
10806 str
= SSDATA (dec
);