1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2011 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 3 of the License, or
16 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
26 /*** TABLE OF CONTENTS ***
30 2. Emacs' internal format (emacs-utf-8) handlers
33 5. Charset-base coding systems handlers
34 6. emacs-mule (old Emacs' internal format) handlers
36 8. Shift-JIS and BIG5 handlers
38 10. C library functions
39 11. Emacs Lisp library functions
44 /*** 0. General comments ***
49 A coding system is an object for an encoding mechanism that contains
50 information about how to convert byte sequences to character
51 sequences and vice versa. When we say "decode", it means converting
52 a byte sequence of a specific coding system into a character
53 sequence that is represented by Emacs' internal coding system
54 `emacs-utf-8', and when we say "encode", it means converting a
55 character sequence of emacs-utf-8 to a byte sequence of a specific
58 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
59 C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
71 o Charset-base coding system
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
77 o Old Emacs internal format (emacs-mule)
79 The coding system adopted by old versions of Emacs (20 and 21).
81 o ISO2022-base coding system
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
117 Like raw text, but don't do end-of-line conversion.
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. MacOS's format is usually one byte of
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
150 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
152 Below is the template of these functions. */
156 detect_coding_XXX (struct coding_system
*coding
,
157 struct coding_detection_info
*detect_info
)
159 const unsigned char *src
= coding
->source
;
160 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
161 int multibytep
= coding
->src_multibyte
;
162 EMACS_INT consumed_chars
= 0;
168 /* Get one byte from the source. If the source is exhausted, jump
169 to no_more_source:. */
172 if (! __C_conforms_to_XXX___ (c
))
174 if (! __C_strongly_suggests_XXX__ (c
))
175 found
= CATEGORY_MASK_XXX
;
177 /* The byte sequence is invalid for XXX. */
178 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
182 /* The source exhausted successfully. */
183 detect_info
->found
|= found
;
188 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
190 These functions decode a byte sequence specified as a source by
191 CODING. The resulting multibyte text goes to a place pointed to by
192 CODING->charbuf, the length of which should not exceed
193 CODING->charbuf_size;
195 These functions set the information of original and decoded texts in
196 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
197 They also set CODING->result to one of CODING_RESULT_XXX indicating
198 how the decoding is finished.
200 Below is the template of these functions. */
204 decode_coding_XXXX (struct coding_system
*coding
)
206 const unsigned char *src
= coding
->source
+ coding
->consumed
;
207 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
208 /* SRC_BASE remembers the start position in source in each loop.
209 The loop will be exited when there's not enough source code, or
210 when there's no room in CHARBUF for a decoded character. */
211 const unsigned char *src_base
;
212 /* A buffer to produce decoded characters. */
213 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
214 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
215 int multibytep
= coding
->src_multibyte
;
220 if (charbuf
< charbuf_end
)
221 /* No more room to produce a decoded character. */
228 if (src_base
< src_end
229 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
230 /* If the source ends by partial bytes to construct a character,
231 treat them as eight-bit raw data. */
232 while (src_base
< src_end
&& charbuf
< charbuf_end
)
233 *charbuf
++ = *src_base
++;
234 /* Remember how many bytes and characters we consumed. If the
235 source is multibyte, the bytes and chars are not identical. */
236 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
237 /* Remember how many characters we produced. */
238 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
242 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
244 These functions encode SRC_BYTES length text at SOURCE of Emacs'
245 internal multibyte format by CODING. The resulting byte sequence
246 goes to a place pointed to by DESTINATION, the length of which
247 should not exceed DST_BYTES.
249 These functions set the information of original and encoded texts in
250 the members produced, produced_char, consumed, and consumed_char of
251 the structure *CODING. They also set the member result to one of
252 CODING_RESULT_XXX indicating how the encoding finished.
254 DST_BYTES zero means that source area and destination area are
255 overlapped, which means that we can produce a encoded text until it
256 reaches at the head of not-yet-encoded source text.
258 Below is a template of these functions. */
261 encode_coding_XXX (struct coding_system
*coding
)
263 int multibytep
= coding
->dst_multibyte
;
264 int *charbuf
= coding
->charbuf
;
265 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
266 unsigned char *dst
= coding
->destination
+ coding
->produced
;
267 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
268 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
269 EMACS_INT produced_chars
= 0;
271 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
274 /* Encode C into DST, and increment DST. */
276 label_no_more_destination
:
277 /* How many chars and bytes we produced. */
278 coding
->produced_char
+= produced_chars
;
279 coding
->produced
= dst
- coding
->destination
;
284 /*** 1. Preamble ***/
292 #include "character.h"
295 #include "composite.h"
299 #include "termhooks.h"
301 Lisp_Object Vcoding_system_hash_table
;
303 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
304 Lisp_Object Qunix
, Qdos
;
305 Lisp_Object Qbuffer_file_coding_system
;
306 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
307 Lisp_Object Qdefault_char
;
308 Lisp_Object Qno_conversion
, Qundecided
;
309 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
310 Lisp_Object Qbig
, Qlittle
;
311 Lisp_Object Qcoding_system_history
;
312 Lisp_Object Qvalid_codes
;
313 Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
314 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
315 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
316 Lisp_Object QCascii_compatible_p
;
318 Lisp_Object Qcall_process
, Qcall_process_region
;
319 Lisp_Object Qstart_process
, Qopen_network_stream
;
320 Lisp_Object Qtarget_idx
;
322 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
323 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
325 /* If a symbol has this property, evaluate the value to define the
326 symbol as a coding system. */
327 static Lisp_Object Qcoding_system_define_form
;
329 /* Format of end-of-line decided by system. This is Qunix on
330 Unix and Mac, Qdos on DOS/Windows.
331 This has an effect only for external encoding (i.e. for output to
332 file and process), not for in-buffer or Lisp string encoding. */
333 static Lisp_Object system_eol_type
;
337 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
339 /* Coding system emacs-mule and raw-text are for converting only
340 end-of-line format. */
341 Lisp_Object Qemacs_mule
, Qraw_text
;
342 Lisp_Object Qutf_8_emacs
;
344 /* Coding-systems are handed between Emacs Lisp programs and C internal
345 routines by the following three variables. */
346 /* Coding system to be used to encode text for terminal display when
347 terminal coding system is nil. */
348 struct coding_system safe_terminal_coding
;
352 Lisp_Object Qtranslation_table
;
353 Lisp_Object Qtranslation_table_id
;
354 Lisp_Object Qtranslation_table_for_decode
;
355 Lisp_Object Qtranslation_table_for_encode
;
357 /* Two special coding systems. */
358 Lisp_Object Vsjis_coding_system
;
359 Lisp_Object Vbig5_coding_system
;
361 /* ISO2022 section */
363 #define CODING_ISO_INITIAL(coding, reg) \
364 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
365 coding_attr_iso_initial), \
369 #define CODING_ISO_REQUEST(coding, charset_id) \
370 (((charset_id) <= (coding)->max_charset_id \
371 ? ((coding)->safe_charsets[charset_id] != 255 \
372 ? (coding)->safe_charsets[charset_id] \
377 #define CODING_ISO_FLAGS(coding) \
378 ((coding)->spec.iso_2022.flags)
379 #define CODING_ISO_DESIGNATION(coding, reg) \
380 ((coding)->spec.iso_2022.current_designation[reg])
381 #define CODING_ISO_INVOCATION(coding, plane) \
382 ((coding)->spec.iso_2022.current_invocation[plane])
383 #define CODING_ISO_SINGLE_SHIFTING(coding) \
384 ((coding)->spec.iso_2022.single_shifting)
385 #define CODING_ISO_BOL(coding) \
386 ((coding)->spec.iso_2022.bol)
387 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
388 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
389 #define CODING_ISO_CMP_STATUS(coding) \
390 (&(coding)->spec.iso_2022.cmp_status)
391 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
392 ((coding)->spec.iso_2022.ctext_extended_segment_len)
393 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
394 ((coding)->spec.iso_2022.embedded_utf_8)
396 /* Control characters of ISO2022. */
397 /* code */ /* function */
398 #define ISO_CODE_SO 0x0E /* shift-out */
399 #define ISO_CODE_SI 0x0F /* shift-in */
400 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
401 #define ISO_CODE_ESC 0x1B /* escape */
402 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
403 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
404 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
406 /* All code (1-byte) of ISO2022 is classified into one of the
408 enum iso_code_class_type
410 ISO_control_0
, /* Control codes in the range
411 0x00..0x1F and 0x7F, except for the
412 following 5 codes. */
413 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
414 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
415 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
416 ISO_escape
, /* ISO_CODE_SO (0x1B) */
417 ISO_control_1
, /* Control codes in the range
418 0x80..0x9F, except for the
419 following 3 codes. */
420 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
421 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
422 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
423 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
424 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
425 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
426 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
429 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
430 `iso-flags' attribute of an iso2022 coding system. */
432 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
433 instead of the correct short-form sequence (e.g. ESC $ A). */
434 #define CODING_ISO_FLAG_LONG_FORM 0x0001
436 /* If set, reset graphic planes and registers at end-of-line to the
438 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
440 /* If set, reset graphic planes and registers before any control
441 characters to the initial state. */
442 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
444 /* If set, encode by 7-bit environment. */
445 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
447 /* If set, use locking-shift function. */
448 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
450 /* If set, use single-shift function. Overwrite
451 CODING_ISO_FLAG_LOCKING_SHIFT. */
452 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
454 /* If set, use designation escape sequence. */
455 #define CODING_ISO_FLAG_DESIGNATION 0x0040
457 /* If set, produce revision number sequence. */
458 #define CODING_ISO_FLAG_REVISION 0x0080
460 /* If set, produce ISO6429's direction specifying sequence. */
461 #define CODING_ISO_FLAG_DIRECTION 0x0100
463 /* If set, assume designation states are reset at beginning of line on
465 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
467 /* If set, designation sequence should be placed at beginning of line
469 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
471 /* If set, do not encode unsafe characters on output. */
472 #define CODING_ISO_FLAG_SAFE 0x0800
474 /* If set, extra latin codes (128..159) are accepted as a valid code
476 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
478 #define CODING_ISO_FLAG_COMPOSITION 0x2000
480 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
482 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
484 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
486 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
488 /* A character to be produced on output if encoding of the original
489 character is prohibited by CODING_ISO_FLAG_SAFE. */
490 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
493 #define CODING_UTF_8_BOM(coding) \
494 ((coding)->spec.utf_8_bom)
497 #define CODING_UTF_16_BOM(coding) \
498 ((coding)->spec.utf_16.bom)
500 #define CODING_UTF_16_ENDIAN(coding) \
501 ((coding)->spec.utf_16.endian)
503 #define CODING_UTF_16_SURROGATE(coding) \
504 ((coding)->spec.utf_16.surrogate)
508 #define CODING_CCL_DECODER(coding) \
509 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
510 #define CODING_CCL_ENCODER(coding) \
511 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
512 #define CODING_CCL_VALIDS(coding) \
513 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
515 /* Index for each coding category in `coding_categories' */
519 coding_category_iso_7
,
520 coding_category_iso_7_tight
,
521 coding_category_iso_8_1
,
522 coding_category_iso_8_2
,
523 coding_category_iso_7_else
,
524 coding_category_iso_8_else
,
525 coding_category_utf_8_auto
,
526 coding_category_utf_8_nosig
,
527 coding_category_utf_8_sig
,
528 coding_category_utf_16_auto
,
529 coding_category_utf_16_be
,
530 coding_category_utf_16_le
,
531 coding_category_utf_16_be_nosig
,
532 coding_category_utf_16_le_nosig
,
533 coding_category_charset
,
534 coding_category_sjis
,
535 coding_category_big5
,
537 coding_category_emacs_mule
,
538 /* All above are targets of code detection. */
539 coding_category_raw_text
,
540 coding_category_undecided
,
544 /* Definitions of flag bits used in detect_coding_XXXX. */
545 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
546 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
547 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
548 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
549 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
550 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
551 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
552 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
553 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
554 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
555 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
556 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
557 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
558 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
559 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
560 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
561 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
562 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
563 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
564 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
566 /* This value is returned if detect_coding_mask () find nothing other
567 than ASCII characters. */
568 #define CATEGORY_MASK_ANY \
569 (CATEGORY_MASK_ISO_7 \
570 | CATEGORY_MASK_ISO_7_TIGHT \
571 | CATEGORY_MASK_ISO_8_1 \
572 | CATEGORY_MASK_ISO_8_2 \
573 | CATEGORY_MASK_ISO_7_ELSE \
574 | CATEGORY_MASK_ISO_8_ELSE \
575 | CATEGORY_MASK_UTF_8_AUTO \
576 | CATEGORY_MASK_UTF_8_NOSIG \
577 | CATEGORY_MASK_UTF_8_SIG \
578 | CATEGORY_MASK_UTF_16_AUTO \
579 | CATEGORY_MASK_UTF_16_BE \
580 | CATEGORY_MASK_UTF_16_LE \
581 | CATEGORY_MASK_UTF_16_BE_NOSIG \
582 | CATEGORY_MASK_UTF_16_LE_NOSIG \
583 | CATEGORY_MASK_CHARSET \
584 | CATEGORY_MASK_SJIS \
585 | CATEGORY_MASK_BIG5 \
586 | CATEGORY_MASK_CCL \
587 | CATEGORY_MASK_EMACS_MULE)
590 #define CATEGORY_MASK_ISO_7BIT \
591 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
593 #define CATEGORY_MASK_ISO_8BIT \
594 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
596 #define CATEGORY_MASK_ISO_ELSE \
597 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
599 #define CATEGORY_MASK_ISO_ESCAPE \
600 (CATEGORY_MASK_ISO_7 \
601 | CATEGORY_MASK_ISO_7_TIGHT \
602 | CATEGORY_MASK_ISO_7_ELSE \
603 | CATEGORY_MASK_ISO_8_ELSE)
605 #define CATEGORY_MASK_ISO \
606 ( CATEGORY_MASK_ISO_7BIT \
607 | CATEGORY_MASK_ISO_8BIT \
608 | CATEGORY_MASK_ISO_ELSE)
610 #define CATEGORY_MASK_UTF_16 \
611 (CATEGORY_MASK_UTF_16_AUTO \
612 | CATEGORY_MASK_UTF_16_BE \
613 | CATEGORY_MASK_UTF_16_LE \
614 | CATEGORY_MASK_UTF_16_BE_NOSIG \
615 | CATEGORY_MASK_UTF_16_LE_NOSIG)
617 #define CATEGORY_MASK_UTF_8 \
618 (CATEGORY_MASK_UTF_8_AUTO \
619 | CATEGORY_MASK_UTF_8_NOSIG \
620 | CATEGORY_MASK_UTF_8_SIG)
622 /* Table of coding categories (Lisp symbols). This variable is for
623 internal use only. */
624 static Lisp_Object Vcoding_category_table
;
626 /* Table of coding-categories ordered by priority. */
627 static enum coding_category coding_priorities
[coding_category_max
];
629 /* Nth element is a coding context for the coding system bound to the
630 Nth coding category. */
631 static struct coding_system coding_categories
[coding_category_max
];
633 /*** Commonly used macros and functions ***/
636 #define min(a, b) ((a) < (b) ? (a) : (b))
639 #define max(a, b) ((a) > (b) ? (a) : (b))
642 #define CODING_GET_INFO(coding, attrs, charset_list) \
644 (attrs) = CODING_ID_ATTRS ((coding)->id); \
645 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
649 /* Safely get one byte from the source text pointed by SRC which ends
650 at SRC_END, and set C to that byte. If there are not enough bytes
651 in the source, it jumps to `no_more_source'. If multibytep is
652 nonzero, and a multibyte character is found at SRC, set C to the
653 negative value of the character code. The caller should declare
654 and set these variables appropriately in advance:
655 src, src_end, multibytep */
657 #define ONE_MORE_BYTE(c) \
659 if (src == src_end) \
661 if (src_base < src) \
662 record_conversion_result \
663 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
664 goto no_more_source; \
667 if (multibytep && (c & 0x80)) \
669 if ((c & 0xFE) == 0xC0) \
670 c = ((c & 1) << 6) | *src++; \
674 c = - string_char (src, &src, NULL); \
675 record_conversion_result \
676 (coding, CODING_RESULT_INVALID_SRC); \
682 /* Safely get two bytes from the source text pointed by SRC which ends
683 at SRC_END, and set C1 and C2 to those bytes while skipping the
684 heading multibyte characters. If there are not enough bytes in the
685 source, it jumps to `no_more_source'. If multibytep is nonzero and
686 a multibyte character is found for C2, set C2 to the negative value
687 of the character code. The caller should declare and set these
688 variables appropriately in advance:
689 src, src_end, multibytep
690 It is intended that this macro is used in detect_coding_utf_16. */
692 #define TWO_MORE_BYTES(c1, c2) \
695 if (src == src_end) \
696 goto no_more_source; \
698 if (multibytep && (c1 & 0x80)) \
700 if ((c1 & 0xFE) == 0xC0) \
701 c1 = ((c1 & 1) << 6) | *src++; \
704 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
709 if (src == src_end) \
710 goto no_more_source; \
712 if (multibytep && (c2 & 0x80)) \
714 if ((c2 & 0xFE) == 0xC0) \
715 c2 = ((c2 & 1) << 6) | *src++; \
722 /* Store a byte C in the place pointed by DST and increment DST to the
723 next free point, and increment PRODUCED_CHARS. The caller should
724 assure that C is 0..127, and declare and set the variable `dst'
725 appropriately in advance.
729 #define EMIT_ONE_ASCII_BYTE(c) \
736 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
738 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
740 produced_chars += 2; \
741 *dst++ = (c1), *dst++ = (c2); \
745 /* Store a byte C in the place pointed by DST and increment DST to the
746 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
747 nonzero, store in an appropriate multibyte from. The caller should
748 declare and set the variables `dst' and `multibytep' appropriately
751 #define EMIT_ONE_BYTE(c) \
758 ch = BYTE8_TO_CHAR (ch); \
759 CHAR_STRING_ADVANCE (ch, dst); \
766 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
768 #define EMIT_TWO_BYTES(c1, c2) \
770 produced_chars += 2; \
777 ch = BYTE8_TO_CHAR (ch); \
778 CHAR_STRING_ADVANCE (ch, dst); \
781 ch = BYTE8_TO_CHAR (ch); \
782 CHAR_STRING_ADVANCE (ch, dst); \
792 #define EMIT_THREE_BYTES(c1, c2, c3) \
794 EMIT_ONE_BYTE (c1); \
795 EMIT_TWO_BYTES (c2, c3); \
799 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
801 EMIT_TWO_BYTES (c1, c2); \
802 EMIT_TWO_BYTES (c3, c4); \
806 /* Prototypes for static functions. */
807 static void record_conversion_result (struct coding_system
*coding
,
808 enum coding_result_code result
);
809 static int detect_coding_utf_8 (struct coding_system
*,
810 struct coding_detection_info
*info
);
811 static void decode_coding_utf_8 (struct coding_system
*);
812 static int encode_coding_utf_8 (struct coding_system
*);
814 static int detect_coding_utf_16 (struct coding_system
*,
815 struct coding_detection_info
*info
);
816 static void decode_coding_utf_16 (struct coding_system
*);
817 static int encode_coding_utf_16 (struct coding_system
*);
819 static int detect_coding_iso_2022 (struct coding_system
*,
820 struct coding_detection_info
*info
);
821 static void decode_coding_iso_2022 (struct coding_system
*);
822 static int encode_coding_iso_2022 (struct coding_system
*);
824 static int detect_coding_emacs_mule (struct coding_system
*,
825 struct coding_detection_info
*info
);
826 static void decode_coding_emacs_mule (struct coding_system
*);
827 static int encode_coding_emacs_mule (struct coding_system
*);
829 static int detect_coding_sjis (struct coding_system
*,
830 struct coding_detection_info
*info
);
831 static void decode_coding_sjis (struct coding_system
*);
832 static int encode_coding_sjis (struct coding_system
*);
834 static int detect_coding_big5 (struct coding_system
*,
835 struct coding_detection_info
*info
);
836 static void decode_coding_big5 (struct coding_system
*);
837 static int encode_coding_big5 (struct coding_system
*);
839 static int detect_coding_ccl (struct coding_system
*,
840 struct coding_detection_info
*info
);
841 static void decode_coding_ccl (struct coding_system
*);
842 static int encode_coding_ccl (struct coding_system
*);
844 static void decode_coding_raw_text (struct coding_system
*);
845 static int encode_coding_raw_text (struct coding_system
*);
847 static void coding_set_source (struct coding_system
*);
848 static void coding_set_destination (struct coding_system
*);
849 static void coding_alloc_by_realloc (struct coding_system
*, EMACS_INT
);
850 static void coding_alloc_by_making_gap (struct coding_system
*,
851 EMACS_INT
, EMACS_INT
);
852 static unsigned char *alloc_destination (struct coding_system
*,
853 EMACS_INT
, unsigned char *);
854 static void setup_iso_safe_charsets (Lisp_Object
);
855 static unsigned char *encode_designation_at_bol (struct coding_system
*,
856 int *, unsigned char *);
857 static int detect_eol (const unsigned char *,
858 EMACS_INT
, enum coding_category
);
859 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*, int);
860 static void decode_eol (struct coding_system
*);
861 static Lisp_Object
get_translation_table (Lisp_Object
, int, int *);
862 static Lisp_Object
get_translation (Lisp_Object
, int *, int *);
863 static int produce_chars (struct coding_system
*, Lisp_Object
, int);
864 static INLINE
void produce_charset (struct coding_system
*, int *,
866 static void produce_annotation (struct coding_system
*, EMACS_INT
);
867 static int decode_coding (struct coding_system
*);
868 static INLINE
int *handle_composition_annotation (EMACS_INT
, EMACS_INT
,
869 struct coding_system
*,
871 static INLINE
int *handle_charset_annotation (EMACS_INT
, EMACS_INT
,
872 struct coding_system
*,
874 static void consume_chars (struct coding_system
*, Lisp_Object
, int);
875 static int encode_coding (struct coding_system
*);
876 static Lisp_Object
make_conversion_work_buffer (int);
877 static Lisp_Object
code_conversion_restore (Lisp_Object
);
878 static INLINE
int char_encodable_p (int, Lisp_Object
);
879 static Lisp_Object
make_subsidiaries (Lisp_Object
);
882 record_conversion_result (struct coding_system
*coding
,
883 enum coding_result_code result
)
885 coding
->result
= result
;
888 case CODING_RESULT_INSUFFICIENT_SRC
:
889 Vlast_code_conversion_error
= Qinsufficient_source
;
891 case CODING_RESULT_INCONSISTENT_EOL
:
892 Vlast_code_conversion_error
= Qinconsistent_eol
;
894 case CODING_RESULT_INVALID_SRC
:
895 Vlast_code_conversion_error
= Qinvalid_source
;
897 case CODING_RESULT_INTERRUPT
:
898 Vlast_code_conversion_error
= Qinterrupted
;
900 case CODING_RESULT_INSUFFICIENT_MEM
:
901 Vlast_code_conversion_error
= Qinsufficient_memory
;
903 case CODING_RESULT_INSUFFICIENT_DST
:
904 /* Don't record this error in Vlast_code_conversion_error
905 because it happens just temporarily and is resolved when the
906 whole conversion is finished. */
908 case CODING_RESULT_SUCCESS
:
911 Vlast_code_conversion_error
= intern ("Unknown error");
915 /* This wrapper macro is used to preserve validity of pointers into
916 buffer text across calls to decode_char, which could cause
917 relocation of buffers if it loads a charset map, because loading a
918 charset map allocates large structures. */
919 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
921 charset_map_loaded = 0; \
922 c = DECODE_CHAR (charset, code); \
923 if (charset_map_loaded) \
925 const unsigned char *orig = coding->source; \
928 coding_set_source (coding); \
929 offset = coding->source - orig; \
931 src_base += offset; \
937 /* If there are at least BYTES length of room at dst, allocate memory
938 for coding->destination and update dst and dst_end. We don't have
939 to take care of coding->source which will be relocated. It is
940 handled by calling coding_set_source in encode_coding. */
942 #define ASSURE_DESTINATION(bytes) \
944 if (dst + (bytes) >= dst_end) \
946 EMACS_INT more_bytes = charbuf_end - charbuf + (bytes); \
948 dst = alloc_destination (coding, more_bytes, dst); \
949 dst_end = coding->destination + coding->dst_bytes; \
954 /* Store multibyte form of the character C in P, and advance P to the
955 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
956 never calls MAYBE_UNIFY_CHAR. */
958 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
960 if ((c) <= MAX_1_BYTE_CHAR) \
962 else if ((c) <= MAX_2_BYTE_CHAR) \
963 *(p)++ = (0xC0 | ((c) >> 6)), \
964 *(p)++ = (0x80 | ((c) & 0x3F)); \
965 else if ((c) <= MAX_3_BYTE_CHAR) \
966 *(p)++ = (0xE0 | ((c) >> 12)), \
967 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
968 *(p)++ = (0x80 | ((c) & 0x3F)); \
969 else if ((c) <= MAX_4_BYTE_CHAR) \
970 *(p)++ = (0xF0 | (c >> 18)), \
971 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
972 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
973 *(p)++ = (0x80 | (c & 0x3F)); \
974 else if ((c) <= MAX_5_BYTE_CHAR) \
976 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
977 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
978 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
979 *(p)++ = (0x80 | (c & 0x3F)); \
981 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
985 /* Return the character code of character whose multibyte form is at
986 P, and advance P to the end of the multibyte form. This is like
987 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
989 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
992 : ! ((p)[0] & 0x20) \
994 ((((p)[-2] & 0x1F) << 6) \
996 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
997 : ! ((p)[0] & 0x10) \
999 ((((p)[-3] & 0x0F) << 12) \
1000 | (((p)[-2] & 0x3F) << 6) \
1001 | ((p)[-1] & 0x3F))) \
1002 : ! ((p)[0] & 0x08) \
1004 ((((p)[-4] & 0xF) << 18) \
1005 | (((p)[-3] & 0x3F) << 12) \
1006 | (((p)[-2] & 0x3F) << 6) \
1007 | ((p)[-1] & 0x3F))) \
1009 ((((p)[-4] & 0x3F) << 18) \
1010 | (((p)[-3] & 0x3F) << 12) \
1011 | (((p)[-2] & 0x3F) << 6) \
1012 | ((p)[-1] & 0x3F))))
1016 coding_set_source (struct coding_system
*coding
)
1018 if (BUFFERP (coding
->src_object
))
1020 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1022 if (coding
->src_pos
< 0)
1023 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1025 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1027 else if (STRINGP (coding
->src_object
))
1029 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1033 /* Otherwise, the source is C string and is never relocated
1034 automatically. Thus we don't have to update anything. */
1039 coding_set_destination (struct coding_system
*coding
)
1041 if (BUFFERP (coding
->dst_object
))
1043 if (coding
->src_pos
< 0)
1045 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1046 coding
->dst_bytes
= (GAP_END_ADDR
1047 - (coding
->src_bytes
- coding
->consumed
)
1048 - coding
->destination
);
1052 /* We are sure that coding->dst_pos_byte is before the gap
1054 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1055 + coding
->dst_pos_byte
- BEG_BYTE
);
1056 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1057 - coding
->destination
);
1062 /* Otherwise, the destination is C string and is never relocated
1063 automatically. Thus we don't have to update anything. */
1069 coding_alloc_by_realloc (struct coding_system
*coding
, EMACS_INT bytes
)
1071 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1072 coding
->dst_bytes
+ bytes
);
1073 coding
->dst_bytes
+= bytes
;
1077 coding_alloc_by_making_gap (struct coding_system
*coding
,
1078 EMACS_INT gap_head_used
, EMACS_INT bytes
)
1080 if (EQ (coding
->src_object
, coding
->dst_object
))
1082 /* The gap may contain the produced data at the head and not-yet
1083 consumed data at the tail. To preserve those data, we at
1084 first make the gap size to zero, then increase the gap
1086 EMACS_INT add
= GAP_SIZE
;
1088 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1089 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1091 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1092 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1096 Lisp_Object this_buffer
;
1098 this_buffer
= Fcurrent_buffer ();
1099 set_buffer_internal (XBUFFER (coding
->dst_object
));
1101 set_buffer_internal (XBUFFER (this_buffer
));
1106 static unsigned char *
1107 alloc_destination (struct coding_system
*coding
, EMACS_INT nbytes
,
1110 EMACS_INT offset
= dst
- coding
->destination
;
1112 if (BUFFERP (coding
->dst_object
))
1114 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1116 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1119 coding_alloc_by_realloc (coding
, nbytes
);
1120 coding_set_destination (coding
);
1121 dst
= coding
->destination
+ offset
;
1125 /** Macros for annotations. */
1127 /* An annotation data is stored in the array coding->charbuf in this
1129 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1130 LENGTH is the number of elements in the annotation.
1131 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1132 NCHARS is the number of characters in the text annotated.
1134 The format of the following elements depend on ANNOTATION_MASK.
1136 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1138 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1140 NBYTES is the number of bytes specified in the header part of
1141 old-style emacs-mule encoding, or 0 for the other kind of
1144 METHOD is one of enum composition_method.
1146 Optional COMPOSITION-COMPONENTS are characters and composition
1149 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1152 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1153 recover from an invalid annotation, and should be skipped by
1154 produce_annotation. */
1156 /* Maximum length of the header of annotation data. */
1157 #define MAX_ANNOTATION_LENGTH 5
1159 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1161 *(buf)++ = -(len); \
1162 *(buf)++ = (mask); \
1163 *(buf)++ = (nchars); \
1164 coding->annotated = 1; \
1167 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1169 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1175 #define ADD_CHARSET_DATA(buf, nchars, id) \
1177 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1182 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1189 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1190 Check if a text is encoded in UTF-8. If it is, return 1, else
1193 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1194 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1195 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1196 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1197 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1198 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1200 #define UTF_8_BOM_1 0xEF
1201 #define UTF_8_BOM_2 0xBB
1202 #define UTF_8_BOM_3 0xBF
1205 detect_coding_utf_8 (struct coding_system
*coding
,
1206 struct coding_detection_info
*detect_info
)
1208 const unsigned char *src
= coding
->source
, *src_base
;
1209 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1210 int multibytep
= coding
->src_multibyte
;
1211 EMACS_INT consumed_chars
= 0;
1215 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1216 /* A coding system of this category is always ASCII compatible. */
1217 src
+= coding
->head_ascii
;
1221 int c
, c1
, c2
, c3
, c4
;
1225 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1228 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1230 if (UTF_8_2_OCTET_LEADING_P (c
))
1236 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1238 if (UTF_8_3_OCTET_LEADING_P (c
))
1241 if (src_base
== coding
->source
1242 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1247 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1249 if (UTF_8_4_OCTET_LEADING_P (c
))
1255 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1257 if (UTF_8_5_OCTET_LEADING_P (c
))
1264 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1268 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1270 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1275 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1276 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1280 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1282 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1289 decode_coding_utf_8 (struct coding_system
*coding
)
1291 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1292 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1293 const unsigned char *src_base
;
1294 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1295 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1296 EMACS_INT consumed_chars
= 0, consumed_chars_base
= 0;
1297 int multibytep
= coding
->src_multibyte
;
1298 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1300 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1301 int byte_after_cr
= -1;
1303 if (bom
!= utf_without_bom
)
1309 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1314 if (! UTF_8_EXTRA_OCTET_P (c2
))
1319 if (! UTF_8_EXTRA_OCTET_P (c3
))
1323 if ((c1
!= UTF_8_BOM_1
)
1324 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1327 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1332 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1336 int c
, c1
, c2
, c3
, c4
, c5
;
1339 consumed_chars_base
= consumed_chars
;
1341 if (charbuf
>= charbuf_end
)
1343 if (byte_after_cr
>= 0)
1348 if (byte_after_cr
>= 0)
1349 c1
= byte_after_cr
, byte_after_cr
= -1;
1356 else if (UTF_8_1_OCTET_P (c1
))
1358 if (eol_dos
&& c1
== '\r')
1359 ONE_MORE_BYTE (byte_after_cr
);
1365 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1367 if (UTF_8_2_OCTET_LEADING_P (c1
))
1369 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1370 /* Reject overlong sequences here and below. Encoders
1371 producing them are incorrect, they can be misleading,
1372 and they mess up read/write invariance. */
1379 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1381 if (UTF_8_3_OCTET_LEADING_P (c1
))
1383 c
= (((c1
& 0xF) << 12)
1384 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1386 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1392 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1394 if (UTF_8_4_OCTET_LEADING_P (c1
))
1396 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1397 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1404 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1406 if (UTF_8_5_OCTET_LEADING_P (c1
))
1408 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1409 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1411 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1426 consumed_chars
= consumed_chars_base
;
1428 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1433 coding
->consumed_char
+= consumed_chars_base
;
1434 coding
->consumed
= src_base
- coding
->source
;
1435 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1440 encode_coding_utf_8 (struct coding_system
*coding
)
1442 int multibytep
= coding
->dst_multibyte
;
1443 int *charbuf
= coding
->charbuf
;
1444 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1445 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1446 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1447 EMACS_INT produced_chars
= 0;
1450 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1452 ASSURE_DESTINATION (3);
1453 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1454 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1459 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1461 while (charbuf
< charbuf_end
)
1463 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1465 ASSURE_DESTINATION (safe_room
);
1467 if (CHAR_BYTE8_P (c
))
1469 c
= CHAR_TO_BYTE8 (c
);
1474 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1475 for (p
= str
; p
< pend
; p
++)
1482 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1484 while (charbuf
< charbuf_end
)
1486 ASSURE_DESTINATION (safe_room
);
1488 if (CHAR_BYTE8_P (c
))
1489 *dst
++ = CHAR_TO_BYTE8 (c
);
1491 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1495 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1496 coding
->produced_char
+= produced_chars
;
1497 coding
->produced
= dst
- coding
->destination
;
1502 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1503 Check if a text is encoded in one of UTF-16 based coding systems.
1504 If it is, return 1, else return 0. */
1506 #define UTF_16_HIGH_SURROGATE_P(val) \
1507 (((val) & 0xFC00) == 0xD800)
1509 #define UTF_16_LOW_SURROGATE_P(val) \
1510 (((val) & 0xFC00) == 0xDC00)
1514 detect_coding_utf_16 (struct coding_system
*coding
,
1515 struct coding_detection_info
*detect_info
)
1517 const unsigned char *src
= coding
->source
;
1518 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1519 int multibytep
= coding
->src_multibyte
;
1522 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1523 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1524 && (coding
->src_chars
& 1))
1526 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1530 TWO_MORE_BYTES (c1
, c2
);
1531 if ((c1
== 0xFF) && (c2
== 0xFE))
1533 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1534 | CATEGORY_MASK_UTF_16_AUTO
);
1535 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1536 | CATEGORY_MASK_UTF_16_BE_NOSIG
1537 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1539 else if ((c1
== 0xFE) && (c2
== 0xFF))
1541 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1542 | CATEGORY_MASK_UTF_16_AUTO
);
1543 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1544 | CATEGORY_MASK_UTF_16_BE_NOSIG
1545 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1549 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1554 /* We check the dispersion of Eth and Oth bytes where E is even and
1555 O is odd. If both are high, we assume binary data.*/
1556 unsigned char e
[256], o
[256];
1557 unsigned e_num
= 1, o_num
= 1;
1564 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1565 |CATEGORY_MASK_UTF_16_BE
1566 | CATEGORY_MASK_UTF_16_LE
);
1568 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1569 != CATEGORY_MASK_UTF_16
)
1571 TWO_MORE_BYTES (c1
, c2
);
1579 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1586 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1597 decode_coding_utf_16 (struct coding_system
*coding
)
1599 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1600 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1601 const unsigned char *src_base
;
1602 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1603 /* We may produces at most 3 chars in one loop. */
1604 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1605 EMACS_INT consumed_chars
= 0, consumed_chars_base
= 0;
1606 int multibytep
= coding
->src_multibyte
;
1607 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1608 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1609 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1611 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1612 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1614 if (bom
== utf_with_bom
)
1623 if (endian
== utf_16_big_endian
1624 ? c
!= 0xFEFF : c
!= 0xFFFE)
1626 /* The first two bytes are not BOM. Treat them as bytes
1627 for a normal character. */
1631 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1633 else if (bom
== utf_detect_bom
)
1635 /* We have already tried to detect BOM and failed in
1637 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1645 consumed_chars_base
= consumed_chars
;
1647 if (charbuf
>= charbuf_end
)
1649 if (byte_after_cr1
>= 0)
1654 if (byte_after_cr1
>= 0)
1655 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1663 if (byte_after_cr2
>= 0)
1664 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1669 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1673 c
= (endian
== utf_16_big_endian
1674 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1678 if (! UTF_16_LOW_SURROGATE_P (c
))
1680 if (endian
== utf_16_big_endian
)
1681 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1683 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1687 if (UTF_16_HIGH_SURROGATE_P (c
))
1688 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1694 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1695 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1696 *charbuf
++ = 0x10000 + c
;
1701 if (UTF_16_HIGH_SURROGATE_P (c
))
1702 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1705 if (eol_dos
&& c
== '\r')
1707 ONE_MORE_BYTE (byte_after_cr1
);
1708 ONE_MORE_BYTE (byte_after_cr2
);
1716 coding
->consumed_char
+= consumed_chars_base
;
1717 coding
->consumed
= src_base
- coding
->source
;
1718 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1722 encode_coding_utf_16 (struct coding_system
*coding
)
1724 int multibytep
= coding
->dst_multibyte
;
1725 int *charbuf
= coding
->charbuf
;
1726 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1727 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1728 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1730 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1731 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1732 EMACS_INT produced_chars
= 0;
1735 if (bom
!= utf_without_bom
)
1737 ASSURE_DESTINATION (safe_room
);
1739 EMIT_TWO_BYTES (0xFE, 0xFF);
1741 EMIT_TWO_BYTES (0xFF, 0xFE);
1742 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1745 while (charbuf
< charbuf_end
)
1747 ASSURE_DESTINATION (safe_room
);
1749 if (c
> MAX_UNICODE_CHAR
)
1750 c
= coding
->default_char
;
1755 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1757 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1764 c1
= (c
>> 10) + 0xD800;
1765 c2
= (c
& 0x3FF) + 0xDC00;
1767 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1769 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1772 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1773 coding
->produced
= dst
- coding
->destination
;
1774 coding
->produced_char
+= produced_chars
;
1779 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1781 /* Emacs' internal format for representation of multiple character
1782 sets is a kind of multi-byte encoding, i.e. characters are
1783 represented by variable-length sequences of one-byte codes.
1785 ASCII characters and control characters (e.g. `tab', `newline') are
1786 represented by one-byte sequences which are their ASCII codes, in
1787 the range 0x00 through 0x7F.
1789 8-bit characters of the range 0x80..0x9F are represented by
1790 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1793 8-bit characters of the range 0xA0..0xFF are represented by
1794 one-byte sequences which are their 8-bit code.
1796 The other characters are represented by a sequence of `base
1797 leading-code', optional `extended leading-code', and one or two
1798 `position-code's. The length of the sequence is determined by the
1799 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1800 whereas extended leading-code and position-code take the range 0xA0
1801 through 0xFF. See `charset.h' for more details about leading-code
1804 --- CODE RANGE of Emacs' internal format ---
1808 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1809 eight-bit-graphic 0xA0..0xBF
1810 ELSE 0x81..0x9D + [0xA0..0xFF]+
1811 ---------------------------------------------
1813 As this is the internal character representation, the format is
1814 usually not used externally (i.e. in a file or in a data sent to a
1815 process). But, it is possible to have a text externally in this
1816 format (i.e. by encoding by the coding system `emacs-mule').
1818 In that case, a sequence of one-byte codes has a slightly different
1821 At first, all characters in eight-bit-control are represented by
1822 one-byte sequences which are their 8-bit code.
1824 Next, character composition data are represented by the byte
1825 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1827 METHOD is 0xF2 plus one of composition method (enum
1828 composition_method),
1830 BYTES is 0xA0 plus a byte length of this composition data,
1832 CHARS is 0xA0 plus a number of characters composed by this
1835 COMPONENTs are characters of multibyte form or composition
1836 rules encoded by two-byte of ASCII codes.
1838 In addition, for backward compatibility, the following formats are
1839 also recognized as composition data on decoding.
1842 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1845 MSEQ is a multibyte form but in these special format:
1846 ASCII: 0xA0 ASCII_CODE+0x80,
1847 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1848 RULE is a one byte code of the range 0xA0..0xF0 that
1849 represents a composition rule.
1852 char emacs_mule_bytes
[256];
1855 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1856 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1860 detect_coding_emacs_mule (struct coding_system
*coding
,
1861 struct coding_detection_info
*detect_info
)
1863 const unsigned char *src
= coding
->source
, *src_base
;
1864 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1865 int multibytep
= coding
->src_multibyte
;
1866 EMACS_INT consumed_chars
= 0;
1870 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1871 /* A coding system of this category is always ASCII compatible. */
1872 src
+= coding
->head_ascii
;
1882 /* Perhaps the start of composite character. We simply skip
1883 it because analyzing it is too heavy for detecting. But,
1884 at least, we check that the composite character
1885 constitutes of more than 4 bytes. */
1886 const unsigned char *src_start
;
1896 if (src
- src_start
<= 4)
1898 found
= CATEGORY_MASK_EMACS_MULE
;
1906 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1911 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1913 while (more_bytes
> 0)
1918 src
--; /* Unread the last byte. */
1923 if (more_bytes
!= 0)
1925 found
= CATEGORY_MASK_EMACS_MULE
;
1928 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1932 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1934 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1937 detect_info
->found
|= found
;
1942 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1943 character. If CMP_STATUS indicates that we must expect MSEQ or
1944 RULE described above, decode it and return the negative value of
1945 the decoded character or rule. If an invalid byte is found, return
1946 -1. If SRC is too short, return -2. */
1949 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1950 int *nbytes
, int *nchars
, int *id
,
1951 struct composition_status
*cmp_status
)
1953 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1954 const unsigned char *src_base
= src
;
1955 int multibytep
= coding
->src_multibyte
;
1959 int consumed_chars
= 0;
1966 charset_ID
= emacs_mule_charset
[0];
1972 if (cmp_status
->state
!= COMPOSING_NO
1973 && cmp_status
->old_form
)
1975 if (cmp_status
->state
== COMPOSING_CHAR
)
1990 *nbytes
= src
- src_base
;
1991 *nchars
= consumed_chars
;
1999 switch (emacs_mule_bytes
[c
])
2002 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2011 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2012 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2015 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2024 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2029 code
= (c
& 0x7F) << 8;
2039 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2044 code
= (c
& 0x7F) << 8;
2053 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2059 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2060 CHARSET_FROM_ID (charset_ID
), code
, c
);
2064 *nbytes
= src
- src_base
;
2065 *nchars
= consumed_chars
;
2068 return (mseq_found
? -c
: c
);
2078 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2080 /* Handle these composition sequence ('|': the end of header elements,
2081 BYTES and CHARS >= 0xA0):
2083 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2084 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2085 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2089 (4) relative composition: 0x80 | MSEQ ... MSEQ
2090 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2092 When the starter 0x80 and the following header elements are found,
2093 this annotation header is produced.
2095 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2097 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2098 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2100 Then, upon reading the following elements, these codes are produced
2101 until the composition end is found:
2104 (2) ALT ... ALT CHAR ... CHAR
2105 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2107 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2109 When the composition end is found, LENGTH and NCHARS in the
2110 annotation header is updated as below:
2112 (1) LENGTH: unchanged, NCHARS: unchanged
2113 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2114 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2115 (4) LENGTH: unchanged, NCHARS: number of CHARs
2116 (5) LENGTH: unchanged, NCHARS: number of CHARs
2118 If an error is found while composing, the annotation header is
2119 changed to the original composition header (plus filler -1s) as
2122 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2123 (5) [ 0x80 0xFF -1 -1- -1 ]
2125 and the sequence [ -2 DECODED-RULE ] is changed to the original
2126 byte sequence as below:
2127 o the original byte sequence is B: [ B -1 ]
2128 o the original byte sequence is B1 B2: [ B1 B2 ]
2130 Most of the routines are implemented by macros because many
2131 variables and labels in the caller decode_coding_emacs_mule must be
2132 accessible, and they are usually called just once (thus doesn't
2133 increase the size of compiled object). */
2135 /* Decode a composition rule represented by C as a component of
2136 composition sequence of Emacs 20 style. Set RULE to the decoded
2139 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2144 if (c < 0 || c >= 81) \
2145 goto invalid_code; \
2146 gref = c / 9, nref = c % 9; \
2147 if (gref == 4) gref = 10; \
2148 if (nref == 4) nref = 10; \
2149 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2153 /* Decode a composition rule represented by C and the following byte
2154 at SRC as a component of composition sequence of Emacs 21 style.
2155 Set RULE to the decoded rule. */
2157 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2162 if (gref < 0 || gref >= 81) \
2163 goto invalid_code; \
2164 ONE_MORE_BYTE (c); \
2166 if (nref < 0 || nref >= 81) \
2167 goto invalid_code; \
2168 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2172 /* Start of Emacs 21 style format. The first three bytes at SRC are
2173 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2174 byte length of this composition information, CHARS is the number of
2175 characters composed by this composition. */
2177 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2179 enum composition_method method = c - 0xF2; \
2180 int nbytes, nchars; \
2182 ONE_MORE_BYTE (c); \
2184 goto invalid_code; \
2185 nbytes = c - 0xA0; \
2186 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2187 goto invalid_code; \
2188 ONE_MORE_BYTE (c); \
2189 nchars = c - 0xA0; \
2190 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2191 goto invalid_code; \
2192 cmp_status->old_form = 0; \
2193 cmp_status->method = method; \
2194 if (method == COMPOSITION_RELATIVE) \
2195 cmp_status->state = COMPOSING_CHAR; \
2197 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2198 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2199 cmp_status->nchars = nchars; \
2200 cmp_status->ncomps = nbytes - 4; \
2201 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2205 /* Start of Emacs 20 style format for relative composition. */
2207 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2209 cmp_status->old_form = 1; \
2210 cmp_status->method = COMPOSITION_RELATIVE; \
2211 cmp_status->state = COMPOSING_CHAR; \
2212 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2213 cmp_status->nchars = cmp_status->ncomps = 0; \
2214 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2218 /* Start of Emacs 20 style format for rule-base composition. */
2220 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2222 cmp_status->old_form = 1; \
2223 cmp_status->method = COMPOSITION_WITH_RULE; \
2224 cmp_status->state = COMPOSING_CHAR; \
2225 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2226 cmp_status->nchars = cmp_status->ncomps = 0; \
2227 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2231 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2233 const unsigned char *current_src = src; \
2235 ONE_MORE_BYTE (c); \
2237 goto invalid_code; \
2238 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2239 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2240 DECODE_EMACS_MULE_21_COMPOSITION (); \
2241 else if (c < 0xA0) \
2242 goto invalid_code; \
2243 else if (c < 0xC0) \
2245 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2246 /* Re-read C as a composition component. */ \
2247 src = current_src; \
2249 else if (c == 0xFF) \
2250 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2252 goto invalid_code; \
2255 #define EMACS_MULE_COMPOSITION_END() \
2257 int idx = - cmp_status->length; \
2259 if (cmp_status->old_form) \
2260 charbuf[idx + 2] = cmp_status->nchars; \
2261 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2262 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2263 cmp_status->state = COMPOSING_NO; \
2268 emacs_mule_finish_composition (int *charbuf
,
2269 struct composition_status
*cmp_status
)
2271 int idx
= - cmp_status
->length
;
2274 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2276 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2278 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2279 && cmp_status
->state
== COMPOSING_CHAR
)
2281 /* The last rule was invalid. */
2282 int rule
= charbuf
[-1] + 0xA0;
2284 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2291 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2293 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2295 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2296 charbuf
[idx
++] = -3;
2302 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2303 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2305 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2306 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2307 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2308 charbuf
[idx
++] = -1;
2312 cmp_status
->state
= COMPOSING_NO
;
2316 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2318 if (cmp_status->state != COMPOSING_NO) \
2319 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2324 decode_coding_emacs_mule (struct coding_system
*coding
)
2326 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2327 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2328 const unsigned char *src_base
;
2329 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2330 /* We may produce two annotations (charset and composition) in one
2331 loop and one more charset annotation at the end. */
2333 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
2334 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
2335 int multibytep
= coding
->src_multibyte
;
2336 EMACS_INT char_offset
= coding
->produced_char
;
2337 EMACS_INT last_offset
= char_offset
;
2338 int last_id
= charset_ascii
;
2340 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2341 int byte_after_cr
= -1;
2342 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2344 if (cmp_status
->state
!= COMPOSING_NO
)
2348 for (i
= 0; i
< cmp_status
->length
; i
++)
2349 *charbuf
++ = cmp_status
->carryover
[i
];
2350 coding
->annotated
= 1;
2355 int c
, id
IF_LINT (= 0);
2358 consumed_chars_base
= consumed_chars
;
2360 if (charbuf
>= charbuf_end
)
2362 if (byte_after_cr
>= 0)
2367 if (byte_after_cr
>= 0)
2368 c
= byte_after_cr
, byte_after_cr
= -1;
2372 if (c
< 0 || c
== 0x80)
2374 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2381 DECODE_EMACS_MULE_COMPOSITION_START ();
2387 if (eol_dos
&& c
== '\r')
2388 ONE_MORE_BYTE (byte_after_cr
);
2390 if (cmp_status
->state
!= COMPOSING_NO
)
2392 if (cmp_status
->old_form
)
2393 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2394 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2395 cmp_status
->ncomps
--;
2400 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2401 /* emacs_mule_char can load a charset map from a file, which
2402 allocates a large structure and might cause buffer text
2403 to be relocated as result. Thus, we need to remember the
2404 original pointer to buffer text, and fix up all related
2405 pointers after the call. */
2406 const unsigned char *orig
= coding
->source
;
2409 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2411 offset
= coding
->source
- orig
;
2425 src
= src_base
+ nbytes
;
2426 consumed_chars
= consumed_chars_base
+ nchars
;
2427 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2428 cmp_status
->ncomps
-= nchars
;
2431 /* Now if C >= 0, we found a normally encoded character, if C <
2432 0, we found an old-style composition component character or
2435 if (cmp_status
->state
== COMPOSING_NO
)
2439 if (last_id
!= charset_ascii
)
2440 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2443 last_offset
= char_offset
;
2448 else if (cmp_status
->state
== COMPOSING_CHAR
)
2450 if (cmp_status
->old_form
)
2454 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2461 cmp_status
->nchars
++;
2462 cmp_status
->length
++;
2463 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2464 EMACS_MULE_COMPOSITION_END ();
2465 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2466 cmp_status
->state
= COMPOSING_RULE
;
2472 cmp_status
->length
++;
2473 cmp_status
->nchars
--;
2474 if (cmp_status
->nchars
== 0)
2475 EMACS_MULE_COMPOSITION_END ();
2478 else if (cmp_status
->state
== COMPOSING_RULE
)
2484 EMACS_MULE_COMPOSITION_END ();
2491 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2496 cmp_status
->length
+= 2;
2497 cmp_status
->state
= COMPOSING_CHAR
;
2500 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2503 cmp_status
->length
++;
2504 if (cmp_status
->ncomps
== 0)
2505 cmp_status
->state
= COMPOSING_CHAR
;
2506 else if (cmp_status
->ncomps
> 0)
2508 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2509 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2512 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2514 else /* COMPOSING_COMPONENT_RULE */
2518 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2523 cmp_status
->length
+= 2;
2524 cmp_status
->ncomps
--;
2525 if (cmp_status
->ncomps
> 0)
2526 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2528 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2533 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2535 consumed_chars
= consumed_chars_base
;
2537 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2543 if (cmp_status
->state
!= COMPOSING_NO
)
2545 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2546 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2551 charbuf
-= cmp_status
->length
;
2552 for (i
= 0; i
< cmp_status
->length
; i
++)
2553 cmp_status
->carryover
[i
] = charbuf
[i
];
2556 if (last_id
!= charset_ascii
)
2557 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2558 coding
->consumed_char
+= consumed_chars_base
;
2559 coding
->consumed
= src_base
- coding
->source
;
2560 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2564 #define EMACS_MULE_LEADING_CODES(id, codes) \
2567 codes[0] = id, codes[1] = 0; \
2568 else if (id < 0xE0) \
2569 codes[0] = 0x9A, codes[1] = id; \
2570 else if (id < 0xF0) \
2571 codes[0] = 0x9B, codes[1] = id; \
2572 else if (id < 0xF5) \
2573 codes[0] = 0x9C, codes[1] = id; \
2575 codes[0] = 0x9D, codes[1] = id; \
2580 encode_coding_emacs_mule (struct coding_system
*coding
)
2582 int multibytep
= coding
->dst_multibyte
;
2583 int *charbuf
= coding
->charbuf
;
2584 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2585 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2586 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2588 EMACS_INT produced_chars
= 0;
2589 Lisp_Object attrs
, charset_list
;
2591 int preferred_charset_id
= -1;
2593 CODING_GET_INFO (coding
, attrs
, charset_list
);
2594 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2596 CODING_ATTR_CHARSET_LIST (attrs
)
2597 = charset_list
= Vemacs_mule_charset_list
;
2600 while (charbuf
< charbuf_end
)
2602 ASSURE_DESTINATION (safe_room
);
2607 /* Handle an annotation. */
2610 case CODING_ANNOTATE_COMPOSITION_MASK
:
2611 /* Not yet implemented. */
2613 case CODING_ANNOTATE_CHARSET_MASK
:
2614 preferred_charset_id
= charbuf
[3];
2615 if (preferred_charset_id
>= 0
2616 && NILP (Fmemq (make_number (preferred_charset_id
),
2618 preferred_charset_id
= -1;
2627 if (ASCII_CHAR_P (c
))
2628 EMIT_ONE_ASCII_BYTE (c
);
2629 else if (CHAR_BYTE8_P (c
))
2631 c
= CHAR_TO_BYTE8 (c
);
2636 struct charset
*charset
;
2640 unsigned char leading_codes
[2];
2642 if (preferred_charset_id
>= 0)
2644 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2645 if (CHAR_CHARSET_P (c
, charset
))
2646 code
= ENCODE_CHAR (charset
, c
);
2648 charset
= char_charset (c
, charset_list
, &code
);
2651 charset
= char_charset (c
, charset_list
, &code
);
2654 c
= coding
->default_char
;
2655 if (ASCII_CHAR_P (c
))
2657 EMIT_ONE_ASCII_BYTE (c
);
2660 charset
= char_charset (c
, charset_list
, &code
);
2662 dimension
= CHARSET_DIMENSION (charset
);
2663 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2664 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2665 EMIT_ONE_BYTE (leading_codes
[0]);
2666 if (leading_codes
[1])
2667 EMIT_ONE_BYTE (leading_codes
[1]);
2669 EMIT_ONE_BYTE (code
| 0x80);
2673 EMIT_ONE_BYTE (code
>> 8);
2674 EMIT_ONE_BYTE (code
& 0xFF);
2678 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2679 coding
->produced_char
+= produced_chars
;
2680 coding
->produced
= dst
- coding
->destination
;
2685 /*** 7. ISO2022 handlers ***/
2687 /* The following note describes the coding system ISO2022 briefly.
2688 Since the intention of this note is to help understand the
2689 functions in this file, some parts are NOT ACCURATE or are OVERLY
2690 SIMPLIFIED. For thorough understanding, please refer to the
2691 original document of ISO2022. This is equivalent to the standard
2692 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2694 ISO2022 provides many mechanisms to encode several character sets
2695 in 7-bit and 8-bit environments. For 7-bit environments, all text
2696 is encoded using bytes less than 128. This may make the encoded
2697 text a little bit longer, but the text passes more easily through
2698 several types of gateway, some of which strip off the MSB (Most
2701 There are two kinds of character sets: control character sets and
2702 graphic character sets. The former contain control characters such
2703 as `newline' and `escape' to provide control functions (control
2704 functions are also provided by escape sequences). The latter
2705 contain graphic characters such as 'A' and '-'. Emacs recognizes
2706 two control character sets and many graphic character sets.
2708 Graphic character sets are classified into one of the following
2709 four classes, according to the number of bytes (DIMENSION) and
2710 number of characters in one dimension (CHARS) of the set:
2711 - DIMENSION1_CHARS94
2712 - DIMENSION1_CHARS96
2713 - DIMENSION2_CHARS94
2714 - DIMENSION2_CHARS96
2716 In addition, each character set is assigned an identification tag,
2717 unique for each set, called the "final character" (denoted as <F>
2718 hereafter). The <F> of each character set is decided by ECMA(*)
2719 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2720 (0x30..0x3F are for private use only).
2722 Note (*): ECMA = European Computer Manufacturers Association
2724 Here are examples of graphic character sets [NAME(<F>)]:
2725 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2726 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2727 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2728 o DIMENSION2_CHARS96 -- none for the moment
2730 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2731 C0 [0x00..0x1F] -- control character plane 0
2732 GL [0x20..0x7F] -- graphic character plane 0
2733 C1 [0x80..0x9F] -- control character plane 1
2734 GR [0xA0..0xFF] -- graphic character plane 1
2736 A control character set is directly designated and invoked to C0 or
2737 C1 by an escape sequence. The most common case is that:
2738 - ISO646's control character set is designated/invoked to C0, and
2739 - ISO6429's control character set is designated/invoked to C1,
2740 and usually these designations/invocations are omitted in encoded
2741 text. In a 7-bit environment, only C0 can be used, and a control
2742 character for C1 is encoded by an appropriate escape sequence to
2743 fit into the environment. All control characters for C1 are
2744 defined to have corresponding escape sequences.
2746 A graphic character set is at first designated to one of four
2747 graphic registers (G0 through G3), then these graphic registers are
2748 invoked to GL or GR. These designations and invocations can be
2749 done independently. The most common case is that G0 is invoked to
2750 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2751 these invocations and designations are omitted in encoded text.
2752 In a 7-bit environment, only GL can be used.
2754 When a graphic character set of CHARS94 is invoked to GL, codes
2755 0x20 and 0x7F of the GL area work as control characters SPACE and
2756 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2759 There are two ways of invocation: locking-shift and single-shift.
2760 With locking-shift, the invocation lasts until the next different
2761 invocation, whereas with single-shift, the invocation affects the
2762 following character only and doesn't affect the locking-shift
2763 state. Invocations are done by the following control characters or
2766 ----------------------------------------------------------------------
2767 abbrev function cntrl escape seq description
2768 ----------------------------------------------------------------------
2769 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2770 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2771 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2772 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2773 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2774 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2775 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2776 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2777 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2778 ----------------------------------------------------------------------
2779 (*) These are not used by any known coding system.
2781 Control characters for these functions are defined by macros
2782 ISO_CODE_XXX in `coding.h'.
2784 Designations are done by the following escape sequences:
2785 ----------------------------------------------------------------------
2786 escape sequence description
2787 ----------------------------------------------------------------------
2788 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2789 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2790 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2791 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2792 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2793 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2794 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2795 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2796 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2797 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2798 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2799 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2800 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2801 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2802 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2803 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2804 ----------------------------------------------------------------------
2806 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2807 of dimension 1, chars 94, and final character <F>, etc...
2809 Note (*): Although these designations are not allowed in ISO2022,
2810 Emacs accepts them on decoding, and produces them on encoding
2811 CHARS96 character sets in a coding system which is characterized as
2812 7-bit environment, non-locking-shift, and non-single-shift.
2814 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2815 '(' must be omitted. We refer to this as "short-form" hereafter.
2817 Now you may notice that there are a lot of ways of encoding the
2818 same multilingual text in ISO2022. Actually, there exist many
2819 coding systems such as Compound Text (used in X11's inter client
2820 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2821 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2822 localized platforms), and all of these are variants of ISO2022.
2824 In addition to the above, Emacs handles two more kinds of escape
2825 sequences: ISO6429's direction specification and Emacs' private
2826 sequence for specifying character composition.
2828 ISO6429's direction specification takes the following form:
2829 o CSI ']' -- end of the current direction
2830 o CSI '0' ']' -- end of the current direction
2831 o CSI '1' ']' -- start of left-to-right text
2832 o CSI '2' ']' -- start of right-to-left text
2833 The control character CSI (0x9B: control sequence introducer) is
2834 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2836 Character composition specification takes the following form:
2837 o ESC '0' -- start relative composition
2838 o ESC '1' -- end composition
2839 o ESC '2' -- start rule-base composition (*)
2840 o ESC '3' -- start relative composition with alternate chars (**)
2841 o ESC '4' -- start rule-base composition with alternate chars (**)
2842 Since these are not standard escape sequences of any ISO standard,
2843 the use of them with these meanings is restricted to Emacs only.
2845 (*) This form is used only in Emacs 20.7 and older versions,
2846 but newer versions can safely decode it.
2847 (**) This form is used only in Emacs 21.1 and newer versions,
2848 and older versions can't decode it.
2850 Here's a list of example usages of these composition escape
2851 sequences (categorized by `enum composition_method').
2853 COMPOSITION_RELATIVE:
2854 ESC 0 CHAR [ CHAR ] ESC 1
2855 COMPOSITION_WITH_RULE:
2856 ESC 2 CHAR [ RULE CHAR ] ESC 1
2857 COMPOSITION_WITH_ALTCHARS:
2858 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2859 COMPOSITION_WITH_RULE_ALTCHARS:
2860 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2862 enum iso_code_class_type iso_code_class
[256];
2864 #define SAFE_CHARSET_P(coding, id) \
2865 ((id) <= (coding)->max_charset_id \
2866 && (coding)->safe_charsets[id] != 255)
2869 setup_iso_safe_charsets (Lisp_Object attrs
)
2871 Lisp_Object charset_list
, safe_charsets
;
2872 Lisp_Object request
;
2873 Lisp_Object reg_usage
;
2876 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2879 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2880 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2881 && ! EQ (charset_list
, Viso_2022_charset_list
))
2883 CODING_ATTR_CHARSET_LIST (attrs
)
2884 = charset_list
= Viso_2022_charset_list
;
2885 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2888 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2892 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2894 int id
= XINT (XCAR (tail
));
2895 if (max_charset_id
< id
)
2896 max_charset_id
= id
;
2899 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2900 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2901 request
= AREF (attrs
, coding_attr_iso_request
);
2902 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2903 reg94
= XINT (XCAR (reg_usage
));
2904 reg96
= XINT (XCDR (reg_usage
));
2906 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2910 struct charset
*charset
;
2913 charset
= CHARSET_FROM_ID (XINT (id
));
2914 reg
= Fcdr (Fassq (id
, request
));
2916 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2917 else if (charset
->iso_chars_96
)
2920 SSET (safe_charsets
, XINT (id
), reg96
);
2925 SSET (safe_charsets
, XINT (id
), reg94
);
2928 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2932 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2933 Check if a text is encoded in one of ISO-2022 based coding systems.
2934 If it is, return 1, else return 0. */
2937 detect_coding_iso_2022 (struct coding_system
*coding
,
2938 struct coding_detection_info
*detect_info
)
2940 const unsigned char *src
= coding
->source
, *src_base
= src
;
2941 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2942 int multibytep
= coding
->src_multibyte
;
2943 int single_shifting
= 0;
2946 EMACS_INT consumed_chars
= 0;
2950 int composition_count
= -1;
2952 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2954 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2956 struct coding_system
*this = &(coding_categories
[i
]);
2957 Lisp_Object attrs
, val
;
2961 attrs
= CODING_ID_ATTRS (this->id
);
2962 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2963 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2964 setup_iso_safe_charsets (attrs
);
2965 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2966 this->max_charset_id
= SCHARS (val
) - 1;
2967 this->safe_charsets
= SDATA (val
);
2970 /* A coding system of this category is always ASCII compatible. */
2971 src
+= coding
->head_ascii
;
2973 while (rejected
!= CATEGORY_MASK_ISO
)
2980 if (inhibit_iso_escape_detection
)
2982 single_shifting
= 0;
2984 if (c
== 'N' || c
== 'O')
2986 /* ESC <Fe> for SS2 or SS3. */
2987 single_shifting
= 1;
2988 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2992 /* End of composition. */
2993 if (composition_count
< 0
2994 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
2997 composition_count
= -1;
2998 found
|= CATEGORY_MASK_ISO
;
3000 else if (c
>= '0' && c
<= '4')
3002 /* ESC <Fp> for start/end composition. */
3003 composition_count
= 0;
3007 if (c
>= '(' && c
<= '/')
3009 /* Designation sequence for a charset of dimension 1. */
3011 if (c1
< ' ' || c1
>= 0x80
3012 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3013 /* Invalid designation sequence. Just ignore. */
3018 /* Designation sequence for a charset of dimension 2. */
3020 if (c
>= '@' && c
<= 'B')
3021 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3022 id
= iso_charset_table
[1][0][c
];
3023 else if (c
>= '(' && c
<= '/')
3026 if (c1
< ' ' || c1
>= 0x80
3027 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3028 /* Invalid designation sequence. Just ignore. */
3032 /* Invalid designation sequence. Just ignore it. */
3037 /* Invalid escape sequence. Just ignore it. */
3041 /* We found a valid designation sequence for CHARSET. */
3042 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3043 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3045 found
|= CATEGORY_MASK_ISO_7
;
3047 rejected
|= CATEGORY_MASK_ISO_7
;
3048 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3050 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3052 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3053 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3055 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3057 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3058 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3060 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3062 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3068 /* Locking shift out/in. */
3069 if (inhibit_iso_escape_detection
)
3071 single_shifting
= 0;
3072 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3076 /* Control sequence introducer. */
3077 single_shifting
= 0;
3078 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3079 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3080 goto check_extra_latin
;
3085 if (inhibit_iso_escape_detection
)
3087 single_shifting
= 0;
3088 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3089 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3090 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3092 found
|= CATEGORY_MASK_ISO_8_1
;
3093 single_shifting
= 1;
3095 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3096 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3098 found
|= CATEGORY_MASK_ISO_8_2
;
3099 single_shifting
= 1;
3101 if (single_shifting
)
3104 if (! VECTORP (Vlatin_extra_code_table
)
3105 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3107 rejected
= CATEGORY_MASK_ISO
;
3110 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3111 & CODING_ISO_FLAG_LATIN_EXTRA
)
3112 found
|= CATEGORY_MASK_ISO_8_1
;
3114 rejected
|= CATEGORY_MASK_ISO_8_1
;
3115 rejected
|= CATEGORY_MASK_ISO_8_2
;
3123 if (composition_count
>= 0)
3124 composition_count
++;
3125 single_shifting
= 0;
3130 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3131 found
|= CATEGORY_MASK_ISO_8_1
;
3132 /* Check the length of succeeding codes of the range
3133 0xA0..0FF. If the byte length is even, we include
3134 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3135 only when we are not single shifting. */
3136 if (! single_shifting
3137 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3140 while (src
< src_end
)
3152 if (len
& 1 && src
< src_end
)
3154 rejected
|= CATEGORY_MASK_ISO_8_2
;
3155 if (composition_count
>= 0)
3156 composition_count
+= len
;
3160 found
|= CATEGORY_MASK_ISO_8_2
;
3161 if (composition_count
>= 0)
3162 composition_count
+= len
/ 2;
3169 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3173 detect_info
->rejected
|= rejected
;
3174 detect_info
->found
|= (found
& ~rejected
);
3179 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3180 escape sequence should be kept. */
3181 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3185 if (final < '0' || final >= 128 \
3186 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3187 || !SAFE_CHARSET_P (coding, id)) \
3189 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3193 prev = CODING_ISO_DESIGNATION (coding, reg); \
3194 if (id == charset_jisx0201_roman) \
3196 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3197 id = charset_ascii; \
3199 else if (id == charset_jisx0208_1978) \
3201 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3202 id = charset_jisx0208; \
3204 CODING_ISO_DESIGNATION (coding, reg) = id; \
3205 /* If there was an invalid designation to REG previously, and this \
3206 designation is ASCII to REG, we should keep this designation \
3208 if (prev == -2 && id == charset_ascii) \
3213 /* Handle these composition sequence (ALT: alternate char):
3215 (1) relative composition: ESC 0 CHAR ... ESC 1
3216 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3217 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3218 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3220 When the start sequence (ESC 0/2/3/4) is found, this annotation
3223 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3225 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3226 produced until the end sequence (ESC 1) is found:
3229 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3230 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3231 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3233 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3234 annotation header is updated as below:
3236 (1) LENGTH: unchanged, NCHARS: number of CHARs
3237 (2) LENGTH: unchanged, NCHARS: number of CHARs
3238 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3239 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3241 If an error is found while composing, the annotation header is
3244 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3246 and the sequence [ -2 DECODED-RULE ] is changed to the original
3247 byte sequence as below:
3248 o the original byte sequence is B: [ B -1 ]
3249 o the original byte sequence is B1 B2: [ B1 B2 ]
3250 and the sequence [ -1 -1 ] is changed to the original byte
3255 /* Decode a composition rule C1 and maybe one more byte from the
3256 source, and set RULE to the encoded composition rule. If the rule
3257 is invalid, goto invalid_code. */
3259 #define DECODE_COMPOSITION_RULE(rule) \
3263 goto invalid_code; \
3264 if (rule < 81) /* old format (before ver.21) */ \
3266 int gref = (rule) / 9; \
3267 int nref = (rule) % 9; \
3268 if (gref == 4) gref = 10; \
3269 if (nref == 4) nref = 10; \
3270 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3272 else /* new format (after ver.21) */ \
3276 ONE_MORE_BYTE (b); \
3277 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3278 goto invalid_code; \
3279 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3280 rule += 0x100; /* Distinguish it from the old format. */ \
3284 #define ENCODE_COMPOSITION_RULE(rule) \
3286 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3288 if (rule < 0x100) /* old format */ \
3290 if (gref == 10) gref = 4; \
3291 if (nref == 10) nref = 4; \
3292 charbuf[idx] = 32 + gref * 9 + nref; \
3293 charbuf[idx + 1] = -1; \
3296 else /* new format */ \
3298 charbuf[idx] = 32 + 81 + gref; \
3299 charbuf[idx + 1] = 32 + nref; \
3304 /* Finish the current composition as invalid. */
3306 static int finish_composition (int *, struct composition_status
*);
3309 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3311 int idx
= - cmp_status
->length
;
3314 /* Recover the original ESC sequence */
3315 charbuf
[idx
++] = ISO_CODE_ESC
;
3316 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3317 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3318 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3319 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3321 charbuf
[idx
++] = -2;
3323 charbuf
[idx
++] = -1;
3324 new_chars
= cmp_status
->nchars
;
3325 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3326 for (; idx
< 0; idx
++)
3328 int elt
= charbuf
[idx
];
3332 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3337 charbuf
[idx
++] = ISO_CODE_ESC
;
3342 cmp_status
->state
= COMPOSING_NO
;
3346 /* If characters are under composition, finish the composition. */
3347 #define MAYBE_FINISH_COMPOSITION() \
3349 if (cmp_status->state != COMPOSING_NO) \
3350 char_offset += finish_composition (charbuf, cmp_status); \
3353 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3355 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3356 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3357 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3358 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3360 Produce this annotation sequence now:
3362 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3365 #define DECODE_COMPOSITION_START(c1) \
3368 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3369 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3370 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3371 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3375 cmp_status->state = COMPOSING_CHAR; \
3376 cmp_status->length += 2; \
3380 MAYBE_FINISH_COMPOSITION (); \
3381 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3382 : c1 == '2' ? COMPOSITION_WITH_RULE \
3383 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3384 : COMPOSITION_WITH_RULE_ALTCHARS); \
3386 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3387 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3388 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3389 cmp_status->nchars = cmp_status->ncomps = 0; \
3390 coding->annotated = 1; \
3395 /* Handle composition end sequence ESC 1. */
3397 #define DECODE_COMPOSITION_END() \
3399 if (cmp_status->nchars == 0 \
3400 || ((cmp_status->state == COMPOSING_CHAR) \
3401 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3403 MAYBE_FINISH_COMPOSITION (); \
3404 goto invalid_code; \
3406 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3407 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3408 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3409 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3410 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3411 char_offset += cmp_status->nchars; \
3412 cmp_status->state = COMPOSING_NO; \
3415 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3417 #define STORE_COMPOSITION_RULE(rule) \
3420 *charbuf++ = rule; \
3421 cmp_status->length += 2; \
3422 cmp_status->state--; \
3425 /* Store a composed char or a component char C in charbuf, and update
3428 #define STORE_COMPOSITION_CHAR(c) \
3431 cmp_status->length++; \
3432 if (cmp_status->state == COMPOSING_CHAR) \
3433 cmp_status->nchars++; \
3435 cmp_status->ncomps++; \
3436 if (cmp_status->method == COMPOSITION_WITH_RULE \
3437 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3438 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3439 cmp_status->state++; \
3443 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3446 decode_coding_iso_2022 (struct coding_system
*coding
)
3448 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3449 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3450 const unsigned char *src_base
;
3451 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3452 /* We may produce two annotations (charset and composition) in one
3453 loop and one more charset annotation at the end. */
3455 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3456 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
3457 int multibytep
= coding
->src_multibyte
;
3458 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3459 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3460 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3461 int charset_id_2
, charset_id_3
;
3462 struct charset
*charset
;
3464 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3465 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3466 EMACS_INT char_offset
= coding
->produced_char
;
3467 EMACS_INT last_offset
= char_offset
;
3468 int last_id
= charset_ascii
;
3470 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3471 int byte_after_cr
= -1;
3474 setup_iso_safe_charsets (attrs
);
3475 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3477 if (cmp_status
->state
!= COMPOSING_NO
)
3479 for (i
= 0; i
< cmp_status
->length
; i
++)
3480 *charbuf
++ = cmp_status
->carryover
[i
];
3481 coding
->annotated
= 1;
3489 consumed_chars_base
= consumed_chars
;
3491 if (charbuf
>= charbuf_end
)
3493 if (byte_after_cr
>= 0)
3498 if (byte_after_cr
>= 0)
3499 c1
= byte_after_cr
, byte_after_cr
= -1;
3505 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3507 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3509 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3513 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3515 if (c1
== ISO_CODE_ESC
)
3517 if (src
+ 1 >= src_end
)
3518 goto no_more_source
;
3519 *charbuf
++ = ISO_CODE_ESC
;
3521 if (src
[0] == '%' && src
[1] == '@')
3524 consumed_chars
+= 2;
3526 /* We are sure charbuf can contain two more chars. */
3529 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3534 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3540 if ((cmp_status
->state
== COMPOSING_RULE
3541 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3542 && c1
!= ISO_CODE_ESC
)
3546 DECODE_COMPOSITION_RULE (rule
);
3547 STORE_COMPOSITION_RULE (rule
);
3551 /* We produce at most one character. */
3552 switch (iso_code_class
[c1
])
3554 case ISO_0x20_or_0x7F
:
3555 if (charset_id_0
< 0
3556 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3557 /* This is SPACE or DEL. */
3558 charset
= CHARSET_FROM_ID (charset_ascii
);
3560 charset
= CHARSET_FROM_ID (charset_id_0
);
3563 case ISO_graphic_plane_0
:
3564 if (charset_id_0
< 0)
3565 charset
= CHARSET_FROM_ID (charset_ascii
);
3567 charset
= CHARSET_FROM_ID (charset_id_0
);
3570 case ISO_0xA0_or_0xFF
:
3571 if (charset_id_1
< 0
3572 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3573 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3575 /* This is a graphic character, we fall down ... */
3577 case ISO_graphic_plane_1
:
3578 if (charset_id_1
< 0)
3580 charset
= CHARSET_FROM_ID (charset_id_1
);
3584 if (eol_dos
&& c1
== '\r')
3585 ONE_MORE_BYTE (byte_after_cr
);
3586 MAYBE_FINISH_COMPOSITION ();
3587 charset
= CHARSET_FROM_ID (charset_ascii
);
3594 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3595 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3597 CODING_ISO_INVOCATION (coding
, 0) = 1;
3598 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3602 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3604 CODING_ISO_INVOCATION (coding
, 0) = 0;
3605 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3608 case ISO_single_shift_2_7
:
3609 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3611 case ISO_single_shift_2
:
3612 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3614 /* SS2 is handled as an escape sequence of ESC 'N' */
3616 goto label_escape_sequence
;
3618 case ISO_single_shift_3
:
3619 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3621 /* SS2 is handled as an escape sequence of ESC 'O' */
3623 goto label_escape_sequence
;
3625 case ISO_control_sequence_introducer
:
3626 /* CSI is handled as an escape sequence of ESC '[' ... */
3628 goto label_escape_sequence
;
3632 label_escape_sequence
:
3633 /* Escape sequences handled here are invocation,
3634 designation, direction specification, and character
3635 composition specification. */
3638 case '&': /* revision of following character set */
3640 if (!(c1
>= '@' && c1
<= '~'))
3643 if (c1
!= ISO_CODE_ESC
)
3646 goto label_escape_sequence
;
3648 case '$': /* designation of 2-byte character set */
3649 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3655 if (c1
>= '@' && c1
<= 'B')
3656 { /* designation of JISX0208.1978, GB2312.1980,
3658 reg
= 0, chars96
= 0;
3660 else if (c1
>= 0x28 && c1
<= 0x2B)
3661 { /* designation of DIMENSION2_CHARS94 character set */
3662 reg
= c1
- 0x28, chars96
= 0;
3665 else if (c1
>= 0x2C && c1
<= 0x2F)
3666 { /* designation of DIMENSION2_CHARS96 character set */
3667 reg
= c1
- 0x2C, chars96
= 1;
3672 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3673 /* We must update these variables now. */
3675 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3677 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3683 case 'n': /* invocation of locking-shift-2 */
3684 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3685 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3687 CODING_ISO_INVOCATION (coding
, 0) = 2;
3688 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3691 case 'o': /* invocation of locking-shift-3 */
3692 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3693 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3695 CODING_ISO_INVOCATION (coding
, 0) = 3;
3696 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3699 case 'N': /* invocation of single-shift-2 */
3700 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3701 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3703 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3704 if (charset_id_2
< 0)
3705 charset
= CHARSET_FROM_ID (charset_ascii
);
3707 charset
= CHARSET_FROM_ID (charset_id_2
);
3709 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3713 case 'O': /* invocation of single-shift-3 */
3714 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3715 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3717 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3718 if (charset_id_3
< 0)
3719 charset
= CHARSET_FROM_ID (charset_ascii
);
3721 charset
= CHARSET_FROM_ID (charset_id_3
);
3723 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3727 case '0': case '2': case '3': case '4': /* start composition */
3728 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3730 if (last_id
!= charset_ascii
)
3732 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3733 last_id
= charset_ascii
;
3734 last_offset
= char_offset
;
3736 DECODE_COMPOSITION_START (c1
);
3739 case '1': /* end composition */
3740 if (cmp_status
->state
== COMPOSING_NO
)
3742 DECODE_COMPOSITION_END ();
3745 case '[': /* specification of direction */
3746 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3748 /* For the moment, nested direction is not supported.
3749 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3750 left-to-right, and nonzero means right-to-left. */
3754 case ']': /* end of the current direction */
3755 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3757 case '0': /* end of the current direction */
3758 case '1': /* start of left-to-right direction */
3761 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3766 case '2': /* start of right-to-left direction */
3769 coding
->mode
|= CODING_MODE_DIRECTION
;
3783 /* CTEXT extended segment:
3784 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3785 We keep these bytes as is for the moment.
3786 They may be decoded by post-read-conversion. */
3790 ONE_MORE_BYTE (dim
);
3791 if (dim
< '0' || dim
> '4')
3799 size
= ((M
- 128) * 128) + (L
- 128);
3800 if (charbuf
+ 6 > charbuf_end
)
3802 *charbuf
++ = ISO_CODE_ESC
;
3806 *charbuf
++ = BYTE8_TO_CHAR (M
);
3807 *charbuf
++ = BYTE8_TO_CHAR (L
);
3808 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3812 /* XFree86 extension for embedding UTF-8 in CTEXT:
3813 ESC % G --UTF-8-BYTES-- ESC % @
3814 We keep these bytes as is for the moment.
3815 They may be decoded by post-read-conversion. */
3816 if (charbuf
+ 3 > charbuf_end
)
3818 *charbuf
++ = ISO_CODE_ESC
;
3821 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3829 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3834 if (c1
>= 0x28 && c1
<= 0x2B)
3835 { /* designation of DIMENSION1_CHARS94 character set */
3836 reg
= c1
- 0x28, chars96
= 0;
3839 else if (c1
>= 0x2C && c1
<= 0x2F)
3840 { /* designation of DIMENSION1_CHARS96 character set */
3841 reg
= c1
- 0x2C, chars96
= 1;
3846 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3847 /* We must update these variables now. */
3849 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3851 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3863 if (cmp_status
->state
== COMPOSING_NO
3864 && charset
->id
!= charset_ascii
3865 && last_id
!= charset
->id
)
3867 if (last_id
!= charset_ascii
)
3868 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3869 last_id
= charset
->id
;
3870 last_offset
= char_offset
;
3873 /* Now we know CHARSET and 1st position code C1 of a character.
3874 Produce a decoded character while getting 2nd and 3rd
3875 position codes C2, C3 if necessary. */
3876 if (CHARSET_DIMENSION (charset
) > 1)
3879 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3880 || ((c1
& 0x80) != (c2
& 0x80)))
3881 /* C2 is not in a valid range. */
3883 if (CHARSET_DIMENSION (charset
) == 2)
3884 c1
= (c1
<< 8) | c2
;
3888 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3889 || ((c1
& 0x80) != (c3
& 0x80)))
3890 /* C3 is not in a valid range. */
3892 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3896 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3899 MAYBE_FINISH_COMPOSITION ();
3900 for (; src_base
< src
; src_base
++, char_offset
++)
3902 if (ASCII_BYTE_P (*src_base
))
3903 *charbuf
++ = *src_base
;
3905 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3908 else if (cmp_status
->state
== COMPOSING_NO
)
3913 else if ((cmp_status
->state
== COMPOSING_CHAR
3914 ? cmp_status
->nchars
3915 : cmp_status
->ncomps
)
3916 >= MAX_COMPOSITION_COMPONENTS
)
3918 /* Too long composition. */
3919 MAYBE_FINISH_COMPOSITION ();
3924 STORE_COMPOSITION_CHAR (c
);
3928 MAYBE_FINISH_COMPOSITION ();
3930 consumed_chars
= consumed_chars_base
;
3932 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3942 if (cmp_status
->state
!= COMPOSING_NO
)
3944 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3945 MAYBE_FINISH_COMPOSITION ();
3948 charbuf
-= cmp_status
->length
;
3949 for (i
= 0; i
< cmp_status
->length
; i
++)
3950 cmp_status
->carryover
[i
] = charbuf
[i
];
3953 else if (last_id
!= charset_ascii
)
3954 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3955 coding
->consumed_char
+= consumed_chars_base
;
3956 coding
->consumed
= src_base
- coding
->source
;
3957 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3961 /* ISO2022 encoding stuff. */
3964 It is not enough to say just "ISO2022" on encoding, we have to
3965 specify more details. In Emacs, each coding system of ISO2022
3966 variant has the following specifications:
3967 1. Initial designation to G0 thru G3.
3968 2. Allows short-form designation?
3969 3. ASCII should be designated to G0 before control characters?
3970 4. ASCII should be designated to G0 at end of line?
3971 5. 7-bit environment or 8-bit environment?
3972 6. Use locking-shift?
3973 7. Use Single-shift?
3974 And the following two are only for Japanese:
3975 8. Use ASCII in place of JIS0201-1976-Roman?
3976 9. Use JISX0208-1983 in place of JISX0208-1978?
3977 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3978 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3982 /* Produce codes (escape sequence) for designating CHARSET to graphic
3983 register REG at DST, and increment DST. If <final-char> of CHARSET is
3984 '@', 'A', or 'B' and the coding system CODING allows, produce
3985 designation sequence of short-form. */
3987 #define ENCODE_DESIGNATION(charset, reg, coding) \
3989 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3990 const char *intermediate_char_94 = "()*+"; \
3991 const char *intermediate_char_96 = ",-./"; \
3992 int revision = -1; \
3994 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3995 revision = CHARSET_ISO_REVISION (charset); \
3997 if (revision >= 0) \
3999 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4000 EMIT_ONE_BYTE ('@' + revision); \
4002 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4003 if (CHARSET_DIMENSION (charset) == 1) \
4006 if (! CHARSET_ISO_CHARS_96 (charset)) \
4007 b = intermediate_char_94[reg]; \
4009 b = intermediate_char_96[reg]; \
4010 EMIT_ONE_ASCII_BYTE (b); \
4014 EMIT_ONE_ASCII_BYTE ('$'); \
4015 if (! CHARSET_ISO_CHARS_96 (charset)) \
4017 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4019 || final_char < '@' || final_char > 'B') \
4020 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4023 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4025 EMIT_ONE_ASCII_BYTE (final_char); \
4027 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4031 /* The following two macros produce codes (control character or escape
4032 sequence) for ISO2022 single-shift functions (single-shift-2 and
4035 #define ENCODE_SINGLE_SHIFT_2 \
4037 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4038 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4040 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4041 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4045 #define ENCODE_SINGLE_SHIFT_3 \
4047 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4048 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4050 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4051 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4055 /* The following four macros produce codes (control character or
4056 escape sequence) for ISO2022 locking-shift functions (shift-in,
4057 shift-out, locking-shift-2, and locking-shift-3). */
4059 #define ENCODE_SHIFT_IN \
4061 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4062 CODING_ISO_INVOCATION (coding, 0) = 0; \
4066 #define ENCODE_SHIFT_OUT \
4068 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4069 CODING_ISO_INVOCATION (coding, 0) = 1; \
4073 #define ENCODE_LOCKING_SHIFT_2 \
4075 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4076 CODING_ISO_INVOCATION (coding, 0) = 2; \
4080 #define ENCODE_LOCKING_SHIFT_3 \
4082 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4083 CODING_ISO_INVOCATION (coding, 0) = 3; \
4087 /* Produce codes for a DIMENSION1 character whose character set is
4088 CHARSET and whose position-code is C1. Designation and invocation
4089 sequences are also produced in advance if necessary. */
4091 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4093 int id = CHARSET_ID (charset); \
4095 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4096 && id == charset_ascii) \
4098 id = charset_jisx0201_roman; \
4099 charset = CHARSET_FROM_ID (id); \
4102 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4104 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4105 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4107 EMIT_ONE_BYTE (c1 | 0x80); \
4108 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4111 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4113 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4116 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4118 EMIT_ONE_BYTE (c1 | 0x80); \
4122 /* Since CHARSET is not yet invoked to any graphic planes, we \
4123 must invoke it, or, at first, designate it to some graphic \
4124 register. Then repeat the loop to actually produce the \
4126 dst = encode_invocation_designation (charset, coding, dst, \
4131 /* Produce codes for a DIMENSION2 character whose character set is
4132 CHARSET and whose position-codes are C1 and C2. Designation and
4133 invocation codes are also produced in advance if necessary. */
4135 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4137 int id = CHARSET_ID (charset); \
4139 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4140 && id == charset_jisx0208) \
4142 id = charset_jisx0208_1978; \
4143 charset = CHARSET_FROM_ID (id); \
4146 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4148 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4149 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4151 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4152 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4155 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4157 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4160 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4162 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4166 /* Since CHARSET is not yet invoked to any graphic planes, we \
4167 must invoke it, or, at first, designate it to some graphic \
4168 register. Then repeat the loop to actually produce the \
4170 dst = encode_invocation_designation (charset, coding, dst, \
4175 #define ENCODE_ISO_CHARACTER(charset, c) \
4177 int code = ENCODE_CHAR ((charset), (c)); \
4179 if (CHARSET_DIMENSION (charset) == 1) \
4180 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4182 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4186 /* Produce designation and invocation codes at a place pointed by DST
4187 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4190 static unsigned char *
4191 encode_invocation_designation (struct charset
*charset
,
4192 struct coding_system
*coding
,
4193 unsigned char *dst
, EMACS_INT
*p_nchars
)
4195 int multibytep
= coding
->dst_multibyte
;
4196 EMACS_INT produced_chars
= *p_nchars
;
4197 int reg
; /* graphic register number */
4198 int id
= CHARSET_ID (charset
);
4200 /* At first, check designations. */
4201 for (reg
= 0; reg
< 4; reg
++)
4202 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4207 /* CHARSET is not yet designated to any graphic registers. */
4208 /* At first check the requested designation. */
4209 reg
= CODING_ISO_REQUEST (coding
, id
);
4211 /* Since CHARSET requests no special designation, designate it
4212 to graphic register 0. */
4215 ENCODE_DESIGNATION (charset
, reg
, coding
);
4218 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4219 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4221 /* Since the graphic register REG is not invoked to any graphic
4222 planes, invoke it to graphic plane 0. */
4225 case 0: /* graphic register 0 */
4229 case 1: /* graphic register 1 */
4233 case 2: /* graphic register 2 */
4234 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4235 ENCODE_SINGLE_SHIFT_2
;
4237 ENCODE_LOCKING_SHIFT_2
;
4240 case 3: /* graphic register 3 */
4241 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4242 ENCODE_SINGLE_SHIFT_3
;
4244 ENCODE_LOCKING_SHIFT_3
;
4249 *p_nchars
= produced_chars
;
4254 /* Produce codes for designation and invocation to reset the graphic
4255 planes and registers to initial state. */
4256 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4259 struct charset *charset; \
4261 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4263 for (reg = 0; reg < 4; reg++) \
4264 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4265 && (CODING_ISO_DESIGNATION (coding, reg) \
4266 != CODING_ISO_INITIAL (coding, reg))) \
4268 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4269 ENCODE_DESIGNATION (charset, reg, coding); \
4274 /* Produce designation sequences of charsets in the line started from
4275 SRC to a place pointed by DST, and return updated DST.
4277 If the current block ends before any end-of-line, we may fail to
4278 find all the necessary designations. */
4280 static unsigned char *
4281 encode_designation_at_bol (struct coding_system
*coding
, int *charbuf
,
4284 struct charset
*charset
;
4285 /* Table of charsets to be designated to each graphic register. */
4287 int c
, found
= 0, reg
;
4288 EMACS_INT produced_chars
= 0;
4289 int multibytep
= coding
->dst_multibyte
;
4291 Lisp_Object charset_list
;
4293 attrs
= CODING_ID_ATTRS (coding
->id
);
4294 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4295 if (EQ (charset_list
, Qiso_2022
))
4296 charset_list
= Viso_2022_charset_list
;
4298 for (reg
= 0; reg
< 4; reg
++)
4308 charset
= char_charset (c
, charset_list
, NULL
);
4309 id
= CHARSET_ID (charset
);
4310 reg
= CODING_ISO_REQUEST (coding
, id
);
4311 if (reg
>= 0 && r
[reg
] < 0)
4320 for (reg
= 0; reg
< 4; reg
++)
4322 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4323 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4329 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4332 encode_coding_iso_2022 (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
;
4341 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4342 && CODING_ISO_BOL (coding
));
4343 EMACS_INT produced_chars
= 0;
4344 Lisp_Object attrs
, eol_type
, charset_list
;
4345 int ascii_compatible
;
4347 int preferred_charset_id
= -1;
4349 CODING_GET_INFO (coding
, attrs
, charset_list
);
4350 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4351 if (VECTORP (eol_type
))
4354 setup_iso_safe_charsets (attrs
);
4355 /* Charset list may have been changed. */
4356 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4357 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4360 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4361 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4362 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4364 while (charbuf
< charbuf_end
)
4366 ASSURE_DESTINATION (safe_room
);
4368 if (bol_designation
)
4370 unsigned char *dst_prev
= dst
;
4372 /* We have to produce designation sequences if any now. */
4373 dst
= encode_designation_at_bol (coding
, charbuf
, dst
);
4374 bol_designation
= 0;
4375 /* We are sure that designation sequences are all ASCII bytes. */
4376 produced_chars
+= dst
- dst_prev
;
4383 /* Handle an annotation. */
4386 case CODING_ANNOTATE_COMPOSITION_MASK
:
4387 /* Not yet implemented. */
4389 case CODING_ANNOTATE_CHARSET_MASK
:
4390 preferred_charset_id
= charbuf
[2];
4391 if (preferred_charset_id
>= 0
4392 && NILP (Fmemq (make_number (preferred_charset_id
),
4394 preferred_charset_id
= -1;
4403 /* Now encode the character C. */
4404 if (c
< 0x20 || c
== 0x7F)
4407 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4409 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4410 ENCODE_RESET_PLANE_AND_REGISTER ();
4411 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4415 for (i
= 0; i
< 4; i
++)
4416 CODING_ISO_DESIGNATION (coding
, i
)
4417 = CODING_ISO_INITIAL (coding
, i
);
4420 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4422 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4423 ENCODE_RESET_PLANE_AND_REGISTER ();
4424 EMIT_ONE_ASCII_BYTE (c
);
4426 else if (ASCII_CHAR_P (c
))
4428 if (ascii_compatible
)
4429 EMIT_ONE_ASCII_BYTE (c
);
4432 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4433 ENCODE_ISO_CHARACTER (charset
, c
);
4436 else if (CHAR_BYTE8_P (c
))
4438 c
= CHAR_TO_BYTE8 (c
);
4443 struct charset
*charset
;
4445 if (preferred_charset_id
>= 0)
4447 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4448 if (! CHAR_CHARSET_P (c
, charset
))
4449 charset
= char_charset (c
, charset_list
, NULL
);
4452 charset
= char_charset (c
, charset_list
, NULL
);
4455 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4457 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4458 charset
= CHARSET_FROM_ID (charset_ascii
);
4462 c
= coding
->default_char
;
4463 charset
= char_charset (c
, charset_list
, NULL
);
4466 ENCODE_ISO_CHARACTER (charset
, c
);
4470 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4471 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4473 ASSURE_DESTINATION (safe_room
);
4474 ENCODE_RESET_PLANE_AND_REGISTER ();
4476 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4477 CODING_ISO_BOL (coding
) = bol_designation
;
4478 coding
->produced_char
+= produced_chars
;
4479 coding
->produced
= dst
- coding
->destination
;
4484 /*** 8,9. SJIS and BIG5 handlers ***/
4486 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4487 quite widely. So, for the moment, Emacs supports them in the bare
4488 C code. But, in the future, they may be supported only by CCL. */
4490 /* SJIS is a coding system encoding three character sets: ASCII, right
4491 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4492 as is. A character of charset katakana-jisx0201 is encoded by
4493 "position-code + 0x80". A character of charset japanese-jisx0208
4494 is encoded in 2-byte but two position-codes are divided and shifted
4495 so that it fit in the range below.
4497 --- CODE RANGE of SJIS ---
4498 (character set) (range)
4500 KATAKANA-JISX0201 0xA0 .. 0xDF
4501 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4502 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4503 -------------------------------
4507 /* BIG5 is a coding system encoding two character sets: ASCII and
4508 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4509 character set and is encoded in two-byte.
4511 --- CODE RANGE of BIG5 ---
4512 (character set) (range)
4514 Big5 (1st byte) 0xA1 .. 0xFE
4515 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4516 --------------------------
4520 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4521 Check if a text is encoded in SJIS. If it is, return
4522 CATEGORY_MASK_SJIS, else return 0. */
4525 detect_coding_sjis (struct coding_system
*coding
,
4526 struct coding_detection_info
*detect_info
)
4528 const unsigned char *src
= coding
->source
, *src_base
;
4529 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4530 int multibytep
= coding
->src_multibyte
;
4531 EMACS_INT consumed_chars
= 0;
4534 Lisp_Object attrs
, charset_list
;
4535 int max_first_byte_of_2_byte_code
;
4537 CODING_GET_INFO (coding
, attrs
, charset_list
);
4538 max_first_byte_of_2_byte_code
4539 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4541 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4542 /* A coding system of this category is always ASCII compatible. */
4543 src
+= coding
->head_ascii
;
4551 if ((c
>= 0x81 && c
<= 0x9F)
4552 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4555 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4557 found
= CATEGORY_MASK_SJIS
;
4559 else if (c
>= 0xA0 && c
< 0xE0)
4560 found
= CATEGORY_MASK_SJIS
;
4564 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4568 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4570 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4573 detect_info
->found
|= found
;
4577 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4578 Check if a text is encoded in BIG5. If it is, return
4579 CATEGORY_MASK_BIG5, else return 0. */
4582 detect_coding_big5 (struct coding_system
*coding
,
4583 struct coding_detection_info
*detect_info
)
4585 const unsigned char *src
= coding
->source
, *src_base
;
4586 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4587 int multibytep
= coding
->src_multibyte
;
4588 EMACS_INT consumed_chars
= 0;
4592 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4593 /* A coding system of this category is always ASCII compatible. */
4594 src
+= coding
->head_ascii
;
4605 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4607 found
= CATEGORY_MASK_BIG5
;
4612 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4616 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4618 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4621 detect_info
->found
|= found
;
4625 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4626 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4629 decode_coding_sjis (struct coding_system
*coding
)
4631 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4632 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4633 const unsigned char *src_base
;
4634 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4635 /* We may produce one charset annotation in one loop and one more at
4638 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4639 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
4640 int multibytep
= coding
->src_multibyte
;
4641 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4642 struct charset
*charset_kanji2
;
4643 Lisp_Object attrs
, charset_list
, val
;
4644 EMACS_INT char_offset
= coding
->produced_char
;
4645 EMACS_INT last_offset
= char_offset
;
4646 int last_id
= charset_ascii
;
4648 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4649 int byte_after_cr
= -1;
4651 CODING_GET_INFO (coding
, attrs
, charset_list
);
4654 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4655 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4656 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4657 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4662 struct charset
*charset
;
4665 consumed_chars_base
= consumed_chars
;
4667 if (charbuf
>= charbuf_end
)
4669 if (byte_after_cr
>= 0)
4674 if (byte_after_cr
>= 0)
4675 c
= byte_after_cr
, byte_after_cr
= -1;
4682 if (eol_dos
&& c
== '\r')
4683 ONE_MORE_BYTE (byte_after_cr
);
4684 charset
= charset_roman
;
4686 else if (c
== 0x80 || c
== 0xA0)
4688 else if (c
>= 0xA1 && c
<= 0xDF)
4690 /* SJIS -> JISX0201-Kana */
4692 charset
= charset_kana
;
4696 /* SJIS -> JISX0208 */
4698 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4702 charset
= charset_kanji
;
4704 else if (c
<= 0xFC && charset_kanji2
)
4706 /* SJIS -> JISX0213-2 */
4708 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4712 charset
= charset_kanji2
;
4716 if (charset
->id
!= charset_ascii
4717 && last_id
!= charset
->id
)
4719 if (last_id
!= charset_ascii
)
4720 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4721 last_id
= charset
->id
;
4722 last_offset
= char_offset
;
4724 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4731 consumed_chars
= consumed_chars_base
;
4733 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4739 if (last_id
!= charset_ascii
)
4740 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4741 coding
->consumed_char
+= consumed_chars_base
;
4742 coding
->consumed
= src_base
- coding
->source
;
4743 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4747 decode_coding_big5 (struct coding_system
*coding
)
4749 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4750 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4751 const unsigned char *src_base
;
4752 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4753 /* We may produce one charset annotation in one loop and one more at
4756 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4757 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
4758 int multibytep
= coding
->src_multibyte
;
4759 struct charset
*charset_roman
, *charset_big5
;
4760 Lisp_Object attrs
, charset_list
, val
;
4761 EMACS_INT char_offset
= coding
->produced_char
;
4762 EMACS_INT last_offset
= char_offset
;
4763 int last_id
= charset_ascii
;
4765 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4766 int byte_after_cr
= -1;
4768 CODING_GET_INFO (coding
, attrs
, charset_list
);
4770 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4771 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4776 struct charset
*charset
;
4779 consumed_chars_base
= consumed_chars
;
4781 if (charbuf
>= charbuf_end
)
4783 if (byte_after_cr
>= 0)
4788 if (byte_after_cr
>= 0)
4789 c
= byte_after_cr
, byte_after_cr
= -1;
4797 if (eol_dos
&& c
== '\r')
4798 ONE_MORE_BYTE (byte_after_cr
);
4799 charset
= charset_roman
;
4804 if (c
< 0xA1 || c
> 0xFE)
4807 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4810 charset
= charset_big5
;
4812 if (charset
->id
!= charset_ascii
4813 && last_id
!= charset
->id
)
4815 if (last_id
!= charset_ascii
)
4816 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4817 last_id
= charset
->id
;
4818 last_offset
= char_offset
;
4820 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4827 consumed_chars
= consumed_chars_base
;
4829 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4835 if (last_id
!= charset_ascii
)
4836 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4837 coding
->consumed_char
+= consumed_chars_base
;
4838 coding
->consumed
= src_base
- coding
->source
;
4839 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4842 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4843 This function can encode charsets `ascii', `katakana-jisx0201',
4844 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4845 are sure that all these charsets are registered as official charset
4846 (i.e. do not have extended leading-codes). Characters of other
4847 charsets are produced without any encoding. If SJIS_P is 1, encode
4848 SJIS text, else encode BIG5 text. */
4851 encode_coding_sjis (struct coding_system
*coding
)
4853 int multibytep
= coding
->dst_multibyte
;
4854 int *charbuf
= coding
->charbuf
;
4855 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4856 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4857 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4859 EMACS_INT produced_chars
= 0;
4860 Lisp_Object attrs
, charset_list
, val
;
4861 int ascii_compatible
;
4862 struct charset
*charset_kanji
, *charset_kana
;
4863 struct charset
*charset_kanji2
;
4866 CODING_GET_INFO (coding
, attrs
, charset_list
);
4867 val
= XCDR (charset_list
);
4868 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4869 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4870 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4872 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4874 while (charbuf
< charbuf_end
)
4876 ASSURE_DESTINATION (safe_room
);
4878 /* Now encode the character C. */
4879 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4880 EMIT_ONE_ASCII_BYTE (c
);
4881 else if (CHAR_BYTE8_P (c
))
4883 c
= CHAR_TO_BYTE8 (c
);
4889 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4893 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4895 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4896 charset
= CHARSET_FROM_ID (charset_ascii
);
4900 c
= coding
->default_char
;
4901 charset
= char_charset (c
, charset_list
, &code
);
4904 if (code
== CHARSET_INVALID_CODE (charset
))
4906 if (charset
== charset_kanji
)
4910 c1
= code
>> 8, c2
= code
& 0xFF;
4911 EMIT_TWO_BYTES (c1
, c2
);
4913 else if (charset
== charset_kana
)
4914 EMIT_ONE_BYTE (code
| 0x80);
4915 else if (charset_kanji2
&& charset
== charset_kanji2
)
4920 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4922 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4924 JIS_TO_SJIS2 (code
);
4925 c1
= code
>> 8, c2
= code
& 0xFF;
4926 EMIT_TWO_BYTES (c1
, c2
);
4929 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4932 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4935 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4936 coding
->produced_char
+= produced_chars
;
4937 coding
->produced
= dst
- coding
->destination
;
4942 encode_coding_big5 (struct coding_system
*coding
)
4944 int multibytep
= coding
->dst_multibyte
;
4945 int *charbuf
= coding
->charbuf
;
4946 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4947 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4948 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4950 EMACS_INT produced_chars
= 0;
4951 Lisp_Object attrs
, charset_list
, val
;
4952 int ascii_compatible
;
4953 struct charset
*charset_big5
;
4956 CODING_GET_INFO (coding
, attrs
, charset_list
);
4957 val
= XCDR (charset_list
);
4958 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4959 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4961 while (charbuf
< charbuf_end
)
4963 ASSURE_DESTINATION (safe_room
);
4965 /* Now encode the character C. */
4966 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4967 EMIT_ONE_ASCII_BYTE (c
);
4968 else if (CHAR_BYTE8_P (c
))
4970 c
= CHAR_TO_BYTE8 (c
);
4976 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4980 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4982 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4983 charset
= CHARSET_FROM_ID (charset_ascii
);
4987 c
= coding
->default_char
;
4988 charset
= char_charset (c
, charset_list
, &code
);
4991 if (code
== CHARSET_INVALID_CODE (charset
))
4993 if (charset
== charset_big5
)
4997 c1
= code
>> 8, c2
= code
& 0xFF;
4998 EMIT_TWO_BYTES (c1
, c2
);
5001 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5004 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5005 coding
->produced_char
+= produced_chars
;
5006 coding
->produced
= dst
- coding
->destination
;
5011 /*** 10. CCL handlers ***/
5013 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5014 Check if a text is encoded in a coding system of which
5015 encoder/decoder are written in CCL program. If it is, return
5016 CATEGORY_MASK_CCL, else return 0. */
5019 detect_coding_ccl (struct coding_system
*coding
,
5020 struct coding_detection_info
*detect_info
)
5022 const unsigned char *src
= coding
->source
, *src_base
;
5023 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5024 int multibytep
= coding
->src_multibyte
;
5025 EMACS_INT consumed_chars
= 0;
5027 unsigned char *valids
;
5028 EMACS_INT head_ascii
= coding
->head_ascii
;
5031 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5033 coding
= &coding_categories
[coding_category_ccl
];
5034 valids
= CODING_CCL_VALIDS (coding
);
5035 attrs
= CODING_ID_ATTRS (coding
->id
);
5036 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5045 if (c
< 0 || ! valids
[c
])
5047 if ((valids
[c
] > 1))
5048 found
= CATEGORY_MASK_CCL
;
5050 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5054 detect_info
->found
|= found
;
5059 decode_coding_ccl (struct coding_system
*coding
)
5061 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5062 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5063 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5064 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5065 EMACS_INT consumed_chars
= 0;
5066 int multibytep
= coding
->src_multibyte
;
5067 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5068 int source_charbuf
[1024];
5069 int source_byteidx
[1025];
5070 Lisp_Object attrs
, charset_list
;
5072 CODING_GET_INFO (coding
, attrs
, charset_list
);
5076 const unsigned char *p
= src
;
5081 while (i
< 1024 && p
< src_end
)
5083 source_byteidx
[i
] = p
- src
;
5084 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5086 source_byteidx
[i
] = p
- src
;
5089 while (i
< 1024 && p
< src_end
)
5090 source_charbuf
[i
++] = *p
++;
5092 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5093 ccl
->last_block
= 1;
5094 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5096 charbuf
+= ccl
->produced
;
5098 src
+= source_byteidx
[ccl
->consumed
];
5100 src
+= ccl
->consumed
;
5101 consumed_chars
+= ccl
->consumed
;
5102 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5106 switch (ccl
->status
)
5108 case CCL_STAT_SUSPEND_BY_SRC
:
5109 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5111 case CCL_STAT_SUSPEND_BY_DST
:
5112 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5115 case CCL_STAT_INVALID_CMD
:
5116 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5119 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5122 coding
->consumed_char
+= consumed_chars
;
5123 coding
->consumed
= src
- coding
->source
;
5124 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5128 encode_coding_ccl (struct coding_system
*coding
)
5130 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5131 int multibytep
= coding
->dst_multibyte
;
5132 int *charbuf
= coding
->charbuf
;
5133 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5134 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5135 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5136 int destination_charbuf
[1024];
5137 EMACS_INT produced_chars
= 0;
5139 Lisp_Object attrs
, charset_list
;
5141 CODING_GET_INFO (coding
, attrs
, charset_list
);
5142 if (coding
->consumed_char
== coding
->src_chars
5143 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5144 ccl
->last_block
= 1;
5146 while (charbuf
< charbuf_end
)
5148 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5149 charbuf_end
- charbuf
, 1024, charset_list
);
5152 ASSURE_DESTINATION (ccl
->produced
* 2);
5153 for (i
= 0; i
< ccl
->produced
; i
++)
5154 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5158 ASSURE_DESTINATION (ccl
->produced
);
5159 for (i
= 0; i
< ccl
->produced
; i
++)
5160 *dst
++ = destination_charbuf
[i
] & 0xFF;
5161 produced_chars
+= ccl
->produced
;
5163 charbuf
+= ccl
->consumed
;
5164 if (ccl
->status
== CCL_STAT_QUIT
5165 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5169 switch (ccl
->status
)
5171 case CCL_STAT_SUSPEND_BY_SRC
:
5172 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5174 case CCL_STAT_SUSPEND_BY_DST
:
5175 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5178 case CCL_STAT_INVALID_CMD
:
5179 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5182 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5186 coding
->produced_char
+= produced_chars
;
5187 coding
->produced
= dst
- coding
->destination
;
5193 /*** 10, 11. no-conversion handlers ***/
5195 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5198 decode_coding_raw_text (struct coding_system
*coding
)
5201 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5203 coding
->chars_at_source
= 1;
5204 coding
->consumed_char
= coding
->src_chars
;
5205 coding
->consumed
= coding
->src_bytes
;
5206 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5208 coding
->consumed_char
--;
5210 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5213 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5217 encode_coding_raw_text (struct coding_system
*coding
)
5219 int multibytep
= coding
->dst_multibyte
;
5220 int *charbuf
= coding
->charbuf
;
5221 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5222 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5223 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5224 EMACS_INT produced_chars
= 0;
5229 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5231 if (coding
->src_multibyte
)
5232 while (charbuf
< charbuf_end
)
5234 ASSURE_DESTINATION (safe_room
);
5236 if (ASCII_CHAR_P (c
))
5237 EMIT_ONE_ASCII_BYTE (c
);
5238 else if (CHAR_BYTE8_P (c
))
5240 c
= CHAR_TO_BYTE8 (c
);
5245 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5247 CHAR_STRING_ADVANCE (c
, p1
);
5250 EMIT_ONE_BYTE (*p0
);
5257 while (charbuf
< charbuf_end
)
5259 ASSURE_DESTINATION (safe_room
);
5266 if (coding
->src_multibyte
)
5268 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5270 while (charbuf
< charbuf_end
)
5272 ASSURE_DESTINATION (safe_room
);
5274 if (ASCII_CHAR_P (c
))
5276 else if (CHAR_BYTE8_P (c
))
5277 *dst
++ = CHAR_TO_BYTE8 (c
);
5279 CHAR_STRING_ADVANCE (c
, dst
);
5284 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5285 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5286 *dst
++ = *charbuf
++;
5288 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5290 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5291 coding
->produced_char
+= produced_chars
;
5292 coding
->produced
= dst
- coding
->destination
;
5296 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5297 Check if a text is encoded in a charset-based coding system. If it
5298 is, return 1, else return 0. */
5301 detect_coding_charset (struct coding_system
*coding
,
5302 struct coding_detection_info
*detect_info
)
5304 const unsigned char *src
= coding
->source
, *src_base
;
5305 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5306 int multibytep
= coding
->src_multibyte
;
5307 EMACS_INT consumed_chars
= 0;
5308 Lisp_Object attrs
, valids
, name
;
5310 EMACS_INT head_ascii
= coding
->head_ascii
;
5311 int check_latin_extra
= 0;
5313 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5315 coding
= &coding_categories
[coding_category_charset
];
5316 attrs
= CODING_ID_ATTRS (coding
->id
);
5317 valids
= AREF (attrs
, coding_attr_charset_valids
);
5318 name
= CODING_ID_NAME (coding
->id
);
5319 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5320 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5321 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5322 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5323 check_latin_extra
= 1;
5325 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5332 struct charset
*charset
;
5339 val
= AREF (valids
, c
);
5345 && check_latin_extra
5346 && (!VECTORP (Vlatin_extra_code_table
)
5347 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5349 found
= CATEGORY_MASK_CHARSET
;
5353 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5354 dim
= CHARSET_DIMENSION (charset
);
5355 for (idx
= 1; idx
< dim
; idx
++)
5360 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5361 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5370 for (; CONSP (val
); val
= XCDR (val
))
5372 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5373 dim
= CHARSET_DIMENSION (charset
);
5379 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5380 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5395 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5399 detect_info
->found
|= found
;
5404 decode_coding_charset (struct coding_system
*coding
)
5406 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5407 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5408 const unsigned char *src_base
;
5409 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5410 /* We may produce one charset annotation in one loop and one more at
5413 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5414 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
5415 int multibytep
= coding
->src_multibyte
;
5416 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5418 EMACS_INT char_offset
= coding
->produced_char
;
5419 EMACS_INT last_offset
= char_offset
;
5420 int last_id
= charset_ascii
;
5422 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5423 int byte_after_cr
= -1;
5425 valids
= AREF (attrs
, coding_attr_charset_valids
);
5431 struct charset
*charset
;
5437 consumed_chars_base
= consumed_chars
;
5439 if (charbuf
>= charbuf_end
)
5441 if (byte_after_cr
>= 0)
5446 if (byte_after_cr
>= 0)
5454 if (eol_dos
&& c
== '\r')
5455 ONE_MORE_BYTE (byte_after_cr
);
5461 val
= AREF (valids
, c
);
5462 if (! INTEGERP (val
) && ! CONSP (val
))
5466 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5467 dim
= CHARSET_DIMENSION (charset
);
5471 code
= (code
<< 8) | c
;
5474 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5479 /* VAL is a list of charset IDs. It is assured that the
5480 list is sorted by charset dimensions (smaller one
5484 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5485 dim
= CHARSET_DIMENSION (charset
);
5489 code
= (code
<< 8) | c
;
5492 CODING_DECODE_CHAR (coding
, src
, src_base
,
5493 src_end
, charset
, code
, c
);
5501 if (charset
->id
!= charset_ascii
5502 && last_id
!= charset
->id
)
5504 if (last_id
!= charset_ascii
)
5505 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5506 last_id
= charset
->id
;
5507 last_offset
= char_offset
;
5516 consumed_chars
= consumed_chars_base
;
5518 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5524 if (last_id
!= charset_ascii
)
5525 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5526 coding
->consumed_char
+= consumed_chars_base
;
5527 coding
->consumed
= src_base
- coding
->source
;
5528 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5532 encode_coding_charset (struct coding_system
*coding
)
5534 int multibytep
= coding
->dst_multibyte
;
5535 int *charbuf
= coding
->charbuf
;
5536 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5537 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5538 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5539 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5540 EMACS_INT produced_chars
= 0;
5541 Lisp_Object attrs
, charset_list
;
5542 int ascii_compatible
;
5545 CODING_GET_INFO (coding
, attrs
, charset_list
);
5546 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5548 while (charbuf
< charbuf_end
)
5550 struct charset
*charset
;
5553 ASSURE_DESTINATION (safe_room
);
5555 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5556 EMIT_ONE_ASCII_BYTE (c
);
5557 else if (CHAR_BYTE8_P (c
))
5559 c
= CHAR_TO_BYTE8 (c
);
5564 charset
= char_charset (c
, charset_list
, &code
);
5567 if (CHARSET_DIMENSION (charset
) == 1)
5568 EMIT_ONE_BYTE (code
);
5569 else if (CHARSET_DIMENSION (charset
) == 2)
5570 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5571 else if (CHARSET_DIMENSION (charset
) == 3)
5572 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5574 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5575 (code
>> 8) & 0xFF, code
& 0xFF);
5579 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5580 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5582 c
= coding
->default_char
;
5588 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5589 coding
->produced_char
+= produced_chars
;
5590 coding
->produced
= dst
- coding
->destination
;
5595 /*** 7. C library functions ***/
5597 /* Setup coding context CODING from information about CODING_SYSTEM.
5598 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5599 CODING_SYSTEM is invalid, signal an error. */
5602 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5605 Lisp_Object eol_type
;
5606 Lisp_Object coding_type
;
5609 if (NILP (coding_system
))
5610 coding_system
= Qundecided
;
5612 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5614 attrs
= CODING_ID_ATTRS (coding
->id
);
5615 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5618 coding
->head_ascii
= -1;
5619 if (VECTORP (eol_type
))
5620 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5621 | CODING_REQUIRE_DETECTION_MASK
);
5622 else if (! EQ (eol_type
, Qunix
))
5623 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5624 | CODING_REQUIRE_ENCODING_MASK
);
5626 coding
->common_flags
= 0;
5627 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5628 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5629 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5630 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5631 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5632 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5634 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5635 coding
->max_charset_id
= SCHARS (val
) - 1;
5636 coding
->safe_charsets
= SDATA (val
);
5637 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5638 coding
->carryover_bytes
= 0;
5640 coding_type
= CODING_ATTR_TYPE (attrs
);
5641 if (EQ (coding_type
, Qundecided
))
5643 coding
->detector
= NULL
;
5644 coding
->decoder
= decode_coding_raw_text
;
5645 coding
->encoder
= encode_coding_raw_text
;
5646 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5648 else if (EQ (coding_type
, Qiso_2022
))
5651 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5653 /* Invoke graphic register 0 to plane 0. */
5654 CODING_ISO_INVOCATION (coding
, 0) = 0;
5655 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5656 CODING_ISO_INVOCATION (coding
, 1)
5657 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5658 /* Setup the initial status of designation. */
5659 for (i
= 0; i
< 4; i
++)
5660 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5661 /* Not single shifting initially. */
5662 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5663 /* Beginning of buffer should also be regarded as bol. */
5664 CODING_ISO_BOL (coding
) = 1;
5665 coding
->detector
= detect_coding_iso_2022
;
5666 coding
->decoder
= decode_coding_iso_2022
;
5667 coding
->encoder
= encode_coding_iso_2022
;
5668 if (flags
& CODING_ISO_FLAG_SAFE
)
5669 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5670 coding
->common_flags
5671 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5672 | CODING_REQUIRE_FLUSHING_MASK
);
5673 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5674 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5675 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5676 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5677 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5679 setup_iso_safe_charsets (attrs
);
5680 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5681 coding
->max_charset_id
= SCHARS (val
) - 1;
5682 coding
->safe_charsets
= SDATA (val
);
5684 CODING_ISO_FLAGS (coding
) = flags
;
5685 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5686 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5687 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5688 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5690 else if (EQ (coding_type
, Qcharset
))
5692 coding
->detector
= detect_coding_charset
;
5693 coding
->decoder
= decode_coding_charset
;
5694 coding
->encoder
= encode_coding_charset
;
5695 coding
->common_flags
5696 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5698 else if (EQ (coding_type
, Qutf_8
))
5700 val
= AREF (attrs
, coding_attr_utf_bom
);
5701 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5702 : EQ (val
, Qt
) ? utf_with_bom
5704 coding
->detector
= detect_coding_utf_8
;
5705 coding
->decoder
= decode_coding_utf_8
;
5706 coding
->encoder
= encode_coding_utf_8
;
5707 coding
->common_flags
5708 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5709 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5710 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5712 else if (EQ (coding_type
, Qutf_16
))
5714 val
= AREF (attrs
, coding_attr_utf_bom
);
5715 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5716 : EQ (val
, Qt
) ? utf_with_bom
5718 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5719 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5720 : utf_16_little_endian
);
5721 CODING_UTF_16_SURROGATE (coding
) = 0;
5722 coding
->detector
= detect_coding_utf_16
;
5723 coding
->decoder
= decode_coding_utf_16
;
5724 coding
->encoder
= encode_coding_utf_16
;
5725 coding
->common_flags
5726 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5727 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5728 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5730 else if (EQ (coding_type
, Qccl
))
5732 coding
->detector
= detect_coding_ccl
;
5733 coding
->decoder
= decode_coding_ccl
;
5734 coding
->encoder
= encode_coding_ccl
;
5735 coding
->common_flags
5736 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5737 | CODING_REQUIRE_FLUSHING_MASK
);
5739 else if (EQ (coding_type
, Qemacs_mule
))
5741 coding
->detector
= detect_coding_emacs_mule
;
5742 coding
->decoder
= decode_coding_emacs_mule
;
5743 coding
->encoder
= encode_coding_emacs_mule
;
5744 coding
->common_flags
5745 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5746 coding
->spec
.emacs_mule
.full_support
= 1;
5747 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5748 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5750 Lisp_Object tail
, safe_charsets
;
5751 int max_charset_id
= 0;
5753 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5755 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5756 max_charset_id
= XFASTINT (XCAR (tail
));
5757 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5758 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5759 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5761 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5762 coding
->max_charset_id
= max_charset_id
;
5763 coding
->safe_charsets
= SDATA (safe_charsets
);
5764 coding
->spec
.emacs_mule
.full_support
= 1;
5766 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5767 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5769 else if (EQ (coding_type
, Qshift_jis
))
5771 coding
->detector
= detect_coding_sjis
;
5772 coding
->decoder
= decode_coding_sjis
;
5773 coding
->encoder
= encode_coding_sjis
;
5774 coding
->common_flags
5775 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5777 else if (EQ (coding_type
, Qbig5
))
5779 coding
->detector
= detect_coding_big5
;
5780 coding
->decoder
= decode_coding_big5
;
5781 coding
->encoder
= encode_coding_big5
;
5782 coding
->common_flags
5783 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5785 else /* EQ (coding_type, Qraw_text) */
5787 coding
->detector
= NULL
;
5788 coding
->decoder
= decode_coding_raw_text
;
5789 coding
->encoder
= encode_coding_raw_text
;
5790 if (! EQ (eol_type
, Qunix
))
5792 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5793 if (! VECTORP (eol_type
))
5794 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5802 /* Return a list of charsets supported by CODING. */
5805 coding_charset_list (struct coding_system
*coding
)
5807 Lisp_Object attrs
, charset_list
;
5809 CODING_GET_INFO (coding
, attrs
, charset_list
);
5810 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5812 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5814 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5815 charset_list
= Viso_2022_charset_list
;
5817 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5819 charset_list
= Vemacs_mule_charset_list
;
5821 return charset_list
;
5825 /* Return a list of charsets supported by CODING-SYSTEM. */
5828 coding_system_charset_list (Lisp_Object coding_system
)
5831 Lisp_Object attrs
, charset_list
;
5833 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5834 attrs
= CODING_ID_ATTRS (id
);
5836 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5838 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5840 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5841 charset_list
= Viso_2022_charset_list
;
5843 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5845 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5847 charset_list
= Vemacs_mule_charset_list
;
5851 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5853 return charset_list
;
5857 /* Return raw-text or one of its subsidiaries that has the same
5858 eol_type as CODING-SYSTEM. */
5861 raw_text_coding_system (Lisp_Object coding_system
)
5863 Lisp_Object spec
, attrs
;
5864 Lisp_Object eol_type
, raw_text_eol_type
;
5866 if (NILP (coding_system
))
5868 spec
= CODING_SYSTEM_SPEC (coding_system
);
5869 attrs
= AREF (spec
, 0);
5871 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5872 return coding_system
;
5874 eol_type
= AREF (spec
, 2);
5875 if (VECTORP (eol_type
))
5877 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5878 raw_text_eol_type
= AREF (spec
, 2);
5879 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5880 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5881 : AREF (raw_text_eol_type
, 2));
5885 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5886 the subsidiary that has the same eol-spec as PARENT (if it is not
5887 nil and specifies end-of-line format) or the system's setting
5888 (system_eol_type). */
5891 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5893 Lisp_Object spec
, eol_type
;
5895 if (NILP (coding_system
))
5896 coding_system
= Qraw_text
;
5897 spec
= CODING_SYSTEM_SPEC (coding_system
);
5898 eol_type
= AREF (spec
, 2);
5899 if (VECTORP (eol_type
))
5901 Lisp_Object parent_eol_type
;
5903 if (! NILP (parent
))
5905 Lisp_Object parent_spec
;
5907 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5908 parent_eol_type
= AREF (parent_spec
, 2);
5909 if (VECTORP (parent_eol_type
))
5910 parent_eol_type
= system_eol_type
;
5913 parent_eol_type
= system_eol_type
;
5914 if (EQ (parent_eol_type
, Qunix
))
5915 coding_system
= AREF (eol_type
, 0);
5916 else if (EQ (parent_eol_type
, Qdos
))
5917 coding_system
= AREF (eol_type
, 1);
5918 else if (EQ (parent_eol_type
, Qmac
))
5919 coding_system
= AREF (eol_type
, 2);
5921 return coding_system
;
5925 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5926 decided for writing to a process. If not, complement them, and
5927 return a new coding system. */
5930 complement_process_encoding_system (Lisp_Object coding_system
)
5932 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5933 Lisp_Object spec
, attrs
;
5936 for (i
= 0; i
< 3; i
++)
5939 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
5941 coding_system
= preferred_coding_system ();
5942 spec
= CODING_SYSTEM_SPEC (coding_system
);
5945 attrs
= AREF (spec
, 0);
5946 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
5947 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
5948 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
5949 eol_base
= coding_system
;
5950 if (! NILP (coding_base
) && ! NILP (eol_base
))
5955 /* The original CODING_SYSTEM didn't specify text-conversion or
5956 eol-conversion. Be sure that we return a fully complemented
5958 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
5959 return coding_system
;
5963 /* Emacs has a mechanism to automatically detect a coding system if it
5964 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5965 it's impossible to distinguish some coding systems accurately
5966 because they use the same range of codes. So, at first, coding
5967 systems are categorized into 7, those are:
5969 o coding-category-emacs-mule
5971 The category for a coding system which has the same code range
5972 as Emacs' internal format. Assigned the coding-system (Lisp
5973 symbol) `emacs-mule' by default.
5975 o coding-category-sjis
5977 The category for a coding system which has the same code range
5978 as SJIS. Assigned the coding-system (Lisp
5979 symbol) `japanese-shift-jis' by default.
5981 o coding-category-iso-7
5983 The category for a coding system which has the same code range
5984 as ISO2022 of 7-bit environment. This doesn't use any locking
5985 shift and single shift functions. This can encode/decode all
5986 charsets. Assigned the coding-system (Lisp symbol)
5987 `iso-2022-7bit' by default.
5989 o coding-category-iso-7-tight
5991 Same as coding-category-iso-7 except that this can
5992 encode/decode only the specified charsets.
5994 o coding-category-iso-8-1
5996 The category for a coding system which has the same code range
5997 as ISO2022 of 8-bit environment and graphic plane 1 used only
5998 for DIMENSION1 charset. This doesn't use any locking shift
5999 and single shift functions. Assigned the coding-system (Lisp
6000 symbol) `iso-latin-1' by default.
6002 o coding-category-iso-8-2
6004 The category for a coding system which has the same code range
6005 as ISO2022 of 8-bit environment and graphic plane 1 used only
6006 for DIMENSION2 charset. This doesn't use any locking shift
6007 and single shift functions. Assigned the coding-system (Lisp
6008 symbol) `japanese-iso-8bit' by default.
6010 o coding-category-iso-7-else
6012 The category for a coding system which has the same code range
6013 as ISO2022 of 7-bit environment but uses locking shift or
6014 single shift functions. Assigned the coding-system (Lisp
6015 symbol) `iso-2022-7bit-lock' by default.
6017 o coding-category-iso-8-else
6019 The category for a coding system which has the same code range
6020 as ISO2022 of 8-bit environment but uses locking shift or
6021 single shift functions. Assigned the coding-system (Lisp
6022 symbol) `iso-2022-8bit-ss2' by default.
6024 o coding-category-big5
6026 The category for a coding system which has the same code range
6027 as BIG5. Assigned the coding-system (Lisp symbol)
6028 `cn-big5' by default.
6030 o coding-category-utf-8
6032 The category for a coding system which has the same code range
6033 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6034 symbol) `utf-8' by default.
6036 o coding-category-utf-16-be
6038 The category for a coding system in which a text has an
6039 Unicode signature (cf. Unicode Standard) in the order of BIG
6040 endian at the head. Assigned the coding-system (Lisp symbol)
6041 `utf-16-be' by default.
6043 o coding-category-utf-16-le
6045 The category for a coding system in which a text has an
6046 Unicode signature (cf. Unicode Standard) in the order of
6047 LITTLE endian at the head. Assigned the coding-system (Lisp
6048 symbol) `utf-16-le' by default.
6050 o coding-category-ccl
6052 The category for a coding system of which encoder/decoder is
6053 written in CCL programs. The default value is nil, i.e., no
6054 coding system is assigned.
6056 o coding-category-binary
6058 The category for a coding system not categorized in any of the
6059 above. Assigned the coding-system (Lisp symbol)
6060 `no-conversion' by default.
6062 Each of them is a Lisp symbol and the value is an actual
6063 `coding-system's (this is also a Lisp symbol) assigned by a user.
6064 What Emacs does actually is to detect a category of coding system.
6065 Then, it uses a `coding-system' assigned to it. If Emacs can't
6066 decide only one possible category, it selects a category of the
6067 highest priority. Priorities of categories are also specified by a
6068 user in a Lisp variable `coding-category-list'.
6072 #define EOL_SEEN_NONE 0
6073 #define EOL_SEEN_LF 1
6074 #define EOL_SEEN_CR 2
6075 #define EOL_SEEN_CRLF 4
6077 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6078 SOURCE is encoded. If CATEGORY is one of
6079 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6080 two-byte, else they are encoded by one-byte.
6082 Return one of EOL_SEEN_XXX. */
6084 #define MAX_EOL_CHECK_COUNT 3
6087 detect_eol (const unsigned char *source
, EMACS_INT src_bytes
,
6088 enum coding_category category
)
6090 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6093 int eol_seen
= EOL_SEEN_NONE
;
6095 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6099 msb
= category
== (coding_category_utf_16_le
6100 | coding_category_utf_16_le_nosig
);
6103 while (src
+ 1 < src_end
)
6106 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6111 this_eol
= EOL_SEEN_LF
;
6112 else if (src
+ 3 >= src_end
6113 || src
[msb
+ 2] != 0
6114 || src
[lsb
+ 2] != '\n')
6115 this_eol
= EOL_SEEN_CR
;
6118 this_eol
= EOL_SEEN_CRLF
;
6122 if (eol_seen
== EOL_SEEN_NONE
)
6123 /* This is the first end-of-line. */
6124 eol_seen
= this_eol
;
6125 else if (eol_seen
!= this_eol
)
6127 /* The found type is different from what found before.
6128 Allow for stray ^M characters in DOS EOL files. */
6129 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6130 || (eol_seen
== EOL_SEEN_CRLF
6131 && this_eol
== EOL_SEEN_CR
))
6132 eol_seen
= EOL_SEEN_CRLF
;
6135 eol_seen
= EOL_SEEN_LF
;
6139 if (++total
== MAX_EOL_CHECK_COUNT
)
6146 while (src
< src_end
)
6149 if (c
== '\n' || c
== '\r')
6154 this_eol
= EOL_SEEN_LF
;
6155 else if (src
>= src_end
|| *src
!= '\n')
6156 this_eol
= EOL_SEEN_CR
;
6158 this_eol
= EOL_SEEN_CRLF
, src
++;
6160 if (eol_seen
== EOL_SEEN_NONE
)
6161 /* This is the first end-of-line. */
6162 eol_seen
= this_eol
;
6163 else if (eol_seen
!= this_eol
)
6165 /* The found type is different from what found before.
6166 Allow for stray ^M characters in DOS EOL files. */
6167 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6168 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6169 eol_seen
= EOL_SEEN_CRLF
;
6172 eol_seen
= EOL_SEEN_LF
;
6176 if (++total
== MAX_EOL_CHECK_COUNT
)
6185 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6187 Lisp_Object eol_type
;
6189 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6190 if (eol_seen
& EOL_SEEN_LF
)
6192 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6195 else if (eol_seen
& EOL_SEEN_CRLF
)
6197 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6200 else if (eol_seen
& EOL_SEEN_CR
)
6202 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6208 /* Detect how a text specified in CODING is encoded. If a coding
6209 system is detected, update fields of CODING by the detected coding
6213 detect_coding (struct coding_system
*coding
)
6215 const unsigned char *src
, *src_end
;
6216 int saved_mode
= coding
->mode
;
6218 coding
->consumed
= coding
->consumed_char
= 0;
6219 coding
->produced
= coding
->produced_char
= 0;
6220 coding_set_source (coding
);
6222 src_end
= coding
->source
+ coding
->src_bytes
;
6223 coding
->head_ascii
= 0;
6225 /* If we have not yet decided the text encoding type, detect it
6227 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6230 struct coding_detection_info detect_info
;
6231 int null_byte_found
= 0, eight_bit_found
= 0;
6233 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6234 for (src
= coding
->source
; src
< src_end
; src
++)
6239 eight_bit_found
= 1;
6240 if (null_byte_found
)
6245 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6246 && ! inhibit_iso_escape_detection
6247 && ! detect_info
.checked
)
6249 if (detect_coding_iso_2022 (coding
, &detect_info
))
6251 /* We have scanned the whole data. */
6252 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6254 /* We didn't find an 8-bit code. We may
6255 have found a null-byte, but it's very
6256 rare that a binary file conforms to
6259 coding
->head_ascii
= src
- coding
->source
;
6261 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6265 else if (! c
&& !inhibit_null_byte_detection
)
6267 null_byte_found
= 1;
6268 if (eight_bit_found
)
6271 if (! eight_bit_found
)
6272 coding
->head_ascii
++;
6274 else if (! eight_bit_found
)
6275 coding
->head_ascii
++;
6278 if (null_byte_found
|| eight_bit_found
6279 || coding
->head_ascii
< coding
->src_bytes
6280 || detect_info
.found
)
6282 enum coding_category category
;
6283 struct coding_system
*this;
6285 if (coding
->head_ascii
== coding
->src_bytes
)
6286 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6287 for (i
= 0; i
< coding_category_raw_text
; i
++)
6289 category
= coding_priorities
[i
];
6290 this = coding_categories
+ category
;
6291 if (detect_info
.found
& (1 << category
))
6296 if (null_byte_found
)
6298 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6299 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6301 for (i
= 0; i
< coding_category_raw_text
; i
++)
6303 category
= coding_priorities
[i
];
6304 this = coding_categories
+ category
;
6307 /* No coding system of this category is defined. */
6308 detect_info
.rejected
|= (1 << category
);
6310 else if (category
>= coding_category_raw_text
)
6312 else if (detect_info
.checked
& (1 << category
))
6314 if (detect_info
.found
& (1 << category
))
6317 else if ((*(this->detector
)) (coding
, &detect_info
)
6318 && detect_info
.found
& (1 << category
))
6320 if (category
== coding_category_utf_16_auto
)
6322 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6323 category
= coding_category_utf_16_le
;
6325 category
= coding_category_utf_16_be
;
6332 if (i
< coding_category_raw_text
)
6333 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6334 else if (null_byte_found
)
6335 setup_coding_system (Qno_conversion
, coding
);
6336 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6337 == CATEGORY_MASK_ANY
)
6338 setup_coding_system (Qraw_text
, coding
);
6339 else if (detect_info
.rejected
)
6340 for (i
= 0; i
< coding_category_raw_text
; i
++)
6341 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6343 this = coding_categories
+ coding_priorities
[i
];
6344 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6349 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6350 == coding_category_utf_8_auto
)
6352 Lisp_Object coding_systems
;
6353 struct coding_detection_info detect_info
;
6356 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6357 detect_info
.found
= detect_info
.rejected
= 0;
6358 coding
->head_ascii
= 0;
6359 if (CONSP (coding_systems
)
6360 && detect_coding_utf_8 (coding
, &detect_info
))
6362 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6363 setup_coding_system (XCAR (coding_systems
), coding
);
6365 setup_coding_system (XCDR (coding_systems
), coding
);
6368 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6369 == coding_category_utf_16_auto
)
6371 Lisp_Object coding_systems
;
6372 struct coding_detection_info detect_info
;
6375 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6376 detect_info
.found
= detect_info
.rejected
= 0;
6377 coding
->head_ascii
= 0;
6378 if (CONSP (coding_systems
)
6379 && detect_coding_utf_16 (coding
, &detect_info
))
6381 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6382 setup_coding_system (XCAR (coding_systems
), coding
);
6383 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6384 setup_coding_system (XCDR (coding_systems
), coding
);
6387 coding
->mode
= saved_mode
;
6392 decode_eol (struct coding_system
*coding
)
6394 Lisp_Object eol_type
;
6395 unsigned char *p
, *pbeg
, *pend
;
6397 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6398 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6401 if (NILP (coding
->dst_object
))
6402 pbeg
= coding
->destination
;
6404 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6405 pend
= pbeg
+ coding
->produced
;
6407 if (VECTORP (eol_type
))
6409 int eol_seen
= EOL_SEEN_NONE
;
6411 for (p
= pbeg
; p
< pend
; p
++)
6414 eol_seen
|= EOL_SEEN_LF
;
6415 else if (*p
== '\r')
6417 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6419 eol_seen
|= EOL_SEEN_CRLF
;
6423 eol_seen
|= EOL_SEEN_CR
;
6426 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6427 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6428 && (eol_seen
& EOL_SEEN_CR
) != 0
6429 && (eol_seen
& EOL_SEEN_LF
) == 0)
6430 eol_seen
= EOL_SEEN_CRLF
;
6431 else if (eol_seen
!= EOL_SEEN_NONE
6432 && eol_seen
!= EOL_SEEN_LF
6433 && eol_seen
!= EOL_SEEN_CRLF
6434 && eol_seen
!= EOL_SEEN_CR
)
6435 eol_seen
= EOL_SEEN_LF
;
6436 if (eol_seen
!= EOL_SEEN_NONE
)
6437 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6440 if (EQ (eol_type
, Qmac
))
6442 for (p
= pbeg
; p
< pend
; p
++)
6446 else if (EQ (eol_type
, Qdos
))
6450 if (NILP (coding
->dst_object
))
6452 /* Start deleting '\r' from the tail to minimize the memory
6454 for (p
= pend
- 2; p
>= pbeg
; p
--)
6457 memmove (p
, p
+ 1, pend
-- - p
- 1);
6463 EMACS_INT pos_byte
= coding
->dst_pos_byte
;
6464 EMACS_INT pos
= coding
->dst_pos
;
6465 EMACS_INT pos_end
= pos
+ coding
->produced_char
- 1;
6467 while (pos
< pos_end
)
6469 p
= BYTE_POS_ADDR (pos_byte
);
6470 if (*p
== '\r' && p
[1] == '\n')
6472 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6477 if (coding
->dst_multibyte
)
6478 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6483 coding
->produced
-= n
;
6484 coding
->produced_char
-= n
;
6489 /* Return a translation table (or list of them) from coding system
6490 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6491 decoding (ENCODEP is zero). */
6494 get_translation_table (Lisp_Object attrs
, int encodep
, int *max_lookup
)
6496 Lisp_Object standard
, translation_table
;
6499 if (NILP (Venable_character_translation
))
6506 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6507 standard
= Vstandard_translation_table_for_encode
;
6509 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6510 standard
= Vstandard_translation_table_for_decode
;
6511 if (NILP (translation_table
))
6512 translation_table
= standard
;
6515 if (SYMBOLP (translation_table
))
6516 translation_table
= Fget (translation_table
, Qtranslation_table
);
6517 else if (CONSP (translation_table
))
6519 translation_table
= Fcopy_sequence (translation_table
);
6520 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6521 if (SYMBOLP (XCAR (val
)))
6522 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6524 if (CHAR_TABLE_P (standard
))
6526 if (CONSP (translation_table
))
6527 translation_table
= nconc2 (translation_table
,
6528 Fcons (standard
, Qnil
));
6530 translation_table
= Fcons (translation_table
,
6531 Fcons (standard
, Qnil
));
6538 if (CHAR_TABLE_P (translation_table
)
6539 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6541 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6542 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6543 *max_lookup
= XFASTINT (val
);
6545 else if (CONSP (translation_table
))
6549 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6550 if (CHAR_TABLE_P (XCAR (tail
))
6551 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6553 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6554 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6555 *max_lookup
= XFASTINT (tailval
);
6559 return translation_table
;
6562 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6565 if (CHAR_TABLE_P (table)) \
6567 trans = CHAR_TABLE_REF (table, c); \
6568 if (CHARACTERP (trans)) \
6569 c = XFASTINT (trans), trans = Qnil; \
6571 else if (CONSP (table)) \
6575 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6576 if (CHAR_TABLE_P (XCAR (tail))) \
6578 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6579 if (CHARACTERP (trans)) \
6580 c = XFASTINT (trans), trans = Qnil; \
6581 else if (! NILP (trans)) \
6588 /* Return a translation of character(s) at BUF according to TRANS.
6589 TRANS is TO-CHAR or ((FROM . TO) ...) where
6590 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6591 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6592 translation is found, and Qnil if not found..
6593 If BUF is too short to lookup characters in FROM, return Qt. */
6596 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6599 if (INTEGERP (trans
))
6601 for (; CONSP (trans
); trans
= XCDR (trans
))
6603 Lisp_Object val
= XCAR (trans
);
6604 Lisp_Object from
= XCAR (val
);
6605 int len
= ASIZE (from
);
6608 for (i
= 0; i
< len
; i
++)
6610 if (buf
+ i
== buf_end
)
6612 if (XINT (AREF (from
, i
)) != buf
[i
])
6623 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6626 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6627 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6629 EMACS_INT produced_chars
= 0;
6632 if (! coding
->chars_at_source
)
6634 /* Source characters are in coding->charbuf. */
6635 int *buf
= coding
->charbuf
;
6636 int *buf_end
= buf
+ coding
->charbuf_used
;
6638 if (EQ (coding
->src_object
, coding
->dst_object
))
6640 coding_set_source (coding
);
6641 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6644 while (buf
< buf_end
)
6650 EMACS_INT from_nchars
= 1, to_nchars
= 1;
6651 Lisp_Object trans
= Qnil
;
6653 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6656 trans
= get_translation (trans
, buf
, buf_end
);
6657 if (INTEGERP (trans
))
6659 else if (CONSP (trans
))
6661 from_nchars
= ASIZE (XCAR (trans
));
6662 trans
= XCDR (trans
);
6663 if (INTEGERP (trans
))
6667 to_nchars
= ASIZE (trans
);
6668 c
= XINT (AREF (trans
, 0));
6671 else if (EQ (trans
, Qt
) && ! last_block
)
6675 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6677 dst
= alloc_destination (coding
,
6679 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6681 if (EQ (coding
->src_object
, coding
->dst_object
))
6683 coding_set_source (coding
);
6684 dst_end
= (((unsigned char *) coding
->source
)
6685 + coding
->consumed
);
6688 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6691 for (i
= 0; i
< to_nchars
; i
++)
6694 c
= XINT (AREF (trans
, i
));
6695 if (coding
->dst_multibyte
6696 || ! CHAR_BYTE8_P (c
))
6697 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6699 *dst
++ = CHAR_TO_BYTE8 (c
);
6701 produced_chars
+= to_nchars
;
6705 /* This is an annotation datum. (-C) is the length. */
6708 carryover
= buf_end
- buf
;
6712 /* Source characters are at coding->source. */
6713 const unsigned char *src
= coding
->source
;
6714 const unsigned char *src_end
= src
+ coding
->consumed
;
6716 if (EQ (coding
->dst_object
, coding
->src_object
))
6717 dst_end
= (unsigned char *) src
;
6718 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6720 if (coding
->src_multibyte
)
6723 EMACS_INT consumed_chars
= 0;
6727 const unsigned char *src_base
= src
;
6733 if (EQ (coding
->src_object
, coding
->dst_object
))
6734 dst_end
= (unsigned char *) src
;
6737 EMACS_INT offset
= src
- coding
->source
;
6739 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6741 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6742 coding_set_source (coding
);
6743 src
= coding
->source
+ offset
;
6744 src_end
= coding
->source
+ coding
->src_bytes
;
6745 if (EQ (coding
->src_object
, coding
->dst_object
))
6746 dst_end
= (unsigned char *) src
;
6756 while (src
< src_end
)
6761 if (dst
>= dst_end
- 1)
6763 if (EQ (coding
->src_object
, coding
->dst_object
))
6764 dst_end
= (unsigned char *) src
;
6765 if (dst
>= dst_end
- 1)
6767 EMACS_INT offset
= src
- coding
->source
;
6768 EMACS_INT more_bytes
;
6770 if (EQ (coding
->src_object
, coding
->dst_object
))
6771 more_bytes
= ((src_end
- src
) / 2) + 2;
6773 more_bytes
= src_end
- src
+ 2;
6774 dst
= alloc_destination (coding
, more_bytes
, dst
);
6775 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6776 coding_set_source (coding
);
6777 src
= coding
->source
+ offset
;
6778 src_end
= coding
->source
+ coding
->src_bytes
;
6779 if (EQ (coding
->src_object
, coding
->dst_object
))
6780 dst_end
= (unsigned char *) src
;
6788 if (!EQ (coding
->src_object
, coding
->dst_object
))
6790 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6794 EMACS_INT offset
= src
- coding
->source
;
6796 dst
= alloc_destination (coding
, require
, dst
);
6797 coding_set_source (coding
);
6798 src
= coding
->source
+ offset
;
6799 src_end
= coding
->source
+ coding
->src_bytes
;
6802 produced_chars
= coding
->consumed_char
;
6803 while (src
< src_end
)
6808 produced
= dst
- (coding
->destination
+ coding
->produced
);
6809 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6810 insert_from_gap (produced_chars
, produced
);
6811 coding
->produced
+= produced
;
6812 coding
->produced_char
+= produced_chars
;
6816 /* Compose text in CODING->object according to the annotation data at
6817 CHARBUF. CHARBUF is an array:
6818 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6822 produce_composition (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6826 enum composition_method method
;
6827 Lisp_Object components
;
6829 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6830 to
= pos
+ charbuf
[2];
6831 method
= (enum composition_method
) (charbuf
[4]);
6833 if (method
== COMPOSITION_RELATIVE
)
6837 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6840 if (method
== COMPOSITION_WITH_RULE
)
6841 len
= charbuf
[2] * 3 - 2;
6842 charbuf
+= MAX_ANNOTATION_LENGTH
;
6843 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6844 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6846 if (charbuf
[i
] >= 0)
6847 args
[j
] = make_number (charbuf
[i
]);
6851 args
[j
] = make_number (charbuf
[i
] % 0x100);
6854 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6856 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6860 /* Put `charset' property on text in CODING->object according to
6861 the annotation data at CHARBUF. CHARBUF is an array:
6862 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6866 produce_charset (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6868 EMACS_INT from
= pos
- charbuf
[2];
6869 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6871 Fput_text_property (make_number (from
), make_number (pos
),
6872 Qcharset
, CHARSET_NAME (charset
),
6873 coding
->dst_object
);
6877 #define CHARBUF_SIZE 0x4000
6879 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6881 int size = CHARBUF_SIZE; \
6883 coding->charbuf = NULL; \
6884 while (size > 1024) \
6886 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6887 if (coding->charbuf) \
6891 if (! coding->charbuf) \
6893 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6894 return coding->result; \
6896 coding->charbuf_size = size; \
6901 produce_annotation (struct coding_system
*coding
, EMACS_INT pos
)
6903 int *charbuf
= coding
->charbuf
;
6904 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6906 if (NILP (coding
->dst_object
))
6909 while (charbuf
< charbuf_end
)
6915 int len
= -*charbuf
;
6920 case CODING_ANNOTATE_COMPOSITION_MASK
:
6921 produce_composition (coding
, charbuf
, pos
);
6923 case CODING_ANNOTATE_CHARSET_MASK
:
6924 produce_charset (coding
, charbuf
, pos
);
6932 /* Decode the data at CODING->src_object into CODING->dst_object.
6933 CODING->src_object is a buffer, a string, or nil.
6934 CODING->dst_object is a buffer.
6936 If CODING->src_object is a buffer, it must be the current buffer.
6937 In this case, if CODING->src_pos is positive, it is a position of
6938 the source text in the buffer, otherwise, the source text is in the
6939 gap area of the buffer, and CODING->src_pos specifies the offset of
6940 the text from GPT (which must be the same as PT). If this is the
6941 same buffer as CODING->dst_object, CODING->src_pos must be
6944 If CODING->src_object is a string, CODING->src_pos is an index to
6947 If CODING->src_object is nil, CODING->source must already point to
6948 the non-relocatable memory area. In this case, CODING->src_pos is
6949 an offset from CODING->source.
6951 The decoded data is inserted at the current point of the buffer
6956 decode_coding (struct coding_system
*coding
)
6959 Lisp_Object undo_list
;
6960 Lisp_Object translation_table
;
6961 struct ccl_spec cclspec
;
6965 if (BUFFERP (coding
->src_object
)
6966 && coding
->src_pos
> 0
6967 && coding
->src_pos
< GPT
6968 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6969 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6972 if (BUFFERP (coding
->dst_object
))
6974 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6975 set_buffer_internal (XBUFFER (coding
->dst_object
));
6977 move_gap_both (PT
, PT_BYTE
);
6978 undo_list
= BVAR (current_buffer
, undo_list
);
6979 BVAR (current_buffer
, undo_list
) = Qt
;
6982 coding
->consumed
= coding
->consumed_char
= 0;
6983 coding
->produced
= coding
->produced_char
= 0;
6984 coding
->chars_at_source
= 0;
6985 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6988 ALLOC_CONVERSION_WORK_AREA (coding
);
6990 attrs
= CODING_ID_ATTRS (coding
->id
);
6991 translation_table
= get_translation_table (attrs
, 0, NULL
);
6994 if (coding
->decoder
== decode_coding_ccl
)
6996 coding
->spec
.ccl
= &cclspec
;
6997 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7001 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7003 coding_set_source (coding
);
7004 coding
->annotated
= 0;
7005 coding
->charbuf_used
= carryover
;
7006 (*(coding
->decoder
)) (coding
);
7007 coding_set_destination (coding
);
7008 carryover
= produce_chars (coding
, translation_table
, 0);
7009 if (coding
->annotated
)
7010 produce_annotation (coding
, pos
);
7011 for (i
= 0; i
< carryover
; i
++)
7013 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7015 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7016 || (coding
->consumed
< coding
->src_bytes
7017 && (coding
->result
== CODING_RESULT_SUCCESS
7018 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7022 coding_set_destination (coding
);
7023 coding
->charbuf_used
= carryover
;
7024 produce_chars (coding
, translation_table
, 1);
7027 coding
->carryover_bytes
= 0;
7028 if (coding
->consumed
< coding
->src_bytes
)
7030 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7031 const unsigned char *src
;
7033 coding_set_source (coding
);
7034 coding_set_destination (coding
);
7035 src
= coding
->source
+ coding
->consumed
;
7037 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7039 /* Flush out unprocessed data as binary chars. We are sure
7040 that the number of data is less than the size of
7042 coding
->charbuf_used
= 0;
7043 coding
->chars_at_source
= 0;
7045 while (nbytes
-- > 0)
7050 c
= BYTE8_TO_CHAR (c
);
7051 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7053 produce_chars (coding
, Qnil
, 1);
7057 /* Record unprocessed bytes in coding->carryover. We are
7058 sure that the number of data is less than the size of
7059 coding->carryover. */
7060 unsigned char *p
= coding
->carryover
;
7062 if (nbytes
> sizeof coding
->carryover
)
7063 nbytes
= sizeof coding
->carryover
;
7064 coding
->carryover_bytes
= nbytes
;
7065 while (nbytes
-- > 0)
7068 coding
->consumed
= coding
->src_bytes
;
7071 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7072 && !inhibit_eol_conversion
)
7073 decode_eol (coding
);
7074 if (BUFFERP (coding
->dst_object
))
7076 BVAR (current_buffer
, undo_list
) = undo_list
;
7077 record_insert (coding
->dst_pos
, coding
->produced_char
);
7079 return coding
->result
;
7083 /* Extract an annotation datum from a composition starting at POS and
7084 ending before LIMIT of CODING->src_object (buffer or string), store
7085 the data in BUF, set *STOP to a starting position of the next
7086 composition (if any) or to LIMIT, and return the address of the
7087 next element of BUF.
7089 If such an annotation is not found, set *STOP to a starting
7090 position of a composition after POS (if any) or to LIMIT, and
7094 handle_composition_annotation (EMACS_INT pos
, EMACS_INT limit
,
7095 struct coding_system
*coding
, int *buf
,
7098 EMACS_INT start
, end
;
7101 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7104 else if (start
> pos
)
7110 /* We found a composition. Store the corresponding
7111 annotation data in BUF. */
7113 enum composition_method method
= COMPOSITION_METHOD (prop
);
7114 int nchars
= COMPOSITION_LENGTH (prop
);
7116 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7117 if (method
!= COMPOSITION_RELATIVE
)
7119 Lisp_Object components
;
7122 components
= COMPOSITION_COMPONENTS (prop
);
7123 if (VECTORP (components
))
7125 len
= XVECTOR (components
)->size
;
7126 for (i
= 0; i
< len
; i
++)
7127 *buf
++ = XINT (AREF (components
, i
));
7129 else if (STRINGP (components
))
7131 len
= SCHARS (components
);
7135 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7139 else if (INTEGERP (components
))
7142 *buf
++ = XINT (components
);
7144 else if (CONSP (components
))
7146 for (len
= 0; CONSP (components
);
7147 len
++, components
= XCDR (components
))
7148 *buf
++ = XINT (XCAR (components
));
7156 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7167 /* Extract an annotation datum from a text property `charset' at POS of
7168 CODING->src_object (buffer of string), store the data in BUF, set
7169 *STOP to the position where the value of `charset' property changes
7170 (limiting by LIMIT), and return the address of the next element of
7173 If the property value is nil, set *STOP to the position where the
7174 property value is non-nil (limiting by LIMIT), and return BUF. */
7177 handle_charset_annotation (EMACS_INT pos
, EMACS_INT limit
,
7178 struct coding_system
*coding
, int *buf
,
7181 Lisp_Object val
, next
;
7184 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7185 if (! NILP (val
) && CHARSETP (val
))
7186 id
= XINT (CHARSET_SYMBOL_ID (val
));
7189 ADD_CHARSET_DATA (buf
, 0, id
);
7190 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7192 make_number (limit
));
7193 *stop
= XINT (next
);
7199 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7202 int *buf
= coding
->charbuf
;
7203 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7204 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7205 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7206 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7207 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7208 int multibytep
= coding
->src_multibyte
;
7209 Lisp_Object eol_type
;
7211 EMACS_INT stop
, stop_composition
, stop_charset
;
7212 int *lookup_buf
= NULL
;
7214 if (! NILP (translation_table
))
7215 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7217 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7218 if (VECTORP (eol_type
))
7221 /* Note: composition handling is not yet implemented. */
7222 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7224 if (NILP (coding
->src_object
))
7225 stop
= stop_composition
= stop_charset
= end_pos
;
7228 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7229 stop
= stop_composition
= pos
;
7231 stop
= stop_composition
= end_pos
;
7232 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7233 stop
= stop_charset
= pos
;
7235 stop_charset
= end_pos
;
7238 /* Compensate for CRLF and conversion. */
7239 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7240 while (buf
< buf_end
)
7248 if (pos
== stop_composition
)
7249 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7250 buf
, &stop_composition
);
7251 if (pos
== stop_charset
)
7252 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7253 buf
, &stop_charset
);
7254 stop
= (stop_composition
< stop_charset
7255 ? stop_composition
: stop_charset
);
7262 if (coding
->encoder
== encode_coding_raw_text
7263 || coding
->encoder
== encode_coding_ccl
)
7265 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7266 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7268 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7271 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7272 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7274 if (! EQ (eol_type
, Qunix
))
7278 if (EQ (eol_type
, Qdos
))
7286 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7291 int from_nchars
= 1, to_nchars
= 1;
7292 int *lookup_buf_end
;
7293 const unsigned char *p
= src
;
7297 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7298 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7299 lookup_buf_end
= lookup_buf
+ i
;
7300 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7301 if (INTEGERP (trans
))
7303 else if (CONSP (trans
))
7305 from_nchars
= ASIZE (XCAR (trans
));
7306 trans
= XCDR (trans
);
7307 if (INTEGERP (trans
))
7311 to_nchars
= ASIZE (trans
);
7312 if (buf
+ to_nchars
> buf_end
)
7314 c
= XINT (AREF (trans
, 0));
7320 for (i
= 1; i
< to_nchars
; i
++)
7321 *buf
++ = XINT (AREF (trans
, i
));
7322 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7323 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7327 coding
->consumed
= src
- coding
->source
;
7328 coding
->consumed_char
= pos
- coding
->src_pos
;
7329 coding
->charbuf_used
= buf
- coding
->charbuf
;
7330 coding
->chars_at_source
= 0;
7334 /* Encode the text at CODING->src_object into CODING->dst_object.
7335 CODING->src_object is a buffer or a string.
7336 CODING->dst_object is a buffer or nil.
7338 If CODING->src_object is a buffer, it must be the current buffer.
7339 In this case, if CODING->src_pos is positive, it is a position of
7340 the source text in the buffer, otherwise. the source text is in the
7341 gap area of the buffer, and coding->src_pos specifies the offset of
7342 the text from GPT (which must be the same as PT). If this is the
7343 same buffer as CODING->dst_object, CODING->src_pos must be
7344 negative and CODING should not have `pre-write-conversion'.
7346 If CODING->src_object is a string, CODING should not have
7347 `pre-write-conversion'.
7349 If CODING->dst_object is a buffer, the encoded data is inserted at
7350 the current point of that buffer.
7352 If CODING->dst_object is nil, the encoded data is placed at the
7353 memory area specified by CODING->destination. */
7356 encode_coding (struct coding_system
*coding
)
7359 Lisp_Object translation_table
;
7361 struct ccl_spec cclspec
;
7363 attrs
= CODING_ID_ATTRS (coding
->id
);
7364 if (coding
->encoder
== encode_coding_raw_text
)
7365 translation_table
= Qnil
, max_lookup
= 0;
7367 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7369 if (BUFFERP (coding
->dst_object
))
7371 set_buffer_internal (XBUFFER (coding
->dst_object
));
7372 coding
->dst_multibyte
7373 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7376 coding
->consumed
= coding
->consumed_char
= 0;
7377 coding
->produced
= coding
->produced_char
= 0;
7378 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7381 ALLOC_CONVERSION_WORK_AREA (coding
);
7383 if (coding
->encoder
== encode_coding_ccl
)
7385 coding
->spec
.ccl
= &cclspec
;
7386 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7389 coding_set_source (coding
);
7390 consume_chars (coding
, translation_table
, max_lookup
);
7391 coding_set_destination (coding
);
7392 (*(coding
->encoder
)) (coding
);
7393 } while (coding
->consumed_char
< coding
->src_chars
);
7395 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7396 insert_from_gap (coding
->produced_char
, coding
->produced
);
7398 return (coding
->result
);
7402 /* Name (or base name) of work buffer for code conversion. */
7403 static Lisp_Object Vcode_conversion_workbuf_name
;
7405 /* A working buffer used by the top level conversion. Once it is
7406 created, it is never destroyed. It has the name
7407 Vcode_conversion_workbuf_name. The other working buffers are
7408 destroyed after the use is finished, and their names are modified
7409 versions of Vcode_conversion_workbuf_name. */
7410 static Lisp_Object Vcode_conversion_reused_workbuf
;
7412 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7413 static int reused_workbuf_in_use
;
7416 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7417 multibyteness of returning buffer. */
7420 make_conversion_work_buffer (int multibyte
)
7422 Lisp_Object name
, workbuf
;
7423 struct buffer
*current
;
7425 if (reused_workbuf_in_use
++)
7427 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7428 workbuf
= Fget_buffer_create (name
);
7432 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7433 Vcode_conversion_reused_workbuf
7434 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7435 workbuf
= Vcode_conversion_reused_workbuf
;
7437 current
= current_buffer
;
7438 set_buffer_internal (XBUFFER (workbuf
));
7439 /* We can't allow modification hooks to run in the work buffer. For
7440 instance, directory_files_internal assumes that file decoding
7441 doesn't compile new regexps. */
7442 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7444 BVAR (current_buffer
, undo_list
) = Qt
;
7445 BVAR (current_buffer
, enable_multibyte_characters
) = multibyte
? Qt
: Qnil
;
7446 set_buffer_internal (current
);
7452 code_conversion_restore (Lisp_Object arg
)
7454 Lisp_Object current
, workbuf
;
7455 struct gcpro gcpro1
;
7458 current
= XCAR (arg
);
7459 workbuf
= XCDR (arg
);
7460 if (! NILP (workbuf
))
7462 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7463 reused_workbuf_in_use
= 0;
7464 else if (! NILP (Fbuffer_live_p (workbuf
)))
7465 Fkill_buffer (workbuf
);
7467 set_buffer_internal (XBUFFER (current
));
7473 code_conversion_save (int with_work_buf
, int multibyte
)
7475 Lisp_Object workbuf
= Qnil
;
7478 workbuf
= make_conversion_work_buffer (multibyte
);
7479 record_unwind_protect (code_conversion_restore
,
7480 Fcons (Fcurrent_buffer (), workbuf
));
7485 decode_coding_gap (struct coding_system
*coding
,
7486 EMACS_INT chars
, EMACS_INT bytes
)
7488 int count
= SPECPDL_INDEX ();
7491 code_conversion_save (0, 0);
7493 coding
->src_object
= Fcurrent_buffer ();
7494 coding
->src_chars
= chars
;
7495 coding
->src_bytes
= bytes
;
7496 coding
->src_pos
= -chars
;
7497 coding
->src_pos_byte
= -bytes
;
7498 coding
->src_multibyte
= chars
< bytes
;
7499 coding
->dst_object
= coding
->src_object
;
7500 coding
->dst_pos
= PT
;
7501 coding
->dst_pos_byte
= PT_BYTE
;
7502 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7504 if (CODING_REQUIRE_DETECTION (coding
))
7505 detect_coding (coding
);
7507 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7508 current_buffer
->text
->inhibit_shrinking
= 1;
7509 decode_coding (coding
);
7510 current_buffer
->text
->inhibit_shrinking
= 0;
7512 attrs
= CODING_ID_ATTRS (coding
->id
);
7513 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7515 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7518 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7519 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7520 make_number (coding
->produced_char
));
7522 coding
->produced_char
+= Z
- prev_Z
;
7523 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7526 unbind_to (count
, Qnil
);
7527 return coding
->result
;
7531 encode_coding_gap (struct coding_system
*coding
,
7532 EMACS_INT chars
, EMACS_INT bytes
)
7534 int count
= SPECPDL_INDEX ();
7536 code_conversion_save (0, 0);
7538 coding
->src_object
= Fcurrent_buffer ();
7539 coding
->src_chars
= chars
;
7540 coding
->src_bytes
= bytes
;
7541 coding
->src_pos
= -chars
;
7542 coding
->src_pos_byte
= -bytes
;
7543 coding
->src_multibyte
= chars
< bytes
;
7544 coding
->dst_object
= coding
->src_object
;
7545 coding
->dst_pos
= PT
;
7546 coding
->dst_pos_byte
= PT_BYTE
;
7548 encode_coding (coding
);
7550 unbind_to (count
, Qnil
);
7551 return coding
->result
;
7555 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7556 SRC_OBJECT into DST_OBJECT by coding context CODING.
7558 SRC_OBJECT is a buffer, a string, or Qnil.
7560 If it is a buffer, the text is at point of the buffer. FROM and TO
7561 are positions in the buffer.
7563 If it is a string, the text is at the beginning of the string.
7564 FROM and TO are indices to the string.
7566 If it is nil, the text is at coding->source. FROM and TO are
7567 indices to coding->source.
7569 DST_OBJECT is a buffer, Qt, or Qnil.
7571 If it is a buffer, the decoded text is inserted at point of the
7572 buffer. If the buffer is the same as SRC_OBJECT, the source text
7575 If it is Qt, a string is made from the decoded text, and
7576 set in CODING->dst_object.
7578 If it is Qnil, the decoded text is stored at CODING->destination.
7579 The caller must allocate CODING->dst_bytes bytes at
7580 CODING->destination by xmalloc. If the decoded text is longer than
7581 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7585 decode_coding_object (struct coding_system
*coding
,
7586 Lisp_Object src_object
,
7587 EMACS_INT from
, EMACS_INT from_byte
,
7588 EMACS_INT to
, EMACS_INT to_byte
,
7589 Lisp_Object dst_object
)
7591 int count
= SPECPDL_INDEX ();
7592 unsigned char *destination
IF_LINT (= NULL
);
7593 EMACS_INT dst_bytes
IF_LINT (= 0);
7594 EMACS_INT chars
= to
- from
;
7595 EMACS_INT bytes
= to_byte
- from_byte
;
7597 int saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7598 int need_marker_adjustment
= 0;
7599 Lisp_Object old_deactivate_mark
;
7601 old_deactivate_mark
= Vdeactivate_mark
;
7603 if (NILP (dst_object
))
7605 destination
= coding
->destination
;
7606 dst_bytes
= coding
->dst_bytes
;
7609 coding
->src_object
= src_object
;
7610 coding
->src_chars
= chars
;
7611 coding
->src_bytes
= bytes
;
7612 coding
->src_multibyte
= chars
< bytes
;
7614 if (STRINGP (src_object
))
7616 coding
->src_pos
= from
;
7617 coding
->src_pos_byte
= from_byte
;
7619 else if (BUFFERP (src_object
))
7621 set_buffer_internal (XBUFFER (src_object
));
7623 move_gap_both (from
, from_byte
);
7624 if (EQ (src_object
, dst_object
))
7626 struct Lisp_Marker
*tail
;
7628 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7630 tail
->need_adjustment
7631 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7632 need_marker_adjustment
|= tail
->need_adjustment
;
7634 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7635 TEMP_SET_PT_BOTH (from
, from_byte
);
7636 current_buffer
->text
->inhibit_shrinking
= 1;
7637 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7638 coding
->src_pos
= -chars
;
7639 coding
->src_pos_byte
= -bytes
;
7643 coding
->src_pos
= from
;
7644 coding
->src_pos_byte
= from_byte
;
7648 if (CODING_REQUIRE_DETECTION (coding
))
7649 detect_coding (coding
);
7650 attrs
= CODING_ID_ATTRS (coding
->id
);
7652 if (EQ (dst_object
, Qt
)
7653 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7654 && NILP (dst_object
)))
7656 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7657 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7658 coding
->dst_pos
= BEG
;
7659 coding
->dst_pos_byte
= BEG_BYTE
;
7661 else if (BUFFERP (dst_object
))
7663 code_conversion_save (0, 0);
7664 coding
->dst_object
= dst_object
;
7665 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7666 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7667 coding
->dst_multibyte
7668 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7672 code_conversion_save (0, 0);
7673 coding
->dst_object
= Qnil
;
7674 /* Most callers presume this will return a multibyte result, and they
7675 won't use `binary' or `raw-text' anyway, so let's not worry about
7676 CODING_FOR_UNIBYTE. */
7677 coding
->dst_multibyte
= 1;
7680 decode_coding (coding
);
7682 if (BUFFERP (coding
->dst_object
))
7683 set_buffer_internal (XBUFFER (coding
->dst_object
));
7685 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7687 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7688 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7691 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7692 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7693 old_deactivate_mark
);
7694 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7695 make_number (coding
->produced_char
));
7698 coding
->produced_char
+= Z
- prev_Z
;
7699 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7702 if (EQ (dst_object
, Qt
))
7704 coding
->dst_object
= Fbuffer_string ();
7706 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7708 set_buffer_internal (XBUFFER (coding
->dst_object
));
7709 if (dst_bytes
< coding
->produced
)
7711 destination
= xrealloc (destination
, coding
->produced
);
7714 record_conversion_result (coding
,
7715 CODING_RESULT_INSUFFICIENT_MEM
);
7716 unbind_to (count
, Qnil
);
7719 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7720 move_gap_both (BEGV
, BEGV_BYTE
);
7721 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7722 coding
->destination
= destination
;
7728 /* This is the case of:
7729 (BUFFERP (src_object) && EQ (src_object, dst_object))
7730 As we have moved PT while replacing the original buffer
7731 contents, we must recover it now. */
7732 set_buffer_internal (XBUFFER (src_object
));
7733 current_buffer
->text
->inhibit_shrinking
= 0;
7734 if (saved_pt
< from
)
7735 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7736 else if (saved_pt
< from
+ chars
)
7737 TEMP_SET_PT_BOTH (from
, from_byte
);
7738 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7739 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7740 saved_pt_byte
+ (coding
->produced
- bytes
));
7742 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7743 saved_pt_byte
+ (coding
->produced
- bytes
));
7745 if (need_marker_adjustment
)
7747 struct Lisp_Marker
*tail
;
7749 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7750 if (tail
->need_adjustment
)
7752 tail
->need_adjustment
= 0;
7753 if (tail
->insertion_type
)
7755 tail
->bytepos
= from_byte
;
7756 tail
->charpos
= from
;
7760 tail
->bytepos
= from_byte
+ coding
->produced
;
7762 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7763 ? tail
->bytepos
: from
+ coding
->produced_char
);
7769 Vdeactivate_mark
= old_deactivate_mark
;
7770 unbind_to (count
, coding
->dst_object
);
7775 encode_coding_object (struct coding_system
*coding
,
7776 Lisp_Object src_object
,
7777 EMACS_INT from
, EMACS_INT from_byte
,
7778 EMACS_INT to
, EMACS_INT to_byte
,
7779 Lisp_Object dst_object
)
7781 int count
= SPECPDL_INDEX ();
7782 EMACS_INT chars
= to
- from
;
7783 EMACS_INT bytes
= to_byte
- from_byte
;
7785 int saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7786 int need_marker_adjustment
= 0;
7787 int kill_src_buffer
= 0;
7788 Lisp_Object old_deactivate_mark
;
7790 old_deactivate_mark
= Vdeactivate_mark
;
7792 coding
->src_object
= src_object
;
7793 coding
->src_chars
= chars
;
7794 coding
->src_bytes
= bytes
;
7795 coding
->src_multibyte
= chars
< bytes
;
7797 attrs
= CODING_ID_ATTRS (coding
->id
);
7799 if (EQ (src_object
, dst_object
))
7801 struct Lisp_Marker
*tail
;
7803 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7805 tail
->need_adjustment
7806 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7807 need_marker_adjustment
|= tail
->need_adjustment
;
7811 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7813 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7814 set_buffer_internal (XBUFFER (coding
->src_object
));
7815 if (STRINGP (src_object
))
7816 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7817 else if (BUFFERP (src_object
))
7818 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7820 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7822 if (EQ (src_object
, dst_object
))
7824 set_buffer_internal (XBUFFER (src_object
));
7825 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7826 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7827 set_buffer_internal (XBUFFER (coding
->src_object
));
7831 Lisp_Object args
[3];
7832 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7834 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7835 old_deactivate_mark
);
7836 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7837 args
[1] = make_number (BEG
);
7838 args
[2] = make_number (Z
);
7839 safe_call (3, args
);
7842 if (XBUFFER (coding
->src_object
) != current_buffer
)
7843 kill_src_buffer
= 1;
7844 coding
->src_object
= Fcurrent_buffer ();
7846 move_gap_both (BEG
, BEG_BYTE
);
7847 coding
->src_chars
= Z
- BEG
;
7848 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7849 coding
->src_pos
= BEG
;
7850 coding
->src_pos_byte
= BEG_BYTE
;
7851 coding
->src_multibyte
= Z
< Z_BYTE
;
7853 else if (STRINGP (src_object
))
7855 code_conversion_save (0, 0);
7856 coding
->src_pos
= from
;
7857 coding
->src_pos_byte
= from_byte
;
7859 else if (BUFFERP (src_object
))
7861 code_conversion_save (0, 0);
7862 set_buffer_internal (XBUFFER (src_object
));
7863 if (EQ (src_object
, dst_object
))
7865 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7866 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7867 coding
->src_pos
= 0;
7868 coding
->src_pos_byte
= 0;
7872 if (from
< GPT
&& to
>= GPT
)
7873 move_gap_both (from
, from_byte
);
7874 coding
->src_pos
= from
;
7875 coding
->src_pos_byte
= from_byte
;
7879 code_conversion_save (0, 0);
7881 if (BUFFERP (dst_object
))
7883 coding
->dst_object
= dst_object
;
7884 if (EQ (src_object
, dst_object
))
7886 coding
->dst_pos
= from
;
7887 coding
->dst_pos_byte
= from_byte
;
7891 struct buffer
*current
= current_buffer
;
7893 set_buffer_temp (XBUFFER (dst_object
));
7894 coding
->dst_pos
= PT
;
7895 coding
->dst_pos_byte
= PT_BYTE
;
7896 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7897 set_buffer_temp (current
);
7899 coding
->dst_multibyte
7900 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7902 else if (EQ (dst_object
, Qt
))
7904 coding
->dst_object
= Qnil
;
7905 coding
->dst_bytes
= coding
->src_chars
;
7906 if (coding
->dst_bytes
== 0)
7907 coding
->dst_bytes
= 1;
7908 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7909 coding
->dst_multibyte
= 0;
7913 coding
->dst_object
= Qnil
;
7914 coding
->dst_multibyte
= 0;
7917 encode_coding (coding
);
7919 if (EQ (dst_object
, Qt
))
7921 if (BUFFERP (coding
->dst_object
))
7922 coding
->dst_object
= Fbuffer_string ();
7926 = make_unibyte_string ((char *) coding
->destination
,
7928 xfree (coding
->destination
);
7934 /* This is the case of:
7935 (BUFFERP (src_object) && EQ (src_object, dst_object))
7936 As we have moved PT while replacing the original buffer
7937 contents, we must recover it now. */
7938 set_buffer_internal (XBUFFER (src_object
));
7939 if (saved_pt
< from
)
7940 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7941 else if (saved_pt
< from
+ chars
)
7942 TEMP_SET_PT_BOTH (from
, from_byte
);
7943 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7944 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7945 saved_pt_byte
+ (coding
->produced
- bytes
));
7947 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7948 saved_pt_byte
+ (coding
->produced
- bytes
));
7950 if (need_marker_adjustment
)
7952 struct Lisp_Marker
*tail
;
7954 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7955 if (tail
->need_adjustment
)
7957 tail
->need_adjustment
= 0;
7958 if (tail
->insertion_type
)
7960 tail
->bytepos
= from_byte
;
7961 tail
->charpos
= from
;
7965 tail
->bytepos
= from_byte
+ coding
->produced
;
7967 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7968 ? tail
->bytepos
: from
+ coding
->produced_char
);
7974 if (kill_src_buffer
)
7975 Fkill_buffer (coding
->src_object
);
7977 Vdeactivate_mark
= old_deactivate_mark
;
7978 unbind_to (count
, Qnil
);
7983 preferred_coding_system (void)
7985 int id
= coding_categories
[coding_priorities
[0]].id
;
7987 return CODING_ID_NAME (id
);
7992 /*** 8. Emacs Lisp library functions ***/
7994 DEFUE ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7995 doc
: /* Return t if OBJECT is nil or a coding-system.
7996 See the documentation of `define-coding-system' for information
7997 about coding-system objects. */)
7998 (Lisp_Object object
)
8001 || CODING_SYSTEM_ID (object
) >= 0)
8003 if (! SYMBOLP (object
)
8004 || NILP (Fget (object
, Qcoding_system_define_form
)))
8009 DEFUE ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8010 Sread_non_nil_coding_system
, 1, 1, 0,
8011 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8012 (Lisp_Object prompt
)
8017 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8018 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8020 while (SCHARS (val
) == 0);
8021 return (Fintern (val
, Qnil
));
8024 DEFUE ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8025 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8026 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8027 Ignores case when completing coding systems (all Emacs coding systems
8028 are lower-case). */)
8029 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8032 int count
= SPECPDL_INDEX ();
8034 if (SYMBOLP (default_coding_system
))
8035 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8036 specbind (Qcompletion_ignore_case
, Qt
);
8037 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8038 Qt
, Qnil
, Qcoding_system_history
,
8039 default_coding_system
, Qnil
);
8040 unbind_to (count
, Qnil
);
8041 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8044 DEFUE ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8046 doc
: /* Check validity of CODING-SYSTEM.
8047 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8048 It is valid if it is nil or a symbol defined as a coding system by the
8049 function `define-coding-system'. */)
8050 (Lisp_Object coding_system
)
8052 Lisp_Object define_form
;
8054 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8055 if (! NILP (define_form
))
8057 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8058 safe_eval (define_form
);
8060 if (!NILP (Fcoding_system_p (coding_system
)))
8061 return coding_system
;
8062 xsignal1 (Qcoding_system_error
, coding_system
);
8066 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8067 HIGHEST is nonzero, return the coding system of the highest
8068 priority among the detected coding systems. Otherwise return a
8069 list of detected coding systems sorted by their priorities. If
8070 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8071 multibyte form but contains only ASCII and eight-bit chars.
8072 Otherwise, the bytes are raw bytes.
8074 CODING-SYSTEM controls the detection as below:
8076 If it is nil, detect both text-format and eol-format. If the
8077 text-format part of CODING-SYSTEM is already specified
8078 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8079 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8080 detect only text-format. */
8083 detect_coding_system (const unsigned char *src
,
8084 EMACS_INT src_chars
, EMACS_INT src_bytes
,
8085 int highest
, int multibytep
,
8086 Lisp_Object coding_system
)
8088 const unsigned char *src_end
= src
+ src_bytes
;
8089 Lisp_Object attrs
, eol_type
;
8090 Lisp_Object val
= Qnil
;
8091 struct coding_system coding
;
8093 struct coding_detection_info detect_info
;
8094 enum coding_category base_category
;
8095 int null_byte_found
= 0, eight_bit_found
= 0;
8097 if (NILP (coding_system
))
8098 coding_system
= Qundecided
;
8099 setup_coding_system (coding_system
, &coding
);
8100 attrs
= CODING_ID_ATTRS (coding
.id
);
8101 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8102 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8104 coding
.source
= src
;
8105 coding
.src_chars
= src_chars
;
8106 coding
.src_bytes
= src_bytes
;
8107 coding
.src_multibyte
= multibytep
;
8108 coding
.consumed
= 0;
8109 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8110 coding
.head_ascii
= 0;
8112 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8114 /* At first, detect text-format if necessary. */
8115 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8116 if (base_category
== coding_category_undecided
)
8118 enum coding_category category
IF_LINT (= 0);
8119 struct coding_system
*this IF_LINT (= NULL
);
8122 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8123 for (; src
< src_end
; src
++)
8128 eight_bit_found
= 1;
8129 if (null_byte_found
)
8134 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8135 && ! inhibit_iso_escape_detection
8136 && ! detect_info
.checked
)
8138 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8140 /* We have scanned the whole data. */
8141 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8143 /* We didn't find an 8-bit code. We may
8144 have found a null-byte, but it's very
8145 rare that a binary file confirm to
8148 coding
.head_ascii
= src
- coding
.source
;
8150 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8154 else if (! c
&& !inhibit_null_byte_detection
)
8156 null_byte_found
= 1;
8157 if (eight_bit_found
)
8160 if (! eight_bit_found
)
8161 coding
.head_ascii
++;
8163 else if (! eight_bit_found
)
8164 coding
.head_ascii
++;
8167 if (null_byte_found
|| eight_bit_found
8168 || coding
.head_ascii
< coding
.src_bytes
8169 || detect_info
.found
)
8171 if (coding
.head_ascii
== coding
.src_bytes
)
8172 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8173 for (i
= 0; i
< coding_category_raw_text
; i
++)
8175 category
= coding_priorities
[i
];
8176 this = coding_categories
+ category
;
8177 if (detect_info
.found
& (1 << category
))
8182 if (null_byte_found
)
8184 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8185 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8187 for (i
= 0; i
< coding_category_raw_text
; i
++)
8189 category
= coding_priorities
[i
];
8190 this = coding_categories
+ category
;
8194 /* No coding system of this category is defined. */
8195 detect_info
.rejected
|= (1 << category
);
8197 else if (category
>= coding_category_raw_text
)
8199 else if (detect_info
.checked
& (1 << category
))
8202 && (detect_info
.found
& (1 << category
)))
8205 else if ((*(this->detector
)) (&coding
, &detect_info
)
8207 && (detect_info
.found
& (1 << category
)))
8209 if (category
== coding_category_utf_16_auto
)
8211 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8212 category
= coding_category_utf_16_le
;
8214 category
= coding_category_utf_16_be
;
8222 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8225 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8226 id
= CODING_SYSTEM_ID (Qno_conversion
);
8227 val
= Fcons (make_number (id
), Qnil
);
8229 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8231 detect_info
.found
= CATEGORY_MASK_ANY
;
8232 id
= coding_categories
[coding_category_undecided
].id
;
8233 val
= Fcons (make_number (id
), Qnil
);
8237 if (detect_info
.found
)
8239 detect_info
.found
= 1 << category
;
8240 val
= Fcons (make_number (this->id
), Qnil
);
8243 for (i
= 0; i
< coding_category_raw_text
; i
++)
8244 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8246 detect_info
.found
= 1 << coding_priorities
[i
];
8247 id
= coding_categories
[coding_priorities
[i
]].id
;
8248 val
= Fcons (make_number (id
), Qnil
);
8254 int mask
= detect_info
.rejected
| detect_info
.found
;
8257 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8259 category
= coding_priorities
[i
];
8260 if (! (mask
& (1 << category
)))
8262 found
|= 1 << category
;
8263 id
= coding_categories
[category
].id
;
8265 val
= Fcons (make_number (id
), val
);
8268 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8270 category
= coding_priorities
[i
];
8271 if (detect_info
.found
& (1 << category
))
8273 id
= coding_categories
[category
].id
;
8274 val
= Fcons (make_number (id
), val
);
8277 detect_info
.found
|= found
;
8280 else if (base_category
== coding_category_utf_8_auto
)
8282 if (detect_coding_utf_8 (&coding
, &detect_info
))
8284 struct coding_system
*this;
8286 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8287 this = coding_categories
+ coding_category_utf_8_sig
;
8289 this = coding_categories
+ coding_category_utf_8_nosig
;
8290 val
= Fcons (make_number (this->id
), Qnil
);
8293 else if (base_category
== coding_category_utf_16_auto
)
8295 if (detect_coding_utf_16 (&coding
, &detect_info
))
8297 struct coding_system
*this;
8299 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8300 this = coding_categories
+ coding_category_utf_16_le
;
8301 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8302 this = coding_categories
+ coding_category_utf_16_be
;
8303 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8304 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8306 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8307 val
= Fcons (make_number (this->id
), Qnil
);
8312 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8313 val
= Fcons (make_number (coding
.id
), Qnil
);
8316 /* Then, detect eol-format if necessary. */
8318 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8321 if (VECTORP (eol_type
))
8323 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8325 if (null_byte_found
)
8326 normal_eol
= EOL_SEEN_LF
;
8328 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8329 coding_category_raw_text
);
8331 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8332 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8333 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8334 coding_category_utf_16_be
);
8335 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8336 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8337 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8338 coding_category_utf_16_le
);
8342 if (EQ (eol_type
, Qunix
))
8343 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8344 else if (EQ (eol_type
, Qdos
))
8345 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8347 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8350 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8352 enum coding_category category
;
8355 id
= XINT (XCAR (tail
));
8356 attrs
= CODING_ID_ATTRS (id
);
8357 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8358 eol_type
= CODING_ID_EOL_TYPE (id
);
8359 if (VECTORP (eol_type
))
8361 if (category
== coding_category_utf_16_be
8362 || category
== coding_category_utf_16_be_nosig
)
8363 this_eol
= utf_16_be_eol
;
8364 else if (category
== coding_category_utf_16_le
8365 || category
== coding_category_utf_16_le_nosig
)
8366 this_eol
= utf_16_le_eol
;
8368 this_eol
= normal_eol
;
8370 if (this_eol
== EOL_SEEN_LF
)
8371 XSETCAR (tail
, AREF (eol_type
, 0));
8372 else if (this_eol
== EOL_SEEN_CRLF
)
8373 XSETCAR (tail
, AREF (eol_type
, 1));
8374 else if (this_eol
== EOL_SEEN_CR
)
8375 XSETCAR (tail
, AREF (eol_type
, 2));
8377 XSETCAR (tail
, CODING_ID_NAME (id
));
8380 XSETCAR (tail
, CODING_ID_NAME (id
));
8384 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8388 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8390 doc
: /* Detect coding system of the text in the region between START and END.
8391 Return a list of possible coding systems ordered by priority.
8392 The coding systems to try and their priorities follows what
8393 the function `coding-system-priority-list' (which see) returns.
8395 If only ASCII characters are found (except for such ISO-2022 control
8396 characters as ESC), it returns a list of single element `undecided'
8397 or its subsidiary coding system according to a detected end-of-line
8400 If optional argument HIGHEST is non-nil, return the coding system of
8401 highest priority. */)
8402 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8405 int from_byte
, to_byte
;
8407 CHECK_NUMBER_COERCE_MARKER (start
);
8408 CHECK_NUMBER_COERCE_MARKER (end
);
8410 validate_region (&start
, &end
);
8411 from
= XINT (start
), to
= XINT (end
);
8412 from_byte
= CHAR_TO_BYTE (from
);
8413 to_byte
= CHAR_TO_BYTE (to
);
8415 if (from
< GPT
&& to
>= GPT
)
8416 move_gap_both (to
, to_byte
);
8418 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8419 to
- from
, to_byte
- from_byte
,
8421 !NILP (BVAR (current_buffer
8422 , enable_multibyte_characters
)),
8426 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8428 doc
: /* Detect coding system of the text in STRING.
8429 Return a list of possible coding systems ordered by priority.
8430 The coding systems to try and their priorities follows what
8431 the function `coding-system-priority-list' (which see) returns.
8433 If only ASCII characters are found (except for such ISO-2022 control
8434 characters as ESC), it returns a list of single element `undecided'
8435 or its subsidiary coding system according to a detected end-of-line
8438 If optional argument HIGHEST is non-nil, return the coding system of
8439 highest priority. */)
8440 (Lisp_Object string
, Lisp_Object highest
)
8442 CHECK_STRING (string
);
8444 return detect_coding_system (SDATA (string
),
8445 SCHARS (string
), SBYTES (string
),
8446 !NILP (highest
), STRING_MULTIBYTE (string
),
8452 char_encodable_p (int c
, Lisp_Object attrs
)
8455 struct charset
*charset
;
8456 Lisp_Object translation_table
;
8458 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8459 if (! NILP (translation_table
))
8460 c
= translate_char (translation_table
, c
);
8461 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8462 CONSP (tail
); tail
= XCDR (tail
))
8464 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8465 if (CHAR_CHARSET_P (c
, charset
))
8468 return (! NILP (tail
));
8472 /* Return a list of coding systems that safely encode the text between
8473 START and END. If EXCLUDE is non-nil, it is a list of coding
8474 systems not to check. The returned list doesn't contain any such
8475 coding systems. In any case, if the text contains only ASCII or is
8476 unibyte, return t. */
8478 DEFUN ("find-coding-systems-region-internal",
8479 Ffind_coding_systems_region_internal
,
8480 Sfind_coding_systems_region_internal
, 2, 3, 0,
8481 doc
: /* Internal use only. */)
8482 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8484 Lisp_Object coding_attrs_list
, safe_codings
;
8485 EMACS_INT start_byte
, end_byte
;
8486 const unsigned char *p
, *pbeg
, *pend
;
8488 Lisp_Object tail
, elt
, work_table
;
8490 if (STRINGP (start
))
8492 if (!STRING_MULTIBYTE (start
)
8493 || SCHARS (start
) == SBYTES (start
))
8496 end_byte
= SBYTES (start
);
8500 CHECK_NUMBER_COERCE_MARKER (start
);
8501 CHECK_NUMBER_COERCE_MARKER (end
);
8502 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8503 args_out_of_range (start
, end
);
8504 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8506 start_byte
= CHAR_TO_BYTE (XINT (start
));
8507 end_byte
= CHAR_TO_BYTE (XINT (end
));
8508 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8511 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8513 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8514 move_gap_both (XINT (start
), start_byte
);
8516 move_gap_both (XINT (end
), end_byte
);
8520 coding_attrs_list
= Qnil
;
8521 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8523 || NILP (Fmemq (XCAR (tail
), exclude
)))
8527 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8528 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8529 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8531 ASET (attrs
, coding_attr_trans_tbl
,
8532 get_translation_table (attrs
, 1, NULL
));
8533 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8537 if (STRINGP (start
))
8538 p
= pbeg
= SDATA (start
);
8540 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8541 pend
= p
+ (end_byte
- start_byte
);
8543 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8544 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8546 work_table
= Fmake_char_table (Qnil
, Qnil
);
8549 if (ASCII_BYTE_P (*p
))
8553 c
= STRING_CHAR_ADVANCE (p
);
8554 if (!NILP (char_table_ref (work_table
, c
)))
8555 /* This character was already checked. Ignore it. */
8558 charset_map_loaded
= 0;
8559 for (tail
= coding_attrs_list
; CONSP (tail
);)
8564 else if (char_encodable_p (c
, elt
))
8566 else if (CONSP (XCDR (tail
)))
8568 XSETCAR (tail
, XCAR (XCDR (tail
)));
8569 XSETCDR (tail
, XCDR (XCDR (tail
)));
8573 XSETCAR (tail
, Qnil
);
8577 if (charset_map_loaded
)
8579 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8581 if (STRINGP (start
))
8582 pbeg
= SDATA (start
);
8584 pbeg
= BYTE_POS_ADDR (start_byte
);
8585 p
= pbeg
+ p_offset
;
8586 pend
= pbeg
+ pend_offset
;
8588 char_table_set (work_table
, c
, Qt
);
8592 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8593 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8594 if (! NILP (XCAR (tail
)))
8595 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8597 return safe_codings
;
8601 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8602 Sunencodable_char_position
, 3, 5, 0,
8604 Return position of first un-encodable character in a region.
8605 START and END specify the region and CODING-SYSTEM specifies the
8606 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8608 If optional 4th argument COUNT is non-nil, it specifies at most how
8609 many un-encodable characters to search. In this case, the value is a
8612 If optional 5th argument STRING is non-nil, it is a string to search
8613 for un-encodable characters. In that case, START and END are indexes
8615 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8618 struct coding_system coding
;
8619 Lisp_Object attrs
, charset_list
, translation_table
;
8620 Lisp_Object positions
;
8622 const unsigned char *p
, *stop
, *pend
;
8623 int ascii_compatible
;
8625 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8626 attrs
= CODING_ID_ATTRS (coding
.id
);
8627 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8629 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8630 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8631 translation_table
= get_translation_table (attrs
, 1, NULL
);
8635 validate_region (&start
, &end
);
8636 from
= XINT (start
);
8638 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8639 || (ascii_compatible
8640 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8642 p
= CHAR_POS_ADDR (from
);
8643 pend
= CHAR_POS_ADDR (to
);
8644 if (from
< GPT
&& to
>= GPT
)
8651 CHECK_STRING (string
);
8652 CHECK_NATNUM (start
);
8654 from
= XINT (start
);
8657 || to
> SCHARS (string
))
8658 args_out_of_range_3 (string
, start
, end
);
8659 if (! STRING_MULTIBYTE (string
))
8661 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8662 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8663 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8671 CHECK_NATNUM (count
);
8680 if (ascii_compatible
)
8681 while (p
< stop
&& ASCII_BYTE_P (*p
))
8691 c
= STRING_CHAR_ADVANCE (p
);
8692 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8693 && ! char_charset (translate_char (translation_table
, c
),
8694 charset_list
, NULL
))
8696 positions
= Fcons (make_number (from
), positions
);
8705 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8709 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8710 Scheck_coding_systems_region
, 3, 3, 0,
8711 doc
: /* Check if the region is encodable by coding systems.
8713 START and END are buffer positions specifying the region.
8714 CODING-SYSTEM-LIST is a list of coding systems to check.
8716 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8717 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8718 whole region, POS0, POS1, ... are buffer positions where non-encodable
8719 characters are found.
8721 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8724 START may be a string. In that case, check if the string is
8725 encodable, and the value contains indices to the string instead of
8726 buffer positions. END is ignored.
8728 If the current buffer (or START if it is a string) is unibyte, the value
8730 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8733 EMACS_INT start_byte
, end_byte
;
8735 const unsigned char *p
, *pbeg
, *pend
;
8737 Lisp_Object tail
, elt
, attrs
;
8739 if (STRINGP (start
))
8741 if (!STRING_MULTIBYTE (start
)
8742 || SCHARS (start
) == SBYTES (start
))
8745 end_byte
= SBYTES (start
);
8750 CHECK_NUMBER_COERCE_MARKER (start
);
8751 CHECK_NUMBER_COERCE_MARKER (end
);
8752 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8753 args_out_of_range (start
, end
);
8754 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8756 start_byte
= CHAR_TO_BYTE (XINT (start
));
8757 end_byte
= CHAR_TO_BYTE (XINT (end
));
8758 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8761 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8763 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8764 move_gap_both (XINT (start
), start_byte
);
8766 move_gap_both (XINT (end
), end_byte
);
8772 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8775 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8776 ASET (attrs
, coding_attr_trans_tbl
,
8777 get_translation_table (attrs
, 1, NULL
));
8778 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8781 if (STRINGP (start
))
8782 p
= pbeg
= SDATA (start
);
8784 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8785 pend
= p
+ (end_byte
- start_byte
);
8787 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8788 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8792 if (ASCII_BYTE_P (*p
))
8796 c
= STRING_CHAR_ADVANCE (p
);
8798 charset_map_loaded
= 0;
8799 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8801 elt
= XCDR (XCAR (tail
));
8802 if (! char_encodable_p (c
, XCAR (elt
)))
8803 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8805 if (charset_map_loaded
)
8807 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8809 if (STRINGP (start
))
8810 pbeg
= SDATA (start
);
8812 pbeg
= BYTE_POS_ADDR (start_byte
);
8813 p
= pbeg
+ p_offset
;
8814 pend
= pbeg
+ pend_offset
;
8822 for (; CONSP (tail
); tail
= XCDR (tail
))
8825 if (CONSP (XCDR (XCDR (elt
))))
8826 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8835 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8836 Lisp_Object coding_system
, Lisp_Object dst_object
,
8837 int encodep
, int norecord
)
8839 struct coding_system coding
;
8840 EMACS_INT from
, from_byte
, to
, to_byte
;
8841 Lisp_Object src_object
;
8843 CHECK_NUMBER_COERCE_MARKER (start
);
8844 CHECK_NUMBER_COERCE_MARKER (end
);
8845 if (NILP (coding_system
))
8846 coding_system
= Qno_conversion
;
8848 CHECK_CODING_SYSTEM (coding_system
);
8849 src_object
= Fcurrent_buffer ();
8850 if (NILP (dst_object
))
8851 dst_object
= src_object
;
8852 else if (! EQ (dst_object
, Qt
))
8853 CHECK_BUFFER (dst_object
);
8855 validate_region (&start
, &end
);
8856 from
= XFASTINT (start
);
8857 from_byte
= CHAR_TO_BYTE (from
);
8858 to
= XFASTINT (end
);
8859 to_byte
= CHAR_TO_BYTE (to
);
8861 setup_coding_system (coding_system
, &coding
);
8862 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8865 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8868 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8871 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8873 return (BUFFERP (dst_object
)
8874 ? make_number (coding
.produced_char
)
8875 : coding
.dst_object
);
8879 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8880 3, 4, "r\nzCoding system: ",
8881 doc
: /* Decode the current region from the specified coding system.
8882 When called from a program, takes four arguments:
8883 START, END, CODING-SYSTEM, and DESTINATION.
8884 START and END are buffer positions.
8886 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8887 If nil, the region between START and END is replaced by the decoded text.
8888 If buffer, the decoded text is inserted in that buffer after point (point
8890 In those cases, the length of the decoded text is returned.
8891 If DESTINATION is t, the decoded text is returned.
8893 This function sets `last-coding-system-used' to the precise coding system
8894 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8895 not fully specified.) */)
8896 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8898 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8901 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8902 3, 4, "r\nzCoding system: ",
8903 doc
: /* Encode the current region by specified coding system.
8904 When called from a program, takes four arguments:
8905 START, END, CODING-SYSTEM and DESTINATION.
8906 START and END are buffer positions.
8908 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8909 If nil, the region between START and END is replace by the encoded text.
8910 If buffer, the encoded text is inserted in that buffer after point (point
8912 In those cases, the length of the encoded text is returned.
8913 If DESTINATION is t, the encoded text is returned.
8915 This function sets `last-coding-system-used' to the precise coding system
8916 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8917 not fully specified.) */)
8918 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8920 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8924 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8925 Lisp_Object dst_object
, int encodep
, int nocopy
, int norecord
)
8927 struct coding_system coding
;
8928 EMACS_INT chars
, bytes
;
8930 CHECK_STRING (string
);
8931 if (NILP (coding_system
))
8934 Vlast_coding_system_used
= Qno_conversion
;
8935 if (NILP (dst_object
))
8936 return (nocopy
? Fcopy_sequence (string
) : string
);
8939 if (NILP (coding_system
))
8940 coding_system
= Qno_conversion
;
8942 CHECK_CODING_SYSTEM (coding_system
);
8943 if (NILP (dst_object
))
8945 else if (! EQ (dst_object
, Qt
))
8946 CHECK_BUFFER (dst_object
);
8948 setup_coding_system (coding_system
, &coding
);
8949 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8950 chars
= SCHARS (string
);
8951 bytes
= SBYTES (string
);
8953 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8955 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8957 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8959 return (BUFFERP (dst_object
)
8960 ? make_number (coding
.produced_char
)
8961 : coding
.dst_object
);
8965 /* Encode or decode STRING according to CODING_SYSTEM.
8966 Do not set Vlast_coding_system_used.
8968 This function is called only from macros DECODE_FILE and
8969 ENCODE_FILE, thus we ignore character composition. */
8972 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
8975 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8979 DEFUE ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8981 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8983 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8984 if the decoding operation is trivial.
8986 Optional fourth arg BUFFER non-nil means that the decoded text is
8987 inserted in that buffer after point (point does not move). In this
8988 case, the return value is the length of the decoded text.
8990 This function sets `last-coding-system-used' to the precise coding system
8991 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8992 not fully specified.) */)
8993 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
8995 return code_convert_string (string
, coding_system
, buffer
,
8996 0, ! NILP (nocopy
), 0);
8999 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9001 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9003 Optional third arg NOCOPY non-nil means it is OK to return STRING
9004 itself if the encoding operation is trivial.
9006 Optional fourth arg BUFFER non-nil means that the encoded text is
9007 inserted in that buffer after point (point does not move). In this
9008 case, the return value is the length of the encoded text.
9010 This function sets `last-coding-system-used' to the precise coding system
9011 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9012 not fully specified.) */)
9013 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9015 return code_convert_string (string
, coding_system
, buffer
,
9016 1, ! NILP (nocopy
), 1);
9020 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9021 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9022 Return the corresponding character. */)
9025 Lisp_Object spec
, attrs
, val
;
9026 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9030 CHECK_NATNUM (code
);
9031 ch
= XFASTINT (code
);
9032 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9033 attrs
= AREF (spec
, 0);
9035 if (ASCII_BYTE_P (ch
)
9036 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9039 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9040 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9041 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9042 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9047 charset
= charset_roman
;
9049 else if (ch
>= 0xA0 && ch
< 0xDF)
9052 charset
= charset_kana
;
9056 EMACS_INT c1
= ch
>> 8;
9059 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9060 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9061 error ("Invalid code: %"pEd
, ch
);
9064 charset
= charset_kanji
;
9066 c
= DECODE_CHAR (charset
, c
);
9068 error ("Invalid code: %"pEd
, ch
);
9069 return make_number (c
);
9073 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9074 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9075 Return the corresponding code in SJIS. */)
9078 Lisp_Object spec
, attrs
, charset_list
;
9080 struct charset
*charset
;
9083 CHECK_CHARACTER (ch
);
9085 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9086 attrs
= AREF (spec
, 0);
9088 if (ASCII_CHAR_P (c
)
9089 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9092 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9093 charset
= char_charset (c
, charset_list
, &code
);
9094 if (code
== CHARSET_INVALID_CODE (charset
))
9095 error ("Can't encode by shift_jis encoding: %d", c
);
9098 return make_number (code
);
9101 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9102 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9103 Return the corresponding character. */)
9106 Lisp_Object spec
, attrs
, val
;
9107 struct charset
*charset_roman
, *charset_big5
, *charset
;
9111 CHECK_NATNUM (code
);
9112 ch
= XFASTINT (code
);
9113 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9114 attrs
= AREF (spec
, 0);
9116 if (ASCII_BYTE_P (ch
)
9117 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9120 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9121 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9122 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9127 charset
= charset_roman
;
9131 EMACS_INT b1
= ch
>> 8;
9133 if (b1
< 0xA1 || b1
> 0xFE
9134 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9135 error ("Invalid code: %"pEd
, ch
);
9137 charset
= charset_big5
;
9139 c
= DECODE_CHAR (charset
, c
);
9141 error ("Invalid code: %"pEd
, ch
);
9142 return make_number (c
);
9145 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9146 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9147 Return the corresponding character code in Big5. */)
9150 Lisp_Object spec
, attrs
, charset_list
;
9151 struct charset
*charset
;
9155 CHECK_CHARACTER (ch
);
9157 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9158 attrs
= AREF (spec
, 0);
9159 if (ASCII_CHAR_P (c
)
9160 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9163 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9164 charset
= char_charset (c
, charset_list
, &code
);
9165 if (code
== CHARSET_INVALID_CODE (charset
))
9166 error ("Can't encode by Big5 encoding: %d", c
);
9168 return make_number (code
);
9172 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9173 Sset_terminal_coding_system_internal
, 1, 2, 0,
9174 doc
: /* Internal use only. */)
9175 (Lisp_Object coding_system
, Lisp_Object terminal
)
9177 struct terminal
*term
= get_terminal (terminal
, 1);
9178 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9179 CHECK_SYMBOL (coding_system
);
9180 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9181 /* We had better not send unsafe characters to terminal. */
9182 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9183 /* Character composition should be disabled. */
9184 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9185 terminal_coding
->src_multibyte
= 1;
9186 terminal_coding
->dst_multibyte
= 0;
9187 if (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
)
9188 term
->charset_list
= coding_charset_list (terminal_coding
);
9190 term
->charset_list
= Fcons (make_number (charset_ascii
), Qnil
);
9194 DEFUN ("set-safe-terminal-coding-system-internal",
9195 Fset_safe_terminal_coding_system_internal
,
9196 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9197 doc
: /* Internal use only. */)
9198 (Lisp_Object coding_system
)
9200 CHECK_SYMBOL (coding_system
);
9201 setup_coding_system (Fcheck_coding_system (coding_system
),
9202 &safe_terminal_coding
);
9203 /* Character composition should be disabled. */
9204 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9205 safe_terminal_coding
.src_multibyte
= 1;
9206 safe_terminal_coding
.dst_multibyte
= 0;
9210 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9211 Sterminal_coding_system
, 0, 1, 0,
9212 doc
: /* Return coding system specified for terminal output on the given terminal.
9213 TERMINAL may be a terminal object, a frame, or nil for the selected
9214 frame's terminal device. */)
9215 (Lisp_Object terminal
)
9217 struct coding_system
*terminal_coding
9218 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9219 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9221 /* For backward compatibility, return nil if it is `undecided'. */
9222 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9225 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9226 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9227 doc
: /* Internal use only. */)
9228 (Lisp_Object coding_system
, Lisp_Object terminal
)
9230 struct terminal
*t
= get_terminal (terminal
, 1);
9231 CHECK_SYMBOL (coding_system
);
9232 if (NILP (coding_system
))
9233 coding_system
= Qno_conversion
;
9235 Fcheck_coding_system (coding_system
);
9236 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9237 /* Character composition should be disabled. */
9238 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9239 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9243 DEFUN ("keyboard-coding-system",
9244 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9245 doc
: /* Return coding system specified for decoding keyboard input. */)
9246 (Lisp_Object terminal
)
9248 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9249 (get_terminal (terminal
, 1))->id
);
9253 DEFUE ("find-operation-coding-system", Ffind_operation_coding_system
,
9254 Sfind_operation_coding_system
, 1, MANY
, 0,
9255 doc
: /* Choose a coding system for an operation based on the target name.
9256 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9257 DECODING-SYSTEM is the coding system to use for decoding
9258 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9259 for encoding (in case OPERATION does encoding).
9261 The first argument OPERATION specifies an I/O primitive:
9262 For file I/O, `insert-file-contents' or `write-region'.
9263 For process I/O, `call-process', `call-process-region', or `start-process'.
9264 For network I/O, `open-network-stream'.
9266 The remaining arguments should be the same arguments that were passed
9267 to the primitive. Depending on which primitive, one of those arguments
9268 is selected as the TARGET. For example, if OPERATION does file I/O,
9269 whichever argument specifies the file name is TARGET.
9271 TARGET has a meaning which depends on OPERATION:
9272 For file I/O, TARGET is a file name (except for the special case below).
9273 For process I/O, TARGET is a process name.
9274 For network I/O, TARGET is a service name or a port number.
9276 This function looks up what is specified for TARGET in
9277 `file-coding-system-alist', `process-coding-system-alist',
9278 or `network-coding-system-alist' depending on OPERATION.
9279 They may specify a coding system, a cons of coding systems,
9280 or a function symbol to call.
9281 In the last case, we call the function with one argument,
9282 which is a list of all the arguments given to this function.
9283 If the function can't decide a coding system, it can return
9284 `undecided' so that the normal code-detection is performed.
9286 If OPERATION is `insert-file-contents', the argument corresponding to
9287 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9288 file name to look up, and BUFFER is a buffer that contains the file's
9289 contents (not yet decoded). If `file-coding-system-alist' specifies a
9290 function to call for FILENAME, that function should examine the
9291 contents of BUFFER instead of reading the file.
9293 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9294 (size_t nargs
, Lisp_Object
*args
)
9296 Lisp_Object operation
, target_idx
, target
, val
;
9297 register Lisp_Object chain
;
9300 error ("Too few arguments");
9301 operation
= args
[0];
9302 if (!SYMBOLP (operation
)
9303 || !NATNUMP (target_idx
= Fget (operation
, Qtarget_idx
)))
9304 error ("Invalid first argument");
9305 if (nargs
< 1 + XFASTINT (target_idx
))
9306 error ("Too few arguments for operation: %s",
9307 SDATA (SYMBOL_NAME (operation
)));
9308 target
= args
[XFASTINT (target_idx
) + 1];
9309 if (!(STRINGP (target
)
9310 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9311 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9312 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9313 error ("Invalid %"pEd
"th argument", XFASTINT (target_idx
) + 1);
9315 target
= XCAR (target
);
9317 chain
= ((EQ (operation
, Qinsert_file_contents
)
9318 || EQ (operation
, Qwrite_region
))
9319 ? Vfile_coding_system_alist
9320 : (EQ (operation
, Qopen_network_stream
)
9321 ? Vnetwork_coding_system_alist
9322 : Vprocess_coding_system_alist
));
9326 for (; CONSP (chain
); chain
= XCDR (chain
))
9332 && ((STRINGP (target
)
9333 && STRINGP (XCAR (elt
))
9334 && fast_string_match (XCAR (elt
), target
) >= 0)
9335 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9338 /* Here, if VAL is both a valid coding system and a valid
9339 function symbol, we return VAL as a coding system. */
9342 if (! SYMBOLP (val
))
9344 if (! NILP (Fcoding_system_p (val
)))
9345 return Fcons (val
, val
);
9346 if (! NILP (Ffboundp (val
)))
9348 /* We use call1 rather than safe_call1
9349 so as to get bug reports about functions called here
9350 which don't handle the current interface. */
9351 val
= call1 (val
, Flist (nargs
, args
));
9354 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9355 return Fcons (val
, val
);
9363 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9364 Sset_coding_system_priority
, 0, MANY
, 0,
9365 doc
: /* Assign higher priority to the coding systems given as arguments.
9366 If multiple coding systems belong to the same category,
9367 all but the first one are ignored.
9369 usage: (set-coding-system-priority &rest coding-systems) */)
9370 (size_t nargs
, Lisp_Object
*args
)
9373 int changed
[coding_category_max
];
9374 enum coding_category priorities
[coding_category_max
];
9376 memset (changed
, 0, sizeof changed
);
9378 for (i
= j
= 0; i
< nargs
; i
++)
9380 enum coding_category category
;
9381 Lisp_Object spec
, attrs
;
9383 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9384 attrs
= AREF (spec
, 0);
9385 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9386 if (changed
[category
])
9387 /* Ignore this coding system because a coding system of the
9388 same category already had a higher priority. */
9390 changed
[category
] = 1;
9391 priorities
[j
++] = category
;
9392 if (coding_categories
[category
].id
>= 0
9393 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9394 setup_coding_system (args
[i
], &coding_categories
[category
]);
9395 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9398 /* Now we have decided top J priorities. Reflect the order of the
9399 original priorities to the remaining priorities. */
9401 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9403 while (j
< coding_category_max
9404 && changed
[coding_priorities
[j
]])
9406 if (j
== coding_category_max
)
9408 priorities
[i
] = coding_priorities
[j
];
9411 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9413 /* Update `coding-category-list'. */
9414 Vcoding_category_list
= Qnil
;
9415 for (i
= coding_category_max
; i
-- > 0; )
9416 Vcoding_category_list
9417 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9418 Vcoding_category_list
);
9423 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9424 Scoding_system_priority_list
, 0, 1, 0,
9425 doc
: /* Return a list of coding systems ordered by their priorities.
9426 The list contains a subset of coding systems; i.e. coding systems
9427 assigned to each coding category (see `coding-category-list').
9429 HIGHESTP non-nil means just return the highest priority one. */)
9430 (Lisp_Object highestp
)
9435 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9437 enum coding_category category
= coding_priorities
[i
];
9438 int id
= coding_categories
[category
].id
;
9443 attrs
= CODING_ID_ATTRS (id
);
9444 if (! NILP (highestp
))
9445 return CODING_ATTR_BASE_NAME (attrs
);
9446 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9448 return Fnreverse (val
);
9451 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9454 make_subsidiaries (Lisp_Object base
)
9456 Lisp_Object subsidiaries
;
9457 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9458 char *buf
= (char *) alloca (base_name_len
+ 6);
9461 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9462 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9463 for (i
= 0; i
< 3; i
++)
9465 memcpy (buf
+ base_name_len
, suffixes
[i
], strlen (suffixes
[i
]) + 1);
9466 ASET (subsidiaries
, i
, intern (buf
));
9468 return subsidiaries
;
9472 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9473 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9474 doc
: /* For internal use only.
9475 usage: (define-coding-system-internal ...) */)
9476 (size_t nargs
, Lisp_Object
*args
)
9479 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9480 Lisp_Object attrs
; /* Vector of attributes. */
9481 Lisp_Object eol_type
;
9482 Lisp_Object aliases
;
9483 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9484 enum coding_category category
;
9485 Lisp_Object tail
, val
;
9486 int max_charset_id
= 0;
9489 if (nargs
< coding_arg_max
)
9492 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9494 name
= args
[coding_arg_name
];
9495 CHECK_SYMBOL (name
);
9496 CODING_ATTR_BASE_NAME (attrs
) = name
;
9498 val
= args
[coding_arg_mnemonic
];
9499 if (! STRINGP (val
))
9500 CHECK_CHARACTER (val
);
9501 CODING_ATTR_MNEMONIC (attrs
) = val
;
9503 coding_type
= args
[coding_arg_coding_type
];
9504 CHECK_SYMBOL (coding_type
);
9505 CODING_ATTR_TYPE (attrs
) = coding_type
;
9507 charset_list
= args
[coding_arg_charset_list
];
9508 if (SYMBOLP (charset_list
))
9510 if (EQ (charset_list
, Qiso_2022
))
9512 if (! EQ (coding_type
, Qiso_2022
))
9513 error ("Invalid charset-list");
9514 charset_list
= Viso_2022_charset_list
;
9516 else if (EQ (charset_list
, Qemacs_mule
))
9518 if (! EQ (coding_type
, Qemacs_mule
))
9519 error ("Invalid charset-list");
9520 charset_list
= Vemacs_mule_charset_list
;
9522 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9523 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9524 max_charset_id
= XFASTINT (XCAR (tail
));
9528 charset_list
= Fcopy_sequence (charset_list
);
9529 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9531 struct charset
*charset
;
9534 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9535 if (EQ (coding_type
, Qiso_2022
)
9536 ? CHARSET_ISO_FINAL (charset
) < 0
9537 : EQ (coding_type
, Qemacs_mule
)
9538 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9540 error ("Can't handle charset `%s'",
9541 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9543 XSETCAR (tail
, make_number (charset
->id
));
9544 if (max_charset_id
< charset
->id
)
9545 max_charset_id
= charset
->id
;
9548 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9550 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9551 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9552 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9553 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9554 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9556 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9558 val
= args
[coding_arg_decode_translation_table
];
9559 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9561 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9563 val
= args
[coding_arg_encode_translation_table
];
9564 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9566 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9568 val
= args
[coding_arg_post_read_conversion
];
9570 CODING_ATTR_POST_READ (attrs
) = val
;
9572 val
= args
[coding_arg_pre_write_conversion
];
9574 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9576 val
= args
[coding_arg_default_char
];
9578 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9581 CHECK_CHARACTER (val
);
9582 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9585 val
= args
[coding_arg_for_unibyte
];
9586 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9588 val
= args
[coding_arg_plist
];
9590 CODING_ATTR_PLIST (attrs
) = val
;
9592 if (EQ (coding_type
, Qcharset
))
9594 /* Generate a lisp vector of 256 elements. Each element is nil,
9595 integer, or a list of charset IDs.
9597 If Nth element is nil, the byte code N is invalid in this
9600 If Nth element is a number NUM, N is the first byte of a
9601 charset whose ID is NUM.
9603 If Nth element is a list of charset IDs, N is the first byte
9604 of one of them. The list is sorted by dimensions of the
9605 charsets. A charset of smaller dimension comes first. */
9606 val
= Fmake_vector (make_number (256), Qnil
);
9608 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9610 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9611 int dim
= CHARSET_DIMENSION (charset
);
9612 int idx
= (dim
- 1) * 4;
9614 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9615 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9617 for (i
= charset
->code_space
[idx
];
9618 i
<= charset
->code_space
[idx
+ 1]; i
++)
9620 Lisp_Object tmp
, tmp2
;
9623 tmp
= AREF (val
, i
);
9626 else if (NUMBERP (tmp
))
9628 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9630 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9632 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9636 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9638 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9643 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9646 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9647 XSETCAR (tmp2
, XCAR (tail
));
9653 ASET (attrs
, coding_attr_charset_valids
, val
);
9654 category
= coding_category_charset
;
9656 else if (EQ (coding_type
, Qccl
))
9660 if (nargs
< coding_arg_ccl_max
)
9663 val
= args
[coding_arg_ccl_decoder
];
9664 CHECK_CCL_PROGRAM (val
);
9666 val
= Fcopy_sequence (val
);
9667 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9669 val
= args
[coding_arg_ccl_encoder
];
9670 CHECK_CCL_PROGRAM (val
);
9672 val
= Fcopy_sequence (val
);
9673 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9675 val
= args
[coding_arg_ccl_valids
];
9676 valids
= Fmake_string (make_number (256), make_number (0));
9677 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9684 from
= to
= XINT (val
);
9685 if (from
< 0 || from
> 255)
9686 args_out_of_range_3 (val
, make_number (0), make_number (255));
9691 CHECK_NATNUM_CAR (val
);
9692 CHECK_NATNUM_CDR (val
);
9693 from
= XINT (XCAR (val
));
9695 args_out_of_range_3 (XCAR (val
),
9696 make_number (0), make_number (255));
9697 to
= XINT (XCDR (val
));
9698 if (to
< from
|| to
> 255)
9699 args_out_of_range_3 (XCDR (val
),
9700 XCAR (val
), make_number (255));
9702 for (i
= from
; i
<= to
; i
++)
9703 SSET (valids
, i
, 1);
9705 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9707 category
= coding_category_ccl
;
9709 else if (EQ (coding_type
, Qutf_16
))
9711 Lisp_Object bom
, endian
;
9713 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9715 if (nargs
< coding_arg_utf16_max
)
9718 bom
= args
[coding_arg_utf16_bom
];
9719 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9723 CHECK_CODING_SYSTEM (val
);
9725 CHECK_CODING_SYSTEM (val
);
9727 ASET (attrs
, coding_attr_utf_bom
, bom
);
9729 endian
= args
[coding_arg_utf16_endian
];
9730 CHECK_SYMBOL (endian
);
9733 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9734 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9735 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9737 category
= (CONSP (bom
)
9738 ? coding_category_utf_16_auto
9740 ? (EQ (endian
, Qbig
)
9741 ? coding_category_utf_16_be_nosig
9742 : coding_category_utf_16_le_nosig
)
9743 : (EQ (endian
, Qbig
)
9744 ? coding_category_utf_16_be
9745 : coding_category_utf_16_le
));
9747 else if (EQ (coding_type
, Qiso_2022
))
9749 Lisp_Object initial
, reg_usage
, request
, flags
;
9751 if (nargs
< coding_arg_iso2022_max
)
9754 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9755 CHECK_VECTOR (initial
);
9756 for (i
= 0; i
< 4; i
++)
9758 val
= Faref (initial
, make_number (i
));
9761 struct charset
*charset
;
9763 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9764 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9765 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9766 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9769 ASET (initial
, i
, make_number (-1));
9772 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9773 CHECK_CONS (reg_usage
);
9774 CHECK_NUMBER_CAR (reg_usage
);
9775 CHECK_NUMBER_CDR (reg_usage
);
9777 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9778 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9786 CHECK_CHARSET_GET_ID (tmp1
, id
);
9787 CHECK_NATNUM_CDR (val
);
9788 if (XINT (XCDR (val
)) >= 4)
9789 error ("Invalid graphic register number: %"pEd
, XINT (XCDR (val
)));
9790 XSETCAR (val
, make_number (id
));
9793 flags
= args
[coding_arg_iso2022_flags
];
9794 CHECK_NATNUM (flags
);
9796 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9797 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9799 ASET (attrs
, coding_attr_iso_initial
, initial
);
9800 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9801 ASET (attrs
, coding_attr_iso_request
, request
);
9802 ASET (attrs
, coding_attr_iso_flags
, flags
);
9803 setup_iso_safe_charsets (attrs
);
9805 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9806 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9807 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9808 ? coding_category_iso_7_else
9809 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9810 ? coding_category_iso_7
9811 : coding_category_iso_7_tight
);
9814 int id
= XINT (AREF (initial
, 1));
9816 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9817 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9819 ? coding_category_iso_8_else
9820 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9821 ? coding_category_iso_8_1
9822 : coding_category_iso_8_2
);
9824 if (category
!= coding_category_iso_8_1
9825 && category
!= coding_category_iso_8_2
)
9826 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9828 else if (EQ (coding_type
, Qemacs_mule
))
9830 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9831 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9832 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9833 category
= coding_category_emacs_mule
;
9835 else if (EQ (coding_type
, Qshift_jis
))
9838 struct charset
*charset
;
9840 if (XINT (Flength (charset_list
)) != 3
9841 && XINT (Flength (charset_list
)) != 4)
9842 error ("There should be three or four charsets");
9844 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9845 if (CHARSET_DIMENSION (charset
) != 1)
9846 error ("Dimension of charset %s is not one",
9847 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9848 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9849 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9851 charset_list
= XCDR (charset_list
);
9852 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9853 if (CHARSET_DIMENSION (charset
) != 1)
9854 error ("Dimension of charset %s is not one",
9855 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9857 charset_list
= XCDR (charset_list
);
9858 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9859 if (CHARSET_DIMENSION (charset
) != 2)
9860 error ("Dimension of charset %s is not two",
9861 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9863 charset_list
= XCDR (charset_list
);
9864 if (! NILP (charset_list
))
9866 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9867 if (CHARSET_DIMENSION (charset
) != 2)
9868 error ("Dimension of charset %s is not two",
9869 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9872 category
= coding_category_sjis
;
9873 Vsjis_coding_system
= name
;
9875 else if (EQ (coding_type
, Qbig5
))
9877 struct charset
*charset
;
9879 if (XINT (Flength (charset_list
)) != 2)
9880 error ("There should be just two charsets");
9882 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9883 if (CHARSET_DIMENSION (charset
) != 1)
9884 error ("Dimension of charset %s is not one",
9885 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9886 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9887 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9889 charset_list
= XCDR (charset_list
);
9890 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9891 if (CHARSET_DIMENSION (charset
) != 2)
9892 error ("Dimension of charset %s is not two",
9893 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9895 category
= coding_category_big5
;
9896 Vbig5_coding_system
= name
;
9898 else if (EQ (coding_type
, Qraw_text
))
9900 category
= coding_category_raw_text
;
9901 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9903 else if (EQ (coding_type
, Qutf_8
))
9907 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9909 if (nargs
< coding_arg_utf8_max
)
9912 bom
= args
[coding_arg_utf8_bom
];
9913 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9917 CHECK_CODING_SYSTEM (val
);
9919 CHECK_CODING_SYSTEM (val
);
9921 ASET (attrs
, coding_attr_utf_bom
, bom
);
9923 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9924 : NILP (bom
) ? coding_category_utf_8_nosig
9925 : coding_category_utf_8_sig
);
9927 else if (EQ (coding_type
, Qundecided
))
9928 category
= coding_category_undecided
;
9930 error ("Invalid coding system type: %s",
9931 SDATA (SYMBOL_NAME (coding_type
)));
9933 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9934 CODING_ATTR_PLIST (attrs
)
9935 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9936 CODING_ATTR_PLIST (attrs
)));
9937 CODING_ATTR_PLIST (attrs
)
9938 = Fcons (QCascii_compatible_p
,
9939 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9940 CODING_ATTR_PLIST (attrs
)));
9942 eol_type
= args
[coding_arg_eol_type
];
9943 if (! NILP (eol_type
)
9944 && ! EQ (eol_type
, Qunix
)
9945 && ! EQ (eol_type
, Qdos
)
9946 && ! EQ (eol_type
, Qmac
))
9947 error ("Invalid eol-type");
9949 aliases
= Fcons (name
, Qnil
);
9951 if (NILP (eol_type
))
9953 eol_type
= make_subsidiaries (name
);
9954 for (i
= 0; i
< 3; i
++)
9956 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9958 this_name
= AREF (eol_type
, i
);
9959 this_aliases
= Fcons (this_name
, Qnil
);
9960 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9961 this_spec
= Fmake_vector (make_number (3), attrs
);
9962 ASET (this_spec
, 1, this_aliases
);
9963 ASET (this_spec
, 2, this_eol_type
);
9964 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9965 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9966 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9968 Vcoding_system_alist
9969 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9970 Vcoding_system_alist
);
9974 spec_vec
= Fmake_vector (make_number (3), attrs
);
9975 ASET (spec_vec
, 1, aliases
);
9976 ASET (spec_vec
, 2, eol_type
);
9978 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9979 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9980 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9982 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9983 Vcoding_system_alist
);
9986 int id
= coding_categories
[category
].id
;
9988 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9989 setup_coding_system (name
, &coding_categories
[category
]);
9995 return Fsignal (Qwrong_number_of_arguments
,
9996 Fcons (intern ("define-coding-system-internal"),
9997 make_number (nargs
)));
10001 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10003 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10004 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10006 Lisp_Object spec
, attrs
;
10008 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10009 attrs
= AREF (spec
, 0);
10010 if (EQ (prop
, QCmnemonic
))
10012 if (! STRINGP (val
))
10013 CHECK_CHARACTER (val
);
10014 CODING_ATTR_MNEMONIC (attrs
) = val
;
10016 else if (EQ (prop
, QCdefault_char
))
10019 val
= make_number (' ');
10021 CHECK_CHARACTER (val
);
10022 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10024 else if (EQ (prop
, QCdecode_translation_table
))
10026 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10027 CHECK_SYMBOL (val
);
10028 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10030 else if (EQ (prop
, QCencode_translation_table
))
10032 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10033 CHECK_SYMBOL (val
);
10034 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10036 else if (EQ (prop
, QCpost_read_conversion
))
10038 CHECK_SYMBOL (val
);
10039 CODING_ATTR_POST_READ (attrs
) = val
;
10041 else if (EQ (prop
, QCpre_write_conversion
))
10043 CHECK_SYMBOL (val
);
10044 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10046 else if (EQ (prop
, QCascii_compatible_p
))
10048 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10051 CODING_ATTR_PLIST (attrs
)
10052 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10057 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10058 Sdefine_coding_system_alias
, 2, 2, 0,
10059 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10060 (Lisp_Object alias
, Lisp_Object coding_system
)
10062 Lisp_Object spec
, aliases
, eol_type
, val
;
10064 CHECK_SYMBOL (alias
);
10065 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10066 aliases
= AREF (spec
, 1);
10067 /* ALIASES should be a list of length more than zero, and the first
10068 element is a base coding system. Append ALIAS at the tail of the
10070 while (!NILP (XCDR (aliases
)))
10071 aliases
= XCDR (aliases
);
10072 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10074 eol_type
= AREF (spec
, 2);
10075 if (VECTORP (eol_type
))
10077 Lisp_Object subsidiaries
;
10080 subsidiaries
= make_subsidiaries (alias
);
10081 for (i
= 0; i
< 3; i
++)
10082 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10083 AREF (eol_type
, i
));
10086 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10087 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10088 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10090 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10091 Vcoding_system_alist
);
10096 DEFUE ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10098 doc
: /* Return the base of CODING-SYSTEM.
10099 Any alias or subsidiary coding system is not a base coding system. */)
10100 (Lisp_Object coding_system
)
10102 Lisp_Object spec
, attrs
;
10104 if (NILP (coding_system
))
10105 return (Qno_conversion
);
10106 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10107 attrs
= AREF (spec
, 0);
10108 return CODING_ATTR_BASE_NAME (attrs
);
10111 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10113 doc
: "Return the property list of CODING-SYSTEM.")
10114 (Lisp_Object coding_system
)
10116 Lisp_Object spec
, attrs
;
10118 if (NILP (coding_system
))
10119 coding_system
= Qno_conversion
;
10120 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10121 attrs
= AREF (spec
, 0);
10122 return CODING_ATTR_PLIST (attrs
);
10126 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10128 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10129 (Lisp_Object coding_system
)
10133 if (NILP (coding_system
))
10134 coding_system
= Qno_conversion
;
10135 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10136 return AREF (spec
, 1);
10139 DEFUE ("coding-system-eol-type", Fcoding_system_eol_type
,
10140 Scoding_system_eol_type
, 1, 1, 0,
10141 doc
: /* Return eol-type of CODING-SYSTEM.
10142 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10144 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10145 and CR respectively.
10147 A vector value indicates that a format of end-of-line should be
10148 detected automatically. Nth element of the vector is the subsidiary
10149 coding system whose eol-type is N. */)
10150 (Lisp_Object coding_system
)
10152 Lisp_Object spec
, eol_type
;
10155 if (NILP (coding_system
))
10156 coding_system
= Qno_conversion
;
10157 if (! CODING_SYSTEM_P (coding_system
))
10159 spec
= CODING_SYSTEM_SPEC (coding_system
);
10160 eol_type
= AREF (spec
, 2);
10161 if (VECTORP (eol_type
))
10162 return Fcopy_sequence (eol_type
);
10163 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10164 return make_number (n
);
10170 /*** 9. Post-amble ***/
10173 init_coding_once (void)
10177 for (i
= 0; i
< coding_category_max
; i
++)
10179 coding_categories
[i
].id
= -1;
10180 coding_priorities
[i
] = i
;
10183 /* ISO2022 specific initialize routine. */
10184 for (i
= 0; i
< 0x20; i
++)
10185 iso_code_class
[i
] = ISO_control_0
;
10186 for (i
= 0x21; i
< 0x7F; i
++)
10187 iso_code_class
[i
] = ISO_graphic_plane_0
;
10188 for (i
= 0x80; i
< 0xA0; i
++)
10189 iso_code_class
[i
] = ISO_control_1
;
10190 for (i
= 0xA1; i
< 0xFF; i
++)
10191 iso_code_class
[i
] = ISO_graphic_plane_1
;
10192 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10193 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10194 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10195 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10196 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10197 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10198 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10199 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10200 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10202 for (i
= 0; i
< 256; i
++)
10204 emacs_mule_bytes
[i
] = 1;
10206 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10207 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10208 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10209 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10215 syms_of_coding (void)
10217 staticpro (&Vcoding_system_hash_table
);
10219 Lisp_Object args
[2];
10222 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10225 staticpro (&Vsjis_coding_system
);
10226 Vsjis_coding_system
= Qnil
;
10228 staticpro (&Vbig5_coding_system
);
10229 Vbig5_coding_system
= Qnil
;
10231 staticpro (&Vcode_conversion_reused_workbuf
);
10232 Vcode_conversion_reused_workbuf
= Qnil
;
10234 staticpro (&Vcode_conversion_workbuf_name
);
10235 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10237 reused_workbuf_in_use
= 0;
10239 DEFSYM (Qcharset
, "charset");
10240 DEFSYM (Qtarget_idx
, "target-idx");
10241 DEFSYM (Qcoding_system_history
, "coding-system-history");
10242 Fset (Qcoding_system_history
, Qnil
);
10244 /* Target FILENAME is the first argument. */
10245 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10246 /* Target FILENAME is the third argument. */
10247 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10249 DEFSYM (Qcall_process
, "call-process");
10250 /* Target PROGRAM is the first argument. */
10251 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10253 DEFSYM (Qcall_process_region
, "call-process-region");
10254 /* Target PROGRAM is the third argument. */
10255 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10257 DEFSYM (Qstart_process
, "start-process");
10258 /* Target PROGRAM is the third argument. */
10259 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10261 DEFSYM (Qopen_network_stream
, "open-network-stream");
10262 /* Target SERVICE is the fourth argument. */
10263 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10265 DEFSYM (Qcoding_system
, "coding-system");
10266 DEFSYM (Qcoding_aliases
, "coding-aliases");
10268 DEFSYM (Qeol_type
, "eol-type");
10269 DEFSYM (Qunix
, "unix");
10270 DEFSYM (Qdos
, "dos");
10272 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10273 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10274 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10275 DEFSYM (Qdefault_char
, "default-char");
10276 DEFSYM (Qundecided
, "undecided");
10277 DEFSYM (Qno_conversion
, "no-conversion");
10278 DEFSYM (Qraw_text
, "raw-text");
10280 DEFSYM (Qiso_2022
, "iso-2022");
10282 DEFSYM (Qutf_8
, "utf-8");
10283 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10285 DEFSYM (Qutf_16
, "utf-16");
10286 DEFSYM (Qbig
, "big");
10287 DEFSYM (Qlittle
, "little");
10289 DEFSYM (Qshift_jis
, "shift-jis");
10290 DEFSYM (Qbig5
, "big5");
10292 DEFSYM (Qcoding_system_p
, "coding-system-p");
10294 DEFSYM (Qcoding_system_error
, "coding-system-error");
10295 Fput (Qcoding_system_error
, Qerror_conditions
,
10296 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10297 Fput (Qcoding_system_error
, Qerror_message
,
10298 make_pure_c_string ("Invalid coding system"));
10300 /* Intern this now in case it isn't already done.
10301 Setting this variable twice is harmless.
10302 But don't staticpro it here--that is done in alloc.c. */
10303 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10305 DEFSYM (Qtranslation_table
, "translation-table");
10306 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10307 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10308 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10309 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10311 DEFSYM (Qvalid_codes
, "valid-codes");
10313 DEFSYM (Qemacs_mule
, "emacs-mule");
10315 DEFSYM (QCcategory
, ":category");
10316 DEFSYM (QCmnemonic
, ":mnemonic");
10317 DEFSYM (QCdefault_char
, ":default-char");
10318 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10319 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10320 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10321 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10322 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10324 Vcoding_category_table
10325 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10326 staticpro (&Vcoding_category_table
);
10327 /* Followings are target of code detection. */
10328 ASET (Vcoding_category_table
, coding_category_iso_7
,
10329 intern_c_string ("coding-category-iso-7"));
10330 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10331 intern_c_string ("coding-category-iso-7-tight"));
10332 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10333 intern_c_string ("coding-category-iso-8-1"));
10334 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10335 intern_c_string ("coding-category-iso-8-2"));
10336 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10337 intern_c_string ("coding-category-iso-7-else"));
10338 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10339 intern_c_string ("coding-category-iso-8-else"));
10340 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10341 intern_c_string ("coding-category-utf-8-auto"));
10342 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10343 intern_c_string ("coding-category-utf-8"));
10344 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10345 intern_c_string ("coding-category-utf-8-sig"));
10346 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10347 intern_c_string ("coding-category-utf-16-be"));
10348 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10349 intern_c_string ("coding-category-utf-16-auto"));
10350 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10351 intern_c_string ("coding-category-utf-16-le"));
10352 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10353 intern_c_string ("coding-category-utf-16-be-nosig"));
10354 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10355 intern_c_string ("coding-category-utf-16-le-nosig"));
10356 ASET (Vcoding_category_table
, coding_category_charset
,
10357 intern_c_string ("coding-category-charset"));
10358 ASET (Vcoding_category_table
, coding_category_sjis
,
10359 intern_c_string ("coding-category-sjis"));
10360 ASET (Vcoding_category_table
, coding_category_big5
,
10361 intern_c_string ("coding-category-big5"));
10362 ASET (Vcoding_category_table
, coding_category_ccl
,
10363 intern_c_string ("coding-category-ccl"));
10364 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10365 intern_c_string ("coding-category-emacs-mule"));
10366 /* Followings are NOT target of code detection. */
10367 ASET (Vcoding_category_table
, coding_category_raw_text
,
10368 intern_c_string ("coding-category-raw-text"));
10369 ASET (Vcoding_category_table
, coding_category_undecided
,
10370 intern_c_string ("coding-category-undecided"));
10372 DEFSYM (Qinsufficient_source
, "insufficient-source");
10373 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10374 DEFSYM (Qinvalid_source
, "invalid-source");
10375 DEFSYM (Qinterrupted
, "interrupted");
10376 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10377 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10379 defsubr (&Scoding_system_p
);
10380 defsubr (&Sread_coding_system
);
10381 defsubr (&Sread_non_nil_coding_system
);
10382 defsubr (&Scheck_coding_system
);
10383 defsubr (&Sdetect_coding_region
);
10384 defsubr (&Sdetect_coding_string
);
10385 defsubr (&Sfind_coding_systems_region_internal
);
10386 defsubr (&Sunencodable_char_position
);
10387 defsubr (&Scheck_coding_systems_region
);
10388 defsubr (&Sdecode_coding_region
);
10389 defsubr (&Sencode_coding_region
);
10390 defsubr (&Sdecode_coding_string
);
10391 defsubr (&Sencode_coding_string
);
10392 defsubr (&Sdecode_sjis_char
);
10393 defsubr (&Sencode_sjis_char
);
10394 defsubr (&Sdecode_big5_char
);
10395 defsubr (&Sencode_big5_char
);
10396 defsubr (&Sset_terminal_coding_system_internal
);
10397 defsubr (&Sset_safe_terminal_coding_system_internal
);
10398 defsubr (&Sterminal_coding_system
);
10399 defsubr (&Sset_keyboard_coding_system_internal
);
10400 defsubr (&Skeyboard_coding_system
);
10401 defsubr (&Sfind_operation_coding_system
);
10402 defsubr (&Sset_coding_system_priority
);
10403 defsubr (&Sdefine_coding_system_internal
);
10404 defsubr (&Sdefine_coding_system_alias
);
10405 defsubr (&Scoding_system_put
);
10406 defsubr (&Scoding_system_base
);
10407 defsubr (&Scoding_system_plist
);
10408 defsubr (&Scoding_system_aliases
);
10409 defsubr (&Scoding_system_eol_type
);
10410 defsubr (&Scoding_system_priority_list
);
10412 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10413 doc
: /* List of coding systems.
10415 Do not alter the value of this variable manually. This variable should be
10416 updated by the functions `define-coding-system' and
10417 `define-coding-system-alias'. */);
10418 Vcoding_system_list
= Qnil
;
10420 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10421 doc
: /* Alist of coding system names.
10422 Each element is one element list of coding system name.
10423 This variable is given to `completing-read' as COLLECTION argument.
10425 Do not alter the value of this variable manually. This variable should be
10426 updated by the functions `make-coding-system' and
10427 `define-coding-system-alias'. */);
10428 Vcoding_system_alist
= Qnil
;
10430 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10431 doc
: /* List of coding-categories (symbols) ordered by priority.
10433 On detecting a coding system, Emacs tries code detection algorithms
10434 associated with each coding-category one by one in this order. When
10435 one algorithm agrees with a byte sequence of source text, the coding
10436 system bound to the corresponding coding-category is selected.
10438 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10442 Vcoding_category_list
= Qnil
;
10443 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10444 Vcoding_category_list
10445 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10446 Vcoding_category_list
);
10449 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10450 doc
: /* Specify the coding system for read operations.
10451 It is useful to bind this variable with `let', but do not set it globally.
10452 If the value is a coding system, it is used for decoding on read operation.
10453 If not, an appropriate element is used from one of the coding system alists.
10454 There are three such tables: `file-coding-system-alist',
10455 `process-coding-system-alist', and `network-coding-system-alist'. */);
10456 Vcoding_system_for_read
= Qnil
;
10458 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10459 doc
: /* Specify the coding system for write operations.
10460 Programs bind this variable with `let', but you should not set it globally.
10461 If the value is a coding system, it is used for encoding of output,
10462 when writing it to a file and when sending it to a file or subprocess.
10464 If this does not specify a coding system, an appropriate element
10465 is used from one of the coding system alists.
10466 There are three such tables: `file-coding-system-alist',
10467 `process-coding-system-alist', and `network-coding-system-alist'.
10468 For output to files, if the above procedure does not specify a coding system,
10469 the value of `buffer-file-coding-system' is used. */);
10470 Vcoding_system_for_write
= Qnil
;
10472 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10474 Coding system used in the latest file or process I/O. */);
10475 Vlast_coding_system_used
= Qnil
;
10477 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10479 Error status of the last code conversion.
10481 When an error was detected in the last code conversion, this variable
10482 is set to one of the following symbols.
10483 `insufficient-source'
10487 `insufficient-memory'
10488 When no error was detected, the value doesn't change. So, to check
10489 the error status of a code conversion by this variable, you must
10490 explicitly set this variable to nil before performing code
10492 Vlast_code_conversion_error
= Qnil
;
10494 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10496 *Non-nil means always inhibit code conversion of end-of-line format.
10497 See info node `Coding Systems' and info node `Text and Binary' concerning
10498 such conversion. */);
10499 inhibit_eol_conversion
= 0;
10501 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10503 Non-nil means process buffer inherits coding system of process output.
10504 Bind it to t if the process output is to be treated as if it were a file
10505 read from some filesystem. */);
10506 inherit_process_coding_system
= 0;
10508 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10510 Alist to decide a coding system to use for a file I/O operation.
10511 The format is ((PATTERN . VAL) ...),
10512 where PATTERN is a regular expression matching a file name,
10513 VAL is a coding system, a cons of coding systems, or a function symbol.
10514 If VAL is a coding system, it is used for both decoding and encoding
10516 If VAL is a cons of coding systems, the car part is used for decoding,
10517 and the cdr part is used for encoding.
10518 If VAL is a function symbol, the function must return a coding system
10519 or a cons of coding systems which are used as above. The function is
10520 called with an argument that is a list of the arguments with which
10521 `find-operation-coding-system' was called. If the function can't decide
10522 a coding system, it can return `undecided' so that the normal
10523 code-detection is performed.
10525 See also the function `find-operation-coding-system'
10526 and the variable `auto-coding-alist'. */);
10527 Vfile_coding_system_alist
= Qnil
;
10529 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10531 Alist to decide a coding system to use for a process I/O operation.
10532 The format is ((PATTERN . VAL) ...),
10533 where PATTERN is a regular expression matching a program name,
10534 VAL is a coding system, a cons of coding systems, or a function symbol.
10535 If VAL is a coding system, it is used for both decoding what received
10536 from the program and encoding what sent to the program.
10537 If VAL is a cons of coding systems, the car part is used for decoding,
10538 and the cdr part is used for encoding.
10539 If VAL is a function symbol, the function must return a coding system
10540 or a cons of coding systems which are used as above.
10542 See also the function `find-operation-coding-system'. */);
10543 Vprocess_coding_system_alist
= Qnil
;
10545 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10547 Alist to decide a coding system to use for a network I/O operation.
10548 The format is ((PATTERN . VAL) ...),
10549 where PATTERN is a regular expression matching a network service name
10550 or is a port number to connect to,
10551 VAL is a coding system, a cons of coding systems, or a function symbol.
10552 If VAL is a coding system, it is used for both decoding what received
10553 from the network stream and encoding what sent to the network stream.
10554 If VAL is a cons of coding systems, the car part is used for decoding,
10555 and the cdr part is used for encoding.
10556 If VAL is a function symbol, the function must return a coding system
10557 or a cons of coding systems which are used as above.
10559 See also the function `find-operation-coding-system'. */);
10560 Vnetwork_coding_system_alist
= Qnil
;
10562 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10563 doc
: /* Coding system to use with system messages.
10564 Also used for decoding keyboard input on X Window system. */);
10565 Vlocale_coding_system
= Qnil
;
10567 /* The eol mnemonics are reset in startup.el system-dependently. */
10568 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10570 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10571 eol_mnemonic_unix
= make_pure_c_string (":");
10573 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10575 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10576 eol_mnemonic_dos
= make_pure_c_string ("\\");
10578 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10580 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10581 eol_mnemonic_mac
= make_pure_c_string ("/");
10583 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10585 *String displayed in mode line when end-of-line format is not yet determined. */);
10586 eol_mnemonic_undecided
= make_pure_c_string (":");
10588 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10590 *Non-nil enables character translation while encoding and decoding. */);
10591 Venable_character_translation
= Qt
;
10593 DEFVAR_LISP ("standard-translation-table-for-decode",
10594 Vstandard_translation_table_for_decode
,
10595 doc
: /* Table for translating characters while decoding. */);
10596 Vstandard_translation_table_for_decode
= Qnil
;
10598 DEFVAR_LISP ("standard-translation-table-for-encode",
10599 Vstandard_translation_table_for_encode
,
10600 doc
: /* Table for translating characters while encoding. */);
10601 Vstandard_translation_table_for_encode
= Qnil
;
10603 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10604 doc
: /* Alist of charsets vs revision numbers.
10605 While encoding, if a charset (car part of an element) is found,
10606 designate it with the escape sequence identifying revision (cdr part
10607 of the element). */);
10608 Vcharset_revision_table
= Qnil
;
10610 DEFVAR_LISP ("default-process-coding-system",
10611 Vdefault_process_coding_system
,
10612 doc
: /* Cons of coding systems used for process I/O by default.
10613 The car part is used for decoding a process output,
10614 the cdr part is used for encoding a text to be sent to a process. */);
10615 Vdefault_process_coding_system
= Qnil
;
10617 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10619 Table of extra Latin codes in the range 128..159 (inclusive).
10620 This is a vector of length 256.
10621 If Nth element is non-nil, the existence of code N in a file
10622 \(or output of subprocess) doesn't prevent it to be detected as
10623 a coding system of ISO 2022 variant which has a flag
10624 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10625 or reading output of a subprocess.
10626 Only 128th through 159th elements have a meaning. */);
10627 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10629 DEFVAR_LISP ("select-safe-coding-system-function",
10630 Vselect_safe_coding_system_function
,
10632 Function to call to select safe coding system for encoding a text.
10634 If set, this function is called to force a user to select a proper
10635 coding system which can encode the text in the case that a default
10636 coding system used in each operation can't encode the text. The
10637 function should take care that the buffer is not modified while
10638 the coding system is being selected.
10640 The default value is `select-safe-coding-system' (which see). */);
10641 Vselect_safe_coding_system_function
= Qnil
;
10643 DEFVAR_BOOL ("coding-system-require-warning",
10644 coding_system_require_warning
,
10645 doc
: /* Internal use only.
10646 If non-nil, on writing a file, `select-safe-coding-system-function' is
10647 called even if `coding-system-for-write' is non-nil. The command
10648 `universal-coding-system-argument' binds this variable to t temporarily. */);
10649 coding_system_require_warning
= 0;
10652 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10653 inhibit_iso_escape_detection
,
10655 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10657 When Emacs reads text, it tries to detect how the text is encoded.
10658 This code detection is sensitive to escape sequences. If Emacs sees
10659 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10660 of the ISO2022 encodings, and decodes text by the corresponding coding
10661 system (e.g. `iso-2022-7bit').
10663 However, there may be a case that you want to read escape sequences in
10664 a file as is. In such a case, you can set this variable to non-nil.
10665 Then the code detection will ignore any escape sequences, and no text is
10666 detected as encoded in some ISO-2022 encoding. The result is that all
10667 escape sequences become visible in a buffer.
10669 The default value is nil, and it is strongly recommended not to change
10670 it. That is because many Emacs Lisp source files that contain
10671 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10672 in Emacs's distribution, and they won't be decoded correctly on
10673 reading if you suppress escape sequence detection.
10675 The other way to read escape sequences in a file without decoding is
10676 to explicitly specify some coding system that doesn't use ISO-2022
10677 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10678 inhibit_iso_escape_detection
= 0;
10680 DEFVAR_BOOL ("inhibit-null-byte-detection",
10681 inhibit_null_byte_detection
,
10682 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10683 By default, Emacs treats it as binary data, and does not attempt to
10684 decode it. The effect is as if you specified `no-conversion' for
10687 Set this to non-nil when a regular text happens to include null bytes.
10688 Examples are Index nodes of Info files and null-byte delimited output
10689 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10690 decode text as usual. */);
10691 inhibit_null_byte_detection
= 0;
10693 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10694 doc
: /* Char table for translating self-inserting characters.
10695 This is applied to the result of input methods, not their input.
10696 See also `keyboard-translate-table'.
10698 Use of this variable for character code unification was rendered
10699 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10700 internal character representation. */);
10701 Vtranslation_table_for_input
= Qnil
;
10704 Lisp_Object args
[coding_arg_max
];
10705 Lisp_Object plist
[16];
10708 for (i
= 0; i
< coding_arg_max
; i
++)
10711 plist
[0] = intern_c_string (":name");
10712 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10713 plist
[2] = intern_c_string (":mnemonic");
10714 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10715 plist
[4] = intern_c_string (":coding-type");
10716 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10717 plist
[6] = intern_c_string (":ascii-compatible-p");
10718 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10719 plist
[8] = intern_c_string (":default-char");
10720 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10721 plist
[10] = intern_c_string (":for-unibyte");
10722 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10723 plist
[12] = intern_c_string (":docstring");
10724 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10726 When you visit a file with this coding, the file is read into a\n\
10727 unibyte buffer as is, thus each byte of a file is treated as a\n\
10729 plist
[14] = intern_c_string (":eol-type");
10730 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10731 args
[coding_arg_plist
] = Flist (16, plist
);
10732 Fdefine_coding_system_internal (coding_arg_max
, args
);
10734 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10735 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10736 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10737 /* This is already set.
10738 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10739 plist
[8] = intern_c_string (":charset-list");
10740 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10741 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10742 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10743 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10744 args
[coding_arg_plist
] = Flist (16, plist
);
10745 Fdefine_coding_system_internal (coding_arg_max
, args
);
10748 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10753 for (i
= 0; i
< coding_category_max
; i
++)
10754 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10756 #if defined (DOS_NT)
10757 system_eol_type
= Qdos
;
10759 system_eol_type
= Qunix
;
10761 staticpro (&system_eol_type
);
10765 emacs_strerror (int error_number
)
10769 synchronize_system_messages_locale ();
10770 str
= strerror (error_number
);
10772 if (! NILP (Vlocale_coding_system
))
10774 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10775 Vlocale_coding_system
,
10777 str
= SSDATA (dec
);