1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008, 2009, 2010
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
12 This file is part of GNU Emacs.
14 GNU Emacs is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 /*** TABLE OF CONTENTS ***
31 2. Emacs' internal format (emacs-utf-8) handlers
34 5. Charset-base coding systems handlers
35 6. emacs-mule (old Emacs' internal format) handlers
37 8. Shift-JIS and BIG5 handlers
39 10. C library functions
40 11. Emacs Lisp library functions
45 /*** 0. General comments ***
50 A coding system is an object for an encoding mechanism that contains
51 information about how to convert byte sequences to character
52 sequences and vice versa. When we say "decode", it means converting
53 a byte sequence of a specific coding system into a character
54 sequence that is represented by Emacs' internal coding system
55 `emacs-utf-8', and when we say "encode", it means converting a
56 character sequence of emacs-utf-8 to a byte sequence of a specific
59 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
60 C level, a coding system is represented by a vector of attributes
61 stored in the hash table Vcharset_hash_table. The conversion from
62 coding system symbol to attributes vector is done by looking up
63 Vcharset_hash_table by the symbol.
65 Coding systems are classified into the following types depending on
66 the encoding mechanism. Here's a brief description of the types.
72 o Charset-base coding system
74 A coding system defined by one or more (coded) character sets.
75 Decoding and encoding are done by a code converter defined for each
78 o Old Emacs internal format (emacs-mule)
80 The coding system adopted by old versions of Emacs (20 and 21).
82 o ISO2022-base coding system
84 The most famous coding system for multiple character sets. X's
85 Compound Text, various EUCs (Extended Unix Code), and coding systems
86 used in the Internet communication such as ISO-2022-JP are all
89 o SJIS (or Shift-JIS or MS-Kanji-Code)
91 A coding system to encode character sets: ASCII, JISX0201, and
92 JISX0208. Widely used for PC's in Japan. Details are described in
97 A coding system to encode character sets: ASCII and Big5. Widely
98 used for Chinese (mainly in Taiwan and Hong Kong). Details are
99 described in section 8. In this file, when we write "big5" (all
100 lowercase), we mean the coding system, and when we write "Big5"
101 (capitalized), we mean the character set.
105 If a user wants to decode/encode text encoded in a coding system
106 not listed above, he can supply a decoder and an encoder for it in
107 CCL (Code Conversion Language) programs. Emacs executes the CCL
108 program while decoding/encoding.
112 A coding system for text containing raw eight-bit data. Emacs
113 treats each byte of source text as a character (except for
114 end-of-line conversion).
118 Like raw text, but don't do end-of-line conversion.
123 How text end-of-line is encoded depends on operating system. For
124 instance, Unix's format is just one byte of LF (line-feed) code,
125 whereas DOS's format is two-byte sequence of `carriage-return' and
126 `line-feed' codes. MacOS's format is usually one byte of
129 Since text character encoding and end-of-line encoding are
130 independent, any coding system described above can take any format
131 of end-of-line (except for no-conversion).
135 Before using a coding system for code conversion (i.e. decoding and
136 encoding), we setup a structure of type `struct coding_system'.
137 This structure keeps various information about a specific code
138 conversion (e.g. the location of source and destination data).
145 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
147 These functions check if a byte sequence specified as a source in
148 CODING conforms to the format of XXX, and update the members of
151 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
153 Below is the template of these functions. */
157 detect_coding_XXX (coding
, detect_info
)
158 struct coding_system
*coding
;
159 struct coding_detection_info
*detect_info
;
161 const unsigned char *src
= coding
->source
;
162 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
163 int multibytep
= coding
->src_multibyte
;
164 int consumed_chars
= 0;
170 /* Get one byte from the source. If the souce is exausted, jump
171 to no_more_source:. */
174 if (! __C_conforms_to_XXX___ (c
))
176 if (! __C_strongly_suggests_XXX__ (c
))
177 found
= CATEGORY_MASK_XXX
;
179 /* The byte sequence is invalid for XXX. */
180 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
184 /* The source exausted successfully. */
185 detect_info
->found
|= found
;
190 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
192 These functions decode a byte sequence specified as a source by
193 CODING. The resulting multibyte text goes to a place pointed to by
194 CODING->charbuf, the length of which should not exceed
195 CODING->charbuf_size;
197 These functions set the information of original and decoded texts in
198 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
199 They also set CODING->result to one of CODING_RESULT_XXX indicating
200 how the decoding is finished.
202 Below is the template of these functions. */
206 decode_coding_XXXX (coding
)
207 struct coding_system
*coding
;
209 const unsigned char *src
= coding
->source
+ coding
->consumed
;
210 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
211 /* SRC_BASE remembers the start position in source in each loop.
212 The loop will be exited when there's not enough source code, or
213 when there's no room in CHARBUF for a decoded character. */
214 const unsigned char *src_base
;
215 /* A buffer to produce decoded characters. */
216 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
217 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
218 int multibytep
= coding
->src_multibyte
;
223 if (charbuf
< charbuf_end
)
224 /* No more room to produce a decoded character. */
231 if (src_base
< src_end
232 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
233 /* If the source ends by partial bytes to construct a character,
234 treat them as eight-bit raw data. */
235 while (src_base
< src_end
&& charbuf
< charbuf_end
)
236 *charbuf
++ = *src_base
++;
237 /* Remember how many bytes and characters we consumed. If the
238 source is multibyte, the bytes and chars are not identical. */
239 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
240 /* Remember how many characters we produced. */
241 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
245 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
247 These functions encode SRC_BYTES length text at SOURCE of Emacs'
248 internal multibyte format by CODING. The resulting byte sequence
249 goes to a place pointed to by DESTINATION, the length of which
250 should not exceed DST_BYTES.
252 These functions set the information of original and encoded texts in
253 the members produced, produced_char, consumed, and consumed_char of
254 the structure *CODING. They also set the member result to one of
255 CODING_RESULT_XXX indicating how the encoding finished.
257 DST_BYTES zero means that source area and destination area are
258 overlapped, which means that we can produce a encoded text until it
259 reaches at the head of not-yet-encoded source text.
261 Below is a template of these functions. */
264 encode_coding_XXX (coding
)
265 struct coding_system
*coding
;
267 int multibytep
= coding
->dst_multibyte
;
268 int *charbuf
= coding
->charbuf
;
269 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
270 unsigned char *dst
= coding
->destination
+ coding
->produced
;
271 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
272 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
273 int produced_chars
= 0;
275 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
278 /* Encode C into DST, and increment DST. */
280 label_no_more_destination
:
281 /* How many chars and bytes we produced. */
282 coding
->produced_char
+= produced_chars
;
283 coding
->produced
= dst
- coding
->destination
;
288 /*** 1. Preamble ***/
296 #include "character.h"
299 #include "composite.h"
303 #include "termhooks.h"
305 Lisp_Object Vcoding_system_hash_table
;
307 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
308 Lisp_Object Qunix
, Qdos
;
309 extern Lisp_Object Qmac
; /* frame.c */
310 Lisp_Object Qbuffer_file_coding_system
;
311 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
312 Lisp_Object Qdefault_char
;
313 Lisp_Object Qno_conversion
, Qundecided
;
314 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
315 Lisp_Object Qbig
, Qlittle
;
316 Lisp_Object Qcoding_system_history
;
317 Lisp_Object Qvalid_codes
;
318 Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
319 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
320 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
321 Lisp_Object QCascii_compatible_p
;
323 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
324 Lisp_Object Qcall_process
, Qcall_process_region
;
325 Lisp_Object Qstart_process
, Qopen_network_stream
;
326 Lisp_Object Qtarget_idx
;
328 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
329 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
331 extern Lisp_Object Qcompletion_ignore_case
;
333 /* If a symbol has this property, evaluate the value to define the
334 symbol as a coding system. */
335 static Lisp_Object Qcoding_system_define_form
;
337 int coding_system_require_warning
;
339 Lisp_Object Vselect_safe_coding_system_function
;
341 /* Mnemonic string for each format of end-of-line. */
342 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
343 /* Mnemonic string to indicate format of end-of-line is not yet
345 Lisp_Object eol_mnemonic_undecided
;
347 /* Format of end-of-line decided by system. This is Qunix on
348 Unix and Mac, Qdos on DOS/Windows.
349 This has an effect only for external encoding (i.e. for output to
350 file and process), not for in-buffer or Lisp string encoding. */
351 static Lisp_Object system_eol_type
;
355 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
357 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
359 /* Coding system emacs-mule and raw-text are for converting only
360 end-of-line format. */
361 Lisp_Object Qemacs_mule
, Qraw_text
;
362 Lisp_Object Qutf_8_emacs
;
364 /* Coding-systems are handed between Emacs Lisp programs and C internal
365 routines by the following three variables. */
366 /* Coding-system for reading files and receiving data from process. */
367 Lisp_Object Vcoding_system_for_read
;
368 /* Coding-system for writing files and sending data to process. */
369 Lisp_Object Vcoding_system_for_write
;
370 /* Coding-system actually used in the latest I/O. */
371 Lisp_Object Vlast_coding_system_used
;
372 /* Set to non-nil when an error is detected while code conversion. */
373 Lisp_Object Vlast_code_conversion_error
;
374 /* A vector of length 256 which contains information about special
375 Latin codes (especially for dealing with Microsoft codes). */
376 Lisp_Object Vlatin_extra_code_table
;
378 /* Flag to inhibit code conversion of end-of-line format. */
379 int inhibit_eol_conversion
;
381 /* Flag to inhibit ISO2022 escape sequence detection. */
382 int inhibit_iso_escape_detection
;
384 /* Flag to inhibit detection of binary files through null bytes. */
385 int inhibit_null_byte_detection
;
387 /* Flag to make buffer-file-coding-system inherit from process-coding. */
388 int inherit_process_coding_system
;
390 /* Coding system to be used to encode text for terminal display when
391 terminal coding system is nil. */
392 struct coding_system safe_terminal_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] != 255 \
439 ? (coding)->safe_charsets[charset_id] \
444 #define CODING_ISO_FLAGS(coding) \
445 ((coding)->spec.iso_2022.flags)
446 #define CODING_ISO_DESIGNATION(coding, reg) \
447 ((coding)->spec.iso_2022.current_designation[reg])
448 #define CODING_ISO_INVOCATION(coding, plane) \
449 ((coding)->spec.iso_2022.current_invocation[plane])
450 #define CODING_ISO_SINGLE_SHIFTING(coding) \
451 ((coding)->spec.iso_2022.single_shifting)
452 #define CODING_ISO_BOL(coding) \
453 ((coding)->spec.iso_2022.bol)
454 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
455 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
456 #define CODING_ISO_CMP_STATUS(coding) \
457 (&(coding)->spec.iso_2022.cmp_status)
458 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
459 ((coding)->spec.iso_2022.ctext_extended_segment_len)
460 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
461 ((coding)->spec.iso_2022.embedded_utf_8)
463 /* Control characters of ISO2022. */
464 /* code */ /* function */
465 #define ISO_CODE_LF 0x0A /* line-feed */
466 #define ISO_CODE_CR 0x0D /* carriage-return */
467 #define ISO_CODE_SO 0x0E /* shift-out */
468 #define ISO_CODE_SI 0x0F /* shift-in */
469 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
470 #define ISO_CODE_ESC 0x1B /* escape */
471 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
472 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
473 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
475 /* All code (1-byte) of ISO2022 is classified into one of the
477 enum iso_code_class_type
479 ISO_control_0
, /* Control codes in the range
480 0x00..0x1F and 0x7F, except for the
481 following 5 codes. */
482 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
483 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
484 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
485 ISO_escape
, /* ISO_CODE_SO (0x1B) */
486 ISO_control_1
, /* Control codes in the range
487 0x80..0x9F, except for the
488 following 3 codes. */
489 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
490 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
491 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
492 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
493 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
494 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
495 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
498 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
499 `iso-flags' attribute of an iso2022 coding system. */
501 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
502 instead of the correct short-form sequence (e.g. ESC $ A). */
503 #define CODING_ISO_FLAG_LONG_FORM 0x0001
505 /* If set, reset graphic planes and registers at end-of-line to the
507 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
509 /* If set, reset graphic planes and registers before any control
510 characters to the initial state. */
511 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
513 /* If set, encode by 7-bit environment. */
514 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
516 /* If set, use locking-shift function. */
517 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
519 /* If set, use single-shift function. Overwrite
520 CODING_ISO_FLAG_LOCKING_SHIFT. */
521 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
523 /* If set, use designation escape sequence. */
524 #define CODING_ISO_FLAG_DESIGNATION 0x0040
526 /* If set, produce revision number sequence. */
527 #define CODING_ISO_FLAG_REVISION 0x0080
529 /* If set, produce ISO6429's direction specifying sequence. */
530 #define CODING_ISO_FLAG_DIRECTION 0x0100
532 /* If set, assume designation states are reset at beginning of line on
534 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
536 /* If set, designation sequence should be placed at beginning of line
538 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
540 /* If set, do not encode unsafe charactes on output. */
541 #define CODING_ISO_FLAG_SAFE 0x0800
543 /* If set, extra latin codes (128..159) are accepted as a valid code
545 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
547 #define CODING_ISO_FLAG_COMPOSITION 0x2000
549 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
551 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
553 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
555 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
557 /* A character to be produced on output if encoding of the original
558 character is prohibited by CODING_ISO_FLAG_SAFE. */
559 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
562 #define CODING_UTF_8_BOM(coding) \
563 ((coding)->spec.utf_8_bom)
566 #define CODING_UTF_16_BOM(coding) \
567 ((coding)->spec.utf_16.bom)
569 #define CODING_UTF_16_ENDIAN(coding) \
570 ((coding)->spec.utf_16.endian)
572 #define CODING_UTF_16_SURROGATE(coding) \
573 ((coding)->spec.utf_16.surrogate)
577 #define CODING_CCL_DECODER(coding) \
578 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
579 #define CODING_CCL_ENCODER(coding) \
580 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
581 #define CODING_CCL_VALIDS(coding) \
582 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
584 /* Index for each coding category in `coding_categories' */
588 coding_category_iso_7
,
589 coding_category_iso_7_tight
,
590 coding_category_iso_8_1
,
591 coding_category_iso_8_2
,
592 coding_category_iso_7_else
,
593 coding_category_iso_8_else
,
594 coding_category_utf_8_auto
,
595 coding_category_utf_8_nosig
,
596 coding_category_utf_8_sig
,
597 coding_category_utf_16_auto
,
598 coding_category_utf_16_be
,
599 coding_category_utf_16_le
,
600 coding_category_utf_16_be_nosig
,
601 coding_category_utf_16_le_nosig
,
602 coding_category_charset
,
603 coding_category_sjis
,
604 coding_category_big5
,
606 coding_category_emacs_mule
,
607 /* All above are targets of code detection. */
608 coding_category_raw_text
,
609 coding_category_undecided
,
613 /* Definitions of flag bits used in detect_coding_XXXX. */
614 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
615 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
616 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
617 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
618 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
619 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
620 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
621 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
622 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
623 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
624 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
625 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
626 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
627 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
628 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
629 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
630 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
631 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
632 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
633 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
635 /* This value is returned if detect_coding_mask () find nothing other
636 than ASCII characters. */
637 #define CATEGORY_MASK_ANY \
638 (CATEGORY_MASK_ISO_7 \
639 | CATEGORY_MASK_ISO_7_TIGHT \
640 | CATEGORY_MASK_ISO_8_1 \
641 | CATEGORY_MASK_ISO_8_2 \
642 | CATEGORY_MASK_ISO_7_ELSE \
643 | CATEGORY_MASK_ISO_8_ELSE \
644 | CATEGORY_MASK_UTF_8_AUTO \
645 | CATEGORY_MASK_UTF_8_NOSIG \
646 | CATEGORY_MASK_UTF_8_SIG \
647 | CATEGORY_MASK_UTF_16_AUTO \
648 | CATEGORY_MASK_UTF_16_BE \
649 | CATEGORY_MASK_UTF_16_LE \
650 | CATEGORY_MASK_UTF_16_BE_NOSIG \
651 | CATEGORY_MASK_UTF_16_LE_NOSIG \
652 | CATEGORY_MASK_CHARSET \
653 | CATEGORY_MASK_SJIS \
654 | CATEGORY_MASK_BIG5 \
655 | CATEGORY_MASK_CCL \
656 | CATEGORY_MASK_EMACS_MULE)
659 #define CATEGORY_MASK_ISO_7BIT \
660 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
662 #define CATEGORY_MASK_ISO_8BIT \
663 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
665 #define CATEGORY_MASK_ISO_ELSE \
666 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
668 #define CATEGORY_MASK_ISO_ESCAPE \
669 (CATEGORY_MASK_ISO_7 \
670 | CATEGORY_MASK_ISO_7_TIGHT \
671 | CATEGORY_MASK_ISO_7_ELSE \
672 | CATEGORY_MASK_ISO_8_ELSE)
674 #define CATEGORY_MASK_ISO \
675 ( CATEGORY_MASK_ISO_7BIT \
676 | CATEGORY_MASK_ISO_8BIT \
677 | CATEGORY_MASK_ISO_ELSE)
679 #define CATEGORY_MASK_UTF_16 \
680 (CATEGORY_MASK_UTF_16_AUTO \
681 | CATEGORY_MASK_UTF_16_BE \
682 | CATEGORY_MASK_UTF_16_LE \
683 | CATEGORY_MASK_UTF_16_BE_NOSIG \
684 | CATEGORY_MASK_UTF_16_LE_NOSIG)
686 #define CATEGORY_MASK_UTF_8 \
687 (CATEGORY_MASK_UTF_8_AUTO \
688 | CATEGORY_MASK_UTF_8_NOSIG \
689 | CATEGORY_MASK_UTF_8_SIG)
691 /* List of symbols `coding-category-xxx' ordered by priority. This
692 variable is exposed to Emacs Lisp. */
693 static Lisp_Object Vcoding_category_list
;
695 /* Table of coding categories (Lisp symbols). This variable is for
697 static Lisp_Object Vcoding_category_table
;
699 /* Table of coding-categories ordered by priority. */
700 static enum coding_category coding_priorities
[coding_category_max
];
702 /* Nth element is a coding context for the coding system bound to the
703 Nth coding category. */
704 static struct coding_system coding_categories
[coding_category_max
];
706 /*** Commonly used macros and functions ***/
709 #define min(a, b) ((a) < (b) ? (a) : (b))
712 #define max(a, b) ((a) > (b) ? (a) : (b))
715 #define CODING_GET_INFO(coding, attrs, charset_list) \
717 (attrs) = CODING_ID_ATTRS ((coding)->id); \
718 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
722 /* Safely get one byte from the source text pointed by SRC which ends
723 at SRC_END, and set C to that byte. If there are not enough bytes
724 in the source, it jumps to `no_more_source'. If multibytep is
725 nonzero, and a multibyte character is found at SRC, set C to the
726 negative value of the character code. The caller should declare
727 and set these variables appropriately in advance:
728 src, src_end, multibytep */
730 #define ONE_MORE_BYTE(c) \
732 if (src == src_end) \
734 if (src_base < src) \
735 record_conversion_result \
736 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
737 goto no_more_source; \
740 if (multibytep && (c & 0x80)) \
742 if ((c & 0xFE) == 0xC0) \
743 c = ((c & 1) << 6) | *src++; \
747 c = - string_char (src, &src, NULL); \
748 record_conversion_result \
749 (coding, CODING_RESULT_INVALID_SRC); \
755 /* Safely get two bytes from the source text pointed by SRC which ends
756 at SRC_END, and set C1 and C2 to those bytes while skipping the
757 heading multibyte characters. If there are not enough bytes in the
758 source, it jumps to `no_more_source'. If multibytep is nonzero and
759 a multibyte character is found for C2, set C2 to the negative value
760 of the character code. The caller should declare and set these
761 variables appropriately in advance:
762 src, src_end, multibytep
763 It is intended that this macro is used in detect_coding_utf_16. */
765 #define TWO_MORE_BYTES(c1, c2) \
768 if (src == src_end) \
769 goto no_more_source; \
771 if (multibytep && (c1 & 0x80)) \
773 if ((c1 & 0xFE) == 0xC0) \
774 c1 = ((c1 & 1) << 6) | *src++; \
777 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
782 if (src == src_end) \
783 goto no_more_source; \
785 if (multibytep && (c2 & 0x80)) \
787 if ((c2 & 0xFE) == 0xC0) \
788 c2 = ((c2 & 1) << 6) | *src++; \
795 #define ONE_MORE_BYTE_NO_CHECK(c) \
798 if (multibytep && (c & 0x80)) \
800 if ((c & 0xFE) == 0xC0) \
801 c = ((c & 1) << 6) | *src++; \
805 c = - string_char (src, &src, NULL); \
806 record_conversion_result \
807 (coding, CODING_RESULT_INVALID_SRC); \
814 /* Store a byte C in the place pointed by DST and increment DST to the
815 next free point, and increment PRODUCED_CHARS. The caller should
816 assure that C is 0..127, and declare and set the variable `dst'
817 appropriately in advance.
821 #define EMIT_ONE_ASCII_BYTE(c) \
828 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
830 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
832 produced_chars += 2; \
833 *dst++ = (c1), *dst++ = (c2); \
837 /* Store a byte C in the place pointed by DST and increment DST to the
838 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
839 nonzero, store in an appropriate multibyte from. The caller should
840 declare and set the variables `dst' and `multibytep' appropriately
843 #define EMIT_ONE_BYTE(c) \
850 ch = BYTE8_TO_CHAR (ch); \
851 CHAR_STRING_ADVANCE (ch, dst); \
858 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
860 #define EMIT_TWO_BYTES(c1, c2) \
862 produced_chars += 2; \
869 ch = BYTE8_TO_CHAR (ch); \
870 CHAR_STRING_ADVANCE (ch, dst); \
873 ch = BYTE8_TO_CHAR (ch); \
874 CHAR_STRING_ADVANCE (ch, dst); \
884 #define EMIT_THREE_BYTES(c1, c2, c3) \
886 EMIT_ONE_BYTE (c1); \
887 EMIT_TWO_BYTES (c2, c3); \
891 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
893 EMIT_TWO_BYTES (c1, c2); \
894 EMIT_TWO_BYTES (c3, c4); \
898 /* Prototypes for static functions. */
899 static void record_conversion_result
P_ ((struct coding_system
*coding
,
900 enum coding_result_code result
));
901 static int detect_coding_utf_8
P_ ((struct coding_system
*,
902 struct coding_detection_info
*info
));
903 static void decode_coding_utf_8
P_ ((struct coding_system
*));
904 static int encode_coding_utf_8
P_ ((struct coding_system
*));
906 static int detect_coding_utf_16
P_ ((struct coding_system
*,
907 struct coding_detection_info
*info
));
908 static void decode_coding_utf_16
P_ ((struct coding_system
*));
909 static int encode_coding_utf_16
P_ ((struct coding_system
*));
911 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
912 struct coding_detection_info
*info
));
913 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
914 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
916 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
917 struct coding_detection_info
*info
));
918 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
919 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
921 static int detect_coding_sjis
P_ ((struct coding_system
*,
922 struct coding_detection_info
*info
));
923 static void decode_coding_sjis
P_ ((struct coding_system
*));
924 static int encode_coding_sjis
P_ ((struct coding_system
*));
926 static int detect_coding_big5
P_ ((struct coding_system
*,
927 struct coding_detection_info
*info
));
928 static void decode_coding_big5
P_ ((struct coding_system
*));
929 static int encode_coding_big5
P_ ((struct coding_system
*));
931 static int detect_coding_ccl
P_ ((struct coding_system
*,
932 struct coding_detection_info
*info
));
933 static void decode_coding_ccl
P_ ((struct coding_system
*));
934 static int encode_coding_ccl
P_ ((struct coding_system
*));
936 static void decode_coding_raw_text
P_ ((struct coding_system
*));
937 static int encode_coding_raw_text
P_ ((struct coding_system
*));
939 static void coding_set_source
P_ ((struct coding_system
*));
940 static void coding_set_destination
P_ ((struct coding_system
*));
941 static void coding_alloc_by_realloc
P_ ((struct coding_system
*, EMACS_INT
));
942 static void coding_alloc_by_making_gap
P_ ((struct coding_system
*,
943 EMACS_INT
, EMACS_INT
));
944 static unsigned char *alloc_destination
P_ ((struct coding_system
*,
945 EMACS_INT
, unsigned char *));
946 static void setup_iso_safe_charsets
P_ ((Lisp_Object
));
947 static unsigned char *encode_designation_at_bol
P_ ((struct coding_system
*,
950 static int detect_eol
P_ ((const unsigned char *,
951 EMACS_INT
, enum coding_category
));
952 static Lisp_Object adjust_coding_eol_type
P_ ((struct coding_system
*, int));
953 static void decode_eol
P_ ((struct coding_system
*));
954 static Lisp_Object get_translation_table
P_ ((Lisp_Object
, int, int *));
955 static Lisp_Object get_translation
P_ ((Lisp_Object
, int *, int *));
956 static int produce_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
957 static INLINE
void produce_charset
P_ ((struct coding_system
*, int *,
959 static void produce_annotation
P_ ((struct coding_system
*, EMACS_INT
));
960 static int decode_coding
P_ ((struct coding_system
*));
961 static INLINE
int *handle_composition_annotation
P_ ((EMACS_INT
, EMACS_INT
,
962 struct coding_system
*,
963 int *, EMACS_INT
*));
964 static INLINE
int *handle_charset_annotation
P_ ((EMACS_INT
, EMACS_INT
,
965 struct coding_system
*,
966 int *, EMACS_INT
*));
967 static void consume_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
968 static int encode_coding
P_ ((struct coding_system
*));
969 static Lisp_Object make_conversion_work_buffer
P_ ((int));
970 static Lisp_Object code_conversion_restore
P_ ((Lisp_Object
));
971 static INLINE
int char_encodable_p
P_ ((int, Lisp_Object
));
972 static Lisp_Object make_subsidiaries
P_ ((Lisp_Object
));
975 record_conversion_result (struct coding_system
*coding
,
976 enum coding_result_code result
)
978 coding
->result
= result
;
981 case CODING_RESULT_INSUFFICIENT_SRC
:
982 Vlast_code_conversion_error
= Qinsufficient_source
;
984 case CODING_RESULT_INCONSISTENT_EOL
:
985 Vlast_code_conversion_error
= Qinconsistent_eol
;
987 case CODING_RESULT_INVALID_SRC
:
988 Vlast_code_conversion_error
= Qinvalid_source
;
990 case CODING_RESULT_INTERRUPT
:
991 Vlast_code_conversion_error
= Qinterrupted
;
993 case CODING_RESULT_INSUFFICIENT_MEM
:
994 Vlast_code_conversion_error
= Qinsufficient_memory
;
996 case CODING_RESULT_SUCCESS
:
999 Vlast_code_conversion_error
= intern ("Unknown error");
1003 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
1005 charset_map_loaded = 0; \
1006 c = DECODE_CHAR (charset, code); \
1007 if (charset_map_loaded) \
1009 const unsigned char *orig = coding->source; \
1012 coding_set_source (coding); \
1013 offset = coding->source - orig; \
1015 src_base += offset; \
1016 src_end += offset; \
1021 /* If there are at least BYTES length of room at dst, allocate memory
1022 for coding->destination and update dst and dst_end. We don't have
1023 to take care of coding->source which will be relocated. It is
1024 handled by calling coding_set_source in encode_coding. */
1026 #define ASSURE_DESTINATION(bytes) \
1028 if (dst + (bytes) >= dst_end) \
1030 int more_bytes = charbuf_end - charbuf + (bytes); \
1032 dst = alloc_destination (coding, more_bytes, dst); \
1033 dst_end = coding->destination + coding->dst_bytes; \
1038 /* Store multibyte form of the character C in P, and advance P to the
1039 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
1040 never calls MAYBE_UNIFY_CHAR. */
1042 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
1044 if ((c) <= MAX_1_BYTE_CHAR) \
1046 else if ((c) <= MAX_2_BYTE_CHAR) \
1047 *(p)++ = (0xC0 | ((c) >> 6)), \
1048 *(p)++ = (0x80 | ((c) & 0x3F)); \
1049 else if ((c) <= MAX_3_BYTE_CHAR) \
1050 *(p)++ = (0xE0 | ((c) >> 12)), \
1051 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
1052 *(p)++ = (0x80 | ((c) & 0x3F)); \
1053 else if ((c) <= MAX_4_BYTE_CHAR) \
1054 *(p)++ = (0xF0 | (c >> 18)), \
1055 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1056 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1057 *(p)++ = (0x80 | (c & 0x3F)); \
1058 else if ((c) <= MAX_5_BYTE_CHAR) \
1060 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
1061 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1062 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1063 *(p)++ = (0x80 | (c & 0x3F)); \
1065 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1069 /* Return the character code of character whose multibyte form is at
1070 P, and advance P to the end of the multibyte form. This is like
1071 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1073 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1076 : ! ((p)[0] & 0x20) \
1078 ((((p)[-2] & 0x1F) << 6) \
1079 | ((p)[-1] & 0x3F) \
1080 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1081 : ! ((p)[0] & 0x10) \
1083 ((((p)[-3] & 0x0F) << 12) \
1084 | (((p)[-2] & 0x3F) << 6) \
1085 | ((p)[-1] & 0x3F))) \
1086 : ! ((p)[0] & 0x08) \
1088 ((((p)[-4] & 0xF) << 18) \
1089 | (((p)[-3] & 0x3F) << 12) \
1090 | (((p)[-2] & 0x3F) << 6) \
1091 | ((p)[-1] & 0x3F))) \
1093 ((((p)[-4] & 0x3F) << 18) \
1094 | (((p)[-3] & 0x3F) << 12) \
1095 | (((p)[-2] & 0x3F) << 6) \
1096 | ((p)[-1] & 0x3F))))
1100 coding_set_source (coding
)
1101 struct coding_system
*coding
;
1103 if (BUFFERP (coding
->src_object
))
1105 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1107 if (coding
->src_pos
< 0)
1108 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1110 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1112 else if (STRINGP (coding
->src_object
))
1114 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1117 /* Otherwise, the source is C string and is never relocated
1118 automatically. Thus we don't have to update anything. */
1123 coding_set_destination (coding
)
1124 struct coding_system
*coding
;
1126 if (BUFFERP (coding
->dst_object
))
1128 if (coding
->src_pos
< 0)
1130 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1131 coding
->dst_bytes
= (GAP_END_ADDR
1132 - (coding
->src_bytes
- coding
->consumed
)
1133 - coding
->destination
);
1137 /* We are sure that coding->dst_pos_byte is before the gap
1139 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1140 + coding
->dst_pos_byte
- BEG_BYTE
);
1141 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1142 - coding
->destination
);
1146 /* Otherwise, the destination is C string and is never relocated
1147 automatically. Thus we don't have to update anything. */
1153 coding_alloc_by_realloc (coding
, bytes
)
1154 struct coding_system
*coding
;
1157 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1158 coding
->dst_bytes
+ bytes
);
1159 coding
->dst_bytes
+= bytes
;
1163 coding_alloc_by_making_gap (coding
, gap_head_used
, bytes
)
1164 struct coding_system
*coding
;
1165 EMACS_INT gap_head_used
, bytes
;
1167 if (EQ (coding
->src_object
, coding
->dst_object
))
1169 /* The gap may contain the produced data at the head and not-yet
1170 consumed data at the tail. To preserve those data, we at
1171 first make the gap size to zero, then increase the gap
1173 EMACS_INT add
= GAP_SIZE
;
1175 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1176 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1178 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1179 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1183 Lisp_Object this_buffer
;
1185 this_buffer
= Fcurrent_buffer ();
1186 set_buffer_internal (XBUFFER (coding
->dst_object
));
1188 set_buffer_internal (XBUFFER (this_buffer
));
1193 static unsigned char *
1194 alloc_destination (coding
, nbytes
, dst
)
1195 struct coding_system
*coding
;
1199 EMACS_INT offset
= dst
- coding
->destination
;
1201 if (BUFFERP (coding
->dst_object
))
1203 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1205 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1208 coding_alloc_by_realloc (coding
, nbytes
);
1209 coding_set_destination (coding
);
1210 dst
= coding
->destination
+ offset
;
1214 /** Macros for annotations. */
1216 /* An annotation data is stored in the array coding->charbuf in this
1218 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1219 LENGTH is the number of elements in the annotation.
1220 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1221 NCHARS is the number of characters in the text annotated.
1223 The format of the following elements depend on ANNOTATION_MASK.
1225 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1227 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1229 NBYTES is the number of bytes specified in the header part of
1230 old-style emacs-mule encoding, or 0 for the other kind of
1233 METHOD is one of enum composition_method.
1235 Optionnal COMPOSITION-COMPONENTS are characters and composition
1238 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1241 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1242 recover from an invalid annotation, and should be skipped by
1243 produce_annotation. */
1245 /* Maximum length of the header of annotation data. */
1246 #define MAX_ANNOTATION_LENGTH 5
1248 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1250 *(buf)++ = -(len); \
1251 *(buf)++ = (mask); \
1252 *(buf)++ = (nchars); \
1253 coding->annotated = 1; \
1256 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1258 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1264 #define ADD_CHARSET_DATA(buf, nchars, id) \
1266 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1271 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1278 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1279 Check if a text is encoded in UTF-8. If it is, return 1, else
1282 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1283 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1284 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1285 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1286 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1287 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1289 #define UTF_BOM 0xFEFF
1290 #define UTF_8_BOM_1 0xEF
1291 #define UTF_8_BOM_2 0xBB
1292 #define UTF_8_BOM_3 0xBF
1295 detect_coding_utf_8 (coding
, detect_info
)
1296 struct coding_system
*coding
;
1297 struct coding_detection_info
*detect_info
;
1299 const unsigned char *src
= coding
->source
, *src_base
;
1300 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1301 int multibytep
= coding
->src_multibyte
;
1302 int consumed_chars
= 0;
1306 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1307 /* A coding system of this category is always ASCII compatible. */
1308 src
+= coding
->head_ascii
;
1312 int c
, c1
, c2
, c3
, c4
;
1316 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1319 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1321 if (UTF_8_2_OCTET_LEADING_P (c
))
1327 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1329 if (UTF_8_3_OCTET_LEADING_P (c
))
1332 if (src_base
== coding
->source
1333 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1338 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1340 if (UTF_8_4_OCTET_LEADING_P (c
))
1346 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1348 if (UTF_8_5_OCTET_LEADING_P (c
))
1355 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1359 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1361 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1366 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1367 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1371 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1373 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1380 decode_coding_utf_8 (coding
)
1381 struct coding_system
*coding
;
1383 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1384 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1385 const unsigned char *src_base
;
1386 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1387 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1388 int consumed_chars
= 0, consumed_chars_base
= 0;
1389 int multibytep
= coding
->src_multibyte
;
1390 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1391 Lisp_Object attr
, charset_list
;
1393 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1394 int byte_after_cr
= -1;
1396 CODING_GET_INFO (coding
, attr
, charset_list
);
1398 if (bom
!= utf_without_bom
)
1404 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1409 if (! UTF_8_EXTRA_OCTET_P (c2
))
1414 if (! UTF_8_EXTRA_OCTET_P (c3
))
1418 if ((c1
!= UTF_8_BOM_1
)
1419 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1422 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1427 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1433 int c
, c1
, c2
, c3
, c4
, c5
;
1436 consumed_chars_base
= consumed_chars
;
1438 if (charbuf
>= charbuf_end
)
1440 if (byte_after_cr
>= 0)
1445 if (byte_after_cr
>= 0)
1446 c1
= byte_after_cr
, byte_after_cr
= -1;
1453 else if (UTF_8_1_OCTET_P(c1
))
1455 if (eol_crlf
&& c1
== '\r')
1456 ONE_MORE_BYTE (byte_after_cr
);
1462 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1464 if (UTF_8_2_OCTET_LEADING_P (c1
))
1466 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1467 /* Reject overlong sequences here and below. Encoders
1468 producing them are incorrect, they can be misleading,
1469 and they mess up read/write invariance. */
1476 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1478 if (UTF_8_3_OCTET_LEADING_P (c1
))
1480 c
= (((c1
& 0xF) << 12)
1481 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1483 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1489 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1491 if (UTF_8_4_OCTET_LEADING_P (c1
))
1493 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1494 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1501 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1503 if (UTF_8_5_OCTET_LEADING_P (c1
))
1505 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1506 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1508 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1523 consumed_chars
= consumed_chars_base
;
1525 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1530 coding
->consumed_char
+= consumed_chars_base
;
1531 coding
->consumed
= src_base
- coding
->source
;
1532 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1537 encode_coding_utf_8 (coding
)
1538 struct coding_system
*coding
;
1540 int multibytep
= coding
->dst_multibyte
;
1541 int *charbuf
= coding
->charbuf
;
1542 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1543 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1544 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1545 int produced_chars
= 0;
1548 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1550 ASSURE_DESTINATION (3);
1551 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1552 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1557 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1559 while (charbuf
< charbuf_end
)
1561 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1563 ASSURE_DESTINATION (safe_room
);
1565 if (CHAR_BYTE8_P (c
))
1567 c
= CHAR_TO_BYTE8 (c
);
1572 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1573 for (p
= str
; p
< pend
; p
++)
1580 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1582 while (charbuf
< charbuf_end
)
1584 ASSURE_DESTINATION (safe_room
);
1586 if (CHAR_BYTE8_P (c
))
1587 *dst
++ = CHAR_TO_BYTE8 (c
);
1589 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1593 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1594 coding
->produced_char
+= produced_chars
;
1595 coding
->produced
= dst
- coding
->destination
;
1600 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1601 Check if a text is encoded in one of UTF-16 based coding systems.
1602 If it is, return 1, else return 0. */
1604 #define UTF_16_HIGH_SURROGATE_P(val) \
1605 (((val) & 0xFC00) == 0xD800)
1607 #define UTF_16_LOW_SURROGATE_P(val) \
1608 (((val) & 0xFC00) == 0xDC00)
1610 #define UTF_16_INVALID_P(val) \
1611 (((val) == 0xFFFE) \
1612 || ((val) == 0xFFFF) \
1613 || UTF_16_LOW_SURROGATE_P (val))
1617 detect_coding_utf_16 (coding
, detect_info
)
1618 struct coding_system
*coding
;
1619 struct coding_detection_info
*detect_info
;
1621 const unsigned char *src
= coding
->source
, *src_base
= src
;
1622 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1623 int multibytep
= coding
->src_multibyte
;
1624 int consumed_chars
= 0;
1627 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1628 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1629 && (coding
->src_chars
& 1))
1631 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1635 TWO_MORE_BYTES (c1
, c2
);
1636 if ((c1
== 0xFF) && (c2
== 0xFE))
1638 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1639 | CATEGORY_MASK_UTF_16_AUTO
);
1640 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1641 | CATEGORY_MASK_UTF_16_BE_NOSIG
1642 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1644 else if ((c1
== 0xFE) && (c2
== 0xFF))
1646 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1647 | CATEGORY_MASK_UTF_16_AUTO
);
1648 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1649 | CATEGORY_MASK_UTF_16_BE_NOSIG
1650 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1654 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1659 /* We check the dispersion of Eth and Oth bytes where E is even and
1660 O is odd. If both are high, we assume binary data.*/
1661 unsigned char e
[256], o
[256];
1662 unsigned e_num
= 1, o_num
= 1;
1669 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1670 |CATEGORY_MASK_UTF_16_BE
1671 | CATEGORY_MASK_UTF_16_LE
);
1673 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1674 != CATEGORY_MASK_UTF_16
)
1676 TWO_MORE_BYTES (c1
, c2
);
1684 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1691 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1702 decode_coding_utf_16 (coding
)
1703 struct coding_system
*coding
;
1705 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1706 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1707 const unsigned char *src_base
;
1708 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1709 /* We may produces at most 3 chars in one loop. */
1710 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1711 int consumed_chars
= 0, consumed_chars_base
= 0;
1712 int multibytep
= coding
->src_multibyte
;
1713 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1714 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1715 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1716 Lisp_Object attr
, charset_list
;
1718 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1719 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1721 CODING_GET_INFO (coding
, attr
, charset_list
);
1723 if (bom
== utf_with_bom
)
1732 if (endian
== utf_16_big_endian
1733 ? c
!= 0xFEFF : c
!= 0xFFFE)
1735 /* The first two bytes are not BOM. Treat them as bytes
1736 for a normal character. */
1740 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1742 else if (bom
== utf_detect_bom
)
1744 /* We have already tried to detect BOM and failed in
1746 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1754 consumed_chars_base
= consumed_chars
;
1756 if (charbuf
>= charbuf_end
)
1758 if (byte_after_cr1
>= 0)
1763 if (byte_after_cr1
>= 0)
1764 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1772 if (byte_after_cr2
>= 0)
1773 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1778 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1782 c
= (endian
== utf_16_big_endian
1783 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1787 if (! UTF_16_LOW_SURROGATE_P (c
))
1789 if (endian
== utf_16_big_endian
)
1790 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1792 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1796 if (UTF_16_HIGH_SURROGATE_P (c
))
1797 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1803 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1804 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1805 *charbuf
++ = 0x10000 + c
;
1810 if (UTF_16_HIGH_SURROGATE_P (c
))
1811 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1814 if (eol_crlf
&& c
== '\r')
1816 ONE_MORE_BYTE (byte_after_cr1
);
1817 ONE_MORE_BYTE (byte_after_cr2
);
1825 coding
->consumed_char
+= consumed_chars_base
;
1826 coding
->consumed
= src_base
- coding
->source
;
1827 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1831 encode_coding_utf_16 (coding
)
1832 struct coding_system
*coding
;
1834 int multibytep
= coding
->dst_multibyte
;
1835 int *charbuf
= coding
->charbuf
;
1836 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1837 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1838 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1840 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1841 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1842 int produced_chars
= 0;
1843 Lisp_Object attrs
, charset_list
;
1846 CODING_GET_INFO (coding
, attrs
, charset_list
);
1848 if (bom
!= utf_without_bom
)
1850 ASSURE_DESTINATION (safe_room
);
1852 EMIT_TWO_BYTES (0xFE, 0xFF);
1854 EMIT_TWO_BYTES (0xFF, 0xFE);
1855 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1858 while (charbuf
< charbuf_end
)
1860 ASSURE_DESTINATION (safe_room
);
1862 if (c
> MAX_UNICODE_CHAR
)
1863 c
= coding
->default_char
;
1868 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1870 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1877 c1
= (c
>> 10) + 0xD800;
1878 c2
= (c
& 0x3FF) + 0xDC00;
1880 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1882 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1885 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1886 coding
->produced
= dst
- coding
->destination
;
1887 coding
->produced_char
+= produced_chars
;
1892 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1894 /* Emacs' internal format for representation of multiple character
1895 sets is a kind of multi-byte encoding, i.e. characters are
1896 represented by variable-length sequences of one-byte codes.
1898 ASCII characters and control characters (e.g. `tab', `newline') are
1899 represented by one-byte sequences which are their ASCII codes, in
1900 the range 0x00 through 0x7F.
1902 8-bit characters of the range 0x80..0x9F are represented by
1903 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1906 8-bit characters of the range 0xA0..0xFF are represented by
1907 one-byte sequences which are their 8-bit code.
1909 The other characters are represented by a sequence of `base
1910 leading-code', optional `extended leading-code', and one or two
1911 `position-code's. The length of the sequence is determined by the
1912 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1913 whereas extended leading-code and position-code take the range 0xA0
1914 through 0xFF. See `charset.h' for more details about leading-code
1917 --- CODE RANGE of Emacs' internal format ---
1921 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1922 eight-bit-graphic 0xA0..0xBF
1923 ELSE 0x81..0x9D + [0xA0..0xFF]+
1924 ---------------------------------------------
1926 As this is the internal character representation, the format is
1927 usually not used externally (i.e. in a file or in a data sent to a
1928 process). But, it is possible to have a text externally in this
1929 format (i.e. by encoding by the coding system `emacs-mule').
1931 In that case, a sequence of one-byte codes has a slightly different
1934 At first, all characters in eight-bit-control are represented by
1935 one-byte sequences which are their 8-bit code.
1937 Next, character composition data are represented by the byte
1938 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1940 METHOD is 0xF2 plus one of composition method (enum
1941 composition_method),
1943 BYTES is 0xA0 plus a byte length of this composition data,
1945 CHARS is 0xA0 plus a number of characters composed by this
1948 COMPONENTs are characters of multibye form or composition
1949 rules encoded by two-byte of ASCII codes.
1951 In addition, for backward compatibility, the following formats are
1952 also recognized as composition data on decoding.
1955 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1958 MSEQ is a multibyte form but in these special format:
1959 ASCII: 0xA0 ASCII_CODE+0x80,
1960 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1961 RULE is a one byte code of the range 0xA0..0xF0 that
1962 represents a composition rule.
1965 char emacs_mule_bytes
[256];
1968 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1969 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1973 detect_coding_emacs_mule (coding
, detect_info
)
1974 struct coding_system
*coding
;
1975 struct coding_detection_info
*detect_info
;
1977 const unsigned char *src
= coding
->source
, *src_base
;
1978 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1979 int multibytep
= coding
->src_multibyte
;
1980 int consumed_chars
= 0;
1984 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1985 /* A coding system of this category is always ASCII compatible. */
1986 src
+= coding
->head_ascii
;
1996 /* Perhaps the start of composite character. We simply skip
1997 it because analyzing it is too heavy for detecting. But,
1998 at least, we check that the composite character
1999 constitutes of more than 4 bytes. */
2000 const unsigned char *src_base
;
2010 if (src
- src_base
<= 4)
2012 found
= CATEGORY_MASK_EMACS_MULE
;
2020 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2025 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
2027 while (more_bytes
> 0)
2032 src
--; /* Unread the last byte. */
2037 if (more_bytes
!= 0)
2039 found
= CATEGORY_MASK_EMACS_MULE
;
2042 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2046 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
2048 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2051 detect_info
->found
|= found
;
2056 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
2057 character. If CMP_STATUS indicates that we must expect MSEQ or
2058 RULE described above, decode it and return the negative value of
2059 the deocded character or rule. If an invalid byte is found, return
2060 -1. If SRC is too short, return -2. */
2063 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
, cmp_status
)
2064 struct coding_system
*coding
;
2065 const unsigned char *src
;
2066 int *nbytes
, *nchars
, *id
;
2067 struct composition_status
*cmp_status
;
2069 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2070 const unsigned char *src_base
= src
;
2071 int multibytep
= coding
->src_multibyte
;
2072 struct charset
*charset
;
2075 int consumed_chars
= 0;
2082 charset
= emacs_mule_charset
[0];
2088 if (cmp_status
->state
!= COMPOSING_NO
2089 && cmp_status
->old_form
)
2091 if (cmp_status
->state
== COMPOSING_CHAR
)
2106 *nbytes
= src
- src_base
;
2107 *nchars
= consumed_chars
;
2115 switch (emacs_mule_bytes
[c
])
2118 if (! (charset
= emacs_mule_charset
[c
]))
2127 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2128 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2131 if (c
< 0xA0 || ! (charset
= emacs_mule_charset
[c
]))
2140 if (! (charset
= emacs_mule_charset
[c
]))
2145 code
= (c
& 0x7F) << 8;
2155 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
2160 code
= (c
& 0x7F) << 8;
2169 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
2170 ? charset_ascii
: charset_eight_bit
);
2176 c
= DECODE_CHAR (charset
, code
);
2180 *nbytes
= src
- src_base
;
2181 *nchars
= consumed_chars
;
2184 return (mseq_found
? -c
: c
);
2194 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2196 /* Handle these composition sequence ('|': the end of header elements,
2197 BYTES and CHARS >= 0xA0):
2199 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2200 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2201 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2205 (4) relative composition: 0x80 | MSEQ ... MSEQ
2206 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2208 When the starter 0x80 and the following header elements are found,
2209 this annotation header is produced.
2211 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2213 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2214 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2216 Then, upon reading the following elements, these codes are produced
2217 until the composition end is found:
2220 (2) ALT ... ALT CHAR ... CHAR
2221 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2223 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2225 When the composition end is found, LENGTH and NCHARS in the
2226 annotation header is updated as below:
2228 (1) LENGTH: unchanged, NCHARS: unchanged
2229 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2230 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2231 (4) LENGTH: unchanged, NCHARS: number of CHARs
2232 (5) LENGTH: unchanged, NCHARS: number of CHARs
2234 If an error is found while composing, the annotation header is
2235 changed to the original composition header (plus filler -1s) as
2238 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2239 (5) [ 0x80 0xFF -1 -1- -1 ]
2241 and the sequence [ -2 DECODED-RULE ] is changed to the original
2242 byte sequence as below:
2243 o the original byte sequence is B: [ B -1 ]
2244 o the original byte sequence is B1 B2: [ B1 B2 ]
2246 Most of the routines are implemented by macros because many
2247 variables and labels in the caller decode_coding_emacs_mule must be
2248 accessible, and they are usually called just once (thus doesn't
2249 increase the size of compiled object). */
2251 /* Decode a composition rule represented by C as a component of
2252 composition sequence of Emacs 20 style. Set RULE to the decoded
2255 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2260 if (c < 0 || c >= 81) \
2261 goto invalid_code; \
2262 gref = c / 9, nref = c % 9; \
2263 if (gref == 4) gref = 10; \
2264 if (nref == 4) nref = 10; \
2265 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2269 /* Decode a composition rule represented by C and the following byte
2270 at SRC as a component of composition sequence of Emacs 21 style.
2271 Set RULE to the decoded rule. */
2273 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2278 if (gref < 0 || gref >= 81) \
2279 goto invalid_code; \
2280 ONE_MORE_BYTE (c); \
2282 if (nref < 0 || nref >= 81) \
2283 goto invalid_code; \
2284 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2288 /* Start of Emacs 21 style format. The first three bytes at SRC are
2289 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2290 byte length of this composition information, CHARS is the number of
2291 characters composed by this composition. */
2293 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2295 enum composition_method method = c - 0xF2; \
2296 int *charbuf_base = charbuf; \
2297 int nbytes, nchars; \
2299 ONE_MORE_BYTE (c); \
2301 goto invalid_code; \
2302 nbytes = c - 0xA0; \
2303 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2304 goto invalid_code; \
2305 ONE_MORE_BYTE (c); \
2306 nchars = c - 0xA0; \
2307 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2308 goto invalid_code; \
2309 cmp_status->old_form = 0; \
2310 cmp_status->method = method; \
2311 if (method == COMPOSITION_RELATIVE) \
2312 cmp_status->state = COMPOSING_CHAR; \
2314 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2315 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2316 cmp_status->nchars = nchars; \
2317 cmp_status->ncomps = nbytes - 4; \
2318 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2322 /* Start of Emacs 20 style format for relative composition. */
2324 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2326 cmp_status->old_form = 1; \
2327 cmp_status->method = COMPOSITION_RELATIVE; \
2328 cmp_status->state = COMPOSING_CHAR; \
2329 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2330 cmp_status->nchars = cmp_status->ncomps = 0; \
2331 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2335 /* Start of Emacs 20 style format for rule-base composition. */
2337 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2339 cmp_status->old_form = 1; \
2340 cmp_status->method = COMPOSITION_WITH_RULE; \
2341 cmp_status->state = COMPOSING_CHAR; \
2342 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2343 cmp_status->nchars = cmp_status->ncomps = 0; \
2344 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2348 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2350 const unsigned char *current_src = src; \
2352 ONE_MORE_BYTE (c); \
2354 goto invalid_code; \
2355 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2356 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2357 DECODE_EMACS_MULE_21_COMPOSITION (); \
2358 else if (c < 0xA0) \
2359 goto invalid_code; \
2360 else if (c < 0xC0) \
2362 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2363 /* Re-read C as a composition component. */ \
2364 src = current_src; \
2366 else if (c == 0xFF) \
2367 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2369 goto invalid_code; \
2372 #define EMACS_MULE_COMPOSITION_END() \
2374 int idx = - cmp_status->length; \
2376 if (cmp_status->old_form) \
2377 charbuf[idx + 2] = cmp_status->nchars; \
2378 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2379 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2380 cmp_status->state = COMPOSING_NO; \
2385 emacs_mule_finish_composition (charbuf
, cmp_status
)
2387 struct composition_status
*cmp_status
;
2389 int idx
= - cmp_status
->length
;
2392 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2394 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2396 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2397 && cmp_status
->state
== COMPOSING_CHAR
)
2399 /* The last rule was invalid. */
2400 int rule
= charbuf
[-1] + 0xA0;
2402 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2409 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2411 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2413 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2414 charbuf
[idx
++] = -3;
2420 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2421 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2423 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2424 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2425 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2426 charbuf
[idx
++] = -1;
2430 cmp_status
->state
= COMPOSING_NO
;
2434 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2436 if (cmp_status->state != COMPOSING_NO) \
2437 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2442 decode_coding_emacs_mule (coding
)
2443 struct coding_system
*coding
;
2445 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2446 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2447 const unsigned char *src_base
;
2448 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2449 /* We may produce two annocations (charset and composition) in one
2450 loop and one more charset annocation at the end. */
2452 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
2453 int consumed_chars
= 0, consumed_chars_base
;
2454 int multibytep
= coding
->src_multibyte
;
2455 Lisp_Object attrs
, charset_list
;
2456 int char_offset
= coding
->produced_char
;
2457 int last_offset
= char_offset
;
2458 int last_id
= charset_ascii
;
2460 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2461 int byte_after_cr
= -1;
2462 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2464 CODING_GET_INFO (coding
, attrs
, charset_list
);
2466 if (cmp_status
->state
!= COMPOSING_NO
)
2470 for (i
= 0; i
< cmp_status
->length
; i
++)
2471 *charbuf
++ = cmp_status
->carryover
[i
];
2472 coding
->annotated
= 1;
2480 consumed_chars_base
= consumed_chars
;
2482 if (charbuf
>= charbuf_end
)
2484 if (byte_after_cr
>= 0)
2489 if (byte_after_cr
>= 0)
2490 c
= byte_after_cr
, byte_after_cr
= -1;
2494 if (c
< 0 || c
== 0x80)
2496 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2503 DECODE_EMACS_MULE_COMPOSITION_START ();
2509 if (eol_crlf
&& c
== '\r')
2510 ONE_MORE_BYTE (byte_after_cr
);
2512 if (cmp_status
->state
!= COMPOSING_NO
)
2514 if (cmp_status
->old_form
)
2515 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2516 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2517 cmp_status
->ncomps
--;
2524 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2533 src
= src_base
+ nbytes
;
2534 consumed_chars
= consumed_chars_base
+ nchars
;
2535 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2536 cmp_status
->ncomps
-= nchars
;
2539 /* Now if C >= 0, we found a normally encoded characer, if C <
2540 0, we found an old-style composition component character or
2543 if (cmp_status
->state
== COMPOSING_NO
)
2547 if (last_id
!= charset_ascii
)
2548 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2551 last_offset
= char_offset
;
2556 else if (cmp_status
->state
== COMPOSING_CHAR
)
2558 if (cmp_status
->old_form
)
2562 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2569 cmp_status
->nchars
++;
2570 cmp_status
->length
++;
2571 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2572 EMACS_MULE_COMPOSITION_END ();
2573 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2574 cmp_status
->state
= COMPOSING_RULE
;
2580 cmp_status
->length
++;
2581 cmp_status
->nchars
--;
2582 if (cmp_status
->nchars
== 0)
2583 EMACS_MULE_COMPOSITION_END ();
2586 else if (cmp_status
->state
== COMPOSING_RULE
)
2592 EMACS_MULE_COMPOSITION_END ();
2599 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2604 cmp_status
->length
+= 2;
2605 cmp_status
->state
= COMPOSING_CHAR
;
2608 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2611 cmp_status
->length
++;
2612 if (cmp_status
->ncomps
== 0)
2613 cmp_status
->state
= COMPOSING_CHAR
;
2614 else if (cmp_status
->ncomps
> 0)
2616 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2617 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2620 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2622 else /* COMPOSING_COMPONENT_RULE */
2626 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2631 cmp_status
->length
+= 2;
2632 cmp_status
->ncomps
--;
2633 if (cmp_status
->ncomps
> 0)
2634 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2636 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2642 consumed_chars
= consumed_chars_base
;
2646 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2648 consumed_chars
= consumed_chars_base
;
2650 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2656 if (cmp_status
->state
!= COMPOSING_NO
)
2658 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2659 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2664 charbuf
-= cmp_status
->length
;
2665 for (i
= 0; i
< cmp_status
->length
; i
++)
2666 cmp_status
->carryover
[i
] = charbuf
[i
];
2669 if (last_id
!= charset_ascii
)
2670 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2671 coding
->consumed_char
+= consumed_chars_base
;
2672 coding
->consumed
= src_base
- coding
->source
;
2673 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2677 #define EMACS_MULE_LEADING_CODES(id, codes) \
2680 codes[0] = id, codes[1] = 0; \
2681 else if (id < 0xE0) \
2682 codes[0] = 0x9A, codes[1] = id; \
2683 else if (id < 0xF0) \
2684 codes[0] = 0x9B, codes[1] = id; \
2685 else if (id < 0xF5) \
2686 codes[0] = 0x9C, codes[1] = id; \
2688 codes[0] = 0x9D, codes[1] = id; \
2693 encode_coding_emacs_mule (coding
)
2694 struct coding_system
*coding
;
2696 int multibytep
= coding
->dst_multibyte
;
2697 int *charbuf
= coding
->charbuf
;
2698 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2699 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2700 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2702 int produced_chars
= 0;
2703 Lisp_Object attrs
, charset_list
;
2705 int preferred_charset_id
= -1;
2707 CODING_GET_INFO (coding
, attrs
, charset_list
);
2708 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2710 CODING_ATTR_CHARSET_LIST (attrs
)
2711 = charset_list
= Vemacs_mule_charset_list
;
2714 while (charbuf
< charbuf_end
)
2716 ASSURE_DESTINATION (safe_room
);
2721 /* Handle an annotation. */
2724 case CODING_ANNOTATE_COMPOSITION_MASK
:
2725 /* Not yet implemented. */
2727 case CODING_ANNOTATE_CHARSET_MASK
:
2728 preferred_charset_id
= charbuf
[3];
2729 if (preferred_charset_id
>= 0
2730 && NILP (Fmemq (make_number (preferred_charset_id
),
2732 preferred_charset_id
= -1;
2741 if (ASCII_CHAR_P (c
))
2742 EMIT_ONE_ASCII_BYTE (c
);
2743 else if (CHAR_BYTE8_P (c
))
2745 c
= CHAR_TO_BYTE8 (c
);
2750 struct charset
*charset
;
2754 unsigned char leading_codes
[2];
2756 if (preferred_charset_id
>= 0)
2758 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2759 if (CHAR_CHARSET_P (c
, charset
))
2760 code
= ENCODE_CHAR (charset
, c
);
2762 charset
= char_charset (c
, charset_list
, &code
);
2765 charset
= char_charset (c
, charset_list
, &code
);
2768 c
= coding
->default_char
;
2769 if (ASCII_CHAR_P (c
))
2771 EMIT_ONE_ASCII_BYTE (c
);
2774 charset
= char_charset (c
, charset_list
, &code
);
2776 dimension
= CHARSET_DIMENSION (charset
);
2777 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2778 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2779 EMIT_ONE_BYTE (leading_codes
[0]);
2780 if (leading_codes
[1])
2781 EMIT_ONE_BYTE (leading_codes
[1]);
2783 EMIT_ONE_BYTE (code
| 0x80);
2787 EMIT_ONE_BYTE (code
>> 8);
2788 EMIT_ONE_BYTE (code
& 0xFF);
2792 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2793 coding
->produced_char
+= produced_chars
;
2794 coding
->produced
= dst
- coding
->destination
;
2799 /*** 7. ISO2022 handlers ***/
2801 /* The following note describes the coding system ISO2022 briefly.
2802 Since the intention of this note is to help understand the
2803 functions in this file, some parts are NOT ACCURATE or are OVERLY
2804 SIMPLIFIED. For thorough understanding, please refer to the
2805 original document of ISO2022. This is equivalent to the standard
2806 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2808 ISO2022 provides many mechanisms to encode several character sets
2809 in 7-bit and 8-bit environments. For 7-bit environments, all text
2810 is encoded using bytes less than 128. This may make the encoded
2811 text a little bit longer, but the text passes more easily through
2812 several types of gateway, some of which strip off the MSB (Most
2815 There are two kinds of character sets: control character sets and
2816 graphic character sets. The former contain control characters such
2817 as `newline' and `escape' to provide control functions (control
2818 functions are also provided by escape sequences). The latter
2819 contain graphic characters such as 'A' and '-'. Emacs recognizes
2820 two control character sets and many graphic character sets.
2822 Graphic character sets are classified into one of the following
2823 four classes, according to the number of bytes (DIMENSION) and
2824 number of characters in one dimension (CHARS) of the set:
2825 - DIMENSION1_CHARS94
2826 - DIMENSION1_CHARS96
2827 - DIMENSION2_CHARS94
2828 - DIMENSION2_CHARS96
2830 In addition, each character set is assigned an identification tag,
2831 unique for each set, called the "final character" (denoted as <F>
2832 hereafter). The <F> of each character set is decided by ECMA(*)
2833 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2834 (0x30..0x3F are for private use only).
2836 Note (*): ECMA = European Computer Manufacturers Association
2838 Here are examples of graphic character sets [NAME(<F>)]:
2839 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2840 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2841 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2842 o DIMENSION2_CHARS96 -- none for the moment
2844 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2845 C0 [0x00..0x1F] -- control character plane 0
2846 GL [0x20..0x7F] -- graphic character plane 0
2847 C1 [0x80..0x9F] -- control character plane 1
2848 GR [0xA0..0xFF] -- graphic character plane 1
2850 A control character set is directly designated and invoked to C0 or
2851 C1 by an escape sequence. The most common case is that:
2852 - ISO646's control character set is designated/invoked to C0, and
2853 - ISO6429's control character set is designated/invoked to C1,
2854 and usually these designations/invocations are omitted in encoded
2855 text. In a 7-bit environment, only C0 can be used, and a control
2856 character for C1 is encoded by an appropriate escape sequence to
2857 fit into the environment. All control characters for C1 are
2858 defined to have corresponding escape sequences.
2860 A graphic character set is at first designated to one of four
2861 graphic registers (G0 through G3), then these graphic registers are
2862 invoked to GL or GR. These designations and invocations can be
2863 done independently. The most common case is that G0 is invoked to
2864 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2865 these invocations and designations are omitted in encoded text.
2866 In a 7-bit environment, only GL can be used.
2868 When a graphic character set of CHARS94 is invoked to GL, codes
2869 0x20 and 0x7F of the GL area work as control characters SPACE and
2870 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2873 There are two ways of invocation: locking-shift and single-shift.
2874 With locking-shift, the invocation lasts until the next different
2875 invocation, whereas with single-shift, the invocation affects the
2876 following character only and doesn't affect the locking-shift
2877 state. Invocations are done by the following control characters or
2880 ----------------------------------------------------------------------
2881 abbrev function cntrl escape seq description
2882 ----------------------------------------------------------------------
2883 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2884 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2885 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2886 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2887 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2888 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2889 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2890 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2891 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2892 ----------------------------------------------------------------------
2893 (*) These are not used by any known coding system.
2895 Control characters for these functions are defined by macros
2896 ISO_CODE_XXX in `coding.h'.
2898 Designations are done by the following escape sequences:
2899 ----------------------------------------------------------------------
2900 escape sequence description
2901 ----------------------------------------------------------------------
2902 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2903 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2904 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2905 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2906 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2907 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2908 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2909 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2910 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2911 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2912 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2913 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2914 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2915 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2916 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2917 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2918 ----------------------------------------------------------------------
2920 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2921 of dimension 1, chars 94, and final character <F>, etc...
2923 Note (*): Although these designations are not allowed in ISO2022,
2924 Emacs accepts them on decoding, and produces them on encoding
2925 CHARS96 character sets in a coding system which is characterized as
2926 7-bit environment, non-locking-shift, and non-single-shift.
2928 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2929 '(' must be omitted. We refer to this as "short-form" hereafter.
2931 Now you may notice that there are a lot of ways of encoding the
2932 same multilingual text in ISO2022. Actually, there exist many
2933 coding systems such as Compound Text (used in X11's inter client
2934 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2935 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2936 localized platforms), and all of these are variants of ISO2022.
2938 In addition to the above, Emacs handles two more kinds of escape
2939 sequences: ISO6429's direction specification and Emacs' private
2940 sequence for specifying character composition.
2942 ISO6429's direction specification takes the following form:
2943 o CSI ']' -- end of the current direction
2944 o CSI '0' ']' -- end of the current direction
2945 o CSI '1' ']' -- start of left-to-right text
2946 o CSI '2' ']' -- start of right-to-left text
2947 The control character CSI (0x9B: control sequence introducer) is
2948 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2950 Character composition specification takes the following form:
2951 o ESC '0' -- start relative composition
2952 o ESC '1' -- end composition
2953 o ESC '2' -- start rule-base composition (*)
2954 o ESC '3' -- start relative composition with alternate chars (**)
2955 o ESC '4' -- start rule-base composition with alternate chars (**)
2956 Since these are not standard escape sequences of any ISO standard,
2957 the use of them with these meanings is restricted to Emacs only.
2959 (*) This form is used only in Emacs 20.7 and older versions,
2960 but newer versions can safely decode it.
2961 (**) This form is used only in Emacs 21.1 and newer versions,
2962 and older versions can't decode it.
2964 Here's a list of example usages of these composition escape
2965 sequences (categorized by `enum composition_method').
2967 COMPOSITION_RELATIVE:
2968 ESC 0 CHAR [ CHAR ] ESC 1
2969 COMPOSITION_WITH_RULE:
2970 ESC 2 CHAR [ RULE CHAR ] ESC 1
2971 COMPOSITION_WITH_ALTCHARS:
2972 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2973 COMPOSITION_WITH_RULE_ALTCHARS:
2974 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2976 enum iso_code_class_type iso_code_class
[256];
2978 #define SAFE_CHARSET_P(coding, id) \
2979 ((id) <= (coding)->max_charset_id \
2980 && (coding)->safe_charsets[id] != 255)
2983 #define SHIFT_OUT_OK(category) \
2984 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2987 setup_iso_safe_charsets (attrs
)
2990 Lisp_Object charset_list
, safe_charsets
;
2991 Lisp_Object request
;
2992 Lisp_Object reg_usage
;
2995 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2998 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2999 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
3000 && ! EQ (charset_list
, Viso_2022_charset_list
))
3002 CODING_ATTR_CHARSET_LIST (attrs
)
3003 = charset_list
= Viso_2022_charset_list
;
3004 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
3007 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
3011 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
3013 int id
= XINT (XCAR (tail
));
3014 if (max_charset_id
< id
)
3015 max_charset_id
= id
;
3018 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
3019 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
3020 request
= AREF (attrs
, coding_attr_iso_request
);
3021 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
3022 reg94
= XINT (XCAR (reg_usage
));
3023 reg96
= XINT (XCDR (reg_usage
));
3025 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
3029 struct charset
*charset
;
3032 charset
= CHARSET_FROM_ID (XINT (id
));
3033 reg
= Fcdr (Fassq (id
, request
));
3035 SSET (safe_charsets
, XINT (id
), XINT (reg
));
3036 else if (charset
->iso_chars_96
)
3039 SSET (safe_charsets
, XINT (id
), reg96
);
3044 SSET (safe_charsets
, XINT (id
), reg94
);
3047 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
3051 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3052 Check if a text is encoded in one of ISO-2022 based codig systems.
3053 If it is, return 1, else return 0. */
3056 detect_coding_iso_2022 (coding
, detect_info
)
3057 struct coding_system
*coding
;
3058 struct coding_detection_info
*detect_info
;
3060 const unsigned char *src
= coding
->source
, *src_base
= src
;
3061 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3062 int multibytep
= coding
->src_multibyte
;
3063 int single_shifting
= 0;
3066 int consumed_chars
= 0;
3070 int composition_count
= -1;
3072 detect_info
->checked
|= CATEGORY_MASK_ISO
;
3074 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
3076 struct coding_system
*this = &(coding_categories
[i
]);
3077 Lisp_Object attrs
, val
;
3081 attrs
= CODING_ID_ATTRS (this->id
);
3082 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
3083 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
3084 setup_iso_safe_charsets (attrs
);
3085 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
3086 this->max_charset_id
= SCHARS (val
) - 1;
3087 this->safe_charsets
= SDATA (val
);
3090 /* A coding system of this category is always ASCII compatible. */
3091 src
+= coding
->head_ascii
;
3093 while (rejected
!= CATEGORY_MASK_ISO
)
3100 if (inhibit_iso_escape_detection
)
3102 single_shifting
= 0;
3104 if (c
>= '(' && c
<= '/')
3106 /* Designation sequence for a charset of dimension 1. */
3108 if (c1
< ' ' || c1
>= 0x80
3109 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3110 /* Invalid designation sequence. Just ignore. */
3115 /* Designation sequence for a charset of dimension 2. */
3117 if (c
>= '@' && c
<= 'B')
3118 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3119 id
= iso_charset_table
[1][0][c
];
3120 else if (c
>= '(' && c
<= '/')
3123 if (c1
< ' ' || c1
>= 0x80
3124 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3125 /* Invalid designation sequence. Just ignore. */
3129 /* Invalid designation sequence. Just ignore it. */
3132 else if (c
== 'N' || c
== 'O')
3134 /* ESC <Fe> for SS2 or SS3. */
3135 single_shifting
= 1;
3136 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3141 /* End of composition. */
3142 if (composition_count
< 0
3143 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3146 composition_count
= -1;
3147 found
|= CATEGORY_MASK_ISO
;
3149 else if (c
>= '0' && c
<= '4')
3151 /* ESC <Fp> for start/end composition. */
3152 composition_count
= 0;
3157 /* Invalid escape sequence. Just ignore it. */
3161 /* We found a valid designation sequence for CHARSET. */
3162 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3163 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3165 found
|= CATEGORY_MASK_ISO_7
;
3167 rejected
|= CATEGORY_MASK_ISO_7
;
3168 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3170 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3172 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3173 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3175 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3177 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3178 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3180 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3182 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3187 /* Locking shift out/in. */
3188 if (inhibit_iso_escape_detection
)
3190 single_shifting
= 0;
3191 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3195 /* Control sequence introducer. */
3196 single_shifting
= 0;
3197 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3198 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3199 goto check_extra_latin
;
3204 if (inhibit_iso_escape_detection
)
3206 single_shifting
= 0;
3207 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3208 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3209 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3210 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
3211 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3212 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3213 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
3214 if (single_shifting
)
3216 goto check_extra_latin
;
3223 if (composition_count
>= 0)
3224 composition_count
++;
3225 single_shifting
= 0;
3230 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3231 found
|= CATEGORY_MASK_ISO_8_1
;
3232 /* Check the length of succeeding codes of the range
3233 0xA0..0FF. If the byte length is even, we include
3234 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3235 only when we are not single shifting. */
3236 if (! single_shifting
3237 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3240 while (src
< src_end
)
3248 if (i
& 1 && src
< src_end
)
3250 rejected
|= CATEGORY_MASK_ISO_8_2
;
3251 if (composition_count
>= 0)
3252 composition_count
+= i
;
3256 found
|= CATEGORY_MASK_ISO_8_2
;
3257 if (composition_count
>= 0)
3258 composition_count
+= i
/ 2;
3264 single_shifting
= 0;
3265 if (! VECTORP (Vlatin_extra_code_table
)
3266 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3268 rejected
= CATEGORY_MASK_ISO
;
3271 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3272 & CODING_ISO_FLAG_LATIN_EXTRA
)
3273 found
|= CATEGORY_MASK_ISO_8_1
;
3275 rejected
|= CATEGORY_MASK_ISO_8_1
;
3276 rejected
|= CATEGORY_MASK_ISO_8_2
;
3279 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3283 detect_info
->rejected
|= rejected
;
3284 detect_info
->found
|= (found
& ~rejected
);
3289 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3290 escape sequence should be kept. */
3291 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3295 if (final < '0' || final >= 128 \
3296 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3297 || !SAFE_CHARSET_P (coding, id)) \
3299 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3303 prev = CODING_ISO_DESIGNATION (coding, reg); \
3304 if (id == charset_jisx0201_roman) \
3306 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3307 id = charset_ascii; \
3309 else if (id == charset_jisx0208_1978) \
3311 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3312 id = charset_jisx0208; \
3314 CODING_ISO_DESIGNATION (coding, reg) = id; \
3315 /* If there was an invalid designation to REG previously, and this \
3316 designation is ASCII to REG, we should keep this designation \
3318 if (prev == -2 && id == charset_ascii) \
3323 /* Handle these composition sequence (ALT: alternate char):
3325 (1) relative composition: ESC 0 CHAR ... ESC 1
3326 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3327 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3328 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3330 When the start sequence (ESC 0/2/3/4) is found, this annotation
3333 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3335 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3336 produced until the end sequence (ESC 1) is found:
3339 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3340 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3341 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3343 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3344 annotation header is updated as below:
3346 (1) LENGTH: unchanged, NCHARS: number of CHARs
3347 (2) LENGTH: unchanged, NCHARS: number of CHARs
3348 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3349 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3351 If an error is found while composing, the annotation header is
3354 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3356 and the sequence [ -2 DECODED-RULE ] is changed to the original
3357 byte sequence as below:
3358 o the original byte sequence is B: [ B -1 ]
3359 o the original byte sequence is B1 B2: [ B1 B2 ]
3360 and the sequence [ -1 -1 ] is changed to the original byte
3365 /* Decode a composition rule C1 and maybe one more byte from the
3366 source, and set RULE to the encoded composition rule, NBYTES to the
3367 length of the composition rule. If the rule is invalid, set RULE
3368 to some negative value. */
3370 #define DECODE_COMPOSITION_RULE(rule, nbytes) \
3375 if (rule < 81) /* old format (before ver.21) */ \
3377 int gref = (rule) / 9; \
3378 int nref = (rule) % 9; \
3379 if (gref == 4) gref = 10; \
3380 if (nref == 4) nref = 10; \
3381 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3384 else /* new format (after ver.21) */ \
3388 ONE_MORE_BYTE (c); \
3389 rule = COMPOSITION_ENCODE_RULE (rule - 81, c - 32); \
3391 rule += 0x100; /* to destinguish it from the old format */ \
3396 #define ENCODE_COMPOSITION_RULE(rule) \
3398 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3400 if (rule < 0x100) /* old format */ \
3402 if (gref == 10) gref = 4; \
3403 if (nref == 10) nref = 4; \
3404 charbuf[idx] = 32 + gref * 9 + nref; \
3405 charbuf[idx + 1] = -1; \
3408 else /* new format */ \
3410 charbuf[idx] = 32 + 81 + gref; \
3411 charbuf[idx + 1] = 32 + nref; \
3416 /* Finish the current composition as invalid. */
3418 static int finish_composition
P_ ((int *, struct composition_status
*));
3421 finish_composition (charbuf
, cmp_status
)
3423 struct composition_status
*cmp_status
;
3425 int idx
= - cmp_status
->length
;
3428 /* Recover the original ESC sequence */
3429 charbuf
[idx
++] = ISO_CODE_ESC
;
3430 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3431 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3432 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3433 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3435 charbuf
[idx
++] = -2;
3437 charbuf
[idx
++] = -1;
3438 new_chars
= cmp_status
->nchars
;
3439 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3440 for (; idx
< 0; idx
++)
3442 int elt
= charbuf
[idx
];
3446 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3451 charbuf
[idx
++] = ISO_CODE_ESC
;
3456 cmp_status
->state
= COMPOSING_NO
;
3460 /* If characers are under composition, finish the composition. */
3461 #define MAYBE_FINISH_COMPOSITION() \
3463 if (cmp_status->state != COMPOSING_NO) \
3464 char_offset += finish_composition (charbuf, cmp_status); \
3467 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3469 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3470 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3471 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3472 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3474 Produce this annotation sequence now:
3476 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3479 #define DECODE_COMPOSITION_START(c1) \
3482 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3483 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3484 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3485 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3489 cmp_status->state = COMPOSING_CHAR; \
3490 cmp_status->length += 2; \
3494 MAYBE_FINISH_COMPOSITION (); \
3495 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3496 : c1 == '2' ? COMPOSITION_WITH_RULE \
3497 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3498 : COMPOSITION_WITH_RULE_ALTCHARS); \
3500 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3501 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3502 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3503 cmp_status->nchars = cmp_status->ncomps = 0; \
3504 coding->annotated = 1; \
3509 /* Handle composition end sequence ESC 1. */
3511 #define DECODE_COMPOSITION_END() \
3513 if (cmp_status->nchars == 0 \
3514 || ((cmp_status->state == COMPOSING_CHAR) \
3515 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3517 MAYBE_FINISH_COMPOSITION (); \
3518 goto invalid_code; \
3520 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3521 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3522 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3523 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3524 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3525 char_offset += cmp_status->nchars; \
3526 cmp_status->state = COMPOSING_NO; \
3529 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3531 #define STORE_COMPOSITION_RULE(rule) \
3534 *charbuf++ = rule; \
3535 cmp_status->length += 2; \
3536 cmp_status->state--; \
3539 /* Store a composed char or a component char C in charbuf, and update
3542 #define STORE_COMPOSITION_CHAR(c) \
3545 cmp_status->length++; \
3546 if (cmp_status->state == COMPOSING_CHAR) \
3547 cmp_status->nchars++; \
3549 cmp_status->ncomps++; \
3550 if (cmp_status->method == COMPOSITION_WITH_RULE \
3551 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3552 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3553 cmp_status->state++; \
3557 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3560 decode_coding_iso_2022 (coding
)
3561 struct coding_system
*coding
;
3563 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3564 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3565 const unsigned char *src_base
;
3566 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3567 /* We may produce two annocations (charset and composition) in one
3568 loop and one more charset annocation at the end. */
3570 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3571 int consumed_chars
= 0, consumed_chars_base
;
3572 int multibytep
= coding
->src_multibyte
;
3573 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3574 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3575 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3576 int charset_id_2
, charset_id_3
;
3577 struct charset
*charset
;
3579 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3580 Lisp_Object attrs
, charset_list
;
3581 int char_offset
= coding
->produced_char
;
3582 int last_offset
= char_offset
;
3583 int last_id
= charset_ascii
;
3585 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3586 int byte_after_cr
= -1;
3589 CODING_GET_INFO (coding
, attrs
, charset_list
);
3590 setup_iso_safe_charsets (attrs
);
3591 /* Charset list may have been changed. */
3592 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3593 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3595 if (cmp_status
->state
!= COMPOSING_NO
)
3597 for (i
= 0; i
< cmp_status
->length
; i
++)
3598 *charbuf
++ = cmp_status
->carryover
[i
];
3599 coding
->annotated
= 1;
3607 consumed_chars_base
= consumed_chars
;
3609 if (charbuf
>= charbuf_end
)
3611 if (byte_after_cr
>= 0)
3616 if (byte_after_cr
>= 0)
3617 c1
= byte_after_cr
, byte_after_cr
= -1;
3623 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3625 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3627 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3631 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3633 if (c1
== ISO_CODE_ESC
)
3635 if (src
+ 1 >= src_end
)
3636 goto no_more_source
;
3637 *charbuf
++ = ISO_CODE_ESC
;
3639 if (src
[0] == '%' && src
[1] == '@')
3642 consumed_chars
+= 2;
3644 /* We are sure charbuf can contain two more chars. */
3647 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3652 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3658 if ((cmp_status
->state
== COMPOSING_RULE
3659 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3660 && c1
!= ISO_CODE_ESC
)
3664 DECODE_COMPOSITION_RULE (rule
, nbytes
);
3667 STORE_COMPOSITION_RULE (rule
);
3671 /* We produce at most one character. */
3672 switch (iso_code_class
[c1
])
3674 case ISO_0x20_or_0x7F
:
3675 if (charset_id_0
< 0
3676 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3677 /* This is SPACE or DEL. */
3678 charset
= CHARSET_FROM_ID (charset_ascii
);
3680 charset
= CHARSET_FROM_ID (charset_id_0
);
3683 case ISO_graphic_plane_0
:
3684 if (charset_id_0
< 0)
3685 charset
= CHARSET_FROM_ID (charset_ascii
);
3687 charset
= CHARSET_FROM_ID (charset_id_0
);
3690 case ISO_0xA0_or_0xFF
:
3691 if (charset_id_1
< 0
3692 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3693 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3695 /* This is a graphic character, we fall down ... */
3697 case ISO_graphic_plane_1
:
3698 if (charset_id_1
< 0)
3700 charset
= CHARSET_FROM_ID (charset_id_1
);
3704 if (eol_crlf
&& c1
== '\r')
3705 ONE_MORE_BYTE (byte_after_cr
);
3706 MAYBE_FINISH_COMPOSITION ();
3707 charset
= CHARSET_FROM_ID (charset_ascii
);
3714 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3715 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3717 CODING_ISO_INVOCATION (coding
, 0) = 1;
3718 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3722 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3724 CODING_ISO_INVOCATION (coding
, 0) = 0;
3725 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3728 case ISO_single_shift_2_7
:
3729 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3731 case ISO_single_shift_2
:
3732 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3734 /* SS2 is handled as an escape sequence of ESC 'N' */
3736 goto label_escape_sequence
;
3738 case ISO_single_shift_3
:
3739 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3741 /* SS2 is handled as an escape sequence of ESC 'O' */
3743 goto label_escape_sequence
;
3745 case ISO_control_sequence_introducer
:
3746 /* CSI is handled as an escape sequence of ESC '[' ... */
3748 goto label_escape_sequence
;
3752 label_escape_sequence
:
3753 /* Escape sequences handled here are invocation,
3754 designation, direction specification, and character
3755 composition specification. */
3758 case '&': /* revision of following character set */
3760 if (!(c1
>= '@' && c1
<= '~'))
3763 if (c1
!= ISO_CODE_ESC
)
3766 goto label_escape_sequence
;
3768 case '$': /* designation of 2-byte character set */
3769 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3775 if (c1
>= '@' && c1
<= 'B')
3776 { /* designation of JISX0208.1978, GB2312.1980,
3778 reg
= 0, chars96
= 0;
3780 else if (c1
>= 0x28 && c1
<= 0x2B)
3781 { /* designation of DIMENSION2_CHARS94 character set */
3782 reg
= c1
- 0x28, chars96
= 0;
3785 else if (c1
>= 0x2C && c1
<= 0x2F)
3786 { /* designation of DIMENSION2_CHARS96 character set */
3787 reg
= c1
- 0x2C, chars96
= 1;
3792 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3793 /* We must update these variables now. */
3795 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3797 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3803 case 'n': /* invocation of locking-shift-2 */
3804 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3805 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3807 CODING_ISO_INVOCATION (coding
, 0) = 2;
3808 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3811 case 'o': /* invocation of locking-shift-3 */
3812 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3813 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3815 CODING_ISO_INVOCATION (coding
, 0) = 3;
3816 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3819 case 'N': /* invocation of single-shift-2 */
3820 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3821 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3823 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3824 if (charset_id_2
< 0)
3825 charset
= CHARSET_FROM_ID (charset_ascii
);
3827 charset
= CHARSET_FROM_ID (charset_id_2
);
3829 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3833 case 'O': /* invocation of single-shift-3 */
3834 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3835 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3837 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3838 if (charset_id_3
< 0)
3839 charset
= CHARSET_FROM_ID (charset_ascii
);
3841 charset
= CHARSET_FROM_ID (charset_id_3
);
3843 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3847 case '0': case '2': case '3': case '4': /* start composition */
3848 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3850 if (last_id
!= charset_ascii
)
3852 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3853 last_id
= charset_ascii
;
3854 last_offset
= char_offset
;
3856 DECODE_COMPOSITION_START (c1
);
3859 case '1': /* end composition */
3860 if (cmp_status
->state
== COMPOSING_NO
)
3862 DECODE_COMPOSITION_END ();
3865 case '[': /* specification of direction */
3866 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3868 /* For the moment, nested direction is not supported.
3869 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3870 left-to-right, and nozero means right-to-left. */
3874 case ']': /* end of the current direction */
3875 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3877 case '0': /* end of the current direction */
3878 case '1': /* start of left-to-right direction */
3881 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3886 case '2': /* start of right-to-left direction */
3889 coding
->mode
|= CODING_MODE_DIRECTION
;
3903 /* CTEXT extended segment:
3904 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3905 We keep these bytes as is for the moment.
3906 They may be decoded by post-read-conversion. */
3910 ONE_MORE_BYTE (dim
);
3911 if (dim
< 0 || dim
> 4)
3919 size
= ((M
- 128) * 128) + (L
- 128);
3920 if (charbuf
+ 6 > charbuf_end
)
3922 *charbuf
++ = ISO_CODE_ESC
;
3926 *charbuf
++ = BYTE8_TO_CHAR (M
);
3927 *charbuf
++ = BYTE8_TO_CHAR (L
);
3928 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3932 /* XFree86 extension for embedding UTF-8 in CTEXT:
3933 ESC % G --UTF-8-BYTES-- ESC % @
3934 We keep these bytes as is for the moment.
3935 They may be decoded by post-read-conversion. */
3936 if (charbuf
+ 3 > charbuf_end
)
3938 *charbuf
++ = ISO_CODE_ESC
;
3941 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3949 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3954 if (c1
>= 0x28 && c1
<= 0x2B)
3955 { /* designation of DIMENSION1_CHARS94 character set */
3956 reg
= c1
- 0x28, chars96
= 0;
3959 else if (c1
>= 0x2C && c1
<= 0x2F)
3960 { /* designation of DIMENSION1_CHARS96 character set */
3961 reg
= c1
- 0x2C, chars96
= 1;
3966 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3967 /* We must update these variables now. */
3969 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3971 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3979 if (cmp_status
->state
== COMPOSING_NO
3980 && charset
->id
!= charset_ascii
3981 && last_id
!= charset
->id
)
3983 if (last_id
!= charset_ascii
)
3984 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3985 last_id
= charset
->id
;
3986 last_offset
= char_offset
;
3989 /* Now we know CHARSET and 1st position code C1 of a character.
3990 Produce a decoded character while getting 2nd and 3rd
3991 position codes C2, C3 if necessary. */
3992 if (CHARSET_DIMENSION (charset
) > 1)
3995 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3996 || ((c1
& 0x80) != (c2
& 0x80)))
3997 /* C2 is not in a valid range. */
3999 if (CHARSET_DIMENSION (charset
) == 2)
4000 c1
= (c1
<< 8) | c2
;
4004 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
4005 || ((c1
& 0x80) != (c3
& 0x80)))
4006 /* C3 is not in a valid range. */
4008 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
4012 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
4015 MAYBE_FINISH_COMPOSITION ();
4016 for (; src_base
< src
; src_base
++, char_offset
++)
4018 if (ASCII_BYTE_P (*src_base
))
4019 *charbuf
++ = *src_base
;
4021 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
4024 else if (cmp_status
->state
== COMPOSING_NO
)
4029 else if ((cmp_status
->state
== COMPOSING_CHAR
4030 ? cmp_status
->nchars
4031 : cmp_status
->ncomps
)
4032 >= MAX_COMPOSITION_COMPONENTS
)
4034 /* Too long composition. */
4035 MAYBE_FINISH_COMPOSITION ();
4040 STORE_COMPOSITION_CHAR (c
);
4044 MAYBE_FINISH_COMPOSITION ();
4046 consumed_chars
= consumed_chars_base
;
4048 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4058 if (cmp_status
->state
!= COMPOSING_NO
)
4060 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
4061 MAYBE_FINISH_COMPOSITION ();
4064 charbuf
-= cmp_status
->length
;
4065 for (i
= 0; i
< cmp_status
->length
; i
++)
4066 cmp_status
->carryover
[i
] = charbuf
[i
];
4069 else if (last_id
!= charset_ascii
)
4070 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4071 coding
->consumed_char
+= consumed_chars_base
;
4072 coding
->consumed
= src_base
- coding
->source
;
4073 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4077 /* ISO2022 encoding stuff. */
4080 It is not enough to say just "ISO2022" on encoding, we have to
4081 specify more details. In Emacs, each coding system of ISO2022
4082 variant has the following specifications:
4083 1. Initial designation to G0 thru G3.
4084 2. Allows short-form designation?
4085 3. ASCII should be designated to G0 before control characters?
4086 4. ASCII should be designated to G0 at end of line?
4087 5. 7-bit environment or 8-bit environment?
4088 6. Use locking-shift?
4089 7. Use Single-shift?
4090 And the following two are only for Japanese:
4091 8. Use ASCII in place of JIS0201-1976-Roman?
4092 9. Use JISX0208-1983 in place of JISX0208-1978?
4093 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4094 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4098 /* Produce codes (escape sequence) for designating CHARSET to graphic
4099 register REG at DST, and increment DST. If <final-char> of CHARSET is
4100 '@', 'A', or 'B' and the coding system CODING allows, produce
4101 designation sequence of short-form. */
4103 #define ENCODE_DESIGNATION(charset, reg, coding) \
4105 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4106 char *intermediate_char_94 = "()*+"; \
4107 char *intermediate_char_96 = ",-./"; \
4108 int revision = -1; \
4111 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4112 revision = CHARSET_ISO_REVISION (charset); \
4114 if (revision >= 0) \
4116 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4117 EMIT_ONE_BYTE ('@' + revision); \
4119 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4120 if (CHARSET_DIMENSION (charset) == 1) \
4122 if (! CHARSET_ISO_CHARS_96 (charset)) \
4123 c = intermediate_char_94[reg]; \
4125 c = intermediate_char_96[reg]; \
4126 EMIT_ONE_ASCII_BYTE (c); \
4130 EMIT_ONE_ASCII_BYTE ('$'); \
4131 if (! CHARSET_ISO_CHARS_96 (charset)) \
4133 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4135 || final_char < '@' || final_char > 'B') \
4136 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4139 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4141 EMIT_ONE_ASCII_BYTE (final_char); \
4143 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4147 /* The following two macros produce codes (control character or escape
4148 sequence) for ISO2022 single-shift functions (single-shift-2 and
4151 #define ENCODE_SINGLE_SHIFT_2 \
4153 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4154 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4156 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4157 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4161 #define ENCODE_SINGLE_SHIFT_3 \
4163 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4164 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4166 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4167 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4171 /* The following four macros produce codes (control character or
4172 escape sequence) for ISO2022 locking-shift functions (shift-in,
4173 shift-out, locking-shift-2, and locking-shift-3). */
4175 #define ENCODE_SHIFT_IN \
4177 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4178 CODING_ISO_INVOCATION (coding, 0) = 0; \
4182 #define ENCODE_SHIFT_OUT \
4184 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4185 CODING_ISO_INVOCATION (coding, 0) = 1; \
4189 #define ENCODE_LOCKING_SHIFT_2 \
4191 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4192 CODING_ISO_INVOCATION (coding, 0) = 2; \
4196 #define ENCODE_LOCKING_SHIFT_3 \
4198 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4199 CODING_ISO_INVOCATION (coding, 0) = 3; \
4203 /* Produce codes for a DIMENSION1 character whose character set is
4204 CHARSET and whose position-code is C1. Designation and invocation
4205 sequences are also produced in advance if necessary. */
4207 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4209 int id = CHARSET_ID (charset); \
4211 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4212 && id == charset_ascii) \
4214 id = charset_jisx0201_roman; \
4215 charset = CHARSET_FROM_ID (id); \
4218 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4220 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4221 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4223 EMIT_ONE_BYTE (c1 | 0x80); \
4224 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4227 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4229 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4232 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4234 EMIT_ONE_BYTE (c1 | 0x80); \
4238 /* Since CHARSET is not yet invoked to any graphic planes, we \
4239 must invoke it, or, at first, designate it to some graphic \
4240 register. Then repeat the loop to actually produce the \
4242 dst = encode_invocation_designation (charset, coding, dst, \
4247 /* Produce codes for a DIMENSION2 character whose character set is
4248 CHARSET and whose position-codes are C1 and C2. Designation and
4249 invocation codes are also produced in advance if necessary. */
4251 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4253 int id = CHARSET_ID (charset); \
4255 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4256 && id == charset_jisx0208) \
4258 id = charset_jisx0208_1978; \
4259 charset = CHARSET_FROM_ID (id); \
4262 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4264 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4265 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4267 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4268 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4271 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4273 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4276 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4278 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4282 /* Since CHARSET is not yet invoked to any graphic planes, we \
4283 must invoke it, or, at first, designate it to some graphic \
4284 register. Then repeat the loop to actually produce the \
4286 dst = encode_invocation_designation (charset, coding, dst, \
4291 #define ENCODE_ISO_CHARACTER(charset, c) \
4293 int code = ENCODE_CHAR ((charset),(c)); \
4295 if (CHARSET_DIMENSION (charset) == 1) \
4296 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4298 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4302 /* Produce designation and invocation codes at a place pointed by DST
4303 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4307 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
4308 struct charset
*charset
;
4309 struct coding_system
*coding
;
4313 int multibytep
= coding
->dst_multibyte
;
4314 int produced_chars
= *p_nchars
;
4315 int reg
; /* graphic register number */
4316 int id
= CHARSET_ID (charset
);
4318 /* At first, check designations. */
4319 for (reg
= 0; reg
< 4; reg
++)
4320 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4325 /* CHARSET is not yet designated to any graphic registers. */
4326 /* At first check the requested designation. */
4327 reg
= CODING_ISO_REQUEST (coding
, id
);
4329 /* Since CHARSET requests no special designation, designate it
4330 to graphic register 0. */
4333 ENCODE_DESIGNATION (charset
, reg
, coding
);
4336 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4337 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4339 /* Since the graphic register REG is not invoked to any graphic
4340 planes, invoke it to graphic plane 0. */
4343 case 0: /* graphic register 0 */
4347 case 1: /* graphic register 1 */
4351 case 2: /* graphic register 2 */
4352 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4353 ENCODE_SINGLE_SHIFT_2
;
4355 ENCODE_LOCKING_SHIFT_2
;
4358 case 3: /* graphic register 3 */
4359 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4360 ENCODE_SINGLE_SHIFT_3
;
4362 ENCODE_LOCKING_SHIFT_3
;
4367 *p_nchars
= produced_chars
;
4371 /* The following three macros produce codes for indicating direction
4373 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
4375 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
4376 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
4378 EMIT_ONE_BYTE (ISO_CODE_CSI); \
4382 #define ENCODE_DIRECTION_R2L() \
4384 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4385 EMIT_TWO_ASCII_BYTES ('2', ']'); \
4389 #define ENCODE_DIRECTION_L2R() \
4391 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
4392 EMIT_TWO_ASCII_BYTES ('0', ']'); \
4396 /* Produce codes for designation and invocation to reset the graphic
4397 planes and registers to initial state. */
4398 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4401 struct charset *charset; \
4403 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4405 for (reg = 0; reg < 4; reg++) \
4406 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4407 && (CODING_ISO_DESIGNATION (coding, reg) \
4408 != CODING_ISO_INITIAL (coding, reg))) \
4410 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4411 ENCODE_DESIGNATION (charset, reg, coding); \
4416 /* Produce designation sequences of charsets in the line started from
4417 SRC to a place pointed by DST, and return updated DST.
4419 If the current block ends before any end-of-line, we may fail to
4420 find all the necessary designations. */
4422 static unsigned char *
4423 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
4424 struct coding_system
*coding
;
4425 int *charbuf
, *charbuf_end
;
4428 struct charset
*charset
;
4429 /* Table of charsets to be designated to each graphic register. */
4431 int c
, found
= 0, reg
;
4432 int produced_chars
= 0;
4433 int multibytep
= coding
->dst_multibyte
;
4435 Lisp_Object charset_list
;
4437 attrs
= CODING_ID_ATTRS (coding
->id
);
4438 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4439 if (EQ (charset_list
, Qiso_2022
))
4440 charset_list
= Viso_2022_charset_list
;
4442 for (reg
= 0; reg
< 4; reg
++)
4452 charset
= char_charset (c
, charset_list
, NULL
);
4453 id
= CHARSET_ID (charset
);
4454 reg
= CODING_ISO_REQUEST (coding
, id
);
4455 if (reg
>= 0 && r
[reg
] < 0)
4464 for (reg
= 0; reg
< 4; reg
++)
4466 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4467 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4473 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4476 encode_coding_iso_2022 (coding
)
4477 struct coding_system
*coding
;
4479 int multibytep
= coding
->dst_multibyte
;
4480 int *charbuf
= coding
->charbuf
;
4481 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4482 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4483 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4486 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4487 && CODING_ISO_BOL (coding
));
4488 int produced_chars
= 0;
4489 Lisp_Object attrs
, eol_type
, charset_list
;
4490 int ascii_compatible
;
4492 int preferred_charset_id
= -1;
4494 CODING_GET_INFO (coding
, attrs
, charset_list
);
4495 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4496 if (VECTORP (eol_type
))
4499 setup_iso_safe_charsets (attrs
);
4500 /* Charset list may have been changed. */
4501 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4502 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4504 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4506 while (charbuf
< charbuf_end
)
4508 ASSURE_DESTINATION (safe_room
);
4510 if (bol_designation
)
4512 unsigned char *dst_prev
= dst
;
4514 /* We have to produce designation sequences if any now. */
4515 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
4516 bol_designation
= 0;
4517 /* We are sure that designation sequences are all ASCII bytes. */
4518 produced_chars
+= dst
- dst_prev
;
4525 /* Handle an annotation. */
4528 case CODING_ANNOTATE_COMPOSITION_MASK
:
4529 /* Not yet implemented. */
4531 case CODING_ANNOTATE_CHARSET_MASK
:
4532 preferred_charset_id
= charbuf
[2];
4533 if (preferred_charset_id
>= 0
4534 && NILP (Fmemq (make_number (preferred_charset_id
),
4536 preferred_charset_id
= -1;
4545 /* Now encode the character C. */
4546 if (c
< 0x20 || c
== 0x7F)
4549 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4551 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4552 ENCODE_RESET_PLANE_AND_REGISTER ();
4553 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4557 for (i
= 0; i
< 4; i
++)
4558 CODING_ISO_DESIGNATION (coding
, i
)
4559 = CODING_ISO_INITIAL (coding
, i
);
4562 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4564 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4565 ENCODE_RESET_PLANE_AND_REGISTER ();
4566 EMIT_ONE_ASCII_BYTE (c
);
4568 else if (ASCII_CHAR_P (c
))
4570 if (ascii_compatible
)
4571 EMIT_ONE_ASCII_BYTE (c
);
4574 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4575 ENCODE_ISO_CHARACTER (charset
, c
);
4578 else if (CHAR_BYTE8_P (c
))
4580 c
= CHAR_TO_BYTE8 (c
);
4585 struct charset
*charset
;
4587 if (preferred_charset_id
>= 0)
4589 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4590 if (! CHAR_CHARSET_P (c
, charset
))
4591 charset
= char_charset (c
, charset_list
, NULL
);
4594 charset
= char_charset (c
, charset_list
, NULL
);
4597 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4599 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4600 charset
= CHARSET_FROM_ID (charset_ascii
);
4604 c
= coding
->default_char
;
4605 charset
= char_charset (c
, charset_list
, NULL
);
4608 ENCODE_ISO_CHARACTER (charset
, c
);
4612 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4613 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4615 ASSURE_DESTINATION (safe_room
);
4616 ENCODE_RESET_PLANE_AND_REGISTER ();
4618 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4619 CODING_ISO_BOL (coding
) = bol_designation
;
4620 coding
->produced_char
+= produced_chars
;
4621 coding
->produced
= dst
- coding
->destination
;
4626 /*** 8,9. SJIS and BIG5 handlers ***/
4628 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4629 quite widely. So, for the moment, Emacs supports them in the bare
4630 C code. But, in the future, they may be supported only by CCL. */
4632 /* SJIS is a coding system encoding three character sets: ASCII, right
4633 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4634 as is. A character of charset katakana-jisx0201 is encoded by
4635 "position-code + 0x80". A character of charset japanese-jisx0208
4636 is encoded in 2-byte but two position-codes are divided and shifted
4637 so that it fit in the range below.
4639 --- CODE RANGE of SJIS ---
4640 (character set) (range)
4642 KATAKANA-JISX0201 0xA0 .. 0xDF
4643 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4644 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4645 -------------------------------
4649 /* BIG5 is a coding system encoding two character sets: ASCII and
4650 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4651 character set and is encoded in two-byte.
4653 --- CODE RANGE of BIG5 ---
4654 (character set) (range)
4656 Big5 (1st byte) 0xA1 .. 0xFE
4657 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4658 --------------------------
4662 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4663 Check if a text is encoded in SJIS. If it is, return
4664 CATEGORY_MASK_SJIS, else return 0. */
4667 detect_coding_sjis (coding
, detect_info
)
4668 struct coding_system
*coding
;
4669 struct coding_detection_info
*detect_info
;
4671 const unsigned char *src
= coding
->source
, *src_base
;
4672 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4673 int multibytep
= coding
->src_multibyte
;
4674 int consumed_chars
= 0;
4677 Lisp_Object attrs
, charset_list
;
4678 int max_first_byte_of_2_byte_code
;
4680 CODING_GET_INFO (coding
, attrs
, charset_list
);
4681 max_first_byte_of_2_byte_code
4682 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4684 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4685 /* A coding system of this category is always ASCII compatible. */
4686 src
+= coding
->head_ascii
;
4694 if ((c
>= 0x81 && c
<= 0x9F)
4695 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4698 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4700 found
= CATEGORY_MASK_SJIS
;
4702 else if (c
>= 0xA0 && c
< 0xE0)
4703 found
= CATEGORY_MASK_SJIS
;
4707 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4711 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4713 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4716 detect_info
->found
|= found
;
4720 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4721 Check if a text is encoded in BIG5. If it is, return
4722 CATEGORY_MASK_BIG5, else return 0. */
4725 detect_coding_big5 (coding
, detect_info
)
4726 struct coding_system
*coding
;
4727 struct coding_detection_info
*detect_info
;
4729 const unsigned char *src
= coding
->source
, *src_base
;
4730 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4731 int multibytep
= coding
->src_multibyte
;
4732 int consumed_chars
= 0;
4736 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4737 /* A coding system of this category is always ASCII compatible. */
4738 src
+= coding
->head_ascii
;
4749 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4751 found
= CATEGORY_MASK_BIG5
;
4756 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4760 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4762 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4765 detect_info
->found
|= found
;
4769 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4770 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4773 decode_coding_sjis (coding
)
4774 struct coding_system
*coding
;
4776 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4777 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4778 const unsigned char *src_base
;
4779 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4780 /* We may produce one charset annocation in one loop and one more at
4783 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4784 int consumed_chars
= 0, consumed_chars_base
;
4785 int multibytep
= coding
->src_multibyte
;
4786 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4787 struct charset
*charset_kanji2
;
4788 Lisp_Object attrs
, charset_list
, val
;
4789 int char_offset
= coding
->produced_char
;
4790 int last_offset
= char_offset
;
4791 int last_id
= charset_ascii
;
4793 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4794 int byte_after_cr
= -1;
4796 CODING_GET_INFO (coding
, attrs
, charset_list
);
4799 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4800 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4801 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4802 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4807 struct charset
*charset
;
4810 consumed_chars_base
= consumed_chars
;
4812 if (charbuf
>= charbuf_end
)
4814 if (byte_after_cr
>= 0)
4819 if (byte_after_cr
>= 0)
4820 c
= byte_after_cr
, byte_after_cr
= -1;
4827 if (eol_crlf
&& c
== '\r')
4828 ONE_MORE_BYTE (byte_after_cr
);
4829 charset
= charset_roman
;
4831 else if (c
== 0x80 || c
== 0xA0)
4833 else if (c
>= 0xA1 && c
<= 0xDF)
4835 /* SJIS -> JISX0201-Kana */
4837 charset
= charset_kana
;
4841 /* SJIS -> JISX0208 */
4843 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4847 charset
= charset_kanji
;
4849 else if (c
<= 0xFC && charset_kanji2
)
4851 /* SJIS -> JISX0213-2 */
4853 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4857 charset
= charset_kanji2
;
4861 if (charset
->id
!= charset_ascii
4862 && last_id
!= charset
->id
)
4864 if (last_id
!= charset_ascii
)
4865 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4866 last_id
= charset
->id
;
4867 last_offset
= char_offset
;
4869 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4876 consumed_chars
= consumed_chars_base
;
4878 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4884 if (last_id
!= charset_ascii
)
4885 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4886 coding
->consumed_char
+= consumed_chars_base
;
4887 coding
->consumed
= src_base
- coding
->source
;
4888 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4892 decode_coding_big5 (coding
)
4893 struct coding_system
*coding
;
4895 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4896 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4897 const unsigned char *src_base
;
4898 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4899 /* We may produce one charset annocation in one loop and one more at
4902 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4903 int consumed_chars
= 0, consumed_chars_base
;
4904 int multibytep
= coding
->src_multibyte
;
4905 struct charset
*charset_roman
, *charset_big5
;
4906 Lisp_Object attrs
, charset_list
, val
;
4907 int char_offset
= coding
->produced_char
;
4908 int last_offset
= char_offset
;
4909 int last_id
= charset_ascii
;
4911 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4912 int byte_after_cr
= -1;
4914 CODING_GET_INFO (coding
, attrs
, charset_list
);
4916 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4917 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4922 struct charset
*charset
;
4925 consumed_chars_base
= consumed_chars
;
4927 if (charbuf
>= charbuf_end
)
4929 if (byte_after_cr
>= 0)
4934 if (byte_after_cr
>= 0)
4935 c
= byte_after_cr
, byte_after_cr
= -1;
4943 if (eol_crlf
&& c
== '\r')
4944 ONE_MORE_BYTE (byte_after_cr
);
4945 charset
= charset_roman
;
4950 if (c
< 0xA1 || c
> 0xFE)
4953 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4956 charset
= charset_big5
;
4958 if (charset
->id
!= charset_ascii
4959 && last_id
!= charset
->id
)
4961 if (last_id
!= charset_ascii
)
4962 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4963 last_id
= charset
->id
;
4964 last_offset
= char_offset
;
4966 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4973 consumed_chars
= consumed_chars_base
;
4975 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4981 if (last_id
!= charset_ascii
)
4982 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4983 coding
->consumed_char
+= consumed_chars_base
;
4984 coding
->consumed
= src_base
- coding
->source
;
4985 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4988 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4989 This function can encode charsets `ascii', `katakana-jisx0201',
4990 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4991 are sure that all these charsets are registered as official charset
4992 (i.e. do not have extended leading-codes). Characters of other
4993 charsets are produced without any encoding. If SJIS_P is 1, encode
4994 SJIS text, else encode BIG5 text. */
4997 encode_coding_sjis (coding
)
4998 struct coding_system
*coding
;
5000 int multibytep
= coding
->dst_multibyte
;
5001 int *charbuf
= coding
->charbuf
;
5002 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5003 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5004 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5006 int produced_chars
= 0;
5007 Lisp_Object attrs
, charset_list
, val
;
5008 int ascii_compatible
;
5009 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
5010 struct charset
*charset_kanji2
;
5013 CODING_GET_INFO (coding
, attrs
, charset_list
);
5015 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5016 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5017 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5018 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
5020 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5022 while (charbuf
< charbuf_end
)
5024 ASSURE_DESTINATION (safe_room
);
5026 /* Now encode the character C. */
5027 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5028 EMIT_ONE_ASCII_BYTE (c
);
5029 else if (CHAR_BYTE8_P (c
))
5031 c
= CHAR_TO_BYTE8 (c
);
5037 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
5041 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5043 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5044 charset
= CHARSET_FROM_ID (charset_ascii
);
5048 c
= coding
->default_char
;
5049 charset
= char_charset (c
, charset_list
, &code
);
5052 if (code
== CHARSET_INVALID_CODE (charset
))
5054 if (charset
== charset_kanji
)
5058 c1
= code
>> 8, c2
= code
& 0xFF;
5059 EMIT_TWO_BYTES (c1
, c2
);
5061 else if (charset
== charset_kana
)
5062 EMIT_ONE_BYTE (code
| 0x80);
5063 else if (charset_kanji2
&& charset
== charset_kanji2
)
5068 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
5070 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
5072 JIS_TO_SJIS2 (code
);
5073 c1
= code
>> 8, c2
= code
& 0xFF;
5074 EMIT_TWO_BYTES (c1
, c2
);
5077 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5080 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5083 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5084 coding
->produced_char
+= produced_chars
;
5085 coding
->produced
= dst
- coding
->destination
;
5090 encode_coding_big5 (coding
)
5091 struct coding_system
*coding
;
5093 int multibytep
= coding
->dst_multibyte
;
5094 int *charbuf
= coding
->charbuf
;
5095 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5096 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5097 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5099 int produced_chars
= 0;
5100 Lisp_Object attrs
, charset_list
, val
;
5101 int ascii_compatible
;
5102 struct charset
*charset_roman
, *charset_big5
;
5105 CODING_GET_INFO (coding
, attrs
, charset_list
);
5107 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
5108 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5109 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5111 while (charbuf
< charbuf_end
)
5113 ASSURE_DESTINATION (safe_room
);
5115 /* Now encode the character C. */
5116 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5117 EMIT_ONE_ASCII_BYTE (c
);
5118 else if (CHAR_BYTE8_P (c
))
5120 c
= CHAR_TO_BYTE8 (c
);
5126 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
5130 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5132 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5133 charset
= CHARSET_FROM_ID (charset_ascii
);
5137 c
= coding
->default_char
;
5138 charset
= char_charset (c
, charset_list
, &code
);
5141 if (code
== CHARSET_INVALID_CODE (charset
))
5143 if (charset
== charset_big5
)
5147 c1
= code
>> 8, c2
= code
& 0xFF;
5148 EMIT_TWO_BYTES (c1
, c2
);
5151 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5154 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5155 coding
->produced_char
+= produced_chars
;
5156 coding
->produced
= dst
- coding
->destination
;
5161 /*** 10. CCL handlers ***/
5163 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5164 Check if a text is encoded in a coding system of which
5165 encoder/decoder are written in CCL program. If it is, return
5166 CATEGORY_MASK_CCL, else return 0. */
5169 detect_coding_ccl (coding
, detect_info
)
5170 struct coding_system
*coding
;
5171 struct coding_detection_info
*detect_info
;
5173 const unsigned char *src
= coding
->source
, *src_base
;
5174 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5175 int multibytep
= coding
->src_multibyte
;
5176 int consumed_chars
= 0;
5178 unsigned char *valids
;
5179 int head_ascii
= coding
->head_ascii
;
5182 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5184 coding
= &coding_categories
[coding_category_ccl
];
5185 valids
= CODING_CCL_VALIDS (coding
);
5186 attrs
= CODING_ID_ATTRS (coding
->id
);
5187 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5196 if (c
< 0 || ! valids
[c
])
5198 if ((valids
[c
] > 1))
5199 found
= CATEGORY_MASK_CCL
;
5201 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5205 detect_info
->found
|= found
;
5210 decode_coding_ccl (coding
)
5211 struct coding_system
*coding
;
5213 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5214 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5215 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5216 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5217 int consumed_chars
= 0;
5218 int multibytep
= coding
->src_multibyte
;
5219 struct ccl_program ccl
;
5220 int source_charbuf
[1024];
5221 int source_byteidx
[1024];
5222 Lisp_Object attrs
, charset_list
;
5224 CODING_GET_INFO (coding
, attrs
, charset_list
);
5225 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
5227 while (src
< src_end
)
5229 const unsigned char *p
= src
;
5230 int *source
, *source_end
;
5234 while (i
< 1024 && p
< src_end
)
5236 source_byteidx
[i
] = p
- src
;
5237 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5240 while (i
< 1024 && p
< src_end
)
5241 source_charbuf
[i
++] = *p
++;
5243 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5246 source
= source_charbuf
;
5247 source_end
= source
+ i
;
5248 while (source
< source_end
)
5250 ccl_driver (&ccl
, source
, charbuf
,
5251 source_end
- source
, charbuf_end
- charbuf
,
5253 source
+= ccl
.consumed
;
5254 charbuf
+= ccl
.produced
;
5255 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
5258 if (source
< source_end
)
5259 src
+= source_byteidx
[source
- source_charbuf
];
5262 consumed_chars
+= source
- source_charbuf
;
5264 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
5265 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
5271 case CCL_STAT_SUSPEND_BY_SRC
:
5272 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5274 case CCL_STAT_SUSPEND_BY_DST
:
5277 case CCL_STAT_INVALID_CMD
:
5278 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5281 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5284 coding
->consumed_char
+= consumed_chars
;
5285 coding
->consumed
= src
- coding
->source
;
5286 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5290 encode_coding_ccl (coding
)
5291 struct coding_system
*coding
;
5293 struct ccl_program ccl
;
5294 int multibytep
= coding
->dst_multibyte
;
5295 int *charbuf
= coding
->charbuf
;
5296 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5297 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5298 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5299 int destination_charbuf
[1024];
5300 int i
, produced_chars
= 0;
5301 Lisp_Object attrs
, charset_list
;
5303 CODING_GET_INFO (coding
, attrs
, charset_list
);
5304 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
5306 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
5307 ccl
.dst_multibyte
= coding
->dst_multibyte
;
5309 while (charbuf
< charbuf_end
)
5311 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
5312 charbuf_end
- charbuf
, 1024, charset_list
);
5315 ASSURE_DESTINATION (ccl
.produced
* 2);
5316 for (i
= 0; i
< ccl
.produced
; i
++)
5317 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5321 ASSURE_DESTINATION (ccl
.produced
);
5322 for (i
= 0; i
< ccl
.produced
; i
++)
5323 *dst
++ = destination_charbuf
[i
] & 0xFF;
5324 produced_chars
+= ccl
.produced
;
5326 charbuf
+= ccl
.consumed
;
5327 if (ccl
.status
== CCL_STAT_QUIT
5328 || ccl
.status
== CCL_STAT_INVALID_CMD
)
5334 case CCL_STAT_SUSPEND_BY_SRC
:
5335 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5337 case CCL_STAT_SUSPEND_BY_DST
:
5338 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5341 case CCL_STAT_INVALID_CMD
:
5342 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5345 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5349 coding
->produced_char
+= produced_chars
;
5350 coding
->produced
= dst
- coding
->destination
;
5356 /*** 10, 11. no-conversion handlers ***/
5358 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5361 decode_coding_raw_text (coding
)
5362 struct coding_system
*coding
;
5365 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5367 coding
->chars_at_source
= 1;
5368 coding
->consumed_char
= coding
->src_chars
;
5369 coding
->consumed
= coding
->src_bytes
;
5370 if (eol_crlf
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5372 coding
->consumed_char
--;
5374 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5377 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5381 encode_coding_raw_text (coding
)
5382 struct coding_system
*coding
;
5384 int multibytep
= coding
->dst_multibyte
;
5385 int *charbuf
= coding
->charbuf
;
5386 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5387 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5388 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5389 int produced_chars
= 0;
5394 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5396 if (coding
->src_multibyte
)
5397 while (charbuf
< charbuf_end
)
5399 ASSURE_DESTINATION (safe_room
);
5401 if (ASCII_CHAR_P (c
))
5402 EMIT_ONE_ASCII_BYTE (c
);
5403 else if (CHAR_BYTE8_P (c
))
5405 c
= CHAR_TO_BYTE8 (c
);
5410 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5412 CHAR_STRING_ADVANCE (c
, p1
);
5415 EMIT_ONE_BYTE (*p0
);
5421 while (charbuf
< charbuf_end
)
5423 ASSURE_DESTINATION (safe_room
);
5430 if (coding
->src_multibyte
)
5432 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5434 while (charbuf
< charbuf_end
)
5436 ASSURE_DESTINATION (safe_room
);
5438 if (ASCII_CHAR_P (c
))
5440 else if (CHAR_BYTE8_P (c
))
5441 *dst
++ = CHAR_TO_BYTE8 (c
);
5443 CHAR_STRING_ADVANCE (c
, dst
);
5448 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5449 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5450 *dst
++ = *charbuf
++;
5452 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5454 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5455 coding
->produced_char
+= produced_chars
;
5456 coding
->produced
= dst
- coding
->destination
;
5460 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5461 Check if a text is encoded in a charset-based coding system. If it
5462 is, return 1, else return 0. */
5465 detect_coding_charset (coding
, detect_info
)
5466 struct coding_system
*coding
;
5467 struct coding_detection_info
*detect_info
;
5469 const unsigned char *src
= coding
->source
, *src_base
;
5470 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5471 int multibytep
= coding
->src_multibyte
;
5472 int consumed_chars
= 0;
5473 Lisp_Object attrs
, valids
, name
;
5475 int head_ascii
= coding
->head_ascii
;
5476 int check_latin_extra
= 0;
5478 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5480 coding
= &coding_categories
[coding_category_charset
];
5481 attrs
= CODING_ID_ATTRS (coding
->id
);
5482 valids
= AREF (attrs
, coding_attr_charset_valids
);
5483 name
= CODING_ID_NAME (coding
->id
);
5484 if (strncmp ((char *) SDATA (SYMBOL_NAME (name
)),
5485 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5486 || strncmp ((char *) SDATA (SYMBOL_NAME (name
)),
5487 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5488 check_latin_extra
= 1;
5490 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5497 struct charset
*charset
;
5504 val
= AREF (valids
, c
);
5510 && check_latin_extra
5511 && (!VECTORP (Vlatin_extra_code_table
)
5512 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5514 found
= CATEGORY_MASK_CHARSET
;
5518 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5519 dim
= CHARSET_DIMENSION (charset
);
5520 for (idx
= 1; idx
< dim
; idx
++)
5525 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5526 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5535 for (; CONSP (val
); val
= XCDR (val
))
5537 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5538 dim
= CHARSET_DIMENSION (charset
);
5544 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5545 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5560 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5564 detect_info
->found
|= found
;
5569 decode_coding_charset (coding
)
5570 struct coding_system
*coding
;
5572 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5573 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5574 const unsigned char *src_base
;
5575 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5576 /* We may produce one charset annocation in one loop and one more at
5579 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5580 int consumed_chars
= 0, consumed_chars_base
;
5581 int multibytep
= coding
->src_multibyte
;
5582 Lisp_Object attrs
, charset_list
, valids
;
5583 int char_offset
= coding
->produced_char
;
5584 int last_offset
= char_offset
;
5585 int last_id
= charset_ascii
;
5587 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5588 int byte_after_cr
= -1;
5590 CODING_GET_INFO (coding
, attrs
, charset_list
);
5591 valids
= AREF (attrs
, coding_attr_charset_valids
);
5597 struct charset
*charset
;
5603 consumed_chars_base
= consumed_chars
;
5605 if (charbuf
>= charbuf_end
)
5607 if (byte_after_cr
>= 0)
5612 if (byte_after_cr
>= 0)
5620 if (eol_crlf
&& c
== '\r')
5621 ONE_MORE_BYTE (byte_after_cr
);
5627 val
= AREF (valids
, c
);
5628 if (! INTEGERP (val
) && ! CONSP (val
))
5632 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5633 dim
= CHARSET_DIMENSION (charset
);
5637 code
= (code
<< 8) | c
;
5640 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5645 /* VAL is a list of charset IDs. It is assured that the
5646 list is sorted by charset dimensions (smaller one
5650 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5651 dim
= CHARSET_DIMENSION (charset
);
5655 code
= (code
<< 8) | c
;
5658 CODING_DECODE_CHAR (coding
, src
, src_base
,
5659 src_end
, charset
, code
, c
);
5667 if (charset
->id
!= charset_ascii
5668 && last_id
!= charset
->id
)
5670 if (last_id
!= charset_ascii
)
5671 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5672 last_id
= charset
->id
;
5673 last_offset
= char_offset
;
5682 consumed_chars
= consumed_chars_base
;
5684 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5690 if (last_id
!= charset_ascii
)
5691 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5692 coding
->consumed_char
+= consumed_chars_base
;
5693 coding
->consumed
= src_base
- coding
->source
;
5694 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5698 encode_coding_charset (coding
)
5699 struct coding_system
*coding
;
5701 int multibytep
= coding
->dst_multibyte
;
5702 int *charbuf
= coding
->charbuf
;
5703 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5704 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5705 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5706 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5707 int produced_chars
= 0;
5708 Lisp_Object attrs
, charset_list
;
5709 int ascii_compatible
;
5712 CODING_GET_INFO (coding
, attrs
, charset_list
);
5713 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5715 while (charbuf
< charbuf_end
)
5717 struct charset
*charset
;
5720 ASSURE_DESTINATION (safe_room
);
5722 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5723 EMIT_ONE_ASCII_BYTE (c
);
5724 else if (CHAR_BYTE8_P (c
))
5726 c
= CHAR_TO_BYTE8 (c
);
5731 charset
= char_charset (c
, charset_list
, &code
);
5734 if (CHARSET_DIMENSION (charset
) == 1)
5735 EMIT_ONE_BYTE (code
);
5736 else if (CHARSET_DIMENSION (charset
) == 2)
5737 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5738 else if (CHARSET_DIMENSION (charset
) == 3)
5739 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5741 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5742 (code
>> 8) & 0xFF, code
& 0xFF);
5746 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5747 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5749 c
= coding
->default_char
;
5755 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5756 coding
->produced_char
+= produced_chars
;
5757 coding
->produced
= dst
- coding
->destination
;
5762 /*** 7. C library functions ***/
5764 /* Setup coding context CODING from information about CODING_SYSTEM.
5765 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5766 CODING_SYSTEM is invalid, signal an error. */
5769 setup_coding_system (coding_system
, coding
)
5770 Lisp_Object coding_system
;
5771 struct coding_system
*coding
;
5774 Lisp_Object eol_type
;
5775 Lisp_Object coding_type
;
5778 if (NILP (coding_system
))
5779 coding_system
= Qundecided
;
5781 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5783 attrs
= CODING_ID_ATTRS (coding
->id
);
5784 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5787 coding
->head_ascii
= -1;
5788 if (VECTORP (eol_type
))
5789 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5790 | CODING_REQUIRE_DETECTION_MASK
);
5791 else if (! EQ (eol_type
, Qunix
))
5792 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5793 | CODING_REQUIRE_ENCODING_MASK
);
5795 coding
->common_flags
= 0;
5796 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5797 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5798 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5799 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5800 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5801 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5803 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5804 coding
->max_charset_id
= SCHARS (val
) - 1;
5805 coding
->safe_charsets
= SDATA (val
);
5806 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5807 coding
->carryover_bytes
= 0;
5809 coding_type
= CODING_ATTR_TYPE (attrs
);
5810 if (EQ (coding_type
, Qundecided
))
5812 coding
->detector
= NULL
;
5813 coding
->decoder
= decode_coding_raw_text
;
5814 coding
->encoder
= encode_coding_raw_text
;
5815 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5817 else if (EQ (coding_type
, Qiso_2022
))
5820 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5822 /* Invoke graphic register 0 to plane 0. */
5823 CODING_ISO_INVOCATION (coding
, 0) = 0;
5824 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5825 CODING_ISO_INVOCATION (coding
, 1)
5826 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5827 /* Setup the initial status of designation. */
5828 for (i
= 0; i
< 4; i
++)
5829 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5830 /* Not single shifting initially. */
5831 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5832 /* Beginning of buffer should also be regarded as bol. */
5833 CODING_ISO_BOL (coding
) = 1;
5834 coding
->detector
= detect_coding_iso_2022
;
5835 coding
->decoder
= decode_coding_iso_2022
;
5836 coding
->encoder
= encode_coding_iso_2022
;
5837 if (flags
& CODING_ISO_FLAG_SAFE
)
5838 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5839 coding
->common_flags
5840 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5841 | CODING_REQUIRE_FLUSHING_MASK
);
5842 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5843 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5844 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5845 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5846 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5848 setup_iso_safe_charsets (attrs
);
5849 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5850 coding
->max_charset_id
= SCHARS (val
) - 1;
5851 coding
->safe_charsets
= SDATA (val
);
5853 CODING_ISO_FLAGS (coding
) = flags
;
5854 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5855 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5856 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5857 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5859 else if (EQ (coding_type
, Qcharset
))
5861 coding
->detector
= detect_coding_charset
;
5862 coding
->decoder
= decode_coding_charset
;
5863 coding
->encoder
= encode_coding_charset
;
5864 coding
->common_flags
5865 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5867 else if (EQ (coding_type
, Qutf_8
))
5869 val
= AREF (attrs
, coding_attr_utf_bom
);
5870 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5871 : EQ (val
, Qt
) ? utf_with_bom
5873 coding
->detector
= detect_coding_utf_8
;
5874 coding
->decoder
= decode_coding_utf_8
;
5875 coding
->encoder
= encode_coding_utf_8
;
5876 coding
->common_flags
5877 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5878 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5879 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5881 else if (EQ (coding_type
, Qutf_16
))
5883 val
= AREF (attrs
, coding_attr_utf_bom
);
5884 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5885 : EQ (val
, Qt
) ? utf_with_bom
5887 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5888 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5889 : utf_16_little_endian
);
5890 CODING_UTF_16_SURROGATE (coding
) = 0;
5891 coding
->detector
= detect_coding_utf_16
;
5892 coding
->decoder
= decode_coding_utf_16
;
5893 coding
->encoder
= encode_coding_utf_16
;
5894 coding
->common_flags
5895 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5896 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5897 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5899 else if (EQ (coding_type
, Qccl
))
5901 coding
->detector
= detect_coding_ccl
;
5902 coding
->decoder
= decode_coding_ccl
;
5903 coding
->encoder
= encode_coding_ccl
;
5904 coding
->common_flags
5905 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5906 | CODING_REQUIRE_FLUSHING_MASK
);
5908 else if (EQ (coding_type
, Qemacs_mule
))
5910 coding
->detector
= detect_coding_emacs_mule
;
5911 coding
->decoder
= decode_coding_emacs_mule
;
5912 coding
->encoder
= encode_coding_emacs_mule
;
5913 coding
->common_flags
5914 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5915 coding
->spec
.emacs_mule
.full_support
= 1;
5916 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5917 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5919 Lisp_Object tail
, safe_charsets
;
5920 int max_charset_id
= 0;
5922 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5924 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5925 max_charset_id
= XFASTINT (XCAR (tail
));
5926 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5927 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5928 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5930 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5931 coding
->max_charset_id
= max_charset_id
;
5932 coding
->safe_charsets
= SDATA (safe_charsets
);
5933 coding
->spec
.emacs_mule
.full_support
= 1;
5935 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5936 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5938 else if (EQ (coding_type
, Qshift_jis
))
5940 coding
->detector
= detect_coding_sjis
;
5941 coding
->decoder
= decode_coding_sjis
;
5942 coding
->encoder
= encode_coding_sjis
;
5943 coding
->common_flags
5944 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5946 else if (EQ (coding_type
, Qbig5
))
5948 coding
->detector
= detect_coding_big5
;
5949 coding
->decoder
= decode_coding_big5
;
5950 coding
->encoder
= encode_coding_big5
;
5951 coding
->common_flags
5952 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5954 else /* EQ (coding_type, Qraw_text) */
5956 coding
->detector
= NULL
;
5957 coding
->decoder
= decode_coding_raw_text
;
5958 coding
->encoder
= encode_coding_raw_text
;
5959 if (! EQ (eol_type
, Qunix
))
5961 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5962 if (! VECTORP (eol_type
))
5963 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5971 /* Return a list of charsets supported by CODING. */
5974 coding_charset_list (coding
)
5975 struct coding_system
*coding
;
5977 Lisp_Object attrs
, charset_list
;
5979 CODING_GET_INFO (coding
, attrs
, charset_list
);
5980 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5982 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5984 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5985 charset_list
= Viso_2022_charset_list
;
5987 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5989 charset_list
= Vemacs_mule_charset_list
;
5991 return charset_list
;
5995 /* Return a list of charsets supported by CODING-SYSTEM. */
5998 coding_system_charset_list (coding_system
)
5999 Lisp_Object coding_system
;
6002 Lisp_Object attrs
, charset_list
;
6004 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
6005 attrs
= CODING_ID_ATTRS (id
);
6007 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
6009 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
6011 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
6012 charset_list
= Viso_2022_charset_list
;
6014 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
6016 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
6018 charset_list
= Vemacs_mule_charset_list
;
6022 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
6024 return charset_list
;
6028 /* Return raw-text or one of its subsidiaries that has the same
6029 eol_type as CODING-SYSTEM. */
6032 raw_text_coding_system (coding_system
)
6033 Lisp_Object coding_system
;
6035 Lisp_Object spec
, attrs
;
6036 Lisp_Object eol_type
, raw_text_eol_type
;
6038 if (NILP (coding_system
))
6040 spec
= CODING_SYSTEM_SPEC (coding_system
);
6041 attrs
= AREF (spec
, 0);
6043 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
6044 return coding_system
;
6046 eol_type
= AREF (spec
, 2);
6047 if (VECTORP (eol_type
))
6049 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
6050 raw_text_eol_type
= AREF (spec
, 2);
6051 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
6052 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
6053 : AREF (raw_text_eol_type
, 2));
6057 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
6058 does, return one of the subsidiary that has the same eol-spec as
6059 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
6060 inherit end-of-line format from the system's setting
6061 (system_eol_type). */
6064 coding_inherit_eol_type (coding_system
, parent
)
6065 Lisp_Object coding_system
, parent
;
6067 Lisp_Object spec
, eol_type
;
6069 if (NILP (coding_system
))
6070 coding_system
= Qraw_text
;
6071 spec
= CODING_SYSTEM_SPEC (coding_system
);
6072 eol_type
= AREF (spec
, 2);
6073 if (VECTORP (eol_type
))
6075 Lisp_Object parent_eol_type
;
6077 if (! NILP (parent
))
6079 Lisp_Object parent_spec
;
6081 parent_spec
= CODING_SYSTEM_SPEC (parent
);
6082 parent_eol_type
= AREF (parent_spec
, 2);
6085 parent_eol_type
= system_eol_type
;
6086 if (EQ (parent_eol_type
, Qunix
))
6087 coding_system
= AREF (eol_type
, 0);
6088 else if (EQ (parent_eol_type
, Qdos
))
6089 coding_system
= AREF (eol_type
, 1);
6090 else if (EQ (parent_eol_type
, Qmac
))
6091 coding_system
= AREF (eol_type
, 2);
6093 return coding_system
;
6096 /* Emacs has a mechanism to automatically detect a coding system if it
6097 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6098 it's impossible to distinguish some coding systems accurately
6099 because they use the same range of codes. So, at first, coding
6100 systems are categorized into 7, those are:
6102 o coding-category-emacs-mule
6104 The category for a coding system which has the same code range
6105 as Emacs' internal format. Assigned the coding-system (Lisp
6106 symbol) `emacs-mule' by default.
6108 o coding-category-sjis
6110 The category for a coding system which has the same code range
6111 as SJIS. Assigned the coding-system (Lisp
6112 symbol) `japanese-shift-jis' by default.
6114 o coding-category-iso-7
6116 The category for a coding system which has the same code range
6117 as ISO2022 of 7-bit environment. This doesn't use any locking
6118 shift and single shift functions. This can encode/decode all
6119 charsets. Assigned the coding-system (Lisp symbol)
6120 `iso-2022-7bit' by default.
6122 o coding-category-iso-7-tight
6124 Same as coding-category-iso-7 except that this can
6125 encode/decode only the specified charsets.
6127 o coding-category-iso-8-1
6129 The category for a coding system which has the same code range
6130 as ISO2022 of 8-bit environment and graphic plane 1 used only
6131 for DIMENSION1 charset. This doesn't use any locking shift
6132 and single shift functions. Assigned the coding-system (Lisp
6133 symbol) `iso-latin-1' by default.
6135 o coding-category-iso-8-2
6137 The category for a coding system which has the same code range
6138 as ISO2022 of 8-bit environment and graphic plane 1 used only
6139 for DIMENSION2 charset. This doesn't use any locking shift
6140 and single shift functions. Assigned the coding-system (Lisp
6141 symbol) `japanese-iso-8bit' by default.
6143 o coding-category-iso-7-else
6145 The category for a coding system which has the same code range
6146 as ISO2022 of 7-bit environemnt but uses locking shift or
6147 single shift functions. Assigned the coding-system (Lisp
6148 symbol) `iso-2022-7bit-lock' by default.
6150 o coding-category-iso-8-else
6152 The category for a coding system which has the same code range
6153 as ISO2022 of 8-bit environemnt but uses locking shift or
6154 single shift functions. Assigned the coding-system (Lisp
6155 symbol) `iso-2022-8bit-ss2' by default.
6157 o coding-category-big5
6159 The category for a coding system which has the same code range
6160 as BIG5. Assigned the coding-system (Lisp symbol)
6161 `cn-big5' by default.
6163 o coding-category-utf-8
6165 The category for a coding system which has the same code range
6166 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6167 symbol) `utf-8' by default.
6169 o coding-category-utf-16-be
6171 The category for a coding system in which a text has an
6172 Unicode signature (cf. Unicode Standard) in the order of BIG
6173 endian at the head. Assigned the coding-system (Lisp symbol)
6174 `utf-16-be' by default.
6176 o coding-category-utf-16-le
6178 The category for a coding system in which a text has an
6179 Unicode signature (cf. Unicode Standard) in the order of
6180 LITTLE endian at the head. Assigned the coding-system (Lisp
6181 symbol) `utf-16-le' by default.
6183 o coding-category-ccl
6185 The category for a coding system of which encoder/decoder is
6186 written in CCL programs. The default value is nil, i.e., no
6187 coding system is assigned.
6189 o coding-category-binary
6191 The category for a coding system not categorized in any of the
6192 above. Assigned the coding-system (Lisp symbol)
6193 `no-conversion' by default.
6195 Each of them is a Lisp symbol and the value is an actual
6196 `coding-system's (this is also a Lisp symbol) assigned by a user.
6197 What Emacs does actually is to detect a category of coding system.
6198 Then, it uses a `coding-system' assigned to it. If Emacs can't
6199 decide only one possible category, it selects a category of the
6200 highest priority. Priorities of categories are also specified by a
6201 user in a Lisp variable `coding-category-list'.
6205 #define EOL_SEEN_NONE 0
6206 #define EOL_SEEN_LF 1
6207 #define EOL_SEEN_CR 2
6208 #define EOL_SEEN_CRLF 4
6210 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6211 SOURCE is encoded. If CATEGORY is one of
6212 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6213 two-byte, else they are encoded by one-byte.
6215 Return one of EOL_SEEN_XXX. */
6217 #define MAX_EOL_CHECK_COUNT 3
6220 detect_eol (source
, src_bytes
, category
)
6221 const unsigned char *source
;
6222 EMACS_INT src_bytes
;
6223 enum coding_category category
;
6225 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6228 int eol_seen
= EOL_SEEN_NONE
;
6230 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6234 msb
= category
== (coding_category_utf_16_le
6235 | coding_category_utf_16_le_nosig
);
6238 while (src
+ 1 < src_end
)
6241 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6246 this_eol
= EOL_SEEN_LF
;
6247 else if (src
+ 3 >= src_end
6248 || src
[msb
+ 2] != 0
6249 || src
[lsb
+ 2] != '\n')
6250 this_eol
= EOL_SEEN_CR
;
6253 this_eol
= EOL_SEEN_CRLF
;
6257 if (eol_seen
== EOL_SEEN_NONE
)
6258 /* This is the first end-of-line. */
6259 eol_seen
= this_eol
;
6260 else if (eol_seen
!= this_eol
)
6262 /* The found type is different from what found before.
6263 Allow for stray ^M characters in DOS EOL files. */
6264 if (eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
6265 || eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
)
6266 eol_seen
= EOL_SEEN_CRLF
;
6269 eol_seen
= EOL_SEEN_LF
;
6273 if (++total
== MAX_EOL_CHECK_COUNT
)
6281 while (src
< src_end
)
6284 if (c
== '\n' || c
== '\r')
6289 this_eol
= EOL_SEEN_LF
;
6290 else if (src
>= src_end
|| *src
!= '\n')
6291 this_eol
= EOL_SEEN_CR
;
6293 this_eol
= EOL_SEEN_CRLF
, src
++;
6295 if (eol_seen
== EOL_SEEN_NONE
)
6296 /* This is the first end-of-line. */
6297 eol_seen
= this_eol
;
6298 else if (eol_seen
!= this_eol
)
6300 /* The found type is different from what found before.
6301 Allow for stray ^M characters in DOS EOL files. */
6302 if (eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
6303 || eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
)
6304 eol_seen
= EOL_SEEN_CRLF
;
6307 eol_seen
= EOL_SEEN_LF
;
6311 if (++total
== MAX_EOL_CHECK_COUNT
)
6321 adjust_coding_eol_type (coding
, eol_seen
)
6322 struct coding_system
*coding
;
6325 Lisp_Object eol_type
;
6327 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6328 if (eol_seen
& EOL_SEEN_LF
)
6330 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6333 else if (eol_seen
& EOL_SEEN_CRLF
)
6335 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6338 else if (eol_seen
& EOL_SEEN_CR
)
6340 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6346 /* Detect how a text specified in CODING is encoded. If a coding
6347 system is detected, update fields of CODING by the detected coding
6351 detect_coding (coding
)
6352 struct coding_system
*coding
;
6354 const unsigned char *src
, *src_end
;
6355 int saved_mode
= coding
->mode
;
6357 coding
->consumed
= coding
->consumed_char
= 0;
6358 coding
->produced
= coding
->produced_char
= 0;
6359 coding_set_source (coding
);
6361 src_end
= coding
->source
+ coding
->src_bytes
;
6362 coding
->head_ascii
= 0;
6364 /* If we have not yet decided the text encoding type, detect it
6366 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6369 struct coding_detection_info detect_info
;
6370 int null_byte_found
= 0, eight_bit_found
= 0;
6372 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6373 for (src
= coding
->source
; src
< src_end
; src
++)
6378 eight_bit_found
= 1;
6379 if (null_byte_found
)
6384 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6385 && ! inhibit_iso_escape_detection
6386 && ! detect_info
.checked
)
6388 if (detect_coding_iso_2022 (coding
, &detect_info
))
6390 /* We have scanned the whole data. */
6391 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6393 /* We didn't find an 8-bit code. We may
6394 have found a null-byte, but it's very
6395 rare that a binary file confirm to
6398 coding
->head_ascii
= src
- coding
->source
;
6400 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6404 else if (! c
&& !inhibit_null_byte_detection
)
6406 null_byte_found
= 1;
6407 if (eight_bit_found
)
6410 if (! eight_bit_found
)
6411 coding
->head_ascii
++;
6413 else if (! eight_bit_found
)
6414 coding
->head_ascii
++;
6417 if (null_byte_found
|| eight_bit_found
6418 || coding
->head_ascii
< coding
->src_bytes
6419 || detect_info
.found
)
6421 enum coding_category category
;
6422 struct coding_system
*this;
6424 if (coding
->head_ascii
== coding
->src_bytes
)
6425 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6426 for (i
= 0; i
< coding_category_raw_text
; i
++)
6428 category
= coding_priorities
[i
];
6429 this = coding_categories
+ category
;
6430 if (detect_info
.found
& (1 << category
))
6435 if (null_byte_found
)
6437 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6438 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6440 for (i
= 0; i
< coding_category_raw_text
; i
++)
6442 category
= coding_priorities
[i
];
6443 this = coding_categories
+ category
;
6446 /* No coding system of this category is defined. */
6447 detect_info
.rejected
|= (1 << category
);
6449 else if (category
>= coding_category_raw_text
)
6451 else if (detect_info
.checked
& (1 << category
))
6453 if (detect_info
.found
& (1 << category
))
6456 else if ((*(this->detector
)) (coding
, &detect_info
)
6457 && detect_info
.found
& (1 << category
))
6459 if (category
== coding_category_utf_16_auto
)
6461 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6462 category
= coding_category_utf_16_le
;
6464 category
= coding_category_utf_16_be
;
6471 if (i
< coding_category_raw_text
)
6472 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6473 else if (null_byte_found
)
6474 setup_coding_system (Qno_conversion
, coding
);
6475 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6476 == CATEGORY_MASK_ANY
)
6477 setup_coding_system (Qraw_text
, coding
);
6478 else if (detect_info
.rejected
)
6479 for (i
= 0; i
< coding_category_raw_text
; i
++)
6480 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6482 this = coding_categories
+ coding_priorities
[i
];
6483 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6488 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6489 == coding_category_utf_8_auto
)
6491 Lisp_Object coding_systems
;
6492 struct coding_detection_info detect_info
;
6495 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6496 detect_info
.found
= detect_info
.rejected
= 0;
6497 coding
->head_ascii
= 0;
6498 if (CONSP (coding_systems
)
6499 && detect_coding_utf_8 (coding
, &detect_info
))
6501 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6502 setup_coding_system (XCAR (coding_systems
), coding
);
6504 setup_coding_system (XCDR (coding_systems
), coding
);
6507 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6508 == coding_category_utf_16_auto
)
6510 Lisp_Object coding_systems
;
6511 struct coding_detection_info detect_info
;
6514 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6515 detect_info
.found
= detect_info
.rejected
= 0;
6516 coding
->head_ascii
= 0;
6517 if (CONSP (coding_systems
)
6518 && detect_coding_utf_16 (coding
, &detect_info
))
6520 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6521 setup_coding_system (XCAR (coding_systems
), coding
);
6522 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6523 setup_coding_system (XCDR (coding_systems
), coding
);
6526 coding
->mode
= saved_mode
;
6532 struct coding_system
*coding
;
6534 Lisp_Object eol_type
;
6535 unsigned char *p
, *pbeg
, *pend
;
6537 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6538 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6541 if (NILP (coding
->dst_object
))
6542 pbeg
= coding
->destination
;
6544 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6545 pend
= pbeg
+ coding
->produced
;
6547 if (VECTORP (eol_type
))
6549 int eol_seen
= EOL_SEEN_NONE
;
6551 for (p
= pbeg
; p
< pend
; p
++)
6554 eol_seen
|= EOL_SEEN_LF
;
6555 else if (*p
== '\r')
6557 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6559 eol_seen
|= EOL_SEEN_CRLF
;
6563 eol_seen
|= EOL_SEEN_CR
;
6566 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6567 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6568 && (eol_seen
& EOL_SEEN_CR
) != 0
6569 && (eol_seen
& EOL_SEEN_LF
) == 0)
6570 eol_seen
= EOL_SEEN_CRLF
;
6571 else if (eol_seen
!= EOL_SEEN_NONE
6572 && eol_seen
!= EOL_SEEN_LF
6573 && eol_seen
!= EOL_SEEN_CRLF
6574 && eol_seen
!= EOL_SEEN_CR
)
6575 eol_seen
= EOL_SEEN_LF
;
6576 if (eol_seen
!= EOL_SEEN_NONE
)
6577 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6580 if (EQ (eol_type
, Qmac
))
6582 for (p
= pbeg
; p
< pend
; p
++)
6586 else if (EQ (eol_type
, Qdos
))
6590 if (NILP (coding
->dst_object
))
6592 /* Start deleting '\r' from the tail to minimize the memory
6594 for (p
= pend
- 2; p
>= pbeg
; p
--)
6597 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
6603 int pos_byte
= coding
->dst_pos_byte
;
6604 int pos
= coding
->dst_pos
;
6605 int pos_end
= pos
+ coding
->produced_char
- 1;
6607 while (pos
< pos_end
)
6609 p
= BYTE_POS_ADDR (pos_byte
);
6610 if (*p
== '\r' && p
[1] == '\n')
6612 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6617 if (coding
->dst_multibyte
)
6618 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6623 coding
->produced
-= n
;
6624 coding
->produced_char
-= n
;
6629 /* Return a translation table (or list of them) from coding system
6630 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6631 decoding (ENCODEP is zero). */
6634 get_translation_table (attrs
, encodep
, max_lookup
)
6636 int encodep
, *max_lookup
;
6638 Lisp_Object standard
, translation_table
;
6641 if (NILP (Venable_character_translation
))
6648 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6649 standard
= Vstandard_translation_table_for_encode
;
6651 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6652 standard
= Vstandard_translation_table_for_decode
;
6653 if (NILP (translation_table
))
6654 translation_table
= standard
;
6657 if (SYMBOLP (translation_table
))
6658 translation_table
= Fget (translation_table
, Qtranslation_table
);
6659 else if (CONSP (translation_table
))
6661 translation_table
= Fcopy_sequence (translation_table
);
6662 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6663 if (SYMBOLP (XCAR (val
)))
6664 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6666 if (CHAR_TABLE_P (standard
))
6668 if (CONSP (translation_table
))
6669 translation_table
= nconc2 (translation_table
,
6670 Fcons (standard
, Qnil
));
6672 translation_table
= Fcons (translation_table
,
6673 Fcons (standard
, Qnil
));
6680 if (CHAR_TABLE_P (translation_table
)
6681 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6683 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6684 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6685 *max_lookup
= XFASTINT (val
);
6687 else if (CONSP (translation_table
))
6689 Lisp_Object tail
, val
;
6691 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6692 if (CHAR_TABLE_P (XCAR (tail
))
6693 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6695 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6696 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6697 *max_lookup
= XFASTINT (val
);
6701 return translation_table
;
6704 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6707 if (CHAR_TABLE_P (table)) \
6709 trans = CHAR_TABLE_REF (table, c); \
6710 if (CHARACTERP (trans)) \
6711 c = XFASTINT (trans), trans = Qnil; \
6713 else if (CONSP (table)) \
6717 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6718 if (CHAR_TABLE_P (XCAR (tail))) \
6720 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6721 if (CHARACTERP (trans)) \
6722 c = XFASTINT (trans), trans = Qnil; \
6723 else if (! NILP (trans)) \
6730 /* Return a translation of character(s) at BUF according to TRANS.
6731 TRANS is TO-CHAR or ((FROM . TO) ...) where
6732 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6733 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6734 translation is found, and Qnil if not found..
6735 If BUF is too short to lookup characters in FROM, return Qt. */
6738 get_translation (trans
, buf
, buf_end
)
6743 if (INTEGERP (trans
))
6745 for (; CONSP (trans
); trans
= XCDR (trans
))
6747 Lisp_Object val
= XCAR (trans
);
6748 Lisp_Object from
= XCAR (val
);
6749 int len
= ASIZE (from
);
6752 for (i
= 0; i
< len
; i
++)
6754 if (buf
+ i
== buf_end
)
6756 if (XINT (AREF (from
, i
)) != buf
[i
])
6767 produce_chars (coding
, translation_table
, last_block
)
6768 struct coding_system
*coding
;
6769 Lisp_Object translation_table
;
6772 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6773 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6775 EMACS_INT produced_chars
= 0;
6778 if (! coding
->chars_at_source
)
6780 /* Source characters are in coding->charbuf. */
6781 int *buf
= coding
->charbuf
;
6782 int *buf_end
= buf
+ coding
->charbuf_used
;
6784 if (EQ (coding
->src_object
, coding
->dst_object
))
6786 coding_set_source (coding
);
6787 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6790 while (buf
< buf_end
)
6796 int from_nchars
= 1, to_nchars
= 1;
6797 Lisp_Object trans
= Qnil
;
6799 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6802 trans
= get_translation (trans
, buf
, buf_end
);
6803 if (INTEGERP (trans
))
6805 else if (CONSP (trans
))
6807 from_nchars
= ASIZE (XCAR (trans
));
6808 trans
= XCDR (trans
);
6809 if (INTEGERP (trans
))
6813 to_nchars
= ASIZE (trans
);
6814 c
= XINT (AREF (trans
, 0));
6817 else if (EQ (trans
, Qt
) && ! last_block
)
6821 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6823 dst
= alloc_destination (coding
,
6825 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6827 if (EQ (coding
->src_object
, coding
->dst_object
))
6829 coding_set_source (coding
);
6830 dst_end
= (((unsigned char *) coding
->source
)
6831 + coding
->consumed
);
6834 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6837 for (i
= 0; i
< to_nchars
; i
++)
6840 c
= XINT (AREF (trans
, i
));
6841 if (coding
->dst_multibyte
6842 || ! CHAR_BYTE8_P (c
))
6843 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6845 *dst
++ = CHAR_TO_BYTE8 (c
);
6847 produced_chars
+= to_nchars
;
6851 /* This is an annotation datum. (-C) is the length. */
6854 carryover
= buf_end
- buf
;
6858 /* Source characters are at coding->source. */
6859 const unsigned char *src
= coding
->source
;
6860 const unsigned char *src_end
= src
+ coding
->consumed
;
6862 if (EQ (coding
->dst_object
, coding
->src_object
))
6863 dst_end
= (unsigned char *) src
;
6864 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6866 if (coding
->src_multibyte
)
6869 EMACS_INT consumed_chars
= 0;
6873 const unsigned char *src_base
= src
;
6879 if (EQ (coding
->src_object
, coding
->dst_object
))
6880 dst_end
= (unsigned char *) src
;
6883 EMACS_INT offset
= src
- coding
->source
;
6885 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6887 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6888 coding_set_source (coding
);
6889 src
= coding
->source
+ offset
;
6890 src_end
= coding
->source
+ coding
->src_bytes
;
6891 if (EQ (coding
->src_object
, coding
->dst_object
))
6892 dst_end
= (unsigned char *) src
;
6902 while (src
< src_end
)
6907 if (dst
>= dst_end
- 1)
6909 if (EQ (coding
->src_object
, coding
->dst_object
))
6910 dst_end
= (unsigned char *) src
;
6911 if (dst
>= dst_end
- 1)
6913 EMACS_INT offset
= src
- coding
->source
;
6914 EMACS_INT more_bytes
;
6916 if (EQ (coding
->src_object
, coding
->dst_object
))
6917 more_bytes
= ((src_end
- src
) / 2) + 2;
6919 more_bytes
= src_end
- src
+ 2;
6920 dst
= alloc_destination (coding
, more_bytes
, dst
);
6921 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6922 coding_set_source (coding
);
6923 src
= coding
->source
+ offset
;
6924 src_end
= coding
->source
+ coding
->src_bytes
;
6925 if (EQ (coding
->src_object
, coding
->dst_object
))
6926 dst_end
= (unsigned char *) src
;
6934 if (!EQ (coding
->src_object
, coding
->dst_object
))
6936 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6940 EMACS_INT offset
= src
- coding
->source
;
6942 dst
= alloc_destination (coding
, require
, dst
);
6943 coding_set_source (coding
);
6944 src
= coding
->source
+ offset
;
6945 src_end
= coding
->source
+ coding
->src_bytes
;
6948 produced_chars
= coding
->consumed_char
;
6949 while (src
< src_end
)
6954 produced
= dst
- (coding
->destination
+ coding
->produced
);
6955 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6956 insert_from_gap (produced_chars
, produced
);
6957 coding
->produced
+= produced
;
6958 coding
->produced_char
+= produced_chars
;
6962 /* Compose text in CODING->object according to the annotation data at
6963 CHARBUF. CHARBUF is an array:
6964 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6968 produce_composition (coding
, charbuf
, pos
)
6969 struct coding_system
*coding
;
6975 enum composition_method method
;
6976 Lisp_Object components
;
6978 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6979 to
= pos
+ charbuf
[2];
6980 method
= (enum composition_method
) (charbuf
[4]);
6982 if (method
== COMPOSITION_RELATIVE
)
6986 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6989 if (method
== COMPOSITION_WITH_RULE
)
6990 len
= charbuf
[2] * 3 - 2;
6991 charbuf
+= MAX_ANNOTATION_LENGTH
;
6992 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6993 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6995 if (charbuf
[i
] >= 0)
6996 args
[j
] = make_number (charbuf
[i
]);
7000 args
[j
] = make_number (charbuf
[i
] % 0x100);
7003 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
7005 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
7009 /* Put `charset' property on text in CODING->object according to
7010 the annotation data at CHARBUF. CHARBUF is an array:
7011 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
7015 produce_charset (coding
, charbuf
, pos
)
7016 struct coding_system
*coding
;
7020 EMACS_INT from
= pos
- charbuf
[2];
7021 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
7023 Fput_text_property (make_number (from
), make_number (pos
),
7024 Qcharset
, CHARSET_NAME (charset
),
7025 coding
->dst_object
);
7029 #define CHARBUF_SIZE 0x4000
7031 #define ALLOC_CONVERSION_WORK_AREA(coding) \
7033 int size = CHARBUF_SIZE; \
7035 coding->charbuf = NULL; \
7036 while (size > 1024) \
7038 coding->charbuf = (int *) alloca (sizeof (int) * size); \
7039 if (coding->charbuf) \
7043 if (! coding->charbuf) \
7045 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
7046 return coding->result; \
7048 coding->charbuf_size = size; \
7053 produce_annotation (coding
, pos
)
7054 struct coding_system
*coding
;
7057 int *charbuf
= coding
->charbuf
;
7058 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
7060 if (NILP (coding
->dst_object
))
7063 while (charbuf
< charbuf_end
)
7069 int len
= -*charbuf
;
7074 case CODING_ANNOTATE_COMPOSITION_MASK
:
7075 produce_composition (coding
, charbuf
, pos
);
7077 case CODING_ANNOTATE_CHARSET_MASK
:
7078 produce_charset (coding
, charbuf
, pos
);
7086 /* Decode the data at CODING->src_object into CODING->dst_object.
7087 CODING->src_object is a buffer, a string, or nil.
7088 CODING->dst_object is a buffer.
7090 If CODING->src_object is a buffer, it must be the current buffer.
7091 In this case, if CODING->src_pos is positive, it is a position of
7092 the source text in the buffer, otherwise, the source text is in the
7093 gap area of the buffer, and CODING->src_pos specifies the offset of
7094 the text from GPT (which must be the same as PT). If this is the
7095 same buffer as CODING->dst_object, CODING->src_pos must be
7098 If CODING->src_object is a string, CODING->src_pos is an index to
7101 If CODING->src_object is nil, CODING->source must already point to
7102 the non-relocatable memory area. In this case, CODING->src_pos is
7103 an offset from CODING->source.
7105 The decoded data is inserted at the current point of the buffer
7110 decode_coding (coding
)
7111 struct coding_system
*coding
;
7114 Lisp_Object undo_list
;
7115 Lisp_Object translation_table
;
7119 if (BUFFERP (coding
->src_object
)
7120 && coding
->src_pos
> 0
7121 && coding
->src_pos
< GPT
7122 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7123 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7126 if (BUFFERP (coding
->dst_object
))
7128 if (current_buffer
!= XBUFFER (coding
->dst_object
))
7129 set_buffer_internal (XBUFFER (coding
->dst_object
));
7131 move_gap_both (PT
, PT_BYTE
);
7132 undo_list
= current_buffer
->undo_list
;
7133 current_buffer
->undo_list
= Qt
;
7136 coding
->consumed
= coding
->consumed_char
= 0;
7137 coding
->produced
= coding
->produced_char
= 0;
7138 coding
->chars_at_source
= 0;
7139 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7142 ALLOC_CONVERSION_WORK_AREA (coding
);
7144 attrs
= CODING_ID_ATTRS (coding
->id
);
7145 translation_table
= get_translation_table (attrs
, 0, NULL
);
7150 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7152 coding_set_source (coding
);
7153 coding
->annotated
= 0;
7154 coding
->charbuf_used
= carryover
;
7155 (*(coding
->decoder
)) (coding
);
7156 coding_set_destination (coding
);
7157 carryover
= produce_chars (coding
, translation_table
, 0);
7158 if (coding
->annotated
)
7159 produce_annotation (coding
, pos
);
7160 for (i
= 0; i
< carryover
; i
++)
7162 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7164 while (coding
->consumed
< coding
->src_bytes
7165 && (coding
->result
== CODING_RESULT_SUCCESS
7166 || coding
->result
== CODING_RESULT_INVALID_SRC
));
7170 coding_set_destination (coding
);
7171 coding
->charbuf_used
= carryover
;
7172 produce_chars (coding
, translation_table
, 1);
7175 coding
->carryover_bytes
= 0;
7176 if (coding
->consumed
< coding
->src_bytes
)
7178 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7179 const unsigned char *src
;
7181 coding_set_source (coding
);
7182 coding_set_destination (coding
);
7183 src
= coding
->source
+ coding
->consumed
;
7185 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7187 /* Flush out unprocessed data as binary chars. We are sure
7188 that the number of data is less than the size of
7190 coding
->charbuf_used
= 0;
7191 coding
->chars_at_source
= 0;
7193 while (nbytes
-- > 0)
7198 c
= BYTE8_TO_CHAR (c
);
7199 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7201 produce_chars (coding
, Qnil
, 1);
7205 /* Record unprocessed bytes in coding->carryover. We are
7206 sure that the number of data is less than the size of
7207 coding->carryover. */
7208 unsigned char *p
= coding
->carryover
;
7210 if (nbytes
> sizeof coding
->carryover
)
7211 nbytes
= sizeof coding
->carryover
;
7212 coding
->carryover_bytes
= nbytes
;
7213 while (nbytes
-- > 0)
7216 coding
->consumed
= coding
->src_bytes
;
7219 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7220 && !inhibit_eol_conversion
)
7221 decode_eol (coding
);
7222 if (BUFFERP (coding
->dst_object
))
7224 current_buffer
->undo_list
= undo_list
;
7225 record_insert (coding
->dst_pos
, coding
->produced_char
);
7227 return coding
->result
;
7231 /* Extract an annotation datum from a composition starting at POS and
7232 ending before LIMIT of CODING->src_object (buffer or string), store
7233 the data in BUF, set *STOP to a starting position of the next
7234 composition (if any) or to LIMIT, and return the address of the
7235 next element of BUF.
7237 If such an annotation is not found, set *STOP to a starting
7238 position of a composition after POS (if any) or to LIMIT, and
7242 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
7243 EMACS_INT pos
, limit
;
7244 struct coding_system
*coding
;
7248 EMACS_INT start
, end
;
7251 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7254 else if (start
> pos
)
7260 /* We found a composition. Store the corresponding
7261 annotation data in BUF. */
7263 enum composition_method method
= COMPOSITION_METHOD (prop
);
7264 int nchars
= COMPOSITION_LENGTH (prop
);
7266 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7267 if (method
!= COMPOSITION_RELATIVE
)
7269 Lisp_Object components
;
7272 components
= COMPOSITION_COMPONENTS (prop
);
7273 if (VECTORP (components
))
7275 len
= XVECTOR (components
)->size
;
7276 for (i
= 0; i
< len
; i
++)
7277 *buf
++ = XINT (AREF (components
, i
));
7279 else if (STRINGP (components
))
7281 len
= SCHARS (components
);
7285 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7289 else if (INTEGERP (components
))
7292 *buf
++ = XINT (components
);
7294 else if (CONSP (components
))
7296 for (len
= 0; CONSP (components
);
7297 len
++, components
= XCDR (components
))
7298 *buf
++ = XINT (XCAR (components
));
7306 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7317 /* Extract an annotation datum from a text property `charset' at POS of
7318 CODING->src_object (buffer of string), store the data in BUF, set
7319 *STOP to the position where the value of `charset' property changes
7320 (limiting by LIMIT), and return the address of the next element of
7323 If the property value is nil, set *STOP to the position where the
7324 property value is non-nil (limiting by LIMIT), and return BUF. */
7327 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
7328 EMACS_INT pos
, limit
;
7329 struct coding_system
*coding
;
7333 Lisp_Object val
, next
;
7336 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7337 if (! NILP (val
) && CHARSETP (val
))
7338 id
= XINT (CHARSET_SYMBOL_ID (val
));
7341 ADD_CHARSET_DATA (buf
, 0, id
);
7342 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7344 make_number (limit
));
7345 *stop
= XINT (next
);
7351 consume_chars (coding
, translation_table
, max_lookup
)
7352 struct coding_system
*coding
;
7353 Lisp_Object translation_table
;
7356 int *buf
= coding
->charbuf
;
7357 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7358 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7359 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7360 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7361 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7362 int multibytep
= coding
->src_multibyte
;
7363 Lisp_Object eol_type
;
7365 EMACS_INT stop
, stop_composition
, stop_charset
;
7366 int *lookup_buf
= NULL
;
7368 if (! NILP (translation_table
))
7369 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7371 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7372 if (VECTORP (eol_type
))
7375 /* Note: composition handling is not yet implemented. */
7376 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7378 if (NILP (coding
->src_object
))
7379 stop
= stop_composition
= stop_charset
= end_pos
;
7382 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7383 stop
= stop_composition
= pos
;
7385 stop
= stop_composition
= end_pos
;
7386 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7387 stop
= stop_charset
= pos
;
7389 stop_charset
= end_pos
;
7392 /* Compensate for CRLF and conversion. */
7393 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7394 while (buf
< buf_end
)
7402 if (pos
== stop_composition
)
7403 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7404 buf
, &stop_composition
);
7405 if (pos
== stop_charset
)
7406 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7407 buf
, &stop_charset
);
7408 stop
= (stop_composition
< stop_charset
7409 ? stop_composition
: stop_charset
);
7416 if (coding
->encoder
== encode_coding_raw_text
)
7418 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7419 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7421 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7424 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7425 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7427 if (! EQ (eol_type
, Qunix
))
7431 if (EQ (eol_type
, Qdos
))
7439 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7444 int from_nchars
= 1, to_nchars
= 1;
7445 int *lookup_buf_end
;
7446 const unsigned char *p
= src
;
7450 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7451 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7452 lookup_buf_end
= lookup_buf
+ i
;
7453 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7454 if (INTEGERP (trans
))
7456 else if (CONSP (trans
))
7458 from_nchars
= ASIZE (XCAR (trans
));
7459 trans
= XCDR (trans
);
7460 if (INTEGERP (trans
))
7464 to_nchars
= ASIZE (trans
);
7465 if (buf
+ to_nchars
> buf_end
)
7467 c
= XINT (AREF (trans
, 0));
7473 for (i
= 1; i
< to_nchars
; i
++)
7474 *buf
++ = XINT (AREF (trans
, i
));
7475 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7476 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7480 coding
->consumed
= src
- coding
->source
;
7481 coding
->consumed_char
= pos
- coding
->src_pos
;
7482 coding
->charbuf_used
= buf
- coding
->charbuf
;
7483 coding
->chars_at_source
= 0;
7487 /* Encode the text at CODING->src_object into CODING->dst_object.
7488 CODING->src_object is a buffer or a string.
7489 CODING->dst_object is a buffer or nil.
7491 If CODING->src_object is a buffer, it must be the current buffer.
7492 In this case, if CODING->src_pos is positive, it is a position of
7493 the source text in the buffer, otherwise. the source text is in the
7494 gap area of the buffer, and coding->src_pos specifies the offset of
7495 the text from GPT (which must be the same as PT). If this is the
7496 same buffer as CODING->dst_object, CODING->src_pos must be
7497 negative and CODING should not have `pre-write-conversion'.
7499 If CODING->src_object is a string, CODING should not have
7500 `pre-write-conversion'.
7502 If CODING->dst_object is a buffer, the encoded data is inserted at
7503 the current point of that buffer.
7505 If CODING->dst_object is nil, the encoded data is placed at the
7506 memory area specified by CODING->destination. */
7509 encode_coding (coding
)
7510 struct coding_system
*coding
;
7513 Lisp_Object translation_table
;
7516 attrs
= CODING_ID_ATTRS (coding
->id
);
7517 if (coding
->encoder
== encode_coding_raw_text
)
7518 translation_table
= Qnil
, max_lookup
= 0;
7520 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7522 if (BUFFERP (coding
->dst_object
))
7524 set_buffer_internal (XBUFFER (coding
->dst_object
));
7525 coding
->dst_multibyte
7526 = ! NILP (current_buffer
->enable_multibyte_characters
);
7529 coding
->consumed
= coding
->consumed_char
= 0;
7530 coding
->produced
= coding
->produced_char
= 0;
7531 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7534 ALLOC_CONVERSION_WORK_AREA (coding
);
7537 coding_set_source (coding
);
7538 consume_chars (coding
, translation_table
, max_lookup
);
7539 coding_set_destination (coding
);
7540 (*(coding
->encoder
)) (coding
);
7541 } while (coding
->consumed_char
< coding
->src_chars
);
7543 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7544 insert_from_gap (coding
->produced_char
, coding
->produced
);
7546 return (coding
->result
);
7550 /* Name (or base name) of work buffer for code conversion. */
7551 static Lisp_Object Vcode_conversion_workbuf_name
;
7553 /* A working buffer used by the top level conversion. Once it is
7554 created, it is never destroyed. It has the name
7555 Vcode_conversion_workbuf_name. The other working buffers are
7556 destroyed after the use is finished, and their names are modified
7557 versions of Vcode_conversion_workbuf_name. */
7558 static Lisp_Object Vcode_conversion_reused_workbuf
;
7560 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7561 static int reused_workbuf_in_use
;
7564 /* Return a working buffer of code convesion. MULTIBYTE specifies the
7565 multibyteness of returning buffer. */
7568 make_conversion_work_buffer (multibyte
)
7571 Lisp_Object name
, workbuf
;
7572 struct buffer
*current
;
7574 if (reused_workbuf_in_use
++)
7576 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7577 workbuf
= Fget_buffer_create (name
);
7581 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7582 Vcode_conversion_reused_workbuf
7583 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7584 workbuf
= Vcode_conversion_reused_workbuf
;
7586 current
= current_buffer
;
7587 set_buffer_internal (XBUFFER (workbuf
));
7588 /* We can't allow modification hooks to run in the work buffer. For
7589 instance, directory_files_internal assumes that file decoding
7590 doesn't compile new regexps. */
7591 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7593 current_buffer
->undo_list
= Qt
;
7594 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
7595 set_buffer_internal (current
);
7601 code_conversion_restore (arg
)
7604 Lisp_Object current
, workbuf
;
7605 struct gcpro gcpro1
;
7608 current
= XCAR (arg
);
7609 workbuf
= XCDR (arg
);
7610 if (! NILP (workbuf
))
7612 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7613 reused_workbuf_in_use
= 0;
7614 else if (! NILP (Fbuffer_live_p (workbuf
)))
7615 Fkill_buffer (workbuf
);
7617 set_buffer_internal (XBUFFER (current
));
7623 code_conversion_save (with_work_buf
, multibyte
)
7624 int with_work_buf
, multibyte
;
7626 Lisp_Object workbuf
= Qnil
;
7629 workbuf
= make_conversion_work_buffer (multibyte
);
7630 record_unwind_protect (code_conversion_restore
,
7631 Fcons (Fcurrent_buffer (), workbuf
));
7636 decode_coding_gap (coding
, chars
, bytes
)
7637 struct coding_system
*coding
;
7638 EMACS_INT chars
, bytes
;
7640 int count
= specpdl_ptr
- specpdl
;
7643 code_conversion_save (0, 0);
7645 coding
->src_object
= Fcurrent_buffer ();
7646 coding
->src_chars
= chars
;
7647 coding
->src_bytes
= bytes
;
7648 coding
->src_pos
= -chars
;
7649 coding
->src_pos_byte
= -bytes
;
7650 coding
->src_multibyte
= chars
< bytes
;
7651 coding
->dst_object
= coding
->src_object
;
7652 coding
->dst_pos
= PT
;
7653 coding
->dst_pos_byte
= PT_BYTE
;
7654 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
7656 if (CODING_REQUIRE_DETECTION (coding
))
7657 detect_coding (coding
);
7659 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7660 current_buffer
->text
->inhibit_shrinking
= 1;
7661 decode_coding (coding
);
7662 current_buffer
->text
->inhibit_shrinking
= 0;
7664 attrs
= CODING_ID_ATTRS (coding
->id
);
7665 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7667 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7670 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7671 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7672 make_number (coding
->produced_char
));
7674 coding
->produced_char
+= Z
- prev_Z
;
7675 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7678 unbind_to (count
, Qnil
);
7679 return coding
->result
;
7683 encode_coding_gap (coding
, chars
, bytes
)
7684 struct coding_system
*coding
;
7685 EMACS_INT chars
, bytes
;
7687 int count
= specpdl_ptr
- specpdl
;
7689 code_conversion_save (0, 0);
7691 coding
->src_object
= Fcurrent_buffer ();
7692 coding
->src_chars
= chars
;
7693 coding
->src_bytes
= bytes
;
7694 coding
->src_pos
= -chars
;
7695 coding
->src_pos_byte
= -bytes
;
7696 coding
->src_multibyte
= chars
< bytes
;
7697 coding
->dst_object
= coding
->src_object
;
7698 coding
->dst_pos
= PT
;
7699 coding
->dst_pos_byte
= PT_BYTE
;
7701 encode_coding (coding
);
7703 unbind_to (count
, Qnil
);
7704 return coding
->result
;
7708 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7709 SRC_OBJECT into DST_OBJECT by coding context CODING.
7711 SRC_OBJECT is a buffer, a string, or Qnil.
7713 If it is a buffer, the text is at point of the buffer. FROM and TO
7714 are positions in the buffer.
7716 If it is a string, the text is at the beginning of the string.
7717 FROM and TO are indices to the string.
7719 If it is nil, the text is at coding->source. FROM and TO are
7720 indices to coding->source.
7722 DST_OBJECT is a buffer, Qt, or Qnil.
7724 If it is a buffer, the decoded text is inserted at point of the
7725 buffer. If the buffer is the same as SRC_OBJECT, the source text
7728 If it is Qt, a string is made from the decoded text, and
7729 set in CODING->dst_object.
7731 If it is Qnil, the decoded text is stored at CODING->destination.
7732 The caller must allocate CODING->dst_bytes bytes at
7733 CODING->destination by xmalloc. If the decoded text is longer than
7734 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7738 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7740 struct coding_system
*coding
;
7741 Lisp_Object src_object
;
7742 EMACS_INT from
, from_byte
, to
, to_byte
;
7743 Lisp_Object dst_object
;
7745 int count
= specpdl_ptr
- specpdl
;
7746 unsigned char *destination
;
7747 EMACS_INT dst_bytes
;
7748 EMACS_INT chars
= to
- from
;
7749 EMACS_INT bytes
= to_byte
- from_byte
;
7751 int saved_pt
= -1, saved_pt_byte
;
7752 int need_marker_adjustment
= 0;
7753 Lisp_Object old_deactivate_mark
;
7755 old_deactivate_mark
= Vdeactivate_mark
;
7757 if (NILP (dst_object
))
7759 destination
= coding
->destination
;
7760 dst_bytes
= coding
->dst_bytes
;
7763 coding
->src_object
= src_object
;
7764 coding
->src_chars
= chars
;
7765 coding
->src_bytes
= bytes
;
7766 coding
->src_multibyte
= chars
< bytes
;
7768 if (STRINGP (src_object
))
7770 coding
->src_pos
= from
;
7771 coding
->src_pos_byte
= from_byte
;
7773 else if (BUFFERP (src_object
))
7775 set_buffer_internal (XBUFFER (src_object
));
7777 move_gap_both (from
, from_byte
);
7778 if (EQ (src_object
, dst_object
))
7780 struct Lisp_Marker
*tail
;
7782 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7784 tail
->need_adjustment
7785 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7786 need_marker_adjustment
|= tail
->need_adjustment
;
7788 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7789 TEMP_SET_PT_BOTH (from
, from_byte
);
7790 current_buffer
->text
->inhibit_shrinking
= 1;
7791 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7792 coding
->src_pos
= -chars
;
7793 coding
->src_pos_byte
= -bytes
;
7797 coding
->src_pos
= from
;
7798 coding
->src_pos_byte
= from_byte
;
7802 if (CODING_REQUIRE_DETECTION (coding
))
7803 detect_coding (coding
);
7804 attrs
= CODING_ID_ATTRS (coding
->id
);
7806 if (EQ (dst_object
, Qt
)
7807 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7808 && NILP (dst_object
)))
7810 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7811 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7812 coding
->dst_pos
= BEG
;
7813 coding
->dst_pos_byte
= BEG_BYTE
;
7815 else if (BUFFERP (dst_object
))
7817 code_conversion_save (0, 0);
7818 coding
->dst_object
= dst_object
;
7819 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7820 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7821 coding
->dst_multibyte
7822 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7826 code_conversion_save (0, 0);
7827 coding
->dst_object
= Qnil
;
7828 /* Most callers presume this will return a multibyte result, and they
7829 won't use `binary' or `raw-text' anyway, so let's not worry about
7830 CODING_FOR_UNIBYTE. */
7831 coding
->dst_multibyte
= 1;
7834 decode_coding (coding
);
7836 if (BUFFERP (coding
->dst_object
))
7837 set_buffer_internal (XBUFFER (coding
->dst_object
));
7839 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7841 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7842 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7845 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7846 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7847 old_deactivate_mark
);
7848 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7849 make_number (coding
->produced_char
));
7852 coding
->produced_char
+= Z
- prev_Z
;
7853 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7856 if (EQ (dst_object
, Qt
))
7858 coding
->dst_object
= Fbuffer_string ();
7860 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7862 set_buffer_internal (XBUFFER (coding
->dst_object
));
7863 if (dst_bytes
< coding
->produced
)
7865 destination
= xrealloc (destination
, coding
->produced
);
7868 record_conversion_result (coding
,
7869 CODING_RESULT_INSUFFICIENT_DST
);
7870 unbind_to (count
, Qnil
);
7873 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7874 move_gap_both (BEGV
, BEGV_BYTE
);
7875 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
7876 coding
->destination
= destination
;
7882 /* This is the case of:
7883 (BUFFERP (src_object) && EQ (src_object, dst_object))
7884 As we have moved PT while replacing the original buffer
7885 contents, we must recover it now. */
7886 set_buffer_internal (XBUFFER (src_object
));
7887 current_buffer
->text
->inhibit_shrinking
= 0;
7888 if (saved_pt
< from
)
7889 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7890 else if (saved_pt
< from
+ chars
)
7891 TEMP_SET_PT_BOTH (from
, from_byte
);
7892 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7893 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7894 saved_pt_byte
+ (coding
->produced
- bytes
));
7896 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7897 saved_pt_byte
+ (coding
->produced
- bytes
));
7899 if (need_marker_adjustment
)
7901 struct Lisp_Marker
*tail
;
7903 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7904 if (tail
->need_adjustment
)
7906 tail
->need_adjustment
= 0;
7907 if (tail
->insertion_type
)
7909 tail
->bytepos
= from_byte
;
7910 tail
->charpos
= from
;
7914 tail
->bytepos
= from_byte
+ coding
->produced
;
7916 = (NILP (current_buffer
->enable_multibyte_characters
)
7917 ? tail
->bytepos
: from
+ coding
->produced_char
);
7923 Vdeactivate_mark
= old_deactivate_mark
;
7924 unbind_to (count
, coding
->dst_object
);
7929 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7931 struct coding_system
*coding
;
7932 Lisp_Object src_object
;
7933 EMACS_INT from
, from_byte
, to
, to_byte
;
7934 Lisp_Object dst_object
;
7936 int count
= specpdl_ptr
- specpdl
;
7937 EMACS_INT chars
= to
- from
;
7938 EMACS_INT bytes
= to_byte
- from_byte
;
7940 int saved_pt
= -1, saved_pt_byte
;
7941 int need_marker_adjustment
= 0;
7942 int kill_src_buffer
= 0;
7943 Lisp_Object old_deactivate_mark
;
7945 old_deactivate_mark
= Vdeactivate_mark
;
7947 coding
->src_object
= src_object
;
7948 coding
->src_chars
= chars
;
7949 coding
->src_bytes
= bytes
;
7950 coding
->src_multibyte
= chars
< bytes
;
7952 attrs
= CODING_ID_ATTRS (coding
->id
);
7954 if (EQ (src_object
, dst_object
))
7956 struct Lisp_Marker
*tail
;
7958 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7960 tail
->need_adjustment
7961 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7962 need_marker_adjustment
|= tail
->need_adjustment
;
7966 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7968 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7969 set_buffer_internal (XBUFFER (coding
->src_object
));
7970 if (STRINGP (src_object
))
7971 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7972 else if (BUFFERP (src_object
))
7973 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7975 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7977 if (EQ (src_object
, dst_object
))
7979 set_buffer_internal (XBUFFER (src_object
));
7980 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7981 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7982 set_buffer_internal (XBUFFER (coding
->src_object
));
7986 Lisp_Object args
[3];
7987 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7989 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7990 old_deactivate_mark
);
7991 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7992 args
[1] = make_number (BEG
);
7993 args
[2] = make_number (Z
);
7994 safe_call (3, args
);
7997 if (XBUFFER (coding
->src_object
) != current_buffer
)
7998 kill_src_buffer
= 1;
7999 coding
->src_object
= Fcurrent_buffer ();
8001 move_gap_both (BEG
, BEG_BYTE
);
8002 coding
->src_chars
= Z
- BEG
;
8003 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
8004 coding
->src_pos
= BEG
;
8005 coding
->src_pos_byte
= BEG_BYTE
;
8006 coding
->src_multibyte
= Z
< Z_BYTE
;
8008 else if (STRINGP (src_object
))
8010 code_conversion_save (0, 0);
8011 coding
->src_pos
= from
;
8012 coding
->src_pos_byte
= from_byte
;
8014 else if (BUFFERP (src_object
))
8016 code_conversion_save (0, 0);
8017 set_buffer_internal (XBUFFER (src_object
));
8018 if (EQ (src_object
, dst_object
))
8020 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
8021 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
8022 coding
->src_pos
= 0;
8023 coding
->src_pos_byte
= 0;
8027 if (from
< GPT
&& to
>= GPT
)
8028 move_gap_both (from
, from_byte
);
8029 coding
->src_pos
= from
;
8030 coding
->src_pos_byte
= from_byte
;
8034 code_conversion_save (0, 0);
8036 if (BUFFERP (dst_object
))
8038 coding
->dst_object
= dst_object
;
8039 if (EQ (src_object
, dst_object
))
8041 coding
->dst_pos
= from
;
8042 coding
->dst_pos_byte
= from_byte
;
8046 struct buffer
*current
= current_buffer
;
8048 set_buffer_temp (XBUFFER (dst_object
));
8049 coding
->dst_pos
= PT
;
8050 coding
->dst_pos_byte
= PT_BYTE
;
8051 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
8052 set_buffer_temp (current
);
8054 coding
->dst_multibyte
8055 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
8057 else if (EQ (dst_object
, Qt
))
8059 coding
->dst_object
= Qnil
;
8060 coding
->dst_bytes
= coding
->src_chars
;
8061 if (coding
->dst_bytes
== 0)
8062 coding
->dst_bytes
= 1;
8063 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
8064 coding
->dst_multibyte
= 0;
8068 coding
->dst_object
= Qnil
;
8069 coding
->dst_multibyte
= 0;
8072 encode_coding (coding
);
8074 if (EQ (dst_object
, Qt
))
8076 if (BUFFERP (coding
->dst_object
))
8077 coding
->dst_object
= Fbuffer_string ();
8081 = make_unibyte_string ((char *) coding
->destination
,
8083 xfree (coding
->destination
);
8089 /* This is the case of:
8090 (BUFFERP (src_object) && EQ (src_object, dst_object))
8091 As we have moved PT while replacing the original buffer
8092 contents, we must recover it now. */
8093 set_buffer_internal (XBUFFER (src_object
));
8094 if (saved_pt
< from
)
8095 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8096 else if (saved_pt
< from
+ chars
)
8097 TEMP_SET_PT_BOTH (from
, from_byte
);
8098 else if (! NILP (current_buffer
->enable_multibyte_characters
))
8099 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8100 saved_pt_byte
+ (coding
->produced
- bytes
));
8102 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8103 saved_pt_byte
+ (coding
->produced
- bytes
));
8105 if (need_marker_adjustment
)
8107 struct Lisp_Marker
*tail
;
8109 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8110 if (tail
->need_adjustment
)
8112 tail
->need_adjustment
= 0;
8113 if (tail
->insertion_type
)
8115 tail
->bytepos
= from_byte
;
8116 tail
->charpos
= from
;
8120 tail
->bytepos
= from_byte
+ coding
->produced
;
8122 = (NILP (current_buffer
->enable_multibyte_characters
)
8123 ? tail
->bytepos
: from
+ coding
->produced_char
);
8129 if (kill_src_buffer
)
8130 Fkill_buffer (coding
->src_object
);
8132 Vdeactivate_mark
= old_deactivate_mark
;
8133 unbind_to (count
, Qnil
);
8138 preferred_coding_system ()
8140 int id
= coding_categories
[coding_priorities
[0]].id
;
8142 return CODING_ID_NAME (id
);
8147 /*** 8. Emacs Lisp library functions ***/
8149 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8150 doc
: /* Return t if OBJECT is nil or a coding-system.
8151 See the documentation of `define-coding-system' for information
8152 about coding-system objects. */)
8157 || CODING_SYSTEM_ID (object
) >= 0)
8159 if (! SYMBOLP (object
)
8160 || NILP (Fget (object
, Qcoding_system_define_form
)))
8165 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8166 Sread_non_nil_coding_system
, 1, 1, 0,
8167 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8174 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8175 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8177 while (SCHARS (val
) == 0);
8178 return (Fintern (val
, Qnil
));
8181 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8182 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8183 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8184 Ignores case when completing coding systems (all Emacs coding systems
8185 are lower-case). */)
8186 (prompt
, default_coding_system
)
8187 Lisp_Object prompt
, default_coding_system
;
8190 int count
= SPECPDL_INDEX ();
8192 if (SYMBOLP (default_coding_system
))
8193 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8194 specbind (Qcompletion_ignore_case
, Qt
);
8195 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8196 Qt
, Qnil
, Qcoding_system_history
,
8197 default_coding_system
, Qnil
);
8198 unbind_to (count
, Qnil
);
8199 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8202 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8204 doc
: /* Check validity of CODING-SYSTEM.
8205 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8206 It is valid if it is nil or a symbol defined as a coding system by the
8207 function `define-coding-system'. */)
8209 Lisp_Object coding_system
;
8211 Lisp_Object define_form
;
8213 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8214 if (! NILP (define_form
))
8216 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8217 safe_eval (define_form
);
8219 if (!NILP (Fcoding_system_p (coding_system
)))
8220 return coding_system
;
8221 xsignal1 (Qcoding_system_error
, coding_system
);
8225 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8226 HIGHEST is nonzero, return the coding system of the highest
8227 priority among the detected coding systems. Otherwize return a
8228 list of detected coding systems sorted by their priorities. If
8229 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8230 multibyte form but contains only ASCII and eight-bit chars.
8231 Otherwise, the bytes are raw bytes.
8233 CODING-SYSTEM controls the detection as below:
8235 If it is nil, detect both text-format and eol-format. If the
8236 text-format part of CODING-SYSTEM is already specified
8237 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8238 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8239 detect only text-format. */
8242 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
8244 const unsigned char *src
;
8245 EMACS_INT src_chars
, src_bytes
;
8248 Lisp_Object coding_system
;
8250 const unsigned char *src_end
= src
+ src_bytes
;
8251 Lisp_Object attrs
, eol_type
;
8252 Lisp_Object val
= Qnil
;
8253 struct coding_system coding
;
8255 struct coding_detection_info detect_info
;
8256 enum coding_category base_category
;
8257 int null_byte_found
= 0, eight_bit_found
= 0;
8259 if (NILP (coding_system
))
8260 coding_system
= Qundecided
;
8261 setup_coding_system (coding_system
, &coding
);
8262 attrs
= CODING_ID_ATTRS (coding
.id
);
8263 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8264 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8266 coding
.source
= src
;
8267 coding
.src_chars
= src_chars
;
8268 coding
.src_bytes
= src_bytes
;
8269 coding
.src_multibyte
= multibytep
;
8270 coding
.consumed
= 0;
8271 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8272 coding
.head_ascii
= 0;
8274 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8276 /* At first, detect text-format if necessary. */
8277 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8278 if (base_category
== coding_category_undecided
)
8280 enum coding_category category
;
8281 struct coding_system
*this;
8284 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8285 for (; src
< src_end
; src
++)
8290 eight_bit_found
= 1;
8291 if (null_byte_found
)
8296 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8297 && ! inhibit_iso_escape_detection
8298 && ! detect_info
.checked
)
8300 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8302 /* We have scanned the whole data. */
8303 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8305 /* We didn't find an 8-bit code. We may
8306 have found a null-byte, but it's very
8307 rare that a binary file confirm to
8310 coding
.head_ascii
= src
- coding
.source
;
8312 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8316 else if (! c
&& !inhibit_null_byte_detection
)
8318 null_byte_found
= 1;
8319 if (eight_bit_found
)
8322 if (! eight_bit_found
)
8323 coding
.head_ascii
++;
8325 else if (! eight_bit_found
)
8326 coding
.head_ascii
++;
8329 if (null_byte_found
|| eight_bit_found
8330 || coding
.head_ascii
< coding
.src_bytes
8331 || detect_info
.found
)
8333 if (coding
.head_ascii
== coding
.src_bytes
)
8334 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8335 for (i
= 0; i
< coding_category_raw_text
; i
++)
8337 category
= coding_priorities
[i
];
8338 this = coding_categories
+ category
;
8339 if (detect_info
.found
& (1 << category
))
8344 if (null_byte_found
)
8346 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8347 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8349 for (i
= 0; i
< coding_category_raw_text
; i
++)
8351 category
= coding_priorities
[i
];
8352 this = coding_categories
+ category
;
8356 /* No coding system of this category is defined. */
8357 detect_info
.rejected
|= (1 << category
);
8359 else if (category
>= coding_category_raw_text
)
8361 else if (detect_info
.checked
& (1 << category
))
8364 && (detect_info
.found
& (1 << category
)))
8367 else if ((*(this->detector
)) (&coding
, &detect_info
)
8369 && (detect_info
.found
& (1 << category
)))
8371 if (category
== coding_category_utf_16_auto
)
8373 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8374 category
= coding_category_utf_16_le
;
8376 category
= coding_category_utf_16_be
;
8384 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8387 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8388 id
= CODING_SYSTEM_ID (Qno_conversion
);
8389 val
= Fcons (make_number (id
), Qnil
);
8391 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8393 detect_info
.found
= CATEGORY_MASK_ANY
;
8394 id
= coding_categories
[coding_category_undecided
].id
;
8395 val
= Fcons (make_number (id
), Qnil
);
8399 if (detect_info
.found
)
8401 detect_info
.found
= 1 << category
;
8402 val
= Fcons (make_number (this->id
), Qnil
);
8405 for (i
= 0; i
< coding_category_raw_text
; i
++)
8406 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8408 detect_info
.found
= 1 << coding_priorities
[i
];
8409 id
= coding_categories
[coding_priorities
[i
]].id
;
8410 val
= Fcons (make_number (id
), Qnil
);
8416 int mask
= detect_info
.rejected
| detect_info
.found
;
8419 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8421 category
= coding_priorities
[i
];
8422 if (! (mask
& (1 << category
)))
8424 found
|= 1 << category
;
8425 id
= coding_categories
[category
].id
;
8427 val
= Fcons (make_number (id
), val
);
8430 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8432 category
= coding_priorities
[i
];
8433 if (detect_info
.found
& (1 << category
))
8435 id
= coding_categories
[category
].id
;
8436 val
= Fcons (make_number (id
), val
);
8439 detect_info
.found
|= found
;
8442 else if (base_category
== coding_category_utf_8_auto
)
8444 if (detect_coding_utf_8 (&coding
, &detect_info
))
8446 struct coding_system
*this;
8448 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8449 this = coding_categories
+ coding_category_utf_8_sig
;
8451 this = coding_categories
+ coding_category_utf_8_nosig
;
8452 val
= Fcons (make_number (this->id
), Qnil
);
8455 else if (base_category
== coding_category_utf_16_auto
)
8457 if (detect_coding_utf_16 (&coding
, &detect_info
))
8459 struct coding_system
*this;
8461 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8462 this = coding_categories
+ coding_category_utf_16_le
;
8463 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8464 this = coding_categories
+ coding_category_utf_16_be
;
8465 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8466 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8468 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8469 val
= Fcons (make_number (this->id
), Qnil
);
8474 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8475 val
= Fcons (make_number (coding
.id
), Qnil
);
8478 /* Then, detect eol-format if necessary. */
8480 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8483 if (VECTORP (eol_type
))
8485 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8487 if (null_byte_found
)
8488 normal_eol
= EOL_SEEN_LF
;
8490 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8491 coding_category_raw_text
);
8493 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8494 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8495 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8496 coding_category_utf_16_be
);
8497 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8498 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8499 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8500 coding_category_utf_16_le
);
8504 if (EQ (eol_type
, Qunix
))
8505 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8506 else if (EQ (eol_type
, Qdos
))
8507 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8509 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8512 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8514 enum coding_category category
;
8517 id
= XINT (XCAR (tail
));
8518 attrs
= CODING_ID_ATTRS (id
);
8519 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8520 eol_type
= CODING_ID_EOL_TYPE (id
);
8521 if (VECTORP (eol_type
))
8523 if (category
== coding_category_utf_16_be
8524 || category
== coding_category_utf_16_be_nosig
)
8525 this_eol
= utf_16_be_eol
;
8526 else if (category
== coding_category_utf_16_le
8527 || category
== coding_category_utf_16_le_nosig
)
8528 this_eol
= utf_16_le_eol
;
8530 this_eol
= normal_eol
;
8532 if (this_eol
== EOL_SEEN_LF
)
8533 XSETCAR (tail
, AREF (eol_type
, 0));
8534 else if (this_eol
== EOL_SEEN_CRLF
)
8535 XSETCAR (tail
, AREF (eol_type
, 1));
8536 else if (this_eol
== EOL_SEEN_CR
)
8537 XSETCAR (tail
, AREF (eol_type
, 2));
8539 XSETCAR (tail
, CODING_ID_NAME (id
));
8542 XSETCAR (tail
, CODING_ID_NAME (id
));
8546 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8550 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8552 doc
: /* Detect coding system of the text in the region between START and END.
8553 Return a list of possible coding systems ordered by priority.
8554 The coding systems to try and their priorities follows what
8555 the function `coding-system-priority-list' (which see) returns.
8557 If only ASCII characters are found (except for such ISO-2022 control
8558 characters as ESC), it returns a list of single element `undecided'
8559 or its subsidiary coding system according to a detected end-of-line
8562 If optional argument HIGHEST is non-nil, return the coding system of
8563 highest priority. */)
8564 (start
, end
, highest
)
8565 Lisp_Object start
, end
, highest
;
8568 int from_byte
, to_byte
;
8570 CHECK_NUMBER_COERCE_MARKER (start
);
8571 CHECK_NUMBER_COERCE_MARKER (end
);
8573 validate_region (&start
, &end
);
8574 from
= XINT (start
), to
= XINT (end
);
8575 from_byte
= CHAR_TO_BYTE (from
);
8576 to_byte
= CHAR_TO_BYTE (to
);
8578 if (from
< GPT
&& to
>= GPT
)
8579 move_gap_both (to
, to_byte
);
8581 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8582 to
- from
, to_byte
- from_byte
,
8584 !NILP (current_buffer
8585 ->enable_multibyte_characters
),
8589 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8591 doc
: /* Detect coding system of the text in STRING.
8592 Return a list of possible coding systems ordered by priority.
8593 The coding systems to try and their priorities follows what
8594 the function `coding-system-priority-list' (which see) returns.
8596 If only ASCII characters are found (except for such ISO-2022 control
8597 characters as ESC), it returns a list of single element `undecided'
8598 or its subsidiary coding system according to a detected end-of-line
8601 If optional argument HIGHEST is non-nil, return the coding system of
8602 highest priority. */)
8604 Lisp_Object string
, highest
;
8606 CHECK_STRING (string
);
8608 return detect_coding_system (SDATA (string
),
8609 SCHARS (string
), SBYTES (string
),
8610 !NILP (highest
), STRING_MULTIBYTE (string
),
8616 char_encodable_p (c
, attrs
)
8621 struct charset
*charset
;
8622 Lisp_Object translation_table
;
8624 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8625 if (! NILP (translation_table
))
8626 c
= translate_char (translation_table
, c
);
8627 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8628 CONSP (tail
); tail
= XCDR (tail
))
8630 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8631 if (CHAR_CHARSET_P (c
, charset
))
8634 return (! NILP (tail
));
8638 /* Return a list of coding systems that safely encode the text between
8639 START and END. If EXCLUDE is non-nil, it is a list of coding
8640 systems not to check. The returned list doesn't contain any such
8641 coding systems. In any case, if the text contains only ASCII or is
8642 unibyte, return t. */
8644 DEFUN ("find-coding-systems-region-internal",
8645 Ffind_coding_systems_region_internal
,
8646 Sfind_coding_systems_region_internal
, 2, 3, 0,
8647 doc
: /* Internal use only. */)
8648 (start
, end
, exclude
)
8649 Lisp_Object start
, end
, exclude
;
8651 Lisp_Object coding_attrs_list
, safe_codings
;
8652 EMACS_INT start_byte
, end_byte
;
8653 const unsigned char *p
, *pbeg
, *pend
;
8655 Lisp_Object tail
, elt
, work_table
;
8657 if (STRINGP (start
))
8659 if (!STRING_MULTIBYTE (start
)
8660 || SCHARS (start
) == SBYTES (start
))
8663 end_byte
= SBYTES (start
);
8667 CHECK_NUMBER_COERCE_MARKER (start
);
8668 CHECK_NUMBER_COERCE_MARKER (end
);
8669 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8670 args_out_of_range (start
, end
);
8671 if (NILP (current_buffer
->enable_multibyte_characters
))
8673 start_byte
= CHAR_TO_BYTE (XINT (start
));
8674 end_byte
= CHAR_TO_BYTE (XINT (end
));
8675 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8678 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8680 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8681 move_gap_both (XINT (start
), start_byte
);
8683 move_gap_both (XINT (end
), end_byte
);
8687 coding_attrs_list
= Qnil
;
8688 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8690 || NILP (Fmemq (XCAR (tail
), exclude
)))
8694 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8695 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8696 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8698 ASET (attrs
, coding_attr_trans_tbl
,
8699 get_translation_table (attrs
, 1, NULL
));
8700 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8704 if (STRINGP (start
))
8705 p
= pbeg
= SDATA (start
);
8707 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8708 pend
= p
+ (end_byte
- start_byte
);
8710 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8711 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8713 work_table
= Fmake_char_table (Qnil
, Qnil
);
8716 if (ASCII_BYTE_P (*p
))
8720 c
= STRING_CHAR_ADVANCE (p
);
8721 if (!NILP (char_table_ref (work_table
, c
)))
8722 /* This character was already checked. Ignore it. */
8725 charset_map_loaded
= 0;
8726 for (tail
= coding_attrs_list
; CONSP (tail
);)
8731 else if (char_encodable_p (c
, elt
))
8733 else if (CONSP (XCDR (tail
)))
8735 XSETCAR (tail
, XCAR (XCDR (tail
)));
8736 XSETCDR (tail
, XCDR (XCDR (tail
)));
8740 XSETCAR (tail
, Qnil
);
8744 if (charset_map_loaded
)
8746 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8748 if (STRINGP (start
))
8749 pbeg
= SDATA (start
);
8751 pbeg
= BYTE_POS_ADDR (start_byte
);
8752 p
= pbeg
+ p_offset
;
8753 pend
= pbeg
+ pend_offset
;
8755 char_table_set (work_table
, c
, Qt
);
8759 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8760 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8761 if (! NILP (XCAR (tail
)))
8762 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8764 return safe_codings
;
8768 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8769 Sunencodable_char_position
, 3, 5, 0,
8771 Return position of first un-encodable character in a region.
8772 START and END specify the region and CODING-SYSTEM specifies the
8773 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8775 If optional 4th argument COUNT is non-nil, it specifies at most how
8776 many un-encodable characters to search. In this case, the value is a
8779 If optional 5th argument STRING is non-nil, it is a string to search
8780 for un-encodable characters. In that case, START and END are indexes
8782 (start
, end
, coding_system
, count
, string
)
8783 Lisp_Object start
, end
, coding_system
, count
, string
;
8786 struct coding_system coding
;
8787 Lisp_Object attrs
, charset_list
, translation_table
;
8788 Lisp_Object positions
;
8790 const unsigned char *p
, *stop
, *pend
;
8791 int ascii_compatible
;
8793 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8794 attrs
= CODING_ID_ATTRS (coding
.id
);
8795 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8797 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8798 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8799 translation_table
= get_translation_table (attrs
, 1, NULL
);
8803 validate_region (&start
, &end
);
8804 from
= XINT (start
);
8806 if (NILP (current_buffer
->enable_multibyte_characters
)
8807 || (ascii_compatible
8808 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8810 p
= CHAR_POS_ADDR (from
);
8811 pend
= CHAR_POS_ADDR (to
);
8812 if (from
< GPT
&& to
>= GPT
)
8819 CHECK_STRING (string
);
8820 CHECK_NATNUM (start
);
8822 from
= XINT (start
);
8825 || to
> SCHARS (string
))
8826 args_out_of_range_3 (string
, start
, end
);
8827 if (! STRING_MULTIBYTE (string
))
8829 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8830 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8831 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8839 CHECK_NATNUM (count
);
8848 if (ascii_compatible
)
8849 while (p
< stop
&& ASCII_BYTE_P (*p
))
8859 c
= STRING_CHAR_ADVANCE (p
);
8860 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8861 && ! char_charset (translate_char (translation_table
, c
),
8862 charset_list
, NULL
))
8864 positions
= Fcons (make_number (from
), positions
);
8873 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8877 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8878 Scheck_coding_systems_region
, 3, 3, 0,
8879 doc
: /* Check if the region is encodable by coding systems.
8881 START and END are buffer positions specifying the region.
8882 CODING-SYSTEM-LIST is a list of coding systems to check.
8884 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8885 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8886 whole region, POS0, POS1, ... are buffer positions where non-encodable
8887 characters are found.
8889 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8892 START may be a string. In that case, check if the string is
8893 encodable, and the value contains indices to the string instead of
8894 buffer positions. END is ignored.
8896 If the current buffer (or START if it is a string) is unibyte, the value
8898 (start
, end
, coding_system_list
)
8899 Lisp_Object start
, end
, coding_system_list
;
8902 EMACS_INT start_byte
, end_byte
;
8904 const unsigned char *p
, *pbeg
, *pend
;
8906 Lisp_Object tail
, elt
, attrs
;
8908 if (STRINGP (start
))
8910 if (!STRING_MULTIBYTE (start
)
8911 || SCHARS (start
) == SBYTES (start
))
8914 end_byte
= SBYTES (start
);
8919 CHECK_NUMBER_COERCE_MARKER (start
);
8920 CHECK_NUMBER_COERCE_MARKER (end
);
8921 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8922 args_out_of_range (start
, end
);
8923 if (NILP (current_buffer
->enable_multibyte_characters
))
8925 start_byte
= CHAR_TO_BYTE (XINT (start
));
8926 end_byte
= CHAR_TO_BYTE (XINT (end
));
8927 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8930 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8932 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8933 move_gap_both (XINT (start
), start_byte
);
8935 move_gap_both (XINT (end
), end_byte
);
8941 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8944 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8945 ASET (attrs
, coding_attr_trans_tbl
,
8946 get_translation_table (attrs
, 1, NULL
));
8947 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8950 if (STRINGP (start
))
8951 p
= pbeg
= SDATA (start
);
8953 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8954 pend
= p
+ (end_byte
- start_byte
);
8956 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8957 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8961 if (ASCII_BYTE_P (*p
))
8965 c
= STRING_CHAR_ADVANCE (p
);
8967 charset_map_loaded
= 0;
8968 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8970 elt
= XCDR (XCAR (tail
));
8971 if (! char_encodable_p (c
, XCAR (elt
)))
8972 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8974 if (charset_map_loaded
)
8976 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8978 if (STRINGP (start
))
8979 pbeg
= SDATA (start
);
8981 pbeg
= BYTE_POS_ADDR (start_byte
);
8982 p
= pbeg
+ p_offset
;
8983 pend
= pbeg
+ pend_offset
;
8991 for (; CONSP (tail
); tail
= XCDR (tail
))
8994 if (CONSP (XCDR (XCDR (elt
))))
8995 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
9004 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
9005 Lisp_Object start
, end
, coding_system
, dst_object
;
9006 int encodep
, norecord
;
9008 struct coding_system coding
;
9009 EMACS_INT from
, from_byte
, to
, to_byte
;
9010 Lisp_Object src_object
;
9012 CHECK_NUMBER_COERCE_MARKER (start
);
9013 CHECK_NUMBER_COERCE_MARKER (end
);
9014 if (NILP (coding_system
))
9015 coding_system
= Qno_conversion
;
9017 CHECK_CODING_SYSTEM (coding_system
);
9018 src_object
= Fcurrent_buffer ();
9019 if (NILP (dst_object
))
9020 dst_object
= src_object
;
9021 else if (! EQ (dst_object
, Qt
))
9022 CHECK_BUFFER (dst_object
);
9024 validate_region (&start
, &end
);
9025 from
= XFASTINT (start
);
9026 from_byte
= CHAR_TO_BYTE (from
);
9027 to
= XFASTINT (end
);
9028 to_byte
= CHAR_TO_BYTE (to
);
9030 setup_coding_system (coding_system
, &coding
);
9031 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9034 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
9037 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
9040 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9042 return (BUFFERP (dst_object
)
9043 ? make_number (coding
.produced_char
)
9044 : coding
.dst_object
);
9048 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
9049 3, 4, "r\nzCoding system: ",
9050 doc
: /* Decode the current region from the specified coding system.
9051 When called from a program, takes four arguments:
9052 START, END, CODING-SYSTEM, and DESTINATION.
9053 START and END are buffer positions.
9055 Optional 4th arguments DESTINATION specifies where the decoded text goes.
9056 If nil, the region between START and END is replaced by the decoded text.
9057 If buffer, the decoded text is inserted in that buffer after point (point
9059 In those cases, the length of the decoded text is returned.
9060 If DESTINATION is t, the decoded text is returned.
9062 This function sets `last-coding-system-used' to the precise coding system
9063 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9064 not fully specified.) */)
9065 (start
, end
, coding_system
, destination
)
9066 Lisp_Object start
, end
, coding_system
, destination
;
9068 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
9071 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
9072 3, 4, "r\nzCoding system: ",
9073 doc
: /* Encode the current region by specified coding system.
9074 When called from a program, takes four arguments:
9075 START, END, CODING-SYSTEM and DESTINATION.
9076 START and END are buffer positions.
9078 Optional 4th arguments DESTINATION specifies where the encoded text goes.
9079 If nil, the region between START and END is replace by the encoded text.
9080 If buffer, the encoded text is inserted in that buffer after point (point
9082 In those cases, the length of the encoded text is returned.
9083 If DESTINATION is t, the encoded text is returned.
9085 This function sets `last-coding-system-used' to the precise coding system
9086 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9087 not fully specified.) */)
9088 (start
, end
, coding_system
, destination
)
9089 Lisp_Object start
, end
, coding_system
, destination
;
9091 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
9095 code_convert_string (string
, coding_system
, dst_object
,
9096 encodep
, nocopy
, norecord
)
9097 Lisp_Object string
, coding_system
, dst_object
;
9098 int encodep
, nocopy
, norecord
;
9100 struct coding_system coding
;
9101 EMACS_INT chars
, bytes
;
9103 CHECK_STRING (string
);
9104 if (NILP (coding_system
))
9107 Vlast_coding_system_used
= Qno_conversion
;
9108 if (NILP (dst_object
))
9109 return (nocopy
? Fcopy_sequence (string
) : string
);
9112 if (NILP (coding_system
))
9113 coding_system
= Qno_conversion
;
9115 CHECK_CODING_SYSTEM (coding_system
);
9116 if (NILP (dst_object
))
9118 else if (! EQ (dst_object
, Qt
))
9119 CHECK_BUFFER (dst_object
);
9121 setup_coding_system (coding_system
, &coding
);
9122 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9123 chars
= SCHARS (string
);
9124 bytes
= SBYTES (string
);
9126 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9128 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9130 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9132 return (BUFFERP (dst_object
)
9133 ? make_number (coding
.produced_char
)
9134 : coding
.dst_object
);
9138 /* Encode or decode STRING according to CODING_SYSTEM.
9139 Do not set Vlast_coding_system_used.
9141 This function is called only from macros DECODE_FILE and
9142 ENCODE_FILE, thus we ignore character composition. */
9145 code_convert_string_norecord (string
, coding_system
, encodep
)
9146 Lisp_Object string
, coding_system
;
9149 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9153 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9155 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9157 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9158 if the decoding operation is trivial.
9160 Optional fourth arg BUFFER non-nil means that the decoded text is
9161 inserted in that buffer after point (point does not move). In this
9162 case, the return value is the length of the decoded text.
9164 This function sets `last-coding-system-used' to the precise coding system
9165 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9166 not fully specified.) */)
9167 (string
, coding_system
, nocopy
, buffer
)
9168 Lisp_Object string
, coding_system
, nocopy
, buffer
;
9170 return code_convert_string (string
, coding_system
, buffer
,
9171 0, ! NILP (nocopy
), 0);
9174 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9176 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9178 Optional third arg NOCOPY non-nil means it is OK to return STRING
9179 itself if the encoding operation is trivial.
9181 Optional fourth arg BUFFER non-nil means that the encoded text is
9182 inserted in that buffer after point (point does not move). In this
9183 case, the return value is the length of the encoded text.
9185 This function sets `last-coding-system-used' to the precise coding system
9186 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9187 not fully specified.) */)
9188 (string
, coding_system
, nocopy
, buffer
)
9189 Lisp_Object string
, coding_system
, nocopy
, buffer
;
9191 return code_convert_string (string
, coding_system
, buffer
,
9192 1, ! NILP (nocopy
), 1);
9196 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9197 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9198 Return the corresponding character. */)
9202 Lisp_Object spec
, attrs
, val
;
9203 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9206 CHECK_NATNUM (code
);
9207 c
= XFASTINT (code
);
9208 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9209 attrs
= AREF (spec
, 0);
9211 if (ASCII_BYTE_P (c
)
9212 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9215 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9216 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9217 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9218 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9221 charset
= charset_roman
;
9222 else if (c
>= 0xA0 && c
< 0xDF)
9224 charset
= charset_kana
;
9229 int s1
= c
>> 8, s2
= c
& 0xFF;
9231 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
9232 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
9233 error ("Invalid code: %d", code
);
9235 charset
= charset_kanji
;
9237 c
= DECODE_CHAR (charset
, c
);
9239 error ("Invalid code: %d", code
);
9240 return make_number (c
);
9244 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9245 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9246 Return the corresponding code in SJIS. */)
9250 Lisp_Object spec
, attrs
, charset_list
;
9252 struct charset
*charset
;
9255 CHECK_CHARACTER (ch
);
9257 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9258 attrs
= AREF (spec
, 0);
9260 if (ASCII_CHAR_P (c
)
9261 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9264 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9265 charset
= char_charset (c
, charset_list
, &code
);
9266 if (code
== CHARSET_INVALID_CODE (charset
))
9267 error ("Can't encode by shift_jis encoding: %d", c
);
9270 return make_number (code
);
9273 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9274 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9275 Return the corresponding character. */)
9279 Lisp_Object spec
, attrs
, val
;
9280 struct charset
*charset_roman
, *charset_big5
, *charset
;
9283 CHECK_NATNUM (code
);
9284 c
= XFASTINT (code
);
9285 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9286 attrs
= AREF (spec
, 0);
9288 if (ASCII_BYTE_P (c
)
9289 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9292 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9293 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9294 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9297 charset
= charset_roman
;
9300 int b1
= c
>> 8, b2
= c
& 0x7F;
9301 if (b1
< 0xA1 || b1
> 0xFE
9302 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9303 error ("Invalid code: %d", code
);
9304 charset
= charset_big5
;
9306 c
= DECODE_CHAR (charset
, (unsigned )c
);
9308 error ("Invalid code: %d", code
);
9309 return make_number (c
);
9312 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9313 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9314 Return the corresponding character code in Big5. */)
9318 Lisp_Object spec
, attrs
, charset_list
;
9319 struct charset
*charset
;
9323 CHECK_CHARACTER (ch
);
9325 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9326 attrs
= AREF (spec
, 0);
9327 if (ASCII_CHAR_P (c
)
9328 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9331 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9332 charset
= char_charset (c
, charset_list
, &code
);
9333 if (code
== CHARSET_INVALID_CODE (charset
))
9334 error ("Can't encode by Big5 encoding: %d", c
);
9336 return make_number (code
);
9340 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9341 Sset_terminal_coding_system_internal
, 1, 2, 0,
9342 doc
: /* Internal use only. */)
9343 (coding_system
, terminal
)
9344 Lisp_Object coding_system
;
9345 Lisp_Object terminal
;
9347 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9348 CHECK_SYMBOL (coding_system
);
9349 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9350 /* We had better not send unsafe characters to terminal. */
9351 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9352 /* Characer composition should be disabled. */
9353 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9354 terminal_coding
->src_multibyte
= 1;
9355 terminal_coding
->dst_multibyte
= 0;
9359 DEFUN ("set-safe-terminal-coding-system-internal",
9360 Fset_safe_terminal_coding_system_internal
,
9361 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9362 doc
: /* Internal use only. */)
9364 Lisp_Object coding_system
;
9366 CHECK_SYMBOL (coding_system
);
9367 setup_coding_system (Fcheck_coding_system (coding_system
),
9368 &safe_terminal_coding
);
9369 /* Characer composition should be disabled. */
9370 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9371 safe_terminal_coding
.src_multibyte
= 1;
9372 safe_terminal_coding
.dst_multibyte
= 0;
9376 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9377 Sterminal_coding_system
, 0, 1, 0,
9378 doc
: /* Return coding system specified for terminal output on the given terminal.
9379 TERMINAL may be a terminal object, a frame, or nil for the selected
9380 frame's terminal device. */)
9382 Lisp_Object terminal
;
9384 struct coding_system
*terminal_coding
9385 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9386 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9388 /* For backward compatibility, return nil if it is `undecided'. */
9389 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9392 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9393 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9394 doc
: /* Internal use only. */)
9395 (coding_system
, terminal
)
9396 Lisp_Object coding_system
;
9397 Lisp_Object terminal
;
9399 struct terminal
*t
= get_terminal (terminal
, 1);
9400 CHECK_SYMBOL (coding_system
);
9401 if (NILP (coding_system
))
9402 coding_system
= Qno_conversion
;
9404 Fcheck_coding_system (coding_system
);
9405 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9406 /* Characer composition should be disabled. */
9407 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9408 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9412 DEFUN ("keyboard-coding-system",
9413 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9414 doc
: /* Return coding system specified for decoding keyboard input. */)
9416 Lisp_Object terminal
;
9418 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9419 (get_terminal (terminal
, 1))->id
);
9423 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9424 Sfind_operation_coding_system
, 1, MANY
, 0,
9425 doc
: /* Choose a coding system for an operation based on the target name.
9426 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9427 DECODING-SYSTEM is the coding system to use for decoding
9428 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9429 for encoding (in case OPERATION does encoding).
9431 The first argument OPERATION specifies an I/O primitive:
9432 For file I/O, `insert-file-contents' or `write-region'.
9433 For process I/O, `call-process', `call-process-region', or `start-process'.
9434 For network I/O, `open-network-stream'.
9436 The remaining arguments should be the same arguments that were passed
9437 to the primitive. Depending on which primitive, one of those arguments
9438 is selected as the TARGET. For example, if OPERATION does file I/O,
9439 whichever argument specifies the file name is TARGET.
9441 TARGET has a meaning which depends on OPERATION:
9442 For file I/O, TARGET is a file name (except for the special case below).
9443 For process I/O, TARGET is a process name.
9444 For network I/O, TARGET is a service name or a port number.
9446 This function looks up what is specified for TARGET in
9447 `file-coding-system-alist', `process-coding-system-alist',
9448 or `network-coding-system-alist' depending on OPERATION.
9449 They may specify a coding system, a cons of coding systems,
9450 or a function symbol to call.
9451 In the last case, we call the function with one argument,
9452 which is a list of all the arguments given to this function.
9453 If the function can't decide a coding system, it can return
9454 `undecided' so that the normal code-detection is performed.
9456 If OPERATION is `insert-file-contents', the argument corresponding to
9457 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9458 file name to look up, and BUFFER is a buffer that contains the file's
9459 contents (not yet decoded). If `file-coding-system-alist' specifies a
9460 function to call for FILENAME, that function should examine the
9461 contents of BUFFER instead of reading the file.
9463 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9468 Lisp_Object operation
, target_idx
, target
, val
;
9469 register Lisp_Object chain
;
9472 error ("Too few arguments");
9473 operation
= args
[0];
9474 if (!SYMBOLP (operation
)
9475 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
9476 error ("Invalid first argument");
9477 if (nargs
< 1 + XINT (target_idx
))
9478 error ("Too few arguments for operation: %s",
9479 SDATA (SYMBOL_NAME (operation
)));
9480 target
= args
[XINT (target_idx
) + 1];
9481 if (!(STRINGP (target
)
9482 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9483 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9484 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9485 error ("Invalid %dth argument", XINT (target_idx
) + 1);
9487 target
= XCAR (target
);
9489 chain
= ((EQ (operation
, Qinsert_file_contents
)
9490 || EQ (operation
, Qwrite_region
))
9491 ? Vfile_coding_system_alist
9492 : (EQ (operation
, Qopen_network_stream
)
9493 ? Vnetwork_coding_system_alist
9494 : Vprocess_coding_system_alist
));
9498 for (; CONSP (chain
); chain
= XCDR (chain
))
9504 && ((STRINGP (target
)
9505 && STRINGP (XCAR (elt
))
9506 && fast_string_match (XCAR (elt
), target
) >= 0)
9507 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9510 /* Here, if VAL is both a valid coding system and a valid
9511 function symbol, we return VAL as a coding system. */
9514 if (! SYMBOLP (val
))
9516 if (! NILP (Fcoding_system_p (val
)))
9517 return Fcons (val
, val
);
9518 if (! NILP (Ffboundp (val
)))
9520 /* We use call1 rather than safe_call1
9521 so as to get bug reports about functions called here
9522 which don't handle the current interface. */
9523 val
= call1 (val
, Flist (nargs
, args
));
9526 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9527 return Fcons (val
, val
);
9535 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9536 Sset_coding_system_priority
, 0, MANY
, 0,
9537 doc
: /* Assign higher priority to the coding systems given as arguments.
9538 If multiple coding systems belong to the same category,
9539 all but the first one are ignored.
9541 usage: (set-coding-system-priority &rest coding-systems) */)
9547 int changed
[coding_category_max
];
9548 enum coding_category priorities
[coding_category_max
];
9550 bzero (changed
, sizeof changed
);
9552 for (i
= j
= 0; i
< nargs
; i
++)
9554 enum coding_category category
;
9555 Lisp_Object spec
, attrs
;
9557 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9558 attrs
= AREF (spec
, 0);
9559 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9560 if (changed
[category
])
9561 /* Ignore this coding system because a coding system of the
9562 same category already had a higher priority. */
9564 changed
[category
] = 1;
9565 priorities
[j
++] = category
;
9566 if (coding_categories
[category
].id
>= 0
9567 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9568 setup_coding_system (args
[i
], &coding_categories
[category
]);
9569 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9572 /* Now we have decided top J priorities. Reflect the order of the
9573 original priorities to the remaining priorities. */
9575 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9577 while (j
< coding_category_max
9578 && changed
[coding_priorities
[j
]])
9580 if (j
== coding_category_max
)
9582 priorities
[i
] = coding_priorities
[j
];
9585 bcopy (priorities
, coding_priorities
, sizeof priorities
);
9587 /* Update `coding-category-list'. */
9588 Vcoding_category_list
= Qnil
;
9589 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9590 Vcoding_category_list
9591 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9592 Vcoding_category_list
);
9597 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9598 Scoding_system_priority_list
, 0, 1, 0,
9599 doc
: /* Return a list of coding systems ordered by their priorities.
9600 The list contains a subset of coding systems; i.e. coding systems
9601 assigned to each coding category (see `coding-category-list').
9603 HIGHESTP non-nil means just return the highest priority one. */)
9605 Lisp_Object highestp
;
9610 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9612 enum coding_category category
= coding_priorities
[i
];
9613 int id
= coding_categories
[category
].id
;
9618 attrs
= CODING_ID_ATTRS (id
);
9619 if (! NILP (highestp
))
9620 return CODING_ATTR_BASE_NAME (attrs
);
9621 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9623 return Fnreverse (val
);
9626 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9629 make_subsidiaries (base
)
9632 Lisp_Object subsidiaries
;
9633 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9634 char *buf
= (char *) alloca (base_name_len
+ 6);
9637 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
9638 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9639 for (i
= 0; i
< 3; i
++)
9641 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
9642 ASET (subsidiaries
, i
, intern (buf
));
9644 return subsidiaries
;
9648 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9649 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9650 doc
: /* For internal use only.
9651 usage: (define-coding-system-internal ...) */)
9657 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9658 Lisp_Object attrs
; /* Vector of attributes. */
9659 Lisp_Object eol_type
;
9660 Lisp_Object aliases
;
9661 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9662 enum coding_category category
;
9663 Lisp_Object tail
, val
;
9664 int max_charset_id
= 0;
9667 if (nargs
< coding_arg_max
)
9670 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9672 name
= args
[coding_arg_name
];
9673 CHECK_SYMBOL (name
);
9674 CODING_ATTR_BASE_NAME (attrs
) = name
;
9676 val
= args
[coding_arg_mnemonic
];
9677 if (! STRINGP (val
))
9678 CHECK_CHARACTER (val
);
9679 CODING_ATTR_MNEMONIC (attrs
) = val
;
9681 coding_type
= args
[coding_arg_coding_type
];
9682 CHECK_SYMBOL (coding_type
);
9683 CODING_ATTR_TYPE (attrs
) = coding_type
;
9685 charset_list
= args
[coding_arg_charset_list
];
9686 if (SYMBOLP (charset_list
))
9688 if (EQ (charset_list
, Qiso_2022
))
9690 if (! EQ (coding_type
, Qiso_2022
))
9691 error ("Invalid charset-list");
9692 charset_list
= Viso_2022_charset_list
;
9694 else if (EQ (charset_list
, Qemacs_mule
))
9696 if (! EQ (coding_type
, Qemacs_mule
))
9697 error ("Invalid charset-list");
9698 charset_list
= Vemacs_mule_charset_list
;
9700 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9701 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9702 max_charset_id
= XFASTINT (XCAR (tail
));
9706 charset_list
= Fcopy_sequence (charset_list
);
9707 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9709 struct charset
*charset
;
9712 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9713 if (EQ (coding_type
, Qiso_2022
)
9714 ? CHARSET_ISO_FINAL (charset
) < 0
9715 : EQ (coding_type
, Qemacs_mule
)
9716 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9718 error ("Can't handle charset `%s'",
9719 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9721 XSETCAR (tail
, make_number (charset
->id
));
9722 if (max_charset_id
< charset
->id
)
9723 max_charset_id
= charset
->id
;
9726 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9728 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9729 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9730 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9731 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9732 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9734 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9736 val
= args
[coding_arg_decode_translation_table
];
9737 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9739 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9741 val
= args
[coding_arg_encode_translation_table
];
9742 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9744 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9746 val
= args
[coding_arg_post_read_conversion
];
9748 CODING_ATTR_POST_READ (attrs
) = val
;
9750 val
= args
[coding_arg_pre_write_conversion
];
9752 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9754 val
= args
[coding_arg_default_char
];
9756 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9759 CHECK_CHARACTER (val
);
9760 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9763 val
= args
[coding_arg_for_unibyte
];
9764 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9766 val
= args
[coding_arg_plist
];
9768 CODING_ATTR_PLIST (attrs
) = val
;
9770 if (EQ (coding_type
, Qcharset
))
9772 /* Generate a lisp vector of 256 elements. Each element is nil,
9773 integer, or a list of charset IDs.
9775 If Nth element is nil, the byte code N is invalid in this
9778 If Nth element is a number NUM, N is the first byte of a
9779 charset whose ID is NUM.
9781 If Nth element is a list of charset IDs, N is the first byte
9782 of one of them. The list is sorted by dimensions of the
9783 charsets. A charset of smaller dimension comes firtst. */
9784 val
= Fmake_vector (make_number (256), Qnil
);
9786 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9788 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9789 int dim
= CHARSET_DIMENSION (charset
);
9790 int idx
= (dim
- 1) * 4;
9792 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9793 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9795 for (i
= charset
->code_space
[idx
];
9796 i
<= charset
->code_space
[idx
+ 1]; i
++)
9798 Lisp_Object tmp
, tmp2
;
9801 tmp
= AREF (val
, i
);
9804 else if (NUMBERP (tmp
))
9806 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9808 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9810 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9814 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9816 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9821 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9824 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9825 XSETCAR (tmp2
, XCAR (tail
));
9831 ASET (attrs
, coding_attr_charset_valids
, val
);
9832 category
= coding_category_charset
;
9834 else if (EQ (coding_type
, Qccl
))
9838 if (nargs
< coding_arg_ccl_max
)
9841 val
= args
[coding_arg_ccl_decoder
];
9842 CHECK_CCL_PROGRAM (val
);
9844 val
= Fcopy_sequence (val
);
9845 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9847 val
= args
[coding_arg_ccl_encoder
];
9848 CHECK_CCL_PROGRAM (val
);
9850 val
= Fcopy_sequence (val
);
9851 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9853 val
= args
[coding_arg_ccl_valids
];
9854 valids
= Fmake_string (make_number (256), make_number (0));
9855 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9862 from
= to
= XINT (val
);
9863 if (from
< 0 || from
> 255)
9864 args_out_of_range_3 (val
, make_number (0), make_number (255));
9869 CHECK_NATNUM_CAR (val
);
9870 CHECK_NATNUM_CDR (val
);
9871 from
= XINT (XCAR (val
));
9873 args_out_of_range_3 (XCAR (val
),
9874 make_number (0), make_number (255));
9875 to
= XINT (XCDR (val
));
9876 if (to
< from
|| to
> 255)
9877 args_out_of_range_3 (XCDR (val
),
9878 XCAR (val
), make_number (255));
9880 for (i
= from
; i
<= to
; i
++)
9881 SSET (valids
, i
, 1);
9883 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9885 category
= coding_category_ccl
;
9887 else if (EQ (coding_type
, Qutf_16
))
9889 Lisp_Object bom
, endian
;
9891 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9893 if (nargs
< coding_arg_utf16_max
)
9896 bom
= args
[coding_arg_utf16_bom
];
9897 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9901 CHECK_CODING_SYSTEM (val
);
9903 CHECK_CODING_SYSTEM (val
);
9905 ASET (attrs
, coding_attr_utf_bom
, bom
);
9907 endian
= args
[coding_arg_utf16_endian
];
9908 CHECK_SYMBOL (endian
);
9911 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9912 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9913 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9915 category
= (CONSP (bom
)
9916 ? coding_category_utf_16_auto
9918 ? (EQ (endian
, Qbig
)
9919 ? coding_category_utf_16_be_nosig
9920 : coding_category_utf_16_le_nosig
)
9921 : (EQ (endian
, Qbig
)
9922 ? coding_category_utf_16_be
9923 : coding_category_utf_16_le
));
9925 else if (EQ (coding_type
, Qiso_2022
))
9927 Lisp_Object initial
, reg_usage
, request
, flags
;
9930 if (nargs
< coding_arg_iso2022_max
)
9933 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9934 CHECK_VECTOR (initial
);
9935 for (i
= 0; i
< 4; i
++)
9937 val
= Faref (initial
, make_number (i
));
9940 struct charset
*charset
;
9942 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9943 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9944 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9945 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9948 ASET (initial
, i
, make_number (-1));
9951 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9952 CHECK_CONS (reg_usage
);
9953 CHECK_NUMBER_CAR (reg_usage
);
9954 CHECK_NUMBER_CDR (reg_usage
);
9956 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9957 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9965 CHECK_CHARSET_GET_ID (tmp
, id
);
9966 CHECK_NATNUM_CDR (val
);
9967 if (XINT (XCDR (val
)) >= 4)
9968 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9969 XSETCAR (val
, make_number (id
));
9972 flags
= args
[coding_arg_iso2022_flags
];
9973 CHECK_NATNUM (flags
);
9975 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9976 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9978 ASET (attrs
, coding_attr_iso_initial
, initial
);
9979 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9980 ASET (attrs
, coding_attr_iso_request
, request
);
9981 ASET (attrs
, coding_attr_iso_flags
, flags
);
9982 setup_iso_safe_charsets (attrs
);
9984 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9985 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9986 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9987 ? coding_category_iso_7_else
9988 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9989 ? coding_category_iso_7
9990 : coding_category_iso_7_tight
);
9993 int id
= XINT (AREF (initial
, 1));
9995 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9996 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9998 ? coding_category_iso_8_else
9999 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
10000 ? coding_category_iso_8_1
10001 : coding_category_iso_8_2
);
10003 if (category
!= coding_category_iso_8_1
10004 && category
!= coding_category_iso_8_2
)
10005 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
10007 else if (EQ (coding_type
, Qemacs_mule
))
10009 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
10010 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
10011 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10012 category
= coding_category_emacs_mule
;
10014 else if (EQ (coding_type
, Qshift_jis
))
10017 struct charset
*charset
;
10019 if (XINT (Flength (charset_list
)) != 3
10020 && XINT (Flength (charset_list
)) != 4)
10021 error ("There should be three or four charsets");
10023 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10024 if (CHARSET_DIMENSION (charset
) != 1)
10025 error ("Dimension of charset %s is not one",
10026 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10027 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10028 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10030 charset_list
= XCDR (charset_list
);
10031 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10032 if (CHARSET_DIMENSION (charset
) != 1)
10033 error ("Dimension of charset %s is not one",
10034 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10036 charset_list
= XCDR (charset_list
);
10037 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10038 if (CHARSET_DIMENSION (charset
) != 2)
10039 error ("Dimension of charset %s is not two",
10040 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10042 charset_list
= XCDR (charset_list
);
10043 if (! NILP (charset_list
))
10045 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10046 if (CHARSET_DIMENSION (charset
) != 2)
10047 error ("Dimension of charset %s is not two",
10048 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10051 category
= coding_category_sjis
;
10052 Vsjis_coding_system
= name
;
10054 else if (EQ (coding_type
, Qbig5
))
10056 struct charset
*charset
;
10058 if (XINT (Flength (charset_list
)) != 2)
10059 error ("There should be just two charsets");
10061 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10062 if (CHARSET_DIMENSION (charset
) != 1)
10063 error ("Dimension of charset %s is not one",
10064 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10065 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
10066 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10068 charset_list
= XCDR (charset_list
);
10069 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
10070 if (CHARSET_DIMENSION (charset
) != 2)
10071 error ("Dimension of charset %s is not two",
10072 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
10074 category
= coding_category_big5
;
10075 Vbig5_coding_system
= name
;
10077 else if (EQ (coding_type
, Qraw_text
))
10079 category
= coding_category_raw_text
;
10080 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10082 else if (EQ (coding_type
, Qutf_8
))
10086 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
10088 if (nargs
< coding_arg_utf8_max
)
10091 bom
= args
[coding_arg_utf8_bom
];
10092 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10096 CHECK_CODING_SYSTEM (val
);
10098 CHECK_CODING_SYSTEM (val
);
10100 ASET (attrs
, coding_attr_utf_bom
, bom
);
10102 category
= (CONSP (bom
) ? coding_category_utf_8_auto
10103 : NILP (bom
) ? coding_category_utf_8_nosig
10104 : coding_category_utf_8_sig
);
10106 else if (EQ (coding_type
, Qundecided
))
10107 category
= coding_category_undecided
;
10109 error ("Invalid coding system type: %s",
10110 SDATA (SYMBOL_NAME (coding_type
)));
10112 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
10113 CODING_ATTR_PLIST (attrs
)
10114 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
10115 CODING_ATTR_PLIST (attrs
)));
10116 CODING_ATTR_PLIST (attrs
)
10117 = Fcons (QCascii_compatible_p
,
10118 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
10119 CODING_ATTR_PLIST (attrs
)));
10121 eol_type
= args
[coding_arg_eol_type
];
10122 if (! NILP (eol_type
)
10123 && ! EQ (eol_type
, Qunix
)
10124 && ! EQ (eol_type
, Qdos
)
10125 && ! EQ (eol_type
, Qmac
))
10126 error ("Invalid eol-type");
10128 aliases
= Fcons (name
, Qnil
);
10130 if (NILP (eol_type
))
10132 eol_type
= make_subsidiaries (name
);
10133 for (i
= 0; i
< 3; i
++)
10135 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10137 this_name
= AREF (eol_type
, i
);
10138 this_aliases
= Fcons (this_name
, Qnil
);
10139 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10140 this_spec
= Fmake_vector (make_number (3), attrs
);
10141 ASET (this_spec
, 1, this_aliases
);
10142 ASET (this_spec
, 2, this_eol_type
);
10143 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10144 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10145 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10147 Vcoding_system_alist
10148 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10149 Vcoding_system_alist
);
10153 spec_vec
= Fmake_vector (make_number (3), attrs
);
10154 ASET (spec_vec
, 1, aliases
);
10155 ASET (spec_vec
, 2, eol_type
);
10157 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10158 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10159 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10161 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10162 Vcoding_system_alist
);
10165 int id
= coding_categories
[category
].id
;
10167 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10168 setup_coding_system (name
, &coding_categories
[category
]);
10174 return Fsignal (Qwrong_number_of_arguments
,
10175 Fcons (intern ("define-coding-system-internal"),
10176 make_number (nargs
)));
10180 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10182 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10183 (coding_system
, prop
, val
)
10184 Lisp_Object coding_system
, prop
, val
;
10186 Lisp_Object spec
, attrs
;
10188 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10189 attrs
= AREF (spec
, 0);
10190 if (EQ (prop
, QCmnemonic
))
10192 if (! STRINGP (val
))
10193 CHECK_CHARACTER (val
);
10194 CODING_ATTR_MNEMONIC (attrs
) = val
;
10196 else if (EQ (prop
, QCdefault_char
))
10199 val
= make_number (' ');
10201 CHECK_CHARACTER (val
);
10202 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10204 else if (EQ (prop
, QCdecode_translation_table
))
10206 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10207 CHECK_SYMBOL (val
);
10208 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10210 else if (EQ (prop
, QCencode_translation_table
))
10212 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10213 CHECK_SYMBOL (val
);
10214 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10216 else if (EQ (prop
, QCpost_read_conversion
))
10218 CHECK_SYMBOL (val
);
10219 CODING_ATTR_POST_READ (attrs
) = val
;
10221 else if (EQ (prop
, QCpre_write_conversion
))
10223 CHECK_SYMBOL (val
);
10224 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10226 else if (EQ (prop
, QCascii_compatible_p
))
10228 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10231 CODING_ATTR_PLIST (attrs
)
10232 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10237 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10238 Sdefine_coding_system_alias
, 2, 2, 0,
10239 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10240 (alias
, coding_system
)
10241 Lisp_Object alias
, coding_system
;
10243 Lisp_Object spec
, aliases
, eol_type
, val
;
10245 CHECK_SYMBOL (alias
);
10246 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10247 aliases
= AREF (spec
, 1);
10248 /* ALIASES should be a list of length more than zero, and the first
10249 element is a base coding system. Append ALIAS at the tail of the
10251 while (!NILP (XCDR (aliases
)))
10252 aliases
= XCDR (aliases
);
10253 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10255 eol_type
= AREF (spec
, 2);
10256 if (VECTORP (eol_type
))
10258 Lisp_Object subsidiaries
;
10261 subsidiaries
= make_subsidiaries (alias
);
10262 for (i
= 0; i
< 3; i
++)
10263 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10264 AREF (eol_type
, i
));
10267 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10268 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10269 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10271 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10272 Vcoding_system_alist
);
10277 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10279 doc
: /* Return the base of CODING-SYSTEM.
10280 Any alias or subsidiary coding system is not a base coding system. */)
10282 Lisp_Object coding_system
;
10284 Lisp_Object spec
, attrs
;
10286 if (NILP (coding_system
))
10287 return (Qno_conversion
);
10288 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10289 attrs
= AREF (spec
, 0);
10290 return CODING_ATTR_BASE_NAME (attrs
);
10293 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10295 doc
: "Return the property list of CODING-SYSTEM.")
10297 Lisp_Object coding_system
;
10299 Lisp_Object spec
, attrs
;
10301 if (NILP (coding_system
))
10302 coding_system
= Qno_conversion
;
10303 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10304 attrs
= AREF (spec
, 0);
10305 return CODING_ATTR_PLIST (attrs
);
10309 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10311 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10313 Lisp_Object coding_system
;
10317 if (NILP (coding_system
))
10318 coding_system
= Qno_conversion
;
10319 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10320 return AREF (spec
, 1);
10323 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10324 Scoding_system_eol_type
, 1, 1, 0,
10325 doc
: /* Return eol-type of CODING-SYSTEM.
10326 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10328 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10329 and CR respectively.
10331 A vector value indicates that a format of end-of-line should be
10332 detected automatically. Nth element of the vector is the subsidiary
10333 coding system whose eol-type is N. */)
10335 Lisp_Object coding_system
;
10337 Lisp_Object spec
, eol_type
;
10340 if (NILP (coding_system
))
10341 coding_system
= Qno_conversion
;
10342 if (! CODING_SYSTEM_P (coding_system
))
10344 spec
= CODING_SYSTEM_SPEC (coding_system
);
10345 eol_type
= AREF (spec
, 2);
10346 if (VECTORP (eol_type
))
10347 return Fcopy_sequence (eol_type
);
10348 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10349 return make_number (n
);
10355 /*** 9. Post-amble ***/
10358 init_coding_once ()
10362 for (i
= 0; i
< coding_category_max
; i
++)
10364 coding_categories
[i
].id
= -1;
10365 coding_priorities
[i
] = i
;
10368 /* ISO2022 specific initialize routine. */
10369 for (i
= 0; i
< 0x20; i
++)
10370 iso_code_class
[i
] = ISO_control_0
;
10371 for (i
= 0x21; i
< 0x7F; i
++)
10372 iso_code_class
[i
] = ISO_graphic_plane_0
;
10373 for (i
= 0x80; i
< 0xA0; i
++)
10374 iso_code_class
[i
] = ISO_control_1
;
10375 for (i
= 0xA1; i
< 0xFF; i
++)
10376 iso_code_class
[i
] = ISO_graphic_plane_1
;
10377 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10378 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10379 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10380 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10381 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10382 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10383 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10384 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10385 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10387 for (i
= 0; i
< 256; i
++)
10389 emacs_mule_bytes
[i
] = 1;
10391 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10392 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10393 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10394 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10402 staticpro (&Vcoding_system_hash_table
);
10404 Lisp_Object args
[2];
10407 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10410 staticpro (&Vsjis_coding_system
);
10411 Vsjis_coding_system
= Qnil
;
10413 staticpro (&Vbig5_coding_system
);
10414 Vbig5_coding_system
= Qnil
;
10416 staticpro (&Vcode_conversion_reused_workbuf
);
10417 Vcode_conversion_reused_workbuf
= Qnil
;
10419 staticpro (&Vcode_conversion_workbuf_name
);
10420 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10422 reused_workbuf_in_use
= 0;
10424 DEFSYM (Qcharset
, "charset");
10425 DEFSYM (Qtarget_idx
, "target-idx");
10426 DEFSYM (Qcoding_system_history
, "coding-system-history");
10427 Fset (Qcoding_system_history
, Qnil
);
10429 /* Target FILENAME is the first argument. */
10430 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10431 /* Target FILENAME is the third argument. */
10432 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10434 DEFSYM (Qcall_process
, "call-process");
10435 /* Target PROGRAM is the first argument. */
10436 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10438 DEFSYM (Qcall_process_region
, "call-process-region");
10439 /* Target PROGRAM is the third argument. */
10440 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10442 DEFSYM (Qstart_process
, "start-process");
10443 /* Target PROGRAM is the third argument. */
10444 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10446 DEFSYM (Qopen_network_stream
, "open-network-stream");
10447 /* Target SERVICE is the fourth argument. */
10448 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10450 DEFSYM (Qcoding_system
, "coding-system");
10451 DEFSYM (Qcoding_aliases
, "coding-aliases");
10453 DEFSYM (Qeol_type
, "eol-type");
10454 DEFSYM (Qunix
, "unix");
10455 DEFSYM (Qdos
, "dos");
10457 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10458 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10459 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10460 DEFSYM (Qdefault_char
, "default-char");
10461 DEFSYM (Qundecided
, "undecided");
10462 DEFSYM (Qno_conversion
, "no-conversion");
10463 DEFSYM (Qraw_text
, "raw-text");
10465 DEFSYM (Qiso_2022
, "iso-2022");
10467 DEFSYM (Qutf_8
, "utf-8");
10468 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10470 DEFSYM (Qutf_16
, "utf-16");
10471 DEFSYM (Qbig
, "big");
10472 DEFSYM (Qlittle
, "little");
10474 DEFSYM (Qshift_jis
, "shift-jis");
10475 DEFSYM (Qbig5
, "big5");
10477 DEFSYM (Qcoding_system_p
, "coding-system-p");
10479 DEFSYM (Qcoding_system_error
, "coding-system-error");
10480 Fput (Qcoding_system_error
, Qerror_conditions
,
10481 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10482 Fput (Qcoding_system_error
, Qerror_message
,
10483 make_pure_c_string ("Invalid coding system"));
10485 /* Intern this now in case it isn't already done.
10486 Setting this variable twice is harmless.
10487 But don't staticpro it here--that is done in alloc.c. */
10488 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10490 DEFSYM (Qtranslation_table
, "translation-table");
10491 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10492 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10493 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10494 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10496 DEFSYM (Qvalid_codes
, "valid-codes");
10498 DEFSYM (Qemacs_mule
, "emacs-mule");
10500 DEFSYM (QCcategory
, ":category");
10501 DEFSYM (QCmnemonic
, ":mnemonic");
10502 DEFSYM (QCdefault_char
, ":default-char");
10503 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10504 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10505 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10506 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10507 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10509 Vcoding_category_table
10510 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10511 staticpro (&Vcoding_category_table
);
10512 /* Followings are target of code detection. */
10513 ASET (Vcoding_category_table
, coding_category_iso_7
,
10514 intern_c_string ("coding-category-iso-7"));
10515 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10516 intern_c_string ("coding-category-iso-7-tight"));
10517 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10518 intern_c_string ("coding-category-iso-8-1"));
10519 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10520 intern_c_string ("coding-category-iso-8-2"));
10521 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10522 intern_c_string ("coding-category-iso-7-else"));
10523 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10524 intern_c_string ("coding-category-iso-8-else"));
10525 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10526 intern_c_string ("coding-category-utf-8-auto"));
10527 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10528 intern_c_string ("coding-category-utf-8"));
10529 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10530 intern_c_string ("coding-category-utf-8-sig"));
10531 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10532 intern_c_string ("coding-category-utf-16-be"));
10533 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10534 intern_c_string ("coding-category-utf-16-auto"));
10535 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10536 intern_c_string ("coding-category-utf-16-le"));
10537 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10538 intern_c_string ("coding-category-utf-16-be-nosig"));
10539 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10540 intern_c_string ("coding-category-utf-16-le-nosig"));
10541 ASET (Vcoding_category_table
, coding_category_charset
,
10542 intern_c_string ("coding-category-charset"));
10543 ASET (Vcoding_category_table
, coding_category_sjis
,
10544 intern_c_string ("coding-category-sjis"));
10545 ASET (Vcoding_category_table
, coding_category_big5
,
10546 intern_c_string ("coding-category-big5"));
10547 ASET (Vcoding_category_table
, coding_category_ccl
,
10548 intern_c_string ("coding-category-ccl"));
10549 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10550 intern_c_string ("coding-category-emacs-mule"));
10551 /* Followings are NOT target of code detection. */
10552 ASET (Vcoding_category_table
, coding_category_raw_text
,
10553 intern_c_string ("coding-category-raw-text"));
10554 ASET (Vcoding_category_table
, coding_category_undecided
,
10555 intern_c_string ("coding-category-undecided"));
10557 DEFSYM (Qinsufficient_source
, "insufficient-source");
10558 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10559 DEFSYM (Qinvalid_source
, "invalid-source");
10560 DEFSYM (Qinterrupted
, "interrupted");
10561 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10562 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10564 defsubr (&Scoding_system_p
);
10565 defsubr (&Sread_coding_system
);
10566 defsubr (&Sread_non_nil_coding_system
);
10567 defsubr (&Scheck_coding_system
);
10568 defsubr (&Sdetect_coding_region
);
10569 defsubr (&Sdetect_coding_string
);
10570 defsubr (&Sfind_coding_systems_region_internal
);
10571 defsubr (&Sunencodable_char_position
);
10572 defsubr (&Scheck_coding_systems_region
);
10573 defsubr (&Sdecode_coding_region
);
10574 defsubr (&Sencode_coding_region
);
10575 defsubr (&Sdecode_coding_string
);
10576 defsubr (&Sencode_coding_string
);
10577 defsubr (&Sdecode_sjis_char
);
10578 defsubr (&Sencode_sjis_char
);
10579 defsubr (&Sdecode_big5_char
);
10580 defsubr (&Sencode_big5_char
);
10581 defsubr (&Sset_terminal_coding_system_internal
);
10582 defsubr (&Sset_safe_terminal_coding_system_internal
);
10583 defsubr (&Sterminal_coding_system
);
10584 defsubr (&Sset_keyboard_coding_system_internal
);
10585 defsubr (&Skeyboard_coding_system
);
10586 defsubr (&Sfind_operation_coding_system
);
10587 defsubr (&Sset_coding_system_priority
);
10588 defsubr (&Sdefine_coding_system_internal
);
10589 defsubr (&Sdefine_coding_system_alias
);
10590 defsubr (&Scoding_system_put
);
10591 defsubr (&Scoding_system_base
);
10592 defsubr (&Scoding_system_plist
);
10593 defsubr (&Scoding_system_aliases
);
10594 defsubr (&Scoding_system_eol_type
);
10595 defsubr (&Scoding_system_priority_list
);
10597 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
10598 doc
: /* List of coding systems.
10600 Do not alter the value of this variable manually. This variable should be
10601 updated by the functions `define-coding-system' and
10602 `define-coding-system-alias'. */);
10603 Vcoding_system_list
= Qnil
;
10605 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
10606 doc
: /* Alist of coding system names.
10607 Each element is one element list of coding system name.
10608 This variable is given to `completing-read' as COLLECTION argument.
10610 Do not alter the value of this variable manually. This variable should be
10611 updated by the functions `make-coding-system' and
10612 `define-coding-system-alias'. */);
10613 Vcoding_system_alist
= Qnil
;
10615 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
10616 doc
: /* List of coding-categories (symbols) ordered by priority.
10618 On detecting a coding system, Emacs tries code detection algorithms
10619 associated with each coding-category one by one in this order. When
10620 one algorithm agrees with a byte sequence of source text, the coding
10621 system bound to the corresponding coding-category is selected.
10623 Don't modify this variable directly, but use `set-coding-priority'. */);
10627 Vcoding_category_list
= Qnil
;
10628 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10629 Vcoding_category_list
10630 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10631 Vcoding_category_list
);
10634 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
10635 doc
: /* Specify the coding system for read operations.
10636 It is useful to bind this variable with `let', but do not set it globally.
10637 If the value is a coding system, it is used for decoding on read operation.
10638 If not, an appropriate element is used from one of the coding system alists.
10639 There are three such tables: `file-coding-system-alist',
10640 `process-coding-system-alist', and `network-coding-system-alist'. */);
10641 Vcoding_system_for_read
= Qnil
;
10643 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
10644 doc
: /* Specify the coding system for write operations.
10645 Programs bind this variable with `let', but you should not set it globally.
10646 If the value is a coding system, it is used for encoding of output,
10647 when writing it to a file and when sending it to a file or subprocess.
10649 If this does not specify a coding system, an appropriate element
10650 is used from one of the coding system alists.
10651 There are three such tables: `file-coding-system-alist',
10652 `process-coding-system-alist', and `network-coding-system-alist'.
10653 For output to files, if the above procedure does not specify a coding system,
10654 the value of `buffer-file-coding-system' is used. */);
10655 Vcoding_system_for_write
= Qnil
;
10657 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
10659 Coding system used in the latest file or process I/O. */);
10660 Vlast_coding_system_used
= Qnil
;
10662 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
10664 Error status of the last code conversion.
10666 When an error was detected in the last code conversion, this variable
10667 is set to one of the following symbols.
10668 `insufficient-source'
10672 `insufficient-memory'
10673 When no error was detected, the value doesn't change. So, to check
10674 the error status of a code conversion by this variable, you must
10675 explicitly set this variable to nil before performing code
10677 Vlast_code_conversion_error
= Qnil
;
10679 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
10681 *Non-nil means always inhibit code conversion of end-of-line format.
10682 See info node `Coding Systems' and info node `Text and Binary' concerning
10683 such conversion. */);
10684 inhibit_eol_conversion
= 0;
10686 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
10688 Non-nil means process buffer inherits coding system of process output.
10689 Bind it to t if the process output is to be treated as if it were a file
10690 read from some filesystem. */);
10691 inherit_process_coding_system
= 0;
10693 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
10695 Alist to decide a coding system to use for a file I/O operation.
10696 The format is ((PATTERN . VAL) ...),
10697 where PATTERN is a regular expression matching a file name,
10698 VAL is a coding system, a cons of coding systems, or a function symbol.
10699 If VAL is a coding system, it is used for both decoding and encoding
10701 If VAL is a cons of coding systems, the car part is used for decoding,
10702 and the cdr part is used for encoding.
10703 If VAL is a function symbol, the function must return a coding system
10704 or a cons of coding systems which are used as above. The function is
10705 called with an argument that is a list of the arguments with which
10706 `find-operation-coding-system' was called. If the function can't decide
10707 a coding system, it can return `undecided' so that the normal
10708 code-detection is performed.
10710 See also the function `find-operation-coding-system'
10711 and the variable `auto-coding-alist'. */);
10712 Vfile_coding_system_alist
= Qnil
;
10714 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
10716 Alist to decide a coding system to use for a process I/O operation.
10717 The format is ((PATTERN . VAL) ...),
10718 where PATTERN is a regular expression matching a program name,
10719 VAL is a coding system, a cons of coding systems, or a function symbol.
10720 If VAL is a coding system, it is used for both decoding what received
10721 from the program and encoding what sent to the program.
10722 If VAL is a cons of coding systems, the car part is used for decoding,
10723 and the cdr part is used for encoding.
10724 If VAL is a function symbol, the function must return a coding system
10725 or a cons of coding systems which are used as above.
10727 See also the function `find-operation-coding-system'. */);
10728 Vprocess_coding_system_alist
= Qnil
;
10730 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
10732 Alist to decide a coding system to use for a network I/O operation.
10733 The format is ((PATTERN . VAL) ...),
10734 where PATTERN is a regular expression matching a network service name
10735 or is a port number to connect to,
10736 VAL is a coding system, a cons of coding systems, or a function symbol.
10737 If VAL is a coding system, it is used for both decoding what received
10738 from the network stream and encoding what sent to the network stream.
10739 If VAL is a cons of coding systems, the car part is used for decoding,
10740 and the cdr part is used for encoding.
10741 If VAL is a function symbol, the function must return a coding system
10742 or a cons of coding systems which are used as above.
10744 See also the function `find-operation-coding-system'. */);
10745 Vnetwork_coding_system_alist
= Qnil
;
10747 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
10748 doc
: /* Coding system to use with system messages.
10749 Also used for decoding keyboard input on X Window system. */);
10750 Vlocale_coding_system
= Qnil
;
10752 /* The eol mnemonics are reset in startup.el system-dependently. */
10753 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
10755 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10756 eol_mnemonic_unix
= make_pure_c_string (":");
10758 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
10760 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10761 eol_mnemonic_dos
= make_pure_c_string ("\\");
10763 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
10765 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10766 eol_mnemonic_mac
= make_pure_c_string ("/");
10768 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
10770 *String displayed in mode line when end-of-line format is not yet determined. */);
10771 eol_mnemonic_undecided
= make_pure_c_string (":");
10773 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
10775 *Non-nil enables character translation while encoding and decoding. */);
10776 Venable_character_translation
= Qt
;
10778 DEFVAR_LISP ("standard-translation-table-for-decode",
10779 &Vstandard_translation_table_for_decode
,
10780 doc
: /* Table for translating characters while decoding. */);
10781 Vstandard_translation_table_for_decode
= Qnil
;
10783 DEFVAR_LISP ("standard-translation-table-for-encode",
10784 &Vstandard_translation_table_for_encode
,
10785 doc
: /* Table for translating characters while encoding. */);
10786 Vstandard_translation_table_for_encode
= Qnil
;
10788 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
10789 doc
: /* Alist of charsets vs revision numbers.
10790 While encoding, if a charset (car part of an element) is found,
10791 designate it with the escape sequence identifying revision (cdr part
10792 of the element). */);
10793 Vcharset_revision_table
= Qnil
;
10795 DEFVAR_LISP ("default-process-coding-system",
10796 &Vdefault_process_coding_system
,
10797 doc
: /* Cons of coding systems used for process I/O by default.
10798 The car part is used for decoding a process output,
10799 the cdr part is used for encoding a text to be sent to a process. */);
10800 Vdefault_process_coding_system
= Qnil
;
10802 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
10804 Table of extra Latin codes in the range 128..159 (inclusive).
10805 This is a vector of length 256.
10806 If Nth element is non-nil, the existence of code N in a file
10807 \(or output of subprocess) doesn't prevent it to be detected as
10808 a coding system of ISO 2022 variant which has a flag
10809 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10810 or reading output of a subprocess.
10811 Only 128th through 159th elements have a meaning. */);
10812 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10814 DEFVAR_LISP ("select-safe-coding-system-function",
10815 &Vselect_safe_coding_system_function
,
10817 Function to call to select safe coding system for encoding a text.
10819 If set, this function is called to force a user to select a proper
10820 coding system which can encode the text in the case that a default
10821 coding system used in each operation can't encode the text. The
10822 function should take care that the buffer is not modified while
10823 the coding system is being selected.
10825 The default value is `select-safe-coding-system' (which see). */);
10826 Vselect_safe_coding_system_function
= Qnil
;
10828 DEFVAR_BOOL ("coding-system-require-warning",
10829 &coding_system_require_warning
,
10830 doc
: /* Internal use only.
10831 If non-nil, on writing a file, `select-safe-coding-system-function' is
10832 called even if `coding-system-for-write' is non-nil. The command
10833 `universal-coding-system-argument' binds this variable to t temporarily. */);
10834 coding_system_require_warning
= 0;
10837 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10838 &inhibit_iso_escape_detection
,
10840 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10842 When Emacs reads text, it tries to detect how the text is encoded.
10843 This code detection is sensitive to escape sequences. If Emacs sees
10844 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10845 of the ISO2022 encodings, and decodes text by the corresponding coding
10846 system (e.g. `iso-2022-7bit').
10848 However, there may be a case that you want to read escape sequences in
10849 a file as is. In such a case, you can set this variable to non-nil.
10850 Then the code detection will ignore any escape sequences, and no text is
10851 detected as encoded in some ISO-2022 encoding. The result is that all
10852 escape sequences become visible in a buffer.
10854 The default value is nil, and it is strongly recommended not to change
10855 it. That is because many Emacs Lisp source files that contain
10856 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10857 in Emacs's distribution, and they won't be decoded correctly on
10858 reading if you suppress escape sequence detection.
10860 The other way to read escape sequences in a file without decoding is
10861 to explicitly specify some coding system that doesn't use ISO-2022
10862 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10863 inhibit_iso_escape_detection
= 0;
10865 DEFVAR_BOOL ("inhibit-null-byte-detection",
10866 &inhibit_null_byte_detection
,
10867 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10868 By default, Emacs treats it as binary data, and does not attempt to
10869 decode it. The effect is as if you specified `no-conversion' for
10872 Set this to non-nil when a regular text happens to include null bytes.
10873 Examples are Index nodes of Info files and null-byte delimited output
10874 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10875 decode text as usual. */);
10876 inhibit_null_byte_detection
= 0;
10878 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
10879 doc
: /* Char table for translating self-inserting characters.
10880 This is applied to the result of input methods, not their input.
10881 See also `keyboard-translate-table'.
10883 Use of this variable for character code unification was rendered
10884 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10885 internal character representation. */);
10886 Vtranslation_table_for_input
= Qnil
;
10889 Lisp_Object args
[coding_arg_max
];
10890 Lisp_Object plist
[16];
10893 for (i
= 0; i
< coding_arg_max
; i
++)
10896 plist
[0] = intern_c_string (":name");
10897 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10898 plist
[2] = intern_c_string (":mnemonic");
10899 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10900 plist
[4] = intern_c_string (":coding-type");
10901 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10902 plist
[6] = intern_c_string (":ascii-compatible-p");
10903 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10904 plist
[8] = intern_c_string (":default-char");
10905 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10906 plist
[10] = intern_c_string (":for-unibyte");
10907 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10908 plist
[12] = intern_c_string (":docstring");
10909 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10911 When you visit a file with this coding, the file is read into a\n\
10912 unibyte buffer as is, thus each byte of a file is treated as a\n\
10914 plist
[14] = intern_c_string (":eol-type");
10915 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10916 args
[coding_arg_plist
] = Flist (16, plist
);
10917 Fdefine_coding_system_internal (coding_arg_max
, args
);
10919 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10920 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10921 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10922 /* This is already set.
10923 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10924 plist
[8] = intern_c_string (":charset-list");
10925 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10926 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10927 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10928 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10929 args
[coding_arg_plist
] = Flist (16, plist
);
10930 Fdefine_coding_system_internal (coding_arg_max
, args
);
10933 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10938 for (i
= 0; i
< coding_category_max
; i
++)
10939 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10941 #if defined (MSDOS) || defined (WINDOWSNT)
10942 system_eol_type
= Qdos
;
10944 system_eol_type
= Qunix
;
10946 staticpro (&system_eol_type
);
10950 emacs_strerror (error_number
)
10955 synchronize_system_messages_locale ();
10956 str
= strerror (error_number
);
10958 if (! NILP (Vlocale_coding_system
))
10960 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10961 Vlocale_coding_system
,
10963 str
= (char *) SDATA (dec
);
10971 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
10972 (do not change this comment) */