1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1997, 1998, 2002, 2003, 2004, 2005
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H14PRO021
7 National Institute of Advanced Industrial Science and Technology (AIST)
8 Registration Number H13PRO009
10 This file is part of GNU Emacs.
12 GNU Emacs is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 GNU Emacs is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GNU Emacs; see the file COPYING. If not, write to
24 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, USA. */
27 /*** TABLE OF CONTENTS ***
31 2. Emacs' internal format (emacs-utf-8) handlers
34 5. Charset-base coding systems handlers
35 6. emacs-mule (old Emacs' internal format) handlers
37 8. Shift-JIS and BIG5 handlers
39 10. C library functions
40 11. Emacs Lisp library functions
45 /*** 0. General comments ***
50 A coding system is an object for an encoding mechanism that contains
51 information about how to convert byte sequences to character
52 sequences and vice versa. When we say "decode", it means converting
53 a byte sequence of a specific coding system into a character
54 sequence that is represented by Emacs' internal coding system
55 `emacs-utf-8', and when we say "encode", it means converting a
56 character sequence of emacs-utf-8 to a byte sequence of a specific
59 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
60 C level, a coding system is represented by a vector of attributes
61 stored in the hash table Vcharset_hash_table. The conversion from
62 coding system symbol to attributes vector is done by looking up
63 Vcharset_hash_table by the symbol.
65 Coding systems are classified into the following types depending on
66 the encoding mechanism. Here's a brief description of the types.
72 o Charset-base coding system
74 A coding system defined by one or more (coded) character sets.
75 Decoding and encoding are done by a code converter defined for each
78 o Old Emacs internal format (emacs-mule)
80 The coding system adopted by old versions of Emacs (20 and 21).
82 o ISO2022-base coding system
84 The most famous coding system for multiple character sets. X's
85 Compound Text, various EUCs (Extended Unix Code), and coding systems
86 used in the Internet communication such as ISO-2022-JP are all
89 o SJIS (or Shift-JIS or MS-Kanji-Code)
91 A coding system to encode character sets: ASCII, JISX0201, and
92 JISX0208. Widely used for PC's in Japan. Details are described in
97 A coding system to encode character sets: ASCII and Big5. Widely
98 used for Chinese (mainly in Taiwan and Hong Kong). Details are
99 described in section 8. In this file, when we write "big5" (all
100 lowercase), we mean the coding system, and when we write "Big5"
101 (capitalized), we mean the character set.
105 If a user wants to decode/encode text encoded in a coding system
106 not listed above, he can supply a decoder and an encoder for it in
107 CCL (Code Conversion Language) programs. Emacs executes the CCL
108 program while decoding/encoding.
112 A coding system for text containing raw eight-bit data. Emacs
113 treats each byte of source text as a character (except for
114 end-of-line conversion).
118 Like raw text, but don't do end-of-line conversion.
123 How text end-of-line is encoded depends on operating system. For
124 instance, Unix's format is just one byte of LF (line-feed) code,
125 whereas DOS's format is two-byte sequence of `carriage-return' and
126 `line-feed' codes. MacOS's format is usually one byte of
129 Since text character encoding and end-of-line encoding are
130 independent, any coding system described above can take any format
131 of end-of-line (except for no-conversion).
135 Before using a coding system for code conversion (i.e. decoding and
136 encoding), we setup a structure of type `struct coding_system'.
137 This structure keeps various information about a specific code
138 conversion (e.g. the location of source and destination data).
145 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
147 These functions check if a byte sequence specified as a source in
148 CODING conforms to the format of XXX, and update the members of
151 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
153 Below is the template of these functions. */
157 detect_coding_XXX (coding
, detect_info
)
158 struct coding_system
*coding
;
159 struct coding_detection_info
*detect_info
;
161 const unsigned char *src
= coding
->source
;
162 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
163 int multibytep
= coding
->src_multibyte
;
164 int consumed_chars
= 0;
170 /* Get one byte from the source. If the souce is exausted, jump
171 to no_more_source:. */
174 if (! __C_conforms_to_XXX___ (c
))
176 if (! __C_strongly_suggests_XXX__ (c
))
177 found
= CATEGORY_MASK_XXX
;
179 /* The byte sequence is invalid for XXX. */
180 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
184 /* The source exausted successfully. */
185 detect_info
->found
|= found
;
190 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
192 These functions decode a byte sequence specified as a source by
193 CODING. The resulting multibyte text goes to a place pointed to by
194 CODING->charbuf, the length of which should not exceed
195 CODING->charbuf_size;
197 These functions set the information of original and decoded texts in
198 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
199 They also set CODING->result to one of CODING_RESULT_XXX indicating
200 how the decoding is finished.
202 Below is the template of these functions. */
206 decode_coding_XXXX (coding
)
207 struct coding_system
*coding
;
209 const unsigned char *src
= coding
->source
+ coding
->consumed
;
210 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
211 /* SRC_BASE remembers the start position in source in each loop.
212 The loop will be exited when there's not enough source code, or
213 when there's no room in CHARBUF for a decoded character. */
214 const unsigned char *src_base
;
215 /* A buffer to produce decoded characters. */
216 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
217 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
218 int multibytep
= coding
->src_multibyte
;
223 if (charbuf
< charbuf_end
)
224 /* No more room to produce a decoded character. */
231 if (src_base
< src_end
232 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
233 /* If the source ends by partial bytes to construct a character,
234 treat them as eight-bit raw data. */
235 while (src_base
< src_end
&& charbuf
< charbuf_end
)
236 *charbuf
++ = *src_base
++;
237 /* Remember how many bytes and characters we consumed. If the
238 source is multibyte, the bytes and chars are not identical. */
239 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
240 /* Remember how many characters we produced. */
241 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
245 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
247 These functions encode SRC_BYTES length text at SOURCE of Emacs'
248 internal multibyte format by CODING. The resulting byte sequence
249 goes to a place pointed to by DESTINATION, the length of which
250 should not exceed DST_BYTES.
252 These functions set the information of original and encoded texts in
253 the members produced, produced_char, consumed, and consumed_char of
254 the structure *CODING. They also set the member result to one of
255 CODING_RESULT_XXX indicating how the encoding finished.
257 DST_BYTES zero means that source area and destination area are
258 overlapped, which means that we can produce a encoded text until it
259 reaches at the head of not-yet-encoded source text.
261 Below is a template of these functions. */
264 encode_coding_XXX (coding
)
265 struct coding_system
*coding
;
267 int multibytep
= coding
->dst_multibyte
;
268 int *charbuf
= coding
->charbuf
;
269 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
270 unsigned char *dst
= coding
->destination
+ coding
->produced
;
271 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
272 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
273 int produced_chars
= 0;
275 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
278 /* Encode C into DST, and increment DST. */
280 label_no_more_destination
:
281 /* How many chars and bytes we produced. */
282 coding
->produced_char
+= produced_chars
;
283 coding
->produced
= dst
- coding
->destination
;
288 /*** 1. Preamble ***/
295 #include "character.h"
298 #include "composite.h"
302 Lisp_Object Vcoding_system_hash_table
;
304 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
305 Lisp_Object Qunix
, Qdos
;
306 extern Lisp_Object Qmac
; /* frame.c */
307 Lisp_Object Qbuffer_file_coding_system
;
308 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
309 Lisp_Object Qdefault_char
;
310 Lisp_Object Qno_conversion
, Qundecided
;
311 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
312 Lisp_Object Qbig
, Qlittle
;
313 Lisp_Object Qcoding_system_history
;
314 Lisp_Object Qvalid_codes
;
315 Lisp_Object QCcategory
, QCmnemonic
, QCdefalut_char
;
316 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
317 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
318 Lisp_Object QCascii_compatible_p
;
320 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
321 Lisp_Object Qcall_process
, Qcall_process_region
;
322 Lisp_Object Qstart_process
, Qopen_network_stream
;
323 Lisp_Object Qtarget_idx
;
325 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
326 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
328 /* If a symbol has this property, evaluate the value to define the
329 symbol as a coding system. */
330 static Lisp_Object Qcoding_system_define_form
;
332 int coding_system_require_warning
;
334 Lisp_Object Vselect_safe_coding_system_function
;
336 /* Mnemonic string for each format of end-of-line. */
337 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
338 /* Mnemonic string to indicate format of end-of-line is not yet
340 Lisp_Object eol_mnemonic_undecided
;
344 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
346 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
348 /* Coding system emacs-mule and raw-text are for converting only
349 end-of-line format. */
350 Lisp_Object Qemacs_mule
, Qraw_text
;
351 Lisp_Object Qutf_8_emacs
;
353 /* Coding-systems are handed between Emacs Lisp programs and C internal
354 routines by the following three variables. */
355 /* Coding-system for reading files and receiving data from process. */
356 Lisp_Object Vcoding_system_for_read
;
357 /* Coding-system for writing files and sending data to process. */
358 Lisp_Object Vcoding_system_for_write
;
359 /* Coding-system actually used in the latest I/O. */
360 Lisp_Object Vlast_coding_system_used
;
361 /* Set to non-nil when an error is detected while code conversion. */
362 Lisp_Object Vlast_code_conversion_error
;
363 /* A vector of length 256 which contains information about special
364 Latin codes (especially for dealing with Microsoft codes). */
365 Lisp_Object Vlatin_extra_code_table
;
367 /* Flag to inhibit code conversion of end-of-line format. */
368 int inhibit_eol_conversion
;
370 /* Flag to inhibit ISO2022 escape sequence detection. */
371 int inhibit_iso_escape_detection
;
373 /* Flag to make buffer-file-coding-system inherit from process-coding. */
374 int inherit_process_coding_system
;
376 /* Coding system to be used to encode text for terminal display. */
377 struct coding_system terminal_coding
;
379 /* Coding system to be used to encode text for terminal display when
380 terminal coding system is nil. */
381 struct coding_system safe_terminal_coding
;
383 /* Coding system of what is sent from terminal keyboard. */
384 struct coding_system keyboard_coding
;
386 Lisp_Object Vfile_coding_system_alist
;
387 Lisp_Object Vprocess_coding_system_alist
;
388 Lisp_Object Vnetwork_coding_system_alist
;
390 Lisp_Object Vlocale_coding_system
;
394 /* Flag to tell if we look up translation table on character code
396 Lisp_Object Venable_character_translation
;
397 /* Standard translation table to look up on decoding (reading). */
398 Lisp_Object Vstandard_translation_table_for_decode
;
399 /* Standard translation table to look up on encoding (writing). */
400 Lisp_Object Vstandard_translation_table_for_encode
;
402 Lisp_Object Qtranslation_table
;
403 Lisp_Object Qtranslation_table_id
;
404 Lisp_Object Qtranslation_table_for_decode
;
405 Lisp_Object Qtranslation_table_for_encode
;
407 /* Alist of charsets vs revision number. */
408 static Lisp_Object Vcharset_revision_table
;
410 /* Default coding systems used for process I/O. */
411 Lisp_Object Vdefault_process_coding_system
;
413 /* Char table for translating Quail and self-inserting input. */
414 Lisp_Object Vtranslation_table_for_input
;
416 /* Two special coding systems. */
417 Lisp_Object Vsjis_coding_system
;
418 Lisp_Object Vbig5_coding_system
;
420 /* ISO2022 section */
422 #define CODING_ISO_INITIAL(coding, reg) \
423 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
424 coding_attr_iso_initial), \
428 #define CODING_ISO_REQUEST(coding, charset_id) \
429 ((charset_id <= (coding)->max_charset_id \
430 ? (coding)->safe_charsets[charset_id] \
434 #define CODING_ISO_FLAGS(coding) \
435 ((coding)->spec.iso_2022.flags)
436 #define CODING_ISO_DESIGNATION(coding, reg) \
437 ((coding)->spec.iso_2022.current_designation[reg])
438 #define CODING_ISO_INVOCATION(coding, plane) \
439 ((coding)->spec.iso_2022.current_invocation[plane])
440 #define CODING_ISO_SINGLE_SHIFTING(coding) \
441 ((coding)->spec.iso_2022.single_shifting)
442 #define CODING_ISO_BOL(coding) \
443 ((coding)->spec.iso_2022.bol)
444 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
445 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
447 /* Control characters of ISO2022. */
448 /* code */ /* function */
449 #define ISO_CODE_LF 0x0A /* line-feed */
450 #define ISO_CODE_CR 0x0D /* carriage-return */
451 #define ISO_CODE_SO 0x0E /* shift-out */
452 #define ISO_CODE_SI 0x0F /* shift-in */
453 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
454 #define ISO_CODE_ESC 0x1B /* escape */
455 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
456 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
457 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
459 /* All code (1-byte) of ISO2022 is classified into one of the
461 enum iso_code_class_type
463 ISO_control_0
, /* Control codes in the range
464 0x00..0x1F and 0x7F, except for the
465 following 5 codes. */
466 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
467 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
468 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
469 ISO_escape
, /* ISO_CODE_SO (0x1B) */
470 ISO_control_1
, /* Control codes in the range
471 0x80..0x9F, except for the
472 following 3 codes. */
473 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
474 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
475 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
476 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
477 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
478 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
479 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
482 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
483 `iso-flags' attribute of an iso2022 coding system. */
485 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
486 instead of the correct short-form sequence (e.g. ESC $ A). */
487 #define CODING_ISO_FLAG_LONG_FORM 0x0001
489 /* If set, reset graphic planes and registers at end-of-line to the
491 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
493 /* If set, reset graphic planes and registers before any control
494 characters to the initial state. */
495 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
497 /* If set, encode by 7-bit environment. */
498 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
500 /* If set, use locking-shift function. */
501 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
503 /* If set, use single-shift function. Overwrite
504 CODING_ISO_FLAG_LOCKING_SHIFT. */
505 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
507 /* If set, use designation escape sequence. */
508 #define CODING_ISO_FLAG_DESIGNATION 0x0040
510 /* If set, produce revision number sequence. */
511 #define CODING_ISO_FLAG_REVISION 0x0080
513 /* If set, produce ISO6429's direction specifying sequence. */
514 #define CODING_ISO_FLAG_DIRECTION 0x0100
516 /* If set, assume designation states are reset at beginning of line on
518 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
520 /* If set, designation sequence should be placed at beginning of line
522 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
524 /* If set, do not encode unsafe charactes on output. */
525 #define CODING_ISO_FLAG_SAFE 0x0800
527 /* If set, extra latin codes (128..159) are accepted as a valid code
529 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
531 #define CODING_ISO_FLAG_COMPOSITION 0x2000
533 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
535 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
537 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
539 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
541 /* A character to be produced on output if encoding of the original
542 character is prohibited by CODING_ISO_FLAG_SAFE. */
543 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
547 #define CODING_UTF_16_BOM(coding) \
548 ((coding)->spec.utf_16.bom)
550 #define CODING_UTF_16_ENDIAN(coding) \
551 ((coding)->spec.utf_16.endian)
553 #define CODING_UTF_16_SURROGATE(coding) \
554 ((coding)->spec.utf_16.surrogate)
558 #define CODING_CCL_DECODER(coding) \
559 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
560 #define CODING_CCL_ENCODER(coding) \
561 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
562 #define CODING_CCL_VALIDS(coding) \
563 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
565 /* Index for each coding category in `coding_categories' */
569 coding_category_iso_7
,
570 coding_category_iso_7_tight
,
571 coding_category_iso_8_1
,
572 coding_category_iso_8_2
,
573 coding_category_iso_7_else
,
574 coding_category_iso_8_else
,
575 coding_category_utf_8
,
576 coding_category_utf_16_auto
,
577 coding_category_utf_16_be
,
578 coding_category_utf_16_le
,
579 coding_category_utf_16_be_nosig
,
580 coding_category_utf_16_le_nosig
,
581 coding_category_charset
,
582 coding_category_sjis
,
583 coding_category_big5
,
585 coding_category_emacs_mule
,
586 /* All above are targets of code detection. */
587 coding_category_raw_text
,
588 coding_category_undecided
,
592 /* Definitions of flag bits used in detect_coding_XXXX. */
593 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
594 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
595 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
596 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
597 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
598 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
599 #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
600 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
601 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
602 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
603 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
604 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
605 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
606 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
607 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
608 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
609 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
610 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
612 /* This value is returned if detect_coding_mask () find nothing other
613 than ASCII characters. */
614 #define CATEGORY_MASK_ANY \
615 (CATEGORY_MASK_ISO_7 \
616 | CATEGORY_MASK_ISO_7_TIGHT \
617 | CATEGORY_MASK_ISO_8_1 \
618 | CATEGORY_MASK_ISO_8_2 \
619 | CATEGORY_MASK_ISO_7_ELSE \
620 | CATEGORY_MASK_ISO_8_ELSE \
621 | CATEGORY_MASK_UTF_8 \
622 | CATEGORY_MASK_UTF_16_BE \
623 | CATEGORY_MASK_UTF_16_LE \
624 | CATEGORY_MASK_UTF_16_BE_NOSIG \
625 | CATEGORY_MASK_UTF_16_LE_NOSIG \
626 | CATEGORY_MASK_CHARSET \
627 | CATEGORY_MASK_SJIS \
628 | CATEGORY_MASK_BIG5 \
629 | CATEGORY_MASK_CCL \
630 | CATEGORY_MASK_EMACS_MULE)
633 #define CATEGORY_MASK_ISO_7BIT \
634 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
636 #define CATEGORY_MASK_ISO_8BIT \
637 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
639 #define CATEGORY_MASK_ISO_ELSE \
640 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
642 #define CATEGORY_MASK_ISO_ESCAPE \
643 (CATEGORY_MASK_ISO_7 \
644 | CATEGORY_MASK_ISO_7_TIGHT \
645 | CATEGORY_MASK_ISO_7_ELSE \
646 | CATEGORY_MASK_ISO_8_ELSE)
648 #define CATEGORY_MASK_ISO \
649 ( CATEGORY_MASK_ISO_7BIT \
650 | CATEGORY_MASK_ISO_8BIT \
651 | CATEGORY_MASK_ISO_ELSE)
653 #define CATEGORY_MASK_UTF_16 \
654 (CATEGORY_MASK_UTF_16_BE \
655 | CATEGORY_MASK_UTF_16_LE \
656 | CATEGORY_MASK_UTF_16_BE_NOSIG \
657 | CATEGORY_MASK_UTF_16_LE_NOSIG)
660 /* List of symbols `coding-category-xxx' ordered by priority. This
661 variable is exposed to Emacs Lisp. */
662 static Lisp_Object Vcoding_category_list
;
664 /* Table of coding categories (Lisp symbols). This variable is for
666 static Lisp_Object Vcoding_category_table
;
668 /* Table of coding-categories ordered by priority. */
669 static enum coding_category coding_priorities
[coding_category_max
];
671 /* Nth element is a coding context for the coding system bound to the
672 Nth coding category. */
673 static struct coding_system coding_categories
[coding_category_max
];
675 /*** Commonly used macros and functions ***/
678 #define min(a, b) ((a) < (b) ? (a) : (b))
681 #define max(a, b) ((a) > (b) ? (a) : (b))
684 #define CODING_GET_INFO(coding, attrs, charset_list) \
686 (attrs) = CODING_ID_ATTRS ((coding)->id); \
687 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
691 /* Safely get one byte from the source text pointed by SRC which ends
692 at SRC_END, and set C to that byte. If there are not enough bytes
693 in the source, it jumps to `no_more_source'. If multibytep is
694 nonzero, and a multibyte character is found at SRC, set C to the
695 negative value of the character code. The caller should declare
696 and set these variables appropriately in advance:
697 src, src_end, multibytep */
699 #define ONE_MORE_BYTE(c) \
701 if (src == src_end) \
703 if (src_base < src) \
704 record_conversion_result \
705 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
706 goto no_more_source; \
709 if (multibytep && (c & 0x80)) \
711 if ((c & 0xFE) == 0xC0) \
712 c = ((c & 1) << 6) | *src++; \
716 c = - string_char (src, &src, NULL); \
717 record_conversion_result \
718 (coding, CODING_RESULT_INVALID_SRC); \
725 #define ONE_MORE_BYTE_NO_CHECK(c) \
728 if (multibytep && (c & 0x80)) \
730 if ((c & 0xFE) == 0xC0) \
731 c = ((c & 1) << 6) | *src++; \
735 c = - string_char (src, &src, NULL); \
736 record_conversion_result \
737 (coding, CODING_RESULT_INVALID_SRC); \
744 /* Store a byte C in the place pointed by DST and increment DST to the
745 next free point, and increment PRODUCED_CHARS. The caller should
746 assure that C is 0..127, and declare and set the variable `dst'
747 appropriately in advance.
751 #define EMIT_ONE_ASCII_BYTE(c) \
758 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
760 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
762 produced_chars += 2; \
763 *dst++ = (c1), *dst++ = (c2); \
767 /* Store a byte C in the place pointed by DST and increment DST to the
768 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
769 nonzero, store in an appropriate multibyte from. The caller should
770 declare and set the variables `dst' and `multibytep' appropriately
773 #define EMIT_ONE_BYTE(c) \
780 ch = BYTE8_TO_CHAR (ch); \
781 CHAR_STRING_ADVANCE (ch, dst); \
788 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
790 #define EMIT_TWO_BYTES(c1, c2) \
792 produced_chars += 2; \
799 ch = BYTE8_TO_CHAR (ch); \
800 CHAR_STRING_ADVANCE (ch, dst); \
803 ch = BYTE8_TO_CHAR (ch); \
804 CHAR_STRING_ADVANCE (ch, dst); \
814 #define EMIT_THREE_BYTES(c1, c2, c3) \
816 EMIT_ONE_BYTE (c1); \
817 EMIT_TWO_BYTES (c2, c3); \
821 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
823 EMIT_TWO_BYTES (c1, c2); \
824 EMIT_TWO_BYTES (c3, c4); \
828 /* Prototypes for static functions. */
829 static void record_conversion_result
P_ ((struct coding_system
*coding
,
830 enum coding_result_code result
));
831 static int detect_coding_utf_8
P_ ((struct coding_system
*,
832 struct coding_detection_info
*info
));
833 static void decode_coding_utf_8
P_ ((struct coding_system
*));
834 static int encode_coding_utf_8
P_ ((struct coding_system
*));
836 static int detect_coding_utf_16
P_ ((struct coding_system
*,
837 struct coding_detection_info
*info
));
838 static void decode_coding_utf_16
P_ ((struct coding_system
*));
839 static int encode_coding_utf_16
P_ ((struct coding_system
*));
841 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
842 struct coding_detection_info
*info
));
843 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
844 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
846 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
847 struct coding_detection_info
*info
));
848 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
849 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
851 static int detect_coding_sjis
P_ ((struct coding_system
*,
852 struct coding_detection_info
*info
));
853 static void decode_coding_sjis
P_ ((struct coding_system
*));
854 static int encode_coding_sjis
P_ ((struct coding_system
*));
856 static int detect_coding_big5
P_ ((struct coding_system
*,
857 struct coding_detection_info
*info
));
858 static void decode_coding_big5
P_ ((struct coding_system
*));
859 static int encode_coding_big5
P_ ((struct coding_system
*));
861 static int detect_coding_ccl
P_ ((struct coding_system
*,
862 struct coding_detection_info
*info
));
863 static void decode_coding_ccl
P_ ((struct coding_system
*));
864 static int encode_coding_ccl
P_ ((struct coding_system
*));
866 static void decode_coding_raw_text
P_ ((struct coding_system
*));
867 static int encode_coding_raw_text
P_ ((struct coding_system
*));
869 static void coding_set_source
P_ ((struct coding_system
*));
870 static void coding_set_destination
P_ ((struct coding_system
*));
871 static void coding_alloc_by_realloc
P_ ((struct coding_system
*, EMACS_INT
));
872 static void coding_alloc_by_making_gap
P_ ((struct coding_system
*,
874 static unsigned char *alloc_destination
P_ ((struct coding_system
*,
875 EMACS_INT
, unsigned char *));
876 static void setup_iso_safe_charsets
P_ ((Lisp_Object
));
877 static unsigned char *encode_designation_at_bol
P_ ((struct coding_system
*,
880 static int detect_eol
P_ ((const unsigned char *,
881 EMACS_INT
, enum coding_category
));
882 static Lisp_Object adjust_coding_eol_type
P_ ((struct coding_system
*, int));
883 static void decode_eol
P_ ((struct coding_system
*));
884 static Lisp_Object get_translation_table
P_ ((Lisp_Object
, int, int *));
885 static Lisp_Object get_translation
P_ ((Lisp_Object
, int *, int *,
887 static int produce_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
888 static INLINE
void produce_composition
P_ ((struct coding_system
*, int *,
890 static INLINE
void produce_charset
P_ ((struct coding_system
*, int *,
892 static void produce_annotation
P_ ((struct coding_system
*, EMACS_INT
));
893 static int decode_coding
P_ ((struct coding_system
*));
894 static INLINE
int *handle_composition_annotation
P_ ((EMACS_INT
, EMACS_INT
,
895 struct coding_system
*,
896 int *, EMACS_INT
*));
897 static INLINE
int *handle_charset_annotation
P_ ((EMACS_INT
, EMACS_INT
,
898 struct coding_system
*,
899 int *, EMACS_INT
*));
900 static void consume_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
901 static int encode_coding
P_ ((struct coding_system
*));
902 static Lisp_Object make_conversion_work_buffer
P_ ((int));
903 static Lisp_Object code_conversion_restore
P_ ((Lisp_Object
));
904 static INLINE
int char_encodable_p
P_ ((int, Lisp_Object
));
905 static Lisp_Object make_subsidiaries
P_ ((Lisp_Object
));
908 record_conversion_result (struct coding_system
*coding
,
909 enum coding_result_code result
)
911 coding
->result
= result
;
914 case CODING_RESULT_INSUFFICIENT_SRC
:
915 Vlast_code_conversion_error
= Qinsufficient_source
;
917 case CODING_RESULT_INCONSISTENT_EOL
:
918 Vlast_code_conversion_error
= Qinconsistent_eol
;
920 case CODING_RESULT_INVALID_SRC
:
921 Vlast_code_conversion_error
= Qinvalid_source
;
923 case CODING_RESULT_INTERRUPT
:
924 Vlast_code_conversion_error
= Qinterrupted
;
926 case CODING_RESULT_INSUFFICIENT_MEM
:
927 Vlast_code_conversion_error
= Qinsufficient_memory
;
930 Vlast_code_conversion_error
= intern ("Unknown error");
934 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
936 charset_map_loaded = 0; \
937 c = DECODE_CHAR (charset, code); \
938 if (charset_map_loaded) \
940 const unsigned char *orig = coding->source; \
943 coding_set_source (coding); \
944 offset = coding->source - orig; \
946 src_base += offset; \
952 #define ASSURE_DESTINATION(bytes) \
954 if (dst + (bytes) >= dst_end) \
956 int more_bytes = charbuf_end - charbuf + (bytes); \
958 dst = alloc_destination (coding, more_bytes, dst); \
959 dst_end = coding->destination + coding->dst_bytes; \
966 coding_set_source (coding
)
967 struct coding_system
*coding
;
969 if (BUFFERP (coding
->src_object
))
971 struct buffer
*buf
= XBUFFER (coding
->src_object
);
973 if (coding
->src_pos
< 0)
974 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
976 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
978 else if (STRINGP (coding
->src_object
))
980 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
983 /* Otherwise, the source is C string and is never relocated
984 automatically. Thus we don't have to update anything. */
989 coding_set_destination (coding
)
990 struct coding_system
*coding
;
992 if (BUFFERP (coding
->dst_object
))
994 if (coding
->src_pos
< 0)
996 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- 1;
997 coding
->dst_bytes
= (GAP_END_ADDR
998 - (coding
->src_bytes
- coding
->consumed
)
999 - coding
->destination
);
1003 /* We are sure that coding->dst_pos_byte is before the gap
1005 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1006 + coding
->dst_pos_byte
- 1);
1007 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1008 - coding
->destination
);
1012 /* Otherwise, the destination is C string and is never relocated
1013 automatically. Thus we don't have to update anything. */
1019 coding_alloc_by_realloc (coding
, bytes
)
1020 struct coding_system
*coding
;
1023 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1024 coding
->dst_bytes
+ bytes
);
1025 coding
->dst_bytes
+= bytes
;
1029 coding_alloc_by_making_gap (coding
, bytes
)
1030 struct coding_system
*coding
;
1033 if (BUFFERP (coding
->dst_object
)
1034 && EQ (coding
->src_object
, coding
->dst_object
))
1036 EMACS_INT add
= coding
->src_bytes
- coding
->consumed
;
1038 GAP_SIZE
-= add
; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1040 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1044 Lisp_Object this_buffer
;
1046 this_buffer
= Fcurrent_buffer ();
1047 set_buffer_internal (XBUFFER (coding
->dst_object
));
1049 set_buffer_internal (XBUFFER (this_buffer
));
1054 static unsigned char *
1055 alloc_destination (coding
, nbytes
, dst
)
1056 struct coding_system
*coding
;
1060 EMACS_INT offset
= dst
- coding
->destination
;
1062 if (BUFFERP (coding
->dst_object
))
1063 coding_alloc_by_making_gap (coding
, nbytes
);
1065 coding_alloc_by_realloc (coding
, nbytes
);
1066 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1067 coding_set_destination (coding
);
1068 dst
= coding
->destination
+ offset
;
1072 /** Macros for annotations. */
1074 /* Maximum length of annotation data (sum of annotations for
1075 composition and charset). */
1076 #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1078 /* An annotation data is stored in the array coding->charbuf in this
1080 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1081 LENGTH is the number of elements in the annotation.
1082 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1083 NCHARS is the number of characters in the text annotated.
1085 The format of the following elements depend on ANNOTATION_MASK.
1087 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1089 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1090 METHOD is one of enum composition_method.
1091 Optionnal COMPOSITION-COMPONENTS are characters and composition
1094 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1097 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1099 *(buf)++ = -(len); \
1100 *(buf)++ = (mask); \
1101 *(buf)++ = (nchars); \
1102 coding->annotated = 1; \
1105 #define ADD_COMPOSITION_DATA(buf, nchars, method) \
1107 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1112 #define ADD_CHARSET_DATA(buf, nchars, id) \
1114 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1119 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1126 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1127 Check if a text is encoded in UTF-8. If it is, return 1, else
1130 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1131 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1132 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1133 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1134 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1135 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1138 detect_coding_utf_8 (coding
, detect_info
)
1139 struct coding_system
*coding
;
1140 struct coding_detection_info
*detect_info
;
1142 const unsigned char *src
= coding
->source
, *src_base
;
1143 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1144 int multibytep
= coding
->src_multibyte
;
1145 int consumed_chars
= 0;
1148 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1149 /* A coding system of this category is always ASCII compatible. */
1150 src
+= coding
->head_ascii
;
1154 int c
, c1
, c2
, c3
, c4
;
1158 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1161 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1163 if (UTF_8_2_OCTET_LEADING_P (c
))
1165 found
= CATEGORY_MASK_UTF_8
;
1169 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1171 if (UTF_8_3_OCTET_LEADING_P (c
))
1173 found
= CATEGORY_MASK_UTF_8
;
1177 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1179 if (UTF_8_4_OCTET_LEADING_P (c
))
1181 found
= CATEGORY_MASK_UTF_8
;
1185 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1187 if (UTF_8_5_OCTET_LEADING_P (c
))
1189 found
= CATEGORY_MASK_UTF_8
;
1194 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1198 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1200 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1203 detect_info
->found
|= found
;
1209 decode_coding_utf_8 (coding
)
1210 struct coding_system
*coding
;
1212 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1213 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1214 const unsigned char *src_base
;
1215 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1216 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1217 int consumed_chars
= 0, consumed_chars_base
;
1218 int multibytep
= coding
->src_multibyte
;
1219 Lisp_Object attr
, charset_list
;
1221 CODING_GET_INFO (coding
, attr
, charset_list
);
1225 int c
, c1
, c2
, c3
, c4
, c5
;
1228 consumed_chars_base
= consumed_chars
;
1230 if (charbuf
>= charbuf_end
)
1238 else if (UTF_8_1_OCTET_P(c1
))
1245 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1247 if (UTF_8_2_OCTET_LEADING_P (c1
))
1249 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1250 /* Reject overlong sequences here and below. Encoders
1251 producing them are incorrect, they can be misleading,
1252 and they mess up read/write invariance. */
1259 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1261 if (UTF_8_3_OCTET_LEADING_P (c1
))
1263 c
= (((c1
& 0xF) << 12)
1264 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1266 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1272 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1274 if (UTF_8_4_OCTET_LEADING_P (c1
))
1276 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1277 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1284 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1286 if (UTF_8_5_OCTET_LEADING_P (c1
))
1288 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1289 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1291 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1306 consumed_chars
= consumed_chars_base
;
1308 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1313 coding
->consumed_char
+= consumed_chars_base
;
1314 coding
->consumed
= src_base
- coding
->source
;
1315 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1320 encode_coding_utf_8 (coding
)
1321 struct coding_system
*coding
;
1323 int multibytep
= coding
->dst_multibyte
;
1324 int *charbuf
= coding
->charbuf
;
1325 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1326 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1327 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1328 int produced_chars
= 0;
1333 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1335 while (charbuf
< charbuf_end
)
1337 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1339 ASSURE_DESTINATION (safe_room
);
1341 if (CHAR_BYTE8_P (c
))
1343 c
= CHAR_TO_BYTE8 (c
);
1348 CHAR_STRING_ADVANCE (c
, pend
);
1349 for (p
= str
; p
< pend
; p
++)
1356 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1358 while (charbuf
< charbuf_end
)
1360 ASSURE_DESTINATION (safe_room
);
1362 if (CHAR_BYTE8_P (c
))
1363 *dst
++ = CHAR_TO_BYTE8 (c
);
1365 dst
+= CHAR_STRING (c
, dst
);
1369 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1370 coding
->produced_char
+= produced_chars
;
1371 coding
->produced
= dst
- coding
->destination
;
1376 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1377 Check if a text is encoded in one of UTF-16 based coding systems.
1378 If it is, return 1, else return 0. */
1380 #define UTF_16_HIGH_SURROGATE_P(val) \
1381 (((val) & 0xFC00) == 0xD800)
1383 #define UTF_16_LOW_SURROGATE_P(val) \
1384 (((val) & 0xFC00) == 0xDC00)
1386 #define UTF_16_INVALID_P(val) \
1387 (((val) == 0xFFFE) \
1388 || ((val) == 0xFFFF) \
1389 || UTF_16_LOW_SURROGATE_P (val))
1393 detect_coding_utf_16 (coding
, detect_info
)
1394 struct coding_system
*coding
;
1395 struct coding_detection_info
*detect_info
;
1397 const unsigned char *src
= coding
->source
, *src_base
= src
;
1398 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1399 int multibytep
= coding
->src_multibyte
;
1400 int consumed_chars
= 0;
1403 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1404 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1405 && (coding
->src_chars
& 1))
1407 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1413 if ((c1
== 0xFF) && (c2
== 0xFE))
1415 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1416 | CATEGORY_MASK_UTF_16_AUTO
);
1417 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1418 | CATEGORY_MASK_UTF_16_BE_NOSIG
1419 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1421 else if ((c1
== 0xFE) && (c2
== 0xFF))
1423 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1424 | CATEGORY_MASK_UTF_16_AUTO
);
1425 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1426 | CATEGORY_MASK_UTF_16_BE_NOSIG
1427 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1429 else if (c1
>= 0 && c2
>= 0)
1431 detect_info
->rejected
1432 |= (CATEGORY_MASK_UTF_16_BE
| CATEGORY_MASK_UTF_16_LE
);
1439 decode_coding_utf_16 (coding
)
1440 struct coding_system
*coding
;
1442 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1443 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1444 const unsigned char *src_base
;
1445 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1446 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1447 int consumed_chars
= 0, consumed_chars_base
;
1448 int multibytep
= coding
->src_multibyte
;
1449 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1450 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1451 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1452 Lisp_Object attr
, charset_list
;
1454 CODING_GET_INFO (coding
, attr
, charset_list
);
1456 if (bom
== utf_16_with_bom
)
1465 if (endian
== utf_16_big_endian
1466 ? c
!= 0xFEFF : c
!= 0xFFFE)
1468 /* The first two bytes are not BOM. Treat them as bytes
1469 for a normal character. */
1473 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1475 else if (bom
== utf_16_detect_bom
)
1477 /* We have already tried to detect BOM and failed in
1479 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1487 consumed_chars_base
= consumed_chars
;
1489 if (charbuf
+ 2 >= charbuf_end
)
1501 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1505 c
= (endian
== utf_16_big_endian
1506 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1509 if (! UTF_16_LOW_SURROGATE_P (c
))
1511 if (endian
== utf_16_big_endian
)
1512 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1514 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1518 if (UTF_16_HIGH_SURROGATE_P (c
))
1519 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1525 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1526 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1527 *charbuf
++ = 0x10000 + c
;
1532 if (UTF_16_HIGH_SURROGATE_P (c
))
1533 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1540 coding
->consumed_char
+= consumed_chars_base
;
1541 coding
->consumed
= src_base
- coding
->source
;
1542 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1546 encode_coding_utf_16 (coding
)
1547 struct coding_system
*coding
;
1549 int multibytep
= coding
->dst_multibyte
;
1550 int *charbuf
= coding
->charbuf
;
1551 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1552 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1553 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1555 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1556 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1557 int produced_chars
= 0;
1558 Lisp_Object attrs
, charset_list
;
1561 CODING_GET_INFO (coding
, attrs
, charset_list
);
1563 if (bom
!= utf_16_without_bom
)
1565 ASSURE_DESTINATION (safe_room
);
1567 EMIT_TWO_BYTES (0xFE, 0xFF);
1569 EMIT_TWO_BYTES (0xFF, 0xFE);
1570 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1573 while (charbuf
< charbuf_end
)
1575 ASSURE_DESTINATION (safe_room
);
1577 if (c
>= MAX_UNICODE_CHAR
)
1578 c
= coding
->default_char
;
1583 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1585 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1592 c1
= (c
>> 10) + 0xD800;
1593 c2
= (c
& 0x3FF) + 0xDC00;
1595 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1597 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1600 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1601 coding
->produced
= dst
- coding
->destination
;
1602 coding
->produced_char
+= produced_chars
;
1607 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1609 /* Emacs' internal format for representation of multiple character
1610 sets is a kind of multi-byte encoding, i.e. characters are
1611 represented by variable-length sequences of one-byte codes.
1613 ASCII characters and control characters (e.g. `tab', `newline') are
1614 represented by one-byte sequences which are their ASCII codes, in
1615 the range 0x00 through 0x7F.
1617 8-bit characters of the range 0x80..0x9F are represented by
1618 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1621 8-bit characters of the range 0xA0..0xFF are represented by
1622 one-byte sequences which are their 8-bit code.
1624 The other characters are represented by a sequence of `base
1625 leading-code', optional `extended leading-code', and one or two
1626 `position-code's. The length of the sequence is determined by the
1627 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1628 whereas extended leading-code and position-code take the range 0xA0
1629 through 0xFF. See `charset.h' for more details about leading-code
1632 --- CODE RANGE of Emacs' internal format ---
1636 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1637 eight-bit-graphic 0xA0..0xBF
1638 ELSE 0x81..0x9D + [0xA0..0xFF]+
1639 ---------------------------------------------
1641 As this is the internal character representation, the format is
1642 usually not used externally (i.e. in a file or in a data sent to a
1643 process). But, it is possible to have a text externally in this
1644 format (i.e. by encoding by the coding system `emacs-mule').
1646 In that case, a sequence of one-byte codes has a slightly different
1649 At first, all characters in eight-bit-control are represented by
1650 one-byte sequences which are their 8-bit code.
1652 Next, character composition data are represented by the byte
1653 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1655 METHOD is 0xF0 plus one of composition method (enum
1656 composition_method),
1658 BYTES is 0xA0 plus a byte length of this composition data,
1660 CHARS is 0x20 plus a number of characters composed by this
1663 COMPONENTs are characters of multibye form or composition
1664 rules encoded by two-byte of ASCII codes.
1666 In addition, for backward compatibility, the following formats are
1667 also recognized as composition data on decoding.
1670 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1673 MSEQ is a multibyte form but in these special format:
1674 ASCII: 0xA0 ASCII_CODE+0x80,
1675 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1676 RULE is a one byte code of the range 0xA0..0xF0 that
1677 represents a composition rule.
1680 char emacs_mule_bytes
[256];
1683 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
)
1684 struct coding_system
*coding
;
1685 const unsigned char *src
;
1686 int *nbytes
, *nchars
, *id
;
1688 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1689 const unsigned char *src_base
= src
;
1690 int multibytep
= coding
->src_multibyte
;
1691 struct charset
*charset
;
1694 int consumed_chars
= 0;
1700 charset
= emacs_mule_charset
[0];
1704 switch (emacs_mule_bytes
[c
])
1707 if (! (charset
= emacs_mule_charset
[c
]))
1716 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1717 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1720 if (c
< 0xA0 || ! (charset
= emacs_mule_charset
[c
]))
1729 if (! (charset
= emacs_mule_charset
[c
]))
1734 code
= (c
& 0x7F) << 8;
1744 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
1749 code
= (c
& 0x7F) << 8;
1758 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
1759 ? charset_ascii
: charset_eight_bit
);
1765 c
= DECODE_CHAR (charset
, code
);
1769 *nbytes
= src
- src_base
;
1770 *nchars
= consumed_chars
;
1783 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1784 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1788 detect_coding_emacs_mule (coding
, detect_info
)
1789 struct coding_system
*coding
;
1790 struct coding_detection_info
*detect_info
;
1792 const unsigned char *src
= coding
->source
, *src_base
;
1793 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1794 int multibytep
= coding
->src_multibyte
;
1795 int consumed_chars
= 0;
1799 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1800 /* A coding system of this category is always ASCII compatible. */
1801 src
+= coding
->head_ascii
;
1811 /* Perhaps the start of composite character. We simple skip
1812 it because analyzing it is too heavy for detecting. But,
1813 at least, we check that the composite character
1814 constitues of more than 4 bytes. */
1815 const unsigned char *src_base
;
1825 if (src
- src_base
<= 4)
1827 found
= CATEGORY_MASK_EMACS_MULE
;
1835 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1840 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
1842 while (more_bytes
> 0)
1847 src
--; /* Unread the last byte. */
1852 if (more_bytes
!= 0)
1854 found
= CATEGORY_MASK_EMACS_MULE
;
1857 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1861 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1863 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1866 detect_info
->found
|= found
;
1871 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1873 /* Decode a character represented as a component of composition
1874 sequence of Emacs 20/21 style at SRC. Set C to that character and
1875 update SRC to the head of next character (or an encoded composition
1876 rule). If SRC doesn't points a composition component, set C to -1.
1877 If SRC points an invalid byte sequence, global exit by a return
1880 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
1884 int nbytes, nchars; \
1886 if (src == src_end) \
1888 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
1893 goto invalid_code; \
1897 consumed_chars += nchars; \
1902 /* Decode a composition rule represented as a component of composition
1903 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
1904 and increment BUF. If SRC points an invalid byte sequence, set C
1907 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
1909 int c, gref, nref; \
1911 if (src >= src_end) \
1912 goto invalid_code; \
1913 ONE_MORE_BYTE_NO_CHECK (c); \
1915 if (c < 0 || c >= 81) \
1916 goto invalid_code; \
1918 gref = c / 9, nref = c % 9; \
1919 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1923 /* Decode a composition rule represented as a component of composition
1924 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
1925 and increment BUF. If SRC points an invalid byte sequence, set C
1928 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
1932 if (src + 1>= src_end) \
1933 goto invalid_code; \
1934 ONE_MORE_BYTE_NO_CHECK (gref); \
1936 ONE_MORE_BYTE_NO_CHECK (nref); \
1938 if (gref < 0 || gref >= 81 \
1939 || nref < 0 || nref >= 81) \
1940 goto invalid_code; \
1941 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1945 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
1947 /* Emacs 21 style format. The first three bytes at SRC are \
1948 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
1949 the byte length of this composition information, CHARS is the \
1950 number of characters composed by this composition. */ \
1951 enum composition_method method = c - 0xF2; \
1952 int *charbuf_base = charbuf; \
1953 int consumed_chars_limit; \
1954 int nbytes, nchars; \
1956 ONE_MORE_BYTE (c); \
1958 goto invalid_code; \
1959 nbytes = c - 0xA0; \
1961 goto invalid_code; \
1962 ONE_MORE_BYTE (c); \
1964 goto invalid_code; \
1965 nchars = c - 0xA0; \
1966 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
1967 consumed_chars_limit = consumed_chars_base + nbytes; \
1968 if (method != COMPOSITION_RELATIVE) \
1971 while (consumed_chars < consumed_chars_limit) \
1973 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
1974 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
1976 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
1979 if (consumed_chars < consumed_chars_limit) \
1980 goto invalid_code; \
1981 charbuf_base[0] -= i; \
1986 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
1988 /* Emacs 20 style format for relative composition. */ \
1989 /* Store multibyte form of characters to be composed. */ \
1990 enum composition_method method = COMPOSITION_RELATIVE; \
1991 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
1992 int *buf = components; \
1996 ONE_MORE_BYTE (c); /* skip 0x80 */ \
1997 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
1998 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2000 goto invalid_code; \
2001 ADD_COMPOSITION_DATA (charbuf, i, method); \
2002 for (j = 0; j < i; j++) \
2003 *charbuf++ = components[j]; \
2007 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
2009 /* Emacs 20 style format for rule-base composition. */ \
2010 /* Store multibyte form of characters to be composed. */ \
2011 enum composition_method method = COMPOSITION_WITH_RULE; \
2012 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2013 int *buf = components; \
2016 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2017 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
2019 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
2020 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2022 if (i < 1 || (buf - components) % 2 == 0) \
2023 goto invalid_code; \
2024 if (charbuf + i + (i / 2) + 1 < charbuf_end) \
2025 goto no_more_source; \
2026 ADD_COMPOSITION_DATA (buf, i, method); \
2027 for (j = 0; j < i; j++) \
2028 *charbuf++ = components[j]; \
2029 for (j = 0; j < i; j += 2) \
2030 *charbuf++ = components[j]; \
2035 decode_coding_emacs_mule (coding
)
2036 struct coding_system
*coding
;
2038 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2039 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2040 const unsigned char *src_base
;
2041 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2043 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
2044 int consumed_chars
= 0, consumed_chars_base
;
2045 int multibytep
= coding
->src_multibyte
;
2046 Lisp_Object attrs
, charset_list
;
2047 int char_offset
= coding
->produced_char
;
2048 int last_offset
= char_offset
;
2049 int last_id
= charset_ascii
;
2051 CODING_GET_INFO (coding
, attrs
, charset_list
);
2058 consumed_chars_base
= consumed_chars
;
2060 if (charbuf
>= charbuf_end
)
2079 if (c
- 0xF2 >= COMPOSITION_RELATIVE
2080 && c
- 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS
)
2081 DECODE_EMACS_MULE_21_COMPOSITION (c
);
2083 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c
);
2085 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c
);
2089 else if (c
< 0xA0 && emacs_mule_bytes
[c
] > 1)
2095 consumed_chars
= consumed_chars_base
;
2096 c
= emacs_mule_char (coding
, src
, &nbytes
, &nchars
, &id
);
2105 if (last_id
!= charset_ascii
)
2106 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2108 last_offset
= char_offset
;
2112 consumed_chars
+= nchars
;
2119 consumed_chars
= consumed_chars_base
;
2121 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2127 if (last_id
!= charset_ascii
)
2128 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2129 coding
->consumed_char
+= consumed_chars_base
;
2130 coding
->consumed
= src_base
- coding
->source
;
2131 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2135 #define EMACS_MULE_LEADING_CODES(id, codes) \
2138 codes[0] = id, codes[1] = 0; \
2139 else if (id < 0xE0) \
2140 codes[0] = 0x9A, codes[1] = id; \
2141 else if (id < 0xF0) \
2142 codes[0] = 0x9B, codes[1] = id; \
2143 else if (id < 0xF5) \
2144 codes[0] = 0x9C, codes[1] = id; \
2146 codes[0] = 0x9D, codes[1] = id; \
2151 encode_coding_emacs_mule (coding
)
2152 struct coding_system
*coding
;
2154 int multibytep
= coding
->dst_multibyte
;
2155 int *charbuf
= coding
->charbuf
;
2156 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2157 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2158 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2160 int produced_chars
= 0;
2161 Lisp_Object attrs
, charset_list
;
2163 int preferred_charset_id
= -1;
2165 CODING_GET_INFO (coding
, attrs
, charset_list
);
2166 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2168 CODING_ATTR_CHARSET_LIST (attrs
)
2169 = charset_list
= Vemacs_mule_charset_list
;
2172 while (charbuf
< charbuf_end
)
2174 ASSURE_DESTINATION (safe_room
);
2179 /* Handle an annotation. */
2182 case CODING_ANNOTATE_COMPOSITION_MASK
:
2183 /* Not yet implemented. */
2185 case CODING_ANNOTATE_CHARSET_MASK
:
2186 preferred_charset_id
= charbuf
[3];
2187 if (preferred_charset_id
>= 0
2188 && NILP (Fmemq (make_number (preferred_charset_id
),
2190 preferred_charset_id
= -1;
2199 if (ASCII_CHAR_P (c
))
2200 EMIT_ONE_ASCII_BYTE (c
);
2201 else if (CHAR_BYTE8_P (c
))
2203 c
= CHAR_TO_BYTE8 (c
);
2208 struct charset
*charset
;
2212 unsigned char leading_codes
[2];
2214 if (preferred_charset_id
>= 0)
2216 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2217 if (! CHAR_CHARSET_P (c
, charset
))
2218 charset
= char_charset (c
, charset_list
, NULL
);
2221 charset
= char_charset (c
, charset_list
, &code
);
2224 c
= coding
->default_char
;
2225 if (ASCII_CHAR_P (c
))
2227 EMIT_ONE_ASCII_BYTE (c
);
2230 charset
= char_charset (c
, charset_list
, &code
);
2232 dimension
= CHARSET_DIMENSION (charset
);
2233 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2234 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2235 EMIT_ONE_BYTE (leading_codes
[0]);
2236 if (leading_codes
[1])
2237 EMIT_ONE_BYTE (leading_codes
[1]);
2239 EMIT_ONE_BYTE (code
| 0x80);
2243 EMIT_ONE_BYTE (code
>> 8);
2244 EMIT_ONE_BYTE (code
& 0xFF);
2248 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2249 coding
->produced_char
+= produced_chars
;
2250 coding
->produced
= dst
- coding
->destination
;
2255 /*** 7. ISO2022 handlers ***/
2257 /* The following note describes the coding system ISO2022 briefly.
2258 Since the intention of this note is to help understand the
2259 functions in this file, some parts are NOT ACCURATE or are OVERLY
2260 SIMPLIFIED. For thorough understanding, please refer to the
2261 original document of ISO2022. This is equivalent to the standard
2262 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2264 ISO2022 provides many mechanisms to encode several character sets
2265 in 7-bit and 8-bit environments. For 7-bit environments, all text
2266 is encoded using bytes less than 128. This may make the encoded
2267 text a little bit longer, but the text passes more easily through
2268 several types of gateway, some of which strip off the MSB (Most
2271 There are two kinds of character sets: control character sets and
2272 graphic character sets. The former contain control characters such
2273 as `newline' and `escape' to provide control functions (control
2274 functions are also provided by escape sequences). The latter
2275 contain graphic characters such as 'A' and '-'. Emacs recognizes
2276 two control character sets and many graphic character sets.
2278 Graphic character sets are classified into one of the following
2279 four classes, according to the number of bytes (DIMENSION) and
2280 number of characters in one dimension (CHARS) of the set:
2281 - DIMENSION1_CHARS94
2282 - DIMENSION1_CHARS96
2283 - DIMENSION2_CHARS94
2284 - DIMENSION2_CHARS96
2286 In addition, each character set is assigned an identification tag,
2287 unique for each set, called the "final character" (denoted as <F>
2288 hereafter). The <F> of each character set is decided by ECMA(*)
2289 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2290 (0x30..0x3F are for private use only).
2292 Note (*): ECMA = European Computer Manufacturers Association
2294 Here are examples of graphic character sets [NAME(<F>)]:
2295 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2296 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2297 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2298 o DIMENSION2_CHARS96 -- none for the moment
2300 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2301 C0 [0x00..0x1F] -- control character plane 0
2302 GL [0x20..0x7F] -- graphic character plane 0
2303 C1 [0x80..0x9F] -- control character plane 1
2304 GR [0xA0..0xFF] -- graphic character plane 1
2306 A control character set is directly designated and invoked to C0 or
2307 C1 by an escape sequence. The most common case is that:
2308 - ISO646's control character set is designated/invoked to C0, and
2309 - ISO6429's control character set is designated/invoked to C1,
2310 and usually these designations/invocations are omitted in encoded
2311 text. In a 7-bit environment, only C0 can be used, and a control
2312 character for C1 is encoded by an appropriate escape sequence to
2313 fit into the environment. All control characters for C1 are
2314 defined to have corresponding escape sequences.
2316 A graphic character set is at first designated to one of four
2317 graphic registers (G0 through G3), then these graphic registers are
2318 invoked to GL or GR. These designations and invocations can be
2319 done independently. The most common case is that G0 is invoked to
2320 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2321 these invocations and designations are omitted in encoded text.
2322 In a 7-bit environment, only GL can be used.
2324 When a graphic character set of CHARS94 is invoked to GL, codes
2325 0x20 and 0x7F of the GL area work as control characters SPACE and
2326 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2329 There are two ways of invocation: locking-shift and single-shift.
2330 With locking-shift, the invocation lasts until the next different
2331 invocation, whereas with single-shift, the invocation affects the
2332 following character only and doesn't affect the locking-shift
2333 state. Invocations are done by the following control characters or
2336 ----------------------------------------------------------------------
2337 abbrev function cntrl escape seq description
2338 ----------------------------------------------------------------------
2339 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2340 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2341 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2342 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2343 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2344 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2345 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2346 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2347 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2348 ----------------------------------------------------------------------
2349 (*) These are not used by any known coding system.
2351 Control characters for these functions are defined by macros
2352 ISO_CODE_XXX in `coding.h'.
2354 Designations are done by the following escape sequences:
2355 ----------------------------------------------------------------------
2356 escape sequence description
2357 ----------------------------------------------------------------------
2358 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2359 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2360 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2361 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2362 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2363 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2364 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2365 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2366 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2367 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2368 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2369 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2370 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2371 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2372 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2373 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2374 ----------------------------------------------------------------------
2376 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2377 of dimension 1, chars 94, and final character <F>, etc...
2379 Note (*): Although these designations are not allowed in ISO2022,
2380 Emacs accepts them on decoding, and produces them on encoding
2381 CHARS96 character sets in a coding system which is characterized as
2382 7-bit environment, non-locking-shift, and non-single-shift.
2384 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2385 '(' must be omitted. We refer to this as "short-form" hereafter.
2387 Now you may notice that there are a lot of ways of encoding the
2388 same multilingual text in ISO2022. Actually, there exist many
2389 coding systems such as Compound Text (used in X11's inter client
2390 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2391 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2392 localized platforms), and all of these are variants of ISO2022.
2394 In addition to the above, Emacs handles two more kinds of escape
2395 sequences: ISO6429's direction specification and Emacs' private
2396 sequence for specifying character composition.
2398 ISO6429's direction specification takes the following form:
2399 o CSI ']' -- end of the current direction
2400 o CSI '0' ']' -- end of the current direction
2401 o CSI '1' ']' -- start of left-to-right text
2402 o CSI '2' ']' -- start of right-to-left text
2403 The control character CSI (0x9B: control sequence introducer) is
2404 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2406 Character composition specification takes the following form:
2407 o ESC '0' -- start relative composition
2408 o ESC '1' -- end composition
2409 o ESC '2' -- start rule-base composition (*)
2410 o ESC '3' -- start relative composition with alternate chars (**)
2411 o ESC '4' -- start rule-base composition with alternate chars (**)
2412 Since these are not standard escape sequences of any ISO standard,
2413 the use of them with these meanings is restricted to Emacs only.
2415 (*) This form is used only in Emacs 20.7 and older versions,
2416 but newer versions can safely decode it.
2417 (**) This form is used only in Emacs 21.1 and newer versions,
2418 and older versions can't decode it.
2420 Here's a list of example usages of these composition escape
2421 sequences (categorized by `enum composition_method').
2423 COMPOSITION_RELATIVE:
2424 ESC 0 CHAR [ CHAR ] ESC 1
2425 COMPOSITION_WITH_RULE:
2426 ESC 2 CHAR [ RULE CHAR ] ESC 1
2427 COMPOSITION_WITH_ALTCHARS:
2428 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2429 COMPOSITION_WITH_RULE_ALTCHARS:
2430 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2432 enum iso_code_class_type iso_code_class
[256];
2434 #define SAFE_CHARSET_P(coding, id) \
2435 ((id) <= (coding)->max_charset_id \
2436 && (coding)->safe_charsets[id] >= 0)
2439 #define SHIFT_OUT_OK(category) \
2440 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2443 setup_iso_safe_charsets (attrs
)
2446 Lisp_Object charset_list
, safe_charsets
;
2447 Lisp_Object request
;
2448 Lisp_Object reg_usage
;
2451 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2454 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2455 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2456 && ! EQ (charset_list
, Viso_2022_charset_list
))
2458 CODING_ATTR_CHARSET_LIST (attrs
)
2459 = charset_list
= Viso_2022_charset_list
;
2460 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2463 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2467 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2469 int id
= XINT (XCAR (tail
));
2470 if (max_charset_id
< id
)
2471 max_charset_id
= id
;
2474 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
2476 request
= AREF (attrs
, coding_attr_iso_request
);
2477 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2478 reg94
= XINT (XCAR (reg_usage
));
2479 reg96
= XINT (XCDR (reg_usage
));
2481 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2485 struct charset
*charset
;
2488 charset
= CHARSET_FROM_ID (XINT (id
));
2489 reg
= Fcdr (Fassq (id
, request
));
2491 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2492 else if (charset
->iso_chars_96
)
2495 SSET (safe_charsets
, XINT (id
), reg96
);
2500 SSET (safe_charsets
, XINT (id
), reg94
);
2503 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2507 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2508 Check if a text is encoded in one of ISO-2022 based codig systems.
2509 If it is, return 1, else return 0. */
2512 detect_coding_iso_2022 (coding
, detect_info
)
2513 struct coding_system
*coding
;
2514 struct coding_detection_info
*detect_info
;
2516 const unsigned char *src
= coding
->source
, *src_base
= src
;
2517 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2518 int multibytep
= coding
->src_multibyte
;
2519 int single_shifting
= 0;
2522 int consumed_chars
= 0;
2527 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2529 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2531 struct coding_system
*this = &(coding_categories
[i
]);
2532 Lisp_Object attrs
, val
;
2534 attrs
= CODING_ID_ATTRS (this->id
);
2535 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2536 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs
), Viso_2022_charset_list
))
2537 setup_iso_safe_charsets (attrs
);
2538 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2539 this->max_charset_id
= SCHARS (val
) - 1;
2540 this->safe_charsets
= (char *) SDATA (val
);
2543 /* A coding system of this category is always ASCII compatible. */
2544 src
+= coding
->head_ascii
;
2546 while (rejected
!= CATEGORY_MASK_ISO
)
2553 if (inhibit_iso_escape_detection
)
2555 single_shifting
= 0;
2557 if (c
>= '(' && c
<= '/')
2559 /* Designation sequence for a charset of dimension 1. */
2561 if (c1
< ' ' || c1
>= 0x80
2562 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2563 /* Invalid designation sequence. Just ignore. */
2568 /* Designation sequence for a charset of dimension 2. */
2570 if (c
>= '@' && c
<= 'B')
2571 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2572 id
= iso_charset_table
[1][0][c
];
2573 else if (c
>= '(' && c
<= '/')
2576 if (c1
< ' ' || c1
>= 0x80
2577 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2578 /* Invalid designation sequence. Just ignore. */
2582 /* Invalid designation sequence. Just ignore it. */
2585 else if (c
== 'N' || c
== 'O')
2587 /* ESC <Fe> for SS2 or SS3. */
2588 single_shifting
= 1;
2589 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2592 else if (c
>= '0' && c
<= '4')
2594 /* ESC <Fp> for start/end composition. */
2595 found
|= CATEGORY_MASK_ISO
;
2600 /* Invalid escape sequence. Just ignore it. */
2604 /* We found a valid designation sequence for CHARSET. */
2605 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2606 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2608 found
|= CATEGORY_MASK_ISO_7
;
2610 rejected
|= CATEGORY_MASK_ISO_7
;
2611 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
2613 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
2615 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
2616 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
2618 found
|= CATEGORY_MASK_ISO_7_ELSE
;
2620 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
2621 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
2623 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2625 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
2630 /* Locking shift out/in. */
2631 if (inhibit_iso_escape_detection
)
2633 single_shifting
= 0;
2634 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2635 found
|= CATEGORY_MASK_ISO_ELSE
;
2639 /* Control sequence introducer. */
2640 single_shifting
= 0;
2641 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2642 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2643 goto check_extra_latin
;
2648 if (inhibit_iso_escape_detection
)
2650 single_shifting
= 0;
2651 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2652 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2653 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2654 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2655 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2656 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2657 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2658 if (single_shifting
)
2660 goto check_extra_latin
;
2667 single_shifting
= 0;
2672 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2673 found
|= CATEGORY_MASK_ISO_8_1
;
2674 /* Check the length of succeeding codes of the range
2675 0xA0..0FF. If the byte length is even, we include
2676 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2677 only when we are not single shifting. */
2678 if (! single_shifting
2679 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2682 while (src
< src_end
)
2690 if (i
& 1 && src
< src_end
)
2691 rejected
|= CATEGORY_MASK_ISO_8_2
;
2693 found
|= CATEGORY_MASK_ISO_8_2
;
2698 single_shifting
= 0;
2699 if (! VECTORP (Vlatin_extra_code_table
)
2700 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
2702 rejected
= CATEGORY_MASK_ISO
;
2705 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2706 & CODING_ISO_FLAG_LATIN_EXTRA
)
2707 found
|= CATEGORY_MASK_ISO_8_1
;
2709 rejected
|= CATEGORY_MASK_ISO_8_1
;
2710 rejected
|= CATEGORY_MASK_ISO_8_2
;
2713 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
2717 detect_info
->rejected
|= rejected
;
2718 detect_info
->found
|= (found
& ~rejected
);
2723 /* Set designation state into CODING. Set CHARS_96 to -1 if the
2724 escape sequence should be kept. */
2725 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2729 if (final < '0' || final >= 128 \
2730 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2731 || !SAFE_CHARSET_P (coding, id)) \
2733 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2737 prev = CODING_ISO_DESIGNATION (coding, reg); \
2738 if (id == charset_jisx0201_roman) \
2740 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2741 id = charset_ascii; \
2743 else if (id == charset_jisx0208_1978) \
2745 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2746 id = charset_jisx0208; \
2748 CODING_ISO_DESIGNATION (coding, reg) = id; \
2749 /* If there was an invalid designation to REG previously, and this \
2750 designation is ASCII to REG, we should keep this designation \
2752 if (prev == -2 && id == charset_ascii) \
2757 #define MAYBE_FINISH_COMPOSITION() \
2760 if (composition_state == COMPOSING_NO) \
2762 /* It is assured that we have enough room for producing \
2763 characters stored in the table `components'. */ \
2764 if (charbuf + component_idx > charbuf_end) \
2765 goto no_more_source; \
2766 composition_state = COMPOSING_NO; \
2767 if (method == COMPOSITION_RELATIVE \
2768 || method == COMPOSITION_WITH_ALTCHARS) \
2770 for (i = 0; i < component_idx; i++) \
2771 *charbuf++ = components[i]; \
2772 char_offset += component_idx; \
2776 for (i = 0; i < component_idx; i += 2) \
2777 *charbuf++ = components[i]; \
2778 char_offset += (component_idx / 2) + 1; \
2783 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2784 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2785 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2786 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2787 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2790 #define DECODE_COMPOSITION_START(c1) \
2793 && composition_state == COMPOSING_COMPONENT_RULE) \
2795 component_len = component_idx; \
2796 composition_state = COMPOSING_CHAR; \
2800 const unsigned char *p; \
2802 MAYBE_FINISH_COMPOSITION (); \
2803 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2804 goto no_more_source; \
2805 for (p = src; p < src_end - 1; p++) \
2806 if (*p == ISO_CODE_ESC && p[1] == '1') \
2808 if (p == src_end - 1) \
2810 if (coding->mode & CODING_MODE_LAST_BLOCK) \
2811 goto invalid_code; \
2812 goto no_more_source; \
2815 /* This is surely the start of a composition. */ \
2816 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2817 : c1 == '2' ? COMPOSITION_WITH_RULE \
2818 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2819 : COMPOSITION_WITH_RULE_ALTCHARS); \
2820 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2821 : COMPOSING_COMPONENT_CHAR); \
2822 component_idx = component_len = 0; \
2827 /* Handle compositoin end sequence ESC 1. */
2829 #define DECODE_COMPOSITION_END() \
2831 int nchars = (component_len > 0 ? component_idx - component_len \
2832 : method == COMPOSITION_RELATIVE ? component_idx \
2833 : (component_idx + 1) / 2); \
2835 int *saved_charbuf = charbuf; \
2837 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2838 if (method != COMPOSITION_RELATIVE) \
2840 if (component_len == 0) \
2841 for (i = 0; i < component_idx; i++) \
2842 *charbuf++ = components[i]; \
2844 for (i = 0; i < component_len; i++) \
2845 *charbuf++ = components[i]; \
2846 *saved_charbuf = saved_charbuf - charbuf; \
2848 if (method == COMPOSITION_WITH_RULE) \
2849 for (i = 0; i < component_idx; i += 2, char_offset++) \
2850 *charbuf++ = components[i]; \
2852 for (i = component_len; i < component_idx; i++, char_offset++) \
2853 *charbuf++ = components[i]; \
2854 coding->annotated = 1; \
2855 composition_state = COMPOSING_NO; \
2859 /* Decode a composition rule from the byte C1 (and maybe one more byte
2860 from SRC) and store one encoded composition rule in
2861 coding->cmp_data. */
2863 #define DECODE_COMPOSITION_RULE(c1) \
2866 if (c1 < 81) /* old format (before ver.21) */ \
2868 int gref = (c1) / 9; \
2869 int nref = (c1) % 9; \
2870 if (gref == 4) gref = 10; \
2871 if (nref == 4) nref = 10; \
2872 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
2874 else if (c1 < 93) /* new format (after ver.21) */ \
2876 ONE_MORE_BYTE (c2); \
2877 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
2884 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2887 decode_coding_iso_2022 (coding
)
2888 struct coding_system
*coding
;
2890 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2891 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2892 const unsigned char *src_base
;
2893 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2895 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
2896 int consumed_chars
= 0, consumed_chars_base
;
2897 int multibytep
= coding
->src_multibyte
;
2898 /* Charsets invoked to graphic plane 0 and 1 respectively. */
2899 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2900 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
2901 int charset_id_2
, charset_id_3
;
2902 struct charset
*charset
;
2904 /* For handling composition sequence. */
2905 #define COMPOSING_NO 0
2906 #define COMPOSING_CHAR 1
2907 #define COMPOSING_RULE 2
2908 #define COMPOSING_COMPONENT_CHAR 3
2909 #define COMPOSING_COMPONENT_RULE 4
2911 int composition_state
= COMPOSING_NO
;
2912 enum composition_method method
;
2913 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
2916 Lisp_Object attrs
, charset_list
;
2917 int char_offset
= coding
->produced_char
;
2918 int last_offset
= char_offset
;
2919 int last_id
= charset_ascii
;
2921 CODING_GET_INFO (coding
, attrs
, charset_list
);
2922 setup_iso_safe_charsets (attrs
);
2929 consumed_chars_base
= consumed_chars
;
2931 if (charbuf
>= charbuf_end
)
2938 /* We produce at most one character. */
2939 switch (iso_code_class
[c1
])
2941 case ISO_0x20_or_0x7F
:
2942 if (composition_state
!= COMPOSING_NO
)
2944 if (composition_state
== COMPOSING_RULE
2945 || composition_state
== COMPOSING_COMPONENT_RULE
)
2947 DECODE_COMPOSITION_RULE (c1
);
2948 components
[component_idx
++] = c1
;
2949 composition_state
--;
2953 if (charset_id_0
< 0
2954 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
2955 /* This is SPACE or DEL. */
2956 charset
= CHARSET_FROM_ID (charset_ascii
);
2958 charset
= CHARSET_FROM_ID (charset_id_0
);
2961 case ISO_graphic_plane_0
:
2962 if (composition_state
!= COMPOSING_NO
)
2964 if (composition_state
== COMPOSING_RULE
2965 || composition_state
== COMPOSING_COMPONENT_RULE
)
2967 DECODE_COMPOSITION_RULE (c1
);
2968 components
[component_idx
++] = c1
;
2969 composition_state
--;
2973 if (charset_id_0
< 0)
2974 charset
= CHARSET_FROM_ID (charset_ascii
);
2976 charset
= CHARSET_FROM_ID (charset_id_0
);
2979 case ISO_0xA0_or_0xFF
:
2980 if (charset_id_1
< 0
2981 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
2982 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
2984 /* This is a graphic character, we fall down ... */
2986 case ISO_graphic_plane_1
:
2987 if (charset_id_1
< 0)
2989 charset
= CHARSET_FROM_ID (charset_id_1
);
2993 MAYBE_FINISH_COMPOSITION ();
2994 charset
= CHARSET_FROM_ID (charset_ascii
);
2998 MAYBE_FINISH_COMPOSITION ();
3002 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3003 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3005 CODING_ISO_INVOCATION (coding
, 0) = 1;
3006 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3010 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3012 CODING_ISO_INVOCATION (coding
, 0) = 0;
3013 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3016 case ISO_single_shift_2_7
:
3017 case ISO_single_shift_2
:
3018 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3020 /* SS2 is handled as an escape sequence of ESC 'N' */
3022 goto label_escape_sequence
;
3024 case ISO_single_shift_3
:
3025 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3027 /* SS2 is handled as an escape sequence of ESC 'O' */
3029 goto label_escape_sequence
;
3031 case ISO_control_sequence_introducer
:
3032 /* CSI is handled as an escape sequence of ESC '[' ... */
3034 goto label_escape_sequence
;
3038 label_escape_sequence
:
3039 /* Escape sequences handled here are invocation,
3040 designation, direction specification, and character
3041 composition specification. */
3044 case '&': /* revision of following character set */
3046 if (!(c1
>= '@' && c1
<= '~'))
3049 if (c1
!= ISO_CODE_ESC
)
3052 goto label_escape_sequence
;
3054 case '$': /* designation of 2-byte character set */
3055 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3061 if (c1
>= '@' && c1
<= 'B')
3062 { /* designation of JISX0208.1978, GB2312.1980,
3064 reg
= 0, chars96
= 0;
3066 else if (c1
>= 0x28 && c1
<= 0x2B)
3067 { /* designation of DIMENSION2_CHARS94 character set */
3068 reg
= c1
- 0x28, chars96
= 0;
3071 else if (c1
>= 0x2C && c1
<= 0x2F)
3072 { /* designation of DIMENSION2_CHARS96 character set */
3073 reg
= c1
- 0x2C, chars96
= 1;
3078 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3079 /* We must update these variables now. */
3081 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3083 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3089 case 'n': /* invocation of locking-shift-2 */
3090 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3091 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3093 CODING_ISO_INVOCATION (coding
, 0) = 2;
3094 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3097 case 'o': /* invocation of locking-shift-3 */
3098 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3099 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3101 CODING_ISO_INVOCATION (coding
, 0) = 3;
3102 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3105 case 'N': /* invocation of single-shift-2 */
3106 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3107 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3109 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3110 if (charset_id_2
< 0)
3111 charset
= CHARSET_FROM_ID (charset_ascii
);
3113 charset
= CHARSET_FROM_ID (charset_id_2
);
3115 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3119 case 'O': /* invocation of single-shift-3 */
3120 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3121 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3123 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3124 if (charset_id_3
< 0)
3125 charset
= CHARSET_FROM_ID (charset_ascii
);
3127 charset
= CHARSET_FROM_ID (charset_id_3
);
3129 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3133 case '0': case '2': case '3': case '4': /* start composition */
3134 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3136 DECODE_COMPOSITION_START (c1
);
3139 case '1': /* end composition */
3140 if (composition_state
== COMPOSING_NO
)
3142 DECODE_COMPOSITION_END ();
3145 case '[': /* specification of direction */
3146 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3148 /* For the moment, nested direction is not supported.
3149 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3150 left-to-right, and nozero means right-to-left. */
3154 case ']': /* end of the current direction */
3155 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3157 case '0': /* end of the current direction */
3158 case '1': /* start of left-to-right direction */
3161 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3166 case '2': /* start of right-to-left direction */
3169 coding
->mode
|= CODING_MODE_DIRECTION
;
3183 /* CTEXT extended segment:
3184 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3185 We keep these bytes as is for the moment.
3186 They may be decoded by post-read-conversion. */
3190 ONE_MORE_BYTE (dim
);
3193 size
= ((M
- 128) * 128) + (L
- 128);
3194 if (charbuf
+ 8 + size
> charbuf_end
)
3196 *charbuf
++ = ISO_CODE_ESC
;
3200 *charbuf
++ = BYTE8_TO_CHAR (M
);
3201 *charbuf
++ = BYTE8_TO_CHAR (L
);
3205 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3210 /* XFree86 extension for embedding UTF-8 in CTEXT:
3211 ESC % G --UTF-8-BYTES-- ESC % @
3212 We keep these bytes as is for the moment.
3213 They may be decoded by post-read-conversion. */
3216 if (p
+ 6 > charbuf_end
)
3218 *p
++ = ISO_CODE_ESC
;
3221 while (p
< charbuf_end
)
3224 if (c1
== ISO_CODE_ESC
3225 && src
+ 1 < src_end
3232 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3234 if (p
+ 3 > charbuf_end
)
3236 *p
++ = ISO_CODE_ESC
;
3247 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3252 if (c1
>= 0x28 && c1
<= 0x2B)
3253 { /* designation of DIMENSION1_CHARS94 character set */
3254 reg
= c1
- 0x28, chars96
= 0;
3257 else if (c1
>= 0x2C && c1
<= 0x2F)
3258 { /* designation of DIMENSION1_CHARS96 character set */
3259 reg
= c1
- 0x2C, chars96
= 1;
3264 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3265 /* We must update these variables now. */
3267 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3269 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3277 if (charset
->id
!= charset_ascii
3278 && last_id
!= charset
->id
)
3280 if (last_id
!= charset_ascii
)
3281 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3282 last_id
= charset
->id
;
3283 last_offset
= char_offset
;
3286 /* Now we know CHARSET and 1st position code C1 of a character.
3287 Produce a decoded character while getting 2nd position code
3290 if (CHARSET_DIMENSION (charset
) > 1)
3293 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3294 /* C2 is not in a valid range. */
3296 c1
= (c1
<< 8) | (c2
& 0x7F);
3297 if (CHARSET_DIMENSION (charset
) > 2)
3300 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3301 /* C2 is not in a valid range. */
3303 c1
= (c1
<< 8) | (c2
& 0x7F);
3307 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3310 MAYBE_FINISH_COMPOSITION ();
3311 for (; src_base
< src
; src_base
++, char_offset
++)
3313 if (ASCII_BYTE_P (*src_base
))
3314 *charbuf
++ = *src_base
;
3316 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3319 else if (composition_state
== COMPOSING_NO
)
3326 components
[component_idx
++] = c
;
3327 if (method
== COMPOSITION_WITH_RULE
3328 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3329 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3330 composition_state
++;
3335 MAYBE_FINISH_COMPOSITION ();
3337 consumed_chars
= consumed_chars_base
;
3339 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3349 if (last_id
!= charset_ascii
)
3350 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3351 coding
->consumed_char
+= consumed_chars_base
;
3352 coding
->consumed
= src_base
- coding
->source
;
3353 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3357 /* ISO2022 encoding stuff. */
3360 It is not enough to say just "ISO2022" on encoding, we have to
3361 specify more details. In Emacs, each coding system of ISO2022
3362 variant has the following specifications:
3363 1. Initial designation to G0 thru G3.
3364 2. Allows short-form designation?
3365 3. ASCII should be designated to G0 before control characters?
3366 4. ASCII should be designated to G0 at end of line?
3367 5. 7-bit environment or 8-bit environment?
3368 6. Use locking-shift?
3369 7. Use Single-shift?
3370 And the following two are only for Japanese:
3371 8. Use ASCII in place of JIS0201-1976-Roman?
3372 9. Use JISX0208-1983 in place of JISX0208-1978?
3373 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3374 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3378 /* Produce codes (escape sequence) for designating CHARSET to graphic
3379 register REG at DST, and increment DST. If <final-char> of CHARSET is
3380 '@', 'A', or 'B' and the coding system CODING allows, produce
3381 designation sequence of short-form. */
3383 #define ENCODE_DESIGNATION(charset, reg, coding) \
3385 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3386 char *intermediate_char_94 = "()*+"; \
3387 char *intermediate_char_96 = ",-./"; \
3388 int revision = -1; \
3391 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3392 revision = CHARSET_ISO_REVISION (charset); \
3394 if (revision >= 0) \
3396 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3397 EMIT_ONE_BYTE ('@' + revision); \
3399 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3400 if (CHARSET_DIMENSION (charset) == 1) \
3402 if (! CHARSET_ISO_CHARS_96 (charset)) \
3403 c = intermediate_char_94[reg]; \
3405 c = intermediate_char_96[reg]; \
3406 EMIT_ONE_ASCII_BYTE (c); \
3410 EMIT_ONE_ASCII_BYTE ('$'); \
3411 if (! CHARSET_ISO_CHARS_96 (charset)) \
3413 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3415 || final_char < '@' || final_char > 'B') \
3416 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3419 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3421 EMIT_ONE_ASCII_BYTE (final_char); \
3423 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3427 /* The following two macros produce codes (control character or escape
3428 sequence) for ISO2022 single-shift functions (single-shift-2 and
3431 #define ENCODE_SINGLE_SHIFT_2 \
3433 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3434 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3436 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3437 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3441 #define ENCODE_SINGLE_SHIFT_3 \
3443 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3444 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3446 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3447 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3451 /* The following four macros produce codes (control character or
3452 escape sequence) for ISO2022 locking-shift functions (shift-in,
3453 shift-out, locking-shift-2, and locking-shift-3). */
3455 #define ENCODE_SHIFT_IN \
3457 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3458 CODING_ISO_INVOCATION (coding, 0) = 0; \
3462 #define ENCODE_SHIFT_OUT \
3464 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3465 CODING_ISO_INVOCATION (coding, 0) = 1; \
3469 #define ENCODE_LOCKING_SHIFT_2 \
3471 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3472 CODING_ISO_INVOCATION (coding, 0) = 2; \
3476 #define ENCODE_LOCKING_SHIFT_3 \
3478 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3479 CODING_ISO_INVOCATION (coding, 0) = 3; \
3483 /* Produce codes for a DIMENSION1 character whose character set is
3484 CHARSET and whose position-code is C1. Designation and invocation
3485 sequences are also produced in advance if necessary. */
3487 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3489 int id = CHARSET_ID (charset); \
3491 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3492 && id == charset_ascii) \
3494 id = charset_jisx0201_roman; \
3495 charset = CHARSET_FROM_ID (id); \
3498 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3500 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3501 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3503 EMIT_ONE_BYTE (c1 | 0x80); \
3504 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3507 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3509 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3512 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3514 EMIT_ONE_BYTE (c1 | 0x80); \
3518 /* Since CHARSET is not yet invoked to any graphic planes, we \
3519 must invoke it, or, at first, designate it to some graphic \
3520 register. Then repeat the loop to actually produce the \
3522 dst = encode_invocation_designation (charset, coding, dst, \
3527 /* Produce codes for a DIMENSION2 character whose character set is
3528 CHARSET and whose position-codes are C1 and C2. Designation and
3529 invocation codes are also produced in advance if necessary. */
3531 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3533 int id = CHARSET_ID (charset); \
3535 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3536 && id == charset_jisx0208) \
3538 id = charset_jisx0208_1978; \
3539 charset = CHARSET_FROM_ID (id); \
3542 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3544 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3545 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3547 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3548 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3551 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3553 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3556 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3558 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3562 /* Since CHARSET is not yet invoked to any graphic planes, we \
3563 must invoke it, or, at first, designate it to some graphic \
3564 register. Then repeat the loop to actually produce the \
3566 dst = encode_invocation_designation (charset, coding, dst, \
3571 #define ENCODE_ISO_CHARACTER(charset, c) \
3573 int code = ENCODE_CHAR ((charset),(c)); \
3575 if (CHARSET_DIMENSION (charset) == 1) \
3576 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3578 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3582 /* Produce designation and invocation codes at a place pointed by DST
3583 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3587 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3588 struct charset
*charset
;
3589 struct coding_system
*coding
;
3593 int multibytep
= coding
->dst_multibyte
;
3594 int produced_chars
= *p_nchars
;
3595 int reg
; /* graphic register number */
3596 int id
= CHARSET_ID (charset
);
3598 /* At first, check designations. */
3599 for (reg
= 0; reg
< 4; reg
++)
3600 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3605 /* CHARSET is not yet designated to any graphic registers. */
3606 /* At first check the requested designation. */
3607 reg
= CODING_ISO_REQUEST (coding
, id
);
3609 /* Since CHARSET requests no special designation, designate it
3610 to graphic register 0. */
3613 ENCODE_DESIGNATION (charset
, reg
, coding
);
3616 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3617 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3619 /* Since the graphic register REG is not invoked to any graphic
3620 planes, invoke it to graphic plane 0. */
3623 case 0: /* graphic register 0 */
3627 case 1: /* graphic register 1 */
3631 case 2: /* graphic register 2 */
3632 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3633 ENCODE_SINGLE_SHIFT_2
;
3635 ENCODE_LOCKING_SHIFT_2
;
3638 case 3: /* graphic register 3 */
3639 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3640 ENCODE_SINGLE_SHIFT_3
;
3642 ENCODE_LOCKING_SHIFT_3
;
3647 *p_nchars
= produced_chars
;
3651 /* The following three macros produce codes for indicating direction
3653 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3655 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3656 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3658 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3662 #define ENCODE_DIRECTION_R2L() \
3664 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3665 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3669 #define ENCODE_DIRECTION_L2R() \
3671 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3672 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3676 /* Produce codes for designation and invocation to reset the graphic
3677 planes and registers to initial state. */
3678 #define ENCODE_RESET_PLANE_AND_REGISTER() \
3681 struct charset *charset; \
3683 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3685 for (reg = 0; reg < 4; reg++) \
3686 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3687 && (CODING_ISO_DESIGNATION (coding, reg) \
3688 != CODING_ISO_INITIAL (coding, reg))) \
3690 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3691 ENCODE_DESIGNATION (charset, reg, coding); \
3696 /* Produce designation sequences of charsets in the line started from
3697 SRC to a place pointed by DST, and return updated DST.
3699 If the current block ends before any end-of-line, we may fail to
3700 find all the necessary designations. */
3702 static unsigned char *
3703 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
3704 struct coding_system
*coding
;
3705 int *charbuf
, *charbuf_end
;
3708 struct charset
*charset
;
3709 /* Table of charsets to be designated to each graphic register. */
3711 int c
, found
= 0, reg
;
3712 int produced_chars
= 0;
3713 int multibytep
= coding
->dst_multibyte
;
3715 Lisp_Object charset_list
;
3717 attrs
= CODING_ID_ATTRS (coding
->id
);
3718 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3719 if (EQ (charset_list
, Qiso_2022
))
3720 charset_list
= Viso_2022_charset_list
;
3722 for (reg
= 0; reg
< 4; reg
++)
3732 charset
= char_charset (c
, charset_list
, NULL
);
3733 id
= CHARSET_ID (charset
);
3734 reg
= CODING_ISO_REQUEST (coding
, id
);
3735 if (reg
>= 0 && r
[reg
] < 0)
3744 for (reg
= 0; reg
< 4; reg
++)
3746 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
3747 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
3753 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3756 encode_coding_iso_2022 (coding
)
3757 struct coding_system
*coding
;
3759 int multibytep
= coding
->dst_multibyte
;
3760 int *charbuf
= coding
->charbuf
;
3761 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
3762 unsigned char *dst
= coding
->destination
+ coding
->produced
;
3763 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
3766 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3767 && CODING_ISO_BOL (coding
));
3768 int produced_chars
= 0;
3769 Lisp_Object attrs
, eol_type
, charset_list
;
3770 int ascii_compatible
;
3772 int preferred_charset_id
= -1;
3774 CODING_GET_INFO (coding
, attrs
, charset_list
);
3775 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
3776 if (VECTORP (eol_type
))
3779 setup_iso_safe_charsets (attrs
);
3780 /* Charset list may have been changed. */
3781 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
); \
3782 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3784 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
3786 while (charbuf
< charbuf_end
)
3788 ASSURE_DESTINATION (safe_room
);
3790 if (bol_designation
)
3792 unsigned char *dst_prev
= dst
;
3794 /* We have to produce designation sequences if any now. */
3795 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
3796 bol_designation
= 0;
3797 /* We are sure that designation sequences are all ASCII bytes. */
3798 produced_chars
+= dst
- dst_prev
;
3805 /* Handle an annotation. */
3808 case CODING_ANNOTATE_COMPOSITION_MASK
:
3809 /* Not yet implemented. */
3811 case CODING_ANNOTATE_CHARSET_MASK
:
3812 preferred_charset_id
= charbuf
[2];
3813 if (preferred_charset_id
>= 0
3814 && NILP (Fmemq (make_number (preferred_charset_id
),
3816 preferred_charset_id
= -1;
3825 /* Now encode the character C. */
3826 if (c
< 0x20 || c
== 0x7F)
3829 || (c
== '\r' && EQ (eol_type
, Qmac
)))
3831 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3832 ENCODE_RESET_PLANE_AND_REGISTER ();
3833 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
3837 for (i
= 0; i
< 4; i
++)
3838 CODING_ISO_DESIGNATION (coding
, i
)
3839 = CODING_ISO_INITIAL (coding
, i
);
3842 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
3844 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
3845 ENCODE_RESET_PLANE_AND_REGISTER ();
3846 EMIT_ONE_ASCII_BYTE (c
);
3848 else if (ASCII_CHAR_P (c
))
3850 if (ascii_compatible
)
3851 EMIT_ONE_ASCII_BYTE (c
);
3854 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
3855 ENCODE_ISO_CHARACTER (charset
, c
);
3858 else if (CHAR_BYTE8_P (c
))
3860 c
= CHAR_TO_BYTE8 (c
);
3865 struct charset
*charset
;
3867 if (preferred_charset_id
>= 0)
3869 charset
= CHARSET_FROM_ID (preferred_charset_id
);
3870 if (! CHAR_CHARSET_P (c
, charset
))
3871 charset
= char_charset (c
, charset_list
, NULL
);
3874 charset
= char_charset (c
, charset_list
, NULL
);
3877 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
3879 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
3880 charset
= CHARSET_FROM_ID (charset_ascii
);
3884 c
= coding
->default_char
;
3885 charset
= char_charset (c
, charset_list
, NULL
);
3888 ENCODE_ISO_CHARACTER (charset
, c
);
3892 if (coding
->mode
& CODING_MODE_LAST_BLOCK
3893 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3895 ASSURE_DESTINATION (safe_room
);
3896 ENCODE_RESET_PLANE_AND_REGISTER ();
3898 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
3899 CODING_ISO_BOL (coding
) = bol_designation
;
3900 coding
->produced_char
+= produced_chars
;
3901 coding
->produced
= dst
- coding
->destination
;
3906 /*** 8,9. SJIS and BIG5 handlers ***/
3908 /* Although SJIS and BIG5 are not ISO's coding system, they are used
3909 quite widely. So, for the moment, Emacs supports them in the bare
3910 C code. But, in the future, they may be supported only by CCL. */
3912 /* SJIS is a coding system encoding three character sets: ASCII, right
3913 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3914 as is. A character of charset katakana-jisx0201 is encoded by
3915 "position-code + 0x80". A character of charset japanese-jisx0208
3916 is encoded in 2-byte but two position-codes are divided and shifted
3917 so that it fit in the range below.
3919 --- CODE RANGE of SJIS ---
3920 (character set) (range)
3922 KATAKANA-JISX0201 0xA0 .. 0xDF
3923 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
3924 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
3925 -------------------------------
3929 /* BIG5 is a coding system encoding two character sets: ASCII and
3930 Big5. An ASCII character is encoded as is. Big5 is a two-byte
3931 character set and is encoded in two-byte.
3933 --- CODE RANGE of BIG5 ---
3934 (character set) (range)
3936 Big5 (1st byte) 0xA1 .. 0xFE
3937 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3938 --------------------------
3942 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3943 Check if a text is encoded in SJIS. If it is, return
3944 CATEGORY_MASK_SJIS, else return 0. */
3947 detect_coding_sjis (coding
, detect_info
)
3948 struct coding_system
*coding
;
3949 struct coding_detection_info
*detect_info
;
3951 const unsigned char *src
= coding
->source
, *src_base
;
3952 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3953 int multibytep
= coding
->src_multibyte
;
3954 int consumed_chars
= 0;
3958 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
3959 /* A coding system of this category is always ASCII compatible. */
3960 src
+= coding
->head_ascii
;
3968 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
3971 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
3973 found
= CATEGORY_MASK_SJIS
;
3975 else if (c
>= 0xA0 && c
< 0xE0)
3976 found
= CATEGORY_MASK_SJIS
;
3980 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
3984 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
3986 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
3989 detect_info
->found
|= found
;
3993 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3994 Check if a text is encoded in BIG5. If it is, return
3995 CATEGORY_MASK_BIG5, else return 0. */
3998 detect_coding_big5 (coding
, detect_info
)
3999 struct coding_system
*coding
;
4000 struct coding_detection_info
*detect_info
;
4002 const unsigned char *src
= coding
->source
, *src_base
;
4003 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4004 int multibytep
= coding
->src_multibyte
;
4005 int consumed_chars
= 0;
4009 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4010 /* A coding system of this category is always ASCII compatible. */
4011 src
+= coding
->head_ascii
;
4022 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4024 found
= CATEGORY_MASK_BIG5
;
4029 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4033 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4035 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4038 detect_info
->found
|= found
;
4042 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4043 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4046 decode_coding_sjis (coding
)
4047 struct coding_system
*coding
;
4049 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4050 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4051 const unsigned char *src_base
;
4052 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4054 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4055 int consumed_chars
= 0, consumed_chars_base
;
4056 int multibytep
= coding
->src_multibyte
;
4057 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4058 struct charset
*charset_kanji2
;
4059 Lisp_Object attrs
, charset_list
, val
;
4060 int char_offset
= coding
->produced_char
;
4061 int last_offset
= char_offset
;
4062 int last_id
= charset_ascii
;
4064 CODING_GET_INFO (coding
, attrs
, charset_list
);
4067 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4068 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4069 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4070 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4075 struct charset
*charset
;
4078 consumed_chars_base
= consumed_chars
;
4080 if (charbuf
>= charbuf_end
)
4087 charset
= charset_roman
;
4088 else if (c
== 0x80 || c
== 0xA0)
4090 else if (c
>= 0xA1 && c
<= 0xDF)
4092 /* SJIS -> JISX0201-Kana */
4094 charset
= charset_kana
;
4098 /* SJIS -> JISX0208 */
4100 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4104 charset
= charset_kanji
;
4106 else if (c
<= 0xFC && charset_kanji2
)
4108 /* SJIS -> JISX0213-2 */
4110 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4114 charset
= charset_kanji2
;
4118 if (charset
->id
!= charset_ascii
4119 && last_id
!= charset
->id
)
4121 if (last_id
!= charset_ascii
)
4122 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4123 last_id
= charset
->id
;
4124 last_offset
= char_offset
;
4126 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4133 consumed_chars
= consumed_chars_base
;
4135 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4141 if (last_id
!= charset_ascii
)
4142 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4143 coding
->consumed_char
+= consumed_chars_base
;
4144 coding
->consumed
= src_base
- coding
->source
;
4145 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4149 decode_coding_big5 (coding
)
4150 struct coding_system
*coding
;
4152 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4153 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4154 const unsigned char *src_base
;
4155 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4157 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4158 int consumed_chars
= 0, consumed_chars_base
;
4159 int multibytep
= coding
->src_multibyte
;
4160 struct charset
*charset_roman
, *charset_big5
;
4161 Lisp_Object attrs
, charset_list
, val
;
4162 int char_offset
= coding
->produced_char
;
4163 int last_offset
= char_offset
;
4164 int last_id
= charset_ascii
;
4166 CODING_GET_INFO (coding
, attrs
, charset_list
);
4168 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4169 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4174 struct charset
*charset
;
4177 consumed_chars_base
= consumed_chars
;
4179 if (charbuf
>= charbuf_end
)
4187 charset
= charset_roman
;
4191 if (c
< 0xA1 || c
> 0xFE)
4194 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4197 charset
= charset_big5
;
4199 if (charset
->id
!= charset_ascii
4200 && last_id
!= charset
->id
)
4202 if (last_id
!= charset_ascii
)
4203 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4204 last_id
= charset
->id
;
4205 last_offset
= char_offset
;
4207 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4214 consumed_chars
= consumed_chars_base
;
4216 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4222 if (last_id
!= charset_ascii
)
4223 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4224 coding
->consumed_char
+= consumed_chars_base
;
4225 coding
->consumed
= src_base
- coding
->source
;
4226 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4229 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4230 This function can encode charsets `ascii', `katakana-jisx0201',
4231 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4232 are sure that all these charsets are registered as official charset
4233 (i.e. do not have extended leading-codes). Characters of other
4234 charsets are produced without any encoding. If SJIS_P is 1, encode
4235 SJIS text, else encode BIG5 text. */
4238 encode_coding_sjis (coding
)
4239 struct coding_system
*coding
;
4241 int multibytep
= coding
->dst_multibyte
;
4242 int *charbuf
= coding
->charbuf
;
4243 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4244 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4245 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4247 int produced_chars
= 0;
4248 Lisp_Object attrs
, charset_list
, val
;
4249 int ascii_compatible
;
4250 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4251 struct charset
*charset_kanji2
;
4254 CODING_GET_INFO (coding
, attrs
, charset_list
);
4256 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4257 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4258 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4259 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4261 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4263 while (charbuf
< charbuf_end
)
4265 ASSURE_DESTINATION (safe_room
);
4267 /* Now encode the character C. */
4268 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4269 EMIT_ONE_ASCII_BYTE (c
);
4270 else if (CHAR_BYTE8_P (c
))
4272 c
= CHAR_TO_BYTE8 (c
);
4278 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4282 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4284 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4285 charset
= CHARSET_FROM_ID (charset_ascii
);
4289 c
= coding
->default_char
;
4290 charset
= char_charset (c
, charset_list
, &code
);
4293 if (code
== CHARSET_INVALID_CODE (charset
))
4295 if (charset
== charset_kanji
)
4299 c1
= code
>> 8, c2
= code
& 0xFF;
4300 EMIT_TWO_BYTES (c1
, c2
);
4302 else if (charset
== charset_kana
)
4303 EMIT_ONE_BYTE (code
| 0x80);
4304 else if (charset_kanji2
&& charset
== charset_kanji2
)
4309 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4310 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4312 JIS_TO_SJIS2 (code
);
4313 c1
= code
>> 8, c2
= code
& 0xFF;
4314 EMIT_TWO_BYTES (c1
, c2
);
4317 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4320 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4323 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4324 coding
->produced_char
+= produced_chars
;
4325 coding
->produced
= dst
- coding
->destination
;
4330 encode_coding_big5 (coding
)
4331 struct coding_system
*coding
;
4333 int multibytep
= coding
->dst_multibyte
;
4334 int *charbuf
= coding
->charbuf
;
4335 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4336 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4337 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4339 int produced_chars
= 0;
4340 Lisp_Object attrs
, charset_list
, val
;
4341 int ascii_compatible
;
4342 struct charset
*charset_roman
, *charset_big5
;
4345 CODING_GET_INFO (coding
, attrs
, charset_list
);
4347 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4348 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4349 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4351 while (charbuf
< charbuf_end
)
4353 ASSURE_DESTINATION (safe_room
);
4355 /* Now encode the character C. */
4356 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4357 EMIT_ONE_ASCII_BYTE (c
);
4358 else if (CHAR_BYTE8_P (c
))
4360 c
= CHAR_TO_BYTE8 (c
);
4366 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4370 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4372 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4373 charset
= CHARSET_FROM_ID (charset_ascii
);
4377 c
= coding
->default_char
;
4378 charset
= char_charset (c
, charset_list
, &code
);
4381 if (code
== CHARSET_INVALID_CODE (charset
))
4383 if (charset
== charset_big5
)
4387 c1
= code
>> 8, c2
= code
& 0xFF;
4388 EMIT_TWO_BYTES (c1
, c2
);
4391 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4394 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4395 coding
->produced_char
+= produced_chars
;
4396 coding
->produced
= dst
- coding
->destination
;
4401 /*** 10. CCL handlers ***/
4403 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4404 Check if a text is encoded in a coding system of which
4405 encoder/decoder are written in CCL program. If it is, return
4406 CATEGORY_MASK_CCL, else return 0. */
4409 detect_coding_ccl (coding
, detect_info
)
4410 struct coding_system
*coding
;
4411 struct coding_detection_info
*detect_info
;
4413 const unsigned char *src
= coding
->source
, *src_base
;
4414 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4415 int multibytep
= coding
->src_multibyte
;
4416 int consumed_chars
= 0;
4418 unsigned char *valids
;
4419 int head_ascii
= coding
->head_ascii
;
4422 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4424 coding
= &coding_categories
[coding_category_ccl
];
4425 valids
= CODING_CCL_VALIDS (coding
);
4426 attrs
= CODING_ID_ATTRS (coding
->id
);
4427 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4436 if (c
< 0 || ! valids
[c
])
4438 if ((valids
[c
] > 1))
4439 found
= CATEGORY_MASK_CCL
;
4441 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4445 detect_info
->found
|= found
;
4450 decode_coding_ccl (coding
)
4451 struct coding_system
*coding
;
4453 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4454 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4455 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4456 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4457 int consumed_chars
= 0;
4458 int multibytep
= coding
->src_multibyte
;
4459 struct ccl_program ccl
;
4460 int source_charbuf
[1024];
4461 int source_byteidx
[1024];
4462 Lisp_Object attrs
, charset_list
;
4464 CODING_GET_INFO (coding
, attrs
, charset_list
);
4465 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4467 while (src
< src_end
)
4469 const unsigned char *p
= src
;
4470 int *source
, *source_end
;
4474 while (i
< 1024 && p
< src_end
)
4476 source_byteidx
[i
] = p
- src
;
4477 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4480 while (i
< 1024 && p
< src_end
)
4481 source_charbuf
[i
++] = *p
++;
4483 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4486 source
= source_charbuf
;
4487 source_end
= source
+ i
;
4488 while (source
< source_end
)
4490 ccl_driver (&ccl
, source
, charbuf
,
4491 source_end
- source
, charbuf_end
- charbuf
,
4493 source
+= ccl
.consumed
;
4494 charbuf
+= ccl
.produced
;
4495 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4498 if (source
< source_end
)
4499 src
+= source_byteidx
[source
- source_charbuf
];
4502 consumed_chars
+= source
- source_charbuf
;
4504 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4505 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4511 case CCL_STAT_SUSPEND_BY_SRC
:
4512 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4514 case CCL_STAT_SUSPEND_BY_DST
:
4517 case CCL_STAT_INVALID_CMD
:
4518 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4521 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4524 coding
->consumed_char
+= consumed_chars
;
4525 coding
->consumed
= src
- coding
->source
;
4526 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4530 encode_coding_ccl (coding
)
4531 struct coding_system
*coding
;
4533 struct ccl_program ccl
;
4534 int multibytep
= coding
->dst_multibyte
;
4535 int *charbuf
= coding
->charbuf
;
4536 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4537 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4538 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4539 unsigned char *adjusted_dst_end
= dst_end
- 1;
4540 int destination_charbuf
[1024];
4541 int i
, produced_chars
= 0;
4542 Lisp_Object attrs
, charset_list
;
4544 CODING_GET_INFO (coding
, attrs
, charset_list
);
4545 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4547 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4548 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4550 while (charbuf
< charbuf_end
&& dst
< adjusted_dst_end
)
4552 int dst_bytes
= dst_end
- dst
;
4553 if (dst_bytes
> 1024)
4556 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4557 charbuf_end
- charbuf
, dst_bytes
, charset_list
);
4558 charbuf
+= ccl
.consumed
;
4560 for (i
= 0; i
< ccl
.produced
; i
++)
4561 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4564 for (i
= 0; i
< ccl
.produced
; i
++)
4565 *dst
++ = destination_charbuf
[i
] & 0xFF;
4566 produced_chars
+= ccl
.produced
;
4572 case CCL_STAT_SUSPEND_BY_SRC
:
4573 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4575 case CCL_STAT_SUSPEND_BY_DST
:
4576 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4579 case CCL_STAT_INVALID_CMD
:
4580 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4583 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4587 coding
->produced_char
+= produced_chars
;
4588 coding
->produced
= dst
- coding
->destination
;
4594 /*** 10, 11. no-conversion handlers ***/
4596 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4599 decode_coding_raw_text (coding
)
4600 struct coding_system
*coding
;
4602 coding
->chars_at_source
= 1;
4603 coding
->consumed_char
= 0;
4604 coding
->consumed
= 0;
4605 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4609 encode_coding_raw_text (coding
)
4610 struct coding_system
*coding
;
4612 int multibytep
= coding
->dst_multibyte
;
4613 int *charbuf
= coding
->charbuf
;
4614 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
4615 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4616 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4617 int produced_chars
= 0;
4622 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4624 if (coding
->src_multibyte
)
4625 while (charbuf
< charbuf_end
)
4627 ASSURE_DESTINATION (safe_room
);
4629 if (ASCII_CHAR_P (c
))
4630 EMIT_ONE_ASCII_BYTE (c
);
4631 else if (CHAR_BYTE8_P (c
))
4633 c
= CHAR_TO_BYTE8 (c
);
4638 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4640 CHAR_STRING_ADVANCE (c
, p1
);
4643 EMIT_ONE_BYTE (*p0
);
4649 while (charbuf
< charbuf_end
)
4651 ASSURE_DESTINATION (safe_room
);
4658 if (coding
->src_multibyte
)
4660 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4662 while (charbuf
< charbuf_end
)
4664 ASSURE_DESTINATION (safe_room
);
4666 if (ASCII_CHAR_P (c
))
4668 else if (CHAR_BYTE8_P (c
))
4669 *dst
++ = CHAR_TO_BYTE8 (c
);
4671 CHAR_STRING_ADVANCE (c
, dst
);
4677 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4678 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4679 *dst
++ = *charbuf
++;
4680 produced_chars
= dst
- (coding
->destination
+ coding
->dst_bytes
);
4683 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4684 coding
->produced_char
+= produced_chars
;
4685 coding
->produced
= dst
- coding
->destination
;
4689 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4690 Check if a text is encoded in a charset-based coding system. If it
4691 is, return 1, else return 0. */
4694 detect_coding_charset (coding
, detect_info
)
4695 struct coding_system
*coding
;
4696 struct coding_detection_info
*detect_info
;
4698 const unsigned char *src
= coding
->source
, *src_base
;
4699 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4700 int multibytep
= coding
->src_multibyte
;
4701 int consumed_chars
= 0;
4702 Lisp_Object attrs
, valids
;
4705 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4707 coding
= &coding_categories
[coding_category_charset
];
4708 attrs
= CODING_ID_ATTRS (coding
->id
);
4709 valids
= AREF (attrs
, coding_attr_charset_valids
);
4711 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4712 src
+= coding
->head_ascii
;
4722 if (NILP (AREF (valids
, c
)))
4725 found
= CATEGORY_MASK_CHARSET
;
4727 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4731 detect_info
->found
|= found
;
4736 decode_coding_charset (coding
)
4737 struct coding_system
*coding
;
4739 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4740 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4741 const unsigned char *src_base
;
4742 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4744 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4745 int consumed_chars
= 0, consumed_chars_base
;
4746 int multibytep
= coding
->src_multibyte
;
4747 Lisp_Object attrs
, charset_list
, valids
;
4748 int char_offset
= coding
->produced_char
;
4749 int last_offset
= char_offset
;
4750 int last_id
= charset_ascii
;
4752 CODING_GET_INFO (coding
, attrs
, charset_list
);
4753 valids
= AREF (attrs
, coding_attr_charset_valids
);
4759 struct charset
*charset
;
4765 consumed_chars_base
= consumed_chars
;
4767 if (charbuf
>= charbuf_end
)
4775 val
= AREF (valids
, c
);
4780 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4781 dim
= CHARSET_DIMENSION (charset
);
4785 code
= (code
<< 8) | c
;
4788 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
4793 /* VAL is a list of charset IDs. It is assured that the
4794 list is sorted by charset dimensions (smaller one
4798 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4799 dim
= CHARSET_DIMENSION (charset
);
4803 code
= (code
<< 8) | c
;
4806 CODING_DECODE_CHAR (coding
, src
, src_base
,
4807 src_end
, charset
, code
, c
);
4815 if (charset
->id
!= charset_ascii
4816 && last_id
!= charset
->id
)
4818 if (last_id
!= charset_ascii
)
4819 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4820 last_id
= charset
->id
;
4821 last_offset
= char_offset
;
4830 consumed_chars
= consumed_chars_base
;
4832 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4838 if (last_id
!= charset_ascii
)
4839 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4840 coding
->consumed_char
+= consumed_chars_base
;
4841 coding
->consumed
= src_base
- coding
->source
;
4842 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4846 encode_coding_charset (coding
)
4847 struct coding_system
*coding
;
4849 int multibytep
= coding
->dst_multibyte
;
4850 int *charbuf
= coding
->charbuf
;
4851 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4852 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4853 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4854 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4855 int produced_chars
= 0;
4856 Lisp_Object attrs
, charset_list
;
4857 int ascii_compatible
;
4860 CODING_GET_INFO (coding
, attrs
, charset_list
);
4861 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4863 while (charbuf
< charbuf_end
)
4865 struct charset
*charset
;
4868 ASSURE_DESTINATION (safe_room
);
4870 if (ascii_compatible
&& ASCII_CHAR_P (c
))
4871 EMIT_ONE_ASCII_BYTE (c
);
4872 else if (CHAR_BYTE8_P (c
))
4874 c
= CHAR_TO_BYTE8 (c
);
4879 charset
= char_charset (c
, charset_list
, &code
);
4882 if (CHARSET_DIMENSION (charset
) == 1)
4883 EMIT_ONE_BYTE (code
);
4884 else if (CHARSET_DIMENSION (charset
) == 2)
4885 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
4886 else if (CHARSET_DIMENSION (charset
) == 3)
4887 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
4889 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
4890 (code
>> 8) & 0xFF, code
& 0xFF);
4894 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4895 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4897 c
= coding
->default_char
;
4903 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4904 coding
->produced_char
+= produced_chars
;
4905 coding
->produced
= dst
- coding
->destination
;
4910 /*** 7. C library functions ***/
4912 /* Setup coding context CODING from information about CODING_SYSTEM.
4913 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4914 CODING_SYSTEM is invalid, signal an error. */
4917 setup_coding_system (coding_system
, coding
)
4918 Lisp_Object coding_system
;
4919 struct coding_system
*coding
;
4922 Lisp_Object eol_type
;
4923 Lisp_Object coding_type
;
4926 if (NILP (coding_system
))
4927 coding_system
= Qundecided
;
4929 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
4931 attrs
= CODING_ID_ATTRS (coding
->id
);
4932 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
4935 coding
->head_ascii
= -1;
4936 coding
->common_flags
4937 = (VECTORP (eol_type
) ? CODING_REQUIRE_DETECTION_MASK
: 0);
4938 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
4939 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
4940 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
4941 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
4942 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
4943 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
4945 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
4946 coding
->max_charset_id
= SCHARS (val
) - 1;
4947 coding
->safe_charsets
= (char *) SDATA (val
);
4948 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
4950 coding_type
= CODING_ATTR_TYPE (attrs
);
4951 if (EQ (coding_type
, Qundecided
))
4953 coding
->detector
= NULL
;
4954 coding
->decoder
= decode_coding_raw_text
;
4955 coding
->encoder
= encode_coding_raw_text
;
4956 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
4958 else if (EQ (coding_type
, Qiso_2022
))
4961 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
4963 /* Invoke graphic register 0 to plane 0. */
4964 CODING_ISO_INVOCATION (coding
, 0) = 0;
4965 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
4966 CODING_ISO_INVOCATION (coding
, 1)
4967 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
4968 /* Setup the initial status of designation. */
4969 for (i
= 0; i
< 4; i
++)
4970 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
4971 /* Not single shifting initially. */
4972 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
4973 /* Beginning of buffer should also be regarded as bol. */
4974 CODING_ISO_BOL (coding
) = 1;
4975 coding
->detector
= detect_coding_iso_2022
;
4976 coding
->decoder
= decode_coding_iso_2022
;
4977 coding
->encoder
= encode_coding_iso_2022
;
4978 if (flags
& CODING_ISO_FLAG_SAFE
)
4979 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
4980 coding
->common_flags
4981 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
4982 | CODING_REQUIRE_FLUSHING_MASK
);
4983 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
4984 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
4985 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
4986 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
4987 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
4989 setup_iso_safe_charsets (attrs
);
4990 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
4991 coding
->max_charset_id
= SCHARS (val
) - 1;
4992 coding
->safe_charsets
= (char *) SDATA (val
);
4994 CODING_ISO_FLAGS (coding
) = flags
;
4996 else if (EQ (coding_type
, Qcharset
))
4998 coding
->detector
= detect_coding_charset
;
4999 coding
->decoder
= decode_coding_charset
;
5000 coding
->encoder
= encode_coding_charset
;
5001 coding
->common_flags
5002 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5004 else if (EQ (coding_type
, Qutf_8
))
5006 coding
->detector
= detect_coding_utf_8
;
5007 coding
->decoder
= decode_coding_utf_8
;
5008 coding
->encoder
= encode_coding_utf_8
;
5009 coding
->common_flags
5010 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5012 else if (EQ (coding_type
, Qutf_16
))
5014 val
= AREF (attrs
, coding_attr_utf_16_bom
);
5015 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
5016 : EQ (val
, Qt
) ? utf_16_with_bom
5017 : utf_16_without_bom
);
5018 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5019 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5020 : utf_16_little_endian
);
5021 CODING_UTF_16_SURROGATE (coding
) = 0;
5022 coding
->detector
= detect_coding_utf_16
;
5023 coding
->decoder
= decode_coding_utf_16
;
5024 coding
->encoder
= encode_coding_utf_16
;
5025 coding
->common_flags
5026 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5027 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
5028 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5030 else if (EQ (coding_type
, Qccl
))
5032 coding
->detector
= detect_coding_ccl
;
5033 coding
->decoder
= decode_coding_ccl
;
5034 coding
->encoder
= encode_coding_ccl
;
5035 coding
->common_flags
5036 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5037 | CODING_REQUIRE_FLUSHING_MASK
);
5039 else if (EQ (coding_type
, Qemacs_mule
))
5041 coding
->detector
= detect_coding_emacs_mule
;
5042 coding
->decoder
= decode_coding_emacs_mule
;
5043 coding
->encoder
= encode_coding_emacs_mule
;
5044 coding
->common_flags
5045 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5046 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5047 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5049 Lisp_Object tail
, safe_charsets
;
5050 int max_charset_id
= 0;
5052 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5054 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5055 max_charset_id
= XFASTINT (XCAR (tail
));
5056 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5058 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5060 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5061 coding
->max_charset_id
= max_charset_id
;
5062 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5065 else if (EQ (coding_type
, Qshift_jis
))
5067 coding
->detector
= detect_coding_sjis
;
5068 coding
->decoder
= decode_coding_sjis
;
5069 coding
->encoder
= encode_coding_sjis
;
5070 coding
->common_flags
5071 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5073 else if (EQ (coding_type
, Qbig5
))
5075 coding
->detector
= detect_coding_big5
;
5076 coding
->decoder
= decode_coding_big5
;
5077 coding
->encoder
= encode_coding_big5
;
5078 coding
->common_flags
5079 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5081 else /* EQ (coding_type, Qraw_text) */
5083 coding
->detector
= NULL
;
5084 coding
->decoder
= decode_coding_raw_text
;
5085 coding
->encoder
= encode_coding_raw_text
;
5086 if (! EQ (eol_type
, Qunix
))
5088 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5089 if (! VECTORP (eol_type
))
5090 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5098 /* Return a list of charsets supported by CODING. */
5101 coding_charset_list (coding
)
5102 struct coding_system
*coding
;
5104 Lisp_Object attrs
, charset_list
;
5106 CODING_GET_INFO (coding
, attrs
, charset_list
);
5107 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5109 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5111 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5112 charset_list
= Viso_2022_charset_list
;
5114 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5116 charset_list
= Vemacs_mule_charset_list
;
5118 return charset_list
;
5122 /* Return raw-text or one of its subsidiaries that has the same
5123 eol_type as CODING-SYSTEM. */
5126 raw_text_coding_system (coding_system
)
5127 Lisp_Object coding_system
;
5129 Lisp_Object spec
, attrs
;
5130 Lisp_Object eol_type
, raw_text_eol_type
;
5132 if (NILP (coding_system
))
5134 spec
= CODING_SYSTEM_SPEC (coding_system
);
5135 attrs
= AREF (spec
, 0);
5137 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5138 return coding_system
;
5140 eol_type
= AREF (spec
, 2);
5141 if (VECTORP (eol_type
))
5143 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5144 raw_text_eol_type
= AREF (spec
, 2);
5145 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5146 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5147 : AREF (raw_text_eol_type
, 2));
5151 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5152 does, return one of the subsidiary that has the same eol-spec as
5153 PARENT. Otherwise, return CODING_SYSTEM. */
5156 coding_inherit_eol_type (coding_system
, parent
)
5157 Lisp_Object coding_system
, parent
;
5159 Lisp_Object spec
, eol_type
;
5161 if (NILP (coding_system
))
5162 coding_system
= Qraw_text
;
5163 spec
= CODING_SYSTEM_SPEC (coding_system
);
5164 eol_type
= AREF (spec
, 2);
5165 if (VECTORP (eol_type
)
5168 Lisp_Object parent_spec
;
5169 Lisp_Object parent_eol_type
;
5172 = CODING_SYSTEM_SPEC (buffer_defaults
.buffer_file_coding_system
);
5173 parent_eol_type
= AREF (parent_spec
, 2);
5174 if (EQ (parent_eol_type
, Qunix
))
5175 coding_system
= AREF (eol_type
, 0);
5176 else if (EQ (parent_eol_type
, Qdos
))
5177 coding_system
= AREF (eol_type
, 1);
5178 else if (EQ (parent_eol_type
, Qmac
))
5179 coding_system
= AREF (eol_type
, 2);
5181 return coding_system
;
5184 /* Emacs has a mechanism to automatically detect a coding system if it
5185 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5186 it's impossible to distinguish some coding systems accurately
5187 because they use the same range of codes. So, at first, coding
5188 systems are categorized into 7, those are:
5190 o coding-category-emacs-mule
5192 The category for a coding system which has the same code range
5193 as Emacs' internal format. Assigned the coding-system (Lisp
5194 symbol) `emacs-mule' by default.
5196 o coding-category-sjis
5198 The category for a coding system which has the same code range
5199 as SJIS. Assigned the coding-system (Lisp
5200 symbol) `japanese-shift-jis' by default.
5202 o coding-category-iso-7
5204 The category for a coding system which has the same code range
5205 as ISO2022 of 7-bit environment. This doesn't use any locking
5206 shift and single shift functions. This can encode/decode all
5207 charsets. Assigned the coding-system (Lisp symbol)
5208 `iso-2022-7bit' by default.
5210 o coding-category-iso-7-tight
5212 Same as coding-category-iso-7 except that this can
5213 encode/decode only the specified charsets.
5215 o coding-category-iso-8-1
5217 The category for a coding system which has the same code range
5218 as ISO2022 of 8-bit environment and graphic plane 1 used only
5219 for DIMENSION1 charset. This doesn't use any locking shift
5220 and single shift functions. Assigned the coding-system (Lisp
5221 symbol) `iso-latin-1' by default.
5223 o coding-category-iso-8-2
5225 The category for a coding system which has the same code range
5226 as ISO2022 of 8-bit environment and graphic plane 1 used only
5227 for DIMENSION2 charset. This doesn't use any locking shift
5228 and single shift functions. Assigned the coding-system (Lisp
5229 symbol) `japanese-iso-8bit' by default.
5231 o coding-category-iso-7-else
5233 The category for a coding system which has the same code range
5234 as ISO2022 of 7-bit environemnt but uses locking shift or
5235 single shift functions. Assigned the coding-system (Lisp
5236 symbol) `iso-2022-7bit-lock' by default.
5238 o coding-category-iso-8-else
5240 The category for a coding system which has the same code range
5241 as ISO2022 of 8-bit environemnt but uses locking shift or
5242 single shift functions. Assigned the coding-system (Lisp
5243 symbol) `iso-2022-8bit-ss2' by default.
5245 o coding-category-big5
5247 The category for a coding system which has the same code range
5248 as BIG5. Assigned the coding-system (Lisp symbol)
5249 `cn-big5' by default.
5251 o coding-category-utf-8
5253 The category for a coding system which has the same code range
5254 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5255 symbol) `utf-8' by default.
5257 o coding-category-utf-16-be
5259 The category for a coding system in which a text has an
5260 Unicode signature (cf. Unicode Standard) in the order of BIG
5261 endian at the head. Assigned the coding-system (Lisp symbol)
5262 `utf-16-be' by default.
5264 o coding-category-utf-16-le
5266 The category for a coding system in which a text has an
5267 Unicode signature (cf. Unicode Standard) in the order of
5268 LITTLE endian at the head. Assigned the coding-system (Lisp
5269 symbol) `utf-16-le' by default.
5271 o coding-category-ccl
5273 The category for a coding system of which encoder/decoder is
5274 written in CCL programs. The default value is nil, i.e., no
5275 coding system is assigned.
5277 o coding-category-binary
5279 The category for a coding system not categorized in any of the
5280 above. Assigned the coding-system (Lisp symbol)
5281 `no-conversion' by default.
5283 Each of them is a Lisp symbol and the value is an actual
5284 `coding-system's (this is also a Lisp symbol) assigned by a user.
5285 What Emacs does actually is to detect a category of coding system.
5286 Then, it uses a `coding-system' assigned to it. If Emacs can't
5287 decide only one possible category, it selects a category of the
5288 highest priority. Priorities of categories are also specified by a
5289 user in a Lisp variable `coding-category-list'.
5293 #define EOL_SEEN_NONE 0
5294 #define EOL_SEEN_LF 1
5295 #define EOL_SEEN_CR 2
5296 #define EOL_SEEN_CRLF 4
5298 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5299 SOURCE is encoded. If CATEGORY is one of
5300 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5301 two-byte, else they are encoded by one-byte.
5303 Return one of EOL_SEEN_XXX. */
5305 #define MAX_EOL_CHECK_COUNT 3
5308 detect_eol (source
, src_bytes
, category
)
5309 const unsigned char *source
;
5310 EMACS_INT src_bytes
;
5311 enum coding_category category
;
5313 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5316 int eol_seen
= EOL_SEEN_NONE
;
5318 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5322 msb
= category
== (coding_category_utf_16_le
5323 | coding_category_utf_16_le_nosig
);
5326 while (src
+ 1 < src_end
)
5329 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5334 this_eol
= EOL_SEEN_LF
;
5335 else if (src
+ 3 >= src_end
5336 || src
[msb
+ 2] != 0
5337 || src
[lsb
+ 2] != '\n')
5338 this_eol
= EOL_SEEN_CR
;
5340 this_eol
= EOL_SEEN_CRLF
;
5342 if (eol_seen
== EOL_SEEN_NONE
)
5343 /* This is the first end-of-line. */
5344 eol_seen
= this_eol
;
5345 else if (eol_seen
!= this_eol
)
5347 /* The found type is different from what found before. */
5348 eol_seen
= EOL_SEEN_LF
;
5351 if (++total
== MAX_EOL_CHECK_COUNT
)
5359 while (src
< src_end
)
5362 if (c
== '\n' || c
== '\r')
5367 this_eol
= EOL_SEEN_LF
;
5368 else if (src
>= src_end
|| *src
!= '\n')
5369 this_eol
= EOL_SEEN_CR
;
5371 this_eol
= EOL_SEEN_CRLF
, src
++;
5373 if (eol_seen
== EOL_SEEN_NONE
)
5374 /* This is the first end-of-line. */
5375 eol_seen
= this_eol
;
5376 else if (eol_seen
!= this_eol
)
5378 /* The found type is different from what found before. */
5379 eol_seen
= EOL_SEEN_LF
;
5382 if (++total
== MAX_EOL_CHECK_COUNT
)
5392 adjust_coding_eol_type (coding
, eol_seen
)
5393 struct coding_system
*coding
;
5396 Lisp_Object eol_type
;
5398 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5399 if (eol_seen
& EOL_SEEN_LF
)
5401 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5404 else if (eol_seen
& EOL_SEEN_CRLF
)
5406 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5409 else if (eol_seen
& EOL_SEEN_CR
)
5411 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5417 /* Detect how a text specified in CODING is encoded. If a coding
5418 system is detected, update fields of CODING by the detected coding
5422 detect_coding (coding
)
5423 struct coding_system
*coding
;
5425 const unsigned char *src
, *src_end
;
5427 coding
->consumed
= coding
->consumed_char
= 0;
5428 coding
->produced
= coding
->produced_char
= 0;
5429 coding_set_source (coding
);
5431 src_end
= coding
->source
+ coding
->src_bytes
;
5433 /* If we have not yet decided the text encoding type, detect it
5435 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5438 struct coding_detection_info detect_info
;
5440 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5441 for (i
= 0, src
= coding
->source
; src
< src_end
; i
++, src
++)
5447 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5448 && ! inhibit_iso_escape_detection
5449 && ! detect_info
.checked
)
5451 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5452 if (detect_coding_iso_2022 (coding
, &detect_info
))
5454 /* We have scanned the whole data. */
5455 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5456 /* We didn't find an 8-bit code. */
5462 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5464 if (coding
->head_ascii
< coding
->src_bytes
5465 || detect_info
.found
)
5467 enum coding_category category
;
5468 struct coding_system
*this;
5470 if (coding
->head_ascii
== coding
->src_bytes
)
5471 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5472 for (i
= 0; i
< coding_category_raw_text
; i
++)
5474 category
= coding_priorities
[i
];
5475 this = coding_categories
+ category
;
5476 if (detect_info
.found
& (1 << category
))
5480 for (i
= 0; i
< coding_category_raw_text
; i
++)
5482 category
= coding_priorities
[i
];
5483 this = coding_categories
+ category
;
5486 /* No coding system of this category is defined. */
5487 detect_info
.rejected
|= (1 << category
);
5489 else if (category
>= coding_category_raw_text
)
5491 else if (detect_info
.checked
& (1 << category
))
5493 if (detect_info
.found
& (1 << category
))
5496 else if ((*(this->detector
)) (coding
, &detect_info
)
5497 && detect_info
.found
& (1 << category
))
5499 if (category
== coding_category_utf_16_auto
)
5501 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5502 category
= coding_category_utf_16_le
;
5504 category
= coding_category_utf_16_be
;
5510 if (i
< coding_category_raw_text
)
5511 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5512 else if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
5513 setup_coding_system (Qraw_text
, coding
);
5514 else if (detect_info
.rejected
)
5515 for (i
= 0; i
< coding_category_raw_text
; i
++)
5516 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5518 this = coding_categories
+ coding_priorities
[i
];
5519 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5524 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5525 == coding_category_utf_16_auto
)
5527 Lisp_Object coding_systems
;
5528 struct coding_detection_info detect_info
;
5531 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5532 detect_info
.found
= detect_info
.rejected
= 0;
5533 if (CONSP (coding_systems
)
5534 && detect_coding_utf_16 (coding
, &detect_info
))
5536 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5537 setup_coding_system (XCAR (coding_systems
), coding
);
5538 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5539 setup_coding_system (XCDR (coding_systems
), coding
);
5547 struct coding_system
*coding
;
5549 Lisp_Object eol_type
;
5550 unsigned char *p
, *pbeg
, *pend
;
5552 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5553 if (EQ (eol_type
, Qunix
))
5556 if (NILP (coding
->dst_object
))
5557 pbeg
= coding
->destination
;
5559 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5560 pend
= pbeg
+ coding
->produced
;
5562 if (VECTORP (eol_type
))
5564 int eol_seen
= EOL_SEEN_NONE
;
5566 for (p
= pbeg
; p
< pend
; p
++)
5569 eol_seen
|= EOL_SEEN_LF
;
5570 else if (*p
== '\r')
5572 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5574 eol_seen
|= EOL_SEEN_CRLF
;
5578 eol_seen
|= EOL_SEEN_CR
;
5581 if (eol_seen
!= EOL_SEEN_NONE
5582 && eol_seen
!= EOL_SEEN_LF
5583 && eol_seen
!= EOL_SEEN_CRLF
5584 && eol_seen
!= EOL_SEEN_CR
)
5585 eol_seen
= EOL_SEEN_LF
;
5586 if (eol_seen
!= EOL_SEEN_NONE
)
5587 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5590 if (EQ (eol_type
, Qmac
))
5592 for (p
= pbeg
; p
< pend
; p
++)
5596 else if (EQ (eol_type
, Qdos
))
5600 if (NILP (coding
->dst_object
))
5602 for (p
= pend
- 2; p
>= pbeg
; p
--)
5605 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5611 for (p
= pend
- 2; p
>= pbeg
; p
--)
5614 int pos_byte
= coding
->dst_pos_byte
+ (p
- pbeg
);
5615 int pos
= BYTE_TO_CHAR (pos_byte
);
5617 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5621 coding
->produced
-= n
;
5622 coding
->produced_char
-= n
;
5627 /* Return a translation table (or list of them) from coding system
5628 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5629 decoding (ENCODEP is zero). */
5632 get_translation_table (attrs
, encodep
, max_lookup
)
5634 int encodep
, *max_lookup
;
5636 Lisp_Object standard
, translation_table
;
5640 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5641 standard
= Vstandard_translation_table_for_encode
;
5643 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5644 standard
= Vstandard_translation_table_for_decode
;
5645 if (NILP (translation_table
))
5646 translation_table
= standard
;
5649 if (SYMBOLP (translation_table
))
5650 translation_table
= Fget (translation_table
, Qtranslation_table
);
5651 else if (CONSP (translation_table
))
5653 translation_table
= Fcopy_sequence (translation_table
);
5654 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5655 if (SYMBOLP (XCAR (val
)))
5656 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5658 if (CHAR_TABLE_P (standard
))
5660 if (CONSP (translation_table
))
5661 translation_table
= nconc2 (translation_table
,
5662 Fcons (standard
, Qnil
));
5664 translation_table
= Fcons (translation_table
,
5665 Fcons (standard
, Qnil
));
5672 if (CHAR_TABLE_P (translation_table
)
5673 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5675 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5676 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5677 *max_lookup
= XFASTINT (val
);
5679 else if (CONSP (translation_table
))
5681 Lisp_Object tail
, val
;
5683 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
5684 if (CHAR_TABLE_P (XCAR (tail
))
5685 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
5687 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
5688 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5689 *max_lookup
= XFASTINT (val
);
5693 return translation_table
;
5696 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5699 if (CHAR_TABLE_P (table)) \
5701 trans = CHAR_TABLE_REF (table, c); \
5702 if (CHARACTERP (trans)) \
5703 c = XFASTINT (trans), trans = Qnil; \
5705 else if (CONSP (table)) \
5709 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5710 if (CHAR_TABLE_P (XCAR (tail))) \
5712 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5713 if (CHARACTERP (trans)) \
5714 c = XFASTINT (trans), trans = Qnil; \
5715 else if (! NILP (trans)) \
5723 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
5727 int *from_nchars
, *to_nchars
;
5729 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5733 Lisp_Object from
, tail
;
5736 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
5741 for (i
= 0; i
< len
; i
++)
5743 if (buf
+ i
== buf_end
)
5749 if (XINT (AREF (from
, i
)) != buf
[i
])
5763 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
5771 produce_chars (coding
, translation_table
, last_block
)
5772 struct coding_system
*coding
;
5773 Lisp_Object translation_table
;
5776 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5777 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5779 int produced_chars
= 0;
5782 if (! coding
->chars_at_source
)
5784 /* Characters are in coding->charbuf. */
5785 int *buf
= coding
->charbuf
;
5786 int *buf_end
= buf
+ coding
->charbuf_used
;
5788 if (BUFFERP (coding
->src_object
)
5789 && EQ (coding
->src_object
, coding
->dst_object
))
5790 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
5792 while (buf
< buf_end
)
5798 int from_nchars
= 1, to_nchars
= 1;
5799 Lisp_Object trans
= Qnil
;
5801 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
5804 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
5805 &from_nchars
, &to_nchars
);
5811 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
5813 dst
= alloc_destination (coding
,
5815 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
5817 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5820 for (i
= 0; i
< to_nchars
; i
++)
5823 c
= XINT (AREF (trans
, i
));
5824 if (coding
->dst_multibyte
5825 || ! CHAR_BYTE8_P (c
))
5826 CHAR_STRING_ADVANCE (c
, dst
);
5828 *dst
++ = CHAR_TO_BYTE8 (c
);
5830 produced_chars
+= to_nchars
;
5832 while (--from_nchars
> 0)
5836 /* This is an annotation datum. (-C) is the length. */
5839 carryover
= buf_end
- buf
;
5843 const unsigned char *src
= coding
->source
;
5844 const unsigned char *src_end
= src
+ coding
->src_bytes
;
5845 Lisp_Object eol_type
;
5847 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5849 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
5851 if (coding
->src_multibyte
)
5858 const unsigned char *src_base
= src
;
5864 if (EQ (eol_type
, Qdos
))
5868 record_conversion_result
5869 (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5870 goto no_more_source
;
5875 else if (EQ (eol_type
, Qmac
))
5880 coding
->consumed
= src
- coding
->source
;
5882 if (EQ (coding
->src_object
, coding
->dst_object
))
5883 dst_end
= (unsigned char *) src
;
5886 dst
= alloc_destination (coding
, src_end
- src
+ 1,
5888 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5889 coding_set_source (coding
);
5890 src
= coding
->source
+ coding
->consumed
;
5891 src_end
= coding
->source
+ coding
->src_bytes
;
5901 while (src
< src_end
)
5908 if (EQ (eol_type
, Qdos
))
5914 else if (EQ (eol_type
, Qmac
))
5917 if (dst
>= dst_end
- 1)
5919 coding
->consumed
= src
- coding
->source
;
5921 if (EQ (coding
->src_object
, coding
->dst_object
))
5922 dst_end
= (unsigned char *) src
;
5923 if (dst
>= dst_end
- 1)
5925 dst
= alloc_destination (coding
, src_end
- src
+ 2,
5927 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5928 coding_set_source (coding
);
5929 src
= coding
->source
+ coding
->consumed
;
5930 src_end
= coding
->source
+ coding
->src_bytes
;
5938 if (!EQ (coding
->src_object
, coding
->dst_object
))
5940 int require
= coding
->src_bytes
- coding
->dst_bytes
;
5944 EMACS_INT offset
= src
- coding
->source
;
5946 dst
= alloc_destination (coding
, require
, dst
);
5947 coding_set_source (coding
);
5948 src
= coding
->source
+ offset
;
5949 src_end
= coding
->source
+ coding
->src_bytes
;
5952 produced_chars
= coding
->src_chars
;
5953 while (src
< src_end
)
5959 if (EQ (eol_type
, Qdos
))
5966 else if (EQ (eol_type
, Qmac
))
5972 coding
->consumed
= coding
->src_bytes
;
5973 coding
->consumed_char
= coding
->src_chars
;
5976 produced
= dst
- (coding
->destination
+ coding
->produced
);
5977 if (BUFFERP (coding
->dst_object
))
5978 insert_from_gap (produced_chars
, produced
);
5979 coding
->produced
+= produced
;
5980 coding
->produced_char
+= produced_chars
;
5984 /* Compose text in CODING->object according to the annotation data at
5985 CHARBUF. CHARBUF is an array:
5986 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
5990 produce_composition (coding
, charbuf
, pos
)
5991 struct coding_system
*coding
;
5997 enum composition_method method
;
5998 Lisp_Object components
;
6001 to
= pos
+ charbuf
[2];
6004 method
= (enum composition_method
) (charbuf
[3]);
6006 if (method
== COMPOSITION_RELATIVE
)
6008 else if (method
>= COMPOSITION_WITH_RULE
6009 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6011 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6016 for (i
= 0; i
< len
; i
++)
6018 args
[i
] = make_number (charbuf
[i
]);
6022 components
= (method
== COMPOSITION_WITH_ALTCHARS
6023 ? Fstring (len
, args
) : Fvector (len
, args
));
6027 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6031 /* Put `charset' property on text in CODING->object according to
6032 the annotation data at CHARBUF. CHARBUF is an array:
6033 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6037 produce_charset (coding
, charbuf
, pos
)
6038 struct coding_system
*coding
;
6042 EMACS_INT from
= pos
- charbuf
[2];
6043 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6045 Fput_text_property (make_number (from
), make_number (pos
),
6046 Qcharset
, CHARSET_NAME (charset
),
6047 coding
->dst_object
);
6051 #define CHARBUF_SIZE 0x4000
6053 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6055 int size = CHARBUF_SIZE;; \
6057 coding->charbuf = NULL; \
6058 while (size > 1024) \
6060 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6061 if (coding->charbuf) \
6065 if (! coding->charbuf) \
6067 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6068 return coding->result; \
6070 coding->charbuf_size = size; \
6075 produce_annotation (coding
, pos
)
6076 struct coding_system
*coding
;
6079 int *charbuf
= coding
->charbuf
;
6080 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6082 if (NILP (coding
->dst_object
))
6085 while (charbuf
< charbuf_end
)
6091 int len
= -*charbuf
;
6094 case CODING_ANNOTATE_COMPOSITION_MASK
:
6095 produce_composition (coding
, charbuf
, pos
);
6097 case CODING_ANNOTATE_CHARSET_MASK
:
6098 produce_charset (coding
, charbuf
, pos
);
6108 /* Decode the data at CODING->src_object into CODING->dst_object.
6109 CODING->src_object is a buffer, a string, or nil.
6110 CODING->dst_object is a buffer.
6112 If CODING->src_object is a buffer, it must be the current buffer.
6113 In this case, if CODING->src_pos is positive, it is a position of
6114 the source text in the buffer, otherwise, the source text is in the
6115 gap area of the buffer, and CODING->src_pos specifies the offset of
6116 the text from GPT (which must be the same as PT). If this is the
6117 same buffer as CODING->dst_object, CODING->src_pos must be
6120 If CODING->src_object is a string, CODING->src_pos in an index to
6123 If CODING->src_object is nil, CODING->source must already point to
6124 the non-relocatable memory area. In this case, CODING->src_pos is
6125 an offset from CODING->source.
6127 The decoded data is inserted at the current point of the buffer
6132 decode_coding (coding
)
6133 struct coding_system
*coding
;
6136 Lisp_Object undo_list
;
6137 Lisp_Object translation_table
;
6141 if (BUFFERP (coding
->src_object
)
6142 && coding
->src_pos
> 0
6143 && coding
->src_pos
< GPT
6144 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6145 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6148 if (BUFFERP (coding
->dst_object
))
6150 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6151 set_buffer_internal (XBUFFER (coding
->dst_object
));
6153 move_gap_both (PT
, PT_BYTE
);
6154 undo_list
= current_buffer
->undo_list
;
6155 current_buffer
->undo_list
= Qt
;
6158 coding
->consumed
= coding
->consumed_char
= 0;
6159 coding
->produced
= coding
->produced_char
= 0;
6160 coding
->chars_at_source
= 0;
6161 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6164 ALLOC_CONVERSION_WORK_AREA (coding
);
6166 attrs
= CODING_ID_ATTRS (coding
->id
);
6167 translation_table
= get_translation_table (attrs
, 0, NULL
);
6172 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6174 coding_set_source (coding
);
6175 coding
->annotated
= 0;
6176 coding
->charbuf_used
= carryover
;
6177 (*(coding
->decoder
)) (coding
);
6178 coding_set_destination (coding
);
6179 carryover
= produce_chars (coding
, translation_table
, 0);
6180 if (coding
->annotated
)
6181 produce_annotation (coding
, pos
);
6182 for (i
= 0; i
< carryover
; i
++)
6184 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6186 while (coding
->consumed
< coding
->src_bytes
6187 && ! coding
->result
);
6191 coding_set_destination (coding
);
6192 coding
->charbuf_used
= carryover
;
6193 produce_chars (coding
, translation_table
, 1);
6196 coding
->carryover_bytes
= 0;
6197 if (coding
->consumed
< coding
->src_bytes
)
6199 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6200 const unsigned char *src
;
6202 coding_set_source (coding
);
6203 coding_set_destination (coding
);
6204 src
= coding
->source
+ coding
->consumed
;
6206 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6208 /* Flush out unprocessed data as binary chars. We are sure
6209 that the number of data is less than the size of
6211 coding
->charbuf_used
= 0;
6212 while (nbytes
-- > 0)
6217 c
= BYTE8_TO_CHAR (c
);
6218 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6220 produce_chars (coding
, Qnil
, 1);
6224 /* Record unprocessed bytes in coding->carryover. We are
6225 sure that the number of data is less than the size of
6226 coding->carryover. */
6227 unsigned char *p
= coding
->carryover
;
6229 coding
->carryover_bytes
= nbytes
;
6230 while (nbytes
-- > 0)
6233 coding
->consumed
= coding
->src_bytes
;
6236 if (BUFFERP (coding
->dst_object
))
6238 current_buffer
->undo_list
= undo_list
;
6239 record_insert (coding
->dst_pos
, coding
->produced_char
);
6241 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6242 decode_eol (coding
);
6243 return coding
->result
;
6247 /* Extract an annotation datum from a composition starting at POS and
6248 ending before LIMIT of CODING->src_object (buffer or string), store
6249 the data in BUF, set *STOP to a starting position of the next
6250 composition (if any) or to LIMIT, and return the address of the
6251 next element of BUF.
6253 If such an annotation is not found, set *STOP to a starting
6254 position of a composition after POS (if any) or to LIMIT, and
6258 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6259 EMACS_INT pos
, limit
;
6260 struct coding_system
*coding
;
6264 EMACS_INT start
, end
;
6267 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6270 else if (start
> pos
)
6276 /* We found a composition. Store the corresponding
6277 annotation data in BUF. */
6279 enum composition_method method
= COMPOSITION_METHOD (prop
);
6280 int nchars
= COMPOSITION_LENGTH (prop
);
6282 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6283 if (method
!= COMPOSITION_RELATIVE
)
6285 Lisp_Object components
;
6288 components
= COMPOSITION_COMPONENTS (prop
);
6289 if (VECTORP (components
))
6291 len
= XVECTOR (components
)->size
;
6292 for (i
= 0; i
< len
; i
++)
6293 *buf
++ = XINT (AREF (components
, i
));
6295 else if (STRINGP (components
))
6297 len
= SCHARS (components
);
6301 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6305 else if (INTEGERP (components
))
6308 *buf
++ = XINT (components
);
6310 else if (CONSP (components
))
6312 for (len
= 0; CONSP (components
);
6313 len
++, components
= XCDR (components
))
6314 *buf
++ = XINT (XCAR (components
));
6322 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6333 /* Extract an annotation datum from a text property `charset' at POS of
6334 CODING->src_object (buffer of string), store the data in BUF, set
6335 *STOP to the position where the value of `charset' property changes
6336 (limiting by LIMIT), and return the address of the next element of
6339 If the property value is nil, set *STOP to the position where the
6340 property value is non-nil (limiting by LIMIT), and return BUF. */
6343 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6344 EMACS_INT pos
, limit
;
6345 struct coding_system
*coding
;
6349 Lisp_Object val
, next
;
6352 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6353 if (! NILP (val
) && CHARSETP (val
))
6354 id
= XINT (CHARSET_SYMBOL_ID (val
));
6357 ADD_CHARSET_DATA (buf
, 0, id
);
6358 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6360 make_number (limit
));
6361 *stop
= XINT (next
);
6367 consume_chars (coding
, translation_table
, max_lookup
)
6368 struct coding_system
*coding
;
6369 Lisp_Object translation_table
;
6372 int *buf
= coding
->charbuf
;
6373 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6374 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6375 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6376 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6377 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6378 int multibytep
= coding
->src_multibyte
;
6379 Lisp_Object eol_type
;
6381 EMACS_INT stop
, stop_composition
, stop_charset
;
6382 int *lookup_buf
= NULL
;
6384 if (! NILP (translation_table
))
6385 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6387 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6388 if (VECTORP (eol_type
))
6391 /* Note: composition handling is not yet implemented. */
6392 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6394 if (NILP (coding
->src_object
))
6395 stop
= stop_composition
= stop_charset
= end_pos
;
6398 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6399 stop
= stop_composition
= pos
;
6401 stop
= stop_composition
= end_pos
;
6402 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6403 stop
= stop_charset
= pos
;
6405 stop_charset
= end_pos
;
6408 /* Compensate for CRLF and conversion. */
6409 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6410 while (buf
< buf_end
)
6418 if (pos
== stop_composition
)
6419 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6420 buf
, &stop_composition
);
6421 if (pos
== stop_charset
)
6422 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6423 buf
, &stop_charset
);
6424 stop
= (stop_composition
< stop_charset
6425 ? stop_composition
: stop_charset
);
6432 if (coding
->encoder
== encode_coding_raw_text
)
6434 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6435 c
= STRING_CHAR_ADVANCE (src
), pos
+= bytes
;
6437 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
6440 c
= STRING_CHAR_ADVANCE (src
), pos
++;
6441 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6443 if (! EQ (eol_type
, Qunix
))
6447 if (EQ (eol_type
, Qdos
))
6455 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6460 int from_nchars
= 1, to_nchars
= 1;
6461 int *lookup_buf_end
;
6462 const unsigned char *p
= src
;
6466 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6467 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6468 lookup_buf_end
= lookup_buf
+ i
;
6469 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6470 &from_nchars
, &to_nchars
);
6472 || buf
+ to_nchars
> buf_end
)
6474 *buf
++ = *lookup_buf
;
6475 for (i
= 1; i
< to_nchars
; i
++)
6476 *buf
++ = XINT (AREF (trans
, i
));
6477 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6478 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6482 coding
->consumed
= src
- coding
->source
;
6483 coding
->consumed_char
= pos
- coding
->src_pos
;
6484 coding
->charbuf_used
= buf
- coding
->charbuf
;
6485 coding
->chars_at_source
= 0;
6489 /* Encode the text at CODING->src_object into CODING->dst_object.
6490 CODING->src_object is a buffer or a string.
6491 CODING->dst_object is a buffer or nil.
6493 If CODING->src_object is a buffer, it must be the current buffer.
6494 In this case, if CODING->src_pos is positive, it is a position of
6495 the source text in the buffer, otherwise. the source text is in the
6496 gap area of the buffer, and coding->src_pos specifies the offset of
6497 the text from GPT (which must be the same as PT). If this is the
6498 same buffer as CODING->dst_object, CODING->src_pos must be
6499 negative and CODING should not have `pre-write-conversion'.
6501 If CODING->src_object is a string, CODING should not have
6502 `pre-write-conversion'.
6504 If CODING->dst_object is a buffer, the encoded data is inserted at
6505 the current point of that buffer.
6507 If CODING->dst_object is nil, the encoded data is placed at the
6508 memory area specified by CODING->destination. */
6511 encode_coding (coding
)
6512 struct coding_system
*coding
;
6515 Lisp_Object translation_table
;
6518 attrs
= CODING_ID_ATTRS (coding
->id
);
6519 if (coding
->encoder
== encode_coding_raw_text
)
6520 translation_table
= Qnil
, max_lookup
= 0;
6522 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6524 if (BUFFERP (coding
->dst_object
))
6526 set_buffer_internal (XBUFFER (coding
->dst_object
));
6527 coding
->dst_multibyte
6528 = ! NILP (current_buffer
->enable_multibyte_characters
);
6531 coding
->consumed
= coding
->consumed_char
= 0;
6532 coding
->produced
= coding
->produced_char
= 0;
6533 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6536 ALLOC_CONVERSION_WORK_AREA (coding
);
6539 coding_set_source (coding
);
6540 consume_chars (coding
, translation_table
, max_lookup
);
6541 coding_set_destination (coding
);
6542 (*(coding
->encoder
)) (coding
);
6543 } while (coding
->consumed_char
< coding
->src_chars
);
6545 if (BUFFERP (coding
->dst_object
))
6546 insert_from_gap (coding
->produced_char
, coding
->produced
);
6548 return (coding
->result
);
6552 /* Name (or base name) of work buffer for code conversion. */
6553 static Lisp_Object Vcode_conversion_workbuf_name
;
6555 /* A working buffer used by the top level conversion. Once it is
6556 created, it is never destroyed. It has the name
6557 Vcode_conversion_workbuf_name. The other working buffers are
6558 destroyed after the use is finished, and their names are modified
6559 versions of Vcode_conversion_workbuf_name. */
6560 static Lisp_Object Vcode_conversion_reused_workbuf
;
6562 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6563 static int reused_workbuf_in_use
;
6566 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6567 multibyteness of returning buffer. */
6570 make_conversion_work_buffer (multibyte
)
6573 Lisp_Object name
, workbuf
;
6574 struct buffer
*current
;
6576 if (reused_workbuf_in_use
++)
6578 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6579 workbuf
= Fget_buffer_create (name
);
6583 name
= Vcode_conversion_workbuf_name
;
6584 workbuf
= Fget_buffer_create (name
);
6585 if (NILP (Vcode_conversion_reused_workbuf
))
6586 Vcode_conversion_reused_workbuf
= workbuf
;
6588 current
= current_buffer
;
6589 set_buffer_internal (XBUFFER (workbuf
));
6591 current_buffer
->undo_list
= Qt
;
6592 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6593 set_buffer_internal (current
);
6599 code_conversion_restore (arg
)
6602 Lisp_Object current
, workbuf
;
6603 struct gcpro gcpro1
;
6606 current
= XCAR (arg
);
6607 workbuf
= XCDR (arg
);
6608 if (! NILP (workbuf
))
6610 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6611 reused_workbuf_in_use
= 0;
6612 else if (! NILP (Fbuffer_live_p (workbuf
)))
6613 Fkill_buffer (workbuf
);
6615 set_buffer_internal (XBUFFER (current
));
6621 code_conversion_save (with_work_buf
, multibyte
)
6622 int with_work_buf
, multibyte
;
6624 Lisp_Object workbuf
= Qnil
;
6627 workbuf
= make_conversion_work_buffer (multibyte
);
6628 record_unwind_protect (code_conversion_restore
,
6629 Fcons (Fcurrent_buffer (), workbuf
));
6634 decode_coding_gap (coding
, chars
, bytes
)
6635 struct coding_system
*coding
;
6636 EMACS_INT chars
, bytes
;
6638 int count
= specpdl_ptr
- specpdl
;
6641 code_conversion_save (0, 0);
6643 coding
->src_object
= Fcurrent_buffer ();
6644 coding
->src_chars
= chars
;
6645 coding
->src_bytes
= bytes
;
6646 coding
->src_pos
= -chars
;
6647 coding
->src_pos_byte
= -bytes
;
6648 coding
->src_multibyte
= chars
< bytes
;
6649 coding
->dst_object
= coding
->src_object
;
6650 coding
->dst_pos
= PT
;
6651 coding
->dst_pos_byte
= PT_BYTE
;
6652 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6653 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6655 if (CODING_REQUIRE_DETECTION (coding
))
6656 detect_coding (coding
);
6658 decode_coding (coding
);
6660 attrs
= CODING_ID_ATTRS (coding
->id
);
6661 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6663 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6666 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6667 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6668 make_number (coding
->produced_char
));
6670 coding
->produced_char
+= Z
- prev_Z
;
6671 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6674 unbind_to (count
, Qnil
);
6675 return coding
->result
;
6679 encode_coding_gap (coding
, chars
, bytes
)
6680 struct coding_system
*coding
;
6681 EMACS_INT chars
, bytes
;
6683 int count
= specpdl_ptr
- specpdl
;
6685 code_conversion_save (0, 0);
6687 coding
->src_object
= Fcurrent_buffer ();
6688 coding
->src_chars
= chars
;
6689 coding
->src_bytes
= bytes
;
6690 coding
->src_pos
= -chars
;
6691 coding
->src_pos_byte
= -bytes
;
6692 coding
->src_multibyte
= chars
< bytes
;
6693 coding
->dst_object
= coding
->src_object
;
6694 coding
->dst_pos
= PT
;
6695 coding
->dst_pos_byte
= PT_BYTE
;
6697 encode_coding (coding
);
6699 unbind_to (count
, Qnil
);
6700 return coding
->result
;
6704 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6705 SRC_OBJECT into DST_OBJECT by coding context CODING.
6707 SRC_OBJECT is a buffer, a string, or Qnil.
6709 If it is a buffer, the text is at point of the buffer. FROM and TO
6710 are positions in the buffer.
6712 If it is a string, the text is at the beginning of the string.
6713 FROM and TO are indices to the string.
6715 If it is nil, the text is at coding->source. FROM and TO are
6716 indices to coding->source.
6718 DST_OBJECT is a buffer, Qt, or Qnil.
6720 If it is a buffer, the decoded text is inserted at point of the
6721 buffer. If the buffer is the same as SRC_OBJECT, the source text
6724 If it is Qt, a string is made from the decoded text, and
6725 set in CODING->dst_object.
6727 If it is Qnil, the decoded text is stored at CODING->destination.
6728 The caller must allocate CODING->dst_bytes bytes at
6729 CODING->destination by xmalloc. If the decoded text is longer than
6730 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6734 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6736 struct coding_system
*coding
;
6737 Lisp_Object src_object
;
6738 EMACS_INT from
, from_byte
, to
, to_byte
;
6739 Lisp_Object dst_object
;
6741 int count
= specpdl_ptr
- specpdl
;
6742 unsigned char *destination
;
6743 EMACS_INT dst_bytes
;
6744 EMACS_INT chars
= to
- from
;
6745 EMACS_INT bytes
= to_byte
- from_byte
;
6748 int saved_pt
= -1, saved_pt_byte
;
6750 buffer
= Fcurrent_buffer ();
6752 if (NILP (dst_object
))
6754 destination
= coding
->destination
;
6755 dst_bytes
= coding
->dst_bytes
;
6758 coding
->src_object
= src_object
;
6759 coding
->src_chars
= chars
;
6760 coding
->src_bytes
= bytes
;
6761 coding
->src_multibyte
= chars
< bytes
;
6763 if (STRINGP (src_object
))
6765 coding
->src_pos
= from
;
6766 coding
->src_pos_byte
= from_byte
;
6768 else if (BUFFERP (src_object
))
6770 set_buffer_internal (XBUFFER (src_object
));
6772 move_gap_both (from
, from_byte
);
6773 if (EQ (src_object
, dst_object
))
6775 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6776 TEMP_SET_PT_BOTH (from
, from_byte
);
6777 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6778 coding
->src_pos
= -chars
;
6779 coding
->src_pos_byte
= -bytes
;
6783 coding
->src_pos
= from
;
6784 coding
->src_pos_byte
= from_byte
;
6788 if (CODING_REQUIRE_DETECTION (coding
))
6789 detect_coding (coding
);
6790 attrs
= CODING_ID_ATTRS (coding
->id
);
6792 if (EQ (dst_object
, Qt
)
6793 || (! NILP (CODING_ATTR_POST_READ (attrs
))
6794 && NILP (dst_object
)))
6796 coding
->dst_object
= code_conversion_save (1, 1);
6797 coding
->dst_pos
= BEG
;
6798 coding
->dst_pos_byte
= BEG_BYTE
;
6799 coding
->dst_multibyte
= 1;
6801 else if (BUFFERP (dst_object
))
6803 code_conversion_save (0, 0);
6804 coding
->dst_object
= dst_object
;
6805 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6806 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6807 coding
->dst_multibyte
6808 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6812 code_conversion_save (0, 0);
6813 coding
->dst_object
= Qnil
;
6814 coding
->dst_multibyte
= 1;
6817 decode_coding (coding
);
6819 if (BUFFERP (coding
->dst_object
))
6820 set_buffer_internal (XBUFFER (coding
->dst_object
));
6822 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6824 struct gcpro gcpro1
, gcpro2
;
6825 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6828 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6829 GCPRO2 (coding
->src_object
, coding
->dst_object
);
6830 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
6831 make_number (coding
->produced_char
));
6834 coding
->produced_char
+= Z
- prev_Z
;
6835 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6838 if (EQ (dst_object
, Qt
))
6840 coding
->dst_object
= Fbuffer_string ();
6842 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
6844 set_buffer_internal (XBUFFER (coding
->dst_object
));
6845 if (dst_bytes
< coding
->produced
)
6848 = (unsigned char *) xrealloc (destination
, coding
->produced
);
6851 record_conversion_result (coding
,
6852 CODING_RESULT_INSUFFICIENT_DST
);
6853 unbind_to (count
, Qnil
);
6856 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
6857 move_gap_both (BEGV
, BEGV_BYTE
);
6858 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
6859 coding
->destination
= destination
;
6865 /* This is the case of:
6866 (BUFFERP (src_object) && EQ (src_object, dst_object))
6867 As we have moved PT while replacing the original buffer
6868 contents, we must recover it now. */
6869 set_buffer_internal (XBUFFER (src_object
));
6870 if (saved_pt
< from
)
6871 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
6872 else if (saved_pt
< from
+ chars
)
6873 TEMP_SET_PT_BOTH (from
, from_byte
);
6874 else if (! NILP (current_buffer
->enable_multibyte_characters
))
6875 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
6876 saved_pt_byte
+ (coding
->produced
- bytes
));
6878 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
6879 saved_pt_byte
+ (coding
->produced
- bytes
));
6882 unbind_to (count
, coding
->dst_object
);
6887 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6889 struct coding_system
*coding
;
6890 Lisp_Object src_object
;
6891 EMACS_INT from
, from_byte
, to
, to_byte
;
6892 Lisp_Object dst_object
;
6894 int count
= specpdl_ptr
- specpdl
;
6895 EMACS_INT chars
= to
- from
;
6896 EMACS_INT bytes
= to_byte
- from_byte
;
6899 int saved_pt
= -1, saved_pt_byte
;
6900 int kill_src_buffer
= 0;
6902 buffer
= Fcurrent_buffer ();
6904 coding
->src_object
= src_object
;
6905 coding
->src_chars
= chars
;
6906 coding
->src_bytes
= bytes
;
6907 coding
->src_multibyte
= chars
< bytes
;
6909 attrs
= CODING_ID_ATTRS (coding
->id
);
6911 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
6913 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
6914 set_buffer_internal (XBUFFER (coding
->src_object
));
6915 if (STRINGP (src_object
))
6916 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
6917 else if (BUFFERP (src_object
))
6918 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
6920 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
6922 if (EQ (src_object
, dst_object
))
6924 set_buffer_internal (XBUFFER (src_object
));
6925 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6926 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6927 set_buffer_internal (XBUFFER (coding
->src_object
));
6931 Lisp_Object args
[3];
6933 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
6934 args
[1] = make_number (BEG
);
6935 args
[2] = make_number (Z
);
6936 safe_call (3, args
);
6938 if (XBUFFER (coding
->src_object
) != current_buffer
)
6939 kill_src_buffer
= 1;
6940 coding
->src_object
= Fcurrent_buffer ();
6942 move_gap_both (BEG
, BEG_BYTE
);
6943 coding
->src_chars
= Z
- BEG
;
6944 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
6945 coding
->src_pos
= BEG
;
6946 coding
->src_pos_byte
= BEG_BYTE
;
6947 coding
->src_multibyte
= Z
< Z_BYTE
;
6949 else if (STRINGP (src_object
))
6951 code_conversion_save (0, 0);
6952 coding
->src_pos
= from
;
6953 coding
->src_pos_byte
= from_byte
;
6955 else if (BUFFERP (src_object
))
6957 code_conversion_save (0, 0);
6958 set_buffer_internal (XBUFFER (src_object
));
6959 if (EQ (src_object
, dst_object
))
6961 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6962 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
6963 coding
->src_pos
= 0;
6964 coding
->src_pos_byte
= 0;
6968 if (from
< GPT
&& to
>= GPT
)
6969 move_gap_both (from
, from_byte
);
6970 coding
->src_pos
= from
;
6971 coding
->src_pos_byte
= from_byte
;
6975 code_conversion_save (0, 0);
6977 if (BUFFERP (dst_object
))
6979 coding
->dst_object
= dst_object
;
6980 if (EQ (src_object
, dst_object
))
6982 coding
->dst_pos
= from
;
6983 coding
->dst_pos_byte
= from_byte
;
6987 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6988 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6990 coding
->dst_multibyte
6991 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6993 else if (EQ (dst_object
, Qt
))
6995 coding
->dst_object
= Qnil
;
6996 coding
->dst_bytes
= coding
->src_chars
;
6997 if (coding
->dst_bytes
== 0)
6998 coding
->dst_bytes
= 1;
6999 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7000 coding
->dst_multibyte
= 0;
7004 coding
->dst_object
= Qnil
;
7005 coding
->dst_multibyte
= 0;
7008 encode_coding (coding
);
7010 if (EQ (dst_object
, Qt
))
7012 if (BUFFERP (coding
->dst_object
))
7013 coding
->dst_object
= Fbuffer_string ();
7017 = make_unibyte_string ((char *) coding
->destination
,
7019 xfree (coding
->destination
);
7025 /* This is the case of:
7026 (BUFFERP (src_object) && EQ (src_object, dst_object))
7027 As we have moved PT while replacing the original buffer
7028 contents, we must recover it now. */
7029 set_buffer_internal (XBUFFER (src_object
));
7030 if (saved_pt
< from
)
7031 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7032 else if (saved_pt
< from
+ chars
)
7033 TEMP_SET_PT_BOTH (from
, from_byte
);
7034 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7035 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7036 saved_pt_byte
+ (coding
->produced
- bytes
));
7038 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7039 saved_pt_byte
+ (coding
->produced
- bytes
));
7042 if (kill_src_buffer
)
7043 Fkill_buffer (coding
->src_object
);
7044 unbind_to (count
, Qnil
);
7049 preferred_coding_system ()
7051 int id
= coding_categories
[coding_priorities
[0]].id
;
7053 return CODING_ID_NAME (id
);
7058 /*** 8. Emacs Lisp library functions ***/
7060 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7061 doc
: /* Return t if OBJECT is nil or a coding-system.
7062 See the documentation of `define-coding-system' for information
7063 about coding-system objects. */)
7068 || CODING_SYSTEM_ID (obj
) >= 0)
7071 || NILP (Fget (obj
, Qcoding_system_define_form
)))
7076 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7077 Sread_non_nil_coding_system
, 1, 1, 0,
7078 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7085 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7086 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7088 while (SCHARS (val
) == 0);
7089 return (Fintern (val
, Qnil
));
7092 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7093 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7094 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
7095 (prompt
, default_coding_system
)
7096 Lisp_Object prompt
, default_coding_system
;
7099 if (SYMBOLP (default_coding_system
))
7100 XSETSTRING (default_coding_system
, XPNTR (SYMBOL_NAME (default_coding_system
)));
7101 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7102 Qt
, Qnil
, Qcoding_system_history
,
7103 default_coding_system
, Qnil
);
7104 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7107 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7109 doc
: /* Check validity of CODING-SYSTEM.
7110 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7111 It is valid if it is nil or a symbol defined as a coding system by the
7112 function `define-coding-system'. */)
7114 Lisp_Object coding_system
;
7116 Lisp_Object define_form
;
7118 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7119 if (! NILP (define_form
))
7121 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7122 safe_eval (define_form
);
7124 if (!NILP (Fcoding_system_p (coding_system
)))
7125 return coding_system
;
7127 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
7131 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7132 HIGHEST is nonzero, return the coding system of the highest
7133 priority among the detected coding systems. Otherwize return a
7134 list of detected coding systems sorted by their priorities. If
7135 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7136 multibyte form but contains only ASCII and eight-bit chars.
7137 Otherwise, the bytes are raw bytes.
7139 CODING-SYSTEM controls the detection as below:
7141 If it is nil, detect both text-format and eol-format. If the
7142 text-format part of CODING-SYSTEM is already specified
7143 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7144 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7145 detect only text-format. */
7148 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7150 const unsigned char *src
;
7151 int src_chars
, src_bytes
, highest
;
7153 Lisp_Object coding_system
;
7155 const unsigned char *src_end
= src
+ src_bytes
;
7156 Lisp_Object attrs
, eol_type
;
7158 struct coding_system coding
;
7160 struct coding_detection_info detect_info
;
7161 enum coding_category base_category
;
7163 if (NILP (coding_system
))
7164 coding_system
= Qundecided
;
7165 setup_coding_system (coding_system
, &coding
);
7166 attrs
= CODING_ID_ATTRS (coding
.id
);
7167 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7168 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7170 coding
.source
= src
;
7171 coding
.src_chars
= src_chars
;
7172 coding
.src_bytes
= src_bytes
;
7173 coding
.src_multibyte
= multibytep
;
7174 coding
.consumed
= 0;
7175 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7177 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7179 /* At first, detect text-format if necessary. */
7180 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7181 if (base_category
== coding_category_undecided
)
7183 enum coding_category category
;
7184 struct coding_system
*this;
7187 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7188 for (i
= 0; src
< src_end
; i
++, src
++)
7194 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7195 && inhibit_iso_escape_detection
)
7197 coding
.head_ascii
= src
- coding
.source
;
7198 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7200 /* We have scanned the whole data. */
7201 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7202 /* We didn't find an 8-bit code. */
7208 coding
.head_ascii
= src
- coding
.source
;
7211 || detect_info
.found
)
7214 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7215 for (i
= 0; i
< coding_category_raw_text
; i
++)
7217 category
= coding_priorities
[i
];
7218 if (detect_info
.found
& (1 << category
))
7222 for (i
= 0; i
< coding_category_raw_text
; i
++)
7224 category
= coding_priorities
[i
];
7225 this = coding_categories
+ category
;
7229 /* No coding system of this category is defined. */
7230 detect_info
.rejected
|= (1 << category
);
7232 else if (category
>= coding_category_raw_text
)
7234 else if (detect_info
.checked
& (1 << category
))
7237 && (detect_info
.found
& (1 << category
)))
7242 if ((*(this->detector
)) (&coding
, &detect_info
)
7244 && (detect_info
.found
& (1 << category
)))
7246 if (category
== coding_category_utf_16_auto
)
7248 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7249 category
= coding_category_utf_16_le
;
7251 category
= coding_category_utf_16_be
;
7259 if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
7261 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7262 id
= coding_categories
[coding_category_raw_text
].id
;
7263 val
= Fcons (make_number (id
), Qnil
);
7265 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7267 detect_info
.found
= CATEGORY_MASK_ANY
;
7268 id
= coding_categories
[coding_category_undecided
].id
;
7269 val
= Fcons (make_number (id
), Qnil
);
7273 if (detect_info
.found
)
7275 detect_info
.found
= 1 << category
;
7276 val
= Fcons (make_number (this->id
), Qnil
);
7279 for (i
= 0; i
< coding_category_raw_text
; i
++)
7280 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7282 detect_info
.found
= 1 << coding_priorities
[i
];
7283 id
= coding_categories
[coding_priorities
[i
]].id
;
7284 val
= Fcons (make_number (id
), Qnil
);
7290 int mask
= detect_info
.rejected
| detect_info
.found
;
7294 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7296 category
= coding_priorities
[i
];
7297 if (! (mask
& (1 << category
)))
7299 found
|= 1 << category
;
7300 id
= coding_categories
[category
].id
;
7301 val
= Fcons (make_number (id
), val
);
7304 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7306 category
= coding_priorities
[i
];
7307 if (detect_info
.found
& (1 << category
))
7309 id
= coding_categories
[category
].id
;
7310 val
= Fcons (make_number (id
), val
);
7313 detect_info
.found
|= found
;
7316 else if (base_category
== coding_category_utf_16_auto
)
7318 if (detect_coding_utf_16 (&coding
, &detect_info
))
7320 struct coding_system
*this;
7322 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7323 this = coding_categories
+ coding_category_utf_16_le
;
7324 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7325 this = coding_categories
+ coding_category_utf_16_be
;
7326 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7327 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7329 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7330 val
= Fcons (make_number (this->id
), Qnil
);
7335 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7336 val
= Fcons (make_number (coding
.id
), Qnil
);
7339 /* Then, detect eol-format if necessary. */
7341 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7344 if (VECTORP (eol_type
))
7346 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7347 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7348 coding_category_raw_text
);
7349 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7350 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7351 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7352 coding_category_utf_16_be
);
7353 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7354 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7355 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7356 coding_category_utf_16_le
);
7360 if (EQ (eol_type
, Qunix
))
7361 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7362 else if (EQ (eol_type
, Qdos
))
7363 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7365 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7368 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7370 enum coding_category category
;
7373 id
= XINT (XCAR (tail
));
7374 attrs
= CODING_ID_ATTRS (id
);
7375 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7376 eol_type
= CODING_ID_EOL_TYPE (id
);
7377 if (VECTORP (eol_type
))
7379 if (category
== coding_category_utf_16_be
7380 || category
== coding_category_utf_16_be_nosig
)
7381 this_eol
= utf_16_be_eol
;
7382 else if (category
== coding_category_utf_16_le
7383 || category
== coding_category_utf_16_le_nosig
)
7384 this_eol
= utf_16_le_eol
;
7386 this_eol
= normal_eol
;
7388 if (this_eol
== EOL_SEEN_LF
)
7389 XSETCAR (tail
, AREF (eol_type
, 0));
7390 else if (this_eol
== EOL_SEEN_CRLF
)
7391 XSETCAR (tail
, AREF (eol_type
, 1));
7392 else if (this_eol
== EOL_SEEN_CR
)
7393 XSETCAR (tail
, AREF (eol_type
, 2));
7395 XSETCAR (tail
, CODING_ID_NAME (id
));
7398 XSETCAR (tail
, CODING_ID_NAME (id
));
7402 return (highest
? XCAR (val
) : val
);
7406 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7408 doc
: /* Detect coding system of the text in the region between START and END.
7409 Return a list of possible coding systems ordered by priority.
7411 If only ASCII characters are found, it returns a list of single element
7412 `undecided' or its subsidiary coding system according to a detected
7415 If optional argument HIGHEST is non-nil, return the coding system of
7416 highest priority. */)
7417 (start
, end
, highest
)
7418 Lisp_Object start
, end
, highest
;
7421 int from_byte
, to_byte
;
7423 CHECK_NUMBER_COERCE_MARKER (start
);
7424 CHECK_NUMBER_COERCE_MARKER (end
);
7426 validate_region (&start
, &end
);
7427 from
= XINT (start
), to
= XINT (end
);
7428 from_byte
= CHAR_TO_BYTE (from
);
7429 to_byte
= CHAR_TO_BYTE (to
);
7431 if (from
< GPT
&& to
>= GPT
)
7432 move_gap_both (to
, to_byte
);
7434 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7435 to
- from
, to_byte
- from_byte
,
7437 !NILP (current_buffer
7438 ->enable_multibyte_characters
),
7442 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7444 doc
: /* Detect coding system of the text in STRING.
7445 Return a list of possible coding systems ordered by priority.
7447 If only ASCII characters are found, it returns a list of single element
7448 `undecided' or its subsidiary coding system according to a detected
7451 If optional argument HIGHEST is non-nil, return the coding system of
7452 highest priority. */)
7454 Lisp_Object string
, highest
;
7456 CHECK_STRING (string
);
7458 return detect_coding_system (SDATA (string
),
7459 SCHARS (string
), SBYTES (string
),
7460 !NILP (highest
), STRING_MULTIBYTE (string
),
7466 char_encodable_p (c
, attrs
)
7471 struct charset
*charset
;
7472 Lisp_Object translation_table
;
7474 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7475 if (! NILP (translation_table
))
7476 c
= translate_char (translation_table
, c
);
7477 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7478 CONSP (tail
); tail
= XCDR (tail
))
7480 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7481 if (CHAR_CHARSET_P (c
, charset
))
7484 return (! NILP (tail
));
7488 /* Return a list of coding systems that safely encode the text between
7489 START and END. If EXCLUDE is non-nil, it is a list of coding
7490 systems not to check. The returned list doesn't contain any such
7491 coding systems. In any case, if the text contains only ASCII or is
7492 unibyte, return t. */
7494 DEFUN ("find-coding-systems-region-internal",
7495 Ffind_coding_systems_region_internal
,
7496 Sfind_coding_systems_region_internal
, 2, 3, 0,
7497 doc
: /* Internal use only. */)
7498 (start
, end
, exclude
)
7499 Lisp_Object start
, end
, exclude
;
7501 Lisp_Object coding_attrs_list
, safe_codings
;
7502 EMACS_INT start_byte
, end_byte
;
7503 const unsigned char *p
, *pbeg
, *pend
;
7505 Lisp_Object tail
, elt
;
7507 if (STRINGP (start
))
7509 if (!STRING_MULTIBYTE (start
)
7510 || SCHARS (start
) == SBYTES (start
))
7513 end_byte
= SBYTES (start
);
7517 CHECK_NUMBER_COERCE_MARKER (start
);
7518 CHECK_NUMBER_COERCE_MARKER (end
);
7519 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7520 args_out_of_range (start
, end
);
7521 if (NILP (current_buffer
->enable_multibyte_characters
))
7523 start_byte
= CHAR_TO_BYTE (XINT (start
));
7524 end_byte
= CHAR_TO_BYTE (XINT (end
));
7525 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7528 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7530 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7531 move_gap_both (XINT (start
), start_byte
);
7533 move_gap_both (XINT (end
), end_byte
);
7537 coding_attrs_list
= Qnil
;
7538 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7540 || NILP (Fmemq (XCAR (tail
), exclude
)))
7544 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7545 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7546 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7548 ASET (attrs
, coding_attr_trans_tbl
,
7549 get_translation_table (attrs
, 1, NULL
));
7550 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7554 if (STRINGP (start
))
7555 p
= pbeg
= SDATA (start
);
7557 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7558 pend
= p
+ (end_byte
- start_byte
);
7560 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7561 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7565 if (ASCII_BYTE_P (*p
))
7569 c
= STRING_CHAR_ADVANCE (p
);
7571 charset_map_loaded
= 0;
7572 for (tail
= coding_attrs_list
; CONSP (tail
);)
7577 else if (char_encodable_p (c
, elt
))
7579 else if (CONSP (XCDR (tail
)))
7581 XSETCAR (tail
, XCAR (XCDR (tail
)));
7582 XSETCDR (tail
, XCDR (XCDR (tail
)));
7586 XSETCAR (tail
, Qnil
);
7590 if (charset_map_loaded
)
7592 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7594 if (STRINGP (start
))
7595 pbeg
= SDATA (start
);
7597 pbeg
= BYTE_POS_ADDR (start_byte
);
7598 p
= pbeg
+ p_offset
;
7599 pend
= pbeg
+ pend_offset
;
7604 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
7605 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
7606 if (! NILP (XCAR (tail
)))
7607 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
7609 return safe_codings
;
7613 DEFUN ("unencodable-char-position", Funencodable_char_position
,
7614 Sunencodable_char_position
, 3, 5, 0,
7616 Return position of first un-encodable character in a region.
7617 START and END specfiy the region and CODING-SYSTEM specifies the
7618 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7620 If optional 4th argument COUNT is non-nil, it specifies at most how
7621 many un-encodable characters to search. In this case, the value is a
7624 If optional 5th argument STRING is non-nil, it is a string to search
7625 for un-encodable characters. In that case, START and END are indexes
7627 (start
, end
, coding_system
, count
, string
)
7628 Lisp_Object start
, end
, coding_system
, count
, string
;
7631 struct coding_system coding
;
7632 Lisp_Object attrs
, charset_list
, translation_table
;
7633 Lisp_Object positions
;
7635 const unsigned char *p
, *stop
, *pend
;
7636 int ascii_compatible
;
7638 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
7639 attrs
= CODING_ID_ATTRS (coding
.id
);
7640 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
7642 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
7643 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7644 translation_table
= get_translation_table (attrs
, 1, NULL
);
7648 validate_region (&start
, &end
);
7649 from
= XINT (start
);
7651 if (NILP (current_buffer
->enable_multibyte_characters
)
7652 || (ascii_compatible
7653 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
7655 p
= CHAR_POS_ADDR (from
);
7656 pend
= CHAR_POS_ADDR (to
);
7657 if (from
< GPT
&& to
>= GPT
)
7664 CHECK_STRING (string
);
7665 CHECK_NATNUM (start
);
7667 from
= XINT (start
);
7670 || to
> SCHARS (string
))
7671 args_out_of_range_3 (string
, start
, end
);
7672 if (! STRING_MULTIBYTE (string
))
7674 p
= SDATA (string
) + string_char_to_byte (string
, from
);
7675 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
7676 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
7684 CHECK_NATNUM (count
);
7693 if (ascii_compatible
)
7694 while (p
< stop
&& ASCII_BYTE_P (*p
))
7704 c
= STRING_CHAR_ADVANCE (p
);
7705 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
7706 && ! char_charset (translate_char (translation_table
, c
),
7707 charset_list
, NULL
))
7709 positions
= Fcons (make_number (from
), positions
);
7718 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
7722 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
7723 Scheck_coding_systems_region
, 3, 3, 0,
7724 doc
: /* Check if the region is encodable by coding systems.
7726 START and END are buffer positions specifying the region.
7727 CODING-SYSTEM-LIST is a list of coding systems to check.
7729 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7730 CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7731 whole region, POS0, POS1, ... are buffer positions where non-encodable
7732 characters are found.
7734 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7737 START may be a string. In that case, check if the string is
7738 encodable, and the value contains indices to the string instead of
7739 buffer positions. END is ignored. */)
7740 (start
, end
, coding_system_list
)
7741 Lisp_Object start
, end
, coding_system_list
;
7744 EMACS_INT start_byte
, end_byte
;
7746 const unsigned char *p
, *pbeg
, *pend
;
7748 Lisp_Object tail
, elt
, attrs
;
7750 if (STRINGP (start
))
7752 if (!STRING_MULTIBYTE (start
)
7753 && SCHARS (start
) != SBYTES (start
))
7756 end_byte
= SBYTES (start
);
7761 CHECK_NUMBER_COERCE_MARKER (start
);
7762 CHECK_NUMBER_COERCE_MARKER (end
);
7763 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7764 args_out_of_range (start
, end
);
7765 if (NILP (current_buffer
->enable_multibyte_characters
))
7767 start_byte
= CHAR_TO_BYTE (XINT (start
));
7768 end_byte
= CHAR_TO_BYTE (XINT (end
));
7769 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7772 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7774 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7775 move_gap_both (XINT (start
), start_byte
);
7777 move_gap_both (XINT (end
), end_byte
);
7783 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7786 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
7787 ASET (attrs
, coding_attr_trans_tbl
,
7788 get_translation_table (attrs
, 1, NULL
));
7789 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
7792 if (STRINGP (start
))
7793 p
= pbeg
= SDATA (start
);
7795 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7796 pend
= p
+ (end_byte
- start_byte
);
7798 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
7799 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7803 if (ASCII_BYTE_P (*p
))
7807 c
= STRING_CHAR_ADVANCE (p
);
7809 charset_map_loaded
= 0;
7810 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
7812 elt
= XCDR (XCAR (tail
));
7813 if (! char_encodable_p (c
, XCAR (elt
)))
7814 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
7816 if (charset_map_loaded
)
7818 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7820 if (STRINGP (start
))
7821 pbeg
= SDATA (start
);
7823 pbeg
= BYTE_POS_ADDR (start_byte
);
7824 p
= pbeg
+ p_offset
;
7825 pend
= pbeg
+ pend_offset
;
7833 for (; CONSP (tail
); tail
= XCDR (tail
))
7836 if (CONSP (XCDR (XCDR (elt
))))
7837 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
7846 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
7847 Lisp_Object start
, end
, coding_system
, dst_object
;
7848 int encodep
, norecord
;
7850 struct coding_system coding
;
7851 EMACS_INT from
, from_byte
, to
, to_byte
;
7852 Lisp_Object src_object
;
7854 CHECK_NUMBER_COERCE_MARKER (start
);
7855 CHECK_NUMBER_COERCE_MARKER (end
);
7856 if (NILP (coding_system
))
7857 coding_system
= Qno_conversion
;
7859 CHECK_CODING_SYSTEM (coding_system
);
7860 src_object
= Fcurrent_buffer ();
7861 if (NILP (dst_object
))
7862 dst_object
= src_object
;
7863 else if (! EQ (dst_object
, Qt
))
7864 CHECK_BUFFER (dst_object
);
7866 validate_region (&start
, &end
);
7867 from
= XFASTINT (start
);
7868 from_byte
= CHAR_TO_BYTE (from
);
7869 to
= XFASTINT (end
);
7870 to_byte
= CHAR_TO_BYTE (to
);
7872 setup_coding_system (coding_system
, &coding
);
7873 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7876 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7879 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7882 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7884 return (BUFFERP (dst_object
)
7885 ? make_number (coding
.produced_char
)
7886 : coding
.dst_object
);
7890 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
7891 3, 4, "r\nzCoding system: ",
7892 doc
: /* Decode the current region from the specified coding system.
7893 When called from a program, takes four arguments:
7894 START, END, CODING-SYSTEM, and DESTINATION.
7895 START and END are buffer positions.
7897 Optional 4th arguments DESTINATION specifies where the decoded text goes.
7898 If nil, the region between START and END is replace by the decoded text.
7899 If buffer, the decoded text is inserted in the buffer.
7900 If t, the decoded text is returned.
7902 This function sets `last-coding-system-used' to the precise coding system
7903 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7904 not fully specified.)
7905 It returns the length of the decoded text. */)
7906 (start
, end
, coding_system
, destination
)
7907 Lisp_Object start
, end
, coding_system
, destination
;
7909 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
7912 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
7913 3, 4, "r\nzCoding system: ",
7914 doc
: /* Encode the current region by specified coding system.
7915 When called from a program, takes three arguments:
7916 START, END, and CODING-SYSTEM. START and END are buffer positions.
7918 Optional 4th arguments DESTINATION specifies where the encoded text goes.
7919 If nil, the region between START and END is replace by the encoded text.
7920 If buffer, the encoded text is inserted in the buffer.
7921 If t, the encoded text is returned.
7923 This function sets `last-coding-system-used' to the precise coding system
7924 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7925 not fully specified.)
7926 It returns the length of the encoded text. */)
7927 (start
, end
, coding_system
, destination
)
7928 Lisp_Object start
, end
, coding_system
, destination
;
7930 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
7934 code_convert_string (string
, coding_system
, dst_object
,
7935 encodep
, nocopy
, norecord
)
7936 Lisp_Object string
, coding_system
, dst_object
;
7937 int encodep
, nocopy
, norecord
;
7939 struct coding_system coding
;
7940 EMACS_INT chars
, bytes
;
7942 CHECK_STRING (string
);
7943 if (NILP (coding_system
))
7946 Vlast_coding_system_used
= Qno_conversion
;
7947 if (NILP (dst_object
))
7948 return (nocopy
? Fcopy_sequence (string
) : string
);
7951 if (NILP (coding_system
))
7952 coding_system
= Qno_conversion
;
7954 CHECK_CODING_SYSTEM (coding_system
);
7955 if (NILP (dst_object
))
7957 else if (! EQ (dst_object
, Qt
))
7958 CHECK_BUFFER (dst_object
);
7960 setup_coding_system (coding_system
, &coding
);
7961 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7962 chars
= SCHARS (string
);
7963 bytes
= SBYTES (string
);
7965 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
7967 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
7969 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7971 return (BUFFERP (dst_object
)
7972 ? make_number (coding
.produced_char
)
7973 : coding
.dst_object
);
7977 /* Encode or decode STRING according to CODING_SYSTEM.
7978 Do not set Vlast_coding_system_used.
7980 This function is called only from macros DECODE_FILE and
7981 ENCODE_FILE, thus we ignore character composition. */
7984 code_convert_string_norecord (string
, coding_system
, encodep
)
7985 Lisp_Object string
, coding_system
;
7988 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
7992 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
7994 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
7996 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
7997 if the decoding operation is trivial.
7999 Optional fourth arg BUFFER non-nil meant that the decoded text is
8000 inserted in BUFFER instead of returned as a string. In this case,
8001 the return value is BUFFER.
8003 This function sets `last-coding-system-used' to the precise coding system
8004 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8005 not fully specified. */)
8006 (string
, coding_system
, nocopy
, buffer
)
8007 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8009 return code_convert_string (string
, coding_system
, buffer
,
8010 0, ! NILP (nocopy
), 0);
8013 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8015 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8017 Optional third arg NOCOPY non-nil means it is OK to return STRING
8018 itself if the encoding operation is trivial.
8020 Optional fourth arg BUFFER non-nil meant that the encoded text is
8021 inserted in BUFFER instead of returned as a string. In this case,
8022 the return value is BUFFER.
8024 This function sets `last-coding-system-used' to the precise coding system
8025 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8026 not fully specified.) */)
8027 (string
, coding_system
, nocopy
, buffer
)
8028 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8030 return code_convert_string (string
, coding_system
, buffer
,
8031 1, ! NILP (nocopy
), 1);
8035 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8036 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8037 Return the corresponding character. */)
8041 Lisp_Object spec
, attrs
, val
;
8042 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8045 CHECK_NATNUM (code
);
8046 c
= XFASTINT (code
);
8047 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8048 attrs
= AREF (spec
, 0);
8050 if (ASCII_BYTE_P (c
)
8051 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8054 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8055 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8056 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8057 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8060 charset
= charset_roman
;
8061 else if (c
>= 0xA0 && c
< 0xDF)
8063 charset
= charset_kana
;
8068 int s1
= c
>> 8, s2
= c
& 0xFF;
8070 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8071 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8072 error ("Invalid code: %d", code
);
8074 charset
= charset_kanji
;
8076 c
= DECODE_CHAR (charset
, c
);
8078 error ("Invalid code: %d", code
);
8079 return make_number (c
);
8083 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8084 doc
: /* Encode a Japanese character CHAR to shift_jis encoding.
8085 Return the corresponding code in SJIS. */)
8089 Lisp_Object spec
, attrs
, charset_list
;
8091 struct charset
*charset
;
8094 CHECK_CHARACTER (ch
);
8096 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8097 attrs
= AREF (spec
, 0);
8099 if (ASCII_CHAR_P (c
)
8100 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8103 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8104 charset
= char_charset (c
, charset_list
, &code
);
8105 if (code
== CHARSET_INVALID_CODE (charset
))
8106 error ("Can't encode by shift_jis encoding: %d", c
);
8109 return make_number (code
);
8112 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8113 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8114 Return the corresponding character. */)
8118 Lisp_Object spec
, attrs
, val
;
8119 struct charset
*charset_roman
, *charset_big5
, *charset
;
8122 CHECK_NATNUM (code
);
8123 c
= XFASTINT (code
);
8124 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8125 attrs
= AREF (spec
, 0);
8127 if (ASCII_BYTE_P (c
)
8128 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8131 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8132 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8133 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8136 charset
= charset_roman
;
8139 int b1
= c
>> 8, b2
= c
& 0x7F;
8140 if (b1
< 0xA1 || b1
> 0xFE
8141 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8142 error ("Invalid code: %d", code
);
8143 charset
= charset_big5
;
8145 c
= DECODE_CHAR (charset
, (unsigned )c
);
8147 error ("Invalid code: %d", code
);
8148 return make_number (c
);
8151 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8152 doc
: /* Encode the Big5 character CHAR to BIG5 coding system.
8153 Return the corresponding character code in Big5. */)
8157 Lisp_Object spec
, attrs
, charset_list
;
8158 struct charset
*charset
;
8162 CHECK_CHARACTER (ch
);
8164 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8165 attrs
= AREF (spec
, 0);
8166 if (ASCII_CHAR_P (c
)
8167 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8170 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8171 charset
= char_charset (c
, charset_list
, &code
);
8172 if (code
== CHARSET_INVALID_CODE (charset
))
8173 error ("Can't encode by Big5 encoding: %d", c
);
8175 return make_number (code
);
8179 DEFUN ("set-terminal-coding-system-internal",
8180 Fset_terminal_coding_system_internal
,
8181 Sset_terminal_coding_system_internal
, 1, 1, 0,
8182 doc
: /* Internal use only. */)
8184 Lisp_Object coding_system
;
8186 CHECK_SYMBOL (coding_system
);
8187 setup_coding_system (Fcheck_coding_system (coding_system
),
8190 /* We had better not send unsafe characters to terminal. */
8191 terminal_coding
.mode
|= CODING_MODE_SAFE_ENCODING
;
8192 /* Characer composition should be disabled. */
8193 terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8194 terminal_coding
.src_multibyte
= 1;
8195 terminal_coding
.dst_multibyte
= 0;
8199 DEFUN ("set-safe-terminal-coding-system-internal",
8200 Fset_safe_terminal_coding_system_internal
,
8201 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8202 doc
: /* Internal use only. */)
8204 Lisp_Object coding_system
;
8206 CHECK_SYMBOL (coding_system
);
8207 setup_coding_system (Fcheck_coding_system (coding_system
),
8208 &safe_terminal_coding
);
8209 /* Characer composition should be disabled. */
8210 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8211 safe_terminal_coding
.src_multibyte
= 1;
8212 safe_terminal_coding
.dst_multibyte
= 0;
8216 DEFUN ("terminal-coding-system",
8217 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
8218 doc
: /* Return coding system specified for terminal output. */)
8221 Lisp_Object coding_system
;
8223 coding_system
= CODING_ID_NAME (terminal_coding
.id
);
8224 /* For backward compatibility, return nil if it is `undecided'. */
8225 return (coding_system
!= Qundecided
? coding_system
: Qnil
);
8228 DEFUN ("set-keyboard-coding-system-internal",
8229 Fset_keyboard_coding_system_internal
,
8230 Sset_keyboard_coding_system_internal
, 1, 1, 0,
8231 doc
: /* Internal use only. */)
8233 Lisp_Object coding_system
;
8235 CHECK_SYMBOL (coding_system
);
8236 setup_coding_system (Fcheck_coding_system (coding_system
),
8238 /* Characer composition should be disabled. */
8239 keyboard_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8243 DEFUN ("keyboard-coding-system",
8244 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
8245 doc
: /* Return coding system specified for decoding keyboard input. */)
8248 return CODING_ID_NAME (keyboard_coding
.id
);
8252 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8253 Sfind_operation_coding_system
, 1, MANY
, 0,
8254 doc
: /* Choose a coding system for an operation based on the target name.
8255 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8256 DECODING-SYSTEM is the coding system to use for decoding
8257 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8258 for encoding (in case OPERATION does encoding).
8260 The first argument OPERATION specifies an I/O primitive:
8261 For file I/O, `insert-file-contents' or `write-region'.
8262 For process I/O, `call-process', `call-process-region', or `start-process'.
8263 For network I/O, `open-network-stream'.
8265 The remaining arguments should be the same arguments that were passed
8266 to the primitive. Depending on which primitive, one of those arguments
8267 is selected as the TARGET. For example, if OPERATION does file I/O,
8268 whichever argument specifies the file name is TARGET.
8270 TARGET has a meaning which depends on OPERATION:
8271 For file I/O, TARGET is a file name.
8272 For process I/O, TARGET is a process name.
8273 For network I/O, TARGET is a service name or a port number
8275 This function looks up what specified for TARGET in,
8276 `file-coding-system-alist', `process-coding-system-alist',
8277 or `network-coding-system-alist' depending on OPERATION.
8278 They may specify a coding system, a cons of coding systems,
8279 or a function symbol to call.
8280 In the last case, we call the function with one argument,
8281 which is a list of all the arguments given to this function.
8283 usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
8288 Lisp_Object operation
, target_idx
, target
, val
;
8289 register Lisp_Object chain
;
8292 error ("Too few arguments");
8293 operation
= args
[0];
8294 if (!SYMBOLP (operation
)
8295 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8296 error ("Invalid first arguement");
8297 if (nargs
< 1 + XINT (target_idx
))
8298 error ("Too few arguments for operation: %s",
8299 SDATA (SYMBOL_NAME (operation
)));
8300 target
= args
[XINT (target_idx
) + 1];
8301 if (!(STRINGP (target
)
8302 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8303 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8305 chain
= ((EQ (operation
, Qinsert_file_contents
)
8306 || EQ (operation
, Qwrite_region
))
8307 ? Vfile_coding_system_alist
8308 : (EQ (operation
, Qopen_network_stream
)
8309 ? Vnetwork_coding_system_alist
8310 : Vprocess_coding_system_alist
));
8314 for (; CONSP (chain
); chain
= XCDR (chain
))
8320 && ((STRINGP (target
)
8321 && STRINGP (XCAR (elt
))
8322 && fast_string_match (XCAR (elt
), target
) >= 0)
8323 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8326 /* Here, if VAL is both a valid coding system and a valid
8327 function symbol, we return VAL as a coding system. */
8330 if (! SYMBOLP (val
))
8332 if (! NILP (Fcoding_system_p (val
)))
8333 return Fcons (val
, val
);
8334 if (! NILP (Ffboundp (val
)))
8336 val
= call1 (val
, Flist (nargs
, args
));
8339 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8340 return Fcons (val
, val
);
8348 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8349 Sset_coding_system_priority
, 0, MANY
, 0,
8350 doc
: /* Assign higher priority to the coding systems given as arguments.
8351 If multiple coding systems belongs to the same category,
8352 all but the first one are ignored.
8354 usage: (set-coding-system-priority ...) */)
8360 int changed
[coding_category_max
];
8361 enum coding_category priorities
[coding_category_max
];
8363 bzero (changed
, sizeof changed
);
8365 for (i
= j
= 0; i
< nargs
; i
++)
8367 enum coding_category category
;
8368 Lisp_Object spec
, attrs
;
8370 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8371 attrs
= AREF (spec
, 0);
8372 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8373 if (changed
[category
])
8374 /* Ignore this coding system because a coding system of the
8375 same category already had a higher priority. */
8377 changed
[category
] = 1;
8378 priorities
[j
++] = category
;
8379 if (coding_categories
[category
].id
>= 0
8380 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8381 setup_coding_system (args
[i
], &coding_categories
[category
]);
8382 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8385 /* Now we have decided top J priorities. Reflect the order of the
8386 original priorities to the remaining priorities. */
8388 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8390 while (j
< coding_category_max
8391 && changed
[coding_priorities
[j
]])
8393 if (j
== coding_category_max
)
8395 priorities
[i
] = coding_priorities
[j
];
8398 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8400 /* Update `coding-category-list'. */
8401 Vcoding_category_list
= Qnil
;
8402 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8403 Vcoding_category_list
8404 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8405 Vcoding_category_list
);
8410 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8411 Scoding_system_priority_list
, 0, 1, 0,
8412 doc
: /* Return a list of coding systems ordered by their priorities.
8413 HIGHESTP non-nil means just return the highest priority one. */)
8415 Lisp_Object highestp
;
8420 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8422 enum coding_category category
= coding_priorities
[i
];
8423 int id
= coding_categories
[category
].id
;
8428 attrs
= CODING_ID_ATTRS (id
);
8429 if (! NILP (highestp
))
8430 return CODING_ATTR_BASE_NAME (attrs
);
8431 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8433 return Fnreverse (val
);
8436 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8439 make_subsidiaries (base
)
8442 Lisp_Object subsidiaries
;
8443 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8444 char *buf
= (char *) alloca (base_name_len
+ 6);
8447 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8448 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8449 for (i
= 0; i
< 3; i
++)
8451 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8452 ASET (subsidiaries
, i
, intern (buf
));
8454 return subsidiaries
;
8458 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8459 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8460 doc
: /* For internal use only.
8461 usage: (define-coding-system-internal ...) */)
8467 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8468 Lisp_Object attrs
; /* Vector of attributes. */
8469 Lisp_Object eol_type
;
8470 Lisp_Object aliases
;
8471 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8472 enum coding_category category
;
8473 Lisp_Object tail
, val
;
8474 int max_charset_id
= 0;
8477 if (nargs
< coding_arg_max
)
8480 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8482 name
= args
[coding_arg_name
];
8483 CHECK_SYMBOL (name
);
8484 CODING_ATTR_BASE_NAME (attrs
) = name
;
8486 val
= args
[coding_arg_mnemonic
];
8487 if (! STRINGP (val
))
8488 CHECK_CHARACTER (val
);
8489 CODING_ATTR_MNEMONIC (attrs
) = val
;
8491 coding_type
= args
[coding_arg_coding_type
];
8492 CHECK_SYMBOL (coding_type
);
8493 CODING_ATTR_TYPE (attrs
) = coding_type
;
8495 charset_list
= args
[coding_arg_charset_list
];
8496 if (SYMBOLP (charset_list
))
8498 if (EQ (charset_list
, Qiso_2022
))
8500 if (! EQ (coding_type
, Qiso_2022
))
8501 error ("Invalid charset-list");
8502 charset_list
= Viso_2022_charset_list
;
8504 else if (EQ (charset_list
, Qemacs_mule
))
8506 if (! EQ (coding_type
, Qemacs_mule
))
8507 error ("Invalid charset-list");
8508 charset_list
= Vemacs_mule_charset_list
;
8510 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8511 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8512 max_charset_id
= XFASTINT (XCAR (tail
));
8516 charset_list
= Fcopy_sequence (charset_list
);
8517 for (tail
= charset_list
; !NILP (tail
); tail
= Fcdr (tail
))
8519 struct charset
*charset
;
8522 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8523 if (EQ (coding_type
, Qiso_2022
)
8524 ? CHARSET_ISO_FINAL (charset
) < 0
8525 : EQ (coding_type
, Qemacs_mule
)
8526 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8528 error ("Can't handle charset `%s'",
8529 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8531 XSETCAR (tail
, make_number (charset
->id
));
8532 if (max_charset_id
< charset
->id
)
8533 max_charset_id
= charset
->id
;
8536 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8538 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8540 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8541 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8542 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8544 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8546 val
= args
[coding_arg_decode_translation_table
];
8547 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8549 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8551 val
= args
[coding_arg_encode_translation_table
];
8552 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8554 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8556 val
= args
[coding_arg_post_read_conversion
];
8558 CODING_ATTR_POST_READ (attrs
) = val
;
8560 val
= args
[coding_arg_pre_write_conversion
];
8562 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8564 val
= args
[coding_arg_default_char
];
8566 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
8569 CHECK_CHARACTER (val
);
8570 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8573 val
= args
[coding_arg_for_unibyte
];
8574 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
8576 val
= args
[coding_arg_plist
];
8578 CODING_ATTR_PLIST (attrs
) = val
;
8580 if (EQ (coding_type
, Qcharset
))
8582 /* Generate a lisp vector of 256 elements. Each element is nil,
8583 integer, or a list of charset IDs.
8585 If Nth element is nil, the byte code N is invalid in this
8588 If Nth element is a number NUM, N is the first byte of a
8589 charset whose ID is NUM.
8591 If Nth element is a list of charset IDs, N is the first byte
8592 of one of them. The list is sorted by dimensions of the
8593 charsets. A charset of smaller dimension comes firtst. */
8594 val
= Fmake_vector (make_number (256), Qnil
);
8596 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8598 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
8599 int dim
= CHARSET_DIMENSION (charset
);
8600 int idx
= (dim
- 1) * 4;
8602 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8603 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8605 for (i
= charset
->code_space
[idx
];
8606 i
<= charset
->code_space
[idx
+ 1]; i
++)
8608 Lisp_Object tmp
, tmp2
;
8611 tmp
= AREF (val
, i
);
8614 else if (NUMBERP (tmp
))
8616 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
8618 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
8620 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
8624 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
8626 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
8631 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
8634 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
8635 XSETCAR (tmp2
, XCAR (tail
));
8641 ASET (attrs
, coding_attr_charset_valids
, val
);
8642 category
= coding_category_charset
;
8644 else if (EQ (coding_type
, Qccl
))
8648 if (nargs
< coding_arg_ccl_max
)
8651 val
= args
[coding_arg_ccl_decoder
];
8652 CHECK_CCL_PROGRAM (val
);
8654 val
= Fcopy_sequence (val
);
8655 ASET (attrs
, coding_attr_ccl_decoder
, val
);
8657 val
= args
[coding_arg_ccl_encoder
];
8658 CHECK_CCL_PROGRAM (val
);
8660 val
= Fcopy_sequence (val
);
8661 ASET (attrs
, coding_attr_ccl_encoder
, val
);
8663 val
= args
[coding_arg_ccl_valids
];
8664 valids
= Fmake_string (make_number (256), make_number (0));
8665 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
8672 from
= to
= XINT (val
);
8673 if (from
< 0 || from
> 255)
8674 args_out_of_range_3 (val
, make_number (0), make_number (255));
8679 CHECK_NATNUM_CAR (val
);
8680 CHECK_NATNUM_CDR (val
);
8681 from
= XINT (XCAR (val
));
8683 args_out_of_range_3 (XCAR (val
),
8684 make_number (0), make_number (255));
8685 to
= XINT (XCDR (val
));
8686 if (to
< from
|| to
> 255)
8687 args_out_of_range_3 (XCDR (val
),
8688 XCAR (val
), make_number (255));
8690 for (i
= from
; i
<= to
; i
++)
8691 SSET (valids
, i
, 1);
8693 ASET (attrs
, coding_attr_ccl_valids
, valids
);
8695 category
= coding_category_ccl
;
8697 else if (EQ (coding_type
, Qutf_16
))
8699 Lisp_Object bom
, endian
;
8701 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8703 if (nargs
< coding_arg_utf16_max
)
8706 bom
= args
[coding_arg_utf16_bom
];
8707 if (! NILP (bom
) && ! EQ (bom
, Qt
))
8711 CHECK_CODING_SYSTEM (val
);
8713 CHECK_CODING_SYSTEM (val
);
8715 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
8717 endian
= args
[coding_arg_utf16_endian
];
8718 CHECK_SYMBOL (endian
);
8721 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
8722 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
8723 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
8725 category
= (CONSP (bom
)
8726 ? coding_category_utf_16_auto
8728 ? (EQ (endian
, Qbig
)
8729 ? coding_category_utf_16_be_nosig
8730 : coding_category_utf_16_le_nosig
)
8731 : (EQ (endian
, Qbig
)
8732 ? coding_category_utf_16_be
8733 : coding_category_utf_16_le
));
8735 else if (EQ (coding_type
, Qiso_2022
))
8737 Lisp_Object initial
, reg_usage
, request
, flags
;
8740 if (nargs
< coding_arg_iso2022_max
)
8743 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
8744 CHECK_VECTOR (initial
);
8745 for (i
= 0; i
< 4; i
++)
8747 val
= Faref (initial
, make_number (i
));
8750 struct charset
*charset
;
8752 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8753 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
8754 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
8755 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8758 ASET (initial
, i
, make_number (-1));
8761 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
8762 CHECK_CONS (reg_usage
);
8763 CHECK_NUMBER_CAR (reg_usage
);
8764 CHECK_NUMBER_CDR (reg_usage
);
8766 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
8767 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
8775 CHECK_CHARSET_GET_ID (tmp
, id
);
8776 CHECK_NATNUM_CDR (val
);
8777 if (XINT (XCDR (val
)) >= 4)
8778 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
8779 XSETCAR (val
, make_number (id
));
8782 flags
= args
[coding_arg_iso2022_flags
];
8783 CHECK_NATNUM (flags
);
8785 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
8786 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
8788 ASET (attrs
, coding_attr_iso_initial
, initial
);
8789 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
8790 ASET (attrs
, coding_attr_iso_request
, request
);
8791 ASET (attrs
, coding_attr_iso_flags
, flags
);
8792 setup_iso_safe_charsets (attrs
);
8794 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
8795 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
8796 | CODING_ISO_FLAG_SINGLE_SHIFT
))
8797 ? coding_category_iso_7_else
8798 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8799 ? coding_category_iso_7
8800 : coding_category_iso_7_tight
);
8803 int id
= XINT (AREF (initial
, 1));
8805 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
8806 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8808 ? coding_category_iso_8_else
8809 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
8810 ? coding_category_iso_8_1
8811 : coding_category_iso_8_2
);
8813 if (category
!= coding_category_iso_8_1
8814 && category
!= coding_category_iso_8_2
)
8815 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8817 else if (EQ (coding_type
, Qemacs_mule
))
8819 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
8820 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
8821 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8822 category
= coding_category_emacs_mule
;
8824 else if (EQ (coding_type
, Qshift_jis
))
8827 struct charset
*charset
;
8829 if (XINT (Flength (charset_list
)) != 3
8830 && XINT (Flength (charset_list
)) != 4)
8831 error ("There should be three or four charsets");
8833 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8834 if (CHARSET_DIMENSION (charset
) != 1)
8835 error ("Dimension of charset %s is not one",
8836 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8837 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8838 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8840 charset_list
= XCDR (charset_list
);
8841 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8842 if (CHARSET_DIMENSION (charset
) != 1)
8843 error ("Dimension of charset %s is not one",
8844 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8846 charset_list
= XCDR (charset_list
);
8847 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8848 if (CHARSET_DIMENSION (charset
) != 2)
8849 error ("Dimension of charset %s is not two",
8850 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8852 charset_list
= XCDR (charset_list
);
8853 if (! NILP (charset_list
))
8855 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8856 if (CHARSET_DIMENSION (charset
) != 2)
8857 error ("Dimension of charset %s is not two",
8858 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8861 category
= coding_category_sjis
;
8862 Vsjis_coding_system
= name
;
8864 else if (EQ (coding_type
, Qbig5
))
8866 struct charset
*charset
;
8868 if (XINT (Flength (charset_list
)) != 2)
8869 error ("There should be just two charsets");
8871 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8872 if (CHARSET_DIMENSION (charset
) != 1)
8873 error ("Dimension of charset %s is not one",
8874 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8875 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8876 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8878 charset_list
= XCDR (charset_list
);
8879 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8880 if (CHARSET_DIMENSION (charset
) != 2)
8881 error ("Dimension of charset %s is not two",
8882 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8884 category
= coding_category_big5
;
8885 Vbig5_coding_system
= name
;
8887 else if (EQ (coding_type
, Qraw_text
))
8889 category
= coding_category_raw_text
;
8890 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8892 else if (EQ (coding_type
, Qutf_8
))
8894 category
= coding_category_utf_8
;
8895 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8897 else if (EQ (coding_type
, Qundecided
))
8898 category
= coding_category_undecided
;
8900 error ("Invalid coding system type: %s",
8901 SDATA (SYMBOL_NAME (coding_type
)));
8903 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
8904 CODING_ATTR_PLIST (attrs
)
8905 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
8906 CODING_ATTR_PLIST (attrs
)));
8907 CODING_ATTR_PLIST (attrs
)
8908 = Fcons (QCascii_compatible_p
,
8909 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
8910 CODING_ATTR_PLIST (attrs
)));
8912 eol_type
= args
[coding_arg_eol_type
];
8913 if (! NILP (eol_type
)
8914 && ! EQ (eol_type
, Qunix
)
8915 && ! EQ (eol_type
, Qdos
)
8916 && ! EQ (eol_type
, Qmac
))
8917 error ("Invalid eol-type");
8919 aliases
= Fcons (name
, Qnil
);
8921 if (NILP (eol_type
))
8923 eol_type
= make_subsidiaries (name
);
8924 for (i
= 0; i
< 3; i
++)
8926 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
8928 this_name
= AREF (eol_type
, i
);
8929 this_aliases
= Fcons (this_name
, Qnil
);
8930 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
8931 this_spec
= Fmake_vector (make_number (3), attrs
);
8932 ASET (this_spec
, 1, this_aliases
);
8933 ASET (this_spec
, 2, this_eol_type
);
8934 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
8935 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
8936 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
8937 Vcoding_system_alist
);
8941 spec_vec
= Fmake_vector (make_number (3), attrs
);
8942 ASET (spec_vec
, 1, aliases
);
8943 ASET (spec_vec
, 2, eol_type
);
8945 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
8946 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
8947 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
8948 Vcoding_system_alist
);
8951 int id
= coding_categories
[category
].id
;
8953 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
8954 setup_coding_system (name
, &coding_categories
[category
]);
8960 return Fsignal (Qwrong_number_of_arguments
,
8961 Fcons (intern ("define-coding-system-internal"),
8962 make_number (nargs
)));
8966 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
8968 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
8969 (coding_system
, prop
, val
)
8970 Lisp_Object coding_system
, prop
, val
;
8972 Lisp_Object spec
, attrs
;
8974 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8975 attrs
= AREF (spec
, 0);
8976 if (EQ (prop
, QCmnemonic
))
8978 if (! STRINGP (val
))
8979 CHECK_CHARACTER (val
);
8980 CODING_ATTR_MNEMONIC (attrs
) = val
;
8982 else if (EQ (prop
, QCdefalut_char
))
8985 val
= make_number (' ');
8987 CHECK_CHARACTER (val
);
8988 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8990 else if (EQ (prop
, QCdecode_translation_table
))
8992 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8994 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8996 else if (EQ (prop
, QCencode_translation_table
))
8998 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9000 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9002 else if (EQ (prop
, QCpost_read_conversion
))
9005 CODING_ATTR_POST_READ (attrs
) = val
;
9007 else if (EQ (prop
, QCpre_write_conversion
))
9010 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9012 else if (EQ (prop
, QCascii_compatible_p
))
9014 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9017 CODING_ATTR_PLIST (attrs
)
9018 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9023 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9024 Sdefine_coding_system_alias
, 2, 2, 0,
9025 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9026 (alias
, coding_system
)
9027 Lisp_Object alias
, coding_system
;
9029 Lisp_Object spec
, aliases
, eol_type
;
9031 CHECK_SYMBOL (alias
);
9032 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9033 aliases
= AREF (spec
, 1);
9034 /* ALISES should be a list of length more than zero, and the first
9035 element is a base coding system. Append ALIAS at the tail of the
9037 while (!NILP (XCDR (aliases
)))
9038 aliases
= XCDR (aliases
);
9039 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9041 eol_type
= AREF (spec
, 2);
9042 if (VECTORP (eol_type
))
9044 Lisp_Object subsidiaries
;
9047 subsidiaries
= make_subsidiaries (alias
);
9048 for (i
= 0; i
< 3; i
++)
9049 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9050 AREF (eol_type
, i
));
9053 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9054 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9055 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9056 Vcoding_system_alist
);
9061 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9063 doc
: /* Return the base of CODING-SYSTEM.
9064 Any alias or subsidiary coding system is not a base coding system. */)
9066 Lisp_Object coding_system
;
9068 Lisp_Object spec
, attrs
;
9070 if (NILP (coding_system
))
9071 return (Qno_conversion
);
9072 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9073 attrs
= AREF (spec
, 0);
9074 return CODING_ATTR_BASE_NAME (attrs
);
9077 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9079 doc
: "Return the property list of CODING-SYSTEM.")
9081 Lisp_Object coding_system
;
9083 Lisp_Object spec
, attrs
;
9085 if (NILP (coding_system
))
9086 coding_system
= Qno_conversion
;
9087 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9088 attrs
= AREF (spec
, 0);
9089 return CODING_ATTR_PLIST (attrs
);
9093 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9095 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9097 Lisp_Object coding_system
;
9101 if (NILP (coding_system
))
9102 coding_system
= Qno_conversion
;
9103 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9104 return AREF (spec
, 1);
9107 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9108 Scoding_system_eol_type
, 1, 1, 0,
9109 doc
: /* Return eol-type of CODING-SYSTEM.
9110 An eol-type is integer 0, 1, 2, or a vector of coding systems.
9112 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9113 and CR respectively.
9115 A vector value indicates that a format of end-of-line should be
9116 detected automatically. Nth element of the vector is the subsidiary
9117 coding system whose eol-type is N. */)
9119 Lisp_Object coding_system
;
9121 Lisp_Object spec
, eol_type
;
9124 if (NILP (coding_system
))
9125 coding_system
= Qno_conversion
;
9126 if (! CODING_SYSTEM_P (coding_system
))
9128 spec
= CODING_SYSTEM_SPEC (coding_system
);
9129 eol_type
= AREF (spec
, 2);
9130 if (VECTORP (eol_type
))
9131 return Fcopy_sequence (eol_type
);
9132 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9133 return make_number (n
);
9139 /*** 9. Post-amble ***/
9146 for (i
= 0; i
< coding_category_max
; i
++)
9148 coding_categories
[i
].id
= -1;
9149 coding_priorities
[i
] = i
;
9152 /* ISO2022 specific initialize routine. */
9153 for (i
= 0; i
< 0x20; i
++)
9154 iso_code_class
[i
] = ISO_control_0
;
9155 for (i
= 0x21; i
< 0x7F; i
++)
9156 iso_code_class
[i
] = ISO_graphic_plane_0
;
9157 for (i
= 0x80; i
< 0xA0; i
++)
9158 iso_code_class
[i
] = ISO_control_1
;
9159 for (i
= 0xA1; i
< 0xFF; i
++)
9160 iso_code_class
[i
] = ISO_graphic_plane_1
;
9161 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9162 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9163 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9164 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9165 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9166 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9167 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9168 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9169 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9171 for (i
= 0; i
< 256; i
++)
9173 emacs_mule_bytes
[i
] = 1;
9175 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9176 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9177 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9178 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9186 staticpro (&Vcoding_system_hash_table
);
9188 Lisp_Object args
[2];
9191 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9194 staticpro (&Vsjis_coding_system
);
9195 Vsjis_coding_system
= Qnil
;
9197 staticpro (&Vbig5_coding_system
);
9198 Vbig5_coding_system
= Qnil
;
9200 staticpro (&Vcode_conversion_reused_workbuf
);
9201 Vcode_conversion_reused_workbuf
= Qnil
;
9203 staticpro (&Vcode_conversion_workbuf_name
);
9204 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9206 reused_workbuf_in_use
= 0;
9208 DEFSYM (Qcharset
, "charset");
9209 DEFSYM (Qtarget_idx
, "target-idx");
9210 DEFSYM (Qcoding_system_history
, "coding-system-history");
9211 Fset (Qcoding_system_history
, Qnil
);
9213 /* Target FILENAME is the first argument. */
9214 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9215 /* Target FILENAME is the third argument. */
9216 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9218 DEFSYM (Qcall_process
, "call-process");
9219 /* Target PROGRAM is the first argument. */
9220 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9222 DEFSYM (Qcall_process_region
, "call-process-region");
9223 /* Target PROGRAM is the third argument. */
9224 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9226 DEFSYM (Qstart_process
, "start-process");
9227 /* Target PROGRAM is the third argument. */
9228 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9230 DEFSYM (Qopen_network_stream
, "open-network-stream");
9231 /* Target SERVICE is the fourth argument. */
9232 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9234 DEFSYM (Qcoding_system
, "coding-system");
9235 DEFSYM (Qcoding_aliases
, "coding-aliases");
9237 DEFSYM (Qeol_type
, "eol-type");
9238 DEFSYM (Qunix
, "unix");
9239 DEFSYM (Qdos
, "dos");
9241 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9242 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9243 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9244 DEFSYM (Qdefault_char
, "default-char");
9245 DEFSYM (Qundecided
, "undecided");
9246 DEFSYM (Qno_conversion
, "no-conversion");
9247 DEFSYM (Qraw_text
, "raw-text");
9249 DEFSYM (Qiso_2022
, "iso-2022");
9251 DEFSYM (Qutf_8
, "utf-8");
9252 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9254 DEFSYM (Qutf_16
, "utf-16");
9255 DEFSYM (Qbig
, "big");
9256 DEFSYM (Qlittle
, "little");
9258 DEFSYM (Qshift_jis
, "shift-jis");
9259 DEFSYM (Qbig5
, "big5");
9261 DEFSYM (Qcoding_system_p
, "coding-system-p");
9263 DEFSYM (Qcoding_system_error
, "coding-system-error");
9264 Fput (Qcoding_system_error
, Qerror_conditions
,
9265 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9266 Fput (Qcoding_system_error
, Qerror_message
,
9267 build_string ("Invalid coding system"));
9269 /* Intern this now in case it isn't already done.
9270 Setting this variable twice is harmless.
9271 But don't staticpro it here--that is done in alloc.c. */
9272 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9274 DEFSYM (Qtranslation_table
, "translation-table");
9275 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9276 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9277 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9278 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9280 DEFSYM (Qvalid_codes
, "valid-codes");
9282 DEFSYM (Qemacs_mule
, "emacs-mule");
9284 DEFSYM (QCcategory
, ":category");
9285 DEFSYM (QCmnemonic
, ":mnemonic");
9286 DEFSYM (QCdefalut_char
, ":default-char");
9287 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9288 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9289 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9290 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9291 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
9293 Vcoding_category_table
9294 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9295 staticpro (&Vcoding_category_table
);
9296 /* Followings are target of code detection. */
9297 ASET (Vcoding_category_table
, coding_category_iso_7
,
9298 intern ("coding-category-iso-7"));
9299 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9300 intern ("coding-category-iso-7-tight"));
9301 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9302 intern ("coding-category-iso-8-1"));
9303 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9304 intern ("coding-category-iso-8-2"));
9305 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9306 intern ("coding-category-iso-7-else"));
9307 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9308 intern ("coding-category-iso-8-else"));
9309 ASET (Vcoding_category_table
, coding_category_utf_8
,
9310 intern ("coding-category-utf-8"));
9311 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9312 intern ("coding-category-utf-16-be"));
9313 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9314 intern ("coding-category-utf-16-auto"));
9315 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9316 intern ("coding-category-utf-16-le"));
9317 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9318 intern ("coding-category-utf-16-be-nosig"));
9319 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9320 intern ("coding-category-utf-16-le-nosig"));
9321 ASET (Vcoding_category_table
, coding_category_charset
,
9322 intern ("coding-category-charset"));
9323 ASET (Vcoding_category_table
, coding_category_sjis
,
9324 intern ("coding-category-sjis"));
9325 ASET (Vcoding_category_table
, coding_category_big5
,
9326 intern ("coding-category-big5"));
9327 ASET (Vcoding_category_table
, coding_category_ccl
,
9328 intern ("coding-category-ccl"));
9329 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9330 intern ("coding-category-emacs-mule"));
9331 /* Followings are NOT target of code detection. */
9332 ASET (Vcoding_category_table
, coding_category_raw_text
,
9333 intern ("coding-category-raw-text"));
9334 ASET (Vcoding_category_table
, coding_category_undecided
,
9335 intern ("coding-category-undecided"));
9337 DEFSYM (Qinsufficient_source
, "insufficient-source");
9338 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9339 DEFSYM (Qinvalid_source
, "invalid-source");
9340 DEFSYM (Qinterrupted
, "interrupted");
9341 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9342 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
9344 defsubr (&Scoding_system_p
);
9345 defsubr (&Sread_coding_system
);
9346 defsubr (&Sread_non_nil_coding_system
);
9347 defsubr (&Scheck_coding_system
);
9348 defsubr (&Sdetect_coding_region
);
9349 defsubr (&Sdetect_coding_string
);
9350 defsubr (&Sfind_coding_systems_region_internal
);
9351 defsubr (&Sunencodable_char_position
);
9352 defsubr (&Scheck_coding_systems_region
);
9353 defsubr (&Sdecode_coding_region
);
9354 defsubr (&Sencode_coding_region
);
9355 defsubr (&Sdecode_coding_string
);
9356 defsubr (&Sencode_coding_string
);
9357 defsubr (&Sdecode_sjis_char
);
9358 defsubr (&Sencode_sjis_char
);
9359 defsubr (&Sdecode_big5_char
);
9360 defsubr (&Sencode_big5_char
);
9361 defsubr (&Sset_terminal_coding_system_internal
);
9362 defsubr (&Sset_safe_terminal_coding_system_internal
);
9363 defsubr (&Sterminal_coding_system
);
9364 defsubr (&Sset_keyboard_coding_system_internal
);
9365 defsubr (&Skeyboard_coding_system
);
9366 defsubr (&Sfind_operation_coding_system
);
9367 defsubr (&Sset_coding_system_priority
);
9368 defsubr (&Sdefine_coding_system_internal
);
9369 defsubr (&Sdefine_coding_system_alias
);
9370 defsubr (&Scoding_system_put
);
9371 defsubr (&Scoding_system_base
);
9372 defsubr (&Scoding_system_plist
);
9373 defsubr (&Scoding_system_aliases
);
9374 defsubr (&Scoding_system_eol_type
);
9375 defsubr (&Scoding_system_priority_list
);
9377 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9378 doc
: /* List of coding systems.
9380 Do not alter the value of this variable manually. This variable should be
9381 updated by the functions `define-coding-system' and
9382 `define-coding-system-alias'. */);
9383 Vcoding_system_list
= Qnil
;
9385 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9386 doc
: /* Alist of coding system names.
9387 Each element is one element list of coding system name.
9388 This variable is given to `completing-read' as TABLE argument.
9390 Do not alter the value of this variable manually. This variable should be
9391 updated by the functions `make-coding-system' and
9392 `define-coding-system-alias'. */);
9393 Vcoding_system_alist
= Qnil
;
9395 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9396 doc
: /* List of coding-categories (symbols) ordered by priority.
9398 On detecting a coding system, Emacs tries code detection algorithms
9399 associated with each coding-category one by one in this order. When
9400 one algorithm agrees with a byte sequence of source text, the coding
9401 system bound to the corresponding coding-category is selected.
9403 Don't modify this variable directly, but use `set-coding-priority'. */);
9407 Vcoding_category_list
= Qnil
;
9408 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9409 Vcoding_category_list
9410 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9411 Vcoding_category_list
);
9414 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9415 doc
: /* Specify the coding system for read operations.
9416 It is useful to bind this variable with `let', but do not set it globally.
9417 If the value is a coding system, it is used for decoding on read operation.
9418 If not, an appropriate element is used from one of the coding system alists:
9419 There are three such tables, `file-coding-system-alist',
9420 `process-coding-system-alist', and `network-coding-system-alist'. */);
9421 Vcoding_system_for_read
= Qnil
;
9423 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9424 doc
: /* Specify the coding system for write operations.
9425 Programs bind this variable with `let', but you should not set it globally.
9426 If the value is a coding system, it is used for encoding of output,
9427 when writing it to a file and when sending it to a file or subprocess.
9429 If this does not specify a coding system, an appropriate element
9430 is used from one of the coding system alists:
9431 There are three such tables, `file-coding-system-alist',
9432 `process-coding-system-alist', and `network-coding-system-alist'.
9433 For output to files, if the above procedure does not specify a coding system,
9434 the value of `buffer-file-coding-system' is used. */);
9435 Vcoding_system_for_write
= Qnil
;
9437 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9439 Coding system used in the latest file or process I/O. */);
9440 Vlast_coding_system_used
= Qnil
;
9442 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9444 Error status of the last code conversion.
9446 When an error was detected in the last code conversion, this variable
9447 is set to one of the following symbols.
9448 `insufficient-source'
9452 `insufficient-memory'
9453 When no error was detected, the value doesn't change. So, to check
9454 the error status of a code conversion by this variable, you must
9455 explicitly set this variable to nil before performing code
9457 Vlast_code_conversion_error
= Qnil
;
9459 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9461 *Non-nil means always inhibit code conversion of end-of-line format.
9462 See info node `Coding Systems' and info node `Text and Binary' concerning
9463 such conversion. */);
9464 inhibit_eol_conversion
= 0;
9466 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9468 Non-nil means process buffer inherits coding system of process output.
9469 Bind it to t if the process output is to be treated as if it were a file
9470 read from some filesystem. */);
9471 inherit_process_coding_system
= 0;
9473 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9475 Alist to decide a coding system to use for a file I/O operation.
9476 The format is ((PATTERN . VAL) ...),
9477 where PATTERN is a regular expression matching a file name,
9478 VAL is a coding system, a cons of coding systems, or a function symbol.
9479 If VAL is a coding system, it is used for both decoding and encoding
9481 If VAL is a cons of coding systems, the car part is used for decoding,
9482 and the cdr part is used for encoding.
9483 If VAL is a function symbol, the function must return a coding system
9484 or a cons of coding systems which are used as above. The function gets
9485 the arguments with which `find-operation-coding-systems' was called.
9487 See also the function `find-operation-coding-system'
9488 and the variable `auto-coding-alist'. */);
9489 Vfile_coding_system_alist
= Qnil
;
9491 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9493 Alist to decide a coding system to use for a process I/O operation.
9494 The format is ((PATTERN . VAL) ...),
9495 where PATTERN is a regular expression matching a program name,
9496 VAL is a coding system, a cons of coding systems, or a function symbol.
9497 If VAL is a coding system, it is used for both decoding what received
9498 from the program and encoding what sent to the program.
9499 If VAL is a cons of coding systems, the car part is used for decoding,
9500 and the cdr part is used for encoding.
9501 If VAL is a function symbol, the function must return a coding system
9502 or a cons of coding systems which are used as above.
9504 See also the function `find-operation-coding-system'. */);
9505 Vprocess_coding_system_alist
= Qnil
;
9507 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9509 Alist to decide a coding system to use for a network I/O operation.
9510 The format is ((PATTERN . VAL) ...),
9511 where PATTERN is a regular expression matching a network service name
9512 or is a port number to connect to,
9513 VAL is a coding system, a cons of coding systems, or a function symbol.
9514 If VAL is a coding system, it is used for both decoding what received
9515 from the network stream and encoding what sent to the network stream.
9516 If VAL is a cons of coding systems, the car part is used for decoding,
9517 and the cdr part is used for encoding.
9518 If VAL is a function symbol, the function must return a coding system
9519 or a cons of coding systems which are used as above.
9521 See also the function `find-operation-coding-system'. */);
9522 Vnetwork_coding_system_alist
= Qnil
;
9524 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9525 doc
: /* Coding system to use with system messages.
9526 Also used for decoding keyboard input on X Window system. */);
9527 Vlocale_coding_system
= Qnil
;
9529 /* The eol mnemonics are reset in startup.el system-dependently. */
9530 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9532 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9533 eol_mnemonic_unix
= build_string (":");
9535 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9537 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9538 eol_mnemonic_dos
= build_string ("\\");
9540 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9542 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9543 eol_mnemonic_mac
= build_string ("/");
9545 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9547 *String displayed in mode line when end-of-line format is not yet determined. */);
9548 eol_mnemonic_undecided
= build_string (":");
9550 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
9552 *Non-nil enables character translation while encoding and decoding. */);
9553 Venable_character_translation
= Qt
;
9555 DEFVAR_LISP ("standard-translation-table-for-decode",
9556 &Vstandard_translation_table_for_decode
,
9557 doc
: /* Table for translating characters while decoding. */);
9558 Vstandard_translation_table_for_decode
= Qnil
;
9560 DEFVAR_LISP ("standard-translation-table-for-encode",
9561 &Vstandard_translation_table_for_encode
,
9562 doc
: /* Table for translating characters while encoding. */);
9563 Vstandard_translation_table_for_encode
= Qnil
;
9565 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
9566 doc
: /* Alist of charsets vs revision numbers.
9567 While encoding, if a charset (car part of an element) is found,
9568 designate it with the escape sequence identifying revision (cdr part
9569 of the element). */);
9570 Vcharset_revision_table
= Qnil
;
9572 DEFVAR_LISP ("default-process-coding-system",
9573 &Vdefault_process_coding_system
,
9574 doc
: /* Cons of coding systems used for process I/O by default.
9575 The car part is used for decoding a process output,
9576 the cdr part is used for encoding a text to be sent to a process. */);
9577 Vdefault_process_coding_system
= Qnil
;
9579 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
9581 Table of extra Latin codes in the range 128..159 (inclusive).
9582 This is a vector of length 256.
9583 If Nth element is non-nil, the existence of code N in a file
9584 \(or output of subprocess) doesn't prevent it to be detected as
9585 a coding system of ISO 2022 variant which has a flag
9586 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9587 or reading output of a subprocess.
9588 Only 128th through 159th elements has a meaning. */);
9589 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
9591 DEFVAR_LISP ("select-safe-coding-system-function",
9592 &Vselect_safe_coding_system_function
,
9594 Function to call to select safe coding system for encoding a text.
9596 If set, this function is called to force a user to select a proper
9597 coding system which can encode the text in the case that a default
9598 coding system used in each operation can't encode the text.
9600 The default value is `select-safe-coding-system' (which see). */);
9601 Vselect_safe_coding_system_function
= Qnil
;
9603 DEFVAR_BOOL ("coding-system-require-warning",
9604 &coding_system_require_warning
,
9605 doc
: /* Internal use only.
9606 If non-nil, on writing a file, `select-safe-coding-system-function' is
9607 called even if `coding-system-for-write' is non-nil. The command
9608 `universal-coding-system-argument' binds this variable to t temporarily. */);
9609 coding_system_require_warning
= 0;
9612 DEFVAR_BOOL ("inhibit-iso-escape-detection",
9613 &inhibit_iso_escape_detection
,
9615 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
9617 By default, on reading a file, Emacs tries to detect how the text is
9618 encoded. This code detection is sensitive to escape sequences. If
9619 the sequence is valid as ISO2022, the code is determined as one of
9620 the ISO2022 encodings, and the file is decoded by the corresponding
9621 coding system (e.g. `iso-2022-7bit').
9623 However, there may be a case that you want to read escape sequences in
9624 a file as is. In such a case, you can set this variable to non-nil.
9625 Then, as the code detection ignores any escape sequences, no file is
9626 detected as encoded in some ISO2022 encoding. The result is that all
9627 escape sequences become visible in a buffer.
9629 The default value is nil, and it is strongly recommended not to change
9630 it. That is because many Emacs Lisp source files that contain
9631 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9632 in Emacs's distribution, and they won't be decoded correctly on
9633 reading if you suppress escape sequence detection.
9635 The other way to read escape sequences in a file without decoding is
9636 to explicitly specify some coding system that doesn't use ISO2022's
9637 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
9638 inhibit_iso_escape_detection
= 0;
9640 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
9641 doc
: /* Char table for translating self-inserting characters.
9642 This is applied to the result of input methods, not their input. See also
9643 `keyboard-translate-table'. */);
9644 Vtranslation_table_for_input
= Qnil
;
9647 Lisp_Object args
[coding_arg_max
];
9648 Lisp_Object plist
[16];
9651 for (i
= 0; i
< coding_arg_max
; i
++)
9654 plist
[0] = intern (":name");
9655 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
9656 plist
[2] = intern (":mnemonic");
9657 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
9658 plist
[4] = intern (":coding-type");
9659 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
9660 plist
[6] = intern (":ascii-compatible-p");
9661 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
9662 plist
[8] = intern (":default-char");
9663 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
9664 plist
[10] = intern (":for-unibyte");
9665 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
9666 plist
[12] = intern (":docstring");
9667 plist
[13] = build_string ("Do no conversion.\n\
9669 When you visit a file with this coding, the file is read into a\n\
9670 unibyte buffer as is, thus each byte of a file is treated as a\n\
9672 plist
[14] = intern (":eol-type");
9673 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
9674 args
[coding_arg_plist
] = Flist (16, plist
);
9675 Fdefine_coding_system_internal (coding_arg_max
, args
);
9677 plist
[1] = args
[coding_arg_name
] = Qundecided
;
9678 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
9679 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
9680 /* This is already set.
9681 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
9682 plist
[8] = intern (":charset-list");
9683 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
9684 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
9685 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9686 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
9687 args
[coding_arg_plist
] = Flist (16, plist
);
9688 Fdefine_coding_system_internal (coding_arg_max
, args
);
9691 setup_coding_system (Qno_conversion
, &keyboard_coding
);
9692 setup_coding_system (Qundecided
, &terminal_coding
);
9693 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
9698 for (i
= 0; i
< coding_category_max
; i
++)
9699 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
9704 emacs_strerror (error_number
)
9709 synchronize_system_messages_locale ();
9710 str
= strerror (error_number
);
9712 if (! NILP (Vlocale_coding_system
))
9714 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
9715 Vlocale_coding_system
,
9717 str
= (char *) SDATA (dec
);
9725 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
9726 (do not change this comment) */