1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2011 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. In
59 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 1 if the byte sequence conforms to XXX, otherwise return 0.
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 int multibytep
= coding
->src_multibyte
;
162 int 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 int 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 int 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 int 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 ***/
292 #include "character.h"
295 #include "composite.h"
299 #include "termhooks.h"
301 Lisp_Object Vcoding_system_hash_table
;
303 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
304 Lisp_Object Qunix
, Qdos
;
305 Lisp_Object Qbuffer_file_coding_system
;
306 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
307 Lisp_Object Qdefault_char
;
308 Lisp_Object Qno_conversion
, Qundecided
;
309 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
310 Lisp_Object Qbig
, Qlittle
;
311 Lisp_Object Qcoding_system_history
;
312 Lisp_Object Qvalid_codes
;
313 Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
314 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
315 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
316 Lisp_Object QCascii_compatible_p
;
318 Lisp_Object Qcall_process
, Qcall_process_region
;
319 Lisp_Object Qstart_process
, Qopen_network_stream
;
320 Lisp_Object Qtarget_idx
;
322 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
323 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
325 /* If a symbol has this property, evaluate the value to define the
326 symbol as a coding system. */
327 static Lisp_Object Qcoding_system_define_form
;
329 /* Format of end-of-line decided by system. This is Qunix on
330 Unix and Mac, Qdos on DOS/Windows.
331 This has an effect only for external encoding (i.e. for output to
332 file and process), not for in-buffer or Lisp string encoding. */
333 static Lisp_Object system_eol_type
;
337 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
339 /* Coding system emacs-mule and raw-text are for converting only
340 end-of-line format. */
341 Lisp_Object Qemacs_mule
, Qraw_text
;
342 Lisp_Object Qutf_8_emacs
;
344 /* Coding-systems are handed between Emacs Lisp programs and C internal
345 routines by the following three variables. */
346 /* Coding system to be used to encode text for terminal display when
347 terminal coding system is nil. */
348 struct coding_system safe_terminal_coding
;
352 Lisp_Object Qtranslation_table
;
353 Lisp_Object Qtranslation_table_id
;
354 Lisp_Object Qtranslation_table_for_decode
;
355 Lisp_Object Qtranslation_table_for_encode
;
357 /* Two special coding systems. */
358 Lisp_Object Vsjis_coding_system
;
359 Lisp_Object Vbig5_coding_system
;
361 /* ISO2022 section */
363 #define CODING_ISO_INITIAL(coding, reg) \
364 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
365 coding_attr_iso_initial), \
369 #define CODING_ISO_REQUEST(coding, charset_id) \
370 (((charset_id) <= (coding)->max_charset_id \
371 ? ((coding)->safe_charsets[charset_id] != 255 \
372 ? (coding)->safe_charsets[charset_id] \
377 #define CODING_ISO_FLAGS(coding) \
378 ((coding)->spec.iso_2022.flags)
379 #define CODING_ISO_DESIGNATION(coding, reg) \
380 ((coding)->spec.iso_2022.current_designation[reg])
381 #define CODING_ISO_INVOCATION(coding, plane) \
382 ((coding)->spec.iso_2022.current_invocation[plane])
383 #define CODING_ISO_SINGLE_SHIFTING(coding) \
384 ((coding)->spec.iso_2022.single_shifting)
385 #define CODING_ISO_BOL(coding) \
386 ((coding)->spec.iso_2022.bol)
387 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
388 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
389 #define CODING_ISO_CMP_STATUS(coding) \
390 (&(coding)->spec.iso_2022.cmp_status)
391 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
392 ((coding)->spec.iso_2022.ctext_extended_segment_len)
393 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
394 ((coding)->spec.iso_2022.embedded_utf_8)
396 /* Control characters of ISO2022. */
397 /* code */ /* function */
398 #define ISO_CODE_LF 0x0A /* line-feed */
399 #define ISO_CODE_CR 0x0D /* carriage-return */
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 is
654 nonzero, 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 is nonzero 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 #define ONE_MORE_BYTE_NO_CHECK(c) \
727 if (multibytep && (c & 0x80)) \
729 if ((c & 0xFE) == 0xC0) \
730 c = ((c & 1) << 6) | *src++; \
734 c = - string_char (src, &src, NULL); \
735 record_conversion_result \
736 (coding, CODING_RESULT_INVALID_SRC); \
743 /* Store a byte C in the place pointed by DST and increment DST to the
744 next free point, and increment PRODUCED_CHARS. The caller should
745 assure that C is 0..127, and declare and set the variable `dst'
746 appropriately in advance.
750 #define EMIT_ONE_ASCII_BYTE(c) \
757 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
759 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
761 produced_chars += 2; \
762 *dst++ = (c1), *dst++ = (c2); \
766 /* Store a byte C in the place pointed by DST and increment DST to the
767 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
768 nonzero, store in an appropriate multibyte from. The caller should
769 declare and set the variables `dst' and `multibytep' appropriately
772 #define EMIT_ONE_BYTE(c) \
779 ch = BYTE8_TO_CHAR (ch); \
780 CHAR_STRING_ADVANCE (ch, dst); \
787 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
789 #define EMIT_TWO_BYTES(c1, c2) \
791 produced_chars += 2; \
798 ch = BYTE8_TO_CHAR (ch); \
799 CHAR_STRING_ADVANCE (ch, dst); \
802 ch = BYTE8_TO_CHAR (ch); \
803 CHAR_STRING_ADVANCE (ch, dst); \
813 #define EMIT_THREE_BYTES(c1, c2, c3) \
815 EMIT_ONE_BYTE (c1); \
816 EMIT_TWO_BYTES (c2, c3); \
820 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
822 EMIT_TWO_BYTES (c1, c2); \
823 EMIT_TWO_BYTES (c3, c4); \
827 /* Prototypes for static functions. */
828 static void record_conversion_result (struct coding_system
*coding
,
829 enum coding_result_code result
);
830 static int detect_coding_utf_8 (struct coding_system
*,
831 struct coding_detection_info
*info
);
832 static void decode_coding_utf_8 (struct coding_system
*);
833 static int encode_coding_utf_8 (struct coding_system
*);
835 static int detect_coding_utf_16 (struct coding_system
*,
836 struct coding_detection_info
*info
);
837 static void decode_coding_utf_16 (struct coding_system
*);
838 static int encode_coding_utf_16 (struct coding_system
*);
840 static int detect_coding_iso_2022 (struct coding_system
*,
841 struct coding_detection_info
*info
);
842 static void decode_coding_iso_2022 (struct coding_system
*);
843 static int encode_coding_iso_2022 (struct coding_system
*);
845 static int detect_coding_emacs_mule (struct coding_system
*,
846 struct coding_detection_info
*info
);
847 static void decode_coding_emacs_mule (struct coding_system
*);
848 static int encode_coding_emacs_mule (struct coding_system
*);
850 static int detect_coding_sjis (struct coding_system
*,
851 struct coding_detection_info
*info
);
852 static void decode_coding_sjis (struct coding_system
*);
853 static int encode_coding_sjis (struct coding_system
*);
855 static int detect_coding_big5 (struct coding_system
*,
856 struct coding_detection_info
*info
);
857 static void decode_coding_big5 (struct coding_system
*);
858 static int encode_coding_big5 (struct coding_system
*);
860 static int detect_coding_ccl (struct coding_system
*,
861 struct coding_detection_info
*info
);
862 static void decode_coding_ccl (struct coding_system
*);
863 static int encode_coding_ccl (struct coding_system
*);
865 static void decode_coding_raw_text (struct coding_system
*);
866 static int encode_coding_raw_text (struct coding_system
*);
868 static void coding_set_source (struct coding_system
*);
869 static void coding_set_destination (struct coding_system
*);
870 static void coding_alloc_by_realloc (struct coding_system
*, EMACS_INT
);
871 static void coding_alloc_by_making_gap (struct coding_system
*,
872 EMACS_INT
, EMACS_INT
);
873 static unsigned char *alloc_destination (struct coding_system
*,
874 EMACS_INT
, unsigned char *);
875 static void setup_iso_safe_charsets (Lisp_Object
);
876 static unsigned char *encode_designation_at_bol (struct coding_system
*,
879 static int detect_eol (const unsigned char *,
880 EMACS_INT
, enum coding_category
);
881 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*, int);
882 static void decode_eol (struct coding_system
*);
883 static Lisp_Object
get_translation_table (Lisp_Object
, int, int *);
884 static Lisp_Object
get_translation (Lisp_Object
, int *, int *);
885 static int produce_chars (struct coding_system
*, Lisp_Object
, int);
886 static INLINE
void produce_charset (struct coding_system
*, int *,
888 static void produce_annotation (struct coding_system
*, EMACS_INT
);
889 static int decode_coding (struct coding_system
*);
890 static INLINE
int *handle_composition_annotation (EMACS_INT
, EMACS_INT
,
891 struct coding_system
*,
893 static INLINE
int *handle_charset_annotation (EMACS_INT
, EMACS_INT
,
894 struct coding_system
*,
896 static void consume_chars (struct coding_system
*, Lisp_Object
, int);
897 static int encode_coding (struct coding_system
*);
898 static Lisp_Object
make_conversion_work_buffer (int);
899 static Lisp_Object
code_conversion_restore (Lisp_Object
);
900 static INLINE
int char_encodable_p (int, Lisp_Object
);
901 static Lisp_Object
make_subsidiaries (Lisp_Object
);
904 record_conversion_result (struct coding_system
*coding
,
905 enum coding_result_code result
)
907 coding
->result
= result
;
910 case CODING_RESULT_INSUFFICIENT_SRC
:
911 Vlast_code_conversion_error
= Qinsufficient_source
;
913 case CODING_RESULT_INCONSISTENT_EOL
:
914 Vlast_code_conversion_error
= Qinconsistent_eol
;
916 case CODING_RESULT_INVALID_SRC
:
917 Vlast_code_conversion_error
= Qinvalid_source
;
919 case CODING_RESULT_INTERRUPT
:
920 Vlast_code_conversion_error
= Qinterrupted
;
922 case CODING_RESULT_INSUFFICIENT_MEM
:
923 Vlast_code_conversion_error
= Qinsufficient_memory
;
925 case CODING_RESULT_INSUFFICIENT_DST
:
926 /* Don't record this error in Vlast_code_conversion_error
927 because it happens just temporarily and is resolved when the
928 whole conversion is finished. */
930 case CODING_RESULT_SUCCESS
:
933 Vlast_code_conversion_error
= intern ("Unknown error");
937 /* This wrapper macro is used to preserve validity of pointers into
938 buffer text across calls to decode_char, which could cause
939 relocation of buffers if it loads a charset map, because loading a
940 charset map allocates large structures. */
941 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
943 charset_map_loaded = 0; \
944 c = DECODE_CHAR (charset, code); \
945 if (charset_map_loaded) \
947 const unsigned char *orig = coding->source; \
950 coding_set_source (coding); \
951 offset = coding->source - orig; \
953 src_base += offset; \
959 /* If there are at least BYTES length of room at dst, allocate memory
960 for coding->destination and update dst and dst_end. We don't have
961 to take care of coding->source which will be relocated. It is
962 handled by calling coding_set_source in encode_coding. */
964 #define ASSURE_DESTINATION(bytes) \
966 if (dst + (bytes) >= dst_end) \
968 int more_bytes = charbuf_end - charbuf + (bytes); \
970 dst = alloc_destination (coding, more_bytes, dst); \
971 dst_end = coding->destination + coding->dst_bytes; \
976 /* Store multibyte form of the character C in P, and advance P to the
977 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
978 never calls MAYBE_UNIFY_CHAR. */
980 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
982 if ((c) <= MAX_1_BYTE_CHAR) \
984 else if ((c) <= MAX_2_BYTE_CHAR) \
985 *(p)++ = (0xC0 | ((c) >> 6)), \
986 *(p)++ = (0x80 | ((c) & 0x3F)); \
987 else if ((c) <= MAX_3_BYTE_CHAR) \
988 *(p)++ = (0xE0 | ((c) >> 12)), \
989 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
990 *(p)++ = (0x80 | ((c) & 0x3F)); \
991 else if ((c) <= MAX_4_BYTE_CHAR) \
992 *(p)++ = (0xF0 | (c >> 18)), \
993 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
994 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
995 *(p)++ = (0x80 | (c & 0x3F)); \
996 else if ((c) <= MAX_5_BYTE_CHAR) \
998 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
999 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1000 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1001 *(p)++ = (0x80 | (c & 0x3F)); \
1003 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1007 /* Return the character code of character whose multibyte form is at
1008 P, and advance P to the end of the multibyte form. This is like
1009 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1011 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1014 : ! ((p)[0] & 0x20) \
1016 ((((p)[-2] & 0x1F) << 6) \
1017 | ((p)[-1] & 0x3F) \
1018 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1019 : ! ((p)[0] & 0x10) \
1021 ((((p)[-3] & 0x0F) << 12) \
1022 | (((p)[-2] & 0x3F) << 6) \
1023 | ((p)[-1] & 0x3F))) \
1024 : ! ((p)[0] & 0x08) \
1026 ((((p)[-4] & 0xF) << 18) \
1027 | (((p)[-3] & 0x3F) << 12) \
1028 | (((p)[-2] & 0x3F) << 6) \
1029 | ((p)[-1] & 0x3F))) \
1031 ((((p)[-4] & 0x3F) << 18) \
1032 | (((p)[-3] & 0x3F) << 12) \
1033 | (((p)[-2] & 0x3F) << 6) \
1034 | ((p)[-1] & 0x3F))))
1038 coding_set_source (struct coding_system
*coding
)
1040 if (BUFFERP (coding
->src_object
))
1042 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1044 if (coding
->src_pos
< 0)
1045 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1047 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1049 else if (STRINGP (coding
->src_object
))
1051 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1054 /* Otherwise, the source is C string and is never relocated
1055 automatically. Thus we don't have to update anything. */
1060 coding_set_destination (struct coding_system
*coding
)
1062 if (BUFFERP (coding
->dst_object
))
1064 if (coding
->src_pos
< 0)
1066 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1067 coding
->dst_bytes
= (GAP_END_ADDR
1068 - (coding
->src_bytes
- coding
->consumed
)
1069 - coding
->destination
);
1073 /* We are sure that coding->dst_pos_byte is before the gap
1075 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1076 + coding
->dst_pos_byte
- BEG_BYTE
);
1077 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1078 - coding
->destination
);
1082 /* Otherwise, the destination is C string and is never relocated
1083 automatically. Thus we don't have to update anything. */
1089 coding_alloc_by_realloc (struct coding_system
*coding
, EMACS_INT bytes
)
1091 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1092 coding
->dst_bytes
+ bytes
);
1093 coding
->dst_bytes
+= bytes
;
1097 coding_alloc_by_making_gap (struct coding_system
*coding
,
1098 EMACS_INT gap_head_used
, EMACS_INT bytes
)
1100 if (EQ (coding
->src_object
, coding
->dst_object
))
1102 /* The gap may contain the produced data at the head and not-yet
1103 consumed data at the tail. To preserve those data, we at
1104 first make the gap size to zero, then increase the gap
1106 EMACS_INT add
= GAP_SIZE
;
1108 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1109 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1111 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1112 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1116 Lisp_Object this_buffer
;
1118 this_buffer
= Fcurrent_buffer ();
1119 set_buffer_internal (XBUFFER (coding
->dst_object
));
1121 set_buffer_internal (XBUFFER (this_buffer
));
1126 static unsigned char *
1127 alloc_destination (struct coding_system
*coding
, EMACS_INT nbytes
,
1130 EMACS_INT offset
= dst
- coding
->destination
;
1132 if (BUFFERP (coding
->dst_object
))
1134 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1136 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1139 coding_alloc_by_realloc (coding
, nbytes
);
1140 coding_set_destination (coding
);
1141 dst
= coding
->destination
+ offset
;
1145 /** Macros for annotations. */
1147 /* An annotation data is stored in the array coding->charbuf in this
1149 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1150 LENGTH is the number of elements in the annotation.
1151 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1152 NCHARS is the number of characters in the text annotated.
1154 The format of the following elements depend on ANNOTATION_MASK.
1156 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1158 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1160 NBYTES is the number of bytes specified in the header part of
1161 old-style emacs-mule encoding, or 0 for the other kind of
1164 METHOD is one of enum composition_method.
1166 Optional COMPOSITION-COMPONENTS are characters and composition
1169 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1172 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1173 recover from an invalid annotation, and should be skipped by
1174 produce_annotation. */
1176 /* Maximum length of the header of annotation data. */
1177 #define MAX_ANNOTATION_LENGTH 5
1179 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1181 *(buf)++ = -(len); \
1182 *(buf)++ = (mask); \
1183 *(buf)++ = (nchars); \
1184 coding->annotated = 1; \
1187 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1189 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1195 #define ADD_CHARSET_DATA(buf, nchars, id) \
1197 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1202 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1209 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1210 Check if a text is encoded in UTF-8. If it is, return 1, else
1213 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1214 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1215 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1216 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1217 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1218 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1220 #define UTF_BOM 0xFEFF
1221 #define UTF_8_BOM_1 0xEF
1222 #define UTF_8_BOM_2 0xBB
1223 #define UTF_8_BOM_3 0xBF
1226 detect_coding_utf_8 (struct coding_system
*coding
,
1227 struct coding_detection_info
*detect_info
)
1229 const unsigned char *src
= coding
->source
, *src_base
;
1230 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1231 int multibytep
= coding
->src_multibyte
;
1232 int consumed_chars
= 0;
1236 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1237 /* A coding system of this category is always ASCII compatible. */
1238 src
+= coding
->head_ascii
;
1242 int c
, c1
, c2
, c3
, c4
;
1246 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1249 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1251 if (UTF_8_2_OCTET_LEADING_P (c
))
1257 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1259 if (UTF_8_3_OCTET_LEADING_P (c
))
1262 if (src_base
== coding
->source
1263 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1268 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1270 if (UTF_8_4_OCTET_LEADING_P (c
))
1276 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1278 if (UTF_8_5_OCTET_LEADING_P (c
))
1285 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1289 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1291 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1296 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1297 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1301 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1303 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1310 decode_coding_utf_8 (struct coding_system
*coding
)
1312 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1313 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1314 const unsigned char *src_base
;
1315 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1316 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1317 int consumed_chars
= 0, consumed_chars_base
= 0;
1318 int multibytep
= coding
->src_multibyte
;
1319 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1320 Lisp_Object attr
, charset_list
;
1322 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1323 int byte_after_cr
= -1;
1325 CODING_GET_INFO (coding
, attr
, charset_list
);
1327 if (bom
!= utf_without_bom
)
1333 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1338 if (! UTF_8_EXTRA_OCTET_P (c2
))
1343 if (! UTF_8_EXTRA_OCTET_P (c3
))
1347 if ((c1
!= UTF_8_BOM_1
)
1348 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1351 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1356 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1360 int c
, c1
, c2
, c3
, c4
, c5
;
1363 consumed_chars_base
= consumed_chars
;
1365 if (charbuf
>= charbuf_end
)
1367 if (byte_after_cr
>= 0)
1372 if (byte_after_cr
>= 0)
1373 c1
= byte_after_cr
, byte_after_cr
= -1;
1380 else if (UTF_8_1_OCTET_P (c1
))
1382 if (eol_crlf
&& c1
== '\r')
1383 ONE_MORE_BYTE (byte_after_cr
);
1389 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1391 if (UTF_8_2_OCTET_LEADING_P (c1
))
1393 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1394 /* Reject overlong sequences here and below. Encoders
1395 producing them are incorrect, they can be misleading,
1396 and they mess up read/write invariance. */
1403 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1405 if (UTF_8_3_OCTET_LEADING_P (c1
))
1407 c
= (((c1
& 0xF) << 12)
1408 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1410 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1416 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1418 if (UTF_8_4_OCTET_LEADING_P (c1
))
1420 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1421 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1428 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1430 if (UTF_8_5_OCTET_LEADING_P (c1
))
1432 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1433 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1435 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1450 consumed_chars
= consumed_chars_base
;
1452 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1457 coding
->consumed_char
+= consumed_chars_base
;
1458 coding
->consumed
= src_base
- coding
->source
;
1459 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1464 encode_coding_utf_8 (struct coding_system
*coding
)
1466 int multibytep
= coding
->dst_multibyte
;
1467 int *charbuf
= coding
->charbuf
;
1468 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1469 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1470 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1471 int produced_chars
= 0;
1474 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1476 ASSURE_DESTINATION (3);
1477 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1478 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1483 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1485 while (charbuf
< charbuf_end
)
1487 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1489 ASSURE_DESTINATION (safe_room
);
1491 if (CHAR_BYTE8_P (c
))
1493 c
= CHAR_TO_BYTE8 (c
);
1498 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1499 for (p
= str
; p
< pend
; p
++)
1506 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1508 while (charbuf
< charbuf_end
)
1510 ASSURE_DESTINATION (safe_room
);
1512 if (CHAR_BYTE8_P (c
))
1513 *dst
++ = CHAR_TO_BYTE8 (c
);
1515 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1519 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1520 coding
->produced_char
+= produced_chars
;
1521 coding
->produced
= dst
- coding
->destination
;
1526 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1527 Check if a text is encoded in one of UTF-16 based coding systems.
1528 If it is, return 1, else return 0. */
1530 #define UTF_16_HIGH_SURROGATE_P(val) \
1531 (((val) & 0xFC00) == 0xD800)
1533 #define UTF_16_LOW_SURROGATE_P(val) \
1534 (((val) & 0xFC00) == 0xDC00)
1536 #define UTF_16_INVALID_P(val) \
1537 (((val) == 0xFFFE) \
1538 || ((val) == 0xFFFF) \
1539 || UTF_16_LOW_SURROGATE_P (val))
1543 detect_coding_utf_16 (struct coding_system
*coding
,
1544 struct coding_detection_info
*detect_info
)
1546 const unsigned char *src
= coding
->source
;
1547 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1548 int multibytep
= coding
->src_multibyte
;
1551 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1552 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1553 && (coding
->src_chars
& 1))
1555 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1559 TWO_MORE_BYTES (c1
, c2
);
1560 if ((c1
== 0xFF) && (c2
== 0xFE))
1562 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1563 | CATEGORY_MASK_UTF_16_AUTO
);
1564 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1565 | CATEGORY_MASK_UTF_16_BE_NOSIG
1566 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1568 else if ((c1
== 0xFE) && (c2
== 0xFF))
1570 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1571 | CATEGORY_MASK_UTF_16_AUTO
);
1572 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1573 | CATEGORY_MASK_UTF_16_BE_NOSIG
1574 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1578 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1583 /* We check the dispersion of Eth and Oth bytes where E is even and
1584 O is odd. If both are high, we assume binary data.*/
1585 unsigned char e
[256], o
[256];
1586 unsigned e_num
= 1, o_num
= 1;
1593 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1594 |CATEGORY_MASK_UTF_16_BE
1595 | CATEGORY_MASK_UTF_16_LE
);
1597 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1598 != CATEGORY_MASK_UTF_16
)
1600 TWO_MORE_BYTES (c1
, c2
);
1608 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1615 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1626 decode_coding_utf_16 (struct coding_system
*coding
)
1628 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1629 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1630 const unsigned char *src_base
;
1631 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1632 /* We may produces at most 3 chars in one loop. */
1633 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1634 int consumed_chars
= 0, consumed_chars_base
= 0;
1635 int multibytep
= coding
->src_multibyte
;
1636 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1637 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1638 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1639 Lisp_Object attr
, charset_list
;
1641 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1642 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1644 CODING_GET_INFO (coding
, attr
, charset_list
);
1646 if (bom
== utf_with_bom
)
1655 if (endian
== utf_16_big_endian
1656 ? c
!= 0xFEFF : c
!= 0xFFFE)
1658 /* The first two bytes are not BOM. Treat them as bytes
1659 for a normal character. */
1663 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1665 else if (bom
== utf_detect_bom
)
1667 /* We have already tried to detect BOM and failed in
1669 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1677 consumed_chars_base
= consumed_chars
;
1679 if (charbuf
>= charbuf_end
)
1681 if (byte_after_cr1
>= 0)
1686 if (byte_after_cr1
>= 0)
1687 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1695 if (byte_after_cr2
>= 0)
1696 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1701 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1705 c
= (endian
== utf_16_big_endian
1706 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1710 if (! UTF_16_LOW_SURROGATE_P (c
))
1712 if (endian
== utf_16_big_endian
)
1713 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1715 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1719 if (UTF_16_HIGH_SURROGATE_P (c
))
1720 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1726 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1727 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1728 *charbuf
++ = 0x10000 + c
;
1733 if (UTF_16_HIGH_SURROGATE_P (c
))
1734 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1737 if (eol_crlf
&& c
== '\r')
1739 ONE_MORE_BYTE (byte_after_cr1
);
1740 ONE_MORE_BYTE (byte_after_cr2
);
1748 coding
->consumed_char
+= consumed_chars_base
;
1749 coding
->consumed
= src_base
- coding
->source
;
1750 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1754 encode_coding_utf_16 (struct coding_system
*coding
)
1756 int multibytep
= coding
->dst_multibyte
;
1757 int *charbuf
= coding
->charbuf
;
1758 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1759 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1760 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1762 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1763 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1764 int produced_chars
= 0;
1765 Lisp_Object attrs
, charset_list
;
1768 CODING_GET_INFO (coding
, attrs
, charset_list
);
1770 if (bom
!= utf_without_bom
)
1772 ASSURE_DESTINATION (safe_room
);
1774 EMIT_TWO_BYTES (0xFE, 0xFF);
1776 EMIT_TWO_BYTES (0xFF, 0xFE);
1777 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1780 while (charbuf
< charbuf_end
)
1782 ASSURE_DESTINATION (safe_room
);
1784 if (c
> MAX_UNICODE_CHAR
)
1785 c
= coding
->default_char
;
1790 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1792 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1799 c1
= (c
>> 10) + 0xD800;
1800 c2
= (c
& 0x3FF) + 0xDC00;
1802 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1804 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1807 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1808 coding
->produced
= dst
- coding
->destination
;
1809 coding
->produced_char
+= produced_chars
;
1814 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1816 /* Emacs' internal format for representation of multiple character
1817 sets is a kind of multi-byte encoding, i.e. characters are
1818 represented by variable-length sequences of one-byte codes.
1820 ASCII characters and control characters (e.g. `tab', `newline') are
1821 represented by one-byte sequences which are their ASCII codes, in
1822 the range 0x00 through 0x7F.
1824 8-bit characters of the range 0x80..0x9F are represented by
1825 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1828 8-bit characters of the range 0xA0..0xFF are represented by
1829 one-byte sequences which are their 8-bit code.
1831 The other characters are represented by a sequence of `base
1832 leading-code', optional `extended leading-code', and one or two
1833 `position-code's. The length of the sequence is determined by the
1834 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1835 whereas extended leading-code and position-code take the range 0xA0
1836 through 0xFF. See `charset.h' for more details about leading-code
1839 --- CODE RANGE of Emacs' internal format ---
1843 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1844 eight-bit-graphic 0xA0..0xBF
1845 ELSE 0x81..0x9D + [0xA0..0xFF]+
1846 ---------------------------------------------
1848 As this is the internal character representation, the format is
1849 usually not used externally (i.e. in a file or in a data sent to a
1850 process). But, it is possible to have a text externally in this
1851 format (i.e. by encoding by the coding system `emacs-mule').
1853 In that case, a sequence of one-byte codes has a slightly different
1856 At first, all characters in eight-bit-control are represented by
1857 one-byte sequences which are their 8-bit code.
1859 Next, character composition data are represented by the byte
1860 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1862 METHOD is 0xF2 plus one of composition method (enum
1863 composition_method),
1865 BYTES is 0xA0 plus a byte length of this composition data,
1867 CHARS is 0xA0 plus a number of characters composed by this
1870 COMPONENTs are characters of multibyte form or composition
1871 rules encoded by two-byte of ASCII codes.
1873 In addition, for backward compatibility, the following formats are
1874 also recognized as composition data on decoding.
1877 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1880 MSEQ is a multibyte form but in these special format:
1881 ASCII: 0xA0 ASCII_CODE+0x80,
1882 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1883 RULE is a one byte code of the range 0xA0..0xF0 that
1884 represents a composition rule.
1887 char emacs_mule_bytes
[256];
1890 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1891 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1895 detect_coding_emacs_mule (struct coding_system
*coding
,
1896 struct coding_detection_info
*detect_info
)
1898 const unsigned char *src
= coding
->source
, *src_base
;
1899 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1900 int multibytep
= coding
->src_multibyte
;
1901 int consumed_chars
= 0;
1905 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1906 /* A coding system of this category is always ASCII compatible. */
1907 src
+= coding
->head_ascii
;
1917 /* Perhaps the start of composite character. We simply skip
1918 it because analyzing it is too heavy for detecting. But,
1919 at least, we check that the composite character
1920 constitutes of more than 4 bytes. */
1921 const unsigned char *src_base
;
1931 if (src
- src_base
<= 4)
1933 found
= CATEGORY_MASK_EMACS_MULE
;
1941 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1946 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1948 while (more_bytes
> 0)
1953 src
--; /* Unread the last byte. */
1958 if (more_bytes
!= 0)
1960 found
= CATEGORY_MASK_EMACS_MULE
;
1963 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1967 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1969 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1972 detect_info
->found
|= found
;
1977 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1978 character. If CMP_STATUS indicates that we must expect MSEQ or
1979 RULE described above, decode it and return the negative value of
1980 the decoded character or rule. If an invalid byte is found, return
1981 -1. If SRC is too short, return -2. */
1984 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1985 int *nbytes
, int *nchars
, int *id
,
1986 struct composition_status
*cmp_status
)
1988 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1989 const unsigned char *src_base
= src
;
1990 int multibytep
= coding
->src_multibyte
;
1994 int consumed_chars
= 0;
2001 charset_id
= emacs_mule_charset
[0];
2007 if (cmp_status
->state
!= COMPOSING_NO
2008 && cmp_status
->old_form
)
2010 if (cmp_status
->state
== COMPOSING_CHAR
)
2025 *nbytes
= src
- src_base
;
2026 *nchars
= consumed_chars
;
2034 switch (emacs_mule_bytes
[c
])
2037 if ((charset_id
= emacs_mule_charset
[c
]) < 0)
2046 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2047 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2050 if (c
< 0xA0 || (charset_id
= emacs_mule_charset
[c
]) < 0)
2059 if ((charset_id
= emacs_mule_charset
[c
]) < 0)
2064 code
= (c
& 0x7F) << 8;
2074 if (c
< 0 || (charset_id
= emacs_mule_charset
[c
]) < 0)
2079 code
= (c
& 0x7F) << 8;
2088 charset_id
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2094 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2095 CHARSET_FROM_ID (charset_id
), code
, c
);
2099 *nbytes
= src
- src_base
;
2100 *nchars
= consumed_chars
;
2103 return (mseq_found
? -c
: c
);
2113 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2115 /* Handle these composition sequence ('|': the end of header elements,
2116 BYTES and CHARS >= 0xA0):
2118 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2119 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2120 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2124 (4) relative composition: 0x80 | MSEQ ... MSEQ
2125 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2127 When the starter 0x80 and the following header elements are found,
2128 this annotation header is produced.
2130 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2132 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2133 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2135 Then, upon reading the following elements, these codes are produced
2136 until the composition end is found:
2139 (2) ALT ... ALT CHAR ... CHAR
2140 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2142 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2144 When the composition end is found, LENGTH and NCHARS in the
2145 annotation header is updated as below:
2147 (1) LENGTH: unchanged, NCHARS: unchanged
2148 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2149 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2150 (4) LENGTH: unchanged, NCHARS: number of CHARs
2151 (5) LENGTH: unchanged, NCHARS: number of CHARs
2153 If an error is found while composing, the annotation header is
2154 changed to the original composition header (plus filler -1s) as
2157 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2158 (5) [ 0x80 0xFF -1 -1- -1 ]
2160 and the sequence [ -2 DECODED-RULE ] is changed to the original
2161 byte sequence as below:
2162 o the original byte sequence is B: [ B -1 ]
2163 o the original byte sequence is B1 B2: [ B1 B2 ]
2165 Most of the routines are implemented by macros because many
2166 variables and labels in the caller decode_coding_emacs_mule must be
2167 accessible, and they are usually called just once (thus doesn't
2168 increase the size of compiled object). */
2170 /* Decode a composition rule represented by C as a component of
2171 composition sequence of Emacs 20 style. Set RULE to the decoded
2174 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2179 if (c < 0 || c >= 81) \
2180 goto invalid_code; \
2181 gref = c / 9, nref = c % 9; \
2182 if (gref == 4) gref = 10; \
2183 if (nref == 4) nref = 10; \
2184 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2188 /* Decode a composition rule represented by C and the following byte
2189 at SRC as a component of composition sequence of Emacs 21 style.
2190 Set RULE to the decoded rule. */
2192 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2197 if (gref < 0 || gref >= 81) \
2198 goto invalid_code; \
2199 ONE_MORE_BYTE (c); \
2201 if (nref < 0 || nref >= 81) \
2202 goto invalid_code; \
2203 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2207 /* Start of Emacs 21 style format. The first three bytes at SRC are
2208 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2209 byte length of this composition information, CHARS is the number of
2210 characters composed by this composition. */
2212 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2214 enum composition_method method = c - 0xF2; \
2215 int nbytes, nchars; \
2217 ONE_MORE_BYTE (c); \
2219 goto invalid_code; \
2220 nbytes = c - 0xA0; \
2221 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2222 goto invalid_code; \
2223 ONE_MORE_BYTE (c); \
2224 nchars = c - 0xA0; \
2225 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2226 goto invalid_code; \
2227 cmp_status->old_form = 0; \
2228 cmp_status->method = method; \
2229 if (method == COMPOSITION_RELATIVE) \
2230 cmp_status->state = COMPOSING_CHAR; \
2232 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2233 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2234 cmp_status->nchars = nchars; \
2235 cmp_status->ncomps = nbytes - 4; \
2236 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2240 /* Start of Emacs 20 style format for relative composition. */
2242 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2244 cmp_status->old_form = 1; \
2245 cmp_status->method = COMPOSITION_RELATIVE; \
2246 cmp_status->state = COMPOSING_CHAR; \
2247 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2248 cmp_status->nchars = cmp_status->ncomps = 0; \
2249 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2253 /* Start of Emacs 20 style format for rule-base composition. */
2255 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2257 cmp_status->old_form = 1; \
2258 cmp_status->method = COMPOSITION_WITH_RULE; \
2259 cmp_status->state = COMPOSING_CHAR; \
2260 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2261 cmp_status->nchars = cmp_status->ncomps = 0; \
2262 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2266 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2268 const unsigned char *current_src = src; \
2270 ONE_MORE_BYTE (c); \
2272 goto invalid_code; \
2273 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2274 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2275 DECODE_EMACS_MULE_21_COMPOSITION (); \
2276 else if (c < 0xA0) \
2277 goto invalid_code; \
2278 else if (c < 0xC0) \
2280 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2281 /* Re-read C as a composition component. */ \
2282 src = current_src; \
2284 else if (c == 0xFF) \
2285 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2287 goto invalid_code; \
2290 #define EMACS_MULE_COMPOSITION_END() \
2292 int idx = - cmp_status->length; \
2294 if (cmp_status->old_form) \
2295 charbuf[idx + 2] = cmp_status->nchars; \
2296 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2297 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2298 cmp_status->state = COMPOSING_NO; \
2303 emacs_mule_finish_composition (int *charbuf
,
2304 struct composition_status
*cmp_status
)
2306 int idx
= - cmp_status
->length
;
2309 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2311 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2313 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2314 && cmp_status
->state
== COMPOSING_CHAR
)
2316 /* The last rule was invalid. */
2317 int rule
= charbuf
[-1] + 0xA0;
2319 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2326 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2328 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2330 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2331 charbuf
[idx
++] = -3;
2337 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2338 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2340 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2341 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2342 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2343 charbuf
[idx
++] = -1;
2347 cmp_status
->state
= COMPOSING_NO
;
2351 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2353 if (cmp_status->state != COMPOSING_NO) \
2354 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2359 decode_coding_emacs_mule (struct coding_system
*coding
)
2361 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2362 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2363 const unsigned char *src_base
;
2364 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2365 /* We may produce two annotations (charset and composition) in one
2366 loop and one more charset annotation at the end. */
2368 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
2369 int consumed_chars
= 0, consumed_chars_base
;
2370 int multibytep
= coding
->src_multibyte
;
2371 Lisp_Object attrs
, charset_list
;
2372 int char_offset
= coding
->produced_char
;
2373 int last_offset
= char_offset
;
2374 int last_id
= charset_ascii
;
2376 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2377 int byte_after_cr
= -1;
2378 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2380 CODING_GET_INFO (coding
, attrs
, charset_list
);
2382 if (cmp_status
->state
!= COMPOSING_NO
)
2386 for (i
= 0; i
< cmp_status
->length
; i
++)
2387 *charbuf
++ = cmp_status
->carryover
[i
];
2388 coding
->annotated
= 1;
2396 consumed_chars_base
= consumed_chars
;
2398 if (charbuf
>= charbuf_end
)
2400 if (byte_after_cr
>= 0)
2405 if (byte_after_cr
>= 0)
2406 c
= byte_after_cr
, byte_after_cr
= -1;
2410 if (c
< 0 || c
== 0x80)
2412 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2419 DECODE_EMACS_MULE_COMPOSITION_START ();
2425 if (eol_crlf
&& c
== '\r')
2426 ONE_MORE_BYTE (byte_after_cr
);
2428 if (cmp_status
->state
!= COMPOSING_NO
)
2430 if (cmp_status
->old_form
)
2431 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2432 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2433 cmp_status
->ncomps
--;
2439 /* emacs_mule_char can load a charset map from a file, which
2440 allocates a large structure and might cause buffer text
2441 to be relocated as result. Thus, we need to remember the
2442 original pointer to buffer text, and fix up all related
2443 pointers after the call. */
2444 const unsigned char *orig
= coding
->source
;
2447 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2449 offset
= coding
->source
- orig
;
2463 src
= src_base
+ nbytes
;
2464 consumed_chars
= consumed_chars_base
+ nchars
;
2465 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2466 cmp_status
->ncomps
-= nchars
;
2469 /* Now if C >= 0, we found a normally encoded character, if C <
2470 0, we found an old-style composition component character or
2473 if (cmp_status
->state
== COMPOSING_NO
)
2477 if (last_id
!= charset_ascii
)
2478 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2481 last_offset
= char_offset
;
2486 else if (cmp_status
->state
== COMPOSING_CHAR
)
2488 if (cmp_status
->old_form
)
2492 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2499 cmp_status
->nchars
++;
2500 cmp_status
->length
++;
2501 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2502 EMACS_MULE_COMPOSITION_END ();
2503 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2504 cmp_status
->state
= COMPOSING_RULE
;
2510 cmp_status
->length
++;
2511 cmp_status
->nchars
--;
2512 if (cmp_status
->nchars
== 0)
2513 EMACS_MULE_COMPOSITION_END ();
2516 else if (cmp_status
->state
== COMPOSING_RULE
)
2522 EMACS_MULE_COMPOSITION_END ();
2529 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2534 cmp_status
->length
+= 2;
2535 cmp_status
->state
= COMPOSING_CHAR
;
2538 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2541 cmp_status
->length
++;
2542 if (cmp_status
->ncomps
== 0)
2543 cmp_status
->state
= COMPOSING_CHAR
;
2544 else if (cmp_status
->ncomps
> 0)
2546 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2547 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2550 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2552 else /* COMPOSING_COMPONENT_RULE */
2556 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2561 cmp_status
->length
+= 2;
2562 cmp_status
->ncomps
--;
2563 if (cmp_status
->ncomps
> 0)
2564 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2566 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2571 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2573 consumed_chars
= consumed_chars_base
;
2575 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2581 if (cmp_status
->state
!= COMPOSING_NO
)
2583 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2584 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2589 charbuf
-= cmp_status
->length
;
2590 for (i
= 0; i
< cmp_status
->length
; i
++)
2591 cmp_status
->carryover
[i
] = charbuf
[i
];
2594 if (last_id
!= charset_ascii
)
2595 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2596 coding
->consumed_char
+= consumed_chars_base
;
2597 coding
->consumed
= src_base
- coding
->source
;
2598 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2602 #define EMACS_MULE_LEADING_CODES(id, codes) \
2605 codes[0] = id, codes[1] = 0; \
2606 else if (id < 0xE0) \
2607 codes[0] = 0x9A, codes[1] = id; \
2608 else if (id < 0xF0) \
2609 codes[0] = 0x9B, codes[1] = id; \
2610 else if (id < 0xF5) \
2611 codes[0] = 0x9C, codes[1] = id; \
2613 codes[0] = 0x9D, codes[1] = id; \
2618 encode_coding_emacs_mule (struct coding_system
*coding
)
2620 int multibytep
= coding
->dst_multibyte
;
2621 int *charbuf
= coding
->charbuf
;
2622 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2623 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2624 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2626 int produced_chars
= 0;
2627 Lisp_Object attrs
, charset_list
;
2629 int preferred_charset_id
= -1;
2631 CODING_GET_INFO (coding
, attrs
, charset_list
);
2632 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2634 CODING_ATTR_CHARSET_LIST (attrs
)
2635 = charset_list
= Vemacs_mule_charset_list
;
2638 while (charbuf
< charbuf_end
)
2640 ASSURE_DESTINATION (safe_room
);
2645 /* Handle an annotation. */
2648 case CODING_ANNOTATE_COMPOSITION_MASK
:
2649 /* Not yet implemented. */
2651 case CODING_ANNOTATE_CHARSET_MASK
:
2652 preferred_charset_id
= charbuf
[3];
2653 if (preferred_charset_id
>= 0
2654 && NILP (Fmemq (make_number (preferred_charset_id
),
2656 preferred_charset_id
= -1;
2665 if (ASCII_CHAR_P (c
))
2666 EMIT_ONE_ASCII_BYTE (c
);
2667 else if (CHAR_BYTE8_P (c
))
2669 c
= CHAR_TO_BYTE8 (c
);
2674 struct charset
*charset
;
2678 unsigned char leading_codes
[2];
2680 if (preferred_charset_id
>= 0)
2682 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2683 if (CHAR_CHARSET_P (c
, charset
))
2684 code
= ENCODE_CHAR (charset
, c
);
2686 charset
= char_charset (c
, charset_list
, &code
);
2689 charset
= char_charset (c
, charset_list
, &code
);
2692 c
= coding
->default_char
;
2693 if (ASCII_CHAR_P (c
))
2695 EMIT_ONE_ASCII_BYTE (c
);
2698 charset
= char_charset (c
, charset_list
, &code
);
2700 dimension
= CHARSET_DIMENSION (charset
);
2701 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2702 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2703 EMIT_ONE_BYTE (leading_codes
[0]);
2704 if (leading_codes
[1])
2705 EMIT_ONE_BYTE (leading_codes
[1]);
2707 EMIT_ONE_BYTE (code
| 0x80);
2711 EMIT_ONE_BYTE (code
>> 8);
2712 EMIT_ONE_BYTE (code
& 0xFF);
2716 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2717 coding
->produced_char
+= produced_chars
;
2718 coding
->produced
= dst
- coding
->destination
;
2723 /*** 7. ISO2022 handlers ***/
2725 /* The following note describes the coding system ISO2022 briefly.
2726 Since the intention of this note is to help understand the
2727 functions in this file, some parts are NOT ACCURATE or are OVERLY
2728 SIMPLIFIED. For thorough understanding, please refer to the
2729 original document of ISO2022. This is equivalent to the standard
2730 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2732 ISO2022 provides many mechanisms to encode several character sets
2733 in 7-bit and 8-bit environments. For 7-bit environments, all text
2734 is encoded using bytes less than 128. This may make the encoded
2735 text a little bit longer, but the text passes more easily through
2736 several types of gateway, some of which strip off the MSB (Most
2739 There are two kinds of character sets: control character sets and
2740 graphic character sets. The former contain control characters such
2741 as `newline' and `escape' to provide control functions (control
2742 functions are also provided by escape sequences). The latter
2743 contain graphic characters such as 'A' and '-'. Emacs recognizes
2744 two control character sets and many graphic character sets.
2746 Graphic character sets are classified into one of the following
2747 four classes, according to the number of bytes (DIMENSION) and
2748 number of characters in one dimension (CHARS) of the set:
2749 - DIMENSION1_CHARS94
2750 - DIMENSION1_CHARS96
2751 - DIMENSION2_CHARS94
2752 - DIMENSION2_CHARS96
2754 In addition, each character set is assigned an identification tag,
2755 unique for each set, called the "final character" (denoted as <F>
2756 hereafter). The <F> of each character set is decided by ECMA(*)
2757 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2758 (0x30..0x3F are for private use only).
2760 Note (*): ECMA = European Computer Manufacturers Association
2762 Here are examples of graphic character sets [NAME(<F>)]:
2763 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2764 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2765 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2766 o DIMENSION2_CHARS96 -- none for the moment
2768 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2769 C0 [0x00..0x1F] -- control character plane 0
2770 GL [0x20..0x7F] -- graphic character plane 0
2771 C1 [0x80..0x9F] -- control character plane 1
2772 GR [0xA0..0xFF] -- graphic character plane 1
2774 A control character set is directly designated and invoked to C0 or
2775 C1 by an escape sequence. The most common case is that:
2776 - ISO646's control character set is designated/invoked to C0, and
2777 - ISO6429's control character set is designated/invoked to C1,
2778 and usually these designations/invocations are omitted in encoded
2779 text. In a 7-bit environment, only C0 can be used, and a control
2780 character for C1 is encoded by an appropriate escape sequence to
2781 fit into the environment. All control characters for C1 are
2782 defined to have corresponding escape sequences.
2784 A graphic character set is at first designated to one of four
2785 graphic registers (G0 through G3), then these graphic registers are
2786 invoked to GL or GR. These designations and invocations can be
2787 done independently. The most common case is that G0 is invoked to
2788 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2789 these invocations and designations are omitted in encoded text.
2790 In a 7-bit environment, only GL can be used.
2792 When a graphic character set of CHARS94 is invoked to GL, codes
2793 0x20 and 0x7F of the GL area work as control characters SPACE and
2794 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2797 There are two ways of invocation: locking-shift and single-shift.
2798 With locking-shift, the invocation lasts until the next different
2799 invocation, whereas with single-shift, the invocation affects the
2800 following character only and doesn't affect the locking-shift
2801 state. Invocations are done by the following control characters or
2804 ----------------------------------------------------------------------
2805 abbrev function cntrl escape seq description
2806 ----------------------------------------------------------------------
2807 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2808 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2809 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2810 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2811 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2812 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2813 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2814 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2815 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2816 ----------------------------------------------------------------------
2817 (*) These are not used by any known coding system.
2819 Control characters for these functions are defined by macros
2820 ISO_CODE_XXX in `coding.h'.
2822 Designations are done by the following escape sequences:
2823 ----------------------------------------------------------------------
2824 escape sequence description
2825 ----------------------------------------------------------------------
2826 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2827 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2828 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2829 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2830 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2831 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2832 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2833 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2834 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2835 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2836 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2837 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2838 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2839 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2840 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2841 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2842 ----------------------------------------------------------------------
2844 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2845 of dimension 1, chars 94, and final character <F>, etc...
2847 Note (*): Although these designations are not allowed in ISO2022,
2848 Emacs accepts them on decoding, and produces them on encoding
2849 CHARS96 character sets in a coding system which is characterized as
2850 7-bit environment, non-locking-shift, and non-single-shift.
2852 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2853 '(' must be omitted. We refer to this as "short-form" hereafter.
2855 Now you may notice that there are a lot of ways of encoding the
2856 same multilingual text in ISO2022. Actually, there exist many
2857 coding systems such as Compound Text (used in X11's inter client
2858 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2859 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2860 localized platforms), and all of these are variants of ISO2022.
2862 In addition to the above, Emacs handles two more kinds of escape
2863 sequences: ISO6429's direction specification and Emacs' private
2864 sequence for specifying character composition.
2866 ISO6429's direction specification takes the following form:
2867 o CSI ']' -- end of the current direction
2868 o CSI '0' ']' -- end of the current direction
2869 o CSI '1' ']' -- start of left-to-right text
2870 o CSI '2' ']' -- start of right-to-left text
2871 The control character CSI (0x9B: control sequence introducer) is
2872 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2874 Character composition specification takes the following form:
2875 o ESC '0' -- start relative composition
2876 o ESC '1' -- end composition
2877 o ESC '2' -- start rule-base composition (*)
2878 o ESC '3' -- start relative composition with alternate chars (**)
2879 o ESC '4' -- start rule-base composition with alternate chars (**)
2880 Since these are not standard escape sequences of any ISO standard,
2881 the use of them with these meanings is restricted to Emacs only.
2883 (*) This form is used only in Emacs 20.7 and older versions,
2884 but newer versions can safely decode it.
2885 (**) This form is used only in Emacs 21.1 and newer versions,
2886 and older versions can't decode it.
2888 Here's a list of example usages of these composition escape
2889 sequences (categorized by `enum composition_method').
2891 COMPOSITION_RELATIVE:
2892 ESC 0 CHAR [ CHAR ] ESC 1
2893 COMPOSITION_WITH_RULE:
2894 ESC 2 CHAR [ RULE CHAR ] ESC 1
2895 COMPOSITION_WITH_ALTCHARS:
2896 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2897 COMPOSITION_WITH_RULE_ALTCHARS:
2898 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2900 enum iso_code_class_type iso_code_class
[256];
2902 #define SAFE_CHARSET_P(coding, id) \
2903 ((id) <= (coding)->max_charset_id \
2904 && (coding)->safe_charsets[id] != 255)
2907 #define SHIFT_OUT_OK(category) \
2908 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2911 setup_iso_safe_charsets (Lisp_Object attrs
)
2913 Lisp_Object charset_list
, safe_charsets
;
2914 Lisp_Object request
;
2915 Lisp_Object reg_usage
;
2918 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2921 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2922 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2923 && ! EQ (charset_list
, Viso_2022_charset_list
))
2925 CODING_ATTR_CHARSET_LIST (attrs
)
2926 = charset_list
= Viso_2022_charset_list
;
2927 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2930 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2934 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2936 int id
= XINT (XCAR (tail
));
2937 if (max_charset_id
< id
)
2938 max_charset_id
= id
;
2941 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2942 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2943 request
= AREF (attrs
, coding_attr_iso_request
);
2944 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2945 reg94
= XINT (XCAR (reg_usage
));
2946 reg96
= XINT (XCDR (reg_usage
));
2948 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2952 struct charset
*charset
;
2955 charset
= CHARSET_FROM_ID (XINT (id
));
2956 reg
= Fcdr (Fassq (id
, request
));
2958 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2959 else if (charset
->iso_chars_96
)
2962 SSET (safe_charsets
, XINT (id
), reg96
);
2967 SSET (safe_charsets
, XINT (id
), reg94
);
2970 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2974 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2975 Check if a text is encoded in one of ISO-2022 based coding systems.
2976 If it is, return 1, else return 0. */
2979 detect_coding_iso_2022 (struct coding_system
*coding
,
2980 struct coding_detection_info
*detect_info
)
2982 const unsigned char *src
= coding
->source
, *src_base
= src
;
2983 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2984 int multibytep
= coding
->src_multibyte
;
2985 int single_shifting
= 0;
2988 int consumed_chars
= 0;
2992 int composition_count
= -1;
2994 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2996 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2998 struct coding_system
*this = &(coding_categories
[i
]);
2999 Lisp_Object attrs
, val
;
3003 attrs
= CODING_ID_ATTRS (this->id
);
3004 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
3005 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
3006 setup_iso_safe_charsets (attrs
);
3007 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
3008 this->max_charset_id
= SCHARS (val
) - 1;
3009 this->safe_charsets
= SDATA (val
);
3012 /* A coding system of this category is always ASCII compatible. */
3013 src
+= coding
->head_ascii
;
3015 while (rejected
!= CATEGORY_MASK_ISO
)
3022 if (inhibit_iso_escape_detection
)
3024 single_shifting
= 0;
3026 if (c
>= '(' && c
<= '/')
3028 /* Designation sequence for a charset of dimension 1. */
3030 if (c1
< ' ' || c1
>= 0x80
3031 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3032 /* Invalid designation sequence. Just ignore. */
3037 /* Designation sequence for a charset of dimension 2. */
3039 if (c
>= '@' && c
<= 'B')
3040 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3041 id
= iso_charset_table
[1][0][c
];
3042 else if (c
>= '(' && c
<= '/')
3045 if (c1
< ' ' || c1
>= 0x80
3046 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3047 /* Invalid designation sequence. Just ignore. */
3051 /* Invalid designation sequence. Just ignore it. */
3054 else if (c
== 'N' || c
== 'O')
3056 /* ESC <Fe> for SS2 or SS3. */
3057 single_shifting
= 1;
3058 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3063 /* End of composition. */
3064 if (composition_count
< 0
3065 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3068 composition_count
= -1;
3069 found
|= CATEGORY_MASK_ISO
;
3071 else if (c
>= '0' && c
<= '4')
3073 /* ESC <Fp> for start/end composition. */
3074 composition_count
= 0;
3079 /* Invalid escape sequence. Just ignore it. */
3083 /* We found a valid designation sequence for CHARSET. */
3084 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3085 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3087 found
|= CATEGORY_MASK_ISO_7
;
3089 rejected
|= CATEGORY_MASK_ISO_7
;
3090 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3092 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3094 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3095 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3097 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3099 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3100 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3102 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3104 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3109 /* Locking shift out/in. */
3110 if (inhibit_iso_escape_detection
)
3112 single_shifting
= 0;
3113 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3117 /* Control sequence introducer. */
3118 single_shifting
= 0;
3119 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3120 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3121 goto check_extra_latin
;
3126 if (inhibit_iso_escape_detection
)
3128 single_shifting
= 0;
3129 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3130 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3131 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3132 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
3133 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3134 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3135 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
3136 if (single_shifting
)
3138 goto check_extra_latin
;
3145 if (composition_count
>= 0)
3146 composition_count
++;
3147 single_shifting
= 0;
3152 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3153 found
|= CATEGORY_MASK_ISO_8_1
;
3154 /* Check the length of succeeding codes of the range
3155 0xA0..0FF. If the byte length is even, we include
3156 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3157 only when we are not single shifting. */
3158 if (! single_shifting
3159 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3162 while (src
< src_end
)
3174 if (i
& 1 && src
< src_end
)
3176 rejected
|= CATEGORY_MASK_ISO_8_2
;
3177 if (composition_count
>= 0)
3178 composition_count
+= i
;
3182 found
|= CATEGORY_MASK_ISO_8_2
;
3183 if (composition_count
>= 0)
3184 composition_count
+= i
/ 2;
3190 single_shifting
= 0;
3191 if (! VECTORP (Vlatin_extra_code_table
)
3192 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3194 rejected
= CATEGORY_MASK_ISO
;
3197 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3198 & CODING_ISO_FLAG_LATIN_EXTRA
)
3199 found
|= CATEGORY_MASK_ISO_8_1
;
3201 rejected
|= CATEGORY_MASK_ISO_8_1
;
3202 rejected
|= CATEGORY_MASK_ISO_8_2
;
3205 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3209 detect_info
->rejected
|= rejected
;
3210 detect_info
->found
|= (found
& ~rejected
);
3215 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3216 escape sequence should be kept. */
3217 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3221 if (final < '0' || final >= 128 \
3222 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3223 || !SAFE_CHARSET_P (coding, id)) \
3225 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3229 prev = CODING_ISO_DESIGNATION (coding, reg); \
3230 if (id == charset_jisx0201_roman) \
3232 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3233 id = charset_ascii; \
3235 else if (id == charset_jisx0208_1978) \
3237 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3238 id = charset_jisx0208; \
3240 CODING_ISO_DESIGNATION (coding, reg) = id; \
3241 /* If there was an invalid designation to REG previously, and this \
3242 designation is ASCII to REG, we should keep this designation \
3244 if (prev == -2 && id == charset_ascii) \
3249 /* Handle these composition sequence (ALT: alternate char):
3251 (1) relative composition: ESC 0 CHAR ... ESC 1
3252 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3253 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3254 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3256 When the start sequence (ESC 0/2/3/4) is found, this annotation
3259 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3261 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3262 produced until the end sequence (ESC 1) is found:
3265 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3266 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3267 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3269 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3270 annotation header is updated as below:
3272 (1) LENGTH: unchanged, NCHARS: number of CHARs
3273 (2) LENGTH: unchanged, NCHARS: number of CHARs
3274 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3275 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3277 If an error is found while composing, the annotation header is
3280 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3282 and the sequence [ -2 DECODED-RULE ] is changed to the original
3283 byte sequence as below:
3284 o the original byte sequence is B: [ B -1 ]
3285 o the original byte sequence is B1 B2: [ B1 B2 ]
3286 and the sequence [ -1 -1 ] is changed to the original byte
3291 /* Decode a composition rule C1 and maybe one more byte from the
3292 source, and set RULE to the encoded composition rule, NBYTES to the
3293 length of the composition rule. If the rule is invalid, set RULE
3294 to some negative value. */
3296 #define DECODE_COMPOSITION_RULE(rule, nbytes) \
3301 if (rule < 81) /* old format (before ver.21) */ \
3303 int gref = (rule) / 9; \
3304 int nref = (rule) % 9; \
3305 if (gref == 4) gref = 10; \
3306 if (nref == 4) nref = 10; \
3307 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3310 else /* new format (after ver.21) */ \
3314 ONE_MORE_BYTE (c); \
3315 rule = COMPOSITION_ENCODE_RULE (rule - 81, c - 32); \
3317 rule += 0x100; /* to destinguish it from the old format */ \
3322 #define ENCODE_COMPOSITION_RULE(rule) \
3324 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3326 if (rule < 0x100) /* old format */ \
3328 if (gref == 10) gref = 4; \
3329 if (nref == 10) nref = 4; \
3330 charbuf[idx] = 32 + gref * 9 + nref; \
3331 charbuf[idx + 1] = -1; \
3334 else /* new format */ \
3336 charbuf[idx] = 32 + 81 + gref; \
3337 charbuf[idx + 1] = 32 + nref; \
3342 /* Finish the current composition as invalid. */
3344 static int finish_composition (int *, struct composition_status
*);
3347 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3349 int idx
= - cmp_status
->length
;
3352 /* Recover the original ESC sequence */
3353 charbuf
[idx
++] = ISO_CODE_ESC
;
3354 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3355 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3356 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3357 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3359 charbuf
[idx
++] = -2;
3361 charbuf
[idx
++] = -1;
3362 new_chars
= cmp_status
->nchars
;
3363 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3364 for (; idx
< 0; idx
++)
3366 int elt
= charbuf
[idx
];
3370 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3375 charbuf
[idx
++] = ISO_CODE_ESC
;
3380 cmp_status
->state
= COMPOSING_NO
;
3384 /* If characters are under composition, finish the composition. */
3385 #define MAYBE_FINISH_COMPOSITION() \
3387 if (cmp_status->state != COMPOSING_NO) \
3388 char_offset += finish_composition (charbuf, cmp_status); \
3391 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3393 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3394 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3395 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3396 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3398 Produce this annotation sequence now:
3400 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3403 #define DECODE_COMPOSITION_START(c1) \
3406 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3407 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3408 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3409 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3413 cmp_status->state = COMPOSING_CHAR; \
3414 cmp_status->length += 2; \
3418 MAYBE_FINISH_COMPOSITION (); \
3419 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3420 : c1 == '2' ? COMPOSITION_WITH_RULE \
3421 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3422 : COMPOSITION_WITH_RULE_ALTCHARS); \
3424 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3425 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3426 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3427 cmp_status->nchars = cmp_status->ncomps = 0; \
3428 coding->annotated = 1; \
3433 /* Handle composition end sequence ESC 1. */
3435 #define DECODE_COMPOSITION_END() \
3437 if (cmp_status->nchars == 0 \
3438 || ((cmp_status->state == COMPOSING_CHAR) \
3439 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3441 MAYBE_FINISH_COMPOSITION (); \
3442 goto invalid_code; \
3444 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3445 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3446 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3447 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3448 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3449 char_offset += cmp_status->nchars; \
3450 cmp_status->state = COMPOSING_NO; \
3453 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3455 #define STORE_COMPOSITION_RULE(rule) \
3458 *charbuf++ = rule; \
3459 cmp_status->length += 2; \
3460 cmp_status->state--; \
3463 /* Store a composed char or a component char C in charbuf, and update
3466 #define STORE_COMPOSITION_CHAR(c) \
3469 cmp_status->length++; \
3470 if (cmp_status->state == COMPOSING_CHAR) \
3471 cmp_status->nchars++; \
3473 cmp_status->ncomps++; \
3474 if (cmp_status->method == COMPOSITION_WITH_RULE \
3475 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3476 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3477 cmp_status->state++; \
3481 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3484 decode_coding_iso_2022 (struct coding_system
*coding
)
3486 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3487 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3488 const unsigned char *src_base
;
3489 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3490 /* We may produce two annotations (charset and composition) in one
3491 loop and one more charset annotation at the end. */
3493 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3494 int consumed_chars
= 0, consumed_chars_base
;
3495 int multibytep
= coding
->src_multibyte
;
3496 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3497 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3498 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3499 int charset_id_2
, charset_id_3
;
3500 struct charset
*charset
;
3502 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3503 Lisp_Object attrs
, charset_list
;
3504 int char_offset
= coding
->produced_char
;
3505 int last_offset
= char_offset
;
3506 int last_id
= charset_ascii
;
3508 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3509 int byte_after_cr
= -1;
3512 CODING_GET_INFO (coding
, attrs
, charset_list
);
3513 setup_iso_safe_charsets (attrs
);
3514 /* Charset list may have been changed. */
3515 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3516 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3518 if (cmp_status
->state
!= COMPOSING_NO
)
3520 for (i
= 0; i
< cmp_status
->length
; i
++)
3521 *charbuf
++ = cmp_status
->carryover
[i
];
3522 coding
->annotated
= 1;
3530 consumed_chars_base
= consumed_chars
;
3532 if (charbuf
>= charbuf_end
)
3534 if (byte_after_cr
>= 0)
3539 if (byte_after_cr
>= 0)
3540 c1
= byte_after_cr
, byte_after_cr
= -1;
3546 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3548 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3550 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3554 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3556 if (c1
== ISO_CODE_ESC
)
3558 if (src
+ 1 >= src_end
)
3559 goto no_more_source
;
3560 *charbuf
++ = ISO_CODE_ESC
;
3562 if (src
[0] == '%' && src
[1] == '@')
3565 consumed_chars
+= 2;
3567 /* We are sure charbuf can contain two more chars. */
3570 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3575 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3581 if ((cmp_status
->state
== COMPOSING_RULE
3582 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3583 && c1
!= ISO_CODE_ESC
)
3587 DECODE_COMPOSITION_RULE (rule
, nbytes
);
3590 STORE_COMPOSITION_RULE (rule
);
3594 /* We produce at most one character. */
3595 switch (iso_code_class
[c1
])
3597 case ISO_0x20_or_0x7F
:
3598 if (charset_id_0
< 0
3599 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3600 /* This is SPACE or DEL. */
3601 charset
= CHARSET_FROM_ID (charset_ascii
);
3603 charset
= CHARSET_FROM_ID (charset_id_0
);
3606 case ISO_graphic_plane_0
:
3607 if (charset_id_0
< 0)
3608 charset
= CHARSET_FROM_ID (charset_ascii
);
3610 charset
= CHARSET_FROM_ID (charset_id_0
);
3613 case ISO_0xA0_or_0xFF
:
3614 if (charset_id_1
< 0
3615 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3616 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3618 /* This is a graphic character, we fall down ... */
3620 case ISO_graphic_plane_1
:
3621 if (charset_id_1
< 0)
3623 charset
= CHARSET_FROM_ID (charset_id_1
);
3627 if (eol_crlf
&& c1
== '\r')
3628 ONE_MORE_BYTE (byte_after_cr
);
3629 MAYBE_FINISH_COMPOSITION ();
3630 charset
= CHARSET_FROM_ID (charset_ascii
);
3637 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3638 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3640 CODING_ISO_INVOCATION (coding
, 0) = 1;
3641 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3645 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3647 CODING_ISO_INVOCATION (coding
, 0) = 0;
3648 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3651 case ISO_single_shift_2_7
:
3652 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3654 case ISO_single_shift_2
:
3655 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3657 /* SS2 is handled as an escape sequence of ESC 'N' */
3659 goto label_escape_sequence
;
3661 case ISO_single_shift_3
:
3662 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3664 /* SS2 is handled as an escape sequence of ESC 'O' */
3666 goto label_escape_sequence
;
3668 case ISO_control_sequence_introducer
:
3669 /* CSI is handled as an escape sequence of ESC '[' ... */
3671 goto label_escape_sequence
;
3675 label_escape_sequence
:
3676 /* Escape sequences handled here are invocation,
3677 designation, direction specification, and character
3678 composition specification. */
3681 case '&': /* revision of following character set */
3683 if (!(c1
>= '@' && c1
<= '~'))
3686 if (c1
!= ISO_CODE_ESC
)
3689 goto label_escape_sequence
;
3691 case '$': /* designation of 2-byte character set */
3692 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3698 if (c1
>= '@' && c1
<= 'B')
3699 { /* designation of JISX0208.1978, GB2312.1980,
3701 reg
= 0, chars96
= 0;
3703 else if (c1
>= 0x28 && c1
<= 0x2B)
3704 { /* designation of DIMENSION2_CHARS94 character set */
3705 reg
= c1
- 0x28, chars96
= 0;
3708 else if (c1
>= 0x2C && c1
<= 0x2F)
3709 { /* designation of DIMENSION2_CHARS96 character set */
3710 reg
= c1
- 0x2C, chars96
= 1;
3715 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3716 /* We must update these variables now. */
3718 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3720 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3726 case 'n': /* invocation of locking-shift-2 */
3727 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3728 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3730 CODING_ISO_INVOCATION (coding
, 0) = 2;
3731 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3734 case 'o': /* invocation of locking-shift-3 */
3735 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3736 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3738 CODING_ISO_INVOCATION (coding
, 0) = 3;
3739 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3742 case 'N': /* invocation of single-shift-2 */
3743 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3744 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3746 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3747 if (charset_id_2
< 0)
3748 charset
= CHARSET_FROM_ID (charset_ascii
);
3750 charset
= CHARSET_FROM_ID (charset_id_2
);
3752 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3756 case 'O': /* invocation of single-shift-3 */
3757 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3758 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3760 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3761 if (charset_id_3
< 0)
3762 charset
= CHARSET_FROM_ID (charset_ascii
);
3764 charset
= CHARSET_FROM_ID (charset_id_3
);
3766 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3770 case '0': case '2': case '3': case '4': /* start composition */
3771 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3773 if (last_id
!= charset_ascii
)
3775 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3776 last_id
= charset_ascii
;
3777 last_offset
= char_offset
;
3779 DECODE_COMPOSITION_START (c1
);
3782 case '1': /* end composition */
3783 if (cmp_status
->state
== COMPOSING_NO
)
3785 DECODE_COMPOSITION_END ();
3788 case '[': /* specification of direction */
3789 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3791 /* For the moment, nested direction is not supported.
3792 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3793 left-to-right, and nonzero means right-to-left. */
3797 case ']': /* end of the current direction */
3798 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3800 case '0': /* end of the current direction */
3801 case '1': /* start of left-to-right direction */
3804 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3809 case '2': /* start of right-to-left direction */
3812 coding
->mode
|= CODING_MODE_DIRECTION
;
3826 /* CTEXT extended segment:
3827 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3828 We keep these bytes as is for the moment.
3829 They may be decoded by post-read-conversion. */
3833 ONE_MORE_BYTE (dim
);
3834 if (dim
< '0' || dim
> '4')
3842 size
= ((M
- 128) * 128) + (L
- 128);
3843 if (charbuf
+ 6 > charbuf_end
)
3845 *charbuf
++ = ISO_CODE_ESC
;
3849 *charbuf
++ = BYTE8_TO_CHAR (M
);
3850 *charbuf
++ = BYTE8_TO_CHAR (L
);
3851 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3855 /* XFree86 extension for embedding UTF-8 in CTEXT:
3856 ESC % G --UTF-8-BYTES-- ESC % @
3857 We keep these bytes as is for the moment.
3858 They may be decoded by post-read-conversion. */
3859 if (charbuf
+ 3 > charbuf_end
)
3861 *charbuf
++ = ISO_CODE_ESC
;
3864 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3872 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3877 if (c1
>= 0x28 && c1
<= 0x2B)
3878 { /* designation of DIMENSION1_CHARS94 character set */
3879 reg
= c1
- 0x28, chars96
= 0;
3882 else if (c1
>= 0x2C && c1
<= 0x2F)
3883 { /* designation of DIMENSION1_CHARS96 character set */
3884 reg
= c1
- 0x2C, chars96
= 1;
3889 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3890 /* We must update these variables now. */
3892 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3894 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3902 if (cmp_status
->state
== COMPOSING_NO
3903 && charset
->id
!= charset_ascii
3904 && last_id
!= charset
->id
)
3906 if (last_id
!= charset_ascii
)
3907 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3908 last_id
= charset
->id
;
3909 last_offset
= char_offset
;
3912 /* Now we know CHARSET and 1st position code C1 of a character.
3913 Produce a decoded character while getting 2nd and 3rd
3914 position codes C2, C3 if necessary. */
3915 if (CHARSET_DIMENSION (charset
) > 1)
3918 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3919 || ((c1
& 0x80) != (c2
& 0x80)))
3920 /* C2 is not in a valid range. */
3922 if (CHARSET_DIMENSION (charset
) == 2)
3923 c1
= (c1
<< 8) | c2
;
3927 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3928 || ((c1
& 0x80) != (c3
& 0x80)))
3929 /* C3 is not in a valid range. */
3931 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3935 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3938 MAYBE_FINISH_COMPOSITION ();
3939 for (; src_base
< src
; src_base
++, char_offset
++)
3941 if (ASCII_BYTE_P (*src_base
))
3942 *charbuf
++ = *src_base
;
3944 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3947 else if (cmp_status
->state
== COMPOSING_NO
)
3952 else if ((cmp_status
->state
== COMPOSING_CHAR
3953 ? cmp_status
->nchars
3954 : cmp_status
->ncomps
)
3955 >= MAX_COMPOSITION_COMPONENTS
)
3957 /* Too long composition. */
3958 MAYBE_FINISH_COMPOSITION ();
3963 STORE_COMPOSITION_CHAR (c
);
3967 MAYBE_FINISH_COMPOSITION ();
3969 consumed_chars
= consumed_chars_base
;
3971 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3981 if (cmp_status
->state
!= COMPOSING_NO
)
3983 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3984 MAYBE_FINISH_COMPOSITION ();
3987 charbuf
-= cmp_status
->length
;
3988 for (i
= 0; i
< cmp_status
->length
; i
++)
3989 cmp_status
->carryover
[i
] = charbuf
[i
];
3992 else if (last_id
!= charset_ascii
)
3993 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3994 coding
->consumed_char
+= consumed_chars_base
;
3995 coding
->consumed
= src_base
- coding
->source
;
3996 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4000 /* ISO2022 encoding stuff. */
4003 It is not enough to say just "ISO2022" on encoding, we have to
4004 specify more details. In Emacs, each coding system of ISO2022
4005 variant has the following specifications:
4006 1. Initial designation to G0 thru G3.
4007 2. Allows short-form designation?
4008 3. ASCII should be designated to G0 before control characters?
4009 4. ASCII should be designated to G0 at end of line?
4010 5. 7-bit environment or 8-bit environment?
4011 6. Use locking-shift?
4012 7. Use Single-shift?
4013 And the following two are only for Japanese:
4014 8. Use ASCII in place of JIS0201-1976-Roman?
4015 9. Use JISX0208-1983 in place of JISX0208-1978?
4016 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4017 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4021 /* Produce codes (escape sequence) for designating CHARSET to graphic
4022 register REG at DST, and increment DST. If <final-char> of CHARSET is
4023 '@', 'A', or 'B' and the coding system CODING allows, produce
4024 designation sequence of short-form. */
4026 #define ENCODE_DESIGNATION(charset, reg, coding) \
4028 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4029 const char *intermediate_char_94 = "()*+"; \
4030 const char *intermediate_char_96 = ",-./"; \
4031 int revision = -1; \
4034 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4035 revision = CHARSET_ISO_REVISION (charset); \
4037 if (revision >= 0) \
4039 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4040 EMIT_ONE_BYTE ('@' + revision); \
4042 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4043 if (CHARSET_DIMENSION (charset) == 1) \
4045 if (! CHARSET_ISO_CHARS_96 (charset)) \
4046 c = intermediate_char_94[reg]; \
4048 c = intermediate_char_96[reg]; \
4049 EMIT_ONE_ASCII_BYTE (c); \
4053 EMIT_ONE_ASCII_BYTE ('$'); \
4054 if (! CHARSET_ISO_CHARS_96 (charset)) \
4056 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4058 || final_char < '@' || final_char > 'B') \
4059 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4062 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4064 EMIT_ONE_ASCII_BYTE (final_char); \
4066 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4070 /* The following two macros produce codes (control character or escape
4071 sequence) for ISO2022 single-shift functions (single-shift-2 and
4074 #define ENCODE_SINGLE_SHIFT_2 \
4076 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4077 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4079 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4080 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4084 #define ENCODE_SINGLE_SHIFT_3 \
4086 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4087 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4089 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4090 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4094 /* The following four macros produce codes (control character or
4095 escape sequence) for ISO2022 locking-shift functions (shift-in,
4096 shift-out, locking-shift-2, and locking-shift-3). */
4098 #define ENCODE_SHIFT_IN \
4100 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4101 CODING_ISO_INVOCATION (coding, 0) = 0; \
4105 #define ENCODE_SHIFT_OUT \
4107 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4108 CODING_ISO_INVOCATION (coding, 0) = 1; \
4112 #define ENCODE_LOCKING_SHIFT_2 \
4114 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4115 CODING_ISO_INVOCATION (coding, 0) = 2; \
4119 #define ENCODE_LOCKING_SHIFT_3 \
4121 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4122 CODING_ISO_INVOCATION (coding, 0) = 3; \
4126 /* Produce codes for a DIMENSION1 character whose character set is
4127 CHARSET and whose position-code is C1. Designation and invocation
4128 sequences are also produced in advance if necessary. */
4130 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4132 int id = CHARSET_ID (charset); \
4134 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4135 && id == charset_ascii) \
4137 id = charset_jisx0201_roman; \
4138 charset = CHARSET_FROM_ID (id); \
4141 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4143 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4144 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4146 EMIT_ONE_BYTE (c1 | 0x80); \
4147 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4150 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4152 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4155 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4157 EMIT_ONE_BYTE (c1 | 0x80); \
4161 /* Since CHARSET is not yet invoked to any graphic planes, we \
4162 must invoke it, or, at first, designate it to some graphic \
4163 register. Then repeat the loop to actually produce the \
4165 dst = encode_invocation_designation (charset, coding, dst, \
4170 /* Produce codes for a DIMENSION2 character whose character set is
4171 CHARSET and whose position-codes are C1 and C2. Designation and
4172 invocation codes are also produced in advance if necessary. */
4174 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4176 int id = CHARSET_ID (charset); \
4178 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4179 && id == charset_jisx0208) \
4181 id = charset_jisx0208_1978; \
4182 charset = CHARSET_FROM_ID (id); \
4185 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4187 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4188 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4190 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4191 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4194 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4196 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4199 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4201 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4205 /* Since CHARSET is not yet invoked to any graphic planes, we \
4206 must invoke it, or, at first, designate it to some graphic \
4207 register. Then repeat the loop to actually produce the \
4209 dst = encode_invocation_designation (charset, coding, dst, \
4214 #define ENCODE_ISO_CHARACTER(charset, c) \
4216 int code = ENCODE_CHAR ((charset), (c)); \
4218 if (CHARSET_DIMENSION (charset) == 1) \
4219 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4221 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4225 /* Produce designation and invocation codes at a place pointed by DST
4226 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4230 encode_invocation_designation (struct charset
*charset
,
4231 struct coding_system
*coding
,
4232 unsigned char *dst
, int *p_nchars
)
4234 int multibytep
= coding
->dst_multibyte
;
4235 int produced_chars
= *p_nchars
;
4236 int reg
; /* graphic register number */
4237 int id
= CHARSET_ID (charset
);
4239 /* At first, check designations. */
4240 for (reg
= 0; reg
< 4; reg
++)
4241 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4246 /* CHARSET is not yet designated to any graphic registers. */
4247 /* At first check the requested designation. */
4248 reg
= CODING_ISO_REQUEST (coding
, id
);
4250 /* Since CHARSET requests no special designation, designate it
4251 to graphic register 0. */
4254 ENCODE_DESIGNATION (charset
, reg
, coding
);
4257 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4258 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4260 /* Since the graphic register REG is not invoked to any graphic
4261 planes, invoke it to graphic plane 0. */
4264 case 0: /* graphic register 0 */
4268 case 1: /* graphic register 1 */
4272 case 2: /* graphic register 2 */
4273 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4274 ENCODE_SINGLE_SHIFT_2
;
4276 ENCODE_LOCKING_SHIFT_2
;
4279 case 3: /* graphic register 3 */
4280 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4281 ENCODE_SINGLE_SHIFT_3
;
4283 ENCODE_LOCKING_SHIFT_3
;
4288 *p_nchars
= produced_chars
;
4292 /* The following three macros produce codes for indicating direction
4294 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
4296 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
4297 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
4299 EMIT_ONE_BYTE (ISO_CODE_CSI); \
4303 #define ENCODE_DIRECTION_R2L() \
4305 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4306 EMIT_TWO_ASCII_BYTES ('2', ']'); \
4310 #define ENCODE_DIRECTION_L2R() \
4312 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4313 EMIT_TWO_ASCII_BYTES ('0', ']'); \
4317 /* Produce codes for designation and invocation to reset the graphic
4318 planes and registers to initial state. */
4319 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4322 struct charset *charset; \
4324 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4326 for (reg = 0; reg < 4; reg++) \
4327 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4328 && (CODING_ISO_DESIGNATION (coding, reg) \
4329 != CODING_ISO_INITIAL (coding, reg))) \
4331 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4332 ENCODE_DESIGNATION (charset, reg, coding); \
4337 /* Produce designation sequences of charsets in the line started from
4338 SRC to a place pointed by DST, and return updated DST.
4340 If the current block ends before any end-of-line, we may fail to
4341 find all the necessary designations. */
4343 static unsigned char *
4344 encode_designation_at_bol (struct coding_system
*coding
, int *charbuf
,
4345 int *charbuf_end
, unsigned char *dst
)
4347 struct charset
*charset
;
4348 /* Table of charsets to be designated to each graphic register. */
4350 int c
, found
= 0, reg
;
4351 int produced_chars
= 0;
4352 int multibytep
= coding
->dst_multibyte
;
4354 Lisp_Object charset_list
;
4356 attrs
= CODING_ID_ATTRS (coding
->id
);
4357 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4358 if (EQ (charset_list
, Qiso_2022
))
4359 charset_list
= Viso_2022_charset_list
;
4361 for (reg
= 0; reg
< 4; reg
++)
4371 charset
= char_charset (c
, charset_list
, NULL
);
4372 id
= CHARSET_ID (charset
);
4373 reg
= CODING_ISO_REQUEST (coding
, id
);
4374 if (reg
>= 0 && r
[reg
] < 0)
4383 for (reg
= 0; reg
< 4; reg
++)
4385 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4386 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4392 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4395 encode_coding_iso_2022 (struct coding_system
*coding
)
4397 int multibytep
= coding
->dst_multibyte
;
4398 int *charbuf
= coding
->charbuf
;
4399 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4400 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4401 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4404 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4405 && CODING_ISO_BOL (coding
));
4406 int produced_chars
= 0;
4407 Lisp_Object attrs
, eol_type
, charset_list
;
4408 int ascii_compatible
;
4410 int preferred_charset_id
= -1;
4412 CODING_GET_INFO (coding
, attrs
, charset_list
);
4413 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4414 if (VECTORP (eol_type
))
4417 setup_iso_safe_charsets (attrs
);
4418 /* Charset list may have been changed. */
4419 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4420 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4423 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4424 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4425 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4427 while (charbuf
< charbuf_end
)
4429 ASSURE_DESTINATION (safe_room
);
4431 if (bol_designation
)
4433 unsigned char *dst_prev
= dst
;
4435 /* We have to produce designation sequences if any now. */
4436 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
4437 bol_designation
= 0;
4438 /* We are sure that designation sequences are all ASCII bytes. */
4439 produced_chars
+= dst
- dst_prev
;
4446 /* Handle an annotation. */
4449 case CODING_ANNOTATE_COMPOSITION_MASK
:
4450 /* Not yet implemented. */
4452 case CODING_ANNOTATE_CHARSET_MASK
:
4453 preferred_charset_id
= charbuf
[2];
4454 if (preferred_charset_id
>= 0
4455 && NILP (Fmemq (make_number (preferred_charset_id
),
4457 preferred_charset_id
= -1;
4466 /* Now encode the character C. */
4467 if (c
< 0x20 || c
== 0x7F)
4470 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4472 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4473 ENCODE_RESET_PLANE_AND_REGISTER ();
4474 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4478 for (i
= 0; i
< 4; i
++)
4479 CODING_ISO_DESIGNATION (coding
, i
)
4480 = CODING_ISO_INITIAL (coding
, i
);
4483 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4485 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4486 ENCODE_RESET_PLANE_AND_REGISTER ();
4487 EMIT_ONE_ASCII_BYTE (c
);
4489 else if (ASCII_CHAR_P (c
))
4491 if (ascii_compatible
)
4492 EMIT_ONE_ASCII_BYTE (c
);
4495 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4496 ENCODE_ISO_CHARACTER (charset
, c
);
4499 else if (CHAR_BYTE8_P (c
))
4501 c
= CHAR_TO_BYTE8 (c
);
4506 struct charset
*charset
;
4508 if (preferred_charset_id
>= 0)
4510 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4511 if (! CHAR_CHARSET_P (c
, charset
))
4512 charset
= char_charset (c
, charset_list
, NULL
);
4515 charset
= char_charset (c
, charset_list
, NULL
);
4518 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4520 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4521 charset
= CHARSET_FROM_ID (charset_ascii
);
4525 c
= coding
->default_char
;
4526 charset
= char_charset (c
, charset_list
, NULL
);
4529 ENCODE_ISO_CHARACTER (charset
, c
);
4533 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4534 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4536 ASSURE_DESTINATION (safe_room
);
4537 ENCODE_RESET_PLANE_AND_REGISTER ();
4539 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4540 CODING_ISO_BOL (coding
) = bol_designation
;
4541 coding
->produced_char
+= produced_chars
;
4542 coding
->produced
= dst
- coding
->destination
;
4547 /*** 8,9. SJIS and BIG5 handlers ***/
4549 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4550 quite widely. So, for the moment, Emacs supports them in the bare
4551 C code. But, in the future, they may be supported only by CCL. */
4553 /* SJIS is a coding system encoding three character sets: ASCII, right
4554 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4555 as is. A character of charset katakana-jisx0201 is encoded by
4556 "position-code + 0x80". A character of charset japanese-jisx0208
4557 is encoded in 2-byte but two position-codes are divided and shifted
4558 so that it fit in the range below.
4560 --- CODE RANGE of SJIS ---
4561 (character set) (range)
4563 KATAKANA-JISX0201 0xA0 .. 0xDF
4564 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4565 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4566 -------------------------------
4570 /* BIG5 is a coding system encoding two character sets: ASCII and
4571 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4572 character set and is encoded in two-byte.
4574 --- CODE RANGE of BIG5 ---
4575 (character set) (range)
4577 Big5 (1st byte) 0xA1 .. 0xFE
4578 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4579 --------------------------
4583 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4584 Check if a text is encoded in SJIS. If it is, return
4585 CATEGORY_MASK_SJIS, else return 0. */
4588 detect_coding_sjis (struct coding_system
*coding
,
4589 struct coding_detection_info
*detect_info
)
4591 const unsigned char *src
= coding
->source
, *src_base
;
4592 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4593 int multibytep
= coding
->src_multibyte
;
4594 int consumed_chars
= 0;
4597 Lisp_Object attrs
, charset_list
;
4598 int max_first_byte_of_2_byte_code
;
4600 CODING_GET_INFO (coding
, attrs
, charset_list
);
4601 max_first_byte_of_2_byte_code
4602 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4604 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4605 /* A coding system of this category is always ASCII compatible. */
4606 src
+= coding
->head_ascii
;
4614 if ((c
>= 0x81 && c
<= 0x9F)
4615 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4618 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4620 found
= CATEGORY_MASK_SJIS
;
4622 else if (c
>= 0xA0 && c
< 0xE0)
4623 found
= CATEGORY_MASK_SJIS
;
4627 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4631 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4633 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4636 detect_info
->found
|= found
;
4640 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4641 Check if a text is encoded in BIG5. If it is, return
4642 CATEGORY_MASK_BIG5, else return 0. */
4645 detect_coding_big5 (struct coding_system
*coding
,
4646 struct coding_detection_info
*detect_info
)
4648 const unsigned char *src
= coding
->source
, *src_base
;
4649 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4650 int multibytep
= coding
->src_multibyte
;
4651 int consumed_chars
= 0;
4655 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4656 /* A coding system of this category is always ASCII compatible. */
4657 src
+= coding
->head_ascii
;
4668 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4670 found
= CATEGORY_MASK_BIG5
;
4675 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4679 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4681 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4684 detect_info
->found
|= found
;
4688 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4689 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4692 decode_coding_sjis (struct coding_system
*coding
)
4694 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4695 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4696 const unsigned char *src_base
;
4697 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4698 /* We may produce one charset annotation in one loop and one more at
4701 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4702 int consumed_chars
= 0, consumed_chars_base
;
4703 int multibytep
= coding
->src_multibyte
;
4704 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4705 struct charset
*charset_kanji2
;
4706 Lisp_Object attrs
, charset_list
, val
;
4707 int char_offset
= coding
->produced_char
;
4708 int last_offset
= char_offset
;
4709 int last_id
= charset_ascii
;
4711 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4712 int byte_after_cr
= -1;
4714 CODING_GET_INFO (coding
, attrs
, charset_list
);
4717 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4718 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4719 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4720 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4725 struct charset
*charset
;
4728 consumed_chars_base
= consumed_chars
;
4730 if (charbuf
>= charbuf_end
)
4732 if (byte_after_cr
>= 0)
4737 if (byte_after_cr
>= 0)
4738 c
= byte_after_cr
, byte_after_cr
= -1;
4745 if (eol_crlf
&& c
== '\r')
4746 ONE_MORE_BYTE (byte_after_cr
);
4747 charset
= charset_roman
;
4749 else if (c
== 0x80 || c
== 0xA0)
4751 else if (c
>= 0xA1 && c
<= 0xDF)
4753 /* SJIS -> JISX0201-Kana */
4755 charset
= charset_kana
;
4759 /* SJIS -> JISX0208 */
4761 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4765 charset
= charset_kanji
;
4767 else if (c
<= 0xFC && charset_kanji2
)
4769 /* SJIS -> JISX0213-2 */
4771 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4775 charset
= charset_kanji2
;
4779 if (charset
->id
!= charset_ascii
4780 && last_id
!= charset
->id
)
4782 if (last_id
!= charset_ascii
)
4783 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4784 last_id
= charset
->id
;
4785 last_offset
= char_offset
;
4787 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4794 consumed_chars
= consumed_chars_base
;
4796 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4802 if (last_id
!= charset_ascii
)
4803 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4804 coding
->consumed_char
+= consumed_chars_base
;
4805 coding
->consumed
= src_base
- coding
->source
;
4806 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4810 decode_coding_big5 (struct coding_system
*coding
)
4812 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4813 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4814 const unsigned char *src_base
;
4815 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4816 /* We may produce one charset annotation in one loop and one more at
4819 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4820 int consumed_chars
= 0, consumed_chars_base
;
4821 int multibytep
= coding
->src_multibyte
;
4822 struct charset
*charset_roman
, *charset_big5
;
4823 Lisp_Object attrs
, charset_list
, val
;
4824 int char_offset
= coding
->produced_char
;
4825 int last_offset
= char_offset
;
4826 int last_id
= charset_ascii
;
4828 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4829 int byte_after_cr
= -1;
4831 CODING_GET_INFO (coding
, attrs
, charset_list
);
4833 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4834 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4839 struct charset
*charset
;
4842 consumed_chars_base
= consumed_chars
;
4844 if (charbuf
>= charbuf_end
)
4846 if (byte_after_cr
>= 0)
4851 if (byte_after_cr
>= 0)
4852 c
= byte_after_cr
, byte_after_cr
= -1;
4860 if (eol_crlf
&& c
== '\r')
4861 ONE_MORE_BYTE (byte_after_cr
);
4862 charset
= charset_roman
;
4867 if (c
< 0xA1 || c
> 0xFE)
4870 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4873 charset
= charset_big5
;
4875 if (charset
->id
!= charset_ascii
4876 && last_id
!= charset
->id
)
4878 if (last_id
!= charset_ascii
)
4879 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4880 last_id
= charset
->id
;
4881 last_offset
= char_offset
;
4883 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4890 consumed_chars
= consumed_chars_base
;
4892 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4898 if (last_id
!= charset_ascii
)
4899 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4900 coding
->consumed_char
+= consumed_chars_base
;
4901 coding
->consumed
= src_base
- coding
->source
;
4902 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4905 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4906 This function can encode charsets `ascii', `katakana-jisx0201',
4907 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4908 are sure that all these charsets are registered as official charset
4909 (i.e. do not have extended leading-codes). Characters of other
4910 charsets are produced without any encoding. If SJIS_P is 1, encode
4911 SJIS text, else encode BIG5 text. */
4914 encode_coding_sjis (struct coding_system
*coding
)
4916 int multibytep
= coding
->dst_multibyte
;
4917 int *charbuf
= coding
->charbuf
;
4918 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4919 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4920 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4922 int produced_chars
= 0;
4923 Lisp_Object attrs
, charset_list
, val
;
4924 int ascii_compatible
;
4925 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4926 struct charset
*charset_kanji2
;
4929 CODING_GET_INFO (coding
, attrs
, charset_list
);
4931 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4932 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4933 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4934 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4936 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4938 while (charbuf
< charbuf_end
)
4940 ASSURE_DESTINATION (safe_room
);
4942 /* Now encode the character C. */
4943 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4944 EMIT_ONE_ASCII_BYTE (c
);
4945 else if (CHAR_BYTE8_P (c
))
4947 c
= CHAR_TO_BYTE8 (c
);
4953 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4957 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4959 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4960 charset
= CHARSET_FROM_ID (charset_ascii
);
4964 c
= coding
->default_char
;
4965 charset
= char_charset (c
, charset_list
, &code
);
4968 if (code
== CHARSET_INVALID_CODE (charset
))
4970 if (charset
== charset_kanji
)
4974 c1
= code
>> 8, c2
= code
& 0xFF;
4975 EMIT_TWO_BYTES (c1
, c2
);
4977 else if (charset
== charset_kana
)
4978 EMIT_ONE_BYTE (code
| 0x80);
4979 else if (charset_kanji2
&& charset
== charset_kanji2
)
4984 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4986 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4988 JIS_TO_SJIS2 (code
);
4989 c1
= code
>> 8, c2
= code
& 0xFF;
4990 EMIT_TWO_BYTES (c1
, c2
);
4993 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4996 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4999 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5000 coding
->produced_char
+= produced_chars
;
5001 coding
->produced
= dst
- coding
->destination
;
5006 encode_coding_big5 (struct coding_system
*coding
)
5008 int multibytep
= coding
->dst_multibyte
;
5009 int *charbuf
= coding
->charbuf
;
5010 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5011 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5012 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5014 int produced_chars
= 0;
5015 Lisp_Object attrs
, charset_list
, val
;
5016 int ascii_compatible
;
5017 struct charset
*charset_roman
, *charset_big5
;
5020 CODING_GET_INFO (coding
, attrs
, charset_list
);
5022 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5023 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5024 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5026 while (charbuf
< charbuf_end
)
5028 ASSURE_DESTINATION (safe_room
);
5030 /* Now encode the character C. */
5031 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5032 EMIT_ONE_ASCII_BYTE (c
);
5033 else if (CHAR_BYTE8_P (c
))
5035 c
= CHAR_TO_BYTE8 (c
);
5041 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
5045 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5047 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5048 charset
= CHARSET_FROM_ID (charset_ascii
);
5052 c
= coding
->default_char
;
5053 charset
= char_charset (c
, charset_list
, &code
);
5056 if (code
== CHARSET_INVALID_CODE (charset
))
5058 if (charset
== charset_big5
)
5062 c1
= code
>> 8, c2
= code
& 0xFF;
5063 EMIT_TWO_BYTES (c1
, c2
);
5066 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5069 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5070 coding
->produced_char
+= produced_chars
;
5071 coding
->produced
= dst
- coding
->destination
;
5076 /*** 10. CCL handlers ***/
5078 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5079 Check if a text is encoded in a coding system of which
5080 encoder/decoder are written in CCL program. If it is, return
5081 CATEGORY_MASK_CCL, else return 0. */
5084 detect_coding_ccl (struct coding_system
*coding
,
5085 struct coding_detection_info
*detect_info
)
5087 const unsigned char *src
= coding
->source
, *src_base
;
5088 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5089 int multibytep
= coding
->src_multibyte
;
5090 int consumed_chars
= 0;
5092 unsigned char *valids
;
5093 int head_ascii
= coding
->head_ascii
;
5096 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5098 coding
= &coding_categories
[coding_category_ccl
];
5099 valids
= CODING_CCL_VALIDS (coding
);
5100 attrs
= CODING_ID_ATTRS (coding
->id
);
5101 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5110 if (c
< 0 || ! valids
[c
])
5112 if ((valids
[c
] > 1))
5113 found
= CATEGORY_MASK_CCL
;
5115 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5119 detect_info
->found
|= found
;
5124 decode_coding_ccl (struct coding_system
*coding
)
5126 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5127 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5128 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5129 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5130 int consumed_chars
= 0;
5131 int multibytep
= coding
->src_multibyte
;
5132 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5133 int source_charbuf
[1024];
5134 int source_byteidx
[1025];
5135 Lisp_Object attrs
, charset_list
;
5137 CODING_GET_INFO (coding
, attrs
, charset_list
);
5141 const unsigned char *p
= src
;
5146 while (i
< 1024 && p
< src_end
)
5148 source_byteidx
[i
] = p
- src
;
5149 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5151 source_byteidx
[i
] = p
- src
;
5154 while (i
< 1024 && p
< src_end
)
5155 source_charbuf
[i
++] = *p
++;
5157 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5158 ccl
->last_block
= 1;
5159 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5161 charbuf
+= ccl
->produced
;
5163 src
+= source_byteidx
[ccl
->consumed
];
5165 src
+= ccl
->consumed
;
5166 consumed_chars
+= ccl
->consumed
;
5167 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5171 switch (ccl
->status
)
5173 case CCL_STAT_SUSPEND_BY_SRC
:
5174 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5176 case CCL_STAT_SUSPEND_BY_DST
:
5177 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5180 case CCL_STAT_INVALID_CMD
:
5181 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5184 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5187 coding
->consumed_char
+= consumed_chars
;
5188 coding
->consumed
= src
- coding
->source
;
5189 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5193 encode_coding_ccl (struct coding_system
*coding
)
5195 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5196 int multibytep
= coding
->dst_multibyte
;
5197 int *charbuf
= coding
->charbuf
;
5198 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5199 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5200 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5201 int destination_charbuf
[1024];
5202 int i
, produced_chars
= 0;
5203 Lisp_Object attrs
, charset_list
;
5205 CODING_GET_INFO (coding
, attrs
, charset_list
);
5206 if (coding
->consumed_char
== coding
->src_chars
5207 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5208 ccl
->last_block
= 1;
5210 while (charbuf
< charbuf_end
)
5212 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5213 charbuf_end
- charbuf
, 1024, charset_list
);
5216 ASSURE_DESTINATION (ccl
->produced
* 2);
5217 for (i
= 0; i
< ccl
->produced
; i
++)
5218 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5222 ASSURE_DESTINATION (ccl
->produced
);
5223 for (i
= 0; i
< ccl
->produced
; i
++)
5224 *dst
++ = destination_charbuf
[i
] & 0xFF;
5225 produced_chars
+= ccl
->produced
;
5227 charbuf
+= ccl
->consumed
;
5228 if (ccl
->status
== CCL_STAT_QUIT
5229 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5233 switch (ccl
->status
)
5235 case CCL_STAT_SUSPEND_BY_SRC
:
5236 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5238 case CCL_STAT_SUSPEND_BY_DST
:
5239 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5242 case CCL_STAT_INVALID_CMD
:
5243 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5246 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5250 coding
->produced_char
+= produced_chars
;
5251 coding
->produced
= dst
- coding
->destination
;
5257 /*** 10, 11. no-conversion handlers ***/
5259 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5262 decode_coding_raw_text (struct coding_system
*coding
)
5265 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5267 coding
->chars_at_source
= 1;
5268 coding
->consumed_char
= coding
->src_chars
;
5269 coding
->consumed
= coding
->src_bytes
;
5270 if (eol_crlf
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5272 coding
->consumed_char
--;
5274 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5277 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5281 encode_coding_raw_text (struct coding_system
*coding
)
5283 int multibytep
= coding
->dst_multibyte
;
5284 int *charbuf
= coding
->charbuf
;
5285 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5286 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5287 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5288 int produced_chars
= 0;
5293 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5295 if (coding
->src_multibyte
)
5296 while (charbuf
< charbuf_end
)
5298 ASSURE_DESTINATION (safe_room
);
5300 if (ASCII_CHAR_P (c
))
5301 EMIT_ONE_ASCII_BYTE (c
);
5302 else if (CHAR_BYTE8_P (c
))
5304 c
= CHAR_TO_BYTE8 (c
);
5309 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5311 CHAR_STRING_ADVANCE (c
, p1
);
5314 EMIT_ONE_BYTE (*p0
);
5320 while (charbuf
< charbuf_end
)
5322 ASSURE_DESTINATION (safe_room
);
5329 if (coding
->src_multibyte
)
5331 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5333 while (charbuf
< charbuf_end
)
5335 ASSURE_DESTINATION (safe_room
);
5337 if (ASCII_CHAR_P (c
))
5339 else if (CHAR_BYTE8_P (c
))
5340 *dst
++ = CHAR_TO_BYTE8 (c
);
5342 CHAR_STRING_ADVANCE (c
, dst
);
5347 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5348 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5349 *dst
++ = *charbuf
++;
5351 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5353 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5354 coding
->produced_char
+= produced_chars
;
5355 coding
->produced
= dst
- coding
->destination
;
5359 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5360 Check if a text is encoded in a charset-based coding system. If it
5361 is, return 1, else return 0. */
5364 detect_coding_charset (struct coding_system
*coding
,
5365 struct coding_detection_info
*detect_info
)
5367 const unsigned char *src
= coding
->source
, *src_base
;
5368 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5369 int multibytep
= coding
->src_multibyte
;
5370 int consumed_chars
= 0;
5371 Lisp_Object attrs
, valids
, name
;
5373 int head_ascii
= coding
->head_ascii
;
5374 int check_latin_extra
= 0;
5376 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5378 coding
= &coding_categories
[coding_category_charset
];
5379 attrs
= CODING_ID_ATTRS (coding
->id
);
5380 valids
= AREF (attrs
, coding_attr_charset_valids
);
5381 name
= CODING_ID_NAME (coding
->id
);
5382 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5383 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5384 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5385 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5386 check_latin_extra
= 1;
5388 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5395 struct charset
*charset
;
5402 val
= AREF (valids
, c
);
5408 && check_latin_extra
5409 && (!VECTORP (Vlatin_extra_code_table
)
5410 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5412 found
= CATEGORY_MASK_CHARSET
;
5416 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5417 dim
= CHARSET_DIMENSION (charset
);
5418 for (idx
= 1; idx
< dim
; idx
++)
5423 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5424 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5433 for (; CONSP (val
); val
= XCDR (val
))
5435 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5436 dim
= CHARSET_DIMENSION (charset
);
5442 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5443 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5458 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5462 detect_info
->found
|= found
;
5467 decode_coding_charset (struct coding_system
*coding
)
5469 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5470 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5471 const unsigned char *src_base
;
5472 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5473 /* We may produce one charset annotation in one loop and one more at
5476 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5477 int consumed_chars
= 0, consumed_chars_base
;
5478 int multibytep
= coding
->src_multibyte
;
5479 Lisp_Object attrs
, charset_list
, valids
;
5480 int char_offset
= coding
->produced_char
;
5481 int last_offset
= char_offset
;
5482 int last_id
= charset_ascii
;
5484 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5485 int byte_after_cr
= -1;
5487 CODING_GET_INFO (coding
, attrs
, charset_list
);
5488 valids
= AREF (attrs
, coding_attr_charset_valids
);
5494 struct charset
*charset
;
5500 consumed_chars_base
= consumed_chars
;
5502 if (charbuf
>= charbuf_end
)
5504 if (byte_after_cr
>= 0)
5509 if (byte_after_cr
>= 0)
5517 if (eol_crlf
&& c
== '\r')
5518 ONE_MORE_BYTE (byte_after_cr
);
5524 val
= AREF (valids
, c
);
5525 if (! INTEGERP (val
) && ! CONSP (val
))
5529 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5530 dim
= CHARSET_DIMENSION (charset
);
5534 code
= (code
<< 8) | c
;
5537 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5542 /* VAL is a list of charset IDs. It is assured that the
5543 list is sorted by charset dimensions (smaller one
5547 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5548 dim
= CHARSET_DIMENSION (charset
);
5552 code
= (code
<< 8) | c
;
5555 CODING_DECODE_CHAR (coding
, src
, src_base
,
5556 src_end
, charset
, code
, c
);
5564 if (charset
->id
!= charset_ascii
5565 && last_id
!= charset
->id
)
5567 if (last_id
!= charset_ascii
)
5568 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5569 last_id
= charset
->id
;
5570 last_offset
= char_offset
;
5579 consumed_chars
= consumed_chars_base
;
5581 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5587 if (last_id
!= charset_ascii
)
5588 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5589 coding
->consumed_char
+= consumed_chars_base
;
5590 coding
->consumed
= src_base
- coding
->source
;
5591 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5595 encode_coding_charset (struct coding_system
*coding
)
5597 int multibytep
= coding
->dst_multibyte
;
5598 int *charbuf
= coding
->charbuf
;
5599 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5600 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5601 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5602 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5603 int produced_chars
= 0;
5604 Lisp_Object attrs
, charset_list
;
5605 int ascii_compatible
;
5608 CODING_GET_INFO (coding
, attrs
, charset_list
);
5609 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5611 while (charbuf
< charbuf_end
)
5613 struct charset
*charset
;
5616 ASSURE_DESTINATION (safe_room
);
5618 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5619 EMIT_ONE_ASCII_BYTE (c
);
5620 else if (CHAR_BYTE8_P (c
))
5622 c
= CHAR_TO_BYTE8 (c
);
5627 charset
= char_charset (c
, charset_list
, &code
);
5630 if (CHARSET_DIMENSION (charset
) == 1)
5631 EMIT_ONE_BYTE (code
);
5632 else if (CHARSET_DIMENSION (charset
) == 2)
5633 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5634 else if (CHARSET_DIMENSION (charset
) == 3)
5635 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5637 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5638 (code
>> 8) & 0xFF, code
& 0xFF);
5642 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5643 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5645 c
= coding
->default_char
;
5651 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5652 coding
->produced_char
+= produced_chars
;
5653 coding
->produced
= dst
- coding
->destination
;
5658 /*** 7. C library functions ***/
5660 /* Setup coding context CODING from information about CODING_SYSTEM.
5661 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5662 CODING_SYSTEM is invalid, signal an error. */
5665 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5668 Lisp_Object eol_type
;
5669 Lisp_Object coding_type
;
5672 if (NILP (coding_system
))
5673 coding_system
= Qundecided
;
5675 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5677 attrs
= CODING_ID_ATTRS (coding
->id
);
5678 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5681 coding
->head_ascii
= -1;
5682 if (VECTORP (eol_type
))
5683 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5684 | CODING_REQUIRE_DETECTION_MASK
);
5685 else if (! EQ (eol_type
, Qunix
))
5686 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5687 | CODING_REQUIRE_ENCODING_MASK
);
5689 coding
->common_flags
= 0;
5690 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5691 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5692 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5693 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5694 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5695 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5697 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5698 coding
->max_charset_id
= SCHARS (val
) - 1;
5699 coding
->safe_charsets
= SDATA (val
);
5700 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5701 coding
->carryover_bytes
= 0;
5703 coding_type
= CODING_ATTR_TYPE (attrs
);
5704 if (EQ (coding_type
, Qundecided
))
5706 coding
->detector
= NULL
;
5707 coding
->decoder
= decode_coding_raw_text
;
5708 coding
->encoder
= encode_coding_raw_text
;
5709 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5711 else if (EQ (coding_type
, Qiso_2022
))
5714 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5716 /* Invoke graphic register 0 to plane 0. */
5717 CODING_ISO_INVOCATION (coding
, 0) = 0;
5718 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5719 CODING_ISO_INVOCATION (coding
, 1)
5720 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5721 /* Setup the initial status of designation. */
5722 for (i
= 0; i
< 4; i
++)
5723 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5724 /* Not single shifting initially. */
5725 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5726 /* Beginning of buffer should also be regarded as bol. */
5727 CODING_ISO_BOL (coding
) = 1;
5728 coding
->detector
= detect_coding_iso_2022
;
5729 coding
->decoder
= decode_coding_iso_2022
;
5730 coding
->encoder
= encode_coding_iso_2022
;
5731 if (flags
& CODING_ISO_FLAG_SAFE
)
5732 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5733 coding
->common_flags
5734 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5735 | CODING_REQUIRE_FLUSHING_MASK
);
5736 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5737 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5738 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5739 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5740 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5742 setup_iso_safe_charsets (attrs
);
5743 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5744 coding
->max_charset_id
= SCHARS (val
) - 1;
5745 coding
->safe_charsets
= SDATA (val
);
5747 CODING_ISO_FLAGS (coding
) = flags
;
5748 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5749 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5750 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5751 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5753 else if (EQ (coding_type
, Qcharset
))
5755 coding
->detector
= detect_coding_charset
;
5756 coding
->decoder
= decode_coding_charset
;
5757 coding
->encoder
= encode_coding_charset
;
5758 coding
->common_flags
5759 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5761 else if (EQ (coding_type
, Qutf_8
))
5763 val
= AREF (attrs
, coding_attr_utf_bom
);
5764 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5765 : EQ (val
, Qt
) ? utf_with_bom
5767 coding
->detector
= detect_coding_utf_8
;
5768 coding
->decoder
= decode_coding_utf_8
;
5769 coding
->encoder
= encode_coding_utf_8
;
5770 coding
->common_flags
5771 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5772 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5773 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5775 else if (EQ (coding_type
, Qutf_16
))
5777 val
= AREF (attrs
, coding_attr_utf_bom
);
5778 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5779 : EQ (val
, Qt
) ? utf_with_bom
5781 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5782 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5783 : utf_16_little_endian
);
5784 CODING_UTF_16_SURROGATE (coding
) = 0;
5785 coding
->detector
= detect_coding_utf_16
;
5786 coding
->decoder
= decode_coding_utf_16
;
5787 coding
->encoder
= encode_coding_utf_16
;
5788 coding
->common_flags
5789 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5790 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5791 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5793 else if (EQ (coding_type
, Qccl
))
5795 coding
->detector
= detect_coding_ccl
;
5796 coding
->decoder
= decode_coding_ccl
;
5797 coding
->encoder
= encode_coding_ccl
;
5798 coding
->common_flags
5799 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5800 | CODING_REQUIRE_FLUSHING_MASK
);
5802 else if (EQ (coding_type
, Qemacs_mule
))
5804 coding
->detector
= detect_coding_emacs_mule
;
5805 coding
->decoder
= decode_coding_emacs_mule
;
5806 coding
->encoder
= encode_coding_emacs_mule
;
5807 coding
->common_flags
5808 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5809 coding
->spec
.emacs_mule
.full_support
= 1;
5810 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5811 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5813 Lisp_Object tail
, safe_charsets
;
5814 int max_charset_id
= 0;
5816 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5818 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5819 max_charset_id
= XFASTINT (XCAR (tail
));
5820 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5821 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5822 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5824 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5825 coding
->max_charset_id
= max_charset_id
;
5826 coding
->safe_charsets
= SDATA (safe_charsets
);
5827 coding
->spec
.emacs_mule
.full_support
= 1;
5829 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5830 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5832 else if (EQ (coding_type
, Qshift_jis
))
5834 coding
->detector
= detect_coding_sjis
;
5835 coding
->decoder
= decode_coding_sjis
;
5836 coding
->encoder
= encode_coding_sjis
;
5837 coding
->common_flags
5838 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5840 else if (EQ (coding_type
, Qbig5
))
5842 coding
->detector
= detect_coding_big5
;
5843 coding
->decoder
= decode_coding_big5
;
5844 coding
->encoder
= encode_coding_big5
;
5845 coding
->common_flags
5846 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5848 else /* EQ (coding_type, Qraw_text) */
5850 coding
->detector
= NULL
;
5851 coding
->decoder
= decode_coding_raw_text
;
5852 coding
->encoder
= encode_coding_raw_text
;
5853 if (! EQ (eol_type
, Qunix
))
5855 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5856 if (! VECTORP (eol_type
))
5857 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5865 /* Return a list of charsets supported by CODING. */
5868 coding_charset_list (struct coding_system
*coding
)
5870 Lisp_Object attrs
, charset_list
;
5872 CODING_GET_INFO (coding
, attrs
, charset_list
);
5873 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5875 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5877 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5878 charset_list
= Viso_2022_charset_list
;
5880 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5882 charset_list
= Vemacs_mule_charset_list
;
5884 return charset_list
;
5888 /* Return a list of charsets supported by CODING-SYSTEM. */
5891 coding_system_charset_list (Lisp_Object coding_system
)
5894 Lisp_Object attrs
, charset_list
;
5896 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5897 attrs
= CODING_ID_ATTRS (id
);
5899 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5901 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5903 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5904 charset_list
= Viso_2022_charset_list
;
5906 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5908 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5910 charset_list
= Vemacs_mule_charset_list
;
5914 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5916 return charset_list
;
5920 /* Return raw-text or one of its subsidiaries that has the same
5921 eol_type as CODING-SYSTEM. */
5924 raw_text_coding_system (Lisp_Object coding_system
)
5926 Lisp_Object spec
, attrs
;
5927 Lisp_Object eol_type
, raw_text_eol_type
;
5929 if (NILP (coding_system
))
5931 spec
= CODING_SYSTEM_SPEC (coding_system
);
5932 attrs
= AREF (spec
, 0);
5934 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5935 return coding_system
;
5937 eol_type
= AREF (spec
, 2);
5938 if (VECTORP (eol_type
))
5940 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5941 raw_text_eol_type
= AREF (spec
, 2);
5942 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5943 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5944 : AREF (raw_text_eol_type
, 2));
5948 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5949 the subsidiary that has the same eol-spec as PARENT (if it is not
5950 nil and specifies end-of-line format) or the system's setting
5951 (system_eol_type). */
5954 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5956 Lisp_Object spec
, eol_type
;
5958 if (NILP (coding_system
))
5959 coding_system
= Qraw_text
;
5960 spec
= CODING_SYSTEM_SPEC (coding_system
);
5961 eol_type
= AREF (spec
, 2);
5962 if (VECTORP (eol_type
))
5964 Lisp_Object parent_eol_type
;
5966 if (! NILP (parent
))
5968 Lisp_Object parent_spec
;
5970 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5971 parent_eol_type
= AREF (parent_spec
, 2);
5972 if (VECTORP (parent_eol_type
))
5973 parent_eol_type
= system_eol_type
;
5976 parent_eol_type
= system_eol_type
;
5977 if (EQ (parent_eol_type
, Qunix
))
5978 coding_system
= AREF (eol_type
, 0);
5979 else if (EQ (parent_eol_type
, Qdos
))
5980 coding_system
= AREF (eol_type
, 1);
5981 else if (EQ (parent_eol_type
, Qmac
))
5982 coding_system
= AREF (eol_type
, 2);
5984 return coding_system
;
5988 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5989 decided for writing to a process. If not, complement them, and
5990 return a new coding system. */
5993 complement_process_encoding_system (Lisp_Object coding_system
)
5995 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5996 Lisp_Object spec
, attrs
;
5999 for (i
= 0; i
< 3; i
++)
6002 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
6004 coding_system
= preferred_coding_system ();
6005 spec
= CODING_SYSTEM_SPEC (coding_system
);
6008 attrs
= AREF (spec
, 0);
6009 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
6010 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
6011 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
6012 eol_base
= coding_system
;
6013 if (! NILP (coding_base
) && ! NILP (eol_base
))
6018 /* The original CODING_SYSTEM didn't specify text-conversion or
6019 eol-conversion. Be sure that we return a fully complemented
6021 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
6022 return coding_system
;
6026 /* Emacs has a mechanism to automatically detect a coding system if it
6027 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6028 it's impossible to distinguish some coding systems accurately
6029 because they use the same range of codes. So, at first, coding
6030 systems are categorized into 7, those are:
6032 o coding-category-emacs-mule
6034 The category for a coding system which has the same code range
6035 as Emacs' internal format. Assigned the coding-system (Lisp
6036 symbol) `emacs-mule' by default.
6038 o coding-category-sjis
6040 The category for a coding system which has the same code range
6041 as SJIS. Assigned the coding-system (Lisp
6042 symbol) `japanese-shift-jis' by default.
6044 o coding-category-iso-7
6046 The category for a coding system which has the same code range
6047 as ISO2022 of 7-bit environment. This doesn't use any locking
6048 shift and single shift functions. This can encode/decode all
6049 charsets. Assigned the coding-system (Lisp symbol)
6050 `iso-2022-7bit' by default.
6052 o coding-category-iso-7-tight
6054 Same as coding-category-iso-7 except that this can
6055 encode/decode only the specified charsets.
6057 o coding-category-iso-8-1
6059 The category for a coding system which has the same code range
6060 as ISO2022 of 8-bit environment and graphic plane 1 used only
6061 for DIMENSION1 charset. This doesn't use any locking shift
6062 and single shift functions. Assigned the coding-system (Lisp
6063 symbol) `iso-latin-1' by default.
6065 o coding-category-iso-8-2
6067 The category for a coding system which has the same code range
6068 as ISO2022 of 8-bit environment and graphic plane 1 used only
6069 for DIMENSION2 charset. This doesn't use any locking shift
6070 and single shift functions. Assigned the coding-system (Lisp
6071 symbol) `japanese-iso-8bit' by default.
6073 o coding-category-iso-7-else
6075 The category for a coding system which has the same code range
6076 as ISO2022 of 7-bit environment but uses locking shift or
6077 single shift functions. Assigned the coding-system (Lisp
6078 symbol) `iso-2022-7bit-lock' by default.
6080 o coding-category-iso-8-else
6082 The category for a coding system which has the same code range
6083 as ISO2022 of 8-bit environment but uses locking shift or
6084 single shift functions. Assigned the coding-system (Lisp
6085 symbol) `iso-2022-8bit-ss2' by default.
6087 o coding-category-big5
6089 The category for a coding system which has the same code range
6090 as BIG5. Assigned the coding-system (Lisp symbol)
6091 `cn-big5' by default.
6093 o coding-category-utf-8
6095 The category for a coding system which has the same code range
6096 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6097 symbol) `utf-8' by default.
6099 o coding-category-utf-16-be
6101 The category for a coding system in which a text has an
6102 Unicode signature (cf. Unicode Standard) in the order of BIG
6103 endian at the head. Assigned the coding-system (Lisp symbol)
6104 `utf-16-be' by default.
6106 o coding-category-utf-16-le
6108 The category for a coding system in which a text has an
6109 Unicode signature (cf. Unicode Standard) in the order of
6110 LITTLE endian at the head. Assigned the coding-system (Lisp
6111 symbol) `utf-16-le' by default.
6113 o coding-category-ccl
6115 The category for a coding system of which encoder/decoder is
6116 written in CCL programs. The default value is nil, i.e., no
6117 coding system is assigned.
6119 o coding-category-binary
6121 The category for a coding system not categorized in any of the
6122 above. Assigned the coding-system (Lisp symbol)
6123 `no-conversion' by default.
6125 Each of them is a Lisp symbol and the value is an actual
6126 `coding-system's (this is also a Lisp symbol) assigned by a user.
6127 What Emacs does actually is to detect a category of coding system.
6128 Then, it uses a `coding-system' assigned to it. If Emacs can't
6129 decide only one possible category, it selects a category of the
6130 highest priority. Priorities of categories are also specified by a
6131 user in a Lisp variable `coding-category-list'.
6135 #define EOL_SEEN_NONE 0
6136 #define EOL_SEEN_LF 1
6137 #define EOL_SEEN_CR 2
6138 #define EOL_SEEN_CRLF 4
6140 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6141 SOURCE is encoded. If CATEGORY is one of
6142 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6143 two-byte, else they are encoded by one-byte.
6145 Return one of EOL_SEEN_XXX. */
6147 #define MAX_EOL_CHECK_COUNT 3
6150 detect_eol (const unsigned char *source
, EMACS_INT src_bytes
,
6151 enum coding_category category
)
6153 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6156 int eol_seen
= EOL_SEEN_NONE
;
6158 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6162 msb
= category
== (coding_category_utf_16_le
6163 | coding_category_utf_16_le_nosig
);
6166 while (src
+ 1 < src_end
)
6169 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6174 this_eol
= EOL_SEEN_LF
;
6175 else if (src
+ 3 >= src_end
6176 || src
[msb
+ 2] != 0
6177 || src
[lsb
+ 2] != '\n')
6178 this_eol
= EOL_SEEN_CR
;
6181 this_eol
= EOL_SEEN_CRLF
;
6185 if (eol_seen
== EOL_SEEN_NONE
)
6186 /* This is the first end-of-line. */
6187 eol_seen
= this_eol
;
6188 else if (eol_seen
!= this_eol
)
6190 /* The found type is different from what found before.
6191 Allow for stray ^M characters in DOS EOL files. */
6192 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6193 || (eol_seen
== EOL_SEEN_CRLF
6194 && this_eol
== EOL_SEEN_CR
))
6195 eol_seen
= EOL_SEEN_CRLF
;
6198 eol_seen
= EOL_SEEN_LF
;
6202 if (++total
== MAX_EOL_CHECK_COUNT
)
6209 while (src
< src_end
)
6212 if (c
== '\n' || c
== '\r')
6217 this_eol
= EOL_SEEN_LF
;
6218 else if (src
>= src_end
|| *src
!= '\n')
6219 this_eol
= EOL_SEEN_CR
;
6221 this_eol
= EOL_SEEN_CRLF
, src
++;
6223 if (eol_seen
== EOL_SEEN_NONE
)
6224 /* This is the first end-of-line. */
6225 eol_seen
= this_eol
;
6226 else if (eol_seen
!= this_eol
)
6228 /* The found type is different from what found before.
6229 Allow for stray ^M characters in DOS EOL files. */
6230 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6231 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6232 eol_seen
= EOL_SEEN_CRLF
;
6235 eol_seen
= EOL_SEEN_LF
;
6239 if (++total
== MAX_EOL_CHECK_COUNT
)
6248 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6250 Lisp_Object eol_type
;
6252 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6253 if (eol_seen
& EOL_SEEN_LF
)
6255 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6258 else if (eol_seen
& EOL_SEEN_CRLF
)
6260 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6263 else if (eol_seen
& EOL_SEEN_CR
)
6265 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6271 /* Detect how a text specified in CODING is encoded. If a coding
6272 system is detected, update fields of CODING by the detected coding
6276 detect_coding (struct coding_system
*coding
)
6278 const unsigned char *src
, *src_end
;
6279 int saved_mode
= coding
->mode
;
6281 coding
->consumed
= coding
->consumed_char
= 0;
6282 coding
->produced
= coding
->produced_char
= 0;
6283 coding_set_source (coding
);
6285 src_end
= coding
->source
+ coding
->src_bytes
;
6286 coding
->head_ascii
= 0;
6288 /* If we have not yet decided the text encoding type, detect it
6290 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6293 struct coding_detection_info detect_info
;
6294 int null_byte_found
= 0, eight_bit_found
= 0;
6296 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6297 for (src
= coding
->source
; src
< src_end
; src
++)
6302 eight_bit_found
= 1;
6303 if (null_byte_found
)
6308 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6309 && ! inhibit_iso_escape_detection
6310 && ! detect_info
.checked
)
6312 if (detect_coding_iso_2022 (coding
, &detect_info
))
6314 /* We have scanned the whole data. */
6315 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6317 /* We didn't find an 8-bit code. We may
6318 have found a null-byte, but it's very
6319 rare that a binary file conforms to
6322 coding
->head_ascii
= src
- coding
->source
;
6324 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6328 else if (! c
&& !inhibit_null_byte_detection
)
6330 null_byte_found
= 1;
6331 if (eight_bit_found
)
6334 if (! eight_bit_found
)
6335 coding
->head_ascii
++;
6337 else if (! eight_bit_found
)
6338 coding
->head_ascii
++;
6341 if (null_byte_found
|| eight_bit_found
6342 || coding
->head_ascii
< coding
->src_bytes
6343 || detect_info
.found
)
6345 enum coding_category category
;
6346 struct coding_system
*this;
6348 if (coding
->head_ascii
== coding
->src_bytes
)
6349 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6350 for (i
= 0; i
< coding_category_raw_text
; i
++)
6352 category
= coding_priorities
[i
];
6353 this = coding_categories
+ category
;
6354 if (detect_info
.found
& (1 << category
))
6359 if (null_byte_found
)
6361 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6362 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6364 for (i
= 0; i
< coding_category_raw_text
; i
++)
6366 category
= coding_priorities
[i
];
6367 this = coding_categories
+ category
;
6370 /* No coding system of this category is defined. */
6371 detect_info
.rejected
|= (1 << category
);
6373 else if (category
>= coding_category_raw_text
)
6375 else if (detect_info
.checked
& (1 << category
))
6377 if (detect_info
.found
& (1 << category
))
6380 else if ((*(this->detector
)) (coding
, &detect_info
)
6381 && detect_info
.found
& (1 << category
))
6383 if (category
== coding_category_utf_16_auto
)
6385 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6386 category
= coding_category_utf_16_le
;
6388 category
= coding_category_utf_16_be
;
6395 if (i
< coding_category_raw_text
)
6396 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6397 else if (null_byte_found
)
6398 setup_coding_system (Qno_conversion
, coding
);
6399 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6400 == CATEGORY_MASK_ANY
)
6401 setup_coding_system (Qraw_text
, coding
);
6402 else if (detect_info
.rejected
)
6403 for (i
= 0; i
< coding_category_raw_text
; i
++)
6404 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6406 this = coding_categories
+ coding_priorities
[i
];
6407 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6412 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6413 == coding_category_utf_8_auto
)
6415 Lisp_Object coding_systems
;
6416 struct coding_detection_info detect_info
;
6419 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6420 detect_info
.found
= detect_info
.rejected
= 0;
6421 coding
->head_ascii
= 0;
6422 if (CONSP (coding_systems
)
6423 && detect_coding_utf_8 (coding
, &detect_info
))
6425 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6426 setup_coding_system (XCAR (coding_systems
), coding
);
6428 setup_coding_system (XCDR (coding_systems
), coding
);
6431 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6432 == coding_category_utf_16_auto
)
6434 Lisp_Object coding_systems
;
6435 struct coding_detection_info detect_info
;
6438 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6439 detect_info
.found
= detect_info
.rejected
= 0;
6440 coding
->head_ascii
= 0;
6441 if (CONSP (coding_systems
)
6442 && detect_coding_utf_16 (coding
, &detect_info
))
6444 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6445 setup_coding_system (XCAR (coding_systems
), coding
);
6446 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6447 setup_coding_system (XCDR (coding_systems
), coding
);
6450 coding
->mode
= saved_mode
;
6455 decode_eol (struct coding_system
*coding
)
6457 Lisp_Object eol_type
;
6458 unsigned char *p
, *pbeg
, *pend
;
6460 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6461 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6464 if (NILP (coding
->dst_object
))
6465 pbeg
= coding
->destination
;
6467 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6468 pend
= pbeg
+ coding
->produced
;
6470 if (VECTORP (eol_type
))
6472 int eol_seen
= EOL_SEEN_NONE
;
6474 for (p
= pbeg
; p
< pend
; p
++)
6477 eol_seen
|= EOL_SEEN_LF
;
6478 else if (*p
== '\r')
6480 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6482 eol_seen
|= EOL_SEEN_CRLF
;
6486 eol_seen
|= EOL_SEEN_CR
;
6489 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6490 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6491 && (eol_seen
& EOL_SEEN_CR
) != 0
6492 && (eol_seen
& EOL_SEEN_LF
) == 0)
6493 eol_seen
= EOL_SEEN_CRLF
;
6494 else if (eol_seen
!= EOL_SEEN_NONE
6495 && eol_seen
!= EOL_SEEN_LF
6496 && eol_seen
!= EOL_SEEN_CRLF
6497 && eol_seen
!= EOL_SEEN_CR
)
6498 eol_seen
= EOL_SEEN_LF
;
6499 if (eol_seen
!= EOL_SEEN_NONE
)
6500 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6503 if (EQ (eol_type
, Qmac
))
6505 for (p
= pbeg
; p
< pend
; p
++)
6509 else if (EQ (eol_type
, Qdos
))
6513 if (NILP (coding
->dst_object
))
6515 /* Start deleting '\r' from the tail to minimize the memory
6517 for (p
= pend
- 2; p
>= pbeg
; p
--)
6520 memmove (p
, p
+ 1, pend
-- - p
- 1);
6526 int pos_byte
= coding
->dst_pos_byte
;
6527 int pos
= coding
->dst_pos
;
6528 int pos_end
= pos
+ coding
->produced_char
- 1;
6530 while (pos
< pos_end
)
6532 p
= BYTE_POS_ADDR (pos_byte
);
6533 if (*p
== '\r' && p
[1] == '\n')
6535 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6540 if (coding
->dst_multibyte
)
6541 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6546 coding
->produced
-= n
;
6547 coding
->produced_char
-= n
;
6552 /* Return a translation table (or list of them) from coding system
6553 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6554 decoding (ENCODEP is zero). */
6557 get_translation_table (Lisp_Object attrs
, int encodep
, int *max_lookup
)
6559 Lisp_Object standard
, translation_table
;
6562 if (NILP (Venable_character_translation
))
6569 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6570 standard
= Vstandard_translation_table_for_encode
;
6572 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6573 standard
= Vstandard_translation_table_for_decode
;
6574 if (NILP (translation_table
))
6575 translation_table
= standard
;
6578 if (SYMBOLP (translation_table
))
6579 translation_table
= Fget (translation_table
, Qtranslation_table
);
6580 else if (CONSP (translation_table
))
6582 translation_table
= Fcopy_sequence (translation_table
);
6583 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6584 if (SYMBOLP (XCAR (val
)))
6585 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6587 if (CHAR_TABLE_P (standard
))
6589 if (CONSP (translation_table
))
6590 translation_table
= nconc2 (translation_table
,
6591 Fcons (standard
, Qnil
));
6593 translation_table
= Fcons (translation_table
,
6594 Fcons (standard
, Qnil
));
6601 if (CHAR_TABLE_P (translation_table
)
6602 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6604 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6605 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6606 *max_lookup
= XFASTINT (val
);
6608 else if (CONSP (translation_table
))
6610 Lisp_Object tail
, val
;
6612 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6613 if (CHAR_TABLE_P (XCAR (tail
))
6614 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6616 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6617 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6618 *max_lookup
= XFASTINT (val
);
6622 return translation_table
;
6625 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6628 if (CHAR_TABLE_P (table)) \
6630 trans = CHAR_TABLE_REF (table, c); \
6631 if (CHARACTERP (trans)) \
6632 c = XFASTINT (trans), trans = Qnil; \
6634 else if (CONSP (table)) \
6638 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6639 if (CHAR_TABLE_P (XCAR (tail))) \
6641 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6642 if (CHARACTERP (trans)) \
6643 c = XFASTINT (trans), trans = Qnil; \
6644 else if (! NILP (trans)) \
6651 /* Return a translation of character(s) at BUF according to TRANS.
6652 TRANS is TO-CHAR or ((FROM . TO) ...) where
6653 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6654 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6655 translation is found, and Qnil if not found..
6656 If BUF is too short to lookup characters in FROM, return Qt. */
6659 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6662 if (INTEGERP (trans
))
6664 for (; CONSP (trans
); trans
= XCDR (trans
))
6666 Lisp_Object val
= XCAR (trans
);
6667 Lisp_Object from
= XCAR (val
);
6668 int len
= ASIZE (from
);
6671 for (i
= 0; i
< len
; i
++)
6673 if (buf
+ i
== buf_end
)
6675 if (XINT (AREF (from
, i
)) != buf
[i
])
6686 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6689 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6690 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6692 EMACS_INT produced_chars
= 0;
6695 if (! coding
->chars_at_source
)
6697 /* Source characters are in coding->charbuf. */
6698 int *buf
= coding
->charbuf
;
6699 int *buf_end
= buf
+ coding
->charbuf_used
;
6701 if (EQ (coding
->src_object
, coding
->dst_object
))
6703 coding_set_source (coding
);
6704 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6707 while (buf
< buf_end
)
6713 int from_nchars
= 1, to_nchars
= 1;
6714 Lisp_Object trans
= Qnil
;
6716 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6719 trans
= get_translation (trans
, buf
, buf_end
);
6720 if (INTEGERP (trans
))
6722 else if (CONSP (trans
))
6724 from_nchars
= ASIZE (XCAR (trans
));
6725 trans
= XCDR (trans
);
6726 if (INTEGERP (trans
))
6730 to_nchars
= ASIZE (trans
);
6731 c
= XINT (AREF (trans
, 0));
6734 else if (EQ (trans
, Qt
) && ! last_block
)
6738 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6740 dst
= alloc_destination (coding
,
6742 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6744 if (EQ (coding
->src_object
, coding
->dst_object
))
6746 coding_set_source (coding
);
6747 dst_end
= (((unsigned char *) coding
->source
)
6748 + coding
->consumed
);
6751 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6754 for (i
= 0; i
< to_nchars
; i
++)
6757 c
= XINT (AREF (trans
, i
));
6758 if (coding
->dst_multibyte
6759 || ! CHAR_BYTE8_P (c
))
6760 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6762 *dst
++ = CHAR_TO_BYTE8 (c
);
6764 produced_chars
+= to_nchars
;
6768 /* This is an annotation datum. (-C) is the length. */
6771 carryover
= buf_end
- buf
;
6775 /* Source characters are at coding->source. */
6776 const unsigned char *src
= coding
->source
;
6777 const unsigned char *src_end
= src
+ coding
->consumed
;
6779 if (EQ (coding
->dst_object
, coding
->src_object
))
6780 dst_end
= (unsigned char *) src
;
6781 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6783 if (coding
->src_multibyte
)
6786 EMACS_INT consumed_chars
= 0;
6790 const unsigned char *src_base
= src
;
6796 if (EQ (coding
->src_object
, coding
->dst_object
))
6797 dst_end
= (unsigned char *) src
;
6800 EMACS_INT offset
= src
- coding
->source
;
6802 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6804 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6805 coding_set_source (coding
);
6806 src
= coding
->source
+ offset
;
6807 src_end
= coding
->source
+ coding
->src_bytes
;
6808 if (EQ (coding
->src_object
, coding
->dst_object
))
6809 dst_end
= (unsigned char *) src
;
6819 while (src
< src_end
)
6824 if (dst
>= dst_end
- 1)
6826 if (EQ (coding
->src_object
, coding
->dst_object
))
6827 dst_end
= (unsigned char *) src
;
6828 if (dst
>= dst_end
- 1)
6830 EMACS_INT offset
= src
- coding
->source
;
6831 EMACS_INT more_bytes
;
6833 if (EQ (coding
->src_object
, coding
->dst_object
))
6834 more_bytes
= ((src_end
- src
) / 2) + 2;
6836 more_bytes
= src_end
- src
+ 2;
6837 dst
= alloc_destination (coding
, more_bytes
, dst
);
6838 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6839 coding_set_source (coding
);
6840 src
= coding
->source
+ offset
;
6841 src_end
= coding
->source
+ coding
->src_bytes
;
6842 if (EQ (coding
->src_object
, coding
->dst_object
))
6843 dst_end
= (unsigned char *) src
;
6851 if (!EQ (coding
->src_object
, coding
->dst_object
))
6853 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6857 EMACS_INT offset
= src
- coding
->source
;
6859 dst
= alloc_destination (coding
, require
, dst
);
6860 coding_set_source (coding
);
6861 src
= coding
->source
+ offset
;
6862 src_end
= coding
->source
+ coding
->src_bytes
;
6865 produced_chars
= coding
->consumed_char
;
6866 while (src
< src_end
)
6871 produced
= dst
- (coding
->destination
+ coding
->produced
);
6872 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6873 insert_from_gap (produced_chars
, produced
);
6874 coding
->produced
+= produced
;
6875 coding
->produced_char
+= produced_chars
;
6879 /* Compose text in CODING->object according to the annotation data at
6880 CHARBUF. CHARBUF is an array:
6881 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6885 produce_composition (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6889 enum composition_method method
;
6890 Lisp_Object components
;
6892 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6893 to
= pos
+ charbuf
[2];
6894 method
= (enum composition_method
) (charbuf
[4]);
6896 if (method
== COMPOSITION_RELATIVE
)
6900 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6903 if (method
== COMPOSITION_WITH_RULE
)
6904 len
= charbuf
[2] * 3 - 2;
6905 charbuf
+= MAX_ANNOTATION_LENGTH
;
6906 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6907 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6909 if (charbuf
[i
] >= 0)
6910 args
[j
] = make_number (charbuf
[i
]);
6914 args
[j
] = make_number (charbuf
[i
] % 0x100);
6917 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6919 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6923 /* Put `charset' property on text in CODING->object according to
6924 the annotation data at CHARBUF. CHARBUF is an array:
6925 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6929 produce_charset (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6931 EMACS_INT from
= pos
- charbuf
[2];
6932 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6934 Fput_text_property (make_number (from
), make_number (pos
),
6935 Qcharset
, CHARSET_NAME (charset
),
6936 coding
->dst_object
);
6940 #define CHARBUF_SIZE 0x4000
6942 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6944 int size = CHARBUF_SIZE; \
6946 coding->charbuf = NULL; \
6947 while (size > 1024) \
6949 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6950 if (coding->charbuf) \
6954 if (! coding->charbuf) \
6956 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6957 return coding->result; \
6959 coding->charbuf_size = size; \
6964 produce_annotation (struct coding_system
*coding
, EMACS_INT pos
)
6966 int *charbuf
= coding
->charbuf
;
6967 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6969 if (NILP (coding
->dst_object
))
6972 while (charbuf
< charbuf_end
)
6978 int len
= -*charbuf
;
6983 case CODING_ANNOTATE_COMPOSITION_MASK
:
6984 produce_composition (coding
, charbuf
, pos
);
6986 case CODING_ANNOTATE_CHARSET_MASK
:
6987 produce_charset (coding
, charbuf
, pos
);
6995 /* Decode the data at CODING->src_object into CODING->dst_object.
6996 CODING->src_object is a buffer, a string, or nil.
6997 CODING->dst_object is a buffer.
6999 If CODING->src_object is a buffer, it must be the current buffer.
7000 In this case, if CODING->src_pos is positive, it is a position of
7001 the source text in the buffer, otherwise, the source text is in the
7002 gap area of the buffer, and CODING->src_pos specifies the offset of
7003 the text from GPT (which must be the same as PT). If this is the
7004 same buffer as CODING->dst_object, CODING->src_pos must be
7007 If CODING->src_object is a string, CODING->src_pos is an index to
7010 If CODING->src_object is nil, CODING->source must already point to
7011 the non-relocatable memory area. In this case, CODING->src_pos is
7012 an offset from CODING->source.
7014 The decoded data is inserted at the current point of the buffer
7019 decode_coding (struct coding_system
*coding
)
7022 Lisp_Object undo_list
;
7023 Lisp_Object translation_table
;
7024 struct ccl_spec cclspec
;
7028 if (BUFFERP (coding
->src_object
)
7029 && coding
->src_pos
> 0
7030 && coding
->src_pos
< GPT
7031 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7032 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7035 if (BUFFERP (coding
->dst_object
))
7037 if (current_buffer
!= XBUFFER (coding
->dst_object
))
7038 set_buffer_internal (XBUFFER (coding
->dst_object
));
7040 move_gap_both (PT
, PT_BYTE
);
7041 undo_list
= current_buffer
->undo_list
;
7042 current_buffer
->undo_list
= Qt
;
7045 coding
->consumed
= coding
->consumed_char
= 0;
7046 coding
->produced
= coding
->produced_char
= 0;
7047 coding
->chars_at_source
= 0;
7048 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7051 ALLOC_CONVERSION_WORK_AREA (coding
);
7053 attrs
= CODING_ID_ATTRS (coding
->id
);
7054 translation_table
= get_translation_table (attrs
, 0, NULL
);
7057 if (coding
->decoder
== decode_coding_ccl
)
7059 coding
->spec
.ccl
= &cclspec
;
7060 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7064 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7066 coding_set_source (coding
);
7067 coding
->annotated
= 0;
7068 coding
->charbuf_used
= carryover
;
7069 (*(coding
->decoder
)) (coding
);
7070 coding_set_destination (coding
);
7071 carryover
= produce_chars (coding
, translation_table
, 0);
7072 if (coding
->annotated
)
7073 produce_annotation (coding
, pos
);
7074 for (i
= 0; i
< carryover
; i
++)
7076 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7078 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7079 || (coding
->consumed
< coding
->src_bytes
7080 && (coding
->result
== CODING_RESULT_SUCCESS
7081 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7085 coding_set_destination (coding
);
7086 coding
->charbuf_used
= carryover
;
7087 produce_chars (coding
, translation_table
, 1);
7090 coding
->carryover_bytes
= 0;
7091 if (coding
->consumed
< coding
->src_bytes
)
7093 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7094 const unsigned char *src
;
7096 coding_set_source (coding
);
7097 coding_set_destination (coding
);
7098 src
= coding
->source
+ coding
->consumed
;
7100 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7102 /* Flush out unprocessed data as binary chars. We are sure
7103 that the number of data is less than the size of
7105 coding
->charbuf_used
= 0;
7106 coding
->chars_at_source
= 0;
7108 while (nbytes
-- > 0)
7113 c
= BYTE8_TO_CHAR (c
);
7114 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7116 produce_chars (coding
, Qnil
, 1);
7120 /* Record unprocessed bytes in coding->carryover. We are
7121 sure that the number of data is less than the size of
7122 coding->carryover. */
7123 unsigned char *p
= coding
->carryover
;
7125 if (nbytes
> sizeof coding
->carryover
)
7126 nbytes
= sizeof coding
->carryover
;
7127 coding
->carryover_bytes
= nbytes
;
7128 while (nbytes
-- > 0)
7131 coding
->consumed
= coding
->src_bytes
;
7134 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7135 && !inhibit_eol_conversion
)
7136 decode_eol (coding
);
7137 if (BUFFERP (coding
->dst_object
))
7139 current_buffer
->undo_list
= undo_list
;
7140 record_insert (coding
->dst_pos
, coding
->produced_char
);
7142 return coding
->result
;
7146 /* Extract an annotation datum from a composition starting at POS and
7147 ending before LIMIT of CODING->src_object (buffer or string), store
7148 the data in BUF, set *STOP to a starting position of the next
7149 composition (if any) or to LIMIT, and return the address of the
7150 next element of BUF.
7152 If such an annotation is not found, set *STOP to a starting
7153 position of a composition after POS (if any) or to LIMIT, and
7157 handle_composition_annotation (EMACS_INT pos
, EMACS_INT limit
,
7158 struct coding_system
*coding
, int *buf
,
7161 EMACS_INT start
, end
;
7164 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7167 else if (start
> pos
)
7173 /* We found a composition. Store the corresponding
7174 annotation data in BUF. */
7176 enum composition_method method
= COMPOSITION_METHOD (prop
);
7177 int nchars
= COMPOSITION_LENGTH (prop
);
7179 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7180 if (method
!= COMPOSITION_RELATIVE
)
7182 Lisp_Object components
;
7185 components
= COMPOSITION_COMPONENTS (prop
);
7186 if (VECTORP (components
))
7188 len
= XVECTOR (components
)->size
;
7189 for (i
= 0; i
< len
; i
++)
7190 *buf
++ = XINT (AREF (components
, i
));
7192 else if (STRINGP (components
))
7194 len
= SCHARS (components
);
7198 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7202 else if (INTEGERP (components
))
7205 *buf
++ = XINT (components
);
7207 else if (CONSP (components
))
7209 for (len
= 0; CONSP (components
);
7210 len
++, components
= XCDR (components
))
7211 *buf
++ = XINT (XCAR (components
));
7219 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7230 /* Extract an annotation datum from a text property `charset' at POS of
7231 CODING->src_object (buffer of string), store the data in BUF, set
7232 *STOP to the position where the value of `charset' property changes
7233 (limiting by LIMIT), and return the address of the next element of
7236 If the property value is nil, set *STOP to the position where the
7237 property value is non-nil (limiting by LIMIT), and return BUF. */
7240 handle_charset_annotation (EMACS_INT pos
, EMACS_INT limit
,
7241 struct coding_system
*coding
, int *buf
,
7244 Lisp_Object val
, next
;
7247 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7248 if (! NILP (val
) && CHARSETP (val
))
7249 id
= XINT (CHARSET_SYMBOL_ID (val
));
7252 ADD_CHARSET_DATA (buf
, 0, id
);
7253 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7255 make_number (limit
));
7256 *stop
= XINT (next
);
7262 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7265 int *buf
= coding
->charbuf
;
7266 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7267 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7268 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7269 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7270 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7271 int multibytep
= coding
->src_multibyte
;
7272 Lisp_Object eol_type
;
7274 EMACS_INT stop
, stop_composition
, stop_charset
;
7275 int *lookup_buf
= NULL
;
7277 if (! NILP (translation_table
))
7278 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7280 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7281 if (VECTORP (eol_type
))
7284 /* Note: composition handling is not yet implemented. */
7285 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7287 if (NILP (coding
->src_object
))
7288 stop
= stop_composition
= stop_charset
= end_pos
;
7291 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7292 stop
= stop_composition
= pos
;
7294 stop
= stop_composition
= end_pos
;
7295 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7296 stop
= stop_charset
= pos
;
7298 stop_charset
= end_pos
;
7301 /* Compensate for CRLF and conversion. */
7302 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7303 while (buf
< buf_end
)
7311 if (pos
== stop_composition
)
7312 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7313 buf
, &stop_composition
);
7314 if (pos
== stop_charset
)
7315 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7316 buf
, &stop_charset
);
7317 stop
= (stop_composition
< stop_charset
7318 ? stop_composition
: stop_charset
);
7325 if (coding
->encoder
== encode_coding_raw_text
7326 || coding
->encoder
== encode_coding_ccl
)
7328 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7329 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7331 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7334 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7335 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7337 if (! EQ (eol_type
, Qunix
))
7341 if (EQ (eol_type
, Qdos
))
7349 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7354 int from_nchars
= 1, to_nchars
= 1;
7355 int *lookup_buf_end
;
7356 const unsigned char *p
= src
;
7360 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7361 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7362 lookup_buf_end
= lookup_buf
+ i
;
7363 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7364 if (INTEGERP (trans
))
7366 else if (CONSP (trans
))
7368 from_nchars
= ASIZE (XCAR (trans
));
7369 trans
= XCDR (trans
);
7370 if (INTEGERP (trans
))
7374 to_nchars
= ASIZE (trans
);
7375 if (buf
+ to_nchars
> buf_end
)
7377 c
= XINT (AREF (trans
, 0));
7383 for (i
= 1; i
< to_nchars
; i
++)
7384 *buf
++ = XINT (AREF (trans
, i
));
7385 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7386 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7390 coding
->consumed
= src
- coding
->source
;
7391 coding
->consumed_char
= pos
- coding
->src_pos
;
7392 coding
->charbuf_used
= buf
- coding
->charbuf
;
7393 coding
->chars_at_source
= 0;
7397 /* Encode the text at CODING->src_object into CODING->dst_object.
7398 CODING->src_object is a buffer or a string.
7399 CODING->dst_object is a buffer or nil.
7401 If CODING->src_object is a buffer, it must be the current buffer.
7402 In this case, if CODING->src_pos is positive, it is a position of
7403 the source text in the buffer, otherwise. the source text is in the
7404 gap area of the buffer, and coding->src_pos specifies the offset of
7405 the text from GPT (which must be the same as PT). If this is the
7406 same buffer as CODING->dst_object, CODING->src_pos must be
7407 negative and CODING should not have `pre-write-conversion'.
7409 If CODING->src_object is a string, CODING should not have
7410 `pre-write-conversion'.
7412 If CODING->dst_object is a buffer, the encoded data is inserted at
7413 the current point of that buffer.
7415 If CODING->dst_object is nil, the encoded data is placed at the
7416 memory area specified by CODING->destination. */
7419 encode_coding (struct coding_system
*coding
)
7422 Lisp_Object translation_table
;
7424 struct ccl_spec cclspec
;
7426 attrs
= CODING_ID_ATTRS (coding
->id
);
7427 if (coding
->encoder
== encode_coding_raw_text
)
7428 translation_table
= Qnil
, max_lookup
= 0;
7430 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7432 if (BUFFERP (coding
->dst_object
))
7434 set_buffer_internal (XBUFFER (coding
->dst_object
));
7435 coding
->dst_multibyte
7436 = ! NILP (current_buffer
->enable_multibyte_characters
);
7439 coding
->consumed
= coding
->consumed_char
= 0;
7440 coding
->produced
= coding
->produced_char
= 0;
7441 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7444 ALLOC_CONVERSION_WORK_AREA (coding
);
7446 if (coding
->encoder
== encode_coding_ccl
)
7448 coding
->spec
.ccl
= &cclspec
;
7449 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7452 coding_set_source (coding
);
7453 consume_chars (coding
, translation_table
, max_lookup
);
7454 coding_set_destination (coding
);
7455 (*(coding
->encoder
)) (coding
);
7456 } while (coding
->consumed_char
< coding
->src_chars
);
7458 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7459 insert_from_gap (coding
->produced_char
, coding
->produced
);
7461 return (coding
->result
);
7465 /* Name (or base name) of work buffer for code conversion. */
7466 static Lisp_Object Vcode_conversion_workbuf_name
;
7468 /* A working buffer used by the top level conversion. Once it is
7469 created, it is never destroyed. It has the name
7470 Vcode_conversion_workbuf_name. The other working buffers are
7471 destroyed after the use is finished, and their names are modified
7472 versions of Vcode_conversion_workbuf_name. */
7473 static Lisp_Object Vcode_conversion_reused_workbuf
;
7475 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7476 static int reused_workbuf_in_use
;
7479 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7480 multibyteness of returning buffer. */
7483 make_conversion_work_buffer (int multibyte
)
7485 Lisp_Object name
, workbuf
;
7486 struct buffer
*current
;
7488 if (reused_workbuf_in_use
++)
7490 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7491 workbuf
= Fget_buffer_create (name
);
7495 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7496 Vcode_conversion_reused_workbuf
7497 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7498 workbuf
= Vcode_conversion_reused_workbuf
;
7500 current
= current_buffer
;
7501 set_buffer_internal (XBUFFER (workbuf
));
7502 /* We can't allow modification hooks to run in the work buffer. For
7503 instance, directory_files_internal assumes that file decoding
7504 doesn't compile new regexps. */
7505 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7507 current_buffer
->undo_list
= Qt
;
7508 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
7509 set_buffer_internal (current
);
7515 code_conversion_restore (Lisp_Object arg
)
7517 Lisp_Object current
, workbuf
;
7518 struct gcpro gcpro1
;
7521 current
= XCAR (arg
);
7522 workbuf
= XCDR (arg
);
7523 if (! NILP (workbuf
))
7525 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7526 reused_workbuf_in_use
= 0;
7527 else if (! NILP (Fbuffer_live_p (workbuf
)))
7528 Fkill_buffer (workbuf
);
7530 set_buffer_internal (XBUFFER (current
));
7536 code_conversion_save (int with_work_buf
, int multibyte
)
7538 Lisp_Object workbuf
= Qnil
;
7541 workbuf
= make_conversion_work_buffer (multibyte
);
7542 record_unwind_protect (code_conversion_restore
,
7543 Fcons (Fcurrent_buffer (), workbuf
));
7548 decode_coding_gap (struct coding_system
*coding
,
7549 EMACS_INT chars
, EMACS_INT bytes
)
7551 int count
= SPECPDL_INDEX ();
7554 code_conversion_save (0, 0);
7556 coding
->src_object
= Fcurrent_buffer ();
7557 coding
->src_chars
= chars
;
7558 coding
->src_bytes
= bytes
;
7559 coding
->src_pos
= -chars
;
7560 coding
->src_pos_byte
= -bytes
;
7561 coding
->src_multibyte
= chars
< bytes
;
7562 coding
->dst_object
= coding
->src_object
;
7563 coding
->dst_pos
= PT
;
7564 coding
->dst_pos_byte
= PT_BYTE
;
7565 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
7567 if (CODING_REQUIRE_DETECTION (coding
))
7568 detect_coding (coding
);
7570 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7571 current_buffer
->text
->inhibit_shrinking
= 1;
7572 decode_coding (coding
);
7573 current_buffer
->text
->inhibit_shrinking
= 0;
7575 attrs
= CODING_ID_ATTRS (coding
->id
);
7576 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7578 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7581 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7582 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7583 make_number (coding
->produced_char
));
7585 coding
->produced_char
+= Z
- prev_Z
;
7586 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7589 unbind_to (count
, Qnil
);
7590 return coding
->result
;
7594 encode_coding_gap (struct coding_system
*coding
,
7595 EMACS_INT chars
, EMACS_INT bytes
)
7597 int count
= SPECPDL_INDEX ();
7599 code_conversion_save (0, 0);
7601 coding
->src_object
= Fcurrent_buffer ();
7602 coding
->src_chars
= chars
;
7603 coding
->src_bytes
= bytes
;
7604 coding
->src_pos
= -chars
;
7605 coding
->src_pos_byte
= -bytes
;
7606 coding
->src_multibyte
= chars
< bytes
;
7607 coding
->dst_object
= coding
->src_object
;
7608 coding
->dst_pos
= PT
;
7609 coding
->dst_pos_byte
= PT_BYTE
;
7611 encode_coding (coding
);
7613 unbind_to (count
, Qnil
);
7614 return coding
->result
;
7618 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7619 SRC_OBJECT into DST_OBJECT by coding context CODING.
7621 SRC_OBJECT is a buffer, a string, or Qnil.
7623 If it is a buffer, the text is at point of the buffer. FROM and TO
7624 are positions in the buffer.
7626 If it is a string, the text is at the beginning of the string.
7627 FROM and TO are indices to the string.
7629 If it is nil, the text is at coding->source. FROM and TO are
7630 indices to coding->source.
7632 DST_OBJECT is a buffer, Qt, or Qnil.
7634 If it is a buffer, the decoded text is inserted at point of the
7635 buffer. If the buffer is the same as SRC_OBJECT, the source text
7638 If it is Qt, a string is made from the decoded text, and
7639 set in CODING->dst_object.
7641 If it is Qnil, the decoded text is stored at CODING->destination.
7642 The caller must allocate CODING->dst_bytes bytes at
7643 CODING->destination by xmalloc. If the decoded text is longer than
7644 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7648 decode_coding_object (struct coding_system
*coding
,
7649 Lisp_Object src_object
,
7650 EMACS_INT from
, EMACS_INT from_byte
,
7651 EMACS_INT to
, EMACS_INT to_byte
,
7652 Lisp_Object dst_object
)
7654 int count
= SPECPDL_INDEX ();
7655 unsigned char *destination
;
7656 EMACS_INT dst_bytes
;
7657 EMACS_INT chars
= to
- from
;
7658 EMACS_INT bytes
= to_byte
- from_byte
;
7660 int saved_pt
= -1, saved_pt_byte
;
7661 int need_marker_adjustment
= 0;
7662 Lisp_Object old_deactivate_mark
;
7664 old_deactivate_mark
= Vdeactivate_mark
;
7666 if (NILP (dst_object
))
7668 destination
= coding
->destination
;
7669 dst_bytes
= coding
->dst_bytes
;
7672 coding
->src_object
= src_object
;
7673 coding
->src_chars
= chars
;
7674 coding
->src_bytes
= bytes
;
7675 coding
->src_multibyte
= chars
< bytes
;
7677 if (STRINGP (src_object
))
7679 coding
->src_pos
= from
;
7680 coding
->src_pos_byte
= from_byte
;
7682 else if (BUFFERP (src_object
))
7684 set_buffer_internal (XBUFFER (src_object
));
7686 move_gap_both (from
, from_byte
);
7687 if (EQ (src_object
, dst_object
))
7689 struct Lisp_Marker
*tail
;
7691 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7693 tail
->need_adjustment
7694 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7695 need_marker_adjustment
|= tail
->need_adjustment
;
7697 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7698 TEMP_SET_PT_BOTH (from
, from_byte
);
7699 current_buffer
->text
->inhibit_shrinking
= 1;
7700 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7701 coding
->src_pos
= -chars
;
7702 coding
->src_pos_byte
= -bytes
;
7706 coding
->src_pos
= from
;
7707 coding
->src_pos_byte
= from_byte
;
7711 if (CODING_REQUIRE_DETECTION (coding
))
7712 detect_coding (coding
);
7713 attrs
= CODING_ID_ATTRS (coding
->id
);
7715 if (EQ (dst_object
, Qt
)
7716 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7717 && NILP (dst_object
)))
7719 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7720 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7721 coding
->dst_pos
= BEG
;
7722 coding
->dst_pos_byte
= BEG_BYTE
;
7724 else if (BUFFERP (dst_object
))
7726 code_conversion_save (0, 0);
7727 coding
->dst_object
= dst_object
;
7728 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7729 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7730 coding
->dst_multibyte
7731 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7735 code_conversion_save (0, 0);
7736 coding
->dst_object
= Qnil
;
7737 /* Most callers presume this will return a multibyte result, and they
7738 won't use `binary' or `raw-text' anyway, so let's not worry about
7739 CODING_FOR_UNIBYTE. */
7740 coding
->dst_multibyte
= 1;
7743 decode_coding (coding
);
7745 if (BUFFERP (coding
->dst_object
))
7746 set_buffer_internal (XBUFFER (coding
->dst_object
));
7748 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7750 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7751 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7754 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7755 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7756 old_deactivate_mark
);
7757 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7758 make_number (coding
->produced_char
));
7761 coding
->produced_char
+= Z
- prev_Z
;
7762 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7765 if (EQ (dst_object
, Qt
))
7767 coding
->dst_object
= Fbuffer_string ();
7769 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7771 set_buffer_internal (XBUFFER (coding
->dst_object
));
7772 if (dst_bytes
< coding
->produced
)
7774 destination
= xrealloc (destination
, coding
->produced
);
7777 record_conversion_result (coding
,
7778 CODING_RESULT_INSUFFICIENT_MEM
);
7779 unbind_to (count
, Qnil
);
7782 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7783 move_gap_both (BEGV
, BEGV_BYTE
);
7784 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7785 coding
->destination
= destination
;
7791 /* This is the case of:
7792 (BUFFERP (src_object) && EQ (src_object, dst_object))
7793 As we have moved PT while replacing the original buffer
7794 contents, we must recover it now. */
7795 set_buffer_internal (XBUFFER (src_object
));
7796 current_buffer
->text
->inhibit_shrinking
= 0;
7797 if (saved_pt
< from
)
7798 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7799 else if (saved_pt
< from
+ chars
)
7800 TEMP_SET_PT_BOTH (from
, from_byte
);
7801 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7802 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7803 saved_pt_byte
+ (coding
->produced
- bytes
));
7805 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7806 saved_pt_byte
+ (coding
->produced
- bytes
));
7808 if (need_marker_adjustment
)
7810 struct Lisp_Marker
*tail
;
7812 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7813 if (tail
->need_adjustment
)
7815 tail
->need_adjustment
= 0;
7816 if (tail
->insertion_type
)
7818 tail
->bytepos
= from_byte
;
7819 tail
->charpos
= from
;
7823 tail
->bytepos
= from_byte
+ coding
->produced
;
7825 = (NILP (current_buffer
->enable_multibyte_characters
)
7826 ? tail
->bytepos
: from
+ coding
->produced_char
);
7832 Vdeactivate_mark
= old_deactivate_mark
;
7833 unbind_to (count
, coding
->dst_object
);
7838 encode_coding_object (struct coding_system
*coding
,
7839 Lisp_Object src_object
,
7840 EMACS_INT from
, EMACS_INT from_byte
,
7841 EMACS_INT to
, EMACS_INT to_byte
,
7842 Lisp_Object dst_object
)
7844 int count
= SPECPDL_INDEX ();
7845 EMACS_INT chars
= to
- from
;
7846 EMACS_INT bytes
= to_byte
- from_byte
;
7848 int saved_pt
= -1, saved_pt_byte
;
7849 int need_marker_adjustment
= 0;
7850 int kill_src_buffer
= 0;
7851 Lisp_Object old_deactivate_mark
;
7853 old_deactivate_mark
= Vdeactivate_mark
;
7855 coding
->src_object
= src_object
;
7856 coding
->src_chars
= chars
;
7857 coding
->src_bytes
= bytes
;
7858 coding
->src_multibyte
= chars
< bytes
;
7860 attrs
= CODING_ID_ATTRS (coding
->id
);
7862 if (EQ (src_object
, dst_object
))
7864 struct Lisp_Marker
*tail
;
7866 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7868 tail
->need_adjustment
7869 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7870 need_marker_adjustment
|= tail
->need_adjustment
;
7874 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7876 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7877 set_buffer_internal (XBUFFER (coding
->src_object
));
7878 if (STRINGP (src_object
))
7879 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7880 else if (BUFFERP (src_object
))
7881 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7883 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7885 if (EQ (src_object
, dst_object
))
7887 set_buffer_internal (XBUFFER (src_object
));
7888 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7889 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7890 set_buffer_internal (XBUFFER (coding
->src_object
));
7894 Lisp_Object args
[3];
7895 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7897 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7898 old_deactivate_mark
);
7899 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7900 args
[1] = make_number (BEG
);
7901 args
[2] = make_number (Z
);
7902 safe_call (3, args
);
7905 if (XBUFFER (coding
->src_object
) != current_buffer
)
7906 kill_src_buffer
= 1;
7907 coding
->src_object
= Fcurrent_buffer ();
7909 move_gap_both (BEG
, BEG_BYTE
);
7910 coding
->src_chars
= Z
- BEG
;
7911 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7912 coding
->src_pos
= BEG
;
7913 coding
->src_pos_byte
= BEG_BYTE
;
7914 coding
->src_multibyte
= Z
< Z_BYTE
;
7916 else if (STRINGP (src_object
))
7918 code_conversion_save (0, 0);
7919 coding
->src_pos
= from
;
7920 coding
->src_pos_byte
= from_byte
;
7922 else if (BUFFERP (src_object
))
7924 code_conversion_save (0, 0);
7925 set_buffer_internal (XBUFFER (src_object
));
7926 if (EQ (src_object
, dst_object
))
7928 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7929 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7930 coding
->src_pos
= 0;
7931 coding
->src_pos_byte
= 0;
7935 if (from
< GPT
&& to
>= GPT
)
7936 move_gap_both (from
, from_byte
);
7937 coding
->src_pos
= from
;
7938 coding
->src_pos_byte
= from_byte
;
7942 code_conversion_save (0, 0);
7944 if (BUFFERP (dst_object
))
7946 coding
->dst_object
= dst_object
;
7947 if (EQ (src_object
, dst_object
))
7949 coding
->dst_pos
= from
;
7950 coding
->dst_pos_byte
= from_byte
;
7954 struct buffer
*current
= current_buffer
;
7956 set_buffer_temp (XBUFFER (dst_object
));
7957 coding
->dst_pos
= PT
;
7958 coding
->dst_pos_byte
= PT_BYTE
;
7959 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7960 set_buffer_temp (current
);
7962 coding
->dst_multibyte
7963 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7965 else if (EQ (dst_object
, Qt
))
7967 coding
->dst_object
= Qnil
;
7968 coding
->dst_bytes
= coding
->src_chars
;
7969 if (coding
->dst_bytes
== 0)
7970 coding
->dst_bytes
= 1;
7971 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7972 coding
->dst_multibyte
= 0;
7976 coding
->dst_object
= Qnil
;
7977 coding
->dst_multibyte
= 0;
7980 encode_coding (coding
);
7982 if (EQ (dst_object
, Qt
))
7984 if (BUFFERP (coding
->dst_object
))
7985 coding
->dst_object
= Fbuffer_string ();
7989 = make_unibyte_string ((char *) coding
->destination
,
7991 xfree (coding
->destination
);
7997 /* This is the case of:
7998 (BUFFERP (src_object) && EQ (src_object, dst_object))
7999 As we have moved PT while replacing the original buffer
8000 contents, we must recover it now. */
8001 set_buffer_internal (XBUFFER (src_object
));
8002 if (saved_pt
< from
)
8003 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8004 else if (saved_pt
< from
+ chars
)
8005 TEMP_SET_PT_BOTH (from
, from_byte
);
8006 else if (! NILP (current_buffer
->enable_multibyte_characters
))
8007 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8008 saved_pt_byte
+ (coding
->produced
- bytes
));
8010 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8011 saved_pt_byte
+ (coding
->produced
- bytes
));
8013 if (need_marker_adjustment
)
8015 struct Lisp_Marker
*tail
;
8017 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8018 if (tail
->need_adjustment
)
8020 tail
->need_adjustment
= 0;
8021 if (tail
->insertion_type
)
8023 tail
->bytepos
= from_byte
;
8024 tail
->charpos
= from
;
8028 tail
->bytepos
= from_byte
+ coding
->produced
;
8030 = (NILP (current_buffer
->enable_multibyte_characters
)
8031 ? tail
->bytepos
: from
+ coding
->produced_char
);
8037 if (kill_src_buffer
)
8038 Fkill_buffer (coding
->src_object
);
8040 Vdeactivate_mark
= old_deactivate_mark
;
8041 unbind_to (count
, Qnil
);
8046 preferred_coding_system (void)
8048 int id
= coding_categories
[coding_priorities
[0]].id
;
8050 return CODING_ID_NAME (id
);
8055 /*** 8. Emacs Lisp library functions ***/
8057 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8058 doc
: /* Return t if OBJECT is nil or a coding-system.
8059 See the documentation of `define-coding-system' for information
8060 about coding-system objects. */)
8061 (Lisp_Object object
)
8064 || CODING_SYSTEM_ID (object
) >= 0)
8066 if (! SYMBOLP (object
)
8067 || NILP (Fget (object
, Qcoding_system_define_form
)))
8072 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8073 Sread_non_nil_coding_system
, 1, 1, 0,
8074 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8075 (Lisp_Object prompt
)
8080 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8081 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8083 while (SCHARS (val
) == 0);
8084 return (Fintern (val
, Qnil
));
8087 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8088 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8089 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8090 Ignores case when completing coding systems (all Emacs coding systems
8091 are lower-case). */)
8092 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8095 int count
= SPECPDL_INDEX ();
8097 if (SYMBOLP (default_coding_system
))
8098 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8099 specbind (Qcompletion_ignore_case
, Qt
);
8100 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8101 Qt
, Qnil
, Qcoding_system_history
,
8102 default_coding_system
, Qnil
);
8103 unbind_to (count
, Qnil
);
8104 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8107 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8109 doc
: /* Check validity of CODING-SYSTEM.
8110 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8111 It is valid if it is nil or a symbol defined as a coding system by the
8112 function `define-coding-system'. */)
8113 (Lisp_Object coding_system
)
8115 Lisp_Object define_form
;
8117 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8118 if (! NILP (define_form
))
8120 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8121 safe_eval (define_form
);
8123 if (!NILP (Fcoding_system_p (coding_system
)))
8124 return coding_system
;
8125 xsignal1 (Qcoding_system_error
, coding_system
);
8129 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8130 HIGHEST is nonzero, return the coding system of the highest
8131 priority among the detected coding systems. Otherwise return a
8132 list of detected coding systems sorted by their priorities. If
8133 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8134 multibyte form but contains only ASCII and eight-bit chars.
8135 Otherwise, the bytes are raw bytes.
8137 CODING-SYSTEM controls the detection as below:
8139 If it is nil, detect both text-format and eol-format. If the
8140 text-format part of CODING-SYSTEM is already specified
8141 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8142 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8143 detect only text-format. */
8146 detect_coding_system (const unsigned char *src
,
8147 EMACS_INT src_chars
, EMACS_INT src_bytes
,
8148 int highest
, int multibytep
,
8149 Lisp_Object coding_system
)
8151 const unsigned char *src_end
= src
+ src_bytes
;
8152 Lisp_Object attrs
, eol_type
;
8153 Lisp_Object val
= Qnil
;
8154 struct coding_system coding
;
8156 struct coding_detection_info detect_info
;
8157 enum coding_category base_category
;
8158 int null_byte_found
= 0, eight_bit_found
= 0;
8160 if (NILP (coding_system
))
8161 coding_system
= Qundecided
;
8162 setup_coding_system (coding_system
, &coding
);
8163 attrs
= CODING_ID_ATTRS (coding
.id
);
8164 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8165 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8167 coding
.source
= src
;
8168 coding
.src_chars
= src_chars
;
8169 coding
.src_bytes
= src_bytes
;
8170 coding
.src_multibyte
= multibytep
;
8171 coding
.consumed
= 0;
8172 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8173 coding
.head_ascii
= 0;
8175 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8177 /* At first, detect text-format if necessary. */
8178 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8179 if (base_category
== coding_category_undecided
)
8181 enum coding_category category
;
8182 struct coding_system
*this;
8185 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8186 for (; src
< src_end
; src
++)
8191 eight_bit_found
= 1;
8192 if (null_byte_found
)
8197 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8198 && ! inhibit_iso_escape_detection
8199 && ! detect_info
.checked
)
8201 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8203 /* We have scanned the whole data. */
8204 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8206 /* We didn't find an 8-bit code. We may
8207 have found a null-byte, but it's very
8208 rare that a binary file confirm to
8211 coding
.head_ascii
= src
- coding
.source
;
8213 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8217 else if (! c
&& !inhibit_null_byte_detection
)
8219 null_byte_found
= 1;
8220 if (eight_bit_found
)
8223 if (! eight_bit_found
)
8224 coding
.head_ascii
++;
8226 else if (! eight_bit_found
)
8227 coding
.head_ascii
++;
8230 if (null_byte_found
|| eight_bit_found
8231 || coding
.head_ascii
< coding
.src_bytes
8232 || detect_info
.found
)
8234 if (coding
.head_ascii
== coding
.src_bytes
)
8235 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8236 for (i
= 0; i
< coding_category_raw_text
; i
++)
8238 category
= coding_priorities
[i
];
8239 this = coding_categories
+ category
;
8240 if (detect_info
.found
& (1 << category
))
8245 if (null_byte_found
)
8247 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8248 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8250 for (i
= 0; i
< coding_category_raw_text
; i
++)
8252 category
= coding_priorities
[i
];
8253 this = coding_categories
+ category
;
8257 /* No coding system of this category is defined. */
8258 detect_info
.rejected
|= (1 << category
);
8260 else if (category
>= coding_category_raw_text
)
8262 else if (detect_info
.checked
& (1 << category
))
8265 && (detect_info
.found
& (1 << category
)))
8268 else if ((*(this->detector
)) (&coding
, &detect_info
)
8270 && (detect_info
.found
& (1 << category
)))
8272 if (category
== coding_category_utf_16_auto
)
8274 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8275 category
= coding_category_utf_16_le
;
8277 category
= coding_category_utf_16_be
;
8285 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8288 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8289 id
= CODING_SYSTEM_ID (Qno_conversion
);
8290 val
= Fcons (make_number (id
), Qnil
);
8292 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8294 detect_info
.found
= CATEGORY_MASK_ANY
;
8295 id
= coding_categories
[coding_category_undecided
].id
;
8296 val
= Fcons (make_number (id
), Qnil
);
8300 if (detect_info
.found
)
8302 detect_info
.found
= 1 << category
;
8303 val
= Fcons (make_number (this->id
), Qnil
);
8306 for (i
= 0; i
< coding_category_raw_text
; i
++)
8307 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8309 detect_info
.found
= 1 << coding_priorities
[i
];
8310 id
= coding_categories
[coding_priorities
[i
]].id
;
8311 val
= Fcons (make_number (id
), Qnil
);
8317 int mask
= detect_info
.rejected
| detect_info
.found
;
8320 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8322 category
= coding_priorities
[i
];
8323 if (! (mask
& (1 << category
)))
8325 found
|= 1 << category
;
8326 id
= coding_categories
[category
].id
;
8328 val
= Fcons (make_number (id
), val
);
8331 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8333 category
= coding_priorities
[i
];
8334 if (detect_info
.found
& (1 << category
))
8336 id
= coding_categories
[category
].id
;
8337 val
= Fcons (make_number (id
), val
);
8340 detect_info
.found
|= found
;
8343 else if (base_category
== coding_category_utf_8_auto
)
8345 if (detect_coding_utf_8 (&coding
, &detect_info
))
8347 struct coding_system
*this;
8349 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8350 this = coding_categories
+ coding_category_utf_8_sig
;
8352 this = coding_categories
+ coding_category_utf_8_nosig
;
8353 val
= Fcons (make_number (this->id
), Qnil
);
8356 else if (base_category
== coding_category_utf_16_auto
)
8358 if (detect_coding_utf_16 (&coding
, &detect_info
))
8360 struct coding_system
*this;
8362 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8363 this = coding_categories
+ coding_category_utf_16_le
;
8364 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8365 this = coding_categories
+ coding_category_utf_16_be
;
8366 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8367 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8369 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8370 val
= Fcons (make_number (this->id
), Qnil
);
8375 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8376 val
= Fcons (make_number (coding
.id
), Qnil
);
8379 /* Then, detect eol-format if necessary. */
8381 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8384 if (VECTORP (eol_type
))
8386 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8388 if (null_byte_found
)
8389 normal_eol
= EOL_SEEN_LF
;
8391 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8392 coding_category_raw_text
);
8394 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8395 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8396 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8397 coding_category_utf_16_be
);
8398 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8399 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8400 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8401 coding_category_utf_16_le
);
8405 if (EQ (eol_type
, Qunix
))
8406 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8407 else if (EQ (eol_type
, Qdos
))
8408 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8410 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8413 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8415 enum coding_category category
;
8418 id
= XINT (XCAR (tail
));
8419 attrs
= CODING_ID_ATTRS (id
);
8420 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8421 eol_type
= CODING_ID_EOL_TYPE (id
);
8422 if (VECTORP (eol_type
))
8424 if (category
== coding_category_utf_16_be
8425 || category
== coding_category_utf_16_be_nosig
)
8426 this_eol
= utf_16_be_eol
;
8427 else if (category
== coding_category_utf_16_le
8428 || category
== coding_category_utf_16_le_nosig
)
8429 this_eol
= utf_16_le_eol
;
8431 this_eol
= normal_eol
;
8433 if (this_eol
== EOL_SEEN_LF
)
8434 XSETCAR (tail
, AREF (eol_type
, 0));
8435 else if (this_eol
== EOL_SEEN_CRLF
)
8436 XSETCAR (tail
, AREF (eol_type
, 1));
8437 else if (this_eol
== EOL_SEEN_CR
)
8438 XSETCAR (tail
, AREF (eol_type
, 2));
8440 XSETCAR (tail
, CODING_ID_NAME (id
));
8443 XSETCAR (tail
, CODING_ID_NAME (id
));
8447 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8451 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8453 doc
: /* Detect coding system of the text in the region between START and END.
8454 Return a list of possible coding systems ordered by priority.
8455 The coding systems to try and their priorities follows what
8456 the function `coding-system-priority-list' (which see) returns.
8458 If only ASCII characters are found (except for such ISO-2022 control
8459 characters as ESC), it returns a list of single element `undecided'
8460 or its subsidiary coding system according to a detected end-of-line
8463 If optional argument HIGHEST is non-nil, return the coding system of
8464 highest priority. */)
8465 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8468 int from_byte
, to_byte
;
8470 CHECK_NUMBER_COERCE_MARKER (start
);
8471 CHECK_NUMBER_COERCE_MARKER (end
);
8473 validate_region (&start
, &end
);
8474 from
= XINT (start
), to
= XINT (end
);
8475 from_byte
= CHAR_TO_BYTE (from
);
8476 to_byte
= CHAR_TO_BYTE (to
);
8478 if (from
< GPT
&& to
>= GPT
)
8479 move_gap_both (to
, to_byte
);
8481 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8482 to
- from
, to_byte
- from_byte
,
8484 !NILP (current_buffer
8485 ->enable_multibyte_characters
),
8489 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8491 doc
: /* Detect coding system of the text in STRING.
8492 Return a list of possible coding systems ordered by priority.
8493 The coding systems to try and their priorities follows what
8494 the function `coding-system-priority-list' (which see) returns.
8496 If only ASCII characters are found (except for such ISO-2022 control
8497 characters as ESC), it returns a list of single element `undecided'
8498 or its subsidiary coding system according to a detected end-of-line
8501 If optional argument HIGHEST is non-nil, return the coding system of
8502 highest priority. */)
8503 (Lisp_Object string
, Lisp_Object highest
)
8505 CHECK_STRING (string
);
8507 return detect_coding_system (SDATA (string
),
8508 SCHARS (string
), SBYTES (string
),
8509 !NILP (highest
), STRING_MULTIBYTE (string
),
8515 char_encodable_p (int c
, Lisp_Object attrs
)
8518 struct charset
*charset
;
8519 Lisp_Object translation_table
;
8521 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8522 if (! NILP (translation_table
))
8523 c
= translate_char (translation_table
, c
);
8524 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8525 CONSP (tail
); tail
= XCDR (tail
))
8527 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8528 if (CHAR_CHARSET_P (c
, charset
))
8531 return (! NILP (tail
));
8535 /* Return a list of coding systems that safely encode the text between
8536 START and END. If EXCLUDE is non-nil, it is a list of coding
8537 systems not to check. The returned list doesn't contain any such
8538 coding systems. In any case, if the text contains only ASCII or is
8539 unibyte, return t. */
8541 DEFUN ("find-coding-systems-region-internal",
8542 Ffind_coding_systems_region_internal
,
8543 Sfind_coding_systems_region_internal
, 2, 3, 0,
8544 doc
: /* Internal use only. */)
8545 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8547 Lisp_Object coding_attrs_list
, safe_codings
;
8548 EMACS_INT start_byte
, end_byte
;
8549 const unsigned char *p
, *pbeg
, *pend
;
8551 Lisp_Object tail
, elt
, work_table
;
8553 if (STRINGP (start
))
8555 if (!STRING_MULTIBYTE (start
)
8556 || SCHARS (start
) == SBYTES (start
))
8559 end_byte
= SBYTES (start
);
8563 CHECK_NUMBER_COERCE_MARKER (start
);
8564 CHECK_NUMBER_COERCE_MARKER (end
);
8565 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8566 args_out_of_range (start
, end
);
8567 if (NILP (current_buffer
->enable_multibyte_characters
))
8569 start_byte
= CHAR_TO_BYTE (XINT (start
));
8570 end_byte
= CHAR_TO_BYTE (XINT (end
));
8571 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8574 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8576 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8577 move_gap_both (XINT (start
), start_byte
);
8579 move_gap_both (XINT (end
), end_byte
);
8583 coding_attrs_list
= Qnil
;
8584 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8586 || NILP (Fmemq (XCAR (tail
), exclude
)))
8590 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8591 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8592 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8594 ASET (attrs
, coding_attr_trans_tbl
,
8595 get_translation_table (attrs
, 1, NULL
));
8596 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8600 if (STRINGP (start
))
8601 p
= pbeg
= SDATA (start
);
8603 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8604 pend
= p
+ (end_byte
- start_byte
);
8606 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8607 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8609 work_table
= Fmake_char_table (Qnil
, Qnil
);
8612 if (ASCII_BYTE_P (*p
))
8616 c
= STRING_CHAR_ADVANCE (p
);
8617 if (!NILP (char_table_ref (work_table
, c
)))
8618 /* This character was already checked. Ignore it. */
8621 charset_map_loaded
= 0;
8622 for (tail
= coding_attrs_list
; CONSP (tail
);)
8627 else if (char_encodable_p (c
, elt
))
8629 else if (CONSP (XCDR (tail
)))
8631 XSETCAR (tail
, XCAR (XCDR (tail
)));
8632 XSETCDR (tail
, XCDR (XCDR (tail
)));
8636 XSETCAR (tail
, Qnil
);
8640 if (charset_map_loaded
)
8642 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8644 if (STRINGP (start
))
8645 pbeg
= SDATA (start
);
8647 pbeg
= BYTE_POS_ADDR (start_byte
);
8648 p
= pbeg
+ p_offset
;
8649 pend
= pbeg
+ pend_offset
;
8651 char_table_set (work_table
, c
, Qt
);
8655 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8656 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8657 if (! NILP (XCAR (tail
)))
8658 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8660 return safe_codings
;
8664 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8665 Sunencodable_char_position
, 3, 5, 0,
8667 Return position of first un-encodable character in a region.
8668 START and END specify the region and CODING-SYSTEM specifies the
8669 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8671 If optional 4th argument COUNT is non-nil, it specifies at most how
8672 many un-encodable characters to search. In this case, the value is a
8675 If optional 5th argument STRING is non-nil, it is a string to search
8676 for un-encodable characters. In that case, START and END are indexes
8678 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8681 struct coding_system coding
;
8682 Lisp_Object attrs
, charset_list
, translation_table
;
8683 Lisp_Object positions
;
8685 const unsigned char *p
, *stop
, *pend
;
8686 int ascii_compatible
;
8688 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8689 attrs
= CODING_ID_ATTRS (coding
.id
);
8690 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8692 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8693 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8694 translation_table
= get_translation_table (attrs
, 1, NULL
);
8698 validate_region (&start
, &end
);
8699 from
= XINT (start
);
8701 if (NILP (current_buffer
->enable_multibyte_characters
)
8702 || (ascii_compatible
8703 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8705 p
= CHAR_POS_ADDR (from
);
8706 pend
= CHAR_POS_ADDR (to
);
8707 if (from
< GPT
&& to
>= GPT
)
8714 CHECK_STRING (string
);
8715 CHECK_NATNUM (start
);
8717 from
= XINT (start
);
8720 || to
> SCHARS (string
))
8721 args_out_of_range_3 (string
, start
, end
);
8722 if (! STRING_MULTIBYTE (string
))
8724 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8725 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8726 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8734 CHECK_NATNUM (count
);
8743 if (ascii_compatible
)
8744 while (p
< stop
&& ASCII_BYTE_P (*p
))
8754 c
= STRING_CHAR_ADVANCE (p
);
8755 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8756 && ! char_charset (translate_char (translation_table
, c
),
8757 charset_list
, NULL
))
8759 positions
= Fcons (make_number (from
), positions
);
8768 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8772 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8773 Scheck_coding_systems_region
, 3, 3, 0,
8774 doc
: /* Check if the region is encodable by coding systems.
8776 START and END are buffer positions specifying the region.
8777 CODING-SYSTEM-LIST is a list of coding systems to check.
8779 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8780 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8781 whole region, POS0, POS1, ... are buffer positions where non-encodable
8782 characters are found.
8784 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8787 START may be a string. In that case, check if the string is
8788 encodable, and the value contains indices to the string instead of
8789 buffer positions. END is ignored.
8791 If the current buffer (or START if it is a string) is unibyte, the value
8793 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8796 EMACS_INT start_byte
, end_byte
;
8798 const unsigned char *p
, *pbeg
, *pend
;
8800 Lisp_Object tail
, elt
, attrs
;
8802 if (STRINGP (start
))
8804 if (!STRING_MULTIBYTE (start
)
8805 || SCHARS (start
) == SBYTES (start
))
8808 end_byte
= SBYTES (start
);
8813 CHECK_NUMBER_COERCE_MARKER (start
);
8814 CHECK_NUMBER_COERCE_MARKER (end
);
8815 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8816 args_out_of_range (start
, end
);
8817 if (NILP (current_buffer
->enable_multibyte_characters
))
8819 start_byte
= CHAR_TO_BYTE (XINT (start
));
8820 end_byte
= CHAR_TO_BYTE (XINT (end
));
8821 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8824 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8826 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8827 move_gap_both (XINT (start
), start_byte
);
8829 move_gap_both (XINT (end
), end_byte
);
8835 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8838 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8839 ASET (attrs
, coding_attr_trans_tbl
,
8840 get_translation_table (attrs
, 1, NULL
));
8841 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8844 if (STRINGP (start
))
8845 p
= pbeg
= SDATA (start
);
8847 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8848 pend
= p
+ (end_byte
- start_byte
);
8850 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8851 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8855 if (ASCII_BYTE_P (*p
))
8859 c
= STRING_CHAR_ADVANCE (p
);
8861 charset_map_loaded
= 0;
8862 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8864 elt
= XCDR (XCAR (tail
));
8865 if (! char_encodable_p (c
, XCAR (elt
)))
8866 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8868 if (charset_map_loaded
)
8870 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8872 if (STRINGP (start
))
8873 pbeg
= SDATA (start
);
8875 pbeg
= BYTE_POS_ADDR (start_byte
);
8876 p
= pbeg
+ p_offset
;
8877 pend
= pbeg
+ pend_offset
;
8885 for (; CONSP (tail
); tail
= XCDR (tail
))
8888 if (CONSP (XCDR (XCDR (elt
))))
8889 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8898 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8899 Lisp_Object coding_system
, Lisp_Object dst_object
,
8900 int encodep
, int norecord
)
8902 struct coding_system coding
;
8903 EMACS_INT from
, from_byte
, to
, to_byte
;
8904 Lisp_Object src_object
;
8906 CHECK_NUMBER_COERCE_MARKER (start
);
8907 CHECK_NUMBER_COERCE_MARKER (end
);
8908 if (NILP (coding_system
))
8909 coding_system
= Qno_conversion
;
8911 CHECK_CODING_SYSTEM (coding_system
);
8912 src_object
= Fcurrent_buffer ();
8913 if (NILP (dst_object
))
8914 dst_object
= src_object
;
8915 else if (! EQ (dst_object
, Qt
))
8916 CHECK_BUFFER (dst_object
);
8918 validate_region (&start
, &end
);
8919 from
= XFASTINT (start
);
8920 from_byte
= CHAR_TO_BYTE (from
);
8921 to
= XFASTINT (end
);
8922 to_byte
= CHAR_TO_BYTE (to
);
8924 setup_coding_system (coding_system
, &coding
);
8925 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8928 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8931 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8934 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8936 return (BUFFERP (dst_object
)
8937 ? make_number (coding
.produced_char
)
8938 : coding
.dst_object
);
8942 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8943 3, 4, "r\nzCoding system: ",
8944 doc
: /* Decode the current region from the specified coding system.
8945 When called from a program, takes four arguments:
8946 START, END, CODING-SYSTEM, and DESTINATION.
8947 START and END are buffer positions.
8949 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8950 If nil, the region between START and END is replaced by the decoded text.
8951 If buffer, the decoded text is inserted in that buffer after point (point
8953 In those cases, the length of the decoded text is returned.
8954 If DESTINATION is t, the decoded text is returned.
8956 This function sets `last-coding-system-used' to the precise coding system
8957 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8958 not fully specified.) */)
8959 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8961 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8964 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8965 3, 4, "r\nzCoding system: ",
8966 doc
: /* Encode the current region by specified coding system.
8967 When called from a program, takes four arguments:
8968 START, END, CODING-SYSTEM and DESTINATION.
8969 START and END are buffer positions.
8971 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8972 If nil, the region between START and END is replace by the encoded text.
8973 If buffer, the encoded text is inserted in that buffer after point (point
8975 In those cases, the length of the encoded text is returned.
8976 If DESTINATION is t, the encoded text is returned.
8978 This function sets `last-coding-system-used' to the precise coding system
8979 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8980 not fully specified.) */)
8981 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8983 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8987 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8988 Lisp_Object dst_object
, int encodep
, int nocopy
, int norecord
)
8990 struct coding_system coding
;
8991 EMACS_INT chars
, bytes
;
8993 CHECK_STRING (string
);
8994 if (NILP (coding_system
))
8997 Vlast_coding_system_used
= Qno_conversion
;
8998 if (NILP (dst_object
))
8999 return (nocopy
? Fcopy_sequence (string
) : string
);
9002 if (NILP (coding_system
))
9003 coding_system
= Qno_conversion
;
9005 CHECK_CODING_SYSTEM (coding_system
);
9006 if (NILP (dst_object
))
9008 else if (! EQ (dst_object
, Qt
))
9009 CHECK_BUFFER (dst_object
);
9011 setup_coding_system (coding_system
, &coding
);
9012 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9013 chars
= SCHARS (string
);
9014 bytes
= SBYTES (string
);
9016 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9018 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9020 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9022 return (BUFFERP (dst_object
)
9023 ? make_number (coding
.produced_char
)
9024 : coding
.dst_object
);
9028 /* Encode or decode STRING according to CODING_SYSTEM.
9029 Do not set Vlast_coding_system_used.
9031 This function is called only from macros DECODE_FILE and
9032 ENCODE_FILE, thus we ignore character composition. */
9035 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9038 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9042 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9044 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9046 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9047 if the decoding operation is trivial.
9049 Optional fourth arg BUFFER non-nil means that the decoded text is
9050 inserted in that buffer after point (point does not move). In this
9051 case, the return value is the length of the decoded text.
9053 This function sets `last-coding-system-used' to the precise coding system
9054 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9055 not fully specified.) */)
9056 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9058 return code_convert_string (string
, coding_system
, buffer
,
9059 0, ! NILP (nocopy
), 0);
9062 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9064 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9066 Optional third arg NOCOPY non-nil means it is OK to return STRING
9067 itself if the encoding operation is trivial.
9069 Optional fourth arg BUFFER non-nil means that the encoded text is
9070 inserted in that buffer after point (point does not move). In this
9071 case, the return value is the length of the encoded text.
9073 This function sets `last-coding-system-used' to the precise coding system
9074 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9075 not fully specified.) */)
9076 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9078 return code_convert_string (string
, coding_system
, buffer
,
9079 1, ! NILP (nocopy
), 1);
9083 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9084 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9085 Return the corresponding character. */)
9088 Lisp_Object spec
, attrs
, val
;
9089 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9092 CHECK_NATNUM (code
);
9093 c
= XFASTINT (code
);
9094 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9095 attrs
= AREF (spec
, 0);
9097 if (ASCII_BYTE_P (c
)
9098 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9101 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9102 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9103 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9104 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9107 charset
= charset_roman
;
9108 else if (c
>= 0xA0 && c
< 0xDF)
9110 charset
= charset_kana
;
9115 int s1
= c
>> 8, s2
= c
& 0xFF;
9117 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
9118 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
9119 error ("Invalid code: %d", code
);
9121 charset
= charset_kanji
;
9123 c
= DECODE_CHAR (charset
, c
);
9125 error ("Invalid code: %d", code
);
9126 return make_number (c
);
9130 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9131 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9132 Return the corresponding code in SJIS. */)
9135 Lisp_Object spec
, attrs
, charset_list
;
9137 struct charset
*charset
;
9140 CHECK_CHARACTER (ch
);
9142 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9143 attrs
= AREF (spec
, 0);
9145 if (ASCII_CHAR_P (c
)
9146 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9149 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9150 charset
= char_charset (c
, charset_list
, &code
);
9151 if (code
== CHARSET_INVALID_CODE (charset
))
9152 error ("Can't encode by shift_jis encoding: %d", c
);
9155 return make_number (code
);
9158 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9159 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9160 Return the corresponding character. */)
9163 Lisp_Object spec
, attrs
, val
;
9164 struct charset
*charset_roman
, *charset_big5
, *charset
;
9167 CHECK_NATNUM (code
);
9168 c
= XFASTINT (code
);
9169 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9170 attrs
= AREF (spec
, 0);
9172 if (ASCII_BYTE_P (c
)
9173 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9176 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9177 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9178 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9181 charset
= charset_roman
;
9184 int b1
= c
>> 8, b2
= c
& 0x7F;
9185 if (b1
< 0xA1 || b1
> 0xFE
9186 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9187 error ("Invalid code: %d", code
);
9188 charset
= charset_big5
;
9190 c
= DECODE_CHAR (charset
, (unsigned )c
);
9192 error ("Invalid code: %d", code
);
9193 return make_number (c
);
9196 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9197 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9198 Return the corresponding character code in Big5. */)
9201 Lisp_Object spec
, attrs
, charset_list
;
9202 struct charset
*charset
;
9206 CHECK_CHARACTER (ch
);
9208 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9209 attrs
= AREF (spec
, 0);
9210 if (ASCII_CHAR_P (c
)
9211 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9214 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9215 charset
= char_charset (c
, charset_list
, &code
);
9216 if (code
== CHARSET_INVALID_CODE (charset
))
9217 error ("Can't encode by Big5 encoding: %d", c
);
9219 return make_number (code
);
9223 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9224 Sset_terminal_coding_system_internal
, 1, 2, 0,
9225 doc
: /* Internal use only. */)
9226 (Lisp_Object coding_system
, Lisp_Object terminal
)
9228 struct terminal
*term
= get_terminal (terminal
, 1);
9229 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9230 CHECK_SYMBOL (coding_system
);
9231 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9232 /* We had better not send unsafe characters to terminal. */
9233 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9234 /* Character composition should be disabled. */
9235 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9236 terminal_coding
->src_multibyte
= 1;
9237 terminal_coding
->dst_multibyte
= 0;
9238 if (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
)
9239 term
->charset_list
= coding_charset_list (terminal_coding
);
9241 term
->charset_list
= Fcons (make_number (charset_ascii
), Qnil
);
9245 DEFUN ("set-safe-terminal-coding-system-internal",
9246 Fset_safe_terminal_coding_system_internal
,
9247 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9248 doc
: /* Internal use only. */)
9249 (Lisp_Object coding_system
)
9251 CHECK_SYMBOL (coding_system
);
9252 setup_coding_system (Fcheck_coding_system (coding_system
),
9253 &safe_terminal_coding
);
9254 /* Character composition should be disabled. */
9255 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9256 safe_terminal_coding
.src_multibyte
= 1;
9257 safe_terminal_coding
.dst_multibyte
= 0;
9261 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9262 Sterminal_coding_system
, 0, 1, 0,
9263 doc
: /* Return coding system specified for terminal output on the given terminal.
9264 TERMINAL may be a terminal object, a frame, or nil for the selected
9265 frame's terminal device. */)
9266 (Lisp_Object terminal
)
9268 struct coding_system
*terminal_coding
9269 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9270 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9272 /* For backward compatibility, return nil if it is `undecided'. */
9273 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9276 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9277 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9278 doc
: /* Internal use only. */)
9279 (Lisp_Object coding_system
, Lisp_Object terminal
)
9281 struct terminal
*t
= get_terminal (terminal
, 1);
9282 CHECK_SYMBOL (coding_system
);
9283 if (NILP (coding_system
))
9284 coding_system
= Qno_conversion
;
9286 Fcheck_coding_system (coding_system
);
9287 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9288 /* Character composition should be disabled. */
9289 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9290 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9294 DEFUN ("keyboard-coding-system",
9295 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9296 doc
: /* Return coding system specified for decoding keyboard input. */)
9297 (Lisp_Object terminal
)
9299 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9300 (get_terminal (terminal
, 1))->id
);
9304 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9305 Sfind_operation_coding_system
, 1, MANY
, 0,
9306 doc
: /* Choose a coding system for an operation based on the target name.
9307 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9308 DECODING-SYSTEM is the coding system to use for decoding
9309 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9310 for encoding (in case OPERATION does encoding).
9312 The first argument OPERATION specifies an I/O primitive:
9313 For file I/O, `insert-file-contents' or `write-region'.
9314 For process I/O, `call-process', `call-process-region', or `start-process'.
9315 For network I/O, `open-network-stream'.
9317 The remaining arguments should be the same arguments that were passed
9318 to the primitive. Depending on which primitive, one of those arguments
9319 is selected as the TARGET. For example, if OPERATION does file I/O,
9320 whichever argument specifies the file name is TARGET.
9322 TARGET has a meaning which depends on OPERATION:
9323 For file I/O, TARGET is a file name (except for the special case below).
9324 For process I/O, TARGET is a process name.
9325 For network I/O, TARGET is a service name or a port number.
9327 This function looks up what is specified for TARGET in
9328 `file-coding-system-alist', `process-coding-system-alist',
9329 or `network-coding-system-alist' depending on OPERATION.
9330 They may specify a coding system, a cons of coding systems,
9331 or a function symbol to call.
9332 In the last case, we call the function with one argument,
9333 which is a list of all the arguments given to this function.
9334 If the function can't decide a coding system, it can return
9335 `undecided' so that the normal code-detection is performed.
9337 If OPERATION is `insert-file-contents', the argument corresponding to
9338 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9339 file name to look up, and BUFFER is a buffer that contains the file's
9340 contents (not yet decoded). If `file-coding-system-alist' specifies a
9341 function to call for FILENAME, that function should examine the
9342 contents of BUFFER instead of reading the file.
9344 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9345 (int nargs
, Lisp_Object
*args
)
9347 Lisp_Object operation
, target_idx
, target
, val
;
9348 register Lisp_Object chain
;
9351 error ("Too few arguments");
9352 operation
= args
[0];
9353 if (!SYMBOLP (operation
)
9354 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
9355 error ("Invalid first argument");
9356 if (nargs
< 1 + XINT (target_idx
))
9357 error ("Too few arguments for operation: %s",
9358 SDATA (SYMBOL_NAME (operation
)));
9359 target
= args
[XINT (target_idx
) + 1];
9360 if (!(STRINGP (target
)
9361 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9362 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9363 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9364 error ("Invalid %dth argument", XINT (target_idx
) + 1);
9366 target
= XCAR (target
);
9368 chain
= ((EQ (operation
, Qinsert_file_contents
)
9369 || EQ (operation
, Qwrite_region
))
9370 ? Vfile_coding_system_alist
9371 : (EQ (operation
, Qopen_network_stream
)
9372 ? Vnetwork_coding_system_alist
9373 : Vprocess_coding_system_alist
));
9377 for (; CONSP (chain
); chain
= XCDR (chain
))
9383 && ((STRINGP (target
)
9384 && STRINGP (XCAR (elt
))
9385 && fast_string_match (XCAR (elt
), target
) >= 0)
9386 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9389 /* Here, if VAL is both a valid coding system and a valid
9390 function symbol, we return VAL as a coding system. */
9393 if (! SYMBOLP (val
))
9395 if (! NILP (Fcoding_system_p (val
)))
9396 return Fcons (val
, val
);
9397 if (! NILP (Ffboundp (val
)))
9399 /* We use call1 rather than safe_call1
9400 so as to get bug reports about functions called here
9401 which don't handle the current interface. */
9402 val
= call1 (val
, Flist (nargs
, args
));
9405 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9406 return Fcons (val
, val
);
9414 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9415 Sset_coding_system_priority
, 0, MANY
, 0,
9416 doc
: /* Assign higher priority to the coding systems given as arguments.
9417 If multiple coding systems belong to the same category,
9418 all but the first one are ignored.
9420 usage: (set-coding-system-priority &rest coding-systems) */)
9421 (int nargs
, Lisp_Object
*args
)
9424 int changed
[coding_category_max
];
9425 enum coding_category priorities
[coding_category_max
];
9427 memset (changed
, 0, sizeof changed
);
9429 for (i
= j
= 0; i
< nargs
; i
++)
9431 enum coding_category category
;
9432 Lisp_Object spec
, attrs
;
9434 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9435 attrs
= AREF (spec
, 0);
9436 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9437 if (changed
[category
])
9438 /* Ignore this coding system because a coding system of the
9439 same category already had a higher priority. */
9441 changed
[category
] = 1;
9442 priorities
[j
++] = category
;
9443 if (coding_categories
[category
].id
>= 0
9444 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9445 setup_coding_system (args
[i
], &coding_categories
[category
]);
9446 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9449 /* Now we have decided top J priorities. Reflect the order of the
9450 original priorities to the remaining priorities. */
9452 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9454 while (j
< coding_category_max
9455 && changed
[coding_priorities
[j
]])
9457 if (j
== coding_category_max
)
9459 priorities
[i
] = coding_priorities
[j
];
9462 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9464 /* Update `coding-category-list'. */
9465 Vcoding_category_list
= Qnil
;
9466 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9467 Vcoding_category_list
9468 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9469 Vcoding_category_list
);
9474 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9475 Scoding_system_priority_list
, 0, 1, 0,
9476 doc
: /* Return a list of coding systems ordered by their priorities.
9477 The list contains a subset of coding systems; i.e. coding systems
9478 assigned to each coding category (see `coding-category-list').
9480 HIGHESTP non-nil means just return the highest priority one. */)
9481 (Lisp_Object highestp
)
9486 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9488 enum coding_category category
= coding_priorities
[i
];
9489 int id
= coding_categories
[category
].id
;
9494 attrs
= CODING_ID_ATTRS (id
);
9495 if (! NILP (highestp
))
9496 return CODING_ATTR_BASE_NAME (attrs
);
9497 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9499 return Fnreverse (val
);
9502 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9505 make_subsidiaries (Lisp_Object base
)
9507 Lisp_Object subsidiaries
;
9508 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9509 char *buf
= (char *) alloca (base_name_len
+ 6);
9512 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9513 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9514 for (i
= 0; i
< 3; i
++)
9516 memcpy (buf
+ base_name_len
, suffixes
[i
], strlen (suffixes
[i
]) + 1);
9517 ASET (subsidiaries
, i
, intern (buf
));
9519 return subsidiaries
;
9523 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9524 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9525 doc
: /* For internal use only.
9526 usage: (define-coding-system-internal ...) */)
9527 (int nargs
, Lisp_Object
*args
)
9530 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9531 Lisp_Object attrs
; /* Vector of attributes. */
9532 Lisp_Object eol_type
;
9533 Lisp_Object aliases
;
9534 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9535 enum coding_category category
;
9536 Lisp_Object tail
, val
;
9537 int max_charset_id
= 0;
9540 if (nargs
< coding_arg_max
)
9543 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9545 name
= args
[coding_arg_name
];
9546 CHECK_SYMBOL (name
);
9547 CODING_ATTR_BASE_NAME (attrs
) = name
;
9549 val
= args
[coding_arg_mnemonic
];
9550 if (! STRINGP (val
))
9551 CHECK_CHARACTER (val
);
9552 CODING_ATTR_MNEMONIC (attrs
) = val
;
9554 coding_type
= args
[coding_arg_coding_type
];
9555 CHECK_SYMBOL (coding_type
);
9556 CODING_ATTR_TYPE (attrs
) = coding_type
;
9558 charset_list
= args
[coding_arg_charset_list
];
9559 if (SYMBOLP (charset_list
))
9561 if (EQ (charset_list
, Qiso_2022
))
9563 if (! EQ (coding_type
, Qiso_2022
))
9564 error ("Invalid charset-list");
9565 charset_list
= Viso_2022_charset_list
;
9567 else if (EQ (charset_list
, Qemacs_mule
))
9569 if (! EQ (coding_type
, Qemacs_mule
))
9570 error ("Invalid charset-list");
9571 charset_list
= Vemacs_mule_charset_list
;
9573 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9574 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9575 max_charset_id
= XFASTINT (XCAR (tail
));
9579 charset_list
= Fcopy_sequence (charset_list
);
9580 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9582 struct charset
*charset
;
9585 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9586 if (EQ (coding_type
, Qiso_2022
)
9587 ? CHARSET_ISO_FINAL (charset
) < 0
9588 : EQ (coding_type
, Qemacs_mule
)
9589 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9591 error ("Can't handle charset `%s'",
9592 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9594 XSETCAR (tail
, make_number (charset
->id
));
9595 if (max_charset_id
< charset
->id
)
9596 max_charset_id
= charset
->id
;
9599 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9601 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9602 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9603 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9604 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9605 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9607 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9609 val
= args
[coding_arg_decode_translation_table
];
9610 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9612 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9614 val
= args
[coding_arg_encode_translation_table
];
9615 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9617 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9619 val
= args
[coding_arg_post_read_conversion
];
9621 CODING_ATTR_POST_READ (attrs
) = val
;
9623 val
= args
[coding_arg_pre_write_conversion
];
9625 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9627 val
= args
[coding_arg_default_char
];
9629 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9632 CHECK_CHARACTER (val
);
9633 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9636 val
= args
[coding_arg_for_unibyte
];
9637 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9639 val
= args
[coding_arg_plist
];
9641 CODING_ATTR_PLIST (attrs
) = val
;
9643 if (EQ (coding_type
, Qcharset
))
9645 /* Generate a lisp vector of 256 elements. Each element is nil,
9646 integer, or a list of charset IDs.
9648 If Nth element is nil, the byte code N is invalid in this
9651 If Nth element is a number NUM, N is the first byte of a
9652 charset whose ID is NUM.
9654 If Nth element is a list of charset IDs, N is the first byte
9655 of one of them. The list is sorted by dimensions of the
9656 charsets. A charset of smaller dimension comes first. */
9657 val
= Fmake_vector (make_number (256), Qnil
);
9659 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9661 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9662 int dim
= CHARSET_DIMENSION (charset
);
9663 int idx
= (dim
- 1) * 4;
9665 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9666 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9668 for (i
= charset
->code_space
[idx
];
9669 i
<= charset
->code_space
[idx
+ 1]; i
++)
9671 Lisp_Object tmp
, tmp2
;
9674 tmp
= AREF (val
, i
);
9677 else if (NUMBERP (tmp
))
9679 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9681 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9683 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9687 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9689 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9694 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9697 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9698 XSETCAR (tmp2
, XCAR (tail
));
9704 ASET (attrs
, coding_attr_charset_valids
, val
);
9705 category
= coding_category_charset
;
9707 else if (EQ (coding_type
, Qccl
))
9711 if (nargs
< coding_arg_ccl_max
)
9714 val
= args
[coding_arg_ccl_decoder
];
9715 CHECK_CCL_PROGRAM (val
);
9717 val
= Fcopy_sequence (val
);
9718 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9720 val
= args
[coding_arg_ccl_encoder
];
9721 CHECK_CCL_PROGRAM (val
);
9723 val
= Fcopy_sequence (val
);
9724 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9726 val
= args
[coding_arg_ccl_valids
];
9727 valids
= Fmake_string (make_number (256), make_number (0));
9728 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9735 from
= to
= XINT (val
);
9736 if (from
< 0 || from
> 255)
9737 args_out_of_range_3 (val
, make_number (0), make_number (255));
9742 CHECK_NATNUM_CAR (val
);
9743 CHECK_NATNUM_CDR (val
);
9744 from
= XINT (XCAR (val
));
9746 args_out_of_range_3 (XCAR (val
),
9747 make_number (0), make_number (255));
9748 to
= XINT (XCDR (val
));
9749 if (to
< from
|| to
> 255)
9750 args_out_of_range_3 (XCDR (val
),
9751 XCAR (val
), make_number (255));
9753 for (i
= from
; i
<= to
; i
++)
9754 SSET (valids
, i
, 1);
9756 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9758 category
= coding_category_ccl
;
9760 else if (EQ (coding_type
, Qutf_16
))
9762 Lisp_Object bom
, endian
;
9764 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9766 if (nargs
< coding_arg_utf16_max
)
9769 bom
= args
[coding_arg_utf16_bom
];
9770 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9774 CHECK_CODING_SYSTEM (val
);
9776 CHECK_CODING_SYSTEM (val
);
9778 ASET (attrs
, coding_attr_utf_bom
, bom
);
9780 endian
= args
[coding_arg_utf16_endian
];
9781 CHECK_SYMBOL (endian
);
9784 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9785 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9786 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9788 category
= (CONSP (bom
)
9789 ? coding_category_utf_16_auto
9791 ? (EQ (endian
, Qbig
)
9792 ? coding_category_utf_16_be_nosig
9793 : coding_category_utf_16_le_nosig
)
9794 : (EQ (endian
, Qbig
)
9795 ? coding_category_utf_16_be
9796 : coding_category_utf_16_le
));
9798 else if (EQ (coding_type
, Qiso_2022
))
9800 Lisp_Object initial
, reg_usage
, request
, flags
;
9803 if (nargs
< coding_arg_iso2022_max
)
9806 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9807 CHECK_VECTOR (initial
);
9808 for (i
= 0; i
< 4; i
++)
9810 val
= Faref (initial
, make_number (i
));
9813 struct charset
*charset
;
9815 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9816 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9817 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9818 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9821 ASET (initial
, i
, make_number (-1));
9824 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9825 CHECK_CONS (reg_usage
);
9826 CHECK_NUMBER_CAR (reg_usage
);
9827 CHECK_NUMBER_CDR (reg_usage
);
9829 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9830 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9838 CHECK_CHARSET_GET_ID (tmp
, id
);
9839 CHECK_NATNUM_CDR (val
);
9840 if (XINT (XCDR (val
)) >= 4)
9841 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9842 XSETCAR (val
, make_number (id
));
9845 flags
= args
[coding_arg_iso2022_flags
];
9846 CHECK_NATNUM (flags
);
9848 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9849 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9851 ASET (attrs
, coding_attr_iso_initial
, initial
);
9852 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9853 ASET (attrs
, coding_attr_iso_request
, request
);
9854 ASET (attrs
, coding_attr_iso_flags
, flags
);
9855 setup_iso_safe_charsets (attrs
);
9857 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9858 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9859 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9860 ? coding_category_iso_7_else
9861 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9862 ? coding_category_iso_7
9863 : coding_category_iso_7_tight
);
9866 int id
= XINT (AREF (initial
, 1));
9868 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9869 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9871 ? coding_category_iso_8_else
9872 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9873 ? coding_category_iso_8_1
9874 : coding_category_iso_8_2
);
9876 if (category
!= coding_category_iso_8_1
9877 && category
!= coding_category_iso_8_2
)
9878 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9880 else if (EQ (coding_type
, Qemacs_mule
))
9882 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9883 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9884 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9885 category
= coding_category_emacs_mule
;
9887 else if (EQ (coding_type
, Qshift_jis
))
9890 struct charset
*charset
;
9892 if (XINT (Flength (charset_list
)) != 3
9893 && XINT (Flength (charset_list
)) != 4)
9894 error ("There should be three or four charsets");
9896 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9897 if (CHARSET_DIMENSION (charset
) != 1)
9898 error ("Dimension of charset %s is not one",
9899 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9900 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9901 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9903 charset_list
= XCDR (charset_list
);
9904 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9905 if (CHARSET_DIMENSION (charset
) != 1)
9906 error ("Dimension of charset %s is not one",
9907 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9909 charset_list
= XCDR (charset_list
);
9910 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9911 if (CHARSET_DIMENSION (charset
) != 2)
9912 error ("Dimension of charset %s is not two",
9913 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9915 charset_list
= XCDR (charset_list
);
9916 if (! NILP (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
))));
9924 category
= coding_category_sjis
;
9925 Vsjis_coding_system
= name
;
9927 else if (EQ (coding_type
, Qbig5
))
9929 struct charset
*charset
;
9931 if (XINT (Flength (charset_list
)) != 2)
9932 error ("There should be just two charsets");
9934 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9935 if (CHARSET_DIMENSION (charset
) != 1)
9936 error ("Dimension of charset %s is not one",
9937 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9938 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9939 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9941 charset_list
= XCDR (charset_list
);
9942 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9943 if (CHARSET_DIMENSION (charset
) != 2)
9944 error ("Dimension of charset %s is not two",
9945 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9947 category
= coding_category_big5
;
9948 Vbig5_coding_system
= name
;
9950 else if (EQ (coding_type
, Qraw_text
))
9952 category
= coding_category_raw_text
;
9953 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9955 else if (EQ (coding_type
, Qutf_8
))
9959 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9961 if (nargs
< coding_arg_utf8_max
)
9964 bom
= args
[coding_arg_utf8_bom
];
9965 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9969 CHECK_CODING_SYSTEM (val
);
9971 CHECK_CODING_SYSTEM (val
);
9973 ASET (attrs
, coding_attr_utf_bom
, bom
);
9975 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9976 : NILP (bom
) ? coding_category_utf_8_nosig
9977 : coding_category_utf_8_sig
);
9979 else if (EQ (coding_type
, Qundecided
))
9980 category
= coding_category_undecided
;
9982 error ("Invalid coding system type: %s",
9983 SDATA (SYMBOL_NAME (coding_type
)));
9985 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9986 CODING_ATTR_PLIST (attrs
)
9987 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9988 CODING_ATTR_PLIST (attrs
)));
9989 CODING_ATTR_PLIST (attrs
)
9990 = Fcons (QCascii_compatible_p
,
9991 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9992 CODING_ATTR_PLIST (attrs
)));
9994 eol_type
= args
[coding_arg_eol_type
];
9995 if (! NILP (eol_type
)
9996 && ! EQ (eol_type
, Qunix
)
9997 && ! EQ (eol_type
, Qdos
)
9998 && ! EQ (eol_type
, Qmac
))
9999 error ("Invalid eol-type");
10001 aliases
= Fcons (name
, Qnil
);
10003 if (NILP (eol_type
))
10005 eol_type
= make_subsidiaries (name
);
10006 for (i
= 0; i
< 3; i
++)
10008 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10010 this_name
= AREF (eol_type
, i
);
10011 this_aliases
= Fcons (this_name
, Qnil
);
10012 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10013 this_spec
= Fmake_vector (make_number (3), attrs
);
10014 ASET (this_spec
, 1, this_aliases
);
10015 ASET (this_spec
, 2, this_eol_type
);
10016 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10017 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10018 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10020 Vcoding_system_alist
10021 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10022 Vcoding_system_alist
);
10026 spec_vec
= Fmake_vector (make_number (3), attrs
);
10027 ASET (spec_vec
, 1, aliases
);
10028 ASET (spec_vec
, 2, eol_type
);
10030 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10031 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10032 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10034 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10035 Vcoding_system_alist
);
10038 int id
= coding_categories
[category
].id
;
10040 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10041 setup_coding_system (name
, &coding_categories
[category
]);
10047 return Fsignal (Qwrong_number_of_arguments
,
10048 Fcons (intern ("define-coding-system-internal"),
10049 make_number (nargs
)));
10053 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10055 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10056 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10058 Lisp_Object spec
, attrs
;
10060 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10061 attrs
= AREF (spec
, 0);
10062 if (EQ (prop
, QCmnemonic
))
10064 if (! STRINGP (val
))
10065 CHECK_CHARACTER (val
);
10066 CODING_ATTR_MNEMONIC (attrs
) = val
;
10068 else if (EQ (prop
, QCdefault_char
))
10071 val
= make_number (' ');
10073 CHECK_CHARACTER (val
);
10074 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10076 else if (EQ (prop
, QCdecode_translation_table
))
10078 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10079 CHECK_SYMBOL (val
);
10080 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10082 else if (EQ (prop
, QCencode_translation_table
))
10084 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10085 CHECK_SYMBOL (val
);
10086 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10088 else if (EQ (prop
, QCpost_read_conversion
))
10090 CHECK_SYMBOL (val
);
10091 CODING_ATTR_POST_READ (attrs
) = val
;
10093 else if (EQ (prop
, QCpre_write_conversion
))
10095 CHECK_SYMBOL (val
);
10096 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10098 else if (EQ (prop
, QCascii_compatible_p
))
10100 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10103 CODING_ATTR_PLIST (attrs
)
10104 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10109 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10110 Sdefine_coding_system_alias
, 2, 2, 0,
10111 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10112 (Lisp_Object alias
, Lisp_Object coding_system
)
10114 Lisp_Object spec
, aliases
, eol_type
, val
;
10116 CHECK_SYMBOL (alias
);
10117 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10118 aliases
= AREF (spec
, 1);
10119 /* ALIASES should be a list of length more than zero, and the first
10120 element is a base coding system. Append ALIAS at the tail of the
10122 while (!NILP (XCDR (aliases
)))
10123 aliases
= XCDR (aliases
);
10124 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10126 eol_type
= AREF (spec
, 2);
10127 if (VECTORP (eol_type
))
10129 Lisp_Object subsidiaries
;
10132 subsidiaries
= make_subsidiaries (alias
);
10133 for (i
= 0; i
< 3; i
++)
10134 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10135 AREF (eol_type
, i
));
10138 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10139 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10140 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10142 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10143 Vcoding_system_alist
);
10148 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10150 doc
: /* Return the base of CODING-SYSTEM.
10151 Any alias or subsidiary coding system is not a base coding system. */)
10152 (Lisp_Object coding_system
)
10154 Lisp_Object spec
, attrs
;
10156 if (NILP (coding_system
))
10157 return (Qno_conversion
);
10158 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10159 attrs
= AREF (spec
, 0);
10160 return CODING_ATTR_BASE_NAME (attrs
);
10163 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10165 doc
: "Return the property list of CODING-SYSTEM.")
10166 (Lisp_Object coding_system
)
10168 Lisp_Object spec
, attrs
;
10170 if (NILP (coding_system
))
10171 coding_system
= Qno_conversion
;
10172 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10173 attrs
= AREF (spec
, 0);
10174 return CODING_ATTR_PLIST (attrs
);
10178 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10180 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10181 (Lisp_Object coding_system
)
10185 if (NILP (coding_system
))
10186 coding_system
= Qno_conversion
;
10187 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10188 return AREF (spec
, 1);
10191 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10192 Scoding_system_eol_type
, 1, 1, 0,
10193 doc
: /* Return eol-type of CODING-SYSTEM.
10194 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10196 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10197 and CR respectively.
10199 A vector value indicates that a format of end-of-line should be
10200 detected automatically. Nth element of the vector is the subsidiary
10201 coding system whose eol-type is N. */)
10202 (Lisp_Object coding_system
)
10204 Lisp_Object spec
, eol_type
;
10207 if (NILP (coding_system
))
10208 coding_system
= Qno_conversion
;
10209 if (! CODING_SYSTEM_P (coding_system
))
10211 spec
= CODING_SYSTEM_SPEC (coding_system
);
10212 eol_type
= AREF (spec
, 2);
10213 if (VECTORP (eol_type
))
10214 return Fcopy_sequence (eol_type
);
10215 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10216 return make_number (n
);
10222 /*** 9. Post-amble ***/
10225 init_coding_once (void)
10229 for (i
= 0; i
< coding_category_max
; i
++)
10231 coding_categories
[i
].id
= -1;
10232 coding_priorities
[i
] = i
;
10235 /* ISO2022 specific initialize routine. */
10236 for (i
= 0; i
< 0x20; i
++)
10237 iso_code_class
[i
] = ISO_control_0
;
10238 for (i
= 0x21; i
< 0x7F; i
++)
10239 iso_code_class
[i
] = ISO_graphic_plane_0
;
10240 for (i
= 0x80; i
< 0xA0; i
++)
10241 iso_code_class
[i
] = ISO_control_1
;
10242 for (i
= 0xA1; i
< 0xFF; i
++)
10243 iso_code_class
[i
] = ISO_graphic_plane_1
;
10244 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10245 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10246 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10247 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10248 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10249 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10250 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10251 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10252 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10254 for (i
= 0; i
< 256; i
++)
10256 emacs_mule_bytes
[i
] = 1;
10258 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10259 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10260 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10261 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10267 syms_of_coding (void)
10269 staticpro (&Vcoding_system_hash_table
);
10271 Lisp_Object args
[2];
10274 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10277 staticpro (&Vsjis_coding_system
);
10278 Vsjis_coding_system
= Qnil
;
10280 staticpro (&Vbig5_coding_system
);
10281 Vbig5_coding_system
= Qnil
;
10283 staticpro (&Vcode_conversion_reused_workbuf
);
10284 Vcode_conversion_reused_workbuf
= Qnil
;
10286 staticpro (&Vcode_conversion_workbuf_name
);
10287 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10289 reused_workbuf_in_use
= 0;
10291 DEFSYM (Qcharset
, "charset");
10292 DEFSYM (Qtarget_idx
, "target-idx");
10293 DEFSYM (Qcoding_system_history
, "coding-system-history");
10294 Fset (Qcoding_system_history
, Qnil
);
10296 /* Target FILENAME is the first argument. */
10297 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10298 /* Target FILENAME is the third argument. */
10299 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10301 DEFSYM (Qcall_process
, "call-process");
10302 /* Target PROGRAM is the first argument. */
10303 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10305 DEFSYM (Qcall_process_region
, "call-process-region");
10306 /* Target PROGRAM is the third argument. */
10307 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10309 DEFSYM (Qstart_process
, "start-process");
10310 /* Target PROGRAM is the third argument. */
10311 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10313 DEFSYM (Qopen_network_stream
, "open-network-stream");
10314 /* Target SERVICE is the fourth argument. */
10315 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10317 DEFSYM (Qcoding_system
, "coding-system");
10318 DEFSYM (Qcoding_aliases
, "coding-aliases");
10320 DEFSYM (Qeol_type
, "eol-type");
10321 DEFSYM (Qunix
, "unix");
10322 DEFSYM (Qdos
, "dos");
10324 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10325 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10326 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10327 DEFSYM (Qdefault_char
, "default-char");
10328 DEFSYM (Qundecided
, "undecided");
10329 DEFSYM (Qno_conversion
, "no-conversion");
10330 DEFSYM (Qraw_text
, "raw-text");
10332 DEFSYM (Qiso_2022
, "iso-2022");
10334 DEFSYM (Qutf_8
, "utf-8");
10335 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10337 DEFSYM (Qutf_16
, "utf-16");
10338 DEFSYM (Qbig
, "big");
10339 DEFSYM (Qlittle
, "little");
10341 DEFSYM (Qshift_jis
, "shift-jis");
10342 DEFSYM (Qbig5
, "big5");
10344 DEFSYM (Qcoding_system_p
, "coding-system-p");
10346 DEFSYM (Qcoding_system_error
, "coding-system-error");
10347 Fput (Qcoding_system_error
, Qerror_conditions
,
10348 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10349 Fput (Qcoding_system_error
, Qerror_message
,
10350 make_pure_c_string ("Invalid coding system"));
10352 /* Intern this now in case it isn't already done.
10353 Setting this variable twice is harmless.
10354 But don't staticpro it here--that is done in alloc.c. */
10355 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10357 DEFSYM (Qtranslation_table
, "translation-table");
10358 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10359 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10360 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10361 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10363 DEFSYM (Qvalid_codes
, "valid-codes");
10365 DEFSYM (Qemacs_mule
, "emacs-mule");
10367 DEFSYM (QCcategory
, ":category");
10368 DEFSYM (QCmnemonic
, ":mnemonic");
10369 DEFSYM (QCdefault_char
, ":default-char");
10370 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10371 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10372 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10373 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10374 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10376 Vcoding_category_table
10377 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10378 staticpro (&Vcoding_category_table
);
10379 /* Followings are target of code detection. */
10380 ASET (Vcoding_category_table
, coding_category_iso_7
,
10381 intern_c_string ("coding-category-iso-7"));
10382 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10383 intern_c_string ("coding-category-iso-7-tight"));
10384 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10385 intern_c_string ("coding-category-iso-8-1"));
10386 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10387 intern_c_string ("coding-category-iso-8-2"));
10388 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10389 intern_c_string ("coding-category-iso-7-else"));
10390 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10391 intern_c_string ("coding-category-iso-8-else"));
10392 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10393 intern_c_string ("coding-category-utf-8-auto"));
10394 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10395 intern_c_string ("coding-category-utf-8"));
10396 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10397 intern_c_string ("coding-category-utf-8-sig"));
10398 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10399 intern_c_string ("coding-category-utf-16-be"));
10400 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10401 intern_c_string ("coding-category-utf-16-auto"));
10402 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10403 intern_c_string ("coding-category-utf-16-le"));
10404 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10405 intern_c_string ("coding-category-utf-16-be-nosig"));
10406 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10407 intern_c_string ("coding-category-utf-16-le-nosig"));
10408 ASET (Vcoding_category_table
, coding_category_charset
,
10409 intern_c_string ("coding-category-charset"));
10410 ASET (Vcoding_category_table
, coding_category_sjis
,
10411 intern_c_string ("coding-category-sjis"));
10412 ASET (Vcoding_category_table
, coding_category_big5
,
10413 intern_c_string ("coding-category-big5"));
10414 ASET (Vcoding_category_table
, coding_category_ccl
,
10415 intern_c_string ("coding-category-ccl"));
10416 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10417 intern_c_string ("coding-category-emacs-mule"));
10418 /* Followings are NOT target of code detection. */
10419 ASET (Vcoding_category_table
, coding_category_raw_text
,
10420 intern_c_string ("coding-category-raw-text"));
10421 ASET (Vcoding_category_table
, coding_category_undecided
,
10422 intern_c_string ("coding-category-undecided"));
10424 DEFSYM (Qinsufficient_source
, "insufficient-source");
10425 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10426 DEFSYM (Qinvalid_source
, "invalid-source");
10427 DEFSYM (Qinterrupted
, "interrupted");
10428 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10429 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10431 defsubr (&Scoding_system_p
);
10432 defsubr (&Sread_coding_system
);
10433 defsubr (&Sread_non_nil_coding_system
);
10434 defsubr (&Scheck_coding_system
);
10435 defsubr (&Sdetect_coding_region
);
10436 defsubr (&Sdetect_coding_string
);
10437 defsubr (&Sfind_coding_systems_region_internal
);
10438 defsubr (&Sunencodable_char_position
);
10439 defsubr (&Scheck_coding_systems_region
);
10440 defsubr (&Sdecode_coding_region
);
10441 defsubr (&Sencode_coding_region
);
10442 defsubr (&Sdecode_coding_string
);
10443 defsubr (&Sencode_coding_string
);
10444 defsubr (&Sdecode_sjis_char
);
10445 defsubr (&Sencode_sjis_char
);
10446 defsubr (&Sdecode_big5_char
);
10447 defsubr (&Sencode_big5_char
);
10448 defsubr (&Sset_terminal_coding_system_internal
);
10449 defsubr (&Sset_safe_terminal_coding_system_internal
);
10450 defsubr (&Sterminal_coding_system
);
10451 defsubr (&Sset_keyboard_coding_system_internal
);
10452 defsubr (&Skeyboard_coding_system
);
10453 defsubr (&Sfind_operation_coding_system
);
10454 defsubr (&Sset_coding_system_priority
);
10455 defsubr (&Sdefine_coding_system_internal
);
10456 defsubr (&Sdefine_coding_system_alias
);
10457 defsubr (&Scoding_system_put
);
10458 defsubr (&Scoding_system_base
);
10459 defsubr (&Scoding_system_plist
);
10460 defsubr (&Scoding_system_aliases
);
10461 defsubr (&Scoding_system_eol_type
);
10462 defsubr (&Scoding_system_priority_list
);
10464 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10465 doc
: /* List of coding systems.
10467 Do not alter the value of this variable manually. This variable should be
10468 updated by the functions `define-coding-system' and
10469 `define-coding-system-alias'. */);
10470 Vcoding_system_list
= Qnil
;
10472 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10473 doc
: /* Alist of coding system names.
10474 Each element is one element list of coding system name.
10475 This variable is given to `completing-read' as COLLECTION argument.
10477 Do not alter the value of this variable manually. This variable should be
10478 updated by the functions `make-coding-system' and
10479 `define-coding-system-alias'. */);
10480 Vcoding_system_alist
= Qnil
;
10482 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10483 doc
: /* List of coding-categories (symbols) ordered by priority.
10485 On detecting a coding system, Emacs tries code detection algorithms
10486 associated with each coding-category one by one in this order. When
10487 one algorithm agrees with a byte sequence of source text, the coding
10488 system bound to the corresponding coding-category is selected.
10490 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10494 Vcoding_category_list
= Qnil
;
10495 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10496 Vcoding_category_list
10497 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10498 Vcoding_category_list
);
10501 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10502 doc
: /* Specify the coding system for read operations.
10503 It is useful to bind this variable with `let', but do not set it globally.
10504 If the value is a coding system, it is used for decoding on read operation.
10505 If not, an appropriate element is used from one of the coding system alists.
10506 There are three such tables: `file-coding-system-alist',
10507 `process-coding-system-alist', and `network-coding-system-alist'. */);
10508 Vcoding_system_for_read
= Qnil
;
10510 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10511 doc
: /* Specify the coding system for write operations.
10512 Programs bind this variable with `let', but you should not set it globally.
10513 If the value is a coding system, it is used for encoding of output,
10514 when writing it to a file and when sending it to a file or subprocess.
10516 If this does not specify a coding system, an appropriate element
10517 is used from one of the coding system alists.
10518 There are three such tables: `file-coding-system-alist',
10519 `process-coding-system-alist', and `network-coding-system-alist'.
10520 For output to files, if the above procedure does not specify a coding system,
10521 the value of `buffer-file-coding-system' is used. */);
10522 Vcoding_system_for_write
= Qnil
;
10524 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10526 Coding system used in the latest file or process I/O. */);
10527 Vlast_coding_system_used
= Qnil
;
10529 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10531 Error status of the last code conversion.
10533 When an error was detected in the last code conversion, this variable
10534 is set to one of the following symbols.
10535 `insufficient-source'
10539 `insufficient-memory'
10540 When no error was detected, the value doesn't change. So, to check
10541 the error status of a code conversion by this variable, you must
10542 explicitly set this variable to nil before performing code
10544 Vlast_code_conversion_error
= Qnil
;
10546 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10548 *Non-nil means always inhibit code conversion of end-of-line format.
10549 See info node `Coding Systems' and info node `Text and Binary' concerning
10550 such conversion. */);
10551 inhibit_eol_conversion
= 0;
10553 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10555 Non-nil means process buffer inherits coding system of process output.
10556 Bind it to t if the process output is to be treated as if it were a file
10557 read from some filesystem. */);
10558 inherit_process_coding_system
= 0;
10560 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10562 Alist to decide a coding system to use for a file I/O operation.
10563 The format is ((PATTERN . VAL) ...),
10564 where PATTERN is a regular expression matching a file name,
10565 VAL is a coding system, a cons of coding systems, or a function symbol.
10566 If VAL is a coding system, it is used for both decoding and encoding
10568 If VAL is a cons of coding systems, the car part is used for decoding,
10569 and the cdr part is used for encoding.
10570 If VAL is a function symbol, the function must return a coding system
10571 or a cons of coding systems which are used as above. The function is
10572 called with an argument that is a list of the arguments with which
10573 `find-operation-coding-system' was called. If the function can't decide
10574 a coding system, it can return `undecided' so that the normal
10575 code-detection is performed.
10577 See also the function `find-operation-coding-system'
10578 and the variable `auto-coding-alist'. */);
10579 Vfile_coding_system_alist
= Qnil
;
10581 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10583 Alist to decide a coding system to use for a process I/O operation.
10584 The format is ((PATTERN . VAL) ...),
10585 where PATTERN is a regular expression matching a program name,
10586 VAL is a coding system, a cons of coding systems, or a function symbol.
10587 If VAL is a coding system, it is used for both decoding what received
10588 from the program and encoding what sent to the program.
10589 If VAL is a cons of coding systems, the car part is used for decoding,
10590 and the cdr part is used for encoding.
10591 If VAL is a function symbol, the function must return a coding system
10592 or a cons of coding systems which are used as above.
10594 See also the function `find-operation-coding-system'. */);
10595 Vprocess_coding_system_alist
= Qnil
;
10597 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10599 Alist to decide a coding system to use for a network I/O operation.
10600 The format is ((PATTERN . VAL) ...),
10601 where PATTERN is a regular expression matching a network service name
10602 or is a port number to connect to,
10603 VAL is a coding system, a cons of coding systems, or a function symbol.
10604 If VAL is a coding system, it is used for both decoding what received
10605 from the network stream and encoding what sent to the network stream.
10606 If VAL is a cons of coding systems, the car part is used for decoding,
10607 and the cdr part is used for encoding.
10608 If VAL is a function symbol, the function must return a coding system
10609 or a cons of coding systems which are used as above.
10611 See also the function `find-operation-coding-system'. */);
10612 Vnetwork_coding_system_alist
= Qnil
;
10614 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10615 doc
: /* Coding system to use with system messages.
10616 Also used for decoding keyboard input on X Window system. */);
10617 Vlocale_coding_system
= Qnil
;
10619 /* The eol mnemonics are reset in startup.el system-dependently. */
10620 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10622 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10623 eol_mnemonic_unix
= make_pure_c_string (":");
10625 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10627 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10628 eol_mnemonic_dos
= make_pure_c_string ("\\");
10630 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10632 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10633 eol_mnemonic_mac
= make_pure_c_string ("/");
10635 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10637 *String displayed in mode line when end-of-line format is not yet determined. */);
10638 eol_mnemonic_undecided
= make_pure_c_string (":");
10640 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10642 *Non-nil enables character translation while encoding and decoding. */);
10643 Venable_character_translation
= Qt
;
10645 DEFVAR_LISP ("standard-translation-table-for-decode",
10646 Vstandard_translation_table_for_decode
,
10647 doc
: /* Table for translating characters while decoding. */);
10648 Vstandard_translation_table_for_decode
= Qnil
;
10650 DEFVAR_LISP ("standard-translation-table-for-encode",
10651 Vstandard_translation_table_for_encode
,
10652 doc
: /* Table for translating characters while encoding. */);
10653 Vstandard_translation_table_for_encode
= Qnil
;
10655 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10656 doc
: /* Alist of charsets vs revision numbers.
10657 While encoding, if a charset (car part of an element) is found,
10658 designate it with the escape sequence identifying revision (cdr part
10659 of the element). */);
10660 Vcharset_revision_table
= Qnil
;
10662 DEFVAR_LISP ("default-process-coding-system",
10663 Vdefault_process_coding_system
,
10664 doc
: /* Cons of coding systems used for process I/O by default.
10665 The car part is used for decoding a process output,
10666 the cdr part is used for encoding a text to be sent to a process. */);
10667 Vdefault_process_coding_system
= Qnil
;
10669 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10671 Table of extra Latin codes in the range 128..159 (inclusive).
10672 This is a vector of length 256.
10673 If Nth element is non-nil, the existence of code N in a file
10674 \(or output of subprocess) doesn't prevent it to be detected as
10675 a coding system of ISO 2022 variant which has a flag
10676 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10677 or reading output of a subprocess.
10678 Only 128th through 159th elements have a meaning. */);
10679 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10681 DEFVAR_LISP ("select-safe-coding-system-function",
10682 Vselect_safe_coding_system_function
,
10684 Function to call to select safe coding system for encoding a text.
10686 If set, this function is called to force a user to select a proper
10687 coding system which can encode the text in the case that a default
10688 coding system used in each operation can't encode the text. The
10689 function should take care that the buffer is not modified while
10690 the coding system is being selected.
10692 The default value is `select-safe-coding-system' (which see). */);
10693 Vselect_safe_coding_system_function
= Qnil
;
10695 DEFVAR_BOOL ("coding-system-require-warning",
10696 coding_system_require_warning
,
10697 doc
: /* Internal use only.
10698 If non-nil, on writing a file, `select-safe-coding-system-function' is
10699 called even if `coding-system-for-write' is non-nil. The command
10700 `universal-coding-system-argument' binds this variable to t temporarily. */);
10701 coding_system_require_warning
= 0;
10704 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10705 inhibit_iso_escape_detection
,
10707 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10709 When Emacs reads text, it tries to detect how the text is encoded.
10710 This code detection is sensitive to escape sequences. If Emacs sees
10711 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10712 of the ISO2022 encodings, and decodes text by the corresponding coding
10713 system (e.g. `iso-2022-7bit').
10715 However, there may be a case that you want to read escape sequences in
10716 a file as is. In such a case, you can set this variable to non-nil.
10717 Then the code detection will ignore any escape sequences, and no text is
10718 detected as encoded in some ISO-2022 encoding. The result is that all
10719 escape sequences become visible in a buffer.
10721 The default value is nil, and it is strongly recommended not to change
10722 it. That is because many Emacs Lisp source files that contain
10723 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10724 in Emacs's distribution, and they won't be decoded correctly on
10725 reading if you suppress escape sequence detection.
10727 The other way to read escape sequences in a file without decoding is
10728 to explicitly specify some coding system that doesn't use ISO-2022
10729 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10730 inhibit_iso_escape_detection
= 0;
10732 DEFVAR_BOOL ("inhibit-null-byte-detection",
10733 inhibit_null_byte_detection
,
10734 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10735 By default, Emacs treats it as binary data, and does not attempt to
10736 decode it. The effect is as if you specified `no-conversion' for
10739 Set this to non-nil when a regular text happens to include null bytes.
10740 Examples are Index nodes of Info files and null-byte delimited output
10741 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10742 decode text as usual. */);
10743 inhibit_null_byte_detection
= 0;
10745 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10746 doc
: /* Char table for translating self-inserting characters.
10747 This is applied to the result of input methods, not their input.
10748 See also `keyboard-translate-table'.
10750 Use of this variable for character code unification was rendered
10751 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10752 internal character representation. */);
10753 Vtranslation_table_for_input
= Qnil
;
10756 Lisp_Object args
[coding_arg_max
];
10757 Lisp_Object plist
[16];
10760 for (i
= 0; i
< coding_arg_max
; i
++)
10763 plist
[0] = intern_c_string (":name");
10764 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10765 plist
[2] = intern_c_string (":mnemonic");
10766 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10767 plist
[4] = intern_c_string (":coding-type");
10768 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10769 plist
[6] = intern_c_string (":ascii-compatible-p");
10770 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10771 plist
[8] = intern_c_string (":default-char");
10772 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10773 plist
[10] = intern_c_string (":for-unibyte");
10774 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10775 plist
[12] = intern_c_string (":docstring");
10776 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10778 When you visit a file with this coding, the file is read into a\n\
10779 unibyte buffer as is, thus each byte of a file is treated as a\n\
10781 plist
[14] = intern_c_string (":eol-type");
10782 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10783 args
[coding_arg_plist
] = Flist (16, plist
);
10784 Fdefine_coding_system_internal (coding_arg_max
, args
);
10786 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10787 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10788 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10789 /* This is already set.
10790 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10791 plist
[8] = intern_c_string (":charset-list");
10792 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10793 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10794 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10795 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10796 args
[coding_arg_plist
] = Flist (16, plist
);
10797 Fdefine_coding_system_internal (coding_arg_max
, args
);
10800 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10805 for (i
= 0; i
< coding_category_max
; i
++)
10806 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10808 #if defined (DOS_NT)
10809 system_eol_type
= Qdos
;
10811 system_eol_type
= Qunix
;
10813 staticpro (&system_eol_type
);
10817 emacs_strerror (int error_number
)
10821 synchronize_system_messages_locale ();
10822 str
= strerror (error_number
);
10824 if (! NILP (Vlocale_coding_system
))
10826 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10827 Vlocale_coding_system
,
10829 str
= SSDATA (dec
);