1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1997, 1998, 2002, 2003, 2004, 2005
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
13 GNU Emacs is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2, or (at your option)
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs; see the file COPYING. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 Boston, MA 02110-1301, USA. */
28 /*** TABLE OF CONTENTS ***
32 2. Emacs' internal format (emacs-utf-8) handlers
35 5. Charset-base coding systems handlers
36 6. emacs-mule (old Emacs' internal format) handlers
38 8. Shift-JIS and BIG5 handlers
40 10. C library functions
41 11. Emacs Lisp library functions
46 /*** 0. General comments ***
51 A coding system is an object for an encoding mechanism that contains
52 information about how to convert byte sequences to character
53 sequences and vice versa. When we say "decode", it means converting
54 a byte sequence of a specific coding system into a character
55 sequence that is represented by Emacs' internal coding system
56 `emacs-utf-8', and when we say "encode", it means converting a
57 character sequence of emacs-utf-8 to a byte sequence of a specific
60 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
61 C level, a coding system is represented by a vector of attributes
62 stored in the hash table Vcharset_hash_table. The conversion from
63 coding system symbol to attributes vector is done by looking up
64 Vcharset_hash_table by the symbol.
66 Coding systems are classified into the following types depending on
67 the encoding mechanism. Here's a brief description of the types.
73 o Charset-base coding system
75 A coding system defined by one or more (coded) character sets.
76 Decoding and encoding are done by a code converter defined for each
79 o Old Emacs internal format (emacs-mule)
81 The coding system adopted by old versions of Emacs (20 and 21).
83 o ISO2022-base coding system
85 The most famous coding system for multiple character sets. X's
86 Compound Text, various EUCs (Extended Unix Code), and coding systems
87 used in the Internet communication such as ISO-2022-JP are all
90 o SJIS (or Shift-JIS or MS-Kanji-Code)
92 A coding system to encode character sets: ASCII, JISX0201, and
93 JISX0208. Widely used for PC's in Japan. Details are described in
98 A coding system to encode character sets: ASCII and Big5. Widely
99 used for Chinese (mainly in Taiwan and Hong Kong). Details are
100 described in section 8. In this file, when we write "big5" (all
101 lowercase), we mean the coding system, and when we write "Big5"
102 (capitalized), we mean the character set.
106 If a user wants to decode/encode text encoded in a coding system
107 not listed above, he can supply a decoder and an encoder for it in
108 CCL (Code Conversion Language) programs. Emacs executes the CCL
109 program while decoding/encoding.
113 A coding system for text containing raw eight-bit data. Emacs
114 treats each byte of source text as a character (except for
115 end-of-line conversion).
119 Like raw text, but don't do end-of-line conversion.
124 How text end-of-line is encoded depends on operating system. For
125 instance, Unix's format is just one byte of LF (line-feed) code,
126 whereas DOS's format is two-byte sequence of `carriage-return' and
127 `line-feed' codes. MacOS's format is usually one byte of
130 Since text character encoding and end-of-line encoding are
131 independent, any coding system described above can take any format
132 of end-of-line (except for no-conversion).
136 Before using a coding system for code conversion (i.e. decoding and
137 encoding), we setup a structure of type `struct coding_system'.
138 This structure keeps various information about a specific code
139 conversion (e.g. the location of source and destination data).
146 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
148 These functions check if a byte sequence specified as a source in
149 CODING conforms to the format of XXX, and update the members of
152 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
154 Below is the template of these functions. */
158 detect_coding_XXX (coding
, detect_info
)
159 struct coding_system
*coding
;
160 struct coding_detection_info
*detect_info
;
162 const unsigned char *src
= coding
->source
;
163 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
164 int multibytep
= coding
->src_multibyte
;
165 int consumed_chars
= 0;
171 /* Get one byte from the source. If the souce is exausted, jump
172 to no_more_source:. */
175 if (! __C_conforms_to_XXX___ (c
))
177 if (! __C_strongly_suggests_XXX__ (c
))
178 found
= CATEGORY_MASK_XXX
;
180 /* The byte sequence is invalid for XXX. */
181 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
185 /* The source exausted successfully. */
186 detect_info
->found
|= found
;
191 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
193 These functions decode a byte sequence specified as a source by
194 CODING. The resulting multibyte text goes to a place pointed to by
195 CODING->charbuf, the length of which should not exceed
196 CODING->charbuf_size;
198 These functions set the information of original and decoded texts in
199 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
200 They also set CODING->result to one of CODING_RESULT_XXX indicating
201 how the decoding is finished.
203 Below is the template of these functions. */
207 decode_coding_XXXX (coding
)
208 struct coding_system
*coding
;
210 const unsigned char *src
= coding
->source
+ coding
->consumed
;
211 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
212 /* SRC_BASE remembers the start position in source in each loop.
213 The loop will be exited when there's not enough source code, or
214 when there's no room in CHARBUF for a decoded character. */
215 const unsigned char *src_base
;
216 /* A buffer to produce decoded characters. */
217 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
218 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
219 int multibytep
= coding
->src_multibyte
;
224 if (charbuf
< charbuf_end
)
225 /* No more room to produce a decoded character. */
232 if (src_base
< src_end
233 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
234 /* If the source ends by partial bytes to construct a character,
235 treat them as eight-bit raw data. */
236 while (src_base
< src_end
&& charbuf
< charbuf_end
)
237 *charbuf
++ = *src_base
++;
238 /* Remember how many bytes and characters we consumed. If the
239 source is multibyte, the bytes and chars are not identical. */
240 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
241 /* Remember how many characters we produced. */
242 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
246 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
248 These functions encode SRC_BYTES length text at SOURCE of Emacs'
249 internal multibyte format by CODING. The resulting byte sequence
250 goes to a place pointed to by DESTINATION, the length of which
251 should not exceed DST_BYTES.
253 These functions set the information of original and encoded texts in
254 the members produced, produced_char, consumed, and consumed_char of
255 the structure *CODING. They also set the member result to one of
256 CODING_RESULT_XXX indicating how the encoding finished.
258 DST_BYTES zero means that source area and destination area are
259 overlapped, which means that we can produce a encoded text until it
260 reaches at the head of not-yet-encoded source text.
262 Below is a template of these functions. */
265 encode_coding_XXX (coding
)
266 struct coding_system
*coding
;
268 int multibytep
= coding
->dst_multibyte
;
269 int *charbuf
= coding
->charbuf
;
270 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
271 unsigned char *dst
= coding
->destination
+ coding
->produced
;
272 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
273 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
274 int produced_chars
= 0;
276 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
279 /* Encode C into DST, and increment DST. */
281 label_no_more_destination
:
282 /* How many chars and bytes we produced. */
283 coding
->produced_char
+= produced_chars
;
284 coding
->produced
= dst
- coding
->destination
;
289 /*** 1. Preamble ***/
296 #include "character.h"
299 #include "composite.h"
303 Lisp_Object Vcoding_system_hash_table
;
305 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
306 Lisp_Object Qunix
, Qdos
;
307 extern Lisp_Object Qmac
; /* frame.c */
308 Lisp_Object Qbuffer_file_coding_system
;
309 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
310 Lisp_Object Qdefault_char
;
311 Lisp_Object Qno_conversion
, Qundecided
;
312 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
313 Lisp_Object Qbig
, Qlittle
;
314 Lisp_Object Qcoding_system_history
;
315 Lisp_Object Qvalid_codes
;
316 Lisp_Object QCcategory
, QCmnemonic
, QCdefalut_char
;
317 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
318 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
319 Lisp_Object QCascii_compatible_p
;
321 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
322 Lisp_Object Qcall_process
, Qcall_process_region
;
323 Lisp_Object Qstart_process
, Qopen_network_stream
;
324 Lisp_Object Qtarget_idx
;
326 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
327 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
329 /* If a symbol has this property, evaluate the value to define the
330 symbol as a coding system. */
331 static Lisp_Object Qcoding_system_define_form
;
333 int coding_system_require_warning
;
335 Lisp_Object Vselect_safe_coding_system_function
;
337 /* Mnemonic string for each format of end-of-line. */
338 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
339 /* Mnemonic string to indicate format of end-of-line is not yet
341 Lisp_Object eol_mnemonic_undecided
;
345 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
347 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
349 /* Coding system emacs-mule and raw-text are for converting only
350 end-of-line format. */
351 Lisp_Object Qemacs_mule
, Qraw_text
;
352 Lisp_Object Qutf_8_emacs
;
354 /* Coding-systems are handed between Emacs Lisp programs and C internal
355 routines by the following three variables. */
356 /* Coding-system for reading files and receiving data from process. */
357 Lisp_Object Vcoding_system_for_read
;
358 /* Coding-system for writing files and sending data to process. */
359 Lisp_Object Vcoding_system_for_write
;
360 /* Coding-system actually used in the latest I/O. */
361 Lisp_Object Vlast_coding_system_used
;
362 /* Set to non-nil when an error is detected while code conversion. */
363 Lisp_Object Vlast_code_conversion_error
;
364 /* A vector of length 256 which contains information about special
365 Latin codes (especially for dealing with Microsoft codes). */
366 Lisp_Object Vlatin_extra_code_table
;
368 /* Flag to inhibit code conversion of end-of-line format. */
369 int inhibit_eol_conversion
;
371 /* Flag to inhibit ISO2022 escape sequence detection. */
372 int inhibit_iso_escape_detection
;
374 /* Flag to make buffer-file-coding-system inherit from process-coding. */
375 int inherit_process_coding_system
;
377 /* Coding system to be used to encode text for terminal display. */
378 struct coding_system terminal_coding
;
380 /* Coding system to be used to encode text for terminal display when
381 terminal coding system is nil. */
382 struct coding_system safe_terminal_coding
;
384 /* Coding system of what is sent from terminal keyboard. */
385 struct coding_system keyboard_coding
;
387 Lisp_Object Vfile_coding_system_alist
;
388 Lisp_Object Vprocess_coding_system_alist
;
389 Lisp_Object Vnetwork_coding_system_alist
;
391 Lisp_Object Vlocale_coding_system
;
395 /* Flag to tell if we look up translation table on character code
397 Lisp_Object Venable_character_translation
;
398 /* Standard translation table to look up on decoding (reading). */
399 Lisp_Object Vstandard_translation_table_for_decode
;
400 /* Standard translation table to look up on encoding (writing). */
401 Lisp_Object Vstandard_translation_table_for_encode
;
403 Lisp_Object Qtranslation_table
;
404 Lisp_Object Qtranslation_table_id
;
405 Lisp_Object Qtranslation_table_for_decode
;
406 Lisp_Object Qtranslation_table_for_encode
;
408 /* Alist of charsets vs revision number. */
409 static Lisp_Object Vcharset_revision_table
;
411 /* Default coding systems used for process I/O. */
412 Lisp_Object Vdefault_process_coding_system
;
414 /* Char table for translating Quail and self-inserting input. */
415 Lisp_Object Vtranslation_table_for_input
;
417 /* Two special coding systems. */
418 Lisp_Object Vsjis_coding_system
;
419 Lisp_Object Vbig5_coding_system
;
421 /* ISO2022 section */
423 #define CODING_ISO_INITIAL(coding, reg) \
424 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
425 coding_attr_iso_initial), \
429 #define CODING_ISO_REQUEST(coding, charset_id) \
430 ((charset_id <= (coding)->max_charset_id \
431 ? (coding)->safe_charsets[charset_id] \
435 #define CODING_ISO_FLAGS(coding) \
436 ((coding)->spec.iso_2022.flags)
437 #define CODING_ISO_DESIGNATION(coding, reg) \
438 ((coding)->spec.iso_2022.current_designation[reg])
439 #define CODING_ISO_INVOCATION(coding, plane) \
440 ((coding)->spec.iso_2022.current_invocation[plane])
441 #define CODING_ISO_SINGLE_SHIFTING(coding) \
442 ((coding)->spec.iso_2022.single_shifting)
443 #define CODING_ISO_BOL(coding) \
444 ((coding)->spec.iso_2022.bol)
445 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
446 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
448 /* Control characters of ISO2022. */
449 /* code */ /* function */
450 #define ISO_CODE_LF 0x0A /* line-feed */
451 #define ISO_CODE_CR 0x0D /* carriage-return */
452 #define ISO_CODE_SO 0x0E /* shift-out */
453 #define ISO_CODE_SI 0x0F /* shift-in */
454 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
455 #define ISO_CODE_ESC 0x1B /* escape */
456 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
457 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
458 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
460 /* All code (1-byte) of ISO2022 is classified into one of the
462 enum iso_code_class_type
464 ISO_control_0
, /* Control codes in the range
465 0x00..0x1F and 0x7F, except for the
466 following 5 codes. */
467 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
468 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
469 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
470 ISO_escape
, /* ISO_CODE_SO (0x1B) */
471 ISO_control_1
, /* Control codes in the range
472 0x80..0x9F, except for the
473 following 3 codes. */
474 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
475 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
476 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
477 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
478 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
479 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
480 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
483 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
484 `iso-flags' attribute of an iso2022 coding system. */
486 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
487 instead of the correct short-form sequence (e.g. ESC $ A). */
488 #define CODING_ISO_FLAG_LONG_FORM 0x0001
490 /* If set, reset graphic planes and registers at end-of-line to the
492 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
494 /* If set, reset graphic planes and registers before any control
495 characters to the initial state. */
496 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
498 /* If set, encode by 7-bit environment. */
499 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
501 /* If set, use locking-shift function. */
502 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
504 /* If set, use single-shift function. Overwrite
505 CODING_ISO_FLAG_LOCKING_SHIFT. */
506 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
508 /* If set, use designation escape sequence. */
509 #define CODING_ISO_FLAG_DESIGNATION 0x0040
511 /* If set, produce revision number sequence. */
512 #define CODING_ISO_FLAG_REVISION 0x0080
514 /* If set, produce ISO6429's direction specifying sequence. */
515 #define CODING_ISO_FLAG_DIRECTION 0x0100
517 /* If set, assume designation states are reset at beginning of line on
519 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
521 /* If set, designation sequence should be placed at beginning of line
523 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
525 /* If set, do not encode unsafe charactes on output. */
526 #define CODING_ISO_FLAG_SAFE 0x0800
528 /* If set, extra latin codes (128..159) are accepted as a valid code
530 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
532 #define CODING_ISO_FLAG_COMPOSITION 0x2000
534 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
536 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
538 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
540 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
542 /* A character to be produced on output if encoding of the original
543 character is prohibited by CODING_ISO_FLAG_SAFE. */
544 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
548 #define CODING_UTF_16_BOM(coding) \
549 ((coding)->spec.utf_16.bom)
551 #define CODING_UTF_16_ENDIAN(coding) \
552 ((coding)->spec.utf_16.endian)
554 #define CODING_UTF_16_SURROGATE(coding) \
555 ((coding)->spec.utf_16.surrogate)
559 #define CODING_CCL_DECODER(coding) \
560 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
561 #define CODING_CCL_ENCODER(coding) \
562 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
563 #define CODING_CCL_VALIDS(coding) \
564 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
566 /* Index for each coding category in `coding_categories' */
570 coding_category_iso_7
,
571 coding_category_iso_7_tight
,
572 coding_category_iso_8_1
,
573 coding_category_iso_8_2
,
574 coding_category_iso_7_else
,
575 coding_category_iso_8_else
,
576 coding_category_utf_8
,
577 coding_category_utf_16_auto
,
578 coding_category_utf_16_be
,
579 coding_category_utf_16_le
,
580 coding_category_utf_16_be_nosig
,
581 coding_category_utf_16_le_nosig
,
582 coding_category_charset
,
583 coding_category_sjis
,
584 coding_category_big5
,
586 coding_category_emacs_mule
,
587 /* All above are targets of code detection. */
588 coding_category_raw_text
,
589 coding_category_undecided
,
593 /* Definitions of flag bits used in detect_coding_XXXX. */
594 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
595 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
596 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
597 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
598 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
599 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
600 #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
601 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
602 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
603 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
604 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
605 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
606 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
607 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
608 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
609 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
610 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
611 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
613 /* This value is returned if detect_coding_mask () find nothing other
614 than ASCII characters. */
615 #define CATEGORY_MASK_ANY \
616 (CATEGORY_MASK_ISO_7 \
617 | CATEGORY_MASK_ISO_7_TIGHT \
618 | CATEGORY_MASK_ISO_8_1 \
619 | CATEGORY_MASK_ISO_8_2 \
620 | CATEGORY_MASK_ISO_7_ELSE \
621 | CATEGORY_MASK_ISO_8_ELSE \
622 | CATEGORY_MASK_UTF_8 \
623 | CATEGORY_MASK_UTF_16_BE \
624 | CATEGORY_MASK_UTF_16_LE \
625 | CATEGORY_MASK_UTF_16_BE_NOSIG \
626 | CATEGORY_MASK_UTF_16_LE_NOSIG \
627 | CATEGORY_MASK_CHARSET \
628 | CATEGORY_MASK_SJIS \
629 | CATEGORY_MASK_BIG5 \
630 | CATEGORY_MASK_CCL \
631 | CATEGORY_MASK_EMACS_MULE)
634 #define CATEGORY_MASK_ISO_7BIT \
635 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
637 #define CATEGORY_MASK_ISO_8BIT \
638 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
640 #define CATEGORY_MASK_ISO_ELSE \
641 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
643 #define CATEGORY_MASK_ISO_ESCAPE \
644 (CATEGORY_MASK_ISO_7 \
645 | CATEGORY_MASK_ISO_7_TIGHT \
646 | CATEGORY_MASK_ISO_7_ELSE \
647 | CATEGORY_MASK_ISO_8_ELSE)
649 #define CATEGORY_MASK_ISO \
650 ( CATEGORY_MASK_ISO_7BIT \
651 | CATEGORY_MASK_ISO_8BIT \
652 | CATEGORY_MASK_ISO_ELSE)
654 #define CATEGORY_MASK_UTF_16 \
655 (CATEGORY_MASK_UTF_16_BE \
656 | CATEGORY_MASK_UTF_16_LE \
657 | CATEGORY_MASK_UTF_16_BE_NOSIG \
658 | CATEGORY_MASK_UTF_16_LE_NOSIG)
661 /* List of symbols `coding-category-xxx' ordered by priority. This
662 variable is exposed to Emacs Lisp. */
663 static Lisp_Object Vcoding_category_list
;
665 /* Table of coding categories (Lisp symbols). This variable is for
667 static Lisp_Object Vcoding_category_table
;
669 /* Table of coding-categories ordered by priority. */
670 static enum coding_category coding_priorities
[coding_category_max
];
672 /* Nth element is a coding context for the coding system bound to the
673 Nth coding category. */
674 static struct coding_system coding_categories
[coding_category_max
];
676 /*** Commonly used macros and functions ***/
679 #define min(a, b) ((a) < (b) ? (a) : (b))
682 #define max(a, b) ((a) > (b) ? (a) : (b))
685 #define CODING_GET_INFO(coding, attrs, charset_list) \
687 (attrs) = CODING_ID_ATTRS ((coding)->id); \
688 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
692 /* Safely get one byte from the source text pointed by SRC which ends
693 at SRC_END, and set C to that byte. If there are not enough bytes
694 in the source, it jumps to `no_more_source'. If multibytep is
695 nonzero, and a multibyte character is found at SRC, set C to the
696 negative value of the character code. The caller should declare
697 and set these variables appropriately in advance:
698 src, src_end, multibytep */
700 #define ONE_MORE_BYTE(c) \
702 if (src == src_end) \
704 if (src_base < src) \
705 record_conversion_result \
706 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
707 goto no_more_source; \
710 if (multibytep && (c & 0x80)) \
712 if ((c & 0xFE) == 0xC0) \
713 c = ((c & 1) << 6) | *src++; \
717 c = - string_char (src, &src, NULL); \
718 record_conversion_result \
719 (coding, CODING_RESULT_INVALID_SRC); \
726 #define ONE_MORE_BYTE_NO_CHECK(c) \
729 if (multibytep && (c & 0x80)) \
731 if ((c & 0xFE) == 0xC0) \
732 c = ((c & 1) << 6) | *src++; \
736 c = - string_char (src, &src, NULL); \
737 record_conversion_result \
738 (coding, CODING_RESULT_INVALID_SRC); \
745 /* Store a byte C in the place pointed by DST and increment DST to the
746 next free point, and increment PRODUCED_CHARS. The caller should
747 assure that C is 0..127, and declare and set the variable `dst'
748 appropriately in advance.
752 #define EMIT_ONE_ASCII_BYTE(c) \
759 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
761 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
763 produced_chars += 2; \
764 *dst++ = (c1), *dst++ = (c2); \
768 /* Store a byte C in the place pointed by DST and increment DST to the
769 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
770 nonzero, store in an appropriate multibyte from. The caller should
771 declare and set the variables `dst' and `multibytep' appropriately
774 #define EMIT_ONE_BYTE(c) \
781 ch = BYTE8_TO_CHAR (ch); \
782 CHAR_STRING_ADVANCE (ch, dst); \
789 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
791 #define EMIT_TWO_BYTES(c1, c2) \
793 produced_chars += 2; \
800 ch = BYTE8_TO_CHAR (ch); \
801 CHAR_STRING_ADVANCE (ch, dst); \
804 ch = BYTE8_TO_CHAR (ch); \
805 CHAR_STRING_ADVANCE (ch, dst); \
815 #define EMIT_THREE_BYTES(c1, c2, c3) \
817 EMIT_ONE_BYTE (c1); \
818 EMIT_TWO_BYTES (c2, c3); \
822 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
824 EMIT_TWO_BYTES (c1, c2); \
825 EMIT_TWO_BYTES (c3, c4); \
829 /* Prototypes for static functions. */
830 static void record_conversion_result
P_ ((struct coding_system
*coding
,
831 enum coding_result_code result
));
832 static int detect_coding_utf_8
P_ ((struct coding_system
*,
833 struct coding_detection_info
*info
));
834 static void decode_coding_utf_8
P_ ((struct coding_system
*));
835 static int encode_coding_utf_8
P_ ((struct coding_system
*));
837 static int detect_coding_utf_16
P_ ((struct coding_system
*,
838 struct coding_detection_info
*info
));
839 static void decode_coding_utf_16
P_ ((struct coding_system
*));
840 static int encode_coding_utf_16
P_ ((struct coding_system
*));
842 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
843 struct coding_detection_info
*info
));
844 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
845 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
847 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
848 struct coding_detection_info
*info
));
849 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
850 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
852 static int detect_coding_sjis
P_ ((struct coding_system
*,
853 struct coding_detection_info
*info
));
854 static void decode_coding_sjis
P_ ((struct coding_system
*));
855 static int encode_coding_sjis
P_ ((struct coding_system
*));
857 static int detect_coding_big5
P_ ((struct coding_system
*,
858 struct coding_detection_info
*info
));
859 static void decode_coding_big5
P_ ((struct coding_system
*));
860 static int encode_coding_big5
P_ ((struct coding_system
*));
862 static int detect_coding_ccl
P_ ((struct coding_system
*,
863 struct coding_detection_info
*info
));
864 static void decode_coding_ccl
P_ ((struct coding_system
*));
865 static int encode_coding_ccl
P_ ((struct coding_system
*));
867 static void decode_coding_raw_text
P_ ((struct coding_system
*));
868 static int encode_coding_raw_text
P_ ((struct coding_system
*));
870 static void coding_set_source
P_ ((struct coding_system
*));
871 static void coding_set_destination
P_ ((struct coding_system
*));
872 static void coding_alloc_by_realloc
P_ ((struct coding_system
*, EMACS_INT
));
873 static void coding_alloc_by_making_gap
P_ ((struct coding_system
*,
875 static unsigned char *alloc_destination
P_ ((struct coding_system
*,
876 EMACS_INT
, unsigned char *));
877 static void setup_iso_safe_charsets
P_ ((Lisp_Object
));
878 static unsigned char *encode_designation_at_bol
P_ ((struct coding_system
*,
881 static int detect_eol
P_ ((const unsigned char *,
882 EMACS_INT
, enum coding_category
));
883 static Lisp_Object adjust_coding_eol_type
P_ ((struct coding_system
*, int));
884 static void decode_eol
P_ ((struct coding_system
*));
885 static Lisp_Object get_translation_table
P_ ((Lisp_Object
, int, int *));
886 static Lisp_Object get_translation
P_ ((Lisp_Object
, int *, int *,
888 static int produce_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
889 static INLINE
void produce_composition
P_ ((struct coding_system
*, int *,
891 static INLINE
void produce_charset
P_ ((struct coding_system
*, int *,
893 static void produce_annotation
P_ ((struct coding_system
*, EMACS_INT
));
894 static int decode_coding
P_ ((struct coding_system
*));
895 static INLINE
int *handle_composition_annotation
P_ ((EMACS_INT
, EMACS_INT
,
896 struct coding_system
*,
897 int *, EMACS_INT
*));
898 static INLINE
int *handle_charset_annotation
P_ ((EMACS_INT
, EMACS_INT
,
899 struct coding_system
*,
900 int *, EMACS_INT
*));
901 static void consume_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
902 static int encode_coding
P_ ((struct coding_system
*));
903 static Lisp_Object make_conversion_work_buffer
P_ ((int));
904 static Lisp_Object code_conversion_restore
P_ ((Lisp_Object
));
905 static INLINE
int char_encodable_p
P_ ((int, Lisp_Object
));
906 static Lisp_Object make_subsidiaries
P_ ((Lisp_Object
));
909 record_conversion_result (struct coding_system
*coding
,
910 enum coding_result_code result
)
912 coding
->result
= result
;
915 case CODING_RESULT_INSUFFICIENT_SRC
:
916 Vlast_code_conversion_error
= Qinsufficient_source
;
918 case CODING_RESULT_INCONSISTENT_EOL
:
919 Vlast_code_conversion_error
= Qinconsistent_eol
;
921 case CODING_RESULT_INVALID_SRC
:
922 Vlast_code_conversion_error
= Qinvalid_source
;
924 case CODING_RESULT_INTERRUPT
:
925 Vlast_code_conversion_error
= Qinterrupted
;
927 case CODING_RESULT_INSUFFICIENT_MEM
:
928 Vlast_code_conversion_error
= Qinsufficient_memory
;
931 Vlast_code_conversion_error
= intern ("Unknown error");
935 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
937 charset_map_loaded = 0; \
938 c = DECODE_CHAR (charset, code); \
939 if (charset_map_loaded) \
941 const unsigned char *orig = coding->source; \
944 coding_set_source (coding); \
945 offset = coding->source - orig; \
947 src_base += offset; \
953 #define ASSURE_DESTINATION(bytes) \
955 if (dst + (bytes) >= dst_end) \
957 int more_bytes = charbuf_end - charbuf + (bytes); \
959 dst = alloc_destination (coding, more_bytes, dst); \
960 dst_end = coding->destination + coding->dst_bytes; \
967 coding_set_source (coding
)
968 struct coding_system
*coding
;
970 if (BUFFERP (coding
->src_object
))
972 struct buffer
*buf
= XBUFFER (coding
->src_object
);
974 if (coding
->src_pos
< 0)
975 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
977 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
979 else if (STRINGP (coding
->src_object
))
981 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
984 /* Otherwise, the source is C string and is never relocated
985 automatically. Thus we don't have to update anything. */
990 coding_set_destination (coding
)
991 struct coding_system
*coding
;
993 if (BUFFERP (coding
->dst_object
))
995 if (coding
->src_pos
< 0)
997 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- 1;
998 coding
->dst_bytes
= (GAP_END_ADDR
999 - (coding
->src_bytes
- coding
->consumed
)
1000 - coding
->destination
);
1004 /* We are sure that coding->dst_pos_byte is before the gap
1006 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1007 + coding
->dst_pos_byte
- 1);
1008 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1009 - coding
->destination
);
1013 /* Otherwise, the destination is C string and is never relocated
1014 automatically. Thus we don't have to update anything. */
1020 coding_alloc_by_realloc (coding
, bytes
)
1021 struct coding_system
*coding
;
1024 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1025 coding
->dst_bytes
+ bytes
);
1026 coding
->dst_bytes
+= bytes
;
1030 coding_alloc_by_making_gap (coding
, bytes
)
1031 struct coding_system
*coding
;
1034 if (BUFFERP (coding
->dst_object
)
1035 && EQ (coding
->src_object
, coding
->dst_object
))
1037 EMACS_INT add
= coding
->src_bytes
- coding
->consumed
;
1039 GAP_SIZE
-= add
; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1041 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1045 Lisp_Object this_buffer
;
1047 this_buffer
= Fcurrent_buffer ();
1048 set_buffer_internal (XBUFFER (coding
->dst_object
));
1050 set_buffer_internal (XBUFFER (this_buffer
));
1055 static unsigned char *
1056 alloc_destination (coding
, nbytes
, dst
)
1057 struct coding_system
*coding
;
1061 EMACS_INT offset
= dst
- coding
->destination
;
1063 if (BUFFERP (coding
->dst_object
))
1064 coding_alloc_by_making_gap (coding
, nbytes
);
1066 coding_alloc_by_realloc (coding
, nbytes
);
1067 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1068 coding_set_destination (coding
);
1069 dst
= coding
->destination
+ offset
;
1073 /** Macros for annotations. */
1075 /* Maximum length of annotation data (sum of annotations for
1076 composition and charset). */
1077 #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1079 /* An annotation data is stored in the array coding->charbuf in this
1081 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1082 LENGTH is the number of elements in the annotation.
1083 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1084 NCHARS is the number of characters in the text annotated.
1086 The format of the following elements depend on ANNOTATION_MASK.
1088 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1090 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1091 METHOD is one of enum composition_method.
1092 Optionnal COMPOSITION-COMPONENTS are characters and composition
1095 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1098 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1100 *(buf)++ = -(len); \
1101 *(buf)++ = (mask); \
1102 *(buf)++ = (nchars); \
1103 coding->annotated = 1; \
1106 #define ADD_COMPOSITION_DATA(buf, nchars, method) \
1108 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1113 #define ADD_CHARSET_DATA(buf, nchars, id) \
1115 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1120 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1127 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1128 Check if a text is encoded in UTF-8. If it is, return 1, else
1131 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1132 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1133 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1134 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1135 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1136 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1139 detect_coding_utf_8 (coding
, detect_info
)
1140 struct coding_system
*coding
;
1141 struct coding_detection_info
*detect_info
;
1143 const unsigned char *src
= coding
->source
, *src_base
;
1144 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1145 int multibytep
= coding
->src_multibyte
;
1146 int consumed_chars
= 0;
1149 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1150 /* A coding system of this category is always ASCII compatible. */
1151 src
+= coding
->head_ascii
;
1155 int c
, c1
, c2
, c3
, c4
;
1159 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1162 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1164 if (UTF_8_2_OCTET_LEADING_P (c
))
1166 found
= CATEGORY_MASK_UTF_8
;
1170 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1172 if (UTF_8_3_OCTET_LEADING_P (c
))
1174 found
= CATEGORY_MASK_UTF_8
;
1178 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1180 if (UTF_8_4_OCTET_LEADING_P (c
))
1182 found
= CATEGORY_MASK_UTF_8
;
1186 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1188 if (UTF_8_5_OCTET_LEADING_P (c
))
1190 found
= CATEGORY_MASK_UTF_8
;
1195 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1199 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1201 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1204 detect_info
->found
|= found
;
1210 decode_coding_utf_8 (coding
)
1211 struct coding_system
*coding
;
1213 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1214 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1215 const unsigned char *src_base
;
1216 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1217 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1218 int consumed_chars
= 0, consumed_chars_base
;
1219 int multibytep
= coding
->src_multibyte
;
1220 Lisp_Object attr
, charset_list
;
1222 CODING_GET_INFO (coding
, attr
, charset_list
);
1226 int c
, c1
, c2
, c3
, c4
, c5
;
1229 consumed_chars_base
= consumed_chars
;
1231 if (charbuf
>= charbuf_end
)
1239 else if (UTF_8_1_OCTET_P(c1
))
1246 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1248 if (UTF_8_2_OCTET_LEADING_P (c1
))
1250 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1251 /* Reject overlong sequences here and below. Encoders
1252 producing them are incorrect, they can be misleading,
1253 and they mess up read/write invariance. */
1260 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1262 if (UTF_8_3_OCTET_LEADING_P (c1
))
1264 c
= (((c1
& 0xF) << 12)
1265 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1267 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1273 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1275 if (UTF_8_4_OCTET_LEADING_P (c1
))
1277 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1278 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1285 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1287 if (UTF_8_5_OCTET_LEADING_P (c1
))
1289 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1290 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1292 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1307 consumed_chars
= consumed_chars_base
;
1309 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1314 coding
->consumed_char
+= consumed_chars_base
;
1315 coding
->consumed
= src_base
- coding
->source
;
1316 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1321 encode_coding_utf_8 (coding
)
1322 struct coding_system
*coding
;
1324 int multibytep
= coding
->dst_multibyte
;
1325 int *charbuf
= coding
->charbuf
;
1326 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1327 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1328 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1329 int produced_chars
= 0;
1334 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1336 while (charbuf
< charbuf_end
)
1338 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1340 ASSURE_DESTINATION (safe_room
);
1342 if (CHAR_BYTE8_P (c
))
1344 c
= CHAR_TO_BYTE8 (c
);
1349 CHAR_STRING_ADVANCE (c
, pend
);
1350 for (p
= str
; p
< pend
; p
++)
1357 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1359 while (charbuf
< charbuf_end
)
1361 ASSURE_DESTINATION (safe_room
);
1363 if (CHAR_BYTE8_P (c
))
1364 *dst
++ = CHAR_TO_BYTE8 (c
);
1366 dst
+= CHAR_STRING (c
, dst
);
1370 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1371 coding
->produced_char
+= produced_chars
;
1372 coding
->produced
= dst
- coding
->destination
;
1377 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1378 Check if a text is encoded in one of UTF-16 based coding systems.
1379 If it is, return 1, else return 0. */
1381 #define UTF_16_HIGH_SURROGATE_P(val) \
1382 (((val) & 0xFC00) == 0xD800)
1384 #define UTF_16_LOW_SURROGATE_P(val) \
1385 (((val) & 0xFC00) == 0xDC00)
1387 #define UTF_16_INVALID_P(val) \
1388 (((val) == 0xFFFE) \
1389 || ((val) == 0xFFFF) \
1390 || UTF_16_LOW_SURROGATE_P (val))
1394 detect_coding_utf_16 (coding
, detect_info
)
1395 struct coding_system
*coding
;
1396 struct coding_detection_info
*detect_info
;
1398 const unsigned char *src
= coding
->source
, *src_base
= src
;
1399 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1400 int multibytep
= coding
->src_multibyte
;
1401 int consumed_chars
= 0;
1404 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1405 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1406 && (coding
->src_chars
& 1))
1408 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1414 if ((c1
== 0xFF) && (c2
== 0xFE))
1416 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1417 | CATEGORY_MASK_UTF_16_AUTO
);
1418 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1419 | CATEGORY_MASK_UTF_16_BE_NOSIG
1420 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1422 else if ((c1
== 0xFE) && (c2
== 0xFF))
1424 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1425 | CATEGORY_MASK_UTF_16_AUTO
);
1426 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1427 | CATEGORY_MASK_UTF_16_BE_NOSIG
1428 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1430 else if (c1
>= 0 && c2
>= 0)
1432 detect_info
->rejected
1433 |= (CATEGORY_MASK_UTF_16_BE
| CATEGORY_MASK_UTF_16_LE
);
1440 decode_coding_utf_16 (coding
)
1441 struct coding_system
*coding
;
1443 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1444 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1445 const unsigned char *src_base
;
1446 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1447 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1448 int consumed_chars
= 0, consumed_chars_base
;
1449 int multibytep
= coding
->src_multibyte
;
1450 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1451 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1452 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1453 Lisp_Object attr
, charset_list
;
1455 CODING_GET_INFO (coding
, attr
, charset_list
);
1457 if (bom
== utf_16_with_bom
)
1466 if (endian
== utf_16_big_endian
1467 ? c
!= 0xFEFF : c
!= 0xFFFE)
1469 /* The first two bytes are not BOM. Treat them as bytes
1470 for a normal character. */
1474 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1476 else if (bom
== utf_16_detect_bom
)
1478 /* We have already tried to detect BOM and failed in
1480 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1488 consumed_chars_base
= consumed_chars
;
1490 if (charbuf
+ 2 >= charbuf_end
)
1502 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1506 c
= (endian
== utf_16_big_endian
1507 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1510 if (! UTF_16_LOW_SURROGATE_P (c
))
1512 if (endian
== utf_16_big_endian
)
1513 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1515 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1519 if (UTF_16_HIGH_SURROGATE_P (c
))
1520 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1526 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1527 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1528 *charbuf
++ = 0x10000 + c
;
1533 if (UTF_16_HIGH_SURROGATE_P (c
))
1534 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1541 coding
->consumed_char
+= consumed_chars_base
;
1542 coding
->consumed
= src_base
- coding
->source
;
1543 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1547 encode_coding_utf_16 (coding
)
1548 struct coding_system
*coding
;
1550 int multibytep
= coding
->dst_multibyte
;
1551 int *charbuf
= coding
->charbuf
;
1552 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1553 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1554 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1556 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1557 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1558 int produced_chars
= 0;
1559 Lisp_Object attrs
, charset_list
;
1562 CODING_GET_INFO (coding
, attrs
, charset_list
);
1564 if (bom
!= utf_16_without_bom
)
1566 ASSURE_DESTINATION (safe_room
);
1568 EMIT_TWO_BYTES (0xFE, 0xFF);
1570 EMIT_TWO_BYTES (0xFF, 0xFE);
1571 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1574 while (charbuf
< charbuf_end
)
1576 ASSURE_DESTINATION (safe_room
);
1578 if (c
>= MAX_UNICODE_CHAR
)
1579 c
= coding
->default_char
;
1584 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1586 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1593 c1
= (c
>> 10) + 0xD800;
1594 c2
= (c
& 0x3FF) + 0xDC00;
1596 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1598 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1601 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1602 coding
->produced
= dst
- coding
->destination
;
1603 coding
->produced_char
+= produced_chars
;
1608 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1610 /* Emacs' internal format for representation of multiple character
1611 sets is a kind of multi-byte encoding, i.e. characters are
1612 represented by variable-length sequences of one-byte codes.
1614 ASCII characters and control characters (e.g. `tab', `newline') are
1615 represented by one-byte sequences which are their ASCII codes, in
1616 the range 0x00 through 0x7F.
1618 8-bit characters of the range 0x80..0x9F are represented by
1619 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1622 8-bit characters of the range 0xA0..0xFF are represented by
1623 one-byte sequences which are their 8-bit code.
1625 The other characters are represented by a sequence of `base
1626 leading-code', optional `extended leading-code', and one or two
1627 `position-code's. The length of the sequence is determined by the
1628 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1629 whereas extended leading-code and position-code take the range 0xA0
1630 through 0xFF. See `charset.h' for more details about leading-code
1633 --- CODE RANGE of Emacs' internal format ---
1637 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1638 eight-bit-graphic 0xA0..0xBF
1639 ELSE 0x81..0x9D + [0xA0..0xFF]+
1640 ---------------------------------------------
1642 As this is the internal character representation, the format is
1643 usually not used externally (i.e. in a file or in a data sent to a
1644 process). But, it is possible to have a text externally in this
1645 format (i.e. by encoding by the coding system `emacs-mule').
1647 In that case, a sequence of one-byte codes has a slightly different
1650 At first, all characters in eight-bit-control are represented by
1651 one-byte sequences which are their 8-bit code.
1653 Next, character composition data are represented by the byte
1654 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1656 METHOD is 0xF0 plus one of composition method (enum
1657 composition_method),
1659 BYTES is 0xA0 plus a byte length of this composition data,
1661 CHARS is 0x20 plus a number of characters composed by this
1664 COMPONENTs are characters of multibye form or composition
1665 rules encoded by two-byte of ASCII codes.
1667 In addition, for backward compatibility, the following formats are
1668 also recognized as composition data on decoding.
1671 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1674 MSEQ is a multibyte form but in these special format:
1675 ASCII: 0xA0 ASCII_CODE+0x80,
1676 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1677 RULE is a one byte code of the range 0xA0..0xF0 that
1678 represents a composition rule.
1681 char emacs_mule_bytes
[256];
1684 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
)
1685 struct coding_system
*coding
;
1686 const unsigned char *src
;
1687 int *nbytes
, *nchars
, *id
;
1689 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1690 const unsigned char *src_base
= src
;
1691 int multibytep
= coding
->src_multibyte
;
1692 struct charset
*charset
;
1695 int consumed_chars
= 0;
1701 charset
= emacs_mule_charset
[0];
1705 switch (emacs_mule_bytes
[c
])
1708 if (! (charset
= emacs_mule_charset
[c
]))
1717 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1718 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1721 if (c
< 0xA0 || ! (charset
= emacs_mule_charset
[c
]))
1730 if (! (charset
= emacs_mule_charset
[c
]))
1735 code
= (c
& 0x7F) << 8;
1745 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
1750 code
= (c
& 0x7F) << 8;
1759 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
1760 ? charset_ascii
: charset_eight_bit
);
1766 c
= DECODE_CHAR (charset
, code
);
1770 *nbytes
= src
- src_base
;
1771 *nchars
= consumed_chars
;
1784 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1785 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1789 detect_coding_emacs_mule (coding
, detect_info
)
1790 struct coding_system
*coding
;
1791 struct coding_detection_info
*detect_info
;
1793 const unsigned char *src
= coding
->source
, *src_base
;
1794 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1795 int multibytep
= coding
->src_multibyte
;
1796 int consumed_chars
= 0;
1800 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1801 /* A coding system of this category is always ASCII compatible. */
1802 src
+= coding
->head_ascii
;
1812 /* Perhaps the start of composite character. We simple skip
1813 it because analyzing it is too heavy for detecting. But,
1814 at least, we check that the composite character
1815 constitues of more than 4 bytes. */
1816 const unsigned char *src_base
;
1826 if (src
- src_base
<= 4)
1828 found
= CATEGORY_MASK_EMACS_MULE
;
1836 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1841 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
1843 while (more_bytes
> 0)
1848 src
--; /* Unread the last byte. */
1853 if (more_bytes
!= 0)
1855 found
= CATEGORY_MASK_EMACS_MULE
;
1858 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1862 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1864 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1867 detect_info
->found
|= found
;
1872 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1874 /* Decode a character represented as a component of composition
1875 sequence of Emacs 20/21 style at SRC. Set C to that character and
1876 update SRC to the head of next character (or an encoded composition
1877 rule). If SRC doesn't points a composition component, set C to -1.
1878 If SRC points an invalid byte sequence, global exit by a return
1881 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
1885 int nbytes, nchars; \
1887 if (src == src_end) \
1889 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
1894 goto invalid_code; \
1898 consumed_chars += nchars; \
1903 /* Decode a composition rule represented as a component of composition
1904 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
1905 and increment BUF. If SRC points an invalid byte sequence, set C
1908 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
1910 int c, gref, nref; \
1912 if (src >= src_end) \
1913 goto invalid_code; \
1914 ONE_MORE_BYTE_NO_CHECK (c); \
1916 if (c < 0 || c >= 81) \
1917 goto invalid_code; \
1919 gref = c / 9, nref = c % 9; \
1920 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1924 /* Decode a composition rule represented as a component of composition
1925 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
1926 and increment BUF. If SRC points an invalid byte sequence, set C
1929 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
1933 if (src + 1>= src_end) \
1934 goto invalid_code; \
1935 ONE_MORE_BYTE_NO_CHECK (gref); \
1937 ONE_MORE_BYTE_NO_CHECK (nref); \
1939 if (gref < 0 || gref >= 81 \
1940 || nref < 0 || nref >= 81) \
1941 goto invalid_code; \
1942 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1946 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
1948 /* Emacs 21 style format. The first three bytes at SRC are \
1949 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
1950 the byte length of this composition information, CHARS is the \
1951 number of characters composed by this composition. */ \
1952 enum composition_method method = c - 0xF2; \
1953 int *charbuf_base = charbuf; \
1954 int consumed_chars_limit; \
1955 int nbytes, nchars; \
1957 ONE_MORE_BYTE (c); \
1959 goto invalid_code; \
1960 nbytes = c - 0xA0; \
1962 goto invalid_code; \
1963 ONE_MORE_BYTE (c); \
1965 goto invalid_code; \
1966 nchars = c - 0xA0; \
1967 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
1968 consumed_chars_limit = consumed_chars_base + nbytes; \
1969 if (method != COMPOSITION_RELATIVE) \
1972 while (consumed_chars < consumed_chars_limit) \
1974 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
1975 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
1977 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
1980 if (consumed_chars < consumed_chars_limit) \
1981 goto invalid_code; \
1982 charbuf_base[0] -= i; \
1987 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
1989 /* Emacs 20 style format for relative composition. */ \
1990 /* Store multibyte form of characters to be composed. */ \
1991 enum composition_method method = COMPOSITION_RELATIVE; \
1992 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
1993 int *buf = components; \
1997 ONE_MORE_BYTE (c); /* skip 0x80 */ \
1998 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
1999 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2001 goto invalid_code; \
2002 ADD_COMPOSITION_DATA (charbuf, i, method); \
2003 for (j = 0; j < i; j++) \
2004 *charbuf++ = components[j]; \
2008 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
2010 /* Emacs 20 style format for rule-base composition. */ \
2011 /* Store multibyte form of characters to be composed. */ \
2012 enum composition_method method = COMPOSITION_WITH_RULE; \
2013 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2014 int *buf = components; \
2017 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2018 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
2020 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
2021 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2023 if (i < 1 || (buf - components) % 2 == 0) \
2024 goto invalid_code; \
2025 if (charbuf + i + (i / 2) + 1 < charbuf_end) \
2026 goto no_more_source; \
2027 ADD_COMPOSITION_DATA (buf, i, method); \
2028 for (j = 0; j < i; j++) \
2029 *charbuf++ = components[j]; \
2030 for (j = 0; j < i; j += 2) \
2031 *charbuf++ = components[j]; \
2036 decode_coding_emacs_mule (coding
)
2037 struct coding_system
*coding
;
2039 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2040 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2041 const unsigned char *src_base
;
2042 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2044 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
2045 int consumed_chars
= 0, consumed_chars_base
;
2046 int multibytep
= coding
->src_multibyte
;
2047 Lisp_Object attrs
, charset_list
;
2048 int char_offset
= coding
->produced_char
;
2049 int last_offset
= char_offset
;
2050 int last_id
= charset_ascii
;
2052 CODING_GET_INFO (coding
, attrs
, charset_list
);
2059 consumed_chars_base
= consumed_chars
;
2061 if (charbuf
>= charbuf_end
)
2080 if (c
- 0xF2 >= COMPOSITION_RELATIVE
2081 && c
- 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS
)
2082 DECODE_EMACS_MULE_21_COMPOSITION (c
);
2084 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c
);
2086 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c
);
2090 else if (c
< 0xA0 && emacs_mule_bytes
[c
] > 1)
2096 consumed_chars
= consumed_chars_base
;
2097 c
= emacs_mule_char (coding
, src
, &nbytes
, &nchars
, &id
);
2106 if (last_id
!= charset_ascii
)
2107 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2109 last_offset
= char_offset
;
2113 consumed_chars
+= nchars
;
2120 consumed_chars
= consumed_chars_base
;
2122 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2128 if (last_id
!= charset_ascii
)
2129 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2130 coding
->consumed_char
+= consumed_chars_base
;
2131 coding
->consumed
= src_base
- coding
->source
;
2132 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2136 #define EMACS_MULE_LEADING_CODES(id, codes) \
2139 codes[0] = id, codes[1] = 0; \
2140 else if (id < 0xE0) \
2141 codes[0] = 0x9A, codes[1] = id; \
2142 else if (id < 0xF0) \
2143 codes[0] = 0x9B, codes[1] = id; \
2144 else if (id < 0xF5) \
2145 codes[0] = 0x9C, codes[1] = id; \
2147 codes[0] = 0x9D, codes[1] = id; \
2152 encode_coding_emacs_mule (coding
)
2153 struct coding_system
*coding
;
2155 int multibytep
= coding
->dst_multibyte
;
2156 int *charbuf
= coding
->charbuf
;
2157 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2158 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2159 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2161 int produced_chars
= 0;
2162 Lisp_Object attrs
, charset_list
;
2164 int preferred_charset_id
= -1;
2166 CODING_GET_INFO (coding
, attrs
, charset_list
);
2167 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2169 CODING_ATTR_CHARSET_LIST (attrs
)
2170 = charset_list
= Vemacs_mule_charset_list
;
2173 while (charbuf
< charbuf_end
)
2175 ASSURE_DESTINATION (safe_room
);
2180 /* Handle an annotation. */
2183 case CODING_ANNOTATE_COMPOSITION_MASK
:
2184 /* Not yet implemented. */
2186 case CODING_ANNOTATE_CHARSET_MASK
:
2187 preferred_charset_id
= charbuf
[3];
2188 if (preferred_charset_id
>= 0
2189 && NILP (Fmemq (make_number (preferred_charset_id
),
2191 preferred_charset_id
= -1;
2200 if (ASCII_CHAR_P (c
))
2201 EMIT_ONE_ASCII_BYTE (c
);
2202 else if (CHAR_BYTE8_P (c
))
2204 c
= CHAR_TO_BYTE8 (c
);
2209 struct charset
*charset
;
2213 unsigned char leading_codes
[2];
2215 if (preferred_charset_id
>= 0)
2217 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2218 if (! CHAR_CHARSET_P (c
, charset
))
2219 charset
= char_charset (c
, charset_list
, NULL
);
2222 charset
= char_charset (c
, charset_list
, &code
);
2225 c
= coding
->default_char
;
2226 if (ASCII_CHAR_P (c
))
2228 EMIT_ONE_ASCII_BYTE (c
);
2231 charset
= char_charset (c
, charset_list
, &code
);
2233 dimension
= CHARSET_DIMENSION (charset
);
2234 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2235 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2236 EMIT_ONE_BYTE (leading_codes
[0]);
2237 if (leading_codes
[1])
2238 EMIT_ONE_BYTE (leading_codes
[1]);
2240 EMIT_ONE_BYTE (code
| 0x80);
2244 EMIT_ONE_BYTE (code
>> 8);
2245 EMIT_ONE_BYTE (code
& 0xFF);
2249 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2250 coding
->produced_char
+= produced_chars
;
2251 coding
->produced
= dst
- coding
->destination
;
2256 /*** 7. ISO2022 handlers ***/
2258 /* The following note describes the coding system ISO2022 briefly.
2259 Since the intention of this note is to help understand the
2260 functions in this file, some parts are NOT ACCURATE or are OVERLY
2261 SIMPLIFIED. For thorough understanding, please refer to the
2262 original document of ISO2022. This is equivalent to the standard
2263 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2265 ISO2022 provides many mechanisms to encode several character sets
2266 in 7-bit and 8-bit environments. For 7-bit environments, all text
2267 is encoded using bytes less than 128. This may make the encoded
2268 text a little bit longer, but the text passes more easily through
2269 several types of gateway, some of which strip off the MSB (Most
2272 There are two kinds of character sets: control character sets and
2273 graphic character sets. The former contain control characters such
2274 as `newline' and `escape' to provide control functions (control
2275 functions are also provided by escape sequences). The latter
2276 contain graphic characters such as 'A' and '-'. Emacs recognizes
2277 two control character sets and many graphic character sets.
2279 Graphic character sets are classified into one of the following
2280 four classes, according to the number of bytes (DIMENSION) and
2281 number of characters in one dimension (CHARS) of the set:
2282 - DIMENSION1_CHARS94
2283 - DIMENSION1_CHARS96
2284 - DIMENSION2_CHARS94
2285 - DIMENSION2_CHARS96
2287 In addition, each character set is assigned an identification tag,
2288 unique for each set, called the "final character" (denoted as <F>
2289 hereafter). The <F> of each character set is decided by ECMA(*)
2290 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2291 (0x30..0x3F are for private use only).
2293 Note (*): ECMA = European Computer Manufacturers Association
2295 Here are examples of graphic character sets [NAME(<F>)]:
2296 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2297 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2298 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2299 o DIMENSION2_CHARS96 -- none for the moment
2301 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2302 C0 [0x00..0x1F] -- control character plane 0
2303 GL [0x20..0x7F] -- graphic character plane 0
2304 C1 [0x80..0x9F] -- control character plane 1
2305 GR [0xA0..0xFF] -- graphic character plane 1
2307 A control character set is directly designated and invoked to C0 or
2308 C1 by an escape sequence. The most common case is that:
2309 - ISO646's control character set is designated/invoked to C0, and
2310 - ISO6429's control character set is designated/invoked to C1,
2311 and usually these designations/invocations are omitted in encoded
2312 text. In a 7-bit environment, only C0 can be used, and a control
2313 character for C1 is encoded by an appropriate escape sequence to
2314 fit into the environment. All control characters for C1 are
2315 defined to have corresponding escape sequences.
2317 A graphic character set is at first designated to one of four
2318 graphic registers (G0 through G3), then these graphic registers are
2319 invoked to GL or GR. These designations and invocations can be
2320 done independently. The most common case is that G0 is invoked to
2321 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2322 these invocations and designations are omitted in encoded text.
2323 In a 7-bit environment, only GL can be used.
2325 When a graphic character set of CHARS94 is invoked to GL, codes
2326 0x20 and 0x7F of the GL area work as control characters SPACE and
2327 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2330 There are two ways of invocation: locking-shift and single-shift.
2331 With locking-shift, the invocation lasts until the next different
2332 invocation, whereas with single-shift, the invocation affects the
2333 following character only and doesn't affect the locking-shift
2334 state. Invocations are done by the following control characters or
2337 ----------------------------------------------------------------------
2338 abbrev function cntrl escape seq description
2339 ----------------------------------------------------------------------
2340 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2341 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2342 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2343 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2344 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2345 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2346 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2347 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2348 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2349 ----------------------------------------------------------------------
2350 (*) These are not used by any known coding system.
2352 Control characters for these functions are defined by macros
2353 ISO_CODE_XXX in `coding.h'.
2355 Designations are done by the following escape sequences:
2356 ----------------------------------------------------------------------
2357 escape sequence description
2358 ----------------------------------------------------------------------
2359 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2360 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2361 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2362 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2363 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2364 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2365 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2366 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2367 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2368 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2369 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2370 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2371 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2372 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2373 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2374 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2375 ----------------------------------------------------------------------
2377 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2378 of dimension 1, chars 94, and final character <F>, etc...
2380 Note (*): Although these designations are not allowed in ISO2022,
2381 Emacs accepts them on decoding, and produces them on encoding
2382 CHARS96 character sets in a coding system which is characterized as
2383 7-bit environment, non-locking-shift, and non-single-shift.
2385 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2386 '(' must be omitted. We refer to this as "short-form" hereafter.
2388 Now you may notice that there are a lot of ways of encoding the
2389 same multilingual text in ISO2022. Actually, there exist many
2390 coding systems such as Compound Text (used in X11's inter client
2391 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2392 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2393 localized platforms), and all of these are variants of ISO2022.
2395 In addition to the above, Emacs handles two more kinds of escape
2396 sequences: ISO6429's direction specification and Emacs' private
2397 sequence for specifying character composition.
2399 ISO6429's direction specification takes the following form:
2400 o CSI ']' -- end of the current direction
2401 o CSI '0' ']' -- end of the current direction
2402 o CSI '1' ']' -- start of left-to-right text
2403 o CSI '2' ']' -- start of right-to-left text
2404 The control character CSI (0x9B: control sequence introducer) is
2405 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2407 Character composition specification takes the following form:
2408 o ESC '0' -- start relative composition
2409 o ESC '1' -- end composition
2410 o ESC '2' -- start rule-base composition (*)
2411 o ESC '3' -- start relative composition with alternate chars (**)
2412 o ESC '4' -- start rule-base composition with alternate chars (**)
2413 Since these are not standard escape sequences of any ISO standard,
2414 the use of them with these meanings is restricted to Emacs only.
2416 (*) This form is used only in Emacs 20.7 and older versions,
2417 but newer versions can safely decode it.
2418 (**) This form is used only in Emacs 21.1 and newer versions,
2419 and older versions can't decode it.
2421 Here's a list of example usages of these composition escape
2422 sequences (categorized by `enum composition_method').
2424 COMPOSITION_RELATIVE:
2425 ESC 0 CHAR [ CHAR ] ESC 1
2426 COMPOSITION_WITH_RULE:
2427 ESC 2 CHAR [ RULE CHAR ] ESC 1
2428 COMPOSITION_WITH_ALTCHARS:
2429 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2430 COMPOSITION_WITH_RULE_ALTCHARS:
2431 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2433 enum iso_code_class_type iso_code_class
[256];
2435 #define SAFE_CHARSET_P(coding, id) \
2436 ((id) <= (coding)->max_charset_id \
2437 && (coding)->safe_charsets[id] >= 0)
2440 #define SHIFT_OUT_OK(category) \
2441 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2444 setup_iso_safe_charsets (attrs
)
2447 Lisp_Object charset_list
, safe_charsets
;
2448 Lisp_Object request
;
2449 Lisp_Object reg_usage
;
2452 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2455 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2456 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2457 && ! EQ (charset_list
, Viso_2022_charset_list
))
2459 CODING_ATTR_CHARSET_LIST (attrs
)
2460 = charset_list
= Viso_2022_charset_list
;
2461 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2464 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2468 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2470 int id
= XINT (XCAR (tail
));
2471 if (max_charset_id
< id
)
2472 max_charset_id
= id
;
2475 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
2477 request
= AREF (attrs
, coding_attr_iso_request
);
2478 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2479 reg94
= XINT (XCAR (reg_usage
));
2480 reg96
= XINT (XCDR (reg_usage
));
2482 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2486 struct charset
*charset
;
2489 charset
= CHARSET_FROM_ID (XINT (id
));
2490 reg
= Fcdr (Fassq (id
, request
));
2492 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2493 else if (charset
->iso_chars_96
)
2496 SSET (safe_charsets
, XINT (id
), reg96
);
2501 SSET (safe_charsets
, XINT (id
), reg94
);
2504 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2508 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2509 Check if a text is encoded in one of ISO-2022 based codig systems.
2510 If it is, return 1, else return 0. */
2513 detect_coding_iso_2022 (coding
, detect_info
)
2514 struct coding_system
*coding
;
2515 struct coding_detection_info
*detect_info
;
2517 const unsigned char *src
= coding
->source
, *src_base
= src
;
2518 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2519 int multibytep
= coding
->src_multibyte
;
2520 int single_shifting
= 0;
2523 int consumed_chars
= 0;
2528 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2530 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2532 struct coding_system
*this = &(coding_categories
[i
]);
2533 Lisp_Object attrs
, val
;
2535 attrs
= CODING_ID_ATTRS (this->id
);
2536 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2537 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs
), Viso_2022_charset_list
))
2538 setup_iso_safe_charsets (attrs
);
2539 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2540 this->max_charset_id
= SCHARS (val
) - 1;
2541 this->safe_charsets
= (char *) SDATA (val
);
2544 /* A coding system of this category is always ASCII compatible. */
2545 src
+= coding
->head_ascii
;
2547 while (rejected
!= CATEGORY_MASK_ISO
)
2554 if (inhibit_iso_escape_detection
)
2556 single_shifting
= 0;
2558 if (c
>= '(' && c
<= '/')
2560 /* Designation sequence for a charset of dimension 1. */
2562 if (c1
< ' ' || c1
>= 0x80
2563 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2564 /* Invalid designation sequence. Just ignore. */
2569 /* Designation sequence for a charset of dimension 2. */
2571 if (c
>= '@' && c
<= 'B')
2572 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2573 id
= iso_charset_table
[1][0][c
];
2574 else if (c
>= '(' && c
<= '/')
2577 if (c1
< ' ' || c1
>= 0x80
2578 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2579 /* Invalid designation sequence. Just ignore. */
2583 /* Invalid designation sequence. Just ignore it. */
2586 else if (c
== 'N' || c
== 'O')
2588 /* ESC <Fe> for SS2 or SS3. */
2589 single_shifting
= 1;
2590 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2593 else if (c
>= '0' && c
<= '4')
2595 /* ESC <Fp> for start/end composition. */
2596 found
|= CATEGORY_MASK_ISO
;
2601 /* Invalid escape sequence. Just ignore it. */
2605 /* We found a valid designation sequence for CHARSET. */
2606 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2607 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2609 found
|= CATEGORY_MASK_ISO_7
;
2611 rejected
|= CATEGORY_MASK_ISO_7
;
2612 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
2614 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
2616 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
2617 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
2619 found
|= CATEGORY_MASK_ISO_7_ELSE
;
2621 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
2622 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
2624 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2626 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
2631 /* Locking shift out/in. */
2632 if (inhibit_iso_escape_detection
)
2634 single_shifting
= 0;
2635 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2636 found
|= CATEGORY_MASK_ISO_ELSE
;
2640 /* Control sequence introducer. */
2641 single_shifting
= 0;
2642 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2643 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2644 goto check_extra_latin
;
2649 if (inhibit_iso_escape_detection
)
2651 single_shifting
= 0;
2652 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2653 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2654 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2655 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2656 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2657 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2658 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2659 if (single_shifting
)
2661 goto check_extra_latin
;
2668 single_shifting
= 0;
2673 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2674 found
|= CATEGORY_MASK_ISO_8_1
;
2675 /* Check the length of succeeding codes of the range
2676 0xA0..0FF. If the byte length is even, we include
2677 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2678 only when we are not single shifting. */
2679 if (! single_shifting
2680 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2683 while (src
< src_end
)
2691 if (i
& 1 && src
< src_end
)
2692 rejected
|= CATEGORY_MASK_ISO_8_2
;
2694 found
|= CATEGORY_MASK_ISO_8_2
;
2699 single_shifting
= 0;
2700 if (! VECTORP (Vlatin_extra_code_table
)
2701 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
2703 rejected
= CATEGORY_MASK_ISO
;
2706 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2707 & CODING_ISO_FLAG_LATIN_EXTRA
)
2708 found
|= CATEGORY_MASK_ISO_8_1
;
2710 rejected
|= CATEGORY_MASK_ISO_8_1
;
2711 rejected
|= CATEGORY_MASK_ISO_8_2
;
2714 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
2718 detect_info
->rejected
|= rejected
;
2719 detect_info
->found
|= (found
& ~rejected
);
2724 /* Set designation state into CODING. Set CHARS_96 to -1 if the
2725 escape sequence should be kept. */
2726 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2730 if (final < '0' || final >= 128 \
2731 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2732 || !SAFE_CHARSET_P (coding, id)) \
2734 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2738 prev = CODING_ISO_DESIGNATION (coding, reg); \
2739 if (id == charset_jisx0201_roman) \
2741 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2742 id = charset_ascii; \
2744 else if (id == charset_jisx0208_1978) \
2746 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2747 id = charset_jisx0208; \
2749 CODING_ISO_DESIGNATION (coding, reg) = id; \
2750 /* If there was an invalid designation to REG previously, and this \
2751 designation is ASCII to REG, we should keep this designation \
2753 if (prev == -2 && id == charset_ascii) \
2758 #define MAYBE_FINISH_COMPOSITION() \
2761 if (composition_state == COMPOSING_NO) \
2763 /* It is assured that we have enough room for producing \
2764 characters stored in the table `components'. */ \
2765 if (charbuf + component_idx > charbuf_end) \
2766 goto no_more_source; \
2767 composition_state = COMPOSING_NO; \
2768 if (method == COMPOSITION_RELATIVE \
2769 || method == COMPOSITION_WITH_ALTCHARS) \
2771 for (i = 0; i < component_idx; i++) \
2772 *charbuf++ = components[i]; \
2773 char_offset += component_idx; \
2777 for (i = 0; i < component_idx; i += 2) \
2778 *charbuf++ = components[i]; \
2779 char_offset += (component_idx / 2) + 1; \
2784 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2785 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2786 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2787 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2788 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2791 #define DECODE_COMPOSITION_START(c1) \
2794 && composition_state == COMPOSING_COMPONENT_RULE) \
2796 component_len = component_idx; \
2797 composition_state = COMPOSING_CHAR; \
2801 const unsigned char *p; \
2803 MAYBE_FINISH_COMPOSITION (); \
2804 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2805 goto no_more_source; \
2806 for (p = src; p < src_end - 1; p++) \
2807 if (*p == ISO_CODE_ESC && p[1] == '1') \
2809 if (p == src_end - 1) \
2811 if (coding->mode & CODING_MODE_LAST_BLOCK) \
2812 goto invalid_code; \
2813 goto no_more_source; \
2816 /* This is surely the start of a composition. */ \
2817 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2818 : c1 == '2' ? COMPOSITION_WITH_RULE \
2819 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2820 : COMPOSITION_WITH_RULE_ALTCHARS); \
2821 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2822 : COMPOSING_COMPONENT_CHAR); \
2823 component_idx = component_len = 0; \
2828 /* Handle compositoin end sequence ESC 1. */
2830 #define DECODE_COMPOSITION_END() \
2832 int nchars = (component_len > 0 ? component_idx - component_len \
2833 : method == COMPOSITION_RELATIVE ? component_idx \
2834 : (component_idx + 1) / 2); \
2836 int *saved_charbuf = charbuf; \
2838 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2839 if (method != COMPOSITION_RELATIVE) \
2841 if (component_len == 0) \
2842 for (i = 0; i < component_idx; i++) \
2843 *charbuf++ = components[i]; \
2845 for (i = 0; i < component_len; i++) \
2846 *charbuf++ = components[i]; \
2847 *saved_charbuf = saved_charbuf - charbuf; \
2849 if (method == COMPOSITION_WITH_RULE) \
2850 for (i = 0; i < component_idx; i += 2, char_offset++) \
2851 *charbuf++ = components[i]; \
2853 for (i = component_len; i < component_idx; i++, char_offset++) \
2854 *charbuf++ = components[i]; \
2855 coding->annotated = 1; \
2856 composition_state = COMPOSING_NO; \
2860 /* Decode a composition rule from the byte C1 (and maybe one more byte
2861 from SRC) and store one encoded composition rule in
2862 coding->cmp_data. */
2864 #define DECODE_COMPOSITION_RULE(c1) \
2867 if (c1 < 81) /* old format (before ver.21) */ \
2869 int gref = (c1) / 9; \
2870 int nref = (c1) % 9; \
2871 if (gref == 4) gref = 10; \
2872 if (nref == 4) nref = 10; \
2873 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
2875 else if (c1 < 93) /* new format (after ver.21) */ \
2877 ONE_MORE_BYTE (c2); \
2878 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
2885 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2888 decode_coding_iso_2022 (coding
)
2889 struct coding_system
*coding
;
2891 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2892 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2893 const unsigned char *src_base
;
2894 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2896 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
2897 int consumed_chars
= 0, consumed_chars_base
;
2898 int multibytep
= coding
->src_multibyte
;
2899 /* Charsets invoked to graphic plane 0 and 1 respectively. */
2900 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2901 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
2902 int charset_id_2
, charset_id_3
;
2903 struct charset
*charset
;
2905 /* For handling composition sequence. */
2906 #define COMPOSING_NO 0
2907 #define COMPOSING_CHAR 1
2908 #define COMPOSING_RULE 2
2909 #define COMPOSING_COMPONENT_CHAR 3
2910 #define COMPOSING_COMPONENT_RULE 4
2912 int composition_state
= COMPOSING_NO
;
2913 enum composition_method method
;
2914 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
2917 Lisp_Object attrs
, charset_list
;
2918 int char_offset
= coding
->produced_char
;
2919 int last_offset
= char_offset
;
2920 int last_id
= charset_ascii
;
2922 CODING_GET_INFO (coding
, attrs
, charset_list
);
2923 setup_iso_safe_charsets (attrs
);
2930 consumed_chars_base
= consumed_chars
;
2932 if (charbuf
>= charbuf_end
)
2939 /* We produce at most one character. */
2940 switch (iso_code_class
[c1
])
2942 case ISO_0x20_or_0x7F
:
2943 if (composition_state
!= COMPOSING_NO
)
2945 if (composition_state
== COMPOSING_RULE
2946 || composition_state
== COMPOSING_COMPONENT_RULE
)
2948 DECODE_COMPOSITION_RULE (c1
);
2949 components
[component_idx
++] = c1
;
2950 composition_state
--;
2954 if (charset_id_0
< 0
2955 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
2956 /* This is SPACE or DEL. */
2957 charset
= CHARSET_FROM_ID (charset_ascii
);
2959 charset
= CHARSET_FROM_ID (charset_id_0
);
2962 case ISO_graphic_plane_0
:
2963 if (composition_state
!= COMPOSING_NO
)
2965 if (composition_state
== COMPOSING_RULE
2966 || composition_state
== COMPOSING_COMPONENT_RULE
)
2968 DECODE_COMPOSITION_RULE (c1
);
2969 components
[component_idx
++] = c1
;
2970 composition_state
--;
2974 if (charset_id_0
< 0)
2975 charset
= CHARSET_FROM_ID (charset_ascii
);
2977 charset
= CHARSET_FROM_ID (charset_id_0
);
2980 case ISO_0xA0_or_0xFF
:
2981 if (charset_id_1
< 0
2982 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
2983 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
2985 /* This is a graphic character, we fall down ... */
2987 case ISO_graphic_plane_1
:
2988 if (charset_id_1
< 0)
2990 charset
= CHARSET_FROM_ID (charset_id_1
);
2994 MAYBE_FINISH_COMPOSITION ();
2995 charset
= CHARSET_FROM_ID (charset_ascii
);
2999 MAYBE_FINISH_COMPOSITION ();
3003 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3004 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3006 CODING_ISO_INVOCATION (coding
, 0) = 1;
3007 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3011 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3013 CODING_ISO_INVOCATION (coding
, 0) = 0;
3014 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3017 case ISO_single_shift_2_7
:
3018 case ISO_single_shift_2
:
3019 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3021 /* SS2 is handled as an escape sequence of ESC 'N' */
3023 goto label_escape_sequence
;
3025 case ISO_single_shift_3
:
3026 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3028 /* SS2 is handled as an escape sequence of ESC 'O' */
3030 goto label_escape_sequence
;
3032 case ISO_control_sequence_introducer
:
3033 /* CSI is handled as an escape sequence of ESC '[' ... */
3035 goto label_escape_sequence
;
3039 label_escape_sequence
:
3040 /* Escape sequences handled here are invocation,
3041 designation, direction specification, and character
3042 composition specification. */
3045 case '&': /* revision of following character set */
3047 if (!(c1
>= '@' && c1
<= '~'))
3050 if (c1
!= ISO_CODE_ESC
)
3053 goto label_escape_sequence
;
3055 case '$': /* designation of 2-byte character set */
3056 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3062 if (c1
>= '@' && c1
<= 'B')
3063 { /* designation of JISX0208.1978, GB2312.1980,
3065 reg
= 0, chars96
= 0;
3067 else if (c1
>= 0x28 && c1
<= 0x2B)
3068 { /* designation of DIMENSION2_CHARS94 character set */
3069 reg
= c1
- 0x28, chars96
= 0;
3072 else if (c1
>= 0x2C && c1
<= 0x2F)
3073 { /* designation of DIMENSION2_CHARS96 character set */
3074 reg
= c1
- 0x2C, chars96
= 1;
3079 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3080 /* We must update these variables now. */
3082 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3084 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3090 case 'n': /* invocation of locking-shift-2 */
3091 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3092 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3094 CODING_ISO_INVOCATION (coding
, 0) = 2;
3095 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3098 case 'o': /* invocation of locking-shift-3 */
3099 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3100 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3102 CODING_ISO_INVOCATION (coding
, 0) = 3;
3103 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3106 case 'N': /* invocation of single-shift-2 */
3107 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3108 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3110 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3111 if (charset_id_2
< 0)
3112 charset
= CHARSET_FROM_ID (charset_ascii
);
3114 charset
= CHARSET_FROM_ID (charset_id_2
);
3116 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3120 case 'O': /* invocation of single-shift-3 */
3121 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3122 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3124 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3125 if (charset_id_3
< 0)
3126 charset
= CHARSET_FROM_ID (charset_ascii
);
3128 charset
= CHARSET_FROM_ID (charset_id_3
);
3130 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3134 case '0': case '2': case '3': case '4': /* start composition */
3135 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3137 DECODE_COMPOSITION_START (c1
);
3140 case '1': /* end composition */
3141 if (composition_state
== COMPOSING_NO
)
3143 DECODE_COMPOSITION_END ();
3146 case '[': /* specification of direction */
3147 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3149 /* For the moment, nested direction is not supported.
3150 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3151 left-to-right, and nozero means right-to-left. */
3155 case ']': /* end of the current direction */
3156 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3158 case '0': /* end of the current direction */
3159 case '1': /* start of left-to-right direction */
3162 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3167 case '2': /* start of right-to-left direction */
3170 coding
->mode
|= CODING_MODE_DIRECTION
;
3184 /* CTEXT extended segment:
3185 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3186 We keep these bytes as is for the moment.
3187 They may be decoded by post-read-conversion. */
3191 ONE_MORE_BYTE (dim
);
3194 size
= ((M
- 128) * 128) + (L
- 128);
3195 if (charbuf
+ 8 + size
> charbuf_end
)
3197 *charbuf
++ = ISO_CODE_ESC
;
3201 *charbuf
++ = BYTE8_TO_CHAR (M
);
3202 *charbuf
++ = BYTE8_TO_CHAR (L
);
3206 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3211 /* XFree86 extension for embedding UTF-8 in CTEXT:
3212 ESC % G --UTF-8-BYTES-- ESC % @
3213 We keep these bytes as is for the moment.
3214 They may be decoded by post-read-conversion. */
3217 if (p
+ 6 > charbuf_end
)
3219 *p
++ = ISO_CODE_ESC
;
3222 while (p
< charbuf_end
)
3225 if (c1
== ISO_CODE_ESC
3226 && src
+ 1 < src_end
3233 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3235 if (p
+ 3 > charbuf_end
)
3237 *p
++ = ISO_CODE_ESC
;
3248 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3253 if (c1
>= 0x28 && c1
<= 0x2B)
3254 { /* designation of DIMENSION1_CHARS94 character set */
3255 reg
= c1
- 0x28, chars96
= 0;
3258 else if (c1
>= 0x2C && c1
<= 0x2F)
3259 { /* designation of DIMENSION1_CHARS96 character set */
3260 reg
= c1
- 0x2C, chars96
= 1;
3265 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3266 /* We must update these variables now. */
3268 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3270 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3278 if (charset
->id
!= charset_ascii
3279 && last_id
!= charset
->id
)
3281 if (last_id
!= charset_ascii
)
3282 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3283 last_id
= charset
->id
;
3284 last_offset
= char_offset
;
3287 /* Now we know CHARSET and 1st position code C1 of a character.
3288 Produce a decoded character while getting 2nd position code
3291 if (CHARSET_DIMENSION (charset
) > 1)
3294 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3295 /* C2 is not in a valid range. */
3297 c1
= (c1
<< 8) | (c2
& 0x7F);
3298 if (CHARSET_DIMENSION (charset
) > 2)
3301 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3302 /* C2 is not in a valid range. */
3304 c1
= (c1
<< 8) | (c2
& 0x7F);
3308 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3311 MAYBE_FINISH_COMPOSITION ();
3312 for (; src_base
< src
; src_base
++, char_offset
++)
3314 if (ASCII_BYTE_P (*src_base
))
3315 *charbuf
++ = *src_base
;
3317 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3320 else if (composition_state
== COMPOSING_NO
)
3327 components
[component_idx
++] = c
;
3328 if (method
== COMPOSITION_WITH_RULE
3329 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3330 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3331 composition_state
++;
3336 MAYBE_FINISH_COMPOSITION ();
3338 consumed_chars
= consumed_chars_base
;
3340 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3350 if (last_id
!= charset_ascii
)
3351 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3352 coding
->consumed_char
+= consumed_chars_base
;
3353 coding
->consumed
= src_base
- coding
->source
;
3354 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3358 /* ISO2022 encoding stuff. */
3361 It is not enough to say just "ISO2022" on encoding, we have to
3362 specify more details. In Emacs, each coding system of ISO2022
3363 variant has the following specifications:
3364 1. Initial designation to G0 thru G3.
3365 2. Allows short-form designation?
3366 3. ASCII should be designated to G0 before control characters?
3367 4. ASCII should be designated to G0 at end of line?
3368 5. 7-bit environment or 8-bit environment?
3369 6. Use locking-shift?
3370 7. Use Single-shift?
3371 And the following two are only for Japanese:
3372 8. Use ASCII in place of JIS0201-1976-Roman?
3373 9. Use JISX0208-1983 in place of JISX0208-1978?
3374 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3375 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3379 /* Produce codes (escape sequence) for designating CHARSET to graphic
3380 register REG at DST, and increment DST. If <final-char> of CHARSET is
3381 '@', 'A', or 'B' and the coding system CODING allows, produce
3382 designation sequence of short-form. */
3384 #define ENCODE_DESIGNATION(charset, reg, coding) \
3386 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3387 char *intermediate_char_94 = "()*+"; \
3388 char *intermediate_char_96 = ",-./"; \
3389 int revision = -1; \
3392 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3393 revision = CHARSET_ISO_REVISION (charset); \
3395 if (revision >= 0) \
3397 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3398 EMIT_ONE_BYTE ('@' + revision); \
3400 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3401 if (CHARSET_DIMENSION (charset) == 1) \
3403 if (! CHARSET_ISO_CHARS_96 (charset)) \
3404 c = intermediate_char_94[reg]; \
3406 c = intermediate_char_96[reg]; \
3407 EMIT_ONE_ASCII_BYTE (c); \
3411 EMIT_ONE_ASCII_BYTE ('$'); \
3412 if (! CHARSET_ISO_CHARS_96 (charset)) \
3414 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3416 || final_char < '@' || final_char > 'B') \
3417 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3420 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3422 EMIT_ONE_ASCII_BYTE (final_char); \
3424 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3428 /* The following two macros produce codes (control character or escape
3429 sequence) for ISO2022 single-shift functions (single-shift-2 and
3432 #define ENCODE_SINGLE_SHIFT_2 \
3434 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3435 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3437 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3438 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3442 #define ENCODE_SINGLE_SHIFT_3 \
3444 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3445 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3447 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3448 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3452 /* The following four macros produce codes (control character or
3453 escape sequence) for ISO2022 locking-shift functions (shift-in,
3454 shift-out, locking-shift-2, and locking-shift-3). */
3456 #define ENCODE_SHIFT_IN \
3458 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3459 CODING_ISO_INVOCATION (coding, 0) = 0; \
3463 #define ENCODE_SHIFT_OUT \
3465 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3466 CODING_ISO_INVOCATION (coding, 0) = 1; \
3470 #define ENCODE_LOCKING_SHIFT_2 \
3472 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3473 CODING_ISO_INVOCATION (coding, 0) = 2; \
3477 #define ENCODE_LOCKING_SHIFT_3 \
3479 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3480 CODING_ISO_INVOCATION (coding, 0) = 3; \
3484 /* Produce codes for a DIMENSION1 character whose character set is
3485 CHARSET and whose position-code is C1. Designation and invocation
3486 sequences are also produced in advance if necessary. */
3488 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3490 int id = CHARSET_ID (charset); \
3492 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3493 && id == charset_ascii) \
3495 id = charset_jisx0201_roman; \
3496 charset = CHARSET_FROM_ID (id); \
3499 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3501 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3502 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3504 EMIT_ONE_BYTE (c1 | 0x80); \
3505 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3508 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3510 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3513 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3515 EMIT_ONE_BYTE (c1 | 0x80); \
3519 /* Since CHARSET is not yet invoked to any graphic planes, we \
3520 must invoke it, or, at first, designate it to some graphic \
3521 register. Then repeat the loop to actually produce the \
3523 dst = encode_invocation_designation (charset, coding, dst, \
3528 /* Produce codes for a DIMENSION2 character whose character set is
3529 CHARSET and whose position-codes are C1 and C2. Designation and
3530 invocation codes are also produced in advance if necessary. */
3532 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3534 int id = CHARSET_ID (charset); \
3536 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3537 && id == charset_jisx0208) \
3539 id = charset_jisx0208_1978; \
3540 charset = CHARSET_FROM_ID (id); \
3543 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3545 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3546 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3548 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3549 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3552 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3554 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3557 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3559 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3563 /* Since CHARSET is not yet invoked to any graphic planes, we \
3564 must invoke it, or, at first, designate it to some graphic \
3565 register. Then repeat the loop to actually produce the \
3567 dst = encode_invocation_designation (charset, coding, dst, \
3572 #define ENCODE_ISO_CHARACTER(charset, c) \
3574 int code = ENCODE_CHAR ((charset),(c)); \
3576 if (CHARSET_DIMENSION (charset) == 1) \
3577 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3579 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3583 /* Produce designation and invocation codes at a place pointed by DST
3584 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3588 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3589 struct charset
*charset
;
3590 struct coding_system
*coding
;
3594 int multibytep
= coding
->dst_multibyte
;
3595 int produced_chars
= *p_nchars
;
3596 int reg
; /* graphic register number */
3597 int id
= CHARSET_ID (charset
);
3599 /* At first, check designations. */
3600 for (reg
= 0; reg
< 4; reg
++)
3601 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3606 /* CHARSET is not yet designated to any graphic registers. */
3607 /* At first check the requested designation. */
3608 reg
= CODING_ISO_REQUEST (coding
, id
);
3610 /* Since CHARSET requests no special designation, designate it
3611 to graphic register 0. */
3614 ENCODE_DESIGNATION (charset
, reg
, coding
);
3617 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3618 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3620 /* Since the graphic register REG is not invoked to any graphic
3621 planes, invoke it to graphic plane 0. */
3624 case 0: /* graphic register 0 */
3628 case 1: /* graphic register 1 */
3632 case 2: /* graphic register 2 */
3633 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3634 ENCODE_SINGLE_SHIFT_2
;
3636 ENCODE_LOCKING_SHIFT_2
;
3639 case 3: /* graphic register 3 */
3640 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3641 ENCODE_SINGLE_SHIFT_3
;
3643 ENCODE_LOCKING_SHIFT_3
;
3648 *p_nchars
= produced_chars
;
3652 /* The following three macros produce codes for indicating direction
3654 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3656 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3657 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3659 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3663 #define ENCODE_DIRECTION_R2L() \
3665 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3666 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3670 #define ENCODE_DIRECTION_L2R() \
3672 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3673 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3677 /* Produce codes for designation and invocation to reset the graphic
3678 planes and registers to initial state. */
3679 #define ENCODE_RESET_PLANE_AND_REGISTER() \
3682 struct charset *charset; \
3684 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3686 for (reg = 0; reg < 4; reg++) \
3687 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3688 && (CODING_ISO_DESIGNATION (coding, reg) \
3689 != CODING_ISO_INITIAL (coding, reg))) \
3691 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3692 ENCODE_DESIGNATION (charset, reg, coding); \
3697 /* Produce designation sequences of charsets in the line started from
3698 SRC to a place pointed by DST, and return updated DST.
3700 If the current block ends before any end-of-line, we may fail to
3701 find all the necessary designations. */
3703 static unsigned char *
3704 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
3705 struct coding_system
*coding
;
3706 int *charbuf
, *charbuf_end
;
3709 struct charset
*charset
;
3710 /* Table of charsets to be designated to each graphic register. */
3712 int c
, found
= 0, reg
;
3713 int produced_chars
= 0;
3714 int multibytep
= coding
->dst_multibyte
;
3716 Lisp_Object charset_list
;
3718 attrs
= CODING_ID_ATTRS (coding
->id
);
3719 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3720 if (EQ (charset_list
, Qiso_2022
))
3721 charset_list
= Viso_2022_charset_list
;
3723 for (reg
= 0; reg
< 4; reg
++)
3733 charset
= char_charset (c
, charset_list
, NULL
);
3734 id
= CHARSET_ID (charset
);
3735 reg
= CODING_ISO_REQUEST (coding
, id
);
3736 if (reg
>= 0 && r
[reg
] < 0)
3745 for (reg
= 0; reg
< 4; reg
++)
3747 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
3748 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
3754 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3757 encode_coding_iso_2022 (coding
)
3758 struct coding_system
*coding
;
3760 int multibytep
= coding
->dst_multibyte
;
3761 int *charbuf
= coding
->charbuf
;
3762 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
3763 unsigned char *dst
= coding
->destination
+ coding
->produced
;
3764 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
3767 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3768 && CODING_ISO_BOL (coding
));
3769 int produced_chars
= 0;
3770 Lisp_Object attrs
, eol_type
, charset_list
;
3771 int ascii_compatible
;
3773 int preferred_charset_id
= -1;
3775 CODING_GET_INFO (coding
, attrs
, charset_list
);
3776 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
3777 if (VECTORP (eol_type
))
3780 setup_iso_safe_charsets (attrs
);
3781 /* Charset list may have been changed. */
3782 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
); \
3783 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3785 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
3787 while (charbuf
< charbuf_end
)
3789 ASSURE_DESTINATION (safe_room
);
3791 if (bol_designation
)
3793 unsigned char *dst_prev
= dst
;
3795 /* We have to produce designation sequences if any now. */
3796 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
3797 bol_designation
= 0;
3798 /* We are sure that designation sequences are all ASCII bytes. */
3799 produced_chars
+= dst
- dst_prev
;
3806 /* Handle an annotation. */
3809 case CODING_ANNOTATE_COMPOSITION_MASK
:
3810 /* Not yet implemented. */
3812 case CODING_ANNOTATE_CHARSET_MASK
:
3813 preferred_charset_id
= charbuf
[2];
3814 if (preferred_charset_id
>= 0
3815 && NILP (Fmemq (make_number (preferred_charset_id
),
3817 preferred_charset_id
= -1;
3826 /* Now encode the character C. */
3827 if (c
< 0x20 || c
== 0x7F)
3830 || (c
== '\r' && EQ (eol_type
, Qmac
)))
3832 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3833 ENCODE_RESET_PLANE_AND_REGISTER ();
3834 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
3838 for (i
= 0; i
< 4; i
++)
3839 CODING_ISO_DESIGNATION (coding
, i
)
3840 = CODING_ISO_INITIAL (coding
, i
);
3843 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
3845 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
3846 ENCODE_RESET_PLANE_AND_REGISTER ();
3847 EMIT_ONE_ASCII_BYTE (c
);
3849 else if (ASCII_CHAR_P (c
))
3851 if (ascii_compatible
)
3852 EMIT_ONE_ASCII_BYTE (c
);
3855 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
3856 ENCODE_ISO_CHARACTER (charset
, c
);
3859 else if (CHAR_BYTE8_P (c
))
3861 c
= CHAR_TO_BYTE8 (c
);
3866 struct charset
*charset
;
3868 if (preferred_charset_id
>= 0)
3870 charset
= CHARSET_FROM_ID (preferred_charset_id
);
3871 if (! CHAR_CHARSET_P (c
, charset
))
3872 charset
= char_charset (c
, charset_list
, NULL
);
3875 charset
= char_charset (c
, charset_list
, NULL
);
3878 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
3880 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
3881 charset
= CHARSET_FROM_ID (charset_ascii
);
3885 c
= coding
->default_char
;
3886 charset
= char_charset (c
, charset_list
, NULL
);
3889 ENCODE_ISO_CHARACTER (charset
, c
);
3893 if (coding
->mode
& CODING_MODE_LAST_BLOCK
3894 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3896 ASSURE_DESTINATION (safe_room
);
3897 ENCODE_RESET_PLANE_AND_REGISTER ();
3899 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
3900 CODING_ISO_BOL (coding
) = bol_designation
;
3901 coding
->produced_char
+= produced_chars
;
3902 coding
->produced
= dst
- coding
->destination
;
3907 /*** 8,9. SJIS and BIG5 handlers ***/
3909 /* Although SJIS and BIG5 are not ISO's coding system, they are used
3910 quite widely. So, for the moment, Emacs supports them in the bare
3911 C code. But, in the future, they may be supported only by CCL. */
3913 /* SJIS is a coding system encoding three character sets: ASCII, right
3914 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3915 as is. A character of charset katakana-jisx0201 is encoded by
3916 "position-code + 0x80". A character of charset japanese-jisx0208
3917 is encoded in 2-byte but two position-codes are divided and shifted
3918 so that it fit in the range below.
3920 --- CODE RANGE of SJIS ---
3921 (character set) (range)
3923 KATAKANA-JISX0201 0xA0 .. 0xDF
3924 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
3925 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
3926 -------------------------------
3930 /* BIG5 is a coding system encoding two character sets: ASCII and
3931 Big5. An ASCII character is encoded as is. Big5 is a two-byte
3932 character set and is encoded in two-byte.
3934 --- CODE RANGE of BIG5 ---
3935 (character set) (range)
3937 Big5 (1st byte) 0xA1 .. 0xFE
3938 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3939 --------------------------
3943 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3944 Check if a text is encoded in SJIS. If it is, return
3945 CATEGORY_MASK_SJIS, else return 0. */
3948 detect_coding_sjis (coding
, detect_info
)
3949 struct coding_system
*coding
;
3950 struct coding_detection_info
*detect_info
;
3952 const unsigned char *src
= coding
->source
, *src_base
;
3953 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3954 int multibytep
= coding
->src_multibyte
;
3955 int consumed_chars
= 0;
3959 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
3960 /* A coding system of this category is always ASCII compatible. */
3961 src
+= coding
->head_ascii
;
3969 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
3972 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
3974 found
= CATEGORY_MASK_SJIS
;
3976 else if (c
>= 0xA0 && c
< 0xE0)
3977 found
= CATEGORY_MASK_SJIS
;
3981 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
3985 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
3987 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
3990 detect_info
->found
|= found
;
3994 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3995 Check if a text is encoded in BIG5. If it is, return
3996 CATEGORY_MASK_BIG5, else return 0. */
3999 detect_coding_big5 (coding
, detect_info
)
4000 struct coding_system
*coding
;
4001 struct coding_detection_info
*detect_info
;
4003 const unsigned char *src
= coding
->source
, *src_base
;
4004 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4005 int multibytep
= coding
->src_multibyte
;
4006 int consumed_chars
= 0;
4010 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4011 /* A coding system of this category is always ASCII compatible. */
4012 src
+= coding
->head_ascii
;
4023 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4025 found
= CATEGORY_MASK_BIG5
;
4030 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4034 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4036 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4039 detect_info
->found
|= found
;
4043 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4044 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4047 decode_coding_sjis (coding
)
4048 struct coding_system
*coding
;
4050 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4051 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4052 const unsigned char *src_base
;
4053 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4055 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4056 int consumed_chars
= 0, consumed_chars_base
;
4057 int multibytep
= coding
->src_multibyte
;
4058 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4059 struct charset
*charset_kanji2
;
4060 Lisp_Object attrs
, charset_list
, val
;
4061 int char_offset
= coding
->produced_char
;
4062 int last_offset
= char_offset
;
4063 int last_id
= charset_ascii
;
4065 CODING_GET_INFO (coding
, attrs
, charset_list
);
4068 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4069 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4070 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4071 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4076 struct charset
*charset
;
4079 consumed_chars_base
= consumed_chars
;
4081 if (charbuf
>= charbuf_end
)
4088 charset
= charset_roman
;
4089 else if (c
== 0x80 || c
== 0xA0)
4091 else if (c
>= 0xA1 && c
<= 0xDF)
4093 /* SJIS -> JISX0201-Kana */
4095 charset
= charset_kana
;
4099 /* SJIS -> JISX0208 */
4101 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4105 charset
= charset_kanji
;
4107 else if (c
<= 0xFC && charset_kanji2
)
4109 /* SJIS -> JISX0213-2 */
4111 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4115 charset
= charset_kanji2
;
4119 if (charset
->id
!= charset_ascii
4120 && last_id
!= charset
->id
)
4122 if (last_id
!= charset_ascii
)
4123 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4124 last_id
= charset
->id
;
4125 last_offset
= char_offset
;
4127 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4134 consumed_chars
= consumed_chars_base
;
4136 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4142 if (last_id
!= charset_ascii
)
4143 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4144 coding
->consumed_char
+= consumed_chars_base
;
4145 coding
->consumed
= src_base
- coding
->source
;
4146 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4150 decode_coding_big5 (coding
)
4151 struct coding_system
*coding
;
4153 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4154 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4155 const unsigned char *src_base
;
4156 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4158 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4159 int consumed_chars
= 0, consumed_chars_base
;
4160 int multibytep
= coding
->src_multibyte
;
4161 struct charset
*charset_roman
, *charset_big5
;
4162 Lisp_Object attrs
, charset_list
, val
;
4163 int char_offset
= coding
->produced_char
;
4164 int last_offset
= char_offset
;
4165 int last_id
= charset_ascii
;
4167 CODING_GET_INFO (coding
, attrs
, charset_list
);
4169 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4170 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4175 struct charset
*charset
;
4178 consumed_chars_base
= consumed_chars
;
4180 if (charbuf
>= charbuf_end
)
4188 charset
= charset_roman
;
4192 if (c
< 0xA1 || c
> 0xFE)
4195 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4198 charset
= charset_big5
;
4200 if (charset
->id
!= charset_ascii
4201 && last_id
!= charset
->id
)
4203 if (last_id
!= charset_ascii
)
4204 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4205 last_id
= charset
->id
;
4206 last_offset
= char_offset
;
4208 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4215 consumed_chars
= consumed_chars_base
;
4217 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4223 if (last_id
!= charset_ascii
)
4224 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4225 coding
->consumed_char
+= consumed_chars_base
;
4226 coding
->consumed
= src_base
- coding
->source
;
4227 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4230 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4231 This function can encode charsets `ascii', `katakana-jisx0201',
4232 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4233 are sure that all these charsets are registered as official charset
4234 (i.e. do not have extended leading-codes). Characters of other
4235 charsets are produced without any encoding. If SJIS_P is 1, encode
4236 SJIS text, else encode BIG5 text. */
4239 encode_coding_sjis (coding
)
4240 struct coding_system
*coding
;
4242 int multibytep
= coding
->dst_multibyte
;
4243 int *charbuf
= coding
->charbuf
;
4244 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4245 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4246 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4248 int produced_chars
= 0;
4249 Lisp_Object attrs
, charset_list
, val
;
4250 int ascii_compatible
;
4251 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4252 struct charset
*charset_kanji2
;
4255 CODING_GET_INFO (coding
, attrs
, charset_list
);
4257 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4258 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4259 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4260 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4262 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4264 while (charbuf
< charbuf_end
)
4266 ASSURE_DESTINATION (safe_room
);
4268 /* Now encode the character C. */
4269 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4270 EMIT_ONE_ASCII_BYTE (c
);
4271 else if (CHAR_BYTE8_P (c
))
4273 c
= CHAR_TO_BYTE8 (c
);
4279 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4283 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4285 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4286 charset
= CHARSET_FROM_ID (charset_ascii
);
4290 c
= coding
->default_char
;
4291 charset
= char_charset (c
, charset_list
, &code
);
4294 if (code
== CHARSET_INVALID_CODE (charset
))
4296 if (charset
== charset_kanji
)
4300 c1
= code
>> 8, c2
= code
& 0xFF;
4301 EMIT_TWO_BYTES (c1
, c2
);
4303 else if (charset
== charset_kana
)
4304 EMIT_ONE_BYTE (code
| 0x80);
4305 else if (charset_kanji2
&& charset
== charset_kanji2
)
4310 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4311 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4313 JIS_TO_SJIS2 (code
);
4314 c1
= code
>> 8, c2
= code
& 0xFF;
4315 EMIT_TWO_BYTES (c1
, c2
);
4318 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4321 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4324 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4325 coding
->produced_char
+= produced_chars
;
4326 coding
->produced
= dst
- coding
->destination
;
4331 encode_coding_big5 (coding
)
4332 struct coding_system
*coding
;
4334 int multibytep
= coding
->dst_multibyte
;
4335 int *charbuf
= coding
->charbuf
;
4336 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4337 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4338 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4340 int produced_chars
= 0;
4341 Lisp_Object attrs
, charset_list
, val
;
4342 int ascii_compatible
;
4343 struct charset
*charset_roman
, *charset_big5
;
4346 CODING_GET_INFO (coding
, attrs
, charset_list
);
4348 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4349 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4350 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4352 while (charbuf
< charbuf_end
)
4354 ASSURE_DESTINATION (safe_room
);
4356 /* Now encode the character C. */
4357 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4358 EMIT_ONE_ASCII_BYTE (c
);
4359 else if (CHAR_BYTE8_P (c
))
4361 c
= CHAR_TO_BYTE8 (c
);
4367 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4371 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4373 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4374 charset
= CHARSET_FROM_ID (charset_ascii
);
4378 c
= coding
->default_char
;
4379 charset
= char_charset (c
, charset_list
, &code
);
4382 if (code
== CHARSET_INVALID_CODE (charset
))
4384 if (charset
== charset_big5
)
4388 c1
= code
>> 8, c2
= code
& 0xFF;
4389 EMIT_TWO_BYTES (c1
, c2
);
4392 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4395 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4396 coding
->produced_char
+= produced_chars
;
4397 coding
->produced
= dst
- coding
->destination
;
4402 /*** 10. CCL handlers ***/
4404 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4405 Check if a text is encoded in a coding system of which
4406 encoder/decoder are written in CCL program. If it is, return
4407 CATEGORY_MASK_CCL, else return 0. */
4410 detect_coding_ccl (coding
, detect_info
)
4411 struct coding_system
*coding
;
4412 struct coding_detection_info
*detect_info
;
4414 const unsigned char *src
= coding
->source
, *src_base
;
4415 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4416 int multibytep
= coding
->src_multibyte
;
4417 int consumed_chars
= 0;
4419 unsigned char *valids
;
4420 int head_ascii
= coding
->head_ascii
;
4423 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4425 coding
= &coding_categories
[coding_category_ccl
];
4426 valids
= CODING_CCL_VALIDS (coding
);
4427 attrs
= CODING_ID_ATTRS (coding
->id
);
4428 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4437 if (c
< 0 || ! valids
[c
])
4439 if ((valids
[c
] > 1))
4440 found
= CATEGORY_MASK_CCL
;
4442 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4446 detect_info
->found
|= found
;
4451 decode_coding_ccl (coding
)
4452 struct coding_system
*coding
;
4454 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4455 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4456 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4457 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4458 int consumed_chars
= 0;
4459 int multibytep
= coding
->src_multibyte
;
4460 struct ccl_program ccl
;
4461 int source_charbuf
[1024];
4462 int source_byteidx
[1024];
4463 Lisp_Object attrs
, charset_list
;
4465 CODING_GET_INFO (coding
, attrs
, charset_list
);
4466 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4468 while (src
< src_end
)
4470 const unsigned char *p
= src
;
4471 int *source
, *source_end
;
4475 while (i
< 1024 && p
< src_end
)
4477 source_byteidx
[i
] = p
- src
;
4478 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4481 while (i
< 1024 && p
< src_end
)
4482 source_charbuf
[i
++] = *p
++;
4484 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4487 source
= source_charbuf
;
4488 source_end
= source
+ i
;
4489 while (source
< source_end
)
4491 ccl_driver (&ccl
, source
, charbuf
,
4492 source_end
- source
, charbuf_end
- charbuf
,
4494 source
+= ccl
.consumed
;
4495 charbuf
+= ccl
.produced
;
4496 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4499 if (source
< source_end
)
4500 src
+= source_byteidx
[source
- source_charbuf
];
4503 consumed_chars
+= source
- source_charbuf
;
4505 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4506 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4512 case CCL_STAT_SUSPEND_BY_SRC
:
4513 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4515 case CCL_STAT_SUSPEND_BY_DST
:
4518 case CCL_STAT_INVALID_CMD
:
4519 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4522 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4525 coding
->consumed_char
+= consumed_chars
;
4526 coding
->consumed
= src
- coding
->source
;
4527 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4531 encode_coding_ccl (coding
)
4532 struct coding_system
*coding
;
4534 struct ccl_program ccl
;
4535 int multibytep
= coding
->dst_multibyte
;
4536 int *charbuf
= coding
->charbuf
;
4537 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4538 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4539 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4540 unsigned char *adjusted_dst_end
= dst_end
- 1;
4541 int destination_charbuf
[1024];
4542 int i
, produced_chars
= 0;
4543 Lisp_Object attrs
, charset_list
;
4545 CODING_GET_INFO (coding
, attrs
, charset_list
);
4546 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4548 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4549 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4551 while (charbuf
< charbuf_end
&& dst
< adjusted_dst_end
)
4553 int dst_bytes
= dst_end
- dst
;
4554 if (dst_bytes
> 1024)
4557 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4558 charbuf_end
- charbuf
, dst_bytes
, charset_list
);
4559 charbuf
+= ccl
.consumed
;
4561 for (i
= 0; i
< ccl
.produced
; i
++)
4562 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4565 for (i
= 0; i
< ccl
.produced
; i
++)
4566 *dst
++ = destination_charbuf
[i
] & 0xFF;
4567 produced_chars
+= ccl
.produced
;
4573 case CCL_STAT_SUSPEND_BY_SRC
:
4574 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4576 case CCL_STAT_SUSPEND_BY_DST
:
4577 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4580 case CCL_STAT_INVALID_CMD
:
4581 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4584 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4588 coding
->produced_char
+= produced_chars
;
4589 coding
->produced
= dst
- coding
->destination
;
4595 /*** 10, 11. no-conversion handlers ***/
4597 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4600 decode_coding_raw_text (coding
)
4601 struct coding_system
*coding
;
4603 coding
->chars_at_source
= 1;
4604 coding
->consumed_char
= 0;
4605 coding
->consumed
= 0;
4606 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4610 encode_coding_raw_text (coding
)
4611 struct coding_system
*coding
;
4613 int multibytep
= coding
->dst_multibyte
;
4614 int *charbuf
= coding
->charbuf
;
4615 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
4616 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4617 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4618 int produced_chars
= 0;
4623 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4625 if (coding
->src_multibyte
)
4626 while (charbuf
< charbuf_end
)
4628 ASSURE_DESTINATION (safe_room
);
4630 if (ASCII_CHAR_P (c
))
4631 EMIT_ONE_ASCII_BYTE (c
);
4632 else if (CHAR_BYTE8_P (c
))
4634 c
= CHAR_TO_BYTE8 (c
);
4639 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4641 CHAR_STRING_ADVANCE (c
, p1
);
4644 EMIT_ONE_BYTE (*p0
);
4650 while (charbuf
< charbuf_end
)
4652 ASSURE_DESTINATION (safe_room
);
4659 if (coding
->src_multibyte
)
4661 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4663 while (charbuf
< charbuf_end
)
4665 ASSURE_DESTINATION (safe_room
);
4667 if (ASCII_CHAR_P (c
))
4669 else if (CHAR_BYTE8_P (c
))
4670 *dst
++ = CHAR_TO_BYTE8 (c
);
4672 CHAR_STRING_ADVANCE (c
, dst
);
4678 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4679 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4680 *dst
++ = *charbuf
++;
4681 produced_chars
= dst
- (coding
->destination
+ coding
->dst_bytes
);
4684 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4685 coding
->produced_char
+= produced_chars
;
4686 coding
->produced
= dst
- coding
->destination
;
4690 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4691 Check if a text is encoded in a charset-based coding system. If it
4692 is, return 1, else return 0. */
4695 detect_coding_charset (coding
, detect_info
)
4696 struct coding_system
*coding
;
4697 struct coding_detection_info
*detect_info
;
4699 const unsigned char *src
= coding
->source
, *src_base
;
4700 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4701 int multibytep
= coding
->src_multibyte
;
4702 int consumed_chars
= 0;
4703 Lisp_Object attrs
, valids
;
4706 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4708 coding
= &coding_categories
[coding_category_charset
];
4709 attrs
= CODING_ID_ATTRS (coding
->id
);
4710 valids
= AREF (attrs
, coding_attr_charset_valids
);
4712 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4713 src
+= coding
->head_ascii
;
4723 if (NILP (AREF (valids
, c
)))
4726 found
= CATEGORY_MASK_CHARSET
;
4728 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4732 detect_info
->found
|= found
;
4737 decode_coding_charset (coding
)
4738 struct coding_system
*coding
;
4740 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4741 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4742 const unsigned char *src_base
;
4743 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4745 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4746 int consumed_chars
= 0, consumed_chars_base
;
4747 int multibytep
= coding
->src_multibyte
;
4748 Lisp_Object attrs
, charset_list
, valids
;
4749 int char_offset
= coding
->produced_char
;
4750 int last_offset
= char_offset
;
4751 int last_id
= charset_ascii
;
4753 CODING_GET_INFO (coding
, attrs
, charset_list
);
4754 valids
= AREF (attrs
, coding_attr_charset_valids
);
4760 struct charset
*charset
;
4766 consumed_chars_base
= consumed_chars
;
4768 if (charbuf
>= charbuf_end
)
4776 val
= AREF (valids
, c
);
4781 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4782 dim
= CHARSET_DIMENSION (charset
);
4786 code
= (code
<< 8) | c
;
4789 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
4794 /* VAL is a list of charset IDs. It is assured that the
4795 list is sorted by charset dimensions (smaller one
4799 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4800 dim
= CHARSET_DIMENSION (charset
);
4804 code
= (code
<< 8) | c
;
4807 CODING_DECODE_CHAR (coding
, src
, src_base
,
4808 src_end
, charset
, code
, c
);
4816 if (charset
->id
!= charset_ascii
4817 && last_id
!= charset
->id
)
4819 if (last_id
!= charset_ascii
)
4820 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4821 last_id
= charset
->id
;
4822 last_offset
= char_offset
;
4831 consumed_chars
= consumed_chars_base
;
4833 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4839 if (last_id
!= charset_ascii
)
4840 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4841 coding
->consumed_char
+= consumed_chars_base
;
4842 coding
->consumed
= src_base
- coding
->source
;
4843 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4847 encode_coding_charset (coding
)
4848 struct coding_system
*coding
;
4850 int multibytep
= coding
->dst_multibyte
;
4851 int *charbuf
= coding
->charbuf
;
4852 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4853 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4854 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4855 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4856 int produced_chars
= 0;
4857 Lisp_Object attrs
, charset_list
;
4858 int ascii_compatible
;
4861 CODING_GET_INFO (coding
, attrs
, charset_list
);
4862 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4864 while (charbuf
< charbuf_end
)
4866 struct charset
*charset
;
4869 ASSURE_DESTINATION (safe_room
);
4871 if (ascii_compatible
&& ASCII_CHAR_P (c
))
4872 EMIT_ONE_ASCII_BYTE (c
);
4873 else if (CHAR_BYTE8_P (c
))
4875 c
= CHAR_TO_BYTE8 (c
);
4880 charset
= char_charset (c
, charset_list
, &code
);
4883 if (CHARSET_DIMENSION (charset
) == 1)
4884 EMIT_ONE_BYTE (code
);
4885 else if (CHARSET_DIMENSION (charset
) == 2)
4886 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
4887 else if (CHARSET_DIMENSION (charset
) == 3)
4888 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
4890 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
4891 (code
>> 8) & 0xFF, code
& 0xFF);
4895 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4896 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4898 c
= coding
->default_char
;
4904 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4905 coding
->produced_char
+= produced_chars
;
4906 coding
->produced
= dst
- coding
->destination
;
4911 /*** 7. C library functions ***/
4913 /* Setup coding context CODING from information about CODING_SYSTEM.
4914 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4915 CODING_SYSTEM is invalid, signal an error. */
4918 setup_coding_system (coding_system
, coding
)
4919 Lisp_Object coding_system
;
4920 struct coding_system
*coding
;
4923 Lisp_Object eol_type
;
4924 Lisp_Object coding_type
;
4927 if (NILP (coding_system
))
4928 coding_system
= Qundecided
;
4930 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
4932 attrs
= CODING_ID_ATTRS (coding
->id
);
4933 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
4936 coding
->head_ascii
= -1;
4937 coding
->common_flags
4938 = (VECTORP (eol_type
) ? CODING_REQUIRE_DETECTION_MASK
: 0);
4939 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
4940 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
4941 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
4942 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
4943 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
4944 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
4946 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
4947 coding
->max_charset_id
= SCHARS (val
) - 1;
4948 coding
->safe_charsets
= (char *) SDATA (val
);
4949 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
4951 coding_type
= CODING_ATTR_TYPE (attrs
);
4952 if (EQ (coding_type
, Qundecided
))
4954 coding
->detector
= NULL
;
4955 coding
->decoder
= decode_coding_raw_text
;
4956 coding
->encoder
= encode_coding_raw_text
;
4957 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
4959 else if (EQ (coding_type
, Qiso_2022
))
4962 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
4964 /* Invoke graphic register 0 to plane 0. */
4965 CODING_ISO_INVOCATION (coding
, 0) = 0;
4966 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
4967 CODING_ISO_INVOCATION (coding
, 1)
4968 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
4969 /* Setup the initial status of designation. */
4970 for (i
= 0; i
< 4; i
++)
4971 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
4972 /* Not single shifting initially. */
4973 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
4974 /* Beginning of buffer should also be regarded as bol. */
4975 CODING_ISO_BOL (coding
) = 1;
4976 coding
->detector
= detect_coding_iso_2022
;
4977 coding
->decoder
= decode_coding_iso_2022
;
4978 coding
->encoder
= encode_coding_iso_2022
;
4979 if (flags
& CODING_ISO_FLAG_SAFE
)
4980 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
4981 coding
->common_flags
4982 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
4983 | CODING_REQUIRE_FLUSHING_MASK
);
4984 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
4985 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
4986 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
4987 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
4988 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
4990 setup_iso_safe_charsets (attrs
);
4991 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
4992 coding
->max_charset_id
= SCHARS (val
) - 1;
4993 coding
->safe_charsets
= (char *) SDATA (val
);
4995 CODING_ISO_FLAGS (coding
) = flags
;
4997 else if (EQ (coding_type
, Qcharset
))
4999 coding
->detector
= detect_coding_charset
;
5000 coding
->decoder
= decode_coding_charset
;
5001 coding
->encoder
= encode_coding_charset
;
5002 coding
->common_flags
5003 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5005 else if (EQ (coding_type
, Qutf_8
))
5007 coding
->detector
= detect_coding_utf_8
;
5008 coding
->decoder
= decode_coding_utf_8
;
5009 coding
->encoder
= encode_coding_utf_8
;
5010 coding
->common_flags
5011 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5013 else if (EQ (coding_type
, Qutf_16
))
5015 val
= AREF (attrs
, coding_attr_utf_16_bom
);
5016 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
5017 : EQ (val
, Qt
) ? utf_16_with_bom
5018 : utf_16_without_bom
);
5019 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5020 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5021 : utf_16_little_endian
);
5022 CODING_UTF_16_SURROGATE (coding
) = 0;
5023 coding
->detector
= detect_coding_utf_16
;
5024 coding
->decoder
= decode_coding_utf_16
;
5025 coding
->encoder
= encode_coding_utf_16
;
5026 coding
->common_flags
5027 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5028 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
5029 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5031 else if (EQ (coding_type
, Qccl
))
5033 coding
->detector
= detect_coding_ccl
;
5034 coding
->decoder
= decode_coding_ccl
;
5035 coding
->encoder
= encode_coding_ccl
;
5036 coding
->common_flags
5037 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5038 | CODING_REQUIRE_FLUSHING_MASK
);
5040 else if (EQ (coding_type
, Qemacs_mule
))
5042 coding
->detector
= detect_coding_emacs_mule
;
5043 coding
->decoder
= decode_coding_emacs_mule
;
5044 coding
->encoder
= encode_coding_emacs_mule
;
5045 coding
->common_flags
5046 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5047 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5048 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5050 Lisp_Object tail
, safe_charsets
;
5051 int max_charset_id
= 0;
5053 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5055 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5056 max_charset_id
= XFASTINT (XCAR (tail
));
5057 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5059 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5061 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5062 coding
->max_charset_id
= max_charset_id
;
5063 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5066 else if (EQ (coding_type
, Qshift_jis
))
5068 coding
->detector
= detect_coding_sjis
;
5069 coding
->decoder
= decode_coding_sjis
;
5070 coding
->encoder
= encode_coding_sjis
;
5071 coding
->common_flags
5072 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5074 else if (EQ (coding_type
, Qbig5
))
5076 coding
->detector
= detect_coding_big5
;
5077 coding
->decoder
= decode_coding_big5
;
5078 coding
->encoder
= encode_coding_big5
;
5079 coding
->common_flags
5080 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5082 else /* EQ (coding_type, Qraw_text) */
5084 coding
->detector
= NULL
;
5085 coding
->decoder
= decode_coding_raw_text
;
5086 coding
->encoder
= encode_coding_raw_text
;
5087 if (! EQ (eol_type
, Qunix
))
5089 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5090 if (! VECTORP (eol_type
))
5091 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5099 /* Return a list of charsets supported by CODING. */
5102 coding_charset_list (coding
)
5103 struct coding_system
*coding
;
5105 Lisp_Object attrs
, charset_list
;
5107 CODING_GET_INFO (coding
, attrs
, charset_list
);
5108 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5110 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5112 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5113 charset_list
= Viso_2022_charset_list
;
5115 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5117 charset_list
= Vemacs_mule_charset_list
;
5119 return charset_list
;
5123 /* Return raw-text or one of its subsidiaries that has the same
5124 eol_type as CODING-SYSTEM. */
5127 raw_text_coding_system (coding_system
)
5128 Lisp_Object coding_system
;
5130 Lisp_Object spec
, attrs
;
5131 Lisp_Object eol_type
, raw_text_eol_type
;
5133 if (NILP (coding_system
))
5135 spec
= CODING_SYSTEM_SPEC (coding_system
);
5136 attrs
= AREF (spec
, 0);
5138 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5139 return coding_system
;
5141 eol_type
= AREF (spec
, 2);
5142 if (VECTORP (eol_type
))
5144 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5145 raw_text_eol_type
= AREF (spec
, 2);
5146 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5147 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5148 : AREF (raw_text_eol_type
, 2));
5152 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5153 does, return one of the subsidiary that has the same eol-spec as
5154 PARENT. Otherwise, return CODING_SYSTEM. */
5157 coding_inherit_eol_type (coding_system
, parent
)
5158 Lisp_Object coding_system
, parent
;
5160 Lisp_Object spec
, eol_type
;
5162 if (NILP (coding_system
))
5163 coding_system
= Qraw_text
;
5164 spec
= CODING_SYSTEM_SPEC (coding_system
);
5165 eol_type
= AREF (spec
, 2);
5166 if (VECTORP (eol_type
)
5169 Lisp_Object parent_spec
;
5170 Lisp_Object parent_eol_type
;
5173 = CODING_SYSTEM_SPEC (buffer_defaults
.buffer_file_coding_system
);
5174 parent_eol_type
= AREF (parent_spec
, 2);
5175 if (EQ (parent_eol_type
, Qunix
))
5176 coding_system
= AREF (eol_type
, 0);
5177 else if (EQ (parent_eol_type
, Qdos
))
5178 coding_system
= AREF (eol_type
, 1);
5179 else if (EQ (parent_eol_type
, Qmac
))
5180 coding_system
= AREF (eol_type
, 2);
5182 return coding_system
;
5185 /* Emacs has a mechanism to automatically detect a coding system if it
5186 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5187 it's impossible to distinguish some coding systems accurately
5188 because they use the same range of codes. So, at first, coding
5189 systems are categorized into 7, those are:
5191 o coding-category-emacs-mule
5193 The category for a coding system which has the same code range
5194 as Emacs' internal format. Assigned the coding-system (Lisp
5195 symbol) `emacs-mule' by default.
5197 o coding-category-sjis
5199 The category for a coding system which has the same code range
5200 as SJIS. Assigned the coding-system (Lisp
5201 symbol) `japanese-shift-jis' by default.
5203 o coding-category-iso-7
5205 The category for a coding system which has the same code range
5206 as ISO2022 of 7-bit environment. This doesn't use any locking
5207 shift and single shift functions. This can encode/decode all
5208 charsets. Assigned the coding-system (Lisp symbol)
5209 `iso-2022-7bit' by default.
5211 o coding-category-iso-7-tight
5213 Same as coding-category-iso-7 except that this can
5214 encode/decode only the specified charsets.
5216 o coding-category-iso-8-1
5218 The category for a coding system which has the same code range
5219 as ISO2022 of 8-bit environment and graphic plane 1 used only
5220 for DIMENSION1 charset. This doesn't use any locking shift
5221 and single shift functions. Assigned the coding-system (Lisp
5222 symbol) `iso-latin-1' by default.
5224 o coding-category-iso-8-2
5226 The category for a coding system which has the same code range
5227 as ISO2022 of 8-bit environment and graphic plane 1 used only
5228 for DIMENSION2 charset. This doesn't use any locking shift
5229 and single shift functions. Assigned the coding-system (Lisp
5230 symbol) `japanese-iso-8bit' by default.
5232 o coding-category-iso-7-else
5234 The category for a coding system which has the same code range
5235 as ISO2022 of 7-bit environemnt but uses locking shift or
5236 single shift functions. Assigned the coding-system (Lisp
5237 symbol) `iso-2022-7bit-lock' by default.
5239 o coding-category-iso-8-else
5241 The category for a coding system which has the same code range
5242 as ISO2022 of 8-bit environemnt but uses locking shift or
5243 single shift functions. Assigned the coding-system (Lisp
5244 symbol) `iso-2022-8bit-ss2' by default.
5246 o coding-category-big5
5248 The category for a coding system which has the same code range
5249 as BIG5. Assigned the coding-system (Lisp symbol)
5250 `cn-big5' by default.
5252 o coding-category-utf-8
5254 The category for a coding system which has the same code range
5255 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5256 symbol) `utf-8' by default.
5258 o coding-category-utf-16-be
5260 The category for a coding system in which a text has an
5261 Unicode signature (cf. Unicode Standard) in the order of BIG
5262 endian at the head. Assigned the coding-system (Lisp symbol)
5263 `utf-16-be' by default.
5265 o coding-category-utf-16-le
5267 The category for a coding system in which a text has an
5268 Unicode signature (cf. Unicode Standard) in the order of
5269 LITTLE endian at the head. Assigned the coding-system (Lisp
5270 symbol) `utf-16-le' by default.
5272 o coding-category-ccl
5274 The category for a coding system of which encoder/decoder is
5275 written in CCL programs. The default value is nil, i.e., no
5276 coding system is assigned.
5278 o coding-category-binary
5280 The category for a coding system not categorized in any of the
5281 above. Assigned the coding-system (Lisp symbol)
5282 `no-conversion' by default.
5284 Each of them is a Lisp symbol and the value is an actual
5285 `coding-system's (this is also a Lisp symbol) assigned by a user.
5286 What Emacs does actually is to detect a category of coding system.
5287 Then, it uses a `coding-system' assigned to it. If Emacs can't
5288 decide only one possible category, it selects a category of the
5289 highest priority. Priorities of categories are also specified by a
5290 user in a Lisp variable `coding-category-list'.
5294 #define EOL_SEEN_NONE 0
5295 #define EOL_SEEN_LF 1
5296 #define EOL_SEEN_CR 2
5297 #define EOL_SEEN_CRLF 4
5299 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5300 SOURCE is encoded. If CATEGORY is one of
5301 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5302 two-byte, else they are encoded by one-byte.
5304 Return one of EOL_SEEN_XXX. */
5306 #define MAX_EOL_CHECK_COUNT 3
5309 detect_eol (source
, src_bytes
, category
)
5310 const unsigned char *source
;
5311 EMACS_INT src_bytes
;
5312 enum coding_category category
;
5314 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5317 int eol_seen
= EOL_SEEN_NONE
;
5319 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5323 msb
= category
== (coding_category_utf_16_le
5324 | coding_category_utf_16_le_nosig
);
5327 while (src
+ 1 < src_end
)
5330 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5335 this_eol
= EOL_SEEN_LF
;
5336 else if (src
+ 3 >= src_end
5337 || src
[msb
+ 2] != 0
5338 || src
[lsb
+ 2] != '\n')
5339 this_eol
= EOL_SEEN_CR
;
5341 this_eol
= EOL_SEEN_CRLF
;
5343 if (eol_seen
== EOL_SEEN_NONE
)
5344 /* This is the first end-of-line. */
5345 eol_seen
= this_eol
;
5346 else if (eol_seen
!= this_eol
)
5348 /* The found type is different from what found before. */
5349 eol_seen
= EOL_SEEN_LF
;
5352 if (++total
== MAX_EOL_CHECK_COUNT
)
5360 while (src
< src_end
)
5363 if (c
== '\n' || c
== '\r')
5368 this_eol
= EOL_SEEN_LF
;
5369 else if (src
>= src_end
|| *src
!= '\n')
5370 this_eol
= EOL_SEEN_CR
;
5372 this_eol
= EOL_SEEN_CRLF
, src
++;
5374 if (eol_seen
== EOL_SEEN_NONE
)
5375 /* This is the first end-of-line. */
5376 eol_seen
= this_eol
;
5377 else if (eol_seen
!= this_eol
)
5379 /* The found type is different from what found before. */
5380 eol_seen
= EOL_SEEN_LF
;
5383 if (++total
== MAX_EOL_CHECK_COUNT
)
5393 adjust_coding_eol_type (coding
, eol_seen
)
5394 struct coding_system
*coding
;
5397 Lisp_Object eol_type
;
5399 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5400 if (eol_seen
& EOL_SEEN_LF
)
5402 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5405 else if (eol_seen
& EOL_SEEN_CRLF
)
5407 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5410 else if (eol_seen
& EOL_SEEN_CR
)
5412 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5418 /* Detect how a text specified in CODING is encoded. If a coding
5419 system is detected, update fields of CODING by the detected coding
5423 detect_coding (coding
)
5424 struct coding_system
*coding
;
5426 const unsigned char *src
, *src_end
;
5428 coding
->consumed
= coding
->consumed_char
= 0;
5429 coding
->produced
= coding
->produced_char
= 0;
5430 coding_set_source (coding
);
5432 src_end
= coding
->source
+ coding
->src_bytes
;
5434 /* If we have not yet decided the text encoding type, detect it
5436 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5439 struct coding_detection_info detect_info
;
5441 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5442 for (i
= 0, src
= coding
->source
; src
< src_end
; i
++, src
++)
5448 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5449 && ! inhibit_iso_escape_detection
5450 && ! detect_info
.checked
)
5452 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5453 if (detect_coding_iso_2022 (coding
, &detect_info
))
5455 /* We have scanned the whole data. */
5456 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5457 /* We didn't find an 8-bit code. */
5463 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5465 if (coding
->head_ascii
< coding
->src_bytes
5466 || detect_info
.found
)
5468 enum coding_category category
;
5469 struct coding_system
*this;
5471 if (coding
->head_ascii
== coding
->src_bytes
)
5472 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5473 for (i
= 0; i
< coding_category_raw_text
; i
++)
5475 category
= coding_priorities
[i
];
5476 this = coding_categories
+ category
;
5477 if (detect_info
.found
& (1 << category
))
5481 for (i
= 0; i
< coding_category_raw_text
; i
++)
5483 category
= coding_priorities
[i
];
5484 this = coding_categories
+ category
;
5487 /* No coding system of this category is defined. */
5488 detect_info
.rejected
|= (1 << category
);
5490 else if (category
>= coding_category_raw_text
)
5492 else if (detect_info
.checked
& (1 << category
))
5494 if (detect_info
.found
& (1 << category
))
5497 else if ((*(this->detector
)) (coding
, &detect_info
)
5498 && detect_info
.found
& (1 << category
))
5500 if (category
== coding_category_utf_16_auto
)
5502 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5503 category
= coding_category_utf_16_le
;
5505 category
= coding_category_utf_16_be
;
5511 if (i
< coding_category_raw_text
)
5512 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5513 else if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
5514 setup_coding_system (Qraw_text
, coding
);
5515 else if (detect_info
.rejected
)
5516 for (i
= 0; i
< coding_category_raw_text
; i
++)
5517 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5519 this = coding_categories
+ coding_priorities
[i
];
5520 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5525 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5526 == coding_category_utf_16_auto
)
5528 Lisp_Object coding_systems
;
5529 struct coding_detection_info detect_info
;
5532 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5533 detect_info
.found
= detect_info
.rejected
= 0;
5534 if (CONSP (coding_systems
)
5535 && detect_coding_utf_16 (coding
, &detect_info
))
5537 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5538 setup_coding_system (XCAR (coding_systems
), coding
);
5539 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5540 setup_coding_system (XCDR (coding_systems
), coding
);
5548 struct coding_system
*coding
;
5550 Lisp_Object eol_type
;
5551 unsigned char *p
, *pbeg
, *pend
;
5553 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5554 if (EQ (eol_type
, Qunix
))
5557 if (NILP (coding
->dst_object
))
5558 pbeg
= coding
->destination
;
5560 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5561 pend
= pbeg
+ coding
->produced
;
5563 if (VECTORP (eol_type
))
5565 int eol_seen
= EOL_SEEN_NONE
;
5567 for (p
= pbeg
; p
< pend
; p
++)
5570 eol_seen
|= EOL_SEEN_LF
;
5571 else if (*p
== '\r')
5573 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5575 eol_seen
|= EOL_SEEN_CRLF
;
5579 eol_seen
|= EOL_SEEN_CR
;
5582 if (eol_seen
!= EOL_SEEN_NONE
5583 && eol_seen
!= EOL_SEEN_LF
5584 && eol_seen
!= EOL_SEEN_CRLF
5585 && eol_seen
!= EOL_SEEN_CR
)
5586 eol_seen
= EOL_SEEN_LF
;
5587 if (eol_seen
!= EOL_SEEN_NONE
)
5588 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5591 if (EQ (eol_type
, Qmac
))
5593 for (p
= pbeg
; p
< pend
; p
++)
5597 else if (EQ (eol_type
, Qdos
))
5601 if (NILP (coding
->dst_object
))
5603 for (p
= pend
- 2; p
>= pbeg
; p
--)
5606 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5612 for (p
= pend
- 2; p
>= pbeg
; p
--)
5615 int pos_byte
= coding
->dst_pos_byte
+ (p
- pbeg
);
5616 int pos
= BYTE_TO_CHAR (pos_byte
);
5618 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5622 coding
->produced
-= n
;
5623 coding
->produced_char
-= n
;
5628 /* Return a translation table (or list of them) from coding system
5629 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5630 decoding (ENCODEP is zero). */
5633 get_translation_table (attrs
, encodep
, max_lookup
)
5635 int encodep
, *max_lookup
;
5637 Lisp_Object standard
, translation_table
;
5641 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5642 standard
= Vstandard_translation_table_for_encode
;
5644 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5645 standard
= Vstandard_translation_table_for_decode
;
5646 if (NILP (translation_table
))
5647 translation_table
= standard
;
5650 if (SYMBOLP (translation_table
))
5651 translation_table
= Fget (translation_table
, Qtranslation_table
);
5652 else if (CONSP (translation_table
))
5654 translation_table
= Fcopy_sequence (translation_table
);
5655 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5656 if (SYMBOLP (XCAR (val
)))
5657 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5659 if (CHAR_TABLE_P (standard
))
5661 if (CONSP (translation_table
))
5662 translation_table
= nconc2 (translation_table
,
5663 Fcons (standard
, Qnil
));
5665 translation_table
= Fcons (translation_table
,
5666 Fcons (standard
, Qnil
));
5673 if (CHAR_TABLE_P (translation_table
)
5674 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5676 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5677 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5678 *max_lookup
= XFASTINT (val
);
5680 else if (CONSP (translation_table
))
5682 Lisp_Object tail
, val
;
5684 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
5685 if (CHAR_TABLE_P (XCAR (tail
))
5686 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
5688 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
5689 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5690 *max_lookup
= XFASTINT (val
);
5694 return translation_table
;
5697 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5700 if (CHAR_TABLE_P (table)) \
5702 trans = CHAR_TABLE_REF (table, c); \
5703 if (CHARACTERP (trans)) \
5704 c = XFASTINT (trans), trans = Qnil; \
5706 else if (CONSP (table)) \
5710 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5711 if (CHAR_TABLE_P (XCAR (tail))) \
5713 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5714 if (CHARACTERP (trans)) \
5715 c = XFASTINT (trans), trans = Qnil; \
5716 else if (! NILP (trans)) \
5724 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
5728 int *from_nchars
, *to_nchars
;
5730 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5734 Lisp_Object from
, tail
;
5737 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
5742 for (i
= 0; i
< len
; i
++)
5744 if (buf
+ i
== buf_end
)
5750 if (XINT (AREF (from
, i
)) != buf
[i
])
5764 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
5772 produce_chars (coding
, translation_table
, last_block
)
5773 struct coding_system
*coding
;
5774 Lisp_Object translation_table
;
5777 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5778 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5780 int produced_chars
= 0;
5783 if (! coding
->chars_at_source
)
5785 /* Characters are in coding->charbuf. */
5786 int *buf
= coding
->charbuf
;
5787 int *buf_end
= buf
+ coding
->charbuf_used
;
5789 if (BUFFERP (coding
->src_object
)
5790 && EQ (coding
->src_object
, coding
->dst_object
))
5791 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
5793 while (buf
< buf_end
)
5799 int from_nchars
= 1, to_nchars
= 1;
5800 Lisp_Object trans
= Qnil
;
5802 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
5805 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
5806 &from_nchars
, &to_nchars
);
5812 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
5814 dst
= alloc_destination (coding
,
5816 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
5818 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5821 for (i
= 0; i
< to_nchars
; i
++)
5824 c
= XINT (AREF (trans
, i
));
5825 if (coding
->dst_multibyte
5826 || ! CHAR_BYTE8_P (c
))
5827 CHAR_STRING_ADVANCE (c
, dst
);
5829 *dst
++ = CHAR_TO_BYTE8 (c
);
5831 produced_chars
+= to_nchars
;
5833 while (--from_nchars
> 0)
5837 /* This is an annotation datum. (-C) is the length. */
5840 carryover
= buf_end
- buf
;
5844 const unsigned char *src
= coding
->source
;
5845 const unsigned char *src_end
= src
+ coding
->src_bytes
;
5846 Lisp_Object eol_type
;
5848 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5850 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
5852 if (coding
->src_multibyte
)
5859 const unsigned char *src_base
= src
;
5865 if (EQ (eol_type
, Qdos
))
5869 record_conversion_result
5870 (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5871 goto no_more_source
;
5876 else if (EQ (eol_type
, Qmac
))
5881 coding
->consumed
= src
- coding
->source
;
5883 if (EQ (coding
->src_object
, coding
->dst_object
))
5884 dst_end
= (unsigned char *) src
;
5887 dst
= alloc_destination (coding
, src_end
- src
+ 1,
5889 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5890 coding_set_source (coding
);
5891 src
= coding
->source
+ coding
->consumed
;
5892 src_end
= coding
->source
+ coding
->src_bytes
;
5902 while (src
< src_end
)
5909 if (EQ (eol_type
, Qdos
))
5915 else if (EQ (eol_type
, Qmac
))
5918 if (dst
>= dst_end
- 1)
5920 coding
->consumed
= src
- coding
->source
;
5922 if (EQ (coding
->src_object
, coding
->dst_object
))
5923 dst_end
= (unsigned char *) src
;
5924 if (dst
>= dst_end
- 1)
5926 dst
= alloc_destination (coding
, src_end
- src
+ 2,
5928 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5929 coding_set_source (coding
);
5930 src
= coding
->source
+ coding
->consumed
;
5931 src_end
= coding
->source
+ coding
->src_bytes
;
5939 if (!EQ (coding
->src_object
, coding
->dst_object
))
5941 int require
= coding
->src_bytes
- coding
->dst_bytes
;
5945 EMACS_INT offset
= src
- coding
->source
;
5947 dst
= alloc_destination (coding
, require
, dst
);
5948 coding_set_source (coding
);
5949 src
= coding
->source
+ offset
;
5950 src_end
= coding
->source
+ coding
->src_bytes
;
5953 produced_chars
= coding
->src_chars
;
5954 while (src
< src_end
)
5960 if (EQ (eol_type
, Qdos
))
5967 else if (EQ (eol_type
, Qmac
))
5973 coding
->consumed
= coding
->src_bytes
;
5974 coding
->consumed_char
= coding
->src_chars
;
5977 produced
= dst
- (coding
->destination
+ coding
->produced
);
5978 if (BUFFERP (coding
->dst_object
))
5979 insert_from_gap (produced_chars
, produced
);
5980 coding
->produced
+= produced
;
5981 coding
->produced_char
+= produced_chars
;
5985 /* Compose text in CODING->object according to the annotation data at
5986 CHARBUF. CHARBUF is an array:
5987 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
5991 produce_composition (coding
, charbuf
, pos
)
5992 struct coding_system
*coding
;
5998 enum composition_method method
;
5999 Lisp_Object components
;
6002 to
= pos
+ charbuf
[2];
6005 method
= (enum composition_method
) (charbuf
[3]);
6007 if (method
== COMPOSITION_RELATIVE
)
6009 else if (method
>= COMPOSITION_WITH_RULE
6010 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6012 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6017 for (i
= 0; i
< len
; i
++)
6019 args
[i
] = make_number (charbuf
[i
]);
6023 components
= (method
== COMPOSITION_WITH_ALTCHARS
6024 ? Fstring (len
, args
) : Fvector (len
, args
));
6028 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6032 /* Put `charset' property on text in CODING->object according to
6033 the annotation data at CHARBUF. CHARBUF is an array:
6034 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6038 produce_charset (coding
, charbuf
, pos
)
6039 struct coding_system
*coding
;
6043 EMACS_INT from
= pos
- charbuf
[2];
6044 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6046 Fput_text_property (make_number (from
), make_number (pos
),
6047 Qcharset
, CHARSET_NAME (charset
),
6048 coding
->dst_object
);
6052 #define CHARBUF_SIZE 0x4000
6054 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6056 int size = CHARBUF_SIZE;; \
6058 coding->charbuf = NULL; \
6059 while (size > 1024) \
6061 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6062 if (coding->charbuf) \
6066 if (! coding->charbuf) \
6068 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6069 return coding->result; \
6071 coding->charbuf_size = size; \
6076 produce_annotation (coding
, pos
)
6077 struct coding_system
*coding
;
6080 int *charbuf
= coding
->charbuf
;
6081 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6083 if (NILP (coding
->dst_object
))
6086 while (charbuf
< charbuf_end
)
6092 int len
= -*charbuf
;
6095 case CODING_ANNOTATE_COMPOSITION_MASK
:
6096 produce_composition (coding
, charbuf
, pos
);
6098 case CODING_ANNOTATE_CHARSET_MASK
:
6099 produce_charset (coding
, charbuf
, pos
);
6109 /* Decode the data at CODING->src_object into CODING->dst_object.
6110 CODING->src_object is a buffer, a string, or nil.
6111 CODING->dst_object is a buffer.
6113 If CODING->src_object is a buffer, it must be the current buffer.
6114 In this case, if CODING->src_pos is positive, it is a position of
6115 the source text in the buffer, otherwise, the source text is in the
6116 gap area of the buffer, and CODING->src_pos specifies the offset of
6117 the text from GPT (which must be the same as PT). If this is the
6118 same buffer as CODING->dst_object, CODING->src_pos must be
6121 If CODING->src_object is a string, CODING->src_pos in an index to
6124 If CODING->src_object is nil, CODING->source must already point to
6125 the non-relocatable memory area. In this case, CODING->src_pos is
6126 an offset from CODING->source.
6128 The decoded data is inserted at the current point of the buffer
6133 decode_coding (coding
)
6134 struct coding_system
*coding
;
6137 Lisp_Object undo_list
;
6138 Lisp_Object translation_table
;
6142 if (BUFFERP (coding
->src_object
)
6143 && coding
->src_pos
> 0
6144 && coding
->src_pos
< GPT
6145 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6146 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6149 if (BUFFERP (coding
->dst_object
))
6151 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6152 set_buffer_internal (XBUFFER (coding
->dst_object
));
6154 move_gap_both (PT
, PT_BYTE
);
6155 undo_list
= current_buffer
->undo_list
;
6156 current_buffer
->undo_list
= Qt
;
6159 coding
->consumed
= coding
->consumed_char
= 0;
6160 coding
->produced
= coding
->produced_char
= 0;
6161 coding
->chars_at_source
= 0;
6162 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6165 ALLOC_CONVERSION_WORK_AREA (coding
);
6167 attrs
= CODING_ID_ATTRS (coding
->id
);
6168 translation_table
= get_translation_table (attrs
, 0, NULL
);
6173 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6175 coding_set_source (coding
);
6176 coding
->annotated
= 0;
6177 coding
->charbuf_used
= carryover
;
6178 (*(coding
->decoder
)) (coding
);
6179 coding_set_destination (coding
);
6180 carryover
= produce_chars (coding
, translation_table
, 0);
6181 if (coding
->annotated
)
6182 produce_annotation (coding
, pos
);
6183 for (i
= 0; i
< carryover
; i
++)
6185 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6187 while (coding
->consumed
< coding
->src_bytes
6188 && (coding
->result
== CODING_RESULT_SUCCESS
6189 || coding
->result
== CODING_RESULT_INVALID_SRC
));
6193 coding_set_destination (coding
);
6194 coding
->charbuf_used
= carryover
;
6195 produce_chars (coding
, translation_table
, 1);
6198 coding
->carryover_bytes
= 0;
6199 if (coding
->consumed
< coding
->src_bytes
)
6201 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6202 const unsigned char *src
;
6204 coding_set_source (coding
);
6205 coding_set_destination (coding
);
6206 src
= coding
->source
+ coding
->consumed
;
6208 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6210 /* Flush out unprocessed data as binary chars. We are sure
6211 that the number of data is less than the size of
6213 coding
->charbuf_used
= 0;
6214 while (nbytes
-- > 0)
6219 c
= BYTE8_TO_CHAR (c
);
6220 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6222 produce_chars (coding
, Qnil
, 1);
6226 /* Record unprocessed bytes in coding->carryover. We are
6227 sure that the number of data is less than the size of
6228 coding->carryover. */
6229 unsigned char *p
= coding
->carryover
;
6231 coding
->carryover_bytes
= nbytes
;
6232 while (nbytes
-- > 0)
6235 coding
->consumed
= coding
->src_bytes
;
6238 if (BUFFERP (coding
->dst_object
))
6240 current_buffer
->undo_list
= undo_list
;
6241 record_insert (coding
->dst_pos
, coding
->produced_char
);
6243 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6244 decode_eol (coding
);
6245 return coding
->result
;
6249 /* Extract an annotation datum from a composition starting at POS and
6250 ending before LIMIT of CODING->src_object (buffer or string), store
6251 the data in BUF, set *STOP to a starting position of the next
6252 composition (if any) or to LIMIT, and return the address of the
6253 next element of BUF.
6255 If such an annotation is not found, set *STOP to a starting
6256 position of a composition after POS (if any) or to LIMIT, and
6260 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6261 EMACS_INT pos
, limit
;
6262 struct coding_system
*coding
;
6266 EMACS_INT start
, end
;
6269 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6272 else if (start
> pos
)
6278 /* We found a composition. Store the corresponding
6279 annotation data in BUF. */
6281 enum composition_method method
= COMPOSITION_METHOD (prop
);
6282 int nchars
= COMPOSITION_LENGTH (prop
);
6284 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6285 if (method
!= COMPOSITION_RELATIVE
)
6287 Lisp_Object components
;
6290 components
= COMPOSITION_COMPONENTS (prop
);
6291 if (VECTORP (components
))
6293 len
= XVECTOR (components
)->size
;
6294 for (i
= 0; i
< len
; i
++)
6295 *buf
++ = XINT (AREF (components
, i
));
6297 else if (STRINGP (components
))
6299 len
= SCHARS (components
);
6303 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6307 else if (INTEGERP (components
))
6310 *buf
++ = XINT (components
);
6312 else if (CONSP (components
))
6314 for (len
= 0; CONSP (components
);
6315 len
++, components
= XCDR (components
))
6316 *buf
++ = XINT (XCAR (components
));
6324 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6335 /* Extract an annotation datum from a text property `charset' at POS of
6336 CODING->src_object (buffer of string), store the data in BUF, set
6337 *STOP to the position where the value of `charset' property changes
6338 (limiting by LIMIT), and return the address of the next element of
6341 If the property value is nil, set *STOP to the position where the
6342 property value is non-nil (limiting by LIMIT), and return BUF. */
6345 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6346 EMACS_INT pos
, limit
;
6347 struct coding_system
*coding
;
6351 Lisp_Object val
, next
;
6354 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6355 if (! NILP (val
) && CHARSETP (val
))
6356 id
= XINT (CHARSET_SYMBOL_ID (val
));
6359 ADD_CHARSET_DATA (buf
, 0, id
);
6360 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6362 make_number (limit
));
6363 *stop
= XINT (next
);
6369 consume_chars (coding
, translation_table
, max_lookup
)
6370 struct coding_system
*coding
;
6371 Lisp_Object translation_table
;
6374 int *buf
= coding
->charbuf
;
6375 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6376 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6377 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6378 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6379 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6380 int multibytep
= coding
->src_multibyte
;
6381 Lisp_Object eol_type
;
6383 EMACS_INT stop
, stop_composition
, stop_charset
;
6384 int *lookup_buf
= NULL
;
6386 if (! NILP (translation_table
))
6387 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6389 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6390 if (VECTORP (eol_type
))
6393 /* Note: composition handling is not yet implemented. */
6394 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6396 if (NILP (coding
->src_object
))
6397 stop
= stop_composition
= stop_charset
= end_pos
;
6400 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6401 stop
= stop_composition
= pos
;
6403 stop
= stop_composition
= end_pos
;
6404 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6405 stop
= stop_charset
= pos
;
6407 stop_charset
= end_pos
;
6410 /* Compensate for CRLF and conversion. */
6411 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6412 while (buf
< buf_end
)
6420 if (pos
== stop_composition
)
6421 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6422 buf
, &stop_composition
);
6423 if (pos
== stop_charset
)
6424 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6425 buf
, &stop_charset
);
6426 stop
= (stop_composition
< stop_charset
6427 ? stop_composition
: stop_charset
);
6434 if (coding
->encoder
== encode_coding_raw_text
)
6436 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6437 c
= STRING_CHAR_ADVANCE (src
), pos
+= bytes
;
6439 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
6442 c
= STRING_CHAR_ADVANCE (src
), pos
++;
6443 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6445 if (! EQ (eol_type
, Qunix
))
6449 if (EQ (eol_type
, Qdos
))
6457 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6462 int from_nchars
= 1, to_nchars
= 1;
6463 int *lookup_buf_end
;
6464 const unsigned char *p
= src
;
6468 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6469 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6470 lookup_buf_end
= lookup_buf
+ i
;
6471 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6472 &from_nchars
, &to_nchars
);
6474 || buf
+ to_nchars
> buf_end
)
6476 *buf
++ = *lookup_buf
;
6477 for (i
= 1; i
< to_nchars
; i
++)
6478 *buf
++ = XINT (AREF (trans
, i
));
6479 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6480 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6484 coding
->consumed
= src
- coding
->source
;
6485 coding
->consumed_char
= pos
- coding
->src_pos
;
6486 coding
->charbuf_used
= buf
- coding
->charbuf
;
6487 coding
->chars_at_source
= 0;
6491 /* Encode the text at CODING->src_object into CODING->dst_object.
6492 CODING->src_object is a buffer or a string.
6493 CODING->dst_object is a buffer or nil.
6495 If CODING->src_object is a buffer, it must be the current buffer.
6496 In this case, if CODING->src_pos is positive, it is a position of
6497 the source text in the buffer, otherwise. the source text is in the
6498 gap area of the buffer, and coding->src_pos specifies the offset of
6499 the text from GPT (which must be the same as PT). If this is the
6500 same buffer as CODING->dst_object, CODING->src_pos must be
6501 negative and CODING should not have `pre-write-conversion'.
6503 If CODING->src_object is a string, CODING should not have
6504 `pre-write-conversion'.
6506 If CODING->dst_object is a buffer, the encoded data is inserted at
6507 the current point of that buffer.
6509 If CODING->dst_object is nil, the encoded data is placed at the
6510 memory area specified by CODING->destination. */
6513 encode_coding (coding
)
6514 struct coding_system
*coding
;
6517 Lisp_Object translation_table
;
6520 attrs
= CODING_ID_ATTRS (coding
->id
);
6521 if (coding
->encoder
== encode_coding_raw_text
)
6522 translation_table
= Qnil
, max_lookup
= 0;
6524 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6526 if (BUFFERP (coding
->dst_object
))
6528 set_buffer_internal (XBUFFER (coding
->dst_object
));
6529 coding
->dst_multibyte
6530 = ! NILP (current_buffer
->enable_multibyte_characters
);
6533 coding
->consumed
= coding
->consumed_char
= 0;
6534 coding
->produced
= coding
->produced_char
= 0;
6535 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6538 ALLOC_CONVERSION_WORK_AREA (coding
);
6541 coding_set_source (coding
);
6542 consume_chars (coding
, translation_table
, max_lookup
);
6543 coding_set_destination (coding
);
6544 (*(coding
->encoder
)) (coding
);
6545 } while (coding
->consumed_char
< coding
->src_chars
);
6547 if (BUFFERP (coding
->dst_object
))
6548 insert_from_gap (coding
->produced_char
, coding
->produced
);
6550 return (coding
->result
);
6554 /* Name (or base name) of work buffer for code conversion. */
6555 static Lisp_Object Vcode_conversion_workbuf_name
;
6557 /* A working buffer used by the top level conversion. Once it is
6558 created, it is never destroyed. It has the name
6559 Vcode_conversion_workbuf_name. The other working buffers are
6560 destroyed after the use is finished, and their names are modified
6561 versions of Vcode_conversion_workbuf_name. */
6562 static Lisp_Object Vcode_conversion_reused_workbuf
;
6564 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6565 static int reused_workbuf_in_use
;
6568 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6569 multibyteness of returning buffer. */
6572 make_conversion_work_buffer (multibyte
)
6575 Lisp_Object name
, workbuf
;
6576 struct buffer
*current
;
6578 if (reused_workbuf_in_use
++)
6580 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6581 workbuf
= Fget_buffer_create (name
);
6585 name
= Vcode_conversion_workbuf_name
;
6586 workbuf
= Fget_buffer_create (name
);
6587 if (NILP (Vcode_conversion_reused_workbuf
))
6588 Vcode_conversion_reused_workbuf
= workbuf
;
6590 current
= current_buffer
;
6591 set_buffer_internal (XBUFFER (workbuf
));
6593 current_buffer
->undo_list
= Qt
;
6594 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6595 set_buffer_internal (current
);
6601 code_conversion_restore (arg
)
6604 Lisp_Object current
, workbuf
;
6605 struct gcpro gcpro1
;
6608 current
= XCAR (arg
);
6609 workbuf
= XCDR (arg
);
6610 if (! NILP (workbuf
))
6612 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6613 reused_workbuf_in_use
= 0;
6614 else if (! NILP (Fbuffer_live_p (workbuf
)))
6615 Fkill_buffer (workbuf
);
6617 set_buffer_internal (XBUFFER (current
));
6623 code_conversion_save (with_work_buf
, multibyte
)
6624 int with_work_buf
, multibyte
;
6626 Lisp_Object workbuf
= Qnil
;
6629 workbuf
= make_conversion_work_buffer (multibyte
);
6630 record_unwind_protect (code_conversion_restore
,
6631 Fcons (Fcurrent_buffer (), workbuf
));
6636 decode_coding_gap (coding
, chars
, bytes
)
6637 struct coding_system
*coding
;
6638 EMACS_INT chars
, bytes
;
6640 int count
= specpdl_ptr
- specpdl
;
6643 code_conversion_save (0, 0);
6645 coding
->src_object
= Fcurrent_buffer ();
6646 coding
->src_chars
= chars
;
6647 coding
->src_bytes
= bytes
;
6648 coding
->src_pos
= -chars
;
6649 coding
->src_pos_byte
= -bytes
;
6650 coding
->src_multibyte
= chars
< bytes
;
6651 coding
->dst_object
= coding
->src_object
;
6652 coding
->dst_pos
= PT
;
6653 coding
->dst_pos_byte
= PT_BYTE
;
6654 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6655 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6657 if (CODING_REQUIRE_DETECTION (coding
))
6658 detect_coding (coding
);
6660 decode_coding (coding
);
6662 attrs
= CODING_ID_ATTRS (coding
->id
);
6663 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6665 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6668 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6669 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6670 make_number (coding
->produced_char
));
6672 coding
->produced_char
+= Z
- prev_Z
;
6673 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6676 unbind_to (count
, Qnil
);
6677 return coding
->result
;
6681 encode_coding_gap (coding
, chars
, bytes
)
6682 struct coding_system
*coding
;
6683 EMACS_INT chars
, bytes
;
6685 int count
= specpdl_ptr
- specpdl
;
6687 code_conversion_save (0, 0);
6689 coding
->src_object
= Fcurrent_buffer ();
6690 coding
->src_chars
= chars
;
6691 coding
->src_bytes
= bytes
;
6692 coding
->src_pos
= -chars
;
6693 coding
->src_pos_byte
= -bytes
;
6694 coding
->src_multibyte
= chars
< bytes
;
6695 coding
->dst_object
= coding
->src_object
;
6696 coding
->dst_pos
= PT
;
6697 coding
->dst_pos_byte
= PT_BYTE
;
6699 encode_coding (coding
);
6701 unbind_to (count
, Qnil
);
6702 return coding
->result
;
6706 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6707 SRC_OBJECT into DST_OBJECT by coding context CODING.
6709 SRC_OBJECT is a buffer, a string, or Qnil.
6711 If it is a buffer, the text is at point of the buffer. FROM and TO
6712 are positions in the buffer.
6714 If it is a string, the text is at the beginning of the string.
6715 FROM and TO are indices to the string.
6717 If it is nil, the text is at coding->source. FROM and TO are
6718 indices to coding->source.
6720 DST_OBJECT is a buffer, Qt, or Qnil.
6722 If it is a buffer, the decoded text is inserted at point of the
6723 buffer. If the buffer is the same as SRC_OBJECT, the source text
6726 If it is Qt, a string is made from the decoded text, and
6727 set in CODING->dst_object.
6729 If it is Qnil, the decoded text is stored at CODING->destination.
6730 The caller must allocate CODING->dst_bytes bytes at
6731 CODING->destination by xmalloc. If the decoded text is longer than
6732 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6736 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6738 struct coding_system
*coding
;
6739 Lisp_Object src_object
;
6740 EMACS_INT from
, from_byte
, to
, to_byte
;
6741 Lisp_Object dst_object
;
6743 int count
= specpdl_ptr
- specpdl
;
6744 unsigned char *destination
;
6745 EMACS_INT dst_bytes
;
6746 EMACS_INT chars
= to
- from
;
6747 EMACS_INT bytes
= to_byte
- from_byte
;
6750 int saved_pt
= -1, saved_pt_byte
;
6752 buffer
= Fcurrent_buffer ();
6754 if (NILP (dst_object
))
6756 destination
= coding
->destination
;
6757 dst_bytes
= coding
->dst_bytes
;
6760 coding
->src_object
= src_object
;
6761 coding
->src_chars
= chars
;
6762 coding
->src_bytes
= bytes
;
6763 coding
->src_multibyte
= chars
< bytes
;
6765 if (STRINGP (src_object
))
6767 coding
->src_pos
= from
;
6768 coding
->src_pos_byte
= from_byte
;
6770 else if (BUFFERP (src_object
))
6772 set_buffer_internal (XBUFFER (src_object
));
6774 move_gap_both (from
, from_byte
);
6775 if (EQ (src_object
, dst_object
))
6777 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6778 TEMP_SET_PT_BOTH (from
, from_byte
);
6779 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6780 coding
->src_pos
= -chars
;
6781 coding
->src_pos_byte
= -bytes
;
6785 coding
->src_pos
= from
;
6786 coding
->src_pos_byte
= from_byte
;
6790 if (CODING_REQUIRE_DETECTION (coding
))
6791 detect_coding (coding
);
6792 attrs
= CODING_ID_ATTRS (coding
->id
);
6794 if (EQ (dst_object
, Qt
)
6795 || (! NILP (CODING_ATTR_POST_READ (attrs
))
6796 && NILP (dst_object
)))
6798 coding
->dst_object
= code_conversion_save (1, 1);
6799 coding
->dst_pos
= BEG
;
6800 coding
->dst_pos_byte
= BEG_BYTE
;
6801 coding
->dst_multibyte
= 1;
6803 else if (BUFFERP (dst_object
))
6805 code_conversion_save (0, 0);
6806 coding
->dst_object
= dst_object
;
6807 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6808 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6809 coding
->dst_multibyte
6810 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6814 code_conversion_save (0, 0);
6815 coding
->dst_object
= Qnil
;
6816 coding
->dst_multibyte
= 1;
6819 decode_coding (coding
);
6821 if (BUFFERP (coding
->dst_object
))
6822 set_buffer_internal (XBUFFER (coding
->dst_object
));
6824 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6826 struct gcpro gcpro1
, gcpro2
;
6827 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6830 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6831 GCPRO2 (coding
->src_object
, coding
->dst_object
);
6832 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
6833 make_number (coding
->produced_char
));
6836 coding
->produced_char
+= Z
- prev_Z
;
6837 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6840 if (EQ (dst_object
, Qt
))
6842 coding
->dst_object
= Fbuffer_string ();
6844 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
6846 set_buffer_internal (XBUFFER (coding
->dst_object
));
6847 if (dst_bytes
< coding
->produced
)
6850 = (unsigned char *) xrealloc (destination
, coding
->produced
);
6853 record_conversion_result (coding
,
6854 CODING_RESULT_INSUFFICIENT_DST
);
6855 unbind_to (count
, Qnil
);
6858 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
6859 move_gap_both (BEGV
, BEGV_BYTE
);
6860 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
6861 coding
->destination
= destination
;
6867 /* This is the case of:
6868 (BUFFERP (src_object) && EQ (src_object, dst_object))
6869 As we have moved PT while replacing the original buffer
6870 contents, we must recover it now. */
6871 set_buffer_internal (XBUFFER (src_object
));
6872 if (saved_pt
< from
)
6873 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
6874 else if (saved_pt
< from
+ chars
)
6875 TEMP_SET_PT_BOTH (from
, from_byte
);
6876 else if (! NILP (current_buffer
->enable_multibyte_characters
))
6877 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
6878 saved_pt_byte
+ (coding
->produced
- bytes
));
6880 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
6881 saved_pt_byte
+ (coding
->produced
- bytes
));
6884 unbind_to (count
, coding
->dst_object
);
6889 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6891 struct coding_system
*coding
;
6892 Lisp_Object src_object
;
6893 EMACS_INT from
, from_byte
, to
, to_byte
;
6894 Lisp_Object dst_object
;
6896 int count
= specpdl_ptr
- specpdl
;
6897 EMACS_INT chars
= to
- from
;
6898 EMACS_INT bytes
= to_byte
- from_byte
;
6901 int saved_pt
= -1, saved_pt_byte
;
6902 int kill_src_buffer
= 0;
6904 buffer
= Fcurrent_buffer ();
6906 coding
->src_object
= src_object
;
6907 coding
->src_chars
= chars
;
6908 coding
->src_bytes
= bytes
;
6909 coding
->src_multibyte
= chars
< bytes
;
6911 attrs
= CODING_ID_ATTRS (coding
->id
);
6913 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
6915 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
6916 set_buffer_internal (XBUFFER (coding
->src_object
));
6917 if (STRINGP (src_object
))
6918 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
6919 else if (BUFFERP (src_object
))
6920 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
6922 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
6924 if (EQ (src_object
, dst_object
))
6926 set_buffer_internal (XBUFFER (src_object
));
6927 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6928 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6929 set_buffer_internal (XBUFFER (coding
->src_object
));
6933 Lisp_Object args
[3];
6935 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
6936 args
[1] = make_number (BEG
);
6937 args
[2] = make_number (Z
);
6938 safe_call (3, args
);
6940 if (XBUFFER (coding
->src_object
) != current_buffer
)
6941 kill_src_buffer
= 1;
6942 coding
->src_object
= Fcurrent_buffer ();
6944 move_gap_both (BEG
, BEG_BYTE
);
6945 coding
->src_chars
= Z
- BEG
;
6946 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
6947 coding
->src_pos
= BEG
;
6948 coding
->src_pos_byte
= BEG_BYTE
;
6949 coding
->src_multibyte
= Z
< Z_BYTE
;
6951 else if (STRINGP (src_object
))
6953 code_conversion_save (0, 0);
6954 coding
->src_pos
= from
;
6955 coding
->src_pos_byte
= from_byte
;
6957 else if (BUFFERP (src_object
))
6959 code_conversion_save (0, 0);
6960 set_buffer_internal (XBUFFER (src_object
));
6961 if (EQ (src_object
, dst_object
))
6963 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6964 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
6965 coding
->src_pos
= 0;
6966 coding
->src_pos_byte
= 0;
6970 if (from
< GPT
&& to
>= GPT
)
6971 move_gap_both (from
, from_byte
);
6972 coding
->src_pos
= from
;
6973 coding
->src_pos_byte
= from_byte
;
6977 code_conversion_save (0, 0);
6979 if (BUFFERP (dst_object
))
6981 coding
->dst_object
= dst_object
;
6982 if (EQ (src_object
, dst_object
))
6984 coding
->dst_pos
= from
;
6985 coding
->dst_pos_byte
= from_byte
;
6989 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6990 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6992 coding
->dst_multibyte
6993 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6995 else if (EQ (dst_object
, Qt
))
6997 coding
->dst_object
= Qnil
;
6998 coding
->dst_bytes
= coding
->src_chars
;
6999 if (coding
->dst_bytes
== 0)
7000 coding
->dst_bytes
= 1;
7001 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7002 coding
->dst_multibyte
= 0;
7006 coding
->dst_object
= Qnil
;
7007 coding
->dst_multibyte
= 0;
7010 encode_coding (coding
);
7012 if (EQ (dst_object
, Qt
))
7014 if (BUFFERP (coding
->dst_object
))
7015 coding
->dst_object
= Fbuffer_string ();
7019 = make_unibyte_string ((char *) coding
->destination
,
7021 xfree (coding
->destination
);
7027 /* This is the case of:
7028 (BUFFERP (src_object) && EQ (src_object, dst_object))
7029 As we have moved PT while replacing the original buffer
7030 contents, we must recover it now. */
7031 set_buffer_internal (XBUFFER (src_object
));
7032 if (saved_pt
< from
)
7033 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7034 else if (saved_pt
< from
+ chars
)
7035 TEMP_SET_PT_BOTH (from
, from_byte
);
7036 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7037 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7038 saved_pt_byte
+ (coding
->produced
- bytes
));
7040 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7041 saved_pt_byte
+ (coding
->produced
- bytes
));
7044 if (kill_src_buffer
)
7045 Fkill_buffer (coding
->src_object
);
7046 unbind_to (count
, Qnil
);
7051 preferred_coding_system ()
7053 int id
= coding_categories
[coding_priorities
[0]].id
;
7055 return CODING_ID_NAME (id
);
7060 /*** 8. Emacs Lisp library functions ***/
7062 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7063 doc
: /* Return t if OBJECT is nil or a coding-system.
7064 See the documentation of `define-coding-system' for information
7065 about coding-system objects. */)
7070 || CODING_SYSTEM_ID (obj
) >= 0)
7073 || NILP (Fget (obj
, Qcoding_system_define_form
)))
7078 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7079 Sread_non_nil_coding_system
, 1, 1, 0,
7080 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7087 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7088 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7090 while (SCHARS (val
) == 0);
7091 return (Fintern (val
, Qnil
));
7094 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7095 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7096 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
7097 (prompt
, default_coding_system
)
7098 Lisp_Object prompt
, default_coding_system
;
7101 if (SYMBOLP (default_coding_system
))
7102 XSETSTRING (default_coding_system
, XPNTR (SYMBOL_NAME (default_coding_system
)));
7103 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7104 Qt
, Qnil
, Qcoding_system_history
,
7105 default_coding_system
, Qnil
);
7106 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7109 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7111 doc
: /* Check validity of CODING-SYSTEM.
7112 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7113 It is valid if it is nil or a symbol defined as a coding system by the
7114 function `define-coding-system'. */)
7116 Lisp_Object coding_system
;
7118 Lisp_Object define_form
;
7120 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7121 if (! NILP (define_form
))
7123 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7124 safe_eval (define_form
);
7126 if (!NILP (Fcoding_system_p (coding_system
)))
7127 return coding_system
;
7129 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
7133 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7134 HIGHEST is nonzero, return the coding system of the highest
7135 priority among the detected coding systems. Otherwize return a
7136 list of detected coding systems sorted by their priorities. If
7137 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7138 multibyte form but contains only ASCII and eight-bit chars.
7139 Otherwise, the bytes are raw bytes.
7141 CODING-SYSTEM controls the detection as below:
7143 If it is nil, detect both text-format and eol-format. If the
7144 text-format part of CODING-SYSTEM is already specified
7145 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7146 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7147 detect only text-format. */
7150 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7152 const unsigned char *src
;
7153 int src_chars
, src_bytes
, highest
;
7155 Lisp_Object coding_system
;
7157 const unsigned char *src_end
= src
+ src_bytes
;
7158 Lisp_Object attrs
, eol_type
;
7160 struct coding_system coding
;
7162 struct coding_detection_info detect_info
;
7163 enum coding_category base_category
;
7165 if (NILP (coding_system
))
7166 coding_system
= Qundecided
;
7167 setup_coding_system (coding_system
, &coding
);
7168 attrs
= CODING_ID_ATTRS (coding
.id
);
7169 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7170 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7172 coding
.source
= src
;
7173 coding
.src_chars
= src_chars
;
7174 coding
.src_bytes
= src_bytes
;
7175 coding
.src_multibyte
= multibytep
;
7176 coding
.consumed
= 0;
7177 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7179 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7181 /* At first, detect text-format if necessary. */
7182 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7183 if (base_category
== coding_category_undecided
)
7185 enum coding_category category
;
7186 struct coding_system
*this;
7189 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7190 for (i
= 0; src
< src_end
; i
++, src
++)
7196 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7197 && inhibit_iso_escape_detection
)
7199 coding
.head_ascii
= src
- coding
.source
;
7200 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7202 /* We have scanned the whole data. */
7203 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7204 /* We didn't find an 8-bit code. */
7210 coding
.head_ascii
= src
- coding
.source
;
7213 || detect_info
.found
)
7216 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7217 for (i
= 0; i
< coding_category_raw_text
; i
++)
7219 category
= coding_priorities
[i
];
7220 if (detect_info
.found
& (1 << category
))
7224 for (i
= 0; i
< coding_category_raw_text
; i
++)
7226 category
= coding_priorities
[i
];
7227 this = coding_categories
+ category
;
7231 /* No coding system of this category is defined. */
7232 detect_info
.rejected
|= (1 << category
);
7234 else if (category
>= coding_category_raw_text
)
7236 else if (detect_info
.checked
& (1 << category
))
7239 && (detect_info
.found
& (1 << category
)))
7244 if ((*(this->detector
)) (&coding
, &detect_info
)
7246 && (detect_info
.found
& (1 << category
)))
7248 if (category
== coding_category_utf_16_auto
)
7250 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7251 category
= coding_category_utf_16_le
;
7253 category
= coding_category_utf_16_be
;
7261 if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
7263 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7264 id
= coding_categories
[coding_category_raw_text
].id
;
7265 val
= Fcons (make_number (id
), Qnil
);
7267 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7269 detect_info
.found
= CATEGORY_MASK_ANY
;
7270 id
= coding_categories
[coding_category_undecided
].id
;
7271 val
= Fcons (make_number (id
), Qnil
);
7275 if (detect_info
.found
)
7277 detect_info
.found
= 1 << category
;
7278 val
= Fcons (make_number (this->id
), Qnil
);
7281 for (i
= 0; i
< coding_category_raw_text
; i
++)
7282 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7284 detect_info
.found
= 1 << coding_priorities
[i
];
7285 id
= coding_categories
[coding_priorities
[i
]].id
;
7286 val
= Fcons (make_number (id
), Qnil
);
7292 int mask
= detect_info
.rejected
| detect_info
.found
;
7296 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7298 category
= coding_priorities
[i
];
7299 if (! (mask
& (1 << category
)))
7301 found
|= 1 << category
;
7302 id
= coding_categories
[category
].id
;
7303 val
= Fcons (make_number (id
), val
);
7306 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7308 category
= coding_priorities
[i
];
7309 if (detect_info
.found
& (1 << category
))
7311 id
= coding_categories
[category
].id
;
7312 val
= Fcons (make_number (id
), val
);
7315 detect_info
.found
|= found
;
7318 else if (base_category
== coding_category_utf_16_auto
)
7320 if (detect_coding_utf_16 (&coding
, &detect_info
))
7322 struct coding_system
*this;
7324 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7325 this = coding_categories
+ coding_category_utf_16_le
;
7326 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7327 this = coding_categories
+ coding_category_utf_16_be
;
7328 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7329 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7331 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7332 val
= Fcons (make_number (this->id
), Qnil
);
7337 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7338 val
= Fcons (make_number (coding
.id
), Qnil
);
7341 /* Then, detect eol-format if necessary. */
7343 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7346 if (VECTORP (eol_type
))
7348 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7349 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7350 coding_category_raw_text
);
7351 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7352 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7353 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7354 coding_category_utf_16_be
);
7355 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7356 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7357 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7358 coding_category_utf_16_le
);
7362 if (EQ (eol_type
, Qunix
))
7363 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7364 else if (EQ (eol_type
, Qdos
))
7365 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7367 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7370 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7372 enum coding_category category
;
7375 id
= XINT (XCAR (tail
));
7376 attrs
= CODING_ID_ATTRS (id
);
7377 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7378 eol_type
= CODING_ID_EOL_TYPE (id
);
7379 if (VECTORP (eol_type
))
7381 if (category
== coding_category_utf_16_be
7382 || category
== coding_category_utf_16_be_nosig
)
7383 this_eol
= utf_16_be_eol
;
7384 else if (category
== coding_category_utf_16_le
7385 || category
== coding_category_utf_16_le_nosig
)
7386 this_eol
= utf_16_le_eol
;
7388 this_eol
= normal_eol
;
7390 if (this_eol
== EOL_SEEN_LF
)
7391 XSETCAR (tail
, AREF (eol_type
, 0));
7392 else if (this_eol
== EOL_SEEN_CRLF
)
7393 XSETCAR (tail
, AREF (eol_type
, 1));
7394 else if (this_eol
== EOL_SEEN_CR
)
7395 XSETCAR (tail
, AREF (eol_type
, 2));
7397 XSETCAR (tail
, CODING_ID_NAME (id
));
7400 XSETCAR (tail
, CODING_ID_NAME (id
));
7404 return (highest
? XCAR (val
) : val
);
7408 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7410 doc
: /* Detect coding system of the text in the region between START and END.
7411 Return a list of possible coding systems ordered by priority.
7413 If only ASCII characters are found, it returns a list of single element
7414 `undecided' or its subsidiary coding system according to a detected
7417 If optional argument HIGHEST is non-nil, return the coding system of
7418 highest priority. */)
7419 (start
, end
, highest
)
7420 Lisp_Object start
, end
, highest
;
7423 int from_byte
, to_byte
;
7425 CHECK_NUMBER_COERCE_MARKER (start
);
7426 CHECK_NUMBER_COERCE_MARKER (end
);
7428 validate_region (&start
, &end
);
7429 from
= XINT (start
), to
= XINT (end
);
7430 from_byte
= CHAR_TO_BYTE (from
);
7431 to_byte
= CHAR_TO_BYTE (to
);
7433 if (from
< GPT
&& to
>= GPT
)
7434 move_gap_both (to
, to_byte
);
7436 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7437 to
- from
, to_byte
- from_byte
,
7439 !NILP (current_buffer
7440 ->enable_multibyte_characters
),
7444 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7446 doc
: /* Detect coding system of the text in STRING.
7447 Return a list of possible coding systems ordered by priority.
7449 If only ASCII characters are found, it returns a list of single element
7450 `undecided' or its subsidiary coding system according to a detected
7453 If optional argument HIGHEST is non-nil, return the coding system of
7454 highest priority. */)
7456 Lisp_Object string
, highest
;
7458 CHECK_STRING (string
);
7460 return detect_coding_system (SDATA (string
),
7461 SCHARS (string
), SBYTES (string
),
7462 !NILP (highest
), STRING_MULTIBYTE (string
),
7468 char_encodable_p (c
, attrs
)
7473 struct charset
*charset
;
7474 Lisp_Object translation_table
;
7476 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7477 if (! NILP (translation_table
))
7478 c
= translate_char (translation_table
, c
);
7479 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7480 CONSP (tail
); tail
= XCDR (tail
))
7482 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7483 if (CHAR_CHARSET_P (c
, charset
))
7486 return (! NILP (tail
));
7490 /* Return a list of coding systems that safely encode the text between
7491 START and END. If EXCLUDE is non-nil, it is a list of coding
7492 systems not to check. The returned list doesn't contain any such
7493 coding systems. In any case, if the text contains only ASCII or is
7494 unibyte, return t. */
7496 DEFUN ("find-coding-systems-region-internal",
7497 Ffind_coding_systems_region_internal
,
7498 Sfind_coding_systems_region_internal
, 2, 3, 0,
7499 doc
: /* Internal use only. */)
7500 (start
, end
, exclude
)
7501 Lisp_Object start
, end
, exclude
;
7503 Lisp_Object coding_attrs_list
, safe_codings
;
7504 EMACS_INT start_byte
, end_byte
;
7505 const unsigned char *p
, *pbeg
, *pend
;
7507 Lisp_Object tail
, elt
;
7509 if (STRINGP (start
))
7511 if (!STRING_MULTIBYTE (start
)
7512 || SCHARS (start
) == SBYTES (start
))
7515 end_byte
= SBYTES (start
);
7519 CHECK_NUMBER_COERCE_MARKER (start
);
7520 CHECK_NUMBER_COERCE_MARKER (end
);
7521 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7522 args_out_of_range (start
, end
);
7523 if (NILP (current_buffer
->enable_multibyte_characters
))
7525 start_byte
= CHAR_TO_BYTE (XINT (start
));
7526 end_byte
= CHAR_TO_BYTE (XINT (end
));
7527 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7530 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7532 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7533 move_gap_both (XINT (start
), start_byte
);
7535 move_gap_both (XINT (end
), end_byte
);
7539 coding_attrs_list
= Qnil
;
7540 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7542 || NILP (Fmemq (XCAR (tail
), exclude
)))
7546 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7547 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7548 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7550 ASET (attrs
, coding_attr_trans_tbl
,
7551 get_translation_table (attrs
, 1, NULL
));
7552 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7556 if (STRINGP (start
))
7557 p
= pbeg
= SDATA (start
);
7559 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7560 pend
= p
+ (end_byte
- start_byte
);
7562 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7563 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7567 if (ASCII_BYTE_P (*p
))
7571 c
= STRING_CHAR_ADVANCE (p
);
7573 charset_map_loaded
= 0;
7574 for (tail
= coding_attrs_list
; CONSP (tail
);)
7579 else if (char_encodable_p (c
, elt
))
7581 else if (CONSP (XCDR (tail
)))
7583 XSETCAR (tail
, XCAR (XCDR (tail
)));
7584 XSETCDR (tail
, XCDR (XCDR (tail
)));
7588 XSETCAR (tail
, Qnil
);
7592 if (charset_map_loaded
)
7594 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7596 if (STRINGP (start
))
7597 pbeg
= SDATA (start
);
7599 pbeg
= BYTE_POS_ADDR (start_byte
);
7600 p
= pbeg
+ p_offset
;
7601 pend
= pbeg
+ pend_offset
;
7606 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
7607 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
7608 if (! NILP (XCAR (tail
)))
7609 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
7611 return safe_codings
;
7615 DEFUN ("unencodable-char-position", Funencodable_char_position
,
7616 Sunencodable_char_position
, 3, 5, 0,
7618 Return position of first un-encodable character in a region.
7619 START and END specfiy the region and CODING-SYSTEM specifies the
7620 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7622 If optional 4th argument COUNT is non-nil, it specifies at most how
7623 many un-encodable characters to search. In this case, the value is a
7626 If optional 5th argument STRING is non-nil, it is a string to search
7627 for un-encodable characters. In that case, START and END are indexes
7629 (start
, end
, coding_system
, count
, string
)
7630 Lisp_Object start
, end
, coding_system
, count
, string
;
7633 struct coding_system coding
;
7634 Lisp_Object attrs
, charset_list
, translation_table
;
7635 Lisp_Object positions
;
7637 const unsigned char *p
, *stop
, *pend
;
7638 int ascii_compatible
;
7640 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
7641 attrs
= CODING_ID_ATTRS (coding
.id
);
7642 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
7644 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
7645 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7646 translation_table
= get_translation_table (attrs
, 1, NULL
);
7650 validate_region (&start
, &end
);
7651 from
= XINT (start
);
7653 if (NILP (current_buffer
->enable_multibyte_characters
)
7654 || (ascii_compatible
7655 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
7657 p
= CHAR_POS_ADDR (from
);
7658 pend
= CHAR_POS_ADDR (to
);
7659 if (from
< GPT
&& to
>= GPT
)
7666 CHECK_STRING (string
);
7667 CHECK_NATNUM (start
);
7669 from
= XINT (start
);
7672 || to
> SCHARS (string
))
7673 args_out_of_range_3 (string
, start
, end
);
7674 if (! STRING_MULTIBYTE (string
))
7676 p
= SDATA (string
) + string_char_to_byte (string
, from
);
7677 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
7678 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
7686 CHECK_NATNUM (count
);
7695 if (ascii_compatible
)
7696 while (p
< stop
&& ASCII_BYTE_P (*p
))
7706 c
= STRING_CHAR_ADVANCE (p
);
7707 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
7708 && ! char_charset (translate_char (translation_table
, c
),
7709 charset_list
, NULL
))
7711 positions
= Fcons (make_number (from
), positions
);
7720 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
7724 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
7725 Scheck_coding_systems_region
, 3, 3, 0,
7726 doc
: /* Check if the region is encodable by coding systems.
7728 START and END are buffer positions specifying the region.
7729 CODING-SYSTEM-LIST is a list of coding systems to check.
7731 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7732 CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7733 whole region, POS0, POS1, ... are buffer positions where non-encodable
7734 characters are found.
7736 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7739 START may be a string. In that case, check if the string is
7740 encodable, and the value contains indices to the string instead of
7741 buffer positions. END is ignored. */)
7742 (start
, end
, coding_system_list
)
7743 Lisp_Object start
, end
, coding_system_list
;
7746 EMACS_INT start_byte
, end_byte
;
7748 const unsigned char *p
, *pbeg
, *pend
;
7750 Lisp_Object tail
, elt
, attrs
;
7752 if (STRINGP (start
))
7754 if (!STRING_MULTIBYTE (start
)
7755 && SCHARS (start
) != SBYTES (start
))
7758 end_byte
= SBYTES (start
);
7763 CHECK_NUMBER_COERCE_MARKER (start
);
7764 CHECK_NUMBER_COERCE_MARKER (end
);
7765 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7766 args_out_of_range (start
, end
);
7767 if (NILP (current_buffer
->enable_multibyte_characters
))
7769 start_byte
= CHAR_TO_BYTE (XINT (start
));
7770 end_byte
= CHAR_TO_BYTE (XINT (end
));
7771 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7774 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7776 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7777 move_gap_both (XINT (start
), start_byte
);
7779 move_gap_both (XINT (end
), end_byte
);
7785 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7788 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
7789 ASET (attrs
, coding_attr_trans_tbl
,
7790 get_translation_table (attrs
, 1, NULL
));
7791 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
7794 if (STRINGP (start
))
7795 p
= pbeg
= SDATA (start
);
7797 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7798 pend
= p
+ (end_byte
- start_byte
);
7800 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
7801 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7805 if (ASCII_BYTE_P (*p
))
7809 c
= STRING_CHAR_ADVANCE (p
);
7811 charset_map_loaded
= 0;
7812 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
7814 elt
= XCDR (XCAR (tail
));
7815 if (! char_encodable_p (c
, XCAR (elt
)))
7816 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
7818 if (charset_map_loaded
)
7820 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7822 if (STRINGP (start
))
7823 pbeg
= SDATA (start
);
7825 pbeg
= BYTE_POS_ADDR (start_byte
);
7826 p
= pbeg
+ p_offset
;
7827 pend
= pbeg
+ pend_offset
;
7835 for (; CONSP (tail
); tail
= XCDR (tail
))
7838 if (CONSP (XCDR (XCDR (elt
))))
7839 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
7848 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
7849 Lisp_Object start
, end
, coding_system
, dst_object
;
7850 int encodep
, norecord
;
7852 struct coding_system coding
;
7853 EMACS_INT from
, from_byte
, to
, to_byte
;
7854 Lisp_Object src_object
;
7856 CHECK_NUMBER_COERCE_MARKER (start
);
7857 CHECK_NUMBER_COERCE_MARKER (end
);
7858 if (NILP (coding_system
))
7859 coding_system
= Qno_conversion
;
7861 CHECK_CODING_SYSTEM (coding_system
);
7862 src_object
= Fcurrent_buffer ();
7863 if (NILP (dst_object
))
7864 dst_object
= src_object
;
7865 else if (! EQ (dst_object
, Qt
))
7866 CHECK_BUFFER (dst_object
);
7868 validate_region (&start
, &end
);
7869 from
= XFASTINT (start
);
7870 from_byte
= CHAR_TO_BYTE (from
);
7871 to
= XFASTINT (end
);
7872 to_byte
= CHAR_TO_BYTE (to
);
7874 setup_coding_system (coding_system
, &coding
);
7875 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7878 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7881 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7884 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7886 return (BUFFERP (dst_object
)
7887 ? make_number (coding
.produced_char
)
7888 : coding
.dst_object
);
7892 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
7893 3, 4, "r\nzCoding system: ",
7894 doc
: /* Decode the current region from the specified coding system.
7895 When called from a program, takes four arguments:
7896 START, END, CODING-SYSTEM, and DESTINATION.
7897 START and END are buffer positions.
7899 Optional 4th arguments DESTINATION specifies where the decoded text goes.
7900 If nil, the region between START and END is replace by the decoded text.
7901 If buffer, the decoded text is inserted in the buffer.
7902 If t, the decoded text is returned.
7904 This function sets `last-coding-system-used' to the precise coding system
7905 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7906 not fully specified.)
7907 It returns the length of the decoded text. */)
7908 (start
, end
, coding_system
, destination
)
7909 Lisp_Object start
, end
, coding_system
, destination
;
7911 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
7914 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
7915 3, 4, "r\nzCoding system: ",
7916 doc
: /* Encode the current region by specified coding system.
7917 When called from a program, takes three arguments:
7918 START, END, and CODING-SYSTEM. START and END are buffer positions.
7920 Optional 4th arguments DESTINATION specifies where the encoded text goes.
7921 If nil, the region between START and END is replace by the encoded text.
7922 If buffer, the encoded text is inserted in the buffer.
7923 If t, the encoded text is returned.
7925 This function sets `last-coding-system-used' to the precise coding system
7926 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7927 not fully specified.)
7928 It returns the length of the encoded text. */)
7929 (start
, end
, coding_system
, destination
)
7930 Lisp_Object start
, end
, coding_system
, destination
;
7932 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
7936 code_convert_string (string
, coding_system
, dst_object
,
7937 encodep
, nocopy
, norecord
)
7938 Lisp_Object string
, coding_system
, dst_object
;
7939 int encodep
, nocopy
, norecord
;
7941 struct coding_system coding
;
7942 EMACS_INT chars
, bytes
;
7944 CHECK_STRING (string
);
7945 if (NILP (coding_system
))
7948 Vlast_coding_system_used
= Qno_conversion
;
7949 if (NILP (dst_object
))
7950 return (nocopy
? Fcopy_sequence (string
) : string
);
7953 if (NILP (coding_system
))
7954 coding_system
= Qno_conversion
;
7956 CHECK_CODING_SYSTEM (coding_system
);
7957 if (NILP (dst_object
))
7959 else if (! EQ (dst_object
, Qt
))
7960 CHECK_BUFFER (dst_object
);
7962 setup_coding_system (coding_system
, &coding
);
7963 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7964 chars
= SCHARS (string
);
7965 bytes
= SBYTES (string
);
7967 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
7969 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
7971 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7973 return (BUFFERP (dst_object
)
7974 ? make_number (coding
.produced_char
)
7975 : coding
.dst_object
);
7979 /* Encode or decode STRING according to CODING_SYSTEM.
7980 Do not set Vlast_coding_system_used.
7982 This function is called only from macros DECODE_FILE and
7983 ENCODE_FILE, thus we ignore character composition. */
7986 code_convert_string_norecord (string
, coding_system
, encodep
)
7987 Lisp_Object string
, coding_system
;
7990 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
7994 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
7996 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
7998 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
7999 if the decoding operation is trivial.
8001 Optional fourth arg BUFFER non-nil meant that the decoded text is
8002 inserted in BUFFER instead of returned as a string. In this case,
8003 the return value is BUFFER.
8005 This function sets `last-coding-system-used' to the precise coding system
8006 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8007 not fully specified. */)
8008 (string
, coding_system
, nocopy
, buffer
)
8009 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8011 return code_convert_string (string
, coding_system
, buffer
,
8012 0, ! NILP (nocopy
), 0);
8015 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8017 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8019 Optional third arg NOCOPY non-nil means it is OK to return STRING
8020 itself if the encoding operation is trivial.
8022 Optional fourth arg BUFFER non-nil meant that the encoded text is
8023 inserted in BUFFER instead of returned as a string. In this case,
8024 the return value is BUFFER.
8026 This function sets `last-coding-system-used' to the precise coding system
8027 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8028 not fully specified.) */)
8029 (string
, coding_system
, nocopy
, buffer
)
8030 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8032 return code_convert_string (string
, coding_system
, buffer
,
8033 1, ! NILP (nocopy
), 1);
8037 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8038 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8039 Return the corresponding character. */)
8043 Lisp_Object spec
, attrs
, val
;
8044 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8047 CHECK_NATNUM (code
);
8048 c
= XFASTINT (code
);
8049 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8050 attrs
= AREF (spec
, 0);
8052 if (ASCII_BYTE_P (c
)
8053 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8056 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8057 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8058 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8059 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8062 charset
= charset_roman
;
8063 else if (c
>= 0xA0 && c
< 0xDF)
8065 charset
= charset_kana
;
8070 int s1
= c
>> 8, s2
= c
& 0xFF;
8072 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8073 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8074 error ("Invalid code: %d", code
);
8076 charset
= charset_kanji
;
8078 c
= DECODE_CHAR (charset
, c
);
8080 error ("Invalid code: %d", code
);
8081 return make_number (c
);
8085 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8086 doc
: /* Encode a Japanese character CHAR to shift_jis encoding.
8087 Return the corresponding code in SJIS. */)
8091 Lisp_Object spec
, attrs
, charset_list
;
8093 struct charset
*charset
;
8096 CHECK_CHARACTER (ch
);
8098 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8099 attrs
= AREF (spec
, 0);
8101 if (ASCII_CHAR_P (c
)
8102 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8105 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8106 charset
= char_charset (c
, charset_list
, &code
);
8107 if (code
== CHARSET_INVALID_CODE (charset
))
8108 error ("Can't encode by shift_jis encoding: %d", c
);
8111 return make_number (code
);
8114 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8115 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8116 Return the corresponding character. */)
8120 Lisp_Object spec
, attrs
, val
;
8121 struct charset
*charset_roman
, *charset_big5
, *charset
;
8124 CHECK_NATNUM (code
);
8125 c
= XFASTINT (code
);
8126 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8127 attrs
= AREF (spec
, 0);
8129 if (ASCII_BYTE_P (c
)
8130 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8133 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8134 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8135 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8138 charset
= charset_roman
;
8141 int b1
= c
>> 8, b2
= c
& 0x7F;
8142 if (b1
< 0xA1 || b1
> 0xFE
8143 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8144 error ("Invalid code: %d", code
);
8145 charset
= charset_big5
;
8147 c
= DECODE_CHAR (charset
, (unsigned )c
);
8149 error ("Invalid code: %d", code
);
8150 return make_number (c
);
8153 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8154 doc
: /* Encode the Big5 character CHAR to BIG5 coding system.
8155 Return the corresponding character code in Big5. */)
8159 Lisp_Object spec
, attrs
, charset_list
;
8160 struct charset
*charset
;
8164 CHECK_CHARACTER (ch
);
8166 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8167 attrs
= AREF (spec
, 0);
8168 if (ASCII_CHAR_P (c
)
8169 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8172 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8173 charset
= char_charset (c
, charset_list
, &code
);
8174 if (code
== CHARSET_INVALID_CODE (charset
))
8175 error ("Can't encode by Big5 encoding: %d", c
);
8177 return make_number (code
);
8181 DEFUN ("set-terminal-coding-system-internal",
8182 Fset_terminal_coding_system_internal
,
8183 Sset_terminal_coding_system_internal
, 1, 1, 0,
8184 doc
: /* Internal use only. */)
8186 Lisp_Object coding_system
;
8188 CHECK_SYMBOL (coding_system
);
8189 setup_coding_system (Fcheck_coding_system (coding_system
),
8192 /* We had better not send unsafe characters to terminal. */
8193 terminal_coding
.mode
|= CODING_MODE_SAFE_ENCODING
;
8194 /* Characer composition should be disabled. */
8195 terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8196 terminal_coding
.src_multibyte
= 1;
8197 terminal_coding
.dst_multibyte
= 0;
8201 DEFUN ("set-safe-terminal-coding-system-internal",
8202 Fset_safe_terminal_coding_system_internal
,
8203 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8204 doc
: /* Internal use only. */)
8206 Lisp_Object coding_system
;
8208 CHECK_SYMBOL (coding_system
);
8209 setup_coding_system (Fcheck_coding_system (coding_system
),
8210 &safe_terminal_coding
);
8211 /* Characer composition should be disabled. */
8212 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8213 safe_terminal_coding
.src_multibyte
= 1;
8214 safe_terminal_coding
.dst_multibyte
= 0;
8218 DEFUN ("terminal-coding-system",
8219 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
8220 doc
: /* Return coding system specified for terminal output. */)
8223 Lisp_Object coding_system
;
8225 coding_system
= CODING_ID_NAME (terminal_coding
.id
);
8226 /* For backward compatibility, return nil if it is `undecided'. */
8227 return (coding_system
!= Qundecided
? coding_system
: Qnil
);
8230 DEFUN ("set-keyboard-coding-system-internal",
8231 Fset_keyboard_coding_system_internal
,
8232 Sset_keyboard_coding_system_internal
, 1, 1, 0,
8233 doc
: /* Internal use only. */)
8235 Lisp_Object coding_system
;
8237 CHECK_SYMBOL (coding_system
);
8238 setup_coding_system (Fcheck_coding_system (coding_system
),
8240 /* Characer composition should be disabled. */
8241 keyboard_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8245 DEFUN ("keyboard-coding-system",
8246 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
8247 doc
: /* Return coding system specified for decoding keyboard input. */)
8250 return CODING_ID_NAME (keyboard_coding
.id
);
8254 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8255 Sfind_operation_coding_system
, 1, MANY
, 0,
8256 doc
: /* Choose a coding system for an operation based on the target name.
8257 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8258 DECODING-SYSTEM is the coding system to use for decoding
8259 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8260 for encoding (in case OPERATION does encoding).
8262 The first argument OPERATION specifies an I/O primitive:
8263 For file I/O, `insert-file-contents' or `write-region'.
8264 For process I/O, `call-process', `call-process-region', or `start-process'.
8265 For network I/O, `open-network-stream'.
8267 The remaining arguments should be the same arguments that were passed
8268 to the primitive. Depending on which primitive, one of those arguments
8269 is selected as the TARGET. For example, if OPERATION does file I/O,
8270 whichever argument specifies the file name is TARGET.
8272 TARGET has a meaning which depends on OPERATION:
8273 For file I/O, TARGET is a file name.
8274 For process I/O, TARGET is a process name.
8275 For network I/O, TARGET is a service name or a port number
8277 This function looks up what specified for TARGET in,
8278 `file-coding-system-alist', `process-coding-system-alist',
8279 or `network-coding-system-alist' depending on OPERATION.
8280 They may specify a coding system, a cons of coding systems,
8281 or a function symbol to call.
8282 In the last case, we call the function with one argument,
8283 which is a list of all the arguments given to this function.
8285 usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
8290 Lisp_Object operation
, target_idx
, target
, val
;
8291 register Lisp_Object chain
;
8294 error ("Too few arguments");
8295 operation
= args
[0];
8296 if (!SYMBOLP (operation
)
8297 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8298 error ("Invalid first arguement");
8299 if (nargs
< 1 + XINT (target_idx
))
8300 error ("Too few arguments for operation: %s",
8301 SDATA (SYMBOL_NAME (operation
)));
8302 target
= args
[XINT (target_idx
) + 1];
8303 if (!(STRINGP (target
)
8304 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8305 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8307 chain
= ((EQ (operation
, Qinsert_file_contents
)
8308 || EQ (operation
, Qwrite_region
))
8309 ? Vfile_coding_system_alist
8310 : (EQ (operation
, Qopen_network_stream
)
8311 ? Vnetwork_coding_system_alist
8312 : Vprocess_coding_system_alist
));
8316 for (; CONSP (chain
); chain
= XCDR (chain
))
8322 && ((STRINGP (target
)
8323 && STRINGP (XCAR (elt
))
8324 && fast_string_match (XCAR (elt
), target
) >= 0)
8325 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8328 /* Here, if VAL is both a valid coding system and a valid
8329 function symbol, we return VAL as a coding system. */
8332 if (! SYMBOLP (val
))
8334 if (! NILP (Fcoding_system_p (val
)))
8335 return Fcons (val
, val
);
8336 if (! NILP (Ffboundp (val
)))
8338 val
= call1 (val
, Flist (nargs
, args
));
8341 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8342 return Fcons (val
, val
);
8350 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8351 Sset_coding_system_priority
, 0, MANY
, 0,
8352 doc
: /* Assign higher priority to the coding systems given as arguments.
8353 If multiple coding systems belongs to the same category,
8354 all but the first one are ignored.
8356 usage: (set-coding-system-priority ...) */)
8362 int changed
[coding_category_max
];
8363 enum coding_category priorities
[coding_category_max
];
8365 bzero (changed
, sizeof changed
);
8367 for (i
= j
= 0; i
< nargs
; i
++)
8369 enum coding_category category
;
8370 Lisp_Object spec
, attrs
;
8372 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8373 attrs
= AREF (spec
, 0);
8374 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8375 if (changed
[category
])
8376 /* Ignore this coding system because a coding system of the
8377 same category already had a higher priority. */
8379 changed
[category
] = 1;
8380 priorities
[j
++] = category
;
8381 if (coding_categories
[category
].id
>= 0
8382 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8383 setup_coding_system (args
[i
], &coding_categories
[category
]);
8384 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8387 /* Now we have decided top J priorities. Reflect the order of the
8388 original priorities to the remaining priorities. */
8390 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8392 while (j
< coding_category_max
8393 && changed
[coding_priorities
[j
]])
8395 if (j
== coding_category_max
)
8397 priorities
[i
] = coding_priorities
[j
];
8400 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8402 /* Update `coding-category-list'. */
8403 Vcoding_category_list
= Qnil
;
8404 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8405 Vcoding_category_list
8406 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8407 Vcoding_category_list
);
8412 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8413 Scoding_system_priority_list
, 0, 1, 0,
8414 doc
: /* Return a list of coding systems ordered by their priorities.
8415 HIGHESTP non-nil means just return the highest priority one. */)
8417 Lisp_Object highestp
;
8422 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8424 enum coding_category category
= coding_priorities
[i
];
8425 int id
= coding_categories
[category
].id
;
8430 attrs
= CODING_ID_ATTRS (id
);
8431 if (! NILP (highestp
))
8432 return CODING_ATTR_BASE_NAME (attrs
);
8433 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8435 return Fnreverse (val
);
8438 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8441 make_subsidiaries (base
)
8444 Lisp_Object subsidiaries
;
8445 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8446 char *buf
= (char *) alloca (base_name_len
+ 6);
8449 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8450 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8451 for (i
= 0; i
< 3; i
++)
8453 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8454 ASET (subsidiaries
, i
, intern (buf
));
8456 return subsidiaries
;
8460 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8461 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8462 doc
: /* For internal use only.
8463 usage: (define-coding-system-internal ...) */)
8469 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8470 Lisp_Object attrs
; /* Vector of attributes. */
8471 Lisp_Object eol_type
;
8472 Lisp_Object aliases
;
8473 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8474 enum coding_category category
;
8475 Lisp_Object tail
, val
;
8476 int max_charset_id
= 0;
8479 if (nargs
< coding_arg_max
)
8482 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8484 name
= args
[coding_arg_name
];
8485 CHECK_SYMBOL (name
);
8486 CODING_ATTR_BASE_NAME (attrs
) = name
;
8488 val
= args
[coding_arg_mnemonic
];
8489 if (! STRINGP (val
))
8490 CHECK_CHARACTER (val
);
8491 CODING_ATTR_MNEMONIC (attrs
) = val
;
8493 coding_type
= args
[coding_arg_coding_type
];
8494 CHECK_SYMBOL (coding_type
);
8495 CODING_ATTR_TYPE (attrs
) = coding_type
;
8497 charset_list
= args
[coding_arg_charset_list
];
8498 if (SYMBOLP (charset_list
))
8500 if (EQ (charset_list
, Qiso_2022
))
8502 if (! EQ (coding_type
, Qiso_2022
))
8503 error ("Invalid charset-list");
8504 charset_list
= Viso_2022_charset_list
;
8506 else if (EQ (charset_list
, Qemacs_mule
))
8508 if (! EQ (coding_type
, Qemacs_mule
))
8509 error ("Invalid charset-list");
8510 charset_list
= Vemacs_mule_charset_list
;
8512 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8513 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8514 max_charset_id
= XFASTINT (XCAR (tail
));
8518 charset_list
= Fcopy_sequence (charset_list
);
8519 for (tail
= charset_list
; !NILP (tail
); tail
= Fcdr (tail
))
8521 struct charset
*charset
;
8524 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8525 if (EQ (coding_type
, Qiso_2022
)
8526 ? CHARSET_ISO_FINAL (charset
) < 0
8527 : EQ (coding_type
, Qemacs_mule
)
8528 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8530 error ("Can't handle charset `%s'",
8531 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8533 XSETCAR (tail
, make_number (charset
->id
));
8534 if (max_charset_id
< charset
->id
)
8535 max_charset_id
= charset
->id
;
8538 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8540 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8542 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8543 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8544 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8546 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8548 val
= args
[coding_arg_decode_translation_table
];
8549 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8551 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8553 val
= args
[coding_arg_encode_translation_table
];
8554 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8556 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8558 val
= args
[coding_arg_post_read_conversion
];
8560 CODING_ATTR_POST_READ (attrs
) = val
;
8562 val
= args
[coding_arg_pre_write_conversion
];
8564 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8566 val
= args
[coding_arg_default_char
];
8568 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
8571 CHECK_CHARACTER (val
);
8572 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8575 val
= args
[coding_arg_for_unibyte
];
8576 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
8578 val
= args
[coding_arg_plist
];
8580 CODING_ATTR_PLIST (attrs
) = val
;
8582 if (EQ (coding_type
, Qcharset
))
8584 /* Generate a lisp vector of 256 elements. Each element is nil,
8585 integer, or a list of charset IDs.
8587 If Nth element is nil, the byte code N is invalid in this
8590 If Nth element is a number NUM, N is the first byte of a
8591 charset whose ID is NUM.
8593 If Nth element is a list of charset IDs, N is the first byte
8594 of one of them. The list is sorted by dimensions of the
8595 charsets. A charset of smaller dimension comes firtst. */
8596 val
= Fmake_vector (make_number (256), Qnil
);
8598 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8600 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
8601 int dim
= CHARSET_DIMENSION (charset
);
8602 int idx
= (dim
- 1) * 4;
8604 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8605 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8607 for (i
= charset
->code_space
[idx
];
8608 i
<= charset
->code_space
[idx
+ 1]; i
++)
8610 Lisp_Object tmp
, tmp2
;
8613 tmp
= AREF (val
, i
);
8616 else if (NUMBERP (tmp
))
8618 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
8620 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
8622 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
8626 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
8628 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
8633 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
8636 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
8637 XSETCAR (tmp2
, XCAR (tail
));
8643 ASET (attrs
, coding_attr_charset_valids
, val
);
8644 category
= coding_category_charset
;
8646 else if (EQ (coding_type
, Qccl
))
8650 if (nargs
< coding_arg_ccl_max
)
8653 val
= args
[coding_arg_ccl_decoder
];
8654 CHECK_CCL_PROGRAM (val
);
8656 val
= Fcopy_sequence (val
);
8657 ASET (attrs
, coding_attr_ccl_decoder
, val
);
8659 val
= args
[coding_arg_ccl_encoder
];
8660 CHECK_CCL_PROGRAM (val
);
8662 val
= Fcopy_sequence (val
);
8663 ASET (attrs
, coding_attr_ccl_encoder
, val
);
8665 val
= args
[coding_arg_ccl_valids
];
8666 valids
= Fmake_string (make_number (256), make_number (0));
8667 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
8674 from
= to
= XINT (val
);
8675 if (from
< 0 || from
> 255)
8676 args_out_of_range_3 (val
, make_number (0), make_number (255));
8681 CHECK_NATNUM_CAR (val
);
8682 CHECK_NATNUM_CDR (val
);
8683 from
= XINT (XCAR (val
));
8685 args_out_of_range_3 (XCAR (val
),
8686 make_number (0), make_number (255));
8687 to
= XINT (XCDR (val
));
8688 if (to
< from
|| to
> 255)
8689 args_out_of_range_3 (XCDR (val
),
8690 XCAR (val
), make_number (255));
8692 for (i
= from
; i
<= to
; i
++)
8693 SSET (valids
, i
, 1);
8695 ASET (attrs
, coding_attr_ccl_valids
, valids
);
8697 category
= coding_category_ccl
;
8699 else if (EQ (coding_type
, Qutf_16
))
8701 Lisp_Object bom
, endian
;
8703 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8705 if (nargs
< coding_arg_utf16_max
)
8708 bom
= args
[coding_arg_utf16_bom
];
8709 if (! NILP (bom
) && ! EQ (bom
, Qt
))
8713 CHECK_CODING_SYSTEM (val
);
8715 CHECK_CODING_SYSTEM (val
);
8717 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
8719 endian
= args
[coding_arg_utf16_endian
];
8720 CHECK_SYMBOL (endian
);
8723 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
8724 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
8725 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
8727 category
= (CONSP (bom
)
8728 ? coding_category_utf_16_auto
8730 ? (EQ (endian
, Qbig
)
8731 ? coding_category_utf_16_be_nosig
8732 : coding_category_utf_16_le_nosig
)
8733 : (EQ (endian
, Qbig
)
8734 ? coding_category_utf_16_be
8735 : coding_category_utf_16_le
));
8737 else if (EQ (coding_type
, Qiso_2022
))
8739 Lisp_Object initial
, reg_usage
, request
, flags
;
8742 if (nargs
< coding_arg_iso2022_max
)
8745 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
8746 CHECK_VECTOR (initial
);
8747 for (i
= 0; i
< 4; i
++)
8749 val
= Faref (initial
, make_number (i
));
8752 struct charset
*charset
;
8754 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8755 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
8756 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
8757 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8760 ASET (initial
, i
, make_number (-1));
8763 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
8764 CHECK_CONS (reg_usage
);
8765 CHECK_NUMBER_CAR (reg_usage
);
8766 CHECK_NUMBER_CDR (reg_usage
);
8768 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
8769 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
8777 CHECK_CHARSET_GET_ID (tmp
, id
);
8778 CHECK_NATNUM_CDR (val
);
8779 if (XINT (XCDR (val
)) >= 4)
8780 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
8781 XSETCAR (val
, make_number (id
));
8784 flags
= args
[coding_arg_iso2022_flags
];
8785 CHECK_NATNUM (flags
);
8787 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
8788 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
8790 ASET (attrs
, coding_attr_iso_initial
, initial
);
8791 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
8792 ASET (attrs
, coding_attr_iso_request
, request
);
8793 ASET (attrs
, coding_attr_iso_flags
, flags
);
8794 setup_iso_safe_charsets (attrs
);
8796 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
8797 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
8798 | CODING_ISO_FLAG_SINGLE_SHIFT
))
8799 ? coding_category_iso_7_else
8800 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8801 ? coding_category_iso_7
8802 : coding_category_iso_7_tight
);
8805 int id
= XINT (AREF (initial
, 1));
8807 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
8808 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8810 ? coding_category_iso_8_else
8811 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
8812 ? coding_category_iso_8_1
8813 : coding_category_iso_8_2
);
8815 if (category
!= coding_category_iso_8_1
8816 && category
!= coding_category_iso_8_2
)
8817 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8819 else if (EQ (coding_type
, Qemacs_mule
))
8821 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
8822 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
8823 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8824 category
= coding_category_emacs_mule
;
8826 else if (EQ (coding_type
, Qshift_jis
))
8829 struct charset
*charset
;
8831 if (XINT (Flength (charset_list
)) != 3
8832 && XINT (Flength (charset_list
)) != 4)
8833 error ("There should be three or four charsets");
8835 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8836 if (CHARSET_DIMENSION (charset
) != 1)
8837 error ("Dimension of charset %s is not one",
8838 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8839 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8840 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8842 charset_list
= XCDR (charset_list
);
8843 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8844 if (CHARSET_DIMENSION (charset
) != 1)
8845 error ("Dimension of charset %s is not one",
8846 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8848 charset_list
= XCDR (charset_list
);
8849 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8850 if (CHARSET_DIMENSION (charset
) != 2)
8851 error ("Dimension of charset %s is not two",
8852 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8854 charset_list
= XCDR (charset_list
);
8855 if (! NILP (charset_list
))
8857 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8858 if (CHARSET_DIMENSION (charset
) != 2)
8859 error ("Dimension of charset %s is not two",
8860 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8863 category
= coding_category_sjis
;
8864 Vsjis_coding_system
= name
;
8866 else if (EQ (coding_type
, Qbig5
))
8868 struct charset
*charset
;
8870 if (XINT (Flength (charset_list
)) != 2)
8871 error ("There should be just two charsets");
8873 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8874 if (CHARSET_DIMENSION (charset
) != 1)
8875 error ("Dimension of charset %s is not one",
8876 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8877 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8878 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8880 charset_list
= XCDR (charset_list
);
8881 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8882 if (CHARSET_DIMENSION (charset
) != 2)
8883 error ("Dimension of charset %s is not two",
8884 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8886 category
= coding_category_big5
;
8887 Vbig5_coding_system
= name
;
8889 else if (EQ (coding_type
, Qraw_text
))
8891 category
= coding_category_raw_text
;
8892 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8894 else if (EQ (coding_type
, Qutf_8
))
8896 category
= coding_category_utf_8
;
8897 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8899 else if (EQ (coding_type
, Qundecided
))
8900 category
= coding_category_undecided
;
8902 error ("Invalid coding system type: %s",
8903 SDATA (SYMBOL_NAME (coding_type
)));
8905 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
8906 CODING_ATTR_PLIST (attrs
)
8907 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
8908 CODING_ATTR_PLIST (attrs
)));
8909 CODING_ATTR_PLIST (attrs
)
8910 = Fcons (QCascii_compatible_p
,
8911 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
8912 CODING_ATTR_PLIST (attrs
)));
8914 eol_type
= args
[coding_arg_eol_type
];
8915 if (! NILP (eol_type
)
8916 && ! EQ (eol_type
, Qunix
)
8917 && ! EQ (eol_type
, Qdos
)
8918 && ! EQ (eol_type
, Qmac
))
8919 error ("Invalid eol-type");
8921 aliases
= Fcons (name
, Qnil
);
8923 if (NILP (eol_type
))
8925 eol_type
= make_subsidiaries (name
);
8926 for (i
= 0; i
< 3; i
++)
8928 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
8930 this_name
= AREF (eol_type
, i
);
8931 this_aliases
= Fcons (this_name
, Qnil
);
8932 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
8933 this_spec
= Fmake_vector (make_number (3), attrs
);
8934 ASET (this_spec
, 1, this_aliases
);
8935 ASET (this_spec
, 2, this_eol_type
);
8936 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
8937 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
8938 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
8940 Vcoding_system_alist
8941 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
8942 Vcoding_system_alist
);
8946 spec_vec
= Fmake_vector (make_number (3), attrs
);
8947 ASET (spec_vec
, 1, aliases
);
8948 ASET (spec_vec
, 2, eol_type
);
8950 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
8951 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
8952 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
8954 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
8955 Vcoding_system_alist
);
8958 int id
= coding_categories
[category
].id
;
8960 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
8961 setup_coding_system (name
, &coding_categories
[category
]);
8967 return Fsignal (Qwrong_number_of_arguments
,
8968 Fcons (intern ("define-coding-system-internal"),
8969 make_number (nargs
)));
8973 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
8975 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
8976 (coding_system
, prop
, val
)
8977 Lisp_Object coding_system
, prop
, val
;
8979 Lisp_Object spec
, attrs
;
8981 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8982 attrs
= AREF (spec
, 0);
8983 if (EQ (prop
, QCmnemonic
))
8985 if (! STRINGP (val
))
8986 CHECK_CHARACTER (val
);
8987 CODING_ATTR_MNEMONIC (attrs
) = val
;
8989 else if (EQ (prop
, QCdefalut_char
))
8992 val
= make_number (' ');
8994 CHECK_CHARACTER (val
);
8995 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8997 else if (EQ (prop
, QCdecode_translation_table
))
8999 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9001 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9003 else if (EQ (prop
, QCencode_translation_table
))
9005 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9007 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9009 else if (EQ (prop
, QCpost_read_conversion
))
9012 CODING_ATTR_POST_READ (attrs
) = val
;
9014 else if (EQ (prop
, QCpre_write_conversion
))
9017 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9019 else if (EQ (prop
, QCascii_compatible_p
))
9021 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9024 CODING_ATTR_PLIST (attrs
)
9025 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9030 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9031 Sdefine_coding_system_alias
, 2, 2, 0,
9032 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9033 (alias
, coding_system
)
9034 Lisp_Object alias
, coding_system
;
9036 Lisp_Object spec
, aliases
, eol_type
, val
;
9038 CHECK_SYMBOL (alias
);
9039 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9040 aliases
= AREF (spec
, 1);
9041 /* ALISES should be a list of length more than zero, and the first
9042 element is a base coding system. Append ALIAS at the tail of the
9044 while (!NILP (XCDR (aliases
)))
9045 aliases
= XCDR (aliases
);
9046 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9048 eol_type
= AREF (spec
, 2);
9049 if (VECTORP (eol_type
))
9051 Lisp_Object subsidiaries
;
9054 subsidiaries
= make_subsidiaries (alias
);
9055 for (i
= 0; i
< 3; i
++)
9056 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9057 AREF (eol_type
, i
));
9060 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9061 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9062 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
9064 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9065 Vcoding_system_alist
);
9070 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9072 doc
: /* Return the base of CODING-SYSTEM.
9073 Any alias or subsidiary coding system is not a base coding system. */)
9075 Lisp_Object coding_system
;
9077 Lisp_Object spec
, attrs
;
9079 if (NILP (coding_system
))
9080 return (Qno_conversion
);
9081 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9082 attrs
= AREF (spec
, 0);
9083 return CODING_ATTR_BASE_NAME (attrs
);
9086 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9088 doc
: "Return the property list of CODING-SYSTEM.")
9090 Lisp_Object coding_system
;
9092 Lisp_Object spec
, attrs
;
9094 if (NILP (coding_system
))
9095 coding_system
= Qno_conversion
;
9096 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9097 attrs
= AREF (spec
, 0);
9098 return CODING_ATTR_PLIST (attrs
);
9102 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9104 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9106 Lisp_Object coding_system
;
9110 if (NILP (coding_system
))
9111 coding_system
= Qno_conversion
;
9112 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9113 return AREF (spec
, 1);
9116 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9117 Scoding_system_eol_type
, 1, 1, 0,
9118 doc
: /* Return eol-type of CODING-SYSTEM.
9119 An eol-type is integer 0, 1, 2, or a vector of coding systems.
9121 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9122 and CR respectively.
9124 A vector value indicates that a format of end-of-line should be
9125 detected automatically. Nth element of the vector is the subsidiary
9126 coding system whose eol-type is N. */)
9128 Lisp_Object coding_system
;
9130 Lisp_Object spec
, eol_type
;
9133 if (NILP (coding_system
))
9134 coding_system
= Qno_conversion
;
9135 if (! CODING_SYSTEM_P (coding_system
))
9137 spec
= CODING_SYSTEM_SPEC (coding_system
);
9138 eol_type
= AREF (spec
, 2);
9139 if (VECTORP (eol_type
))
9140 return Fcopy_sequence (eol_type
);
9141 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9142 return make_number (n
);
9148 /*** 9. Post-amble ***/
9155 for (i
= 0; i
< coding_category_max
; i
++)
9157 coding_categories
[i
].id
= -1;
9158 coding_priorities
[i
] = i
;
9161 /* ISO2022 specific initialize routine. */
9162 for (i
= 0; i
< 0x20; i
++)
9163 iso_code_class
[i
] = ISO_control_0
;
9164 for (i
= 0x21; i
< 0x7F; i
++)
9165 iso_code_class
[i
] = ISO_graphic_plane_0
;
9166 for (i
= 0x80; i
< 0xA0; i
++)
9167 iso_code_class
[i
] = ISO_control_1
;
9168 for (i
= 0xA1; i
< 0xFF; i
++)
9169 iso_code_class
[i
] = ISO_graphic_plane_1
;
9170 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9171 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9172 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9173 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9174 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9175 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9176 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9177 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9178 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9180 for (i
= 0; i
< 256; i
++)
9182 emacs_mule_bytes
[i
] = 1;
9184 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9185 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9186 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9187 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9195 staticpro (&Vcoding_system_hash_table
);
9197 Lisp_Object args
[2];
9200 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9203 staticpro (&Vsjis_coding_system
);
9204 Vsjis_coding_system
= Qnil
;
9206 staticpro (&Vbig5_coding_system
);
9207 Vbig5_coding_system
= Qnil
;
9209 staticpro (&Vcode_conversion_reused_workbuf
);
9210 Vcode_conversion_reused_workbuf
= Qnil
;
9212 staticpro (&Vcode_conversion_workbuf_name
);
9213 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9215 reused_workbuf_in_use
= 0;
9217 DEFSYM (Qcharset
, "charset");
9218 DEFSYM (Qtarget_idx
, "target-idx");
9219 DEFSYM (Qcoding_system_history
, "coding-system-history");
9220 Fset (Qcoding_system_history
, Qnil
);
9222 /* Target FILENAME is the first argument. */
9223 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9224 /* Target FILENAME is the third argument. */
9225 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9227 DEFSYM (Qcall_process
, "call-process");
9228 /* Target PROGRAM is the first argument. */
9229 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9231 DEFSYM (Qcall_process_region
, "call-process-region");
9232 /* Target PROGRAM is the third argument. */
9233 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9235 DEFSYM (Qstart_process
, "start-process");
9236 /* Target PROGRAM is the third argument. */
9237 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9239 DEFSYM (Qopen_network_stream
, "open-network-stream");
9240 /* Target SERVICE is the fourth argument. */
9241 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9243 DEFSYM (Qcoding_system
, "coding-system");
9244 DEFSYM (Qcoding_aliases
, "coding-aliases");
9246 DEFSYM (Qeol_type
, "eol-type");
9247 DEFSYM (Qunix
, "unix");
9248 DEFSYM (Qdos
, "dos");
9250 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9251 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9252 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9253 DEFSYM (Qdefault_char
, "default-char");
9254 DEFSYM (Qundecided
, "undecided");
9255 DEFSYM (Qno_conversion
, "no-conversion");
9256 DEFSYM (Qraw_text
, "raw-text");
9258 DEFSYM (Qiso_2022
, "iso-2022");
9260 DEFSYM (Qutf_8
, "utf-8");
9261 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9263 DEFSYM (Qutf_16
, "utf-16");
9264 DEFSYM (Qbig
, "big");
9265 DEFSYM (Qlittle
, "little");
9267 DEFSYM (Qshift_jis
, "shift-jis");
9268 DEFSYM (Qbig5
, "big5");
9270 DEFSYM (Qcoding_system_p
, "coding-system-p");
9272 DEFSYM (Qcoding_system_error
, "coding-system-error");
9273 Fput (Qcoding_system_error
, Qerror_conditions
,
9274 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9275 Fput (Qcoding_system_error
, Qerror_message
,
9276 build_string ("Invalid coding system"));
9278 /* Intern this now in case it isn't already done.
9279 Setting this variable twice is harmless.
9280 But don't staticpro it here--that is done in alloc.c. */
9281 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9283 DEFSYM (Qtranslation_table
, "translation-table");
9284 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9285 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9286 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9287 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9289 DEFSYM (Qvalid_codes
, "valid-codes");
9291 DEFSYM (Qemacs_mule
, "emacs-mule");
9293 DEFSYM (QCcategory
, ":category");
9294 DEFSYM (QCmnemonic
, ":mnemonic");
9295 DEFSYM (QCdefalut_char
, ":default-char");
9296 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9297 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9298 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9299 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9300 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
9302 Vcoding_category_table
9303 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9304 staticpro (&Vcoding_category_table
);
9305 /* Followings are target of code detection. */
9306 ASET (Vcoding_category_table
, coding_category_iso_7
,
9307 intern ("coding-category-iso-7"));
9308 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9309 intern ("coding-category-iso-7-tight"));
9310 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9311 intern ("coding-category-iso-8-1"));
9312 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9313 intern ("coding-category-iso-8-2"));
9314 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9315 intern ("coding-category-iso-7-else"));
9316 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9317 intern ("coding-category-iso-8-else"));
9318 ASET (Vcoding_category_table
, coding_category_utf_8
,
9319 intern ("coding-category-utf-8"));
9320 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9321 intern ("coding-category-utf-16-be"));
9322 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9323 intern ("coding-category-utf-16-auto"));
9324 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9325 intern ("coding-category-utf-16-le"));
9326 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9327 intern ("coding-category-utf-16-be-nosig"));
9328 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9329 intern ("coding-category-utf-16-le-nosig"));
9330 ASET (Vcoding_category_table
, coding_category_charset
,
9331 intern ("coding-category-charset"));
9332 ASET (Vcoding_category_table
, coding_category_sjis
,
9333 intern ("coding-category-sjis"));
9334 ASET (Vcoding_category_table
, coding_category_big5
,
9335 intern ("coding-category-big5"));
9336 ASET (Vcoding_category_table
, coding_category_ccl
,
9337 intern ("coding-category-ccl"));
9338 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9339 intern ("coding-category-emacs-mule"));
9340 /* Followings are NOT target of code detection. */
9341 ASET (Vcoding_category_table
, coding_category_raw_text
,
9342 intern ("coding-category-raw-text"));
9343 ASET (Vcoding_category_table
, coding_category_undecided
,
9344 intern ("coding-category-undecided"));
9346 DEFSYM (Qinsufficient_source
, "insufficient-source");
9347 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9348 DEFSYM (Qinvalid_source
, "invalid-source");
9349 DEFSYM (Qinterrupted
, "interrupted");
9350 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9351 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
9353 defsubr (&Scoding_system_p
);
9354 defsubr (&Sread_coding_system
);
9355 defsubr (&Sread_non_nil_coding_system
);
9356 defsubr (&Scheck_coding_system
);
9357 defsubr (&Sdetect_coding_region
);
9358 defsubr (&Sdetect_coding_string
);
9359 defsubr (&Sfind_coding_systems_region_internal
);
9360 defsubr (&Sunencodable_char_position
);
9361 defsubr (&Scheck_coding_systems_region
);
9362 defsubr (&Sdecode_coding_region
);
9363 defsubr (&Sencode_coding_region
);
9364 defsubr (&Sdecode_coding_string
);
9365 defsubr (&Sencode_coding_string
);
9366 defsubr (&Sdecode_sjis_char
);
9367 defsubr (&Sencode_sjis_char
);
9368 defsubr (&Sdecode_big5_char
);
9369 defsubr (&Sencode_big5_char
);
9370 defsubr (&Sset_terminal_coding_system_internal
);
9371 defsubr (&Sset_safe_terminal_coding_system_internal
);
9372 defsubr (&Sterminal_coding_system
);
9373 defsubr (&Sset_keyboard_coding_system_internal
);
9374 defsubr (&Skeyboard_coding_system
);
9375 defsubr (&Sfind_operation_coding_system
);
9376 defsubr (&Sset_coding_system_priority
);
9377 defsubr (&Sdefine_coding_system_internal
);
9378 defsubr (&Sdefine_coding_system_alias
);
9379 defsubr (&Scoding_system_put
);
9380 defsubr (&Scoding_system_base
);
9381 defsubr (&Scoding_system_plist
);
9382 defsubr (&Scoding_system_aliases
);
9383 defsubr (&Scoding_system_eol_type
);
9384 defsubr (&Scoding_system_priority_list
);
9386 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9387 doc
: /* List of coding systems.
9389 Do not alter the value of this variable manually. This variable should be
9390 updated by the functions `define-coding-system' and
9391 `define-coding-system-alias'. */);
9392 Vcoding_system_list
= Qnil
;
9394 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9395 doc
: /* Alist of coding system names.
9396 Each element is one element list of coding system name.
9397 This variable is given to `completing-read' as TABLE argument.
9399 Do not alter the value of this variable manually. This variable should be
9400 updated by the functions `make-coding-system' and
9401 `define-coding-system-alias'. */);
9402 Vcoding_system_alist
= Qnil
;
9404 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9405 doc
: /* List of coding-categories (symbols) ordered by priority.
9407 On detecting a coding system, Emacs tries code detection algorithms
9408 associated with each coding-category one by one in this order. When
9409 one algorithm agrees with a byte sequence of source text, the coding
9410 system bound to the corresponding coding-category is selected.
9412 Don't modify this variable directly, but use `set-coding-priority'. */);
9416 Vcoding_category_list
= Qnil
;
9417 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9418 Vcoding_category_list
9419 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9420 Vcoding_category_list
);
9423 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9424 doc
: /* Specify the coding system for read operations.
9425 It is useful to bind this variable with `let', but do not set it globally.
9426 If the value is a coding system, it is used for decoding on read operation.
9427 If not, an appropriate element is used from one of the coding system alists:
9428 There are three such tables, `file-coding-system-alist',
9429 `process-coding-system-alist', and `network-coding-system-alist'. */);
9430 Vcoding_system_for_read
= Qnil
;
9432 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9433 doc
: /* Specify the coding system for write operations.
9434 Programs bind this variable with `let', but you should not set it globally.
9435 If the value is a coding system, it is used for encoding of output,
9436 when writing it to a file and when sending it to a file or subprocess.
9438 If this does not specify a coding system, an appropriate element
9439 is used from one of the coding system alists:
9440 There are three such tables, `file-coding-system-alist',
9441 `process-coding-system-alist', and `network-coding-system-alist'.
9442 For output to files, if the above procedure does not specify a coding system,
9443 the value of `buffer-file-coding-system' is used. */);
9444 Vcoding_system_for_write
= Qnil
;
9446 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9448 Coding system used in the latest file or process I/O. */);
9449 Vlast_coding_system_used
= Qnil
;
9451 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9453 Error status of the last code conversion.
9455 When an error was detected in the last code conversion, this variable
9456 is set to one of the following symbols.
9457 `insufficient-source'
9461 `insufficient-memory'
9462 When no error was detected, the value doesn't change. So, to check
9463 the error status of a code conversion by this variable, you must
9464 explicitly set this variable to nil before performing code
9466 Vlast_code_conversion_error
= Qnil
;
9468 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9470 *Non-nil means always inhibit code conversion of end-of-line format.
9471 See info node `Coding Systems' and info node `Text and Binary' concerning
9472 such conversion. */);
9473 inhibit_eol_conversion
= 0;
9475 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9477 Non-nil means process buffer inherits coding system of process output.
9478 Bind it to t if the process output is to be treated as if it were a file
9479 read from some filesystem. */);
9480 inherit_process_coding_system
= 0;
9482 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9484 Alist to decide a coding system to use for a file I/O operation.
9485 The format is ((PATTERN . VAL) ...),
9486 where PATTERN is a regular expression matching a file name,
9487 VAL is a coding system, a cons of coding systems, or a function symbol.
9488 If VAL is a coding system, it is used for both decoding and encoding
9490 If VAL is a cons of coding systems, the car part is used for decoding,
9491 and the cdr part is used for encoding.
9492 If VAL is a function symbol, the function must return a coding system
9493 or a cons of coding systems which are used as above. The function gets
9494 the arguments with which `find-operation-coding-systems' was called.
9496 See also the function `find-operation-coding-system'
9497 and the variable `auto-coding-alist'. */);
9498 Vfile_coding_system_alist
= Qnil
;
9500 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9502 Alist to decide a coding system to use for a process I/O operation.
9503 The format is ((PATTERN . VAL) ...),
9504 where PATTERN is a regular expression matching a program name,
9505 VAL is a coding system, a cons of coding systems, or a function symbol.
9506 If VAL is a coding system, it is used for both decoding what received
9507 from the program and encoding what sent to the program.
9508 If VAL is a cons of coding systems, the car part is used for decoding,
9509 and the cdr part is used for encoding.
9510 If VAL is a function symbol, the function must return a coding system
9511 or a cons of coding systems which are used as above.
9513 See also the function `find-operation-coding-system'. */);
9514 Vprocess_coding_system_alist
= Qnil
;
9516 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9518 Alist to decide a coding system to use for a network I/O operation.
9519 The format is ((PATTERN . VAL) ...),
9520 where PATTERN is a regular expression matching a network service name
9521 or is a port number to connect to,
9522 VAL is a coding system, a cons of coding systems, or a function symbol.
9523 If VAL is a coding system, it is used for both decoding what received
9524 from the network stream and encoding what sent to the network stream.
9525 If VAL is a cons of coding systems, the car part is used for decoding,
9526 and the cdr part is used for encoding.
9527 If VAL is a function symbol, the function must return a coding system
9528 or a cons of coding systems which are used as above.
9530 See also the function `find-operation-coding-system'. */);
9531 Vnetwork_coding_system_alist
= Qnil
;
9533 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9534 doc
: /* Coding system to use with system messages.
9535 Also used for decoding keyboard input on X Window system. */);
9536 Vlocale_coding_system
= Qnil
;
9538 /* The eol mnemonics are reset in startup.el system-dependently. */
9539 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9541 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9542 eol_mnemonic_unix
= build_string (":");
9544 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9546 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9547 eol_mnemonic_dos
= build_string ("\\");
9549 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9551 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9552 eol_mnemonic_mac
= build_string ("/");
9554 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9556 *String displayed in mode line when end-of-line format is not yet determined. */);
9557 eol_mnemonic_undecided
= build_string (":");
9559 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
9561 *Non-nil enables character translation while encoding and decoding. */);
9562 Venable_character_translation
= Qt
;
9564 DEFVAR_LISP ("standard-translation-table-for-decode",
9565 &Vstandard_translation_table_for_decode
,
9566 doc
: /* Table for translating characters while decoding. */);
9567 Vstandard_translation_table_for_decode
= Qnil
;
9569 DEFVAR_LISP ("standard-translation-table-for-encode",
9570 &Vstandard_translation_table_for_encode
,
9571 doc
: /* Table for translating characters while encoding. */);
9572 Vstandard_translation_table_for_encode
= Qnil
;
9574 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
9575 doc
: /* Alist of charsets vs revision numbers.
9576 While encoding, if a charset (car part of an element) is found,
9577 designate it with the escape sequence identifying revision (cdr part
9578 of the element). */);
9579 Vcharset_revision_table
= Qnil
;
9581 DEFVAR_LISP ("default-process-coding-system",
9582 &Vdefault_process_coding_system
,
9583 doc
: /* Cons of coding systems used for process I/O by default.
9584 The car part is used for decoding a process output,
9585 the cdr part is used for encoding a text to be sent to a process. */);
9586 Vdefault_process_coding_system
= Qnil
;
9588 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
9590 Table of extra Latin codes in the range 128..159 (inclusive).
9591 This is a vector of length 256.
9592 If Nth element is non-nil, the existence of code N in a file
9593 \(or output of subprocess) doesn't prevent it to be detected as
9594 a coding system of ISO 2022 variant which has a flag
9595 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9596 or reading output of a subprocess.
9597 Only 128th through 159th elements has a meaning. */);
9598 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
9600 DEFVAR_LISP ("select-safe-coding-system-function",
9601 &Vselect_safe_coding_system_function
,
9603 Function to call to select safe coding system for encoding a text.
9605 If set, this function is called to force a user to select a proper
9606 coding system which can encode the text in the case that a default
9607 coding system used in each operation can't encode the text.
9609 The default value is `select-safe-coding-system' (which see). */);
9610 Vselect_safe_coding_system_function
= Qnil
;
9612 DEFVAR_BOOL ("coding-system-require-warning",
9613 &coding_system_require_warning
,
9614 doc
: /* Internal use only.
9615 If non-nil, on writing a file, `select-safe-coding-system-function' is
9616 called even if `coding-system-for-write' is non-nil. The command
9617 `universal-coding-system-argument' binds this variable to t temporarily. */);
9618 coding_system_require_warning
= 0;
9621 DEFVAR_BOOL ("inhibit-iso-escape-detection",
9622 &inhibit_iso_escape_detection
,
9624 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
9626 By default, on reading a file, Emacs tries to detect how the text is
9627 encoded. This code detection is sensitive to escape sequences. If
9628 the sequence is valid as ISO2022, the code is determined as one of
9629 the ISO2022 encodings, and the file is decoded by the corresponding
9630 coding system (e.g. `iso-2022-7bit').
9632 However, there may be a case that you want to read escape sequences in
9633 a file as is. In such a case, you can set this variable to non-nil.
9634 Then, as the code detection ignores any escape sequences, no file is
9635 detected as encoded in some ISO2022 encoding. The result is that all
9636 escape sequences become visible in a buffer.
9638 The default value is nil, and it is strongly recommended not to change
9639 it. That is because many Emacs Lisp source files that contain
9640 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9641 in Emacs's distribution, and they won't be decoded correctly on
9642 reading if you suppress escape sequence detection.
9644 The other way to read escape sequences in a file without decoding is
9645 to explicitly specify some coding system that doesn't use ISO2022's
9646 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
9647 inhibit_iso_escape_detection
= 0;
9649 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
9650 doc
: /* Char table for translating self-inserting characters.
9651 This is applied to the result of input methods, not their input. See also
9652 `keyboard-translate-table'. */);
9653 Vtranslation_table_for_input
= Qnil
;
9656 Lisp_Object args
[coding_arg_max
];
9657 Lisp_Object plist
[16];
9660 for (i
= 0; i
< coding_arg_max
; i
++)
9663 plist
[0] = intern (":name");
9664 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
9665 plist
[2] = intern (":mnemonic");
9666 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
9667 plist
[4] = intern (":coding-type");
9668 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
9669 plist
[6] = intern (":ascii-compatible-p");
9670 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
9671 plist
[8] = intern (":default-char");
9672 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
9673 plist
[10] = intern (":for-unibyte");
9674 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
9675 plist
[12] = intern (":docstring");
9676 plist
[13] = build_string ("Do no conversion.\n\
9678 When you visit a file with this coding, the file is read into a\n\
9679 unibyte buffer as is, thus each byte of a file is treated as a\n\
9681 plist
[14] = intern (":eol-type");
9682 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
9683 args
[coding_arg_plist
] = Flist (16, plist
);
9684 Fdefine_coding_system_internal (coding_arg_max
, args
);
9686 plist
[1] = args
[coding_arg_name
] = Qundecided
;
9687 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
9688 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
9689 /* This is already set.
9690 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
9691 plist
[8] = intern (":charset-list");
9692 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
9693 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
9694 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9695 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
9696 args
[coding_arg_plist
] = Flist (16, plist
);
9697 Fdefine_coding_system_internal (coding_arg_max
, args
);
9700 setup_coding_system (Qno_conversion
, &keyboard_coding
);
9701 setup_coding_system (Qundecided
, &terminal_coding
);
9702 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
9707 for (i
= 0; i
< coding_category_max
; i
++)
9708 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
9713 emacs_strerror (error_number
)
9718 synchronize_system_messages_locale ();
9719 str
= strerror (error_number
);
9721 if (! NILP (Vlocale_coding_system
))
9723 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
9724 Vlocale_coding_system
,
9726 str
= (char *) SDATA (dec
);
9734 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
9735 (do not change this comment) */