1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001-2011 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
13 GNU Emacs is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 /*** TABLE OF CONTENTS ***
30 2. Emacs' internal format (emacs-utf-8) handlers
33 5. Charset-base coding systems handlers
34 6. emacs-mule (old Emacs' internal format) handlers
36 8. Shift-JIS and BIG5 handlers
38 10. C library functions
39 11. Emacs Lisp library functions
44 /*** 0. General comments ***
49 A coding system is an object for an encoding mechanism that contains
50 information about how to convert byte sequences to character
51 sequences and vice versa. When we say "decode", it means converting
52 a byte sequence of a specific coding system into a character
53 sequence that is represented by Emacs' internal coding system
54 `emacs-utf-8', and when we say "encode", it means converting a
55 character sequence of emacs-utf-8 to a byte sequence of a specific
58 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
59 C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
71 o Charset-base coding system
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
77 o Old Emacs internal format (emacs-mule)
79 The coding system adopted by old versions of Emacs (20 and 21).
81 o ISO2022-base coding system
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
117 Like raw text, but don't do end-of-line conversion.
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. MacOS's format is usually one byte of
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
150 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
152 Below is the template of these functions. */
156 detect_coding_XXX (struct coding_system
*coding
,
157 struct coding_detection_info
*detect_info
)
159 const unsigned char *src
= coding
->source
;
160 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
161 int multibytep
= coding
->src_multibyte
;
162 EMACS_INT consumed_chars
= 0;
168 /* Get one byte from the source. If the source is exhausted, jump
169 to no_more_source:. */
172 if (! __C_conforms_to_XXX___ (c
))
174 if (! __C_strongly_suggests_XXX__ (c
))
175 found
= CATEGORY_MASK_XXX
;
177 /* The byte sequence is invalid for XXX. */
178 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
182 /* The source exhausted successfully. */
183 detect_info
->found
|= found
;
188 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
190 These functions decode a byte sequence specified as a source by
191 CODING. The resulting multibyte text goes to a place pointed to by
192 CODING->charbuf, the length of which should not exceed
193 CODING->charbuf_size;
195 These functions set the information of original and decoded texts in
196 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
197 They also set CODING->result to one of CODING_RESULT_XXX indicating
198 how the decoding is finished.
200 Below is the template of these functions. */
204 decode_coding_XXXX (struct coding_system
*coding
)
206 const unsigned char *src
= coding
->source
+ coding
->consumed
;
207 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
208 /* SRC_BASE remembers the start position in source in each loop.
209 The loop will be exited when there's not enough source code, or
210 when there's no room in CHARBUF for a decoded character. */
211 const unsigned char *src_base
;
212 /* A buffer to produce decoded characters. */
213 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
214 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
215 int multibytep
= coding
->src_multibyte
;
220 if (charbuf
< charbuf_end
)
221 /* No more room to produce a decoded character. */
228 if (src_base
< src_end
229 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
230 /* If the source ends by partial bytes to construct a character,
231 treat them as eight-bit raw data. */
232 while (src_base
< src_end
&& charbuf
< charbuf_end
)
233 *charbuf
++ = *src_base
++;
234 /* Remember how many bytes and characters we consumed. If the
235 source is multibyte, the bytes and chars are not identical. */
236 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
237 /* Remember how many characters we produced. */
238 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
242 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
244 These functions encode SRC_BYTES length text at SOURCE of Emacs'
245 internal multibyte format by CODING. The resulting byte sequence
246 goes to a place pointed to by DESTINATION, the length of which
247 should not exceed DST_BYTES.
249 These functions set the information of original and encoded texts in
250 the members produced, produced_char, consumed, and consumed_char of
251 the structure *CODING. They also set the member result to one of
252 CODING_RESULT_XXX indicating how the encoding finished.
254 DST_BYTES zero means that source area and destination area are
255 overlapped, which means that we can produce a encoded text until it
256 reaches at the head of not-yet-encoded source text.
258 Below is a template of these functions. */
261 encode_coding_XXX (struct coding_system
*coding
)
263 int multibytep
= coding
->dst_multibyte
;
264 int *charbuf
= coding
->charbuf
;
265 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
266 unsigned char *dst
= coding
->destination
+ coding
->produced
;
267 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
268 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
269 EMACS_INT produced_chars
= 0;
271 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
274 /* Encode C into DST, and increment DST. */
276 label_no_more_destination
:
277 /* How many chars and bytes we produced. */
278 coding
->produced_char
+= produced_chars
;
279 coding
->produced
= dst
- coding
->destination
;
284 /*** 1. Preamble ***/
292 #include "character.h"
295 #include "composite.h"
299 #include "termhooks.h"
301 Lisp_Object Vcoding_system_hash_table
;
303 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 is
655 nonzero, 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 is nonzero 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 is
750 nonzero, store in an appropriate multibyte from. 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); \
809 /* Prototypes for static functions. */
810 static void record_conversion_result (struct coding_system
*coding
,
811 enum coding_result_code result
);
812 static int detect_coding_utf_8 (struct coding_system
*,
813 struct coding_detection_info
*info
);
814 static void decode_coding_utf_8 (struct coding_system
*);
815 static int encode_coding_utf_8 (struct coding_system
*);
817 static int detect_coding_utf_16 (struct coding_system
*,
818 struct coding_detection_info
*info
);
819 static void decode_coding_utf_16 (struct coding_system
*);
820 static int encode_coding_utf_16 (struct coding_system
*);
822 static int detect_coding_iso_2022 (struct coding_system
*,
823 struct coding_detection_info
*info
);
824 static void decode_coding_iso_2022 (struct coding_system
*);
825 static int encode_coding_iso_2022 (struct coding_system
*);
827 static int detect_coding_emacs_mule (struct coding_system
*,
828 struct coding_detection_info
*info
);
829 static void decode_coding_emacs_mule (struct coding_system
*);
830 static int encode_coding_emacs_mule (struct coding_system
*);
832 static int detect_coding_sjis (struct coding_system
*,
833 struct coding_detection_info
*info
);
834 static void decode_coding_sjis (struct coding_system
*);
835 static int encode_coding_sjis (struct coding_system
*);
837 static int detect_coding_big5 (struct coding_system
*,
838 struct coding_detection_info
*info
);
839 static void decode_coding_big5 (struct coding_system
*);
840 static int encode_coding_big5 (struct coding_system
*);
842 static int detect_coding_ccl (struct coding_system
*,
843 struct coding_detection_info
*info
);
844 static void decode_coding_ccl (struct coding_system
*);
845 static int encode_coding_ccl (struct coding_system
*);
847 static void decode_coding_raw_text (struct coding_system
*);
848 static int encode_coding_raw_text (struct coding_system
*);
850 static void coding_set_source (struct coding_system
*);
851 static void coding_set_destination (struct coding_system
*);
852 static void coding_alloc_by_realloc (struct coding_system
*, EMACS_INT
);
853 static void coding_alloc_by_making_gap (struct coding_system
*,
854 EMACS_INT
, EMACS_INT
);
855 static unsigned char *alloc_destination (struct coding_system
*,
856 EMACS_INT
, unsigned char *);
857 static void setup_iso_safe_charsets (Lisp_Object
);
858 static unsigned char *encode_designation_at_bol (struct coding_system
*,
859 int *, unsigned char *);
860 static int detect_eol (const unsigned char *,
861 EMACS_INT
, enum coding_category
);
862 static Lisp_Object
adjust_coding_eol_type (struct coding_system
*, int);
863 static void decode_eol (struct coding_system
*);
864 static Lisp_Object
get_translation_table (Lisp_Object
, int, int *);
865 static Lisp_Object
get_translation (Lisp_Object
, int *, int *);
866 static int produce_chars (struct coding_system
*, Lisp_Object
, int);
867 static INLINE
void produce_charset (struct coding_system
*, int *,
869 static void produce_annotation (struct coding_system
*, EMACS_INT
);
870 static int decode_coding (struct coding_system
*);
871 static INLINE
int *handle_composition_annotation (EMACS_INT
, EMACS_INT
,
872 struct coding_system
*,
874 static INLINE
int *handle_charset_annotation (EMACS_INT
, EMACS_INT
,
875 struct coding_system
*,
877 static void consume_chars (struct coding_system
*, Lisp_Object
, int);
878 static int encode_coding (struct coding_system
*);
879 static Lisp_Object
make_conversion_work_buffer (int);
880 static Lisp_Object
code_conversion_restore (Lisp_Object
);
881 static INLINE
int char_encodable_p (int, Lisp_Object
);
882 static Lisp_Object
make_subsidiaries (Lisp_Object
);
885 record_conversion_result (struct coding_system
*coding
,
886 enum coding_result_code result
)
888 coding
->result
= result
;
891 case CODING_RESULT_INSUFFICIENT_SRC
:
892 Vlast_code_conversion_error
= Qinsufficient_source
;
894 case CODING_RESULT_INCONSISTENT_EOL
:
895 Vlast_code_conversion_error
= Qinconsistent_eol
;
897 case CODING_RESULT_INVALID_SRC
:
898 Vlast_code_conversion_error
= Qinvalid_source
;
900 case CODING_RESULT_INTERRUPT
:
901 Vlast_code_conversion_error
= Qinterrupted
;
903 case CODING_RESULT_INSUFFICIENT_MEM
:
904 Vlast_code_conversion_error
= Qinsufficient_memory
;
906 case CODING_RESULT_INSUFFICIENT_DST
:
907 /* Don't record this error in Vlast_code_conversion_error
908 because it happens just temporarily and is resolved when the
909 whole conversion is finished. */
911 case CODING_RESULT_SUCCESS
:
914 Vlast_code_conversion_error
= intern ("Unknown error");
918 /* This wrapper macro is used to preserve validity of pointers into
919 buffer text across calls to decode_char, which could cause
920 relocation of buffers if it loads a charset map, because loading a
921 charset map allocates large structures. */
922 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
924 charset_map_loaded = 0; \
925 c = DECODE_CHAR (charset, code); \
926 if (charset_map_loaded) \
928 const unsigned char *orig = coding->source; \
931 coding_set_source (coding); \
932 offset = coding->source - orig; \
934 src_base += offset; \
940 /* If there are at least BYTES length of room at dst, allocate memory
941 for coding->destination and update dst and dst_end. We don't have
942 to take care of coding->source which will be relocated. It is
943 handled by calling coding_set_source in encode_coding. */
945 #define ASSURE_DESTINATION(bytes) \
947 if (dst + (bytes) >= dst_end) \
949 EMACS_INT more_bytes = charbuf_end - charbuf + (bytes); \
951 dst = alloc_destination (coding, more_bytes, dst); \
952 dst_end = coding->destination + coding->dst_bytes; \
957 /* Store multibyte form of the character C in P, and advance P to the
958 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
959 never calls MAYBE_UNIFY_CHAR. */
961 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
963 if ((c) <= MAX_1_BYTE_CHAR) \
965 else if ((c) <= MAX_2_BYTE_CHAR) \
966 *(p)++ = (0xC0 | ((c) >> 6)), \
967 *(p)++ = (0x80 | ((c) & 0x3F)); \
968 else if ((c) <= MAX_3_BYTE_CHAR) \
969 *(p)++ = (0xE0 | ((c) >> 12)), \
970 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
971 *(p)++ = (0x80 | ((c) & 0x3F)); \
972 else if ((c) <= MAX_4_BYTE_CHAR) \
973 *(p)++ = (0xF0 | (c >> 18)), \
974 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
975 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
976 *(p)++ = (0x80 | (c & 0x3F)); \
977 else if ((c) <= MAX_5_BYTE_CHAR) \
979 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
980 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
981 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
982 *(p)++ = (0x80 | (c & 0x3F)); \
984 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
988 /* Return the character code of character whose multibyte form is at
989 P, and advance P to the end of the multibyte form. This is like
990 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
992 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
995 : ! ((p)[0] & 0x20) \
997 ((((p)[-2] & 0x1F) << 6) \
999 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1000 : ! ((p)[0] & 0x10) \
1002 ((((p)[-3] & 0x0F) << 12) \
1003 | (((p)[-2] & 0x3F) << 6) \
1004 | ((p)[-1] & 0x3F))) \
1005 : ! ((p)[0] & 0x08) \
1007 ((((p)[-4] & 0xF) << 18) \
1008 | (((p)[-3] & 0x3F) << 12) \
1009 | (((p)[-2] & 0x3F) << 6) \
1010 | ((p)[-1] & 0x3F))) \
1012 ((((p)[-4] & 0x3F) << 18) \
1013 | (((p)[-3] & 0x3F) << 12) \
1014 | (((p)[-2] & 0x3F) << 6) \
1015 | ((p)[-1] & 0x3F))))
1019 coding_set_source (struct coding_system
*coding
)
1021 if (BUFFERP (coding
->src_object
))
1023 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1025 if (coding
->src_pos
< 0)
1026 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1028 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1030 else if (STRINGP (coding
->src_object
))
1032 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1036 /* Otherwise, the source is C string and is never relocated
1037 automatically. Thus we don't have to update anything. */
1042 coding_set_destination (struct coding_system
*coding
)
1044 if (BUFFERP (coding
->dst_object
))
1046 if (coding
->src_pos
< 0)
1048 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1049 coding
->dst_bytes
= (GAP_END_ADDR
1050 - (coding
->src_bytes
- coding
->consumed
)
1051 - coding
->destination
);
1055 /* We are sure that coding->dst_pos_byte is before the gap
1057 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1058 + coding
->dst_pos_byte
- BEG_BYTE
);
1059 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1060 - coding
->destination
);
1065 /* Otherwise, the destination is C string and is never relocated
1066 automatically. Thus we don't have to update anything. */
1072 coding_alloc_by_realloc (struct coding_system
*coding
, EMACS_INT bytes
)
1074 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1075 coding
->dst_bytes
+ bytes
);
1076 coding
->dst_bytes
+= bytes
;
1080 coding_alloc_by_making_gap (struct coding_system
*coding
,
1081 EMACS_INT gap_head_used
, EMACS_INT bytes
)
1083 if (EQ (coding
->src_object
, coding
->dst_object
))
1085 /* The gap may contain the produced data at the head and not-yet
1086 consumed data at the tail. To preserve those data, we at
1087 first make the gap size to zero, then increase the gap
1089 EMACS_INT add
= GAP_SIZE
;
1091 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1092 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1094 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1095 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1099 Lisp_Object this_buffer
;
1101 this_buffer
= Fcurrent_buffer ();
1102 set_buffer_internal (XBUFFER (coding
->dst_object
));
1104 set_buffer_internal (XBUFFER (this_buffer
));
1109 static unsigned char *
1110 alloc_destination (struct coding_system
*coding
, EMACS_INT nbytes
,
1113 EMACS_INT offset
= dst
- coding
->destination
;
1115 if (BUFFERP (coding
->dst_object
))
1117 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1119 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1122 coding_alloc_by_realloc (coding
, nbytes
);
1123 coding_set_destination (coding
);
1124 dst
= coding
->destination
+ offset
;
1128 /** Macros for annotations. */
1130 /* An annotation data is stored in the array coding->charbuf in this
1132 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1133 LENGTH is the number of elements in the annotation.
1134 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1135 NCHARS is the number of characters in the text annotated.
1137 The format of the following elements depend on ANNOTATION_MASK.
1139 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1141 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1143 NBYTES is the number of bytes specified in the header part of
1144 old-style emacs-mule encoding, or 0 for the other kind of
1147 METHOD is one of enum composition_method.
1149 Optional COMPOSITION-COMPONENTS are characters and composition
1152 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1155 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1156 recover from an invalid annotation, and should be skipped by
1157 produce_annotation. */
1159 /* Maximum length of the header of annotation data. */
1160 #define MAX_ANNOTATION_LENGTH 5
1162 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1164 *(buf)++ = -(len); \
1165 *(buf)++ = (mask); \
1166 *(buf)++ = (nchars); \
1167 coding->annotated = 1; \
1170 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1172 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1178 #define ADD_CHARSET_DATA(buf, nchars, id) \
1180 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1185 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1192 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1193 Check if a text is encoded in UTF-8. If it is, return 1, else
1196 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1197 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1198 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1199 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1200 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1201 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1203 #define UTF_8_BOM_1 0xEF
1204 #define UTF_8_BOM_2 0xBB
1205 #define UTF_8_BOM_3 0xBF
1208 detect_coding_utf_8 (struct coding_system
*coding
,
1209 struct coding_detection_info
*detect_info
)
1211 const unsigned char *src
= coding
->source
, *src_base
;
1212 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1213 int multibytep
= coding
->src_multibyte
;
1214 EMACS_INT consumed_chars
= 0;
1218 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1219 /* A coding system of this category is always ASCII compatible. */
1220 src
+= coding
->head_ascii
;
1224 int c
, c1
, c2
, c3
, c4
;
1228 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1231 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1233 if (UTF_8_2_OCTET_LEADING_P (c
))
1239 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1241 if (UTF_8_3_OCTET_LEADING_P (c
))
1244 if (src_base
== coding
->source
1245 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1250 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1252 if (UTF_8_4_OCTET_LEADING_P (c
))
1258 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1260 if (UTF_8_5_OCTET_LEADING_P (c
))
1267 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1271 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1273 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1278 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1279 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1283 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1285 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1292 decode_coding_utf_8 (struct coding_system
*coding
)
1294 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1295 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1296 const unsigned char *src_base
;
1297 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1298 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1299 EMACS_INT consumed_chars
= 0, consumed_chars_base
= 0;
1300 int multibytep
= coding
->src_multibyte
;
1301 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1303 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1304 int byte_after_cr
= -1;
1306 if (bom
!= utf_without_bom
)
1312 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1317 if (! UTF_8_EXTRA_OCTET_P (c2
))
1322 if (! UTF_8_EXTRA_OCTET_P (c3
))
1326 if ((c1
!= UTF_8_BOM_1
)
1327 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1330 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1335 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1339 int c
, c1
, c2
, c3
, c4
, c5
;
1342 consumed_chars_base
= consumed_chars
;
1344 if (charbuf
>= charbuf_end
)
1346 if (byte_after_cr
>= 0)
1351 if (byte_after_cr
>= 0)
1352 c1
= byte_after_cr
, byte_after_cr
= -1;
1359 else if (UTF_8_1_OCTET_P (c1
))
1361 if (eol_dos
&& c1
== '\r')
1362 ONE_MORE_BYTE (byte_after_cr
);
1368 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1370 if (UTF_8_2_OCTET_LEADING_P (c1
))
1372 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1373 /* Reject overlong sequences here and below. Encoders
1374 producing them are incorrect, they can be misleading,
1375 and they mess up read/write invariance. */
1382 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1384 if (UTF_8_3_OCTET_LEADING_P (c1
))
1386 c
= (((c1
& 0xF) << 12)
1387 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1389 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1395 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1397 if (UTF_8_4_OCTET_LEADING_P (c1
))
1399 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1400 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1407 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1409 if (UTF_8_5_OCTET_LEADING_P (c1
))
1411 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1412 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1414 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1429 consumed_chars
= consumed_chars_base
;
1431 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1436 coding
->consumed_char
+= consumed_chars_base
;
1437 coding
->consumed
= src_base
- coding
->source
;
1438 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1443 encode_coding_utf_8 (struct coding_system
*coding
)
1445 int multibytep
= coding
->dst_multibyte
;
1446 int *charbuf
= coding
->charbuf
;
1447 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1448 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1449 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1450 EMACS_INT produced_chars
= 0;
1453 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1455 ASSURE_DESTINATION (3);
1456 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1457 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1462 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1464 while (charbuf
< charbuf_end
)
1466 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1468 ASSURE_DESTINATION (safe_room
);
1470 if (CHAR_BYTE8_P (c
))
1472 c
= CHAR_TO_BYTE8 (c
);
1477 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1478 for (p
= str
; p
< pend
; p
++)
1485 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1487 while (charbuf
< charbuf_end
)
1489 ASSURE_DESTINATION (safe_room
);
1491 if (CHAR_BYTE8_P (c
))
1492 *dst
++ = CHAR_TO_BYTE8 (c
);
1494 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1498 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1499 coding
->produced_char
+= produced_chars
;
1500 coding
->produced
= dst
- coding
->destination
;
1505 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1506 Check if a text is encoded in one of UTF-16 based coding systems.
1507 If it is, return 1, else return 0. */
1509 #define UTF_16_HIGH_SURROGATE_P(val) \
1510 (((val) & 0xFC00) == 0xD800)
1512 #define UTF_16_LOW_SURROGATE_P(val) \
1513 (((val) & 0xFC00) == 0xDC00)
1517 detect_coding_utf_16 (struct coding_system
*coding
,
1518 struct coding_detection_info
*detect_info
)
1520 const unsigned char *src
= coding
->source
;
1521 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1522 int multibytep
= coding
->src_multibyte
;
1525 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1526 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1527 && (coding
->src_chars
& 1))
1529 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1533 TWO_MORE_BYTES (c1
, c2
);
1534 if ((c1
== 0xFF) && (c2
== 0xFE))
1536 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1537 | CATEGORY_MASK_UTF_16_AUTO
);
1538 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1539 | CATEGORY_MASK_UTF_16_BE_NOSIG
1540 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1542 else if ((c1
== 0xFE) && (c2
== 0xFF))
1544 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1545 | CATEGORY_MASK_UTF_16_AUTO
);
1546 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1547 | CATEGORY_MASK_UTF_16_BE_NOSIG
1548 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1552 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1557 /* We check the dispersion of Eth and Oth bytes where E is even and
1558 O is odd. If both are high, we assume binary data.*/
1559 unsigned char e
[256], o
[256];
1560 unsigned e_num
= 1, o_num
= 1;
1567 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1568 |CATEGORY_MASK_UTF_16_BE
1569 | CATEGORY_MASK_UTF_16_LE
);
1571 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1572 != CATEGORY_MASK_UTF_16
)
1574 TWO_MORE_BYTES (c1
, c2
);
1582 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1589 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1600 decode_coding_utf_16 (struct coding_system
*coding
)
1602 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1603 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1604 const unsigned char *src_base
;
1605 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1606 /* We may produces at most 3 chars in one loop. */
1607 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1608 EMACS_INT consumed_chars
= 0, consumed_chars_base
= 0;
1609 int multibytep
= coding
->src_multibyte
;
1610 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1611 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1612 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1614 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1615 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1617 if (bom
== utf_with_bom
)
1626 if (endian
== utf_16_big_endian
1627 ? c
!= 0xFEFF : c
!= 0xFFFE)
1629 /* The first two bytes are not BOM. Treat them as bytes
1630 for a normal character. */
1634 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1636 else if (bom
== utf_detect_bom
)
1638 /* We have already tried to detect BOM and failed in
1640 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1648 consumed_chars_base
= consumed_chars
;
1650 if (charbuf
>= charbuf_end
)
1652 if (byte_after_cr1
>= 0)
1657 if (byte_after_cr1
>= 0)
1658 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1666 if (byte_after_cr2
>= 0)
1667 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1672 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1676 c
= (endian
== utf_16_big_endian
1677 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1681 if (! UTF_16_LOW_SURROGATE_P (c
))
1683 if (endian
== utf_16_big_endian
)
1684 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1686 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1690 if (UTF_16_HIGH_SURROGATE_P (c
))
1691 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1697 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1698 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1699 *charbuf
++ = 0x10000 + c
;
1704 if (UTF_16_HIGH_SURROGATE_P (c
))
1705 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1708 if (eol_dos
&& c
== '\r')
1710 ONE_MORE_BYTE (byte_after_cr1
);
1711 ONE_MORE_BYTE (byte_after_cr2
);
1719 coding
->consumed_char
+= consumed_chars_base
;
1720 coding
->consumed
= src_base
- coding
->source
;
1721 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1725 encode_coding_utf_16 (struct coding_system
*coding
)
1727 int multibytep
= coding
->dst_multibyte
;
1728 int *charbuf
= coding
->charbuf
;
1729 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1730 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1731 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1733 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1734 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1735 EMACS_INT produced_chars
= 0;
1738 if (bom
!= utf_without_bom
)
1740 ASSURE_DESTINATION (safe_room
);
1742 EMIT_TWO_BYTES (0xFE, 0xFF);
1744 EMIT_TWO_BYTES (0xFF, 0xFE);
1745 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1748 while (charbuf
< charbuf_end
)
1750 ASSURE_DESTINATION (safe_room
);
1752 if (c
> MAX_UNICODE_CHAR
)
1753 c
= coding
->default_char
;
1758 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1760 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1767 c1
= (c
>> 10) + 0xD800;
1768 c2
= (c
& 0x3FF) + 0xDC00;
1770 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1772 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1775 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1776 coding
->produced
= dst
- coding
->destination
;
1777 coding
->produced_char
+= produced_chars
;
1782 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1784 /* Emacs' internal format for representation of multiple character
1785 sets is a kind of multi-byte encoding, i.e. characters are
1786 represented by variable-length sequences of one-byte codes.
1788 ASCII characters and control characters (e.g. `tab', `newline') are
1789 represented by one-byte sequences which are their ASCII codes, in
1790 the range 0x00 through 0x7F.
1792 8-bit characters of the range 0x80..0x9F are represented by
1793 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1796 8-bit characters of the range 0xA0..0xFF are represented by
1797 one-byte sequences which are their 8-bit code.
1799 The other characters are represented by a sequence of `base
1800 leading-code', optional `extended leading-code', and one or two
1801 `position-code's. The length of the sequence is determined by the
1802 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1803 whereas extended leading-code and position-code take the range 0xA0
1804 through 0xFF. See `charset.h' for more details about leading-code
1807 --- CODE RANGE of Emacs' internal format ---
1811 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1812 eight-bit-graphic 0xA0..0xBF
1813 ELSE 0x81..0x9D + [0xA0..0xFF]+
1814 ---------------------------------------------
1816 As this is the internal character representation, the format is
1817 usually not used externally (i.e. in a file or in a data sent to a
1818 process). But, it is possible to have a text externally in this
1819 format (i.e. by encoding by the coding system `emacs-mule').
1821 In that case, a sequence of one-byte codes has a slightly different
1824 At first, all characters in eight-bit-control are represented by
1825 one-byte sequences which are their 8-bit code.
1827 Next, character composition data are represented by the byte
1828 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1830 METHOD is 0xF2 plus one of composition method (enum
1831 composition_method),
1833 BYTES is 0xA0 plus a byte length of this composition data,
1835 CHARS is 0xA0 plus a number of characters composed by this
1838 COMPONENTs are characters of multibyte form or composition
1839 rules encoded by two-byte of ASCII codes.
1841 In addition, for backward compatibility, the following formats are
1842 also recognized as composition data on decoding.
1845 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1848 MSEQ is a multibyte form but in these special format:
1849 ASCII: 0xA0 ASCII_CODE+0x80,
1850 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1851 RULE is a one byte code of the range 0xA0..0xF0 that
1852 represents a composition rule.
1855 char emacs_mule_bytes
[256];
1858 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1859 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1863 detect_coding_emacs_mule (struct coding_system
*coding
,
1864 struct coding_detection_info
*detect_info
)
1866 const unsigned char *src
= coding
->source
, *src_base
;
1867 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1868 int multibytep
= coding
->src_multibyte
;
1869 EMACS_INT consumed_chars
= 0;
1873 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1874 /* A coding system of this category is always ASCII compatible. */
1875 src
+= coding
->head_ascii
;
1885 /* Perhaps the start of composite character. We simply skip
1886 it because analyzing it is too heavy for detecting. But,
1887 at least, we check that the composite character
1888 constitutes of more than 4 bytes. */
1889 const unsigned char *src_start
;
1899 if (src
- src_start
<= 4)
1901 found
= CATEGORY_MASK_EMACS_MULE
;
1909 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1914 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1916 while (more_bytes
> 0)
1921 src
--; /* Unread the last byte. */
1926 if (more_bytes
!= 0)
1928 found
= CATEGORY_MASK_EMACS_MULE
;
1931 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1935 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1937 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1940 detect_info
->found
|= found
;
1945 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
1946 character. If CMP_STATUS indicates that we must expect MSEQ or
1947 RULE described above, decode it and return the negative value of
1948 the decoded character or rule. If an invalid byte is found, return
1949 -1. If SRC is too short, return -2. */
1952 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
1953 int *nbytes
, int *nchars
, int *id
,
1954 struct composition_status
*cmp_status
)
1956 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1957 const unsigned char *src_base
= src
;
1958 int multibytep
= coding
->src_multibyte
;
1962 int consumed_chars
= 0;
1969 charset_ID
= emacs_mule_charset
[0];
1975 if (cmp_status
->state
!= COMPOSING_NO
1976 && cmp_status
->old_form
)
1978 if (cmp_status
->state
== COMPOSING_CHAR
)
1993 *nbytes
= src
- src_base
;
1994 *nchars
= consumed_chars
;
2002 switch (emacs_mule_bytes
[c
])
2005 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2014 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2015 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2018 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2027 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2032 code
= (c
& 0x7F) << 8;
2042 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2047 code
= (c
& 0x7F) << 8;
2056 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2062 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2063 CHARSET_FROM_ID (charset_ID
), code
, c
);
2067 *nbytes
= src
- src_base
;
2068 *nchars
= consumed_chars
;
2071 return (mseq_found
? -c
: c
);
2081 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2083 /* Handle these composition sequence ('|': the end of header elements,
2084 BYTES and CHARS >= 0xA0):
2086 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2087 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2088 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2092 (4) relative composition: 0x80 | MSEQ ... MSEQ
2093 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2095 When the starter 0x80 and the following header elements are found,
2096 this annotation header is produced.
2098 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2100 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2101 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2103 Then, upon reading the following elements, these codes are produced
2104 until the composition end is found:
2107 (2) ALT ... ALT CHAR ... CHAR
2108 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2110 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2112 When the composition end is found, LENGTH and NCHARS in the
2113 annotation header is updated as below:
2115 (1) LENGTH: unchanged, NCHARS: unchanged
2116 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2117 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2118 (4) LENGTH: unchanged, NCHARS: number of CHARs
2119 (5) LENGTH: unchanged, NCHARS: number of CHARs
2121 If an error is found while composing, the annotation header is
2122 changed to the original composition header (plus filler -1s) as
2125 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2126 (5) [ 0x80 0xFF -1 -1- -1 ]
2128 and the sequence [ -2 DECODED-RULE ] is changed to the original
2129 byte sequence as below:
2130 o the original byte sequence is B: [ B -1 ]
2131 o the original byte sequence is B1 B2: [ B1 B2 ]
2133 Most of the routines are implemented by macros because many
2134 variables and labels in the caller decode_coding_emacs_mule must be
2135 accessible, and they are usually called just once (thus doesn't
2136 increase the size of compiled object). */
2138 /* Decode a composition rule represented by C as a component of
2139 composition sequence of Emacs 20 style. Set RULE to the decoded
2142 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2147 if (c < 0 || c >= 81) \
2148 goto invalid_code; \
2149 gref = c / 9, nref = c % 9; \
2150 if (gref == 4) gref = 10; \
2151 if (nref == 4) nref = 10; \
2152 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2156 /* Decode a composition rule represented by C and the following byte
2157 at SRC as a component of composition sequence of Emacs 21 style.
2158 Set RULE to the decoded rule. */
2160 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2165 if (gref < 0 || gref >= 81) \
2166 goto invalid_code; \
2167 ONE_MORE_BYTE (c); \
2169 if (nref < 0 || nref >= 81) \
2170 goto invalid_code; \
2171 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2175 /* Start of Emacs 21 style format. The first three bytes at SRC are
2176 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2177 byte length of this composition information, CHARS is the number of
2178 characters composed by this composition. */
2180 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2182 enum composition_method method = c - 0xF2; \
2183 int nbytes, nchars; \
2185 ONE_MORE_BYTE (c); \
2187 goto invalid_code; \
2188 nbytes = c - 0xA0; \
2189 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2190 goto invalid_code; \
2191 ONE_MORE_BYTE (c); \
2192 nchars = c - 0xA0; \
2193 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2194 goto invalid_code; \
2195 cmp_status->old_form = 0; \
2196 cmp_status->method = method; \
2197 if (method == COMPOSITION_RELATIVE) \
2198 cmp_status->state = COMPOSING_CHAR; \
2200 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2201 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2202 cmp_status->nchars = nchars; \
2203 cmp_status->ncomps = nbytes - 4; \
2204 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2208 /* Start of Emacs 20 style format for relative composition. */
2210 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2212 cmp_status->old_form = 1; \
2213 cmp_status->method = COMPOSITION_RELATIVE; \
2214 cmp_status->state = COMPOSING_CHAR; \
2215 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2216 cmp_status->nchars = cmp_status->ncomps = 0; \
2217 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2221 /* Start of Emacs 20 style format for rule-base composition. */
2223 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2225 cmp_status->old_form = 1; \
2226 cmp_status->method = COMPOSITION_WITH_RULE; \
2227 cmp_status->state = COMPOSING_CHAR; \
2228 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2229 cmp_status->nchars = cmp_status->ncomps = 0; \
2230 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2234 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2236 const unsigned char *current_src = src; \
2238 ONE_MORE_BYTE (c); \
2240 goto invalid_code; \
2241 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2242 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2243 DECODE_EMACS_MULE_21_COMPOSITION (); \
2244 else if (c < 0xA0) \
2245 goto invalid_code; \
2246 else if (c < 0xC0) \
2248 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2249 /* Re-read C as a composition component. */ \
2250 src = current_src; \
2252 else if (c == 0xFF) \
2253 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2255 goto invalid_code; \
2258 #define EMACS_MULE_COMPOSITION_END() \
2260 int idx = - cmp_status->length; \
2262 if (cmp_status->old_form) \
2263 charbuf[idx + 2] = cmp_status->nchars; \
2264 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2265 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2266 cmp_status->state = COMPOSING_NO; \
2271 emacs_mule_finish_composition (int *charbuf
,
2272 struct composition_status
*cmp_status
)
2274 int idx
= - cmp_status
->length
;
2277 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2279 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2281 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2282 && cmp_status
->state
== COMPOSING_CHAR
)
2284 /* The last rule was invalid. */
2285 int rule
= charbuf
[-1] + 0xA0;
2287 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2294 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2296 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2298 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2299 charbuf
[idx
++] = -3;
2305 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2306 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2308 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2309 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2310 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2311 charbuf
[idx
++] = -1;
2315 cmp_status
->state
= COMPOSING_NO
;
2319 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2321 if (cmp_status->state != COMPOSING_NO) \
2322 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2327 decode_coding_emacs_mule (struct coding_system
*coding
)
2329 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2330 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2331 const unsigned char *src_base
;
2332 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2333 /* We may produce two annotations (charset and composition) in one
2334 loop and one more charset annotation at the end. */
2336 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
2337 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
2338 int multibytep
= coding
->src_multibyte
;
2339 EMACS_INT char_offset
= coding
->produced_char
;
2340 EMACS_INT last_offset
= char_offset
;
2341 int last_id
= charset_ascii
;
2343 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2344 int byte_after_cr
= -1;
2345 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2347 if (cmp_status
->state
!= COMPOSING_NO
)
2351 for (i
= 0; i
< cmp_status
->length
; i
++)
2352 *charbuf
++ = cmp_status
->carryover
[i
];
2353 coding
->annotated
= 1;
2358 int c
, id
IF_LINT (= 0);
2361 consumed_chars_base
= consumed_chars
;
2363 if (charbuf
>= charbuf_end
)
2365 if (byte_after_cr
>= 0)
2370 if (byte_after_cr
>= 0)
2371 c
= byte_after_cr
, byte_after_cr
= -1;
2375 if (c
< 0 || c
== 0x80)
2377 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2384 DECODE_EMACS_MULE_COMPOSITION_START ();
2390 if (eol_dos
&& c
== '\r')
2391 ONE_MORE_BYTE (byte_after_cr
);
2393 if (cmp_status
->state
!= COMPOSING_NO
)
2395 if (cmp_status
->old_form
)
2396 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2397 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2398 cmp_status
->ncomps
--;
2403 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2404 /* emacs_mule_char can load a charset map from a file, which
2405 allocates a large structure and might cause buffer text
2406 to be relocated as result. Thus, we need to remember the
2407 original pointer to buffer text, and fix up all related
2408 pointers after the call. */
2409 const unsigned char *orig
= coding
->source
;
2412 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2414 offset
= coding
->source
- orig
;
2428 src
= src_base
+ nbytes
;
2429 consumed_chars
= consumed_chars_base
+ nchars
;
2430 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2431 cmp_status
->ncomps
-= nchars
;
2434 /* Now if C >= 0, we found a normally encoded character, if C <
2435 0, we found an old-style composition component character or
2438 if (cmp_status
->state
== COMPOSING_NO
)
2442 if (last_id
!= charset_ascii
)
2443 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2446 last_offset
= char_offset
;
2451 else if (cmp_status
->state
== COMPOSING_CHAR
)
2453 if (cmp_status
->old_form
)
2457 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2464 cmp_status
->nchars
++;
2465 cmp_status
->length
++;
2466 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2467 EMACS_MULE_COMPOSITION_END ();
2468 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2469 cmp_status
->state
= COMPOSING_RULE
;
2475 cmp_status
->length
++;
2476 cmp_status
->nchars
--;
2477 if (cmp_status
->nchars
== 0)
2478 EMACS_MULE_COMPOSITION_END ();
2481 else if (cmp_status
->state
== COMPOSING_RULE
)
2487 EMACS_MULE_COMPOSITION_END ();
2494 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2499 cmp_status
->length
+= 2;
2500 cmp_status
->state
= COMPOSING_CHAR
;
2503 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2506 cmp_status
->length
++;
2507 if (cmp_status
->ncomps
== 0)
2508 cmp_status
->state
= COMPOSING_CHAR
;
2509 else if (cmp_status
->ncomps
> 0)
2511 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2512 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2515 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2517 else /* COMPOSING_COMPONENT_RULE */
2521 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2526 cmp_status
->length
+= 2;
2527 cmp_status
->ncomps
--;
2528 if (cmp_status
->ncomps
> 0)
2529 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2531 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2536 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2538 consumed_chars
= consumed_chars_base
;
2540 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2546 if (cmp_status
->state
!= COMPOSING_NO
)
2548 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2549 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2554 charbuf
-= cmp_status
->length
;
2555 for (i
= 0; i
< cmp_status
->length
; i
++)
2556 cmp_status
->carryover
[i
] = charbuf
[i
];
2559 if (last_id
!= charset_ascii
)
2560 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2561 coding
->consumed_char
+= consumed_chars_base
;
2562 coding
->consumed
= src_base
- coding
->source
;
2563 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2567 #define EMACS_MULE_LEADING_CODES(id, codes) \
2570 codes[0] = id, codes[1] = 0; \
2571 else if (id < 0xE0) \
2572 codes[0] = 0x9A, codes[1] = id; \
2573 else if (id < 0xF0) \
2574 codes[0] = 0x9B, codes[1] = id; \
2575 else if (id < 0xF5) \
2576 codes[0] = 0x9C, codes[1] = id; \
2578 codes[0] = 0x9D, codes[1] = id; \
2583 encode_coding_emacs_mule (struct coding_system
*coding
)
2585 int multibytep
= coding
->dst_multibyte
;
2586 int *charbuf
= coding
->charbuf
;
2587 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2588 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2589 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2591 EMACS_INT produced_chars
= 0;
2592 Lisp_Object attrs
, charset_list
;
2594 int preferred_charset_id
= -1;
2596 CODING_GET_INFO (coding
, attrs
, charset_list
);
2597 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2599 CODING_ATTR_CHARSET_LIST (attrs
)
2600 = charset_list
= Vemacs_mule_charset_list
;
2603 while (charbuf
< charbuf_end
)
2605 ASSURE_DESTINATION (safe_room
);
2610 /* Handle an annotation. */
2613 case CODING_ANNOTATE_COMPOSITION_MASK
:
2614 /* Not yet implemented. */
2616 case CODING_ANNOTATE_CHARSET_MASK
:
2617 preferred_charset_id
= charbuf
[3];
2618 if (preferred_charset_id
>= 0
2619 && NILP (Fmemq (make_number (preferred_charset_id
),
2621 preferred_charset_id
= -1;
2630 if (ASCII_CHAR_P (c
))
2631 EMIT_ONE_ASCII_BYTE (c
);
2632 else if (CHAR_BYTE8_P (c
))
2634 c
= CHAR_TO_BYTE8 (c
);
2639 struct charset
*charset
;
2643 unsigned char leading_codes
[2];
2645 if (preferred_charset_id
>= 0)
2647 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2648 if (CHAR_CHARSET_P (c
, charset
))
2649 code
= ENCODE_CHAR (charset
, c
);
2651 charset
= char_charset (c
, charset_list
, &code
);
2654 charset
= char_charset (c
, charset_list
, &code
);
2657 c
= coding
->default_char
;
2658 if (ASCII_CHAR_P (c
))
2660 EMIT_ONE_ASCII_BYTE (c
);
2663 charset
= char_charset (c
, charset_list
, &code
);
2665 dimension
= CHARSET_DIMENSION (charset
);
2666 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2667 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2668 EMIT_ONE_BYTE (leading_codes
[0]);
2669 if (leading_codes
[1])
2670 EMIT_ONE_BYTE (leading_codes
[1]);
2672 EMIT_ONE_BYTE (code
| 0x80);
2676 EMIT_ONE_BYTE (code
>> 8);
2677 EMIT_ONE_BYTE (code
& 0xFF);
2681 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2682 coding
->produced_char
+= produced_chars
;
2683 coding
->produced
= dst
- coding
->destination
;
2688 /*** 7. ISO2022 handlers ***/
2690 /* The following note describes the coding system ISO2022 briefly.
2691 Since the intention of this note is to help understand the
2692 functions in this file, some parts are NOT ACCURATE or are OVERLY
2693 SIMPLIFIED. For thorough understanding, please refer to the
2694 original document of ISO2022. This is equivalent to the standard
2695 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2697 ISO2022 provides many mechanisms to encode several character sets
2698 in 7-bit and 8-bit environments. For 7-bit environments, all text
2699 is encoded using bytes less than 128. This may make the encoded
2700 text a little bit longer, but the text passes more easily through
2701 several types of gateway, some of which strip off the MSB (Most
2704 There are two kinds of character sets: control character sets and
2705 graphic character sets. The former contain control characters such
2706 as `newline' and `escape' to provide control functions (control
2707 functions are also provided by escape sequences). The latter
2708 contain graphic characters such as 'A' and '-'. Emacs recognizes
2709 two control character sets and many graphic character sets.
2711 Graphic character sets are classified into one of the following
2712 four classes, according to the number of bytes (DIMENSION) and
2713 number of characters in one dimension (CHARS) of the set:
2714 - DIMENSION1_CHARS94
2715 - DIMENSION1_CHARS96
2716 - DIMENSION2_CHARS94
2717 - DIMENSION2_CHARS96
2719 In addition, each character set is assigned an identification tag,
2720 unique for each set, called the "final character" (denoted as <F>
2721 hereafter). The <F> of each character set is decided by ECMA(*)
2722 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2723 (0x30..0x3F are for private use only).
2725 Note (*): ECMA = European Computer Manufacturers Association
2727 Here are examples of graphic character sets [NAME(<F>)]:
2728 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2729 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2730 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2731 o DIMENSION2_CHARS96 -- none for the moment
2733 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2734 C0 [0x00..0x1F] -- control character plane 0
2735 GL [0x20..0x7F] -- graphic character plane 0
2736 C1 [0x80..0x9F] -- control character plane 1
2737 GR [0xA0..0xFF] -- graphic character plane 1
2739 A control character set is directly designated and invoked to C0 or
2740 C1 by an escape sequence. The most common case is that:
2741 - ISO646's control character set is designated/invoked to C0, and
2742 - ISO6429's control character set is designated/invoked to C1,
2743 and usually these designations/invocations are omitted in encoded
2744 text. In a 7-bit environment, only C0 can be used, and a control
2745 character for C1 is encoded by an appropriate escape sequence to
2746 fit into the environment. All control characters for C1 are
2747 defined to have corresponding escape sequences.
2749 A graphic character set is at first designated to one of four
2750 graphic registers (G0 through G3), then these graphic registers are
2751 invoked to GL or GR. These designations and invocations can be
2752 done independently. The most common case is that G0 is invoked to
2753 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2754 these invocations and designations are omitted in encoded text.
2755 In a 7-bit environment, only GL can be used.
2757 When a graphic character set of CHARS94 is invoked to GL, codes
2758 0x20 and 0x7F of the GL area work as control characters SPACE and
2759 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2762 There are two ways of invocation: locking-shift and single-shift.
2763 With locking-shift, the invocation lasts until the next different
2764 invocation, whereas with single-shift, the invocation affects the
2765 following character only and doesn't affect the locking-shift
2766 state. Invocations are done by the following control characters or
2769 ----------------------------------------------------------------------
2770 abbrev function cntrl escape seq description
2771 ----------------------------------------------------------------------
2772 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2773 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2774 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2775 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2776 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2777 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2778 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2779 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2780 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2781 ----------------------------------------------------------------------
2782 (*) These are not used by any known coding system.
2784 Control characters for these functions are defined by macros
2785 ISO_CODE_XXX in `coding.h'.
2787 Designations are done by the following escape sequences:
2788 ----------------------------------------------------------------------
2789 escape sequence description
2790 ----------------------------------------------------------------------
2791 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2792 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2793 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2794 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2795 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2796 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2797 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2798 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2799 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2800 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2801 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2802 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2803 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2804 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2805 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2806 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2807 ----------------------------------------------------------------------
2809 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2810 of dimension 1, chars 94, and final character <F>, etc...
2812 Note (*): Although these designations are not allowed in ISO2022,
2813 Emacs accepts them on decoding, and produces them on encoding
2814 CHARS96 character sets in a coding system which is characterized as
2815 7-bit environment, non-locking-shift, and non-single-shift.
2817 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2818 '(' must be omitted. We refer to this as "short-form" hereafter.
2820 Now you may notice that there are a lot of ways of encoding the
2821 same multilingual text in ISO2022. Actually, there exist many
2822 coding systems such as Compound Text (used in X11's inter client
2823 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2824 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2825 localized platforms), and all of these are variants of ISO2022.
2827 In addition to the above, Emacs handles two more kinds of escape
2828 sequences: ISO6429's direction specification and Emacs' private
2829 sequence for specifying character composition.
2831 ISO6429's direction specification takes the following form:
2832 o CSI ']' -- end of the current direction
2833 o CSI '0' ']' -- end of the current direction
2834 o CSI '1' ']' -- start of left-to-right text
2835 o CSI '2' ']' -- start of right-to-left text
2836 The control character CSI (0x9B: control sequence introducer) is
2837 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2839 Character composition specification takes the following form:
2840 o ESC '0' -- start relative composition
2841 o ESC '1' -- end composition
2842 o ESC '2' -- start rule-base composition (*)
2843 o ESC '3' -- start relative composition with alternate chars (**)
2844 o ESC '4' -- start rule-base composition with alternate chars (**)
2845 Since these are not standard escape sequences of any ISO standard,
2846 the use of them with these meanings is restricted to Emacs only.
2848 (*) This form is used only in Emacs 20.7 and older versions,
2849 but newer versions can safely decode it.
2850 (**) This form is used only in Emacs 21.1 and newer versions,
2851 and older versions can't decode it.
2853 Here's a list of example usages of these composition escape
2854 sequences (categorized by `enum composition_method').
2856 COMPOSITION_RELATIVE:
2857 ESC 0 CHAR [ CHAR ] ESC 1
2858 COMPOSITION_WITH_RULE:
2859 ESC 2 CHAR [ RULE CHAR ] ESC 1
2860 COMPOSITION_WITH_ALTCHARS:
2861 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2862 COMPOSITION_WITH_RULE_ALTCHARS:
2863 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2865 static enum iso_code_class_type iso_code_class
[256];
2867 #define SAFE_CHARSET_P(coding, id) \
2868 ((id) <= (coding)->max_charset_id \
2869 && (coding)->safe_charsets[id] != 255)
2872 setup_iso_safe_charsets (Lisp_Object attrs
)
2874 Lisp_Object charset_list
, safe_charsets
;
2875 Lisp_Object request
;
2876 Lisp_Object reg_usage
;
2879 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2882 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2883 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2884 && ! EQ (charset_list
, Viso_2022_charset_list
))
2886 CODING_ATTR_CHARSET_LIST (attrs
)
2887 = charset_list
= Viso_2022_charset_list
;
2888 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2891 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2895 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2897 int id
= XINT (XCAR (tail
));
2898 if (max_charset_id
< id
)
2899 max_charset_id
= id
;
2902 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2903 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2904 request
= AREF (attrs
, coding_attr_iso_request
);
2905 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2906 reg94
= XINT (XCAR (reg_usage
));
2907 reg96
= XINT (XCDR (reg_usage
));
2909 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2913 struct charset
*charset
;
2916 charset
= CHARSET_FROM_ID (XINT (id
));
2917 reg
= Fcdr (Fassq (id
, request
));
2919 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2920 else if (charset
->iso_chars_96
)
2923 SSET (safe_charsets
, XINT (id
), reg96
);
2928 SSET (safe_charsets
, XINT (id
), reg94
);
2931 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2935 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2936 Check if a text is encoded in one of ISO-2022 based coding systems.
2937 If it is, return 1, else return 0. */
2940 detect_coding_iso_2022 (struct coding_system
*coding
,
2941 struct coding_detection_info
*detect_info
)
2943 const unsigned char *src
= coding
->source
, *src_base
= src
;
2944 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2945 int multibytep
= coding
->src_multibyte
;
2946 int single_shifting
= 0;
2949 EMACS_INT consumed_chars
= 0;
2953 int composition_count
= -1;
2955 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2957 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2959 struct coding_system
*this = &(coding_categories
[i
]);
2960 Lisp_Object attrs
, val
;
2964 attrs
= CODING_ID_ATTRS (this->id
);
2965 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2966 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
2967 setup_iso_safe_charsets (attrs
);
2968 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2969 this->max_charset_id
= SCHARS (val
) - 1;
2970 this->safe_charsets
= SDATA (val
);
2973 /* A coding system of this category is always ASCII compatible. */
2974 src
+= coding
->head_ascii
;
2976 while (rejected
!= CATEGORY_MASK_ISO
)
2983 if (inhibit_iso_escape_detection
)
2985 single_shifting
= 0;
2987 if (c
== 'N' || c
== 'O')
2989 /* ESC <Fe> for SS2 or SS3. */
2990 single_shifting
= 1;
2991 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2995 /* End of composition. */
2996 if (composition_count
< 0
2997 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3000 composition_count
= -1;
3001 found
|= CATEGORY_MASK_ISO
;
3003 else if (c
>= '0' && c
<= '4')
3005 /* ESC <Fp> for start/end composition. */
3006 composition_count
= 0;
3010 if (c
>= '(' && c
<= '/')
3012 /* Designation sequence for a charset of dimension 1. */
3014 if (c1
< ' ' || c1
>= 0x80
3015 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3016 /* Invalid designation sequence. Just ignore. */
3021 /* Designation sequence for a charset of dimension 2. */
3023 if (c
>= '@' && c
<= 'B')
3024 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3025 id
= iso_charset_table
[1][0][c
];
3026 else if (c
>= '(' && c
<= '/')
3029 if (c1
< ' ' || c1
>= 0x80
3030 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3031 /* Invalid designation sequence. Just ignore. */
3035 /* Invalid designation sequence. Just ignore it. */
3040 /* Invalid escape sequence. Just ignore it. */
3044 /* We found a valid designation sequence for CHARSET. */
3045 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3046 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3048 found
|= CATEGORY_MASK_ISO_7
;
3050 rejected
|= CATEGORY_MASK_ISO_7
;
3051 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3053 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3055 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3056 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3058 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3060 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3061 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3063 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3065 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3071 /* Locking shift out/in. */
3072 if (inhibit_iso_escape_detection
)
3074 single_shifting
= 0;
3075 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3079 /* Control sequence introducer. */
3080 single_shifting
= 0;
3081 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3082 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3083 goto check_extra_latin
;
3088 if (inhibit_iso_escape_detection
)
3090 single_shifting
= 0;
3091 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3092 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3093 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3095 found
|= CATEGORY_MASK_ISO_8_1
;
3096 single_shifting
= 1;
3098 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3099 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3101 found
|= CATEGORY_MASK_ISO_8_2
;
3102 single_shifting
= 1;
3104 if (single_shifting
)
3107 if (! VECTORP (Vlatin_extra_code_table
)
3108 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3110 rejected
= CATEGORY_MASK_ISO
;
3113 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3114 & CODING_ISO_FLAG_LATIN_EXTRA
)
3115 found
|= CATEGORY_MASK_ISO_8_1
;
3117 rejected
|= CATEGORY_MASK_ISO_8_1
;
3118 rejected
|= CATEGORY_MASK_ISO_8_2
;
3126 if (composition_count
>= 0)
3127 composition_count
++;
3128 single_shifting
= 0;
3133 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3134 found
|= CATEGORY_MASK_ISO_8_1
;
3135 /* Check the length of succeeding codes of the range
3136 0xA0..0FF. If the byte length is even, we include
3137 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3138 only when we are not single shifting. */
3139 if (! single_shifting
3140 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3143 while (src
< src_end
)
3155 if (len
& 1 && src
< src_end
)
3157 rejected
|= CATEGORY_MASK_ISO_8_2
;
3158 if (composition_count
>= 0)
3159 composition_count
+= len
;
3163 found
|= CATEGORY_MASK_ISO_8_2
;
3164 if (composition_count
>= 0)
3165 composition_count
+= len
/ 2;
3172 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3176 detect_info
->rejected
|= rejected
;
3177 detect_info
->found
|= (found
& ~rejected
);
3182 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3183 escape sequence should be kept. */
3184 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3188 if (final < '0' || final >= 128 \
3189 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3190 || !SAFE_CHARSET_P (coding, id)) \
3192 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3196 prev = CODING_ISO_DESIGNATION (coding, reg); \
3197 if (id == charset_jisx0201_roman) \
3199 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3200 id = charset_ascii; \
3202 else if (id == charset_jisx0208_1978) \
3204 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3205 id = charset_jisx0208; \
3207 CODING_ISO_DESIGNATION (coding, reg) = id; \
3208 /* If there was an invalid designation to REG previously, and this \
3209 designation is ASCII to REG, we should keep this designation \
3211 if (prev == -2 && id == charset_ascii) \
3216 /* Handle these composition sequence (ALT: alternate char):
3218 (1) relative composition: ESC 0 CHAR ... ESC 1
3219 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3220 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3221 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3223 When the start sequence (ESC 0/2/3/4) is found, this annotation
3226 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3228 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3229 produced until the end sequence (ESC 1) is found:
3232 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3233 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3234 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3236 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3237 annotation header is updated as below:
3239 (1) LENGTH: unchanged, NCHARS: number of CHARs
3240 (2) LENGTH: unchanged, NCHARS: number of CHARs
3241 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3242 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3244 If an error is found while composing, the annotation header is
3247 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3249 and the sequence [ -2 DECODED-RULE ] is changed to the original
3250 byte sequence as below:
3251 o the original byte sequence is B: [ B -1 ]
3252 o the original byte sequence is B1 B2: [ B1 B2 ]
3253 and the sequence [ -1 -1 ] is changed to the original byte
3258 /* Decode a composition rule C1 and maybe one more byte from the
3259 source, and set RULE to the encoded composition rule. If the rule
3260 is invalid, goto invalid_code. */
3262 #define DECODE_COMPOSITION_RULE(rule) \
3266 goto invalid_code; \
3267 if (rule < 81) /* old format (before ver.21) */ \
3269 int gref = (rule) / 9; \
3270 int nref = (rule) % 9; \
3271 if (gref == 4) gref = 10; \
3272 if (nref == 4) nref = 10; \
3273 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3275 else /* new format (after ver.21) */ \
3279 ONE_MORE_BYTE (b); \
3280 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3281 goto invalid_code; \
3282 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3283 rule += 0x100; /* Distinguish it from the old format. */ \
3287 #define ENCODE_COMPOSITION_RULE(rule) \
3289 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3291 if (rule < 0x100) /* old format */ \
3293 if (gref == 10) gref = 4; \
3294 if (nref == 10) nref = 4; \
3295 charbuf[idx] = 32 + gref * 9 + nref; \
3296 charbuf[idx + 1] = -1; \
3299 else /* new format */ \
3301 charbuf[idx] = 32 + 81 + gref; \
3302 charbuf[idx + 1] = 32 + nref; \
3307 /* Finish the current composition as invalid. */
3309 static int finish_composition (int *, struct composition_status
*);
3312 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3314 int idx
= - cmp_status
->length
;
3317 /* Recover the original ESC sequence */
3318 charbuf
[idx
++] = ISO_CODE_ESC
;
3319 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3320 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3321 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3322 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3324 charbuf
[idx
++] = -2;
3326 charbuf
[idx
++] = -1;
3327 new_chars
= cmp_status
->nchars
;
3328 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3329 for (; idx
< 0; idx
++)
3331 int elt
= charbuf
[idx
];
3335 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3340 charbuf
[idx
++] = ISO_CODE_ESC
;
3345 cmp_status
->state
= COMPOSING_NO
;
3349 /* If characters are under composition, finish the composition. */
3350 #define MAYBE_FINISH_COMPOSITION() \
3352 if (cmp_status->state != COMPOSING_NO) \
3353 char_offset += finish_composition (charbuf, cmp_status); \
3356 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3358 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3359 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3360 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3361 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3363 Produce this annotation sequence now:
3365 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3368 #define DECODE_COMPOSITION_START(c1) \
3371 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3372 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3373 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3374 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3378 cmp_status->state = COMPOSING_CHAR; \
3379 cmp_status->length += 2; \
3383 MAYBE_FINISH_COMPOSITION (); \
3384 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3385 : c1 == '2' ? COMPOSITION_WITH_RULE \
3386 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3387 : COMPOSITION_WITH_RULE_ALTCHARS); \
3389 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3390 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3391 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3392 cmp_status->nchars = cmp_status->ncomps = 0; \
3393 coding->annotated = 1; \
3398 /* Handle composition end sequence ESC 1. */
3400 #define DECODE_COMPOSITION_END() \
3402 if (cmp_status->nchars == 0 \
3403 || ((cmp_status->state == COMPOSING_CHAR) \
3404 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3406 MAYBE_FINISH_COMPOSITION (); \
3407 goto invalid_code; \
3409 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3410 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3411 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3412 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3413 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3414 char_offset += cmp_status->nchars; \
3415 cmp_status->state = COMPOSING_NO; \
3418 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3420 #define STORE_COMPOSITION_RULE(rule) \
3423 *charbuf++ = rule; \
3424 cmp_status->length += 2; \
3425 cmp_status->state--; \
3428 /* Store a composed char or a component char C in charbuf, and update
3431 #define STORE_COMPOSITION_CHAR(c) \
3434 cmp_status->length++; \
3435 if (cmp_status->state == COMPOSING_CHAR) \
3436 cmp_status->nchars++; \
3438 cmp_status->ncomps++; \
3439 if (cmp_status->method == COMPOSITION_WITH_RULE \
3440 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3441 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3442 cmp_status->state++; \
3446 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3449 decode_coding_iso_2022 (struct coding_system
*coding
)
3451 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3452 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3453 const unsigned char *src_base
;
3454 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3455 /* We may produce two annotations (charset and composition) in one
3456 loop and one more charset annotation at the end. */
3458 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3459 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
3460 int multibytep
= coding
->src_multibyte
;
3461 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3462 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3463 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3464 int charset_id_2
, charset_id_3
;
3465 struct charset
*charset
;
3467 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3468 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3469 EMACS_INT char_offset
= coding
->produced_char
;
3470 EMACS_INT last_offset
= char_offset
;
3471 int last_id
= charset_ascii
;
3473 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3474 int byte_after_cr
= -1;
3477 setup_iso_safe_charsets (attrs
);
3478 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3480 if (cmp_status
->state
!= COMPOSING_NO
)
3482 for (i
= 0; i
< cmp_status
->length
; i
++)
3483 *charbuf
++ = cmp_status
->carryover
[i
];
3484 coding
->annotated
= 1;
3492 consumed_chars_base
= consumed_chars
;
3494 if (charbuf
>= charbuf_end
)
3496 if (byte_after_cr
>= 0)
3501 if (byte_after_cr
>= 0)
3502 c1
= byte_after_cr
, byte_after_cr
= -1;
3508 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3510 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3512 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3516 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3518 if (c1
== ISO_CODE_ESC
)
3520 if (src
+ 1 >= src_end
)
3521 goto no_more_source
;
3522 *charbuf
++ = ISO_CODE_ESC
;
3524 if (src
[0] == '%' && src
[1] == '@')
3527 consumed_chars
+= 2;
3529 /* We are sure charbuf can contain two more chars. */
3532 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3537 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3543 if ((cmp_status
->state
== COMPOSING_RULE
3544 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3545 && c1
!= ISO_CODE_ESC
)
3549 DECODE_COMPOSITION_RULE (rule
);
3550 STORE_COMPOSITION_RULE (rule
);
3554 /* We produce at most one character. */
3555 switch (iso_code_class
[c1
])
3557 case ISO_0x20_or_0x7F
:
3558 if (charset_id_0
< 0
3559 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3560 /* This is SPACE or DEL. */
3561 charset
= CHARSET_FROM_ID (charset_ascii
);
3563 charset
= CHARSET_FROM_ID (charset_id_0
);
3566 case ISO_graphic_plane_0
:
3567 if (charset_id_0
< 0)
3568 charset
= CHARSET_FROM_ID (charset_ascii
);
3570 charset
= CHARSET_FROM_ID (charset_id_0
);
3573 case ISO_0xA0_or_0xFF
:
3574 if (charset_id_1
< 0
3575 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3576 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3578 /* This is a graphic character, we fall down ... */
3580 case ISO_graphic_plane_1
:
3581 if (charset_id_1
< 0)
3583 charset
= CHARSET_FROM_ID (charset_id_1
);
3587 if (eol_dos
&& c1
== '\r')
3588 ONE_MORE_BYTE (byte_after_cr
);
3589 MAYBE_FINISH_COMPOSITION ();
3590 charset
= CHARSET_FROM_ID (charset_ascii
);
3597 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3598 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3600 CODING_ISO_INVOCATION (coding
, 0) = 1;
3601 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3605 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3607 CODING_ISO_INVOCATION (coding
, 0) = 0;
3608 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3611 case ISO_single_shift_2_7
:
3612 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3614 case ISO_single_shift_2
:
3615 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3617 /* SS2 is handled as an escape sequence of ESC 'N' */
3619 goto label_escape_sequence
;
3621 case ISO_single_shift_3
:
3622 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3624 /* SS2 is handled as an escape sequence of ESC 'O' */
3626 goto label_escape_sequence
;
3628 case ISO_control_sequence_introducer
:
3629 /* CSI is handled as an escape sequence of ESC '[' ... */
3631 goto label_escape_sequence
;
3635 label_escape_sequence
:
3636 /* Escape sequences handled here are invocation,
3637 designation, direction specification, and character
3638 composition specification. */
3641 case '&': /* revision of following character set */
3643 if (!(c1
>= '@' && c1
<= '~'))
3646 if (c1
!= ISO_CODE_ESC
)
3649 goto label_escape_sequence
;
3651 case '$': /* designation of 2-byte character set */
3652 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3658 if (c1
>= '@' && c1
<= 'B')
3659 { /* designation of JISX0208.1978, GB2312.1980,
3661 reg
= 0, chars96
= 0;
3663 else if (c1
>= 0x28 && c1
<= 0x2B)
3664 { /* designation of DIMENSION2_CHARS94 character set */
3665 reg
= c1
- 0x28, chars96
= 0;
3668 else if (c1
>= 0x2C && c1
<= 0x2F)
3669 { /* designation of DIMENSION2_CHARS96 character set */
3670 reg
= c1
- 0x2C, chars96
= 1;
3675 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3676 /* We must update these variables now. */
3678 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3680 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3686 case 'n': /* invocation of locking-shift-2 */
3687 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3688 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3690 CODING_ISO_INVOCATION (coding
, 0) = 2;
3691 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3694 case 'o': /* invocation of locking-shift-3 */
3695 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3696 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3698 CODING_ISO_INVOCATION (coding
, 0) = 3;
3699 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3702 case 'N': /* invocation of single-shift-2 */
3703 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3704 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3706 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3707 if (charset_id_2
< 0)
3708 charset
= CHARSET_FROM_ID (charset_ascii
);
3710 charset
= CHARSET_FROM_ID (charset_id_2
);
3712 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3716 case 'O': /* invocation of single-shift-3 */
3717 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3718 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3720 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3721 if (charset_id_3
< 0)
3722 charset
= CHARSET_FROM_ID (charset_ascii
);
3724 charset
= CHARSET_FROM_ID (charset_id_3
);
3726 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3730 case '0': case '2': case '3': case '4': /* start composition */
3731 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3733 if (last_id
!= charset_ascii
)
3735 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3736 last_id
= charset_ascii
;
3737 last_offset
= char_offset
;
3739 DECODE_COMPOSITION_START (c1
);
3742 case '1': /* end composition */
3743 if (cmp_status
->state
== COMPOSING_NO
)
3745 DECODE_COMPOSITION_END ();
3748 case '[': /* specification of direction */
3749 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3751 /* For the moment, nested direction is not supported.
3752 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3753 left-to-right, and nonzero means right-to-left. */
3757 case ']': /* end of the current direction */
3758 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3760 case '0': /* end of the current direction */
3761 case '1': /* start of left-to-right direction */
3764 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3769 case '2': /* start of right-to-left direction */
3772 coding
->mode
|= CODING_MODE_DIRECTION
;
3786 /* CTEXT extended segment:
3787 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3788 We keep these bytes as is for the moment.
3789 They may be decoded by post-read-conversion. */
3793 ONE_MORE_BYTE (dim
);
3794 if (dim
< '0' || dim
> '4')
3802 size
= ((M
- 128) * 128) + (L
- 128);
3803 if (charbuf
+ 6 > charbuf_end
)
3805 *charbuf
++ = ISO_CODE_ESC
;
3809 *charbuf
++ = BYTE8_TO_CHAR (M
);
3810 *charbuf
++ = BYTE8_TO_CHAR (L
);
3811 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3815 /* XFree86 extension for embedding UTF-8 in CTEXT:
3816 ESC % G --UTF-8-BYTES-- ESC % @
3817 We keep these bytes as is for the moment.
3818 They may be decoded by post-read-conversion. */
3819 if (charbuf
+ 3 > charbuf_end
)
3821 *charbuf
++ = ISO_CODE_ESC
;
3824 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3832 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3837 if (c1
>= 0x28 && c1
<= 0x2B)
3838 { /* designation of DIMENSION1_CHARS94 character set */
3839 reg
= c1
- 0x28, chars96
= 0;
3842 else if (c1
>= 0x2C && c1
<= 0x2F)
3843 { /* designation of DIMENSION1_CHARS96 character set */
3844 reg
= c1
- 0x2C, chars96
= 1;
3849 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3850 /* We must update these variables now. */
3852 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3854 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3866 if (cmp_status
->state
== COMPOSING_NO
3867 && charset
->id
!= charset_ascii
3868 && last_id
!= charset
->id
)
3870 if (last_id
!= charset_ascii
)
3871 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3872 last_id
= charset
->id
;
3873 last_offset
= char_offset
;
3876 /* Now we know CHARSET and 1st position code C1 of a character.
3877 Produce a decoded character while getting 2nd and 3rd
3878 position codes C2, C3 if necessary. */
3879 if (CHARSET_DIMENSION (charset
) > 1)
3882 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3883 || ((c1
& 0x80) != (c2
& 0x80)))
3884 /* C2 is not in a valid range. */
3886 if (CHARSET_DIMENSION (charset
) == 2)
3887 c1
= (c1
<< 8) | c2
;
3891 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3892 || ((c1
& 0x80) != (c3
& 0x80)))
3893 /* C3 is not in a valid range. */
3895 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3899 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3902 MAYBE_FINISH_COMPOSITION ();
3903 for (; src_base
< src
; src_base
++, char_offset
++)
3905 if (ASCII_BYTE_P (*src_base
))
3906 *charbuf
++ = *src_base
;
3908 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3911 else if (cmp_status
->state
== COMPOSING_NO
)
3916 else if ((cmp_status
->state
== COMPOSING_CHAR
3917 ? cmp_status
->nchars
3918 : cmp_status
->ncomps
)
3919 >= MAX_COMPOSITION_COMPONENTS
)
3921 /* Too long composition. */
3922 MAYBE_FINISH_COMPOSITION ();
3927 STORE_COMPOSITION_CHAR (c
);
3931 MAYBE_FINISH_COMPOSITION ();
3933 consumed_chars
= consumed_chars_base
;
3935 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3945 if (cmp_status
->state
!= COMPOSING_NO
)
3947 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
3948 MAYBE_FINISH_COMPOSITION ();
3951 charbuf
-= cmp_status
->length
;
3952 for (i
= 0; i
< cmp_status
->length
; i
++)
3953 cmp_status
->carryover
[i
] = charbuf
[i
];
3956 else if (last_id
!= charset_ascii
)
3957 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3958 coding
->consumed_char
+= consumed_chars_base
;
3959 coding
->consumed
= src_base
- coding
->source
;
3960 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3964 /* ISO2022 encoding stuff. */
3967 It is not enough to say just "ISO2022" on encoding, we have to
3968 specify more details. In Emacs, each coding system of ISO2022
3969 variant has the following specifications:
3970 1. Initial designation to G0 thru G3.
3971 2. Allows short-form designation?
3972 3. ASCII should be designated to G0 before control characters?
3973 4. ASCII should be designated to G0 at end of line?
3974 5. 7-bit environment or 8-bit environment?
3975 6. Use locking-shift?
3976 7. Use Single-shift?
3977 And the following two are only for Japanese:
3978 8. Use ASCII in place of JIS0201-1976-Roman?
3979 9. Use JISX0208-1983 in place of JISX0208-1978?
3980 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3981 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3985 /* Produce codes (escape sequence) for designating CHARSET to graphic
3986 register REG at DST, and increment DST. If <final-char> of CHARSET is
3987 '@', 'A', or 'B' and the coding system CODING allows, produce
3988 designation sequence of short-form. */
3990 #define ENCODE_DESIGNATION(charset, reg, coding) \
3992 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3993 const char *intermediate_char_94 = "()*+"; \
3994 const char *intermediate_char_96 = ",-./"; \
3995 int revision = -1; \
3997 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3998 revision = CHARSET_ISO_REVISION (charset); \
4000 if (revision >= 0) \
4002 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4003 EMIT_ONE_BYTE ('@' + revision); \
4005 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4006 if (CHARSET_DIMENSION (charset) == 1) \
4009 if (! CHARSET_ISO_CHARS_96 (charset)) \
4010 b = intermediate_char_94[reg]; \
4012 b = intermediate_char_96[reg]; \
4013 EMIT_ONE_ASCII_BYTE (b); \
4017 EMIT_ONE_ASCII_BYTE ('$'); \
4018 if (! CHARSET_ISO_CHARS_96 (charset)) \
4020 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4022 || final_char < '@' || final_char > 'B') \
4023 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4026 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4028 EMIT_ONE_ASCII_BYTE (final_char); \
4030 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4034 /* The following two macros produce codes (control character or escape
4035 sequence) for ISO2022 single-shift functions (single-shift-2 and
4038 #define ENCODE_SINGLE_SHIFT_2 \
4040 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4041 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4043 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4044 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4048 #define ENCODE_SINGLE_SHIFT_3 \
4050 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4051 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4053 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4054 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4058 /* The following four macros produce codes (control character or
4059 escape sequence) for ISO2022 locking-shift functions (shift-in,
4060 shift-out, locking-shift-2, and locking-shift-3). */
4062 #define ENCODE_SHIFT_IN \
4064 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4065 CODING_ISO_INVOCATION (coding, 0) = 0; \
4069 #define ENCODE_SHIFT_OUT \
4071 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4072 CODING_ISO_INVOCATION (coding, 0) = 1; \
4076 #define ENCODE_LOCKING_SHIFT_2 \
4078 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4079 CODING_ISO_INVOCATION (coding, 0) = 2; \
4083 #define ENCODE_LOCKING_SHIFT_3 \
4085 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4086 CODING_ISO_INVOCATION (coding, 0) = 3; \
4090 /* Produce codes for a DIMENSION1 character whose character set is
4091 CHARSET and whose position-code is C1. Designation and invocation
4092 sequences are also produced in advance if necessary. */
4094 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4096 int id = CHARSET_ID (charset); \
4098 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4099 && id == charset_ascii) \
4101 id = charset_jisx0201_roman; \
4102 charset = CHARSET_FROM_ID (id); \
4105 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4107 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4108 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4110 EMIT_ONE_BYTE (c1 | 0x80); \
4111 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4114 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4116 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4119 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4121 EMIT_ONE_BYTE (c1 | 0x80); \
4125 /* Since CHARSET is not yet invoked to any graphic planes, we \
4126 must invoke it, or, at first, designate it to some graphic \
4127 register. Then repeat the loop to actually produce the \
4129 dst = encode_invocation_designation (charset, coding, dst, \
4134 /* Produce codes for a DIMENSION2 character whose character set is
4135 CHARSET and whose position-codes are C1 and C2. Designation and
4136 invocation codes are also produced in advance if necessary. */
4138 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4140 int id = CHARSET_ID (charset); \
4142 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4143 && id == charset_jisx0208) \
4145 id = charset_jisx0208_1978; \
4146 charset = CHARSET_FROM_ID (id); \
4149 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4151 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4152 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4154 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4155 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4158 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4160 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4163 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4165 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4169 /* Since CHARSET is not yet invoked to any graphic planes, we \
4170 must invoke it, or, at first, designate it to some graphic \
4171 register. Then repeat the loop to actually produce the \
4173 dst = encode_invocation_designation (charset, coding, dst, \
4178 #define ENCODE_ISO_CHARACTER(charset, c) \
4180 int code = ENCODE_CHAR ((charset), (c)); \
4182 if (CHARSET_DIMENSION (charset) == 1) \
4183 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4185 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4189 /* Produce designation and invocation codes at a place pointed by DST
4190 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4193 static unsigned char *
4194 encode_invocation_designation (struct charset
*charset
,
4195 struct coding_system
*coding
,
4196 unsigned char *dst
, EMACS_INT
*p_nchars
)
4198 int multibytep
= coding
->dst_multibyte
;
4199 EMACS_INT produced_chars
= *p_nchars
;
4200 int reg
; /* graphic register number */
4201 int id
= CHARSET_ID (charset
);
4203 /* At first, check designations. */
4204 for (reg
= 0; reg
< 4; reg
++)
4205 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4210 /* CHARSET is not yet designated to any graphic registers. */
4211 /* At first check the requested designation. */
4212 reg
= CODING_ISO_REQUEST (coding
, id
);
4214 /* Since CHARSET requests no special designation, designate it
4215 to graphic register 0. */
4218 ENCODE_DESIGNATION (charset
, reg
, coding
);
4221 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4222 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4224 /* Since the graphic register REG is not invoked to any graphic
4225 planes, invoke it to graphic plane 0. */
4228 case 0: /* graphic register 0 */
4232 case 1: /* graphic register 1 */
4236 case 2: /* graphic register 2 */
4237 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4238 ENCODE_SINGLE_SHIFT_2
;
4240 ENCODE_LOCKING_SHIFT_2
;
4243 case 3: /* graphic register 3 */
4244 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4245 ENCODE_SINGLE_SHIFT_3
;
4247 ENCODE_LOCKING_SHIFT_3
;
4252 *p_nchars
= produced_chars
;
4257 /* Produce codes for designation and invocation to reset the graphic
4258 planes and registers to initial state. */
4259 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4262 struct charset *charset; \
4264 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4266 for (reg = 0; reg < 4; reg++) \
4267 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4268 && (CODING_ISO_DESIGNATION (coding, reg) \
4269 != CODING_ISO_INITIAL (coding, reg))) \
4271 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4272 ENCODE_DESIGNATION (charset, reg, coding); \
4277 /* Produce designation sequences of charsets in the line started from
4278 SRC to a place pointed by DST, and return updated DST.
4280 If the current block ends before any end-of-line, we may fail to
4281 find all the necessary designations. */
4283 static unsigned char *
4284 encode_designation_at_bol (struct coding_system
*coding
, int *charbuf
,
4287 struct charset
*charset
;
4288 /* Table of charsets to be designated to each graphic register. */
4290 int c
, found
= 0, reg
;
4291 EMACS_INT produced_chars
= 0;
4292 int multibytep
= coding
->dst_multibyte
;
4294 Lisp_Object charset_list
;
4296 attrs
= CODING_ID_ATTRS (coding
->id
);
4297 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4298 if (EQ (charset_list
, Qiso_2022
))
4299 charset_list
= Viso_2022_charset_list
;
4301 for (reg
= 0; reg
< 4; reg
++)
4311 charset
= char_charset (c
, charset_list
, NULL
);
4312 id
= CHARSET_ID (charset
);
4313 reg
= CODING_ISO_REQUEST (coding
, id
);
4314 if (reg
>= 0 && r
[reg
] < 0)
4323 for (reg
= 0; reg
< 4; reg
++)
4325 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4326 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4332 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4335 encode_coding_iso_2022 (struct coding_system
*coding
)
4337 int multibytep
= coding
->dst_multibyte
;
4338 int *charbuf
= coding
->charbuf
;
4339 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4340 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4341 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4344 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4345 && CODING_ISO_BOL (coding
));
4346 EMACS_INT produced_chars
= 0;
4347 Lisp_Object attrs
, eol_type
, charset_list
;
4348 int ascii_compatible
;
4350 int preferred_charset_id
= -1;
4352 CODING_GET_INFO (coding
, attrs
, charset_list
);
4353 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4354 if (VECTORP (eol_type
))
4357 setup_iso_safe_charsets (attrs
);
4358 /* Charset list may have been changed. */
4359 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4360 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4363 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4364 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4365 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4367 while (charbuf
< charbuf_end
)
4369 ASSURE_DESTINATION (safe_room
);
4371 if (bol_designation
)
4373 unsigned char *dst_prev
= dst
;
4375 /* We have to produce designation sequences if any now. */
4376 dst
= encode_designation_at_bol (coding
, charbuf
, dst
);
4377 bol_designation
= 0;
4378 /* We are sure that designation sequences are all ASCII bytes. */
4379 produced_chars
+= dst
- dst_prev
;
4386 /* Handle an annotation. */
4389 case CODING_ANNOTATE_COMPOSITION_MASK
:
4390 /* Not yet implemented. */
4392 case CODING_ANNOTATE_CHARSET_MASK
:
4393 preferred_charset_id
= charbuf
[2];
4394 if (preferred_charset_id
>= 0
4395 && NILP (Fmemq (make_number (preferred_charset_id
),
4397 preferred_charset_id
= -1;
4406 /* Now encode the character C. */
4407 if (c
< 0x20 || c
== 0x7F)
4410 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4412 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4413 ENCODE_RESET_PLANE_AND_REGISTER ();
4414 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4418 for (i
= 0; i
< 4; i
++)
4419 CODING_ISO_DESIGNATION (coding
, i
)
4420 = CODING_ISO_INITIAL (coding
, i
);
4423 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4425 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4426 ENCODE_RESET_PLANE_AND_REGISTER ();
4427 EMIT_ONE_ASCII_BYTE (c
);
4429 else if (ASCII_CHAR_P (c
))
4431 if (ascii_compatible
)
4432 EMIT_ONE_ASCII_BYTE (c
);
4435 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4436 ENCODE_ISO_CHARACTER (charset
, c
);
4439 else if (CHAR_BYTE8_P (c
))
4441 c
= CHAR_TO_BYTE8 (c
);
4446 struct charset
*charset
;
4448 if (preferred_charset_id
>= 0)
4450 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4451 if (! CHAR_CHARSET_P (c
, charset
))
4452 charset
= char_charset (c
, charset_list
, NULL
);
4455 charset
= char_charset (c
, charset_list
, NULL
);
4458 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4460 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4461 charset
= CHARSET_FROM_ID (charset_ascii
);
4465 c
= coding
->default_char
;
4466 charset
= char_charset (c
, charset_list
, NULL
);
4469 ENCODE_ISO_CHARACTER (charset
, c
);
4473 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4474 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4476 ASSURE_DESTINATION (safe_room
);
4477 ENCODE_RESET_PLANE_AND_REGISTER ();
4479 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4480 CODING_ISO_BOL (coding
) = bol_designation
;
4481 coding
->produced_char
+= produced_chars
;
4482 coding
->produced
= dst
- coding
->destination
;
4487 /*** 8,9. SJIS and BIG5 handlers ***/
4489 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4490 quite widely. So, for the moment, Emacs supports them in the bare
4491 C code. But, in the future, they may be supported only by CCL. */
4493 /* SJIS is a coding system encoding three character sets: ASCII, right
4494 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4495 as is. A character of charset katakana-jisx0201 is encoded by
4496 "position-code + 0x80". A character of charset japanese-jisx0208
4497 is encoded in 2-byte but two position-codes are divided and shifted
4498 so that it fit in the range below.
4500 --- CODE RANGE of SJIS ---
4501 (character set) (range)
4503 KATAKANA-JISX0201 0xA0 .. 0xDF
4504 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4505 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4506 -------------------------------
4510 /* BIG5 is a coding system encoding two character sets: ASCII and
4511 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4512 character set and is encoded in two-byte.
4514 --- CODE RANGE of BIG5 ---
4515 (character set) (range)
4517 Big5 (1st byte) 0xA1 .. 0xFE
4518 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4519 --------------------------
4523 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4524 Check if a text is encoded in SJIS. If it is, return
4525 CATEGORY_MASK_SJIS, else return 0. */
4528 detect_coding_sjis (struct coding_system
*coding
,
4529 struct coding_detection_info
*detect_info
)
4531 const unsigned char *src
= coding
->source
, *src_base
;
4532 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4533 int multibytep
= coding
->src_multibyte
;
4534 EMACS_INT consumed_chars
= 0;
4537 Lisp_Object attrs
, charset_list
;
4538 int max_first_byte_of_2_byte_code
;
4540 CODING_GET_INFO (coding
, attrs
, charset_list
);
4541 max_first_byte_of_2_byte_code
4542 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4544 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4545 /* A coding system of this category is always ASCII compatible. */
4546 src
+= coding
->head_ascii
;
4554 if ((c
>= 0x81 && c
<= 0x9F)
4555 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4558 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4560 found
= CATEGORY_MASK_SJIS
;
4562 else if (c
>= 0xA0 && c
< 0xE0)
4563 found
= CATEGORY_MASK_SJIS
;
4567 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4571 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4573 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4576 detect_info
->found
|= found
;
4580 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4581 Check if a text is encoded in BIG5. If it is, return
4582 CATEGORY_MASK_BIG5, else return 0. */
4585 detect_coding_big5 (struct coding_system
*coding
,
4586 struct coding_detection_info
*detect_info
)
4588 const unsigned char *src
= coding
->source
, *src_base
;
4589 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4590 int multibytep
= coding
->src_multibyte
;
4591 EMACS_INT consumed_chars
= 0;
4595 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4596 /* A coding system of this category is always ASCII compatible. */
4597 src
+= coding
->head_ascii
;
4608 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4610 found
= CATEGORY_MASK_BIG5
;
4615 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4619 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4621 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4624 detect_info
->found
|= found
;
4628 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4629 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4632 decode_coding_sjis (struct coding_system
*coding
)
4634 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4635 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4636 const unsigned char *src_base
;
4637 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4638 /* We may produce one charset annotation in one loop and one more at
4641 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4642 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
4643 int multibytep
= coding
->src_multibyte
;
4644 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4645 struct charset
*charset_kanji2
;
4646 Lisp_Object attrs
, charset_list
, val
;
4647 EMACS_INT char_offset
= coding
->produced_char
;
4648 EMACS_INT last_offset
= char_offset
;
4649 int last_id
= charset_ascii
;
4651 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4652 int byte_after_cr
= -1;
4654 CODING_GET_INFO (coding
, attrs
, charset_list
);
4657 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4658 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4659 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4660 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4665 struct charset
*charset
;
4668 consumed_chars_base
= consumed_chars
;
4670 if (charbuf
>= charbuf_end
)
4672 if (byte_after_cr
>= 0)
4677 if (byte_after_cr
>= 0)
4678 c
= byte_after_cr
, byte_after_cr
= -1;
4685 if (eol_dos
&& c
== '\r')
4686 ONE_MORE_BYTE (byte_after_cr
);
4687 charset
= charset_roman
;
4689 else if (c
== 0x80 || c
== 0xA0)
4691 else if (c
>= 0xA1 && c
<= 0xDF)
4693 /* SJIS -> JISX0201-Kana */
4695 charset
= charset_kana
;
4699 /* SJIS -> JISX0208 */
4701 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4705 charset
= charset_kanji
;
4707 else if (c
<= 0xFC && charset_kanji2
)
4709 /* SJIS -> JISX0213-2 */
4711 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4715 charset
= charset_kanji2
;
4719 if (charset
->id
!= charset_ascii
4720 && last_id
!= charset
->id
)
4722 if (last_id
!= charset_ascii
)
4723 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4724 last_id
= charset
->id
;
4725 last_offset
= char_offset
;
4727 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4734 consumed_chars
= consumed_chars_base
;
4736 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4742 if (last_id
!= charset_ascii
)
4743 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4744 coding
->consumed_char
+= consumed_chars_base
;
4745 coding
->consumed
= src_base
- coding
->source
;
4746 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4750 decode_coding_big5 (struct coding_system
*coding
)
4752 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4753 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4754 const unsigned char *src_base
;
4755 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4756 /* We may produce one charset annotation in one loop and one more at
4759 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4760 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
4761 int multibytep
= coding
->src_multibyte
;
4762 struct charset
*charset_roman
, *charset_big5
;
4763 Lisp_Object attrs
, charset_list
, val
;
4764 EMACS_INT char_offset
= coding
->produced_char
;
4765 EMACS_INT last_offset
= char_offset
;
4766 int last_id
= charset_ascii
;
4768 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4769 int byte_after_cr
= -1;
4771 CODING_GET_INFO (coding
, attrs
, charset_list
);
4773 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4774 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4779 struct charset
*charset
;
4782 consumed_chars_base
= consumed_chars
;
4784 if (charbuf
>= charbuf_end
)
4786 if (byte_after_cr
>= 0)
4791 if (byte_after_cr
>= 0)
4792 c
= byte_after_cr
, byte_after_cr
= -1;
4800 if (eol_dos
&& c
== '\r')
4801 ONE_MORE_BYTE (byte_after_cr
);
4802 charset
= charset_roman
;
4807 if (c
< 0xA1 || c
> 0xFE)
4810 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4813 charset
= charset_big5
;
4815 if (charset
->id
!= charset_ascii
4816 && last_id
!= charset
->id
)
4818 if (last_id
!= charset_ascii
)
4819 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4820 last_id
= charset
->id
;
4821 last_offset
= char_offset
;
4823 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4830 consumed_chars
= consumed_chars_base
;
4832 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4838 if (last_id
!= charset_ascii
)
4839 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4840 coding
->consumed_char
+= consumed_chars_base
;
4841 coding
->consumed
= src_base
- coding
->source
;
4842 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4845 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4846 This function can encode charsets `ascii', `katakana-jisx0201',
4847 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4848 are sure that all these charsets are registered as official charset
4849 (i.e. do not have extended leading-codes). Characters of other
4850 charsets are produced without any encoding. If SJIS_P is 1, encode
4851 SJIS text, else encode BIG5 text. */
4854 encode_coding_sjis (struct coding_system
*coding
)
4856 int multibytep
= coding
->dst_multibyte
;
4857 int *charbuf
= coding
->charbuf
;
4858 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4859 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4860 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4862 EMACS_INT produced_chars
= 0;
4863 Lisp_Object attrs
, charset_list
, val
;
4864 int ascii_compatible
;
4865 struct charset
*charset_kanji
, *charset_kana
;
4866 struct charset
*charset_kanji2
;
4869 CODING_GET_INFO (coding
, attrs
, charset_list
);
4870 val
= XCDR (charset_list
);
4871 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4872 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4873 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4875 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4877 while (charbuf
< charbuf_end
)
4879 ASSURE_DESTINATION (safe_room
);
4881 /* Now encode the character C. */
4882 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4883 EMIT_ONE_ASCII_BYTE (c
);
4884 else if (CHAR_BYTE8_P (c
))
4886 c
= CHAR_TO_BYTE8 (c
);
4892 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4896 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4898 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4899 charset
= CHARSET_FROM_ID (charset_ascii
);
4903 c
= coding
->default_char
;
4904 charset
= char_charset (c
, charset_list
, &code
);
4907 if (code
== CHARSET_INVALID_CODE (charset
))
4909 if (charset
== charset_kanji
)
4913 c1
= code
>> 8, c2
= code
& 0xFF;
4914 EMIT_TWO_BYTES (c1
, c2
);
4916 else if (charset
== charset_kana
)
4917 EMIT_ONE_BYTE (code
| 0x80);
4918 else if (charset_kanji2
&& charset
== charset_kanji2
)
4923 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
4925 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4927 JIS_TO_SJIS2 (code
);
4928 c1
= code
>> 8, c2
= code
& 0xFF;
4929 EMIT_TWO_BYTES (c1
, c2
);
4932 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4935 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4938 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4939 coding
->produced_char
+= produced_chars
;
4940 coding
->produced
= dst
- coding
->destination
;
4945 encode_coding_big5 (struct coding_system
*coding
)
4947 int multibytep
= coding
->dst_multibyte
;
4948 int *charbuf
= coding
->charbuf
;
4949 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4950 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4951 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4953 EMACS_INT produced_chars
= 0;
4954 Lisp_Object attrs
, charset_list
, val
;
4955 int ascii_compatible
;
4956 struct charset
*charset_big5
;
4959 CODING_GET_INFO (coding
, attrs
, charset_list
);
4960 val
= XCDR (charset_list
);
4961 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4962 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4964 while (charbuf
< charbuf_end
)
4966 ASSURE_DESTINATION (safe_room
);
4968 /* Now encode the character C. */
4969 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4970 EMIT_ONE_ASCII_BYTE (c
);
4971 else if (CHAR_BYTE8_P (c
))
4973 c
= CHAR_TO_BYTE8 (c
);
4979 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4983 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4985 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4986 charset
= CHARSET_FROM_ID (charset_ascii
);
4990 c
= coding
->default_char
;
4991 charset
= char_charset (c
, charset_list
, &code
);
4994 if (code
== CHARSET_INVALID_CODE (charset
))
4996 if (charset
== charset_big5
)
5000 c1
= code
>> 8, c2
= code
& 0xFF;
5001 EMIT_TWO_BYTES (c1
, c2
);
5004 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5007 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5008 coding
->produced_char
+= produced_chars
;
5009 coding
->produced
= dst
- coding
->destination
;
5014 /*** 10. CCL handlers ***/
5016 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5017 Check if a text is encoded in a coding system of which
5018 encoder/decoder are written in CCL program. If it is, return
5019 CATEGORY_MASK_CCL, else return 0. */
5022 detect_coding_ccl (struct coding_system
*coding
,
5023 struct coding_detection_info
*detect_info
)
5025 const unsigned char *src
= coding
->source
, *src_base
;
5026 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5027 int multibytep
= coding
->src_multibyte
;
5028 EMACS_INT consumed_chars
= 0;
5030 unsigned char *valids
;
5031 EMACS_INT head_ascii
= coding
->head_ascii
;
5034 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5036 coding
= &coding_categories
[coding_category_ccl
];
5037 valids
= CODING_CCL_VALIDS (coding
);
5038 attrs
= CODING_ID_ATTRS (coding
->id
);
5039 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5048 if (c
< 0 || ! valids
[c
])
5050 if ((valids
[c
] > 1))
5051 found
= CATEGORY_MASK_CCL
;
5053 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5057 detect_info
->found
|= found
;
5062 decode_coding_ccl (struct coding_system
*coding
)
5064 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5065 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5066 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5067 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5068 EMACS_INT consumed_chars
= 0;
5069 int multibytep
= coding
->src_multibyte
;
5070 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5071 int source_charbuf
[1024];
5072 int source_byteidx
[1025];
5073 Lisp_Object attrs
, charset_list
;
5075 CODING_GET_INFO (coding
, attrs
, charset_list
);
5079 const unsigned char *p
= src
;
5084 while (i
< 1024 && p
< src_end
)
5086 source_byteidx
[i
] = p
- src
;
5087 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5089 source_byteidx
[i
] = p
- src
;
5092 while (i
< 1024 && p
< src_end
)
5093 source_charbuf
[i
++] = *p
++;
5095 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5096 ccl
->last_block
= 1;
5097 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5099 charbuf
+= ccl
->produced
;
5101 src
+= source_byteidx
[ccl
->consumed
];
5103 src
+= ccl
->consumed
;
5104 consumed_chars
+= ccl
->consumed
;
5105 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5109 switch (ccl
->status
)
5111 case CCL_STAT_SUSPEND_BY_SRC
:
5112 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5114 case CCL_STAT_SUSPEND_BY_DST
:
5115 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5118 case CCL_STAT_INVALID_CMD
:
5119 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5122 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5125 coding
->consumed_char
+= consumed_chars
;
5126 coding
->consumed
= src
- coding
->source
;
5127 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5131 encode_coding_ccl (struct coding_system
*coding
)
5133 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5134 int multibytep
= coding
->dst_multibyte
;
5135 int *charbuf
= coding
->charbuf
;
5136 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5137 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5138 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5139 int destination_charbuf
[1024];
5140 EMACS_INT produced_chars
= 0;
5142 Lisp_Object attrs
, charset_list
;
5144 CODING_GET_INFO (coding
, attrs
, charset_list
);
5145 if (coding
->consumed_char
== coding
->src_chars
5146 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5147 ccl
->last_block
= 1;
5149 while (charbuf
< charbuf_end
)
5151 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5152 charbuf_end
- charbuf
, 1024, charset_list
);
5155 ASSURE_DESTINATION (ccl
->produced
* 2);
5156 for (i
= 0; i
< ccl
->produced
; i
++)
5157 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5161 ASSURE_DESTINATION (ccl
->produced
);
5162 for (i
= 0; i
< ccl
->produced
; i
++)
5163 *dst
++ = destination_charbuf
[i
] & 0xFF;
5164 produced_chars
+= ccl
->produced
;
5166 charbuf
+= ccl
->consumed
;
5167 if (ccl
->status
== CCL_STAT_QUIT
5168 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5172 switch (ccl
->status
)
5174 case CCL_STAT_SUSPEND_BY_SRC
:
5175 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5177 case CCL_STAT_SUSPEND_BY_DST
:
5178 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5181 case CCL_STAT_INVALID_CMD
:
5182 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5185 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5189 coding
->produced_char
+= produced_chars
;
5190 coding
->produced
= dst
- coding
->destination
;
5196 /*** 10, 11. no-conversion handlers ***/
5198 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5201 decode_coding_raw_text (struct coding_system
*coding
)
5204 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5206 coding
->chars_at_source
= 1;
5207 coding
->consumed_char
= coding
->src_chars
;
5208 coding
->consumed
= coding
->src_bytes
;
5209 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5211 coding
->consumed_char
--;
5213 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5216 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5220 encode_coding_raw_text (struct coding_system
*coding
)
5222 int multibytep
= coding
->dst_multibyte
;
5223 int *charbuf
= coding
->charbuf
;
5224 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5225 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5226 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5227 EMACS_INT produced_chars
= 0;
5232 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5234 if (coding
->src_multibyte
)
5235 while (charbuf
< charbuf_end
)
5237 ASSURE_DESTINATION (safe_room
);
5239 if (ASCII_CHAR_P (c
))
5240 EMIT_ONE_ASCII_BYTE (c
);
5241 else if (CHAR_BYTE8_P (c
))
5243 c
= CHAR_TO_BYTE8 (c
);
5248 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5250 CHAR_STRING_ADVANCE (c
, p1
);
5253 EMIT_ONE_BYTE (*p0
);
5260 while (charbuf
< charbuf_end
)
5262 ASSURE_DESTINATION (safe_room
);
5269 if (coding
->src_multibyte
)
5271 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5273 while (charbuf
< charbuf_end
)
5275 ASSURE_DESTINATION (safe_room
);
5277 if (ASCII_CHAR_P (c
))
5279 else if (CHAR_BYTE8_P (c
))
5280 *dst
++ = CHAR_TO_BYTE8 (c
);
5282 CHAR_STRING_ADVANCE (c
, dst
);
5287 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5288 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5289 *dst
++ = *charbuf
++;
5291 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5293 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5294 coding
->produced_char
+= produced_chars
;
5295 coding
->produced
= dst
- coding
->destination
;
5299 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5300 Check if a text is encoded in a charset-based coding system. If it
5301 is, return 1, else return 0. */
5304 detect_coding_charset (struct coding_system
*coding
,
5305 struct coding_detection_info
*detect_info
)
5307 const unsigned char *src
= coding
->source
, *src_base
;
5308 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5309 int multibytep
= coding
->src_multibyte
;
5310 EMACS_INT consumed_chars
= 0;
5311 Lisp_Object attrs
, valids
, name
;
5313 EMACS_INT head_ascii
= coding
->head_ascii
;
5314 int check_latin_extra
= 0;
5316 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5318 coding
= &coding_categories
[coding_category_charset
];
5319 attrs
= CODING_ID_ATTRS (coding
->id
);
5320 valids
= AREF (attrs
, coding_attr_charset_valids
);
5321 name
= CODING_ID_NAME (coding
->id
);
5322 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5323 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5324 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5325 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5326 check_latin_extra
= 1;
5328 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5335 struct charset
*charset
;
5342 val
= AREF (valids
, c
);
5348 && check_latin_extra
5349 && (!VECTORP (Vlatin_extra_code_table
)
5350 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5352 found
= CATEGORY_MASK_CHARSET
;
5356 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5357 dim
= CHARSET_DIMENSION (charset
);
5358 for (idx
= 1; idx
< dim
; idx
++)
5363 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
5364 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
5373 for (; CONSP (val
); val
= XCDR (val
))
5375 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5376 dim
= CHARSET_DIMENSION (charset
);
5382 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5383 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5398 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5402 detect_info
->found
|= found
;
5407 decode_coding_charset (struct coding_system
*coding
)
5409 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5410 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5411 const unsigned char *src_base
;
5412 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5413 /* We may produce one charset annotation in one loop and one more at
5416 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5417 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
5418 int multibytep
= coding
->src_multibyte
;
5419 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5421 EMACS_INT char_offset
= coding
->produced_char
;
5422 EMACS_INT last_offset
= char_offset
;
5423 int last_id
= charset_ascii
;
5425 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5426 int byte_after_cr
= -1;
5428 valids
= AREF (attrs
, coding_attr_charset_valids
);
5434 struct charset
*charset
;
5440 consumed_chars_base
= consumed_chars
;
5442 if (charbuf
>= charbuf_end
)
5444 if (byte_after_cr
>= 0)
5449 if (byte_after_cr
>= 0)
5457 if (eol_dos
&& c
== '\r')
5458 ONE_MORE_BYTE (byte_after_cr
);
5464 val
= AREF (valids
, c
);
5465 if (! INTEGERP (val
) && ! CONSP (val
))
5469 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5470 dim
= CHARSET_DIMENSION (charset
);
5474 code
= (code
<< 8) | c
;
5477 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5482 /* VAL is a list of charset IDs. It is assured that the
5483 list is sorted by charset dimensions (smaller one
5487 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5488 dim
= CHARSET_DIMENSION (charset
);
5492 code
= (code
<< 8) | c
;
5495 CODING_DECODE_CHAR (coding
, src
, src_base
,
5496 src_end
, charset
, code
, c
);
5504 if (charset
->id
!= charset_ascii
5505 && last_id
!= charset
->id
)
5507 if (last_id
!= charset_ascii
)
5508 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5509 last_id
= charset
->id
;
5510 last_offset
= char_offset
;
5519 consumed_chars
= consumed_chars_base
;
5521 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5527 if (last_id
!= charset_ascii
)
5528 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5529 coding
->consumed_char
+= consumed_chars_base
;
5530 coding
->consumed
= src_base
- coding
->source
;
5531 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5535 encode_coding_charset (struct coding_system
*coding
)
5537 int multibytep
= coding
->dst_multibyte
;
5538 int *charbuf
= coding
->charbuf
;
5539 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5540 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5541 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5542 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5543 EMACS_INT produced_chars
= 0;
5544 Lisp_Object attrs
, charset_list
;
5545 int ascii_compatible
;
5548 CODING_GET_INFO (coding
, attrs
, charset_list
);
5549 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5551 while (charbuf
< charbuf_end
)
5553 struct charset
*charset
;
5556 ASSURE_DESTINATION (safe_room
);
5558 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5559 EMIT_ONE_ASCII_BYTE (c
);
5560 else if (CHAR_BYTE8_P (c
))
5562 c
= CHAR_TO_BYTE8 (c
);
5567 charset
= char_charset (c
, charset_list
, &code
);
5570 if (CHARSET_DIMENSION (charset
) == 1)
5571 EMIT_ONE_BYTE (code
);
5572 else if (CHARSET_DIMENSION (charset
) == 2)
5573 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5574 else if (CHARSET_DIMENSION (charset
) == 3)
5575 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5577 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5578 (code
>> 8) & 0xFF, code
& 0xFF);
5582 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5583 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5585 c
= coding
->default_char
;
5591 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5592 coding
->produced_char
+= produced_chars
;
5593 coding
->produced
= dst
- coding
->destination
;
5598 /*** 7. C library functions ***/
5600 /* Setup coding context CODING from information about CODING_SYSTEM.
5601 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5602 CODING_SYSTEM is invalid, signal an error. */
5605 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5608 Lisp_Object eol_type
;
5609 Lisp_Object coding_type
;
5612 if (NILP (coding_system
))
5613 coding_system
= Qundecided
;
5615 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5617 attrs
= CODING_ID_ATTRS (coding
->id
);
5618 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5621 coding
->head_ascii
= -1;
5622 if (VECTORP (eol_type
))
5623 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5624 | CODING_REQUIRE_DETECTION_MASK
);
5625 else if (! EQ (eol_type
, Qunix
))
5626 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5627 | CODING_REQUIRE_ENCODING_MASK
);
5629 coding
->common_flags
= 0;
5630 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5631 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5632 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5633 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5634 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5635 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5637 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5638 coding
->max_charset_id
= SCHARS (val
) - 1;
5639 coding
->safe_charsets
= SDATA (val
);
5640 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5641 coding
->carryover_bytes
= 0;
5643 coding_type
= CODING_ATTR_TYPE (attrs
);
5644 if (EQ (coding_type
, Qundecided
))
5646 coding
->detector
= NULL
;
5647 coding
->decoder
= decode_coding_raw_text
;
5648 coding
->encoder
= encode_coding_raw_text
;
5649 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5651 else if (EQ (coding_type
, Qiso_2022
))
5654 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5656 /* Invoke graphic register 0 to plane 0. */
5657 CODING_ISO_INVOCATION (coding
, 0) = 0;
5658 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5659 CODING_ISO_INVOCATION (coding
, 1)
5660 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5661 /* Setup the initial status of designation. */
5662 for (i
= 0; i
< 4; i
++)
5663 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5664 /* Not single shifting initially. */
5665 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5666 /* Beginning of buffer should also be regarded as bol. */
5667 CODING_ISO_BOL (coding
) = 1;
5668 coding
->detector
= detect_coding_iso_2022
;
5669 coding
->decoder
= decode_coding_iso_2022
;
5670 coding
->encoder
= encode_coding_iso_2022
;
5671 if (flags
& CODING_ISO_FLAG_SAFE
)
5672 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5673 coding
->common_flags
5674 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5675 | CODING_REQUIRE_FLUSHING_MASK
);
5676 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5677 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5678 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5679 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5680 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5682 setup_iso_safe_charsets (attrs
);
5683 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5684 coding
->max_charset_id
= SCHARS (val
) - 1;
5685 coding
->safe_charsets
= SDATA (val
);
5687 CODING_ISO_FLAGS (coding
) = flags
;
5688 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5689 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5690 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5691 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5693 else if (EQ (coding_type
, Qcharset
))
5695 coding
->detector
= detect_coding_charset
;
5696 coding
->decoder
= decode_coding_charset
;
5697 coding
->encoder
= encode_coding_charset
;
5698 coding
->common_flags
5699 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5701 else if (EQ (coding_type
, Qutf_8
))
5703 val
= AREF (attrs
, coding_attr_utf_bom
);
5704 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5705 : EQ (val
, Qt
) ? utf_with_bom
5707 coding
->detector
= detect_coding_utf_8
;
5708 coding
->decoder
= decode_coding_utf_8
;
5709 coding
->encoder
= encode_coding_utf_8
;
5710 coding
->common_flags
5711 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5712 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5713 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5715 else if (EQ (coding_type
, Qutf_16
))
5717 val
= AREF (attrs
, coding_attr_utf_bom
);
5718 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5719 : EQ (val
, Qt
) ? utf_with_bom
5721 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5722 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5723 : utf_16_little_endian
);
5724 CODING_UTF_16_SURROGATE (coding
) = 0;
5725 coding
->detector
= detect_coding_utf_16
;
5726 coding
->decoder
= decode_coding_utf_16
;
5727 coding
->encoder
= encode_coding_utf_16
;
5728 coding
->common_flags
5729 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5730 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5731 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5733 else if (EQ (coding_type
, Qccl
))
5735 coding
->detector
= detect_coding_ccl
;
5736 coding
->decoder
= decode_coding_ccl
;
5737 coding
->encoder
= encode_coding_ccl
;
5738 coding
->common_flags
5739 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5740 | CODING_REQUIRE_FLUSHING_MASK
);
5742 else if (EQ (coding_type
, Qemacs_mule
))
5744 coding
->detector
= detect_coding_emacs_mule
;
5745 coding
->decoder
= decode_coding_emacs_mule
;
5746 coding
->encoder
= encode_coding_emacs_mule
;
5747 coding
->common_flags
5748 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5749 coding
->spec
.emacs_mule
.full_support
= 1;
5750 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5751 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5753 Lisp_Object tail
, safe_charsets
;
5754 int max_charset_id
= 0;
5756 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5758 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5759 max_charset_id
= XFASTINT (XCAR (tail
));
5760 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5761 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5762 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5764 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5765 coding
->max_charset_id
= max_charset_id
;
5766 coding
->safe_charsets
= SDATA (safe_charsets
);
5767 coding
->spec
.emacs_mule
.full_support
= 1;
5769 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5770 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5772 else if (EQ (coding_type
, Qshift_jis
))
5774 coding
->detector
= detect_coding_sjis
;
5775 coding
->decoder
= decode_coding_sjis
;
5776 coding
->encoder
= encode_coding_sjis
;
5777 coding
->common_flags
5778 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5780 else if (EQ (coding_type
, Qbig5
))
5782 coding
->detector
= detect_coding_big5
;
5783 coding
->decoder
= decode_coding_big5
;
5784 coding
->encoder
= encode_coding_big5
;
5785 coding
->common_flags
5786 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5788 else /* EQ (coding_type, Qraw_text) */
5790 coding
->detector
= NULL
;
5791 coding
->decoder
= decode_coding_raw_text
;
5792 coding
->encoder
= encode_coding_raw_text
;
5793 if (! EQ (eol_type
, Qunix
))
5795 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5796 if (! VECTORP (eol_type
))
5797 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5805 /* Return a list of charsets supported by CODING. */
5808 coding_charset_list (struct coding_system
*coding
)
5810 Lisp_Object attrs
, charset_list
;
5812 CODING_GET_INFO (coding
, attrs
, charset_list
);
5813 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5815 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5817 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5818 charset_list
= Viso_2022_charset_list
;
5820 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5822 charset_list
= Vemacs_mule_charset_list
;
5824 return charset_list
;
5828 /* Return a list of charsets supported by CODING-SYSTEM. */
5831 coding_system_charset_list (Lisp_Object coding_system
)
5834 Lisp_Object attrs
, charset_list
;
5836 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5837 attrs
= CODING_ID_ATTRS (id
);
5839 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5841 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5843 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5844 charset_list
= Viso_2022_charset_list
;
5846 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5848 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5850 charset_list
= Vemacs_mule_charset_list
;
5854 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5856 return charset_list
;
5860 /* Return raw-text or one of its subsidiaries that has the same
5861 eol_type as CODING-SYSTEM. */
5864 raw_text_coding_system (Lisp_Object coding_system
)
5866 Lisp_Object spec
, attrs
;
5867 Lisp_Object eol_type
, raw_text_eol_type
;
5869 if (NILP (coding_system
))
5871 spec
= CODING_SYSTEM_SPEC (coding_system
);
5872 attrs
= AREF (spec
, 0);
5874 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5875 return coding_system
;
5877 eol_type
= AREF (spec
, 2);
5878 if (VECTORP (eol_type
))
5880 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5881 raw_text_eol_type
= AREF (spec
, 2);
5882 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5883 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5884 : AREF (raw_text_eol_type
, 2));
5888 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5889 the subsidiary that has the same eol-spec as PARENT (if it is not
5890 nil and specifies end-of-line format) or the system's setting
5891 (system_eol_type). */
5894 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5896 Lisp_Object spec
, eol_type
;
5898 if (NILP (coding_system
))
5899 coding_system
= Qraw_text
;
5900 spec
= CODING_SYSTEM_SPEC (coding_system
);
5901 eol_type
= AREF (spec
, 2);
5902 if (VECTORP (eol_type
))
5904 Lisp_Object parent_eol_type
;
5906 if (! NILP (parent
))
5908 Lisp_Object parent_spec
;
5910 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5911 parent_eol_type
= AREF (parent_spec
, 2);
5912 if (VECTORP (parent_eol_type
))
5913 parent_eol_type
= system_eol_type
;
5916 parent_eol_type
= system_eol_type
;
5917 if (EQ (parent_eol_type
, Qunix
))
5918 coding_system
= AREF (eol_type
, 0);
5919 else if (EQ (parent_eol_type
, Qdos
))
5920 coding_system
= AREF (eol_type
, 1);
5921 else if (EQ (parent_eol_type
, Qmac
))
5922 coding_system
= AREF (eol_type
, 2);
5924 return coding_system
;
5928 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
5929 decided for writing to a process. If not, complement them, and
5930 return a new coding system. */
5933 complement_process_encoding_system (Lisp_Object coding_system
)
5935 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
5936 Lisp_Object spec
, attrs
;
5939 for (i
= 0; i
< 3; i
++)
5942 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
5944 coding_system
= preferred_coding_system ();
5945 spec
= CODING_SYSTEM_SPEC (coding_system
);
5948 attrs
= AREF (spec
, 0);
5949 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
5950 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
5951 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
5952 eol_base
= coding_system
;
5953 if (! NILP (coding_base
) && ! NILP (eol_base
))
5958 /* The original CODING_SYSTEM didn't specify text-conversion or
5959 eol-conversion. Be sure that we return a fully complemented
5961 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
5962 return coding_system
;
5966 /* Emacs has a mechanism to automatically detect a coding system if it
5967 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5968 it's impossible to distinguish some coding systems accurately
5969 because they use the same range of codes. So, at first, coding
5970 systems are categorized into 7, those are:
5972 o coding-category-emacs-mule
5974 The category for a coding system which has the same code range
5975 as Emacs' internal format. Assigned the coding-system (Lisp
5976 symbol) `emacs-mule' by default.
5978 o coding-category-sjis
5980 The category for a coding system which has the same code range
5981 as SJIS. Assigned the coding-system (Lisp
5982 symbol) `japanese-shift-jis' by default.
5984 o coding-category-iso-7
5986 The category for a coding system which has the same code range
5987 as ISO2022 of 7-bit environment. This doesn't use any locking
5988 shift and single shift functions. This can encode/decode all
5989 charsets. Assigned the coding-system (Lisp symbol)
5990 `iso-2022-7bit' by default.
5992 o coding-category-iso-7-tight
5994 Same as coding-category-iso-7 except that this can
5995 encode/decode only the specified charsets.
5997 o coding-category-iso-8-1
5999 The category for a coding system which has the same code range
6000 as ISO2022 of 8-bit environment and graphic plane 1 used only
6001 for DIMENSION1 charset. This doesn't use any locking shift
6002 and single shift functions. Assigned the coding-system (Lisp
6003 symbol) `iso-latin-1' by default.
6005 o coding-category-iso-8-2
6007 The category for a coding system which has the same code range
6008 as ISO2022 of 8-bit environment and graphic plane 1 used only
6009 for DIMENSION2 charset. This doesn't use any locking shift
6010 and single shift functions. Assigned the coding-system (Lisp
6011 symbol) `japanese-iso-8bit' by default.
6013 o coding-category-iso-7-else
6015 The category for a coding system which has the same code range
6016 as ISO2022 of 7-bit environment but uses locking shift or
6017 single shift functions. Assigned the coding-system (Lisp
6018 symbol) `iso-2022-7bit-lock' by default.
6020 o coding-category-iso-8-else
6022 The category for a coding system which has the same code range
6023 as ISO2022 of 8-bit environment but uses locking shift or
6024 single shift functions. Assigned the coding-system (Lisp
6025 symbol) `iso-2022-8bit-ss2' by default.
6027 o coding-category-big5
6029 The category for a coding system which has the same code range
6030 as BIG5. Assigned the coding-system (Lisp symbol)
6031 `cn-big5' by default.
6033 o coding-category-utf-8
6035 The category for a coding system which has the same code range
6036 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6037 symbol) `utf-8' by default.
6039 o coding-category-utf-16-be
6041 The category for a coding system in which a text has an
6042 Unicode signature (cf. Unicode Standard) in the order of BIG
6043 endian at the head. Assigned the coding-system (Lisp symbol)
6044 `utf-16-be' by default.
6046 o coding-category-utf-16-le
6048 The category for a coding system in which a text has an
6049 Unicode signature (cf. Unicode Standard) in the order of
6050 LITTLE endian at the head. Assigned the coding-system (Lisp
6051 symbol) `utf-16-le' by default.
6053 o coding-category-ccl
6055 The category for a coding system of which encoder/decoder is
6056 written in CCL programs. The default value is nil, i.e., no
6057 coding system is assigned.
6059 o coding-category-binary
6061 The category for a coding system not categorized in any of the
6062 above. Assigned the coding-system (Lisp symbol)
6063 `no-conversion' by default.
6065 Each of them is a Lisp symbol and the value is an actual
6066 `coding-system's (this is also a Lisp symbol) assigned by a user.
6067 What Emacs does actually is to detect a category of coding system.
6068 Then, it uses a `coding-system' assigned to it. If Emacs can't
6069 decide only one possible category, it selects a category of the
6070 highest priority. Priorities of categories are also specified by a
6071 user in a Lisp variable `coding-category-list'.
6075 #define EOL_SEEN_NONE 0
6076 #define EOL_SEEN_LF 1
6077 #define EOL_SEEN_CR 2
6078 #define EOL_SEEN_CRLF 4
6080 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6081 SOURCE is encoded. If CATEGORY is one of
6082 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6083 two-byte, else they are encoded by one-byte.
6085 Return one of EOL_SEEN_XXX. */
6087 #define MAX_EOL_CHECK_COUNT 3
6090 detect_eol (const unsigned char *source
, EMACS_INT src_bytes
,
6091 enum coding_category category
)
6093 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6096 int eol_seen
= EOL_SEEN_NONE
;
6098 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6102 msb
= category
== (coding_category_utf_16_le
6103 | coding_category_utf_16_le_nosig
);
6106 while (src
+ 1 < src_end
)
6109 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6114 this_eol
= EOL_SEEN_LF
;
6115 else if (src
+ 3 >= src_end
6116 || src
[msb
+ 2] != 0
6117 || src
[lsb
+ 2] != '\n')
6118 this_eol
= EOL_SEEN_CR
;
6121 this_eol
= EOL_SEEN_CRLF
;
6125 if (eol_seen
== EOL_SEEN_NONE
)
6126 /* This is the first end-of-line. */
6127 eol_seen
= this_eol
;
6128 else if (eol_seen
!= this_eol
)
6130 /* The found type is different from what found before.
6131 Allow for stray ^M characters in DOS EOL files. */
6132 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6133 || (eol_seen
== EOL_SEEN_CRLF
6134 && this_eol
== EOL_SEEN_CR
))
6135 eol_seen
= EOL_SEEN_CRLF
;
6138 eol_seen
= EOL_SEEN_LF
;
6142 if (++total
== MAX_EOL_CHECK_COUNT
)
6149 while (src
< src_end
)
6152 if (c
== '\n' || c
== '\r')
6157 this_eol
= EOL_SEEN_LF
;
6158 else if (src
>= src_end
|| *src
!= '\n')
6159 this_eol
= EOL_SEEN_CR
;
6161 this_eol
= EOL_SEEN_CRLF
, src
++;
6163 if (eol_seen
== EOL_SEEN_NONE
)
6164 /* This is the first end-of-line. */
6165 eol_seen
= this_eol
;
6166 else if (eol_seen
!= this_eol
)
6168 /* The found type is different from what found before.
6169 Allow for stray ^M characters in DOS EOL files. */
6170 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6171 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6172 eol_seen
= EOL_SEEN_CRLF
;
6175 eol_seen
= EOL_SEEN_LF
;
6179 if (++total
== MAX_EOL_CHECK_COUNT
)
6188 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6190 Lisp_Object eol_type
;
6192 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6193 if (eol_seen
& EOL_SEEN_LF
)
6195 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6198 else if (eol_seen
& EOL_SEEN_CRLF
)
6200 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6203 else if (eol_seen
& EOL_SEEN_CR
)
6205 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6211 /* Detect how a text specified in CODING is encoded. If a coding
6212 system is detected, update fields of CODING by the detected coding
6216 detect_coding (struct coding_system
*coding
)
6218 const unsigned char *src
, *src_end
;
6219 int saved_mode
= coding
->mode
;
6221 coding
->consumed
= coding
->consumed_char
= 0;
6222 coding
->produced
= coding
->produced_char
= 0;
6223 coding_set_source (coding
);
6225 src_end
= coding
->source
+ coding
->src_bytes
;
6226 coding
->head_ascii
= 0;
6228 /* If we have not yet decided the text encoding type, detect it
6230 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6233 struct coding_detection_info detect_info
;
6234 int null_byte_found
= 0, eight_bit_found
= 0;
6236 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6237 for (src
= coding
->source
; src
< src_end
; src
++)
6242 eight_bit_found
= 1;
6243 if (null_byte_found
)
6248 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6249 && ! inhibit_iso_escape_detection
6250 && ! detect_info
.checked
)
6252 if (detect_coding_iso_2022 (coding
, &detect_info
))
6254 /* We have scanned the whole data. */
6255 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6257 /* We didn't find an 8-bit code. We may
6258 have found a null-byte, but it's very
6259 rare that a binary file conforms to
6262 coding
->head_ascii
= src
- coding
->source
;
6264 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6268 else if (! c
&& !inhibit_null_byte_detection
)
6270 null_byte_found
= 1;
6271 if (eight_bit_found
)
6274 if (! eight_bit_found
)
6275 coding
->head_ascii
++;
6277 else if (! eight_bit_found
)
6278 coding
->head_ascii
++;
6281 if (null_byte_found
|| eight_bit_found
6282 || coding
->head_ascii
< coding
->src_bytes
6283 || detect_info
.found
)
6285 enum coding_category category
;
6286 struct coding_system
*this;
6288 if (coding
->head_ascii
== coding
->src_bytes
)
6289 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6290 for (i
= 0; i
< coding_category_raw_text
; i
++)
6292 category
= coding_priorities
[i
];
6293 this = coding_categories
+ category
;
6294 if (detect_info
.found
& (1 << category
))
6299 if (null_byte_found
)
6301 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6302 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6304 for (i
= 0; i
< coding_category_raw_text
; i
++)
6306 category
= coding_priorities
[i
];
6307 this = coding_categories
+ category
;
6310 /* No coding system of this category is defined. */
6311 detect_info
.rejected
|= (1 << category
);
6313 else if (category
>= coding_category_raw_text
)
6315 else if (detect_info
.checked
& (1 << category
))
6317 if (detect_info
.found
& (1 << category
))
6320 else if ((*(this->detector
)) (coding
, &detect_info
)
6321 && detect_info
.found
& (1 << category
))
6323 if (category
== coding_category_utf_16_auto
)
6325 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6326 category
= coding_category_utf_16_le
;
6328 category
= coding_category_utf_16_be
;
6335 if (i
< coding_category_raw_text
)
6336 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6337 else if (null_byte_found
)
6338 setup_coding_system (Qno_conversion
, coding
);
6339 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6340 == CATEGORY_MASK_ANY
)
6341 setup_coding_system (Qraw_text
, coding
);
6342 else if (detect_info
.rejected
)
6343 for (i
= 0; i
< coding_category_raw_text
; i
++)
6344 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6346 this = coding_categories
+ coding_priorities
[i
];
6347 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6352 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6353 == coding_category_utf_8_auto
)
6355 Lisp_Object coding_systems
;
6356 struct coding_detection_info detect_info
;
6359 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6360 detect_info
.found
= detect_info
.rejected
= 0;
6361 coding
->head_ascii
= 0;
6362 if (CONSP (coding_systems
)
6363 && detect_coding_utf_8 (coding
, &detect_info
))
6365 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6366 setup_coding_system (XCAR (coding_systems
), coding
);
6368 setup_coding_system (XCDR (coding_systems
), coding
);
6371 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6372 == coding_category_utf_16_auto
)
6374 Lisp_Object coding_systems
;
6375 struct coding_detection_info detect_info
;
6378 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6379 detect_info
.found
= detect_info
.rejected
= 0;
6380 coding
->head_ascii
= 0;
6381 if (CONSP (coding_systems
)
6382 && detect_coding_utf_16 (coding
, &detect_info
))
6384 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6385 setup_coding_system (XCAR (coding_systems
), coding
);
6386 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6387 setup_coding_system (XCDR (coding_systems
), coding
);
6390 coding
->mode
= saved_mode
;
6395 decode_eol (struct coding_system
*coding
)
6397 Lisp_Object eol_type
;
6398 unsigned char *p
, *pbeg
, *pend
;
6400 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6401 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6404 if (NILP (coding
->dst_object
))
6405 pbeg
= coding
->destination
;
6407 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6408 pend
= pbeg
+ coding
->produced
;
6410 if (VECTORP (eol_type
))
6412 int eol_seen
= EOL_SEEN_NONE
;
6414 for (p
= pbeg
; p
< pend
; p
++)
6417 eol_seen
|= EOL_SEEN_LF
;
6418 else if (*p
== '\r')
6420 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6422 eol_seen
|= EOL_SEEN_CRLF
;
6426 eol_seen
|= EOL_SEEN_CR
;
6429 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6430 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6431 && (eol_seen
& EOL_SEEN_CR
) != 0
6432 && (eol_seen
& EOL_SEEN_LF
) == 0)
6433 eol_seen
= EOL_SEEN_CRLF
;
6434 else if (eol_seen
!= EOL_SEEN_NONE
6435 && eol_seen
!= EOL_SEEN_LF
6436 && eol_seen
!= EOL_SEEN_CRLF
6437 && eol_seen
!= EOL_SEEN_CR
)
6438 eol_seen
= EOL_SEEN_LF
;
6439 if (eol_seen
!= EOL_SEEN_NONE
)
6440 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6443 if (EQ (eol_type
, Qmac
))
6445 for (p
= pbeg
; p
< pend
; p
++)
6449 else if (EQ (eol_type
, Qdos
))
6453 if (NILP (coding
->dst_object
))
6455 /* Start deleting '\r' from the tail to minimize the memory
6457 for (p
= pend
- 2; p
>= pbeg
; p
--)
6460 memmove (p
, p
+ 1, pend
-- - p
- 1);
6466 EMACS_INT pos_byte
= coding
->dst_pos_byte
;
6467 EMACS_INT pos
= coding
->dst_pos
;
6468 EMACS_INT pos_end
= pos
+ coding
->produced_char
- 1;
6470 while (pos
< pos_end
)
6472 p
= BYTE_POS_ADDR (pos_byte
);
6473 if (*p
== '\r' && p
[1] == '\n')
6475 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6480 if (coding
->dst_multibyte
)
6481 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6486 coding
->produced
-= n
;
6487 coding
->produced_char
-= n
;
6492 /* Return a translation table (or list of them) from coding system
6493 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6494 decoding (ENCODEP is zero). */
6497 get_translation_table (Lisp_Object attrs
, int encodep
, int *max_lookup
)
6499 Lisp_Object standard
, translation_table
;
6502 if (NILP (Venable_character_translation
))
6509 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6510 standard
= Vstandard_translation_table_for_encode
;
6512 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6513 standard
= Vstandard_translation_table_for_decode
;
6514 if (NILP (translation_table
))
6515 translation_table
= standard
;
6518 if (SYMBOLP (translation_table
))
6519 translation_table
= Fget (translation_table
, Qtranslation_table
);
6520 else if (CONSP (translation_table
))
6522 translation_table
= Fcopy_sequence (translation_table
);
6523 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6524 if (SYMBOLP (XCAR (val
)))
6525 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6527 if (CHAR_TABLE_P (standard
))
6529 if (CONSP (translation_table
))
6530 translation_table
= nconc2 (translation_table
,
6531 Fcons (standard
, Qnil
));
6533 translation_table
= Fcons (translation_table
,
6534 Fcons (standard
, Qnil
));
6541 if (CHAR_TABLE_P (translation_table
)
6542 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6544 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6545 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6546 *max_lookup
= XFASTINT (val
);
6548 else if (CONSP (translation_table
))
6552 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6553 if (CHAR_TABLE_P (XCAR (tail
))
6554 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6556 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6557 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6558 *max_lookup
= XFASTINT (tailval
);
6562 return translation_table
;
6565 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6568 if (CHAR_TABLE_P (table)) \
6570 trans = CHAR_TABLE_REF (table, c); \
6571 if (CHARACTERP (trans)) \
6572 c = XFASTINT (trans), trans = Qnil; \
6574 else if (CONSP (table)) \
6578 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6579 if (CHAR_TABLE_P (XCAR (tail))) \
6581 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6582 if (CHARACTERP (trans)) \
6583 c = XFASTINT (trans), trans = Qnil; \
6584 else if (! NILP (trans)) \
6591 /* Return a translation of character(s) at BUF according to TRANS.
6592 TRANS is TO-CHAR or ((FROM . TO) ...) where
6593 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6594 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6595 translation is found, and Qnil if not found..
6596 If BUF is too short to lookup characters in FROM, return Qt. */
6599 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6602 if (INTEGERP (trans
))
6604 for (; CONSP (trans
); trans
= XCDR (trans
))
6606 Lisp_Object val
= XCAR (trans
);
6607 Lisp_Object from
= XCAR (val
);
6608 int len
= ASIZE (from
);
6611 for (i
= 0; i
< len
; i
++)
6613 if (buf
+ i
== buf_end
)
6615 if (XINT (AREF (from
, i
)) != buf
[i
])
6626 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6629 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6630 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6632 EMACS_INT produced_chars
= 0;
6635 if (! coding
->chars_at_source
)
6637 /* Source characters are in coding->charbuf. */
6638 int *buf
= coding
->charbuf
;
6639 int *buf_end
= buf
+ coding
->charbuf_used
;
6641 if (EQ (coding
->src_object
, coding
->dst_object
))
6643 coding_set_source (coding
);
6644 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6647 while (buf
< buf_end
)
6653 EMACS_INT from_nchars
= 1, to_nchars
= 1;
6654 Lisp_Object trans
= Qnil
;
6656 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6659 trans
= get_translation (trans
, buf
, buf_end
);
6660 if (INTEGERP (trans
))
6662 else if (CONSP (trans
))
6664 from_nchars
= ASIZE (XCAR (trans
));
6665 trans
= XCDR (trans
);
6666 if (INTEGERP (trans
))
6670 to_nchars
= ASIZE (trans
);
6671 c
= XINT (AREF (trans
, 0));
6674 else if (EQ (trans
, Qt
) && ! last_block
)
6678 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6680 dst
= alloc_destination (coding
,
6682 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6684 if (EQ (coding
->src_object
, coding
->dst_object
))
6686 coding_set_source (coding
);
6687 dst_end
= (((unsigned char *) coding
->source
)
6688 + coding
->consumed
);
6691 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6694 for (i
= 0; i
< to_nchars
; i
++)
6697 c
= XINT (AREF (trans
, i
));
6698 if (coding
->dst_multibyte
6699 || ! CHAR_BYTE8_P (c
))
6700 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6702 *dst
++ = CHAR_TO_BYTE8 (c
);
6704 produced_chars
+= to_nchars
;
6708 /* This is an annotation datum. (-C) is the length. */
6711 carryover
= buf_end
- buf
;
6715 /* Source characters are at coding->source. */
6716 const unsigned char *src
= coding
->source
;
6717 const unsigned char *src_end
= src
+ coding
->consumed
;
6719 if (EQ (coding
->dst_object
, coding
->src_object
))
6720 dst_end
= (unsigned char *) src
;
6721 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6723 if (coding
->src_multibyte
)
6726 EMACS_INT consumed_chars
= 0;
6730 const unsigned char *src_base
= src
;
6736 if (EQ (coding
->src_object
, coding
->dst_object
))
6737 dst_end
= (unsigned char *) src
;
6740 EMACS_INT offset
= src
- coding
->source
;
6742 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6744 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6745 coding_set_source (coding
);
6746 src
= coding
->source
+ offset
;
6747 src_end
= coding
->source
+ coding
->src_bytes
;
6748 if (EQ (coding
->src_object
, coding
->dst_object
))
6749 dst_end
= (unsigned char *) src
;
6759 while (src
< src_end
)
6764 if (dst
>= dst_end
- 1)
6766 if (EQ (coding
->src_object
, coding
->dst_object
))
6767 dst_end
= (unsigned char *) src
;
6768 if (dst
>= dst_end
- 1)
6770 EMACS_INT offset
= src
- coding
->source
;
6771 EMACS_INT more_bytes
;
6773 if (EQ (coding
->src_object
, coding
->dst_object
))
6774 more_bytes
= ((src_end
- src
) / 2) + 2;
6776 more_bytes
= src_end
- src
+ 2;
6777 dst
= alloc_destination (coding
, more_bytes
, dst
);
6778 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6779 coding_set_source (coding
);
6780 src
= coding
->source
+ offset
;
6781 src_end
= coding
->source
+ coding
->src_bytes
;
6782 if (EQ (coding
->src_object
, coding
->dst_object
))
6783 dst_end
= (unsigned char *) src
;
6791 if (!EQ (coding
->src_object
, coding
->dst_object
))
6793 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6797 EMACS_INT offset
= src
- coding
->source
;
6799 dst
= alloc_destination (coding
, require
, dst
);
6800 coding_set_source (coding
);
6801 src
= coding
->source
+ offset
;
6802 src_end
= coding
->source
+ coding
->src_bytes
;
6805 produced_chars
= coding
->consumed_char
;
6806 while (src
< src_end
)
6811 produced
= dst
- (coding
->destination
+ coding
->produced
);
6812 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6813 insert_from_gap (produced_chars
, produced
);
6814 coding
->produced
+= produced
;
6815 coding
->produced_char
+= produced_chars
;
6819 /* Compose text in CODING->object according to the annotation data at
6820 CHARBUF. CHARBUF is an array:
6821 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6825 produce_composition (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6829 enum composition_method method
;
6830 Lisp_Object components
;
6832 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6833 to
= pos
+ charbuf
[2];
6834 method
= (enum composition_method
) (charbuf
[4]);
6836 if (method
== COMPOSITION_RELATIVE
)
6840 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6843 if (method
== COMPOSITION_WITH_RULE
)
6844 len
= charbuf
[2] * 3 - 2;
6845 charbuf
+= MAX_ANNOTATION_LENGTH
;
6846 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6847 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6849 if (charbuf
[i
] >= 0)
6850 args
[j
] = make_number (charbuf
[i
]);
6854 args
[j
] = make_number (charbuf
[i
] % 0x100);
6857 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6859 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6863 /* Put `charset' property on text in CODING->object according to
6864 the annotation data at CHARBUF. CHARBUF is an array:
6865 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6869 produce_charset (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6871 EMACS_INT from
= pos
- charbuf
[2];
6872 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6874 Fput_text_property (make_number (from
), make_number (pos
),
6875 Qcharset
, CHARSET_NAME (charset
),
6876 coding
->dst_object
);
6880 #define CHARBUF_SIZE 0x4000
6882 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6884 int size = CHARBUF_SIZE; \
6886 coding->charbuf = NULL; \
6887 while (size > 1024) \
6889 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6890 if (coding->charbuf) \
6894 if (! coding->charbuf) \
6896 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6897 return coding->result; \
6899 coding->charbuf_size = size; \
6904 produce_annotation (struct coding_system
*coding
, EMACS_INT pos
)
6906 int *charbuf
= coding
->charbuf
;
6907 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6909 if (NILP (coding
->dst_object
))
6912 while (charbuf
< charbuf_end
)
6918 int len
= -*charbuf
;
6923 case CODING_ANNOTATE_COMPOSITION_MASK
:
6924 produce_composition (coding
, charbuf
, pos
);
6926 case CODING_ANNOTATE_CHARSET_MASK
:
6927 produce_charset (coding
, charbuf
, pos
);
6935 /* Decode the data at CODING->src_object into CODING->dst_object.
6936 CODING->src_object is a buffer, a string, or nil.
6937 CODING->dst_object is a buffer.
6939 If CODING->src_object is a buffer, it must be the current buffer.
6940 In this case, if CODING->src_pos is positive, it is a position of
6941 the source text in the buffer, otherwise, the source text is in the
6942 gap area of the buffer, and CODING->src_pos specifies the offset of
6943 the text from GPT (which must be the same as PT). If this is the
6944 same buffer as CODING->dst_object, CODING->src_pos must be
6947 If CODING->src_object is a string, CODING->src_pos is an index to
6950 If CODING->src_object is nil, CODING->source must already point to
6951 the non-relocatable memory area. In this case, CODING->src_pos is
6952 an offset from CODING->source.
6954 The decoded data is inserted at the current point of the buffer
6959 decode_coding (struct coding_system
*coding
)
6962 Lisp_Object undo_list
;
6963 Lisp_Object translation_table
;
6964 struct ccl_spec cclspec
;
6968 if (BUFFERP (coding
->src_object
)
6969 && coding
->src_pos
> 0
6970 && coding
->src_pos
< GPT
6971 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6972 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6975 if (BUFFERP (coding
->dst_object
))
6977 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6978 set_buffer_internal (XBUFFER (coding
->dst_object
));
6980 move_gap_both (PT
, PT_BYTE
);
6981 undo_list
= BVAR (current_buffer
, undo_list
);
6982 BVAR (current_buffer
, undo_list
) = Qt
;
6985 coding
->consumed
= coding
->consumed_char
= 0;
6986 coding
->produced
= coding
->produced_char
= 0;
6987 coding
->chars_at_source
= 0;
6988 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6991 ALLOC_CONVERSION_WORK_AREA (coding
);
6993 attrs
= CODING_ID_ATTRS (coding
->id
);
6994 translation_table
= get_translation_table (attrs
, 0, NULL
);
6997 if (coding
->decoder
== decode_coding_ccl
)
6999 coding
->spec
.ccl
= &cclspec
;
7000 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7004 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7006 coding_set_source (coding
);
7007 coding
->annotated
= 0;
7008 coding
->charbuf_used
= carryover
;
7009 (*(coding
->decoder
)) (coding
);
7010 coding_set_destination (coding
);
7011 carryover
= produce_chars (coding
, translation_table
, 0);
7012 if (coding
->annotated
)
7013 produce_annotation (coding
, pos
);
7014 for (i
= 0; i
< carryover
; i
++)
7016 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7018 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7019 || (coding
->consumed
< coding
->src_bytes
7020 && (coding
->result
== CODING_RESULT_SUCCESS
7021 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7025 coding_set_destination (coding
);
7026 coding
->charbuf_used
= carryover
;
7027 produce_chars (coding
, translation_table
, 1);
7030 coding
->carryover_bytes
= 0;
7031 if (coding
->consumed
< coding
->src_bytes
)
7033 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7034 const unsigned char *src
;
7036 coding_set_source (coding
);
7037 coding_set_destination (coding
);
7038 src
= coding
->source
+ coding
->consumed
;
7040 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7042 /* Flush out unprocessed data as binary chars. We are sure
7043 that the number of data is less than the size of
7045 coding
->charbuf_used
= 0;
7046 coding
->chars_at_source
= 0;
7048 while (nbytes
-- > 0)
7053 c
= BYTE8_TO_CHAR (c
);
7054 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7056 produce_chars (coding
, Qnil
, 1);
7060 /* Record unprocessed bytes in coding->carryover. We are
7061 sure that the number of data is less than the size of
7062 coding->carryover. */
7063 unsigned char *p
= coding
->carryover
;
7065 if (nbytes
> sizeof coding
->carryover
)
7066 nbytes
= sizeof coding
->carryover
;
7067 coding
->carryover_bytes
= nbytes
;
7068 while (nbytes
-- > 0)
7071 coding
->consumed
= coding
->src_bytes
;
7074 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7075 && !inhibit_eol_conversion
)
7076 decode_eol (coding
);
7077 if (BUFFERP (coding
->dst_object
))
7079 BVAR (current_buffer
, undo_list
) = undo_list
;
7080 record_insert (coding
->dst_pos
, coding
->produced_char
);
7082 return coding
->result
;
7086 /* Extract an annotation datum from a composition starting at POS and
7087 ending before LIMIT of CODING->src_object (buffer or string), store
7088 the data in BUF, set *STOP to a starting position of the next
7089 composition (if any) or to LIMIT, and return the address of the
7090 next element of BUF.
7092 If such an annotation is not found, set *STOP to a starting
7093 position of a composition after POS (if any) or to LIMIT, and
7097 handle_composition_annotation (EMACS_INT pos
, EMACS_INT limit
,
7098 struct coding_system
*coding
, int *buf
,
7101 EMACS_INT start
, end
;
7104 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7107 else if (start
> pos
)
7113 /* We found a composition. Store the corresponding
7114 annotation data in BUF. */
7116 enum composition_method method
= COMPOSITION_METHOD (prop
);
7117 int nchars
= COMPOSITION_LENGTH (prop
);
7119 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7120 if (method
!= COMPOSITION_RELATIVE
)
7122 Lisp_Object components
;
7125 components
= COMPOSITION_COMPONENTS (prop
);
7126 if (VECTORP (components
))
7128 len
= XVECTOR (components
)->size
;
7129 for (i
= 0; i
< len
; i
++)
7130 *buf
++ = XINT (AREF (components
, i
));
7132 else if (STRINGP (components
))
7134 len
= SCHARS (components
);
7138 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7142 else if (INTEGERP (components
))
7145 *buf
++ = XINT (components
);
7147 else if (CONSP (components
))
7149 for (len
= 0; CONSP (components
);
7150 len
++, components
= XCDR (components
))
7151 *buf
++ = XINT (XCAR (components
));
7159 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7170 /* Extract an annotation datum from a text property `charset' at POS of
7171 CODING->src_object (buffer of string), store the data in BUF, set
7172 *STOP to the position where the value of `charset' property changes
7173 (limiting by LIMIT), and return the address of the next element of
7176 If the property value is nil, set *STOP to the position where the
7177 property value is non-nil (limiting by LIMIT), and return BUF. */
7180 handle_charset_annotation (EMACS_INT pos
, EMACS_INT limit
,
7181 struct coding_system
*coding
, int *buf
,
7184 Lisp_Object val
, next
;
7187 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7188 if (! NILP (val
) && CHARSETP (val
))
7189 id
= XINT (CHARSET_SYMBOL_ID (val
));
7192 ADD_CHARSET_DATA (buf
, 0, id
);
7193 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7195 make_number (limit
));
7196 *stop
= XINT (next
);
7202 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7205 int *buf
= coding
->charbuf
;
7206 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7207 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7208 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7209 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7210 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7211 int multibytep
= coding
->src_multibyte
;
7212 Lisp_Object eol_type
;
7214 EMACS_INT stop
, stop_composition
, stop_charset
;
7215 int *lookup_buf
= NULL
;
7217 if (! NILP (translation_table
))
7218 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7220 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7221 if (VECTORP (eol_type
))
7224 /* Note: composition handling is not yet implemented. */
7225 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7227 if (NILP (coding
->src_object
))
7228 stop
= stop_composition
= stop_charset
= end_pos
;
7231 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7232 stop
= stop_composition
= pos
;
7234 stop
= stop_composition
= end_pos
;
7235 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7236 stop
= stop_charset
= pos
;
7238 stop_charset
= end_pos
;
7241 /* Compensate for CRLF and conversion. */
7242 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7243 while (buf
< buf_end
)
7251 if (pos
== stop_composition
)
7252 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7253 buf
, &stop_composition
);
7254 if (pos
== stop_charset
)
7255 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7256 buf
, &stop_charset
);
7257 stop
= (stop_composition
< stop_charset
7258 ? stop_composition
: stop_charset
);
7265 if (coding
->encoder
== encode_coding_raw_text
7266 || coding
->encoder
== encode_coding_ccl
)
7268 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7269 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7271 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7274 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7275 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7277 if (! EQ (eol_type
, Qunix
))
7281 if (EQ (eol_type
, Qdos
))
7289 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7294 int from_nchars
= 1, to_nchars
= 1;
7295 int *lookup_buf_end
;
7296 const unsigned char *p
= src
;
7300 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7301 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7302 lookup_buf_end
= lookup_buf
+ i
;
7303 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7304 if (INTEGERP (trans
))
7306 else if (CONSP (trans
))
7308 from_nchars
= ASIZE (XCAR (trans
));
7309 trans
= XCDR (trans
);
7310 if (INTEGERP (trans
))
7314 to_nchars
= ASIZE (trans
);
7315 if (buf
+ to_nchars
> buf_end
)
7317 c
= XINT (AREF (trans
, 0));
7323 for (i
= 1; i
< to_nchars
; i
++)
7324 *buf
++ = XINT (AREF (trans
, i
));
7325 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7326 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7330 coding
->consumed
= src
- coding
->source
;
7331 coding
->consumed_char
= pos
- coding
->src_pos
;
7332 coding
->charbuf_used
= buf
- coding
->charbuf
;
7333 coding
->chars_at_source
= 0;
7337 /* Encode the text at CODING->src_object into CODING->dst_object.
7338 CODING->src_object is a buffer or a string.
7339 CODING->dst_object is a buffer or nil.
7341 If CODING->src_object is a buffer, it must be the current buffer.
7342 In this case, if CODING->src_pos is positive, it is a position of
7343 the source text in the buffer, otherwise. the source text is in the
7344 gap area of the buffer, and coding->src_pos specifies the offset of
7345 the text from GPT (which must be the same as PT). If this is the
7346 same buffer as CODING->dst_object, CODING->src_pos must be
7347 negative and CODING should not have `pre-write-conversion'.
7349 If CODING->src_object is a string, CODING should not have
7350 `pre-write-conversion'.
7352 If CODING->dst_object is a buffer, the encoded data is inserted at
7353 the current point of that buffer.
7355 If CODING->dst_object is nil, the encoded data is placed at the
7356 memory area specified by CODING->destination. */
7359 encode_coding (struct coding_system
*coding
)
7362 Lisp_Object translation_table
;
7364 struct ccl_spec cclspec
;
7366 attrs
= CODING_ID_ATTRS (coding
->id
);
7367 if (coding
->encoder
== encode_coding_raw_text
)
7368 translation_table
= Qnil
, max_lookup
= 0;
7370 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7372 if (BUFFERP (coding
->dst_object
))
7374 set_buffer_internal (XBUFFER (coding
->dst_object
));
7375 coding
->dst_multibyte
7376 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7379 coding
->consumed
= coding
->consumed_char
= 0;
7380 coding
->produced
= coding
->produced_char
= 0;
7381 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7384 ALLOC_CONVERSION_WORK_AREA (coding
);
7386 if (coding
->encoder
== encode_coding_ccl
)
7388 coding
->spec
.ccl
= &cclspec
;
7389 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7392 coding_set_source (coding
);
7393 consume_chars (coding
, translation_table
, max_lookup
);
7394 coding_set_destination (coding
);
7395 (*(coding
->encoder
)) (coding
);
7396 } while (coding
->consumed_char
< coding
->src_chars
);
7398 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7399 insert_from_gap (coding
->produced_char
, coding
->produced
);
7401 return (coding
->result
);
7405 /* Name (or base name) of work buffer for code conversion. */
7406 static Lisp_Object Vcode_conversion_workbuf_name
;
7408 /* A working buffer used by the top level conversion. Once it is
7409 created, it is never destroyed. It has the name
7410 Vcode_conversion_workbuf_name. The other working buffers are
7411 destroyed after the use is finished, and their names are modified
7412 versions of Vcode_conversion_workbuf_name. */
7413 static Lisp_Object Vcode_conversion_reused_workbuf
;
7415 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7416 static int reused_workbuf_in_use
;
7419 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7420 multibyteness of returning buffer. */
7423 make_conversion_work_buffer (int multibyte
)
7425 Lisp_Object name
, workbuf
;
7426 struct buffer
*current
;
7428 if (reused_workbuf_in_use
++)
7430 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7431 workbuf
= Fget_buffer_create (name
);
7435 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7436 Vcode_conversion_reused_workbuf
7437 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7438 workbuf
= Vcode_conversion_reused_workbuf
;
7440 current
= current_buffer
;
7441 set_buffer_internal (XBUFFER (workbuf
));
7442 /* We can't allow modification hooks to run in the work buffer. For
7443 instance, directory_files_internal assumes that file decoding
7444 doesn't compile new regexps. */
7445 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7447 BVAR (current_buffer
, undo_list
) = Qt
;
7448 BVAR (current_buffer
, enable_multibyte_characters
) = multibyte
? Qt
: Qnil
;
7449 set_buffer_internal (current
);
7455 code_conversion_restore (Lisp_Object arg
)
7457 Lisp_Object current
, workbuf
;
7458 struct gcpro gcpro1
;
7461 current
= XCAR (arg
);
7462 workbuf
= XCDR (arg
);
7463 if (! NILP (workbuf
))
7465 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7466 reused_workbuf_in_use
= 0;
7467 else if (! NILP (Fbuffer_live_p (workbuf
)))
7468 Fkill_buffer (workbuf
);
7470 set_buffer_internal (XBUFFER (current
));
7476 code_conversion_save (int with_work_buf
, int multibyte
)
7478 Lisp_Object workbuf
= Qnil
;
7481 workbuf
= make_conversion_work_buffer (multibyte
);
7482 record_unwind_protect (code_conversion_restore
,
7483 Fcons (Fcurrent_buffer (), workbuf
));
7488 decode_coding_gap (struct coding_system
*coding
,
7489 EMACS_INT chars
, EMACS_INT bytes
)
7491 int count
= SPECPDL_INDEX ();
7494 code_conversion_save (0, 0);
7496 coding
->src_object
= Fcurrent_buffer ();
7497 coding
->src_chars
= chars
;
7498 coding
->src_bytes
= bytes
;
7499 coding
->src_pos
= -chars
;
7500 coding
->src_pos_byte
= -bytes
;
7501 coding
->src_multibyte
= chars
< bytes
;
7502 coding
->dst_object
= coding
->src_object
;
7503 coding
->dst_pos
= PT
;
7504 coding
->dst_pos_byte
= PT_BYTE
;
7505 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7507 if (CODING_REQUIRE_DETECTION (coding
))
7508 detect_coding (coding
);
7510 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7511 current_buffer
->text
->inhibit_shrinking
= 1;
7512 decode_coding (coding
);
7513 current_buffer
->text
->inhibit_shrinking
= 0;
7515 attrs
= CODING_ID_ATTRS (coding
->id
);
7516 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7518 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7521 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7522 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7523 make_number (coding
->produced_char
));
7525 coding
->produced_char
+= Z
- prev_Z
;
7526 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7529 unbind_to (count
, Qnil
);
7530 return coding
->result
;
7534 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7535 SRC_OBJECT into DST_OBJECT by coding context CODING.
7537 SRC_OBJECT is a buffer, a string, or Qnil.
7539 If it is a buffer, the text is at point of the buffer. FROM and TO
7540 are positions in the buffer.
7542 If it is a string, the text is at the beginning of the string.
7543 FROM and TO are indices to the string.
7545 If it is nil, the text is at coding->source. FROM and TO are
7546 indices to coding->source.
7548 DST_OBJECT is a buffer, Qt, or Qnil.
7550 If it is a buffer, the decoded text is inserted at point of the
7551 buffer. If the buffer is the same as SRC_OBJECT, the source text
7554 If it is Qt, a string is made from the decoded text, and
7555 set in CODING->dst_object.
7557 If it is Qnil, the decoded text is stored at CODING->destination.
7558 The caller must allocate CODING->dst_bytes bytes at
7559 CODING->destination by xmalloc. If the decoded text is longer than
7560 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7564 decode_coding_object (struct coding_system
*coding
,
7565 Lisp_Object src_object
,
7566 EMACS_INT from
, EMACS_INT from_byte
,
7567 EMACS_INT to
, EMACS_INT to_byte
,
7568 Lisp_Object dst_object
)
7570 int count
= SPECPDL_INDEX ();
7571 unsigned char *destination
IF_LINT (= NULL
);
7572 EMACS_INT dst_bytes
IF_LINT (= 0);
7573 EMACS_INT chars
= to
- from
;
7574 EMACS_INT bytes
= to_byte
- from_byte
;
7576 int saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7577 int need_marker_adjustment
= 0;
7578 Lisp_Object old_deactivate_mark
;
7580 old_deactivate_mark
= Vdeactivate_mark
;
7582 if (NILP (dst_object
))
7584 destination
= coding
->destination
;
7585 dst_bytes
= coding
->dst_bytes
;
7588 coding
->src_object
= src_object
;
7589 coding
->src_chars
= chars
;
7590 coding
->src_bytes
= bytes
;
7591 coding
->src_multibyte
= chars
< bytes
;
7593 if (STRINGP (src_object
))
7595 coding
->src_pos
= from
;
7596 coding
->src_pos_byte
= from_byte
;
7598 else if (BUFFERP (src_object
))
7600 set_buffer_internal (XBUFFER (src_object
));
7602 move_gap_both (from
, from_byte
);
7603 if (EQ (src_object
, dst_object
))
7605 struct Lisp_Marker
*tail
;
7607 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7609 tail
->need_adjustment
7610 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7611 need_marker_adjustment
|= tail
->need_adjustment
;
7613 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7614 TEMP_SET_PT_BOTH (from
, from_byte
);
7615 current_buffer
->text
->inhibit_shrinking
= 1;
7616 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7617 coding
->src_pos
= -chars
;
7618 coding
->src_pos_byte
= -bytes
;
7622 coding
->src_pos
= from
;
7623 coding
->src_pos_byte
= from_byte
;
7627 if (CODING_REQUIRE_DETECTION (coding
))
7628 detect_coding (coding
);
7629 attrs
= CODING_ID_ATTRS (coding
->id
);
7631 if (EQ (dst_object
, Qt
)
7632 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7633 && NILP (dst_object
)))
7635 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7636 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7637 coding
->dst_pos
= BEG
;
7638 coding
->dst_pos_byte
= BEG_BYTE
;
7640 else if (BUFFERP (dst_object
))
7642 code_conversion_save (0, 0);
7643 coding
->dst_object
= dst_object
;
7644 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7645 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7646 coding
->dst_multibyte
7647 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7651 code_conversion_save (0, 0);
7652 coding
->dst_object
= Qnil
;
7653 /* Most callers presume this will return a multibyte result, and they
7654 won't use `binary' or `raw-text' anyway, so let's not worry about
7655 CODING_FOR_UNIBYTE. */
7656 coding
->dst_multibyte
= 1;
7659 decode_coding (coding
);
7661 if (BUFFERP (coding
->dst_object
))
7662 set_buffer_internal (XBUFFER (coding
->dst_object
));
7664 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7666 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7667 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7670 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7671 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7672 old_deactivate_mark
);
7673 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7674 make_number (coding
->produced_char
));
7677 coding
->produced_char
+= Z
- prev_Z
;
7678 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7681 if (EQ (dst_object
, Qt
))
7683 coding
->dst_object
= Fbuffer_string ();
7685 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7687 set_buffer_internal (XBUFFER (coding
->dst_object
));
7688 if (dst_bytes
< coding
->produced
)
7690 destination
= xrealloc (destination
, coding
->produced
);
7693 record_conversion_result (coding
,
7694 CODING_RESULT_INSUFFICIENT_MEM
);
7695 unbind_to (count
, Qnil
);
7698 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7699 move_gap_both (BEGV
, BEGV_BYTE
);
7700 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7701 coding
->destination
= destination
;
7707 /* This is the case of:
7708 (BUFFERP (src_object) && EQ (src_object, dst_object))
7709 As we have moved PT while replacing the original buffer
7710 contents, we must recover it now. */
7711 set_buffer_internal (XBUFFER (src_object
));
7712 current_buffer
->text
->inhibit_shrinking
= 0;
7713 if (saved_pt
< from
)
7714 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7715 else if (saved_pt
< from
+ chars
)
7716 TEMP_SET_PT_BOTH (from
, from_byte
);
7717 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7718 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7719 saved_pt_byte
+ (coding
->produced
- bytes
));
7721 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7722 saved_pt_byte
+ (coding
->produced
- bytes
));
7724 if (need_marker_adjustment
)
7726 struct Lisp_Marker
*tail
;
7728 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7729 if (tail
->need_adjustment
)
7731 tail
->need_adjustment
= 0;
7732 if (tail
->insertion_type
)
7734 tail
->bytepos
= from_byte
;
7735 tail
->charpos
= from
;
7739 tail
->bytepos
= from_byte
+ coding
->produced
;
7741 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7742 ? tail
->bytepos
: from
+ coding
->produced_char
);
7748 Vdeactivate_mark
= old_deactivate_mark
;
7749 unbind_to (count
, coding
->dst_object
);
7754 encode_coding_object (struct coding_system
*coding
,
7755 Lisp_Object src_object
,
7756 EMACS_INT from
, EMACS_INT from_byte
,
7757 EMACS_INT to
, EMACS_INT to_byte
,
7758 Lisp_Object dst_object
)
7760 int count
= SPECPDL_INDEX ();
7761 EMACS_INT chars
= to
- from
;
7762 EMACS_INT bytes
= to_byte
- from_byte
;
7764 int saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7765 int need_marker_adjustment
= 0;
7766 int kill_src_buffer
= 0;
7767 Lisp_Object old_deactivate_mark
;
7769 old_deactivate_mark
= Vdeactivate_mark
;
7771 coding
->src_object
= src_object
;
7772 coding
->src_chars
= chars
;
7773 coding
->src_bytes
= bytes
;
7774 coding
->src_multibyte
= chars
< bytes
;
7776 attrs
= CODING_ID_ATTRS (coding
->id
);
7778 if (EQ (src_object
, dst_object
))
7780 struct Lisp_Marker
*tail
;
7782 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7784 tail
->need_adjustment
7785 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7786 need_marker_adjustment
|= tail
->need_adjustment
;
7790 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7792 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7793 set_buffer_internal (XBUFFER (coding
->src_object
));
7794 if (STRINGP (src_object
))
7795 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7796 else if (BUFFERP (src_object
))
7797 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7799 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7801 if (EQ (src_object
, dst_object
))
7803 set_buffer_internal (XBUFFER (src_object
));
7804 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7805 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7806 set_buffer_internal (XBUFFER (coding
->src_object
));
7810 Lisp_Object args
[3];
7811 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7813 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7814 old_deactivate_mark
);
7815 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7816 args
[1] = make_number (BEG
);
7817 args
[2] = make_number (Z
);
7818 safe_call (3, args
);
7821 if (XBUFFER (coding
->src_object
) != current_buffer
)
7822 kill_src_buffer
= 1;
7823 coding
->src_object
= Fcurrent_buffer ();
7825 move_gap_both (BEG
, BEG_BYTE
);
7826 coding
->src_chars
= Z
- BEG
;
7827 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7828 coding
->src_pos
= BEG
;
7829 coding
->src_pos_byte
= BEG_BYTE
;
7830 coding
->src_multibyte
= Z
< Z_BYTE
;
7832 else if (STRINGP (src_object
))
7834 code_conversion_save (0, 0);
7835 coding
->src_pos
= from
;
7836 coding
->src_pos_byte
= from_byte
;
7838 else if (BUFFERP (src_object
))
7840 code_conversion_save (0, 0);
7841 set_buffer_internal (XBUFFER (src_object
));
7842 if (EQ (src_object
, dst_object
))
7844 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7845 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7846 coding
->src_pos
= 0;
7847 coding
->src_pos_byte
= 0;
7851 if (from
< GPT
&& to
>= GPT
)
7852 move_gap_both (from
, from_byte
);
7853 coding
->src_pos
= from
;
7854 coding
->src_pos_byte
= from_byte
;
7858 code_conversion_save (0, 0);
7860 if (BUFFERP (dst_object
))
7862 coding
->dst_object
= dst_object
;
7863 if (EQ (src_object
, dst_object
))
7865 coding
->dst_pos
= from
;
7866 coding
->dst_pos_byte
= from_byte
;
7870 struct buffer
*current
= current_buffer
;
7872 set_buffer_temp (XBUFFER (dst_object
));
7873 coding
->dst_pos
= PT
;
7874 coding
->dst_pos_byte
= PT_BYTE
;
7875 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7876 set_buffer_temp (current
);
7878 coding
->dst_multibyte
7879 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7881 else if (EQ (dst_object
, Qt
))
7883 coding
->dst_object
= Qnil
;
7884 coding
->dst_bytes
= coding
->src_chars
;
7885 if (coding
->dst_bytes
== 0)
7886 coding
->dst_bytes
= 1;
7887 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7888 coding
->dst_multibyte
= 0;
7892 coding
->dst_object
= Qnil
;
7893 coding
->dst_multibyte
= 0;
7896 encode_coding (coding
);
7898 if (EQ (dst_object
, Qt
))
7900 if (BUFFERP (coding
->dst_object
))
7901 coding
->dst_object
= Fbuffer_string ();
7905 = make_unibyte_string ((char *) coding
->destination
,
7907 xfree (coding
->destination
);
7913 /* This is the case of:
7914 (BUFFERP (src_object) && EQ (src_object, dst_object))
7915 As we have moved PT while replacing the original buffer
7916 contents, we must recover it now. */
7917 set_buffer_internal (XBUFFER (src_object
));
7918 if (saved_pt
< from
)
7919 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7920 else if (saved_pt
< from
+ chars
)
7921 TEMP_SET_PT_BOTH (from
, from_byte
);
7922 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7923 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7924 saved_pt_byte
+ (coding
->produced
- bytes
));
7926 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7927 saved_pt_byte
+ (coding
->produced
- bytes
));
7929 if (need_marker_adjustment
)
7931 struct Lisp_Marker
*tail
;
7933 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7934 if (tail
->need_adjustment
)
7936 tail
->need_adjustment
= 0;
7937 if (tail
->insertion_type
)
7939 tail
->bytepos
= from_byte
;
7940 tail
->charpos
= from
;
7944 tail
->bytepos
= from_byte
+ coding
->produced
;
7946 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7947 ? tail
->bytepos
: from
+ coding
->produced_char
);
7953 if (kill_src_buffer
)
7954 Fkill_buffer (coding
->src_object
);
7956 Vdeactivate_mark
= old_deactivate_mark
;
7957 unbind_to (count
, Qnil
);
7962 preferred_coding_system (void)
7964 int id
= coding_categories
[coding_priorities
[0]].id
;
7966 return CODING_ID_NAME (id
);
7971 /*** 8. Emacs Lisp library functions ***/
7973 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7974 doc
: /* Return t if OBJECT is nil or a coding-system.
7975 See the documentation of `define-coding-system' for information
7976 about coding-system objects. */)
7977 (Lisp_Object object
)
7980 || CODING_SYSTEM_ID (object
) >= 0)
7982 if (! SYMBOLP (object
)
7983 || NILP (Fget (object
, Qcoding_system_define_form
)))
7988 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7989 Sread_non_nil_coding_system
, 1, 1, 0,
7990 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7991 (Lisp_Object prompt
)
7996 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7997 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7999 while (SCHARS (val
) == 0);
8000 return (Fintern (val
, Qnil
));
8003 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8004 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8005 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8006 Ignores case when completing coding systems (all Emacs coding systems
8007 are lower-case). */)
8008 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8011 int count
= SPECPDL_INDEX ();
8013 if (SYMBOLP (default_coding_system
))
8014 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8015 specbind (Qcompletion_ignore_case
, Qt
);
8016 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8017 Qt
, Qnil
, Qcoding_system_history
,
8018 default_coding_system
, Qnil
);
8019 unbind_to (count
, Qnil
);
8020 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8023 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8025 doc
: /* Check validity of CODING-SYSTEM.
8026 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8027 It is valid if it is nil or a symbol defined as a coding system by the
8028 function `define-coding-system'. */)
8029 (Lisp_Object coding_system
)
8031 Lisp_Object define_form
;
8033 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8034 if (! NILP (define_form
))
8036 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8037 safe_eval (define_form
);
8039 if (!NILP (Fcoding_system_p (coding_system
)))
8040 return coding_system
;
8041 xsignal1 (Qcoding_system_error
, coding_system
);
8045 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8046 HIGHEST is nonzero, return the coding system of the highest
8047 priority among the detected coding systems. Otherwise return a
8048 list of detected coding systems sorted by their priorities. If
8049 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8050 multibyte form but contains only ASCII and eight-bit chars.
8051 Otherwise, the bytes are raw bytes.
8053 CODING-SYSTEM controls the detection as below:
8055 If it is nil, detect both text-format and eol-format. If the
8056 text-format part of CODING-SYSTEM is already specified
8057 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8058 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8059 detect only text-format. */
8062 detect_coding_system (const unsigned char *src
,
8063 EMACS_INT src_chars
, EMACS_INT src_bytes
,
8064 int highest
, int multibytep
,
8065 Lisp_Object coding_system
)
8067 const unsigned char *src_end
= src
+ src_bytes
;
8068 Lisp_Object attrs
, eol_type
;
8069 Lisp_Object val
= Qnil
;
8070 struct coding_system coding
;
8072 struct coding_detection_info detect_info
;
8073 enum coding_category base_category
;
8074 int null_byte_found
= 0, eight_bit_found
= 0;
8076 if (NILP (coding_system
))
8077 coding_system
= Qundecided
;
8078 setup_coding_system (coding_system
, &coding
);
8079 attrs
= CODING_ID_ATTRS (coding
.id
);
8080 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8081 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8083 coding
.source
= src
;
8084 coding
.src_chars
= src_chars
;
8085 coding
.src_bytes
= src_bytes
;
8086 coding
.src_multibyte
= multibytep
;
8087 coding
.consumed
= 0;
8088 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8089 coding
.head_ascii
= 0;
8091 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8093 /* At first, detect text-format if necessary. */
8094 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8095 if (base_category
== coding_category_undecided
)
8097 enum coding_category category
IF_LINT (= 0);
8098 struct coding_system
*this IF_LINT (= NULL
);
8101 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8102 for (; src
< src_end
; src
++)
8107 eight_bit_found
= 1;
8108 if (null_byte_found
)
8113 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8114 && ! inhibit_iso_escape_detection
8115 && ! detect_info
.checked
)
8117 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8119 /* We have scanned the whole data. */
8120 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8122 /* We didn't find an 8-bit code. We may
8123 have found a null-byte, but it's very
8124 rare that a binary file confirm to
8127 coding
.head_ascii
= src
- coding
.source
;
8129 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8133 else if (! c
&& !inhibit_null_byte_detection
)
8135 null_byte_found
= 1;
8136 if (eight_bit_found
)
8139 if (! eight_bit_found
)
8140 coding
.head_ascii
++;
8142 else if (! eight_bit_found
)
8143 coding
.head_ascii
++;
8146 if (null_byte_found
|| eight_bit_found
8147 || coding
.head_ascii
< coding
.src_bytes
8148 || detect_info
.found
)
8150 if (coding
.head_ascii
== coding
.src_bytes
)
8151 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8152 for (i
= 0; i
< coding_category_raw_text
; i
++)
8154 category
= coding_priorities
[i
];
8155 this = coding_categories
+ category
;
8156 if (detect_info
.found
& (1 << category
))
8161 if (null_byte_found
)
8163 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8164 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8166 for (i
= 0; i
< coding_category_raw_text
; i
++)
8168 category
= coding_priorities
[i
];
8169 this = coding_categories
+ category
;
8173 /* No coding system of this category is defined. */
8174 detect_info
.rejected
|= (1 << category
);
8176 else if (category
>= coding_category_raw_text
)
8178 else if (detect_info
.checked
& (1 << category
))
8181 && (detect_info
.found
& (1 << category
)))
8184 else if ((*(this->detector
)) (&coding
, &detect_info
)
8186 && (detect_info
.found
& (1 << category
)))
8188 if (category
== coding_category_utf_16_auto
)
8190 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8191 category
= coding_category_utf_16_le
;
8193 category
= coding_category_utf_16_be
;
8201 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8204 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8205 id
= CODING_SYSTEM_ID (Qno_conversion
);
8206 val
= Fcons (make_number (id
), Qnil
);
8208 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8210 detect_info
.found
= CATEGORY_MASK_ANY
;
8211 id
= coding_categories
[coding_category_undecided
].id
;
8212 val
= Fcons (make_number (id
), Qnil
);
8216 if (detect_info
.found
)
8218 detect_info
.found
= 1 << category
;
8219 val
= Fcons (make_number (this->id
), Qnil
);
8222 for (i
= 0; i
< coding_category_raw_text
; i
++)
8223 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8225 detect_info
.found
= 1 << coding_priorities
[i
];
8226 id
= coding_categories
[coding_priorities
[i
]].id
;
8227 val
= Fcons (make_number (id
), Qnil
);
8233 int mask
= detect_info
.rejected
| detect_info
.found
;
8236 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8238 category
= coding_priorities
[i
];
8239 if (! (mask
& (1 << category
)))
8241 found
|= 1 << category
;
8242 id
= coding_categories
[category
].id
;
8244 val
= Fcons (make_number (id
), val
);
8247 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8249 category
= coding_priorities
[i
];
8250 if (detect_info
.found
& (1 << category
))
8252 id
= coding_categories
[category
].id
;
8253 val
= Fcons (make_number (id
), val
);
8256 detect_info
.found
|= found
;
8259 else if (base_category
== coding_category_utf_8_auto
)
8261 if (detect_coding_utf_8 (&coding
, &detect_info
))
8263 struct coding_system
*this;
8265 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8266 this = coding_categories
+ coding_category_utf_8_sig
;
8268 this = coding_categories
+ coding_category_utf_8_nosig
;
8269 val
= Fcons (make_number (this->id
), Qnil
);
8272 else if (base_category
== coding_category_utf_16_auto
)
8274 if (detect_coding_utf_16 (&coding
, &detect_info
))
8276 struct coding_system
*this;
8278 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8279 this = coding_categories
+ coding_category_utf_16_le
;
8280 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8281 this = coding_categories
+ coding_category_utf_16_be
;
8282 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8283 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8285 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8286 val
= Fcons (make_number (this->id
), Qnil
);
8291 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8292 val
= Fcons (make_number (coding
.id
), Qnil
);
8295 /* Then, detect eol-format if necessary. */
8297 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8300 if (VECTORP (eol_type
))
8302 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8304 if (null_byte_found
)
8305 normal_eol
= EOL_SEEN_LF
;
8307 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8308 coding_category_raw_text
);
8310 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8311 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8312 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8313 coding_category_utf_16_be
);
8314 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8315 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8316 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8317 coding_category_utf_16_le
);
8321 if (EQ (eol_type
, Qunix
))
8322 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8323 else if (EQ (eol_type
, Qdos
))
8324 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8326 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8329 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8331 enum coding_category category
;
8334 id
= XINT (XCAR (tail
));
8335 attrs
= CODING_ID_ATTRS (id
);
8336 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8337 eol_type
= CODING_ID_EOL_TYPE (id
);
8338 if (VECTORP (eol_type
))
8340 if (category
== coding_category_utf_16_be
8341 || category
== coding_category_utf_16_be_nosig
)
8342 this_eol
= utf_16_be_eol
;
8343 else if (category
== coding_category_utf_16_le
8344 || category
== coding_category_utf_16_le_nosig
)
8345 this_eol
= utf_16_le_eol
;
8347 this_eol
= normal_eol
;
8349 if (this_eol
== EOL_SEEN_LF
)
8350 XSETCAR (tail
, AREF (eol_type
, 0));
8351 else if (this_eol
== EOL_SEEN_CRLF
)
8352 XSETCAR (tail
, AREF (eol_type
, 1));
8353 else if (this_eol
== EOL_SEEN_CR
)
8354 XSETCAR (tail
, AREF (eol_type
, 2));
8356 XSETCAR (tail
, CODING_ID_NAME (id
));
8359 XSETCAR (tail
, CODING_ID_NAME (id
));
8363 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8367 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8369 doc
: /* Detect coding system of the text in the region between START and END.
8370 Return a list of possible coding systems ordered by priority.
8371 The coding systems to try and their priorities follows what
8372 the function `coding-system-priority-list' (which see) returns.
8374 If only ASCII characters are found (except for such ISO-2022 control
8375 characters as ESC), it returns a list of single element `undecided'
8376 or its subsidiary coding system according to a detected end-of-line
8379 If optional argument HIGHEST is non-nil, return the coding system of
8380 highest priority. */)
8381 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8384 int from_byte
, to_byte
;
8386 CHECK_NUMBER_COERCE_MARKER (start
);
8387 CHECK_NUMBER_COERCE_MARKER (end
);
8389 validate_region (&start
, &end
);
8390 from
= XINT (start
), to
= XINT (end
);
8391 from_byte
= CHAR_TO_BYTE (from
);
8392 to_byte
= CHAR_TO_BYTE (to
);
8394 if (from
< GPT
&& to
>= GPT
)
8395 move_gap_both (to
, to_byte
);
8397 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8398 to
- from
, to_byte
- from_byte
,
8400 !NILP (BVAR (current_buffer
8401 , enable_multibyte_characters
)),
8405 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8407 doc
: /* Detect coding system of the text in STRING.
8408 Return a list of possible coding systems ordered by priority.
8409 The coding systems to try and their priorities follows what
8410 the function `coding-system-priority-list' (which see) returns.
8412 If only ASCII characters are found (except for such ISO-2022 control
8413 characters as ESC), it returns a list of single element `undecided'
8414 or its subsidiary coding system according to a detected end-of-line
8417 If optional argument HIGHEST is non-nil, return the coding system of
8418 highest priority. */)
8419 (Lisp_Object string
, Lisp_Object highest
)
8421 CHECK_STRING (string
);
8423 return detect_coding_system (SDATA (string
),
8424 SCHARS (string
), SBYTES (string
),
8425 !NILP (highest
), STRING_MULTIBYTE (string
),
8431 char_encodable_p (int c
, Lisp_Object attrs
)
8434 struct charset
*charset
;
8435 Lisp_Object translation_table
;
8437 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8438 if (! NILP (translation_table
))
8439 c
= translate_char (translation_table
, c
);
8440 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8441 CONSP (tail
); tail
= XCDR (tail
))
8443 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8444 if (CHAR_CHARSET_P (c
, charset
))
8447 return (! NILP (tail
));
8451 /* Return a list of coding systems that safely encode the text between
8452 START and END. If EXCLUDE is non-nil, it is a list of coding
8453 systems not to check. The returned list doesn't contain any such
8454 coding systems. In any case, if the text contains only ASCII or is
8455 unibyte, return t. */
8457 DEFUN ("find-coding-systems-region-internal",
8458 Ffind_coding_systems_region_internal
,
8459 Sfind_coding_systems_region_internal
, 2, 3, 0,
8460 doc
: /* Internal use only. */)
8461 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8463 Lisp_Object coding_attrs_list
, safe_codings
;
8464 EMACS_INT start_byte
, end_byte
;
8465 const unsigned char *p
, *pbeg
, *pend
;
8467 Lisp_Object tail
, elt
, work_table
;
8469 if (STRINGP (start
))
8471 if (!STRING_MULTIBYTE (start
)
8472 || SCHARS (start
) == SBYTES (start
))
8475 end_byte
= SBYTES (start
);
8479 CHECK_NUMBER_COERCE_MARKER (start
);
8480 CHECK_NUMBER_COERCE_MARKER (end
);
8481 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8482 args_out_of_range (start
, end
);
8483 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8485 start_byte
= CHAR_TO_BYTE (XINT (start
));
8486 end_byte
= CHAR_TO_BYTE (XINT (end
));
8487 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8490 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8492 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8493 move_gap_both (XINT (start
), start_byte
);
8495 move_gap_both (XINT (end
), end_byte
);
8499 coding_attrs_list
= Qnil
;
8500 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8502 || NILP (Fmemq (XCAR (tail
), exclude
)))
8506 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8507 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8508 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8510 ASET (attrs
, coding_attr_trans_tbl
,
8511 get_translation_table (attrs
, 1, NULL
));
8512 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8516 if (STRINGP (start
))
8517 p
= pbeg
= SDATA (start
);
8519 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8520 pend
= p
+ (end_byte
- start_byte
);
8522 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8523 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8525 work_table
= Fmake_char_table (Qnil
, Qnil
);
8528 if (ASCII_BYTE_P (*p
))
8532 c
= STRING_CHAR_ADVANCE (p
);
8533 if (!NILP (char_table_ref (work_table
, c
)))
8534 /* This character was already checked. Ignore it. */
8537 charset_map_loaded
= 0;
8538 for (tail
= coding_attrs_list
; CONSP (tail
);)
8543 else if (char_encodable_p (c
, elt
))
8545 else if (CONSP (XCDR (tail
)))
8547 XSETCAR (tail
, XCAR (XCDR (tail
)));
8548 XSETCDR (tail
, XCDR (XCDR (tail
)));
8552 XSETCAR (tail
, Qnil
);
8556 if (charset_map_loaded
)
8558 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8560 if (STRINGP (start
))
8561 pbeg
= SDATA (start
);
8563 pbeg
= BYTE_POS_ADDR (start_byte
);
8564 p
= pbeg
+ p_offset
;
8565 pend
= pbeg
+ pend_offset
;
8567 char_table_set (work_table
, c
, Qt
);
8571 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8572 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8573 if (! NILP (XCAR (tail
)))
8574 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8576 return safe_codings
;
8580 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8581 Sunencodable_char_position
, 3, 5, 0,
8583 Return position of first un-encodable character in a region.
8584 START and END specify the region and CODING-SYSTEM specifies the
8585 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8587 If optional 4th argument COUNT is non-nil, it specifies at most how
8588 many un-encodable characters to search. In this case, the value is a
8591 If optional 5th argument STRING is non-nil, it is a string to search
8592 for un-encodable characters. In that case, START and END are indexes
8594 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8597 struct coding_system coding
;
8598 Lisp_Object attrs
, charset_list
, translation_table
;
8599 Lisp_Object positions
;
8601 const unsigned char *p
, *stop
, *pend
;
8602 int ascii_compatible
;
8604 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8605 attrs
= CODING_ID_ATTRS (coding
.id
);
8606 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8608 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8609 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8610 translation_table
= get_translation_table (attrs
, 1, NULL
);
8614 validate_region (&start
, &end
);
8615 from
= XINT (start
);
8617 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8618 || (ascii_compatible
8619 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8621 p
= CHAR_POS_ADDR (from
);
8622 pend
= CHAR_POS_ADDR (to
);
8623 if (from
< GPT
&& to
>= GPT
)
8630 CHECK_STRING (string
);
8631 CHECK_NATNUM (start
);
8633 from
= XINT (start
);
8636 || to
> SCHARS (string
))
8637 args_out_of_range_3 (string
, start
, end
);
8638 if (! STRING_MULTIBYTE (string
))
8640 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8641 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8642 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8650 CHECK_NATNUM (count
);
8659 if (ascii_compatible
)
8660 while (p
< stop
&& ASCII_BYTE_P (*p
))
8670 c
= STRING_CHAR_ADVANCE (p
);
8671 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8672 && ! char_charset (translate_char (translation_table
, c
),
8673 charset_list
, NULL
))
8675 positions
= Fcons (make_number (from
), positions
);
8684 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8688 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8689 Scheck_coding_systems_region
, 3, 3, 0,
8690 doc
: /* Check if the region is encodable by coding systems.
8692 START and END are buffer positions specifying the region.
8693 CODING-SYSTEM-LIST is a list of coding systems to check.
8695 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8696 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8697 whole region, POS0, POS1, ... are buffer positions where non-encodable
8698 characters are found.
8700 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8703 START may be a string. In that case, check if the string is
8704 encodable, and the value contains indices to the string instead of
8705 buffer positions. END is ignored.
8707 If the current buffer (or START if it is a string) is unibyte, the value
8709 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8712 EMACS_INT start_byte
, end_byte
;
8714 const unsigned char *p
, *pbeg
, *pend
;
8716 Lisp_Object tail
, elt
, attrs
;
8718 if (STRINGP (start
))
8720 if (!STRING_MULTIBYTE (start
)
8721 || SCHARS (start
) == SBYTES (start
))
8724 end_byte
= SBYTES (start
);
8729 CHECK_NUMBER_COERCE_MARKER (start
);
8730 CHECK_NUMBER_COERCE_MARKER (end
);
8731 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8732 args_out_of_range (start
, end
);
8733 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8735 start_byte
= CHAR_TO_BYTE (XINT (start
));
8736 end_byte
= CHAR_TO_BYTE (XINT (end
));
8737 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8740 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8742 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8743 move_gap_both (XINT (start
), start_byte
);
8745 move_gap_both (XINT (end
), end_byte
);
8751 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8754 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8755 ASET (attrs
, coding_attr_trans_tbl
,
8756 get_translation_table (attrs
, 1, NULL
));
8757 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8760 if (STRINGP (start
))
8761 p
= pbeg
= SDATA (start
);
8763 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8764 pend
= p
+ (end_byte
- start_byte
);
8766 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8767 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8771 if (ASCII_BYTE_P (*p
))
8775 c
= STRING_CHAR_ADVANCE (p
);
8777 charset_map_loaded
= 0;
8778 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8780 elt
= XCDR (XCAR (tail
));
8781 if (! char_encodable_p (c
, XCAR (elt
)))
8782 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8784 if (charset_map_loaded
)
8786 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8788 if (STRINGP (start
))
8789 pbeg
= SDATA (start
);
8791 pbeg
= BYTE_POS_ADDR (start_byte
);
8792 p
= pbeg
+ p_offset
;
8793 pend
= pbeg
+ pend_offset
;
8801 for (; CONSP (tail
); tail
= XCDR (tail
))
8804 if (CONSP (XCDR (XCDR (elt
))))
8805 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8814 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8815 Lisp_Object coding_system
, Lisp_Object dst_object
,
8816 int encodep
, int norecord
)
8818 struct coding_system coding
;
8819 EMACS_INT from
, from_byte
, to
, to_byte
;
8820 Lisp_Object src_object
;
8822 CHECK_NUMBER_COERCE_MARKER (start
);
8823 CHECK_NUMBER_COERCE_MARKER (end
);
8824 if (NILP (coding_system
))
8825 coding_system
= Qno_conversion
;
8827 CHECK_CODING_SYSTEM (coding_system
);
8828 src_object
= Fcurrent_buffer ();
8829 if (NILP (dst_object
))
8830 dst_object
= src_object
;
8831 else if (! EQ (dst_object
, Qt
))
8832 CHECK_BUFFER (dst_object
);
8834 validate_region (&start
, &end
);
8835 from
= XFASTINT (start
);
8836 from_byte
= CHAR_TO_BYTE (from
);
8837 to
= XFASTINT (end
);
8838 to_byte
= CHAR_TO_BYTE (to
);
8840 setup_coding_system (coding_system
, &coding
);
8841 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8844 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8847 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8850 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8852 return (BUFFERP (dst_object
)
8853 ? make_number (coding
.produced_char
)
8854 : coding
.dst_object
);
8858 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8859 3, 4, "r\nzCoding system: ",
8860 doc
: /* Decode the current region from the specified coding system.
8861 When called from a program, takes four arguments:
8862 START, END, CODING-SYSTEM, and DESTINATION.
8863 START and END are buffer positions.
8865 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8866 If nil, the region between START and END is replaced by the decoded text.
8867 If buffer, the decoded text is inserted in that buffer after point (point
8869 In those cases, the length of the decoded text is returned.
8870 If DESTINATION is t, the decoded text is returned.
8872 This function sets `last-coding-system-used' to the precise coding system
8873 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8874 not fully specified.) */)
8875 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8877 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8880 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8881 3, 4, "r\nzCoding system: ",
8882 doc
: /* Encode the current region by specified coding system.
8883 When called from a program, takes four arguments:
8884 START, END, CODING-SYSTEM and DESTINATION.
8885 START and END are buffer positions.
8887 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8888 If nil, the region between START and END is replace by the encoded text.
8889 If buffer, the encoded text is inserted in that buffer after point (point
8891 In those cases, the length of the encoded text is returned.
8892 If DESTINATION is t, the encoded text is returned.
8894 This function sets `last-coding-system-used' to the precise coding system
8895 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8896 not fully specified.) */)
8897 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8899 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8903 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
8904 Lisp_Object dst_object
, int encodep
, int nocopy
, int norecord
)
8906 struct coding_system coding
;
8907 EMACS_INT chars
, bytes
;
8909 CHECK_STRING (string
);
8910 if (NILP (coding_system
))
8913 Vlast_coding_system_used
= Qno_conversion
;
8914 if (NILP (dst_object
))
8915 return (nocopy
? Fcopy_sequence (string
) : string
);
8918 if (NILP (coding_system
))
8919 coding_system
= Qno_conversion
;
8921 CHECK_CODING_SYSTEM (coding_system
);
8922 if (NILP (dst_object
))
8924 else if (! EQ (dst_object
, Qt
))
8925 CHECK_BUFFER (dst_object
);
8927 setup_coding_system (coding_system
, &coding
);
8928 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8929 chars
= SCHARS (string
);
8930 bytes
= SBYTES (string
);
8932 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8934 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8936 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8938 return (BUFFERP (dst_object
)
8939 ? make_number (coding
.produced_char
)
8940 : coding
.dst_object
);
8944 /* Encode or decode STRING according to CODING_SYSTEM.
8945 Do not set Vlast_coding_system_used.
8947 This function is called only from macros DECODE_FILE and
8948 ENCODE_FILE, thus we ignore character composition. */
8951 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
8954 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8958 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8960 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8962 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8963 if the decoding operation is trivial.
8965 Optional fourth arg BUFFER non-nil means that the decoded text is
8966 inserted in that buffer after point (point does not move). In this
8967 case, the return value is the length of the decoded text.
8969 This function sets `last-coding-system-used' to the precise coding system
8970 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8971 not fully specified.) */)
8972 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
8974 return code_convert_string (string
, coding_system
, buffer
,
8975 0, ! NILP (nocopy
), 0);
8978 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8980 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8982 Optional third arg NOCOPY non-nil means it is OK to return STRING
8983 itself if the encoding operation is trivial.
8985 Optional fourth arg BUFFER non-nil means that the encoded text is
8986 inserted in that buffer after point (point does not move). In this
8987 case, the return value is the length of the encoded text.
8989 This function sets `last-coding-system-used' to the precise coding system
8990 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8991 not fully specified.) */)
8992 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
8994 return code_convert_string (string
, coding_system
, buffer
,
8995 1, ! NILP (nocopy
), 1);
8999 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9000 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9001 Return the corresponding character. */)
9004 Lisp_Object spec
, attrs
, val
;
9005 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9009 CHECK_NATNUM (code
);
9010 ch
= XFASTINT (code
);
9011 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9012 attrs
= AREF (spec
, 0);
9014 if (ASCII_BYTE_P (ch
)
9015 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9018 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9019 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9020 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9021 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9026 charset
= charset_roman
;
9028 else if (ch
>= 0xA0 && ch
< 0xDF)
9031 charset
= charset_kana
;
9035 EMACS_INT c1
= ch
>> 8;
9038 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9039 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9040 error ("Invalid code: %"pEd
, ch
);
9043 charset
= charset_kanji
;
9045 c
= DECODE_CHAR (charset
, c
);
9047 error ("Invalid code: %"pEd
, ch
);
9048 return make_number (c
);
9052 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9053 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9054 Return the corresponding code in SJIS. */)
9057 Lisp_Object spec
, attrs
, charset_list
;
9059 struct charset
*charset
;
9062 CHECK_CHARACTER (ch
);
9064 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9065 attrs
= AREF (spec
, 0);
9067 if (ASCII_CHAR_P (c
)
9068 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9071 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9072 charset
= char_charset (c
, charset_list
, &code
);
9073 if (code
== CHARSET_INVALID_CODE (charset
))
9074 error ("Can't encode by shift_jis encoding: %c", c
);
9077 return make_number (code
);
9080 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9081 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9082 Return the corresponding character. */)
9085 Lisp_Object spec
, attrs
, val
;
9086 struct charset
*charset_roman
, *charset_big5
, *charset
;
9090 CHECK_NATNUM (code
);
9091 ch
= XFASTINT (code
);
9092 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9093 attrs
= AREF (spec
, 0);
9095 if (ASCII_BYTE_P (ch
)
9096 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9099 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9100 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9101 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9106 charset
= charset_roman
;
9110 EMACS_INT b1
= ch
>> 8;
9112 if (b1
< 0xA1 || b1
> 0xFE
9113 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9114 error ("Invalid code: %"pEd
, ch
);
9116 charset
= charset_big5
;
9118 c
= DECODE_CHAR (charset
, c
);
9120 error ("Invalid code: %"pEd
, ch
);
9121 return make_number (c
);
9124 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9125 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9126 Return the corresponding character code in Big5. */)
9129 Lisp_Object spec
, attrs
, charset_list
;
9130 struct charset
*charset
;
9134 CHECK_CHARACTER (ch
);
9136 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9137 attrs
= AREF (spec
, 0);
9138 if (ASCII_CHAR_P (c
)
9139 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9142 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9143 charset
= char_charset (c
, charset_list
, &code
);
9144 if (code
== CHARSET_INVALID_CODE (charset
))
9145 error ("Can't encode by Big5 encoding: %c", c
);
9147 return make_number (code
);
9151 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9152 Sset_terminal_coding_system_internal
, 1, 2, 0,
9153 doc
: /* Internal use only. */)
9154 (Lisp_Object coding_system
, Lisp_Object terminal
)
9156 struct terminal
*term
= get_terminal (terminal
, 1);
9157 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9158 CHECK_SYMBOL (coding_system
);
9159 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9160 /* We had better not send unsafe characters to terminal. */
9161 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9162 /* Character composition should be disabled. */
9163 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9164 terminal_coding
->src_multibyte
= 1;
9165 terminal_coding
->dst_multibyte
= 0;
9166 if (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
)
9167 term
->charset_list
= coding_charset_list (terminal_coding
);
9169 term
->charset_list
= Fcons (make_number (charset_ascii
), Qnil
);
9173 DEFUN ("set-safe-terminal-coding-system-internal",
9174 Fset_safe_terminal_coding_system_internal
,
9175 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9176 doc
: /* Internal use only. */)
9177 (Lisp_Object coding_system
)
9179 CHECK_SYMBOL (coding_system
);
9180 setup_coding_system (Fcheck_coding_system (coding_system
),
9181 &safe_terminal_coding
);
9182 /* Character composition should be disabled. */
9183 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9184 safe_terminal_coding
.src_multibyte
= 1;
9185 safe_terminal_coding
.dst_multibyte
= 0;
9189 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9190 Sterminal_coding_system
, 0, 1, 0,
9191 doc
: /* Return coding system specified for terminal output on the given terminal.
9192 TERMINAL may be a terminal object, a frame, or nil for the selected
9193 frame's terminal device. */)
9194 (Lisp_Object terminal
)
9196 struct coding_system
*terminal_coding
9197 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9198 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9200 /* For backward compatibility, return nil if it is `undecided'. */
9201 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9204 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9205 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9206 doc
: /* Internal use only. */)
9207 (Lisp_Object coding_system
, Lisp_Object terminal
)
9209 struct terminal
*t
= get_terminal (terminal
, 1);
9210 CHECK_SYMBOL (coding_system
);
9211 if (NILP (coding_system
))
9212 coding_system
= Qno_conversion
;
9214 Fcheck_coding_system (coding_system
);
9215 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9216 /* Character composition should be disabled. */
9217 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9218 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9222 DEFUN ("keyboard-coding-system",
9223 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9224 doc
: /* Return coding system specified for decoding keyboard input. */)
9225 (Lisp_Object terminal
)
9227 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9228 (get_terminal (terminal
, 1))->id
);
9232 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9233 Sfind_operation_coding_system
, 1, MANY
, 0,
9234 doc
: /* Choose a coding system for an operation based on the target name.
9235 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9236 DECODING-SYSTEM is the coding system to use for decoding
9237 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9238 for encoding (in case OPERATION does encoding).
9240 The first argument OPERATION specifies an I/O primitive:
9241 For file I/O, `insert-file-contents' or `write-region'.
9242 For process I/O, `call-process', `call-process-region', or `start-process'.
9243 For network I/O, `open-network-stream'.
9245 The remaining arguments should be the same arguments that were passed
9246 to the primitive. Depending on which primitive, one of those arguments
9247 is selected as the TARGET. For example, if OPERATION does file I/O,
9248 whichever argument specifies the file name is TARGET.
9250 TARGET has a meaning which depends on OPERATION:
9251 For file I/O, TARGET is a file name (except for the special case below).
9252 For process I/O, TARGET is a process name.
9253 For network I/O, TARGET is a service name or a port number.
9255 This function looks up what is specified for TARGET in
9256 `file-coding-system-alist', `process-coding-system-alist',
9257 or `network-coding-system-alist' depending on OPERATION.
9258 They may specify a coding system, a cons of coding systems,
9259 or a function symbol to call.
9260 In the last case, we call the function with one argument,
9261 which is a list of all the arguments given to this function.
9262 If the function can't decide a coding system, it can return
9263 `undecided' so that the normal code-detection is performed.
9265 If OPERATION is `insert-file-contents', the argument corresponding to
9266 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9267 file name to look up, and BUFFER is a buffer that contains the file's
9268 contents (not yet decoded). If `file-coding-system-alist' specifies a
9269 function to call for FILENAME, that function should examine the
9270 contents of BUFFER instead of reading the file.
9272 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9273 (size_t nargs
, Lisp_Object
*args
)
9275 Lisp_Object operation
, target_idx
, target
, val
;
9276 register Lisp_Object chain
;
9279 error ("Too few arguments");
9280 operation
= args
[0];
9281 if (!SYMBOLP (operation
)
9282 || !NATNUMP (target_idx
= Fget (operation
, Qtarget_idx
)))
9283 error ("Invalid first argument");
9284 if (nargs
< 1 + XFASTINT (target_idx
))
9285 error ("Too few arguments for operation: %s",
9286 SDATA (SYMBOL_NAME (operation
)));
9287 target
= args
[XFASTINT (target_idx
) + 1];
9288 if (!(STRINGP (target
)
9289 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9290 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9291 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9292 error ("Invalid %"pEd
"th argument", XFASTINT (target_idx
) + 1);
9294 target
= XCAR (target
);
9296 chain
= ((EQ (operation
, Qinsert_file_contents
)
9297 || EQ (operation
, Qwrite_region
))
9298 ? Vfile_coding_system_alist
9299 : (EQ (operation
, Qopen_network_stream
)
9300 ? Vnetwork_coding_system_alist
9301 : Vprocess_coding_system_alist
));
9305 for (; CONSP (chain
); chain
= XCDR (chain
))
9311 && ((STRINGP (target
)
9312 && STRINGP (XCAR (elt
))
9313 && fast_string_match (XCAR (elt
), target
) >= 0)
9314 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9317 /* Here, if VAL is both a valid coding system and a valid
9318 function symbol, we return VAL as a coding system. */
9321 if (! SYMBOLP (val
))
9323 if (! NILP (Fcoding_system_p (val
)))
9324 return Fcons (val
, val
);
9325 if (! NILP (Ffboundp (val
)))
9327 /* We use call1 rather than safe_call1
9328 so as to get bug reports about functions called here
9329 which don't handle the current interface. */
9330 val
= call1 (val
, Flist (nargs
, args
));
9333 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9334 return Fcons (val
, val
);
9342 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9343 Sset_coding_system_priority
, 0, MANY
, 0,
9344 doc
: /* Assign higher priority to the coding systems given as arguments.
9345 If multiple coding systems belong to the same category,
9346 all but the first one are ignored.
9348 usage: (set-coding-system-priority &rest coding-systems) */)
9349 (size_t nargs
, Lisp_Object
*args
)
9352 int changed
[coding_category_max
];
9353 enum coding_category priorities
[coding_category_max
];
9355 memset (changed
, 0, sizeof changed
);
9357 for (i
= j
= 0; i
< nargs
; i
++)
9359 enum coding_category category
;
9360 Lisp_Object spec
, attrs
;
9362 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9363 attrs
= AREF (spec
, 0);
9364 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9365 if (changed
[category
])
9366 /* Ignore this coding system because a coding system of the
9367 same category already had a higher priority. */
9369 changed
[category
] = 1;
9370 priorities
[j
++] = category
;
9371 if (coding_categories
[category
].id
>= 0
9372 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9373 setup_coding_system (args
[i
], &coding_categories
[category
]);
9374 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9377 /* Now we have decided top J priorities. Reflect the order of the
9378 original priorities to the remaining priorities. */
9380 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9382 while (j
< coding_category_max
9383 && changed
[coding_priorities
[j
]])
9385 if (j
== coding_category_max
)
9387 priorities
[i
] = coding_priorities
[j
];
9390 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9392 /* Update `coding-category-list'. */
9393 Vcoding_category_list
= Qnil
;
9394 for (i
= coding_category_max
; i
-- > 0; )
9395 Vcoding_category_list
9396 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9397 Vcoding_category_list
);
9402 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9403 Scoding_system_priority_list
, 0, 1, 0,
9404 doc
: /* Return a list of coding systems ordered by their priorities.
9405 The list contains a subset of coding systems; i.e. coding systems
9406 assigned to each coding category (see `coding-category-list').
9408 HIGHESTP non-nil means just return the highest priority one. */)
9409 (Lisp_Object highestp
)
9414 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9416 enum coding_category category
= coding_priorities
[i
];
9417 int id
= coding_categories
[category
].id
;
9422 attrs
= CODING_ID_ATTRS (id
);
9423 if (! NILP (highestp
))
9424 return CODING_ATTR_BASE_NAME (attrs
);
9425 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9427 return Fnreverse (val
);
9430 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9433 make_subsidiaries (Lisp_Object base
)
9435 Lisp_Object subsidiaries
;
9436 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
9437 char *buf
= (char *) alloca (base_name_len
+ 6);
9440 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9441 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9442 for (i
= 0; i
< 3; i
++)
9444 memcpy (buf
+ base_name_len
, suffixes
[i
], strlen (suffixes
[i
]) + 1);
9445 ASET (subsidiaries
, i
, intern (buf
));
9447 return subsidiaries
;
9451 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9452 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9453 doc
: /* For internal use only.
9454 usage: (define-coding-system-internal ...) */)
9455 (size_t nargs
, Lisp_Object
*args
)
9458 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9459 Lisp_Object attrs
; /* Vector of attributes. */
9460 Lisp_Object eol_type
;
9461 Lisp_Object aliases
;
9462 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9463 enum coding_category category
;
9464 Lisp_Object tail
, val
;
9465 int max_charset_id
= 0;
9468 if (nargs
< coding_arg_max
)
9471 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9473 name
= args
[coding_arg_name
];
9474 CHECK_SYMBOL (name
);
9475 CODING_ATTR_BASE_NAME (attrs
) = name
;
9477 val
= args
[coding_arg_mnemonic
];
9478 if (! STRINGP (val
))
9479 CHECK_CHARACTER (val
);
9480 CODING_ATTR_MNEMONIC (attrs
) = val
;
9482 coding_type
= args
[coding_arg_coding_type
];
9483 CHECK_SYMBOL (coding_type
);
9484 CODING_ATTR_TYPE (attrs
) = coding_type
;
9486 charset_list
= args
[coding_arg_charset_list
];
9487 if (SYMBOLP (charset_list
))
9489 if (EQ (charset_list
, Qiso_2022
))
9491 if (! EQ (coding_type
, Qiso_2022
))
9492 error ("Invalid charset-list");
9493 charset_list
= Viso_2022_charset_list
;
9495 else if (EQ (charset_list
, Qemacs_mule
))
9497 if (! EQ (coding_type
, Qemacs_mule
))
9498 error ("Invalid charset-list");
9499 charset_list
= Vemacs_mule_charset_list
;
9501 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9502 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9503 max_charset_id
= XFASTINT (XCAR (tail
));
9507 charset_list
= Fcopy_sequence (charset_list
);
9508 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9510 struct charset
*charset
;
9513 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9514 if (EQ (coding_type
, Qiso_2022
)
9515 ? CHARSET_ISO_FINAL (charset
) < 0
9516 : EQ (coding_type
, Qemacs_mule
)
9517 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9519 error ("Can't handle charset `%s'",
9520 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9522 XSETCAR (tail
, make_number (charset
->id
));
9523 if (max_charset_id
< charset
->id
)
9524 max_charset_id
= charset
->id
;
9527 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9529 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9530 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9531 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9532 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9533 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9535 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9537 val
= args
[coding_arg_decode_translation_table
];
9538 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9540 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9542 val
= args
[coding_arg_encode_translation_table
];
9543 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9545 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9547 val
= args
[coding_arg_post_read_conversion
];
9549 CODING_ATTR_POST_READ (attrs
) = val
;
9551 val
= args
[coding_arg_pre_write_conversion
];
9553 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9555 val
= args
[coding_arg_default_char
];
9557 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9560 CHECK_CHARACTER (val
);
9561 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9564 val
= args
[coding_arg_for_unibyte
];
9565 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9567 val
= args
[coding_arg_plist
];
9569 CODING_ATTR_PLIST (attrs
) = val
;
9571 if (EQ (coding_type
, Qcharset
))
9573 /* Generate a lisp vector of 256 elements. Each element is nil,
9574 integer, or a list of charset IDs.
9576 If Nth element is nil, the byte code N is invalid in this
9579 If Nth element is a number NUM, N is the first byte of a
9580 charset whose ID is NUM.
9582 If Nth element is a list of charset IDs, N is the first byte
9583 of one of them. The list is sorted by dimensions of the
9584 charsets. A charset of smaller dimension comes first. */
9585 val
= Fmake_vector (make_number (256), Qnil
);
9587 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9589 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9590 int dim
= CHARSET_DIMENSION (charset
);
9591 int idx
= (dim
- 1) * 4;
9593 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9594 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9596 for (i
= charset
->code_space
[idx
];
9597 i
<= charset
->code_space
[idx
+ 1]; i
++)
9599 Lisp_Object tmp
, tmp2
;
9602 tmp
= AREF (val
, i
);
9605 else if (NUMBERP (tmp
))
9607 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9609 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9611 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9615 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9617 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9622 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9625 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9626 XSETCAR (tmp2
, XCAR (tail
));
9632 ASET (attrs
, coding_attr_charset_valids
, val
);
9633 category
= coding_category_charset
;
9635 else if (EQ (coding_type
, Qccl
))
9639 if (nargs
< coding_arg_ccl_max
)
9642 val
= args
[coding_arg_ccl_decoder
];
9643 CHECK_CCL_PROGRAM (val
);
9645 val
= Fcopy_sequence (val
);
9646 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9648 val
= args
[coding_arg_ccl_encoder
];
9649 CHECK_CCL_PROGRAM (val
);
9651 val
= Fcopy_sequence (val
);
9652 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9654 val
= args
[coding_arg_ccl_valids
];
9655 valids
= Fmake_string (make_number (256), make_number (0));
9656 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9663 from
= to
= XINT (val
);
9664 if (from
< 0 || from
> 255)
9665 args_out_of_range_3 (val
, make_number (0), make_number (255));
9670 CHECK_NATNUM_CAR (val
);
9671 CHECK_NATNUM_CDR (val
);
9672 from
= XINT (XCAR (val
));
9674 args_out_of_range_3 (XCAR (val
),
9675 make_number (0), make_number (255));
9676 to
= XINT (XCDR (val
));
9677 if (to
< from
|| to
> 255)
9678 args_out_of_range_3 (XCDR (val
),
9679 XCAR (val
), make_number (255));
9681 for (i
= from
; i
<= to
; i
++)
9682 SSET (valids
, i
, 1);
9684 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9686 category
= coding_category_ccl
;
9688 else if (EQ (coding_type
, Qutf_16
))
9690 Lisp_Object bom
, endian
;
9692 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9694 if (nargs
< coding_arg_utf16_max
)
9697 bom
= args
[coding_arg_utf16_bom
];
9698 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9702 CHECK_CODING_SYSTEM (val
);
9704 CHECK_CODING_SYSTEM (val
);
9706 ASET (attrs
, coding_attr_utf_bom
, bom
);
9708 endian
= args
[coding_arg_utf16_endian
];
9709 CHECK_SYMBOL (endian
);
9712 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9713 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9714 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9716 category
= (CONSP (bom
)
9717 ? coding_category_utf_16_auto
9719 ? (EQ (endian
, Qbig
)
9720 ? coding_category_utf_16_be_nosig
9721 : coding_category_utf_16_le_nosig
)
9722 : (EQ (endian
, Qbig
)
9723 ? coding_category_utf_16_be
9724 : coding_category_utf_16_le
));
9726 else if (EQ (coding_type
, Qiso_2022
))
9728 Lisp_Object initial
, reg_usage
, request
, flags
;
9730 if (nargs
< coding_arg_iso2022_max
)
9733 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9734 CHECK_VECTOR (initial
);
9735 for (i
= 0; i
< 4; i
++)
9737 val
= Faref (initial
, make_number (i
));
9740 struct charset
*charset
;
9742 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9743 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9744 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9745 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9748 ASET (initial
, i
, make_number (-1));
9751 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9752 CHECK_CONS (reg_usage
);
9753 CHECK_NUMBER_CAR (reg_usage
);
9754 CHECK_NUMBER_CDR (reg_usage
);
9756 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9757 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9765 CHECK_CHARSET_GET_ID (tmp1
, id
);
9766 CHECK_NATNUM_CDR (val
);
9767 if (XINT (XCDR (val
)) >= 4)
9768 error ("Invalid graphic register number: %"pEd
, XINT (XCDR (val
)));
9769 XSETCAR (val
, make_number (id
));
9772 flags
= args
[coding_arg_iso2022_flags
];
9773 CHECK_NATNUM (flags
);
9775 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9776 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9778 ASET (attrs
, coding_attr_iso_initial
, initial
);
9779 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9780 ASET (attrs
, coding_attr_iso_request
, request
);
9781 ASET (attrs
, coding_attr_iso_flags
, flags
);
9782 setup_iso_safe_charsets (attrs
);
9784 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9785 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9786 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9787 ? coding_category_iso_7_else
9788 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9789 ? coding_category_iso_7
9790 : coding_category_iso_7_tight
);
9793 int id
= XINT (AREF (initial
, 1));
9795 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9796 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9798 ? coding_category_iso_8_else
9799 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9800 ? coding_category_iso_8_1
9801 : coding_category_iso_8_2
);
9803 if (category
!= coding_category_iso_8_1
9804 && category
!= coding_category_iso_8_2
)
9805 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9807 else if (EQ (coding_type
, Qemacs_mule
))
9809 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9810 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9811 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9812 category
= coding_category_emacs_mule
;
9814 else if (EQ (coding_type
, Qshift_jis
))
9817 struct charset
*charset
;
9819 if (XINT (Flength (charset_list
)) != 3
9820 && XINT (Flength (charset_list
)) != 4)
9821 error ("There should be three or four charsets");
9823 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9824 if (CHARSET_DIMENSION (charset
) != 1)
9825 error ("Dimension of charset %s is not one",
9826 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9827 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9828 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9830 charset_list
= XCDR (charset_list
);
9831 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9832 if (CHARSET_DIMENSION (charset
) != 1)
9833 error ("Dimension of charset %s is not one",
9834 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9836 charset_list
= XCDR (charset_list
);
9837 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9838 if (CHARSET_DIMENSION (charset
) != 2)
9839 error ("Dimension of charset %s is not two",
9840 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9842 charset_list
= XCDR (charset_list
);
9843 if (! NILP (charset_list
))
9845 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9846 if (CHARSET_DIMENSION (charset
) != 2)
9847 error ("Dimension of charset %s is not two",
9848 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9851 category
= coding_category_sjis
;
9852 Vsjis_coding_system
= name
;
9854 else if (EQ (coding_type
, Qbig5
))
9856 struct charset
*charset
;
9858 if (XINT (Flength (charset_list
)) != 2)
9859 error ("There should be just two charsets");
9861 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9862 if (CHARSET_DIMENSION (charset
) != 1)
9863 error ("Dimension of charset %s is not one",
9864 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9865 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9866 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9868 charset_list
= XCDR (charset_list
);
9869 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9870 if (CHARSET_DIMENSION (charset
) != 2)
9871 error ("Dimension of charset %s is not two",
9872 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9874 category
= coding_category_big5
;
9875 Vbig5_coding_system
= name
;
9877 else if (EQ (coding_type
, Qraw_text
))
9879 category
= coding_category_raw_text
;
9880 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9882 else if (EQ (coding_type
, Qutf_8
))
9886 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9888 if (nargs
< coding_arg_utf8_max
)
9891 bom
= args
[coding_arg_utf8_bom
];
9892 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9896 CHECK_CODING_SYSTEM (val
);
9898 CHECK_CODING_SYSTEM (val
);
9900 ASET (attrs
, coding_attr_utf_bom
, bom
);
9902 category
= (CONSP (bom
) ? coding_category_utf_8_auto
9903 : NILP (bom
) ? coding_category_utf_8_nosig
9904 : coding_category_utf_8_sig
);
9906 else if (EQ (coding_type
, Qundecided
))
9907 category
= coding_category_undecided
;
9909 error ("Invalid coding system type: %s",
9910 SDATA (SYMBOL_NAME (coding_type
)));
9912 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9913 CODING_ATTR_PLIST (attrs
)
9914 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9915 CODING_ATTR_PLIST (attrs
)));
9916 CODING_ATTR_PLIST (attrs
)
9917 = Fcons (QCascii_compatible_p
,
9918 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9919 CODING_ATTR_PLIST (attrs
)));
9921 eol_type
= args
[coding_arg_eol_type
];
9922 if (! NILP (eol_type
)
9923 && ! EQ (eol_type
, Qunix
)
9924 && ! EQ (eol_type
, Qdos
)
9925 && ! EQ (eol_type
, Qmac
))
9926 error ("Invalid eol-type");
9928 aliases
= Fcons (name
, Qnil
);
9930 if (NILP (eol_type
))
9932 eol_type
= make_subsidiaries (name
);
9933 for (i
= 0; i
< 3; i
++)
9935 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9937 this_name
= AREF (eol_type
, i
);
9938 this_aliases
= Fcons (this_name
, Qnil
);
9939 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9940 this_spec
= Fmake_vector (make_number (3), attrs
);
9941 ASET (this_spec
, 1, this_aliases
);
9942 ASET (this_spec
, 2, this_eol_type
);
9943 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9944 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9945 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9947 Vcoding_system_alist
9948 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9949 Vcoding_system_alist
);
9953 spec_vec
= Fmake_vector (make_number (3), attrs
);
9954 ASET (spec_vec
, 1, aliases
);
9955 ASET (spec_vec
, 2, eol_type
);
9957 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9958 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9959 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9961 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9962 Vcoding_system_alist
);
9965 int id
= coding_categories
[category
].id
;
9967 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9968 setup_coding_system (name
, &coding_categories
[category
]);
9974 return Fsignal (Qwrong_number_of_arguments
,
9975 Fcons (intern ("define-coding-system-internal"),
9976 make_number (nargs
)));
9980 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
9982 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9983 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
9985 Lisp_Object spec
, attrs
;
9987 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9988 attrs
= AREF (spec
, 0);
9989 if (EQ (prop
, QCmnemonic
))
9991 if (! STRINGP (val
))
9992 CHECK_CHARACTER (val
);
9993 CODING_ATTR_MNEMONIC (attrs
) = val
;
9995 else if (EQ (prop
, QCdefault_char
))
9998 val
= make_number (' ');
10000 CHECK_CHARACTER (val
);
10001 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10003 else if (EQ (prop
, QCdecode_translation_table
))
10005 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10006 CHECK_SYMBOL (val
);
10007 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10009 else if (EQ (prop
, QCencode_translation_table
))
10011 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10012 CHECK_SYMBOL (val
);
10013 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10015 else if (EQ (prop
, QCpost_read_conversion
))
10017 CHECK_SYMBOL (val
);
10018 CODING_ATTR_POST_READ (attrs
) = val
;
10020 else if (EQ (prop
, QCpre_write_conversion
))
10022 CHECK_SYMBOL (val
);
10023 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10025 else if (EQ (prop
, QCascii_compatible_p
))
10027 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10030 CODING_ATTR_PLIST (attrs
)
10031 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10036 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10037 Sdefine_coding_system_alias
, 2, 2, 0,
10038 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10039 (Lisp_Object alias
, Lisp_Object coding_system
)
10041 Lisp_Object spec
, aliases
, eol_type
, val
;
10043 CHECK_SYMBOL (alias
);
10044 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10045 aliases
= AREF (spec
, 1);
10046 /* ALIASES should be a list of length more than zero, and the first
10047 element is a base coding system. Append ALIAS at the tail of the
10049 while (!NILP (XCDR (aliases
)))
10050 aliases
= XCDR (aliases
);
10051 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10053 eol_type
= AREF (spec
, 2);
10054 if (VECTORP (eol_type
))
10056 Lisp_Object subsidiaries
;
10059 subsidiaries
= make_subsidiaries (alias
);
10060 for (i
= 0; i
< 3; i
++)
10061 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10062 AREF (eol_type
, i
));
10065 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10066 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10067 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10069 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10070 Vcoding_system_alist
);
10075 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10077 doc
: /* Return the base of CODING-SYSTEM.
10078 Any alias or subsidiary coding system is not a base coding system. */)
10079 (Lisp_Object coding_system
)
10081 Lisp_Object spec
, attrs
;
10083 if (NILP (coding_system
))
10084 return (Qno_conversion
);
10085 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10086 attrs
= AREF (spec
, 0);
10087 return CODING_ATTR_BASE_NAME (attrs
);
10090 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10092 doc
: "Return the property list of CODING-SYSTEM.")
10093 (Lisp_Object coding_system
)
10095 Lisp_Object spec
, attrs
;
10097 if (NILP (coding_system
))
10098 coding_system
= Qno_conversion
;
10099 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10100 attrs
= AREF (spec
, 0);
10101 return CODING_ATTR_PLIST (attrs
);
10105 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10107 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10108 (Lisp_Object coding_system
)
10112 if (NILP (coding_system
))
10113 coding_system
= Qno_conversion
;
10114 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10115 return AREF (spec
, 1);
10118 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10119 Scoding_system_eol_type
, 1, 1, 0,
10120 doc
: /* Return eol-type of CODING-SYSTEM.
10121 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10123 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10124 and CR respectively.
10126 A vector value indicates that a format of end-of-line should be
10127 detected automatically. Nth element of the vector is the subsidiary
10128 coding system whose eol-type is N. */)
10129 (Lisp_Object coding_system
)
10131 Lisp_Object spec
, eol_type
;
10134 if (NILP (coding_system
))
10135 coding_system
= Qno_conversion
;
10136 if (! CODING_SYSTEM_P (coding_system
))
10138 spec
= CODING_SYSTEM_SPEC (coding_system
);
10139 eol_type
= AREF (spec
, 2);
10140 if (VECTORP (eol_type
))
10141 return Fcopy_sequence (eol_type
);
10142 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10143 return make_number (n
);
10149 /*** 9. Post-amble ***/
10152 init_coding_once (void)
10156 for (i
= 0; i
< coding_category_max
; i
++)
10158 coding_categories
[i
].id
= -1;
10159 coding_priorities
[i
] = i
;
10162 /* ISO2022 specific initialize routine. */
10163 for (i
= 0; i
< 0x20; i
++)
10164 iso_code_class
[i
] = ISO_control_0
;
10165 for (i
= 0x21; i
< 0x7F; i
++)
10166 iso_code_class
[i
] = ISO_graphic_plane_0
;
10167 for (i
= 0x80; i
< 0xA0; i
++)
10168 iso_code_class
[i
] = ISO_control_1
;
10169 for (i
= 0xA1; i
< 0xFF; i
++)
10170 iso_code_class
[i
] = ISO_graphic_plane_1
;
10171 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10172 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10173 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10174 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10175 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10176 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10177 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10178 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10179 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10181 for (i
= 0; i
< 256; i
++)
10183 emacs_mule_bytes
[i
] = 1;
10185 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10186 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10187 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10188 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10194 syms_of_coding (void)
10196 staticpro (&Vcoding_system_hash_table
);
10198 Lisp_Object args
[2];
10201 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10204 staticpro (&Vsjis_coding_system
);
10205 Vsjis_coding_system
= Qnil
;
10207 staticpro (&Vbig5_coding_system
);
10208 Vbig5_coding_system
= Qnil
;
10210 staticpro (&Vcode_conversion_reused_workbuf
);
10211 Vcode_conversion_reused_workbuf
= Qnil
;
10213 staticpro (&Vcode_conversion_workbuf_name
);
10214 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10216 reused_workbuf_in_use
= 0;
10218 DEFSYM (Qcharset
, "charset");
10219 DEFSYM (Qtarget_idx
, "target-idx");
10220 DEFSYM (Qcoding_system_history
, "coding-system-history");
10221 Fset (Qcoding_system_history
, Qnil
);
10223 /* Target FILENAME is the first argument. */
10224 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10225 /* Target FILENAME is the third argument. */
10226 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10228 DEFSYM (Qcall_process
, "call-process");
10229 /* Target PROGRAM is the first argument. */
10230 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10232 DEFSYM (Qcall_process_region
, "call-process-region");
10233 /* Target PROGRAM is the third argument. */
10234 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10236 DEFSYM (Qstart_process
, "start-process");
10237 /* Target PROGRAM is the third argument. */
10238 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10240 DEFSYM (Qopen_network_stream
, "open-network-stream");
10241 /* Target SERVICE is the fourth argument. */
10242 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10244 DEFSYM (Qcoding_system
, "coding-system");
10245 DEFSYM (Qcoding_aliases
, "coding-aliases");
10247 DEFSYM (Qeol_type
, "eol-type");
10248 DEFSYM (Qunix
, "unix");
10249 DEFSYM (Qdos
, "dos");
10251 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10252 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10253 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10254 DEFSYM (Qdefault_char
, "default-char");
10255 DEFSYM (Qundecided
, "undecided");
10256 DEFSYM (Qno_conversion
, "no-conversion");
10257 DEFSYM (Qraw_text
, "raw-text");
10259 DEFSYM (Qiso_2022
, "iso-2022");
10261 DEFSYM (Qutf_8
, "utf-8");
10262 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10264 DEFSYM (Qutf_16
, "utf-16");
10265 DEFSYM (Qbig
, "big");
10266 DEFSYM (Qlittle
, "little");
10268 DEFSYM (Qshift_jis
, "shift-jis");
10269 DEFSYM (Qbig5
, "big5");
10271 DEFSYM (Qcoding_system_p
, "coding-system-p");
10273 DEFSYM (Qcoding_system_error
, "coding-system-error");
10274 Fput (Qcoding_system_error
, Qerror_conditions
,
10275 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10276 Fput (Qcoding_system_error
, Qerror_message
,
10277 make_pure_c_string ("Invalid coding system"));
10279 /* Intern this now in case it isn't already done.
10280 Setting this variable twice is harmless.
10281 But don't staticpro it here--that is done in alloc.c. */
10282 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10284 DEFSYM (Qtranslation_table
, "translation-table");
10285 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10286 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10287 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10288 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10290 DEFSYM (Qvalid_codes
, "valid-codes");
10292 DEFSYM (Qemacs_mule
, "emacs-mule");
10294 DEFSYM (QCcategory
, ":category");
10295 DEFSYM (QCmnemonic
, ":mnemonic");
10296 DEFSYM (QCdefault_char
, ":default-char");
10297 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10298 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10299 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10300 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10301 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10303 Vcoding_category_table
10304 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10305 staticpro (&Vcoding_category_table
);
10306 /* Followings are target of code detection. */
10307 ASET (Vcoding_category_table
, coding_category_iso_7
,
10308 intern_c_string ("coding-category-iso-7"));
10309 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10310 intern_c_string ("coding-category-iso-7-tight"));
10311 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10312 intern_c_string ("coding-category-iso-8-1"));
10313 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10314 intern_c_string ("coding-category-iso-8-2"));
10315 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10316 intern_c_string ("coding-category-iso-7-else"));
10317 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10318 intern_c_string ("coding-category-iso-8-else"));
10319 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10320 intern_c_string ("coding-category-utf-8-auto"));
10321 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10322 intern_c_string ("coding-category-utf-8"));
10323 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10324 intern_c_string ("coding-category-utf-8-sig"));
10325 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10326 intern_c_string ("coding-category-utf-16-be"));
10327 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10328 intern_c_string ("coding-category-utf-16-auto"));
10329 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10330 intern_c_string ("coding-category-utf-16-le"));
10331 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10332 intern_c_string ("coding-category-utf-16-be-nosig"));
10333 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10334 intern_c_string ("coding-category-utf-16-le-nosig"));
10335 ASET (Vcoding_category_table
, coding_category_charset
,
10336 intern_c_string ("coding-category-charset"));
10337 ASET (Vcoding_category_table
, coding_category_sjis
,
10338 intern_c_string ("coding-category-sjis"));
10339 ASET (Vcoding_category_table
, coding_category_big5
,
10340 intern_c_string ("coding-category-big5"));
10341 ASET (Vcoding_category_table
, coding_category_ccl
,
10342 intern_c_string ("coding-category-ccl"));
10343 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10344 intern_c_string ("coding-category-emacs-mule"));
10345 /* Followings are NOT target of code detection. */
10346 ASET (Vcoding_category_table
, coding_category_raw_text
,
10347 intern_c_string ("coding-category-raw-text"));
10348 ASET (Vcoding_category_table
, coding_category_undecided
,
10349 intern_c_string ("coding-category-undecided"));
10351 DEFSYM (Qinsufficient_source
, "insufficient-source");
10352 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10353 DEFSYM (Qinvalid_source
, "invalid-source");
10354 DEFSYM (Qinterrupted
, "interrupted");
10355 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10356 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10358 defsubr (&Scoding_system_p
);
10359 defsubr (&Sread_coding_system
);
10360 defsubr (&Sread_non_nil_coding_system
);
10361 defsubr (&Scheck_coding_system
);
10362 defsubr (&Sdetect_coding_region
);
10363 defsubr (&Sdetect_coding_string
);
10364 defsubr (&Sfind_coding_systems_region_internal
);
10365 defsubr (&Sunencodable_char_position
);
10366 defsubr (&Scheck_coding_systems_region
);
10367 defsubr (&Sdecode_coding_region
);
10368 defsubr (&Sencode_coding_region
);
10369 defsubr (&Sdecode_coding_string
);
10370 defsubr (&Sencode_coding_string
);
10371 defsubr (&Sdecode_sjis_char
);
10372 defsubr (&Sencode_sjis_char
);
10373 defsubr (&Sdecode_big5_char
);
10374 defsubr (&Sencode_big5_char
);
10375 defsubr (&Sset_terminal_coding_system_internal
);
10376 defsubr (&Sset_safe_terminal_coding_system_internal
);
10377 defsubr (&Sterminal_coding_system
);
10378 defsubr (&Sset_keyboard_coding_system_internal
);
10379 defsubr (&Skeyboard_coding_system
);
10380 defsubr (&Sfind_operation_coding_system
);
10381 defsubr (&Sset_coding_system_priority
);
10382 defsubr (&Sdefine_coding_system_internal
);
10383 defsubr (&Sdefine_coding_system_alias
);
10384 defsubr (&Scoding_system_put
);
10385 defsubr (&Scoding_system_base
);
10386 defsubr (&Scoding_system_plist
);
10387 defsubr (&Scoding_system_aliases
);
10388 defsubr (&Scoding_system_eol_type
);
10389 defsubr (&Scoding_system_priority_list
);
10391 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10392 doc
: /* List of coding systems.
10394 Do not alter the value of this variable manually. This variable should be
10395 updated by the functions `define-coding-system' and
10396 `define-coding-system-alias'. */);
10397 Vcoding_system_list
= Qnil
;
10399 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10400 doc
: /* Alist of coding system names.
10401 Each element is one element list of coding system name.
10402 This variable is given to `completing-read' as COLLECTION argument.
10404 Do not alter the value of this variable manually. This variable should be
10405 updated by the functions `make-coding-system' and
10406 `define-coding-system-alias'. */);
10407 Vcoding_system_alist
= Qnil
;
10409 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10410 doc
: /* List of coding-categories (symbols) ordered by priority.
10412 On detecting a coding system, Emacs tries code detection algorithms
10413 associated with each coding-category one by one in this order. When
10414 one algorithm agrees with a byte sequence of source text, the coding
10415 system bound to the corresponding coding-category is selected.
10417 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10421 Vcoding_category_list
= Qnil
;
10422 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10423 Vcoding_category_list
10424 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10425 Vcoding_category_list
);
10428 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10429 doc
: /* Specify the coding system for read operations.
10430 It is useful to bind this variable with `let', but do not set it globally.
10431 If the value is a coding system, it is used for decoding on read operation.
10432 If not, an appropriate element is used from one of the coding system alists.
10433 There are three such tables: `file-coding-system-alist',
10434 `process-coding-system-alist', and `network-coding-system-alist'. */);
10435 Vcoding_system_for_read
= Qnil
;
10437 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10438 doc
: /* Specify the coding system for write operations.
10439 Programs bind this variable with `let', but you should not set it globally.
10440 If the value is a coding system, it is used for encoding of output,
10441 when writing it to a file and when sending it to a file or subprocess.
10443 If this does not specify a coding system, an appropriate element
10444 is used from one of the coding system alists.
10445 There are three such tables: `file-coding-system-alist',
10446 `process-coding-system-alist', and `network-coding-system-alist'.
10447 For output to files, if the above procedure does not specify a coding system,
10448 the value of `buffer-file-coding-system' is used. */);
10449 Vcoding_system_for_write
= Qnil
;
10451 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10453 Coding system used in the latest file or process I/O. */);
10454 Vlast_coding_system_used
= Qnil
;
10456 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10458 Error status of the last code conversion.
10460 When an error was detected in the last code conversion, this variable
10461 is set to one of the following symbols.
10462 `insufficient-source'
10466 `insufficient-memory'
10467 When no error was detected, the value doesn't change. So, to check
10468 the error status of a code conversion by this variable, you must
10469 explicitly set this variable to nil before performing code
10471 Vlast_code_conversion_error
= Qnil
;
10473 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10475 *Non-nil means always inhibit code conversion of end-of-line format.
10476 See info node `Coding Systems' and info node `Text and Binary' concerning
10477 such conversion. */);
10478 inhibit_eol_conversion
= 0;
10480 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10482 Non-nil means process buffer inherits coding system of process output.
10483 Bind it to t if the process output is to be treated as if it were a file
10484 read from some filesystem. */);
10485 inherit_process_coding_system
= 0;
10487 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10489 Alist to decide a coding system to use for a file I/O operation.
10490 The format is ((PATTERN . VAL) ...),
10491 where PATTERN is a regular expression matching a file name,
10492 VAL is a coding system, a cons of coding systems, or a function symbol.
10493 If VAL is a coding system, it is used for both decoding and encoding
10495 If VAL is a cons of coding systems, the car part is used for decoding,
10496 and the cdr part is used for encoding.
10497 If VAL is a function symbol, the function must return a coding system
10498 or a cons of coding systems which are used as above. The function is
10499 called with an argument that is a list of the arguments with which
10500 `find-operation-coding-system' was called. If the function can't decide
10501 a coding system, it can return `undecided' so that the normal
10502 code-detection is performed.
10504 See also the function `find-operation-coding-system'
10505 and the variable `auto-coding-alist'. */);
10506 Vfile_coding_system_alist
= Qnil
;
10508 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10510 Alist to decide a coding system to use for a process I/O operation.
10511 The format is ((PATTERN . VAL) ...),
10512 where PATTERN is a regular expression matching a program name,
10513 VAL is a coding system, a cons of coding systems, or a function symbol.
10514 If VAL is a coding system, it is used for both decoding what received
10515 from the program and encoding what sent to the program.
10516 If VAL is a cons of coding systems, the car part is used for decoding,
10517 and the cdr part is used for encoding.
10518 If VAL is a function symbol, the function must return a coding system
10519 or a cons of coding systems which are used as above.
10521 See also the function `find-operation-coding-system'. */);
10522 Vprocess_coding_system_alist
= Qnil
;
10524 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10526 Alist to decide a coding system to use for a network I/O operation.
10527 The format is ((PATTERN . VAL) ...),
10528 where PATTERN is a regular expression matching a network service name
10529 or is a port number to connect to,
10530 VAL is a coding system, a cons of coding systems, or a function symbol.
10531 If VAL is a coding system, it is used for both decoding what received
10532 from the network stream and encoding what sent to the network stream.
10533 If VAL is a cons of coding systems, the car part is used for decoding,
10534 and the cdr part is used for encoding.
10535 If VAL is a function symbol, the function must return a coding system
10536 or a cons of coding systems which are used as above.
10538 See also the function `find-operation-coding-system'. */);
10539 Vnetwork_coding_system_alist
= Qnil
;
10541 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10542 doc
: /* Coding system to use with system messages.
10543 Also used for decoding keyboard input on X Window system. */);
10544 Vlocale_coding_system
= Qnil
;
10546 /* The eol mnemonics are reset in startup.el system-dependently. */
10547 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10549 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10550 eol_mnemonic_unix
= make_pure_c_string (":");
10552 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10554 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10555 eol_mnemonic_dos
= make_pure_c_string ("\\");
10557 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10559 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10560 eol_mnemonic_mac
= make_pure_c_string ("/");
10562 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10564 *String displayed in mode line when end-of-line format is not yet determined. */);
10565 eol_mnemonic_undecided
= make_pure_c_string (":");
10567 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10569 *Non-nil enables character translation while encoding and decoding. */);
10570 Venable_character_translation
= Qt
;
10572 DEFVAR_LISP ("standard-translation-table-for-decode",
10573 Vstandard_translation_table_for_decode
,
10574 doc
: /* Table for translating characters while decoding. */);
10575 Vstandard_translation_table_for_decode
= Qnil
;
10577 DEFVAR_LISP ("standard-translation-table-for-encode",
10578 Vstandard_translation_table_for_encode
,
10579 doc
: /* Table for translating characters while encoding. */);
10580 Vstandard_translation_table_for_encode
= Qnil
;
10582 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10583 doc
: /* Alist of charsets vs revision numbers.
10584 While encoding, if a charset (car part of an element) is found,
10585 designate it with the escape sequence identifying revision (cdr part
10586 of the element). */);
10587 Vcharset_revision_table
= Qnil
;
10589 DEFVAR_LISP ("default-process-coding-system",
10590 Vdefault_process_coding_system
,
10591 doc
: /* Cons of coding systems used for process I/O by default.
10592 The car part is used for decoding a process output,
10593 the cdr part is used for encoding a text to be sent to a process. */);
10594 Vdefault_process_coding_system
= Qnil
;
10596 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10598 Table of extra Latin codes in the range 128..159 (inclusive).
10599 This is a vector of length 256.
10600 If Nth element is non-nil, the existence of code N in a file
10601 \(or output of subprocess) doesn't prevent it to be detected as
10602 a coding system of ISO 2022 variant which has a flag
10603 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10604 or reading output of a subprocess.
10605 Only 128th through 159th elements have a meaning. */);
10606 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10608 DEFVAR_LISP ("select-safe-coding-system-function",
10609 Vselect_safe_coding_system_function
,
10611 Function to call to select safe coding system for encoding a text.
10613 If set, this function is called to force a user to select a proper
10614 coding system which can encode the text in the case that a default
10615 coding system used in each operation can't encode the text. The
10616 function should take care that the buffer is not modified while
10617 the coding system is being selected.
10619 The default value is `select-safe-coding-system' (which see). */);
10620 Vselect_safe_coding_system_function
= Qnil
;
10622 DEFVAR_BOOL ("coding-system-require-warning",
10623 coding_system_require_warning
,
10624 doc
: /* Internal use only.
10625 If non-nil, on writing a file, `select-safe-coding-system-function' is
10626 called even if `coding-system-for-write' is non-nil. The command
10627 `universal-coding-system-argument' binds this variable to t temporarily. */);
10628 coding_system_require_warning
= 0;
10631 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10632 inhibit_iso_escape_detection
,
10634 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10636 When Emacs reads text, it tries to detect how the text is encoded.
10637 This code detection is sensitive to escape sequences. If Emacs sees
10638 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10639 of the ISO2022 encodings, and decodes text by the corresponding coding
10640 system (e.g. `iso-2022-7bit').
10642 However, there may be a case that you want to read escape sequences in
10643 a file as is. In such a case, you can set this variable to non-nil.
10644 Then the code detection will ignore any escape sequences, and no text is
10645 detected as encoded in some ISO-2022 encoding. The result is that all
10646 escape sequences become visible in a buffer.
10648 The default value is nil, and it is strongly recommended not to change
10649 it. That is because many Emacs Lisp source files that contain
10650 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10651 in Emacs's distribution, and they won't be decoded correctly on
10652 reading if you suppress escape sequence detection.
10654 The other way to read escape sequences in a file without decoding is
10655 to explicitly specify some coding system that doesn't use ISO-2022
10656 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10657 inhibit_iso_escape_detection
= 0;
10659 DEFVAR_BOOL ("inhibit-null-byte-detection",
10660 inhibit_null_byte_detection
,
10661 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10662 By default, Emacs treats it as binary data, and does not attempt to
10663 decode it. The effect is as if you specified `no-conversion' for
10666 Set this to non-nil when a regular text happens to include null bytes.
10667 Examples are Index nodes of Info files and null-byte delimited output
10668 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10669 decode text as usual. */);
10670 inhibit_null_byte_detection
= 0;
10672 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10673 doc
: /* Char table for translating self-inserting characters.
10674 This is applied to the result of input methods, not their input.
10675 See also `keyboard-translate-table'.
10677 Use of this variable for character code unification was rendered
10678 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10679 internal character representation. */);
10680 Vtranslation_table_for_input
= Qnil
;
10683 Lisp_Object args
[coding_arg_max
];
10684 Lisp_Object plist
[16];
10687 for (i
= 0; i
< coding_arg_max
; i
++)
10690 plist
[0] = intern_c_string (":name");
10691 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10692 plist
[2] = intern_c_string (":mnemonic");
10693 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10694 plist
[4] = intern_c_string (":coding-type");
10695 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10696 plist
[6] = intern_c_string (":ascii-compatible-p");
10697 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10698 plist
[8] = intern_c_string (":default-char");
10699 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10700 plist
[10] = intern_c_string (":for-unibyte");
10701 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10702 plist
[12] = intern_c_string (":docstring");
10703 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10705 When you visit a file with this coding, the file is read into a\n\
10706 unibyte buffer as is, thus each byte of a file is treated as a\n\
10708 plist
[14] = intern_c_string (":eol-type");
10709 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10710 args
[coding_arg_plist
] = Flist (16, plist
);
10711 Fdefine_coding_system_internal (coding_arg_max
, args
);
10713 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10714 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10715 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10716 /* This is already set.
10717 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10718 plist
[8] = intern_c_string (":charset-list");
10719 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10720 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10721 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10722 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10723 args
[coding_arg_plist
] = Flist (16, plist
);
10724 Fdefine_coding_system_internal (coding_arg_max
, args
);
10727 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10732 for (i
= 0; i
< coding_category_max
; i
++)
10733 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10735 #if defined (DOS_NT)
10736 system_eol_type
= Qdos
;
10738 system_eol_type
= Qunix
;
10740 staticpro (&system_eol_type
);
10744 emacs_strerror (int error_number
)
10748 synchronize_system_messages_locale ();
10749 str
= strerror (error_number
);
10751 if (! NILP (Vlocale_coding_system
))
10753 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10754 Vlocale_coding_system
,
10756 str
= SSDATA (dec
);