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 3, 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
;
2667 /* Control sequence introducer. */
2668 single_shifting
= 0;
2669 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2670 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2671 goto check_extra_latin
;
2676 if (inhibit_iso_escape_detection
)
2678 single_shifting
= 0;
2679 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2680 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2681 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2682 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2683 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2684 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2685 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2686 if (single_shifting
)
2688 goto check_extra_latin
;
2695 single_shifting
= 0;
2700 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2701 found
|= CATEGORY_MASK_ISO_8_1
;
2702 /* Check the length of succeeding codes of the range
2703 0xA0..0FF. If the byte length is even, we include
2704 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2705 only when we are not single shifting. */
2706 if (! single_shifting
2707 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2710 while (src
< src_end
)
2718 if (i
& 1 && src
< src_end
)
2719 rejected
|= CATEGORY_MASK_ISO_8_2
;
2721 found
|= CATEGORY_MASK_ISO_8_2
;
2726 single_shifting
= 0;
2727 if (! VECTORP (Vlatin_extra_code_table
)
2728 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
2730 rejected
= CATEGORY_MASK_ISO
;
2733 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2734 & CODING_ISO_FLAG_LATIN_EXTRA
)
2735 found
|= CATEGORY_MASK_ISO_8_1
;
2737 rejected
|= CATEGORY_MASK_ISO_8_1
;
2738 rejected
|= CATEGORY_MASK_ISO_8_2
;
2741 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
2745 detect_info
->rejected
|= rejected
;
2746 detect_info
->found
|= (found
& ~rejected
);
2751 /* Set designation state into CODING. Set CHARS_96 to -1 if the
2752 escape sequence should be kept. */
2753 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2757 if (final < '0' || final >= 128 \
2758 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2759 || !SAFE_CHARSET_P (coding, id)) \
2761 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2765 prev = CODING_ISO_DESIGNATION (coding, reg); \
2766 if (id == charset_jisx0201_roman) \
2768 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2769 id = charset_ascii; \
2771 else if (id == charset_jisx0208_1978) \
2773 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2774 id = charset_jisx0208; \
2776 CODING_ISO_DESIGNATION (coding, reg) = id; \
2777 /* If there was an invalid designation to REG previously, and this \
2778 designation is ASCII to REG, we should keep this designation \
2780 if (prev == -2 && id == charset_ascii) \
2785 #define MAYBE_FINISH_COMPOSITION() \
2788 if (composition_state == COMPOSING_NO) \
2790 /* It is assured that we have enough room for producing \
2791 characters stored in the table `components'. */ \
2792 if (charbuf + component_idx > charbuf_end) \
2793 goto no_more_source; \
2794 composition_state = COMPOSING_NO; \
2795 if (method == COMPOSITION_RELATIVE \
2796 || method == COMPOSITION_WITH_ALTCHARS) \
2798 for (i = 0; i < component_idx; i++) \
2799 *charbuf++ = components[i]; \
2800 char_offset += component_idx; \
2804 for (i = 0; i < component_idx; i += 2) \
2805 *charbuf++ = components[i]; \
2806 char_offset += (component_idx / 2) + 1; \
2811 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2812 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2813 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2814 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2815 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2818 #define DECODE_COMPOSITION_START(c1) \
2821 && composition_state == COMPOSING_COMPONENT_RULE) \
2823 component_len = component_idx; \
2824 composition_state = COMPOSING_CHAR; \
2828 const unsigned char *p; \
2830 MAYBE_FINISH_COMPOSITION (); \
2831 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2832 goto no_more_source; \
2833 for (p = src; p < src_end - 1; p++) \
2834 if (*p == ISO_CODE_ESC && p[1] == '1') \
2836 if (p == src_end - 1) \
2838 /* The current composition doesn't end in the current \
2840 record_conversion_result \
2841 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
2842 goto no_more_source; \
2845 /* This is surely the start of a composition. */ \
2846 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2847 : c1 == '2' ? COMPOSITION_WITH_RULE \
2848 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2849 : COMPOSITION_WITH_RULE_ALTCHARS); \
2850 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2851 : COMPOSING_COMPONENT_CHAR); \
2852 component_idx = component_len = 0; \
2857 /* Handle compositoin end sequence ESC 1. */
2859 #define DECODE_COMPOSITION_END() \
2861 int nchars = (component_len > 0 ? component_idx - component_len \
2862 : method == COMPOSITION_RELATIVE ? component_idx \
2863 : (component_idx + 1) / 2); \
2865 int *saved_charbuf = charbuf; \
2867 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2868 if (method != COMPOSITION_RELATIVE) \
2870 if (component_len == 0) \
2871 for (i = 0; i < component_idx; i++) \
2872 *charbuf++ = components[i]; \
2874 for (i = 0; i < component_len; i++) \
2875 *charbuf++ = components[i]; \
2876 *saved_charbuf = saved_charbuf - charbuf; \
2878 if (method == COMPOSITION_WITH_RULE) \
2879 for (i = 0; i < component_idx; i += 2, char_offset++) \
2880 *charbuf++ = components[i]; \
2882 for (i = component_len; i < component_idx; i++, char_offset++) \
2883 *charbuf++ = components[i]; \
2884 coding->annotated = 1; \
2885 composition_state = COMPOSING_NO; \
2889 /* Decode a composition rule from the byte C1 (and maybe one more byte
2890 from SRC) and store one encoded composition rule in
2891 coding->cmp_data. */
2893 #define DECODE_COMPOSITION_RULE(c1) \
2896 if (c1 < 81) /* old format (before ver.21) */ \
2898 int gref = (c1) / 9; \
2899 int nref = (c1) % 9; \
2900 if (gref == 4) gref = 10; \
2901 if (nref == 4) nref = 10; \
2902 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
2904 else if (c1 < 93) /* new format (after ver.21) */ \
2906 ONE_MORE_BYTE (c2); \
2907 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
2914 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2917 decode_coding_iso_2022 (coding
)
2918 struct coding_system
*coding
;
2920 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2921 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2922 const unsigned char *src_base
;
2923 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2925 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
2926 int consumed_chars
= 0, consumed_chars_base
;
2927 int multibytep
= coding
->src_multibyte
;
2928 /* Charsets invoked to graphic plane 0 and 1 respectively. */
2929 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2930 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
2931 int charset_id_2
, charset_id_3
;
2932 struct charset
*charset
;
2934 /* For handling composition sequence. */
2935 #define COMPOSING_NO 0
2936 #define COMPOSING_CHAR 1
2937 #define COMPOSING_RULE 2
2938 #define COMPOSING_COMPONENT_CHAR 3
2939 #define COMPOSING_COMPONENT_RULE 4
2941 int composition_state
= COMPOSING_NO
;
2942 enum composition_method method
;
2943 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
2946 Lisp_Object attrs
, charset_list
;
2947 int char_offset
= coding
->produced_char
;
2948 int last_offset
= char_offset
;
2949 int last_id
= charset_ascii
;
2951 CODING_GET_INFO (coding
, attrs
, charset_list
);
2952 setup_iso_safe_charsets (attrs
);
2953 /* Charset list may have been changed. */
2954 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2955 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
2962 consumed_chars_base
= consumed_chars
;
2964 if (charbuf
>= charbuf_end
)
2971 /* We produce at most one character. */
2972 switch (iso_code_class
[c1
])
2974 case ISO_0x20_or_0x7F
:
2975 if (composition_state
!= COMPOSING_NO
)
2977 if (composition_state
== COMPOSING_RULE
2978 || composition_state
== COMPOSING_COMPONENT_RULE
)
2980 DECODE_COMPOSITION_RULE (c1
);
2981 components
[component_idx
++] = c1
;
2982 composition_state
--;
2986 if (charset_id_0
< 0
2987 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
2988 /* This is SPACE or DEL. */
2989 charset
= CHARSET_FROM_ID (charset_ascii
);
2991 charset
= CHARSET_FROM_ID (charset_id_0
);
2994 case ISO_graphic_plane_0
:
2995 if (composition_state
!= COMPOSING_NO
)
2997 if (composition_state
== COMPOSING_RULE
2998 || composition_state
== COMPOSING_COMPONENT_RULE
)
3000 DECODE_COMPOSITION_RULE (c1
);
3001 components
[component_idx
++] = c1
;
3002 composition_state
--;
3006 if (charset_id_0
< 0)
3007 charset
= CHARSET_FROM_ID (charset_ascii
);
3009 charset
= CHARSET_FROM_ID (charset_id_0
);
3012 case ISO_0xA0_or_0xFF
:
3013 if (charset_id_1
< 0
3014 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3015 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3017 /* This is a graphic character, we fall down ... */
3019 case ISO_graphic_plane_1
:
3020 if (charset_id_1
< 0)
3022 charset
= CHARSET_FROM_ID (charset_id_1
);
3026 MAYBE_FINISH_COMPOSITION ();
3027 charset
= CHARSET_FROM_ID (charset_ascii
);
3031 MAYBE_FINISH_COMPOSITION ();
3035 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3036 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3038 CODING_ISO_INVOCATION (coding
, 0) = 1;
3039 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3043 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3045 CODING_ISO_INVOCATION (coding
, 0) = 0;
3046 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3049 case ISO_single_shift_2_7
:
3050 case ISO_single_shift_2
:
3051 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3053 /* SS2 is handled as an escape sequence of ESC 'N' */
3055 goto label_escape_sequence
;
3057 case ISO_single_shift_3
:
3058 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3060 /* SS2 is handled as an escape sequence of ESC 'O' */
3062 goto label_escape_sequence
;
3064 case ISO_control_sequence_introducer
:
3065 /* CSI is handled as an escape sequence of ESC '[' ... */
3067 goto label_escape_sequence
;
3071 label_escape_sequence
:
3072 /* Escape sequences handled here are invocation,
3073 designation, direction specification, and character
3074 composition specification. */
3077 case '&': /* revision of following character set */
3079 if (!(c1
>= '@' && c1
<= '~'))
3082 if (c1
!= ISO_CODE_ESC
)
3085 goto label_escape_sequence
;
3087 case '$': /* designation of 2-byte character set */
3088 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3094 if (c1
>= '@' && c1
<= 'B')
3095 { /* designation of JISX0208.1978, GB2312.1980,
3097 reg
= 0, chars96
= 0;
3099 else if (c1
>= 0x28 && c1
<= 0x2B)
3100 { /* designation of DIMENSION2_CHARS94 character set */
3101 reg
= c1
- 0x28, chars96
= 0;
3104 else if (c1
>= 0x2C && c1
<= 0x2F)
3105 { /* designation of DIMENSION2_CHARS96 character set */
3106 reg
= c1
- 0x2C, chars96
= 1;
3111 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3112 /* We must update these variables now. */
3114 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3116 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3122 case 'n': /* invocation of locking-shift-2 */
3123 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3124 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3126 CODING_ISO_INVOCATION (coding
, 0) = 2;
3127 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3130 case 'o': /* invocation of locking-shift-3 */
3131 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3132 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3134 CODING_ISO_INVOCATION (coding
, 0) = 3;
3135 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3138 case 'N': /* invocation of single-shift-2 */
3139 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3140 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3142 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3143 if (charset_id_2
< 0)
3144 charset
= CHARSET_FROM_ID (charset_ascii
);
3146 charset
= CHARSET_FROM_ID (charset_id_2
);
3148 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3152 case 'O': /* invocation of single-shift-3 */
3153 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3154 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3156 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3157 if (charset_id_3
< 0)
3158 charset
= CHARSET_FROM_ID (charset_ascii
);
3160 charset
= CHARSET_FROM_ID (charset_id_3
);
3162 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3166 case '0': case '2': case '3': case '4': /* start composition */
3167 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3169 DECODE_COMPOSITION_START (c1
);
3172 case '1': /* end composition */
3173 if (composition_state
== COMPOSING_NO
)
3175 DECODE_COMPOSITION_END ();
3178 case '[': /* specification of direction */
3179 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3181 /* For the moment, nested direction is not supported.
3182 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3183 left-to-right, and nozero means right-to-left. */
3187 case ']': /* end of the current direction */
3188 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3190 case '0': /* end of the current direction */
3191 case '1': /* start of left-to-right direction */
3194 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3199 case '2': /* start of right-to-left direction */
3202 coding
->mode
|= CODING_MODE_DIRECTION
;
3216 /* CTEXT extended segment:
3217 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3218 We keep these bytes as is for the moment.
3219 They may be decoded by post-read-conversion. */
3223 ONE_MORE_BYTE (dim
);
3226 size
= ((M
- 128) * 128) + (L
- 128);
3227 if (charbuf
+ 8 + size
> charbuf_end
)
3229 *charbuf
++ = ISO_CODE_ESC
;
3233 *charbuf
++ = BYTE8_TO_CHAR (M
);
3234 *charbuf
++ = BYTE8_TO_CHAR (L
);
3238 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3243 /* XFree86 extension for embedding UTF-8 in CTEXT:
3244 ESC % G --UTF-8-BYTES-- ESC % @
3245 We keep these bytes as is for the moment.
3246 They may be decoded by post-read-conversion. */
3249 if (p
+ 6 > charbuf_end
)
3251 *p
++ = ISO_CODE_ESC
;
3254 while (p
< charbuf_end
)
3257 if (c1
== ISO_CODE_ESC
3258 && src
+ 1 < src_end
3265 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3267 if (p
+ 3 > charbuf_end
)
3269 *p
++ = ISO_CODE_ESC
;
3280 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3285 if (c1
>= 0x28 && c1
<= 0x2B)
3286 { /* designation of DIMENSION1_CHARS94 character set */
3287 reg
= c1
- 0x28, chars96
= 0;
3290 else if (c1
>= 0x2C && c1
<= 0x2F)
3291 { /* designation of DIMENSION1_CHARS96 character set */
3292 reg
= c1
- 0x2C, chars96
= 1;
3297 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3298 /* We must update these variables now. */
3300 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3302 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3310 if (charset
->id
!= charset_ascii
3311 && last_id
!= charset
->id
)
3313 if (last_id
!= charset_ascii
)
3314 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3315 last_id
= charset
->id
;
3316 last_offset
= char_offset
;
3319 /* Now we know CHARSET and 1st position code C1 of a character.
3320 Produce a decoded character while getting 2nd position code
3323 if (CHARSET_DIMENSION (charset
) > 1)
3326 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3327 /* C2 is not in a valid range. */
3329 c1
= (c1
<< 8) | (c2
& 0x7F);
3330 if (CHARSET_DIMENSION (charset
) > 2)
3333 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3334 /* C2 is not in a valid range. */
3336 c1
= (c1
<< 8) | (c2
& 0x7F);
3340 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3343 MAYBE_FINISH_COMPOSITION ();
3344 for (; src_base
< src
; src_base
++, char_offset
++)
3346 if (ASCII_BYTE_P (*src_base
))
3347 *charbuf
++ = *src_base
;
3349 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3352 else if (composition_state
== COMPOSING_NO
)
3359 components
[component_idx
++] = c
;
3360 if (method
== COMPOSITION_WITH_RULE
3361 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3362 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3363 composition_state
++;
3368 MAYBE_FINISH_COMPOSITION ();
3370 consumed_chars
= consumed_chars_base
;
3372 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3382 if (last_id
!= charset_ascii
)
3383 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3384 coding
->consumed_char
+= consumed_chars_base
;
3385 coding
->consumed
= src_base
- coding
->source
;
3386 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3390 /* ISO2022 encoding stuff. */
3393 It is not enough to say just "ISO2022" on encoding, we have to
3394 specify more details. In Emacs, each coding system of ISO2022
3395 variant has the following specifications:
3396 1. Initial designation to G0 thru G3.
3397 2. Allows short-form designation?
3398 3. ASCII should be designated to G0 before control characters?
3399 4. ASCII should be designated to G0 at end of line?
3400 5. 7-bit environment or 8-bit environment?
3401 6. Use locking-shift?
3402 7. Use Single-shift?
3403 And the following two are only for Japanese:
3404 8. Use ASCII in place of JIS0201-1976-Roman?
3405 9. Use JISX0208-1983 in place of JISX0208-1978?
3406 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3407 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3411 /* Produce codes (escape sequence) for designating CHARSET to graphic
3412 register REG at DST, and increment DST. If <final-char> of CHARSET is
3413 '@', 'A', or 'B' and the coding system CODING allows, produce
3414 designation sequence of short-form. */
3416 #define ENCODE_DESIGNATION(charset, reg, coding) \
3418 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3419 char *intermediate_char_94 = "()*+"; \
3420 char *intermediate_char_96 = ",-./"; \
3421 int revision = -1; \
3424 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3425 revision = CHARSET_ISO_REVISION (charset); \
3427 if (revision >= 0) \
3429 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3430 EMIT_ONE_BYTE ('@' + revision); \
3432 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3433 if (CHARSET_DIMENSION (charset) == 1) \
3435 if (! CHARSET_ISO_CHARS_96 (charset)) \
3436 c = intermediate_char_94[reg]; \
3438 c = intermediate_char_96[reg]; \
3439 EMIT_ONE_ASCII_BYTE (c); \
3443 EMIT_ONE_ASCII_BYTE ('$'); \
3444 if (! CHARSET_ISO_CHARS_96 (charset)) \
3446 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3448 || final_char < '@' || final_char > 'B') \
3449 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3452 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3454 EMIT_ONE_ASCII_BYTE (final_char); \
3456 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3460 /* The following two macros produce codes (control character or escape
3461 sequence) for ISO2022 single-shift functions (single-shift-2 and
3464 #define ENCODE_SINGLE_SHIFT_2 \
3466 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3467 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3469 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3470 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3474 #define ENCODE_SINGLE_SHIFT_3 \
3476 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3477 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3479 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3480 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3484 /* The following four macros produce codes (control character or
3485 escape sequence) for ISO2022 locking-shift functions (shift-in,
3486 shift-out, locking-shift-2, and locking-shift-3). */
3488 #define ENCODE_SHIFT_IN \
3490 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3491 CODING_ISO_INVOCATION (coding, 0) = 0; \
3495 #define ENCODE_SHIFT_OUT \
3497 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3498 CODING_ISO_INVOCATION (coding, 0) = 1; \
3502 #define ENCODE_LOCKING_SHIFT_2 \
3504 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3505 CODING_ISO_INVOCATION (coding, 0) = 2; \
3509 #define ENCODE_LOCKING_SHIFT_3 \
3511 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3512 CODING_ISO_INVOCATION (coding, 0) = 3; \
3516 /* Produce codes for a DIMENSION1 character whose character set is
3517 CHARSET and whose position-code is C1. Designation and invocation
3518 sequences are also produced in advance if necessary. */
3520 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3522 int id = CHARSET_ID (charset); \
3524 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3525 && id == charset_ascii) \
3527 id = charset_jisx0201_roman; \
3528 charset = CHARSET_FROM_ID (id); \
3531 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3533 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3534 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3536 EMIT_ONE_BYTE (c1 | 0x80); \
3537 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3540 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3542 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3545 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3547 EMIT_ONE_BYTE (c1 | 0x80); \
3551 /* Since CHARSET is not yet invoked to any graphic planes, we \
3552 must invoke it, or, at first, designate it to some graphic \
3553 register. Then repeat the loop to actually produce the \
3555 dst = encode_invocation_designation (charset, coding, dst, \
3560 /* Produce codes for a DIMENSION2 character whose character set is
3561 CHARSET and whose position-codes are C1 and C2. Designation and
3562 invocation codes are also produced in advance if necessary. */
3564 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3566 int id = CHARSET_ID (charset); \
3568 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3569 && id == charset_jisx0208) \
3571 id = charset_jisx0208_1978; \
3572 charset = CHARSET_FROM_ID (id); \
3575 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3577 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3578 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3580 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3581 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3584 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3586 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3589 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3591 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3595 /* Since CHARSET is not yet invoked to any graphic planes, we \
3596 must invoke it, or, at first, designate it to some graphic \
3597 register. Then repeat the loop to actually produce the \
3599 dst = encode_invocation_designation (charset, coding, dst, \
3604 #define ENCODE_ISO_CHARACTER(charset, c) \
3606 int code = ENCODE_CHAR ((charset),(c)); \
3608 if (CHARSET_DIMENSION (charset) == 1) \
3609 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3611 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3615 /* Produce designation and invocation codes at a place pointed by DST
3616 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3620 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3621 struct charset
*charset
;
3622 struct coding_system
*coding
;
3626 int multibytep
= coding
->dst_multibyte
;
3627 int produced_chars
= *p_nchars
;
3628 int reg
; /* graphic register number */
3629 int id
= CHARSET_ID (charset
);
3631 /* At first, check designations. */
3632 for (reg
= 0; reg
< 4; reg
++)
3633 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3638 /* CHARSET is not yet designated to any graphic registers. */
3639 /* At first check the requested designation. */
3640 reg
= CODING_ISO_REQUEST (coding
, id
);
3642 /* Since CHARSET requests no special designation, designate it
3643 to graphic register 0. */
3646 ENCODE_DESIGNATION (charset
, reg
, coding
);
3649 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3650 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3652 /* Since the graphic register REG is not invoked to any graphic
3653 planes, invoke it to graphic plane 0. */
3656 case 0: /* graphic register 0 */
3660 case 1: /* graphic register 1 */
3664 case 2: /* graphic register 2 */
3665 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3666 ENCODE_SINGLE_SHIFT_2
;
3668 ENCODE_LOCKING_SHIFT_2
;
3671 case 3: /* graphic register 3 */
3672 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3673 ENCODE_SINGLE_SHIFT_3
;
3675 ENCODE_LOCKING_SHIFT_3
;
3680 *p_nchars
= produced_chars
;
3684 /* The following three macros produce codes for indicating direction
3686 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3688 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3689 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3691 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3695 #define ENCODE_DIRECTION_R2L() \
3697 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3698 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3702 #define ENCODE_DIRECTION_L2R() \
3704 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3705 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3709 /* Produce codes for designation and invocation to reset the graphic
3710 planes and registers to initial state. */
3711 #define ENCODE_RESET_PLANE_AND_REGISTER() \
3714 struct charset *charset; \
3716 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3718 for (reg = 0; reg < 4; reg++) \
3719 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3720 && (CODING_ISO_DESIGNATION (coding, reg) \
3721 != CODING_ISO_INITIAL (coding, reg))) \
3723 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3724 ENCODE_DESIGNATION (charset, reg, coding); \
3729 /* Produce designation sequences of charsets in the line started from
3730 SRC to a place pointed by DST, and return updated DST.
3732 If the current block ends before any end-of-line, we may fail to
3733 find all the necessary designations. */
3735 static unsigned char *
3736 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
3737 struct coding_system
*coding
;
3738 int *charbuf
, *charbuf_end
;
3741 struct charset
*charset
;
3742 /* Table of charsets to be designated to each graphic register. */
3744 int c
, found
= 0, reg
;
3745 int produced_chars
= 0;
3746 int multibytep
= coding
->dst_multibyte
;
3748 Lisp_Object charset_list
;
3750 attrs
= CODING_ID_ATTRS (coding
->id
);
3751 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3752 if (EQ (charset_list
, Qiso_2022
))
3753 charset_list
= Viso_2022_charset_list
;
3755 for (reg
= 0; reg
< 4; reg
++)
3765 charset
= char_charset (c
, charset_list
, NULL
);
3766 id
= CHARSET_ID (charset
);
3767 reg
= CODING_ISO_REQUEST (coding
, id
);
3768 if (reg
>= 0 && r
[reg
] < 0)
3777 for (reg
= 0; reg
< 4; reg
++)
3779 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
3780 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
3786 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3789 encode_coding_iso_2022 (coding
)
3790 struct coding_system
*coding
;
3792 int multibytep
= coding
->dst_multibyte
;
3793 int *charbuf
= coding
->charbuf
;
3794 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
3795 unsigned char *dst
= coding
->destination
+ coding
->produced
;
3796 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
3799 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3800 && CODING_ISO_BOL (coding
));
3801 int produced_chars
= 0;
3802 Lisp_Object attrs
, eol_type
, charset_list
;
3803 int ascii_compatible
;
3805 int preferred_charset_id
= -1;
3807 CODING_GET_INFO (coding
, attrs
, charset_list
);
3808 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
3809 if (VECTORP (eol_type
))
3812 setup_iso_safe_charsets (attrs
);
3813 /* Charset list may have been changed. */
3814 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3815 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3817 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
3819 while (charbuf
< charbuf_end
)
3821 ASSURE_DESTINATION (safe_room
);
3823 if (bol_designation
)
3825 unsigned char *dst_prev
= dst
;
3827 /* We have to produce designation sequences if any now. */
3828 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
3829 bol_designation
= 0;
3830 /* We are sure that designation sequences are all ASCII bytes. */
3831 produced_chars
+= dst
- dst_prev
;
3838 /* Handle an annotation. */
3841 case CODING_ANNOTATE_COMPOSITION_MASK
:
3842 /* Not yet implemented. */
3844 case CODING_ANNOTATE_CHARSET_MASK
:
3845 preferred_charset_id
= charbuf
[2];
3846 if (preferred_charset_id
>= 0
3847 && NILP (Fmemq (make_number (preferred_charset_id
),
3849 preferred_charset_id
= -1;
3858 /* Now encode the character C. */
3859 if (c
< 0x20 || c
== 0x7F)
3862 || (c
== '\r' && EQ (eol_type
, Qmac
)))
3864 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3865 ENCODE_RESET_PLANE_AND_REGISTER ();
3866 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
3870 for (i
= 0; i
< 4; i
++)
3871 CODING_ISO_DESIGNATION (coding
, i
)
3872 = CODING_ISO_INITIAL (coding
, i
);
3875 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
3877 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
3878 ENCODE_RESET_PLANE_AND_REGISTER ();
3879 EMIT_ONE_ASCII_BYTE (c
);
3881 else if (ASCII_CHAR_P (c
))
3883 if (ascii_compatible
)
3884 EMIT_ONE_ASCII_BYTE (c
);
3887 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
3888 ENCODE_ISO_CHARACTER (charset
, c
);
3891 else if (CHAR_BYTE8_P (c
))
3893 c
= CHAR_TO_BYTE8 (c
);
3898 struct charset
*charset
;
3900 if (preferred_charset_id
>= 0)
3902 charset
= CHARSET_FROM_ID (preferred_charset_id
);
3903 if (! CHAR_CHARSET_P (c
, charset
))
3904 charset
= char_charset (c
, charset_list
, NULL
);
3907 charset
= char_charset (c
, charset_list
, NULL
);
3910 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
3912 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
3913 charset
= CHARSET_FROM_ID (charset_ascii
);
3917 c
= coding
->default_char
;
3918 charset
= char_charset (c
, charset_list
, NULL
);
3921 ENCODE_ISO_CHARACTER (charset
, c
);
3925 if (coding
->mode
& CODING_MODE_LAST_BLOCK
3926 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3928 ASSURE_DESTINATION (safe_room
);
3929 ENCODE_RESET_PLANE_AND_REGISTER ();
3931 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
3932 CODING_ISO_BOL (coding
) = bol_designation
;
3933 coding
->produced_char
+= produced_chars
;
3934 coding
->produced
= dst
- coding
->destination
;
3939 /*** 8,9. SJIS and BIG5 handlers ***/
3941 /* Although SJIS and BIG5 are not ISO's coding system, they are used
3942 quite widely. So, for the moment, Emacs supports them in the bare
3943 C code. But, in the future, they may be supported only by CCL. */
3945 /* SJIS is a coding system encoding three character sets: ASCII, right
3946 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3947 as is. A character of charset katakana-jisx0201 is encoded by
3948 "position-code + 0x80". A character of charset japanese-jisx0208
3949 is encoded in 2-byte but two position-codes are divided and shifted
3950 so that it fit in the range below.
3952 --- CODE RANGE of SJIS ---
3953 (character set) (range)
3955 KATAKANA-JISX0201 0xA0 .. 0xDF
3956 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
3957 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
3958 -------------------------------
3962 /* BIG5 is a coding system encoding two character sets: ASCII and
3963 Big5. An ASCII character is encoded as is. Big5 is a two-byte
3964 character set and is encoded in two-byte.
3966 --- CODE RANGE of BIG5 ---
3967 (character set) (range)
3969 Big5 (1st byte) 0xA1 .. 0xFE
3970 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3971 --------------------------
3975 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3976 Check if a text is encoded in SJIS. If it is, return
3977 CATEGORY_MASK_SJIS, else return 0. */
3980 detect_coding_sjis (coding
, detect_info
)
3981 struct coding_system
*coding
;
3982 struct coding_detection_info
*detect_info
;
3984 const unsigned char *src
= coding
->source
, *src_base
;
3985 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3986 int multibytep
= coding
->src_multibyte
;
3987 int consumed_chars
= 0;
3991 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
3992 /* A coding system of this category is always ASCII compatible. */
3993 src
+= coding
->head_ascii
;
4001 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
4004 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4006 found
= CATEGORY_MASK_SJIS
;
4008 else if (c
>= 0xA0 && c
< 0xE0)
4009 found
= CATEGORY_MASK_SJIS
;
4013 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4017 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4019 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4022 detect_info
->found
|= found
;
4026 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4027 Check if a text is encoded in BIG5. If it is, return
4028 CATEGORY_MASK_BIG5, else return 0. */
4031 detect_coding_big5 (coding
, detect_info
)
4032 struct coding_system
*coding
;
4033 struct coding_detection_info
*detect_info
;
4035 const unsigned char *src
= coding
->source
, *src_base
;
4036 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4037 int multibytep
= coding
->src_multibyte
;
4038 int consumed_chars
= 0;
4042 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4043 /* A coding system of this category is always ASCII compatible. */
4044 src
+= coding
->head_ascii
;
4055 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4057 found
= CATEGORY_MASK_BIG5
;
4062 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4066 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4068 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4071 detect_info
->found
|= found
;
4075 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4076 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4079 decode_coding_sjis (coding
)
4080 struct coding_system
*coding
;
4082 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4083 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4084 const unsigned char *src_base
;
4085 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4087 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4088 int consumed_chars
= 0, consumed_chars_base
;
4089 int multibytep
= coding
->src_multibyte
;
4090 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4091 struct charset
*charset_kanji2
;
4092 Lisp_Object attrs
, charset_list
, val
;
4093 int char_offset
= coding
->produced_char
;
4094 int last_offset
= char_offset
;
4095 int last_id
= charset_ascii
;
4097 CODING_GET_INFO (coding
, attrs
, charset_list
);
4100 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4101 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4102 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4103 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4108 struct charset
*charset
;
4111 consumed_chars_base
= consumed_chars
;
4113 if (charbuf
>= charbuf_end
)
4120 charset
= charset_roman
;
4121 else if (c
== 0x80 || c
== 0xA0)
4123 else if (c
>= 0xA1 && c
<= 0xDF)
4125 /* SJIS -> JISX0201-Kana */
4127 charset
= charset_kana
;
4131 /* SJIS -> JISX0208 */
4133 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4137 charset
= charset_kanji
;
4139 else if (c
<= 0xFC && charset_kanji2
)
4141 /* SJIS -> JISX0213-2 */
4143 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4147 charset
= charset_kanji2
;
4151 if (charset
->id
!= charset_ascii
4152 && last_id
!= charset
->id
)
4154 if (last_id
!= charset_ascii
)
4155 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4156 last_id
= charset
->id
;
4157 last_offset
= char_offset
;
4159 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4166 consumed_chars
= consumed_chars_base
;
4168 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4174 if (last_id
!= charset_ascii
)
4175 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4176 coding
->consumed_char
+= consumed_chars_base
;
4177 coding
->consumed
= src_base
- coding
->source
;
4178 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4182 decode_coding_big5 (coding
)
4183 struct coding_system
*coding
;
4185 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4186 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4187 const unsigned char *src_base
;
4188 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4190 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4191 int consumed_chars
= 0, consumed_chars_base
;
4192 int multibytep
= coding
->src_multibyte
;
4193 struct charset
*charset_roman
, *charset_big5
;
4194 Lisp_Object attrs
, charset_list
, val
;
4195 int char_offset
= coding
->produced_char
;
4196 int last_offset
= char_offset
;
4197 int last_id
= charset_ascii
;
4199 CODING_GET_INFO (coding
, attrs
, charset_list
);
4201 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4202 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4207 struct charset
*charset
;
4210 consumed_chars_base
= consumed_chars
;
4212 if (charbuf
>= charbuf_end
)
4220 charset
= charset_roman
;
4224 if (c
< 0xA1 || c
> 0xFE)
4227 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4230 charset
= charset_big5
;
4232 if (charset
->id
!= charset_ascii
4233 && last_id
!= charset
->id
)
4235 if (last_id
!= charset_ascii
)
4236 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4237 last_id
= charset
->id
;
4238 last_offset
= char_offset
;
4240 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4247 consumed_chars
= consumed_chars_base
;
4249 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4255 if (last_id
!= charset_ascii
)
4256 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4257 coding
->consumed_char
+= consumed_chars_base
;
4258 coding
->consumed
= src_base
- coding
->source
;
4259 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4262 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4263 This function can encode charsets `ascii', `katakana-jisx0201',
4264 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4265 are sure that all these charsets are registered as official charset
4266 (i.e. do not have extended leading-codes). Characters of other
4267 charsets are produced without any encoding. If SJIS_P is 1, encode
4268 SJIS text, else encode BIG5 text. */
4271 encode_coding_sjis (coding
)
4272 struct coding_system
*coding
;
4274 int multibytep
= coding
->dst_multibyte
;
4275 int *charbuf
= coding
->charbuf
;
4276 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4277 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4278 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4280 int produced_chars
= 0;
4281 Lisp_Object attrs
, charset_list
, val
;
4282 int ascii_compatible
;
4283 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4284 struct charset
*charset_kanji2
;
4287 CODING_GET_INFO (coding
, attrs
, charset_list
);
4289 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4290 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4291 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4292 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4294 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4296 while (charbuf
< charbuf_end
)
4298 ASSURE_DESTINATION (safe_room
);
4300 /* Now encode the character C. */
4301 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4302 EMIT_ONE_ASCII_BYTE (c
);
4303 else if (CHAR_BYTE8_P (c
))
4305 c
= CHAR_TO_BYTE8 (c
);
4311 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4315 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4317 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4318 charset
= CHARSET_FROM_ID (charset_ascii
);
4322 c
= coding
->default_char
;
4323 charset
= char_charset (c
, charset_list
, &code
);
4326 if (code
== CHARSET_INVALID_CODE (charset
))
4328 if (charset
== charset_kanji
)
4332 c1
= code
>> 8, c2
= code
& 0xFF;
4333 EMIT_TWO_BYTES (c1
, c2
);
4335 else if (charset
== charset_kana
)
4336 EMIT_ONE_BYTE (code
| 0x80);
4337 else if (charset_kanji2
&& charset
== charset_kanji2
)
4342 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4343 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4345 JIS_TO_SJIS2 (code
);
4346 c1
= code
>> 8, c2
= code
& 0xFF;
4347 EMIT_TWO_BYTES (c1
, c2
);
4350 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4353 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4356 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4357 coding
->produced_char
+= produced_chars
;
4358 coding
->produced
= dst
- coding
->destination
;
4363 encode_coding_big5 (coding
)
4364 struct coding_system
*coding
;
4366 int multibytep
= coding
->dst_multibyte
;
4367 int *charbuf
= coding
->charbuf
;
4368 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4369 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4370 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4372 int produced_chars
= 0;
4373 Lisp_Object attrs
, charset_list
, val
;
4374 int ascii_compatible
;
4375 struct charset
*charset_roman
, *charset_big5
;
4378 CODING_GET_INFO (coding
, attrs
, charset_list
);
4380 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4381 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4382 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4384 while (charbuf
< charbuf_end
)
4386 ASSURE_DESTINATION (safe_room
);
4388 /* Now encode the character C. */
4389 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4390 EMIT_ONE_ASCII_BYTE (c
);
4391 else if (CHAR_BYTE8_P (c
))
4393 c
= CHAR_TO_BYTE8 (c
);
4399 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4403 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4405 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4406 charset
= CHARSET_FROM_ID (charset_ascii
);
4410 c
= coding
->default_char
;
4411 charset
= char_charset (c
, charset_list
, &code
);
4414 if (code
== CHARSET_INVALID_CODE (charset
))
4416 if (charset
== charset_big5
)
4420 c1
= code
>> 8, c2
= code
& 0xFF;
4421 EMIT_TWO_BYTES (c1
, c2
);
4424 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4427 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4428 coding
->produced_char
+= produced_chars
;
4429 coding
->produced
= dst
- coding
->destination
;
4434 /*** 10. CCL handlers ***/
4436 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4437 Check if a text is encoded in a coding system of which
4438 encoder/decoder are written in CCL program. If it is, return
4439 CATEGORY_MASK_CCL, else return 0. */
4442 detect_coding_ccl (coding
, detect_info
)
4443 struct coding_system
*coding
;
4444 struct coding_detection_info
*detect_info
;
4446 const unsigned char *src
= coding
->source
, *src_base
;
4447 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4448 int multibytep
= coding
->src_multibyte
;
4449 int consumed_chars
= 0;
4451 unsigned char *valids
;
4452 int head_ascii
= coding
->head_ascii
;
4455 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4457 coding
= &coding_categories
[coding_category_ccl
];
4458 valids
= CODING_CCL_VALIDS (coding
);
4459 attrs
= CODING_ID_ATTRS (coding
->id
);
4460 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4469 if (c
< 0 || ! valids
[c
])
4471 if ((valids
[c
] > 1))
4472 found
= CATEGORY_MASK_CCL
;
4474 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4478 detect_info
->found
|= found
;
4483 decode_coding_ccl (coding
)
4484 struct coding_system
*coding
;
4486 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4487 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4488 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4489 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4490 int consumed_chars
= 0;
4491 int multibytep
= coding
->src_multibyte
;
4492 struct ccl_program ccl
;
4493 int source_charbuf
[1024];
4494 int source_byteidx
[1024];
4495 Lisp_Object attrs
, charset_list
;
4497 CODING_GET_INFO (coding
, attrs
, charset_list
);
4498 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4500 while (src
< src_end
)
4502 const unsigned char *p
= src
;
4503 int *source
, *source_end
;
4507 while (i
< 1024 && p
< src_end
)
4509 source_byteidx
[i
] = p
- src
;
4510 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4513 while (i
< 1024 && p
< src_end
)
4514 source_charbuf
[i
++] = *p
++;
4516 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4519 source
= source_charbuf
;
4520 source_end
= source
+ i
;
4521 while (source
< source_end
)
4523 ccl_driver (&ccl
, source
, charbuf
,
4524 source_end
- source
, charbuf_end
- charbuf
,
4526 source
+= ccl
.consumed
;
4527 charbuf
+= ccl
.produced
;
4528 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4531 if (source
< source_end
)
4532 src
+= source_byteidx
[source
- source_charbuf
];
4535 consumed_chars
+= source
- source_charbuf
;
4537 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4538 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4544 case CCL_STAT_SUSPEND_BY_SRC
:
4545 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4547 case CCL_STAT_SUSPEND_BY_DST
:
4550 case CCL_STAT_INVALID_CMD
:
4551 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4554 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4557 coding
->consumed_char
+= consumed_chars
;
4558 coding
->consumed
= src
- coding
->source
;
4559 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4563 encode_coding_ccl (coding
)
4564 struct coding_system
*coding
;
4566 struct ccl_program ccl
;
4567 int multibytep
= coding
->dst_multibyte
;
4568 int *charbuf
= coding
->charbuf
;
4569 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4570 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4571 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4572 int destination_charbuf
[1024];
4573 int i
, produced_chars
= 0;
4574 Lisp_Object attrs
, charset_list
;
4576 CODING_GET_INFO (coding
, attrs
, charset_list
);
4577 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4579 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4580 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4582 while (charbuf
< charbuf_end
)
4584 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4585 charbuf_end
- charbuf
, 1024, charset_list
);
4588 ASSURE_DESTINATION (ccl
.produced
* 2);
4589 for (i
= 0; i
< ccl
.produced
; i
++)
4590 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4594 ASSURE_DESTINATION (ccl
.produced
);
4595 for (i
= 0; i
< ccl
.produced
; i
++)
4596 *dst
++ = destination_charbuf
[i
] & 0xFF;
4597 produced_chars
+= ccl
.produced
;
4599 charbuf
+= ccl
.consumed
;
4600 if (ccl
.status
== CCL_STAT_QUIT
4601 || ccl
.status
== CCL_STAT_INVALID_CMD
)
4607 case CCL_STAT_SUSPEND_BY_SRC
:
4608 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4610 case CCL_STAT_SUSPEND_BY_DST
:
4611 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4614 case CCL_STAT_INVALID_CMD
:
4615 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4618 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4622 coding
->produced_char
+= produced_chars
;
4623 coding
->produced
= dst
- coding
->destination
;
4629 /*** 10, 11. no-conversion handlers ***/
4631 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4634 decode_coding_raw_text (coding
)
4635 struct coding_system
*coding
;
4637 coding
->chars_at_source
= 1;
4638 coding
->consumed_char
= 0;
4639 coding
->consumed
= 0;
4640 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4644 encode_coding_raw_text (coding
)
4645 struct coding_system
*coding
;
4647 int multibytep
= coding
->dst_multibyte
;
4648 int *charbuf
= coding
->charbuf
;
4649 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
4650 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4651 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4652 int produced_chars
= 0;
4657 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4659 if (coding
->src_multibyte
)
4660 while (charbuf
< charbuf_end
)
4662 ASSURE_DESTINATION (safe_room
);
4664 if (ASCII_CHAR_P (c
))
4665 EMIT_ONE_ASCII_BYTE (c
);
4666 else if (CHAR_BYTE8_P (c
))
4668 c
= CHAR_TO_BYTE8 (c
);
4673 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4675 CHAR_STRING_ADVANCE (c
, p1
);
4678 EMIT_ONE_BYTE (*p0
);
4684 while (charbuf
< charbuf_end
)
4686 ASSURE_DESTINATION (safe_room
);
4693 if (coding
->src_multibyte
)
4695 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4697 while (charbuf
< charbuf_end
)
4699 ASSURE_DESTINATION (safe_room
);
4701 if (ASCII_CHAR_P (c
))
4703 else if (CHAR_BYTE8_P (c
))
4704 *dst
++ = CHAR_TO_BYTE8 (c
);
4706 CHAR_STRING_ADVANCE (c
, dst
);
4712 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4713 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4714 *dst
++ = *charbuf
++;
4715 produced_chars
= dst
- (coding
->destination
+ coding
->dst_bytes
);
4718 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4719 coding
->produced_char
+= produced_chars
;
4720 coding
->produced
= dst
- coding
->destination
;
4724 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4725 Check if a text is encoded in a charset-based coding system. If it
4726 is, return 1, else return 0. */
4729 detect_coding_charset (coding
, detect_info
)
4730 struct coding_system
*coding
;
4731 struct coding_detection_info
*detect_info
;
4733 const unsigned char *src
= coding
->source
, *src_base
;
4734 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4735 int multibytep
= coding
->src_multibyte
;
4736 int consumed_chars
= 0;
4737 Lisp_Object attrs
, valids
;
4739 int head_ascii
= coding
->head_ascii
;
4741 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4743 coding
= &coding_categories
[coding_category_charset
];
4744 attrs
= CODING_ID_ATTRS (coding
->id
);
4745 valids
= AREF (attrs
, coding_attr_charset_valids
);
4747 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4754 struct charset
*charset
;
4761 val
= AREF (valids
, c
);
4765 found
= CATEGORY_MASK_CHARSET
;
4768 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4769 dim
= CHARSET_DIMENSION (charset
);
4770 for (idx
= 1; idx
< dim
; idx
++)
4775 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
4776 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
4785 for (; CONSP (val
); val
= XCDR (val
))
4787 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4788 dim
= CHARSET_DIMENSION (charset
);
4794 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
4795 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
4810 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4814 detect_info
->found
|= found
;
4819 decode_coding_charset (coding
)
4820 struct coding_system
*coding
;
4822 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4823 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4824 const unsigned char *src_base
;
4825 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4827 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4828 int consumed_chars
= 0, consumed_chars_base
;
4829 int multibytep
= coding
->src_multibyte
;
4830 Lisp_Object attrs
, charset_list
, valids
;
4831 int char_offset
= coding
->produced_char
;
4832 int last_offset
= char_offset
;
4833 int last_id
= charset_ascii
;
4835 CODING_GET_INFO (coding
, attrs
, charset_list
);
4836 valids
= AREF (attrs
, coding_attr_charset_valids
);
4842 struct charset
*charset
;
4848 consumed_chars_base
= consumed_chars
;
4850 if (charbuf
>= charbuf_end
)
4858 val
= AREF (valids
, c
);
4863 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4864 dim
= CHARSET_DIMENSION (charset
);
4868 code
= (code
<< 8) | c
;
4871 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
4876 /* VAL is a list of charset IDs. It is assured that the
4877 list is sorted by charset dimensions (smaller one
4881 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4882 dim
= CHARSET_DIMENSION (charset
);
4886 code
= (code
<< 8) | c
;
4889 CODING_DECODE_CHAR (coding
, src
, src_base
,
4890 src_end
, charset
, code
, c
);
4898 if (charset
->id
!= charset_ascii
4899 && last_id
!= charset
->id
)
4901 if (last_id
!= charset_ascii
)
4902 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4903 last_id
= charset
->id
;
4904 last_offset
= char_offset
;
4913 consumed_chars
= consumed_chars_base
;
4915 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4921 if (last_id
!= charset_ascii
)
4922 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4923 coding
->consumed_char
+= consumed_chars_base
;
4924 coding
->consumed
= src_base
- coding
->source
;
4925 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4929 encode_coding_charset (coding
)
4930 struct coding_system
*coding
;
4932 int multibytep
= coding
->dst_multibyte
;
4933 int *charbuf
= coding
->charbuf
;
4934 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4935 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4936 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4937 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4938 int produced_chars
= 0;
4939 Lisp_Object attrs
, charset_list
;
4940 int ascii_compatible
;
4943 CODING_GET_INFO (coding
, attrs
, charset_list
);
4944 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4946 while (charbuf
< charbuf_end
)
4948 struct charset
*charset
;
4951 ASSURE_DESTINATION (safe_room
);
4953 if (ascii_compatible
&& ASCII_CHAR_P (c
))
4954 EMIT_ONE_ASCII_BYTE (c
);
4955 else if (CHAR_BYTE8_P (c
))
4957 c
= CHAR_TO_BYTE8 (c
);
4962 charset
= char_charset (c
, charset_list
, &code
);
4965 if (CHARSET_DIMENSION (charset
) == 1)
4966 EMIT_ONE_BYTE (code
);
4967 else if (CHARSET_DIMENSION (charset
) == 2)
4968 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
4969 else if (CHARSET_DIMENSION (charset
) == 3)
4970 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
4972 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
4973 (code
>> 8) & 0xFF, code
& 0xFF);
4977 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4978 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4980 c
= coding
->default_char
;
4986 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4987 coding
->produced_char
+= produced_chars
;
4988 coding
->produced
= dst
- coding
->destination
;
4993 /*** 7. C library functions ***/
4995 /* Setup coding context CODING from information about CODING_SYSTEM.
4996 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4997 CODING_SYSTEM is invalid, signal an error. */
5000 setup_coding_system (coding_system
, coding
)
5001 Lisp_Object coding_system
;
5002 struct coding_system
*coding
;
5005 Lisp_Object eol_type
;
5006 Lisp_Object coding_type
;
5009 if (NILP (coding_system
))
5010 coding_system
= Qundecided
;
5012 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5014 attrs
= CODING_ID_ATTRS (coding
->id
);
5015 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5018 coding
->head_ascii
= -1;
5019 if (VECTORP (eol_type
))
5020 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5021 | CODING_REQUIRE_DETECTION_MASK
);
5022 else if (! EQ (eol_type
, Qunix
))
5023 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5024 | CODING_REQUIRE_ENCODING_MASK
);
5026 coding
->common_flags
= 0;
5027 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5028 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5029 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5030 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5031 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5032 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5034 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5035 coding
->max_charset_id
= SCHARS (val
) - 1;
5036 coding
->safe_charsets
= (char *) SDATA (val
);
5037 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5039 coding_type
= CODING_ATTR_TYPE (attrs
);
5040 if (EQ (coding_type
, Qundecided
))
5042 coding
->detector
= NULL
;
5043 coding
->decoder
= decode_coding_raw_text
;
5044 coding
->encoder
= encode_coding_raw_text
;
5045 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5047 else if (EQ (coding_type
, Qiso_2022
))
5050 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5052 /* Invoke graphic register 0 to plane 0. */
5053 CODING_ISO_INVOCATION (coding
, 0) = 0;
5054 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5055 CODING_ISO_INVOCATION (coding
, 1)
5056 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5057 /* Setup the initial status of designation. */
5058 for (i
= 0; i
< 4; i
++)
5059 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5060 /* Not single shifting initially. */
5061 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5062 /* Beginning of buffer should also be regarded as bol. */
5063 CODING_ISO_BOL (coding
) = 1;
5064 coding
->detector
= detect_coding_iso_2022
;
5065 coding
->decoder
= decode_coding_iso_2022
;
5066 coding
->encoder
= encode_coding_iso_2022
;
5067 if (flags
& CODING_ISO_FLAG_SAFE
)
5068 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5069 coding
->common_flags
5070 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5071 | CODING_REQUIRE_FLUSHING_MASK
);
5072 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5073 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5074 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5075 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5076 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5078 setup_iso_safe_charsets (attrs
);
5079 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5080 coding
->max_charset_id
= SCHARS (val
) - 1;
5081 coding
->safe_charsets
= (char *) SDATA (val
);
5083 CODING_ISO_FLAGS (coding
) = flags
;
5085 else if (EQ (coding_type
, Qcharset
))
5087 coding
->detector
= detect_coding_charset
;
5088 coding
->decoder
= decode_coding_charset
;
5089 coding
->encoder
= encode_coding_charset
;
5090 coding
->common_flags
5091 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5093 else if (EQ (coding_type
, Qutf_8
))
5095 coding
->detector
= detect_coding_utf_8
;
5096 coding
->decoder
= decode_coding_utf_8
;
5097 coding
->encoder
= encode_coding_utf_8
;
5098 coding
->common_flags
5099 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5101 else if (EQ (coding_type
, Qutf_16
))
5103 val
= AREF (attrs
, coding_attr_utf_16_bom
);
5104 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
5105 : EQ (val
, Qt
) ? utf_16_with_bom
5106 : utf_16_without_bom
);
5107 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5108 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5109 : utf_16_little_endian
);
5110 CODING_UTF_16_SURROGATE (coding
) = 0;
5111 coding
->detector
= detect_coding_utf_16
;
5112 coding
->decoder
= decode_coding_utf_16
;
5113 coding
->encoder
= encode_coding_utf_16
;
5114 coding
->common_flags
5115 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5116 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
5117 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5119 else if (EQ (coding_type
, Qccl
))
5121 coding
->detector
= detect_coding_ccl
;
5122 coding
->decoder
= decode_coding_ccl
;
5123 coding
->encoder
= encode_coding_ccl
;
5124 coding
->common_flags
5125 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5126 | CODING_REQUIRE_FLUSHING_MASK
);
5128 else if (EQ (coding_type
, Qemacs_mule
))
5130 coding
->detector
= detect_coding_emacs_mule
;
5131 coding
->decoder
= decode_coding_emacs_mule
;
5132 coding
->encoder
= encode_coding_emacs_mule
;
5133 coding
->common_flags
5134 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5135 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5136 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5138 Lisp_Object tail
, safe_charsets
;
5139 int max_charset_id
= 0;
5141 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5143 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5144 max_charset_id
= XFASTINT (XCAR (tail
));
5145 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5147 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5149 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5150 coding
->max_charset_id
= max_charset_id
;
5151 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5154 else if (EQ (coding_type
, Qshift_jis
))
5156 coding
->detector
= detect_coding_sjis
;
5157 coding
->decoder
= decode_coding_sjis
;
5158 coding
->encoder
= encode_coding_sjis
;
5159 coding
->common_flags
5160 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5162 else if (EQ (coding_type
, Qbig5
))
5164 coding
->detector
= detect_coding_big5
;
5165 coding
->decoder
= decode_coding_big5
;
5166 coding
->encoder
= encode_coding_big5
;
5167 coding
->common_flags
5168 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5170 else /* EQ (coding_type, Qraw_text) */
5172 coding
->detector
= NULL
;
5173 coding
->decoder
= decode_coding_raw_text
;
5174 coding
->encoder
= encode_coding_raw_text
;
5175 if (! EQ (eol_type
, Qunix
))
5177 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5178 if (! VECTORP (eol_type
))
5179 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5187 /* Return a list of charsets supported by CODING. */
5190 coding_charset_list (coding
)
5191 struct coding_system
*coding
;
5193 Lisp_Object attrs
, charset_list
;
5195 CODING_GET_INFO (coding
, attrs
, charset_list
);
5196 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5198 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5200 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5201 charset_list
= Viso_2022_charset_list
;
5203 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5205 charset_list
= Vemacs_mule_charset_list
;
5207 return charset_list
;
5211 /* Return raw-text or one of its subsidiaries that has the same
5212 eol_type as CODING-SYSTEM. */
5215 raw_text_coding_system (coding_system
)
5216 Lisp_Object coding_system
;
5218 Lisp_Object spec
, attrs
;
5219 Lisp_Object eol_type
, raw_text_eol_type
;
5221 if (NILP (coding_system
))
5223 spec
= CODING_SYSTEM_SPEC (coding_system
);
5224 attrs
= AREF (spec
, 0);
5226 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5227 return coding_system
;
5229 eol_type
= AREF (spec
, 2);
5230 if (VECTORP (eol_type
))
5232 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5233 raw_text_eol_type
= AREF (spec
, 2);
5234 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5235 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5236 : AREF (raw_text_eol_type
, 2));
5240 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5241 does, return one of the subsidiary that has the same eol-spec as
5242 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5243 inherit end-of-line format from the system's setting
5244 (system_eol_type). */
5247 coding_inherit_eol_type (coding_system
, parent
)
5248 Lisp_Object coding_system
, parent
;
5250 Lisp_Object spec
, eol_type
;
5252 if (NILP (coding_system
))
5253 coding_system
= Qraw_text
;
5254 spec
= CODING_SYSTEM_SPEC (coding_system
);
5255 eol_type
= AREF (spec
, 2);
5256 if (VECTORP (eol_type
))
5258 Lisp_Object parent_eol_type
;
5260 if (! NILP (parent
))
5262 Lisp_Object parent_spec
;
5264 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5265 parent_eol_type
= AREF (parent_spec
, 2);
5268 parent_eol_type
= system_eol_type
;
5269 if (EQ (parent_eol_type
, Qunix
))
5270 coding_system
= AREF (eol_type
, 0);
5271 else if (EQ (parent_eol_type
, Qdos
))
5272 coding_system
= AREF (eol_type
, 1);
5273 else if (EQ (parent_eol_type
, Qmac
))
5274 coding_system
= AREF (eol_type
, 2);
5276 return coding_system
;
5279 /* Emacs has a mechanism to automatically detect a coding system if it
5280 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5281 it's impossible to distinguish some coding systems accurately
5282 because they use the same range of codes. So, at first, coding
5283 systems are categorized into 7, those are:
5285 o coding-category-emacs-mule
5287 The category for a coding system which has the same code range
5288 as Emacs' internal format. Assigned the coding-system (Lisp
5289 symbol) `emacs-mule' by default.
5291 o coding-category-sjis
5293 The category for a coding system which has the same code range
5294 as SJIS. Assigned the coding-system (Lisp
5295 symbol) `japanese-shift-jis' by default.
5297 o coding-category-iso-7
5299 The category for a coding system which has the same code range
5300 as ISO2022 of 7-bit environment. This doesn't use any locking
5301 shift and single shift functions. This can encode/decode all
5302 charsets. Assigned the coding-system (Lisp symbol)
5303 `iso-2022-7bit' by default.
5305 o coding-category-iso-7-tight
5307 Same as coding-category-iso-7 except that this can
5308 encode/decode only the specified charsets.
5310 o coding-category-iso-8-1
5312 The category for a coding system which has the same code range
5313 as ISO2022 of 8-bit environment and graphic plane 1 used only
5314 for DIMENSION1 charset. This doesn't use any locking shift
5315 and single shift functions. Assigned the coding-system (Lisp
5316 symbol) `iso-latin-1' by default.
5318 o coding-category-iso-8-2
5320 The category for a coding system which has the same code range
5321 as ISO2022 of 8-bit environment and graphic plane 1 used only
5322 for DIMENSION2 charset. This doesn't use any locking shift
5323 and single shift functions. Assigned the coding-system (Lisp
5324 symbol) `japanese-iso-8bit' by default.
5326 o coding-category-iso-7-else
5328 The category for a coding system which has the same code range
5329 as ISO2022 of 7-bit environemnt but uses locking shift or
5330 single shift functions. Assigned the coding-system (Lisp
5331 symbol) `iso-2022-7bit-lock' by default.
5333 o coding-category-iso-8-else
5335 The category for a coding system which has the same code range
5336 as ISO2022 of 8-bit environemnt but uses locking shift or
5337 single shift functions. Assigned the coding-system (Lisp
5338 symbol) `iso-2022-8bit-ss2' by default.
5340 o coding-category-big5
5342 The category for a coding system which has the same code range
5343 as BIG5. Assigned the coding-system (Lisp symbol)
5344 `cn-big5' by default.
5346 o coding-category-utf-8
5348 The category for a coding system which has the same code range
5349 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5350 symbol) `utf-8' by default.
5352 o coding-category-utf-16-be
5354 The category for a coding system in which a text has an
5355 Unicode signature (cf. Unicode Standard) in the order of BIG
5356 endian at the head. Assigned the coding-system (Lisp symbol)
5357 `utf-16-be' by default.
5359 o coding-category-utf-16-le
5361 The category for a coding system in which a text has an
5362 Unicode signature (cf. Unicode Standard) in the order of
5363 LITTLE endian at the head. Assigned the coding-system (Lisp
5364 symbol) `utf-16-le' by default.
5366 o coding-category-ccl
5368 The category for a coding system of which encoder/decoder is
5369 written in CCL programs. The default value is nil, i.e., no
5370 coding system is assigned.
5372 o coding-category-binary
5374 The category for a coding system not categorized in any of the
5375 above. Assigned the coding-system (Lisp symbol)
5376 `no-conversion' by default.
5378 Each of them is a Lisp symbol and the value is an actual
5379 `coding-system's (this is also a Lisp symbol) assigned by a user.
5380 What Emacs does actually is to detect a category of coding system.
5381 Then, it uses a `coding-system' assigned to it. If Emacs can't
5382 decide only one possible category, it selects a category of the
5383 highest priority. Priorities of categories are also specified by a
5384 user in a Lisp variable `coding-category-list'.
5388 #define EOL_SEEN_NONE 0
5389 #define EOL_SEEN_LF 1
5390 #define EOL_SEEN_CR 2
5391 #define EOL_SEEN_CRLF 4
5393 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5394 SOURCE is encoded. If CATEGORY is one of
5395 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5396 two-byte, else they are encoded by one-byte.
5398 Return one of EOL_SEEN_XXX. */
5400 #define MAX_EOL_CHECK_COUNT 3
5403 detect_eol (source
, src_bytes
, category
)
5404 const unsigned char *source
;
5405 EMACS_INT src_bytes
;
5406 enum coding_category category
;
5408 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5411 int eol_seen
= EOL_SEEN_NONE
;
5413 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5417 msb
= category
== (coding_category_utf_16_le
5418 | coding_category_utf_16_le_nosig
);
5421 while (src
+ 1 < src_end
)
5424 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5429 this_eol
= EOL_SEEN_LF
;
5430 else if (src
+ 3 >= src_end
5431 || src
[msb
+ 2] != 0
5432 || src
[lsb
+ 2] != '\n')
5433 this_eol
= EOL_SEEN_CR
;
5435 this_eol
= EOL_SEEN_CRLF
;
5437 if (eol_seen
== EOL_SEEN_NONE
)
5438 /* This is the first end-of-line. */
5439 eol_seen
= this_eol
;
5440 else if (eol_seen
!= this_eol
)
5442 /* The found type is different from what found before. */
5443 eol_seen
= EOL_SEEN_LF
;
5446 if (++total
== MAX_EOL_CHECK_COUNT
)
5454 while (src
< src_end
)
5457 if (c
== '\n' || c
== '\r')
5462 this_eol
= EOL_SEEN_LF
;
5463 else if (src
>= src_end
|| *src
!= '\n')
5464 this_eol
= EOL_SEEN_CR
;
5466 this_eol
= EOL_SEEN_CRLF
, src
++;
5468 if (eol_seen
== EOL_SEEN_NONE
)
5469 /* This is the first end-of-line. */
5470 eol_seen
= this_eol
;
5471 else if (eol_seen
!= this_eol
)
5473 /* The found type is different from what found before. */
5474 eol_seen
= EOL_SEEN_LF
;
5477 if (++total
== MAX_EOL_CHECK_COUNT
)
5487 adjust_coding_eol_type (coding
, eol_seen
)
5488 struct coding_system
*coding
;
5491 Lisp_Object eol_type
;
5493 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5494 if (eol_seen
& EOL_SEEN_LF
)
5496 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5499 else if (eol_seen
& EOL_SEEN_CRLF
)
5501 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5504 else if (eol_seen
& EOL_SEEN_CR
)
5506 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5512 /* Detect how a text specified in CODING is encoded. If a coding
5513 system is detected, update fields of CODING by the detected coding
5517 detect_coding (coding
)
5518 struct coding_system
*coding
;
5520 const unsigned char *src
, *src_end
;
5522 coding
->consumed
= coding
->consumed_char
= 0;
5523 coding
->produced
= coding
->produced_char
= 0;
5524 coding_set_source (coding
);
5526 src_end
= coding
->source
+ coding
->src_bytes
;
5528 /* If we have not yet decided the text encoding type, detect it
5530 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5533 struct coding_detection_info detect_info
;
5535 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5536 for (i
= 0, src
= coding
->source
; src
< src_end
; i
++, src
++)
5542 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5543 && ! inhibit_iso_escape_detection
5544 && ! detect_info
.checked
)
5546 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5547 if (detect_coding_iso_2022 (coding
, &detect_info
))
5549 /* We have scanned the whole data. */
5550 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5551 /* We didn't find an 8-bit code. */
5557 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5559 if (coding
->head_ascii
< coding
->src_bytes
5560 || detect_info
.found
)
5562 enum coding_category category
;
5563 struct coding_system
*this;
5565 if (coding
->head_ascii
== coding
->src_bytes
)
5566 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5567 for (i
= 0; i
< coding_category_raw_text
; i
++)
5569 category
= coding_priorities
[i
];
5570 this = coding_categories
+ category
;
5571 if (detect_info
.found
& (1 << category
))
5575 for (i
= 0; i
< coding_category_raw_text
; i
++)
5577 category
= coding_priorities
[i
];
5578 this = coding_categories
+ category
;
5581 /* No coding system of this category is defined. */
5582 detect_info
.rejected
|= (1 << category
);
5584 else if (category
>= coding_category_raw_text
)
5586 else if (detect_info
.checked
& (1 << category
))
5588 if (detect_info
.found
& (1 << category
))
5591 else if ((*(this->detector
)) (coding
, &detect_info
)
5592 && detect_info
.found
& (1 << category
))
5594 if (category
== coding_category_utf_16_auto
)
5596 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5597 category
= coding_category_utf_16_le
;
5599 category
= coding_category_utf_16_be
;
5605 if (i
< coding_category_raw_text
)
5606 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5607 else if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
5608 setup_coding_system (Qraw_text
, coding
);
5609 else if (detect_info
.rejected
)
5610 for (i
= 0; i
< coding_category_raw_text
; i
++)
5611 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5613 this = coding_categories
+ coding_priorities
[i
];
5614 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5619 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5620 == coding_category_utf_16_auto
)
5622 Lisp_Object coding_systems
;
5623 struct coding_detection_info detect_info
;
5626 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5627 detect_info
.found
= detect_info
.rejected
= 0;
5628 if (CONSP (coding_systems
)
5629 && detect_coding_utf_16 (coding
, &detect_info
))
5631 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5632 setup_coding_system (XCAR (coding_systems
), coding
);
5633 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5634 setup_coding_system (XCDR (coding_systems
), coding
);
5642 struct coding_system
*coding
;
5644 Lisp_Object eol_type
;
5645 unsigned char *p
, *pbeg
, *pend
;
5647 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5648 if (EQ (eol_type
, Qunix
))
5651 if (NILP (coding
->dst_object
))
5652 pbeg
= coding
->destination
;
5654 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5655 pend
= pbeg
+ coding
->produced
;
5657 if (VECTORP (eol_type
))
5659 int eol_seen
= EOL_SEEN_NONE
;
5661 for (p
= pbeg
; p
< pend
; p
++)
5664 eol_seen
|= EOL_SEEN_LF
;
5665 else if (*p
== '\r')
5667 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5669 eol_seen
|= EOL_SEEN_CRLF
;
5673 eol_seen
|= EOL_SEEN_CR
;
5676 if (eol_seen
!= EOL_SEEN_NONE
5677 && eol_seen
!= EOL_SEEN_LF
5678 && eol_seen
!= EOL_SEEN_CRLF
5679 && eol_seen
!= EOL_SEEN_CR
)
5680 eol_seen
= EOL_SEEN_LF
;
5681 if (eol_seen
!= EOL_SEEN_NONE
)
5682 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5685 if (EQ (eol_type
, Qmac
))
5687 for (p
= pbeg
; p
< pend
; p
++)
5691 else if (EQ (eol_type
, Qdos
))
5695 if (NILP (coding
->dst_object
))
5697 /* Start deleting '\r' from the tail to minimize the memory
5699 for (p
= pend
- 2; p
>= pbeg
; p
--)
5702 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5708 int pos_byte
= coding
->dst_pos_byte
;
5709 int pos
= coding
->dst_pos
;
5710 int pos_end
= pos
+ coding
->produced_char
- 1;
5712 while (pos
< pos_end
)
5714 p
= BYTE_POS_ADDR (pos_byte
);
5715 if (*p
== '\r' && p
[1] == '\n')
5717 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5722 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
5725 coding
->produced
-= n
;
5726 coding
->produced_char
-= n
;
5731 /* Return a translation table (or list of them) from coding system
5732 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5733 decoding (ENCODEP is zero). */
5736 get_translation_table (attrs
, encodep
, max_lookup
)
5738 int encodep
, *max_lookup
;
5740 Lisp_Object standard
, translation_table
;
5744 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5745 standard
= Vstandard_translation_table_for_encode
;
5747 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5748 standard
= Vstandard_translation_table_for_decode
;
5749 if (NILP (translation_table
))
5750 translation_table
= standard
;
5753 if (SYMBOLP (translation_table
))
5754 translation_table
= Fget (translation_table
, Qtranslation_table
);
5755 else if (CONSP (translation_table
))
5757 translation_table
= Fcopy_sequence (translation_table
);
5758 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5759 if (SYMBOLP (XCAR (val
)))
5760 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5762 if (CHAR_TABLE_P (standard
))
5764 if (CONSP (translation_table
))
5765 translation_table
= nconc2 (translation_table
,
5766 Fcons (standard
, Qnil
));
5768 translation_table
= Fcons (translation_table
,
5769 Fcons (standard
, Qnil
));
5776 if (CHAR_TABLE_P (translation_table
)
5777 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5779 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5780 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5781 *max_lookup
= XFASTINT (val
);
5783 else if (CONSP (translation_table
))
5785 Lisp_Object tail
, val
;
5787 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
5788 if (CHAR_TABLE_P (XCAR (tail
))
5789 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
5791 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
5792 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5793 *max_lookup
= XFASTINT (val
);
5797 return translation_table
;
5800 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5803 if (CHAR_TABLE_P (table)) \
5805 trans = CHAR_TABLE_REF (table, c); \
5806 if (CHARACTERP (trans)) \
5807 c = XFASTINT (trans), trans = Qnil; \
5809 else if (CONSP (table)) \
5813 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5814 if (CHAR_TABLE_P (XCAR (tail))) \
5816 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5817 if (CHARACTERP (trans)) \
5818 c = XFASTINT (trans), trans = Qnil; \
5819 else if (! NILP (trans)) \
5827 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
5831 int *from_nchars
, *to_nchars
;
5833 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5837 Lisp_Object from
, tail
;
5840 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
5845 for (i
= 0; i
< len
; i
++)
5847 if (buf
+ i
== buf_end
)
5853 if (XINT (AREF (from
, i
)) != buf
[i
])
5867 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
5875 produce_chars (coding
, translation_table
, last_block
)
5876 struct coding_system
*coding
;
5877 Lisp_Object translation_table
;
5880 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5881 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5883 int produced_chars
= 0;
5886 if (! coding
->chars_at_source
)
5888 /* Characters are in coding->charbuf. */
5889 int *buf
= coding
->charbuf
;
5890 int *buf_end
= buf
+ coding
->charbuf_used
;
5892 if (BUFFERP (coding
->src_object
)
5893 && EQ (coding
->src_object
, coding
->dst_object
))
5894 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
5896 while (buf
< buf_end
)
5902 int from_nchars
= 1, to_nchars
= 1;
5903 Lisp_Object trans
= Qnil
;
5905 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
5908 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
5909 &from_nchars
, &to_nchars
);
5915 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
5917 dst
= alloc_destination (coding
,
5919 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
5921 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5924 for (i
= 0; i
< to_nchars
; i
++)
5927 c
= XINT (AREF (trans
, i
));
5928 if (coding
->dst_multibyte
5929 || ! CHAR_BYTE8_P (c
))
5930 CHAR_STRING_ADVANCE (c
, dst
);
5932 *dst
++ = CHAR_TO_BYTE8 (c
);
5934 produced_chars
+= to_nchars
;
5936 while (--from_nchars
> 0)
5940 /* This is an annotation datum. (-C) is the length. */
5943 carryover
= buf_end
- buf
;
5947 const unsigned char *src
= coding
->source
;
5948 const unsigned char *src_end
= src
+ coding
->src_bytes
;
5949 Lisp_Object eol_type
;
5951 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5953 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
5955 if (coding
->src_multibyte
)
5962 const unsigned char *src_base
= src
;
5968 if (EQ (eol_type
, Qdos
))
5972 record_conversion_result
5973 (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5974 goto no_more_source
;
5979 else if (EQ (eol_type
, Qmac
))
5984 coding
->consumed
= src
- coding
->source
;
5986 if (EQ (coding
->src_object
, coding
->dst_object
))
5987 dst_end
= (unsigned char *) src
;
5990 dst
= alloc_destination (coding
, src_end
- src
+ 1,
5992 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5993 coding_set_source (coding
);
5994 src
= coding
->source
+ coding
->consumed
;
5995 src_end
= coding
->source
+ coding
->src_bytes
;
6005 while (src
< src_end
)
6012 if (EQ (eol_type
, Qdos
))
6018 else if (EQ (eol_type
, Qmac
))
6021 if (dst
>= dst_end
- 1)
6023 coding
->consumed
= src
- coding
->source
;
6025 if (EQ (coding
->src_object
, coding
->dst_object
))
6026 dst_end
= (unsigned char *) src
;
6027 if (dst
>= dst_end
- 1)
6029 dst
= alloc_destination (coding
, src_end
- src
+ 2,
6031 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6032 coding_set_source (coding
);
6033 src
= coding
->source
+ coding
->consumed
;
6034 src_end
= coding
->source
+ coding
->src_bytes
;
6042 if (!EQ (coding
->src_object
, coding
->dst_object
))
6044 int require
= coding
->src_bytes
- coding
->dst_bytes
;
6048 EMACS_INT offset
= src
- coding
->source
;
6050 dst
= alloc_destination (coding
, require
, dst
);
6051 coding_set_source (coding
);
6052 src
= coding
->source
+ offset
;
6053 src_end
= coding
->source
+ coding
->src_bytes
;
6056 produced_chars
= coding
->src_chars
;
6057 while (src
< src_end
)
6063 if (EQ (eol_type
, Qdos
))
6070 else if (EQ (eol_type
, Qmac
))
6076 coding
->consumed
= coding
->src_bytes
;
6077 coding
->consumed_char
= coding
->src_chars
;
6080 produced
= dst
- (coding
->destination
+ coding
->produced
);
6081 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6082 insert_from_gap (produced_chars
, produced
);
6083 coding
->produced
+= produced
;
6084 coding
->produced_char
+= produced_chars
;
6088 /* Compose text in CODING->object according to the annotation data at
6089 CHARBUF. CHARBUF is an array:
6090 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6094 produce_composition (coding
, charbuf
, pos
)
6095 struct coding_system
*coding
;
6101 enum composition_method method
;
6102 Lisp_Object components
;
6105 to
= pos
+ charbuf
[2];
6108 method
= (enum composition_method
) (charbuf
[3]);
6110 if (method
== COMPOSITION_RELATIVE
)
6112 else if (method
>= COMPOSITION_WITH_RULE
6113 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6115 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6120 for (i
= 0; i
< len
; i
++)
6122 args
[i
] = make_number (charbuf
[i
]);
6126 components
= (method
== COMPOSITION_WITH_ALTCHARS
6127 ? Fstring (len
, args
) : Fvector (len
, args
));
6131 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6135 /* Put `charset' property on text in CODING->object according to
6136 the annotation data at CHARBUF. CHARBUF is an array:
6137 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6141 produce_charset (coding
, charbuf
, pos
)
6142 struct coding_system
*coding
;
6146 EMACS_INT from
= pos
- charbuf
[2];
6147 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6149 Fput_text_property (make_number (from
), make_number (pos
),
6150 Qcharset
, CHARSET_NAME (charset
),
6151 coding
->dst_object
);
6155 #define CHARBUF_SIZE 0x4000
6157 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6159 int size = CHARBUF_SIZE;; \
6161 coding->charbuf = NULL; \
6162 while (size > 1024) \
6164 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6165 if (coding->charbuf) \
6169 if (! coding->charbuf) \
6171 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6172 return coding->result; \
6174 coding->charbuf_size = size; \
6179 produce_annotation (coding
, pos
)
6180 struct coding_system
*coding
;
6183 int *charbuf
= coding
->charbuf
;
6184 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6186 if (NILP (coding
->dst_object
))
6189 while (charbuf
< charbuf_end
)
6195 int len
= -*charbuf
;
6198 case CODING_ANNOTATE_COMPOSITION_MASK
:
6199 produce_composition (coding
, charbuf
, pos
);
6201 case CODING_ANNOTATE_CHARSET_MASK
:
6202 produce_charset (coding
, charbuf
, pos
);
6212 /* Decode the data at CODING->src_object into CODING->dst_object.
6213 CODING->src_object is a buffer, a string, or nil.
6214 CODING->dst_object is a buffer.
6216 If CODING->src_object is a buffer, it must be the current buffer.
6217 In this case, if CODING->src_pos is positive, it is a position of
6218 the source text in the buffer, otherwise, the source text is in the
6219 gap area of the buffer, and CODING->src_pos specifies the offset of
6220 the text from GPT (which must be the same as PT). If this is the
6221 same buffer as CODING->dst_object, CODING->src_pos must be
6224 If CODING->src_object is a string, CODING->src_pos is an index to
6227 If CODING->src_object is nil, CODING->source must already point to
6228 the non-relocatable memory area. In this case, CODING->src_pos is
6229 an offset from CODING->source.
6231 The decoded data is inserted at the current point of the buffer
6236 decode_coding (coding
)
6237 struct coding_system
*coding
;
6240 Lisp_Object undo_list
;
6241 Lisp_Object translation_table
;
6245 if (BUFFERP (coding
->src_object
)
6246 && coding
->src_pos
> 0
6247 && coding
->src_pos
< GPT
6248 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6249 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6252 if (BUFFERP (coding
->dst_object
))
6254 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6255 set_buffer_internal (XBUFFER (coding
->dst_object
));
6257 move_gap_both (PT
, PT_BYTE
);
6258 undo_list
= current_buffer
->undo_list
;
6259 current_buffer
->undo_list
= Qt
;
6262 coding
->consumed
= coding
->consumed_char
= 0;
6263 coding
->produced
= coding
->produced_char
= 0;
6264 coding
->chars_at_source
= 0;
6265 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6268 ALLOC_CONVERSION_WORK_AREA (coding
);
6270 attrs
= CODING_ID_ATTRS (coding
->id
);
6271 translation_table
= get_translation_table (attrs
, 0, NULL
);
6276 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6278 coding_set_source (coding
);
6279 coding
->annotated
= 0;
6280 coding
->charbuf_used
= carryover
;
6281 (*(coding
->decoder
)) (coding
);
6282 coding_set_destination (coding
);
6283 carryover
= produce_chars (coding
, translation_table
, 0);
6284 if (coding
->annotated
)
6285 produce_annotation (coding
, pos
);
6286 for (i
= 0; i
< carryover
; i
++)
6288 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6290 while (coding
->consumed
< coding
->src_bytes
6291 && (coding
->result
== CODING_RESULT_SUCCESS
6292 || coding
->result
== CODING_RESULT_INVALID_SRC
));
6296 coding_set_destination (coding
);
6297 coding
->charbuf_used
= carryover
;
6298 produce_chars (coding
, translation_table
, 1);
6301 coding
->carryover_bytes
= 0;
6302 if (coding
->consumed
< coding
->src_bytes
)
6304 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6305 const unsigned char *src
;
6307 coding_set_source (coding
);
6308 coding_set_destination (coding
);
6309 src
= coding
->source
+ coding
->consumed
;
6311 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6313 /* Flush out unprocessed data as binary chars. We are sure
6314 that the number of data is less than the size of
6316 coding
->charbuf_used
= 0;
6317 while (nbytes
-- > 0)
6322 c
= BYTE8_TO_CHAR (c
);
6323 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6325 produce_chars (coding
, Qnil
, 1);
6329 /* Record unprocessed bytes in coding->carryover. We are
6330 sure that the number of data is less than the size of
6331 coding->carryover. */
6332 unsigned char *p
= coding
->carryover
;
6334 coding
->carryover_bytes
= nbytes
;
6335 while (nbytes
-- > 0)
6338 coding
->consumed
= coding
->src_bytes
;
6341 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6342 decode_eol (coding
);
6343 if (BUFFERP (coding
->dst_object
))
6345 current_buffer
->undo_list
= undo_list
;
6346 record_insert (coding
->dst_pos
, coding
->produced_char
);
6348 return coding
->result
;
6352 /* Extract an annotation datum from a composition starting at POS and
6353 ending before LIMIT of CODING->src_object (buffer or string), store
6354 the data in BUF, set *STOP to a starting position of the next
6355 composition (if any) or to LIMIT, and return the address of the
6356 next element of BUF.
6358 If such an annotation is not found, set *STOP to a starting
6359 position of a composition after POS (if any) or to LIMIT, and
6363 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6364 EMACS_INT pos
, limit
;
6365 struct coding_system
*coding
;
6369 EMACS_INT start
, end
;
6372 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6375 else if (start
> pos
)
6381 /* We found a composition. Store the corresponding
6382 annotation data in BUF. */
6384 enum composition_method method
= COMPOSITION_METHOD (prop
);
6385 int nchars
= COMPOSITION_LENGTH (prop
);
6387 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6388 if (method
!= COMPOSITION_RELATIVE
)
6390 Lisp_Object components
;
6393 components
= COMPOSITION_COMPONENTS (prop
);
6394 if (VECTORP (components
))
6396 len
= XVECTOR (components
)->size
;
6397 for (i
= 0; i
< len
; i
++)
6398 *buf
++ = XINT (AREF (components
, i
));
6400 else if (STRINGP (components
))
6402 len
= SCHARS (components
);
6406 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6410 else if (INTEGERP (components
))
6413 *buf
++ = XINT (components
);
6415 else if (CONSP (components
))
6417 for (len
= 0; CONSP (components
);
6418 len
++, components
= XCDR (components
))
6419 *buf
++ = XINT (XCAR (components
));
6427 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6438 /* Extract an annotation datum from a text property `charset' at POS of
6439 CODING->src_object (buffer of string), store the data in BUF, set
6440 *STOP to the position where the value of `charset' property changes
6441 (limiting by LIMIT), and return the address of the next element of
6444 If the property value is nil, set *STOP to the position where the
6445 property value is non-nil (limiting by LIMIT), and return BUF. */
6448 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6449 EMACS_INT pos
, limit
;
6450 struct coding_system
*coding
;
6454 Lisp_Object val
, next
;
6457 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6458 if (! NILP (val
) && CHARSETP (val
))
6459 id
= XINT (CHARSET_SYMBOL_ID (val
));
6462 ADD_CHARSET_DATA (buf
, 0, id
);
6463 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6465 make_number (limit
));
6466 *stop
= XINT (next
);
6472 consume_chars (coding
, translation_table
, max_lookup
)
6473 struct coding_system
*coding
;
6474 Lisp_Object translation_table
;
6477 int *buf
= coding
->charbuf
;
6478 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6479 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6480 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6481 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6482 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6483 int multibytep
= coding
->src_multibyte
;
6484 Lisp_Object eol_type
;
6486 EMACS_INT stop
, stop_composition
, stop_charset
;
6487 int *lookup_buf
= NULL
;
6489 if (! NILP (translation_table
))
6490 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6492 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6493 if (VECTORP (eol_type
))
6496 /* Note: composition handling is not yet implemented. */
6497 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6499 if (NILP (coding
->src_object
))
6500 stop
= stop_composition
= stop_charset
= end_pos
;
6503 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6504 stop
= stop_composition
= pos
;
6506 stop
= stop_composition
= end_pos
;
6507 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6508 stop
= stop_charset
= pos
;
6510 stop_charset
= end_pos
;
6513 /* Compensate for CRLF and conversion. */
6514 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6515 while (buf
< buf_end
)
6523 if (pos
== stop_composition
)
6524 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6525 buf
, &stop_composition
);
6526 if (pos
== stop_charset
)
6527 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6528 buf
, &stop_charset
);
6529 stop
= (stop_composition
< stop_charset
6530 ? stop_composition
: stop_charset
);
6537 if (coding
->encoder
== encode_coding_raw_text
)
6539 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6540 c
= STRING_CHAR_ADVANCE (src
), pos
+= bytes
;
6542 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
6545 c
= STRING_CHAR_ADVANCE (src
), pos
++;
6546 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6548 if (! EQ (eol_type
, Qunix
))
6552 if (EQ (eol_type
, Qdos
))
6560 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6565 int from_nchars
= 1, to_nchars
= 1;
6566 int *lookup_buf_end
;
6567 const unsigned char *p
= src
;
6571 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6572 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6573 lookup_buf_end
= lookup_buf
+ i
;
6574 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6575 &from_nchars
, &to_nchars
);
6577 || buf
+ to_nchars
> buf_end
)
6579 *buf
++ = *lookup_buf
;
6580 for (i
= 1; i
< to_nchars
; i
++)
6581 *buf
++ = XINT (AREF (trans
, i
));
6582 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6583 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6587 coding
->consumed
= src
- coding
->source
;
6588 coding
->consumed_char
= pos
- coding
->src_pos
;
6589 coding
->charbuf_used
= buf
- coding
->charbuf
;
6590 coding
->chars_at_source
= 0;
6594 /* Encode the text at CODING->src_object into CODING->dst_object.
6595 CODING->src_object is a buffer or a string.
6596 CODING->dst_object is a buffer or nil.
6598 If CODING->src_object is a buffer, it must be the current buffer.
6599 In this case, if CODING->src_pos is positive, it is a position of
6600 the source text in the buffer, otherwise. the source text is in the
6601 gap area of the buffer, and coding->src_pos specifies the offset of
6602 the text from GPT (which must be the same as PT). If this is the
6603 same buffer as CODING->dst_object, CODING->src_pos must be
6604 negative and CODING should not have `pre-write-conversion'.
6606 If CODING->src_object is a string, CODING should not have
6607 `pre-write-conversion'.
6609 If CODING->dst_object is a buffer, the encoded data is inserted at
6610 the current point of that buffer.
6612 If CODING->dst_object is nil, the encoded data is placed at the
6613 memory area specified by CODING->destination. */
6616 encode_coding (coding
)
6617 struct coding_system
*coding
;
6620 Lisp_Object translation_table
;
6623 attrs
= CODING_ID_ATTRS (coding
->id
);
6624 if (coding
->encoder
== encode_coding_raw_text
)
6625 translation_table
= Qnil
, max_lookup
= 0;
6627 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6629 if (BUFFERP (coding
->dst_object
))
6631 set_buffer_internal (XBUFFER (coding
->dst_object
));
6632 coding
->dst_multibyte
6633 = ! NILP (current_buffer
->enable_multibyte_characters
);
6636 coding
->consumed
= coding
->consumed_char
= 0;
6637 coding
->produced
= coding
->produced_char
= 0;
6638 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6641 ALLOC_CONVERSION_WORK_AREA (coding
);
6644 coding_set_source (coding
);
6645 consume_chars (coding
, translation_table
, max_lookup
);
6646 coding_set_destination (coding
);
6647 (*(coding
->encoder
)) (coding
);
6648 } while (coding
->consumed_char
< coding
->src_chars
);
6650 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
6651 insert_from_gap (coding
->produced_char
, coding
->produced
);
6653 return (coding
->result
);
6657 /* Name (or base name) of work buffer for code conversion. */
6658 static Lisp_Object Vcode_conversion_workbuf_name
;
6660 /* A working buffer used by the top level conversion. Once it is
6661 created, it is never destroyed. It has the name
6662 Vcode_conversion_workbuf_name. The other working buffers are
6663 destroyed after the use is finished, and their names are modified
6664 versions of Vcode_conversion_workbuf_name. */
6665 static Lisp_Object Vcode_conversion_reused_workbuf
;
6667 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6668 static int reused_workbuf_in_use
;
6671 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6672 multibyteness of returning buffer. */
6675 make_conversion_work_buffer (multibyte
)
6678 Lisp_Object name
, workbuf
;
6679 struct buffer
*current
;
6681 if (reused_workbuf_in_use
++)
6683 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6684 workbuf
= Fget_buffer_create (name
);
6688 name
= Vcode_conversion_workbuf_name
;
6689 workbuf
= Fget_buffer_create (name
);
6690 if (NILP (Vcode_conversion_reused_workbuf
))
6691 Vcode_conversion_reused_workbuf
= workbuf
;
6693 current
= current_buffer
;
6694 set_buffer_internal (XBUFFER (workbuf
));
6696 current_buffer
->undo_list
= Qt
;
6697 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6698 set_buffer_internal (current
);
6704 code_conversion_restore (arg
)
6707 Lisp_Object current
, workbuf
;
6708 struct gcpro gcpro1
;
6711 current
= XCAR (arg
);
6712 workbuf
= XCDR (arg
);
6713 if (! NILP (workbuf
))
6715 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6716 reused_workbuf_in_use
= 0;
6717 else if (! NILP (Fbuffer_live_p (workbuf
)))
6718 Fkill_buffer (workbuf
);
6720 set_buffer_internal (XBUFFER (current
));
6726 code_conversion_save (with_work_buf
, multibyte
)
6727 int with_work_buf
, multibyte
;
6729 Lisp_Object workbuf
= Qnil
;
6732 workbuf
= make_conversion_work_buffer (multibyte
);
6733 record_unwind_protect (code_conversion_restore
,
6734 Fcons (Fcurrent_buffer (), workbuf
));
6739 decode_coding_gap (coding
, chars
, bytes
)
6740 struct coding_system
*coding
;
6741 EMACS_INT chars
, bytes
;
6743 int count
= specpdl_ptr
- specpdl
;
6746 code_conversion_save (0, 0);
6748 coding
->src_object
= Fcurrent_buffer ();
6749 coding
->src_chars
= chars
;
6750 coding
->src_bytes
= bytes
;
6751 coding
->src_pos
= -chars
;
6752 coding
->src_pos_byte
= -bytes
;
6753 coding
->src_multibyte
= chars
< bytes
;
6754 coding
->dst_object
= coding
->src_object
;
6755 coding
->dst_pos
= PT
;
6756 coding
->dst_pos_byte
= PT_BYTE
;
6757 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6759 if (CODING_REQUIRE_DETECTION (coding
))
6760 detect_coding (coding
);
6762 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6763 current_buffer
->text
->inhibit_shrinking
= 1;
6764 decode_coding (coding
);
6765 current_buffer
->text
->inhibit_shrinking
= 0;
6767 attrs
= CODING_ID_ATTRS (coding
->id
);
6768 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6770 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6773 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6774 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6775 make_number (coding
->produced_char
));
6777 coding
->produced_char
+= Z
- prev_Z
;
6778 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6781 unbind_to (count
, Qnil
);
6782 return coding
->result
;
6786 encode_coding_gap (coding
, chars
, bytes
)
6787 struct coding_system
*coding
;
6788 EMACS_INT chars
, bytes
;
6790 int count
= specpdl_ptr
- specpdl
;
6792 code_conversion_save (0, 0);
6794 coding
->src_object
= Fcurrent_buffer ();
6795 coding
->src_chars
= chars
;
6796 coding
->src_bytes
= bytes
;
6797 coding
->src_pos
= -chars
;
6798 coding
->src_pos_byte
= -bytes
;
6799 coding
->src_multibyte
= chars
< bytes
;
6800 coding
->dst_object
= coding
->src_object
;
6801 coding
->dst_pos
= PT
;
6802 coding
->dst_pos_byte
= PT_BYTE
;
6804 encode_coding (coding
);
6806 unbind_to (count
, Qnil
);
6807 return coding
->result
;
6811 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6812 SRC_OBJECT into DST_OBJECT by coding context CODING.
6814 SRC_OBJECT is a buffer, a string, or Qnil.
6816 If it is a buffer, the text is at point of the buffer. FROM and TO
6817 are positions in the buffer.
6819 If it is a string, the text is at the beginning of the string.
6820 FROM and TO are indices to the string.
6822 If it is nil, the text is at coding->source. FROM and TO are
6823 indices to coding->source.
6825 DST_OBJECT is a buffer, Qt, or Qnil.
6827 If it is a buffer, the decoded text is inserted at point of the
6828 buffer. If the buffer is the same as SRC_OBJECT, the source text
6831 If it is Qt, a string is made from the decoded text, and
6832 set in CODING->dst_object.
6834 If it is Qnil, the decoded text is stored at CODING->destination.
6835 The caller must allocate CODING->dst_bytes bytes at
6836 CODING->destination by xmalloc. If the decoded text is longer than
6837 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6841 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6843 struct coding_system
*coding
;
6844 Lisp_Object src_object
;
6845 EMACS_INT from
, from_byte
, to
, to_byte
;
6846 Lisp_Object dst_object
;
6848 int count
= specpdl_ptr
- specpdl
;
6849 unsigned char *destination
;
6850 EMACS_INT dst_bytes
;
6851 EMACS_INT chars
= to
- from
;
6852 EMACS_INT bytes
= to_byte
- from_byte
;
6855 int saved_pt
= -1, saved_pt_byte
;
6857 buffer
= Fcurrent_buffer ();
6859 if (NILP (dst_object
))
6861 destination
= coding
->destination
;
6862 dst_bytes
= coding
->dst_bytes
;
6865 coding
->src_object
= src_object
;
6866 coding
->src_chars
= chars
;
6867 coding
->src_bytes
= bytes
;
6868 coding
->src_multibyte
= chars
< bytes
;
6870 if (STRINGP (src_object
))
6872 coding
->src_pos
= from
;
6873 coding
->src_pos_byte
= from_byte
;
6875 else if (BUFFERP (src_object
))
6877 set_buffer_internal (XBUFFER (src_object
));
6879 move_gap_both (from
, from_byte
);
6880 if (EQ (src_object
, dst_object
))
6882 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6883 TEMP_SET_PT_BOTH (from
, from_byte
);
6884 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6885 coding
->src_pos
= -chars
;
6886 coding
->src_pos_byte
= -bytes
;
6890 coding
->src_pos
= from
;
6891 coding
->src_pos_byte
= from_byte
;
6895 if (CODING_REQUIRE_DETECTION (coding
))
6896 detect_coding (coding
);
6897 attrs
= CODING_ID_ATTRS (coding
->id
);
6899 if (EQ (dst_object
, Qt
)
6900 || (! NILP (CODING_ATTR_POST_READ (attrs
))
6901 && NILP (dst_object
)))
6903 coding
->dst_object
= code_conversion_save (1, 1);
6904 coding
->dst_pos
= BEG
;
6905 coding
->dst_pos_byte
= BEG_BYTE
;
6906 coding
->dst_multibyte
= 1;
6908 else if (BUFFERP (dst_object
))
6910 code_conversion_save (0, 0);
6911 coding
->dst_object
= dst_object
;
6912 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6913 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6914 coding
->dst_multibyte
6915 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6919 code_conversion_save (0, 0);
6920 coding
->dst_object
= Qnil
;
6921 coding
->dst_multibyte
= 1;
6924 decode_coding (coding
);
6926 if (BUFFERP (coding
->dst_object
))
6927 set_buffer_internal (XBUFFER (coding
->dst_object
));
6929 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6931 struct gcpro gcpro1
, gcpro2
;
6932 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6935 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6936 GCPRO2 (coding
->src_object
, coding
->dst_object
);
6937 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
6938 make_number (coding
->produced_char
));
6941 coding
->produced_char
+= Z
- prev_Z
;
6942 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6945 if (EQ (dst_object
, Qt
))
6947 coding
->dst_object
= Fbuffer_string ();
6949 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
6951 set_buffer_internal (XBUFFER (coding
->dst_object
));
6952 if (dst_bytes
< coding
->produced
)
6955 = (unsigned char *) xrealloc (destination
, coding
->produced
);
6958 record_conversion_result (coding
,
6959 CODING_RESULT_INSUFFICIENT_DST
);
6960 unbind_to (count
, Qnil
);
6963 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
6964 move_gap_both (BEGV
, BEGV_BYTE
);
6965 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
6966 coding
->destination
= destination
;
6972 /* This is the case of:
6973 (BUFFERP (src_object) && EQ (src_object, dst_object))
6974 As we have moved PT while replacing the original buffer
6975 contents, we must recover it now. */
6976 set_buffer_internal (XBUFFER (src_object
));
6977 if (saved_pt
< from
)
6978 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
6979 else if (saved_pt
< from
+ chars
)
6980 TEMP_SET_PT_BOTH (from
, from_byte
);
6981 else if (! NILP (current_buffer
->enable_multibyte_characters
))
6982 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
6983 saved_pt_byte
+ (coding
->produced
- bytes
));
6985 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
6986 saved_pt_byte
+ (coding
->produced
- bytes
));
6989 unbind_to (count
, coding
->dst_object
);
6994 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6996 struct coding_system
*coding
;
6997 Lisp_Object src_object
;
6998 EMACS_INT from
, from_byte
, to
, to_byte
;
6999 Lisp_Object dst_object
;
7001 int count
= specpdl_ptr
- specpdl
;
7002 EMACS_INT chars
= to
- from
;
7003 EMACS_INT bytes
= to_byte
- from_byte
;
7006 int saved_pt
= -1, saved_pt_byte
;
7007 int kill_src_buffer
= 0;
7009 buffer
= Fcurrent_buffer ();
7011 coding
->src_object
= src_object
;
7012 coding
->src_chars
= chars
;
7013 coding
->src_bytes
= bytes
;
7014 coding
->src_multibyte
= chars
< bytes
;
7016 attrs
= CODING_ID_ATTRS (coding
->id
);
7018 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7020 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7021 set_buffer_internal (XBUFFER (coding
->src_object
));
7022 if (STRINGP (src_object
))
7023 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7024 else if (BUFFERP (src_object
))
7025 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7027 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7029 if (EQ (src_object
, dst_object
))
7031 set_buffer_internal (XBUFFER (src_object
));
7032 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7033 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7034 set_buffer_internal (XBUFFER (coding
->src_object
));
7038 Lisp_Object args
[3];
7040 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7041 args
[1] = make_number (BEG
);
7042 args
[2] = make_number (Z
);
7043 safe_call (3, args
);
7045 if (XBUFFER (coding
->src_object
) != current_buffer
)
7046 kill_src_buffer
= 1;
7047 coding
->src_object
= Fcurrent_buffer ();
7049 move_gap_both (BEG
, BEG_BYTE
);
7050 coding
->src_chars
= Z
- BEG
;
7051 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7052 coding
->src_pos
= BEG
;
7053 coding
->src_pos_byte
= BEG_BYTE
;
7054 coding
->src_multibyte
= Z
< Z_BYTE
;
7056 else if (STRINGP (src_object
))
7058 code_conversion_save (0, 0);
7059 coding
->src_pos
= from
;
7060 coding
->src_pos_byte
= from_byte
;
7062 else if (BUFFERP (src_object
))
7064 code_conversion_save (0, 0);
7065 set_buffer_internal (XBUFFER (src_object
));
7066 if (EQ (src_object
, dst_object
))
7068 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7069 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7070 coding
->src_pos
= 0;
7071 coding
->src_pos_byte
= 0;
7075 if (from
< GPT
&& to
>= GPT
)
7076 move_gap_both (from
, from_byte
);
7077 coding
->src_pos
= from
;
7078 coding
->src_pos_byte
= from_byte
;
7082 code_conversion_save (0, 0);
7084 if (BUFFERP (dst_object
))
7086 coding
->dst_object
= dst_object
;
7087 if (EQ (src_object
, dst_object
))
7089 coding
->dst_pos
= from
;
7090 coding
->dst_pos_byte
= from_byte
;
7094 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7095 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7097 coding
->dst_multibyte
7098 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7100 else if (EQ (dst_object
, Qt
))
7102 coding
->dst_object
= Qnil
;
7103 coding
->dst_bytes
= coding
->src_chars
;
7104 if (coding
->dst_bytes
== 0)
7105 coding
->dst_bytes
= 1;
7106 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7107 coding
->dst_multibyte
= 0;
7111 coding
->dst_object
= Qnil
;
7112 coding
->dst_multibyte
= 0;
7115 encode_coding (coding
);
7117 if (EQ (dst_object
, Qt
))
7119 if (BUFFERP (coding
->dst_object
))
7120 coding
->dst_object
= Fbuffer_string ();
7124 = make_unibyte_string ((char *) coding
->destination
,
7126 xfree (coding
->destination
);
7132 /* This is the case of:
7133 (BUFFERP (src_object) && EQ (src_object, dst_object))
7134 As we have moved PT while replacing the original buffer
7135 contents, we must recover it now. */
7136 set_buffer_internal (XBUFFER (src_object
));
7137 if (saved_pt
< from
)
7138 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7139 else if (saved_pt
< from
+ chars
)
7140 TEMP_SET_PT_BOTH (from
, from_byte
);
7141 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7142 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7143 saved_pt_byte
+ (coding
->produced
- bytes
));
7145 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7146 saved_pt_byte
+ (coding
->produced
- bytes
));
7149 if (kill_src_buffer
)
7150 Fkill_buffer (coding
->src_object
);
7151 unbind_to (count
, Qnil
);
7156 preferred_coding_system ()
7158 int id
= coding_categories
[coding_priorities
[0]].id
;
7160 return CODING_ID_NAME (id
);
7165 /*** 8. Emacs Lisp library functions ***/
7167 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7168 doc
: /* Return t if OBJECT is nil or a coding-system.
7169 See the documentation of `define-coding-system' for information
7170 about coding-system objects. */)
7175 || CODING_SYSTEM_ID (obj
) >= 0)
7178 || NILP (Fget (obj
, Qcoding_system_define_form
)))
7183 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7184 Sread_non_nil_coding_system
, 1, 1, 0,
7185 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7192 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7193 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7195 while (SCHARS (val
) == 0);
7196 return (Fintern (val
, Qnil
));
7199 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7200 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7201 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
7202 (prompt
, default_coding_system
)
7203 Lisp_Object prompt
, default_coding_system
;
7206 if (SYMBOLP (default_coding_system
))
7207 XSETSTRING (default_coding_system
, XPNTR (SYMBOL_NAME (default_coding_system
)));
7208 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7209 Qt
, Qnil
, Qcoding_system_history
,
7210 default_coding_system
, Qnil
);
7211 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7214 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7216 doc
: /* Check validity of CODING-SYSTEM.
7217 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7218 It is valid if it is nil or a symbol defined as a coding system by the
7219 function `define-coding-system'. */)
7221 Lisp_Object coding_system
;
7223 Lisp_Object define_form
;
7225 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7226 if (! NILP (define_form
))
7228 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7229 safe_eval (define_form
);
7231 if (!NILP (Fcoding_system_p (coding_system
)))
7232 return coding_system
;
7233 xsignal1 (Qcoding_system_error
, coding_system
);
7237 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7238 HIGHEST is nonzero, return the coding system of the highest
7239 priority among the detected coding systems. Otherwize return a
7240 list of detected coding systems sorted by their priorities. If
7241 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7242 multibyte form but contains only ASCII and eight-bit chars.
7243 Otherwise, the bytes are raw bytes.
7245 CODING-SYSTEM controls the detection as below:
7247 If it is nil, detect both text-format and eol-format. If the
7248 text-format part of CODING-SYSTEM is already specified
7249 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7250 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7251 detect only text-format. */
7254 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7256 const unsigned char *src
;
7257 int src_chars
, src_bytes
, highest
;
7259 Lisp_Object coding_system
;
7261 const unsigned char *src_end
= src
+ src_bytes
;
7262 Lisp_Object attrs
, eol_type
;
7264 struct coding_system coding
;
7266 struct coding_detection_info detect_info
;
7267 enum coding_category base_category
;
7269 if (NILP (coding_system
))
7270 coding_system
= Qundecided
;
7271 setup_coding_system (coding_system
, &coding
);
7272 attrs
= CODING_ID_ATTRS (coding
.id
);
7273 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7274 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7276 coding
.source
= src
;
7277 coding
.src_chars
= src_chars
;
7278 coding
.src_bytes
= src_bytes
;
7279 coding
.src_multibyte
= multibytep
;
7280 coding
.consumed
= 0;
7281 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7283 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7285 /* At first, detect text-format if necessary. */
7286 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7287 if (base_category
== coding_category_undecided
)
7289 enum coding_category category
;
7290 struct coding_system
*this;
7293 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7294 for (i
= 0; src
< src_end
; i
++, src
++)
7300 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7301 && ! inhibit_iso_escape_detection
)
7303 coding
.head_ascii
= src
- coding
.source
;
7304 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7306 /* We have scanned the whole data. */
7307 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7308 /* We didn't find an 8-bit code. */
7314 coding
.head_ascii
= src
- coding
.source
;
7317 || detect_info
.found
)
7320 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7321 for (i
= 0; i
< coding_category_raw_text
; i
++)
7323 category
= coding_priorities
[i
];
7324 this = coding_categories
+ category
;
7325 if (detect_info
.found
& (1 << category
))
7329 for (i
= 0; i
< coding_category_raw_text
; i
++)
7331 category
= coding_priorities
[i
];
7332 this = coding_categories
+ category
;
7336 /* No coding system of this category is defined. */
7337 detect_info
.rejected
|= (1 << category
);
7339 else if (category
>= coding_category_raw_text
)
7341 else if (detect_info
.checked
& (1 << category
))
7344 && (detect_info
.found
& (1 << category
)))
7349 if ((*(this->detector
)) (&coding
, &detect_info
)
7351 && (detect_info
.found
& (1 << category
)))
7353 if (category
== coding_category_utf_16_auto
)
7355 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7356 category
= coding_category_utf_16_le
;
7358 category
= coding_category_utf_16_be
;
7366 if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
7368 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7369 id
= coding_categories
[coding_category_raw_text
].id
;
7370 val
= Fcons (make_number (id
), Qnil
);
7372 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7374 detect_info
.found
= CATEGORY_MASK_ANY
;
7375 id
= coding_categories
[coding_category_undecided
].id
;
7376 val
= Fcons (make_number (id
), Qnil
);
7380 if (detect_info
.found
)
7382 detect_info
.found
= 1 << category
;
7383 val
= Fcons (make_number (this->id
), Qnil
);
7386 for (i
= 0; i
< coding_category_raw_text
; i
++)
7387 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7389 detect_info
.found
= 1 << coding_priorities
[i
];
7390 id
= coding_categories
[coding_priorities
[i
]].id
;
7391 val
= Fcons (make_number (id
), Qnil
);
7397 int mask
= detect_info
.rejected
| detect_info
.found
;
7401 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7403 category
= coding_priorities
[i
];
7404 if (! (mask
& (1 << category
)))
7406 found
|= 1 << category
;
7407 id
= coding_categories
[category
].id
;
7409 val
= Fcons (make_number (id
), val
);
7412 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7414 category
= coding_priorities
[i
];
7415 if (detect_info
.found
& (1 << category
))
7417 id
= coding_categories
[category
].id
;
7418 val
= Fcons (make_number (id
), val
);
7421 detect_info
.found
|= found
;
7424 else if (base_category
== coding_category_utf_16_auto
)
7426 if (detect_coding_utf_16 (&coding
, &detect_info
))
7428 struct coding_system
*this;
7430 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7431 this = coding_categories
+ coding_category_utf_16_le
;
7432 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7433 this = coding_categories
+ coding_category_utf_16_be
;
7434 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7435 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7437 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7438 val
= Fcons (make_number (this->id
), Qnil
);
7443 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7444 val
= Fcons (make_number (coding
.id
), Qnil
);
7447 /* Then, detect eol-format if necessary. */
7449 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7452 if (VECTORP (eol_type
))
7454 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7455 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7456 coding_category_raw_text
);
7457 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7458 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7459 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7460 coding_category_utf_16_be
);
7461 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7462 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7463 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7464 coding_category_utf_16_le
);
7468 if (EQ (eol_type
, Qunix
))
7469 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7470 else if (EQ (eol_type
, Qdos
))
7471 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7473 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7476 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7478 enum coding_category category
;
7481 id
= XINT (XCAR (tail
));
7482 attrs
= CODING_ID_ATTRS (id
);
7483 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7484 eol_type
= CODING_ID_EOL_TYPE (id
);
7485 if (VECTORP (eol_type
))
7487 if (category
== coding_category_utf_16_be
7488 || category
== coding_category_utf_16_be_nosig
)
7489 this_eol
= utf_16_be_eol
;
7490 else if (category
== coding_category_utf_16_le
7491 || category
== coding_category_utf_16_le_nosig
)
7492 this_eol
= utf_16_le_eol
;
7494 this_eol
= normal_eol
;
7496 if (this_eol
== EOL_SEEN_LF
)
7497 XSETCAR (tail
, AREF (eol_type
, 0));
7498 else if (this_eol
== EOL_SEEN_CRLF
)
7499 XSETCAR (tail
, AREF (eol_type
, 1));
7500 else if (this_eol
== EOL_SEEN_CR
)
7501 XSETCAR (tail
, AREF (eol_type
, 2));
7503 XSETCAR (tail
, CODING_ID_NAME (id
));
7506 XSETCAR (tail
, CODING_ID_NAME (id
));
7510 return (highest
? XCAR (val
) : val
);
7514 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7516 doc
: /* Detect coding system of the text in the region between START and END.
7517 Return a list of possible coding systems ordered by priority.
7519 If only ASCII characters are found (except for such ISO-2022 control
7520 characters ISO-2022 as ESC), it returns a list of single element
7521 `undecided' or its subsidiary coding system according to a detected
7524 If optional argument HIGHEST is non-nil, return the coding system of
7525 highest priority. */)
7526 (start
, end
, highest
)
7527 Lisp_Object start
, end
, highest
;
7530 int from_byte
, to_byte
;
7532 CHECK_NUMBER_COERCE_MARKER (start
);
7533 CHECK_NUMBER_COERCE_MARKER (end
);
7535 validate_region (&start
, &end
);
7536 from
= XINT (start
), to
= XINT (end
);
7537 from_byte
= CHAR_TO_BYTE (from
);
7538 to_byte
= CHAR_TO_BYTE (to
);
7540 if (from
< GPT
&& to
>= GPT
)
7541 move_gap_both (to
, to_byte
);
7543 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7544 to
- from
, to_byte
- from_byte
,
7546 !NILP (current_buffer
7547 ->enable_multibyte_characters
),
7551 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7553 doc
: /* Detect coding system of the text in STRING.
7554 Return a list of possible coding systems ordered by priority.
7556 If only ASCII characters are found (except for such ISO-2022 control
7557 characters ISO-2022 as ESC), it returns a list of single element
7558 `undecided' or its subsidiary coding system according to a detected
7561 If optional argument HIGHEST is non-nil, return the coding system of
7562 highest priority. */)
7564 Lisp_Object string
, highest
;
7566 CHECK_STRING (string
);
7568 return detect_coding_system (SDATA (string
),
7569 SCHARS (string
), SBYTES (string
),
7570 !NILP (highest
), STRING_MULTIBYTE (string
),
7576 char_encodable_p (c
, attrs
)
7581 struct charset
*charset
;
7582 Lisp_Object translation_table
;
7584 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7585 if (! NILP (translation_table
))
7586 c
= translate_char (translation_table
, c
);
7587 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7588 CONSP (tail
); tail
= XCDR (tail
))
7590 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7591 if (CHAR_CHARSET_P (c
, charset
))
7594 return (! NILP (tail
));
7598 /* Return a list of coding systems that safely encode the text between
7599 START and END. If EXCLUDE is non-nil, it is a list of coding
7600 systems not to check. The returned list doesn't contain any such
7601 coding systems. In any case, if the text contains only ASCII or is
7602 unibyte, return t. */
7604 DEFUN ("find-coding-systems-region-internal",
7605 Ffind_coding_systems_region_internal
,
7606 Sfind_coding_systems_region_internal
, 2, 3, 0,
7607 doc
: /* Internal use only. */)
7608 (start
, end
, exclude
)
7609 Lisp_Object start
, end
, exclude
;
7611 Lisp_Object coding_attrs_list
, safe_codings
;
7612 EMACS_INT start_byte
, end_byte
;
7613 const unsigned char *p
, *pbeg
, *pend
;
7615 Lisp_Object tail
, elt
;
7617 if (STRINGP (start
))
7619 if (!STRING_MULTIBYTE (start
)
7620 || SCHARS (start
) == SBYTES (start
))
7623 end_byte
= SBYTES (start
);
7627 CHECK_NUMBER_COERCE_MARKER (start
);
7628 CHECK_NUMBER_COERCE_MARKER (end
);
7629 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7630 args_out_of_range (start
, end
);
7631 if (NILP (current_buffer
->enable_multibyte_characters
))
7633 start_byte
= CHAR_TO_BYTE (XINT (start
));
7634 end_byte
= CHAR_TO_BYTE (XINT (end
));
7635 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7638 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7640 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7641 move_gap_both (XINT (start
), start_byte
);
7643 move_gap_both (XINT (end
), end_byte
);
7647 coding_attrs_list
= Qnil
;
7648 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7650 || NILP (Fmemq (XCAR (tail
), exclude
)))
7654 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7655 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7656 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7658 ASET (attrs
, coding_attr_trans_tbl
,
7659 get_translation_table (attrs
, 1, NULL
));
7660 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7664 if (STRINGP (start
))
7665 p
= pbeg
= SDATA (start
);
7667 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7668 pend
= p
+ (end_byte
- start_byte
);
7670 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7671 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7675 if (ASCII_BYTE_P (*p
))
7679 c
= STRING_CHAR_ADVANCE (p
);
7681 charset_map_loaded
= 0;
7682 for (tail
= coding_attrs_list
; CONSP (tail
);)
7687 else if (char_encodable_p (c
, elt
))
7689 else if (CONSP (XCDR (tail
)))
7691 XSETCAR (tail
, XCAR (XCDR (tail
)));
7692 XSETCDR (tail
, XCDR (XCDR (tail
)));
7696 XSETCAR (tail
, Qnil
);
7700 if (charset_map_loaded
)
7702 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7704 if (STRINGP (start
))
7705 pbeg
= SDATA (start
);
7707 pbeg
= BYTE_POS_ADDR (start_byte
);
7708 p
= pbeg
+ p_offset
;
7709 pend
= pbeg
+ pend_offset
;
7714 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
7715 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
7716 if (! NILP (XCAR (tail
)))
7717 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
7719 return safe_codings
;
7723 DEFUN ("unencodable-char-position", Funencodable_char_position
,
7724 Sunencodable_char_position
, 3, 5, 0,
7726 Return position of first un-encodable character in a region.
7727 START and END specfiy the region and CODING-SYSTEM specifies the
7728 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7730 If optional 4th argument COUNT is non-nil, it specifies at most how
7731 many un-encodable characters to search. In this case, the value is a
7734 If optional 5th argument STRING is non-nil, it is a string to search
7735 for un-encodable characters. In that case, START and END are indexes
7737 (start
, end
, coding_system
, count
, string
)
7738 Lisp_Object start
, end
, coding_system
, count
, string
;
7741 struct coding_system coding
;
7742 Lisp_Object attrs
, charset_list
, translation_table
;
7743 Lisp_Object positions
;
7745 const unsigned char *p
, *stop
, *pend
;
7746 int ascii_compatible
;
7748 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
7749 attrs
= CODING_ID_ATTRS (coding
.id
);
7750 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
7752 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
7753 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7754 translation_table
= get_translation_table (attrs
, 1, NULL
);
7758 validate_region (&start
, &end
);
7759 from
= XINT (start
);
7761 if (NILP (current_buffer
->enable_multibyte_characters
)
7762 || (ascii_compatible
7763 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
7765 p
= CHAR_POS_ADDR (from
);
7766 pend
= CHAR_POS_ADDR (to
);
7767 if (from
< GPT
&& to
>= GPT
)
7774 CHECK_STRING (string
);
7775 CHECK_NATNUM (start
);
7777 from
= XINT (start
);
7780 || to
> SCHARS (string
))
7781 args_out_of_range_3 (string
, start
, end
);
7782 if (! STRING_MULTIBYTE (string
))
7784 p
= SDATA (string
) + string_char_to_byte (string
, from
);
7785 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
7786 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
7794 CHECK_NATNUM (count
);
7803 if (ascii_compatible
)
7804 while (p
< stop
&& ASCII_BYTE_P (*p
))
7814 c
= STRING_CHAR_ADVANCE (p
);
7815 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
7816 && ! char_charset (translate_char (translation_table
, c
),
7817 charset_list
, NULL
))
7819 positions
= Fcons (make_number (from
), positions
);
7828 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
7832 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
7833 Scheck_coding_systems_region
, 3, 3, 0,
7834 doc
: /* Check if the region is encodable by coding systems.
7836 START and END are buffer positions specifying the region.
7837 CODING-SYSTEM-LIST is a list of coding systems to check.
7839 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7840 CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7841 whole region, POS0, POS1, ... are buffer positions where non-encodable
7842 characters are found.
7844 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7847 START may be a string. In that case, check if the string is
7848 encodable, and the value contains indices to the string instead of
7849 buffer positions. END is ignored. */)
7850 (start
, end
, coding_system_list
)
7851 Lisp_Object start
, end
, coding_system_list
;
7854 EMACS_INT start_byte
, end_byte
;
7856 const unsigned char *p
, *pbeg
, *pend
;
7858 Lisp_Object tail
, elt
, attrs
;
7860 if (STRINGP (start
))
7862 if (!STRING_MULTIBYTE (start
)
7863 && SCHARS (start
) != SBYTES (start
))
7866 end_byte
= SBYTES (start
);
7871 CHECK_NUMBER_COERCE_MARKER (start
);
7872 CHECK_NUMBER_COERCE_MARKER (end
);
7873 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7874 args_out_of_range (start
, end
);
7875 if (NILP (current_buffer
->enable_multibyte_characters
))
7877 start_byte
= CHAR_TO_BYTE (XINT (start
));
7878 end_byte
= CHAR_TO_BYTE (XINT (end
));
7879 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7882 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7884 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7885 move_gap_both (XINT (start
), start_byte
);
7887 move_gap_both (XINT (end
), end_byte
);
7893 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7896 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
7897 ASET (attrs
, coding_attr_trans_tbl
,
7898 get_translation_table (attrs
, 1, NULL
));
7899 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
7902 if (STRINGP (start
))
7903 p
= pbeg
= SDATA (start
);
7905 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7906 pend
= p
+ (end_byte
- start_byte
);
7908 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
7909 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7913 if (ASCII_BYTE_P (*p
))
7917 c
= STRING_CHAR_ADVANCE (p
);
7919 charset_map_loaded
= 0;
7920 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
7922 elt
= XCDR (XCAR (tail
));
7923 if (! char_encodable_p (c
, XCAR (elt
)))
7924 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
7926 if (charset_map_loaded
)
7928 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7930 if (STRINGP (start
))
7931 pbeg
= SDATA (start
);
7933 pbeg
= BYTE_POS_ADDR (start_byte
);
7934 p
= pbeg
+ p_offset
;
7935 pend
= pbeg
+ pend_offset
;
7943 for (; CONSP (tail
); tail
= XCDR (tail
))
7946 if (CONSP (XCDR (XCDR (elt
))))
7947 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
7956 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
7957 Lisp_Object start
, end
, coding_system
, dst_object
;
7958 int encodep
, norecord
;
7960 struct coding_system coding
;
7961 EMACS_INT from
, from_byte
, to
, to_byte
;
7962 Lisp_Object src_object
;
7964 CHECK_NUMBER_COERCE_MARKER (start
);
7965 CHECK_NUMBER_COERCE_MARKER (end
);
7966 if (NILP (coding_system
))
7967 coding_system
= Qno_conversion
;
7969 CHECK_CODING_SYSTEM (coding_system
);
7970 src_object
= Fcurrent_buffer ();
7971 if (NILP (dst_object
))
7972 dst_object
= src_object
;
7973 else if (! EQ (dst_object
, Qt
))
7974 CHECK_BUFFER (dst_object
);
7976 validate_region (&start
, &end
);
7977 from
= XFASTINT (start
);
7978 from_byte
= CHAR_TO_BYTE (from
);
7979 to
= XFASTINT (end
);
7980 to_byte
= CHAR_TO_BYTE (to
);
7982 setup_coding_system (coding_system
, &coding
);
7983 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7986 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7989 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7992 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7994 return (BUFFERP (dst_object
)
7995 ? make_number (coding
.produced_char
)
7996 : coding
.dst_object
);
8000 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8001 3, 4, "r\nzCoding system: ",
8002 doc
: /* Decode the current region from the specified coding system.
8003 When called from a program, takes four arguments:
8004 START, END, CODING-SYSTEM, and DESTINATION.
8005 START and END are buffer positions.
8007 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8008 If nil, the region between START and END is replace by the decoded text.
8009 If buffer, the decoded text is inserted in the buffer.
8010 If t, the decoded text is returned.
8012 This function sets `last-coding-system-used' to the precise coding system
8013 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8014 not fully specified.)
8015 It returns the length of the decoded text. */)
8016 (start
, end
, coding_system
, destination
)
8017 Lisp_Object start
, end
, coding_system
, destination
;
8019 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8022 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8023 3, 4, "r\nzCoding system: ",
8024 doc
: /* Encode the current region by specified coding system.
8025 When called from a program, takes three arguments:
8026 START, END, and CODING-SYSTEM. START and END are buffer positions.
8028 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8029 If nil, the region between START and END is replace by the encoded text.
8030 If buffer, the encoded text is inserted in the buffer.
8031 If t, the encoded text is returned.
8033 This function sets `last-coding-system-used' to the precise coding system
8034 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8035 not fully specified.)
8036 It returns the length of the encoded text. */)
8037 (start
, end
, coding_system
, destination
)
8038 Lisp_Object start
, end
, coding_system
, destination
;
8040 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8044 code_convert_string (string
, coding_system
, dst_object
,
8045 encodep
, nocopy
, norecord
)
8046 Lisp_Object string
, coding_system
, dst_object
;
8047 int encodep
, nocopy
, norecord
;
8049 struct coding_system coding
;
8050 EMACS_INT chars
, bytes
;
8052 CHECK_STRING (string
);
8053 if (NILP (coding_system
))
8056 Vlast_coding_system_used
= Qno_conversion
;
8057 if (NILP (dst_object
))
8058 return (nocopy
? Fcopy_sequence (string
) : string
);
8061 if (NILP (coding_system
))
8062 coding_system
= Qno_conversion
;
8064 CHECK_CODING_SYSTEM (coding_system
);
8065 if (NILP (dst_object
))
8067 else if (! EQ (dst_object
, Qt
))
8068 CHECK_BUFFER (dst_object
);
8070 setup_coding_system (coding_system
, &coding
);
8071 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8072 chars
= SCHARS (string
);
8073 bytes
= SBYTES (string
);
8075 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8077 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8079 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8081 return (BUFFERP (dst_object
)
8082 ? make_number (coding
.produced_char
)
8083 : coding
.dst_object
);
8087 /* Encode or decode STRING according to CODING_SYSTEM.
8088 Do not set Vlast_coding_system_used.
8090 This function is called only from macros DECODE_FILE and
8091 ENCODE_FILE, thus we ignore character composition. */
8094 code_convert_string_norecord (string
, coding_system
, encodep
)
8095 Lisp_Object string
, coding_system
;
8098 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8102 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8104 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8106 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8107 if the decoding operation is trivial.
8109 Optional fourth arg BUFFER non-nil meant that the decoded text is
8110 inserted in BUFFER instead of returned as a string. In this case,
8111 the return value is BUFFER.
8113 This function sets `last-coding-system-used' to the precise coding system
8114 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8115 not fully specified. */)
8116 (string
, coding_system
, nocopy
, buffer
)
8117 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8119 return code_convert_string (string
, coding_system
, buffer
,
8120 0, ! NILP (nocopy
), 0);
8123 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8125 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8127 Optional third arg NOCOPY non-nil means it is OK to return STRING
8128 itself if the encoding operation is trivial.
8130 Optional fourth arg BUFFER non-nil meant that the encoded text is
8131 inserted in BUFFER instead of returned as a string. In this case,
8132 the return value is BUFFER.
8134 This function sets `last-coding-system-used' to the precise coding system
8135 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8136 not fully specified.) */)
8137 (string
, coding_system
, nocopy
, buffer
)
8138 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8140 return code_convert_string (string
, coding_system
, buffer
,
8141 1, ! NILP (nocopy
), 1);
8145 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8146 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8147 Return the corresponding character. */)
8151 Lisp_Object spec
, attrs
, val
;
8152 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8155 CHECK_NATNUM (code
);
8156 c
= XFASTINT (code
);
8157 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8158 attrs
= AREF (spec
, 0);
8160 if (ASCII_BYTE_P (c
)
8161 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8164 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8165 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8166 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8167 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8170 charset
= charset_roman
;
8171 else if (c
>= 0xA0 && c
< 0xDF)
8173 charset
= charset_kana
;
8178 int s1
= c
>> 8, s2
= c
& 0xFF;
8180 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8181 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8182 error ("Invalid code: %d", code
);
8184 charset
= charset_kanji
;
8186 c
= DECODE_CHAR (charset
, c
);
8188 error ("Invalid code: %d", code
);
8189 return make_number (c
);
8193 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8194 doc
: /* Encode a Japanese character CH to shift_jis encoding.
8195 Return the corresponding code in SJIS. */)
8199 Lisp_Object spec
, attrs
, charset_list
;
8201 struct charset
*charset
;
8204 CHECK_CHARACTER (ch
);
8206 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8207 attrs
= AREF (spec
, 0);
8209 if (ASCII_CHAR_P (c
)
8210 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8213 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8214 charset
= char_charset (c
, charset_list
, &code
);
8215 if (code
== CHARSET_INVALID_CODE (charset
))
8216 error ("Can't encode by shift_jis encoding: %d", c
);
8219 return make_number (code
);
8222 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8223 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8224 Return the corresponding character. */)
8228 Lisp_Object spec
, attrs
, val
;
8229 struct charset
*charset_roman
, *charset_big5
, *charset
;
8232 CHECK_NATNUM (code
);
8233 c
= XFASTINT (code
);
8234 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8235 attrs
= AREF (spec
, 0);
8237 if (ASCII_BYTE_P (c
)
8238 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8241 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8242 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8243 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8246 charset
= charset_roman
;
8249 int b1
= c
>> 8, b2
= c
& 0x7F;
8250 if (b1
< 0xA1 || b1
> 0xFE
8251 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8252 error ("Invalid code: %d", code
);
8253 charset
= charset_big5
;
8255 c
= DECODE_CHAR (charset
, (unsigned )c
);
8257 error ("Invalid code: %d", code
);
8258 return make_number (c
);
8261 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8262 doc
: /* Encode the Big5 character CH to BIG5 coding system.
8263 Return the corresponding character code in Big5. */)
8267 Lisp_Object spec
, attrs
, charset_list
;
8268 struct charset
*charset
;
8272 CHECK_CHARACTER (ch
);
8274 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8275 attrs
= AREF (spec
, 0);
8276 if (ASCII_CHAR_P (c
)
8277 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8280 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8281 charset
= char_charset (c
, charset_list
, &code
);
8282 if (code
== CHARSET_INVALID_CODE (charset
))
8283 error ("Can't encode by Big5 encoding: %d", c
);
8285 return make_number (code
);
8289 DEFUN ("set-terminal-coding-system-internal",
8290 Fset_terminal_coding_system_internal
,
8291 Sset_terminal_coding_system_internal
, 1, 1, 0,
8292 doc
: /* Internal use only. */)
8294 Lisp_Object coding_system
;
8296 CHECK_SYMBOL (coding_system
);
8297 setup_coding_system (Fcheck_coding_system (coding_system
),
8300 /* We had better not send unsafe characters to terminal. */
8301 terminal_coding
.mode
|= CODING_MODE_SAFE_ENCODING
;
8302 /* Characer composition should be disabled. */
8303 terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8304 terminal_coding
.src_multibyte
= 1;
8305 terminal_coding
.dst_multibyte
= 0;
8309 DEFUN ("set-safe-terminal-coding-system-internal",
8310 Fset_safe_terminal_coding_system_internal
,
8311 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8312 doc
: /* Internal use only. */)
8314 Lisp_Object coding_system
;
8316 CHECK_SYMBOL (coding_system
);
8317 setup_coding_system (Fcheck_coding_system (coding_system
),
8318 &safe_terminal_coding
);
8319 /* Characer composition should be disabled. */
8320 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8321 safe_terminal_coding
.src_multibyte
= 1;
8322 safe_terminal_coding
.dst_multibyte
= 0;
8326 DEFUN ("terminal-coding-system",
8327 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
8328 doc
: /* Return coding system specified for terminal output. */)
8331 Lisp_Object coding_system
;
8333 coding_system
= CODING_ID_NAME (terminal_coding
.id
);
8334 /* For backward compatibility, return nil if it is `undecided'. */
8335 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
8338 DEFUN ("set-keyboard-coding-system-internal",
8339 Fset_keyboard_coding_system_internal
,
8340 Sset_keyboard_coding_system_internal
, 1, 1, 0,
8341 doc
: /* Internal use only. */)
8343 Lisp_Object coding_system
;
8345 CHECK_SYMBOL (coding_system
);
8346 setup_coding_system (Fcheck_coding_system (coding_system
),
8348 /* Characer composition should be disabled. */
8349 keyboard_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8353 DEFUN ("keyboard-coding-system",
8354 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
8355 doc
: /* Return coding system specified for decoding keyboard input. */)
8358 return CODING_ID_NAME (keyboard_coding
.id
);
8362 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8363 Sfind_operation_coding_system
, 1, MANY
, 0,
8364 doc
: /* Choose a coding system for an operation based on the target name.
8365 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8366 DECODING-SYSTEM is the coding system to use for decoding
8367 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8368 for encoding (in case OPERATION does encoding).
8370 The first argument OPERATION specifies an I/O primitive:
8371 For file I/O, `insert-file-contents' or `write-region'.
8372 For process I/O, `call-process', `call-process-region', or `start-process'.
8373 For network I/O, `open-network-stream'.
8375 The remaining arguments should be the same arguments that were passed
8376 to the primitive. Depending on which primitive, one of those arguments
8377 is selected as the TARGET. For example, if OPERATION does file I/O,
8378 whichever argument specifies the file name is TARGET.
8380 TARGET has a meaning which depends on OPERATION:
8381 For file I/O, TARGET is a file name (except for the special case below).
8382 For process I/O, TARGET is a process name.
8383 For network I/O, TARGET is a service name or a port number
8385 This function looks up what specified for TARGET in,
8386 `file-coding-system-alist', `process-coding-system-alist',
8387 or `network-coding-system-alist' depending on OPERATION.
8388 They may specify a coding system, a cons of coding systems,
8389 or a function symbol to call.
8390 In the last case, we call the function with one argument,
8391 which is a list of all the arguments given to this function.
8392 If the function can't decide a coding system, it can return
8393 `undecided' so that the normal code-detection is performed.
8395 If OPERATION is `insert-file-contents', the argument corresponding to
8396 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8397 file name to look up, and BUFFER is a buffer that contains the file's
8398 contents (not yet decoded). If `file-coding-system-alist' specifies a
8399 function to call for FILENAME, that function should examine the
8400 contents of BUFFER instead of reading the file.
8402 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
8407 Lisp_Object operation
, target_idx
, target
, val
;
8408 register Lisp_Object chain
;
8411 error ("Too few arguments");
8412 operation
= args
[0];
8413 if (!SYMBOLP (operation
)
8414 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8415 error ("Invalid first arguement");
8416 if (nargs
< 1 + XINT (target_idx
))
8417 error ("Too few arguments for operation: %s",
8418 SDATA (SYMBOL_NAME (operation
)));
8419 target
= args
[XINT (target_idx
) + 1];
8420 if (!(STRINGP (target
)
8421 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
8422 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
8423 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8424 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8426 target
= XCAR (target
);
8428 chain
= ((EQ (operation
, Qinsert_file_contents
)
8429 || EQ (operation
, Qwrite_region
))
8430 ? Vfile_coding_system_alist
8431 : (EQ (operation
, Qopen_network_stream
)
8432 ? Vnetwork_coding_system_alist
8433 : Vprocess_coding_system_alist
));
8437 for (; CONSP (chain
); chain
= XCDR (chain
))
8443 && ((STRINGP (target
)
8444 && STRINGP (XCAR (elt
))
8445 && fast_string_match (XCAR (elt
), target
) >= 0)
8446 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8449 /* Here, if VAL is both a valid coding system and a valid
8450 function symbol, we return VAL as a coding system. */
8453 if (! SYMBOLP (val
))
8455 if (! NILP (Fcoding_system_p (val
)))
8456 return Fcons (val
, val
);
8457 if (! NILP (Ffboundp (val
)))
8459 /* We use call1 rather than safe_call1
8460 so as to get bug reports about functions called here
8461 which don't handle the current interface. */
8462 val
= call1 (val
, Flist (nargs
, args
));
8465 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8466 return Fcons (val
, val
);
8474 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8475 Sset_coding_system_priority
, 0, MANY
, 0,
8476 doc
: /* Assign higher priority to the coding systems given as arguments.
8477 If multiple coding systems belongs to the same category,
8478 all but the first one are ignored.
8480 usage: (set-coding-system-priority ...) */)
8486 int changed
[coding_category_max
];
8487 enum coding_category priorities
[coding_category_max
];
8489 bzero (changed
, sizeof changed
);
8491 for (i
= j
= 0; i
< nargs
; i
++)
8493 enum coding_category category
;
8494 Lisp_Object spec
, attrs
;
8496 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8497 attrs
= AREF (spec
, 0);
8498 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8499 if (changed
[category
])
8500 /* Ignore this coding system because a coding system of the
8501 same category already had a higher priority. */
8503 changed
[category
] = 1;
8504 priorities
[j
++] = category
;
8505 if (coding_categories
[category
].id
>= 0
8506 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8507 setup_coding_system (args
[i
], &coding_categories
[category
]);
8508 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8511 /* Now we have decided top J priorities. Reflect the order of the
8512 original priorities to the remaining priorities. */
8514 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8516 while (j
< coding_category_max
8517 && changed
[coding_priorities
[j
]])
8519 if (j
== coding_category_max
)
8521 priorities
[i
] = coding_priorities
[j
];
8524 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8526 /* Update `coding-category-list'. */
8527 Vcoding_category_list
= Qnil
;
8528 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8529 Vcoding_category_list
8530 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8531 Vcoding_category_list
);
8536 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8537 Scoding_system_priority_list
, 0, 1, 0,
8538 doc
: /* Return a list of coding systems ordered by their priorities.
8539 HIGHESTP non-nil means just return the highest priority one. */)
8541 Lisp_Object highestp
;
8546 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8548 enum coding_category category
= coding_priorities
[i
];
8549 int id
= coding_categories
[category
].id
;
8554 attrs
= CODING_ID_ATTRS (id
);
8555 if (! NILP (highestp
))
8556 return CODING_ATTR_BASE_NAME (attrs
);
8557 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8559 return Fnreverse (val
);
8562 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8565 make_subsidiaries (base
)
8568 Lisp_Object subsidiaries
;
8569 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8570 char *buf
= (char *) alloca (base_name_len
+ 6);
8573 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8574 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8575 for (i
= 0; i
< 3; i
++)
8577 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8578 ASET (subsidiaries
, i
, intern (buf
));
8580 return subsidiaries
;
8584 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8585 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8586 doc
: /* For internal use only.
8587 usage: (define-coding-system-internal ...) */)
8593 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8594 Lisp_Object attrs
; /* Vector of attributes. */
8595 Lisp_Object eol_type
;
8596 Lisp_Object aliases
;
8597 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8598 enum coding_category category
;
8599 Lisp_Object tail
, val
;
8600 int max_charset_id
= 0;
8603 if (nargs
< coding_arg_max
)
8606 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8608 name
= args
[coding_arg_name
];
8609 CHECK_SYMBOL (name
);
8610 CODING_ATTR_BASE_NAME (attrs
) = name
;
8612 val
= args
[coding_arg_mnemonic
];
8613 if (! STRINGP (val
))
8614 CHECK_CHARACTER (val
);
8615 CODING_ATTR_MNEMONIC (attrs
) = val
;
8617 coding_type
= args
[coding_arg_coding_type
];
8618 CHECK_SYMBOL (coding_type
);
8619 CODING_ATTR_TYPE (attrs
) = coding_type
;
8621 charset_list
= args
[coding_arg_charset_list
];
8622 if (SYMBOLP (charset_list
))
8624 if (EQ (charset_list
, Qiso_2022
))
8626 if (! EQ (coding_type
, Qiso_2022
))
8627 error ("Invalid charset-list");
8628 charset_list
= Viso_2022_charset_list
;
8630 else if (EQ (charset_list
, Qemacs_mule
))
8632 if (! EQ (coding_type
, Qemacs_mule
))
8633 error ("Invalid charset-list");
8634 charset_list
= Vemacs_mule_charset_list
;
8636 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8637 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8638 max_charset_id
= XFASTINT (XCAR (tail
));
8642 charset_list
= Fcopy_sequence (charset_list
);
8643 for (tail
= charset_list
; !NILP (tail
); tail
= Fcdr (tail
))
8645 struct charset
*charset
;
8648 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8649 if (EQ (coding_type
, Qiso_2022
)
8650 ? CHARSET_ISO_FINAL (charset
) < 0
8651 : EQ (coding_type
, Qemacs_mule
)
8652 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8654 error ("Can't handle charset `%s'",
8655 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8657 XSETCAR (tail
, make_number (charset
->id
));
8658 if (max_charset_id
< charset
->id
)
8659 max_charset_id
= charset
->id
;
8662 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8664 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8666 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8667 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8668 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8670 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8672 val
= args
[coding_arg_decode_translation_table
];
8673 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8675 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8677 val
= args
[coding_arg_encode_translation_table
];
8678 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8680 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8682 val
= args
[coding_arg_post_read_conversion
];
8684 CODING_ATTR_POST_READ (attrs
) = val
;
8686 val
= args
[coding_arg_pre_write_conversion
];
8688 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8690 val
= args
[coding_arg_default_char
];
8692 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
8695 CHECK_CHARACTER (val
);
8696 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8699 val
= args
[coding_arg_for_unibyte
];
8700 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
8702 val
= args
[coding_arg_plist
];
8704 CODING_ATTR_PLIST (attrs
) = val
;
8706 if (EQ (coding_type
, Qcharset
))
8708 /* Generate a lisp vector of 256 elements. Each element is nil,
8709 integer, or a list of charset IDs.
8711 If Nth element is nil, the byte code N is invalid in this
8714 If Nth element is a number NUM, N is the first byte of a
8715 charset whose ID is NUM.
8717 If Nth element is a list of charset IDs, N is the first byte
8718 of one of them. The list is sorted by dimensions of the
8719 charsets. A charset of smaller dimension comes firtst. */
8720 val
= Fmake_vector (make_number (256), Qnil
);
8722 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8724 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
8725 int dim
= CHARSET_DIMENSION (charset
);
8726 int idx
= (dim
- 1) * 4;
8728 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8729 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8731 for (i
= charset
->code_space
[idx
];
8732 i
<= charset
->code_space
[idx
+ 1]; i
++)
8734 Lisp_Object tmp
, tmp2
;
8737 tmp
= AREF (val
, i
);
8740 else if (NUMBERP (tmp
))
8742 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
8744 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
8746 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
8750 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
8752 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
8757 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
8760 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
8761 XSETCAR (tmp2
, XCAR (tail
));
8767 ASET (attrs
, coding_attr_charset_valids
, val
);
8768 category
= coding_category_charset
;
8770 else if (EQ (coding_type
, Qccl
))
8774 if (nargs
< coding_arg_ccl_max
)
8777 val
= args
[coding_arg_ccl_decoder
];
8778 CHECK_CCL_PROGRAM (val
);
8780 val
= Fcopy_sequence (val
);
8781 ASET (attrs
, coding_attr_ccl_decoder
, val
);
8783 val
= args
[coding_arg_ccl_encoder
];
8784 CHECK_CCL_PROGRAM (val
);
8786 val
= Fcopy_sequence (val
);
8787 ASET (attrs
, coding_attr_ccl_encoder
, val
);
8789 val
= args
[coding_arg_ccl_valids
];
8790 valids
= Fmake_string (make_number (256), make_number (0));
8791 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
8798 from
= to
= XINT (val
);
8799 if (from
< 0 || from
> 255)
8800 args_out_of_range_3 (val
, make_number (0), make_number (255));
8805 CHECK_NATNUM_CAR (val
);
8806 CHECK_NATNUM_CDR (val
);
8807 from
= XINT (XCAR (val
));
8809 args_out_of_range_3 (XCAR (val
),
8810 make_number (0), make_number (255));
8811 to
= XINT (XCDR (val
));
8812 if (to
< from
|| to
> 255)
8813 args_out_of_range_3 (XCDR (val
),
8814 XCAR (val
), make_number (255));
8816 for (i
= from
; i
<= to
; i
++)
8817 SSET (valids
, i
, 1);
8819 ASET (attrs
, coding_attr_ccl_valids
, valids
);
8821 category
= coding_category_ccl
;
8823 else if (EQ (coding_type
, Qutf_16
))
8825 Lisp_Object bom
, endian
;
8827 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8829 if (nargs
< coding_arg_utf16_max
)
8832 bom
= args
[coding_arg_utf16_bom
];
8833 if (! NILP (bom
) && ! EQ (bom
, Qt
))
8837 CHECK_CODING_SYSTEM (val
);
8839 CHECK_CODING_SYSTEM (val
);
8841 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
8843 endian
= args
[coding_arg_utf16_endian
];
8844 CHECK_SYMBOL (endian
);
8847 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
8848 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
8849 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
8851 category
= (CONSP (bom
)
8852 ? coding_category_utf_16_auto
8854 ? (EQ (endian
, Qbig
)
8855 ? coding_category_utf_16_be_nosig
8856 : coding_category_utf_16_le_nosig
)
8857 : (EQ (endian
, Qbig
)
8858 ? coding_category_utf_16_be
8859 : coding_category_utf_16_le
));
8861 else if (EQ (coding_type
, Qiso_2022
))
8863 Lisp_Object initial
, reg_usage
, request
, flags
;
8866 if (nargs
< coding_arg_iso2022_max
)
8869 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
8870 CHECK_VECTOR (initial
);
8871 for (i
= 0; i
< 4; i
++)
8873 val
= Faref (initial
, make_number (i
));
8876 struct charset
*charset
;
8878 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8879 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
8880 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
8881 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8884 ASET (initial
, i
, make_number (-1));
8887 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
8888 CHECK_CONS (reg_usage
);
8889 CHECK_NUMBER_CAR (reg_usage
);
8890 CHECK_NUMBER_CDR (reg_usage
);
8892 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
8893 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
8901 CHECK_CHARSET_GET_ID (tmp
, id
);
8902 CHECK_NATNUM_CDR (val
);
8903 if (XINT (XCDR (val
)) >= 4)
8904 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
8905 XSETCAR (val
, make_number (id
));
8908 flags
= args
[coding_arg_iso2022_flags
];
8909 CHECK_NATNUM (flags
);
8911 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
8912 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
8914 ASET (attrs
, coding_attr_iso_initial
, initial
);
8915 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
8916 ASET (attrs
, coding_attr_iso_request
, request
);
8917 ASET (attrs
, coding_attr_iso_flags
, flags
);
8918 setup_iso_safe_charsets (attrs
);
8920 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
8921 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
8922 | CODING_ISO_FLAG_SINGLE_SHIFT
))
8923 ? coding_category_iso_7_else
8924 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8925 ? coding_category_iso_7
8926 : coding_category_iso_7_tight
);
8929 int id
= XINT (AREF (initial
, 1));
8931 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
8932 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8934 ? coding_category_iso_8_else
8935 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
8936 ? coding_category_iso_8_1
8937 : coding_category_iso_8_2
);
8939 if (category
!= coding_category_iso_8_1
8940 && category
!= coding_category_iso_8_2
)
8941 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8943 else if (EQ (coding_type
, Qemacs_mule
))
8945 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
8946 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
8947 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8948 category
= coding_category_emacs_mule
;
8950 else if (EQ (coding_type
, Qshift_jis
))
8953 struct charset
*charset
;
8955 if (XINT (Flength (charset_list
)) != 3
8956 && XINT (Flength (charset_list
)) != 4)
8957 error ("There should be three or four charsets");
8959 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8960 if (CHARSET_DIMENSION (charset
) != 1)
8961 error ("Dimension of charset %s is not one",
8962 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8963 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8964 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8966 charset_list
= XCDR (charset_list
);
8967 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8968 if (CHARSET_DIMENSION (charset
) != 1)
8969 error ("Dimension of charset %s is not one",
8970 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8972 charset_list
= XCDR (charset_list
);
8973 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8974 if (CHARSET_DIMENSION (charset
) != 2)
8975 error ("Dimension of charset %s is not two",
8976 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8978 charset_list
= XCDR (charset_list
);
8979 if (! NILP (charset_list
))
8981 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8982 if (CHARSET_DIMENSION (charset
) != 2)
8983 error ("Dimension of charset %s is not two",
8984 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8987 category
= coding_category_sjis
;
8988 Vsjis_coding_system
= name
;
8990 else if (EQ (coding_type
, Qbig5
))
8992 struct charset
*charset
;
8994 if (XINT (Flength (charset_list
)) != 2)
8995 error ("There should be just two charsets");
8997 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8998 if (CHARSET_DIMENSION (charset
) != 1)
8999 error ("Dimension of charset %s is not one",
9000 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9001 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9002 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9004 charset_list
= XCDR (charset_list
);
9005 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9006 if (CHARSET_DIMENSION (charset
) != 2)
9007 error ("Dimension of charset %s is not two",
9008 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9010 category
= coding_category_big5
;
9011 Vbig5_coding_system
= name
;
9013 else if (EQ (coding_type
, Qraw_text
))
9015 category
= coding_category_raw_text
;
9016 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9018 else if (EQ (coding_type
, Qutf_8
))
9020 category
= coding_category_utf_8
;
9021 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9023 else if (EQ (coding_type
, Qundecided
))
9024 category
= coding_category_undecided
;
9026 error ("Invalid coding system type: %s",
9027 SDATA (SYMBOL_NAME (coding_type
)));
9029 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9030 CODING_ATTR_PLIST (attrs
)
9031 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9032 CODING_ATTR_PLIST (attrs
)));
9033 CODING_ATTR_PLIST (attrs
)
9034 = Fcons (QCascii_compatible_p
,
9035 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9036 CODING_ATTR_PLIST (attrs
)));
9038 eol_type
= args
[coding_arg_eol_type
];
9039 if (! NILP (eol_type
)
9040 && ! EQ (eol_type
, Qunix
)
9041 && ! EQ (eol_type
, Qdos
)
9042 && ! EQ (eol_type
, Qmac
))
9043 error ("Invalid eol-type");
9045 aliases
= Fcons (name
, Qnil
);
9047 if (NILP (eol_type
))
9049 eol_type
= make_subsidiaries (name
);
9050 for (i
= 0; i
< 3; i
++)
9052 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9054 this_name
= AREF (eol_type
, i
);
9055 this_aliases
= Fcons (this_name
, Qnil
);
9056 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9057 this_spec
= Fmake_vector (make_number (3), attrs
);
9058 ASET (this_spec
, 1, this_aliases
);
9059 ASET (this_spec
, 2, this_eol_type
);
9060 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9061 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9062 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9064 Vcoding_system_alist
9065 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9066 Vcoding_system_alist
);
9070 spec_vec
= Fmake_vector (make_number (3), attrs
);
9071 ASET (spec_vec
, 1, aliases
);
9072 ASET (spec_vec
, 2, eol_type
);
9074 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9075 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9076 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9078 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9079 Vcoding_system_alist
);
9082 int id
= coding_categories
[category
].id
;
9084 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9085 setup_coding_system (name
, &coding_categories
[category
]);
9091 return Fsignal (Qwrong_number_of_arguments
,
9092 Fcons (intern ("define-coding-system-internal"),
9093 make_number (nargs
)));
9097 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
9099 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9100 (coding_system
, prop
, val
)
9101 Lisp_Object coding_system
, prop
, val
;
9103 Lisp_Object spec
, attrs
;
9105 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9106 attrs
= AREF (spec
, 0);
9107 if (EQ (prop
, QCmnemonic
))
9109 if (! STRINGP (val
))
9110 CHECK_CHARACTER (val
);
9111 CODING_ATTR_MNEMONIC (attrs
) = val
;
9113 else if (EQ (prop
, QCdefalut_char
))
9116 val
= make_number (' ');
9118 CHECK_CHARACTER (val
);
9119 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9121 else if (EQ (prop
, QCdecode_translation_table
))
9123 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9125 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9127 else if (EQ (prop
, QCencode_translation_table
))
9129 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9131 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9133 else if (EQ (prop
, QCpost_read_conversion
))
9136 CODING_ATTR_POST_READ (attrs
) = val
;
9138 else if (EQ (prop
, QCpre_write_conversion
))
9141 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9143 else if (EQ (prop
, QCascii_compatible_p
))
9145 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9148 CODING_ATTR_PLIST (attrs
)
9149 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9154 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9155 Sdefine_coding_system_alias
, 2, 2, 0,
9156 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9157 (alias
, coding_system
)
9158 Lisp_Object alias
, coding_system
;
9160 Lisp_Object spec
, aliases
, eol_type
, val
;
9162 CHECK_SYMBOL (alias
);
9163 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9164 aliases
= AREF (spec
, 1);
9165 /* ALISES should be a list of length more than zero, and the first
9166 element is a base coding system. Append ALIAS at the tail of the
9168 while (!NILP (XCDR (aliases
)))
9169 aliases
= XCDR (aliases
);
9170 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9172 eol_type
= AREF (spec
, 2);
9173 if (VECTORP (eol_type
))
9175 Lisp_Object subsidiaries
;
9178 subsidiaries
= make_subsidiaries (alias
);
9179 for (i
= 0; i
< 3; i
++)
9180 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9181 AREF (eol_type
, i
));
9184 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9185 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9186 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
9188 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9189 Vcoding_system_alist
);
9194 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9196 doc
: /* Return the base of CODING-SYSTEM.
9197 Any alias or subsidiary coding system is not a base coding system. */)
9199 Lisp_Object coding_system
;
9201 Lisp_Object spec
, attrs
;
9203 if (NILP (coding_system
))
9204 return (Qno_conversion
);
9205 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9206 attrs
= AREF (spec
, 0);
9207 return CODING_ATTR_BASE_NAME (attrs
);
9210 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9212 doc
: "Return the property list of CODING-SYSTEM.")
9214 Lisp_Object coding_system
;
9216 Lisp_Object spec
, attrs
;
9218 if (NILP (coding_system
))
9219 coding_system
= Qno_conversion
;
9220 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9221 attrs
= AREF (spec
, 0);
9222 return CODING_ATTR_PLIST (attrs
);
9226 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9228 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9230 Lisp_Object coding_system
;
9234 if (NILP (coding_system
))
9235 coding_system
= Qno_conversion
;
9236 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9237 return AREF (spec
, 1);
9240 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9241 Scoding_system_eol_type
, 1, 1, 0,
9242 doc
: /* Return eol-type of CODING-SYSTEM.
9243 An eol-type is integer 0, 1, 2, or a vector of coding systems.
9245 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9246 and CR respectively.
9248 A vector value indicates that a format of end-of-line should be
9249 detected automatically. Nth element of the vector is the subsidiary
9250 coding system whose eol-type is N. */)
9252 Lisp_Object coding_system
;
9254 Lisp_Object spec
, eol_type
;
9257 if (NILP (coding_system
))
9258 coding_system
= Qno_conversion
;
9259 if (! CODING_SYSTEM_P (coding_system
))
9261 spec
= CODING_SYSTEM_SPEC (coding_system
);
9262 eol_type
= AREF (spec
, 2);
9263 if (VECTORP (eol_type
))
9264 return Fcopy_sequence (eol_type
);
9265 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9266 return make_number (n
);
9272 /*** 9. Post-amble ***/
9279 for (i
= 0; i
< coding_category_max
; i
++)
9281 coding_categories
[i
].id
= -1;
9282 coding_priorities
[i
] = i
;
9285 /* ISO2022 specific initialize routine. */
9286 for (i
= 0; i
< 0x20; i
++)
9287 iso_code_class
[i
] = ISO_control_0
;
9288 for (i
= 0x21; i
< 0x7F; i
++)
9289 iso_code_class
[i
] = ISO_graphic_plane_0
;
9290 for (i
= 0x80; i
< 0xA0; i
++)
9291 iso_code_class
[i
] = ISO_control_1
;
9292 for (i
= 0xA1; i
< 0xFF; i
++)
9293 iso_code_class
[i
] = ISO_graphic_plane_1
;
9294 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9295 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9296 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9297 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9298 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9299 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9300 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9301 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9302 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9304 for (i
= 0; i
< 256; i
++)
9306 emacs_mule_bytes
[i
] = 1;
9308 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9309 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9310 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9311 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9319 staticpro (&Vcoding_system_hash_table
);
9321 Lisp_Object args
[2];
9324 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9327 staticpro (&Vsjis_coding_system
);
9328 Vsjis_coding_system
= Qnil
;
9330 staticpro (&Vbig5_coding_system
);
9331 Vbig5_coding_system
= Qnil
;
9333 staticpro (&Vcode_conversion_reused_workbuf
);
9334 Vcode_conversion_reused_workbuf
= Qnil
;
9336 staticpro (&Vcode_conversion_workbuf_name
);
9337 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9339 reused_workbuf_in_use
= 0;
9341 DEFSYM (Qcharset
, "charset");
9342 DEFSYM (Qtarget_idx
, "target-idx");
9343 DEFSYM (Qcoding_system_history
, "coding-system-history");
9344 Fset (Qcoding_system_history
, Qnil
);
9346 /* Target FILENAME is the first argument. */
9347 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9348 /* Target FILENAME is the third argument. */
9349 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9351 DEFSYM (Qcall_process
, "call-process");
9352 /* Target PROGRAM is the first argument. */
9353 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9355 DEFSYM (Qcall_process_region
, "call-process-region");
9356 /* Target PROGRAM is the third argument. */
9357 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9359 DEFSYM (Qstart_process
, "start-process");
9360 /* Target PROGRAM is the third argument. */
9361 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9363 DEFSYM (Qopen_network_stream
, "open-network-stream");
9364 /* Target SERVICE is the fourth argument. */
9365 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9367 DEFSYM (Qcoding_system
, "coding-system");
9368 DEFSYM (Qcoding_aliases
, "coding-aliases");
9370 DEFSYM (Qeol_type
, "eol-type");
9371 DEFSYM (Qunix
, "unix");
9372 DEFSYM (Qdos
, "dos");
9374 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9375 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9376 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9377 DEFSYM (Qdefault_char
, "default-char");
9378 DEFSYM (Qundecided
, "undecided");
9379 DEFSYM (Qno_conversion
, "no-conversion");
9380 DEFSYM (Qraw_text
, "raw-text");
9382 DEFSYM (Qiso_2022
, "iso-2022");
9384 DEFSYM (Qutf_8
, "utf-8");
9385 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9387 DEFSYM (Qutf_16
, "utf-16");
9388 DEFSYM (Qbig
, "big");
9389 DEFSYM (Qlittle
, "little");
9391 DEFSYM (Qshift_jis
, "shift-jis");
9392 DEFSYM (Qbig5
, "big5");
9394 DEFSYM (Qcoding_system_p
, "coding-system-p");
9396 DEFSYM (Qcoding_system_error
, "coding-system-error");
9397 Fput (Qcoding_system_error
, Qerror_conditions
,
9398 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9399 Fput (Qcoding_system_error
, Qerror_message
,
9400 build_string ("Invalid coding system"));
9402 /* Intern this now in case it isn't already done.
9403 Setting this variable twice is harmless.
9404 But don't staticpro it here--that is done in alloc.c. */
9405 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9407 DEFSYM (Qtranslation_table
, "translation-table");
9408 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9409 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9410 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9411 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9413 DEFSYM (Qvalid_codes
, "valid-codes");
9415 DEFSYM (Qemacs_mule
, "emacs-mule");
9417 DEFSYM (QCcategory
, ":category");
9418 DEFSYM (QCmnemonic
, ":mnemonic");
9419 DEFSYM (QCdefalut_char
, ":default-char");
9420 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9421 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9422 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9423 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9424 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
9426 Vcoding_category_table
9427 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9428 staticpro (&Vcoding_category_table
);
9429 /* Followings are target of code detection. */
9430 ASET (Vcoding_category_table
, coding_category_iso_7
,
9431 intern ("coding-category-iso-7"));
9432 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9433 intern ("coding-category-iso-7-tight"));
9434 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9435 intern ("coding-category-iso-8-1"));
9436 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9437 intern ("coding-category-iso-8-2"));
9438 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9439 intern ("coding-category-iso-7-else"));
9440 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9441 intern ("coding-category-iso-8-else"));
9442 ASET (Vcoding_category_table
, coding_category_utf_8
,
9443 intern ("coding-category-utf-8"));
9444 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9445 intern ("coding-category-utf-16-be"));
9446 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9447 intern ("coding-category-utf-16-auto"));
9448 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9449 intern ("coding-category-utf-16-le"));
9450 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9451 intern ("coding-category-utf-16-be-nosig"));
9452 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9453 intern ("coding-category-utf-16-le-nosig"));
9454 ASET (Vcoding_category_table
, coding_category_charset
,
9455 intern ("coding-category-charset"));
9456 ASET (Vcoding_category_table
, coding_category_sjis
,
9457 intern ("coding-category-sjis"));
9458 ASET (Vcoding_category_table
, coding_category_big5
,
9459 intern ("coding-category-big5"));
9460 ASET (Vcoding_category_table
, coding_category_ccl
,
9461 intern ("coding-category-ccl"));
9462 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9463 intern ("coding-category-emacs-mule"));
9464 /* Followings are NOT target of code detection. */
9465 ASET (Vcoding_category_table
, coding_category_raw_text
,
9466 intern ("coding-category-raw-text"));
9467 ASET (Vcoding_category_table
, coding_category_undecided
,
9468 intern ("coding-category-undecided"));
9470 DEFSYM (Qinsufficient_source
, "insufficient-source");
9471 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9472 DEFSYM (Qinvalid_source
, "invalid-source");
9473 DEFSYM (Qinterrupted
, "interrupted");
9474 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9475 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
9477 defsubr (&Scoding_system_p
);
9478 defsubr (&Sread_coding_system
);
9479 defsubr (&Sread_non_nil_coding_system
);
9480 defsubr (&Scheck_coding_system
);
9481 defsubr (&Sdetect_coding_region
);
9482 defsubr (&Sdetect_coding_string
);
9483 defsubr (&Sfind_coding_systems_region_internal
);
9484 defsubr (&Sunencodable_char_position
);
9485 defsubr (&Scheck_coding_systems_region
);
9486 defsubr (&Sdecode_coding_region
);
9487 defsubr (&Sencode_coding_region
);
9488 defsubr (&Sdecode_coding_string
);
9489 defsubr (&Sencode_coding_string
);
9490 defsubr (&Sdecode_sjis_char
);
9491 defsubr (&Sencode_sjis_char
);
9492 defsubr (&Sdecode_big5_char
);
9493 defsubr (&Sencode_big5_char
);
9494 defsubr (&Sset_terminal_coding_system_internal
);
9495 defsubr (&Sset_safe_terminal_coding_system_internal
);
9496 defsubr (&Sterminal_coding_system
);
9497 defsubr (&Sset_keyboard_coding_system_internal
);
9498 defsubr (&Skeyboard_coding_system
);
9499 defsubr (&Sfind_operation_coding_system
);
9500 defsubr (&Sset_coding_system_priority
);
9501 defsubr (&Sdefine_coding_system_internal
);
9502 defsubr (&Sdefine_coding_system_alias
);
9503 defsubr (&Scoding_system_put
);
9504 defsubr (&Scoding_system_base
);
9505 defsubr (&Scoding_system_plist
);
9506 defsubr (&Scoding_system_aliases
);
9507 defsubr (&Scoding_system_eol_type
);
9508 defsubr (&Scoding_system_priority_list
);
9510 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9511 doc
: /* List of coding systems.
9513 Do not alter the value of this variable manually. This variable should be
9514 updated by the functions `define-coding-system' and
9515 `define-coding-system-alias'. */);
9516 Vcoding_system_list
= Qnil
;
9518 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9519 doc
: /* Alist of coding system names.
9520 Each element is one element list of coding system name.
9521 This variable is given to `completing-read' as TABLE argument.
9523 Do not alter the value of this variable manually. This variable should be
9524 updated by the functions `make-coding-system' and
9525 `define-coding-system-alias'. */);
9526 Vcoding_system_alist
= Qnil
;
9528 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9529 doc
: /* List of coding-categories (symbols) ordered by priority.
9531 On detecting a coding system, Emacs tries code detection algorithms
9532 associated with each coding-category one by one in this order. When
9533 one algorithm agrees with a byte sequence of source text, the coding
9534 system bound to the corresponding coding-category is selected.
9536 Don't modify this variable directly, but use `set-coding-priority'. */);
9540 Vcoding_category_list
= Qnil
;
9541 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9542 Vcoding_category_list
9543 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9544 Vcoding_category_list
);
9547 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9548 doc
: /* Specify the coding system for read operations.
9549 It is useful to bind this variable with `let', but do not set it globally.
9550 If the value is a coding system, it is used for decoding on read operation.
9551 If not, an appropriate element is used from one of the coding system alists:
9552 There are three such tables, `file-coding-system-alist',
9553 `process-coding-system-alist', and `network-coding-system-alist'. */);
9554 Vcoding_system_for_read
= Qnil
;
9556 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9557 doc
: /* Specify the coding system for write operations.
9558 Programs bind this variable with `let', but you should not set it globally.
9559 If the value is a coding system, it is used for encoding of output,
9560 when writing it to a file and when sending it to a file or subprocess.
9562 If this does not specify a coding system, an appropriate element
9563 is used from one of the coding system alists:
9564 There are three such tables, `file-coding-system-alist',
9565 `process-coding-system-alist', and `network-coding-system-alist'.
9566 For output to files, if the above procedure does not specify a coding system,
9567 the value of `buffer-file-coding-system' is used. */);
9568 Vcoding_system_for_write
= Qnil
;
9570 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9572 Coding system used in the latest file or process I/O. */);
9573 Vlast_coding_system_used
= Qnil
;
9575 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9577 Error status of the last code conversion.
9579 When an error was detected in the last code conversion, this variable
9580 is set to one of the following symbols.
9581 `insufficient-source'
9585 `insufficient-memory'
9586 When no error was detected, the value doesn't change. So, to check
9587 the error status of a code conversion by this variable, you must
9588 explicitly set this variable to nil before performing code
9590 Vlast_code_conversion_error
= Qnil
;
9592 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9594 *Non-nil means always inhibit code conversion of end-of-line format.
9595 See info node `Coding Systems' and info node `Text and Binary' concerning
9596 such conversion. */);
9597 inhibit_eol_conversion
= 0;
9599 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9601 Non-nil means process buffer inherits coding system of process output.
9602 Bind it to t if the process output is to be treated as if it were a file
9603 read from some filesystem. */);
9604 inherit_process_coding_system
= 0;
9606 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9608 Alist to decide a coding system to use for a file I/O operation.
9609 The format is ((PATTERN . VAL) ...),
9610 where PATTERN is a regular expression matching a file name,
9611 VAL is a coding system, a cons of coding systems, or a function symbol.
9612 If VAL is a coding system, it is used for both decoding and encoding
9614 If VAL is a cons of coding systems, the car part is used for decoding,
9615 and the cdr part is used for encoding.
9616 If VAL is a function symbol, the function must return a coding system
9617 or a cons of coding systems which are used as above. The function is
9618 called with an argument that is a list of the arguments with which
9619 `find-operation-coding-system' was called. If the function can't decide
9620 a coding system, it can return `undecided' so that the normal
9621 code-detection is performed.
9623 See also the function `find-operation-coding-system'
9624 and the variable `auto-coding-alist'. */);
9625 Vfile_coding_system_alist
= Qnil
;
9627 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9629 Alist to decide a coding system to use for a process I/O operation.
9630 The format is ((PATTERN . VAL) ...),
9631 where PATTERN is a regular expression matching a program name,
9632 VAL is a coding system, a cons of coding systems, or a function symbol.
9633 If VAL is a coding system, it is used for both decoding what received
9634 from the program and encoding what sent to the program.
9635 If VAL is a cons of coding systems, the car part is used for decoding,
9636 and the cdr part is used for encoding.
9637 If VAL is a function symbol, the function must return a coding system
9638 or a cons of coding systems which are used as above.
9640 See also the function `find-operation-coding-system'. */);
9641 Vprocess_coding_system_alist
= Qnil
;
9643 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9645 Alist to decide a coding system to use for a network I/O operation.
9646 The format is ((PATTERN . VAL) ...),
9647 where PATTERN is a regular expression matching a network service name
9648 or is a port number to connect to,
9649 VAL is a coding system, a cons of coding systems, or a function symbol.
9650 If VAL is a coding system, it is used for both decoding what received
9651 from the network stream and encoding what sent to the network stream.
9652 If VAL is a cons of coding systems, the car part is used for decoding,
9653 and the cdr part is used for encoding.
9654 If VAL is a function symbol, the function must return a coding system
9655 or a cons of coding systems which are used as above.
9657 See also the function `find-operation-coding-system'. */);
9658 Vnetwork_coding_system_alist
= Qnil
;
9660 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9661 doc
: /* Coding system to use with system messages.
9662 Also used for decoding keyboard input on X Window system. */);
9663 Vlocale_coding_system
= Qnil
;
9665 /* The eol mnemonics are reset in startup.el system-dependently. */
9666 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9668 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9669 eol_mnemonic_unix
= build_string (":");
9671 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9673 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9674 eol_mnemonic_dos
= build_string ("\\");
9676 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9678 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9679 eol_mnemonic_mac
= build_string ("/");
9681 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9683 *String displayed in mode line when end-of-line format is not yet determined. */);
9684 eol_mnemonic_undecided
= build_string (":");
9686 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
9688 *Non-nil enables character translation while encoding and decoding. */);
9689 Venable_character_translation
= Qt
;
9691 DEFVAR_LISP ("standard-translation-table-for-decode",
9692 &Vstandard_translation_table_for_decode
,
9693 doc
: /* Table for translating characters while decoding. */);
9694 Vstandard_translation_table_for_decode
= Qnil
;
9696 DEFVAR_LISP ("standard-translation-table-for-encode",
9697 &Vstandard_translation_table_for_encode
,
9698 doc
: /* Table for translating characters while encoding. */);
9699 Vstandard_translation_table_for_encode
= Qnil
;
9701 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
9702 doc
: /* Alist of charsets vs revision numbers.
9703 While encoding, if a charset (car part of an element) is found,
9704 designate it with the escape sequence identifying revision (cdr part
9705 of the element). */);
9706 Vcharset_revision_table
= Qnil
;
9708 DEFVAR_LISP ("default-process-coding-system",
9709 &Vdefault_process_coding_system
,
9710 doc
: /* Cons of coding systems used for process I/O by default.
9711 The car part is used for decoding a process output,
9712 the cdr part is used for encoding a text to be sent to a process. */);
9713 Vdefault_process_coding_system
= Qnil
;
9715 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
9717 Table of extra Latin codes in the range 128..159 (inclusive).
9718 This is a vector of length 256.
9719 If Nth element is non-nil, the existence of code N in a file
9720 \(or output of subprocess) doesn't prevent it to be detected as
9721 a coding system of ISO 2022 variant which has a flag
9722 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9723 or reading output of a subprocess.
9724 Only 128th through 159th elements has a meaning. */);
9725 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
9727 DEFVAR_LISP ("select-safe-coding-system-function",
9728 &Vselect_safe_coding_system_function
,
9730 Function to call to select safe coding system for encoding a text.
9732 If set, this function is called to force a user to select a proper
9733 coding system which can encode the text in the case that a default
9734 coding system used in each operation can't encode the text.
9736 The default value is `select-safe-coding-system' (which see). */);
9737 Vselect_safe_coding_system_function
= Qnil
;
9739 DEFVAR_BOOL ("coding-system-require-warning",
9740 &coding_system_require_warning
,
9741 doc
: /* Internal use only.
9742 If non-nil, on writing a file, `select-safe-coding-system-function' is
9743 called even if `coding-system-for-write' is non-nil. The command
9744 `universal-coding-system-argument' binds this variable to t temporarily. */);
9745 coding_system_require_warning
= 0;
9748 DEFVAR_BOOL ("inhibit-iso-escape-detection",
9749 &inhibit_iso_escape_detection
,
9751 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
9753 By default, on reading a file, Emacs tries to detect how the text is
9754 encoded. This code detection is sensitive to escape sequences. If
9755 the sequence is valid as ISO2022, the code is determined as one of
9756 the ISO2022 encodings, and the file is decoded by the corresponding
9757 coding system (e.g. `iso-2022-7bit').
9759 However, there may be a case that you want to read escape sequences in
9760 a file as is. In such a case, you can set this variable to non-nil.
9761 Then, as the code detection ignores any escape sequences, no file is
9762 detected as encoded in some ISO2022 encoding. The result is that all
9763 escape sequences become visible in a buffer.
9765 The default value is nil, and it is strongly recommended not to change
9766 it. That is because many Emacs Lisp source files that contain
9767 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9768 in Emacs's distribution, and they won't be decoded correctly on
9769 reading if you suppress escape sequence detection.
9771 The other way to read escape sequences in a file without decoding is
9772 to explicitly specify some coding system that doesn't use ISO2022's
9773 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
9774 inhibit_iso_escape_detection
= 0;
9776 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
9777 doc
: /* Char table for translating self-inserting characters.
9778 This is applied to the result of input methods, not their input. See also
9779 `keyboard-translate-table'. */);
9780 Vtranslation_table_for_input
= Qnil
;
9783 Lisp_Object args
[coding_arg_max
];
9784 Lisp_Object plist
[16];
9787 for (i
= 0; i
< coding_arg_max
; i
++)
9790 plist
[0] = intern (":name");
9791 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
9792 plist
[2] = intern (":mnemonic");
9793 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
9794 plist
[4] = intern (":coding-type");
9795 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
9796 plist
[6] = intern (":ascii-compatible-p");
9797 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
9798 plist
[8] = intern (":default-char");
9799 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
9800 plist
[10] = intern (":for-unibyte");
9801 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
9802 plist
[12] = intern (":docstring");
9803 plist
[13] = build_string ("Do no conversion.\n\
9805 When you visit a file with this coding, the file is read into a\n\
9806 unibyte buffer as is, thus each byte of a file is treated as a\n\
9808 plist
[14] = intern (":eol-type");
9809 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
9810 args
[coding_arg_plist
] = Flist (16, plist
);
9811 Fdefine_coding_system_internal (coding_arg_max
, args
);
9813 plist
[1] = args
[coding_arg_name
] = Qundecided
;
9814 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
9815 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
9816 /* This is already set.
9817 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
9818 plist
[8] = intern (":charset-list");
9819 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
9820 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
9821 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9822 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
9823 args
[coding_arg_plist
] = Flist (16, plist
);
9824 Fdefine_coding_system_internal (coding_arg_max
, args
);
9827 setup_coding_system (Qno_conversion
, &keyboard_coding
);
9828 setup_coding_system (Qundecided
, &terminal_coding
);
9829 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
9834 for (i
= 0; i
< coding_category_max
; i
++)
9835 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
9837 #if defined (MSDOS) || defined (WINDOWSNT)
9838 system_eol_type
= Qdos
;
9840 system_eol_type
= Qunix
;
9842 staticpro (&system_eol_type
);
9846 emacs_strerror (error_number
)
9851 synchronize_system_messages_locale ();
9852 str
= strerror (error_number
);
9854 if (! NILP (Vlocale_coding_system
))
9856 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
9857 Vlocale_coding_system
,
9859 str
= (char *) SDATA (dec
);
9867 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
9868 (do not change this comment) */