1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
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 2, or (at your option)
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; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301, USA. */
29 /*** TABLE OF CONTENTS ***
33 2. Emacs' internal format (emacs-utf-8) handlers
36 5. Charset-base coding systems handlers
37 6. emacs-mule (old Emacs' internal format) handlers
39 8. Shift-JIS and BIG5 handlers
41 10. C library functions
42 11. Emacs Lisp library functions
47 /*** 0. General comments ***
52 A coding system is an object for an encoding mechanism that contains
53 information about how to convert byte sequences to character
54 sequences and vice versa. When we say "decode", it means converting
55 a byte sequence of a specific coding system into a character
56 sequence that is represented by Emacs' internal coding system
57 `emacs-utf-8', and when we say "encode", it means converting a
58 character sequence of emacs-utf-8 to a byte sequence of a specific
61 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
62 C level, a coding system is represented by a vector of attributes
63 stored in the hash table Vcharset_hash_table. The conversion from
64 coding system symbol to attributes vector is done by looking up
65 Vcharset_hash_table by the symbol.
67 Coding systems are classified into the following types depending on
68 the encoding mechanism. Here's a brief description of the types.
74 o Charset-base coding system
76 A coding system defined by one or more (coded) character sets.
77 Decoding and encoding are done by a code converter defined for each
80 o Old Emacs internal format (emacs-mule)
82 The coding system adopted by old versions of Emacs (20 and 21).
84 o ISO2022-base coding system
86 The most famous coding system for multiple character sets. X's
87 Compound Text, various EUCs (Extended Unix Code), and coding systems
88 used in the Internet communication such as ISO-2022-JP are all
91 o SJIS (or Shift-JIS or MS-Kanji-Code)
93 A coding system to encode character sets: ASCII, JISX0201, and
94 JISX0208. Widely used for PC's in Japan. Details are described in
99 A coding system to encode character sets: ASCII and Big5. Widely
100 used for Chinese (mainly in Taiwan and Hong Kong). Details are
101 described in section 8. In this file, when we write "big5" (all
102 lowercase), we mean the coding system, and when we write "Big5"
103 (capitalized), we mean the character set.
107 If a user wants to decode/encode text encoded in a coding system
108 not listed above, he can supply a decoder and an encoder for it in
109 CCL (Code Conversion Language) programs. Emacs executes the CCL
110 program while decoding/encoding.
114 A coding system for text containing raw eight-bit data. Emacs
115 treats each byte of source text as a character (except for
116 end-of-line conversion).
120 Like raw text, but don't do end-of-line conversion.
125 How text end-of-line is encoded depends on operating system. For
126 instance, Unix's format is just one byte of LF (line-feed) code,
127 whereas DOS's format is two-byte sequence of `carriage-return' and
128 `line-feed' codes. MacOS's format is usually one byte of
131 Since text character encoding and end-of-line encoding are
132 independent, any coding system described above can take any format
133 of end-of-line (except for no-conversion).
137 Before using a coding system for code conversion (i.e. decoding and
138 encoding), we setup a structure of type `struct coding_system'.
139 This structure keeps various information about a specific code
140 conversion (e.g. the location of source and destination data).
147 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
149 These functions check if a byte sequence specified as a source in
150 CODING conforms to the format of XXX, and update the members of
153 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
155 Below is the template of these functions. */
159 detect_coding_XXX (coding
, detect_info
)
160 struct coding_system
*coding
;
161 struct coding_detection_info
*detect_info
;
163 const unsigned char *src
= coding
->source
;
164 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
165 int multibytep
= coding
->src_multibyte
;
166 int consumed_chars
= 0;
172 /* Get one byte from the source. If the souce is exausted, jump
173 to no_more_source:. */
176 if (! __C_conforms_to_XXX___ (c
))
178 if (! __C_strongly_suggests_XXX__ (c
))
179 found
= CATEGORY_MASK_XXX
;
181 /* The byte sequence is invalid for XXX. */
182 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
186 /* The source exausted successfully. */
187 detect_info
->found
|= found
;
192 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
194 These functions decode a byte sequence specified as a source by
195 CODING. The resulting multibyte text goes to a place pointed to by
196 CODING->charbuf, the length of which should not exceed
197 CODING->charbuf_size;
199 These functions set the information of original and decoded texts in
200 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
201 They also set CODING->result to one of CODING_RESULT_XXX indicating
202 how the decoding is finished.
204 Below is the template of these functions. */
208 decode_coding_XXXX (coding
)
209 struct coding_system
*coding
;
211 const unsigned char *src
= coding
->source
+ coding
->consumed
;
212 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
213 /* SRC_BASE remembers the start position in source in each loop.
214 The loop will be exited when there's not enough source code, or
215 when there's no room in CHARBUF for a decoded character. */
216 const unsigned char *src_base
;
217 /* A buffer to produce decoded characters. */
218 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
219 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
220 int multibytep
= coding
->src_multibyte
;
225 if (charbuf
< charbuf_end
)
226 /* No more room to produce a decoded character. */
233 if (src_base
< src_end
234 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
235 /* If the source ends by partial bytes to construct a character,
236 treat them as eight-bit raw data. */
237 while (src_base
< src_end
&& charbuf
< charbuf_end
)
238 *charbuf
++ = *src_base
++;
239 /* Remember how many bytes and characters we consumed. If the
240 source is multibyte, the bytes and chars are not identical. */
241 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
242 /* Remember how many characters we produced. */
243 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
247 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
249 These functions encode SRC_BYTES length text at SOURCE of Emacs'
250 internal multibyte format by CODING. The resulting byte sequence
251 goes to a place pointed to by DESTINATION, the length of which
252 should not exceed DST_BYTES.
254 These functions set the information of original and encoded texts in
255 the members produced, produced_char, consumed, and consumed_char of
256 the structure *CODING. They also set the member result to one of
257 CODING_RESULT_XXX indicating how the encoding finished.
259 DST_BYTES zero means that source area and destination area are
260 overlapped, which means that we can produce a encoded text until it
261 reaches at the head of not-yet-encoded source text.
263 Below is a template of these functions. */
266 encode_coding_XXX (coding
)
267 struct coding_system
*coding
;
269 int multibytep
= coding
->dst_multibyte
;
270 int *charbuf
= coding
->charbuf
;
271 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
272 unsigned char *dst
= coding
->destination
+ coding
->produced
;
273 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
274 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
275 int produced_chars
= 0;
277 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
280 /* Encode C into DST, and increment DST. */
282 label_no_more_destination
:
283 /* How many chars and bytes we produced. */
284 coding
->produced_char
+= produced_chars
;
285 coding
->produced
= dst
- coding
->destination
;
290 /*** 1. Preamble ***/
297 #include "character.h"
300 #include "composite.h"
304 Lisp_Object Vcoding_system_hash_table
;
306 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
307 Lisp_Object Qunix
, Qdos
;
308 extern Lisp_Object Qmac
; /* frame.c */
309 Lisp_Object Qbuffer_file_coding_system
;
310 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
311 Lisp_Object Qdefault_char
;
312 Lisp_Object Qno_conversion
, Qundecided
;
313 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
314 Lisp_Object Qbig
, Qlittle
;
315 Lisp_Object Qcoding_system_history
;
316 Lisp_Object Qvalid_codes
;
317 Lisp_Object QCcategory
, QCmnemonic
, QCdefalut_char
;
318 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
319 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
320 Lisp_Object QCascii_compatible_p
;
322 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
323 Lisp_Object Qcall_process
, Qcall_process_region
;
324 Lisp_Object Qstart_process
, Qopen_network_stream
;
325 Lisp_Object Qtarget_idx
;
327 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
328 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
330 /* If a symbol has this property, evaluate the value to define the
331 symbol as a coding system. */
332 static Lisp_Object Qcoding_system_define_form
;
334 int coding_system_require_warning
;
336 Lisp_Object Vselect_safe_coding_system_function
;
338 /* Mnemonic string for each format of end-of-line. */
339 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
340 /* Mnemonic string to indicate format of end-of-line is not yet
342 Lisp_Object eol_mnemonic_undecided
;
344 /* Format of end-of-line decided by system. This is Qunix on
345 Unix and Mac, Qdos on DOS/Windows.
346 This has an effect only for external encoding (i.e. for output to
347 file and process), not for in-buffer or Lisp string encoding. */
348 static Lisp_Object system_eol_type
;
352 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
354 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
356 /* Coding system emacs-mule and raw-text are for converting only
357 end-of-line format. */
358 Lisp_Object Qemacs_mule
, Qraw_text
;
359 Lisp_Object Qutf_8_emacs
;
361 /* Coding-systems are handed between Emacs Lisp programs and C internal
362 routines by the following three variables. */
363 /* Coding-system for reading files and receiving data from process. */
364 Lisp_Object Vcoding_system_for_read
;
365 /* Coding-system for writing files and sending data to process. */
366 Lisp_Object Vcoding_system_for_write
;
367 /* Coding-system actually used in the latest I/O. */
368 Lisp_Object Vlast_coding_system_used
;
369 /* Set to non-nil when an error is detected while code conversion. */
370 Lisp_Object Vlast_code_conversion_error
;
371 /* A vector of length 256 which contains information about special
372 Latin codes (especially for dealing with Microsoft codes). */
373 Lisp_Object Vlatin_extra_code_table
;
375 /* Flag to inhibit code conversion of end-of-line format. */
376 int inhibit_eol_conversion
;
378 /* Flag to inhibit ISO2022 escape sequence detection. */
379 int inhibit_iso_escape_detection
;
381 /* Flag to make buffer-file-coding-system inherit from process-coding. */
382 int inherit_process_coding_system
;
384 /* Coding system to be used to encode text for terminal display. */
385 struct coding_system terminal_coding
;
387 /* Coding system to be used to encode text for terminal display when
388 terminal coding system is nil. */
389 struct coding_system safe_terminal_coding
;
391 /* Coding system of what is sent from terminal keyboard. */
392 struct coding_system keyboard_coding
;
394 Lisp_Object Vfile_coding_system_alist
;
395 Lisp_Object Vprocess_coding_system_alist
;
396 Lisp_Object Vnetwork_coding_system_alist
;
398 Lisp_Object Vlocale_coding_system
;
402 /* Flag to tell if we look up translation table on character code
404 Lisp_Object Venable_character_translation
;
405 /* Standard translation table to look up on decoding (reading). */
406 Lisp_Object Vstandard_translation_table_for_decode
;
407 /* Standard translation table to look up on encoding (writing). */
408 Lisp_Object Vstandard_translation_table_for_encode
;
410 Lisp_Object Qtranslation_table
;
411 Lisp_Object Qtranslation_table_id
;
412 Lisp_Object Qtranslation_table_for_decode
;
413 Lisp_Object Qtranslation_table_for_encode
;
415 /* Alist of charsets vs revision number. */
416 static Lisp_Object Vcharset_revision_table
;
418 /* Default coding systems used for process I/O. */
419 Lisp_Object Vdefault_process_coding_system
;
421 /* Char table for translating Quail and self-inserting input. */
422 Lisp_Object Vtranslation_table_for_input
;
424 /* Two special coding systems. */
425 Lisp_Object Vsjis_coding_system
;
426 Lisp_Object Vbig5_coding_system
;
428 /* ISO2022 section */
430 #define CODING_ISO_INITIAL(coding, reg) \
431 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
432 coding_attr_iso_initial), \
436 #define CODING_ISO_REQUEST(coding, charset_id) \
437 ((charset_id <= (coding)->max_charset_id \
438 ? (coding)->safe_charsets[charset_id] \
442 #define CODING_ISO_FLAGS(coding) \
443 ((coding)->spec.iso_2022.flags)
444 #define CODING_ISO_DESIGNATION(coding, reg) \
445 ((coding)->spec.iso_2022.current_designation[reg])
446 #define CODING_ISO_INVOCATION(coding, plane) \
447 ((coding)->spec.iso_2022.current_invocation[plane])
448 #define CODING_ISO_SINGLE_SHIFTING(coding) \
449 ((coding)->spec.iso_2022.single_shifting)
450 #define CODING_ISO_BOL(coding) \
451 ((coding)->spec.iso_2022.bol)
452 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
453 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
455 /* Control characters of ISO2022. */
456 /* code */ /* function */
457 #define ISO_CODE_LF 0x0A /* line-feed */
458 #define ISO_CODE_CR 0x0D /* carriage-return */
459 #define ISO_CODE_SO 0x0E /* shift-out */
460 #define ISO_CODE_SI 0x0F /* shift-in */
461 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
462 #define ISO_CODE_ESC 0x1B /* escape */
463 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
464 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
465 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
467 /* All code (1-byte) of ISO2022 is classified into one of the
469 enum iso_code_class_type
471 ISO_control_0
, /* Control codes in the range
472 0x00..0x1F and 0x7F, except for the
473 following 5 codes. */
474 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
475 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
476 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
477 ISO_escape
, /* ISO_CODE_SO (0x1B) */
478 ISO_control_1
, /* Control codes in the range
479 0x80..0x9F, except for the
480 following 3 codes. */
481 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
482 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
483 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
484 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
485 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
486 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
487 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
490 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
491 `iso-flags' attribute of an iso2022 coding system. */
493 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
494 instead of the correct short-form sequence (e.g. ESC $ A). */
495 #define CODING_ISO_FLAG_LONG_FORM 0x0001
497 /* If set, reset graphic planes and registers at end-of-line to the
499 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
501 /* If set, reset graphic planes and registers before any control
502 characters to the initial state. */
503 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
505 /* If set, encode by 7-bit environment. */
506 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
508 /* If set, use locking-shift function. */
509 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
511 /* If set, use single-shift function. Overwrite
512 CODING_ISO_FLAG_LOCKING_SHIFT. */
513 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
515 /* If set, use designation escape sequence. */
516 #define CODING_ISO_FLAG_DESIGNATION 0x0040
518 /* If set, produce revision number sequence. */
519 #define CODING_ISO_FLAG_REVISION 0x0080
521 /* If set, produce ISO6429's direction specifying sequence. */
522 #define CODING_ISO_FLAG_DIRECTION 0x0100
524 /* If set, assume designation states are reset at beginning of line on
526 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
528 /* If set, designation sequence should be placed at beginning of line
530 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
532 /* If set, do not encode unsafe charactes on output. */
533 #define CODING_ISO_FLAG_SAFE 0x0800
535 /* If set, extra latin codes (128..159) are accepted as a valid code
537 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
539 #define CODING_ISO_FLAG_COMPOSITION 0x2000
541 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
543 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
545 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
547 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
549 /* A character to be produced on output if encoding of the original
550 character is prohibited by CODING_ISO_FLAG_SAFE. */
551 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
555 #define CODING_UTF_16_BOM(coding) \
556 ((coding)->spec.utf_16.bom)
558 #define CODING_UTF_16_ENDIAN(coding) \
559 ((coding)->spec.utf_16.endian)
561 #define CODING_UTF_16_SURROGATE(coding) \
562 ((coding)->spec.utf_16.surrogate)
566 #define CODING_CCL_DECODER(coding) \
567 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
568 #define CODING_CCL_ENCODER(coding) \
569 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
570 #define CODING_CCL_VALIDS(coding) \
571 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
573 /* Index for each coding category in `coding_categories' */
577 coding_category_iso_7
,
578 coding_category_iso_7_tight
,
579 coding_category_iso_8_1
,
580 coding_category_iso_8_2
,
581 coding_category_iso_7_else
,
582 coding_category_iso_8_else
,
583 coding_category_utf_8
,
584 coding_category_utf_16_auto
,
585 coding_category_utf_16_be
,
586 coding_category_utf_16_le
,
587 coding_category_utf_16_be_nosig
,
588 coding_category_utf_16_le_nosig
,
589 coding_category_charset
,
590 coding_category_sjis
,
591 coding_category_big5
,
593 coding_category_emacs_mule
,
594 /* All above are targets of code detection. */
595 coding_category_raw_text
,
596 coding_category_undecided
,
600 /* Definitions of flag bits used in detect_coding_XXXX. */
601 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
602 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
603 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
604 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
605 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
606 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
607 #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
608 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
609 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
610 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
611 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
612 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
613 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
614 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
615 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
616 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
617 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
618 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
620 /* This value is returned if detect_coding_mask () find nothing other
621 than ASCII characters. */
622 #define CATEGORY_MASK_ANY \
623 (CATEGORY_MASK_ISO_7 \
624 | CATEGORY_MASK_ISO_7_TIGHT \
625 | CATEGORY_MASK_ISO_8_1 \
626 | CATEGORY_MASK_ISO_8_2 \
627 | CATEGORY_MASK_ISO_7_ELSE \
628 | CATEGORY_MASK_ISO_8_ELSE \
629 | CATEGORY_MASK_UTF_8 \
630 | CATEGORY_MASK_UTF_16_BE \
631 | CATEGORY_MASK_UTF_16_LE \
632 | CATEGORY_MASK_UTF_16_BE_NOSIG \
633 | CATEGORY_MASK_UTF_16_LE_NOSIG \
634 | CATEGORY_MASK_CHARSET \
635 | CATEGORY_MASK_SJIS \
636 | CATEGORY_MASK_BIG5 \
637 | CATEGORY_MASK_CCL \
638 | CATEGORY_MASK_EMACS_MULE)
641 #define CATEGORY_MASK_ISO_7BIT \
642 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
644 #define CATEGORY_MASK_ISO_8BIT \
645 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
647 #define CATEGORY_MASK_ISO_ELSE \
648 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
650 #define CATEGORY_MASK_ISO_ESCAPE \
651 (CATEGORY_MASK_ISO_7 \
652 | CATEGORY_MASK_ISO_7_TIGHT \
653 | CATEGORY_MASK_ISO_7_ELSE \
654 | CATEGORY_MASK_ISO_8_ELSE)
656 #define CATEGORY_MASK_ISO \
657 ( CATEGORY_MASK_ISO_7BIT \
658 | CATEGORY_MASK_ISO_8BIT \
659 | CATEGORY_MASK_ISO_ELSE)
661 #define CATEGORY_MASK_UTF_16 \
662 (CATEGORY_MASK_UTF_16_BE \
663 | CATEGORY_MASK_UTF_16_LE \
664 | CATEGORY_MASK_UTF_16_BE_NOSIG \
665 | CATEGORY_MASK_UTF_16_LE_NOSIG)
668 /* List of symbols `coding-category-xxx' ordered by priority. This
669 variable is exposed to Emacs Lisp. */
670 static Lisp_Object Vcoding_category_list
;
672 /* Table of coding categories (Lisp symbols). This variable is for
674 static Lisp_Object Vcoding_category_table
;
676 /* Table of coding-categories ordered by priority. */
677 static enum coding_category coding_priorities
[coding_category_max
];
679 /* Nth element is a coding context for the coding system bound to the
680 Nth coding category. */
681 static struct coding_system coding_categories
[coding_category_max
];
683 /*** Commonly used macros and functions ***/
686 #define min(a, b) ((a) < (b) ? (a) : (b))
689 #define max(a, b) ((a) > (b) ? (a) : (b))
692 #define CODING_GET_INFO(coding, attrs, charset_list) \
694 (attrs) = CODING_ID_ATTRS ((coding)->id); \
695 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
699 /* Safely get one byte from the source text pointed by SRC which ends
700 at SRC_END, and set C to that byte. If there are not enough bytes
701 in the source, it jumps to `no_more_source'. If multibytep is
702 nonzero, and a multibyte character is found at SRC, set C to the
703 negative value of the character code. The caller should declare
704 and set these variables appropriately in advance:
705 src, src_end, multibytep */
707 #define ONE_MORE_BYTE(c) \
709 if (src == src_end) \
711 if (src_base < src) \
712 record_conversion_result \
713 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
714 goto no_more_source; \
717 if (multibytep && (c & 0x80)) \
719 if ((c & 0xFE) == 0xC0) \
720 c = ((c & 1) << 6) | *src++; \
724 c = - string_char (src, &src, NULL); \
725 record_conversion_result \
726 (coding, CODING_RESULT_INVALID_SRC); \
733 #define ONE_MORE_BYTE_NO_CHECK(c) \
736 if (multibytep && (c & 0x80)) \
738 if ((c & 0xFE) == 0xC0) \
739 c = ((c & 1) << 6) | *src++; \
743 c = - string_char (src, &src, NULL); \
744 record_conversion_result \
745 (coding, CODING_RESULT_INVALID_SRC); \
752 /* Store a byte C in the place pointed by DST and increment DST to the
753 next free point, and increment PRODUCED_CHARS. The caller should
754 assure that C is 0..127, and declare and set the variable `dst'
755 appropriately in advance.
759 #define EMIT_ONE_ASCII_BYTE(c) \
766 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
768 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
770 produced_chars += 2; \
771 *dst++ = (c1), *dst++ = (c2); \
775 /* Store a byte C in the place pointed by DST and increment DST to the
776 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
777 nonzero, store in an appropriate multibyte from. The caller should
778 declare and set the variables `dst' and `multibytep' appropriately
781 #define EMIT_ONE_BYTE(c) \
788 ch = BYTE8_TO_CHAR (ch); \
789 CHAR_STRING_ADVANCE (ch, dst); \
796 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
798 #define EMIT_TWO_BYTES(c1, c2) \
800 produced_chars += 2; \
807 ch = BYTE8_TO_CHAR (ch); \
808 CHAR_STRING_ADVANCE (ch, dst); \
811 ch = BYTE8_TO_CHAR (ch); \
812 CHAR_STRING_ADVANCE (ch, dst); \
822 #define EMIT_THREE_BYTES(c1, c2, c3) \
824 EMIT_ONE_BYTE (c1); \
825 EMIT_TWO_BYTES (c2, c3); \
829 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
831 EMIT_TWO_BYTES (c1, c2); \
832 EMIT_TWO_BYTES (c3, c4); \
836 /* Prototypes for static functions. */
837 static void record_conversion_result
P_ ((struct coding_system
*coding
,
838 enum coding_result_code result
));
839 static int detect_coding_utf_8
P_ ((struct coding_system
*,
840 struct coding_detection_info
*info
));
841 static void decode_coding_utf_8
P_ ((struct coding_system
*));
842 static int encode_coding_utf_8
P_ ((struct coding_system
*));
844 static int detect_coding_utf_16
P_ ((struct coding_system
*,
845 struct coding_detection_info
*info
));
846 static void decode_coding_utf_16
P_ ((struct coding_system
*));
847 static int encode_coding_utf_16
P_ ((struct coding_system
*));
849 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
850 struct coding_detection_info
*info
));
851 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
852 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
854 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
855 struct coding_detection_info
*info
));
856 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
857 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
859 static int detect_coding_sjis
P_ ((struct coding_system
*,
860 struct coding_detection_info
*info
));
861 static void decode_coding_sjis
P_ ((struct coding_system
*));
862 static int encode_coding_sjis
P_ ((struct coding_system
*));
864 static int detect_coding_big5
P_ ((struct coding_system
*,
865 struct coding_detection_info
*info
));
866 static void decode_coding_big5
P_ ((struct coding_system
*));
867 static int encode_coding_big5
P_ ((struct coding_system
*));
869 static int detect_coding_ccl
P_ ((struct coding_system
*,
870 struct coding_detection_info
*info
));
871 static void decode_coding_ccl
P_ ((struct coding_system
*));
872 static int encode_coding_ccl
P_ ((struct coding_system
*));
874 static void decode_coding_raw_text
P_ ((struct coding_system
*));
875 static int encode_coding_raw_text
P_ ((struct coding_system
*));
877 static void coding_set_source
P_ ((struct coding_system
*));
878 static void coding_set_destination
P_ ((struct coding_system
*));
879 static void coding_alloc_by_realloc
P_ ((struct coding_system
*, EMACS_INT
));
880 static void coding_alloc_by_making_gap
P_ ((struct coding_system
*,
881 EMACS_INT
, EMACS_INT
));
882 static unsigned char *alloc_destination
P_ ((struct coding_system
*,
883 EMACS_INT
, unsigned char *));
884 static void setup_iso_safe_charsets
P_ ((Lisp_Object
));
885 static unsigned char *encode_designation_at_bol
P_ ((struct coding_system
*,
888 static int detect_eol
P_ ((const unsigned char *,
889 EMACS_INT
, enum coding_category
));
890 static Lisp_Object adjust_coding_eol_type
P_ ((struct coding_system
*, int));
891 static void decode_eol
P_ ((struct coding_system
*));
892 static Lisp_Object get_translation_table
P_ ((Lisp_Object
, int, int *));
893 static Lisp_Object get_translation
P_ ((Lisp_Object
, int *, int *,
895 static int produce_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
896 static INLINE
void produce_composition
P_ ((struct coding_system
*, int *,
898 static INLINE
void produce_charset
P_ ((struct coding_system
*, int *,
900 static void produce_annotation
P_ ((struct coding_system
*, EMACS_INT
));
901 static int decode_coding
P_ ((struct coding_system
*));
902 static INLINE
int *handle_composition_annotation
P_ ((EMACS_INT
, EMACS_INT
,
903 struct coding_system
*,
904 int *, EMACS_INT
*));
905 static INLINE
int *handle_charset_annotation
P_ ((EMACS_INT
, EMACS_INT
,
906 struct coding_system
*,
907 int *, EMACS_INT
*));
908 static void consume_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
909 static int encode_coding
P_ ((struct coding_system
*));
910 static Lisp_Object make_conversion_work_buffer
P_ ((int));
911 static Lisp_Object code_conversion_restore
P_ ((Lisp_Object
));
912 static INLINE
int char_encodable_p
P_ ((int, Lisp_Object
));
913 static Lisp_Object make_subsidiaries
P_ ((Lisp_Object
));
916 record_conversion_result (struct coding_system
*coding
,
917 enum coding_result_code result
)
919 coding
->result
= result
;
922 case CODING_RESULT_INSUFFICIENT_SRC
:
923 Vlast_code_conversion_error
= Qinsufficient_source
;
925 case CODING_RESULT_INCONSISTENT_EOL
:
926 Vlast_code_conversion_error
= Qinconsistent_eol
;
928 case CODING_RESULT_INVALID_SRC
:
929 Vlast_code_conversion_error
= Qinvalid_source
;
931 case CODING_RESULT_INTERRUPT
:
932 Vlast_code_conversion_error
= Qinterrupted
;
934 case CODING_RESULT_INSUFFICIENT_MEM
:
935 Vlast_code_conversion_error
= Qinsufficient_memory
;
938 Vlast_code_conversion_error
= intern ("Unknown error");
942 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
944 charset_map_loaded = 0; \
945 c = DECODE_CHAR (charset, code); \
946 if (charset_map_loaded) \
948 const unsigned char *orig = coding->source; \
951 coding_set_source (coding); \
952 offset = coding->source - orig; \
954 src_base += offset; \
960 #define ASSURE_DESTINATION(bytes) \
962 if (dst + (bytes) >= dst_end) \
964 int more_bytes = charbuf_end - charbuf + (bytes); \
966 dst = alloc_destination (coding, more_bytes, dst); \
967 dst_end = coding->destination + coding->dst_bytes; \
974 coding_set_source (coding
)
975 struct coding_system
*coding
;
977 if (BUFFERP (coding
->src_object
))
979 struct buffer
*buf
= XBUFFER (coding
->src_object
);
981 if (coding
->src_pos
< 0)
982 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
984 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
986 else if (STRINGP (coding
->src_object
))
988 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
991 /* Otherwise, the source is C string and is never relocated
992 automatically. Thus we don't have to update anything. */
997 coding_set_destination (coding
)
998 struct coding_system
*coding
;
1000 if (BUFFERP (coding
->dst_object
))
1002 if (coding
->src_pos
< 0)
1004 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- 1;
1005 coding
->dst_bytes
= (GAP_END_ADDR
1006 - (coding
->src_bytes
- coding
->consumed
)
1007 - coding
->destination
);
1011 /* We are sure that coding->dst_pos_byte is before the gap
1013 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1014 + coding
->dst_pos_byte
- 1);
1015 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1016 - coding
->destination
);
1020 /* Otherwise, the destination is C string and is never relocated
1021 automatically. Thus we don't have to update anything. */
1027 coding_alloc_by_realloc (coding
, bytes
)
1028 struct coding_system
*coding
;
1031 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1032 coding
->dst_bytes
+ bytes
);
1033 coding
->dst_bytes
+= bytes
;
1037 coding_alloc_by_making_gap (coding
, offset
, bytes
)
1038 struct coding_system
*coding
;
1039 EMACS_INT offset
, bytes
;
1041 if (BUFFERP (coding
->dst_object
)
1042 && EQ (coding
->src_object
, coding
->dst_object
))
1044 EMACS_INT add
= offset
+ (coding
->src_bytes
- coding
->consumed
);
1046 GPT
+= offset
, GPT_BYTE
+= offset
;
1047 GAP_SIZE
-= add
; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1049 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1050 GPT
-= offset
, GPT_BYTE
-= offset
;
1054 Lisp_Object this_buffer
;
1056 this_buffer
= Fcurrent_buffer ();
1057 set_buffer_internal (XBUFFER (coding
->dst_object
));
1059 set_buffer_internal (XBUFFER (this_buffer
));
1064 static unsigned char *
1065 alloc_destination (coding
, nbytes
, dst
)
1066 struct coding_system
*coding
;
1070 EMACS_INT offset
= dst
- coding
->destination
;
1072 if (BUFFERP (coding
->dst_object
))
1073 coding_alloc_by_making_gap (coding
, offset
, nbytes
);
1075 coding_alloc_by_realloc (coding
, nbytes
);
1076 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1077 coding_set_destination (coding
);
1078 dst
= coding
->destination
+ offset
;
1082 /** Macros for annotations. */
1084 /* Maximum length of annotation data (sum of annotations for
1085 composition and charset). */
1086 #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1088 /* An annotation data is stored in the array coding->charbuf in this
1090 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1091 LENGTH is the number of elements in the annotation.
1092 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1093 NCHARS is the number of characters in the text annotated.
1095 The format of the following elements depend on ANNOTATION_MASK.
1097 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1099 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1100 METHOD is one of enum composition_method.
1101 Optionnal COMPOSITION-COMPONENTS are characters and composition
1104 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1107 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1109 *(buf)++ = -(len); \
1110 *(buf)++ = (mask); \
1111 *(buf)++ = (nchars); \
1112 coding->annotated = 1; \
1115 #define ADD_COMPOSITION_DATA(buf, nchars, method) \
1117 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1122 #define ADD_CHARSET_DATA(buf, nchars, id) \
1124 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1129 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1136 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1137 Check if a text is encoded in UTF-8. If it is, return 1, else
1140 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1141 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1142 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1143 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1144 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1145 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1148 detect_coding_utf_8 (coding
, detect_info
)
1149 struct coding_system
*coding
;
1150 struct coding_detection_info
*detect_info
;
1152 const unsigned char *src
= coding
->source
, *src_base
;
1153 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1154 int multibytep
= coding
->src_multibyte
;
1155 int consumed_chars
= 0;
1158 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1159 /* A coding system of this category is always ASCII compatible. */
1160 src
+= coding
->head_ascii
;
1164 int c
, c1
, c2
, c3
, c4
;
1168 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1171 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1173 if (UTF_8_2_OCTET_LEADING_P (c
))
1175 found
= CATEGORY_MASK_UTF_8
;
1179 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1181 if (UTF_8_3_OCTET_LEADING_P (c
))
1183 found
= CATEGORY_MASK_UTF_8
;
1187 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1189 if (UTF_8_4_OCTET_LEADING_P (c
))
1191 found
= CATEGORY_MASK_UTF_8
;
1195 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1197 if (UTF_8_5_OCTET_LEADING_P (c
))
1199 found
= CATEGORY_MASK_UTF_8
;
1204 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1208 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1210 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1213 detect_info
->found
|= found
;
1219 decode_coding_utf_8 (coding
)
1220 struct coding_system
*coding
;
1222 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1223 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1224 const unsigned char *src_base
;
1225 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1226 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1227 int consumed_chars
= 0, consumed_chars_base
;
1228 int multibytep
= coding
->src_multibyte
;
1229 Lisp_Object attr
, charset_list
;
1231 CODING_GET_INFO (coding
, attr
, charset_list
);
1235 int c
, c1
, c2
, c3
, c4
, c5
;
1238 consumed_chars_base
= consumed_chars
;
1240 if (charbuf
>= charbuf_end
)
1248 else if (UTF_8_1_OCTET_P(c1
))
1255 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1257 if (UTF_8_2_OCTET_LEADING_P (c1
))
1259 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1260 /* Reject overlong sequences here and below. Encoders
1261 producing them are incorrect, they can be misleading,
1262 and they mess up read/write invariance. */
1269 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1271 if (UTF_8_3_OCTET_LEADING_P (c1
))
1273 c
= (((c1
& 0xF) << 12)
1274 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1276 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1282 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1284 if (UTF_8_4_OCTET_LEADING_P (c1
))
1286 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1287 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1294 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1296 if (UTF_8_5_OCTET_LEADING_P (c1
))
1298 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1299 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1301 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1316 consumed_chars
= consumed_chars_base
;
1318 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1323 coding
->consumed_char
+= consumed_chars_base
;
1324 coding
->consumed
= src_base
- coding
->source
;
1325 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1330 encode_coding_utf_8 (coding
)
1331 struct coding_system
*coding
;
1333 int multibytep
= coding
->dst_multibyte
;
1334 int *charbuf
= coding
->charbuf
;
1335 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1336 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1337 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1338 int produced_chars
= 0;
1343 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1345 while (charbuf
< charbuf_end
)
1347 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1349 ASSURE_DESTINATION (safe_room
);
1351 if (CHAR_BYTE8_P (c
))
1353 c
= CHAR_TO_BYTE8 (c
);
1358 CHAR_STRING_ADVANCE (c
, pend
);
1359 for (p
= str
; p
< pend
; p
++)
1366 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1368 while (charbuf
< charbuf_end
)
1370 ASSURE_DESTINATION (safe_room
);
1372 if (CHAR_BYTE8_P (c
))
1373 *dst
++ = CHAR_TO_BYTE8 (c
);
1375 dst
+= CHAR_STRING (c
, dst
);
1379 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1380 coding
->produced_char
+= produced_chars
;
1381 coding
->produced
= dst
- coding
->destination
;
1386 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1387 Check if a text is encoded in one of UTF-16 based coding systems.
1388 If it is, return 1, else return 0. */
1390 #define UTF_16_HIGH_SURROGATE_P(val) \
1391 (((val) & 0xFC00) == 0xD800)
1393 #define UTF_16_LOW_SURROGATE_P(val) \
1394 (((val) & 0xFC00) == 0xDC00)
1396 #define UTF_16_INVALID_P(val) \
1397 (((val) == 0xFFFE) \
1398 || ((val) == 0xFFFF) \
1399 || UTF_16_LOW_SURROGATE_P (val))
1403 detect_coding_utf_16 (coding
, detect_info
)
1404 struct coding_system
*coding
;
1405 struct coding_detection_info
*detect_info
;
1407 const unsigned char *src
= coding
->source
, *src_base
= src
;
1408 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1409 int multibytep
= coding
->src_multibyte
;
1410 int consumed_chars
= 0;
1413 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1414 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1415 && (coding
->src_chars
& 1))
1417 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1423 if ((c1
== 0xFF) && (c2
== 0xFE))
1425 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1426 | CATEGORY_MASK_UTF_16_AUTO
);
1427 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1428 | CATEGORY_MASK_UTF_16_BE_NOSIG
1429 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1431 else if ((c1
== 0xFE) && (c2
== 0xFF))
1433 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1434 | CATEGORY_MASK_UTF_16_AUTO
);
1435 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1436 | CATEGORY_MASK_UTF_16_BE_NOSIG
1437 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1439 else if (c1
>= 0 && c2
>= 0)
1441 detect_info
->rejected
1442 |= (CATEGORY_MASK_UTF_16_BE
| CATEGORY_MASK_UTF_16_LE
);
1449 decode_coding_utf_16 (coding
)
1450 struct coding_system
*coding
;
1452 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1453 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1454 const unsigned char *src_base
;
1455 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1456 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1457 int consumed_chars
= 0, consumed_chars_base
;
1458 int multibytep
= coding
->src_multibyte
;
1459 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1460 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1461 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1462 Lisp_Object attr
, charset_list
;
1464 CODING_GET_INFO (coding
, attr
, charset_list
);
1466 if (bom
== utf_16_with_bom
)
1475 if (endian
== utf_16_big_endian
1476 ? c
!= 0xFEFF : c
!= 0xFFFE)
1478 /* The first two bytes are not BOM. Treat them as bytes
1479 for a normal character. */
1483 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1485 else if (bom
== utf_16_detect_bom
)
1487 /* We have already tried to detect BOM and failed in
1489 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1497 consumed_chars_base
= consumed_chars
;
1499 if (charbuf
+ 2 >= charbuf_end
)
1511 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1515 c
= (endian
== utf_16_big_endian
1516 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1519 if (! UTF_16_LOW_SURROGATE_P (c
))
1521 if (endian
== utf_16_big_endian
)
1522 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1524 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1528 if (UTF_16_HIGH_SURROGATE_P (c
))
1529 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1535 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1536 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1537 *charbuf
++ = 0x10000 + c
;
1542 if (UTF_16_HIGH_SURROGATE_P (c
))
1543 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1550 coding
->consumed_char
+= consumed_chars_base
;
1551 coding
->consumed
= src_base
- coding
->source
;
1552 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1556 encode_coding_utf_16 (coding
)
1557 struct coding_system
*coding
;
1559 int multibytep
= coding
->dst_multibyte
;
1560 int *charbuf
= coding
->charbuf
;
1561 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1562 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1563 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1565 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1566 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1567 int produced_chars
= 0;
1568 Lisp_Object attrs
, charset_list
;
1571 CODING_GET_INFO (coding
, attrs
, charset_list
);
1573 if (bom
!= utf_16_without_bom
)
1575 ASSURE_DESTINATION (safe_room
);
1577 EMIT_TWO_BYTES (0xFE, 0xFF);
1579 EMIT_TWO_BYTES (0xFF, 0xFE);
1580 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1583 while (charbuf
< charbuf_end
)
1585 ASSURE_DESTINATION (safe_room
);
1587 if (c
>= MAX_UNICODE_CHAR
)
1588 c
= coding
->default_char
;
1593 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1595 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1602 c1
= (c
>> 10) + 0xD800;
1603 c2
= (c
& 0x3FF) + 0xDC00;
1605 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1607 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1610 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1611 coding
->produced
= dst
- coding
->destination
;
1612 coding
->produced_char
+= produced_chars
;
1617 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1619 /* Emacs' internal format for representation of multiple character
1620 sets is a kind of multi-byte encoding, i.e. characters are
1621 represented by variable-length sequences of one-byte codes.
1623 ASCII characters and control characters (e.g. `tab', `newline') are
1624 represented by one-byte sequences which are their ASCII codes, in
1625 the range 0x00 through 0x7F.
1627 8-bit characters of the range 0x80..0x9F are represented by
1628 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1631 8-bit characters of the range 0xA0..0xFF are represented by
1632 one-byte sequences which are their 8-bit code.
1634 The other characters are represented by a sequence of `base
1635 leading-code', optional `extended leading-code', and one or two
1636 `position-code's. The length of the sequence is determined by the
1637 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1638 whereas extended leading-code and position-code take the range 0xA0
1639 through 0xFF. See `charset.h' for more details about leading-code
1642 --- CODE RANGE of Emacs' internal format ---
1646 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1647 eight-bit-graphic 0xA0..0xBF
1648 ELSE 0x81..0x9D + [0xA0..0xFF]+
1649 ---------------------------------------------
1651 As this is the internal character representation, the format is
1652 usually not used externally (i.e. in a file or in a data sent to a
1653 process). But, it is possible to have a text externally in this
1654 format (i.e. by encoding by the coding system `emacs-mule').
1656 In that case, a sequence of one-byte codes has a slightly different
1659 At first, all characters in eight-bit-control are represented by
1660 one-byte sequences which are their 8-bit code.
1662 Next, character composition data are represented by the byte
1663 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1665 METHOD is 0xF0 plus one of composition method (enum
1666 composition_method),
1668 BYTES is 0xA0 plus a byte length of this composition data,
1670 CHARS is 0x20 plus a number of characters composed by this
1673 COMPONENTs are characters of multibye form or composition
1674 rules encoded by two-byte of ASCII codes.
1676 In addition, for backward compatibility, the following formats are
1677 also recognized as composition data on decoding.
1680 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1683 MSEQ is a multibyte form but in these special format:
1684 ASCII: 0xA0 ASCII_CODE+0x80,
1685 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1686 RULE is a one byte code of the range 0xA0..0xF0 that
1687 represents a composition rule.
1690 char emacs_mule_bytes
[256];
1693 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
)
1694 struct coding_system
*coding
;
1695 const unsigned char *src
;
1696 int *nbytes
, *nchars
, *id
;
1698 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1699 const unsigned char *src_base
= src
;
1700 int multibytep
= coding
->src_multibyte
;
1701 struct charset
*charset
;
1704 int consumed_chars
= 0;
1710 charset
= emacs_mule_charset
[0];
1716 /* Old style component character of a compostion. */
1726 switch (emacs_mule_bytes
[c
])
1729 if (! (charset
= emacs_mule_charset
[c
]))
1738 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1739 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1742 if (c
< 0xA0 || ! (charset
= emacs_mule_charset
[c
]))
1751 if (! (charset
= emacs_mule_charset
[c
]))
1756 code
= (c
& 0x7F) << 8;
1766 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
1771 code
= (c
& 0x7F) << 8;
1780 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
1781 ? charset_ascii
: charset_eight_bit
);
1787 c
= DECODE_CHAR (charset
, code
);
1791 *nbytes
= src
- src_base
;
1792 *nchars
= consumed_chars
;
1805 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1806 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1810 detect_coding_emacs_mule (coding
, detect_info
)
1811 struct coding_system
*coding
;
1812 struct coding_detection_info
*detect_info
;
1814 const unsigned char *src
= coding
->source
, *src_base
;
1815 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1816 int multibytep
= coding
->src_multibyte
;
1817 int consumed_chars
= 0;
1821 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1822 /* A coding system of this category is always ASCII compatible. */
1823 src
+= coding
->head_ascii
;
1833 /* Perhaps the start of composite character. We simple skip
1834 it because analyzing it is too heavy for detecting. But,
1835 at least, we check that the composite character
1836 constitues of more than 4 bytes. */
1837 const unsigned char *src_base
;
1847 if (src
- src_base
<= 4)
1849 found
= CATEGORY_MASK_EMACS_MULE
;
1857 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1862 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
1864 while (more_bytes
> 0)
1869 src
--; /* Unread the last byte. */
1874 if (more_bytes
!= 0)
1876 found
= CATEGORY_MASK_EMACS_MULE
;
1879 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1883 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1885 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1888 detect_info
->found
|= found
;
1893 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1895 /* Decode a character represented as a component of composition
1896 sequence of Emacs 20/21 style at SRC. Set C to that character and
1897 update SRC to the head of next character (or an encoded composition
1898 rule). If SRC doesn't points a composition component, set C to -1.
1899 If SRC points an invalid byte sequence, global exit by a return
1902 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
1906 int nbytes, nchars; \
1908 if (src == src_end) \
1910 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
1915 goto invalid_code; \
1919 consumed_chars += nchars; \
1924 /* Decode a composition rule represented as a component of composition
1925 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
1926 and increment BUF. If SRC points an invalid byte sequence, set C
1929 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
1931 int c, gref, nref; \
1933 if (src >= src_end) \
1934 goto invalid_code; \
1935 ONE_MORE_BYTE_NO_CHECK (c); \
1937 if (c < 0 || c >= 81) \
1938 goto invalid_code; \
1940 gref = c / 9, nref = c % 9; \
1941 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1945 /* Decode a composition rule represented as a component of composition
1946 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
1947 and increment BUF. If SRC points an invalid byte sequence, set C
1950 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
1954 if (src + 1>= src_end) \
1955 goto invalid_code; \
1956 ONE_MORE_BYTE_NO_CHECK (gref); \
1958 ONE_MORE_BYTE_NO_CHECK (nref); \
1960 if (gref < 0 || gref >= 81 \
1961 || nref < 0 || nref >= 81) \
1962 goto invalid_code; \
1963 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1967 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
1969 /* Emacs 21 style format. The first three bytes at SRC are \
1970 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
1971 the byte length of this composition information, CHARS is the \
1972 number of characters composed by this composition. */ \
1973 enum composition_method method = c - 0xF2; \
1974 int *charbuf_base = charbuf; \
1975 int consumed_chars_limit; \
1976 int nbytes, nchars; \
1978 ONE_MORE_BYTE (c); \
1980 goto invalid_code; \
1981 nbytes = c - 0xA0; \
1983 goto invalid_code; \
1984 ONE_MORE_BYTE (c); \
1986 goto invalid_code; \
1987 nchars = c - 0xA0; \
1988 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
1989 consumed_chars_limit = consumed_chars_base + nbytes; \
1990 if (method != COMPOSITION_RELATIVE) \
1993 while (consumed_chars < consumed_chars_limit) \
1995 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
1996 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
1998 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
2001 if (consumed_chars < consumed_chars_limit) \
2002 goto invalid_code; \
2003 charbuf_base[0] -= i; \
2008 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
2010 /* Emacs 20 style format for relative composition. */ \
2011 /* Store multibyte form of characters to be composed. */ \
2012 enum composition_method method = COMPOSITION_RELATIVE; \
2013 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2014 int *buf = components; \
2018 ONE_MORE_BYTE (c); /* skip 0x80 */ \
2019 for (i = 0; *src >= 0xA0 && i < MAX_COMPOSITION_COMPONENTS; i++) \
2020 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2022 goto invalid_code; \
2023 ADD_COMPOSITION_DATA (charbuf, i, method); \
2024 for (j = 0; j < i; j++) \
2025 *charbuf++ = components[j]; \
2029 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
2031 /* Emacs 20 style format for rule-base composition. */ \
2032 /* Store multibyte form of characters to be composed. */ \
2033 enum composition_method method = COMPOSITION_WITH_RULE; \
2034 int *charbuf_base = charbuf; \
2035 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2036 int *buf = components; \
2039 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2040 for (i = 1; i < MAX_COMPOSITION_COMPONENTS; i++) \
2044 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
2045 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2047 if (i <= 1 || (buf - components) % 2 == 0) \
2048 goto invalid_code; \
2049 if (charbuf + i + (i / 2) + 1 >= charbuf_end) \
2050 goto no_more_source; \
2051 ADD_COMPOSITION_DATA (charbuf, i, method); \
2053 for (j = 0; j < i; j++) \
2054 *charbuf++ = components[j]; \
2055 charbuf_base[0] -= i; \
2056 for (j = 0; j < i; j += 2) \
2057 *charbuf++ = components[j]; \
2062 decode_coding_emacs_mule (coding
)
2063 struct coding_system
*coding
;
2065 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2066 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2067 const unsigned char *src_base
;
2068 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2070 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
2071 int consumed_chars
= 0, consumed_chars_base
;
2072 int multibytep
= coding
->src_multibyte
;
2073 Lisp_Object attrs
, charset_list
;
2074 int char_offset
= coding
->produced_char
;
2075 int last_offset
= char_offset
;
2076 int last_id
= charset_ascii
;
2078 CODING_GET_INFO (coding
, attrs
, charset_list
);
2085 consumed_chars_base
= consumed_chars
;
2087 if (charbuf
>= charbuf_end
)
2106 if (c
- 0xF2 >= COMPOSITION_RELATIVE
2107 && c
- 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS
)
2108 DECODE_EMACS_MULE_21_COMPOSITION (c
);
2110 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c
);
2112 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c
);
2116 else if (c
< 0xA0 && emacs_mule_bytes
[c
] > 1)
2122 consumed_chars
= consumed_chars_base
;
2123 c
= emacs_mule_char (coding
, src
, &nbytes
, &nchars
, &id
);
2132 if (last_id
!= charset_ascii
)
2133 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2135 last_offset
= char_offset
;
2139 consumed_chars
+= nchars
;
2148 consumed_chars
= consumed_chars_base
;
2150 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2156 if (last_id
!= charset_ascii
)
2157 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2158 coding
->consumed_char
+= consumed_chars_base
;
2159 coding
->consumed
= src_base
- coding
->source
;
2160 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2164 #define EMACS_MULE_LEADING_CODES(id, codes) \
2167 codes[0] = id, codes[1] = 0; \
2168 else if (id < 0xE0) \
2169 codes[0] = 0x9A, codes[1] = id; \
2170 else if (id < 0xF0) \
2171 codes[0] = 0x9B, codes[1] = id; \
2172 else if (id < 0xF5) \
2173 codes[0] = 0x9C, codes[1] = id; \
2175 codes[0] = 0x9D, codes[1] = id; \
2180 encode_coding_emacs_mule (coding
)
2181 struct coding_system
*coding
;
2183 int multibytep
= coding
->dst_multibyte
;
2184 int *charbuf
= coding
->charbuf
;
2185 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2186 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2187 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2189 int produced_chars
= 0;
2190 Lisp_Object attrs
, charset_list
;
2192 int preferred_charset_id
= -1;
2194 CODING_GET_INFO (coding
, attrs
, charset_list
);
2195 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2197 CODING_ATTR_CHARSET_LIST (attrs
)
2198 = charset_list
= Vemacs_mule_charset_list
;
2201 while (charbuf
< charbuf_end
)
2203 ASSURE_DESTINATION (safe_room
);
2208 /* Handle an annotation. */
2211 case CODING_ANNOTATE_COMPOSITION_MASK
:
2212 /* Not yet implemented. */
2214 case CODING_ANNOTATE_CHARSET_MASK
:
2215 preferred_charset_id
= charbuf
[3];
2216 if (preferred_charset_id
>= 0
2217 && NILP (Fmemq (make_number (preferred_charset_id
),
2219 preferred_charset_id
= -1;
2228 if (ASCII_CHAR_P (c
))
2229 EMIT_ONE_ASCII_BYTE (c
);
2230 else if (CHAR_BYTE8_P (c
))
2232 c
= CHAR_TO_BYTE8 (c
);
2237 struct charset
*charset
;
2241 unsigned char leading_codes
[2];
2243 if (preferred_charset_id
>= 0)
2245 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2246 if (! CHAR_CHARSET_P (c
, charset
))
2247 charset
= char_charset (c
, charset_list
, NULL
);
2250 charset
= char_charset (c
, charset_list
, &code
);
2253 c
= coding
->default_char
;
2254 if (ASCII_CHAR_P (c
))
2256 EMIT_ONE_ASCII_BYTE (c
);
2259 charset
= char_charset (c
, charset_list
, &code
);
2261 dimension
= CHARSET_DIMENSION (charset
);
2262 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2263 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2264 EMIT_ONE_BYTE (leading_codes
[0]);
2265 if (leading_codes
[1])
2266 EMIT_ONE_BYTE (leading_codes
[1]);
2268 EMIT_ONE_BYTE (code
| 0x80);
2272 EMIT_ONE_BYTE (code
>> 8);
2273 EMIT_ONE_BYTE (code
& 0xFF);
2277 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2278 coding
->produced_char
+= produced_chars
;
2279 coding
->produced
= dst
- coding
->destination
;
2284 /*** 7. ISO2022 handlers ***/
2286 /* The following note describes the coding system ISO2022 briefly.
2287 Since the intention of this note is to help understand the
2288 functions in this file, some parts are NOT ACCURATE or are OVERLY
2289 SIMPLIFIED. For thorough understanding, please refer to the
2290 original document of ISO2022. This is equivalent to the standard
2291 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2293 ISO2022 provides many mechanisms to encode several character sets
2294 in 7-bit and 8-bit environments. For 7-bit environments, all text
2295 is encoded using bytes less than 128. This may make the encoded
2296 text a little bit longer, but the text passes more easily through
2297 several types of gateway, some of which strip off the MSB (Most
2300 There are two kinds of character sets: control character sets and
2301 graphic character sets. The former contain control characters such
2302 as `newline' and `escape' to provide control functions (control
2303 functions are also provided by escape sequences). The latter
2304 contain graphic characters such as 'A' and '-'. Emacs recognizes
2305 two control character sets and many graphic character sets.
2307 Graphic character sets are classified into one of the following
2308 four classes, according to the number of bytes (DIMENSION) and
2309 number of characters in one dimension (CHARS) of the set:
2310 - DIMENSION1_CHARS94
2311 - DIMENSION1_CHARS96
2312 - DIMENSION2_CHARS94
2313 - DIMENSION2_CHARS96
2315 In addition, each character set is assigned an identification tag,
2316 unique for each set, called the "final character" (denoted as <F>
2317 hereafter). The <F> of each character set is decided by ECMA(*)
2318 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2319 (0x30..0x3F are for private use only).
2321 Note (*): ECMA = European Computer Manufacturers Association
2323 Here are examples of graphic character sets [NAME(<F>)]:
2324 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2325 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2326 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2327 o DIMENSION2_CHARS96 -- none for the moment
2329 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2330 C0 [0x00..0x1F] -- control character plane 0
2331 GL [0x20..0x7F] -- graphic character plane 0
2332 C1 [0x80..0x9F] -- control character plane 1
2333 GR [0xA0..0xFF] -- graphic character plane 1
2335 A control character set is directly designated and invoked to C0 or
2336 C1 by an escape sequence. The most common case is that:
2337 - ISO646's control character set is designated/invoked to C0, and
2338 - ISO6429's control character set is designated/invoked to C1,
2339 and usually these designations/invocations are omitted in encoded
2340 text. In a 7-bit environment, only C0 can be used, and a control
2341 character for C1 is encoded by an appropriate escape sequence to
2342 fit into the environment. All control characters for C1 are
2343 defined to have corresponding escape sequences.
2345 A graphic character set is at first designated to one of four
2346 graphic registers (G0 through G3), then these graphic registers are
2347 invoked to GL or GR. These designations and invocations can be
2348 done independently. The most common case is that G0 is invoked to
2349 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2350 these invocations and designations are omitted in encoded text.
2351 In a 7-bit environment, only GL can be used.
2353 When a graphic character set of CHARS94 is invoked to GL, codes
2354 0x20 and 0x7F of the GL area work as control characters SPACE and
2355 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2358 There are two ways of invocation: locking-shift and single-shift.
2359 With locking-shift, the invocation lasts until the next different
2360 invocation, whereas with single-shift, the invocation affects the
2361 following character only and doesn't affect the locking-shift
2362 state. Invocations are done by the following control characters or
2365 ----------------------------------------------------------------------
2366 abbrev function cntrl escape seq description
2367 ----------------------------------------------------------------------
2368 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2369 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2370 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2371 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2372 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2373 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2374 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2375 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2376 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2377 ----------------------------------------------------------------------
2378 (*) These are not used by any known coding system.
2380 Control characters for these functions are defined by macros
2381 ISO_CODE_XXX in `coding.h'.
2383 Designations are done by the following escape sequences:
2384 ----------------------------------------------------------------------
2385 escape sequence description
2386 ----------------------------------------------------------------------
2387 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2388 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2389 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2390 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2391 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2392 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2393 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2394 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2395 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2396 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2397 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2398 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2399 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2400 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2401 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2402 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2403 ----------------------------------------------------------------------
2405 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2406 of dimension 1, chars 94, and final character <F>, etc...
2408 Note (*): Although these designations are not allowed in ISO2022,
2409 Emacs accepts them on decoding, and produces them on encoding
2410 CHARS96 character sets in a coding system which is characterized as
2411 7-bit environment, non-locking-shift, and non-single-shift.
2413 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2414 '(' must be omitted. We refer to this as "short-form" hereafter.
2416 Now you may notice that there are a lot of ways of encoding the
2417 same multilingual text in ISO2022. Actually, there exist many
2418 coding systems such as Compound Text (used in X11's inter client
2419 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2420 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2421 localized platforms), and all of these are variants of ISO2022.
2423 In addition to the above, Emacs handles two more kinds of escape
2424 sequences: ISO6429's direction specification and Emacs' private
2425 sequence for specifying character composition.
2427 ISO6429's direction specification takes the following form:
2428 o CSI ']' -- end of the current direction
2429 o CSI '0' ']' -- end of the current direction
2430 o CSI '1' ']' -- start of left-to-right text
2431 o CSI '2' ']' -- start of right-to-left text
2432 The control character CSI (0x9B: control sequence introducer) is
2433 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2435 Character composition specification takes the following form:
2436 o ESC '0' -- start relative composition
2437 o ESC '1' -- end composition
2438 o ESC '2' -- start rule-base composition (*)
2439 o ESC '3' -- start relative composition with alternate chars (**)
2440 o ESC '4' -- start rule-base composition with alternate chars (**)
2441 Since these are not standard escape sequences of any ISO standard,
2442 the use of them with these meanings is restricted to Emacs only.
2444 (*) This form is used only in Emacs 20.7 and older versions,
2445 but newer versions can safely decode it.
2446 (**) This form is used only in Emacs 21.1 and newer versions,
2447 and older versions can't decode it.
2449 Here's a list of example usages of these composition escape
2450 sequences (categorized by `enum composition_method').
2452 COMPOSITION_RELATIVE:
2453 ESC 0 CHAR [ CHAR ] ESC 1
2454 COMPOSITION_WITH_RULE:
2455 ESC 2 CHAR [ RULE CHAR ] ESC 1
2456 COMPOSITION_WITH_ALTCHARS:
2457 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2458 COMPOSITION_WITH_RULE_ALTCHARS:
2459 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2461 enum iso_code_class_type iso_code_class
[256];
2463 #define SAFE_CHARSET_P(coding, id) \
2464 ((id) <= (coding)->max_charset_id \
2465 && (coding)->safe_charsets[id] >= 0)
2468 #define SHIFT_OUT_OK(category) \
2469 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2472 setup_iso_safe_charsets (attrs
)
2475 Lisp_Object charset_list
, safe_charsets
;
2476 Lisp_Object request
;
2477 Lisp_Object reg_usage
;
2480 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2483 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2484 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2485 && ! EQ (charset_list
, Viso_2022_charset_list
))
2487 CODING_ATTR_CHARSET_LIST (attrs
)
2488 = charset_list
= Viso_2022_charset_list
;
2489 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2492 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2496 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2498 int id
= XINT (XCAR (tail
));
2499 if (max_charset_id
< id
)
2500 max_charset_id
= id
;
2503 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
2505 request
= AREF (attrs
, coding_attr_iso_request
);
2506 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2507 reg94
= XINT (XCAR (reg_usage
));
2508 reg96
= XINT (XCDR (reg_usage
));
2510 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2514 struct charset
*charset
;
2517 charset
= CHARSET_FROM_ID (XINT (id
));
2518 reg
= Fcdr (Fassq (id
, request
));
2520 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2521 else if (charset
->iso_chars_96
)
2524 SSET (safe_charsets
, XINT (id
), reg96
);
2529 SSET (safe_charsets
, XINT (id
), reg94
);
2532 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2536 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2537 Check if a text is encoded in one of ISO-2022 based codig systems.
2538 If it is, return 1, else return 0. */
2541 detect_coding_iso_2022 (coding
, detect_info
)
2542 struct coding_system
*coding
;
2543 struct coding_detection_info
*detect_info
;
2545 const unsigned char *src
= coding
->source
, *src_base
= src
;
2546 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2547 int multibytep
= coding
->src_multibyte
;
2548 int single_shifting
= 0;
2551 int consumed_chars
= 0;
2556 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2558 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2560 struct coding_system
*this = &(coding_categories
[i
]);
2561 Lisp_Object attrs
, val
;
2563 attrs
= CODING_ID_ATTRS (this->id
);
2564 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2565 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs
), Viso_2022_charset_list
))
2566 setup_iso_safe_charsets (attrs
);
2567 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2568 this->max_charset_id
= SCHARS (val
) - 1;
2569 this->safe_charsets
= (char *) SDATA (val
);
2572 /* A coding system of this category is always ASCII compatible. */
2573 src
+= coding
->head_ascii
;
2575 while (rejected
!= CATEGORY_MASK_ISO
)
2582 if (inhibit_iso_escape_detection
)
2584 single_shifting
= 0;
2586 if (c
>= '(' && c
<= '/')
2588 /* Designation sequence for a charset of dimension 1. */
2590 if (c1
< ' ' || c1
>= 0x80
2591 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2592 /* Invalid designation sequence. Just ignore. */
2597 /* Designation sequence for a charset of dimension 2. */
2599 if (c
>= '@' && c
<= 'B')
2600 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2601 id
= iso_charset_table
[1][0][c
];
2602 else if (c
>= '(' && c
<= '/')
2605 if (c1
< ' ' || c1
>= 0x80
2606 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2607 /* Invalid designation sequence. Just ignore. */
2611 /* Invalid designation sequence. Just ignore it. */
2614 else if (c
== 'N' || c
== 'O')
2616 /* ESC <Fe> for SS2 or SS3. */
2617 single_shifting
= 1;
2618 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2621 else if (c
>= '0' && c
<= '4')
2623 /* ESC <Fp> for start/end composition. */
2624 found
|= CATEGORY_MASK_ISO
;
2629 /* Invalid escape sequence. Just ignore it. */
2633 /* We found a valid designation sequence for CHARSET. */
2634 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2635 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2637 found
|= CATEGORY_MASK_ISO_7
;
2639 rejected
|= CATEGORY_MASK_ISO_7
;
2640 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
2642 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
2644 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
2645 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
2647 found
|= CATEGORY_MASK_ISO_7_ELSE
;
2649 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
2650 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
2652 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2654 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
2659 /* Locking shift out/in. */
2660 if (inhibit_iso_escape_detection
)
2662 single_shifting
= 0;
2663 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2664 found
|= CATEGORY_MASK_ISO_ELSE
;
2668 /* Control sequence introducer. */
2669 single_shifting
= 0;
2670 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2671 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2672 goto check_extra_latin
;
2677 if (inhibit_iso_escape_detection
)
2679 single_shifting
= 0;
2680 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2681 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2682 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2683 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2684 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2685 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2686 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2687 if (single_shifting
)
2689 goto check_extra_latin
;
2696 single_shifting
= 0;
2701 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2702 found
|= CATEGORY_MASK_ISO_8_1
;
2703 /* Check the length of succeeding codes of the range
2704 0xA0..0FF. If the byte length is even, we include
2705 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2706 only when we are not single shifting. */
2707 if (! single_shifting
2708 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2711 while (src
< src_end
)
2719 if (i
& 1 && src
< src_end
)
2720 rejected
|= CATEGORY_MASK_ISO_8_2
;
2722 found
|= CATEGORY_MASK_ISO_8_2
;
2727 single_shifting
= 0;
2728 if (! VECTORP (Vlatin_extra_code_table
)
2729 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
2731 rejected
= CATEGORY_MASK_ISO
;
2734 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2735 & CODING_ISO_FLAG_LATIN_EXTRA
)
2736 found
|= CATEGORY_MASK_ISO_8_1
;
2738 rejected
|= CATEGORY_MASK_ISO_8_1
;
2739 rejected
|= CATEGORY_MASK_ISO_8_2
;
2742 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
2746 detect_info
->rejected
|= rejected
;
2747 detect_info
->found
|= (found
& ~rejected
);
2752 /* Set designation state into CODING. Set CHARS_96 to -1 if the
2753 escape sequence should be kept. */
2754 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2758 if (final < '0' || final >= 128 \
2759 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2760 || !SAFE_CHARSET_P (coding, id)) \
2762 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2766 prev = CODING_ISO_DESIGNATION (coding, reg); \
2767 if (id == charset_jisx0201_roman) \
2769 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2770 id = charset_ascii; \
2772 else if (id == charset_jisx0208_1978) \
2774 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2775 id = charset_jisx0208; \
2777 CODING_ISO_DESIGNATION (coding, reg) = id; \
2778 /* If there was an invalid designation to REG previously, and this \
2779 designation is ASCII to REG, we should keep this designation \
2781 if (prev == -2 && id == charset_ascii) \
2786 #define MAYBE_FINISH_COMPOSITION() \
2789 if (composition_state == COMPOSING_NO) \
2791 /* It is assured that we have enough room for producing \
2792 characters stored in the table `components'. */ \
2793 if (charbuf + component_idx > charbuf_end) \
2794 goto no_more_source; \
2795 composition_state = COMPOSING_NO; \
2796 if (method == COMPOSITION_RELATIVE \
2797 || method == COMPOSITION_WITH_ALTCHARS) \
2799 for (i = 0; i < component_idx; i++) \
2800 *charbuf++ = components[i]; \
2801 char_offset += component_idx; \
2805 for (i = 0; i < component_idx; i += 2) \
2806 *charbuf++ = components[i]; \
2807 char_offset += (component_idx / 2) + 1; \
2812 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2813 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2814 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2815 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2816 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2819 #define DECODE_COMPOSITION_START(c1) \
2822 && composition_state == COMPOSING_COMPONENT_RULE) \
2824 component_len = component_idx; \
2825 composition_state = COMPOSING_CHAR; \
2829 const unsigned char *p; \
2831 MAYBE_FINISH_COMPOSITION (); \
2832 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2833 goto no_more_source; \
2834 for (p = src; p < src_end - 1; p++) \
2835 if (*p == ISO_CODE_ESC && p[1] == '1') \
2837 if (p == src_end - 1) \
2839 /* The current composition doesn't end in the current \
2841 record_conversion_result \
2842 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
2843 goto no_more_source; \
2846 /* This is surely the start of a composition. */ \
2847 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2848 : c1 == '2' ? COMPOSITION_WITH_RULE \
2849 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2850 : COMPOSITION_WITH_RULE_ALTCHARS); \
2851 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2852 : COMPOSING_COMPONENT_CHAR); \
2853 component_idx = component_len = 0; \
2858 /* Handle compositoin end sequence ESC 1. */
2860 #define DECODE_COMPOSITION_END() \
2862 int nchars = (component_len > 0 ? component_idx - component_len \
2863 : method == COMPOSITION_RELATIVE ? component_idx \
2864 : (component_idx + 1) / 2); \
2866 int *saved_charbuf = charbuf; \
2868 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2869 if (method != COMPOSITION_RELATIVE) \
2871 if (component_len == 0) \
2872 for (i = 0; i < component_idx; i++) \
2873 *charbuf++ = components[i]; \
2875 for (i = 0; i < component_len; i++) \
2876 *charbuf++ = components[i]; \
2877 *saved_charbuf = saved_charbuf - charbuf; \
2879 if (method == COMPOSITION_WITH_RULE) \
2880 for (i = 0; i < component_idx; i += 2, char_offset++) \
2881 *charbuf++ = components[i]; \
2883 for (i = component_len; i < component_idx; i++, char_offset++) \
2884 *charbuf++ = components[i]; \
2885 coding->annotated = 1; \
2886 composition_state = COMPOSING_NO; \
2890 /* Decode a composition rule from the byte C1 (and maybe one more byte
2891 from SRC) and store one encoded composition rule in
2892 coding->cmp_data. */
2894 #define DECODE_COMPOSITION_RULE(c1) \
2897 if (c1 < 81) /* old format (before ver.21) */ \
2899 int gref = (c1) / 9; \
2900 int nref = (c1) % 9; \
2901 if (gref == 4) gref = 10; \
2902 if (nref == 4) nref = 10; \
2903 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
2905 else if (c1 < 93) /* new format (after ver.21) */ \
2907 ONE_MORE_BYTE (c2); \
2908 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
2915 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2918 decode_coding_iso_2022 (coding
)
2919 struct coding_system
*coding
;
2921 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2922 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2923 const unsigned char *src_base
;
2924 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2926 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
2927 int consumed_chars
= 0, consumed_chars_base
;
2928 int multibytep
= coding
->src_multibyte
;
2929 /* Charsets invoked to graphic plane 0 and 1 respectively. */
2930 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2931 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
2932 int charset_id_2
, charset_id_3
;
2933 struct charset
*charset
;
2935 /* For handling composition sequence. */
2936 #define COMPOSING_NO 0
2937 #define COMPOSING_CHAR 1
2938 #define COMPOSING_RULE 2
2939 #define COMPOSING_COMPONENT_CHAR 3
2940 #define COMPOSING_COMPONENT_RULE 4
2942 int composition_state
= COMPOSING_NO
;
2943 enum composition_method method
;
2944 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
2947 Lisp_Object attrs
, charset_list
;
2948 int char_offset
= coding
->produced_char
;
2949 int last_offset
= char_offset
;
2950 int last_id
= charset_ascii
;
2952 CODING_GET_INFO (coding
, attrs
, charset_list
);
2953 setup_iso_safe_charsets (attrs
);
2954 /* Charset list may have been changed. */
2955 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2956 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
2963 consumed_chars_base
= consumed_chars
;
2965 if (charbuf
>= charbuf_end
)
2972 /* We produce at most one character. */
2973 switch (iso_code_class
[c1
])
2975 case ISO_0x20_or_0x7F
:
2976 if (composition_state
!= COMPOSING_NO
)
2978 if (composition_state
== COMPOSING_RULE
2979 || composition_state
== COMPOSING_COMPONENT_RULE
)
2981 DECODE_COMPOSITION_RULE (c1
);
2982 components
[component_idx
++] = c1
;
2983 composition_state
--;
2987 if (charset_id_0
< 0
2988 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
2989 /* This is SPACE or DEL. */
2990 charset
= CHARSET_FROM_ID (charset_ascii
);
2992 charset
= CHARSET_FROM_ID (charset_id_0
);
2995 case ISO_graphic_plane_0
:
2996 if (composition_state
!= COMPOSING_NO
)
2998 if (composition_state
== COMPOSING_RULE
2999 || composition_state
== COMPOSING_COMPONENT_RULE
)
3001 DECODE_COMPOSITION_RULE (c1
);
3002 components
[component_idx
++] = c1
;
3003 composition_state
--;
3007 if (charset_id_0
< 0)
3008 charset
= CHARSET_FROM_ID (charset_ascii
);
3010 charset
= CHARSET_FROM_ID (charset_id_0
);
3013 case ISO_0xA0_or_0xFF
:
3014 if (charset_id_1
< 0
3015 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3016 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3018 /* This is a graphic character, we fall down ... */
3020 case ISO_graphic_plane_1
:
3021 if (charset_id_1
< 0)
3023 charset
= CHARSET_FROM_ID (charset_id_1
);
3027 MAYBE_FINISH_COMPOSITION ();
3028 charset
= CHARSET_FROM_ID (charset_ascii
);
3032 MAYBE_FINISH_COMPOSITION ();
3036 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3037 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3039 CODING_ISO_INVOCATION (coding
, 0) = 1;
3040 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3044 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3046 CODING_ISO_INVOCATION (coding
, 0) = 0;
3047 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3050 case ISO_single_shift_2_7
:
3051 case ISO_single_shift_2
:
3052 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3054 /* SS2 is handled as an escape sequence of ESC 'N' */
3056 goto label_escape_sequence
;
3058 case ISO_single_shift_3
:
3059 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3061 /* SS2 is handled as an escape sequence of ESC 'O' */
3063 goto label_escape_sequence
;
3065 case ISO_control_sequence_introducer
:
3066 /* CSI is handled as an escape sequence of ESC '[' ... */
3068 goto label_escape_sequence
;
3072 label_escape_sequence
:
3073 /* Escape sequences handled here are invocation,
3074 designation, direction specification, and character
3075 composition specification. */
3078 case '&': /* revision of following character set */
3080 if (!(c1
>= '@' && c1
<= '~'))
3083 if (c1
!= ISO_CODE_ESC
)
3086 goto label_escape_sequence
;
3088 case '$': /* designation of 2-byte character set */
3089 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3095 if (c1
>= '@' && c1
<= 'B')
3096 { /* designation of JISX0208.1978, GB2312.1980,
3098 reg
= 0, chars96
= 0;
3100 else if (c1
>= 0x28 && c1
<= 0x2B)
3101 { /* designation of DIMENSION2_CHARS94 character set */
3102 reg
= c1
- 0x28, chars96
= 0;
3105 else if (c1
>= 0x2C && c1
<= 0x2F)
3106 { /* designation of DIMENSION2_CHARS96 character set */
3107 reg
= c1
- 0x2C, chars96
= 1;
3112 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3113 /* We must update these variables now. */
3115 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3117 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3123 case 'n': /* invocation of locking-shift-2 */
3124 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3125 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3127 CODING_ISO_INVOCATION (coding
, 0) = 2;
3128 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3131 case 'o': /* invocation of locking-shift-3 */
3132 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3133 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3135 CODING_ISO_INVOCATION (coding
, 0) = 3;
3136 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3139 case 'N': /* invocation of single-shift-2 */
3140 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3141 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3143 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3144 if (charset_id_2
< 0)
3145 charset
= CHARSET_FROM_ID (charset_ascii
);
3147 charset
= CHARSET_FROM_ID (charset_id_2
);
3149 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3153 case 'O': /* invocation of single-shift-3 */
3154 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3155 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3157 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3158 if (charset_id_3
< 0)
3159 charset
= CHARSET_FROM_ID (charset_ascii
);
3161 charset
= CHARSET_FROM_ID (charset_id_3
);
3163 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3167 case '0': case '2': case '3': case '4': /* start composition */
3168 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3170 DECODE_COMPOSITION_START (c1
);
3173 case '1': /* end composition */
3174 if (composition_state
== COMPOSING_NO
)
3176 DECODE_COMPOSITION_END ();
3179 case '[': /* specification of direction */
3180 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3182 /* For the moment, nested direction is not supported.
3183 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3184 left-to-right, and nozero means right-to-left. */
3188 case ']': /* end of the current direction */
3189 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3191 case '0': /* end of the current direction */
3192 case '1': /* start of left-to-right direction */
3195 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3200 case '2': /* start of right-to-left direction */
3203 coding
->mode
|= CODING_MODE_DIRECTION
;
3217 /* CTEXT extended segment:
3218 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3219 We keep these bytes as is for the moment.
3220 They may be decoded by post-read-conversion. */
3224 ONE_MORE_BYTE (dim
);
3227 size
= ((M
- 128) * 128) + (L
- 128);
3228 if (charbuf
+ 8 + size
> charbuf_end
)
3230 *charbuf
++ = ISO_CODE_ESC
;
3234 *charbuf
++ = BYTE8_TO_CHAR (M
);
3235 *charbuf
++ = BYTE8_TO_CHAR (L
);
3239 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3244 /* XFree86 extension for embedding UTF-8 in CTEXT:
3245 ESC % G --UTF-8-BYTES-- ESC % @
3246 We keep these bytes as is for the moment.
3247 They may be decoded by post-read-conversion. */
3250 if (p
+ 6 > charbuf_end
)
3252 *p
++ = ISO_CODE_ESC
;
3255 while (p
< charbuf_end
)
3258 if (c1
== ISO_CODE_ESC
3259 && src
+ 1 < src_end
3266 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3268 if (p
+ 3 > charbuf_end
)
3270 *p
++ = ISO_CODE_ESC
;
3281 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3286 if (c1
>= 0x28 && c1
<= 0x2B)
3287 { /* designation of DIMENSION1_CHARS94 character set */
3288 reg
= c1
- 0x28, chars96
= 0;
3291 else if (c1
>= 0x2C && c1
<= 0x2F)
3292 { /* designation of DIMENSION1_CHARS96 character set */
3293 reg
= c1
- 0x2C, chars96
= 1;
3298 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3299 /* We must update these variables now. */
3301 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3303 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3311 if (charset
->id
!= charset_ascii
3312 && last_id
!= charset
->id
)
3314 if (last_id
!= charset_ascii
)
3315 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3316 last_id
= charset
->id
;
3317 last_offset
= char_offset
;
3320 /* Now we know CHARSET and 1st position code C1 of a character.
3321 Produce a decoded character while getting 2nd position code
3324 if (CHARSET_DIMENSION (charset
) > 1)
3327 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3328 /* C2 is not in a valid range. */
3330 c1
= (c1
<< 8) | (c2
& 0x7F);
3331 if (CHARSET_DIMENSION (charset
) > 2)
3334 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3335 /* C2 is not in a valid range. */
3337 c1
= (c1
<< 8) | (c2
& 0x7F);
3341 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3344 MAYBE_FINISH_COMPOSITION ();
3345 for (; src_base
< src
; src_base
++, char_offset
++)
3347 if (ASCII_BYTE_P (*src_base
))
3348 *charbuf
++ = *src_base
;
3350 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3353 else if (composition_state
== COMPOSING_NO
)
3360 components
[component_idx
++] = c
;
3361 if (method
== COMPOSITION_WITH_RULE
3362 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3363 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3364 composition_state
++;
3369 MAYBE_FINISH_COMPOSITION ();
3371 consumed_chars
= consumed_chars_base
;
3373 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3383 if (last_id
!= charset_ascii
)
3384 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3385 coding
->consumed_char
+= consumed_chars_base
;
3386 coding
->consumed
= src_base
- coding
->source
;
3387 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3391 /* ISO2022 encoding stuff. */
3394 It is not enough to say just "ISO2022" on encoding, we have to
3395 specify more details. In Emacs, each coding system of ISO2022
3396 variant has the following specifications:
3397 1. Initial designation to G0 thru G3.
3398 2. Allows short-form designation?
3399 3. ASCII should be designated to G0 before control characters?
3400 4. ASCII should be designated to G0 at end of line?
3401 5. 7-bit environment or 8-bit environment?
3402 6. Use locking-shift?
3403 7. Use Single-shift?
3404 And the following two are only for Japanese:
3405 8. Use ASCII in place of JIS0201-1976-Roman?
3406 9. Use JISX0208-1983 in place of JISX0208-1978?
3407 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3408 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3412 /* Produce codes (escape sequence) for designating CHARSET to graphic
3413 register REG at DST, and increment DST. If <final-char> of CHARSET is
3414 '@', 'A', or 'B' and the coding system CODING allows, produce
3415 designation sequence of short-form. */
3417 #define ENCODE_DESIGNATION(charset, reg, coding) \
3419 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3420 char *intermediate_char_94 = "()*+"; \
3421 char *intermediate_char_96 = ",-./"; \
3422 int revision = -1; \
3425 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3426 revision = CHARSET_ISO_REVISION (charset); \
3428 if (revision >= 0) \
3430 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3431 EMIT_ONE_BYTE ('@' + revision); \
3433 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3434 if (CHARSET_DIMENSION (charset) == 1) \
3436 if (! CHARSET_ISO_CHARS_96 (charset)) \
3437 c = intermediate_char_94[reg]; \
3439 c = intermediate_char_96[reg]; \
3440 EMIT_ONE_ASCII_BYTE (c); \
3444 EMIT_ONE_ASCII_BYTE ('$'); \
3445 if (! CHARSET_ISO_CHARS_96 (charset)) \
3447 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3449 || final_char < '@' || final_char > 'B') \
3450 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3453 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3455 EMIT_ONE_ASCII_BYTE (final_char); \
3457 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3461 /* The following two macros produce codes (control character or escape
3462 sequence) for ISO2022 single-shift functions (single-shift-2 and
3465 #define ENCODE_SINGLE_SHIFT_2 \
3467 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3468 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3470 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3471 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3475 #define ENCODE_SINGLE_SHIFT_3 \
3477 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3478 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3480 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3481 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3485 /* The following four macros produce codes (control character or
3486 escape sequence) for ISO2022 locking-shift functions (shift-in,
3487 shift-out, locking-shift-2, and locking-shift-3). */
3489 #define ENCODE_SHIFT_IN \
3491 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3492 CODING_ISO_INVOCATION (coding, 0) = 0; \
3496 #define ENCODE_SHIFT_OUT \
3498 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3499 CODING_ISO_INVOCATION (coding, 0) = 1; \
3503 #define ENCODE_LOCKING_SHIFT_2 \
3505 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3506 CODING_ISO_INVOCATION (coding, 0) = 2; \
3510 #define ENCODE_LOCKING_SHIFT_3 \
3512 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3513 CODING_ISO_INVOCATION (coding, 0) = 3; \
3517 /* Produce codes for a DIMENSION1 character whose character set is
3518 CHARSET and whose position-code is C1. Designation and invocation
3519 sequences are also produced in advance if necessary. */
3521 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3523 int id = CHARSET_ID (charset); \
3525 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3526 && id == charset_ascii) \
3528 id = charset_jisx0201_roman; \
3529 charset = CHARSET_FROM_ID (id); \
3532 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3534 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3535 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3537 EMIT_ONE_BYTE (c1 | 0x80); \
3538 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3541 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3543 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3546 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3548 EMIT_ONE_BYTE (c1 | 0x80); \
3552 /* Since CHARSET is not yet invoked to any graphic planes, we \
3553 must invoke it, or, at first, designate it to some graphic \
3554 register. Then repeat the loop to actually produce the \
3556 dst = encode_invocation_designation (charset, coding, dst, \
3561 /* Produce codes for a DIMENSION2 character whose character set is
3562 CHARSET and whose position-codes are C1 and C2. Designation and
3563 invocation codes are also produced in advance if necessary. */
3565 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3567 int id = CHARSET_ID (charset); \
3569 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3570 && id == charset_jisx0208) \
3572 id = charset_jisx0208_1978; \
3573 charset = CHARSET_FROM_ID (id); \
3576 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3578 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3579 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3581 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3582 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3585 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3587 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3590 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3592 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3596 /* Since CHARSET is not yet invoked to any graphic planes, we \
3597 must invoke it, or, at first, designate it to some graphic \
3598 register. Then repeat the loop to actually produce the \
3600 dst = encode_invocation_designation (charset, coding, dst, \
3605 #define ENCODE_ISO_CHARACTER(charset, c) \
3607 int code = ENCODE_CHAR ((charset),(c)); \
3609 if (CHARSET_DIMENSION (charset) == 1) \
3610 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3612 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3616 /* Produce designation and invocation codes at a place pointed by DST
3617 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3621 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3622 struct charset
*charset
;
3623 struct coding_system
*coding
;
3627 int multibytep
= coding
->dst_multibyte
;
3628 int produced_chars
= *p_nchars
;
3629 int reg
; /* graphic register number */
3630 int id
= CHARSET_ID (charset
);
3632 /* At first, check designations. */
3633 for (reg
= 0; reg
< 4; reg
++)
3634 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3639 /* CHARSET is not yet designated to any graphic registers. */
3640 /* At first check the requested designation. */
3641 reg
= CODING_ISO_REQUEST (coding
, id
);
3643 /* Since CHARSET requests no special designation, designate it
3644 to graphic register 0. */
3647 ENCODE_DESIGNATION (charset
, reg
, coding
);
3650 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3651 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3653 /* Since the graphic register REG is not invoked to any graphic
3654 planes, invoke it to graphic plane 0. */
3657 case 0: /* graphic register 0 */
3661 case 1: /* graphic register 1 */
3665 case 2: /* graphic register 2 */
3666 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3667 ENCODE_SINGLE_SHIFT_2
;
3669 ENCODE_LOCKING_SHIFT_2
;
3672 case 3: /* graphic register 3 */
3673 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3674 ENCODE_SINGLE_SHIFT_3
;
3676 ENCODE_LOCKING_SHIFT_3
;
3681 *p_nchars
= produced_chars
;
3685 /* The following three macros produce codes for indicating direction
3687 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3689 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3690 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3692 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3696 #define ENCODE_DIRECTION_R2L() \
3698 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3699 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3703 #define ENCODE_DIRECTION_L2R() \
3705 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3706 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3710 /* Produce codes for designation and invocation to reset the graphic
3711 planes and registers to initial state. */
3712 #define ENCODE_RESET_PLANE_AND_REGISTER() \
3715 struct charset *charset; \
3717 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3719 for (reg = 0; reg < 4; reg++) \
3720 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3721 && (CODING_ISO_DESIGNATION (coding, reg) \
3722 != CODING_ISO_INITIAL (coding, reg))) \
3724 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3725 ENCODE_DESIGNATION (charset, reg, coding); \
3730 /* Produce designation sequences of charsets in the line started from
3731 SRC to a place pointed by DST, and return updated DST.
3733 If the current block ends before any end-of-line, we may fail to
3734 find all the necessary designations. */
3736 static unsigned char *
3737 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
3738 struct coding_system
*coding
;
3739 int *charbuf
, *charbuf_end
;
3742 struct charset
*charset
;
3743 /* Table of charsets to be designated to each graphic register. */
3745 int c
, found
= 0, reg
;
3746 int produced_chars
= 0;
3747 int multibytep
= coding
->dst_multibyte
;
3749 Lisp_Object charset_list
;
3751 attrs
= CODING_ID_ATTRS (coding
->id
);
3752 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3753 if (EQ (charset_list
, Qiso_2022
))
3754 charset_list
= Viso_2022_charset_list
;
3756 for (reg
= 0; reg
< 4; reg
++)
3766 charset
= char_charset (c
, charset_list
, NULL
);
3767 id
= CHARSET_ID (charset
);
3768 reg
= CODING_ISO_REQUEST (coding
, id
);
3769 if (reg
>= 0 && r
[reg
] < 0)
3778 for (reg
= 0; reg
< 4; reg
++)
3780 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
3781 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
3787 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3790 encode_coding_iso_2022 (coding
)
3791 struct coding_system
*coding
;
3793 int multibytep
= coding
->dst_multibyte
;
3794 int *charbuf
= coding
->charbuf
;
3795 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
3796 unsigned char *dst
= coding
->destination
+ coding
->produced
;
3797 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
3800 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3801 && CODING_ISO_BOL (coding
));
3802 int produced_chars
= 0;
3803 Lisp_Object attrs
, eol_type
, charset_list
;
3804 int ascii_compatible
;
3806 int preferred_charset_id
= -1;
3808 CODING_GET_INFO (coding
, attrs
, charset_list
);
3809 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
3810 if (VECTORP (eol_type
))
3813 setup_iso_safe_charsets (attrs
);
3814 /* Charset list may have been changed. */
3815 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3816 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3818 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
3820 while (charbuf
< charbuf_end
)
3822 ASSURE_DESTINATION (safe_room
);
3824 if (bol_designation
)
3826 unsigned char *dst_prev
= dst
;
3828 /* We have to produce designation sequences if any now. */
3829 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
3830 bol_designation
= 0;
3831 /* We are sure that designation sequences are all ASCII bytes. */
3832 produced_chars
+= dst
- dst_prev
;
3839 /* Handle an annotation. */
3842 case CODING_ANNOTATE_COMPOSITION_MASK
:
3843 /* Not yet implemented. */
3845 case CODING_ANNOTATE_CHARSET_MASK
:
3846 preferred_charset_id
= charbuf
[2];
3847 if (preferred_charset_id
>= 0
3848 && NILP (Fmemq (make_number (preferred_charset_id
),
3850 preferred_charset_id
= -1;
3859 /* Now encode the character C. */
3860 if (c
< 0x20 || c
== 0x7F)
3863 || (c
== '\r' && EQ (eol_type
, Qmac
)))
3865 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3866 ENCODE_RESET_PLANE_AND_REGISTER ();
3867 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
3871 for (i
= 0; i
< 4; i
++)
3872 CODING_ISO_DESIGNATION (coding
, i
)
3873 = CODING_ISO_INITIAL (coding
, i
);
3876 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
3878 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
3879 ENCODE_RESET_PLANE_AND_REGISTER ();
3880 EMIT_ONE_ASCII_BYTE (c
);
3882 else if (ASCII_CHAR_P (c
))
3884 if (ascii_compatible
)
3885 EMIT_ONE_ASCII_BYTE (c
);
3888 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
3889 ENCODE_ISO_CHARACTER (charset
, c
);
3892 else if (CHAR_BYTE8_P (c
))
3894 c
= CHAR_TO_BYTE8 (c
);
3899 struct charset
*charset
;
3901 if (preferred_charset_id
>= 0)
3903 charset
= CHARSET_FROM_ID (preferred_charset_id
);
3904 if (! CHAR_CHARSET_P (c
, charset
))
3905 charset
= char_charset (c
, charset_list
, NULL
);
3908 charset
= char_charset (c
, charset_list
, NULL
);
3911 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
3913 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
3914 charset
= CHARSET_FROM_ID (charset_ascii
);
3918 c
= coding
->default_char
;
3919 charset
= char_charset (c
, charset_list
, NULL
);
3922 ENCODE_ISO_CHARACTER (charset
, c
);
3926 if (coding
->mode
& CODING_MODE_LAST_BLOCK
3927 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3929 ASSURE_DESTINATION (safe_room
);
3930 ENCODE_RESET_PLANE_AND_REGISTER ();
3932 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
3933 CODING_ISO_BOL (coding
) = bol_designation
;
3934 coding
->produced_char
+= produced_chars
;
3935 coding
->produced
= dst
- coding
->destination
;
3940 /*** 8,9. SJIS and BIG5 handlers ***/
3942 /* Although SJIS and BIG5 are not ISO's coding system, they are used
3943 quite widely. So, for the moment, Emacs supports them in the bare
3944 C code. But, in the future, they may be supported only by CCL. */
3946 /* SJIS is a coding system encoding three character sets: ASCII, right
3947 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3948 as is. A character of charset katakana-jisx0201 is encoded by
3949 "position-code + 0x80". A character of charset japanese-jisx0208
3950 is encoded in 2-byte but two position-codes are divided and shifted
3951 so that it fit in the range below.
3953 --- CODE RANGE of SJIS ---
3954 (character set) (range)
3956 KATAKANA-JISX0201 0xA0 .. 0xDF
3957 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
3958 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
3959 -------------------------------
3963 /* BIG5 is a coding system encoding two character sets: ASCII and
3964 Big5. An ASCII character is encoded as is. Big5 is a two-byte
3965 character set and is encoded in two-byte.
3967 --- CODE RANGE of BIG5 ---
3968 (character set) (range)
3970 Big5 (1st byte) 0xA1 .. 0xFE
3971 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3972 --------------------------
3976 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3977 Check if a text is encoded in SJIS. If it is, return
3978 CATEGORY_MASK_SJIS, else return 0. */
3981 detect_coding_sjis (coding
, detect_info
)
3982 struct coding_system
*coding
;
3983 struct coding_detection_info
*detect_info
;
3985 const unsigned char *src
= coding
->source
, *src_base
;
3986 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3987 int multibytep
= coding
->src_multibyte
;
3988 int consumed_chars
= 0;
3992 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
3993 /* A coding system of this category is always ASCII compatible. */
3994 src
+= coding
->head_ascii
;
4002 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
4005 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4007 found
= CATEGORY_MASK_SJIS
;
4009 else if (c
>= 0xA0 && c
< 0xE0)
4010 found
= CATEGORY_MASK_SJIS
;
4014 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4018 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4020 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4023 detect_info
->found
|= found
;
4027 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4028 Check if a text is encoded in BIG5. If it is, return
4029 CATEGORY_MASK_BIG5, else return 0. */
4032 detect_coding_big5 (coding
, detect_info
)
4033 struct coding_system
*coding
;
4034 struct coding_detection_info
*detect_info
;
4036 const unsigned char *src
= coding
->source
, *src_base
;
4037 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4038 int multibytep
= coding
->src_multibyte
;
4039 int consumed_chars
= 0;
4043 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4044 /* A coding system of this category is always ASCII compatible. */
4045 src
+= coding
->head_ascii
;
4056 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4058 found
= CATEGORY_MASK_BIG5
;
4063 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4067 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4069 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4072 detect_info
->found
|= found
;
4076 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4077 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4080 decode_coding_sjis (coding
)
4081 struct coding_system
*coding
;
4083 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4084 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4085 const unsigned char *src_base
;
4086 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4088 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4089 int consumed_chars
= 0, consumed_chars_base
;
4090 int multibytep
= coding
->src_multibyte
;
4091 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4092 struct charset
*charset_kanji2
;
4093 Lisp_Object attrs
, charset_list
, val
;
4094 int char_offset
= coding
->produced_char
;
4095 int last_offset
= char_offset
;
4096 int last_id
= charset_ascii
;
4098 CODING_GET_INFO (coding
, attrs
, charset_list
);
4101 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4102 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4103 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4104 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4109 struct charset
*charset
;
4112 consumed_chars_base
= consumed_chars
;
4114 if (charbuf
>= charbuf_end
)
4121 charset
= charset_roman
;
4122 else if (c
== 0x80 || c
== 0xA0)
4124 else if (c
>= 0xA1 && c
<= 0xDF)
4126 /* SJIS -> JISX0201-Kana */
4128 charset
= charset_kana
;
4132 /* SJIS -> JISX0208 */
4134 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4138 charset
= charset_kanji
;
4140 else if (c
<= 0xFC && charset_kanji2
)
4142 /* SJIS -> JISX0213-2 */
4144 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4148 charset
= charset_kanji2
;
4152 if (charset
->id
!= charset_ascii
4153 && last_id
!= charset
->id
)
4155 if (last_id
!= charset_ascii
)
4156 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4157 last_id
= charset
->id
;
4158 last_offset
= char_offset
;
4160 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4167 consumed_chars
= consumed_chars_base
;
4169 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4175 if (last_id
!= charset_ascii
)
4176 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4177 coding
->consumed_char
+= consumed_chars_base
;
4178 coding
->consumed
= src_base
- coding
->source
;
4179 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4183 decode_coding_big5 (coding
)
4184 struct coding_system
*coding
;
4186 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4187 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4188 const unsigned char *src_base
;
4189 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4191 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4192 int consumed_chars
= 0, consumed_chars_base
;
4193 int multibytep
= coding
->src_multibyte
;
4194 struct charset
*charset_roman
, *charset_big5
;
4195 Lisp_Object attrs
, charset_list
, val
;
4196 int char_offset
= coding
->produced_char
;
4197 int last_offset
= char_offset
;
4198 int last_id
= charset_ascii
;
4200 CODING_GET_INFO (coding
, attrs
, charset_list
);
4202 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4203 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4208 struct charset
*charset
;
4211 consumed_chars_base
= consumed_chars
;
4213 if (charbuf
>= charbuf_end
)
4221 charset
= charset_roman
;
4225 if (c
< 0xA1 || c
> 0xFE)
4228 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4231 charset
= charset_big5
;
4233 if (charset
->id
!= charset_ascii
4234 && last_id
!= charset
->id
)
4236 if (last_id
!= charset_ascii
)
4237 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4238 last_id
= charset
->id
;
4239 last_offset
= char_offset
;
4241 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4248 consumed_chars
= consumed_chars_base
;
4250 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4256 if (last_id
!= charset_ascii
)
4257 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4258 coding
->consumed_char
+= consumed_chars_base
;
4259 coding
->consumed
= src_base
- coding
->source
;
4260 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4263 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4264 This function can encode charsets `ascii', `katakana-jisx0201',
4265 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4266 are sure that all these charsets are registered as official charset
4267 (i.e. do not have extended leading-codes). Characters of other
4268 charsets are produced without any encoding. If SJIS_P is 1, encode
4269 SJIS text, else encode BIG5 text. */
4272 encode_coding_sjis (coding
)
4273 struct coding_system
*coding
;
4275 int multibytep
= coding
->dst_multibyte
;
4276 int *charbuf
= coding
->charbuf
;
4277 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4278 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4279 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4281 int produced_chars
= 0;
4282 Lisp_Object attrs
, charset_list
, val
;
4283 int ascii_compatible
;
4284 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4285 struct charset
*charset_kanji2
;
4288 CODING_GET_INFO (coding
, attrs
, charset_list
);
4290 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4291 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4292 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4293 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4295 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4297 while (charbuf
< charbuf_end
)
4299 ASSURE_DESTINATION (safe_room
);
4301 /* Now encode the character C. */
4302 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4303 EMIT_ONE_ASCII_BYTE (c
);
4304 else if (CHAR_BYTE8_P (c
))
4306 c
= CHAR_TO_BYTE8 (c
);
4312 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4316 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4318 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4319 charset
= CHARSET_FROM_ID (charset_ascii
);
4323 c
= coding
->default_char
;
4324 charset
= char_charset (c
, charset_list
, &code
);
4327 if (code
== CHARSET_INVALID_CODE (charset
))
4329 if (charset
== charset_kanji
)
4333 c1
= code
>> 8, c2
= code
& 0xFF;
4334 EMIT_TWO_BYTES (c1
, c2
);
4336 else if (charset
== charset_kana
)
4337 EMIT_ONE_BYTE (code
| 0x80);
4338 else if (charset_kanji2
&& charset
== charset_kanji2
)
4343 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4344 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4346 JIS_TO_SJIS2 (code
);
4347 c1
= code
>> 8, c2
= code
& 0xFF;
4348 EMIT_TWO_BYTES (c1
, c2
);
4351 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4354 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4357 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4358 coding
->produced_char
+= produced_chars
;
4359 coding
->produced
= dst
- coding
->destination
;
4364 encode_coding_big5 (coding
)
4365 struct coding_system
*coding
;
4367 int multibytep
= coding
->dst_multibyte
;
4368 int *charbuf
= coding
->charbuf
;
4369 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4370 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4371 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4373 int produced_chars
= 0;
4374 Lisp_Object attrs
, charset_list
, val
;
4375 int ascii_compatible
;
4376 struct charset
*charset_roman
, *charset_big5
;
4379 CODING_GET_INFO (coding
, attrs
, charset_list
);
4381 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4382 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4383 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4385 while (charbuf
< charbuf_end
)
4387 ASSURE_DESTINATION (safe_room
);
4389 /* Now encode the character C. */
4390 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4391 EMIT_ONE_ASCII_BYTE (c
);
4392 else if (CHAR_BYTE8_P (c
))
4394 c
= CHAR_TO_BYTE8 (c
);
4400 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4404 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4406 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4407 charset
= CHARSET_FROM_ID (charset_ascii
);
4411 c
= coding
->default_char
;
4412 charset
= char_charset (c
, charset_list
, &code
);
4415 if (code
== CHARSET_INVALID_CODE (charset
))
4417 if (charset
== charset_big5
)
4421 c1
= code
>> 8, c2
= code
& 0xFF;
4422 EMIT_TWO_BYTES (c1
, c2
);
4425 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4428 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4429 coding
->produced_char
+= produced_chars
;
4430 coding
->produced
= dst
- coding
->destination
;
4435 /*** 10. CCL handlers ***/
4437 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4438 Check if a text is encoded in a coding system of which
4439 encoder/decoder are written in CCL program. If it is, return
4440 CATEGORY_MASK_CCL, else return 0. */
4443 detect_coding_ccl (coding
, detect_info
)
4444 struct coding_system
*coding
;
4445 struct coding_detection_info
*detect_info
;
4447 const unsigned char *src
= coding
->source
, *src_base
;
4448 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4449 int multibytep
= coding
->src_multibyte
;
4450 int consumed_chars
= 0;
4452 unsigned char *valids
;
4453 int head_ascii
= coding
->head_ascii
;
4456 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4458 coding
= &coding_categories
[coding_category_ccl
];
4459 valids
= CODING_CCL_VALIDS (coding
);
4460 attrs
= CODING_ID_ATTRS (coding
->id
);
4461 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4470 if (c
< 0 || ! valids
[c
])
4472 if ((valids
[c
] > 1))
4473 found
= CATEGORY_MASK_CCL
;
4475 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4479 detect_info
->found
|= found
;
4484 decode_coding_ccl (coding
)
4485 struct coding_system
*coding
;
4487 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4488 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4489 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4490 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4491 int consumed_chars
= 0;
4492 int multibytep
= coding
->src_multibyte
;
4493 struct ccl_program ccl
;
4494 int source_charbuf
[1024];
4495 int source_byteidx
[1024];
4496 Lisp_Object attrs
, charset_list
;
4498 CODING_GET_INFO (coding
, attrs
, charset_list
);
4499 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4501 while (src
< src_end
)
4503 const unsigned char *p
= src
;
4504 int *source
, *source_end
;
4508 while (i
< 1024 && p
< src_end
)
4510 source_byteidx
[i
] = p
- src
;
4511 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4514 while (i
< 1024 && p
< src_end
)
4515 source_charbuf
[i
++] = *p
++;
4517 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4520 source
= source_charbuf
;
4521 source_end
= source
+ i
;
4522 while (source
< source_end
)
4524 ccl_driver (&ccl
, source
, charbuf
,
4525 source_end
- source
, charbuf_end
- charbuf
,
4527 source
+= ccl
.consumed
;
4528 charbuf
+= ccl
.produced
;
4529 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4532 if (source
< source_end
)
4533 src
+= source_byteidx
[source
- source_charbuf
];
4536 consumed_chars
+= source
- source_charbuf
;
4538 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4539 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4545 case CCL_STAT_SUSPEND_BY_SRC
:
4546 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4548 case CCL_STAT_SUSPEND_BY_DST
:
4551 case CCL_STAT_INVALID_CMD
:
4552 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4555 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4558 coding
->consumed_char
+= consumed_chars
;
4559 coding
->consumed
= src
- coding
->source
;
4560 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4564 encode_coding_ccl (coding
)
4565 struct coding_system
*coding
;
4567 struct ccl_program ccl
;
4568 int multibytep
= coding
->dst_multibyte
;
4569 int *charbuf
= coding
->charbuf
;
4570 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4571 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4572 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4573 int destination_charbuf
[1024];
4574 int i
, produced_chars
= 0;
4575 Lisp_Object attrs
, charset_list
;
4577 CODING_GET_INFO (coding
, attrs
, charset_list
);
4578 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4580 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4581 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4583 while (charbuf
< charbuf_end
)
4585 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4586 charbuf_end
- charbuf
, 1024, charset_list
);
4589 ASSURE_DESTINATION (ccl
.produced
* 2);
4590 for (i
= 0; i
< ccl
.produced
; i
++)
4591 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4595 ASSURE_DESTINATION (ccl
.produced
);
4596 for (i
= 0; i
< ccl
.produced
; i
++)
4597 *dst
++ = destination_charbuf
[i
] & 0xFF;
4598 produced_chars
+= ccl
.produced
;
4600 charbuf
+= ccl
.consumed
;
4601 if (ccl
.status
== CCL_STAT_QUIT
4602 || ccl
.status
== CCL_STAT_INVALID_CMD
)
4608 case CCL_STAT_SUSPEND_BY_SRC
:
4609 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4611 case CCL_STAT_SUSPEND_BY_DST
:
4612 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4615 case CCL_STAT_INVALID_CMD
:
4616 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4619 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4623 coding
->produced_char
+= produced_chars
;
4624 coding
->produced
= dst
- coding
->destination
;
4630 /*** 10, 11. no-conversion handlers ***/
4632 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4635 decode_coding_raw_text (coding
)
4636 struct coding_system
*coding
;
4638 coding
->chars_at_source
= 1;
4639 coding
->consumed_char
= 0;
4640 coding
->consumed
= 0;
4641 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4645 encode_coding_raw_text (coding
)
4646 struct coding_system
*coding
;
4648 int multibytep
= coding
->dst_multibyte
;
4649 int *charbuf
= coding
->charbuf
;
4650 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
4651 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4652 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4653 int produced_chars
= 0;
4658 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4660 if (coding
->src_multibyte
)
4661 while (charbuf
< charbuf_end
)
4663 ASSURE_DESTINATION (safe_room
);
4665 if (ASCII_CHAR_P (c
))
4666 EMIT_ONE_ASCII_BYTE (c
);
4667 else if (CHAR_BYTE8_P (c
))
4669 c
= CHAR_TO_BYTE8 (c
);
4674 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4676 CHAR_STRING_ADVANCE (c
, p1
);
4679 EMIT_ONE_BYTE (*p0
);
4685 while (charbuf
< charbuf_end
)
4687 ASSURE_DESTINATION (safe_room
);
4694 if (coding
->src_multibyte
)
4696 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4698 while (charbuf
< charbuf_end
)
4700 ASSURE_DESTINATION (safe_room
);
4702 if (ASCII_CHAR_P (c
))
4704 else if (CHAR_BYTE8_P (c
))
4705 *dst
++ = CHAR_TO_BYTE8 (c
);
4707 CHAR_STRING_ADVANCE (c
, dst
);
4713 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4714 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4715 *dst
++ = *charbuf
++;
4716 produced_chars
= dst
- (coding
->destination
+ coding
->dst_bytes
);
4719 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4720 coding
->produced_char
+= produced_chars
;
4721 coding
->produced
= dst
- coding
->destination
;
4725 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4726 Check if a text is encoded in a charset-based coding system. If it
4727 is, return 1, else return 0. */
4730 detect_coding_charset (coding
, detect_info
)
4731 struct coding_system
*coding
;
4732 struct coding_detection_info
*detect_info
;
4734 const unsigned char *src
= coding
->source
, *src_base
;
4735 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4736 int multibytep
= coding
->src_multibyte
;
4737 int consumed_chars
= 0;
4738 Lisp_Object attrs
, valids
;
4740 int head_ascii
= coding
->head_ascii
;
4742 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4744 coding
= &coding_categories
[coding_category_charset
];
4745 attrs
= CODING_ID_ATTRS (coding
->id
);
4746 valids
= AREF (attrs
, coding_attr_charset_valids
);
4748 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4755 struct charset
*charset
;
4762 val
= AREF (valids
, c
);
4766 found
= CATEGORY_MASK_CHARSET
;
4769 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4770 dim
= CHARSET_DIMENSION (charset
);
4771 for (idx
= 1; idx
< dim
; idx
++)
4776 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
4777 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
4786 for (; CONSP (val
); val
= XCDR (val
))
4788 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4789 dim
= CHARSET_DIMENSION (charset
);
4795 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
4796 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
4811 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4815 detect_info
->found
|= found
;
4820 decode_coding_charset (coding
)
4821 struct coding_system
*coding
;
4823 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4824 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4825 const unsigned char *src_base
;
4826 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4828 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4829 int consumed_chars
= 0, consumed_chars_base
;
4830 int multibytep
= coding
->src_multibyte
;
4831 Lisp_Object attrs
, charset_list
, valids
;
4832 int char_offset
= coding
->produced_char
;
4833 int last_offset
= char_offset
;
4834 int last_id
= charset_ascii
;
4836 CODING_GET_INFO (coding
, attrs
, charset_list
);
4837 valids
= AREF (attrs
, coding_attr_charset_valids
);
4843 struct charset
*charset
;
4849 consumed_chars_base
= consumed_chars
;
4851 if (charbuf
>= charbuf_end
)
4859 val
= AREF (valids
, c
);
4864 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4865 dim
= CHARSET_DIMENSION (charset
);
4869 code
= (code
<< 8) | c
;
4872 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
4877 /* VAL is a list of charset IDs. It is assured that the
4878 list is sorted by charset dimensions (smaller one
4882 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4883 dim
= CHARSET_DIMENSION (charset
);
4887 code
= (code
<< 8) | c
;
4890 CODING_DECODE_CHAR (coding
, src
, src_base
,
4891 src_end
, charset
, code
, c
);
4899 if (charset
->id
!= charset_ascii
4900 && last_id
!= charset
->id
)
4902 if (last_id
!= charset_ascii
)
4903 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4904 last_id
= charset
->id
;
4905 last_offset
= char_offset
;
4914 consumed_chars
= consumed_chars_base
;
4916 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4922 if (last_id
!= charset_ascii
)
4923 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4924 coding
->consumed_char
+= consumed_chars_base
;
4925 coding
->consumed
= src_base
- coding
->source
;
4926 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4930 encode_coding_charset (coding
)
4931 struct coding_system
*coding
;
4933 int multibytep
= coding
->dst_multibyte
;
4934 int *charbuf
= coding
->charbuf
;
4935 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4936 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4937 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4938 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4939 int produced_chars
= 0;
4940 Lisp_Object attrs
, charset_list
;
4941 int ascii_compatible
;
4944 CODING_GET_INFO (coding
, attrs
, charset_list
);
4945 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4947 while (charbuf
< charbuf_end
)
4949 struct charset
*charset
;
4952 ASSURE_DESTINATION (safe_room
);
4954 if (ascii_compatible
&& ASCII_CHAR_P (c
))
4955 EMIT_ONE_ASCII_BYTE (c
);
4956 else if (CHAR_BYTE8_P (c
))
4958 c
= CHAR_TO_BYTE8 (c
);
4963 charset
= char_charset (c
, charset_list
, &code
);
4966 if (CHARSET_DIMENSION (charset
) == 1)
4967 EMIT_ONE_BYTE (code
);
4968 else if (CHARSET_DIMENSION (charset
) == 2)
4969 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
4970 else if (CHARSET_DIMENSION (charset
) == 3)
4971 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
4973 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
4974 (code
>> 8) & 0xFF, code
& 0xFF);
4978 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4979 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4981 c
= coding
->default_char
;
4987 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4988 coding
->produced_char
+= produced_chars
;
4989 coding
->produced
= dst
- coding
->destination
;
4994 /*** 7. C library functions ***/
4996 /* Setup coding context CODING from information about CODING_SYSTEM.
4997 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4998 CODING_SYSTEM is invalid, signal an error. */
5001 setup_coding_system (coding_system
, coding
)
5002 Lisp_Object coding_system
;
5003 struct coding_system
*coding
;
5006 Lisp_Object eol_type
;
5007 Lisp_Object coding_type
;
5010 if (NILP (coding_system
))
5011 coding_system
= Qundecided
;
5013 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5015 attrs
= CODING_ID_ATTRS (coding
->id
);
5016 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5019 coding
->head_ascii
= -1;
5020 if (VECTORP (eol_type
))
5021 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5022 | CODING_REQUIRE_DETECTION_MASK
);
5023 else if (! EQ (eol_type
, Qunix
))
5024 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5025 | CODING_REQUIRE_ENCODING_MASK
);
5027 coding
->common_flags
= 0;
5028 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5029 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5030 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5031 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5032 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5033 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5035 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5036 coding
->max_charset_id
= SCHARS (val
) - 1;
5037 coding
->safe_charsets
= (char *) SDATA (val
);
5038 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5040 coding_type
= CODING_ATTR_TYPE (attrs
);
5041 if (EQ (coding_type
, Qundecided
))
5043 coding
->detector
= NULL
;
5044 coding
->decoder
= decode_coding_raw_text
;
5045 coding
->encoder
= encode_coding_raw_text
;
5046 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5048 else if (EQ (coding_type
, Qiso_2022
))
5051 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5053 /* Invoke graphic register 0 to plane 0. */
5054 CODING_ISO_INVOCATION (coding
, 0) = 0;
5055 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5056 CODING_ISO_INVOCATION (coding
, 1)
5057 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5058 /* Setup the initial status of designation. */
5059 for (i
= 0; i
< 4; i
++)
5060 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5061 /* Not single shifting initially. */
5062 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5063 /* Beginning of buffer should also be regarded as bol. */
5064 CODING_ISO_BOL (coding
) = 1;
5065 coding
->detector
= detect_coding_iso_2022
;
5066 coding
->decoder
= decode_coding_iso_2022
;
5067 coding
->encoder
= encode_coding_iso_2022
;
5068 if (flags
& CODING_ISO_FLAG_SAFE
)
5069 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5070 coding
->common_flags
5071 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5072 | CODING_REQUIRE_FLUSHING_MASK
);
5073 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5074 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5075 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5076 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5077 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5079 setup_iso_safe_charsets (attrs
);
5080 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5081 coding
->max_charset_id
= SCHARS (val
) - 1;
5082 coding
->safe_charsets
= (char *) SDATA (val
);
5084 CODING_ISO_FLAGS (coding
) = flags
;
5086 else if (EQ (coding_type
, Qcharset
))
5088 coding
->detector
= detect_coding_charset
;
5089 coding
->decoder
= decode_coding_charset
;
5090 coding
->encoder
= encode_coding_charset
;
5091 coding
->common_flags
5092 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5094 else if (EQ (coding_type
, Qutf_8
))
5096 coding
->detector
= detect_coding_utf_8
;
5097 coding
->decoder
= decode_coding_utf_8
;
5098 coding
->encoder
= encode_coding_utf_8
;
5099 coding
->common_flags
5100 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5102 else if (EQ (coding_type
, Qutf_16
))
5104 val
= AREF (attrs
, coding_attr_utf_16_bom
);
5105 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
5106 : EQ (val
, Qt
) ? utf_16_with_bom
5107 : utf_16_without_bom
);
5108 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5109 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5110 : utf_16_little_endian
);
5111 CODING_UTF_16_SURROGATE (coding
) = 0;
5112 coding
->detector
= detect_coding_utf_16
;
5113 coding
->decoder
= decode_coding_utf_16
;
5114 coding
->encoder
= encode_coding_utf_16
;
5115 coding
->common_flags
5116 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5117 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
5118 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5120 else if (EQ (coding_type
, Qccl
))
5122 coding
->detector
= detect_coding_ccl
;
5123 coding
->decoder
= decode_coding_ccl
;
5124 coding
->encoder
= encode_coding_ccl
;
5125 coding
->common_flags
5126 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5127 | CODING_REQUIRE_FLUSHING_MASK
);
5129 else if (EQ (coding_type
, Qemacs_mule
))
5131 coding
->detector
= detect_coding_emacs_mule
;
5132 coding
->decoder
= decode_coding_emacs_mule
;
5133 coding
->encoder
= encode_coding_emacs_mule
;
5134 coding
->common_flags
5135 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5136 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5137 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5139 Lisp_Object tail
, safe_charsets
;
5140 int max_charset_id
= 0;
5142 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5144 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5145 max_charset_id
= XFASTINT (XCAR (tail
));
5146 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5148 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5150 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5151 coding
->max_charset_id
= max_charset_id
;
5152 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5155 else if (EQ (coding_type
, Qshift_jis
))
5157 coding
->detector
= detect_coding_sjis
;
5158 coding
->decoder
= decode_coding_sjis
;
5159 coding
->encoder
= encode_coding_sjis
;
5160 coding
->common_flags
5161 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5163 else if (EQ (coding_type
, Qbig5
))
5165 coding
->detector
= detect_coding_big5
;
5166 coding
->decoder
= decode_coding_big5
;
5167 coding
->encoder
= encode_coding_big5
;
5168 coding
->common_flags
5169 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5171 else /* EQ (coding_type, Qraw_text) */
5173 coding
->detector
= NULL
;
5174 coding
->decoder
= decode_coding_raw_text
;
5175 coding
->encoder
= encode_coding_raw_text
;
5176 if (! EQ (eol_type
, Qunix
))
5178 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5179 if (! VECTORP (eol_type
))
5180 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5188 /* Return a list of charsets supported by CODING. */
5191 coding_charset_list (coding
)
5192 struct coding_system
*coding
;
5194 Lisp_Object attrs
, charset_list
;
5196 CODING_GET_INFO (coding
, attrs
, charset_list
);
5197 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5199 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5201 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5202 charset_list
= Viso_2022_charset_list
;
5204 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5206 charset_list
= Vemacs_mule_charset_list
;
5208 return charset_list
;
5212 /* Return raw-text or one of its subsidiaries that has the same
5213 eol_type as CODING-SYSTEM. */
5216 raw_text_coding_system (coding_system
)
5217 Lisp_Object coding_system
;
5219 Lisp_Object spec
, attrs
;
5220 Lisp_Object eol_type
, raw_text_eol_type
;
5222 if (NILP (coding_system
))
5224 spec
= CODING_SYSTEM_SPEC (coding_system
);
5225 attrs
= AREF (spec
, 0);
5227 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5228 return coding_system
;
5230 eol_type
= AREF (spec
, 2);
5231 if (VECTORP (eol_type
))
5233 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5234 raw_text_eol_type
= AREF (spec
, 2);
5235 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5236 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5237 : AREF (raw_text_eol_type
, 2));
5241 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5242 does, return one of the subsidiary that has the same eol-spec as
5243 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5244 inherit end-of-line format from the system's setting
5245 (system_eol_type). */
5248 coding_inherit_eol_type (coding_system
, parent
)
5249 Lisp_Object coding_system
, parent
;
5251 Lisp_Object spec
, eol_type
;
5253 if (NILP (coding_system
))
5254 coding_system
= Qraw_text
;
5255 spec
= CODING_SYSTEM_SPEC (coding_system
);
5256 eol_type
= AREF (spec
, 2);
5257 if (VECTORP (eol_type
))
5259 Lisp_Object parent_eol_type
;
5261 if (! NILP (parent
))
5263 Lisp_Object parent_spec
;
5265 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5266 parent_eol_type
= AREF (parent_spec
, 2);
5269 parent_eol_type
= system_eol_type
;
5270 if (EQ (parent_eol_type
, Qunix
))
5271 coding_system
= AREF (eol_type
, 0);
5272 else if (EQ (parent_eol_type
, Qdos
))
5273 coding_system
= AREF (eol_type
, 1);
5274 else if (EQ (parent_eol_type
, Qmac
))
5275 coding_system
= AREF (eol_type
, 2);
5277 return coding_system
;
5280 /* Emacs has a mechanism to automatically detect a coding system if it
5281 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5282 it's impossible to distinguish some coding systems accurately
5283 because they use the same range of codes. So, at first, coding
5284 systems are categorized into 7, those are:
5286 o coding-category-emacs-mule
5288 The category for a coding system which has the same code range
5289 as Emacs' internal format. Assigned the coding-system (Lisp
5290 symbol) `emacs-mule' by default.
5292 o coding-category-sjis
5294 The category for a coding system which has the same code range
5295 as SJIS. Assigned the coding-system (Lisp
5296 symbol) `japanese-shift-jis' by default.
5298 o coding-category-iso-7
5300 The category for a coding system which has the same code range
5301 as ISO2022 of 7-bit environment. This doesn't use any locking
5302 shift and single shift functions. This can encode/decode all
5303 charsets. Assigned the coding-system (Lisp symbol)
5304 `iso-2022-7bit' by default.
5306 o coding-category-iso-7-tight
5308 Same as coding-category-iso-7 except that this can
5309 encode/decode only the specified charsets.
5311 o coding-category-iso-8-1
5313 The category for a coding system which has the same code range
5314 as ISO2022 of 8-bit environment and graphic plane 1 used only
5315 for DIMENSION1 charset. This doesn't use any locking shift
5316 and single shift functions. Assigned the coding-system (Lisp
5317 symbol) `iso-latin-1' by default.
5319 o coding-category-iso-8-2
5321 The category for a coding system which has the same code range
5322 as ISO2022 of 8-bit environment and graphic plane 1 used only
5323 for DIMENSION2 charset. This doesn't use any locking shift
5324 and single shift functions. Assigned the coding-system (Lisp
5325 symbol) `japanese-iso-8bit' by default.
5327 o coding-category-iso-7-else
5329 The category for a coding system which has the same code range
5330 as ISO2022 of 7-bit environemnt but uses locking shift or
5331 single shift functions. Assigned the coding-system (Lisp
5332 symbol) `iso-2022-7bit-lock' by default.
5334 o coding-category-iso-8-else
5336 The category for a coding system which has the same code range
5337 as ISO2022 of 8-bit environemnt but uses locking shift or
5338 single shift functions. Assigned the coding-system (Lisp
5339 symbol) `iso-2022-8bit-ss2' by default.
5341 o coding-category-big5
5343 The category for a coding system which has the same code range
5344 as BIG5. Assigned the coding-system (Lisp symbol)
5345 `cn-big5' by default.
5347 o coding-category-utf-8
5349 The category for a coding system which has the same code range
5350 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5351 symbol) `utf-8' by default.
5353 o coding-category-utf-16-be
5355 The category for a coding system in which a text has an
5356 Unicode signature (cf. Unicode Standard) in the order of BIG
5357 endian at the head. Assigned the coding-system (Lisp symbol)
5358 `utf-16-be' by default.
5360 o coding-category-utf-16-le
5362 The category for a coding system in which a text has an
5363 Unicode signature (cf. Unicode Standard) in the order of
5364 LITTLE endian at the head. Assigned the coding-system (Lisp
5365 symbol) `utf-16-le' by default.
5367 o coding-category-ccl
5369 The category for a coding system of which encoder/decoder is
5370 written in CCL programs. The default value is nil, i.e., no
5371 coding system is assigned.
5373 o coding-category-binary
5375 The category for a coding system not categorized in any of the
5376 above. Assigned the coding-system (Lisp symbol)
5377 `no-conversion' by default.
5379 Each of them is a Lisp symbol and the value is an actual
5380 `coding-system's (this is also a Lisp symbol) assigned by a user.
5381 What Emacs does actually is to detect a category of coding system.
5382 Then, it uses a `coding-system' assigned to it. If Emacs can't
5383 decide only one possible category, it selects a category of the
5384 highest priority. Priorities of categories are also specified by a
5385 user in a Lisp variable `coding-category-list'.
5389 #define EOL_SEEN_NONE 0
5390 #define EOL_SEEN_LF 1
5391 #define EOL_SEEN_CR 2
5392 #define EOL_SEEN_CRLF 4
5394 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5395 SOURCE is encoded. If CATEGORY is one of
5396 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5397 two-byte, else they are encoded by one-byte.
5399 Return one of EOL_SEEN_XXX. */
5401 #define MAX_EOL_CHECK_COUNT 3
5404 detect_eol (source
, src_bytes
, category
)
5405 const unsigned char *source
;
5406 EMACS_INT src_bytes
;
5407 enum coding_category category
;
5409 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5412 int eol_seen
= EOL_SEEN_NONE
;
5414 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5418 msb
= category
== (coding_category_utf_16_le
5419 | coding_category_utf_16_le_nosig
);
5422 while (src
+ 1 < src_end
)
5425 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5430 this_eol
= EOL_SEEN_LF
;
5431 else if (src
+ 3 >= src_end
5432 || src
[msb
+ 2] != 0
5433 || src
[lsb
+ 2] != '\n')
5434 this_eol
= EOL_SEEN_CR
;
5436 this_eol
= EOL_SEEN_CRLF
;
5438 if (eol_seen
== EOL_SEEN_NONE
)
5439 /* This is the first end-of-line. */
5440 eol_seen
= this_eol
;
5441 else if (eol_seen
!= this_eol
)
5443 /* The found type is different from what found before. */
5444 eol_seen
= EOL_SEEN_LF
;
5447 if (++total
== MAX_EOL_CHECK_COUNT
)
5455 while (src
< src_end
)
5458 if (c
== '\n' || c
== '\r')
5463 this_eol
= EOL_SEEN_LF
;
5464 else if (src
>= src_end
|| *src
!= '\n')
5465 this_eol
= EOL_SEEN_CR
;
5467 this_eol
= EOL_SEEN_CRLF
, src
++;
5469 if (eol_seen
== EOL_SEEN_NONE
)
5470 /* This is the first end-of-line. */
5471 eol_seen
= this_eol
;
5472 else if (eol_seen
!= this_eol
)
5474 /* The found type is different from what found before. */
5475 eol_seen
= EOL_SEEN_LF
;
5478 if (++total
== MAX_EOL_CHECK_COUNT
)
5488 adjust_coding_eol_type (coding
, eol_seen
)
5489 struct coding_system
*coding
;
5492 Lisp_Object eol_type
;
5494 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5495 if (eol_seen
& EOL_SEEN_LF
)
5497 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5500 else if (eol_seen
& EOL_SEEN_CRLF
)
5502 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5505 else if (eol_seen
& EOL_SEEN_CR
)
5507 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5513 /* Detect how a text specified in CODING is encoded. If a coding
5514 system is detected, update fields of CODING by the detected coding
5518 detect_coding (coding
)
5519 struct coding_system
*coding
;
5521 const unsigned char *src
, *src_end
;
5523 coding
->consumed
= coding
->consumed_char
= 0;
5524 coding
->produced
= coding
->produced_char
= 0;
5525 coding_set_source (coding
);
5527 src_end
= coding
->source
+ coding
->src_bytes
;
5529 /* If we have not yet decided the text encoding type, detect it
5531 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5534 struct coding_detection_info detect_info
;
5536 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5537 for (i
= 0, src
= coding
->source
; src
< src_end
; i
++, src
++)
5543 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5544 && ! inhibit_iso_escape_detection
5545 && ! detect_info
.checked
)
5547 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5548 if (detect_coding_iso_2022 (coding
, &detect_info
))
5550 /* We have scanned the whole data. */
5551 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5552 /* We didn't find an 8-bit code. */
5558 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5560 if (coding
->head_ascii
< coding
->src_bytes
5561 || detect_info
.found
)
5563 enum coding_category category
;
5564 struct coding_system
*this;
5566 if (coding
->head_ascii
== coding
->src_bytes
)
5567 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5568 for (i
= 0; i
< coding_category_raw_text
; i
++)
5570 category
= coding_priorities
[i
];
5571 this = coding_categories
+ category
;
5572 if (detect_info
.found
& (1 << category
))
5576 for (i
= 0; i
< coding_category_raw_text
; i
++)
5578 category
= coding_priorities
[i
];
5579 this = coding_categories
+ category
;
5582 /* No coding system of this category is defined. */
5583 detect_info
.rejected
|= (1 << category
);
5585 else if (category
>= coding_category_raw_text
)
5587 else if (detect_info
.checked
& (1 << category
))
5589 if (detect_info
.found
& (1 << category
))
5592 else if ((*(this->detector
)) (coding
, &detect_info
)
5593 && detect_info
.found
& (1 << category
))
5595 if (category
== coding_category_utf_16_auto
)
5597 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5598 category
= coding_category_utf_16_le
;
5600 category
= coding_category_utf_16_be
;
5606 if (i
< coding_category_raw_text
)
5607 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5608 else if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
5609 setup_coding_system (Qraw_text
, coding
);
5610 else if (detect_info
.rejected
)
5611 for (i
= 0; i
< coding_category_raw_text
; i
++)
5612 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5614 this = coding_categories
+ coding_priorities
[i
];
5615 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5620 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5621 == coding_category_utf_16_auto
)
5623 Lisp_Object coding_systems
;
5624 struct coding_detection_info detect_info
;
5627 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5628 detect_info
.found
= detect_info
.rejected
= 0;
5629 if (CONSP (coding_systems
)
5630 && detect_coding_utf_16 (coding
, &detect_info
))
5632 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5633 setup_coding_system (XCAR (coding_systems
), coding
);
5634 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5635 setup_coding_system (XCDR (coding_systems
), coding
);
5643 struct coding_system
*coding
;
5645 Lisp_Object eol_type
;
5646 unsigned char *p
, *pbeg
, *pend
;
5648 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5649 if (EQ (eol_type
, Qunix
))
5652 if (NILP (coding
->dst_object
))
5653 pbeg
= coding
->destination
;
5655 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5656 pend
= pbeg
+ coding
->produced
;
5658 if (VECTORP (eol_type
))
5660 int eol_seen
= EOL_SEEN_NONE
;
5662 for (p
= pbeg
; p
< pend
; p
++)
5665 eol_seen
|= EOL_SEEN_LF
;
5666 else if (*p
== '\r')
5668 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5670 eol_seen
|= EOL_SEEN_CRLF
;
5674 eol_seen
|= EOL_SEEN_CR
;
5677 if (eol_seen
!= EOL_SEEN_NONE
5678 && eol_seen
!= EOL_SEEN_LF
5679 && eol_seen
!= EOL_SEEN_CRLF
5680 && eol_seen
!= EOL_SEEN_CR
)
5681 eol_seen
= EOL_SEEN_LF
;
5682 if (eol_seen
!= EOL_SEEN_NONE
)
5683 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5686 if (EQ (eol_type
, Qmac
))
5688 for (p
= pbeg
; p
< pend
; p
++)
5692 else if (EQ (eol_type
, Qdos
))
5696 if (NILP (coding
->dst_object
))
5698 /* Start deleting '\r' from the tail to minimize the memory
5700 for (p
= pend
- 2; p
>= pbeg
; p
--)
5703 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5709 int pos_byte
= coding
->dst_pos_byte
;
5710 int pos
= coding
->dst_pos
;
5711 int pos_end
= pos
+ coding
->produced_char
- 1;
5713 while (pos
< pos_end
)
5715 p
= BYTE_POS_ADDR (pos_byte
);
5716 if (*p
== '\r' && p
[1] == '\n')
5718 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5723 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
5726 coding
->produced
-= n
;
5727 coding
->produced_char
-= n
;
5732 /* Return a translation table (or list of them) from coding system
5733 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5734 decoding (ENCODEP is zero). */
5737 get_translation_table (attrs
, encodep
, max_lookup
)
5739 int encodep
, *max_lookup
;
5741 Lisp_Object standard
, translation_table
;
5745 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5746 standard
= Vstandard_translation_table_for_encode
;
5748 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5749 standard
= Vstandard_translation_table_for_decode
;
5750 if (NILP (translation_table
))
5751 translation_table
= standard
;
5754 if (SYMBOLP (translation_table
))
5755 translation_table
= Fget (translation_table
, Qtranslation_table
);
5756 else if (CONSP (translation_table
))
5758 translation_table
= Fcopy_sequence (translation_table
);
5759 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5760 if (SYMBOLP (XCAR (val
)))
5761 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5763 if (CHAR_TABLE_P (standard
))
5765 if (CONSP (translation_table
))
5766 translation_table
= nconc2 (translation_table
,
5767 Fcons (standard
, Qnil
));
5769 translation_table
= Fcons (translation_table
,
5770 Fcons (standard
, Qnil
));
5777 if (CHAR_TABLE_P (translation_table
)
5778 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5780 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5781 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5782 *max_lookup
= XFASTINT (val
);
5784 else if (CONSP (translation_table
))
5786 Lisp_Object tail
, val
;
5788 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
5789 if (CHAR_TABLE_P (XCAR (tail
))
5790 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
5792 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
5793 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5794 *max_lookup
= XFASTINT (val
);
5798 return translation_table
;
5801 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5804 if (CHAR_TABLE_P (table)) \
5806 trans = CHAR_TABLE_REF (table, c); \
5807 if (CHARACTERP (trans)) \
5808 c = XFASTINT (trans), trans = Qnil; \
5810 else if (CONSP (table)) \
5814 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5815 if (CHAR_TABLE_P (XCAR (tail))) \
5817 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5818 if (CHARACTERP (trans)) \
5819 c = XFASTINT (trans), trans = Qnil; \
5820 else if (! NILP (trans)) \
5828 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
5832 int *from_nchars
, *to_nchars
;
5834 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5838 Lisp_Object from
, tail
;
5841 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
5846 for (i
= 0; i
< len
; i
++)
5848 if (buf
+ i
== buf_end
)
5854 if (XINT (AREF (from
, i
)) != buf
[i
])
5868 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
5876 produce_chars (coding
, translation_table
, last_block
)
5877 struct coding_system
*coding
;
5878 Lisp_Object translation_table
;
5881 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5882 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5884 int produced_chars
= 0;
5887 if (! coding
->chars_at_source
)
5889 /* Characters are in coding->charbuf. */
5890 int *buf
= coding
->charbuf
;
5891 int *buf_end
= buf
+ coding
->charbuf_used
;
5893 if (BUFFERP (coding
->src_object
)
5894 && EQ (coding
->src_object
, coding
->dst_object
))
5895 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
5897 while (buf
< buf_end
)
5903 int from_nchars
= 1, to_nchars
= 1;
5904 Lisp_Object trans
= Qnil
;
5906 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
5909 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
5910 &from_nchars
, &to_nchars
);
5916 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
5918 dst
= alloc_destination (coding
,
5920 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
5922 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5925 for (i
= 0; i
< to_nchars
; i
++)
5928 c
= XINT (AREF (trans
, i
));
5929 if (coding
->dst_multibyte
5930 || ! CHAR_BYTE8_P (c
))
5931 CHAR_STRING_ADVANCE (c
, dst
);
5933 *dst
++ = CHAR_TO_BYTE8 (c
);
5935 produced_chars
+= to_nchars
;
5937 while (--from_nchars
> 0)
5941 /* This is an annotation datum. (-C) is the length. */
5944 carryover
= buf_end
- buf
;
5948 const unsigned char *src
= coding
->source
;
5949 const unsigned char *src_end
= src
+ coding
->src_bytes
;
5950 Lisp_Object eol_type
;
5952 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5954 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
5956 if (coding
->src_multibyte
)
5963 const unsigned char *src_base
= src
;
5969 if (EQ (eol_type
, Qdos
))
5973 record_conversion_result
5974 (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5975 goto no_more_source
;
5980 else if (EQ (eol_type
, Qmac
))
5985 coding
->consumed
= src
- coding
->source
;
5987 if (EQ (coding
->src_object
, coding
->dst_object
))
5988 dst_end
= (unsigned char *) src
;
5991 dst
= alloc_destination (coding
, src_end
- src
+ 1,
5993 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5994 coding_set_source (coding
);
5995 src
= coding
->source
+ coding
->consumed
;
5996 src_end
= coding
->source
+ coding
->src_bytes
;
6006 while (src
< src_end
)
6013 if (EQ (eol_type
, Qdos
))
6019 else if (EQ (eol_type
, Qmac
))
6022 if (dst
>= dst_end
- 1)
6024 coding
->consumed
= src
- coding
->source
;
6026 if (EQ (coding
->src_object
, coding
->dst_object
))
6027 dst_end
= (unsigned char *) src
;
6028 if (dst
>= dst_end
- 1)
6030 dst
= alloc_destination (coding
, src_end
- src
+ 2,
6032 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6033 coding_set_source (coding
);
6034 src
= coding
->source
+ coding
->consumed
;
6035 src_end
= coding
->source
+ coding
->src_bytes
;
6043 if (!EQ (coding
->src_object
, coding
->dst_object
))
6045 int require
= coding
->src_bytes
- coding
->dst_bytes
;
6049 EMACS_INT offset
= src
- coding
->source
;
6051 dst
= alloc_destination (coding
, require
, dst
);
6052 coding_set_source (coding
);
6053 src
= coding
->source
+ offset
;
6054 src_end
= coding
->source
+ coding
->src_bytes
;
6057 produced_chars
= coding
->src_chars
;
6058 while (src
< src_end
)
6064 if (EQ (eol_type
, Qdos
))
6071 else if (EQ (eol_type
, Qmac
))
6077 coding
->consumed
= coding
->src_bytes
;
6078 coding
->consumed_char
= coding
->src_chars
;
6081 produced
= dst
- (coding
->destination
+ coding
->produced
);
6082 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6083 insert_from_gap (produced_chars
, produced
);
6084 coding
->produced
+= produced
;
6085 coding
->produced_char
+= produced_chars
;
6089 /* Compose text in CODING->object according to the annotation data at
6090 CHARBUF. CHARBUF is an array:
6091 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6095 produce_composition (coding
, charbuf
, pos
)
6096 struct coding_system
*coding
;
6102 enum composition_method method
;
6103 Lisp_Object components
;
6106 to
= pos
+ charbuf
[2];
6109 method
= (enum composition_method
) (charbuf
[3]);
6111 if (method
== COMPOSITION_RELATIVE
)
6113 else if (method
>= COMPOSITION_WITH_RULE
6114 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6116 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6121 for (i
= 0; i
< len
; i
++)
6123 args
[i
] = make_number (charbuf
[i
]);
6127 components
= (method
== COMPOSITION_WITH_ALTCHARS
6128 ? Fstring (len
, args
) : Fvector (len
, args
));
6132 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6136 /* Put `charset' property on text in CODING->object according to
6137 the annotation data at CHARBUF. CHARBUF is an array:
6138 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6142 produce_charset (coding
, charbuf
, pos
)
6143 struct coding_system
*coding
;
6147 EMACS_INT from
= pos
- charbuf
[2];
6148 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6150 Fput_text_property (make_number (from
), make_number (pos
),
6151 Qcharset
, CHARSET_NAME (charset
),
6152 coding
->dst_object
);
6156 #define CHARBUF_SIZE 0x4000
6158 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6160 int size = CHARBUF_SIZE;; \
6162 coding->charbuf = NULL; \
6163 while (size > 1024) \
6165 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6166 if (coding->charbuf) \
6170 if (! coding->charbuf) \
6172 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6173 return coding->result; \
6175 coding->charbuf_size = size; \
6180 produce_annotation (coding
, pos
)
6181 struct coding_system
*coding
;
6184 int *charbuf
= coding
->charbuf
;
6185 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6187 if (NILP (coding
->dst_object
))
6190 while (charbuf
< charbuf_end
)
6196 int len
= -*charbuf
;
6199 case CODING_ANNOTATE_COMPOSITION_MASK
:
6200 produce_composition (coding
, charbuf
, pos
);
6202 case CODING_ANNOTATE_CHARSET_MASK
:
6203 produce_charset (coding
, charbuf
, pos
);
6213 /* Decode the data at CODING->src_object into CODING->dst_object.
6214 CODING->src_object is a buffer, a string, or nil.
6215 CODING->dst_object is a buffer.
6217 If CODING->src_object is a buffer, it must be the current buffer.
6218 In this case, if CODING->src_pos is positive, it is a position of
6219 the source text in the buffer, otherwise, the source text is in the
6220 gap area of the buffer, and CODING->src_pos specifies the offset of
6221 the text from GPT (which must be the same as PT). If this is the
6222 same buffer as CODING->dst_object, CODING->src_pos must be
6225 If CODING->src_object is a string, CODING->src_pos is an index to
6228 If CODING->src_object is nil, CODING->source must already point to
6229 the non-relocatable memory area. In this case, CODING->src_pos is
6230 an offset from CODING->source.
6232 The decoded data is inserted at the current point of the buffer
6237 decode_coding (coding
)
6238 struct coding_system
*coding
;
6241 Lisp_Object undo_list
;
6242 Lisp_Object translation_table
;
6246 if (BUFFERP (coding
->src_object
)
6247 && coding
->src_pos
> 0
6248 && coding
->src_pos
< GPT
6249 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6250 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6253 if (BUFFERP (coding
->dst_object
))
6255 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6256 set_buffer_internal (XBUFFER (coding
->dst_object
));
6258 move_gap_both (PT
, PT_BYTE
);
6259 undo_list
= current_buffer
->undo_list
;
6260 current_buffer
->undo_list
= Qt
;
6263 coding
->consumed
= coding
->consumed_char
= 0;
6264 coding
->produced
= coding
->produced_char
= 0;
6265 coding
->chars_at_source
= 0;
6266 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6269 ALLOC_CONVERSION_WORK_AREA (coding
);
6271 attrs
= CODING_ID_ATTRS (coding
->id
);
6272 translation_table
= get_translation_table (attrs
, 0, NULL
);
6277 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6279 coding_set_source (coding
);
6280 coding
->annotated
= 0;
6281 coding
->charbuf_used
= carryover
;
6282 (*(coding
->decoder
)) (coding
);
6283 coding_set_destination (coding
);
6284 carryover
= produce_chars (coding
, translation_table
, 0);
6285 if (coding
->annotated
)
6286 produce_annotation (coding
, pos
);
6287 for (i
= 0; i
< carryover
; i
++)
6289 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6291 while (coding
->consumed
< coding
->src_bytes
6292 && (coding
->result
== CODING_RESULT_SUCCESS
6293 || coding
->result
== CODING_RESULT_INVALID_SRC
));
6297 coding_set_destination (coding
);
6298 coding
->charbuf_used
= carryover
;
6299 produce_chars (coding
, translation_table
, 1);
6302 coding
->carryover_bytes
= 0;
6303 if (coding
->consumed
< coding
->src_bytes
)
6305 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6306 const unsigned char *src
;
6308 coding_set_source (coding
);
6309 coding_set_destination (coding
);
6310 src
= coding
->source
+ coding
->consumed
;
6312 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6314 /* Flush out unprocessed data as binary chars. We are sure
6315 that the number of data is less than the size of
6317 coding
->charbuf_used
= 0;
6318 while (nbytes
-- > 0)
6323 c
= BYTE8_TO_CHAR (c
);
6324 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6326 produce_chars (coding
, Qnil
, 1);
6330 /* Record unprocessed bytes in coding->carryover. We are
6331 sure that the number of data is less than the size of
6332 coding->carryover. */
6333 unsigned char *p
= coding
->carryover
;
6335 coding
->carryover_bytes
= nbytes
;
6336 while (nbytes
-- > 0)
6339 coding
->consumed
= coding
->src_bytes
;
6342 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6343 decode_eol (coding
);
6344 if (BUFFERP (coding
->dst_object
))
6346 current_buffer
->undo_list
= undo_list
;
6347 record_insert (coding
->dst_pos
, coding
->produced_char
);
6349 return coding
->result
;
6353 /* Extract an annotation datum from a composition starting at POS and
6354 ending before LIMIT of CODING->src_object (buffer or string), store
6355 the data in BUF, set *STOP to a starting position of the next
6356 composition (if any) or to LIMIT, and return the address of the
6357 next element of BUF.
6359 If such an annotation is not found, set *STOP to a starting
6360 position of a composition after POS (if any) or to LIMIT, and
6364 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6365 EMACS_INT pos
, limit
;
6366 struct coding_system
*coding
;
6370 EMACS_INT start
, end
;
6373 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6376 else if (start
> pos
)
6382 /* We found a composition. Store the corresponding
6383 annotation data in BUF. */
6385 enum composition_method method
= COMPOSITION_METHOD (prop
);
6386 int nchars
= COMPOSITION_LENGTH (prop
);
6388 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6389 if (method
!= COMPOSITION_RELATIVE
)
6391 Lisp_Object components
;
6394 components
= COMPOSITION_COMPONENTS (prop
);
6395 if (VECTORP (components
))
6397 len
= XVECTOR (components
)->size
;
6398 for (i
= 0; i
< len
; i
++)
6399 *buf
++ = XINT (AREF (components
, i
));
6401 else if (STRINGP (components
))
6403 len
= SCHARS (components
);
6407 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6411 else if (INTEGERP (components
))
6414 *buf
++ = XINT (components
);
6416 else if (CONSP (components
))
6418 for (len
= 0; CONSP (components
);
6419 len
++, components
= XCDR (components
))
6420 *buf
++ = XINT (XCAR (components
));
6428 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6439 /* Extract an annotation datum from a text property `charset' at POS of
6440 CODING->src_object (buffer of string), store the data in BUF, set
6441 *STOP to the position where the value of `charset' property changes
6442 (limiting by LIMIT), and return the address of the next element of
6445 If the property value is nil, set *STOP to the position where the
6446 property value is non-nil (limiting by LIMIT), and return BUF. */
6449 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6450 EMACS_INT pos
, limit
;
6451 struct coding_system
*coding
;
6455 Lisp_Object val
, next
;
6458 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6459 if (! NILP (val
) && CHARSETP (val
))
6460 id
= XINT (CHARSET_SYMBOL_ID (val
));
6463 ADD_CHARSET_DATA (buf
, 0, id
);
6464 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6466 make_number (limit
));
6467 *stop
= XINT (next
);
6473 consume_chars (coding
, translation_table
, max_lookup
)
6474 struct coding_system
*coding
;
6475 Lisp_Object translation_table
;
6478 int *buf
= coding
->charbuf
;
6479 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6480 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6481 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6482 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6483 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6484 int multibytep
= coding
->src_multibyte
;
6485 Lisp_Object eol_type
;
6487 EMACS_INT stop
, stop_composition
, stop_charset
;
6488 int *lookup_buf
= NULL
;
6490 if (! NILP (translation_table
))
6491 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6493 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6494 if (VECTORP (eol_type
))
6497 /* Note: composition handling is not yet implemented. */
6498 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6500 if (NILP (coding
->src_object
))
6501 stop
= stop_composition
= stop_charset
= end_pos
;
6504 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6505 stop
= stop_composition
= pos
;
6507 stop
= stop_composition
= end_pos
;
6508 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6509 stop
= stop_charset
= pos
;
6511 stop_charset
= end_pos
;
6514 /* Compensate for CRLF and conversion. */
6515 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6516 while (buf
< buf_end
)
6524 if (pos
== stop_composition
)
6525 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6526 buf
, &stop_composition
);
6527 if (pos
== stop_charset
)
6528 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6529 buf
, &stop_charset
);
6530 stop
= (stop_composition
< stop_charset
6531 ? stop_composition
: stop_charset
);
6538 if (coding
->encoder
== encode_coding_raw_text
)
6540 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6541 c
= STRING_CHAR_ADVANCE (src
), pos
+= bytes
;
6543 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
6546 c
= STRING_CHAR_ADVANCE (src
), pos
++;
6547 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6549 if (! EQ (eol_type
, Qunix
))
6553 if (EQ (eol_type
, Qdos
))
6561 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6566 int from_nchars
= 1, to_nchars
= 1;
6567 int *lookup_buf_end
;
6568 const unsigned char *p
= src
;
6572 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6573 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6574 lookup_buf_end
= lookup_buf
+ i
;
6575 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6576 &from_nchars
, &to_nchars
);
6578 || buf
+ to_nchars
> buf_end
)
6580 *buf
++ = *lookup_buf
;
6581 for (i
= 1; i
< to_nchars
; i
++)
6582 *buf
++ = XINT (AREF (trans
, i
));
6583 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6584 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6588 coding
->consumed
= src
- coding
->source
;
6589 coding
->consumed_char
= pos
- coding
->src_pos
;
6590 coding
->charbuf_used
= buf
- coding
->charbuf
;
6591 coding
->chars_at_source
= 0;
6595 /* Encode the text at CODING->src_object into CODING->dst_object.
6596 CODING->src_object is a buffer or a string.
6597 CODING->dst_object is a buffer or nil.
6599 If CODING->src_object is a buffer, it must be the current buffer.
6600 In this case, if CODING->src_pos is positive, it is a position of
6601 the source text in the buffer, otherwise. the source text is in the
6602 gap area of the buffer, and coding->src_pos specifies the offset of
6603 the text from GPT (which must be the same as PT). If this is the
6604 same buffer as CODING->dst_object, CODING->src_pos must be
6605 negative and CODING should not have `pre-write-conversion'.
6607 If CODING->src_object is a string, CODING should not have
6608 `pre-write-conversion'.
6610 If CODING->dst_object is a buffer, the encoded data is inserted at
6611 the current point of that buffer.
6613 If CODING->dst_object is nil, the encoded data is placed at the
6614 memory area specified by CODING->destination. */
6617 encode_coding (coding
)
6618 struct coding_system
*coding
;
6621 Lisp_Object translation_table
;
6624 attrs
= CODING_ID_ATTRS (coding
->id
);
6625 if (coding
->encoder
== encode_coding_raw_text
)
6626 translation_table
= Qnil
, max_lookup
= 0;
6628 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6630 if (BUFFERP (coding
->dst_object
))
6632 set_buffer_internal (XBUFFER (coding
->dst_object
));
6633 coding
->dst_multibyte
6634 = ! NILP (current_buffer
->enable_multibyte_characters
);
6637 coding
->consumed
= coding
->consumed_char
= 0;
6638 coding
->produced
= coding
->produced_char
= 0;
6639 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6642 ALLOC_CONVERSION_WORK_AREA (coding
);
6645 coding_set_source (coding
);
6646 consume_chars (coding
, translation_table
, max_lookup
);
6647 coding_set_destination (coding
);
6648 (*(coding
->encoder
)) (coding
);
6649 } while (coding
->consumed_char
< coding
->src_chars
);
6651 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
6652 insert_from_gap (coding
->produced_char
, coding
->produced
);
6654 return (coding
->result
);
6658 /* Name (or base name) of work buffer for code conversion. */
6659 static Lisp_Object Vcode_conversion_workbuf_name
;
6661 /* A working buffer used by the top level conversion. Once it is
6662 created, it is never destroyed. It has the name
6663 Vcode_conversion_workbuf_name. The other working buffers are
6664 destroyed after the use is finished, and their names are modified
6665 versions of Vcode_conversion_workbuf_name. */
6666 static Lisp_Object Vcode_conversion_reused_workbuf
;
6668 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6669 static int reused_workbuf_in_use
;
6672 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6673 multibyteness of returning buffer. */
6676 make_conversion_work_buffer (multibyte
)
6679 Lisp_Object name
, workbuf
;
6680 struct buffer
*current
;
6682 if (reused_workbuf_in_use
++)
6684 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6685 workbuf
= Fget_buffer_create (name
);
6689 name
= Vcode_conversion_workbuf_name
;
6690 workbuf
= Fget_buffer_create (name
);
6691 if (NILP (Vcode_conversion_reused_workbuf
))
6692 Vcode_conversion_reused_workbuf
= workbuf
;
6694 current
= current_buffer
;
6695 set_buffer_internal (XBUFFER (workbuf
));
6697 current_buffer
->undo_list
= Qt
;
6698 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6699 set_buffer_internal (current
);
6705 code_conversion_restore (arg
)
6708 Lisp_Object current
, workbuf
;
6709 struct gcpro gcpro1
;
6712 current
= XCAR (arg
);
6713 workbuf
= XCDR (arg
);
6714 if (! NILP (workbuf
))
6716 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6717 reused_workbuf_in_use
= 0;
6718 else if (! NILP (Fbuffer_live_p (workbuf
)))
6719 Fkill_buffer (workbuf
);
6721 set_buffer_internal (XBUFFER (current
));
6727 code_conversion_save (with_work_buf
, multibyte
)
6728 int with_work_buf
, multibyte
;
6730 Lisp_Object workbuf
= Qnil
;
6733 workbuf
= make_conversion_work_buffer (multibyte
);
6734 record_unwind_protect (code_conversion_restore
,
6735 Fcons (Fcurrent_buffer (), workbuf
));
6740 decode_coding_gap (coding
, chars
, bytes
)
6741 struct coding_system
*coding
;
6742 EMACS_INT chars
, bytes
;
6744 int count
= specpdl_ptr
- specpdl
;
6747 code_conversion_save (0, 0);
6749 coding
->src_object
= Fcurrent_buffer ();
6750 coding
->src_chars
= chars
;
6751 coding
->src_bytes
= bytes
;
6752 coding
->src_pos
= -chars
;
6753 coding
->src_pos_byte
= -bytes
;
6754 coding
->src_multibyte
= chars
< bytes
;
6755 coding
->dst_object
= coding
->src_object
;
6756 coding
->dst_pos
= PT
;
6757 coding
->dst_pos_byte
= PT_BYTE
;
6758 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6760 if (CODING_REQUIRE_DETECTION (coding
))
6761 detect_coding (coding
);
6763 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6764 current_buffer
->text
->inhibit_shrinking
= 1;
6765 decode_coding (coding
);
6766 current_buffer
->text
->inhibit_shrinking
= 0;
6768 attrs
= CODING_ID_ATTRS (coding
->id
);
6769 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6771 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6774 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6775 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6776 make_number (coding
->produced_char
));
6778 coding
->produced_char
+= Z
- prev_Z
;
6779 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6782 unbind_to (count
, Qnil
);
6783 return coding
->result
;
6787 encode_coding_gap (coding
, chars
, bytes
)
6788 struct coding_system
*coding
;
6789 EMACS_INT chars
, bytes
;
6791 int count
= specpdl_ptr
- specpdl
;
6793 code_conversion_save (0, 0);
6795 coding
->src_object
= Fcurrent_buffer ();
6796 coding
->src_chars
= chars
;
6797 coding
->src_bytes
= bytes
;
6798 coding
->src_pos
= -chars
;
6799 coding
->src_pos_byte
= -bytes
;
6800 coding
->src_multibyte
= chars
< bytes
;
6801 coding
->dst_object
= coding
->src_object
;
6802 coding
->dst_pos
= PT
;
6803 coding
->dst_pos_byte
= PT_BYTE
;
6805 encode_coding (coding
);
6807 unbind_to (count
, Qnil
);
6808 return coding
->result
;
6812 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6813 SRC_OBJECT into DST_OBJECT by coding context CODING.
6815 SRC_OBJECT is a buffer, a string, or Qnil.
6817 If it is a buffer, the text is at point of the buffer. FROM and TO
6818 are positions in the buffer.
6820 If it is a string, the text is at the beginning of the string.
6821 FROM and TO are indices to the string.
6823 If it is nil, the text is at coding->source. FROM and TO are
6824 indices to coding->source.
6826 DST_OBJECT is a buffer, Qt, or Qnil.
6828 If it is a buffer, the decoded text is inserted at point of the
6829 buffer. If the buffer is the same as SRC_OBJECT, the source text
6832 If it is Qt, a string is made from the decoded text, and
6833 set in CODING->dst_object.
6835 If it is Qnil, the decoded text is stored at CODING->destination.
6836 The caller must allocate CODING->dst_bytes bytes at
6837 CODING->destination by xmalloc. If the decoded text is longer than
6838 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6842 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6844 struct coding_system
*coding
;
6845 Lisp_Object src_object
;
6846 EMACS_INT from
, from_byte
, to
, to_byte
;
6847 Lisp_Object dst_object
;
6849 int count
= specpdl_ptr
- specpdl
;
6850 unsigned char *destination
;
6851 EMACS_INT dst_bytes
;
6852 EMACS_INT chars
= to
- from
;
6853 EMACS_INT bytes
= to_byte
- from_byte
;
6856 int saved_pt
= -1, saved_pt_byte
;
6858 buffer
= Fcurrent_buffer ();
6860 if (NILP (dst_object
))
6862 destination
= coding
->destination
;
6863 dst_bytes
= coding
->dst_bytes
;
6866 coding
->src_object
= src_object
;
6867 coding
->src_chars
= chars
;
6868 coding
->src_bytes
= bytes
;
6869 coding
->src_multibyte
= chars
< bytes
;
6871 if (STRINGP (src_object
))
6873 coding
->src_pos
= from
;
6874 coding
->src_pos_byte
= from_byte
;
6876 else if (BUFFERP (src_object
))
6878 set_buffer_internal (XBUFFER (src_object
));
6880 move_gap_both (from
, from_byte
);
6881 if (EQ (src_object
, dst_object
))
6883 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6884 TEMP_SET_PT_BOTH (from
, from_byte
);
6885 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6886 coding
->src_pos
= -chars
;
6887 coding
->src_pos_byte
= -bytes
;
6891 coding
->src_pos
= from
;
6892 coding
->src_pos_byte
= from_byte
;
6896 if (CODING_REQUIRE_DETECTION (coding
))
6897 detect_coding (coding
);
6898 attrs
= CODING_ID_ATTRS (coding
->id
);
6900 if (EQ (dst_object
, Qt
)
6901 || (! NILP (CODING_ATTR_POST_READ (attrs
))
6902 && NILP (dst_object
)))
6904 coding
->dst_object
= code_conversion_save (1, 1);
6905 coding
->dst_pos
= BEG
;
6906 coding
->dst_pos_byte
= BEG_BYTE
;
6907 coding
->dst_multibyte
= 1;
6909 else if (BUFFERP (dst_object
))
6911 code_conversion_save (0, 0);
6912 coding
->dst_object
= dst_object
;
6913 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6914 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6915 coding
->dst_multibyte
6916 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6920 code_conversion_save (0, 0);
6921 coding
->dst_object
= Qnil
;
6922 coding
->dst_multibyte
= 1;
6925 decode_coding (coding
);
6927 if (BUFFERP (coding
->dst_object
))
6928 set_buffer_internal (XBUFFER (coding
->dst_object
));
6930 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6932 struct gcpro gcpro1
, gcpro2
;
6933 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6936 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6937 GCPRO2 (coding
->src_object
, coding
->dst_object
);
6938 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
6939 make_number (coding
->produced_char
));
6942 coding
->produced_char
+= Z
- prev_Z
;
6943 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6946 if (EQ (dst_object
, Qt
))
6948 coding
->dst_object
= Fbuffer_string ();
6950 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
6952 set_buffer_internal (XBUFFER (coding
->dst_object
));
6953 if (dst_bytes
< coding
->produced
)
6956 = (unsigned char *) xrealloc (destination
, coding
->produced
);
6959 record_conversion_result (coding
,
6960 CODING_RESULT_INSUFFICIENT_DST
);
6961 unbind_to (count
, Qnil
);
6964 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
6965 move_gap_both (BEGV
, BEGV_BYTE
);
6966 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
6967 coding
->destination
= destination
;
6973 /* This is the case of:
6974 (BUFFERP (src_object) && EQ (src_object, dst_object))
6975 As we have moved PT while replacing the original buffer
6976 contents, we must recover it now. */
6977 set_buffer_internal (XBUFFER (src_object
));
6978 if (saved_pt
< from
)
6979 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
6980 else if (saved_pt
< from
+ chars
)
6981 TEMP_SET_PT_BOTH (from
, from_byte
);
6982 else if (! NILP (current_buffer
->enable_multibyte_characters
))
6983 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
6984 saved_pt_byte
+ (coding
->produced
- bytes
));
6986 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
6987 saved_pt_byte
+ (coding
->produced
- bytes
));
6990 unbind_to (count
, coding
->dst_object
);
6995 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6997 struct coding_system
*coding
;
6998 Lisp_Object src_object
;
6999 EMACS_INT from
, from_byte
, to
, to_byte
;
7000 Lisp_Object dst_object
;
7002 int count
= specpdl_ptr
- specpdl
;
7003 EMACS_INT chars
= to
- from
;
7004 EMACS_INT bytes
= to_byte
- from_byte
;
7007 int saved_pt
= -1, saved_pt_byte
;
7008 int kill_src_buffer
= 0;
7010 buffer
= Fcurrent_buffer ();
7012 coding
->src_object
= src_object
;
7013 coding
->src_chars
= chars
;
7014 coding
->src_bytes
= bytes
;
7015 coding
->src_multibyte
= chars
< bytes
;
7017 attrs
= CODING_ID_ATTRS (coding
->id
);
7019 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7021 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7022 set_buffer_internal (XBUFFER (coding
->src_object
));
7023 if (STRINGP (src_object
))
7024 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7025 else if (BUFFERP (src_object
))
7026 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7028 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7030 if (EQ (src_object
, dst_object
))
7032 set_buffer_internal (XBUFFER (src_object
));
7033 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7034 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7035 set_buffer_internal (XBUFFER (coding
->src_object
));
7039 Lisp_Object args
[3];
7041 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7042 args
[1] = make_number (BEG
);
7043 args
[2] = make_number (Z
);
7044 safe_call (3, args
);
7046 if (XBUFFER (coding
->src_object
) != current_buffer
)
7047 kill_src_buffer
= 1;
7048 coding
->src_object
= Fcurrent_buffer ();
7050 move_gap_both (BEG
, BEG_BYTE
);
7051 coding
->src_chars
= Z
- BEG
;
7052 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7053 coding
->src_pos
= BEG
;
7054 coding
->src_pos_byte
= BEG_BYTE
;
7055 coding
->src_multibyte
= Z
< Z_BYTE
;
7057 else if (STRINGP (src_object
))
7059 code_conversion_save (0, 0);
7060 coding
->src_pos
= from
;
7061 coding
->src_pos_byte
= from_byte
;
7063 else if (BUFFERP (src_object
))
7065 code_conversion_save (0, 0);
7066 set_buffer_internal (XBUFFER (src_object
));
7067 if (EQ (src_object
, dst_object
))
7069 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7070 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7071 coding
->src_pos
= 0;
7072 coding
->src_pos_byte
= 0;
7076 if (from
< GPT
&& to
>= GPT
)
7077 move_gap_both (from
, from_byte
);
7078 coding
->src_pos
= from
;
7079 coding
->src_pos_byte
= from_byte
;
7083 code_conversion_save (0, 0);
7085 if (BUFFERP (dst_object
))
7087 coding
->dst_object
= dst_object
;
7088 if (EQ (src_object
, dst_object
))
7090 coding
->dst_pos
= from
;
7091 coding
->dst_pos_byte
= from_byte
;
7095 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7096 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7098 coding
->dst_multibyte
7099 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7101 else if (EQ (dst_object
, Qt
))
7103 coding
->dst_object
= Qnil
;
7104 coding
->dst_bytes
= coding
->src_chars
;
7105 if (coding
->dst_bytes
== 0)
7106 coding
->dst_bytes
= 1;
7107 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7108 coding
->dst_multibyte
= 0;
7112 coding
->dst_object
= Qnil
;
7113 coding
->dst_multibyte
= 0;
7116 encode_coding (coding
);
7118 if (EQ (dst_object
, Qt
))
7120 if (BUFFERP (coding
->dst_object
))
7121 coding
->dst_object
= Fbuffer_string ();
7125 = make_unibyte_string ((char *) coding
->destination
,
7127 xfree (coding
->destination
);
7133 /* This is the case of:
7134 (BUFFERP (src_object) && EQ (src_object, dst_object))
7135 As we have moved PT while replacing the original buffer
7136 contents, we must recover it now. */
7137 set_buffer_internal (XBUFFER (src_object
));
7138 if (saved_pt
< from
)
7139 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7140 else if (saved_pt
< from
+ chars
)
7141 TEMP_SET_PT_BOTH (from
, from_byte
);
7142 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7143 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7144 saved_pt_byte
+ (coding
->produced
- bytes
));
7146 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7147 saved_pt_byte
+ (coding
->produced
- bytes
));
7150 if (kill_src_buffer
)
7151 Fkill_buffer (coding
->src_object
);
7152 unbind_to (count
, Qnil
);
7157 preferred_coding_system ()
7159 int id
= coding_categories
[coding_priorities
[0]].id
;
7161 return CODING_ID_NAME (id
);
7166 /*** 8. Emacs Lisp library functions ***/
7168 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7169 doc
: /* Return t if OBJECT is nil or a coding-system.
7170 See the documentation of `define-coding-system' for information
7171 about coding-system objects. */)
7176 || CODING_SYSTEM_ID (obj
) >= 0)
7179 || NILP (Fget (obj
, Qcoding_system_define_form
)))
7184 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7185 Sread_non_nil_coding_system
, 1, 1, 0,
7186 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7193 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7194 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7196 while (SCHARS (val
) == 0);
7197 return (Fintern (val
, Qnil
));
7200 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7201 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7202 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
7203 (prompt
, default_coding_system
)
7204 Lisp_Object prompt
, default_coding_system
;
7207 if (SYMBOLP (default_coding_system
))
7208 XSETSTRING (default_coding_system
, XPNTR (SYMBOL_NAME (default_coding_system
)));
7209 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7210 Qt
, Qnil
, Qcoding_system_history
,
7211 default_coding_system
, Qnil
);
7212 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7215 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7217 doc
: /* Check validity of CODING-SYSTEM.
7218 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7219 It is valid if it is nil or a symbol defined as a coding system by the
7220 function `define-coding-system'. */)
7222 Lisp_Object coding_system
;
7224 Lisp_Object define_form
;
7226 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7227 if (! NILP (define_form
))
7229 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7230 safe_eval (define_form
);
7232 if (!NILP (Fcoding_system_p (coding_system
)))
7233 return coding_system
;
7234 xsignal1 (Qcoding_system_error
, coding_system
);
7238 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7239 HIGHEST is nonzero, return the coding system of the highest
7240 priority among the detected coding systems. Otherwize return a
7241 list of detected coding systems sorted by their priorities. If
7242 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7243 multibyte form but contains only ASCII and eight-bit chars.
7244 Otherwise, the bytes are raw bytes.
7246 CODING-SYSTEM controls the detection as below:
7248 If it is nil, detect both text-format and eol-format. If the
7249 text-format part of CODING-SYSTEM is already specified
7250 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7251 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7252 detect only text-format. */
7255 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7257 const unsigned char *src
;
7258 int src_chars
, src_bytes
, highest
;
7260 Lisp_Object coding_system
;
7262 const unsigned char *src_end
= src
+ src_bytes
;
7263 Lisp_Object attrs
, eol_type
;
7265 struct coding_system coding
;
7267 struct coding_detection_info detect_info
;
7268 enum coding_category base_category
;
7270 if (NILP (coding_system
))
7271 coding_system
= Qundecided
;
7272 setup_coding_system (coding_system
, &coding
);
7273 attrs
= CODING_ID_ATTRS (coding
.id
);
7274 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7275 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7277 coding
.source
= src
;
7278 coding
.src_chars
= src_chars
;
7279 coding
.src_bytes
= src_bytes
;
7280 coding
.src_multibyte
= multibytep
;
7281 coding
.consumed
= 0;
7282 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7284 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7286 /* At first, detect text-format if necessary. */
7287 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7288 if (base_category
== coding_category_undecided
)
7290 enum coding_category category
;
7291 struct coding_system
*this;
7294 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7295 for (i
= 0; src
< src_end
; i
++, src
++)
7301 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7302 && ! inhibit_iso_escape_detection
)
7304 coding
.head_ascii
= src
- coding
.source
;
7305 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7307 /* We have scanned the whole data. */
7308 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7309 /* We didn't find an 8-bit code. */
7315 coding
.head_ascii
= src
- coding
.source
;
7318 || detect_info
.found
)
7321 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7322 for (i
= 0; i
< coding_category_raw_text
; i
++)
7324 category
= coding_priorities
[i
];
7325 this = coding_categories
+ category
;
7326 if (detect_info
.found
& (1 << category
))
7330 for (i
= 0; i
< coding_category_raw_text
; i
++)
7332 category
= coding_priorities
[i
];
7333 this = coding_categories
+ category
;
7337 /* No coding system of this category is defined. */
7338 detect_info
.rejected
|= (1 << category
);
7340 else if (category
>= coding_category_raw_text
)
7342 else if (detect_info
.checked
& (1 << category
))
7345 && (detect_info
.found
& (1 << category
)))
7350 if ((*(this->detector
)) (&coding
, &detect_info
)
7352 && (detect_info
.found
& (1 << category
)))
7354 if (category
== coding_category_utf_16_auto
)
7356 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7357 category
= coding_category_utf_16_le
;
7359 category
= coding_category_utf_16_be
;
7367 if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
7369 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7370 id
= coding_categories
[coding_category_raw_text
].id
;
7371 val
= Fcons (make_number (id
), Qnil
);
7373 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7375 detect_info
.found
= CATEGORY_MASK_ANY
;
7376 id
= coding_categories
[coding_category_undecided
].id
;
7377 val
= Fcons (make_number (id
), Qnil
);
7381 if (detect_info
.found
)
7383 detect_info
.found
= 1 << category
;
7384 val
= Fcons (make_number (this->id
), Qnil
);
7387 for (i
= 0; i
< coding_category_raw_text
; i
++)
7388 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7390 detect_info
.found
= 1 << coding_priorities
[i
];
7391 id
= coding_categories
[coding_priorities
[i
]].id
;
7392 val
= Fcons (make_number (id
), Qnil
);
7398 int mask
= detect_info
.rejected
| detect_info
.found
;
7402 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7404 category
= coding_priorities
[i
];
7405 if (! (mask
& (1 << category
)))
7407 found
|= 1 << category
;
7408 id
= coding_categories
[category
].id
;
7410 val
= Fcons (make_number (id
), val
);
7413 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7415 category
= coding_priorities
[i
];
7416 if (detect_info
.found
& (1 << category
))
7418 id
= coding_categories
[category
].id
;
7419 val
= Fcons (make_number (id
), val
);
7422 detect_info
.found
|= found
;
7425 else if (base_category
== coding_category_utf_16_auto
)
7427 if (detect_coding_utf_16 (&coding
, &detect_info
))
7429 struct coding_system
*this;
7431 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7432 this = coding_categories
+ coding_category_utf_16_le
;
7433 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7434 this = coding_categories
+ coding_category_utf_16_be
;
7435 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7436 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7438 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7439 val
= Fcons (make_number (this->id
), Qnil
);
7444 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7445 val
= Fcons (make_number (coding
.id
), Qnil
);
7448 /* Then, detect eol-format if necessary. */
7450 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7453 if (VECTORP (eol_type
))
7455 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7456 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7457 coding_category_raw_text
);
7458 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7459 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7460 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7461 coding_category_utf_16_be
);
7462 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7463 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7464 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7465 coding_category_utf_16_le
);
7469 if (EQ (eol_type
, Qunix
))
7470 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7471 else if (EQ (eol_type
, Qdos
))
7472 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7474 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7477 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7479 enum coding_category category
;
7482 id
= XINT (XCAR (tail
));
7483 attrs
= CODING_ID_ATTRS (id
);
7484 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7485 eol_type
= CODING_ID_EOL_TYPE (id
);
7486 if (VECTORP (eol_type
))
7488 if (category
== coding_category_utf_16_be
7489 || category
== coding_category_utf_16_be_nosig
)
7490 this_eol
= utf_16_be_eol
;
7491 else if (category
== coding_category_utf_16_le
7492 || category
== coding_category_utf_16_le_nosig
)
7493 this_eol
= utf_16_le_eol
;
7495 this_eol
= normal_eol
;
7497 if (this_eol
== EOL_SEEN_LF
)
7498 XSETCAR (tail
, AREF (eol_type
, 0));
7499 else if (this_eol
== EOL_SEEN_CRLF
)
7500 XSETCAR (tail
, AREF (eol_type
, 1));
7501 else if (this_eol
== EOL_SEEN_CR
)
7502 XSETCAR (tail
, AREF (eol_type
, 2));
7504 XSETCAR (tail
, CODING_ID_NAME (id
));
7507 XSETCAR (tail
, CODING_ID_NAME (id
));
7511 return (highest
? XCAR (val
) : val
);
7515 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7517 doc
: /* Detect coding system of the text in the region between START and END.
7518 Return a list of possible coding systems ordered by priority.
7520 If only ASCII characters are found (except for such ISO-2022 control
7521 characters ISO-2022 as ESC), it returns a list of single element
7522 `undecided' or its subsidiary coding system according to a detected
7525 If optional argument HIGHEST is non-nil, return the coding system of
7526 highest priority. */)
7527 (start
, end
, highest
)
7528 Lisp_Object start
, end
, highest
;
7531 int from_byte
, to_byte
;
7533 CHECK_NUMBER_COERCE_MARKER (start
);
7534 CHECK_NUMBER_COERCE_MARKER (end
);
7536 validate_region (&start
, &end
);
7537 from
= XINT (start
), to
= XINT (end
);
7538 from_byte
= CHAR_TO_BYTE (from
);
7539 to_byte
= CHAR_TO_BYTE (to
);
7541 if (from
< GPT
&& to
>= GPT
)
7542 move_gap_both (to
, to_byte
);
7544 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7545 to
- from
, to_byte
- from_byte
,
7547 !NILP (current_buffer
7548 ->enable_multibyte_characters
),
7552 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7554 doc
: /* Detect coding system of the text in STRING.
7555 Return a list of possible coding systems ordered by priority.
7557 If only ASCII characters are found (except for such ISO-2022 control
7558 characters ISO-2022 as ESC), it returns a list of single element
7559 `undecided' or its subsidiary coding system according to a detected
7562 If optional argument HIGHEST is non-nil, return the coding system of
7563 highest priority. */)
7565 Lisp_Object string
, highest
;
7567 CHECK_STRING (string
);
7569 return detect_coding_system (SDATA (string
),
7570 SCHARS (string
), SBYTES (string
),
7571 !NILP (highest
), STRING_MULTIBYTE (string
),
7577 char_encodable_p (c
, attrs
)
7582 struct charset
*charset
;
7583 Lisp_Object translation_table
;
7585 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7586 if (! NILP (translation_table
))
7587 c
= translate_char (translation_table
, c
);
7588 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7589 CONSP (tail
); tail
= XCDR (tail
))
7591 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7592 if (CHAR_CHARSET_P (c
, charset
))
7595 return (! NILP (tail
));
7599 /* Return a list of coding systems that safely encode the text between
7600 START and END. If EXCLUDE is non-nil, it is a list of coding
7601 systems not to check. The returned list doesn't contain any such
7602 coding systems. In any case, if the text contains only ASCII or is
7603 unibyte, return t. */
7605 DEFUN ("find-coding-systems-region-internal",
7606 Ffind_coding_systems_region_internal
,
7607 Sfind_coding_systems_region_internal
, 2, 3, 0,
7608 doc
: /* Internal use only. */)
7609 (start
, end
, exclude
)
7610 Lisp_Object start
, end
, exclude
;
7612 Lisp_Object coding_attrs_list
, safe_codings
;
7613 EMACS_INT start_byte
, end_byte
;
7614 const unsigned char *p
, *pbeg
, *pend
;
7616 Lisp_Object tail
, elt
;
7618 if (STRINGP (start
))
7620 if (!STRING_MULTIBYTE (start
)
7621 || SCHARS (start
) == SBYTES (start
))
7624 end_byte
= SBYTES (start
);
7628 CHECK_NUMBER_COERCE_MARKER (start
);
7629 CHECK_NUMBER_COERCE_MARKER (end
);
7630 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7631 args_out_of_range (start
, end
);
7632 if (NILP (current_buffer
->enable_multibyte_characters
))
7634 start_byte
= CHAR_TO_BYTE (XINT (start
));
7635 end_byte
= CHAR_TO_BYTE (XINT (end
));
7636 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7639 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7641 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7642 move_gap_both (XINT (start
), start_byte
);
7644 move_gap_both (XINT (end
), end_byte
);
7648 coding_attrs_list
= Qnil
;
7649 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7651 || NILP (Fmemq (XCAR (tail
), exclude
)))
7655 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7656 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7657 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7659 ASET (attrs
, coding_attr_trans_tbl
,
7660 get_translation_table (attrs
, 1, NULL
));
7661 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7665 if (STRINGP (start
))
7666 p
= pbeg
= SDATA (start
);
7668 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7669 pend
= p
+ (end_byte
- start_byte
);
7671 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7672 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7676 if (ASCII_BYTE_P (*p
))
7680 c
= STRING_CHAR_ADVANCE (p
);
7682 charset_map_loaded
= 0;
7683 for (tail
= coding_attrs_list
; CONSP (tail
);)
7688 else if (char_encodable_p (c
, elt
))
7690 else if (CONSP (XCDR (tail
)))
7692 XSETCAR (tail
, XCAR (XCDR (tail
)));
7693 XSETCDR (tail
, XCDR (XCDR (tail
)));
7697 XSETCAR (tail
, Qnil
);
7701 if (charset_map_loaded
)
7703 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7705 if (STRINGP (start
))
7706 pbeg
= SDATA (start
);
7708 pbeg
= BYTE_POS_ADDR (start_byte
);
7709 p
= pbeg
+ p_offset
;
7710 pend
= pbeg
+ pend_offset
;
7715 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
7716 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
7717 if (! NILP (XCAR (tail
)))
7718 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
7720 return safe_codings
;
7724 DEFUN ("unencodable-char-position", Funencodable_char_position
,
7725 Sunencodable_char_position
, 3, 5, 0,
7727 Return position of first un-encodable character in a region.
7728 START and END specfiy the region and CODING-SYSTEM specifies the
7729 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7731 If optional 4th argument COUNT is non-nil, it specifies at most how
7732 many un-encodable characters to search. In this case, the value is a
7735 If optional 5th argument STRING is non-nil, it is a string to search
7736 for un-encodable characters. In that case, START and END are indexes
7738 (start
, end
, coding_system
, count
, string
)
7739 Lisp_Object start
, end
, coding_system
, count
, string
;
7742 struct coding_system coding
;
7743 Lisp_Object attrs
, charset_list
, translation_table
;
7744 Lisp_Object positions
;
7746 const unsigned char *p
, *stop
, *pend
;
7747 int ascii_compatible
;
7749 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
7750 attrs
= CODING_ID_ATTRS (coding
.id
);
7751 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
7753 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
7754 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7755 translation_table
= get_translation_table (attrs
, 1, NULL
);
7759 validate_region (&start
, &end
);
7760 from
= XINT (start
);
7762 if (NILP (current_buffer
->enable_multibyte_characters
)
7763 || (ascii_compatible
7764 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
7766 p
= CHAR_POS_ADDR (from
);
7767 pend
= CHAR_POS_ADDR (to
);
7768 if (from
< GPT
&& to
>= GPT
)
7775 CHECK_STRING (string
);
7776 CHECK_NATNUM (start
);
7778 from
= XINT (start
);
7781 || to
> SCHARS (string
))
7782 args_out_of_range_3 (string
, start
, end
);
7783 if (! STRING_MULTIBYTE (string
))
7785 p
= SDATA (string
) + string_char_to_byte (string
, from
);
7786 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
7787 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
7795 CHECK_NATNUM (count
);
7804 if (ascii_compatible
)
7805 while (p
< stop
&& ASCII_BYTE_P (*p
))
7815 c
= STRING_CHAR_ADVANCE (p
);
7816 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
7817 && ! char_charset (translate_char (translation_table
, c
),
7818 charset_list
, NULL
))
7820 positions
= Fcons (make_number (from
), positions
);
7829 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
7833 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
7834 Scheck_coding_systems_region
, 3, 3, 0,
7835 doc
: /* Check if the region is encodable by coding systems.
7837 START and END are buffer positions specifying the region.
7838 CODING-SYSTEM-LIST is a list of coding systems to check.
7840 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7841 CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7842 whole region, POS0, POS1, ... are buffer positions where non-encodable
7843 characters are found.
7845 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7848 START may be a string. In that case, check if the string is
7849 encodable, and the value contains indices to the string instead of
7850 buffer positions. END is ignored. */)
7851 (start
, end
, coding_system_list
)
7852 Lisp_Object start
, end
, coding_system_list
;
7855 EMACS_INT start_byte
, end_byte
;
7857 const unsigned char *p
, *pbeg
, *pend
;
7859 Lisp_Object tail
, elt
, attrs
;
7861 if (STRINGP (start
))
7863 if (!STRING_MULTIBYTE (start
)
7864 && SCHARS (start
) != SBYTES (start
))
7867 end_byte
= SBYTES (start
);
7872 CHECK_NUMBER_COERCE_MARKER (start
);
7873 CHECK_NUMBER_COERCE_MARKER (end
);
7874 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7875 args_out_of_range (start
, end
);
7876 if (NILP (current_buffer
->enable_multibyte_characters
))
7878 start_byte
= CHAR_TO_BYTE (XINT (start
));
7879 end_byte
= CHAR_TO_BYTE (XINT (end
));
7880 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7883 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7885 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7886 move_gap_both (XINT (start
), start_byte
);
7888 move_gap_both (XINT (end
), end_byte
);
7894 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7897 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
7898 ASET (attrs
, coding_attr_trans_tbl
,
7899 get_translation_table (attrs
, 1, NULL
));
7900 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
7903 if (STRINGP (start
))
7904 p
= pbeg
= SDATA (start
);
7906 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7907 pend
= p
+ (end_byte
- start_byte
);
7909 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
7910 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7914 if (ASCII_BYTE_P (*p
))
7918 c
= STRING_CHAR_ADVANCE (p
);
7920 charset_map_loaded
= 0;
7921 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
7923 elt
= XCDR (XCAR (tail
));
7924 if (! char_encodable_p (c
, XCAR (elt
)))
7925 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
7927 if (charset_map_loaded
)
7929 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7931 if (STRINGP (start
))
7932 pbeg
= SDATA (start
);
7934 pbeg
= BYTE_POS_ADDR (start_byte
);
7935 p
= pbeg
+ p_offset
;
7936 pend
= pbeg
+ pend_offset
;
7944 for (; CONSP (tail
); tail
= XCDR (tail
))
7947 if (CONSP (XCDR (XCDR (elt
))))
7948 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
7957 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
7958 Lisp_Object start
, end
, coding_system
, dst_object
;
7959 int encodep
, norecord
;
7961 struct coding_system coding
;
7962 EMACS_INT from
, from_byte
, to
, to_byte
;
7963 Lisp_Object src_object
;
7965 CHECK_NUMBER_COERCE_MARKER (start
);
7966 CHECK_NUMBER_COERCE_MARKER (end
);
7967 if (NILP (coding_system
))
7968 coding_system
= Qno_conversion
;
7970 CHECK_CODING_SYSTEM (coding_system
);
7971 src_object
= Fcurrent_buffer ();
7972 if (NILP (dst_object
))
7973 dst_object
= src_object
;
7974 else if (! EQ (dst_object
, Qt
))
7975 CHECK_BUFFER (dst_object
);
7977 validate_region (&start
, &end
);
7978 from
= XFASTINT (start
);
7979 from_byte
= CHAR_TO_BYTE (from
);
7980 to
= XFASTINT (end
);
7981 to_byte
= CHAR_TO_BYTE (to
);
7983 setup_coding_system (coding_system
, &coding
);
7984 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7987 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7990 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7993 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7995 return (BUFFERP (dst_object
)
7996 ? make_number (coding
.produced_char
)
7997 : coding
.dst_object
);
8001 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8002 3, 4, "r\nzCoding system: ",
8003 doc
: /* Decode the current region from the specified coding system.
8004 When called from a program, takes four arguments:
8005 START, END, CODING-SYSTEM, and DESTINATION.
8006 START and END are buffer positions.
8008 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8009 If nil, the region between START and END is replace by the decoded text.
8010 If buffer, the decoded text is inserted in the buffer.
8011 If t, the decoded text is returned.
8013 This function sets `last-coding-system-used' to the precise coding system
8014 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8015 not fully specified.)
8016 It returns the length of the decoded text. */)
8017 (start
, end
, coding_system
, destination
)
8018 Lisp_Object start
, end
, coding_system
, destination
;
8020 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8023 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8024 3, 4, "r\nzCoding system: ",
8025 doc
: /* Encode the current region by specified coding system.
8026 When called from a program, takes three arguments:
8027 START, END, and CODING-SYSTEM. START and END are buffer positions.
8029 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8030 If nil, the region between START and END is replace by the encoded text.
8031 If buffer, the encoded text is inserted in the buffer.
8032 If t, the encoded text is returned.
8034 This function sets `last-coding-system-used' to the precise coding system
8035 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8036 not fully specified.)
8037 It returns the length of the encoded text. */)
8038 (start
, end
, coding_system
, destination
)
8039 Lisp_Object start
, end
, coding_system
, destination
;
8041 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8045 code_convert_string (string
, coding_system
, dst_object
,
8046 encodep
, nocopy
, norecord
)
8047 Lisp_Object string
, coding_system
, dst_object
;
8048 int encodep
, nocopy
, norecord
;
8050 struct coding_system coding
;
8051 EMACS_INT chars
, bytes
;
8053 CHECK_STRING (string
);
8054 if (NILP (coding_system
))
8057 Vlast_coding_system_used
= Qno_conversion
;
8058 if (NILP (dst_object
))
8059 return (nocopy
? Fcopy_sequence (string
) : string
);
8062 if (NILP (coding_system
))
8063 coding_system
= Qno_conversion
;
8065 CHECK_CODING_SYSTEM (coding_system
);
8066 if (NILP (dst_object
))
8068 else if (! EQ (dst_object
, Qt
))
8069 CHECK_BUFFER (dst_object
);
8071 setup_coding_system (coding_system
, &coding
);
8072 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8073 chars
= SCHARS (string
);
8074 bytes
= SBYTES (string
);
8076 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8078 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8080 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8082 return (BUFFERP (dst_object
)
8083 ? make_number (coding
.produced_char
)
8084 : coding
.dst_object
);
8088 /* Encode or decode STRING according to CODING_SYSTEM.
8089 Do not set Vlast_coding_system_used.
8091 This function is called only from macros DECODE_FILE and
8092 ENCODE_FILE, thus we ignore character composition. */
8095 code_convert_string_norecord (string
, coding_system
, encodep
)
8096 Lisp_Object string
, coding_system
;
8099 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8103 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8105 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8107 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8108 if the decoding operation is trivial.
8110 Optional fourth arg BUFFER non-nil meant that the decoded text is
8111 inserted in BUFFER instead of returned as a string. In this case,
8112 the return value is BUFFER.
8114 This function sets `last-coding-system-used' to the precise coding system
8115 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8116 not fully specified. */)
8117 (string
, coding_system
, nocopy
, buffer
)
8118 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8120 return code_convert_string (string
, coding_system
, buffer
,
8121 0, ! NILP (nocopy
), 0);
8124 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8126 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8128 Optional third arg NOCOPY non-nil means it is OK to return STRING
8129 itself if the encoding operation is trivial.
8131 Optional fourth arg BUFFER non-nil meant that the encoded text is
8132 inserted in BUFFER instead of returned as a string. In this case,
8133 the return value is BUFFER.
8135 This function sets `last-coding-system-used' to the precise coding system
8136 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8137 not fully specified.) */)
8138 (string
, coding_system
, nocopy
, buffer
)
8139 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8141 return code_convert_string (string
, coding_system
, buffer
,
8142 1, ! NILP (nocopy
), 1);
8146 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8147 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8148 Return the corresponding character. */)
8152 Lisp_Object spec
, attrs
, val
;
8153 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8156 CHECK_NATNUM (code
);
8157 c
= XFASTINT (code
);
8158 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8159 attrs
= AREF (spec
, 0);
8161 if (ASCII_BYTE_P (c
)
8162 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8165 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8166 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8167 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8168 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8171 charset
= charset_roman
;
8172 else if (c
>= 0xA0 && c
< 0xDF)
8174 charset
= charset_kana
;
8179 int s1
= c
>> 8, s2
= c
& 0xFF;
8181 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8182 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8183 error ("Invalid code: %d", code
);
8185 charset
= charset_kanji
;
8187 c
= DECODE_CHAR (charset
, c
);
8189 error ("Invalid code: %d", code
);
8190 return make_number (c
);
8194 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8195 doc
: /* Encode a Japanese character CH to shift_jis encoding.
8196 Return the corresponding code in SJIS. */)
8200 Lisp_Object spec
, attrs
, charset_list
;
8202 struct charset
*charset
;
8205 CHECK_CHARACTER (ch
);
8207 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8208 attrs
= AREF (spec
, 0);
8210 if (ASCII_CHAR_P (c
)
8211 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8214 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8215 charset
= char_charset (c
, charset_list
, &code
);
8216 if (code
== CHARSET_INVALID_CODE (charset
))
8217 error ("Can't encode by shift_jis encoding: %d", c
);
8220 return make_number (code
);
8223 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8224 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8225 Return the corresponding character. */)
8229 Lisp_Object spec
, attrs
, val
;
8230 struct charset
*charset_roman
, *charset_big5
, *charset
;
8233 CHECK_NATNUM (code
);
8234 c
= XFASTINT (code
);
8235 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8236 attrs
= AREF (spec
, 0);
8238 if (ASCII_BYTE_P (c
)
8239 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8242 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8243 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8244 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8247 charset
= charset_roman
;
8250 int b1
= c
>> 8, b2
= c
& 0x7F;
8251 if (b1
< 0xA1 || b1
> 0xFE
8252 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8253 error ("Invalid code: %d", code
);
8254 charset
= charset_big5
;
8256 c
= DECODE_CHAR (charset
, (unsigned )c
);
8258 error ("Invalid code: %d", code
);
8259 return make_number (c
);
8262 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8263 doc
: /* Encode the Big5 character CH to BIG5 coding system.
8264 Return the corresponding character code in Big5. */)
8268 Lisp_Object spec
, attrs
, charset_list
;
8269 struct charset
*charset
;
8273 CHECK_CHARACTER (ch
);
8275 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8276 attrs
= AREF (spec
, 0);
8277 if (ASCII_CHAR_P (c
)
8278 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8281 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8282 charset
= char_charset (c
, charset_list
, &code
);
8283 if (code
== CHARSET_INVALID_CODE (charset
))
8284 error ("Can't encode by Big5 encoding: %d", c
);
8286 return make_number (code
);
8290 DEFUN ("set-terminal-coding-system-internal",
8291 Fset_terminal_coding_system_internal
,
8292 Sset_terminal_coding_system_internal
, 1, 1, 0,
8293 doc
: /* Internal use only. */)
8295 Lisp_Object coding_system
;
8297 CHECK_SYMBOL (coding_system
);
8298 setup_coding_system (Fcheck_coding_system (coding_system
),
8301 /* We had better not send unsafe characters to terminal. */
8302 terminal_coding
.mode
|= CODING_MODE_SAFE_ENCODING
;
8303 /* Characer composition should be disabled. */
8304 terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8305 terminal_coding
.src_multibyte
= 1;
8306 terminal_coding
.dst_multibyte
= 0;
8310 DEFUN ("set-safe-terminal-coding-system-internal",
8311 Fset_safe_terminal_coding_system_internal
,
8312 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8313 doc
: /* Internal use only. */)
8315 Lisp_Object coding_system
;
8317 CHECK_SYMBOL (coding_system
);
8318 setup_coding_system (Fcheck_coding_system (coding_system
),
8319 &safe_terminal_coding
);
8320 /* Characer composition should be disabled. */
8321 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8322 safe_terminal_coding
.src_multibyte
= 1;
8323 safe_terminal_coding
.dst_multibyte
= 0;
8327 DEFUN ("terminal-coding-system",
8328 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
8329 doc
: /* Return coding system specified for terminal output. */)
8332 Lisp_Object coding_system
;
8334 coding_system
= CODING_ID_NAME (terminal_coding
.id
);
8335 /* For backward compatibility, return nil if it is `undecided'. */
8336 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
8339 DEFUN ("set-keyboard-coding-system-internal",
8340 Fset_keyboard_coding_system_internal
,
8341 Sset_keyboard_coding_system_internal
, 1, 1, 0,
8342 doc
: /* Internal use only. */)
8344 Lisp_Object coding_system
;
8346 CHECK_SYMBOL (coding_system
);
8347 setup_coding_system (Fcheck_coding_system (coding_system
),
8349 /* Characer composition should be disabled. */
8350 keyboard_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8354 DEFUN ("keyboard-coding-system",
8355 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
8356 doc
: /* Return coding system specified for decoding keyboard input. */)
8359 return CODING_ID_NAME (keyboard_coding
.id
);
8363 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8364 Sfind_operation_coding_system
, 1, MANY
, 0,
8365 doc
: /* Choose a coding system for an operation based on the target name.
8366 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8367 DECODING-SYSTEM is the coding system to use for decoding
8368 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8369 for encoding (in case OPERATION does encoding).
8371 The first argument OPERATION specifies an I/O primitive:
8372 For file I/O, `insert-file-contents' or `write-region'.
8373 For process I/O, `call-process', `call-process-region', or `start-process'.
8374 For network I/O, `open-network-stream'.
8376 The remaining arguments should be the same arguments that were passed
8377 to the primitive. Depending on which primitive, one of those arguments
8378 is selected as the TARGET. For example, if OPERATION does file I/O,
8379 whichever argument specifies the file name is TARGET.
8381 TARGET has a meaning which depends on OPERATION:
8382 For file I/O, TARGET is a file name (except for the special case below).
8383 For process I/O, TARGET is a process name.
8384 For network I/O, TARGET is a service name or a port number
8386 This function looks up what specified for TARGET in,
8387 `file-coding-system-alist', `process-coding-system-alist',
8388 or `network-coding-system-alist' depending on OPERATION.
8389 They may specify a coding system, a cons of coding systems,
8390 or a function symbol to call.
8391 In the last case, we call the function with one argument,
8392 which is a list of all the arguments given to this function.
8393 If the function can't decide a coding system, it can return
8394 `undecided' so that the normal code-detection is performed.
8396 If OPERATION is `insert-file-contents', the argument corresponding to
8397 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8398 file name to look up, and BUFFER is a buffer that contains the file's
8399 contents (not yet decoded). If `file-coding-system-alist' specifies a
8400 function to call for FILENAME, that function should examine the
8401 contents of BUFFER instead of reading the file.
8403 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
8408 Lisp_Object operation
, target_idx
, target
, val
;
8409 register Lisp_Object chain
;
8412 error ("Too few arguments");
8413 operation
= args
[0];
8414 if (!SYMBOLP (operation
)
8415 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8416 error ("Invalid first arguement");
8417 if (nargs
< 1 + XINT (target_idx
))
8418 error ("Too few arguments for operation: %s",
8419 SDATA (SYMBOL_NAME (operation
)));
8420 target
= args
[XINT (target_idx
) + 1];
8421 if (!(STRINGP (target
)
8422 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
8423 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
8424 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8425 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8427 target
= XCAR (target
);
8429 chain
= ((EQ (operation
, Qinsert_file_contents
)
8430 || EQ (operation
, Qwrite_region
))
8431 ? Vfile_coding_system_alist
8432 : (EQ (operation
, Qopen_network_stream
)
8433 ? Vnetwork_coding_system_alist
8434 : Vprocess_coding_system_alist
));
8438 for (; CONSP (chain
); chain
= XCDR (chain
))
8444 && ((STRINGP (target
)
8445 && STRINGP (XCAR (elt
))
8446 && fast_string_match (XCAR (elt
), target
) >= 0)
8447 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8450 /* Here, if VAL is both a valid coding system and a valid
8451 function symbol, we return VAL as a coding system. */
8454 if (! SYMBOLP (val
))
8456 if (! NILP (Fcoding_system_p (val
)))
8457 return Fcons (val
, val
);
8458 if (! NILP (Ffboundp (val
)))
8460 /* We use call1 rather than safe_call1
8461 so as to get bug reports about functions called here
8462 which don't handle the current interface. */
8463 val
= call1 (val
, Flist (nargs
, args
));
8466 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8467 return Fcons (val
, val
);
8475 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8476 Sset_coding_system_priority
, 0, MANY
, 0,
8477 doc
: /* Assign higher priority to the coding systems given as arguments.
8478 If multiple coding systems belongs to the same category,
8479 all but the first one are ignored.
8481 usage: (set-coding-system-priority ...) */)
8487 int changed
[coding_category_max
];
8488 enum coding_category priorities
[coding_category_max
];
8490 bzero (changed
, sizeof changed
);
8492 for (i
= j
= 0; i
< nargs
; i
++)
8494 enum coding_category category
;
8495 Lisp_Object spec
, attrs
;
8497 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8498 attrs
= AREF (spec
, 0);
8499 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8500 if (changed
[category
])
8501 /* Ignore this coding system because a coding system of the
8502 same category already had a higher priority. */
8504 changed
[category
] = 1;
8505 priorities
[j
++] = category
;
8506 if (coding_categories
[category
].id
>= 0
8507 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8508 setup_coding_system (args
[i
], &coding_categories
[category
]);
8509 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8512 /* Now we have decided top J priorities. Reflect the order of the
8513 original priorities to the remaining priorities. */
8515 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8517 while (j
< coding_category_max
8518 && changed
[coding_priorities
[j
]])
8520 if (j
== coding_category_max
)
8522 priorities
[i
] = coding_priorities
[j
];
8525 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8527 /* Update `coding-category-list'. */
8528 Vcoding_category_list
= Qnil
;
8529 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8530 Vcoding_category_list
8531 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8532 Vcoding_category_list
);
8537 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8538 Scoding_system_priority_list
, 0, 1, 0,
8539 doc
: /* Return a list of coding systems ordered by their priorities.
8540 HIGHESTP non-nil means just return the highest priority one. */)
8542 Lisp_Object highestp
;
8547 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8549 enum coding_category category
= coding_priorities
[i
];
8550 int id
= coding_categories
[category
].id
;
8555 attrs
= CODING_ID_ATTRS (id
);
8556 if (! NILP (highestp
))
8557 return CODING_ATTR_BASE_NAME (attrs
);
8558 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8560 return Fnreverse (val
);
8563 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8566 make_subsidiaries (base
)
8569 Lisp_Object subsidiaries
;
8570 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8571 char *buf
= (char *) alloca (base_name_len
+ 6);
8574 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8575 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8576 for (i
= 0; i
< 3; i
++)
8578 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8579 ASET (subsidiaries
, i
, intern (buf
));
8581 return subsidiaries
;
8585 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8586 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8587 doc
: /* For internal use only.
8588 usage: (define-coding-system-internal ...) */)
8594 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8595 Lisp_Object attrs
; /* Vector of attributes. */
8596 Lisp_Object eol_type
;
8597 Lisp_Object aliases
;
8598 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8599 enum coding_category category
;
8600 Lisp_Object tail
, val
;
8601 int max_charset_id
= 0;
8604 if (nargs
< coding_arg_max
)
8607 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8609 name
= args
[coding_arg_name
];
8610 CHECK_SYMBOL (name
);
8611 CODING_ATTR_BASE_NAME (attrs
) = name
;
8613 val
= args
[coding_arg_mnemonic
];
8614 if (! STRINGP (val
))
8615 CHECK_CHARACTER (val
);
8616 CODING_ATTR_MNEMONIC (attrs
) = val
;
8618 coding_type
= args
[coding_arg_coding_type
];
8619 CHECK_SYMBOL (coding_type
);
8620 CODING_ATTR_TYPE (attrs
) = coding_type
;
8622 charset_list
= args
[coding_arg_charset_list
];
8623 if (SYMBOLP (charset_list
))
8625 if (EQ (charset_list
, Qiso_2022
))
8627 if (! EQ (coding_type
, Qiso_2022
))
8628 error ("Invalid charset-list");
8629 charset_list
= Viso_2022_charset_list
;
8631 else if (EQ (charset_list
, Qemacs_mule
))
8633 if (! EQ (coding_type
, Qemacs_mule
))
8634 error ("Invalid charset-list");
8635 charset_list
= Vemacs_mule_charset_list
;
8637 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8638 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8639 max_charset_id
= XFASTINT (XCAR (tail
));
8643 charset_list
= Fcopy_sequence (charset_list
);
8644 for (tail
= charset_list
; !NILP (tail
); tail
= Fcdr (tail
))
8646 struct charset
*charset
;
8649 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8650 if (EQ (coding_type
, Qiso_2022
)
8651 ? CHARSET_ISO_FINAL (charset
) < 0
8652 : EQ (coding_type
, Qemacs_mule
)
8653 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8655 error ("Can't handle charset `%s'",
8656 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8658 XSETCAR (tail
, make_number (charset
->id
));
8659 if (max_charset_id
< charset
->id
)
8660 max_charset_id
= charset
->id
;
8663 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8665 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8667 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8668 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8669 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8671 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8673 val
= args
[coding_arg_decode_translation_table
];
8674 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8676 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8678 val
= args
[coding_arg_encode_translation_table
];
8679 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8681 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8683 val
= args
[coding_arg_post_read_conversion
];
8685 CODING_ATTR_POST_READ (attrs
) = val
;
8687 val
= args
[coding_arg_pre_write_conversion
];
8689 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8691 val
= args
[coding_arg_default_char
];
8693 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
8696 CHECK_CHARACTER (val
);
8697 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8700 val
= args
[coding_arg_for_unibyte
];
8701 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
8703 val
= args
[coding_arg_plist
];
8705 CODING_ATTR_PLIST (attrs
) = val
;
8707 if (EQ (coding_type
, Qcharset
))
8709 /* Generate a lisp vector of 256 elements. Each element is nil,
8710 integer, or a list of charset IDs.
8712 If Nth element is nil, the byte code N is invalid in this
8715 If Nth element is a number NUM, N is the first byte of a
8716 charset whose ID is NUM.
8718 If Nth element is a list of charset IDs, N is the first byte
8719 of one of them. The list is sorted by dimensions of the
8720 charsets. A charset of smaller dimension comes firtst. */
8721 val
= Fmake_vector (make_number (256), Qnil
);
8723 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8725 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
8726 int dim
= CHARSET_DIMENSION (charset
);
8727 int idx
= (dim
- 1) * 4;
8729 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8730 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8732 for (i
= charset
->code_space
[idx
];
8733 i
<= charset
->code_space
[idx
+ 1]; i
++)
8735 Lisp_Object tmp
, tmp2
;
8738 tmp
= AREF (val
, i
);
8741 else if (NUMBERP (tmp
))
8743 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
8745 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
8747 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
8751 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
8753 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
8758 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
8761 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
8762 XSETCAR (tmp2
, XCAR (tail
));
8768 ASET (attrs
, coding_attr_charset_valids
, val
);
8769 category
= coding_category_charset
;
8771 else if (EQ (coding_type
, Qccl
))
8775 if (nargs
< coding_arg_ccl_max
)
8778 val
= args
[coding_arg_ccl_decoder
];
8779 CHECK_CCL_PROGRAM (val
);
8781 val
= Fcopy_sequence (val
);
8782 ASET (attrs
, coding_attr_ccl_decoder
, val
);
8784 val
= args
[coding_arg_ccl_encoder
];
8785 CHECK_CCL_PROGRAM (val
);
8787 val
= Fcopy_sequence (val
);
8788 ASET (attrs
, coding_attr_ccl_encoder
, val
);
8790 val
= args
[coding_arg_ccl_valids
];
8791 valids
= Fmake_string (make_number (256), make_number (0));
8792 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
8799 from
= to
= XINT (val
);
8800 if (from
< 0 || from
> 255)
8801 args_out_of_range_3 (val
, make_number (0), make_number (255));
8806 CHECK_NATNUM_CAR (val
);
8807 CHECK_NATNUM_CDR (val
);
8808 from
= XINT (XCAR (val
));
8810 args_out_of_range_3 (XCAR (val
),
8811 make_number (0), make_number (255));
8812 to
= XINT (XCDR (val
));
8813 if (to
< from
|| to
> 255)
8814 args_out_of_range_3 (XCDR (val
),
8815 XCAR (val
), make_number (255));
8817 for (i
= from
; i
<= to
; i
++)
8818 SSET (valids
, i
, 1);
8820 ASET (attrs
, coding_attr_ccl_valids
, valids
);
8822 category
= coding_category_ccl
;
8824 else if (EQ (coding_type
, Qutf_16
))
8826 Lisp_Object bom
, endian
;
8828 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8830 if (nargs
< coding_arg_utf16_max
)
8833 bom
= args
[coding_arg_utf16_bom
];
8834 if (! NILP (bom
) && ! EQ (bom
, Qt
))
8838 CHECK_CODING_SYSTEM (val
);
8840 CHECK_CODING_SYSTEM (val
);
8842 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
8844 endian
= args
[coding_arg_utf16_endian
];
8845 CHECK_SYMBOL (endian
);
8848 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
8849 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
8850 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
8852 category
= (CONSP (bom
)
8853 ? coding_category_utf_16_auto
8855 ? (EQ (endian
, Qbig
)
8856 ? coding_category_utf_16_be_nosig
8857 : coding_category_utf_16_le_nosig
)
8858 : (EQ (endian
, Qbig
)
8859 ? coding_category_utf_16_be
8860 : coding_category_utf_16_le
));
8862 else if (EQ (coding_type
, Qiso_2022
))
8864 Lisp_Object initial
, reg_usage
, request
, flags
;
8867 if (nargs
< coding_arg_iso2022_max
)
8870 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
8871 CHECK_VECTOR (initial
);
8872 for (i
= 0; i
< 4; i
++)
8874 val
= Faref (initial
, make_number (i
));
8877 struct charset
*charset
;
8879 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8880 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
8881 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
8882 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8885 ASET (initial
, i
, make_number (-1));
8888 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
8889 CHECK_CONS (reg_usage
);
8890 CHECK_NUMBER_CAR (reg_usage
);
8891 CHECK_NUMBER_CDR (reg_usage
);
8893 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
8894 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
8902 CHECK_CHARSET_GET_ID (tmp
, id
);
8903 CHECK_NATNUM_CDR (val
);
8904 if (XINT (XCDR (val
)) >= 4)
8905 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
8906 XSETCAR (val
, make_number (id
));
8909 flags
= args
[coding_arg_iso2022_flags
];
8910 CHECK_NATNUM (flags
);
8912 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
8913 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
8915 ASET (attrs
, coding_attr_iso_initial
, initial
);
8916 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
8917 ASET (attrs
, coding_attr_iso_request
, request
);
8918 ASET (attrs
, coding_attr_iso_flags
, flags
);
8919 setup_iso_safe_charsets (attrs
);
8921 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
8922 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
8923 | CODING_ISO_FLAG_SINGLE_SHIFT
))
8924 ? coding_category_iso_7_else
8925 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8926 ? coding_category_iso_7
8927 : coding_category_iso_7_tight
);
8930 int id
= XINT (AREF (initial
, 1));
8932 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
8933 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8935 ? coding_category_iso_8_else
8936 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
8937 ? coding_category_iso_8_1
8938 : coding_category_iso_8_2
);
8940 if (category
!= coding_category_iso_8_1
8941 && category
!= coding_category_iso_8_2
)
8942 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8944 else if (EQ (coding_type
, Qemacs_mule
))
8946 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
8947 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
8948 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8949 category
= coding_category_emacs_mule
;
8951 else if (EQ (coding_type
, Qshift_jis
))
8954 struct charset
*charset
;
8956 if (XINT (Flength (charset_list
)) != 3
8957 && XINT (Flength (charset_list
)) != 4)
8958 error ("There should be three or four charsets");
8960 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8961 if (CHARSET_DIMENSION (charset
) != 1)
8962 error ("Dimension of charset %s is not one",
8963 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8964 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8965 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8967 charset_list
= XCDR (charset_list
);
8968 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8969 if (CHARSET_DIMENSION (charset
) != 1)
8970 error ("Dimension of charset %s is not one",
8971 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8973 charset_list
= XCDR (charset_list
);
8974 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8975 if (CHARSET_DIMENSION (charset
) != 2)
8976 error ("Dimension of charset %s is not two",
8977 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8979 charset_list
= XCDR (charset_list
);
8980 if (! NILP (charset_list
))
8982 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8983 if (CHARSET_DIMENSION (charset
) != 2)
8984 error ("Dimension of charset %s is not two",
8985 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8988 category
= coding_category_sjis
;
8989 Vsjis_coding_system
= name
;
8991 else if (EQ (coding_type
, Qbig5
))
8993 struct charset
*charset
;
8995 if (XINT (Flength (charset_list
)) != 2)
8996 error ("There should be just two charsets");
8998 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8999 if (CHARSET_DIMENSION (charset
) != 1)
9000 error ("Dimension of charset %s is not one",
9001 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9002 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9003 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9005 charset_list
= XCDR (charset_list
);
9006 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9007 if (CHARSET_DIMENSION (charset
) != 2)
9008 error ("Dimension of charset %s is not two",
9009 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9011 category
= coding_category_big5
;
9012 Vbig5_coding_system
= name
;
9014 else if (EQ (coding_type
, Qraw_text
))
9016 category
= coding_category_raw_text
;
9017 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9019 else if (EQ (coding_type
, Qutf_8
))
9021 category
= coding_category_utf_8
;
9022 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9024 else if (EQ (coding_type
, Qundecided
))
9025 category
= coding_category_undecided
;
9027 error ("Invalid coding system type: %s",
9028 SDATA (SYMBOL_NAME (coding_type
)));
9030 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9031 CODING_ATTR_PLIST (attrs
)
9032 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9033 CODING_ATTR_PLIST (attrs
)));
9034 CODING_ATTR_PLIST (attrs
)
9035 = Fcons (QCascii_compatible_p
,
9036 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9037 CODING_ATTR_PLIST (attrs
)));
9039 eol_type
= args
[coding_arg_eol_type
];
9040 if (! NILP (eol_type
)
9041 && ! EQ (eol_type
, Qunix
)
9042 && ! EQ (eol_type
, Qdos
)
9043 && ! EQ (eol_type
, Qmac
))
9044 error ("Invalid eol-type");
9046 aliases
= Fcons (name
, Qnil
);
9048 if (NILP (eol_type
))
9050 eol_type
= make_subsidiaries (name
);
9051 for (i
= 0; i
< 3; i
++)
9053 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9055 this_name
= AREF (eol_type
, i
);
9056 this_aliases
= Fcons (this_name
, Qnil
);
9057 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9058 this_spec
= Fmake_vector (make_number (3), attrs
);
9059 ASET (this_spec
, 1, this_aliases
);
9060 ASET (this_spec
, 2, this_eol_type
);
9061 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9062 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9063 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9065 Vcoding_system_alist
9066 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9067 Vcoding_system_alist
);
9071 spec_vec
= Fmake_vector (make_number (3), attrs
);
9072 ASET (spec_vec
, 1, aliases
);
9073 ASET (spec_vec
, 2, eol_type
);
9075 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9076 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9077 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9079 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9080 Vcoding_system_alist
);
9083 int id
= coding_categories
[category
].id
;
9085 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9086 setup_coding_system (name
, &coding_categories
[category
]);
9092 return Fsignal (Qwrong_number_of_arguments
,
9093 Fcons (intern ("define-coding-system-internal"),
9094 make_number (nargs
)));
9098 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
9100 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9101 (coding_system
, prop
, val
)
9102 Lisp_Object coding_system
, prop
, val
;
9104 Lisp_Object spec
, attrs
;
9106 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9107 attrs
= AREF (spec
, 0);
9108 if (EQ (prop
, QCmnemonic
))
9110 if (! STRINGP (val
))
9111 CHECK_CHARACTER (val
);
9112 CODING_ATTR_MNEMONIC (attrs
) = val
;
9114 else if (EQ (prop
, QCdefalut_char
))
9117 val
= make_number (' ');
9119 CHECK_CHARACTER (val
);
9120 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9122 else if (EQ (prop
, QCdecode_translation_table
))
9124 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9126 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9128 else if (EQ (prop
, QCencode_translation_table
))
9130 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9132 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9134 else if (EQ (prop
, QCpost_read_conversion
))
9137 CODING_ATTR_POST_READ (attrs
) = val
;
9139 else if (EQ (prop
, QCpre_write_conversion
))
9142 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9144 else if (EQ (prop
, QCascii_compatible_p
))
9146 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9149 CODING_ATTR_PLIST (attrs
)
9150 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9155 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9156 Sdefine_coding_system_alias
, 2, 2, 0,
9157 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9158 (alias
, coding_system
)
9159 Lisp_Object alias
, coding_system
;
9161 Lisp_Object spec
, aliases
, eol_type
, val
;
9163 CHECK_SYMBOL (alias
);
9164 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9165 aliases
= AREF (spec
, 1);
9166 /* ALISES should be a list of length more than zero, and the first
9167 element is a base coding system. Append ALIAS at the tail of the
9169 while (!NILP (XCDR (aliases
)))
9170 aliases
= XCDR (aliases
);
9171 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9173 eol_type
= AREF (spec
, 2);
9174 if (VECTORP (eol_type
))
9176 Lisp_Object subsidiaries
;
9179 subsidiaries
= make_subsidiaries (alias
);
9180 for (i
= 0; i
< 3; i
++)
9181 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9182 AREF (eol_type
, i
));
9185 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9186 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9187 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
9189 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9190 Vcoding_system_alist
);
9195 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9197 doc
: /* Return the base of CODING-SYSTEM.
9198 Any alias or subsidiary coding system is not a base coding system. */)
9200 Lisp_Object coding_system
;
9202 Lisp_Object spec
, attrs
;
9204 if (NILP (coding_system
))
9205 return (Qno_conversion
);
9206 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9207 attrs
= AREF (spec
, 0);
9208 return CODING_ATTR_BASE_NAME (attrs
);
9211 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9213 doc
: "Return the property list of CODING-SYSTEM.")
9215 Lisp_Object coding_system
;
9217 Lisp_Object spec
, attrs
;
9219 if (NILP (coding_system
))
9220 coding_system
= Qno_conversion
;
9221 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9222 attrs
= AREF (spec
, 0);
9223 return CODING_ATTR_PLIST (attrs
);
9227 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9229 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9231 Lisp_Object coding_system
;
9235 if (NILP (coding_system
))
9236 coding_system
= Qno_conversion
;
9237 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9238 return AREF (spec
, 1);
9241 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9242 Scoding_system_eol_type
, 1, 1, 0,
9243 doc
: /* Return eol-type of CODING-SYSTEM.
9244 An eol-type is integer 0, 1, 2, or a vector of coding systems.
9246 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9247 and CR respectively.
9249 A vector value indicates that a format of end-of-line should be
9250 detected automatically. Nth element of the vector is the subsidiary
9251 coding system whose eol-type is N. */)
9253 Lisp_Object coding_system
;
9255 Lisp_Object spec
, eol_type
;
9258 if (NILP (coding_system
))
9259 coding_system
= Qno_conversion
;
9260 if (! CODING_SYSTEM_P (coding_system
))
9262 spec
= CODING_SYSTEM_SPEC (coding_system
);
9263 eol_type
= AREF (spec
, 2);
9264 if (VECTORP (eol_type
))
9265 return Fcopy_sequence (eol_type
);
9266 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9267 return make_number (n
);
9273 /*** 9. Post-amble ***/
9280 for (i
= 0; i
< coding_category_max
; i
++)
9282 coding_categories
[i
].id
= -1;
9283 coding_priorities
[i
] = i
;
9286 /* ISO2022 specific initialize routine. */
9287 for (i
= 0; i
< 0x20; i
++)
9288 iso_code_class
[i
] = ISO_control_0
;
9289 for (i
= 0x21; i
< 0x7F; i
++)
9290 iso_code_class
[i
] = ISO_graphic_plane_0
;
9291 for (i
= 0x80; i
< 0xA0; i
++)
9292 iso_code_class
[i
] = ISO_control_1
;
9293 for (i
= 0xA1; i
< 0xFF; i
++)
9294 iso_code_class
[i
] = ISO_graphic_plane_1
;
9295 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9296 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9297 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9298 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9299 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9300 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9301 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9302 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9303 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9305 for (i
= 0; i
< 256; i
++)
9307 emacs_mule_bytes
[i
] = 1;
9309 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9310 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9311 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9312 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9320 staticpro (&Vcoding_system_hash_table
);
9322 Lisp_Object args
[2];
9325 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9328 staticpro (&Vsjis_coding_system
);
9329 Vsjis_coding_system
= Qnil
;
9331 staticpro (&Vbig5_coding_system
);
9332 Vbig5_coding_system
= Qnil
;
9334 staticpro (&Vcode_conversion_reused_workbuf
);
9335 Vcode_conversion_reused_workbuf
= Qnil
;
9337 staticpro (&Vcode_conversion_workbuf_name
);
9338 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9340 reused_workbuf_in_use
= 0;
9342 DEFSYM (Qcharset
, "charset");
9343 DEFSYM (Qtarget_idx
, "target-idx");
9344 DEFSYM (Qcoding_system_history
, "coding-system-history");
9345 Fset (Qcoding_system_history
, Qnil
);
9347 /* Target FILENAME is the first argument. */
9348 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9349 /* Target FILENAME is the third argument. */
9350 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9352 DEFSYM (Qcall_process
, "call-process");
9353 /* Target PROGRAM is the first argument. */
9354 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9356 DEFSYM (Qcall_process_region
, "call-process-region");
9357 /* Target PROGRAM is the third argument. */
9358 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9360 DEFSYM (Qstart_process
, "start-process");
9361 /* Target PROGRAM is the third argument. */
9362 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9364 DEFSYM (Qopen_network_stream
, "open-network-stream");
9365 /* Target SERVICE is the fourth argument. */
9366 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9368 DEFSYM (Qcoding_system
, "coding-system");
9369 DEFSYM (Qcoding_aliases
, "coding-aliases");
9371 DEFSYM (Qeol_type
, "eol-type");
9372 DEFSYM (Qunix
, "unix");
9373 DEFSYM (Qdos
, "dos");
9375 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9376 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9377 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9378 DEFSYM (Qdefault_char
, "default-char");
9379 DEFSYM (Qundecided
, "undecided");
9380 DEFSYM (Qno_conversion
, "no-conversion");
9381 DEFSYM (Qraw_text
, "raw-text");
9383 DEFSYM (Qiso_2022
, "iso-2022");
9385 DEFSYM (Qutf_8
, "utf-8");
9386 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9388 DEFSYM (Qutf_16
, "utf-16");
9389 DEFSYM (Qbig
, "big");
9390 DEFSYM (Qlittle
, "little");
9392 DEFSYM (Qshift_jis
, "shift-jis");
9393 DEFSYM (Qbig5
, "big5");
9395 DEFSYM (Qcoding_system_p
, "coding-system-p");
9397 DEFSYM (Qcoding_system_error
, "coding-system-error");
9398 Fput (Qcoding_system_error
, Qerror_conditions
,
9399 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9400 Fput (Qcoding_system_error
, Qerror_message
,
9401 build_string ("Invalid coding system"));
9403 /* Intern this now in case it isn't already done.
9404 Setting this variable twice is harmless.
9405 But don't staticpro it here--that is done in alloc.c. */
9406 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9408 DEFSYM (Qtranslation_table
, "translation-table");
9409 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9410 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9411 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9412 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9414 DEFSYM (Qvalid_codes
, "valid-codes");
9416 DEFSYM (Qemacs_mule
, "emacs-mule");
9418 DEFSYM (QCcategory
, ":category");
9419 DEFSYM (QCmnemonic
, ":mnemonic");
9420 DEFSYM (QCdefalut_char
, ":default-char");
9421 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9422 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9423 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9424 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9425 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
9427 Vcoding_category_table
9428 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9429 staticpro (&Vcoding_category_table
);
9430 /* Followings are target of code detection. */
9431 ASET (Vcoding_category_table
, coding_category_iso_7
,
9432 intern ("coding-category-iso-7"));
9433 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9434 intern ("coding-category-iso-7-tight"));
9435 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9436 intern ("coding-category-iso-8-1"));
9437 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9438 intern ("coding-category-iso-8-2"));
9439 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9440 intern ("coding-category-iso-7-else"));
9441 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9442 intern ("coding-category-iso-8-else"));
9443 ASET (Vcoding_category_table
, coding_category_utf_8
,
9444 intern ("coding-category-utf-8"));
9445 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9446 intern ("coding-category-utf-16-be"));
9447 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9448 intern ("coding-category-utf-16-auto"));
9449 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9450 intern ("coding-category-utf-16-le"));
9451 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9452 intern ("coding-category-utf-16-be-nosig"));
9453 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9454 intern ("coding-category-utf-16-le-nosig"));
9455 ASET (Vcoding_category_table
, coding_category_charset
,
9456 intern ("coding-category-charset"));
9457 ASET (Vcoding_category_table
, coding_category_sjis
,
9458 intern ("coding-category-sjis"));
9459 ASET (Vcoding_category_table
, coding_category_big5
,
9460 intern ("coding-category-big5"));
9461 ASET (Vcoding_category_table
, coding_category_ccl
,
9462 intern ("coding-category-ccl"));
9463 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9464 intern ("coding-category-emacs-mule"));
9465 /* Followings are NOT target of code detection. */
9466 ASET (Vcoding_category_table
, coding_category_raw_text
,
9467 intern ("coding-category-raw-text"));
9468 ASET (Vcoding_category_table
, coding_category_undecided
,
9469 intern ("coding-category-undecided"));
9471 DEFSYM (Qinsufficient_source
, "insufficient-source");
9472 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9473 DEFSYM (Qinvalid_source
, "invalid-source");
9474 DEFSYM (Qinterrupted
, "interrupted");
9475 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9476 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
9478 defsubr (&Scoding_system_p
);
9479 defsubr (&Sread_coding_system
);
9480 defsubr (&Sread_non_nil_coding_system
);
9481 defsubr (&Scheck_coding_system
);
9482 defsubr (&Sdetect_coding_region
);
9483 defsubr (&Sdetect_coding_string
);
9484 defsubr (&Sfind_coding_systems_region_internal
);
9485 defsubr (&Sunencodable_char_position
);
9486 defsubr (&Scheck_coding_systems_region
);
9487 defsubr (&Sdecode_coding_region
);
9488 defsubr (&Sencode_coding_region
);
9489 defsubr (&Sdecode_coding_string
);
9490 defsubr (&Sencode_coding_string
);
9491 defsubr (&Sdecode_sjis_char
);
9492 defsubr (&Sencode_sjis_char
);
9493 defsubr (&Sdecode_big5_char
);
9494 defsubr (&Sencode_big5_char
);
9495 defsubr (&Sset_terminal_coding_system_internal
);
9496 defsubr (&Sset_safe_terminal_coding_system_internal
);
9497 defsubr (&Sterminal_coding_system
);
9498 defsubr (&Sset_keyboard_coding_system_internal
);
9499 defsubr (&Skeyboard_coding_system
);
9500 defsubr (&Sfind_operation_coding_system
);
9501 defsubr (&Sset_coding_system_priority
);
9502 defsubr (&Sdefine_coding_system_internal
);
9503 defsubr (&Sdefine_coding_system_alias
);
9504 defsubr (&Scoding_system_put
);
9505 defsubr (&Scoding_system_base
);
9506 defsubr (&Scoding_system_plist
);
9507 defsubr (&Scoding_system_aliases
);
9508 defsubr (&Scoding_system_eol_type
);
9509 defsubr (&Scoding_system_priority_list
);
9511 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9512 doc
: /* List of coding systems.
9514 Do not alter the value of this variable manually. This variable should be
9515 updated by the functions `define-coding-system' and
9516 `define-coding-system-alias'. */);
9517 Vcoding_system_list
= Qnil
;
9519 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9520 doc
: /* Alist of coding system names.
9521 Each element is one element list of coding system name.
9522 This variable is given to `completing-read' as TABLE argument.
9524 Do not alter the value of this variable manually. This variable should be
9525 updated by the functions `make-coding-system' and
9526 `define-coding-system-alias'. */);
9527 Vcoding_system_alist
= Qnil
;
9529 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9530 doc
: /* List of coding-categories (symbols) ordered by priority.
9532 On detecting a coding system, Emacs tries code detection algorithms
9533 associated with each coding-category one by one in this order. When
9534 one algorithm agrees with a byte sequence of source text, the coding
9535 system bound to the corresponding coding-category is selected.
9537 Don't modify this variable directly, but use `set-coding-priority'. */);
9541 Vcoding_category_list
= Qnil
;
9542 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9543 Vcoding_category_list
9544 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9545 Vcoding_category_list
);
9548 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9549 doc
: /* Specify the coding system for read operations.
9550 It is useful to bind this variable with `let', but do not set it globally.
9551 If the value is a coding system, it is used for decoding on read operation.
9552 If not, an appropriate element is used from one of the coding system alists:
9553 There are three such tables, `file-coding-system-alist',
9554 `process-coding-system-alist', and `network-coding-system-alist'. */);
9555 Vcoding_system_for_read
= Qnil
;
9557 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9558 doc
: /* Specify the coding system for write operations.
9559 Programs bind this variable with `let', but you should not set it globally.
9560 If the value is a coding system, it is used for encoding of output,
9561 when writing it to a file and when sending it to a file or subprocess.
9563 If this does not specify a coding system, an appropriate element
9564 is used from one of the coding system alists:
9565 There are three such tables, `file-coding-system-alist',
9566 `process-coding-system-alist', and `network-coding-system-alist'.
9567 For output to files, if the above procedure does not specify a coding system,
9568 the value of `buffer-file-coding-system' is used. */);
9569 Vcoding_system_for_write
= Qnil
;
9571 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9573 Coding system used in the latest file or process I/O. */);
9574 Vlast_coding_system_used
= Qnil
;
9576 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9578 Error status of the last code conversion.
9580 When an error was detected in the last code conversion, this variable
9581 is set to one of the following symbols.
9582 `insufficient-source'
9586 `insufficient-memory'
9587 When no error was detected, the value doesn't change. So, to check
9588 the error status of a code conversion by this variable, you must
9589 explicitly set this variable to nil before performing code
9591 Vlast_code_conversion_error
= Qnil
;
9593 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9595 *Non-nil means always inhibit code conversion of end-of-line format.
9596 See info node `Coding Systems' and info node `Text and Binary' concerning
9597 such conversion. */);
9598 inhibit_eol_conversion
= 0;
9600 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9602 Non-nil means process buffer inherits coding system of process output.
9603 Bind it to t if the process output is to be treated as if it were a file
9604 read from some filesystem. */);
9605 inherit_process_coding_system
= 0;
9607 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9609 Alist to decide a coding system to use for a file I/O operation.
9610 The format is ((PATTERN . VAL) ...),
9611 where PATTERN is a regular expression matching a file name,
9612 VAL is a coding system, a cons of coding systems, or a function symbol.
9613 If VAL is a coding system, it is used for both decoding and encoding
9615 If VAL is a cons of coding systems, the car part is used for decoding,
9616 and the cdr part is used for encoding.
9617 If VAL is a function symbol, the function must return a coding system
9618 or a cons of coding systems which are used as above. The function is
9619 called with an argument that is a list of the arguments with which
9620 `find-operation-coding-system' was called. If the function can't decide
9621 a coding system, it can return `undecided' so that the normal
9622 code-detection is performed.
9624 See also the function `find-operation-coding-system'
9625 and the variable `auto-coding-alist'. */);
9626 Vfile_coding_system_alist
= Qnil
;
9628 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9630 Alist to decide a coding system to use for a process I/O operation.
9631 The format is ((PATTERN . VAL) ...),
9632 where PATTERN is a regular expression matching a program name,
9633 VAL is a coding system, a cons of coding systems, or a function symbol.
9634 If VAL is a coding system, it is used for both decoding what received
9635 from the program and encoding what sent to the program.
9636 If VAL is a cons of coding systems, the car part is used for decoding,
9637 and the cdr part is used for encoding.
9638 If VAL is a function symbol, the function must return a coding system
9639 or a cons of coding systems which are used as above.
9641 See also the function `find-operation-coding-system'. */);
9642 Vprocess_coding_system_alist
= Qnil
;
9644 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9646 Alist to decide a coding system to use for a network I/O operation.
9647 The format is ((PATTERN . VAL) ...),
9648 where PATTERN is a regular expression matching a network service name
9649 or is a port number to connect to,
9650 VAL is a coding system, a cons of coding systems, or a function symbol.
9651 If VAL is a coding system, it is used for both decoding what received
9652 from the network stream and encoding what sent to the network stream.
9653 If VAL is a cons of coding systems, the car part is used for decoding,
9654 and the cdr part is used for encoding.
9655 If VAL is a function symbol, the function must return a coding system
9656 or a cons of coding systems which are used as above.
9658 See also the function `find-operation-coding-system'. */);
9659 Vnetwork_coding_system_alist
= Qnil
;
9661 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9662 doc
: /* Coding system to use with system messages.
9663 Also used for decoding keyboard input on X Window system. */);
9664 Vlocale_coding_system
= Qnil
;
9666 /* The eol mnemonics are reset in startup.el system-dependently. */
9667 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9669 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9670 eol_mnemonic_unix
= build_string (":");
9672 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9674 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9675 eol_mnemonic_dos
= build_string ("\\");
9677 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9679 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9680 eol_mnemonic_mac
= build_string ("/");
9682 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9684 *String displayed in mode line when end-of-line format is not yet determined. */);
9685 eol_mnemonic_undecided
= build_string (":");
9687 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
9689 *Non-nil enables character translation while encoding and decoding. */);
9690 Venable_character_translation
= Qt
;
9692 DEFVAR_LISP ("standard-translation-table-for-decode",
9693 &Vstandard_translation_table_for_decode
,
9694 doc
: /* Table for translating characters while decoding. */);
9695 Vstandard_translation_table_for_decode
= Qnil
;
9697 DEFVAR_LISP ("standard-translation-table-for-encode",
9698 &Vstandard_translation_table_for_encode
,
9699 doc
: /* Table for translating characters while encoding. */);
9700 Vstandard_translation_table_for_encode
= Qnil
;
9702 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
9703 doc
: /* Alist of charsets vs revision numbers.
9704 While encoding, if a charset (car part of an element) is found,
9705 designate it with the escape sequence identifying revision (cdr part
9706 of the element). */);
9707 Vcharset_revision_table
= Qnil
;
9709 DEFVAR_LISP ("default-process-coding-system",
9710 &Vdefault_process_coding_system
,
9711 doc
: /* Cons of coding systems used for process I/O by default.
9712 The car part is used for decoding a process output,
9713 the cdr part is used for encoding a text to be sent to a process. */);
9714 Vdefault_process_coding_system
= Qnil
;
9716 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
9718 Table of extra Latin codes in the range 128..159 (inclusive).
9719 This is a vector of length 256.
9720 If Nth element is non-nil, the existence of code N in a file
9721 \(or output of subprocess) doesn't prevent it to be detected as
9722 a coding system of ISO 2022 variant which has a flag
9723 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9724 or reading output of a subprocess.
9725 Only 128th through 159th elements has a meaning. */);
9726 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
9728 DEFVAR_LISP ("select-safe-coding-system-function",
9729 &Vselect_safe_coding_system_function
,
9731 Function to call to select safe coding system for encoding a text.
9733 If set, this function is called to force a user to select a proper
9734 coding system which can encode the text in the case that a default
9735 coding system used in each operation can't encode the text.
9737 The default value is `select-safe-coding-system' (which see). */);
9738 Vselect_safe_coding_system_function
= Qnil
;
9740 DEFVAR_BOOL ("coding-system-require-warning",
9741 &coding_system_require_warning
,
9742 doc
: /* Internal use only.
9743 If non-nil, on writing a file, `select-safe-coding-system-function' is
9744 called even if `coding-system-for-write' is non-nil. The command
9745 `universal-coding-system-argument' binds this variable to t temporarily. */);
9746 coding_system_require_warning
= 0;
9749 DEFVAR_BOOL ("inhibit-iso-escape-detection",
9750 &inhibit_iso_escape_detection
,
9752 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
9754 By default, on reading a file, Emacs tries to detect how the text is
9755 encoded. This code detection is sensitive to escape sequences. If
9756 the sequence is valid as ISO2022, the code is determined as one of
9757 the ISO2022 encodings, and the file is decoded by the corresponding
9758 coding system (e.g. `iso-2022-7bit').
9760 However, there may be a case that you want to read escape sequences in
9761 a file as is. In such a case, you can set this variable to non-nil.
9762 Then, as the code detection ignores any escape sequences, no file is
9763 detected as encoded in some ISO2022 encoding. The result is that all
9764 escape sequences become visible in a buffer.
9766 The default value is nil, and it is strongly recommended not to change
9767 it. That is because many Emacs Lisp source files that contain
9768 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9769 in Emacs's distribution, and they won't be decoded correctly on
9770 reading if you suppress escape sequence detection.
9772 The other way to read escape sequences in a file without decoding is
9773 to explicitly specify some coding system that doesn't use ISO2022's
9774 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
9775 inhibit_iso_escape_detection
= 0;
9777 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
9778 doc
: /* Char table for translating self-inserting characters.
9779 This is applied to the result of input methods, not their input. See also
9780 `keyboard-translate-table'. */);
9781 Vtranslation_table_for_input
= Qnil
;
9784 Lisp_Object args
[coding_arg_max
];
9785 Lisp_Object plist
[16];
9788 for (i
= 0; i
< coding_arg_max
; i
++)
9791 plist
[0] = intern (":name");
9792 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
9793 plist
[2] = intern (":mnemonic");
9794 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
9795 plist
[4] = intern (":coding-type");
9796 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
9797 plist
[6] = intern (":ascii-compatible-p");
9798 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
9799 plist
[8] = intern (":default-char");
9800 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
9801 plist
[10] = intern (":for-unibyte");
9802 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
9803 plist
[12] = intern (":docstring");
9804 plist
[13] = build_string ("Do no conversion.\n\
9806 When you visit a file with this coding, the file is read into a\n\
9807 unibyte buffer as is, thus each byte of a file is treated as a\n\
9809 plist
[14] = intern (":eol-type");
9810 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
9811 args
[coding_arg_plist
] = Flist (16, plist
);
9812 Fdefine_coding_system_internal (coding_arg_max
, args
);
9814 plist
[1] = args
[coding_arg_name
] = Qundecided
;
9815 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
9816 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
9817 /* This is already set.
9818 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
9819 plist
[8] = intern (":charset-list");
9820 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
9821 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
9822 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9823 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
9824 args
[coding_arg_plist
] = Flist (16, plist
);
9825 Fdefine_coding_system_internal (coding_arg_max
, args
);
9828 setup_coding_system (Qno_conversion
, &keyboard_coding
);
9829 setup_coding_system (Qundecided
, &terminal_coding
);
9830 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
9835 for (i
= 0; i
< coding_category_max
; i
++)
9836 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
9838 #if defined (MSDOS) || defined (WINDOWSNT)
9839 system_eol_type
= Qdos
;
9841 system_eol_type
= Qunix
;
9843 staticpro (&system_eol_type
);
9847 emacs_strerror (error_number
)
9852 synchronize_system_messages_locale ();
9853 str
= strerror (error_number
);
9855 if (! NILP (Vlocale_coding_system
))
9857 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
9858 Vlocale_coding_system
,
9860 str
= (char *) SDATA (dec
);
9868 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
9869 (do not change this comment) */