1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2012 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. On
59 the 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 true if the byte sequence conforms to XXX.
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 bool multibytep
= coding
->src_multibyte
;
162 ptrdiff_t 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 bool 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 bool 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 ptrdiff_t 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 ***/
291 #include "character.h"
295 #include "composite.h"
299 #include "termhooks.h"
301 Lisp_Object Vcoding_system_hash_table
;
303 static Lisp_Object Qcoding_system
, Qeol_type
;
304 static Lisp_Object Qcoding_aliases
;
305 Lisp_Object Qunix
, Qdos
;
306 Lisp_Object Qbuffer_file_coding_system
;
307 static Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
308 static Lisp_Object Qdefault_char
;
309 Lisp_Object Qno_conversion
, Qundecided
;
310 Lisp_Object Qcharset
, Qutf_8
;
311 static Lisp_Object Qiso_2022
;
312 static Lisp_Object Qutf_16
, Qshift_jis
, Qbig5
;
313 static Lisp_Object Qbig
, Qlittle
;
314 static Lisp_Object Qcoding_system_history
;
315 static Lisp_Object Qvalid_codes
;
316 static Lisp_Object QCcategory
, QCmnemonic
, QCdefault_char
;
317 static Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
318 static Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
319 static Lisp_Object QCascii_compatible_p
;
321 Lisp_Object Qcall_process
, Qcall_process_region
;
322 Lisp_Object Qstart_process
, Qopen_network_stream
;
323 static Lisp_Object Qtarget_idx
;
325 static Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
326 static Lisp_Object Qinterrupted
, Qinsufficient_memory
;
328 /* If a symbol has this property, evaluate the value to define the
329 symbol as a coding system. */
330 static Lisp_Object Qcoding_system_define_form
;
332 /* Format of end-of-line decided by system. This is Qunix on
333 Unix and Mac, Qdos on DOS/Windows.
334 This has an effect only for external encoding (i.e. for output to
335 file and process), not for in-buffer or Lisp string encoding. */
336 static Lisp_Object system_eol_type
;
340 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
342 /* Coding system emacs-mule and raw-text are for converting only
343 end-of-line format. */
344 Lisp_Object Qemacs_mule
, Qraw_text
;
345 Lisp_Object Qutf_8_emacs
;
347 /* Coding-systems are handed between Emacs Lisp programs and C internal
348 routines by the following three variables. */
349 /* Coding system to be used to encode text for terminal display when
350 terminal coding system is nil. */
351 struct coding_system safe_terminal_coding
;
355 Lisp_Object Qtranslation_table
;
356 Lisp_Object Qtranslation_table_id
;
357 static Lisp_Object Qtranslation_table_for_decode
;
358 static Lisp_Object Qtranslation_table_for_encode
;
360 /* Two special coding systems. */
361 static Lisp_Object Vsjis_coding_system
;
362 static Lisp_Object Vbig5_coding_system
;
364 /* ISO2022 section */
366 #define CODING_ISO_INITIAL(coding, reg) \
367 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
368 coding_attr_iso_initial), \
372 #define CODING_ISO_REQUEST(coding, charset_id) \
373 (((charset_id) <= (coding)->max_charset_id \
374 ? ((coding)->safe_charsets[charset_id] != 255 \
375 ? (coding)->safe_charsets[charset_id] \
380 #define CODING_ISO_FLAGS(coding) \
381 ((coding)->spec.iso_2022.flags)
382 #define CODING_ISO_DESIGNATION(coding, reg) \
383 ((coding)->spec.iso_2022.current_designation[reg])
384 #define CODING_ISO_INVOCATION(coding, plane) \
385 ((coding)->spec.iso_2022.current_invocation[plane])
386 #define CODING_ISO_SINGLE_SHIFTING(coding) \
387 ((coding)->spec.iso_2022.single_shifting)
388 #define CODING_ISO_BOL(coding) \
389 ((coding)->spec.iso_2022.bol)
390 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
391 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
392 #define CODING_ISO_CMP_STATUS(coding) \
393 (&(coding)->spec.iso_2022.cmp_status)
394 #define CODING_ISO_EXTSEGMENT_LEN(coding) \
395 ((coding)->spec.iso_2022.ctext_extended_segment_len)
396 #define CODING_ISO_EMBEDDED_UTF_8(coding) \
397 ((coding)->spec.iso_2022.embedded_utf_8)
399 /* Control characters of ISO2022. */
400 /* code */ /* function */
401 #define ISO_CODE_SO 0x0E /* shift-out */
402 #define ISO_CODE_SI 0x0F /* shift-in */
403 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
404 #define ISO_CODE_ESC 0x1B /* escape */
405 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
406 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
407 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
409 /* All code (1-byte) of ISO2022 is classified into one of the
411 enum iso_code_class_type
413 ISO_control_0
, /* Control codes in the range
414 0x00..0x1F and 0x7F, except for the
415 following 5 codes. */
416 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
417 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
418 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
419 ISO_escape
, /* ISO_CODE_SO (0x1B) */
420 ISO_control_1
, /* Control codes in the range
421 0x80..0x9F, except for the
422 following 3 codes. */
423 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
424 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
425 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
426 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
427 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
428 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
429 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
432 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
433 `iso-flags' attribute of an iso2022 coding system. */
435 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
436 instead of the correct short-form sequence (e.g. ESC $ A). */
437 #define CODING_ISO_FLAG_LONG_FORM 0x0001
439 /* If set, reset graphic planes and registers at end-of-line to the
441 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
443 /* If set, reset graphic planes and registers before any control
444 characters to the initial state. */
445 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
447 /* If set, encode by 7-bit environment. */
448 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
450 /* If set, use locking-shift function. */
451 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
453 /* If set, use single-shift function. Overwrite
454 CODING_ISO_FLAG_LOCKING_SHIFT. */
455 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
457 /* If set, use designation escape sequence. */
458 #define CODING_ISO_FLAG_DESIGNATION 0x0040
460 /* If set, produce revision number sequence. */
461 #define CODING_ISO_FLAG_REVISION 0x0080
463 /* If set, produce ISO6429's direction specifying sequence. */
464 #define CODING_ISO_FLAG_DIRECTION 0x0100
466 /* If set, assume designation states are reset at beginning of line on
468 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
470 /* If set, designation sequence should be placed at beginning of line
472 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
474 /* If set, do not encode unsafe characters on output. */
475 #define CODING_ISO_FLAG_SAFE 0x0800
477 /* If set, extra latin codes (128..159) are accepted as a valid code
479 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
481 #define CODING_ISO_FLAG_COMPOSITION 0x2000
483 /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */
485 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
487 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
489 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
491 /* A character to be produced on output if encoding of the original
492 character is prohibited by CODING_ISO_FLAG_SAFE. */
493 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
496 #define CODING_UTF_8_BOM(coding) \
497 ((coding)->spec.utf_8_bom)
500 #define CODING_UTF_16_BOM(coding) \
501 ((coding)->spec.utf_16.bom)
503 #define CODING_UTF_16_ENDIAN(coding) \
504 ((coding)->spec.utf_16.endian)
506 #define CODING_UTF_16_SURROGATE(coding) \
507 ((coding)->spec.utf_16.surrogate)
511 #define CODING_CCL_DECODER(coding) \
512 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
513 #define CODING_CCL_ENCODER(coding) \
514 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
515 #define CODING_CCL_VALIDS(coding) \
516 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
518 /* Index for each coding category in `coding_categories' */
522 coding_category_iso_7
,
523 coding_category_iso_7_tight
,
524 coding_category_iso_8_1
,
525 coding_category_iso_8_2
,
526 coding_category_iso_7_else
,
527 coding_category_iso_8_else
,
528 coding_category_utf_8_auto
,
529 coding_category_utf_8_nosig
,
530 coding_category_utf_8_sig
,
531 coding_category_utf_16_auto
,
532 coding_category_utf_16_be
,
533 coding_category_utf_16_le
,
534 coding_category_utf_16_be_nosig
,
535 coding_category_utf_16_le_nosig
,
536 coding_category_charset
,
537 coding_category_sjis
,
538 coding_category_big5
,
540 coding_category_emacs_mule
,
541 /* All above are targets of code detection. */
542 coding_category_raw_text
,
543 coding_category_undecided
,
547 /* Definitions of flag bits used in detect_coding_XXXX. */
548 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
549 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
550 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
551 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
552 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
553 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
554 #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto)
555 #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig)
556 #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig)
557 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
558 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
559 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
560 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
561 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
562 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
563 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
564 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
565 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
566 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
567 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
569 /* This value is returned if detect_coding_mask () find nothing other
570 than ASCII characters. */
571 #define CATEGORY_MASK_ANY \
572 (CATEGORY_MASK_ISO_7 \
573 | CATEGORY_MASK_ISO_7_TIGHT \
574 | CATEGORY_MASK_ISO_8_1 \
575 | CATEGORY_MASK_ISO_8_2 \
576 | CATEGORY_MASK_ISO_7_ELSE \
577 | CATEGORY_MASK_ISO_8_ELSE \
578 | CATEGORY_MASK_UTF_8_AUTO \
579 | CATEGORY_MASK_UTF_8_NOSIG \
580 | CATEGORY_MASK_UTF_8_SIG \
581 | CATEGORY_MASK_UTF_16_AUTO \
582 | CATEGORY_MASK_UTF_16_BE \
583 | CATEGORY_MASK_UTF_16_LE \
584 | CATEGORY_MASK_UTF_16_BE_NOSIG \
585 | CATEGORY_MASK_UTF_16_LE_NOSIG \
586 | CATEGORY_MASK_CHARSET \
587 | CATEGORY_MASK_SJIS \
588 | CATEGORY_MASK_BIG5 \
589 | CATEGORY_MASK_CCL \
590 | CATEGORY_MASK_EMACS_MULE)
593 #define CATEGORY_MASK_ISO_7BIT \
594 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
596 #define CATEGORY_MASK_ISO_8BIT \
597 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
599 #define CATEGORY_MASK_ISO_ELSE \
600 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
602 #define CATEGORY_MASK_ISO_ESCAPE \
603 (CATEGORY_MASK_ISO_7 \
604 | CATEGORY_MASK_ISO_7_TIGHT \
605 | CATEGORY_MASK_ISO_7_ELSE \
606 | CATEGORY_MASK_ISO_8_ELSE)
608 #define CATEGORY_MASK_ISO \
609 ( CATEGORY_MASK_ISO_7BIT \
610 | CATEGORY_MASK_ISO_8BIT \
611 | CATEGORY_MASK_ISO_ELSE)
613 #define CATEGORY_MASK_UTF_16 \
614 (CATEGORY_MASK_UTF_16_AUTO \
615 | CATEGORY_MASK_UTF_16_BE \
616 | CATEGORY_MASK_UTF_16_LE \
617 | CATEGORY_MASK_UTF_16_BE_NOSIG \
618 | CATEGORY_MASK_UTF_16_LE_NOSIG)
620 #define CATEGORY_MASK_UTF_8 \
621 (CATEGORY_MASK_UTF_8_AUTO \
622 | CATEGORY_MASK_UTF_8_NOSIG \
623 | CATEGORY_MASK_UTF_8_SIG)
625 /* Table of coding categories (Lisp symbols). This variable is for
626 internal use only. */
627 static Lisp_Object Vcoding_category_table
;
629 /* Table of coding-categories ordered by priority. */
630 static enum coding_category coding_priorities
[coding_category_max
];
632 /* Nth element is a coding context for the coding system bound to the
633 Nth coding category. */
634 static struct coding_system coding_categories
[coding_category_max
];
636 /*** Commonly used macros and functions ***/
639 #define min(a, b) ((a) < (b) ? (a) : (b))
642 #define max(a, b) ((a) > (b) ? (a) : (b))
645 #define CODING_GET_INFO(coding, attrs, charset_list) \
647 (attrs) = CODING_ID_ATTRS ((coding)->id); \
648 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
652 /* Safely get one byte from the source text pointed by SRC which ends
653 at SRC_END, and set C to that byte. If there are not enough bytes
654 in the source, it jumps to 'no_more_source'. If MULTIBYTEP,
655 and a multibyte character is found at SRC, set C to the
656 negative value of the character code. The caller should declare
657 and set these variables appropriately in advance:
658 src, src_end, multibytep */
660 #define ONE_MORE_BYTE(c) \
662 if (src == src_end) \
664 if (src_base < src) \
665 record_conversion_result \
666 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
667 goto no_more_source; \
670 if (multibytep && (c & 0x80)) \
672 if ((c & 0xFE) == 0xC0) \
673 c = ((c & 1) << 6) | *src++; \
677 c = - string_char (src, &src, NULL); \
678 record_conversion_result \
679 (coding, CODING_RESULT_INVALID_SRC); \
685 /* Safely get two bytes from the source text pointed by SRC which ends
686 at SRC_END, and set C1 and C2 to those bytes while skipping the
687 heading multibyte characters. If there are not enough bytes in the
688 source, it jumps to 'no_more_source'. If MULTIBYTEP and
689 a multibyte character is found for C2, set C2 to the negative value
690 of the character code. The caller should declare and set these
691 variables appropriately in advance:
692 src, src_end, multibytep
693 It is intended that this macro is used in detect_coding_utf_16. */
695 #define TWO_MORE_BYTES(c1, c2) \
698 if (src == src_end) \
699 goto no_more_source; \
701 if (multibytep && (c1 & 0x80)) \
703 if ((c1 & 0xFE) == 0xC0) \
704 c1 = ((c1 & 1) << 6) | *src++; \
707 src += BYTES_BY_CHAR_HEAD (c1) - 1; \
712 if (src == src_end) \
713 goto no_more_source; \
715 if (multibytep && (c2 & 0x80)) \
717 if ((c2 & 0xFE) == 0xC0) \
718 c2 = ((c2 & 1) << 6) | *src++; \
725 /* Store a byte C in the place pointed by DST and increment DST to the
726 next free point, and increment PRODUCED_CHARS. The caller should
727 assure that C is 0..127, and declare and set the variable `dst'
728 appropriately in advance.
732 #define EMIT_ONE_ASCII_BYTE(c) \
739 /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */
741 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
743 produced_chars += 2; \
744 *dst++ = (c1), *dst++ = (c2); \
748 /* Store a byte C in the place pointed by DST and increment DST to the
749 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP,
750 store in an appropriate multibyte form. The caller should
751 declare and set the variables `dst' and `multibytep' appropriately
754 #define EMIT_ONE_BYTE(c) \
761 ch = BYTE8_TO_CHAR (ch); \
762 CHAR_STRING_ADVANCE (ch, dst); \
769 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
771 #define EMIT_TWO_BYTES(c1, c2) \
773 produced_chars += 2; \
780 ch = BYTE8_TO_CHAR (ch); \
781 CHAR_STRING_ADVANCE (ch, dst); \
784 ch = BYTE8_TO_CHAR (ch); \
785 CHAR_STRING_ADVANCE (ch, dst); \
795 #define EMIT_THREE_BYTES(c1, c2, c3) \
797 EMIT_ONE_BYTE (c1); \
798 EMIT_TWO_BYTES (c2, c3); \
802 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
804 EMIT_TWO_BYTES (c1, c2); \
805 EMIT_TWO_BYTES (c3, c4); \
810 record_conversion_result (struct coding_system
*coding
,
811 enum coding_result_code result
)
813 coding
->result
= result
;
816 case CODING_RESULT_INSUFFICIENT_SRC
:
817 Vlast_code_conversion_error
= Qinsufficient_source
;
819 case CODING_RESULT_INCONSISTENT_EOL
:
820 Vlast_code_conversion_error
= Qinconsistent_eol
;
822 case CODING_RESULT_INVALID_SRC
:
823 Vlast_code_conversion_error
= Qinvalid_source
;
825 case CODING_RESULT_INTERRUPT
:
826 Vlast_code_conversion_error
= Qinterrupted
;
828 case CODING_RESULT_INSUFFICIENT_MEM
:
829 Vlast_code_conversion_error
= Qinsufficient_memory
;
831 case CODING_RESULT_INSUFFICIENT_DST
:
832 /* Don't record this error in Vlast_code_conversion_error
833 because it happens just temporarily and is resolved when the
834 whole conversion is finished. */
836 case CODING_RESULT_SUCCESS
:
839 Vlast_code_conversion_error
= intern ("Unknown error");
843 /* These wrapper macros are used to preserve validity of pointers into
844 buffer text across calls to decode_char, encode_char, etc, which
845 could cause relocation of buffers if it loads a charset map,
846 because loading a charset map allocates large structures. */
848 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
852 charset_map_loaded = 0; \
853 c = DECODE_CHAR (charset, code); \
854 if (charset_map_loaded \
855 && (offset = coding_change_source (coding))) \
858 src_base += offset; \
863 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
867 charset_map_loaded = 0; \
868 code = ENCODE_CHAR (charset, c); \
869 if (charset_map_loaded \
870 && (offset = coding_change_destination (coding))) \
877 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
881 charset_map_loaded = 0; \
882 charset = char_charset (c, charset_list, code_return); \
883 if (charset_map_loaded \
884 && (offset = coding_change_destination (coding))) \
891 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
895 charset_map_loaded = 0; \
896 result = CHAR_CHARSET_P (c, charset); \
897 if (charset_map_loaded \
898 && (offset = coding_change_destination (coding))) \
906 /* If there are at least BYTES length of room at dst, allocate memory
907 for coding->destination and update dst and dst_end. We don't have
908 to take care of coding->source which will be relocated. It is
909 handled by calling coding_set_source in encode_coding. */
911 #define ASSURE_DESTINATION(bytes) \
913 if (dst + (bytes) >= dst_end) \
915 ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \
917 dst = alloc_destination (coding, more_bytes, dst); \
918 dst_end = coding->destination + coding->dst_bytes; \
923 /* Store multibyte form of the character C in P, and advance P to the
924 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
925 never calls MAYBE_UNIFY_CHAR. */
927 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
929 if ((c) <= MAX_1_BYTE_CHAR) \
931 else if ((c) <= MAX_2_BYTE_CHAR) \
932 *(p)++ = (0xC0 | ((c) >> 6)), \
933 *(p)++ = (0x80 | ((c) & 0x3F)); \
934 else if ((c) <= MAX_3_BYTE_CHAR) \
935 *(p)++ = (0xE0 | ((c) >> 12)), \
936 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
937 *(p)++ = (0x80 | ((c) & 0x3F)); \
938 else if ((c) <= MAX_4_BYTE_CHAR) \
939 *(p)++ = (0xF0 | (c >> 18)), \
940 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
941 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
942 *(p)++ = (0x80 | (c & 0x3F)); \
943 else if ((c) <= MAX_5_BYTE_CHAR) \
945 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
946 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
947 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
948 *(p)++ = (0x80 | (c & 0x3F)); \
950 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
954 /* Return the character code of character whose multibyte form is at
955 P, and advance P to the end of the multibyte form. This is like
956 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
958 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
961 : ! ((p)[0] & 0x20) \
963 ((((p)[-2] & 0x1F) << 6) \
965 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
966 : ! ((p)[0] & 0x10) \
968 ((((p)[-3] & 0x0F) << 12) \
969 | (((p)[-2] & 0x3F) << 6) \
970 | ((p)[-1] & 0x3F))) \
971 : ! ((p)[0] & 0x08) \
973 ((((p)[-4] & 0xF) << 18) \
974 | (((p)[-3] & 0x3F) << 12) \
975 | (((p)[-2] & 0x3F) << 6) \
976 | ((p)[-1] & 0x3F))) \
978 ((((p)[-4] & 0x3F) << 18) \
979 | (((p)[-3] & 0x3F) << 12) \
980 | (((p)[-2] & 0x3F) << 6) \
981 | ((p)[-1] & 0x3F))))
984 /* Set coding->source from coding->src_object. */
987 coding_set_source (struct coding_system
*coding
)
989 if (BUFFERP (coding
->src_object
))
991 struct buffer
*buf
= XBUFFER (coding
->src_object
);
993 if (coding
->src_pos
< 0)
994 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
996 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
998 else if (STRINGP (coding
->src_object
))
1000 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1004 /* Otherwise, the source is C string and is never relocated
1005 automatically. Thus we don't have to update anything. */
1010 /* Set coding->source from coding->src_object, and return how many
1011 bytes coding->source was changed. */
1014 coding_change_source (struct coding_system
*coding
)
1016 const unsigned char *orig
= coding
->source
;
1017 coding_set_source (coding
);
1018 return coding
->source
- orig
;
1022 /* Set coding->destination from coding->dst_object. */
1025 coding_set_destination (struct coding_system
*coding
)
1027 if (BUFFERP (coding
->dst_object
))
1029 if (BUFFERP (coding
->src_object
) && coding
->src_pos
< 0)
1031 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1032 coding
->dst_bytes
= (GAP_END_ADDR
1033 - (coding
->src_bytes
- coding
->consumed
)
1034 - coding
->destination
);
1038 /* We are sure that coding->dst_pos_byte is before the gap
1040 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1041 + coding
->dst_pos_byte
- BEG_BYTE
);
1042 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1043 - coding
->destination
);
1048 /* Otherwise, the destination is C string and is never relocated
1049 automatically. Thus we don't have to update anything. */
1054 /* Set coding->destination from coding->dst_object, and return how
1055 many bytes coding->destination was changed. */
1058 coding_change_destination (struct coding_system
*coding
)
1060 const unsigned char *orig
= coding
->destination
;
1061 coding_set_destination (coding
);
1062 return coding
->destination
- orig
;
1067 coding_alloc_by_realloc (struct coding_system
*coding
, ptrdiff_t bytes
)
1069 if (STRING_BYTES_BOUND
- coding
->dst_bytes
< bytes
)
1071 coding
->destination
= xrealloc (coding
->destination
,
1072 coding
->dst_bytes
+ bytes
);
1073 coding
->dst_bytes
+= bytes
;
1077 coding_alloc_by_making_gap (struct coding_system
*coding
,
1078 ptrdiff_t gap_head_used
, ptrdiff_t 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 ptrdiff_t 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
, ptrdiff_t nbytes
,
1110 ptrdiff_t 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 Return true if a text is encoded in UTF-8. */
1192 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1193 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1194 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1195 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1196 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1197 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1199 #define UTF_8_BOM_1 0xEF
1200 #define UTF_8_BOM_2 0xBB
1201 #define UTF_8_BOM_3 0xBF
1204 detect_coding_utf_8 (struct coding_system
*coding
,
1205 struct coding_detection_info
*detect_info
)
1207 const unsigned char *src
= coding
->source
, *src_base
;
1208 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1209 bool multibytep
= coding
->src_multibyte
;
1210 ptrdiff_t consumed_chars
= 0;
1214 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1215 /* A coding system of this category is always ASCII compatible. */
1216 src
+= coding
->head_ascii
;
1220 int c
, c1
, c2
, c3
, c4
;
1224 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1227 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1229 if (UTF_8_2_OCTET_LEADING_P (c
))
1235 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1237 if (UTF_8_3_OCTET_LEADING_P (c
))
1240 if (src_base
== coding
->source
1241 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1246 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1248 if (UTF_8_4_OCTET_LEADING_P (c
))
1254 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1256 if (UTF_8_5_OCTET_LEADING_P (c
))
1263 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1267 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1269 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1274 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1275 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1279 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1281 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1288 decode_coding_utf_8 (struct coding_system
*coding
)
1290 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1291 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1292 const unsigned char *src_base
;
1293 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1294 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1295 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1296 bool multibytep
= coding
->src_multibyte
;
1297 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1299 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1300 int byte_after_cr
= -1;
1302 if (bom
!= utf_without_bom
)
1308 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1313 if (! UTF_8_EXTRA_OCTET_P (c2
))
1318 if (! UTF_8_EXTRA_OCTET_P (c3
))
1322 if ((c1
!= UTF_8_BOM_1
)
1323 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1326 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1331 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1335 int c
, c1
, c2
, c3
, c4
, c5
;
1338 consumed_chars_base
= consumed_chars
;
1340 if (charbuf
>= charbuf_end
)
1342 if (byte_after_cr
>= 0)
1347 if (byte_after_cr
>= 0)
1348 c1
= byte_after_cr
, byte_after_cr
= -1;
1355 else if (UTF_8_1_OCTET_P (c1
))
1357 if (eol_dos
&& c1
== '\r')
1358 ONE_MORE_BYTE (byte_after_cr
);
1364 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1366 if (UTF_8_2_OCTET_LEADING_P (c1
))
1368 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1369 /* Reject overlong sequences here and below. Encoders
1370 producing them are incorrect, they can be misleading,
1371 and they mess up read/write invariance. */
1378 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1380 if (UTF_8_3_OCTET_LEADING_P (c1
))
1382 c
= (((c1
& 0xF) << 12)
1383 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1385 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1391 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1393 if (UTF_8_4_OCTET_LEADING_P (c1
))
1395 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1396 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1403 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1405 if (UTF_8_5_OCTET_LEADING_P (c1
))
1407 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1408 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1410 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1425 consumed_chars
= consumed_chars_base
;
1427 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1432 coding
->consumed_char
+= consumed_chars_base
;
1433 coding
->consumed
= src_base
- coding
->source
;
1434 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1439 encode_coding_utf_8 (struct coding_system
*coding
)
1441 bool multibytep
= coding
->dst_multibyte
;
1442 int *charbuf
= coding
->charbuf
;
1443 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1444 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1445 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1446 ptrdiff_t produced_chars
= 0;
1449 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1451 ASSURE_DESTINATION (3);
1452 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1453 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1458 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1460 while (charbuf
< charbuf_end
)
1462 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1464 ASSURE_DESTINATION (safe_room
);
1466 if (CHAR_BYTE8_P (c
))
1468 c
= CHAR_TO_BYTE8 (c
);
1473 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1474 for (p
= str
; p
< pend
; p
++)
1481 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1483 while (charbuf
< charbuf_end
)
1485 ASSURE_DESTINATION (safe_room
);
1487 if (CHAR_BYTE8_P (c
))
1488 *dst
++ = CHAR_TO_BYTE8 (c
);
1490 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1494 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1495 coding
->produced_char
+= produced_chars
;
1496 coding
->produced
= dst
- coding
->destination
;
1501 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1502 Return true if a text is encoded in one of UTF-16 based coding systems. */
1504 #define UTF_16_HIGH_SURROGATE_P(val) \
1505 (((val) & 0xFC00) == 0xD800)
1507 #define UTF_16_LOW_SURROGATE_P(val) \
1508 (((val) & 0xFC00) == 0xDC00)
1512 detect_coding_utf_16 (struct coding_system
*coding
,
1513 struct coding_detection_info
*detect_info
)
1515 const unsigned char *src
= coding
->source
;
1516 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1517 bool multibytep
= coding
->src_multibyte
;
1520 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1521 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1522 && (coding
->src_chars
& 1))
1524 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1528 TWO_MORE_BYTES (c1
, c2
);
1529 if ((c1
== 0xFF) && (c2
== 0xFE))
1531 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1532 | CATEGORY_MASK_UTF_16_AUTO
);
1533 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1534 | CATEGORY_MASK_UTF_16_BE_NOSIG
1535 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1537 else if ((c1
== 0xFE) && (c2
== 0xFF))
1539 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1540 | CATEGORY_MASK_UTF_16_AUTO
);
1541 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1542 | CATEGORY_MASK_UTF_16_BE_NOSIG
1543 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1547 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1552 /* We check the dispersion of Eth and Oth bytes where E is even and
1553 O is odd. If both are high, we assume binary data.*/
1554 unsigned char e
[256], o
[256];
1555 unsigned e_num
= 1, o_num
= 1;
1562 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1563 |CATEGORY_MASK_UTF_16_BE
1564 | CATEGORY_MASK_UTF_16_LE
);
1566 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1567 != CATEGORY_MASK_UTF_16
)
1569 TWO_MORE_BYTES (c1
, c2
);
1577 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1584 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1595 decode_coding_utf_16 (struct coding_system
*coding
)
1597 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1598 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1599 const unsigned char *src_base
;
1600 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1601 /* We may produces at most 3 chars in one loop. */
1602 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1603 ptrdiff_t consumed_chars
= 0, consumed_chars_base
= 0;
1604 bool multibytep
= coding
->src_multibyte
;
1605 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1606 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1607 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1609 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1610 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1612 if (bom
== utf_with_bom
)
1621 if (endian
== utf_16_big_endian
1622 ? c
!= 0xFEFF : c
!= 0xFFFE)
1624 /* The first two bytes are not BOM. Treat them as bytes
1625 for a normal character. */
1629 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1631 else if (bom
== utf_detect_bom
)
1633 /* We have already tried to detect BOM and failed in
1635 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1643 consumed_chars_base
= consumed_chars
;
1645 if (charbuf
>= charbuf_end
)
1647 if (byte_after_cr1
>= 0)
1652 if (byte_after_cr1
>= 0)
1653 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1661 if (byte_after_cr2
>= 0)
1662 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1667 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1671 c
= (endian
== utf_16_big_endian
1672 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1676 if (! UTF_16_LOW_SURROGATE_P (c
))
1678 if (endian
== utf_16_big_endian
)
1679 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1681 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1685 if (UTF_16_HIGH_SURROGATE_P (c
))
1686 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1692 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1693 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1694 *charbuf
++ = 0x10000 + c
;
1699 if (UTF_16_HIGH_SURROGATE_P (c
))
1700 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1703 if (eol_dos
&& c
== '\r')
1705 ONE_MORE_BYTE (byte_after_cr1
);
1706 ONE_MORE_BYTE (byte_after_cr2
);
1714 coding
->consumed_char
+= consumed_chars_base
;
1715 coding
->consumed
= src_base
- coding
->source
;
1716 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1720 encode_coding_utf_16 (struct coding_system
*coding
)
1722 bool multibytep
= coding
->dst_multibyte
;
1723 int *charbuf
= coding
->charbuf
;
1724 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1725 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1726 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1728 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1729 bool big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1730 ptrdiff_t produced_chars
= 0;
1733 if (bom
!= utf_without_bom
)
1735 ASSURE_DESTINATION (safe_room
);
1737 EMIT_TWO_BYTES (0xFE, 0xFF);
1739 EMIT_TWO_BYTES (0xFF, 0xFE);
1740 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1743 while (charbuf
< charbuf_end
)
1745 ASSURE_DESTINATION (safe_room
);
1747 if (c
> MAX_UNICODE_CHAR
)
1748 c
= coding
->default_char
;
1753 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1755 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1762 c1
= (c
>> 10) + 0xD800;
1763 c2
= (c
& 0x3FF) + 0xDC00;
1765 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1767 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1770 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1771 coding
->produced
= dst
- coding
->destination
;
1772 coding
->produced_char
+= produced_chars
;
1777 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1779 /* Emacs' internal format for representation of multiple character
1780 sets is a kind of multi-byte encoding, i.e. characters are
1781 represented by variable-length sequences of one-byte codes.
1783 ASCII characters and control characters (e.g. `tab', `newline') are
1784 represented by one-byte sequences which are their ASCII codes, in
1785 the range 0x00 through 0x7F.
1787 8-bit characters of the range 0x80..0x9F are represented by
1788 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1791 8-bit characters of the range 0xA0..0xFF are represented by
1792 one-byte sequences which are their 8-bit code.
1794 The other characters are represented by a sequence of `base
1795 leading-code', optional `extended leading-code', and one or two
1796 `position-code's. The length of the sequence is determined by the
1797 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1798 whereas extended leading-code and position-code take the range 0xA0
1799 through 0xFF. See `charset.h' for more details about leading-code
1802 --- CODE RANGE of Emacs' internal format ---
1806 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1807 eight-bit-graphic 0xA0..0xBF
1808 ELSE 0x81..0x9D + [0xA0..0xFF]+
1809 ---------------------------------------------
1811 As this is the internal character representation, the format is
1812 usually not used externally (i.e. in a file or in a data sent to a
1813 process). But, it is possible to have a text externally in this
1814 format (i.e. by encoding by the coding system `emacs-mule').
1816 In that case, a sequence of one-byte codes has a slightly different
1819 At first, all characters in eight-bit-control are represented by
1820 one-byte sequences which are their 8-bit code.
1822 Next, character composition data are represented by the byte
1823 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1825 METHOD is 0xF2 plus one of composition method (enum
1826 composition_method),
1828 BYTES is 0xA0 plus a byte length of this composition data,
1830 CHARS is 0xA0 plus a number of characters composed by this
1833 COMPONENTs are characters of multibyte form or composition
1834 rules encoded by two-byte of ASCII codes.
1836 In addition, for backward compatibility, the following formats are
1837 also recognized as composition data on decoding.
1840 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1843 MSEQ is a multibyte form but in these special format:
1844 ASCII: 0xA0 ASCII_CODE+0x80,
1845 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1846 RULE is a one byte code of the range 0xA0..0xF0 that
1847 represents a composition rule.
1850 char emacs_mule_bytes
[256];
1853 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1854 Return true if a text is encoded in 'emacs-mule'. */
1857 detect_coding_emacs_mule (struct coding_system
*coding
,
1858 struct coding_detection_info
*detect_info
)
1860 const unsigned char *src
= coding
->source
, *src_base
;
1861 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1862 bool multibytep
= coding
->src_multibyte
;
1863 ptrdiff_t consumed_chars
= 0;
1867 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1868 /* A coding system of this category is always ASCII compatible. */
1869 src
+= coding
->head_ascii
;
1879 /* Perhaps the start of composite character. We simply skip
1880 it because analyzing it is too heavy for detecting. But,
1881 at least, we check that the composite character
1882 constitutes of more than 4 bytes. */
1883 const unsigned char *src_start
;
1893 if (src
- src_start
<= 4)
1895 found
= CATEGORY_MASK_EMACS_MULE
;
1903 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1908 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1910 while (more_bytes
> 0)
1915 src
--; /* Unread the last byte. */
1920 if (more_bytes
!= 0)
1922 found
= CATEGORY_MASK_EMACS_MULE
;
1925 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1929 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1931 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1934 detect_info
->found
|= found
;
1939 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1940 character. If CMP_STATUS indicates that we must expect MSEQ or
1941 RULE described above, decode it and return the negative value of
1942 the decoded character or rule. If an invalid byte is found, return
1943 -1. If SRC is too short, return -2. */
1946 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1947 int *nbytes
, int *nchars
, int *id
,
1948 struct composition_status
*cmp_status
)
1950 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1951 const unsigned char *src_base
= src
;
1952 bool multibytep
= coding
->src_multibyte
;
1956 int consumed_chars
= 0;
1957 bool mseq_found
= 0;
1963 charset_ID
= emacs_mule_charset
[0];
1969 if (cmp_status
->state
!= COMPOSING_NO
1970 && cmp_status
->old_form
)
1972 if (cmp_status
->state
== COMPOSING_CHAR
)
1987 *nbytes
= src
- src_base
;
1988 *nchars
= consumed_chars
;
1996 switch (emacs_mule_bytes
[c
])
1999 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2008 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2009 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2012 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2021 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2026 code
= (c
& 0x7F) << 8;
2036 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2041 code
= (c
& 0x7F) << 8;
2050 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2056 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2057 CHARSET_FROM_ID (charset_ID
), code
, c
);
2061 *nbytes
= src
- src_base
;
2062 *nchars
= consumed_chars
;
2065 return (mseq_found
? -c
: c
);
2075 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2077 /* Handle these composition sequence ('|': the end of header elements,
2078 BYTES and CHARS >= 0xA0):
2080 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2081 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2082 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2086 (4) relative composition: 0x80 | MSEQ ... MSEQ
2087 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2089 When the starter 0x80 and the following header elements are found,
2090 this annotation header is produced.
2092 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2094 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2095 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2097 Then, upon reading the following elements, these codes are produced
2098 until the composition end is found:
2101 (2) ALT ... ALT CHAR ... CHAR
2102 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2104 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2106 When the composition end is found, LENGTH and NCHARS in the
2107 annotation header is updated as below:
2109 (1) LENGTH: unchanged, NCHARS: unchanged
2110 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2111 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2112 (4) LENGTH: unchanged, NCHARS: number of CHARs
2113 (5) LENGTH: unchanged, NCHARS: number of CHARs
2115 If an error is found while composing, the annotation header is
2116 changed to the original composition header (plus filler -1s) as
2119 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2120 (5) [ 0x80 0xFF -1 -1- -1 ]
2122 and the sequence [ -2 DECODED-RULE ] is changed to the original
2123 byte sequence as below:
2124 o the original byte sequence is B: [ B -1 ]
2125 o the original byte sequence is B1 B2: [ B1 B2 ]
2127 Most of the routines are implemented by macros because many
2128 variables and labels in the caller decode_coding_emacs_mule must be
2129 accessible, and they are usually called just once (thus doesn't
2130 increase the size of compiled object). */
2132 /* Decode a composition rule represented by C as a component of
2133 composition sequence of Emacs 20 style. Set RULE to the decoded
2136 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2141 if (c < 0 || c >= 81) \
2142 goto invalid_code; \
2143 gref = c / 9, nref = c % 9; \
2144 if (gref == 4) gref = 10; \
2145 if (nref == 4) nref = 10; \
2146 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2150 /* Decode a composition rule represented by C and the following byte
2151 at SRC as a component of composition sequence of Emacs 21 style.
2152 Set RULE to the decoded rule. */
2154 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2159 if (gref < 0 || gref >= 81) \
2160 goto invalid_code; \
2161 ONE_MORE_BYTE (c); \
2163 if (nref < 0 || nref >= 81) \
2164 goto invalid_code; \
2165 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2169 /* Start of Emacs 21 style format. The first three bytes at SRC are
2170 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2171 byte length of this composition information, CHARS is the number of
2172 characters composed by this composition. */
2174 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2176 enum composition_method method = c - 0xF2; \
2177 int nbytes, nchars; \
2179 ONE_MORE_BYTE (c); \
2181 goto invalid_code; \
2182 nbytes = c - 0xA0; \
2183 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2184 goto invalid_code; \
2185 ONE_MORE_BYTE (c); \
2186 nchars = c - 0xA0; \
2187 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2188 goto invalid_code; \
2189 cmp_status->old_form = 0; \
2190 cmp_status->method = method; \
2191 if (method == COMPOSITION_RELATIVE) \
2192 cmp_status->state = COMPOSING_CHAR; \
2194 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2195 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2196 cmp_status->nchars = nchars; \
2197 cmp_status->ncomps = nbytes - 4; \
2198 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2202 /* Start of Emacs 20 style format for relative composition. */
2204 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2206 cmp_status->old_form = 1; \
2207 cmp_status->method = COMPOSITION_RELATIVE; \
2208 cmp_status->state = COMPOSING_CHAR; \
2209 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2210 cmp_status->nchars = cmp_status->ncomps = 0; \
2211 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2215 /* Start of Emacs 20 style format for rule-base composition. */
2217 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2219 cmp_status->old_form = 1; \
2220 cmp_status->method = COMPOSITION_WITH_RULE; \
2221 cmp_status->state = COMPOSING_CHAR; \
2222 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2223 cmp_status->nchars = cmp_status->ncomps = 0; \
2224 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2228 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2230 const unsigned char *current_src = src; \
2232 ONE_MORE_BYTE (c); \
2234 goto invalid_code; \
2235 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2236 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2237 DECODE_EMACS_MULE_21_COMPOSITION (); \
2238 else if (c < 0xA0) \
2239 goto invalid_code; \
2240 else if (c < 0xC0) \
2242 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2243 /* Re-read C as a composition component. */ \
2244 src = current_src; \
2246 else if (c == 0xFF) \
2247 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2249 goto invalid_code; \
2252 #define EMACS_MULE_COMPOSITION_END() \
2254 int idx = - cmp_status->length; \
2256 if (cmp_status->old_form) \
2257 charbuf[idx + 2] = cmp_status->nchars; \
2258 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2259 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2260 cmp_status->state = COMPOSING_NO; \
2265 emacs_mule_finish_composition (int *charbuf
,
2266 struct composition_status
*cmp_status
)
2268 int idx
= - cmp_status
->length
;
2271 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2273 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2275 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2276 && cmp_status
->state
== COMPOSING_CHAR
)
2278 /* The last rule was invalid. */
2279 int rule
= charbuf
[-1] + 0xA0;
2281 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2288 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2290 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2292 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2293 charbuf
[idx
++] = -3;
2299 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2300 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2302 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2303 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2304 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2305 charbuf
[idx
++] = -1;
2309 cmp_status
->state
= COMPOSING_NO
;
2313 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2315 if (cmp_status->state != COMPOSING_NO) \
2316 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2321 decode_coding_emacs_mule (struct coding_system
*coding
)
2323 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2324 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2325 const unsigned char *src_base
;
2326 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2327 /* We may produce two annotations (charset and composition) in one
2328 loop and one more charset annotation at the end. */
2330 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2331 /* We can produce up to 2 characters in a loop. */
2333 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
2334 bool multibytep
= coding
->src_multibyte
;
2335 ptrdiff_t char_offset
= coding
->produced_char
;
2336 ptrdiff_t last_offset
= char_offset
;
2337 int last_id
= charset_ascii
;
2339 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2340 int byte_after_cr
= -1;
2341 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2343 if (cmp_status
->state
!= COMPOSING_NO
)
2347 if (charbuf_end
- charbuf
< cmp_status
->length
)
2349 for (i
= 0; i
< cmp_status
->length
; i
++)
2350 *charbuf
++ = cmp_status
->carryover
[i
];
2351 coding
->annotated
= 1;
2356 int c
, id
IF_LINT (= 0);
2359 consumed_chars_base
= consumed_chars
;
2361 if (charbuf
>= charbuf_end
)
2363 if (byte_after_cr
>= 0)
2368 if (byte_after_cr
>= 0)
2369 c
= byte_after_cr
, byte_after_cr
= -1;
2373 if (c
< 0 || c
== 0x80)
2375 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2382 DECODE_EMACS_MULE_COMPOSITION_START ();
2388 if (eol_dos
&& c
== '\r')
2389 ONE_MORE_BYTE (byte_after_cr
);
2391 if (cmp_status
->state
!= COMPOSING_NO
)
2393 if (cmp_status
->old_form
)
2394 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2395 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2396 cmp_status
->ncomps
--;
2401 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2402 /* emacs_mule_char can load a charset map from a file, which
2403 allocates a large structure and might cause buffer text
2404 to be relocated as result. Thus, we need to remember the
2405 original pointer to buffer text, and fix up all related
2406 pointers after the call. */
2407 const unsigned char *orig
= coding
->source
;
2410 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2412 offset
= coding
->source
- orig
;
2426 src
= src_base
+ nbytes
;
2427 consumed_chars
= consumed_chars_base
+ nchars
;
2428 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2429 cmp_status
->ncomps
-= nchars
;
2432 /* Now if C >= 0, we found a normally encoded character, if C <
2433 0, we found an old-style composition component character or
2436 if (cmp_status
->state
== COMPOSING_NO
)
2440 if (last_id
!= charset_ascii
)
2441 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2444 last_offset
= char_offset
;
2449 else if (cmp_status
->state
== COMPOSING_CHAR
)
2451 if (cmp_status
->old_form
)
2455 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2462 cmp_status
->nchars
++;
2463 cmp_status
->length
++;
2464 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2465 EMACS_MULE_COMPOSITION_END ();
2466 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2467 cmp_status
->state
= COMPOSING_RULE
;
2473 cmp_status
->length
++;
2474 cmp_status
->nchars
--;
2475 if (cmp_status
->nchars
== 0)
2476 EMACS_MULE_COMPOSITION_END ();
2479 else if (cmp_status
->state
== COMPOSING_RULE
)
2485 EMACS_MULE_COMPOSITION_END ();
2492 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2497 cmp_status
->length
+= 2;
2498 cmp_status
->state
= COMPOSING_CHAR
;
2501 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2504 cmp_status
->length
++;
2505 if (cmp_status
->ncomps
== 0)
2506 cmp_status
->state
= COMPOSING_CHAR
;
2507 else if (cmp_status
->ncomps
> 0)
2509 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2510 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2513 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2515 else /* COMPOSING_COMPONENT_RULE */
2519 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2524 cmp_status
->length
+= 2;
2525 cmp_status
->ncomps
--;
2526 if (cmp_status
->ncomps
> 0)
2527 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2529 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2534 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2536 consumed_chars
= consumed_chars_base
;
2538 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2544 if (cmp_status
->state
!= COMPOSING_NO
)
2546 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2547 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2552 charbuf
-= cmp_status
->length
;
2553 for (i
= 0; i
< cmp_status
->length
; i
++)
2554 cmp_status
->carryover
[i
] = charbuf
[i
];
2557 if (last_id
!= charset_ascii
)
2558 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2559 coding
->consumed_char
+= consumed_chars_base
;
2560 coding
->consumed
= src_base
- coding
->source
;
2561 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2565 #define EMACS_MULE_LEADING_CODES(id, codes) \
2568 codes[0] = id, codes[1] = 0; \
2569 else if (id < 0xE0) \
2570 codes[0] = 0x9A, codes[1] = id; \
2571 else if (id < 0xF0) \
2572 codes[0] = 0x9B, codes[1] = id; \
2573 else if (id < 0xF5) \
2574 codes[0] = 0x9C, codes[1] = id; \
2576 codes[0] = 0x9D, codes[1] = id; \
2581 encode_coding_emacs_mule (struct coding_system
*coding
)
2583 bool multibytep
= coding
->dst_multibyte
;
2584 int *charbuf
= coding
->charbuf
;
2585 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2586 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2587 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2589 ptrdiff_t produced_chars
= 0;
2590 Lisp_Object attrs
, charset_list
;
2592 int preferred_charset_id
= -1;
2594 CODING_GET_INFO (coding
, attrs
, charset_list
);
2595 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2597 charset_list
= Vemacs_mule_charset_list
;
2598 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2601 while (charbuf
< charbuf_end
)
2603 ASSURE_DESTINATION (safe_room
);
2608 /* Handle an annotation. */
2611 case CODING_ANNOTATE_COMPOSITION_MASK
:
2612 /* Not yet implemented. */
2614 case CODING_ANNOTATE_CHARSET_MASK
:
2615 preferred_charset_id
= charbuf
[3];
2616 if (preferred_charset_id
>= 0
2617 && NILP (Fmemq (make_number (preferred_charset_id
),
2619 preferred_charset_id
= -1;
2628 if (ASCII_CHAR_P (c
))
2629 EMIT_ONE_ASCII_BYTE (c
);
2630 else if (CHAR_BYTE8_P (c
))
2632 c
= CHAR_TO_BYTE8 (c
);
2637 struct charset
*charset
;
2641 unsigned char leading_codes
[2];
2643 if (preferred_charset_id
>= 0)
2647 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2648 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2650 code
= ENCODE_CHAR (charset
, c
);
2652 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2656 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2660 c
= coding
->default_char
;
2661 if (ASCII_CHAR_P (c
))
2663 EMIT_ONE_ASCII_BYTE (c
);
2666 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2669 dimension
= CHARSET_DIMENSION (charset
);
2670 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2671 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2672 EMIT_ONE_BYTE (leading_codes
[0]);
2673 if (leading_codes
[1])
2674 EMIT_ONE_BYTE (leading_codes
[1]);
2676 EMIT_ONE_BYTE (code
| 0x80);
2680 EMIT_ONE_BYTE (code
>> 8);
2681 EMIT_ONE_BYTE (code
& 0xFF);
2685 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2686 coding
->produced_char
+= produced_chars
;
2687 coding
->produced
= dst
- coding
->destination
;
2692 /*** 7. ISO2022 handlers ***/
2694 /* The following note describes the coding system ISO2022 briefly.
2695 Since the intention of this note is to help understand the
2696 functions in this file, some parts are NOT ACCURATE or are OVERLY
2697 SIMPLIFIED. For thorough understanding, please refer to the
2698 original document of ISO2022. This is equivalent to the standard
2699 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2701 ISO2022 provides many mechanisms to encode several character sets
2702 in 7-bit and 8-bit environments. For 7-bit environments, all text
2703 is encoded using bytes less than 128. This may make the encoded
2704 text a little bit longer, but the text passes more easily through
2705 several types of gateway, some of which strip off the MSB (Most
2708 There are two kinds of character sets: control character sets and
2709 graphic character sets. The former contain control characters such
2710 as `newline' and `escape' to provide control functions (control
2711 functions are also provided by escape sequences). The latter
2712 contain graphic characters such as 'A' and '-'. Emacs recognizes
2713 two control character sets and many graphic character sets.
2715 Graphic character sets are classified into one of the following
2716 four classes, according to the number of bytes (DIMENSION) and
2717 number of characters in one dimension (CHARS) of the set:
2718 - DIMENSION1_CHARS94
2719 - DIMENSION1_CHARS96
2720 - DIMENSION2_CHARS94
2721 - DIMENSION2_CHARS96
2723 In addition, each character set is assigned an identification tag,
2724 unique for each set, called the "final character" (denoted as <F>
2725 hereafter). The <F> of each character set is decided by ECMA(*)
2726 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2727 (0x30..0x3F are for private use only).
2729 Note (*): ECMA = European Computer Manufacturers Association
2731 Here are examples of graphic character sets [NAME(<F>)]:
2732 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2733 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2734 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2735 o DIMENSION2_CHARS96 -- none for the moment
2737 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2738 C0 [0x00..0x1F] -- control character plane 0
2739 GL [0x20..0x7F] -- graphic character plane 0
2740 C1 [0x80..0x9F] -- control character plane 1
2741 GR [0xA0..0xFF] -- graphic character plane 1
2743 A control character set is directly designated and invoked to C0 or
2744 C1 by an escape sequence. The most common case is that:
2745 - ISO646's control character set is designated/invoked to C0, and
2746 - ISO6429's control character set is designated/invoked to C1,
2747 and usually these designations/invocations are omitted in encoded
2748 text. In a 7-bit environment, only C0 can be used, and a control
2749 character for C1 is encoded by an appropriate escape sequence to
2750 fit into the environment. All control characters for C1 are
2751 defined to have corresponding escape sequences.
2753 A graphic character set is at first designated to one of four
2754 graphic registers (G0 through G3), then these graphic registers are
2755 invoked to GL or GR. These designations and invocations can be
2756 done independently. The most common case is that G0 is invoked to
2757 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2758 these invocations and designations are omitted in encoded text.
2759 In a 7-bit environment, only GL can be used.
2761 When a graphic character set of CHARS94 is invoked to GL, codes
2762 0x20 and 0x7F of the GL area work as control characters SPACE and
2763 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2766 There are two ways of invocation: locking-shift and single-shift.
2767 With locking-shift, the invocation lasts until the next different
2768 invocation, whereas with single-shift, the invocation affects the
2769 following character only and doesn't affect the locking-shift
2770 state. Invocations are done by the following control characters or
2773 ----------------------------------------------------------------------
2774 abbrev function cntrl escape seq description
2775 ----------------------------------------------------------------------
2776 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2777 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2778 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2779 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2780 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2781 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2782 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2783 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2784 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2785 ----------------------------------------------------------------------
2786 (*) These are not used by any known coding system.
2788 Control characters for these functions are defined by macros
2789 ISO_CODE_XXX in `coding.h'.
2791 Designations are done by the following escape sequences:
2792 ----------------------------------------------------------------------
2793 escape sequence description
2794 ----------------------------------------------------------------------
2795 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2796 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2797 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2798 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2799 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2800 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2801 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2802 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2803 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2804 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2805 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2806 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2807 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2808 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2809 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2810 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2811 ----------------------------------------------------------------------
2813 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2814 of dimension 1, chars 94, and final character <F>, etc...
2816 Note (*): Although these designations are not allowed in ISO2022,
2817 Emacs accepts them on decoding, and produces them on encoding
2818 CHARS96 character sets in a coding system which is characterized as
2819 7-bit environment, non-locking-shift, and non-single-shift.
2821 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2822 '(' must be omitted. We refer to this as "short-form" hereafter.
2824 Now you may notice that there are a lot of ways of encoding the
2825 same multilingual text in ISO2022. Actually, there exist many
2826 coding systems such as Compound Text (used in X11's inter client
2827 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2828 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2829 localized platforms), and all of these are variants of ISO2022.
2831 In addition to the above, Emacs handles two more kinds of escape
2832 sequences: ISO6429's direction specification and Emacs' private
2833 sequence for specifying character composition.
2835 ISO6429's direction specification takes the following form:
2836 o CSI ']' -- end of the current direction
2837 o CSI '0' ']' -- end of the current direction
2838 o CSI '1' ']' -- start of left-to-right text
2839 o CSI '2' ']' -- start of right-to-left text
2840 The control character CSI (0x9B: control sequence introducer) is
2841 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2843 Character composition specification takes the following form:
2844 o ESC '0' -- start relative composition
2845 o ESC '1' -- end composition
2846 o ESC '2' -- start rule-base composition (*)
2847 o ESC '3' -- start relative composition with alternate chars (**)
2848 o ESC '4' -- start rule-base composition with alternate chars (**)
2849 Since these are not standard escape sequences of any ISO standard,
2850 the use of them with these meanings is restricted to Emacs only.
2852 (*) This form is used only in Emacs 20.7 and older versions,
2853 but newer versions can safely decode it.
2854 (**) This form is used only in Emacs 21.1 and newer versions,
2855 and older versions can't decode it.
2857 Here's a list of example usages of these composition escape
2858 sequences (categorized by `enum composition_method').
2860 COMPOSITION_RELATIVE:
2861 ESC 0 CHAR [ CHAR ] ESC 1
2862 COMPOSITION_WITH_RULE:
2863 ESC 2 CHAR [ RULE CHAR ] ESC 1
2864 COMPOSITION_WITH_ALTCHARS:
2865 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2866 COMPOSITION_WITH_RULE_ALTCHARS:
2867 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2869 static enum iso_code_class_type iso_code_class
[256];
2871 #define SAFE_CHARSET_P(coding, id) \
2872 ((id) <= (coding)->max_charset_id \
2873 && (coding)->safe_charsets[id] != 255)
2876 setup_iso_safe_charsets (Lisp_Object attrs
)
2878 Lisp_Object charset_list
, safe_charsets
;
2879 Lisp_Object request
;
2880 Lisp_Object reg_usage
;
2882 EMACS_INT reg94
, reg96
;
2883 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2886 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2887 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2888 && ! EQ (charset_list
, Viso_2022_charset_list
))
2890 charset_list
= Viso_2022_charset_list
;
2891 ASET (attrs
, coding_attr_charset_list
, charset_list
);
2892 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2895 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2899 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2901 int id
= XINT (XCAR (tail
));
2902 if (max_charset_id
< id
)
2903 max_charset_id
= id
;
2906 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2907 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2908 request
= AREF (attrs
, coding_attr_iso_request
);
2909 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2910 reg94
= XINT (XCAR (reg_usage
));
2911 reg96
= XINT (XCDR (reg_usage
));
2913 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2917 struct charset
*charset
;
2920 charset
= CHARSET_FROM_ID (XINT (id
));
2921 reg
= Fcdr (Fassq (id
, request
));
2923 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2924 else if (charset
->iso_chars_96
)
2927 SSET (safe_charsets
, XINT (id
), reg96
);
2932 SSET (safe_charsets
, XINT (id
), reg94
);
2935 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2939 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2940 Return true if a text is encoded in one of ISO-2022 based coding
2944 detect_coding_iso_2022 (struct coding_system
*coding
,
2945 struct coding_detection_info
*detect_info
)
2947 const unsigned char *src
= coding
->source
, *src_base
= src
;
2948 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2949 bool multibytep
= coding
->src_multibyte
;
2950 bool single_shifting
= 0;
2953 ptrdiff_t consumed_chars
= 0;
2957 int composition_count
= -1;
2959 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2961 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2963 struct coding_system
*this = &(coding_categories
[i
]);
2964 Lisp_Object attrs
, val
;
2968 attrs
= CODING_ID_ATTRS (this->id
);
2969 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2970 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2971 setup_iso_safe_charsets (attrs
);
2972 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2973 this->max_charset_id
= SCHARS (val
) - 1;
2974 this->safe_charsets
= SDATA (val
);
2977 /* A coding system of this category is always ASCII compatible. */
2978 src
+= coding
->head_ascii
;
2980 while (rejected
!= CATEGORY_MASK_ISO
)
2987 if (inhibit_iso_escape_detection
)
2989 single_shifting
= 0;
2991 if (c
== 'N' || c
== 'O')
2993 /* ESC <Fe> for SS2 or SS3. */
2994 single_shifting
= 1;
2995 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2999 /* End of composition. */
3000 if (composition_count
< 0
3001 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3004 composition_count
= -1;
3005 found
|= CATEGORY_MASK_ISO
;
3007 else if (c
>= '0' && c
<= '4')
3009 /* ESC <Fp> for start/end composition. */
3010 composition_count
= 0;
3014 if (c
>= '(' && c
<= '/')
3016 /* Designation sequence for a charset of dimension 1. */
3018 if (c1
< ' ' || c1
>= 0x80
3019 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3020 /* Invalid designation sequence. Just ignore. */
3025 /* Designation sequence for a charset of dimension 2. */
3027 if (c
>= '@' && c
<= 'B')
3028 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3029 id
= iso_charset_table
[1][0][c
];
3030 else if (c
>= '(' && c
<= '/')
3033 if (c1
< ' ' || c1
>= 0x80
3034 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3035 /* Invalid designation sequence. Just ignore. */
3039 /* Invalid designation sequence. Just ignore it. */
3044 /* Invalid escape sequence. Just ignore it. */
3048 /* We found a valid designation sequence for CHARSET. */
3049 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3050 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3052 found
|= CATEGORY_MASK_ISO_7
;
3054 rejected
|= CATEGORY_MASK_ISO_7
;
3055 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3057 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3059 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3060 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3062 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3064 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3065 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3067 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3069 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3075 /* Locking shift out/in. */
3076 if (inhibit_iso_escape_detection
)
3078 single_shifting
= 0;
3079 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3083 /* Control sequence introducer. */
3084 single_shifting
= 0;
3085 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3086 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3087 goto check_extra_latin
;
3092 if (inhibit_iso_escape_detection
)
3094 single_shifting
= 0;
3095 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3096 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3097 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3099 found
|= CATEGORY_MASK_ISO_8_1
;
3100 single_shifting
= 1;
3102 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3103 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3105 found
|= CATEGORY_MASK_ISO_8_2
;
3106 single_shifting
= 1;
3108 if (single_shifting
)
3111 if (! VECTORP (Vlatin_extra_code_table
)
3112 || NILP (AREF (Vlatin_extra_code_table
, c
)))
3114 rejected
= CATEGORY_MASK_ISO
;
3117 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3118 & CODING_ISO_FLAG_LATIN_EXTRA
)
3119 found
|= CATEGORY_MASK_ISO_8_1
;
3121 rejected
|= CATEGORY_MASK_ISO_8_1
;
3122 rejected
|= CATEGORY_MASK_ISO_8_2
;
3130 if (composition_count
>= 0)
3131 composition_count
++;
3132 single_shifting
= 0;
3137 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3138 found
|= CATEGORY_MASK_ISO_8_1
;
3139 /* Check the length of succeeding codes of the range
3140 0xA0..0FF. If the byte length is even, we include
3141 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3142 only when we are not single shifting. */
3143 if (! single_shifting
3144 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3147 while (src
< src_end
)
3159 if (len
& 1 && src
< src_end
)
3161 rejected
|= CATEGORY_MASK_ISO_8_2
;
3162 if (composition_count
>= 0)
3163 composition_count
+= len
;
3167 found
|= CATEGORY_MASK_ISO_8_2
;
3168 if (composition_count
>= 0)
3169 composition_count
+= len
/ 2;
3176 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3180 detect_info
->rejected
|= rejected
;
3181 detect_info
->found
|= (found
& ~rejected
);
3186 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3187 escape sequence should be kept. */
3188 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3192 if (final < '0' || final >= 128 \
3193 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3194 || !SAFE_CHARSET_P (coding, id)) \
3196 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3200 prev = CODING_ISO_DESIGNATION (coding, reg); \
3201 if (id == charset_jisx0201_roman) \
3203 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3204 id = charset_ascii; \
3206 else if (id == charset_jisx0208_1978) \
3208 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3209 id = charset_jisx0208; \
3211 CODING_ISO_DESIGNATION (coding, reg) = id; \
3212 /* If there was an invalid designation to REG previously, and this \
3213 designation is ASCII to REG, we should keep this designation \
3215 if (prev == -2 && id == charset_ascii) \
3220 /* Handle these composition sequence (ALT: alternate char):
3222 (1) relative composition: ESC 0 CHAR ... ESC 1
3223 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3224 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3225 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3227 When the start sequence (ESC 0/2/3/4) is found, this annotation
3230 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3232 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3233 produced until the end sequence (ESC 1) is found:
3236 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3237 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3238 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3240 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3241 annotation header is updated as below:
3243 (1) LENGTH: unchanged, NCHARS: number of CHARs
3244 (2) LENGTH: unchanged, NCHARS: number of CHARs
3245 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3246 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3248 If an error is found while composing, the annotation header is
3251 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3253 and the sequence [ -2 DECODED-RULE ] is changed to the original
3254 byte sequence as below:
3255 o the original byte sequence is B: [ B -1 ]
3256 o the original byte sequence is B1 B2: [ B1 B2 ]
3257 and the sequence [ -1 -1 ] is changed to the original byte
3262 /* Decode a composition rule C1 and maybe one more byte from the
3263 source, and set RULE to the encoded composition rule. If the rule
3264 is invalid, goto invalid_code. */
3266 #define DECODE_COMPOSITION_RULE(rule) \
3270 goto invalid_code; \
3271 if (rule < 81) /* old format (before ver.21) */ \
3273 int gref = (rule) / 9; \
3274 int nref = (rule) % 9; \
3275 if (gref == 4) gref = 10; \
3276 if (nref == 4) nref = 10; \
3277 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3279 else /* new format (after ver.21) */ \
3283 ONE_MORE_BYTE (b); \
3284 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3285 goto invalid_code; \
3286 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3287 rule += 0x100; /* Distinguish it from the old format. */ \
3291 #define ENCODE_COMPOSITION_RULE(rule) \
3293 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3295 if (rule < 0x100) /* old format */ \
3297 if (gref == 10) gref = 4; \
3298 if (nref == 10) nref = 4; \
3299 charbuf[idx] = 32 + gref * 9 + nref; \
3300 charbuf[idx + 1] = -1; \
3303 else /* new format */ \
3305 charbuf[idx] = 32 + 81 + gref; \
3306 charbuf[idx + 1] = 32 + nref; \
3311 /* Finish the current composition as invalid. */
3314 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3316 int idx
= - cmp_status
->length
;
3319 /* Recover the original ESC sequence */
3320 charbuf
[idx
++] = ISO_CODE_ESC
;
3321 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3322 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3323 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3324 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3326 charbuf
[idx
++] = -2;
3328 charbuf
[idx
++] = -1;
3329 new_chars
= cmp_status
->nchars
;
3330 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3331 for (; idx
< 0; idx
++)
3333 int elt
= charbuf
[idx
];
3337 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3342 charbuf
[idx
++] = ISO_CODE_ESC
;
3347 cmp_status
->state
= COMPOSING_NO
;
3351 /* If characters are under composition, finish the composition. */
3352 #define MAYBE_FINISH_COMPOSITION() \
3354 if (cmp_status->state != COMPOSING_NO) \
3355 char_offset += finish_composition (charbuf, cmp_status); \
3358 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3360 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3361 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3362 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3363 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3365 Produce this annotation sequence now:
3367 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3370 #define DECODE_COMPOSITION_START(c1) \
3373 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3374 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3375 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3376 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3380 cmp_status->state = COMPOSING_CHAR; \
3381 cmp_status->length += 2; \
3385 MAYBE_FINISH_COMPOSITION (); \
3386 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3387 : c1 == '2' ? COMPOSITION_WITH_RULE \
3388 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3389 : COMPOSITION_WITH_RULE_ALTCHARS); \
3391 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3392 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3393 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3394 cmp_status->nchars = cmp_status->ncomps = 0; \
3395 coding->annotated = 1; \
3400 /* Handle composition end sequence ESC 1. */
3402 #define DECODE_COMPOSITION_END() \
3404 if (cmp_status->nchars == 0 \
3405 || ((cmp_status->state == COMPOSING_CHAR) \
3406 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3408 MAYBE_FINISH_COMPOSITION (); \
3409 goto invalid_code; \
3411 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3412 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3413 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3414 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3415 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3416 char_offset += cmp_status->nchars; \
3417 cmp_status->state = COMPOSING_NO; \
3420 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3422 #define STORE_COMPOSITION_RULE(rule) \
3425 *charbuf++ = rule; \
3426 cmp_status->length += 2; \
3427 cmp_status->state--; \
3430 /* Store a composed char or a component char C in charbuf, and update
3433 #define STORE_COMPOSITION_CHAR(c) \
3436 cmp_status->length++; \
3437 if (cmp_status->state == COMPOSING_CHAR) \
3438 cmp_status->nchars++; \
3440 cmp_status->ncomps++; \
3441 if (cmp_status->method == COMPOSITION_WITH_RULE \
3442 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3443 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3444 cmp_status->state++; \
3448 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3451 decode_coding_iso_2022 (struct coding_system
*coding
)
3453 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3454 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3455 const unsigned char *src_base
;
3456 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3457 /* We may produce two annotations (charset and composition) in one
3458 loop and one more charset annotation at the end. */
3460 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3461 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
3462 bool multibytep
= coding
->src_multibyte
;
3463 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3464 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3465 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3466 int charset_id_2
, charset_id_3
;
3467 struct charset
*charset
;
3469 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3470 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3471 ptrdiff_t char_offset
= coding
->produced_char
;
3472 ptrdiff_t last_offset
= char_offset
;
3473 int last_id
= charset_ascii
;
3475 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3476 int byte_after_cr
= -1;
3479 setup_iso_safe_charsets (attrs
);
3480 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3482 if (cmp_status
->state
!= COMPOSING_NO
)
3484 if (charbuf_end
- charbuf
< cmp_status
->length
)
3486 for (i
= 0; i
< cmp_status
->length
; i
++)
3487 *charbuf
++ = cmp_status
->carryover
[i
];
3488 coding
->annotated
= 1;
3496 consumed_chars_base
= consumed_chars
;
3498 if (charbuf
>= charbuf_end
)
3500 if (byte_after_cr
>= 0)
3505 if (byte_after_cr
>= 0)
3506 c1
= byte_after_cr
, byte_after_cr
= -1;
3512 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3514 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3516 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3520 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3522 if (c1
== ISO_CODE_ESC
)
3524 if (src
+ 1 >= src_end
)
3525 goto no_more_source
;
3526 *charbuf
++ = ISO_CODE_ESC
;
3528 if (src
[0] == '%' && src
[1] == '@')
3531 consumed_chars
+= 2;
3533 /* We are sure charbuf can contain two more chars. */
3536 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3541 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3547 if ((cmp_status
->state
== COMPOSING_RULE
3548 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3549 && c1
!= ISO_CODE_ESC
)
3553 DECODE_COMPOSITION_RULE (rule
);
3554 STORE_COMPOSITION_RULE (rule
);
3558 /* We produce at most one character. */
3559 switch (iso_code_class
[c1
])
3561 case ISO_0x20_or_0x7F
:
3562 if (charset_id_0
< 0
3563 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3564 /* This is SPACE or DEL. */
3565 charset
= CHARSET_FROM_ID (charset_ascii
);
3567 charset
= CHARSET_FROM_ID (charset_id_0
);
3570 case ISO_graphic_plane_0
:
3571 if (charset_id_0
< 0)
3572 charset
= CHARSET_FROM_ID (charset_ascii
);
3574 charset
= CHARSET_FROM_ID (charset_id_0
);
3577 case ISO_0xA0_or_0xFF
:
3578 if (charset_id_1
< 0
3579 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3580 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3582 /* This is a graphic character, we fall down ... */
3584 case ISO_graphic_plane_1
:
3585 if (charset_id_1
< 0)
3587 charset
= CHARSET_FROM_ID (charset_id_1
);
3591 if (eol_dos
&& c1
== '\r')
3592 ONE_MORE_BYTE (byte_after_cr
);
3593 MAYBE_FINISH_COMPOSITION ();
3594 charset
= CHARSET_FROM_ID (charset_ascii
);
3601 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3602 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3604 CODING_ISO_INVOCATION (coding
, 0) = 1;
3605 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3609 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3611 CODING_ISO_INVOCATION (coding
, 0) = 0;
3612 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3615 case ISO_single_shift_2_7
:
3616 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3618 case ISO_single_shift_2
:
3619 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3621 /* SS2 is handled as an escape sequence of ESC 'N' */
3623 goto label_escape_sequence
;
3625 case ISO_single_shift_3
:
3626 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3628 /* SS2 is handled as an escape sequence of ESC 'O' */
3630 goto label_escape_sequence
;
3632 case ISO_control_sequence_introducer
:
3633 /* CSI is handled as an escape sequence of ESC '[' ... */
3635 goto label_escape_sequence
;
3639 label_escape_sequence
:
3640 /* Escape sequences handled here are invocation,
3641 designation, direction specification, and character
3642 composition specification. */
3645 case '&': /* revision of following character set */
3647 if (!(c1
>= '@' && c1
<= '~'))
3650 if (c1
!= ISO_CODE_ESC
)
3653 goto label_escape_sequence
;
3655 case '$': /* designation of 2-byte character set */
3656 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3662 if (c1
>= '@' && c1
<= 'B')
3663 { /* designation of JISX0208.1978, GB2312.1980,
3665 reg
= 0, chars96
= 0;
3667 else if (c1
>= 0x28 && c1
<= 0x2B)
3668 { /* designation of DIMENSION2_CHARS94 character set */
3669 reg
= c1
- 0x28, chars96
= 0;
3672 else if (c1
>= 0x2C && c1
<= 0x2F)
3673 { /* designation of DIMENSION2_CHARS96 character set */
3674 reg
= c1
- 0x2C, chars96
= 1;
3679 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3680 /* We must update these variables now. */
3682 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3684 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3690 case 'n': /* invocation of locking-shift-2 */
3691 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3692 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3694 CODING_ISO_INVOCATION (coding
, 0) = 2;
3695 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3698 case 'o': /* invocation of locking-shift-3 */
3699 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3700 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3702 CODING_ISO_INVOCATION (coding
, 0) = 3;
3703 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3706 case 'N': /* invocation of single-shift-2 */
3707 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3708 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3710 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3711 if (charset_id_2
< 0)
3712 charset
= CHARSET_FROM_ID (charset_ascii
);
3714 charset
= CHARSET_FROM_ID (charset_id_2
);
3716 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3720 case 'O': /* invocation of single-shift-3 */
3721 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3722 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3724 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3725 if (charset_id_3
< 0)
3726 charset
= CHARSET_FROM_ID (charset_ascii
);
3728 charset
= CHARSET_FROM_ID (charset_id_3
);
3730 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3734 case '0': case '2': case '3': case '4': /* start composition */
3735 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3737 if (last_id
!= charset_ascii
)
3739 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3740 last_id
= charset_ascii
;
3741 last_offset
= char_offset
;
3743 DECODE_COMPOSITION_START (c1
);
3746 case '1': /* end composition */
3747 if (cmp_status
->state
== COMPOSING_NO
)
3749 DECODE_COMPOSITION_END ();
3752 case '[': /* specification of direction */
3753 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3755 /* For the moment, nested direction is not supported.
3756 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3757 left-to-right, and nonzero means right-to-left. */
3761 case ']': /* end of the current direction */
3762 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3764 case '0': /* end of the current direction */
3765 case '1': /* start of left-to-right direction */
3768 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3773 case '2': /* start of right-to-left direction */
3776 coding
->mode
|= CODING_MODE_DIRECTION
;
3790 /* CTEXT extended segment:
3791 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3792 We keep these bytes as is for the moment.
3793 They may be decoded by post-read-conversion. */
3797 ONE_MORE_BYTE (dim
);
3798 if (dim
< '0' || dim
> '4')
3806 size
= ((M
- 128) * 128) + (L
- 128);
3807 if (charbuf
+ 6 > charbuf_end
)
3809 *charbuf
++ = ISO_CODE_ESC
;
3813 *charbuf
++ = BYTE8_TO_CHAR (M
);
3814 *charbuf
++ = BYTE8_TO_CHAR (L
);
3815 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3819 /* XFree86 extension for embedding UTF-8 in CTEXT:
3820 ESC % G --UTF-8-BYTES-- ESC % @
3821 We keep these bytes as is for the moment.
3822 They may be decoded by post-read-conversion. */
3823 if (charbuf
+ 3 > charbuf_end
)
3825 *charbuf
++ = ISO_CODE_ESC
;
3828 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3836 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3841 if (c1
>= 0x28 && c1
<= 0x2B)
3842 { /* designation of DIMENSION1_CHARS94 character set */
3843 reg
= c1
- 0x28, chars96
= 0;
3846 else if (c1
>= 0x2C && c1
<= 0x2F)
3847 { /* designation of DIMENSION1_CHARS96 character set */
3848 reg
= c1
- 0x2C, chars96
= 1;
3853 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3854 /* We must update these variables now. */
3856 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3858 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3870 if (cmp_status
->state
== COMPOSING_NO
3871 && charset
->id
!= charset_ascii
3872 && last_id
!= charset
->id
)
3874 if (last_id
!= charset_ascii
)
3875 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3876 last_id
= charset
->id
;
3877 last_offset
= char_offset
;
3880 /* Now we know CHARSET and 1st position code C1 of a character.
3881 Produce a decoded character while getting 2nd and 3rd
3882 position codes C2, C3 if necessary. */
3883 if (CHARSET_DIMENSION (charset
) > 1)
3886 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3887 || ((c1
& 0x80) != (c2
& 0x80)))
3888 /* C2 is not in a valid range. */
3890 if (CHARSET_DIMENSION (charset
) == 2)
3891 c1
= (c1
<< 8) | c2
;
3895 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3896 || ((c1
& 0x80) != (c3
& 0x80)))
3897 /* C3 is not in a valid range. */
3899 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3903 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3906 MAYBE_FINISH_COMPOSITION ();
3907 for (; src_base
< src
; src_base
++, char_offset
++)
3909 if (ASCII_BYTE_P (*src_base
))
3910 *charbuf
++ = *src_base
;
3912 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3915 else if (cmp_status
->state
== COMPOSING_NO
)
3920 else if ((cmp_status
->state
== COMPOSING_CHAR
3921 ? cmp_status
->nchars
3922 : cmp_status
->ncomps
)
3923 >= MAX_COMPOSITION_COMPONENTS
)
3925 /* Too long composition. */
3926 MAYBE_FINISH_COMPOSITION ();
3931 STORE_COMPOSITION_CHAR (c
);
3935 MAYBE_FINISH_COMPOSITION ();
3937 consumed_chars
= consumed_chars_base
;
3939 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3949 if (cmp_status
->state
!= COMPOSING_NO
)
3951 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3952 MAYBE_FINISH_COMPOSITION ();
3955 charbuf
-= cmp_status
->length
;
3956 for (i
= 0; i
< cmp_status
->length
; i
++)
3957 cmp_status
->carryover
[i
] = charbuf
[i
];
3960 else if (last_id
!= charset_ascii
)
3961 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3962 coding
->consumed_char
+= consumed_chars_base
;
3963 coding
->consumed
= src_base
- coding
->source
;
3964 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3968 /* ISO2022 encoding stuff. */
3971 It is not enough to say just "ISO2022" on encoding, we have to
3972 specify more details. In Emacs, each coding system of ISO2022
3973 variant has the following specifications:
3974 1. Initial designation to G0 thru G3.
3975 2. Allows short-form designation?
3976 3. ASCII should be designated to G0 before control characters?
3977 4. ASCII should be designated to G0 at end of line?
3978 5. 7-bit environment or 8-bit environment?
3979 6. Use locking-shift?
3980 7. Use Single-shift?
3981 And the following two are only for Japanese:
3982 8. Use ASCII in place of JIS0201-1976-Roman?
3983 9. Use JISX0208-1983 in place of JISX0208-1978?
3984 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3985 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3989 /* Produce codes (escape sequence) for designating CHARSET to graphic
3990 register REG at DST, and increment DST. If <final-char> of CHARSET is
3991 '@', 'A', or 'B' and the coding system CODING allows, produce
3992 designation sequence of short-form. */
3994 #define ENCODE_DESIGNATION(charset, reg, coding) \
3996 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3997 const char *intermediate_char_94 = "()*+"; \
3998 const char *intermediate_char_96 = ",-./"; \
3999 int revision = -1; \
4001 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4002 revision = CHARSET_ISO_REVISION (charset); \
4004 if (revision >= 0) \
4006 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4007 EMIT_ONE_BYTE ('@' + revision); \
4009 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4010 if (CHARSET_DIMENSION (charset) == 1) \
4013 if (! CHARSET_ISO_CHARS_96 (charset)) \
4014 b = intermediate_char_94[reg]; \
4016 b = intermediate_char_96[reg]; \
4017 EMIT_ONE_ASCII_BYTE (b); \
4021 EMIT_ONE_ASCII_BYTE ('$'); \
4022 if (! CHARSET_ISO_CHARS_96 (charset)) \
4024 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4026 || final_char < '@' || final_char > 'B') \
4027 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4030 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4032 EMIT_ONE_ASCII_BYTE (final_char); \
4034 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4038 /* The following two macros produce codes (control character or escape
4039 sequence) for ISO2022 single-shift functions (single-shift-2 and
4042 #define ENCODE_SINGLE_SHIFT_2 \
4044 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4045 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4047 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4048 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4052 #define ENCODE_SINGLE_SHIFT_3 \
4054 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4055 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4057 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4058 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4062 /* The following four macros produce codes (control character or
4063 escape sequence) for ISO2022 locking-shift functions (shift-in,
4064 shift-out, locking-shift-2, and locking-shift-3). */
4066 #define ENCODE_SHIFT_IN \
4068 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4069 CODING_ISO_INVOCATION (coding, 0) = 0; \
4073 #define ENCODE_SHIFT_OUT \
4075 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4076 CODING_ISO_INVOCATION (coding, 0) = 1; \
4080 #define ENCODE_LOCKING_SHIFT_2 \
4082 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4083 CODING_ISO_INVOCATION (coding, 0) = 2; \
4087 #define ENCODE_LOCKING_SHIFT_3 \
4089 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4090 CODING_ISO_INVOCATION (coding, 0) = 3; \
4094 /* Produce codes for a DIMENSION1 character whose character set is
4095 CHARSET and whose position-code is C1. Designation and invocation
4096 sequences are also produced in advance if necessary. */
4098 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4100 int id = CHARSET_ID (charset); \
4102 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4103 && id == charset_ascii) \
4105 id = charset_jisx0201_roman; \
4106 charset = CHARSET_FROM_ID (id); \
4109 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4111 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4112 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4114 EMIT_ONE_BYTE (c1 | 0x80); \
4115 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4118 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4120 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4123 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4125 EMIT_ONE_BYTE (c1 | 0x80); \
4129 /* Since CHARSET is not yet invoked to any graphic planes, we \
4130 must invoke it, or, at first, designate it to some graphic \
4131 register. Then repeat the loop to actually produce the \
4133 dst = encode_invocation_designation (charset, coding, dst, \
4138 /* Produce codes for a DIMENSION2 character whose character set is
4139 CHARSET and whose position-codes are C1 and C2. Designation and
4140 invocation codes are also produced in advance if necessary. */
4142 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4144 int id = CHARSET_ID (charset); \
4146 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4147 && id == charset_jisx0208) \
4149 id = charset_jisx0208_1978; \
4150 charset = CHARSET_FROM_ID (id); \
4153 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4155 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4156 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4158 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4159 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4162 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4164 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4167 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4169 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4173 /* Since CHARSET is not yet invoked to any graphic planes, we \
4174 must invoke it, or, at first, designate it to some graphic \
4175 register. Then repeat the loop to actually produce the \
4177 dst = encode_invocation_designation (charset, coding, dst, \
4182 #define ENCODE_ISO_CHARACTER(charset, c) \
4185 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4187 if (CHARSET_DIMENSION (charset) == 1) \
4188 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4190 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4194 /* Produce designation and invocation codes at a place pointed by DST
4195 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4198 static unsigned char *
4199 encode_invocation_designation (struct charset
*charset
,
4200 struct coding_system
*coding
,
4201 unsigned char *dst
, ptrdiff_t *p_nchars
)
4203 bool multibytep
= coding
->dst_multibyte
;
4204 ptrdiff_t produced_chars
= *p_nchars
;
4205 int reg
; /* graphic register number */
4206 int id
= CHARSET_ID (charset
);
4208 /* At first, check designations. */
4209 for (reg
= 0; reg
< 4; reg
++)
4210 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4215 /* CHARSET is not yet designated to any graphic registers. */
4216 /* At first check the requested designation. */
4217 reg
= CODING_ISO_REQUEST (coding
, id
);
4219 /* Since CHARSET requests no special designation, designate it
4220 to graphic register 0. */
4223 ENCODE_DESIGNATION (charset
, reg
, coding
);
4226 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4227 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4229 /* Since the graphic register REG is not invoked to any graphic
4230 planes, invoke it to graphic plane 0. */
4233 case 0: /* graphic register 0 */
4237 case 1: /* graphic register 1 */
4241 case 2: /* graphic register 2 */
4242 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4243 ENCODE_SINGLE_SHIFT_2
;
4245 ENCODE_LOCKING_SHIFT_2
;
4248 case 3: /* graphic register 3 */
4249 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4250 ENCODE_SINGLE_SHIFT_3
;
4252 ENCODE_LOCKING_SHIFT_3
;
4257 *p_nchars
= produced_chars
;
4262 /* Produce codes for designation and invocation to reset the graphic
4263 planes and registers to initial state. */
4264 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4267 struct charset *charset; \
4269 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4271 for (reg = 0; reg < 4; reg++) \
4272 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4273 && (CODING_ISO_DESIGNATION (coding, reg) \
4274 != CODING_ISO_INITIAL (coding, reg))) \
4276 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4277 ENCODE_DESIGNATION (charset, reg, coding); \
4282 /* Produce designation sequences of charsets in the line started from
4283 CHARBUF to a place pointed by DST, and return the number of
4284 produced bytes. DST should not directly point a buffer text area
4285 which may be relocated by char_charset call.
4287 If the current block ends before any end-of-line, we may fail to
4288 find all the necessary designations. */
4291 encode_designation_at_bol (struct coding_system
*coding
,
4292 int *charbuf
, int *charbuf_end
,
4295 unsigned char *orig
= dst
;
4296 struct charset
*charset
;
4297 /* Table of charsets to be designated to each graphic register. */
4299 int c
, found
= 0, reg
;
4300 ptrdiff_t produced_chars
= 0;
4301 bool multibytep
= coding
->dst_multibyte
;
4303 Lisp_Object charset_list
;
4305 attrs
= CODING_ID_ATTRS (coding
->id
);
4306 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4307 if (EQ (charset_list
, Qiso_2022
))
4308 charset_list
= Viso_2022_charset_list
;
4310 for (reg
= 0; reg
< 4; reg
++)
4313 while (charbuf
< charbuf_end
&& found
< 4)
4320 charset
= char_charset (c
, charset_list
, NULL
);
4321 id
= CHARSET_ID (charset
);
4322 reg
= CODING_ISO_REQUEST (coding
, id
);
4323 if (reg
>= 0 && r
[reg
] < 0)
4332 for (reg
= 0; reg
< 4; reg
++)
4334 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4335 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4341 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4344 encode_coding_iso_2022 (struct coding_system
*coding
)
4346 bool multibytep
= coding
->dst_multibyte
;
4347 int *charbuf
= coding
->charbuf
;
4348 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4349 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4350 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4352 bool bol_designation
4353 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4354 && CODING_ISO_BOL (coding
));
4355 ptrdiff_t produced_chars
= 0;
4356 Lisp_Object attrs
, eol_type
, charset_list
;
4357 bool ascii_compatible
;
4359 int preferred_charset_id
= -1;
4361 CODING_GET_INFO (coding
, attrs
, charset_list
);
4362 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4363 if (VECTORP (eol_type
))
4366 setup_iso_safe_charsets (attrs
);
4367 /* Charset list may have been changed. */
4368 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4369 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4372 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4373 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4374 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4376 while (charbuf
< charbuf_end
)
4378 ASSURE_DESTINATION (safe_room
);
4380 if (bol_designation
)
4382 /* We have to produce designation sequences if any now. */
4383 unsigned char desig_buf
[16];
4387 charset_map_loaded
= 0;
4388 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4390 if (charset_map_loaded
4391 && (offset
= coding_change_destination (coding
)))
4396 memcpy (dst
, desig_buf
, nbytes
);
4398 /* We are sure that designation sequences are all ASCII bytes. */
4399 produced_chars
+= nbytes
;
4400 bol_designation
= 0;
4401 ASSURE_DESTINATION (safe_room
);
4408 /* Handle an annotation. */
4411 case CODING_ANNOTATE_COMPOSITION_MASK
:
4412 /* Not yet implemented. */
4414 case CODING_ANNOTATE_CHARSET_MASK
:
4415 preferred_charset_id
= charbuf
[2];
4416 if (preferred_charset_id
>= 0
4417 && NILP (Fmemq (make_number (preferred_charset_id
),
4419 preferred_charset_id
= -1;
4428 /* Now encode the character C. */
4429 if (c
< 0x20 || c
== 0x7F)
4432 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4434 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4435 ENCODE_RESET_PLANE_AND_REGISTER ();
4436 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4440 for (i
= 0; i
< 4; i
++)
4441 CODING_ISO_DESIGNATION (coding
, i
)
4442 = CODING_ISO_INITIAL (coding
, i
);
4444 bol_designation
= ((CODING_ISO_FLAGS (coding
)
4445 & CODING_ISO_FLAG_DESIGNATE_AT_BOL
)
4448 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4449 ENCODE_RESET_PLANE_AND_REGISTER ();
4450 EMIT_ONE_ASCII_BYTE (c
);
4452 else if (ASCII_CHAR_P (c
))
4454 if (ascii_compatible
)
4455 EMIT_ONE_ASCII_BYTE (c
);
4458 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4459 ENCODE_ISO_CHARACTER (charset
, c
);
4462 else if (CHAR_BYTE8_P (c
))
4464 c
= CHAR_TO_BYTE8 (c
);
4469 struct charset
*charset
;
4471 if (preferred_charset_id
>= 0)
4475 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4476 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4478 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4482 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4486 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4488 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4489 charset
= CHARSET_FROM_ID (charset_ascii
);
4493 c
= coding
->default_char
;
4494 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4495 charset_list
, NULL
, charset
);
4498 ENCODE_ISO_CHARACTER (charset
, c
);
4502 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4503 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4505 ASSURE_DESTINATION (safe_room
);
4506 ENCODE_RESET_PLANE_AND_REGISTER ();
4508 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4509 CODING_ISO_BOL (coding
) = bol_designation
;
4510 coding
->produced_char
+= produced_chars
;
4511 coding
->produced
= dst
- coding
->destination
;
4516 /*** 8,9. SJIS and BIG5 handlers ***/
4518 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4519 quite widely. So, for the moment, Emacs supports them in the bare
4520 C code. But, in the future, they may be supported only by CCL. */
4522 /* SJIS is a coding system encoding three character sets: ASCII, right
4523 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4524 as is. A character of charset katakana-jisx0201 is encoded by
4525 "position-code + 0x80". A character of charset japanese-jisx0208
4526 is encoded in 2-byte but two position-codes are divided and shifted
4527 so that it fit in the range below.
4529 --- CODE RANGE of SJIS ---
4530 (character set) (range)
4532 KATAKANA-JISX0201 0xA0 .. 0xDF
4533 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4534 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4535 -------------------------------
4539 /* BIG5 is a coding system encoding two character sets: ASCII and
4540 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4541 character set and is encoded in two-byte.
4543 --- CODE RANGE of BIG5 ---
4544 (character set) (range)
4546 Big5 (1st byte) 0xA1 .. 0xFE
4547 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4548 --------------------------
4552 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4553 Return true if a text is encoded in SJIS. */
4556 detect_coding_sjis (struct coding_system
*coding
,
4557 struct coding_detection_info
*detect_info
)
4559 const unsigned char *src
= coding
->source
, *src_base
;
4560 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4561 bool multibytep
= coding
->src_multibyte
;
4562 ptrdiff_t consumed_chars
= 0;
4565 Lisp_Object attrs
, charset_list
;
4566 int max_first_byte_of_2_byte_code
;
4568 CODING_GET_INFO (coding
, attrs
, charset_list
);
4569 max_first_byte_of_2_byte_code
4570 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4572 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4573 /* A coding system of this category is always ASCII compatible. */
4574 src
+= coding
->head_ascii
;
4582 if ((c
>= 0x81 && c
<= 0x9F)
4583 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4586 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4588 found
= CATEGORY_MASK_SJIS
;
4590 else if (c
>= 0xA0 && c
< 0xE0)
4591 found
= CATEGORY_MASK_SJIS
;
4595 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4599 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4601 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4604 detect_info
->found
|= found
;
4608 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4609 Return true if a text is encoded in BIG5. */
4612 detect_coding_big5 (struct coding_system
*coding
,
4613 struct coding_detection_info
*detect_info
)
4615 const unsigned char *src
= coding
->source
, *src_base
;
4616 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4617 bool multibytep
= coding
->src_multibyte
;
4618 ptrdiff_t consumed_chars
= 0;
4622 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4623 /* A coding system of this category is always ASCII compatible. */
4624 src
+= coding
->head_ascii
;
4635 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4637 found
= CATEGORY_MASK_BIG5
;
4642 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4646 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4648 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4651 detect_info
->found
|= found
;
4655 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4658 decode_coding_sjis (struct coding_system
*coding
)
4660 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4661 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4662 const unsigned char *src_base
;
4663 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4664 /* We may produce one charset annotation in one loop and one more at
4667 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4668 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4669 bool multibytep
= coding
->src_multibyte
;
4670 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4671 struct charset
*charset_kanji2
;
4672 Lisp_Object attrs
, charset_list
, val
;
4673 ptrdiff_t char_offset
= coding
->produced_char
;
4674 ptrdiff_t last_offset
= char_offset
;
4675 int last_id
= charset_ascii
;
4677 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4678 int byte_after_cr
= -1;
4680 CODING_GET_INFO (coding
, attrs
, charset_list
);
4683 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4684 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4685 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4686 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4691 struct charset
*charset
;
4694 consumed_chars_base
= consumed_chars
;
4696 if (charbuf
>= charbuf_end
)
4698 if (byte_after_cr
>= 0)
4703 if (byte_after_cr
>= 0)
4704 c
= byte_after_cr
, byte_after_cr
= -1;
4711 if (eol_dos
&& c
== '\r')
4712 ONE_MORE_BYTE (byte_after_cr
);
4713 charset
= charset_roman
;
4715 else if (c
== 0x80 || c
== 0xA0)
4717 else if (c
>= 0xA1 && c
<= 0xDF)
4719 /* SJIS -> JISX0201-Kana */
4721 charset
= charset_kana
;
4725 /* SJIS -> JISX0208 */
4727 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4731 charset
= charset_kanji
;
4733 else if (c
<= 0xFC && charset_kanji2
)
4735 /* SJIS -> JISX0213-2 */
4737 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4741 charset
= charset_kanji2
;
4745 if (charset
->id
!= charset_ascii
4746 && last_id
!= charset
->id
)
4748 if (last_id
!= charset_ascii
)
4749 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4750 last_id
= charset
->id
;
4751 last_offset
= char_offset
;
4753 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4760 consumed_chars
= consumed_chars_base
;
4762 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4768 if (last_id
!= charset_ascii
)
4769 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4770 coding
->consumed_char
+= consumed_chars_base
;
4771 coding
->consumed
= src_base
- coding
->source
;
4772 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4776 decode_coding_big5 (struct coding_system
*coding
)
4778 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4779 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4780 const unsigned char *src_base
;
4781 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4782 /* We may produce one charset annotation in one loop and one more at
4785 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4786 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
4787 bool multibytep
= coding
->src_multibyte
;
4788 struct charset
*charset_roman
, *charset_big5
;
4789 Lisp_Object attrs
, charset_list
, val
;
4790 ptrdiff_t char_offset
= coding
->produced_char
;
4791 ptrdiff_t last_offset
= char_offset
;
4792 int last_id
= charset_ascii
;
4794 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4795 int byte_after_cr
= -1;
4797 CODING_GET_INFO (coding
, attrs
, charset_list
);
4799 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4800 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4805 struct charset
*charset
;
4808 consumed_chars_base
= consumed_chars
;
4810 if (charbuf
>= charbuf_end
)
4812 if (byte_after_cr
>= 0)
4817 if (byte_after_cr
>= 0)
4818 c
= byte_after_cr
, byte_after_cr
= -1;
4826 if (eol_dos
&& c
== '\r')
4827 ONE_MORE_BYTE (byte_after_cr
);
4828 charset
= charset_roman
;
4833 if (c
< 0xA1 || c
> 0xFE)
4836 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4839 charset
= charset_big5
;
4841 if (charset
->id
!= charset_ascii
4842 && last_id
!= charset
->id
)
4844 if (last_id
!= charset_ascii
)
4845 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4846 last_id
= charset
->id
;
4847 last_offset
= char_offset
;
4849 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4856 consumed_chars
= consumed_chars_base
;
4858 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4864 if (last_id
!= charset_ascii
)
4865 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4866 coding
->consumed_char
+= consumed_chars_base
;
4867 coding
->consumed
= src_base
- coding
->source
;
4868 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4871 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4872 This function can encode charsets `ascii', `katakana-jisx0201',
4873 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4874 are sure that all these charsets are registered as official charset
4875 (i.e. do not have extended leading-codes). Characters of other
4876 charsets are produced without any encoding. */
4879 encode_coding_sjis (struct coding_system
*coding
)
4881 bool multibytep
= coding
->dst_multibyte
;
4882 int *charbuf
= coding
->charbuf
;
4883 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4884 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4885 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4887 ptrdiff_t produced_chars
= 0;
4888 Lisp_Object attrs
, charset_list
, val
;
4889 bool ascii_compatible
;
4890 struct charset
*charset_kanji
, *charset_kana
;
4891 struct charset
*charset_kanji2
;
4894 CODING_GET_INFO (coding
, attrs
, charset_list
);
4895 val
= XCDR (charset_list
);
4896 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4897 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4898 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4900 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4902 while (charbuf
< charbuf_end
)
4904 ASSURE_DESTINATION (safe_room
);
4906 /* Now encode the character C. */
4907 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4908 EMIT_ONE_ASCII_BYTE (c
);
4909 else if (CHAR_BYTE8_P (c
))
4911 c
= CHAR_TO_BYTE8 (c
);
4917 struct charset
*charset
;
4918 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4923 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4925 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4926 charset
= CHARSET_FROM_ID (charset_ascii
);
4930 c
= coding
->default_char
;
4931 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4932 charset_list
, &code
, charset
);
4935 if (code
== CHARSET_INVALID_CODE (charset
))
4937 if (charset
== charset_kanji
)
4941 c1
= code
>> 8, c2
= code
& 0xFF;
4942 EMIT_TWO_BYTES (c1
, c2
);
4944 else if (charset
== charset_kana
)
4945 EMIT_ONE_BYTE (code
| 0x80);
4946 else if (charset_kanji2
&& charset
== charset_kanji2
)
4951 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4953 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4955 JIS_TO_SJIS2 (code
);
4956 c1
= code
>> 8, c2
= code
& 0xFF;
4957 EMIT_TWO_BYTES (c1
, c2
);
4960 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4963 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4966 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4967 coding
->produced_char
+= produced_chars
;
4968 coding
->produced
= dst
- coding
->destination
;
4973 encode_coding_big5 (struct coding_system
*coding
)
4975 bool multibytep
= coding
->dst_multibyte
;
4976 int *charbuf
= coding
->charbuf
;
4977 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4978 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4979 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4981 ptrdiff_t produced_chars
= 0;
4982 Lisp_Object attrs
, charset_list
, val
;
4983 bool ascii_compatible
;
4984 struct charset
*charset_big5
;
4987 CODING_GET_INFO (coding
, attrs
, charset_list
);
4988 val
= XCDR (charset_list
);
4989 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4990 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4992 while (charbuf
< charbuf_end
)
4994 ASSURE_DESTINATION (safe_room
);
4996 /* Now encode the character C. */
4997 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4998 EMIT_ONE_ASCII_BYTE (c
);
4999 else if (CHAR_BYTE8_P (c
))
5001 c
= CHAR_TO_BYTE8 (c
);
5007 struct charset
*charset
;
5008 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5013 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5015 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5016 charset
= CHARSET_FROM_ID (charset_ascii
);
5020 c
= coding
->default_char
;
5021 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
5022 charset_list
, &code
, charset
);
5025 if (code
== CHARSET_INVALID_CODE (charset
))
5027 if (charset
== charset_big5
)
5031 c1
= code
>> 8, c2
= code
& 0xFF;
5032 EMIT_TWO_BYTES (c1
, c2
);
5035 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5038 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5039 coding
->produced_char
+= produced_chars
;
5040 coding
->produced
= dst
- coding
->destination
;
5045 /*** 10. CCL handlers ***/
5047 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5048 Return true if a text is encoded in a coding system of which
5049 encoder/decoder are written in CCL program. */
5052 detect_coding_ccl (struct coding_system
*coding
,
5053 struct coding_detection_info
*detect_info
)
5055 const unsigned char *src
= coding
->source
, *src_base
;
5056 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5057 bool multibytep
= coding
->src_multibyte
;
5058 ptrdiff_t consumed_chars
= 0;
5060 unsigned char *valids
;
5061 ptrdiff_t head_ascii
= coding
->head_ascii
;
5064 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5066 coding
= &coding_categories
[coding_category_ccl
];
5067 valids
= CODING_CCL_VALIDS (coding
);
5068 attrs
= CODING_ID_ATTRS (coding
->id
);
5069 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5078 if (c
< 0 || ! valids
[c
])
5080 if ((valids
[c
] > 1))
5081 found
= CATEGORY_MASK_CCL
;
5083 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5087 detect_info
->found
|= found
;
5092 decode_coding_ccl (struct coding_system
*coding
)
5094 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5095 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5096 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5097 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5098 ptrdiff_t consumed_chars
= 0;
5099 bool multibytep
= coding
->src_multibyte
;
5100 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5101 int source_charbuf
[1024];
5102 int source_byteidx
[1025];
5103 Lisp_Object attrs
, charset_list
;
5105 CODING_GET_INFO (coding
, attrs
, charset_list
);
5109 const unsigned char *p
= src
;
5114 while (i
< 1024 && p
< src_end
)
5116 source_byteidx
[i
] = p
- src
;
5117 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5119 source_byteidx
[i
] = p
- src
;
5122 while (i
< 1024 && p
< src_end
)
5123 source_charbuf
[i
++] = *p
++;
5125 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5126 ccl
->last_block
= 1;
5127 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5129 charbuf
+= ccl
->produced
;
5131 src
+= source_byteidx
[ccl
->consumed
];
5133 src
+= ccl
->consumed
;
5134 consumed_chars
+= ccl
->consumed
;
5135 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5139 switch (ccl
->status
)
5141 case CCL_STAT_SUSPEND_BY_SRC
:
5142 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5144 case CCL_STAT_SUSPEND_BY_DST
:
5145 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5148 case CCL_STAT_INVALID_CMD
:
5149 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5152 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5155 coding
->consumed_char
+= consumed_chars
;
5156 coding
->consumed
= src
- coding
->source
;
5157 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5161 encode_coding_ccl (struct coding_system
*coding
)
5163 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5164 bool multibytep
= coding
->dst_multibyte
;
5165 int *charbuf
= coding
->charbuf
;
5166 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5167 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5168 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5169 int destination_charbuf
[1024];
5170 ptrdiff_t produced_chars
= 0;
5172 Lisp_Object attrs
, charset_list
;
5174 CODING_GET_INFO (coding
, attrs
, charset_list
);
5175 if (coding
->consumed_char
== coding
->src_chars
5176 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5177 ccl
->last_block
= 1;
5181 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5182 charbuf_end
- charbuf
, 1024, charset_list
);
5185 ASSURE_DESTINATION (ccl
->produced
* 2);
5186 for (i
= 0; i
< ccl
->produced
; i
++)
5187 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5191 ASSURE_DESTINATION (ccl
->produced
);
5192 for (i
= 0; i
< ccl
->produced
; i
++)
5193 *dst
++ = destination_charbuf
[i
] & 0xFF;
5194 produced_chars
+= ccl
->produced
;
5196 charbuf
+= ccl
->consumed
;
5197 if (ccl
->status
== CCL_STAT_QUIT
5198 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5201 while (charbuf
< charbuf_end
);
5203 switch (ccl
->status
)
5205 case CCL_STAT_SUSPEND_BY_SRC
:
5206 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5208 case CCL_STAT_SUSPEND_BY_DST
:
5209 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5212 case CCL_STAT_INVALID_CMD
:
5213 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5216 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5220 coding
->produced_char
+= produced_chars
;
5221 coding
->produced
= dst
- coding
->destination
;
5226 /*** 10, 11. no-conversion handlers ***/
5228 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5231 decode_coding_raw_text (struct coding_system
*coding
)
5234 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5236 coding
->chars_at_source
= 1;
5237 coding
->consumed_char
= coding
->src_chars
;
5238 coding
->consumed
= coding
->src_bytes
;
5239 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5241 coding
->consumed_char
--;
5243 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5246 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5250 encode_coding_raw_text (struct coding_system
*coding
)
5252 bool multibytep
= coding
->dst_multibyte
;
5253 int *charbuf
= coding
->charbuf
;
5254 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5255 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5256 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5257 ptrdiff_t produced_chars
= 0;
5262 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5264 if (coding
->src_multibyte
)
5265 while (charbuf
< charbuf_end
)
5267 ASSURE_DESTINATION (safe_room
);
5269 if (ASCII_CHAR_P (c
))
5270 EMIT_ONE_ASCII_BYTE (c
);
5271 else if (CHAR_BYTE8_P (c
))
5273 c
= CHAR_TO_BYTE8 (c
);
5278 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5280 CHAR_STRING_ADVANCE (c
, p1
);
5283 EMIT_ONE_BYTE (*p0
);
5290 while (charbuf
< charbuf_end
)
5292 ASSURE_DESTINATION (safe_room
);
5299 if (coding
->src_multibyte
)
5301 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5303 while (charbuf
< charbuf_end
)
5305 ASSURE_DESTINATION (safe_room
);
5307 if (ASCII_CHAR_P (c
))
5309 else if (CHAR_BYTE8_P (c
))
5310 *dst
++ = CHAR_TO_BYTE8 (c
);
5312 CHAR_STRING_ADVANCE (c
, dst
);
5317 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5318 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5319 *dst
++ = *charbuf
++;
5321 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5323 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5324 coding
->produced_char
+= produced_chars
;
5325 coding
->produced
= dst
- coding
->destination
;
5329 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5330 Return true if a text is encoded in a charset-based coding system. */
5333 detect_coding_charset (struct coding_system
*coding
,
5334 struct coding_detection_info
*detect_info
)
5336 const unsigned char *src
= coding
->source
, *src_base
;
5337 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5338 bool multibytep
= coding
->src_multibyte
;
5339 ptrdiff_t consumed_chars
= 0;
5340 Lisp_Object attrs
, valids
, name
;
5342 ptrdiff_t head_ascii
= coding
->head_ascii
;
5343 bool check_latin_extra
= 0;
5345 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5347 coding
= &coding_categories
[coding_category_charset
];
5348 attrs
= CODING_ID_ATTRS (coding
->id
);
5349 valids
= AREF (attrs
, coding_attr_charset_valids
);
5350 name
= CODING_ID_NAME (coding
->id
);
5351 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5352 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5353 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5354 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5355 check_latin_extra
= 1;
5357 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5364 struct charset
*charset
;
5371 val
= AREF (valids
, c
);
5377 && check_latin_extra
5378 && (!VECTORP (Vlatin_extra_code_table
)
5379 || NILP (AREF (Vlatin_extra_code_table
, c
))))
5381 found
= CATEGORY_MASK_CHARSET
;
5385 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5386 dim
= CHARSET_DIMENSION (charset
);
5387 for (idx
= 1; idx
< dim
; idx
++)
5392 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5393 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5402 for (; CONSP (val
); val
= XCDR (val
))
5404 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5405 dim
= CHARSET_DIMENSION (charset
);
5411 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5412 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5427 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5431 detect_info
->found
|= found
;
5436 decode_coding_charset (struct coding_system
*coding
)
5438 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5439 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5440 const unsigned char *src_base
;
5441 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5442 /* We may produce one charset annotation in one loop and one more at
5445 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5446 ptrdiff_t consumed_chars
= 0, consumed_chars_base
;
5447 bool multibytep
= coding
->src_multibyte
;
5448 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5450 ptrdiff_t char_offset
= coding
->produced_char
;
5451 ptrdiff_t last_offset
= char_offset
;
5452 int last_id
= charset_ascii
;
5454 = !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5455 int byte_after_cr
= -1;
5457 valids
= AREF (attrs
, coding_attr_charset_valids
);
5463 struct charset
*charset
;
5469 consumed_chars_base
= consumed_chars
;
5471 if (charbuf
>= charbuf_end
)
5473 if (byte_after_cr
>= 0)
5478 if (byte_after_cr
>= 0)
5486 if (eol_dos
&& c
== '\r')
5487 ONE_MORE_BYTE (byte_after_cr
);
5493 val
= AREF (valids
, c
);
5494 if (! INTEGERP (val
) && ! CONSP (val
))
5498 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5499 dim
= CHARSET_DIMENSION (charset
);
5503 code
= (code
<< 8) | c
;
5506 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5511 /* VAL is a list of charset IDs. It is assured that the
5512 list is sorted by charset dimensions (smaller one
5516 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5517 dim
= CHARSET_DIMENSION (charset
);
5521 code
= (code
<< 8) | c
;
5524 CODING_DECODE_CHAR (coding
, src
, src_base
,
5525 src_end
, charset
, code
, c
);
5533 if (charset
->id
!= charset_ascii
5534 && last_id
!= charset
->id
)
5536 if (last_id
!= charset_ascii
)
5537 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5538 last_id
= charset
->id
;
5539 last_offset
= char_offset
;
5548 consumed_chars
= consumed_chars_base
;
5550 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5556 if (last_id
!= charset_ascii
)
5557 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5558 coding
->consumed_char
+= consumed_chars_base
;
5559 coding
->consumed
= src_base
- coding
->source
;
5560 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5564 encode_coding_charset (struct coding_system
*coding
)
5566 bool multibytep
= coding
->dst_multibyte
;
5567 int *charbuf
= coding
->charbuf
;
5568 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5569 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5570 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5571 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5572 ptrdiff_t produced_chars
= 0;
5573 Lisp_Object attrs
, charset_list
;
5574 bool ascii_compatible
;
5577 CODING_GET_INFO (coding
, attrs
, charset_list
);
5578 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5580 while (charbuf
< charbuf_end
)
5582 struct charset
*charset
;
5585 ASSURE_DESTINATION (safe_room
);
5587 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5588 EMIT_ONE_ASCII_BYTE (c
);
5589 else if (CHAR_BYTE8_P (c
))
5591 c
= CHAR_TO_BYTE8 (c
);
5596 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5601 if (CHARSET_DIMENSION (charset
) == 1)
5602 EMIT_ONE_BYTE (code
);
5603 else if (CHARSET_DIMENSION (charset
) == 2)
5604 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5605 else if (CHARSET_DIMENSION (charset
) == 3)
5606 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5608 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5609 (code
>> 8) & 0xFF, code
& 0xFF);
5613 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5614 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5616 c
= coding
->default_char
;
5622 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5623 coding
->produced_char
+= produced_chars
;
5624 coding
->produced
= dst
- coding
->destination
;
5629 /*** 7. C library functions ***/
5631 /* Setup coding context CODING from information about CODING_SYSTEM.
5632 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5633 CODING_SYSTEM is invalid, signal an error. */
5636 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5639 Lisp_Object eol_type
;
5640 Lisp_Object coding_type
;
5643 if (NILP (coding_system
))
5644 coding_system
= Qundecided
;
5646 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5648 attrs
= CODING_ID_ATTRS (coding
->id
);
5649 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5652 coding
->head_ascii
= -1;
5653 if (VECTORP (eol_type
))
5654 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5655 | CODING_REQUIRE_DETECTION_MASK
);
5656 else if (! EQ (eol_type
, Qunix
))
5657 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5658 | CODING_REQUIRE_ENCODING_MASK
);
5660 coding
->common_flags
= 0;
5661 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5662 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5663 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5664 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5665 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5666 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5668 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5669 coding
->max_charset_id
= SCHARS (val
) - 1;
5670 coding
->safe_charsets
= SDATA (val
);
5671 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5672 coding
->carryover_bytes
= 0;
5674 coding_type
= CODING_ATTR_TYPE (attrs
);
5675 if (EQ (coding_type
, Qundecided
))
5677 coding
->detector
= NULL
;
5678 coding
->decoder
= decode_coding_raw_text
;
5679 coding
->encoder
= encode_coding_raw_text
;
5680 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5682 else if (EQ (coding_type
, Qiso_2022
))
5685 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5687 /* Invoke graphic register 0 to plane 0. */
5688 CODING_ISO_INVOCATION (coding
, 0) = 0;
5689 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5690 CODING_ISO_INVOCATION (coding
, 1)
5691 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5692 /* Setup the initial status of designation. */
5693 for (i
= 0; i
< 4; i
++)
5694 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5695 /* Not single shifting initially. */
5696 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5697 /* Beginning of buffer should also be regarded as bol. */
5698 CODING_ISO_BOL (coding
) = 1;
5699 coding
->detector
= detect_coding_iso_2022
;
5700 coding
->decoder
= decode_coding_iso_2022
;
5701 coding
->encoder
= encode_coding_iso_2022
;
5702 if (flags
& CODING_ISO_FLAG_SAFE
)
5703 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5704 coding
->common_flags
5705 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5706 | CODING_REQUIRE_FLUSHING_MASK
);
5707 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5708 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5709 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5710 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5711 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5713 setup_iso_safe_charsets (attrs
);
5714 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5715 coding
->max_charset_id
= SCHARS (val
) - 1;
5716 coding
->safe_charsets
= SDATA (val
);
5718 CODING_ISO_FLAGS (coding
) = flags
;
5719 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5720 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5721 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5722 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5724 else if (EQ (coding_type
, Qcharset
))
5726 coding
->detector
= detect_coding_charset
;
5727 coding
->decoder
= decode_coding_charset
;
5728 coding
->encoder
= encode_coding_charset
;
5729 coding
->common_flags
5730 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5732 else if (EQ (coding_type
, Qutf_8
))
5734 val
= AREF (attrs
, coding_attr_utf_bom
);
5735 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5736 : EQ (val
, Qt
) ? utf_with_bom
5738 coding
->detector
= detect_coding_utf_8
;
5739 coding
->decoder
= decode_coding_utf_8
;
5740 coding
->encoder
= encode_coding_utf_8
;
5741 coding
->common_flags
5742 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5743 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5744 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5746 else if (EQ (coding_type
, Qutf_16
))
5748 val
= AREF (attrs
, coding_attr_utf_bom
);
5749 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5750 : EQ (val
, Qt
) ? utf_with_bom
5752 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5753 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5754 : utf_16_little_endian
);
5755 CODING_UTF_16_SURROGATE (coding
) = 0;
5756 coding
->detector
= detect_coding_utf_16
;
5757 coding
->decoder
= decode_coding_utf_16
;
5758 coding
->encoder
= encode_coding_utf_16
;
5759 coding
->common_flags
5760 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5761 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5762 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5764 else if (EQ (coding_type
, Qccl
))
5766 coding
->detector
= detect_coding_ccl
;
5767 coding
->decoder
= decode_coding_ccl
;
5768 coding
->encoder
= encode_coding_ccl
;
5769 coding
->common_flags
5770 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5771 | CODING_REQUIRE_FLUSHING_MASK
);
5773 else if (EQ (coding_type
, Qemacs_mule
))
5775 coding
->detector
= detect_coding_emacs_mule
;
5776 coding
->decoder
= decode_coding_emacs_mule
;
5777 coding
->encoder
= encode_coding_emacs_mule
;
5778 coding
->common_flags
5779 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5780 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5781 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5783 Lisp_Object tail
, safe_charsets
;
5784 int max_charset_id
= 0;
5786 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5788 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5789 max_charset_id
= XFASTINT (XCAR (tail
));
5790 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5791 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5792 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5794 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5795 coding
->max_charset_id
= max_charset_id
;
5796 coding
->safe_charsets
= SDATA (safe_charsets
);
5798 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5799 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5801 else if (EQ (coding_type
, Qshift_jis
))
5803 coding
->detector
= detect_coding_sjis
;
5804 coding
->decoder
= decode_coding_sjis
;
5805 coding
->encoder
= encode_coding_sjis
;
5806 coding
->common_flags
5807 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5809 else if (EQ (coding_type
, Qbig5
))
5811 coding
->detector
= detect_coding_big5
;
5812 coding
->decoder
= decode_coding_big5
;
5813 coding
->encoder
= encode_coding_big5
;
5814 coding
->common_flags
5815 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5817 else /* EQ (coding_type, Qraw_text) */
5819 coding
->detector
= NULL
;
5820 coding
->decoder
= decode_coding_raw_text
;
5821 coding
->encoder
= encode_coding_raw_text
;
5822 if (! EQ (eol_type
, Qunix
))
5824 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5825 if (! VECTORP (eol_type
))
5826 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5834 /* Return a list of charsets supported by CODING. */
5837 coding_charset_list (struct coding_system
*coding
)
5839 Lisp_Object attrs
, charset_list
;
5841 CODING_GET_INFO (coding
, attrs
, charset_list
);
5842 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5844 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5846 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5847 charset_list
= Viso_2022_charset_list
;
5849 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5851 charset_list
= Vemacs_mule_charset_list
;
5853 return charset_list
;
5857 /* Return a list of charsets supported by CODING-SYSTEM. */
5860 coding_system_charset_list (Lisp_Object coding_system
)
5863 Lisp_Object attrs
, charset_list
;
5865 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5866 attrs
= CODING_ID_ATTRS (id
);
5868 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5870 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5872 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5873 charset_list
= Viso_2022_charset_list
;
5875 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5877 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5879 charset_list
= Vemacs_mule_charset_list
;
5883 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5885 return charset_list
;
5889 /* Return raw-text or one of its subsidiaries that has the same
5890 eol_type as CODING-SYSTEM. */
5893 raw_text_coding_system (Lisp_Object coding_system
)
5895 Lisp_Object spec
, attrs
;
5896 Lisp_Object eol_type
, raw_text_eol_type
;
5898 if (NILP (coding_system
))
5900 spec
= CODING_SYSTEM_SPEC (coding_system
);
5901 attrs
= AREF (spec
, 0);
5903 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5904 return coding_system
;
5906 eol_type
= AREF (spec
, 2);
5907 if (VECTORP (eol_type
))
5909 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5910 raw_text_eol_type
= AREF (spec
, 2);
5911 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5912 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5913 : AREF (raw_text_eol_type
, 2));
5917 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5918 the subsidiary that has the same eol-spec as PARENT (if it is not
5919 nil and specifies end-of-line format) or the system's setting
5920 (system_eol_type). */
5923 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5925 Lisp_Object spec
, eol_type
;
5927 if (NILP (coding_system
))
5928 coding_system
= Qraw_text
;
5929 spec
= CODING_SYSTEM_SPEC (coding_system
);
5930 eol_type
= AREF (spec
, 2);
5931 if (VECTORP (eol_type
))
5933 Lisp_Object parent_eol_type
;
5935 if (! NILP (parent
))
5937 Lisp_Object parent_spec
;
5939 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5940 parent_eol_type
= AREF (parent_spec
, 2);
5941 if (VECTORP (parent_eol_type
))
5942 parent_eol_type
= system_eol_type
;
5945 parent_eol_type
= system_eol_type
;
5946 if (EQ (parent_eol_type
, Qunix
))
5947 coding_system
= AREF (eol_type
, 0);
5948 else if (EQ (parent_eol_type
, Qdos
))
5949 coding_system
= AREF (eol_type
, 1);
5950 else if (EQ (parent_eol_type
, Qmac
))
5951 coding_system
= AREF (eol_type
, 2);
5953 return coding_system
;
5957 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5958 decided for writing to a process. If not, complement them, and
5959 return a new coding system. */
5962 complement_process_encoding_system (Lisp_Object coding_system
)
5964 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5965 Lisp_Object spec
, attrs
;
5968 for (i
= 0; i
< 3; i
++)
5971 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
5973 coding_system
= preferred_coding_system ();
5974 spec
= CODING_SYSTEM_SPEC (coding_system
);
5977 attrs
= AREF (spec
, 0);
5978 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
5979 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
5980 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
5981 eol_base
= coding_system
;
5982 if (! NILP (coding_base
) && ! NILP (eol_base
))
5987 /* The original CODING_SYSTEM didn't specify text-conversion or
5988 eol-conversion. Be sure that we return a fully complemented
5990 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
5991 return coding_system
;
5995 /* Emacs has a mechanism to automatically detect a coding system if it
5996 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5997 it's impossible to distinguish some coding systems accurately
5998 because they use the same range of codes. So, at first, coding
5999 systems are categorized into 7, those are:
6001 o coding-category-emacs-mule
6003 The category for a coding system which has the same code range
6004 as Emacs' internal format. Assigned the coding-system (Lisp
6005 symbol) `emacs-mule' by default.
6007 o coding-category-sjis
6009 The category for a coding system which has the same code range
6010 as SJIS. Assigned the coding-system (Lisp
6011 symbol) `japanese-shift-jis' by default.
6013 o coding-category-iso-7
6015 The category for a coding system which has the same code range
6016 as ISO2022 of 7-bit environment. This doesn't use any locking
6017 shift and single shift functions. This can encode/decode all
6018 charsets. Assigned the coding-system (Lisp symbol)
6019 `iso-2022-7bit' by default.
6021 o coding-category-iso-7-tight
6023 Same as coding-category-iso-7 except that this can
6024 encode/decode only the specified charsets.
6026 o coding-category-iso-8-1
6028 The category for a coding system which has the same code range
6029 as ISO2022 of 8-bit environment and graphic plane 1 used only
6030 for DIMENSION1 charset. This doesn't use any locking shift
6031 and single shift functions. Assigned the coding-system (Lisp
6032 symbol) `iso-latin-1' by default.
6034 o coding-category-iso-8-2
6036 The category for a coding system which has the same code range
6037 as ISO2022 of 8-bit environment and graphic plane 1 used only
6038 for DIMENSION2 charset. This doesn't use any locking shift
6039 and single shift functions. Assigned the coding-system (Lisp
6040 symbol) `japanese-iso-8bit' by default.
6042 o coding-category-iso-7-else
6044 The category for a coding system which has the same code range
6045 as ISO2022 of 7-bit environment but uses locking shift or
6046 single shift functions. Assigned the coding-system (Lisp
6047 symbol) `iso-2022-7bit-lock' by default.
6049 o coding-category-iso-8-else
6051 The category for a coding system which has the same code range
6052 as ISO2022 of 8-bit environment but uses locking shift or
6053 single shift functions. Assigned the coding-system (Lisp
6054 symbol) `iso-2022-8bit-ss2' by default.
6056 o coding-category-big5
6058 The category for a coding system which has the same code range
6059 as BIG5. Assigned the coding-system (Lisp symbol)
6060 `cn-big5' by default.
6062 o coding-category-utf-8
6064 The category for a coding system which has the same code range
6065 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6066 symbol) `utf-8' by default.
6068 o coding-category-utf-16-be
6070 The category for a coding system in which a text has an
6071 Unicode signature (cf. Unicode Standard) in the order of BIG
6072 endian at the head. Assigned the coding-system (Lisp symbol)
6073 `utf-16-be' by default.
6075 o coding-category-utf-16-le
6077 The category for a coding system in which a text has an
6078 Unicode signature (cf. Unicode Standard) in the order of
6079 LITTLE endian at the head. Assigned the coding-system (Lisp
6080 symbol) `utf-16-le' by default.
6082 o coding-category-ccl
6084 The category for a coding system of which encoder/decoder is
6085 written in CCL programs. The default value is nil, i.e., no
6086 coding system is assigned.
6088 o coding-category-binary
6090 The category for a coding system not categorized in any of the
6091 above. Assigned the coding-system (Lisp symbol)
6092 `no-conversion' by default.
6094 Each of them is a Lisp symbol and the value is an actual
6095 `coding-system's (this is also a Lisp symbol) assigned by a user.
6096 What Emacs does actually is to detect a category of coding system.
6097 Then, it uses a `coding-system' assigned to it. If Emacs can't
6098 decide only one possible category, it selects a category of the
6099 highest priority. Priorities of categories are also specified by a
6100 user in a Lisp variable `coding-category-list'.
6104 #define EOL_SEEN_NONE 0
6105 #define EOL_SEEN_LF 1
6106 #define EOL_SEEN_CR 2
6107 #define EOL_SEEN_CRLF 4
6109 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6110 SOURCE is encoded. If CATEGORY is one of
6111 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6112 two-byte, else they are encoded by one-byte.
6114 Return one of EOL_SEEN_XXX. */
6116 #define MAX_EOL_CHECK_COUNT 3
6119 detect_eol (const unsigned char *source
, ptrdiff_t src_bytes
,
6120 enum coding_category category
)
6122 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6125 int eol_seen
= EOL_SEEN_NONE
;
6127 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6129 bool msb
= category
== (coding_category_utf_16_le
6130 | coding_category_utf_16_le_nosig
);
6133 while (src
+ 1 < src_end
)
6136 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6141 this_eol
= EOL_SEEN_LF
;
6142 else if (src
+ 3 >= src_end
6143 || src
[msb
+ 2] != 0
6144 || src
[lsb
+ 2] != '\n')
6145 this_eol
= EOL_SEEN_CR
;
6148 this_eol
= EOL_SEEN_CRLF
;
6152 if (eol_seen
== EOL_SEEN_NONE
)
6153 /* This is the first end-of-line. */
6154 eol_seen
= this_eol
;
6155 else if (eol_seen
!= this_eol
)
6157 /* The found type is different from what found before.
6158 Allow for stray ^M characters in DOS EOL files. */
6159 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6160 || (eol_seen
== EOL_SEEN_CRLF
6161 && this_eol
== EOL_SEEN_CR
))
6162 eol_seen
= EOL_SEEN_CRLF
;
6165 eol_seen
= EOL_SEEN_LF
;
6169 if (++total
== MAX_EOL_CHECK_COUNT
)
6176 while (src
< src_end
)
6179 if (c
== '\n' || c
== '\r')
6184 this_eol
= EOL_SEEN_LF
;
6185 else if (src
>= src_end
|| *src
!= '\n')
6186 this_eol
= EOL_SEEN_CR
;
6188 this_eol
= EOL_SEEN_CRLF
, src
++;
6190 if (eol_seen
== EOL_SEEN_NONE
)
6191 /* This is the first end-of-line. */
6192 eol_seen
= this_eol
;
6193 else if (eol_seen
!= this_eol
)
6195 /* The found type is different from what found before.
6196 Allow for stray ^M characters in DOS EOL files. */
6197 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6198 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6199 eol_seen
= EOL_SEEN_CRLF
;
6202 eol_seen
= EOL_SEEN_LF
;
6206 if (++total
== MAX_EOL_CHECK_COUNT
)
6215 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6217 Lisp_Object eol_type
;
6219 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6220 if (eol_seen
& EOL_SEEN_LF
)
6222 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6225 else if (eol_seen
& EOL_SEEN_CRLF
)
6227 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6230 else if (eol_seen
& EOL_SEEN_CR
)
6232 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6238 /* Detect how a text specified in CODING is encoded. If a coding
6239 system is detected, update fields of CODING by the detected coding
6243 detect_coding (struct coding_system
*coding
)
6245 const unsigned char *src
, *src_end
;
6246 unsigned int saved_mode
= coding
->mode
;
6248 coding
->consumed
= coding
->consumed_char
= 0;
6249 coding
->produced
= coding
->produced_char
= 0;
6250 coding_set_source (coding
);
6252 src_end
= coding
->source
+ coding
->src_bytes
;
6253 coding
->head_ascii
= 0;
6255 /* If we have not yet decided the text encoding type, detect it
6257 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6260 struct coding_detection_info detect_info
;
6261 bool null_byte_found
= 0, eight_bit_found
= 0;
6263 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6264 for (src
= coding
->source
; src
< src_end
; src
++)
6269 eight_bit_found
= 1;
6270 if (null_byte_found
)
6275 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6276 && ! inhibit_iso_escape_detection
6277 && ! detect_info
.checked
)
6279 if (detect_coding_iso_2022 (coding
, &detect_info
))
6281 /* We have scanned the whole data. */
6282 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6284 /* We didn't find an 8-bit code. We may
6285 have found a null-byte, but it's very
6286 rare that a binary file conforms to
6289 coding
->head_ascii
= src
- coding
->source
;
6291 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6295 else if (! c
&& !inhibit_null_byte_detection
)
6297 null_byte_found
= 1;
6298 if (eight_bit_found
)
6301 if (! eight_bit_found
)
6302 coding
->head_ascii
++;
6304 else if (! eight_bit_found
)
6305 coding
->head_ascii
++;
6308 if (null_byte_found
|| eight_bit_found
6309 || coding
->head_ascii
< coding
->src_bytes
6310 || detect_info
.found
)
6312 enum coding_category category
;
6313 struct coding_system
*this;
6315 if (coding
->head_ascii
== coding
->src_bytes
)
6316 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6317 for (i
= 0; i
< coding_category_raw_text
; i
++)
6319 category
= coding_priorities
[i
];
6320 this = coding_categories
+ category
;
6321 if (detect_info
.found
& (1 << category
))
6326 if (null_byte_found
)
6328 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6329 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6331 for (i
= 0; i
< coding_category_raw_text
; i
++)
6333 category
= coding_priorities
[i
];
6334 this = coding_categories
+ category
;
6337 /* No coding system of this category is defined. */
6338 detect_info
.rejected
|= (1 << category
);
6340 else if (category
>= coding_category_raw_text
)
6342 else if (detect_info
.checked
& (1 << category
))
6344 if (detect_info
.found
& (1 << category
))
6347 else if ((*(this->detector
)) (coding
, &detect_info
)
6348 && detect_info
.found
& (1 << category
))
6350 if (category
== coding_category_utf_16_auto
)
6352 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6353 category
= coding_category_utf_16_le
;
6355 category
= coding_category_utf_16_be
;
6362 if (i
< coding_category_raw_text
)
6363 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6364 else if (null_byte_found
)
6365 setup_coding_system (Qno_conversion
, coding
);
6366 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6367 == CATEGORY_MASK_ANY
)
6368 setup_coding_system (Qraw_text
, coding
);
6369 else if (detect_info
.rejected
)
6370 for (i
= 0; i
< coding_category_raw_text
; i
++)
6371 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6373 this = coding_categories
+ coding_priorities
[i
];
6374 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6379 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6380 == coding_category_utf_8_auto
)
6382 Lisp_Object coding_systems
;
6383 struct coding_detection_info detect_info
;
6386 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6387 detect_info
.found
= detect_info
.rejected
= 0;
6388 coding
->head_ascii
= 0;
6389 if (CONSP (coding_systems
)
6390 && detect_coding_utf_8 (coding
, &detect_info
))
6392 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6393 setup_coding_system (XCAR (coding_systems
), coding
);
6395 setup_coding_system (XCDR (coding_systems
), coding
);
6398 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6399 == coding_category_utf_16_auto
)
6401 Lisp_Object coding_systems
;
6402 struct coding_detection_info detect_info
;
6405 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6406 detect_info
.found
= detect_info
.rejected
= 0;
6407 coding
->head_ascii
= 0;
6408 if (CONSP (coding_systems
)
6409 && detect_coding_utf_16 (coding
, &detect_info
))
6411 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6412 setup_coding_system (XCAR (coding_systems
), coding
);
6413 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6414 setup_coding_system (XCDR (coding_systems
), coding
);
6417 coding
->mode
= saved_mode
;
6422 decode_eol (struct coding_system
*coding
)
6424 Lisp_Object eol_type
;
6425 unsigned char *p
, *pbeg
, *pend
;
6427 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6428 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6431 if (NILP (coding
->dst_object
))
6432 pbeg
= coding
->destination
;
6434 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6435 pend
= pbeg
+ coding
->produced
;
6437 if (VECTORP (eol_type
))
6439 int eol_seen
= EOL_SEEN_NONE
;
6441 for (p
= pbeg
; p
< pend
; p
++)
6444 eol_seen
|= EOL_SEEN_LF
;
6445 else if (*p
== '\r')
6447 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6449 eol_seen
|= EOL_SEEN_CRLF
;
6453 eol_seen
|= EOL_SEEN_CR
;
6456 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6457 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6458 && (eol_seen
& EOL_SEEN_CR
) != 0
6459 && (eol_seen
& EOL_SEEN_LF
) == 0)
6460 eol_seen
= EOL_SEEN_CRLF
;
6461 else if (eol_seen
!= EOL_SEEN_NONE
6462 && eol_seen
!= EOL_SEEN_LF
6463 && eol_seen
!= EOL_SEEN_CRLF
6464 && eol_seen
!= EOL_SEEN_CR
)
6465 eol_seen
= EOL_SEEN_LF
;
6466 if (eol_seen
!= EOL_SEEN_NONE
)
6467 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6470 if (EQ (eol_type
, Qmac
))
6472 for (p
= pbeg
; p
< pend
; p
++)
6476 else if (EQ (eol_type
, Qdos
))
6480 if (NILP (coding
->dst_object
))
6482 /* Start deleting '\r' from the tail to minimize the memory
6484 for (p
= pend
- 2; p
>= pbeg
; p
--)
6487 memmove (p
, p
+ 1, pend
-- - p
- 1);
6493 ptrdiff_t pos_byte
= coding
->dst_pos_byte
;
6494 ptrdiff_t pos
= coding
->dst_pos
;
6495 ptrdiff_t pos_end
= pos
+ coding
->produced_char
- 1;
6497 while (pos
< pos_end
)
6499 p
= BYTE_POS_ADDR (pos_byte
);
6500 if (*p
== '\r' && p
[1] == '\n')
6502 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6507 if (coding
->dst_multibyte
)
6508 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6513 coding
->produced
-= n
;
6514 coding
->produced_char
-= n
;
6519 /* Return a translation table (or list of them) from coding system
6520 attribute vector ATTRS for encoding (if ENCODEP) or decoding (if
6524 get_translation_table (Lisp_Object attrs
, bool encodep
, int *max_lookup
)
6526 Lisp_Object standard
, translation_table
;
6529 if (NILP (Venable_character_translation
))
6536 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6537 standard
= Vstandard_translation_table_for_encode
;
6539 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6540 standard
= Vstandard_translation_table_for_decode
;
6541 if (NILP (translation_table
))
6542 translation_table
= standard
;
6545 if (SYMBOLP (translation_table
))
6546 translation_table
= Fget (translation_table
, Qtranslation_table
);
6547 else if (CONSP (translation_table
))
6549 translation_table
= Fcopy_sequence (translation_table
);
6550 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6551 if (SYMBOLP (XCAR (val
)))
6552 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6554 if (CHAR_TABLE_P (standard
))
6556 if (CONSP (translation_table
))
6557 translation_table
= nconc2 (translation_table
,
6558 Fcons (standard
, Qnil
));
6560 translation_table
= Fcons (translation_table
,
6561 Fcons (standard
, Qnil
));
6568 if (CHAR_TABLE_P (translation_table
)
6569 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6571 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6572 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6573 *max_lookup
= XFASTINT (val
);
6575 else if (CONSP (translation_table
))
6579 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6580 if (CHAR_TABLE_P (XCAR (tail
))
6581 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6583 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6584 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6585 *max_lookup
= XFASTINT (tailval
);
6589 return translation_table
;
6592 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6595 if (CHAR_TABLE_P (table)) \
6597 trans = CHAR_TABLE_REF (table, c); \
6598 if (CHARACTERP (trans)) \
6599 c = XFASTINT (trans), trans = Qnil; \
6601 else if (CONSP (table)) \
6605 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6606 if (CHAR_TABLE_P (XCAR (tail))) \
6608 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6609 if (CHARACTERP (trans)) \
6610 c = XFASTINT (trans), trans = Qnil; \
6611 else if (! NILP (trans)) \
6618 /* Return a translation of character(s) at BUF according to TRANS.
6619 TRANS is TO-CHAR or ((FROM . TO) ...) where
6620 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6621 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6622 translation is found, and Qnil if not found..
6623 If BUF is too short to lookup characters in FROM, return Qt. */
6626 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6629 if (INTEGERP (trans
))
6631 for (; CONSP (trans
); trans
= XCDR (trans
))
6633 Lisp_Object val
= XCAR (trans
);
6634 Lisp_Object from
= XCAR (val
);
6635 ptrdiff_t len
= ASIZE (from
);
6638 for (i
= 0; i
< len
; i
++)
6640 if (buf
+ i
== buf_end
)
6642 if (XINT (AREF (from
, i
)) != buf
[i
])
6653 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6656 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6657 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6659 ptrdiff_t produced_chars
= 0;
6662 if (! coding
->chars_at_source
)
6664 /* Source characters are in coding->charbuf. */
6665 int *buf
= coding
->charbuf
;
6666 int *buf_end
= buf
+ coding
->charbuf_used
;
6668 if (EQ (coding
->src_object
, coding
->dst_object
))
6670 coding_set_source (coding
);
6671 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6674 while (buf
< buf_end
)
6681 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
6682 Lisp_Object trans
= Qnil
;
6684 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6687 trans
= get_translation (trans
, buf
, buf_end
);
6688 if (INTEGERP (trans
))
6690 else if (CONSP (trans
))
6692 from_nchars
= ASIZE (XCAR (trans
));
6693 trans
= XCDR (trans
);
6694 if (INTEGERP (trans
))
6698 to_nchars
= ASIZE (trans
);
6699 c
= XINT (AREF (trans
, 0));
6702 else if (EQ (trans
, Qt
) && ! last_block
)
6706 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
6708 if (((min (PTRDIFF_MAX
, SIZE_MAX
) - (buf_end
- buf
))
6709 / MAX_MULTIBYTE_LENGTH
)
6711 memory_full (SIZE_MAX
);
6712 dst
= alloc_destination (coding
,
6714 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6716 if (EQ (coding
->src_object
, coding
->dst_object
))
6718 coding_set_source (coding
);
6719 dst_end
= (((unsigned char *) coding
->source
)
6720 + coding
->consumed
);
6723 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6726 for (i
= 0; i
< to_nchars
; i
++)
6729 c
= XINT (AREF (trans
, i
));
6730 if (coding
->dst_multibyte
6731 || ! CHAR_BYTE8_P (c
))
6732 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6734 *dst
++ = CHAR_TO_BYTE8 (c
);
6736 produced_chars
+= to_nchars
;
6740 /* This is an annotation datum. (-C) is the length. */
6743 carryover
= buf_end
- buf
;
6747 /* Source characters are at coding->source. */
6748 const unsigned char *src
= coding
->source
;
6749 const unsigned char *src_end
= src
+ coding
->consumed
;
6751 if (EQ (coding
->dst_object
, coding
->src_object
))
6752 dst_end
= (unsigned char *) src
;
6753 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6755 if (coding
->src_multibyte
)
6757 bool multibytep
= 1;
6758 ptrdiff_t consumed_chars
= 0;
6762 const unsigned char *src_base
= src
;
6768 if (EQ (coding
->src_object
, coding
->dst_object
))
6769 dst_end
= (unsigned char *) src
;
6772 ptrdiff_t offset
= src
- coding
->source
;
6774 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6776 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6777 coding_set_source (coding
);
6778 src
= coding
->source
+ offset
;
6779 src_end
= coding
->source
+ coding
->consumed
;
6780 if (EQ (coding
->src_object
, coding
->dst_object
))
6781 dst_end
= (unsigned char *) src
;
6791 while (src
< src_end
)
6793 bool multibytep
= 1;
6796 if (dst
>= dst_end
- 1)
6798 if (EQ (coding
->src_object
, coding
->dst_object
))
6799 dst_end
= (unsigned char *) src
;
6800 if (dst
>= dst_end
- 1)
6802 ptrdiff_t offset
= src
- coding
->source
;
6803 ptrdiff_t more_bytes
;
6805 if (EQ (coding
->src_object
, coding
->dst_object
))
6806 more_bytes
= ((src_end
- src
) / 2) + 2;
6808 more_bytes
= src_end
- src
+ 2;
6809 dst
= alloc_destination (coding
, more_bytes
, dst
);
6810 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6811 coding_set_source (coding
);
6812 src
= coding
->source
+ offset
;
6813 src_end
= coding
->source
+ coding
->consumed
;
6814 if (EQ (coding
->src_object
, coding
->dst_object
))
6815 dst_end
= (unsigned char *) src
;
6823 if (!EQ (coding
->src_object
, coding
->dst_object
))
6825 ptrdiff_t require
= coding
->src_bytes
- coding
->dst_bytes
;
6829 ptrdiff_t offset
= src
- coding
->source
;
6831 dst
= alloc_destination (coding
, require
, dst
);
6832 coding_set_source (coding
);
6833 src
= coding
->source
+ offset
;
6834 src_end
= coding
->source
+ coding
->consumed
;
6837 produced_chars
= coding
->consumed_char
;
6838 while (src
< src_end
)
6843 produced
= dst
- (coding
->destination
+ coding
->produced
);
6844 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6845 insert_from_gap (produced_chars
, produced
);
6846 coding
->produced
+= produced
;
6847 coding
->produced_char
+= produced_chars
;
6851 /* Compose text in CODING->object according to the annotation data at
6852 CHARBUF. CHARBUF is an array:
6853 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6857 produce_composition (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6861 enum composition_method method
;
6862 Lisp_Object components
;
6864 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6865 to
= pos
+ charbuf
[2];
6866 method
= (enum composition_method
) (charbuf
[4]);
6868 if (method
== COMPOSITION_RELATIVE
)
6872 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6875 if (method
== COMPOSITION_WITH_RULE
)
6876 len
= charbuf
[2] * 3 - 2;
6877 charbuf
+= MAX_ANNOTATION_LENGTH
;
6878 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6879 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6881 if (charbuf
[i
] >= 0)
6882 args
[j
] = make_number (charbuf
[i
]);
6886 args
[j
] = make_number (charbuf
[i
] % 0x100);
6889 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6891 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6895 /* Put `charset' property on text in CODING->object according to
6896 the annotation data at CHARBUF. CHARBUF is an array:
6897 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6901 produce_charset (struct coding_system
*coding
, int *charbuf
, ptrdiff_t pos
)
6903 ptrdiff_t from
= pos
- charbuf
[2];
6904 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6906 Fput_text_property (make_number (from
), make_number (pos
),
6907 Qcharset
, CHARSET_NAME (charset
),
6908 coding
->dst_object
);
6912 #define CHARBUF_SIZE 0x4000
6914 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6916 int size = CHARBUF_SIZE; \
6918 coding->charbuf = NULL; \
6919 while (size > 1024) \
6921 coding->charbuf = alloca (sizeof (int) * size); \
6922 if (coding->charbuf) \
6926 if (! coding->charbuf) \
6928 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6931 coding->charbuf_size = size; \
6936 produce_annotation (struct coding_system
*coding
, ptrdiff_t pos
)
6938 int *charbuf
= coding
->charbuf
;
6939 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6941 if (NILP (coding
->dst_object
))
6944 while (charbuf
< charbuf_end
)
6950 int len
= -*charbuf
;
6955 case CODING_ANNOTATE_COMPOSITION_MASK
:
6956 produce_composition (coding
, charbuf
, pos
);
6958 case CODING_ANNOTATE_CHARSET_MASK
:
6959 produce_charset (coding
, charbuf
, pos
);
6967 /* Decode the data at CODING->src_object into CODING->dst_object.
6968 CODING->src_object is a buffer, a string, or nil.
6969 CODING->dst_object is a buffer.
6971 If CODING->src_object is a buffer, it must be the current buffer.
6972 In this case, if CODING->src_pos is positive, it is a position of
6973 the source text in the buffer, otherwise, the source text is in the
6974 gap area of the buffer, and CODING->src_pos specifies the offset of
6975 the text from GPT (which must be the same as PT). If this is the
6976 same buffer as CODING->dst_object, CODING->src_pos must be
6979 If CODING->src_object is a string, CODING->src_pos is an index to
6982 If CODING->src_object is nil, CODING->source must already point to
6983 the non-relocatable memory area. In this case, CODING->src_pos is
6984 an offset from CODING->source.
6986 The decoded data is inserted at the current point of the buffer
6991 decode_coding (struct coding_system
*coding
)
6994 Lisp_Object undo_list
;
6995 Lisp_Object translation_table
;
6996 struct ccl_spec cclspec
;
7000 if (BUFFERP (coding
->src_object
)
7001 && coding
->src_pos
> 0
7002 && coding
->src_pos
< GPT
7003 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7004 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7007 if (BUFFERP (coding
->dst_object
))
7009 if (current_buffer
!= XBUFFER (coding
->dst_object
))
7010 set_buffer_internal (XBUFFER (coding
->dst_object
));
7012 move_gap_both (PT
, PT_BYTE
);
7014 /* We must disable undo_list in order to record the whole insert
7015 transaction via record_insert at the end. But doing so also
7016 disables the recording of the first change to the undo_list.
7017 Therefore we check for first change here and record it via
7018 record_first_change if needed. */
7019 if (MODIFF
<= SAVE_MODIFF
)
7020 record_first_change ();
7022 undo_list
= BVAR (current_buffer
, undo_list
);
7023 bset_undo_list (current_buffer
, Qt
);
7026 coding
->consumed
= coding
->consumed_char
= 0;
7027 coding
->produced
= coding
->produced_char
= 0;
7028 coding
->chars_at_source
= 0;
7029 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7032 ALLOC_CONVERSION_WORK_AREA (coding
);
7034 attrs
= CODING_ID_ATTRS (coding
->id
);
7035 translation_table
= get_translation_table (attrs
, 0, NULL
);
7038 if (coding
->decoder
== decode_coding_ccl
)
7040 coding
->spec
.ccl
= &cclspec
;
7041 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7045 ptrdiff_t pos
= coding
->dst_pos
+ coding
->produced_char
;
7047 coding_set_source (coding
);
7048 coding
->annotated
= 0;
7049 coding
->charbuf_used
= carryover
;
7050 (*(coding
->decoder
)) (coding
);
7051 coding_set_destination (coding
);
7052 carryover
= produce_chars (coding
, translation_table
, 0);
7053 if (coding
->annotated
)
7054 produce_annotation (coding
, pos
);
7055 for (i
= 0; i
< carryover
; i
++)
7057 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7059 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7060 || (coding
->consumed
< coding
->src_bytes
7061 && (coding
->result
== CODING_RESULT_SUCCESS
7062 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7066 coding_set_destination (coding
);
7067 coding
->charbuf_used
= carryover
;
7068 produce_chars (coding
, translation_table
, 1);
7071 coding
->carryover_bytes
= 0;
7072 if (coding
->consumed
< coding
->src_bytes
)
7074 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7075 const unsigned char *src
;
7077 coding_set_source (coding
);
7078 coding_set_destination (coding
);
7079 src
= coding
->source
+ coding
->consumed
;
7081 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7083 /* Flush out unprocessed data as binary chars. We are sure
7084 that the number of data is less than the size of
7086 coding
->charbuf_used
= 0;
7087 coding
->chars_at_source
= 0;
7089 while (nbytes
-- > 0)
7094 c
= BYTE8_TO_CHAR (c
);
7095 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7097 produce_chars (coding
, Qnil
, 1);
7101 /* Record unprocessed bytes in coding->carryover. We are
7102 sure that the number of data is less than the size of
7103 coding->carryover. */
7104 unsigned char *p
= coding
->carryover
;
7106 if (nbytes
> sizeof coding
->carryover
)
7107 nbytes
= sizeof coding
->carryover
;
7108 coding
->carryover_bytes
= nbytes
;
7109 while (nbytes
-- > 0)
7112 coding
->consumed
= coding
->src_bytes
;
7115 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7116 && !inhibit_eol_conversion
)
7117 decode_eol (coding
);
7118 if (BUFFERP (coding
->dst_object
))
7120 bset_undo_list (current_buffer
, undo_list
);
7121 record_insert (coding
->dst_pos
, coding
->produced_char
);
7126 /* Extract an annotation datum from a composition starting at POS and
7127 ending before LIMIT of CODING->src_object (buffer or string), store
7128 the data in BUF, set *STOP to a starting position of the next
7129 composition (if any) or to LIMIT, and return the address of the
7130 next element of BUF.
7132 If such an annotation is not found, set *STOP to a starting
7133 position of a composition after POS (if any) or to LIMIT, and
7137 handle_composition_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7138 struct coding_system
*coding
, int *buf
,
7141 ptrdiff_t start
, end
;
7144 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7147 else if (start
> pos
)
7153 /* We found a composition. Store the corresponding
7154 annotation data in BUF. */
7156 enum composition_method method
= COMPOSITION_METHOD (prop
);
7157 int nchars
= COMPOSITION_LENGTH (prop
);
7159 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7160 if (method
!= COMPOSITION_RELATIVE
)
7162 Lisp_Object components
;
7163 ptrdiff_t i
, len
, i_byte
;
7165 components
= COMPOSITION_COMPONENTS (prop
);
7166 if (VECTORP (components
))
7168 len
= ASIZE (components
);
7169 for (i
= 0; i
< len
; i
++)
7170 *buf
++ = XINT (AREF (components
, i
));
7172 else if (STRINGP (components
))
7174 len
= SCHARS (components
);
7178 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7182 else if (INTEGERP (components
))
7185 *buf
++ = XINT (components
);
7187 else if (CONSP (components
))
7189 for (len
= 0; CONSP (components
);
7190 len
++, components
= XCDR (components
))
7191 *buf
++ = XINT (XCAR (components
));
7199 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7210 /* Extract an annotation datum from a text property `charset' at POS of
7211 CODING->src_object (buffer of string), store the data in BUF, set
7212 *STOP to the position where the value of `charset' property changes
7213 (limiting by LIMIT), and return the address of the next element of
7216 If the property value is nil, set *STOP to the position where the
7217 property value is non-nil (limiting by LIMIT), and return BUF. */
7220 handle_charset_annotation (ptrdiff_t pos
, ptrdiff_t limit
,
7221 struct coding_system
*coding
, int *buf
,
7224 Lisp_Object val
, next
;
7227 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7228 if (! NILP (val
) && CHARSETP (val
))
7229 id
= XINT (CHARSET_SYMBOL_ID (val
));
7232 ADD_CHARSET_DATA (buf
, 0, id
);
7233 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7235 make_number (limit
));
7236 *stop
= XINT (next
);
7242 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7245 int *buf
= coding
->charbuf
;
7246 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7247 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7248 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7249 ptrdiff_t pos
= coding
->src_pos
+ coding
->consumed_char
;
7250 ptrdiff_t end_pos
= coding
->src_pos
+ coding
->src_chars
;
7251 bool multibytep
= coding
->src_multibyte
;
7252 Lisp_Object eol_type
;
7254 ptrdiff_t stop
, stop_composition
, stop_charset
;
7255 int *lookup_buf
= NULL
;
7257 if (! NILP (translation_table
))
7258 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7260 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7261 if (VECTORP (eol_type
))
7264 /* Note: composition handling is not yet implemented. */
7265 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7267 if (NILP (coding
->src_object
))
7268 stop
= stop_composition
= stop_charset
= end_pos
;
7271 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7272 stop
= stop_composition
= pos
;
7274 stop
= stop_composition
= end_pos
;
7275 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7276 stop
= stop_charset
= pos
;
7278 stop_charset
= end_pos
;
7281 /* Compensate for CRLF and conversion. */
7282 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7283 while (buf
< buf_end
)
7291 if (pos
== stop_composition
)
7292 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7293 buf
, &stop_composition
);
7294 if (pos
== stop_charset
)
7295 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7296 buf
, &stop_charset
);
7297 stop
= (stop_composition
< stop_charset
7298 ? stop_composition
: stop_charset
);
7305 if (coding
->encoder
== encode_coding_raw_text
7306 || coding
->encoder
== encode_coding_ccl
)
7308 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7309 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7311 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7314 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7315 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7317 if (! EQ (eol_type
, Qunix
))
7321 if (EQ (eol_type
, Qdos
))
7329 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7334 ptrdiff_t from_nchars
= 1, to_nchars
= 1;
7335 int *lookup_buf_end
;
7336 const unsigned char *p
= src
;
7340 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7341 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7342 lookup_buf_end
= lookup_buf
+ i
;
7343 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7344 if (INTEGERP (trans
))
7346 else if (CONSP (trans
))
7348 from_nchars
= ASIZE (XCAR (trans
));
7349 trans
= XCDR (trans
);
7350 if (INTEGERP (trans
))
7354 to_nchars
= ASIZE (trans
);
7355 if (buf_end
- buf
< to_nchars
)
7357 c
= XINT (AREF (trans
, 0));
7363 for (i
= 1; i
< to_nchars
; i
++)
7364 *buf
++ = XINT (AREF (trans
, i
));
7365 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7366 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7370 coding
->consumed
= src
- coding
->source
;
7371 coding
->consumed_char
= pos
- coding
->src_pos
;
7372 coding
->charbuf_used
= buf
- coding
->charbuf
;
7373 coding
->chars_at_source
= 0;
7377 /* Encode the text at CODING->src_object into CODING->dst_object.
7378 CODING->src_object is a buffer or a string.
7379 CODING->dst_object is a buffer or nil.
7381 If CODING->src_object is a buffer, it must be the current buffer.
7382 In this case, if CODING->src_pos is positive, it is a position of
7383 the source text in the buffer, otherwise. the source text is in the
7384 gap area of the buffer, and coding->src_pos specifies the offset of
7385 the text from GPT (which must be the same as PT). If this is the
7386 same buffer as CODING->dst_object, CODING->src_pos must be
7387 negative and CODING should not have `pre-write-conversion'.
7389 If CODING->src_object is a string, CODING should not have
7390 `pre-write-conversion'.
7392 If CODING->dst_object is a buffer, the encoded data is inserted at
7393 the current point of that buffer.
7395 If CODING->dst_object is nil, the encoded data is placed at the
7396 memory area specified by CODING->destination. */
7399 encode_coding (struct coding_system
*coding
)
7402 Lisp_Object translation_table
;
7404 struct ccl_spec cclspec
;
7406 attrs
= CODING_ID_ATTRS (coding
->id
);
7407 if (coding
->encoder
== encode_coding_raw_text
)
7408 translation_table
= Qnil
, max_lookup
= 0;
7410 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7412 if (BUFFERP (coding
->dst_object
))
7414 set_buffer_internal (XBUFFER (coding
->dst_object
));
7415 coding
->dst_multibyte
7416 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7419 coding
->consumed
= coding
->consumed_char
= 0;
7420 coding
->produced
= coding
->produced_char
= 0;
7421 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7424 ALLOC_CONVERSION_WORK_AREA (coding
);
7426 if (coding
->encoder
== encode_coding_ccl
)
7428 coding
->spec
.ccl
= &cclspec
;
7429 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7432 coding_set_source (coding
);
7433 consume_chars (coding
, translation_table
, max_lookup
);
7434 coding_set_destination (coding
);
7435 (*(coding
->encoder
)) (coding
);
7436 } while (coding
->consumed_char
< coding
->src_chars
);
7438 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7439 insert_from_gap (coding
->produced_char
, coding
->produced
);
7443 /* Name (or base name) of work buffer for code conversion. */
7444 static Lisp_Object Vcode_conversion_workbuf_name
;
7446 /* A working buffer used by the top level conversion. Once it is
7447 created, it is never destroyed. It has the name
7448 Vcode_conversion_workbuf_name. The other working buffers are
7449 destroyed after the use is finished, and their names are modified
7450 versions of Vcode_conversion_workbuf_name. */
7451 static Lisp_Object Vcode_conversion_reused_workbuf
;
7453 /* True iff Vcode_conversion_reused_workbuf is already in use. */
7454 static bool reused_workbuf_in_use
;
7457 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7458 multibyteness of returning buffer. */
7461 make_conversion_work_buffer (bool multibyte
)
7463 Lisp_Object name
, workbuf
;
7464 struct buffer
*current
;
7466 if (reused_workbuf_in_use
)
7468 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7469 workbuf
= Fget_buffer_create (name
);
7473 reused_workbuf_in_use
= 1;
7474 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7475 Vcode_conversion_reused_workbuf
7476 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7477 workbuf
= Vcode_conversion_reused_workbuf
;
7479 current
= current_buffer
;
7480 set_buffer_internal (XBUFFER (workbuf
));
7481 /* We can't allow modification hooks to run in the work buffer. For
7482 instance, directory_files_internal assumes that file decoding
7483 doesn't compile new regexps. */
7484 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7486 bset_undo_list (current_buffer
, Qt
);
7487 bset_enable_multibyte_characters (current_buffer
, multibyte
? Qt
: Qnil
);
7488 set_buffer_internal (current
);
7494 code_conversion_restore (Lisp_Object arg
)
7496 Lisp_Object current
, workbuf
;
7497 struct gcpro gcpro1
;
7500 current
= XCAR (arg
);
7501 workbuf
= XCDR (arg
);
7502 if (! NILP (workbuf
))
7504 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7505 reused_workbuf_in_use
= 0;
7507 Fkill_buffer (workbuf
);
7509 set_buffer_internal (XBUFFER (current
));
7515 code_conversion_save (bool with_work_buf
, bool multibyte
)
7517 Lisp_Object workbuf
= Qnil
;
7520 workbuf
= make_conversion_work_buffer (multibyte
);
7521 record_unwind_protect (code_conversion_restore
,
7522 Fcons (Fcurrent_buffer (), workbuf
));
7527 decode_coding_gap (struct coding_system
*coding
,
7528 ptrdiff_t chars
, ptrdiff_t bytes
)
7530 ptrdiff_t count
= SPECPDL_INDEX ();
7533 code_conversion_save (0, 0);
7535 coding
->src_object
= Fcurrent_buffer ();
7536 coding
->src_chars
= chars
;
7537 coding
->src_bytes
= bytes
;
7538 coding
->src_pos
= -chars
;
7539 coding
->src_pos_byte
= -bytes
;
7540 coding
->src_multibyte
= chars
< bytes
;
7541 coding
->dst_object
= coding
->src_object
;
7542 coding
->dst_pos
= PT
;
7543 coding
->dst_pos_byte
= PT_BYTE
;
7544 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7546 if (CODING_REQUIRE_DETECTION (coding
))
7547 detect_coding (coding
);
7549 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7550 current_buffer
->text
->inhibit_shrinking
= 1;
7551 decode_coding (coding
);
7552 current_buffer
->text
->inhibit_shrinking
= 0;
7554 attrs
= CODING_ID_ATTRS (coding
->id
);
7555 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7557 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7560 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7561 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7562 make_number (coding
->produced_char
));
7564 coding
->produced_char
+= Z
- prev_Z
;
7565 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7568 unbind_to (count
, Qnil
);
7572 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7573 SRC_OBJECT into DST_OBJECT by coding context CODING.
7575 SRC_OBJECT is a buffer, a string, or Qnil.
7577 If it is a buffer, the text is at point of the buffer. FROM and TO
7578 are positions in the buffer.
7580 If it is a string, the text is at the beginning of the string.
7581 FROM and TO are indices to the string.
7583 If it is nil, the text is at coding->source. FROM and TO are
7584 indices to coding->source.
7586 DST_OBJECT is a buffer, Qt, or Qnil.
7588 If it is a buffer, the decoded text is inserted at point of the
7589 buffer. If the buffer is the same as SRC_OBJECT, the source text
7592 If it is Qt, a string is made from the decoded text, and
7593 set in CODING->dst_object.
7595 If it is Qnil, the decoded text is stored at CODING->destination.
7596 The caller must allocate CODING->dst_bytes bytes at
7597 CODING->destination by xmalloc. If the decoded text is longer than
7598 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7602 decode_coding_object (struct coding_system
*coding
,
7603 Lisp_Object src_object
,
7604 ptrdiff_t from
, ptrdiff_t from_byte
,
7605 ptrdiff_t to
, ptrdiff_t to_byte
,
7606 Lisp_Object dst_object
)
7608 ptrdiff_t count
= SPECPDL_INDEX ();
7609 unsigned char *destination
IF_LINT (= NULL
);
7610 ptrdiff_t dst_bytes
IF_LINT (= 0);
7611 ptrdiff_t chars
= to
- from
;
7612 ptrdiff_t bytes
= to_byte
- from_byte
;
7614 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7615 bool need_marker_adjustment
= 0;
7616 Lisp_Object old_deactivate_mark
;
7618 old_deactivate_mark
= Vdeactivate_mark
;
7620 if (NILP (dst_object
))
7622 destination
= coding
->destination
;
7623 dst_bytes
= coding
->dst_bytes
;
7626 coding
->src_object
= src_object
;
7627 coding
->src_chars
= chars
;
7628 coding
->src_bytes
= bytes
;
7629 coding
->src_multibyte
= chars
< bytes
;
7631 if (STRINGP (src_object
))
7633 coding
->src_pos
= from
;
7634 coding
->src_pos_byte
= from_byte
;
7636 else if (BUFFERP (src_object
))
7638 set_buffer_internal (XBUFFER (src_object
));
7640 move_gap_both (from
, from_byte
);
7641 if (EQ (src_object
, dst_object
))
7643 struct Lisp_Marker
*tail
;
7645 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7647 tail
->need_adjustment
7648 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7649 need_marker_adjustment
|= tail
->need_adjustment
;
7651 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7652 TEMP_SET_PT_BOTH (from
, from_byte
);
7653 current_buffer
->text
->inhibit_shrinking
= 1;
7654 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7655 coding
->src_pos
= -chars
;
7656 coding
->src_pos_byte
= -bytes
;
7660 coding
->src_pos
= from
;
7661 coding
->src_pos_byte
= from_byte
;
7665 if (CODING_REQUIRE_DETECTION (coding
))
7666 detect_coding (coding
);
7667 attrs
= CODING_ID_ATTRS (coding
->id
);
7669 if (EQ (dst_object
, Qt
)
7670 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7671 && NILP (dst_object
)))
7673 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7674 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7675 coding
->dst_pos
= BEG
;
7676 coding
->dst_pos_byte
= BEG_BYTE
;
7678 else if (BUFFERP (dst_object
))
7680 code_conversion_save (0, 0);
7681 coding
->dst_object
= dst_object
;
7682 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7683 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7684 coding
->dst_multibyte
7685 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7689 code_conversion_save (0, 0);
7690 coding
->dst_object
= Qnil
;
7691 /* Most callers presume this will return a multibyte result, and they
7692 won't use `binary' or `raw-text' anyway, so let's not worry about
7693 CODING_FOR_UNIBYTE. */
7694 coding
->dst_multibyte
= 1;
7697 decode_coding (coding
);
7699 if (BUFFERP (coding
->dst_object
))
7700 set_buffer_internal (XBUFFER (coding
->dst_object
));
7702 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7704 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7705 ptrdiff_t prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7708 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7709 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7710 old_deactivate_mark
);
7711 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7712 make_number (coding
->produced_char
));
7715 coding
->produced_char
+= Z
- prev_Z
;
7716 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7719 if (EQ (dst_object
, Qt
))
7721 coding
->dst_object
= Fbuffer_string ();
7723 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7725 set_buffer_internal (XBUFFER (coding
->dst_object
));
7726 if (dst_bytes
< coding
->produced
)
7728 destination
= xrealloc (destination
, coding
->produced
);
7731 record_conversion_result (coding
,
7732 CODING_RESULT_INSUFFICIENT_MEM
);
7733 unbind_to (count
, Qnil
);
7736 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7737 move_gap_both (BEGV
, BEGV_BYTE
);
7738 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7739 coding
->destination
= destination
;
7745 /* This is the case of:
7746 (BUFFERP (src_object) && EQ (src_object, dst_object))
7747 As we have moved PT while replacing the original buffer
7748 contents, we must recover it now. */
7749 set_buffer_internal (XBUFFER (src_object
));
7750 current_buffer
->text
->inhibit_shrinking
= 0;
7751 if (saved_pt
< from
)
7752 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7753 else if (saved_pt
< from
+ chars
)
7754 TEMP_SET_PT_BOTH (from
, from_byte
);
7755 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7756 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7757 saved_pt_byte
+ (coding
->produced
- bytes
));
7759 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7760 saved_pt_byte
+ (coding
->produced
- bytes
));
7762 if (need_marker_adjustment
)
7764 struct Lisp_Marker
*tail
;
7766 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7767 if (tail
->need_adjustment
)
7769 tail
->need_adjustment
= 0;
7770 if (tail
->insertion_type
)
7772 tail
->bytepos
= from_byte
;
7773 tail
->charpos
= from
;
7777 tail
->bytepos
= from_byte
+ coding
->produced
;
7779 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7780 ? tail
->bytepos
: from
+ coding
->produced_char
);
7786 Vdeactivate_mark
= old_deactivate_mark
;
7787 unbind_to (count
, coding
->dst_object
);
7792 encode_coding_object (struct coding_system
*coding
,
7793 Lisp_Object src_object
,
7794 ptrdiff_t from
, ptrdiff_t from_byte
,
7795 ptrdiff_t to
, ptrdiff_t to_byte
,
7796 Lisp_Object dst_object
)
7798 ptrdiff_t count
= SPECPDL_INDEX ();
7799 ptrdiff_t chars
= to
- from
;
7800 ptrdiff_t bytes
= to_byte
- from_byte
;
7802 ptrdiff_t saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7803 bool need_marker_adjustment
= 0;
7804 bool kill_src_buffer
= 0;
7805 Lisp_Object old_deactivate_mark
;
7807 old_deactivate_mark
= Vdeactivate_mark
;
7809 coding
->src_object
= src_object
;
7810 coding
->src_chars
= chars
;
7811 coding
->src_bytes
= bytes
;
7812 coding
->src_multibyte
= chars
< bytes
;
7814 attrs
= CODING_ID_ATTRS (coding
->id
);
7816 if (EQ (src_object
, dst_object
))
7818 struct Lisp_Marker
*tail
;
7820 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7822 tail
->need_adjustment
7823 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7824 need_marker_adjustment
|= tail
->need_adjustment
;
7828 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7830 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7831 set_buffer_internal (XBUFFER (coding
->src_object
));
7832 if (STRINGP (src_object
))
7833 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7834 else if (BUFFERP (src_object
))
7835 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7837 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7839 if (EQ (src_object
, dst_object
))
7841 set_buffer_internal (XBUFFER (src_object
));
7842 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7843 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7844 set_buffer_internal (XBUFFER (coding
->src_object
));
7848 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7850 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7851 old_deactivate_mark
);
7852 safe_call2 (CODING_ATTR_PRE_WRITE (attrs
),
7853 make_number (BEG
), make_number (Z
));
7856 if (XBUFFER (coding
->src_object
) != current_buffer
)
7857 kill_src_buffer
= 1;
7858 coding
->src_object
= Fcurrent_buffer ();
7860 move_gap_both (BEG
, BEG_BYTE
);
7861 coding
->src_chars
= Z
- BEG
;
7862 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7863 coding
->src_pos
= BEG
;
7864 coding
->src_pos_byte
= BEG_BYTE
;
7865 coding
->src_multibyte
= Z
< Z_BYTE
;
7867 else if (STRINGP (src_object
))
7869 code_conversion_save (0, 0);
7870 coding
->src_pos
= from
;
7871 coding
->src_pos_byte
= from_byte
;
7873 else if (BUFFERP (src_object
))
7875 code_conversion_save (0, 0);
7876 set_buffer_internal (XBUFFER (src_object
));
7877 if (EQ (src_object
, dst_object
))
7879 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7880 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7881 coding
->src_pos
= 0;
7882 coding
->src_pos_byte
= 0;
7886 if (from
< GPT
&& to
>= GPT
)
7887 move_gap_both (from
, from_byte
);
7888 coding
->src_pos
= from
;
7889 coding
->src_pos_byte
= from_byte
;
7893 code_conversion_save (0, 0);
7895 if (BUFFERP (dst_object
))
7897 coding
->dst_object
= dst_object
;
7898 if (EQ (src_object
, dst_object
))
7900 coding
->dst_pos
= from
;
7901 coding
->dst_pos_byte
= from_byte
;
7905 struct buffer
*current
= current_buffer
;
7907 set_buffer_temp (XBUFFER (dst_object
));
7908 coding
->dst_pos
= PT
;
7909 coding
->dst_pos_byte
= PT_BYTE
;
7910 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7911 set_buffer_temp (current
);
7913 coding
->dst_multibyte
7914 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7916 else if (EQ (dst_object
, Qt
))
7918 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
7919 coding
->dst_object
= Qnil
;
7920 coding
->destination
= xmalloc (dst_bytes
);
7921 coding
->dst_bytes
= dst_bytes
;
7922 coding
->dst_multibyte
= 0;
7926 coding
->dst_object
= Qnil
;
7927 coding
->dst_multibyte
= 0;
7930 encode_coding (coding
);
7932 if (EQ (dst_object
, Qt
))
7934 if (BUFFERP (coding
->dst_object
))
7935 coding
->dst_object
= Fbuffer_string ();
7939 = make_unibyte_string ((char *) coding
->destination
,
7941 xfree (coding
->destination
);
7947 /* This is the case of:
7948 (BUFFERP (src_object) && EQ (src_object, dst_object))
7949 As we have moved PT while replacing the original buffer
7950 contents, we must recover it now. */
7951 set_buffer_internal (XBUFFER (src_object
));
7952 if (saved_pt
< from
)
7953 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7954 else if (saved_pt
< from
+ chars
)
7955 TEMP_SET_PT_BOTH (from
, from_byte
);
7956 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7957 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7958 saved_pt_byte
+ (coding
->produced
- bytes
));
7960 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7961 saved_pt_byte
+ (coding
->produced
- bytes
));
7963 if (need_marker_adjustment
)
7965 struct Lisp_Marker
*tail
;
7967 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7968 if (tail
->need_adjustment
)
7970 tail
->need_adjustment
= 0;
7971 if (tail
->insertion_type
)
7973 tail
->bytepos
= from_byte
;
7974 tail
->charpos
= from
;
7978 tail
->bytepos
= from_byte
+ coding
->produced
;
7980 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7981 ? tail
->bytepos
: from
+ coding
->produced_char
);
7987 if (kill_src_buffer
)
7988 Fkill_buffer (coding
->src_object
);
7990 Vdeactivate_mark
= old_deactivate_mark
;
7991 unbind_to (count
, Qnil
);
7996 preferred_coding_system (void)
7998 int id
= coding_categories
[coding_priorities
[0]].id
;
8000 return CODING_ID_NAME (id
);
8005 /*** 8. Emacs Lisp library functions ***/
8007 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8008 doc
: /* Return t if OBJECT is nil or a coding-system.
8009 See the documentation of `define-coding-system' for information
8010 about coding-system objects. */)
8011 (Lisp_Object object
)
8014 || CODING_SYSTEM_ID (object
) >= 0)
8016 if (! SYMBOLP (object
)
8017 || NILP (Fget (object
, Qcoding_system_define_form
)))
8022 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8023 Sread_non_nil_coding_system
, 1, 1, 0,
8024 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8025 (Lisp_Object prompt
)
8030 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8031 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8033 while (SCHARS (val
) == 0);
8034 return (Fintern (val
, Qnil
));
8037 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8038 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8039 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8040 Ignores case when completing coding systems (all Emacs coding systems
8041 are lower-case). */)
8042 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8045 ptrdiff_t count
= SPECPDL_INDEX ();
8047 if (SYMBOLP (default_coding_system
))
8048 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8049 specbind (Qcompletion_ignore_case
, Qt
);
8050 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8051 Qt
, Qnil
, Qcoding_system_history
,
8052 default_coding_system
, Qnil
);
8053 unbind_to (count
, Qnil
);
8054 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8057 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8059 doc
: /* Check validity of CODING-SYSTEM.
8060 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8061 It is valid if it is nil or a symbol defined as a coding system by the
8062 function `define-coding-system'. */)
8063 (Lisp_Object coding_system
)
8065 Lisp_Object define_form
;
8067 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8068 if (! NILP (define_form
))
8070 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8071 safe_eval (define_form
);
8073 if (!NILP (Fcoding_system_p (coding_system
)))
8074 return coding_system
;
8075 xsignal1 (Qcoding_system_error
, coding_system
);
8079 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8080 HIGHEST, return the coding system of the highest
8081 priority among the detected coding systems. Otherwise return a
8082 list of detected coding systems sorted by their priorities. If
8083 MULTIBYTEP, it is assumed that the bytes are in correct
8084 multibyte form but contains only ASCII and eight-bit chars.
8085 Otherwise, the bytes are raw bytes.
8087 CODING-SYSTEM controls the detection as below:
8089 If it is nil, detect both text-format and eol-format. If the
8090 text-format part of CODING-SYSTEM is already specified
8091 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8092 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8093 detect only text-format. */
8096 detect_coding_system (const unsigned char *src
,
8097 ptrdiff_t src_chars
, ptrdiff_t src_bytes
,
8098 bool highest
, bool multibytep
,
8099 Lisp_Object coding_system
)
8101 const unsigned char *src_end
= src
+ src_bytes
;
8102 Lisp_Object attrs
, eol_type
;
8103 Lisp_Object val
= Qnil
;
8104 struct coding_system coding
;
8106 struct coding_detection_info detect_info
;
8107 enum coding_category base_category
;
8108 bool null_byte_found
= 0, eight_bit_found
= 0;
8110 if (NILP (coding_system
))
8111 coding_system
= Qundecided
;
8112 setup_coding_system (coding_system
, &coding
);
8113 attrs
= CODING_ID_ATTRS (coding
.id
);
8114 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8115 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8117 coding
.source
= src
;
8118 coding
.src_chars
= src_chars
;
8119 coding
.src_bytes
= src_bytes
;
8120 coding
.src_multibyte
= multibytep
;
8121 coding
.consumed
= 0;
8122 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8123 coding
.head_ascii
= 0;
8125 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8127 /* At first, detect text-format if necessary. */
8128 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8129 if (base_category
== coding_category_undecided
)
8131 enum coding_category category
IF_LINT (= 0);
8132 struct coding_system
*this IF_LINT (= NULL
);
8135 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8136 for (; src
< src_end
; src
++)
8141 eight_bit_found
= 1;
8142 if (null_byte_found
)
8147 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8148 && ! inhibit_iso_escape_detection
8149 && ! detect_info
.checked
)
8151 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8153 /* We have scanned the whole data. */
8154 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8156 /* We didn't find an 8-bit code. We may
8157 have found a null-byte, but it's very
8158 rare that a binary file confirm to
8161 coding
.head_ascii
= src
- coding
.source
;
8163 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8167 else if (! c
&& !inhibit_null_byte_detection
)
8169 null_byte_found
= 1;
8170 if (eight_bit_found
)
8173 if (! eight_bit_found
)
8174 coding
.head_ascii
++;
8176 else if (! eight_bit_found
)
8177 coding
.head_ascii
++;
8180 if (null_byte_found
|| eight_bit_found
8181 || coding
.head_ascii
< coding
.src_bytes
8182 || detect_info
.found
)
8184 if (coding
.head_ascii
== coding
.src_bytes
)
8185 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8186 for (i
= 0; i
< coding_category_raw_text
; i
++)
8188 category
= coding_priorities
[i
];
8189 this = coding_categories
+ category
;
8190 if (detect_info
.found
& (1 << category
))
8195 if (null_byte_found
)
8197 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8198 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8200 for (i
= 0; i
< coding_category_raw_text
; i
++)
8202 category
= coding_priorities
[i
];
8203 this = coding_categories
+ category
;
8207 /* No coding system of this category is defined. */
8208 detect_info
.rejected
|= (1 << category
);
8210 else if (category
>= coding_category_raw_text
)
8212 else if (detect_info
.checked
& (1 << category
))
8215 && (detect_info
.found
& (1 << category
)))
8218 else if ((*(this->detector
)) (&coding
, &detect_info
)
8220 && (detect_info
.found
& (1 << category
)))
8222 if (category
== coding_category_utf_16_auto
)
8224 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8225 category
= coding_category_utf_16_le
;
8227 category
= coding_category_utf_16_be
;
8235 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8238 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8239 id
= CODING_SYSTEM_ID (Qno_conversion
);
8240 val
= Fcons (make_number (id
), Qnil
);
8242 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8244 detect_info
.found
= CATEGORY_MASK_ANY
;
8245 id
= coding_categories
[coding_category_undecided
].id
;
8246 val
= Fcons (make_number (id
), Qnil
);
8250 if (detect_info
.found
)
8252 detect_info
.found
= 1 << category
;
8253 val
= Fcons (make_number (this->id
), Qnil
);
8256 for (i
= 0; i
< coding_category_raw_text
; i
++)
8257 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8259 detect_info
.found
= 1 << coding_priorities
[i
];
8260 id
= coding_categories
[coding_priorities
[i
]].id
;
8261 val
= Fcons (make_number (id
), Qnil
);
8267 int mask
= detect_info
.rejected
| detect_info
.found
;
8270 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8272 category
= coding_priorities
[i
];
8273 if (! (mask
& (1 << category
)))
8275 found
|= 1 << category
;
8276 id
= coding_categories
[category
].id
;
8278 val
= Fcons (make_number (id
), val
);
8281 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8283 category
= coding_priorities
[i
];
8284 if (detect_info
.found
& (1 << category
))
8286 id
= coding_categories
[category
].id
;
8287 val
= Fcons (make_number (id
), val
);
8290 detect_info
.found
|= found
;
8293 else if (base_category
== coding_category_utf_8_auto
)
8295 if (detect_coding_utf_8 (&coding
, &detect_info
))
8297 struct coding_system
*this;
8299 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8300 this = coding_categories
+ coding_category_utf_8_sig
;
8302 this = coding_categories
+ coding_category_utf_8_nosig
;
8303 val
= Fcons (make_number (this->id
), Qnil
);
8306 else if (base_category
== coding_category_utf_16_auto
)
8308 if (detect_coding_utf_16 (&coding
, &detect_info
))
8310 struct coding_system
*this;
8312 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8313 this = coding_categories
+ coding_category_utf_16_le
;
8314 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8315 this = coding_categories
+ coding_category_utf_16_be
;
8316 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8317 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8319 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8320 val
= Fcons (make_number (this->id
), Qnil
);
8325 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8326 val
= Fcons (make_number (coding
.id
), Qnil
);
8329 /* Then, detect eol-format if necessary. */
8331 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8334 if (VECTORP (eol_type
))
8336 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8338 if (null_byte_found
)
8339 normal_eol
= EOL_SEEN_LF
;
8341 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8342 coding_category_raw_text
);
8344 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8345 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8346 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8347 coding_category_utf_16_be
);
8348 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8349 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8350 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8351 coding_category_utf_16_le
);
8355 if (EQ (eol_type
, Qunix
))
8356 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8357 else if (EQ (eol_type
, Qdos
))
8358 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8360 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8363 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8365 enum coding_category category
;
8368 id
= XINT (XCAR (tail
));
8369 attrs
= CODING_ID_ATTRS (id
);
8370 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8371 eol_type
= CODING_ID_EOL_TYPE (id
);
8372 if (VECTORP (eol_type
))
8374 if (category
== coding_category_utf_16_be
8375 || category
== coding_category_utf_16_be_nosig
)
8376 this_eol
= utf_16_be_eol
;
8377 else if (category
== coding_category_utf_16_le
8378 || category
== coding_category_utf_16_le_nosig
)
8379 this_eol
= utf_16_le_eol
;
8381 this_eol
= normal_eol
;
8383 if (this_eol
== EOL_SEEN_LF
)
8384 XSETCAR (tail
, AREF (eol_type
, 0));
8385 else if (this_eol
== EOL_SEEN_CRLF
)
8386 XSETCAR (tail
, AREF (eol_type
, 1));
8387 else if (this_eol
== EOL_SEEN_CR
)
8388 XSETCAR (tail
, AREF (eol_type
, 2));
8390 XSETCAR (tail
, CODING_ID_NAME (id
));
8393 XSETCAR (tail
, CODING_ID_NAME (id
));
8397 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8401 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8403 doc
: /* Detect coding system of the text in the region between START and END.
8404 Return a list of possible coding systems ordered by priority.
8405 The coding systems to try and their priorities follows what
8406 the function `coding-system-priority-list' (which see) returns.
8408 If only ASCII characters are found (except for such ISO-2022 control
8409 characters as ESC), it returns a list of single element `undecided'
8410 or its subsidiary coding system according to a detected end-of-line
8413 If optional argument HIGHEST is non-nil, return the coding system of
8414 highest priority. */)
8415 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8418 ptrdiff_t from_byte
, to_byte
;
8420 CHECK_NUMBER_COERCE_MARKER (start
);
8421 CHECK_NUMBER_COERCE_MARKER (end
);
8423 validate_region (&start
, &end
);
8424 from
= XINT (start
), to
= XINT (end
);
8425 from_byte
= CHAR_TO_BYTE (from
);
8426 to_byte
= CHAR_TO_BYTE (to
);
8428 if (from
< GPT
&& to
>= GPT
)
8429 move_gap_both (to
, to_byte
);
8431 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8432 to
- from
, to_byte
- from_byte
,
8434 !NILP (BVAR (current_buffer
8435 , enable_multibyte_characters
)),
8439 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8441 doc
: /* Detect coding system of the text in STRING.
8442 Return a list of possible coding systems ordered by priority.
8443 The coding systems to try and their priorities follows what
8444 the function `coding-system-priority-list' (which see) returns.
8446 If only ASCII characters are found (except for such ISO-2022 control
8447 characters as ESC), it returns a list of single element `undecided'
8448 or its subsidiary coding system according to a detected end-of-line
8451 If optional argument HIGHEST is non-nil, return the coding system of
8452 highest priority. */)
8453 (Lisp_Object string
, Lisp_Object highest
)
8455 CHECK_STRING (string
);
8457 return detect_coding_system (SDATA (string
),
8458 SCHARS (string
), SBYTES (string
),
8459 !NILP (highest
), STRING_MULTIBYTE (string
),
8465 char_encodable_p (int c
, Lisp_Object attrs
)
8468 struct charset
*charset
;
8469 Lisp_Object translation_table
;
8471 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8472 if (! NILP (translation_table
))
8473 c
= translate_char (translation_table
, c
);
8474 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8475 CONSP (tail
); tail
= XCDR (tail
))
8477 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8478 if (CHAR_CHARSET_P (c
, charset
))
8481 return (! NILP (tail
));
8485 /* Return a list of coding systems that safely encode the text between
8486 START and END. If EXCLUDE is non-nil, it is a list of coding
8487 systems not to check. The returned list doesn't contain any such
8488 coding systems. In any case, if the text contains only ASCII or is
8489 unibyte, return t. */
8491 DEFUN ("find-coding-systems-region-internal",
8492 Ffind_coding_systems_region_internal
,
8493 Sfind_coding_systems_region_internal
, 2, 3, 0,
8494 doc
: /* Internal use only. */)
8495 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8497 Lisp_Object coding_attrs_list
, safe_codings
;
8498 ptrdiff_t start_byte
, end_byte
;
8499 const unsigned char *p
, *pbeg
, *pend
;
8501 Lisp_Object tail
, elt
, work_table
;
8503 if (STRINGP (start
))
8505 if (!STRING_MULTIBYTE (start
)
8506 || SCHARS (start
) == SBYTES (start
))
8509 end_byte
= SBYTES (start
);
8513 CHECK_NUMBER_COERCE_MARKER (start
);
8514 CHECK_NUMBER_COERCE_MARKER (end
);
8515 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8516 args_out_of_range (start
, end
);
8517 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8519 start_byte
= CHAR_TO_BYTE (XINT (start
));
8520 end_byte
= CHAR_TO_BYTE (XINT (end
));
8521 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8524 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8526 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8527 move_gap_both (XINT (start
), start_byte
);
8529 move_gap_both (XINT (end
), end_byte
);
8533 coding_attrs_list
= Qnil
;
8534 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8536 || NILP (Fmemq (XCAR (tail
), exclude
)))
8540 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8541 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8542 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8544 ASET (attrs
, coding_attr_trans_tbl
,
8545 get_translation_table (attrs
, 1, NULL
));
8546 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8550 if (STRINGP (start
))
8551 p
= pbeg
= SDATA (start
);
8553 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8554 pend
= p
+ (end_byte
- start_byte
);
8556 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8557 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8559 work_table
= Fmake_char_table (Qnil
, Qnil
);
8562 if (ASCII_BYTE_P (*p
))
8566 c
= STRING_CHAR_ADVANCE (p
);
8567 if (!NILP (char_table_ref (work_table
, c
)))
8568 /* This character was already checked. Ignore it. */
8571 charset_map_loaded
= 0;
8572 for (tail
= coding_attrs_list
; CONSP (tail
);)
8577 else if (char_encodable_p (c
, elt
))
8579 else if (CONSP (XCDR (tail
)))
8581 XSETCAR (tail
, XCAR (XCDR (tail
)));
8582 XSETCDR (tail
, XCDR (XCDR (tail
)));
8586 XSETCAR (tail
, Qnil
);
8590 if (charset_map_loaded
)
8592 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8594 if (STRINGP (start
))
8595 pbeg
= SDATA (start
);
8597 pbeg
= BYTE_POS_ADDR (start_byte
);
8598 p
= pbeg
+ p_offset
;
8599 pend
= pbeg
+ pend_offset
;
8601 char_table_set (work_table
, c
, Qt
);
8605 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8606 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8607 if (! NILP (XCAR (tail
)))
8608 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8610 return safe_codings
;
8614 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8615 Sunencodable_char_position
, 3, 5, 0,
8617 Return position of first un-encodable character in a region.
8618 START and END specify the region and CODING-SYSTEM specifies the
8619 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8621 If optional 4th argument COUNT is non-nil, it specifies at most how
8622 many un-encodable characters to search. In this case, the value is a
8625 If optional 5th argument STRING is non-nil, it is a string to search
8626 for un-encodable characters. In that case, START and END are indexes
8628 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8631 struct coding_system coding
;
8632 Lisp_Object attrs
, charset_list
, translation_table
;
8633 Lisp_Object positions
;
8635 const unsigned char *p
, *stop
, *pend
;
8636 bool ascii_compatible
;
8638 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8639 attrs
= CODING_ID_ATTRS (coding
.id
);
8640 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8642 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8643 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8644 translation_table
= get_translation_table (attrs
, 1, NULL
);
8648 validate_region (&start
, &end
);
8649 from
= XINT (start
);
8651 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8652 || (ascii_compatible
8653 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8655 p
= CHAR_POS_ADDR (from
);
8656 pend
= CHAR_POS_ADDR (to
);
8657 if (from
< GPT
&& to
>= GPT
)
8664 CHECK_STRING (string
);
8665 CHECK_NATNUM (start
);
8667 if (! (XINT (start
) <= XINT (end
) && XINT (end
) <= SCHARS (string
)))
8668 args_out_of_range_3 (string
, start
, end
);
8669 from
= XINT (start
);
8671 if (! STRING_MULTIBYTE (string
))
8673 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8674 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8675 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8683 CHECK_NATNUM (count
);
8688 charset_map_loaded
= 0;
8693 if (ascii_compatible
)
8694 while (p
< stop
&& ASCII_BYTE_P (*p
))
8704 c
= STRING_CHAR_ADVANCE (p
);
8705 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8706 && ! char_charset (translate_char (translation_table
, c
),
8707 charset_list
, NULL
))
8709 positions
= Fcons (make_number (from
), positions
);
8716 if (charset_map_loaded
&& NILP (string
))
8718 p
= CHAR_POS_ADDR (from
);
8719 pend
= CHAR_POS_ADDR (to
);
8720 if (from
< GPT
&& to
>= GPT
)
8724 charset_map_loaded
= 0;
8728 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8732 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8733 Scheck_coding_systems_region
, 3, 3, 0,
8734 doc
: /* Check if the region is encodable by coding systems.
8736 START and END are buffer positions specifying the region.
8737 CODING-SYSTEM-LIST is a list of coding systems to check.
8739 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8740 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8741 whole region, POS0, POS1, ... are buffer positions where non-encodable
8742 characters are found.
8744 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8747 START may be a string. In that case, check if the string is
8748 encodable, and the value contains indices to the string instead of
8749 buffer positions. END is ignored.
8751 If the current buffer (or START if it is a string) is unibyte, the value
8753 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8756 ptrdiff_t start_byte
, end_byte
;
8758 const unsigned char *p
, *pbeg
, *pend
;
8760 Lisp_Object tail
, elt
, attrs
;
8762 if (STRINGP (start
))
8764 if (!STRING_MULTIBYTE (start
)
8765 || SCHARS (start
) == SBYTES (start
))
8768 end_byte
= SBYTES (start
);
8773 CHECK_NUMBER_COERCE_MARKER (start
);
8774 CHECK_NUMBER_COERCE_MARKER (end
);
8775 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8776 args_out_of_range (start
, end
);
8777 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8779 start_byte
= CHAR_TO_BYTE (XINT (start
));
8780 end_byte
= CHAR_TO_BYTE (XINT (end
));
8781 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8784 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8786 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8787 move_gap_both (XINT (start
), start_byte
);
8789 move_gap_both (XINT (end
), end_byte
);
8795 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8798 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8799 ASET (attrs
, coding_attr_trans_tbl
,
8800 get_translation_table (attrs
, 1, NULL
));
8801 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8804 if (STRINGP (start
))
8805 p
= pbeg
= SDATA (start
);
8807 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8808 pend
= p
+ (end_byte
- start_byte
);
8810 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8811 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8815 if (ASCII_BYTE_P (*p
))
8819 c
= STRING_CHAR_ADVANCE (p
);
8821 charset_map_loaded
= 0;
8822 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8824 elt
= XCDR (XCAR (tail
));
8825 if (! char_encodable_p (c
, XCAR (elt
)))
8826 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8828 if (charset_map_loaded
)
8830 ptrdiff_t p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8832 if (STRINGP (start
))
8833 pbeg
= SDATA (start
);
8835 pbeg
= BYTE_POS_ADDR (start_byte
);
8836 p
= pbeg
+ p_offset
;
8837 pend
= pbeg
+ pend_offset
;
8845 for (; CONSP (tail
); tail
= XCDR (tail
))
8848 if (CONSP (XCDR (XCDR (elt
))))
8849 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8858 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8859 Lisp_Object coding_system
, Lisp_Object dst_object
,
8860 bool encodep
, bool norecord
)
8862 struct coding_system coding
;
8863 ptrdiff_t from
, from_byte
, to
, to_byte
;
8864 Lisp_Object src_object
;
8866 CHECK_NUMBER_COERCE_MARKER (start
);
8867 CHECK_NUMBER_COERCE_MARKER (end
);
8868 if (NILP (coding_system
))
8869 coding_system
= Qno_conversion
;
8871 CHECK_CODING_SYSTEM (coding_system
);
8872 src_object
= Fcurrent_buffer ();
8873 if (NILP (dst_object
))
8874 dst_object
= src_object
;
8875 else if (! EQ (dst_object
, Qt
))
8876 CHECK_BUFFER (dst_object
);
8878 validate_region (&start
, &end
);
8879 from
= XFASTINT (start
);
8880 from_byte
= CHAR_TO_BYTE (from
);
8881 to
= XFASTINT (end
);
8882 to_byte
= CHAR_TO_BYTE (to
);
8884 setup_coding_system (coding_system
, &coding
);
8885 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8888 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8891 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8894 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8896 return (BUFFERP (dst_object
)
8897 ? make_number (coding
.produced_char
)
8898 : coding
.dst_object
);
8902 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8903 3, 4, "r\nzCoding system: ",
8904 doc
: /* Decode the current region from the specified coding system.
8905 When called from a program, takes four arguments:
8906 START, END, CODING-SYSTEM, and DESTINATION.
8907 START and END are buffer positions.
8909 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8910 If nil, the region between START and END is replaced by the decoded text.
8911 If buffer, the decoded text is inserted in that buffer after point (point
8913 In those cases, the length of the decoded text is returned.
8914 If DESTINATION is t, the decoded text is returned.
8916 This function sets `last-coding-system-used' to the precise coding system
8917 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8918 not fully specified.) */)
8919 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8921 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8924 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8925 3, 4, "r\nzCoding system: ",
8926 doc
: /* Encode the current region by specified coding system.
8927 When called from a program, takes four arguments:
8928 START, END, CODING-SYSTEM and DESTINATION.
8929 START and END are buffer positions.
8931 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8932 If nil, the region between START and END is replace by the encoded text.
8933 If buffer, the encoded text is inserted in that buffer after point (point
8935 In those cases, the length of the encoded text is returned.
8936 If DESTINATION is t, the encoded text is returned.
8938 This function sets `last-coding-system-used' to the precise coding system
8939 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8940 not fully specified.) */)
8941 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8943 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8947 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8948 Lisp_Object dst_object
, bool encodep
, bool nocopy
,
8951 struct coding_system coding
;
8952 ptrdiff_t chars
, bytes
;
8954 CHECK_STRING (string
);
8955 if (NILP (coding_system
))
8958 Vlast_coding_system_used
= Qno_conversion
;
8959 if (NILP (dst_object
))
8960 return (nocopy
? Fcopy_sequence (string
) : string
);
8963 if (NILP (coding_system
))
8964 coding_system
= Qno_conversion
;
8966 CHECK_CODING_SYSTEM (coding_system
);
8967 if (NILP (dst_object
))
8969 else if (! EQ (dst_object
, Qt
))
8970 CHECK_BUFFER (dst_object
);
8972 setup_coding_system (coding_system
, &coding
);
8973 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8974 chars
= SCHARS (string
);
8975 bytes
= SBYTES (string
);
8977 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8979 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8981 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8983 return (BUFFERP (dst_object
)
8984 ? make_number (coding
.produced_char
)
8985 : coding
.dst_object
);
8989 /* Encode or decode STRING according to CODING_SYSTEM.
8990 Do not set Vlast_coding_system_used.
8992 This function is called only from macros DECODE_FILE and
8993 ENCODE_FILE, thus we ignore character composition. */
8996 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
8999 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9003 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9005 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9007 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9008 if the decoding operation is trivial.
9010 Optional fourth arg BUFFER non-nil means that the decoded text is
9011 inserted in that buffer after point (point does not move). In this
9012 case, the return value is the length of the decoded text.
9014 This function sets `last-coding-system-used' to the precise coding system
9015 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9016 not fully specified.) */)
9017 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9019 return code_convert_string (string
, coding_system
, buffer
,
9020 0, ! NILP (nocopy
), 0);
9023 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9025 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9027 Optional third arg NOCOPY non-nil means it is OK to return STRING
9028 itself if the encoding operation is trivial.
9030 Optional fourth arg BUFFER non-nil means that the encoded text is
9031 inserted in that buffer after point (point does not move). In this
9032 case, the return value is the length of the encoded text.
9034 This function sets `last-coding-system-used' to the precise coding system
9035 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9036 not fully specified.) */)
9037 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9039 return code_convert_string (string
, coding_system
, buffer
,
9040 1, ! NILP (nocopy
), 0);
9044 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9045 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9046 Return the corresponding character. */)
9049 Lisp_Object spec
, attrs
, val
;
9050 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9054 CHECK_NATNUM (code
);
9055 ch
= XFASTINT (code
);
9056 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9057 attrs
= AREF (spec
, 0);
9059 if (ASCII_BYTE_P (ch
)
9060 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9063 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9064 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9065 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9066 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9071 charset
= charset_roman
;
9073 else if (ch
>= 0xA0 && ch
< 0xDF)
9076 charset
= charset_kana
;
9080 EMACS_INT c1
= ch
>> 8;
9083 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9084 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9085 error ("Invalid code: %"pI
"d", ch
);
9088 charset
= charset_kanji
;
9090 c
= DECODE_CHAR (charset
, c
);
9092 error ("Invalid code: %"pI
"d", ch
);
9093 return make_number (c
);
9097 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9098 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9099 Return the corresponding code in SJIS. */)
9102 Lisp_Object spec
, attrs
, charset_list
;
9104 struct charset
*charset
;
9107 CHECK_CHARACTER (ch
);
9109 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9110 attrs
= AREF (spec
, 0);
9112 if (ASCII_CHAR_P (c
)
9113 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9116 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9117 charset
= char_charset (c
, charset_list
, &code
);
9118 if (code
== CHARSET_INVALID_CODE (charset
))
9119 error ("Can't encode by shift_jis encoding: %c", c
);
9122 return make_number (code
);
9125 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9126 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9127 Return the corresponding character. */)
9130 Lisp_Object spec
, attrs
, val
;
9131 struct charset
*charset_roman
, *charset_big5
, *charset
;
9135 CHECK_NATNUM (code
);
9136 ch
= XFASTINT (code
);
9137 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9138 attrs
= AREF (spec
, 0);
9140 if (ASCII_BYTE_P (ch
)
9141 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9144 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9145 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9146 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9151 charset
= charset_roman
;
9155 EMACS_INT b1
= ch
>> 8;
9157 if (b1
< 0xA1 || b1
> 0xFE
9158 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9159 error ("Invalid code: %"pI
"d", ch
);
9161 charset
= charset_big5
;
9163 c
= DECODE_CHAR (charset
, c
);
9165 error ("Invalid code: %"pI
"d", ch
);
9166 return make_number (c
);
9169 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9170 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9171 Return the corresponding character code in Big5. */)
9174 Lisp_Object spec
, attrs
, charset_list
;
9175 struct charset
*charset
;
9179 CHECK_CHARACTER (ch
);
9181 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9182 attrs
= AREF (spec
, 0);
9183 if (ASCII_CHAR_P (c
)
9184 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9187 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9188 charset
= char_charset (c
, charset_list
, &code
);
9189 if (code
== CHARSET_INVALID_CODE (charset
))
9190 error ("Can't encode by Big5 encoding: %c", c
);
9192 return make_number (code
);
9196 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9197 Sset_terminal_coding_system_internal
, 1, 2, 0,
9198 doc
: /* Internal use only. */)
9199 (Lisp_Object coding_system
, Lisp_Object terminal
)
9201 struct terminal
*term
= get_terminal (terminal
, 1);
9202 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9203 CHECK_SYMBOL (coding_system
);
9204 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9205 /* We had better not send unsafe characters to terminal. */
9206 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9207 /* Character composition should be disabled. */
9208 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9209 terminal_coding
->src_multibyte
= 1;
9210 terminal_coding
->dst_multibyte
= 0;
9212 (term
, (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
9213 ? coding_charset_list (terminal_coding
)
9214 : Fcons (make_number (charset_ascii
), Qnil
)));
9218 DEFUN ("set-safe-terminal-coding-system-internal",
9219 Fset_safe_terminal_coding_system_internal
,
9220 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9221 doc
: /* Internal use only. */)
9222 (Lisp_Object coding_system
)
9224 CHECK_SYMBOL (coding_system
);
9225 setup_coding_system (Fcheck_coding_system (coding_system
),
9226 &safe_terminal_coding
);
9227 /* Character composition should be disabled. */
9228 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9229 safe_terminal_coding
.src_multibyte
= 1;
9230 safe_terminal_coding
.dst_multibyte
= 0;
9234 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9235 Sterminal_coding_system
, 0, 1, 0,
9236 doc
: /* Return coding system specified for terminal output on the given terminal.
9237 TERMINAL may be a terminal object, a frame, or nil for the selected
9238 frame's terminal device. */)
9239 (Lisp_Object terminal
)
9241 struct coding_system
*terminal_coding
9242 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9243 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9245 /* For backward compatibility, return nil if it is `undecided'. */
9246 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9249 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9250 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9251 doc
: /* Internal use only. */)
9252 (Lisp_Object coding_system
, Lisp_Object terminal
)
9254 struct terminal
*t
= get_terminal (terminal
, 1);
9255 CHECK_SYMBOL (coding_system
);
9256 if (NILP (coding_system
))
9257 coding_system
= Qno_conversion
;
9259 Fcheck_coding_system (coding_system
);
9260 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9261 /* Character composition should be disabled. */
9262 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9263 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9267 DEFUN ("keyboard-coding-system",
9268 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9269 doc
: /* Return coding system specified for decoding keyboard input. */)
9270 (Lisp_Object terminal
)
9272 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9273 (get_terminal (terminal
, 1))->id
);
9277 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9278 Sfind_operation_coding_system
, 1, MANY
, 0,
9279 doc
: /* Choose a coding system for an operation based on the target name.
9280 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9281 DECODING-SYSTEM is the coding system to use for decoding
9282 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9283 for encoding (in case OPERATION does encoding).
9285 The first argument OPERATION specifies an I/O primitive:
9286 For file I/O, `insert-file-contents' or `write-region'.
9287 For process I/O, `call-process', `call-process-region', or `start-process'.
9288 For network I/O, `open-network-stream'.
9290 The remaining arguments should be the same arguments that were passed
9291 to the primitive. Depending on which primitive, one of those arguments
9292 is selected as the TARGET. For example, if OPERATION does file I/O,
9293 whichever argument specifies the file name is TARGET.
9295 TARGET has a meaning which depends on OPERATION:
9296 For file I/O, TARGET is a file name (except for the special case below).
9297 For process I/O, TARGET is a process name.
9298 For network I/O, TARGET is a service name or a port number.
9300 This function looks up what is specified for TARGET in
9301 `file-coding-system-alist', `process-coding-system-alist',
9302 or `network-coding-system-alist' depending on OPERATION.
9303 They may specify a coding system, a cons of coding systems,
9304 or a function symbol to call.
9305 In the last case, we call the function with one argument,
9306 which is a list of all the arguments given to this function.
9307 If the function can't decide a coding system, it can return
9308 `undecided' so that the normal code-detection is performed.
9310 If OPERATION is `insert-file-contents', the argument corresponding to
9311 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9312 file name to look up, and BUFFER is a buffer that contains the file's
9313 contents (not yet decoded). If `file-coding-system-alist' specifies a
9314 function to call for FILENAME, that function should examine the
9315 contents of BUFFER instead of reading the file.
9317 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9318 (ptrdiff_t nargs
, Lisp_Object
*args
)
9320 Lisp_Object operation
, target_idx
, target
, val
;
9321 register Lisp_Object chain
;
9324 error ("Too few arguments");
9325 operation
= args
[0];
9326 if (!SYMBOLP (operation
)
9327 || (target_idx
= Fget (operation
, Qtarget_idx
), !NATNUMP (target_idx
)))
9328 error ("Invalid first argument");
9329 if (nargs
<= 1 + XFASTINT (target_idx
))
9330 error ("Too few arguments for operation `%s'",
9331 SDATA (SYMBOL_NAME (operation
)));
9332 target
= args
[XFASTINT (target_idx
) + 1];
9333 if (!(STRINGP (target
)
9334 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9335 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9336 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9337 error ("Invalid argument %"pI
"d of operation `%s'",
9338 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9340 target
= XCAR (target
);
9342 chain
= ((EQ (operation
, Qinsert_file_contents
)
9343 || EQ (operation
, Qwrite_region
))
9344 ? Vfile_coding_system_alist
9345 : (EQ (operation
, Qopen_network_stream
)
9346 ? Vnetwork_coding_system_alist
9347 : Vprocess_coding_system_alist
));
9351 for (; CONSP (chain
); chain
= XCDR (chain
))
9357 && ((STRINGP (target
)
9358 && STRINGP (XCAR (elt
))
9359 && fast_string_match (XCAR (elt
), target
) >= 0)
9360 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9363 /* Here, if VAL is both a valid coding system and a valid
9364 function symbol, we return VAL as a coding system. */
9367 if (! SYMBOLP (val
))
9369 if (! NILP (Fcoding_system_p (val
)))
9370 return Fcons (val
, val
);
9371 if (! NILP (Ffboundp (val
)))
9373 /* We use call1 rather than safe_call1
9374 so as to get bug reports about functions called here
9375 which don't handle the current interface. */
9376 val
= call1 (val
, Flist (nargs
, args
));
9379 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9380 return Fcons (val
, val
);
9388 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9389 Sset_coding_system_priority
, 0, MANY
, 0,
9390 doc
: /* Assign higher priority to the coding systems given as arguments.
9391 If multiple coding systems belong to the same category,
9392 all but the first one are ignored.
9394 usage: (set-coding-system-priority &rest coding-systems) */)
9395 (ptrdiff_t nargs
, Lisp_Object
*args
)
9398 bool changed
[coding_category_max
];
9399 enum coding_category priorities
[coding_category_max
];
9401 memset (changed
, 0, sizeof changed
);
9403 for (i
= j
= 0; i
< nargs
; i
++)
9405 enum coding_category category
;
9406 Lisp_Object spec
, attrs
;
9408 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9409 attrs
= AREF (spec
, 0);
9410 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9411 if (changed
[category
])
9412 /* Ignore this coding system because a coding system of the
9413 same category already had a higher priority. */
9415 changed
[category
] = 1;
9416 priorities
[j
++] = category
;
9417 if (coding_categories
[category
].id
>= 0
9418 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9419 setup_coding_system (args
[i
], &coding_categories
[category
]);
9420 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9423 /* Now we have decided top J priorities. Reflect the order of the
9424 original priorities to the remaining priorities. */
9426 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9428 while (j
< coding_category_max
9429 && changed
[coding_priorities
[j
]])
9431 if (j
== coding_category_max
)
9433 priorities
[i
] = coding_priorities
[j
];
9436 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9438 /* Update `coding-category-list'. */
9439 Vcoding_category_list
= Qnil
;
9440 for (i
= coding_category_max
; i
-- > 0; )
9441 Vcoding_category_list
9442 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9443 Vcoding_category_list
);
9448 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9449 Scoding_system_priority_list
, 0, 1, 0,
9450 doc
: /* Return a list of coding systems ordered by their priorities.
9451 The list contains a subset of coding systems; i.e. coding systems
9452 assigned to each coding category (see `coding-category-list').
9454 HIGHESTP non-nil means just return the highest priority one. */)
9455 (Lisp_Object highestp
)
9460 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9462 enum coding_category category
= coding_priorities
[i
];
9463 int id
= coding_categories
[category
].id
;
9468 attrs
= CODING_ID_ATTRS (id
);
9469 if (! NILP (highestp
))
9470 return CODING_ATTR_BASE_NAME (attrs
);
9471 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9473 return Fnreverse (val
);
9476 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9479 make_subsidiaries (Lisp_Object base
)
9481 Lisp_Object subsidiaries
;
9482 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
9483 char *buf
= alloca (base_name_len
+ 6);
9486 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9487 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9488 for (i
= 0; i
< 3; i
++)
9490 strcpy (buf
+ base_name_len
, suffixes
[i
]);
9491 ASET (subsidiaries
, i
, intern (buf
));
9493 return subsidiaries
;
9497 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9498 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9499 doc
: /* For internal use only.
9500 usage: (define-coding-system-internal ...) */)
9501 (ptrdiff_t nargs
, Lisp_Object
*args
)
9504 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9505 Lisp_Object attrs
; /* Vector of attributes. */
9506 Lisp_Object eol_type
;
9507 Lisp_Object aliases
;
9508 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9509 enum coding_category category
;
9510 Lisp_Object tail
, val
;
9511 int max_charset_id
= 0;
9514 if (nargs
< coding_arg_max
)
9517 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9519 name
= args
[coding_arg_name
];
9520 CHECK_SYMBOL (name
);
9521 ASET (attrs
, coding_attr_base_name
, name
);
9523 val
= args
[coding_arg_mnemonic
];
9524 if (! STRINGP (val
))
9525 CHECK_CHARACTER (val
);
9526 ASET (attrs
, coding_attr_mnemonic
, val
);
9528 coding_type
= args
[coding_arg_coding_type
];
9529 CHECK_SYMBOL (coding_type
);
9530 ASET (attrs
, coding_attr_type
, coding_type
);
9532 charset_list
= args
[coding_arg_charset_list
];
9533 if (SYMBOLP (charset_list
))
9535 if (EQ (charset_list
, Qiso_2022
))
9537 if (! EQ (coding_type
, Qiso_2022
))
9538 error ("Invalid charset-list");
9539 charset_list
= Viso_2022_charset_list
;
9541 else if (EQ (charset_list
, Qemacs_mule
))
9543 if (! EQ (coding_type
, Qemacs_mule
))
9544 error ("Invalid charset-list");
9545 charset_list
= Vemacs_mule_charset_list
;
9547 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9549 if (! RANGED_INTEGERP (0, XCAR (tail
), INT_MAX
- 1))
9550 error ("Invalid charset-list");
9551 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9552 max_charset_id
= XFASTINT (XCAR (tail
));
9557 charset_list
= Fcopy_sequence (charset_list
);
9558 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9560 struct charset
*charset
;
9563 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9564 if (EQ (coding_type
, Qiso_2022
)
9565 ? CHARSET_ISO_FINAL (charset
) < 0
9566 : EQ (coding_type
, Qemacs_mule
)
9567 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9569 error ("Can't handle charset `%s'",
9570 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9572 XSETCAR (tail
, make_number (charset
->id
));
9573 if (max_charset_id
< charset
->id
)
9574 max_charset_id
= charset
->id
;
9577 ASET (attrs
, coding_attr_charset_list
, charset_list
);
9579 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9580 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9581 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9582 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9583 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
9585 ASET (attrs
, coding_attr_ascii_compat
, args
[coding_arg_ascii_compatible_p
]);
9587 val
= args
[coding_arg_decode_translation_table
];
9588 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9590 ASET (attrs
, coding_attr_decode_tbl
, val
);
9592 val
= args
[coding_arg_encode_translation_table
];
9593 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9595 ASET (attrs
, coding_attr_encode_tbl
, val
);
9597 val
= args
[coding_arg_post_read_conversion
];
9599 ASET (attrs
, coding_attr_post_read
, val
);
9601 val
= args
[coding_arg_pre_write_conversion
];
9603 ASET (attrs
, coding_attr_pre_write
, val
);
9605 val
= args
[coding_arg_default_char
];
9607 ASET (attrs
, coding_attr_default_char
, make_number (' '));
9610 CHECK_CHARACTER (val
);
9611 ASET (attrs
, coding_attr_default_char
, val
);
9614 val
= args
[coding_arg_for_unibyte
];
9615 ASET (attrs
, coding_attr_for_unibyte
, NILP (val
) ? Qnil
: Qt
);
9617 val
= args
[coding_arg_plist
];
9619 ASET (attrs
, coding_attr_plist
, val
);
9621 if (EQ (coding_type
, Qcharset
))
9623 /* Generate a lisp vector of 256 elements. Each element is nil,
9624 integer, or a list of charset IDs.
9626 If Nth element is nil, the byte code N is invalid in this
9629 If Nth element is a number NUM, N is the first byte of a
9630 charset whose ID is NUM.
9632 If Nth element is a list of charset IDs, N is the first byte
9633 of one of them. The list is sorted by dimensions of the
9634 charsets. A charset of smaller dimension comes first. */
9635 val
= Fmake_vector (make_number (256), Qnil
);
9637 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9639 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9640 int dim
= CHARSET_DIMENSION (charset
);
9641 int idx
= (dim
- 1) * 4;
9643 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9644 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9646 for (i
= charset
->code_space
[idx
];
9647 i
<= charset
->code_space
[idx
+ 1]; i
++)
9649 Lisp_Object tmp
, tmp2
;
9652 tmp
= AREF (val
, i
);
9655 else if (NUMBERP (tmp
))
9657 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9659 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9661 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9665 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9667 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9672 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9675 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9676 XSETCAR (tmp2
, XCAR (tail
));
9682 ASET (attrs
, coding_attr_charset_valids
, val
);
9683 category
= coding_category_charset
;
9685 else if (EQ (coding_type
, Qccl
))
9689 if (nargs
< coding_arg_ccl_max
)
9692 val
= args
[coding_arg_ccl_decoder
];
9693 CHECK_CCL_PROGRAM (val
);
9695 val
= Fcopy_sequence (val
);
9696 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9698 val
= args
[coding_arg_ccl_encoder
];
9699 CHECK_CCL_PROGRAM (val
);
9701 val
= Fcopy_sequence (val
);
9702 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9704 val
= args
[coding_arg_ccl_valids
];
9705 valids
= Fmake_string (make_number (256), make_number (0));
9706 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
9713 if (! (0 <= XINT (val
) && XINT (val
) <= 255))
9714 args_out_of_range_3 (val
, make_number (0), make_number (255));
9715 from
= to
= XINT (val
);
9720 CHECK_NATNUM_CAR (val
);
9721 CHECK_NUMBER_CDR (val
);
9722 if (XINT (XCAR (val
)) > 255)
9723 args_out_of_range_3 (XCAR (val
),
9724 make_number (0), make_number (255));
9725 from
= XINT (XCAR (val
));
9726 if (! (from
<= XINT (XCDR (val
)) && XINT (XCDR (val
)) <= 255))
9727 args_out_of_range_3 (XCDR (val
),
9728 XCAR (val
), make_number (255));
9729 to
= XINT (XCDR (val
));
9731 for (i
= from
; i
<= to
; i
++)
9732 SSET (valids
, i
, 1);
9734 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9736 category
= coding_category_ccl
;
9738 else if (EQ (coding_type
, Qutf_16
))
9740 Lisp_Object bom
, endian
;
9742 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9744 if (nargs
< coding_arg_utf16_max
)
9747 bom
= args
[coding_arg_utf16_bom
];
9748 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9752 CHECK_CODING_SYSTEM (val
);
9754 CHECK_CODING_SYSTEM (val
);
9756 ASET (attrs
, coding_attr_utf_bom
, bom
);
9758 endian
= args
[coding_arg_utf16_endian
];
9759 CHECK_SYMBOL (endian
);
9762 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9763 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9764 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9766 category
= (CONSP (bom
)
9767 ? coding_category_utf_16_auto
9769 ? (EQ (endian
, Qbig
)
9770 ? coding_category_utf_16_be_nosig
9771 : coding_category_utf_16_le_nosig
)
9772 : (EQ (endian
, Qbig
)
9773 ? coding_category_utf_16_be
9774 : coding_category_utf_16_le
));
9776 else if (EQ (coding_type
, Qiso_2022
))
9778 Lisp_Object initial
, reg_usage
, request
, flags
;
9780 if (nargs
< coding_arg_iso2022_max
)
9783 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9784 CHECK_VECTOR (initial
);
9785 for (i
= 0; i
< 4; i
++)
9787 val
= Faref (initial
, make_number (i
));
9790 struct charset
*charset
;
9792 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9793 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9794 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9795 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9798 ASET (initial
, i
, make_number (-1));
9801 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9802 CHECK_CONS (reg_usage
);
9803 CHECK_NUMBER_CAR (reg_usage
);
9804 CHECK_NUMBER_CDR (reg_usage
);
9806 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9807 for (tail
= request
; CONSP (tail
); tail
= XCDR (tail
))
9815 CHECK_CHARSET_GET_ID (tmp1
, id
);
9816 CHECK_NATNUM_CDR (val
);
9817 if (XINT (XCDR (val
)) >= 4)
9818 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
9819 XSETCAR (val
, make_number (id
));
9822 flags
= args
[coding_arg_iso2022_flags
];
9823 CHECK_NATNUM (flags
);
9824 i
= XINT (flags
) & INT_MAX
;
9825 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9826 i
|= CODING_ISO_FLAG_FULL_SUPPORT
;
9827 flags
= make_number (i
);
9829 ASET (attrs
, coding_attr_iso_initial
, initial
);
9830 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9831 ASET (attrs
, coding_attr_iso_request
, request
);
9832 ASET (attrs
, coding_attr_iso_flags
, flags
);
9833 setup_iso_safe_charsets (attrs
);
9835 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9836 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9837 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9838 ? coding_category_iso_7_else
9839 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9840 ? coding_category_iso_7
9841 : coding_category_iso_7_tight
);
9844 int id
= XINT (AREF (initial
, 1));
9846 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9847 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9849 ? coding_category_iso_8_else
9850 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9851 ? coding_category_iso_8_1
9852 : coding_category_iso_8_2
);
9854 if (category
!= coding_category_iso_8_1
9855 && category
!= coding_category_iso_8_2
)
9856 ASET (attrs
, coding_attr_ascii_compat
, Qnil
);
9858 else if (EQ (coding_type
, Qemacs_mule
))
9860 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9861 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9862 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9863 category
= coding_category_emacs_mule
;
9865 else if (EQ (coding_type
, Qshift_jis
))
9868 struct charset
*charset
;
9870 if (XINT (Flength (charset_list
)) != 3
9871 && XINT (Flength (charset_list
)) != 4)
9872 error ("There should be three or four charsets");
9874 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9875 if (CHARSET_DIMENSION (charset
) != 1)
9876 error ("Dimension of charset %s is not one",
9877 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9878 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9879 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9881 charset_list
= XCDR (charset_list
);
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
))));
9887 charset_list
= XCDR (charset_list
);
9888 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9889 if (CHARSET_DIMENSION (charset
) != 2)
9890 error ("Dimension of charset %s is not two",
9891 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9893 charset_list
= XCDR (charset_list
);
9894 if (! NILP (charset_list
))
9896 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9897 if (CHARSET_DIMENSION (charset
) != 2)
9898 error ("Dimension of charset %s is not two",
9899 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9902 category
= coding_category_sjis
;
9903 Vsjis_coding_system
= name
;
9905 else if (EQ (coding_type
, Qbig5
))
9907 struct charset
*charset
;
9909 if (XINT (Flength (charset_list
)) != 2)
9910 error ("There should be just two charsets");
9912 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9913 if (CHARSET_DIMENSION (charset
) != 1)
9914 error ("Dimension of charset %s is not one",
9915 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9916 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9917 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9919 charset_list
= XCDR (charset_list
);
9920 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9921 if (CHARSET_DIMENSION (charset
) != 2)
9922 error ("Dimension of charset %s is not two",
9923 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9925 category
= coding_category_big5
;
9926 Vbig5_coding_system
= name
;
9928 else if (EQ (coding_type
, Qraw_text
))
9930 category
= coding_category_raw_text
;
9931 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9933 else if (EQ (coding_type
, Qutf_8
))
9937 if (nargs
< coding_arg_utf8_max
)
9940 bom
= args
[coding_arg_utf8_bom
];
9941 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9945 CHECK_CODING_SYSTEM (val
);
9947 CHECK_CODING_SYSTEM (val
);
9949 ASET (attrs
, coding_attr_utf_bom
, bom
);
9951 ASET (attrs
, coding_attr_ascii_compat
, Qt
);
9953 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9954 : NILP (bom
) ? coding_category_utf_8_nosig
9955 : coding_category_utf_8_sig
);
9957 else if (EQ (coding_type
, Qundecided
))
9958 category
= coding_category_undecided
;
9960 error ("Invalid coding system type: %s",
9961 SDATA (SYMBOL_NAME (coding_type
)));
9963 ASET (attrs
, coding_attr_category
, make_number (category
));
9964 ASET (attrs
, coding_attr_plist
,
9966 Fcons (AREF (Vcoding_category_table
, category
),
9967 CODING_ATTR_PLIST (attrs
))));
9968 ASET (attrs
, coding_attr_plist
,
9969 Fcons (QCascii_compatible_p
,
9970 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9971 CODING_ATTR_PLIST (attrs
))));
9973 eol_type
= args
[coding_arg_eol_type
];
9974 if (! NILP (eol_type
)
9975 && ! EQ (eol_type
, Qunix
)
9976 && ! EQ (eol_type
, Qdos
)
9977 && ! EQ (eol_type
, Qmac
))
9978 error ("Invalid eol-type");
9980 aliases
= Fcons (name
, Qnil
);
9982 if (NILP (eol_type
))
9984 eol_type
= make_subsidiaries (name
);
9985 for (i
= 0; i
< 3; i
++)
9987 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9989 this_name
= AREF (eol_type
, i
);
9990 this_aliases
= Fcons (this_name
, Qnil
);
9991 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9992 this_spec
= Fmake_vector (make_number (3), attrs
);
9993 ASET (this_spec
, 1, this_aliases
);
9994 ASET (this_spec
, 2, this_eol_type
);
9995 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9996 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9997 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9999 Vcoding_system_alist
10000 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10001 Vcoding_system_alist
);
10005 spec_vec
= Fmake_vector (make_number (3), attrs
);
10006 ASET (spec_vec
, 1, aliases
);
10007 ASET (spec_vec
, 2, eol_type
);
10009 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10010 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10011 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10013 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10014 Vcoding_system_alist
);
10017 int id
= coding_categories
[category
].id
;
10019 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10020 setup_coding_system (name
, &coding_categories
[category
]);
10026 return Fsignal (Qwrong_number_of_arguments
,
10027 Fcons (intern ("define-coding-system-internal"),
10028 make_number (nargs
)));
10032 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10034 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10035 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10037 Lisp_Object spec
, attrs
;
10039 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10040 attrs
= AREF (spec
, 0);
10041 if (EQ (prop
, QCmnemonic
))
10043 if (! STRINGP (val
))
10044 CHECK_CHARACTER (val
);
10045 ASET (attrs
, coding_attr_mnemonic
, val
);
10047 else if (EQ (prop
, QCdefault_char
))
10050 val
= make_number (' ');
10052 CHECK_CHARACTER (val
);
10053 ASET (attrs
, coding_attr_default_char
, val
);
10055 else if (EQ (prop
, QCdecode_translation_table
))
10057 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10058 CHECK_SYMBOL (val
);
10059 ASET (attrs
, coding_attr_decode_tbl
, val
);
10061 else if (EQ (prop
, QCencode_translation_table
))
10063 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10064 CHECK_SYMBOL (val
);
10065 ASET (attrs
, coding_attr_encode_tbl
, val
);
10067 else if (EQ (prop
, QCpost_read_conversion
))
10069 CHECK_SYMBOL (val
);
10070 ASET (attrs
, coding_attr_post_read
, val
);
10072 else if (EQ (prop
, QCpre_write_conversion
))
10074 CHECK_SYMBOL (val
);
10075 ASET (attrs
, coding_attr_pre_write
, val
);
10077 else if (EQ (prop
, QCascii_compatible_p
))
10079 ASET (attrs
, coding_attr_ascii_compat
, val
);
10082 ASET (attrs
, coding_attr_plist
,
10083 Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
));
10088 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10089 Sdefine_coding_system_alias
, 2, 2, 0,
10090 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10091 (Lisp_Object alias
, Lisp_Object coding_system
)
10093 Lisp_Object spec
, aliases
, eol_type
, val
;
10095 CHECK_SYMBOL (alias
);
10096 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10097 aliases
= AREF (spec
, 1);
10098 /* ALIASES should be a list of length more than zero, and the first
10099 element is a base coding system. Append ALIAS at the tail of the
10101 while (!NILP (XCDR (aliases
)))
10102 aliases
= XCDR (aliases
);
10103 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10105 eol_type
= AREF (spec
, 2);
10106 if (VECTORP (eol_type
))
10108 Lisp_Object subsidiaries
;
10111 subsidiaries
= make_subsidiaries (alias
);
10112 for (i
= 0; i
< 3; i
++)
10113 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10114 AREF (eol_type
, i
));
10117 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10118 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10119 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10121 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10122 Vcoding_system_alist
);
10127 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10129 doc
: /* Return the base of CODING-SYSTEM.
10130 Any alias or subsidiary coding system is not a base coding system. */)
10131 (Lisp_Object coding_system
)
10133 Lisp_Object spec
, attrs
;
10135 if (NILP (coding_system
))
10136 return (Qno_conversion
);
10137 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10138 attrs
= AREF (spec
, 0);
10139 return CODING_ATTR_BASE_NAME (attrs
);
10142 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10144 doc
: "Return the property list of CODING-SYSTEM.")
10145 (Lisp_Object coding_system
)
10147 Lisp_Object spec
, attrs
;
10149 if (NILP (coding_system
))
10150 coding_system
= Qno_conversion
;
10151 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10152 attrs
= AREF (spec
, 0);
10153 return CODING_ATTR_PLIST (attrs
);
10157 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10159 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10160 (Lisp_Object coding_system
)
10164 if (NILP (coding_system
))
10165 coding_system
= Qno_conversion
;
10166 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10167 return AREF (spec
, 1);
10170 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10171 Scoding_system_eol_type
, 1, 1, 0,
10172 doc
: /* Return eol-type of CODING-SYSTEM.
10173 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10175 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10176 and CR respectively.
10178 A vector value indicates that a format of end-of-line should be
10179 detected automatically. Nth element of the vector is the subsidiary
10180 coding system whose eol-type is N. */)
10181 (Lisp_Object coding_system
)
10183 Lisp_Object spec
, eol_type
;
10186 if (NILP (coding_system
))
10187 coding_system
= Qno_conversion
;
10188 if (! CODING_SYSTEM_P (coding_system
))
10190 spec
= CODING_SYSTEM_SPEC (coding_system
);
10191 eol_type
= AREF (spec
, 2);
10192 if (VECTORP (eol_type
))
10193 return Fcopy_sequence (eol_type
);
10194 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10195 return make_number (n
);
10201 /*** 9. Post-amble ***/
10204 init_coding_once (void)
10208 for (i
= 0; i
< coding_category_max
; i
++)
10210 coding_categories
[i
].id
= -1;
10211 coding_priorities
[i
] = i
;
10214 /* ISO2022 specific initialize routine. */
10215 for (i
= 0; i
< 0x20; i
++)
10216 iso_code_class
[i
] = ISO_control_0
;
10217 for (i
= 0x21; i
< 0x7F; i
++)
10218 iso_code_class
[i
] = ISO_graphic_plane_0
;
10219 for (i
= 0x80; i
< 0xA0; i
++)
10220 iso_code_class
[i
] = ISO_control_1
;
10221 for (i
= 0xA1; i
< 0xFF; i
++)
10222 iso_code_class
[i
] = ISO_graphic_plane_1
;
10223 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10224 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10225 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10226 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10227 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10228 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10229 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10230 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10231 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10233 for (i
= 0; i
< 256; i
++)
10235 emacs_mule_bytes
[i
] = 1;
10237 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10238 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10239 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10240 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10246 syms_of_coding (void)
10248 staticpro (&Vcoding_system_hash_table
);
10250 Lisp_Object args
[2];
10253 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10256 staticpro (&Vsjis_coding_system
);
10257 Vsjis_coding_system
= Qnil
;
10259 staticpro (&Vbig5_coding_system
);
10260 Vbig5_coding_system
= Qnil
;
10262 staticpro (&Vcode_conversion_reused_workbuf
);
10263 Vcode_conversion_reused_workbuf
= Qnil
;
10265 staticpro (&Vcode_conversion_workbuf_name
);
10266 Vcode_conversion_workbuf_name
= build_pure_c_string (" *code-conversion-work*");
10268 reused_workbuf_in_use
= 0;
10270 DEFSYM (Qcharset
, "charset");
10271 DEFSYM (Qtarget_idx
, "target-idx");
10272 DEFSYM (Qcoding_system_history
, "coding-system-history");
10273 Fset (Qcoding_system_history
, Qnil
);
10275 /* Target FILENAME is the first argument. */
10276 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10277 /* Target FILENAME is the third argument. */
10278 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10280 DEFSYM (Qcall_process
, "call-process");
10281 /* Target PROGRAM is the first argument. */
10282 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10284 DEFSYM (Qcall_process_region
, "call-process-region");
10285 /* Target PROGRAM is the third argument. */
10286 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10288 DEFSYM (Qstart_process
, "start-process");
10289 /* Target PROGRAM is the third argument. */
10290 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10292 DEFSYM (Qopen_network_stream
, "open-network-stream");
10293 /* Target SERVICE is the fourth argument. */
10294 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10296 DEFSYM (Qcoding_system
, "coding-system");
10297 DEFSYM (Qcoding_aliases
, "coding-aliases");
10299 DEFSYM (Qeol_type
, "eol-type");
10300 DEFSYM (Qunix
, "unix");
10301 DEFSYM (Qdos
, "dos");
10303 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10304 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10305 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10306 DEFSYM (Qdefault_char
, "default-char");
10307 DEFSYM (Qundecided
, "undecided");
10308 DEFSYM (Qno_conversion
, "no-conversion");
10309 DEFSYM (Qraw_text
, "raw-text");
10311 DEFSYM (Qiso_2022
, "iso-2022");
10313 DEFSYM (Qutf_8
, "utf-8");
10314 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10316 DEFSYM (Qutf_16
, "utf-16");
10317 DEFSYM (Qbig
, "big");
10318 DEFSYM (Qlittle
, "little");
10320 DEFSYM (Qshift_jis
, "shift-jis");
10321 DEFSYM (Qbig5
, "big5");
10323 DEFSYM (Qcoding_system_p
, "coding-system-p");
10325 DEFSYM (Qcoding_system_error
, "coding-system-error");
10326 Fput (Qcoding_system_error
, Qerror_conditions
,
10327 listn (CONSTYPE_PURE
, 2, Qcoding_system_error
, Qerror
));
10328 Fput (Qcoding_system_error
, Qerror_message
,
10329 build_pure_c_string ("Invalid coding system"));
10331 /* Intern this now in case it isn't already done.
10332 Setting this variable twice is harmless.
10333 But don't staticpro it here--that is done in alloc.c. */
10334 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10336 DEFSYM (Qtranslation_table
, "translation-table");
10337 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10338 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10339 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10340 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10342 DEFSYM (Qvalid_codes
, "valid-codes");
10344 DEFSYM (Qemacs_mule
, "emacs-mule");
10346 DEFSYM (QCcategory
, ":category");
10347 DEFSYM (QCmnemonic
, ":mnemonic");
10348 DEFSYM (QCdefault_char
, ":default-char");
10349 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10350 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10351 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10352 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10353 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10355 Vcoding_category_table
10356 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10357 staticpro (&Vcoding_category_table
);
10358 /* Followings are target of code detection. */
10359 ASET (Vcoding_category_table
, coding_category_iso_7
,
10360 intern_c_string ("coding-category-iso-7"));
10361 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10362 intern_c_string ("coding-category-iso-7-tight"));
10363 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10364 intern_c_string ("coding-category-iso-8-1"));
10365 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10366 intern_c_string ("coding-category-iso-8-2"));
10367 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10368 intern_c_string ("coding-category-iso-7-else"));
10369 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10370 intern_c_string ("coding-category-iso-8-else"));
10371 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10372 intern_c_string ("coding-category-utf-8-auto"));
10373 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10374 intern_c_string ("coding-category-utf-8"));
10375 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10376 intern_c_string ("coding-category-utf-8-sig"));
10377 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10378 intern_c_string ("coding-category-utf-16-be"));
10379 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10380 intern_c_string ("coding-category-utf-16-auto"));
10381 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10382 intern_c_string ("coding-category-utf-16-le"));
10383 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10384 intern_c_string ("coding-category-utf-16-be-nosig"));
10385 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10386 intern_c_string ("coding-category-utf-16-le-nosig"));
10387 ASET (Vcoding_category_table
, coding_category_charset
,
10388 intern_c_string ("coding-category-charset"));
10389 ASET (Vcoding_category_table
, coding_category_sjis
,
10390 intern_c_string ("coding-category-sjis"));
10391 ASET (Vcoding_category_table
, coding_category_big5
,
10392 intern_c_string ("coding-category-big5"));
10393 ASET (Vcoding_category_table
, coding_category_ccl
,
10394 intern_c_string ("coding-category-ccl"));
10395 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10396 intern_c_string ("coding-category-emacs-mule"));
10397 /* Followings are NOT target of code detection. */
10398 ASET (Vcoding_category_table
, coding_category_raw_text
,
10399 intern_c_string ("coding-category-raw-text"));
10400 ASET (Vcoding_category_table
, coding_category_undecided
,
10401 intern_c_string ("coding-category-undecided"));
10403 DEFSYM (Qinsufficient_source
, "insufficient-source");
10404 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10405 DEFSYM (Qinvalid_source
, "invalid-source");
10406 DEFSYM (Qinterrupted
, "interrupted");
10407 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10408 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10410 defsubr (&Scoding_system_p
);
10411 defsubr (&Sread_coding_system
);
10412 defsubr (&Sread_non_nil_coding_system
);
10413 defsubr (&Scheck_coding_system
);
10414 defsubr (&Sdetect_coding_region
);
10415 defsubr (&Sdetect_coding_string
);
10416 defsubr (&Sfind_coding_systems_region_internal
);
10417 defsubr (&Sunencodable_char_position
);
10418 defsubr (&Scheck_coding_systems_region
);
10419 defsubr (&Sdecode_coding_region
);
10420 defsubr (&Sencode_coding_region
);
10421 defsubr (&Sdecode_coding_string
);
10422 defsubr (&Sencode_coding_string
);
10423 defsubr (&Sdecode_sjis_char
);
10424 defsubr (&Sencode_sjis_char
);
10425 defsubr (&Sdecode_big5_char
);
10426 defsubr (&Sencode_big5_char
);
10427 defsubr (&Sset_terminal_coding_system_internal
);
10428 defsubr (&Sset_safe_terminal_coding_system_internal
);
10429 defsubr (&Sterminal_coding_system
);
10430 defsubr (&Sset_keyboard_coding_system_internal
);
10431 defsubr (&Skeyboard_coding_system
);
10432 defsubr (&Sfind_operation_coding_system
);
10433 defsubr (&Sset_coding_system_priority
);
10434 defsubr (&Sdefine_coding_system_internal
);
10435 defsubr (&Sdefine_coding_system_alias
);
10436 defsubr (&Scoding_system_put
);
10437 defsubr (&Scoding_system_base
);
10438 defsubr (&Scoding_system_plist
);
10439 defsubr (&Scoding_system_aliases
);
10440 defsubr (&Scoding_system_eol_type
);
10441 defsubr (&Scoding_system_priority_list
);
10443 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10444 doc
: /* List of coding systems.
10446 Do not alter the value of this variable manually. This variable should be
10447 updated by the functions `define-coding-system' and
10448 `define-coding-system-alias'. */);
10449 Vcoding_system_list
= Qnil
;
10451 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10452 doc
: /* Alist of coding system names.
10453 Each element is one element list of coding system name.
10454 This variable is given to `completing-read' as COLLECTION argument.
10456 Do not alter the value of this variable manually. This variable should be
10457 updated by the functions `make-coding-system' and
10458 `define-coding-system-alias'. */);
10459 Vcoding_system_alist
= Qnil
;
10461 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10462 doc
: /* List of coding-categories (symbols) ordered by priority.
10464 On detecting a coding system, Emacs tries code detection algorithms
10465 associated with each coding-category one by one in this order. When
10466 one algorithm agrees with a byte sequence of source text, the coding
10467 system bound to the corresponding coding-category is selected.
10469 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10473 Vcoding_category_list
= Qnil
;
10474 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10475 Vcoding_category_list
10476 = Fcons (AREF (Vcoding_category_table
, i
),
10477 Vcoding_category_list
);
10480 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10481 doc
: /* Specify the coding system for read operations.
10482 It is useful to bind this variable with `let', but do not set it globally.
10483 If the value is a coding system, it is used for decoding on read operation.
10484 If not, an appropriate element is used from one of the coding system alists.
10485 There are three such tables: `file-coding-system-alist',
10486 `process-coding-system-alist', and `network-coding-system-alist'. */);
10487 Vcoding_system_for_read
= Qnil
;
10489 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10490 doc
: /* Specify the coding system for write operations.
10491 Programs bind this variable with `let', but you should not set it globally.
10492 If the value is a coding system, it is used for encoding of output,
10493 when writing it to a file and when sending it to a file or subprocess.
10495 If this does not specify a coding system, an appropriate element
10496 is used from one of the coding system alists.
10497 There are three such tables: `file-coding-system-alist',
10498 `process-coding-system-alist', and `network-coding-system-alist'.
10499 For output to files, if the above procedure does not specify a coding system,
10500 the value of `buffer-file-coding-system' is used. */);
10501 Vcoding_system_for_write
= Qnil
;
10503 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10505 Coding system used in the latest file or process I/O. */);
10506 Vlast_coding_system_used
= Qnil
;
10508 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10510 Error status of the last code conversion.
10512 When an error was detected in the last code conversion, this variable
10513 is set to one of the following symbols.
10514 `insufficient-source'
10518 `insufficient-memory'
10519 When no error was detected, the value doesn't change. So, to check
10520 the error status of a code conversion by this variable, you must
10521 explicitly set this variable to nil before performing code
10523 Vlast_code_conversion_error
= Qnil
;
10525 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10527 *Non-nil means always inhibit code conversion of end-of-line format.
10528 See info node `Coding Systems' and info node `Text and Binary' concerning
10529 such conversion. */);
10530 inhibit_eol_conversion
= 0;
10532 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10534 Non-nil means process buffer inherits coding system of process output.
10535 Bind it to t if the process output is to be treated as if it were a file
10536 read from some filesystem. */);
10537 inherit_process_coding_system
= 0;
10539 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10541 Alist to decide a coding system to use for a file I/O operation.
10542 The format is ((PATTERN . VAL) ...),
10543 where PATTERN is a regular expression matching a file name,
10544 VAL is a coding system, a cons of coding systems, or a function symbol.
10545 If VAL is a coding system, it is used for both decoding and encoding
10547 If VAL is a cons of coding systems, the car part is used for decoding,
10548 and the cdr part is used for encoding.
10549 If VAL is a function symbol, the function must return a coding system
10550 or a cons of coding systems which are used as above. The function is
10551 called with an argument that is a list of the arguments with which
10552 `find-operation-coding-system' was called. If the function can't decide
10553 a coding system, it can return `undecided' so that the normal
10554 code-detection is performed.
10556 See also the function `find-operation-coding-system'
10557 and the variable `auto-coding-alist'. */);
10558 Vfile_coding_system_alist
= Qnil
;
10560 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10562 Alist to decide a coding system to use for a process I/O operation.
10563 The format is ((PATTERN . VAL) ...),
10564 where PATTERN is a regular expression matching a program name,
10565 VAL is a coding system, a cons of coding systems, or a function symbol.
10566 If VAL is a coding system, it is used for both decoding what received
10567 from the program and encoding what sent to the program.
10568 If VAL is a cons of coding systems, the car part is used for decoding,
10569 and the cdr part is used for encoding.
10570 If VAL is a function symbol, the function must return a coding system
10571 or a cons of coding systems which are used as above.
10573 See also the function `find-operation-coding-system'. */);
10574 Vprocess_coding_system_alist
= Qnil
;
10576 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10578 Alist to decide a coding system to use for a network I/O operation.
10579 The format is ((PATTERN . VAL) ...),
10580 where PATTERN is a regular expression matching a network service name
10581 or is a port number to connect to,
10582 VAL is a coding system, a cons of coding systems, or a function symbol.
10583 If VAL is a coding system, it is used for both decoding what received
10584 from the network stream and encoding what sent to the network stream.
10585 If VAL is a cons of coding systems, the car part is used for decoding,
10586 and the cdr part is used for encoding.
10587 If VAL is a function symbol, the function must return a coding system
10588 or a cons of coding systems which are used as above.
10590 See also the function `find-operation-coding-system'. */);
10591 Vnetwork_coding_system_alist
= Qnil
;
10593 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10594 doc
: /* Coding system to use with system messages.
10595 Also used for decoding keyboard input on X Window system. */);
10596 Vlocale_coding_system
= Qnil
;
10598 /* The eol mnemonics are reset in startup.el system-dependently. */
10599 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10601 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10602 eol_mnemonic_unix
= build_pure_c_string (":");
10604 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10606 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10607 eol_mnemonic_dos
= build_pure_c_string ("\\");
10609 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10611 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10612 eol_mnemonic_mac
= build_pure_c_string ("/");
10614 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10616 *String displayed in mode line when end-of-line format is not yet determined. */);
10617 eol_mnemonic_undecided
= build_pure_c_string (":");
10619 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10621 *Non-nil enables character translation while encoding and decoding. */);
10622 Venable_character_translation
= Qt
;
10624 DEFVAR_LISP ("standard-translation-table-for-decode",
10625 Vstandard_translation_table_for_decode
,
10626 doc
: /* Table for translating characters while decoding. */);
10627 Vstandard_translation_table_for_decode
= Qnil
;
10629 DEFVAR_LISP ("standard-translation-table-for-encode",
10630 Vstandard_translation_table_for_encode
,
10631 doc
: /* Table for translating characters while encoding. */);
10632 Vstandard_translation_table_for_encode
= Qnil
;
10634 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10635 doc
: /* Alist of charsets vs revision numbers.
10636 While encoding, if a charset (car part of an element) is found,
10637 designate it with the escape sequence identifying revision (cdr part
10638 of the element). */);
10639 Vcharset_revision_table
= Qnil
;
10641 DEFVAR_LISP ("default-process-coding-system",
10642 Vdefault_process_coding_system
,
10643 doc
: /* Cons of coding systems used for process I/O by default.
10644 The car part is used for decoding a process output,
10645 the cdr part is used for encoding a text to be sent to a process. */);
10646 Vdefault_process_coding_system
= Qnil
;
10648 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10650 Table of extra Latin codes in the range 128..159 (inclusive).
10651 This is a vector of length 256.
10652 If Nth element is non-nil, the existence of code N in a file
10653 \(or output of subprocess) doesn't prevent it to be detected as
10654 a coding system of ISO 2022 variant which has a flag
10655 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10656 or reading output of a subprocess.
10657 Only 128th through 159th elements have a meaning. */);
10658 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10660 DEFVAR_LISP ("select-safe-coding-system-function",
10661 Vselect_safe_coding_system_function
,
10663 Function to call to select safe coding system for encoding a text.
10665 If set, this function is called to force a user to select a proper
10666 coding system which can encode the text in the case that a default
10667 coding system used in each operation can't encode the text. The
10668 function should take care that the buffer is not modified while
10669 the coding system is being selected.
10671 The default value is `select-safe-coding-system' (which see). */);
10672 Vselect_safe_coding_system_function
= Qnil
;
10674 DEFVAR_BOOL ("coding-system-require-warning",
10675 coding_system_require_warning
,
10676 doc
: /* Internal use only.
10677 If non-nil, on writing a file, `select-safe-coding-system-function' is
10678 called even if `coding-system-for-write' is non-nil. The command
10679 `universal-coding-system-argument' binds this variable to t temporarily. */);
10680 coding_system_require_warning
= 0;
10683 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10684 inhibit_iso_escape_detection
,
10686 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10688 When Emacs reads text, it tries to detect how the text is encoded.
10689 This code detection is sensitive to escape sequences. If Emacs sees
10690 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10691 of the ISO2022 encodings, and decodes text by the corresponding coding
10692 system (e.g. `iso-2022-7bit').
10694 However, there may be a case that you want to read escape sequences in
10695 a file as is. In such a case, you can set this variable to non-nil.
10696 Then the code detection will ignore any escape sequences, and no text is
10697 detected as encoded in some ISO-2022 encoding. The result is that all
10698 escape sequences become visible in a buffer.
10700 The default value is nil, and it is strongly recommended not to change
10701 it. That is because many Emacs Lisp source files that contain
10702 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10703 in Emacs's distribution, and they won't be decoded correctly on
10704 reading if you suppress escape sequence detection.
10706 The other way to read escape sequences in a file without decoding is
10707 to explicitly specify some coding system that doesn't use ISO-2022
10708 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10709 inhibit_iso_escape_detection
= 0;
10711 DEFVAR_BOOL ("inhibit-null-byte-detection",
10712 inhibit_null_byte_detection
,
10713 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10714 By default, Emacs treats it as binary data, and does not attempt to
10715 decode it. The effect is as if you specified `no-conversion' for
10718 Set this to non-nil when a regular text happens to include null bytes.
10719 Examples are Index nodes of Info files and null-byte delimited output
10720 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10721 decode text as usual. */);
10722 inhibit_null_byte_detection
= 0;
10724 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10725 doc
: /* Char table for translating self-inserting characters.
10726 This is applied to the result of input methods, not their input.
10727 See also `keyboard-translate-table'.
10729 Use of this variable for character code unification was rendered
10730 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10731 internal character representation. */);
10732 Vtranslation_table_for_input
= Qnil
;
10735 Lisp_Object args
[coding_arg_max
];
10736 Lisp_Object plist
[16];
10739 for (i
= 0; i
< coding_arg_max
; i
++)
10742 plist
[0] = intern_c_string (":name");
10743 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10744 plist
[2] = intern_c_string (":mnemonic");
10745 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10746 plist
[4] = intern_c_string (":coding-type");
10747 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10748 plist
[6] = intern_c_string (":ascii-compatible-p");
10749 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10750 plist
[8] = intern_c_string (":default-char");
10751 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10752 plist
[10] = intern_c_string (":for-unibyte");
10753 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10754 plist
[12] = intern_c_string (":docstring");
10755 plist
[13] = build_pure_c_string ("Do no conversion.\n\
10757 When you visit a file with this coding, the file is read into a\n\
10758 unibyte buffer as is, thus each byte of a file is treated as a\n\
10760 plist
[14] = intern_c_string (":eol-type");
10761 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10762 args
[coding_arg_plist
] = Flist (16, plist
);
10763 Fdefine_coding_system_internal (coding_arg_max
, args
);
10765 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10766 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10767 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10768 /* This is already set.
10769 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10770 plist
[8] = intern_c_string (":charset-list");
10771 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10772 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10773 plist
[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10774 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10775 args
[coding_arg_plist
] = Flist (16, plist
);
10776 Fdefine_coding_system_internal (coding_arg_max
, args
);
10779 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10784 for (i
= 0; i
< coding_category_max
; i
++)
10785 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10787 #if defined (DOS_NT)
10788 system_eol_type
= Qdos
;
10790 system_eol_type
= Qunix
;
10792 staticpro (&system_eol_type
);
10796 emacs_strerror (int error_number
)
10800 synchronize_system_messages_locale ();
10801 str
= strerror (error_number
);
10803 if (! NILP (Vlocale_coding_system
))
10805 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10806 Vlocale_coding_system
,
10808 str
= SSDATA (dec
);