1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
12 This file is part of GNU Emacs.
14 GNU Emacs is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 /*** TABLE OF CONTENTS ***
31 2. Emacs' internal format (emacs-utf-8) handlers
34 5. Charset-base coding systems handlers
35 6. emacs-mule (old Emacs' internal format) handlers
37 8. Shift-JIS and BIG5 handlers
39 10. C library functions
40 11. Emacs Lisp library functions
45 /*** 0. General comments ***
50 A coding system is an object for an encoding mechanism that contains
51 information about how to convert byte sequences to character
52 sequences and vice versa. When we say "decode", it means converting
53 a byte sequence of a specific coding system into a character
54 sequence that is represented by Emacs' internal coding system
55 `emacs-utf-8', and when we say "encode", it means converting a
56 character sequence of emacs-utf-8 to a byte sequence of a specific
59 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
60 C level, a coding system is represented by a vector of attributes
61 stored in the hash table Vcharset_hash_table. The conversion from
62 coding system symbol to attributes vector is done by looking up
63 Vcharset_hash_table by the symbol.
65 Coding systems are classified into the following types depending on
66 the encoding mechanism. Here's a brief description of the types.
72 o Charset-base coding system
74 A coding system defined by one or more (coded) character sets.
75 Decoding and encoding are done by a code converter defined for each
78 o Old Emacs internal format (emacs-mule)
80 The coding system adopted by old versions of Emacs (20 and 21).
82 o ISO2022-base coding system
84 The most famous coding system for multiple character sets. X's
85 Compound Text, various EUCs (Extended Unix Code), and coding systems
86 used in the Internet communication such as ISO-2022-JP are all
89 o SJIS (or Shift-JIS or MS-Kanji-Code)
91 A coding system to encode character sets: ASCII, JISX0201, and
92 JISX0208. Widely used for PC's in Japan. Details are described in
97 A coding system to encode character sets: ASCII and Big5. Widely
98 used for Chinese (mainly in Taiwan and Hong Kong). Details are
99 described in section 8. In this file, when we write "big5" (all
100 lowercase), we mean the coding system, and when we write "Big5"
101 (capitalized), we mean the character set.
105 If a user wants to decode/encode text encoded in a coding system
106 not listed above, he can supply a decoder and an encoder for it in
107 CCL (Code Conversion Language) programs. Emacs executes the CCL
108 program while decoding/encoding.
112 A coding system for text containing raw eight-bit data. Emacs
113 treats each byte of source text as a character (except for
114 end-of-line conversion).
118 Like raw text, but don't do end-of-line conversion.
123 How text end-of-line is encoded depends on operating system. For
124 instance, Unix's format is just one byte of LF (line-feed) code,
125 whereas DOS's format is two-byte sequence of `carriage-return' and
126 `line-feed' codes. MacOS's format is usually one byte of
129 Since text character encoding and end-of-line encoding are
130 independent, any coding system described above can take any format
131 of end-of-line (except for no-conversion).
135 Before using a coding system for code conversion (i.e. decoding and
136 encoding), we setup a structure of type `struct coding_system'.
137 This structure keeps various information about a specific code
138 conversion (e.g. the location of source and destination data).
145 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
147 These functions check if a byte sequence specified as a source in
148 CODING conforms to the format of XXX, and update the members of
151 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
153 Below is the template of these functions. */
157 detect_coding_XXX (struct coding_system
*coding
,
158 struct coding_detection_info
*detect_info
)
160 const unsigned char *src
= coding
->source
;
161 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
162 int multibytep
= coding
->src_multibyte
;
163 int consumed_chars
= 0;
169 /* Get one byte from the source. If the source is exhausted, jump
170 to no_more_source:. */
173 if (! __C_conforms_to_XXX___ (c
))
175 if (! __C_strongly_suggests_XXX__ (c
))
176 found
= CATEGORY_MASK_XXX
;
178 /* The byte sequence is invalid for XXX. */
179 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
183 /* The source exhausted successfully. */
184 detect_info
->found
|= found
;
189 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
191 These functions decode a byte sequence specified as a source by
192 CODING. The resulting multibyte text goes to a place pointed to by
193 CODING->charbuf, the length of which should not exceed
194 CODING->charbuf_size;
196 These functions set the information of original and decoded texts in
197 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
198 They also set CODING->result to one of CODING_RESULT_XXX indicating
199 how the decoding is finished.
201 Below is the template of these functions. */
205 decode_coding_XXXX (struct coding_system
*coding
)
207 const unsigned char *src
= coding
->source
+ coding
->consumed
;
208 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
209 /* SRC_BASE remembers the start position in source in each loop.
210 The loop will be exited when there's not enough source code, or
211 when there's no room in CHARBUF for a decoded character. */
212 const unsigned char *src_base
;
213 /* A buffer to produce decoded characters. */
214 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
215 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
216 int multibytep
= coding
->src_multibyte
;
221 if (charbuf
< charbuf_end
)
222 /* No more room to produce a decoded character. */
229 if (src_base
< src_end
230 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
231 /* If the source ends by partial bytes to construct a character,
232 treat them as eight-bit raw data. */
233 while (src_base
< src_end
&& charbuf
< charbuf_end
)
234 *charbuf
++ = *src_base
++;
235 /* Remember how many bytes and characters we consumed. If the
236 source is multibyte, the bytes and chars are not identical. */
237 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
238 /* Remember how many characters we produced. */
239 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
243 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
245 These functions encode SRC_BYTES length text at SOURCE of Emacs'
246 internal multibyte format by CODING. The resulting byte sequence
247 goes to a place pointed to by DESTINATION, the length of which
248 should not exceed DST_BYTES.
250 These functions set the information of original and encoded texts in
251 the members produced, produced_char, consumed, and consumed_char of
252 the structure *CODING. They also set the member result to one of
253 CODING_RESULT_XXX indicating how the encoding finished.
255 DST_BYTES zero means that source area and destination area are
256 overlapped, which means that we can produce a encoded text until it
257 reaches at the head of not-yet-encoded source text.
259 Below is a template of these functions. */
262 encode_coding_XXX (struct coding_system
*coding
)
264 int multibytep
= coding
->dst_multibyte
;
265 int *charbuf
= coding
->charbuf
;
266 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
267 unsigned char *dst
= coding
->destination
+ coding
->produced
;
268 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
269 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
270 int produced_chars
= 0;
272 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
275 /* Encode C into DST, and increment DST. */
277 label_no_more_destination
:
278 /* How many chars and bytes we produced. */
279 coding
->produced_char
+= produced_chars
;
280 coding
->produced
= dst
- coding
->destination
;
285 /*** 1. Preamble ***/
293 #include "character.h"
296 #include "composite.h"
300 #include "termhooks.h"
302 Lisp_Object Vcoding_system_hash_table
;
304 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
305 Lisp_Object Qunix
, Qdos
;
306 Lisp_Object Qbuffer_file_coding_system
;
307 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
308 Lisp_Object Qdefault_char
;
309 Lisp_Object Qno_conversion
, Qundecided
;
310 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
311 Lisp_Object Qbig
, Qlittle
;
312 Lisp_Object Qcoding_system_history
;
313 Lisp_Object Qvalid_codes
;
314 Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
315 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
316 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
317 Lisp_Object QCascii_compatible_p
;
319 Lisp_Object Qcall_process
, Qcall_process_region
;
320 Lisp_Object Qstart_process
, Qopen_network_stream
;
321 Lisp_Object Qtarget_idx
;
323 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
324 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
326 /* If a symbol has this property, evaluate the value to define the
327 symbol as a coding system. */
328 static Lisp_Object Qcoding_system_define_form
;
330 int coding_system_require_warning
;
332 Lisp_Object Vselect_safe_coding_system_function
;
334 /* Mnemonic string for each format of end-of-line. */
335 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
336 /* Mnemonic string to indicate format of end-of-line is not yet
338 Lisp_Object eol_mnemonic_undecided
;
340 /* Format of end-of-line decided by system. This is Qunix on
341 Unix and Mac, Qdos on DOS/Windows.
342 This has an effect only for external encoding (i.e. for output to
343 file and process), not for in-buffer or Lisp string encoding. */
344 static Lisp_Object system_eol_type
;
348 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
350 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
352 /* Coding system emacs-mule and raw-text are for converting only
353 end-of-line format. */
354 Lisp_Object Qemacs_mule
, Qraw_text
;
355 Lisp_Object Qutf_8_emacs
;
357 /* Coding-systems are handed between Emacs Lisp programs and C internal
358 routines by the following three variables. */
359 /* Coding-system for reading files and receiving data from process. */
360 Lisp_Object Vcoding_system_for_read
;
361 /* Coding-system for writing files and sending data to process. */
362 Lisp_Object Vcoding_system_for_write
;
363 /* Coding-system actually used in the latest I/O. */
364 Lisp_Object Vlast_coding_system_used
;
365 /* Set to non-nil when an error is detected while code conversion. */
366 Lisp_Object Vlast_code_conversion_error
;
367 /* A vector of length 256 which contains information about special
368 Latin codes (especially for dealing with Microsoft codes). */
369 Lisp_Object Vlatin_extra_code_table
;
371 /* Flag to inhibit code conversion of end-of-line format. */
372 int inhibit_eol_conversion
;
374 /* Flag to inhibit ISO2022 escape sequence detection. */
375 int inhibit_iso_escape_detection
;
377 /* Flag to inhibit detection of binary files through null bytes. */
378 int inhibit_null_byte_detection
;
380 /* Flag to make buffer-file-coding-system inherit from process-coding. */
381 int inherit_process_coding_system
;
383 /* Coding system to be used to encode text for terminal display when
384 terminal coding system is nil. */
385 struct coding_system safe_terminal_coding
;
387 Lisp_Object Vfile_coding_system_alist
;
388 Lisp_Object Vprocess_coding_system_alist
;
389 Lisp_Object Vnetwork_coding_system_alist
;
391 Lisp_Object Vlocale_coding_system
;
395 /* Flag to tell if we look up translation table on character code
397 Lisp_Object Venable_character_translation
;
398 /* Standard translation table to look up on decoding (reading). */
399 Lisp_Object Vstandard_translation_table_for_decode
;
400 /* Standard translation table to look up on encoding (writing). */
401 Lisp_Object Vstandard_translation_table_for_encode
;
403 Lisp_Object Qtranslation_table
;
404 Lisp_Object Qtranslation_table_id
;
405 Lisp_Object Qtranslation_table_for_decode
;
406 Lisp_Object Qtranslation_table_for_encode
;
408 /* Alist of charsets vs revision number. */
409 static Lisp_Object Vcharset_revision_table
;
411 /* Default coding systems used for process I/O. */
412 Lisp_Object Vdefault_process_coding_system
;
414 /* Char table for translating Quail and self-inserting input. */
415 Lisp_Object Vtranslation_table_for_input
;
417 /* Two special coding systems. */
418 Lisp_Object Vsjis_coding_system
;
419 Lisp_Object Vbig5_coding_system
;
421 /* ISO2022 section */
423 #define CODING_ISO_INITIAL(coding, reg) \
424 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
425 coding_attr_iso_initial), \
429 #define CODING_ISO_REQUEST(coding, charset_id) \
430 (((charset_id) <= (coding)->max_charset_id \
431 ? ((coding)->safe_charsets[charset_id] != 255 \
432 ? (coding)->safe_charsets[charset_id] \
437 #define CODING_ISO_FLAGS(coding) \
438 ((coding)->spec.iso_2022.flags)
439 #define CODING_ISO_DESIGNATION(coding, reg) \
440 ((coding)->spec.iso_2022.current_designation[reg])
441 #define CODING_ISO_INVOCATION(coding, plane) \
442 ((coding)->spec.iso_2022.current_invocation[plane])
443 #define CODING_ISO_SINGLE_SHIFTING(coding) \
444 ((coding)->spec.iso_2022.single_shifting)
445 #define CODING_ISO_BOL(coding) \
446 ((coding)->spec.iso_2022.bol)
447 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
448 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
449 #define CODING_ISO_CMP_STATUS(coding) \
450 (&(coding)->spec.iso_2022.cmp_status)
451 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
452 ((coding)->spec.iso_2022.ctext_extended_segment_len)
453 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
454 ((coding)->spec.iso_2022.embedded_utf_8)
456 /* Control characters of ISO2022. */
457 /* code */ /* function */
458 #define ISO_CODE_LF 0x0A /* line-feed */
459 #define ISO_CODE_CR 0x0D /* carriage-return */
460 #define ISO_CODE_SO 0x0E /* shift-out */
461 #define ISO_CODE_SI 0x0F /* shift-in */
462 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
463 #define ISO_CODE_ESC 0x1B /* escape */
464 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
465 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
466 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
468 /* All code (1-byte) of ISO2022 is classified into one of the
470 enum iso_code_class_type
472 ISO_control_0
, /* Control codes in the range
473 0x00..0x1F and 0x7F, except for the
474 following 5 codes. */
475 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
476 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
477 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
478 ISO_escape
, /* ISO_CODE_SO (0x1B) */
479 ISO_control_1
, /* Control codes in the range
480 0x80..0x9F, except for the
481 following 3 codes. */
482 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
483 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
484 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
485 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
486 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
487 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
488 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
491 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
492 `iso-flags' attribute of an iso2022 coding system. */
494 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
495 instead of the correct short-form sequence (e.g. ESC $ A). */
496 #define CODING_ISO_FLAG_LONG_FORM 0x0001
498 /* If set, reset graphic planes and registers at end-of-line to the
500 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
502 /* If set, reset graphic planes and registers before any control
503 characters to the initial state. */
504 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
506 /* If set, encode by 7-bit environment. */
507 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
509 /* If set, use locking-shift function. */
510 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
512 /* If set, use single-shift function. Overwrite
513 CODING_ISO_FLAG_LOCKING_SHIFT. */
514 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
516 /* If set, use designation escape sequence. */
517 #define CODING_ISO_FLAG_DESIGNATION 0x0040
519 /* If set, produce revision number sequence. */
520 #define CODING_ISO_FLAG_REVISION 0x0080
522 /* If set, produce ISO6429's direction specifying sequence. */
523 #define CODING_ISO_FLAG_DIRECTION 0x0100
525 /* If set, assume designation states are reset at beginning of line on
527 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
529 /* If set, designation sequence should be placed at beginning of line
531 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
533 /* If set, do not encode unsafe characters on output. */
534 #define CODING_ISO_FLAG_SAFE 0x0800
536 /* If set, extra latin codes (128..159) are accepted as a valid code
538 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
540 #define CODING_ISO_FLAG_COMPOSITION 0x2000
542 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
544 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
546 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
548 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
550 /* A character to be produced on output if encoding of the original
551 character is prohibited by CODING_ISO_FLAG_SAFE. */
552 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
555 #define CODING_UTF_8_BOM(coding) \
556 ((coding)->spec.utf_8_bom)
559 #define CODING_UTF_16_BOM(coding) \
560 ((coding)->spec.utf_16.bom)
562 #define CODING_UTF_16_ENDIAN(coding) \
563 ((coding)->spec.utf_16.endian)
565 #define CODING_UTF_16_SURROGATE(coding) \
566 ((coding)->spec.utf_16.surrogate)
570 #define CODING_CCL_DECODER(coding) \
571 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
572 #define CODING_CCL_ENCODER(coding) \
573 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
574 #define CODING_CCL_VALIDS(coding) \
575 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
577 /* Index for each coding category in `coding_categories' */
581 coding_category_iso_7
,
582 coding_category_iso_7_tight
,
583 coding_category_iso_8_1
,
584 coding_category_iso_8_2
,
585 coding_category_iso_7_else
,
586 coding_category_iso_8_else
,
587 coding_category_utf_8_auto
,
588 coding_category_utf_8_nosig
,
589 coding_category_utf_8_sig
,
590 coding_category_utf_16_auto
,
591 coding_category_utf_16_be
,
592 coding_category_utf_16_le
,
593 coding_category_utf_16_be_nosig
,
594 coding_category_utf_16_le_nosig
,
595 coding_category_charset
,
596 coding_category_sjis
,
597 coding_category_big5
,
599 coding_category_emacs_mule
,
600 /* All above are targets of code detection. */
601 coding_category_raw_text
,
602 coding_category_undecided
,
606 /* Definitions of flag bits used in detect_coding_XXXX. */
607 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
608 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
609 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
610 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
611 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
612 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
613 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
614 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
615 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
616 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
617 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
618 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
619 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
620 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
621 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
622 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
623 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
624 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
625 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
626 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
628 /* This value is returned if detect_coding_mask () find nothing other
629 than ASCII characters. */
630 #define CATEGORY_MASK_ANY \
631 (CATEGORY_MASK_ISO_7 \
632 | CATEGORY_MASK_ISO_7_TIGHT \
633 | CATEGORY_MASK_ISO_8_1 \
634 | CATEGORY_MASK_ISO_8_2 \
635 | CATEGORY_MASK_ISO_7_ELSE \
636 | CATEGORY_MASK_ISO_8_ELSE \
637 | CATEGORY_MASK_UTF_8_AUTO \
638 | CATEGORY_MASK_UTF_8_NOSIG \
639 | CATEGORY_MASK_UTF_8_SIG \
640 | CATEGORY_MASK_UTF_16_AUTO \
641 | CATEGORY_MASK_UTF_16_BE \
642 | CATEGORY_MASK_UTF_16_LE \
643 | CATEGORY_MASK_UTF_16_BE_NOSIG \
644 | CATEGORY_MASK_UTF_16_LE_NOSIG \
645 | CATEGORY_MASK_CHARSET \
646 | CATEGORY_MASK_SJIS \
647 | CATEGORY_MASK_BIG5 \
648 | CATEGORY_MASK_CCL \
649 | CATEGORY_MASK_EMACS_MULE)
652 #define CATEGORY_MASK_ISO_7BIT \
653 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
655 #define CATEGORY_MASK_ISO_8BIT \
656 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
658 #define CATEGORY_MASK_ISO_ELSE \
659 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
661 #define CATEGORY_MASK_ISO_ESCAPE \
662 (CATEGORY_MASK_ISO_7 \
663 | CATEGORY_MASK_ISO_7_TIGHT \
664 | CATEGORY_MASK_ISO_7_ELSE \
665 | CATEGORY_MASK_ISO_8_ELSE)
667 #define CATEGORY_MASK_ISO \
668 ( CATEGORY_MASK_ISO_7BIT \
669 | CATEGORY_MASK_ISO_8BIT \
670 | CATEGORY_MASK_ISO_ELSE)
672 #define CATEGORY_MASK_UTF_16 \
673 (CATEGORY_MASK_UTF_16_AUTO \
674 | CATEGORY_MASK_UTF_16_BE \
675 | CATEGORY_MASK_UTF_16_LE \
676 | CATEGORY_MASK_UTF_16_BE_NOSIG \
677 | CATEGORY_MASK_UTF_16_LE_NOSIG)
679 #define CATEGORY_MASK_UTF_8 \
680 (CATEGORY_MASK_UTF_8_AUTO \
681 | CATEGORY_MASK_UTF_8_NOSIG \
682 | CATEGORY_MASK_UTF_8_SIG)
684 /* List of symbols `coding-category-xxx' ordered by priority. This
685 variable is exposed to Emacs Lisp. */
686 static Lisp_Object Vcoding_category_list
;
688 /* Table of coding categories (Lisp symbols). This variable is for
689 internal use only. */
690 static Lisp_Object Vcoding_category_table
;
692 /* Table of coding-categories ordered by priority. */
693 static enum coding_category coding_priorities
[coding_category_max
];
695 /* Nth element is a coding context for the coding system bound to the
696 Nth coding category. */
697 static struct coding_system coding_categories
[coding_category_max
];
699 /*** Commonly used macros and functions ***/
702 #define min(a, b) ((a) < (b) ? (a) : (b))
705 #define max(a, b) ((a) > (b) ? (a) : (b))
708 #define CODING_GET_INFO(coding, attrs, charset_list) \
710 (attrs) = CODING_ID_ATTRS ((coding)->id); \
711 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
715 /* Safely get one byte from the source text pointed by SRC which ends
716 at SRC_END, and set C to that byte. If there are not enough bytes
717 in the source, it jumps to `no_more_source'. If multibytep is
718 nonzero, and a multibyte character is found at SRC, set C to the
719 negative value of the character code. The caller should declare
720 and set these variables appropriately in advance:
721 src, src_end, multibytep */
723 #define ONE_MORE_BYTE(c) \
725 if (src == src_end) \
727 if (src_base < src) \
728 record_conversion_result \
729 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
730 goto no_more_source; \
733 if (multibytep && (c & 0x80)) \
735 if ((c & 0xFE) == 0xC0) \
736 c = ((c & 1) << 6) | *src++; \
740 c = - string_char (src, &src, NULL); \
741 record_conversion_result \
742 (coding, CODING_RESULT_INVALID_SRC); \
748 /* Safely get two bytes from the source text pointed by SRC which ends
749 at SRC_END, and set C1 and C2 to those bytes while skipping the
750 heading multibyte characters. If there are not enough bytes in the
751 source, it jumps to `no_more_source'. If multibytep is nonzero and
752 a multibyte character is found for C2, set C2 to the negative value
753 of the character code. The caller should declare and set these
754 variables appropriately in advance:
755 src, src_end, multibytep
756 It is intended that this macro is used in detect_coding_utf_16. */
758 #define TWO_MORE_BYTES(c1, c2) \
761 if (src == src_end) \
762 goto no_more_source; \
764 if (multibytep && (c1 & 0x80)) \
766 if ((c1 & 0xFE) == 0xC0) \
767 c1 = ((c1 & 1) << 6) | *src++; \
770 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
775 if (src == src_end) \
776 goto no_more_source; \
778 if (multibytep && (c2 & 0x80)) \
780 if ((c2 & 0xFE) == 0xC0) \
781 c2 = ((c2 & 1) << 6) | *src++; \
788 #define ONE_MORE_BYTE_NO_CHECK(c) \
791 if (multibytep && (c & 0x80)) \
793 if ((c & 0xFE) == 0xC0) \
794 c = ((c & 1) << 6) | *src++; \
798 c = - string_char (src, &src, NULL); \
799 record_conversion_result \
800 (coding, CODING_RESULT_INVALID_SRC); \
807 /* Store a byte C in the place pointed by DST and increment DST to the
808 next free point, and increment PRODUCED_CHARS. The caller should
809 assure that C is 0..127, and declare and set the variable `dst'
810 appropriately in advance.
814 #define EMIT_ONE_ASCII_BYTE(c) \
821 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
823 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
825 produced_chars += 2; \
826 *dst++ = (c1), *dst++ = (c2); \
830 /* Store a byte C in the place pointed by DST and increment DST to the
831 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
832 nonzero, store in an appropriate multibyte from. The caller should
833 declare and set the variables `dst' and `multibytep' appropriately
836 #define EMIT_ONE_BYTE(c) \
843 ch = BYTE8_TO_CHAR (ch); \
844 CHAR_STRING_ADVANCE (ch, dst); \
851 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
853 #define EMIT_TWO_BYTES(c1, c2) \
855 produced_chars += 2; \
862 ch = BYTE8_TO_CHAR (ch); \
863 CHAR_STRING_ADVANCE (ch, dst); \
866 ch = BYTE8_TO_CHAR (ch); \
867 CHAR_STRING_ADVANCE (ch, dst); \
877 #define EMIT_THREE_BYTES(c1, c2, c3) \
879 EMIT_ONE_BYTE (c1); \
880 EMIT_TWO_BYTES (c2, c3); \
884 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
886 EMIT_TWO_BYTES (c1, c2); \
887 EMIT_TWO_BYTES (c3, c4); \
891 /* Prototypes for static functions. */
892 static void record_conversion_result (struct coding_system
*coding
,
893 enum coding_result_code result
);
894 static int detect_coding_utf_8 (struct coding_system
*,
895 struct coding_detection_info
*info
);
896 static void decode_coding_utf_8 (struct coding_system
*);
897 static int encode_coding_utf_8 (struct coding_system
*);
899 static int detect_coding_utf_16 (struct coding_system
*,
900 struct coding_detection_info
*info
);
901 static void decode_coding_utf_16 (struct coding_system
*);
902 static int encode_coding_utf_16 (struct coding_system
*);
904 static int detect_coding_iso_2022 (struct coding_system
*,
905 struct coding_detection_info
*info
);
906 static void decode_coding_iso_2022 (struct coding_system
*);
907 static int encode_coding_iso_2022 (struct coding_system
*);
909 static int detect_coding_emacs_mule (struct coding_system
*,
910 struct coding_detection_info
*info
);
911 static void decode_coding_emacs_mule (struct coding_system
*);
912 static int encode_coding_emacs_mule (struct coding_system
*);
914 static int detect_coding_sjis (struct coding_system
*,
915 struct coding_detection_info
*info
);
916 static void decode_coding_sjis (struct coding_system
*);
917 static int encode_coding_sjis (struct coding_system
*);
919 static int detect_coding_big5 (struct coding_system
*,
920 struct coding_detection_info
*info
);
921 static void decode_coding_big5 (struct coding_system
*);
922 static int encode_coding_big5 (struct coding_system
*);
924 static int detect_coding_ccl (struct coding_system
*,
925 struct coding_detection_info
*info
);
926 static void decode_coding_ccl (struct coding_system
*);
927 static int encode_coding_ccl (struct coding_system
*);
929 static void decode_coding_raw_text (struct coding_system
*);
930 static int encode_coding_raw_text (struct coding_system
*);
932 static void coding_set_source (struct coding_system
*);
933 static void coding_set_destination (struct coding_system
*);
934 static void coding_alloc_by_realloc (struct coding_system
*, EMACS_INT
);
935 static void coding_alloc_by_making_gap (struct coding_system
*,
936 EMACS_INT
, EMACS_INT
);
937 static unsigned char *alloc_destination (struct coding_system
*,
938 EMACS_INT
, unsigned char *);
939 static void setup_iso_safe_charsets (Lisp_Object
);
940 static unsigned char *encode_designation_at_bol (struct coding_system
*,
943 static int detect_eol (const unsigned char *,
944 EMACS_INT
, enum coding_category
);
945 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*, int);
946 static void decode_eol (struct coding_system
*);
947 static Lisp_Object
get_translation_table (Lisp_Object
, int, int *);
948 static Lisp_Object
get_translation (Lisp_Object
, int *, int *);
949 static int produce_chars (struct coding_system
*, Lisp_Object
, int);
950 static INLINE
void produce_charset (struct coding_system
*, int *,
952 static void produce_annotation (struct coding_system
*, EMACS_INT
);
953 static int decode_coding (struct coding_system
*);
954 static INLINE
int *handle_composition_annotation (EMACS_INT
, EMACS_INT
,
955 struct coding_system
*,
957 static INLINE
int *handle_charset_annotation (EMACS_INT
, EMACS_INT
,
958 struct coding_system
*,
960 static void consume_chars (struct coding_system
*, Lisp_Object
, int);
961 static int encode_coding (struct coding_system
*);
962 static Lisp_Object
make_conversion_work_buffer (int);
963 static Lisp_Object
code_conversion_restore (Lisp_Object
);
964 static INLINE
int char_encodable_p (int, Lisp_Object
);
965 static Lisp_Object
make_subsidiaries (Lisp_Object
);
968 record_conversion_result (struct coding_system
*coding
,
969 enum coding_result_code result
)
971 coding
->result
= result
;
974 case CODING_RESULT_INSUFFICIENT_SRC
:
975 Vlast_code_conversion_error
= Qinsufficient_source
;
977 case CODING_RESULT_INCONSISTENT_EOL
:
978 Vlast_code_conversion_error
= Qinconsistent_eol
;
980 case CODING_RESULT_INVALID_SRC
:
981 Vlast_code_conversion_error
= Qinvalid_source
;
983 case CODING_RESULT_INTERRUPT
:
984 Vlast_code_conversion_error
= Qinterrupted
;
986 case CODING_RESULT_INSUFFICIENT_MEM
:
987 Vlast_code_conversion_error
= Qinsufficient_memory
;
989 case CODING_RESULT_INSUFFICIENT_DST
:
990 /* Don't record this error in Vlast_code_conversion_error
991 because it happens just temporarily and is resolved when the
992 whole conversion is finished. */
994 case CODING_RESULT_SUCCESS
:
997 Vlast_code_conversion_error
= intern ("Unknown error");
1001 /* This wrapper macro is used to preserve validity of pointers into
1002 buffer text across calls to decode_char, which could cause
1003 relocation of buffers if it loads a charset map, because loading a
1004 charset map allocates large structures. */
1005 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
1007 charset_map_loaded = 0; \
1008 c = DECODE_CHAR (charset, code); \
1009 if (charset_map_loaded) \
1011 const unsigned char *orig = coding->source; \
1014 coding_set_source (coding); \
1015 offset = coding->source - orig; \
1017 src_base += offset; \
1018 src_end += offset; \
1023 /* If there are at least BYTES length of room at dst, allocate memory
1024 for coding->destination and update dst and dst_end. We don't have
1025 to take care of coding->source which will be relocated. It is
1026 handled by calling coding_set_source in encode_coding. */
1028 #define ASSURE_DESTINATION(bytes) \
1030 if (dst + (bytes) >= dst_end) \
1032 int more_bytes = charbuf_end - charbuf + (bytes); \
1034 dst = alloc_destination (coding, more_bytes, dst); \
1035 dst_end = coding->destination + coding->dst_bytes; \
1040 /* Store multibyte form of the character C in P, and advance P to the
1041 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
1042 never calls MAYBE_UNIFY_CHAR. */
1044 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
1046 if ((c) <= MAX_1_BYTE_CHAR) \
1048 else if ((c) <= MAX_2_BYTE_CHAR) \
1049 *(p)++ = (0xC0 | ((c) >> 6)), \
1050 *(p)++ = (0x80 | ((c) & 0x3F)); \
1051 else if ((c) <= MAX_3_BYTE_CHAR) \
1052 *(p)++ = (0xE0 | ((c) >> 12)), \
1053 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
1054 *(p)++ = (0x80 | ((c) & 0x3F)); \
1055 else if ((c) <= MAX_4_BYTE_CHAR) \
1056 *(p)++ = (0xF0 | (c >> 18)), \
1057 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1058 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1059 *(p)++ = (0x80 | (c & 0x3F)); \
1060 else if ((c) <= MAX_5_BYTE_CHAR) \
1062 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
1063 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1064 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1065 *(p)++ = (0x80 | (c & 0x3F)); \
1067 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1071 /* Return the character code of character whose multibyte form is at
1072 P, and advance P to the end of the multibyte form. This is like
1073 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1075 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1078 : ! ((p)[0] & 0x20) \
1080 ((((p)[-2] & 0x1F) << 6) \
1081 | ((p)[-1] & 0x3F) \
1082 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1083 : ! ((p)[0] & 0x10) \
1085 ((((p)[-3] & 0x0F) << 12) \
1086 | (((p)[-2] & 0x3F) << 6) \
1087 | ((p)[-1] & 0x3F))) \
1088 : ! ((p)[0] & 0x08) \
1090 ((((p)[-4] & 0xF) << 18) \
1091 | (((p)[-3] & 0x3F) << 12) \
1092 | (((p)[-2] & 0x3F) << 6) \
1093 | ((p)[-1] & 0x3F))) \
1095 ((((p)[-4] & 0x3F) << 18) \
1096 | (((p)[-3] & 0x3F) << 12) \
1097 | (((p)[-2] & 0x3F) << 6) \
1098 | ((p)[-1] & 0x3F))))
1102 coding_set_source (struct coding_system
*coding
)
1104 if (BUFFERP (coding
->src_object
))
1106 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1108 if (coding
->src_pos
< 0)
1109 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1111 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1113 else if (STRINGP (coding
->src_object
))
1115 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1118 /* Otherwise, the source is C string and is never relocated
1119 automatically. Thus we don't have to update anything. */
1124 coding_set_destination (struct coding_system
*coding
)
1126 if (BUFFERP (coding
->dst_object
))
1128 if (coding
->src_pos
< 0)
1130 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1131 coding
->dst_bytes
= (GAP_END_ADDR
1132 - (coding
->src_bytes
- coding
->consumed
)
1133 - coding
->destination
);
1137 /* We are sure that coding->dst_pos_byte is before the gap
1139 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1140 + coding
->dst_pos_byte
- BEG_BYTE
);
1141 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1142 - coding
->destination
);
1146 /* Otherwise, the destination is C string and is never relocated
1147 automatically. Thus we don't have to update anything. */
1153 coding_alloc_by_realloc (struct coding_system
*coding
, EMACS_INT bytes
)
1155 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1156 coding
->dst_bytes
+ bytes
);
1157 coding
->dst_bytes
+= bytes
;
1161 coding_alloc_by_making_gap (struct coding_system
*coding
,
1162 EMACS_INT gap_head_used
, EMACS_INT bytes
)
1164 if (EQ (coding
->src_object
, coding
->dst_object
))
1166 /* The gap may contain the produced data at the head and not-yet
1167 consumed data at the tail. To preserve those data, we at
1168 first make the gap size to zero, then increase the gap
1170 EMACS_INT add
= GAP_SIZE
;
1172 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1173 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1175 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1176 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1180 Lisp_Object this_buffer
;
1182 this_buffer
= Fcurrent_buffer ();
1183 set_buffer_internal (XBUFFER (coding
->dst_object
));
1185 set_buffer_internal (XBUFFER (this_buffer
));
1190 static unsigned char *
1191 alloc_destination (struct coding_system
*coding
, EMACS_INT nbytes
,
1194 EMACS_INT offset
= dst
- coding
->destination
;
1196 if (BUFFERP (coding
->dst_object
))
1198 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1200 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1203 coding_alloc_by_realloc (coding
, nbytes
);
1204 coding_set_destination (coding
);
1205 dst
= coding
->destination
+ offset
;
1209 /** Macros for annotations. */
1211 /* An annotation data is stored in the array coding->charbuf in this
1213 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1214 LENGTH is the number of elements in the annotation.
1215 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1216 NCHARS is the number of characters in the text annotated.
1218 The format of the following elements depend on ANNOTATION_MASK.
1220 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1222 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1224 NBYTES is the number of bytes specified in the header part of
1225 old-style emacs-mule encoding, or 0 for the other kind of
1228 METHOD is one of enum composition_method.
1230 Optional COMPOSITION-COMPONENTS are characters and composition
1233 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1236 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1237 recover from an invalid annotation, and should be skipped by
1238 produce_annotation. */
1240 /* Maximum length of the header of annotation data. */
1241 #define MAX_ANNOTATION_LENGTH 5
1243 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1245 *(buf)++ = -(len); \
1246 *(buf)++ = (mask); \
1247 *(buf)++ = (nchars); \
1248 coding->annotated = 1; \
1251 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1253 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1259 #define ADD_CHARSET_DATA(buf, nchars, id) \
1261 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1266 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1273 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1274 Check if a text is encoded in UTF-8. If it is, return 1, else
1277 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1278 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1279 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1280 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1281 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1282 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1284 #define UTF_BOM 0xFEFF
1285 #define UTF_8_BOM_1 0xEF
1286 #define UTF_8_BOM_2 0xBB
1287 #define UTF_8_BOM_3 0xBF
1290 detect_coding_utf_8 (struct coding_system
*coding
,
1291 struct coding_detection_info
*detect_info
)
1293 const unsigned char *src
= coding
->source
, *src_base
;
1294 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1295 int multibytep
= coding
->src_multibyte
;
1296 int consumed_chars
= 0;
1300 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1301 /* A coding system of this category is always ASCII compatible. */
1302 src
+= coding
->head_ascii
;
1306 int c
, c1
, c2
, c3
, c4
;
1310 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1313 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1315 if (UTF_8_2_OCTET_LEADING_P (c
))
1321 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1323 if (UTF_8_3_OCTET_LEADING_P (c
))
1326 if (src_base
== coding
->source
1327 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1332 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1334 if (UTF_8_4_OCTET_LEADING_P (c
))
1340 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1342 if (UTF_8_5_OCTET_LEADING_P (c
))
1349 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1353 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1355 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1360 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1361 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1365 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1367 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1374 decode_coding_utf_8 (struct coding_system
*coding
)
1376 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1377 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1378 const unsigned char *src_base
;
1379 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1380 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1381 int consumed_chars
= 0, consumed_chars_base
= 0;
1382 int multibytep
= coding
->src_multibyte
;
1383 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1384 Lisp_Object attr
, charset_list
;
1386 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1387 int byte_after_cr
= -1;
1389 CODING_GET_INFO (coding
, attr
, charset_list
);
1391 if (bom
!= utf_without_bom
)
1397 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1402 if (! UTF_8_EXTRA_OCTET_P (c2
))
1407 if (! UTF_8_EXTRA_OCTET_P (c3
))
1411 if ((c1
!= UTF_8_BOM_1
)
1412 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1415 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1420 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1424 int c
, c1
, c2
, c3
, c4
, c5
;
1427 consumed_chars_base
= consumed_chars
;
1429 if (charbuf
>= charbuf_end
)
1431 if (byte_after_cr
>= 0)
1436 if (byte_after_cr
>= 0)
1437 c1
= byte_after_cr
, byte_after_cr
= -1;
1444 else if (UTF_8_1_OCTET_P (c1
))
1446 if (eol_crlf
&& c1
== '\r')
1447 ONE_MORE_BYTE (byte_after_cr
);
1453 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1455 if (UTF_8_2_OCTET_LEADING_P (c1
))
1457 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1458 /* Reject overlong sequences here and below. Encoders
1459 producing them are incorrect, they can be misleading,
1460 and they mess up read/write invariance. */
1467 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1469 if (UTF_8_3_OCTET_LEADING_P (c1
))
1471 c
= (((c1
& 0xF) << 12)
1472 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1474 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1480 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1482 if (UTF_8_4_OCTET_LEADING_P (c1
))
1484 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1485 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1492 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1494 if (UTF_8_5_OCTET_LEADING_P (c1
))
1496 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1497 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1499 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1514 consumed_chars
= consumed_chars_base
;
1516 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1521 coding
->consumed_char
+= consumed_chars_base
;
1522 coding
->consumed
= src_base
- coding
->source
;
1523 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1528 encode_coding_utf_8 (struct coding_system
*coding
)
1530 int multibytep
= coding
->dst_multibyte
;
1531 int *charbuf
= coding
->charbuf
;
1532 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1533 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1534 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1535 int produced_chars
= 0;
1538 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1540 ASSURE_DESTINATION (3);
1541 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1542 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1547 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1549 while (charbuf
< charbuf_end
)
1551 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1553 ASSURE_DESTINATION (safe_room
);
1555 if (CHAR_BYTE8_P (c
))
1557 c
= CHAR_TO_BYTE8 (c
);
1562 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1563 for (p
= str
; p
< pend
; p
++)
1570 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1572 while (charbuf
< charbuf_end
)
1574 ASSURE_DESTINATION (safe_room
);
1576 if (CHAR_BYTE8_P (c
))
1577 *dst
++ = CHAR_TO_BYTE8 (c
);
1579 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1583 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1584 coding
->produced_char
+= produced_chars
;
1585 coding
->produced
= dst
- coding
->destination
;
1590 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1591 Check if a text is encoded in one of UTF-16 based coding systems.
1592 If it is, return 1, else return 0. */
1594 #define UTF_16_HIGH_SURROGATE_P(val) \
1595 (((val) & 0xFC00) == 0xD800)
1597 #define UTF_16_LOW_SURROGATE_P(val) \
1598 (((val) & 0xFC00) == 0xDC00)
1600 #define UTF_16_INVALID_P(val) \
1601 (((val) == 0xFFFE) \
1602 || ((val) == 0xFFFF) \
1603 || UTF_16_LOW_SURROGATE_P (val))
1607 detect_coding_utf_16 (struct coding_system
*coding
,
1608 struct coding_detection_info
*detect_info
)
1610 const unsigned char *src
= coding
->source
;
1611 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1612 int multibytep
= coding
->src_multibyte
;
1615 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1616 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1617 && (coding
->src_chars
& 1))
1619 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1623 TWO_MORE_BYTES (c1
, c2
);
1624 if ((c1
== 0xFF) && (c2
== 0xFE))
1626 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1627 | CATEGORY_MASK_UTF_16_AUTO
);
1628 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1629 | CATEGORY_MASK_UTF_16_BE_NOSIG
1630 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1632 else if ((c1
== 0xFE) && (c2
== 0xFF))
1634 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1635 | CATEGORY_MASK_UTF_16_AUTO
);
1636 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1637 | CATEGORY_MASK_UTF_16_BE_NOSIG
1638 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1642 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1647 /* We check the dispersion of Eth and Oth bytes where E is even and
1648 O is odd. If both are high, we assume binary data.*/
1649 unsigned char e
[256], o
[256];
1650 unsigned e_num
= 1, o_num
= 1;
1657 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1658 |CATEGORY_MASK_UTF_16_BE
1659 | CATEGORY_MASK_UTF_16_LE
);
1661 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1662 != CATEGORY_MASK_UTF_16
)
1664 TWO_MORE_BYTES (c1
, c2
);
1672 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1679 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1690 decode_coding_utf_16 (struct coding_system
*coding
)
1692 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1693 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1694 const unsigned char *src_base
;
1695 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1696 /* We may produces at most 3 chars in one loop. */
1697 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1698 int consumed_chars
= 0, consumed_chars_base
= 0;
1699 int multibytep
= coding
->src_multibyte
;
1700 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1701 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1702 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1703 Lisp_Object attr
, charset_list
;
1705 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1706 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1708 CODING_GET_INFO (coding
, attr
, charset_list
);
1710 if (bom
== utf_with_bom
)
1719 if (endian
== utf_16_big_endian
1720 ? c
!= 0xFEFF : c
!= 0xFFFE)
1722 /* The first two bytes are not BOM. Treat them as bytes
1723 for a normal character. */
1727 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1729 else if (bom
== utf_detect_bom
)
1731 /* We have already tried to detect BOM and failed in
1733 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1741 consumed_chars_base
= consumed_chars
;
1743 if (charbuf
>= charbuf_end
)
1745 if (byte_after_cr1
>= 0)
1750 if (byte_after_cr1
>= 0)
1751 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1759 if (byte_after_cr2
>= 0)
1760 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1765 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1769 c
= (endian
== utf_16_big_endian
1770 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1774 if (! UTF_16_LOW_SURROGATE_P (c
))
1776 if (endian
== utf_16_big_endian
)
1777 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1779 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1783 if (UTF_16_HIGH_SURROGATE_P (c
))
1784 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1790 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1791 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1792 *charbuf
++ = 0x10000 + c
;
1797 if (UTF_16_HIGH_SURROGATE_P (c
))
1798 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1801 if (eol_crlf
&& c
== '\r')
1803 ONE_MORE_BYTE (byte_after_cr1
);
1804 ONE_MORE_BYTE (byte_after_cr2
);
1812 coding
->consumed_char
+= consumed_chars_base
;
1813 coding
->consumed
= src_base
- coding
->source
;
1814 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1818 encode_coding_utf_16 (struct coding_system
*coding
)
1820 int multibytep
= coding
->dst_multibyte
;
1821 int *charbuf
= coding
->charbuf
;
1822 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1823 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1824 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1826 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1827 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1828 int produced_chars
= 0;
1829 Lisp_Object attrs
, charset_list
;
1832 CODING_GET_INFO (coding
, attrs
, charset_list
);
1834 if (bom
!= utf_without_bom
)
1836 ASSURE_DESTINATION (safe_room
);
1838 EMIT_TWO_BYTES (0xFE, 0xFF);
1840 EMIT_TWO_BYTES (0xFF, 0xFE);
1841 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1844 while (charbuf
< charbuf_end
)
1846 ASSURE_DESTINATION (safe_room
);
1848 if (c
> MAX_UNICODE_CHAR
)
1849 c
= coding
->default_char
;
1854 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1856 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1863 c1
= (c
>> 10) + 0xD800;
1864 c2
= (c
& 0x3FF) + 0xDC00;
1866 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1868 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1871 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1872 coding
->produced
= dst
- coding
->destination
;
1873 coding
->produced_char
+= produced_chars
;
1878 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1880 /* Emacs' internal format for representation of multiple character
1881 sets is a kind of multi-byte encoding, i.e. characters are
1882 represented by variable-length sequences of one-byte codes.
1884 ASCII characters and control characters (e.g. `tab', `newline') are
1885 represented by one-byte sequences which are their ASCII codes, in
1886 the range 0x00 through 0x7F.
1888 8-bit characters of the range 0x80..0x9F are represented by
1889 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1892 8-bit characters of the range 0xA0..0xFF are represented by
1893 one-byte sequences which are their 8-bit code.
1895 The other characters are represented by a sequence of `base
1896 leading-code', optional `extended leading-code', and one or two
1897 `position-code's. The length of the sequence is determined by the
1898 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1899 whereas extended leading-code and position-code take the range 0xA0
1900 through 0xFF. See `charset.h' for more details about leading-code
1903 --- CODE RANGE of Emacs' internal format ---
1907 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1908 eight-bit-graphic 0xA0..0xBF
1909 ELSE 0x81..0x9D + [0xA0..0xFF]+
1910 ---------------------------------------------
1912 As this is the internal character representation, the format is
1913 usually not used externally (i.e. in a file or in a data sent to a
1914 process). But, it is possible to have a text externally in this
1915 format (i.e. by encoding by the coding system `emacs-mule').
1917 In that case, a sequence of one-byte codes has a slightly different
1920 At first, all characters in eight-bit-control are represented by
1921 one-byte sequences which are their 8-bit code.
1923 Next, character composition data are represented by the byte
1924 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1926 METHOD is 0xF2 plus one of composition method (enum
1927 composition_method),
1929 BYTES is 0xA0 plus a byte length of this composition data,
1931 CHARS is 0xA0 plus a number of characters composed by this
1934 COMPONENTs are characters of multibyte form or composition
1935 rules encoded by two-byte of ASCII codes.
1937 In addition, for backward compatibility, the following formats are
1938 also recognized as composition data on decoding.
1941 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1944 MSEQ is a multibyte form but in these special format:
1945 ASCII: 0xA0 ASCII_CODE+0x80,
1946 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1947 RULE is a one byte code of the range 0xA0..0xF0 that
1948 represents a composition rule.
1951 char emacs_mule_bytes
[256];
1954 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1955 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1959 detect_coding_emacs_mule (struct coding_system
*coding
,
1960 struct coding_detection_info
*detect_info
)
1962 const unsigned char *src
= coding
->source
, *src_base
;
1963 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1964 int multibytep
= coding
->src_multibyte
;
1965 int consumed_chars
= 0;
1969 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1970 /* A coding system of this category is always ASCII compatible. */
1971 src
+= coding
->head_ascii
;
1981 /* Perhaps the start of composite character. We simply skip
1982 it because analyzing it is too heavy for detecting. But,
1983 at least, we check that the composite character
1984 constitutes of more than 4 bytes. */
1985 const unsigned char *src_base
;
1995 if (src
- src_base
<= 4)
1997 found
= CATEGORY_MASK_EMACS_MULE
;
2005 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2010 int more_bytes
= emacs_mule_bytes
[c
] - 1;
2012 while (more_bytes
> 0)
2017 src
--; /* Unread the last byte. */
2022 if (more_bytes
!= 0)
2024 found
= CATEGORY_MASK_EMACS_MULE
;
2027 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2031 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
2033 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2036 detect_info
->found
|= found
;
2041 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
2042 character. If CMP_STATUS indicates that we must expect MSEQ or
2043 RULE described above, decode it and return the negative value of
2044 the decoded character or rule. If an invalid byte is found, return
2045 -1. If SRC is too short, return -2. */
2048 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
2049 int *nbytes
, int *nchars
, int *id
,
2050 struct composition_status
*cmp_status
)
2052 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2053 const unsigned char *src_base
= src
;
2054 int multibytep
= coding
->src_multibyte
;
2058 int consumed_chars
= 0;
2065 charset_id
= emacs_mule_charset
[0];
2071 if (cmp_status
->state
!= COMPOSING_NO
2072 && cmp_status
->old_form
)
2074 if (cmp_status
->state
== COMPOSING_CHAR
)
2089 *nbytes
= src
- src_base
;
2090 *nchars
= consumed_chars
;
2098 switch (emacs_mule_bytes
[c
])
2101 if ((charset_id
= emacs_mule_charset
[c
]) < 0)
2110 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2111 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2114 if (c
< 0xA0 || (charset_id
= emacs_mule_charset
[c
]) < 0)
2123 if ((charset_id
= emacs_mule_charset
[c
]) < 0)
2128 code
= (c
& 0x7F) << 8;
2138 if (c
< 0 || (charset_id
= emacs_mule_charset
[c
]) < 0)
2143 code
= (c
& 0x7F) << 8;
2152 charset_id
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2158 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2159 CHARSET_FROM_ID (charset_id
), code
, c
);
2163 *nbytes
= src
- src_base
;
2164 *nchars
= consumed_chars
;
2167 return (mseq_found
? -c
: c
);
2177 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2179 /* Handle these composition sequence ('|': the end of header elements,
2180 BYTES and CHARS >= 0xA0):
2182 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2183 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2184 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2188 (4) relative composition: 0x80 | MSEQ ... MSEQ
2189 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2191 When the starter 0x80 and the following header elements are found,
2192 this annotation header is produced.
2194 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2196 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2197 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2199 Then, upon reading the following elements, these codes are produced
2200 until the composition end is found:
2203 (2) ALT ... ALT CHAR ... CHAR
2204 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2206 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2208 When the composition end is found, LENGTH and NCHARS in the
2209 annotation header is updated as below:
2211 (1) LENGTH: unchanged, NCHARS: unchanged
2212 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2213 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2214 (4) LENGTH: unchanged, NCHARS: number of CHARs
2215 (5) LENGTH: unchanged, NCHARS: number of CHARs
2217 If an error is found while composing, the annotation header is
2218 changed to the original composition header (plus filler -1s) as
2221 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2222 (5) [ 0x80 0xFF -1 -1- -1 ]
2224 and the sequence [ -2 DECODED-RULE ] is changed to the original
2225 byte sequence as below:
2226 o the original byte sequence is B: [ B -1 ]
2227 o the original byte sequence is B1 B2: [ B1 B2 ]
2229 Most of the routines are implemented by macros because many
2230 variables and labels in the caller decode_coding_emacs_mule must be
2231 accessible, and they are usually called just once (thus doesn't
2232 increase the size of compiled object). */
2234 /* Decode a composition rule represented by C as a component of
2235 composition sequence of Emacs 20 style. Set RULE to the decoded
2238 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2243 if (c < 0 || c >= 81) \
2244 goto invalid_code; \
2245 gref = c / 9, nref = c % 9; \
2246 if (gref == 4) gref = 10; \
2247 if (nref == 4) nref = 10; \
2248 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2252 /* Decode a composition rule represented by C and the following byte
2253 at SRC as a component of composition sequence of Emacs 21 style.
2254 Set RULE to the decoded rule. */
2256 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2261 if (gref < 0 || gref >= 81) \
2262 goto invalid_code; \
2263 ONE_MORE_BYTE (c); \
2265 if (nref < 0 || nref >= 81) \
2266 goto invalid_code; \
2267 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2271 /* Start of Emacs 21 style format. The first three bytes at SRC are
2272 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2273 byte length of this composition information, CHARS is the number of
2274 characters composed by this composition. */
2276 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2278 enum composition_method method = c - 0xF2; \
2279 int nbytes, nchars; \
2281 ONE_MORE_BYTE (c); \
2283 goto invalid_code; \
2284 nbytes = c - 0xA0; \
2285 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2286 goto invalid_code; \
2287 ONE_MORE_BYTE (c); \
2288 nchars = c - 0xA0; \
2289 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2290 goto invalid_code; \
2291 cmp_status->old_form = 0; \
2292 cmp_status->method = method; \
2293 if (method == COMPOSITION_RELATIVE) \
2294 cmp_status->state = COMPOSING_CHAR; \
2296 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2297 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2298 cmp_status->nchars = nchars; \
2299 cmp_status->ncomps = nbytes - 4; \
2300 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2304 /* Start of Emacs 20 style format for relative composition. */
2306 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2308 cmp_status->old_form = 1; \
2309 cmp_status->method = COMPOSITION_RELATIVE; \
2310 cmp_status->state = COMPOSING_CHAR; \
2311 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2312 cmp_status->nchars = cmp_status->ncomps = 0; \
2313 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2317 /* Start of Emacs 20 style format for rule-base composition. */
2319 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2321 cmp_status->old_form = 1; \
2322 cmp_status->method = COMPOSITION_WITH_RULE; \
2323 cmp_status->state = COMPOSING_CHAR; \
2324 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2325 cmp_status->nchars = cmp_status->ncomps = 0; \
2326 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2330 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2332 const unsigned char *current_src = src; \
2334 ONE_MORE_BYTE (c); \
2336 goto invalid_code; \
2337 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2338 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2339 DECODE_EMACS_MULE_21_COMPOSITION (); \
2340 else if (c < 0xA0) \
2341 goto invalid_code; \
2342 else if (c < 0xC0) \
2344 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2345 /* Re-read C as a composition component. */ \
2346 src = current_src; \
2348 else if (c == 0xFF) \
2349 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2351 goto invalid_code; \
2354 #define EMACS_MULE_COMPOSITION_END() \
2356 int idx = - cmp_status->length; \
2358 if (cmp_status->old_form) \
2359 charbuf[idx + 2] = cmp_status->nchars; \
2360 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2361 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2362 cmp_status->state = COMPOSING_NO; \
2367 emacs_mule_finish_composition (int *charbuf
,
2368 struct composition_status
*cmp_status
)
2370 int idx
= - cmp_status
->length
;
2373 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2375 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2377 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2378 && cmp_status
->state
== COMPOSING_CHAR
)
2380 /* The last rule was invalid. */
2381 int rule
= charbuf
[-1] + 0xA0;
2383 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2390 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2392 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2394 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2395 charbuf
[idx
++] = -3;
2401 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2402 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2404 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2405 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2406 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2407 charbuf
[idx
++] = -1;
2411 cmp_status
->state
= COMPOSING_NO
;
2415 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2417 if (cmp_status->state != COMPOSING_NO) \
2418 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2423 decode_coding_emacs_mule (struct coding_system
*coding
)
2425 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2426 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2427 const unsigned char *src_base
;
2428 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2429 /* We may produce two annotations (charset and composition) in one
2430 loop and one more charset annotation at the end. */
2432 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
2433 int consumed_chars
= 0, consumed_chars_base
;
2434 int multibytep
= coding
->src_multibyte
;
2435 Lisp_Object attrs
, charset_list
;
2436 int char_offset
= coding
->produced_char
;
2437 int last_offset
= char_offset
;
2438 int last_id
= charset_ascii
;
2440 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2441 int byte_after_cr
= -1;
2442 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2444 CODING_GET_INFO (coding
, attrs
, charset_list
);
2446 if (cmp_status
->state
!= COMPOSING_NO
)
2450 for (i
= 0; i
< cmp_status
->length
; i
++)
2451 *charbuf
++ = cmp_status
->carryover
[i
];
2452 coding
->annotated
= 1;
2460 consumed_chars_base
= consumed_chars
;
2462 if (charbuf
>= charbuf_end
)
2464 if (byte_after_cr
>= 0)
2469 if (byte_after_cr
>= 0)
2470 c
= byte_after_cr
, byte_after_cr
= -1;
2474 if (c
< 0 || c
== 0x80)
2476 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2483 DECODE_EMACS_MULE_COMPOSITION_START ();
2489 if (eol_crlf
&& c
== '\r')
2490 ONE_MORE_BYTE (byte_after_cr
);
2492 if (cmp_status
->state
!= COMPOSING_NO
)
2494 if (cmp_status
->old_form
)
2495 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2496 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2497 cmp_status
->ncomps
--;
2503 /* emacs_mule_char can load a charset map from a file, which
2504 allocates a large structure and might cause buffer text
2505 to be relocated as result. Thus, we need to remember the
2506 original pointer to buffer text, and fix up all related
2507 pointers after the call. */
2508 const unsigned char *orig
= coding
->source
;
2511 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2513 offset
= coding
->source
- orig
;
2527 src
= src_base
+ nbytes
;
2528 consumed_chars
= consumed_chars_base
+ nchars
;
2529 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2530 cmp_status
->ncomps
-= nchars
;
2533 /* Now if C >= 0, we found a normally encoded character, if C <
2534 0, we found an old-style composition component character or
2537 if (cmp_status
->state
== COMPOSING_NO
)
2541 if (last_id
!= charset_ascii
)
2542 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2545 last_offset
= char_offset
;
2550 else if (cmp_status
->state
== COMPOSING_CHAR
)
2552 if (cmp_status
->old_form
)
2556 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2563 cmp_status
->nchars
++;
2564 cmp_status
->length
++;
2565 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2566 EMACS_MULE_COMPOSITION_END ();
2567 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2568 cmp_status
->state
= COMPOSING_RULE
;
2574 cmp_status
->length
++;
2575 cmp_status
->nchars
--;
2576 if (cmp_status
->nchars
== 0)
2577 EMACS_MULE_COMPOSITION_END ();
2580 else if (cmp_status
->state
== COMPOSING_RULE
)
2586 EMACS_MULE_COMPOSITION_END ();
2593 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2598 cmp_status
->length
+= 2;
2599 cmp_status
->state
= COMPOSING_CHAR
;
2602 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2605 cmp_status
->length
++;
2606 if (cmp_status
->ncomps
== 0)
2607 cmp_status
->state
= COMPOSING_CHAR
;
2608 else if (cmp_status
->ncomps
> 0)
2610 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2611 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2614 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2616 else /* COMPOSING_COMPONENT_RULE */
2620 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2625 cmp_status
->length
+= 2;
2626 cmp_status
->ncomps
--;
2627 if (cmp_status
->ncomps
> 0)
2628 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2630 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2635 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2637 consumed_chars
= consumed_chars_base
;
2639 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2645 if (cmp_status
->state
!= COMPOSING_NO
)
2647 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2648 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2653 charbuf
-= cmp_status
->length
;
2654 for (i
= 0; i
< cmp_status
->length
; i
++)
2655 cmp_status
->carryover
[i
] = charbuf
[i
];
2658 if (last_id
!= charset_ascii
)
2659 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2660 coding
->consumed_char
+= consumed_chars_base
;
2661 coding
->consumed
= src_base
- coding
->source
;
2662 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2666 #define EMACS_MULE_LEADING_CODES(id, codes) \
2669 codes[0] = id, codes[1] = 0; \
2670 else if (id < 0xE0) \
2671 codes[0] = 0x9A, codes[1] = id; \
2672 else if (id < 0xF0) \
2673 codes[0] = 0x9B, codes[1] = id; \
2674 else if (id < 0xF5) \
2675 codes[0] = 0x9C, codes[1] = id; \
2677 codes[0] = 0x9D, codes[1] = id; \
2682 encode_coding_emacs_mule (struct coding_system
*coding
)
2684 int multibytep
= coding
->dst_multibyte
;
2685 int *charbuf
= coding
->charbuf
;
2686 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2687 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2688 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2690 int produced_chars
= 0;
2691 Lisp_Object attrs
, charset_list
;
2693 int preferred_charset_id
= -1;
2695 CODING_GET_INFO (coding
, attrs
, charset_list
);
2696 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2698 CODING_ATTR_CHARSET_LIST (attrs
)
2699 = charset_list
= Vemacs_mule_charset_list
;
2702 while (charbuf
< charbuf_end
)
2704 ASSURE_DESTINATION (safe_room
);
2709 /* Handle an annotation. */
2712 case CODING_ANNOTATE_COMPOSITION_MASK
:
2713 /* Not yet implemented. */
2715 case CODING_ANNOTATE_CHARSET_MASK
:
2716 preferred_charset_id
= charbuf
[3];
2717 if (preferred_charset_id
>= 0
2718 && NILP (Fmemq (make_number (preferred_charset_id
),
2720 preferred_charset_id
= -1;
2729 if (ASCII_CHAR_P (c
))
2730 EMIT_ONE_ASCII_BYTE (c
);
2731 else if (CHAR_BYTE8_P (c
))
2733 c
= CHAR_TO_BYTE8 (c
);
2738 struct charset
*charset
;
2742 unsigned char leading_codes
[2];
2744 if (preferred_charset_id
>= 0)
2746 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2747 if (CHAR_CHARSET_P (c
, charset
))
2748 code
= ENCODE_CHAR (charset
, c
);
2750 charset
= char_charset (c
, charset_list
, &code
);
2753 charset
= char_charset (c
, charset_list
, &code
);
2756 c
= coding
->default_char
;
2757 if (ASCII_CHAR_P (c
))
2759 EMIT_ONE_ASCII_BYTE (c
);
2762 charset
= char_charset (c
, charset_list
, &code
);
2764 dimension
= CHARSET_DIMENSION (charset
);
2765 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2766 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2767 EMIT_ONE_BYTE (leading_codes
[0]);
2768 if (leading_codes
[1])
2769 EMIT_ONE_BYTE (leading_codes
[1]);
2771 EMIT_ONE_BYTE (code
| 0x80);
2775 EMIT_ONE_BYTE (code
>> 8);
2776 EMIT_ONE_BYTE (code
& 0xFF);
2780 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2781 coding
->produced_char
+= produced_chars
;
2782 coding
->produced
= dst
- coding
->destination
;
2787 /*** 7. ISO2022 handlers ***/
2789 /* The following note describes the coding system ISO2022 briefly.
2790 Since the intention of this note is to help understand the
2791 functions in this file, some parts are NOT ACCURATE or are OVERLY
2792 SIMPLIFIED. For thorough understanding, please refer to the
2793 original document of ISO2022. This is equivalent to the standard
2794 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2796 ISO2022 provides many mechanisms to encode several character sets
2797 in 7-bit and 8-bit environments. For 7-bit environments, all text
2798 is encoded using bytes less than 128. This may make the encoded
2799 text a little bit longer, but the text passes more easily through
2800 several types of gateway, some of which strip off the MSB (Most
2803 There are two kinds of character sets: control character sets and
2804 graphic character sets. The former contain control characters such
2805 as `newline' and `escape' to provide control functions (control
2806 functions are also provided by escape sequences). The latter
2807 contain graphic characters such as 'A' and '-'. Emacs recognizes
2808 two control character sets and many graphic character sets.
2810 Graphic character sets are classified into one of the following
2811 four classes, according to the number of bytes (DIMENSION) and
2812 number of characters in one dimension (CHARS) of the set:
2813 - DIMENSION1_CHARS94
2814 - DIMENSION1_CHARS96
2815 - DIMENSION2_CHARS94
2816 - DIMENSION2_CHARS96
2818 In addition, each character set is assigned an identification tag,
2819 unique for each set, called the "final character" (denoted as <F>
2820 hereafter). The <F> of each character set is decided by ECMA(*)
2821 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2822 (0x30..0x3F are for private use only).
2824 Note (*): ECMA = European Computer Manufacturers Association
2826 Here are examples of graphic character sets [NAME(<F>)]:
2827 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2828 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2829 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2830 o DIMENSION2_CHARS96 -- none for the moment
2832 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2833 C0 [0x00..0x1F] -- control character plane 0
2834 GL [0x20..0x7F] -- graphic character plane 0
2835 C1 [0x80..0x9F] -- control character plane 1
2836 GR [0xA0..0xFF] -- graphic character plane 1
2838 A control character set is directly designated and invoked to C0 or
2839 C1 by an escape sequence. The most common case is that:
2840 - ISO646's control character set is designated/invoked to C0, and
2841 - ISO6429's control character set is designated/invoked to C1,
2842 and usually these designations/invocations are omitted in encoded
2843 text. In a 7-bit environment, only C0 can be used, and a control
2844 character for C1 is encoded by an appropriate escape sequence to
2845 fit into the environment. All control characters for C1 are
2846 defined to have corresponding escape sequences.
2848 A graphic character set is at first designated to one of four
2849 graphic registers (G0 through G3), then these graphic registers are
2850 invoked to GL or GR. These designations and invocations can be
2851 done independently. The most common case is that G0 is invoked to
2852 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2853 these invocations and designations are omitted in encoded text.
2854 In a 7-bit environment, only GL can be used.
2856 When a graphic character set of CHARS94 is invoked to GL, codes
2857 0x20 and 0x7F of the GL area work as control characters SPACE and
2858 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2861 There are two ways of invocation: locking-shift and single-shift.
2862 With locking-shift, the invocation lasts until the next different
2863 invocation, whereas with single-shift, the invocation affects the
2864 following character only and doesn't affect the locking-shift
2865 state. Invocations are done by the following control characters or
2868 ----------------------------------------------------------------------
2869 abbrev function cntrl escape seq description
2870 ----------------------------------------------------------------------
2871 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2872 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2873 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2874 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2875 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2876 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2877 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2878 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2879 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2880 ----------------------------------------------------------------------
2881 (*) These are not used by any known coding system.
2883 Control characters for these functions are defined by macros
2884 ISO_CODE_XXX in `coding.h'.
2886 Designations are done by the following escape sequences:
2887 ----------------------------------------------------------------------
2888 escape sequence description
2889 ----------------------------------------------------------------------
2890 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2891 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2892 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2893 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2894 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2895 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2896 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2897 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2898 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2899 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2900 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2901 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2902 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2903 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2904 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2905 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2906 ----------------------------------------------------------------------
2908 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2909 of dimension 1, chars 94, and final character <F>, etc...
2911 Note (*): Although these designations are not allowed in ISO2022,
2912 Emacs accepts them on decoding, and produces them on encoding
2913 CHARS96 character sets in a coding system which is characterized as
2914 7-bit environment, non-locking-shift, and non-single-shift.
2916 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2917 '(' must be omitted. We refer to this as "short-form" hereafter.
2919 Now you may notice that there are a lot of ways of encoding the
2920 same multilingual text in ISO2022. Actually, there exist many
2921 coding systems such as Compound Text (used in X11's inter client
2922 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2923 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2924 localized platforms), and all of these are variants of ISO2022.
2926 In addition to the above, Emacs handles two more kinds of escape
2927 sequences: ISO6429's direction specification and Emacs' private
2928 sequence for specifying character composition.
2930 ISO6429's direction specification takes the following form:
2931 o CSI ']' -- end of the current direction
2932 o CSI '0' ']' -- end of the current direction
2933 o CSI '1' ']' -- start of left-to-right text
2934 o CSI '2' ']' -- start of right-to-left text
2935 The control character CSI (0x9B: control sequence introducer) is
2936 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2938 Character composition specification takes the following form:
2939 o ESC '0' -- start relative composition
2940 o ESC '1' -- end composition
2941 o ESC '2' -- start rule-base composition (*)
2942 o ESC '3' -- start relative composition with alternate chars (**)
2943 o ESC '4' -- start rule-base composition with alternate chars (**)
2944 Since these are not standard escape sequences of any ISO standard,
2945 the use of them with these meanings is restricted to Emacs only.
2947 (*) This form is used only in Emacs 20.7 and older versions,
2948 but newer versions can safely decode it.
2949 (**) This form is used only in Emacs 21.1 and newer versions,
2950 and older versions can't decode it.
2952 Here's a list of example usages of these composition escape
2953 sequences (categorized by `enum composition_method').
2955 COMPOSITION_RELATIVE:
2956 ESC 0 CHAR [ CHAR ] ESC 1
2957 COMPOSITION_WITH_RULE:
2958 ESC 2 CHAR [ RULE CHAR ] ESC 1
2959 COMPOSITION_WITH_ALTCHARS:
2960 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2961 COMPOSITION_WITH_RULE_ALTCHARS:
2962 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2964 enum iso_code_class_type iso_code_class
[256];
2966 #define SAFE_CHARSET_P(coding, id) \
2967 ((id) <= (coding)->max_charset_id \
2968 && (coding)->safe_charsets[id] != 255)
2971 #define SHIFT_OUT_OK(category) \
2972 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2975 setup_iso_safe_charsets (Lisp_Object attrs
)
2977 Lisp_Object charset_list
, safe_charsets
;
2978 Lisp_Object request
;
2979 Lisp_Object reg_usage
;
2982 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2985 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2986 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2987 && ! EQ (charset_list
, Viso_2022_charset_list
))
2989 CODING_ATTR_CHARSET_LIST (attrs
)
2990 = charset_list
= Viso_2022_charset_list
;
2991 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2994 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2998 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
3000 int id
= XINT (XCAR (tail
));
3001 if (max_charset_id
< id
)
3002 max_charset_id
= id
;
3005 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
3006 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
3007 request
= AREF (attrs
, coding_attr_iso_request
);
3008 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
3009 reg94
= XINT (XCAR (reg_usage
));
3010 reg96
= XINT (XCDR (reg_usage
));
3012 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
3016 struct charset
*charset
;
3019 charset
= CHARSET_FROM_ID (XINT (id
));
3020 reg
= Fcdr (Fassq (id
, request
));
3022 SSET (safe_charsets
, XINT (id
), XINT (reg
));
3023 else if (charset
->iso_chars_96
)
3026 SSET (safe_charsets
, XINT (id
), reg96
);
3031 SSET (safe_charsets
, XINT (id
), reg94
);
3034 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
3038 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3039 Check if a text is encoded in one of ISO-2022 based coding systems.
3040 If it is, return 1, else return 0. */
3043 detect_coding_iso_2022 (struct coding_system
*coding
,
3044 struct coding_detection_info
*detect_info
)
3046 const unsigned char *src
= coding
->source
, *src_base
= src
;
3047 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3048 int multibytep
= coding
->src_multibyte
;
3049 int single_shifting
= 0;
3052 int consumed_chars
= 0;
3056 int composition_count
= -1;
3058 detect_info
->checked
|= CATEGORY_MASK_ISO
;
3060 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
3062 struct coding_system
*this = &(coding_categories
[i
]);
3063 Lisp_Object attrs
, val
;
3067 attrs
= CODING_ID_ATTRS (this->id
);
3068 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
3069 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
3070 setup_iso_safe_charsets (attrs
);
3071 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
3072 this->max_charset_id
= SCHARS (val
) - 1;
3073 this->safe_charsets
= SDATA (val
);
3076 /* A coding system of this category is always ASCII compatible. */
3077 src
+= coding
->head_ascii
;
3079 while (rejected
!= CATEGORY_MASK_ISO
)
3086 if (inhibit_iso_escape_detection
)
3088 single_shifting
= 0;
3090 if (c
>= '(' && c
<= '/')
3092 /* Designation sequence for a charset of dimension 1. */
3094 if (c1
< ' ' || c1
>= 0x80
3095 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3096 /* Invalid designation sequence. Just ignore. */
3101 /* Designation sequence for a charset of dimension 2. */
3103 if (c
>= '@' && c
<= 'B')
3104 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3105 id
= iso_charset_table
[1][0][c
];
3106 else if (c
>= '(' && c
<= '/')
3109 if (c1
< ' ' || c1
>= 0x80
3110 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3111 /* Invalid designation sequence. Just ignore. */
3115 /* Invalid designation sequence. Just ignore it. */
3118 else if (c
== 'N' || c
== 'O')
3120 /* ESC <Fe> for SS2 or SS3. */
3121 single_shifting
= 1;
3122 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3127 /* End of composition. */
3128 if (composition_count
< 0
3129 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3132 composition_count
= -1;
3133 found
|= CATEGORY_MASK_ISO
;
3135 else if (c
>= '0' && c
<= '4')
3137 /* ESC <Fp> for start/end composition. */
3138 composition_count
= 0;
3143 /* Invalid escape sequence. Just ignore it. */
3147 /* We found a valid designation sequence for CHARSET. */
3148 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3149 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3151 found
|= CATEGORY_MASK_ISO_7
;
3153 rejected
|= CATEGORY_MASK_ISO_7
;
3154 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3156 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3158 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3159 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3161 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3163 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3164 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3166 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3168 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3173 /* Locking shift out/in. */
3174 if (inhibit_iso_escape_detection
)
3176 single_shifting
= 0;
3177 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3181 /* Control sequence introducer. */
3182 single_shifting
= 0;
3183 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3184 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3185 goto check_extra_latin
;
3190 if (inhibit_iso_escape_detection
)
3192 single_shifting
= 0;
3193 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3194 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3195 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3196 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
3197 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3198 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3199 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
3200 if (single_shifting
)
3202 goto check_extra_latin
;
3209 if (composition_count
>= 0)
3210 composition_count
++;
3211 single_shifting
= 0;
3216 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3217 found
|= CATEGORY_MASK_ISO_8_1
;
3218 /* Check the length of succeeding codes of the range
3219 0xA0..0FF. If the byte length is even, we include
3220 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3221 only when we are not single shifting. */
3222 if (! single_shifting
3223 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3226 while (src
< src_end
)
3238 if (i
& 1 && src
< src_end
)
3240 rejected
|= CATEGORY_MASK_ISO_8_2
;
3241 if (composition_count
>= 0)
3242 composition_count
+= i
;
3246 found
|= CATEGORY_MASK_ISO_8_2
;
3247 if (composition_count
>= 0)
3248 composition_count
+= i
/ 2;
3254 single_shifting
= 0;
3255 if (! VECTORP (Vlatin_extra_code_table
)
3256 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3258 rejected
= CATEGORY_MASK_ISO
;
3261 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3262 & CODING_ISO_FLAG_LATIN_EXTRA
)
3263 found
|= CATEGORY_MASK_ISO_8_1
;
3265 rejected
|= CATEGORY_MASK_ISO_8_1
;
3266 rejected
|= CATEGORY_MASK_ISO_8_2
;
3269 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3273 detect_info
->rejected
|= rejected
;
3274 detect_info
->found
|= (found
& ~rejected
);
3279 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3280 escape sequence should be kept. */
3281 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3285 if (final < '0' || final >= 128 \
3286 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3287 || !SAFE_CHARSET_P (coding, id)) \
3289 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3293 prev = CODING_ISO_DESIGNATION (coding, reg); \
3294 if (id == charset_jisx0201_roman) \
3296 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3297 id = charset_ascii; \
3299 else if (id == charset_jisx0208_1978) \
3301 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3302 id = charset_jisx0208; \
3304 CODING_ISO_DESIGNATION (coding, reg) = id; \
3305 /* If there was an invalid designation to REG previously, and this \
3306 designation is ASCII to REG, we should keep this designation \
3308 if (prev == -2 && id == charset_ascii) \
3313 /* Handle these composition sequence (ALT: alternate char):
3315 (1) relative composition: ESC 0 CHAR ... ESC 1
3316 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3317 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3318 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3320 When the start sequence (ESC 0/2/3/4) is found, this annotation
3323 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3325 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3326 produced until the end sequence (ESC 1) is found:
3329 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3330 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3331 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3333 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3334 annotation header is updated as below:
3336 (1) LENGTH: unchanged, NCHARS: number of CHARs
3337 (2) LENGTH: unchanged, NCHARS: number of CHARs
3338 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3339 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3341 If an error is found while composing, the annotation header is
3344 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3346 and the sequence [ -2 DECODED-RULE ] is changed to the original
3347 byte sequence as below:
3348 o the original byte sequence is B: [ B -1 ]
3349 o the original byte sequence is B1 B2: [ B1 B2 ]
3350 and the sequence [ -1 -1 ] is changed to the original byte
3355 /* Decode a composition rule C1 and maybe one more byte from the
3356 source, and set RULE to the encoded composition rule, NBYTES to the
3357 length of the composition rule. If the rule is invalid, set RULE
3358 to some negative value. */
3360 #define DECODE_COMPOSITION_RULE(rule, nbytes) \
3365 if (rule < 81) /* old format (before ver.21) */ \
3367 int gref = (rule) / 9; \
3368 int nref = (rule) % 9; \
3369 if (gref == 4) gref = 10; \
3370 if (nref == 4) nref = 10; \
3371 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3374 else /* new format (after ver.21) */ \
3378 ONE_MORE_BYTE (c); \
3379 rule = COMPOSITION_ENCODE_RULE (rule - 81, c - 32); \
3381 rule += 0x100; /* to destinguish it from the old format */ \
3386 #define ENCODE_COMPOSITION_RULE(rule) \
3388 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3390 if (rule < 0x100) /* old format */ \
3392 if (gref == 10) gref = 4; \
3393 if (nref == 10) nref = 4; \
3394 charbuf[idx] = 32 + gref * 9 + nref; \
3395 charbuf[idx + 1] = -1; \
3398 else /* new format */ \
3400 charbuf[idx] = 32 + 81 + gref; \
3401 charbuf[idx + 1] = 32 + nref; \
3406 /* Finish the current composition as invalid. */
3408 static int finish_composition (int *, struct composition_status
*);
3411 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3413 int idx
= - cmp_status
->length
;
3416 /* Recover the original ESC sequence */
3417 charbuf
[idx
++] = ISO_CODE_ESC
;
3418 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3419 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3420 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3421 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3423 charbuf
[idx
++] = -2;
3425 charbuf
[idx
++] = -1;
3426 new_chars
= cmp_status
->nchars
;
3427 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3428 for (; idx
< 0; idx
++)
3430 int elt
= charbuf
[idx
];
3434 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3439 charbuf
[idx
++] = ISO_CODE_ESC
;
3444 cmp_status
->state
= COMPOSING_NO
;
3448 /* If characters are under composition, finish the composition. */
3449 #define MAYBE_FINISH_COMPOSITION() \
3451 if (cmp_status->state != COMPOSING_NO) \
3452 char_offset += finish_composition (charbuf, cmp_status); \
3455 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3457 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3458 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3459 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3460 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3462 Produce this annotation sequence now:
3464 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3467 #define DECODE_COMPOSITION_START(c1) \
3470 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3471 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3472 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3473 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3477 cmp_status->state = COMPOSING_CHAR; \
3478 cmp_status->length += 2; \
3482 MAYBE_FINISH_COMPOSITION (); \
3483 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3484 : c1 == '2' ? COMPOSITION_WITH_RULE \
3485 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3486 : COMPOSITION_WITH_RULE_ALTCHARS); \
3488 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3489 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3490 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3491 cmp_status->nchars = cmp_status->ncomps = 0; \
3492 coding->annotated = 1; \
3497 /* Handle composition end sequence ESC 1. */
3499 #define DECODE_COMPOSITION_END() \
3501 if (cmp_status->nchars == 0 \
3502 || ((cmp_status->state == COMPOSING_CHAR) \
3503 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3505 MAYBE_FINISH_COMPOSITION (); \
3506 goto invalid_code; \
3508 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3509 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3510 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3511 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3512 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3513 char_offset += cmp_status->nchars; \
3514 cmp_status->state = COMPOSING_NO; \
3517 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3519 #define STORE_COMPOSITION_RULE(rule) \
3522 *charbuf++ = rule; \
3523 cmp_status->length += 2; \
3524 cmp_status->state--; \
3527 /* Store a composed char or a component char C in charbuf, and update
3530 #define STORE_COMPOSITION_CHAR(c) \
3533 cmp_status->length++; \
3534 if (cmp_status->state == COMPOSING_CHAR) \
3535 cmp_status->nchars++; \
3537 cmp_status->ncomps++; \
3538 if (cmp_status->method == COMPOSITION_WITH_RULE \
3539 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3540 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3541 cmp_status->state++; \
3545 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3548 decode_coding_iso_2022 (struct coding_system
*coding
)
3550 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3551 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3552 const unsigned char *src_base
;
3553 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3554 /* We may produce two annotations (charset and composition) in one
3555 loop and one more charset annotation at the end. */
3557 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3558 int consumed_chars
= 0, consumed_chars_base
;
3559 int multibytep
= coding
->src_multibyte
;
3560 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3561 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3562 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3563 int charset_id_2
, charset_id_3
;
3564 struct charset
*charset
;
3566 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3567 Lisp_Object attrs
, charset_list
;
3568 int char_offset
= coding
->produced_char
;
3569 int last_offset
= char_offset
;
3570 int last_id
= charset_ascii
;
3572 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3573 int byte_after_cr
= -1;
3576 CODING_GET_INFO (coding
, attrs
, charset_list
);
3577 setup_iso_safe_charsets (attrs
);
3578 /* Charset list may have been changed. */
3579 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3580 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3582 if (cmp_status
->state
!= COMPOSING_NO
)
3584 for (i
= 0; i
< cmp_status
->length
; i
++)
3585 *charbuf
++ = cmp_status
->carryover
[i
];
3586 coding
->annotated
= 1;
3594 consumed_chars_base
= consumed_chars
;
3596 if (charbuf
>= charbuf_end
)
3598 if (byte_after_cr
>= 0)
3603 if (byte_after_cr
>= 0)
3604 c1
= byte_after_cr
, byte_after_cr
= -1;
3610 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3612 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3614 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3618 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3620 if (c1
== ISO_CODE_ESC
)
3622 if (src
+ 1 >= src_end
)
3623 goto no_more_source
;
3624 *charbuf
++ = ISO_CODE_ESC
;
3626 if (src
[0] == '%' && src
[1] == '@')
3629 consumed_chars
+= 2;
3631 /* We are sure charbuf can contain two more chars. */
3634 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3639 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3645 if ((cmp_status
->state
== COMPOSING_RULE
3646 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3647 && c1
!= ISO_CODE_ESC
)
3651 DECODE_COMPOSITION_RULE (rule
, nbytes
);
3654 STORE_COMPOSITION_RULE (rule
);
3658 /* We produce at most one character. */
3659 switch (iso_code_class
[c1
])
3661 case ISO_0x20_or_0x7F
:
3662 if (charset_id_0
< 0
3663 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3664 /* This is SPACE or DEL. */
3665 charset
= CHARSET_FROM_ID (charset_ascii
);
3667 charset
= CHARSET_FROM_ID (charset_id_0
);
3670 case ISO_graphic_plane_0
:
3671 if (charset_id_0
< 0)
3672 charset
= CHARSET_FROM_ID (charset_ascii
);
3674 charset
= CHARSET_FROM_ID (charset_id_0
);
3677 case ISO_0xA0_or_0xFF
:
3678 if (charset_id_1
< 0
3679 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3680 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3682 /* This is a graphic character, we fall down ... */
3684 case ISO_graphic_plane_1
:
3685 if (charset_id_1
< 0)
3687 charset
= CHARSET_FROM_ID (charset_id_1
);
3691 if (eol_crlf
&& c1
== '\r')
3692 ONE_MORE_BYTE (byte_after_cr
);
3693 MAYBE_FINISH_COMPOSITION ();
3694 charset
= CHARSET_FROM_ID (charset_ascii
);
3701 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3702 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3704 CODING_ISO_INVOCATION (coding
, 0) = 1;
3705 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3709 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3711 CODING_ISO_INVOCATION (coding
, 0) = 0;
3712 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3715 case ISO_single_shift_2_7
:
3716 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3718 case ISO_single_shift_2
:
3719 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3721 /* SS2 is handled as an escape sequence of ESC 'N' */
3723 goto label_escape_sequence
;
3725 case ISO_single_shift_3
:
3726 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3728 /* SS2 is handled as an escape sequence of ESC 'O' */
3730 goto label_escape_sequence
;
3732 case ISO_control_sequence_introducer
:
3733 /* CSI is handled as an escape sequence of ESC '[' ... */
3735 goto label_escape_sequence
;
3739 label_escape_sequence
:
3740 /* Escape sequences handled here are invocation,
3741 designation, direction specification, and character
3742 composition specification. */
3745 case '&': /* revision of following character set */
3747 if (!(c1
>= '@' && c1
<= '~'))
3750 if (c1
!= ISO_CODE_ESC
)
3753 goto label_escape_sequence
;
3755 case '$': /* designation of 2-byte character set */
3756 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3762 if (c1
>= '@' && c1
<= 'B')
3763 { /* designation of JISX0208.1978, GB2312.1980,
3765 reg
= 0, chars96
= 0;
3767 else if (c1
>= 0x28 && c1
<= 0x2B)
3768 { /* designation of DIMENSION2_CHARS94 character set */
3769 reg
= c1
- 0x28, chars96
= 0;
3772 else if (c1
>= 0x2C && c1
<= 0x2F)
3773 { /* designation of DIMENSION2_CHARS96 character set */
3774 reg
= c1
- 0x2C, chars96
= 1;
3779 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3780 /* We must update these variables now. */
3782 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3784 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3790 case 'n': /* invocation of locking-shift-2 */
3791 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3792 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3794 CODING_ISO_INVOCATION (coding
, 0) = 2;
3795 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3798 case 'o': /* invocation of locking-shift-3 */
3799 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3800 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3802 CODING_ISO_INVOCATION (coding
, 0) = 3;
3803 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3806 case 'N': /* invocation of single-shift-2 */
3807 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3808 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3810 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3811 if (charset_id_2
< 0)
3812 charset
= CHARSET_FROM_ID (charset_ascii
);
3814 charset
= CHARSET_FROM_ID (charset_id_2
);
3816 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3820 case 'O': /* invocation of single-shift-3 */
3821 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3822 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3824 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3825 if (charset_id_3
< 0)
3826 charset
= CHARSET_FROM_ID (charset_ascii
);
3828 charset
= CHARSET_FROM_ID (charset_id_3
);
3830 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3834 case '0': case '2': case '3': case '4': /* start composition */
3835 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3837 if (last_id
!= charset_ascii
)
3839 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3840 last_id
= charset_ascii
;
3841 last_offset
= char_offset
;
3843 DECODE_COMPOSITION_START (c1
);
3846 case '1': /* end composition */
3847 if (cmp_status
->state
== COMPOSING_NO
)
3849 DECODE_COMPOSITION_END ();
3852 case '[': /* specification of direction */
3853 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3855 /* For the moment, nested direction is not supported.
3856 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3857 left-to-right, and nonzero means right-to-left. */
3861 case ']': /* end of the current direction */
3862 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3864 case '0': /* end of the current direction */
3865 case '1': /* start of left-to-right direction */
3868 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3873 case '2': /* start of right-to-left direction */
3876 coding
->mode
|= CODING_MODE_DIRECTION
;
3890 /* CTEXT extended segment:
3891 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3892 We keep these bytes as is for the moment.
3893 They may be decoded by post-read-conversion. */
3897 ONE_MORE_BYTE (dim
);
3898 if (dim
< '0' || dim
> '4')
3906 size
= ((M
- 128) * 128) + (L
- 128);
3907 if (charbuf
+ 6 > charbuf_end
)
3909 *charbuf
++ = ISO_CODE_ESC
;
3913 *charbuf
++ = BYTE8_TO_CHAR (M
);
3914 *charbuf
++ = BYTE8_TO_CHAR (L
);
3915 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3919 /* XFree86 extension for embedding UTF-8 in CTEXT:
3920 ESC % G --UTF-8-BYTES-- ESC % @
3921 We keep these bytes as is for the moment.
3922 They may be decoded by post-read-conversion. */
3923 if (charbuf
+ 3 > charbuf_end
)
3925 *charbuf
++ = ISO_CODE_ESC
;
3928 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3936 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3941 if (c1
>= 0x28 && c1
<= 0x2B)
3942 { /* designation of DIMENSION1_CHARS94 character set */
3943 reg
= c1
- 0x28, chars96
= 0;
3946 else if (c1
>= 0x2C && c1
<= 0x2F)
3947 { /* designation of DIMENSION1_CHARS96 character set */
3948 reg
= c1
- 0x2C, chars96
= 1;
3953 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3954 /* We must update these variables now. */
3956 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3958 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3966 if (cmp_status
->state
== COMPOSING_NO
3967 && charset
->id
!= charset_ascii
3968 && last_id
!= charset
->id
)
3970 if (last_id
!= charset_ascii
)
3971 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3972 last_id
= charset
->id
;
3973 last_offset
= char_offset
;
3976 /* Now we know CHARSET and 1st position code C1 of a character.
3977 Produce a decoded character while getting 2nd and 3rd
3978 position codes C2, C3 if necessary. */
3979 if (CHARSET_DIMENSION (charset
) > 1)
3982 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3983 || ((c1
& 0x80) != (c2
& 0x80)))
3984 /* C2 is not in a valid range. */
3986 if (CHARSET_DIMENSION (charset
) == 2)
3987 c1
= (c1
<< 8) | c2
;
3991 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3992 || ((c1
& 0x80) != (c3
& 0x80)))
3993 /* C3 is not in a valid range. */
3995 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3999 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
4002 MAYBE_FINISH_COMPOSITION ();
4003 for (; src_base
< src
; src_base
++, char_offset
++)
4005 if (ASCII_BYTE_P (*src_base
))
4006 *charbuf
++ = *src_base
;
4008 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
4011 else if (cmp_status
->state
== COMPOSING_NO
)
4016 else if ((cmp_status
->state
== COMPOSING_CHAR
4017 ? cmp_status
->nchars
4018 : cmp_status
->ncomps
)
4019 >= MAX_COMPOSITION_COMPONENTS
)
4021 /* Too long composition. */
4022 MAYBE_FINISH_COMPOSITION ();
4027 STORE_COMPOSITION_CHAR (c
);
4031 MAYBE_FINISH_COMPOSITION ();
4033 consumed_chars
= consumed_chars_base
;
4035 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4045 if (cmp_status
->state
!= COMPOSING_NO
)
4047 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
4048 MAYBE_FINISH_COMPOSITION ();
4051 charbuf
-= cmp_status
->length
;
4052 for (i
= 0; i
< cmp_status
->length
; i
++)
4053 cmp_status
->carryover
[i
] = charbuf
[i
];
4056 else if (last_id
!= charset_ascii
)
4057 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4058 coding
->consumed_char
+= consumed_chars_base
;
4059 coding
->consumed
= src_base
- coding
->source
;
4060 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4064 /* ISO2022 encoding stuff. */
4067 It is not enough to say just "ISO2022" on encoding, we have to
4068 specify more details. In Emacs, each coding system of ISO2022
4069 variant has the following specifications:
4070 1. Initial designation to G0 thru G3.
4071 2. Allows short-form designation?
4072 3. ASCII should be designated to G0 before control characters?
4073 4. ASCII should be designated to G0 at end of line?
4074 5. 7-bit environment or 8-bit environment?
4075 6. Use locking-shift?
4076 7. Use Single-shift?
4077 And the following two are only for Japanese:
4078 8. Use ASCII in place of JIS0201-1976-Roman?
4079 9. Use JISX0208-1983 in place of JISX0208-1978?
4080 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4081 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4085 /* Produce codes (escape sequence) for designating CHARSET to graphic
4086 register REG at DST, and increment DST. If <final-char> of CHARSET is
4087 '@', 'A', or 'B' and the coding system CODING allows, produce
4088 designation sequence of short-form. */
4090 #define ENCODE_DESIGNATION(charset, reg, coding) \
4092 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4093 const char *intermediate_char_94 = "()*+"; \
4094 const char *intermediate_char_96 = ",-./"; \
4095 int revision = -1; \
4098 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4099 revision = CHARSET_ISO_REVISION (charset); \
4101 if (revision >= 0) \
4103 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4104 EMIT_ONE_BYTE ('@' + revision); \
4106 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4107 if (CHARSET_DIMENSION (charset) == 1) \
4109 if (! CHARSET_ISO_CHARS_96 (charset)) \
4110 c = intermediate_char_94[reg]; \
4112 c = intermediate_char_96[reg]; \
4113 EMIT_ONE_ASCII_BYTE (c); \
4117 EMIT_ONE_ASCII_BYTE ('$'); \
4118 if (! CHARSET_ISO_CHARS_96 (charset)) \
4120 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4122 || final_char < '@' || final_char > 'B') \
4123 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4126 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4128 EMIT_ONE_ASCII_BYTE (final_char); \
4130 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4134 /* The following two macros produce codes (control character or escape
4135 sequence) for ISO2022 single-shift functions (single-shift-2 and
4138 #define ENCODE_SINGLE_SHIFT_2 \
4140 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4141 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4143 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4144 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4148 #define ENCODE_SINGLE_SHIFT_3 \
4150 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4151 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4153 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4154 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4158 /* The following four macros produce codes (control character or
4159 escape sequence) for ISO2022 locking-shift functions (shift-in,
4160 shift-out, locking-shift-2, and locking-shift-3). */
4162 #define ENCODE_SHIFT_IN \
4164 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4165 CODING_ISO_INVOCATION (coding, 0) = 0; \
4169 #define ENCODE_SHIFT_OUT \
4171 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4172 CODING_ISO_INVOCATION (coding, 0) = 1; \
4176 #define ENCODE_LOCKING_SHIFT_2 \
4178 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4179 CODING_ISO_INVOCATION (coding, 0) = 2; \
4183 #define ENCODE_LOCKING_SHIFT_3 \
4185 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4186 CODING_ISO_INVOCATION (coding, 0) = 3; \
4190 /* Produce codes for a DIMENSION1 character whose character set is
4191 CHARSET and whose position-code is C1. Designation and invocation
4192 sequences are also produced in advance if necessary. */
4194 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4196 int id = CHARSET_ID (charset); \
4198 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4199 && id == charset_ascii) \
4201 id = charset_jisx0201_roman; \
4202 charset = CHARSET_FROM_ID (id); \
4205 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4207 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4208 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4210 EMIT_ONE_BYTE (c1 | 0x80); \
4211 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4214 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4216 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4219 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4221 EMIT_ONE_BYTE (c1 | 0x80); \
4225 /* Since CHARSET is not yet invoked to any graphic planes, we \
4226 must invoke it, or, at first, designate it to some graphic \
4227 register. Then repeat the loop to actually produce the \
4229 dst = encode_invocation_designation (charset, coding, dst, \
4234 /* Produce codes for a DIMENSION2 character whose character set is
4235 CHARSET and whose position-codes are C1 and C2. Designation and
4236 invocation codes are also produced in advance if necessary. */
4238 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4240 int id = CHARSET_ID (charset); \
4242 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4243 && id == charset_jisx0208) \
4245 id = charset_jisx0208_1978; \
4246 charset = CHARSET_FROM_ID (id); \
4249 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4251 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4252 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4254 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4255 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4258 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4260 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4263 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4265 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4269 /* Since CHARSET is not yet invoked to any graphic planes, we \
4270 must invoke it, or, at first, designate it to some graphic \
4271 register. Then repeat the loop to actually produce the \
4273 dst = encode_invocation_designation (charset, coding, dst, \
4278 #define ENCODE_ISO_CHARACTER(charset, c) \
4280 int code = ENCODE_CHAR ((charset), (c)); \
4282 if (CHARSET_DIMENSION (charset) == 1) \
4283 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4285 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4289 /* Produce designation and invocation codes at a place pointed by DST
4290 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4294 encode_invocation_designation (struct charset
*charset
,
4295 struct coding_system
*coding
,
4296 unsigned char *dst
, int *p_nchars
)
4298 int multibytep
= coding
->dst_multibyte
;
4299 int produced_chars
= *p_nchars
;
4300 int reg
; /* graphic register number */
4301 int id
= CHARSET_ID (charset
);
4303 /* At first, check designations. */
4304 for (reg
= 0; reg
< 4; reg
++)
4305 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4310 /* CHARSET is not yet designated to any graphic registers. */
4311 /* At first check the requested designation. */
4312 reg
= CODING_ISO_REQUEST (coding
, id
);
4314 /* Since CHARSET requests no special designation, designate it
4315 to graphic register 0. */
4318 ENCODE_DESIGNATION (charset
, reg
, coding
);
4321 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4322 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4324 /* Since the graphic register REG is not invoked to any graphic
4325 planes, invoke it to graphic plane 0. */
4328 case 0: /* graphic register 0 */
4332 case 1: /* graphic register 1 */
4336 case 2: /* graphic register 2 */
4337 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4338 ENCODE_SINGLE_SHIFT_2
;
4340 ENCODE_LOCKING_SHIFT_2
;
4343 case 3: /* graphic register 3 */
4344 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4345 ENCODE_SINGLE_SHIFT_3
;
4347 ENCODE_LOCKING_SHIFT_3
;
4352 *p_nchars
= produced_chars
;
4356 /* The following three macros produce codes for indicating direction
4358 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
4360 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
4361 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
4363 EMIT_ONE_BYTE (ISO_CODE_CSI); \
4367 #define ENCODE_DIRECTION_R2L() \
4369 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4370 EMIT_TWO_ASCII_BYTES ('2', ']'); \
4374 #define ENCODE_DIRECTION_L2R() \
4376 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4377 EMIT_TWO_ASCII_BYTES ('0', ']'); \
4381 /* Produce codes for designation and invocation to reset the graphic
4382 planes and registers to initial state. */
4383 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4386 struct charset *charset; \
4388 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4390 for (reg = 0; reg < 4; reg++) \
4391 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4392 && (CODING_ISO_DESIGNATION (coding, reg) \
4393 != CODING_ISO_INITIAL (coding, reg))) \
4395 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4396 ENCODE_DESIGNATION (charset, reg, coding); \
4401 /* Produce designation sequences of charsets in the line started from
4402 SRC to a place pointed by DST, and return updated DST.
4404 If the current block ends before any end-of-line, we may fail to
4405 find all the necessary designations. */
4407 static unsigned char *
4408 encode_designation_at_bol (struct coding_system
*coding
, int *charbuf
,
4409 int *charbuf_end
, unsigned char *dst
)
4411 struct charset
*charset
;
4412 /* Table of charsets to be designated to each graphic register. */
4414 int c
, found
= 0, reg
;
4415 int produced_chars
= 0;
4416 int multibytep
= coding
->dst_multibyte
;
4418 Lisp_Object charset_list
;
4420 attrs
= CODING_ID_ATTRS (coding
->id
);
4421 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4422 if (EQ (charset_list
, Qiso_2022
))
4423 charset_list
= Viso_2022_charset_list
;
4425 for (reg
= 0; reg
< 4; reg
++)
4435 charset
= char_charset (c
, charset_list
, NULL
);
4436 id
= CHARSET_ID (charset
);
4437 reg
= CODING_ISO_REQUEST (coding
, id
);
4438 if (reg
>= 0 && r
[reg
] < 0)
4447 for (reg
= 0; reg
< 4; reg
++)
4449 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4450 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4456 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4459 encode_coding_iso_2022 (struct coding_system
*coding
)
4461 int multibytep
= coding
->dst_multibyte
;
4462 int *charbuf
= coding
->charbuf
;
4463 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4464 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4465 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4468 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4469 && CODING_ISO_BOL (coding
));
4470 int produced_chars
= 0;
4471 Lisp_Object attrs
, eol_type
, charset_list
;
4472 int ascii_compatible
;
4474 int preferred_charset_id
= -1;
4476 CODING_GET_INFO (coding
, attrs
, charset_list
);
4477 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4478 if (VECTORP (eol_type
))
4481 setup_iso_safe_charsets (attrs
);
4482 /* Charset list may have been changed. */
4483 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4484 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4487 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4488 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4489 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4491 while (charbuf
< charbuf_end
)
4493 ASSURE_DESTINATION (safe_room
);
4495 if (bol_designation
)
4497 unsigned char *dst_prev
= dst
;
4499 /* We have to produce designation sequences if any now. */
4500 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
4501 bol_designation
= 0;
4502 /* We are sure that designation sequences are all ASCII bytes. */
4503 produced_chars
+= dst
- dst_prev
;
4510 /* Handle an annotation. */
4513 case CODING_ANNOTATE_COMPOSITION_MASK
:
4514 /* Not yet implemented. */
4516 case CODING_ANNOTATE_CHARSET_MASK
:
4517 preferred_charset_id
= charbuf
[2];
4518 if (preferred_charset_id
>= 0
4519 && NILP (Fmemq (make_number (preferred_charset_id
),
4521 preferred_charset_id
= -1;
4530 /* Now encode the character C. */
4531 if (c
< 0x20 || c
== 0x7F)
4534 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4536 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4537 ENCODE_RESET_PLANE_AND_REGISTER ();
4538 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4542 for (i
= 0; i
< 4; i
++)
4543 CODING_ISO_DESIGNATION (coding
, i
)
4544 = CODING_ISO_INITIAL (coding
, i
);
4547 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4549 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4550 ENCODE_RESET_PLANE_AND_REGISTER ();
4551 EMIT_ONE_ASCII_BYTE (c
);
4553 else if (ASCII_CHAR_P (c
))
4555 if (ascii_compatible
)
4556 EMIT_ONE_ASCII_BYTE (c
);
4559 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4560 ENCODE_ISO_CHARACTER (charset
, c
);
4563 else if (CHAR_BYTE8_P (c
))
4565 c
= CHAR_TO_BYTE8 (c
);
4570 struct charset
*charset
;
4572 if (preferred_charset_id
>= 0)
4574 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4575 if (! CHAR_CHARSET_P (c
, charset
))
4576 charset
= char_charset (c
, charset_list
, NULL
);
4579 charset
= char_charset (c
, charset_list
, NULL
);
4582 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4584 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4585 charset
= CHARSET_FROM_ID (charset_ascii
);
4589 c
= coding
->default_char
;
4590 charset
= char_charset (c
, charset_list
, NULL
);
4593 ENCODE_ISO_CHARACTER (charset
, c
);
4597 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4598 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4600 ASSURE_DESTINATION (safe_room
);
4601 ENCODE_RESET_PLANE_AND_REGISTER ();
4603 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4604 CODING_ISO_BOL (coding
) = bol_designation
;
4605 coding
->produced_char
+= produced_chars
;
4606 coding
->produced
= dst
- coding
->destination
;
4611 /*** 8,9. SJIS and BIG5 handlers ***/
4613 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4614 quite widely. So, for the moment, Emacs supports them in the bare
4615 C code. But, in the future, they may be supported only by CCL. */
4617 /* SJIS is a coding system encoding three character sets: ASCII, right
4618 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4619 as is. A character of charset katakana-jisx0201 is encoded by
4620 "position-code + 0x80". A character of charset japanese-jisx0208
4621 is encoded in 2-byte but two position-codes are divided and shifted
4622 so that it fit in the range below.
4624 --- CODE RANGE of SJIS ---
4625 (character set) (range)
4627 KATAKANA-JISX0201 0xA0 .. 0xDF
4628 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4629 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4630 -------------------------------
4634 /* BIG5 is a coding system encoding two character sets: ASCII and
4635 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4636 character set and is encoded in two-byte.
4638 --- CODE RANGE of BIG5 ---
4639 (character set) (range)
4641 Big5 (1st byte) 0xA1 .. 0xFE
4642 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4643 --------------------------
4647 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4648 Check if a text is encoded in SJIS. If it is, return
4649 CATEGORY_MASK_SJIS, else return 0. */
4652 detect_coding_sjis (struct coding_system
*coding
,
4653 struct coding_detection_info
*detect_info
)
4655 const unsigned char *src
= coding
->source
, *src_base
;
4656 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4657 int multibytep
= coding
->src_multibyte
;
4658 int consumed_chars
= 0;
4661 Lisp_Object attrs
, charset_list
;
4662 int max_first_byte_of_2_byte_code
;
4664 CODING_GET_INFO (coding
, attrs
, charset_list
);
4665 max_first_byte_of_2_byte_code
4666 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4668 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4669 /* A coding system of this category is always ASCII compatible. */
4670 src
+= coding
->head_ascii
;
4678 if ((c
>= 0x81 && c
<= 0x9F)
4679 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4682 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4684 found
= CATEGORY_MASK_SJIS
;
4686 else if (c
>= 0xA0 && c
< 0xE0)
4687 found
= CATEGORY_MASK_SJIS
;
4691 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4695 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4697 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4700 detect_info
->found
|= found
;
4704 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4705 Check if a text is encoded in BIG5. If it is, return
4706 CATEGORY_MASK_BIG5, else return 0. */
4709 detect_coding_big5 (struct coding_system
*coding
,
4710 struct coding_detection_info
*detect_info
)
4712 const unsigned char *src
= coding
->source
, *src_base
;
4713 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4714 int multibytep
= coding
->src_multibyte
;
4715 int consumed_chars
= 0;
4719 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4720 /* A coding system of this category is always ASCII compatible. */
4721 src
+= coding
->head_ascii
;
4732 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4734 found
= CATEGORY_MASK_BIG5
;
4739 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4743 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4745 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4748 detect_info
->found
|= found
;
4752 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4753 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4756 decode_coding_sjis (struct coding_system
*coding
)
4758 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4759 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4760 const unsigned char *src_base
;
4761 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4762 /* We may produce one charset annotation in one loop and one more at
4765 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4766 int consumed_chars
= 0, consumed_chars_base
;
4767 int multibytep
= coding
->src_multibyte
;
4768 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4769 struct charset
*charset_kanji2
;
4770 Lisp_Object attrs
, charset_list
, val
;
4771 int char_offset
= coding
->produced_char
;
4772 int last_offset
= char_offset
;
4773 int last_id
= charset_ascii
;
4775 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4776 int byte_after_cr
= -1;
4778 CODING_GET_INFO (coding
, attrs
, charset_list
);
4781 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4782 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4783 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4784 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4789 struct charset
*charset
;
4792 consumed_chars_base
= consumed_chars
;
4794 if (charbuf
>= charbuf_end
)
4796 if (byte_after_cr
>= 0)
4801 if (byte_after_cr
>= 0)
4802 c
= byte_after_cr
, byte_after_cr
= -1;
4809 if (eol_crlf
&& c
== '\r')
4810 ONE_MORE_BYTE (byte_after_cr
);
4811 charset
= charset_roman
;
4813 else if (c
== 0x80 || c
== 0xA0)
4815 else if (c
>= 0xA1 && c
<= 0xDF)
4817 /* SJIS -> JISX0201-Kana */
4819 charset
= charset_kana
;
4823 /* SJIS -> JISX0208 */
4825 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4829 charset
= charset_kanji
;
4831 else if (c
<= 0xFC && charset_kanji2
)
4833 /* SJIS -> JISX0213-2 */
4835 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4839 charset
= charset_kanji2
;
4843 if (charset
->id
!= charset_ascii
4844 && last_id
!= charset
->id
)
4846 if (last_id
!= charset_ascii
)
4847 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4848 last_id
= charset
->id
;
4849 last_offset
= char_offset
;
4851 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4858 consumed_chars
= consumed_chars_base
;
4860 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4866 if (last_id
!= charset_ascii
)
4867 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4868 coding
->consumed_char
+= consumed_chars_base
;
4869 coding
->consumed
= src_base
- coding
->source
;
4870 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4874 decode_coding_big5 (struct coding_system
*coding
)
4876 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4877 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4878 const unsigned char *src_base
;
4879 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4880 /* We may produce one charset annotation in one loop and one more at
4883 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4884 int consumed_chars
= 0, consumed_chars_base
;
4885 int multibytep
= coding
->src_multibyte
;
4886 struct charset
*charset_roman
, *charset_big5
;
4887 Lisp_Object attrs
, charset_list
, val
;
4888 int char_offset
= coding
->produced_char
;
4889 int last_offset
= char_offset
;
4890 int last_id
= charset_ascii
;
4892 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4893 int byte_after_cr
= -1;
4895 CODING_GET_INFO (coding
, attrs
, charset_list
);
4897 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4898 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4903 struct charset
*charset
;
4906 consumed_chars_base
= consumed_chars
;
4908 if (charbuf
>= charbuf_end
)
4910 if (byte_after_cr
>= 0)
4915 if (byte_after_cr
>= 0)
4916 c
= byte_after_cr
, byte_after_cr
= -1;
4924 if (eol_crlf
&& c
== '\r')
4925 ONE_MORE_BYTE (byte_after_cr
);
4926 charset
= charset_roman
;
4931 if (c
< 0xA1 || c
> 0xFE)
4934 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4937 charset
= charset_big5
;
4939 if (charset
->id
!= charset_ascii
4940 && last_id
!= charset
->id
)
4942 if (last_id
!= charset_ascii
)
4943 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4944 last_id
= charset
->id
;
4945 last_offset
= char_offset
;
4947 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4954 consumed_chars
= consumed_chars_base
;
4956 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4962 if (last_id
!= charset_ascii
)
4963 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4964 coding
->consumed_char
+= consumed_chars_base
;
4965 coding
->consumed
= src_base
- coding
->source
;
4966 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4969 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4970 This function can encode charsets `ascii', `katakana-jisx0201',
4971 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4972 are sure that all these charsets are registered as official charset
4973 (i.e. do not have extended leading-codes). Characters of other
4974 charsets are produced without any encoding. If SJIS_P is 1, encode
4975 SJIS text, else encode BIG5 text. */
4978 encode_coding_sjis (struct coding_system
*coding
)
4980 int multibytep
= coding
->dst_multibyte
;
4981 int *charbuf
= coding
->charbuf
;
4982 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4983 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4984 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4986 int produced_chars
= 0;
4987 Lisp_Object attrs
, charset_list
, val
;
4988 int ascii_compatible
;
4989 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4990 struct charset
*charset_kanji2
;
4993 CODING_GET_INFO (coding
, attrs
, charset_list
);
4995 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4996 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4997 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4998 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
5000 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5002 while (charbuf
< charbuf_end
)
5004 ASSURE_DESTINATION (safe_room
);
5006 /* Now encode the character C. */
5007 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5008 EMIT_ONE_ASCII_BYTE (c
);
5009 else if (CHAR_BYTE8_P (c
))
5011 c
= CHAR_TO_BYTE8 (c
);
5017 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
5021 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5023 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5024 charset
= CHARSET_FROM_ID (charset_ascii
);
5028 c
= coding
->default_char
;
5029 charset
= char_charset (c
, charset_list
, &code
);
5032 if (code
== CHARSET_INVALID_CODE (charset
))
5034 if (charset
== charset_kanji
)
5038 c1
= code
>> 8, c2
= code
& 0xFF;
5039 EMIT_TWO_BYTES (c1
, c2
);
5041 else if (charset
== charset_kana
)
5042 EMIT_ONE_BYTE (code
| 0x80);
5043 else if (charset_kanji2
&& charset
== charset_kanji2
)
5048 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
5050 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
5052 JIS_TO_SJIS2 (code
);
5053 c1
= code
>> 8, c2
= code
& 0xFF;
5054 EMIT_TWO_BYTES (c1
, c2
);
5057 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5060 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5063 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5064 coding
->produced_char
+= produced_chars
;
5065 coding
->produced
= dst
- coding
->destination
;
5070 encode_coding_big5 (struct coding_system
*coding
)
5072 int multibytep
= coding
->dst_multibyte
;
5073 int *charbuf
= coding
->charbuf
;
5074 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5075 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5076 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5078 int produced_chars
= 0;
5079 Lisp_Object attrs
, charset_list
, val
;
5080 int ascii_compatible
;
5081 struct charset
*charset_roman
, *charset_big5
;
5084 CODING_GET_INFO (coding
, attrs
, charset_list
);
5086 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5087 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5088 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5090 while (charbuf
< charbuf_end
)
5092 ASSURE_DESTINATION (safe_room
);
5094 /* Now encode the character C. */
5095 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5096 EMIT_ONE_ASCII_BYTE (c
);
5097 else if (CHAR_BYTE8_P (c
))
5099 c
= CHAR_TO_BYTE8 (c
);
5105 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
5109 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5111 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5112 charset
= CHARSET_FROM_ID (charset_ascii
);
5116 c
= coding
->default_char
;
5117 charset
= char_charset (c
, charset_list
, &code
);
5120 if (code
== CHARSET_INVALID_CODE (charset
))
5122 if (charset
== charset_big5
)
5126 c1
= code
>> 8, c2
= code
& 0xFF;
5127 EMIT_TWO_BYTES (c1
, c2
);
5130 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5133 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5134 coding
->produced_char
+= produced_chars
;
5135 coding
->produced
= dst
- coding
->destination
;
5140 /*** 10. CCL handlers ***/
5142 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5143 Check if a text is encoded in a coding system of which
5144 encoder/decoder are written in CCL program. If it is, return
5145 CATEGORY_MASK_CCL, else return 0. */
5148 detect_coding_ccl (struct coding_system
*coding
,
5149 struct coding_detection_info
*detect_info
)
5151 const unsigned char *src
= coding
->source
, *src_base
;
5152 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5153 int multibytep
= coding
->src_multibyte
;
5154 int consumed_chars
= 0;
5156 unsigned char *valids
;
5157 int head_ascii
= coding
->head_ascii
;
5160 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5162 coding
= &coding_categories
[coding_category_ccl
];
5163 valids
= CODING_CCL_VALIDS (coding
);
5164 attrs
= CODING_ID_ATTRS (coding
->id
);
5165 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5174 if (c
< 0 || ! valids
[c
])
5176 if ((valids
[c
] > 1))
5177 found
= CATEGORY_MASK_CCL
;
5179 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5183 detect_info
->found
|= found
;
5188 decode_coding_ccl (struct coding_system
*coding
)
5190 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5191 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5192 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5193 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5194 int consumed_chars
= 0;
5195 int multibytep
= coding
->src_multibyte
;
5196 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5197 int source_charbuf
[1024];
5198 int source_byteidx
[1025];
5199 Lisp_Object attrs
, charset_list
;
5201 CODING_GET_INFO (coding
, attrs
, charset_list
);
5205 const unsigned char *p
= src
;
5210 while (i
< 1024 && p
< src_end
)
5212 source_byteidx
[i
] = p
- src
;
5213 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5215 source_byteidx
[i
] = p
- src
;
5218 while (i
< 1024 && p
< src_end
)
5219 source_charbuf
[i
++] = *p
++;
5221 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5222 ccl
->last_block
= 1;
5223 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5225 charbuf
+= ccl
->produced
;
5227 src
+= source_byteidx
[ccl
->consumed
];
5229 src
+= ccl
->consumed
;
5230 consumed_chars
+= ccl
->consumed
;
5231 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5235 switch (ccl
->status
)
5237 case CCL_STAT_SUSPEND_BY_SRC
:
5238 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5240 case CCL_STAT_SUSPEND_BY_DST
:
5241 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5244 case CCL_STAT_INVALID_CMD
:
5245 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5248 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5251 coding
->consumed_char
+= consumed_chars
;
5252 coding
->consumed
= src
- coding
->source
;
5253 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5257 encode_coding_ccl (struct coding_system
*coding
)
5259 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5260 int multibytep
= coding
->dst_multibyte
;
5261 int *charbuf
= coding
->charbuf
;
5262 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5263 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5264 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5265 int destination_charbuf
[1024];
5266 int i
, produced_chars
= 0;
5267 Lisp_Object attrs
, charset_list
;
5269 CODING_GET_INFO (coding
, attrs
, charset_list
);
5270 if (coding
->consumed_char
== coding
->src_chars
5271 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5272 ccl
->last_block
= 1;
5274 while (charbuf
< charbuf_end
)
5276 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5277 charbuf_end
- charbuf
, 1024, charset_list
);
5280 ASSURE_DESTINATION (ccl
->produced
* 2);
5281 for (i
= 0; i
< ccl
->produced
; i
++)
5282 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5286 ASSURE_DESTINATION (ccl
->produced
);
5287 for (i
= 0; i
< ccl
->produced
; i
++)
5288 *dst
++ = destination_charbuf
[i
] & 0xFF;
5289 produced_chars
+= ccl
->produced
;
5291 charbuf
+= ccl
->consumed
;
5292 if (ccl
->status
== CCL_STAT_QUIT
5293 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5297 switch (ccl
->status
)
5299 case CCL_STAT_SUSPEND_BY_SRC
:
5300 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5302 case CCL_STAT_SUSPEND_BY_DST
:
5303 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5306 case CCL_STAT_INVALID_CMD
:
5307 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5310 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5314 coding
->produced_char
+= produced_chars
;
5315 coding
->produced
= dst
- coding
->destination
;
5321 /*** 10, 11. no-conversion handlers ***/
5323 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5326 decode_coding_raw_text (struct coding_system
*coding
)
5329 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5331 coding
->chars_at_source
= 1;
5332 coding
->consumed_char
= coding
->src_chars
;
5333 coding
->consumed
= coding
->src_bytes
;
5334 if (eol_crlf
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5336 coding
->consumed_char
--;
5338 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5341 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5345 encode_coding_raw_text (struct coding_system
*coding
)
5347 int multibytep
= coding
->dst_multibyte
;
5348 int *charbuf
= coding
->charbuf
;
5349 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5350 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5351 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5352 int produced_chars
= 0;
5357 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5359 if (coding
->src_multibyte
)
5360 while (charbuf
< charbuf_end
)
5362 ASSURE_DESTINATION (safe_room
);
5364 if (ASCII_CHAR_P (c
))
5365 EMIT_ONE_ASCII_BYTE (c
);
5366 else if (CHAR_BYTE8_P (c
))
5368 c
= CHAR_TO_BYTE8 (c
);
5373 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5375 CHAR_STRING_ADVANCE (c
, p1
);
5378 EMIT_ONE_BYTE (*p0
);
5384 while (charbuf
< charbuf_end
)
5386 ASSURE_DESTINATION (safe_room
);
5393 if (coding
->src_multibyte
)
5395 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5397 while (charbuf
< charbuf_end
)
5399 ASSURE_DESTINATION (safe_room
);
5401 if (ASCII_CHAR_P (c
))
5403 else if (CHAR_BYTE8_P (c
))
5404 *dst
++ = CHAR_TO_BYTE8 (c
);
5406 CHAR_STRING_ADVANCE (c
, dst
);
5411 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5412 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5413 *dst
++ = *charbuf
++;
5415 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5417 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5418 coding
->produced_char
+= produced_chars
;
5419 coding
->produced
= dst
- coding
->destination
;
5423 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5424 Check if a text is encoded in a charset-based coding system. If it
5425 is, return 1, else return 0. */
5428 detect_coding_charset (struct coding_system
*coding
,
5429 struct coding_detection_info
*detect_info
)
5431 const unsigned char *src
= coding
->source
, *src_base
;
5432 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5433 int multibytep
= coding
->src_multibyte
;
5434 int consumed_chars
= 0;
5435 Lisp_Object attrs
, valids
, name
;
5437 int head_ascii
= coding
->head_ascii
;
5438 int check_latin_extra
= 0;
5440 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5442 coding
= &coding_categories
[coding_category_charset
];
5443 attrs
= CODING_ID_ATTRS (coding
->id
);
5444 valids
= AREF (attrs
, coding_attr_charset_valids
);
5445 name
= CODING_ID_NAME (coding
->id
);
5446 if (strncmp ((char *) SDATA (SYMBOL_NAME (name
)),
5447 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5448 || strncmp ((char *) SDATA (SYMBOL_NAME (name
)),
5449 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5450 check_latin_extra
= 1;
5452 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5459 struct charset
*charset
;
5466 val
= AREF (valids
, c
);
5472 && check_latin_extra
5473 && (!VECTORP (Vlatin_extra_code_table
)
5474 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5476 found
= CATEGORY_MASK_CHARSET
;
5480 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5481 dim
= CHARSET_DIMENSION (charset
);
5482 for (idx
= 1; idx
< dim
; idx
++)
5487 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5488 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5497 for (; CONSP (val
); val
= XCDR (val
))
5499 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5500 dim
= CHARSET_DIMENSION (charset
);
5506 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5507 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5522 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5526 detect_info
->found
|= found
;
5531 decode_coding_charset (struct coding_system
*coding
)
5533 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5534 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5535 const unsigned char *src_base
;
5536 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5537 /* We may produce one charset annotation in one loop and one more at
5540 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5541 int consumed_chars
= 0, consumed_chars_base
;
5542 int multibytep
= coding
->src_multibyte
;
5543 Lisp_Object attrs
, charset_list
, valids
;
5544 int char_offset
= coding
->produced_char
;
5545 int last_offset
= char_offset
;
5546 int last_id
= charset_ascii
;
5548 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5549 int byte_after_cr
= -1;
5551 CODING_GET_INFO (coding
, attrs
, charset_list
);
5552 valids
= AREF (attrs
, coding_attr_charset_valids
);
5558 struct charset
*charset
;
5564 consumed_chars_base
= consumed_chars
;
5566 if (charbuf
>= charbuf_end
)
5568 if (byte_after_cr
>= 0)
5573 if (byte_after_cr
>= 0)
5581 if (eol_crlf
&& c
== '\r')
5582 ONE_MORE_BYTE (byte_after_cr
);
5588 val
= AREF (valids
, c
);
5589 if (! INTEGERP (val
) && ! CONSP (val
))
5593 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5594 dim
= CHARSET_DIMENSION (charset
);
5598 code
= (code
<< 8) | c
;
5601 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5606 /* VAL is a list of charset IDs. It is assured that the
5607 list is sorted by charset dimensions (smaller one
5611 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5612 dim
= CHARSET_DIMENSION (charset
);
5616 code
= (code
<< 8) | c
;
5619 CODING_DECODE_CHAR (coding
, src
, src_base
,
5620 src_end
, charset
, code
, c
);
5628 if (charset
->id
!= charset_ascii
5629 && last_id
!= charset
->id
)
5631 if (last_id
!= charset_ascii
)
5632 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5633 last_id
= charset
->id
;
5634 last_offset
= char_offset
;
5643 consumed_chars
= consumed_chars_base
;
5645 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5651 if (last_id
!= charset_ascii
)
5652 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5653 coding
->consumed_char
+= consumed_chars_base
;
5654 coding
->consumed
= src_base
- coding
->source
;
5655 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5659 encode_coding_charset (struct coding_system
*coding
)
5661 int multibytep
= coding
->dst_multibyte
;
5662 int *charbuf
= coding
->charbuf
;
5663 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5664 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5665 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5666 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5667 int produced_chars
= 0;
5668 Lisp_Object attrs
, charset_list
;
5669 int ascii_compatible
;
5672 CODING_GET_INFO (coding
, attrs
, charset_list
);
5673 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5675 while (charbuf
< charbuf_end
)
5677 struct charset
*charset
;
5680 ASSURE_DESTINATION (safe_room
);
5682 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5683 EMIT_ONE_ASCII_BYTE (c
);
5684 else if (CHAR_BYTE8_P (c
))
5686 c
= CHAR_TO_BYTE8 (c
);
5691 charset
= char_charset (c
, charset_list
, &code
);
5694 if (CHARSET_DIMENSION (charset
) == 1)
5695 EMIT_ONE_BYTE (code
);
5696 else if (CHARSET_DIMENSION (charset
) == 2)
5697 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5698 else if (CHARSET_DIMENSION (charset
) == 3)
5699 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5701 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5702 (code
>> 8) & 0xFF, code
& 0xFF);
5706 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5707 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5709 c
= coding
->default_char
;
5715 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5716 coding
->produced_char
+= produced_chars
;
5717 coding
->produced
= dst
- coding
->destination
;
5722 /*** 7. C library functions ***/
5724 /* Setup coding context CODING from information about CODING_SYSTEM.
5725 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5726 CODING_SYSTEM is invalid, signal an error. */
5729 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5732 Lisp_Object eol_type
;
5733 Lisp_Object coding_type
;
5736 if (NILP (coding_system
))
5737 coding_system
= Qundecided
;
5739 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5741 attrs
= CODING_ID_ATTRS (coding
->id
);
5742 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5745 coding
->head_ascii
= -1;
5746 if (VECTORP (eol_type
))
5747 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5748 | CODING_REQUIRE_DETECTION_MASK
);
5749 else if (! EQ (eol_type
, Qunix
))
5750 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5751 | CODING_REQUIRE_ENCODING_MASK
);
5753 coding
->common_flags
= 0;
5754 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5755 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5756 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5757 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5758 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5759 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5761 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5762 coding
->max_charset_id
= SCHARS (val
) - 1;
5763 coding
->safe_charsets
= SDATA (val
);
5764 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5765 coding
->carryover_bytes
= 0;
5767 coding_type
= CODING_ATTR_TYPE (attrs
);
5768 if (EQ (coding_type
, Qundecided
))
5770 coding
->detector
= NULL
;
5771 coding
->decoder
= decode_coding_raw_text
;
5772 coding
->encoder
= encode_coding_raw_text
;
5773 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5775 else if (EQ (coding_type
, Qiso_2022
))
5778 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5780 /* Invoke graphic register 0 to plane 0. */
5781 CODING_ISO_INVOCATION (coding
, 0) = 0;
5782 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5783 CODING_ISO_INVOCATION (coding
, 1)
5784 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5785 /* Setup the initial status of designation. */
5786 for (i
= 0; i
< 4; i
++)
5787 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5788 /* Not single shifting initially. */
5789 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5790 /* Beginning of buffer should also be regarded as bol. */
5791 CODING_ISO_BOL (coding
) = 1;
5792 coding
->detector
= detect_coding_iso_2022
;
5793 coding
->decoder
= decode_coding_iso_2022
;
5794 coding
->encoder
= encode_coding_iso_2022
;
5795 if (flags
& CODING_ISO_FLAG_SAFE
)
5796 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5797 coding
->common_flags
5798 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5799 | CODING_REQUIRE_FLUSHING_MASK
);
5800 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5801 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5802 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5803 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5804 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5806 setup_iso_safe_charsets (attrs
);
5807 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5808 coding
->max_charset_id
= SCHARS (val
) - 1;
5809 coding
->safe_charsets
= SDATA (val
);
5811 CODING_ISO_FLAGS (coding
) = flags
;
5812 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5813 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5814 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5815 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5817 else if (EQ (coding_type
, Qcharset
))
5819 coding
->detector
= detect_coding_charset
;
5820 coding
->decoder
= decode_coding_charset
;
5821 coding
->encoder
= encode_coding_charset
;
5822 coding
->common_flags
5823 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5825 else if (EQ (coding_type
, Qutf_8
))
5827 val
= AREF (attrs
, coding_attr_utf_bom
);
5828 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5829 : EQ (val
, Qt
) ? utf_with_bom
5831 coding
->detector
= detect_coding_utf_8
;
5832 coding
->decoder
= decode_coding_utf_8
;
5833 coding
->encoder
= encode_coding_utf_8
;
5834 coding
->common_flags
5835 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5836 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5837 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5839 else if (EQ (coding_type
, Qutf_16
))
5841 val
= AREF (attrs
, coding_attr_utf_bom
);
5842 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5843 : EQ (val
, Qt
) ? utf_with_bom
5845 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5846 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5847 : utf_16_little_endian
);
5848 CODING_UTF_16_SURROGATE (coding
) = 0;
5849 coding
->detector
= detect_coding_utf_16
;
5850 coding
->decoder
= decode_coding_utf_16
;
5851 coding
->encoder
= encode_coding_utf_16
;
5852 coding
->common_flags
5853 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5854 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5855 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5857 else if (EQ (coding_type
, Qccl
))
5859 coding
->detector
= detect_coding_ccl
;
5860 coding
->decoder
= decode_coding_ccl
;
5861 coding
->encoder
= encode_coding_ccl
;
5862 coding
->common_flags
5863 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5864 | CODING_REQUIRE_FLUSHING_MASK
);
5866 else if (EQ (coding_type
, Qemacs_mule
))
5868 coding
->detector
= detect_coding_emacs_mule
;
5869 coding
->decoder
= decode_coding_emacs_mule
;
5870 coding
->encoder
= encode_coding_emacs_mule
;
5871 coding
->common_flags
5872 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5873 coding
->spec
.emacs_mule
.full_support
= 1;
5874 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5875 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5877 Lisp_Object tail
, safe_charsets
;
5878 int max_charset_id
= 0;
5880 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5882 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5883 max_charset_id
= XFASTINT (XCAR (tail
));
5884 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5885 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5886 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5888 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5889 coding
->max_charset_id
= max_charset_id
;
5890 coding
->safe_charsets
= SDATA (safe_charsets
);
5891 coding
->spec
.emacs_mule
.full_support
= 1;
5893 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5894 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5896 else if (EQ (coding_type
, Qshift_jis
))
5898 coding
->detector
= detect_coding_sjis
;
5899 coding
->decoder
= decode_coding_sjis
;
5900 coding
->encoder
= encode_coding_sjis
;
5901 coding
->common_flags
5902 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5904 else if (EQ (coding_type
, Qbig5
))
5906 coding
->detector
= detect_coding_big5
;
5907 coding
->decoder
= decode_coding_big5
;
5908 coding
->encoder
= encode_coding_big5
;
5909 coding
->common_flags
5910 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5912 else /* EQ (coding_type, Qraw_text) */
5914 coding
->detector
= NULL
;
5915 coding
->decoder
= decode_coding_raw_text
;
5916 coding
->encoder
= encode_coding_raw_text
;
5917 if (! EQ (eol_type
, Qunix
))
5919 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5920 if (! VECTORP (eol_type
))
5921 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5929 /* Return a list of charsets supported by CODING. */
5932 coding_charset_list (struct coding_system
*coding
)
5934 Lisp_Object attrs
, charset_list
;
5936 CODING_GET_INFO (coding
, attrs
, charset_list
);
5937 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5939 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5941 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5942 charset_list
= Viso_2022_charset_list
;
5944 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5946 charset_list
= Vemacs_mule_charset_list
;
5948 return charset_list
;
5952 /* Return a list of charsets supported by CODING-SYSTEM. */
5955 coding_system_charset_list (Lisp_Object coding_system
)
5958 Lisp_Object attrs
, charset_list
;
5960 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5961 attrs
= CODING_ID_ATTRS (id
);
5963 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5965 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5967 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5968 charset_list
= Viso_2022_charset_list
;
5970 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5972 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5974 charset_list
= Vemacs_mule_charset_list
;
5978 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5980 return charset_list
;
5984 /* Return raw-text or one of its subsidiaries that has the same
5985 eol_type as CODING-SYSTEM. */
5988 raw_text_coding_system (Lisp_Object coding_system
)
5990 Lisp_Object spec
, attrs
;
5991 Lisp_Object eol_type
, raw_text_eol_type
;
5993 if (NILP (coding_system
))
5995 spec
= CODING_SYSTEM_SPEC (coding_system
);
5996 attrs
= AREF (spec
, 0);
5998 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5999 return coding_system
;
6001 eol_type
= AREF (spec
, 2);
6002 if (VECTORP (eol_type
))
6004 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
6005 raw_text_eol_type
= AREF (spec
, 2);
6006 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
6007 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
6008 : AREF (raw_text_eol_type
, 2));
6012 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
6013 the subsidiary that has the same eol-spec as PARENT (if it is not
6014 nil and specifies end-of-line format) or the system's setting
6015 (system_eol_type). */
6018 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
6020 Lisp_Object spec
, eol_type
;
6022 if (NILP (coding_system
))
6023 coding_system
= Qraw_text
;
6024 spec
= CODING_SYSTEM_SPEC (coding_system
);
6025 eol_type
= AREF (spec
, 2);
6026 if (VECTORP (eol_type
))
6028 Lisp_Object parent_eol_type
;
6030 if (! NILP (parent
))
6032 Lisp_Object parent_spec
;
6034 parent_spec
= CODING_SYSTEM_SPEC (parent
);
6035 parent_eol_type
= AREF (parent_spec
, 2);
6036 if (VECTORP (parent_eol_type
))
6037 parent_eol_type
= system_eol_type
;
6040 parent_eol_type
= system_eol_type
;
6041 if (EQ (parent_eol_type
, Qunix
))
6042 coding_system
= AREF (eol_type
, 0);
6043 else if (EQ (parent_eol_type
, Qdos
))
6044 coding_system
= AREF (eol_type
, 1);
6045 else if (EQ (parent_eol_type
, Qmac
))
6046 coding_system
= AREF (eol_type
, 2);
6048 return coding_system
;
6052 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
6053 decided for writing to a process. If not, complement them, and
6054 return a new coding system. */
6057 complement_process_encoding_system (Lisp_Object coding_system
)
6059 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
6060 Lisp_Object spec
, attrs
;
6063 for (i
= 0; i
< 3; i
++)
6066 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
6068 coding_system
= preferred_coding_system ();
6069 spec
= CODING_SYSTEM_SPEC (coding_system
);
6072 attrs
= AREF (spec
, 0);
6073 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
6074 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
6075 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
6076 eol_base
= coding_system
;
6077 if (! NILP (coding_base
) && ! NILP (eol_base
))
6082 /* The original CODING_SYSTEM didn't specify text-conversion or
6083 eol-conversion. Be sure that we return a fully complemented
6085 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
6086 return coding_system
;
6090 /* Emacs has a mechanism to automatically detect a coding system if it
6091 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6092 it's impossible to distinguish some coding systems accurately
6093 because they use the same range of codes. So, at first, coding
6094 systems are categorized into 7, those are:
6096 o coding-category-emacs-mule
6098 The category for a coding system which has the same code range
6099 as Emacs' internal format. Assigned the coding-system (Lisp
6100 symbol) `emacs-mule' by default.
6102 o coding-category-sjis
6104 The category for a coding system which has the same code range
6105 as SJIS. Assigned the coding-system (Lisp
6106 symbol) `japanese-shift-jis' by default.
6108 o coding-category-iso-7
6110 The category for a coding system which has the same code range
6111 as ISO2022 of 7-bit environment. This doesn't use any locking
6112 shift and single shift functions. This can encode/decode all
6113 charsets. Assigned the coding-system (Lisp symbol)
6114 `iso-2022-7bit' by default.
6116 o coding-category-iso-7-tight
6118 Same as coding-category-iso-7 except that this can
6119 encode/decode only the specified charsets.
6121 o coding-category-iso-8-1
6123 The category for a coding system which has the same code range
6124 as ISO2022 of 8-bit environment and graphic plane 1 used only
6125 for DIMENSION1 charset. This doesn't use any locking shift
6126 and single shift functions. Assigned the coding-system (Lisp
6127 symbol) `iso-latin-1' by default.
6129 o coding-category-iso-8-2
6131 The category for a coding system which has the same code range
6132 as ISO2022 of 8-bit environment and graphic plane 1 used only
6133 for DIMENSION2 charset. This doesn't use any locking shift
6134 and single shift functions. Assigned the coding-system (Lisp
6135 symbol) `japanese-iso-8bit' by default.
6137 o coding-category-iso-7-else
6139 The category for a coding system which has the same code range
6140 as ISO2022 of 7-bit environment but uses locking shift or
6141 single shift functions. Assigned the coding-system (Lisp
6142 symbol) `iso-2022-7bit-lock' by default.
6144 o coding-category-iso-8-else
6146 The category for a coding system which has the same code range
6147 as ISO2022 of 8-bit environment but uses locking shift or
6148 single shift functions. Assigned the coding-system (Lisp
6149 symbol) `iso-2022-8bit-ss2' by default.
6151 o coding-category-big5
6153 The category for a coding system which has the same code range
6154 as BIG5. Assigned the coding-system (Lisp symbol)
6155 `cn-big5' by default.
6157 o coding-category-utf-8
6159 The category for a coding system which has the same code range
6160 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6161 symbol) `utf-8' by default.
6163 o coding-category-utf-16-be
6165 The category for a coding system in which a text has an
6166 Unicode signature (cf. Unicode Standard) in the order of BIG
6167 endian at the head. Assigned the coding-system (Lisp symbol)
6168 `utf-16-be' by default.
6170 o coding-category-utf-16-le
6172 The category for a coding system in which a text has an
6173 Unicode signature (cf. Unicode Standard) in the order of
6174 LITTLE endian at the head. Assigned the coding-system (Lisp
6175 symbol) `utf-16-le' by default.
6177 o coding-category-ccl
6179 The category for a coding system of which encoder/decoder is
6180 written in CCL programs. The default value is nil, i.e., no
6181 coding system is assigned.
6183 o coding-category-binary
6185 The category for a coding system not categorized in any of the
6186 above. Assigned the coding-system (Lisp symbol)
6187 `no-conversion' by default.
6189 Each of them is a Lisp symbol and the value is an actual
6190 `coding-system's (this is also a Lisp symbol) assigned by a user.
6191 What Emacs does actually is to detect a category of coding system.
6192 Then, it uses a `coding-system' assigned to it. If Emacs can't
6193 decide only one possible category, it selects a category of the
6194 highest priority. Priorities of categories are also specified by a
6195 user in a Lisp variable `coding-category-list'.
6199 #define EOL_SEEN_NONE 0
6200 #define EOL_SEEN_LF 1
6201 #define EOL_SEEN_CR 2
6202 #define EOL_SEEN_CRLF 4
6204 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6205 SOURCE is encoded. If CATEGORY is one of
6206 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6207 two-byte, else they are encoded by one-byte.
6209 Return one of EOL_SEEN_XXX. */
6211 #define MAX_EOL_CHECK_COUNT 3
6214 detect_eol (const unsigned char *source
, EMACS_INT src_bytes
,
6215 enum coding_category category
)
6217 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6220 int eol_seen
= EOL_SEEN_NONE
;
6222 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6226 msb
= category
== (coding_category_utf_16_le
6227 | coding_category_utf_16_le_nosig
);
6230 while (src
+ 1 < src_end
)
6233 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6238 this_eol
= EOL_SEEN_LF
;
6239 else if (src
+ 3 >= src_end
6240 || src
[msb
+ 2] != 0
6241 || src
[lsb
+ 2] != '\n')
6242 this_eol
= EOL_SEEN_CR
;
6245 this_eol
= EOL_SEEN_CRLF
;
6249 if (eol_seen
== EOL_SEEN_NONE
)
6250 /* This is the first end-of-line. */
6251 eol_seen
= this_eol
;
6252 else if (eol_seen
!= this_eol
)
6254 /* The found type is different from what found before.
6255 Allow for stray ^M characters in DOS EOL files. */
6256 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6257 || (eol_seen
== EOL_SEEN_CRLF
6258 && this_eol
== EOL_SEEN_CR
))
6259 eol_seen
= EOL_SEEN_CRLF
;
6262 eol_seen
= EOL_SEEN_LF
;
6266 if (++total
== MAX_EOL_CHECK_COUNT
)
6273 while (src
< src_end
)
6276 if (c
== '\n' || c
== '\r')
6281 this_eol
= EOL_SEEN_LF
;
6282 else if (src
>= src_end
|| *src
!= '\n')
6283 this_eol
= EOL_SEEN_CR
;
6285 this_eol
= EOL_SEEN_CRLF
, src
++;
6287 if (eol_seen
== EOL_SEEN_NONE
)
6288 /* This is the first end-of-line. */
6289 eol_seen
= this_eol
;
6290 else if (eol_seen
!= this_eol
)
6292 /* The found type is different from what found before.
6293 Allow for stray ^M characters in DOS EOL files. */
6294 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6295 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6296 eol_seen
= EOL_SEEN_CRLF
;
6299 eol_seen
= EOL_SEEN_LF
;
6303 if (++total
== MAX_EOL_CHECK_COUNT
)
6312 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6314 Lisp_Object eol_type
;
6316 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6317 if (eol_seen
& EOL_SEEN_LF
)
6319 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6322 else if (eol_seen
& EOL_SEEN_CRLF
)
6324 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6327 else if (eol_seen
& EOL_SEEN_CR
)
6329 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6335 /* Detect how a text specified in CODING is encoded. If a coding
6336 system is detected, update fields of CODING by the detected coding
6340 detect_coding (struct coding_system
*coding
)
6342 const unsigned char *src
, *src_end
;
6343 int saved_mode
= coding
->mode
;
6345 coding
->consumed
= coding
->consumed_char
= 0;
6346 coding
->produced
= coding
->produced_char
= 0;
6347 coding_set_source (coding
);
6349 src_end
= coding
->source
+ coding
->src_bytes
;
6350 coding
->head_ascii
= 0;
6352 /* If we have not yet decided the text encoding type, detect it
6354 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6357 struct coding_detection_info detect_info
;
6358 int null_byte_found
= 0, eight_bit_found
= 0;
6360 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6361 for (src
= coding
->source
; src
< src_end
; src
++)
6366 eight_bit_found
= 1;
6367 if (null_byte_found
)
6372 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6373 && ! inhibit_iso_escape_detection
6374 && ! detect_info
.checked
)
6376 if (detect_coding_iso_2022 (coding
, &detect_info
))
6378 /* We have scanned the whole data. */
6379 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6381 /* We didn't find an 8-bit code. We may
6382 have found a null-byte, but it's very
6383 rare that a binary file conforms to
6386 coding
->head_ascii
= src
- coding
->source
;
6388 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6392 else if (! c
&& !inhibit_null_byte_detection
)
6394 null_byte_found
= 1;
6395 if (eight_bit_found
)
6398 if (! eight_bit_found
)
6399 coding
->head_ascii
++;
6401 else if (! eight_bit_found
)
6402 coding
->head_ascii
++;
6405 if (null_byte_found
|| eight_bit_found
6406 || coding
->head_ascii
< coding
->src_bytes
6407 || detect_info
.found
)
6409 enum coding_category category
;
6410 struct coding_system
*this;
6412 if (coding
->head_ascii
== coding
->src_bytes
)
6413 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6414 for (i
= 0; i
< coding_category_raw_text
; i
++)
6416 category
= coding_priorities
[i
];
6417 this = coding_categories
+ category
;
6418 if (detect_info
.found
& (1 << category
))
6423 if (null_byte_found
)
6425 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6426 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6428 for (i
= 0; i
< coding_category_raw_text
; i
++)
6430 category
= coding_priorities
[i
];
6431 this = coding_categories
+ category
;
6434 /* No coding system of this category is defined. */
6435 detect_info
.rejected
|= (1 << category
);
6437 else if (category
>= coding_category_raw_text
)
6439 else if (detect_info
.checked
& (1 << category
))
6441 if (detect_info
.found
& (1 << category
))
6444 else if ((*(this->detector
)) (coding
, &detect_info
)
6445 && detect_info
.found
& (1 << category
))
6447 if (category
== coding_category_utf_16_auto
)
6449 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6450 category
= coding_category_utf_16_le
;
6452 category
= coding_category_utf_16_be
;
6459 if (i
< coding_category_raw_text
)
6460 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6461 else if (null_byte_found
)
6462 setup_coding_system (Qno_conversion
, coding
);
6463 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6464 == CATEGORY_MASK_ANY
)
6465 setup_coding_system (Qraw_text
, coding
);
6466 else if (detect_info
.rejected
)
6467 for (i
= 0; i
< coding_category_raw_text
; i
++)
6468 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6470 this = coding_categories
+ coding_priorities
[i
];
6471 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6476 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6477 == coding_category_utf_8_auto
)
6479 Lisp_Object coding_systems
;
6480 struct coding_detection_info detect_info
;
6483 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6484 detect_info
.found
= detect_info
.rejected
= 0;
6485 coding
->head_ascii
= 0;
6486 if (CONSP (coding_systems
)
6487 && detect_coding_utf_8 (coding
, &detect_info
))
6489 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6490 setup_coding_system (XCAR (coding_systems
), coding
);
6492 setup_coding_system (XCDR (coding_systems
), coding
);
6495 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6496 == coding_category_utf_16_auto
)
6498 Lisp_Object coding_systems
;
6499 struct coding_detection_info detect_info
;
6502 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6503 detect_info
.found
= detect_info
.rejected
= 0;
6504 coding
->head_ascii
= 0;
6505 if (CONSP (coding_systems
)
6506 && detect_coding_utf_16 (coding
, &detect_info
))
6508 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6509 setup_coding_system (XCAR (coding_systems
), coding
);
6510 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6511 setup_coding_system (XCDR (coding_systems
), coding
);
6514 coding
->mode
= saved_mode
;
6519 decode_eol (struct coding_system
*coding
)
6521 Lisp_Object eol_type
;
6522 unsigned char *p
, *pbeg
, *pend
;
6524 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6525 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6528 if (NILP (coding
->dst_object
))
6529 pbeg
= coding
->destination
;
6531 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6532 pend
= pbeg
+ coding
->produced
;
6534 if (VECTORP (eol_type
))
6536 int eol_seen
= EOL_SEEN_NONE
;
6538 for (p
= pbeg
; p
< pend
; p
++)
6541 eol_seen
|= EOL_SEEN_LF
;
6542 else if (*p
== '\r')
6544 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6546 eol_seen
|= EOL_SEEN_CRLF
;
6550 eol_seen
|= EOL_SEEN_CR
;
6553 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6554 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6555 && (eol_seen
& EOL_SEEN_CR
) != 0
6556 && (eol_seen
& EOL_SEEN_LF
) == 0)
6557 eol_seen
= EOL_SEEN_CRLF
;
6558 else if (eol_seen
!= EOL_SEEN_NONE
6559 && eol_seen
!= EOL_SEEN_LF
6560 && eol_seen
!= EOL_SEEN_CRLF
6561 && eol_seen
!= EOL_SEEN_CR
)
6562 eol_seen
= EOL_SEEN_LF
;
6563 if (eol_seen
!= EOL_SEEN_NONE
)
6564 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6567 if (EQ (eol_type
, Qmac
))
6569 for (p
= pbeg
; p
< pend
; p
++)
6573 else if (EQ (eol_type
, Qdos
))
6577 if (NILP (coding
->dst_object
))
6579 /* Start deleting '\r' from the tail to minimize the memory
6581 for (p
= pend
- 2; p
>= pbeg
; p
--)
6584 memmove (p
, p
+ 1, pend
-- - p
- 1);
6590 int pos_byte
= coding
->dst_pos_byte
;
6591 int pos
= coding
->dst_pos
;
6592 int pos_end
= pos
+ coding
->produced_char
- 1;
6594 while (pos
< pos_end
)
6596 p
= BYTE_POS_ADDR (pos_byte
);
6597 if (*p
== '\r' && p
[1] == '\n')
6599 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6604 if (coding
->dst_multibyte
)
6605 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6610 coding
->produced
-= n
;
6611 coding
->produced_char
-= n
;
6616 /* Return a translation table (or list of them) from coding system
6617 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6618 decoding (ENCODEP is zero). */
6621 get_translation_table (Lisp_Object attrs
, int encodep
, int *max_lookup
)
6623 Lisp_Object standard
, translation_table
;
6626 if (NILP (Venable_character_translation
))
6633 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6634 standard
= Vstandard_translation_table_for_encode
;
6636 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6637 standard
= Vstandard_translation_table_for_decode
;
6638 if (NILP (translation_table
))
6639 translation_table
= standard
;
6642 if (SYMBOLP (translation_table
))
6643 translation_table
= Fget (translation_table
, Qtranslation_table
);
6644 else if (CONSP (translation_table
))
6646 translation_table
= Fcopy_sequence (translation_table
);
6647 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6648 if (SYMBOLP (XCAR (val
)))
6649 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6651 if (CHAR_TABLE_P (standard
))
6653 if (CONSP (translation_table
))
6654 translation_table
= nconc2 (translation_table
,
6655 Fcons (standard
, Qnil
));
6657 translation_table
= Fcons (translation_table
,
6658 Fcons (standard
, Qnil
));
6665 if (CHAR_TABLE_P (translation_table
)
6666 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6668 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6669 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6670 *max_lookup
= XFASTINT (val
);
6672 else if (CONSP (translation_table
))
6674 Lisp_Object tail
, val
;
6676 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6677 if (CHAR_TABLE_P (XCAR (tail
))
6678 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6680 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6681 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6682 *max_lookup
= XFASTINT (val
);
6686 return translation_table
;
6689 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6692 if (CHAR_TABLE_P (table)) \
6694 trans = CHAR_TABLE_REF (table, c); \
6695 if (CHARACTERP (trans)) \
6696 c = XFASTINT (trans), trans = Qnil; \
6698 else if (CONSP (table)) \
6702 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6703 if (CHAR_TABLE_P (XCAR (tail))) \
6705 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6706 if (CHARACTERP (trans)) \
6707 c = XFASTINT (trans), trans = Qnil; \
6708 else if (! NILP (trans)) \
6715 /* Return a translation of character(s) at BUF according to TRANS.
6716 TRANS is TO-CHAR or ((FROM . TO) ...) where
6717 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6718 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6719 translation is found, and Qnil if not found..
6720 If BUF is too short to lookup characters in FROM, return Qt. */
6723 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6726 if (INTEGERP (trans
))
6728 for (; CONSP (trans
); trans
= XCDR (trans
))
6730 Lisp_Object val
= XCAR (trans
);
6731 Lisp_Object from
= XCAR (val
);
6732 int len
= ASIZE (from
);
6735 for (i
= 0; i
< len
; i
++)
6737 if (buf
+ i
== buf_end
)
6739 if (XINT (AREF (from
, i
)) != buf
[i
])
6750 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6753 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6754 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6756 EMACS_INT produced_chars
= 0;
6759 if (! coding
->chars_at_source
)
6761 /* Source characters are in coding->charbuf. */
6762 int *buf
= coding
->charbuf
;
6763 int *buf_end
= buf
+ coding
->charbuf_used
;
6765 if (EQ (coding
->src_object
, coding
->dst_object
))
6767 coding_set_source (coding
);
6768 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6771 while (buf
< buf_end
)
6777 int from_nchars
= 1, to_nchars
= 1;
6778 Lisp_Object trans
= Qnil
;
6780 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6783 trans
= get_translation (trans
, buf
, buf_end
);
6784 if (INTEGERP (trans
))
6786 else if (CONSP (trans
))
6788 from_nchars
= ASIZE (XCAR (trans
));
6789 trans
= XCDR (trans
);
6790 if (INTEGERP (trans
))
6794 to_nchars
= ASIZE (trans
);
6795 c
= XINT (AREF (trans
, 0));
6798 else if (EQ (trans
, Qt
) && ! last_block
)
6802 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6804 dst
= alloc_destination (coding
,
6806 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6808 if (EQ (coding
->src_object
, coding
->dst_object
))
6810 coding_set_source (coding
);
6811 dst_end
= (((unsigned char *) coding
->source
)
6812 + coding
->consumed
);
6815 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6818 for (i
= 0; i
< to_nchars
; i
++)
6821 c
= XINT (AREF (trans
, i
));
6822 if (coding
->dst_multibyte
6823 || ! CHAR_BYTE8_P (c
))
6824 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6826 *dst
++ = CHAR_TO_BYTE8 (c
);
6828 produced_chars
+= to_nchars
;
6832 /* This is an annotation datum. (-C) is the length. */
6835 carryover
= buf_end
- buf
;
6839 /* Source characters are at coding->source. */
6840 const unsigned char *src
= coding
->source
;
6841 const unsigned char *src_end
= src
+ coding
->consumed
;
6843 if (EQ (coding
->dst_object
, coding
->src_object
))
6844 dst_end
= (unsigned char *) src
;
6845 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6847 if (coding
->src_multibyte
)
6850 EMACS_INT consumed_chars
= 0;
6854 const unsigned char *src_base
= src
;
6860 if (EQ (coding
->src_object
, coding
->dst_object
))
6861 dst_end
= (unsigned char *) src
;
6864 EMACS_INT offset
= src
- coding
->source
;
6866 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6868 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6869 coding_set_source (coding
);
6870 src
= coding
->source
+ offset
;
6871 src_end
= coding
->source
+ coding
->src_bytes
;
6872 if (EQ (coding
->src_object
, coding
->dst_object
))
6873 dst_end
= (unsigned char *) src
;
6883 while (src
< src_end
)
6888 if (dst
>= dst_end
- 1)
6890 if (EQ (coding
->src_object
, coding
->dst_object
))
6891 dst_end
= (unsigned char *) src
;
6892 if (dst
>= dst_end
- 1)
6894 EMACS_INT offset
= src
- coding
->source
;
6895 EMACS_INT more_bytes
;
6897 if (EQ (coding
->src_object
, coding
->dst_object
))
6898 more_bytes
= ((src_end
- src
) / 2) + 2;
6900 more_bytes
= src_end
- src
+ 2;
6901 dst
= alloc_destination (coding
, more_bytes
, dst
);
6902 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6903 coding_set_source (coding
);
6904 src
= coding
->source
+ offset
;
6905 src_end
= coding
->source
+ coding
->src_bytes
;
6906 if (EQ (coding
->src_object
, coding
->dst_object
))
6907 dst_end
= (unsigned char *) src
;
6915 if (!EQ (coding
->src_object
, coding
->dst_object
))
6917 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6921 EMACS_INT offset
= src
- coding
->source
;
6923 dst
= alloc_destination (coding
, require
, dst
);
6924 coding_set_source (coding
);
6925 src
= coding
->source
+ offset
;
6926 src_end
= coding
->source
+ coding
->src_bytes
;
6929 produced_chars
= coding
->consumed_char
;
6930 while (src
< src_end
)
6935 produced
= dst
- (coding
->destination
+ coding
->produced
);
6936 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6937 insert_from_gap (produced_chars
, produced
);
6938 coding
->produced
+= produced
;
6939 coding
->produced_char
+= produced_chars
;
6943 /* Compose text in CODING->object according to the annotation data at
6944 CHARBUF. CHARBUF is an array:
6945 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6949 produce_composition (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6953 enum composition_method method
;
6954 Lisp_Object components
;
6956 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6957 to
= pos
+ charbuf
[2];
6958 method
= (enum composition_method
) (charbuf
[4]);
6960 if (method
== COMPOSITION_RELATIVE
)
6964 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6967 if (method
== COMPOSITION_WITH_RULE
)
6968 len
= charbuf
[2] * 3 - 2;
6969 charbuf
+= MAX_ANNOTATION_LENGTH
;
6970 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6971 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6973 if (charbuf
[i
] >= 0)
6974 args
[j
] = make_number (charbuf
[i
]);
6978 args
[j
] = make_number (charbuf
[i
] % 0x100);
6981 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6983 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6987 /* Put `charset' property on text in CODING->object according to
6988 the annotation data at CHARBUF. CHARBUF is an array:
6989 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6993 produce_charset (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6995 EMACS_INT from
= pos
- charbuf
[2];
6996 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6998 Fput_text_property (make_number (from
), make_number (pos
),
6999 Qcharset
, CHARSET_NAME (charset
),
7000 coding
->dst_object
);
7004 #define CHARBUF_SIZE 0x4000
7006 #define ALLOC_CONVERSION_WORK_AREA(coding) \
7008 int size = CHARBUF_SIZE; \
7010 coding->charbuf = NULL; \
7011 while (size > 1024) \
7013 coding->charbuf = (int *) alloca (sizeof (int) * size); \
7014 if (coding->charbuf) \
7018 if (! coding->charbuf) \
7020 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
7021 return coding->result; \
7023 coding->charbuf_size = size; \
7028 produce_annotation (struct coding_system
*coding
, EMACS_INT pos
)
7030 int *charbuf
= coding
->charbuf
;
7031 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
7033 if (NILP (coding
->dst_object
))
7036 while (charbuf
< charbuf_end
)
7042 int len
= -*charbuf
;
7047 case CODING_ANNOTATE_COMPOSITION_MASK
:
7048 produce_composition (coding
, charbuf
, pos
);
7050 case CODING_ANNOTATE_CHARSET_MASK
:
7051 produce_charset (coding
, charbuf
, pos
);
7059 /* Decode the data at CODING->src_object into CODING->dst_object.
7060 CODING->src_object is a buffer, a string, or nil.
7061 CODING->dst_object is a buffer.
7063 If CODING->src_object is a buffer, it must be the current buffer.
7064 In this case, if CODING->src_pos is positive, it is a position of
7065 the source text in the buffer, otherwise, the source text is in the
7066 gap area of the buffer, and CODING->src_pos specifies the offset of
7067 the text from GPT (which must be the same as PT). If this is the
7068 same buffer as CODING->dst_object, CODING->src_pos must be
7071 If CODING->src_object is a string, CODING->src_pos is an index to
7074 If CODING->src_object is nil, CODING->source must already point to
7075 the non-relocatable memory area. In this case, CODING->src_pos is
7076 an offset from CODING->source.
7078 The decoded data is inserted at the current point of the buffer
7083 decode_coding (struct coding_system
*coding
)
7086 Lisp_Object undo_list
;
7087 Lisp_Object translation_table
;
7088 struct ccl_spec cclspec
;
7092 if (BUFFERP (coding
->src_object
)
7093 && coding
->src_pos
> 0
7094 && coding
->src_pos
< GPT
7095 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7096 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7099 if (BUFFERP (coding
->dst_object
))
7101 if (current_buffer
!= XBUFFER (coding
->dst_object
))
7102 set_buffer_internal (XBUFFER (coding
->dst_object
));
7104 move_gap_both (PT
, PT_BYTE
);
7105 undo_list
= current_buffer
->undo_list
;
7106 current_buffer
->undo_list
= Qt
;
7109 coding
->consumed
= coding
->consumed_char
= 0;
7110 coding
->produced
= coding
->produced_char
= 0;
7111 coding
->chars_at_source
= 0;
7112 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7115 ALLOC_CONVERSION_WORK_AREA (coding
);
7117 attrs
= CODING_ID_ATTRS (coding
->id
);
7118 translation_table
= get_translation_table (attrs
, 0, NULL
);
7121 if (coding
->decoder
== decode_coding_ccl
)
7123 coding
->spec
.ccl
= &cclspec
;
7124 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7128 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7130 coding_set_source (coding
);
7131 coding
->annotated
= 0;
7132 coding
->charbuf_used
= carryover
;
7133 (*(coding
->decoder
)) (coding
);
7134 coding_set_destination (coding
);
7135 carryover
= produce_chars (coding
, translation_table
, 0);
7136 if (coding
->annotated
)
7137 produce_annotation (coding
, pos
);
7138 for (i
= 0; i
< carryover
; i
++)
7140 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7142 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7143 || (coding
->consumed
< coding
->src_bytes
7144 && (coding
->result
== CODING_RESULT_SUCCESS
7145 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7149 coding_set_destination (coding
);
7150 coding
->charbuf_used
= carryover
;
7151 produce_chars (coding
, translation_table
, 1);
7154 coding
->carryover_bytes
= 0;
7155 if (coding
->consumed
< coding
->src_bytes
)
7157 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7158 const unsigned char *src
;
7160 coding_set_source (coding
);
7161 coding_set_destination (coding
);
7162 src
= coding
->source
+ coding
->consumed
;
7164 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7166 /* Flush out unprocessed data as binary chars. We are sure
7167 that the number of data is less than the size of
7169 coding
->charbuf_used
= 0;
7170 coding
->chars_at_source
= 0;
7172 while (nbytes
-- > 0)
7177 c
= BYTE8_TO_CHAR (c
);
7178 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7180 produce_chars (coding
, Qnil
, 1);
7184 /* Record unprocessed bytes in coding->carryover. We are
7185 sure that the number of data is less than the size of
7186 coding->carryover. */
7187 unsigned char *p
= coding
->carryover
;
7189 if (nbytes
> sizeof coding
->carryover
)
7190 nbytes
= sizeof coding
->carryover
;
7191 coding
->carryover_bytes
= nbytes
;
7192 while (nbytes
-- > 0)
7195 coding
->consumed
= coding
->src_bytes
;
7198 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7199 && !inhibit_eol_conversion
)
7200 decode_eol (coding
);
7201 if (BUFFERP (coding
->dst_object
))
7203 current_buffer
->undo_list
= undo_list
;
7204 record_insert (coding
->dst_pos
, coding
->produced_char
);
7206 return coding
->result
;
7210 /* Extract an annotation datum from a composition starting at POS and
7211 ending before LIMIT of CODING->src_object (buffer or string), store
7212 the data in BUF, set *STOP to a starting position of the next
7213 composition (if any) or to LIMIT, and return the address of the
7214 next element of BUF.
7216 If such an annotation is not found, set *STOP to a starting
7217 position of a composition after POS (if any) or to LIMIT, and
7221 handle_composition_annotation (EMACS_INT pos
, EMACS_INT limit
,
7222 struct coding_system
*coding
, int *buf
,
7225 EMACS_INT start
, end
;
7228 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7231 else if (start
> pos
)
7237 /* We found a composition. Store the corresponding
7238 annotation data in BUF. */
7240 enum composition_method method
= COMPOSITION_METHOD (prop
);
7241 int nchars
= COMPOSITION_LENGTH (prop
);
7243 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7244 if (method
!= COMPOSITION_RELATIVE
)
7246 Lisp_Object components
;
7249 components
= COMPOSITION_COMPONENTS (prop
);
7250 if (VECTORP (components
))
7252 len
= XVECTOR (components
)->size
;
7253 for (i
= 0; i
< len
; i
++)
7254 *buf
++ = XINT (AREF (components
, i
));
7256 else if (STRINGP (components
))
7258 len
= SCHARS (components
);
7262 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7266 else if (INTEGERP (components
))
7269 *buf
++ = XINT (components
);
7271 else if (CONSP (components
))
7273 for (len
= 0; CONSP (components
);
7274 len
++, components
= XCDR (components
))
7275 *buf
++ = XINT (XCAR (components
));
7283 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7294 /* Extract an annotation datum from a text property `charset' at POS of
7295 CODING->src_object (buffer of string), store the data in BUF, set
7296 *STOP to the position where the value of `charset' property changes
7297 (limiting by LIMIT), and return the address of the next element of
7300 If the property value is nil, set *STOP to the position where the
7301 property value is non-nil (limiting by LIMIT), and return BUF. */
7304 handle_charset_annotation (EMACS_INT pos
, EMACS_INT limit
,
7305 struct coding_system
*coding
, int *buf
,
7308 Lisp_Object val
, next
;
7311 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7312 if (! NILP (val
) && CHARSETP (val
))
7313 id
= XINT (CHARSET_SYMBOL_ID (val
));
7316 ADD_CHARSET_DATA (buf
, 0, id
);
7317 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7319 make_number (limit
));
7320 *stop
= XINT (next
);
7326 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7329 int *buf
= coding
->charbuf
;
7330 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7331 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7332 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7333 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7334 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7335 int multibytep
= coding
->src_multibyte
;
7336 Lisp_Object eol_type
;
7338 EMACS_INT stop
, stop_composition
, stop_charset
;
7339 int *lookup_buf
= NULL
;
7341 if (! NILP (translation_table
))
7342 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7344 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7345 if (VECTORP (eol_type
))
7348 /* Note: composition handling is not yet implemented. */
7349 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7351 if (NILP (coding
->src_object
))
7352 stop
= stop_composition
= stop_charset
= end_pos
;
7355 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7356 stop
= stop_composition
= pos
;
7358 stop
= stop_composition
= end_pos
;
7359 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7360 stop
= stop_charset
= pos
;
7362 stop_charset
= end_pos
;
7365 /* Compensate for CRLF and conversion. */
7366 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7367 while (buf
< buf_end
)
7375 if (pos
== stop_composition
)
7376 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7377 buf
, &stop_composition
);
7378 if (pos
== stop_charset
)
7379 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7380 buf
, &stop_charset
);
7381 stop
= (stop_composition
< stop_charset
7382 ? stop_composition
: stop_charset
);
7389 if (coding
->encoder
== encode_coding_raw_text
7390 || coding
->encoder
== encode_coding_ccl
)
7392 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7393 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7395 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7398 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7399 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7401 if (! EQ (eol_type
, Qunix
))
7405 if (EQ (eol_type
, Qdos
))
7413 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7418 int from_nchars
= 1, to_nchars
= 1;
7419 int *lookup_buf_end
;
7420 const unsigned char *p
= src
;
7424 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7425 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7426 lookup_buf_end
= lookup_buf
+ i
;
7427 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7428 if (INTEGERP (trans
))
7430 else if (CONSP (trans
))
7432 from_nchars
= ASIZE (XCAR (trans
));
7433 trans
= XCDR (trans
);
7434 if (INTEGERP (trans
))
7438 to_nchars
= ASIZE (trans
);
7439 if (buf
+ to_nchars
> buf_end
)
7441 c
= XINT (AREF (trans
, 0));
7447 for (i
= 1; i
< to_nchars
; i
++)
7448 *buf
++ = XINT (AREF (trans
, i
));
7449 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7450 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7454 coding
->consumed
= src
- coding
->source
;
7455 coding
->consumed_char
= pos
- coding
->src_pos
;
7456 coding
->charbuf_used
= buf
- coding
->charbuf
;
7457 coding
->chars_at_source
= 0;
7461 /* Encode the text at CODING->src_object into CODING->dst_object.
7462 CODING->src_object is a buffer or a string.
7463 CODING->dst_object is a buffer or nil.
7465 If CODING->src_object is a buffer, it must be the current buffer.
7466 In this case, if CODING->src_pos is positive, it is a position of
7467 the source text in the buffer, otherwise. the source text is in the
7468 gap area of the buffer, and coding->src_pos specifies the offset of
7469 the text from GPT (which must be the same as PT). If this is the
7470 same buffer as CODING->dst_object, CODING->src_pos must be
7471 negative and CODING should not have `pre-write-conversion'.
7473 If CODING->src_object is a string, CODING should not have
7474 `pre-write-conversion'.
7476 If CODING->dst_object is a buffer, the encoded data is inserted at
7477 the current point of that buffer.
7479 If CODING->dst_object is nil, the encoded data is placed at the
7480 memory area specified by CODING->destination. */
7483 encode_coding (struct coding_system
*coding
)
7486 Lisp_Object translation_table
;
7488 struct ccl_spec cclspec
;
7490 attrs
= CODING_ID_ATTRS (coding
->id
);
7491 if (coding
->encoder
== encode_coding_raw_text
)
7492 translation_table
= Qnil
, max_lookup
= 0;
7494 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7496 if (BUFFERP (coding
->dst_object
))
7498 set_buffer_internal (XBUFFER (coding
->dst_object
));
7499 coding
->dst_multibyte
7500 = ! NILP (current_buffer
->enable_multibyte_characters
);
7503 coding
->consumed
= coding
->consumed_char
= 0;
7504 coding
->produced
= coding
->produced_char
= 0;
7505 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7508 ALLOC_CONVERSION_WORK_AREA (coding
);
7510 if (coding
->encoder
== encode_coding_ccl
)
7512 coding
->spec
.ccl
= &cclspec
;
7513 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7516 coding_set_source (coding
);
7517 consume_chars (coding
, translation_table
, max_lookup
);
7518 coding_set_destination (coding
);
7519 (*(coding
->encoder
)) (coding
);
7520 } while (coding
->consumed_char
< coding
->src_chars
);
7522 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7523 insert_from_gap (coding
->produced_char
, coding
->produced
);
7525 return (coding
->result
);
7529 /* Name (or base name) of work buffer for code conversion. */
7530 static Lisp_Object Vcode_conversion_workbuf_name
;
7532 /* A working buffer used by the top level conversion. Once it is
7533 created, it is never destroyed. It has the name
7534 Vcode_conversion_workbuf_name. The other working buffers are
7535 destroyed after the use is finished, and their names are modified
7536 versions of Vcode_conversion_workbuf_name. */
7537 static Lisp_Object Vcode_conversion_reused_workbuf
;
7539 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7540 static int reused_workbuf_in_use
;
7543 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7544 multibyteness of returning buffer. */
7547 make_conversion_work_buffer (int multibyte
)
7549 Lisp_Object name
, workbuf
;
7550 struct buffer
*current
;
7552 if (reused_workbuf_in_use
++)
7554 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7555 workbuf
= Fget_buffer_create (name
);
7559 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7560 Vcode_conversion_reused_workbuf
7561 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7562 workbuf
= Vcode_conversion_reused_workbuf
;
7564 current
= current_buffer
;
7565 set_buffer_internal (XBUFFER (workbuf
));
7566 /* We can't allow modification hooks to run in the work buffer. For
7567 instance, directory_files_internal assumes that file decoding
7568 doesn't compile new regexps. */
7569 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7571 current_buffer
->undo_list
= Qt
;
7572 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
7573 set_buffer_internal (current
);
7579 code_conversion_restore (Lisp_Object arg
)
7581 Lisp_Object current
, workbuf
;
7582 struct gcpro gcpro1
;
7585 current
= XCAR (arg
);
7586 workbuf
= XCDR (arg
);
7587 if (! NILP (workbuf
))
7589 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7590 reused_workbuf_in_use
= 0;
7591 else if (! NILP (Fbuffer_live_p (workbuf
)))
7592 Fkill_buffer (workbuf
);
7594 set_buffer_internal (XBUFFER (current
));
7600 code_conversion_save (int with_work_buf
, int multibyte
)
7602 Lisp_Object workbuf
= Qnil
;
7605 workbuf
= make_conversion_work_buffer (multibyte
);
7606 record_unwind_protect (code_conversion_restore
,
7607 Fcons (Fcurrent_buffer (), workbuf
));
7612 decode_coding_gap (struct coding_system
*coding
,
7613 EMACS_INT chars
, EMACS_INT bytes
)
7615 int count
= SPECPDL_INDEX ();
7618 code_conversion_save (0, 0);
7620 coding
->src_object
= Fcurrent_buffer ();
7621 coding
->src_chars
= chars
;
7622 coding
->src_bytes
= bytes
;
7623 coding
->src_pos
= -chars
;
7624 coding
->src_pos_byte
= -bytes
;
7625 coding
->src_multibyte
= chars
< bytes
;
7626 coding
->dst_object
= coding
->src_object
;
7627 coding
->dst_pos
= PT
;
7628 coding
->dst_pos_byte
= PT_BYTE
;
7629 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
7631 if (CODING_REQUIRE_DETECTION (coding
))
7632 detect_coding (coding
);
7634 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7635 current_buffer
->text
->inhibit_shrinking
= 1;
7636 decode_coding (coding
);
7637 current_buffer
->text
->inhibit_shrinking
= 0;
7639 attrs
= CODING_ID_ATTRS (coding
->id
);
7640 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7642 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7645 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7646 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7647 make_number (coding
->produced_char
));
7649 coding
->produced_char
+= Z
- prev_Z
;
7650 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7653 unbind_to (count
, Qnil
);
7654 return coding
->result
;
7658 encode_coding_gap (struct coding_system
*coding
,
7659 EMACS_INT chars
, EMACS_INT bytes
)
7661 int count
= SPECPDL_INDEX ();
7663 code_conversion_save (0, 0);
7665 coding
->src_object
= Fcurrent_buffer ();
7666 coding
->src_chars
= chars
;
7667 coding
->src_bytes
= bytes
;
7668 coding
->src_pos
= -chars
;
7669 coding
->src_pos_byte
= -bytes
;
7670 coding
->src_multibyte
= chars
< bytes
;
7671 coding
->dst_object
= coding
->src_object
;
7672 coding
->dst_pos
= PT
;
7673 coding
->dst_pos_byte
= PT_BYTE
;
7675 encode_coding (coding
);
7677 unbind_to (count
, Qnil
);
7678 return coding
->result
;
7682 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7683 SRC_OBJECT into DST_OBJECT by coding context CODING.
7685 SRC_OBJECT is a buffer, a string, or Qnil.
7687 If it is a buffer, the text is at point of the buffer. FROM and TO
7688 are positions in the buffer.
7690 If it is a string, the text is at the beginning of the string.
7691 FROM and TO are indices to the string.
7693 If it is nil, the text is at coding->source. FROM and TO are
7694 indices to coding->source.
7696 DST_OBJECT is a buffer, Qt, or Qnil.
7698 If it is a buffer, the decoded text is inserted at point of the
7699 buffer. If the buffer is the same as SRC_OBJECT, the source text
7702 If it is Qt, a string is made from the decoded text, and
7703 set in CODING->dst_object.
7705 If it is Qnil, the decoded text is stored at CODING->destination.
7706 The caller must allocate CODING->dst_bytes bytes at
7707 CODING->destination by xmalloc. If the decoded text is longer than
7708 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7712 decode_coding_object (struct coding_system
*coding
,
7713 Lisp_Object src_object
,
7714 EMACS_INT from
, EMACS_INT from_byte
,
7715 EMACS_INT to
, EMACS_INT to_byte
,
7716 Lisp_Object dst_object
)
7718 int count
= SPECPDL_INDEX ();
7719 unsigned char *destination
;
7720 EMACS_INT dst_bytes
;
7721 EMACS_INT chars
= to
- from
;
7722 EMACS_INT bytes
= to_byte
- from_byte
;
7724 int saved_pt
= -1, saved_pt_byte
;
7725 int need_marker_adjustment
= 0;
7726 Lisp_Object old_deactivate_mark
;
7728 old_deactivate_mark
= Vdeactivate_mark
;
7730 if (NILP (dst_object
))
7732 destination
= coding
->destination
;
7733 dst_bytes
= coding
->dst_bytes
;
7736 coding
->src_object
= src_object
;
7737 coding
->src_chars
= chars
;
7738 coding
->src_bytes
= bytes
;
7739 coding
->src_multibyte
= chars
< bytes
;
7741 if (STRINGP (src_object
))
7743 coding
->src_pos
= from
;
7744 coding
->src_pos_byte
= from_byte
;
7746 else if (BUFFERP (src_object
))
7748 set_buffer_internal (XBUFFER (src_object
));
7750 move_gap_both (from
, from_byte
);
7751 if (EQ (src_object
, dst_object
))
7753 struct Lisp_Marker
*tail
;
7755 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7757 tail
->need_adjustment
7758 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7759 need_marker_adjustment
|= tail
->need_adjustment
;
7761 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7762 TEMP_SET_PT_BOTH (from
, from_byte
);
7763 current_buffer
->text
->inhibit_shrinking
= 1;
7764 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7765 coding
->src_pos
= -chars
;
7766 coding
->src_pos_byte
= -bytes
;
7770 coding
->src_pos
= from
;
7771 coding
->src_pos_byte
= from_byte
;
7775 if (CODING_REQUIRE_DETECTION (coding
))
7776 detect_coding (coding
);
7777 attrs
= CODING_ID_ATTRS (coding
->id
);
7779 if (EQ (dst_object
, Qt
)
7780 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7781 && NILP (dst_object
)))
7783 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7784 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7785 coding
->dst_pos
= BEG
;
7786 coding
->dst_pos_byte
= BEG_BYTE
;
7788 else if (BUFFERP (dst_object
))
7790 code_conversion_save (0, 0);
7791 coding
->dst_object
= dst_object
;
7792 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7793 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7794 coding
->dst_multibyte
7795 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7799 code_conversion_save (0, 0);
7800 coding
->dst_object
= Qnil
;
7801 /* Most callers presume this will return a multibyte result, and they
7802 won't use `binary' or `raw-text' anyway, so let's not worry about
7803 CODING_FOR_UNIBYTE. */
7804 coding
->dst_multibyte
= 1;
7807 decode_coding (coding
);
7809 if (BUFFERP (coding
->dst_object
))
7810 set_buffer_internal (XBUFFER (coding
->dst_object
));
7812 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7814 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7815 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7818 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7819 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7820 old_deactivate_mark
);
7821 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7822 make_number (coding
->produced_char
));
7825 coding
->produced_char
+= Z
- prev_Z
;
7826 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7829 if (EQ (dst_object
, Qt
))
7831 coding
->dst_object
= Fbuffer_string ();
7833 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7835 set_buffer_internal (XBUFFER (coding
->dst_object
));
7836 if (dst_bytes
< coding
->produced
)
7838 destination
= xrealloc (destination
, coding
->produced
);
7841 record_conversion_result (coding
,
7842 CODING_RESULT_INSUFFICIENT_MEM
);
7843 unbind_to (count
, Qnil
);
7846 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7847 move_gap_both (BEGV
, BEGV_BYTE
);
7848 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7849 coding
->destination
= destination
;
7855 /* This is the case of:
7856 (BUFFERP (src_object) && EQ (src_object, dst_object))
7857 As we have moved PT while replacing the original buffer
7858 contents, we must recover it now. */
7859 set_buffer_internal (XBUFFER (src_object
));
7860 current_buffer
->text
->inhibit_shrinking
= 0;
7861 if (saved_pt
< from
)
7862 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7863 else if (saved_pt
< from
+ chars
)
7864 TEMP_SET_PT_BOTH (from
, from_byte
);
7865 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7866 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7867 saved_pt_byte
+ (coding
->produced
- bytes
));
7869 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7870 saved_pt_byte
+ (coding
->produced
- bytes
));
7872 if (need_marker_adjustment
)
7874 struct Lisp_Marker
*tail
;
7876 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7877 if (tail
->need_adjustment
)
7879 tail
->need_adjustment
= 0;
7880 if (tail
->insertion_type
)
7882 tail
->bytepos
= from_byte
;
7883 tail
->charpos
= from
;
7887 tail
->bytepos
= from_byte
+ coding
->produced
;
7889 = (NILP (current_buffer
->enable_multibyte_characters
)
7890 ? tail
->bytepos
: from
+ coding
->produced_char
);
7896 Vdeactivate_mark
= old_deactivate_mark
;
7897 unbind_to (count
, coding
->dst_object
);
7902 encode_coding_object (struct coding_system
*coding
,
7903 Lisp_Object src_object
,
7904 EMACS_INT from
, EMACS_INT from_byte
,
7905 EMACS_INT to
, EMACS_INT to_byte
,
7906 Lisp_Object dst_object
)
7908 int count
= SPECPDL_INDEX ();
7909 EMACS_INT chars
= to
- from
;
7910 EMACS_INT bytes
= to_byte
- from_byte
;
7912 int saved_pt
= -1, saved_pt_byte
;
7913 int need_marker_adjustment
= 0;
7914 int kill_src_buffer
= 0;
7915 Lisp_Object old_deactivate_mark
;
7917 old_deactivate_mark
= Vdeactivate_mark
;
7919 coding
->src_object
= src_object
;
7920 coding
->src_chars
= chars
;
7921 coding
->src_bytes
= bytes
;
7922 coding
->src_multibyte
= chars
< bytes
;
7924 attrs
= CODING_ID_ATTRS (coding
->id
);
7926 if (EQ (src_object
, dst_object
))
7928 struct Lisp_Marker
*tail
;
7930 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7932 tail
->need_adjustment
7933 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7934 need_marker_adjustment
|= tail
->need_adjustment
;
7938 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7940 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7941 set_buffer_internal (XBUFFER (coding
->src_object
));
7942 if (STRINGP (src_object
))
7943 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7944 else if (BUFFERP (src_object
))
7945 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7947 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7949 if (EQ (src_object
, dst_object
))
7951 set_buffer_internal (XBUFFER (src_object
));
7952 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7953 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7954 set_buffer_internal (XBUFFER (coding
->src_object
));
7958 Lisp_Object args
[3];
7959 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7961 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7962 old_deactivate_mark
);
7963 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7964 args
[1] = make_number (BEG
);
7965 args
[2] = make_number (Z
);
7966 safe_call (3, args
);
7969 if (XBUFFER (coding
->src_object
) != current_buffer
)
7970 kill_src_buffer
= 1;
7971 coding
->src_object
= Fcurrent_buffer ();
7973 move_gap_both (BEG
, BEG_BYTE
);
7974 coding
->src_chars
= Z
- BEG
;
7975 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7976 coding
->src_pos
= BEG
;
7977 coding
->src_pos_byte
= BEG_BYTE
;
7978 coding
->src_multibyte
= Z
< Z_BYTE
;
7980 else if (STRINGP (src_object
))
7982 code_conversion_save (0, 0);
7983 coding
->src_pos
= from
;
7984 coding
->src_pos_byte
= from_byte
;
7986 else if (BUFFERP (src_object
))
7988 code_conversion_save (0, 0);
7989 set_buffer_internal (XBUFFER (src_object
));
7990 if (EQ (src_object
, dst_object
))
7992 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7993 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7994 coding
->src_pos
= 0;
7995 coding
->src_pos_byte
= 0;
7999 if (from
< GPT
&& to
>= GPT
)
8000 move_gap_both (from
, from_byte
);
8001 coding
->src_pos
= from
;
8002 coding
->src_pos_byte
= from_byte
;
8006 code_conversion_save (0, 0);
8008 if (BUFFERP (dst_object
))
8010 coding
->dst_object
= dst_object
;
8011 if (EQ (src_object
, dst_object
))
8013 coding
->dst_pos
= from
;
8014 coding
->dst_pos_byte
= from_byte
;
8018 struct buffer
*current
= current_buffer
;
8020 set_buffer_temp (XBUFFER (dst_object
));
8021 coding
->dst_pos
= PT
;
8022 coding
->dst_pos_byte
= PT_BYTE
;
8023 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
8024 set_buffer_temp (current
);
8026 coding
->dst_multibyte
8027 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
8029 else if (EQ (dst_object
, Qt
))
8031 coding
->dst_object
= Qnil
;
8032 coding
->dst_bytes
= coding
->src_chars
;
8033 if (coding
->dst_bytes
== 0)
8034 coding
->dst_bytes
= 1;
8035 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
8036 coding
->dst_multibyte
= 0;
8040 coding
->dst_object
= Qnil
;
8041 coding
->dst_multibyte
= 0;
8044 encode_coding (coding
);
8046 if (EQ (dst_object
, Qt
))
8048 if (BUFFERP (coding
->dst_object
))
8049 coding
->dst_object
= Fbuffer_string ();
8053 = make_unibyte_string ((char *) coding
->destination
,
8055 xfree (coding
->destination
);
8061 /* This is the case of:
8062 (BUFFERP (src_object) && EQ (src_object, dst_object))
8063 As we have moved PT while replacing the original buffer
8064 contents, we must recover it now. */
8065 set_buffer_internal (XBUFFER (src_object
));
8066 if (saved_pt
< from
)
8067 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8068 else if (saved_pt
< from
+ chars
)
8069 TEMP_SET_PT_BOTH (from
, from_byte
);
8070 else if (! NILP (current_buffer
->enable_multibyte_characters
))
8071 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8072 saved_pt_byte
+ (coding
->produced
- bytes
));
8074 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8075 saved_pt_byte
+ (coding
->produced
- bytes
));
8077 if (need_marker_adjustment
)
8079 struct Lisp_Marker
*tail
;
8081 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8082 if (tail
->need_adjustment
)
8084 tail
->need_adjustment
= 0;
8085 if (tail
->insertion_type
)
8087 tail
->bytepos
= from_byte
;
8088 tail
->charpos
= from
;
8092 tail
->bytepos
= from_byte
+ coding
->produced
;
8094 = (NILP (current_buffer
->enable_multibyte_characters
)
8095 ? tail
->bytepos
: from
+ coding
->produced_char
);
8101 if (kill_src_buffer
)
8102 Fkill_buffer (coding
->src_object
);
8104 Vdeactivate_mark
= old_deactivate_mark
;
8105 unbind_to (count
, Qnil
);
8110 preferred_coding_system (void)
8112 int id
= coding_categories
[coding_priorities
[0]].id
;
8114 return CODING_ID_NAME (id
);
8119 /*** 8. Emacs Lisp library functions ***/
8121 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8122 doc
: /* Return t if OBJECT is nil or a coding-system.
8123 See the documentation of `define-coding-system' for information
8124 about coding-system objects. */)
8125 (Lisp_Object object
)
8128 || CODING_SYSTEM_ID (object
) >= 0)
8130 if (! SYMBOLP (object
)
8131 || NILP (Fget (object
, Qcoding_system_define_form
)))
8136 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8137 Sread_non_nil_coding_system
, 1, 1, 0,
8138 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8139 (Lisp_Object prompt
)
8144 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8145 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8147 while (SCHARS (val
) == 0);
8148 return (Fintern (val
, Qnil
));
8151 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8152 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8153 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8154 Ignores case when completing coding systems (all Emacs coding systems
8155 are lower-case). */)
8156 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8159 int count
= SPECPDL_INDEX ();
8161 if (SYMBOLP (default_coding_system
))
8162 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8163 specbind (Qcompletion_ignore_case
, Qt
);
8164 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8165 Qt
, Qnil
, Qcoding_system_history
,
8166 default_coding_system
, Qnil
);
8167 unbind_to (count
, Qnil
);
8168 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8171 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8173 doc
: /* Check validity of CODING-SYSTEM.
8174 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8175 It is valid if it is nil or a symbol defined as a coding system by the
8176 function `define-coding-system'. */)
8177 (Lisp_Object coding_system
)
8179 Lisp_Object define_form
;
8181 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8182 if (! NILP (define_form
))
8184 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8185 safe_eval (define_form
);
8187 if (!NILP (Fcoding_system_p (coding_system
)))
8188 return coding_system
;
8189 xsignal1 (Qcoding_system_error
, coding_system
);
8193 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8194 HIGHEST is nonzero, return the coding system of the highest
8195 priority among the detected coding systems. Otherwise return a
8196 list of detected coding systems sorted by their priorities. If
8197 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8198 multibyte form but contains only ASCII and eight-bit chars.
8199 Otherwise, the bytes are raw bytes.
8201 CODING-SYSTEM controls the detection as below:
8203 If it is nil, detect both text-format and eol-format. If the
8204 text-format part of CODING-SYSTEM is already specified
8205 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8206 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8207 detect only text-format. */
8210 detect_coding_system (const unsigned char *src
,
8211 EMACS_INT src_chars
, EMACS_INT src_bytes
,
8212 int highest
, int multibytep
,
8213 Lisp_Object coding_system
)
8215 const unsigned char *src_end
= src
+ src_bytes
;
8216 Lisp_Object attrs
, eol_type
;
8217 Lisp_Object val
= Qnil
;
8218 struct coding_system coding
;
8220 struct coding_detection_info detect_info
;
8221 enum coding_category base_category
;
8222 int null_byte_found
= 0, eight_bit_found
= 0;
8224 if (NILP (coding_system
))
8225 coding_system
= Qundecided
;
8226 setup_coding_system (coding_system
, &coding
);
8227 attrs
= CODING_ID_ATTRS (coding
.id
);
8228 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8229 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8231 coding
.source
= src
;
8232 coding
.src_chars
= src_chars
;
8233 coding
.src_bytes
= src_bytes
;
8234 coding
.src_multibyte
= multibytep
;
8235 coding
.consumed
= 0;
8236 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8237 coding
.head_ascii
= 0;
8239 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8241 /* At first, detect text-format if necessary. */
8242 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8243 if (base_category
== coding_category_undecided
)
8245 enum coding_category category
;
8246 struct coding_system
*this;
8249 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8250 for (; src
< src_end
; src
++)
8255 eight_bit_found
= 1;
8256 if (null_byte_found
)
8261 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8262 && ! inhibit_iso_escape_detection
8263 && ! detect_info
.checked
)
8265 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8267 /* We have scanned the whole data. */
8268 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8270 /* We didn't find an 8-bit code. We may
8271 have found a null-byte, but it's very
8272 rare that a binary file confirm to
8275 coding
.head_ascii
= src
- coding
.source
;
8277 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8281 else if (! c
&& !inhibit_null_byte_detection
)
8283 null_byte_found
= 1;
8284 if (eight_bit_found
)
8287 if (! eight_bit_found
)
8288 coding
.head_ascii
++;
8290 else if (! eight_bit_found
)
8291 coding
.head_ascii
++;
8294 if (null_byte_found
|| eight_bit_found
8295 || coding
.head_ascii
< coding
.src_bytes
8296 || detect_info
.found
)
8298 if (coding
.head_ascii
== coding
.src_bytes
)
8299 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8300 for (i
= 0; i
< coding_category_raw_text
; i
++)
8302 category
= coding_priorities
[i
];
8303 this = coding_categories
+ category
;
8304 if (detect_info
.found
& (1 << category
))
8309 if (null_byte_found
)
8311 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8312 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8314 for (i
= 0; i
< coding_category_raw_text
; i
++)
8316 category
= coding_priorities
[i
];
8317 this = coding_categories
+ category
;
8321 /* No coding system of this category is defined. */
8322 detect_info
.rejected
|= (1 << category
);
8324 else if (category
>= coding_category_raw_text
)
8326 else if (detect_info
.checked
& (1 << category
))
8329 && (detect_info
.found
& (1 << category
)))
8332 else if ((*(this->detector
)) (&coding
, &detect_info
)
8334 && (detect_info
.found
& (1 << category
)))
8336 if (category
== coding_category_utf_16_auto
)
8338 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8339 category
= coding_category_utf_16_le
;
8341 category
= coding_category_utf_16_be
;
8349 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8352 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8353 id
= CODING_SYSTEM_ID (Qno_conversion
);
8354 val
= Fcons (make_number (id
), Qnil
);
8356 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8358 detect_info
.found
= CATEGORY_MASK_ANY
;
8359 id
= coding_categories
[coding_category_undecided
].id
;
8360 val
= Fcons (make_number (id
), Qnil
);
8364 if (detect_info
.found
)
8366 detect_info
.found
= 1 << category
;
8367 val
= Fcons (make_number (this->id
), Qnil
);
8370 for (i
= 0; i
< coding_category_raw_text
; i
++)
8371 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8373 detect_info
.found
= 1 << coding_priorities
[i
];
8374 id
= coding_categories
[coding_priorities
[i
]].id
;
8375 val
= Fcons (make_number (id
), Qnil
);
8381 int mask
= detect_info
.rejected
| detect_info
.found
;
8384 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8386 category
= coding_priorities
[i
];
8387 if (! (mask
& (1 << category
)))
8389 found
|= 1 << category
;
8390 id
= coding_categories
[category
].id
;
8392 val
= Fcons (make_number (id
), val
);
8395 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8397 category
= coding_priorities
[i
];
8398 if (detect_info
.found
& (1 << category
))
8400 id
= coding_categories
[category
].id
;
8401 val
= Fcons (make_number (id
), val
);
8404 detect_info
.found
|= found
;
8407 else if (base_category
== coding_category_utf_8_auto
)
8409 if (detect_coding_utf_8 (&coding
, &detect_info
))
8411 struct coding_system
*this;
8413 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8414 this = coding_categories
+ coding_category_utf_8_sig
;
8416 this = coding_categories
+ coding_category_utf_8_nosig
;
8417 val
= Fcons (make_number (this->id
), Qnil
);
8420 else if (base_category
== coding_category_utf_16_auto
)
8422 if (detect_coding_utf_16 (&coding
, &detect_info
))
8424 struct coding_system
*this;
8426 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8427 this = coding_categories
+ coding_category_utf_16_le
;
8428 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8429 this = coding_categories
+ coding_category_utf_16_be
;
8430 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8431 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8433 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8434 val
= Fcons (make_number (this->id
), Qnil
);
8439 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8440 val
= Fcons (make_number (coding
.id
), Qnil
);
8443 /* Then, detect eol-format if necessary. */
8445 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8448 if (VECTORP (eol_type
))
8450 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8452 if (null_byte_found
)
8453 normal_eol
= EOL_SEEN_LF
;
8455 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8456 coding_category_raw_text
);
8458 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8459 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8460 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8461 coding_category_utf_16_be
);
8462 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8463 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8464 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8465 coding_category_utf_16_le
);
8469 if (EQ (eol_type
, Qunix
))
8470 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8471 else if (EQ (eol_type
, Qdos
))
8472 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8474 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8477 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8479 enum coding_category category
;
8482 id
= XINT (XCAR (tail
));
8483 attrs
= CODING_ID_ATTRS (id
);
8484 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8485 eol_type
= CODING_ID_EOL_TYPE (id
);
8486 if (VECTORP (eol_type
))
8488 if (category
== coding_category_utf_16_be
8489 || category
== coding_category_utf_16_be_nosig
)
8490 this_eol
= utf_16_be_eol
;
8491 else if (category
== coding_category_utf_16_le
8492 || category
== coding_category_utf_16_le_nosig
)
8493 this_eol
= utf_16_le_eol
;
8495 this_eol
= normal_eol
;
8497 if (this_eol
== EOL_SEEN_LF
)
8498 XSETCAR (tail
, AREF (eol_type
, 0));
8499 else if (this_eol
== EOL_SEEN_CRLF
)
8500 XSETCAR (tail
, AREF (eol_type
, 1));
8501 else if (this_eol
== EOL_SEEN_CR
)
8502 XSETCAR (tail
, AREF (eol_type
, 2));
8504 XSETCAR (tail
, CODING_ID_NAME (id
));
8507 XSETCAR (tail
, CODING_ID_NAME (id
));
8511 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8515 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8517 doc
: /* Detect coding system of the text in the region between START and END.
8518 Return a list of possible coding systems ordered by priority.
8519 The coding systems to try and their priorities follows what
8520 the function `coding-system-priority-list' (which see) returns.
8522 If only ASCII characters are found (except for such ISO-2022 control
8523 characters as ESC), it returns a list of single element `undecided'
8524 or its subsidiary coding system according to a detected end-of-line
8527 If optional argument HIGHEST is non-nil, return the coding system of
8528 highest priority. */)
8529 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8532 int from_byte
, to_byte
;
8534 CHECK_NUMBER_COERCE_MARKER (start
);
8535 CHECK_NUMBER_COERCE_MARKER (end
);
8537 validate_region (&start
, &end
);
8538 from
= XINT (start
), to
= XINT (end
);
8539 from_byte
= CHAR_TO_BYTE (from
);
8540 to_byte
= CHAR_TO_BYTE (to
);
8542 if (from
< GPT
&& to
>= GPT
)
8543 move_gap_both (to
, to_byte
);
8545 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8546 to
- from
, to_byte
- from_byte
,
8548 !NILP (current_buffer
8549 ->enable_multibyte_characters
),
8553 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8555 doc
: /* Detect coding system of the text in STRING.
8556 Return a list of possible coding systems ordered by priority.
8557 The coding systems to try and their priorities follows what
8558 the function `coding-system-priority-list' (which see) returns.
8560 If only ASCII characters are found (except for such ISO-2022 control
8561 characters as ESC), it returns a list of single element `undecided'
8562 or its subsidiary coding system according to a detected end-of-line
8565 If optional argument HIGHEST is non-nil, return the coding system of
8566 highest priority. */)
8567 (Lisp_Object string
, Lisp_Object highest
)
8569 CHECK_STRING (string
);
8571 return detect_coding_system (SDATA (string
),
8572 SCHARS (string
), SBYTES (string
),
8573 !NILP (highest
), STRING_MULTIBYTE (string
),
8579 char_encodable_p (int c
, Lisp_Object attrs
)
8582 struct charset
*charset
;
8583 Lisp_Object translation_table
;
8585 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8586 if (! NILP (translation_table
))
8587 c
= translate_char (translation_table
, c
);
8588 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8589 CONSP (tail
); tail
= XCDR (tail
))
8591 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8592 if (CHAR_CHARSET_P (c
, charset
))
8595 return (! NILP (tail
));
8599 /* Return a list of coding systems that safely encode the text between
8600 START and END. If EXCLUDE is non-nil, it is a list of coding
8601 systems not to check. The returned list doesn't contain any such
8602 coding systems. In any case, if the text contains only ASCII or is
8603 unibyte, return t. */
8605 DEFUN ("find-coding-systems-region-internal",
8606 Ffind_coding_systems_region_internal
,
8607 Sfind_coding_systems_region_internal
, 2, 3, 0,
8608 doc
: /* Internal use only. */)
8609 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8611 Lisp_Object coding_attrs_list
, safe_codings
;
8612 EMACS_INT start_byte
, end_byte
;
8613 const unsigned char *p
, *pbeg
, *pend
;
8615 Lisp_Object tail
, elt
, work_table
;
8617 if (STRINGP (start
))
8619 if (!STRING_MULTIBYTE (start
)
8620 || SCHARS (start
) == SBYTES (start
))
8623 end_byte
= SBYTES (start
);
8627 CHECK_NUMBER_COERCE_MARKER (start
);
8628 CHECK_NUMBER_COERCE_MARKER (end
);
8629 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8630 args_out_of_range (start
, end
);
8631 if (NILP (current_buffer
->enable_multibyte_characters
))
8633 start_byte
= CHAR_TO_BYTE (XINT (start
));
8634 end_byte
= CHAR_TO_BYTE (XINT (end
));
8635 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8638 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8640 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8641 move_gap_both (XINT (start
), start_byte
);
8643 move_gap_both (XINT (end
), end_byte
);
8647 coding_attrs_list
= Qnil
;
8648 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8650 || NILP (Fmemq (XCAR (tail
), exclude
)))
8654 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8655 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8656 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8658 ASET (attrs
, coding_attr_trans_tbl
,
8659 get_translation_table (attrs
, 1, NULL
));
8660 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8664 if (STRINGP (start
))
8665 p
= pbeg
= SDATA (start
);
8667 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8668 pend
= p
+ (end_byte
- start_byte
);
8670 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8671 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8673 work_table
= Fmake_char_table (Qnil
, Qnil
);
8676 if (ASCII_BYTE_P (*p
))
8680 c
= STRING_CHAR_ADVANCE (p
);
8681 if (!NILP (char_table_ref (work_table
, c
)))
8682 /* This character was already checked. Ignore it. */
8685 charset_map_loaded
= 0;
8686 for (tail
= coding_attrs_list
; CONSP (tail
);)
8691 else if (char_encodable_p (c
, elt
))
8693 else if (CONSP (XCDR (tail
)))
8695 XSETCAR (tail
, XCAR (XCDR (tail
)));
8696 XSETCDR (tail
, XCDR (XCDR (tail
)));
8700 XSETCAR (tail
, Qnil
);
8704 if (charset_map_loaded
)
8706 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8708 if (STRINGP (start
))
8709 pbeg
= SDATA (start
);
8711 pbeg
= BYTE_POS_ADDR (start_byte
);
8712 p
= pbeg
+ p_offset
;
8713 pend
= pbeg
+ pend_offset
;
8715 char_table_set (work_table
, c
, Qt
);
8719 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8720 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8721 if (! NILP (XCAR (tail
)))
8722 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8724 return safe_codings
;
8728 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8729 Sunencodable_char_position
, 3, 5, 0,
8731 Return position of first un-encodable character in a region.
8732 START and END specify the region and CODING-SYSTEM specifies the
8733 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8735 If optional 4th argument COUNT is non-nil, it specifies at most how
8736 many un-encodable characters to search. In this case, the value is a
8739 If optional 5th argument STRING is non-nil, it is a string to search
8740 for un-encodable characters. In that case, START and END are indexes
8742 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8745 struct coding_system coding
;
8746 Lisp_Object attrs
, charset_list
, translation_table
;
8747 Lisp_Object positions
;
8749 const unsigned char *p
, *stop
, *pend
;
8750 int ascii_compatible
;
8752 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8753 attrs
= CODING_ID_ATTRS (coding
.id
);
8754 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8756 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8757 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8758 translation_table
= get_translation_table (attrs
, 1, NULL
);
8762 validate_region (&start
, &end
);
8763 from
= XINT (start
);
8765 if (NILP (current_buffer
->enable_multibyte_characters
)
8766 || (ascii_compatible
8767 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8769 p
= CHAR_POS_ADDR (from
);
8770 pend
= CHAR_POS_ADDR (to
);
8771 if (from
< GPT
&& to
>= GPT
)
8778 CHECK_STRING (string
);
8779 CHECK_NATNUM (start
);
8781 from
= XINT (start
);
8784 || to
> SCHARS (string
))
8785 args_out_of_range_3 (string
, start
, end
);
8786 if (! STRING_MULTIBYTE (string
))
8788 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8789 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8790 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8798 CHECK_NATNUM (count
);
8807 if (ascii_compatible
)
8808 while (p
< stop
&& ASCII_BYTE_P (*p
))
8818 c
= STRING_CHAR_ADVANCE (p
);
8819 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8820 && ! char_charset (translate_char (translation_table
, c
),
8821 charset_list
, NULL
))
8823 positions
= Fcons (make_number (from
), positions
);
8832 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8836 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8837 Scheck_coding_systems_region
, 3, 3, 0,
8838 doc
: /* Check if the region is encodable by coding systems.
8840 START and END are buffer positions specifying the region.
8841 CODING-SYSTEM-LIST is a list of coding systems to check.
8843 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8844 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8845 whole region, POS0, POS1, ... are buffer positions where non-encodable
8846 characters are found.
8848 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8851 START may be a string. In that case, check if the string is
8852 encodable, and the value contains indices to the string instead of
8853 buffer positions. END is ignored.
8855 If the current buffer (or START if it is a string) is unibyte, the value
8857 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8860 EMACS_INT start_byte
, end_byte
;
8862 const unsigned char *p
, *pbeg
, *pend
;
8864 Lisp_Object tail
, elt
, attrs
;
8866 if (STRINGP (start
))
8868 if (!STRING_MULTIBYTE (start
)
8869 || SCHARS (start
) == SBYTES (start
))
8872 end_byte
= SBYTES (start
);
8877 CHECK_NUMBER_COERCE_MARKER (start
);
8878 CHECK_NUMBER_COERCE_MARKER (end
);
8879 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8880 args_out_of_range (start
, end
);
8881 if (NILP (current_buffer
->enable_multibyte_characters
))
8883 start_byte
= CHAR_TO_BYTE (XINT (start
));
8884 end_byte
= CHAR_TO_BYTE (XINT (end
));
8885 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8888 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8890 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8891 move_gap_both (XINT (start
), start_byte
);
8893 move_gap_both (XINT (end
), end_byte
);
8899 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8902 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8903 ASET (attrs
, coding_attr_trans_tbl
,
8904 get_translation_table (attrs
, 1, NULL
));
8905 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8908 if (STRINGP (start
))
8909 p
= pbeg
= SDATA (start
);
8911 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8912 pend
= p
+ (end_byte
- start_byte
);
8914 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8915 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8919 if (ASCII_BYTE_P (*p
))
8923 c
= STRING_CHAR_ADVANCE (p
);
8925 charset_map_loaded
= 0;
8926 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8928 elt
= XCDR (XCAR (tail
));
8929 if (! char_encodable_p (c
, XCAR (elt
)))
8930 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8932 if (charset_map_loaded
)
8934 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8936 if (STRINGP (start
))
8937 pbeg
= SDATA (start
);
8939 pbeg
= BYTE_POS_ADDR (start_byte
);
8940 p
= pbeg
+ p_offset
;
8941 pend
= pbeg
+ pend_offset
;
8949 for (; CONSP (tail
); tail
= XCDR (tail
))
8952 if (CONSP (XCDR (XCDR (elt
))))
8953 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8962 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8963 Lisp_Object coding_system
, Lisp_Object dst_object
,
8964 int encodep
, int norecord
)
8966 struct coding_system coding
;
8967 EMACS_INT from
, from_byte
, to
, to_byte
;
8968 Lisp_Object src_object
;
8970 CHECK_NUMBER_COERCE_MARKER (start
);
8971 CHECK_NUMBER_COERCE_MARKER (end
);
8972 if (NILP (coding_system
))
8973 coding_system
= Qno_conversion
;
8975 CHECK_CODING_SYSTEM (coding_system
);
8976 src_object
= Fcurrent_buffer ();
8977 if (NILP (dst_object
))
8978 dst_object
= src_object
;
8979 else if (! EQ (dst_object
, Qt
))
8980 CHECK_BUFFER (dst_object
);
8982 validate_region (&start
, &end
);
8983 from
= XFASTINT (start
);
8984 from_byte
= CHAR_TO_BYTE (from
);
8985 to
= XFASTINT (end
);
8986 to_byte
= CHAR_TO_BYTE (to
);
8988 setup_coding_system (coding_system
, &coding
);
8989 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8992 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8995 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8998 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9000 return (BUFFERP (dst_object
)
9001 ? make_number (coding
.produced_char
)
9002 : coding
.dst_object
);
9006 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
9007 3, 4, "r\nzCoding system: ",
9008 doc
: /* Decode the current region from the specified coding system.
9009 When called from a program, takes four arguments:
9010 START, END, CODING-SYSTEM, and DESTINATION.
9011 START and END are buffer positions.
9013 Optional 4th arguments DESTINATION specifies where the decoded text goes.
9014 If nil, the region between START and END is replaced by the decoded text.
9015 If buffer, the decoded text is inserted in that buffer after point (point
9017 In those cases, the length of the decoded text is returned.
9018 If DESTINATION is t, the decoded text is returned.
9020 This function sets `last-coding-system-used' to the precise coding system
9021 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9022 not fully specified.) */)
9023 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
9025 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
9028 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
9029 3, 4, "r\nzCoding system: ",
9030 doc
: /* Encode the current region by specified coding system.
9031 When called from a program, takes four arguments:
9032 START, END, CODING-SYSTEM and DESTINATION.
9033 START and END are buffer positions.
9035 Optional 4th arguments DESTINATION specifies where the encoded text goes.
9036 If nil, the region between START and END is replace by the encoded text.
9037 If buffer, the encoded text is inserted in that buffer after point (point
9039 In those cases, the length of the encoded text is returned.
9040 If DESTINATION is t, the encoded text is returned.
9042 This function sets `last-coding-system-used' to the precise coding system
9043 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9044 not fully specified.) */)
9045 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
9047 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
9051 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
9052 Lisp_Object dst_object
, int encodep
, int nocopy
, int norecord
)
9054 struct coding_system coding
;
9055 EMACS_INT chars
, bytes
;
9057 CHECK_STRING (string
);
9058 if (NILP (coding_system
))
9061 Vlast_coding_system_used
= Qno_conversion
;
9062 if (NILP (dst_object
))
9063 return (nocopy
? Fcopy_sequence (string
) : string
);
9066 if (NILP (coding_system
))
9067 coding_system
= Qno_conversion
;
9069 CHECK_CODING_SYSTEM (coding_system
);
9070 if (NILP (dst_object
))
9072 else if (! EQ (dst_object
, Qt
))
9073 CHECK_BUFFER (dst_object
);
9075 setup_coding_system (coding_system
, &coding
);
9076 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9077 chars
= SCHARS (string
);
9078 bytes
= SBYTES (string
);
9080 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9082 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9084 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9086 return (BUFFERP (dst_object
)
9087 ? make_number (coding
.produced_char
)
9088 : coding
.dst_object
);
9092 /* Encode or decode STRING according to CODING_SYSTEM.
9093 Do not set Vlast_coding_system_used.
9095 This function is called only from macros DECODE_FILE and
9096 ENCODE_FILE, thus we ignore character composition. */
9099 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9102 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9106 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9108 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9110 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9111 if the decoding operation is trivial.
9113 Optional fourth arg BUFFER non-nil means that the decoded text is
9114 inserted in that buffer after point (point does not move). In this
9115 case, the return value is the length of the decoded text.
9117 This function sets `last-coding-system-used' to the precise coding system
9118 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9119 not fully specified.) */)
9120 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9122 return code_convert_string (string
, coding_system
, buffer
,
9123 0, ! NILP (nocopy
), 0);
9126 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9128 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9130 Optional third arg NOCOPY non-nil means it is OK to return STRING
9131 itself if the encoding operation is trivial.
9133 Optional fourth arg BUFFER non-nil means that the encoded text is
9134 inserted in that buffer after point (point does not move). In this
9135 case, the return value is the length of the encoded text.
9137 This function sets `last-coding-system-used' to the precise coding system
9138 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9139 not fully specified.) */)
9140 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9142 return code_convert_string (string
, coding_system
, buffer
,
9143 1, ! NILP (nocopy
), 1);
9147 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9148 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9149 Return the corresponding character. */)
9152 Lisp_Object spec
, attrs
, val
;
9153 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9156 CHECK_NATNUM (code
);
9157 c
= XFASTINT (code
);
9158 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9159 attrs
= AREF (spec
, 0);
9161 if (ASCII_BYTE_P (c
)
9162 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9165 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9166 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9167 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9168 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9171 charset
= charset_roman
;
9172 else if (c
>= 0xA0 && c
< 0xDF)
9174 charset
= charset_kana
;
9179 int s1
= c
>> 8, s2
= c
& 0xFF;
9181 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
9182 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
9183 error ("Invalid code: %d", code
);
9185 charset
= charset_kanji
;
9187 c
= DECODE_CHAR (charset
, c
);
9189 error ("Invalid code: %d", code
);
9190 return make_number (c
);
9194 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9195 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9196 Return the corresponding code in SJIS. */)
9199 Lisp_Object spec
, attrs
, charset_list
;
9201 struct charset
*charset
;
9204 CHECK_CHARACTER (ch
);
9206 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9207 attrs
= AREF (spec
, 0);
9209 if (ASCII_CHAR_P (c
)
9210 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9213 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9214 charset
= char_charset (c
, charset_list
, &code
);
9215 if (code
== CHARSET_INVALID_CODE (charset
))
9216 error ("Can't encode by shift_jis encoding: %d", c
);
9219 return make_number (code
);
9222 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9223 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9224 Return the corresponding character. */)
9227 Lisp_Object spec
, attrs
, val
;
9228 struct charset
*charset_roman
, *charset_big5
, *charset
;
9231 CHECK_NATNUM (code
);
9232 c
= XFASTINT (code
);
9233 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9234 attrs
= AREF (spec
, 0);
9236 if (ASCII_BYTE_P (c
)
9237 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9240 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9241 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9242 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9245 charset
= charset_roman
;
9248 int b1
= c
>> 8, b2
= c
& 0x7F;
9249 if (b1
< 0xA1 || b1
> 0xFE
9250 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9251 error ("Invalid code: %d", code
);
9252 charset
= charset_big5
;
9254 c
= DECODE_CHAR (charset
, (unsigned )c
);
9256 error ("Invalid code: %d", code
);
9257 return make_number (c
);
9260 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9261 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9262 Return the corresponding character code in Big5. */)
9265 Lisp_Object spec
, attrs
, charset_list
;
9266 struct charset
*charset
;
9270 CHECK_CHARACTER (ch
);
9272 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9273 attrs
= AREF (spec
, 0);
9274 if (ASCII_CHAR_P (c
)
9275 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9278 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9279 charset
= char_charset (c
, charset_list
, &code
);
9280 if (code
== CHARSET_INVALID_CODE (charset
))
9281 error ("Can't encode by Big5 encoding: %d", c
);
9283 return make_number (code
);
9287 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9288 Sset_terminal_coding_system_internal
, 1, 2, 0,
9289 doc
: /* Internal use only. */)
9290 (Lisp_Object coding_system
, Lisp_Object terminal
)
9292 struct terminal
*term
= get_terminal (terminal
, 1);
9293 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9294 CHECK_SYMBOL (coding_system
);
9295 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9296 /* We had better not send unsafe characters to terminal. */
9297 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9298 /* Character composition should be disabled. */
9299 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9300 terminal_coding
->src_multibyte
= 1;
9301 terminal_coding
->dst_multibyte
= 0;
9302 if (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
)
9303 term
->charset_list
= coding_charset_list (terminal_coding
);
9305 term
->charset_list
= Fcons (make_number (charset_ascii
), Qnil
);
9309 DEFUN ("set-safe-terminal-coding-system-internal",
9310 Fset_safe_terminal_coding_system_internal
,
9311 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9312 doc
: /* Internal use only. */)
9313 (Lisp_Object coding_system
)
9315 CHECK_SYMBOL (coding_system
);
9316 setup_coding_system (Fcheck_coding_system (coding_system
),
9317 &safe_terminal_coding
);
9318 /* Character composition should be disabled. */
9319 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9320 safe_terminal_coding
.src_multibyte
= 1;
9321 safe_terminal_coding
.dst_multibyte
= 0;
9325 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9326 Sterminal_coding_system
, 0, 1, 0,
9327 doc
: /* Return coding system specified for terminal output on the given terminal.
9328 TERMINAL may be a terminal object, a frame, or nil for the selected
9329 frame's terminal device. */)
9330 (Lisp_Object terminal
)
9332 struct coding_system
*terminal_coding
9333 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9334 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9336 /* For backward compatibility, return nil if it is `undecided'. */
9337 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9340 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9341 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9342 doc
: /* Internal use only. */)
9343 (Lisp_Object coding_system
, Lisp_Object terminal
)
9345 struct terminal
*t
= get_terminal (terminal
, 1);
9346 CHECK_SYMBOL (coding_system
);
9347 if (NILP (coding_system
))
9348 coding_system
= Qno_conversion
;
9350 Fcheck_coding_system (coding_system
);
9351 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9352 /* Character composition should be disabled. */
9353 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9354 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9358 DEFUN ("keyboard-coding-system",
9359 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9360 doc
: /* Return coding system specified for decoding keyboard input. */)
9361 (Lisp_Object terminal
)
9363 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9364 (get_terminal (terminal
, 1))->id
);
9368 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9369 Sfind_operation_coding_system
, 1, MANY
, 0,
9370 doc
: /* Choose a coding system for an operation based on the target name.
9371 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9372 DECODING-SYSTEM is the coding system to use for decoding
9373 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9374 for encoding (in case OPERATION does encoding).
9376 The first argument OPERATION specifies an I/O primitive:
9377 For file I/O, `insert-file-contents' or `write-region'.
9378 For process I/O, `call-process', `call-process-region', or `start-process'.
9379 For network I/O, `open-network-stream'.
9381 The remaining arguments should be the same arguments that were passed
9382 to the primitive. Depending on which primitive, one of those arguments
9383 is selected as the TARGET. For example, if OPERATION does file I/O,
9384 whichever argument specifies the file name is TARGET.
9386 TARGET has a meaning which depends on OPERATION:
9387 For file I/O, TARGET is a file name (except for the special case below).
9388 For process I/O, TARGET is a process name.
9389 For network I/O, TARGET is a service name or a port number.
9391 This function looks up what is specified for TARGET in
9392 `file-coding-system-alist', `process-coding-system-alist',
9393 or `network-coding-system-alist' depending on OPERATION.
9394 They may specify a coding system, a cons of coding systems,
9395 or a function symbol to call.
9396 In the last case, we call the function with one argument,
9397 which is a list of all the arguments given to this function.
9398 If the function can't decide a coding system, it can return
9399 `undecided' so that the normal code-detection is performed.
9401 If OPERATION is `insert-file-contents', the argument corresponding to
9402 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9403 file name to look up, and BUFFER is a buffer that contains the file's
9404 contents (not yet decoded). If `file-coding-system-alist' specifies a
9405 function to call for FILENAME, that function should examine the
9406 contents of BUFFER instead of reading the file.
9408 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9409 (int nargs
, Lisp_Object
*args
)
9411 Lisp_Object operation
, target_idx
, target
, val
;
9412 register Lisp_Object chain
;
9415 error ("Too few arguments");
9416 operation
= args
[0];
9417 if (!SYMBOLP (operation
)
9418 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
9419 error ("Invalid first argument");
9420 if (nargs
< 1 + XINT (target_idx
))
9421 error ("Too few arguments for operation: %s",
9422 SDATA (SYMBOL_NAME (operation
)));
9423 target
= args
[XINT (target_idx
) + 1];
9424 if (!(STRINGP (target
)
9425 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9426 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9427 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9428 error ("Invalid %dth argument", XINT (target_idx
) + 1);
9430 target
= XCAR (target
);
9432 chain
= ((EQ (operation
, Qinsert_file_contents
)
9433 || EQ (operation
, Qwrite_region
))
9434 ? Vfile_coding_system_alist
9435 : (EQ (operation
, Qopen_network_stream
)
9436 ? Vnetwork_coding_system_alist
9437 : Vprocess_coding_system_alist
));
9441 for (; CONSP (chain
); chain
= XCDR (chain
))
9447 && ((STRINGP (target
)
9448 && STRINGP (XCAR (elt
))
9449 && fast_string_match (XCAR (elt
), target
) >= 0)
9450 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9453 /* Here, if VAL is both a valid coding system and a valid
9454 function symbol, we return VAL as a coding system. */
9457 if (! SYMBOLP (val
))
9459 if (! NILP (Fcoding_system_p (val
)))
9460 return Fcons (val
, val
);
9461 if (! NILP (Ffboundp (val
)))
9463 /* We use call1 rather than safe_call1
9464 so as to get bug reports about functions called here
9465 which don't handle the current interface. */
9466 val
= call1 (val
, Flist (nargs
, args
));
9469 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9470 return Fcons (val
, val
);
9478 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9479 Sset_coding_system_priority
, 0, MANY
, 0,
9480 doc
: /* Assign higher priority to the coding systems given as arguments.
9481 If multiple coding systems belong to the same category,
9482 all but the first one are ignored.
9484 usage: (set-coding-system-priority &rest coding-systems) */)
9485 (int nargs
, Lisp_Object
*args
)
9488 int changed
[coding_category_max
];
9489 enum coding_category priorities
[coding_category_max
];
9491 memset (changed
, 0, sizeof changed
);
9493 for (i
= j
= 0; i
< nargs
; i
++)
9495 enum coding_category category
;
9496 Lisp_Object spec
, attrs
;
9498 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9499 attrs
= AREF (spec
, 0);
9500 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9501 if (changed
[category
])
9502 /* Ignore this coding system because a coding system of the
9503 same category already had a higher priority. */
9505 changed
[category
] = 1;
9506 priorities
[j
++] = category
;
9507 if (coding_categories
[category
].id
>= 0
9508 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9509 setup_coding_system (args
[i
], &coding_categories
[category
]);
9510 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9513 /* Now we have decided top J priorities. Reflect the order of the
9514 original priorities to the remaining priorities. */
9516 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9518 while (j
< coding_category_max
9519 && changed
[coding_priorities
[j
]])
9521 if (j
== coding_category_max
)
9523 priorities
[i
] = coding_priorities
[j
];
9526 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9528 /* Update `coding-category-list'. */
9529 Vcoding_category_list
= Qnil
;
9530 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9531 Vcoding_category_list
9532 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9533 Vcoding_category_list
);
9538 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9539 Scoding_system_priority_list
, 0, 1, 0,
9540 doc
: /* Return a list of coding systems ordered by their priorities.
9541 The list contains a subset of coding systems; i.e. coding systems
9542 assigned to each coding category (see `coding-category-list').
9544 HIGHESTP non-nil means just return the highest priority one. */)
9545 (Lisp_Object highestp
)
9550 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9552 enum coding_category category
= coding_priorities
[i
];
9553 int id
= coding_categories
[category
].id
;
9558 attrs
= CODING_ID_ATTRS (id
);
9559 if (! NILP (highestp
))
9560 return CODING_ATTR_BASE_NAME (attrs
);
9561 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9563 return Fnreverse (val
);
9566 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9569 make_subsidiaries (Lisp_Object base
)
9571 Lisp_Object subsidiaries
;
9572 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9573 char *buf
= (char *) alloca (base_name_len
+ 6);
9576 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9577 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9578 for (i
= 0; i
< 3; i
++)
9580 memcpy (buf
+ base_name_len
, suffixes
[i
], strlen (suffixes
[i
]) + 1);
9581 ASET (subsidiaries
, i
, intern (buf
));
9583 return subsidiaries
;
9587 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9588 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9589 doc
: /* For internal use only.
9590 usage: (define-coding-system-internal ...) */)
9591 (int nargs
, Lisp_Object
*args
)
9594 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9595 Lisp_Object attrs
; /* Vector of attributes. */
9596 Lisp_Object eol_type
;
9597 Lisp_Object aliases
;
9598 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9599 enum coding_category category
;
9600 Lisp_Object tail
, val
;
9601 int max_charset_id
= 0;
9604 if (nargs
< coding_arg_max
)
9607 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9609 name
= args
[coding_arg_name
];
9610 CHECK_SYMBOL (name
);
9611 CODING_ATTR_BASE_NAME (attrs
) = name
;
9613 val
= args
[coding_arg_mnemonic
];
9614 if (! STRINGP (val
))
9615 CHECK_CHARACTER (val
);
9616 CODING_ATTR_MNEMONIC (attrs
) = val
;
9618 coding_type
= args
[coding_arg_coding_type
];
9619 CHECK_SYMBOL (coding_type
);
9620 CODING_ATTR_TYPE (attrs
) = coding_type
;
9622 charset_list
= args
[coding_arg_charset_list
];
9623 if (SYMBOLP (charset_list
))
9625 if (EQ (charset_list
, Qiso_2022
))
9627 if (! EQ (coding_type
, Qiso_2022
))
9628 error ("Invalid charset-list");
9629 charset_list
= Viso_2022_charset_list
;
9631 else if (EQ (charset_list
, Qemacs_mule
))
9633 if (! EQ (coding_type
, Qemacs_mule
))
9634 error ("Invalid charset-list");
9635 charset_list
= Vemacs_mule_charset_list
;
9637 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9638 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9639 max_charset_id
= XFASTINT (XCAR (tail
));
9643 charset_list
= Fcopy_sequence (charset_list
);
9644 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9646 struct charset
*charset
;
9649 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9650 if (EQ (coding_type
, Qiso_2022
)
9651 ? CHARSET_ISO_FINAL (charset
) < 0
9652 : EQ (coding_type
, Qemacs_mule
)
9653 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9655 error ("Can't handle charset `%s'",
9656 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9658 XSETCAR (tail
, make_number (charset
->id
));
9659 if (max_charset_id
< charset
->id
)
9660 max_charset_id
= charset
->id
;
9663 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9665 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9666 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9667 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9668 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9669 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9671 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9673 val
= args
[coding_arg_decode_translation_table
];
9674 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9676 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9678 val
= args
[coding_arg_encode_translation_table
];
9679 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9681 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9683 val
= args
[coding_arg_post_read_conversion
];
9685 CODING_ATTR_POST_READ (attrs
) = val
;
9687 val
= args
[coding_arg_pre_write_conversion
];
9689 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9691 val
= args
[coding_arg_default_char
];
9693 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9696 CHECK_CHARACTER (val
);
9697 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9700 val
= args
[coding_arg_for_unibyte
];
9701 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9703 val
= args
[coding_arg_plist
];
9705 CODING_ATTR_PLIST (attrs
) = val
;
9707 if (EQ (coding_type
, Qcharset
))
9709 /* Generate a lisp vector of 256 elements. Each element is nil,
9710 integer, or a list of charset IDs.
9712 If Nth element is nil, the byte code N is invalid in this
9715 If Nth element is a number NUM, N is the first byte of a
9716 charset whose ID is NUM.
9718 If Nth element is a list of charset IDs, N is the first byte
9719 of one of them. The list is sorted by dimensions of the
9720 charsets. A charset of smaller dimension comes first. */
9721 val
= Fmake_vector (make_number (256), Qnil
);
9723 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9725 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9726 int dim
= CHARSET_DIMENSION (charset
);
9727 int idx
= (dim
- 1) * 4;
9729 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9730 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9732 for (i
= charset
->code_space
[idx
];
9733 i
<= charset
->code_space
[idx
+ 1]; i
++)
9735 Lisp_Object tmp
, tmp2
;
9738 tmp
= AREF (val
, i
);
9741 else if (NUMBERP (tmp
))
9743 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9745 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9747 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9751 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9753 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9758 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9761 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9762 XSETCAR (tmp2
, XCAR (tail
));
9768 ASET (attrs
, coding_attr_charset_valids
, val
);
9769 category
= coding_category_charset
;
9771 else if (EQ (coding_type
, Qccl
))
9775 if (nargs
< coding_arg_ccl_max
)
9778 val
= args
[coding_arg_ccl_decoder
];
9779 CHECK_CCL_PROGRAM (val
);
9781 val
= Fcopy_sequence (val
);
9782 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9784 val
= args
[coding_arg_ccl_encoder
];
9785 CHECK_CCL_PROGRAM (val
);
9787 val
= Fcopy_sequence (val
);
9788 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9790 val
= args
[coding_arg_ccl_valids
];
9791 valids
= Fmake_string (make_number (256), make_number (0));
9792 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9799 from
= to
= XINT (val
);
9800 if (from
< 0 || from
> 255)
9801 args_out_of_range_3 (val
, make_number (0), make_number (255));
9806 CHECK_NATNUM_CAR (val
);
9807 CHECK_NATNUM_CDR (val
);
9808 from
= XINT (XCAR (val
));
9810 args_out_of_range_3 (XCAR (val
),
9811 make_number (0), make_number (255));
9812 to
= XINT (XCDR (val
));
9813 if (to
< from
|| to
> 255)
9814 args_out_of_range_3 (XCDR (val
),
9815 XCAR (val
), make_number (255));
9817 for (i
= from
; i
<= to
; i
++)
9818 SSET (valids
, i
, 1);
9820 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9822 category
= coding_category_ccl
;
9824 else if (EQ (coding_type
, Qutf_16
))
9826 Lisp_Object bom
, endian
;
9828 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9830 if (nargs
< coding_arg_utf16_max
)
9833 bom
= args
[coding_arg_utf16_bom
];
9834 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9838 CHECK_CODING_SYSTEM (val
);
9840 CHECK_CODING_SYSTEM (val
);
9842 ASET (attrs
, coding_attr_utf_bom
, bom
);
9844 endian
= args
[coding_arg_utf16_endian
];
9845 CHECK_SYMBOL (endian
);
9848 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9849 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9850 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9852 category
= (CONSP (bom
)
9853 ? coding_category_utf_16_auto
9855 ? (EQ (endian
, Qbig
)
9856 ? coding_category_utf_16_be_nosig
9857 : coding_category_utf_16_le_nosig
)
9858 : (EQ (endian
, Qbig
)
9859 ? coding_category_utf_16_be
9860 : coding_category_utf_16_le
));
9862 else if (EQ (coding_type
, Qiso_2022
))
9864 Lisp_Object initial
, reg_usage
, request
, flags
;
9867 if (nargs
< coding_arg_iso2022_max
)
9870 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9871 CHECK_VECTOR (initial
);
9872 for (i
= 0; i
< 4; i
++)
9874 val
= Faref (initial
, make_number (i
));
9877 struct charset
*charset
;
9879 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9880 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9881 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9882 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9885 ASET (initial
, i
, make_number (-1));
9888 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9889 CHECK_CONS (reg_usage
);
9890 CHECK_NUMBER_CAR (reg_usage
);
9891 CHECK_NUMBER_CDR (reg_usage
);
9893 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9894 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9902 CHECK_CHARSET_GET_ID (tmp
, id
);
9903 CHECK_NATNUM_CDR (val
);
9904 if (XINT (XCDR (val
)) >= 4)
9905 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9906 XSETCAR (val
, make_number (id
));
9909 flags
= args
[coding_arg_iso2022_flags
];
9910 CHECK_NATNUM (flags
);
9912 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9913 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9915 ASET (attrs
, coding_attr_iso_initial
, initial
);
9916 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9917 ASET (attrs
, coding_attr_iso_request
, request
);
9918 ASET (attrs
, coding_attr_iso_flags
, flags
);
9919 setup_iso_safe_charsets (attrs
);
9921 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9922 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9923 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9924 ? coding_category_iso_7_else
9925 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9926 ? coding_category_iso_7
9927 : coding_category_iso_7_tight
);
9930 int id
= XINT (AREF (initial
, 1));
9932 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9933 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9935 ? coding_category_iso_8_else
9936 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9937 ? coding_category_iso_8_1
9938 : coding_category_iso_8_2
);
9940 if (category
!= coding_category_iso_8_1
9941 && category
!= coding_category_iso_8_2
)
9942 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9944 else if (EQ (coding_type
, Qemacs_mule
))
9946 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9947 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9948 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9949 category
= coding_category_emacs_mule
;
9951 else if (EQ (coding_type
, Qshift_jis
))
9954 struct charset
*charset
;
9956 if (XINT (Flength (charset_list
)) != 3
9957 && XINT (Flength (charset_list
)) != 4)
9958 error ("There should be three or four charsets");
9960 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9961 if (CHARSET_DIMENSION (charset
) != 1)
9962 error ("Dimension of charset %s is not one",
9963 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9964 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9965 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9967 charset_list
= XCDR (charset_list
);
9968 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9969 if (CHARSET_DIMENSION (charset
) != 1)
9970 error ("Dimension of charset %s is not one",
9971 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9973 charset_list
= XCDR (charset_list
);
9974 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9975 if (CHARSET_DIMENSION (charset
) != 2)
9976 error ("Dimension of charset %s is not two",
9977 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9979 charset_list
= XCDR (charset_list
);
9980 if (! NILP (charset_list
))
9982 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9983 if (CHARSET_DIMENSION (charset
) != 2)
9984 error ("Dimension of charset %s is not two",
9985 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9988 category
= coding_category_sjis
;
9989 Vsjis_coding_system
= name
;
9991 else if (EQ (coding_type
, Qbig5
))
9993 struct charset
*charset
;
9995 if (XINT (Flength (charset_list
)) != 2)
9996 error ("There should be just two charsets");
9998 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9999 if (CHARSET_DIMENSION (charset
) != 1)
10000 error ("Dimension of charset %s is not one",
10001 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10002 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10003 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10005 charset_list
= XCDR (charset_list
);
10006 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10007 if (CHARSET_DIMENSION (charset
) != 2)
10008 error ("Dimension of charset %s is not two",
10009 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10011 category
= coding_category_big5
;
10012 Vbig5_coding_system
= name
;
10014 else if (EQ (coding_type
, Qraw_text
))
10016 category
= coding_category_raw_text
;
10017 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10019 else if (EQ (coding_type
, Qutf_8
))
10023 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10025 if (nargs
< coding_arg_utf8_max
)
10028 bom
= args
[coding_arg_utf8_bom
];
10029 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10033 CHECK_CODING_SYSTEM (val
);
10035 CHECK_CODING_SYSTEM (val
);
10037 ASET (attrs
, coding_attr_utf_bom
, bom
);
10039 category
= (CONSP (bom
) ? coding_category_utf_8_auto
10040 : NILP (bom
) ? coding_category_utf_8_nosig
10041 : coding_category_utf_8_sig
);
10043 else if (EQ (coding_type
, Qundecided
))
10044 category
= coding_category_undecided
;
10046 error ("Invalid coding system type: %s",
10047 SDATA (SYMBOL_NAME (coding_type
)));
10049 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
10050 CODING_ATTR_PLIST (attrs
)
10051 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
10052 CODING_ATTR_PLIST (attrs
)));
10053 CODING_ATTR_PLIST (attrs
)
10054 = Fcons (QCascii_compatible_p
,
10055 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
10056 CODING_ATTR_PLIST (attrs
)));
10058 eol_type
= args
[coding_arg_eol_type
];
10059 if (! NILP (eol_type
)
10060 && ! EQ (eol_type
, Qunix
)
10061 && ! EQ (eol_type
, Qdos
)
10062 && ! EQ (eol_type
, Qmac
))
10063 error ("Invalid eol-type");
10065 aliases
= Fcons (name
, Qnil
);
10067 if (NILP (eol_type
))
10069 eol_type
= make_subsidiaries (name
);
10070 for (i
= 0; i
< 3; i
++)
10072 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10074 this_name
= AREF (eol_type
, i
);
10075 this_aliases
= Fcons (this_name
, Qnil
);
10076 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10077 this_spec
= Fmake_vector (make_number (3), attrs
);
10078 ASET (this_spec
, 1, this_aliases
);
10079 ASET (this_spec
, 2, this_eol_type
);
10080 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10081 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10082 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10084 Vcoding_system_alist
10085 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10086 Vcoding_system_alist
);
10090 spec_vec
= Fmake_vector (make_number (3), attrs
);
10091 ASET (spec_vec
, 1, aliases
);
10092 ASET (spec_vec
, 2, eol_type
);
10094 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10095 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10096 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10098 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10099 Vcoding_system_alist
);
10102 int id
= coding_categories
[category
].id
;
10104 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10105 setup_coding_system (name
, &coding_categories
[category
]);
10111 return Fsignal (Qwrong_number_of_arguments
,
10112 Fcons (intern ("define-coding-system-internal"),
10113 make_number (nargs
)));
10117 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10119 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10120 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10122 Lisp_Object spec
, attrs
;
10124 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10125 attrs
= AREF (spec
, 0);
10126 if (EQ (prop
, QCmnemonic
))
10128 if (! STRINGP (val
))
10129 CHECK_CHARACTER (val
);
10130 CODING_ATTR_MNEMONIC (attrs
) = val
;
10132 else if (EQ (prop
, QCdefault_char
))
10135 val
= make_number (' ');
10137 CHECK_CHARACTER (val
);
10138 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10140 else if (EQ (prop
, QCdecode_translation_table
))
10142 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10143 CHECK_SYMBOL (val
);
10144 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10146 else if (EQ (prop
, QCencode_translation_table
))
10148 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10149 CHECK_SYMBOL (val
);
10150 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10152 else if (EQ (prop
, QCpost_read_conversion
))
10154 CHECK_SYMBOL (val
);
10155 CODING_ATTR_POST_READ (attrs
) = val
;
10157 else if (EQ (prop
, QCpre_write_conversion
))
10159 CHECK_SYMBOL (val
);
10160 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10162 else if (EQ (prop
, QCascii_compatible_p
))
10164 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10167 CODING_ATTR_PLIST (attrs
)
10168 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10173 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10174 Sdefine_coding_system_alias
, 2, 2, 0,
10175 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10176 (Lisp_Object alias
, Lisp_Object coding_system
)
10178 Lisp_Object spec
, aliases
, eol_type
, val
;
10180 CHECK_SYMBOL (alias
);
10181 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10182 aliases
= AREF (spec
, 1);
10183 /* ALIASES should be a list of length more than zero, and the first
10184 element is a base coding system. Append ALIAS at the tail of the
10186 while (!NILP (XCDR (aliases
)))
10187 aliases
= XCDR (aliases
);
10188 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10190 eol_type
= AREF (spec
, 2);
10191 if (VECTORP (eol_type
))
10193 Lisp_Object subsidiaries
;
10196 subsidiaries
= make_subsidiaries (alias
);
10197 for (i
= 0; i
< 3; i
++)
10198 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10199 AREF (eol_type
, i
));
10202 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10203 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10204 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10206 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10207 Vcoding_system_alist
);
10212 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10214 doc
: /* Return the base of CODING-SYSTEM.
10215 Any alias or subsidiary coding system is not a base coding system. */)
10216 (Lisp_Object coding_system
)
10218 Lisp_Object spec
, attrs
;
10220 if (NILP (coding_system
))
10221 return (Qno_conversion
);
10222 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10223 attrs
= AREF (spec
, 0);
10224 return CODING_ATTR_BASE_NAME (attrs
);
10227 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10229 doc
: "Return the property list of CODING-SYSTEM.")
10230 (Lisp_Object coding_system
)
10232 Lisp_Object spec
, attrs
;
10234 if (NILP (coding_system
))
10235 coding_system
= Qno_conversion
;
10236 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10237 attrs
= AREF (spec
, 0);
10238 return CODING_ATTR_PLIST (attrs
);
10242 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10244 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10245 (Lisp_Object coding_system
)
10249 if (NILP (coding_system
))
10250 coding_system
= Qno_conversion
;
10251 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10252 return AREF (spec
, 1);
10255 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10256 Scoding_system_eol_type
, 1, 1, 0,
10257 doc
: /* Return eol-type of CODING-SYSTEM.
10258 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10260 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10261 and CR respectively.
10263 A vector value indicates that a format of end-of-line should be
10264 detected automatically. Nth element of the vector is the subsidiary
10265 coding system whose eol-type is N. */)
10266 (Lisp_Object coding_system
)
10268 Lisp_Object spec
, eol_type
;
10271 if (NILP (coding_system
))
10272 coding_system
= Qno_conversion
;
10273 if (! CODING_SYSTEM_P (coding_system
))
10275 spec
= CODING_SYSTEM_SPEC (coding_system
);
10276 eol_type
= AREF (spec
, 2);
10277 if (VECTORP (eol_type
))
10278 return Fcopy_sequence (eol_type
);
10279 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10280 return make_number (n
);
10286 /*** 9. Post-amble ***/
10289 init_coding_once (void)
10293 for (i
= 0; i
< coding_category_max
; i
++)
10295 coding_categories
[i
].id
= -1;
10296 coding_priorities
[i
] = i
;
10299 /* ISO2022 specific initialize routine. */
10300 for (i
= 0; i
< 0x20; i
++)
10301 iso_code_class
[i
] = ISO_control_0
;
10302 for (i
= 0x21; i
< 0x7F; i
++)
10303 iso_code_class
[i
] = ISO_graphic_plane_0
;
10304 for (i
= 0x80; i
< 0xA0; i
++)
10305 iso_code_class
[i
] = ISO_control_1
;
10306 for (i
= 0xA1; i
< 0xFF; i
++)
10307 iso_code_class
[i
] = ISO_graphic_plane_1
;
10308 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10309 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10310 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10311 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10312 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10313 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10314 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10315 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10316 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10318 for (i
= 0; i
< 256; i
++)
10320 emacs_mule_bytes
[i
] = 1;
10322 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10323 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10324 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10325 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10331 syms_of_coding (void)
10333 staticpro (&Vcoding_system_hash_table
);
10335 Lisp_Object args
[2];
10338 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10341 staticpro (&Vsjis_coding_system
);
10342 Vsjis_coding_system
= Qnil
;
10344 staticpro (&Vbig5_coding_system
);
10345 Vbig5_coding_system
= Qnil
;
10347 staticpro (&Vcode_conversion_reused_workbuf
);
10348 Vcode_conversion_reused_workbuf
= Qnil
;
10350 staticpro (&Vcode_conversion_workbuf_name
);
10351 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10353 reused_workbuf_in_use
= 0;
10355 DEFSYM (Qcharset
, "charset");
10356 DEFSYM (Qtarget_idx
, "target-idx");
10357 DEFSYM (Qcoding_system_history
, "coding-system-history");
10358 Fset (Qcoding_system_history
, Qnil
);
10360 /* Target FILENAME is the first argument. */
10361 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10362 /* Target FILENAME is the third argument. */
10363 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10365 DEFSYM (Qcall_process
, "call-process");
10366 /* Target PROGRAM is the first argument. */
10367 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10369 DEFSYM (Qcall_process_region
, "call-process-region");
10370 /* Target PROGRAM is the third argument. */
10371 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10373 DEFSYM (Qstart_process
, "start-process");
10374 /* Target PROGRAM is the third argument. */
10375 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10377 DEFSYM (Qopen_network_stream
, "open-network-stream");
10378 /* Target SERVICE is the fourth argument. */
10379 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10381 DEFSYM (Qcoding_system
, "coding-system");
10382 DEFSYM (Qcoding_aliases
, "coding-aliases");
10384 DEFSYM (Qeol_type
, "eol-type");
10385 DEFSYM (Qunix
, "unix");
10386 DEFSYM (Qdos
, "dos");
10388 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10389 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10390 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10391 DEFSYM (Qdefault_char
, "default-char");
10392 DEFSYM (Qundecided
, "undecided");
10393 DEFSYM (Qno_conversion
, "no-conversion");
10394 DEFSYM (Qraw_text
, "raw-text");
10396 DEFSYM (Qiso_2022
, "iso-2022");
10398 DEFSYM (Qutf_8
, "utf-8");
10399 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10401 DEFSYM (Qutf_16
, "utf-16");
10402 DEFSYM (Qbig
, "big");
10403 DEFSYM (Qlittle
, "little");
10405 DEFSYM (Qshift_jis
, "shift-jis");
10406 DEFSYM (Qbig5
, "big5");
10408 DEFSYM (Qcoding_system_p
, "coding-system-p");
10410 DEFSYM (Qcoding_system_error
, "coding-system-error");
10411 Fput (Qcoding_system_error
, Qerror_conditions
,
10412 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10413 Fput (Qcoding_system_error
, Qerror_message
,
10414 make_pure_c_string ("Invalid coding system"));
10416 /* Intern this now in case it isn't already done.
10417 Setting this variable twice is harmless.
10418 But don't staticpro it here--that is done in alloc.c. */
10419 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10421 DEFSYM (Qtranslation_table
, "translation-table");
10422 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10423 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10424 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10425 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10427 DEFSYM (Qvalid_codes
, "valid-codes");
10429 DEFSYM (Qemacs_mule
, "emacs-mule");
10431 DEFSYM (QCcategory
, ":category");
10432 DEFSYM (QCmnemonic
, ":mnemonic");
10433 DEFSYM (QCdefault_char
, ":default-char");
10434 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10435 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10436 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10437 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10438 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10440 Vcoding_category_table
10441 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10442 staticpro (&Vcoding_category_table
);
10443 /* Followings are target of code detection. */
10444 ASET (Vcoding_category_table
, coding_category_iso_7
,
10445 intern_c_string ("coding-category-iso-7"));
10446 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10447 intern_c_string ("coding-category-iso-7-tight"));
10448 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10449 intern_c_string ("coding-category-iso-8-1"));
10450 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10451 intern_c_string ("coding-category-iso-8-2"));
10452 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10453 intern_c_string ("coding-category-iso-7-else"));
10454 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10455 intern_c_string ("coding-category-iso-8-else"));
10456 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10457 intern_c_string ("coding-category-utf-8-auto"));
10458 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10459 intern_c_string ("coding-category-utf-8"));
10460 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10461 intern_c_string ("coding-category-utf-8-sig"));
10462 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10463 intern_c_string ("coding-category-utf-16-be"));
10464 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10465 intern_c_string ("coding-category-utf-16-auto"));
10466 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10467 intern_c_string ("coding-category-utf-16-le"));
10468 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10469 intern_c_string ("coding-category-utf-16-be-nosig"));
10470 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10471 intern_c_string ("coding-category-utf-16-le-nosig"));
10472 ASET (Vcoding_category_table
, coding_category_charset
,
10473 intern_c_string ("coding-category-charset"));
10474 ASET (Vcoding_category_table
, coding_category_sjis
,
10475 intern_c_string ("coding-category-sjis"));
10476 ASET (Vcoding_category_table
, coding_category_big5
,
10477 intern_c_string ("coding-category-big5"));
10478 ASET (Vcoding_category_table
, coding_category_ccl
,
10479 intern_c_string ("coding-category-ccl"));
10480 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10481 intern_c_string ("coding-category-emacs-mule"));
10482 /* Followings are NOT target of code detection. */
10483 ASET (Vcoding_category_table
, coding_category_raw_text
,
10484 intern_c_string ("coding-category-raw-text"));
10485 ASET (Vcoding_category_table
, coding_category_undecided
,
10486 intern_c_string ("coding-category-undecided"));
10488 DEFSYM (Qinsufficient_source
, "insufficient-source");
10489 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10490 DEFSYM (Qinvalid_source
, "invalid-source");
10491 DEFSYM (Qinterrupted
, "interrupted");
10492 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10493 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10495 defsubr (&Scoding_system_p
);
10496 defsubr (&Sread_coding_system
);
10497 defsubr (&Sread_non_nil_coding_system
);
10498 defsubr (&Scheck_coding_system
);
10499 defsubr (&Sdetect_coding_region
);
10500 defsubr (&Sdetect_coding_string
);
10501 defsubr (&Sfind_coding_systems_region_internal
);
10502 defsubr (&Sunencodable_char_position
);
10503 defsubr (&Scheck_coding_systems_region
);
10504 defsubr (&Sdecode_coding_region
);
10505 defsubr (&Sencode_coding_region
);
10506 defsubr (&Sdecode_coding_string
);
10507 defsubr (&Sencode_coding_string
);
10508 defsubr (&Sdecode_sjis_char
);
10509 defsubr (&Sencode_sjis_char
);
10510 defsubr (&Sdecode_big5_char
);
10511 defsubr (&Sencode_big5_char
);
10512 defsubr (&Sset_terminal_coding_system_internal
);
10513 defsubr (&Sset_safe_terminal_coding_system_internal
);
10514 defsubr (&Sterminal_coding_system
);
10515 defsubr (&Sset_keyboard_coding_system_internal
);
10516 defsubr (&Skeyboard_coding_system
);
10517 defsubr (&Sfind_operation_coding_system
);
10518 defsubr (&Sset_coding_system_priority
);
10519 defsubr (&Sdefine_coding_system_internal
);
10520 defsubr (&Sdefine_coding_system_alias
);
10521 defsubr (&Scoding_system_put
);
10522 defsubr (&Scoding_system_base
);
10523 defsubr (&Scoding_system_plist
);
10524 defsubr (&Scoding_system_aliases
);
10525 defsubr (&Scoding_system_eol_type
);
10526 defsubr (&Scoding_system_priority_list
);
10528 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
10529 doc
: /* List of coding systems.
10531 Do not alter the value of this variable manually. This variable should be
10532 updated by the functions `define-coding-system' and
10533 `define-coding-system-alias'. */);
10534 Vcoding_system_list
= Qnil
;
10536 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
10537 doc
: /* Alist of coding system names.
10538 Each element is one element list of coding system name.
10539 This variable is given to `completing-read' as COLLECTION argument.
10541 Do not alter the value of this variable manually. This variable should be
10542 updated by the functions `make-coding-system' and
10543 `define-coding-system-alias'. */);
10544 Vcoding_system_alist
= Qnil
;
10546 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
10547 doc
: /* List of coding-categories (symbols) ordered by priority.
10549 On detecting a coding system, Emacs tries code detection algorithms
10550 associated with each coding-category one by one in this order. When
10551 one algorithm agrees with a byte sequence of source text, the coding
10552 system bound to the corresponding coding-category is selected.
10554 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10558 Vcoding_category_list
= Qnil
;
10559 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10560 Vcoding_category_list
10561 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10562 Vcoding_category_list
);
10565 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
10566 doc
: /* Specify the coding system for read operations.
10567 It is useful to bind this variable with `let', but do not set it globally.
10568 If the value is a coding system, it is used for decoding on read operation.
10569 If not, an appropriate element is used from one of the coding system alists.
10570 There are three such tables: `file-coding-system-alist',
10571 `process-coding-system-alist', and `network-coding-system-alist'. */);
10572 Vcoding_system_for_read
= Qnil
;
10574 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
10575 doc
: /* Specify the coding system for write operations.
10576 Programs bind this variable with `let', but you should not set it globally.
10577 If the value is a coding system, it is used for encoding of output,
10578 when writing it to a file and when sending it to a file or subprocess.
10580 If this does not specify a coding system, an appropriate element
10581 is used from one of the coding system alists.
10582 There are three such tables: `file-coding-system-alist',
10583 `process-coding-system-alist', and `network-coding-system-alist'.
10584 For output to files, if the above procedure does not specify a coding system,
10585 the value of `buffer-file-coding-system' is used. */);
10586 Vcoding_system_for_write
= Qnil
;
10588 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
10590 Coding system used in the latest file or process I/O. */);
10591 Vlast_coding_system_used
= Qnil
;
10593 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
10595 Error status of the last code conversion.
10597 When an error was detected in the last code conversion, this variable
10598 is set to one of the following symbols.
10599 `insufficient-source'
10603 `insufficient-memory'
10604 When no error was detected, the value doesn't change. So, to check
10605 the error status of a code conversion by this variable, you must
10606 explicitly set this variable to nil before performing code
10608 Vlast_code_conversion_error
= Qnil
;
10610 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
10612 *Non-nil means always inhibit code conversion of end-of-line format.
10613 See info node `Coding Systems' and info node `Text and Binary' concerning
10614 such conversion. */);
10615 inhibit_eol_conversion
= 0;
10617 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
10619 Non-nil means process buffer inherits coding system of process output.
10620 Bind it to t if the process output is to be treated as if it were a file
10621 read from some filesystem. */);
10622 inherit_process_coding_system
= 0;
10624 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
10626 Alist to decide a coding system to use for a file I/O operation.
10627 The format is ((PATTERN . VAL) ...),
10628 where PATTERN is a regular expression matching a file name,
10629 VAL is a coding system, a cons of coding systems, or a function symbol.
10630 If VAL is a coding system, it is used for both decoding and encoding
10632 If VAL is a cons of coding systems, the car part is used for decoding,
10633 and the cdr part is used for encoding.
10634 If VAL is a function symbol, the function must return a coding system
10635 or a cons of coding systems which are used as above. The function is
10636 called with an argument that is a list of the arguments with which
10637 `find-operation-coding-system' was called. If the function can't decide
10638 a coding system, it can return `undecided' so that the normal
10639 code-detection is performed.
10641 See also the function `find-operation-coding-system'
10642 and the variable `auto-coding-alist'. */);
10643 Vfile_coding_system_alist
= Qnil
;
10645 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
10647 Alist to decide a coding system to use for a process I/O operation.
10648 The format is ((PATTERN . VAL) ...),
10649 where PATTERN is a regular expression matching a program name,
10650 VAL is a coding system, a cons of coding systems, or a function symbol.
10651 If VAL is a coding system, it is used for both decoding what received
10652 from the program and encoding what sent to the program.
10653 If VAL is a cons of coding systems, the car part is used for decoding,
10654 and the cdr part is used for encoding.
10655 If VAL is a function symbol, the function must return a coding system
10656 or a cons of coding systems which are used as above.
10658 See also the function `find-operation-coding-system'. */);
10659 Vprocess_coding_system_alist
= Qnil
;
10661 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
10663 Alist to decide a coding system to use for a network I/O operation.
10664 The format is ((PATTERN . VAL) ...),
10665 where PATTERN is a regular expression matching a network service name
10666 or is a port number to connect to,
10667 VAL is a coding system, a cons of coding systems, or a function symbol.
10668 If VAL is a coding system, it is used for both decoding what received
10669 from the network stream and encoding what sent to the network stream.
10670 If VAL is a cons of coding systems, the car part is used for decoding,
10671 and the cdr part is used for encoding.
10672 If VAL is a function symbol, the function must return a coding system
10673 or a cons of coding systems which are used as above.
10675 See also the function `find-operation-coding-system'. */);
10676 Vnetwork_coding_system_alist
= Qnil
;
10678 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
10679 doc
: /* Coding system to use with system messages.
10680 Also used for decoding keyboard input on X Window system. */);
10681 Vlocale_coding_system
= Qnil
;
10683 /* The eol mnemonics are reset in startup.el system-dependently. */
10684 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
10686 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10687 eol_mnemonic_unix
= make_pure_c_string (":");
10689 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
10691 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10692 eol_mnemonic_dos
= make_pure_c_string ("\\");
10694 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
10696 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10697 eol_mnemonic_mac
= make_pure_c_string ("/");
10699 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
10701 *String displayed in mode line when end-of-line format is not yet determined. */);
10702 eol_mnemonic_undecided
= make_pure_c_string (":");
10704 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
10706 *Non-nil enables character translation while encoding and decoding. */);
10707 Venable_character_translation
= Qt
;
10709 DEFVAR_LISP ("standard-translation-table-for-decode",
10710 &Vstandard_translation_table_for_decode
,
10711 doc
: /* Table for translating characters while decoding. */);
10712 Vstandard_translation_table_for_decode
= Qnil
;
10714 DEFVAR_LISP ("standard-translation-table-for-encode",
10715 &Vstandard_translation_table_for_encode
,
10716 doc
: /* Table for translating characters while encoding. */);
10717 Vstandard_translation_table_for_encode
= Qnil
;
10719 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
10720 doc
: /* Alist of charsets vs revision numbers.
10721 While encoding, if a charset (car part of an element) is found,
10722 designate it with the escape sequence identifying revision (cdr part
10723 of the element). */);
10724 Vcharset_revision_table
= Qnil
;
10726 DEFVAR_LISP ("default-process-coding-system",
10727 &Vdefault_process_coding_system
,
10728 doc
: /* Cons of coding systems used for process I/O by default.
10729 The car part is used for decoding a process output,
10730 the cdr part is used for encoding a text to be sent to a process. */);
10731 Vdefault_process_coding_system
= Qnil
;
10733 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
10735 Table of extra Latin codes in the range 128..159 (inclusive).
10736 This is a vector of length 256.
10737 If Nth element is non-nil, the existence of code N in a file
10738 \(or output of subprocess) doesn't prevent it to be detected as
10739 a coding system of ISO 2022 variant which has a flag
10740 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10741 or reading output of a subprocess.
10742 Only 128th through 159th elements have a meaning. */);
10743 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10745 DEFVAR_LISP ("select-safe-coding-system-function",
10746 &Vselect_safe_coding_system_function
,
10748 Function to call to select safe coding system for encoding a text.
10750 If set, this function is called to force a user to select a proper
10751 coding system which can encode the text in the case that a default
10752 coding system used in each operation can't encode the text. The
10753 function should take care that the buffer is not modified while
10754 the coding system is being selected.
10756 The default value is `select-safe-coding-system' (which see). */);
10757 Vselect_safe_coding_system_function
= Qnil
;
10759 DEFVAR_BOOL ("coding-system-require-warning",
10760 &coding_system_require_warning
,
10761 doc
: /* Internal use only.
10762 If non-nil, on writing a file, `select-safe-coding-system-function' is
10763 called even if `coding-system-for-write' is non-nil. The command
10764 `universal-coding-system-argument' binds this variable to t temporarily. */);
10765 coding_system_require_warning
= 0;
10768 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10769 &inhibit_iso_escape_detection
,
10771 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10773 When Emacs reads text, it tries to detect how the text is encoded.
10774 This code detection is sensitive to escape sequences. If Emacs sees
10775 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10776 of the ISO2022 encodings, and decodes text by the corresponding coding
10777 system (e.g. `iso-2022-7bit').
10779 However, there may be a case that you want to read escape sequences in
10780 a file as is. In such a case, you can set this variable to non-nil.
10781 Then the code detection will ignore any escape sequences, and no text is
10782 detected as encoded in some ISO-2022 encoding. The result is that all
10783 escape sequences become visible in a buffer.
10785 The default value is nil, and it is strongly recommended not to change
10786 it. That is because many Emacs Lisp source files that contain
10787 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10788 in Emacs's distribution, and they won't be decoded correctly on
10789 reading if you suppress escape sequence detection.
10791 The other way to read escape sequences in a file without decoding is
10792 to explicitly specify some coding system that doesn't use ISO-2022
10793 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10794 inhibit_iso_escape_detection
= 0;
10796 DEFVAR_BOOL ("inhibit-null-byte-detection",
10797 &inhibit_null_byte_detection
,
10798 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10799 By default, Emacs treats it as binary data, and does not attempt to
10800 decode it. The effect is as if you specified `no-conversion' for
10803 Set this to non-nil when a regular text happens to include null bytes.
10804 Examples are Index nodes of Info files and null-byte delimited output
10805 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10806 decode text as usual. */);
10807 inhibit_null_byte_detection
= 0;
10809 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
10810 doc
: /* Char table for translating self-inserting characters.
10811 This is applied to the result of input methods, not their input.
10812 See also `keyboard-translate-table'.
10814 Use of this variable for character code unification was rendered
10815 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10816 internal character representation. */);
10817 Vtranslation_table_for_input
= Qnil
;
10820 Lisp_Object args
[coding_arg_max
];
10821 Lisp_Object plist
[16];
10824 for (i
= 0; i
< coding_arg_max
; i
++)
10827 plist
[0] = intern_c_string (":name");
10828 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10829 plist
[2] = intern_c_string (":mnemonic");
10830 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10831 plist
[4] = intern_c_string (":coding-type");
10832 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10833 plist
[6] = intern_c_string (":ascii-compatible-p");
10834 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10835 plist
[8] = intern_c_string (":default-char");
10836 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10837 plist
[10] = intern_c_string (":for-unibyte");
10838 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10839 plist
[12] = intern_c_string (":docstring");
10840 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10842 When you visit a file with this coding, the file is read into a\n\
10843 unibyte buffer as is, thus each byte of a file is treated as a\n\
10845 plist
[14] = intern_c_string (":eol-type");
10846 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10847 args
[coding_arg_plist
] = Flist (16, plist
);
10848 Fdefine_coding_system_internal (coding_arg_max
, args
);
10850 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10851 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10852 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10853 /* This is already set.
10854 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10855 plist
[8] = intern_c_string (":charset-list");
10856 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10857 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10858 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10859 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10860 args
[coding_arg_plist
] = Flist (16, plist
);
10861 Fdefine_coding_system_internal (coding_arg_max
, args
);
10864 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10869 for (i
= 0; i
< coding_category_max
; i
++)
10870 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10872 #if defined (DOS_NT)
10873 system_eol_type
= Qdos
;
10875 system_eol_type
= Qunix
;
10877 staticpro (&system_eol_type
);
10881 emacs_strerror (int error_number
)
10885 synchronize_system_messages_locale ();
10886 str
= strerror (error_number
);
10888 if (! NILP (Vlocale_coding_system
))
10890 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10891 Vlocale_coding_system
,
10893 str
= (char *) SDATA (dec
);