1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
12 This file is part of GNU Emacs.
14 GNU Emacs is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 /*** TABLE OF CONTENTS ***
31 2. Emacs' internal format (emacs-utf-8) handlers
34 5. Charset-base coding systems handlers
35 6. emacs-mule (old Emacs' internal format) handlers
37 8. Shift-JIS and BIG5 handlers
39 10. C library functions
40 11. Emacs Lisp library functions
45 /*** 0. General comments ***
50 A coding system is an object for an encoding mechanism that contains
51 information about how to convert byte sequences to character
52 sequences and vice versa. When we say "decode", it means converting
53 a byte sequence of a specific coding system into a character
54 sequence that is represented by Emacs' internal coding system
55 `emacs-utf-8', and when we say "encode", it means converting a
56 character sequence of emacs-utf-8 to a byte sequence of a specific
59 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
60 C level, a coding system is represented by a vector of attributes
61 stored in the hash table Vcharset_hash_table. The conversion from
62 coding system symbol to attributes vector is done by looking up
63 Vcharset_hash_table by the symbol.
65 Coding systems are classified into the following types depending on
66 the encoding mechanism. Here's a brief description of the types.
72 o Charset-base coding system
74 A coding system defined by one or more (coded) character sets.
75 Decoding and encoding are done by a code converter defined for each
78 o Old Emacs internal format (emacs-mule)
80 The coding system adopted by old versions of Emacs (20 and 21).
82 o ISO2022-base coding system
84 The most famous coding system for multiple character sets. X's
85 Compound Text, various EUCs (Extended Unix Code), and coding systems
86 used in the Internet communication such as ISO-2022-JP are all
89 o SJIS (or Shift-JIS or MS-Kanji-Code)
91 A coding system to encode character sets: ASCII, JISX0201, and
92 JISX0208. Widely used for PC's in Japan. Details are described in
97 A coding system to encode character sets: ASCII and Big5. Widely
98 used for Chinese (mainly in Taiwan and Hong Kong). Details are
99 described in section 8. In this file, when we write "big5" (all
100 lowercase), we mean the coding system, and when we write "Big5"
101 (capitalized), we mean the character set.
105 If a user wants to decode/encode text encoded in a coding system
106 not listed above, he can supply a decoder and an encoder for it in
107 CCL (Code Conversion Language) programs. Emacs executes the CCL
108 program while decoding/encoding.
112 A coding system for text containing raw eight-bit data. Emacs
113 treats each byte of source text as a character (except for
114 end-of-line conversion).
118 Like raw text, but don't do end-of-line conversion.
123 How text end-of-line is encoded depends on operating system. For
124 instance, Unix's format is just one byte of LF (line-feed) code,
125 whereas DOS's format is two-byte sequence of `carriage-return' and
126 `line-feed' codes. MacOS's format is usually one byte of
129 Since text character encoding and end-of-line encoding are
130 independent, any coding system described above can take any format
131 of end-of-line (except for no-conversion).
135 Before using a coding system for code conversion (i.e. decoding and
136 encoding), we setup a structure of type `struct coding_system'.
137 This structure keeps various information about a specific code
138 conversion (e.g. the location of source and destination data).
145 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
147 These functions check if a byte sequence specified as a source in
148 CODING conforms to the format of XXX, and update the members of
151 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
153 Below is the template of these functions. */
157 detect_coding_XXX (struct coding_system
*coding
,
158 struct coding_detection_info
*detect_info
)
160 const unsigned char *src
= coding
->source
;
161 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
162 int multibytep
= coding
->src_multibyte
;
163 int consumed_chars
= 0;
169 /* Get one byte from the source. If the source is exhausted, jump
170 to no_more_source:. */
173 if (! __C_conforms_to_XXX___ (c
))
175 if (! __C_strongly_suggests_XXX__ (c
))
176 found
= CATEGORY_MASK_XXX
;
178 /* The byte sequence is invalid for XXX. */
179 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
183 /* The source exhausted successfully. */
184 detect_info
->found
|= found
;
189 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
191 These functions decode a byte sequence specified as a source by
192 CODING. The resulting multibyte text goes to a place pointed to by
193 CODING->charbuf, the length of which should not exceed
194 CODING->charbuf_size;
196 These functions set the information of original and decoded texts in
197 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
198 They also set CODING->result to one of CODING_RESULT_XXX indicating
199 how the decoding is finished.
201 Below is the template of these functions. */
205 decode_coding_XXXX (struct coding_system
*coding
)
207 const unsigned char *src
= coding
->source
+ coding
->consumed
;
208 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
209 /* SRC_BASE remembers the start position in source in each loop.
210 The loop will be exited when there's not enough source code, or
211 when there's no room in CHARBUF for a decoded character. */
212 const unsigned char *src_base
;
213 /* A buffer to produce decoded characters. */
214 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
215 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
216 int multibytep
= coding
->src_multibyte
;
221 if (charbuf
< charbuf_end
)
222 /* No more room to produce a decoded character. */
229 if (src_base
< src_end
230 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
231 /* If the source ends by partial bytes to construct a character,
232 treat them as eight-bit raw data. */
233 while (src_base
< src_end
&& charbuf
< charbuf_end
)
234 *charbuf
++ = *src_base
++;
235 /* Remember how many bytes and characters we consumed. If the
236 source is multibyte, the bytes and chars are not identical. */
237 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
238 /* Remember how many characters we produced. */
239 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
243 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
245 These functions encode SRC_BYTES length text at SOURCE of Emacs'
246 internal multibyte format by CODING. The resulting byte sequence
247 goes to a place pointed to by DESTINATION, the length of which
248 should not exceed DST_BYTES.
250 These functions set the information of original and encoded texts in
251 the members produced, produced_char, consumed, and consumed_char of
252 the structure *CODING. They also set the member result to one of
253 CODING_RESULT_XXX indicating how the encoding finished.
255 DST_BYTES zero means that source area and destination area are
256 overlapped, which means that we can produce a encoded text until it
257 reaches at the head of not-yet-encoded source text.
259 Below is a template of these functions. */
262 encode_coding_XXX (struct coding_system
*coding
)
264 int multibytep
= coding
->dst_multibyte
;
265 int *charbuf
= coding
->charbuf
;
266 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
267 unsigned char *dst
= coding
->destination
+ coding
->produced
;
268 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
269 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
270 int produced_chars
= 0;
272 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
275 /* Encode C into DST, and increment DST. */
277 label_no_more_destination
:
278 /* How many chars and bytes we produced. */
279 coding
->produced_char
+= produced_chars
;
280 coding
->produced
= dst
- coding
->destination
;
285 /*** 1. Preamble ***/
293 #include "character.h"
296 #include "composite.h"
300 #include "termhooks.h"
302 Lisp_Object Vcoding_system_hash_table
;
304 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
305 Lisp_Object Qunix
, Qdos
;
306 Lisp_Object Qbuffer_file_coding_system
;
307 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
308 Lisp_Object Qdefault_char
;
309 Lisp_Object Qno_conversion
, Qundecided
;
310 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
311 Lisp_Object Qbig
, Qlittle
;
312 Lisp_Object Qcoding_system_history
;
313 Lisp_Object Qvalid_codes
;
314 Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
315 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
316 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
317 Lisp_Object QCascii_compatible_p
;
319 Lisp_Object Qcall_process
, Qcall_process_region
;
320 Lisp_Object Qstart_process
, Qopen_network_stream
;
321 Lisp_Object Qtarget_idx
;
323 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
324 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
326 /* If a symbol has this property, evaluate the value to define the
327 symbol as a coding system. */
328 static Lisp_Object Qcoding_system_define_form
;
330 /* Format of end-of-line decided by system. This is Qunix on
331 Unix and Mac, Qdos on DOS/Windows.
332 This has an effect only for external encoding (i.e. for output to
333 file and process), not for in-buffer or Lisp string encoding. */
334 static Lisp_Object system_eol_type
;
338 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
340 /* Coding system emacs-mule and raw-text are for converting only
341 end-of-line format. */
342 Lisp_Object Qemacs_mule
, Qraw_text
;
343 Lisp_Object Qutf_8_emacs
;
345 /* Coding-systems are handed between Emacs Lisp programs and C internal
346 routines by the following three variables. */
347 /* Coding system to be used to encode text for terminal display when
348 terminal coding system is nil. */
349 struct coding_system safe_terminal_coding
;
353 Lisp_Object Qtranslation_table
;
354 Lisp_Object Qtranslation_table_id
;
355 Lisp_Object Qtranslation_table_for_decode
;
356 Lisp_Object Qtranslation_table_for_encode
;
358 /* Two special coding systems. */
359 Lisp_Object Vsjis_coding_system
;
360 Lisp_Object Vbig5_coding_system
;
362 /* ISO2022 section */
364 #define CODING_ISO_INITIAL(coding, reg) \
365 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
366 coding_attr_iso_initial), \
370 #define CODING_ISO_REQUEST(coding, charset_id) \
371 (((charset_id) <= (coding)->max_charset_id \
372 ? ((coding)->safe_charsets[charset_id] != 255 \
373 ? (coding)->safe_charsets[charset_id] \
378 #define CODING_ISO_FLAGS(coding) \
379 ((coding)->spec.iso_2022.flags)
380 #define CODING_ISO_DESIGNATION(coding, reg) \
381 ((coding)->spec.iso_2022.current_designation[reg])
382 #define CODING_ISO_INVOCATION(coding, plane) \
383 ((coding)->spec.iso_2022.current_invocation[plane])
384 #define CODING_ISO_SINGLE_SHIFTING(coding) \
385 ((coding)->spec.iso_2022.single_shifting)
386 #define CODING_ISO_BOL(coding) \
387 ((coding)->spec.iso_2022.bol)
388 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
389 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
390 #define CODING_ISO_CMP_STATUS(coding) \
391 (&(coding)->spec.iso_2022.cmp_status)
392 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
393 ((coding)->spec.iso_2022.ctext_extended_segment_len)
394 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
395 ((coding)->spec.iso_2022.embedded_utf_8)
397 /* Control characters of ISO2022. */
398 /* code */ /* function */
399 #define ISO_CODE_LF 0x0A /* line-feed */
400 #define ISO_CODE_CR 0x0D /* carriage-return */
401 #define ISO_CODE_SO 0x0E /* shift-out */
402 #define ISO_CODE_SI 0x0F /* shift-in */
403 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
404 #define ISO_CODE_ESC 0x1B /* escape */
405 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
406 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
407 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
409 /* All code (1-byte) of ISO2022 is classified into one of the
411 enum iso_code_class_type
413 ISO_control_0
, /* Control codes in the range
414 0x00..0x1F and 0x7F, except for the
415 following 5 codes. */
416 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
417 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
418 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
419 ISO_escape
, /* ISO_CODE_SO (0x1B) */
420 ISO_control_1
, /* Control codes in the range
421 0x80..0x9F, except for the
422 following 3 codes. */
423 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
424 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
425 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
426 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
427 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
428 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
429 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
432 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
433 `iso-flags' attribute of an iso2022 coding system. */
435 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
436 instead of the correct short-form sequence (e.g. ESC $ A). */
437 #define CODING_ISO_FLAG_LONG_FORM 0x0001
439 /* If set, reset graphic planes and registers at end-of-line to the
441 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
443 /* If set, reset graphic planes and registers before any control
444 characters to the initial state. */
445 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
447 /* If set, encode by 7-bit environment. */
448 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
450 /* If set, use locking-shift function. */
451 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
453 /* If set, use single-shift function. Overwrite
454 CODING_ISO_FLAG_LOCKING_SHIFT. */
455 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
457 /* If set, use designation escape sequence. */
458 #define CODING_ISO_FLAG_DESIGNATION 0x0040
460 /* If set, produce revision number sequence. */
461 #define CODING_ISO_FLAG_REVISION 0x0080
463 /* If set, produce ISO6429's direction specifying sequence. */
464 #define CODING_ISO_FLAG_DIRECTION 0x0100
466 /* If set, assume designation states are reset at beginning of line on
468 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
470 /* If set, designation sequence should be placed at beginning of line
472 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
474 /* If set, do not encode unsafe characters on output. */
475 #define CODING_ISO_FLAG_SAFE 0x0800
477 /* If set, extra latin codes (128..159) are accepted as a valid code
479 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
481 #define CODING_ISO_FLAG_COMPOSITION 0x2000
483 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
485 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
487 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
489 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
491 /* A character to be produced on output if encoding of the original
492 character is prohibited by CODING_ISO_FLAG_SAFE. */
493 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
496 #define CODING_UTF_8_BOM(coding) \
497 ((coding)->spec.utf_8_bom)
500 #define CODING_UTF_16_BOM(coding) \
501 ((coding)->spec.utf_16.bom)
503 #define CODING_UTF_16_ENDIAN(coding) \
504 ((coding)->spec.utf_16.endian)
506 #define CODING_UTF_16_SURROGATE(coding) \
507 ((coding)->spec.utf_16.surrogate)
511 #define CODING_CCL_DECODER(coding) \
512 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
513 #define CODING_CCL_ENCODER(coding) \
514 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
515 #define CODING_CCL_VALIDS(coding) \
516 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
518 /* Index for each coding category in `coding_categories' */
522 coding_category_iso_7
,
523 coding_category_iso_7_tight
,
524 coding_category_iso_8_1
,
525 coding_category_iso_8_2
,
526 coding_category_iso_7_else
,
527 coding_category_iso_8_else
,
528 coding_category_utf_8_auto
,
529 coding_category_utf_8_nosig
,
530 coding_category_utf_8_sig
,
531 coding_category_utf_16_auto
,
532 coding_category_utf_16_be
,
533 coding_category_utf_16_le
,
534 coding_category_utf_16_be_nosig
,
535 coding_category_utf_16_le_nosig
,
536 coding_category_charset
,
537 coding_category_sjis
,
538 coding_category_big5
,
540 coding_category_emacs_mule
,
541 /* All above are targets of code detection. */
542 coding_category_raw_text
,
543 coding_category_undecided
,
547 /* Definitions of flag bits used in detect_coding_XXXX. */
548 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
549 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
550 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
551 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
552 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
553 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
554 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
555 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
556 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
557 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
558 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
559 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
560 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
561 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
562 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
563 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
564 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
565 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
566 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
567 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
569 /* This value is returned if detect_coding_mask () find nothing other
570 than ASCII characters. */
571 #define CATEGORY_MASK_ANY \
572 (CATEGORY_MASK_ISO_7 \
573 | CATEGORY_MASK_ISO_7_TIGHT \
574 | CATEGORY_MASK_ISO_8_1 \
575 | CATEGORY_MASK_ISO_8_2 \
576 | CATEGORY_MASK_ISO_7_ELSE \
577 | CATEGORY_MASK_ISO_8_ELSE \
578 | CATEGORY_MASK_UTF_8_AUTO \
579 | CATEGORY_MASK_UTF_8_NOSIG \
580 | CATEGORY_MASK_UTF_8_SIG \
581 | CATEGORY_MASK_UTF_16_AUTO \
582 | CATEGORY_MASK_UTF_16_BE \
583 | CATEGORY_MASK_UTF_16_LE \
584 | CATEGORY_MASK_UTF_16_BE_NOSIG \
585 | CATEGORY_MASK_UTF_16_LE_NOSIG \
586 | CATEGORY_MASK_CHARSET \
587 | CATEGORY_MASK_SJIS \
588 | CATEGORY_MASK_BIG5 \
589 | CATEGORY_MASK_CCL \
590 | CATEGORY_MASK_EMACS_MULE)
593 #define CATEGORY_MASK_ISO_7BIT \
594 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
596 #define CATEGORY_MASK_ISO_8BIT \
597 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
599 #define CATEGORY_MASK_ISO_ELSE \
600 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
602 #define CATEGORY_MASK_ISO_ESCAPE \
603 (CATEGORY_MASK_ISO_7 \
604 | CATEGORY_MASK_ISO_7_TIGHT \
605 | CATEGORY_MASK_ISO_7_ELSE \
606 | CATEGORY_MASK_ISO_8_ELSE)
608 #define CATEGORY_MASK_ISO \
609 ( CATEGORY_MASK_ISO_7BIT \
610 | CATEGORY_MASK_ISO_8BIT \
611 | CATEGORY_MASK_ISO_ELSE)
613 #define CATEGORY_MASK_UTF_16 \
614 (CATEGORY_MASK_UTF_16_AUTO \
615 | CATEGORY_MASK_UTF_16_BE \
616 | CATEGORY_MASK_UTF_16_LE \
617 | CATEGORY_MASK_UTF_16_BE_NOSIG \
618 | CATEGORY_MASK_UTF_16_LE_NOSIG)
620 #define CATEGORY_MASK_UTF_8 \
621 (CATEGORY_MASK_UTF_8_AUTO \
622 | CATEGORY_MASK_UTF_8_NOSIG \
623 | CATEGORY_MASK_UTF_8_SIG)
625 /* Table of coding categories (Lisp symbols). This variable is for
626 internal use only. */
627 static Lisp_Object Vcoding_category_table
;
629 /* Table of coding-categories ordered by priority. */
630 static enum coding_category coding_priorities
[coding_category_max
];
632 /* Nth element is a coding context for the coding system bound to the
633 Nth coding category. */
634 static struct coding_system coding_categories
[coding_category_max
];
636 /*** Commonly used macros and functions ***/
639 #define min(a, b) ((a) < (b) ? (a) : (b))
642 #define max(a, b) ((a) > (b) ? (a) : (b))
645 #define CODING_GET_INFO(coding, attrs, charset_list) \
647 (attrs) = CODING_ID_ATTRS ((coding)->id); \
648 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
652 /* Safely get one byte from the source text pointed by SRC which ends
653 at SRC_END, and set C to that byte. If there are not enough bytes
654 in the source, it jumps to `no_more_source'. If multibytep is
655 nonzero, and a multibyte character is found at SRC, set C to the
656 negative value of the character code. The caller should declare
657 and set these variables appropriately in advance:
658 src, src_end, multibytep */
660 #define ONE_MORE_BYTE(c) \
662 if (src == src_end) \
664 if (src_base < src) \
665 record_conversion_result \
666 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
667 goto no_more_source; \
670 if (multibytep && (c & 0x80)) \
672 if ((c & 0xFE) == 0xC0) \
673 c = ((c & 1) << 6) | *src++; \
677 c = - string_char (src, &src, NULL); \
678 record_conversion_result \
679 (coding, CODING_RESULT_INVALID_SRC); \
685 /* Safely get two bytes from the source text pointed by SRC which ends
686 at SRC_END, and set C1 and C2 to those bytes while skipping the
687 heading multibyte characters. If there are not enough bytes in the
688 source, it jumps to `no_more_source'. If multibytep is nonzero and
689 a multibyte character is found for C2, set C2 to the negative value
690 of the character code. The caller should declare and set these
691 variables appropriately in advance:
692 src, src_end, multibytep
693 It is intended that this macro is used in detect_coding_utf_16. */
695 #define TWO_MORE_BYTES(c1, c2) \
698 if (src == src_end) \
699 goto no_more_source; \
701 if (multibytep && (c1 & 0x80)) \
703 if ((c1 & 0xFE) == 0xC0) \
704 c1 = ((c1 & 1) << 6) | *src++; \
707 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
712 if (src == src_end) \
713 goto no_more_source; \
715 if (multibytep && (c2 & 0x80)) \
717 if ((c2 & 0xFE) == 0xC0) \
718 c2 = ((c2 & 1) << 6) | *src++; \
725 #define ONE_MORE_BYTE_NO_CHECK(c) \
728 if (multibytep && (c & 0x80)) \
730 if ((c & 0xFE) == 0xC0) \
731 c = ((c & 1) << 6) | *src++; \
735 c = - string_char (src, &src, NULL); \
736 record_conversion_result \
737 (coding, CODING_RESULT_INVALID_SRC); \
744 /* Store a byte C in the place pointed by DST and increment DST to the
745 next free point, and increment PRODUCED_CHARS. The caller should
746 assure that C is 0..127, and declare and set the variable `dst'
747 appropriately in advance.
751 #define EMIT_ONE_ASCII_BYTE(c) \
758 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
760 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
762 produced_chars += 2; \
763 *dst++ = (c1), *dst++ = (c2); \
767 /* Store a byte C in the place pointed by DST and increment DST to the
768 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
769 nonzero, store in an appropriate multibyte from. The caller should
770 declare and set the variables `dst' and `multibytep' appropriately
773 #define EMIT_ONE_BYTE(c) \
780 ch = BYTE8_TO_CHAR (ch); \
781 CHAR_STRING_ADVANCE (ch, dst); \
788 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
790 #define EMIT_TWO_BYTES(c1, c2) \
792 produced_chars += 2; \
799 ch = BYTE8_TO_CHAR (ch); \
800 CHAR_STRING_ADVANCE (ch, dst); \
803 ch = BYTE8_TO_CHAR (ch); \
804 CHAR_STRING_ADVANCE (ch, dst); \
814 #define EMIT_THREE_BYTES(c1, c2, c3) \
816 EMIT_ONE_BYTE (c1); \
817 EMIT_TWO_BYTES (c2, c3); \
821 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
823 EMIT_TWO_BYTES (c1, c2); \
824 EMIT_TWO_BYTES (c3, c4); \
828 /* Prototypes for static functions. */
829 static void record_conversion_result (struct coding_system
*coding
,
830 enum coding_result_code result
);
831 static int detect_coding_utf_8 (struct coding_system
*,
832 struct coding_detection_info
*info
);
833 static void decode_coding_utf_8 (struct coding_system
*);
834 static int encode_coding_utf_8 (struct coding_system
*);
836 static int detect_coding_utf_16 (struct coding_system
*,
837 struct coding_detection_info
*info
);
838 static void decode_coding_utf_16 (struct coding_system
*);
839 static int encode_coding_utf_16 (struct coding_system
*);
841 static int detect_coding_iso_2022 (struct coding_system
*,
842 struct coding_detection_info
*info
);
843 static void decode_coding_iso_2022 (struct coding_system
*);
844 static int encode_coding_iso_2022 (struct coding_system
*);
846 static int detect_coding_emacs_mule (struct coding_system
*,
847 struct coding_detection_info
*info
);
848 static void decode_coding_emacs_mule (struct coding_system
*);
849 static int encode_coding_emacs_mule (struct coding_system
*);
851 static int detect_coding_sjis (struct coding_system
*,
852 struct coding_detection_info
*info
);
853 static void decode_coding_sjis (struct coding_system
*);
854 static int encode_coding_sjis (struct coding_system
*);
856 static int detect_coding_big5 (struct coding_system
*,
857 struct coding_detection_info
*info
);
858 static void decode_coding_big5 (struct coding_system
*);
859 static int encode_coding_big5 (struct coding_system
*);
861 static int detect_coding_ccl (struct coding_system
*,
862 struct coding_detection_info
*info
);
863 static void decode_coding_ccl (struct coding_system
*);
864 static int encode_coding_ccl (struct coding_system
*);
866 static void decode_coding_raw_text (struct coding_system
*);
867 static int encode_coding_raw_text (struct coding_system
*);
869 static void coding_set_source (struct coding_system
*);
870 static void coding_set_destination (struct coding_system
*);
871 static void coding_alloc_by_realloc (struct coding_system
*, EMACS_INT
);
872 static void coding_alloc_by_making_gap (struct coding_system
*,
873 EMACS_INT
, EMACS_INT
);
874 static unsigned char *alloc_destination (struct coding_system
*,
875 EMACS_INT
, unsigned char *);
876 static void setup_iso_safe_charsets (Lisp_Object
);
877 static unsigned char *encode_designation_at_bol (struct coding_system
*,
880 static int detect_eol (const unsigned char *,
881 EMACS_INT
, enum coding_category
);
882 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*, int);
883 static void decode_eol (struct coding_system
*);
884 static Lisp_Object
get_translation_table (Lisp_Object
, int, int *);
885 static Lisp_Object
get_translation (Lisp_Object
, int *, int *);
886 static int produce_chars (struct coding_system
*, Lisp_Object
, int);
887 static INLINE
void produce_charset (struct coding_system
*, int *,
889 static void produce_annotation (struct coding_system
*, EMACS_INT
);
890 static int decode_coding (struct coding_system
*);
891 static INLINE
int *handle_composition_annotation (EMACS_INT
, EMACS_INT
,
892 struct coding_system
*,
894 static INLINE
int *handle_charset_annotation (EMACS_INT
, EMACS_INT
,
895 struct coding_system
*,
897 static void consume_chars (struct coding_system
*, Lisp_Object
, int);
898 static int encode_coding (struct coding_system
*);
899 static Lisp_Object
make_conversion_work_buffer (int);
900 static Lisp_Object
code_conversion_restore (Lisp_Object
);
901 static INLINE
int char_encodable_p (int, Lisp_Object
);
902 static Lisp_Object
make_subsidiaries (Lisp_Object
);
905 record_conversion_result (struct coding_system
*coding
,
906 enum coding_result_code result
)
908 coding
->result
= result
;
911 case CODING_RESULT_INSUFFICIENT_SRC
:
912 Vlast_code_conversion_error
= Qinsufficient_source
;
914 case CODING_RESULT_INCONSISTENT_EOL
:
915 Vlast_code_conversion_error
= Qinconsistent_eol
;
917 case CODING_RESULT_INVALID_SRC
:
918 Vlast_code_conversion_error
= Qinvalid_source
;
920 case CODING_RESULT_INTERRUPT
:
921 Vlast_code_conversion_error
= Qinterrupted
;
923 case CODING_RESULT_INSUFFICIENT_MEM
:
924 Vlast_code_conversion_error
= Qinsufficient_memory
;
926 case CODING_RESULT_INSUFFICIENT_DST
:
927 /* Don't record this error in Vlast_code_conversion_error
928 because it happens just temporarily and is resolved when the
929 whole conversion is finished. */
931 case CODING_RESULT_SUCCESS
:
934 Vlast_code_conversion_error
= intern ("Unknown error");
938 /* This wrapper macro is used to preserve validity of pointers into
939 buffer text across calls to decode_char, which could cause
940 relocation of buffers if it loads a charset map, because loading a
941 charset map allocates large structures. */
942 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
944 charset_map_loaded = 0; \
945 c = DECODE_CHAR (charset, code); \
946 if (charset_map_loaded) \
948 const unsigned char *orig = coding->source; \
951 coding_set_source (coding); \
952 offset = coding->source - orig; \
954 src_base += offset; \
960 /* If there are at least BYTES length of room at dst, allocate memory
961 for coding->destination and update dst and dst_end. We don't have
962 to take care of coding->source which will be relocated. It is
963 handled by calling coding_set_source in encode_coding. */
965 #define ASSURE_DESTINATION(bytes) \
967 if (dst + (bytes) >= dst_end) \
969 int more_bytes = charbuf_end - charbuf + (bytes); \
971 dst = alloc_destination (coding, more_bytes, dst); \
972 dst_end = coding->destination + coding->dst_bytes; \
977 /* Store multibyte form of the character C in P, and advance P to the
978 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
979 never calls MAYBE_UNIFY_CHAR. */
981 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
983 if ((c) <= MAX_1_BYTE_CHAR) \
985 else if ((c) <= MAX_2_BYTE_CHAR) \
986 *(p)++ = (0xC0 | ((c) >> 6)), \
987 *(p)++ = (0x80 | ((c) & 0x3F)); \
988 else if ((c) <= MAX_3_BYTE_CHAR) \
989 *(p)++ = (0xE0 | ((c) >> 12)), \
990 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
991 *(p)++ = (0x80 | ((c) & 0x3F)); \
992 else if ((c) <= MAX_4_BYTE_CHAR) \
993 *(p)++ = (0xF0 | (c >> 18)), \
994 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
995 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
996 *(p)++ = (0x80 | (c & 0x3F)); \
997 else if ((c) <= MAX_5_BYTE_CHAR) \
999 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
1000 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1001 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1002 *(p)++ = (0x80 | (c & 0x3F)); \
1004 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1008 /* Return the character code of character whose multibyte form is at
1009 P, and advance P to the end of the multibyte form. This is like
1010 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1012 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1015 : ! ((p)[0] & 0x20) \
1017 ((((p)[-2] & 0x1F) << 6) \
1018 | ((p)[-1] & 0x3F) \
1019 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1020 : ! ((p)[0] & 0x10) \
1022 ((((p)[-3] & 0x0F) << 12) \
1023 | (((p)[-2] & 0x3F) << 6) \
1024 | ((p)[-1] & 0x3F))) \
1025 : ! ((p)[0] & 0x08) \
1027 ((((p)[-4] & 0xF) << 18) \
1028 | (((p)[-3] & 0x3F) << 12) \
1029 | (((p)[-2] & 0x3F) << 6) \
1030 | ((p)[-1] & 0x3F))) \
1032 ((((p)[-4] & 0x3F) << 18) \
1033 | (((p)[-3] & 0x3F) << 12) \
1034 | (((p)[-2] & 0x3F) << 6) \
1035 | ((p)[-1] & 0x3F))))
1039 coding_set_source (struct coding_system
*coding
)
1041 if (BUFFERP (coding
->src_object
))
1043 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1045 if (coding
->src_pos
< 0)
1046 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1048 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1050 else if (STRINGP (coding
->src_object
))
1052 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1055 /* Otherwise, the source is C string and is never relocated
1056 automatically. Thus we don't have to update anything. */
1061 coding_set_destination (struct coding_system
*coding
)
1063 if (BUFFERP (coding
->dst_object
))
1065 if (coding
->src_pos
< 0)
1067 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1068 coding
->dst_bytes
= (GAP_END_ADDR
1069 - (coding
->src_bytes
- coding
->consumed
)
1070 - coding
->destination
);
1074 /* We are sure that coding->dst_pos_byte is before the gap
1076 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1077 + coding
->dst_pos_byte
- BEG_BYTE
);
1078 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1079 - coding
->destination
);
1083 /* Otherwise, the destination is C string and is never relocated
1084 automatically. Thus we don't have to update anything. */
1090 coding_alloc_by_realloc (struct coding_system
*coding
, EMACS_INT bytes
)
1092 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1093 coding
->dst_bytes
+ bytes
);
1094 coding
->dst_bytes
+= bytes
;
1098 coding_alloc_by_making_gap (struct coding_system
*coding
,
1099 EMACS_INT gap_head_used
, EMACS_INT bytes
)
1101 if (EQ (coding
->src_object
, coding
->dst_object
))
1103 /* The gap may contain the produced data at the head and not-yet
1104 consumed data at the tail. To preserve those data, we at
1105 first make the gap size to zero, then increase the gap
1107 EMACS_INT add
= GAP_SIZE
;
1109 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1110 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1112 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1113 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1117 Lisp_Object this_buffer
;
1119 this_buffer
= Fcurrent_buffer ();
1120 set_buffer_internal (XBUFFER (coding
->dst_object
));
1122 set_buffer_internal (XBUFFER (this_buffer
));
1127 static unsigned char *
1128 alloc_destination (struct coding_system
*coding
, EMACS_INT nbytes
,
1131 EMACS_INT offset
= dst
- coding
->destination
;
1133 if (BUFFERP (coding
->dst_object
))
1135 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1137 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1140 coding_alloc_by_realloc (coding
, nbytes
);
1141 coding_set_destination (coding
);
1142 dst
= coding
->destination
+ offset
;
1146 /** Macros for annotations. */
1148 /* An annotation data is stored in the array coding->charbuf in this
1150 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1151 LENGTH is the number of elements in the annotation.
1152 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1153 NCHARS is the number of characters in the text annotated.
1155 The format of the following elements depend on ANNOTATION_MASK.
1157 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1159 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1161 NBYTES is the number of bytes specified in the header part of
1162 old-style emacs-mule encoding, or 0 for the other kind of
1165 METHOD is one of enum composition_method.
1167 Optional COMPOSITION-COMPONENTS are characters and composition
1170 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1173 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1174 recover from an invalid annotation, and should be skipped by
1175 produce_annotation. */
1177 /* Maximum length of the header of annotation data. */
1178 #define MAX_ANNOTATION_LENGTH 5
1180 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1182 *(buf)++ = -(len); \
1183 *(buf)++ = (mask); \
1184 *(buf)++ = (nchars); \
1185 coding->annotated = 1; \
1188 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1190 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1196 #define ADD_CHARSET_DATA(buf, nchars, id) \
1198 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1203 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1210 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1211 Check if a text is encoded in UTF-8. If it is, return 1, else
1214 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1215 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1216 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1217 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1218 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1219 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1221 #define UTF_BOM 0xFEFF
1222 #define UTF_8_BOM_1 0xEF
1223 #define UTF_8_BOM_2 0xBB
1224 #define UTF_8_BOM_3 0xBF
1227 detect_coding_utf_8 (struct coding_system
*coding
,
1228 struct coding_detection_info
*detect_info
)
1230 const unsigned char *src
= coding
->source
, *src_base
;
1231 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1232 int multibytep
= coding
->src_multibyte
;
1233 int consumed_chars
= 0;
1237 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1238 /* A coding system of this category is always ASCII compatible. */
1239 src
+= coding
->head_ascii
;
1243 int c
, c1
, c2
, c3
, c4
;
1247 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1250 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1252 if (UTF_8_2_OCTET_LEADING_P (c
))
1258 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1260 if (UTF_8_3_OCTET_LEADING_P (c
))
1263 if (src_base
== coding
->source
1264 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1269 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1271 if (UTF_8_4_OCTET_LEADING_P (c
))
1277 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1279 if (UTF_8_5_OCTET_LEADING_P (c
))
1286 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1290 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1292 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1297 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1298 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1302 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1304 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1311 decode_coding_utf_8 (struct coding_system
*coding
)
1313 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1314 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1315 const unsigned char *src_base
;
1316 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1317 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1318 int consumed_chars
= 0, consumed_chars_base
= 0;
1319 int multibytep
= coding
->src_multibyte
;
1320 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1321 Lisp_Object attr
, charset_list
;
1323 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1324 int byte_after_cr
= -1;
1326 CODING_GET_INFO (coding
, attr
, charset_list
);
1328 if (bom
!= utf_without_bom
)
1334 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1339 if (! UTF_8_EXTRA_OCTET_P (c2
))
1344 if (! UTF_8_EXTRA_OCTET_P (c3
))
1348 if ((c1
!= UTF_8_BOM_1
)
1349 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1352 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1357 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1361 int c
, c1
, c2
, c3
, c4
, c5
;
1364 consumed_chars_base
= consumed_chars
;
1366 if (charbuf
>= charbuf_end
)
1368 if (byte_after_cr
>= 0)
1373 if (byte_after_cr
>= 0)
1374 c1
= byte_after_cr
, byte_after_cr
= -1;
1381 else if (UTF_8_1_OCTET_P (c1
))
1383 if (eol_crlf
&& c1
== '\r')
1384 ONE_MORE_BYTE (byte_after_cr
);
1390 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1392 if (UTF_8_2_OCTET_LEADING_P (c1
))
1394 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1395 /* Reject overlong sequences here and below. Encoders
1396 producing them are incorrect, they can be misleading,
1397 and they mess up read/write invariance. */
1404 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1406 if (UTF_8_3_OCTET_LEADING_P (c1
))
1408 c
= (((c1
& 0xF) << 12)
1409 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1411 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1417 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1419 if (UTF_8_4_OCTET_LEADING_P (c1
))
1421 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1422 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1429 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1431 if (UTF_8_5_OCTET_LEADING_P (c1
))
1433 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1434 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1436 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1451 consumed_chars
= consumed_chars_base
;
1453 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1458 coding
->consumed_char
+= consumed_chars_base
;
1459 coding
->consumed
= src_base
- coding
->source
;
1460 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1465 encode_coding_utf_8 (struct coding_system
*coding
)
1467 int multibytep
= coding
->dst_multibyte
;
1468 int *charbuf
= coding
->charbuf
;
1469 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1470 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1471 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1472 int produced_chars
= 0;
1475 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1477 ASSURE_DESTINATION (3);
1478 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1479 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1484 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1486 while (charbuf
< charbuf_end
)
1488 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1490 ASSURE_DESTINATION (safe_room
);
1492 if (CHAR_BYTE8_P (c
))
1494 c
= CHAR_TO_BYTE8 (c
);
1499 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1500 for (p
= str
; p
< pend
; p
++)
1507 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1509 while (charbuf
< charbuf_end
)
1511 ASSURE_DESTINATION (safe_room
);
1513 if (CHAR_BYTE8_P (c
))
1514 *dst
++ = CHAR_TO_BYTE8 (c
);
1516 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1520 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1521 coding
->produced_char
+= produced_chars
;
1522 coding
->produced
= dst
- coding
->destination
;
1527 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1528 Check if a text is encoded in one of UTF-16 based coding systems.
1529 If it is, return 1, else return 0. */
1531 #define UTF_16_HIGH_SURROGATE_P(val) \
1532 (((val) & 0xFC00) == 0xD800)
1534 #define UTF_16_LOW_SURROGATE_P(val) \
1535 (((val) & 0xFC00) == 0xDC00)
1537 #define UTF_16_INVALID_P(val) \
1538 (((val) == 0xFFFE) \
1539 || ((val) == 0xFFFF) \
1540 || UTF_16_LOW_SURROGATE_P (val))
1544 detect_coding_utf_16 (struct coding_system
*coding
,
1545 struct coding_detection_info
*detect_info
)
1547 const unsigned char *src
= coding
->source
;
1548 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1549 int multibytep
= coding
->src_multibyte
;
1552 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1553 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1554 && (coding
->src_chars
& 1))
1556 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1560 TWO_MORE_BYTES (c1
, c2
);
1561 if ((c1
== 0xFF) && (c2
== 0xFE))
1563 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1564 | CATEGORY_MASK_UTF_16_AUTO
);
1565 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1566 | CATEGORY_MASK_UTF_16_BE_NOSIG
1567 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1569 else if ((c1
== 0xFE) && (c2
== 0xFF))
1571 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1572 | CATEGORY_MASK_UTF_16_AUTO
);
1573 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1574 | CATEGORY_MASK_UTF_16_BE_NOSIG
1575 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1579 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1584 /* We check the dispersion of Eth and Oth bytes where E is even and
1585 O is odd. If both are high, we assume binary data.*/
1586 unsigned char e
[256], o
[256];
1587 unsigned e_num
= 1, o_num
= 1;
1594 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1595 |CATEGORY_MASK_UTF_16_BE
1596 | CATEGORY_MASK_UTF_16_LE
);
1598 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1599 != CATEGORY_MASK_UTF_16
)
1601 TWO_MORE_BYTES (c1
, c2
);
1609 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1616 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1627 decode_coding_utf_16 (struct coding_system
*coding
)
1629 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1630 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1631 const unsigned char *src_base
;
1632 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1633 /* We may produces at most 3 chars in one loop. */
1634 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1635 int consumed_chars
= 0, consumed_chars_base
= 0;
1636 int multibytep
= coding
->src_multibyte
;
1637 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1638 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1639 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1640 Lisp_Object attr
, charset_list
;
1642 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1643 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1645 CODING_GET_INFO (coding
, attr
, charset_list
);
1647 if (bom
== utf_with_bom
)
1656 if (endian
== utf_16_big_endian
1657 ? c
!= 0xFEFF : c
!= 0xFFFE)
1659 /* The first two bytes are not BOM. Treat them as bytes
1660 for a normal character. */
1664 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1666 else if (bom
== utf_detect_bom
)
1668 /* We have already tried to detect BOM and failed in
1670 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1678 consumed_chars_base
= consumed_chars
;
1680 if (charbuf
>= charbuf_end
)
1682 if (byte_after_cr1
>= 0)
1687 if (byte_after_cr1
>= 0)
1688 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1696 if (byte_after_cr2
>= 0)
1697 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1702 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1706 c
= (endian
== utf_16_big_endian
1707 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1711 if (! UTF_16_LOW_SURROGATE_P (c
))
1713 if (endian
== utf_16_big_endian
)
1714 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1716 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1720 if (UTF_16_HIGH_SURROGATE_P (c
))
1721 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1727 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1728 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1729 *charbuf
++ = 0x10000 + c
;
1734 if (UTF_16_HIGH_SURROGATE_P (c
))
1735 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1738 if (eol_crlf
&& c
== '\r')
1740 ONE_MORE_BYTE (byte_after_cr1
);
1741 ONE_MORE_BYTE (byte_after_cr2
);
1749 coding
->consumed_char
+= consumed_chars_base
;
1750 coding
->consumed
= src_base
- coding
->source
;
1751 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1755 encode_coding_utf_16 (struct coding_system
*coding
)
1757 int multibytep
= coding
->dst_multibyte
;
1758 int *charbuf
= coding
->charbuf
;
1759 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1760 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1761 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1763 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1764 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1765 int produced_chars
= 0;
1766 Lisp_Object attrs
, charset_list
;
1769 CODING_GET_INFO (coding
, attrs
, charset_list
);
1771 if (bom
!= utf_without_bom
)
1773 ASSURE_DESTINATION (safe_room
);
1775 EMIT_TWO_BYTES (0xFE, 0xFF);
1777 EMIT_TWO_BYTES (0xFF, 0xFE);
1778 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1781 while (charbuf
< charbuf_end
)
1783 ASSURE_DESTINATION (safe_room
);
1785 if (c
> MAX_UNICODE_CHAR
)
1786 c
= coding
->default_char
;
1791 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1793 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1800 c1
= (c
>> 10) + 0xD800;
1801 c2
= (c
& 0x3FF) + 0xDC00;
1803 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1805 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1808 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1809 coding
->produced
= dst
- coding
->destination
;
1810 coding
->produced_char
+= produced_chars
;
1815 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1817 /* Emacs' internal format for representation of multiple character
1818 sets is a kind of multi-byte encoding, i.e. characters are
1819 represented by variable-length sequences of one-byte codes.
1821 ASCII characters and control characters (e.g. `tab', `newline') are
1822 represented by one-byte sequences which are their ASCII codes, in
1823 the range 0x00 through 0x7F.
1825 8-bit characters of the range 0x80..0x9F are represented by
1826 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1829 8-bit characters of the range 0xA0..0xFF are represented by
1830 one-byte sequences which are their 8-bit code.
1832 The other characters are represented by a sequence of `base
1833 leading-code', optional `extended leading-code', and one or two
1834 `position-code's. The length of the sequence is determined by the
1835 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1836 whereas extended leading-code and position-code take the range 0xA0
1837 through 0xFF. See `charset.h' for more details about leading-code
1840 --- CODE RANGE of Emacs' internal format ---
1844 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1845 eight-bit-graphic 0xA0..0xBF
1846 ELSE 0x81..0x9D + [0xA0..0xFF]+
1847 ---------------------------------------------
1849 As this is the internal character representation, the format is
1850 usually not used externally (i.e. in a file or in a data sent to a
1851 process). But, it is possible to have a text externally in this
1852 format (i.e. by encoding by the coding system `emacs-mule').
1854 In that case, a sequence of one-byte codes has a slightly different
1857 At first, all characters in eight-bit-control are represented by
1858 one-byte sequences which are their 8-bit code.
1860 Next, character composition data are represented by the byte
1861 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1863 METHOD is 0xF2 plus one of composition method (enum
1864 composition_method),
1866 BYTES is 0xA0 plus a byte length of this composition data,
1868 CHARS is 0xA0 plus a number of characters composed by this
1871 COMPONENTs are characters of multibyte form or composition
1872 rules encoded by two-byte of ASCII codes.
1874 In addition, for backward compatibility, the following formats are
1875 also recognized as composition data on decoding.
1878 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1881 MSEQ is a multibyte form but in these special format:
1882 ASCII: 0xA0 ASCII_CODE+0x80,
1883 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1884 RULE is a one byte code of the range 0xA0..0xF0 that
1885 represents a composition rule.
1888 char emacs_mule_bytes
[256];
1891 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1892 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1896 detect_coding_emacs_mule (struct coding_system
*coding
,
1897 struct coding_detection_info
*detect_info
)
1899 const unsigned char *src
= coding
->source
, *src_base
;
1900 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1901 int multibytep
= coding
->src_multibyte
;
1902 int consumed_chars
= 0;
1906 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1907 /* A coding system of this category is always ASCII compatible. */
1908 src
+= coding
->head_ascii
;
1918 /* Perhaps the start of composite character. We simply skip
1919 it because analyzing it is too heavy for detecting. But,
1920 at least, we check that the composite character
1921 constitutes of more than 4 bytes. */
1922 const unsigned char *src_base
;
1932 if (src
- src_base
<= 4)
1934 found
= CATEGORY_MASK_EMACS_MULE
;
1942 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1947 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1949 while (more_bytes
> 0)
1954 src
--; /* Unread the last byte. */
1959 if (more_bytes
!= 0)
1961 found
= CATEGORY_MASK_EMACS_MULE
;
1964 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1968 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1970 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1973 detect_info
->found
|= found
;
1978 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1979 character. If CMP_STATUS indicates that we must expect MSEQ or
1980 RULE described above, decode it and return the negative value of
1981 the decoded character or rule. If an invalid byte is found, return
1982 -1. If SRC is too short, return -2. */
1985 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1986 int *nbytes
, int *nchars
, int *id
,
1987 struct composition_status
*cmp_status
)
1989 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1990 const unsigned char *src_base
= src
;
1991 int multibytep
= coding
->src_multibyte
;
1995 int consumed_chars
= 0;
2002 charset_id
= emacs_mule_charset
[0];
2008 if (cmp_status
->state
!= COMPOSING_NO
2009 && cmp_status
->old_form
)
2011 if (cmp_status
->state
== COMPOSING_CHAR
)
2026 *nbytes
= src
- src_base
;
2027 *nchars
= consumed_chars
;
2035 switch (emacs_mule_bytes
[c
])
2038 if ((charset_id
= emacs_mule_charset
[c
]) < 0)
2047 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2048 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2051 if (c
< 0xA0 || (charset_id
= emacs_mule_charset
[c
]) < 0)
2060 if ((charset_id
= emacs_mule_charset
[c
]) < 0)
2065 code
= (c
& 0x7F) << 8;
2075 if (c
< 0 || (charset_id
= emacs_mule_charset
[c
]) < 0)
2080 code
= (c
& 0x7F) << 8;
2089 charset_id
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2095 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2096 CHARSET_FROM_ID (charset_id
), code
, c
);
2100 *nbytes
= src
- src_base
;
2101 *nchars
= consumed_chars
;
2104 return (mseq_found
? -c
: c
);
2114 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2116 /* Handle these composition sequence ('|': the end of header elements,
2117 BYTES and CHARS >= 0xA0):
2119 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2120 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2121 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2125 (4) relative composition: 0x80 | MSEQ ... MSEQ
2126 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2128 When the starter 0x80 and the following header elements are found,
2129 this annotation header is produced.
2131 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2133 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2134 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2136 Then, upon reading the following elements, these codes are produced
2137 until the composition end is found:
2140 (2) ALT ... ALT CHAR ... CHAR
2141 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2143 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2145 When the composition end is found, LENGTH and NCHARS in the
2146 annotation header is updated as below:
2148 (1) LENGTH: unchanged, NCHARS: unchanged
2149 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2150 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2151 (4) LENGTH: unchanged, NCHARS: number of CHARs
2152 (5) LENGTH: unchanged, NCHARS: number of CHARs
2154 If an error is found while composing, the annotation header is
2155 changed to the original composition header (plus filler -1s) as
2158 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2159 (5) [ 0x80 0xFF -1 -1- -1 ]
2161 and the sequence [ -2 DECODED-RULE ] is changed to the original
2162 byte sequence as below:
2163 o the original byte sequence is B: [ B -1 ]
2164 o the original byte sequence is B1 B2: [ B1 B2 ]
2166 Most of the routines are implemented by macros because many
2167 variables and labels in the caller decode_coding_emacs_mule must be
2168 accessible, and they are usually called just once (thus doesn't
2169 increase the size of compiled object). */
2171 /* Decode a composition rule represented by C as a component of
2172 composition sequence of Emacs 20 style. Set RULE to the decoded
2175 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2180 if (c < 0 || c >= 81) \
2181 goto invalid_code; \
2182 gref = c / 9, nref = c % 9; \
2183 if (gref == 4) gref = 10; \
2184 if (nref == 4) nref = 10; \
2185 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2189 /* Decode a composition rule represented by C and the following byte
2190 at SRC as a component of composition sequence of Emacs 21 style.
2191 Set RULE to the decoded rule. */
2193 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2198 if (gref < 0 || gref >= 81) \
2199 goto invalid_code; \
2200 ONE_MORE_BYTE (c); \
2202 if (nref < 0 || nref >= 81) \
2203 goto invalid_code; \
2204 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2208 /* Start of Emacs 21 style format. The first three bytes at SRC are
2209 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2210 byte length of this composition information, CHARS is the number of
2211 characters composed by this composition. */
2213 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2215 enum composition_method method = c - 0xF2; \
2216 int nbytes, nchars; \
2218 ONE_MORE_BYTE (c); \
2220 goto invalid_code; \
2221 nbytes = c - 0xA0; \
2222 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2223 goto invalid_code; \
2224 ONE_MORE_BYTE (c); \
2225 nchars = c - 0xA0; \
2226 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2227 goto invalid_code; \
2228 cmp_status->old_form = 0; \
2229 cmp_status->method = method; \
2230 if (method == COMPOSITION_RELATIVE) \
2231 cmp_status->state = COMPOSING_CHAR; \
2233 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2234 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2235 cmp_status->nchars = nchars; \
2236 cmp_status->ncomps = nbytes - 4; \
2237 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2241 /* Start of Emacs 20 style format for relative composition. */
2243 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2245 cmp_status->old_form = 1; \
2246 cmp_status->method = COMPOSITION_RELATIVE; \
2247 cmp_status->state = COMPOSING_CHAR; \
2248 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2249 cmp_status->nchars = cmp_status->ncomps = 0; \
2250 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2254 /* Start of Emacs 20 style format for rule-base composition. */
2256 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2258 cmp_status->old_form = 1; \
2259 cmp_status->method = COMPOSITION_WITH_RULE; \
2260 cmp_status->state = COMPOSING_CHAR; \
2261 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2262 cmp_status->nchars = cmp_status->ncomps = 0; \
2263 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2267 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2269 const unsigned char *current_src = src; \
2271 ONE_MORE_BYTE (c); \
2273 goto invalid_code; \
2274 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2275 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2276 DECODE_EMACS_MULE_21_COMPOSITION (); \
2277 else if (c < 0xA0) \
2278 goto invalid_code; \
2279 else if (c < 0xC0) \
2281 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2282 /* Re-read C as a composition component. */ \
2283 src = current_src; \
2285 else if (c == 0xFF) \
2286 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2288 goto invalid_code; \
2291 #define EMACS_MULE_COMPOSITION_END() \
2293 int idx = - cmp_status->length; \
2295 if (cmp_status->old_form) \
2296 charbuf[idx + 2] = cmp_status->nchars; \
2297 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2298 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2299 cmp_status->state = COMPOSING_NO; \
2304 emacs_mule_finish_composition (int *charbuf
,
2305 struct composition_status
*cmp_status
)
2307 int idx
= - cmp_status
->length
;
2310 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2312 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2314 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2315 && cmp_status
->state
== COMPOSING_CHAR
)
2317 /* The last rule was invalid. */
2318 int rule
= charbuf
[-1] + 0xA0;
2320 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2327 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2329 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2331 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2332 charbuf
[idx
++] = -3;
2338 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2339 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2341 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2342 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2343 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2344 charbuf
[idx
++] = -1;
2348 cmp_status
->state
= COMPOSING_NO
;
2352 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2354 if (cmp_status->state != COMPOSING_NO) \
2355 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2360 decode_coding_emacs_mule (struct coding_system
*coding
)
2362 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2363 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2364 const unsigned char *src_base
;
2365 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2366 /* We may produce two annotations (charset and composition) in one
2367 loop and one more charset annotation at the end. */
2369 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
2370 int consumed_chars
= 0, consumed_chars_base
;
2371 int multibytep
= coding
->src_multibyte
;
2372 Lisp_Object attrs
, charset_list
;
2373 int char_offset
= coding
->produced_char
;
2374 int last_offset
= char_offset
;
2375 int last_id
= charset_ascii
;
2377 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2378 int byte_after_cr
= -1;
2379 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2381 CODING_GET_INFO (coding
, attrs
, charset_list
);
2383 if (cmp_status
->state
!= COMPOSING_NO
)
2387 for (i
= 0; i
< cmp_status
->length
; i
++)
2388 *charbuf
++ = cmp_status
->carryover
[i
];
2389 coding
->annotated
= 1;
2397 consumed_chars_base
= consumed_chars
;
2399 if (charbuf
>= charbuf_end
)
2401 if (byte_after_cr
>= 0)
2406 if (byte_after_cr
>= 0)
2407 c
= byte_after_cr
, byte_after_cr
= -1;
2411 if (c
< 0 || c
== 0x80)
2413 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2420 DECODE_EMACS_MULE_COMPOSITION_START ();
2426 if (eol_crlf
&& c
== '\r')
2427 ONE_MORE_BYTE (byte_after_cr
);
2429 if (cmp_status
->state
!= COMPOSING_NO
)
2431 if (cmp_status
->old_form
)
2432 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2433 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2434 cmp_status
->ncomps
--;
2440 /* emacs_mule_char can load a charset map from a file, which
2441 allocates a large structure and might cause buffer text
2442 to be relocated as result. Thus, we need to remember the
2443 original pointer to buffer text, and fix up all related
2444 pointers after the call. */
2445 const unsigned char *orig
= coding
->source
;
2448 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2450 offset
= coding
->source
- orig
;
2464 src
= src_base
+ nbytes
;
2465 consumed_chars
= consumed_chars_base
+ nchars
;
2466 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2467 cmp_status
->ncomps
-= nchars
;
2470 /* Now if C >= 0, we found a normally encoded character, if C <
2471 0, we found an old-style composition component character or
2474 if (cmp_status
->state
== COMPOSING_NO
)
2478 if (last_id
!= charset_ascii
)
2479 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2482 last_offset
= char_offset
;
2487 else if (cmp_status
->state
== COMPOSING_CHAR
)
2489 if (cmp_status
->old_form
)
2493 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2500 cmp_status
->nchars
++;
2501 cmp_status
->length
++;
2502 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2503 EMACS_MULE_COMPOSITION_END ();
2504 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2505 cmp_status
->state
= COMPOSING_RULE
;
2511 cmp_status
->length
++;
2512 cmp_status
->nchars
--;
2513 if (cmp_status
->nchars
== 0)
2514 EMACS_MULE_COMPOSITION_END ();
2517 else if (cmp_status
->state
== COMPOSING_RULE
)
2523 EMACS_MULE_COMPOSITION_END ();
2530 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2535 cmp_status
->length
+= 2;
2536 cmp_status
->state
= COMPOSING_CHAR
;
2539 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2542 cmp_status
->length
++;
2543 if (cmp_status
->ncomps
== 0)
2544 cmp_status
->state
= COMPOSING_CHAR
;
2545 else if (cmp_status
->ncomps
> 0)
2547 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2548 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2551 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2553 else /* COMPOSING_COMPONENT_RULE */
2557 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2562 cmp_status
->length
+= 2;
2563 cmp_status
->ncomps
--;
2564 if (cmp_status
->ncomps
> 0)
2565 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2567 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2572 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2574 consumed_chars
= consumed_chars_base
;
2576 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2582 if (cmp_status
->state
!= COMPOSING_NO
)
2584 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2585 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2590 charbuf
-= cmp_status
->length
;
2591 for (i
= 0; i
< cmp_status
->length
; i
++)
2592 cmp_status
->carryover
[i
] = charbuf
[i
];
2595 if (last_id
!= charset_ascii
)
2596 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2597 coding
->consumed_char
+= consumed_chars_base
;
2598 coding
->consumed
= src_base
- coding
->source
;
2599 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2603 #define EMACS_MULE_LEADING_CODES(id, codes) \
2606 codes[0] = id, codes[1] = 0; \
2607 else if (id < 0xE0) \
2608 codes[0] = 0x9A, codes[1] = id; \
2609 else if (id < 0xF0) \
2610 codes[0] = 0x9B, codes[1] = id; \
2611 else if (id < 0xF5) \
2612 codes[0] = 0x9C, codes[1] = id; \
2614 codes[0] = 0x9D, codes[1] = id; \
2619 encode_coding_emacs_mule (struct coding_system
*coding
)
2621 int multibytep
= coding
->dst_multibyte
;
2622 int *charbuf
= coding
->charbuf
;
2623 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2624 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2625 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2627 int produced_chars
= 0;
2628 Lisp_Object attrs
, charset_list
;
2630 int preferred_charset_id
= -1;
2632 CODING_GET_INFO (coding
, attrs
, charset_list
);
2633 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2635 CODING_ATTR_CHARSET_LIST (attrs
)
2636 = charset_list
= Vemacs_mule_charset_list
;
2639 while (charbuf
< charbuf_end
)
2641 ASSURE_DESTINATION (safe_room
);
2646 /* Handle an annotation. */
2649 case CODING_ANNOTATE_COMPOSITION_MASK
:
2650 /* Not yet implemented. */
2652 case CODING_ANNOTATE_CHARSET_MASK
:
2653 preferred_charset_id
= charbuf
[3];
2654 if (preferred_charset_id
>= 0
2655 && NILP (Fmemq (make_number (preferred_charset_id
),
2657 preferred_charset_id
= -1;
2666 if (ASCII_CHAR_P (c
))
2667 EMIT_ONE_ASCII_BYTE (c
);
2668 else if (CHAR_BYTE8_P (c
))
2670 c
= CHAR_TO_BYTE8 (c
);
2675 struct charset
*charset
;
2679 unsigned char leading_codes
[2];
2681 if (preferred_charset_id
>= 0)
2683 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2684 if (CHAR_CHARSET_P (c
, charset
))
2685 code
= ENCODE_CHAR (charset
, c
);
2687 charset
= char_charset (c
, charset_list
, &code
);
2690 charset
= char_charset (c
, charset_list
, &code
);
2693 c
= coding
->default_char
;
2694 if (ASCII_CHAR_P (c
))
2696 EMIT_ONE_ASCII_BYTE (c
);
2699 charset
= char_charset (c
, charset_list
, &code
);
2701 dimension
= CHARSET_DIMENSION (charset
);
2702 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2703 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2704 EMIT_ONE_BYTE (leading_codes
[0]);
2705 if (leading_codes
[1])
2706 EMIT_ONE_BYTE (leading_codes
[1]);
2708 EMIT_ONE_BYTE (code
| 0x80);
2712 EMIT_ONE_BYTE (code
>> 8);
2713 EMIT_ONE_BYTE (code
& 0xFF);
2717 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2718 coding
->produced_char
+= produced_chars
;
2719 coding
->produced
= dst
- coding
->destination
;
2724 /*** 7. ISO2022 handlers ***/
2726 /* The following note describes the coding system ISO2022 briefly.
2727 Since the intention of this note is to help understand the
2728 functions in this file, some parts are NOT ACCURATE or are OVERLY
2729 SIMPLIFIED. For thorough understanding, please refer to the
2730 original document of ISO2022. This is equivalent to the standard
2731 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2733 ISO2022 provides many mechanisms to encode several character sets
2734 in 7-bit and 8-bit environments. For 7-bit environments, all text
2735 is encoded using bytes less than 128. This may make the encoded
2736 text a little bit longer, but the text passes more easily through
2737 several types of gateway, some of which strip off the MSB (Most
2740 There are two kinds of character sets: control character sets and
2741 graphic character sets. The former contain control characters such
2742 as `newline' and `escape' to provide control functions (control
2743 functions are also provided by escape sequences). The latter
2744 contain graphic characters such as 'A' and '-'. Emacs recognizes
2745 two control character sets and many graphic character sets.
2747 Graphic character sets are classified into one of the following
2748 four classes, according to the number of bytes (DIMENSION) and
2749 number of characters in one dimension (CHARS) of the set:
2750 - DIMENSION1_CHARS94
2751 - DIMENSION1_CHARS96
2752 - DIMENSION2_CHARS94
2753 - DIMENSION2_CHARS96
2755 In addition, each character set is assigned an identification tag,
2756 unique for each set, called the "final character" (denoted as <F>
2757 hereafter). The <F> of each character set is decided by ECMA(*)
2758 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2759 (0x30..0x3F are for private use only).
2761 Note (*): ECMA = European Computer Manufacturers Association
2763 Here are examples of graphic character sets [NAME(<F>)]:
2764 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2765 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2766 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2767 o DIMENSION2_CHARS96 -- none for the moment
2769 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2770 C0 [0x00..0x1F] -- control character plane 0
2771 GL [0x20..0x7F] -- graphic character plane 0
2772 C1 [0x80..0x9F] -- control character plane 1
2773 GR [0xA0..0xFF] -- graphic character plane 1
2775 A control character set is directly designated and invoked to C0 or
2776 C1 by an escape sequence. The most common case is that:
2777 - ISO646's control character set is designated/invoked to C0, and
2778 - ISO6429's control character set is designated/invoked to C1,
2779 and usually these designations/invocations are omitted in encoded
2780 text. In a 7-bit environment, only C0 can be used, and a control
2781 character for C1 is encoded by an appropriate escape sequence to
2782 fit into the environment. All control characters for C1 are
2783 defined to have corresponding escape sequences.
2785 A graphic character set is at first designated to one of four
2786 graphic registers (G0 through G3), then these graphic registers are
2787 invoked to GL or GR. These designations and invocations can be
2788 done independently. The most common case is that G0 is invoked to
2789 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2790 these invocations and designations are omitted in encoded text.
2791 In a 7-bit environment, only GL can be used.
2793 When a graphic character set of CHARS94 is invoked to GL, codes
2794 0x20 and 0x7F of the GL area work as control characters SPACE and
2795 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2798 There are two ways of invocation: locking-shift and single-shift.
2799 With locking-shift, the invocation lasts until the next different
2800 invocation, whereas with single-shift, the invocation affects the
2801 following character only and doesn't affect the locking-shift
2802 state. Invocations are done by the following control characters or
2805 ----------------------------------------------------------------------
2806 abbrev function cntrl escape seq description
2807 ----------------------------------------------------------------------
2808 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2809 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2810 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2811 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2812 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2813 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2814 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2815 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2816 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2817 ----------------------------------------------------------------------
2818 (*) These are not used by any known coding system.
2820 Control characters for these functions are defined by macros
2821 ISO_CODE_XXX in `coding.h'.
2823 Designations are done by the following escape sequences:
2824 ----------------------------------------------------------------------
2825 escape sequence description
2826 ----------------------------------------------------------------------
2827 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2828 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2829 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2830 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2831 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2832 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2833 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2834 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2835 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2836 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2837 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2838 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2839 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2840 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2841 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2842 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2843 ----------------------------------------------------------------------
2845 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2846 of dimension 1, chars 94, and final character <F>, etc...
2848 Note (*): Although these designations are not allowed in ISO2022,
2849 Emacs accepts them on decoding, and produces them on encoding
2850 CHARS96 character sets in a coding system which is characterized as
2851 7-bit environment, non-locking-shift, and non-single-shift.
2853 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2854 '(' must be omitted. We refer to this as "short-form" hereafter.
2856 Now you may notice that there are a lot of ways of encoding the
2857 same multilingual text in ISO2022. Actually, there exist many
2858 coding systems such as Compound Text (used in X11's inter client
2859 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2860 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2861 localized platforms), and all of these are variants of ISO2022.
2863 In addition to the above, Emacs handles two more kinds of escape
2864 sequences: ISO6429's direction specification and Emacs' private
2865 sequence for specifying character composition.
2867 ISO6429's direction specification takes the following form:
2868 o CSI ']' -- end of the current direction
2869 o CSI '0' ']' -- end of the current direction
2870 o CSI '1' ']' -- start of left-to-right text
2871 o CSI '2' ']' -- start of right-to-left text
2872 The control character CSI (0x9B: control sequence introducer) is
2873 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2875 Character composition specification takes the following form:
2876 o ESC '0' -- start relative composition
2877 o ESC '1' -- end composition
2878 o ESC '2' -- start rule-base composition (*)
2879 o ESC '3' -- start relative composition with alternate chars (**)
2880 o ESC '4' -- start rule-base composition with alternate chars (**)
2881 Since these are not standard escape sequences of any ISO standard,
2882 the use of them with these meanings is restricted to Emacs only.
2884 (*) This form is used only in Emacs 20.7 and older versions,
2885 but newer versions can safely decode it.
2886 (**) This form is used only in Emacs 21.1 and newer versions,
2887 and older versions can't decode it.
2889 Here's a list of example usages of these composition escape
2890 sequences (categorized by `enum composition_method').
2892 COMPOSITION_RELATIVE:
2893 ESC 0 CHAR [ CHAR ] ESC 1
2894 COMPOSITION_WITH_RULE:
2895 ESC 2 CHAR [ RULE CHAR ] ESC 1
2896 COMPOSITION_WITH_ALTCHARS:
2897 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2898 COMPOSITION_WITH_RULE_ALTCHARS:
2899 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2901 enum iso_code_class_type iso_code_class
[256];
2903 #define SAFE_CHARSET_P(coding, id) \
2904 ((id) <= (coding)->max_charset_id \
2905 && (coding)->safe_charsets[id] != 255)
2908 #define SHIFT_OUT_OK(category) \
2909 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2912 setup_iso_safe_charsets (Lisp_Object attrs
)
2914 Lisp_Object charset_list
, safe_charsets
;
2915 Lisp_Object request
;
2916 Lisp_Object reg_usage
;
2919 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2922 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2923 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2924 && ! EQ (charset_list
, Viso_2022_charset_list
))
2926 CODING_ATTR_CHARSET_LIST (attrs
)
2927 = charset_list
= Viso_2022_charset_list
;
2928 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2931 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2935 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2937 int id
= XINT (XCAR (tail
));
2938 if (max_charset_id
< id
)
2939 max_charset_id
= id
;
2942 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2943 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2944 request
= AREF (attrs
, coding_attr_iso_request
);
2945 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2946 reg94
= XINT (XCAR (reg_usage
));
2947 reg96
= XINT (XCDR (reg_usage
));
2949 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2953 struct charset
*charset
;
2956 charset
= CHARSET_FROM_ID (XINT (id
));
2957 reg
= Fcdr (Fassq (id
, request
));
2959 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2960 else if (charset
->iso_chars_96
)
2963 SSET (safe_charsets
, XINT (id
), reg96
);
2968 SSET (safe_charsets
, XINT (id
), reg94
);
2971 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2975 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2976 Check if a text is encoded in one of ISO-2022 based coding systems.
2977 If it is, return 1, else return 0. */
2980 detect_coding_iso_2022 (struct coding_system
*coding
,
2981 struct coding_detection_info
*detect_info
)
2983 const unsigned char *src
= coding
->source
, *src_base
= src
;
2984 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2985 int multibytep
= coding
->src_multibyte
;
2986 int single_shifting
= 0;
2989 int consumed_chars
= 0;
2993 int composition_count
= -1;
2995 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2997 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2999 struct coding_system
*this = &(coding_categories
[i
]);
3000 Lisp_Object attrs
, val
;
3004 attrs
= CODING_ID_ATTRS (this->id
);
3005 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
3006 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
3007 setup_iso_safe_charsets (attrs
);
3008 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
3009 this->max_charset_id
= SCHARS (val
) - 1;
3010 this->safe_charsets
= SDATA (val
);
3013 /* A coding system of this category is always ASCII compatible. */
3014 src
+= coding
->head_ascii
;
3016 while (rejected
!= CATEGORY_MASK_ISO
)
3023 if (inhibit_iso_escape_detection
)
3025 single_shifting
= 0;
3027 if (c
>= '(' && c
<= '/')
3029 /* Designation sequence for a charset of dimension 1. */
3031 if (c1
< ' ' || c1
>= 0x80
3032 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3033 /* Invalid designation sequence. Just ignore. */
3038 /* Designation sequence for a charset of dimension 2. */
3040 if (c
>= '@' && c
<= 'B')
3041 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3042 id
= iso_charset_table
[1][0][c
];
3043 else if (c
>= '(' && c
<= '/')
3046 if (c1
< ' ' || c1
>= 0x80
3047 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3048 /* Invalid designation sequence. Just ignore. */
3052 /* Invalid designation sequence. Just ignore it. */
3055 else if (c
== 'N' || c
== 'O')
3057 /* ESC <Fe> for SS2 or SS3. */
3058 single_shifting
= 1;
3059 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3064 /* End of composition. */
3065 if (composition_count
< 0
3066 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3069 composition_count
= -1;
3070 found
|= CATEGORY_MASK_ISO
;
3072 else if (c
>= '0' && c
<= '4')
3074 /* ESC <Fp> for start/end composition. */
3075 composition_count
= 0;
3080 /* Invalid escape sequence. Just ignore it. */
3084 /* We found a valid designation sequence for CHARSET. */
3085 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3086 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3088 found
|= CATEGORY_MASK_ISO_7
;
3090 rejected
|= CATEGORY_MASK_ISO_7
;
3091 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3093 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3095 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3096 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3098 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3100 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3101 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3103 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3105 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3110 /* Locking shift out/in. */
3111 if (inhibit_iso_escape_detection
)
3113 single_shifting
= 0;
3114 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3118 /* Control sequence introducer. */
3119 single_shifting
= 0;
3120 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3121 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3122 goto check_extra_latin
;
3127 if (inhibit_iso_escape_detection
)
3129 single_shifting
= 0;
3130 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3131 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3132 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3133 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
3134 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3135 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3136 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
3137 if (single_shifting
)
3139 goto check_extra_latin
;
3146 if (composition_count
>= 0)
3147 composition_count
++;
3148 single_shifting
= 0;
3153 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3154 found
|= CATEGORY_MASK_ISO_8_1
;
3155 /* Check the length of succeeding codes of the range
3156 0xA0..0FF. If the byte length is even, we include
3157 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3158 only when we are not single shifting. */
3159 if (! single_shifting
3160 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3163 while (src
< src_end
)
3175 if (i
& 1 && src
< src_end
)
3177 rejected
|= CATEGORY_MASK_ISO_8_2
;
3178 if (composition_count
>= 0)
3179 composition_count
+= i
;
3183 found
|= CATEGORY_MASK_ISO_8_2
;
3184 if (composition_count
>= 0)
3185 composition_count
+= i
/ 2;
3191 single_shifting
= 0;
3192 if (! VECTORP (Vlatin_extra_code_table
)
3193 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3195 rejected
= CATEGORY_MASK_ISO
;
3198 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3199 & CODING_ISO_FLAG_LATIN_EXTRA
)
3200 found
|= CATEGORY_MASK_ISO_8_1
;
3202 rejected
|= CATEGORY_MASK_ISO_8_1
;
3203 rejected
|= CATEGORY_MASK_ISO_8_2
;
3206 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3210 detect_info
->rejected
|= rejected
;
3211 detect_info
->found
|= (found
& ~rejected
);
3216 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3217 escape sequence should be kept. */
3218 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3222 if (final < '0' || final >= 128 \
3223 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3224 || !SAFE_CHARSET_P (coding, id)) \
3226 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3230 prev = CODING_ISO_DESIGNATION (coding, reg); \
3231 if (id == charset_jisx0201_roman) \
3233 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3234 id = charset_ascii; \
3236 else if (id == charset_jisx0208_1978) \
3238 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3239 id = charset_jisx0208; \
3241 CODING_ISO_DESIGNATION (coding, reg) = id; \
3242 /* If there was an invalid designation to REG previously, and this \
3243 designation is ASCII to REG, we should keep this designation \
3245 if (prev == -2 && id == charset_ascii) \
3250 /* Handle these composition sequence (ALT: alternate char):
3252 (1) relative composition: ESC 0 CHAR ... ESC 1
3253 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3254 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3255 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3257 When the start sequence (ESC 0/2/3/4) is found, this annotation
3260 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3262 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3263 produced until the end sequence (ESC 1) is found:
3266 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3267 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3268 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3270 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3271 annotation header is updated as below:
3273 (1) LENGTH: unchanged, NCHARS: number of CHARs
3274 (2) LENGTH: unchanged, NCHARS: number of CHARs
3275 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3276 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3278 If an error is found while composing, the annotation header is
3281 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3283 and the sequence [ -2 DECODED-RULE ] is changed to the original
3284 byte sequence as below:
3285 o the original byte sequence is B: [ B -1 ]
3286 o the original byte sequence is B1 B2: [ B1 B2 ]
3287 and the sequence [ -1 -1 ] is changed to the original byte
3292 /* Decode a composition rule C1 and maybe one more byte from the
3293 source, and set RULE to the encoded composition rule, NBYTES to the
3294 length of the composition rule. If the rule is invalid, set RULE
3295 to some negative value. */
3297 #define DECODE_COMPOSITION_RULE(rule, nbytes) \
3302 if (rule < 81) /* old format (before ver.21) */ \
3304 int gref = (rule) / 9; \
3305 int nref = (rule) % 9; \
3306 if (gref == 4) gref = 10; \
3307 if (nref == 4) nref = 10; \
3308 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3311 else /* new format (after ver.21) */ \
3315 ONE_MORE_BYTE (c); \
3316 rule = COMPOSITION_ENCODE_RULE (rule - 81, c - 32); \
3318 rule += 0x100; /* to destinguish it from the old format */ \
3323 #define ENCODE_COMPOSITION_RULE(rule) \
3325 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3327 if (rule < 0x100) /* old format */ \
3329 if (gref == 10) gref = 4; \
3330 if (nref == 10) nref = 4; \
3331 charbuf[idx] = 32 + gref * 9 + nref; \
3332 charbuf[idx + 1] = -1; \
3335 else /* new format */ \
3337 charbuf[idx] = 32 + 81 + gref; \
3338 charbuf[idx + 1] = 32 + nref; \
3343 /* Finish the current composition as invalid. */
3345 static int finish_composition (int *, struct composition_status
*);
3348 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3350 int idx
= - cmp_status
->length
;
3353 /* Recover the original ESC sequence */
3354 charbuf
[idx
++] = ISO_CODE_ESC
;
3355 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3356 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3357 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3358 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3360 charbuf
[idx
++] = -2;
3362 charbuf
[idx
++] = -1;
3363 new_chars
= cmp_status
->nchars
;
3364 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3365 for (; idx
< 0; idx
++)
3367 int elt
= charbuf
[idx
];
3371 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3376 charbuf
[idx
++] = ISO_CODE_ESC
;
3381 cmp_status
->state
= COMPOSING_NO
;
3385 /* If characters are under composition, finish the composition. */
3386 #define MAYBE_FINISH_COMPOSITION() \
3388 if (cmp_status->state != COMPOSING_NO) \
3389 char_offset += finish_composition (charbuf, cmp_status); \
3392 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3394 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3395 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3396 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3397 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3399 Produce this annotation sequence now:
3401 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3404 #define DECODE_COMPOSITION_START(c1) \
3407 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3408 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3409 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3410 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3414 cmp_status->state = COMPOSING_CHAR; \
3415 cmp_status->length += 2; \
3419 MAYBE_FINISH_COMPOSITION (); \
3420 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3421 : c1 == '2' ? COMPOSITION_WITH_RULE \
3422 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3423 : COMPOSITION_WITH_RULE_ALTCHARS); \
3425 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3426 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3427 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3428 cmp_status->nchars = cmp_status->ncomps = 0; \
3429 coding->annotated = 1; \
3434 /* Handle composition end sequence ESC 1. */
3436 #define DECODE_COMPOSITION_END() \
3438 if (cmp_status->nchars == 0 \
3439 || ((cmp_status->state == COMPOSING_CHAR) \
3440 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3442 MAYBE_FINISH_COMPOSITION (); \
3443 goto invalid_code; \
3445 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3446 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3447 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3448 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3449 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3450 char_offset += cmp_status->nchars; \
3451 cmp_status->state = COMPOSING_NO; \
3454 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3456 #define STORE_COMPOSITION_RULE(rule) \
3459 *charbuf++ = rule; \
3460 cmp_status->length += 2; \
3461 cmp_status->state--; \
3464 /* Store a composed char or a component char C in charbuf, and update
3467 #define STORE_COMPOSITION_CHAR(c) \
3470 cmp_status->length++; \
3471 if (cmp_status->state == COMPOSING_CHAR) \
3472 cmp_status->nchars++; \
3474 cmp_status->ncomps++; \
3475 if (cmp_status->method == COMPOSITION_WITH_RULE \
3476 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3477 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3478 cmp_status->state++; \
3482 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3485 decode_coding_iso_2022 (struct coding_system
*coding
)
3487 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3488 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3489 const unsigned char *src_base
;
3490 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3491 /* We may produce two annotations (charset and composition) in one
3492 loop and one more charset annotation at the end. */
3494 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3495 int consumed_chars
= 0, consumed_chars_base
;
3496 int multibytep
= coding
->src_multibyte
;
3497 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3498 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3499 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3500 int charset_id_2
, charset_id_3
;
3501 struct charset
*charset
;
3503 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3504 Lisp_Object attrs
, charset_list
;
3505 int char_offset
= coding
->produced_char
;
3506 int last_offset
= char_offset
;
3507 int last_id
= charset_ascii
;
3509 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3510 int byte_after_cr
= -1;
3513 CODING_GET_INFO (coding
, attrs
, charset_list
);
3514 setup_iso_safe_charsets (attrs
);
3515 /* Charset list may have been changed. */
3516 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3517 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3519 if (cmp_status
->state
!= COMPOSING_NO
)
3521 for (i
= 0; i
< cmp_status
->length
; i
++)
3522 *charbuf
++ = cmp_status
->carryover
[i
];
3523 coding
->annotated
= 1;
3531 consumed_chars_base
= consumed_chars
;
3533 if (charbuf
>= charbuf_end
)
3535 if (byte_after_cr
>= 0)
3540 if (byte_after_cr
>= 0)
3541 c1
= byte_after_cr
, byte_after_cr
= -1;
3547 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3549 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3551 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3555 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3557 if (c1
== ISO_CODE_ESC
)
3559 if (src
+ 1 >= src_end
)
3560 goto no_more_source
;
3561 *charbuf
++ = ISO_CODE_ESC
;
3563 if (src
[0] == '%' && src
[1] == '@')
3566 consumed_chars
+= 2;
3568 /* We are sure charbuf can contain two more chars. */
3571 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3576 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3582 if ((cmp_status
->state
== COMPOSING_RULE
3583 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3584 && c1
!= ISO_CODE_ESC
)
3588 DECODE_COMPOSITION_RULE (rule
, nbytes
);
3591 STORE_COMPOSITION_RULE (rule
);
3595 /* We produce at most one character. */
3596 switch (iso_code_class
[c1
])
3598 case ISO_0x20_or_0x7F
:
3599 if (charset_id_0
< 0
3600 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3601 /* This is SPACE or DEL. */
3602 charset
= CHARSET_FROM_ID (charset_ascii
);
3604 charset
= CHARSET_FROM_ID (charset_id_0
);
3607 case ISO_graphic_plane_0
:
3608 if (charset_id_0
< 0)
3609 charset
= CHARSET_FROM_ID (charset_ascii
);
3611 charset
= CHARSET_FROM_ID (charset_id_0
);
3614 case ISO_0xA0_or_0xFF
:
3615 if (charset_id_1
< 0
3616 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3617 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3619 /* This is a graphic character, we fall down ... */
3621 case ISO_graphic_plane_1
:
3622 if (charset_id_1
< 0)
3624 charset
= CHARSET_FROM_ID (charset_id_1
);
3628 if (eol_crlf
&& c1
== '\r')
3629 ONE_MORE_BYTE (byte_after_cr
);
3630 MAYBE_FINISH_COMPOSITION ();
3631 charset
= CHARSET_FROM_ID (charset_ascii
);
3638 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3639 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3641 CODING_ISO_INVOCATION (coding
, 0) = 1;
3642 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3646 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3648 CODING_ISO_INVOCATION (coding
, 0) = 0;
3649 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3652 case ISO_single_shift_2_7
:
3653 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3655 case ISO_single_shift_2
:
3656 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3658 /* SS2 is handled as an escape sequence of ESC 'N' */
3660 goto label_escape_sequence
;
3662 case ISO_single_shift_3
:
3663 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3665 /* SS2 is handled as an escape sequence of ESC 'O' */
3667 goto label_escape_sequence
;
3669 case ISO_control_sequence_introducer
:
3670 /* CSI is handled as an escape sequence of ESC '[' ... */
3672 goto label_escape_sequence
;
3676 label_escape_sequence
:
3677 /* Escape sequences handled here are invocation,
3678 designation, direction specification, and character
3679 composition specification. */
3682 case '&': /* revision of following character set */
3684 if (!(c1
>= '@' && c1
<= '~'))
3687 if (c1
!= ISO_CODE_ESC
)
3690 goto label_escape_sequence
;
3692 case '$': /* designation of 2-byte character set */
3693 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3699 if (c1
>= '@' && c1
<= 'B')
3700 { /* designation of JISX0208.1978, GB2312.1980,
3702 reg
= 0, chars96
= 0;
3704 else if (c1
>= 0x28 && c1
<= 0x2B)
3705 { /* designation of DIMENSION2_CHARS94 character set */
3706 reg
= c1
- 0x28, chars96
= 0;
3709 else if (c1
>= 0x2C && c1
<= 0x2F)
3710 { /* designation of DIMENSION2_CHARS96 character set */
3711 reg
= c1
- 0x2C, chars96
= 1;
3716 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3717 /* We must update these variables now. */
3719 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3721 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3727 case 'n': /* invocation of locking-shift-2 */
3728 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3729 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3731 CODING_ISO_INVOCATION (coding
, 0) = 2;
3732 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3735 case 'o': /* invocation of locking-shift-3 */
3736 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3737 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3739 CODING_ISO_INVOCATION (coding
, 0) = 3;
3740 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3743 case 'N': /* invocation of single-shift-2 */
3744 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3745 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3747 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3748 if (charset_id_2
< 0)
3749 charset
= CHARSET_FROM_ID (charset_ascii
);
3751 charset
= CHARSET_FROM_ID (charset_id_2
);
3753 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3757 case 'O': /* invocation of single-shift-3 */
3758 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3759 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3761 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3762 if (charset_id_3
< 0)
3763 charset
= CHARSET_FROM_ID (charset_ascii
);
3765 charset
= CHARSET_FROM_ID (charset_id_3
);
3767 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3771 case '0': case '2': case '3': case '4': /* start composition */
3772 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3774 if (last_id
!= charset_ascii
)
3776 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3777 last_id
= charset_ascii
;
3778 last_offset
= char_offset
;
3780 DECODE_COMPOSITION_START (c1
);
3783 case '1': /* end composition */
3784 if (cmp_status
->state
== COMPOSING_NO
)
3786 DECODE_COMPOSITION_END ();
3789 case '[': /* specification of direction */
3790 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3792 /* For the moment, nested direction is not supported.
3793 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3794 left-to-right, and nonzero means right-to-left. */
3798 case ']': /* end of the current direction */
3799 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3801 case '0': /* end of the current direction */
3802 case '1': /* start of left-to-right direction */
3805 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3810 case '2': /* start of right-to-left direction */
3813 coding
->mode
|= CODING_MODE_DIRECTION
;
3827 /* CTEXT extended segment:
3828 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3829 We keep these bytes as is for the moment.
3830 They may be decoded by post-read-conversion. */
3834 ONE_MORE_BYTE (dim
);
3835 if (dim
< '0' || dim
> '4')
3843 size
= ((M
- 128) * 128) + (L
- 128);
3844 if (charbuf
+ 6 > charbuf_end
)
3846 *charbuf
++ = ISO_CODE_ESC
;
3850 *charbuf
++ = BYTE8_TO_CHAR (M
);
3851 *charbuf
++ = BYTE8_TO_CHAR (L
);
3852 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3856 /* XFree86 extension for embedding UTF-8 in CTEXT:
3857 ESC % G --UTF-8-BYTES-- ESC % @
3858 We keep these bytes as is for the moment.
3859 They may be decoded by post-read-conversion. */
3860 if (charbuf
+ 3 > charbuf_end
)
3862 *charbuf
++ = ISO_CODE_ESC
;
3865 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3873 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3878 if (c1
>= 0x28 && c1
<= 0x2B)
3879 { /* designation of DIMENSION1_CHARS94 character set */
3880 reg
= c1
- 0x28, chars96
= 0;
3883 else if (c1
>= 0x2C && c1
<= 0x2F)
3884 { /* designation of DIMENSION1_CHARS96 character set */
3885 reg
= c1
- 0x2C, chars96
= 1;
3890 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3891 /* We must update these variables now. */
3893 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3895 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3903 if (cmp_status
->state
== COMPOSING_NO
3904 && charset
->id
!= charset_ascii
3905 && last_id
!= charset
->id
)
3907 if (last_id
!= charset_ascii
)
3908 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3909 last_id
= charset
->id
;
3910 last_offset
= char_offset
;
3913 /* Now we know CHARSET and 1st position code C1 of a character.
3914 Produce a decoded character while getting 2nd and 3rd
3915 position codes C2, C3 if necessary. */
3916 if (CHARSET_DIMENSION (charset
) > 1)
3919 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3920 || ((c1
& 0x80) != (c2
& 0x80)))
3921 /* C2 is not in a valid range. */
3923 if (CHARSET_DIMENSION (charset
) == 2)
3924 c1
= (c1
<< 8) | c2
;
3928 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3929 || ((c1
& 0x80) != (c3
& 0x80)))
3930 /* C3 is not in a valid range. */
3932 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3936 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3939 MAYBE_FINISH_COMPOSITION ();
3940 for (; src_base
< src
; src_base
++, char_offset
++)
3942 if (ASCII_BYTE_P (*src_base
))
3943 *charbuf
++ = *src_base
;
3945 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3948 else if (cmp_status
->state
== COMPOSING_NO
)
3953 else if ((cmp_status
->state
== COMPOSING_CHAR
3954 ? cmp_status
->nchars
3955 : cmp_status
->ncomps
)
3956 >= MAX_COMPOSITION_COMPONENTS
)
3958 /* Too long composition. */
3959 MAYBE_FINISH_COMPOSITION ();
3964 STORE_COMPOSITION_CHAR (c
);
3968 MAYBE_FINISH_COMPOSITION ();
3970 consumed_chars
= consumed_chars_base
;
3972 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3982 if (cmp_status
->state
!= COMPOSING_NO
)
3984 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3985 MAYBE_FINISH_COMPOSITION ();
3988 charbuf
-= cmp_status
->length
;
3989 for (i
= 0; i
< cmp_status
->length
; i
++)
3990 cmp_status
->carryover
[i
] = charbuf
[i
];
3993 else if (last_id
!= charset_ascii
)
3994 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3995 coding
->consumed_char
+= consumed_chars_base
;
3996 coding
->consumed
= src_base
- coding
->source
;
3997 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4001 /* ISO2022 encoding stuff. */
4004 It is not enough to say just "ISO2022" on encoding, we have to
4005 specify more details. In Emacs, each coding system of ISO2022
4006 variant has the following specifications:
4007 1. Initial designation to G0 thru G3.
4008 2. Allows short-form designation?
4009 3. ASCII should be designated to G0 before control characters?
4010 4. ASCII should be designated to G0 at end of line?
4011 5. 7-bit environment or 8-bit environment?
4012 6. Use locking-shift?
4013 7. Use Single-shift?
4014 And the following two are only for Japanese:
4015 8. Use ASCII in place of JIS0201-1976-Roman?
4016 9. Use JISX0208-1983 in place of JISX0208-1978?
4017 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4018 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4022 /* Produce codes (escape sequence) for designating CHARSET to graphic
4023 register REG at DST, and increment DST. If <final-char> of CHARSET is
4024 '@', 'A', or 'B' and the coding system CODING allows, produce
4025 designation sequence of short-form. */
4027 #define ENCODE_DESIGNATION(charset, reg, coding) \
4029 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4030 const char *intermediate_char_94 = "()*+"; \
4031 const char *intermediate_char_96 = ",-./"; \
4032 int revision = -1; \
4035 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4036 revision = CHARSET_ISO_REVISION (charset); \
4038 if (revision >= 0) \
4040 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4041 EMIT_ONE_BYTE ('@' + revision); \
4043 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4044 if (CHARSET_DIMENSION (charset) == 1) \
4046 if (! CHARSET_ISO_CHARS_96 (charset)) \
4047 c = intermediate_char_94[reg]; \
4049 c = intermediate_char_96[reg]; \
4050 EMIT_ONE_ASCII_BYTE (c); \
4054 EMIT_ONE_ASCII_BYTE ('$'); \
4055 if (! CHARSET_ISO_CHARS_96 (charset)) \
4057 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4059 || final_char < '@' || final_char > 'B') \
4060 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4063 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4065 EMIT_ONE_ASCII_BYTE (final_char); \
4067 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4071 /* The following two macros produce codes (control character or escape
4072 sequence) for ISO2022 single-shift functions (single-shift-2 and
4075 #define ENCODE_SINGLE_SHIFT_2 \
4077 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4078 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4080 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4081 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4085 #define ENCODE_SINGLE_SHIFT_3 \
4087 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4088 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4090 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4091 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4095 /* The following four macros produce codes (control character or
4096 escape sequence) for ISO2022 locking-shift functions (shift-in,
4097 shift-out, locking-shift-2, and locking-shift-3). */
4099 #define ENCODE_SHIFT_IN \
4101 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4102 CODING_ISO_INVOCATION (coding, 0) = 0; \
4106 #define ENCODE_SHIFT_OUT \
4108 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4109 CODING_ISO_INVOCATION (coding, 0) = 1; \
4113 #define ENCODE_LOCKING_SHIFT_2 \
4115 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4116 CODING_ISO_INVOCATION (coding, 0) = 2; \
4120 #define ENCODE_LOCKING_SHIFT_3 \
4122 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4123 CODING_ISO_INVOCATION (coding, 0) = 3; \
4127 /* Produce codes for a DIMENSION1 character whose character set is
4128 CHARSET and whose position-code is C1. Designation and invocation
4129 sequences are also produced in advance if necessary. */
4131 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4133 int id = CHARSET_ID (charset); \
4135 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4136 && id == charset_ascii) \
4138 id = charset_jisx0201_roman; \
4139 charset = CHARSET_FROM_ID (id); \
4142 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4144 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4145 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4147 EMIT_ONE_BYTE (c1 | 0x80); \
4148 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4151 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4153 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4156 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4158 EMIT_ONE_BYTE (c1 | 0x80); \
4162 /* Since CHARSET is not yet invoked to any graphic planes, we \
4163 must invoke it, or, at first, designate it to some graphic \
4164 register. Then repeat the loop to actually produce the \
4166 dst = encode_invocation_designation (charset, coding, dst, \
4171 /* Produce codes for a DIMENSION2 character whose character set is
4172 CHARSET and whose position-codes are C1 and C2. Designation and
4173 invocation codes are also produced in advance if necessary. */
4175 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4177 int id = CHARSET_ID (charset); \
4179 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4180 && id == charset_jisx0208) \
4182 id = charset_jisx0208_1978; \
4183 charset = CHARSET_FROM_ID (id); \
4186 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4188 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4189 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4191 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4192 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4195 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4197 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4200 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4202 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4206 /* Since CHARSET is not yet invoked to any graphic planes, we \
4207 must invoke it, or, at first, designate it to some graphic \
4208 register. Then repeat the loop to actually produce the \
4210 dst = encode_invocation_designation (charset, coding, dst, \
4215 #define ENCODE_ISO_CHARACTER(charset, c) \
4217 int code = ENCODE_CHAR ((charset), (c)); \
4219 if (CHARSET_DIMENSION (charset) == 1) \
4220 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4222 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4226 /* Produce designation and invocation codes at a place pointed by DST
4227 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4231 encode_invocation_designation (struct charset
*charset
,
4232 struct coding_system
*coding
,
4233 unsigned char *dst
, int *p_nchars
)
4235 int multibytep
= coding
->dst_multibyte
;
4236 int produced_chars
= *p_nchars
;
4237 int reg
; /* graphic register number */
4238 int id
= CHARSET_ID (charset
);
4240 /* At first, check designations. */
4241 for (reg
= 0; reg
< 4; reg
++)
4242 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4247 /* CHARSET is not yet designated to any graphic registers. */
4248 /* At first check the requested designation. */
4249 reg
= CODING_ISO_REQUEST (coding
, id
);
4251 /* Since CHARSET requests no special designation, designate it
4252 to graphic register 0. */
4255 ENCODE_DESIGNATION (charset
, reg
, coding
);
4258 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4259 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4261 /* Since the graphic register REG is not invoked to any graphic
4262 planes, invoke it to graphic plane 0. */
4265 case 0: /* graphic register 0 */
4269 case 1: /* graphic register 1 */
4273 case 2: /* graphic register 2 */
4274 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4275 ENCODE_SINGLE_SHIFT_2
;
4277 ENCODE_LOCKING_SHIFT_2
;
4280 case 3: /* graphic register 3 */
4281 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4282 ENCODE_SINGLE_SHIFT_3
;
4284 ENCODE_LOCKING_SHIFT_3
;
4289 *p_nchars
= produced_chars
;
4293 /* The following three macros produce codes for indicating direction
4295 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
4297 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
4298 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
4300 EMIT_ONE_BYTE (ISO_CODE_CSI); \
4304 #define ENCODE_DIRECTION_R2L() \
4306 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4307 EMIT_TWO_ASCII_BYTES ('2', ']'); \
4311 #define ENCODE_DIRECTION_L2R() \
4313 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4314 EMIT_TWO_ASCII_BYTES ('0', ']'); \
4318 /* Produce codes for designation and invocation to reset the graphic
4319 planes and registers to initial state. */
4320 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4323 struct charset *charset; \
4325 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4327 for (reg = 0; reg < 4; reg++) \
4328 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4329 && (CODING_ISO_DESIGNATION (coding, reg) \
4330 != CODING_ISO_INITIAL (coding, reg))) \
4332 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4333 ENCODE_DESIGNATION (charset, reg, coding); \
4338 /* Produce designation sequences of charsets in the line started from
4339 SRC to a place pointed by DST, and return updated DST.
4341 If the current block ends before any end-of-line, we may fail to
4342 find all the necessary designations. */
4344 static unsigned char *
4345 encode_designation_at_bol (struct coding_system
*coding
, int *charbuf
,
4346 int *charbuf_end
, unsigned char *dst
)
4348 struct charset
*charset
;
4349 /* Table of charsets to be designated to each graphic register. */
4351 int c
, found
= 0, reg
;
4352 int produced_chars
= 0;
4353 int multibytep
= coding
->dst_multibyte
;
4355 Lisp_Object charset_list
;
4357 attrs
= CODING_ID_ATTRS (coding
->id
);
4358 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4359 if (EQ (charset_list
, Qiso_2022
))
4360 charset_list
= Viso_2022_charset_list
;
4362 for (reg
= 0; reg
< 4; reg
++)
4372 charset
= char_charset (c
, charset_list
, NULL
);
4373 id
= CHARSET_ID (charset
);
4374 reg
= CODING_ISO_REQUEST (coding
, id
);
4375 if (reg
>= 0 && r
[reg
] < 0)
4384 for (reg
= 0; reg
< 4; reg
++)
4386 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4387 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4393 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4396 encode_coding_iso_2022 (struct coding_system
*coding
)
4398 int multibytep
= coding
->dst_multibyte
;
4399 int *charbuf
= coding
->charbuf
;
4400 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4401 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4402 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4405 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4406 && CODING_ISO_BOL (coding
));
4407 int produced_chars
= 0;
4408 Lisp_Object attrs
, eol_type
, charset_list
;
4409 int ascii_compatible
;
4411 int preferred_charset_id
= -1;
4413 CODING_GET_INFO (coding
, attrs
, charset_list
);
4414 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4415 if (VECTORP (eol_type
))
4418 setup_iso_safe_charsets (attrs
);
4419 /* Charset list may have been changed. */
4420 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4421 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4424 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4425 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4426 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4428 while (charbuf
< charbuf_end
)
4430 ASSURE_DESTINATION (safe_room
);
4432 if (bol_designation
)
4434 unsigned char *dst_prev
= dst
;
4436 /* We have to produce designation sequences if any now. */
4437 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
4438 bol_designation
= 0;
4439 /* We are sure that designation sequences are all ASCII bytes. */
4440 produced_chars
+= dst
- dst_prev
;
4447 /* Handle an annotation. */
4450 case CODING_ANNOTATE_COMPOSITION_MASK
:
4451 /* Not yet implemented. */
4453 case CODING_ANNOTATE_CHARSET_MASK
:
4454 preferred_charset_id
= charbuf
[2];
4455 if (preferred_charset_id
>= 0
4456 && NILP (Fmemq (make_number (preferred_charset_id
),
4458 preferred_charset_id
= -1;
4467 /* Now encode the character C. */
4468 if (c
< 0x20 || c
== 0x7F)
4471 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4473 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4474 ENCODE_RESET_PLANE_AND_REGISTER ();
4475 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4479 for (i
= 0; i
< 4; i
++)
4480 CODING_ISO_DESIGNATION (coding
, i
)
4481 = CODING_ISO_INITIAL (coding
, i
);
4484 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4486 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4487 ENCODE_RESET_PLANE_AND_REGISTER ();
4488 EMIT_ONE_ASCII_BYTE (c
);
4490 else if (ASCII_CHAR_P (c
))
4492 if (ascii_compatible
)
4493 EMIT_ONE_ASCII_BYTE (c
);
4496 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4497 ENCODE_ISO_CHARACTER (charset
, c
);
4500 else if (CHAR_BYTE8_P (c
))
4502 c
= CHAR_TO_BYTE8 (c
);
4507 struct charset
*charset
;
4509 if (preferred_charset_id
>= 0)
4511 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4512 if (! CHAR_CHARSET_P (c
, charset
))
4513 charset
= char_charset (c
, charset_list
, NULL
);
4516 charset
= char_charset (c
, charset_list
, NULL
);
4519 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4521 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4522 charset
= CHARSET_FROM_ID (charset_ascii
);
4526 c
= coding
->default_char
;
4527 charset
= char_charset (c
, charset_list
, NULL
);
4530 ENCODE_ISO_CHARACTER (charset
, c
);
4534 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4535 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4537 ASSURE_DESTINATION (safe_room
);
4538 ENCODE_RESET_PLANE_AND_REGISTER ();
4540 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4541 CODING_ISO_BOL (coding
) = bol_designation
;
4542 coding
->produced_char
+= produced_chars
;
4543 coding
->produced
= dst
- coding
->destination
;
4548 /*** 8,9. SJIS and BIG5 handlers ***/
4550 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4551 quite widely. So, for the moment, Emacs supports them in the bare
4552 C code. But, in the future, they may be supported only by CCL. */
4554 /* SJIS is a coding system encoding three character sets: ASCII, right
4555 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4556 as is. A character of charset katakana-jisx0201 is encoded by
4557 "position-code + 0x80". A character of charset japanese-jisx0208
4558 is encoded in 2-byte but two position-codes are divided and shifted
4559 so that it fit in the range below.
4561 --- CODE RANGE of SJIS ---
4562 (character set) (range)
4564 KATAKANA-JISX0201 0xA0 .. 0xDF
4565 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4566 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4567 -------------------------------
4571 /* BIG5 is a coding system encoding two character sets: ASCII and
4572 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4573 character set and is encoded in two-byte.
4575 --- CODE RANGE of BIG5 ---
4576 (character set) (range)
4578 Big5 (1st byte) 0xA1 .. 0xFE
4579 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4580 --------------------------
4584 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4585 Check if a text is encoded in SJIS. If it is, return
4586 CATEGORY_MASK_SJIS, else return 0. */
4589 detect_coding_sjis (struct coding_system
*coding
,
4590 struct coding_detection_info
*detect_info
)
4592 const unsigned char *src
= coding
->source
, *src_base
;
4593 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4594 int multibytep
= coding
->src_multibyte
;
4595 int consumed_chars
= 0;
4598 Lisp_Object attrs
, charset_list
;
4599 int max_first_byte_of_2_byte_code
;
4601 CODING_GET_INFO (coding
, attrs
, charset_list
);
4602 max_first_byte_of_2_byte_code
4603 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4605 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4606 /* A coding system of this category is always ASCII compatible. */
4607 src
+= coding
->head_ascii
;
4615 if ((c
>= 0x81 && c
<= 0x9F)
4616 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4619 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4621 found
= CATEGORY_MASK_SJIS
;
4623 else if (c
>= 0xA0 && c
< 0xE0)
4624 found
= CATEGORY_MASK_SJIS
;
4628 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4632 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4634 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4637 detect_info
->found
|= found
;
4641 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4642 Check if a text is encoded in BIG5. If it is, return
4643 CATEGORY_MASK_BIG5, else return 0. */
4646 detect_coding_big5 (struct coding_system
*coding
,
4647 struct coding_detection_info
*detect_info
)
4649 const unsigned char *src
= coding
->source
, *src_base
;
4650 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4651 int multibytep
= coding
->src_multibyte
;
4652 int consumed_chars
= 0;
4656 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4657 /* A coding system of this category is always ASCII compatible. */
4658 src
+= coding
->head_ascii
;
4669 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4671 found
= CATEGORY_MASK_BIG5
;
4676 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4680 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4682 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4685 detect_info
->found
|= found
;
4689 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4690 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4693 decode_coding_sjis (struct coding_system
*coding
)
4695 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4696 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4697 const unsigned char *src_base
;
4698 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4699 /* We may produce one charset annotation in one loop and one more at
4702 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4703 int consumed_chars
= 0, consumed_chars_base
;
4704 int multibytep
= coding
->src_multibyte
;
4705 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4706 struct charset
*charset_kanji2
;
4707 Lisp_Object attrs
, charset_list
, val
;
4708 int char_offset
= coding
->produced_char
;
4709 int last_offset
= char_offset
;
4710 int last_id
= charset_ascii
;
4712 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4713 int byte_after_cr
= -1;
4715 CODING_GET_INFO (coding
, attrs
, charset_list
);
4718 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4719 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4720 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4721 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4726 struct charset
*charset
;
4729 consumed_chars_base
= consumed_chars
;
4731 if (charbuf
>= charbuf_end
)
4733 if (byte_after_cr
>= 0)
4738 if (byte_after_cr
>= 0)
4739 c
= byte_after_cr
, byte_after_cr
= -1;
4746 if (eol_crlf
&& c
== '\r')
4747 ONE_MORE_BYTE (byte_after_cr
);
4748 charset
= charset_roman
;
4750 else if (c
== 0x80 || c
== 0xA0)
4752 else if (c
>= 0xA1 && c
<= 0xDF)
4754 /* SJIS -> JISX0201-Kana */
4756 charset
= charset_kana
;
4760 /* SJIS -> JISX0208 */
4762 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4766 charset
= charset_kanji
;
4768 else if (c
<= 0xFC && charset_kanji2
)
4770 /* SJIS -> JISX0213-2 */
4772 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4776 charset
= charset_kanji2
;
4780 if (charset
->id
!= charset_ascii
4781 && last_id
!= charset
->id
)
4783 if (last_id
!= charset_ascii
)
4784 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4785 last_id
= charset
->id
;
4786 last_offset
= char_offset
;
4788 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4795 consumed_chars
= consumed_chars_base
;
4797 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4803 if (last_id
!= charset_ascii
)
4804 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4805 coding
->consumed_char
+= consumed_chars_base
;
4806 coding
->consumed
= src_base
- coding
->source
;
4807 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4811 decode_coding_big5 (struct coding_system
*coding
)
4813 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4814 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4815 const unsigned char *src_base
;
4816 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4817 /* We may produce one charset annotation in one loop and one more at
4820 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4821 int consumed_chars
= 0, consumed_chars_base
;
4822 int multibytep
= coding
->src_multibyte
;
4823 struct charset
*charset_roman
, *charset_big5
;
4824 Lisp_Object attrs
, charset_list
, val
;
4825 int char_offset
= coding
->produced_char
;
4826 int last_offset
= char_offset
;
4827 int last_id
= charset_ascii
;
4829 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4830 int byte_after_cr
= -1;
4832 CODING_GET_INFO (coding
, attrs
, charset_list
);
4834 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4835 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4840 struct charset
*charset
;
4843 consumed_chars_base
= consumed_chars
;
4845 if (charbuf
>= charbuf_end
)
4847 if (byte_after_cr
>= 0)
4852 if (byte_after_cr
>= 0)
4853 c
= byte_after_cr
, byte_after_cr
= -1;
4861 if (eol_crlf
&& c
== '\r')
4862 ONE_MORE_BYTE (byte_after_cr
);
4863 charset
= charset_roman
;
4868 if (c
< 0xA1 || c
> 0xFE)
4871 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4874 charset
= charset_big5
;
4876 if (charset
->id
!= charset_ascii
4877 && last_id
!= charset
->id
)
4879 if (last_id
!= charset_ascii
)
4880 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4881 last_id
= charset
->id
;
4882 last_offset
= char_offset
;
4884 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4891 consumed_chars
= consumed_chars_base
;
4893 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4899 if (last_id
!= charset_ascii
)
4900 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4901 coding
->consumed_char
+= consumed_chars_base
;
4902 coding
->consumed
= src_base
- coding
->source
;
4903 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4906 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4907 This function can encode charsets `ascii', `katakana-jisx0201',
4908 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4909 are sure that all these charsets are registered as official charset
4910 (i.e. do not have extended leading-codes). Characters of other
4911 charsets are produced without any encoding. If SJIS_P is 1, encode
4912 SJIS text, else encode BIG5 text. */
4915 encode_coding_sjis (struct coding_system
*coding
)
4917 int multibytep
= coding
->dst_multibyte
;
4918 int *charbuf
= coding
->charbuf
;
4919 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4920 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4921 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4923 int produced_chars
= 0;
4924 Lisp_Object attrs
, charset_list
, val
;
4925 int ascii_compatible
;
4926 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4927 struct charset
*charset_kanji2
;
4930 CODING_GET_INFO (coding
, attrs
, charset_list
);
4932 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4933 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4934 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4935 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4937 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4939 while (charbuf
< charbuf_end
)
4941 ASSURE_DESTINATION (safe_room
);
4943 /* Now encode the character C. */
4944 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4945 EMIT_ONE_ASCII_BYTE (c
);
4946 else if (CHAR_BYTE8_P (c
))
4948 c
= CHAR_TO_BYTE8 (c
);
4954 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4958 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4960 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4961 charset
= CHARSET_FROM_ID (charset_ascii
);
4965 c
= coding
->default_char
;
4966 charset
= char_charset (c
, charset_list
, &code
);
4969 if (code
== CHARSET_INVALID_CODE (charset
))
4971 if (charset
== charset_kanji
)
4975 c1
= code
>> 8, c2
= code
& 0xFF;
4976 EMIT_TWO_BYTES (c1
, c2
);
4978 else if (charset
== charset_kana
)
4979 EMIT_ONE_BYTE (code
| 0x80);
4980 else if (charset_kanji2
&& charset
== charset_kanji2
)
4985 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4987 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4989 JIS_TO_SJIS2 (code
);
4990 c1
= code
>> 8, c2
= code
& 0xFF;
4991 EMIT_TWO_BYTES (c1
, c2
);
4994 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4997 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5000 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5001 coding
->produced_char
+= produced_chars
;
5002 coding
->produced
= dst
- coding
->destination
;
5007 encode_coding_big5 (struct coding_system
*coding
)
5009 int multibytep
= coding
->dst_multibyte
;
5010 int *charbuf
= coding
->charbuf
;
5011 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5012 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5013 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5015 int produced_chars
= 0;
5016 Lisp_Object attrs
, charset_list
, val
;
5017 int ascii_compatible
;
5018 struct charset
*charset_roman
, *charset_big5
;
5021 CODING_GET_INFO (coding
, attrs
, charset_list
);
5023 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5024 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5025 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5027 while (charbuf
< charbuf_end
)
5029 ASSURE_DESTINATION (safe_room
);
5031 /* Now encode the character C. */
5032 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5033 EMIT_ONE_ASCII_BYTE (c
);
5034 else if (CHAR_BYTE8_P (c
))
5036 c
= CHAR_TO_BYTE8 (c
);
5042 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
5046 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5048 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5049 charset
= CHARSET_FROM_ID (charset_ascii
);
5053 c
= coding
->default_char
;
5054 charset
= char_charset (c
, charset_list
, &code
);
5057 if (code
== CHARSET_INVALID_CODE (charset
))
5059 if (charset
== charset_big5
)
5063 c1
= code
>> 8, c2
= code
& 0xFF;
5064 EMIT_TWO_BYTES (c1
, c2
);
5067 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5070 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5071 coding
->produced_char
+= produced_chars
;
5072 coding
->produced
= dst
- coding
->destination
;
5077 /*** 10. CCL handlers ***/
5079 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5080 Check if a text is encoded in a coding system of which
5081 encoder/decoder are written in CCL program. If it is, return
5082 CATEGORY_MASK_CCL, else return 0. */
5085 detect_coding_ccl (struct coding_system
*coding
,
5086 struct coding_detection_info
*detect_info
)
5088 const unsigned char *src
= coding
->source
, *src_base
;
5089 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5090 int multibytep
= coding
->src_multibyte
;
5091 int consumed_chars
= 0;
5093 unsigned char *valids
;
5094 int head_ascii
= coding
->head_ascii
;
5097 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5099 coding
= &coding_categories
[coding_category_ccl
];
5100 valids
= CODING_CCL_VALIDS (coding
);
5101 attrs
= CODING_ID_ATTRS (coding
->id
);
5102 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5111 if (c
< 0 || ! valids
[c
])
5113 if ((valids
[c
] > 1))
5114 found
= CATEGORY_MASK_CCL
;
5116 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5120 detect_info
->found
|= found
;
5125 decode_coding_ccl (struct coding_system
*coding
)
5127 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5128 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5129 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5130 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5131 int consumed_chars
= 0;
5132 int multibytep
= coding
->src_multibyte
;
5133 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5134 int source_charbuf
[1024];
5135 int source_byteidx
[1025];
5136 Lisp_Object attrs
, charset_list
;
5138 CODING_GET_INFO (coding
, attrs
, charset_list
);
5142 const unsigned char *p
= src
;
5147 while (i
< 1024 && p
< src_end
)
5149 source_byteidx
[i
] = p
- src
;
5150 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5152 source_byteidx
[i
] = p
- src
;
5155 while (i
< 1024 && p
< src_end
)
5156 source_charbuf
[i
++] = *p
++;
5158 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5159 ccl
->last_block
= 1;
5160 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5162 charbuf
+= ccl
->produced
;
5164 src
+= source_byteidx
[ccl
->consumed
];
5166 src
+= ccl
->consumed
;
5167 consumed_chars
+= ccl
->consumed
;
5168 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5172 switch (ccl
->status
)
5174 case CCL_STAT_SUSPEND_BY_SRC
:
5175 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5177 case CCL_STAT_SUSPEND_BY_DST
:
5178 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5181 case CCL_STAT_INVALID_CMD
:
5182 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5185 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5188 coding
->consumed_char
+= consumed_chars
;
5189 coding
->consumed
= src
- coding
->source
;
5190 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5194 encode_coding_ccl (struct coding_system
*coding
)
5196 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5197 int multibytep
= coding
->dst_multibyte
;
5198 int *charbuf
= coding
->charbuf
;
5199 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5200 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5201 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5202 int destination_charbuf
[1024];
5203 int i
, produced_chars
= 0;
5204 Lisp_Object attrs
, charset_list
;
5206 CODING_GET_INFO (coding
, attrs
, charset_list
);
5207 if (coding
->consumed_char
== coding
->src_chars
5208 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5209 ccl
->last_block
= 1;
5211 while (charbuf
< charbuf_end
)
5213 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5214 charbuf_end
- charbuf
, 1024, charset_list
);
5217 ASSURE_DESTINATION (ccl
->produced
* 2);
5218 for (i
= 0; i
< ccl
->produced
; i
++)
5219 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5223 ASSURE_DESTINATION (ccl
->produced
);
5224 for (i
= 0; i
< ccl
->produced
; i
++)
5225 *dst
++ = destination_charbuf
[i
] & 0xFF;
5226 produced_chars
+= ccl
->produced
;
5228 charbuf
+= ccl
->consumed
;
5229 if (ccl
->status
== CCL_STAT_QUIT
5230 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5234 switch (ccl
->status
)
5236 case CCL_STAT_SUSPEND_BY_SRC
:
5237 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5239 case CCL_STAT_SUSPEND_BY_DST
:
5240 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5243 case CCL_STAT_INVALID_CMD
:
5244 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5247 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5251 coding
->produced_char
+= produced_chars
;
5252 coding
->produced
= dst
- coding
->destination
;
5258 /*** 10, 11. no-conversion handlers ***/
5260 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5263 decode_coding_raw_text (struct coding_system
*coding
)
5266 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5268 coding
->chars_at_source
= 1;
5269 coding
->consumed_char
= coding
->src_chars
;
5270 coding
->consumed
= coding
->src_bytes
;
5271 if (eol_crlf
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5273 coding
->consumed_char
--;
5275 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5278 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5282 encode_coding_raw_text (struct coding_system
*coding
)
5284 int multibytep
= coding
->dst_multibyte
;
5285 int *charbuf
= coding
->charbuf
;
5286 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5287 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5288 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5289 int produced_chars
= 0;
5294 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5296 if (coding
->src_multibyte
)
5297 while (charbuf
< charbuf_end
)
5299 ASSURE_DESTINATION (safe_room
);
5301 if (ASCII_CHAR_P (c
))
5302 EMIT_ONE_ASCII_BYTE (c
);
5303 else if (CHAR_BYTE8_P (c
))
5305 c
= CHAR_TO_BYTE8 (c
);
5310 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5312 CHAR_STRING_ADVANCE (c
, p1
);
5315 EMIT_ONE_BYTE (*p0
);
5321 while (charbuf
< charbuf_end
)
5323 ASSURE_DESTINATION (safe_room
);
5330 if (coding
->src_multibyte
)
5332 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5334 while (charbuf
< charbuf_end
)
5336 ASSURE_DESTINATION (safe_room
);
5338 if (ASCII_CHAR_P (c
))
5340 else if (CHAR_BYTE8_P (c
))
5341 *dst
++ = CHAR_TO_BYTE8 (c
);
5343 CHAR_STRING_ADVANCE (c
, dst
);
5348 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5349 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5350 *dst
++ = *charbuf
++;
5352 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5354 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5355 coding
->produced_char
+= produced_chars
;
5356 coding
->produced
= dst
- coding
->destination
;
5360 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5361 Check if a text is encoded in a charset-based coding system. If it
5362 is, return 1, else return 0. */
5365 detect_coding_charset (struct coding_system
*coding
,
5366 struct coding_detection_info
*detect_info
)
5368 const unsigned char *src
= coding
->source
, *src_base
;
5369 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5370 int multibytep
= coding
->src_multibyte
;
5371 int consumed_chars
= 0;
5372 Lisp_Object attrs
, valids
, name
;
5374 int head_ascii
= coding
->head_ascii
;
5375 int check_latin_extra
= 0;
5377 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5379 coding
= &coding_categories
[coding_category_charset
];
5380 attrs
= CODING_ID_ATTRS (coding
->id
);
5381 valids
= AREF (attrs
, coding_attr_charset_valids
);
5382 name
= CODING_ID_NAME (coding
->id
);
5383 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5384 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5385 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5386 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5387 check_latin_extra
= 1;
5389 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5396 struct charset
*charset
;
5403 val
= AREF (valids
, c
);
5409 && check_latin_extra
5410 && (!VECTORP (Vlatin_extra_code_table
)
5411 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5413 found
= CATEGORY_MASK_CHARSET
;
5417 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5418 dim
= CHARSET_DIMENSION (charset
);
5419 for (idx
= 1; idx
< dim
; idx
++)
5424 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5425 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5434 for (; CONSP (val
); val
= XCDR (val
))
5436 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5437 dim
= CHARSET_DIMENSION (charset
);
5443 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5444 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5459 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5463 detect_info
->found
|= found
;
5468 decode_coding_charset (struct coding_system
*coding
)
5470 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5471 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5472 const unsigned char *src_base
;
5473 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5474 /* We may produce one charset annotation in one loop and one more at
5477 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5478 int consumed_chars
= 0, consumed_chars_base
;
5479 int multibytep
= coding
->src_multibyte
;
5480 Lisp_Object attrs
, charset_list
, valids
;
5481 int char_offset
= coding
->produced_char
;
5482 int last_offset
= char_offset
;
5483 int last_id
= charset_ascii
;
5485 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5486 int byte_after_cr
= -1;
5488 CODING_GET_INFO (coding
, attrs
, charset_list
);
5489 valids
= AREF (attrs
, coding_attr_charset_valids
);
5495 struct charset
*charset
;
5501 consumed_chars_base
= consumed_chars
;
5503 if (charbuf
>= charbuf_end
)
5505 if (byte_after_cr
>= 0)
5510 if (byte_after_cr
>= 0)
5518 if (eol_crlf
&& c
== '\r')
5519 ONE_MORE_BYTE (byte_after_cr
);
5525 val
= AREF (valids
, c
);
5526 if (! INTEGERP (val
) && ! CONSP (val
))
5530 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5531 dim
= CHARSET_DIMENSION (charset
);
5535 code
= (code
<< 8) | c
;
5538 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5543 /* VAL is a list of charset IDs. It is assured that the
5544 list is sorted by charset dimensions (smaller one
5548 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5549 dim
= CHARSET_DIMENSION (charset
);
5553 code
= (code
<< 8) | c
;
5556 CODING_DECODE_CHAR (coding
, src
, src_base
,
5557 src_end
, charset
, code
, c
);
5565 if (charset
->id
!= charset_ascii
5566 && last_id
!= charset
->id
)
5568 if (last_id
!= charset_ascii
)
5569 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5570 last_id
= charset
->id
;
5571 last_offset
= char_offset
;
5580 consumed_chars
= consumed_chars_base
;
5582 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5588 if (last_id
!= charset_ascii
)
5589 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5590 coding
->consumed_char
+= consumed_chars_base
;
5591 coding
->consumed
= src_base
- coding
->source
;
5592 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5596 encode_coding_charset (struct coding_system
*coding
)
5598 int multibytep
= coding
->dst_multibyte
;
5599 int *charbuf
= coding
->charbuf
;
5600 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5601 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5602 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5603 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5604 int produced_chars
= 0;
5605 Lisp_Object attrs
, charset_list
;
5606 int ascii_compatible
;
5609 CODING_GET_INFO (coding
, attrs
, charset_list
);
5610 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5612 while (charbuf
< charbuf_end
)
5614 struct charset
*charset
;
5617 ASSURE_DESTINATION (safe_room
);
5619 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5620 EMIT_ONE_ASCII_BYTE (c
);
5621 else if (CHAR_BYTE8_P (c
))
5623 c
= CHAR_TO_BYTE8 (c
);
5628 charset
= char_charset (c
, charset_list
, &code
);
5631 if (CHARSET_DIMENSION (charset
) == 1)
5632 EMIT_ONE_BYTE (code
);
5633 else if (CHARSET_DIMENSION (charset
) == 2)
5634 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5635 else if (CHARSET_DIMENSION (charset
) == 3)
5636 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5638 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5639 (code
>> 8) & 0xFF, code
& 0xFF);
5643 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5644 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5646 c
= coding
->default_char
;
5652 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5653 coding
->produced_char
+= produced_chars
;
5654 coding
->produced
= dst
- coding
->destination
;
5659 /*** 7. C library functions ***/
5661 /* Setup coding context CODING from information about CODING_SYSTEM.
5662 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5663 CODING_SYSTEM is invalid, signal an error. */
5666 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5669 Lisp_Object eol_type
;
5670 Lisp_Object coding_type
;
5673 if (NILP (coding_system
))
5674 coding_system
= Qundecided
;
5676 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5678 attrs
= CODING_ID_ATTRS (coding
->id
);
5679 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5682 coding
->head_ascii
= -1;
5683 if (VECTORP (eol_type
))
5684 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5685 | CODING_REQUIRE_DETECTION_MASK
);
5686 else if (! EQ (eol_type
, Qunix
))
5687 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5688 | CODING_REQUIRE_ENCODING_MASK
);
5690 coding
->common_flags
= 0;
5691 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5692 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5693 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5694 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5695 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5696 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5698 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5699 coding
->max_charset_id
= SCHARS (val
) - 1;
5700 coding
->safe_charsets
= SDATA (val
);
5701 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5702 coding
->carryover_bytes
= 0;
5704 coding_type
= CODING_ATTR_TYPE (attrs
);
5705 if (EQ (coding_type
, Qundecided
))
5707 coding
->detector
= NULL
;
5708 coding
->decoder
= decode_coding_raw_text
;
5709 coding
->encoder
= encode_coding_raw_text
;
5710 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5712 else if (EQ (coding_type
, Qiso_2022
))
5715 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5717 /* Invoke graphic register 0 to plane 0. */
5718 CODING_ISO_INVOCATION (coding
, 0) = 0;
5719 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5720 CODING_ISO_INVOCATION (coding
, 1)
5721 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5722 /* Setup the initial status of designation. */
5723 for (i
= 0; i
< 4; i
++)
5724 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5725 /* Not single shifting initially. */
5726 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5727 /* Beginning of buffer should also be regarded as bol. */
5728 CODING_ISO_BOL (coding
) = 1;
5729 coding
->detector
= detect_coding_iso_2022
;
5730 coding
->decoder
= decode_coding_iso_2022
;
5731 coding
->encoder
= encode_coding_iso_2022
;
5732 if (flags
& CODING_ISO_FLAG_SAFE
)
5733 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5734 coding
->common_flags
5735 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5736 | CODING_REQUIRE_FLUSHING_MASK
);
5737 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5738 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5739 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5740 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5741 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5743 setup_iso_safe_charsets (attrs
);
5744 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5745 coding
->max_charset_id
= SCHARS (val
) - 1;
5746 coding
->safe_charsets
= SDATA (val
);
5748 CODING_ISO_FLAGS (coding
) = flags
;
5749 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5750 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5751 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5752 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5754 else if (EQ (coding_type
, Qcharset
))
5756 coding
->detector
= detect_coding_charset
;
5757 coding
->decoder
= decode_coding_charset
;
5758 coding
->encoder
= encode_coding_charset
;
5759 coding
->common_flags
5760 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5762 else if (EQ (coding_type
, Qutf_8
))
5764 val
= AREF (attrs
, coding_attr_utf_bom
);
5765 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5766 : EQ (val
, Qt
) ? utf_with_bom
5768 coding
->detector
= detect_coding_utf_8
;
5769 coding
->decoder
= decode_coding_utf_8
;
5770 coding
->encoder
= encode_coding_utf_8
;
5771 coding
->common_flags
5772 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5773 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5774 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5776 else if (EQ (coding_type
, Qutf_16
))
5778 val
= AREF (attrs
, coding_attr_utf_bom
);
5779 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5780 : EQ (val
, Qt
) ? utf_with_bom
5782 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5783 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5784 : utf_16_little_endian
);
5785 CODING_UTF_16_SURROGATE (coding
) = 0;
5786 coding
->detector
= detect_coding_utf_16
;
5787 coding
->decoder
= decode_coding_utf_16
;
5788 coding
->encoder
= encode_coding_utf_16
;
5789 coding
->common_flags
5790 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5791 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5792 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5794 else if (EQ (coding_type
, Qccl
))
5796 coding
->detector
= detect_coding_ccl
;
5797 coding
->decoder
= decode_coding_ccl
;
5798 coding
->encoder
= encode_coding_ccl
;
5799 coding
->common_flags
5800 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5801 | CODING_REQUIRE_FLUSHING_MASK
);
5803 else if (EQ (coding_type
, Qemacs_mule
))
5805 coding
->detector
= detect_coding_emacs_mule
;
5806 coding
->decoder
= decode_coding_emacs_mule
;
5807 coding
->encoder
= encode_coding_emacs_mule
;
5808 coding
->common_flags
5809 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5810 coding
->spec
.emacs_mule
.full_support
= 1;
5811 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5812 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5814 Lisp_Object tail
, safe_charsets
;
5815 int max_charset_id
= 0;
5817 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5819 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5820 max_charset_id
= XFASTINT (XCAR (tail
));
5821 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5822 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5823 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5825 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5826 coding
->max_charset_id
= max_charset_id
;
5827 coding
->safe_charsets
= SDATA (safe_charsets
);
5828 coding
->spec
.emacs_mule
.full_support
= 1;
5830 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5831 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5833 else if (EQ (coding_type
, Qshift_jis
))
5835 coding
->detector
= detect_coding_sjis
;
5836 coding
->decoder
= decode_coding_sjis
;
5837 coding
->encoder
= encode_coding_sjis
;
5838 coding
->common_flags
5839 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5841 else if (EQ (coding_type
, Qbig5
))
5843 coding
->detector
= detect_coding_big5
;
5844 coding
->decoder
= decode_coding_big5
;
5845 coding
->encoder
= encode_coding_big5
;
5846 coding
->common_flags
5847 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5849 else /* EQ (coding_type, Qraw_text) */
5851 coding
->detector
= NULL
;
5852 coding
->decoder
= decode_coding_raw_text
;
5853 coding
->encoder
= encode_coding_raw_text
;
5854 if (! EQ (eol_type
, Qunix
))
5856 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5857 if (! VECTORP (eol_type
))
5858 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5866 /* Return a list of charsets supported by CODING. */
5869 coding_charset_list (struct coding_system
*coding
)
5871 Lisp_Object attrs
, charset_list
;
5873 CODING_GET_INFO (coding
, attrs
, charset_list
);
5874 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5876 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5878 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5879 charset_list
= Viso_2022_charset_list
;
5881 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5883 charset_list
= Vemacs_mule_charset_list
;
5885 return charset_list
;
5889 /* Return a list of charsets supported by CODING-SYSTEM. */
5892 coding_system_charset_list (Lisp_Object coding_system
)
5895 Lisp_Object attrs
, charset_list
;
5897 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5898 attrs
= CODING_ID_ATTRS (id
);
5900 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5902 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5904 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5905 charset_list
= Viso_2022_charset_list
;
5907 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5909 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5911 charset_list
= Vemacs_mule_charset_list
;
5915 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5917 return charset_list
;
5921 /* Return raw-text or one of its subsidiaries that has the same
5922 eol_type as CODING-SYSTEM. */
5925 raw_text_coding_system (Lisp_Object coding_system
)
5927 Lisp_Object spec
, attrs
;
5928 Lisp_Object eol_type
, raw_text_eol_type
;
5930 if (NILP (coding_system
))
5932 spec
= CODING_SYSTEM_SPEC (coding_system
);
5933 attrs
= AREF (spec
, 0);
5935 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5936 return coding_system
;
5938 eol_type
= AREF (spec
, 2);
5939 if (VECTORP (eol_type
))
5941 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5942 raw_text_eol_type
= AREF (spec
, 2);
5943 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5944 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5945 : AREF (raw_text_eol_type
, 2));
5949 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5950 the subsidiary that has the same eol-spec as PARENT (if it is not
5951 nil and specifies end-of-line format) or the system's setting
5952 (system_eol_type). */
5955 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5957 Lisp_Object spec
, eol_type
;
5959 if (NILP (coding_system
))
5960 coding_system
= Qraw_text
;
5961 spec
= CODING_SYSTEM_SPEC (coding_system
);
5962 eol_type
= AREF (spec
, 2);
5963 if (VECTORP (eol_type
))
5965 Lisp_Object parent_eol_type
;
5967 if (! NILP (parent
))
5969 Lisp_Object parent_spec
;
5971 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5972 parent_eol_type
= AREF (parent_spec
, 2);
5973 if (VECTORP (parent_eol_type
))
5974 parent_eol_type
= system_eol_type
;
5977 parent_eol_type
= system_eol_type
;
5978 if (EQ (parent_eol_type
, Qunix
))
5979 coding_system
= AREF (eol_type
, 0);
5980 else if (EQ (parent_eol_type
, Qdos
))
5981 coding_system
= AREF (eol_type
, 1);
5982 else if (EQ (parent_eol_type
, Qmac
))
5983 coding_system
= AREF (eol_type
, 2);
5985 return coding_system
;
5989 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5990 decided for writing to a process. If not, complement them, and
5991 return a new coding system. */
5994 complement_process_encoding_system (Lisp_Object coding_system
)
5996 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5997 Lisp_Object spec
, attrs
;
6000 for (i
= 0; i
< 3; i
++)
6003 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
6005 coding_system
= preferred_coding_system ();
6006 spec
= CODING_SYSTEM_SPEC (coding_system
);
6009 attrs
= AREF (spec
, 0);
6010 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
6011 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
6012 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
6013 eol_base
= coding_system
;
6014 if (! NILP (coding_base
) && ! NILP (eol_base
))
6019 /* The original CODING_SYSTEM didn't specify text-conversion or
6020 eol-conversion. Be sure that we return a fully complemented
6022 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
6023 return coding_system
;
6027 /* Emacs has a mechanism to automatically detect a coding system if it
6028 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6029 it's impossible to distinguish some coding systems accurately
6030 because they use the same range of codes. So, at first, coding
6031 systems are categorized into 7, those are:
6033 o coding-category-emacs-mule
6035 The category for a coding system which has the same code range
6036 as Emacs' internal format. Assigned the coding-system (Lisp
6037 symbol) `emacs-mule' by default.
6039 o coding-category-sjis
6041 The category for a coding system which has the same code range
6042 as SJIS. Assigned the coding-system (Lisp
6043 symbol) `japanese-shift-jis' by default.
6045 o coding-category-iso-7
6047 The category for a coding system which has the same code range
6048 as ISO2022 of 7-bit environment. This doesn't use any locking
6049 shift and single shift functions. This can encode/decode all
6050 charsets. Assigned the coding-system (Lisp symbol)
6051 `iso-2022-7bit' by default.
6053 o coding-category-iso-7-tight
6055 Same as coding-category-iso-7 except that this can
6056 encode/decode only the specified charsets.
6058 o coding-category-iso-8-1
6060 The category for a coding system which has the same code range
6061 as ISO2022 of 8-bit environment and graphic plane 1 used only
6062 for DIMENSION1 charset. This doesn't use any locking shift
6063 and single shift functions. Assigned the coding-system (Lisp
6064 symbol) `iso-latin-1' by default.
6066 o coding-category-iso-8-2
6068 The category for a coding system which has the same code range
6069 as ISO2022 of 8-bit environment and graphic plane 1 used only
6070 for DIMENSION2 charset. This doesn't use any locking shift
6071 and single shift functions. Assigned the coding-system (Lisp
6072 symbol) `japanese-iso-8bit' by default.
6074 o coding-category-iso-7-else
6076 The category for a coding system which has the same code range
6077 as ISO2022 of 7-bit environment but uses locking shift or
6078 single shift functions. Assigned the coding-system (Lisp
6079 symbol) `iso-2022-7bit-lock' by default.
6081 o coding-category-iso-8-else
6083 The category for a coding system which has the same code range
6084 as ISO2022 of 8-bit environment but uses locking shift or
6085 single shift functions. Assigned the coding-system (Lisp
6086 symbol) `iso-2022-8bit-ss2' by default.
6088 o coding-category-big5
6090 The category for a coding system which has the same code range
6091 as BIG5. Assigned the coding-system (Lisp symbol)
6092 `cn-big5' by default.
6094 o coding-category-utf-8
6096 The category for a coding system which has the same code range
6097 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6098 symbol) `utf-8' by default.
6100 o coding-category-utf-16-be
6102 The category for a coding system in which a text has an
6103 Unicode signature (cf. Unicode Standard) in the order of BIG
6104 endian at the head. Assigned the coding-system (Lisp symbol)
6105 `utf-16-be' by default.
6107 o coding-category-utf-16-le
6109 The category for a coding system in which a text has an
6110 Unicode signature (cf. Unicode Standard) in the order of
6111 LITTLE endian at the head. Assigned the coding-system (Lisp
6112 symbol) `utf-16-le' by default.
6114 o coding-category-ccl
6116 The category for a coding system of which encoder/decoder is
6117 written in CCL programs. The default value is nil, i.e., no
6118 coding system is assigned.
6120 o coding-category-binary
6122 The category for a coding system not categorized in any of the
6123 above. Assigned the coding-system (Lisp symbol)
6124 `no-conversion' by default.
6126 Each of them is a Lisp symbol and the value is an actual
6127 `coding-system's (this is also a Lisp symbol) assigned by a user.
6128 What Emacs does actually is to detect a category of coding system.
6129 Then, it uses a `coding-system' assigned to it. If Emacs can't
6130 decide only one possible category, it selects a category of the
6131 highest priority. Priorities of categories are also specified by a
6132 user in a Lisp variable `coding-category-list'.
6136 #define EOL_SEEN_NONE 0
6137 #define EOL_SEEN_LF 1
6138 #define EOL_SEEN_CR 2
6139 #define EOL_SEEN_CRLF 4
6141 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6142 SOURCE is encoded. If CATEGORY is one of
6143 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6144 two-byte, else they are encoded by one-byte.
6146 Return one of EOL_SEEN_XXX. */
6148 #define MAX_EOL_CHECK_COUNT 3
6151 detect_eol (const unsigned char *source
, EMACS_INT src_bytes
,
6152 enum coding_category category
)
6154 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6157 int eol_seen
= EOL_SEEN_NONE
;
6159 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6163 msb
= category
== (coding_category_utf_16_le
6164 | coding_category_utf_16_le_nosig
);
6167 while (src
+ 1 < src_end
)
6170 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6175 this_eol
= EOL_SEEN_LF
;
6176 else if (src
+ 3 >= src_end
6177 || src
[msb
+ 2] != 0
6178 || src
[lsb
+ 2] != '\n')
6179 this_eol
= EOL_SEEN_CR
;
6182 this_eol
= EOL_SEEN_CRLF
;
6186 if (eol_seen
== EOL_SEEN_NONE
)
6187 /* This is the first end-of-line. */
6188 eol_seen
= this_eol
;
6189 else if (eol_seen
!= this_eol
)
6191 /* The found type is different from what found before.
6192 Allow for stray ^M characters in DOS EOL files. */
6193 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6194 || (eol_seen
== EOL_SEEN_CRLF
6195 && this_eol
== EOL_SEEN_CR
))
6196 eol_seen
= EOL_SEEN_CRLF
;
6199 eol_seen
= EOL_SEEN_LF
;
6203 if (++total
== MAX_EOL_CHECK_COUNT
)
6210 while (src
< src_end
)
6213 if (c
== '\n' || c
== '\r')
6218 this_eol
= EOL_SEEN_LF
;
6219 else if (src
>= src_end
|| *src
!= '\n')
6220 this_eol
= EOL_SEEN_CR
;
6222 this_eol
= EOL_SEEN_CRLF
, src
++;
6224 if (eol_seen
== EOL_SEEN_NONE
)
6225 /* This is the first end-of-line. */
6226 eol_seen
= this_eol
;
6227 else if (eol_seen
!= this_eol
)
6229 /* The found type is different from what found before.
6230 Allow for stray ^M characters in DOS EOL files. */
6231 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6232 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6233 eol_seen
= EOL_SEEN_CRLF
;
6236 eol_seen
= EOL_SEEN_LF
;
6240 if (++total
== MAX_EOL_CHECK_COUNT
)
6249 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6251 Lisp_Object eol_type
;
6253 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6254 if (eol_seen
& EOL_SEEN_LF
)
6256 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6259 else if (eol_seen
& EOL_SEEN_CRLF
)
6261 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6264 else if (eol_seen
& EOL_SEEN_CR
)
6266 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6272 /* Detect how a text specified in CODING is encoded. If a coding
6273 system is detected, update fields of CODING by the detected coding
6277 detect_coding (struct coding_system
*coding
)
6279 const unsigned char *src
, *src_end
;
6280 int saved_mode
= coding
->mode
;
6282 coding
->consumed
= coding
->consumed_char
= 0;
6283 coding
->produced
= coding
->produced_char
= 0;
6284 coding_set_source (coding
);
6286 src_end
= coding
->source
+ coding
->src_bytes
;
6287 coding
->head_ascii
= 0;
6289 /* If we have not yet decided the text encoding type, detect it
6291 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6294 struct coding_detection_info detect_info
;
6295 int null_byte_found
= 0, eight_bit_found
= 0;
6297 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6298 for (src
= coding
->source
; src
< src_end
; src
++)
6303 eight_bit_found
= 1;
6304 if (null_byte_found
)
6309 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6310 && ! inhibit_iso_escape_detection
6311 && ! detect_info
.checked
)
6313 if (detect_coding_iso_2022 (coding
, &detect_info
))
6315 /* We have scanned the whole data. */
6316 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6318 /* We didn't find an 8-bit code. We may
6319 have found a null-byte, but it's very
6320 rare that a binary file conforms to
6323 coding
->head_ascii
= src
- coding
->source
;
6325 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6329 else if (! c
&& !inhibit_null_byte_detection
)
6331 null_byte_found
= 1;
6332 if (eight_bit_found
)
6335 if (! eight_bit_found
)
6336 coding
->head_ascii
++;
6338 else if (! eight_bit_found
)
6339 coding
->head_ascii
++;
6342 if (null_byte_found
|| eight_bit_found
6343 || coding
->head_ascii
< coding
->src_bytes
6344 || detect_info
.found
)
6346 enum coding_category category
;
6347 struct coding_system
*this;
6349 if (coding
->head_ascii
== coding
->src_bytes
)
6350 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6351 for (i
= 0; i
< coding_category_raw_text
; i
++)
6353 category
= coding_priorities
[i
];
6354 this = coding_categories
+ category
;
6355 if (detect_info
.found
& (1 << category
))
6360 if (null_byte_found
)
6362 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6363 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6365 for (i
= 0; i
< coding_category_raw_text
; i
++)
6367 category
= coding_priorities
[i
];
6368 this = coding_categories
+ category
;
6371 /* No coding system of this category is defined. */
6372 detect_info
.rejected
|= (1 << category
);
6374 else if (category
>= coding_category_raw_text
)
6376 else if (detect_info
.checked
& (1 << category
))
6378 if (detect_info
.found
& (1 << category
))
6381 else if ((*(this->detector
)) (coding
, &detect_info
)
6382 && detect_info
.found
& (1 << category
))
6384 if (category
== coding_category_utf_16_auto
)
6386 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6387 category
= coding_category_utf_16_le
;
6389 category
= coding_category_utf_16_be
;
6396 if (i
< coding_category_raw_text
)
6397 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6398 else if (null_byte_found
)
6399 setup_coding_system (Qno_conversion
, coding
);
6400 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6401 == CATEGORY_MASK_ANY
)
6402 setup_coding_system (Qraw_text
, coding
);
6403 else if (detect_info
.rejected
)
6404 for (i
= 0; i
< coding_category_raw_text
; i
++)
6405 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6407 this = coding_categories
+ coding_priorities
[i
];
6408 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6413 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6414 == coding_category_utf_8_auto
)
6416 Lisp_Object coding_systems
;
6417 struct coding_detection_info detect_info
;
6420 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6421 detect_info
.found
= detect_info
.rejected
= 0;
6422 coding
->head_ascii
= 0;
6423 if (CONSP (coding_systems
)
6424 && detect_coding_utf_8 (coding
, &detect_info
))
6426 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6427 setup_coding_system (XCAR (coding_systems
), coding
);
6429 setup_coding_system (XCDR (coding_systems
), coding
);
6432 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6433 == coding_category_utf_16_auto
)
6435 Lisp_Object coding_systems
;
6436 struct coding_detection_info detect_info
;
6439 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6440 detect_info
.found
= detect_info
.rejected
= 0;
6441 coding
->head_ascii
= 0;
6442 if (CONSP (coding_systems
)
6443 && detect_coding_utf_16 (coding
, &detect_info
))
6445 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6446 setup_coding_system (XCAR (coding_systems
), coding
);
6447 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6448 setup_coding_system (XCDR (coding_systems
), coding
);
6451 coding
->mode
= saved_mode
;
6456 decode_eol (struct coding_system
*coding
)
6458 Lisp_Object eol_type
;
6459 unsigned char *p
, *pbeg
, *pend
;
6461 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6462 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6465 if (NILP (coding
->dst_object
))
6466 pbeg
= coding
->destination
;
6468 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6469 pend
= pbeg
+ coding
->produced
;
6471 if (VECTORP (eol_type
))
6473 int eol_seen
= EOL_SEEN_NONE
;
6475 for (p
= pbeg
; p
< pend
; p
++)
6478 eol_seen
|= EOL_SEEN_LF
;
6479 else if (*p
== '\r')
6481 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6483 eol_seen
|= EOL_SEEN_CRLF
;
6487 eol_seen
|= EOL_SEEN_CR
;
6490 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6491 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6492 && (eol_seen
& EOL_SEEN_CR
) != 0
6493 && (eol_seen
& EOL_SEEN_LF
) == 0)
6494 eol_seen
= EOL_SEEN_CRLF
;
6495 else if (eol_seen
!= EOL_SEEN_NONE
6496 && eol_seen
!= EOL_SEEN_LF
6497 && eol_seen
!= EOL_SEEN_CRLF
6498 && eol_seen
!= EOL_SEEN_CR
)
6499 eol_seen
= EOL_SEEN_LF
;
6500 if (eol_seen
!= EOL_SEEN_NONE
)
6501 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6504 if (EQ (eol_type
, Qmac
))
6506 for (p
= pbeg
; p
< pend
; p
++)
6510 else if (EQ (eol_type
, Qdos
))
6514 if (NILP (coding
->dst_object
))
6516 /* Start deleting '\r' from the tail to minimize the memory
6518 for (p
= pend
- 2; p
>= pbeg
; p
--)
6521 memmove (p
, p
+ 1, pend
-- - p
- 1);
6527 int pos_byte
= coding
->dst_pos_byte
;
6528 int pos
= coding
->dst_pos
;
6529 int pos_end
= pos
+ coding
->produced_char
- 1;
6531 while (pos
< pos_end
)
6533 p
= BYTE_POS_ADDR (pos_byte
);
6534 if (*p
== '\r' && p
[1] == '\n')
6536 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6541 if (coding
->dst_multibyte
)
6542 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6547 coding
->produced
-= n
;
6548 coding
->produced_char
-= n
;
6553 /* Return a translation table (or list of them) from coding system
6554 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6555 decoding (ENCODEP is zero). */
6558 get_translation_table (Lisp_Object attrs
, int encodep
, int *max_lookup
)
6560 Lisp_Object standard
, translation_table
;
6563 if (NILP (Venable_character_translation
))
6570 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6571 standard
= Vstandard_translation_table_for_encode
;
6573 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6574 standard
= Vstandard_translation_table_for_decode
;
6575 if (NILP (translation_table
))
6576 translation_table
= standard
;
6579 if (SYMBOLP (translation_table
))
6580 translation_table
= Fget (translation_table
, Qtranslation_table
);
6581 else if (CONSP (translation_table
))
6583 translation_table
= Fcopy_sequence (translation_table
);
6584 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6585 if (SYMBOLP (XCAR (val
)))
6586 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6588 if (CHAR_TABLE_P (standard
))
6590 if (CONSP (translation_table
))
6591 translation_table
= nconc2 (translation_table
,
6592 Fcons (standard
, Qnil
));
6594 translation_table
= Fcons (translation_table
,
6595 Fcons (standard
, Qnil
));
6602 if (CHAR_TABLE_P (translation_table
)
6603 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6605 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6606 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6607 *max_lookup
= XFASTINT (val
);
6609 else if (CONSP (translation_table
))
6611 Lisp_Object tail
, val
;
6613 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6614 if (CHAR_TABLE_P (XCAR (tail
))
6615 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6617 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6618 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6619 *max_lookup
= XFASTINT (val
);
6623 return translation_table
;
6626 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6629 if (CHAR_TABLE_P (table)) \
6631 trans = CHAR_TABLE_REF (table, c); \
6632 if (CHARACTERP (trans)) \
6633 c = XFASTINT (trans), trans = Qnil; \
6635 else if (CONSP (table)) \
6639 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6640 if (CHAR_TABLE_P (XCAR (tail))) \
6642 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6643 if (CHARACTERP (trans)) \
6644 c = XFASTINT (trans), trans = Qnil; \
6645 else if (! NILP (trans)) \
6652 /* Return a translation of character(s) at BUF according to TRANS.
6653 TRANS is TO-CHAR or ((FROM . TO) ...) where
6654 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6655 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6656 translation is found, and Qnil if not found..
6657 If BUF is too short to lookup characters in FROM, return Qt. */
6660 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6663 if (INTEGERP (trans
))
6665 for (; CONSP (trans
); trans
= XCDR (trans
))
6667 Lisp_Object val
= XCAR (trans
);
6668 Lisp_Object from
= XCAR (val
);
6669 int len
= ASIZE (from
);
6672 for (i
= 0; i
< len
; i
++)
6674 if (buf
+ i
== buf_end
)
6676 if (XINT (AREF (from
, i
)) != buf
[i
])
6687 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6690 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6691 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6693 EMACS_INT produced_chars
= 0;
6696 if (! coding
->chars_at_source
)
6698 /* Source characters are in coding->charbuf. */
6699 int *buf
= coding
->charbuf
;
6700 int *buf_end
= buf
+ coding
->charbuf_used
;
6702 if (EQ (coding
->src_object
, coding
->dst_object
))
6704 coding_set_source (coding
);
6705 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6708 while (buf
< buf_end
)
6714 int from_nchars
= 1, to_nchars
= 1;
6715 Lisp_Object trans
= Qnil
;
6717 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6720 trans
= get_translation (trans
, buf
, buf_end
);
6721 if (INTEGERP (trans
))
6723 else if (CONSP (trans
))
6725 from_nchars
= ASIZE (XCAR (trans
));
6726 trans
= XCDR (trans
);
6727 if (INTEGERP (trans
))
6731 to_nchars
= ASIZE (trans
);
6732 c
= XINT (AREF (trans
, 0));
6735 else if (EQ (trans
, Qt
) && ! last_block
)
6739 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6741 dst
= alloc_destination (coding
,
6743 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6745 if (EQ (coding
->src_object
, coding
->dst_object
))
6747 coding_set_source (coding
);
6748 dst_end
= (((unsigned char *) coding
->source
)
6749 + coding
->consumed
);
6752 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6755 for (i
= 0; i
< to_nchars
; i
++)
6758 c
= XINT (AREF (trans
, i
));
6759 if (coding
->dst_multibyte
6760 || ! CHAR_BYTE8_P (c
))
6761 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6763 *dst
++ = CHAR_TO_BYTE8 (c
);
6765 produced_chars
+= to_nchars
;
6769 /* This is an annotation datum. (-C) is the length. */
6772 carryover
= buf_end
- buf
;
6776 /* Source characters are at coding->source. */
6777 const unsigned char *src
= coding
->source
;
6778 const unsigned char *src_end
= src
+ coding
->consumed
;
6780 if (EQ (coding
->dst_object
, coding
->src_object
))
6781 dst_end
= (unsigned char *) src
;
6782 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6784 if (coding
->src_multibyte
)
6787 EMACS_INT consumed_chars
= 0;
6791 const unsigned char *src_base
= src
;
6797 if (EQ (coding
->src_object
, coding
->dst_object
))
6798 dst_end
= (unsigned char *) src
;
6801 EMACS_INT offset
= src
- coding
->source
;
6803 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6805 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6806 coding_set_source (coding
);
6807 src
= coding
->source
+ offset
;
6808 src_end
= coding
->source
+ coding
->src_bytes
;
6809 if (EQ (coding
->src_object
, coding
->dst_object
))
6810 dst_end
= (unsigned char *) src
;
6820 while (src
< src_end
)
6825 if (dst
>= dst_end
- 1)
6827 if (EQ (coding
->src_object
, coding
->dst_object
))
6828 dst_end
= (unsigned char *) src
;
6829 if (dst
>= dst_end
- 1)
6831 EMACS_INT offset
= src
- coding
->source
;
6832 EMACS_INT more_bytes
;
6834 if (EQ (coding
->src_object
, coding
->dst_object
))
6835 more_bytes
= ((src_end
- src
) / 2) + 2;
6837 more_bytes
= src_end
- src
+ 2;
6838 dst
= alloc_destination (coding
, more_bytes
, dst
);
6839 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6840 coding_set_source (coding
);
6841 src
= coding
->source
+ offset
;
6842 src_end
= coding
->source
+ coding
->src_bytes
;
6843 if (EQ (coding
->src_object
, coding
->dst_object
))
6844 dst_end
= (unsigned char *) src
;
6852 if (!EQ (coding
->src_object
, coding
->dst_object
))
6854 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6858 EMACS_INT offset
= src
- coding
->source
;
6860 dst
= alloc_destination (coding
, require
, dst
);
6861 coding_set_source (coding
);
6862 src
= coding
->source
+ offset
;
6863 src_end
= coding
->source
+ coding
->src_bytes
;
6866 produced_chars
= coding
->consumed_char
;
6867 while (src
< src_end
)
6872 produced
= dst
- (coding
->destination
+ coding
->produced
);
6873 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6874 insert_from_gap (produced_chars
, produced
);
6875 coding
->produced
+= produced
;
6876 coding
->produced_char
+= produced_chars
;
6880 /* Compose text in CODING->object according to the annotation data at
6881 CHARBUF. CHARBUF is an array:
6882 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6886 produce_composition (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6890 enum composition_method method
;
6891 Lisp_Object components
;
6893 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6894 to
= pos
+ charbuf
[2];
6895 method
= (enum composition_method
) (charbuf
[4]);
6897 if (method
== COMPOSITION_RELATIVE
)
6901 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6904 if (method
== COMPOSITION_WITH_RULE
)
6905 len
= charbuf
[2] * 3 - 2;
6906 charbuf
+= MAX_ANNOTATION_LENGTH
;
6907 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6908 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6910 if (charbuf
[i
] >= 0)
6911 args
[j
] = make_number (charbuf
[i
]);
6915 args
[j
] = make_number (charbuf
[i
] % 0x100);
6918 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6920 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6924 /* Put `charset' property on text in CODING->object according to
6925 the annotation data at CHARBUF. CHARBUF is an array:
6926 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6930 produce_charset (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6932 EMACS_INT from
= pos
- charbuf
[2];
6933 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6935 Fput_text_property (make_number (from
), make_number (pos
),
6936 Qcharset
, CHARSET_NAME (charset
),
6937 coding
->dst_object
);
6941 #define CHARBUF_SIZE 0x4000
6943 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6945 int size = CHARBUF_SIZE; \
6947 coding->charbuf = NULL; \
6948 while (size > 1024) \
6950 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6951 if (coding->charbuf) \
6955 if (! coding->charbuf) \
6957 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6958 return coding->result; \
6960 coding->charbuf_size = size; \
6965 produce_annotation (struct coding_system
*coding
, EMACS_INT pos
)
6967 int *charbuf
= coding
->charbuf
;
6968 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6970 if (NILP (coding
->dst_object
))
6973 while (charbuf
< charbuf_end
)
6979 int len
= -*charbuf
;
6984 case CODING_ANNOTATE_COMPOSITION_MASK
:
6985 produce_composition (coding
, charbuf
, pos
);
6987 case CODING_ANNOTATE_CHARSET_MASK
:
6988 produce_charset (coding
, charbuf
, pos
);
6996 /* Decode the data at CODING->src_object into CODING->dst_object.
6997 CODING->src_object is a buffer, a string, or nil.
6998 CODING->dst_object is a buffer.
7000 If CODING->src_object is a buffer, it must be the current buffer.
7001 In this case, if CODING->src_pos is positive, it is a position of
7002 the source text in the buffer, otherwise, the source text is in the
7003 gap area of the buffer, and CODING->src_pos specifies the offset of
7004 the text from GPT (which must be the same as PT). If this is the
7005 same buffer as CODING->dst_object, CODING->src_pos must be
7008 If CODING->src_object is a string, CODING->src_pos is an index to
7011 If CODING->src_object is nil, CODING->source must already point to
7012 the non-relocatable memory area. In this case, CODING->src_pos is
7013 an offset from CODING->source.
7015 The decoded data is inserted at the current point of the buffer
7020 decode_coding (struct coding_system
*coding
)
7023 Lisp_Object undo_list
;
7024 Lisp_Object translation_table
;
7025 struct ccl_spec cclspec
;
7029 if (BUFFERP (coding
->src_object
)
7030 && coding
->src_pos
> 0
7031 && coding
->src_pos
< GPT
7032 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7033 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7036 if (BUFFERP (coding
->dst_object
))
7038 if (current_buffer
!= XBUFFER (coding
->dst_object
))
7039 set_buffer_internal (XBUFFER (coding
->dst_object
));
7041 move_gap_both (PT
, PT_BYTE
);
7042 undo_list
= current_buffer
->undo_list
;
7043 current_buffer
->undo_list
= Qt
;
7046 coding
->consumed
= coding
->consumed_char
= 0;
7047 coding
->produced
= coding
->produced_char
= 0;
7048 coding
->chars_at_source
= 0;
7049 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7052 ALLOC_CONVERSION_WORK_AREA (coding
);
7054 attrs
= CODING_ID_ATTRS (coding
->id
);
7055 translation_table
= get_translation_table (attrs
, 0, NULL
);
7058 if (coding
->decoder
== decode_coding_ccl
)
7060 coding
->spec
.ccl
= &cclspec
;
7061 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7065 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7067 coding_set_source (coding
);
7068 coding
->annotated
= 0;
7069 coding
->charbuf_used
= carryover
;
7070 (*(coding
->decoder
)) (coding
);
7071 coding_set_destination (coding
);
7072 carryover
= produce_chars (coding
, translation_table
, 0);
7073 if (coding
->annotated
)
7074 produce_annotation (coding
, pos
);
7075 for (i
= 0; i
< carryover
; i
++)
7077 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7079 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7080 || (coding
->consumed
< coding
->src_bytes
7081 && (coding
->result
== CODING_RESULT_SUCCESS
7082 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7086 coding_set_destination (coding
);
7087 coding
->charbuf_used
= carryover
;
7088 produce_chars (coding
, translation_table
, 1);
7091 coding
->carryover_bytes
= 0;
7092 if (coding
->consumed
< coding
->src_bytes
)
7094 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7095 const unsigned char *src
;
7097 coding_set_source (coding
);
7098 coding_set_destination (coding
);
7099 src
= coding
->source
+ coding
->consumed
;
7101 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7103 /* Flush out unprocessed data as binary chars. We are sure
7104 that the number of data is less than the size of
7106 coding
->charbuf_used
= 0;
7107 coding
->chars_at_source
= 0;
7109 while (nbytes
-- > 0)
7114 c
= BYTE8_TO_CHAR (c
);
7115 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7117 produce_chars (coding
, Qnil
, 1);
7121 /* Record unprocessed bytes in coding->carryover. We are
7122 sure that the number of data is less than the size of
7123 coding->carryover. */
7124 unsigned char *p
= coding
->carryover
;
7126 if (nbytes
> sizeof coding
->carryover
)
7127 nbytes
= sizeof coding
->carryover
;
7128 coding
->carryover_bytes
= nbytes
;
7129 while (nbytes
-- > 0)
7132 coding
->consumed
= coding
->src_bytes
;
7135 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7136 && !inhibit_eol_conversion
)
7137 decode_eol (coding
);
7138 if (BUFFERP (coding
->dst_object
))
7140 current_buffer
->undo_list
= undo_list
;
7141 record_insert (coding
->dst_pos
, coding
->produced_char
);
7143 return coding
->result
;
7147 /* Extract an annotation datum from a composition starting at POS and
7148 ending before LIMIT of CODING->src_object (buffer or string), store
7149 the data in BUF, set *STOP to a starting position of the next
7150 composition (if any) or to LIMIT, and return the address of the
7151 next element of BUF.
7153 If such an annotation is not found, set *STOP to a starting
7154 position of a composition after POS (if any) or to LIMIT, and
7158 handle_composition_annotation (EMACS_INT pos
, EMACS_INT limit
,
7159 struct coding_system
*coding
, int *buf
,
7162 EMACS_INT start
, end
;
7165 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7168 else if (start
> pos
)
7174 /* We found a composition. Store the corresponding
7175 annotation data in BUF. */
7177 enum composition_method method
= COMPOSITION_METHOD (prop
);
7178 int nchars
= COMPOSITION_LENGTH (prop
);
7180 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7181 if (method
!= COMPOSITION_RELATIVE
)
7183 Lisp_Object components
;
7186 components
= COMPOSITION_COMPONENTS (prop
);
7187 if (VECTORP (components
))
7189 len
= XVECTOR (components
)->size
;
7190 for (i
= 0; i
< len
; i
++)
7191 *buf
++ = XINT (AREF (components
, i
));
7193 else if (STRINGP (components
))
7195 len
= SCHARS (components
);
7199 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7203 else if (INTEGERP (components
))
7206 *buf
++ = XINT (components
);
7208 else if (CONSP (components
))
7210 for (len
= 0; CONSP (components
);
7211 len
++, components
= XCDR (components
))
7212 *buf
++ = XINT (XCAR (components
));
7220 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7231 /* Extract an annotation datum from a text property `charset' at POS of
7232 CODING->src_object (buffer of string), store the data in BUF, set
7233 *STOP to the position where the value of `charset' property changes
7234 (limiting by LIMIT), and return the address of the next element of
7237 If the property value is nil, set *STOP to the position where the
7238 property value is non-nil (limiting by LIMIT), and return BUF. */
7241 handle_charset_annotation (EMACS_INT pos
, EMACS_INT limit
,
7242 struct coding_system
*coding
, int *buf
,
7245 Lisp_Object val
, next
;
7248 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7249 if (! NILP (val
) && CHARSETP (val
))
7250 id
= XINT (CHARSET_SYMBOL_ID (val
));
7253 ADD_CHARSET_DATA (buf
, 0, id
);
7254 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7256 make_number (limit
));
7257 *stop
= XINT (next
);
7263 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7266 int *buf
= coding
->charbuf
;
7267 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7268 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7269 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7270 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7271 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7272 int multibytep
= coding
->src_multibyte
;
7273 Lisp_Object eol_type
;
7275 EMACS_INT stop
, stop_composition
, stop_charset
;
7276 int *lookup_buf
= NULL
;
7278 if (! NILP (translation_table
))
7279 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7281 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7282 if (VECTORP (eol_type
))
7285 /* Note: composition handling is not yet implemented. */
7286 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7288 if (NILP (coding
->src_object
))
7289 stop
= stop_composition
= stop_charset
= end_pos
;
7292 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7293 stop
= stop_composition
= pos
;
7295 stop
= stop_composition
= end_pos
;
7296 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7297 stop
= stop_charset
= pos
;
7299 stop_charset
= end_pos
;
7302 /* Compensate for CRLF and conversion. */
7303 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7304 while (buf
< buf_end
)
7312 if (pos
== stop_composition
)
7313 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7314 buf
, &stop_composition
);
7315 if (pos
== stop_charset
)
7316 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7317 buf
, &stop_charset
);
7318 stop
= (stop_composition
< stop_charset
7319 ? stop_composition
: stop_charset
);
7326 if (coding
->encoder
== encode_coding_raw_text
7327 || coding
->encoder
== encode_coding_ccl
)
7329 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7330 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7332 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7335 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7336 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7338 if (! EQ (eol_type
, Qunix
))
7342 if (EQ (eol_type
, Qdos
))
7350 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7355 int from_nchars
= 1, to_nchars
= 1;
7356 int *lookup_buf_end
;
7357 const unsigned char *p
= src
;
7361 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7362 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7363 lookup_buf_end
= lookup_buf
+ i
;
7364 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7365 if (INTEGERP (trans
))
7367 else if (CONSP (trans
))
7369 from_nchars
= ASIZE (XCAR (trans
));
7370 trans
= XCDR (trans
);
7371 if (INTEGERP (trans
))
7375 to_nchars
= ASIZE (trans
);
7376 if (buf
+ to_nchars
> buf_end
)
7378 c
= XINT (AREF (trans
, 0));
7384 for (i
= 1; i
< to_nchars
; i
++)
7385 *buf
++ = XINT (AREF (trans
, i
));
7386 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7387 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7391 coding
->consumed
= src
- coding
->source
;
7392 coding
->consumed_char
= pos
- coding
->src_pos
;
7393 coding
->charbuf_used
= buf
- coding
->charbuf
;
7394 coding
->chars_at_source
= 0;
7398 /* Encode the text at CODING->src_object into CODING->dst_object.
7399 CODING->src_object is a buffer or a string.
7400 CODING->dst_object is a buffer or nil.
7402 If CODING->src_object is a buffer, it must be the current buffer.
7403 In this case, if CODING->src_pos is positive, it is a position of
7404 the source text in the buffer, otherwise. the source text is in the
7405 gap area of the buffer, and coding->src_pos specifies the offset of
7406 the text from GPT (which must be the same as PT). If this is the
7407 same buffer as CODING->dst_object, CODING->src_pos must be
7408 negative and CODING should not have `pre-write-conversion'.
7410 If CODING->src_object is a string, CODING should not have
7411 `pre-write-conversion'.
7413 If CODING->dst_object is a buffer, the encoded data is inserted at
7414 the current point of that buffer.
7416 If CODING->dst_object is nil, the encoded data is placed at the
7417 memory area specified by CODING->destination. */
7420 encode_coding (struct coding_system
*coding
)
7423 Lisp_Object translation_table
;
7425 struct ccl_spec cclspec
;
7427 attrs
= CODING_ID_ATTRS (coding
->id
);
7428 if (coding
->encoder
== encode_coding_raw_text
)
7429 translation_table
= Qnil
, max_lookup
= 0;
7431 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7433 if (BUFFERP (coding
->dst_object
))
7435 set_buffer_internal (XBUFFER (coding
->dst_object
));
7436 coding
->dst_multibyte
7437 = ! NILP (current_buffer
->enable_multibyte_characters
);
7440 coding
->consumed
= coding
->consumed_char
= 0;
7441 coding
->produced
= coding
->produced_char
= 0;
7442 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7445 ALLOC_CONVERSION_WORK_AREA (coding
);
7447 if (coding
->encoder
== encode_coding_ccl
)
7449 coding
->spec
.ccl
= &cclspec
;
7450 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7453 coding_set_source (coding
);
7454 consume_chars (coding
, translation_table
, max_lookup
);
7455 coding_set_destination (coding
);
7456 (*(coding
->encoder
)) (coding
);
7457 } while (coding
->consumed_char
< coding
->src_chars
);
7459 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7460 insert_from_gap (coding
->produced_char
, coding
->produced
);
7462 return (coding
->result
);
7466 /* Name (or base name) of work buffer for code conversion. */
7467 static Lisp_Object Vcode_conversion_workbuf_name
;
7469 /* A working buffer used by the top level conversion. Once it is
7470 created, it is never destroyed. It has the name
7471 Vcode_conversion_workbuf_name. The other working buffers are
7472 destroyed after the use is finished, and their names are modified
7473 versions of Vcode_conversion_workbuf_name. */
7474 static Lisp_Object Vcode_conversion_reused_workbuf
;
7476 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7477 static int reused_workbuf_in_use
;
7480 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7481 multibyteness of returning buffer. */
7484 make_conversion_work_buffer (int multibyte
)
7486 Lisp_Object name
, workbuf
;
7487 struct buffer
*current
;
7489 if (reused_workbuf_in_use
++)
7491 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7492 workbuf
= Fget_buffer_create (name
);
7496 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7497 Vcode_conversion_reused_workbuf
7498 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7499 workbuf
= Vcode_conversion_reused_workbuf
;
7501 current
= current_buffer
;
7502 set_buffer_internal (XBUFFER (workbuf
));
7503 /* We can't allow modification hooks to run in the work buffer. For
7504 instance, directory_files_internal assumes that file decoding
7505 doesn't compile new regexps. */
7506 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7508 current_buffer
->undo_list
= Qt
;
7509 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
7510 set_buffer_internal (current
);
7516 code_conversion_restore (Lisp_Object arg
)
7518 Lisp_Object current
, workbuf
;
7519 struct gcpro gcpro1
;
7522 current
= XCAR (arg
);
7523 workbuf
= XCDR (arg
);
7524 if (! NILP (workbuf
))
7526 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7527 reused_workbuf_in_use
= 0;
7528 else if (! NILP (Fbuffer_live_p (workbuf
)))
7529 Fkill_buffer (workbuf
);
7531 set_buffer_internal (XBUFFER (current
));
7537 code_conversion_save (int with_work_buf
, int multibyte
)
7539 Lisp_Object workbuf
= Qnil
;
7542 workbuf
= make_conversion_work_buffer (multibyte
);
7543 record_unwind_protect (code_conversion_restore
,
7544 Fcons (Fcurrent_buffer (), workbuf
));
7549 decode_coding_gap (struct coding_system
*coding
,
7550 EMACS_INT chars
, EMACS_INT bytes
)
7552 int count
= SPECPDL_INDEX ();
7555 code_conversion_save (0, 0);
7557 coding
->src_object
= Fcurrent_buffer ();
7558 coding
->src_chars
= chars
;
7559 coding
->src_bytes
= bytes
;
7560 coding
->src_pos
= -chars
;
7561 coding
->src_pos_byte
= -bytes
;
7562 coding
->src_multibyte
= chars
< bytes
;
7563 coding
->dst_object
= coding
->src_object
;
7564 coding
->dst_pos
= PT
;
7565 coding
->dst_pos_byte
= PT_BYTE
;
7566 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
7568 if (CODING_REQUIRE_DETECTION (coding
))
7569 detect_coding (coding
);
7571 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7572 current_buffer
->text
->inhibit_shrinking
= 1;
7573 decode_coding (coding
);
7574 current_buffer
->text
->inhibit_shrinking
= 0;
7576 attrs
= CODING_ID_ATTRS (coding
->id
);
7577 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7579 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7582 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7583 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7584 make_number (coding
->produced_char
));
7586 coding
->produced_char
+= Z
- prev_Z
;
7587 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7590 unbind_to (count
, Qnil
);
7591 return coding
->result
;
7595 encode_coding_gap (struct coding_system
*coding
,
7596 EMACS_INT chars
, EMACS_INT bytes
)
7598 int count
= SPECPDL_INDEX ();
7600 code_conversion_save (0, 0);
7602 coding
->src_object
= Fcurrent_buffer ();
7603 coding
->src_chars
= chars
;
7604 coding
->src_bytes
= bytes
;
7605 coding
->src_pos
= -chars
;
7606 coding
->src_pos_byte
= -bytes
;
7607 coding
->src_multibyte
= chars
< bytes
;
7608 coding
->dst_object
= coding
->src_object
;
7609 coding
->dst_pos
= PT
;
7610 coding
->dst_pos_byte
= PT_BYTE
;
7612 encode_coding (coding
);
7614 unbind_to (count
, Qnil
);
7615 return coding
->result
;
7619 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7620 SRC_OBJECT into DST_OBJECT by coding context CODING.
7622 SRC_OBJECT is a buffer, a string, or Qnil.
7624 If it is a buffer, the text is at point of the buffer. FROM and TO
7625 are positions in the buffer.
7627 If it is a string, the text is at the beginning of the string.
7628 FROM and TO are indices to the string.
7630 If it is nil, the text is at coding->source. FROM and TO are
7631 indices to coding->source.
7633 DST_OBJECT is a buffer, Qt, or Qnil.
7635 If it is a buffer, the decoded text is inserted at point of the
7636 buffer. If the buffer is the same as SRC_OBJECT, the source text
7639 If it is Qt, a string is made from the decoded text, and
7640 set in CODING->dst_object.
7642 If it is Qnil, the decoded text is stored at CODING->destination.
7643 The caller must allocate CODING->dst_bytes bytes at
7644 CODING->destination by xmalloc. If the decoded text is longer than
7645 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7649 decode_coding_object (struct coding_system
*coding
,
7650 Lisp_Object src_object
,
7651 EMACS_INT from
, EMACS_INT from_byte
,
7652 EMACS_INT to
, EMACS_INT to_byte
,
7653 Lisp_Object dst_object
)
7655 int count
= SPECPDL_INDEX ();
7656 unsigned char *destination
;
7657 EMACS_INT dst_bytes
;
7658 EMACS_INT chars
= to
- from
;
7659 EMACS_INT bytes
= to_byte
- from_byte
;
7661 int saved_pt
= -1, saved_pt_byte
;
7662 int need_marker_adjustment
= 0;
7663 Lisp_Object old_deactivate_mark
;
7665 old_deactivate_mark
= Vdeactivate_mark
;
7667 if (NILP (dst_object
))
7669 destination
= coding
->destination
;
7670 dst_bytes
= coding
->dst_bytes
;
7673 coding
->src_object
= src_object
;
7674 coding
->src_chars
= chars
;
7675 coding
->src_bytes
= bytes
;
7676 coding
->src_multibyte
= chars
< bytes
;
7678 if (STRINGP (src_object
))
7680 coding
->src_pos
= from
;
7681 coding
->src_pos_byte
= from_byte
;
7683 else if (BUFFERP (src_object
))
7685 set_buffer_internal (XBUFFER (src_object
));
7687 move_gap_both (from
, from_byte
);
7688 if (EQ (src_object
, dst_object
))
7690 struct Lisp_Marker
*tail
;
7692 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7694 tail
->need_adjustment
7695 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7696 need_marker_adjustment
|= tail
->need_adjustment
;
7698 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7699 TEMP_SET_PT_BOTH (from
, from_byte
);
7700 current_buffer
->text
->inhibit_shrinking
= 1;
7701 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7702 coding
->src_pos
= -chars
;
7703 coding
->src_pos_byte
= -bytes
;
7707 coding
->src_pos
= from
;
7708 coding
->src_pos_byte
= from_byte
;
7712 if (CODING_REQUIRE_DETECTION (coding
))
7713 detect_coding (coding
);
7714 attrs
= CODING_ID_ATTRS (coding
->id
);
7716 if (EQ (dst_object
, Qt
)
7717 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7718 && NILP (dst_object
)))
7720 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7721 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7722 coding
->dst_pos
= BEG
;
7723 coding
->dst_pos_byte
= BEG_BYTE
;
7725 else if (BUFFERP (dst_object
))
7727 code_conversion_save (0, 0);
7728 coding
->dst_object
= dst_object
;
7729 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7730 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7731 coding
->dst_multibyte
7732 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7736 code_conversion_save (0, 0);
7737 coding
->dst_object
= Qnil
;
7738 /* Most callers presume this will return a multibyte result, and they
7739 won't use `binary' or `raw-text' anyway, so let's not worry about
7740 CODING_FOR_UNIBYTE. */
7741 coding
->dst_multibyte
= 1;
7744 decode_coding (coding
);
7746 if (BUFFERP (coding
->dst_object
))
7747 set_buffer_internal (XBUFFER (coding
->dst_object
));
7749 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7751 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7752 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7755 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7756 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7757 old_deactivate_mark
);
7758 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7759 make_number (coding
->produced_char
));
7762 coding
->produced_char
+= Z
- prev_Z
;
7763 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7766 if (EQ (dst_object
, Qt
))
7768 coding
->dst_object
= Fbuffer_string ();
7770 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7772 set_buffer_internal (XBUFFER (coding
->dst_object
));
7773 if (dst_bytes
< coding
->produced
)
7775 destination
= xrealloc (destination
, coding
->produced
);
7778 record_conversion_result (coding
,
7779 CODING_RESULT_INSUFFICIENT_MEM
);
7780 unbind_to (count
, Qnil
);
7783 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7784 move_gap_both (BEGV
, BEGV_BYTE
);
7785 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7786 coding
->destination
= destination
;
7792 /* This is the case of:
7793 (BUFFERP (src_object) && EQ (src_object, dst_object))
7794 As we have moved PT while replacing the original buffer
7795 contents, we must recover it now. */
7796 set_buffer_internal (XBUFFER (src_object
));
7797 current_buffer
->text
->inhibit_shrinking
= 0;
7798 if (saved_pt
< from
)
7799 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7800 else if (saved_pt
< from
+ chars
)
7801 TEMP_SET_PT_BOTH (from
, from_byte
);
7802 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7803 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7804 saved_pt_byte
+ (coding
->produced
- bytes
));
7806 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7807 saved_pt_byte
+ (coding
->produced
- bytes
));
7809 if (need_marker_adjustment
)
7811 struct Lisp_Marker
*tail
;
7813 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7814 if (tail
->need_adjustment
)
7816 tail
->need_adjustment
= 0;
7817 if (tail
->insertion_type
)
7819 tail
->bytepos
= from_byte
;
7820 tail
->charpos
= from
;
7824 tail
->bytepos
= from_byte
+ coding
->produced
;
7826 = (NILP (current_buffer
->enable_multibyte_characters
)
7827 ? tail
->bytepos
: from
+ coding
->produced_char
);
7833 Vdeactivate_mark
= old_deactivate_mark
;
7834 unbind_to (count
, coding
->dst_object
);
7839 encode_coding_object (struct coding_system
*coding
,
7840 Lisp_Object src_object
,
7841 EMACS_INT from
, EMACS_INT from_byte
,
7842 EMACS_INT to
, EMACS_INT to_byte
,
7843 Lisp_Object dst_object
)
7845 int count
= SPECPDL_INDEX ();
7846 EMACS_INT chars
= to
- from
;
7847 EMACS_INT bytes
= to_byte
- from_byte
;
7849 int saved_pt
= -1, saved_pt_byte
;
7850 int need_marker_adjustment
= 0;
7851 int kill_src_buffer
= 0;
7852 Lisp_Object old_deactivate_mark
;
7854 old_deactivate_mark
= Vdeactivate_mark
;
7856 coding
->src_object
= src_object
;
7857 coding
->src_chars
= chars
;
7858 coding
->src_bytes
= bytes
;
7859 coding
->src_multibyte
= chars
< bytes
;
7861 attrs
= CODING_ID_ATTRS (coding
->id
);
7863 if (EQ (src_object
, dst_object
))
7865 struct Lisp_Marker
*tail
;
7867 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7869 tail
->need_adjustment
7870 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7871 need_marker_adjustment
|= tail
->need_adjustment
;
7875 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7877 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7878 set_buffer_internal (XBUFFER (coding
->src_object
));
7879 if (STRINGP (src_object
))
7880 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7881 else if (BUFFERP (src_object
))
7882 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7884 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7886 if (EQ (src_object
, dst_object
))
7888 set_buffer_internal (XBUFFER (src_object
));
7889 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7890 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7891 set_buffer_internal (XBUFFER (coding
->src_object
));
7895 Lisp_Object args
[3];
7896 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7898 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7899 old_deactivate_mark
);
7900 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7901 args
[1] = make_number (BEG
);
7902 args
[2] = make_number (Z
);
7903 safe_call (3, args
);
7906 if (XBUFFER (coding
->src_object
) != current_buffer
)
7907 kill_src_buffer
= 1;
7908 coding
->src_object
= Fcurrent_buffer ();
7910 move_gap_both (BEG
, BEG_BYTE
);
7911 coding
->src_chars
= Z
- BEG
;
7912 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7913 coding
->src_pos
= BEG
;
7914 coding
->src_pos_byte
= BEG_BYTE
;
7915 coding
->src_multibyte
= Z
< Z_BYTE
;
7917 else if (STRINGP (src_object
))
7919 code_conversion_save (0, 0);
7920 coding
->src_pos
= from
;
7921 coding
->src_pos_byte
= from_byte
;
7923 else if (BUFFERP (src_object
))
7925 code_conversion_save (0, 0);
7926 set_buffer_internal (XBUFFER (src_object
));
7927 if (EQ (src_object
, dst_object
))
7929 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7930 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7931 coding
->src_pos
= 0;
7932 coding
->src_pos_byte
= 0;
7936 if (from
< GPT
&& to
>= GPT
)
7937 move_gap_both (from
, from_byte
);
7938 coding
->src_pos
= from
;
7939 coding
->src_pos_byte
= from_byte
;
7943 code_conversion_save (0, 0);
7945 if (BUFFERP (dst_object
))
7947 coding
->dst_object
= dst_object
;
7948 if (EQ (src_object
, dst_object
))
7950 coding
->dst_pos
= from
;
7951 coding
->dst_pos_byte
= from_byte
;
7955 struct buffer
*current
= current_buffer
;
7957 set_buffer_temp (XBUFFER (dst_object
));
7958 coding
->dst_pos
= PT
;
7959 coding
->dst_pos_byte
= PT_BYTE
;
7960 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7961 set_buffer_temp (current
);
7963 coding
->dst_multibyte
7964 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7966 else if (EQ (dst_object
, Qt
))
7968 coding
->dst_object
= Qnil
;
7969 coding
->dst_bytes
= coding
->src_chars
;
7970 if (coding
->dst_bytes
== 0)
7971 coding
->dst_bytes
= 1;
7972 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7973 coding
->dst_multibyte
= 0;
7977 coding
->dst_object
= Qnil
;
7978 coding
->dst_multibyte
= 0;
7981 encode_coding (coding
);
7983 if (EQ (dst_object
, Qt
))
7985 if (BUFFERP (coding
->dst_object
))
7986 coding
->dst_object
= Fbuffer_string ();
7990 = make_unibyte_string ((char *) coding
->destination
,
7992 xfree (coding
->destination
);
7998 /* This is the case of:
7999 (BUFFERP (src_object) && EQ (src_object, dst_object))
8000 As we have moved PT while replacing the original buffer
8001 contents, we must recover it now. */
8002 set_buffer_internal (XBUFFER (src_object
));
8003 if (saved_pt
< from
)
8004 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8005 else if (saved_pt
< from
+ chars
)
8006 TEMP_SET_PT_BOTH (from
, from_byte
);
8007 else if (! NILP (current_buffer
->enable_multibyte_characters
))
8008 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8009 saved_pt_byte
+ (coding
->produced
- bytes
));
8011 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8012 saved_pt_byte
+ (coding
->produced
- bytes
));
8014 if (need_marker_adjustment
)
8016 struct Lisp_Marker
*tail
;
8018 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8019 if (tail
->need_adjustment
)
8021 tail
->need_adjustment
= 0;
8022 if (tail
->insertion_type
)
8024 tail
->bytepos
= from_byte
;
8025 tail
->charpos
= from
;
8029 tail
->bytepos
= from_byte
+ coding
->produced
;
8031 = (NILP (current_buffer
->enable_multibyte_characters
)
8032 ? tail
->bytepos
: from
+ coding
->produced_char
);
8038 if (kill_src_buffer
)
8039 Fkill_buffer (coding
->src_object
);
8041 Vdeactivate_mark
= old_deactivate_mark
;
8042 unbind_to (count
, Qnil
);
8047 preferred_coding_system (void)
8049 int id
= coding_categories
[coding_priorities
[0]].id
;
8051 return CODING_ID_NAME (id
);
8056 /*** 8. Emacs Lisp library functions ***/
8058 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8059 doc
: /* Return t if OBJECT is nil or a coding-system.
8060 See the documentation of `define-coding-system' for information
8061 about coding-system objects. */)
8062 (Lisp_Object object
)
8065 || CODING_SYSTEM_ID (object
) >= 0)
8067 if (! SYMBOLP (object
)
8068 || NILP (Fget (object
, Qcoding_system_define_form
)))
8073 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8074 Sread_non_nil_coding_system
, 1, 1, 0,
8075 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8076 (Lisp_Object prompt
)
8081 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8082 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8084 while (SCHARS (val
) == 0);
8085 return (Fintern (val
, Qnil
));
8088 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8089 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8090 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8091 Ignores case when completing coding systems (all Emacs coding systems
8092 are lower-case). */)
8093 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8096 int count
= SPECPDL_INDEX ();
8098 if (SYMBOLP (default_coding_system
))
8099 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8100 specbind (Qcompletion_ignore_case
, Qt
);
8101 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8102 Qt
, Qnil
, Qcoding_system_history
,
8103 default_coding_system
, Qnil
);
8104 unbind_to (count
, Qnil
);
8105 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8108 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8110 doc
: /* Check validity of CODING-SYSTEM.
8111 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8112 It is valid if it is nil or a symbol defined as a coding system by the
8113 function `define-coding-system'. */)
8114 (Lisp_Object coding_system
)
8116 Lisp_Object define_form
;
8118 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8119 if (! NILP (define_form
))
8121 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8122 safe_eval (define_form
);
8124 if (!NILP (Fcoding_system_p (coding_system
)))
8125 return coding_system
;
8126 xsignal1 (Qcoding_system_error
, coding_system
);
8130 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8131 HIGHEST is nonzero, return the coding system of the highest
8132 priority among the detected coding systems. Otherwise return a
8133 list of detected coding systems sorted by their priorities. If
8134 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8135 multibyte form but contains only ASCII and eight-bit chars.
8136 Otherwise, the bytes are raw bytes.
8138 CODING-SYSTEM controls the detection as below:
8140 If it is nil, detect both text-format and eol-format. If the
8141 text-format part of CODING-SYSTEM is already specified
8142 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8143 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8144 detect only text-format. */
8147 detect_coding_system (const unsigned char *src
,
8148 EMACS_INT src_chars
, EMACS_INT src_bytes
,
8149 int highest
, int multibytep
,
8150 Lisp_Object coding_system
)
8152 const unsigned char *src_end
= src
+ src_bytes
;
8153 Lisp_Object attrs
, eol_type
;
8154 Lisp_Object val
= Qnil
;
8155 struct coding_system coding
;
8157 struct coding_detection_info detect_info
;
8158 enum coding_category base_category
;
8159 int null_byte_found
= 0, eight_bit_found
= 0;
8161 if (NILP (coding_system
))
8162 coding_system
= Qundecided
;
8163 setup_coding_system (coding_system
, &coding
);
8164 attrs
= CODING_ID_ATTRS (coding
.id
);
8165 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8166 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8168 coding
.source
= src
;
8169 coding
.src_chars
= src_chars
;
8170 coding
.src_bytes
= src_bytes
;
8171 coding
.src_multibyte
= multibytep
;
8172 coding
.consumed
= 0;
8173 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8174 coding
.head_ascii
= 0;
8176 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8178 /* At first, detect text-format if necessary. */
8179 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8180 if (base_category
== coding_category_undecided
)
8182 enum coding_category category
;
8183 struct coding_system
*this;
8186 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8187 for (; src
< src_end
; src
++)
8192 eight_bit_found
= 1;
8193 if (null_byte_found
)
8198 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8199 && ! inhibit_iso_escape_detection
8200 && ! detect_info
.checked
)
8202 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8204 /* We have scanned the whole data. */
8205 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8207 /* We didn't find an 8-bit code. We may
8208 have found a null-byte, but it's very
8209 rare that a binary file confirm to
8212 coding
.head_ascii
= src
- coding
.source
;
8214 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8218 else if (! c
&& !inhibit_null_byte_detection
)
8220 null_byte_found
= 1;
8221 if (eight_bit_found
)
8224 if (! eight_bit_found
)
8225 coding
.head_ascii
++;
8227 else if (! eight_bit_found
)
8228 coding
.head_ascii
++;
8231 if (null_byte_found
|| eight_bit_found
8232 || coding
.head_ascii
< coding
.src_bytes
8233 || detect_info
.found
)
8235 if (coding
.head_ascii
== coding
.src_bytes
)
8236 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8237 for (i
= 0; i
< coding_category_raw_text
; i
++)
8239 category
= coding_priorities
[i
];
8240 this = coding_categories
+ category
;
8241 if (detect_info
.found
& (1 << category
))
8246 if (null_byte_found
)
8248 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8249 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8251 for (i
= 0; i
< coding_category_raw_text
; i
++)
8253 category
= coding_priorities
[i
];
8254 this = coding_categories
+ category
;
8258 /* No coding system of this category is defined. */
8259 detect_info
.rejected
|= (1 << category
);
8261 else if (category
>= coding_category_raw_text
)
8263 else if (detect_info
.checked
& (1 << category
))
8266 && (detect_info
.found
& (1 << category
)))
8269 else if ((*(this->detector
)) (&coding
, &detect_info
)
8271 && (detect_info
.found
& (1 << category
)))
8273 if (category
== coding_category_utf_16_auto
)
8275 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8276 category
= coding_category_utf_16_le
;
8278 category
= coding_category_utf_16_be
;
8286 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8289 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8290 id
= CODING_SYSTEM_ID (Qno_conversion
);
8291 val
= Fcons (make_number (id
), Qnil
);
8293 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8295 detect_info
.found
= CATEGORY_MASK_ANY
;
8296 id
= coding_categories
[coding_category_undecided
].id
;
8297 val
= Fcons (make_number (id
), Qnil
);
8301 if (detect_info
.found
)
8303 detect_info
.found
= 1 << category
;
8304 val
= Fcons (make_number (this->id
), Qnil
);
8307 for (i
= 0; i
< coding_category_raw_text
; i
++)
8308 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8310 detect_info
.found
= 1 << coding_priorities
[i
];
8311 id
= coding_categories
[coding_priorities
[i
]].id
;
8312 val
= Fcons (make_number (id
), Qnil
);
8318 int mask
= detect_info
.rejected
| detect_info
.found
;
8321 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8323 category
= coding_priorities
[i
];
8324 if (! (mask
& (1 << category
)))
8326 found
|= 1 << category
;
8327 id
= coding_categories
[category
].id
;
8329 val
= Fcons (make_number (id
), val
);
8332 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8334 category
= coding_priorities
[i
];
8335 if (detect_info
.found
& (1 << category
))
8337 id
= coding_categories
[category
].id
;
8338 val
= Fcons (make_number (id
), val
);
8341 detect_info
.found
|= found
;
8344 else if (base_category
== coding_category_utf_8_auto
)
8346 if (detect_coding_utf_8 (&coding
, &detect_info
))
8348 struct coding_system
*this;
8350 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8351 this = coding_categories
+ coding_category_utf_8_sig
;
8353 this = coding_categories
+ coding_category_utf_8_nosig
;
8354 val
= Fcons (make_number (this->id
), Qnil
);
8357 else if (base_category
== coding_category_utf_16_auto
)
8359 if (detect_coding_utf_16 (&coding
, &detect_info
))
8361 struct coding_system
*this;
8363 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8364 this = coding_categories
+ coding_category_utf_16_le
;
8365 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8366 this = coding_categories
+ coding_category_utf_16_be
;
8367 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8368 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8370 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8371 val
= Fcons (make_number (this->id
), Qnil
);
8376 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8377 val
= Fcons (make_number (coding
.id
), Qnil
);
8380 /* Then, detect eol-format if necessary. */
8382 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8385 if (VECTORP (eol_type
))
8387 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8389 if (null_byte_found
)
8390 normal_eol
= EOL_SEEN_LF
;
8392 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8393 coding_category_raw_text
);
8395 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8396 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8397 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8398 coding_category_utf_16_be
);
8399 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8400 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8401 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8402 coding_category_utf_16_le
);
8406 if (EQ (eol_type
, Qunix
))
8407 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8408 else if (EQ (eol_type
, Qdos
))
8409 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8411 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8414 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8416 enum coding_category category
;
8419 id
= XINT (XCAR (tail
));
8420 attrs
= CODING_ID_ATTRS (id
);
8421 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8422 eol_type
= CODING_ID_EOL_TYPE (id
);
8423 if (VECTORP (eol_type
))
8425 if (category
== coding_category_utf_16_be
8426 || category
== coding_category_utf_16_be_nosig
)
8427 this_eol
= utf_16_be_eol
;
8428 else if (category
== coding_category_utf_16_le
8429 || category
== coding_category_utf_16_le_nosig
)
8430 this_eol
= utf_16_le_eol
;
8432 this_eol
= normal_eol
;
8434 if (this_eol
== EOL_SEEN_LF
)
8435 XSETCAR (tail
, AREF (eol_type
, 0));
8436 else if (this_eol
== EOL_SEEN_CRLF
)
8437 XSETCAR (tail
, AREF (eol_type
, 1));
8438 else if (this_eol
== EOL_SEEN_CR
)
8439 XSETCAR (tail
, AREF (eol_type
, 2));
8441 XSETCAR (tail
, CODING_ID_NAME (id
));
8444 XSETCAR (tail
, CODING_ID_NAME (id
));
8448 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8452 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8454 doc
: /* Detect coding system of the text in the region between START and END.
8455 Return a list of possible coding systems ordered by priority.
8456 The coding systems to try and their priorities follows what
8457 the function `coding-system-priority-list' (which see) returns.
8459 If only ASCII characters are found (except for such ISO-2022 control
8460 characters as ESC), it returns a list of single element `undecided'
8461 or its subsidiary coding system according to a detected end-of-line
8464 If optional argument HIGHEST is non-nil, return the coding system of
8465 highest priority. */)
8466 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8469 int from_byte
, to_byte
;
8471 CHECK_NUMBER_COERCE_MARKER (start
);
8472 CHECK_NUMBER_COERCE_MARKER (end
);
8474 validate_region (&start
, &end
);
8475 from
= XINT (start
), to
= XINT (end
);
8476 from_byte
= CHAR_TO_BYTE (from
);
8477 to_byte
= CHAR_TO_BYTE (to
);
8479 if (from
< GPT
&& to
>= GPT
)
8480 move_gap_both (to
, to_byte
);
8482 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8483 to
- from
, to_byte
- from_byte
,
8485 !NILP (current_buffer
8486 ->enable_multibyte_characters
),
8490 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8492 doc
: /* Detect coding system of the text in STRING.
8493 Return a list of possible coding systems ordered by priority.
8494 The coding systems to try and their priorities follows what
8495 the function `coding-system-priority-list' (which see) returns.
8497 If only ASCII characters are found (except for such ISO-2022 control
8498 characters as ESC), it returns a list of single element `undecided'
8499 or its subsidiary coding system according to a detected end-of-line
8502 If optional argument HIGHEST is non-nil, return the coding system of
8503 highest priority. */)
8504 (Lisp_Object string
, Lisp_Object highest
)
8506 CHECK_STRING (string
);
8508 return detect_coding_system (SDATA (string
),
8509 SCHARS (string
), SBYTES (string
),
8510 !NILP (highest
), STRING_MULTIBYTE (string
),
8516 char_encodable_p (int c
, Lisp_Object attrs
)
8519 struct charset
*charset
;
8520 Lisp_Object translation_table
;
8522 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8523 if (! NILP (translation_table
))
8524 c
= translate_char (translation_table
, c
);
8525 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8526 CONSP (tail
); tail
= XCDR (tail
))
8528 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8529 if (CHAR_CHARSET_P (c
, charset
))
8532 return (! NILP (tail
));
8536 /* Return a list of coding systems that safely encode the text between
8537 START and END. If EXCLUDE is non-nil, it is a list of coding
8538 systems not to check. The returned list doesn't contain any such
8539 coding systems. In any case, if the text contains only ASCII or is
8540 unibyte, return t. */
8542 DEFUN ("find-coding-systems-region-internal",
8543 Ffind_coding_systems_region_internal
,
8544 Sfind_coding_systems_region_internal
, 2, 3, 0,
8545 doc
: /* Internal use only. */)
8546 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8548 Lisp_Object coding_attrs_list
, safe_codings
;
8549 EMACS_INT start_byte
, end_byte
;
8550 const unsigned char *p
, *pbeg
, *pend
;
8552 Lisp_Object tail
, elt
, work_table
;
8554 if (STRINGP (start
))
8556 if (!STRING_MULTIBYTE (start
)
8557 || SCHARS (start
) == SBYTES (start
))
8560 end_byte
= SBYTES (start
);
8564 CHECK_NUMBER_COERCE_MARKER (start
);
8565 CHECK_NUMBER_COERCE_MARKER (end
);
8566 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8567 args_out_of_range (start
, end
);
8568 if (NILP (current_buffer
->enable_multibyte_characters
))
8570 start_byte
= CHAR_TO_BYTE (XINT (start
));
8571 end_byte
= CHAR_TO_BYTE (XINT (end
));
8572 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8575 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8577 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8578 move_gap_both (XINT (start
), start_byte
);
8580 move_gap_both (XINT (end
), end_byte
);
8584 coding_attrs_list
= Qnil
;
8585 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8587 || NILP (Fmemq (XCAR (tail
), exclude
)))
8591 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8592 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8593 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8595 ASET (attrs
, coding_attr_trans_tbl
,
8596 get_translation_table (attrs
, 1, NULL
));
8597 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8601 if (STRINGP (start
))
8602 p
= pbeg
= SDATA (start
);
8604 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8605 pend
= p
+ (end_byte
- start_byte
);
8607 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8608 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8610 work_table
= Fmake_char_table (Qnil
, Qnil
);
8613 if (ASCII_BYTE_P (*p
))
8617 c
= STRING_CHAR_ADVANCE (p
);
8618 if (!NILP (char_table_ref (work_table
, c
)))
8619 /* This character was already checked. Ignore it. */
8622 charset_map_loaded
= 0;
8623 for (tail
= coding_attrs_list
; CONSP (tail
);)
8628 else if (char_encodable_p (c
, elt
))
8630 else if (CONSP (XCDR (tail
)))
8632 XSETCAR (tail
, XCAR (XCDR (tail
)));
8633 XSETCDR (tail
, XCDR (XCDR (tail
)));
8637 XSETCAR (tail
, Qnil
);
8641 if (charset_map_loaded
)
8643 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8645 if (STRINGP (start
))
8646 pbeg
= SDATA (start
);
8648 pbeg
= BYTE_POS_ADDR (start_byte
);
8649 p
= pbeg
+ p_offset
;
8650 pend
= pbeg
+ pend_offset
;
8652 char_table_set (work_table
, c
, Qt
);
8656 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8657 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8658 if (! NILP (XCAR (tail
)))
8659 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8661 return safe_codings
;
8665 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8666 Sunencodable_char_position
, 3, 5, 0,
8668 Return position of first un-encodable character in a region.
8669 START and END specify the region and CODING-SYSTEM specifies the
8670 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8672 If optional 4th argument COUNT is non-nil, it specifies at most how
8673 many un-encodable characters to search. In this case, the value is a
8676 If optional 5th argument STRING is non-nil, it is a string to search
8677 for un-encodable characters. In that case, START and END are indexes
8679 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8682 struct coding_system coding
;
8683 Lisp_Object attrs
, charset_list
, translation_table
;
8684 Lisp_Object positions
;
8686 const unsigned char *p
, *stop
, *pend
;
8687 int ascii_compatible
;
8689 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8690 attrs
= CODING_ID_ATTRS (coding
.id
);
8691 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8693 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8694 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8695 translation_table
= get_translation_table (attrs
, 1, NULL
);
8699 validate_region (&start
, &end
);
8700 from
= XINT (start
);
8702 if (NILP (current_buffer
->enable_multibyte_characters
)
8703 || (ascii_compatible
8704 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8706 p
= CHAR_POS_ADDR (from
);
8707 pend
= CHAR_POS_ADDR (to
);
8708 if (from
< GPT
&& to
>= GPT
)
8715 CHECK_STRING (string
);
8716 CHECK_NATNUM (start
);
8718 from
= XINT (start
);
8721 || to
> SCHARS (string
))
8722 args_out_of_range_3 (string
, start
, end
);
8723 if (! STRING_MULTIBYTE (string
))
8725 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8726 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8727 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8735 CHECK_NATNUM (count
);
8744 if (ascii_compatible
)
8745 while (p
< stop
&& ASCII_BYTE_P (*p
))
8755 c
= STRING_CHAR_ADVANCE (p
);
8756 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8757 && ! char_charset (translate_char (translation_table
, c
),
8758 charset_list
, NULL
))
8760 positions
= Fcons (make_number (from
), positions
);
8769 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8773 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8774 Scheck_coding_systems_region
, 3, 3, 0,
8775 doc
: /* Check if the region is encodable by coding systems.
8777 START and END are buffer positions specifying the region.
8778 CODING-SYSTEM-LIST is a list of coding systems to check.
8780 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8781 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8782 whole region, POS0, POS1, ... are buffer positions where non-encodable
8783 characters are found.
8785 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8788 START may be a string. In that case, check if the string is
8789 encodable, and the value contains indices to the string instead of
8790 buffer positions. END is ignored.
8792 If the current buffer (or START if it is a string) is unibyte, the value
8794 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8797 EMACS_INT start_byte
, end_byte
;
8799 const unsigned char *p
, *pbeg
, *pend
;
8801 Lisp_Object tail
, elt
, attrs
;
8803 if (STRINGP (start
))
8805 if (!STRING_MULTIBYTE (start
)
8806 || SCHARS (start
) == SBYTES (start
))
8809 end_byte
= SBYTES (start
);
8814 CHECK_NUMBER_COERCE_MARKER (start
);
8815 CHECK_NUMBER_COERCE_MARKER (end
);
8816 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8817 args_out_of_range (start
, end
);
8818 if (NILP (current_buffer
->enable_multibyte_characters
))
8820 start_byte
= CHAR_TO_BYTE (XINT (start
));
8821 end_byte
= CHAR_TO_BYTE (XINT (end
));
8822 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8825 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8827 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8828 move_gap_both (XINT (start
), start_byte
);
8830 move_gap_both (XINT (end
), end_byte
);
8836 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8839 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8840 ASET (attrs
, coding_attr_trans_tbl
,
8841 get_translation_table (attrs
, 1, NULL
));
8842 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8845 if (STRINGP (start
))
8846 p
= pbeg
= SDATA (start
);
8848 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8849 pend
= p
+ (end_byte
- start_byte
);
8851 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8852 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8856 if (ASCII_BYTE_P (*p
))
8860 c
= STRING_CHAR_ADVANCE (p
);
8862 charset_map_loaded
= 0;
8863 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8865 elt
= XCDR (XCAR (tail
));
8866 if (! char_encodable_p (c
, XCAR (elt
)))
8867 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8869 if (charset_map_loaded
)
8871 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8873 if (STRINGP (start
))
8874 pbeg
= SDATA (start
);
8876 pbeg
= BYTE_POS_ADDR (start_byte
);
8877 p
= pbeg
+ p_offset
;
8878 pend
= pbeg
+ pend_offset
;
8886 for (; CONSP (tail
); tail
= XCDR (tail
))
8889 if (CONSP (XCDR (XCDR (elt
))))
8890 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8899 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8900 Lisp_Object coding_system
, Lisp_Object dst_object
,
8901 int encodep
, int norecord
)
8903 struct coding_system coding
;
8904 EMACS_INT from
, from_byte
, to
, to_byte
;
8905 Lisp_Object src_object
;
8907 CHECK_NUMBER_COERCE_MARKER (start
);
8908 CHECK_NUMBER_COERCE_MARKER (end
);
8909 if (NILP (coding_system
))
8910 coding_system
= Qno_conversion
;
8912 CHECK_CODING_SYSTEM (coding_system
);
8913 src_object
= Fcurrent_buffer ();
8914 if (NILP (dst_object
))
8915 dst_object
= src_object
;
8916 else if (! EQ (dst_object
, Qt
))
8917 CHECK_BUFFER (dst_object
);
8919 validate_region (&start
, &end
);
8920 from
= XFASTINT (start
);
8921 from_byte
= CHAR_TO_BYTE (from
);
8922 to
= XFASTINT (end
);
8923 to_byte
= CHAR_TO_BYTE (to
);
8925 setup_coding_system (coding_system
, &coding
);
8926 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8929 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8932 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8935 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8937 return (BUFFERP (dst_object
)
8938 ? make_number (coding
.produced_char
)
8939 : coding
.dst_object
);
8943 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8944 3, 4, "r\nzCoding system: ",
8945 doc
: /* Decode the current region from the specified coding system.
8946 When called from a program, takes four arguments:
8947 START, END, CODING-SYSTEM, and DESTINATION.
8948 START and END are buffer positions.
8950 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8951 If nil, the region between START and END is replaced by the decoded text.
8952 If buffer, the decoded text is inserted in that buffer after point (point
8954 In those cases, the length of the decoded text is returned.
8955 If DESTINATION is t, the decoded text is returned.
8957 This function sets `last-coding-system-used' to the precise coding system
8958 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8959 not fully specified.) */)
8960 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8962 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8965 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8966 3, 4, "r\nzCoding system: ",
8967 doc
: /* Encode the current region by specified coding system.
8968 When called from a program, takes four arguments:
8969 START, END, CODING-SYSTEM and DESTINATION.
8970 START and END are buffer positions.
8972 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8973 If nil, the region between START and END is replace by the encoded text.
8974 If buffer, the encoded text is inserted in that buffer after point (point
8976 In those cases, the length of the encoded text is returned.
8977 If DESTINATION is t, the encoded text is returned.
8979 This function sets `last-coding-system-used' to the precise coding system
8980 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8981 not fully specified.) */)
8982 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8984 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8988 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8989 Lisp_Object dst_object
, int encodep
, int nocopy
, int norecord
)
8991 struct coding_system coding
;
8992 EMACS_INT chars
, bytes
;
8994 CHECK_STRING (string
);
8995 if (NILP (coding_system
))
8998 Vlast_coding_system_used
= Qno_conversion
;
8999 if (NILP (dst_object
))
9000 return (nocopy
? Fcopy_sequence (string
) : string
);
9003 if (NILP (coding_system
))
9004 coding_system
= Qno_conversion
;
9006 CHECK_CODING_SYSTEM (coding_system
);
9007 if (NILP (dst_object
))
9009 else if (! EQ (dst_object
, Qt
))
9010 CHECK_BUFFER (dst_object
);
9012 setup_coding_system (coding_system
, &coding
);
9013 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9014 chars
= SCHARS (string
);
9015 bytes
= SBYTES (string
);
9017 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9019 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9021 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9023 return (BUFFERP (dst_object
)
9024 ? make_number (coding
.produced_char
)
9025 : coding
.dst_object
);
9029 /* Encode or decode STRING according to CODING_SYSTEM.
9030 Do not set Vlast_coding_system_used.
9032 This function is called only from macros DECODE_FILE and
9033 ENCODE_FILE, thus we ignore character composition. */
9036 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9039 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9043 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9045 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9047 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9048 if the decoding operation is trivial.
9050 Optional fourth arg BUFFER non-nil means that the decoded text is
9051 inserted in that buffer after point (point does not move). In this
9052 case, the return value is the length of the decoded text.
9054 This function sets `last-coding-system-used' to the precise coding system
9055 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9056 not fully specified.) */)
9057 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9059 return code_convert_string (string
, coding_system
, buffer
,
9060 0, ! NILP (nocopy
), 0);
9063 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9065 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9067 Optional third arg NOCOPY non-nil means it is OK to return STRING
9068 itself if the encoding operation is trivial.
9070 Optional fourth arg BUFFER non-nil means that the encoded text is
9071 inserted in that buffer after point (point does not move). In this
9072 case, the return value is the length of the encoded text.
9074 This function sets `last-coding-system-used' to the precise coding system
9075 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9076 not fully specified.) */)
9077 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9079 return code_convert_string (string
, coding_system
, buffer
,
9080 1, ! NILP (nocopy
), 1);
9084 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9085 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9086 Return the corresponding character. */)
9089 Lisp_Object spec
, attrs
, val
;
9090 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9093 CHECK_NATNUM (code
);
9094 c
= XFASTINT (code
);
9095 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9096 attrs
= AREF (spec
, 0);
9098 if (ASCII_BYTE_P (c
)
9099 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9102 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9103 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9104 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9105 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9108 charset
= charset_roman
;
9109 else if (c
>= 0xA0 && c
< 0xDF)
9111 charset
= charset_kana
;
9116 int s1
= c
>> 8, s2
= c
& 0xFF;
9118 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
9119 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
9120 error ("Invalid code: %d", code
);
9122 charset
= charset_kanji
;
9124 c
= DECODE_CHAR (charset
, c
);
9126 error ("Invalid code: %d", code
);
9127 return make_number (c
);
9131 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9132 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9133 Return the corresponding code in SJIS. */)
9136 Lisp_Object spec
, attrs
, charset_list
;
9138 struct charset
*charset
;
9141 CHECK_CHARACTER (ch
);
9143 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9144 attrs
= AREF (spec
, 0);
9146 if (ASCII_CHAR_P (c
)
9147 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9150 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9151 charset
= char_charset (c
, charset_list
, &code
);
9152 if (code
== CHARSET_INVALID_CODE (charset
))
9153 error ("Can't encode by shift_jis encoding: %d", c
);
9156 return make_number (code
);
9159 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9160 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9161 Return the corresponding character. */)
9164 Lisp_Object spec
, attrs
, val
;
9165 struct charset
*charset_roman
, *charset_big5
, *charset
;
9168 CHECK_NATNUM (code
);
9169 c
= XFASTINT (code
);
9170 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9171 attrs
= AREF (spec
, 0);
9173 if (ASCII_BYTE_P (c
)
9174 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9177 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9178 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9179 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9182 charset
= charset_roman
;
9185 int b1
= c
>> 8, b2
= c
& 0x7F;
9186 if (b1
< 0xA1 || b1
> 0xFE
9187 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9188 error ("Invalid code: %d", code
);
9189 charset
= charset_big5
;
9191 c
= DECODE_CHAR (charset
, (unsigned )c
);
9193 error ("Invalid code: %d", code
);
9194 return make_number (c
);
9197 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9198 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9199 Return the corresponding character code in Big5. */)
9202 Lisp_Object spec
, attrs
, charset_list
;
9203 struct charset
*charset
;
9207 CHECK_CHARACTER (ch
);
9209 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9210 attrs
= AREF (spec
, 0);
9211 if (ASCII_CHAR_P (c
)
9212 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9215 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9216 charset
= char_charset (c
, charset_list
, &code
);
9217 if (code
== CHARSET_INVALID_CODE (charset
))
9218 error ("Can't encode by Big5 encoding: %d", c
);
9220 return make_number (code
);
9224 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9225 Sset_terminal_coding_system_internal
, 1, 2, 0,
9226 doc
: /* Internal use only. */)
9227 (Lisp_Object coding_system
, Lisp_Object terminal
)
9229 struct terminal
*term
= get_terminal (terminal
, 1);
9230 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9231 CHECK_SYMBOL (coding_system
);
9232 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9233 /* We had better not send unsafe characters to terminal. */
9234 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9235 /* Character composition should be disabled. */
9236 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9237 terminal_coding
->src_multibyte
= 1;
9238 terminal_coding
->dst_multibyte
= 0;
9239 if (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
)
9240 term
->charset_list
= coding_charset_list (terminal_coding
);
9242 term
->charset_list
= Fcons (make_number (charset_ascii
), Qnil
);
9246 DEFUN ("set-safe-terminal-coding-system-internal",
9247 Fset_safe_terminal_coding_system_internal
,
9248 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9249 doc
: /* Internal use only. */)
9250 (Lisp_Object coding_system
)
9252 CHECK_SYMBOL (coding_system
);
9253 setup_coding_system (Fcheck_coding_system (coding_system
),
9254 &safe_terminal_coding
);
9255 /* Character composition should be disabled. */
9256 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9257 safe_terminal_coding
.src_multibyte
= 1;
9258 safe_terminal_coding
.dst_multibyte
= 0;
9262 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9263 Sterminal_coding_system
, 0, 1, 0,
9264 doc
: /* Return coding system specified for terminal output on the given terminal.
9265 TERMINAL may be a terminal object, a frame, or nil for the selected
9266 frame's terminal device. */)
9267 (Lisp_Object terminal
)
9269 struct coding_system
*terminal_coding
9270 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9271 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9273 /* For backward compatibility, return nil if it is `undecided'. */
9274 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9277 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9278 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9279 doc
: /* Internal use only. */)
9280 (Lisp_Object coding_system
, Lisp_Object terminal
)
9282 struct terminal
*t
= get_terminal (terminal
, 1);
9283 CHECK_SYMBOL (coding_system
);
9284 if (NILP (coding_system
))
9285 coding_system
= Qno_conversion
;
9287 Fcheck_coding_system (coding_system
);
9288 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9289 /* Character composition should be disabled. */
9290 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9291 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9295 DEFUN ("keyboard-coding-system",
9296 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9297 doc
: /* Return coding system specified for decoding keyboard input. */)
9298 (Lisp_Object terminal
)
9300 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9301 (get_terminal (terminal
, 1))->id
);
9305 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9306 Sfind_operation_coding_system
, 1, MANY
, 0,
9307 doc
: /* Choose a coding system for an operation based on the target name.
9308 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9309 DECODING-SYSTEM is the coding system to use for decoding
9310 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9311 for encoding (in case OPERATION does encoding).
9313 The first argument OPERATION specifies an I/O primitive:
9314 For file I/O, `insert-file-contents' or `write-region'.
9315 For process I/O, `call-process', `call-process-region', or `start-process'.
9316 For network I/O, `open-network-stream'.
9318 The remaining arguments should be the same arguments that were passed
9319 to the primitive. Depending on which primitive, one of those arguments
9320 is selected as the TARGET. For example, if OPERATION does file I/O,
9321 whichever argument specifies the file name is TARGET.
9323 TARGET has a meaning which depends on OPERATION:
9324 For file I/O, TARGET is a file name (except for the special case below).
9325 For process I/O, TARGET is a process name.
9326 For network I/O, TARGET is a service name or a port number.
9328 This function looks up what is specified for TARGET in
9329 `file-coding-system-alist', `process-coding-system-alist',
9330 or `network-coding-system-alist' depending on OPERATION.
9331 They may specify a coding system, a cons of coding systems,
9332 or a function symbol to call.
9333 In the last case, we call the function with one argument,
9334 which is a list of all the arguments given to this function.
9335 If the function can't decide a coding system, it can return
9336 `undecided' so that the normal code-detection is performed.
9338 If OPERATION is `insert-file-contents', the argument corresponding to
9339 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9340 file name to look up, and BUFFER is a buffer that contains the file's
9341 contents (not yet decoded). If `file-coding-system-alist' specifies a
9342 function to call for FILENAME, that function should examine the
9343 contents of BUFFER instead of reading the file.
9345 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9346 (int nargs
, Lisp_Object
*args
)
9348 Lisp_Object operation
, target_idx
, target
, val
;
9349 register Lisp_Object chain
;
9352 error ("Too few arguments");
9353 operation
= args
[0];
9354 if (!SYMBOLP (operation
)
9355 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
9356 error ("Invalid first argument");
9357 if (nargs
< 1 + XINT (target_idx
))
9358 error ("Too few arguments for operation: %s",
9359 SDATA (SYMBOL_NAME (operation
)));
9360 target
= args
[XINT (target_idx
) + 1];
9361 if (!(STRINGP (target
)
9362 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9363 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9364 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9365 error ("Invalid %dth argument", XINT (target_idx
) + 1);
9367 target
= XCAR (target
);
9369 chain
= ((EQ (operation
, Qinsert_file_contents
)
9370 || EQ (operation
, Qwrite_region
))
9371 ? Vfile_coding_system_alist
9372 : (EQ (operation
, Qopen_network_stream
)
9373 ? Vnetwork_coding_system_alist
9374 : Vprocess_coding_system_alist
));
9378 for (; CONSP (chain
); chain
= XCDR (chain
))
9384 && ((STRINGP (target
)
9385 && STRINGP (XCAR (elt
))
9386 && fast_string_match (XCAR (elt
), target
) >= 0)
9387 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9390 /* Here, if VAL is both a valid coding system and a valid
9391 function symbol, we return VAL as a coding system. */
9394 if (! SYMBOLP (val
))
9396 if (! NILP (Fcoding_system_p (val
)))
9397 return Fcons (val
, val
);
9398 if (! NILP (Ffboundp (val
)))
9400 /* We use call1 rather than safe_call1
9401 so as to get bug reports about functions called here
9402 which don't handle the current interface. */
9403 val
= call1 (val
, Flist (nargs
, args
));
9406 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9407 return Fcons (val
, val
);
9415 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9416 Sset_coding_system_priority
, 0, MANY
, 0,
9417 doc
: /* Assign higher priority to the coding systems given as arguments.
9418 If multiple coding systems belong to the same category,
9419 all but the first one are ignored.
9421 usage: (set-coding-system-priority &rest coding-systems) */)
9422 (int nargs
, Lisp_Object
*args
)
9425 int changed
[coding_category_max
];
9426 enum coding_category priorities
[coding_category_max
];
9428 memset (changed
, 0, sizeof changed
);
9430 for (i
= j
= 0; i
< nargs
; i
++)
9432 enum coding_category category
;
9433 Lisp_Object spec
, attrs
;
9435 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9436 attrs
= AREF (spec
, 0);
9437 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9438 if (changed
[category
])
9439 /* Ignore this coding system because a coding system of the
9440 same category already had a higher priority. */
9442 changed
[category
] = 1;
9443 priorities
[j
++] = category
;
9444 if (coding_categories
[category
].id
>= 0
9445 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9446 setup_coding_system (args
[i
], &coding_categories
[category
]);
9447 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9450 /* Now we have decided top J priorities. Reflect the order of the
9451 original priorities to the remaining priorities. */
9453 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9455 while (j
< coding_category_max
9456 && changed
[coding_priorities
[j
]])
9458 if (j
== coding_category_max
)
9460 priorities
[i
] = coding_priorities
[j
];
9463 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9465 /* Update `coding-category-list'. */
9466 Vcoding_category_list
= Qnil
;
9467 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9468 Vcoding_category_list
9469 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9470 Vcoding_category_list
);
9475 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9476 Scoding_system_priority_list
, 0, 1, 0,
9477 doc
: /* Return a list of coding systems ordered by their priorities.
9478 The list contains a subset of coding systems; i.e. coding systems
9479 assigned to each coding category (see `coding-category-list').
9481 HIGHESTP non-nil means just return the highest priority one. */)
9482 (Lisp_Object highestp
)
9487 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9489 enum coding_category category
= coding_priorities
[i
];
9490 int id
= coding_categories
[category
].id
;
9495 attrs
= CODING_ID_ATTRS (id
);
9496 if (! NILP (highestp
))
9497 return CODING_ATTR_BASE_NAME (attrs
);
9498 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9500 return Fnreverse (val
);
9503 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9506 make_subsidiaries (Lisp_Object base
)
9508 Lisp_Object subsidiaries
;
9509 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9510 char *buf
= (char *) alloca (base_name_len
+ 6);
9513 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9514 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9515 for (i
= 0; i
< 3; i
++)
9517 memcpy (buf
+ base_name_len
, suffixes
[i
], strlen (suffixes
[i
]) + 1);
9518 ASET (subsidiaries
, i
, intern (buf
));
9520 return subsidiaries
;
9524 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9525 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9526 doc
: /* For internal use only.
9527 usage: (define-coding-system-internal ...) */)
9528 (int nargs
, Lisp_Object
*args
)
9531 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9532 Lisp_Object attrs
; /* Vector of attributes. */
9533 Lisp_Object eol_type
;
9534 Lisp_Object aliases
;
9535 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9536 enum coding_category category
;
9537 Lisp_Object tail
, val
;
9538 int max_charset_id
= 0;
9541 if (nargs
< coding_arg_max
)
9544 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9546 name
= args
[coding_arg_name
];
9547 CHECK_SYMBOL (name
);
9548 CODING_ATTR_BASE_NAME (attrs
) = name
;
9550 val
= args
[coding_arg_mnemonic
];
9551 if (! STRINGP (val
))
9552 CHECK_CHARACTER (val
);
9553 CODING_ATTR_MNEMONIC (attrs
) = val
;
9555 coding_type
= args
[coding_arg_coding_type
];
9556 CHECK_SYMBOL (coding_type
);
9557 CODING_ATTR_TYPE (attrs
) = coding_type
;
9559 charset_list
= args
[coding_arg_charset_list
];
9560 if (SYMBOLP (charset_list
))
9562 if (EQ (charset_list
, Qiso_2022
))
9564 if (! EQ (coding_type
, Qiso_2022
))
9565 error ("Invalid charset-list");
9566 charset_list
= Viso_2022_charset_list
;
9568 else if (EQ (charset_list
, Qemacs_mule
))
9570 if (! EQ (coding_type
, Qemacs_mule
))
9571 error ("Invalid charset-list");
9572 charset_list
= Vemacs_mule_charset_list
;
9574 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9575 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9576 max_charset_id
= XFASTINT (XCAR (tail
));
9580 charset_list
= Fcopy_sequence (charset_list
);
9581 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9583 struct charset
*charset
;
9586 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9587 if (EQ (coding_type
, Qiso_2022
)
9588 ? CHARSET_ISO_FINAL (charset
) < 0
9589 : EQ (coding_type
, Qemacs_mule
)
9590 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9592 error ("Can't handle charset `%s'",
9593 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9595 XSETCAR (tail
, make_number (charset
->id
));
9596 if (max_charset_id
< charset
->id
)
9597 max_charset_id
= charset
->id
;
9600 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9602 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9603 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9604 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9605 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9606 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9608 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9610 val
= args
[coding_arg_decode_translation_table
];
9611 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9613 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9615 val
= args
[coding_arg_encode_translation_table
];
9616 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9618 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9620 val
= args
[coding_arg_post_read_conversion
];
9622 CODING_ATTR_POST_READ (attrs
) = val
;
9624 val
= args
[coding_arg_pre_write_conversion
];
9626 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9628 val
= args
[coding_arg_default_char
];
9630 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9633 CHECK_CHARACTER (val
);
9634 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9637 val
= args
[coding_arg_for_unibyte
];
9638 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9640 val
= args
[coding_arg_plist
];
9642 CODING_ATTR_PLIST (attrs
) = val
;
9644 if (EQ (coding_type
, Qcharset
))
9646 /* Generate a lisp vector of 256 elements. Each element is nil,
9647 integer, or a list of charset IDs.
9649 If Nth element is nil, the byte code N is invalid in this
9652 If Nth element is a number NUM, N is the first byte of a
9653 charset whose ID is NUM.
9655 If Nth element is a list of charset IDs, N is the first byte
9656 of one of them. The list is sorted by dimensions of the
9657 charsets. A charset of smaller dimension comes first. */
9658 val
= Fmake_vector (make_number (256), Qnil
);
9660 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9662 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9663 int dim
= CHARSET_DIMENSION (charset
);
9664 int idx
= (dim
- 1) * 4;
9666 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9667 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9669 for (i
= charset
->code_space
[idx
];
9670 i
<= charset
->code_space
[idx
+ 1]; i
++)
9672 Lisp_Object tmp
, tmp2
;
9675 tmp
= AREF (val
, i
);
9678 else if (NUMBERP (tmp
))
9680 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9682 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9684 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9688 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9690 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9695 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9698 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9699 XSETCAR (tmp2
, XCAR (tail
));
9705 ASET (attrs
, coding_attr_charset_valids
, val
);
9706 category
= coding_category_charset
;
9708 else if (EQ (coding_type
, Qccl
))
9712 if (nargs
< coding_arg_ccl_max
)
9715 val
= args
[coding_arg_ccl_decoder
];
9716 CHECK_CCL_PROGRAM (val
);
9718 val
= Fcopy_sequence (val
);
9719 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9721 val
= args
[coding_arg_ccl_encoder
];
9722 CHECK_CCL_PROGRAM (val
);
9724 val
= Fcopy_sequence (val
);
9725 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9727 val
= args
[coding_arg_ccl_valids
];
9728 valids
= Fmake_string (make_number (256), make_number (0));
9729 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9736 from
= to
= XINT (val
);
9737 if (from
< 0 || from
> 255)
9738 args_out_of_range_3 (val
, make_number (0), make_number (255));
9743 CHECK_NATNUM_CAR (val
);
9744 CHECK_NATNUM_CDR (val
);
9745 from
= XINT (XCAR (val
));
9747 args_out_of_range_3 (XCAR (val
),
9748 make_number (0), make_number (255));
9749 to
= XINT (XCDR (val
));
9750 if (to
< from
|| to
> 255)
9751 args_out_of_range_3 (XCDR (val
),
9752 XCAR (val
), make_number (255));
9754 for (i
= from
; i
<= to
; i
++)
9755 SSET (valids
, i
, 1);
9757 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9759 category
= coding_category_ccl
;
9761 else if (EQ (coding_type
, Qutf_16
))
9763 Lisp_Object bom
, endian
;
9765 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9767 if (nargs
< coding_arg_utf16_max
)
9770 bom
= args
[coding_arg_utf16_bom
];
9771 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9775 CHECK_CODING_SYSTEM (val
);
9777 CHECK_CODING_SYSTEM (val
);
9779 ASET (attrs
, coding_attr_utf_bom
, bom
);
9781 endian
= args
[coding_arg_utf16_endian
];
9782 CHECK_SYMBOL (endian
);
9785 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9786 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9787 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9789 category
= (CONSP (bom
)
9790 ? coding_category_utf_16_auto
9792 ? (EQ (endian
, Qbig
)
9793 ? coding_category_utf_16_be_nosig
9794 : coding_category_utf_16_le_nosig
)
9795 : (EQ (endian
, Qbig
)
9796 ? coding_category_utf_16_be
9797 : coding_category_utf_16_le
));
9799 else if (EQ (coding_type
, Qiso_2022
))
9801 Lisp_Object initial
, reg_usage
, request
, flags
;
9804 if (nargs
< coding_arg_iso2022_max
)
9807 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9808 CHECK_VECTOR (initial
);
9809 for (i
= 0; i
< 4; i
++)
9811 val
= Faref (initial
, make_number (i
));
9814 struct charset
*charset
;
9816 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9817 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9818 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9819 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9822 ASET (initial
, i
, make_number (-1));
9825 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9826 CHECK_CONS (reg_usage
);
9827 CHECK_NUMBER_CAR (reg_usage
);
9828 CHECK_NUMBER_CDR (reg_usage
);
9830 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9831 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9839 CHECK_CHARSET_GET_ID (tmp
, id
);
9840 CHECK_NATNUM_CDR (val
);
9841 if (XINT (XCDR (val
)) >= 4)
9842 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9843 XSETCAR (val
, make_number (id
));
9846 flags
= args
[coding_arg_iso2022_flags
];
9847 CHECK_NATNUM (flags
);
9849 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9850 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9852 ASET (attrs
, coding_attr_iso_initial
, initial
);
9853 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9854 ASET (attrs
, coding_attr_iso_request
, request
);
9855 ASET (attrs
, coding_attr_iso_flags
, flags
);
9856 setup_iso_safe_charsets (attrs
);
9858 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9859 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9860 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9861 ? coding_category_iso_7_else
9862 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9863 ? coding_category_iso_7
9864 : coding_category_iso_7_tight
);
9867 int id
= XINT (AREF (initial
, 1));
9869 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9870 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9872 ? coding_category_iso_8_else
9873 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9874 ? coding_category_iso_8_1
9875 : coding_category_iso_8_2
);
9877 if (category
!= coding_category_iso_8_1
9878 && category
!= coding_category_iso_8_2
)
9879 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9881 else if (EQ (coding_type
, Qemacs_mule
))
9883 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9884 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9885 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9886 category
= coding_category_emacs_mule
;
9888 else if (EQ (coding_type
, Qshift_jis
))
9891 struct charset
*charset
;
9893 if (XINT (Flength (charset_list
)) != 3
9894 && XINT (Flength (charset_list
)) != 4)
9895 error ("There should be three or four charsets");
9897 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9898 if (CHARSET_DIMENSION (charset
) != 1)
9899 error ("Dimension of charset %s is not one",
9900 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9901 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9902 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9904 charset_list
= XCDR (charset_list
);
9905 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9906 if (CHARSET_DIMENSION (charset
) != 1)
9907 error ("Dimension of charset %s is not one",
9908 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9910 charset_list
= XCDR (charset_list
);
9911 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9912 if (CHARSET_DIMENSION (charset
) != 2)
9913 error ("Dimension of charset %s is not two",
9914 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9916 charset_list
= XCDR (charset_list
);
9917 if (! NILP (charset_list
))
9919 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9920 if (CHARSET_DIMENSION (charset
) != 2)
9921 error ("Dimension of charset %s is not two",
9922 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9925 category
= coding_category_sjis
;
9926 Vsjis_coding_system
= name
;
9928 else if (EQ (coding_type
, Qbig5
))
9930 struct charset
*charset
;
9932 if (XINT (Flength (charset_list
)) != 2)
9933 error ("There should be just two charsets");
9935 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9936 if (CHARSET_DIMENSION (charset
) != 1)
9937 error ("Dimension of charset %s is not one",
9938 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9939 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9940 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9942 charset_list
= XCDR (charset_list
);
9943 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9944 if (CHARSET_DIMENSION (charset
) != 2)
9945 error ("Dimension of charset %s is not two",
9946 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9948 category
= coding_category_big5
;
9949 Vbig5_coding_system
= name
;
9951 else if (EQ (coding_type
, Qraw_text
))
9953 category
= coding_category_raw_text
;
9954 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9956 else if (EQ (coding_type
, Qutf_8
))
9960 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9962 if (nargs
< coding_arg_utf8_max
)
9965 bom
= args
[coding_arg_utf8_bom
];
9966 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9970 CHECK_CODING_SYSTEM (val
);
9972 CHECK_CODING_SYSTEM (val
);
9974 ASET (attrs
, coding_attr_utf_bom
, bom
);
9976 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9977 : NILP (bom
) ? coding_category_utf_8_nosig
9978 : coding_category_utf_8_sig
);
9980 else if (EQ (coding_type
, Qundecided
))
9981 category
= coding_category_undecided
;
9983 error ("Invalid coding system type: %s",
9984 SDATA (SYMBOL_NAME (coding_type
)));
9986 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9987 CODING_ATTR_PLIST (attrs
)
9988 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9989 CODING_ATTR_PLIST (attrs
)));
9990 CODING_ATTR_PLIST (attrs
)
9991 = Fcons (QCascii_compatible_p
,
9992 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9993 CODING_ATTR_PLIST (attrs
)));
9995 eol_type
= args
[coding_arg_eol_type
];
9996 if (! NILP (eol_type
)
9997 && ! EQ (eol_type
, Qunix
)
9998 && ! EQ (eol_type
, Qdos
)
9999 && ! EQ (eol_type
, Qmac
))
10000 error ("Invalid eol-type");
10002 aliases
= Fcons (name
, Qnil
);
10004 if (NILP (eol_type
))
10006 eol_type
= make_subsidiaries (name
);
10007 for (i
= 0; i
< 3; i
++)
10009 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10011 this_name
= AREF (eol_type
, i
);
10012 this_aliases
= Fcons (this_name
, Qnil
);
10013 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10014 this_spec
= Fmake_vector (make_number (3), attrs
);
10015 ASET (this_spec
, 1, this_aliases
);
10016 ASET (this_spec
, 2, this_eol_type
);
10017 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10018 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10019 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10021 Vcoding_system_alist
10022 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10023 Vcoding_system_alist
);
10027 spec_vec
= Fmake_vector (make_number (3), attrs
);
10028 ASET (spec_vec
, 1, aliases
);
10029 ASET (spec_vec
, 2, eol_type
);
10031 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10032 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10033 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10035 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10036 Vcoding_system_alist
);
10039 int id
= coding_categories
[category
].id
;
10041 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10042 setup_coding_system (name
, &coding_categories
[category
]);
10048 return Fsignal (Qwrong_number_of_arguments
,
10049 Fcons (intern ("define-coding-system-internal"),
10050 make_number (nargs
)));
10054 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10056 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10057 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10059 Lisp_Object spec
, attrs
;
10061 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10062 attrs
= AREF (spec
, 0);
10063 if (EQ (prop
, QCmnemonic
))
10065 if (! STRINGP (val
))
10066 CHECK_CHARACTER (val
);
10067 CODING_ATTR_MNEMONIC (attrs
) = val
;
10069 else if (EQ (prop
, QCdefault_char
))
10072 val
= make_number (' ');
10074 CHECK_CHARACTER (val
);
10075 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10077 else if (EQ (prop
, QCdecode_translation_table
))
10079 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10080 CHECK_SYMBOL (val
);
10081 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10083 else if (EQ (prop
, QCencode_translation_table
))
10085 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10086 CHECK_SYMBOL (val
);
10087 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10089 else if (EQ (prop
, QCpost_read_conversion
))
10091 CHECK_SYMBOL (val
);
10092 CODING_ATTR_POST_READ (attrs
) = val
;
10094 else if (EQ (prop
, QCpre_write_conversion
))
10096 CHECK_SYMBOL (val
);
10097 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10099 else if (EQ (prop
, QCascii_compatible_p
))
10101 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10104 CODING_ATTR_PLIST (attrs
)
10105 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10110 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10111 Sdefine_coding_system_alias
, 2, 2, 0,
10112 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10113 (Lisp_Object alias
, Lisp_Object coding_system
)
10115 Lisp_Object spec
, aliases
, eol_type
, val
;
10117 CHECK_SYMBOL (alias
);
10118 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10119 aliases
= AREF (spec
, 1);
10120 /* ALIASES should be a list of length more than zero, and the first
10121 element is a base coding system. Append ALIAS at the tail of the
10123 while (!NILP (XCDR (aliases
)))
10124 aliases
= XCDR (aliases
);
10125 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10127 eol_type
= AREF (spec
, 2);
10128 if (VECTORP (eol_type
))
10130 Lisp_Object subsidiaries
;
10133 subsidiaries
= make_subsidiaries (alias
);
10134 for (i
= 0; i
< 3; i
++)
10135 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10136 AREF (eol_type
, i
));
10139 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10140 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10141 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10143 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10144 Vcoding_system_alist
);
10149 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10151 doc
: /* Return the base of CODING-SYSTEM.
10152 Any alias or subsidiary coding system is not a base coding system. */)
10153 (Lisp_Object coding_system
)
10155 Lisp_Object spec
, attrs
;
10157 if (NILP (coding_system
))
10158 return (Qno_conversion
);
10159 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10160 attrs
= AREF (spec
, 0);
10161 return CODING_ATTR_BASE_NAME (attrs
);
10164 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10166 doc
: "Return the property list of CODING-SYSTEM.")
10167 (Lisp_Object coding_system
)
10169 Lisp_Object spec
, attrs
;
10171 if (NILP (coding_system
))
10172 coding_system
= Qno_conversion
;
10173 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10174 attrs
= AREF (spec
, 0);
10175 return CODING_ATTR_PLIST (attrs
);
10179 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10181 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10182 (Lisp_Object coding_system
)
10186 if (NILP (coding_system
))
10187 coding_system
= Qno_conversion
;
10188 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10189 return AREF (spec
, 1);
10192 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10193 Scoding_system_eol_type
, 1, 1, 0,
10194 doc
: /* Return eol-type of CODING-SYSTEM.
10195 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10197 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10198 and CR respectively.
10200 A vector value indicates that a format of end-of-line should be
10201 detected automatically. Nth element of the vector is the subsidiary
10202 coding system whose eol-type is N. */)
10203 (Lisp_Object coding_system
)
10205 Lisp_Object spec
, eol_type
;
10208 if (NILP (coding_system
))
10209 coding_system
= Qno_conversion
;
10210 if (! CODING_SYSTEM_P (coding_system
))
10212 spec
= CODING_SYSTEM_SPEC (coding_system
);
10213 eol_type
= AREF (spec
, 2);
10214 if (VECTORP (eol_type
))
10215 return Fcopy_sequence (eol_type
);
10216 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10217 return make_number (n
);
10223 /*** 9. Post-amble ***/
10226 init_coding_once (void)
10230 for (i
= 0; i
< coding_category_max
; i
++)
10232 coding_categories
[i
].id
= -1;
10233 coding_priorities
[i
] = i
;
10236 /* ISO2022 specific initialize routine. */
10237 for (i
= 0; i
< 0x20; i
++)
10238 iso_code_class
[i
] = ISO_control_0
;
10239 for (i
= 0x21; i
< 0x7F; i
++)
10240 iso_code_class
[i
] = ISO_graphic_plane_0
;
10241 for (i
= 0x80; i
< 0xA0; i
++)
10242 iso_code_class
[i
] = ISO_control_1
;
10243 for (i
= 0xA1; i
< 0xFF; i
++)
10244 iso_code_class
[i
] = ISO_graphic_plane_1
;
10245 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10246 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10247 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10248 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10249 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10250 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10251 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10252 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10253 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10255 for (i
= 0; i
< 256; i
++)
10257 emacs_mule_bytes
[i
] = 1;
10259 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10260 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10261 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10262 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10268 syms_of_coding (void)
10270 staticpro (&Vcoding_system_hash_table
);
10272 Lisp_Object args
[2];
10275 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10278 staticpro (&Vsjis_coding_system
);
10279 Vsjis_coding_system
= Qnil
;
10281 staticpro (&Vbig5_coding_system
);
10282 Vbig5_coding_system
= Qnil
;
10284 staticpro (&Vcode_conversion_reused_workbuf
);
10285 Vcode_conversion_reused_workbuf
= Qnil
;
10287 staticpro (&Vcode_conversion_workbuf_name
);
10288 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10290 reused_workbuf_in_use
= 0;
10292 DEFSYM (Qcharset
, "charset");
10293 DEFSYM (Qtarget_idx
, "target-idx");
10294 DEFSYM (Qcoding_system_history
, "coding-system-history");
10295 Fset (Qcoding_system_history
, Qnil
);
10297 /* Target FILENAME is the first argument. */
10298 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10299 /* Target FILENAME is the third argument. */
10300 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10302 DEFSYM (Qcall_process
, "call-process");
10303 /* Target PROGRAM is the first argument. */
10304 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10306 DEFSYM (Qcall_process_region
, "call-process-region");
10307 /* Target PROGRAM is the third argument. */
10308 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10310 DEFSYM (Qstart_process
, "start-process");
10311 /* Target PROGRAM is the third argument. */
10312 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10314 DEFSYM (Qopen_network_stream
, "open-network-stream");
10315 /* Target SERVICE is the fourth argument. */
10316 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10318 DEFSYM (Qcoding_system
, "coding-system");
10319 DEFSYM (Qcoding_aliases
, "coding-aliases");
10321 DEFSYM (Qeol_type
, "eol-type");
10322 DEFSYM (Qunix
, "unix");
10323 DEFSYM (Qdos
, "dos");
10325 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10326 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10327 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10328 DEFSYM (Qdefault_char
, "default-char");
10329 DEFSYM (Qundecided
, "undecided");
10330 DEFSYM (Qno_conversion
, "no-conversion");
10331 DEFSYM (Qraw_text
, "raw-text");
10333 DEFSYM (Qiso_2022
, "iso-2022");
10335 DEFSYM (Qutf_8
, "utf-8");
10336 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10338 DEFSYM (Qutf_16
, "utf-16");
10339 DEFSYM (Qbig
, "big");
10340 DEFSYM (Qlittle
, "little");
10342 DEFSYM (Qshift_jis
, "shift-jis");
10343 DEFSYM (Qbig5
, "big5");
10345 DEFSYM (Qcoding_system_p
, "coding-system-p");
10347 DEFSYM (Qcoding_system_error
, "coding-system-error");
10348 Fput (Qcoding_system_error
, Qerror_conditions
,
10349 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10350 Fput (Qcoding_system_error
, Qerror_message
,
10351 make_pure_c_string ("Invalid coding system"));
10353 /* Intern this now in case it isn't already done.
10354 Setting this variable twice is harmless.
10355 But don't staticpro it here--that is done in alloc.c. */
10356 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10358 DEFSYM (Qtranslation_table
, "translation-table");
10359 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10360 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10361 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10362 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10364 DEFSYM (Qvalid_codes
, "valid-codes");
10366 DEFSYM (Qemacs_mule
, "emacs-mule");
10368 DEFSYM (QCcategory
, ":category");
10369 DEFSYM (QCmnemonic
, ":mnemonic");
10370 DEFSYM (QCdefault_char
, ":default-char");
10371 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10372 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10373 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10374 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10375 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10377 Vcoding_category_table
10378 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10379 staticpro (&Vcoding_category_table
);
10380 /* Followings are target of code detection. */
10381 ASET (Vcoding_category_table
, coding_category_iso_7
,
10382 intern_c_string ("coding-category-iso-7"));
10383 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10384 intern_c_string ("coding-category-iso-7-tight"));
10385 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10386 intern_c_string ("coding-category-iso-8-1"));
10387 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10388 intern_c_string ("coding-category-iso-8-2"));
10389 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10390 intern_c_string ("coding-category-iso-7-else"));
10391 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10392 intern_c_string ("coding-category-iso-8-else"));
10393 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10394 intern_c_string ("coding-category-utf-8-auto"));
10395 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10396 intern_c_string ("coding-category-utf-8"));
10397 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10398 intern_c_string ("coding-category-utf-8-sig"));
10399 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10400 intern_c_string ("coding-category-utf-16-be"));
10401 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10402 intern_c_string ("coding-category-utf-16-auto"));
10403 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10404 intern_c_string ("coding-category-utf-16-le"));
10405 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10406 intern_c_string ("coding-category-utf-16-be-nosig"));
10407 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10408 intern_c_string ("coding-category-utf-16-le-nosig"));
10409 ASET (Vcoding_category_table
, coding_category_charset
,
10410 intern_c_string ("coding-category-charset"));
10411 ASET (Vcoding_category_table
, coding_category_sjis
,
10412 intern_c_string ("coding-category-sjis"));
10413 ASET (Vcoding_category_table
, coding_category_big5
,
10414 intern_c_string ("coding-category-big5"));
10415 ASET (Vcoding_category_table
, coding_category_ccl
,
10416 intern_c_string ("coding-category-ccl"));
10417 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10418 intern_c_string ("coding-category-emacs-mule"));
10419 /* Followings are NOT target of code detection. */
10420 ASET (Vcoding_category_table
, coding_category_raw_text
,
10421 intern_c_string ("coding-category-raw-text"));
10422 ASET (Vcoding_category_table
, coding_category_undecided
,
10423 intern_c_string ("coding-category-undecided"));
10425 DEFSYM (Qinsufficient_source
, "insufficient-source");
10426 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10427 DEFSYM (Qinvalid_source
, "invalid-source");
10428 DEFSYM (Qinterrupted
, "interrupted");
10429 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10430 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10432 defsubr (&Scoding_system_p
);
10433 defsubr (&Sread_coding_system
);
10434 defsubr (&Sread_non_nil_coding_system
);
10435 defsubr (&Scheck_coding_system
);
10436 defsubr (&Sdetect_coding_region
);
10437 defsubr (&Sdetect_coding_string
);
10438 defsubr (&Sfind_coding_systems_region_internal
);
10439 defsubr (&Sunencodable_char_position
);
10440 defsubr (&Scheck_coding_systems_region
);
10441 defsubr (&Sdecode_coding_region
);
10442 defsubr (&Sencode_coding_region
);
10443 defsubr (&Sdecode_coding_string
);
10444 defsubr (&Sencode_coding_string
);
10445 defsubr (&Sdecode_sjis_char
);
10446 defsubr (&Sencode_sjis_char
);
10447 defsubr (&Sdecode_big5_char
);
10448 defsubr (&Sencode_big5_char
);
10449 defsubr (&Sset_terminal_coding_system_internal
);
10450 defsubr (&Sset_safe_terminal_coding_system_internal
);
10451 defsubr (&Sterminal_coding_system
);
10452 defsubr (&Sset_keyboard_coding_system_internal
);
10453 defsubr (&Skeyboard_coding_system
);
10454 defsubr (&Sfind_operation_coding_system
);
10455 defsubr (&Sset_coding_system_priority
);
10456 defsubr (&Sdefine_coding_system_internal
);
10457 defsubr (&Sdefine_coding_system_alias
);
10458 defsubr (&Scoding_system_put
);
10459 defsubr (&Scoding_system_base
);
10460 defsubr (&Scoding_system_plist
);
10461 defsubr (&Scoding_system_aliases
);
10462 defsubr (&Scoding_system_eol_type
);
10463 defsubr (&Scoding_system_priority_list
);
10465 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10466 doc
: /* List of coding systems.
10468 Do not alter the value of this variable manually. This variable should be
10469 updated by the functions `define-coding-system' and
10470 `define-coding-system-alias'. */);
10471 Vcoding_system_list
= Qnil
;
10473 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10474 doc
: /* Alist of coding system names.
10475 Each element is one element list of coding system name.
10476 This variable is given to `completing-read' as COLLECTION argument.
10478 Do not alter the value of this variable manually. This variable should be
10479 updated by the functions `make-coding-system' and
10480 `define-coding-system-alias'. */);
10481 Vcoding_system_alist
= Qnil
;
10483 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10484 doc
: /* List of coding-categories (symbols) ordered by priority.
10486 On detecting a coding system, Emacs tries code detection algorithms
10487 associated with each coding-category one by one in this order. When
10488 one algorithm agrees with a byte sequence of source text, the coding
10489 system bound to the corresponding coding-category is selected.
10491 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10495 Vcoding_category_list
= Qnil
;
10496 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10497 Vcoding_category_list
10498 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10499 Vcoding_category_list
);
10502 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10503 doc
: /* Specify the coding system for read operations.
10504 It is useful to bind this variable with `let', but do not set it globally.
10505 If the value is a coding system, it is used for decoding on read operation.
10506 If not, an appropriate element is used from one of the coding system alists.
10507 There are three such tables: `file-coding-system-alist',
10508 `process-coding-system-alist', and `network-coding-system-alist'. */);
10509 Vcoding_system_for_read
= Qnil
;
10511 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10512 doc
: /* Specify the coding system for write operations.
10513 Programs bind this variable with `let', but you should not set it globally.
10514 If the value is a coding system, it is used for encoding of output,
10515 when writing it to a file and when sending it to a file or subprocess.
10517 If this does not specify a coding system, an appropriate element
10518 is used from one of the coding system alists.
10519 There are three such tables: `file-coding-system-alist',
10520 `process-coding-system-alist', and `network-coding-system-alist'.
10521 For output to files, if the above procedure does not specify a coding system,
10522 the value of `buffer-file-coding-system' is used. */);
10523 Vcoding_system_for_write
= Qnil
;
10525 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10527 Coding system used in the latest file or process I/O. */);
10528 Vlast_coding_system_used
= Qnil
;
10530 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10532 Error status of the last code conversion.
10534 When an error was detected in the last code conversion, this variable
10535 is set to one of the following symbols.
10536 `insufficient-source'
10540 `insufficient-memory'
10541 When no error was detected, the value doesn't change. So, to check
10542 the error status of a code conversion by this variable, you must
10543 explicitly set this variable to nil before performing code
10545 Vlast_code_conversion_error
= Qnil
;
10547 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10549 *Non-nil means always inhibit code conversion of end-of-line format.
10550 See info node `Coding Systems' and info node `Text and Binary' concerning
10551 such conversion. */);
10552 inhibit_eol_conversion
= 0;
10554 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10556 Non-nil means process buffer inherits coding system of process output.
10557 Bind it to t if the process output is to be treated as if it were a file
10558 read from some filesystem. */);
10559 inherit_process_coding_system
= 0;
10561 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10563 Alist to decide a coding system to use for a file I/O operation.
10564 The format is ((PATTERN . VAL) ...),
10565 where PATTERN is a regular expression matching a file name,
10566 VAL is a coding system, a cons of coding systems, or a function symbol.
10567 If VAL is a coding system, it is used for both decoding and encoding
10569 If VAL is a cons of coding systems, the car part is used for decoding,
10570 and the cdr part is used for encoding.
10571 If VAL is a function symbol, the function must return a coding system
10572 or a cons of coding systems which are used as above. The function is
10573 called with an argument that is a list of the arguments with which
10574 `find-operation-coding-system' was called. If the function can't decide
10575 a coding system, it can return `undecided' so that the normal
10576 code-detection is performed.
10578 See also the function `find-operation-coding-system'
10579 and the variable `auto-coding-alist'. */);
10580 Vfile_coding_system_alist
= Qnil
;
10582 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10584 Alist to decide a coding system to use for a process I/O operation.
10585 The format is ((PATTERN . VAL) ...),
10586 where PATTERN is a regular expression matching a program name,
10587 VAL is a coding system, a cons of coding systems, or a function symbol.
10588 If VAL is a coding system, it is used for both decoding what received
10589 from the program and encoding what sent to the program.
10590 If VAL is a cons of coding systems, the car part is used for decoding,
10591 and the cdr part is used for encoding.
10592 If VAL is a function symbol, the function must return a coding system
10593 or a cons of coding systems which are used as above.
10595 See also the function `find-operation-coding-system'. */);
10596 Vprocess_coding_system_alist
= Qnil
;
10598 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10600 Alist to decide a coding system to use for a network I/O operation.
10601 The format is ((PATTERN . VAL) ...),
10602 where PATTERN is a regular expression matching a network service name
10603 or is a port number to connect to,
10604 VAL is a coding system, a cons of coding systems, or a function symbol.
10605 If VAL is a coding system, it is used for both decoding what received
10606 from the network stream and encoding what sent to the network stream.
10607 If VAL is a cons of coding systems, the car part is used for decoding,
10608 and the cdr part is used for encoding.
10609 If VAL is a function symbol, the function must return a coding system
10610 or a cons of coding systems which are used as above.
10612 See also the function `find-operation-coding-system'. */);
10613 Vnetwork_coding_system_alist
= Qnil
;
10615 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10616 doc
: /* Coding system to use with system messages.
10617 Also used for decoding keyboard input on X Window system. */);
10618 Vlocale_coding_system
= Qnil
;
10620 /* The eol mnemonics are reset in startup.el system-dependently. */
10621 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10623 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10624 eol_mnemonic_unix
= make_pure_c_string (":");
10626 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10628 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10629 eol_mnemonic_dos
= make_pure_c_string ("\\");
10631 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10633 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10634 eol_mnemonic_mac
= make_pure_c_string ("/");
10636 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10638 *String displayed in mode line when end-of-line format is not yet determined. */);
10639 eol_mnemonic_undecided
= make_pure_c_string (":");
10641 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10643 *Non-nil enables character translation while encoding and decoding. */);
10644 Venable_character_translation
= Qt
;
10646 DEFVAR_LISP ("standard-translation-table-for-decode",
10647 Vstandard_translation_table_for_decode
,
10648 doc
: /* Table for translating characters while decoding. */);
10649 Vstandard_translation_table_for_decode
= Qnil
;
10651 DEFVAR_LISP ("standard-translation-table-for-encode",
10652 Vstandard_translation_table_for_encode
,
10653 doc
: /* Table for translating characters while encoding. */);
10654 Vstandard_translation_table_for_encode
= Qnil
;
10656 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10657 doc
: /* Alist of charsets vs revision numbers.
10658 While encoding, if a charset (car part of an element) is found,
10659 designate it with the escape sequence identifying revision (cdr part
10660 of the element). */);
10661 Vcharset_revision_table
= Qnil
;
10663 DEFVAR_LISP ("default-process-coding-system",
10664 Vdefault_process_coding_system
,
10665 doc
: /* Cons of coding systems used for process I/O by default.
10666 The car part is used for decoding a process output,
10667 the cdr part is used for encoding a text to be sent to a process. */);
10668 Vdefault_process_coding_system
= Qnil
;
10670 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10672 Table of extra Latin codes in the range 128..159 (inclusive).
10673 This is a vector of length 256.
10674 If Nth element is non-nil, the existence of code N in a file
10675 \(or output of subprocess) doesn't prevent it to be detected as
10676 a coding system of ISO 2022 variant which has a flag
10677 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10678 or reading output of a subprocess.
10679 Only 128th through 159th elements have a meaning. */);
10680 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10682 DEFVAR_LISP ("select-safe-coding-system-function",
10683 Vselect_safe_coding_system_function
,
10685 Function to call to select safe coding system for encoding a text.
10687 If set, this function is called to force a user to select a proper
10688 coding system which can encode the text in the case that a default
10689 coding system used in each operation can't encode the text. The
10690 function should take care that the buffer is not modified while
10691 the coding system is being selected.
10693 The default value is `select-safe-coding-system' (which see). */);
10694 Vselect_safe_coding_system_function
= Qnil
;
10696 DEFVAR_BOOL ("coding-system-require-warning",
10697 coding_system_require_warning
,
10698 doc
: /* Internal use only.
10699 If non-nil, on writing a file, `select-safe-coding-system-function' is
10700 called even if `coding-system-for-write' is non-nil. The command
10701 `universal-coding-system-argument' binds this variable to t temporarily. */);
10702 coding_system_require_warning
= 0;
10705 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10706 inhibit_iso_escape_detection
,
10708 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10710 When Emacs reads text, it tries to detect how the text is encoded.
10711 This code detection is sensitive to escape sequences. If Emacs sees
10712 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10713 of the ISO2022 encodings, and decodes text by the corresponding coding
10714 system (e.g. `iso-2022-7bit').
10716 However, there may be a case that you want to read escape sequences in
10717 a file as is. In such a case, you can set this variable to non-nil.
10718 Then the code detection will ignore any escape sequences, and no text is
10719 detected as encoded in some ISO-2022 encoding. The result is that all
10720 escape sequences become visible in a buffer.
10722 The default value is nil, and it is strongly recommended not to change
10723 it. That is because many Emacs Lisp source files that contain
10724 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10725 in Emacs's distribution, and they won't be decoded correctly on
10726 reading if you suppress escape sequence detection.
10728 The other way to read escape sequences in a file without decoding is
10729 to explicitly specify some coding system that doesn't use ISO-2022
10730 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10731 inhibit_iso_escape_detection
= 0;
10733 DEFVAR_BOOL ("inhibit-null-byte-detection",
10734 inhibit_null_byte_detection
,
10735 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10736 By default, Emacs treats it as binary data, and does not attempt to
10737 decode it. The effect is as if you specified `no-conversion' for
10740 Set this to non-nil when a regular text happens to include null bytes.
10741 Examples are Index nodes of Info files and null-byte delimited output
10742 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10743 decode text as usual. */);
10744 inhibit_null_byte_detection
= 0;
10746 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10747 doc
: /* Char table for translating self-inserting characters.
10748 This is applied to the result of input methods, not their input.
10749 See also `keyboard-translate-table'.
10751 Use of this variable for character code unification was rendered
10752 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10753 internal character representation. */);
10754 Vtranslation_table_for_input
= Qnil
;
10757 Lisp_Object args
[coding_arg_max
];
10758 Lisp_Object plist
[16];
10761 for (i
= 0; i
< coding_arg_max
; i
++)
10764 plist
[0] = intern_c_string (":name");
10765 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10766 plist
[2] = intern_c_string (":mnemonic");
10767 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10768 plist
[4] = intern_c_string (":coding-type");
10769 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10770 plist
[6] = intern_c_string (":ascii-compatible-p");
10771 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10772 plist
[8] = intern_c_string (":default-char");
10773 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10774 plist
[10] = intern_c_string (":for-unibyte");
10775 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10776 plist
[12] = intern_c_string (":docstring");
10777 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10779 When you visit a file with this coding, the file is read into a\n\
10780 unibyte buffer as is, thus each byte of a file is treated as a\n\
10782 plist
[14] = intern_c_string (":eol-type");
10783 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10784 args
[coding_arg_plist
] = Flist (16, plist
);
10785 Fdefine_coding_system_internal (coding_arg_max
, args
);
10787 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10788 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10789 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10790 /* This is already set.
10791 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10792 plist
[8] = intern_c_string (":charset-list");
10793 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10794 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10795 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10796 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10797 args
[coding_arg_plist
] = Flist (16, plist
);
10798 Fdefine_coding_system_internal (coding_arg_max
, args
);
10801 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10806 for (i
= 0; i
< coding_category_max
; i
++)
10807 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10809 #if defined (DOS_NT)
10810 system_eol_type
= Qdos
;
10812 system_eol_type
= Qunix
;
10814 staticpro (&system_eol_type
);
10818 emacs_strerror (int error_number
)
10822 synchronize_system_messages_locale ();
10823 str
= strerror (error_number
);
10825 if (! NILP (Vlocale_coding_system
))
10827 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10828 Vlocale_coding_system
,
10830 str
= SSDATA (dec
);