1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
12 This file is part of GNU Emacs.
14 GNU Emacs is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 GNU Emacs is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 /*** TABLE OF CONTENTS ***
31 2. Emacs' internal format (emacs-utf-8) handlers
34 5. Charset-base coding systems handlers
35 6. emacs-mule (old Emacs' internal format) handlers
37 8. Shift-JIS and BIG5 handlers
39 10. C library functions
40 11. Emacs Lisp library functions
45 /*** 0. General comments ***
50 A coding system is an object for an encoding mechanism that contains
51 information about how to convert byte sequences to character
52 sequences and vice versa. When we say "decode", it means converting
53 a byte sequence of a specific coding system into a character
54 sequence that is represented by Emacs' internal coding system
55 `emacs-utf-8', and when we say "encode", it means converting a
56 character sequence of emacs-utf-8 to a byte sequence of a specific
59 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
60 C level, a coding system is represented by a vector of attributes
61 stored in the hash table Vcharset_hash_table. The conversion from
62 coding system symbol to attributes vector is done by looking up
63 Vcharset_hash_table by the symbol.
65 Coding systems are classified into the following types depending on
66 the encoding mechanism. Here's a brief description of the types.
72 o Charset-base coding system
74 A coding system defined by one or more (coded) character sets.
75 Decoding and encoding are done by a code converter defined for each
78 o Old Emacs internal format (emacs-mule)
80 The coding system adopted by old versions of Emacs (20 and 21).
82 o ISO2022-base coding system
84 The most famous coding system for multiple character sets. X's
85 Compound Text, various EUCs (Extended Unix Code), and coding systems
86 used in the Internet communication such as ISO-2022-JP are all
89 o SJIS (or Shift-JIS or MS-Kanji-Code)
91 A coding system to encode character sets: ASCII, JISX0201, and
92 JISX0208. Widely used for PC's in Japan. Details are described in
97 A coding system to encode character sets: ASCII and Big5. Widely
98 used for Chinese (mainly in Taiwan and Hong Kong). Details are
99 described in section 8. In this file, when we write "big5" (all
100 lowercase), we mean the coding system, and when we write "Big5"
101 (capitalized), we mean the character set.
105 If a user wants to decode/encode text encoded in a coding system
106 not listed above, he can supply a decoder and an encoder for it in
107 CCL (Code Conversion Language) programs. Emacs executes the CCL
108 program while decoding/encoding.
112 A coding system for text containing raw eight-bit data. Emacs
113 treats each byte of source text as a character (except for
114 end-of-line conversion).
118 Like raw text, but don't do end-of-line conversion.
123 How text end-of-line is encoded depends on operating system. For
124 instance, Unix's format is just one byte of LF (line-feed) code,
125 whereas DOS's format is two-byte sequence of `carriage-return' and
126 `line-feed' codes. MacOS's format is usually one byte of
129 Since text character encoding and end-of-line encoding are
130 independent, any coding system described above can take any format
131 of end-of-line (except for no-conversion).
135 Before using a coding system for code conversion (i.e. decoding and
136 encoding), we setup a structure of type `struct coding_system'.
137 This structure keeps various information about a specific code
138 conversion (e.g. the location of source and destination data).
145 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
147 These functions check if a byte sequence specified as a source in
148 CODING conforms to the format of XXX, and update the members of
151 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
153 Below is the template of these functions. */
157 detect_coding_XXX (coding
, detect_info
)
158 struct coding_system
*coding
;
159 struct coding_detection_info
*detect_info
;
161 const unsigned char *src
= coding
->source
;
162 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
163 int multibytep
= coding
->src_multibyte
;
164 int consumed_chars
= 0;
170 /* Get one byte from the source. If the souce is exausted, jump
171 to no_more_source:. */
174 if (! __C_conforms_to_XXX___ (c
))
176 if (! __C_strongly_suggests_XXX__ (c
))
177 found
= CATEGORY_MASK_XXX
;
179 /* The byte sequence is invalid for XXX. */
180 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
184 /* The source exausted successfully. */
185 detect_info
->found
|= found
;
190 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
192 These functions decode a byte sequence specified as a source by
193 CODING. The resulting multibyte text goes to a place pointed to by
194 CODING->charbuf, the length of which should not exceed
195 CODING->charbuf_size;
197 These functions set the information of original and decoded texts in
198 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
199 They also set CODING->result to one of CODING_RESULT_XXX indicating
200 how the decoding is finished.
202 Below is the template of these functions. */
206 decode_coding_XXXX (coding
)
207 struct coding_system
*coding
;
209 const unsigned char *src
= coding
->source
+ coding
->consumed
;
210 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
211 /* SRC_BASE remembers the start position in source in each loop.
212 The loop will be exited when there's not enough source code, or
213 when there's no room in CHARBUF for a decoded character. */
214 const unsigned char *src_base
;
215 /* A buffer to produce decoded characters. */
216 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
217 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
218 int multibytep
= coding
->src_multibyte
;
223 if (charbuf
< charbuf_end
)
224 /* No more room to produce a decoded character. */
231 if (src_base
< src_end
232 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
233 /* If the source ends by partial bytes to construct a character,
234 treat them as eight-bit raw data. */
235 while (src_base
< src_end
&& charbuf
< charbuf_end
)
236 *charbuf
++ = *src_base
++;
237 /* Remember how many bytes and characters we consumed. If the
238 source is multibyte, the bytes and chars are not identical. */
239 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
240 /* Remember how many characters we produced. */
241 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
245 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
247 These functions encode SRC_BYTES length text at SOURCE of Emacs'
248 internal multibyte format by CODING. The resulting byte sequence
249 goes to a place pointed to by DESTINATION, the length of which
250 should not exceed DST_BYTES.
252 These functions set the information of original and encoded texts in
253 the members produced, produced_char, consumed, and consumed_char of
254 the structure *CODING. They also set the member result to one of
255 CODING_RESULT_XXX indicating how the encoding finished.
257 DST_BYTES zero means that source area and destination area are
258 overlapped, which means that we can produce a encoded text until it
259 reaches at the head of not-yet-encoded source text.
261 Below is a template of these functions. */
264 encode_coding_XXX (coding
)
265 struct coding_system
*coding
;
267 int multibytep
= coding
->dst_multibyte
;
268 int *charbuf
= coding
->charbuf
;
269 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
270 unsigned char *dst
= coding
->destination
+ coding
->produced
;
271 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
272 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
273 int produced_chars
= 0;
275 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
278 /* Encode C into DST, and increment DST. */
280 label_no_more_destination
:
281 /* How many chars and bytes we produced. */
282 coding
->produced_char
+= produced_chars
;
283 coding
->produced
= dst
- coding
->destination
;
288 /*** 1. Preamble ***/
295 #include "character.h"
298 #include "composite.h"
302 #include "termhooks.h"
304 Lisp_Object Vcoding_system_hash_table
;
306 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
307 Lisp_Object Qunix
, Qdos
;
308 extern Lisp_Object Qmac
; /* frame.c */
309 Lisp_Object Qbuffer_file_coding_system
;
310 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
311 Lisp_Object Qdefault_char
;
312 Lisp_Object Qno_conversion
, Qundecided
;
313 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
314 Lisp_Object Qbig
, Qlittle
;
315 Lisp_Object Qcoding_system_history
;
316 Lisp_Object Qvalid_codes
;
317 Lisp_Object QCcategory
, QCmnemonic
, QCdefalut_char
;
318 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
319 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
320 Lisp_Object QCascii_compatible_p
;
322 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
323 Lisp_Object Qcall_process
, Qcall_process_region
;
324 Lisp_Object Qstart_process
, Qopen_network_stream
;
325 Lisp_Object Qtarget_idx
;
327 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
328 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
330 extern Lisp_Object Qcompletion_ignore_case
;
332 /* If a symbol has this property, evaluate the value to define the
333 symbol as a coding system. */
334 static Lisp_Object Qcoding_system_define_form
;
336 int coding_system_require_warning
;
338 Lisp_Object Vselect_safe_coding_system_function
;
340 /* Mnemonic string for each format of end-of-line. */
341 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
342 /* Mnemonic string to indicate format of end-of-line is not yet
344 Lisp_Object eol_mnemonic_undecided
;
346 /* Format of end-of-line decided by system. This is Qunix on
347 Unix and Mac, Qdos on DOS/Windows.
348 This has an effect only for external encoding (i.e. for output to
349 file and process), not for in-buffer or Lisp string encoding. */
350 static Lisp_Object system_eol_type
;
354 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
356 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
358 /* Coding system emacs-mule and raw-text are for converting only
359 end-of-line format. */
360 Lisp_Object Qemacs_mule
, Qraw_text
;
361 Lisp_Object Qutf_8_emacs
;
363 /* Coding-systems are handed between Emacs Lisp programs and C internal
364 routines by the following three variables. */
365 /* Coding-system for reading files and receiving data from process. */
366 Lisp_Object Vcoding_system_for_read
;
367 /* Coding-system for writing files and sending data to process. */
368 Lisp_Object Vcoding_system_for_write
;
369 /* Coding-system actually used in the latest I/O. */
370 Lisp_Object Vlast_coding_system_used
;
371 /* Set to non-nil when an error is detected while code conversion. */
372 Lisp_Object Vlast_code_conversion_error
;
373 /* A vector of length 256 which contains information about special
374 Latin codes (especially for dealing with Microsoft codes). */
375 Lisp_Object Vlatin_extra_code_table
;
377 /* Flag to inhibit code conversion of end-of-line format. */
378 int inhibit_eol_conversion
;
380 /* Flag to inhibit ISO2022 escape sequence detection. */
381 int inhibit_iso_escape_detection
;
383 /* Flag to make buffer-file-coding-system inherit from process-coding. */
384 int inherit_process_coding_system
;
386 /* Coding system to be used to encode text for terminal display when
387 terminal coding system is nil. */
388 struct coding_system safe_terminal_coding
;
390 Lisp_Object Vfile_coding_system_alist
;
391 Lisp_Object Vprocess_coding_system_alist
;
392 Lisp_Object Vnetwork_coding_system_alist
;
394 Lisp_Object Vlocale_coding_system
;
398 /* Flag to tell if we look up translation table on character code
400 Lisp_Object Venable_character_translation
;
401 /* Standard translation table to look up on decoding (reading). */
402 Lisp_Object Vstandard_translation_table_for_decode
;
403 /* Standard translation table to look up on encoding (writing). */
404 Lisp_Object Vstandard_translation_table_for_encode
;
406 Lisp_Object Qtranslation_table
;
407 Lisp_Object Qtranslation_table_id
;
408 Lisp_Object Qtranslation_table_for_decode
;
409 Lisp_Object Qtranslation_table_for_encode
;
411 /* Alist of charsets vs revision number. */
412 static Lisp_Object Vcharset_revision_table
;
414 /* Default coding systems used for process I/O. */
415 Lisp_Object Vdefault_process_coding_system
;
417 /* Char table for translating Quail and self-inserting input. */
418 Lisp_Object Vtranslation_table_for_input
;
420 /* Two special coding systems. */
421 Lisp_Object Vsjis_coding_system
;
422 Lisp_Object Vbig5_coding_system
;
424 /* ISO2022 section */
426 #define CODING_ISO_INITIAL(coding, reg) \
427 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
428 coding_attr_iso_initial), \
432 #define CODING_ISO_REQUEST(coding, charset_id) \
433 ((charset_id <= (coding)->max_charset_id \
434 ? (coding)->safe_charsets[charset_id] \
438 #define CODING_ISO_FLAGS(coding) \
439 ((coding)->spec.iso_2022.flags)
440 #define CODING_ISO_DESIGNATION(coding, reg) \
441 ((coding)->spec.iso_2022.current_designation[reg])
442 #define CODING_ISO_INVOCATION(coding, plane) \
443 ((coding)->spec.iso_2022.current_invocation[plane])
444 #define CODING_ISO_SINGLE_SHIFTING(coding) \
445 ((coding)->spec.iso_2022.single_shifting)
446 #define CODING_ISO_BOL(coding) \
447 ((coding)->spec.iso_2022.bol)
448 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
449 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
451 /* Control characters of ISO2022. */
452 /* code */ /* function */
453 #define ISO_CODE_LF 0x0A /* line-feed */
454 #define ISO_CODE_CR 0x0D /* carriage-return */
455 #define ISO_CODE_SO 0x0E /* shift-out */
456 #define ISO_CODE_SI 0x0F /* shift-in */
457 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
458 #define ISO_CODE_ESC 0x1B /* escape */
459 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
460 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
461 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
463 /* All code (1-byte) of ISO2022 is classified into one of the
465 enum iso_code_class_type
467 ISO_control_0
, /* Control codes in the range
468 0x00..0x1F and 0x7F, except for the
469 following 5 codes. */
470 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
471 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
472 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
473 ISO_escape
, /* ISO_CODE_SO (0x1B) */
474 ISO_control_1
, /* Control codes in the range
475 0x80..0x9F, except for the
476 following 3 codes. */
477 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
478 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
479 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
480 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
481 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
482 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
483 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
486 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
487 `iso-flags' attribute of an iso2022 coding system. */
489 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
490 instead of the correct short-form sequence (e.g. ESC $ A). */
491 #define CODING_ISO_FLAG_LONG_FORM 0x0001
493 /* If set, reset graphic planes and registers at end-of-line to the
495 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
497 /* If set, reset graphic planes and registers before any control
498 characters to the initial state. */
499 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
501 /* If set, encode by 7-bit environment. */
502 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
504 /* If set, use locking-shift function. */
505 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
507 /* If set, use single-shift function. Overwrite
508 CODING_ISO_FLAG_LOCKING_SHIFT. */
509 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
511 /* If set, use designation escape sequence. */
512 #define CODING_ISO_FLAG_DESIGNATION 0x0040
514 /* If set, produce revision number sequence. */
515 #define CODING_ISO_FLAG_REVISION 0x0080
517 /* If set, produce ISO6429's direction specifying sequence. */
518 #define CODING_ISO_FLAG_DIRECTION 0x0100
520 /* If set, assume designation states are reset at beginning of line on
522 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
524 /* If set, designation sequence should be placed at beginning of line
526 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
528 /* If set, do not encode unsafe charactes on output. */
529 #define CODING_ISO_FLAG_SAFE 0x0800
531 /* If set, extra latin codes (128..159) are accepted as a valid code
533 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
535 #define CODING_ISO_FLAG_COMPOSITION 0x2000
537 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
539 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
541 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
543 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
545 /* A character to be produced on output if encoding of the original
546 character is prohibited by CODING_ISO_FLAG_SAFE. */
547 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
551 #define CODING_UTF_16_BOM(coding) \
552 ((coding)->spec.utf_16.bom)
554 #define CODING_UTF_16_ENDIAN(coding) \
555 ((coding)->spec.utf_16.endian)
557 #define CODING_UTF_16_SURROGATE(coding) \
558 ((coding)->spec.utf_16.surrogate)
562 #define CODING_CCL_DECODER(coding) \
563 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
564 #define CODING_CCL_ENCODER(coding) \
565 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
566 #define CODING_CCL_VALIDS(coding) \
567 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
569 /* Index for each coding category in `coding_categories' */
573 coding_category_iso_7
,
574 coding_category_iso_7_tight
,
575 coding_category_iso_8_1
,
576 coding_category_iso_8_2
,
577 coding_category_iso_7_else
,
578 coding_category_iso_8_else
,
579 coding_category_utf_8
,
580 coding_category_utf_16_auto
,
581 coding_category_utf_16_be
,
582 coding_category_utf_16_le
,
583 coding_category_utf_16_be_nosig
,
584 coding_category_utf_16_le_nosig
,
585 coding_category_charset
,
586 coding_category_sjis
,
587 coding_category_big5
,
589 coding_category_emacs_mule
,
590 /* All above are targets of code detection. */
591 coding_category_raw_text
,
592 coding_category_undecided
,
596 /* Definitions of flag bits used in detect_coding_XXXX. */
597 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
598 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
599 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
600 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
601 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
602 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
603 #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
604 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
605 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
606 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
607 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
608 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
609 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
610 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
611 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
612 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
613 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
614 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
616 /* This value is returned if detect_coding_mask () find nothing other
617 than ASCII characters. */
618 #define CATEGORY_MASK_ANY \
619 (CATEGORY_MASK_ISO_7 \
620 | CATEGORY_MASK_ISO_7_TIGHT \
621 | CATEGORY_MASK_ISO_8_1 \
622 | CATEGORY_MASK_ISO_8_2 \
623 | CATEGORY_MASK_ISO_7_ELSE \
624 | CATEGORY_MASK_ISO_8_ELSE \
625 | CATEGORY_MASK_UTF_8 \
626 | CATEGORY_MASK_UTF_16_AUTO \
627 | CATEGORY_MASK_UTF_16_BE \
628 | CATEGORY_MASK_UTF_16_LE \
629 | CATEGORY_MASK_UTF_16_BE_NOSIG \
630 | CATEGORY_MASK_UTF_16_LE_NOSIG \
631 | CATEGORY_MASK_CHARSET \
632 | CATEGORY_MASK_SJIS \
633 | CATEGORY_MASK_BIG5 \
634 | CATEGORY_MASK_CCL \
635 | CATEGORY_MASK_EMACS_MULE)
638 #define CATEGORY_MASK_ISO_7BIT \
639 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
641 #define CATEGORY_MASK_ISO_8BIT \
642 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
644 #define CATEGORY_MASK_ISO_ELSE \
645 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
647 #define CATEGORY_MASK_ISO_ESCAPE \
648 (CATEGORY_MASK_ISO_7 \
649 | CATEGORY_MASK_ISO_7_TIGHT \
650 | CATEGORY_MASK_ISO_7_ELSE \
651 | CATEGORY_MASK_ISO_8_ELSE)
653 #define CATEGORY_MASK_ISO \
654 ( CATEGORY_MASK_ISO_7BIT \
655 | CATEGORY_MASK_ISO_8BIT \
656 | CATEGORY_MASK_ISO_ELSE)
658 #define CATEGORY_MASK_UTF_16 \
659 (CATEGORY_MASK_UTF_16_AUTO \
660 | CATEGORY_MASK_UTF_16_BE \
661 | CATEGORY_MASK_UTF_16_LE \
662 | CATEGORY_MASK_UTF_16_BE_NOSIG \
663 | CATEGORY_MASK_UTF_16_LE_NOSIG)
666 /* List of symbols `coding-category-xxx' ordered by priority. This
667 variable is exposed to Emacs Lisp. */
668 static Lisp_Object Vcoding_category_list
;
670 /* Table of coding categories (Lisp symbols). This variable is for
672 static Lisp_Object Vcoding_category_table
;
674 /* Table of coding-categories ordered by priority. */
675 static enum coding_category coding_priorities
[coding_category_max
];
677 /* Nth element is a coding context for the coding system bound to the
678 Nth coding category. */
679 static struct coding_system coding_categories
[coding_category_max
];
681 /*** Commonly used macros and functions ***/
684 #define min(a, b) ((a) < (b) ? (a) : (b))
687 #define max(a, b) ((a) > (b) ? (a) : (b))
690 #define CODING_GET_INFO(coding, attrs, charset_list) \
692 (attrs) = CODING_ID_ATTRS ((coding)->id); \
693 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
697 /* Safely get one byte from the source text pointed by SRC which ends
698 at SRC_END, and set C to that byte. If there are not enough bytes
699 in the source, it jumps to `no_more_source'. If multibytep is
700 nonzero, and a multibyte character is found at SRC, set C to the
701 negative value of the character code. The caller should declare
702 and set these variables appropriately in advance:
703 src, src_end, multibytep */
705 #define ONE_MORE_BYTE(c) \
707 if (src == src_end) \
709 if (src_base < src) \
710 record_conversion_result \
711 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
712 goto no_more_source; \
715 if (multibytep && (c & 0x80)) \
717 if ((c & 0xFE) == 0xC0) \
718 c = ((c & 1) << 6) | *src++; \
722 c = - string_char (src, &src, NULL); \
723 record_conversion_result \
724 (coding, CODING_RESULT_INVALID_SRC); \
731 #define ONE_MORE_BYTE_NO_CHECK(c) \
734 if (multibytep && (c & 0x80)) \
736 if ((c & 0xFE) == 0xC0) \
737 c = ((c & 1) << 6) | *src++; \
741 c = - string_char (src, &src, NULL); \
742 record_conversion_result \
743 (coding, CODING_RESULT_INVALID_SRC); \
750 /* Store a byte C in the place pointed by DST and increment DST to the
751 next free point, and increment PRODUCED_CHARS. The caller should
752 assure that C is 0..127, and declare and set the variable `dst'
753 appropriately in advance.
757 #define EMIT_ONE_ASCII_BYTE(c) \
764 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
766 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
768 produced_chars += 2; \
769 *dst++ = (c1), *dst++ = (c2); \
773 /* Store a byte C in the place pointed by DST and increment DST to the
774 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
775 nonzero, store in an appropriate multibyte from. The caller should
776 declare and set the variables `dst' and `multibytep' appropriately
779 #define EMIT_ONE_BYTE(c) \
786 ch = BYTE8_TO_CHAR (ch); \
787 CHAR_STRING_ADVANCE (ch, dst); \
794 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
796 #define EMIT_TWO_BYTES(c1, c2) \
798 produced_chars += 2; \
805 ch = BYTE8_TO_CHAR (ch); \
806 CHAR_STRING_ADVANCE (ch, dst); \
809 ch = BYTE8_TO_CHAR (ch); \
810 CHAR_STRING_ADVANCE (ch, dst); \
820 #define EMIT_THREE_BYTES(c1, c2, c3) \
822 EMIT_ONE_BYTE (c1); \
823 EMIT_TWO_BYTES (c2, c3); \
827 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
829 EMIT_TWO_BYTES (c1, c2); \
830 EMIT_TWO_BYTES (c3, c4); \
834 /* Prototypes for static functions. */
835 static void record_conversion_result
P_ ((struct coding_system
*coding
,
836 enum coding_result_code result
));
837 static int detect_coding_utf_8
P_ ((struct coding_system
*,
838 struct coding_detection_info
*info
));
839 static void decode_coding_utf_8
P_ ((struct coding_system
*));
840 static int encode_coding_utf_8
P_ ((struct coding_system
*));
842 static int detect_coding_utf_16
P_ ((struct coding_system
*,
843 struct coding_detection_info
*info
));
844 static void decode_coding_utf_16
P_ ((struct coding_system
*));
845 static int encode_coding_utf_16
P_ ((struct coding_system
*));
847 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
848 struct coding_detection_info
*info
));
849 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
850 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
852 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
853 struct coding_detection_info
*info
));
854 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
855 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
857 static int detect_coding_sjis
P_ ((struct coding_system
*,
858 struct coding_detection_info
*info
));
859 static void decode_coding_sjis
P_ ((struct coding_system
*));
860 static int encode_coding_sjis
P_ ((struct coding_system
*));
862 static int detect_coding_big5
P_ ((struct coding_system
*,
863 struct coding_detection_info
*info
));
864 static void decode_coding_big5
P_ ((struct coding_system
*));
865 static int encode_coding_big5
P_ ((struct coding_system
*));
867 static int detect_coding_ccl
P_ ((struct coding_system
*,
868 struct coding_detection_info
*info
));
869 static void decode_coding_ccl
P_ ((struct coding_system
*));
870 static int encode_coding_ccl
P_ ((struct coding_system
*));
872 static void decode_coding_raw_text
P_ ((struct coding_system
*));
873 static int encode_coding_raw_text
P_ ((struct coding_system
*));
875 static void coding_set_source
P_ ((struct coding_system
*));
876 static void coding_set_destination
P_ ((struct coding_system
*));
877 static void coding_alloc_by_realloc
P_ ((struct coding_system
*, EMACS_INT
));
878 static void coding_alloc_by_making_gap
P_ ((struct coding_system
*,
879 EMACS_INT
, EMACS_INT
));
880 static unsigned char *alloc_destination
P_ ((struct coding_system
*,
881 EMACS_INT
, unsigned char *));
882 static void setup_iso_safe_charsets
P_ ((Lisp_Object
));
883 static unsigned char *encode_designation_at_bol
P_ ((struct coding_system
*,
886 static int detect_eol
P_ ((const unsigned char *,
887 EMACS_INT
, enum coding_category
));
888 static Lisp_Object adjust_coding_eol_type
P_ ((struct coding_system
*, int));
889 static void decode_eol
P_ ((struct coding_system
*));
890 static Lisp_Object get_translation_table
P_ ((Lisp_Object
, int, int *));
891 static Lisp_Object get_translation
P_ ((Lisp_Object
, int *, int *,
893 static int produce_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
894 static INLINE
void produce_composition
P_ ((struct coding_system
*, int *,
896 static INLINE
void produce_charset
P_ ((struct coding_system
*, int *,
898 static void produce_annotation
P_ ((struct coding_system
*, EMACS_INT
));
899 static int decode_coding
P_ ((struct coding_system
*));
900 static INLINE
int *handle_composition_annotation
P_ ((EMACS_INT
, EMACS_INT
,
901 struct coding_system
*,
902 int *, EMACS_INT
*));
903 static INLINE
int *handle_charset_annotation
P_ ((EMACS_INT
, EMACS_INT
,
904 struct coding_system
*,
905 int *, EMACS_INT
*));
906 static void consume_chars
P_ ((struct coding_system
*, Lisp_Object
, int));
907 static int encode_coding
P_ ((struct coding_system
*));
908 static Lisp_Object make_conversion_work_buffer
P_ ((int));
909 static Lisp_Object code_conversion_restore
P_ ((Lisp_Object
));
910 static INLINE
int char_encodable_p
P_ ((int, Lisp_Object
));
911 static Lisp_Object make_subsidiaries
P_ ((Lisp_Object
));
914 record_conversion_result (struct coding_system
*coding
,
915 enum coding_result_code result
)
917 coding
->result
= result
;
920 case CODING_RESULT_INSUFFICIENT_SRC
:
921 Vlast_code_conversion_error
= Qinsufficient_source
;
923 case CODING_RESULT_INCONSISTENT_EOL
:
924 Vlast_code_conversion_error
= Qinconsistent_eol
;
926 case CODING_RESULT_INVALID_SRC
:
927 Vlast_code_conversion_error
= Qinvalid_source
;
929 case CODING_RESULT_INTERRUPT
:
930 Vlast_code_conversion_error
= Qinterrupted
;
932 case CODING_RESULT_INSUFFICIENT_MEM
:
933 Vlast_code_conversion_error
= Qinsufficient_memory
;
936 Vlast_code_conversion_error
= intern ("Unknown error");
940 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
942 charset_map_loaded = 0; \
943 c = DECODE_CHAR (charset, code); \
944 if (charset_map_loaded) \
946 const unsigned char *orig = coding->source; \
949 coding_set_source (coding); \
950 offset = coding->source - orig; \
952 src_base += offset; \
958 /* If there are at least BYTES length of room at dst, allocate memory
959 for coding->destination and update dst and dst_end. We don't have
960 to take care of coding->source which will be relocated. It is
961 handled by calling coding_set_source in encode_coding. */
963 #define ASSURE_DESTINATION(bytes) \
965 if (dst + (bytes) >= dst_end) \
967 int more_bytes = charbuf_end - charbuf + (bytes); \
969 dst = alloc_destination (coding, more_bytes, dst); \
970 dst_end = coding->destination + coding->dst_bytes; \
975 /* Store multibyte form of the character C in P, and advance P to the
976 end of the multibyte form. This is like CHAR_STRING_ADVANCE but it
977 never calls MAYBE_UNIFY_CHAR. */
979 #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \
981 if ((c) <= MAX_1_BYTE_CHAR) \
983 else if ((c) <= MAX_2_BYTE_CHAR) \
984 *(p)++ = (0xC0 | ((c) >> 6)), \
985 *(p)++ = (0x80 | ((c) & 0x3F)); \
986 else if ((c) <= MAX_3_BYTE_CHAR) \
987 *(p)++ = (0xE0 | ((c) >> 12)), \
988 *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \
989 *(p)++ = (0x80 | ((c) & 0x3F)); \
990 else if ((c) <= MAX_4_BYTE_CHAR) \
991 *(p)++ = (0xF0 | (c >> 18)), \
992 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
993 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
994 *(p)++ = (0x80 | (c & 0x3F)); \
995 else if ((c) <= MAX_5_BYTE_CHAR) \
997 *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \
998 *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \
999 *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \
1000 *(p)++ = (0x80 | (c & 0x3F)); \
1002 (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \
1006 /* Return the character code of character whose multibyte form is at
1007 P, and advance P to the end of the multibyte form. This is like
1008 STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */
1010 #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \
1013 : ! ((p)[0] & 0x20) \
1015 ((((p)[-2] & 0x1F) << 6) \
1016 | ((p)[-1] & 0x3F) \
1017 | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \
1018 : ! ((p)[0] & 0x10) \
1020 ((((p)[-3] & 0x0F) << 12) \
1021 | (((p)[-2] & 0x3F) << 6) \
1022 | ((p)[-1] & 0x3F))) \
1023 : ! ((p)[0] & 0x08) \
1025 ((((p)[-4] & 0xF) << 18) \
1026 | (((p)[-3] & 0x3F) << 12) \
1027 | (((p)[-2] & 0x3F) << 6) \
1028 | ((p)[-1] & 0x3F))) \
1030 ((((p)[-4] & 0x3F) << 18) \
1031 | (((p)[-3] & 0x3F) << 12) \
1032 | (((p)[-2] & 0x3F) << 6) \
1033 | ((p)[-1] & 0x3F))))
1037 coding_set_source (coding
)
1038 struct coding_system
*coding
;
1040 if (BUFFERP (coding
->src_object
))
1042 struct buffer
*buf
= XBUFFER (coding
->src_object
);
1044 if (coding
->src_pos
< 0)
1045 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
1047 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
1049 else if (STRINGP (coding
->src_object
))
1051 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
1054 /* Otherwise, the source is C string and is never relocated
1055 automatically. Thus we don't have to update anything. */
1060 coding_set_destination (coding
)
1061 struct coding_system
*coding
;
1063 if (BUFFERP (coding
->dst_object
))
1065 if (coding
->src_pos
< 0)
1067 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- BEG_BYTE
;
1068 coding
->dst_bytes
= (GAP_END_ADDR
1069 - (coding
->src_bytes
- coding
->consumed
)
1070 - coding
->destination
);
1074 /* We are sure that coding->dst_pos_byte is before the gap
1076 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
1077 + coding
->dst_pos_byte
- BEG_BYTE
);
1078 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
1079 - coding
->destination
);
1083 /* Otherwise, the destination is C string and is never relocated
1084 automatically. Thus we don't have to update anything. */
1090 coding_alloc_by_realloc (coding
, bytes
)
1091 struct coding_system
*coding
;
1094 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
1095 coding
->dst_bytes
+ bytes
);
1096 coding
->dst_bytes
+= bytes
;
1100 coding_alloc_by_making_gap (coding
, gap_head_used
, bytes
)
1101 struct coding_system
*coding
;
1102 EMACS_INT gap_head_used
, bytes
;
1104 if (EQ (coding
->src_object
, coding
->dst_object
))
1106 /* The gap may contain the produced data at the head and not-yet
1107 consumed data at the tail. To preserve those data, we at
1108 first make the gap size to zero, then increase the gap
1110 EMACS_INT add
= GAP_SIZE
;
1112 GPT
+= gap_head_used
, GPT_BYTE
+= gap_head_used
;
1113 GAP_SIZE
= 0; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
1115 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
1116 GPT
-= gap_head_used
, GPT_BYTE
-= gap_head_used
;
1120 Lisp_Object this_buffer
;
1122 this_buffer
= Fcurrent_buffer ();
1123 set_buffer_internal (XBUFFER (coding
->dst_object
));
1125 set_buffer_internal (XBUFFER (this_buffer
));
1130 static unsigned char *
1131 alloc_destination (coding
, nbytes
, dst
)
1132 struct coding_system
*coding
;
1136 EMACS_INT offset
= dst
- coding
->destination
;
1138 if (BUFFERP (coding
->dst_object
))
1140 struct buffer
*buf
= XBUFFER (coding
->dst_object
);
1142 coding_alloc_by_making_gap (coding
, dst
- BUF_GPT_ADDR (buf
), nbytes
);
1145 coding_alloc_by_realloc (coding
, nbytes
);
1146 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1147 coding_set_destination (coding
);
1148 dst
= coding
->destination
+ offset
;
1152 /** Macros for annotations. */
1154 /* Maximum length of annotation data (sum of annotations for
1155 composition and charset). */
1156 #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1158 /* An annotation data is stored in the array coding->charbuf in this
1160 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1161 LENGTH is the number of elements in the annotation.
1162 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1163 NCHARS is the number of characters in the text annotated.
1165 The format of the following elements depend on ANNOTATION_MASK.
1167 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1169 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1170 METHOD is one of enum composition_method.
1171 Optionnal COMPOSITION-COMPONENTS are characters and composition
1174 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1177 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1179 *(buf)++ = -(len); \
1180 *(buf)++ = (mask); \
1181 *(buf)++ = (nchars); \
1182 coding->annotated = 1; \
1185 #define ADD_COMPOSITION_DATA(buf, nchars, method) \
1187 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1192 #define ADD_CHARSET_DATA(buf, nchars, id) \
1194 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1199 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1206 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1207 Check if a text is encoded in UTF-8. If it is, return 1, else
1210 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1211 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1212 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1213 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1214 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1215 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1218 detect_coding_utf_8 (coding
, detect_info
)
1219 struct coding_system
*coding
;
1220 struct coding_detection_info
*detect_info
;
1222 const unsigned char *src
= coding
->source
, *src_base
;
1223 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1224 int multibytep
= coding
->src_multibyte
;
1225 int consumed_chars
= 0;
1228 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1229 /* A coding system of this category is always ASCII compatible. */
1230 src
+= coding
->head_ascii
;
1234 int c
, c1
, c2
, c3
, c4
;
1238 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1241 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1243 if (UTF_8_2_OCTET_LEADING_P (c
))
1245 found
= CATEGORY_MASK_UTF_8
;
1249 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1251 if (UTF_8_3_OCTET_LEADING_P (c
))
1253 found
= CATEGORY_MASK_UTF_8
;
1257 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1259 if (UTF_8_4_OCTET_LEADING_P (c
))
1261 found
= CATEGORY_MASK_UTF_8
;
1265 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1267 if (UTF_8_5_OCTET_LEADING_P (c
))
1269 found
= CATEGORY_MASK_UTF_8
;
1274 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1278 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1280 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1283 detect_info
->found
|= found
;
1289 decode_coding_utf_8 (coding
)
1290 struct coding_system
*coding
;
1292 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1293 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1294 const unsigned char *src_base
;
1295 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1296 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1297 int consumed_chars
= 0, consumed_chars_base
;
1298 int multibytep
= coding
->src_multibyte
;
1299 Lisp_Object attr
, charset_list
;
1300 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1301 int byte_after_cr
= -1;
1303 CODING_GET_INFO (coding
, attr
, charset_list
);
1307 int c
, c1
, c2
, c3
, c4
, c5
;
1310 consumed_chars_base
= consumed_chars
;
1312 if (charbuf
>= charbuf_end
)
1315 if (byte_after_cr
>= 0)
1316 c1
= byte_after_cr
, byte_after_cr
= -1;
1323 else if (UTF_8_1_OCTET_P(c1
))
1325 if (eol_crlf
&& c1
== '\r')
1326 ONE_MORE_BYTE (byte_after_cr
);
1332 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1334 if (UTF_8_2_OCTET_LEADING_P (c1
))
1336 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1337 /* Reject overlong sequences here and below. Encoders
1338 producing them are incorrect, they can be misleading,
1339 and they mess up read/write invariance. */
1346 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1348 if (UTF_8_3_OCTET_LEADING_P (c1
))
1350 c
= (((c1
& 0xF) << 12)
1351 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1353 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1359 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1361 if (UTF_8_4_OCTET_LEADING_P (c1
))
1363 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1364 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1371 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1373 if (UTF_8_5_OCTET_LEADING_P (c1
))
1375 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1376 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1378 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1393 consumed_chars
= consumed_chars_base
;
1395 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1400 coding
->consumed_char
+= consumed_chars_base
;
1401 coding
->consumed
= src_base
- coding
->source
;
1402 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1407 encode_coding_utf_8 (coding
)
1408 struct coding_system
*coding
;
1410 int multibytep
= coding
->dst_multibyte
;
1411 int *charbuf
= coding
->charbuf
;
1412 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1413 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1414 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1415 int produced_chars
= 0;
1420 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1422 while (charbuf
< charbuf_end
)
1424 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1426 ASSURE_DESTINATION (safe_room
);
1428 if (CHAR_BYTE8_P (c
))
1430 c
= CHAR_TO_BYTE8 (c
);
1435 CHAR_STRING_ADVANCE_NO_UNIFY (c
, pend
);
1436 for (p
= str
; p
< pend
; p
++)
1443 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1445 while (charbuf
< charbuf_end
)
1447 ASSURE_DESTINATION (safe_room
);
1449 if (CHAR_BYTE8_P (c
))
1450 *dst
++ = CHAR_TO_BYTE8 (c
);
1452 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
1456 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1457 coding
->produced_char
+= produced_chars
;
1458 coding
->produced
= dst
- coding
->destination
;
1463 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1464 Check if a text is encoded in one of UTF-16 based coding systems.
1465 If it is, return 1, else return 0. */
1467 #define UTF_16_HIGH_SURROGATE_P(val) \
1468 (((val) & 0xFC00) == 0xD800)
1470 #define UTF_16_LOW_SURROGATE_P(val) \
1471 (((val) & 0xFC00) == 0xDC00)
1473 #define UTF_16_INVALID_P(val) \
1474 (((val) == 0xFFFE) \
1475 || ((val) == 0xFFFF) \
1476 || UTF_16_LOW_SURROGATE_P (val))
1480 detect_coding_utf_16 (coding
, detect_info
)
1481 struct coding_system
*coding
;
1482 struct coding_detection_info
*detect_info
;
1484 const unsigned char *src
= coding
->source
, *src_base
= src
;
1485 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1486 int multibytep
= coding
->src_multibyte
;
1487 int consumed_chars
= 0;
1490 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1491 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1492 && (coding
->src_chars
& 1))
1494 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1500 if ((c1
== 0xFF) && (c2
== 0xFE))
1502 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1503 | CATEGORY_MASK_UTF_16_AUTO
);
1504 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1505 | CATEGORY_MASK_UTF_16_BE_NOSIG
1506 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1508 else if ((c1
== 0xFE) && (c2
== 0xFF))
1510 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1511 | CATEGORY_MASK_UTF_16_AUTO
);
1512 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1513 | CATEGORY_MASK_UTF_16_BE_NOSIG
1514 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1518 /* We check the dispersion of Eth and Oth bytes where E is even and
1519 O is odd. If both are high, we assume binary data.*/
1520 unsigned char e
[256], o
[256];
1521 unsigned e_num
= 1, o_num
= 1;
1528 detect_info
->rejected
1529 |= (CATEGORY_MASK_UTF_16_BE
| CATEGORY_MASK_UTF_16_LE
);
1550 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1559 decode_coding_utf_16 (coding
)
1560 struct coding_system
*coding
;
1562 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1563 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1564 const unsigned char *src_base
;
1565 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1566 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1567 int consumed_chars
= 0, consumed_chars_base
;
1568 int multibytep
= coding
->src_multibyte
;
1569 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1570 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1571 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1572 Lisp_Object attr
, charset_list
;
1573 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
1574 int byte_after_cr1
= -1, byte_after_cr2
= -1;
1576 CODING_GET_INFO (coding
, attr
, charset_list
);
1578 if (bom
== utf_16_with_bom
)
1587 if (endian
== utf_16_big_endian
1588 ? c
!= 0xFEFF : c
!= 0xFFFE)
1590 /* The first two bytes are not BOM. Treat them as bytes
1591 for a normal character. */
1595 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1597 else if (bom
== utf_16_detect_bom
)
1599 /* We have already tried to detect BOM and failed in
1601 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1609 consumed_chars_base
= consumed_chars
;
1611 if (charbuf
+ 2 >= charbuf_end
)
1614 if (byte_after_cr1
>= 0)
1615 c1
= byte_after_cr1
, byte_after_cr1
= -1;
1623 if (byte_after_cr2
>= 0)
1624 c2
= byte_after_cr2
, byte_after_cr2
= -1;
1629 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1633 c
= (endian
== utf_16_big_endian
1634 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1638 if (! UTF_16_LOW_SURROGATE_P (c
))
1640 if (endian
== utf_16_big_endian
)
1641 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1643 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1647 if (UTF_16_HIGH_SURROGATE_P (c
))
1648 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1654 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1655 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1656 *charbuf
++ = 0x10000 + c
;
1661 if (UTF_16_HIGH_SURROGATE_P (c
))
1662 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1665 if (eol_crlf
&& c
== '\r')
1667 ONE_MORE_BYTE (byte_after_cr1
);
1668 ONE_MORE_BYTE (byte_after_cr2
);
1676 coding
->consumed_char
+= consumed_chars_base
;
1677 coding
->consumed
= src_base
- coding
->source
;
1678 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1682 encode_coding_utf_16 (coding
)
1683 struct coding_system
*coding
;
1685 int multibytep
= coding
->dst_multibyte
;
1686 int *charbuf
= coding
->charbuf
;
1687 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1688 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1689 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1691 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1692 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1693 int produced_chars
= 0;
1694 Lisp_Object attrs
, charset_list
;
1697 CODING_GET_INFO (coding
, attrs
, charset_list
);
1699 if (bom
!= utf_16_without_bom
)
1701 ASSURE_DESTINATION (safe_room
);
1703 EMIT_TWO_BYTES (0xFE, 0xFF);
1705 EMIT_TWO_BYTES (0xFF, 0xFE);
1706 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1709 while (charbuf
< charbuf_end
)
1711 ASSURE_DESTINATION (safe_room
);
1713 if (c
>= MAX_UNICODE_CHAR
)
1714 c
= coding
->default_char
;
1719 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1721 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1728 c1
= (c
>> 10) + 0xD800;
1729 c2
= (c
& 0x3FF) + 0xDC00;
1731 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1733 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1736 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1737 coding
->produced
= dst
- coding
->destination
;
1738 coding
->produced_char
+= produced_chars
;
1743 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1745 /* Emacs' internal format for representation of multiple character
1746 sets is a kind of multi-byte encoding, i.e. characters are
1747 represented by variable-length sequences of one-byte codes.
1749 ASCII characters and control characters (e.g. `tab', `newline') are
1750 represented by one-byte sequences which are their ASCII codes, in
1751 the range 0x00 through 0x7F.
1753 8-bit characters of the range 0x80..0x9F are represented by
1754 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1757 8-bit characters of the range 0xA0..0xFF are represented by
1758 one-byte sequences which are their 8-bit code.
1760 The other characters are represented by a sequence of `base
1761 leading-code', optional `extended leading-code', and one or two
1762 `position-code's. The length of the sequence is determined by the
1763 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1764 whereas extended leading-code and position-code take the range 0xA0
1765 through 0xFF. See `charset.h' for more details about leading-code
1768 --- CODE RANGE of Emacs' internal format ---
1772 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1773 eight-bit-graphic 0xA0..0xBF
1774 ELSE 0x81..0x9D + [0xA0..0xFF]+
1775 ---------------------------------------------
1777 As this is the internal character representation, the format is
1778 usually not used externally (i.e. in a file or in a data sent to a
1779 process). But, it is possible to have a text externally in this
1780 format (i.e. by encoding by the coding system `emacs-mule').
1782 In that case, a sequence of one-byte codes has a slightly different
1785 At first, all characters in eight-bit-control are represented by
1786 one-byte sequences which are their 8-bit code.
1788 Next, character composition data are represented by the byte
1789 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1791 METHOD is 0xF0 plus one of composition method (enum
1792 composition_method),
1794 BYTES is 0xA0 plus a byte length of this composition data,
1796 CHARS is 0x20 plus a number of characters composed by this
1799 COMPONENTs are characters of multibye form or composition
1800 rules encoded by two-byte of ASCII codes.
1802 In addition, for backward compatibility, the following formats are
1803 also recognized as composition data on decoding.
1806 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1809 MSEQ is a multibyte form but in these special format:
1810 ASCII: 0xA0 ASCII_CODE+0x80,
1811 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1812 RULE is a one byte code of the range 0xA0..0xF0 that
1813 represents a composition rule.
1816 char emacs_mule_bytes
[256];
1819 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
)
1820 struct coding_system
*coding
;
1821 const unsigned char *src
;
1822 int *nbytes
, *nchars
, *id
;
1824 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1825 const unsigned char *src_base
= src
;
1826 int multibytep
= coding
->src_multibyte
;
1827 struct charset
*charset
;
1830 int consumed_chars
= 0;
1836 charset
= emacs_mule_charset
[0];
1842 /* Old style component character of a composition. */
1852 switch (emacs_mule_bytes
[c
])
1855 if (! (charset
= emacs_mule_charset
[c
]))
1864 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1865 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1868 if (c
< 0xA0 || ! (charset
= emacs_mule_charset
[c
]))
1877 if (! (charset
= emacs_mule_charset
[c
]))
1882 code
= (c
& 0x7F) << 8;
1892 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
1897 code
= (c
& 0x7F) << 8;
1906 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
1907 ? charset_ascii
: charset_eight_bit
);
1913 c
= DECODE_CHAR (charset
, code
);
1917 *nbytes
= src
- src_base
;
1918 *nchars
= consumed_chars
;
1931 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1932 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1936 detect_coding_emacs_mule (coding
, detect_info
)
1937 struct coding_system
*coding
;
1938 struct coding_detection_info
*detect_info
;
1940 const unsigned char *src
= coding
->source
, *src_base
;
1941 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1942 int multibytep
= coding
->src_multibyte
;
1943 int consumed_chars
= 0;
1947 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1948 /* A coding system of this category is always ASCII compatible. */
1949 src
+= coding
->head_ascii
;
1959 /* Perhaps the start of composite character. We simple skip
1960 it because analyzing it is too heavy for detecting. But,
1961 at least, we check that the composite character
1962 constitutes of more than 4 bytes. */
1963 const unsigned char *src_base
;
1973 if (src
- src_base
<= 4)
1975 found
= CATEGORY_MASK_EMACS_MULE
;
1983 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1988 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
1990 while (more_bytes
> 0)
1995 src
--; /* Unread the last byte. */
2000 if (more_bytes
!= 0)
2002 found
= CATEGORY_MASK_EMACS_MULE
;
2005 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2009 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
2011 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
2014 detect_info
->found
|= found
;
2019 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2021 /* Decode a character represented as a component of composition
2022 sequence of Emacs 20/21 style at SRC. Set C to that character and
2023 update SRC to the head of next character (or an encoded composition
2024 rule). If SRC doesn't points a composition component, set C to -1.
2025 If SRC points an invalid byte sequence, global exit by a return
2028 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
2032 int nbytes, nchars; \
2034 if (src == src_end) \
2036 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
2041 goto invalid_code; \
2045 consumed_chars += nchars; \
2050 /* Decode a composition rule represented as a component of composition
2051 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
2052 and increment BUF. If SRC points an invalid byte sequence, set C
2055 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
2057 int c, gref, nref; \
2059 if (src >= src_end) \
2060 goto invalid_code; \
2061 ONE_MORE_BYTE_NO_CHECK (c); \
2063 if (c < 0 || c >= 81) \
2064 goto invalid_code; \
2066 gref = c / 9, nref = c % 9; \
2067 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
2071 /* Decode a composition rule represented as a component of composition
2072 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
2073 and increment BUF. If SRC points an invalid byte sequence, set C
2076 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
2080 if (src + 1>= src_end) \
2081 goto invalid_code; \
2082 ONE_MORE_BYTE_NO_CHECK (gref); \
2084 ONE_MORE_BYTE_NO_CHECK (nref); \
2086 if (gref < 0 || gref >= 81 \
2087 || nref < 0 || nref >= 81) \
2088 goto invalid_code; \
2089 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
2093 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
2095 /* Emacs 21 style format. The first three bytes at SRC are \
2096 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
2097 the byte length of this composition information, CHARS is the \
2098 number of characters composed by this composition. */ \
2099 enum composition_method method = c - 0xF2; \
2100 int *charbuf_base = charbuf; \
2101 int consumed_chars_limit; \
2102 int nbytes, nchars; \
2104 ONE_MORE_BYTE (c); \
2106 goto invalid_code; \
2107 nbytes = c - 0xA0; \
2109 goto invalid_code; \
2110 ONE_MORE_BYTE (c); \
2112 goto invalid_code; \
2113 nchars = c - 0xA0; \
2114 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2115 consumed_chars_limit = consumed_chars_base + nbytes; \
2116 if (method != COMPOSITION_RELATIVE) \
2119 while (consumed_chars < consumed_chars_limit) \
2121 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
2122 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
2124 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
2127 if (consumed_chars < consumed_chars_limit) \
2128 goto invalid_code; \
2129 charbuf_base[0] -= i; \
2134 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
2136 /* Emacs 20 style format for relative composition. */ \
2137 /* Store multibyte form of characters to be composed. */ \
2138 enum composition_method method = COMPOSITION_RELATIVE; \
2139 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2140 int *buf = components; \
2144 ONE_MORE_BYTE (c); /* skip 0x80 */ \
2145 for (i = 0; *src >= 0xA0 && i < MAX_COMPOSITION_COMPONENTS; i++) \
2146 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2148 goto invalid_code; \
2149 ADD_COMPOSITION_DATA (charbuf, i, method); \
2150 for (j = 0; j < i; j++) \
2151 *charbuf++ = components[j]; \
2155 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
2157 /* Emacs 20 style format for rule-base composition. */ \
2158 /* Store multibyte form of characters to be composed. */ \
2159 enum composition_method method = COMPOSITION_WITH_RULE; \
2160 int *charbuf_base = charbuf; \
2161 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2162 int *buf = components; \
2165 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2166 for (i = 1; i < MAX_COMPOSITION_COMPONENTS; i++) \
2170 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
2171 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2173 if (i <= 1 || (buf - components) % 2 == 0) \
2174 goto invalid_code; \
2175 if (charbuf + i + (i / 2) + 1 >= charbuf_end) \
2176 goto no_more_source; \
2177 ADD_COMPOSITION_DATA (charbuf, i, method); \
2179 for (j = 0; j < i; j++) \
2180 *charbuf++ = components[j]; \
2181 charbuf_base[0] -= i; \
2182 for (j = 0; j < i; j += 2) \
2183 *charbuf++ = components[j]; \
2188 decode_coding_emacs_mule (coding
)
2189 struct coding_system
*coding
;
2191 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2192 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2193 const unsigned char *src_base
;
2194 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2196 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
2197 int consumed_chars
= 0, consumed_chars_base
;
2198 int multibytep
= coding
->src_multibyte
;
2199 Lisp_Object attrs
, charset_list
;
2200 int char_offset
= coding
->produced_char
;
2201 int last_offset
= char_offset
;
2202 int last_id
= charset_ascii
;
2203 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
2204 int byte_after_cr
= -1;
2206 CODING_GET_INFO (coding
, attrs
, charset_list
);
2213 consumed_chars_base
= consumed_chars
;
2215 if (charbuf
>= charbuf_end
)
2218 if (byte_after_cr
>= 0)
2219 c
= byte_after_cr
, byte_after_cr
= -1;
2229 if (eol_crlf
&& c
== '\r')
2230 ONE_MORE_BYTE (byte_after_cr
);
2239 if (c
- 0xF2 >= COMPOSITION_RELATIVE
2240 && c
- 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS
)
2241 DECODE_EMACS_MULE_21_COMPOSITION (c
);
2243 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c
);
2245 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c
);
2249 else if (c
< 0xA0 && emacs_mule_bytes
[c
] > 1)
2255 consumed_chars
= consumed_chars_base
;
2256 c
= emacs_mule_char (coding
, src
, &nbytes
, &nchars
, &id
);
2265 if (last_id
!= charset_ascii
)
2266 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2268 last_offset
= char_offset
;
2272 consumed_chars
+= nchars
;
2281 consumed_chars
= consumed_chars_base
;
2283 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2289 if (last_id
!= charset_ascii
)
2290 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2291 coding
->consumed_char
+= consumed_chars_base
;
2292 coding
->consumed
= src_base
- coding
->source
;
2293 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2297 #define EMACS_MULE_LEADING_CODES(id, codes) \
2300 codes[0] = id, codes[1] = 0; \
2301 else if (id < 0xE0) \
2302 codes[0] = 0x9A, codes[1] = id; \
2303 else if (id < 0xF0) \
2304 codes[0] = 0x9B, codes[1] = id; \
2305 else if (id < 0xF5) \
2306 codes[0] = 0x9C, codes[1] = id; \
2308 codes[0] = 0x9D, codes[1] = id; \
2313 encode_coding_emacs_mule (coding
)
2314 struct coding_system
*coding
;
2316 int multibytep
= coding
->dst_multibyte
;
2317 int *charbuf
= coding
->charbuf
;
2318 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2319 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2320 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2322 int produced_chars
= 0;
2323 Lisp_Object attrs
, charset_list
;
2325 int preferred_charset_id
= -1;
2327 CODING_GET_INFO (coding
, attrs
, charset_list
);
2328 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2330 CODING_ATTR_CHARSET_LIST (attrs
)
2331 = charset_list
= Vemacs_mule_charset_list
;
2334 while (charbuf
< charbuf_end
)
2336 ASSURE_DESTINATION (safe_room
);
2341 /* Handle an annotation. */
2344 case CODING_ANNOTATE_COMPOSITION_MASK
:
2345 /* Not yet implemented. */
2347 case CODING_ANNOTATE_CHARSET_MASK
:
2348 preferred_charset_id
= charbuf
[3];
2349 if (preferred_charset_id
>= 0
2350 && NILP (Fmemq (make_number (preferred_charset_id
),
2352 preferred_charset_id
= -1;
2361 if (ASCII_CHAR_P (c
))
2362 EMIT_ONE_ASCII_BYTE (c
);
2363 else if (CHAR_BYTE8_P (c
))
2365 c
= CHAR_TO_BYTE8 (c
);
2370 struct charset
*charset
;
2374 unsigned char leading_codes
[2];
2376 if (preferred_charset_id
>= 0)
2378 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2379 if (! CHAR_CHARSET_P (c
, charset
))
2380 charset
= char_charset (c
, charset_list
, NULL
);
2383 charset
= char_charset (c
, charset_list
, &code
);
2386 c
= coding
->default_char
;
2387 if (ASCII_CHAR_P (c
))
2389 EMIT_ONE_ASCII_BYTE (c
);
2392 charset
= char_charset (c
, charset_list
, &code
);
2394 dimension
= CHARSET_DIMENSION (charset
);
2395 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2396 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2397 EMIT_ONE_BYTE (leading_codes
[0]);
2398 if (leading_codes
[1])
2399 EMIT_ONE_BYTE (leading_codes
[1]);
2401 EMIT_ONE_BYTE (code
| 0x80);
2405 EMIT_ONE_BYTE (code
>> 8);
2406 EMIT_ONE_BYTE (code
& 0xFF);
2410 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2411 coding
->produced_char
+= produced_chars
;
2412 coding
->produced
= dst
- coding
->destination
;
2417 /*** 7. ISO2022 handlers ***/
2419 /* The following note describes the coding system ISO2022 briefly.
2420 Since the intention of this note is to help understand the
2421 functions in this file, some parts are NOT ACCURATE or are OVERLY
2422 SIMPLIFIED. For thorough understanding, please refer to the
2423 original document of ISO2022. This is equivalent to the standard
2424 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2426 ISO2022 provides many mechanisms to encode several character sets
2427 in 7-bit and 8-bit environments. For 7-bit environments, all text
2428 is encoded using bytes less than 128. This may make the encoded
2429 text a little bit longer, but the text passes more easily through
2430 several types of gateway, some of which strip off the MSB (Most
2433 There are two kinds of character sets: control character sets and
2434 graphic character sets. The former contain control characters such
2435 as `newline' and `escape' to provide control functions (control
2436 functions are also provided by escape sequences). The latter
2437 contain graphic characters such as 'A' and '-'. Emacs recognizes
2438 two control character sets and many graphic character sets.
2440 Graphic character sets are classified into one of the following
2441 four classes, according to the number of bytes (DIMENSION) and
2442 number of characters in one dimension (CHARS) of the set:
2443 - DIMENSION1_CHARS94
2444 - DIMENSION1_CHARS96
2445 - DIMENSION2_CHARS94
2446 - DIMENSION2_CHARS96
2448 In addition, each character set is assigned an identification tag,
2449 unique for each set, called the "final character" (denoted as <F>
2450 hereafter). The <F> of each character set is decided by ECMA(*)
2451 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2452 (0x30..0x3F are for private use only).
2454 Note (*): ECMA = European Computer Manufacturers Association
2456 Here are examples of graphic character sets [NAME(<F>)]:
2457 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2458 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2459 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2460 o DIMENSION2_CHARS96 -- none for the moment
2462 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2463 C0 [0x00..0x1F] -- control character plane 0
2464 GL [0x20..0x7F] -- graphic character plane 0
2465 C1 [0x80..0x9F] -- control character plane 1
2466 GR [0xA0..0xFF] -- graphic character plane 1
2468 A control character set is directly designated and invoked to C0 or
2469 C1 by an escape sequence. The most common case is that:
2470 - ISO646's control character set is designated/invoked to C0, and
2471 - ISO6429's control character set is designated/invoked to C1,
2472 and usually these designations/invocations are omitted in encoded
2473 text. In a 7-bit environment, only C0 can be used, and a control
2474 character for C1 is encoded by an appropriate escape sequence to
2475 fit into the environment. All control characters for C1 are
2476 defined to have corresponding escape sequences.
2478 A graphic character set is at first designated to one of four
2479 graphic registers (G0 through G3), then these graphic registers are
2480 invoked to GL or GR. These designations and invocations can be
2481 done independently. The most common case is that G0 is invoked to
2482 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2483 these invocations and designations are omitted in encoded text.
2484 In a 7-bit environment, only GL can be used.
2486 When a graphic character set of CHARS94 is invoked to GL, codes
2487 0x20 and 0x7F of the GL area work as control characters SPACE and
2488 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2491 There are two ways of invocation: locking-shift and single-shift.
2492 With locking-shift, the invocation lasts until the next different
2493 invocation, whereas with single-shift, the invocation affects the
2494 following character only and doesn't affect the locking-shift
2495 state. Invocations are done by the following control characters or
2498 ----------------------------------------------------------------------
2499 abbrev function cntrl escape seq description
2500 ----------------------------------------------------------------------
2501 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2502 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2503 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2504 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2505 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2506 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2507 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2508 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2509 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2510 ----------------------------------------------------------------------
2511 (*) These are not used by any known coding system.
2513 Control characters for these functions are defined by macros
2514 ISO_CODE_XXX in `coding.h'.
2516 Designations are done by the following escape sequences:
2517 ----------------------------------------------------------------------
2518 escape sequence description
2519 ----------------------------------------------------------------------
2520 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2521 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2522 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2523 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2524 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2525 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2526 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2527 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2528 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2529 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2530 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2531 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2532 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2533 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2534 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2535 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2536 ----------------------------------------------------------------------
2538 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2539 of dimension 1, chars 94, and final character <F>, etc...
2541 Note (*): Although these designations are not allowed in ISO2022,
2542 Emacs accepts them on decoding, and produces them on encoding
2543 CHARS96 character sets in a coding system which is characterized as
2544 7-bit environment, non-locking-shift, and non-single-shift.
2546 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2547 '(' must be omitted. We refer to this as "short-form" hereafter.
2549 Now you may notice that there are a lot of ways of encoding the
2550 same multilingual text in ISO2022. Actually, there exist many
2551 coding systems such as Compound Text (used in X11's inter client
2552 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2553 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2554 localized platforms), and all of these are variants of ISO2022.
2556 In addition to the above, Emacs handles two more kinds of escape
2557 sequences: ISO6429's direction specification and Emacs' private
2558 sequence for specifying character composition.
2560 ISO6429's direction specification takes the following form:
2561 o CSI ']' -- end of the current direction
2562 o CSI '0' ']' -- end of the current direction
2563 o CSI '1' ']' -- start of left-to-right text
2564 o CSI '2' ']' -- start of right-to-left text
2565 The control character CSI (0x9B: control sequence introducer) is
2566 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2568 Character composition specification takes the following form:
2569 o ESC '0' -- start relative composition
2570 o ESC '1' -- end composition
2571 o ESC '2' -- start rule-base composition (*)
2572 o ESC '3' -- start relative composition with alternate chars (**)
2573 o ESC '4' -- start rule-base composition with alternate chars (**)
2574 Since these are not standard escape sequences of any ISO standard,
2575 the use of them with these meanings is restricted to Emacs only.
2577 (*) This form is used only in Emacs 20.7 and older versions,
2578 but newer versions can safely decode it.
2579 (**) This form is used only in Emacs 21.1 and newer versions,
2580 and older versions can't decode it.
2582 Here's a list of example usages of these composition escape
2583 sequences (categorized by `enum composition_method').
2585 COMPOSITION_RELATIVE:
2586 ESC 0 CHAR [ CHAR ] ESC 1
2587 COMPOSITION_WITH_RULE:
2588 ESC 2 CHAR [ RULE CHAR ] ESC 1
2589 COMPOSITION_WITH_ALTCHARS:
2590 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2591 COMPOSITION_WITH_RULE_ALTCHARS:
2592 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2594 enum iso_code_class_type iso_code_class
[256];
2596 #define SAFE_CHARSET_P(coding, id) \
2597 ((id) <= (coding)->max_charset_id \
2598 && (coding)->safe_charsets[id] >= 0)
2601 #define SHIFT_OUT_OK(category) \
2602 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2605 setup_iso_safe_charsets (attrs
)
2608 Lisp_Object charset_list
, safe_charsets
;
2609 Lisp_Object request
;
2610 Lisp_Object reg_usage
;
2613 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2616 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2617 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2618 && ! EQ (charset_list
, Viso_2022_charset_list
))
2620 CODING_ATTR_CHARSET_LIST (attrs
)
2621 = charset_list
= Viso_2022_charset_list
;
2622 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2625 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2629 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2631 int id
= XINT (XCAR (tail
));
2632 if (max_charset_id
< id
)
2633 max_charset_id
= id
;
2636 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
2638 request
= AREF (attrs
, coding_attr_iso_request
);
2639 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2640 reg94
= XINT (XCAR (reg_usage
));
2641 reg96
= XINT (XCDR (reg_usage
));
2643 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2647 struct charset
*charset
;
2650 charset
= CHARSET_FROM_ID (XINT (id
));
2651 reg
= Fcdr (Fassq (id
, request
));
2653 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2654 else if (charset
->iso_chars_96
)
2657 SSET (safe_charsets
, XINT (id
), reg96
);
2662 SSET (safe_charsets
, XINT (id
), reg94
);
2665 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2669 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2670 Check if a text is encoded in one of ISO-2022 based codig systems.
2671 If it is, return 1, else return 0. */
2674 detect_coding_iso_2022 (coding
, detect_info
)
2675 struct coding_system
*coding
;
2676 struct coding_detection_info
*detect_info
;
2678 const unsigned char *src
= coding
->source
, *src_base
= src
;
2679 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2680 int multibytep
= coding
->src_multibyte
;
2681 int single_shifting
= 0;
2684 int consumed_chars
= 0;
2689 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2691 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2693 struct coding_system
*this = &(coding_categories
[i
]);
2694 Lisp_Object attrs
, val
;
2698 attrs
= CODING_ID_ATTRS (this->id
);
2699 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2700 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs
), Viso_2022_charset_list
))
2701 setup_iso_safe_charsets (attrs
);
2702 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2703 this->max_charset_id
= SCHARS (val
) - 1;
2704 this->safe_charsets
= (char *) SDATA (val
);
2707 /* A coding system of this category is always ASCII compatible. */
2708 src
+= coding
->head_ascii
;
2710 while (rejected
!= CATEGORY_MASK_ISO
)
2717 if (inhibit_iso_escape_detection
)
2719 single_shifting
= 0;
2721 if (c
>= '(' && c
<= '/')
2723 /* Designation sequence for a charset of dimension 1. */
2725 if (c1
< ' ' || c1
>= 0x80
2726 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2727 /* Invalid designation sequence. Just ignore. */
2732 /* Designation sequence for a charset of dimension 2. */
2734 if (c
>= '@' && c
<= 'B')
2735 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2736 id
= iso_charset_table
[1][0][c
];
2737 else if (c
>= '(' && c
<= '/')
2740 if (c1
< ' ' || c1
>= 0x80
2741 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2742 /* Invalid designation sequence. Just ignore. */
2746 /* Invalid designation sequence. Just ignore it. */
2749 else if (c
== 'N' || c
== 'O')
2751 /* ESC <Fe> for SS2 or SS3. */
2752 single_shifting
= 1;
2753 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2756 else if (c
>= '0' && c
<= '4')
2758 /* ESC <Fp> for start/end composition. */
2759 found
|= CATEGORY_MASK_ISO
;
2764 /* Invalid escape sequence. Just ignore it. */
2768 /* We found a valid designation sequence for CHARSET. */
2769 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2770 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2772 found
|= CATEGORY_MASK_ISO_7
;
2774 rejected
|= CATEGORY_MASK_ISO_7
;
2775 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
2777 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
2779 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
2780 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
2782 found
|= CATEGORY_MASK_ISO_7_ELSE
;
2784 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
2785 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
2787 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2789 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
2794 /* Locking shift out/in. */
2795 if (inhibit_iso_escape_detection
)
2797 single_shifting
= 0;
2798 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2802 /* Control sequence introducer. */
2803 single_shifting
= 0;
2804 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2805 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2806 goto check_extra_latin
;
2811 if (inhibit_iso_escape_detection
)
2813 single_shifting
= 0;
2814 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2815 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2816 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2817 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2818 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2819 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2820 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2821 if (single_shifting
)
2823 goto check_extra_latin
;
2830 single_shifting
= 0;
2835 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2836 found
|= CATEGORY_MASK_ISO_8_1
;
2837 /* Check the length of succeeding codes of the range
2838 0xA0..0FF. If the byte length is even, we include
2839 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2840 only when we are not single shifting. */
2841 if (! single_shifting
2842 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2845 while (src
< src_end
)
2853 if (i
& 1 && src
< src_end
)
2854 rejected
|= CATEGORY_MASK_ISO_8_2
;
2856 found
|= CATEGORY_MASK_ISO_8_2
;
2861 single_shifting
= 0;
2862 if (! VECTORP (Vlatin_extra_code_table
)
2863 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
2865 rejected
= CATEGORY_MASK_ISO
;
2868 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2869 & CODING_ISO_FLAG_LATIN_EXTRA
)
2870 found
|= CATEGORY_MASK_ISO_8_1
;
2872 rejected
|= CATEGORY_MASK_ISO_8_1
;
2873 rejected
|= CATEGORY_MASK_ISO_8_2
;
2876 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
2880 detect_info
->rejected
|= rejected
;
2881 detect_info
->found
|= (found
& ~rejected
);
2886 /* Set designation state into CODING. Set CHARS_96 to -1 if the
2887 escape sequence should be kept. */
2888 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2892 if (final < '0' || final >= 128 \
2893 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2894 || !SAFE_CHARSET_P (coding, id)) \
2896 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2900 prev = CODING_ISO_DESIGNATION (coding, reg); \
2901 if (id == charset_jisx0201_roman) \
2903 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2904 id = charset_ascii; \
2906 else if (id == charset_jisx0208_1978) \
2908 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2909 id = charset_jisx0208; \
2911 CODING_ISO_DESIGNATION (coding, reg) = id; \
2912 /* If there was an invalid designation to REG previously, and this \
2913 designation is ASCII to REG, we should keep this designation \
2915 if (prev == -2 && id == charset_ascii) \
2920 #define MAYBE_FINISH_COMPOSITION() \
2923 if (composition_state == COMPOSING_NO) \
2925 /* It is assured that we have enough room for producing \
2926 characters stored in the table `components'. */ \
2927 if (charbuf + component_idx > charbuf_end) \
2928 goto no_more_source; \
2929 composition_state = COMPOSING_NO; \
2930 if (method == COMPOSITION_RELATIVE \
2931 || method == COMPOSITION_WITH_ALTCHARS) \
2933 for (i = 0; i < component_idx; i++) \
2934 *charbuf++ = components[i]; \
2935 char_offset += component_idx; \
2939 for (i = 0; i < component_idx; i += 2) \
2940 *charbuf++ = components[i]; \
2941 char_offset += (component_idx / 2) + 1; \
2946 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2947 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2948 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2949 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2950 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2953 #define DECODE_COMPOSITION_START(c1) \
2956 && composition_state == COMPOSING_COMPONENT_RULE) \
2958 component_len = component_idx; \
2959 composition_state = COMPOSING_CHAR; \
2963 const unsigned char *p; \
2965 MAYBE_FINISH_COMPOSITION (); \
2966 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2967 goto no_more_source; \
2968 for (p = src; p < src_end - 1; p++) \
2969 if (*p == ISO_CODE_ESC && p[1] == '1') \
2971 if (p == src_end - 1) \
2973 /* The current composition doesn't end in the current \
2975 record_conversion_result \
2976 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
2977 goto no_more_source; \
2980 /* This is surely the start of a composition. */ \
2981 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2982 : c1 == '2' ? COMPOSITION_WITH_RULE \
2983 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2984 : COMPOSITION_WITH_RULE_ALTCHARS); \
2985 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2986 : COMPOSING_COMPONENT_CHAR); \
2987 component_idx = component_len = 0; \
2992 /* Handle compositoin end sequence ESC 1. */
2994 #define DECODE_COMPOSITION_END() \
2996 int nchars = (component_len > 0 ? component_idx - component_len \
2997 : method == COMPOSITION_RELATIVE ? component_idx \
2998 : (component_idx + 1) / 2); \
3000 int *saved_charbuf = charbuf; \
3002 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
3003 if (method != COMPOSITION_RELATIVE) \
3005 if (component_len == 0) \
3006 for (i = 0; i < component_idx; i++) \
3007 *charbuf++ = components[i]; \
3009 for (i = 0; i < component_len; i++) \
3010 *charbuf++ = components[i]; \
3011 *saved_charbuf = saved_charbuf - charbuf; \
3013 if (method == COMPOSITION_WITH_RULE) \
3014 for (i = 0; i < component_idx; i += 2, char_offset++) \
3015 *charbuf++ = components[i]; \
3017 for (i = component_len; i < component_idx; i++, char_offset++) \
3018 *charbuf++ = components[i]; \
3019 coding->annotated = 1; \
3020 composition_state = COMPOSING_NO; \
3024 /* Decode a composition rule from the byte C1 (and maybe one more byte
3025 from SRC) and store one encoded composition rule in
3026 coding->cmp_data. */
3028 #define DECODE_COMPOSITION_RULE(c1) \
3031 if (c1 < 81) /* old format (before ver.21) */ \
3033 int gref = (c1) / 9; \
3034 int nref = (c1) % 9; \
3035 if (gref == 4) gref = 10; \
3036 if (nref == 4) nref = 10; \
3037 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
3039 else if (c1 < 93) /* new format (after ver.21) */ \
3041 ONE_MORE_BYTE (c2); \
3042 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
3049 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3052 decode_coding_iso_2022 (coding
)
3053 struct coding_system
*coding
;
3055 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3056 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3057 const unsigned char *src_base
;
3058 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3060 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
3061 int consumed_chars
= 0, consumed_chars_base
;
3062 int multibytep
= coding
->src_multibyte
;
3063 /* Charsets invoked to graphic plane 0 and 1 respectively. */
3064 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3065 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3066 int charset_id_2
, charset_id_3
;
3067 struct charset
*charset
;
3069 /* For handling composition sequence. */
3070 #define COMPOSING_NO 0
3071 #define COMPOSING_CHAR 1
3072 #define COMPOSING_RULE 2
3073 #define COMPOSING_COMPONENT_CHAR 3
3074 #define COMPOSING_COMPONENT_RULE 4
3076 int composition_state
= COMPOSING_NO
;
3077 enum composition_method method
;
3078 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
3081 Lisp_Object attrs
, charset_list
;
3082 int char_offset
= coding
->produced_char
;
3083 int last_offset
= char_offset
;
3084 int last_id
= charset_ascii
;
3085 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
3086 int byte_after_cr
= -1;
3088 CODING_GET_INFO (coding
, attrs
, charset_list
);
3089 setup_iso_safe_charsets (attrs
);
3090 /* Charset list may have been changed. */
3091 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3092 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3099 consumed_chars_base
= consumed_chars
;
3101 if (charbuf
>= charbuf_end
)
3104 if (byte_after_cr
>= 0)
3105 c1
= byte_after_cr
, byte_after_cr
= -1;
3111 /* We produce at most one character. */
3112 switch (iso_code_class
[c1
])
3114 case ISO_0x20_or_0x7F
:
3115 if (composition_state
!= COMPOSING_NO
)
3117 if (composition_state
== COMPOSING_RULE
3118 || composition_state
== COMPOSING_COMPONENT_RULE
)
3120 DECODE_COMPOSITION_RULE (c1
);
3121 components
[component_idx
++] = c1
;
3122 composition_state
--;
3126 if (charset_id_0
< 0
3127 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
3128 /* This is SPACE or DEL. */
3129 charset
= CHARSET_FROM_ID (charset_ascii
);
3131 charset
= CHARSET_FROM_ID (charset_id_0
);
3134 case ISO_graphic_plane_0
:
3135 if (composition_state
!= COMPOSING_NO
)
3137 if (composition_state
== COMPOSING_RULE
3138 || composition_state
== COMPOSING_COMPONENT_RULE
)
3140 DECODE_COMPOSITION_RULE (c1
);
3141 components
[component_idx
++] = c1
;
3142 composition_state
--;
3146 if (charset_id_0
< 0)
3147 charset
= CHARSET_FROM_ID (charset_ascii
);
3149 charset
= CHARSET_FROM_ID (charset_id_0
);
3152 case ISO_0xA0_or_0xFF
:
3153 if (charset_id_1
< 0
3154 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
3155 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
3157 /* This is a graphic character, we fall down ... */
3159 case ISO_graphic_plane_1
:
3160 if (charset_id_1
< 0)
3162 charset
= CHARSET_FROM_ID (charset_id_1
);
3166 if (eol_crlf
&& c1
== '\r')
3167 ONE_MORE_BYTE (byte_after_cr
);
3168 MAYBE_FINISH_COMPOSITION ();
3169 charset
= CHARSET_FROM_ID (charset_ascii
);
3173 MAYBE_FINISH_COMPOSITION ();
3177 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3178 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
3180 CODING_ISO_INVOCATION (coding
, 0) = 1;
3181 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3185 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
3187 CODING_ISO_INVOCATION (coding
, 0) = 0;
3188 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3191 case ISO_single_shift_2_7
:
3192 case ISO_single_shift_2
:
3193 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3195 /* SS2 is handled as an escape sequence of ESC 'N' */
3197 goto label_escape_sequence
;
3199 case ISO_single_shift_3
:
3200 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
3202 /* SS2 is handled as an escape sequence of ESC 'O' */
3204 goto label_escape_sequence
;
3206 case ISO_control_sequence_introducer
:
3207 /* CSI is handled as an escape sequence of ESC '[' ... */
3209 goto label_escape_sequence
;
3213 label_escape_sequence
:
3214 /* Escape sequences handled here are invocation,
3215 designation, direction specification, and character
3216 composition specification. */
3219 case '&': /* revision of following character set */
3221 if (!(c1
>= '@' && c1
<= '~'))
3224 if (c1
!= ISO_CODE_ESC
)
3227 goto label_escape_sequence
;
3229 case '$': /* designation of 2-byte character set */
3230 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3236 if (c1
>= '@' && c1
<= 'B')
3237 { /* designation of JISX0208.1978, GB2312.1980,
3239 reg
= 0, chars96
= 0;
3241 else if (c1
>= 0x28 && c1
<= 0x2B)
3242 { /* designation of DIMENSION2_CHARS94 character set */
3243 reg
= c1
- 0x28, chars96
= 0;
3246 else if (c1
>= 0x2C && c1
<= 0x2F)
3247 { /* designation of DIMENSION2_CHARS96 character set */
3248 reg
= c1
- 0x2C, chars96
= 1;
3253 DECODE_DESIGNATION (reg
, 2, chars96
, c1
);
3254 /* We must update these variables now. */
3256 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3258 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3264 case 'n': /* invocation of locking-shift-2 */
3265 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3266 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3268 CODING_ISO_INVOCATION (coding
, 0) = 2;
3269 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3272 case 'o': /* invocation of locking-shift-3 */
3273 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3274 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3276 CODING_ISO_INVOCATION (coding
, 0) = 3;
3277 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3280 case 'N': /* invocation of single-shift-2 */
3281 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3282 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3284 charset_id_2
= CODING_ISO_DESIGNATION (coding
, 2);
3285 if (charset_id_2
< 0)
3286 charset
= CHARSET_FROM_ID (charset_ascii
);
3288 charset
= CHARSET_FROM_ID (charset_id_2
);
3290 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3294 case 'O': /* invocation of single-shift-3 */
3295 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3296 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3298 charset_id_3
= CODING_ISO_DESIGNATION (coding
, 3);
3299 if (charset_id_3
< 0)
3300 charset
= CHARSET_FROM_ID (charset_ascii
);
3302 charset
= CHARSET_FROM_ID (charset_id_3
);
3304 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3308 case '0': case '2': case '3': case '4': /* start composition */
3309 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3311 DECODE_COMPOSITION_START (c1
);
3314 case '1': /* end composition */
3315 if (composition_state
== COMPOSING_NO
)
3317 DECODE_COMPOSITION_END ();
3320 case '[': /* specification of direction */
3321 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3323 /* For the moment, nested direction is not supported.
3324 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3325 left-to-right, and nozero means right-to-left. */
3329 case ']': /* end of the current direction */
3330 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3332 case '0': /* end of the current direction */
3333 case '1': /* start of left-to-right direction */
3336 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3341 case '2': /* start of right-to-left direction */
3344 coding
->mode
|= CODING_MODE_DIRECTION
;
3358 /* CTEXT extended segment:
3359 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3360 We keep these bytes as is for the moment.
3361 They may be decoded by post-read-conversion. */
3365 ONE_MORE_BYTE (dim
);
3368 size
= ((M
- 128) * 128) + (L
- 128);
3369 if (charbuf
+ 8 + size
> charbuf_end
)
3371 *charbuf
++ = ISO_CODE_ESC
;
3375 *charbuf
++ = BYTE8_TO_CHAR (M
);
3376 *charbuf
++ = BYTE8_TO_CHAR (L
);
3380 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3385 /* XFree86 extension for embedding UTF-8 in CTEXT:
3386 ESC % G --UTF-8-BYTES-- ESC % @
3387 We keep these bytes as is for the moment.
3388 They may be decoded by post-read-conversion. */
3391 if (p
+ 6 > charbuf_end
)
3393 *p
++ = ISO_CODE_ESC
;
3396 while (p
< charbuf_end
)
3399 if (c1
== ISO_CODE_ESC
3400 && src
+ 1 < src_end
3407 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3409 if (p
+ 3 > charbuf_end
)
3411 *p
++ = ISO_CODE_ESC
;
3422 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3427 if (c1
>= 0x28 && c1
<= 0x2B)
3428 { /* designation of DIMENSION1_CHARS94 character set */
3429 reg
= c1
- 0x28, chars96
= 0;
3432 else if (c1
>= 0x2C && c1
<= 0x2F)
3433 { /* designation of DIMENSION1_CHARS96 character set */
3434 reg
= c1
- 0x2C, chars96
= 1;
3439 DECODE_DESIGNATION (reg
, 1, chars96
, c1
);
3440 /* We must update these variables now. */
3442 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3444 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3452 if (charset
->id
!= charset_ascii
3453 && last_id
!= charset
->id
)
3455 if (last_id
!= charset_ascii
)
3456 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3457 last_id
= charset
->id
;
3458 last_offset
= char_offset
;
3461 /* Now we know CHARSET and 1st position code C1 of a character.
3462 Produce a decoded character while getting 2nd position code
3465 if (CHARSET_DIMENSION (charset
) > 1)
3468 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3469 /* C2 is not in a valid range. */
3471 c1
= (c1
<< 8) | (c2
& 0x7F);
3472 if (CHARSET_DIMENSION (charset
) > 2)
3475 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3476 /* C2 is not in a valid range. */
3478 c1
= (c1
<< 8) | (c2
& 0x7F);
3482 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3485 MAYBE_FINISH_COMPOSITION ();
3486 for (; src_base
< src
; src_base
++, char_offset
++)
3488 if (ASCII_BYTE_P (*src_base
))
3489 *charbuf
++ = *src_base
;
3491 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3494 else if (composition_state
== COMPOSING_NO
)
3501 components
[component_idx
++] = c
;
3502 if (method
== COMPOSITION_WITH_RULE
3503 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3504 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3505 composition_state
++;
3510 MAYBE_FINISH_COMPOSITION ();
3512 consumed_chars
= consumed_chars_base
;
3514 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3524 if (last_id
!= charset_ascii
)
3525 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3526 coding
->consumed_char
+= consumed_chars_base
;
3527 coding
->consumed
= src_base
- coding
->source
;
3528 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3532 /* ISO2022 encoding stuff. */
3535 It is not enough to say just "ISO2022" on encoding, we have to
3536 specify more details. In Emacs, each coding system of ISO2022
3537 variant has the following specifications:
3538 1. Initial designation to G0 thru G3.
3539 2. Allows short-form designation?
3540 3. ASCII should be designated to G0 before control characters?
3541 4. ASCII should be designated to G0 at end of line?
3542 5. 7-bit environment or 8-bit environment?
3543 6. Use locking-shift?
3544 7. Use Single-shift?
3545 And the following two are only for Japanese:
3546 8. Use ASCII in place of JIS0201-1976-Roman?
3547 9. Use JISX0208-1983 in place of JISX0208-1978?
3548 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3549 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3553 /* Produce codes (escape sequence) for designating CHARSET to graphic
3554 register REG at DST, and increment DST. If <final-char> of CHARSET is
3555 '@', 'A', or 'B' and the coding system CODING allows, produce
3556 designation sequence of short-form. */
3558 #define ENCODE_DESIGNATION(charset, reg, coding) \
3560 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3561 char *intermediate_char_94 = "()*+"; \
3562 char *intermediate_char_96 = ",-./"; \
3563 int revision = -1; \
3566 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3567 revision = CHARSET_ISO_REVISION (charset); \
3569 if (revision >= 0) \
3571 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3572 EMIT_ONE_BYTE ('@' + revision); \
3574 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3575 if (CHARSET_DIMENSION (charset) == 1) \
3577 if (! CHARSET_ISO_CHARS_96 (charset)) \
3578 c = intermediate_char_94[reg]; \
3580 c = intermediate_char_96[reg]; \
3581 EMIT_ONE_ASCII_BYTE (c); \
3585 EMIT_ONE_ASCII_BYTE ('$'); \
3586 if (! CHARSET_ISO_CHARS_96 (charset)) \
3588 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3590 || final_char < '@' || final_char > 'B') \
3591 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3594 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3596 EMIT_ONE_ASCII_BYTE (final_char); \
3598 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3602 /* The following two macros produce codes (control character or escape
3603 sequence) for ISO2022 single-shift functions (single-shift-2 and
3606 #define ENCODE_SINGLE_SHIFT_2 \
3608 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3609 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3611 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3612 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3616 #define ENCODE_SINGLE_SHIFT_3 \
3618 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3619 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3621 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3622 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3626 /* The following four macros produce codes (control character or
3627 escape sequence) for ISO2022 locking-shift functions (shift-in,
3628 shift-out, locking-shift-2, and locking-shift-3). */
3630 #define ENCODE_SHIFT_IN \
3632 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3633 CODING_ISO_INVOCATION (coding, 0) = 0; \
3637 #define ENCODE_SHIFT_OUT \
3639 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3640 CODING_ISO_INVOCATION (coding, 0) = 1; \
3644 #define ENCODE_LOCKING_SHIFT_2 \
3646 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3647 CODING_ISO_INVOCATION (coding, 0) = 2; \
3651 #define ENCODE_LOCKING_SHIFT_3 \
3653 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3654 CODING_ISO_INVOCATION (coding, 0) = 3; \
3658 /* Produce codes for a DIMENSION1 character whose character set is
3659 CHARSET and whose position-code is C1. Designation and invocation
3660 sequences are also produced in advance if necessary. */
3662 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3664 int id = CHARSET_ID (charset); \
3666 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3667 && id == charset_ascii) \
3669 id = charset_jisx0201_roman; \
3670 charset = CHARSET_FROM_ID (id); \
3673 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3675 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3676 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3678 EMIT_ONE_BYTE (c1 | 0x80); \
3679 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3682 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3684 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3687 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3689 EMIT_ONE_BYTE (c1 | 0x80); \
3693 /* Since CHARSET is not yet invoked to any graphic planes, we \
3694 must invoke it, or, at first, designate it to some graphic \
3695 register. Then repeat the loop to actually produce the \
3697 dst = encode_invocation_designation (charset, coding, dst, \
3702 /* Produce codes for a DIMENSION2 character whose character set is
3703 CHARSET and whose position-codes are C1 and C2. Designation and
3704 invocation codes are also produced in advance if necessary. */
3706 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3708 int id = CHARSET_ID (charset); \
3710 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3711 && id == charset_jisx0208) \
3713 id = charset_jisx0208_1978; \
3714 charset = CHARSET_FROM_ID (id); \
3717 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3719 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3720 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3722 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3723 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3726 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3728 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3731 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3733 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3737 /* Since CHARSET is not yet invoked to any graphic planes, we \
3738 must invoke it, or, at first, designate it to some graphic \
3739 register. Then repeat the loop to actually produce the \
3741 dst = encode_invocation_designation (charset, coding, dst, \
3746 #define ENCODE_ISO_CHARACTER(charset, c) \
3748 int code = ENCODE_CHAR ((charset),(c)); \
3750 if (CHARSET_DIMENSION (charset) == 1) \
3751 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3753 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3757 /* Produce designation and invocation codes at a place pointed by DST
3758 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3762 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3763 struct charset
*charset
;
3764 struct coding_system
*coding
;
3768 int multibytep
= coding
->dst_multibyte
;
3769 int produced_chars
= *p_nchars
;
3770 int reg
; /* graphic register number */
3771 int id
= CHARSET_ID (charset
);
3773 /* At first, check designations. */
3774 for (reg
= 0; reg
< 4; reg
++)
3775 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3780 /* CHARSET is not yet designated to any graphic registers. */
3781 /* At first check the requested designation. */
3782 reg
= CODING_ISO_REQUEST (coding
, id
);
3784 /* Since CHARSET requests no special designation, designate it
3785 to graphic register 0. */
3788 ENCODE_DESIGNATION (charset
, reg
, coding
);
3791 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3792 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3794 /* Since the graphic register REG is not invoked to any graphic
3795 planes, invoke it to graphic plane 0. */
3798 case 0: /* graphic register 0 */
3802 case 1: /* graphic register 1 */
3806 case 2: /* graphic register 2 */
3807 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3808 ENCODE_SINGLE_SHIFT_2
;
3810 ENCODE_LOCKING_SHIFT_2
;
3813 case 3: /* graphic register 3 */
3814 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3815 ENCODE_SINGLE_SHIFT_3
;
3817 ENCODE_LOCKING_SHIFT_3
;
3822 *p_nchars
= produced_chars
;
3826 /* The following three macros produce codes for indicating direction
3828 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3830 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3831 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3833 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3837 #define ENCODE_DIRECTION_R2L() \
3839 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3840 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3844 #define ENCODE_DIRECTION_L2R() \
3846 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3847 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3851 /* Produce codes for designation and invocation to reset the graphic
3852 planes and registers to initial state. */
3853 #define ENCODE_RESET_PLANE_AND_REGISTER() \
3856 struct charset *charset; \
3858 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3860 for (reg = 0; reg < 4; reg++) \
3861 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3862 && (CODING_ISO_DESIGNATION (coding, reg) \
3863 != CODING_ISO_INITIAL (coding, reg))) \
3865 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3866 ENCODE_DESIGNATION (charset, reg, coding); \
3871 /* Produce designation sequences of charsets in the line started from
3872 SRC to a place pointed by DST, and return updated DST.
3874 If the current block ends before any end-of-line, we may fail to
3875 find all the necessary designations. */
3877 static unsigned char *
3878 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
3879 struct coding_system
*coding
;
3880 int *charbuf
, *charbuf_end
;
3883 struct charset
*charset
;
3884 /* Table of charsets to be designated to each graphic register. */
3886 int c
, found
= 0, reg
;
3887 int produced_chars
= 0;
3888 int multibytep
= coding
->dst_multibyte
;
3890 Lisp_Object charset_list
;
3892 attrs
= CODING_ID_ATTRS (coding
->id
);
3893 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3894 if (EQ (charset_list
, Qiso_2022
))
3895 charset_list
= Viso_2022_charset_list
;
3897 for (reg
= 0; reg
< 4; reg
++)
3907 charset
= char_charset (c
, charset_list
, NULL
);
3908 id
= CHARSET_ID (charset
);
3909 reg
= CODING_ISO_REQUEST (coding
, id
);
3910 if (reg
>= 0 && r
[reg
] < 0)
3919 for (reg
= 0; reg
< 4; reg
++)
3921 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
3922 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
3928 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3931 encode_coding_iso_2022 (coding
)
3932 struct coding_system
*coding
;
3934 int multibytep
= coding
->dst_multibyte
;
3935 int *charbuf
= coding
->charbuf
;
3936 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
3937 unsigned char *dst
= coding
->destination
+ coding
->produced
;
3938 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
3941 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3942 && CODING_ISO_BOL (coding
));
3943 int produced_chars
= 0;
3944 Lisp_Object attrs
, eol_type
, charset_list
;
3945 int ascii_compatible
;
3947 int preferred_charset_id
= -1;
3949 CODING_GET_INFO (coding
, attrs
, charset_list
);
3950 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
3951 if (VECTORP (eol_type
))
3954 setup_iso_safe_charsets (attrs
);
3955 /* Charset list may have been changed. */
3956 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3957 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3959 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
3961 while (charbuf
< charbuf_end
)
3963 ASSURE_DESTINATION (safe_room
);
3965 if (bol_designation
)
3967 unsigned char *dst_prev
= dst
;
3969 /* We have to produce designation sequences if any now. */
3970 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
3971 bol_designation
= 0;
3972 /* We are sure that designation sequences are all ASCII bytes. */
3973 produced_chars
+= dst
- dst_prev
;
3980 /* Handle an annotation. */
3983 case CODING_ANNOTATE_COMPOSITION_MASK
:
3984 /* Not yet implemented. */
3986 case CODING_ANNOTATE_CHARSET_MASK
:
3987 preferred_charset_id
= charbuf
[2];
3988 if (preferred_charset_id
>= 0
3989 && NILP (Fmemq (make_number (preferred_charset_id
),
3991 preferred_charset_id
= -1;
4000 /* Now encode the character C. */
4001 if (c
< 0x20 || c
== 0x7F)
4004 || (c
== '\r' && EQ (eol_type
, Qmac
)))
4006 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4007 ENCODE_RESET_PLANE_AND_REGISTER ();
4008 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
4012 for (i
= 0; i
< 4; i
++)
4013 CODING_ISO_DESIGNATION (coding
, i
)
4014 = CODING_ISO_INITIAL (coding
, i
);
4017 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
4019 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
4020 ENCODE_RESET_PLANE_AND_REGISTER ();
4021 EMIT_ONE_ASCII_BYTE (c
);
4023 else if (ASCII_CHAR_P (c
))
4025 if (ascii_compatible
)
4026 EMIT_ONE_ASCII_BYTE (c
);
4029 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
4030 ENCODE_ISO_CHARACTER (charset
, c
);
4033 else if (CHAR_BYTE8_P (c
))
4035 c
= CHAR_TO_BYTE8 (c
);
4040 struct charset
*charset
;
4042 if (preferred_charset_id
>= 0)
4044 charset
= CHARSET_FROM_ID (preferred_charset_id
);
4045 if (! CHAR_CHARSET_P (c
, charset
))
4046 charset
= char_charset (c
, charset_list
, NULL
);
4049 charset
= char_charset (c
, charset_list
, NULL
);
4052 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4054 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4055 charset
= CHARSET_FROM_ID (charset_ascii
);
4059 c
= coding
->default_char
;
4060 charset
= char_charset (c
, charset_list
, NULL
);
4063 ENCODE_ISO_CHARACTER (charset
, c
);
4067 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4068 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
4070 ASSURE_DESTINATION (safe_room
);
4071 ENCODE_RESET_PLANE_AND_REGISTER ();
4073 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4074 CODING_ISO_BOL (coding
) = bol_designation
;
4075 coding
->produced_char
+= produced_chars
;
4076 coding
->produced
= dst
- coding
->destination
;
4081 /*** 8,9. SJIS and BIG5 handlers ***/
4083 /* Although SJIS and BIG5 are not ISO's coding system, they are used
4084 quite widely. So, for the moment, Emacs supports them in the bare
4085 C code. But, in the future, they may be supported only by CCL. */
4087 /* SJIS is a coding system encoding three character sets: ASCII, right
4088 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
4089 as is. A character of charset katakana-jisx0201 is encoded by
4090 "position-code + 0x80". A character of charset japanese-jisx0208
4091 is encoded in 2-byte but two position-codes are divided and shifted
4092 so that it fit in the range below.
4094 --- CODE RANGE of SJIS ---
4095 (character set) (range)
4097 KATAKANA-JISX0201 0xA0 .. 0xDF
4098 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
4099 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4100 -------------------------------
4104 /* BIG5 is a coding system encoding two character sets: ASCII and
4105 Big5. An ASCII character is encoded as is. Big5 is a two-byte
4106 character set and is encoded in two-byte.
4108 --- CODE RANGE of BIG5 ---
4109 (character set) (range)
4111 Big5 (1st byte) 0xA1 .. 0xFE
4112 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
4113 --------------------------
4117 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4118 Check if a text is encoded in SJIS. If it is, return
4119 CATEGORY_MASK_SJIS, else return 0. */
4122 detect_coding_sjis (coding
, detect_info
)
4123 struct coding_system
*coding
;
4124 struct coding_detection_info
*detect_info
;
4126 const unsigned char *src
= coding
->source
, *src_base
;
4127 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4128 int multibytep
= coding
->src_multibyte
;
4129 int consumed_chars
= 0;
4133 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
4134 /* A coding system of this category is always ASCII compatible. */
4135 src
+= coding
->head_ascii
;
4143 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
4146 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
4148 found
= CATEGORY_MASK_SJIS
;
4150 else if (c
>= 0xA0 && c
< 0xE0)
4151 found
= CATEGORY_MASK_SJIS
;
4155 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4159 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4161 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
4164 detect_info
->found
|= found
;
4168 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4169 Check if a text is encoded in BIG5. If it is, return
4170 CATEGORY_MASK_BIG5, else return 0. */
4173 detect_coding_big5 (coding
, detect_info
)
4174 struct coding_system
*coding
;
4175 struct coding_detection_info
*detect_info
;
4177 const unsigned char *src
= coding
->source
, *src_base
;
4178 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4179 int multibytep
= coding
->src_multibyte
;
4180 int consumed_chars
= 0;
4184 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
4185 /* A coding system of this category is always ASCII compatible. */
4186 src
+= coding
->head_ascii
;
4197 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
4199 found
= CATEGORY_MASK_BIG5
;
4204 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4208 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4210 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
4213 detect_info
->found
|= found
;
4217 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4218 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
4221 decode_coding_sjis (coding
)
4222 struct coding_system
*coding
;
4224 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4225 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4226 const unsigned char *src_base
;
4227 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4229 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4230 int consumed_chars
= 0, consumed_chars_base
;
4231 int multibytep
= coding
->src_multibyte
;
4232 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4233 struct charset
*charset_kanji2
;
4234 Lisp_Object attrs
, charset_list
, val
;
4235 int char_offset
= coding
->produced_char
;
4236 int last_offset
= char_offset
;
4237 int last_id
= charset_ascii
;
4238 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4239 int byte_after_cr
= -1;
4241 CODING_GET_INFO (coding
, attrs
, charset_list
);
4244 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4245 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4246 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4247 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4252 struct charset
*charset
;
4255 consumed_chars_base
= consumed_chars
;
4257 if (charbuf
>= charbuf_end
)
4260 if (byte_after_cr
>= 0)
4261 c
= byte_after_cr
, byte_after_cr
= -1;
4268 if (eol_crlf
&& c
== '\r')
4269 ONE_MORE_BYTE (byte_after_cr
);
4270 charset
= charset_roman
;
4272 else if (c
== 0x80 || c
== 0xA0)
4274 else if (c
>= 0xA1 && c
<= 0xDF)
4276 /* SJIS -> JISX0201-Kana */
4278 charset
= charset_kana
;
4282 /* SJIS -> JISX0208 */
4284 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4288 charset
= charset_kanji
;
4290 else if (c
<= 0xFC && charset_kanji2
)
4292 /* SJIS -> JISX0213-2 */
4294 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4298 charset
= charset_kanji2
;
4302 if (charset
->id
!= charset_ascii
4303 && last_id
!= charset
->id
)
4305 if (last_id
!= charset_ascii
)
4306 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4307 last_id
= charset
->id
;
4308 last_offset
= char_offset
;
4310 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4317 consumed_chars
= consumed_chars_base
;
4319 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4325 if (last_id
!= charset_ascii
)
4326 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4327 coding
->consumed_char
+= consumed_chars_base
;
4328 coding
->consumed
= src_base
- coding
->source
;
4329 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4333 decode_coding_big5 (coding
)
4334 struct coding_system
*coding
;
4336 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4337 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4338 const unsigned char *src_base
;
4339 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4341 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4342 int consumed_chars
= 0, consumed_chars_base
;
4343 int multibytep
= coding
->src_multibyte
;
4344 struct charset
*charset_roman
, *charset_big5
;
4345 Lisp_Object attrs
, charset_list
, val
;
4346 int char_offset
= coding
->produced_char
;
4347 int last_offset
= char_offset
;
4348 int last_id
= charset_ascii
;
4349 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4350 int byte_after_cr
= -1;
4352 CODING_GET_INFO (coding
, attrs
, charset_list
);
4354 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4355 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4360 struct charset
*charset
;
4363 consumed_chars_base
= consumed_chars
;
4365 if (charbuf
>= charbuf_end
)
4368 if (byte_after_cr
>= 0)
4369 c
= byte_after_cr
, byte_after_cr
= -1;
4377 if (eol_crlf
&& c
== '\r')
4378 ONE_MORE_BYTE (byte_after_cr
);
4379 charset
= charset_roman
;
4384 if (c
< 0xA1 || c
> 0xFE)
4387 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4390 charset
= charset_big5
;
4392 if (charset
->id
!= charset_ascii
4393 && last_id
!= charset
->id
)
4395 if (last_id
!= charset_ascii
)
4396 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4397 last_id
= charset
->id
;
4398 last_offset
= char_offset
;
4400 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4407 consumed_chars
= consumed_chars_base
;
4409 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4415 if (last_id
!= charset_ascii
)
4416 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4417 coding
->consumed_char
+= consumed_chars_base
;
4418 coding
->consumed
= src_base
- coding
->source
;
4419 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4422 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4423 This function can encode charsets `ascii', `katakana-jisx0201',
4424 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4425 are sure that all these charsets are registered as official charset
4426 (i.e. do not have extended leading-codes). Characters of other
4427 charsets are produced without any encoding. If SJIS_P is 1, encode
4428 SJIS text, else encode BIG5 text. */
4431 encode_coding_sjis (coding
)
4432 struct coding_system
*coding
;
4434 int multibytep
= coding
->dst_multibyte
;
4435 int *charbuf
= coding
->charbuf
;
4436 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4437 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4438 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4440 int produced_chars
= 0;
4441 Lisp_Object attrs
, charset_list
, val
;
4442 int ascii_compatible
;
4443 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4444 struct charset
*charset_kanji2
;
4447 CODING_GET_INFO (coding
, attrs
, charset_list
);
4449 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4450 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4451 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4452 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4454 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4456 while (charbuf
< charbuf_end
)
4458 ASSURE_DESTINATION (safe_room
);
4460 /* Now encode the character C. */
4461 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4462 EMIT_ONE_ASCII_BYTE (c
);
4463 else if (CHAR_BYTE8_P (c
))
4465 c
= CHAR_TO_BYTE8 (c
);
4471 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4475 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4477 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4478 charset
= CHARSET_FROM_ID (charset_ascii
);
4482 c
= coding
->default_char
;
4483 charset
= char_charset (c
, charset_list
, &code
);
4486 if (code
== CHARSET_INVALID_CODE (charset
))
4488 if (charset
== charset_kanji
)
4492 c1
= code
>> 8, c2
= code
& 0xFF;
4493 EMIT_TWO_BYTES (c1
, c2
);
4495 else if (charset
== charset_kana
)
4496 EMIT_ONE_BYTE (code
| 0x80);
4497 else if (charset_kanji2
&& charset
== charset_kanji2
)
4502 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4503 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4505 JIS_TO_SJIS2 (code
);
4506 c1
= code
>> 8, c2
= code
& 0xFF;
4507 EMIT_TWO_BYTES (c1
, c2
);
4510 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4513 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4516 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4517 coding
->produced_char
+= produced_chars
;
4518 coding
->produced
= dst
- coding
->destination
;
4523 encode_coding_big5 (coding
)
4524 struct coding_system
*coding
;
4526 int multibytep
= coding
->dst_multibyte
;
4527 int *charbuf
= coding
->charbuf
;
4528 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4529 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4530 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4532 int produced_chars
= 0;
4533 Lisp_Object attrs
, charset_list
, val
;
4534 int ascii_compatible
;
4535 struct charset
*charset_roman
, *charset_big5
;
4538 CODING_GET_INFO (coding
, attrs
, charset_list
);
4540 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4541 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4542 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4544 while (charbuf
< charbuf_end
)
4546 ASSURE_DESTINATION (safe_room
);
4548 /* Now encode the character C. */
4549 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4550 EMIT_ONE_ASCII_BYTE (c
);
4551 else if (CHAR_BYTE8_P (c
))
4553 c
= CHAR_TO_BYTE8 (c
);
4559 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4563 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4565 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4566 charset
= CHARSET_FROM_ID (charset_ascii
);
4570 c
= coding
->default_char
;
4571 charset
= char_charset (c
, charset_list
, &code
);
4574 if (code
== CHARSET_INVALID_CODE (charset
))
4576 if (charset
== charset_big5
)
4580 c1
= code
>> 8, c2
= code
& 0xFF;
4581 EMIT_TWO_BYTES (c1
, c2
);
4584 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4587 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4588 coding
->produced_char
+= produced_chars
;
4589 coding
->produced
= dst
- coding
->destination
;
4594 /*** 10. CCL handlers ***/
4596 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4597 Check if a text is encoded in a coding system of which
4598 encoder/decoder are written in CCL program. If it is, return
4599 CATEGORY_MASK_CCL, else return 0. */
4602 detect_coding_ccl (coding
, detect_info
)
4603 struct coding_system
*coding
;
4604 struct coding_detection_info
*detect_info
;
4606 const unsigned char *src
= coding
->source
, *src_base
;
4607 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4608 int multibytep
= coding
->src_multibyte
;
4609 int consumed_chars
= 0;
4611 unsigned char *valids
;
4612 int head_ascii
= coding
->head_ascii
;
4615 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4617 coding
= &coding_categories
[coding_category_ccl
];
4618 valids
= CODING_CCL_VALIDS (coding
);
4619 attrs
= CODING_ID_ATTRS (coding
->id
);
4620 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4629 if (c
< 0 || ! valids
[c
])
4631 if ((valids
[c
] > 1))
4632 found
= CATEGORY_MASK_CCL
;
4634 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4638 detect_info
->found
|= found
;
4643 decode_coding_ccl (coding
)
4644 struct coding_system
*coding
;
4646 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4647 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4648 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4649 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4650 int consumed_chars
= 0;
4651 int multibytep
= coding
->src_multibyte
;
4652 struct ccl_program ccl
;
4653 int source_charbuf
[1024];
4654 int source_byteidx
[1024];
4655 Lisp_Object attrs
, charset_list
;
4657 CODING_GET_INFO (coding
, attrs
, charset_list
);
4658 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4660 while (src
< src_end
)
4662 const unsigned char *p
= src
;
4663 int *source
, *source_end
;
4667 while (i
< 1024 && p
< src_end
)
4669 source_byteidx
[i
] = p
- src
;
4670 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4673 while (i
< 1024 && p
< src_end
)
4674 source_charbuf
[i
++] = *p
++;
4676 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4679 source
= source_charbuf
;
4680 source_end
= source
+ i
;
4681 while (source
< source_end
)
4683 ccl_driver (&ccl
, source
, charbuf
,
4684 source_end
- source
, charbuf_end
- charbuf
,
4686 source
+= ccl
.consumed
;
4687 charbuf
+= ccl
.produced
;
4688 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4691 if (source
< source_end
)
4692 src
+= source_byteidx
[source
- source_charbuf
];
4695 consumed_chars
+= source
- source_charbuf
;
4697 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4698 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4704 case CCL_STAT_SUSPEND_BY_SRC
:
4705 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4707 case CCL_STAT_SUSPEND_BY_DST
:
4710 case CCL_STAT_INVALID_CMD
:
4711 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4714 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4717 coding
->consumed_char
+= consumed_chars
;
4718 coding
->consumed
= src
- coding
->source
;
4719 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4723 encode_coding_ccl (coding
)
4724 struct coding_system
*coding
;
4726 struct ccl_program ccl
;
4727 int multibytep
= coding
->dst_multibyte
;
4728 int *charbuf
= coding
->charbuf
;
4729 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4730 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4731 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4732 int destination_charbuf
[1024];
4733 int i
, produced_chars
= 0;
4734 Lisp_Object attrs
, charset_list
;
4736 CODING_GET_INFO (coding
, attrs
, charset_list
);
4737 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4739 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4740 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4742 while (charbuf
< charbuf_end
)
4744 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4745 charbuf_end
- charbuf
, 1024, charset_list
);
4748 ASSURE_DESTINATION (ccl
.produced
* 2);
4749 for (i
= 0; i
< ccl
.produced
; i
++)
4750 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4754 ASSURE_DESTINATION (ccl
.produced
);
4755 for (i
= 0; i
< ccl
.produced
; i
++)
4756 *dst
++ = destination_charbuf
[i
] & 0xFF;
4757 produced_chars
+= ccl
.produced
;
4759 charbuf
+= ccl
.consumed
;
4760 if (ccl
.status
== CCL_STAT_QUIT
4761 || ccl
.status
== CCL_STAT_INVALID_CMD
)
4767 case CCL_STAT_SUSPEND_BY_SRC
:
4768 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4770 case CCL_STAT_SUSPEND_BY_DST
:
4771 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4774 case CCL_STAT_INVALID_CMD
:
4775 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4778 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4782 coding
->produced_char
+= produced_chars
;
4783 coding
->produced
= dst
- coding
->destination
;
4789 /*** 10, 11. no-conversion handlers ***/
4791 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4794 decode_coding_raw_text (coding
)
4795 struct coding_system
*coding
;
4797 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
4799 coding
->chars_at_source
= 1;
4800 coding
->consumed_char
= coding
->src_chars
;
4801 coding
->consumed
= coding
->src_bytes
;
4802 if (eol_crlf
&& coding
->source
[coding
->src_bytes
- 1] == '\r')
4804 coding
->consumed_char
--;
4806 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4809 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4813 encode_coding_raw_text (coding
)
4814 struct coding_system
*coding
;
4816 int multibytep
= coding
->dst_multibyte
;
4817 int *charbuf
= coding
->charbuf
;
4818 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
4819 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4820 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4825 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4827 if (coding
->src_multibyte
)
4828 while (charbuf
< charbuf_end
)
4830 ASSURE_DESTINATION (safe_room
);
4832 if (ASCII_CHAR_P (c
))
4833 EMIT_ONE_ASCII_BYTE (c
);
4834 else if (CHAR_BYTE8_P (c
))
4836 c
= CHAR_TO_BYTE8 (c
);
4841 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4843 CHAR_STRING_ADVANCE (c
, p1
);
4846 EMIT_ONE_BYTE (*p0
);
4852 while (charbuf
< charbuf_end
)
4854 ASSURE_DESTINATION (safe_room
);
4861 if (coding
->src_multibyte
)
4863 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4865 while (charbuf
< charbuf_end
)
4867 ASSURE_DESTINATION (safe_room
);
4869 if (ASCII_CHAR_P (c
))
4871 else if (CHAR_BYTE8_P (c
))
4872 *dst
++ = CHAR_TO_BYTE8 (c
);
4874 CHAR_STRING_ADVANCE (c
, dst
);
4879 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4880 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4881 *dst
++ = *charbuf
++;
4884 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4885 coding
->produced_char
+= charbuf
- coding
->charbuf
;
4886 coding
->produced
= dst
- coding
->destination
;
4890 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4891 Check if a text is encoded in a charset-based coding system. If it
4892 is, return 1, else return 0. */
4895 detect_coding_charset (coding
, detect_info
)
4896 struct coding_system
*coding
;
4897 struct coding_detection_info
*detect_info
;
4899 const unsigned char *src
= coding
->source
, *src_base
;
4900 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4901 int multibytep
= coding
->src_multibyte
;
4902 int consumed_chars
= 0;
4903 Lisp_Object attrs
, valids
;
4905 int head_ascii
= coding
->head_ascii
;
4907 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4909 coding
= &coding_categories
[coding_category_charset
];
4910 attrs
= CODING_ID_ATTRS (coding
->id
);
4911 valids
= AREF (attrs
, coding_attr_charset_valids
);
4913 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4920 struct charset
*charset
;
4927 val
= AREF (valids
, c
);
4931 found
= CATEGORY_MASK_CHARSET
;
4934 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4935 dim
= CHARSET_DIMENSION (charset
);
4936 for (idx
= 1; idx
< dim
; idx
++)
4941 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
4942 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
4951 for (; CONSP (val
); val
= XCDR (val
))
4953 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4954 dim
= CHARSET_DIMENSION (charset
);
4960 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
4961 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
4976 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4980 detect_info
->found
|= found
;
4985 decode_coding_charset (coding
)
4986 struct coding_system
*coding
;
4988 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4989 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4990 const unsigned char *src_base
;
4991 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4993 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4994 int consumed_chars
= 0, consumed_chars_base
;
4995 int multibytep
= coding
->src_multibyte
;
4996 Lisp_Object attrs
, charset_list
, valids
;
4997 int char_offset
= coding
->produced_char
;
4998 int last_offset
= char_offset
;
4999 int last_id
= charset_ascii
;
5000 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5001 int byte_after_cr
= -1;
5003 CODING_GET_INFO (coding
, attrs
, charset_list
);
5004 valids
= AREF (attrs
, coding_attr_charset_valids
);
5010 struct charset
*charset
;
5016 consumed_chars_base
= consumed_chars
;
5018 if (charbuf
>= charbuf_end
)
5021 if (byte_after_cr
>= 0)
5029 if (eol_crlf
&& c
== '\r')
5030 ONE_MORE_BYTE (byte_after_cr
);
5036 val
= AREF (valids
, c
);
5041 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5042 dim
= CHARSET_DIMENSION (charset
);
5046 code
= (code
<< 8) | c
;
5049 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5054 /* VAL is a list of charset IDs. It is assured that the
5055 list is sorted by charset dimensions (smaller one
5059 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5060 dim
= CHARSET_DIMENSION (charset
);
5064 code
= (code
<< 8) | c
;
5067 CODING_DECODE_CHAR (coding
, src
, src_base
,
5068 src_end
, charset
, code
, c
);
5076 if (charset
->id
!= charset_ascii
5077 && last_id
!= charset
->id
)
5079 if (last_id
!= charset_ascii
)
5080 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5081 last_id
= charset
->id
;
5082 last_offset
= char_offset
;
5091 consumed_chars
= consumed_chars_base
;
5093 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5099 if (last_id
!= charset_ascii
)
5100 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5101 coding
->consumed_char
+= consumed_chars_base
;
5102 coding
->consumed
= src_base
- coding
->source
;
5103 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5107 encode_coding_charset (coding
)
5108 struct coding_system
*coding
;
5110 int multibytep
= coding
->dst_multibyte
;
5111 int *charbuf
= coding
->charbuf
;
5112 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5113 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5114 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5115 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5116 int produced_chars
= 0;
5117 Lisp_Object attrs
, charset_list
;
5118 int ascii_compatible
;
5121 CODING_GET_INFO (coding
, attrs
, charset_list
);
5122 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5124 while (charbuf
< charbuf_end
)
5126 struct charset
*charset
;
5129 ASSURE_DESTINATION (safe_room
);
5131 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5132 EMIT_ONE_ASCII_BYTE (c
);
5133 else if (CHAR_BYTE8_P (c
))
5135 c
= CHAR_TO_BYTE8 (c
);
5140 charset
= char_charset (c
, charset_list
, &code
);
5143 if (CHARSET_DIMENSION (charset
) == 1)
5144 EMIT_ONE_BYTE (code
);
5145 else if (CHARSET_DIMENSION (charset
) == 2)
5146 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5147 else if (CHARSET_DIMENSION (charset
) == 3)
5148 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5150 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5151 (code
>> 8) & 0xFF, code
& 0xFF);
5155 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5156 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5158 c
= coding
->default_char
;
5164 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5165 coding
->produced_char
+= produced_chars
;
5166 coding
->produced
= dst
- coding
->destination
;
5171 /*** 7. C library functions ***/
5173 /* Setup coding context CODING from information about CODING_SYSTEM.
5174 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5175 CODING_SYSTEM is invalid, signal an error. */
5178 setup_coding_system (coding_system
, coding
)
5179 Lisp_Object coding_system
;
5180 struct coding_system
*coding
;
5183 Lisp_Object eol_type
;
5184 Lisp_Object coding_type
;
5187 if (NILP (coding_system
))
5188 coding_system
= Qundecided
;
5190 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5192 attrs
= CODING_ID_ATTRS (coding
->id
);
5193 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5196 coding
->head_ascii
= -1;
5197 if (VECTORP (eol_type
))
5198 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5199 | CODING_REQUIRE_DETECTION_MASK
);
5200 else if (! EQ (eol_type
, Qunix
))
5201 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5202 | CODING_REQUIRE_ENCODING_MASK
);
5204 coding
->common_flags
= 0;
5205 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5206 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5207 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5208 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5209 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5210 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5212 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5213 coding
->max_charset_id
= SCHARS (val
) - 1;
5214 coding
->safe_charsets
= (char *) SDATA (val
);
5215 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5217 coding_type
= CODING_ATTR_TYPE (attrs
);
5218 if (EQ (coding_type
, Qundecided
))
5220 coding
->detector
= NULL
;
5221 coding
->decoder
= decode_coding_raw_text
;
5222 coding
->encoder
= encode_coding_raw_text
;
5223 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5225 else if (EQ (coding_type
, Qiso_2022
))
5228 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5230 /* Invoke graphic register 0 to plane 0. */
5231 CODING_ISO_INVOCATION (coding
, 0) = 0;
5232 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5233 CODING_ISO_INVOCATION (coding
, 1)
5234 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5235 /* Setup the initial status of designation. */
5236 for (i
= 0; i
< 4; i
++)
5237 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5238 /* Not single shifting initially. */
5239 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5240 /* Beginning of buffer should also be regarded as bol. */
5241 CODING_ISO_BOL (coding
) = 1;
5242 coding
->detector
= detect_coding_iso_2022
;
5243 coding
->decoder
= decode_coding_iso_2022
;
5244 coding
->encoder
= encode_coding_iso_2022
;
5245 if (flags
& CODING_ISO_FLAG_SAFE
)
5246 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5247 coding
->common_flags
5248 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5249 | CODING_REQUIRE_FLUSHING_MASK
);
5250 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5251 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5252 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5253 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5254 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5256 setup_iso_safe_charsets (attrs
);
5257 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5258 coding
->max_charset_id
= SCHARS (val
) - 1;
5259 coding
->safe_charsets
= (char *) SDATA (val
);
5261 CODING_ISO_FLAGS (coding
) = flags
;
5263 else if (EQ (coding_type
, Qcharset
))
5265 coding
->detector
= detect_coding_charset
;
5266 coding
->decoder
= decode_coding_charset
;
5267 coding
->encoder
= encode_coding_charset
;
5268 coding
->common_flags
5269 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5271 else if (EQ (coding_type
, Qutf_8
))
5273 coding
->detector
= detect_coding_utf_8
;
5274 coding
->decoder
= decode_coding_utf_8
;
5275 coding
->encoder
= encode_coding_utf_8
;
5276 coding
->common_flags
5277 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5279 else if (EQ (coding_type
, Qutf_16
))
5281 val
= AREF (attrs
, coding_attr_utf_16_bom
);
5282 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
5283 : EQ (val
, Qt
) ? utf_16_with_bom
5284 : utf_16_without_bom
);
5285 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5286 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5287 : utf_16_little_endian
);
5288 CODING_UTF_16_SURROGATE (coding
) = 0;
5289 coding
->detector
= detect_coding_utf_16
;
5290 coding
->decoder
= decode_coding_utf_16
;
5291 coding
->encoder
= encode_coding_utf_16
;
5292 coding
->common_flags
5293 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5294 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
5295 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5297 else if (EQ (coding_type
, Qccl
))
5299 coding
->detector
= detect_coding_ccl
;
5300 coding
->decoder
= decode_coding_ccl
;
5301 coding
->encoder
= encode_coding_ccl
;
5302 coding
->common_flags
5303 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5304 | CODING_REQUIRE_FLUSHING_MASK
);
5306 else if (EQ (coding_type
, Qemacs_mule
))
5308 coding
->detector
= detect_coding_emacs_mule
;
5309 coding
->decoder
= decode_coding_emacs_mule
;
5310 coding
->encoder
= encode_coding_emacs_mule
;
5311 coding
->common_flags
5312 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5313 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5314 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5316 Lisp_Object tail
, safe_charsets
;
5317 int max_charset_id
= 0;
5319 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5321 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5322 max_charset_id
= XFASTINT (XCAR (tail
));
5323 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5325 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5327 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5328 coding
->max_charset_id
= max_charset_id
;
5329 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5332 else if (EQ (coding_type
, Qshift_jis
))
5334 coding
->detector
= detect_coding_sjis
;
5335 coding
->decoder
= decode_coding_sjis
;
5336 coding
->encoder
= encode_coding_sjis
;
5337 coding
->common_flags
5338 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5340 else if (EQ (coding_type
, Qbig5
))
5342 coding
->detector
= detect_coding_big5
;
5343 coding
->decoder
= decode_coding_big5
;
5344 coding
->encoder
= encode_coding_big5
;
5345 coding
->common_flags
5346 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5348 else /* EQ (coding_type, Qraw_text) */
5350 coding
->detector
= NULL
;
5351 coding
->decoder
= decode_coding_raw_text
;
5352 coding
->encoder
= encode_coding_raw_text
;
5353 if (! EQ (eol_type
, Qunix
))
5355 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5356 if (! VECTORP (eol_type
))
5357 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5365 /* Return a list of charsets supported by CODING. */
5368 coding_charset_list (coding
)
5369 struct coding_system
*coding
;
5371 Lisp_Object attrs
, charset_list
;
5373 CODING_GET_INFO (coding
, attrs
, charset_list
);
5374 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5376 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5378 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5379 charset_list
= Viso_2022_charset_list
;
5381 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5383 charset_list
= Vemacs_mule_charset_list
;
5385 return charset_list
;
5389 /* Return raw-text or one of its subsidiaries that has the same
5390 eol_type as CODING-SYSTEM. */
5393 raw_text_coding_system (coding_system
)
5394 Lisp_Object coding_system
;
5396 Lisp_Object spec
, attrs
;
5397 Lisp_Object eol_type
, raw_text_eol_type
;
5399 if (NILP (coding_system
))
5401 spec
= CODING_SYSTEM_SPEC (coding_system
);
5402 attrs
= AREF (spec
, 0);
5404 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5405 return coding_system
;
5407 eol_type
= AREF (spec
, 2);
5408 if (VECTORP (eol_type
))
5410 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5411 raw_text_eol_type
= AREF (spec
, 2);
5412 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5413 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5414 : AREF (raw_text_eol_type
, 2));
5418 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5419 does, return one of the subsidiary that has the same eol-spec as
5420 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5421 inherit end-of-line format from the system's setting
5422 (system_eol_type). */
5425 coding_inherit_eol_type (coding_system
, parent
)
5426 Lisp_Object coding_system
, parent
;
5428 Lisp_Object spec
, eol_type
;
5430 if (NILP (coding_system
))
5431 coding_system
= Qraw_text
;
5432 spec
= CODING_SYSTEM_SPEC (coding_system
);
5433 eol_type
= AREF (spec
, 2);
5434 if (VECTORP (eol_type
))
5436 Lisp_Object parent_eol_type
;
5438 if (! NILP (parent
))
5440 Lisp_Object parent_spec
;
5442 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5443 parent_eol_type
= AREF (parent_spec
, 2);
5446 parent_eol_type
= system_eol_type
;
5447 if (EQ (parent_eol_type
, Qunix
))
5448 coding_system
= AREF (eol_type
, 0);
5449 else if (EQ (parent_eol_type
, Qdos
))
5450 coding_system
= AREF (eol_type
, 1);
5451 else if (EQ (parent_eol_type
, Qmac
))
5452 coding_system
= AREF (eol_type
, 2);
5454 return coding_system
;
5457 /* Emacs has a mechanism to automatically detect a coding system if it
5458 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5459 it's impossible to distinguish some coding systems accurately
5460 because they use the same range of codes. So, at first, coding
5461 systems are categorized into 7, those are:
5463 o coding-category-emacs-mule
5465 The category for a coding system which has the same code range
5466 as Emacs' internal format. Assigned the coding-system (Lisp
5467 symbol) `emacs-mule' by default.
5469 o coding-category-sjis
5471 The category for a coding system which has the same code range
5472 as SJIS. Assigned the coding-system (Lisp
5473 symbol) `japanese-shift-jis' by default.
5475 o coding-category-iso-7
5477 The category for a coding system which has the same code range
5478 as ISO2022 of 7-bit environment. This doesn't use any locking
5479 shift and single shift functions. This can encode/decode all
5480 charsets. Assigned the coding-system (Lisp symbol)
5481 `iso-2022-7bit' by default.
5483 o coding-category-iso-7-tight
5485 Same as coding-category-iso-7 except that this can
5486 encode/decode only the specified charsets.
5488 o coding-category-iso-8-1
5490 The category for a coding system which has the same code range
5491 as ISO2022 of 8-bit environment and graphic plane 1 used only
5492 for DIMENSION1 charset. This doesn't use any locking shift
5493 and single shift functions. Assigned the coding-system (Lisp
5494 symbol) `iso-latin-1' by default.
5496 o coding-category-iso-8-2
5498 The category for a coding system which has the same code range
5499 as ISO2022 of 8-bit environment and graphic plane 1 used only
5500 for DIMENSION2 charset. This doesn't use any locking shift
5501 and single shift functions. Assigned the coding-system (Lisp
5502 symbol) `japanese-iso-8bit' by default.
5504 o coding-category-iso-7-else
5506 The category for a coding system which has the same code range
5507 as ISO2022 of 7-bit environemnt but uses locking shift or
5508 single shift functions. Assigned the coding-system (Lisp
5509 symbol) `iso-2022-7bit-lock' by default.
5511 o coding-category-iso-8-else
5513 The category for a coding system which has the same code range
5514 as ISO2022 of 8-bit environemnt but uses locking shift or
5515 single shift functions. Assigned the coding-system (Lisp
5516 symbol) `iso-2022-8bit-ss2' by default.
5518 o coding-category-big5
5520 The category for a coding system which has the same code range
5521 as BIG5. Assigned the coding-system (Lisp symbol)
5522 `cn-big5' by default.
5524 o coding-category-utf-8
5526 The category for a coding system which has the same code range
5527 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5528 symbol) `utf-8' by default.
5530 o coding-category-utf-16-be
5532 The category for a coding system in which a text has an
5533 Unicode signature (cf. Unicode Standard) in the order of BIG
5534 endian at the head. Assigned the coding-system (Lisp symbol)
5535 `utf-16-be' by default.
5537 o coding-category-utf-16-le
5539 The category for a coding system in which a text has an
5540 Unicode signature (cf. Unicode Standard) in the order of
5541 LITTLE endian at the head. Assigned the coding-system (Lisp
5542 symbol) `utf-16-le' by default.
5544 o coding-category-ccl
5546 The category for a coding system of which encoder/decoder is
5547 written in CCL programs. The default value is nil, i.e., no
5548 coding system is assigned.
5550 o coding-category-binary
5552 The category for a coding system not categorized in any of the
5553 above. Assigned the coding-system (Lisp symbol)
5554 `no-conversion' by default.
5556 Each of them is a Lisp symbol and the value is an actual
5557 `coding-system's (this is also a Lisp symbol) assigned by a user.
5558 What Emacs does actually is to detect a category of coding system.
5559 Then, it uses a `coding-system' assigned to it. If Emacs can't
5560 decide only one possible category, it selects a category of the
5561 highest priority. Priorities of categories are also specified by a
5562 user in a Lisp variable `coding-category-list'.
5566 #define EOL_SEEN_NONE 0
5567 #define EOL_SEEN_LF 1
5568 #define EOL_SEEN_CR 2
5569 #define EOL_SEEN_CRLF 4
5571 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5572 SOURCE is encoded. If CATEGORY is one of
5573 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5574 two-byte, else they are encoded by one-byte.
5576 Return one of EOL_SEEN_XXX. */
5578 #define MAX_EOL_CHECK_COUNT 3
5581 detect_eol (source
, src_bytes
, category
)
5582 const unsigned char *source
;
5583 EMACS_INT src_bytes
;
5584 enum coding_category category
;
5586 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5589 int eol_seen
= EOL_SEEN_NONE
;
5591 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5595 msb
= category
== (coding_category_utf_16_le
5596 | coding_category_utf_16_le_nosig
);
5599 while (src
+ 1 < src_end
)
5602 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5607 this_eol
= EOL_SEEN_LF
;
5608 else if (src
+ 3 >= src_end
5609 || src
[msb
+ 2] != 0
5610 || src
[lsb
+ 2] != '\n')
5611 this_eol
= EOL_SEEN_CR
;
5613 this_eol
= EOL_SEEN_CRLF
;
5615 if (eol_seen
== EOL_SEEN_NONE
)
5616 /* This is the first end-of-line. */
5617 eol_seen
= this_eol
;
5618 else if (eol_seen
!= this_eol
)
5620 /* The found type is different from what found before. */
5621 eol_seen
= EOL_SEEN_LF
;
5624 if (++total
== MAX_EOL_CHECK_COUNT
)
5632 while (src
< src_end
)
5635 if (c
== '\n' || c
== '\r')
5640 this_eol
= EOL_SEEN_LF
;
5641 else if (src
>= src_end
|| *src
!= '\n')
5642 this_eol
= EOL_SEEN_CR
;
5644 this_eol
= EOL_SEEN_CRLF
, src
++;
5646 if (eol_seen
== EOL_SEEN_NONE
)
5647 /* This is the first end-of-line. */
5648 eol_seen
= this_eol
;
5649 else if (eol_seen
!= this_eol
)
5651 /* The found type is different from what found before. */
5652 eol_seen
= EOL_SEEN_LF
;
5655 if (++total
== MAX_EOL_CHECK_COUNT
)
5665 adjust_coding_eol_type (coding
, eol_seen
)
5666 struct coding_system
*coding
;
5669 Lisp_Object eol_type
;
5671 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5672 if (eol_seen
& EOL_SEEN_LF
)
5674 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5677 else if (eol_seen
& EOL_SEEN_CRLF
)
5679 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5682 else if (eol_seen
& EOL_SEEN_CR
)
5684 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5690 /* Detect how a text specified in CODING is encoded. If a coding
5691 system is detected, update fields of CODING by the detected coding
5695 detect_coding (coding
)
5696 struct coding_system
*coding
;
5698 const unsigned char *src
, *src_end
;
5700 coding
->consumed
= coding
->consumed_char
= 0;
5701 coding
->produced
= coding
->produced_char
= 0;
5702 coding_set_source (coding
);
5704 src_end
= coding
->source
+ coding
->src_bytes
;
5706 /* If we have not yet decided the text encoding type, detect it
5708 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5711 struct coding_detection_info detect_info
;
5712 int null_byte_found
= 0, eight_bit_found
= 0;
5714 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5715 coding
->head_ascii
= -1;
5716 for (src
= coding
->source
; src
< src_end
; src
++)
5721 eight_bit_found
= 1;
5722 if (coding
->head_ascii
< 0)
5723 coding
->head_ascii
= src
- coding
->source
;
5724 if (null_byte_found
)
5729 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5730 && ! inhibit_iso_escape_detection
5731 && ! detect_info
.checked
)
5733 if (coding
->head_ascii
< 0)
5734 coding
->head_ascii
= src
- coding
->source
;
5735 if (detect_coding_iso_2022 (coding
, &detect_info
))
5737 /* We have scanned the whole data. */
5738 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5739 /* We didn't find an 8-bit code. We may have
5740 found a null-byte, but it's very rare that
5741 a binary file confirm to ISO-2022. */
5748 null_byte_found
= 1;
5749 if (eight_bit_found
)
5754 if (coding
->head_ascii
< 0)
5755 coding
->head_ascii
= src
- coding
->source
;
5757 if (null_byte_found
|| eight_bit_found
5758 || coding
->head_ascii
< coding
->src_bytes
5759 || detect_info
.found
)
5761 enum coding_category category
;
5762 struct coding_system
*this;
5764 if (coding
->head_ascii
== coding
->src_bytes
)
5765 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5766 for (i
= 0; i
< coding_category_raw_text
; i
++)
5768 category
= coding_priorities
[i
];
5769 this = coding_categories
+ category
;
5770 if (detect_info
.found
& (1 << category
))
5775 if (null_byte_found
)
5777 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
5778 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
5780 for (i
= 0; i
< coding_category_raw_text
; i
++)
5782 category
= coding_priorities
[i
];
5783 this = coding_categories
+ category
;
5786 /* No coding system of this category is defined. */
5787 detect_info
.rejected
|= (1 << category
);
5789 else if (category
>= coding_category_raw_text
)
5791 else if (detect_info
.checked
& (1 << category
))
5793 if (detect_info
.found
& (1 << category
))
5796 else if ((*(this->detector
)) (coding
, &detect_info
)
5797 && detect_info
.found
& (1 << category
))
5799 if (category
== coding_category_utf_16_auto
)
5801 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5802 category
= coding_category_utf_16_le
;
5804 category
= coding_category_utf_16_be
;
5810 if (i
< coding_category_raw_text
)
5811 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5812 else if (null_byte_found
)
5813 setup_coding_system (Qno_conversion
, coding
);
5814 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
5815 == CATEGORY_MASK_ANY
)
5816 setup_coding_system (Qraw_text
, coding
);
5817 else if (detect_info
.rejected
)
5818 for (i
= 0; i
< coding_category_raw_text
; i
++)
5819 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5821 this = coding_categories
+ coding_priorities
[i
];
5822 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5828 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5829 == coding_category_utf_16_auto
)
5831 Lisp_Object coding_systems
;
5832 struct coding_detection_info detect_info
;
5835 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5836 detect_info
.found
= detect_info
.rejected
= 0;
5837 if (CONSP (coding_systems
)
5838 && detect_coding_utf_16 (coding
, &detect_info
))
5840 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5841 setup_coding_system (XCAR (coding_systems
), coding
);
5842 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5843 setup_coding_system (XCDR (coding_systems
), coding
);
5851 struct coding_system
*coding
;
5853 Lisp_Object eol_type
;
5854 unsigned char *p
, *pbeg
, *pend
;
5856 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5857 if (EQ (eol_type
, Qunix
))
5860 if (NILP (coding
->dst_object
))
5861 pbeg
= coding
->destination
;
5863 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5864 pend
= pbeg
+ coding
->produced
;
5866 if (VECTORP (eol_type
))
5868 int eol_seen
= EOL_SEEN_NONE
;
5870 for (p
= pbeg
; p
< pend
; p
++)
5873 eol_seen
|= EOL_SEEN_LF
;
5874 else if (*p
== '\r')
5876 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5878 eol_seen
|= EOL_SEEN_CRLF
;
5882 eol_seen
|= EOL_SEEN_CR
;
5885 if (eol_seen
!= EOL_SEEN_NONE
5886 && eol_seen
!= EOL_SEEN_LF
5887 && eol_seen
!= EOL_SEEN_CRLF
5888 && eol_seen
!= EOL_SEEN_CR
)
5889 eol_seen
= EOL_SEEN_LF
;
5890 if (eol_seen
!= EOL_SEEN_NONE
)
5891 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5894 if (EQ (eol_type
, Qmac
))
5896 for (p
= pbeg
; p
< pend
; p
++)
5900 else if (EQ (eol_type
, Qdos
))
5904 if (NILP (coding
->dst_object
))
5906 /* Start deleting '\r' from the tail to minimize the memory
5908 for (p
= pend
- 2; p
>= pbeg
; p
--)
5911 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5917 int pos_byte
= coding
->dst_pos_byte
;
5918 int pos
= coding
->dst_pos
;
5919 int pos_end
= pos
+ coding
->produced_char
- 1;
5921 while (pos
< pos_end
)
5923 p
= BYTE_POS_ADDR (pos_byte
);
5924 if (*p
== '\r' && p
[1] == '\n')
5926 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5931 if (coding
->dst_multibyte
)
5932 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
5937 coding
->produced
-= n
;
5938 coding
->produced_char
-= n
;
5943 /* Return a translation table (or list of them) from coding system
5944 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5945 decoding (ENCODEP is zero). */
5948 get_translation_table (attrs
, encodep
, max_lookup
)
5950 int encodep
, *max_lookup
;
5952 Lisp_Object standard
, translation_table
;
5956 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5957 standard
= Vstandard_translation_table_for_encode
;
5959 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5960 standard
= Vstandard_translation_table_for_decode
;
5961 if (NILP (translation_table
))
5962 translation_table
= standard
;
5965 if (SYMBOLP (translation_table
))
5966 translation_table
= Fget (translation_table
, Qtranslation_table
);
5967 else if (CONSP (translation_table
))
5969 translation_table
= Fcopy_sequence (translation_table
);
5970 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5971 if (SYMBOLP (XCAR (val
)))
5972 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5974 if (CHAR_TABLE_P (standard
))
5976 if (CONSP (translation_table
))
5977 translation_table
= nconc2 (translation_table
,
5978 Fcons (standard
, Qnil
));
5980 translation_table
= Fcons (translation_table
,
5981 Fcons (standard
, Qnil
));
5988 if (CHAR_TABLE_P (translation_table
)
5989 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5991 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5992 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5993 *max_lookup
= XFASTINT (val
);
5995 else if (CONSP (translation_table
))
5997 Lisp_Object tail
, val
;
5999 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6000 if (CHAR_TABLE_P (XCAR (tail
))
6001 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6003 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6004 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6005 *max_lookup
= XFASTINT (val
);
6009 return translation_table
;
6012 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6015 if (CHAR_TABLE_P (table)) \
6017 trans = CHAR_TABLE_REF (table, c); \
6018 if (CHARACTERP (trans)) \
6019 c = XFASTINT (trans), trans = Qnil; \
6021 else if (CONSP (table)) \
6025 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6026 if (CHAR_TABLE_P (XCAR (tail))) \
6028 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6029 if (CHARACTERP (trans)) \
6030 c = XFASTINT (trans), trans = Qnil; \
6031 else if (! NILP (trans)) \
6039 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
6043 int *from_nchars
, *to_nchars
;
6045 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
6049 Lisp_Object from
, tail
;
6052 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
6057 for (i
= 0; i
< len
; i
++)
6059 if (buf
+ i
== buf_end
)
6065 if (XINT (AREF (from
, i
)) != buf
[i
])
6079 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
6087 produce_chars (coding
, translation_table
, last_block
)
6088 struct coding_system
*coding
;
6089 Lisp_Object translation_table
;
6092 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6093 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6095 EMACS_INT produced_chars
= 0;
6098 if (! coding
->chars_at_source
)
6100 /* Source characters are in coding->charbuf. */
6101 int *buf
= coding
->charbuf
;
6102 int *buf_end
= buf
+ coding
->charbuf_used
;
6104 if (EQ (coding
->src_object
, coding
->dst_object
))
6106 coding_set_source (coding
);
6107 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6110 while (buf
< buf_end
)
6116 int from_nchars
= 1, to_nchars
= 1;
6117 Lisp_Object trans
= Qnil
;
6119 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6122 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
6123 &from_nchars
, &to_nchars
);
6129 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6131 dst
= alloc_destination (coding
,
6133 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6135 if (EQ (coding
->src_object
, coding
->dst_object
))
6137 coding_set_source (coding
);
6138 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6141 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6144 for (i
= 0; i
< to_nchars
; i
++)
6147 c
= XINT (AREF (trans
, i
));
6148 if (coding
->dst_multibyte
6149 || ! CHAR_BYTE8_P (c
))
6150 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6152 *dst
++ = CHAR_TO_BYTE8 (c
);
6154 produced_chars
+= to_nchars
;
6156 while (--from_nchars
> 0)
6160 /* This is an annotation datum. (-C) is the length. */
6163 carryover
= buf_end
- buf
;
6167 /* Source characters are at coding->source. */
6168 const unsigned char *src
= coding
->source
;
6169 const unsigned char *src_end
= src
+ coding
->consumed
;
6171 if (EQ (coding
->dst_object
, coding
->src_object
))
6172 dst_end
= (unsigned char *) src
;
6173 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6175 if (coding
->src_multibyte
)
6178 EMACS_INT consumed_chars
;
6182 const unsigned char *src_base
= src
;
6188 if (EQ (coding
->src_object
, coding
->dst_object
))
6189 dst_end
= (unsigned char *) src
;
6192 EMACS_INT offset
= src
- coding
->source
;
6194 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6196 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6197 coding_set_source (coding
);
6198 src
= coding
->source
+ offset
;
6199 src_end
= coding
->source
+ coding
->src_bytes
;
6200 if (EQ (coding
->src_object
, coding
->dst_object
))
6201 dst_end
= (unsigned char *) src
;
6211 while (src
< src_end
)
6216 if (dst
>= dst_end
- 1)
6218 if (EQ (coding
->src_object
, coding
->dst_object
))
6219 dst_end
= (unsigned char *) src
;
6220 if (dst
>= dst_end
- 1)
6222 EMACS_INT offset
= src
- coding
->source
;
6223 EMACS_INT more_bytes
;
6225 if (EQ (coding
->src_object
, coding
->dst_object
))
6226 more_bytes
= ((src_end
- src
) / 2) + 2;
6228 more_bytes
= src_end
- src
+ 2;
6229 dst
= alloc_destination (coding
, more_bytes
, dst
);
6230 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6231 coding_set_source (coding
);
6232 src
= coding
->source
+ offset
;
6233 src_end
= coding
->source
+ coding
->src_bytes
;
6234 if (EQ (coding
->src_object
, coding
->dst_object
))
6235 dst_end
= (unsigned char *) src
;
6243 if (!EQ (coding
->src_object
, coding
->dst_object
))
6245 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6249 EMACS_INT offset
= src
- coding
->source
;
6251 dst
= alloc_destination (coding
, require
, dst
);
6252 coding_set_source (coding
);
6253 src
= coding
->source
+ offset
;
6254 src_end
= coding
->source
+ coding
->src_bytes
;
6257 produced_chars
= coding
->consumed_char
;
6258 while (src
< src_end
)
6263 produced
= dst
- (coding
->destination
+ coding
->produced
);
6264 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6265 insert_from_gap (produced_chars
, produced
);
6266 coding
->produced
+= produced
;
6267 coding
->produced_char
+= produced_chars
;
6271 /* Compose text in CODING->object according to the annotation data at
6272 CHARBUF. CHARBUF is an array:
6273 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6277 produce_composition (coding
, charbuf
, pos
)
6278 struct coding_system
*coding
;
6284 enum composition_method method
;
6285 Lisp_Object components
;
6288 to
= pos
+ charbuf
[2];
6291 method
= (enum composition_method
) (charbuf
[3]);
6293 if (method
== COMPOSITION_RELATIVE
)
6295 else if (method
>= COMPOSITION_WITH_RULE
6296 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6298 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6303 for (i
= 0; i
< len
; i
++)
6305 args
[i
] = make_number (charbuf
[i
]);
6309 components
= (method
== COMPOSITION_WITH_ALTCHARS
6310 ? Fstring (len
, args
) : Fvector (len
, args
));
6314 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6318 /* Put `charset' property on text in CODING->object according to
6319 the annotation data at CHARBUF. CHARBUF is an array:
6320 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6324 produce_charset (coding
, charbuf
, pos
)
6325 struct coding_system
*coding
;
6329 EMACS_INT from
= pos
- charbuf
[2];
6330 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6332 Fput_text_property (make_number (from
), make_number (pos
),
6333 Qcharset
, CHARSET_NAME (charset
),
6334 coding
->dst_object
);
6338 #define CHARBUF_SIZE 0x4000
6340 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6342 int size = CHARBUF_SIZE;; \
6344 coding->charbuf = NULL; \
6345 while (size > 1024) \
6347 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6348 if (coding->charbuf) \
6352 if (! coding->charbuf) \
6354 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6355 return coding->result; \
6357 coding->charbuf_size = size; \
6362 produce_annotation (coding
, pos
)
6363 struct coding_system
*coding
;
6366 int *charbuf
= coding
->charbuf
;
6367 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6369 if (NILP (coding
->dst_object
))
6372 while (charbuf
< charbuf_end
)
6378 int len
= -*charbuf
;
6381 case CODING_ANNOTATE_COMPOSITION_MASK
:
6382 produce_composition (coding
, charbuf
, pos
);
6384 case CODING_ANNOTATE_CHARSET_MASK
:
6385 produce_charset (coding
, charbuf
, pos
);
6395 /* Decode the data at CODING->src_object into CODING->dst_object.
6396 CODING->src_object is a buffer, a string, or nil.
6397 CODING->dst_object is a buffer.
6399 If CODING->src_object is a buffer, it must be the current buffer.
6400 In this case, if CODING->src_pos is positive, it is a position of
6401 the source text in the buffer, otherwise, the source text is in the
6402 gap area of the buffer, and CODING->src_pos specifies the offset of
6403 the text from GPT (which must be the same as PT). If this is the
6404 same buffer as CODING->dst_object, CODING->src_pos must be
6407 If CODING->src_object is a string, CODING->src_pos is an index to
6410 If CODING->src_object is nil, CODING->source must already point to
6411 the non-relocatable memory area. In this case, CODING->src_pos is
6412 an offset from CODING->source.
6414 The decoded data is inserted at the current point of the buffer
6419 decode_coding (coding
)
6420 struct coding_system
*coding
;
6423 Lisp_Object undo_list
;
6424 Lisp_Object translation_table
;
6428 if (BUFFERP (coding
->src_object
)
6429 && coding
->src_pos
> 0
6430 && coding
->src_pos
< GPT
6431 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6432 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6435 if (BUFFERP (coding
->dst_object
))
6437 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6438 set_buffer_internal (XBUFFER (coding
->dst_object
));
6440 move_gap_both (PT
, PT_BYTE
);
6441 undo_list
= current_buffer
->undo_list
;
6442 current_buffer
->undo_list
= Qt
;
6445 coding
->consumed
= coding
->consumed_char
= 0;
6446 coding
->produced
= coding
->produced_char
= 0;
6447 coding
->chars_at_source
= 0;
6448 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6451 ALLOC_CONVERSION_WORK_AREA (coding
);
6453 attrs
= CODING_ID_ATTRS (coding
->id
);
6454 translation_table
= get_translation_table (attrs
, 0, NULL
);
6459 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6461 coding_set_source (coding
);
6462 coding
->annotated
= 0;
6463 coding
->charbuf_used
= carryover
;
6464 (*(coding
->decoder
)) (coding
);
6465 coding_set_destination (coding
);
6466 carryover
= produce_chars (coding
, translation_table
, 0);
6467 if (coding
->annotated
)
6468 produce_annotation (coding
, pos
);
6469 for (i
= 0; i
< carryover
; i
++)
6471 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6473 while (coding
->consumed
< coding
->src_bytes
6474 && (coding
->result
== CODING_RESULT_SUCCESS
6475 || coding
->result
== CODING_RESULT_INVALID_SRC
));
6479 coding_set_destination (coding
);
6480 coding
->charbuf_used
= carryover
;
6481 produce_chars (coding
, translation_table
, 1);
6484 coding
->carryover_bytes
= 0;
6485 if (coding
->consumed
< coding
->src_bytes
)
6487 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6488 const unsigned char *src
;
6490 coding_set_source (coding
);
6491 coding_set_destination (coding
);
6492 src
= coding
->source
+ coding
->consumed
;
6494 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6496 /* Flush out unprocessed data as binary chars. We are sure
6497 that the number of data is less than the size of
6499 coding
->charbuf_used
= 0;
6500 while (nbytes
-- > 0)
6505 c
= BYTE8_TO_CHAR (c
);
6506 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6508 produce_chars (coding
, Qnil
, 1);
6512 /* Record unprocessed bytes in coding->carryover. We are
6513 sure that the number of data is less than the size of
6514 coding->carryover. */
6515 unsigned char *p
= coding
->carryover
;
6517 coding
->carryover_bytes
= nbytes
;
6518 while (nbytes
-- > 0)
6521 coding
->consumed
= coding
->src_bytes
;
6524 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6525 decode_eol (coding
);
6526 if (BUFFERP (coding
->dst_object
))
6528 current_buffer
->undo_list
= undo_list
;
6529 record_insert (coding
->dst_pos
, coding
->produced_char
);
6531 return coding
->result
;
6535 /* Extract an annotation datum from a composition starting at POS and
6536 ending before LIMIT of CODING->src_object (buffer or string), store
6537 the data in BUF, set *STOP to a starting position of the next
6538 composition (if any) or to LIMIT, and return the address of the
6539 next element of BUF.
6541 If such an annotation is not found, set *STOP to a starting
6542 position of a composition after POS (if any) or to LIMIT, and
6546 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6547 EMACS_INT pos
, limit
;
6548 struct coding_system
*coding
;
6552 EMACS_INT start
, end
;
6555 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6558 else if (start
> pos
)
6564 /* We found a composition. Store the corresponding
6565 annotation data in BUF. */
6567 enum composition_method method
= COMPOSITION_METHOD (prop
);
6568 int nchars
= COMPOSITION_LENGTH (prop
);
6570 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6571 if (method
!= COMPOSITION_RELATIVE
)
6573 Lisp_Object components
;
6576 components
= COMPOSITION_COMPONENTS (prop
);
6577 if (VECTORP (components
))
6579 len
= XVECTOR (components
)->size
;
6580 for (i
= 0; i
< len
; i
++)
6581 *buf
++ = XINT (AREF (components
, i
));
6583 else if (STRINGP (components
))
6585 len
= SCHARS (components
);
6589 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6593 else if (INTEGERP (components
))
6596 *buf
++ = XINT (components
);
6598 else if (CONSP (components
))
6600 for (len
= 0; CONSP (components
);
6601 len
++, components
= XCDR (components
))
6602 *buf
++ = XINT (XCAR (components
));
6610 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6621 /* Extract an annotation datum from a text property `charset' at POS of
6622 CODING->src_object (buffer of string), store the data in BUF, set
6623 *STOP to the position where the value of `charset' property changes
6624 (limiting by LIMIT), and return the address of the next element of
6627 If the property value is nil, set *STOP to the position where the
6628 property value is non-nil (limiting by LIMIT), and return BUF. */
6631 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6632 EMACS_INT pos
, limit
;
6633 struct coding_system
*coding
;
6637 Lisp_Object val
, next
;
6640 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6641 if (! NILP (val
) && CHARSETP (val
))
6642 id
= XINT (CHARSET_SYMBOL_ID (val
));
6645 ADD_CHARSET_DATA (buf
, 0, id
);
6646 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6648 make_number (limit
));
6649 *stop
= XINT (next
);
6655 consume_chars (coding
, translation_table
, max_lookup
)
6656 struct coding_system
*coding
;
6657 Lisp_Object translation_table
;
6660 int *buf
= coding
->charbuf
;
6661 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6662 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6663 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6664 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6665 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6666 int multibytep
= coding
->src_multibyte
;
6667 Lisp_Object eol_type
;
6669 EMACS_INT stop
, stop_composition
, stop_charset
;
6670 int *lookup_buf
= NULL
;
6672 if (! NILP (translation_table
))
6673 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6675 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6676 if (VECTORP (eol_type
))
6679 /* Note: composition handling is not yet implemented. */
6680 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6682 if (NILP (coding
->src_object
))
6683 stop
= stop_composition
= stop_charset
= end_pos
;
6686 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6687 stop
= stop_composition
= pos
;
6689 stop
= stop_composition
= end_pos
;
6690 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6691 stop
= stop_charset
= pos
;
6693 stop_charset
= end_pos
;
6696 /* Compensate for CRLF and conversion. */
6697 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6698 while (buf
< buf_end
)
6706 if (pos
== stop_composition
)
6707 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6708 buf
, &stop_composition
);
6709 if (pos
== stop_charset
)
6710 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6711 buf
, &stop_charset
);
6712 stop
= (stop_composition
< stop_charset
6713 ? stop_composition
: stop_charset
);
6720 if (coding
->encoder
== encode_coding_raw_text
)
6722 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6723 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
6725 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
6728 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
6729 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6731 if (! EQ (eol_type
, Qunix
))
6735 if (EQ (eol_type
, Qdos
))
6743 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6748 int from_nchars
= 1, to_nchars
= 1;
6749 int *lookup_buf_end
;
6750 const unsigned char *p
= src
;
6754 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6755 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6756 lookup_buf_end
= lookup_buf
+ i
;
6757 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6758 &from_nchars
, &to_nchars
);
6760 || buf
+ to_nchars
> buf_end
)
6762 *buf
++ = *lookup_buf
;
6763 for (i
= 1; i
< to_nchars
; i
++)
6764 *buf
++ = XINT (AREF (trans
, i
));
6765 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6766 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6770 coding
->consumed
= src
- coding
->source
;
6771 coding
->consumed_char
= pos
- coding
->src_pos
;
6772 coding
->charbuf_used
= buf
- coding
->charbuf
;
6773 coding
->chars_at_source
= 0;
6777 /* Encode the text at CODING->src_object into CODING->dst_object.
6778 CODING->src_object is a buffer or a string.
6779 CODING->dst_object is a buffer or nil.
6781 If CODING->src_object is a buffer, it must be the current buffer.
6782 In this case, if CODING->src_pos is positive, it is a position of
6783 the source text in the buffer, otherwise. the source text is in the
6784 gap area of the buffer, and coding->src_pos specifies the offset of
6785 the text from GPT (which must be the same as PT). If this is the
6786 same buffer as CODING->dst_object, CODING->src_pos must be
6787 negative and CODING should not have `pre-write-conversion'.
6789 If CODING->src_object is a string, CODING should not have
6790 `pre-write-conversion'.
6792 If CODING->dst_object is a buffer, the encoded data is inserted at
6793 the current point of that buffer.
6795 If CODING->dst_object is nil, the encoded data is placed at the
6796 memory area specified by CODING->destination. */
6799 encode_coding (coding
)
6800 struct coding_system
*coding
;
6803 Lisp_Object translation_table
;
6806 attrs
= CODING_ID_ATTRS (coding
->id
);
6807 if (coding
->encoder
== encode_coding_raw_text
)
6808 translation_table
= Qnil
, max_lookup
= 0;
6810 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6812 if (BUFFERP (coding
->dst_object
))
6814 set_buffer_internal (XBUFFER (coding
->dst_object
));
6815 coding
->dst_multibyte
6816 = ! NILP (current_buffer
->enable_multibyte_characters
);
6819 coding
->consumed
= coding
->consumed_char
= 0;
6820 coding
->produced
= coding
->produced_char
= 0;
6821 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6824 ALLOC_CONVERSION_WORK_AREA (coding
);
6827 coding_set_source (coding
);
6828 consume_chars (coding
, translation_table
, max_lookup
);
6829 coding_set_destination (coding
);
6830 (*(coding
->encoder
)) (coding
);
6831 } while (coding
->consumed_char
< coding
->src_chars
);
6833 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
6834 insert_from_gap (coding
->produced_char
, coding
->produced
);
6836 return (coding
->result
);
6840 /* Name (or base name) of work buffer for code conversion. */
6841 static Lisp_Object Vcode_conversion_workbuf_name
;
6843 /* A working buffer used by the top level conversion. Once it is
6844 created, it is never destroyed. It has the name
6845 Vcode_conversion_workbuf_name. The other working buffers are
6846 destroyed after the use is finished, and their names are modified
6847 versions of Vcode_conversion_workbuf_name. */
6848 static Lisp_Object Vcode_conversion_reused_workbuf
;
6850 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6851 static int reused_workbuf_in_use
;
6854 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6855 multibyteness of returning buffer. */
6858 make_conversion_work_buffer (multibyte
)
6861 Lisp_Object name
, workbuf
;
6862 struct buffer
*current
;
6864 if (reused_workbuf_in_use
++)
6866 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6867 workbuf
= Fget_buffer_create (name
);
6871 name
= Vcode_conversion_workbuf_name
;
6872 workbuf
= Fget_buffer_create (name
);
6873 if (NILP (Vcode_conversion_reused_workbuf
))
6874 Vcode_conversion_reused_workbuf
= workbuf
;
6876 current
= current_buffer
;
6877 set_buffer_internal (XBUFFER (workbuf
));
6879 current_buffer
->undo_list
= Qt
;
6880 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6881 set_buffer_internal (current
);
6887 code_conversion_restore (arg
)
6890 Lisp_Object current
, workbuf
;
6891 struct gcpro gcpro1
;
6894 current
= XCAR (arg
);
6895 workbuf
= XCDR (arg
);
6896 if (! NILP (workbuf
))
6898 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6899 reused_workbuf_in_use
= 0;
6900 else if (! NILP (Fbuffer_live_p (workbuf
)))
6901 Fkill_buffer (workbuf
);
6903 set_buffer_internal (XBUFFER (current
));
6909 code_conversion_save (with_work_buf
, multibyte
)
6910 int with_work_buf
, multibyte
;
6912 Lisp_Object workbuf
= Qnil
;
6915 workbuf
= make_conversion_work_buffer (multibyte
);
6916 record_unwind_protect (code_conversion_restore
,
6917 Fcons (Fcurrent_buffer (), workbuf
));
6922 decode_coding_gap (coding
, chars
, bytes
)
6923 struct coding_system
*coding
;
6924 EMACS_INT chars
, bytes
;
6926 int count
= specpdl_ptr
- specpdl
;
6929 code_conversion_save (0, 0);
6931 coding
->src_object
= Fcurrent_buffer ();
6932 coding
->src_chars
= chars
;
6933 coding
->src_bytes
= bytes
;
6934 coding
->src_pos
= -chars
;
6935 coding
->src_pos_byte
= -bytes
;
6936 coding
->src_multibyte
= chars
< bytes
;
6937 coding
->dst_object
= coding
->src_object
;
6938 coding
->dst_pos
= PT
;
6939 coding
->dst_pos_byte
= PT_BYTE
;
6940 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6942 if (CODING_REQUIRE_DETECTION (coding
))
6943 detect_coding (coding
);
6945 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6946 current_buffer
->text
->inhibit_shrinking
= 1;
6947 decode_coding (coding
);
6948 current_buffer
->text
->inhibit_shrinking
= 0;
6950 attrs
= CODING_ID_ATTRS (coding
->id
);
6951 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6953 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6956 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6957 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6958 make_number (coding
->produced_char
));
6960 coding
->produced_char
+= Z
- prev_Z
;
6961 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6964 unbind_to (count
, Qnil
);
6965 return coding
->result
;
6969 encode_coding_gap (coding
, chars
, bytes
)
6970 struct coding_system
*coding
;
6971 EMACS_INT chars
, bytes
;
6973 int count
= specpdl_ptr
- specpdl
;
6975 code_conversion_save (0, 0);
6977 coding
->src_object
= Fcurrent_buffer ();
6978 coding
->src_chars
= chars
;
6979 coding
->src_bytes
= bytes
;
6980 coding
->src_pos
= -chars
;
6981 coding
->src_pos_byte
= -bytes
;
6982 coding
->src_multibyte
= chars
< bytes
;
6983 coding
->dst_object
= coding
->src_object
;
6984 coding
->dst_pos
= PT
;
6985 coding
->dst_pos_byte
= PT_BYTE
;
6987 encode_coding (coding
);
6989 unbind_to (count
, Qnil
);
6990 return coding
->result
;
6994 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6995 SRC_OBJECT into DST_OBJECT by coding context CODING.
6997 SRC_OBJECT is a buffer, a string, or Qnil.
6999 If it is a buffer, the text is at point of the buffer. FROM and TO
7000 are positions in the buffer.
7002 If it is a string, the text is at the beginning of the string.
7003 FROM and TO are indices to the string.
7005 If it is nil, the text is at coding->source. FROM and TO are
7006 indices to coding->source.
7008 DST_OBJECT is a buffer, Qt, or Qnil.
7010 If it is a buffer, the decoded text is inserted at point of the
7011 buffer. If the buffer is the same as SRC_OBJECT, the source text
7014 If it is Qt, a string is made from the decoded text, and
7015 set in CODING->dst_object.
7017 If it is Qnil, the decoded text is stored at CODING->destination.
7018 The caller must allocate CODING->dst_bytes bytes at
7019 CODING->destination by xmalloc. If the decoded text is longer than
7020 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7024 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7026 struct coding_system
*coding
;
7027 Lisp_Object src_object
;
7028 EMACS_INT from
, from_byte
, to
, to_byte
;
7029 Lisp_Object dst_object
;
7031 int count
= specpdl_ptr
- specpdl
;
7032 unsigned char *destination
;
7033 EMACS_INT dst_bytes
;
7034 EMACS_INT chars
= to
- from
;
7035 EMACS_INT bytes
= to_byte
- from_byte
;
7037 int saved_pt
= -1, saved_pt_byte
;
7038 int need_marker_adjustment
= 0;
7039 Lisp_Object old_deactivate_mark
;
7041 old_deactivate_mark
= Vdeactivate_mark
;
7043 if (NILP (dst_object
))
7045 destination
= coding
->destination
;
7046 dst_bytes
= coding
->dst_bytes
;
7049 coding
->src_object
= src_object
;
7050 coding
->src_chars
= chars
;
7051 coding
->src_bytes
= bytes
;
7052 coding
->src_multibyte
= chars
< bytes
;
7054 if (STRINGP (src_object
))
7056 coding
->src_pos
= from
;
7057 coding
->src_pos_byte
= from_byte
;
7059 else if (BUFFERP (src_object
))
7061 set_buffer_internal (XBUFFER (src_object
));
7063 move_gap_both (from
, from_byte
);
7064 if (EQ (src_object
, dst_object
))
7066 struct Lisp_Marker
*tail
;
7068 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7070 tail
->need_adjustment
7071 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7072 need_marker_adjustment
|= tail
->need_adjustment
;
7074 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7075 TEMP_SET_PT_BOTH (from
, from_byte
);
7076 current_buffer
->text
->inhibit_shrinking
= 1;
7077 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7078 coding
->src_pos
= -chars
;
7079 coding
->src_pos_byte
= -bytes
;
7083 coding
->src_pos
= from
;
7084 coding
->src_pos_byte
= from_byte
;
7088 if (CODING_REQUIRE_DETECTION (coding
))
7089 detect_coding (coding
);
7090 attrs
= CODING_ID_ATTRS (coding
->id
);
7092 if (EQ (dst_object
, Qt
)
7093 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7094 && NILP (dst_object
)))
7096 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7097 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7098 coding
->dst_pos
= BEG
;
7099 coding
->dst_pos_byte
= BEG_BYTE
;
7101 else if (BUFFERP (dst_object
))
7103 code_conversion_save (0, 0);
7104 coding
->dst_object
= dst_object
;
7105 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7106 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7107 coding
->dst_multibyte
7108 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7112 code_conversion_save (0, 0);
7113 coding
->dst_object
= Qnil
;
7114 /* Most callers presume this will return a multibyte result, and they
7115 won't use `binary' or `raw-text' anyway, so let's not worry about
7116 CODING_FOR_UNIBYTE. */
7117 coding
->dst_multibyte
= 1;
7120 decode_coding (coding
);
7122 if (BUFFERP (coding
->dst_object
))
7123 set_buffer_internal (XBUFFER (coding
->dst_object
));
7125 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7127 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7128 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7131 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7132 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7133 old_deactivate_mark
);
7134 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7135 make_number (coding
->produced_char
));
7138 coding
->produced_char
+= Z
- prev_Z
;
7139 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7142 if (EQ (dst_object
, Qt
))
7144 coding
->dst_object
= Fbuffer_string ();
7146 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7148 set_buffer_internal (XBUFFER (coding
->dst_object
));
7149 if (dst_bytes
< coding
->produced
)
7151 destination
= xrealloc (destination
, coding
->produced
);
7154 record_conversion_result (coding
,
7155 CODING_RESULT_INSUFFICIENT_DST
);
7156 unbind_to (count
, Qnil
);
7159 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7160 move_gap_both (BEGV
, BEGV_BYTE
);
7161 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
7162 coding
->destination
= destination
;
7168 /* This is the case of:
7169 (BUFFERP (src_object) && EQ (src_object, dst_object))
7170 As we have moved PT while replacing the original buffer
7171 contents, we must recover it now. */
7172 set_buffer_internal (XBUFFER (src_object
));
7173 current_buffer
->text
->inhibit_shrinking
= 0;
7174 if (saved_pt
< from
)
7175 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7176 else if (saved_pt
< from
+ chars
)
7177 TEMP_SET_PT_BOTH (from
, from_byte
);
7178 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7179 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7180 saved_pt_byte
+ (coding
->produced
- bytes
));
7182 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7183 saved_pt_byte
+ (coding
->produced
- bytes
));
7185 if (need_marker_adjustment
)
7187 struct Lisp_Marker
*tail
;
7189 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7190 if (tail
->need_adjustment
)
7192 tail
->need_adjustment
= 0;
7193 if (tail
->insertion_type
)
7195 tail
->bytepos
= from_byte
;
7196 tail
->charpos
= from
;
7200 tail
->bytepos
= from_byte
+ coding
->produced
;
7202 = (NILP (current_buffer
->enable_multibyte_characters
)
7203 ? tail
->bytepos
: from
+ coding
->produced_char
);
7209 Vdeactivate_mark
= old_deactivate_mark
;
7210 unbind_to (count
, coding
->dst_object
);
7215 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7217 struct coding_system
*coding
;
7218 Lisp_Object src_object
;
7219 EMACS_INT from
, from_byte
, to
, to_byte
;
7220 Lisp_Object dst_object
;
7222 int count
= specpdl_ptr
- specpdl
;
7223 EMACS_INT chars
= to
- from
;
7224 EMACS_INT bytes
= to_byte
- from_byte
;
7226 int saved_pt
= -1, saved_pt_byte
;
7227 int need_marker_adjustment
= 0;
7228 int kill_src_buffer
= 0;
7229 Lisp_Object old_deactivate_mark
;
7231 old_deactivate_mark
= Vdeactivate_mark
;
7233 coding
->src_object
= src_object
;
7234 coding
->src_chars
= chars
;
7235 coding
->src_bytes
= bytes
;
7236 coding
->src_multibyte
= chars
< bytes
;
7238 attrs
= CODING_ID_ATTRS (coding
->id
);
7240 if (EQ (src_object
, dst_object
))
7242 struct Lisp_Marker
*tail
;
7244 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7246 tail
->need_adjustment
7247 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7248 need_marker_adjustment
|= tail
->need_adjustment
;
7252 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7254 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7255 set_buffer_internal (XBUFFER (coding
->src_object
));
7256 if (STRINGP (src_object
))
7257 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7258 else if (BUFFERP (src_object
))
7259 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7261 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7263 if (EQ (src_object
, dst_object
))
7265 set_buffer_internal (XBUFFER (src_object
));
7266 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7267 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7268 set_buffer_internal (XBUFFER (coding
->src_object
));
7272 Lisp_Object args
[3];
7273 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7275 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7276 old_deactivate_mark
);
7277 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7278 args
[1] = make_number (BEG
);
7279 args
[2] = make_number (Z
);
7280 safe_call (3, args
);
7283 if (XBUFFER (coding
->src_object
) != current_buffer
)
7284 kill_src_buffer
= 1;
7285 coding
->src_object
= Fcurrent_buffer ();
7287 move_gap_both (BEG
, BEG_BYTE
);
7288 coding
->src_chars
= Z
- BEG
;
7289 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7290 coding
->src_pos
= BEG
;
7291 coding
->src_pos_byte
= BEG_BYTE
;
7292 coding
->src_multibyte
= Z
< Z_BYTE
;
7294 else if (STRINGP (src_object
))
7296 code_conversion_save (0, 0);
7297 coding
->src_pos
= from
;
7298 coding
->src_pos_byte
= from_byte
;
7300 else if (BUFFERP (src_object
))
7302 code_conversion_save (0, 0);
7303 set_buffer_internal (XBUFFER (src_object
));
7304 if (EQ (src_object
, dst_object
))
7306 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7307 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7308 coding
->src_pos
= 0;
7309 coding
->src_pos_byte
= 0;
7313 if (from
< GPT
&& to
>= GPT
)
7314 move_gap_both (from
, from_byte
);
7315 coding
->src_pos
= from
;
7316 coding
->src_pos_byte
= from_byte
;
7320 code_conversion_save (0, 0);
7322 if (BUFFERP (dst_object
))
7324 coding
->dst_object
= dst_object
;
7325 if (EQ (src_object
, dst_object
))
7327 coding
->dst_pos
= from
;
7328 coding
->dst_pos_byte
= from_byte
;
7332 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7333 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7335 coding
->dst_multibyte
7336 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7338 else if (EQ (dst_object
, Qt
))
7340 coding
->dst_object
= Qnil
;
7341 coding
->dst_bytes
= coding
->src_chars
;
7342 if (coding
->dst_bytes
== 0)
7343 coding
->dst_bytes
= 1;
7344 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7345 coding
->dst_multibyte
= 0;
7349 coding
->dst_object
= Qnil
;
7350 coding
->dst_multibyte
= 0;
7353 encode_coding (coding
);
7355 if (EQ (dst_object
, Qt
))
7357 if (BUFFERP (coding
->dst_object
))
7358 coding
->dst_object
= Fbuffer_string ();
7362 = make_unibyte_string ((char *) coding
->destination
,
7364 xfree (coding
->destination
);
7370 /* This is the case of:
7371 (BUFFERP (src_object) && EQ (src_object, dst_object))
7372 As we have moved PT while replacing the original buffer
7373 contents, we must recover it now. */
7374 set_buffer_internal (XBUFFER (src_object
));
7375 if (saved_pt
< from
)
7376 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7377 else if (saved_pt
< from
+ chars
)
7378 TEMP_SET_PT_BOTH (from
, from_byte
);
7379 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7380 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7381 saved_pt_byte
+ (coding
->produced
- bytes
));
7383 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7384 saved_pt_byte
+ (coding
->produced
- bytes
));
7386 if (need_marker_adjustment
)
7388 struct Lisp_Marker
*tail
;
7390 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7391 if (tail
->need_adjustment
)
7393 tail
->need_adjustment
= 0;
7394 if (tail
->insertion_type
)
7396 tail
->bytepos
= from_byte
;
7397 tail
->charpos
= from
;
7401 tail
->bytepos
= from_byte
+ coding
->produced
;
7403 = (NILP (current_buffer
->enable_multibyte_characters
)
7404 ? tail
->bytepos
: from
+ coding
->produced_char
);
7410 if (kill_src_buffer
)
7411 Fkill_buffer (coding
->src_object
);
7413 Vdeactivate_mark
= old_deactivate_mark
;
7414 unbind_to (count
, Qnil
);
7419 preferred_coding_system ()
7421 int id
= coding_categories
[coding_priorities
[0]].id
;
7423 return CODING_ID_NAME (id
);
7428 /*** 8. Emacs Lisp library functions ***/
7430 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7431 doc
: /* Return t if OBJECT is nil or a coding-system.
7432 See the documentation of `define-coding-system' for information
7433 about coding-system objects. */)
7438 || CODING_SYSTEM_ID (object
) >= 0)
7440 if (! SYMBOLP (object
)
7441 || NILP (Fget (object
, Qcoding_system_define_form
)))
7446 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7447 Sread_non_nil_coding_system
, 1, 1, 0,
7448 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7455 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7456 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7458 while (SCHARS (val
) == 0);
7459 return (Fintern (val
, Qnil
));
7462 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7463 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7464 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
7465 Ignores case when completing coding systems (all Emacs coding systems
7466 are lower-case). */)
7467 (prompt
, default_coding_system
)
7468 Lisp_Object prompt
, default_coding_system
;
7471 int count
= SPECPDL_INDEX ();
7473 if (SYMBOLP (default_coding_system
))
7474 default_coding_system
= SYMBOL_NAME (default_coding_system
);
7475 specbind (Qcompletion_ignore_case
, Qt
);
7476 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7477 Qt
, Qnil
, Qcoding_system_history
,
7478 default_coding_system
, Qnil
);
7479 unbind_to (count
, Qnil
);
7480 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7483 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7485 doc
: /* Check validity of CODING-SYSTEM.
7486 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7487 It is valid if it is nil or a symbol defined as a coding system by the
7488 function `define-coding-system'. */)
7490 Lisp_Object coding_system
;
7492 Lisp_Object define_form
;
7494 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7495 if (! NILP (define_form
))
7497 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7498 safe_eval (define_form
);
7500 if (!NILP (Fcoding_system_p (coding_system
)))
7501 return coding_system
;
7502 xsignal1 (Qcoding_system_error
, coding_system
);
7506 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7507 HIGHEST is nonzero, return the coding system of the highest
7508 priority among the detected coding systems. Otherwize return a
7509 list of detected coding systems sorted by their priorities. If
7510 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7511 multibyte form but contains only ASCII and eight-bit chars.
7512 Otherwise, the bytes are raw bytes.
7514 CODING-SYSTEM controls the detection as below:
7516 If it is nil, detect both text-format and eol-format. If the
7517 text-format part of CODING-SYSTEM is already specified
7518 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7519 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7520 detect only text-format. */
7523 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7525 const unsigned char *src
;
7526 EMACS_INT src_chars
, src_bytes
;
7529 Lisp_Object coding_system
;
7531 const unsigned char *src_end
= src
+ src_bytes
;
7532 Lisp_Object attrs
, eol_type
;
7534 struct coding_system coding
;
7536 struct coding_detection_info detect_info
;
7537 enum coding_category base_category
;
7538 int null_byte_found
= 0, eight_bit_found
= 0;
7540 if (NILP (coding_system
))
7541 coding_system
= Qundecided
;
7542 setup_coding_system (coding_system
, &coding
);
7543 attrs
= CODING_ID_ATTRS (coding
.id
);
7544 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7545 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7547 coding
.source
= src
;
7548 coding
.src_chars
= src_chars
;
7549 coding
.src_bytes
= src_bytes
;
7550 coding
.src_multibyte
= multibytep
;
7551 coding
.consumed
= 0;
7552 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7554 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7556 /* At first, detect text-format if necessary. */
7557 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7558 if (base_category
== coding_category_undecided
)
7560 enum coding_category category
;
7561 struct coding_system
*this;
7564 coding
.head_ascii
= -1;
7565 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7566 for (; src
< src_end
; src
++)
7571 eight_bit_found
= 1;
7572 if (coding
.head_ascii
< 0)
7573 coding
.head_ascii
= src
- coding
.source
;
7574 if (null_byte_found
)
7579 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7580 && ! inhibit_iso_escape_detection
7581 && ! detect_info
.checked
)
7583 if (coding
.head_ascii
< 0)
7584 coding
.head_ascii
= src
- coding
.source
;
7585 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7587 /* We have scanned the whole data. */
7588 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7589 /* We didn't find an 8-bit code. We may have
7590 found a null-byte, but it's very rare that
7591 a binary file confirm to ISO-2022. */
7598 null_byte_found
= 1;
7599 if (eight_bit_found
)
7604 if (coding
.head_ascii
< 0)
7605 coding
.head_ascii
= src
- coding
.source
;
7607 if (null_byte_found
|| eight_bit_found
7608 || coding
.head_ascii
< coding
.src_bytes
7609 || detect_info
.found
)
7611 if (coding
.head_ascii
== coding
.src_bytes
)
7612 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7613 for (i
= 0; i
< coding_category_raw_text
; i
++)
7615 category
= coding_priorities
[i
];
7616 this = coding_categories
+ category
;
7617 if (detect_info
.found
& (1 << category
))
7622 if (null_byte_found
)
7624 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
7625 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
7627 for (i
= 0; i
< coding_category_raw_text
; i
++)
7629 category
= coding_priorities
[i
];
7630 this = coding_categories
+ category
;
7634 /* No coding system of this category is defined. */
7635 detect_info
.rejected
|= (1 << category
);
7637 else if (category
>= coding_category_raw_text
)
7639 else if (detect_info
.checked
& (1 << category
))
7642 && (detect_info
.found
& (1 << category
)))
7645 else if ((*(this->detector
)) (&coding
, &detect_info
)
7647 && (detect_info
.found
& (1 << category
)))
7649 if (category
== coding_category_utf_16_auto
)
7651 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7652 category
= coding_category_utf_16_le
;
7654 category
= coding_category_utf_16_be
;
7662 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
)
7664 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7665 id
= coding_categories
[coding_category_raw_text
].id
;
7666 val
= Fcons (make_number (id
), Qnil
);
7668 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7670 detect_info
.found
= CATEGORY_MASK_ANY
;
7671 id
= coding_categories
[coding_category_undecided
].id
;
7672 val
= Fcons (make_number (id
), Qnil
);
7676 if (detect_info
.found
)
7678 detect_info
.found
= 1 << category
;
7679 val
= Fcons (make_number (this->id
), Qnil
);
7682 for (i
= 0; i
< coding_category_raw_text
; i
++)
7683 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7685 detect_info
.found
= 1 << coding_priorities
[i
];
7686 id
= coding_categories
[coding_priorities
[i
]].id
;
7687 val
= Fcons (make_number (id
), Qnil
);
7693 int mask
= detect_info
.rejected
| detect_info
.found
;
7697 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7699 category
= coding_priorities
[i
];
7700 if (! (mask
& (1 << category
)))
7702 found
|= 1 << category
;
7703 id
= coding_categories
[category
].id
;
7705 val
= Fcons (make_number (id
), val
);
7708 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7710 category
= coding_priorities
[i
];
7711 if (detect_info
.found
& (1 << category
))
7713 id
= coding_categories
[category
].id
;
7714 val
= Fcons (make_number (id
), val
);
7717 detect_info
.found
|= found
;
7720 else if (base_category
== coding_category_utf_16_auto
)
7722 if (detect_coding_utf_16 (&coding
, &detect_info
))
7724 struct coding_system
*this;
7726 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7727 this = coding_categories
+ coding_category_utf_16_le
;
7728 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7729 this = coding_categories
+ coding_category_utf_16_be
;
7730 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7731 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7733 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7734 val
= Fcons (make_number (this->id
), Qnil
);
7739 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7740 val
= Fcons (make_number (coding
.id
), Qnil
);
7743 /* Then, detect eol-format if necessary. */
7745 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7748 if (VECTORP (eol_type
))
7750 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7752 if (null_byte_found
)
7753 normal_eol
= EOL_SEEN_LF
;
7755 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7756 coding_category_raw_text
);
7758 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7759 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7760 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7761 coding_category_utf_16_be
);
7762 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7763 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7764 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7765 coding_category_utf_16_le
);
7769 if (EQ (eol_type
, Qunix
))
7770 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7771 else if (EQ (eol_type
, Qdos
))
7772 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7774 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7777 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7779 enum coding_category category
;
7782 id
= XINT (XCAR (tail
));
7783 attrs
= CODING_ID_ATTRS (id
);
7784 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7785 eol_type
= CODING_ID_EOL_TYPE (id
);
7786 if (VECTORP (eol_type
))
7788 if (category
== coding_category_utf_16_be
7789 || category
== coding_category_utf_16_be_nosig
)
7790 this_eol
= utf_16_be_eol
;
7791 else if (category
== coding_category_utf_16_le
7792 || category
== coding_category_utf_16_le_nosig
)
7793 this_eol
= utf_16_le_eol
;
7795 this_eol
= normal_eol
;
7797 if (this_eol
== EOL_SEEN_LF
)
7798 XSETCAR (tail
, AREF (eol_type
, 0));
7799 else if (this_eol
== EOL_SEEN_CRLF
)
7800 XSETCAR (tail
, AREF (eol_type
, 1));
7801 else if (this_eol
== EOL_SEEN_CR
)
7802 XSETCAR (tail
, AREF (eol_type
, 2));
7804 XSETCAR (tail
, CODING_ID_NAME (id
));
7807 XSETCAR (tail
, CODING_ID_NAME (id
));
7811 return (highest
? XCAR (val
) : val
);
7815 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7817 doc
: /* Detect coding system of the text in the region between START and END.
7818 Return a list of possible coding systems ordered by priority.
7820 If only ASCII characters are found (except for such ISO-2022 control
7821 characters as ESC), it returns a list of single element `undecided'
7822 or its subsidiary coding system according to a detected end-of-line
7825 If optional argument HIGHEST is non-nil, return the coding system of
7826 highest priority. */)
7827 (start
, end
, highest
)
7828 Lisp_Object start
, end
, highest
;
7831 int from_byte
, to_byte
;
7833 CHECK_NUMBER_COERCE_MARKER (start
);
7834 CHECK_NUMBER_COERCE_MARKER (end
);
7836 validate_region (&start
, &end
);
7837 from
= XINT (start
), to
= XINT (end
);
7838 from_byte
= CHAR_TO_BYTE (from
);
7839 to_byte
= CHAR_TO_BYTE (to
);
7841 if (from
< GPT
&& to
>= GPT
)
7842 move_gap_both (to
, to_byte
);
7844 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7845 to
- from
, to_byte
- from_byte
,
7847 !NILP (current_buffer
7848 ->enable_multibyte_characters
),
7852 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7854 doc
: /* Detect coding system of the text in STRING.
7855 Return a list of possible coding systems ordered by priority.
7857 If only ASCII characters are found (except for such ISO-2022 control
7858 characters as ESC), it returns a list of single element `undecided'
7859 or its subsidiary coding system according to a detected end-of-line
7862 If optional argument HIGHEST is non-nil, return the coding system of
7863 highest priority. */)
7865 Lisp_Object string
, highest
;
7867 CHECK_STRING (string
);
7869 return detect_coding_system (SDATA (string
),
7870 SCHARS (string
), SBYTES (string
),
7871 !NILP (highest
), STRING_MULTIBYTE (string
),
7877 char_encodable_p (c
, attrs
)
7882 struct charset
*charset
;
7883 Lisp_Object translation_table
;
7885 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7886 if (! NILP (translation_table
))
7887 c
= translate_char (translation_table
, c
);
7888 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7889 CONSP (tail
); tail
= XCDR (tail
))
7891 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7892 if (CHAR_CHARSET_P (c
, charset
))
7895 return (! NILP (tail
));
7899 /* Return a list of coding systems that safely encode the text between
7900 START and END. If EXCLUDE is non-nil, it is a list of coding
7901 systems not to check. The returned list doesn't contain any such
7902 coding systems. In any case, if the text contains only ASCII or is
7903 unibyte, return t. */
7905 DEFUN ("find-coding-systems-region-internal",
7906 Ffind_coding_systems_region_internal
,
7907 Sfind_coding_systems_region_internal
, 2, 3, 0,
7908 doc
: /* Internal use only. */)
7909 (start
, end
, exclude
)
7910 Lisp_Object start
, end
, exclude
;
7912 Lisp_Object coding_attrs_list
, safe_codings
;
7913 EMACS_INT start_byte
, end_byte
;
7914 const unsigned char *p
, *pbeg
, *pend
;
7916 Lisp_Object tail
, elt
;
7918 if (STRINGP (start
))
7920 if (!STRING_MULTIBYTE (start
)
7921 || SCHARS (start
) == SBYTES (start
))
7924 end_byte
= SBYTES (start
);
7928 CHECK_NUMBER_COERCE_MARKER (start
);
7929 CHECK_NUMBER_COERCE_MARKER (end
);
7930 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7931 args_out_of_range (start
, end
);
7932 if (NILP (current_buffer
->enable_multibyte_characters
))
7934 start_byte
= CHAR_TO_BYTE (XINT (start
));
7935 end_byte
= CHAR_TO_BYTE (XINT (end
));
7936 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7939 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7941 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7942 move_gap_both (XINT (start
), start_byte
);
7944 move_gap_both (XINT (end
), end_byte
);
7948 coding_attrs_list
= Qnil
;
7949 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7951 || NILP (Fmemq (XCAR (tail
), exclude
)))
7955 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7956 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7957 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7959 ASET (attrs
, coding_attr_trans_tbl
,
7960 get_translation_table (attrs
, 1, NULL
));
7961 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7965 if (STRINGP (start
))
7966 p
= pbeg
= SDATA (start
);
7968 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7969 pend
= p
+ (end_byte
- start_byte
);
7971 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7972 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7976 if (ASCII_BYTE_P (*p
))
7980 c
= STRING_CHAR_ADVANCE (p
);
7982 charset_map_loaded
= 0;
7983 for (tail
= coding_attrs_list
; CONSP (tail
);)
7988 else if (char_encodable_p (c
, elt
))
7990 else if (CONSP (XCDR (tail
)))
7992 XSETCAR (tail
, XCAR (XCDR (tail
)));
7993 XSETCDR (tail
, XCDR (XCDR (tail
)));
7997 XSETCAR (tail
, Qnil
);
8001 if (charset_map_loaded
)
8003 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8005 if (STRINGP (start
))
8006 pbeg
= SDATA (start
);
8008 pbeg
= BYTE_POS_ADDR (start_byte
);
8009 p
= pbeg
+ p_offset
;
8010 pend
= pbeg
+ pend_offset
;
8015 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8016 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8017 if (! NILP (XCAR (tail
)))
8018 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8020 return safe_codings
;
8024 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8025 Sunencodable_char_position
, 3, 5, 0,
8027 Return position of first un-encodable character in a region.
8028 START and END specify the region and CODING-SYSTEM specifies the
8029 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8031 If optional 4th argument COUNT is non-nil, it specifies at most how
8032 many un-encodable characters to search. In this case, the value is a
8035 If optional 5th argument STRING is non-nil, it is a string to search
8036 for un-encodable characters. In that case, START and END are indexes
8038 (start
, end
, coding_system
, count
, string
)
8039 Lisp_Object start
, end
, coding_system
, count
, string
;
8042 struct coding_system coding
;
8043 Lisp_Object attrs
, charset_list
, translation_table
;
8044 Lisp_Object positions
;
8046 const unsigned char *p
, *stop
, *pend
;
8047 int ascii_compatible
;
8049 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8050 attrs
= CODING_ID_ATTRS (coding
.id
);
8051 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8053 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8054 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8055 translation_table
= get_translation_table (attrs
, 1, NULL
);
8059 validate_region (&start
, &end
);
8060 from
= XINT (start
);
8062 if (NILP (current_buffer
->enable_multibyte_characters
)
8063 || (ascii_compatible
8064 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8066 p
= CHAR_POS_ADDR (from
);
8067 pend
= CHAR_POS_ADDR (to
);
8068 if (from
< GPT
&& to
>= GPT
)
8075 CHECK_STRING (string
);
8076 CHECK_NATNUM (start
);
8078 from
= XINT (start
);
8081 || to
> SCHARS (string
))
8082 args_out_of_range_3 (string
, start
, end
);
8083 if (! STRING_MULTIBYTE (string
))
8085 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8086 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8087 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8095 CHECK_NATNUM (count
);
8104 if (ascii_compatible
)
8105 while (p
< stop
&& ASCII_BYTE_P (*p
))
8115 c
= STRING_CHAR_ADVANCE (p
);
8116 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8117 && ! char_charset (translate_char (translation_table
, c
),
8118 charset_list
, NULL
))
8120 positions
= Fcons (make_number (from
), positions
);
8129 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8133 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8134 Scheck_coding_systems_region
, 3, 3, 0,
8135 doc
: /* Check if the region is encodable by coding systems.
8137 START and END are buffer positions specifying the region.
8138 CODING-SYSTEM-LIST is a list of coding systems to check.
8140 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8141 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8142 whole region, POS0, POS1, ... are buffer positions where non-encodable
8143 characters are found.
8145 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8148 START may be a string. In that case, check if the string is
8149 encodable, and the value contains indices to the string instead of
8150 buffer positions. END is ignored. */)
8151 (start
, end
, coding_system_list
)
8152 Lisp_Object start
, end
, coding_system_list
;
8155 EMACS_INT start_byte
, end_byte
;
8157 const unsigned char *p
, *pbeg
, *pend
;
8159 Lisp_Object tail
, elt
, attrs
;
8161 if (STRINGP (start
))
8163 if (!STRING_MULTIBYTE (start
)
8164 && SCHARS (start
) != SBYTES (start
))
8167 end_byte
= SBYTES (start
);
8172 CHECK_NUMBER_COERCE_MARKER (start
);
8173 CHECK_NUMBER_COERCE_MARKER (end
);
8174 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8175 args_out_of_range (start
, end
);
8176 if (NILP (current_buffer
->enable_multibyte_characters
))
8178 start_byte
= CHAR_TO_BYTE (XINT (start
));
8179 end_byte
= CHAR_TO_BYTE (XINT (end
));
8180 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8183 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8185 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8186 move_gap_both (XINT (start
), start_byte
);
8188 move_gap_both (XINT (end
), end_byte
);
8194 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8197 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8198 ASET (attrs
, coding_attr_trans_tbl
,
8199 get_translation_table (attrs
, 1, NULL
));
8200 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8203 if (STRINGP (start
))
8204 p
= pbeg
= SDATA (start
);
8206 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8207 pend
= p
+ (end_byte
- start_byte
);
8209 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8210 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8214 if (ASCII_BYTE_P (*p
))
8218 c
= STRING_CHAR_ADVANCE (p
);
8220 charset_map_loaded
= 0;
8221 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8223 elt
= XCDR (XCAR (tail
));
8224 if (! char_encodable_p (c
, XCAR (elt
)))
8225 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8227 if (charset_map_loaded
)
8229 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8231 if (STRINGP (start
))
8232 pbeg
= SDATA (start
);
8234 pbeg
= BYTE_POS_ADDR (start_byte
);
8235 p
= pbeg
+ p_offset
;
8236 pend
= pbeg
+ pend_offset
;
8244 for (; CONSP (tail
); tail
= XCDR (tail
))
8247 if (CONSP (XCDR (XCDR (elt
))))
8248 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8257 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
8258 Lisp_Object start
, end
, coding_system
, dst_object
;
8259 int encodep
, norecord
;
8261 struct coding_system coding
;
8262 EMACS_INT from
, from_byte
, to
, to_byte
;
8263 Lisp_Object src_object
;
8265 CHECK_NUMBER_COERCE_MARKER (start
);
8266 CHECK_NUMBER_COERCE_MARKER (end
);
8267 if (NILP (coding_system
))
8268 coding_system
= Qno_conversion
;
8270 CHECK_CODING_SYSTEM (coding_system
);
8271 src_object
= Fcurrent_buffer ();
8272 if (NILP (dst_object
))
8273 dst_object
= src_object
;
8274 else if (! EQ (dst_object
, Qt
))
8275 CHECK_BUFFER (dst_object
);
8277 validate_region (&start
, &end
);
8278 from
= XFASTINT (start
);
8279 from_byte
= CHAR_TO_BYTE (from
);
8280 to
= XFASTINT (end
);
8281 to_byte
= CHAR_TO_BYTE (to
);
8283 setup_coding_system (coding_system
, &coding
);
8284 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8287 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8290 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8293 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8295 return (BUFFERP (dst_object
)
8296 ? make_number (coding
.produced_char
)
8297 : coding
.dst_object
);
8301 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8302 3, 4, "r\nzCoding system: ",
8303 doc
: /* Decode the current region from the specified coding system.
8304 When called from a program, takes four arguments:
8305 START, END, CODING-SYSTEM, and DESTINATION.
8306 START and END are buffer positions.
8308 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8309 If nil, the region between START and END is replaced by the decoded text.
8310 If buffer, the decoded text is inserted in the buffer.
8311 If t, the decoded text is returned.
8313 This function sets `last-coding-system-used' to the precise coding system
8314 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8315 not fully specified.)
8316 It returns the length of the decoded text. */)
8317 (start
, end
, coding_system
, destination
)
8318 Lisp_Object start
, end
, coding_system
, destination
;
8320 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8323 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8324 3, 4, "r\nzCoding system: ",
8325 doc
: /* Encode the current region by specified coding system.
8326 When called from a program, takes four arguments:
8327 START, END, CODING-SYSTEM and DESTINATION.
8328 START and END are buffer positions.
8330 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8331 If nil, the region between START and END is replace by the encoded text.
8332 If buffer, the encoded text is inserted in the buffer.
8333 If t, the encoded text is returned.
8335 This function sets `last-coding-system-used' to the precise coding system
8336 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8337 not fully specified.)
8338 It returns the length of the encoded text. */)
8339 (start
, end
, coding_system
, destination
)
8340 Lisp_Object start
, end
, coding_system
, destination
;
8342 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8346 code_convert_string (string
, coding_system
, dst_object
,
8347 encodep
, nocopy
, norecord
)
8348 Lisp_Object string
, coding_system
, dst_object
;
8349 int encodep
, nocopy
, norecord
;
8351 struct coding_system coding
;
8352 EMACS_INT chars
, bytes
;
8354 CHECK_STRING (string
);
8355 if (NILP (coding_system
))
8358 Vlast_coding_system_used
= Qno_conversion
;
8359 if (NILP (dst_object
))
8360 return (nocopy
? Fcopy_sequence (string
) : string
);
8363 if (NILP (coding_system
))
8364 coding_system
= Qno_conversion
;
8366 CHECK_CODING_SYSTEM (coding_system
);
8367 if (NILP (dst_object
))
8369 else if (! EQ (dst_object
, Qt
))
8370 CHECK_BUFFER (dst_object
);
8372 setup_coding_system (coding_system
, &coding
);
8373 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8374 chars
= SCHARS (string
);
8375 bytes
= SBYTES (string
);
8377 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8379 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8381 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8383 return (BUFFERP (dst_object
)
8384 ? make_number (coding
.produced_char
)
8385 : coding
.dst_object
);
8389 /* Encode or decode STRING according to CODING_SYSTEM.
8390 Do not set Vlast_coding_system_used.
8392 This function is called only from macros DECODE_FILE and
8393 ENCODE_FILE, thus we ignore character composition. */
8396 code_convert_string_norecord (string
, coding_system
, encodep
)
8397 Lisp_Object string
, coding_system
;
8400 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8404 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8406 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8408 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8409 if the decoding operation is trivial.
8411 Optional fourth arg BUFFER non-nil means that the decoded text is
8412 inserted in BUFFER instead of returned as a string. In this case,
8413 the return value is BUFFER.
8415 This function sets `last-coding-system-used' to the precise coding system
8416 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8417 not fully specified.) */)
8418 (string
, coding_system
, nocopy
, buffer
)
8419 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8421 return code_convert_string (string
, coding_system
, buffer
,
8422 0, ! NILP (nocopy
), 0);
8425 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8427 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8429 Optional third arg NOCOPY non-nil means it is OK to return STRING
8430 itself if the encoding operation is trivial.
8432 Optional fourth arg BUFFER non-nil means that the encoded text is
8433 inserted in BUFFER instead of returned as a string. In this case,
8434 the return value is BUFFER.
8436 This function sets `last-coding-system-used' to the precise coding system
8437 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8438 not fully specified.) */)
8439 (string
, coding_system
, nocopy
, buffer
)
8440 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8442 return code_convert_string (string
, coding_system
, buffer
,
8443 1, ! NILP (nocopy
), 1);
8447 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8448 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8449 Return the corresponding character. */)
8453 Lisp_Object spec
, attrs
, val
;
8454 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8457 CHECK_NATNUM (code
);
8458 c
= XFASTINT (code
);
8459 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8460 attrs
= AREF (spec
, 0);
8462 if (ASCII_BYTE_P (c
)
8463 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8466 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8467 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8468 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8469 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8472 charset
= charset_roman
;
8473 else if (c
>= 0xA0 && c
< 0xDF)
8475 charset
= charset_kana
;
8480 int s1
= c
>> 8, s2
= c
& 0xFF;
8482 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8483 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8484 error ("Invalid code: %d", code
);
8486 charset
= charset_kanji
;
8488 c
= DECODE_CHAR (charset
, c
);
8490 error ("Invalid code: %d", code
);
8491 return make_number (c
);
8495 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8496 doc
: /* Encode a Japanese character CH to shift_jis encoding.
8497 Return the corresponding code in SJIS. */)
8501 Lisp_Object spec
, attrs
, charset_list
;
8503 struct charset
*charset
;
8506 CHECK_CHARACTER (ch
);
8508 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8509 attrs
= AREF (spec
, 0);
8511 if (ASCII_CHAR_P (c
)
8512 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8515 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8516 charset
= char_charset (c
, charset_list
, &code
);
8517 if (code
== CHARSET_INVALID_CODE (charset
))
8518 error ("Can't encode by shift_jis encoding: %d", c
);
8521 return make_number (code
);
8524 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8525 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8526 Return the corresponding character. */)
8530 Lisp_Object spec
, attrs
, val
;
8531 struct charset
*charset_roman
, *charset_big5
, *charset
;
8534 CHECK_NATNUM (code
);
8535 c
= XFASTINT (code
);
8536 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8537 attrs
= AREF (spec
, 0);
8539 if (ASCII_BYTE_P (c
)
8540 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8543 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8544 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8545 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8548 charset
= charset_roman
;
8551 int b1
= c
>> 8, b2
= c
& 0x7F;
8552 if (b1
< 0xA1 || b1
> 0xFE
8553 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8554 error ("Invalid code: %d", code
);
8555 charset
= charset_big5
;
8557 c
= DECODE_CHAR (charset
, (unsigned )c
);
8559 error ("Invalid code: %d", code
);
8560 return make_number (c
);
8563 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8564 doc
: /* Encode the Big5 character CH to BIG5 coding system.
8565 Return the corresponding character code in Big5. */)
8569 Lisp_Object spec
, attrs
, charset_list
;
8570 struct charset
*charset
;
8574 CHECK_CHARACTER (ch
);
8576 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8577 attrs
= AREF (spec
, 0);
8578 if (ASCII_CHAR_P (c
)
8579 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8582 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8583 charset
= char_charset (c
, charset_list
, &code
);
8584 if (code
== CHARSET_INVALID_CODE (charset
))
8585 error ("Can't encode by Big5 encoding: %d", c
);
8587 return make_number (code
);
8591 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
8592 Sset_terminal_coding_system_internal
, 1, 2, 0,
8593 doc
: /* Internal use only. */)
8594 (coding_system
, terminal
)
8595 Lisp_Object coding_system
;
8596 Lisp_Object terminal
;
8598 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
8599 CHECK_SYMBOL (coding_system
);
8600 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
8601 /* We had better not send unsafe characters to terminal. */
8602 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
8603 /* Characer composition should be disabled. */
8604 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8605 terminal_coding
->src_multibyte
= 1;
8606 terminal_coding
->dst_multibyte
= 0;
8610 DEFUN ("set-safe-terminal-coding-system-internal",
8611 Fset_safe_terminal_coding_system_internal
,
8612 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8613 doc
: /* Internal use only. */)
8615 Lisp_Object coding_system
;
8617 CHECK_SYMBOL (coding_system
);
8618 setup_coding_system (Fcheck_coding_system (coding_system
),
8619 &safe_terminal_coding
);
8620 /* Characer composition should be disabled. */
8621 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8622 safe_terminal_coding
.src_multibyte
= 1;
8623 safe_terminal_coding
.dst_multibyte
= 0;
8627 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
8628 Sterminal_coding_system
, 0, 1, 0,
8629 doc
: /* Return coding system specified for terminal output on the given terminal.
8630 TERMINAL may be a terminal id, a frame, or nil for the selected
8631 frame's terminal device. */)
8633 Lisp_Object terminal
;
8635 struct coding_system
*terminal_coding
8636 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
8637 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
8639 /* For backward compatibility, return nil if it is `undecided'. */
8640 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
8643 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
8644 Sset_keyboard_coding_system_internal
, 1, 2, 0,
8645 doc
: /* Internal use only. */)
8646 (coding_system
, terminal
)
8647 Lisp_Object coding_system
;
8648 Lisp_Object terminal
;
8650 struct terminal
*t
= get_terminal (terminal
, 1);
8651 CHECK_SYMBOL (coding_system
);
8652 setup_coding_system (Fcheck_coding_system (coding_system
),
8653 TERMINAL_KEYBOARD_CODING (t
));
8654 /* Characer composition should be disabled. */
8655 TERMINAL_KEYBOARD_CODING (t
)->common_flags
8656 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8660 DEFUN ("keyboard-coding-system",
8661 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
8662 doc
: /* Return coding system specified for decoding keyboard input. */)
8664 Lisp_Object terminal
;
8666 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
8667 (get_terminal (terminal
, 1))->id
);
8671 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8672 Sfind_operation_coding_system
, 1, MANY
, 0,
8673 doc
: /* Choose a coding system for an operation based on the target name.
8674 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8675 DECODING-SYSTEM is the coding system to use for decoding
8676 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8677 for encoding (in case OPERATION does encoding).
8679 The first argument OPERATION specifies an I/O primitive:
8680 For file I/O, `insert-file-contents' or `write-region'.
8681 For process I/O, `call-process', `call-process-region', or `start-process'.
8682 For network I/O, `open-network-stream'.
8684 The remaining arguments should be the same arguments that were passed
8685 to the primitive. Depending on which primitive, one of those arguments
8686 is selected as the TARGET. For example, if OPERATION does file I/O,
8687 whichever argument specifies the file name is TARGET.
8689 TARGET has a meaning which depends on OPERATION:
8690 For file I/O, TARGET is a file name (except for the special case below).
8691 For process I/O, TARGET is a process name.
8692 For network I/O, TARGET is a service name or a port number.
8694 This function looks up what is specified for TARGET in
8695 `file-coding-system-alist', `process-coding-system-alist',
8696 or `network-coding-system-alist' depending on OPERATION.
8697 They may specify a coding system, a cons of coding systems,
8698 or a function symbol to call.
8699 In the last case, we call the function with one argument,
8700 which is a list of all the arguments given to this function.
8701 If the function can't decide a coding system, it can return
8702 `undecided' so that the normal code-detection is performed.
8704 If OPERATION is `insert-file-contents', the argument corresponding to
8705 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8706 file name to look up, and BUFFER is a buffer that contains the file's
8707 contents (not yet decoded). If `file-coding-system-alist' specifies a
8708 function to call for FILENAME, that function should examine the
8709 contents of BUFFER instead of reading the file.
8711 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
8716 Lisp_Object operation
, target_idx
, target
, val
;
8717 register Lisp_Object chain
;
8720 error ("Too few arguments");
8721 operation
= args
[0];
8722 if (!SYMBOLP (operation
)
8723 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8724 error ("Invalid first argument");
8725 if (nargs
< 1 + XINT (target_idx
))
8726 error ("Too few arguments for operation: %s",
8727 SDATA (SYMBOL_NAME (operation
)));
8728 target
= args
[XINT (target_idx
) + 1];
8729 if (!(STRINGP (target
)
8730 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
8731 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
8732 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8733 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8735 target
= XCAR (target
);
8737 chain
= ((EQ (operation
, Qinsert_file_contents
)
8738 || EQ (operation
, Qwrite_region
))
8739 ? Vfile_coding_system_alist
8740 : (EQ (operation
, Qopen_network_stream
)
8741 ? Vnetwork_coding_system_alist
8742 : Vprocess_coding_system_alist
));
8746 for (; CONSP (chain
); chain
= XCDR (chain
))
8752 && ((STRINGP (target
)
8753 && STRINGP (XCAR (elt
))
8754 && fast_string_match (XCAR (elt
), target
) >= 0)
8755 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8758 /* Here, if VAL is both a valid coding system and a valid
8759 function symbol, we return VAL as a coding system. */
8762 if (! SYMBOLP (val
))
8764 if (! NILP (Fcoding_system_p (val
)))
8765 return Fcons (val
, val
);
8766 if (! NILP (Ffboundp (val
)))
8768 /* We use call1 rather than safe_call1
8769 so as to get bug reports about functions called here
8770 which don't handle the current interface. */
8771 val
= call1 (val
, Flist (nargs
, args
));
8774 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8775 return Fcons (val
, val
);
8783 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8784 Sset_coding_system_priority
, 0, MANY
, 0,
8785 doc
: /* Assign higher priority to the coding systems given as arguments.
8786 If multiple coding systems belong to the same category,
8787 all but the first one are ignored.
8789 usage: (set-coding-system-priority &rest coding-systems) */)
8795 int changed
[coding_category_max
];
8796 enum coding_category priorities
[coding_category_max
];
8798 bzero (changed
, sizeof changed
);
8800 for (i
= j
= 0; i
< nargs
; i
++)
8802 enum coding_category category
;
8803 Lisp_Object spec
, attrs
;
8805 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8806 attrs
= AREF (spec
, 0);
8807 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8808 if (changed
[category
])
8809 /* Ignore this coding system because a coding system of the
8810 same category already had a higher priority. */
8812 changed
[category
] = 1;
8813 priorities
[j
++] = category
;
8814 if (coding_categories
[category
].id
>= 0
8815 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8816 setup_coding_system (args
[i
], &coding_categories
[category
]);
8817 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8820 /* Now we have decided top J priorities. Reflect the order of the
8821 original priorities to the remaining priorities. */
8823 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8825 while (j
< coding_category_max
8826 && changed
[coding_priorities
[j
]])
8828 if (j
== coding_category_max
)
8830 priorities
[i
] = coding_priorities
[j
];
8833 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8835 /* Update `coding-category-list'. */
8836 Vcoding_category_list
= Qnil
;
8837 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8838 Vcoding_category_list
8839 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8840 Vcoding_category_list
);
8845 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8846 Scoding_system_priority_list
, 0, 1, 0,
8847 doc
: /* Return a list of coding systems ordered by their priorities.
8848 HIGHESTP non-nil means just return the highest priority one. */)
8850 Lisp_Object highestp
;
8855 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8857 enum coding_category category
= coding_priorities
[i
];
8858 int id
= coding_categories
[category
].id
;
8863 attrs
= CODING_ID_ATTRS (id
);
8864 if (! NILP (highestp
))
8865 return CODING_ATTR_BASE_NAME (attrs
);
8866 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8868 return Fnreverse (val
);
8871 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8874 make_subsidiaries (base
)
8877 Lisp_Object subsidiaries
;
8878 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8879 char *buf
= (char *) alloca (base_name_len
+ 6);
8882 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8883 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8884 for (i
= 0; i
< 3; i
++)
8886 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8887 ASET (subsidiaries
, i
, intern (buf
));
8889 return subsidiaries
;
8893 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8894 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8895 doc
: /* For internal use only.
8896 usage: (define-coding-system-internal ...) */)
8902 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8903 Lisp_Object attrs
; /* Vector of attributes. */
8904 Lisp_Object eol_type
;
8905 Lisp_Object aliases
;
8906 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8907 enum coding_category category
;
8908 Lisp_Object tail
, val
;
8909 int max_charset_id
= 0;
8912 if (nargs
< coding_arg_max
)
8915 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8917 name
= args
[coding_arg_name
];
8918 CHECK_SYMBOL (name
);
8919 CODING_ATTR_BASE_NAME (attrs
) = name
;
8921 val
= args
[coding_arg_mnemonic
];
8922 if (! STRINGP (val
))
8923 CHECK_CHARACTER (val
);
8924 CODING_ATTR_MNEMONIC (attrs
) = val
;
8926 coding_type
= args
[coding_arg_coding_type
];
8927 CHECK_SYMBOL (coding_type
);
8928 CODING_ATTR_TYPE (attrs
) = coding_type
;
8930 charset_list
= args
[coding_arg_charset_list
];
8931 if (SYMBOLP (charset_list
))
8933 if (EQ (charset_list
, Qiso_2022
))
8935 if (! EQ (coding_type
, Qiso_2022
))
8936 error ("Invalid charset-list");
8937 charset_list
= Viso_2022_charset_list
;
8939 else if (EQ (charset_list
, Qemacs_mule
))
8941 if (! EQ (coding_type
, Qemacs_mule
))
8942 error ("Invalid charset-list");
8943 charset_list
= Vemacs_mule_charset_list
;
8945 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8946 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8947 max_charset_id
= XFASTINT (XCAR (tail
));
8951 charset_list
= Fcopy_sequence (charset_list
);
8952 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8954 struct charset
*charset
;
8957 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8958 if (EQ (coding_type
, Qiso_2022
)
8959 ? CHARSET_ISO_FINAL (charset
) < 0
8960 : EQ (coding_type
, Qemacs_mule
)
8961 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8963 error ("Can't handle charset `%s'",
8964 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8966 XSETCAR (tail
, make_number (charset
->id
));
8967 if (max_charset_id
< charset
->id
)
8968 max_charset_id
= charset
->id
;
8971 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8973 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8975 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8976 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8977 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8979 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8981 val
= args
[coding_arg_decode_translation_table
];
8982 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8984 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8986 val
= args
[coding_arg_encode_translation_table
];
8987 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8989 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8991 val
= args
[coding_arg_post_read_conversion
];
8993 CODING_ATTR_POST_READ (attrs
) = val
;
8995 val
= args
[coding_arg_pre_write_conversion
];
8997 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8999 val
= args
[coding_arg_default_char
];
9001 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9004 CHECK_CHARACTER (val
);
9005 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9008 val
= args
[coding_arg_for_unibyte
];
9009 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9011 val
= args
[coding_arg_plist
];
9013 CODING_ATTR_PLIST (attrs
) = val
;
9015 if (EQ (coding_type
, Qcharset
))
9017 /* Generate a lisp vector of 256 elements. Each element is nil,
9018 integer, or a list of charset IDs.
9020 If Nth element is nil, the byte code N is invalid in this
9023 If Nth element is a number NUM, N is the first byte of a
9024 charset whose ID is NUM.
9026 If Nth element is a list of charset IDs, N is the first byte
9027 of one of them. The list is sorted by dimensions of the
9028 charsets. A charset of smaller dimension comes firtst. */
9029 val
= Fmake_vector (make_number (256), Qnil
);
9031 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9033 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9034 int dim
= CHARSET_DIMENSION (charset
);
9035 int idx
= (dim
- 1) * 4;
9037 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9038 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9040 for (i
= charset
->code_space
[idx
];
9041 i
<= charset
->code_space
[idx
+ 1]; i
++)
9043 Lisp_Object tmp
, tmp2
;
9046 tmp
= AREF (val
, i
);
9049 else if (NUMBERP (tmp
))
9051 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9053 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9055 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9059 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9061 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9066 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9069 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9070 XSETCAR (tmp2
, XCAR (tail
));
9076 ASET (attrs
, coding_attr_charset_valids
, val
);
9077 category
= coding_category_charset
;
9079 else if (EQ (coding_type
, Qccl
))
9083 if (nargs
< coding_arg_ccl_max
)
9086 val
= args
[coding_arg_ccl_decoder
];
9087 CHECK_CCL_PROGRAM (val
);
9089 val
= Fcopy_sequence (val
);
9090 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9092 val
= args
[coding_arg_ccl_encoder
];
9093 CHECK_CCL_PROGRAM (val
);
9095 val
= Fcopy_sequence (val
);
9096 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9098 val
= args
[coding_arg_ccl_valids
];
9099 valids
= Fmake_string (make_number (256), make_number (0));
9100 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9107 from
= to
= XINT (val
);
9108 if (from
< 0 || from
> 255)
9109 args_out_of_range_3 (val
, make_number (0), make_number (255));
9114 CHECK_NATNUM_CAR (val
);
9115 CHECK_NATNUM_CDR (val
);
9116 from
= XINT (XCAR (val
));
9118 args_out_of_range_3 (XCAR (val
),
9119 make_number (0), make_number (255));
9120 to
= XINT (XCDR (val
));
9121 if (to
< from
|| to
> 255)
9122 args_out_of_range_3 (XCDR (val
),
9123 XCAR (val
), make_number (255));
9125 for (i
= from
; i
<= to
; i
++)
9126 SSET (valids
, i
, 1);
9128 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9130 category
= coding_category_ccl
;
9132 else if (EQ (coding_type
, Qutf_16
))
9134 Lisp_Object bom
, endian
;
9136 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9138 if (nargs
< coding_arg_utf16_max
)
9141 bom
= args
[coding_arg_utf16_bom
];
9142 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9146 CHECK_CODING_SYSTEM (val
);
9148 CHECK_CODING_SYSTEM (val
);
9150 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
9152 endian
= args
[coding_arg_utf16_endian
];
9153 CHECK_SYMBOL (endian
);
9156 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9157 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9158 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9160 category
= (CONSP (bom
)
9161 ? coding_category_utf_16_auto
9163 ? (EQ (endian
, Qbig
)
9164 ? coding_category_utf_16_be_nosig
9165 : coding_category_utf_16_le_nosig
)
9166 : (EQ (endian
, Qbig
)
9167 ? coding_category_utf_16_be
9168 : coding_category_utf_16_le
));
9170 else if (EQ (coding_type
, Qiso_2022
))
9172 Lisp_Object initial
, reg_usage
, request
, flags
;
9175 if (nargs
< coding_arg_iso2022_max
)
9178 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9179 CHECK_VECTOR (initial
);
9180 for (i
= 0; i
< 4; i
++)
9182 val
= Faref (initial
, make_number (i
));
9185 struct charset
*charset
;
9187 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9188 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9189 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9190 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9193 ASET (initial
, i
, make_number (-1));
9196 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9197 CHECK_CONS (reg_usage
);
9198 CHECK_NUMBER_CAR (reg_usage
);
9199 CHECK_NUMBER_CDR (reg_usage
);
9201 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9202 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9210 CHECK_CHARSET_GET_ID (tmp
, id
);
9211 CHECK_NATNUM_CDR (val
);
9212 if (XINT (XCDR (val
)) >= 4)
9213 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9214 XSETCAR (val
, make_number (id
));
9217 flags
= args
[coding_arg_iso2022_flags
];
9218 CHECK_NATNUM (flags
);
9220 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9221 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9223 ASET (attrs
, coding_attr_iso_initial
, initial
);
9224 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9225 ASET (attrs
, coding_attr_iso_request
, request
);
9226 ASET (attrs
, coding_attr_iso_flags
, flags
);
9227 setup_iso_safe_charsets (attrs
);
9229 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9230 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9231 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9232 ? coding_category_iso_7_else
9233 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9234 ? coding_category_iso_7
9235 : coding_category_iso_7_tight
);
9238 int id
= XINT (AREF (initial
, 1));
9240 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9241 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9243 ? coding_category_iso_8_else
9244 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9245 ? coding_category_iso_8_1
9246 : coding_category_iso_8_2
);
9248 if (category
!= coding_category_iso_8_1
9249 && category
!= coding_category_iso_8_2
)
9250 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9252 else if (EQ (coding_type
, Qemacs_mule
))
9254 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9255 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9256 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9257 category
= coding_category_emacs_mule
;
9259 else if (EQ (coding_type
, Qshift_jis
))
9262 struct charset
*charset
;
9264 if (XINT (Flength (charset_list
)) != 3
9265 && XINT (Flength (charset_list
)) != 4)
9266 error ("There should be three or four charsets");
9268 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9269 if (CHARSET_DIMENSION (charset
) != 1)
9270 error ("Dimension of charset %s is not one",
9271 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9272 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9273 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9275 charset_list
= XCDR (charset_list
);
9276 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9277 if (CHARSET_DIMENSION (charset
) != 1)
9278 error ("Dimension of charset %s is not one",
9279 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9281 charset_list
= XCDR (charset_list
);
9282 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9283 if (CHARSET_DIMENSION (charset
) != 2)
9284 error ("Dimension of charset %s is not two",
9285 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9287 charset_list
= XCDR (charset_list
);
9288 if (! NILP (charset_list
))
9290 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9291 if (CHARSET_DIMENSION (charset
) != 2)
9292 error ("Dimension of charset %s is not two",
9293 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9296 category
= coding_category_sjis
;
9297 Vsjis_coding_system
= name
;
9299 else if (EQ (coding_type
, Qbig5
))
9301 struct charset
*charset
;
9303 if (XINT (Flength (charset_list
)) != 2)
9304 error ("There should be just two charsets");
9306 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9307 if (CHARSET_DIMENSION (charset
) != 1)
9308 error ("Dimension of charset %s is not one",
9309 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9310 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9311 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9313 charset_list
= XCDR (charset_list
);
9314 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9315 if (CHARSET_DIMENSION (charset
) != 2)
9316 error ("Dimension of charset %s is not two",
9317 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9319 category
= coding_category_big5
;
9320 Vbig5_coding_system
= name
;
9322 else if (EQ (coding_type
, Qraw_text
))
9324 category
= coding_category_raw_text
;
9325 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9327 else if (EQ (coding_type
, Qutf_8
))
9329 category
= coding_category_utf_8
;
9330 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9332 else if (EQ (coding_type
, Qundecided
))
9333 category
= coding_category_undecided
;
9335 error ("Invalid coding system type: %s",
9336 SDATA (SYMBOL_NAME (coding_type
)));
9338 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9339 CODING_ATTR_PLIST (attrs
)
9340 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9341 CODING_ATTR_PLIST (attrs
)));
9342 CODING_ATTR_PLIST (attrs
)
9343 = Fcons (QCascii_compatible_p
,
9344 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9345 CODING_ATTR_PLIST (attrs
)));
9347 eol_type
= args
[coding_arg_eol_type
];
9348 if (! NILP (eol_type
)
9349 && ! EQ (eol_type
, Qunix
)
9350 && ! EQ (eol_type
, Qdos
)
9351 && ! EQ (eol_type
, Qmac
))
9352 error ("Invalid eol-type");
9354 aliases
= Fcons (name
, Qnil
);
9356 if (NILP (eol_type
))
9358 eol_type
= make_subsidiaries (name
);
9359 for (i
= 0; i
< 3; i
++)
9361 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9363 this_name
= AREF (eol_type
, i
);
9364 this_aliases
= Fcons (this_name
, Qnil
);
9365 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9366 this_spec
= Fmake_vector (make_number (3), attrs
);
9367 ASET (this_spec
, 1, this_aliases
);
9368 ASET (this_spec
, 2, this_eol_type
);
9369 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9370 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9371 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9373 Vcoding_system_alist
9374 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9375 Vcoding_system_alist
);
9379 spec_vec
= Fmake_vector (make_number (3), attrs
);
9380 ASET (spec_vec
, 1, aliases
);
9381 ASET (spec_vec
, 2, eol_type
);
9383 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9384 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9385 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9387 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9388 Vcoding_system_alist
);
9391 int id
= coding_categories
[category
].id
;
9393 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9394 setup_coding_system (name
, &coding_categories
[category
]);
9400 return Fsignal (Qwrong_number_of_arguments
,
9401 Fcons (intern ("define-coding-system-internal"),
9402 make_number (nargs
)));
9406 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
9408 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9409 (coding_system
, prop
, val
)
9410 Lisp_Object coding_system
, prop
, val
;
9412 Lisp_Object spec
, attrs
;
9414 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9415 attrs
= AREF (spec
, 0);
9416 if (EQ (prop
, QCmnemonic
))
9418 if (! STRINGP (val
))
9419 CHECK_CHARACTER (val
);
9420 CODING_ATTR_MNEMONIC (attrs
) = val
;
9422 else if (EQ (prop
, QCdefalut_char
))
9425 val
= make_number (' ');
9427 CHECK_CHARACTER (val
);
9428 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9430 else if (EQ (prop
, QCdecode_translation_table
))
9432 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9434 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9436 else if (EQ (prop
, QCencode_translation_table
))
9438 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9440 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9442 else if (EQ (prop
, QCpost_read_conversion
))
9445 CODING_ATTR_POST_READ (attrs
) = val
;
9447 else if (EQ (prop
, QCpre_write_conversion
))
9450 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9452 else if (EQ (prop
, QCascii_compatible_p
))
9454 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9457 CODING_ATTR_PLIST (attrs
)
9458 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9463 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9464 Sdefine_coding_system_alias
, 2, 2, 0,
9465 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9466 (alias
, coding_system
)
9467 Lisp_Object alias
, coding_system
;
9469 Lisp_Object spec
, aliases
, eol_type
, val
;
9471 CHECK_SYMBOL (alias
);
9472 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9473 aliases
= AREF (spec
, 1);
9474 /* ALIASES should be a list of length more than zero, and the first
9475 element is a base coding system. Append ALIAS at the tail of the
9477 while (!NILP (XCDR (aliases
)))
9478 aliases
= XCDR (aliases
);
9479 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9481 eol_type
= AREF (spec
, 2);
9482 if (VECTORP (eol_type
))
9484 Lisp_Object subsidiaries
;
9487 subsidiaries
= make_subsidiaries (alias
);
9488 for (i
= 0; i
< 3; i
++)
9489 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9490 AREF (eol_type
, i
));
9493 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9494 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9495 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
9497 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9498 Vcoding_system_alist
);
9503 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9505 doc
: /* Return the base of CODING-SYSTEM.
9506 Any alias or subsidiary coding system is not a base coding system. */)
9508 Lisp_Object coding_system
;
9510 Lisp_Object spec
, attrs
;
9512 if (NILP (coding_system
))
9513 return (Qno_conversion
);
9514 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9515 attrs
= AREF (spec
, 0);
9516 return CODING_ATTR_BASE_NAME (attrs
);
9519 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9521 doc
: "Return the property list of CODING-SYSTEM.")
9523 Lisp_Object coding_system
;
9525 Lisp_Object spec
, attrs
;
9527 if (NILP (coding_system
))
9528 coding_system
= Qno_conversion
;
9529 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9530 attrs
= AREF (spec
, 0);
9531 return CODING_ATTR_PLIST (attrs
);
9535 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9537 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9539 Lisp_Object coding_system
;
9543 if (NILP (coding_system
))
9544 coding_system
= Qno_conversion
;
9545 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9546 return AREF (spec
, 1);
9549 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9550 Scoding_system_eol_type
, 1, 1, 0,
9551 doc
: /* Return eol-type of CODING-SYSTEM.
9552 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
9554 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9555 and CR respectively.
9557 A vector value indicates that a format of end-of-line should be
9558 detected automatically. Nth element of the vector is the subsidiary
9559 coding system whose eol-type is N. */)
9561 Lisp_Object coding_system
;
9563 Lisp_Object spec
, eol_type
;
9566 if (NILP (coding_system
))
9567 coding_system
= Qno_conversion
;
9568 if (! CODING_SYSTEM_P (coding_system
))
9570 spec
= CODING_SYSTEM_SPEC (coding_system
);
9571 eol_type
= AREF (spec
, 2);
9572 if (VECTORP (eol_type
))
9573 return Fcopy_sequence (eol_type
);
9574 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9575 return make_number (n
);
9581 /*** 9. Post-amble ***/
9588 for (i
= 0; i
< coding_category_max
; i
++)
9590 coding_categories
[i
].id
= -1;
9591 coding_priorities
[i
] = i
;
9594 /* ISO2022 specific initialize routine. */
9595 for (i
= 0; i
< 0x20; i
++)
9596 iso_code_class
[i
] = ISO_control_0
;
9597 for (i
= 0x21; i
< 0x7F; i
++)
9598 iso_code_class
[i
] = ISO_graphic_plane_0
;
9599 for (i
= 0x80; i
< 0xA0; i
++)
9600 iso_code_class
[i
] = ISO_control_1
;
9601 for (i
= 0xA1; i
< 0xFF; i
++)
9602 iso_code_class
[i
] = ISO_graphic_plane_1
;
9603 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9604 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9605 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9606 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9607 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9608 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9609 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9610 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9611 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9613 for (i
= 0; i
< 256; i
++)
9615 emacs_mule_bytes
[i
] = 1;
9617 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9618 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9619 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9620 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9628 staticpro (&Vcoding_system_hash_table
);
9630 Lisp_Object args
[2];
9633 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9636 staticpro (&Vsjis_coding_system
);
9637 Vsjis_coding_system
= Qnil
;
9639 staticpro (&Vbig5_coding_system
);
9640 Vbig5_coding_system
= Qnil
;
9642 staticpro (&Vcode_conversion_reused_workbuf
);
9643 Vcode_conversion_reused_workbuf
= Qnil
;
9645 staticpro (&Vcode_conversion_workbuf_name
);
9646 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9648 reused_workbuf_in_use
= 0;
9650 DEFSYM (Qcharset
, "charset");
9651 DEFSYM (Qtarget_idx
, "target-idx");
9652 DEFSYM (Qcoding_system_history
, "coding-system-history");
9653 Fset (Qcoding_system_history
, Qnil
);
9655 /* Target FILENAME is the first argument. */
9656 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9657 /* Target FILENAME is the third argument. */
9658 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9660 DEFSYM (Qcall_process
, "call-process");
9661 /* Target PROGRAM is the first argument. */
9662 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9664 DEFSYM (Qcall_process_region
, "call-process-region");
9665 /* Target PROGRAM is the third argument. */
9666 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9668 DEFSYM (Qstart_process
, "start-process");
9669 /* Target PROGRAM is the third argument. */
9670 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9672 DEFSYM (Qopen_network_stream
, "open-network-stream");
9673 /* Target SERVICE is the fourth argument. */
9674 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9676 DEFSYM (Qcoding_system
, "coding-system");
9677 DEFSYM (Qcoding_aliases
, "coding-aliases");
9679 DEFSYM (Qeol_type
, "eol-type");
9680 DEFSYM (Qunix
, "unix");
9681 DEFSYM (Qdos
, "dos");
9683 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9684 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9685 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9686 DEFSYM (Qdefault_char
, "default-char");
9687 DEFSYM (Qundecided
, "undecided");
9688 DEFSYM (Qno_conversion
, "no-conversion");
9689 DEFSYM (Qraw_text
, "raw-text");
9691 DEFSYM (Qiso_2022
, "iso-2022");
9693 DEFSYM (Qutf_8
, "utf-8");
9694 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9696 DEFSYM (Qutf_16
, "utf-16");
9697 DEFSYM (Qbig
, "big");
9698 DEFSYM (Qlittle
, "little");
9700 DEFSYM (Qshift_jis
, "shift-jis");
9701 DEFSYM (Qbig5
, "big5");
9703 DEFSYM (Qcoding_system_p
, "coding-system-p");
9705 DEFSYM (Qcoding_system_error
, "coding-system-error");
9706 Fput (Qcoding_system_error
, Qerror_conditions
,
9707 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9708 Fput (Qcoding_system_error
, Qerror_message
,
9709 build_string ("Invalid coding system"));
9711 /* Intern this now in case it isn't already done.
9712 Setting this variable twice is harmless.
9713 But don't staticpro it here--that is done in alloc.c. */
9714 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9716 DEFSYM (Qtranslation_table
, "translation-table");
9717 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9718 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9719 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9720 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9722 DEFSYM (Qvalid_codes
, "valid-codes");
9724 DEFSYM (Qemacs_mule
, "emacs-mule");
9726 DEFSYM (QCcategory
, ":category");
9727 DEFSYM (QCmnemonic
, ":mnemonic");
9728 DEFSYM (QCdefalut_char
, ":default-char");
9729 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9730 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9731 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9732 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9733 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
9735 Vcoding_category_table
9736 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9737 staticpro (&Vcoding_category_table
);
9738 /* Followings are target of code detection. */
9739 ASET (Vcoding_category_table
, coding_category_iso_7
,
9740 intern ("coding-category-iso-7"));
9741 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9742 intern ("coding-category-iso-7-tight"));
9743 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9744 intern ("coding-category-iso-8-1"));
9745 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9746 intern ("coding-category-iso-8-2"));
9747 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9748 intern ("coding-category-iso-7-else"));
9749 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9750 intern ("coding-category-iso-8-else"));
9751 ASET (Vcoding_category_table
, coding_category_utf_8
,
9752 intern ("coding-category-utf-8"));
9753 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9754 intern ("coding-category-utf-16-be"));
9755 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9756 intern ("coding-category-utf-16-auto"));
9757 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9758 intern ("coding-category-utf-16-le"));
9759 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9760 intern ("coding-category-utf-16-be-nosig"));
9761 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9762 intern ("coding-category-utf-16-le-nosig"));
9763 ASET (Vcoding_category_table
, coding_category_charset
,
9764 intern ("coding-category-charset"));
9765 ASET (Vcoding_category_table
, coding_category_sjis
,
9766 intern ("coding-category-sjis"));
9767 ASET (Vcoding_category_table
, coding_category_big5
,
9768 intern ("coding-category-big5"));
9769 ASET (Vcoding_category_table
, coding_category_ccl
,
9770 intern ("coding-category-ccl"));
9771 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9772 intern ("coding-category-emacs-mule"));
9773 /* Followings are NOT target of code detection. */
9774 ASET (Vcoding_category_table
, coding_category_raw_text
,
9775 intern ("coding-category-raw-text"));
9776 ASET (Vcoding_category_table
, coding_category_undecided
,
9777 intern ("coding-category-undecided"));
9779 DEFSYM (Qinsufficient_source
, "insufficient-source");
9780 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9781 DEFSYM (Qinvalid_source
, "invalid-source");
9782 DEFSYM (Qinterrupted
, "interrupted");
9783 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9784 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
9786 defsubr (&Scoding_system_p
);
9787 defsubr (&Sread_coding_system
);
9788 defsubr (&Sread_non_nil_coding_system
);
9789 defsubr (&Scheck_coding_system
);
9790 defsubr (&Sdetect_coding_region
);
9791 defsubr (&Sdetect_coding_string
);
9792 defsubr (&Sfind_coding_systems_region_internal
);
9793 defsubr (&Sunencodable_char_position
);
9794 defsubr (&Scheck_coding_systems_region
);
9795 defsubr (&Sdecode_coding_region
);
9796 defsubr (&Sencode_coding_region
);
9797 defsubr (&Sdecode_coding_string
);
9798 defsubr (&Sencode_coding_string
);
9799 defsubr (&Sdecode_sjis_char
);
9800 defsubr (&Sencode_sjis_char
);
9801 defsubr (&Sdecode_big5_char
);
9802 defsubr (&Sencode_big5_char
);
9803 defsubr (&Sset_terminal_coding_system_internal
);
9804 defsubr (&Sset_safe_terminal_coding_system_internal
);
9805 defsubr (&Sterminal_coding_system
);
9806 defsubr (&Sset_keyboard_coding_system_internal
);
9807 defsubr (&Skeyboard_coding_system
);
9808 defsubr (&Sfind_operation_coding_system
);
9809 defsubr (&Sset_coding_system_priority
);
9810 defsubr (&Sdefine_coding_system_internal
);
9811 defsubr (&Sdefine_coding_system_alias
);
9812 defsubr (&Scoding_system_put
);
9813 defsubr (&Scoding_system_base
);
9814 defsubr (&Scoding_system_plist
);
9815 defsubr (&Scoding_system_aliases
);
9816 defsubr (&Scoding_system_eol_type
);
9817 defsubr (&Scoding_system_priority_list
);
9819 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9820 doc
: /* List of coding systems.
9822 Do not alter the value of this variable manually. This variable should be
9823 updated by the functions `define-coding-system' and
9824 `define-coding-system-alias'. */);
9825 Vcoding_system_list
= Qnil
;
9827 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9828 doc
: /* Alist of coding system names.
9829 Each element is one element list of coding system name.
9830 This variable is given to `completing-read' as TABLE argument.
9832 Do not alter the value of this variable manually. This variable should be
9833 updated by the functions `make-coding-system' and
9834 `define-coding-system-alias'. */);
9835 Vcoding_system_alist
= Qnil
;
9837 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9838 doc
: /* List of coding-categories (symbols) ordered by priority.
9840 On detecting a coding system, Emacs tries code detection algorithms
9841 associated with each coding-category one by one in this order. When
9842 one algorithm agrees with a byte sequence of source text, the coding
9843 system bound to the corresponding coding-category is selected.
9845 Don't modify this variable directly, but use `set-coding-priority'. */);
9849 Vcoding_category_list
= Qnil
;
9850 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9851 Vcoding_category_list
9852 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9853 Vcoding_category_list
);
9856 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9857 doc
: /* Specify the coding system for read operations.
9858 It is useful to bind this variable with `let', but do not set it globally.
9859 If the value is a coding system, it is used for decoding on read operation.
9860 If not, an appropriate element is used from one of the coding system alists:
9861 There are three such tables, `file-coding-system-alist',
9862 `process-coding-system-alist', and `network-coding-system-alist'. */);
9863 Vcoding_system_for_read
= Qnil
;
9865 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9866 doc
: /* Specify the coding system for write operations.
9867 Programs bind this variable with `let', but you should not set it globally.
9868 If the value is a coding system, it is used for encoding of output,
9869 when writing it to a file and when sending it to a file or subprocess.
9871 If this does not specify a coding system, an appropriate element
9872 is used from one of the coding system alists:
9873 There are three such tables, `file-coding-system-alist',
9874 `process-coding-system-alist', and `network-coding-system-alist'.
9875 For output to files, if the above procedure does not specify a coding system,
9876 the value of `buffer-file-coding-system' is used. */);
9877 Vcoding_system_for_write
= Qnil
;
9879 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9881 Coding system used in the latest file or process I/O. */);
9882 Vlast_coding_system_used
= Qnil
;
9884 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9886 Error status of the last code conversion.
9888 When an error was detected in the last code conversion, this variable
9889 is set to one of the following symbols.
9890 `insufficient-source'
9894 `insufficient-memory'
9895 When no error was detected, the value doesn't change. So, to check
9896 the error status of a code conversion by this variable, you must
9897 explicitly set this variable to nil before performing code
9899 Vlast_code_conversion_error
= Qnil
;
9901 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9903 *Non-nil means always inhibit code conversion of end-of-line format.
9904 See info node `Coding Systems' and info node `Text and Binary' concerning
9905 such conversion. */);
9906 inhibit_eol_conversion
= 0;
9908 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9910 Non-nil means process buffer inherits coding system of process output.
9911 Bind it to t if the process output is to be treated as if it were a file
9912 read from some filesystem. */);
9913 inherit_process_coding_system
= 0;
9915 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9917 Alist to decide a coding system to use for a file I/O operation.
9918 The format is ((PATTERN . VAL) ...),
9919 where PATTERN is a regular expression matching a file name,
9920 VAL is a coding system, a cons of coding systems, or a function symbol.
9921 If VAL is a coding system, it is used for both decoding and encoding
9923 If VAL is a cons of coding systems, the car part is used for decoding,
9924 and the cdr part is used for encoding.
9925 If VAL is a function symbol, the function must return a coding system
9926 or a cons of coding systems which are used as above. The function is
9927 called with an argument that is a list of the arguments with which
9928 `find-operation-coding-system' was called. If the function can't decide
9929 a coding system, it can return `undecided' so that the normal
9930 code-detection is performed.
9932 See also the function `find-operation-coding-system'
9933 and the variable `auto-coding-alist'. */);
9934 Vfile_coding_system_alist
= Qnil
;
9936 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9938 Alist to decide a coding system to use for a process I/O operation.
9939 The format is ((PATTERN . VAL) ...),
9940 where PATTERN is a regular expression matching a program name,
9941 VAL is a coding system, a cons of coding systems, or a function symbol.
9942 If VAL is a coding system, it is used for both decoding what received
9943 from the program and encoding what sent to the program.
9944 If VAL is a cons of coding systems, the car part is used for decoding,
9945 and the cdr part is used for encoding.
9946 If VAL is a function symbol, the function must return a coding system
9947 or a cons of coding systems which are used as above.
9949 See also the function `find-operation-coding-system'. */);
9950 Vprocess_coding_system_alist
= Qnil
;
9952 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9954 Alist to decide a coding system to use for a network I/O operation.
9955 The format is ((PATTERN . VAL) ...),
9956 where PATTERN is a regular expression matching a network service name
9957 or is a port number to connect to,
9958 VAL is a coding system, a cons of coding systems, or a function symbol.
9959 If VAL is a coding system, it is used for both decoding what received
9960 from the network stream and encoding what sent to the network stream.
9961 If VAL is a cons of coding systems, the car part is used for decoding,
9962 and the cdr part is used for encoding.
9963 If VAL is a function symbol, the function must return a coding system
9964 or a cons of coding systems which are used as above.
9966 See also the function `find-operation-coding-system'. */);
9967 Vnetwork_coding_system_alist
= Qnil
;
9969 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9970 doc
: /* Coding system to use with system messages.
9971 Also used for decoding keyboard input on X Window system. */);
9972 Vlocale_coding_system
= Qnil
;
9974 /* The eol mnemonics are reset in startup.el system-dependently. */
9975 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9977 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9978 eol_mnemonic_unix
= build_string (":");
9980 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9982 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9983 eol_mnemonic_dos
= build_string ("\\");
9985 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9987 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9988 eol_mnemonic_mac
= build_string ("/");
9990 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9992 *String displayed in mode line when end-of-line format is not yet determined. */);
9993 eol_mnemonic_undecided
= build_string (":");
9995 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
9997 *Non-nil enables character translation while encoding and decoding. */);
9998 Venable_character_translation
= Qt
;
10000 DEFVAR_LISP ("standard-translation-table-for-decode",
10001 &Vstandard_translation_table_for_decode
,
10002 doc
: /* Table for translating characters while decoding. */);
10003 Vstandard_translation_table_for_decode
= Qnil
;
10005 DEFVAR_LISP ("standard-translation-table-for-encode",
10006 &Vstandard_translation_table_for_encode
,
10007 doc
: /* Table for translating characters while encoding. */);
10008 Vstandard_translation_table_for_encode
= Qnil
;
10010 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
10011 doc
: /* Alist of charsets vs revision numbers.
10012 While encoding, if a charset (car part of an element) is found,
10013 designate it with the escape sequence identifying revision (cdr part
10014 of the element). */);
10015 Vcharset_revision_table
= Qnil
;
10017 DEFVAR_LISP ("default-process-coding-system",
10018 &Vdefault_process_coding_system
,
10019 doc
: /* Cons of coding systems used for process I/O by default.
10020 The car part is used for decoding a process output,
10021 the cdr part is used for encoding a text to be sent to a process. */);
10022 Vdefault_process_coding_system
= Qnil
;
10024 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
10026 Table of extra Latin codes in the range 128..159 (inclusive).
10027 This is a vector of length 256.
10028 If Nth element is non-nil, the existence of code N in a file
10029 \(or output of subprocess) doesn't prevent it to be detected as
10030 a coding system of ISO 2022 variant which has a flag
10031 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10032 or reading output of a subprocess.
10033 Only 128th through 159th elements has a meaning. */);
10034 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10036 DEFVAR_LISP ("select-safe-coding-system-function",
10037 &Vselect_safe_coding_system_function
,
10039 Function to call to select safe coding system for encoding a text.
10041 If set, this function is called to force a user to select a proper
10042 coding system which can encode the text in the case that a default
10043 coding system used in each operation can't encode the text. The
10044 function should take care that the buffer is not modified while
10045 the coding system is being selected.
10047 The default value is `select-safe-coding-system' (which see). */);
10048 Vselect_safe_coding_system_function
= Qnil
;
10050 DEFVAR_BOOL ("coding-system-require-warning",
10051 &coding_system_require_warning
,
10052 doc
: /* Internal use only.
10053 If non-nil, on writing a file, `select-safe-coding-system-function' is
10054 called even if `coding-system-for-write' is non-nil. The command
10055 `universal-coding-system-argument' binds this variable to t temporarily. */);
10056 coding_system_require_warning
= 0;
10059 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10060 &inhibit_iso_escape_detection
,
10062 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
10064 By default, on reading a file, Emacs tries to detect how the text is
10065 encoded. This code detection is sensitive to escape sequences. If
10066 the sequence is valid as ISO2022, the code is determined as one of
10067 the ISO2022 encodings, and the file is decoded by the corresponding
10068 coding system (e.g. `iso-2022-7bit').
10070 However, there may be a case that you want to read escape sequences in
10071 a file as is. In such a case, you can set this variable to non-nil.
10072 Then, as the code detection ignores any escape sequences, no file is
10073 detected as encoded in some ISO2022 encoding. The result is that all
10074 escape sequences become visible in a buffer.
10076 The default value is nil, and it is strongly recommended not to change
10077 it. That is because many Emacs Lisp source files that contain
10078 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10079 in Emacs's distribution, and they won't be decoded correctly on
10080 reading if you suppress escape sequence detection.
10082 The other way to read escape sequences in a file without decoding is
10083 to explicitly specify some coding system that doesn't use ISO2022's
10084 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10085 inhibit_iso_escape_detection
= 0;
10087 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
10088 doc
: /* Char table for translating self-inserting characters.
10089 This is applied to the result of input methods, not their input. See also
10090 `keyboard-translate-table'. */);
10091 Vtranslation_table_for_input
= Qnil
;
10094 Lisp_Object args
[coding_arg_max
];
10095 Lisp_Object plist
[16];
10098 for (i
= 0; i
< coding_arg_max
; i
++)
10101 plist
[0] = intern (":name");
10102 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10103 plist
[2] = intern (":mnemonic");
10104 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10105 plist
[4] = intern (":coding-type");
10106 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10107 plist
[6] = intern (":ascii-compatible-p");
10108 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10109 plist
[8] = intern (":default-char");
10110 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10111 plist
[10] = intern (":for-unibyte");
10112 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10113 plist
[12] = intern (":docstring");
10114 plist
[13] = build_string ("Do no conversion.\n\
10116 When you visit a file with this coding, the file is read into a\n\
10117 unibyte buffer as is, thus each byte of a file is treated as a\n\
10119 plist
[14] = intern (":eol-type");
10120 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10121 args
[coding_arg_plist
] = Flist (16, plist
);
10122 Fdefine_coding_system_internal (coding_arg_max
, args
);
10124 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10125 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10126 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10127 /* This is already set.
10128 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10129 plist
[8] = intern (":charset-list");
10130 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10131 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10132 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
10133 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10134 args
[coding_arg_plist
] = Flist (16, plist
);
10135 Fdefine_coding_system_internal (coding_arg_max
, args
);
10138 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10143 for (i
= 0; i
< coding_category_max
; i
++)
10144 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10146 #if defined (MSDOS) || defined (WINDOWSNT)
10147 system_eol_type
= Qdos
;
10149 system_eol_type
= Qunix
;
10151 staticpro (&system_eol_type
);
10155 emacs_strerror (error_number
)
10160 synchronize_system_messages_locale ();
10161 str
= strerror (error_number
);
10163 if (! NILP (Vlocale_coding_system
))
10165 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10166 Vlocale_coding_system
,
10168 str
= (char *) SDATA (dec
);
10176 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
10177 (do not change this comment) */