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. On
59 the C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
71 o Charset-base coding system
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
77 o Old Emacs internal format (emacs-mule)
79 The coding system adopted by old versions of Emacs (20 and 21).
81 o ISO2022-base coding system
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
117 Like raw text, but don't do end-of-line conversion.
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. MacOS's format is usually one byte of
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
150 Return 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 EMACS_INT
coding_set_source (struct coding_system
*);
851 static EMACS_INT
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 int encode_designation_at_bol (struct coding_system
*,
859 int *, 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 /* These wrapper macros are used to preserve validity of pointers into
919 buffer text across calls to decode_char, encode_char, etc, which
920 could cause relocation of buffers if it loads a charset map,
921 because loading a charset map allocates large structures. */
923 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
927 charset_map_loaded = 0; \
928 c = DECODE_CHAR (charset, code); \
929 if (charset_map_loaded \
930 && (offset = coding_set_source (coding))) \
933 src_base += offset; \
938 #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \
942 charset_map_loaded = 0; \
943 code = ENCODE_CHAR (charset, c); \
944 if (charset_map_loaded \
945 && (offset = coding_set_destination (coding))) \
952 #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \
956 charset_map_loaded = 0; \
957 charset = char_charset (c, charset_list, code_return); \
958 if (charset_map_loaded \
959 && (offset = coding_set_destination (coding))) \
966 #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \
970 charset_map_loaded = 0; \
971 result = CHAR_CHARSET_P (c, charset); \
972 if (charset_map_loaded \
973 && (offset = coding_set_destination (coding))) \
981 /* If there are at least BYTES length of room at dst, allocate memory
982 for coding->destination and update dst and dst_end. We don't have
983 to take care of coding->source which will be relocated. It is
984 handled by calling coding_set_source in encode_coding. */
986 #define ASSURE_DESTINATION(bytes) \
988 if (dst + (bytes) >= dst_end) \
990 EMACS_INT more_bytes = charbuf_end - charbuf + (bytes); \
992 dst = alloc_destination (coding, more_bytes, dst); \
993 dst_end = coding->destination + coding->dst_bytes; \
998 /* Store multibyte form of the character C in P, and advance P to the
999 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
1000 never calls MAYBE_UNIFY_CHAR. */
1002 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
1004 if ((c) <= MAX_1_BYTE_CHAR) \
1006 else if ((c) <= MAX_2_BYTE_CHAR) \
1007 *(p)++ = (0xC0 | ((c) >> 6)), \
1008 *(p)++ = (0x80 | ((c) & 0x3F)); \
1009 else if ((c) <= MAX_3_BYTE_CHAR) \
1010 *(p)++ = (0xE0 | ((c) >> 12)), \
1011 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
1012 *(p)++ = (0x80 | ((c) & 0x3F)); \
1013 else if ((c) <= MAX_4_BYTE_CHAR) \
1014 *(p)++ = (0xF0 | (c >> 18)), \
1015 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1016 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1017 *(p)++ = (0x80 | (c & 0x3F)); \
1018 else if ((c) <= MAX_5_BYTE_CHAR) \
1020 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
1021 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
1022 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1023 *(p)++ = (0x80 | (c & 0x3F)); \
1025 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1029 /* Return the character code of character whose multibyte form is at
1030 P, and advance P to the end of the multibyte form. This is like
1031 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1033 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1036 : ! ((p)[0] & 0x20) \
1038 ((((p)[-2] & 0x1F) << 6) \
1039 | ((p)[-1] & 0x3F) \
1040 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1041 : ! ((p)[0] & 0x10) \
1043 ((((p)[-3] & 0x0F) << 12) \
1044 | (((p)[-2] & 0x3F) << 6) \
1045 | ((p)[-1] & 0x3F))) \
1046 : ! ((p)[0] & 0x08) \
1048 ((((p)[-4] & 0xF) << 18) \
1049 | (((p)[-3] & 0x3F) << 12) \
1050 | (((p)[-2] & 0x3F) << 6) \
1051 | ((p)[-1] & 0x3F))) \
1053 ((((p)[-4] & 0x3F) << 18) \
1054 | (((p)[-3] & 0x3F) << 12) \
1055 | (((p)[-2] & 0x3F) << 6) \
1056 | ((p)[-1] & 0x3F))))
1059 /* Update coding->source from coding->src_object, and return how many
1060 bytes coding->source was changed. */
1063 coding_set_source (struct coding_system
*coding
)
1065 const unsigned char *orig
= coding
->source
;
1067 if (BUFFERP (coding
->src_object
))
1069 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1071 if (coding
->src_pos
< 0)
1072 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1074 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1076 else if (STRINGP (coding
->src_object
))
1078 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1082 /* Otherwise, the source is C string and is never relocated
1083 automatically. Thus we don't have to update anything. */
1085 return coding
->source
- orig
;
1089 /* Update coding->destination from coding->dst_object, and return how
1090 many bytes coding->destination was changed. */
1093 coding_set_destination (struct coding_system
*coding
)
1095 const unsigned char *orig
= coding
->destination
;
1097 if (BUFFERP (coding
->dst_object
))
1099 if (BUFFERP (coding
->src_object
) && coding
->src_pos
< 0)
1101 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1102 coding
->dst_bytes
= (GAP_END_ADDR
1103 - (coding
->src_bytes
- coding
->consumed
)
1104 - coding
->destination
);
1108 /* We are sure that coding->dst_pos_byte is before the gap
1110 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1111 + coding
->dst_pos_byte
- BEG_BYTE
);
1112 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1113 - coding
->destination
);
1118 /* Otherwise, the destination is C string and is never relocated
1119 automatically. Thus we don't have to update anything. */
1121 return coding
->destination
- orig
;
1126 coding_alloc_by_realloc (struct coding_system
*coding
, EMACS_INT bytes
)
1128 if (STRING_BYTES_BOUND
- coding
->dst_bytes
< bytes
)
1130 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1131 coding
->dst_bytes
+ bytes
);
1132 coding
->dst_bytes
+= bytes
;
1136 coding_alloc_by_making_gap (struct coding_system
*coding
,
1137 EMACS_INT gap_head_used
, EMACS_INT bytes
)
1139 if (EQ (coding
->src_object
, coding
->dst_object
))
1141 /* The gap may contain the produced data at the head and not-yet
1142 consumed data at the tail. To preserve those data, we at
1143 first make the gap size to zero, then increase the gap
1145 EMACS_INT add
= GAP_SIZE
;
1147 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1148 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1150 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1151 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1155 Lisp_Object this_buffer
;
1157 this_buffer
= Fcurrent_buffer ();
1158 set_buffer_internal (XBUFFER (coding
->dst_object
));
1160 set_buffer_internal (XBUFFER (this_buffer
));
1165 static unsigned char *
1166 alloc_destination (struct coding_system
*coding
, EMACS_INT nbytes
,
1169 EMACS_INT offset
= dst
- coding
->destination
;
1171 if (BUFFERP (coding
->dst_object
))
1173 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1175 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1178 coding_alloc_by_realloc (coding
, nbytes
);
1179 coding_set_destination (coding
);
1180 dst
= coding
->destination
+ offset
;
1184 /** Macros for annotations. */
1186 /* An annotation data is stored in the array coding->charbuf in this
1188 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1189 LENGTH is the number of elements in the annotation.
1190 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1191 NCHARS is the number of characters in the text annotated.
1193 The format of the following elements depend on ANNOTATION_MASK.
1195 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1197 ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ]
1199 NBYTES is the number of bytes specified in the header part of
1200 old-style emacs-mule encoding, or 0 for the other kind of
1203 METHOD is one of enum composition_method.
1205 Optional COMPOSITION-COMPONENTS are characters and composition
1208 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1211 If ANNOTATION_MASK is 0, this annotation is just a space holder to
1212 recover from an invalid annotation, and should be skipped by
1213 produce_annotation. */
1215 /* Maximum length of the header of annotation data. */
1216 #define MAX_ANNOTATION_LENGTH 5
1218 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1220 *(buf)++ = -(len); \
1221 *(buf)++ = (mask); \
1222 *(buf)++ = (nchars); \
1223 coding->annotated = 1; \
1226 #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \
1228 ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1234 #define ADD_CHARSET_DATA(buf, nchars, id) \
1236 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1241 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1248 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1249 Check if a text is encoded in UTF-8. If it is, return 1, else
1252 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1253 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1254 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1255 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1256 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1257 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1259 #define UTF_8_BOM_1 0xEF
1260 #define UTF_8_BOM_2 0xBB
1261 #define UTF_8_BOM_3 0xBF
1264 detect_coding_utf_8 (struct coding_system
*coding
,
1265 struct coding_detection_info
*detect_info
)
1267 const unsigned char *src
= coding
->source
, *src_base
;
1268 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1269 int multibytep
= coding
->src_multibyte
;
1270 EMACS_INT consumed_chars
= 0;
1274 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1275 /* A coding system of this category is always ASCII compatible. */
1276 src
+= coding
->head_ascii
;
1280 int c
, c1
, c2
, c3
, c4
;
1284 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1287 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1289 if (UTF_8_2_OCTET_LEADING_P (c
))
1295 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1297 if (UTF_8_3_OCTET_LEADING_P (c
))
1300 if (src_base
== coding
->source
1301 && c
== UTF_8_BOM_1
&& c1
== UTF_8_BOM_2
&& c2
== UTF_8_BOM_3
)
1306 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1308 if (UTF_8_4_OCTET_LEADING_P (c
))
1314 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1316 if (UTF_8_5_OCTET_LEADING_P (c
))
1323 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1327 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1329 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1334 /* The first character 0xFFFE doesn't necessarily mean a BOM. */
1335 detect_info
->found
|= CATEGORY_MASK_UTF_8_SIG
| CATEGORY_MASK_UTF_8_NOSIG
;
1339 detect_info
->rejected
|= CATEGORY_MASK_UTF_8_SIG
;
1341 detect_info
->found
|= CATEGORY_MASK_UTF_8_NOSIG
;
1348 decode_coding_utf_8 (struct coding_system
*coding
)
1350 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1351 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1352 const unsigned char *src_base
;
1353 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1354 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1355 EMACS_INT consumed_chars
= 0, consumed_chars_base
= 0;
1356 int multibytep
= coding
->src_multibyte
;
1357 enum utf_bom_type bom
= CODING_UTF_8_BOM (coding
);
1359 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1360 int byte_after_cr
= -1;
1362 if (bom
!= utf_without_bom
)
1368 if (! UTF_8_3_OCTET_LEADING_P (c1
))
1373 if (! UTF_8_EXTRA_OCTET_P (c2
))
1378 if (! UTF_8_EXTRA_OCTET_P (c3
))
1382 if ((c1
!= UTF_8_BOM_1
)
1383 || (c2
!= UTF_8_BOM_2
) || (c3
!= UTF_8_BOM_3
))
1386 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1391 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1395 int c
, c1
, c2
, c3
, c4
, c5
;
1398 consumed_chars_base
= consumed_chars
;
1400 if (charbuf
>= charbuf_end
)
1402 if (byte_after_cr
>= 0)
1407 if (byte_after_cr
>= 0)
1408 c1
= byte_after_cr
, byte_after_cr
= -1;
1415 else if (UTF_8_1_OCTET_P (c1
))
1417 if (eol_dos
&& c1
== '\r')
1418 ONE_MORE_BYTE (byte_after_cr
);
1424 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1426 if (UTF_8_2_OCTET_LEADING_P (c1
))
1428 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1429 /* Reject overlong sequences here and below. Encoders
1430 producing them are incorrect, they can be misleading,
1431 and they mess up read/write invariance. */
1438 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1440 if (UTF_8_3_OCTET_LEADING_P (c1
))
1442 c
= (((c1
& 0xF) << 12)
1443 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1445 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1451 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1453 if (UTF_8_4_OCTET_LEADING_P (c1
))
1455 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1456 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1463 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1465 if (UTF_8_5_OCTET_LEADING_P (c1
))
1467 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1468 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1470 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1485 consumed_chars
= consumed_chars_base
;
1487 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1492 coding
->consumed_char
+= consumed_chars_base
;
1493 coding
->consumed
= src_base
- coding
->source
;
1494 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1499 encode_coding_utf_8 (struct coding_system
*coding
)
1501 int multibytep
= coding
->dst_multibyte
;
1502 int *charbuf
= coding
->charbuf
;
1503 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1504 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1505 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1506 EMACS_INT produced_chars
= 0;
1509 if (CODING_UTF_8_BOM (coding
) == utf_with_bom
)
1511 ASSURE_DESTINATION (3);
1512 EMIT_THREE_BYTES (UTF_8_BOM_1
, UTF_8_BOM_2
, UTF_8_BOM_3
);
1513 CODING_UTF_8_BOM (coding
) = utf_without_bom
;
1518 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1520 while (charbuf
< charbuf_end
)
1522 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1524 ASSURE_DESTINATION (safe_room
);
1526 if (CHAR_BYTE8_P (c
))
1528 c
= CHAR_TO_BYTE8 (c
);
1533 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1534 for (p
= str
; p
< pend
; p
++)
1541 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1543 while (charbuf
< charbuf_end
)
1545 ASSURE_DESTINATION (safe_room
);
1547 if (CHAR_BYTE8_P (c
))
1548 *dst
++ = CHAR_TO_BYTE8 (c
);
1550 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1554 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1555 coding
->produced_char
+= produced_chars
;
1556 coding
->produced
= dst
- coding
->destination
;
1561 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1562 Check if a text is encoded in one of UTF-16 based coding systems.
1563 If it is, return 1, else return 0. */
1565 #define UTF_16_HIGH_SURROGATE_P(val) \
1566 (((val) & 0xFC00) == 0xD800)
1568 #define UTF_16_LOW_SURROGATE_P(val) \
1569 (((val) & 0xFC00) == 0xDC00)
1573 detect_coding_utf_16 (struct coding_system
*coding
,
1574 struct coding_detection_info
*detect_info
)
1576 const unsigned char *src
= coding
->source
;
1577 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1578 int multibytep
= coding
->src_multibyte
;
1581 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1582 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1583 && (coding
->src_chars
& 1))
1585 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1589 TWO_MORE_BYTES (c1
, c2
);
1590 if ((c1
== 0xFF) && (c2
== 0xFE))
1592 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1593 | CATEGORY_MASK_UTF_16_AUTO
);
1594 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1595 | CATEGORY_MASK_UTF_16_BE_NOSIG
1596 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1598 else if ((c1
== 0xFE) && (c2
== 0xFF))
1600 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1601 | CATEGORY_MASK_UTF_16_AUTO
);
1602 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1603 | CATEGORY_MASK_UTF_16_BE_NOSIG
1604 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1608 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1613 /* We check the dispersion of Eth and Oth bytes where E is even and
1614 O is odd. If both are high, we assume binary data.*/
1615 unsigned char e
[256], o
[256];
1616 unsigned e_num
= 1, o_num
= 1;
1623 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_AUTO
1624 |CATEGORY_MASK_UTF_16_BE
1625 | CATEGORY_MASK_UTF_16_LE
);
1627 while ((detect_info
->rejected
& CATEGORY_MASK_UTF_16
)
1628 != CATEGORY_MASK_UTF_16
)
1630 TWO_MORE_BYTES (c1
, c2
);
1638 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1645 detect_info
->rejected
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1656 decode_coding_utf_16 (struct coding_system
*coding
)
1658 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1659 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1660 const unsigned char *src_base
;
1661 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1662 /* We may produces at most 3 chars in one loop. */
1663 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
- 2;
1664 EMACS_INT consumed_chars
= 0, consumed_chars_base
= 0;
1665 int multibytep
= coding
->src_multibyte
;
1666 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1667 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1668 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1670 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1671 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1673 if (bom
== utf_with_bom
)
1682 if (endian
== utf_16_big_endian
1683 ? c
!= 0xFEFF : c
!= 0xFFFE)
1685 /* The first two bytes are not BOM. Treat them as bytes
1686 for a normal character. */
1690 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1692 else if (bom
== utf_detect_bom
)
1694 /* We have already tried to detect BOM and failed in
1696 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1704 consumed_chars_base
= consumed_chars
;
1706 if (charbuf
>= charbuf_end
)
1708 if (byte_after_cr1
>= 0)
1713 if (byte_after_cr1
>= 0)
1714 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1722 if (byte_after_cr2
>= 0)
1723 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1728 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1732 c
= (endian
== utf_16_big_endian
1733 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1737 if (! UTF_16_LOW_SURROGATE_P (c
))
1739 if (endian
== utf_16_big_endian
)
1740 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1742 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1746 if (UTF_16_HIGH_SURROGATE_P (c
))
1747 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1753 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1754 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1755 *charbuf
++ = 0x10000 + c
;
1760 if (UTF_16_HIGH_SURROGATE_P (c
))
1761 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1764 if (eol_dos
&& c
== '\r')
1766 ONE_MORE_BYTE (byte_after_cr1
);
1767 ONE_MORE_BYTE (byte_after_cr2
);
1775 coding
->consumed_char
+= consumed_chars_base
;
1776 coding
->consumed
= src_base
- coding
->source
;
1777 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1781 encode_coding_utf_16 (struct coding_system
*coding
)
1783 int multibytep
= coding
->dst_multibyte
;
1784 int *charbuf
= coding
->charbuf
;
1785 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1786 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1787 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1789 enum utf_bom_type bom
= CODING_UTF_16_BOM (coding
);
1790 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1791 EMACS_INT produced_chars
= 0;
1794 if (bom
!= utf_without_bom
)
1796 ASSURE_DESTINATION (safe_room
);
1798 EMIT_TWO_BYTES (0xFE, 0xFF);
1800 EMIT_TWO_BYTES (0xFF, 0xFE);
1801 CODING_UTF_16_BOM (coding
) = utf_without_bom
;
1804 while (charbuf
< charbuf_end
)
1806 ASSURE_DESTINATION (safe_room
);
1808 if (c
> MAX_UNICODE_CHAR
)
1809 c
= coding
->default_char
;
1814 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1816 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1823 c1
= (c
>> 10) + 0xD800;
1824 c2
= (c
& 0x3FF) + 0xDC00;
1826 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1828 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1831 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1832 coding
->produced
= dst
- coding
->destination
;
1833 coding
->produced_char
+= produced_chars
;
1838 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1840 /* Emacs' internal format for representation of multiple character
1841 sets is a kind of multi-byte encoding, i.e. characters are
1842 represented by variable-length sequences of one-byte codes.
1844 ASCII characters and control characters (e.g. `tab', `newline') are
1845 represented by one-byte sequences which are their ASCII codes, in
1846 the range 0x00 through 0x7F.
1848 8-bit characters of the range 0x80..0x9F are represented by
1849 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1852 8-bit characters of the range 0xA0..0xFF are represented by
1853 one-byte sequences which are their 8-bit code.
1855 The other characters are represented by a sequence of `base
1856 leading-code', optional `extended leading-code', and one or two
1857 `position-code's. The length of the sequence is determined by the
1858 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1859 whereas extended leading-code and position-code take the range 0xA0
1860 through 0xFF. See `charset.h' for more details about leading-code
1863 --- CODE RANGE of Emacs' internal format ---
1867 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1868 eight-bit-graphic 0xA0..0xBF
1869 ELSE 0x81..0x9D + [0xA0..0xFF]+
1870 ---------------------------------------------
1872 As this is the internal character representation, the format is
1873 usually not used externally (i.e. in a file or in a data sent to a
1874 process). But, it is possible to have a text externally in this
1875 format (i.e. by encoding by the coding system `emacs-mule').
1877 In that case, a sequence of one-byte codes has a slightly different
1880 At first, all characters in eight-bit-control are represented by
1881 one-byte sequences which are their 8-bit code.
1883 Next, character composition data are represented by the byte
1884 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1886 METHOD is 0xF2 plus one of composition method (enum
1887 composition_method),
1889 BYTES is 0xA0 plus a byte length of this composition data,
1891 CHARS is 0xA0 plus a number of characters composed by this
1894 COMPONENTs are characters of multibyte form or composition
1895 rules encoded by two-byte of ASCII codes.
1897 In addition, for backward compatibility, the following formats are
1898 also recognized as composition data on decoding.
1901 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1904 MSEQ is a multibyte form but in these special format:
1905 ASCII: 0xA0 ASCII_CODE+0x80,
1906 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1907 RULE is a one byte code of the range 0xA0..0xF0 that
1908 represents a composition rule.
1911 char emacs_mule_bytes
[256];
1914 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1915 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1919 detect_coding_emacs_mule (struct coding_system
*coding
,
1920 struct coding_detection_info
*detect_info
)
1922 const unsigned char *src
= coding
->source
, *src_base
;
1923 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1924 int multibytep
= coding
->src_multibyte
;
1925 EMACS_INT consumed_chars
= 0;
1929 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1930 /* A coding system of this category is always ASCII compatible. */
1931 src
+= coding
->head_ascii
;
1941 /* Perhaps the start of composite character. We simply skip
1942 it because analyzing it is too heavy for detecting. But,
1943 at least, we check that the composite character
1944 constitutes of more than 4 bytes. */
1945 const unsigned char *src_start
;
1955 if (src
- src_start
<= 4)
1957 found
= CATEGORY_MASK_EMACS_MULE
;
1965 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1970 int more_bytes
= emacs_mule_bytes
[c
] - 1;
1972 while (more_bytes
> 0)
1977 src
--; /* Unread the last byte. */
1982 if (more_bytes
!= 0)
1984 found
= CATEGORY_MASK_EMACS_MULE
;
1987 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1991 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1993 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1996 detect_info
->found
|= found
;
2001 /* Parse emacs-mule multibyte sequence at SRC and return the decoded
2002 character. If CMP_STATUS indicates that we must expect MSEQ or
2003 RULE described above, decode it and return the negative value of
2004 the decoded character or rule. If an invalid byte is found, return
2005 -1. If SRC is too short, return -2. */
2008 emacs_mule_char (struct coding_system
*coding
, const unsigned char *src
,
2009 int *nbytes
, int *nchars
, int *id
,
2010 struct composition_status
*cmp_status
)
2012 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2013 const unsigned char *src_base
= src
;
2014 int multibytep
= coding
->src_multibyte
;
2018 int consumed_chars
= 0;
2025 charset_ID
= emacs_mule_charset
[0];
2031 if (cmp_status
->state
!= COMPOSING_NO
2032 && cmp_status
->old_form
)
2034 if (cmp_status
->state
== COMPOSING_CHAR
)
2049 *nbytes
= src
- src_base
;
2050 *nchars
= consumed_chars
;
2058 switch (emacs_mule_bytes
[c
])
2061 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2070 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
2071 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
2074 if (c
< 0xA0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2083 if ((charset_ID
= emacs_mule_charset
[c
]) < 0)
2088 code
= (c
& 0x7F) << 8;
2098 if (c
< 0 || (charset_ID
= emacs_mule_charset
[c
]) < 0)
2103 code
= (c
& 0x7F) << 8;
2112 charset_ID
= ASCII_BYTE_P (code
) ? charset_ascii
: charset_eight_bit
;
2118 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
2119 CHARSET_FROM_ID (charset_ID
), code
, c
);
2123 *nbytes
= src
- src_base
;
2124 *nchars
= consumed_chars
;
2127 return (mseq_found
? -c
: c
);
2137 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2139 /* Handle these composition sequence ('|': the end of header elements,
2140 BYTES and CHARS >= 0xA0):
2142 (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ...
2143 (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ...
2144 (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ...
2148 (4) relative composition: 0x80 | MSEQ ... MSEQ
2149 (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ
2151 When the starter 0x80 and the following header elements are found,
2152 this annotation header is produced.
2154 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ]
2156 NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2157 NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5).
2159 Then, upon reading the following elements, these codes are produced
2160 until the composition end is found:
2163 (2) ALT ... ALT CHAR ... CHAR
2164 (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR
2166 (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
2168 When the composition end is found, LENGTH and NCHARS in the
2169 annotation header is updated as below:
2171 (1) LENGTH: unchanged, NCHARS: unchanged
2172 (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2173 (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged
2174 (4) LENGTH: unchanged, NCHARS: number of CHARs
2175 (5) LENGTH: unchanged, NCHARS: number of CHARs
2177 If an error is found while composing, the annotation header is
2178 changed to the original composition header (plus filler -1s) as
2181 (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ]
2182 (5) [ 0x80 0xFF -1 -1- -1 ]
2184 and the sequence [ -2 DECODED-RULE ] is changed to the original
2185 byte sequence as below:
2186 o the original byte sequence is B: [ B -1 ]
2187 o the original byte sequence is B1 B2: [ B1 B2 ]
2189 Most of the routines are implemented by macros because many
2190 variables and labels in the caller decode_coding_emacs_mule must be
2191 accessible, and they are usually called just once (thus doesn't
2192 increase the size of compiled object). */
2194 /* Decode a composition rule represented by C as a component of
2195 composition sequence of Emacs 20 style. Set RULE to the decoded
2198 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \
2203 if (c < 0 || c >= 81) \
2204 goto invalid_code; \
2205 gref = c / 9, nref = c % 9; \
2206 if (gref == 4) gref = 10; \
2207 if (nref == 4) nref = 10; \
2208 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2212 /* Decode a composition rule represented by C and the following byte
2213 at SRC as a component of composition sequence of Emacs 21 style.
2214 Set RULE to the decoded rule. */
2216 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \
2221 if (gref < 0 || gref >= 81) \
2222 goto invalid_code; \
2223 ONE_MORE_BYTE (c); \
2225 if (nref < 0 || nref >= 81) \
2226 goto invalid_code; \
2227 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
2231 /* Start of Emacs 21 style format. The first three bytes at SRC are
2232 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the
2233 byte length of this composition information, CHARS is the number of
2234 characters composed by this composition. */
2236 #define DECODE_EMACS_MULE_21_COMPOSITION() \
2238 enum composition_method method = c - 0xF2; \
2239 int nbytes, nchars; \
2241 ONE_MORE_BYTE (c); \
2243 goto invalid_code; \
2244 nbytes = c - 0xA0; \
2245 if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \
2246 goto invalid_code; \
2247 ONE_MORE_BYTE (c); \
2248 nchars = c - 0xA0; \
2249 if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \
2250 goto invalid_code; \
2251 cmp_status->old_form = 0; \
2252 cmp_status->method = method; \
2253 if (method == COMPOSITION_RELATIVE) \
2254 cmp_status->state = COMPOSING_CHAR; \
2256 cmp_status->state = COMPOSING_COMPONENT_CHAR; \
2257 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2258 cmp_status->nchars = nchars; \
2259 cmp_status->ncomps = nbytes - 4; \
2260 ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \
2264 /* Start of Emacs 20 style format for relative composition. */
2266 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \
2268 cmp_status->old_form = 1; \
2269 cmp_status->method = COMPOSITION_RELATIVE; \
2270 cmp_status->state = COMPOSING_CHAR; \
2271 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2272 cmp_status->nchars = cmp_status->ncomps = 0; \
2273 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2277 /* Start of Emacs 20 style format for rule-base composition. */
2279 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \
2281 cmp_status->old_form = 1; \
2282 cmp_status->method = COMPOSITION_WITH_RULE; \
2283 cmp_status->state = COMPOSING_CHAR; \
2284 cmp_status->length = MAX_ANNOTATION_LENGTH; \
2285 cmp_status->nchars = cmp_status->ncomps = 0; \
2286 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
2290 #define DECODE_EMACS_MULE_COMPOSITION_START() \
2292 const unsigned char *current_src = src; \
2294 ONE_MORE_BYTE (c); \
2296 goto invalid_code; \
2297 if (c - 0xF2 >= COMPOSITION_RELATIVE \
2298 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \
2299 DECODE_EMACS_MULE_21_COMPOSITION (); \
2300 else if (c < 0xA0) \
2301 goto invalid_code; \
2302 else if (c < 0xC0) \
2304 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \
2305 /* Re-read C as a composition component. */ \
2306 src = current_src; \
2308 else if (c == 0xFF) \
2309 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \
2311 goto invalid_code; \
2314 #define EMACS_MULE_COMPOSITION_END() \
2316 int idx = - cmp_status->length; \
2318 if (cmp_status->old_form) \
2319 charbuf[idx + 2] = cmp_status->nchars; \
2320 else if (cmp_status->method > COMPOSITION_RELATIVE) \
2321 charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \
2322 cmp_status->state = COMPOSING_NO; \
2327 emacs_mule_finish_composition (int *charbuf
,
2328 struct composition_status
*cmp_status
)
2330 int idx
= - cmp_status
->length
;
2333 if (cmp_status
->old_form
&& cmp_status
->nchars
> 0)
2335 charbuf
[idx
+ 2] = cmp_status
->nchars
;
2337 if (cmp_status
->method
== COMPOSITION_WITH_RULE
2338 && cmp_status
->state
== COMPOSING_CHAR
)
2340 /* The last rule was invalid. */
2341 int rule
= charbuf
[-1] + 0xA0;
2343 charbuf
[-2] = BYTE8_TO_CHAR (rule
);
2350 charbuf
[idx
++] = BYTE8_TO_CHAR (0x80);
2352 if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2354 charbuf
[idx
++] = BYTE8_TO_CHAR (0xFF);
2355 charbuf
[idx
++] = -3;
2361 int nchars
= charbuf
[idx
+ 1] + 0xA0;
2362 int nbytes
= charbuf
[idx
+ 2] + 0xA0;
2364 charbuf
[idx
++] = BYTE8_TO_CHAR (0xF2 + cmp_status
->method
);
2365 charbuf
[idx
++] = BYTE8_TO_CHAR (nbytes
);
2366 charbuf
[idx
++] = BYTE8_TO_CHAR (nchars
);
2367 charbuf
[idx
++] = -1;
2371 cmp_status
->state
= COMPOSING_NO
;
2375 #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \
2377 if (cmp_status->state != COMPOSING_NO) \
2378 char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \
2383 decode_coding_emacs_mule (struct coding_system
*coding
)
2385 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2386 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2387 const unsigned char *src_base
;
2388 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2389 /* We may produce two annotations (charset and composition) in one
2390 loop and one more charset annotation at the end. */
2392 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3)
2393 /* We can produce up to 2 characters in a loop. */
2395 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
2396 int multibytep
= coding
->src_multibyte
;
2397 EMACS_INT char_offset
= coding
->produced_char
;
2398 EMACS_INT last_offset
= char_offset
;
2399 int last_id
= charset_ascii
;
2401 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2402 int byte_after_cr
= -1;
2403 struct composition_status
*cmp_status
= &coding
->spec
.emacs_mule
.cmp_status
;
2405 if (cmp_status
->state
!= COMPOSING_NO
)
2409 if (charbuf_end
- charbuf
< cmp_status
->length
)
2411 for (i
= 0; i
< cmp_status
->length
; i
++)
2412 *charbuf
++ = cmp_status
->carryover
[i
];
2413 coding
->annotated
= 1;
2418 int c
, id
IF_LINT (= 0);
2421 consumed_chars_base
= consumed_chars
;
2423 if (charbuf
>= charbuf_end
)
2425 if (byte_after_cr
>= 0)
2430 if (byte_after_cr
>= 0)
2431 c
= byte_after_cr
, byte_after_cr
= -1;
2435 if (c
< 0 || c
== 0x80)
2437 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2444 DECODE_EMACS_MULE_COMPOSITION_START ();
2450 if (eol_dos
&& c
== '\r')
2451 ONE_MORE_BYTE (byte_after_cr
);
2453 if (cmp_status
->state
!= COMPOSING_NO
)
2455 if (cmp_status
->old_form
)
2456 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2457 else if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2458 cmp_status
->ncomps
--;
2463 int nchars
IF_LINT (= 0), nbytes
IF_LINT (= 0);
2464 /* emacs_mule_char can load a charset map from a file, which
2465 allocates a large structure and might cause buffer text
2466 to be relocated as result. Thus, we need to remember the
2467 original pointer to buffer text, and fix up all related
2468 pointers after the call. */
2469 const unsigned char *orig
= coding
->source
;
2472 c
= emacs_mule_char (coding
, src_base
, &nbytes
, &nchars
, &id
,
2474 offset
= coding
->source
- orig
;
2488 src
= src_base
+ nbytes
;
2489 consumed_chars
= consumed_chars_base
+ nchars
;
2490 if (cmp_status
->state
>= COMPOSING_COMPONENT_CHAR
)
2491 cmp_status
->ncomps
-= nchars
;
2494 /* Now if C >= 0, we found a normally encoded character, if C <
2495 0, we found an old-style composition component character or
2498 if (cmp_status
->state
== COMPOSING_NO
)
2502 if (last_id
!= charset_ascii
)
2503 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
,
2506 last_offset
= char_offset
;
2511 else if (cmp_status
->state
== COMPOSING_CHAR
)
2513 if (cmp_status
->old_form
)
2517 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2524 cmp_status
->nchars
++;
2525 cmp_status
->length
++;
2526 if (cmp_status
->nchars
== MAX_COMPOSITION_COMPONENTS
)
2527 EMACS_MULE_COMPOSITION_END ();
2528 else if (cmp_status
->method
== COMPOSITION_WITH_RULE
)
2529 cmp_status
->state
= COMPOSING_RULE
;
2535 cmp_status
->length
++;
2536 cmp_status
->nchars
--;
2537 if (cmp_status
->nchars
== 0)
2538 EMACS_MULE_COMPOSITION_END ();
2541 else if (cmp_status
->state
== COMPOSING_RULE
)
2547 EMACS_MULE_COMPOSITION_END ();
2554 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c
, rule
);
2559 cmp_status
->length
+= 2;
2560 cmp_status
->state
= COMPOSING_CHAR
;
2563 else if (cmp_status
->state
== COMPOSING_COMPONENT_CHAR
)
2566 cmp_status
->length
++;
2567 if (cmp_status
->ncomps
== 0)
2568 cmp_status
->state
= COMPOSING_CHAR
;
2569 else if (cmp_status
->ncomps
> 0)
2571 if (cmp_status
->method
== COMPOSITION_WITH_RULE_ALTCHARS
)
2572 cmp_status
->state
= COMPOSING_COMPONENT_RULE
;
2575 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2577 else /* COMPOSING_COMPONENT_RULE */
2581 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c
, rule
);
2586 cmp_status
->length
+= 2;
2587 cmp_status
->ncomps
--;
2588 if (cmp_status
->ncomps
> 0)
2589 cmp_status
->state
= COMPOSING_COMPONENT_CHAR
;
2591 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2596 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2598 consumed_chars
= consumed_chars_base
;
2600 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2606 if (cmp_status
->state
!= COMPOSING_NO
)
2608 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
2609 EMACS_MULE_MAYBE_FINISH_COMPOSITION ();
2614 charbuf
-= cmp_status
->length
;
2615 for (i
= 0; i
< cmp_status
->length
; i
++)
2616 cmp_status
->carryover
[i
] = charbuf
[i
];
2619 if (last_id
!= charset_ascii
)
2620 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2621 coding
->consumed_char
+= consumed_chars_base
;
2622 coding
->consumed
= src_base
- coding
->source
;
2623 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2627 #define EMACS_MULE_LEADING_CODES(id, codes) \
2630 codes[0] = id, codes[1] = 0; \
2631 else if (id < 0xE0) \
2632 codes[0] = 0x9A, codes[1] = id; \
2633 else if (id < 0xF0) \
2634 codes[0] = 0x9B, codes[1] = id; \
2635 else if (id < 0xF5) \
2636 codes[0] = 0x9C, codes[1] = id; \
2638 codes[0] = 0x9D, codes[1] = id; \
2643 encode_coding_emacs_mule (struct coding_system
*coding
)
2645 int multibytep
= coding
->dst_multibyte
;
2646 int *charbuf
= coding
->charbuf
;
2647 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2648 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2649 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2651 EMACS_INT produced_chars
= 0;
2652 Lisp_Object attrs
, charset_list
;
2654 int preferred_charset_id
= -1;
2656 CODING_GET_INFO (coding
, attrs
, charset_list
);
2657 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2659 CODING_ATTR_CHARSET_LIST (attrs
)
2660 = charset_list
= Vemacs_mule_charset_list
;
2663 while (charbuf
< charbuf_end
)
2665 ASSURE_DESTINATION (safe_room
);
2670 /* Handle an annotation. */
2673 case CODING_ANNOTATE_COMPOSITION_MASK
:
2674 /* Not yet implemented. */
2676 case CODING_ANNOTATE_CHARSET_MASK
:
2677 preferred_charset_id
= charbuf
[3];
2678 if (preferred_charset_id
>= 0
2679 && NILP (Fmemq (make_number (preferred_charset_id
),
2681 preferred_charset_id
= -1;
2690 if (ASCII_CHAR_P (c
))
2691 EMIT_ONE_ASCII_BYTE (c
);
2692 else if (CHAR_BYTE8_P (c
))
2694 c
= CHAR_TO_BYTE8 (c
);
2699 struct charset
*charset
;
2703 unsigned char leading_codes
[2];
2705 if (preferred_charset_id
>= 0)
2709 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2710 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
2712 code
= ENCODE_CHAR (charset
, c
);
2714 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2718 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2722 c
= coding
->default_char
;
2723 if (ASCII_CHAR_P (c
))
2725 EMIT_ONE_ASCII_BYTE (c
);
2728 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
2731 dimension
= CHARSET_DIMENSION (charset
);
2732 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2733 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2734 EMIT_ONE_BYTE (leading_codes
[0]);
2735 if (leading_codes
[1])
2736 EMIT_ONE_BYTE (leading_codes
[1]);
2738 EMIT_ONE_BYTE (code
| 0x80);
2742 EMIT_ONE_BYTE (code
>> 8);
2743 EMIT_ONE_BYTE (code
& 0xFF);
2747 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2748 coding
->produced_char
+= produced_chars
;
2749 coding
->produced
= dst
- coding
->destination
;
2754 /*** 7. ISO2022 handlers ***/
2756 /* The following note describes the coding system ISO2022 briefly.
2757 Since the intention of this note is to help understand the
2758 functions in this file, some parts are NOT ACCURATE or are OVERLY
2759 SIMPLIFIED. For thorough understanding, please refer to the
2760 original document of ISO2022. This is equivalent to the standard
2761 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2763 ISO2022 provides many mechanisms to encode several character sets
2764 in 7-bit and 8-bit environments. For 7-bit environments, all text
2765 is encoded using bytes less than 128. This may make the encoded
2766 text a little bit longer, but the text passes more easily through
2767 several types of gateway, some of which strip off the MSB (Most
2770 There are two kinds of character sets: control character sets and
2771 graphic character sets. The former contain control characters such
2772 as `newline' and `escape' to provide control functions (control
2773 functions are also provided by escape sequences). The latter
2774 contain graphic characters such as 'A' and '-'. Emacs recognizes
2775 two control character sets and many graphic character sets.
2777 Graphic character sets are classified into one of the following
2778 four classes, according to the number of bytes (DIMENSION) and
2779 number of characters in one dimension (CHARS) of the set:
2780 - DIMENSION1_CHARS94
2781 - DIMENSION1_CHARS96
2782 - DIMENSION2_CHARS94
2783 - DIMENSION2_CHARS96
2785 In addition, each character set is assigned an identification tag,
2786 unique for each set, called the "final character" (denoted as <F>
2787 hereafter). The <F> of each character set is decided by ECMA(*)
2788 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2789 (0x30..0x3F are for private use only).
2791 Note (*): ECMA = European Computer Manufacturers Association
2793 Here are examples of graphic character sets [NAME(<F>)]:
2794 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2795 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2796 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2797 o DIMENSION2_CHARS96 -- none for the moment
2799 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2800 C0 [0x00..0x1F] -- control character plane 0
2801 GL [0x20..0x7F] -- graphic character plane 0
2802 C1 [0x80..0x9F] -- control character plane 1
2803 GR [0xA0..0xFF] -- graphic character plane 1
2805 A control character set is directly designated and invoked to C0 or
2806 C1 by an escape sequence. The most common case is that:
2807 - ISO646's control character set is designated/invoked to C0, and
2808 - ISO6429's control character set is designated/invoked to C1,
2809 and usually these designations/invocations are omitted in encoded
2810 text. In a 7-bit environment, only C0 can be used, and a control
2811 character for C1 is encoded by an appropriate escape sequence to
2812 fit into the environment. All control characters for C1 are
2813 defined to have corresponding escape sequences.
2815 A graphic character set is at first designated to one of four
2816 graphic registers (G0 through G3), then these graphic registers are
2817 invoked to GL or GR. These designations and invocations can be
2818 done independently. The most common case is that G0 is invoked to
2819 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2820 these invocations and designations are omitted in encoded text.
2821 In a 7-bit environment, only GL can be used.
2823 When a graphic character set of CHARS94 is invoked to GL, codes
2824 0x20 and 0x7F of the GL area work as control characters SPACE and
2825 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2828 There are two ways of invocation: locking-shift and single-shift.
2829 With locking-shift, the invocation lasts until the next different
2830 invocation, whereas with single-shift, the invocation affects the
2831 following character only and doesn't affect the locking-shift
2832 state. Invocations are done by the following control characters or
2835 ----------------------------------------------------------------------
2836 abbrev function cntrl escape seq description
2837 ----------------------------------------------------------------------
2838 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2839 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2840 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2841 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2842 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2843 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2844 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2845 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2846 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2847 ----------------------------------------------------------------------
2848 (*) These are not used by any known coding system.
2850 Control characters for these functions are defined by macros
2851 ISO_CODE_XXX in `coding.h'.
2853 Designations are done by the following escape sequences:
2854 ----------------------------------------------------------------------
2855 escape sequence description
2856 ----------------------------------------------------------------------
2857 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2858 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2859 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2860 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2861 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2862 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2863 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2864 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2865 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2866 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2867 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2868 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2869 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2870 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2871 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2872 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2873 ----------------------------------------------------------------------
2875 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2876 of dimension 1, chars 94, and final character <F>, etc...
2878 Note (*): Although these designations are not allowed in ISO2022,
2879 Emacs accepts them on decoding, and produces them on encoding
2880 CHARS96 character sets in a coding system which is characterized as
2881 7-bit environment, non-locking-shift, and non-single-shift.
2883 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2884 '(' must be omitted. We refer to this as "short-form" hereafter.
2886 Now you may notice that there are a lot of ways of encoding the
2887 same multilingual text in ISO2022. Actually, there exist many
2888 coding systems such as Compound Text (used in X11's inter client
2889 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2890 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2891 localized platforms), and all of these are variants of ISO2022.
2893 In addition to the above, Emacs handles two more kinds of escape
2894 sequences: ISO6429's direction specification and Emacs' private
2895 sequence for specifying character composition.
2897 ISO6429's direction specification takes the following form:
2898 o CSI ']' -- end of the current direction
2899 o CSI '0' ']' -- end of the current direction
2900 o CSI '1' ']' -- start of left-to-right text
2901 o CSI '2' ']' -- start of right-to-left text
2902 The control character CSI (0x9B: control sequence introducer) is
2903 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2905 Character composition specification takes the following form:
2906 o ESC '0' -- start relative composition
2907 o ESC '1' -- end composition
2908 o ESC '2' -- start rule-base composition (*)
2909 o ESC '3' -- start relative composition with alternate chars (**)
2910 o ESC '4' -- start rule-base composition with alternate chars (**)
2911 Since these are not standard escape sequences of any ISO standard,
2912 the use of them with these meanings is restricted to Emacs only.
2914 (*) This form is used only in Emacs 20.7 and older versions,
2915 but newer versions can safely decode it.
2916 (**) This form is used only in Emacs 21.1 and newer versions,
2917 and older versions can't decode it.
2919 Here's a list of example usages of these composition escape
2920 sequences (categorized by `enum composition_method').
2922 COMPOSITION_RELATIVE:
2923 ESC 0 CHAR [ CHAR ] ESC 1
2924 COMPOSITION_WITH_RULE:
2925 ESC 2 CHAR [ RULE CHAR ] ESC 1
2926 COMPOSITION_WITH_ALTCHARS:
2927 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2928 COMPOSITION_WITH_RULE_ALTCHARS:
2929 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2931 static enum iso_code_class_type iso_code_class
[256];
2933 #define SAFE_CHARSET_P(coding, id) \
2934 ((id) <= (coding)->max_charset_id \
2935 && (coding)->safe_charsets[id] != 255)
2938 setup_iso_safe_charsets (Lisp_Object attrs
)
2940 Lisp_Object charset_list
, safe_charsets
;
2941 Lisp_Object request
;
2942 Lisp_Object reg_usage
;
2945 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2948 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2949 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2950 && ! EQ (charset_list
, Viso_2022_charset_list
))
2952 CODING_ATTR_CHARSET_LIST (attrs
)
2953 = charset_list
= Viso_2022_charset_list
;
2954 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2957 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2961 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2963 int id
= XINT (XCAR (tail
));
2964 if (max_charset_id
< id
)
2965 max_charset_id
= id
;
2968 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
2969 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
2970 request
= AREF (attrs
, coding_attr_iso_request
);
2971 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2972 reg94
= XINT (XCAR (reg_usage
));
2973 reg96
= XINT (XCDR (reg_usage
));
2975 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2979 struct charset
*charset
;
2982 charset
= CHARSET_FROM_ID (XINT (id
));
2983 reg
= Fcdr (Fassq (id
, request
));
2985 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2986 else if (charset
->iso_chars_96
)
2989 SSET (safe_charsets
, XINT (id
), reg96
);
2994 SSET (safe_charsets
, XINT (id
), reg94
);
2997 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
3001 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3002 Check if a text is encoded in one of ISO-2022 based coding systems.
3003 If it is, return 1, else return 0. */
3006 detect_coding_iso_2022 (struct coding_system
*coding
,
3007 struct coding_detection_info
*detect_info
)
3009 const unsigned char *src
= coding
->source
, *src_base
= src
;
3010 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3011 int multibytep
= coding
->src_multibyte
;
3012 int single_shifting
= 0;
3015 EMACS_INT consumed_chars
= 0;
3019 int composition_count
= -1;
3021 detect_info
->checked
|= CATEGORY_MASK_ISO
;
3023 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
3025 struct coding_system
*this = &(coding_categories
[i
]);
3026 Lisp_Object attrs
, val
;
3030 attrs
= CODING_ID_ATTRS (this->id
);
3031 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
3032 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Viso_2022_charset_list
))
3033 setup_iso_safe_charsets (attrs
);
3034 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
3035 this->max_charset_id
= SCHARS (val
) - 1;
3036 this->safe_charsets
= SDATA (val
);
3039 /* A coding system of this category is always ASCII compatible. */
3040 src
+= coding
->head_ascii
;
3042 while (rejected
!= CATEGORY_MASK_ISO
)
3049 if (inhibit_iso_escape_detection
)
3051 single_shifting
= 0;
3053 if (c
== 'N' || c
== 'O')
3055 /* ESC <Fe> for SS2 or SS3. */
3056 single_shifting
= 1;
3057 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3061 /* End of composition. */
3062 if (composition_count
< 0
3063 || composition_count
> MAX_COMPOSITION_COMPONENTS
)
3066 composition_count
= -1;
3067 found
|= CATEGORY_MASK_ISO
;
3069 else if (c
>= '0' && c
<= '4')
3071 /* ESC <Fp> for start/end composition. */
3072 composition_count
= 0;
3076 if (c
>= '(' && c
<= '/')
3078 /* Designation sequence for a charset of dimension 1. */
3080 if (c1
< ' ' || c1
>= 0x80
3081 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
3082 /* Invalid designation sequence. Just ignore. */
3087 /* Designation sequence for a charset of dimension 2. */
3089 if (c
>= '@' && c
<= 'B')
3090 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
3091 id
= iso_charset_table
[1][0][c
];
3092 else if (c
>= '(' && c
<= '/')
3095 if (c1
< ' ' || c1
>= 0x80
3096 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
3097 /* Invalid designation sequence. Just ignore. */
3101 /* Invalid designation sequence. Just ignore it. */
3106 /* Invalid escape sequence. Just ignore it. */
3110 /* We found a valid designation sequence for CHARSET. */
3111 rejected
|= CATEGORY_MASK_ISO_8BIT
;
3112 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
3114 found
|= CATEGORY_MASK_ISO_7
;
3116 rejected
|= CATEGORY_MASK_ISO_7
;
3117 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
3119 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
3121 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
3122 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
3124 found
|= CATEGORY_MASK_ISO_7_ELSE
;
3126 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
3127 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
3129 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3131 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
3137 /* Locking shift out/in. */
3138 if (inhibit_iso_escape_detection
)
3140 single_shifting
= 0;
3141 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
3145 /* Control sequence introducer. */
3146 single_shifting
= 0;
3147 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3148 found
|= CATEGORY_MASK_ISO_8_ELSE
;
3149 goto check_extra_latin
;
3154 if (inhibit_iso_escape_detection
)
3156 single_shifting
= 0;
3157 rejected
|= CATEGORY_MASK_ISO_7BIT
;
3158 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3159 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3161 found
|= CATEGORY_MASK_ISO_8_1
;
3162 single_shifting
= 1;
3164 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
3165 & CODING_ISO_FLAG_SINGLE_SHIFT
)
3167 found
|= CATEGORY_MASK_ISO_8_2
;
3168 single_shifting
= 1;
3170 if (single_shifting
)
3173 if (! VECTORP (Vlatin_extra_code_table
)
3174 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3176 rejected
= CATEGORY_MASK_ISO
;
3179 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
3180 & CODING_ISO_FLAG_LATIN_EXTRA
)
3181 found
|= CATEGORY_MASK_ISO_8_1
;
3183 rejected
|= CATEGORY_MASK_ISO_8_1
;
3184 rejected
|= CATEGORY_MASK_ISO_8_2
;
3192 if (composition_count
>= 0)
3193 composition_count
++;
3194 single_shifting
= 0;
3199 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
3200 found
|= CATEGORY_MASK_ISO_8_1
;
3201 /* Check the length of succeeding codes of the range
3202 0xA0..0FF. If the byte length is even, we include
3203 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
3204 only when we are not single shifting. */
3205 if (! single_shifting
3206 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
3209 while (src
< src_end
)
3221 if (len
& 1 && src
< src_end
)
3223 rejected
|= CATEGORY_MASK_ISO_8_2
;
3224 if (composition_count
>= 0)
3225 composition_count
+= len
;
3229 found
|= CATEGORY_MASK_ISO_8_2
;
3230 if (composition_count
>= 0)
3231 composition_count
+= len
/ 2;
3238 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
3242 detect_info
->rejected
|= rejected
;
3243 detect_info
->found
|= (found
& ~rejected
);
3248 /* Set designation state into CODING. Set CHARS_96 to -1 if the
3249 escape sequence should be kept. */
3250 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
3254 if (final < '0' || final >= 128 \
3255 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
3256 || !SAFE_CHARSET_P (coding, id)) \
3258 CODING_ISO_DESIGNATION (coding, reg) = -2; \
3262 prev = CODING_ISO_DESIGNATION (coding, reg); \
3263 if (id == charset_jisx0201_roman) \
3265 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3266 id = charset_ascii; \
3268 else if (id == charset_jisx0208_1978) \
3270 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3271 id = charset_jisx0208; \
3273 CODING_ISO_DESIGNATION (coding, reg) = id; \
3274 /* If there was an invalid designation to REG previously, and this \
3275 designation is ASCII to REG, we should keep this designation \
3277 if (prev == -2 && id == charset_ascii) \
3282 /* Handle these composition sequence (ALT: alternate char):
3284 (1) relative composition: ESC 0 CHAR ... ESC 1
3285 (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3286 (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1
3287 (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1
3289 When the start sequence (ESC 0/2/3/4) is found, this annotation
3292 [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ]
3294 Then, upon reading CHAR or RULE (one or two bytes), these codes are
3295 produced until the end sequence (ESC 1) is found:
3298 (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR
3299 (3) ALT ... ALT -1 -1 CHAR ... CHAR
3300 (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR
3302 When the end sequence (ESC 1) is found, LENGTH and NCHARS in the
3303 annotation header is updated as below:
3305 (1) LENGTH: unchanged, NCHARS: number of CHARs
3306 (2) LENGTH: unchanged, NCHARS: number of CHARs
3307 (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs
3308 (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs
3310 If an error is found while composing, the annotation header is
3313 [ ESC '0'/'2'/'3'/'4' -2 0 ]
3315 and the sequence [ -2 DECODED-RULE ] is changed to the original
3316 byte sequence as below:
3317 o the original byte sequence is B: [ B -1 ]
3318 o the original byte sequence is B1 B2: [ B1 B2 ]
3319 and the sequence [ -1 -1 ] is changed to the original byte
3324 /* Decode a composition rule C1 and maybe one more byte from the
3325 source, and set RULE to the encoded composition rule. If the rule
3326 is invalid, goto invalid_code. */
3328 #define DECODE_COMPOSITION_RULE(rule) \
3332 goto invalid_code; \
3333 if (rule < 81) /* old format (before ver.21) */ \
3335 int gref = (rule) / 9; \
3336 int nref = (rule) % 9; \
3337 if (gref == 4) gref = 10; \
3338 if (nref == 4) nref = 10; \
3339 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
3341 else /* new format (after ver.21) */ \
3345 ONE_MORE_BYTE (b); \
3346 if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \
3347 goto invalid_code; \
3348 rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \
3349 rule += 0x100; /* Distinguish it from the old format. */ \
3353 #define ENCODE_COMPOSITION_RULE(rule) \
3355 int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \
3357 if (rule < 0x100) /* old format */ \
3359 if (gref == 10) gref = 4; \
3360 if (nref == 10) nref = 4; \
3361 charbuf[idx] = 32 + gref * 9 + nref; \
3362 charbuf[idx + 1] = -1; \
3365 else /* new format */ \
3367 charbuf[idx] = 32 + 81 + gref; \
3368 charbuf[idx + 1] = 32 + nref; \
3373 /* Finish the current composition as invalid. */
3375 static int finish_composition (int *, struct composition_status
*);
3378 finish_composition (int *charbuf
, struct composition_status
*cmp_status
)
3380 int idx
= - cmp_status
->length
;
3383 /* Recover the original ESC sequence */
3384 charbuf
[idx
++] = ISO_CODE_ESC
;
3385 charbuf
[idx
++] = (cmp_status
->method
== COMPOSITION_RELATIVE
? '0'
3386 : cmp_status
->method
== COMPOSITION_WITH_RULE
? '2'
3387 : cmp_status
->method
== COMPOSITION_WITH_ALTCHARS
? '3'
3388 /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */
3390 charbuf
[idx
++] = -2;
3392 charbuf
[idx
++] = -1;
3393 new_chars
= cmp_status
->nchars
;
3394 if (cmp_status
->method
>= COMPOSITION_WITH_RULE
)
3395 for (; idx
< 0; idx
++)
3397 int elt
= charbuf
[idx
];
3401 ENCODE_COMPOSITION_RULE (charbuf
[idx
+ 1]);
3406 charbuf
[idx
++] = ISO_CODE_ESC
;
3411 cmp_status
->state
= COMPOSING_NO
;
3415 /* If characters are under composition, finish the composition. */
3416 #define MAYBE_FINISH_COMPOSITION() \
3418 if (cmp_status->state != COMPOSING_NO) \
3419 char_offset += finish_composition (charbuf, cmp_status); \
3422 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
3424 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
3425 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
3426 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
3427 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
3429 Produce this annotation sequence now:
3431 [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ]
3434 #define DECODE_COMPOSITION_START(c1) \
3437 && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \
3438 && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3439 || (cmp_status->state == COMPOSING_COMPONENT_RULE \
3440 && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \
3444 cmp_status->state = COMPOSING_CHAR; \
3445 cmp_status->length += 2; \
3449 MAYBE_FINISH_COMPOSITION (); \
3450 cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \
3451 : c1 == '2' ? COMPOSITION_WITH_RULE \
3452 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
3453 : COMPOSITION_WITH_RULE_ALTCHARS); \
3455 = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \
3456 ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \
3457 cmp_status->length = MAX_ANNOTATION_LENGTH; \
3458 cmp_status->nchars = cmp_status->ncomps = 0; \
3459 coding->annotated = 1; \
3464 /* Handle composition end sequence ESC 1. */
3466 #define DECODE_COMPOSITION_END() \
3468 if (cmp_status->nchars == 0 \
3469 || ((cmp_status->state == COMPOSING_CHAR) \
3470 == (cmp_status->method == COMPOSITION_WITH_RULE))) \
3472 MAYBE_FINISH_COMPOSITION (); \
3473 goto invalid_code; \
3475 if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \
3476 charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \
3477 else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \
3478 charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \
3479 charbuf[- cmp_status->length + 2] = cmp_status->nchars; \
3480 char_offset += cmp_status->nchars; \
3481 cmp_status->state = COMPOSING_NO; \
3484 /* Store a composition rule RULE in charbuf, and update cmp_status. */
3486 #define STORE_COMPOSITION_RULE(rule) \
3489 *charbuf++ = rule; \
3490 cmp_status->length += 2; \
3491 cmp_status->state--; \
3494 /* Store a composed char or a component char C in charbuf, and update
3497 #define STORE_COMPOSITION_CHAR(c) \
3500 cmp_status->length++; \
3501 if (cmp_status->state == COMPOSING_CHAR) \
3502 cmp_status->nchars++; \
3504 cmp_status->ncomps++; \
3505 if (cmp_status->method == COMPOSITION_WITH_RULE \
3506 || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \
3507 && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \
3508 cmp_status->state++; \
3512 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3515 decode_coding_iso_2022 (struct coding_system
*coding
)
3517 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3518 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3519 const unsigned char *src_base
;
3520 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3521 /* We may produce two annotations (charset and composition) in one
3522 loop and one more charset annotation at the end. */
3524 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 3);
3525 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
3526 int multibytep
= coding
->src_multibyte
;
3527 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3528 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3529 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3530 int charset_id_2
, charset_id_3
;
3531 struct charset
*charset
;
3533 struct composition_status
*cmp_status
= CODING_ISO_CMP_STATUS (coding
);
3534 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
3535 EMACS_INT char_offset
= coding
->produced_char
;
3536 EMACS_INT last_offset
= char_offset
;
3537 int last_id
= charset_ascii
;
3539 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3540 int byte_after_cr
= -1;
3543 setup_iso_safe_charsets (attrs
);
3544 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
3546 if (cmp_status
->state
!= COMPOSING_NO
)
3548 if (charbuf_end
- charbuf
< cmp_status
->length
)
3550 for (i
= 0; i
< cmp_status
->length
; i
++)
3551 *charbuf
++ = cmp_status
->carryover
[i
];
3552 coding
->annotated
= 1;
3560 consumed_chars_base
= consumed_chars
;
3562 if (charbuf
>= charbuf_end
)
3564 if (byte_after_cr
>= 0)
3569 if (byte_after_cr
>= 0)
3570 c1
= byte_after_cr
, byte_after_cr
= -1;
3576 if (CODING_ISO_EXTSEGMENT_LEN (coding
) > 0)
3578 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3580 CODING_ISO_EXTSEGMENT_LEN (coding
)--;
3584 if (CODING_ISO_EMBEDDED_UTF_8 (coding
))
3586 if (c1
== ISO_CODE_ESC
)
3588 if (src
+ 1 >= src_end
)
3589 goto no_more_source
;
3590 *charbuf
++ = ISO_CODE_ESC
;
3592 if (src
[0] == '%' && src
[1] == '@')
3595 consumed_chars
+= 2;
3597 /* We are sure charbuf can contain two more chars. */
3600 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
3605 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3611 if ((cmp_status
->state
== COMPOSING_RULE
3612 || cmp_status
->state
== COMPOSING_COMPONENT_RULE
)
3613 && c1
!= ISO_CODE_ESC
)
3617 DECODE_COMPOSITION_RULE (rule
);
3618 STORE_COMPOSITION_RULE (rule
);
3622 /* We produce at most one character. */
3623 switch (iso_code_class
[c1
])
3625 case ISO_0x20_or_0x7F
:
3626 if (charset_id_0
< 0
3627 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3628 /* This is SPACE or DEL. */
3629 charset
= CHARSET_FROM_ID (charset_ascii
);
3631 charset
= CHARSET_FROM_ID (charset_id_0
);
3634 case ISO_graphic_plane_0
:
3635 if (charset_id_0
< 0)
3636 charset
= CHARSET_FROM_ID (charset_ascii
);
3638 charset
= CHARSET_FROM_ID (charset_id_0
);
3641 case ISO_0xA0_or_0xFF
:
3642 if (charset_id_1
< 0
3643 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3644 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3646 /* This is a graphic character, we fall down ... */
3648 case ISO_graphic_plane_1
:
3649 if (charset_id_1
< 0)
3651 charset
= CHARSET_FROM_ID (charset_id_1
);
3655 if (eol_dos
&& c1
== '\r')
3656 ONE_MORE_BYTE (byte_after_cr
);
3657 MAYBE_FINISH_COMPOSITION ();
3658 charset
= CHARSET_FROM_ID (charset_ascii
);
3665 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3666 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3668 CODING_ISO_INVOCATION (coding
, 0) = 1;
3669 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3673 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3675 CODING_ISO_INVOCATION (coding
, 0) = 0;
3676 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3679 case ISO_single_shift_2_7
:
3680 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
))
3682 case ISO_single_shift_2
:
3683 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3685 /* SS2 is handled as an escape sequence of ESC 'N' */
3687 goto label_escape_sequence
;
3689 case ISO_single_shift_3
:
3690 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3692 /* SS2 is handled as an escape sequence of ESC 'O' */
3694 goto label_escape_sequence
;
3696 case ISO_control_sequence_introducer
:
3697 /* CSI is handled as an escape sequence of ESC '[' ... */
3699 goto label_escape_sequence
;
3703 label_escape_sequence
:
3704 /* Escape sequences handled here are invocation,
3705 designation, direction specification, and character
3706 composition specification. */
3709 case '&': /* revision of following character set */
3711 if (!(c1
>= '@' && c1
<= '~'))
3714 if (c1
!= ISO_CODE_ESC
)
3717 goto label_escape_sequence
;
3719 case '$': /* designation of 2-byte character set */
3720 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3726 if (c1
>= '@' && c1
<= 'B')
3727 { /* designation of JISX0208.1978, GB2312.1980,
3729 reg
= 0, chars96
= 0;
3731 else if (c1
>= 0x28 && c1
<= 0x2B)
3732 { /* designation of DIMENSION2_CHARS94 character set */
3733 reg
= c1
- 0x28, chars96
= 0;
3736 else if (c1
>= 0x2C && c1
<= 0x2F)
3737 { /* designation of DIMENSION2_CHARS96 character set */
3738 reg
= c1
- 0x2C, chars96
= 1;
3743 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3744 /* We must update these variables now. */
3746 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3748 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3754 case 'n': /* invocation of locking-shift-2 */
3755 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3756 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3758 CODING_ISO_INVOCATION (coding
, 0) = 2;
3759 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3762 case 'o': /* invocation of locking-shift-3 */
3763 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3764 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3766 CODING_ISO_INVOCATION (coding
, 0) = 3;
3767 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3770 case 'N': /* invocation of single-shift-2 */
3771 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3772 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3774 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3775 if (charset_id_2
< 0)
3776 charset
= CHARSET_FROM_ID (charset_ascii
);
3778 charset
= CHARSET_FROM_ID (charset_id_2
);
3780 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3784 case 'O': /* invocation of single-shift-3 */
3785 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3786 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3788 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3789 if (charset_id_3
< 0)
3790 charset
= CHARSET_FROM_ID (charset_ascii
);
3792 charset
= CHARSET_FROM_ID (charset_id_3
);
3794 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3798 case '0': case '2': case '3': case '4': /* start composition */
3799 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3801 if (last_id
!= charset_ascii
)
3803 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3804 last_id
= charset_ascii
;
3805 last_offset
= char_offset
;
3807 DECODE_COMPOSITION_START (c1
);
3810 case '1': /* end composition */
3811 if (cmp_status
->state
== COMPOSING_NO
)
3813 DECODE_COMPOSITION_END ();
3816 case '[': /* specification of direction */
3817 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
))
3819 /* For the moment, nested direction is not supported.
3820 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3821 left-to-right, and nonzero means right-to-left. */
3825 case ']': /* end of the current direction */
3826 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3828 case '0': /* end of the current direction */
3829 case '1': /* start of left-to-right direction */
3832 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3837 case '2': /* start of right-to-left direction */
3840 coding
->mode
|= CODING_MODE_DIRECTION
;
3854 /* CTEXT extended segment:
3855 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3856 We keep these bytes as is for the moment.
3857 They may be decoded by post-read-conversion. */
3861 ONE_MORE_BYTE (dim
);
3862 if (dim
< '0' || dim
> '4')
3870 size
= ((M
- 128) * 128) + (L
- 128);
3871 if (charbuf
+ 6 > charbuf_end
)
3873 *charbuf
++ = ISO_CODE_ESC
;
3877 *charbuf
++ = BYTE8_TO_CHAR (M
);
3878 *charbuf
++ = BYTE8_TO_CHAR (L
);
3879 CODING_ISO_EXTSEGMENT_LEN (coding
) = size
;
3883 /* XFree86 extension for embedding UTF-8 in CTEXT:
3884 ESC % G --UTF-8-BYTES-- ESC % @
3885 We keep these bytes as is for the moment.
3886 They may be decoded by post-read-conversion. */
3887 if (charbuf
+ 3 > charbuf_end
)
3889 *charbuf
++ = ISO_CODE_ESC
;
3892 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 1;
3900 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3905 if (c1
>= 0x28 && c1
<= 0x2B)
3906 { /* designation of DIMENSION1_CHARS94 character set */
3907 reg
= c1
- 0x28, chars96
= 0;
3910 else if (c1
>= 0x2C && c1
<= 0x2F)
3911 { /* designation of DIMENSION1_CHARS96 character set */
3912 reg
= c1
- 0x2C, chars96
= 1;
3917 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3918 /* We must update these variables now. */
3920 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3922 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3934 if (cmp_status
->state
== COMPOSING_NO
3935 && charset
->id
!= charset_ascii
3936 && last_id
!= charset
->id
)
3938 if (last_id
!= charset_ascii
)
3939 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3940 last_id
= charset
->id
;
3941 last_offset
= char_offset
;
3944 /* Now we know CHARSET and 1st position code C1 of a character.
3945 Produce a decoded character while getting 2nd and 3rd
3946 position codes C2, C3 if necessary. */
3947 if (CHARSET_DIMENSION (charset
) > 1)
3950 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0)
3951 || ((c1
& 0x80) != (c2
& 0x80)))
3952 /* C2 is not in a valid range. */
3954 if (CHARSET_DIMENSION (charset
) == 2)
3955 c1
= (c1
<< 8) | c2
;
3959 if (c3
< 0x20 || (c3
>= 0x80 && c3
< 0xA0)
3960 || ((c1
& 0x80) != (c3
& 0x80)))
3961 /* C3 is not in a valid range. */
3963 c1
= (c1
<< 16) | (c2
<< 8) | c2
;
3967 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3970 MAYBE_FINISH_COMPOSITION ();
3971 for (; src_base
< src
; src_base
++, char_offset
++)
3973 if (ASCII_BYTE_P (*src_base
))
3974 *charbuf
++ = *src_base
;
3976 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3979 else if (cmp_status
->state
== COMPOSING_NO
)
3984 else if ((cmp_status
->state
== COMPOSING_CHAR
3985 ? cmp_status
->nchars
3986 : cmp_status
->ncomps
)
3987 >= MAX_COMPOSITION_COMPONENTS
)
3989 /* Too long composition. */
3990 MAYBE_FINISH_COMPOSITION ();
3995 STORE_COMPOSITION_CHAR (c
);
3999 MAYBE_FINISH_COMPOSITION ();
4001 consumed_chars
= consumed_chars_base
;
4003 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4013 if (cmp_status
->state
!= COMPOSING_NO
)
4015 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
4016 MAYBE_FINISH_COMPOSITION ();
4019 charbuf
-= cmp_status
->length
;
4020 for (i
= 0; i
< cmp_status
->length
; i
++)
4021 cmp_status
->carryover
[i
] = charbuf
[i
];
4024 else if (last_id
!= charset_ascii
)
4025 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4026 coding
->consumed_char
+= consumed_chars_base
;
4027 coding
->consumed
= src_base
- coding
->source
;
4028 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4032 /* ISO2022 encoding stuff. */
4035 It is not enough to say just "ISO2022" on encoding, we have to
4036 specify more details. In Emacs, each coding system of ISO2022
4037 variant has the following specifications:
4038 1. Initial designation to G0 thru G3.
4039 2. Allows short-form designation?
4040 3. ASCII should be designated to G0 before control characters?
4041 4. ASCII should be designated to G0 at end of line?
4042 5. 7-bit environment or 8-bit environment?
4043 6. Use locking-shift?
4044 7. Use Single-shift?
4045 And the following two are only for Japanese:
4046 8. Use ASCII in place of JIS0201-1976-Roman?
4047 9. Use JISX0208-1983 in place of JISX0208-1978?
4048 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
4049 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
4053 /* Produce codes (escape sequence) for designating CHARSET to graphic
4054 register REG at DST, and increment DST. If <final-char> of CHARSET is
4055 '@', 'A', or 'B' and the coding system CODING allows, produce
4056 designation sequence of short-form. */
4058 #define ENCODE_DESIGNATION(charset, reg, coding) \
4060 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4061 const char *intermediate_char_94 = "()*+"; \
4062 const char *intermediate_char_96 = ",-./"; \
4063 int revision = -1; \
4065 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
4066 revision = CHARSET_ISO_REVISION (charset); \
4068 if (revision >= 0) \
4070 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
4071 EMIT_ONE_BYTE ('@' + revision); \
4073 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4074 if (CHARSET_DIMENSION (charset) == 1) \
4077 if (! CHARSET_ISO_CHARS_96 (charset)) \
4078 b = intermediate_char_94[reg]; \
4080 b = intermediate_char_96[reg]; \
4081 EMIT_ONE_ASCII_BYTE (b); \
4085 EMIT_ONE_ASCII_BYTE ('$'); \
4086 if (! CHARSET_ISO_CHARS_96 (charset)) \
4088 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
4090 || final_char < '@' || final_char > 'B') \
4091 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4094 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4096 EMIT_ONE_ASCII_BYTE (final_char); \
4098 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4102 /* The following two macros produce codes (control character or escape
4103 sequence) for ISO2022 single-shift functions (single-shift-2 and
4106 #define ENCODE_SINGLE_SHIFT_2 \
4108 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4109 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
4111 EMIT_ONE_BYTE (ISO_CODE_SS2); \
4112 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4116 #define ENCODE_SINGLE_SHIFT_3 \
4118 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4119 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
4121 EMIT_ONE_BYTE (ISO_CODE_SS3); \
4122 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4126 /* The following four macros produce codes (control character or
4127 escape sequence) for ISO2022 locking-shift functions (shift-in,
4128 shift-out, locking-shift-2, and locking-shift-3). */
4130 #define ENCODE_SHIFT_IN \
4132 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
4133 CODING_ISO_INVOCATION (coding, 0) = 0; \
4137 #define ENCODE_SHIFT_OUT \
4139 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
4140 CODING_ISO_INVOCATION (coding, 0) = 1; \
4144 #define ENCODE_LOCKING_SHIFT_2 \
4146 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4147 CODING_ISO_INVOCATION (coding, 0) = 2; \
4151 #define ENCODE_LOCKING_SHIFT_3 \
4153 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
4154 CODING_ISO_INVOCATION (coding, 0) = 3; \
4158 /* Produce codes for a DIMENSION1 character whose character set is
4159 CHARSET and whose position-code is C1. Designation and invocation
4160 sequences are also produced in advance if necessary. */
4162 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
4164 int id = CHARSET_ID (charset); \
4166 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
4167 && id == charset_ascii) \
4169 id = charset_jisx0201_roman; \
4170 charset = CHARSET_FROM_ID (id); \
4173 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4175 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4176 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4178 EMIT_ONE_BYTE (c1 | 0x80); \
4179 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4182 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4184 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
4187 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4189 EMIT_ONE_BYTE (c1 | 0x80); \
4193 /* Since CHARSET is not yet invoked to any graphic planes, we \
4194 must invoke it, or, at first, designate it to some graphic \
4195 register. Then repeat the loop to actually produce the \
4197 dst = encode_invocation_designation (charset, coding, dst, \
4202 /* Produce codes for a DIMENSION2 character whose character set is
4203 CHARSET and whose position-codes are C1 and C2. Designation and
4204 invocation codes are also produced in advance if necessary. */
4206 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
4208 int id = CHARSET_ID (charset); \
4210 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
4211 && id == charset_jisx0208) \
4213 id = charset_jisx0208_1978; \
4214 charset = CHARSET_FROM_ID (id); \
4217 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
4219 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
4220 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4222 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4223 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
4226 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
4228 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
4231 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
4233 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
4237 /* Since CHARSET is not yet invoked to any graphic planes, we \
4238 must invoke it, or, at first, designate it to some graphic \
4239 register. Then repeat the loop to actually produce the \
4241 dst = encode_invocation_designation (charset, coding, dst, \
4246 #define ENCODE_ISO_CHARACTER(charset, c) \
4249 CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \
4251 if (CHARSET_DIMENSION (charset) == 1) \
4252 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
4254 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
4258 /* Produce designation and invocation codes at a place pointed by DST
4259 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4262 static unsigned char *
4263 encode_invocation_designation (struct charset
*charset
,
4264 struct coding_system
*coding
,
4265 unsigned char *dst
, EMACS_INT
*p_nchars
)
4267 int multibytep
= coding
->dst_multibyte
;
4268 EMACS_INT produced_chars
= *p_nchars
;
4269 int reg
; /* graphic register number */
4270 int id
= CHARSET_ID (charset
);
4272 /* At first, check designations. */
4273 for (reg
= 0; reg
< 4; reg
++)
4274 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
4279 /* CHARSET is not yet designated to any graphic registers. */
4280 /* At first check the requested designation. */
4281 reg
= CODING_ISO_REQUEST (coding
, id
);
4283 /* Since CHARSET requests no special designation, designate it
4284 to graphic register 0. */
4287 ENCODE_DESIGNATION (charset
, reg
, coding
);
4290 if (CODING_ISO_INVOCATION (coding
, 0) != reg
4291 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
4293 /* Since the graphic register REG is not invoked to any graphic
4294 planes, invoke it to graphic plane 0. */
4297 case 0: /* graphic register 0 */
4301 case 1: /* graphic register 1 */
4305 case 2: /* graphic register 2 */
4306 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4307 ENCODE_SINGLE_SHIFT_2
;
4309 ENCODE_LOCKING_SHIFT_2
;
4312 case 3: /* graphic register 3 */
4313 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
4314 ENCODE_SINGLE_SHIFT_3
;
4316 ENCODE_LOCKING_SHIFT_3
;
4321 *p_nchars
= produced_chars
;
4326 /* Produce codes for designation and invocation to reset the graphic
4327 planes and registers to initial state. */
4328 #define ENCODE_RESET_PLANE_AND_REGISTER() \
4331 struct charset *charset; \
4333 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
4335 for (reg = 0; reg < 4; reg++) \
4336 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
4337 && (CODING_ISO_DESIGNATION (coding, reg) \
4338 != CODING_ISO_INITIAL (coding, reg))) \
4340 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
4341 ENCODE_DESIGNATION (charset, reg, coding); \
4346 /* Produce designation sequences of charsets in the line started from
4347 CHARBUF to a place pointed by DST, and return the number of
4348 produced bytes. DST should not directly point a buffer text area
4349 which may be relocated by char_charset call.
4351 If the current block ends before any end-of-line, we may fail to
4352 find all the necessary designations. */
4355 encode_designation_at_bol (struct coding_system
*coding
,
4356 int *charbuf
, int *charbuf_end
,
4359 unsigned char *orig
= dst
;
4360 struct charset
*charset
;
4361 /* Table of charsets to be designated to each graphic register. */
4363 int c
, found
= 0, reg
;
4364 EMACS_INT produced_chars
= 0;
4365 int multibytep
= coding
->dst_multibyte
;
4367 Lisp_Object charset_list
;
4369 attrs
= CODING_ID_ATTRS (coding
->id
);
4370 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4371 if (EQ (charset_list
, Qiso_2022
))
4372 charset_list
= Viso_2022_charset_list
;
4374 for (reg
= 0; reg
< 4; reg
++)
4377 while (charbuf
< charbuf_end
&& found
< 4)
4384 charset
= char_charset (c
, charset_list
, NULL
);
4385 id
= CHARSET_ID (charset
);
4386 reg
= CODING_ISO_REQUEST (coding
, id
);
4387 if (reg
>= 0 && r
[reg
] < 0)
4396 for (reg
= 0; reg
< 4; reg
++)
4398 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
4399 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
4405 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
4408 encode_coding_iso_2022 (struct coding_system
*coding
)
4410 int multibytep
= coding
->dst_multibyte
;
4411 int *charbuf
= coding
->charbuf
;
4412 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4413 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4414 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4417 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
4418 && CODING_ISO_BOL (coding
));
4419 EMACS_INT produced_chars
= 0;
4420 Lisp_Object attrs
, eol_type
, charset_list
;
4421 int ascii_compatible
;
4423 int preferred_charset_id
= -1;
4425 CODING_GET_INFO (coding
, attrs
, charset_list
);
4426 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
4427 if (VECTORP (eol_type
))
4430 setup_iso_safe_charsets (attrs
);
4431 /* Charset list may have been changed. */
4432 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
4433 coding
->safe_charsets
= SDATA (CODING_ATTR_SAFE_CHARSETS (attrs
));
4436 = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
))
4437 && ! (CODING_ISO_FLAGS (coding
) & (CODING_ISO_FLAG_DESIGNATION
4438 | CODING_ISO_FLAG_LOCKING_SHIFT
)));
4440 while (charbuf
< charbuf_end
)
4442 ASSURE_DESTINATION (safe_room
);
4444 if (bol_designation
)
4446 /* We have to produce designation sequences if any now. */
4447 unsigned char desig_buf
[16];
4451 charset_map_loaded
= 0;
4452 nbytes
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
,
4454 if (charset_map_loaded
4455 && (offset
= coding_set_destination (coding
)))
4460 memcpy (dst
, desig_buf
, nbytes
);
4462 /* We are sure that designation sequences are all ASCII bytes. */
4463 produced_chars
+= nbytes
;
4464 bol_designation
= 0;
4465 ASSURE_DESTINATION (safe_room
);
4472 /* Handle an annotation. */
4475 case CODING_ANNOTATE_COMPOSITION_MASK
:
4476 /* Not yet implemented. */
4478 case CODING_ANNOTATE_CHARSET_MASK
:
4479 preferred_charset_id
= charbuf
[2];
4480 if (preferred_charset_id
>= 0
4481 && NILP (Fmemq (make_number (preferred_charset_id
),
4483 preferred_charset_id
= -1;
4492 /* Now encode the character C. */
4493 if (c
< 0x20 || c
== 0x7F)
4496 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4498 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4499 ENCODE_RESET_PLANE_AND_REGISTER ();
4500 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4504 for (i
= 0; i
< 4; i
++)
4505 CODING_ISO_DESIGNATION (coding
, i
)
4506 = CODING_ISO_INITIAL (coding
, i
);
4509 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4511 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4512 ENCODE_RESET_PLANE_AND_REGISTER ();
4513 EMIT_ONE_ASCII_BYTE (c
);
4515 else if (ASCII_CHAR_P (c
))
4517 if (ascii_compatible
)
4518 EMIT_ONE_ASCII_BYTE (c
);
4521 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4522 ENCODE_ISO_CHARACTER (charset
, c
);
4525 else if (CHAR_BYTE8_P (c
))
4527 c
= CHAR_TO_BYTE8 (c
);
4532 struct charset
*charset
;
4534 if (preferred_charset_id
>= 0)
4538 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4539 CODING_CHAR_CHARSET_P (coding
, dst
, dst_end
, c
, charset
, result
);
4541 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4545 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4549 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4551 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4552 charset
= CHARSET_FROM_ID (charset_ascii
);
4556 c
= coding
->default_char
;
4557 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4558 charset_list
, NULL
, charset
);
4561 ENCODE_ISO_CHARACTER (charset
, c
);
4565 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4566 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4568 ASSURE_DESTINATION (safe_room
);
4569 ENCODE_RESET_PLANE_AND_REGISTER ();
4571 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4572 CODING_ISO_BOL (coding
) = bol_designation
;
4573 coding
->produced_char
+= produced_chars
;
4574 coding
->produced
= dst
- coding
->destination
;
4579 /*** 8,9. SJIS and BIG5 handlers ***/
4581 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4582 quite widely. So, for the moment, Emacs supports them in the bare
4583 C code. But, in the future, they may be supported only by CCL. */
4585 /* SJIS is a coding system encoding three character sets: ASCII, right
4586 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4587 as is. A character of charset katakana-jisx0201 is encoded by
4588 "position-code + 0x80". A character of charset japanese-jisx0208
4589 is encoded in 2-byte but two position-codes are divided and shifted
4590 so that it fit in the range below.
4592 --- CODE RANGE of SJIS ---
4593 (character set) (range)
4595 KATAKANA-JISX0201 0xA0 .. 0xDF
4596 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4597 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4598 -------------------------------
4602 /* BIG5 is a coding system encoding two character sets: ASCII and
4603 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4604 character set and is encoded in two-byte.
4606 --- CODE RANGE of BIG5 ---
4607 (character set) (range)
4609 Big5 (1st byte) 0xA1 .. 0xFE
4610 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4611 --------------------------
4615 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4616 Check if a text is encoded in SJIS. If it is, return
4617 CATEGORY_MASK_SJIS, else return 0. */
4620 detect_coding_sjis (struct coding_system
*coding
,
4621 struct coding_detection_info
*detect_info
)
4623 const unsigned char *src
= coding
->source
, *src_base
;
4624 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4625 int multibytep
= coding
->src_multibyte
;
4626 EMACS_INT consumed_chars
= 0;
4629 Lisp_Object attrs
, charset_list
;
4630 int max_first_byte_of_2_byte_code
;
4632 CODING_GET_INFO (coding
, attrs
, charset_list
);
4633 max_first_byte_of_2_byte_code
4634 = (XINT (Flength (charset_list
)) > 3 ? 0xFC : 0xEF);
4636 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4637 /* A coding system of this category is always ASCII compatible. */
4638 src
+= coding
->head_ascii
;
4646 if ((c
>= 0x81 && c
<= 0x9F)
4647 || (c
>= 0xE0 && c
<= max_first_byte_of_2_byte_code
))
4650 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4652 found
= CATEGORY_MASK_SJIS
;
4654 else if (c
>= 0xA0 && c
< 0xE0)
4655 found
= CATEGORY_MASK_SJIS
;
4659 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4663 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4665 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4668 detect_info
->found
|= found
;
4672 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4673 Check if a text is encoded in BIG5. If it is, return
4674 CATEGORY_MASK_BIG5, else return 0. */
4677 detect_coding_big5 (struct coding_system
*coding
,
4678 struct coding_detection_info
*detect_info
)
4680 const unsigned char *src
= coding
->source
, *src_base
;
4681 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4682 int multibytep
= coding
->src_multibyte
;
4683 EMACS_INT consumed_chars
= 0;
4687 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4688 /* A coding system of this category is always ASCII compatible. */
4689 src
+= coding
->head_ascii
;
4700 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4702 found
= CATEGORY_MASK_BIG5
;
4707 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4711 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4713 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4716 detect_info
->found
|= found
;
4720 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4721 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4724 decode_coding_sjis (struct coding_system
*coding
)
4726 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4727 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4728 const unsigned char *src_base
;
4729 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4730 /* We may produce one charset annotation in one loop and one more at
4733 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4734 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
4735 int multibytep
= coding
->src_multibyte
;
4736 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4737 struct charset
*charset_kanji2
;
4738 Lisp_Object attrs
, charset_list
, val
;
4739 EMACS_INT char_offset
= coding
->produced_char
;
4740 EMACS_INT last_offset
= char_offset
;
4741 int last_id
= charset_ascii
;
4743 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4744 int byte_after_cr
= -1;
4746 CODING_GET_INFO (coding
, attrs
, charset_list
);
4749 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4750 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4751 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4752 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4757 struct charset
*charset
;
4760 consumed_chars_base
= consumed_chars
;
4762 if (charbuf
>= charbuf_end
)
4764 if (byte_after_cr
>= 0)
4769 if (byte_after_cr
>= 0)
4770 c
= byte_after_cr
, byte_after_cr
= -1;
4777 if (eol_dos
&& c
== '\r')
4778 ONE_MORE_BYTE (byte_after_cr
);
4779 charset
= charset_roman
;
4781 else if (c
== 0x80 || c
== 0xA0)
4783 else if (c
>= 0xA1 && c
<= 0xDF)
4785 /* SJIS -> JISX0201-Kana */
4787 charset
= charset_kana
;
4791 /* SJIS -> JISX0208 */
4793 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4797 charset
= charset_kanji
;
4799 else if (c
<= 0xFC && charset_kanji2
)
4801 /* SJIS -> JISX0213-2 */
4803 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4807 charset
= charset_kanji2
;
4811 if (charset
->id
!= charset_ascii
4812 && last_id
!= charset
->id
)
4814 if (last_id
!= charset_ascii
)
4815 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4816 last_id
= charset
->id
;
4817 last_offset
= char_offset
;
4819 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4826 consumed_chars
= consumed_chars_base
;
4828 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4834 if (last_id
!= charset_ascii
)
4835 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4836 coding
->consumed_char
+= consumed_chars_base
;
4837 coding
->consumed
= src_base
- coding
->source
;
4838 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4842 decode_coding_big5 (struct coding_system
*coding
)
4844 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4845 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4846 const unsigned char *src_base
;
4847 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4848 /* We may produce one charset annotation in one loop and one more at
4851 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
4852 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
4853 int multibytep
= coding
->src_multibyte
;
4854 struct charset
*charset_roman
, *charset_big5
;
4855 Lisp_Object attrs
, charset_list
, val
;
4856 EMACS_INT char_offset
= coding
->produced_char
;
4857 EMACS_INT last_offset
= char_offset
;
4858 int last_id
= charset_ascii
;
4860 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4861 int byte_after_cr
= -1;
4863 CODING_GET_INFO (coding
, attrs
, charset_list
);
4865 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4866 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4871 struct charset
*charset
;
4874 consumed_chars_base
= consumed_chars
;
4876 if (charbuf
>= charbuf_end
)
4878 if (byte_after_cr
>= 0)
4883 if (byte_after_cr
>= 0)
4884 c
= byte_after_cr
, byte_after_cr
= -1;
4892 if (eol_dos
&& c
== '\r')
4893 ONE_MORE_BYTE (byte_after_cr
);
4894 charset
= charset_roman
;
4899 if (c
< 0xA1 || c
> 0xFE)
4902 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4905 charset
= charset_big5
;
4907 if (charset
->id
!= charset_ascii
4908 && last_id
!= charset
->id
)
4910 if (last_id
!= charset_ascii
)
4911 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4912 last_id
= charset
->id
;
4913 last_offset
= char_offset
;
4915 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4922 consumed_chars
= consumed_chars_base
;
4924 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4930 if (last_id
!= charset_ascii
)
4931 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4932 coding
->consumed_char
+= consumed_chars_base
;
4933 coding
->consumed
= src_base
- coding
->source
;
4934 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4937 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4938 This function can encode charsets `ascii', `katakana-jisx0201',
4939 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4940 are sure that all these charsets are registered as official charset
4941 (i.e. do not have extended leading-codes). Characters of other
4942 charsets are produced without any encoding. If SJIS_P is 1, encode
4943 SJIS text, else encode BIG5 text. */
4946 encode_coding_sjis (struct coding_system
*coding
)
4948 int multibytep
= coding
->dst_multibyte
;
4949 int *charbuf
= coding
->charbuf
;
4950 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4951 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4952 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4954 EMACS_INT produced_chars
= 0;
4955 Lisp_Object attrs
, charset_list
, val
;
4956 int ascii_compatible
;
4957 struct charset
*charset_kanji
, *charset_kana
;
4958 struct charset
*charset_kanji2
;
4961 CODING_GET_INFO (coding
, attrs
, charset_list
);
4962 val
= XCDR (charset_list
);
4963 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4964 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4965 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4967 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4969 while (charbuf
< charbuf_end
)
4971 ASSURE_DESTINATION (safe_room
);
4973 /* Now encode the character C. */
4974 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4975 EMIT_ONE_ASCII_BYTE (c
);
4976 else if (CHAR_BYTE8_P (c
))
4978 c
= CHAR_TO_BYTE8 (c
);
4984 struct charset
*charset
;
4985 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
4990 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4992 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4993 charset
= CHARSET_FROM_ID (charset_ascii
);
4997 c
= coding
->default_char
;
4998 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
4999 charset_list
, &code
, charset
);
5002 if (code
== CHARSET_INVALID_CODE (charset
))
5004 if (charset
== charset_kanji
)
5008 c1
= code
>> 8, c2
= code
& 0xFF;
5009 EMIT_TWO_BYTES (c1
, c2
);
5011 else if (charset
== charset_kana
)
5012 EMIT_ONE_BYTE (code
| 0x80);
5013 else if (charset_kanji2
&& charset
== charset_kanji2
)
5018 if (c1
== 0x21 || (c1
>= 0x23 && c1
<= 0x25)
5020 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
5022 JIS_TO_SJIS2 (code
);
5023 c1
= code
>> 8, c2
= code
& 0xFF;
5024 EMIT_TWO_BYTES (c1
, c2
);
5027 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5030 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5033 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5034 coding
->produced_char
+= produced_chars
;
5035 coding
->produced
= dst
- coding
->destination
;
5040 encode_coding_big5 (struct coding_system
*coding
)
5042 int multibytep
= coding
->dst_multibyte
;
5043 int *charbuf
= coding
->charbuf
;
5044 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5045 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5046 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5048 EMACS_INT produced_chars
= 0;
5049 Lisp_Object attrs
, charset_list
, val
;
5050 int ascii_compatible
;
5051 struct charset
*charset_big5
;
5054 CODING_GET_INFO (coding
, attrs
, charset_list
);
5055 val
= XCDR (charset_list
);
5056 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
5057 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5059 while (charbuf
< charbuf_end
)
5061 ASSURE_DESTINATION (safe_room
);
5063 /* Now encode the character C. */
5064 if (ASCII_CHAR_P (c
) && ascii_compatible
)
5065 EMIT_ONE_ASCII_BYTE (c
);
5066 else if (CHAR_BYTE8_P (c
))
5068 c
= CHAR_TO_BYTE8 (c
);
5074 struct charset
*charset
;
5075 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5080 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5082 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5083 charset
= CHARSET_FROM_ID (charset_ascii
);
5087 c
= coding
->default_char
;
5088 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
,
5089 charset_list
, &code
, charset
);
5092 if (code
== CHARSET_INVALID_CODE (charset
))
5094 if (charset
== charset_big5
)
5098 c1
= code
>> 8, c2
= code
& 0xFF;
5099 EMIT_TWO_BYTES (c1
, c2
);
5102 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
5105 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5106 coding
->produced_char
+= produced_chars
;
5107 coding
->produced
= dst
- coding
->destination
;
5112 /*** 10. CCL handlers ***/
5114 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5115 Check if a text is encoded in a coding system of which
5116 encoder/decoder are written in CCL program. If it is, return
5117 CATEGORY_MASK_CCL, else return 0. */
5120 detect_coding_ccl (struct coding_system
*coding
,
5121 struct coding_detection_info
*detect_info
)
5123 const unsigned char *src
= coding
->source
, *src_base
;
5124 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5125 int multibytep
= coding
->src_multibyte
;
5126 EMACS_INT consumed_chars
= 0;
5128 unsigned char *valids
;
5129 EMACS_INT head_ascii
= coding
->head_ascii
;
5132 detect_info
->checked
|= CATEGORY_MASK_CCL
;
5134 coding
= &coding_categories
[coding_category_ccl
];
5135 valids
= CODING_CCL_VALIDS (coding
);
5136 attrs
= CODING_ID_ATTRS (coding
->id
);
5137 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5146 if (c
< 0 || ! valids
[c
])
5148 if ((valids
[c
] > 1))
5149 found
= CATEGORY_MASK_CCL
;
5151 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
5155 detect_info
->found
|= found
;
5160 decode_coding_ccl (struct coding_system
*coding
)
5162 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5163 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5164 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5165 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
5166 EMACS_INT consumed_chars
= 0;
5167 int multibytep
= coding
->src_multibyte
;
5168 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5169 int source_charbuf
[1024];
5170 int source_byteidx
[1025];
5171 Lisp_Object attrs
, charset_list
;
5173 CODING_GET_INFO (coding
, attrs
, charset_list
);
5177 const unsigned char *p
= src
;
5182 while (i
< 1024 && p
< src_end
)
5184 source_byteidx
[i
] = p
- src
;
5185 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
5187 source_byteidx
[i
] = p
- src
;
5190 while (i
< 1024 && p
< src_end
)
5191 source_charbuf
[i
++] = *p
++;
5193 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
5194 ccl
->last_block
= 1;
5195 ccl_driver (ccl
, source_charbuf
, charbuf
, i
, charbuf_end
- charbuf
,
5197 charbuf
+= ccl
->produced
;
5199 src
+= source_byteidx
[ccl
->consumed
];
5201 src
+= ccl
->consumed
;
5202 consumed_chars
+= ccl
->consumed
;
5203 if (p
== src_end
|| ccl
->status
!= CCL_STAT_SUSPEND_BY_SRC
)
5207 switch (ccl
->status
)
5209 case CCL_STAT_SUSPEND_BY_SRC
:
5210 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5212 case CCL_STAT_SUSPEND_BY_DST
:
5213 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5216 case CCL_STAT_INVALID_CMD
:
5217 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5220 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5223 coding
->consumed_char
+= consumed_chars
;
5224 coding
->consumed
= src
- coding
->source
;
5225 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5229 encode_coding_ccl (struct coding_system
*coding
)
5231 struct ccl_program
*ccl
= &coding
->spec
.ccl
->ccl
;
5232 int multibytep
= coding
->dst_multibyte
;
5233 int *charbuf
= coding
->charbuf
;
5234 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5235 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5236 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5237 int destination_charbuf
[1024];
5238 EMACS_INT produced_chars
= 0;
5240 Lisp_Object attrs
, charset_list
;
5242 CODING_GET_INFO (coding
, attrs
, charset_list
);
5243 if (coding
->consumed_char
== coding
->src_chars
5244 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
5245 ccl
->last_block
= 1;
5249 ccl_driver (ccl
, charbuf
, destination_charbuf
,
5250 charbuf_end
- charbuf
, 1024, charset_list
);
5253 ASSURE_DESTINATION (ccl
->produced
* 2);
5254 for (i
= 0; i
< ccl
->produced
; i
++)
5255 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
5259 ASSURE_DESTINATION (ccl
->produced
);
5260 for (i
= 0; i
< ccl
->produced
; i
++)
5261 *dst
++ = destination_charbuf
[i
] & 0xFF;
5262 produced_chars
+= ccl
->produced
;
5264 charbuf
+= ccl
->consumed
;
5265 if (ccl
->status
== CCL_STAT_QUIT
5266 || ccl
->status
== CCL_STAT_INVALID_CMD
)
5269 while (charbuf
< charbuf_end
);
5271 switch (ccl
->status
)
5273 case CCL_STAT_SUSPEND_BY_SRC
:
5274 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5276 case CCL_STAT_SUSPEND_BY_DST
:
5277 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
5280 case CCL_STAT_INVALID_CMD
:
5281 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
5284 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5288 coding
->produced_char
+= produced_chars
;
5289 coding
->produced
= dst
- coding
->destination
;
5295 /*** 10, 11. no-conversion handlers ***/
5297 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
5300 decode_coding_raw_text (struct coding_system
*coding
)
5303 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5305 coding
->chars_at_source
= 1;
5306 coding
->consumed_char
= coding
->src_chars
;
5307 coding
->consumed
= coding
->src_bytes
;
5308 if (eol_dos
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
5310 coding
->consumed_char
--;
5312 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5315 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5319 encode_coding_raw_text (struct coding_system
*coding
)
5321 int multibytep
= coding
->dst_multibyte
;
5322 int *charbuf
= coding
->charbuf
;
5323 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
5324 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5325 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5326 EMACS_INT produced_chars
= 0;
5331 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
5333 if (coding
->src_multibyte
)
5334 while (charbuf
< charbuf_end
)
5336 ASSURE_DESTINATION (safe_room
);
5338 if (ASCII_CHAR_P (c
))
5339 EMIT_ONE_ASCII_BYTE (c
);
5340 else if (CHAR_BYTE8_P (c
))
5342 c
= CHAR_TO_BYTE8 (c
);
5347 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
5349 CHAR_STRING_ADVANCE (c
, p1
);
5352 EMIT_ONE_BYTE (*p0
);
5359 while (charbuf
< charbuf_end
)
5361 ASSURE_DESTINATION (safe_room
);
5368 if (coding
->src_multibyte
)
5370 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5372 while (charbuf
< charbuf_end
)
5374 ASSURE_DESTINATION (safe_room
);
5376 if (ASCII_CHAR_P (c
))
5378 else if (CHAR_BYTE8_P (c
))
5379 *dst
++ = CHAR_TO_BYTE8 (c
);
5381 CHAR_STRING_ADVANCE (c
, dst
);
5386 ASSURE_DESTINATION (charbuf_end
- charbuf
);
5387 while (charbuf
< charbuf_end
&& dst
< dst_end
)
5388 *dst
++ = *charbuf
++;
5390 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
5392 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5393 coding
->produced_char
+= produced_chars
;
5394 coding
->produced
= dst
- coding
->destination
;
5398 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
5399 Check if a text is encoded in a charset-based coding system. If it
5400 is, return 1, else return 0. */
5403 detect_coding_charset (struct coding_system
*coding
,
5404 struct coding_detection_info
*detect_info
)
5406 const unsigned char *src
= coding
->source
, *src_base
;
5407 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5408 int multibytep
= coding
->src_multibyte
;
5409 EMACS_INT consumed_chars
= 0;
5410 Lisp_Object attrs
, valids
, name
;
5412 EMACS_INT head_ascii
= coding
->head_ascii
;
5413 int check_latin_extra
= 0;
5415 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
5417 coding
= &coding_categories
[coding_category_charset
];
5418 attrs
= CODING_ID_ATTRS (coding
->id
);
5419 valids
= AREF (attrs
, coding_attr_charset_valids
);
5420 name
= CODING_ID_NAME (coding
->id
);
5421 if (strncmp (SSDATA (SYMBOL_NAME (name
)),
5422 "iso-8859-", sizeof ("iso-8859-") - 1) == 0
5423 || strncmp (SSDATA (SYMBOL_NAME (name
)),
5424 "iso-latin-", sizeof ("iso-latin-") - 1) == 0)
5425 check_latin_extra
= 1;
5427 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
5434 struct charset
*charset
;
5441 val
= AREF (valids
, c
);
5447 && check_latin_extra
5448 && (!VECTORP (Vlatin_extra_code_table
)
5449 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
])))
5451 found
= CATEGORY_MASK_CHARSET
;
5455 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5456 dim
= CHARSET_DIMENSION (charset
);
5457 for (idx
= 1; idx
< dim
; idx
++)
5462 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5463 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5472 for (; CONSP (val
); val
= XCDR (val
))
5474 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5475 dim
= CHARSET_DIMENSION (charset
);
5481 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
5482 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
5497 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
5501 detect_info
->found
|= found
;
5506 decode_coding_charset (struct coding_system
*coding
)
5508 const unsigned char *src
= coding
->source
+ coding
->consumed
;
5509 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
5510 const unsigned char *src_base
;
5511 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
5512 /* We may produce one charset annotation in one loop and one more at
5515 = coding
->charbuf
+ coding
->charbuf_size
- (MAX_ANNOTATION_LENGTH
* 2);
5516 EMACS_INT consumed_chars
= 0, consumed_chars_base
;
5517 int multibytep
= coding
->src_multibyte
;
5518 Lisp_Object attrs
= CODING_ID_ATTRS (coding
->id
);
5520 EMACS_INT char_offset
= coding
->produced_char
;
5521 EMACS_INT last_offset
= char_offset
;
5522 int last_id
= charset_ascii
;
5524 !inhibit_eol_conversion
&& EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5525 int byte_after_cr
= -1;
5527 valids
= AREF (attrs
, coding_attr_charset_valids
);
5533 struct charset
*charset
;
5539 consumed_chars_base
= consumed_chars
;
5541 if (charbuf
>= charbuf_end
)
5543 if (byte_after_cr
>= 0)
5548 if (byte_after_cr
>= 0)
5556 if (eol_dos
&& c
== '\r')
5557 ONE_MORE_BYTE (byte_after_cr
);
5563 val
= AREF (valids
, c
);
5564 if (! INTEGERP (val
) && ! CONSP (val
))
5568 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5569 dim
= CHARSET_DIMENSION (charset
);
5573 code
= (code
<< 8) | c
;
5576 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5581 /* VAL is a list of charset IDs. It is assured that the
5582 list is sorted by charset dimensions (smaller one
5586 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5587 dim
= CHARSET_DIMENSION (charset
);
5591 code
= (code
<< 8) | c
;
5594 CODING_DECODE_CHAR (coding
, src
, src_base
,
5595 src_end
, charset
, code
, c
);
5603 if (charset
->id
!= charset_ascii
5604 && last_id
!= charset
->id
)
5606 if (last_id
!= charset_ascii
)
5607 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5608 last_id
= charset
->id
;
5609 last_offset
= char_offset
;
5618 consumed_chars
= consumed_chars_base
;
5620 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5626 if (last_id
!= charset_ascii
)
5627 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5628 coding
->consumed_char
+= consumed_chars_base
;
5629 coding
->consumed
= src_base
- coding
->source
;
5630 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5634 encode_coding_charset (struct coding_system
*coding
)
5636 int multibytep
= coding
->dst_multibyte
;
5637 int *charbuf
= coding
->charbuf
;
5638 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5639 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5640 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5641 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5642 EMACS_INT produced_chars
= 0;
5643 Lisp_Object attrs
, charset_list
;
5644 int ascii_compatible
;
5647 CODING_GET_INFO (coding
, attrs
, charset_list
);
5648 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5650 while (charbuf
< charbuf_end
)
5652 struct charset
*charset
;
5655 ASSURE_DESTINATION (safe_room
);
5657 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5658 EMIT_ONE_ASCII_BYTE (c
);
5659 else if (CHAR_BYTE8_P (c
))
5661 c
= CHAR_TO_BYTE8 (c
);
5666 CODING_CHAR_CHARSET (coding
, dst
, dst_end
, c
, charset_list
,
5671 if (CHARSET_DIMENSION (charset
) == 1)
5672 EMIT_ONE_BYTE (code
);
5673 else if (CHARSET_DIMENSION (charset
) == 2)
5674 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5675 else if (CHARSET_DIMENSION (charset
) == 3)
5676 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5678 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5679 (code
>> 8) & 0xFF, code
& 0xFF);
5683 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5684 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5686 c
= coding
->default_char
;
5692 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5693 coding
->produced_char
+= produced_chars
;
5694 coding
->produced
= dst
- coding
->destination
;
5699 /*** 7. C library functions ***/
5701 /* Setup coding context CODING from information about CODING_SYSTEM.
5702 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5703 CODING_SYSTEM is invalid, signal an error. */
5706 setup_coding_system (Lisp_Object coding_system
, struct coding_system
*coding
)
5709 Lisp_Object eol_type
;
5710 Lisp_Object coding_type
;
5713 if (NILP (coding_system
))
5714 coding_system
= Qundecided
;
5716 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5718 attrs
= CODING_ID_ATTRS (coding
->id
);
5719 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
5722 coding
->head_ascii
= -1;
5723 if (VECTORP (eol_type
))
5724 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5725 | CODING_REQUIRE_DETECTION_MASK
);
5726 else if (! EQ (eol_type
, Qunix
))
5727 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5728 | CODING_REQUIRE_ENCODING_MASK
);
5730 coding
->common_flags
= 0;
5731 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5732 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5733 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5734 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5735 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5736 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5738 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5739 coding
->max_charset_id
= SCHARS (val
) - 1;
5740 coding
->safe_charsets
= SDATA (val
);
5741 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5742 coding
->carryover_bytes
= 0;
5744 coding_type
= CODING_ATTR_TYPE (attrs
);
5745 if (EQ (coding_type
, Qundecided
))
5747 coding
->detector
= NULL
;
5748 coding
->decoder
= decode_coding_raw_text
;
5749 coding
->encoder
= encode_coding_raw_text
;
5750 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5752 else if (EQ (coding_type
, Qiso_2022
))
5755 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5757 /* Invoke graphic register 0 to plane 0. */
5758 CODING_ISO_INVOCATION (coding
, 0) = 0;
5759 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5760 CODING_ISO_INVOCATION (coding
, 1)
5761 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5762 /* Setup the initial status of designation. */
5763 for (i
= 0; i
< 4; i
++)
5764 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5765 /* Not single shifting initially. */
5766 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5767 /* Beginning of buffer should also be regarded as bol. */
5768 CODING_ISO_BOL (coding
) = 1;
5769 coding
->detector
= detect_coding_iso_2022
;
5770 coding
->decoder
= decode_coding_iso_2022
;
5771 coding
->encoder
= encode_coding_iso_2022
;
5772 if (flags
& CODING_ISO_FLAG_SAFE
)
5773 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5774 coding
->common_flags
5775 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5776 | CODING_REQUIRE_FLUSHING_MASK
);
5777 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5778 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5779 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5780 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5781 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5783 setup_iso_safe_charsets (attrs
);
5784 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5785 coding
->max_charset_id
= SCHARS (val
) - 1;
5786 coding
->safe_charsets
= SDATA (val
);
5788 CODING_ISO_FLAGS (coding
) = flags
;
5789 CODING_ISO_CMP_STATUS (coding
)->state
= COMPOSING_NO
;
5790 CODING_ISO_CMP_STATUS (coding
)->method
= COMPOSITION_NO
;
5791 CODING_ISO_EXTSEGMENT_LEN (coding
) = 0;
5792 CODING_ISO_EMBEDDED_UTF_8 (coding
) = 0;
5794 else if (EQ (coding_type
, Qcharset
))
5796 coding
->detector
= detect_coding_charset
;
5797 coding
->decoder
= decode_coding_charset
;
5798 coding
->encoder
= encode_coding_charset
;
5799 coding
->common_flags
5800 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5802 else if (EQ (coding_type
, Qutf_8
))
5804 val
= AREF (attrs
, coding_attr_utf_bom
);
5805 CODING_UTF_8_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5806 : EQ (val
, Qt
) ? utf_with_bom
5808 coding
->detector
= detect_coding_utf_8
;
5809 coding
->decoder
= decode_coding_utf_8
;
5810 coding
->encoder
= encode_coding_utf_8
;
5811 coding
->common_flags
5812 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5813 if (CODING_UTF_8_BOM (coding
) == utf_detect_bom
)
5814 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5816 else if (EQ (coding_type
, Qutf_16
))
5818 val
= AREF (attrs
, coding_attr_utf_bom
);
5819 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_detect_bom
5820 : EQ (val
, Qt
) ? utf_with_bom
5822 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5823 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5824 : utf_16_little_endian
);
5825 CODING_UTF_16_SURROGATE (coding
) = 0;
5826 coding
->detector
= detect_coding_utf_16
;
5827 coding
->decoder
= decode_coding_utf_16
;
5828 coding
->encoder
= encode_coding_utf_16
;
5829 coding
->common_flags
5830 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5831 if (CODING_UTF_16_BOM (coding
) == utf_detect_bom
)
5832 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5834 else if (EQ (coding_type
, Qccl
))
5836 coding
->detector
= detect_coding_ccl
;
5837 coding
->decoder
= decode_coding_ccl
;
5838 coding
->encoder
= encode_coding_ccl
;
5839 coding
->common_flags
5840 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5841 | CODING_REQUIRE_FLUSHING_MASK
);
5843 else if (EQ (coding_type
, Qemacs_mule
))
5845 coding
->detector
= detect_coding_emacs_mule
;
5846 coding
->decoder
= decode_coding_emacs_mule
;
5847 coding
->encoder
= encode_coding_emacs_mule
;
5848 coding
->common_flags
5849 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5850 coding
->spec
.emacs_mule
.full_support
= 1;
5851 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5852 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5854 Lisp_Object tail
, safe_charsets
;
5855 int max_charset_id
= 0;
5857 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5859 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5860 max_charset_id
= XFASTINT (XCAR (tail
));
5861 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
5862 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
5863 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5865 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5866 coding
->max_charset_id
= max_charset_id
;
5867 coding
->safe_charsets
= SDATA (safe_charsets
);
5868 coding
->spec
.emacs_mule
.full_support
= 1;
5870 coding
->spec
.emacs_mule
.cmp_status
.state
= COMPOSING_NO
;
5871 coding
->spec
.emacs_mule
.cmp_status
.method
= COMPOSITION_NO
;
5873 else if (EQ (coding_type
, Qshift_jis
))
5875 coding
->detector
= detect_coding_sjis
;
5876 coding
->decoder
= decode_coding_sjis
;
5877 coding
->encoder
= encode_coding_sjis
;
5878 coding
->common_flags
5879 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5881 else if (EQ (coding_type
, Qbig5
))
5883 coding
->detector
= detect_coding_big5
;
5884 coding
->decoder
= decode_coding_big5
;
5885 coding
->encoder
= encode_coding_big5
;
5886 coding
->common_flags
5887 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5889 else /* EQ (coding_type, Qraw_text) */
5891 coding
->detector
= NULL
;
5892 coding
->decoder
= decode_coding_raw_text
;
5893 coding
->encoder
= encode_coding_raw_text
;
5894 if (! EQ (eol_type
, Qunix
))
5896 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5897 if (! VECTORP (eol_type
))
5898 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5906 /* Return a list of charsets supported by CODING. */
5909 coding_charset_list (struct coding_system
*coding
)
5911 Lisp_Object attrs
, charset_list
;
5913 CODING_GET_INFO (coding
, attrs
, charset_list
);
5914 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5916 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5918 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5919 charset_list
= Viso_2022_charset_list
;
5921 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5923 charset_list
= Vemacs_mule_charset_list
;
5925 return charset_list
;
5929 /* Return a list of charsets supported by CODING-SYSTEM. */
5932 coding_system_charset_list (Lisp_Object coding_system
)
5935 Lisp_Object attrs
, charset_list
;
5937 CHECK_CODING_SYSTEM_GET_ID (coding_system
, id
);
5938 attrs
= CODING_ID_ATTRS (id
);
5940 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5942 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5944 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5945 charset_list
= Viso_2022_charset_list
;
5947 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5949 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5951 charset_list
= Vemacs_mule_charset_list
;
5955 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
5957 return charset_list
;
5961 /* Return raw-text or one of its subsidiaries that has the same
5962 eol_type as CODING-SYSTEM. */
5965 raw_text_coding_system (Lisp_Object coding_system
)
5967 Lisp_Object spec
, attrs
;
5968 Lisp_Object eol_type
, raw_text_eol_type
;
5970 if (NILP (coding_system
))
5972 spec
= CODING_SYSTEM_SPEC (coding_system
);
5973 attrs
= AREF (spec
, 0);
5975 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5976 return coding_system
;
5978 eol_type
= AREF (spec
, 2);
5979 if (VECTORP (eol_type
))
5981 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5982 raw_text_eol_type
= AREF (spec
, 2);
5983 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5984 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5985 : AREF (raw_text_eol_type
, 2));
5989 /* If CODING_SYSTEM doesn't specify end-of-line format, return one of
5990 the subsidiary that has the same eol-spec as PARENT (if it is not
5991 nil and specifies end-of-line format) or the system's setting
5992 (system_eol_type). */
5995 coding_inherit_eol_type (Lisp_Object coding_system
, Lisp_Object parent
)
5997 Lisp_Object spec
, eol_type
;
5999 if (NILP (coding_system
))
6000 coding_system
= Qraw_text
;
6001 spec
= CODING_SYSTEM_SPEC (coding_system
);
6002 eol_type
= AREF (spec
, 2);
6003 if (VECTORP (eol_type
))
6005 Lisp_Object parent_eol_type
;
6007 if (! NILP (parent
))
6009 Lisp_Object parent_spec
;
6011 parent_spec
= CODING_SYSTEM_SPEC (parent
);
6012 parent_eol_type
= AREF (parent_spec
, 2);
6013 if (VECTORP (parent_eol_type
))
6014 parent_eol_type
= system_eol_type
;
6017 parent_eol_type
= system_eol_type
;
6018 if (EQ (parent_eol_type
, Qunix
))
6019 coding_system
= AREF (eol_type
, 0);
6020 else if (EQ (parent_eol_type
, Qdos
))
6021 coding_system
= AREF (eol_type
, 1);
6022 else if (EQ (parent_eol_type
, Qmac
))
6023 coding_system
= AREF (eol_type
, 2);
6025 return coding_system
;
6029 /* Check if text-conversion and eol-conversion of CODING_SYSTEM are
6030 decided for writing to a process. If not, complement them, and
6031 return a new coding system. */
6034 complement_process_encoding_system (Lisp_Object coding_system
)
6036 Lisp_Object coding_base
= Qnil
, eol_base
= Qnil
;
6037 Lisp_Object spec
, attrs
;
6040 for (i
= 0; i
< 3; i
++)
6043 coding_system
= CDR_SAFE (Vdefault_process_coding_system
);
6045 coding_system
= preferred_coding_system ();
6046 spec
= CODING_SYSTEM_SPEC (coding_system
);
6049 attrs
= AREF (spec
, 0);
6050 if (NILP (coding_base
) && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
6051 coding_base
= CODING_ATTR_BASE_NAME (attrs
);
6052 if (NILP (eol_base
) && ! VECTORP (AREF (spec
, 2)))
6053 eol_base
= coding_system
;
6054 if (! NILP (coding_base
) && ! NILP (eol_base
))
6059 /* The original CODING_SYSTEM didn't specify text-conversion or
6060 eol-conversion. Be sure that we return a fully complemented
6062 coding_system
= coding_inherit_eol_type (coding_base
, eol_base
);
6063 return coding_system
;
6067 /* Emacs has a mechanism to automatically detect a coding system if it
6068 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
6069 it's impossible to distinguish some coding systems accurately
6070 because they use the same range of codes. So, at first, coding
6071 systems are categorized into 7, those are:
6073 o coding-category-emacs-mule
6075 The category for a coding system which has the same code range
6076 as Emacs' internal format. Assigned the coding-system (Lisp
6077 symbol) `emacs-mule' by default.
6079 o coding-category-sjis
6081 The category for a coding system which has the same code range
6082 as SJIS. Assigned the coding-system (Lisp
6083 symbol) `japanese-shift-jis' by default.
6085 o coding-category-iso-7
6087 The category for a coding system which has the same code range
6088 as ISO2022 of 7-bit environment. This doesn't use any locking
6089 shift and single shift functions. This can encode/decode all
6090 charsets. Assigned the coding-system (Lisp symbol)
6091 `iso-2022-7bit' by default.
6093 o coding-category-iso-7-tight
6095 Same as coding-category-iso-7 except that this can
6096 encode/decode only the specified charsets.
6098 o coding-category-iso-8-1
6100 The category for a coding system which has the same code range
6101 as ISO2022 of 8-bit environment and graphic plane 1 used only
6102 for DIMENSION1 charset. This doesn't use any locking shift
6103 and single shift functions. Assigned the coding-system (Lisp
6104 symbol) `iso-latin-1' by default.
6106 o coding-category-iso-8-2
6108 The category for a coding system which has the same code range
6109 as ISO2022 of 8-bit environment and graphic plane 1 used only
6110 for DIMENSION2 charset. This doesn't use any locking shift
6111 and single shift functions. Assigned the coding-system (Lisp
6112 symbol) `japanese-iso-8bit' by default.
6114 o coding-category-iso-7-else
6116 The category for a coding system which has the same code range
6117 as ISO2022 of 7-bit environment but uses locking shift or
6118 single shift functions. Assigned the coding-system (Lisp
6119 symbol) `iso-2022-7bit-lock' by default.
6121 o coding-category-iso-8-else
6123 The category for a coding system which has the same code range
6124 as ISO2022 of 8-bit environment but uses locking shift or
6125 single shift functions. Assigned the coding-system (Lisp
6126 symbol) `iso-2022-8bit-ss2' by default.
6128 o coding-category-big5
6130 The category for a coding system which has the same code range
6131 as BIG5. Assigned the coding-system (Lisp symbol)
6132 `cn-big5' by default.
6134 o coding-category-utf-8
6136 The category for a coding system which has the same code range
6137 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
6138 symbol) `utf-8' by default.
6140 o coding-category-utf-16-be
6142 The category for a coding system in which a text has an
6143 Unicode signature (cf. Unicode Standard) in the order of BIG
6144 endian at the head. Assigned the coding-system (Lisp symbol)
6145 `utf-16-be' by default.
6147 o coding-category-utf-16-le
6149 The category for a coding system in which a text has an
6150 Unicode signature (cf. Unicode Standard) in the order of
6151 LITTLE endian at the head. Assigned the coding-system (Lisp
6152 symbol) `utf-16-le' by default.
6154 o coding-category-ccl
6156 The category for a coding system of which encoder/decoder is
6157 written in CCL programs. The default value is nil, i.e., no
6158 coding system is assigned.
6160 o coding-category-binary
6162 The category for a coding system not categorized in any of the
6163 above. Assigned the coding-system (Lisp symbol)
6164 `no-conversion' by default.
6166 Each of them is a Lisp symbol and the value is an actual
6167 `coding-system's (this is also a Lisp symbol) assigned by a user.
6168 What Emacs does actually is to detect a category of coding system.
6169 Then, it uses a `coding-system' assigned to it. If Emacs can't
6170 decide only one possible category, it selects a category of the
6171 highest priority. Priorities of categories are also specified by a
6172 user in a Lisp variable `coding-category-list'.
6176 #define EOL_SEEN_NONE 0
6177 #define EOL_SEEN_LF 1
6178 #define EOL_SEEN_CR 2
6179 #define EOL_SEEN_CRLF 4
6181 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
6182 SOURCE is encoded. If CATEGORY is one of
6183 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
6184 two-byte, else they are encoded by one-byte.
6186 Return one of EOL_SEEN_XXX. */
6188 #define MAX_EOL_CHECK_COUNT 3
6191 detect_eol (const unsigned char *source
, EMACS_INT src_bytes
,
6192 enum coding_category category
)
6194 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
6197 int eol_seen
= EOL_SEEN_NONE
;
6199 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
6203 msb
= category
== (coding_category_utf_16_le
6204 | coding_category_utf_16_le_nosig
);
6207 while (src
+ 1 < src_end
)
6210 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
6215 this_eol
= EOL_SEEN_LF
;
6216 else if (src
+ 3 >= src_end
6217 || src
[msb
+ 2] != 0
6218 || src
[lsb
+ 2] != '\n')
6219 this_eol
= EOL_SEEN_CR
;
6222 this_eol
= EOL_SEEN_CRLF
;
6226 if (eol_seen
== EOL_SEEN_NONE
)
6227 /* This is the first end-of-line. */
6228 eol_seen
= this_eol
;
6229 else if (eol_seen
!= this_eol
)
6231 /* The found type is different from what found before.
6232 Allow for stray ^M characters in DOS EOL files. */
6233 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6234 || (eol_seen
== EOL_SEEN_CRLF
6235 && this_eol
== EOL_SEEN_CR
))
6236 eol_seen
= EOL_SEEN_CRLF
;
6239 eol_seen
= EOL_SEEN_LF
;
6243 if (++total
== MAX_EOL_CHECK_COUNT
)
6250 while (src
< src_end
)
6253 if (c
== '\n' || c
== '\r')
6258 this_eol
= EOL_SEEN_LF
;
6259 else if (src
>= src_end
|| *src
!= '\n')
6260 this_eol
= EOL_SEEN_CR
;
6262 this_eol
= EOL_SEEN_CRLF
, src
++;
6264 if (eol_seen
== EOL_SEEN_NONE
)
6265 /* This is the first end-of-line. */
6266 eol_seen
= this_eol
;
6267 else if (eol_seen
!= this_eol
)
6269 /* The found type is different from what found before.
6270 Allow for stray ^M characters in DOS EOL files. */
6271 if ((eol_seen
== EOL_SEEN_CR
&& this_eol
== EOL_SEEN_CRLF
)
6272 || (eol_seen
== EOL_SEEN_CRLF
&& this_eol
== EOL_SEEN_CR
))
6273 eol_seen
= EOL_SEEN_CRLF
;
6276 eol_seen
= EOL_SEEN_LF
;
6280 if (++total
== MAX_EOL_CHECK_COUNT
)
6289 adjust_coding_eol_type (struct coding_system
*coding
, int eol_seen
)
6291 Lisp_Object eol_type
;
6293 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6294 if (eol_seen
& EOL_SEEN_LF
)
6296 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
6299 else if (eol_seen
& EOL_SEEN_CRLF
)
6301 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
6304 else if (eol_seen
& EOL_SEEN_CR
)
6306 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
6312 /* Detect how a text specified in CODING is encoded. If a coding
6313 system is detected, update fields of CODING by the detected coding
6317 detect_coding (struct coding_system
*coding
)
6319 const unsigned char *src
, *src_end
;
6320 int saved_mode
= coding
->mode
;
6322 coding
->consumed
= coding
->consumed_char
= 0;
6323 coding
->produced
= coding
->produced_char
= 0;
6324 coding_set_source (coding
);
6326 src_end
= coding
->source
+ coding
->src_bytes
;
6327 coding
->head_ascii
= 0;
6329 /* If we have not yet decided the text encoding type, detect it
6331 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
6334 struct coding_detection_info detect_info
;
6335 int null_byte_found
= 0, eight_bit_found
= 0;
6337 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
6338 for (src
= coding
->source
; src
< src_end
; src
++)
6343 eight_bit_found
= 1;
6344 if (null_byte_found
)
6349 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
6350 && ! inhibit_iso_escape_detection
6351 && ! detect_info
.checked
)
6353 if (detect_coding_iso_2022 (coding
, &detect_info
))
6355 /* We have scanned the whole data. */
6356 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
6358 /* We didn't find an 8-bit code. We may
6359 have found a null-byte, but it's very
6360 rare that a binary file conforms to
6363 coding
->head_ascii
= src
- coding
->source
;
6365 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
6369 else if (! c
&& !inhibit_null_byte_detection
)
6371 null_byte_found
= 1;
6372 if (eight_bit_found
)
6375 if (! eight_bit_found
)
6376 coding
->head_ascii
++;
6378 else if (! eight_bit_found
)
6379 coding
->head_ascii
++;
6382 if (null_byte_found
|| eight_bit_found
6383 || coding
->head_ascii
< coding
->src_bytes
6384 || detect_info
.found
)
6386 enum coding_category category
;
6387 struct coding_system
*this;
6389 if (coding
->head_ascii
== coding
->src_bytes
)
6390 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
6391 for (i
= 0; i
< coding_category_raw_text
; i
++)
6393 category
= coding_priorities
[i
];
6394 this = coding_categories
+ category
;
6395 if (detect_info
.found
& (1 << category
))
6400 if (null_byte_found
)
6402 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
6403 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
6405 for (i
= 0; i
< coding_category_raw_text
; i
++)
6407 category
= coding_priorities
[i
];
6408 this = coding_categories
+ category
;
6411 /* No coding system of this category is defined. */
6412 detect_info
.rejected
|= (1 << category
);
6414 else if (category
>= coding_category_raw_text
)
6416 else if (detect_info
.checked
& (1 << category
))
6418 if (detect_info
.found
& (1 << category
))
6421 else if ((*(this->detector
)) (coding
, &detect_info
)
6422 && detect_info
.found
& (1 << category
))
6424 if (category
== coding_category_utf_16_auto
)
6426 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6427 category
= coding_category_utf_16_le
;
6429 category
= coding_category_utf_16_be
;
6436 if (i
< coding_category_raw_text
)
6437 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6438 else if (null_byte_found
)
6439 setup_coding_system (Qno_conversion
, coding
);
6440 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
6441 == CATEGORY_MASK_ANY
)
6442 setup_coding_system (Qraw_text
, coding
);
6443 else if (detect_info
.rejected
)
6444 for (i
= 0; i
< coding_category_raw_text
; i
++)
6445 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
6447 this = coding_categories
+ coding_priorities
[i
];
6448 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
6453 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6454 == coding_category_utf_8_auto
)
6456 Lisp_Object coding_systems
;
6457 struct coding_detection_info detect_info
;
6460 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6461 detect_info
.found
= detect_info
.rejected
= 0;
6462 coding
->head_ascii
= 0;
6463 if (CONSP (coding_systems
)
6464 && detect_coding_utf_8 (coding
, &detect_info
))
6466 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
6467 setup_coding_system (XCAR (coding_systems
), coding
);
6469 setup_coding_system (XCDR (coding_systems
), coding
);
6472 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
6473 == coding_category_utf_16_auto
)
6475 Lisp_Object coding_systems
;
6476 struct coding_detection_info detect_info
;
6479 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_bom
);
6480 detect_info
.found
= detect_info
.rejected
= 0;
6481 coding
->head_ascii
= 0;
6482 if (CONSP (coding_systems
)
6483 && detect_coding_utf_16 (coding
, &detect_info
))
6485 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
6486 setup_coding_system (XCAR (coding_systems
), coding
);
6487 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
6488 setup_coding_system (XCDR (coding_systems
), coding
);
6491 coding
->mode
= saved_mode
;
6496 decode_eol (struct coding_system
*coding
)
6498 Lisp_Object eol_type
;
6499 unsigned char *p
, *pbeg
, *pend
;
6501 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6502 if (EQ (eol_type
, Qunix
) || inhibit_eol_conversion
)
6505 if (NILP (coding
->dst_object
))
6506 pbeg
= coding
->destination
;
6508 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
6509 pend
= pbeg
+ coding
->produced
;
6511 if (VECTORP (eol_type
))
6513 int eol_seen
= EOL_SEEN_NONE
;
6515 for (p
= pbeg
; p
< pend
; p
++)
6518 eol_seen
|= EOL_SEEN_LF
;
6519 else if (*p
== '\r')
6521 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
6523 eol_seen
|= EOL_SEEN_CRLF
;
6527 eol_seen
|= EOL_SEEN_CR
;
6530 /* Handle DOS-style EOLs in a file with stray ^M characters. */
6531 if ((eol_seen
& EOL_SEEN_CRLF
) != 0
6532 && (eol_seen
& EOL_SEEN_CR
) != 0
6533 && (eol_seen
& EOL_SEEN_LF
) == 0)
6534 eol_seen
= EOL_SEEN_CRLF
;
6535 else if (eol_seen
!= EOL_SEEN_NONE
6536 && eol_seen
!= EOL_SEEN_LF
6537 && eol_seen
!= EOL_SEEN_CRLF
6538 && eol_seen
!= EOL_SEEN_CR
)
6539 eol_seen
= EOL_SEEN_LF
;
6540 if (eol_seen
!= EOL_SEEN_NONE
)
6541 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
6544 if (EQ (eol_type
, Qmac
))
6546 for (p
= pbeg
; p
< pend
; p
++)
6550 else if (EQ (eol_type
, Qdos
))
6554 if (NILP (coding
->dst_object
))
6556 /* Start deleting '\r' from the tail to minimize the memory
6558 for (p
= pend
- 2; p
>= pbeg
; p
--)
6561 memmove (p
, p
+ 1, pend
-- - p
- 1);
6567 EMACS_INT pos_byte
= coding
->dst_pos_byte
;
6568 EMACS_INT pos
= coding
->dst_pos
;
6569 EMACS_INT pos_end
= pos
+ coding
->produced_char
- 1;
6571 while (pos
< pos_end
)
6573 p
= BYTE_POS_ADDR (pos_byte
);
6574 if (*p
== '\r' && p
[1] == '\n')
6576 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
6581 if (coding
->dst_multibyte
)
6582 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
6587 coding
->produced
-= n
;
6588 coding
->produced_char
-= n
;
6593 /* Return a translation table (or list of them) from coding system
6594 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
6595 decoding (ENCODEP is zero). */
6598 get_translation_table (Lisp_Object attrs
, int encodep
, int *max_lookup
)
6600 Lisp_Object standard
, translation_table
;
6603 if (NILP (Venable_character_translation
))
6610 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
6611 standard
= Vstandard_translation_table_for_encode
;
6613 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
6614 standard
= Vstandard_translation_table_for_decode
;
6615 if (NILP (translation_table
))
6616 translation_table
= standard
;
6619 if (SYMBOLP (translation_table
))
6620 translation_table
= Fget (translation_table
, Qtranslation_table
);
6621 else if (CONSP (translation_table
))
6623 translation_table
= Fcopy_sequence (translation_table
);
6624 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
6625 if (SYMBOLP (XCAR (val
)))
6626 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
6628 if (CHAR_TABLE_P (standard
))
6630 if (CONSP (translation_table
))
6631 translation_table
= nconc2 (translation_table
,
6632 Fcons (standard
, Qnil
));
6634 translation_table
= Fcons (translation_table
,
6635 Fcons (standard
, Qnil
));
6642 if (CHAR_TABLE_P (translation_table
)
6643 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
6645 val
= XCHAR_TABLE (translation_table
)->extras
[1];
6646 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6647 *max_lookup
= XFASTINT (val
);
6649 else if (CONSP (translation_table
))
6653 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6654 if (CHAR_TABLE_P (XCAR (tail
))
6655 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6657 Lisp_Object tailval
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6658 if (NATNUMP (tailval
) && *max_lookup
< XFASTINT (tailval
))
6659 *max_lookup
= XFASTINT (tailval
);
6663 return translation_table
;
6666 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6669 if (CHAR_TABLE_P (table)) \
6671 trans = CHAR_TABLE_REF (table, c); \
6672 if (CHARACTERP (trans)) \
6673 c = XFASTINT (trans), trans = Qnil; \
6675 else if (CONSP (table)) \
6679 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6680 if (CHAR_TABLE_P (XCAR (tail))) \
6682 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6683 if (CHARACTERP (trans)) \
6684 c = XFASTINT (trans), trans = Qnil; \
6685 else if (! NILP (trans)) \
6692 /* Return a translation of character(s) at BUF according to TRANS.
6693 TRANS is TO-CHAR or ((FROM . TO) ...) where
6694 FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...].
6695 The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a
6696 translation is found, and Qnil if not found..
6697 If BUF is too short to lookup characters in FROM, return Qt. */
6700 get_translation (Lisp_Object trans
, int *buf
, int *buf_end
)
6703 if (INTEGERP (trans
))
6705 for (; CONSP (trans
); trans
= XCDR (trans
))
6707 Lisp_Object val
= XCAR (trans
);
6708 Lisp_Object from
= XCAR (val
);
6709 int len
= ASIZE (from
);
6712 for (i
= 0; i
< len
; i
++)
6714 if (buf
+ i
== buf_end
)
6716 if (XINT (AREF (from
, i
)) != buf
[i
])
6727 produce_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
6730 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6731 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6733 EMACS_INT produced_chars
= 0;
6736 if (! coding
->chars_at_source
)
6738 /* Source characters are in coding->charbuf. */
6739 int *buf
= coding
->charbuf
;
6740 int *buf_end
= buf
+ coding
->charbuf_used
;
6742 if (EQ (coding
->src_object
, coding
->dst_object
))
6744 coding_set_source (coding
);
6745 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6748 while (buf
< buf_end
)
6754 EMACS_INT from_nchars
= 1, to_nchars
= 1;
6755 Lisp_Object trans
= Qnil
;
6757 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6760 trans
= get_translation (trans
, buf
, buf_end
);
6761 if (INTEGERP (trans
))
6763 else if (CONSP (trans
))
6765 from_nchars
= ASIZE (XCAR (trans
));
6766 trans
= XCDR (trans
);
6767 if (INTEGERP (trans
))
6771 to_nchars
= ASIZE (trans
);
6772 c
= XINT (AREF (trans
, 0));
6775 else if (EQ (trans
, Qt
) && ! last_block
)
6779 if ((dst_end
- dst
) / MAX_MULTIBYTE_LENGTH
< to_nchars
)
6781 if (((min (PTRDIFF_MAX
, SIZE_MAX
) - (buf_end
- buf
))
6782 / MAX_MULTIBYTE_LENGTH
)
6784 memory_full (SIZE_MAX
);
6785 dst
= alloc_destination (coding
,
6787 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6789 if (EQ (coding
->src_object
, coding
->dst_object
))
6791 coding_set_source (coding
);
6792 dst_end
= (((unsigned char *) coding
->source
)
6793 + coding
->consumed
);
6796 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6799 for (i
= 0; i
< to_nchars
; i
++)
6802 c
= XINT (AREF (trans
, i
));
6803 if (coding
->dst_multibyte
6804 || ! CHAR_BYTE8_P (c
))
6805 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6807 *dst
++ = CHAR_TO_BYTE8 (c
);
6809 produced_chars
+= to_nchars
;
6813 /* This is an annotation datum. (-C) is the length. */
6816 carryover
= buf_end
- buf
;
6820 /* Source characters are at coding->source. */
6821 const unsigned char *src
= coding
->source
;
6822 const unsigned char *src_end
= src
+ coding
->consumed
;
6824 if (EQ (coding
->dst_object
, coding
->src_object
))
6825 dst_end
= (unsigned char *) src
;
6826 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6828 if (coding
->src_multibyte
)
6831 EMACS_INT consumed_chars
= 0;
6835 const unsigned char *src_base
= src
;
6841 if (EQ (coding
->src_object
, coding
->dst_object
))
6842 dst_end
= (unsigned char *) src
;
6845 EMACS_INT offset
= src
- coding
->source
;
6847 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6849 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6850 coding_set_source (coding
);
6851 src
= coding
->source
+ offset
;
6852 src_end
= coding
->source
+ coding
->src_bytes
;
6853 if (EQ (coding
->src_object
, coding
->dst_object
))
6854 dst_end
= (unsigned char *) src
;
6864 while (src
< src_end
)
6869 if (dst
>= dst_end
- 1)
6871 if (EQ (coding
->src_object
, coding
->dst_object
))
6872 dst_end
= (unsigned char *) src
;
6873 if (dst
>= dst_end
- 1)
6875 EMACS_INT offset
= src
- coding
->source
;
6876 EMACS_INT more_bytes
;
6878 if (EQ (coding
->src_object
, coding
->dst_object
))
6879 more_bytes
= ((src_end
- src
) / 2) + 2;
6881 more_bytes
= src_end
- src
+ 2;
6882 dst
= alloc_destination (coding
, more_bytes
, dst
);
6883 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6884 coding_set_source (coding
);
6885 src
= coding
->source
+ offset
;
6886 src_end
= coding
->source
+ coding
->src_bytes
;
6887 if (EQ (coding
->src_object
, coding
->dst_object
))
6888 dst_end
= (unsigned char *) src
;
6896 if (!EQ (coding
->src_object
, coding
->dst_object
))
6898 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6902 EMACS_INT offset
= src
- coding
->source
;
6904 dst
= alloc_destination (coding
, require
, dst
);
6905 coding_set_source (coding
);
6906 src
= coding
->source
+ offset
;
6907 src_end
= coding
->source
+ coding
->src_bytes
;
6910 produced_chars
= coding
->consumed_char
;
6911 while (src
< src_end
)
6916 produced
= dst
- (coding
->destination
+ coding
->produced
);
6917 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6918 insert_from_gap (produced_chars
, produced
);
6919 coding
->produced
+= produced
;
6920 coding
->produced_char
+= produced_chars
;
6924 /* Compose text in CODING->object according to the annotation data at
6925 CHARBUF. CHARBUF is an array:
6926 [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ]
6930 produce_composition (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6934 enum composition_method method
;
6935 Lisp_Object components
;
6937 len
= -charbuf
[0] - MAX_ANNOTATION_LENGTH
;
6938 to
= pos
+ charbuf
[2];
6939 method
= (enum composition_method
) (charbuf
[4]);
6941 if (method
== COMPOSITION_RELATIVE
)
6945 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6948 if (method
== COMPOSITION_WITH_RULE
)
6949 len
= charbuf
[2] * 3 - 2;
6950 charbuf
+= MAX_ANNOTATION_LENGTH
;
6951 /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */
6952 for (i
= j
= 0; i
< len
&& charbuf
[i
] != -1; i
++, j
++)
6954 if (charbuf
[i
] >= 0)
6955 args
[j
] = make_number (charbuf
[i
]);
6959 args
[j
] = make_number (charbuf
[i
] % 0x100);
6962 components
= (i
== j
? Fstring (j
, args
) : Fvector (j
, args
));
6964 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6968 /* Put `charset' property on text in CODING->object according to
6969 the annotation data at CHARBUF. CHARBUF is an array:
6970 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6974 produce_charset (struct coding_system
*coding
, int *charbuf
, EMACS_INT pos
)
6976 EMACS_INT from
= pos
- charbuf
[2];
6977 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6979 Fput_text_property (make_number (from
), make_number (pos
),
6980 Qcharset
, CHARSET_NAME (charset
),
6981 coding
->dst_object
);
6985 #define CHARBUF_SIZE 0x4000
6987 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6989 int size = CHARBUF_SIZE; \
6991 coding->charbuf = NULL; \
6992 while (size > 1024) \
6994 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6995 if (coding->charbuf) \
6999 if (! coding->charbuf) \
7001 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
7002 return coding->result; \
7004 coding->charbuf_size = size; \
7009 produce_annotation (struct coding_system
*coding
, EMACS_INT pos
)
7011 int *charbuf
= coding
->charbuf
;
7012 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
7014 if (NILP (coding
->dst_object
))
7017 while (charbuf
< charbuf_end
)
7023 int len
= -*charbuf
;
7028 case CODING_ANNOTATE_COMPOSITION_MASK
:
7029 produce_composition (coding
, charbuf
, pos
);
7031 case CODING_ANNOTATE_CHARSET_MASK
:
7032 produce_charset (coding
, charbuf
, pos
);
7040 /* Decode the data at CODING->src_object into CODING->dst_object.
7041 CODING->src_object is a buffer, a string, or nil.
7042 CODING->dst_object is a buffer.
7044 If CODING->src_object is a buffer, it must be the current buffer.
7045 In this case, if CODING->src_pos is positive, it is a position of
7046 the source text in the buffer, otherwise, the source text is in the
7047 gap area of the buffer, and CODING->src_pos specifies the offset of
7048 the text from GPT (which must be the same as PT). If this is the
7049 same buffer as CODING->dst_object, CODING->src_pos must be
7052 If CODING->src_object is a string, CODING->src_pos is an index to
7055 If CODING->src_object is nil, CODING->source must already point to
7056 the non-relocatable memory area. In this case, CODING->src_pos is
7057 an offset from CODING->source.
7059 The decoded data is inserted at the current point of the buffer
7064 decode_coding (struct coding_system
*coding
)
7067 Lisp_Object undo_list
;
7068 Lisp_Object translation_table
;
7069 struct ccl_spec cclspec
;
7073 if (BUFFERP (coding
->src_object
)
7074 && coding
->src_pos
> 0
7075 && coding
->src_pos
< GPT
7076 && coding
->src_pos
+ coding
->src_chars
> GPT
)
7077 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
7080 if (BUFFERP (coding
->dst_object
))
7082 if (current_buffer
!= XBUFFER (coding
->dst_object
))
7083 set_buffer_internal (XBUFFER (coding
->dst_object
));
7085 move_gap_both (PT
, PT_BYTE
);
7086 undo_list
= BVAR (current_buffer
, undo_list
);
7087 BVAR (current_buffer
, undo_list
) = Qt
;
7090 coding
->consumed
= coding
->consumed_char
= 0;
7091 coding
->produced
= coding
->produced_char
= 0;
7092 coding
->chars_at_source
= 0;
7093 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7096 ALLOC_CONVERSION_WORK_AREA (coding
);
7098 attrs
= CODING_ID_ATTRS (coding
->id
);
7099 translation_table
= get_translation_table (attrs
, 0, NULL
);
7102 if (coding
->decoder
== decode_coding_ccl
)
7104 coding
->spec
.ccl
= &cclspec
;
7105 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_DECODER (coding
));
7109 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
7111 coding_set_source (coding
);
7112 coding
->annotated
= 0;
7113 coding
->charbuf_used
= carryover
;
7114 (*(coding
->decoder
)) (coding
);
7115 coding_set_destination (coding
);
7116 carryover
= produce_chars (coding
, translation_table
, 0);
7117 if (coding
->annotated
)
7118 produce_annotation (coding
, pos
);
7119 for (i
= 0; i
< carryover
; i
++)
7121 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
7123 while (coding
->result
== CODING_RESULT_INSUFFICIENT_DST
7124 || (coding
->consumed
< coding
->src_bytes
7125 && (coding
->result
== CODING_RESULT_SUCCESS
7126 || coding
->result
== CODING_RESULT_INVALID_SRC
)));
7130 coding_set_destination (coding
);
7131 coding
->charbuf_used
= carryover
;
7132 produce_chars (coding
, translation_table
, 1);
7135 coding
->carryover_bytes
= 0;
7136 if (coding
->consumed
< coding
->src_bytes
)
7138 int nbytes
= coding
->src_bytes
- coding
->consumed
;
7139 const unsigned char *src
;
7141 coding_set_source (coding
);
7142 coding_set_destination (coding
);
7143 src
= coding
->source
+ coding
->consumed
;
7145 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
7147 /* Flush out unprocessed data as binary chars. We are sure
7148 that the number of data is less than the size of
7150 coding
->charbuf_used
= 0;
7151 coding
->chars_at_source
= 0;
7153 while (nbytes
-- > 0)
7158 c
= BYTE8_TO_CHAR (c
);
7159 coding
->charbuf
[coding
->charbuf_used
++] = c
;
7161 produce_chars (coding
, Qnil
, 1);
7165 /* Record unprocessed bytes in coding->carryover. We are
7166 sure that the number of data is less than the size of
7167 coding->carryover. */
7168 unsigned char *p
= coding
->carryover
;
7170 if (nbytes
> sizeof coding
->carryover
)
7171 nbytes
= sizeof coding
->carryover
;
7172 coding
->carryover_bytes
= nbytes
;
7173 while (nbytes
-- > 0)
7176 coding
->consumed
= coding
->src_bytes
;
7179 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
)
7180 && !inhibit_eol_conversion
)
7181 decode_eol (coding
);
7182 if (BUFFERP (coding
->dst_object
))
7184 BVAR (current_buffer
, undo_list
) = undo_list
;
7185 record_insert (coding
->dst_pos
, coding
->produced_char
);
7187 return coding
->result
;
7191 /* Extract an annotation datum from a composition starting at POS and
7192 ending before LIMIT of CODING->src_object (buffer or string), store
7193 the data in BUF, set *STOP to a starting position of the next
7194 composition (if any) or to LIMIT, and return the address of the
7195 next element of BUF.
7197 If such an annotation is not found, set *STOP to a starting
7198 position of a composition after POS (if any) or to LIMIT, and
7202 handle_composition_annotation (EMACS_INT pos
, EMACS_INT limit
,
7203 struct coding_system
*coding
, int *buf
,
7206 EMACS_INT start
, end
;
7209 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
7212 else if (start
> pos
)
7218 /* We found a composition. Store the corresponding
7219 annotation data in BUF. */
7221 enum composition_method method
= COMPOSITION_METHOD (prop
);
7222 int nchars
= COMPOSITION_LENGTH (prop
);
7224 ADD_COMPOSITION_DATA (buf
, nchars
, 0, method
);
7225 if (method
!= COMPOSITION_RELATIVE
)
7227 Lisp_Object components
;
7230 components
= COMPOSITION_COMPONENTS (prop
);
7231 if (VECTORP (components
))
7233 len
= ASIZE (components
);
7234 for (i
= 0; i
< len
; i
++)
7235 *buf
++ = XINT (AREF (components
, i
));
7237 else if (STRINGP (components
))
7239 len
= SCHARS (components
);
7243 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
7247 else if (INTEGERP (components
))
7250 *buf
++ = XINT (components
);
7252 else if (CONSP (components
))
7254 for (len
= 0; CONSP (components
);
7255 len
++, components
= XCDR (components
))
7256 *buf
++ = XINT (XCAR (components
));
7264 if (find_composition (end
, limit
, &start
, &end
, &prop
,
7275 /* Extract an annotation datum from a text property `charset' at POS of
7276 CODING->src_object (buffer of string), store the data in BUF, set
7277 *STOP to the position where the value of `charset' property changes
7278 (limiting by LIMIT), and return the address of the next element of
7281 If the property value is nil, set *STOP to the position where the
7282 property value is non-nil (limiting by LIMIT), and return BUF. */
7285 handle_charset_annotation (EMACS_INT pos
, EMACS_INT limit
,
7286 struct coding_system
*coding
, int *buf
,
7289 Lisp_Object val
, next
;
7292 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
7293 if (! NILP (val
) && CHARSETP (val
))
7294 id
= XINT (CHARSET_SYMBOL_ID (val
));
7297 ADD_CHARSET_DATA (buf
, 0, id
);
7298 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
7300 make_number (limit
));
7301 *stop
= XINT (next
);
7307 consume_chars (struct coding_system
*coding
, Lisp_Object translation_table
,
7310 int *buf
= coding
->charbuf
;
7311 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
7312 const unsigned char *src
= coding
->source
+ coding
->consumed
;
7313 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
7314 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
7315 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
7316 int multibytep
= coding
->src_multibyte
;
7317 Lisp_Object eol_type
;
7319 EMACS_INT stop
, stop_composition
, stop_charset
;
7320 int *lookup_buf
= NULL
;
7322 if (! NILP (translation_table
))
7323 lookup_buf
= alloca (sizeof (int) * max_lookup
);
7325 eol_type
= inhibit_eol_conversion
? Qunix
: CODING_ID_EOL_TYPE (coding
->id
);
7326 if (VECTORP (eol_type
))
7329 /* Note: composition handling is not yet implemented. */
7330 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
7332 if (NILP (coding
->src_object
))
7333 stop
= stop_composition
= stop_charset
= end_pos
;
7336 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
7337 stop
= stop_composition
= pos
;
7339 stop
= stop_composition
= end_pos
;
7340 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
7341 stop
= stop_charset
= pos
;
7343 stop_charset
= end_pos
;
7346 /* Compensate for CRLF and conversion. */
7347 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
7348 while (buf
< buf_end
)
7356 if (pos
== stop_composition
)
7357 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
7358 buf
, &stop_composition
);
7359 if (pos
== stop_charset
)
7360 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
7361 buf
, &stop_charset
);
7362 stop
= (stop_composition
< stop_charset
7363 ? stop_composition
: stop_charset
);
7370 if (coding
->encoder
== encode_coding_raw_text
7371 || coding
->encoder
== encode_coding_ccl
)
7373 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
7374 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
7376 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
7379 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
7380 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
7382 if (! EQ (eol_type
, Qunix
))
7386 if (EQ (eol_type
, Qdos
))
7394 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
7399 int from_nchars
= 1, to_nchars
= 1;
7400 int *lookup_buf_end
;
7401 const unsigned char *p
= src
;
7405 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
7406 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
7407 lookup_buf_end
= lookup_buf
+ i
;
7408 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
);
7409 if (INTEGERP (trans
))
7411 else if (CONSP (trans
))
7413 from_nchars
= ASIZE (XCAR (trans
));
7414 trans
= XCDR (trans
);
7415 if (INTEGERP (trans
))
7419 to_nchars
= ASIZE (trans
);
7420 if (buf
+ to_nchars
> buf_end
)
7422 c
= XINT (AREF (trans
, 0));
7428 for (i
= 1; i
< to_nchars
; i
++)
7429 *buf
++ = XINT (AREF (trans
, i
));
7430 for (i
= 1; i
< from_nchars
; i
++, pos
++)
7431 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
7435 coding
->consumed
= src
- coding
->source
;
7436 coding
->consumed_char
= pos
- coding
->src_pos
;
7437 coding
->charbuf_used
= buf
- coding
->charbuf
;
7438 coding
->chars_at_source
= 0;
7442 /* Encode the text at CODING->src_object into CODING->dst_object.
7443 CODING->src_object is a buffer or a string.
7444 CODING->dst_object is a buffer or nil.
7446 If CODING->src_object is a buffer, it must be the current buffer.
7447 In this case, if CODING->src_pos is positive, it is a position of
7448 the source text in the buffer, otherwise. the source text is in the
7449 gap area of the buffer, and coding->src_pos specifies the offset of
7450 the text from GPT (which must be the same as PT). If this is the
7451 same buffer as CODING->dst_object, CODING->src_pos must be
7452 negative and CODING should not have `pre-write-conversion'.
7454 If CODING->src_object is a string, CODING should not have
7455 `pre-write-conversion'.
7457 If CODING->dst_object is a buffer, the encoded data is inserted at
7458 the current point of that buffer.
7460 If CODING->dst_object is nil, the encoded data is placed at the
7461 memory area specified by CODING->destination. */
7464 encode_coding (struct coding_system
*coding
)
7467 Lisp_Object translation_table
;
7469 struct ccl_spec cclspec
;
7471 attrs
= CODING_ID_ATTRS (coding
->id
);
7472 if (coding
->encoder
== encode_coding_raw_text
)
7473 translation_table
= Qnil
, max_lookup
= 0;
7475 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
7477 if (BUFFERP (coding
->dst_object
))
7479 set_buffer_internal (XBUFFER (coding
->dst_object
));
7480 coding
->dst_multibyte
7481 = ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7484 coding
->consumed
= coding
->consumed_char
= 0;
7485 coding
->produced
= coding
->produced_char
= 0;
7486 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
7489 ALLOC_CONVERSION_WORK_AREA (coding
);
7491 if (coding
->encoder
== encode_coding_ccl
)
7493 coding
->spec
.ccl
= &cclspec
;
7494 setup_ccl_program (&cclspec
.ccl
, CODING_CCL_ENCODER (coding
));
7497 coding_set_source (coding
);
7498 consume_chars (coding
, translation_table
, max_lookup
);
7499 coding_set_destination (coding
);
7500 (*(coding
->encoder
)) (coding
);
7501 } while (coding
->consumed_char
< coding
->src_chars
);
7503 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
7504 insert_from_gap (coding
->produced_char
, coding
->produced
);
7506 return (coding
->result
);
7510 /* Name (or base name) of work buffer for code conversion. */
7511 static Lisp_Object Vcode_conversion_workbuf_name
;
7513 /* A working buffer used by the top level conversion. Once it is
7514 created, it is never destroyed. It has the name
7515 Vcode_conversion_workbuf_name. The other working buffers are
7516 destroyed after the use is finished, and their names are modified
7517 versions of Vcode_conversion_workbuf_name. */
7518 static Lisp_Object Vcode_conversion_reused_workbuf
;
7520 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
7521 static int reused_workbuf_in_use
;
7524 /* Return a working buffer of code conversion. MULTIBYTE specifies the
7525 multibyteness of returning buffer. */
7528 make_conversion_work_buffer (int multibyte
)
7530 Lisp_Object name
, workbuf
;
7531 struct buffer
*current
;
7533 if (reused_workbuf_in_use
++)
7535 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
7536 workbuf
= Fget_buffer_create (name
);
7540 if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf
)))
7541 Vcode_conversion_reused_workbuf
7542 = Fget_buffer_create (Vcode_conversion_workbuf_name
);
7543 workbuf
= Vcode_conversion_reused_workbuf
;
7545 current
= current_buffer
;
7546 set_buffer_internal (XBUFFER (workbuf
));
7547 /* We can't allow modification hooks to run in the work buffer. For
7548 instance, directory_files_internal assumes that file decoding
7549 doesn't compile new regexps. */
7550 Fset (Fmake_local_variable (Qinhibit_modification_hooks
), Qt
);
7552 BVAR (current_buffer
, undo_list
) = Qt
;
7553 BVAR (current_buffer
, enable_multibyte_characters
) = multibyte
? Qt
: Qnil
;
7554 set_buffer_internal (current
);
7560 code_conversion_restore (Lisp_Object arg
)
7562 Lisp_Object current
, workbuf
;
7563 struct gcpro gcpro1
;
7566 current
= XCAR (arg
);
7567 workbuf
= XCDR (arg
);
7568 if (! NILP (workbuf
))
7570 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
7571 reused_workbuf_in_use
= 0;
7572 else if (! NILP (Fbuffer_live_p (workbuf
)))
7573 Fkill_buffer (workbuf
);
7575 set_buffer_internal (XBUFFER (current
));
7581 code_conversion_save (int with_work_buf
, int multibyte
)
7583 Lisp_Object workbuf
= Qnil
;
7586 workbuf
= make_conversion_work_buffer (multibyte
);
7587 record_unwind_protect (code_conversion_restore
,
7588 Fcons (Fcurrent_buffer (), workbuf
));
7593 decode_coding_gap (struct coding_system
*coding
,
7594 EMACS_INT chars
, EMACS_INT bytes
)
7596 int count
= SPECPDL_INDEX ();
7599 code_conversion_save (0, 0);
7601 coding
->src_object
= Fcurrent_buffer ();
7602 coding
->src_chars
= chars
;
7603 coding
->src_bytes
= bytes
;
7604 coding
->src_pos
= -chars
;
7605 coding
->src_pos_byte
= -bytes
;
7606 coding
->src_multibyte
= chars
< bytes
;
7607 coding
->dst_object
= coding
->src_object
;
7608 coding
->dst_pos
= PT
;
7609 coding
->dst_pos_byte
= PT_BYTE
;
7610 coding
->dst_multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
7612 if (CODING_REQUIRE_DETECTION (coding
))
7613 detect_coding (coding
);
7615 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
7616 current_buffer
->text
->inhibit_shrinking
= 1;
7617 decode_coding (coding
);
7618 current_buffer
->text
->inhibit_shrinking
= 0;
7620 attrs
= CODING_ID_ATTRS (coding
->id
);
7621 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7623 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7626 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7627 val
= call1 (CODING_ATTR_POST_READ (attrs
),
7628 make_number (coding
->produced_char
));
7630 coding
->produced_char
+= Z
- prev_Z
;
7631 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7634 unbind_to (count
, Qnil
);
7635 return coding
->result
;
7639 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
7640 SRC_OBJECT into DST_OBJECT by coding context CODING.
7642 SRC_OBJECT is a buffer, a string, or Qnil.
7644 If it is a buffer, the text is at point of the buffer. FROM and TO
7645 are positions in the buffer.
7647 If it is a string, the text is at the beginning of the string.
7648 FROM and TO are indices to the string.
7650 If it is nil, the text is at coding->source. FROM and TO are
7651 indices to coding->source.
7653 DST_OBJECT is a buffer, Qt, or Qnil.
7655 If it is a buffer, the decoded text is inserted at point of the
7656 buffer. If the buffer is the same as SRC_OBJECT, the source text
7659 If it is Qt, a string is made from the decoded text, and
7660 set in CODING->dst_object.
7662 If it is Qnil, the decoded text is stored at CODING->destination.
7663 The caller must allocate CODING->dst_bytes bytes at
7664 CODING->destination by xmalloc. If the decoded text is longer than
7665 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7669 decode_coding_object (struct coding_system
*coding
,
7670 Lisp_Object src_object
,
7671 EMACS_INT from
, EMACS_INT from_byte
,
7672 EMACS_INT to
, EMACS_INT to_byte
,
7673 Lisp_Object dst_object
)
7675 int count
= SPECPDL_INDEX ();
7676 unsigned char *destination
IF_LINT (= NULL
);
7677 EMACS_INT dst_bytes
IF_LINT (= 0);
7678 EMACS_INT chars
= to
- from
;
7679 EMACS_INT bytes
= to_byte
- from_byte
;
7681 int saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7682 int need_marker_adjustment
= 0;
7683 Lisp_Object old_deactivate_mark
;
7685 old_deactivate_mark
= Vdeactivate_mark
;
7687 if (NILP (dst_object
))
7689 destination
= coding
->destination
;
7690 dst_bytes
= coding
->dst_bytes
;
7693 coding
->src_object
= src_object
;
7694 coding
->src_chars
= chars
;
7695 coding
->src_bytes
= bytes
;
7696 coding
->src_multibyte
= chars
< bytes
;
7698 if (STRINGP (src_object
))
7700 coding
->src_pos
= from
;
7701 coding
->src_pos_byte
= from_byte
;
7703 else if (BUFFERP (src_object
))
7705 set_buffer_internal (XBUFFER (src_object
));
7707 move_gap_both (from
, from_byte
);
7708 if (EQ (src_object
, dst_object
))
7710 struct Lisp_Marker
*tail
;
7712 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7714 tail
->need_adjustment
7715 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7716 need_marker_adjustment
|= tail
->need_adjustment
;
7718 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7719 TEMP_SET_PT_BOTH (from
, from_byte
);
7720 current_buffer
->text
->inhibit_shrinking
= 1;
7721 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7722 coding
->src_pos
= -chars
;
7723 coding
->src_pos_byte
= -bytes
;
7727 coding
->src_pos
= from
;
7728 coding
->src_pos_byte
= from_byte
;
7732 if (CODING_REQUIRE_DETECTION (coding
))
7733 detect_coding (coding
);
7734 attrs
= CODING_ID_ATTRS (coding
->id
);
7736 if (EQ (dst_object
, Qt
)
7737 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7738 && NILP (dst_object
)))
7740 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7741 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7742 coding
->dst_pos
= BEG
;
7743 coding
->dst_pos_byte
= BEG_BYTE
;
7745 else if (BUFFERP (dst_object
))
7747 code_conversion_save (0, 0);
7748 coding
->dst_object
= dst_object
;
7749 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7750 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7751 coding
->dst_multibyte
7752 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7756 code_conversion_save (0, 0);
7757 coding
->dst_object
= Qnil
;
7758 /* Most callers presume this will return a multibyte result, and they
7759 won't use `binary' or `raw-text' anyway, so let's not worry about
7760 CODING_FOR_UNIBYTE. */
7761 coding
->dst_multibyte
= 1;
7764 decode_coding (coding
);
7766 if (BUFFERP (coding
->dst_object
))
7767 set_buffer_internal (XBUFFER (coding
->dst_object
));
7769 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7771 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7772 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7775 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7776 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7777 old_deactivate_mark
);
7778 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7779 make_number (coding
->produced_char
));
7782 coding
->produced_char
+= Z
- prev_Z
;
7783 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7786 if (EQ (dst_object
, Qt
))
7788 coding
->dst_object
= Fbuffer_string ();
7790 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7792 set_buffer_internal (XBUFFER (coding
->dst_object
));
7793 if (dst_bytes
< coding
->produced
)
7795 destination
= xrealloc (destination
, coding
->produced
);
7798 record_conversion_result (coding
,
7799 CODING_RESULT_INSUFFICIENT_MEM
);
7800 unbind_to (count
, Qnil
);
7803 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7804 move_gap_both (BEGV
, BEGV_BYTE
);
7805 memcpy (destination
, BEGV_ADDR
, coding
->produced
);
7806 coding
->destination
= destination
;
7812 /* This is the case of:
7813 (BUFFERP (src_object) && EQ (src_object, dst_object))
7814 As we have moved PT while replacing the original buffer
7815 contents, we must recover it now. */
7816 set_buffer_internal (XBUFFER (src_object
));
7817 current_buffer
->text
->inhibit_shrinking
= 0;
7818 if (saved_pt
< from
)
7819 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7820 else if (saved_pt
< from
+ chars
)
7821 TEMP_SET_PT_BOTH (from
, from_byte
);
7822 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
7823 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7824 saved_pt_byte
+ (coding
->produced
- bytes
));
7826 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7827 saved_pt_byte
+ (coding
->produced
- bytes
));
7829 if (need_marker_adjustment
)
7831 struct Lisp_Marker
*tail
;
7833 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7834 if (tail
->need_adjustment
)
7836 tail
->need_adjustment
= 0;
7837 if (tail
->insertion_type
)
7839 tail
->bytepos
= from_byte
;
7840 tail
->charpos
= from
;
7844 tail
->bytepos
= from_byte
+ coding
->produced
;
7846 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
7847 ? tail
->bytepos
: from
+ coding
->produced_char
);
7853 Vdeactivate_mark
= old_deactivate_mark
;
7854 unbind_to (count
, coding
->dst_object
);
7859 encode_coding_object (struct coding_system
*coding
,
7860 Lisp_Object src_object
,
7861 EMACS_INT from
, EMACS_INT from_byte
,
7862 EMACS_INT to
, EMACS_INT to_byte
,
7863 Lisp_Object dst_object
)
7865 int count
= SPECPDL_INDEX ();
7866 EMACS_INT chars
= to
- from
;
7867 EMACS_INT bytes
= to_byte
- from_byte
;
7869 int saved_pt
= -1, saved_pt_byte
IF_LINT (= 0);
7870 int need_marker_adjustment
= 0;
7871 int kill_src_buffer
= 0;
7872 Lisp_Object old_deactivate_mark
;
7874 old_deactivate_mark
= Vdeactivate_mark
;
7876 coding
->src_object
= src_object
;
7877 coding
->src_chars
= chars
;
7878 coding
->src_bytes
= bytes
;
7879 coding
->src_multibyte
= chars
< bytes
;
7881 attrs
= CODING_ID_ATTRS (coding
->id
);
7883 if (EQ (src_object
, dst_object
))
7885 struct Lisp_Marker
*tail
;
7887 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7889 tail
->need_adjustment
7890 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7891 need_marker_adjustment
|= tail
->need_adjustment
;
7895 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7897 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7898 set_buffer_internal (XBUFFER (coding
->src_object
));
7899 if (STRINGP (src_object
))
7900 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7901 else if (BUFFERP (src_object
))
7902 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7904 insert_1_both ((char *) coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7906 if (EQ (src_object
, dst_object
))
7908 set_buffer_internal (XBUFFER (src_object
));
7909 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7910 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7911 set_buffer_internal (XBUFFER (coding
->src_object
));
7915 Lisp_Object args
[3];
7916 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7918 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7919 old_deactivate_mark
);
7920 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7921 args
[1] = make_number (BEG
);
7922 args
[2] = make_number (Z
);
7923 safe_call (3, args
);
7926 if (XBUFFER (coding
->src_object
) != current_buffer
)
7927 kill_src_buffer
= 1;
7928 coding
->src_object
= Fcurrent_buffer ();
7930 move_gap_both (BEG
, BEG_BYTE
);
7931 coding
->src_chars
= Z
- BEG
;
7932 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7933 coding
->src_pos
= BEG
;
7934 coding
->src_pos_byte
= BEG_BYTE
;
7935 coding
->src_multibyte
= Z
< Z_BYTE
;
7937 else if (STRINGP (src_object
))
7939 code_conversion_save (0, 0);
7940 coding
->src_pos
= from
;
7941 coding
->src_pos_byte
= from_byte
;
7943 else if (BUFFERP (src_object
))
7945 code_conversion_save (0, 0);
7946 set_buffer_internal (XBUFFER (src_object
));
7947 if (EQ (src_object
, dst_object
))
7949 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7950 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7951 coding
->src_pos
= 0;
7952 coding
->src_pos_byte
= 0;
7956 if (from
< GPT
&& to
>= GPT
)
7957 move_gap_both (from
, from_byte
);
7958 coding
->src_pos
= from
;
7959 coding
->src_pos_byte
= from_byte
;
7963 code_conversion_save (0, 0);
7965 if (BUFFERP (dst_object
))
7967 coding
->dst_object
= dst_object
;
7968 if (EQ (src_object
, dst_object
))
7970 coding
->dst_pos
= from
;
7971 coding
->dst_pos_byte
= from_byte
;
7975 struct buffer
*current
= current_buffer
;
7977 set_buffer_temp (XBUFFER (dst_object
));
7978 coding
->dst_pos
= PT
;
7979 coding
->dst_pos_byte
= PT_BYTE
;
7980 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7981 set_buffer_temp (current
);
7983 coding
->dst_multibyte
7984 = ! NILP (BVAR (XBUFFER (dst_object
), enable_multibyte_characters
));
7986 else if (EQ (dst_object
, Qt
))
7988 ptrdiff_t dst_bytes
= max (1, coding
->src_chars
);
7989 coding
->dst_object
= Qnil
;
7990 coding
->destination
= (unsigned char *) xmalloc (dst_bytes
);
7991 coding
->dst_bytes
= dst_bytes
;
7992 coding
->dst_multibyte
= 0;
7996 coding
->dst_object
= Qnil
;
7997 coding
->dst_multibyte
= 0;
8000 encode_coding (coding
);
8002 if (EQ (dst_object
, Qt
))
8004 if (BUFFERP (coding
->dst_object
))
8005 coding
->dst_object
= Fbuffer_string ();
8009 = make_unibyte_string ((char *) coding
->destination
,
8011 xfree (coding
->destination
);
8017 /* This is the case of:
8018 (BUFFERP (src_object) && EQ (src_object, dst_object))
8019 As we have moved PT while replacing the original buffer
8020 contents, we must recover it now. */
8021 set_buffer_internal (XBUFFER (src_object
));
8022 if (saved_pt
< from
)
8023 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
8024 else if (saved_pt
< from
+ chars
)
8025 TEMP_SET_PT_BOTH (from
, from_byte
);
8026 else if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8027 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
8028 saved_pt_byte
+ (coding
->produced
- bytes
));
8030 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
8031 saved_pt_byte
+ (coding
->produced
- bytes
));
8033 if (need_marker_adjustment
)
8035 struct Lisp_Marker
*tail
;
8037 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
8038 if (tail
->need_adjustment
)
8040 tail
->need_adjustment
= 0;
8041 if (tail
->insertion_type
)
8043 tail
->bytepos
= from_byte
;
8044 tail
->charpos
= from
;
8048 tail
->bytepos
= from_byte
+ coding
->produced
;
8050 = (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8051 ? tail
->bytepos
: from
+ coding
->produced_char
);
8057 if (kill_src_buffer
)
8058 Fkill_buffer (coding
->src_object
);
8060 Vdeactivate_mark
= old_deactivate_mark
;
8061 unbind_to (count
, Qnil
);
8066 preferred_coding_system (void)
8068 int id
= coding_categories
[coding_priorities
[0]].id
;
8070 return CODING_ID_NAME (id
);
8075 /*** 8. Emacs Lisp library functions ***/
8077 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
8078 doc
: /* Return t if OBJECT is nil or a coding-system.
8079 See the documentation of `define-coding-system' for information
8080 about coding-system objects. */)
8081 (Lisp_Object object
)
8084 || CODING_SYSTEM_ID (object
) >= 0)
8086 if (! SYMBOLP (object
)
8087 || NILP (Fget (object
, Qcoding_system_define_form
)))
8092 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
8093 Sread_non_nil_coding_system
, 1, 1, 0,
8094 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
8095 (Lisp_Object prompt
)
8100 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8101 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
8103 while (SCHARS (val
) == 0);
8104 return (Fintern (val
, Qnil
));
8107 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
8108 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
8109 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
8110 Ignores case when completing coding systems (all Emacs coding systems
8111 are lower-case). */)
8112 (Lisp_Object prompt
, Lisp_Object default_coding_system
)
8115 int count
= SPECPDL_INDEX ();
8117 if (SYMBOLP (default_coding_system
))
8118 default_coding_system
= SYMBOL_NAME (default_coding_system
);
8119 specbind (Qcompletion_ignore_case
, Qt
);
8120 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
8121 Qt
, Qnil
, Qcoding_system_history
,
8122 default_coding_system
, Qnil
);
8123 unbind_to (count
, Qnil
);
8124 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
8127 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
8129 doc
: /* Check validity of CODING-SYSTEM.
8130 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
8131 It is valid if it is nil or a symbol defined as a coding system by the
8132 function `define-coding-system'. */)
8133 (Lisp_Object coding_system
)
8135 Lisp_Object define_form
;
8137 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
8138 if (! NILP (define_form
))
8140 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
8141 safe_eval (define_form
);
8143 if (!NILP (Fcoding_system_p (coding_system
)))
8144 return coding_system
;
8145 xsignal1 (Qcoding_system_error
, coding_system
);
8149 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
8150 HIGHEST is nonzero, return the coding system of the highest
8151 priority among the detected coding systems. Otherwise return a
8152 list of detected coding systems sorted by their priorities. If
8153 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
8154 multibyte form but contains only ASCII and eight-bit chars.
8155 Otherwise, the bytes are raw bytes.
8157 CODING-SYSTEM controls the detection as below:
8159 If it is nil, detect both text-format and eol-format. If the
8160 text-format part of CODING-SYSTEM is already specified
8161 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
8162 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
8163 detect only text-format. */
8166 detect_coding_system (const unsigned char *src
,
8167 EMACS_INT src_chars
, EMACS_INT src_bytes
,
8168 int highest
, int multibytep
,
8169 Lisp_Object coding_system
)
8171 const unsigned char *src_end
= src
+ src_bytes
;
8172 Lisp_Object attrs
, eol_type
;
8173 Lisp_Object val
= Qnil
;
8174 struct coding_system coding
;
8176 struct coding_detection_info detect_info
;
8177 enum coding_category base_category
;
8178 int null_byte_found
= 0, eight_bit_found
= 0;
8180 if (NILP (coding_system
))
8181 coding_system
= Qundecided
;
8182 setup_coding_system (coding_system
, &coding
);
8183 attrs
= CODING_ID_ATTRS (coding
.id
);
8184 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
8185 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
8187 coding
.source
= src
;
8188 coding
.src_chars
= src_chars
;
8189 coding
.src_bytes
= src_bytes
;
8190 coding
.src_multibyte
= multibytep
;
8191 coding
.consumed
= 0;
8192 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8193 coding
.head_ascii
= 0;
8195 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
8197 /* At first, detect text-format if necessary. */
8198 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8199 if (base_category
== coding_category_undecided
)
8201 enum coding_category category
IF_LINT (= 0);
8202 struct coding_system
*this IF_LINT (= NULL
);
8205 /* Skip all ASCII bytes except for a few ISO2022 controls. */
8206 for (; src
< src_end
; src
++)
8211 eight_bit_found
= 1;
8212 if (null_byte_found
)
8217 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
8218 && ! inhibit_iso_escape_detection
8219 && ! detect_info
.checked
)
8221 if (detect_coding_iso_2022 (&coding
, &detect_info
))
8223 /* We have scanned the whole data. */
8224 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
8226 /* We didn't find an 8-bit code. We may
8227 have found a null-byte, but it's very
8228 rare that a binary file confirm to
8231 coding
.head_ascii
= src
- coding
.source
;
8233 detect_info
.rejected
|= ~CATEGORY_MASK_ISO_ESCAPE
;
8237 else if (! c
&& !inhibit_null_byte_detection
)
8239 null_byte_found
= 1;
8240 if (eight_bit_found
)
8243 if (! eight_bit_found
)
8244 coding
.head_ascii
++;
8246 else if (! eight_bit_found
)
8247 coding
.head_ascii
++;
8250 if (null_byte_found
|| eight_bit_found
8251 || coding
.head_ascii
< coding
.src_bytes
8252 || detect_info
.found
)
8254 if (coding
.head_ascii
== coding
.src_bytes
)
8255 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
8256 for (i
= 0; i
< coding_category_raw_text
; i
++)
8258 category
= coding_priorities
[i
];
8259 this = coding_categories
+ category
;
8260 if (detect_info
.found
& (1 << category
))
8265 if (null_byte_found
)
8267 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
8268 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
8270 for (i
= 0; i
< coding_category_raw_text
; i
++)
8272 category
= coding_priorities
[i
];
8273 this = coding_categories
+ category
;
8277 /* No coding system of this category is defined. */
8278 detect_info
.rejected
|= (1 << category
);
8280 else if (category
>= coding_category_raw_text
)
8282 else if (detect_info
.checked
& (1 << category
))
8285 && (detect_info
.found
& (1 << category
)))
8288 else if ((*(this->detector
)) (&coding
, &detect_info
)
8290 && (detect_info
.found
& (1 << category
)))
8292 if (category
== coding_category_utf_16_auto
)
8294 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8295 category
= coding_category_utf_16_le
;
8297 category
= coding_category_utf_16_be
;
8305 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
8308 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
8309 id
= CODING_SYSTEM_ID (Qno_conversion
);
8310 val
= Fcons (make_number (id
), Qnil
);
8312 else if (! detect_info
.rejected
&& ! detect_info
.found
)
8314 detect_info
.found
= CATEGORY_MASK_ANY
;
8315 id
= coding_categories
[coding_category_undecided
].id
;
8316 val
= Fcons (make_number (id
), Qnil
);
8320 if (detect_info
.found
)
8322 detect_info
.found
= 1 << category
;
8323 val
= Fcons (make_number (this->id
), Qnil
);
8326 for (i
= 0; i
< coding_category_raw_text
; i
++)
8327 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
8329 detect_info
.found
= 1 << coding_priorities
[i
];
8330 id
= coding_categories
[coding_priorities
[i
]].id
;
8331 val
= Fcons (make_number (id
), Qnil
);
8337 int mask
= detect_info
.rejected
| detect_info
.found
;
8340 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8342 category
= coding_priorities
[i
];
8343 if (! (mask
& (1 << category
)))
8345 found
|= 1 << category
;
8346 id
= coding_categories
[category
].id
;
8348 val
= Fcons (make_number (id
), val
);
8351 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
8353 category
= coding_priorities
[i
];
8354 if (detect_info
.found
& (1 << category
))
8356 id
= coding_categories
[category
].id
;
8357 val
= Fcons (make_number (id
), val
);
8360 detect_info
.found
|= found
;
8363 else if (base_category
== coding_category_utf_8_auto
)
8365 if (detect_coding_utf_8 (&coding
, &detect_info
))
8367 struct coding_system
*this;
8369 if (detect_info
.found
& CATEGORY_MASK_UTF_8_SIG
)
8370 this = coding_categories
+ coding_category_utf_8_sig
;
8372 this = coding_categories
+ coding_category_utf_8_nosig
;
8373 val
= Fcons (make_number (this->id
), Qnil
);
8376 else if (base_category
== coding_category_utf_16_auto
)
8378 if (detect_coding_utf_16 (&coding
, &detect_info
))
8380 struct coding_system
*this;
8382 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
8383 this = coding_categories
+ coding_category_utf_16_le
;
8384 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
8385 this = coding_categories
+ coding_category_utf_16_be
;
8386 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
8387 this = coding_categories
+ coding_category_utf_16_be_nosig
;
8389 this = coding_categories
+ coding_category_utf_16_le_nosig
;
8390 val
= Fcons (make_number (this->id
), Qnil
);
8395 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
8396 val
= Fcons (make_number (coding
.id
), Qnil
);
8399 /* Then, detect eol-format if necessary. */
8401 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
= -1;
8404 if (VECTORP (eol_type
))
8406 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
8408 if (null_byte_found
)
8409 normal_eol
= EOL_SEEN_LF
;
8411 normal_eol
= detect_eol (coding
.source
, src_bytes
,
8412 coding_category_raw_text
);
8414 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
8415 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
8416 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
8417 coding_category_utf_16_be
);
8418 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
8419 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
8420 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
8421 coding_category_utf_16_le
);
8425 if (EQ (eol_type
, Qunix
))
8426 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
8427 else if (EQ (eol_type
, Qdos
))
8428 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
8430 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
8433 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
8435 enum coding_category category
;
8438 id
= XINT (XCAR (tail
));
8439 attrs
= CODING_ID_ATTRS (id
);
8440 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8441 eol_type
= CODING_ID_EOL_TYPE (id
);
8442 if (VECTORP (eol_type
))
8444 if (category
== coding_category_utf_16_be
8445 || category
== coding_category_utf_16_be_nosig
)
8446 this_eol
= utf_16_be_eol
;
8447 else if (category
== coding_category_utf_16_le
8448 || category
== coding_category_utf_16_le_nosig
)
8449 this_eol
= utf_16_le_eol
;
8451 this_eol
= normal_eol
;
8453 if (this_eol
== EOL_SEEN_LF
)
8454 XSETCAR (tail
, AREF (eol_type
, 0));
8455 else if (this_eol
== EOL_SEEN_CRLF
)
8456 XSETCAR (tail
, AREF (eol_type
, 1));
8457 else if (this_eol
== EOL_SEEN_CR
)
8458 XSETCAR (tail
, AREF (eol_type
, 2));
8460 XSETCAR (tail
, CODING_ID_NAME (id
));
8463 XSETCAR (tail
, CODING_ID_NAME (id
));
8467 return (highest
? (CONSP (val
) ? XCAR (val
) : Qnil
) : val
);
8471 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
8473 doc
: /* Detect coding system of the text in the region between START and END.
8474 Return a list of possible coding systems ordered by priority.
8475 The coding systems to try and their priorities follows what
8476 the function `coding-system-priority-list' (which see) returns.
8478 If only ASCII characters are found (except for such ISO-2022 control
8479 characters as ESC), it returns a list of single element `undecided'
8480 or its subsidiary coding system according to a detected end-of-line
8483 If optional argument HIGHEST is non-nil, return the coding system of
8484 highest priority. */)
8485 (Lisp_Object start
, Lisp_Object end
, Lisp_Object highest
)
8488 int from_byte
, to_byte
;
8490 CHECK_NUMBER_COERCE_MARKER (start
);
8491 CHECK_NUMBER_COERCE_MARKER (end
);
8493 validate_region (&start
, &end
);
8494 from
= XINT (start
), to
= XINT (end
);
8495 from_byte
= CHAR_TO_BYTE (from
);
8496 to_byte
= CHAR_TO_BYTE (to
);
8498 if (from
< GPT
&& to
>= GPT
)
8499 move_gap_both (to
, to_byte
);
8501 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
8502 to
- from
, to_byte
- from_byte
,
8504 !NILP (BVAR (current_buffer
8505 , enable_multibyte_characters
)),
8509 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
8511 doc
: /* Detect coding system of the text in STRING.
8512 Return a list of possible coding systems ordered by priority.
8513 The coding systems to try and their priorities follows what
8514 the function `coding-system-priority-list' (which see) returns.
8516 If only ASCII characters are found (except for such ISO-2022 control
8517 characters as ESC), it returns a list of single element `undecided'
8518 or its subsidiary coding system according to a detected end-of-line
8521 If optional argument HIGHEST is non-nil, return the coding system of
8522 highest priority. */)
8523 (Lisp_Object string
, Lisp_Object highest
)
8525 CHECK_STRING (string
);
8527 return detect_coding_system (SDATA (string
),
8528 SCHARS (string
), SBYTES (string
),
8529 !NILP (highest
), STRING_MULTIBYTE (string
),
8535 char_encodable_p (int c
, Lisp_Object attrs
)
8538 struct charset
*charset
;
8539 Lisp_Object translation_table
;
8541 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
8542 if (! NILP (translation_table
))
8543 c
= translate_char (translation_table
, c
);
8544 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
8545 CONSP (tail
); tail
= XCDR (tail
))
8547 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
8548 if (CHAR_CHARSET_P (c
, charset
))
8551 return (! NILP (tail
));
8555 /* Return a list of coding systems that safely encode the text between
8556 START and END. If EXCLUDE is non-nil, it is a list of coding
8557 systems not to check. The returned list doesn't contain any such
8558 coding systems. In any case, if the text contains only ASCII or is
8559 unibyte, return t. */
8561 DEFUN ("find-coding-systems-region-internal",
8562 Ffind_coding_systems_region_internal
,
8563 Sfind_coding_systems_region_internal
, 2, 3, 0,
8564 doc
: /* Internal use only. */)
8565 (Lisp_Object start
, Lisp_Object end
, Lisp_Object exclude
)
8567 Lisp_Object coding_attrs_list
, safe_codings
;
8568 EMACS_INT start_byte
, end_byte
;
8569 const unsigned char *p
, *pbeg
, *pend
;
8571 Lisp_Object tail
, elt
, work_table
;
8573 if (STRINGP (start
))
8575 if (!STRING_MULTIBYTE (start
)
8576 || SCHARS (start
) == SBYTES (start
))
8579 end_byte
= SBYTES (start
);
8583 CHECK_NUMBER_COERCE_MARKER (start
);
8584 CHECK_NUMBER_COERCE_MARKER (end
);
8585 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8586 args_out_of_range (start
, end
);
8587 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8589 start_byte
= CHAR_TO_BYTE (XINT (start
));
8590 end_byte
= CHAR_TO_BYTE (XINT (end
));
8591 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8594 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8596 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8597 move_gap_both (XINT (start
), start_byte
);
8599 move_gap_both (XINT (end
), end_byte
);
8603 coding_attrs_list
= Qnil
;
8604 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8606 || NILP (Fmemq (XCAR (tail
), exclude
)))
8610 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
8611 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
8612 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
8614 ASET (attrs
, coding_attr_trans_tbl
,
8615 get_translation_table (attrs
, 1, NULL
));
8616 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
8620 if (STRINGP (start
))
8621 p
= pbeg
= SDATA (start
);
8623 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8624 pend
= p
+ (end_byte
- start_byte
);
8626 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
8627 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8629 work_table
= Fmake_char_table (Qnil
, Qnil
);
8632 if (ASCII_BYTE_P (*p
))
8636 c
= STRING_CHAR_ADVANCE (p
);
8637 if (!NILP (char_table_ref (work_table
, c
)))
8638 /* This character was already checked. Ignore it. */
8641 charset_map_loaded
= 0;
8642 for (tail
= coding_attrs_list
; CONSP (tail
);)
8647 else if (char_encodable_p (c
, elt
))
8649 else if (CONSP (XCDR (tail
)))
8651 XSETCAR (tail
, XCAR (XCDR (tail
)));
8652 XSETCDR (tail
, XCDR (XCDR (tail
)));
8656 XSETCAR (tail
, Qnil
);
8660 if (charset_map_loaded
)
8662 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8664 if (STRINGP (start
))
8665 pbeg
= SDATA (start
);
8667 pbeg
= BYTE_POS_ADDR (start_byte
);
8668 p
= pbeg
+ p_offset
;
8669 pend
= pbeg
+ pend_offset
;
8671 char_table_set (work_table
, c
, Qt
);
8675 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8676 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8677 if (! NILP (XCAR (tail
)))
8678 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8680 return safe_codings
;
8684 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8685 Sunencodable_char_position
, 3, 5, 0,
8687 Return position of first un-encodable character in a region.
8688 START and END specify the region and CODING-SYSTEM specifies the
8689 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8691 If optional 4th argument COUNT is non-nil, it specifies at most how
8692 many un-encodable characters to search. In this case, the value is a
8695 If optional 5th argument STRING is non-nil, it is a string to search
8696 for un-encodable characters. In that case, START and END are indexes
8698 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object count
, Lisp_Object string
)
8701 struct coding_system coding
;
8702 Lisp_Object attrs
, charset_list
, translation_table
;
8703 Lisp_Object positions
;
8705 const unsigned char *p
, *stop
, *pend
;
8706 int ascii_compatible
;
8708 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8709 attrs
= CODING_ID_ATTRS (coding
.id
);
8710 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8712 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8713 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8714 translation_table
= get_translation_table (attrs
, 1, NULL
);
8718 validate_region (&start
, &end
);
8719 from
= XINT (start
);
8721 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
))
8722 || (ascii_compatible
8723 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8725 p
= CHAR_POS_ADDR (from
);
8726 pend
= CHAR_POS_ADDR (to
);
8727 if (from
< GPT
&& to
>= GPT
)
8734 CHECK_STRING (string
);
8735 CHECK_NATNUM (start
);
8737 from
= XINT (start
);
8740 || to
> SCHARS (string
))
8741 args_out_of_range_3 (string
, start
, end
);
8742 if (! STRING_MULTIBYTE (string
))
8744 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8745 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8746 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8754 CHECK_NATNUM (count
);
8763 if (ascii_compatible
)
8764 while (p
< stop
&& ASCII_BYTE_P (*p
))
8774 c
= STRING_CHAR_ADVANCE (p
);
8775 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8776 && ! char_charset (translate_char (translation_table
, c
),
8777 charset_list
, NULL
))
8779 positions
= Fcons (make_number (from
), positions
);
8788 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8792 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8793 Scheck_coding_systems_region
, 3, 3, 0,
8794 doc
: /* Check if the region is encodable by coding systems.
8796 START and END are buffer positions specifying the region.
8797 CODING-SYSTEM-LIST is a list of coding systems to check.
8799 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8800 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8801 whole region, POS0, POS1, ... are buffer positions where non-encodable
8802 characters are found.
8804 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8807 START may be a string. In that case, check if the string is
8808 encodable, and the value contains indices to the string instead of
8809 buffer positions. END is ignored.
8811 If the current buffer (or START if it is a string) is unibyte, the value
8813 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system_list
)
8816 EMACS_INT start_byte
, end_byte
;
8818 const unsigned char *p
, *pbeg
, *pend
;
8820 Lisp_Object tail
, elt
, attrs
;
8822 if (STRINGP (start
))
8824 if (!STRING_MULTIBYTE (start
)
8825 || SCHARS (start
) == SBYTES (start
))
8828 end_byte
= SBYTES (start
);
8833 CHECK_NUMBER_COERCE_MARKER (start
);
8834 CHECK_NUMBER_COERCE_MARKER (end
);
8835 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8836 args_out_of_range (start
, end
);
8837 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
8839 start_byte
= CHAR_TO_BYTE (XINT (start
));
8840 end_byte
= CHAR_TO_BYTE (XINT (end
));
8841 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8844 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8846 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8847 move_gap_both (XINT (start
), start_byte
);
8849 move_gap_both (XINT (end
), end_byte
);
8855 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8858 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8859 ASET (attrs
, coding_attr_trans_tbl
,
8860 get_translation_table (attrs
, 1, NULL
));
8861 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8864 if (STRINGP (start
))
8865 p
= pbeg
= SDATA (start
);
8867 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8868 pend
= p
+ (end_byte
- start_byte
);
8870 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8871 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8875 if (ASCII_BYTE_P (*p
))
8879 c
= STRING_CHAR_ADVANCE (p
);
8881 charset_map_loaded
= 0;
8882 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8884 elt
= XCDR (XCAR (tail
));
8885 if (! char_encodable_p (c
, XCAR (elt
)))
8886 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8888 if (charset_map_loaded
)
8890 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8892 if (STRINGP (start
))
8893 pbeg
= SDATA (start
);
8895 pbeg
= BYTE_POS_ADDR (start_byte
);
8896 p
= pbeg
+ p_offset
;
8897 pend
= pbeg
+ pend_offset
;
8905 for (; CONSP (tail
); tail
= XCDR (tail
))
8908 if (CONSP (XCDR (XCDR (elt
))))
8909 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8918 code_convert_region (Lisp_Object start
, Lisp_Object end
,
8919 Lisp_Object coding_system
, Lisp_Object dst_object
,
8920 int encodep
, int norecord
)
8922 struct coding_system coding
;
8923 EMACS_INT from
, from_byte
, to
, to_byte
;
8924 Lisp_Object src_object
;
8926 CHECK_NUMBER_COERCE_MARKER (start
);
8927 CHECK_NUMBER_COERCE_MARKER (end
);
8928 if (NILP (coding_system
))
8929 coding_system
= Qno_conversion
;
8931 CHECK_CODING_SYSTEM (coding_system
);
8932 src_object
= Fcurrent_buffer ();
8933 if (NILP (dst_object
))
8934 dst_object
= src_object
;
8935 else if (! EQ (dst_object
, Qt
))
8936 CHECK_BUFFER (dst_object
);
8938 validate_region (&start
, &end
);
8939 from
= XFASTINT (start
);
8940 from_byte
= CHAR_TO_BYTE (from
);
8941 to
= XFASTINT (end
);
8942 to_byte
= CHAR_TO_BYTE (to
);
8944 setup_coding_system (coding_system
, &coding
);
8945 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8948 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8951 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8954 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8956 return (BUFFERP (dst_object
)
8957 ? make_number (coding
.produced_char
)
8958 : coding
.dst_object
);
8962 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8963 3, 4, "r\nzCoding system: ",
8964 doc
: /* Decode the current region from the specified coding system.
8965 When called from a program, takes four arguments:
8966 START, END, CODING-SYSTEM, and DESTINATION.
8967 START and END are buffer positions.
8969 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8970 If nil, the region between START and END is replaced by the decoded text.
8971 If buffer, the decoded text is inserted in that buffer after point (point
8973 In those cases, the length of the decoded text is returned.
8974 If DESTINATION is t, the decoded text is returned.
8976 This function sets `last-coding-system-used' to the precise coding system
8977 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8978 not fully specified.) */)
8979 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
8981 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8984 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8985 3, 4, "r\nzCoding system: ",
8986 doc
: /* Encode the current region by specified coding system.
8987 When called from a program, takes four arguments:
8988 START, END, CODING-SYSTEM and DESTINATION.
8989 START and END are buffer positions.
8991 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8992 If nil, the region between START and END is replace by the encoded text.
8993 If buffer, the encoded text is inserted in that buffer after point (point
8995 In those cases, the length of the encoded text is returned.
8996 If DESTINATION is t, the encoded text is returned.
8998 This function sets `last-coding-system-used' to the precise coding system
8999 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9000 not fully specified.) */)
9001 (Lisp_Object start
, Lisp_Object end
, Lisp_Object coding_system
, Lisp_Object destination
)
9003 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
9007 code_convert_string (Lisp_Object string
, Lisp_Object coding_system
,
9008 Lisp_Object dst_object
, int encodep
, int nocopy
, int norecord
)
9010 struct coding_system coding
;
9011 EMACS_INT chars
, bytes
;
9013 CHECK_STRING (string
);
9014 if (NILP (coding_system
))
9017 Vlast_coding_system_used
= Qno_conversion
;
9018 if (NILP (dst_object
))
9019 return (nocopy
? Fcopy_sequence (string
) : string
);
9022 if (NILP (coding_system
))
9023 coding_system
= Qno_conversion
;
9025 CHECK_CODING_SYSTEM (coding_system
);
9026 if (NILP (dst_object
))
9028 else if (! EQ (dst_object
, Qt
))
9029 CHECK_BUFFER (dst_object
);
9031 setup_coding_system (coding_system
, &coding
);
9032 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
9033 chars
= SCHARS (string
);
9034 bytes
= SBYTES (string
);
9036 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9038 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
9040 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
9042 return (BUFFERP (dst_object
)
9043 ? make_number (coding
.produced_char
)
9044 : coding
.dst_object
);
9048 /* Encode or decode STRING according to CODING_SYSTEM.
9049 Do not set Vlast_coding_system_used.
9051 This function is called only from macros DECODE_FILE and
9052 ENCODE_FILE, thus we ignore character composition. */
9055 code_convert_string_norecord (Lisp_Object string
, Lisp_Object coding_system
,
9058 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
9062 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
9064 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
9066 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
9067 if the decoding operation is trivial.
9069 Optional fourth arg BUFFER non-nil means that the decoded text is
9070 inserted in that buffer after point (point does not move). In this
9071 case, the return value is the length of the decoded text.
9073 This function sets `last-coding-system-used' to the precise coding system
9074 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9075 not fully specified.) */)
9076 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9078 return code_convert_string (string
, coding_system
, buffer
,
9079 0, ! NILP (nocopy
), 0);
9082 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
9084 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
9086 Optional third arg NOCOPY non-nil means it is OK to return STRING
9087 itself if the encoding operation is trivial.
9089 Optional fourth arg BUFFER non-nil means that the encoded text is
9090 inserted in that buffer after point (point does not move). In this
9091 case, the return value is the length of the encoded text.
9093 This function sets `last-coding-system-used' to the precise coding system
9094 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
9095 not fully specified.) */)
9096 (Lisp_Object string
, Lisp_Object coding_system
, Lisp_Object nocopy
, Lisp_Object buffer
)
9098 return code_convert_string (string
, coding_system
, buffer
,
9099 1, ! NILP (nocopy
), 0);
9103 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
9104 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
9105 Return the corresponding character. */)
9108 Lisp_Object spec
, attrs
, val
;
9109 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
9113 CHECK_NATNUM (code
);
9114 ch
= XFASTINT (code
);
9115 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9116 attrs
= AREF (spec
, 0);
9118 if (ASCII_BYTE_P (ch
)
9119 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9122 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9123 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9124 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9125 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9130 charset
= charset_roman
;
9132 else if (ch
>= 0xA0 && ch
< 0xDF)
9135 charset
= charset_kana
;
9139 EMACS_INT c1
= ch
>> 8;
9142 if (c1
< 0x81 || (c1
> 0x9F && c1
< 0xE0) || c1
> 0xEF
9143 || c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
9144 error ("Invalid code: %"pI
"d", ch
);
9147 charset
= charset_kanji
;
9149 c
= DECODE_CHAR (charset
, c
);
9151 error ("Invalid code: %"pI
"d", ch
);
9152 return make_number (c
);
9156 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
9157 doc
: /* Encode a Japanese character CH to shift_jis encoding.
9158 Return the corresponding code in SJIS. */)
9161 Lisp_Object spec
, attrs
, charset_list
;
9163 struct charset
*charset
;
9166 CHECK_CHARACTER (ch
);
9168 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
9169 attrs
= AREF (spec
, 0);
9171 if (ASCII_CHAR_P (c
)
9172 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9175 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9176 charset
= char_charset (c
, charset_list
, &code
);
9177 if (code
== CHARSET_INVALID_CODE (charset
))
9178 error ("Can't encode by shift_jis encoding: %c", c
);
9181 return make_number (code
);
9184 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
9185 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
9186 Return the corresponding character. */)
9189 Lisp_Object spec
, attrs
, val
;
9190 struct charset
*charset_roman
, *charset_big5
, *charset
;
9194 CHECK_NATNUM (code
);
9195 ch
= XFASTINT (code
);
9196 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9197 attrs
= AREF (spec
, 0);
9199 if (ASCII_BYTE_P (ch
)
9200 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9203 val
= CODING_ATTR_CHARSET_LIST (attrs
);
9204 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
9205 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
9210 charset
= charset_roman
;
9214 EMACS_INT b1
= ch
>> 8;
9216 if (b1
< 0xA1 || b1
> 0xFE
9217 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
9218 error ("Invalid code: %"pI
"d", ch
);
9220 charset
= charset_big5
;
9222 c
= DECODE_CHAR (charset
, c
);
9224 error ("Invalid code: %"pI
"d", ch
);
9225 return make_number (c
);
9228 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
9229 doc
: /* Encode the Big5 character CH to BIG5 coding system.
9230 Return the corresponding character code in Big5. */)
9233 Lisp_Object spec
, attrs
, charset_list
;
9234 struct charset
*charset
;
9238 CHECK_CHARACTER (ch
);
9240 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
9241 attrs
= AREF (spec
, 0);
9242 if (ASCII_CHAR_P (c
)
9243 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
9246 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
9247 charset
= char_charset (c
, charset_list
, &code
);
9248 if (code
== CHARSET_INVALID_CODE (charset
))
9249 error ("Can't encode by Big5 encoding: %c", c
);
9251 return make_number (code
);
9255 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
9256 Sset_terminal_coding_system_internal
, 1, 2, 0,
9257 doc
: /* Internal use only. */)
9258 (Lisp_Object coding_system
, Lisp_Object terminal
)
9260 struct terminal
*term
= get_terminal (terminal
, 1);
9261 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (term
);
9262 CHECK_SYMBOL (coding_system
);
9263 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
9264 /* We had better not send unsafe characters to terminal. */
9265 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
9266 /* Character composition should be disabled. */
9267 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9268 terminal_coding
->src_multibyte
= 1;
9269 terminal_coding
->dst_multibyte
= 0;
9270 if (terminal_coding
->common_flags
& CODING_REQUIRE_ENCODING_MASK
)
9271 term
->charset_list
= coding_charset_list (terminal_coding
);
9273 term
->charset_list
= Fcons (make_number (charset_ascii
), Qnil
);
9277 DEFUN ("set-safe-terminal-coding-system-internal",
9278 Fset_safe_terminal_coding_system_internal
,
9279 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
9280 doc
: /* Internal use only. */)
9281 (Lisp_Object coding_system
)
9283 CHECK_SYMBOL (coding_system
);
9284 setup_coding_system (Fcheck_coding_system (coding_system
),
9285 &safe_terminal_coding
);
9286 /* Character composition should be disabled. */
9287 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9288 safe_terminal_coding
.src_multibyte
= 1;
9289 safe_terminal_coding
.dst_multibyte
= 0;
9293 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
9294 Sterminal_coding_system
, 0, 1, 0,
9295 doc
: /* Return coding system specified for terminal output on the given terminal.
9296 TERMINAL may be a terminal object, a frame, or nil for the selected
9297 frame's terminal device. */)
9298 (Lisp_Object terminal
)
9300 struct coding_system
*terminal_coding
9301 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
9302 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
9304 /* For backward compatibility, return nil if it is `undecided'. */
9305 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
9308 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
9309 Sset_keyboard_coding_system_internal
, 1, 2, 0,
9310 doc
: /* Internal use only. */)
9311 (Lisp_Object coding_system
, Lisp_Object terminal
)
9313 struct terminal
*t
= get_terminal (terminal
, 1);
9314 CHECK_SYMBOL (coding_system
);
9315 if (NILP (coding_system
))
9316 coding_system
= Qno_conversion
;
9318 Fcheck_coding_system (coding_system
);
9319 setup_coding_system (coding_system
, TERMINAL_KEYBOARD_CODING (t
));
9320 /* Character composition should be disabled. */
9321 TERMINAL_KEYBOARD_CODING (t
)->common_flags
9322 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
9326 DEFUN ("keyboard-coding-system",
9327 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
9328 doc
: /* Return coding system specified for decoding keyboard input. */)
9329 (Lisp_Object terminal
)
9331 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
9332 (get_terminal (terminal
, 1))->id
);
9336 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
9337 Sfind_operation_coding_system
, 1, MANY
, 0,
9338 doc
: /* Choose a coding system for an operation based on the target name.
9339 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
9340 DECODING-SYSTEM is the coding system to use for decoding
9341 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
9342 for encoding (in case OPERATION does encoding).
9344 The first argument OPERATION specifies an I/O primitive:
9345 For file I/O, `insert-file-contents' or `write-region'.
9346 For process I/O, `call-process', `call-process-region', or `start-process'.
9347 For network I/O, `open-network-stream'.
9349 The remaining arguments should be the same arguments that were passed
9350 to the primitive. Depending on which primitive, one of those arguments
9351 is selected as the TARGET. For example, if OPERATION does file I/O,
9352 whichever argument specifies the file name is TARGET.
9354 TARGET has a meaning which depends on OPERATION:
9355 For file I/O, TARGET is a file name (except for the special case below).
9356 For process I/O, TARGET is a process name.
9357 For network I/O, TARGET is a service name or a port number.
9359 This function looks up what is specified for TARGET in
9360 `file-coding-system-alist', `process-coding-system-alist',
9361 or `network-coding-system-alist' depending on OPERATION.
9362 They may specify a coding system, a cons of coding systems,
9363 or a function symbol to call.
9364 In the last case, we call the function with one argument,
9365 which is a list of all the arguments given to this function.
9366 If the function can't decide a coding system, it can return
9367 `undecided' so that the normal code-detection is performed.
9369 If OPERATION is `insert-file-contents', the argument corresponding to
9370 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
9371 file name to look up, and BUFFER is a buffer that contains the file's
9372 contents (not yet decoded). If `file-coding-system-alist' specifies a
9373 function to call for FILENAME, that function should examine the
9374 contents of BUFFER instead of reading the file.
9376 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
9377 (ptrdiff_t nargs
, Lisp_Object
*args
)
9379 Lisp_Object operation
, target_idx
, target
, val
;
9380 register Lisp_Object chain
;
9383 error ("Too few arguments");
9384 operation
= args
[0];
9385 if (!SYMBOLP (operation
)
9386 || !NATNUMP (target_idx
= Fget (operation
, Qtarget_idx
)))
9387 error ("Invalid first argument");
9388 if (nargs
< 1 + XFASTINT (target_idx
))
9389 error ("Too few arguments for operation `%s'",
9390 SDATA (SYMBOL_NAME (operation
)));
9391 target
= args
[XFASTINT (target_idx
) + 1];
9392 if (!(STRINGP (target
)
9393 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
9394 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
9395 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
9396 error ("Invalid argument %"pI
"d of operation `%s'",
9397 XFASTINT (target_idx
) + 1, SDATA (SYMBOL_NAME (operation
)));
9399 target
= XCAR (target
);
9401 chain
= ((EQ (operation
, Qinsert_file_contents
)
9402 || EQ (operation
, Qwrite_region
))
9403 ? Vfile_coding_system_alist
9404 : (EQ (operation
, Qopen_network_stream
)
9405 ? Vnetwork_coding_system_alist
9406 : Vprocess_coding_system_alist
));
9410 for (; CONSP (chain
); chain
= XCDR (chain
))
9416 && ((STRINGP (target
)
9417 && STRINGP (XCAR (elt
))
9418 && fast_string_match (XCAR (elt
), target
) >= 0)
9419 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
9422 /* Here, if VAL is both a valid coding system and a valid
9423 function symbol, we return VAL as a coding system. */
9426 if (! SYMBOLP (val
))
9428 if (! NILP (Fcoding_system_p (val
)))
9429 return Fcons (val
, val
);
9430 if (! NILP (Ffboundp (val
)))
9432 /* We use call1 rather than safe_call1
9433 so as to get bug reports about functions called here
9434 which don't handle the current interface. */
9435 val
= call1 (val
, Flist (nargs
, args
));
9438 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
9439 return Fcons (val
, val
);
9447 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
9448 Sset_coding_system_priority
, 0, MANY
, 0,
9449 doc
: /* Assign higher priority to the coding systems given as arguments.
9450 If multiple coding systems belong to the same category,
9451 all but the first one are ignored.
9453 usage: (set-coding-system-priority &rest coding-systems) */)
9454 (ptrdiff_t nargs
, Lisp_Object
*args
)
9457 int changed
[coding_category_max
];
9458 enum coding_category priorities
[coding_category_max
];
9460 memset (changed
, 0, sizeof changed
);
9462 for (i
= j
= 0; i
< nargs
; i
++)
9464 enum coding_category category
;
9465 Lisp_Object spec
, attrs
;
9467 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
9468 attrs
= AREF (spec
, 0);
9469 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
9470 if (changed
[category
])
9471 /* Ignore this coding system because a coding system of the
9472 same category already had a higher priority. */
9474 changed
[category
] = 1;
9475 priorities
[j
++] = category
;
9476 if (coding_categories
[category
].id
>= 0
9477 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
9478 setup_coding_system (args
[i
], &coding_categories
[category
]);
9479 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
9482 /* Now we have decided top J priorities. Reflect the order of the
9483 original priorities to the remaining priorities. */
9485 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
9487 while (j
< coding_category_max
9488 && changed
[coding_priorities
[j
]])
9490 if (j
== coding_category_max
)
9492 priorities
[i
] = coding_priorities
[j
];
9495 memcpy (coding_priorities
, priorities
, sizeof priorities
);
9497 /* Update `coding-category-list'. */
9498 Vcoding_category_list
= Qnil
;
9499 for (i
= coding_category_max
; i
-- > 0; )
9500 Vcoding_category_list
9501 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
9502 Vcoding_category_list
);
9507 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
9508 Scoding_system_priority_list
, 0, 1, 0,
9509 doc
: /* Return a list of coding systems ordered by their priorities.
9510 The list contains a subset of coding systems; i.e. coding systems
9511 assigned to each coding category (see `coding-category-list').
9513 HIGHESTP non-nil means just return the highest priority one. */)
9514 (Lisp_Object highestp
)
9519 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
9521 enum coding_category category
= coding_priorities
[i
];
9522 int id
= coding_categories
[category
].id
;
9527 attrs
= CODING_ID_ATTRS (id
);
9528 if (! NILP (highestp
))
9529 return CODING_ATTR_BASE_NAME (attrs
);
9530 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
9532 return Fnreverse (val
);
9535 static const char *const suffixes
[] = { "-unix", "-dos", "-mac" };
9538 make_subsidiaries (Lisp_Object base
)
9540 Lisp_Object subsidiaries
;
9541 ptrdiff_t base_name_len
= SBYTES (SYMBOL_NAME (base
));
9542 char *buf
= (char *) alloca (base_name_len
+ 6);
9545 memcpy (buf
, SDATA (SYMBOL_NAME (base
)), base_name_len
);
9546 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
9547 for (i
= 0; i
< 3; i
++)
9549 strcpy (buf
+ base_name_len
, suffixes
[i
]);
9550 ASET (subsidiaries
, i
, intern (buf
));
9552 return subsidiaries
;
9556 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
9557 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
9558 doc
: /* For internal use only.
9559 usage: (define-coding-system-internal ...) */)
9560 (ptrdiff_t nargs
, Lisp_Object
*args
)
9563 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
9564 Lisp_Object attrs
; /* Vector of attributes. */
9565 Lisp_Object eol_type
;
9566 Lisp_Object aliases
;
9567 Lisp_Object coding_type
, charset_list
, safe_charsets
;
9568 enum coding_category category
;
9569 Lisp_Object tail
, val
;
9570 int max_charset_id
= 0;
9573 if (nargs
< coding_arg_max
)
9576 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
9578 name
= args
[coding_arg_name
];
9579 CHECK_SYMBOL (name
);
9580 CODING_ATTR_BASE_NAME (attrs
) = name
;
9582 val
= args
[coding_arg_mnemonic
];
9583 if (! STRINGP (val
))
9584 CHECK_CHARACTER (val
);
9585 CODING_ATTR_MNEMONIC (attrs
) = val
;
9587 coding_type
= args
[coding_arg_coding_type
];
9588 CHECK_SYMBOL (coding_type
);
9589 CODING_ATTR_TYPE (attrs
) = coding_type
;
9591 charset_list
= args
[coding_arg_charset_list
];
9592 if (SYMBOLP (charset_list
))
9594 if (EQ (charset_list
, Qiso_2022
))
9596 if (! EQ (coding_type
, Qiso_2022
))
9597 error ("Invalid charset-list");
9598 charset_list
= Viso_2022_charset_list
;
9600 else if (EQ (charset_list
, Qemacs_mule
))
9602 if (! EQ (coding_type
, Qemacs_mule
))
9603 error ("Invalid charset-list");
9604 charset_list
= Vemacs_mule_charset_list
;
9606 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9607 if (max_charset_id
< XFASTINT (XCAR (tail
)))
9608 max_charset_id
= XFASTINT (XCAR (tail
));
9612 charset_list
= Fcopy_sequence (charset_list
);
9613 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9615 struct charset
*charset
;
9618 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9619 if (EQ (coding_type
, Qiso_2022
)
9620 ? CHARSET_ISO_FINAL (charset
) < 0
9621 : EQ (coding_type
, Qemacs_mule
)
9622 ? CHARSET_EMACS_MULE_ID (charset
) < 0
9624 error ("Can't handle charset `%s'",
9625 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9627 XSETCAR (tail
, make_number (charset
->id
));
9628 if (max_charset_id
< charset
->id
)
9629 max_charset_id
= charset
->id
;
9632 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
9634 safe_charsets
= make_uninit_string (max_charset_id
+ 1);
9635 memset (SDATA (safe_charsets
), 255, max_charset_id
+ 1);
9636 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9637 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
9638 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
9640 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
9642 val
= args
[coding_arg_decode_translation_table
];
9643 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9645 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9647 val
= args
[coding_arg_encode_translation_table
];
9648 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9650 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9652 val
= args
[coding_arg_post_read_conversion
];
9654 CODING_ATTR_POST_READ (attrs
) = val
;
9656 val
= args
[coding_arg_pre_write_conversion
];
9658 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9660 val
= args
[coding_arg_default_char
];
9662 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9665 CHECK_CHARACTER (val
);
9666 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9669 val
= args
[coding_arg_for_unibyte
];
9670 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9672 val
= args
[coding_arg_plist
];
9674 CODING_ATTR_PLIST (attrs
) = val
;
9676 if (EQ (coding_type
, Qcharset
))
9678 /* Generate a lisp vector of 256 elements. Each element is nil,
9679 integer, or a list of charset IDs.
9681 If Nth element is nil, the byte code N is invalid in this
9684 If Nth element is a number NUM, N is the first byte of a
9685 charset whose ID is NUM.
9687 If Nth element is a list of charset IDs, N is the first byte
9688 of one of them. The list is sorted by dimensions of the
9689 charsets. A charset of smaller dimension comes first. */
9690 val
= Fmake_vector (make_number (256), Qnil
);
9692 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9694 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9695 int dim
= CHARSET_DIMENSION (charset
);
9696 int idx
= (dim
- 1) * 4;
9698 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9699 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9701 for (i
= charset
->code_space
[idx
];
9702 i
<= charset
->code_space
[idx
+ 1]; i
++)
9704 Lisp_Object tmp
, tmp2
;
9707 tmp
= AREF (val
, i
);
9710 else if (NUMBERP (tmp
))
9712 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9714 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9716 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9720 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9722 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9727 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9730 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9731 XSETCAR (tmp2
, XCAR (tail
));
9737 ASET (attrs
, coding_attr_charset_valids
, val
);
9738 category
= coding_category_charset
;
9740 else if (EQ (coding_type
, Qccl
))
9744 if (nargs
< coding_arg_ccl_max
)
9747 val
= args
[coding_arg_ccl_decoder
];
9748 CHECK_CCL_PROGRAM (val
);
9750 val
= Fcopy_sequence (val
);
9751 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9753 val
= args
[coding_arg_ccl_encoder
];
9754 CHECK_CCL_PROGRAM (val
);
9756 val
= Fcopy_sequence (val
);
9757 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9759 val
= args
[coding_arg_ccl_valids
];
9760 valids
= Fmake_string (make_number (256), make_number (0));
9761 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9768 from
= to
= XINT (val
);
9769 if (from
< 0 || from
> 255)
9770 args_out_of_range_3 (val
, make_number (0), make_number (255));
9775 CHECK_NATNUM_CAR (val
);
9776 CHECK_NATNUM_CDR (val
);
9777 from
= XINT (XCAR (val
));
9779 args_out_of_range_3 (XCAR (val
),
9780 make_number (0), make_number (255));
9781 to
= XINT (XCDR (val
));
9782 if (to
< from
|| to
> 255)
9783 args_out_of_range_3 (XCDR (val
),
9784 XCAR (val
), make_number (255));
9786 for (i
= from
; i
<= to
; i
++)
9787 SSET (valids
, i
, 1);
9789 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9791 category
= coding_category_ccl
;
9793 else if (EQ (coding_type
, Qutf_16
))
9795 Lisp_Object bom
, endian
;
9797 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9799 if (nargs
< coding_arg_utf16_max
)
9802 bom
= args
[coding_arg_utf16_bom
];
9803 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9807 CHECK_CODING_SYSTEM (val
);
9809 CHECK_CODING_SYSTEM (val
);
9811 ASET (attrs
, coding_attr_utf_bom
, bom
);
9813 endian
= args
[coding_arg_utf16_endian
];
9814 CHECK_SYMBOL (endian
);
9817 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9818 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9819 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9821 category
= (CONSP (bom
)
9822 ? coding_category_utf_16_auto
9824 ? (EQ (endian
, Qbig
)
9825 ? coding_category_utf_16_be_nosig
9826 : coding_category_utf_16_le_nosig
)
9827 : (EQ (endian
, Qbig
)
9828 ? coding_category_utf_16_be
9829 : coding_category_utf_16_le
));
9831 else if (EQ (coding_type
, Qiso_2022
))
9833 Lisp_Object initial
, reg_usage
, request
, flags
;
9835 if (nargs
< coding_arg_iso2022_max
)
9838 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9839 CHECK_VECTOR (initial
);
9840 for (i
= 0; i
< 4; i
++)
9842 val
= Faref (initial
, make_number (i
));
9845 struct charset
*charset
;
9847 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9848 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9849 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9850 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9853 ASET (initial
, i
, make_number (-1));
9856 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9857 CHECK_CONS (reg_usage
);
9858 CHECK_NUMBER_CAR (reg_usage
);
9859 CHECK_NUMBER_CDR (reg_usage
);
9861 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9862 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9870 CHECK_CHARSET_GET_ID (tmp1
, id
);
9871 CHECK_NATNUM_CDR (val
);
9872 if (XINT (XCDR (val
)) >= 4)
9873 error ("Invalid graphic register number: %"pI
"d", XINT (XCDR (val
)));
9874 XSETCAR (val
, make_number (id
));
9877 flags
= args
[coding_arg_iso2022_flags
];
9878 CHECK_NATNUM (flags
);
9880 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9881 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9883 ASET (attrs
, coding_attr_iso_initial
, initial
);
9884 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9885 ASET (attrs
, coding_attr_iso_request
, request
);
9886 ASET (attrs
, coding_attr_iso_flags
, flags
);
9887 setup_iso_safe_charsets (attrs
);
9889 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9890 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9891 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9892 ? coding_category_iso_7_else
9893 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9894 ? coding_category_iso_7
9895 : coding_category_iso_7_tight
);
9898 int id
= XINT (AREF (initial
, 1));
9900 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9901 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9903 ? coding_category_iso_8_else
9904 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9905 ? coding_category_iso_8_1
9906 : coding_category_iso_8_2
);
9908 if (category
!= coding_category_iso_8_1
9909 && category
!= coding_category_iso_8_2
)
9910 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9912 else if (EQ (coding_type
, Qemacs_mule
))
9914 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9915 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9916 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9917 category
= coding_category_emacs_mule
;
9919 else if (EQ (coding_type
, Qshift_jis
))
9922 struct charset
*charset
;
9924 if (XINT (Flength (charset_list
)) != 3
9925 && XINT (Flength (charset_list
)) != 4)
9926 error ("There should be three or four charsets");
9928 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9929 if (CHARSET_DIMENSION (charset
) != 1)
9930 error ("Dimension of charset %s is not one",
9931 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9932 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9933 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9935 charset_list
= XCDR (charset_list
);
9936 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9937 if (CHARSET_DIMENSION (charset
) != 1)
9938 error ("Dimension of charset %s is not one",
9939 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9941 charset_list
= XCDR (charset_list
);
9942 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9943 if (CHARSET_DIMENSION (charset
) != 2)
9944 error ("Dimension of charset %s is not two",
9945 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9947 charset_list
= XCDR (charset_list
);
9948 if (! NILP (charset_list
))
9950 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9951 if (CHARSET_DIMENSION (charset
) != 2)
9952 error ("Dimension of charset %s is not two",
9953 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9956 category
= coding_category_sjis
;
9957 Vsjis_coding_system
= name
;
9959 else if (EQ (coding_type
, Qbig5
))
9961 struct charset
*charset
;
9963 if (XINT (Flength (charset_list
)) != 2)
9964 error ("There should be just two charsets");
9966 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9967 if (CHARSET_DIMENSION (charset
) != 1)
9968 error ("Dimension of charset %s is not one",
9969 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9970 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9971 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9973 charset_list
= XCDR (charset_list
);
9974 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9975 if (CHARSET_DIMENSION (charset
) != 2)
9976 error ("Dimension of charset %s is not two",
9977 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9979 category
= coding_category_big5
;
9980 Vbig5_coding_system
= name
;
9982 else if (EQ (coding_type
, Qraw_text
))
9984 category
= coding_category_raw_text
;
9985 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9987 else if (EQ (coding_type
, Qutf_8
))
9991 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9993 if (nargs
< coding_arg_utf8_max
)
9996 bom
= args
[coding_arg_utf8_bom
];
9997 if (! NILP (bom
) && ! EQ (bom
, Qt
))
10001 CHECK_CODING_SYSTEM (val
);
10003 CHECK_CODING_SYSTEM (val
);
10005 ASET (attrs
, coding_attr_utf_bom
, bom
);
10007 category
= (CONSP (bom
) ? coding_category_utf_8_auto
10008 : NILP (bom
) ? coding_category_utf_8_nosig
10009 : coding_category_utf_8_sig
);
10011 else if (EQ (coding_type
, Qundecided
))
10012 category
= coding_category_undecided
;
10014 error ("Invalid coding system type: %s",
10015 SDATA (SYMBOL_NAME (coding_type
)));
10017 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
10018 CODING_ATTR_PLIST (attrs
)
10019 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
10020 CODING_ATTR_PLIST (attrs
)));
10021 CODING_ATTR_PLIST (attrs
)
10022 = Fcons (QCascii_compatible_p
,
10023 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
10024 CODING_ATTR_PLIST (attrs
)));
10026 eol_type
= args
[coding_arg_eol_type
];
10027 if (! NILP (eol_type
)
10028 && ! EQ (eol_type
, Qunix
)
10029 && ! EQ (eol_type
, Qdos
)
10030 && ! EQ (eol_type
, Qmac
))
10031 error ("Invalid eol-type");
10033 aliases
= Fcons (name
, Qnil
);
10035 if (NILP (eol_type
))
10037 eol_type
= make_subsidiaries (name
);
10038 for (i
= 0; i
< 3; i
++)
10040 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
10042 this_name
= AREF (eol_type
, i
);
10043 this_aliases
= Fcons (this_name
, Qnil
);
10044 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
10045 this_spec
= Fmake_vector (make_number (3), attrs
);
10046 ASET (this_spec
, 1, this_aliases
);
10047 ASET (this_spec
, 2, this_eol_type
);
10048 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
10049 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
10050 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
10052 Vcoding_system_alist
10053 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
10054 Vcoding_system_alist
);
10058 spec_vec
= Fmake_vector (make_number (3), attrs
);
10059 ASET (spec_vec
, 1, aliases
);
10060 ASET (spec_vec
, 2, eol_type
);
10062 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
10063 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
10064 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
10066 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
10067 Vcoding_system_alist
);
10070 int id
= coding_categories
[category
].id
;
10072 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
10073 setup_coding_system (name
, &coding_categories
[category
]);
10079 return Fsignal (Qwrong_number_of_arguments
,
10080 Fcons (intern ("define-coding-system-internal"),
10081 make_number (nargs
)));
10085 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
10087 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
10088 (Lisp_Object coding_system
, Lisp_Object prop
, Lisp_Object val
)
10090 Lisp_Object spec
, attrs
;
10092 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10093 attrs
= AREF (spec
, 0);
10094 if (EQ (prop
, QCmnemonic
))
10096 if (! STRINGP (val
))
10097 CHECK_CHARACTER (val
);
10098 CODING_ATTR_MNEMONIC (attrs
) = val
;
10100 else if (EQ (prop
, QCdefault_char
))
10103 val
= make_number (' ');
10105 CHECK_CHARACTER (val
);
10106 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
10108 else if (EQ (prop
, QCdecode_translation_table
))
10110 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10111 CHECK_SYMBOL (val
);
10112 CODING_ATTR_DECODE_TBL (attrs
) = val
;
10114 else if (EQ (prop
, QCencode_translation_table
))
10116 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
10117 CHECK_SYMBOL (val
);
10118 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
10120 else if (EQ (prop
, QCpost_read_conversion
))
10122 CHECK_SYMBOL (val
);
10123 CODING_ATTR_POST_READ (attrs
) = val
;
10125 else if (EQ (prop
, QCpre_write_conversion
))
10127 CHECK_SYMBOL (val
);
10128 CODING_ATTR_PRE_WRITE (attrs
) = val
;
10130 else if (EQ (prop
, QCascii_compatible_p
))
10132 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
10135 CODING_ATTR_PLIST (attrs
)
10136 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
10141 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
10142 Sdefine_coding_system_alias
, 2, 2, 0,
10143 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
10144 (Lisp_Object alias
, Lisp_Object coding_system
)
10146 Lisp_Object spec
, aliases
, eol_type
, val
;
10148 CHECK_SYMBOL (alias
);
10149 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10150 aliases
= AREF (spec
, 1);
10151 /* ALIASES should be a list of length more than zero, and the first
10152 element is a base coding system. Append ALIAS at the tail of the
10154 while (!NILP (XCDR (aliases
)))
10155 aliases
= XCDR (aliases
);
10156 XSETCDR (aliases
, Fcons (alias
, Qnil
));
10158 eol_type
= AREF (spec
, 2);
10159 if (VECTORP (eol_type
))
10161 Lisp_Object subsidiaries
;
10164 subsidiaries
= make_subsidiaries (alias
);
10165 for (i
= 0; i
< 3; i
++)
10166 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
10167 AREF (eol_type
, i
));
10170 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
10171 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
10172 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
10174 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
10175 Vcoding_system_alist
);
10180 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
10182 doc
: /* Return the base of CODING-SYSTEM.
10183 Any alias or subsidiary coding system is not a base coding system. */)
10184 (Lisp_Object coding_system
)
10186 Lisp_Object spec
, attrs
;
10188 if (NILP (coding_system
))
10189 return (Qno_conversion
);
10190 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10191 attrs
= AREF (spec
, 0);
10192 return CODING_ATTR_BASE_NAME (attrs
);
10195 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
10197 doc
: "Return the property list of CODING-SYSTEM.")
10198 (Lisp_Object coding_system
)
10200 Lisp_Object spec
, attrs
;
10202 if (NILP (coding_system
))
10203 coding_system
= Qno_conversion
;
10204 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10205 attrs
= AREF (spec
, 0);
10206 return CODING_ATTR_PLIST (attrs
);
10210 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
10212 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
10213 (Lisp_Object coding_system
)
10217 if (NILP (coding_system
))
10218 coding_system
= Qno_conversion
;
10219 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
10220 return AREF (spec
, 1);
10223 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
10224 Scoding_system_eol_type
, 1, 1, 0,
10225 doc
: /* Return eol-type of CODING-SYSTEM.
10226 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
10228 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
10229 and CR respectively.
10231 A vector value indicates that a format of end-of-line should be
10232 detected automatically. Nth element of the vector is the subsidiary
10233 coding system whose eol-type is N. */)
10234 (Lisp_Object coding_system
)
10236 Lisp_Object spec
, eol_type
;
10239 if (NILP (coding_system
))
10240 coding_system
= Qno_conversion
;
10241 if (! CODING_SYSTEM_P (coding_system
))
10243 spec
= CODING_SYSTEM_SPEC (coding_system
);
10244 eol_type
= AREF (spec
, 2);
10245 if (VECTORP (eol_type
))
10246 return Fcopy_sequence (eol_type
);
10247 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
10248 return make_number (n
);
10254 /*** 9. Post-amble ***/
10257 init_coding_once (void)
10261 for (i
= 0; i
< coding_category_max
; i
++)
10263 coding_categories
[i
].id
= -1;
10264 coding_priorities
[i
] = i
;
10267 /* ISO2022 specific initialize routine. */
10268 for (i
= 0; i
< 0x20; i
++)
10269 iso_code_class
[i
] = ISO_control_0
;
10270 for (i
= 0x21; i
< 0x7F; i
++)
10271 iso_code_class
[i
] = ISO_graphic_plane_0
;
10272 for (i
= 0x80; i
< 0xA0; i
++)
10273 iso_code_class
[i
] = ISO_control_1
;
10274 for (i
= 0xA1; i
< 0xFF; i
++)
10275 iso_code_class
[i
] = ISO_graphic_plane_1
;
10276 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
10277 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
10278 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
10279 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
10280 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
10281 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
10282 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
10283 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
10284 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
10286 for (i
= 0; i
< 256; i
++)
10288 emacs_mule_bytes
[i
] = 1;
10290 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
10291 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
10292 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
10293 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
10299 syms_of_coding (void)
10301 staticpro (&Vcoding_system_hash_table
);
10303 Lisp_Object args
[2];
10306 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
10309 staticpro (&Vsjis_coding_system
);
10310 Vsjis_coding_system
= Qnil
;
10312 staticpro (&Vbig5_coding_system
);
10313 Vbig5_coding_system
= Qnil
;
10315 staticpro (&Vcode_conversion_reused_workbuf
);
10316 Vcode_conversion_reused_workbuf
= Qnil
;
10318 staticpro (&Vcode_conversion_workbuf_name
);
10319 Vcode_conversion_workbuf_name
= make_pure_c_string (" *code-conversion-work*");
10321 reused_workbuf_in_use
= 0;
10323 DEFSYM (Qcharset
, "charset");
10324 DEFSYM (Qtarget_idx
, "target-idx");
10325 DEFSYM (Qcoding_system_history
, "coding-system-history");
10326 Fset (Qcoding_system_history
, Qnil
);
10328 /* Target FILENAME is the first argument. */
10329 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
10330 /* Target FILENAME is the third argument. */
10331 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
10333 DEFSYM (Qcall_process
, "call-process");
10334 /* Target PROGRAM is the first argument. */
10335 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
10337 DEFSYM (Qcall_process_region
, "call-process-region");
10338 /* Target PROGRAM is the third argument. */
10339 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
10341 DEFSYM (Qstart_process
, "start-process");
10342 /* Target PROGRAM is the third argument. */
10343 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
10345 DEFSYM (Qopen_network_stream
, "open-network-stream");
10346 /* Target SERVICE is the fourth argument. */
10347 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
10349 DEFSYM (Qcoding_system
, "coding-system");
10350 DEFSYM (Qcoding_aliases
, "coding-aliases");
10352 DEFSYM (Qeol_type
, "eol-type");
10353 DEFSYM (Qunix
, "unix");
10354 DEFSYM (Qdos
, "dos");
10356 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
10357 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
10358 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
10359 DEFSYM (Qdefault_char
, "default-char");
10360 DEFSYM (Qundecided
, "undecided");
10361 DEFSYM (Qno_conversion
, "no-conversion");
10362 DEFSYM (Qraw_text
, "raw-text");
10364 DEFSYM (Qiso_2022
, "iso-2022");
10366 DEFSYM (Qutf_8
, "utf-8");
10367 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
10369 DEFSYM (Qutf_16
, "utf-16");
10370 DEFSYM (Qbig
, "big");
10371 DEFSYM (Qlittle
, "little");
10373 DEFSYM (Qshift_jis
, "shift-jis");
10374 DEFSYM (Qbig5
, "big5");
10376 DEFSYM (Qcoding_system_p
, "coding-system-p");
10378 DEFSYM (Qcoding_system_error
, "coding-system-error");
10379 Fput (Qcoding_system_error
, Qerror_conditions
,
10380 pure_cons (Qcoding_system_error
, pure_cons (Qerror
, Qnil
)));
10381 Fput (Qcoding_system_error
, Qerror_message
,
10382 make_pure_c_string ("Invalid coding system"));
10384 /* Intern this now in case it isn't already done.
10385 Setting this variable twice is harmless.
10386 But don't staticpro it here--that is done in alloc.c. */
10387 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
10389 DEFSYM (Qtranslation_table
, "translation-table");
10390 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
10391 DEFSYM (Qtranslation_table_id
, "translation-table-id");
10392 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
10393 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
10395 DEFSYM (Qvalid_codes
, "valid-codes");
10397 DEFSYM (Qemacs_mule
, "emacs-mule");
10399 DEFSYM (QCcategory
, ":category");
10400 DEFSYM (QCmnemonic
, ":mnemonic");
10401 DEFSYM (QCdefault_char
, ":default-char");
10402 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
10403 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
10404 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
10405 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
10406 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
10408 Vcoding_category_table
10409 = Fmake_vector (make_number (coding_category_max
), Qnil
);
10410 staticpro (&Vcoding_category_table
);
10411 /* Followings are target of code detection. */
10412 ASET (Vcoding_category_table
, coding_category_iso_7
,
10413 intern_c_string ("coding-category-iso-7"));
10414 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
10415 intern_c_string ("coding-category-iso-7-tight"));
10416 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
10417 intern_c_string ("coding-category-iso-8-1"));
10418 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
10419 intern_c_string ("coding-category-iso-8-2"));
10420 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
10421 intern_c_string ("coding-category-iso-7-else"));
10422 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
10423 intern_c_string ("coding-category-iso-8-else"));
10424 ASET (Vcoding_category_table
, coding_category_utf_8_auto
,
10425 intern_c_string ("coding-category-utf-8-auto"));
10426 ASET (Vcoding_category_table
, coding_category_utf_8_nosig
,
10427 intern_c_string ("coding-category-utf-8"));
10428 ASET (Vcoding_category_table
, coding_category_utf_8_sig
,
10429 intern_c_string ("coding-category-utf-8-sig"));
10430 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
10431 intern_c_string ("coding-category-utf-16-be"));
10432 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
10433 intern_c_string ("coding-category-utf-16-auto"));
10434 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
10435 intern_c_string ("coding-category-utf-16-le"));
10436 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
10437 intern_c_string ("coding-category-utf-16-be-nosig"));
10438 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
10439 intern_c_string ("coding-category-utf-16-le-nosig"));
10440 ASET (Vcoding_category_table
, coding_category_charset
,
10441 intern_c_string ("coding-category-charset"));
10442 ASET (Vcoding_category_table
, coding_category_sjis
,
10443 intern_c_string ("coding-category-sjis"));
10444 ASET (Vcoding_category_table
, coding_category_big5
,
10445 intern_c_string ("coding-category-big5"));
10446 ASET (Vcoding_category_table
, coding_category_ccl
,
10447 intern_c_string ("coding-category-ccl"));
10448 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
10449 intern_c_string ("coding-category-emacs-mule"));
10450 /* Followings are NOT target of code detection. */
10451 ASET (Vcoding_category_table
, coding_category_raw_text
,
10452 intern_c_string ("coding-category-raw-text"));
10453 ASET (Vcoding_category_table
, coding_category_undecided
,
10454 intern_c_string ("coding-category-undecided"));
10456 DEFSYM (Qinsufficient_source
, "insufficient-source");
10457 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
10458 DEFSYM (Qinvalid_source
, "invalid-source");
10459 DEFSYM (Qinterrupted
, "interrupted");
10460 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
10461 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
10463 defsubr (&Scoding_system_p
);
10464 defsubr (&Sread_coding_system
);
10465 defsubr (&Sread_non_nil_coding_system
);
10466 defsubr (&Scheck_coding_system
);
10467 defsubr (&Sdetect_coding_region
);
10468 defsubr (&Sdetect_coding_string
);
10469 defsubr (&Sfind_coding_systems_region_internal
);
10470 defsubr (&Sunencodable_char_position
);
10471 defsubr (&Scheck_coding_systems_region
);
10472 defsubr (&Sdecode_coding_region
);
10473 defsubr (&Sencode_coding_region
);
10474 defsubr (&Sdecode_coding_string
);
10475 defsubr (&Sencode_coding_string
);
10476 defsubr (&Sdecode_sjis_char
);
10477 defsubr (&Sencode_sjis_char
);
10478 defsubr (&Sdecode_big5_char
);
10479 defsubr (&Sencode_big5_char
);
10480 defsubr (&Sset_terminal_coding_system_internal
);
10481 defsubr (&Sset_safe_terminal_coding_system_internal
);
10482 defsubr (&Sterminal_coding_system
);
10483 defsubr (&Sset_keyboard_coding_system_internal
);
10484 defsubr (&Skeyboard_coding_system
);
10485 defsubr (&Sfind_operation_coding_system
);
10486 defsubr (&Sset_coding_system_priority
);
10487 defsubr (&Sdefine_coding_system_internal
);
10488 defsubr (&Sdefine_coding_system_alias
);
10489 defsubr (&Scoding_system_put
);
10490 defsubr (&Scoding_system_base
);
10491 defsubr (&Scoding_system_plist
);
10492 defsubr (&Scoding_system_aliases
);
10493 defsubr (&Scoding_system_eol_type
);
10494 defsubr (&Scoding_system_priority_list
);
10496 DEFVAR_LISP ("coding-system-list", Vcoding_system_list
,
10497 doc
: /* List of coding systems.
10499 Do not alter the value of this variable manually. This variable should be
10500 updated by the functions `define-coding-system' and
10501 `define-coding-system-alias'. */);
10502 Vcoding_system_list
= Qnil
;
10504 DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist
,
10505 doc
: /* Alist of coding system names.
10506 Each element is one element list of coding system name.
10507 This variable is given to `completing-read' as COLLECTION argument.
10509 Do not alter the value of this variable manually. This variable should be
10510 updated by the functions `make-coding-system' and
10511 `define-coding-system-alias'. */);
10512 Vcoding_system_alist
= Qnil
;
10514 DEFVAR_LISP ("coding-category-list", Vcoding_category_list
,
10515 doc
: /* List of coding-categories (symbols) ordered by priority.
10517 On detecting a coding system, Emacs tries code detection algorithms
10518 associated with each coding-category one by one in this order. When
10519 one algorithm agrees with a byte sequence of source text, the coding
10520 system bound to the corresponding coding-category is selected.
10522 Don't modify this variable directly, but use `set-coding-system-priority'. */);
10526 Vcoding_category_list
= Qnil
;
10527 for (i
= coding_category_max
- 1; i
>= 0; i
--)
10528 Vcoding_category_list
10529 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
10530 Vcoding_category_list
);
10533 DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read
,
10534 doc
: /* Specify the coding system for read operations.
10535 It is useful to bind this variable with `let', but do not set it globally.
10536 If the value is a coding system, it is used for decoding on read operation.
10537 If not, an appropriate element is used from one of the coding system alists.
10538 There are three such tables: `file-coding-system-alist',
10539 `process-coding-system-alist', and `network-coding-system-alist'. */);
10540 Vcoding_system_for_read
= Qnil
;
10542 DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write
,
10543 doc
: /* Specify the coding system for write operations.
10544 Programs bind this variable with `let', but you should not set it globally.
10545 If the value is a coding system, it is used for encoding of output,
10546 when writing it to a file and when sending it to a file or subprocess.
10548 If this does not specify a coding system, an appropriate element
10549 is used from one of the coding system alists.
10550 There are three such tables: `file-coding-system-alist',
10551 `process-coding-system-alist', and `network-coding-system-alist'.
10552 For output to files, if the above procedure does not specify a coding system,
10553 the value of `buffer-file-coding-system' is used. */);
10554 Vcoding_system_for_write
= Qnil
;
10556 DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used
,
10558 Coding system used in the latest file or process I/O. */);
10559 Vlast_coding_system_used
= Qnil
;
10561 DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error
,
10563 Error status of the last code conversion.
10565 When an error was detected in the last code conversion, this variable
10566 is set to one of the following symbols.
10567 `insufficient-source'
10571 `insufficient-memory'
10572 When no error was detected, the value doesn't change. So, to check
10573 the error status of a code conversion by this variable, you must
10574 explicitly set this variable to nil before performing code
10576 Vlast_code_conversion_error
= Qnil
;
10578 DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion
,
10580 *Non-nil means always inhibit code conversion of end-of-line format.
10581 See info node `Coding Systems' and info node `Text and Binary' concerning
10582 such conversion. */);
10583 inhibit_eol_conversion
= 0;
10585 DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system
,
10587 Non-nil means process buffer inherits coding system of process output.
10588 Bind it to t if the process output is to be treated as if it were a file
10589 read from some filesystem. */);
10590 inherit_process_coding_system
= 0;
10592 DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist
,
10594 Alist to decide a coding system to use for a file I/O operation.
10595 The format is ((PATTERN . VAL) ...),
10596 where PATTERN is a regular expression matching a file name,
10597 VAL is a coding system, a cons of coding systems, or a function symbol.
10598 If VAL is a coding system, it is used for both decoding and encoding
10600 If VAL is a cons of coding systems, the car part is used for decoding,
10601 and the cdr part is used for encoding.
10602 If VAL is a function symbol, the function must return a coding system
10603 or a cons of coding systems which are used as above. The function is
10604 called with an argument that is a list of the arguments with which
10605 `find-operation-coding-system' was called. If the function can't decide
10606 a coding system, it can return `undecided' so that the normal
10607 code-detection is performed.
10609 See also the function `find-operation-coding-system'
10610 and the variable `auto-coding-alist'. */);
10611 Vfile_coding_system_alist
= Qnil
;
10613 DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist
,
10615 Alist to decide a coding system to use for a process I/O operation.
10616 The format is ((PATTERN . VAL) ...),
10617 where PATTERN is a regular expression matching a program name,
10618 VAL is a coding system, a cons of coding systems, or a function symbol.
10619 If VAL is a coding system, it is used for both decoding what received
10620 from the program and encoding what sent to the program.
10621 If VAL is a cons of coding systems, the car part is used for decoding,
10622 and the cdr part is used for encoding.
10623 If VAL is a function symbol, the function must return a coding system
10624 or a cons of coding systems which are used as above.
10626 See also the function `find-operation-coding-system'. */);
10627 Vprocess_coding_system_alist
= Qnil
;
10629 DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist
,
10631 Alist to decide a coding system to use for a network I/O operation.
10632 The format is ((PATTERN . VAL) ...),
10633 where PATTERN is a regular expression matching a network service name
10634 or is a port number to connect to,
10635 VAL is a coding system, a cons of coding systems, or a function symbol.
10636 If VAL is a coding system, it is used for both decoding what received
10637 from the network stream and encoding what sent to the network stream.
10638 If VAL is a cons of coding systems, the car part is used for decoding,
10639 and the cdr part is used for encoding.
10640 If VAL is a function symbol, the function must return a coding system
10641 or a cons of coding systems which are used as above.
10643 See also the function `find-operation-coding-system'. */);
10644 Vnetwork_coding_system_alist
= Qnil
;
10646 DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system
,
10647 doc
: /* Coding system to use with system messages.
10648 Also used for decoding keyboard input on X Window system. */);
10649 Vlocale_coding_system
= Qnil
;
10651 /* The eol mnemonics are reset in startup.el system-dependently. */
10652 DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix
,
10654 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
10655 eol_mnemonic_unix
= make_pure_c_string (":");
10657 DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos
,
10659 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
10660 eol_mnemonic_dos
= make_pure_c_string ("\\");
10662 DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac
,
10664 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
10665 eol_mnemonic_mac
= make_pure_c_string ("/");
10667 DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided
,
10669 *String displayed in mode line when end-of-line format is not yet determined. */);
10670 eol_mnemonic_undecided
= make_pure_c_string (":");
10672 DEFVAR_LISP ("enable-character-translation", Venable_character_translation
,
10674 *Non-nil enables character translation while encoding and decoding. */);
10675 Venable_character_translation
= Qt
;
10677 DEFVAR_LISP ("standard-translation-table-for-decode",
10678 Vstandard_translation_table_for_decode
,
10679 doc
: /* Table for translating characters while decoding. */);
10680 Vstandard_translation_table_for_decode
= Qnil
;
10682 DEFVAR_LISP ("standard-translation-table-for-encode",
10683 Vstandard_translation_table_for_encode
,
10684 doc
: /* Table for translating characters while encoding. */);
10685 Vstandard_translation_table_for_encode
= Qnil
;
10687 DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table
,
10688 doc
: /* Alist of charsets vs revision numbers.
10689 While encoding, if a charset (car part of an element) is found,
10690 designate it with the escape sequence identifying revision (cdr part
10691 of the element). */);
10692 Vcharset_revision_table
= Qnil
;
10694 DEFVAR_LISP ("default-process-coding-system",
10695 Vdefault_process_coding_system
,
10696 doc
: /* Cons of coding systems used for process I/O by default.
10697 The car part is used for decoding a process output,
10698 the cdr part is used for encoding a text to be sent to a process. */);
10699 Vdefault_process_coding_system
= Qnil
;
10701 DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table
,
10703 Table of extra Latin codes in the range 128..159 (inclusive).
10704 This is a vector of length 256.
10705 If Nth element is non-nil, the existence of code N in a file
10706 \(or output of subprocess) doesn't prevent it to be detected as
10707 a coding system of ISO 2022 variant which has a flag
10708 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10709 or reading output of a subprocess.
10710 Only 128th through 159th elements have a meaning. */);
10711 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10713 DEFVAR_LISP ("select-safe-coding-system-function",
10714 Vselect_safe_coding_system_function
,
10716 Function to call to select safe coding system for encoding a text.
10718 If set, this function is called to force a user to select a proper
10719 coding system which can encode the text in the case that a default
10720 coding system used in each operation can't encode the text. The
10721 function should take care that the buffer is not modified while
10722 the coding system is being selected.
10724 The default value is `select-safe-coding-system' (which see). */);
10725 Vselect_safe_coding_system_function
= Qnil
;
10727 DEFVAR_BOOL ("coding-system-require-warning",
10728 coding_system_require_warning
,
10729 doc
: /* Internal use only.
10730 If non-nil, on writing a file, `select-safe-coding-system-function' is
10731 called even if `coding-system-for-write' is non-nil. The command
10732 `universal-coding-system-argument' binds this variable to t temporarily. */);
10733 coding_system_require_warning
= 0;
10736 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10737 inhibit_iso_escape_detection
,
10739 If non-nil, Emacs ignores ISO-2022 escape sequences during code detection.
10741 When Emacs reads text, it tries to detect how the text is encoded.
10742 This code detection is sensitive to escape sequences. If Emacs sees
10743 a valid ISO-2022 escape sequence, it assumes the text is encoded in one
10744 of the ISO2022 encodings, and decodes text by the corresponding coding
10745 system (e.g. `iso-2022-7bit').
10747 However, there may be a case that you want to read escape sequences in
10748 a file as is. In such a case, you can set this variable to non-nil.
10749 Then the code detection will ignore any escape sequences, and no text is
10750 detected as encoded in some ISO-2022 encoding. The result is that all
10751 escape sequences become visible in a buffer.
10753 The default value is nil, and it is strongly recommended not to change
10754 it. That is because many Emacs Lisp source files that contain
10755 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10756 in Emacs's distribution, and they won't be decoded correctly on
10757 reading if you suppress escape sequence detection.
10759 The other way to read escape sequences in a file without decoding is
10760 to explicitly specify some coding system that doesn't use ISO-2022
10761 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10762 inhibit_iso_escape_detection
= 0;
10764 DEFVAR_BOOL ("inhibit-null-byte-detection",
10765 inhibit_null_byte_detection
,
10766 doc
: /* If non-nil, Emacs ignores null bytes on code detection.
10767 By default, Emacs treats it as binary data, and does not attempt to
10768 decode it. The effect is as if you specified `no-conversion' for
10771 Set this to non-nil when a regular text happens to include null bytes.
10772 Examples are Index nodes of Info files and null-byte delimited output
10773 from GNU Find and GNU Grep. Emacs will then ignore the null bytes and
10774 decode text as usual. */);
10775 inhibit_null_byte_detection
= 0;
10777 DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input
,
10778 doc
: /* Char table for translating self-inserting characters.
10779 This is applied to the result of input methods, not their input.
10780 See also `keyboard-translate-table'.
10782 Use of this variable for character code unification was rendered
10783 obsolete in Emacs 23.1 and later, since Unicode is now the basis of
10784 internal character representation. */);
10785 Vtranslation_table_for_input
= Qnil
;
10788 Lisp_Object args
[coding_arg_max
];
10789 Lisp_Object plist
[16];
10792 for (i
= 0; i
< coding_arg_max
; i
++)
10795 plist
[0] = intern_c_string (":name");
10796 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10797 plist
[2] = intern_c_string (":mnemonic");
10798 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10799 plist
[4] = intern_c_string (":coding-type");
10800 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10801 plist
[6] = intern_c_string (":ascii-compatible-p");
10802 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10803 plist
[8] = intern_c_string (":default-char");
10804 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10805 plist
[10] = intern_c_string (":for-unibyte");
10806 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10807 plist
[12] = intern_c_string (":docstring");
10808 plist
[13] = make_pure_c_string ("Do no conversion.\n\
10810 When you visit a file with this coding, the file is read into a\n\
10811 unibyte buffer as is, thus each byte of a file is treated as a\n\
10813 plist
[14] = intern_c_string (":eol-type");
10814 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10815 args
[coding_arg_plist
] = Flist (16, plist
);
10816 Fdefine_coding_system_internal (coding_arg_max
, args
);
10818 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10819 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10820 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10821 /* This is already set.
10822 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10823 plist
[8] = intern_c_string (":charset-list");
10824 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10825 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10826 plist
[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding.");
10827 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10828 args
[coding_arg_plist
] = Flist (16, plist
);
10829 Fdefine_coding_system_internal (coding_arg_max
, args
);
10832 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10837 for (i
= 0; i
< coding_category_max
; i
++)
10838 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10840 #if defined (DOS_NT)
10841 system_eol_type
= Qdos
;
10843 system_eol_type
= Qunix
;
10845 staticpro (&system_eol_type
);
10849 emacs_strerror (int error_number
)
10853 synchronize_system_messages_locale ();
10854 str
= strerror (error_number
);
10856 if (! NILP (Vlocale_coding_system
))
10858 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10859 Vlocale_coding_system
,
10861 str
= SSDATA (dec
);