1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009
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 (coding
, detect_info
)
158 struct coding_system
*coding
;
159 struct coding_detection_info
*detect_info
;
161 const unsigned char *src
= coding
->source
;
162 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
163 int multibytep
= coding
->src_multibyte
;
164 int consumed_chars
= 0;
170 /* Get one byte from the source. If the souce is exausted, jump
171 to no_more_source:. */
174 if (! __C_conforms_to_XXX___ (c
))
176 if (! __C_strongly_suggests_XXX__ (c
))
177 found
= CATEGORY_MASK_XXX
;
179 /* The byte sequence is invalid for XXX. */
180 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
184 /* The source exausted successfully. */
185 detect_info
->found
|= found
;
190 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
192 These functions decode a byte sequence specified as a source by
193 CODING. The resulting multibyte text goes to a place pointed to by
194 CODING->charbuf, the length of which should not exceed
195 CODING->charbuf_size;
197 These functions set the information of original and decoded texts in
198 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
199 They also set CODING->result to one of CODING_RESULT_XXX indicating
200 how the decoding is finished.
202 Below is the template of these functions. */
206 decode_coding_XXXX (coding
)
207 struct coding_system
*coding
;
209 const unsigned char *src
= coding
->source
+ coding
->consumed
;
210 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
211 /* SRC_BASE remembers the start position in source in each loop.
212 The loop will be exited when there's not enough source code, or
213 when there's no room in CHARBUF for a decoded character. */
214 const unsigned char *src_base
;
215 /* A buffer to produce decoded characters. */
216 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
217 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
218 int multibytep
= coding
->src_multibyte
;
223 if (charbuf
< charbuf_end
)
224 /* No more room to produce a decoded character. */
231 if (src_base
< src_end
232 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
233 /* If the source ends by partial bytes to construct a character,
234 treat them as eight-bit raw data. */
235 while (src_base
< src_end
&& charbuf
< charbuf_end
)
236 *charbuf
++ = *src_base
++;
237 /* Remember how many bytes and characters we consumed. If the
238 source is multibyte, the bytes and chars are not identical. */
239 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
240 /* Remember how many characters we produced. */
241 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
245 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
247 These functions encode SRC_BYTES length text at SOURCE of Emacs'
248 internal multibyte format by CODING. The resulting byte sequence
249 goes to a place pointed to by DESTINATION, the length of which
250 should not exceed DST_BYTES.
252 These functions set the information of original and encoded texts in
253 the members produced, produced_char, consumed, and consumed_char of
254 the structure *CODING. They also set the member result to one of
255 CODING_RESULT_XXX indicating how the encoding finished.
257 DST_BYTES zero means that source area and destination area are
258 overlapped, which means that we can produce a encoded text until it
259 reaches at the head of not-yet-encoded source text.
261 Below is a template of these functions. */
264 encode_coding_XXX (coding
)
265 struct coding_system
*coding
;
267 int multibytep
= coding
->dst_multibyte
;
268 int *charbuf
= coding
->charbuf
;
269 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
270 unsigned char *dst
= coding
->destination
+ coding
->produced
;
271 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
272 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
273 int produced_chars
= 0;
275 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
278 /* Encode C into DST, and increment DST. */
280 label_no_more_destination
:
281 /* How many chars and bytes we produced. */
282 coding
->produced_char
+= produced_chars
;
283 coding
->produced
= dst
- coding
->destination
;
288 /*** 1. Preamble ***/
295 #include "character.h"
298 #include "composite.h"
302 #include "termhooks.h"
304 Lisp_Object Vcoding_system_hash_table
;
306 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
307 Lisp_Object Qunix
, Qdos
;
308 extern Lisp_Object Qmac
; /* frame.c */
309 Lisp_Object Qbuffer_file_coding_system
;
310 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
311 Lisp_Object Qdefault_char
;
312 Lisp_Object Qno_conversion
, Qundecided
;
313 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
314 Lisp_Object Qbig
, Qlittle
;
315 Lisp_Object Qcoding_system_history
;
316 Lisp_Object Qvalid_codes
;
317 Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
318 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
319 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
320 Lisp_Object QCascii_compatible_p
;
322 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
323 Lisp_Object Qcall_process
, Qcall_process_region
;
324 Lisp_Object Qstart_process
, Qopen_network_stream
;
325 Lisp_Object Qtarget_idx
;
327 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
328 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
330 extern Lisp_Object Qcompletion_ignore_case
;
332 /* If a symbol has this property, evaluate the value to define the
333 symbol as a coding system. */
334 static Lisp_Object Qcoding_system_define_form
;
336 int coding_system_require_warning
;
338 Lisp_Object Vselect_safe_coding_system_function
;
340 /* Mnemonic string for each format of end-of-line. */
341 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
342 /* Mnemonic string to indicate format of end-of-line is not yet
344 Lisp_Object eol_mnemonic_undecided
;
346 /* Format of end-of-line decided by system. This is Qunix on
347 Unix and Mac, Qdos on DOS/Windows.
348 This has an effect only for external encoding (i.e. for output to
349 file and process), not for in-buffer or Lisp string encoding. */
350 static Lisp_Object system_eol_type
;
354 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
356 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
358 /* Coding system emacs-mule and raw-text are for converting only
359 end-of-line format. */
360 Lisp_Object Qemacs_mule
, Qraw_text
;
361 Lisp_Object Qutf_8_emacs
;
363 /* Coding-systems are handed between Emacs Lisp programs and C internal
364 routines by the following three variables. */
365 /* Coding-system for reading files and receiving data from process. */
366 Lisp_Object Vcoding_system_for_read
;
367 /* Coding-system for writing files and sending data to process. */
368 Lisp_Object Vcoding_system_for_write
;
369 /* Coding-system actually used in the latest I/O. */
370 Lisp_Object Vlast_coding_system_used
;
371 /* Set to non-nil when an error is detected while code conversion. */
372 Lisp_Object Vlast_code_conversion_error
;
373 /* A vector of length 256 which contains information about special
374 Latin codes (especially for dealing with Microsoft codes). */
375 Lisp_Object Vlatin_extra_code_table
;
377 /* Flag to inhibit code conversion of end-of-line format. */
378 int inhibit_eol_conversion
;
380 /* Flag to inhibit ISO2022 escape sequence detection. */
381 int inhibit_iso_escape_detection
;
383 /* Flag to inhibit detection of binary files through null bytes. */
384 int inhibit_null_byte_detection
;
386 /* Flag to make buffer-file-coding-system inherit from process-coding. */
387 int inherit_process_coding_system
;
389 /* Coding system to be used to encode text for terminal display when
390 terminal coding system is nil. */
391 struct coding_system safe_terminal_coding
;
393 Lisp_Object Vfile_coding_system_alist
;
394 Lisp_Object Vprocess_coding_system_alist
;
395 Lisp_Object Vnetwork_coding_system_alist
;
397 Lisp_Object Vlocale_coding_system
;
401 /* Flag to tell if we look up translation table on character code
403 Lisp_Object Venable_character_translation
;
404 /* Standard translation table to look up on decoding (reading). */
405 Lisp_Object Vstandard_translation_table_for_decode
;
406 /* Standard translation table to look up on encoding (writing). */
407 Lisp_Object Vstandard_translation_table_for_encode
;
409 Lisp_Object Qtranslation_table
;
410 Lisp_Object Qtranslation_table_id
;
411 Lisp_Object Qtranslation_table_for_decode
;
412 Lisp_Object Qtranslation_table_for_encode
;
414 /* Alist of charsets vs revision number. */
415 static Lisp_Object Vcharset_revision_table
;
417 /* Default coding systems used for process I/O. */
418 Lisp_Object Vdefault_process_coding_system
;
420 /* Char table for translating Quail and self-inserting input. */
421 Lisp_Object Vtranslation_table_for_input
;
423 /* Two special coding systems. */
424 Lisp_Object Vsjis_coding_system
;
425 Lisp_Object Vbig5_coding_system
;
427 /* ISO2022 section */
429 #define CODING_ISO_INITIAL(coding, reg) \
430 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
431 coding_attr_iso_initial), \
435 #define CODING_ISO_REQUEST(coding, charset_id) \
436 ((charset_id <= (coding)->max_charset_id \
437 ? (coding)->safe_charsets[charset_id] \
441 #define CODING_ISO_FLAGS(coding) \
442 ((coding)->spec.iso_2022.flags)
443 #define CODING_ISO_DESIGNATION(coding, reg) \
444 ((coding)->spec.iso_2022.current_designation[reg])
445 #define CODING_ISO_INVOCATION(coding, plane) \
446 ((coding)->spec.iso_2022.current_invocation[plane])
447 #define CODING_ISO_SINGLE_SHIFTING(coding) \
448 ((coding)->spec.iso_2022.single_shifting)
449 #define CODING_ISO_BOL(coding) \
450 ((coding)->spec.iso_2022.bol)
451 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
452 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
454 /* Control characters of ISO2022. */
455 /* code */ /* function */
456 #define ISO_CODE_LF 0x0A /* line-feed */
457 #define ISO_CODE_CR 0x0D /* carriage-return */
458 #define ISO_CODE_SO 0x0E /* shift-out */
459 #define ISO_CODE_SI 0x0F /* shift-in */
460 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
461 #define ISO_CODE_ESC 0x1B /* escape */
462 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
463 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
464 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
466 /* All code (1-byte) of ISO2022 is classified into one of the
468 enum iso_code_class_type
470 ISO_control_0
, /* Control codes in the range
471 0x00..0x1F and 0x7F, except for the
472 following 5 codes. */
473 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
474 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
475 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
476 ISO_escape
, /* ISO_CODE_SO (0x1B) */
477 ISO_control_1
, /* Control codes in the range
478 0x80..0x9F, except for the
479 following 3 codes. */
480 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
481 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
482 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
483 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
484 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
485 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
486 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
489 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
490 `iso-flags' attribute of an iso2022 coding system. */
492 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
493 instead of the correct short-form sequence (e.g. ESC $ A). */
494 #define CODING_ISO_FLAG_LONG_FORM 0x0001
496 /* If set, reset graphic planes and registers at end-of-line to the
498 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
500 /* If set, reset graphic planes and registers before any control
501 characters to the initial state. */
502 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
504 /* If set, encode by 7-bit environment. */
505 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
507 /* If set, use locking-shift function. */
508 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
510 /* If set, use single-shift function. Overwrite
511 CODING_ISO_FLAG_LOCKING_SHIFT. */
512 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
514 /* If set, use designation escape sequence. */
515 #define CODING_ISO_FLAG_DESIGNATION 0x0040
517 /* If set, produce revision number sequence. */
518 #define CODING_ISO_FLAG_REVISION 0x0080
520 /* If set, produce ISO6429's direction specifying sequence. */
521 #define CODING_ISO_FLAG_DIRECTION 0x0100
523 /* If set, assume designation states are reset at beginning of line on
525 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
527 /* If set, designation sequence should be placed at beginning of line
529 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
531 /* If set, do not encode unsafe charactes on output. */
532 #define CODING_ISO_FLAG_SAFE 0x0800
534 /* If set, extra latin codes (128..159) are accepted as a valid code
536 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
538 #define CODING_ISO_FLAG_COMPOSITION 0x2000
540 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
542 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
544 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
546 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
548 /* A character to be produced on output if encoding of the original
549 character is prohibited by CODING_ISO_FLAG_SAFE. */
550 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
553 #define CODING_UTF_8_BOM(coding) \
554 ((coding)->spec.utf_8_bom)
557 #define CODING_UTF_16_BOM(coding) \
558 ((coding)->spec.utf_16.bom)
560 #define CODING_UTF_16_ENDIAN(coding) \
561 ((coding)->spec.utf_16.endian)
563 #define CODING_UTF_16_SURROGATE(coding) \
564 ((coding)->spec.utf_16.surrogate)
568 #define CODING_CCL_DECODER(coding) \
569 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
570 #define CODING_CCL_ENCODER(coding) \
571 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
572 #define CODING_CCL_VALIDS(coding) \
573 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
575 /* Index for each coding category in `coding_categories' */
579 coding_category_iso_7
,
580 coding_category_iso_7_tight
,
581 coding_category_iso_8_1
,
582 coding_category_iso_8_2
,
583 coding_category_iso_7_else
,
584 coding_category_iso_8_else
,
585 coding_category_utf_8_auto
,
586 coding_category_utf_8_nosig
,
587 coding_category_utf_8_sig
,
588 coding_category_utf_16_auto
,
589 coding_category_utf_16_be
,
590 coding_category_utf_16_le
,
591 coding_category_utf_16_be_nosig
,
592 coding_category_utf_16_le_nosig
,
593 coding_category_charset
,
594 coding_category_sjis
,
595 coding_category_big5
,
597 coding_category_emacs_mule
,
598 /* All above are targets of code detection. */
599 coding_category_raw_text
,
600 coding_category_undecided
,
604 /* Definitions of flag bits used in detect_coding_XXXX. */
605 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
606 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
607 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
608 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
609 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
610 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
611 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
612 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
613 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
614 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
615 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
616 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
617 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
618 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
619 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
620 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
621 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
622 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
623 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
624 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
626 /* This value is returned if detect_coding_mask () find nothing other
627 than ASCII characters. */
628 #define CATEGORY_MASK_ANY \
629 (CATEGORY_MASK_ISO_7 \
630 | CATEGORY_MASK_ISO_7_TIGHT \
631 | CATEGORY_MASK_ISO_8_1 \
632 | CATEGORY_MASK_ISO_8_2 \
633 | CATEGORY_MASK_ISO_7_ELSE \
634 | CATEGORY_MASK_ISO_8_ELSE \
635 | CATEGORY_MASK_UTF_8_AUTO \
636 | CATEGORY_MASK_UTF_8_NOSIG \
637 | CATEGORY_MASK_UTF_8_SIG \
638 | CATEGORY_MASK_UTF_16_AUTO \
639 | CATEGORY_MASK_UTF_16_BE \
640 | CATEGORY_MASK_UTF_16_LE \
641 | CATEGORY_MASK_UTF_16_BE_NOSIG \
642 | CATEGORY_MASK_UTF_16_LE_NOSIG \
643 | CATEGORY_MASK_CHARSET \
644 | CATEGORY_MASK_SJIS \
645 | CATEGORY_MASK_BIG5 \
646 | CATEGORY_MASK_CCL \
647 | CATEGORY_MASK_EMACS_MULE)
650 #define CATEGORY_MASK_ISO_7BIT \
651 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
653 #define CATEGORY_MASK_ISO_8BIT \
654 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
656 #define CATEGORY_MASK_ISO_ELSE \
657 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
659 #define CATEGORY_MASK_ISO_ESCAPE \
660 (CATEGORY_MASK_ISO_7 \
661 | CATEGORY_MASK_ISO_7_TIGHT \
662 | CATEGORY_MASK_ISO_7_ELSE \
663 | CATEGORY_MASK_ISO_8_ELSE)
665 #define CATEGORY_MASK_ISO \
666 ( CATEGORY_MASK_ISO_7BIT \
667 | CATEGORY_MASK_ISO_8BIT \
668 | CATEGORY_MASK_ISO_ELSE)
670 #define CATEGORY_MASK_UTF_16 \
671 (CATEGORY_MASK_UTF_16_AUTO \
672 | CATEGORY_MASK_UTF_16_BE \
673 | CATEGORY_MASK_UTF_16_LE \
674 | CATEGORY_MASK_UTF_16_BE_NOSIG \
675 | CATEGORY_MASK_UTF_16_LE_NOSIG)
677 #define CATEGORY_MASK_UTF_8 \
678 (CATEGORY_MASK_UTF_8_AUTO \
679 | CATEGORY_MASK_UTF_8_NOSIG \
680 | CATEGORY_MASK_UTF_8_SIG)
682 /* List of symbols `coding-category-xxx' ordered by priority. This
683 variable is exposed to Emacs Lisp. */
684 static Lisp_Object Vcoding_category_list
;
686 /* Table of coding categories (Lisp symbols). This variable is for
688 static Lisp_Object Vcoding_category_table
;
690 /* Table of coding-categories ordered by priority. */
691 static enum coding_category coding_priorities
[coding_category_max
];
693 /* Nth element is a coding context for the coding system bound to the
694 Nth coding category. */
695 static struct coding_system coding_categories
[coding_category_max
];
697 /*** Commonly used macros and functions ***/
700 #define min(a, b) ((a) < (b) ? (a) : (b))
703 #define max(a, b) ((a) > (b) ? (a) : (b))
706 #define CODING_GET_INFO(coding, attrs, charset_list) \
708 (attrs) = CODING_ID_ATTRS ((coding)->id); \
709 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
713 /* Safely get one byte from the source text pointed by SRC which ends
714 at SRC_END, and set C to that byte. If there are not enough bytes
715 in the source, it jumps to `no_more_source'. If multibytep is
716 nonzero, and a multibyte character is found at SRC, set C to the
717 negative value of the character code. The caller should declare
718 and set these variables appropriately in advance:
719 src, src_end, multibytep */
721 #define ONE_MORE_BYTE(c) \
723 if (src == src_end) \
725 if (src_base < src) \
726 record_conversion_result \
727 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
728 goto no_more_source; \
731 if (multibytep && (c & 0x80)) \
733 if ((c & 0xFE) == 0xC0) \
734 c = ((c & 1) << 6) | *src++; \
738 c = - string_char (src, &src, NULL); \
739 record_conversion_result \
740 (coding, CODING_RESULT_INVALID_SRC); \
746 /* Safely get two bytes from the source text pointed by SRC which ends
747 at SRC_END, and set C1 and C2 to those bytes while skipping the
748 heading multibyte characters. If there are not enough bytes in the
749 source, it jumps to `no_more_source'. If multibytep is nonzero and
750 a multibyte character is found for C2, set C2 to the negative value
751 of the character code. The caller should declare and set these
752 variables appropriately in advance:
753 src, src_end, multibytep
754 It is intended that this macro is used in detect_coding_utf_16. */
756 #define TWO_MORE_BYTES(c1, c2) \
759 if (src == src_end) \
760 goto no_more_source; \
762 if (multibytep && (c1 & 0x80)) \
764 if ((c1 & 0xFE) == 0xC0) \
765 c1 = ((c1 & 1) << 6) | *src++; \
768 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
773 if (src == src_end) \
774 goto no_more_source; \
776 if (multibytep && (c2 & 0x80)) \
778 if ((c2 & 0xFE) == 0xC0) \
779 c2 = ((c2 & 1) << 6) | *src++; \
786 #define ONE_MORE_BYTE_NO_CHECK(c) \
789 if (multibytep && (c & 0x80)) \
791 if ((c & 0xFE) == 0xC0) \
792 c = ((c & 1) << 6) | *src++; \
796 c = - string_char (src, &src, NULL); \
797 record_conversion_result \
798 (coding, CODING_RESULT_INVALID_SRC); \
805 /* Store a byte C in the place pointed by DST and increment DST to the
806 next free point, and increment PRODUCED_CHARS. The caller should
807 assure that C is 0..127, and declare and set the variable `dst'
808 appropriately in advance.
812 #define EMIT_ONE_ASCII_BYTE(c) \
819 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
821 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
823 produced_chars += 2; \
824 *dst++ = (c1), *dst++ = (c2); \
828 /* Store a byte C in the place pointed by DST and increment DST to the
829 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
830 nonzero, store in an appropriate multibyte from. The caller should
831 declare and set the variables `dst' and `multibytep' appropriately
834 #define EMIT_ONE_BYTE(c) \
841 ch = BYTE8_TO_CHAR (ch); \
842 CHAR_STRING_ADVANCE (ch, dst); \
849 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
851 #define EMIT_TWO_BYTES(c1, c2) \
853 produced_chars += 2; \
860 ch = BYTE8_TO_CHAR (ch); \
861 CHAR_STRING_ADVANCE (ch, dst); \
864 ch = BYTE8_TO_CHAR (ch); \
865 CHAR_STRING_ADVANCE (ch, dst); \
875 #define EMIT_THREE_BYTES(c1, c2, c3) \
877 EMIT_ONE_BYTE (c1); \
878 EMIT_TWO_BYTES (c2, c3); \
882 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
884 EMIT_TWO_BYTES (c1, c2); \
885 EMIT_TWO_BYTES (c3, c4); \
889 /* Prototypes for static functions. */
890 static void record_conversion_result
P_ ((struct coding_system
*coding
,
891 enum coding_result_code result
));
892 static int detect_coding_utf_8
P_ ((struct coding_system
*,
893 struct coding_detection_info
*info
));
894 static void decode_coding_utf_8
P_ ((struct coding_system
*));
895 static int encode_coding_utf_8
P_ ((struct coding_system
*));
897 static int detect_coding_utf_16
P_ ((struct coding_system
*,
898 struct coding_detection_info
*info
));
899 static void decode_coding_utf_16
P_ ((struct coding_system
*));
900 static int encode_coding_utf_16
P_ ((struct coding_system
*));
902 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
903 struct coding_detection_info
*info
));
904 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
905 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
907 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
908 struct coding_detection_info
*info
));
909 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
910 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
912 static int detect_coding_sjis
P_ ((struct coding_system
*,
913 struct coding_detection_info
*info
));
914 static void decode_coding_sjis
P_ ((struct coding_system
*));
915 static int encode_coding_sjis
P_ ((struct coding_system
*));
917 static int detect_coding_big5
P_ ((struct coding_system
*,
918 struct coding_detection_info
*info
));
919 static void decode_coding_big5
P_ ((struct coding_system
*));
920 static int encode_coding_big5
P_ ((struct coding_system
*));
922 static int detect_coding_ccl
P_ ((struct coding_system
*,
923 struct coding_detection_info
*info
));
924 static void decode_coding_ccl
P_ ((struct coding_system
*));
925 static int encode_coding_ccl
P_ ((struct coding_system
*));
927 static void decode_coding_raw_text
P_ ((struct coding_system
*));
928 static int encode_coding_raw_text
P_ ((struct coding_system
*));
930 static void coding_set_source
P_ ((struct coding_system
*));
931 static void coding_set_destination
P_ ((struct coding_system
*));
932 static void coding_alloc_by_realloc
P_ ((struct coding_system
*, EMACS_INT
));
933 static void coding_alloc_by_making_gap
P_ ((struct coding_system
*,
934 EMACS_INT
, EMACS_INT
));
935 static unsigned char *alloc_destination
P_ ((struct coding_system
*,
936 EMACS_INT
, unsigned char *));
937 static void setup_iso_safe_charsets
P_ ((Lisp_Object
));
938 static unsigned char *encode_designation_at_bol
P_ ((struct coding_system
*,
941 static int detect_eol
P_ ((const unsigned char *,
942 EMACS_INT
, enum coding_category
));
943 static Lisp_Object adjust_coding_eol_type
P_ ((struct coding_system
*, int));
944 static void decode_eol
P_ ((struct coding_system
*));
945 static Lisp_Object get_translation_table
P_ ((Lisp_Object
, int, int *));
946 static Lisp_Object get_translation
P_ ((Lisp_Object
, int *, int *,
948 static int produce_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
949 static INLINE
void produce_composition
P_ ((struct coding_system
*, int *,
951 static INLINE
void produce_charset
P_ ((struct coding_system
*, int *,
953 static void produce_annotation
P_ ((struct coding_system
*, EMACS_INT
));
954 static int decode_coding
P_ ((struct coding_system
*));
955 static INLINE
int *handle_composition_annotation
P_ ((EMACS_INT
, EMACS_INT
,
956 struct coding_system
*,
957 int *, EMACS_INT
*));
958 static INLINE
int *handle_charset_annotation
P_ ((EMACS_INT
, EMACS_INT
,
959 struct coding_system
*,
960 int *, EMACS_INT
*));
961 static void consume_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
962 static int encode_coding
P_ ((struct coding_system
*));
963 static Lisp_Object make_conversion_work_buffer
P_ ((int));
964 static Lisp_Object code_conversion_restore
P_ ((Lisp_Object
));
965 static INLINE
int char_encodable_p
P_ ((int, Lisp_Object
));
966 static Lisp_Object make_subsidiaries
P_ ((Lisp_Object
));
969 record_conversion_result (struct coding_system
*coding
,
970 enum coding_result_code result
)
972 coding
->result
= result
;
975 case CODING_RESULT_INSUFFICIENT_SRC
:
976 Vlast_code_conversion_error
= Qinsufficient_source
;
978 case CODING_RESULT_INCONSISTENT_EOL
:
979 Vlast_code_conversion_error
= Qinconsistent_eol
;
981 case CODING_RESULT_INVALID_SRC
:
982 Vlast_code_conversion_error
= Qinvalid_source
;
984 case CODING_RESULT_INTERRUPT
:
985 Vlast_code_conversion_error
= Qinterrupted
;
987 case CODING_RESULT_INSUFFICIENT_MEM
:
988 Vlast_code_conversion_error
= Qinsufficient_memory
;
991 Vlast_code_conversion_error
= intern ("Unknown error");
995 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
997 charset_map_loaded = 0; \
998 c = DECODE_CHAR (charset, code); \
999 if (charset_map_loaded) \
1001 const unsigned char *orig = coding->source; \
1004 coding_set_source (coding); \
1005 offset = coding->source - orig; \
1007 src_base += offset; \
1008 src_end += offset; \
1013 /* If there are at least BYTES length of room at dst, allocate memory
1014 for coding->destination and update dst and dst_end. We don't have
1015 to take care of coding->source which will be relocated. It is
1016 handled by calling coding_set_source in encode_coding. */
1018 #define ASSURE_DESTINATION(bytes) \
1020 if (dst + (bytes) >= dst_end) \
1022 int more_bytes = charbuf_end - charbuf + (bytes); \
1024 dst = alloc_destination (coding, more_bytes, dst); \
1025 dst_end = coding->destination + coding->dst_bytes; \
1030 /* Store multibyte form of the character C in P, and advance P to the
1031 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
1032 never calls MAYBE_UNIFY_CHAR. */
1034 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
1036 if ((c) <= MAX_1_BYTE_CHAR) \
1038 else if ((c) <= MAX_2_BYTE_CHAR) \
1039 *(p)++ = (0xC0 | ((c) >> 6)), \
1040 *(p)++ = (0x80 | ((c) & 0x3F)); \
1041 else if ((c) <= MAX_3_BYTE_CHAR) \
1042 *(p)++ = (0xE0 | ((c) >> 12)), \
1043 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
1044 *(p)++ = (0x80 | ((c) & 0x3F)); \
1045 else if ((c) <= MAX_4_BYTE_CHAR) \
1046 *(p)++ = (0xF0 | (c >> 18)), \
1047 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1048 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1049 *(p)++ = (0x80 | (c & 0x3F)); \
1050 else if ((c) <= MAX_5_BYTE_CHAR) \
1052 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
1053 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1054 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1055 *(p)++ = (0x80 | (c & 0x3F)); \
1057 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1061 /* Return the character code of character whose multibyte form is at
1062 P, and advance P to the end of the multibyte form. This is like
1063 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1065 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1068 : ! ((p)[0] & 0x20) \
1070 ((((p)[-2] & 0x1F) << 6) \
1071 | ((p)[-1] & 0x3F) \
1072 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1073 : ! ((p)[0] & 0x10) \
1075 ((((p)[-3] & 0x0F) << 12) \
1076 | (((p)[-2] & 0x3F) << 6) \
1077 | ((p)[-1] & 0x3F))) \
1078 : ! ((p)[0] & 0x08) \
1080 ((((p)[-4] & 0xF) << 18) \
1081 | (((p)[-3] & 0x3F) << 12) \
1082 | (((p)[-2] & 0x3F) << 6) \
1083 | ((p)[-1] & 0x3F))) \
1085 ((((p)[-4] & 0x3F) << 18) \
1086 | (((p)[-3] & 0x3F) << 12) \
1087 | (((p)[-2] & 0x3F) << 6) \
1088 | ((p)[-1] & 0x3F))))
1092 coding_set_source (coding
)
1093 struct coding_system
*coding
;
1095 if (BUFFERP (coding
->src_object
))
1097 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1099 if (coding
->src_pos
< 0)
1100 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1102 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1104 else if (STRINGP (coding
->src_object
))
1106 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1109 /* Otherwise, the source is C string and is never relocated
1110 automatically. Thus we don't have to update anything. */
1115 coding_set_destination (coding
)
1116 struct coding_system
*coding
;
1118 if (BUFFERP (coding
->dst_object
))
1120 if (coding
->src_pos
< 0)
1122 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1123 coding
->dst_bytes
= (GAP_END_ADDR
1124 - (coding
->src_bytes
- coding
->consumed
)
1125 - coding
->destination
);
1129 /* We are sure that coding->dst_pos_byte is before the gap
1131 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1132 + coding
->dst_pos_byte
- BEG_BYTE
);
1133 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1134 - coding
->destination
);
1138 /* Otherwise, the destination is C string and is never relocated
1139 automatically. Thus we don't have to update anything. */
1145 coding_alloc_by_realloc (coding
, bytes
)
1146 struct coding_system
*coding
;
1149 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1150 coding
->dst_bytes
+ bytes
);
1151 coding
->dst_bytes
+= bytes
;
1155 coding_alloc_by_making_gap (coding
, gap_head_used
, bytes
)
1156 struct coding_system
*coding
;
1157 EMACS_INT gap_head_used
, bytes
;
1159 if (EQ (coding
->src_object
, coding
->dst_object
))
1161 /* The gap may contain the produced data at the head and not-yet
1162 consumed data at the tail. To preserve those data, we at
1163 first make the gap size to zero, then increase the gap
1165 EMACS_INT add
= GAP_SIZE
;
1167 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1168 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1170 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1171 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1175 Lisp_Object this_buffer
;
1177 this_buffer
= Fcurrent_buffer ();
1178 set_buffer_internal (XBUFFER (coding
->dst_object
));
1180 set_buffer_internal (XBUFFER (this_buffer
));
1185 static unsigned char *
1186 alloc_destination (coding
, nbytes
, dst
)
1187 struct coding_system
*coding
;
1191 EMACS_INT offset
= dst
- coding
->destination
;
1193 if (BUFFERP (coding
->dst_object
))
1195 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1197 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1200 coding_alloc_by_realloc (coding
, nbytes
);
1201 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1202 coding_set_destination (coding
);
1203 dst
= coding
->destination
+ offset
;
1207 /** Macros for annotations. */
1209 /* Maximum length of annotation data (sum of annotations for
1210 composition and charset). */
1211 #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1213 /* An annotation data is stored in the array coding->charbuf in this
1215 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1216 LENGTH is the number of elements in the annotation.
1217 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1218 NCHARS is the number of characters in the text annotated.
1220 The format of the following elements depend on ANNOTATION_MASK.
1222 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1224 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1225 METHOD is one of enum composition_method.
1226 Optionnal COMPOSITION-COMPONENTS are characters and composition
1229 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1232 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1234 *(buf)++ = -(len); \
1235 *(buf)++ = (mask); \
1236 *(buf)++ = (nchars); \
1237 coding->annotated = 1; \
1240 #define ADD_COMPOSITION_DATA(buf, nchars, method) \
1242 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1247 #define ADD_CHARSET_DATA(buf, nchars, id) \
1249 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1254 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1261 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1262 Check if a text is encoded in UTF-8. If it is, return 1, else
1265 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1266 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1267 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1268 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1269 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1270 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1272 #define UTF_BOM 0xFEFF
1273 #define UTF_8_BOM_1 0xEF
1274 #define UTF_8_BOM_2 0xBB
1275 #define UTF_8_BOM_3 0xBF
1278 detect_coding_utf_8 (coding
, detect_info
)
1279 struct coding_system
*coding
;
1280 struct coding_detection_info
*detect_info
;
1282 const unsigned char *src
= coding
->source
, *src_base
;
1283 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1284 int multibytep
= coding
->src_multibyte
;
1285 int consumed_chars
= 0;
1289 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1290 /* A coding system of this category is always ASCII compatible. */
1291 src
+= coding
->head_ascii
;
1295 int c
, c1
, c2
, c3
, c4
;
1299 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1302 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1304 if (UTF_8_2_OCTET_LEADING_P (c
))
1310 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1312 if (UTF_8_3_OCTET_LEADING_P (c
))
1315 if (src_base
== coding
->source
1316 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1321 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1323 if (UTF_8_4_OCTET_LEADING_P (c
))
1329 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1331 if (UTF_8_5_OCTET_LEADING_P (c
))
1338 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1342 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1344 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1349 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1350 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1354 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1356 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1363 decode_coding_utf_8 (coding
)
1364 struct coding_system
*coding
;
1366 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1367 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1368 const unsigned char *src_base
;
1369 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1370 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1371 int consumed_chars
= 0, consumed_chars_base
= 0;
1372 int multibytep
= coding
->src_multibyte
;
1373 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1374 Lisp_Object attr
, charset_list
;
1375 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1376 int byte_after_cr
= -1;
1378 CODING_GET_INFO (coding
, attr
, charset_list
);
1380 if (bom
!= utf_without_bom
)
1386 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1391 if (! UTF_8_EXTRA_OCTET_P (c2
))
1396 if (! UTF_8_EXTRA_OCTET_P (c3
))
1400 if ((c1
!= UTF_8_BOM_1
)
1401 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1404 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1409 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1415 int c
, c1
, c2
, c3
, c4
, c5
;
1418 consumed_chars_base
= consumed_chars
;
1420 if (charbuf
>= charbuf_end
)
1422 if (byte_after_cr
>= 0)
1427 if (byte_after_cr
>= 0)
1428 c1
= byte_after_cr
, byte_after_cr
= -1;
1435 else if (UTF_8_1_OCTET_P(c1
))
1437 if (eol_crlf
&& c1
== '\r')
1438 ONE_MORE_BYTE (byte_after_cr
);
1444 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1446 if (UTF_8_2_OCTET_LEADING_P (c1
))
1448 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1449 /* Reject overlong sequences here and below. Encoders
1450 producing them are incorrect, they can be misleading,
1451 and they mess up read/write invariance. */
1458 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1460 if (UTF_8_3_OCTET_LEADING_P (c1
))
1462 c
= (((c1
& 0xF) << 12)
1463 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1465 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1471 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1473 if (UTF_8_4_OCTET_LEADING_P (c1
))
1475 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1476 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1483 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1485 if (UTF_8_5_OCTET_LEADING_P (c1
))
1487 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1488 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1490 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1505 consumed_chars
= consumed_chars_base
;
1507 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1512 coding
->consumed_char
+= consumed_chars_base
;
1513 coding
->consumed
= src_base
- coding
->source
;
1514 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1519 encode_coding_utf_8 (coding
)
1520 struct coding_system
*coding
;
1522 int multibytep
= coding
->dst_multibyte
;
1523 int *charbuf
= coding
->charbuf
;
1524 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1525 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1526 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1527 int produced_chars
= 0;
1530 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1532 ASSURE_DESTINATION (3);
1533 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1534 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1539 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1541 while (charbuf
< charbuf_end
)
1543 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1545 ASSURE_DESTINATION (safe_room
);
1547 if (CHAR_BYTE8_P (c
))
1549 c
= CHAR_TO_BYTE8 (c
);
1554 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1555 for (p
= str
; p
< pend
; p
++)
1562 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1564 while (charbuf
< charbuf_end
)
1566 ASSURE_DESTINATION (safe_room
);
1568 if (CHAR_BYTE8_P (c
))
1569 *dst
++ = CHAR_TO_BYTE8 (c
);
1571 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1575 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1576 coding
->produced_char
+= produced_chars
;
1577 coding
->produced
= dst
- coding
->destination
;
1582 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1583 Check if a text is encoded in one of UTF-16 based coding systems.
1584 If it is, return 1, else return 0. */
1586 #define UTF_16_HIGH_SURROGATE_P(val) \
1587 (((val) & 0xFC00) == 0xD800)
1589 #define UTF_16_LOW_SURROGATE_P(val) \
1590 (((val) & 0xFC00) == 0xDC00)
1592 #define UTF_16_INVALID_P(val) \
1593 (((val) == 0xFFFE) \
1594 || ((val) == 0xFFFF) \
1595 || UTF_16_LOW_SURROGATE_P (val))
1599 detect_coding_utf_16 (coding
, detect_info
)
1600 struct coding_system
*coding
;
1601 struct coding_detection_info
*detect_info
;
1603 const unsigned char *src
= coding
->source
, *src_base
= src
;
1604 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1605 int multibytep
= coding
->src_multibyte
;
1606 int consumed_chars
= 0;
1609 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1610 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1611 && (coding
->src_chars
& 1))
1613 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1617 TWO_MORE_BYTES (c1
, c2
);
1618 if ((c1
== 0xFF) && (c2
== 0xFE))
1620 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1621 | CATEGORY_MASK_UTF_16_AUTO
);
1622 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1623 | CATEGORY_MASK_UTF_16_BE_NOSIG
1624 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1626 else if ((c1
== 0xFE) && (c2
== 0xFF))
1628 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1629 | CATEGORY_MASK_UTF_16_AUTO
);
1630 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1631 | CATEGORY_MASK_UTF_16_BE_NOSIG
1632 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1636 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1641 /* We check the dispersion of Eth and Oth bytes where E is even and
1642 O is odd. If both are high, we assume binary data.*/
1643 unsigned char e
[256], o
[256];
1644 unsigned e_num
= 1, o_num
= 1;
1651 detect_info
->rejected
1652 |= (CATEGORY_MASK_UTF_16_BE
| CATEGORY_MASK_UTF_16_LE
);
1656 TWO_MORE_BYTES (c1
, c2
);
1674 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1683 decode_coding_utf_16 (coding
)
1684 struct coding_system
*coding
;
1686 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1687 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1688 const unsigned char *src_base
;
1689 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1690 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1691 int consumed_chars
= 0, consumed_chars_base
= 0;
1692 int multibytep
= coding
->src_multibyte
;
1693 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1694 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1695 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1696 Lisp_Object attr
, charset_list
;
1697 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1698 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1700 CODING_GET_INFO (coding
, attr
, charset_list
);
1702 if (bom
== utf_with_bom
)
1711 if (endian
== utf_16_big_endian
1712 ? c
!= 0xFEFF : c
!= 0xFFFE)
1714 /* The first two bytes are not BOM. Treat them as bytes
1715 for a normal character. */
1719 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1721 else if (bom
== utf_detect_bom
)
1723 /* We have already tried to detect BOM and failed in
1725 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1733 consumed_chars_base
= consumed_chars
;
1735 if (charbuf
+ 2 >= charbuf_end
)
1737 if (byte_after_cr1
>= 0)
1742 if (byte_after_cr1
>= 0)
1743 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1751 if (byte_after_cr2
>= 0)
1752 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1757 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1761 c
= (endian
== utf_16_big_endian
1762 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1766 if (! UTF_16_LOW_SURROGATE_P (c
))
1768 if (endian
== utf_16_big_endian
)
1769 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1771 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1775 if (UTF_16_HIGH_SURROGATE_P (c
))
1776 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1782 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1783 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1784 *charbuf
++ = 0x10000 + c
;
1789 if (UTF_16_HIGH_SURROGATE_P (c
))
1790 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1793 if (eol_crlf
&& c
== '\r')
1795 ONE_MORE_BYTE (byte_after_cr1
);
1796 ONE_MORE_BYTE (byte_after_cr2
);
1804 coding
->consumed_char
+= consumed_chars_base
;
1805 coding
->consumed
= src_base
- coding
->source
;
1806 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1810 encode_coding_utf_16 (coding
)
1811 struct coding_system
*coding
;
1813 int multibytep
= coding
->dst_multibyte
;
1814 int *charbuf
= coding
->charbuf
;
1815 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1816 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1817 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1819 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1820 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1821 int produced_chars
= 0;
1822 Lisp_Object attrs
, charset_list
;
1825 CODING_GET_INFO (coding
, attrs
, charset_list
);
1827 if (bom
!= utf_without_bom
)
1829 ASSURE_DESTINATION (safe_room
);
1831 EMIT_TWO_BYTES (0xFE, 0xFF);
1833 EMIT_TWO_BYTES (0xFF, 0xFE);
1834 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1837 while (charbuf
< charbuf_end
)
1839 ASSURE_DESTINATION (safe_room
);
1841 if (c
>= MAX_UNICODE_CHAR
)
1842 c
= coding
->default_char
;
1847 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1849 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1856 c1
= (c
>> 10) + 0xD800;
1857 c2
= (c
& 0x3FF) + 0xDC00;
1859 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1861 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1864 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1865 coding
->produced
= dst
- coding
->destination
;
1866 coding
->produced_char
+= produced_chars
;
1871 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1873 /* Emacs' internal format for representation of multiple character
1874 sets is a kind of multi-byte encoding, i.e. characters are
1875 represented by variable-length sequences of one-byte codes.
1877 ASCII characters and control characters (e.g. `tab', `newline') are
1878 represented by one-byte sequences which are their ASCII codes, in
1879 the range 0x00 through 0x7F.
1881 8-bit characters of the range 0x80..0x9F are represented by
1882 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1885 8-bit characters of the range 0xA0..0xFF are represented by
1886 one-byte sequences which are their 8-bit code.
1888 The other characters are represented by a sequence of `base
1889 leading-code', optional `extended leading-code', and one or two
1890 `position-code's. The length of the sequence is determined by the
1891 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1892 whereas extended leading-code and position-code take the range 0xA0
1893 through 0xFF. See `charset.h' for more details about leading-code
1896 --- CODE RANGE of Emacs' internal format ---
1900 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1901 eight-bit-graphic 0xA0..0xBF
1902 ELSE 0x81..0x9D + [0xA0..0xFF]+
1903 ---------------------------------------------
1905 As this is the internal character representation, the format is
1906 usually not used externally (i.e. in a file or in a data sent to a
1907 process). But, it is possible to have a text externally in this
1908 format (i.e. by encoding by the coding system `emacs-mule').
1910 In that case, a sequence of one-byte codes has a slightly different
1913 At first, all characters in eight-bit-control are represented by
1914 one-byte sequences which are their 8-bit code.
1916 Next, character composition data are represented by the byte
1917 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1919 METHOD is 0xF0 plus one of composition method (enum
1920 composition_method),
1922 BYTES is 0xA0 plus a byte length of this composition data,
1924 CHARS is 0x20 plus a number of characters composed by this
1927 COMPONENTs are characters of multibye form or composition
1928 rules encoded by two-byte of ASCII codes.
1930 In addition, for backward compatibility, the following formats are
1931 also recognized as composition data on decoding.
1934 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1937 MSEQ is a multibyte form but in these special format:
1938 ASCII: 0xA0 ASCII_CODE+0x80,
1939 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1940 RULE is a one byte code of the range 0xA0..0xF0 that
1941 represents a composition rule.
1944 char emacs_mule_bytes
[256];
1947 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
)
1948 struct coding_system
*coding
;
1949 const unsigned char *src
;
1950 int *nbytes
, *nchars
, *id
;
1952 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1953 const unsigned char *src_base
= src
;
1954 int multibytep
= coding
->src_multibyte
;
1955 struct charset
*charset
;
1958 int consumed_chars
= 0;
1964 charset
= emacs_mule_charset
[0];
1970 /* Old style component character of a composition. */
1980 switch (emacs_mule_bytes
[c
])
1983 if (! (charset
= emacs_mule_charset
[c
]))
1992 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1993 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1996 if (c
< 0xA0 || ! (charset
= emacs_mule_charset
[c
]))
2005 if (! (charset
= emacs_mule_charset
[c
]))
2010 code
= (c
& 0x7F) << 8;
2020 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
2025 code
= (c
& 0x7F) << 8;
2034 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
2035 ? charset_ascii
: charset_eight_bit
);
2041 c
= DECODE_CHAR (charset
, code
);
2045 *nbytes
= src
- src_base
;
2046 *nchars
= consumed_chars
;
2059 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2060 Check if a text is encoded in `emacs-mule'. If it is, return 1,
2064 detect_coding_emacs_mule (coding
, detect_info
)
2065 struct coding_system
*coding
;
2066 struct coding_detection_info
*detect_info
;
2068 const unsigned char *src
= coding
->source
, *src_base
;
2069 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2070 int multibytep
= coding
->src_multibyte
;
2071 int consumed_chars
= 0;
2075 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
2076 /* A coding system of this category is always ASCII compatible. */
2077 src
+= coding
->head_ascii
;
2087 /* Perhaps the start of composite character. We simple skip
2088 it because analyzing it is too heavy for detecting. But,
2089 at least, we check that the composite character
2090 constitutes of more than 4 bytes. */
2091 const unsigned char *src_base
;
2101 if (src
- src_base
<= 4)
2103 found
= CATEGORY_MASK_EMACS_MULE
;
2111 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2116 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
2118 while (more_bytes
> 0)
2123 src
--; /* Unread the last byte. */
2128 if (more_bytes
!= 0)
2130 found
= CATEGORY_MASK_EMACS_MULE
;
2133 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2137 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
2139 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2142 detect_info
->found
|= found
;
2147 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2149 /* Decode a character represented as a component of composition
2150 sequence of Emacs 20/21 style at SRC. Set C to that character and
2151 update SRC to the head of next character (or an encoded composition
2152 rule). If SRC doesn't points a composition component, set C to -1.
2153 If SRC points an invalid byte sequence, global exit by a return
2156 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
2160 int nbytes, nchars; \
2162 if (src == src_end) \
2164 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
2169 goto invalid_code; \
2173 consumed_chars += nchars; \
2178 /* Decode a composition rule represented as a component of composition
2179 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
2180 and increment BUF. If SRC points an invalid byte sequence, set C
2183 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
2185 int c, gref, nref; \
2187 if (src >= src_end) \
2188 goto invalid_code; \
2189 ONE_MORE_BYTE_NO_CHECK (c); \
2191 if (c < 0 || c >= 81) \
2192 goto invalid_code; \
2194 gref = c / 9, nref = c % 9; \
2195 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
2199 /* Decode a composition rule represented as a component of composition
2200 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
2201 and increment BUF. If SRC points an invalid byte sequence, set C
2204 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
2208 if (src + 1>= src_end) \
2209 goto invalid_code; \
2210 ONE_MORE_BYTE_NO_CHECK (gref); \
2212 ONE_MORE_BYTE_NO_CHECK (nref); \
2214 if (gref < 0 || gref >= 81 \
2215 || nref < 0 || nref >= 81) \
2216 goto invalid_code; \
2217 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
2221 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
2223 /* Emacs 21 style format. The first three bytes at SRC are \
2224 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
2225 the byte length of this composition information, CHARS is the \
2226 number of characters composed by this composition. */ \
2227 enum composition_method method = c - 0xF2; \
2228 int *charbuf_base = charbuf; \
2229 int consumed_chars_limit; \
2230 int nbytes, nchars; \
2232 ONE_MORE_BYTE (c); \
2234 goto invalid_code; \
2235 nbytes = c - 0xA0; \
2237 goto invalid_code; \
2238 ONE_MORE_BYTE (c); \
2240 goto invalid_code; \
2241 nchars = c - 0xA0; \
2242 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2243 consumed_chars_limit = consumed_chars_base + nbytes; \
2244 if (method != COMPOSITION_RELATIVE) \
2247 while (consumed_chars < consumed_chars_limit) \
2249 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
2250 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
2252 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
2255 if (consumed_chars < consumed_chars_limit) \
2256 goto invalid_code; \
2257 charbuf_base[0] -= i; \
2262 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
2264 /* Emacs 20 style format for relative composition. */ \
2265 /* Store multibyte form of characters to be composed. */ \
2266 enum composition_method method = COMPOSITION_RELATIVE; \
2267 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2268 int *buf = components; \
2272 ONE_MORE_BYTE (c); /* skip 0x80 */ \
2273 for (i = 0; *src >= 0xA0 && i < MAX_COMPOSITION_COMPONENTS; i++) \
2274 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2276 goto invalid_code; \
2277 ADD_COMPOSITION_DATA (charbuf, i, method); \
2278 for (j = 0; j < i; j++) \
2279 *charbuf++ = components[j]; \
2283 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
2285 /* Emacs 20 style format for rule-base composition. */ \
2286 /* Store multibyte form of characters to be composed. */ \
2287 enum composition_method method = COMPOSITION_WITH_RULE; \
2288 int *charbuf_base = charbuf; \
2289 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2290 int *buf = components; \
2293 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2294 for (i = 1; i < MAX_COMPOSITION_COMPONENTS; i++) \
2298 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
2299 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2301 if (i <= 1 || (buf - components) % 2 == 0) \
2302 goto invalid_code; \
2303 if (charbuf + i + (i / 2) + 1 >= charbuf_end) \
2304 goto no_more_source; \
2305 ADD_COMPOSITION_DATA (charbuf, i, method); \
2307 for (j = 0; j < i; j++) \
2308 *charbuf++ = components[j]; \
2309 charbuf_base[0] -= i; \
2310 for (j = 0; j < i; j += 2) \
2311 *charbuf++ = components[j]; \
2316 decode_coding_emacs_mule (coding
)
2317 struct coding_system
*coding
;
2319 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2320 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2321 const unsigned char *src_base
;
2322 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2324 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
2325 int consumed_chars
= 0, consumed_chars_base
;
2326 int multibytep
= coding
->src_multibyte
;
2327 Lisp_Object attrs
, charset_list
;
2328 int char_offset
= coding
->produced_char
;
2329 int last_offset
= char_offset
;
2330 int last_id
= charset_ascii
;
2331 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2332 int byte_after_cr
= -1;
2334 CODING_GET_INFO (coding
, attrs
, charset_list
);
2341 consumed_chars_base
= consumed_chars
;
2343 if (charbuf
>= charbuf_end
)
2345 if (byte_after_cr
>= 0)
2350 if (byte_after_cr
>= 0)
2351 c
= byte_after_cr
, byte_after_cr
= -1;
2361 if (eol_crlf
&& c
== '\r')
2362 ONE_MORE_BYTE (byte_after_cr
);
2371 if (c
- 0xF2 >= COMPOSITION_RELATIVE
2372 && c
- 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS
)
2373 DECODE_EMACS_MULE_21_COMPOSITION (c
);
2375 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c
);
2377 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c
);
2381 else if (c
< 0xA0 && emacs_mule_bytes
[c
] > 1)
2387 consumed_chars
= consumed_chars_base
;
2388 c
= emacs_mule_char (coding
, src
, &nbytes
, &nchars
, &id
);
2397 if (last_id
!= charset_ascii
)
2398 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2400 last_offset
= char_offset
;
2404 consumed_chars
+= nchars
;
2413 consumed_chars
= consumed_chars_base
;
2415 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2421 if (last_id
!= charset_ascii
)
2422 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2423 coding
->consumed_char
+= consumed_chars_base
;
2424 coding
->consumed
= src_base
- coding
->source
;
2425 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2429 #define EMACS_MULE_LEADING_CODES(id, codes) \
2432 codes[0] = id, codes[1] = 0; \
2433 else if (id < 0xE0) \
2434 codes[0] = 0x9A, codes[1] = id; \
2435 else if (id < 0xF0) \
2436 codes[0] = 0x9B, codes[1] = id; \
2437 else if (id < 0xF5) \
2438 codes[0] = 0x9C, codes[1] = id; \
2440 codes[0] = 0x9D, codes[1] = id; \
2445 encode_coding_emacs_mule (coding
)
2446 struct coding_system
*coding
;
2448 int multibytep
= coding
->dst_multibyte
;
2449 int *charbuf
= coding
->charbuf
;
2450 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2451 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2452 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2454 int produced_chars
= 0;
2455 Lisp_Object attrs
, charset_list
;
2457 int preferred_charset_id
= -1;
2459 CODING_GET_INFO (coding
, attrs
, charset_list
);
2460 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2462 CODING_ATTR_CHARSET_LIST (attrs
)
2463 = charset_list
= Vemacs_mule_charset_list
;
2466 while (charbuf
< charbuf_end
)
2468 ASSURE_DESTINATION (safe_room
);
2473 /* Handle an annotation. */
2476 case CODING_ANNOTATE_COMPOSITION_MASK
:
2477 /* Not yet implemented. */
2479 case CODING_ANNOTATE_CHARSET_MASK
:
2480 preferred_charset_id
= charbuf
[3];
2481 if (preferred_charset_id
>= 0
2482 && NILP (Fmemq (make_number (preferred_charset_id
),
2484 preferred_charset_id
= -1;
2493 if (ASCII_CHAR_P (c
))
2494 EMIT_ONE_ASCII_BYTE (c
);
2495 else if (CHAR_BYTE8_P (c
))
2497 c
= CHAR_TO_BYTE8 (c
);
2502 struct charset
*charset
;
2506 unsigned char leading_codes
[2];
2508 if (preferred_charset_id
>= 0)
2510 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2511 if (CHAR_CHARSET_P (c
, charset
))
2512 code
= ENCODE_CHAR (charset
, c
);
2514 charset
= char_charset (c
, charset_list
, &code
);
2517 charset
= char_charset (c
, charset_list
, &code
);
2520 c
= coding
->default_char
;
2521 if (ASCII_CHAR_P (c
))
2523 EMIT_ONE_ASCII_BYTE (c
);
2526 charset
= char_charset (c
, charset_list
, &code
);
2528 dimension
= CHARSET_DIMENSION (charset
);
2529 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2530 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2531 EMIT_ONE_BYTE (leading_codes
[0]);
2532 if (leading_codes
[1])
2533 EMIT_ONE_BYTE (leading_codes
[1]);
2535 EMIT_ONE_BYTE (code
| 0x80);
2539 EMIT_ONE_BYTE (code
>> 8);
2540 EMIT_ONE_BYTE (code
& 0xFF);
2544 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2545 coding
->produced_char
+= produced_chars
;
2546 coding
->produced
= dst
- coding
->destination
;
2551 /*** 7. ISO2022 handlers ***/
2553 /* The following note describes the coding system ISO2022 briefly.
2554 Since the intention of this note is to help understand the
2555 functions in this file, some parts are NOT ACCURATE or are OVERLY
2556 SIMPLIFIED. For thorough understanding, please refer to the
2557 original document of ISO2022. This is equivalent to the standard
2558 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2560 ISO2022 provides many mechanisms to encode several character sets
2561 in 7-bit and 8-bit environments. For 7-bit environments, all text
2562 is encoded using bytes less than 128. This may make the encoded
2563 text a little bit longer, but the text passes more easily through
2564 several types of gateway, some of which strip off the MSB (Most
2567 There are two kinds of character sets: control character sets and
2568 graphic character sets. The former contain control characters such
2569 as `newline' and `escape' to provide control functions (control
2570 functions are also provided by escape sequences). The latter
2571 contain graphic characters such as 'A' and '-'. Emacs recognizes
2572 two control character sets and many graphic character sets.
2574 Graphic character sets are classified into one of the following
2575 four classes, according to the number of bytes (DIMENSION) and
2576 number of characters in one dimension (CHARS) of the set:
2577 - DIMENSION1_CHARS94
2578 - DIMENSION1_CHARS96
2579 - DIMENSION2_CHARS94
2580 - DIMENSION2_CHARS96
2582 In addition, each character set is assigned an identification tag,
2583 unique for each set, called the "final character" (denoted as <F>
2584 hereafter). The <F> of each character set is decided by ECMA(*)
2585 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2586 (0x30..0x3F are for private use only).
2588 Note (*): ECMA = European Computer Manufacturers Association
2590 Here are examples of graphic character sets [NAME(<F>)]:
2591 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2592 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2593 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2594 o DIMENSION2_CHARS96 -- none for the moment
2596 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2597 C0 [0x00..0x1F] -- control character plane 0
2598 GL [0x20..0x7F] -- graphic character plane 0
2599 C1 [0x80..0x9F] -- control character plane 1
2600 GR [0xA0..0xFF] -- graphic character plane 1
2602 A control character set is directly designated and invoked to C0 or
2603 C1 by an escape sequence. The most common case is that:
2604 - ISO646's control character set is designated/invoked to C0, and
2605 - ISO6429's control character set is designated/invoked to C1,
2606 and usually these designations/invocations are omitted in encoded
2607 text. In a 7-bit environment, only C0 can be used, and a control
2608 character for C1 is encoded by an appropriate escape sequence to
2609 fit into the environment. All control characters for C1 are
2610 defined to have corresponding escape sequences.
2612 A graphic character set is at first designated to one of four
2613 graphic registers (G0 through G3), then these graphic registers are
2614 invoked to GL or GR. These designations and invocations can be
2615 done independently. The most common case is that G0 is invoked to
2616 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2617 these invocations and designations are omitted in encoded text.
2618 In a 7-bit environment, only GL can be used.
2620 When a graphic character set of CHARS94 is invoked to GL, codes
2621 0x20 and 0x7F of the GL area work as control characters SPACE and
2622 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2625 There are two ways of invocation: locking-shift and single-shift.
2626 With locking-shift, the invocation lasts until the next different
2627 invocation, whereas with single-shift, the invocation affects the
2628 following character only and doesn't affect the locking-shift
2629 state. Invocations are done by the following control characters or
2632 ----------------------------------------------------------------------
2633 abbrev function cntrl escape seq description
2634 ----------------------------------------------------------------------
2635 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2636 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2637 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2638 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2639 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2640 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2641 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2642 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2643 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2644 ----------------------------------------------------------------------
2645 (*) These are not used by any known coding system.
2647 Control characters for these functions are defined by macros
2648 ISO_CODE_XXX in `coding.h'.
2650 Designations are done by the following escape sequences:
2651 ----------------------------------------------------------------------
2652 escape sequence description
2653 ----------------------------------------------------------------------
2654 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2655 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2656 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2657 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2658 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2659 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2660 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2661 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2662 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2663 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2664 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2665 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2666 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2667 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2668 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2669 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2670 ----------------------------------------------------------------------
2672 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2673 of dimension 1, chars 94, and final character <F>, etc...
2675 Note (*): Although these designations are not allowed in ISO2022,
2676 Emacs accepts them on decoding, and produces them on encoding
2677 CHARS96 character sets in a coding system which is characterized as
2678 7-bit environment, non-locking-shift, and non-single-shift.
2680 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2681 '(' must be omitted. We refer to this as "short-form" hereafter.
2683 Now you may notice that there are a lot of ways of encoding the
2684 same multilingual text in ISO2022. Actually, there exist many
2685 coding systems such as Compound Text (used in X11's inter client
2686 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2687 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2688 localized platforms), and all of these are variants of ISO2022.
2690 In addition to the above, Emacs handles two more kinds of escape
2691 sequences: ISO6429's direction specification and Emacs' private
2692 sequence for specifying character composition.
2694 ISO6429's direction specification takes the following form:
2695 o CSI ']' -- end of the current direction
2696 o CSI '0' ']' -- end of the current direction
2697 o CSI '1' ']' -- start of left-to-right text
2698 o CSI '2' ']' -- start of right-to-left text
2699 The control character CSI (0x9B: control sequence introducer) is
2700 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2702 Character composition specification takes the following form:
2703 o ESC '0' -- start relative composition
2704 o ESC '1' -- end composition
2705 o ESC '2' -- start rule-base composition (*)
2706 o ESC '3' -- start relative composition with alternate chars (**)
2707 o ESC '4' -- start rule-base composition with alternate chars (**)
2708 Since these are not standard escape sequences of any ISO standard,
2709 the use of them with these meanings is restricted to Emacs only.
2711 (*) This form is used only in Emacs 20.7 and older versions,
2712 but newer versions can safely decode it.
2713 (**) This form is used only in Emacs 21.1 and newer versions,
2714 and older versions can't decode it.
2716 Here's a list of example usages of these composition escape
2717 sequences (categorized by `enum composition_method').
2719 COMPOSITION_RELATIVE:
2720 ESC 0 CHAR [ CHAR ] ESC 1
2721 COMPOSITION_WITH_RULE:
2722 ESC 2 CHAR [ RULE CHAR ] ESC 1
2723 COMPOSITION_WITH_ALTCHARS:
2724 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2725 COMPOSITION_WITH_RULE_ALTCHARS:
2726 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2728 enum iso_code_class_type iso_code_class
[256];
2730 #define SAFE_CHARSET_P(coding, id) \
2731 ((id) <= (coding)->max_charset_id \
2732 && (coding)->safe_charsets[id] >= 0)
2735 #define SHIFT_OUT_OK(category) \
2736 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2739 setup_iso_safe_charsets (attrs
)
2742 Lisp_Object charset_list
, safe_charsets
;
2743 Lisp_Object request
;
2744 Lisp_Object reg_usage
;
2747 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2750 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2751 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2752 && ! EQ (charset_list
, Viso_2022_charset_list
))
2754 CODING_ATTR_CHARSET_LIST (attrs
)
2755 = charset_list
= Viso_2022_charset_list
;
2756 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2759 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2763 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2765 int id
= XINT (XCAR (tail
));
2766 if (max_charset_id
< id
)
2767 max_charset_id
= id
;
2770 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
2772 request
= AREF (attrs
, coding_attr_iso_request
);
2773 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2774 reg94
= XINT (XCAR (reg_usage
));
2775 reg96
= XINT (XCDR (reg_usage
));
2777 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2781 struct charset
*charset
;
2784 charset
= CHARSET_FROM_ID (XINT (id
));
2785 reg
= Fcdr (Fassq (id
, request
));
2787 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2788 else if (charset
->iso_chars_96
)
2791 SSET (safe_charsets
, XINT (id
), reg96
);
2796 SSET (safe_charsets
, XINT (id
), reg94
);
2799 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2803 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2804 Check if a text is encoded in one of ISO-2022 based codig systems.
2805 If it is, return 1, else return 0. */
2808 detect_coding_iso_2022 (coding
, detect_info
)
2809 struct coding_system
*coding
;
2810 struct coding_detection_info
*detect_info
;
2812 const unsigned char *src
= coding
->source
, *src_base
= src
;
2813 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2814 int multibytep
= coding
->src_multibyte
;
2815 int single_shifting
= 0;
2818 int consumed_chars
= 0;
2822 int composition_count
= -1;
2824 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2826 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2828 struct coding_system
*this = &(coding_categories
[i
]);
2829 Lisp_Object attrs
, val
;
2833 attrs
= CODING_ID_ATTRS (this->id
);
2834 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2835 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs
), Viso_2022_charset_list
))
2836 setup_iso_safe_charsets (attrs
);
2837 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2838 this->max_charset_id
= SCHARS (val
) - 1;
2839 this->safe_charsets
= (char *) SDATA (val
);
2842 /* A coding system of this category is always ASCII compatible. */
2843 src
+= coding
->head_ascii
;
2845 while (rejected
!= CATEGORY_MASK_ISO
)
2852 if (inhibit_iso_escape_detection
)
2854 single_shifting
= 0;
2856 if (c
>= '(' && c
<= '/')
2858 /* Designation sequence for a charset of dimension 1. */
2860 if (c1
< ' ' || c1
>= 0x80
2861 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2862 /* Invalid designation sequence. Just ignore. */
2867 /* Designation sequence for a charset of dimension 2. */
2869 if (c
>= '@' && c
<= 'B')
2870 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2871 id
= iso_charset_table
[1][0][c
];
2872 else if (c
>= '(' && c
<= '/')
2875 if (c1
< ' ' || c1
>= 0x80
2876 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2877 /* Invalid designation sequence. Just ignore. */
2881 /* Invalid designation sequence. Just ignore it. */
2884 else if (c
== 'N' || c
== 'O')
2886 /* ESC <Fe> for SS2 or SS3. */
2887 single_shifting
= 1;
2888 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2893 /* End of composition. */
2894 if (composition_count
< 0
2895 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
2898 composition_count
= -1;
2899 found
|= CATEGORY_MASK_ISO
;
2901 else if (c
>= '0' && c
<= '4')
2903 /* ESC <Fp> for start/end composition. */
2904 composition_count
= 0;
2909 /* Invalid escape sequence. Just ignore it. */
2913 /* We found a valid designation sequence for CHARSET. */
2914 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2915 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2917 found
|= CATEGORY_MASK_ISO_7
;
2919 rejected
|= CATEGORY_MASK_ISO_7
;
2920 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
2922 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
2924 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
2925 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
2927 found
|= CATEGORY_MASK_ISO_7_ELSE
;
2929 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
2930 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
2932 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2934 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
2939 /* Locking shift out/in. */
2940 if (inhibit_iso_escape_detection
)
2942 single_shifting
= 0;
2943 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2947 /* Control sequence introducer. */
2948 single_shifting
= 0;
2949 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2950 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2951 goto check_extra_latin
;
2956 if (inhibit_iso_escape_detection
)
2958 single_shifting
= 0;
2959 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2960 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2961 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2962 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2963 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2964 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2965 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2966 if (single_shifting
)
2968 goto check_extra_latin
;
2975 if (composition_count
>= 0)
2976 composition_count
++;
2977 single_shifting
= 0;
2982 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2983 found
|= CATEGORY_MASK_ISO_8_1
;
2984 /* Check the length of succeeding codes of the range
2985 0xA0..0FF. If the byte length is even, we include
2986 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2987 only when we are not single shifting. */
2988 if (! single_shifting
2989 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2992 while (src
< src_end
)
3000 if (i
& 1 && src
< src_end
)
3002 rejected
|= CATEGORY_MASK_ISO_8_2
;
3003 if (composition_count
>= 0)
3004 composition_count
+= i
;
3008 found
|= CATEGORY_MASK_ISO_8_2
;
3009 if (composition_count
>= 0)
3010 composition_count
+= i
/ 2;
3016 single_shifting
= 0;
3017 if (! VECTORP (Vlatin_extra_code_table
)
3018 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3020 rejected
= CATEGORY_MASK_ISO
;
3023 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3024 & CODING_ISO_FLAG_LATIN_EXTRA
)
3025 found
|= CATEGORY_MASK_ISO_8_1
;
3027 rejected
|= CATEGORY_MASK_ISO_8_1
;
3028 rejected
|= CATEGORY_MASK_ISO_8_2
;
3031 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3035 detect_info
->rejected
|= rejected
;
3036 detect_info
->found
|= (found
& ~rejected
);
3041 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3042 escape sequence should be kept. */
3043 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3047 if (final < '0' || final >= 128 \
3048 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3049 || !SAFE_CHARSET_P (coding, id)) \
3051 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3055 prev = CODING_ISO_DESIGNATION (coding, reg); \
3056 if (id == charset_jisx0201_roman) \
3058 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3059 id = charset_ascii; \
3061 else if (id == charset_jisx0208_1978) \
3063 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3064 id = charset_jisx0208; \
3066 CODING_ISO_DESIGNATION (coding, reg) = id; \
3067 /* If there was an invalid designation to REG previously, and this \
3068 designation is ASCII to REG, we should keep this designation \
3070 if (prev == -2 && id == charset_ascii) \
3075 #define MAYBE_FINISH_COMPOSITION() \
3078 if (composition_state == COMPOSING_NO) \
3080 /* It is assured that we have enough room for producing \
3081 characters stored in the table `components'. */ \
3082 if (charbuf + component_idx > charbuf_end) \
3083 goto no_more_source; \
3084 composition_state = COMPOSING_NO; \
3085 if (method == COMPOSITION_RELATIVE \
3086 || method == COMPOSITION_WITH_ALTCHARS) \
3088 for (i = 0; i < component_idx; i++) \
3089 *charbuf++ = components[i]; \
3090 char_offset += component_idx; \
3094 for (i = 0; i < component_idx; i += 2) \
3095 *charbuf++ = components[i]; \
3096 char_offset += (component_idx / 2) + 1; \
3101 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3102 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3103 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3104 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3105 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3108 #define DECODE_COMPOSITION_START(c1) \
3111 && composition_state == COMPOSING_COMPONENT_RULE) \
3113 component_len = component_idx; \
3114 composition_state = COMPOSING_CHAR; \
3118 const unsigned char *p; \
3120 MAYBE_FINISH_COMPOSITION (); \
3121 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
3122 goto no_more_source; \
3123 for (p = src; p < src_end - 1; p++) \
3124 if (*p == ISO_CODE_ESC && p[1] == '1') \
3126 if (p == src_end - 1) \
3128 if (coding->mode & CODING_MODE_LAST_BLOCK) \
3129 goto invalid_code; \
3130 /* The current composition doesn't end in the current \
3132 record_conversion_result \
3133 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
3134 goto no_more_source; \
3137 /* This is surely the start of a composition. */ \
3138 method = (c1 == '0' ? COMPOSITION_RELATIVE \
3139 : c1 == '2' ? COMPOSITION_WITH_RULE \
3140 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3141 : COMPOSITION_WITH_RULE_ALTCHARS); \
3142 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
3143 : COMPOSING_COMPONENT_CHAR); \
3144 component_idx = component_len = 0; \
3149 /* Handle compositoin end sequence ESC 1. */
3151 #define DECODE_COMPOSITION_END() \
3153 int nchars = (component_len > 0 ? component_idx - component_len \
3154 : method == COMPOSITION_RELATIVE ? component_idx \
3155 : (component_idx + 1) / 2); \
3157 int *saved_charbuf = charbuf; \
3159 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
3160 if (method != COMPOSITION_RELATIVE) \
3162 if (component_len == 0) \
3163 for (i = 0; i < component_idx; i++) \
3164 *charbuf++ = components[i]; \
3166 for (i = 0; i < component_len; i++) \
3167 *charbuf++ = components[i]; \
3168 *saved_charbuf = saved_charbuf - charbuf; \
3170 if (method == COMPOSITION_WITH_RULE) \
3171 for (i = 0; i < component_idx; i += 2, char_offset++) \
3172 *charbuf++ = components[i]; \
3174 for (i = component_len; i < component_idx; i++, char_offset++) \
3175 *charbuf++ = components[i]; \
3176 coding->annotated = 1; \
3177 composition_state = COMPOSING_NO; \
3181 /* Decode a composition rule from the byte C1 (and maybe one more byte
3182 from SRC) and store one encoded composition rule in
3183 coding->cmp_data. */
3185 #define DECODE_COMPOSITION_RULE(c1) \
3188 if (c1 < 81) /* old format (before ver.21) */ \
3190 int gref = (c1) / 9; \
3191 int nref = (c1) % 9; \
3192 if (gref == 4) gref = 10; \
3193 if (nref == 4) nref = 10; \
3194 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
3196 else if (c1 < 93) /* new format (after ver.21) */ \
3198 ONE_MORE_BYTE (c2); \
3199 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
3206 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3209 decode_coding_iso_2022 (coding
)
3210 struct coding_system
*coding
;
3212 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3213 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3214 const unsigned char *src_base
;
3215 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3217 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
3218 int consumed_chars
= 0, consumed_chars_base
;
3219 int multibytep
= coding
->src_multibyte
;
3220 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3221 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3222 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3223 int charset_id_2
, charset_id_3
;
3224 struct charset
*charset
;
3226 /* For handling composition sequence. */
3227 #define COMPOSING_NO 0
3228 #define COMPOSING_CHAR 1
3229 #define COMPOSING_RULE 2
3230 #define COMPOSING_COMPONENT_CHAR 3
3231 #define COMPOSING_COMPONENT_RULE 4
3233 int composition_state
= COMPOSING_NO
;
3234 enum composition_method method
;
3235 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
3238 Lisp_Object attrs
, charset_list
;
3239 int char_offset
= coding
->produced_char
;
3240 int last_offset
= char_offset
;
3241 int last_id
= charset_ascii
;
3242 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3243 int byte_after_cr
= -1;
3245 CODING_GET_INFO (coding
, attrs
, charset_list
);
3246 setup_iso_safe_charsets (attrs
);
3247 /* Charset list may have been changed. */
3248 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3249 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3256 consumed_chars_base
= consumed_chars
;
3258 if (charbuf
>= charbuf_end
)
3260 if (byte_after_cr
>= 0)
3265 if (byte_after_cr
>= 0)
3266 c1
= byte_after_cr
, byte_after_cr
= -1;
3272 /* We produce at most one character. */
3273 switch (iso_code_class
[c1
])
3275 case ISO_0x20_or_0x7F
:
3276 if (composition_state
!= COMPOSING_NO
)
3278 if (composition_state
== COMPOSING_RULE
3279 || composition_state
== COMPOSING_COMPONENT_RULE
)
3281 if (component_idx
< MAX_COMPOSITION_COMPONENTS
* 2 + 1)
3283 DECODE_COMPOSITION_RULE (c1
);
3284 components
[component_idx
++] = c1
;
3285 composition_state
--;
3288 /* Too long composition. */
3289 MAYBE_FINISH_COMPOSITION ();
3292 if (charset_id_0
< 0
3293 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3294 /* This is SPACE or DEL. */
3295 charset
= CHARSET_FROM_ID (charset_ascii
);
3297 charset
= CHARSET_FROM_ID (charset_id_0
);
3300 case ISO_graphic_plane_0
:
3301 if (composition_state
!= COMPOSING_NO
)
3303 if (composition_state
== COMPOSING_RULE
3304 || composition_state
== COMPOSING_COMPONENT_RULE
)
3306 if (component_idx
< MAX_COMPOSITION_COMPONENTS
* 2 + 1)
3308 DECODE_COMPOSITION_RULE (c1
);
3309 components
[component_idx
++] = c1
;
3310 composition_state
--;
3313 MAYBE_FINISH_COMPOSITION ();
3316 if (charset_id_0
< 0)
3317 charset
= CHARSET_FROM_ID (charset_ascii
);
3319 charset
= CHARSET_FROM_ID (charset_id_0
);
3322 case ISO_0xA0_or_0xFF
:
3323 if (charset_id_1
< 0
3324 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3325 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3327 /* This is a graphic character, we fall down ... */
3329 case ISO_graphic_plane_1
:
3330 if (charset_id_1
< 0)
3332 charset
= CHARSET_FROM_ID (charset_id_1
);
3336 if (eol_crlf
&& c1
== '\r')
3337 ONE_MORE_BYTE (byte_after_cr
);
3338 MAYBE_FINISH_COMPOSITION ();
3339 charset
= CHARSET_FROM_ID (charset_ascii
);
3343 MAYBE_FINISH_COMPOSITION ();
3347 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3348 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3350 CODING_ISO_INVOCATION (coding
, 0) = 1;
3351 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3355 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3357 CODING_ISO_INVOCATION (coding
, 0) = 0;
3358 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3361 case ISO_single_shift_2_7
:
3362 case ISO_single_shift_2
:
3363 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3365 /* SS2 is handled as an escape sequence of ESC 'N' */
3367 goto label_escape_sequence
;
3369 case ISO_single_shift_3
:
3370 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3372 /* SS2 is handled as an escape sequence of ESC 'O' */
3374 goto label_escape_sequence
;
3376 case ISO_control_sequence_introducer
:
3377 /* CSI is handled as an escape sequence of ESC '[' ... */
3379 goto label_escape_sequence
;
3383 label_escape_sequence
:
3384 /* Escape sequences handled here are invocation,
3385 designation, direction specification, and character
3386 composition specification. */
3389 case '&': /* revision of following character set */
3391 if (!(c1
>= '@' && c1
<= '~'))
3394 if (c1
!= ISO_CODE_ESC
)
3397 goto label_escape_sequence
;
3399 case '$': /* designation of 2-byte character set */
3400 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3406 if (c1
>= '@' && c1
<= 'B')
3407 { /* designation of JISX0208.1978, GB2312.1980,
3409 reg
= 0, chars96
= 0;
3411 else if (c1
>= 0x28 && c1
<= 0x2B)
3412 { /* designation of DIMENSION2_CHARS94 character set */
3413 reg
= c1
- 0x28, chars96
= 0;
3416 else if (c1
>= 0x2C && c1
<= 0x2F)
3417 { /* designation of DIMENSION2_CHARS96 character set */
3418 reg
= c1
- 0x2C, chars96
= 1;
3423 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3424 /* We must update these variables now. */
3426 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3428 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3434 case 'n': /* invocation of locking-shift-2 */
3435 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3436 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3438 CODING_ISO_INVOCATION (coding
, 0) = 2;
3439 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3442 case 'o': /* invocation of locking-shift-3 */
3443 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3444 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3446 CODING_ISO_INVOCATION (coding
, 0) = 3;
3447 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3450 case 'N': /* invocation of single-shift-2 */
3451 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3452 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3454 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3455 if (charset_id_2
< 0)
3456 charset
= CHARSET_FROM_ID (charset_ascii
);
3458 charset
= CHARSET_FROM_ID (charset_id_2
);
3460 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3464 case 'O': /* invocation of single-shift-3 */
3465 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3466 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3468 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3469 if (charset_id_3
< 0)
3470 charset
= CHARSET_FROM_ID (charset_ascii
);
3472 charset
= CHARSET_FROM_ID (charset_id_3
);
3474 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3478 case '0': case '2': case '3': case '4': /* start composition */
3479 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3481 DECODE_COMPOSITION_START (c1
);
3484 case '1': /* end composition */
3485 if (composition_state
== COMPOSING_NO
)
3487 DECODE_COMPOSITION_END ();
3490 case '[': /* specification of direction */
3491 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3493 /* For the moment, nested direction is not supported.
3494 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3495 left-to-right, and nozero means right-to-left. */
3499 case ']': /* end of the current direction */
3500 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3502 case '0': /* end of the current direction */
3503 case '1': /* start of left-to-right direction */
3506 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3511 case '2': /* start of right-to-left direction */
3514 coding
->mode
|= CODING_MODE_DIRECTION
;
3528 /* CTEXT extended segment:
3529 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3530 We keep these bytes as is for the moment.
3531 They may be decoded by post-read-conversion. */
3535 ONE_MORE_BYTE (dim
);
3538 size
= ((M
- 128) * 128) + (L
- 128);
3539 if (charbuf
+ 8 + size
> charbuf_end
)
3541 *charbuf
++ = ISO_CODE_ESC
;
3545 *charbuf
++ = BYTE8_TO_CHAR (M
);
3546 *charbuf
++ = BYTE8_TO_CHAR (L
);
3550 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3555 /* XFree86 extension for embedding UTF-8 in CTEXT:
3556 ESC % G --UTF-8-BYTES-- ESC % @
3557 We keep these bytes as is for the moment.
3558 They may be decoded by post-read-conversion. */
3561 if (p
+ 6 > charbuf_end
)
3563 *p
++ = ISO_CODE_ESC
;
3566 while (p
< charbuf_end
)
3569 if (c1
== ISO_CODE_ESC
3570 && src
+ 1 < src_end
3577 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3579 if (p
+ 3 > charbuf_end
)
3581 *p
++ = ISO_CODE_ESC
;
3592 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3597 if (c1
>= 0x28 && c1
<= 0x2B)
3598 { /* designation of DIMENSION1_CHARS94 character set */
3599 reg
= c1
- 0x28, chars96
= 0;
3602 else if (c1
>= 0x2C && c1
<= 0x2F)
3603 { /* designation of DIMENSION1_CHARS96 character set */
3604 reg
= c1
- 0x2C, chars96
= 1;
3609 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3610 /* We must update these variables now. */
3612 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3614 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3622 if (charset
->id
!= charset_ascii
3623 && last_id
!= charset
->id
)
3625 if (last_id
!= charset_ascii
)
3626 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3627 last_id
= charset
->id
;
3628 last_offset
= char_offset
;
3631 /* Now we know CHARSET and 1st position code C1 of a character.
3632 Produce a decoded character while getting 2nd position code
3635 if (CHARSET_DIMENSION (charset
) > 1)
3638 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3639 /* C2 is not in a valid range. */
3641 c1
= (c1
<< 8) | (c2
& 0x7F);
3642 if (CHARSET_DIMENSION (charset
) > 2)
3645 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3646 /* C2 is not in a valid range. */
3648 c1
= (c1
<< 8) | (c2
& 0x7F);
3652 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3655 MAYBE_FINISH_COMPOSITION ();
3656 for (; src_base
< src
; src_base
++, char_offset
++)
3658 if (ASCII_BYTE_P (*src_base
))
3659 *charbuf
++ = *src_base
;
3661 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3664 else if (composition_state
== COMPOSING_NO
)
3671 if (component_idx
< MAX_COMPOSITION_COMPONENTS
* 2 + 1)
3673 components
[component_idx
++] = c
;
3674 if (method
== COMPOSITION_WITH_RULE
3675 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3676 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3677 composition_state
++;
3681 MAYBE_FINISH_COMPOSITION ();
3689 MAYBE_FINISH_COMPOSITION ();
3691 consumed_chars
= consumed_chars_base
;
3693 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3703 if (last_id
!= charset_ascii
)
3704 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3705 coding
->consumed_char
+= consumed_chars_base
;
3706 coding
->consumed
= src_base
- coding
->source
;
3707 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3711 /* ISO2022 encoding stuff. */
3714 It is not enough to say just "ISO2022" on encoding, we have to
3715 specify more details. In Emacs, each coding system of ISO2022
3716 variant has the following specifications:
3717 1. Initial designation to G0 thru G3.
3718 2. Allows short-form designation?
3719 3. ASCII should be designated to G0 before control characters?
3720 4. ASCII should be designated to G0 at end of line?
3721 5. 7-bit environment or 8-bit environment?
3722 6. Use locking-shift?
3723 7. Use Single-shift?
3724 And the following two are only for Japanese:
3725 8. Use ASCII in place of JIS0201-1976-Roman?
3726 9. Use JISX0208-1983 in place of JISX0208-1978?
3727 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3728 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3732 /* Produce codes (escape sequence) for designating CHARSET to graphic
3733 register REG at DST, and increment DST. If <final-char> of CHARSET is
3734 '@', 'A', or 'B' and the coding system CODING allows, produce
3735 designation sequence of short-form. */
3737 #define ENCODE_DESIGNATION(charset, reg, coding) \
3739 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3740 char *intermediate_char_94 = "()*+"; \
3741 char *intermediate_char_96 = ",-./"; \
3742 int revision = -1; \
3745 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3746 revision = CHARSET_ISO_REVISION (charset); \
3748 if (revision >= 0) \
3750 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3751 EMIT_ONE_BYTE ('@' + revision); \
3753 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3754 if (CHARSET_DIMENSION (charset) == 1) \
3756 if (! CHARSET_ISO_CHARS_96 (charset)) \
3757 c = intermediate_char_94[reg]; \
3759 c = intermediate_char_96[reg]; \
3760 EMIT_ONE_ASCII_BYTE (c); \
3764 EMIT_ONE_ASCII_BYTE ('$'); \
3765 if (! CHARSET_ISO_CHARS_96 (charset)) \
3767 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3769 || final_char < '@' || final_char > 'B') \
3770 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3773 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3775 EMIT_ONE_ASCII_BYTE (final_char); \
3777 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3781 /* The following two macros produce codes (control character or escape
3782 sequence) for ISO2022 single-shift functions (single-shift-2 and
3785 #define ENCODE_SINGLE_SHIFT_2 \
3787 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3788 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3790 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3791 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3795 #define ENCODE_SINGLE_SHIFT_3 \
3797 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3798 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3800 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3801 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3805 /* The following four macros produce codes (control character or
3806 escape sequence) for ISO2022 locking-shift functions (shift-in,
3807 shift-out, locking-shift-2, and locking-shift-3). */
3809 #define ENCODE_SHIFT_IN \
3811 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3812 CODING_ISO_INVOCATION (coding, 0) = 0; \
3816 #define ENCODE_SHIFT_OUT \
3818 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3819 CODING_ISO_INVOCATION (coding, 0) = 1; \
3823 #define ENCODE_LOCKING_SHIFT_2 \
3825 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3826 CODING_ISO_INVOCATION (coding, 0) = 2; \
3830 #define ENCODE_LOCKING_SHIFT_3 \
3832 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3833 CODING_ISO_INVOCATION (coding, 0) = 3; \
3837 /* Produce codes for a DIMENSION1 character whose character set is
3838 CHARSET and whose position-code is C1. Designation and invocation
3839 sequences are also produced in advance if necessary. */
3841 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3843 int id = CHARSET_ID (charset); \
3845 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3846 && id == charset_ascii) \
3848 id = charset_jisx0201_roman; \
3849 charset = CHARSET_FROM_ID (id); \
3852 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3854 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3855 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3857 EMIT_ONE_BYTE (c1 | 0x80); \
3858 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3861 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3863 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3866 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3868 EMIT_ONE_BYTE (c1 | 0x80); \
3872 /* Since CHARSET is not yet invoked to any graphic planes, we \
3873 must invoke it, or, at first, designate it to some graphic \
3874 register. Then repeat the loop to actually produce the \
3876 dst = encode_invocation_designation (charset, coding, dst, \
3881 /* Produce codes for a DIMENSION2 character whose character set is
3882 CHARSET and whose position-codes are C1 and C2. Designation and
3883 invocation codes are also produced in advance if necessary. */
3885 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3887 int id = CHARSET_ID (charset); \
3889 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3890 && id == charset_jisx0208) \
3892 id = charset_jisx0208_1978; \
3893 charset = CHARSET_FROM_ID (id); \
3896 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3898 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3899 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3901 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3902 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3905 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3907 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3910 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3912 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3916 /* Since CHARSET is not yet invoked to any graphic planes, we \
3917 must invoke it, or, at first, designate it to some graphic \
3918 register. Then repeat the loop to actually produce the \
3920 dst = encode_invocation_designation (charset, coding, dst, \
3925 #define ENCODE_ISO_CHARACTER(charset, c) \
3927 int code = ENCODE_CHAR ((charset),(c)); \
3929 if (CHARSET_DIMENSION (charset) == 1) \
3930 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3932 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3936 /* Produce designation and invocation codes at a place pointed by DST
3937 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3941 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3942 struct charset
*charset
;
3943 struct coding_system
*coding
;
3947 int multibytep
= coding
->dst_multibyte
;
3948 int produced_chars
= *p_nchars
;
3949 int reg
; /* graphic register number */
3950 int id
= CHARSET_ID (charset
);
3952 /* At first, check designations. */
3953 for (reg
= 0; reg
< 4; reg
++)
3954 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3959 /* CHARSET is not yet designated to any graphic registers. */
3960 /* At first check the requested designation. */
3961 reg
= CODING_ISO_REQUEST (coding
, id
);
3963 /* Since CHARSET requests no special designation, designate it
3964 to graphic register 0. */
3967 ENCODE_DESIGNATION (charset
, reg
, coding
);
3970 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3971 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3973 /* Since the graphic register REG is not invoked to any graphic
3974 planes, invoke it to graphic plane 0. */
3977 case 0: /* graphic register 0 */
3981 case 1: /* graphic register 1 */
3985 case 2: /* graphic register 2 */
3986 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3987 ENCODE_SINGLE_SHIFT_2
;
3989 ENCODE_LOCKING_SHIFT_2
;
3992 case 3: /* graphic register 3 */
3993 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3994 ENCODE_SINGLE_SHIFT_3
;
3996 ENCODE_LOCKING_SHIFT_3
;
4001 *p_nchars
= produced_chars
;
4005 /* The following three macros produce codes for indicating direction
4007 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
4009 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
4010 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
4012 EMIT_ONE_BYTE (ISO_CODE_CSI); \
4016 #define ENCODE_DIRECTION_R2L() \
4018 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4019 EMIT_TWO_ASCII_BYTES ('2', ']'); \
4023 #define ENCODE_DIRECTION_L2R() \
4025 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4026 EMIT_TWO_ASCII_BYTES ('0', ']'); \
4030 /* Produce codes for designation and invocation to reset the graphic
4031 planes and registers to initial state. */
4032 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4035 struct charset *charset; \
4037 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4039 for (reg = 0; reg < 4; reg++) \
4040 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4041 && (CODING_ISO_DESIGNATION (coding, reg) \
4042 != CODING_ISO_INITIAL (coding, reg))) \
4044 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4045 ENCODE_DESIGNATION (charset, reg, coding); \
4050 /* Produce designation sequences of charsets in the line started from
4051 SRC to a place pointed by DST, and return updated DST.
4053 If the current block ends before any end-of-line, we may fail to
4054 find all the necessary designations. */
4056 static unsigned char *
4057 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
4058 struct coding_system
*coding
;
4059 int *charbuf
, *charbuf_end
;
4062 struct charset
*charset
;
4063 /* Table of charsets to be designated to each graphic register. */
4065 int c
, found
= 0, reg
;
4066 int produced_chars
= 0;
4067 int multibytep
= coding
->dst_multibyte
;
4069 Lisp_Object charset_list
;
4071 attrs
= CODING_ID_ATTRS (coding
->id
);
4072 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4073 if (EQ (charset_list
, Qiso_2022
))
4074 charset_list
= Viso_2022_charset_list
;
4076 for (reg
= 0; reg
< 4; reg
++)
4086 charset
= char_charset (c
, charset_list
, NULL
);
4087 id
= CHARSET_ID (charset
);
4088 reg
= CODING_ISO_REQUEST (coding
, id
);
4089 if (reg
>= 0 && r
[reg
] < 0)
4098 for (reg
= 0; reg
< 4; reg
++)
4100 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4101 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4107 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4110 encode_coding_iso_2022 (coding
)
4111 struct coding_system
*coding
;
4113 int multibytep
= coding
->dst_multibyte
;
4114 int *charbuf
= coding
->charbuf
;
4115 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4116 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4117 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4120 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4121 && CODING_ISO_BOL (coding
));
4122 int produced_chars
= 0;
4123 Lisp_Object attrs
, eol_type
, charset_list
;
4124 int ascii_compatible
;
4126 int preferred_charset_id
= -1;
4128 CODING_GET_INFO (coding
, attrs
, charset_list
);
4129 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
4130 if (VECTORP (eol_type
))
4133 setup_iso_safe_charsets (attrs
);
4134 /* Charset list may have been changed. */
4135 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4136 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
4138 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4140 while (charbuf
< charbuf_end
)
4142 ASSURE_DESTINATION (safe_room
);
4144 if (bol_designation
)
4146 unsigned char *dst_prev
= dst
;
4148 /* We have to produce designation sequences if any now. */
4149 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
4150 bol_designation
= 0;
4151 /* We are sure that designation sequences are all ASCII bytes. */
4152 produced_chars
+= dst
- dst_prev
;
4159 /* Handle an annotation. */
4162 case CODING_ANNOTATE_COMPOSITION_MASK
:
4163 /* Not yet implemented. */
4165 case CODING_ANNOTATE_CHARSET_MASK
:
4166 preferred_charset_id
= charbuf
[2];
4167 if (preferred_charset_id
>= 0
4168 && NILP (Fmemq (make_number (preferred_charset_id
),
4170 preferred_charset_id
= -1;
4179 /* Now encode the character C. */
4180 if (c
< 0x20 || c
== 0x7F)
4183 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4185 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4186 ENCODE_RESET_PLANE_AND_REGISTER ();
4187 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4191 for (i
= 0; i
< 4; i
++)
4192 CODING_ISO_DESIGNATION (coding
, i
)
4193 = CODING_ISO_INITIAL (coding
, i
);
4196 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4198 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4199 ENCODE_RESET_PLANE_AND_REGISTER ();
4200 EMIT_ONE_ASCII_BYTE (c
);
4202 else if (ASCII_CHAR_P (c
))
4204 if (ascii_compatible
)
4205 EMIT_ONE_ASCII_BYTE (c
);
4208 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4209 ENCODE_ISO_CHARACTER (charset
, c
);
4212 else if (CHAR_BYTE8_P (c
))
4214 c
= CHAR_TO_BYTE8 (c
);
4219 struct charset
*charset
;
4221 if (preferred_charset_id
>= 0)
4223 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4224 if (! CHAR_CHARSET_P (c
, charset
))
4225 charset
= char_charset (c
, charset_list
, NULL
);
4228 charset
= char_charset (c
, charset_list
, NULL
);
4231 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4233 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4234 charset
= CHARSET_FROM_ID (charset_ascii
);
4238 c
= coding
->default_char
;
4239 charset
= char_charset (c
, charset_list
, NULL
);
4242 ENCODE_ISO_CHARACTER (charset
, c
);
4246 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4247 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4249 ASSURE_DESTINATION (safe_room
);
4250 ENCODE_RESET_PLANE_AND_REGISTER ();
4252 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4253 CODING_ISO_BOL (coding
) = bol_designation
;
4254 coding
->produced_char
+= produced_chars
;
4255 coding
->produced
= dst
- coding
->destination
;
4260 /*** 8,9. SJIS and BIG5 handlers ***/
4262 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4263 quite widely. So, for the moment, Emacs supports them in the bare
4264 C code. But, in the future, they may be supported only by CCL. */
4266 /* SJIS is a coding system encoding three character sets: ASCII, right
4267 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4268 as is. A character of charset katakana-jisx0201 is encoded by
4269 "position-code + 0x80". A character of charset japanese-jisx0208
4270 is encoded in 2-byte but two position-codes are divided and shifted
4271 so that it fit in the range below.
4273 --- CODE RANGE of SJIS ---
4274 (character set) (range)
4276 KATAKANA-JISX0201 0xA0 .. 0xDF
4277 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4278 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4279 -------------------------------
4283 /* BIG5 is a coding system encoding two character sets: ASCII and
4284 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4285 character set and is encoded in two-byte.
4287 --- CODE RANGE of BIG5 ---
4288 (character set) (range)
4290 Big5 (1st byte) 0xA1 .. 0xFE
4291 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4292 --------------------------
4296 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4297 Check if a text is encoded in SJIS. If it is, return
4298 CATEGORY_MASK_SJIS, else return 0. */
4301 detect_coding_sjis (coding
, detect_info
)
4302 struct coding_system
*coding
;
4303 struct coding_detection_info
*detect_info
;
4305 const unsigned char *src
= coding
->source
, *src_base
;
4306 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4307 int multibytep
= coding
->src_multibyte
;
4308 int consumed_chars
= 0;
4312 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4313 /* A coding system of this category is always ASCII compatible. */
4314 src
+= coding
->head_ascii
;
4322 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
4325 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4327 found
= CATEGORY_MASK_SJIS
;
4329 else if (c
>= 0xA0 && c
< 0xE0)
4330 found
= CATEGORY_MASK_SJIS
;
4334 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4338 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4340 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4343 detect_info
->found
|= found
;
4347 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4348 Check if a text is encoded in BIG5. If it is, return
4349 CATEGORY_MASK_BIG5, else return 0. */
4352 detect_coding_big5 (coding
, detect_info
)
4353 struct coding_system
*coding
;
4354 struct coding_detection_info
*detect_info
;
4356 const unsigned char *src
= coding
->source
, *src_base
;
4357 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4358 int multibytep
= coding
->src_multibyte
;
4359 int consumed_chars
= 0;
4363 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4364 /* A coding system of this category is always ASCII compatible. */
4365 src
+= coding
->head_ascii
;
4376 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4378 found
= CATEGORY_MASK_BIG5
;
4383 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4387 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4389 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4392 detect_info
->found
|= found
;
4396 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4397 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4400 decode_coding_sjis (coding
)
4401 struct coding_system
*coding
;
4403 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4404 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4405 const unsigned char *src_base
;
4406 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4408 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4409 int consumed_chars
= 0, consumed_chars_base
;
4410 int multibytep
= coding
->src_multibyte
;
4411 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4412 struct charset
*charset_kanji2
;
4413 Lisp_Object attrs
, charset_list
, val
;
4414 int char_offset
= coding
->produced_char
;
4415 int last_offset
= char_offset
;
4416 int last_id
= charset_ascii
;
4417 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4418 int byte_after_cr
= -1;
4420 CODING_GET_INFO (coding
, attrs
, charset_list
);
4423 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4424 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4425 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4426 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4431 struct charset
*charset
;
4434 consumed_chars_base
= consumed_chars
;
4436 if (charbuf
>= charbuf_end
)
4438 if (byte_after_cr
>= 0)
4443 if (byte_after_cr
>= 0)
4444 c
= byte_after_cr
, byte_after_cr
= -1;
4451 if (eol_crlf
&& c
== '\r')
4452 ONE_MORE_BYTE (byte_after_cr
);
4453 charset
= charset_roman
;
4455 else if (c
== 0x80 || c
== 0xA0)
4457 else if (c
>= 0xA1 && c
<= 0xDF)
4459 /* SJIS -> JISX0201-Kana */
4461 charset
= charset_kana
;
4465 /* SJIS -> JISX0208 */
4467 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4471 charset
= charset_kanji
;
4473 else if (c
<= 0xFC && charset_kanji2
)
4475 /* SJIS -> JISX0213-2 */
4477 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4481 charset
= charset_kanji2
;
4485 if (charset
->id
!= charset_ascii
4486 && last_id
!= charset
->id
)
4488 if (last_id
!= charset_ascii
)
4489 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4490 last_id
= charset
->id
;
4491 last_offset
= char_offset
;
4493 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4500 consumed_chars
= consumed_chars_base
;
4502 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4508 if (last_id
!= charset_ascii
)
4509 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4510 coding
->consumed_char
+= consumed_chars_base
;
4511 coding
->consumed
= src_base
- coding
->source
;
4512 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4516 decode_coding_big5 (coding
)
4517 struct coding_system
*coding
;
4519 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4520 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4521 const unsigned char *src_base
;
4522 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4524 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4525 int consumed_chars
= 0, consumed_chars_base
;
4526 int multibytep
= coding
->src_multibyte
;
4527 struct charset
*charset_roman
, *charset_big5
;
4528 Lisp_Object attrs
, charset_list
, val
;
4529 int char_offset
= coding
->produced_char
;
4530 int last_offset
= char_offset
;
4531 int last_id
= charset_ascii
;
4532 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4533 int byte_after_cr
= -1;
4535 CODING_GET_INFO (coding
, attrs
, charset_list
);
4537 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4538 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4543 struct charset
*charset
;
4546 consumed_chars_base
= consumed_chars
;
4548 if (charbuf
>= charbuf_end
)
4550 if (byte_after_cr
>= 0)
4555 if (byte_after_cr
>= 0)
4556 c
= byte_after_cr
, byte_after_cr
= -1;
4564 if (eol_crlf
&& c
== '\r')
4565 ONE_MORE_BYTE (byte_after_cr
);
4566 charset
= charset_roman
;
4571 if (c
< 0xA1 || c
> 0xFE)
4574 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4577 charset
= charset_big5
;
4579 if (charset
->id
!= charset_ascii
4580 && last_id
!= charset
->id
)
4582 if (last_id
!= charset_ascii
)
4583 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4584 last_id
= charset
->id
;
4585 last_offset
= char_offset
;
4587 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4594 consumed_chars
= consumed_chars_base
;
4596 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4602 if (last_id
!= charset_ascii
)
4603 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4604 coding
->consumed_char
+= consumed_chars_base
;
4605 coding
->consumed
= src_base
- coding
->source
;
4606 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4609 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4610 This function can encode charsets `ascii', `katakana-jisx0201',
4611 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4612 are sure that all these charsets are registered as official charset
4613 (i.e. do not have extended leading-codes). Characters of other
4614 charsets are produced without any encoding. If SJIS_P is 1, encode
4615 SJIS text, else encode BIG5 text. */
4618 encode_coding_sjis (coding
)
4619 struct coding_system
*coding
;
4621 int multibytep
= coding
->dst_multibyte
;
4622 int *charbuf
= coding
->charbuf
;
4623 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4624 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4625 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4627 int produced_chars
= 0;
4628 Lisp_Object attrs
, charset_list
, val
;
4629 int ascii_compatible
;
4630 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4631 struct charset
*charset_kanji2
;
4634 CODING_GET_INFO (coding
, attrs
, charset_list
);
4636 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4637 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4638 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4639 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4641 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4643 while (charbuf
< charbuf_end
)
4645 ASSURE_DESTINATION (safe_room
);
4647 /* Now encode the character C. */
4648 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4649 EMIT_ONE_ASCII_BYTE (c
);
4650 else if (CHAR_BYTE8_P (c
))
4652 c
= CHAR_TO_BYTE8 (c
);
4658 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4662 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4664 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4665 charset
= CHARSET_FROM_ID (charset_ascii
);
4669 c
= coding
->default_char
;
4670 charset
= char_charset (c
, charset_list
, &code
);
4673 if (code
== CHARSET_INVALID_CODE (charset
))
4675 if (charset
== charset_kanji
)
4679 c1
= code
>> 8, c2
= code
& 0xFF;
4680 EMIT_TWO_BYTES (c1
, c2
);
4682 else if (charset
== charset_kana
)
4683 EMIT_ONE_BYTE (code
| 0x80);
4684 else if (charset_kanji2
&& charset
== charset_kanji2
)
4689 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4690 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4692 JIS_TO_SJIS2 (code
);
4693 c1
= code
>> 8, c2
= code
& 0xFF;
4694 EMIT_TWO_BYTES (c1
, c2
);
4697 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4700 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4703 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4704 coding
->produced_char
+= produced_chars
;
4705 coding
->produced
= dst
- coding
->destination
;
4710 encode_coding_big5 (coding
)
4711 struct coding_system
*coding
;
4713 int multibytep
= coding
->dst_multibyte
;
4714 int *charbuf
= coding
->charbuf
;
4715 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4716 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4717 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4719 int produced_chars
= 0;
4720 Lisp_Object attrs
, charset_list
, val
;
4721 int ascii_compatible
;
4722 struct charset
*charset_roman
, *charset_big5
;
4725 CODING_GET_INFO (coding
, attrs
, charset_list
);
4727 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4728 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4729 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4731 while (charbuf
< charbuf_end
)
4733 ASSURE_DESTINATION (safe_room
);
4735 /* Now encode the character C. */
4736 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4737 EMIT_ONE_ASCII_BYTE (c
);
4738 else if (CHAR_BYTE8_P (c
))
4740 c
= CHAR_TO_BYTE8 (c
);
4746 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4750 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4752 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4753 charset
= CHARSET_FROM_ID (charset_ascii
);
4757 c
= coding
->default_char
;
4758 charset
= char_charset (c
, charset_list
, &code
);
4761 if (code
== CHARSET_INVALID_CODE (charset
))
4763 if (charset
== charset_big5
)
4767 c1
= code
>> 8, c2
= code
& 0xFF;
4768 EMIT_TWO_BYTES (c1
, c2
);
4771 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4774 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4775 coding
->produced_char
+= produced_chars
;
4776 coding
->produced
= dst
- coding
->destination
;
4781 /*** 10. CCL handlers ***/
4783 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4784 Check if a text is encoded in a coding system of which
4785 encoder/decoder are written in CCL program. If it is, return
4786 CATEGORY_MASK_CCL, else return 0. */
4789 detect_coding_ccl (coding
, detect_info
)
4790 struct coding_system
*coding
;
4791 struct coding_detection_info
*detect_info
;
4793 const unsigned char *src
= coding
->source
, *src_base
;
4794 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4795 int multibytep
= coding
->src_multibyte
;
4796 int consumed_chars
= 0;
4798 unsigned char *valids
;
4799 int head_ascii
= coding
->head_ascii
;
4802 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4804 coding
= &coding_categories
[coding_category_ccl
];
4805 valids
= CODING_CCL_VALIDS (coding
);
4806 attrs
= CODING_ID_ATTRS (coding
->id
);
4807 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4816 if (c
< 0 || ! valids
[c
])
4818 if ((valids
[c
] > 1))
4819 found
= CATEGORY_MASK_CCL
;
4821 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4825 detect_info
->found
|= found
;
4830 decode_coding_ccl (coding
)
4831 struct coding_system
*coding
;
4833 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4834 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4835 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4836 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4837 int consumed_chars
= 0;
4838 int multibytep
= coding
->src_multibyte
;
4839 struct ccl_program ccl
;
4840 int source_charbuf
[1024];
4841 int source_byteidx
[1024];
4842 Lisp_Object attrs
, charset_list
;
4844 CODING_GET_INFO (coding
, attrs
, charset_list
);
4845 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4847 while (src
< src_end
)
4849 const unsigned char *p
= src
;
4850 int *source
, *source_end
;
4854 while (i
< 1024 && p
< src_end
)
4856 source_byteidx
[i
] = p
- src
;
4857 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4860 while (i
< 1024 && p
< src_end
)
4861 source_charbuf
[i
++] = *p
++;
4863 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4866 source
= source_charbuf
;
4867 source_end
= source
+ i
;
4868 while (source
< source_end
)
4870 ccl_driver (&ccl
, source
, charbuf
,
4871 source_end
- source
, charbuf_end
- charbuf
,
4873 source
+= ccl
.consumed
;
4874 charbuf
+= ccl
.produced
;
4875 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4878 if (source
< source_end
)
4879 src
+= source_byteidx
[source
- source_charbuf
];
4882 consumed_chars
+= source
- source_charbuf
;
4884 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4885 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4891 case CCL_STAT_SUSPEND_BY_SRC
:
4892 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4894 case CCL_STAT_SUSPEND_BY_DST
:
4897 case CCL_STAT_INVALID_CMD
:
4898 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4901 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4904 coding
->consumed_char
+= consumed_chars
;
4905 coding
->consumed
= src
- coding
->source
;
4906 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4910 encode_coding_ccl (coding
)
4911 struct coding_system
*coding
;
4913 struct ccl_program ccl
;
4914 int multibytep
= coding
->dst_multibyte
;
4915 int *charbuf
= coding
->charbuf
;
4916 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4917 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4918 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4919 int destination_charbuf
[1024];
4920 int i
, produced_chars
= 0;
4921 Lisp_Object attrs
, charset_list
;
4923 CODING_GET_INFO (coding
, attrs
, charset_list
);
4924 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4926 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4927 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4929 while (charbuf
< charbuf_end
)
4931 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4932 charbuf_end
- charbuf
, 1024, charset_list
);
4935 ASSURE_DESTINATION (ccl
.produced
* 2);
4936 for (i
= 0; i
< ccl
.produced
; i
++)
4937 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4941 ASSURE_DESTINATION (ccl
.produced
);
4942 for (i
= 0; i
< ccl
.produced
; i
++)
4943 *dst
++ = destination_charbuf
[i
] & 0xFF;
4944 produced_chars
+= ccl
.produced
;
4946 charbuf
+= ccl
.consumed
;
4947 if (ccl
.status
== CCL_STAT_QUIT
4948 || ccl
.status
== CCL_STAT_INVALID_CMD
)
4954 case CCL_STAT_SUSPEND_BY_SRC
:
4955 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4957 case CCL_STAT_SUSPEND_BY_DST
:
4958 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4961 case CCL_STAT_INVALID_CMD
:
4962 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4965 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4969 coding
->produced_char
+= produced_chars
;
4970 coding
->produced
= dst
- coding
->destination
;
4976 /*** 10, 11. no-conversion handlers ***/
4978 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4981 decode_coding_raw_text (coding
)
4982 struct coding_system
*coding
;
4984 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4986 coding
->chars_at_source
= 1;
4987 coding
->consumed_char
= coding
->src_chars
;
4988 coding
->consumed
= coding
->src_bytes
;
4989 if (eol_crlf
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
4991 coding
->consumed_char
--;
4993 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4996 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5000 encode_coding_raw_text (coding
)
5001 struct coding_system
*coding
;
5003 int multibytep
= coding
->dst_multibyte
;
5004 int *charbuf
= coding
->charbuf
;
5005 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5006 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5007 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5008 int produced_chars
= 0;
5013 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5015 if (coding
->src_multibyte
)
5016 while (charbuf
< charbuf_end
)
5018 ASSURE_DESTINATION (safe_room
);
5020 if (ASCII_CHAR_P (c
))
5021 EMIT_ONE_ASCII_BYTE (c
);
5022 else if (CHAR_BYTE8_P (c
))
5024 c
= CHAR_TO_BYTE8 (c
);
5029 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5031 CHAR_STRING_ADVANCE (c
, p1
);
5034 EMIT_ONE_BYTE (*p0
);
5040 while (charbuf
< charbuf_end
)
5042 ASSURE_DESTINATION (safe_room
);
5049 if (coding
->src_multibyte
)
5051 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5053 while (charbuf
< charbuf_end
)
5055 ASSURE_DESTINATION (safe_room
);
5057 if (ASCII_CHAR_P (c
))
5059 else if (CHAR_BYTE8_P (c
))
5060 *dst
++ = CHAR_TO_BYTE8 (c
);
5062 CHAR_STRING_ADVANCE (c
, dst
);
5067 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5068 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5069 *dst
++ = *charbuf
++;
5071 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5073 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5074 coding
->produced_char
+= produced_chars
;
5075 coding
->produced
= dst
- coding
->destination
;
5079 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5080 Check if a text is encoded in a charset-based coding system. If it
5081 is, return 1, else return 0. */
5084 detect_coding_charset (coding
, detect_info
)
5085 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;
5092 Lisp_Object attrs
, valids
, name
;
5094 int head_ascii
= coding
->head_ascii
;
5095 int check_latin_extra
= 0;
5097 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5099 coding
= &coding_categories
[coding_category_charset
];
5100 attrs
= CODING_ID_ATTRS (coding
->id
);
5101 valids
= AREF (attrs
, coding_attr_charset_valids
);
5102 name
= CODING_ID_NAME (coding
->id
);
5103 if (VECTORP (Vlatin_extra_code_table
)
5104 && strcmp ((char *) SDATA (SYMBOL_NAME (name
)), "iso-8859-") == 0)
5105 check_latin_extra
= 1;
5106 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5113 struct charset
*charset
;
5120 val
= AREF (valids
, c
);
5126 && check_latin_extra
5127 && NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
5129 found
= CATEGORY_MASK_CHARSET
;
5133 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5134 dim
= CHARSET_DIMENSION (charset
);
5135 for (idx
= 1; idx
< dim
; idx
++)
5140 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5141 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5150 for (; CONSP (val
); val
= XCDR (val
))
5152 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5153 dim
= CHARSET_DIMENSION (charset
);
5159 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5160 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5175 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5179 detect_info
->found
|= found
;
5184 decode_coding_charset (coding
)
5185 struct coding_system
*coding
;
5187 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5188 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5189 const unsigned char *src_base
;
5190 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5192 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
5193 int consumed_chars
= 0, consumed_chars_base
;
5194 int multibytep
= coding
->src_multibyte
;
5195 Lisp_Object attrs
, charset_list
, valids
;
5196 int char_offset
= coding
->produced_char
;
5197 int last_offset
= char_offset
;
5198 int last_id
= charset_ascii
;
5199 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5200 int byte_after_cr
= -1;
5202 CODING_GET_INFO (coding
, attrs
, charset_list
);
5203 valids
= AREF (attrs
, coding_attr_charset_valids
);
5209 struct charset
*charset
;
5215 consumed_chars_base
= consumed_chars
;
5217 if (charbuf
>= charbuf_end
)
5219 if (byte_after_cr
>= 0)
5224 if (byte_after_cr
>= 0)
5232 if (eol_crlf
&& c
== '\r')
5233 ONE_MORE_BYTE (byte_after_cr
);
5239 val
= AREF (valids
, c
);
5240 if (! INTEGERP (val
) && ! CONSP (val
))
5244 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5245 dim
= CHARSET_DIMENSION (charset
);
5249 code
= (code
<< 8) | c
;
5252 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5257 /* VAL is a list of charset IDs. It is assured that the
5258 list is sorted by charset dimensions (smaller one
5262 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5263 dim
= CHARSET_DIMENSION (charset
);
5267 code
= (code
<< 8) | c
;
5270 CODING_DECODE_CHAR (coding
, src
, src_base
,
5271 src_end
, charset
, code
, c
);
5279 if (charset
->id
!= charset_ascii
5280 && last_id
!= charset
->id
)
5282 if (last_id
!= charset_ascii
)
5283 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5284 last_id
= charset
->id
;
5285 last_offset
= char_offset
;
5294 consumed_chars
= consumed_chars_base
;
5296 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5302 if (last_id
!= charset_ascii
)
5303 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5304 coding
->consumed_char
+= consumed_chars_base
;
5305 coding
->consumed
= src_base
- coding
->source
;
5306 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5310 encode_coding_charset (coding
)
5311 struct coding_system
*coding
;
5313 int multibytep
= coding
->dst_multibyte
;
5314 int *charbuf
= coding
->charbuf
;
5315 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5316 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5317 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5318 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5319 int produced_chars
= 0;
5320 Lisp_Object attrs
, charset_list
;
5321 int ascii_compatible
;
5324 CODING_GET_INFO (coding
, attrs
, charset_list
);
5325 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5327 while (charbuf
< charbuf_end
)
5329 struct charset
*charset
;
5332 ASSURE_DESTINATION (safe_room
);
5334 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5335 EMIT_ONE_ASCII_BYTE (c
);
5336 else if (CHAR_BYTE8_P (c
))
5338 c
= CHAR_TO_BYTE8 (c
);
5343 charset
= char_charset (c
, charset_list
, &code
);
5346 if (CHARSET_DIMENSION (charset
) == 1)
5347 EMIT_ONE_BYTE (code
);
5348 else if (CHARSET_DIMENSION (charset
) == 2)
5349 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5350 else if (CHARSET_DIMENSION (charset
) == 3)
5351 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5353 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5354 (code
>> 8) & 0xFF, code
& 0xFF);
5358 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5359 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5361 c
= coding
->default_char
;
5367 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5368 coding
->produced_char
+= produced_chars
;
5369 coding
->produced
= dst
- coding
->destination
;
5374 /*** 7. C library functions ***/
5376 /* Setup coding context CODING from information about CODING_SYSTEM.
5377 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5378 CODING_SYSTEM is invalid, signal an error. */
5381 setup_coding_system (coding_system
, coding
)
5382 Lisp_Object coding_system
;
5383 struct coding_system
*coding
;
5386 Lisp_Object eol_type
;
5387 Lisp_Object coding_type
;
5390 if (NILP (coding_system
))
5391 coding_system
= Qundecided
;
5393 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5395 attrs
= CODING_ID_ATTRS (coding
->id
);
5396 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5399 coding
->head_ascii
= -1;
5400 if (VECTORP (eol_type
))
5401 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5402 | CODING_REQUIRE_DETECTION_MASK
);
5403 else if (! EQ (eol_type
, Qunix
))
5404 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5405 | CODING_REQUIRE_ENCODING_MASK
);
5407 coding
->common_flags
= 0;
5408 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5409 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5410 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5411 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5412 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5413 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5415 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5416 coding
->max_charset_id
= SCHARS (val
) - 1;
5417 coding
->safe_charsets
= (char *) SDATA (val
);
5418 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5420 coding_type
= CODING_ATTR_TYPE (attrs
);
5421 if (EQ (coding_type
, Qundecided
))
5423 coding
->detector
= NULL
;
5424 coding
->decoder
= decode_coding_raw_text
;
5425 coding
->encoder
= encode_coding_raw_text
;
5426 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5428 else if (EQ (coding_type
, Qiso_2022
))
5431 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5433 /* Invoke graphic register 0 to plane 0. */
5434 CODING_ISO_INVOCATION (coding
, 0) = 0;
5435 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5436 CODING_ISO_INVOCATION (coding
, 1)
5437 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5438 /* Setup the initial status of designation. */
5439 for (i
= 0; i
< 4; i
++)
5440 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5441 /* Not single shifting initially. */
5442 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5443 /* Beginning of buffer should also be regarded as bol. */
5444 CODING_ISO_BOL (coding
) = 1;
5445 coding
->detector
= detect_coding_iso_2022
;
5446 coding
->decoder
= decode_coding_iso_2022
;
5447 coding
->encoder
= encode_coding_iso_2022
;
5448 if (flags
& CODING_ISO_FLAG_SAFE
)
5449 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5450 coding
->common_flags
5451 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5452 | CODING_REQUIRE_FLUSHING_MASK
);
5453 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5454 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5455 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5456 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5457 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5459 setup_iso_safe_charsets (attrs
);
5460 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5461 coding
->max_charset_id
= SCHARS (val
) - 1;
5462 coding
->safe_charsets
= (char *) SDATA (val
);
5464 CODING_ISO_FLAGS (coding
) = flags
;
5466 else if (EQ (coding_type
, Qcharset
))
5468 coding
->detector
= detect_coding_charset
;
5469 coding
->decoder
= decode_coding_charset
;
5470 coding
->encoder
= encode_coding_charset
;
5471 coding
->common_flags
5472 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5474 else if (EQ (coding_type
, Qutf_8
))
5476 val
= AREF (attrs
, coding_attr_utf_bom
);
5477 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5478 : EQ (val
, Qt
) ? utf_with_bom
5480 coding
->detector
= detect_coding_utf_8
;
5481 coding
->decoder
= decode_coding_utf_8
;
5482 coding
->encoder
= encode_coding_utf_8
;
5483 coding
->common_flags
5484 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5485 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5486 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5488 else if (EQ (coding_type
, Qutf_16
))
5490 val
= AREF (attrs
, coding_attr_utf_bom
);
5491 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5492 : EQ (val
, Qt
) ? utf_with_bom
5494 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5495 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5496 : utf_16_little_endian
);
5497 CODING_UTF_16_SURROGATE (coding
) = 0;
5498 coding
->detector
= detect_coding_utf_16
;
5499 coding
->decoder
= decode_coding_utf_16
;
5500 coding
->encoder
= encode_coding_utf_16
;
5501 coding
->common_flags
5502 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5503 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5504 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5506 else if (EQ (coding_type
, Qccl
))
5508 coding
->detector
= detect_coding_ccl
;
5509 coding
->decoder
= decode_coding_ccl
;
5510 coding
->encoder
= encode_coding_ccl
;
5511 coding
->common_flags
5512 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5513 | CODING_REQUIRE_FLUSHING_MASK
);
5515 else if (EQ (coding_type
, Qemacs_mule
))
5517 coding
->detector
= detect_coding_emacs_mule
;
5518 coding
->decoder
= decode_coding_emacs_mule
;
5519 coding
->encoder
= encode_coding_emacs_mule
;
5520 coding
->common_flags
5521 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5522 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5523 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5525 Lisp_Object tail
, safe_charsets
;
5526 int max_charset_id
= 0;
5528 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5530 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5531 max_charset_id
= XFASTINT (XCAR (tail
));
5532 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5534 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5536 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5537 coding
->max_charset_id
= max_charset_id
;
5538 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5541 else if (EQ (coding_type
, Qshift_jis
))
5543 coding
->detector
= detect_coding_sjis
;
5544 coding
->decoder
= decode_coding_sjis
;
5545 coding
->encoder
= encode_coding_sjis
;
5546 coding
->common_flags
5547 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5549 else if (EQ (coding_type
, Qbig5
))
5551 coding
->detector
= detect_coding_big5
;
5552 coding
->decoder
= decode_coding_big5
;
5553 coding
->encoder
= encode_coding_big5
;
5554 coding
->common_flags
5555 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5557 else /* EQ (coding_type, Qraw_text) */
5559 coding
->detector
= NULL
;
5560 coding
->decoder
= decode_coding_raw_text
;
5561 coding
->encoder
= encode_coding_raw_text
;
5562 if (! EQ (eol_type
, Qunix
))
5564 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5565 if (! VECTORP (eol_type
))
5566 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5574 /* Return a list of charsets supported by CODING. */
5577 coding_charset_list (coding
)
5578 struct coding_system
*coding
;
5580 Lisp_Object attrs
, charset_list
;
5582 CODING_GET_INFO (coding
, attrs
, charset_list
);
5583 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5585 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5587 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5588 charset_list
= Viso_2022_charset_list
;
5590 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5592 charset_list
= Vemacs_mule_charset_list
;
5594 return charset_list
;
5598 /* Return a list of charsets supported by CODING-SYSTEM. */
5601 coding_system_charset_list (coding_system
)
5602 Lisp_Object coding_system
;
5605 Lisp_Object attrs
, charset_list
;
5607 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5608 attrs
= CODING_ID_ATTRS (id
);
5610 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5612 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5614 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5615 charset_list
= Viso_2022_charset_list
;
5617 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5619 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5621 charset_list
= Vemacs_mule_charset_list
;
5625 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5627 return charset_list
;
5631 /* Return raw-text or one of its subsidiaries that has the same
5632 eol_type as CODING-SYSTEM. */
5635 raw_text_coding_system (coding_system
)
5636 Lisp_Object coding_system
;
5638 Lisp_Object spec
, attrs
;
5639 Lisp_Object eol_type
, raw_text_eol_type
;
5641 if (NILP (coding_system
))
5643 spec
= CODING_SYSTEM_SPEC (coding_system
);
5644 attrs
= AREF (spec
, 0);
5646 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5647 return coding_system
;
5649 eol_type
= AREF (spec
, 2);
5650 if (VECTORP (eol_type
))
5652 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5653 raw_text_eol_type
= AREF (spec
, 2);
5654 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5655 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5656 : AREF (raw_text_eol_type
, 2));
5660 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5661 does, return one of the subsidiary that has the same eol-spec as
5662 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5663 inherit end-of-line format from the system's setting
5664 (system_eol_type). */
5667 coding_inherit_eol_type (coding_system
, parent
)
5668 Lisp_Object coding_system
, parent
;
5670 Lisp_Object spec
, eol_type
;
5672 if (NILP (coding_system
))
5673 coding_system
= Qraw_text
;
5674 spec
= CODING_SYSTEM_SPEC (coding_system
);
5675 eol_type
= AREF (spec
, 2);
5676 if (VECTORP (eol_type
))
5678 Lisp_Object parent_eol_type
;
5680 if (! NILP (parent
))
5682 Lisp_Object parent_spec
;
5684 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5685 parent_eol_type
= AREF (parent_spec
, 2);
5688 parent_eol_type
= system_eol_type
;
5689 if (EQ (parent_eol_type
, Qunix
))
5690 coding_system
= AREF (eol_type
, 0);
5691 else if (EQ (parent_eol_type
, Qdos
))
5692 coding_system
= AREF (eol_type
, 1);
5693 else if (EQ (parent_eol_type
, Qmac
))
5694 coding_system
= AREF (eol_type
, 2);
5696 return coding_system
;
5699 /* Emacs has a mechanism to automatically detect a coding system if it
5700 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5701 it's impossible to distinguish some coding systems accurately
5702 because they use the same range of codes. So, at first, coding
5703 systems are categorized into 7, those are:
5705 o coding-category-emacs-mule
5707 The category for a coding system which has the same code range
5708 as Emacs' internal format. Assigned the coding-system (Lisp
5709 symbol) `emacs-mule' by default.
5711 o coding-category-sjis
5713 The category for a coding system which has the same code range
5714 as SJIS. Assigned the coding-system (Lisp
5715 symbol) `japanese-shift-jis' by default.
5717 o coding-category-iso-7
5719 The category for a coding system which has the same code range
5720 as ISO2022 of 7-bit environment. This doesn't use any locking
5721 shift and single shift functions. This can encode/decode all
5722 charsets. Assigned the coding-system (Lisp symbol)
5723 `iso-2022-7bit' by default.
5725 o coding-category-iso-7-tight
5727 Same as coding-category-iso-7 except that this can
5728 encode/decode only the specified charsets.
5730 o coding-category-iso-8-1
5732 The category for a coding system which has the same code range
5733 as ISO2022 of 8-bit environment and graphic plane 1 used only
5734 for DIMENSION1 charset. This doesn't use any locking shift
5735 and single shift functions. Assigned the coding-system (Lisp
5736 symbol) `iso-latin-1' by default.
5738 o coding-category-iso-8-2
5740 The category for a coding system which has the same code range
5741 as ISO2022 of 8-bit environment and graphic plane 1 used only
5742 for DIMENSION2 charset. This doesn't use any locking shift
5743 and single shift functions. Assigned the coding-system (Lisp
5744 symbol) `japanese-iso-8bit' by default.
5746 o coding-category-iso-7-else
5748 The category for a coding system which has the same code range
5749 as ISO2022 of 7-bit environemnt but uses locking shift or
5750 single shift functions. Assigned the coding-system (Lisp
5751 symbol) `iso-2022-7bit-lock' by default.
5753 o coding-category-iso-8-else
5755 The category for a coding system which has the same code range
5756 as ISO2022 of 8-bit environemnt but uses locking shift or
5757 single shift functions. Assigned the coding-system (Lisp
5758 symbol) `iso-2022-8bit-ss2' by default.
5760 o coding-category-big5
5762 The category for a coding system which has the same code range
5763 as BIG5. Assigned the coding-system (Lisp symbol)
5764 `cn-big5' by default.
5766 o coding-category-utf-8
5768 The category for a coding system which has the same code range
5769 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5770 symbol) `utf-8' by default.
5772 o coding-category-utf-16-be
5774 The category for a coding system in which a text has an
5775 Unicode signature (cf. Unicode Standard) in the order of BIG
5776 endian at the head. Assigned the coding-system (Lisp symbol)
5777 `utf-16-be' by default.
5779 o coding-category-utf-16-le
5781 The category for a coding system in which a text has an
5782 Unicode signature (cf. Unicode Standard) in the order of
5783 LITTLE endian at the head. Assigned the coding-system (Lisp
5784 symbol) `utf-16-le' by default.
5786 o coding-category-ccl
5788 The category for a coding system of which encoder/decoder is
5789 written in CCL programs. The default value is nil, i.e., no
5790 coding system is assigned.
5792 o coding-category-binary
5794 The category for a coding system not categorized in any of the
5795 above. Assigned the coding-system (Lisp symbol)
5796 `no-conversion' by default.
5798 Each of them is a Lisp symbol and the value is an actual
5799 `coding-system's (this is also a Lisp symbol) assigned by a user.
5800 What Emacs does actually is to detect a category of coding system.
5801 Then, it uses a `coding-system' assigned to it. If Emacs can't
5802 decide only one possible category, it selects a category of the
5803 highest priority. Priorities of categories are also specified by a
5804 user in a Lisp variable `coding-category-list'.
5808 #define EOL_SEEN_NONE 0
5809 #define EOL_SEEN_LF 1
5810 #define EOL_SEEN_CR 2
5811 #define EOL_SEEN_CRLF 4
5813 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5814 SOURCE is encoded. If CATEGORY is one of
5815 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5816 two-byte, else they are encoded by one-byte.
5818 Return one of EOL_SEEN_XXX. */
5820 #define MAX_EOL_CHECK_COUNT 3
5823 detect_eol (source
, src_bytes
, category
)
5824 const unsigned char *source
;
5825 EMACS_INT src_bytes
;
5826 enum coding_category category
;
5828 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5831 int eol_seen
= EOL_SEEN_NONE
;
5833 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5837 msb
= category
== (coding_category_utf_16_le
5838 | coding_category_utf_16_le_nosig
);
5841 while (src
+ 1 < src_end
)
5844 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5849 this_eol
= EOL_SEEN_LF
;
5850 else if (src
+ 3 >= src_end
5851 || src
[msb
+ 2] != 0
5852 || src
[lsb
+ 2] != '\n')
5853 this_eol
= EOL_SEEN_CR
;
5856 this_eol
= EOL_SEEN_CRLF
;
5860 if (eol_seen
== EOL_SEEN_NONE
)
5861 /* This is the first end-of-line. */
5862 eol_seen
= this_eol
;
5863 else if (eol_seen
!= this_eol
)
5865 /* The found type is different from what found before.
5866 Allow for stray ^M characters in DOS EOL files. */
5867 if (eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
5868 || eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
)
5869 eol_seen
= EOL_SEEN_CRLF
;
5872 eol_seen
= EOL_SEEN_LF
;
5876 if (++total
== MAX_EOL_CHECK_COUNT
)
5884 while (src
< src_end
)
5887 if (c
== '\n' || c
== '\r')
5892 this_eol
= EOL_SEEN_LF
;
5893 else if (src
>= src_end
|| *src
!= '\n')
5894 this_eol
= EOL_SEEN_CR
;
5896 this_eol
= EOL_SEEN_CRLF
, src
++;
5898 if (eol_seen
== EOL_SEEN_NONE
)
5899 /* This is the first end-of-line. */
5900 eol_seen
= this_eol
;
5901 else if (eol_seen
!= this_eol
)
5903 /* The found type is different from what found before.
5904 Allow for stray ^M characters in DOS EOL files. */
5905 if (eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
5906 || eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
)
5907 eol_seen
= EOL_SEEN_CRLF
;
5910 eol_seen
= EOL_SEEN_LF
;
5914 if (++total
== MAX_EOL_CHECK_COUNT
)
5924 adjust_coding_eol_type (coding
, eol_seen
)
5925 struct coding_system
*coding
;
5928 Lisp_Object eol_type
;
5930 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5931 if (eol_seen
& EOL_SEEN_LF
)
5933 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5936 else if (eol_seen
& EOL_SEEN_CRLF
)
5938 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5941 else if (eol_seen
& EOL_SEEN_CR
)
5943 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5949 /* Detect how a text specified in CODING is encoded. If a coding
5950 system is detected, update fields of CODING by the detected coding
5954 detect_coding (coding
)
5955 struct coding_system
*coding
;
5957 const unsigned char *src
, *src_end
;
5959 coding
->consumed
= coding
->consumed_char
= 0;
5960 coding
->produced
= coding
->produced_char
= 0;
5961 coding_set_source (coding
);
5963 src_end
= coding
->source
+ coding
->src_bytes
;
5964 coding
->head_ascii
= 0;
5966 /* If we have not yet decided the text encoding type, detect it
5968 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5971 struct coding_detection_info detect_info
;
5972 int null_byte_found
= 0, eight_bit_found
= 0;
5974 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5975 for (src
= coding
->source
; src
< src_end
; src
++)
5980 eight_bit_found
= 1;
5981 if (null_byte_found
)
5986 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5987 && ! inhibit_iso_escape_detection
5988 && ! detect_info
.checked
)
5990 if (detect_coding_iso_2022 (coding
, &detect_info
))
5992 /* We have scanned the whole data. */
5993 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5995 /* We didn't find an 8-bit code. We may
5996 have found a null-byte, but it's very
5997 rare that a binary file confirm to
6000 coding
->head_ascii
= src
- coding
->source
;
6002 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6006 else if (! c
&& !inhibit_null_byte_detection
)
6008 null_byte_found
= 1;
6009 if (eight_bit_found
)
6012 if (! eight_bit_found
)
6013 coding
->head_ascii
++;
6015 else if (! eight_bit_found
)
6016 coding
->head_ascii
++;
6019 if (null_byte_found
|| eight_bit_found
6020 || coding
->head_ascii
< coding
->src_bytes
6021 || detect_info
.found
)
6023 enum coding_category category
;
6024 struct coding_system
*this;
6026 if (coding
->head_ascii
== coding
->src_bytes
)
6027 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6028 for (i
= 0; i
< coding_category_raw_text
; i
++)
6030 category
= coding_priorities
[i
];
6031 this = coding_categories
+ category
;
6032 if (detect_info
.found
& (1 << category
))
6037 if (null_byte_found
)
6039 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6040 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6042 for (i
= 0; i
< coding_category_raw_text
; i
++)
6044 category
= coding_priorities
[i
];
6045 this = coding_categories
+ category
;
6048 /* No coding system of this category is defined. */
6049 detect_info
.rejected
|= (1 << category
);
6051 else if (category
>= coding_category_raw_text
)
6053 else if (detect_info
.checked
& (1 << category
))
6055 if (detect_info
.found
& (1 << category
))
6058 else if ((*(this->detector
)) (coding
, &detect_info
)
6059 && detect_info
.found
& (1 << category
))
6061 if (category
== coding_category_utf_16_auto
)
6063 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6064 category
= coding_category_utf_16_le
;
6066 category
= coding_category_utf_16_be
;
6073 if (i
< coding_category_raw_text
)
6074 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6075 else if (null_byte_found
)
6076 setup_coding_system (Qno_conversion
, coding
);
6077 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6078 == CATEGORY_MASK_ANY
)
6079 setup_coding_system (Qraw_text
, coding
);
6080 else if (detect_info
.rejected
)
6081 for (i
= 0; i
< coding_category_raw_text
; i
++)
6082 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6084 this = coding_categories
+ coding_priorities
[i
];
6085 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6090 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6091 == coding_category_utf_8_auto
)
6093 Lisp_Object coding_systems
;
6094 struct coding_detection_info detect_info
;
6097 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6098 detect_info
.found
= detect_info
.rejected
= 0;
6099 coding
->head_ascii
= 0;
6100 if (CONSP (coding_systems
)
6101 && detect_coding_utf_8 (coding
, &detect_info
))
6103 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6104 setup_coding_system (XCAR (coding_systems
), coding
);
6106 setup_coding_system (XCDR (coding_systems
), coding
);
6109 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6110 == coding_category_utf_16_auto
)
6112 Lisp_Object coding_systems
;
6113 struct coding_detection_info detect_info
;
6116 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6117 detect_info
.found
= detect_info
.rejected
= 0;
6118 coding
->head_ascii
= 0;
6119 if (CONSP (coding_systems
)
6120 && detect_coding_utf_16 (coding
, &detect_info
))
6122 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6123 setup_coding_system (XCAR (coding_systems
), coding
);
6124 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6125 setup_coding_system (XCDR (coding_systems
), coding
);
6133 struct coding_system
*coding
;
6135 Lisp_Object eol_type
;
6136 unsigned char *p
, *pbeg
, *pend
;
6138 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6139 if (EQ (eol_type
, Qunix
))
6142 if (NILP (coding
->dst_object
))
6143 pbeg
= coding
->destination
;
6145 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6146 pend
= pbeg
+ coding
->produced
;
6148 if (VECTORP (eol_type
))
6150 int eol_seen
= EOL_SEEN_NONE
;
6152 for (p
= pbeg
; p
< pend
; p
++)
6155 eol_seen
|= EOL_SEEN_LF
;
6156 else if (*p
== '\r')
6158 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6160 eol_seen
|= EOL_SEEN_CRLF
;
6164 eol_seen
|= EOL_SEEN_CR
;
6167 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6168 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6169 && (eol_seen
& EOL_SEEN_CR
) != 0
6170 && (eol_seen
& EOL_SEEN_LF
) == 0)
6171 eol_seen
= EOL_SEEN_CRLF
;
6172 else if (eol_seen
!= EOL_SEEN_NONE
6173 && eol_seen
!= EOL_SEEN_LF
6174 && eol_seen
!= EOL_SEEN_CRLF
6175 && eol_seen
!= EOL_SEEN_CR
)
6176 eol_seen
= EOL_SEEN_LF
;
6177 if (eol_seen
!= EOL_SEEN_NONE
)
6178 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6181 if (EQ (eol_type
, Qmac
))
6183 for (p
= pbeg
; p
< pend
; p
++)
6187 else if (EQ (eol_type
, Qdos
))
6191 if (NILP (coding
->dst_object
))
6193 /* Start deleting '\r' from the tail to minimize the memory
6195 for (p
= pend
- 2; p
>= pbeg
; p
--)
6198 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
6204 int pos_byte
= coding
->dst_pos_byte
;
6205 int pos
= coding
->dst_pos
;
6206 int pos_end
= pos
+ coding
->produced_char
- 1;
6208 while (pos
< pos_end
)
6210 p
= BYTE_POS_ADDR (pos_byte
);
6211 if (*p
== '\r' && p
[1] == '\n')
6213 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6218 if (coding
->dst_multibyte
)
6219 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6224 coding
->produced
-= n
;
6225 coding
->produced_char
-= n
;
6230 /* Return a translation table (or list of them) from coding system
6231 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6232 decoding (ENCODEP is zero). */
6235 get_translation_table (attrs
, encodep
, max_lookup
)
6237 int encodep
, *max_lookup
;
6239 Lisp_Object standard
, translation_table
;
6243 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6244 standard
= Vstandard_translation_table_for_encode
;
6246 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6247 standard
= Vstandard_translation_table_for_decode
;
6248 if (NILP (translation_table
))
6249 translation_table
= standard
;
6252 if (SYMBOLP (translation_table
))
6253 translation_table
= Fget (translation_table
, Qtranslation_table
);
6254 else if (CONSP (translation_table
))
6256 translation_table
= Fcopy_sequence (translation_table
);
6257 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6258 if (SYMBOLP (XCAR (val
)))
6259 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6261 if (CHAR_TABLE_P (standard
))
6263 if (CONSP (translation_table
))
6264 translation_table
= nconc2 (translation_table
,
6265 Fcons (standard
, Qnil
));
6267 translation_table
= Fcons (translation_table
,
6268 Fcons (standard
, Qnil
));
6275 if (CHAR_TABLE_P (translation_table
)
6276 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6278 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6279 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6280 *max_lookup
= XFASTINT (val
);
6282 else if (CONSP (translation_table
))
6284 Lisp_Object tail
, val
;
6286 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6287 if (CHAR_TABLE_P (XCAR (tail
))
6288 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6290 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6291 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6292 *max_lookup
= XFASTINT (val
);
6296 return translation_table
;
6299 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6302 if (CHAR_TABLE_P (table)) \
6304 trans = CHAR_TABLE_REF (table, c); \
6305 if (CHARACTERP (trans)) \
6306 c = XFASTINT (trans), trans = Qnil; \
6308 else if (CONSP (table)) \
6312 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6313 if (CHAR_TABLE_P (XCAR (tail))) \
6315 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6316 if (CHARACTERP (trans)) \
6317 c = XFASTINT (trans), trans = Qnil; \
6318 else if (! NILP (trans)) \
6326 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
6330 int *from_nchars
, *to_nchars
;
6332 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
6336 Lisp_Object from
, tail
;
6339 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
6344 for (i
= 0; i
< len
; i
++)
6346 if (buf
+ i
== buf_end
)
6352 if (XINT (AREF (from
, i
)) != buf
[i
])
6366 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
6374 produce_chars (coding
, translation_table
, last_block
)
6375 struct coding_system
*coding
;
6376 Lisp_Object translation_table
;
6379 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6380 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6382 EMACS_INT produced_chars
= 0;
6385 if (! coding
->chars_at_source
)
6387 /* Source characters are in coding->charbuf. */
6388 int *buf
= coding
->charbuf
;
6389 int *buf_end
= buf
+ coding
->charbuf_used
;
6391 if (EQ (coding
->src_object
, coding
->dst_object
))
6393 coding_set_source (coding
);
6394 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6397 while (buf
< buf_end
)
6403 int from_nchars
= 1, to_nchars
= 1;
6404 Lisp_Object trans
= Qnil
;
6406 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6409 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
6410 &from_nchars
, &to_nchars
);
6416 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6418 dst
= alloc_destination (coding
,
6420 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6422 if (EQ (coding
->src_object
, coding
->dst_object
))
6424 coding_set_source (coding
);
6425 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6428 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6431 for (i
= 0; i
< to_nchars
; i
++)
6434 c
= XINT (AREF (trans
, i
));
6435 if (coding
->dst_multibyte
6436 || ! CHAR_BYTE8_P (c
))
6437 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6439 *dst
++ = CHAR_TO_BYTE8 (c
);
6441 produced_chars
+= to_nchars
;
6443 while (--from_nchars
> 0)
6447 /* This is an annotation datum. (-C) is the length. */
6450 carryover
= buf_end
- buf
;
6454 /* Source characters are at coding->source. */
6455 const unsigned char *src
= coding
->source
;
6456 const unsigned char *src_end
= src
+ coding
->consumed
;
6458 if (EQ (coding
->dst_object
, coding
->src_object
))
6459 dst_end
= (unsigned char *) src
;
6460 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6462 if (coding
->src_multibyte
)
6465 EMACS_INT consumed_chars
= 0;
6469 const unsigned char *src_base
= src
;
6475 if (EQ (coding
->src_object
, coding
->dst_object
))
6476 dst_end
= (unsigned char *) src
;
6479 EMACS_INT offset
= src
- coding
->source
;
6481 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6483 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6484 coding_set_source (coding
);
6485 src
= coding
->source
+ offset
;
6486 src_end
= coding
->source
+ coding
->src_bytes
;
6487 if (EQ (coding
->src_object
, coding
->dst_object
))
6488 dst_end
= (unsigned char *) src
;
6498 while (src
< src_end
)
6503 if (dst
>= dst_end
- 1)
6505 if (EQ (coding
->src_object
, coding
->dst_object
))
6506 dst_end
= (unsigned char *) src
;
6507 if (dst
>= dst_end
- 1)
6509 EMACS_INT offset
= src
- coding
->source
;
6510 EMACS_INT more_bytes
;
6512 if (EQ (coding
->src_object
, coding
->dst_object
))
6513 more_bytes
= ((src_end
- src
) / 2) + 2;
6515 more_bytes
= src_end
- src
+ 2;
6516 dst
= alloc_destination (coding
, more_bytes
, dst
);
6517 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6518 coding_set_source (coding
);
6519 src
= coding
->source
+ offset
;
6520 src_end
= coding
->source
+ coding
->src_bytes
;
6521 if (EQ (coding
->src_object
, coding
->dst_object
))
6522 dst_end
= (unsigned char *) src
;
6530 if (!EQ (coding
->src_object
, coding
->dst_object
))
6532 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6536 EMACS_INT offset
= src
- coding
->source
;
6538 dst
= alloc_destination (coding
, require
, dst
);
6539 coding_set_source (coding
);
6540 src
= coding
->source
+ offset
;
6541 src_end
= coding
->source
+ coding
->src_bytes
;
6544 produced_chars
= coding
->consumed_char
;
6545 while (src
< src_end
)
6550 produced
= dst
- (coding
->destination
+ coding
->produced
);
6551 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6552 insert_from_gap (produced_chars
, produced
);
6553 coding
->produced
+= produced
;
6554 coding
->produced_char
+= produced_chars
;
6558 /* Compose text in CODING->object according to the annotation data at
6559 CHARBUF. CHARBUF is an array:
6560 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6564 produce_composition (coding
, charbuf
, pos
)
6565 struct coding_system
*coding
;
6571 enum composition_method method
;
6572 Lisp_Object components
;
6575 to
= pos
+ charbuf
[2];
6578 method
= (enum composition_method
) (charbuf
[3]);
6580 if (method
== COMPOSITION_RELATIVE
)
6582 else if (method
>= COMPOSITION_WITH_RULE
6583 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6585 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6590 for (i
= 0; i
< len
; i
++)
6592 args
[i
] = make_number (charbuf
[i
]);
6596 components
= (method
== COMPOSITION_WITH_ALTCHARS
6597 ? Fstring (len
, args
) : Fvector (len
, args
));
6601 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6605 /* Put `charset' property on text in CODING->object according to
6606 the annotation data at CHARBUF. CHARBUF is an array:
6607 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6611 produce_charset (coding
, charbuf
, pos
)
6612 struct coding_system
*coding
;
6616 EMACS_INT from
= pos
- charbuf
[2];
6617 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6619 Fput_text_property (make_number (from
), make_number (pos
),
6620 Qcharset
, CHARSET_NAME (charset
),
6621 coding
->dst_object
);
6625 #define CHARBUF_SIZE 0x4000
6627 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6629 int size = CHARBUF_SIZE; \
6631 coding->charbuf = NULL; \
6632 while (size > 1024) \
6634 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6635 if (coding->charbuf) \
6639 if (! coding->charbuf) \
6641 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6642 return coding->result; \
6644 coding->charbuf_size = size; \
6649 produce_annotation (coding
, pos
)
6650 struct coding_system
*coding
;
6653 int *charbuf
= coding
->charbuf
;
6654 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6656 if (NILP (coding
->dst_object
))
6659 while (charbuf
< charbuf_end
)
6665 int len
= -*charbuf
;
6668 case CODING_ANNOTATE_COMPOSITION_MASK
:
6669 produce_composition (coding
, charbuf
, pos
);
6671 case CODING_ANNOTATE_CHARSET_MASK
:
6672 produce_charset (coding
, charbuf
, pos
);
6682 /* Decode the data at CODING->src_object into CODING->dst_object.
6683 CODING->src_object is a buffer, a string, or nil.
6684 CODING->dst_object is a buffer.
6686 If CODING->src_object is a buffer, it must be the current buffer.
6687 In this case, if CODING->src_pos is positive, it is a position of
6688 the source text in the buffer, otherwise, the source text is in the
6689 gap area of the buffer, and CODING->src_pos specifies the offset of
6690 the text from GPT (which must be the same as PT). If this is the
6691 same buffer as CODING->dst_object, CODING->src_pos must be
6694 If CODING->src_object is a string, CODING->src_pos is an index to
6697 If CODING->src_object is nil, CODING->source must already point to
6698 the non-relocatable memory area. In this case, CODING->src_pos is
6699 an offset from CODING->source.
6701 The decoded data is inserted at the current point of the buffer
6706 decode_coding (coding
)
6707 struct coding_system
*coding
;
6710 Lisp_Object undo_list
;
6711 Lisp_Object translation_table
;
6715 if (BUFFERP (coding
->src_object
)
6716 && coding
->src_pos
> 0
6717 && coding
->src_pos
< GPT
6718 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6719 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6722 if (BUFFERP (coding
->dst_object
))
6724 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6725 set_buffer_internal (XBUFFER (coding
->dst_object
));
6727 move_gap_both (PT
, PT_BYTE
);
6728 undo_list
= current_buffer
->undo_list
;
6729 current_buffer
->undo_list
= Qt
;
6732 coding
->consumed
= coding
->consumed_char
= 0;
6733 coding
->produced
= coding
->produced_char
= 0;
6734 coding
->chars_at_source
= 0;
6735 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6738 ALLOC_CONVERSION_WORK_AREA (coding
);
6740 attrs
= CODING_ID_ATTRS (coding
->id
);
6741 translation_table
= get_translation_table (attrs
, 0, NULL
);
6746 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6748 coding_set_source (coding
);
6749 coding
->annotated
= 0;
6750 coding
->charbuf_used
= carryover
;
6751 (*(coding
->decoder
)) (coding
);
6752 coding_set_destination (coding
);
6753 carryover
= produce_chars (coding
, translation_table
, 0);
6754 if (coding
->annotated
)
6755 produce_annotation (coding
, pos
);
6756 for (i
= 0; i
< carryover
; i
++)
6758 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6760 while (coding
->consumed
< coding
->src_bytes
6761 && (coding
->result
== CODING_RESULT_SUCCESS
6762 || coding
->result
== CODING_RESULT_INVALID_SRC
));
6766 coding_set_destination (coding
);
6767 coding
->charbuf_used
= carryover
;
6768 produce_chars (coding
, translation_table
, 1);
6771 coding
->carryover_bytes
= 0;
6772 if (coding
->consumed
< coding
->src_bytes
)
6774 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6775 const unsigned char *src
;
6777 coding_set_source (coding
);
6778 coding_set_destination (coding
);
6779 src
= coding
->source
+ coding
->consumed
;
6781 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6783 /* Flush out unprocessed data as binary chars. We are sure
6784 that the number of data is less than the size of
6786 coding
->charbuf_used
= 0;
6787 coding
->chars_at_source
= 0;
6789 while (nbytes
-- > 0)
6794 c
= BYTE8_TO_CHAR (c
);
6795 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6797 produce_chars (coding
, Qnil
, 1);
6801 /* Record unprocessed bytes in coding->carryover. We are
6802 sure that the number of data is less than the size of
6803 coding->carryover. */
6804 unsigned char *p
= coding
->carryover
;
6806 coding
->carryover_bytes
= nbytes
;
6807 while (nbytes
-- > 0)
6810 coding
->consumed
= coding
->src_bytes
;
6813 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6814 decode_eol (coding
);
6815 if (BUFFERP (coding
->dst_object
))
6817 current_buffer
->undo_list
= undo_list
;
6818 record_insert (coding
->dst_pos
, coding
->produced_char
);
6820 return coding
->result
;
6824 /* Extract an annotation datum from a composition starting at POS and
6825 ending before LIMIT of CODING->src_object (buffer or string), store
6826 the data in BUF, set *STOP to a starting position of the next
6827 composition (if any) or to LIMIT, and return the address of the
6828 next element of BUF.
6830 If such an annotation is not found, set *STOP to a starting
6831 position of a composition after POS (if any) or to LIMIT, and
6835 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6836 EMACS_INT pos
, limit
;
6837 struct coding_system
*coding
;
6841 EMACS_INT start
, end
;
6844 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6847 else if (start
> pos
)
6853 /* We found a composition. Store the corresponding
6854 annotation data in BUF. */
6856 enum composition_method method
= COMPOSITION_METHOD (prop
);
6857 int nchars
= COMPOSITION_LENGTH (prop
);
6859 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6860 if (method
!= COMPOSITION_RELATIVE
)
6862 Lisp_Object components
;
6865 components
= COMPOSITION_COMPONENTS (prop
);
6866 if (VECTORP (components
))
6868 len
= XVECTOR (components
)->size
;
6869 for (i
= 0; i
< len
; i
++)
6870 *buf
++ = XINT (AREF (components
, i
));
6872 else if (STRINGP (components
))
6874 len
= SCHARS (components
);
6878 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6882 else if (INTEGERP (components
))
6885 *buf
++ = XINT (components
);
6887 else if (CONSP (components
))
6889 for (len
= 0; CONSP (components
);
6890 len
++, components
= XCDR (components
))
6891 *buf
++ = XINT (XCAR (components
));
6899 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6910 /* Extract an annotation datum from a text property `charset' at POS of
6911 CODING->src_object (buffer of string), store the data in BUF, set
6912 *STOP to the position where the value of `charset' property changes
6913 (limiting by LIMIT), and return the address of the next element of
6916 If the property value is nil, set *STOP to the position where the
6917 property value is non-nil (limiting by LIMIT), and return BUF. */
6920 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6921 EMACS_INT pos
, limit
;
6922 struct coding_system
*coding
;
6926 Lisp_Object val
, next
;
6929 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6930 if (! NILP (val
) && CHARSETP (val
))
6931 id
= XINT (CHARSET_SYMBOL_ID (val
));
6934 ADD_CHARSET_DATA (buf
, 0, id
);
6935 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6937 make_number (limit
));
6938 *stop
= XINT (next
);
6944 consume_chars (coding
, translation_table
, max_lookup
)
6945 struct coding_system
*coding
;
6946 Lisp_Object translation_table
;
6949 int *buf
= coding
->charbuf
;
6950 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6951 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6952 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6953 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6954 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6955 int multibytep
= coding
->src_multibyte
;
6956 Lisp_Object eol_type
;
6958 EMACS_INT stop
, stop_composition
, stop_charset
;
6959 int *lookup_buf
= NULL
;
6961 if (! NILP (translation_table
))
6962 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6964 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6965 if (VECTORP (eol_type
))
6968 /* Note: composition handling is not yet implemented. */
6969 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6971 if (NILP (coding
->src_object
))
6972 stop
= stop_composition
= stop_charset
= end_pos
;
6975 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6976 stop
= stop_composition
= pos
;
6978 stop
= stop_composition
= end_pos
;
6979 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6980 stop
= stop_charset
= pos
;
6982 stop_charset
= end_pos
;
6985 /* Compensate for CRLF and conversion. */
6986 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6987 while (buf
< buf_end
)
6995 if (pos
== stop_composition
)
6996 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6997 buf
, &stop_composition
);
6998 if (pos
== stop_charset
)
6999 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7000 buf
, &stop_charset
);
7001 stop
= (stop_composition
< stop_charset
7002 ? stop_composition
: stop_charset
);
7009 if (coding
->encoder
== encode_coding_raw_text
)
7011 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7012 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7014 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7017 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7018 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7020 if (! EQ (eol_type
, Qunix
))
7024 if (EQ (eol_type
, Qdos
))
7032 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7037 int from_nchars
= 1, to_nchars
= 1;
7038 int *lookup_buf_end
;
7039 const unsigned char *p
= src
;
7043 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7044 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7045 lookup_buf_end
= lookup_buf
+ i
;
7046 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
7047 &from_nchars
, &to_nchars
);
7049 || buf
+ to_nchars
> buf_end
)
7051 *buf
++ = *lookup_buf
;
7052 for (i
= 1; i
< to_nchars
; i
++)
7053 *buf
++ = XINT (AREF (trans
, i
));
7054 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7055 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7059 coding
->consumed
= src
- coding
->source
;
7060 coding
->consumed_char
= pos
- coding
->src_pos
;
7061 coding
->charbuf_used
= buf
- coding
->charbuf
;
7062 coding
->chars_at_source
= 0;
7066 /* Encode the text at CODING->src_object into CODING->dst_object.
7067 CODING->src_object is a buffer or a string.
7068 CODING->dst_object is a buffer or nil.
7070 If CODING->src_object is a buffer, it must be the current buffer.
7071 In this case, if CODING->src_pos is positive, it is a position of
7072 the source text in the buffer, otherwise. the source text is in the
7073 gap area of the buffer, and coding->src_pos specifies the offset of
7074 the text from GPT (which must be the same as PT). If this is the
7075 same buffer as CODING->dst_object, CODING->src_pos must be
7076 negative and CODING should not have `pre-write-conversion'.
7078 If CODING->src_object is a string, CODING should not have
7079 `pre-write-conversion'.
7081 If CODING->dst_object is a buffer, the encoded data is inserted at
7082 the current point of that buffer.
7084 If CODING->dst_object is nil, the encoded data is placed at the
7085 memory area specified by CODING->destination. */
7088 encode_coding (coding
)
7089 struct coding_system
*coding
;
7092 Lisp_Object translation_table
;
7095 attrs
= CODING_ID_ATTRS (coding
->id
);
7096 if (coding
->encoder
== encode_coding_raw_text
)
7097 translation_table
= Qnil
, max_lookup
= 0;
7099 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7101 if (BUFFERP (coding
->dst_object
))
7103 set_buffer_internal (XBUFFER (coding
->dst_object
));
7104 coding
->dst_multibyte
7105 = ! NILP (current_buffer
->enable_multibyte_characters
);
7108 coding
->consumed
= coding
->consumed_char
= 0;
7109 coding
->produced
= coding
->produced_char
= 0;
7110 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7113 ALLOC_CONVERSION_WORK_AREA (coding
);
7116 coding_set_source (coding
);
7117 consume_chars (coding
, translation_table
, max_lookup
);
7118 coding_set_destination (coding
);
7119 (*(coding
->encoder
)) (coding
);
7120 } while (coding
->consumed_char
< coding
->src_chars
);
7122 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7123 insert_from_gap (coding
->produced_char
, coding
->produced
);
7125 return (coding
->result
);
7129 /* Name (or base name) of work buffer for code conversion. */
7130 static Lisp_Object Vcode_conversion_workbuf_name
;
7132 /* A working buffer used by the top level conversion. Once it is
7133 created, it is never destroyed. It has the name
7134 Vcode_conversion_workbuf_name. The other working buffers are
7135 destroyed after the use is finished, and their names are modified
7136 versions of Vcode_conversion_workbuf_name. */
7137 static Lisp_Object Vcode_conversion_reused_workbuf
;
7139 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7140 static int reused_workbuf_in_use
;
7143 /* Return a working buffer of code convesion. MULTIBYTE specifies the
7144 multibyteness of returning buffer. */
7147 make_conversion_work_buffer (multibyte
)
7150 Lisp_Object name
, workbuf
;
7151 struct buffer
*current
;
7153 if (reused_workbuf_in_use
++)
7155 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7156 workbuf
= Fget_buffer_create (name
);
7160 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7161 Vcode_conversion_reused_workbuf
7162 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7163 workbuf
= Vcode_conversion_reused_workbuf
;
7165 current
= current_buffer
;
7166 set_buffer_internal (XBUFFER (workbuf
));
7167 /* We can't allow modification hooks to run in the work buffer. For
7168 instance, directory_files_internal assumes that file decoding
7169 doesn't compile new regexps. */
7170 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7172 current_buffer
->undo_list
= Qt
;
7173 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
7174 set_buffer_internal (current
);
7180 code_conversion_restore (arg
)
7183 Lisp_Object current
, workbuf
;
7184 struct gcpro gcpro1
;
7187 current
= XCAR (arg
);
7188 workbuf
= XCDR (arg
);
7189 if (! NILP (workbuf
))
7191 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7192 reused_workbuf_in_use
= 0;
7193 else if (! NILP (Fbuffer_live_p (workbuf
)))
7194 Fkill_buffer (workbuf
);
7196 set_buffer_internal (XBUFFER (current
));
7202 code_conversion_save (with_work_buf
, multibyte
)
7203 int with_work_buf
, multibyte
;
7205 Lisp_Object workbuf
= Qnil
;
7208 workbuf
= make_conversion_work_buffer (multibyte
);
7209 record_unwind_protect (code_conversion_restore
,
7210 Fcons (Fcurrent_buffer (), workbuf
));
7215 decode_coding_gap (coding
, chars
, bytes
)
7216 struct coding_system
*coding
;
7217 EMACS_INT chars
, bytes
;
7219 int count
= specpdl_ptr
- specpdl
;
7222 code_conversion_save (0, 0);
7224 coding
->src_object
= Fcurrent_buffer ();
7225 coding
->src_chars
= chars
;
7226 coding
->src_bytes
= bytes
;
7227 coding
->src_pos
= -chars
;
7228 coding
->src_pos_byte
= -bytes
;
7229 coding
->src_multibyte
= chars
< bytes
;
7230 coding
->dst_object
= coding
->src_object
;
7231 coding
->dst_pos
= PT
;
7232 coding
->dst_pos_byte
= PT_BYTE
;
7233 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
7235 if (CODING_REQUIRE_DETECTION (coding
))
7236 detect_coding (coding
);
7238 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7239 current_buffer
->text
->inhibit_shrinking
= 1;
7240 decode_coding (coding
);
7241 current_buffer
->text
->inhibit_shrinking
= 0;
7243 attrs
= CODING_ID_ATTRS (coding
->id
);
7244 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7246 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7249 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7250 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7251 make_number (coding
->produced_char
));
7253 coding
->produced_char
+= Z
- prev_Z
;
7254 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7257 unbind_to (count
, Qnil
);
7258 return coding
->result
;
7262 encode_coding_gap (coding
, chars
, bytes
)
7263 struct coding_system
*coding
;
7264 EMACS_INT chars
, bytes
;
7266 int count
= specpdl_ptr
- specpdl
;
7268 code_conversion_save (0, 0);
7270 coding
->src_object
= Fcurrent_buffer ();
7271 coding
->src_chars
= chars
;
7272 coding
->src_bytes
= bytes
;
7273 coding
->src_pos
= -chars
;
7274 coding
->src_pos_byte
= -bytes
;
7275 coding
->src_multibyte
= chars
< bytes
;
7276 coding
->dst_object
= coding
->src_object
;
7277 coding
->dst_pos
= PT
;
7278 coding
->dst_pos_byte
= PT_BYTE
;
7280 encode_coding (coding
);
7282 unbind_to (count
, Qnil
);
7283 return coding
->result
;
7287 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7288 SRC_OBJECT into DST_OBJECT by coding context CODING.
7290 SRC_OBJECT is a buffer, a string, or Qnil.
7292 If it is a buffer, the text is at point of the buffer. FROM and TO
7293 are positions in the buffer.
7295 If it is a string, the text is at the beginning of the string.
7296 FROM and TO are indices to the string.
7298 If it is nil, the text is at coding->source. FROM and TO are
7299 indices to coding->source.
7301 DST_OBJECT is a buffer, Qt, or Qnil.
7303 If it is a buffer, the decoded text is inserted at point of the
7304 buffer. If the buffer is the same as SRC_OBJECT, the source text
7307 If it is Qt, a string is made from the decoded text, and
7308 set in CODING->dst_object.
7310 If it is Qnil, the decoded text is stored at CODING->destination.
7311 The caller must allocate CODING->dst_bytes bytes at
7312 CODING->destination by xmalloc. If the decoded text is longer than
7313 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7317 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7319 struct coding_system
*coding
;
7320 Lisp_Object src_object
;
7321 EMACS_INT from
, from_byte
, to
, to_byte
;
7322 Lisp_Object dst_object
;
7324 int count
= specpdl_ptr
- specpdl
;
7325 unsigned char *destination
;
7326 EMACS_INT dst_bytes
;
7327 EMACS_INT chars
= to
- from
;
7328 EMACS_INT bytes
= to_byte
- from_byte
;
7330 int saved_pt
= -1, saved_pt_byte
;
7331 int need_marker_adjustment
= 0;
7332 Lisp_Object old_deactivate_mark
;
7334 old_deactivate_mark
= Vdeactivate_mark
;
7336 if (NILP (dst_object
))
7338 destination
= coding
->destination
;
7339 dst_bytes
= coding
->dst_bytes
;
7342 coding
->src_object
= src_object
;
7343 coding
->src_chars
= chars
;
7344 coding
->src_bytes
= bytes
;
7345 coding
->src_multibyte
= chars
< bytes
;
7347 if (STRINGP (src_object
))
7349 coding
->src_pos
= from
;
7350 coding
->src_pos_byte
= from_byte
;
7352 else if (BUFFERP (src_object
))
7354 set_buffer_internal (XBUFFER (src_object
));
7356 move_gap_both (from
, from_byte
);
7357 if (EQ (src_object
, dst_object
))
7359 struct Lisp_Marker
*tail
;
7361 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7363 tail
->need_adjustment
7364 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7365 need_marker_adjustment
|= tail
->need_adjustment
;
7367 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7368 TEMP_SET_PT_BOTH (from
, from_byte
);
7369 current_buffer
->text
->inhibit_shrinking
= 1;
7370 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7371 coding
->src_pos
= -chars
;
7372 coding
->src_pos_byte
= -bytes
;
7376 coding
->src_pos
= from
;
7377 coding
->src_pos_byte
= from_byte
;
7381 if (CODING_REQUIRE_DETECTION (coding
))
7382 detect_coding (coding
);
7383 attrs
= CODING_ID_ATTRS (coding
->id
);
7385 if (EQ (dst_object
, Qt
)
7386 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7387 && NILP (dst_object
)))
7389 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7390 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7391 coding
->dst_pos
= BEG
;
7392 coding
->dst_pos_byte
= BEG_BYTE
;
7394 else if (BUFFERP (dst_object
))
7396 code_conversion_save (0, 0);
7397 coding
->dst_object
= dst_object
;
7398 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7399 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7400 coding
->dst_multibyte
7401 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7405 code_conversion_save (0, 0);
7406 coding
->dst_object
= Qnil
;
7407 /* Most callers presume this will return a multibyte result, and they
7408 won't use `binary' or `raw-text' anyway, so let's not worry about
7409 CODING_FOR_UNIBYTE. */
7410 coding
->dst_multibyte
= 1;
7413 decode_coding (coding
);
7415 if (BUFFERP (coding
->dst_object
))
7416 set_buffer_internal (XBUFFER (coding
->dst_object
));
7418 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7420 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7421 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7424 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7425 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7426 old_deactivate_mark
);
7427 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7428 make_number (coding
->produced_char
));
7431 coding
->produced_char
+= Z
- prev_Z
;
7432 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7435 if (EQ (dst_object
, Qt
))
7437 coding
->dst_object
= Fbuffer_string ();
7439 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7441 set_buffer_internal (XBUFFER (coding
->dst_object
));
7442 if (dst_bytes
< coding
->produced
)
7444 destination
= xrealloc (destination
, coding
->produced
);
7447 record_conversion_result (coding
,
7448 CODING_RESULT_INSUFFICIENT_DST
);
7449 unbind_to (count
, Qnil
);
7452 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7453 move_gap_both (BEGV
, BEGV_BYTE
);
7454 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
7455 coding
->destination
= destination
;
7461 /* This is the case of:
7462 (BUFFERP (src_object) && EQ (src_object, dst_object))
7463 As we have moved PT while replacing the original buffer
7464 contents, we must recover it now. */
7465 set_buffer_internal (XBUFFER (src_object
));
7466 current_buffer
->text
->inhibit_shrinking
= 0;
7467 if (saved_pt
< from
)
7468 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7469 else if (saved_pt
< from
+ chars
)
7470 TEMP_SET_PT_BOTH (from
, from_byte
);
7471 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7472 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7473 saved_pt_byte
+ (coding
->produced
- bytes
));
7475 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7476 saved_pt_byte
+ (coding
->produced
- bytes
));
7478 if (need_marker_adjustment
)
7480 struct Lisp_Marker
*tail
;
7482 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7483 if (tail
->need_adjustment
)
7485 tail
->need_adjustment
= 0;
7486 if (tail
->insertion_type
)
7488 tail
->bytepos
= from_byte
;
7489 tail
->charpos
= from
;
7493 tail
->bytepos
= from_byte
+ coding
->produced
;
7495 = (NILP (current_buffer
->enable_multibyte_characters
)
7496 ? tail
->bytepos
: from
+ coding
->produced_char
);
7502 Vdeactivate_mark
= old_deactivate_mark
;
7503 unbind_to (count
, coding
->dst_object
);
7508 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7510 struct coding_system
*coding
;
7511 Lisp_Object src_object
;
7512 EMACS_INT from
, from_byte
, to
, to_byte
;
7513 Lisp_Object dst_object
;
7515 int count
= specpdl_ptr
- specpdl
;
7516 EMACS_INT chars
= to
- from
;
7517 EMACS_INT bytes
= to_byte
- from_byte
;
7519 int saved_pt
= -1, saved_pt_byte
;
7520 int need_marker_adjustment
= 0;
7521 int kill_src_buffer
= 0;
7522 Lisp_Object old_deactivate_mark
;
7524 old_deactivate_mark
= Vdeactivate_mark
;
7526 coding
->src_object
= src_object
;
7527 coding
->src_chars
= chars
;
7528 coding
->src_bytes
= bytes
;
7529 coding
->src_multibyte
= chars
< bytes
;
7531 attrs
= CODING_ID_ATTRS (coding
->id
);
7533 if (EQ (src_object
, dst_object
))
7535 struct Lisp_Marker
*tail
;
7537 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7539 tail
->need_adjustment
7540 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7541 need_marker_adjustment
|= tail
->need_adjustment
;
7545 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7547 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7548 set_buffer_internal (XBUFFER (coding
->src_object
));
7549 if (STRINGP (src_object
))
7550 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7551 else if (BUFFERP (src_object
))
7552 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7554 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7556 if (EQ (src_object
, dst_object
))
7558 set_buffer_internal (XBUFFER (src_object
));
7559 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7560 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7561 set_buffer_internal (XBUFFER (coding
->src_object
));
7565 Lisp_Object args
[3];
7566 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7568 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7569 old_deactivate_mark
);
7570 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7571 args
[1] = make_number (BEG
);
7572 args
[2] = make_number (Z
);
7573 safe_call (3, args
);
7576 if (XBUFFER (coding
->src_object
) != current_buffer
)
7577 kill_src_buffer
= 1;
7578 coding
->src_object
= Fcurrent_buffer ();
7580 move_gap_both (BEG
, BEG_BYTE
);
7581 coding
->src_chars
= Z
- BEG
;
7582 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7583 coding
->src_pos
= BEG
;
7584 coding
->src_pos_byte
= BEG_BYTE
;
7585 coding
->src_multibyte
= Z
< Z_BYTE
;
7587 else if (STRINGP (src_object
))
7589 code_conversion_save (0, 0);
7590 coding
->src_pos
= from
;
7591 coding
->src_pos_byte
= from_byte
;
7593 else if (BUFFERP (src_object
))
7595 code_conversion_save (0, 0);
7596 set_buffer_internal (XBUFFER (src_object
));
7597 if (EQ (src_object
, dst_object
))
7599 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7600 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7601 coding
->src_pos
= 0;
7602 coding
->src_pos_byte
= 0;
7606 if (from
< GPT
&& to
>= GPT
)
7607 move_gap_both (from
, from_byte
);
7608 coding
->src_pos
= from
;
7609 coding
->src_pos_byte
= from_byte
;
7613 code_conversion_save (0, 0);
7615 if (BUFFERP (dst_object
))
7617 coding
->dst_object
= dst_object
;
7618 if (EQ (src_object
, dst_object
))
7620 coding
->dst_pos
= from
;
7621 coding
->dst_pos_byte
= from_byte
;
7625 struct buffer
*current
= current_buffer
;
7627 set_buffer_temp (XBUFFER (dst_object
));
7628 coding
->dst_pos
= PT
;
7629 coding
->dst_pos_byte
= PT_BYTE
;
7630 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7631 set_buffer_temp (current
);
7633 coding
->dst_multibyte
7634 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7636 else if (EQ (dst_object
, Qt
))
7638 coding
->dst_object
= Qnil
;
7639 coding
->dst_bytes
= coding
->src_chars
;
7640 if (coding
->dst_bytes
== 0)
7641 coding
->dst_bytes
= 1;
7642 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7643 coding
->dst_multibyte
= 0;
7647 coding
->dst_object
= Qnil
;
7648 coding
->dst_multibyte
= 0;
7651 encode_coding (coding
);
7653 if (EQ (dst_object
, Qt
))
7655 if (BUFFERP (coding
->dst_object
))
7656 coding
->dst_object
= Fbuffer_string ();
7660 = make_unibyte_string ((char *) coding
->destination
,
7662 xfree (coding
->destination
);
7668 /* This is the case of:
7669 (BUFFERP (src_object) && EQ (src_object, dst_object))
7670 As we have moved PT while replacing the original buffer
7671 contents, we must recover it now. */
7672 set_buffer_internal (XBUFFER (src_object
));
7673 if (saved_pt
< from
)
7674 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7675 else if (saved_pt
< from
+ chars
)
7676 TEMP_SET_PT_BOTH (from
, from_byte
);
7677 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7678 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7679 saved_pt_byte
+ (coding
->produced
- bytes
));
7681 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7682 saved_pt_byte
+ (coding
->produced
- bytes
));
7684 if (need_marker_adjustment
)
7686 struct Lisp_Marker
*tail
;
7688 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7689 if (tail
->need_adjustment
)
7691 tail
->need_adjustment
= 0;
7692 if (tail
->insertion_type
)
7694 tail
->bytepos
= from_byte
;
7695 tail
->charpos
= from
;
7699 tail
->bytepos
= from_byte
+ coding
->produced
;
7701 = (NILP (current_buffer
->enable_multibyte_characters
)
7702 ? tail
->bytepos
: from
+ coding
->produced_char
);
7708 if (kill_src_buffer
)
7709 Fkill_buffer (coding
->src_object
);
7711 Vdeactivate_mark
= old_deactivate_mark
;
7712 unbind_to (count
, Qnil
);
7717 preferred_coding_system ()
7719 int id
= coding_categories
[coding_priorities
[0]].id
;
7721 return CODING_ID_NAME (id
);
7726 /*** 8. Emacs Lisp library functions ***/
7728 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7729 doc
: /* Return t if OBJECT is nil or a coding-system.
7730 See the documentation of `define-coding-system' for information
7731 about coding-system objects. */)
7736 || CODING_SYSTEM_ID (object
) >= 0)
7738 if (! SYMBOLP (object
)
7739 || NILP (Fget (object
, Qcoding_system_define_form
)))
7744 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7745 Sread_non_nil_coding_system
, 1, 1, 0,
7746 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7753 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7754 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7756 while (SCHARS (val
) == 0);
7757 return (Fintern (val
, Qnil
));
7760 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7761 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7762 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
7763 Ignores case when completing coding systems (all Emacs coding systems
7764 are lower-case). */)
7765 (prompt
, default_coding_system
)
7766 Lisp_Object prompt
, default_coding_system
;
7769 int count
= SPECPDL_INDEX ();
7771 if (SYMBOLP (default_coding_system
))
7772 default_coding_system
= SYMBOL_NAME (default_coding_system
);
7773 specbind (Qcompletion_ignore_case
, Qt
);
7774 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7775 Qt
, Qnil
, Qcoding_system_history
,
7776 default_coding_system
, Qnil
);
7777 unbind_to (count
, Qnil
);
7778 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7781 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7783 doc
: /* Check validity of CODING-SYSTEM.
7784 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7785 It is valid if it is nil or a symbol defined as a coding system by the
7786 function `define-coding-system'. */)
7788 Lisp_Object coding_system
;
7790 Lisp_Object define_form
;
7792 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7793 if (! NILP (define_form
))
7795 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7796 safe_eval (define_form
);
7798 if (!NILP (Fcoding_system_p (coding_system
)))
7799 return coding_system
;
7800 xsignal1 (Qcoding_system_error
, coding_system
);
7804 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7805 HIGHEST is nonzero, return the coding system of the highest
7806 priority among the detected coding systems. Otherwize return a
7807 list of detected coding systems sorted by their priorities. If
7808 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7809 multibyte form but contains only ASCII and eight-bit chars.
7810 Otherwise, the bytes are raw bytes.
7812 CODING-SYSTEM controls the detection as below:
7814 If it is nil, detect both text-format and eol-format. If the
7815 text-format part of CODING-SYSTEM is already specified
7816 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7817 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7818 detect only text-format. */
7821 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7823 const unsigned char *src
;
7824 EMACS_INT src_chars
, src_bytes
;
7827 Lisp_Object coding_system
;
7829 const unsigned char *src_end
= src
+ src_bytes
;
7830 Lisp_Object attrs
, eol_type
;
7831 Lisp_Object val
= Qnil
;
7832 struct coding_system coding
;
7834 struct coding_detection_info detect_info
;
7835 enum coding_category base_category
;
7836 int null_byte_found
= 0, eight_bit_found
= 0;
7838 if (NILP (coding_system
))
7839 coding_system
= Qundecided
;
7840 setup_coding_system (coding_system
, &coding
);
7841 attrs
= CODING_ID_ATTRS (coding
.id
);
7842 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7843 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7845 coding
.source
= src
;
7846 coding
.src_chars
= src_chars
;
7847 coding
.src_bytes
= src_bytes
;
7848 coding
.src_multibyte
= multibytep
;
7849 coding
.consumed
= 0;
7850 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7851 coding
.head_ascii
= 0;
7853 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7855 /* At first, detect text-format if necessary. */
7856 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7857 if (base_category
== coding_category_undecided
)
7859 enum coding_category category
;
7860 struct coding_system
*this;
7863 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7864 for (; src
< src_end
; src
++)
7869 eight_bit_found
= 1;
7870 if (null_byte_found
)
7875 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7876 && ! inhibit_iso_escape_detection
7877 && ! detect_info
.checked
)
7879 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7881 /* We have scanned the whole data. */
7882 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7884 /* We didn't find an 8-bit code. We may
7885 have found a null-byte, but it's very
7886 rare that a binary file confirm to
7889 coding
.head_ascii
= src
- coding
.source
;
7891 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
7895 else if (! c
&& !inhibit_null_byte_detection
)
7897 null_byte_found
= 1;
7898 if (eight_bit_found
)
7901 if (! eight_bit_found
)
7902 coding
.head_ascii
++;
7904 else if (! eight_bit_found
)
7905 coding
.head_ascii
++;
7908 if (null_byte_found
|| eight_bit_found
7909 || coding
.head_ascii
< coding
.src_bytes
7910 || detect_info
.found
)
7912 if (coding
.head_ascii
== coding
.src_bytes
)
7913 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7914 for (i
= 0; i
< coding_category_raw_text
; i
++)
7916 category
= coding_priorities
[i
];
7917 this = coding_categories
+ category
;
7918 if (detect_info
.found
& (1 << category
))
7923 if (null_byte_found
)
7925 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
7926 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
7928 for (i
= 0; i
< coding_category_raw_text
; i
++)
7930 category
= coding_priorities
[i
];
7931 this = coding_categories
+ category
;
7935 /* No coding system of this category is defined. */
7936 detect_info
.rejected
|= (1 << category
);
7938 else if (category
>= coding_category_raw_text
)
7940 else if (detect_info
.checked
& (1 << category
))
7943 && (detect_info
.found
& (1 << category
)))
7946 else if ((*(this->detector
)) (&coding
, &detect_info
)
7948 && (detect_info
.found
& (1 << category
)))
7950 if (category
== coding_category_utf_16_auto
)
7952 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7953 category
= coding_category_utf_16_le
;
7955 category
= coding_category_utf_16_be
;
7963 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
7966 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7967 id
= CODING_SYSTEM_ID (Qno_conversion
);
7968 val
= Fcons (make_number (id
), Qnil
);
7970 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7972 detect_info
.found
= CATEGORY_MASK_ANY
;
7973 id
= coding_categories
[coding_category_undecided
].id
;
7974 val
= Fcons (make_number (id
), Qnil
);
7978 if (detect_info
.found
)
7980 detect_info
.found
= 1 << category
;
7981 val
= Fcons (make_number (this->id
), Qnil
);
7984 for (i
= 0; i
< coding_category_raw_text
; i
++)
7985 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7987 detect_info
.found
= 1 << coding_priorities
[i
];
7988 id
= coding_categories
[coding_priorities
[i
]].id
;
7989 val
= Fcons (make_number (id
), Qnil
);
7995 int mask
= detect_info
.rejected
| detect_info
.found
;
7998 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8000 category
= coding_priorities
[i
];
8001 if (! (mask
& (1 << category
)))
8003 found
|= 1 << category
;
8004 id
= coding_categories
[category
].id
;
8006 val
= Fcons (make_number (id
), val
);
8009 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8011 category
= coding_priorities
[i
];
8012 if (detect_info
.found
& (1 << category
))
8014 id
= coding_categories
[category
].id
;
8015 val
= Fcons (make_number (id
), val
);
8018 detect_info
.found
|= found
;
8021 else if (base_category
== coding_category_utf_8_auto
)
8023 if (detect_coding_utf_8 (&coding
, &detect_info
))
8025 struct coding_system
*this;
8027 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8028 this = coding_categories
+ coding_category_utf_8_sig
;
8030 this = coding_categories
+ coding_category_utf_8_nosig
;
8031 val
= Fcons (make_number (this->id
), Qnil
);
8034 else if (base_category
== coding_category_utf_16_auto
)
8036 if (detect_coding_utf_16 (&coding
, &detect_info
))
8038 struct coding_system
*this;
8040 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8041 this = coding_categories
+ coding_category_utf_16_le
;
8042 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8043 this = coding_categories
+ coding_category_utf_16_be
;
8044 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8045 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8047 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8048 val
= Fcons (make_number (this->id
), Qnil
);
8053 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8054 val
= Fcons (make_number (coding
.id
), Qnil
);
8057 /* Then, detect eol-format if necessary. */
8059 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8062 if (VECTORP (eol_type
))
8064 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8066 if (null_byte_found
)
8067 normal_eol
= EOL_SEEN_LF
;
8069 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8070 coding_category_raw_text
);
8072 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8073 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8074 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8075 coding_category_utf_16_be
);
8076 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8077 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8078 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8079 coding_category_utf_16_le
);
8083 if (EQ (eol_type
, Qunix
))
8084 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8085 else if (EQ (eol_type
, Qdos
))
8086 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8088 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8091 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8093 enum coding_category category
;
8096 id
= XINT (XCAR (tail
));
8097 attrs
= CODING_ID_ATTRS (id
);
8098 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8099 eol_type
= CODING_ID_EOL_TYPE (id
);
8100 if (VECTORP (eol_type
))
8102 if (category
== coding_category_utf_16_be
8103 || category
== coding_category_utf_16_be_nosig
)
8104 this_eol
= utf_16_be_eol
;
8105 else if (category
== coding_category_utf_16_le
8106 || category
== coding_category_utf_16_le_nosig
)
8107 this_eol
= utf_16_le_eol
;
8109 this_eol
= normal_eol
;
8111 if (this_eol
== EOL_SEEN_LF
)
8112 XSETCAR (tail
, AREF (eol_type
, 0));
8113 else if (this_eol
== EOL_SEEN_CRLF
)
8114 XSETCAR (tail
, AREF (eol_type
, 1));
8115 else if (this_eol
== EOL_SEEN_CR
)
8116 XSETCAR (tail
, AREF (eol_type
, 2));
8118 XSETCAR (tail
, CODING_ID_NAME (id
));
8121 XSETCAR (tail
, CODING_ID_NAME (id
));
8125 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8129 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8131 doc
: /* Detect coding system of the text in the region between START and END.
8132 Return a list of possible coding systems ordered by priority.
8134 If only ASCII characters are found (except for such ISO-2022 control
8135 characters as ESC), it returns a list of single element `undecided'
8136 or its subsidiary coding system according to a detected end-of-line
8139 If optional argument HIGHEST is non-nil, return the coding system of
8140 highest priority. */)
8141 (start
, end
, highest
)
8142 Lisp_Object start
, end
, highest
;
8145 int from_byte
, to_byte
;
8147 CHECK_NUMBER_COERCE_MARKER (start
);
8148 CHECK_NUMBER_COERCE_MARKER (end
);
8150 validate_region (&start
, &end
);
8151 from
= XINT (start
), to
= XINT (end
);
8152 from_byte
= CHAR_TO_BYTE (from
);
8153 to_byte
= CHAR_TO_BYTE (to
);
8155 if (from
< GPT
&& to
>= GPT
)
8156 move_gap_both (to
, to_byte
);
8158 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8159 to
- from
, to_byte
- from_byte
,
8161 !NILP (current_buffer
8162 ->enable_multibyte_characters
),
8166 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8168 doc
: /* Detect coding system of the text in STRING.
8169 Return a list of possible coding systems ordered by priority.
8171 If only ASCII characters are found (except for such ISO-2022 control
8172 characters as ESC), it returns a list of single element `undecided'
8173 or its subsidiary coding system according to a detected end-of-line
8176 If optional argument HIGHEST is non-nil, return the coding system of
8177 highest priority. */)
8179 Lisp_Object string
, highest
;
8181 CHECK_STRING (string
);
8183 return detect_coding_system (SDATA (string
),
8184 SCHARS (string
), SBYTES (string
),
8185 !NILP (highest
), STRING_MULTIBYTE (string
),
8191 char_encodable_p (c
, attrs
)
8196 struct charset
*charset
;
8197 Lisp_Object translation_table
;
8199 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8200 if (! NILP (translation_table
))
8201 c
= translate_char (translation_table
, c
);
8202 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8203 CONSP (tail
); tail
= XCDR (tail
))
8205 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8206 if (CHAR_CHARSET_P (c
, charset
))
8209 return (! NILP (tail
));
8213 /* Return a list of coding systems that safely encode the text between
8214 START and END. If EXCLUDE is non-nil, it is a list of coding
8215 systems not to check. The returned list doesn't contain any such
8216 coding systems. In any case, if the text contains only ASCII or is
8217 unibyte, return t. */
8219 DEFUN ("find-coding-systems-region-internal",
8220 Ffind_coding_systems_region_internal
,
8221 Sfind_coding_systems_region_internal
, 2, 3, 0,
8222 doc
: /* Internal use only. */)
8223 (start
, end
, exclude
)
8224 Lisp_Object start
, end
, exclude
;
8226 Lisp_Object coding_attrs_list
, safe_codings
;
8227 EMACS_INT start_byte
, end_byte
;
8228 const unsigned char *p
, *pbeg
, *pend
;
8230 Lisp_Object tail
, elt
;
8232 if (STRINGP (start
))
8234 if (!STRING_MULTIBYTE (start
)
8235 || SCHARS (start
) == SBYTES (start
))
8238 end_byte
= SBYTES (start
);
8242 CHECK_NUMBER_COERCE_MARKER (start
);
8243 CHECK_NUMBER_COERCE_MARKER (end
);
8244 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8245 args_out_of_range (start
, end
);
8246 if (NILP (current_buffer
->enable_multibyte_characters
))
8248 start_byte
= CHAR_TO_BYTE (XINT (start
));
8249 end_byte
= CHAR_TO_BYTE (XINT (end
));
8250 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8253 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8255 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8256 move_gap_both (XINT (start
), start_byte
);
8258 move_gap_both (XINT (end
), end_byte
);
8262 coding_attrs_list
= Qnil
;
8263 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8265 || NILP (Fmemq (XCAR (tail
), exclude
)))
8269 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8270 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8271 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8273 ASET (attrs
, coding_attr_trans_tbl
,
8274 get_translation_table (attrs
, 1, NULL
));
8275 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8279 if (STRINGP (start
))
8280 p
= pbeg
= SDATA (start
);
8282 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8283 pend
= p
+ (end_byte
- start_byte
);
8285 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8286 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8290 if (ASCII_BYTE_P (*p
))
8294 c
= STRING_CHAR_ADVANCE (p
);
8296 charset_map_loaded
= 0;
8297 for (tail
= coding_attrs_list
; CONSP (tail
);)
8302 else if (char_encodable_p (c
, elt
))
8304 else if (CONSP (XCDR (tail
)))
8306 XSETCAR (tail
, XCAR (XCDR (tail
)));
8307 XSETCDR (tail
, XCDR (XCDR (tail
)));
8311 XSETCAR (tail
, Qnil
);
8315 if (charset_map_loaded
)
8317 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8319 if (STRINGP (start
))
8320 pbeg
= SDATA (start
);
8322 pbeg
= BYTE_POS_ADDR (start_byte
);
8323 p
= pbeg
+ p_offset
;
8324 pend
= pbeg
+ pend_offset
;
8329 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8330 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8331 if (! NILP (XCAR (tail
)))
8332 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8334 return safe_codings
;
8338 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8339 Sunencodable_char_position
, 3, 5, 0,
8341 Return position of first un-encodable character in a region.
8342 START and END specify the region and CODING-SYSTEM specifies the
8343 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8345 If optional 4th argument COUNT is non-nil, it specifies at most how
8346 many un-encodable characters to search. In this case, the value is a
8349 If optional 5th argument STRING is non-nil, it is a string to search
8350 for un-encodable characters. In that case, START and END are indexes
8352 (start
, end
, coding_system
, count
, string
)
8353 Lisp_Object start
, end
, coding_system
, count
, string
;
8356 struct coding_system coding
;
8357 Lisp_Object attrs
, charset_list
, translation_table
;
8358 Lisp_Object positions
;
8360 const unsigned char *p
, *stop
, *pend
;
8361 int ascii_compatible
;
8363 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8364 attrs
= CODING_ID_ATTRS (coding
.id
);
8365 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8367 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8368 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8369 translation_table
= get_translation_table (attrs
, 1, NULL
);
8373 validate_region (&start
, &end
);
8374 from
= XINT (start
);
8376 if (NILP (current_buffer
->enable_multibyte_characters
)
8377 || (ascii_compatible
8378 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8380 p
= CHAR_POS_ADDR (from
);
8381 pend
= CHAR_POS_ADDR (to
);
8382 if (from
< GPT
&& to
>= GPT
)
8389 CHECK_STRING (string
);
8390 CHECK_NATNUM (start
);
8392 from
= XINT (start
);
8395 || to
> SCHARS (string
))
8396 args_out_of_range_3 (string
, start
, end
);
8397 if (! STRING_MULTIBYTE (string
))
8399 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8400 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8401 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8409 CHECK_NATNUM (count
);
8418 if (ascii_compatible
)
8419 while (p
< stop
&& ASCII_BYTE_P (*p
))
8429 c
= STRING_CHAR_ADVANCE (p
);
8430 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8431 && ! char_charset (translate_char (translation_table
, c
),
8432 charset_list
, NULL
))
8434 positions
= Fcons (make_number (from
), positions
);
8443 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8447 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8448 Scheck_coding_systems_region
, 3, 3, 0,
8449 doc
: /* Check if the region is encodable by coding systems.
8451 START and END are buffer positions specifying the region.
8452 CODING-SYSTEM-LIST is a list of coding systems to check.
8454 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8455 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8456 whole region, POS0, POS1, ... are buffer positions where non-encodable
8457 characters are found.
8459 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8462 START may be a string. In that case, check if the string is
8463 encodable, and the value contains indices to the string instead of
8464 buffer positions. END is ignored. */)
8465 (start
, end
, coding_system_list
)
8466 Lisp_Object start
, end
, coding_system_list
;
8469 EMACS_INT start_byte
, end_byte
;
8471 const unsigned char *p
, *pbeg
, *pend
;
8473 Lisp_Object tail
, elt
, attrs
;
8475 if (STRINGP (start
))
8477 if (!STRING_MULTIBYTE (start
)
8478 && SCHARS (start
) != SBYTES (start
))
8481 end_byte
= SBYTES (start
);
8486 CHECK_NUMBER_COERCE_MARKER (start
);
8487 CHECK_NUMBER_COERCE_MARKER (end
);
8488 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8489 args_out_of_range (start
, end
);
8490 if (NILP (current_buffer
->enable_multibyte_characters
))
8492 start_byte
= CHAR_TO_BYTE (XINT (start
));
8493 end_byte
= CHAR_TO_BYTE (XINT (end
));
8494 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8497 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8499 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8500 move_gap_both (XINT (start
), start_byte
);
8502 move_gap_both (XINT (end
), end_byte
);
8508 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8511 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8512 ASET (attrs
, coding_attr_trans_tbl
,
8513 get_translation_table (attrs
, 1, NULL
));
8514 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8517 if (STRINGP (start
))
8518 p
= pbeg
= SDATA (start
);
8520 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8521 pend
= p
+ (end_byte
- start_byte
);
8523 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8524 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8528 if (ASCII_BYTE_P (*p
))
8532 c
= STRING_CHAR_ADVANCE (p
);
8534 charset_map_loaded
= 0;
8535 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8537 elt
= XCDR (XCAR (tail
));
8538 if (! char_encodable_p (c
, XCAR (elt
)))
8539 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8541 if (charset_map_loaded
)
8543 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8545 if (STRINGP (start
))
8546 pbeg
= SDATA (start
);
8548 pbeg
= BYTE_POS_ADDR (start_byte
);
8549 p
= pbeg
+ p_offset
;
8550 pend
= pbeg
+ pend_offset
;
8558 for (; CONSP (tail
); tail
= XCDR (tail
))
8561 if (CONSP (XCDR (XCDR (elt
))))
8562 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8571 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
8572 Lisp_Object start
, end
, coding_system
, dst_object
;
8573 int encodep
, norecord
;
8575 struct coding_system coding
;
8576 EMACS_INT from
, from_byte
, to
, to_byte
;
8577 Lisp_Object src_object
;
8579 CHECK_NUMBER_COERCE_MARKER (start
);
8580 CHECK_NUMBER_COERCE_MARKER (end
);
8581 if (NILP (coding_system
))
8582 coding_system
= Qno_conversion
;
8584 CHECK_CODING_SYSTEM (coding_system
);
8585 src_object
= Fcurrent_buffer ();
8586 if (NILP (dst_object
))
8587 dst_object
= src_object
;
8588 else if (! EQ (dst_object
, Qt
))
8589 CHECK_BUFFER (dst_object
);
8591 validate_region (&start
, &end
);
8592 from
= XFASTINT (start
);
8593 from_byte
= CHAR_TO_BYTE (from
);
8594 to
= XFASTINT (end
);
8595 to_byte
= CHAR_TO_BYTE (to
);
8597 setup_coding_system (coding_system
, &coding
);
8598 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8601 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8604 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8607 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8609 return (BUFFERP (dst_object
)
8610 ? make_number (coding
.produced_char
)
8611 : coding
.dst_object
);
8615 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8616 3, 4, "r\nzCoding system: ",
8617 doc
: /* Decode the current region from the specified coding system.
8618 When called from a program, takes four arguments:
8619 START, END, CODING-SYSTEM, and DESTINATION.
8620 START and END are buffer positions.
8622 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8623 If nil, the region between START and END is replaced by the decoded text.
8624 If buffer, the decoded text is inserted in that buffer after point (point
8626 In those cases, the length of the decoded text is returned.
8627 If DESTINATION is t, the decoded text is returned.
8629 This function sets `last-coding-system-used' to the precise coding system
8630 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8631 not fully specified.) */)
8632 (start
, end
, coding_system
, destination
)
8633 Lisp_Object start
, end
, coding_system
, destination
;
8635 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8638 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8639 3, 4, "r\nzCoding system: ",
8640 doc
: /* Encode the current region by specified coding system.
8641 When called from a program, takes four arguments:
8642 START, END, CODING-SYSTEM and DESTINATION.
8643 START and END are buffer positions.
8645 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8646 If nil, the region between START and END is replace by the encoded text.
8647 If buffer, the encoded text is inserted in that buffer after point (point
8649 In those cases, the length of the encoded text is returned.
8650 If DESTINATION is t, the encoded text is returned.
8652 This function sets `last-coding-system-used' to the precise coding system
8653 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8654 not fully specified.) */)
8655 (start
, end
, coding_system
, destination
)
8656 Lisp_Object start
, end
, coding_system
, destination
;
8658 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8662 code_convert_string (string
, coding_system
, dst_object
,
8663 encodep
, nocopy
, norecord
)
8664 Lisp_Object string
, coding_system
, dst_object
;
8665 int encodep
, nocopy
, norecord
;
8667 struct coding_system coding
;
8668 EMACS_INT chars
, bytes
;
8670 CHECK_STRING (string
);
8671 if (NILP (coding_system
))
8674 Vlast_coding_system_used
= Qno_conversion
;
8675 if (NILP (dst_object
))
8676 return (nocopy
? Fcopy_sequence (string
) : string
);
8679 if (NILP (coding_system
))
8680 coding_system
= Qno_conversion
;
8682 CHECK_CODING_SYSTEM (coding_system
);
8683 if (NILP (dst_object
))
8685 else if (! EQ (dst_object
, Qt
))
8686 CHECK_BUFFER (dst_object
);
8688 setup_coding_system (coding_system
, &coding
);
8689 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8690 chars
= SCHARS (string
);
8691 bytes
= SBYTES (string
);
8693 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8695 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8697 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8699 return (BUFFERP (dst_object
)
8700 ? make_number (coding
.produced_char
)
8701 : coding
.dst_object
);
8705 /* Encode or decode STRING according to CODING_SYSTEM.
8706 Do not set Vlast_coding_system_used.
8708 This function is called only from macros DECODE_FILE and
8709 ENCODE_FILE, thus we ignore character composition. */
8712 code_convert_string_norecord (string
, coding_system
, encodep
)
8713 Lisp_Object string
, coding_system
;
8716 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8720 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8722 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8724 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8725 if the decoding operation is trivial.
8727 Optional fourth arg BUFFER non-nil means that the decoded text is
8728 inserted in that buffer after point (point does not move). In this
8729 case, the return value is the length of the decoded text.
8731 This function sets `last-coding-system-used' to the precise coding system
8732 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8733 not fully specified.) */)
8734 (string
, coding_system
, nocopy
, buffer
)
8735 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8737 return code_convert_string (string
, coding_system
, buffer
,
8738 0, ! NILP (nocopy
), 0);
8741 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8743 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8745 Optional third arg NOCOPY non-nil means it is OK to return STRING
8746 itself if the encoding operation is trivial.
8748 Optional fourth arg BUFFER non-nil means that the encoded text is
8749 inserted in that buffer after point (point does not move). In this
8750 case, the return value is the length of the encoded text.
8752 This function sets `last-coding-system-used' to the precise coding system
8753 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8754 not fully specified.) */)
8755 (string
, coding_system
, nocopy
, buffer
)
8756 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8758 return code_convert_string (string
, coding_system
, buffer
,
8759 1, ! NILP (nocopy
), 1);
8763 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8764 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8765 Return the corresponding character. */)
8769 Lisp_Object spec
, attrs
, val
;
8770 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8773 CHECK_NATNUM (code
);
8774 c
= XFASTINT (code
);
8775 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8776 attrs
= AREF (spec
, 0);
8778 if (ASCII_BYTE_P (c
)
8779 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8782 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8783 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8784 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8785 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8788 charset
= charset_roman
;
8789 else if (c
>= 0xA0 && c
< 0xDF)
8791 charset
= charset_kana
;
8796 int s1
= c
>> 8, s2
= c
& 0xFF;
8798 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8799 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8800 error ("Invalid code: %d", code
);
8802 charset
= charset_kanji
;
8804 c
= DECODE_CHAR (charset
, c
);
8806 error ("Invalid code: %d", code
);
8807 return make_number (c
);
8811 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8812 doc
: /* Encode a Japanese character CH to shift_jis encoding.
8813 Return the corresponding code in SJIS. */)
8817 Lisp_Object spec
, attrs
, charset_list
;
8819 struct charset
*charset
;
8822 CHECK_CHARACTER (ch
);
8824 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8825 attrs
= AREF (spec
, 0);
8827 if (ASCII_CHAR_P (c
)
8828 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8831 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8832 charset
= char_charset (c
, charset_list
, &code
);
8833 if (code
== CHARSET_INVALID_CODE (charset
))
8834 error ("Can't encode by shift_jis encoding: %d", c
);
8837 return make_number (code
);
8840 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8841 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8842 Return the corresponding character. */)
8846 Lisp_Object spec
, attrs
, val
;
8847 struct charset
*charset_roman
, *charset_big5
, *charset
;
8850 CHECK_NATNUM (code
);
8851 c
= XFASTINT (code
);
8852 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8853 attrs
= AREF (spec
, 0);
8855 if (ASCII_BYTE_P (c
)
8856 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8859 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8860 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8861 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8864 charset
= charset_roman
;
8867 int b1
= c
>> 8, b2
= c
& 0x7F;
8868 if (b1
< 0xA1 || b1
> 0xFE
8869 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8870 error ("Invalid code: %d", code
);
8871 charset
= charset_big5
;
8873 c
= DECODE_CHAR (charset
, (unsigned )c
);
8875 error ("Invalid code: %d", code
);
8876 return make_number (c
);
8879 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8880 doc
: /* Encode the Big5 character CH to BIG5 coding system.
8881 Return the corresponding character code in Big5. */)
8885 Lisp_Object spec
, attrs
, charset_list
;
8886 struct charset
*charset
;
8890 CHECK_CHARACTER (ch
);
8892 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8893 attrs
= AREF (spec
, 0);
8894 if (ASCII_CHAR_P (c
)
8895 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8898 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8899 charset
= char_charset (c
, charset_list
, &code
);
8900 if (code
== CHARSET_INVALID_CODE (charset
))
8901 error ("Can't encode by Big5 encoding: %d", c
);
8903 return make_number (code
);
8907 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
8908 Sset_terminal_coding_system_internal
, 1, 2, 0,
8909 doc
: /* Internal use only. */)
8910 (coding_system
, terminal
)
8911 Lisp_Object coding_system
;
8912 Lisp_Object terminal
;
8914 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
8915 CHECK_SYMBOL (coding_system
);
8916 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
8917 /* We had better not send unsafe characters to terminal. */
8918 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
8919 /* Characer composition should be disabled. */
8920 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8921 terminal_coding
->src_multibyte
= 1;
8922 terminal_coding
->dst_multibyte
= 0;
8926 DEFUN ("set-safe-terminal-coding-system-internal",
8927 Fset_safe_terminal_coding_system_internal
,
8928 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8929 doc
: /* Internal use only. */)
8931 Lisp_Object coding_system
;
8933 CHECK_SYMBOL (coding_system
);
8934 setup_coding_system (Fcheck_coding_system (coding_system
),
8935 &safe_terminal_coding
);
8936 /* Characer composition should be disabled. */
8937 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8938 safe_terminal_coding
.src_multibyte
= 1;
8939 safe_terminal_coding
.dst_multibyte
= 0;
8943 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
8944 Sterminal_coding_system
, 0, 1, 0,
8945 doc
: /* Return coding system specified for terminal output on the given terminal.
8946 TERMINAL may be a terminal id, a frame, or nil for the selected
8947 frame's terminal device. */)
8949 Lisp_Object terminal
;
8951 struct coding_system
*terminal_coding
8952 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
8953 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
8955 /* For backward compatibility, return nil if it is `undecided'. */
8956 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
8959 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
8960 Sset_keyboard_coding_system_internal
, 1, 2, 0,
8961 doc
: /* Internal use only. */)
8962 (coding_system
, terminal
)
8963 Lisp_Object coding_system
;
8964 Lisp_Object terminal
;
8966 struct terminal
*t
= get_terminal (terminal
, 1);
8967 CHECK_SYMBOL (coding_system
);
8968 setup_coding_system (Fcheck_coding_system (coding_system
),
8969 TERMINAL_KEYBOARD_CODING (t
));
8970 /* Characer composition should be disabled. */
8971 TERMINAL_KEYBOARD_CODING (t
)->common_flags
8972 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8976 DEFUN ("keyboard-coding-system",
8977 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
8978 doc
: /* Return coding system specified for decoding keyboard input. */)
8980 Lisp_Object terminal
;
8982 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
8983 (get_terminal (terminal
, 1))->id
);
8987 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8988 Sfind_operation_coding_system
, 1, MANY
, 0,
8989 doc
: /* Choose a coding system for an operation based on the target name.
8990 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8991 DECODING-SYSTEM is the coding system to use for decoding
8992 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8993 for encoding (in case OPERATION does encoding).
8995 The first argument OPERATION specifies an I/O primitive:
8996 For file I/O, `insert-file-contents' or `write-region'.
8997 For process I/O, `call-process', `call-process-region', or `start-process'.
8998 For network I/O, `open-network-stream'.
9000 The remaining arguments should be the same arguments that were passed
9001 to the primitive. Depending on which primitive, one of those arguments
9002 is selected as the TARGET. For example, if OPERATION does file I/O,
9003 whichever argument specifies the file name is TARGET.
9005 TARGET has a meaning which depends on OPERATION:
9006 For file I/O, TARGET is a file name (except for the special case below).
9007 For process I/O, TARGET is a process name.
9008 For network I/O, TARGET is a service name or a port number.
9010 This function looks up what is specified for TARGET in
9011 `file-coding-system-alist', `process-coding-system-alist',
9012 or `network-coding-system-alist' depending on OPERATION.
9013 They may specify a coding system, a cons of coding systems,
9014 or a function symbol to call.
9015 In the last case, we call the function with one argument,
9016 which is a list of all the arguments given to this function.
9017 If the function can't decide a coding system, it can return
9018 `undecided' so that the normal code-detection is performed.
9020 If OPERATION is `insert-file-contents', the argument corresponding to
9021 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9022 file name to look up, and BUFFER is a buffer that contains the file's
9023 contents (not yet decoded). If `file-coding-system-alist' specifies a
9024 function to call for FILENAME, that function should examine the
9025 contents of BUFFER instead of reading the file.
9027 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9032 Lisp_Object operation
, target_idx
, target
, val
;
9033 register Lisp_Object chain
;
9036 error ("Too few arguments");
9037 operation
= args
[0];
9038 if (!SYMBOLP (operation
)
9039 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
9040 error ("Invalid first argument");
9041 if (nargs
< 1 + XINT (target_idx
))
9042 error ("Too few arguments for operation: %s",
9043 SDATA (SYMBOL_NAME (operation
)));
9044 target
= args
[XINT (target_idx
) + 1];
9045 if (!(STRINGP (target
)
9046 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9047 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9048 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9049 error ("Invalid %dth argument", XINT (target_idx
) + 1);
9051 target
= XCAR (target
);
9053 chain
= ((EQ (operation
, Qinsert_file_contents
)
9054 || EQ (operation
, Qwrite_region
))
9055 ? Vfile_coding_system_alist
9056 : (EQ (operation
, Qopen_network_stream
)
9057 ? Vnetwork_coding_system_alist
9058 : Vprocess_coding_system_alist
));
9062 for (; CONSP (chain
); chain
= XCDR (chain
))
9068 && ((STRINGP (target
)
9069 && STRINGP (XCAR (elt
))
9070 && fast_string_match (XCAR (elt
), target
) >= 0)
9071 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9074 /* Here, if VAL is both a valid coding system and a valid
9075 function symbol, we return VAL as a coding system. */
9078 if (! SYMBOLP (val
))
9080 if (! NILP (Fcoding_system_p (val
)))
9081 return Fcons (val
, val
);
9082 if (! NILP (Ffboundp (val
)))
9084 /* We use call1 rather than safe_call1
9085 so as to get bug reports about functions called here
9086 which don't handle the current interface. */
9087 val
= call1 (val
, Flist (nargs
, args
));
9090 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9091 return Fcons (val
, val
);
9099 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9100 Sset_coding_system_priority
, 0, MANY
, 0,
9101 doc
: /* Assign higher priority to the coding systems given as arguments.
9102 If multiple coding systems belong to the same category,
9103 all but the first one are ignored.
9105 usage: (set-coding-system-priority &rest coding-systems) */)
9111 int changed
[coding_category_max
];
9112 enum coding_category priorities
[coding_category_max
];
9114 bzero (changed
, sizeof changed
);
9116 for (i
= j
= 0; i
< nargs
; i
++)
9118 enum coding_category category
;
9119 Lisp_Object spec
, attrs
;
9121 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9122 attrs
= AREF (spec
, 0);
9123 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9124 if (changed
[category
])
9125 /* Ignore this coding system because a coding system of the
9126 same category already had a higher priority. */
9128 changed
[category
] = 1;
9129 priorities
[j
++] = category
;
9130 if (coding_categories
[category
].id
>= 0
9131 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9132 setup_coding_system (args
[i
], &coding_categories
[category
]);
9133 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9136 /* Now we have decided top J priorities. Reflect the order of the
9137 original priorities to the remaining priorities. */
9139 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9141 while (j
< coding_category_max
9142 && changed
[coding_priorities
[j
]])
9144 if (j
== coding_category_max
)
9146 priorities
[i
] = coding_priorities
[j
];
9149 bcopy (priorities
, coding_priorities
, sizeof priorities
);
9151 /* Update `coding-category-list'. */
9152 Vcoding_category_list
= Qnil
;
9153 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9154 Vcoding_category_list
9155 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9156 Vcoding_category_list
);
9161 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9162 Scoding_system_priority_list
, 0, 1, 0,
9163 doc
: /* Return a list of coding systems ordered by their priorities.
9164 HIGHESTP non-nil means just return the highest priority one. */)
9166 Lisp_Object highestp
;
9171 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9173 enum coding_category category
= coding_priorities
[i
];
9174 int id
= coding_categories
[category
].id
;
9179 attrs
= CODING_ID_ATTRS (id
);
9180 if (! NILP (highestp
))
9181 return CODING_ATTR_BASE_NAME (attrs
);
9182 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9184 return Fnreverse (val
);
9187 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
9190 make_subsidiaries (base
)
9193 Lisp_Object subsidiaries
;
9194 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9195 char *buf
= (char *) alloca (base_name_len
+ 6);
9198 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
9199 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9200 for (i
= 0; i
< 3; i
++)
9202 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
9203 ASET (subsidiaries
, i
, intern (buf
));
9205 return subsidiaries
;
9209 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9210 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9211 doc
: /* For internal use only.
9212 usage: (define-coding-system-internal ...) */)
9218 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9219 Lisp_Object attrs
; /* Vector of attributes. */
9220 Lisp_Object eol_type
;
9221 Lisp_Object aliases
;
9222 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9223 enum coding_category category
;
9224 Lisp_Object tail
, val
;
9225 int max_charset_id
= 0;
9228 if (nargs
< coding_arg_max
)
9231 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9233 name
= args
[coding_arg_name
];
9234 CHECK_SYMBOL (name
);
9235 CODING_ATTR_BASE_NAME (attrs
) = name
;
9237 val
= args
[coding_arg_mnemonic
];
9238 if (! STRINGP (val
))
9239 CHECK_CHARACTER (val
);
9240 CODING_ATTR_MNEMONIC (attrs
) = val
;
9242 coding_type
= args
[coding_arg_coding_type
];
9243 CHECK_SYMBOL (coding_type
);
9244 CODING_ATTR_TYPE (attrs
) = coding_type
;
9246 charset_list
= args
[coding_arg_charset_list
];
9247 if (SYMBOLP (charset_list
))
9249 if (EQ (charset_list
, Qiso_2022
))
9251 if (! EQ (coding_type
, Qiso_2022
))
9252 error ("Invalid charset-list");
9253 charset_list
= Viso_2022_charset_list
;
9255 else if (EQ (charset_list
, Qemacs_mule
))
9257 if (! EQ (coding_type
, Qemacs_mule
))
9258 error ("Invalid charset-list");
9259 charset_list
= Vemacs_mule_charset_list
;
9261 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9262 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9263 max_charset_id
= XFASTINT (XCAR (tail
));
9267 charset_list
= Fcopy_sequence (charset_list
);
9268 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9270 struct charset
*charset
;
9273 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9274 if (EQ (coding_type
, Qiso_2022
)
9275 ? CHARSET_ISO_FINAL (charset
) < 0
9276 : EQ (coding_type
, Qemacs_mule
)
9277 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9279 error ("Can't handle charset `%s'",
9280 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9282 XSETCAR (tail
, make_number (charset
->id
));
9283 if (max_charset_id
< charset
->id
)
9284 max_charset_id
= charset
->id
;
9287 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9289 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
9291 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9292 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9293 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9295 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9297 val
= args
[coding_arg_decode_translation_table
];
9298 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9300 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9302 val
= args
[coding_arg_encode_translation_table
];
9303 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9305 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9307 val
= args
[coding_arg_post_read_conversion
];
9309 CODING_ATTR_POST_READ (attrs
) = val
;
9311 val
= args
[coding_arg_pre_write_conversion
];
9313 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9315 val
= args
[coding_arg_default_char
];
9317 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9320 CHECK_CHARACTER (val
);
9321 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9324 val
= args
[coding_arg_for_unibyte
];
9325 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9327 val
= args
[coding_arg_plist
];
9329 CODING_ATTR_PLIST (attrs
) = val
;
9331 if (EQ (coding_type
, Qcharset
))
9333 /* Generate a lisp vector of 256 elements. Each element is nil,
9334 integer, or a list of charset IDs.
9336 If Nth element is nil, the byte code N is invalid in this
9339 If Nth element is a number NUM, N is the first byte of a
9340 charset whose ID is NUM.
9342 If Nth element is a list of charset IDs, N is the first byte
9343 of one of them. The list is sorted by dimensions of the
9344 charsets. A charset of smaller dimension comes firtst. */
9345 val
= Fmake_vector (make_number (256), Qnil
);
9347 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9349 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9350 int dim
= CHARSET_DIMENSION (charset
);
9351 int idx
= (dim
- 1) * 4;
9353 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9354 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9356 for (i
= charset
->code_space
[idx
];
9357 i
<= charset
->code_space
[idx
+ 1]; i
++)
9359 Lisp_Object tmp
, tmp2
;
9362 tmp
= AREF (val
, i
);
9365 else if (NUMBERP (tmp
))
9367 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9369 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9371 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9375 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9377 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9382 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9385 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9386 XSETCAR (tmp2
, XCAR (tail
));
9392 ASET (attrs
, coding_attr_charset_valids
, val
);
9393 category
= coding_category_charset
;
9395 else if (EQ (coding_type
, Qccl
))
9399 if (nargs
< coding_arg_ccl_max
)
9402 val
= args
[coding_arg_ccl_decoder
];
9403 CHECK_CCL_PROGRAM (val
);
9405 val
= Fcopy_sequence (val
);
9406 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9408 val
= args
[coding_arg_ccl_encoder
];
9409 CHECK_CCL_PROGRAM (val
);
9411 val
= Fcopy_sequence (val
);
9412 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9414 val
= args
[coding_arg_ccl_valids
];
9415 valids
= Fmake_string (make_number (256), make_number (0));
9416 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9423 from
= to
= XINT (val
);
9424 if (from
< 0 || from
> 255)
9425 args_out_of_range_3 (val
, make_number (0), make_number (255));
9430 CHECK_NATNUM_CAR (val
);
9431 CHECK_NATNUM_CDR (val
);
9432 from
= XINT (XCAR (val
));
9434 args_out_of_range_3 (XCAR (val
),
9435 make_number (0), make_number (255));
9436 to
= XINT (XCDR (val
));
9437 if (to
< from
|| to
> 255)
9438 args_out_of_range_3 (XCDR (val
),
9439 XCAR (val
), make_number (255));
9441 for (i
= from
; i
<= to
; i
++)
9442 SSET (valids
, i
, 1);
9444 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9446 category
= coding_category_ccl
;
9448 else if (EQ (coding_type
, Qutf_16
))
9450 Lisp_Object bom
, endian
;
9452 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9454 if (nargs
< coding_arg_utf16_max
)
9457 bom
= args
[coding_arg_utf16_bom
];
9458 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9462 CHECK_CODING_SYSTEM (val
);
9464 CHECK_CODING_SYSTEM (val
);
9466 ASET (attrs
, coding_attr_utf_bom
, bom
);
9468 endian
= args
[coding_arg_utf16_endian
];
9469 CHECK_SYMBOL (endian
);
9472 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9473 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9474 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9476 category
= (CONSP (bom
)
9477 ? coding_category_utf_16_auto
9479 ? (EQ (endian
, Qbig
)
9480 ? coding_category_utf_16_be_nosig
9481 : coding_category_utf_16_le_nosig
)
9482 : (EQ (endian
, Qbig
)
9483 ? coding_category_utf_16_be
9484 : coding_category_utf_16_le
));
9486 else if (EQ (coding_type
, Qiso_2022
))
9488 Lisp_Object initial
, reg_usage
, request
, flags
;
9491 if (nargs
< coding_arg_iso2022_max
)
9494 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9495 CHECK_VECTOR (initial
);
9496 for (i
= 0; i
< 4; i
++)
9498 val
= Faref (initial
, make_number (i
));
9501 struct charset
*charset
;
9503 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9504 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9505 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9506 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9509 ASET (initial
, i
, make_number (-1));
9512 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9513 CHECK_CONS (reg_usage
);
9514 CHECK_NUMBER_CAR (reg_usage
);
9515 CHECK_NUMBER_CDR (reg_usage
);
9517 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9518 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9526 CHECK_CHARSET_GET_ID (tmp
, id
);
9527 CHECK_NATNUM_CDR (val
);
9528 if (XINT (XCDR (val
)) >= 4)
9529 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9530 XSETCAR (val
, make_number (id
));
9533 flags
= args
[coding_arg_iso2022_flags
];
9534 CHECK_NATNUM (flags
);
9536 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9537 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9539 ASET (attrs
, coding_attr_iso_initial
, initial
);
9540 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9541 ASET (attrs
, coding_attr_iso_request
, request
);
9542 ASET (attrs
, coding_attr_iso_flags
, flags
);
9543 setup_iso_safe_charsets (attrs
);
9545 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9546 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9547 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9548 ? coding_category_iso_7_else
9549 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9550 ? coding_category_iso_7
9551 : coding_category_iso_7_tight
);
9554 int id
= XINT (AREF (initial
, 1));
9556 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9557 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9559 ? coding_category_iso_8_else
9560 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9561 ? coding_category_iso_8_1
9562 : coding_category_iso_8_2
);
9564 if (category
!= coding_category_iso_8_1
9565 && category
!= coding_category_iso_8_2
)
9566 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9568 else if (EQ (coding_type
, Qemacs_mule
))
9570 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9571 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9572 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9573 category
= coding_category_emacs_mule
;
9575 else if (EQ (coding_type
, Qshift_jis
))
9578 struct charset
*charset
;
9580 if (XINT (Flength (charset_list
)) != 3
9581 && XINT (Flength (charset_list
)) != 4)
9582 error ("There should be three or four charsets");
9584 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9585 if (CHARSET_DIMENSION (charset
) != 1)
9586 error ("Dimension of charset %s is not one",
9587 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9588 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9589 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9591 charset_list
= XCDR (charset_list
);
9592 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9593 if (CHARSET_DIMENSION (charset
) != 1)
9594 error ("Dimension of charset %s is not one",
9595 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9597 charset_list
= XCDR (charset_list
);
9598 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9599 if (CHARSET_DIMENSION (charset
) != 2)
9600 error ("Dimension of charset %s is not two",
9601 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9603 charset_list
= XCDR (charset_list
);
9604 if (! NILP (charset_list
))
9606 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9607 if (CHARSET_DIMENSION (charset
) != 2)
9608 error ("Dimension of charset %s is not two",
9609 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9612 category
= coding_category_sjis
;
9613 Vsjis_coding_system
= name
;
9615 else if (EQ (coding_type
, Qbig5
))
9617 struct charset
*charset
;
9619 if (XINT (Flength (charset_list
)) != 2)
9620 error ("There should be just two charsets");
9622 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9623 if (CHARSET_DIMENSION (charset
) != 1)
9624 error ("Dimension of charset %s is not one",
9625 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9626 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9627 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9629 charset_list
= XCDR (charset_list
);
9630 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9631 if (CHARSET_DIMENSION (charset
) != 2)
9632 error ("Dimension of charset %s is not two",
9633 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9635 category
= coding_category_big5
;
9636 Vbig5_coding_system
= name
;
9638 else if (EQ (coding_type
, Qraw_text
))
9640 category
= coding_category_raw_text
;
9641 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9643 else if (EQ (coding_type
, Qutf_8
))
9647 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9649 if (nargs
< coding_arg_utf8_max
)
9652 bom
= args
[coding_arg_utf8_bom
];
9653 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9657 CHECK_CODING_SYSTEM (val
);
9659 CHECK_CODING_SYSTEM (val
);
9661 ASET (attrs
, coding_attr_utf_bom
, bom
);
9663 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9664 : NILP (bom
) ? coding_category_utf_8_nosig
9665 : coding_category_utf_8_sig
);
9667 else if (EQ (coding_type
, Qundecided
))
9668 category
= coding_category_undecided
;
9670 error ("Invalid coding system type: %s",
9671 SDATA (SYMBOL_NAME (coding_type
)));
9673 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9674 CODING_ATTR_PLIST (attrs
)
9675 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9676 CODING_ATTR_PLIST (attrs
)));
9677 CODING_ATTR_PLIST (attrs
)
9678 = Fcons (QCascii_compatible_p
,
9679 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9680 CODING_ATTR_PLIST (attrs
)));
9682 eol_type
= args
[coding_arg_eol_type
];
9683 if (! NILP (eol_type
)
9684 && ! EQ (eol_type
, Qunix
)
9685 && ! EQ (eol_type
, Qdos
)
9686 && ! EQ (eol_type
, Qmac
))
9687 error ("Invalid eol-type");
9689 aliases
= Fcons (name
, Qnil
);
9691 if (NILP (eol_type
))
9693 eol_type
= make_subsidiaries (name
);
9694 for (i
= 0; i
< 3; i
++)
9696 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9698 this_name
= AREF (eol_type
, i
);
9699 this_aliases
= Fcons (this_name
, Qnil
);
9700 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9701 this_spec
= Fmake_vector (make_number (3), attrs
);
9702 ASET (this_spec
, 1, this_aliases
);
9703 ASET (this_spec
, 2, this_eol_type
);
9704 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9705 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9706 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9708 Vcoding_system_alist
9709 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9710 Vcoding_system_alist
);
9714 spec_vec
= Fmake_vector (make_number (3), attrs
);
9715 ASET (spec_vec
, 1, aliases
);
9716 ASET (spec_vec
, 2, eol_type
);
9718 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9719 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9720 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9722 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9723 Vcoding_system_alist
);
9726 int id
= coding_categories
[category
].id
;
9728 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9729 setup_coding_system (name
, &coding_categories
[category
]);
9735 return Fsignal (Qwrong_number_of_arguments
,
9736 Fcons (intern ("define-coding-system-internal"),
9737 make_number (nargs
)));
9741 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
9743 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9744 (coding_system
, prop
, val
)
9745 Lisp_Object coding_system
, prop
, val
;
9747 Lisp_Object spec
, attrs
;
9749 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9750 attrs
= AREF (spec
, 0);
9751 if (EQ (prop
, QCmnemonic
))
9753 if (! STRINGP (val
))
9754 CHECK_CHARACTER (val
);
9755 CODING_ATTR_MNEMONIC (attrs
) = val
;
9757 else if (EQ (prop
, QCdefault_char
))
9760 val
= make_number (' ');
9762 CHECK_CHARACTER (val
);
9763 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9765 else if (EQ (prop
, QCdecode_translation_table
))
9767 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9769 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9771 else if (EQ (prop
, QCencode_translation_table
))
9773 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9775 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9777 else if (EQ (prop
, QCpost_read_conversion
))
9780 CODING_ATTR_POST_READ (attrs
) = val
;
9782 else if (EQ (prop
, QCpre_write_conversion
))
9785 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9787 else if (EQ (prop
, QCascii_compatible_p
))
9789 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9792 CODING_ATTR_PLIST (attrs
)
9793 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9798 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9799 Sdefine_coding_system_alias
, 2, 2, 0,
9800 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9801 (alias
, coding_system
)
9802 Lisp_Object alias
, coding_system
;
9804 Lisp_Object spec
, aliases
, eol_type
, val
;
9806 CHECK_SYMBOL (alias
);
9807 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9808 aliases
= AREF (spec
, 1);
9809 /* ALIASES should be a list of length more than zero, and the first
9810 element is a base coding system. Append ALIAS at the tail of the
9812 while (!NILP (XCDR (aliases
)))
9813 aliases
= XCDR (aliases
);
9814 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9816 eol_type
= AREF (spec
, 2);
9817 if (VECTORP (eol_type
))
9819 Lisp_Object subsidiaries
;
9822 subsidiaries
= make_subsidiaries (alias
);
9823 for (i
= 0; i
< 3; i
++)
9824 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9825 AREF (eol_type
, i
));
9828 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9829 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9830 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
9832 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9833 Vcoding_system_alist
);
9838 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9840 doc
: /* Return the base of CODING-SYSTEM.
9841 Any alias or subsidiary coding system is not a base coding system. */)
9843 Lisp_Object coding_system
;
9845 Lisp_Object spec
, attrs
;
9847 if (NILP (coding_system
))
9848 return (Qno_conversion
);
9849 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9850 attrs
= AREF (spec
, 0);
9851 return CODING_ATTR_BASE_NAME (attrs
);
9854 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9856 doc
: "Return the property list of CODING-SYSTEM.")
9858 Lisp_Object coding_system
;
9860 Lisp_Object spec
, attrs
;
9862 if (NILP (coding_system
))
9863 coding_system
= Qno_conversion
;
9864 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9865 attrs
= AREF (spec
, 0);
9866 return CODING_ATTR_PLIST (attrs
);
9870 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9872 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9874 Lisp_Object coding_system
;
9878 if (NILP (coding_system
))
9879 coding_system
= Qno_conversion
;
9880 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9881 return AREF (spec
, 1);
9884 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9885 Scoding_system_eol_type
, 1, 1, 0,
9886 doc
: /* Return eol-type of CODING-SYSTEM.
9887 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
9889 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9890 and CR respectively.
9892 A vector value indicates that a format of end-of-line should be
9893 detected automatically. Nth element of the vector is the subsidiary
9894 coding system whose eol-type is N. */)
9896 Lisp_Object coding_system
;
9898 Lisp_Object spec
, eol_type
;
9901 if (NILP (coding_system
))
9902 coding_system
= Qno_conversion
;
9903 if (! CODING_SYSTEM_P (coding_system
))
9905 spec
= CODING_SYSTEM_SPEC (coding_system
);
9906 eol_type
= AREF (spec
, 2);
9907 if (VECTORP (eol_type
))
9908 return Fcopy_sequence (eol_type
);
9909 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9910 return make_number (n
);
9916 /*** 9. Post-amble ***/
9923 for (i
= 0; i
< coding_category_max
; i
++)
9925 coding_categories
[i
].id
= -1;
9926 coding_priorities
[i
] = i
;
9929 /* ISO2022 specific initialize routine. */
9930 for (i
= 0; i
< 0x20; i
++)
9931 iso_code_class
[i
] = ISO_control_0
;
9932 for (i
= 0x21; i
< 0x7F; i
++)
9933 iso_code_class
[i
] = ISO_graphic_plane_0
;
9934 for (i
= 0x80; i
< 0xA0; i
++)
9935 iso_code_class
[i
] = ISO_control_1
;
9936 for (i
= 0xA1; i
< 0xFF; i
++)
9937 iso_code_class
[i
] = ISO_graphic_plane_1
;
9938 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9939 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9940 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9941 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9942 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9943 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9944 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9945 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9946 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9948 for (i
= 0; i
< 256; i
++)
9950 emacs_mule_bytes
[i
] = 1;
9952 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9953 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9954 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9955 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9963 staticpro (&Vcoding_system_hash_table
);
9965 Lisp_Object args
[2];
9968 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9971 staticpro (&Vsjis_coding_system
);
9972 Vsjis_coding_system
= Qnil
;
9974 staticpro (&Vbig5_coding_system
);
9975 Vbig5_coding_system
= Qnil
;
9977 staticpro (&Vcode_conversion_reused_workbuf
);
9978 Vcode_conversion_reused_workbuf
= Qnil
;
9980 staticpro (&Vcode_conversion_workbuf_name
);
9981 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9983 reused_workbuf_in_use
= 0;
9985 DEFSYM (Qcharset
, "charset");
9986 DEFSYM (Qtarget_idx
, "target-idx");
9987 DEFSYM (Qcoding_system_history
, "coding-system-history");
9988 Fset (Qcoding_system_history
, Qnil
);
9990 /* Target FILENAME is the first argument. */
9991 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9992 /* Target FILENAME is the third argument. */
9993 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9995 DEFSYM (Qcall_process
, "call-process");
9996 /* Target PROGRAM is the first argument. */
9997 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9999 DEFSYM (Qcall_process_region
, "call-process-region");
10000 /* Target PROGRAM is the third argument. */
10001 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10003 DEFSYM (Qstart_process
, "start-process");
10004 /* Target PROGRAM is the third argument. */
10005 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10007 DEFSYM (Qopen_network_stream
, "open-network-stream");
10008 /* Target SERVICE is the fourth argument. */
10009 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10011 DEFSYM (Qcoding_system
, "coding-system");
10012 DEFSYM (Qcoding_aliases
, "coding-aliases");
10014 DEFSYM (Qeol_type
, "eol-type");
10015 DEFSYM (Qunix
, "unix");
10016 DEFSYM (Qdos
, "dos");
10018 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10019 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10020 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10021 DEFSYM (Qdefault_char
, "default-char");
10022 DEFSYM (Qundecided
, "undecided");
10023 DEFSYM (Qno_conversion
, "no-conversion");
10024 DEFSYM (Qraw_text
, "raw-text");
10026 DEFSYM (Qiso_2022
, "iso-2022");
10028 DEFSYM (Qutf_8
, "utf-8");
10029 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10031 DEFSYM (Qutf_16
, "utf-16");
10032 DEFSYM (Qbig
, "big");
10033 DEFSYM (Qlittle
, "little");
10035 DEFSYM (Qshift_jis
, "shift-jis");
10036 DEFSYM (Qbig5
, "big5");
10038 DEFSYM (Qcoding_system_p
, "coding-system-p");
10040 DEFSYM (Qcoding_system_error
, "coding-system-error");
10041 Fput (Qcoding_system_error
, Qerror_conditions
,
10042 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
10043 Fput (Qcoding_system_error
, Qerror_message
,
10044 build_string ("Invalid coding system"));
10046 /* Intern this now in case it isn't already done.
10047 Setting this variable twice is harmless.
10048 But don't staticpro it here--that is done in alloc.c. */
10049 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
10051 DEFSYM (Qtranslation_table
, "translation-table");
10052 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10053 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10054 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10055 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10057 DEFSYM (Qvalid_codes
, "valid-codes");
10059 DEFSYM (Qemacs_mule
, "emacs-mule");
10061 DEFSYM (QCcategory
, ":category");
10062 DEFSYM (QCmnemonic
, ":mnemonic");
10063 DEFSYM (QCdefault_char
, ":default-char");
10064 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10065 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10066 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10067 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10068 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10070 Vcoding_category_table
10071 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10072 staticpro (&Vcoding_category_table
);
10073 /* Followings are target of code detection. */
10074 ASET (Vcoding_category_table
, coding_category_iso_7
,
10075 intern ("coding-category-iso-7"));
10076 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10077 intern ("coding-category-iso-7-tight"));
10078 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10079 intern ("coding-category-iso-8-1"));
10080 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10081 intern ("coding-category-iso-8-2"));
10082 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10083 intern ("coding-category-iso-7-else"));
10084 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10085 intern ("coding-category-iso-8-else"));
10086 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10087 intern ("coding-category-utf-8-auto"));
10088 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10089 intern ("coding-category-utf-8"));
10090 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10091 intern ("coding-category-utf-8-sig"));
10092 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10093 intern ("coding-category-utf-16-be"));
10094 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10095 intern ("coding-category-utf-16-auto"));
10096 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10097 intern ("coding-category-utf-16-le"));
10098 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10099 intern ("coding-category-utf-16-be-nosig"));
10100 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10101 intern ("coding-category-utf-16-le-nosig"));
10102 ASET (Vcoding_category_table
, coding_category_charset
,
10103 intern ("coding-category-charset"));
10104 ASET (Vcoding_category_table
, coding_category_sjis
,
10105 intern ("coding-category-sjis"));
10106 ASET (Vcoding_category_table
, coding_category_big5
,
10107 intern ("coding-category-big5"));
10108 ASET (Vcoding_category_table
, coding_category_ccl
,
10109 intern ("coding-category-ccl"));
10110 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10111 intern ("coding-category-emacs-mule"));
10112 /* Followings are NOT target of code detection. */
10113 ASET (Vcoding_category_table
, coding_category_raw_text
,
10114 intern ("coding-category-raw-text"));
10115 ASET (Vcoding_category_table
, coding_category_undecided
,
10116 intern ("coding-category-undecided"));
10118 DEFSYM (Qinsufficient_source
, "insufficient-source");
10119 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10120 DEFSYM (Qinvalid_source
, "invalid-source");
10121 DEFSYM (Qinterrupted
, "interrupted");
10122 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10123 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10125 defsubr (&Scoding_system_p
);
10126 defsubr (&Sread_coding_system
);
10127 defsubr (&Sread_non_nil_coding_system
);
10128 defsubr (&Scheck_coding_system
);
10129 defsubr (&Sdetect_coding_region
);
10130 defsubr (&Sdetect_coding_string
);
10131 defsubr (&Sfind_coding_systems_region_internal
);
10132 defsubr (&Sunencodable_char_position
);
10133 defsubr (&Scheck_coding_systems_region
);
10134 defsubr (&Sdecode_coding_region
);
10135 defsubr (&Sencode_coding_region
);
10136 defsubr (&Sdecode_coding_string
);
10137 defsubr (&Sencode_coding_string
);
10138 defsubr (&Sdecode_sjis_char
);
10139 defsubr (&Sencode_sjis_char
);
10140 defsubr (&Sdecode_big5_char
);
10141 defsubr (&Sencode_big5_char
);
10142 defsubr (&Sset_terminal_coding_system_internal
);
10143 defsubr (&Sset_safe_terminal_coding_system_internal
);
10144 defsubr (&Sterminal_coding_system
);
10145 defsubr (&Sset_keyboard_coding_system_internal
);
10146 defsubr (&Skeyboard_coding_system
);
10147 defsubr (&Sfind_operation_coding_system
);
10148 defsubr (&Sset_coding_system_priority
);
10149 defsubr (&Sdefine_coding_system_internal
);
10150 defsubr (&Sdefine_coding_system_alias
);
10151 defsubr (&Scoding_system_put
);
10152 defsubr (&Scoding_system_base
);
10153 defsubr (&Scoding_system_plist
);
10154 defsubr (&Scoding_system_aliases
);
10155 defsubr (&Scoding_system_eol_type
);
10156 defsubr (&Scoding_system_priority_list
);
10158 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
10159 doc
: /* List of coding systems.
10161 Do not alter the value of this variable manually. This variable should be
10162 updated by the functions `define-coding-system' and
10163 `define-coding-system-alias'. */);
10164 Vcoding_system_list
= Qnil
;
10166 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
10167 doc
: /* Alist of coding system names.
10168 Each element is one element list of coding system name.
10169 This variable is given to `completing-read' as COLLECTION argument.
10171 Do not alter the value of this variable manually. This variable should be
10172 updated by the functions `make-coding-system' and
10173 `define-coding-system-alias'. */);
10174 Vcoding_system_alist
= Qnil
;
10176 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
10177 doc
: /* List of coding-categories (symbols) ordered by priority.
10179 On detecting a coding system, Emacs tries code detection algorithms
10180 associated with each coding-category one by one in this order. When
10181 one algorithm agrees with a byte sequence of source text, the coding
10182 system bound to the corresponding coding-category is selected.
10184 Don't modify this variable directly, but use `set-coding-priority'. */);
10188 Vcoding_category_list
= Qnil
;
10189 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10190 Vcoding_category_list
10191 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10192 Vcoding_category_list
);
10195 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
10196 doc
: /* Specify the coding system for read operations.
10197 It is useful to bind this variable with `let', but do not set it globally.
10198 If the value is a coding system, it is used for decoding on read operation.
10199 If not, an appropriate element is used from one of the coding system alists.
10200 There are three such tables: `file-coding-system-alist',
10201 `process-coding-system-alist', and `network-coding-system-alist'. */);
10202 Vcoding_system_for_read
= Qnil
;
10204 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
10205 doc
: /* Specify the coding system for write operations.
10206 Programs bind this variable with `let', but you should not set it globally.
10207 If the value is a coding system, it is used for encoding of output,
10208 when writing it to a file and when sending it to a file or subprocess.
10210 If this does not specify a coding system, an appropriate element
10211 is used from one of the coding system alists.
10212 There are three such tables: `file-coding-system-alist',
10213 `process-coding-system-alist', and `network-coding-system-alist'.
10214 For output to files, if the above procedure does not specify a coding system,
10215 the value of `buffer-file-coding-system' is used. */);
10216 Vcoding_system_for_write
= Qnil
;
10218 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
10220 Coding system used in the latest file or process I/O. */);
10221 Vlast_coding_system_used
= Qnil
;
10223 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
10225 Error status of the last code conversion.
10227 When an error was detected in the last code conversion, this variable
10228 is set to one of the following symbols.
10229 `insufficient-source'
10233 `insufficient-memory'
10234 When no error was detected, the value doesn't change. So, to check
10235 the error status of a code conversion by this variable, you must
10236 explicitly set this variable to nil before performing code
10238 Vlast_code_conversion_error
= Qnil
;
10240 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
10242 *Non-nil means always inhibit code conversion of end-of-line format.
10243 See info node `Coding Systems' and info node `Text and Binary' concerning
10244 such conversion. */);
10245 inhibit_eol_conversion
= 0;
10247 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
10249 Non-nil means process buffer inherits coding system of process output.
10250 Bind it to t if the process output is to be treated as if it were a file
10251 read from some filesystem. */);
10252 inherit_process_coding_system
= 0;
10254 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
10256 Alist to decide a coding system to use for a file I/O operation.
10257 The format is ((PATTERN . VAL) ...),
10258 where PATTERN is a regular expression matching a file name,
10259 VAL is a coding system, a cons of coding systems, or a function symbol.
10260 If VAL is a coding system, it is used for both decoding and encoding
10262 If VAL is a cons of coding systems, the car part is used for decoding,
10263 and the cdr part is used for encoding.
10264 If VAL is a function symbol, the function must return a coding system
10265 or a cons of coding systems which are used as above. The function is
10266 called with an argument that is a list of the arguments with which
10267 `find-operation-coding-system' was called. If the function can't decide
10268 a coding system, it can return `undecided' so that the normal
10269 code-detection is performed.
10271 See also the function `find-operation-coding-system'
10272 and the variable `auto-coding-alist'. */);
10273 Vfile_coding_system_alist
= Qnil
;
10275 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
10277 Alist to decide a coding system to use for a process I/O operation.
10278 The format is ((PATTERN . VAL) ...),
10279 where PATTERN is a regular expression matching a program name,
10280 VAL is a coding system, a cons of coding systems, or a function symbol.
10281 If VAL is a coding system, it is used for both decoding what received
10282 from the program and encoding what sent to the program.
10283 If VAL is a cons of coding systems, the car part is used for decoding,
10284 and the cdr part is used for encoding.
10285 If VAL is a function symbol, the function must return a coding system
10286 or a cons of coding systems which are used as above.
10288 See also the function `find-operation-coding-system'. */);
10289 Vprocess_coding_system_alist
= Qnil
;
10291 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
10293 Alist to decide a coding system to use for a network I/O operation.
10294 The format is ((PATTERN . VAL) ...),
10295 where PATTERN is a regular expression matching a network service name
10296 or is a port number to connect to,
10297 VAL is a coding system, a cons of coding systems, or a function symbol.
10298 If VAL is a coding system, it is used for both decoding what received
10299 from the network stream and encoding what sent to the network stream.
10300 If VAL is a cons of coding systems, the car part is used for decoding,
10301 and the cdr part is used for encoding.
10302 If VAL is a function symbol, the function must return a coding system
10303 or a cons of coding systems which are used as above.
10305 See also the function `find-operation-coding-system'. */);
10306 Vnetwork_coding_system_alist
= Qnil
;
10308 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
10309 doc
: /* Coding system to use with system messages.
10310 Also used for decoding keyboard input on X Window system. */);
10311 Vlocale_coding_system
= Qnil
;
10313 /* The eol mnemonics are reset in startup.el system-dependently. */
10314 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
10316 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10317 eol_mnemonic_unix
= build_string (":");
10319 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
10321 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10322 eol_mnemonic_dos
= build_string ("\\");
10324 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
10326 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10327 eol_mnemonic_mac
= build_string ("/");
10329 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
10331 *String displayed in mode line when end-of-line format is not yet determined. */);
10332 eol_mnemonic_undecided
= build_string (":");
10334 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
10336 *Non-nil enables character translation while encoding and decoding. */);
10337 Venable_character_translation
= Qt
;
10339 DEFVAR_LISP ("standard-translation-table-for-decode",
10340 &Vstandard_translation_table_for_decode
,
10341 doc
: /* Table for translating characters while decoding. */);
10342 Vstandard_translation_table_for_decode
= Qnil
;
10344 DEFVAR_LISP ("standard-translation-table-for-encode",
10345 &Vstandard_translation_table_for_encode
,
10346 doc
: /* Table for translating characters while encoding. */);
10347 Vstandard_translation_table_for_encode
= Qnil
;
10349 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
10350 doc
: /* Alist of charsets vs revision numbers.
10351 While encoding, if a charset (car part of an element) is found,
10352 designate it with the escape sequence identifying revision (cdr part
10353 of the element). */);
10354 Vcharset_revision_table
= Qnil
;
10356 DEFVAR_LISP ("default-process-coding-system",
10357 &Vdefault_process_coding_system
,
10358 doc
: /* Cons of coding systems used for process I/O by default.
10359 The car part is used for decoding a process output,
10360 the cdr part is used for encoding a text to be sent to a process. */);
10361 Vdefault_process_coding_system
= Qnil
;
10363 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
10365 Table of extra Latin codes in the range 128..159 (inclusive).
10366 This is a vector of length 256.
10367 If Nth element is non-nil, the existence of code N in a file
10368 \(or output of subprocess) doesn't prevent it to be detected as
10369 a coding system of ISO 2022 variant which has a flag
10370 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10371 or reading output of a subprocess.
10372 Only 128th through 159th elements have a meaning. */);
10373 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10375 DEFVAR_LISP ("select-safe-coding-system-function",
10376 &Vselect_safe_coding_system_function
,
10378 Function to call to select safe coding system for encoding a text.
10380 If set, this function is called to force a user to select a proper
10381 coding system which can encode the text in the case that a default
10382 coding system used in each operation can't encode the text. The
10383 function should take care that the buffer is not modified while
10384 the coding system is being selected.
10386 The default value is `select-safe-coding-system' (which see). */);
10387 Vselect_safe_coding_system_function
= Qnil
;
10389 DEFVAR_BOOL ("coding-system-require-warning",
10390 &coding_system_require_warning
,
10391 doc
: /* Internal use only.
10392 If non-nil, on writing a file, `select-safe-coding-system-function' is
10393 called even if `coding-system-for-write' is non-nil. The command
10394 `universal-coding-system-argument' binds this variable to t temporarily. */);
10395 coding_system_require_warning
= 0;
10398 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10399 &inhibit_iso_escape_detection
,
10401 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10403 When Emacs reads text, it tries to detect how the text is encoded.
10404 This code detection is sensitive to escape sequences. If Emacs sees
10405 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10406 of the ISO2022 encodings, and decodes text by the corresponding coding
10407 system (e.g. `iso-2022-7bit').
10409 However, there may be a case that you want to read escape sequences in
10410 a file as is. In such a case, you can set this variable to non-nil.
10411 Then the code detection will ignore any escape sequences, and no text is
10412 detected as encoded in some ISO-2022 encoding. The result is that all
10413 escape sequences become visible in a buffer.
10415 The default value is nil, and it is strongly recommended not to change
10416 it. That is because many Emacs Lisp source files that contain
10417 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10418 in Emacs's distribution, and they won't be decoded correctly on
10419 reading if you suppress escape sequence detection.
10421 The other way to read escape sequences in a file without decoding is
10422 to explicitly specify some coding system that doesn't use ISO-2022
10423 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10424 inhibit_iso_escape_detection
= 0;
10426 DEFVAR_BOOL ("inhibit-null-byte-detection",
10427 &inhibit_null_byte_detection
,
10428 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10429 By default, Emacs treats it as binary data, and does not attempt to
10430 decode it. The effect is as if you specified `no-conversion' for
10433 Set this to non-nil when a regular text happens to include null bytes.
10434 Examples are Index nodes of Info files and null-byte delimited output
10435 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10436 decode text as usual. */);
10437 inhibit_null_byte_detection
= 0;
10439 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
10440 doc
: /* Char table for translating self-inserting characters.
10441 This is applied to the result of input methods, not their input.
10442 See also `keyboard-translate-table'.
10444 Use of this variable for character code unification was rendered
10445 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10446 internal character representation. */);
10447 Vtranslation_table_for_input
= Qnil
;
10450 Lisp_Object args
[coding_arg_max
];
10451 Lisp_Object plist
[16];
10454 for (i
= 0; i
< coding_arg_max
; i
++)
10457 plist
[0] = intern (":name");
10458 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10459 plist
[2] = intern (":mnemonic");
10460 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10461 plist
[4] = intern (":coding-type");
10462 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10463 plist
[6] = intern (":ascii-compatible-p");
10464 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10465 plist
[8] = intern (":default-char");
10466 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10467 plist
[10] = intern (":for-unibyte");
10468 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10469 plist
[12] = intern (":docstring");
10470 plist
[13] = build_string ("Do no conversion.\n\
10472 When you visit a file with this coding, the file is read into a\n\
10473 unibyte buffer as is, thus each byte of a file is treated as a\n\
10475 plist
[14] = intern (":eol-type");
10476 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10477 args
[coding_arg_plist
] = Flist (16, plist
);
10478 Fdefine_coding_system_internal (coding_arg_max
, args
);
10480 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10481 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10482 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10483 /* This is already set.
10484 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10485 plist
[8] = intern (":charset-list");
10486 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10487 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10488 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
10489 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10490 args
[coding_arg_plist
] = Flist (16, plist
);
10491 Fdefine_coding_system_internal (coding_arg_max
, args
);
10494 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10499 for (i
= 0; i
< coding_category_max
; i
++)
10500 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10502 #if defined (MSDOS) || defined (WINDOWSNT)
10503 system_eol_type
= Qdos
;
10505 system_eol_type
= Qunix
;
10507 staticpro (&system_eol_type
);
10511 emacs_strerror (error_number
)
10516 synchronize_system_messages_locale ();
10517 str
= strerror (error_number
);
10519 if (! NILP (Vlocale_coding_system
))
10521 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10522 Vlocale_coding_system
,
10524 str
= (char *) SDATA (dec
);
10532 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
10533 (do not change this comment) */