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
;
4821 int produced_chars
= 0;
4826 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4828 if (coding
->src_multibyte
)
4829 while (charbuf
< charbuf_end
)
4831 ASSURE_DESTINATION (safe_room
);
4833 if (ASCII_CHAR_P (c
))
4834 EMIT_ONE_ASCII_BYTE (c
);
4835 else if (CHAR_BYTE8_P (c
))
4837 c
= CHAR_TO_BYTE8 (c
);
4842 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4844 CHAR_STRING_ADVANCE (c
, p1
);
4847 EMIT_ONE_BYTE (*p0
);
4853 while (charbuf
< charbuf_end
)
4855 ASSURE_DESTINATION (safe_room
);
4862 if (coding
->src_multibyte
)
4864 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4866 while (charbuf
< charbuf_end
)
4868 ASSURE_DESTINATION (safe_room
);
4870 if (ASCII_CHAR_P (c
))
4872 else if (CHAR_BYTE8_P (c
))
4873 *dst
++ = CHAR_TO_BYTE8 (c
);
4875 CHAR_STRING_ADVANCE (c
, dst
);
4880 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4881 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4882 *dst
++ = *charbuf
++;
4884 produced_chars
= dst
- (coding
->destination
+ coding
->produced
);
4886 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4887 coding
->produced_char
+= produced_chars
;
4888 coding
->produced
= dst
- coding
->destination
;
4892 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4893 Check if a text is encoded in a charset-based coding system. If it
4894 is, return 1, else return 0. */
4897 detect_coding_charset (coding
, detect_info
)
4898 struct coding_system
*coding
;
4899 struct coding_detection_info
*detect_info
;
4901 const unsigned char *src
= coding
->source
, *src_base
;
4902 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4903 int multibytep
= coding
->src_multibyte
;
4904 int consumed_chars
= 0;
4905 Lisp_Object attrs
, valids
;
4907 int head_ascii
= coding
->head_ascii
;
4909 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4911 coding
= &coding_categories
[coding_category_charset
];
4912 attrs
= CODING_ID_ATTRS (coding
->id
);
4913 valids
= AREF (attrs
, coding_attr_charset_valids
);
4915 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4922 struct charset
*charset
;
4929 val
= AREF (valids
, c
);
4933 found
= CATEGORY_MASK_CHARSET
;
4936 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4937 dim
= CHARSET_DIMENSION (charset
);
4938 for (idx
= 1; idx
< dim
; idx
++)
4943 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 2]
4944 || c
> charset
->code_space
[(dim
- 1 - idx
) * 2 + 1])
4953 for (; CONSP (val
); val
= XCDR (val
))
4955 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4956 dim
= CHARSET_DIMENSION (charset
);
4962 if (c
< charset
->code_space
[(dim
- 1 - idx
) * 4]
4963 || c
> charset
->code_space
[(dim
- 1 - idx
) * 4 + 1])
4978 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4982 detect_info
->found
|= found
;
4987 decode_coding_charset (coding
)
4988 struct coding_system
*coding
;
4990 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4991 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4992 const unsigned char *src_base
;
4993 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4995 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4996 int consumed_chars
= 0, consumed_chars_base
;
4997 int multibytep
= coding
->src_multibyte
;
4998 Lisp_Object attrs
, charset_list
, valids
;
4999 int char_offset
= coding
->produced_char
;
5000 int last_offset
= char_offset
;
5001 int last_id
= charset_ascii
;
5002 int eol_crlf
= EQ (CODING_ID_EOL_TYPE (coding
->id
), Qdos
);
5003 int byte_after_cr
= -1;
5005 CODING_GET_INFO (coding
, attrs
, charset_list
);
5006 valids
= AREF (attrs
, coding_attr_charset_valids
);
5012 struct charset
*charset
;
5018 consumed_chars_base
= consumed_chars
;
5020 if (charbuf
>= charbuf_end
)
5023 if (byte_after_cr
>= 0)
5031 if (eol_crlf
&& c
== '\r')
5032 ONE_MORE_BYTE (byte_after_cr
);
5038 val
= AREF (valids
, c
);
5043 charset
= CHARSET_FROM_ID (XFASTINT (val
));
5044 dim
= CHARSET_DIMENSION (charset
);
5048 code
= (code
<< 8) | c
;
5051 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
5056 /* VAL is a list of charset IDs. It is assured that the
5057 list is sorted by charset dimensions (smaller one
5061 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
5062 dim
= CHARSET_DIMENSION (charset
);
5066 code
= (code
<< 8) | c
;
5069 CODING_DECODE_CHAR (coding
, src
, src_base
,
5070 src_end
, charset
, code
, c
);
5078 if (charset
->id
!= charset_ascii
5079 && last_id
!= charset
->id
)
5081 if (last_id
!= charset_ascii
)
5082 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5083 last_id
= charset
->id
;
5084 last_offset
= char_offset
;
5093 consumed_chars
= consumed_chars_base
;
5095 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
5101 if (last_id
!= charset_ascii
)
5102 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
5103 coding
->consumed_char
+= consumed_chars_base
;
5104 coding
->consumed
= src_base
- coding
->source
;
5105 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
5109 encode_coding_charset (coding
)
5110 struct coding_system
*coding
;
5112 int multibytep
= coding
->dst_multibyte
;
5113 int *charbuf
= coding
->charbuf
;
5114 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5115 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5116 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5117 int safe_room
= MAX_MULTIBYTE_LENGTH
;
5118 int produced_chars
= 0;
5119 Lisp_Object attrs
, charset_list
;
5120 int ascii_compatible
;
5123 CODING_GET_INFO (coding
, attrs
, charset_list
);
5124 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
5126 while (charbuf
< charbuf_end
)
5128 struct charset
*charset
;
5131 ASSURE_DESTINATION (safe_room
);
5133 if (ascii_compatible
&& ASCII_CHAR_P (c
))
5134 EMIT_ONE_ASCII_BYTE (c
);
5135 else if (CHAR_BYTE8_P (c
))
5137 c
= CHAR_TO_BYTE8 (c
);
5142 charset
= char_charset (c
, charset_list
, &code
);
5145 if (CHARSET_DIMENSION (charset
) == 1)
5146 EMIT_ONE_BYTE (code
);
5147 else if (CHARSET_DIMENSION (charset
) == 2)
5148 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
5149 else if (CHARSET_DIMENSION (charset
) == 3)
5150 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
5152 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
5153 (code
>> 8) & 0xFF, code
& 0xFF);
5157 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
5158 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
5160 c
= coding
->default_char
;
5166 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
5167 coding
->produced_char
+= produced_chars
;
5168 coding
->produced
= dst
- coding
->destination
;
5173 /*** 7. C library functions ***/
5175 /* Setup coding context CODING from information about CODING_SYSTEM.
5176 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
5177 CODING_SYSTEM is invalid, signal an error. */
5180 setup_coding_system (coding_system
, coding
)
5181 Lisp_Object coding_system
;
5182 struct coding_system
*coding
;
5185 Lisp_Object eol_type
;
5186 Lisp_Object coding_type
;
5189 if (NILP (coding_system
))
5190 coding_system
= Qundecided
;
5192 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
5194 attrs
= CODING_ID_ATTRS (coding
->id
);
5195 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5198 coding
->head_ascii
= -1;
5199 if (VECTORP (eol_type
))
5200 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5201 | CODING_REQUIRE_DETECTION_MASK
);
5202 else if (! EQ (eol_type
, Qunix
))
5203 coding
->common_flags
= (CODING_REQUIRE_DECODING_MASK
5204 | CODING_REQUIRE_ENCODING_MASK
);
5206 coding
->common_flags
= 0;
5207 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
5208 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5209 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
5210 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5211 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
5212 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
5214 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5215 coding
->max_charset_id
= SCHARS (val
) - 1;
5216 coding
->safe_charsets
= (char *) SDATA (val
);
5217 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
5219 coding_type
= CODING_ATTR_TYPE (attrs
);
5220 if (EQ (coding_type
, Qundecided
))
5222 coding
->detector
= NULL
;
5223 coding
->decoder
= decode_coding_raw_text
;
5224 coding
->encoder
= encode_coding_raw_text
;
5225 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5227 else if (EQ (coding_type
, Qiso_2022
))
5230 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5232 /* Invoke graphic register 0 to plane 0. */
5233 CODING_ISO_INVOCATION (coding
, 0) = 0;
5234 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5235 CODING_ISO_INVOCATION (coding
, 1)
5236 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
5237 /* Setup the initial status of designation. */
5238 for (i
= 0; i
< 4; i
++)
5239 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
5240 /* Not single shifting initially. */
5241 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
5242 /* Beginning of buffer should also be regarded as bol. */
5243 CODING_ISO_BOL (coding
) = 1;
5244 coding
->detector
= detect_coding_iso_2022
;
5245 coding
->decoder
= decode_coding_iso_2022
;
5246 coding
->encoder
= encode_coding_iso_2022
;
5247 if (flags
& CODING_ISO_FLAG_SAFE
)
5248 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
5249 coding
->common_flags
5250 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5251 | CODING_REQUIRE_FLUSHING_MASK
);
5252 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
5253 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
5254 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
5255 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
5256 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5258 setup_iso_safe_charsets (attrs
);
5259 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
5260 coding
->max_charset_id
= SCHARS (val
) - 1;
5261 coding
->safe_charsets
= (char *) SDATA (val
);
5263 CODING_ISO_FLAGS (coding
) = flags
;
5265 else if (EQ (coding_type
, Qcharset
))
5267 coding
->detector
= detect_coding_charset
;
5268 coding
->decoder
= decode_coding_charset
;
5269 coding
->encoder
= encode_coding_charset
;
5270 coding
->common_flags
5271 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5273 else if (EQ (coding_type
, Qutf_8
))
5275 coding
->detector
= detect_coding_utf_8
;
5276 coding
->decoder
= decode_coding_utf_8
;
5277 coding
->encoder
= encode_coding_utf_8
;
5278 coding
->common_flags
5279 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5281 else if (EQ (coding_type
, Qutf_16
))
5283 val
= AREF (attrs
, coding_attr_utf_16_bom
);
5284 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
5285 : EQ (val
, Qt
) ? utf_16_with_bom
5286 : utf_16_without_bom
);
5287 val
= AREF (attrs
, coding_attr_utf_16_endian
);
5288 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
5289 : utf_16_little_endian
);
5290 CODING_UTF_16_SURROGATE (coding
) = 0;
5291 coding
->detector
= detect_coding_utf_16
;
5292 coding
->decoder
= decode_coding_utf_16
;
5293 coding
->encoder
= encode_coding_utf_16
;
5294 coding
->common_flags
5295 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5296 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
5297 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
5299 else if (EQ (coding_type
, Qccl
))
5301 coding
->detector
= detect_coding_ccl
;
5302 coding
->decoder
= decode_coding_ccl
;
5303 coding
->encoder
= encode_coding_ccl
;
5304 coding
->common_flags
5305 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
5306 | CODING_REQUIRE_FLUSHING_MASK
);
5308 else if (EQ (coding_type
, Qemacs_mule
))
5310 coding
->detector
= detect_coding_emacs_mule
;
5311 coding
->decoder
= decode_coding_emacs_mule
;
5312 coding
->encoder
= encode_coding_emacs_mule
;
5313 coding
->common_flags
5314 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5315 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
5316 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
5318 Lisp_Object tail
, safe_charsets
;
5319 int max_charset_id
= 0;
5321 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5323 if (max_charset_id
< XFASTINT (XCAR (tail
)))
5324 max_charset_id
= XFASTINT (XCAR (tail
));
5325 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
5327 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
5329 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
5330 coding
->max_charset_id
= max_charset_id
;
5331 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
5334 else if (EQ (coding_type
, Qshift_jis
))
5336 coding
->detector
= detect_coding_sjis
;
5337 coding
->decoder
= decode_coding_sjis
;
5338 coding
->encoder
= encode_coding_sjis
;
5339 coding
->common_flags
5340 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5342 else if (EQ (coding_type
, Qbig5
))
5344 coding
->detector
= detect_coding_big5
;
5345 coding
->decoder
= decode_coding_big5
;
5346 coding
->encoder
= encode_coding_big5
;
5347 coding
->common_flags
5348 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5350 else /* EQ (coding_type, Qraw_text) */
5352 coding
->detector
= NULL
;
5353 coding
->decoder
= decode_coding_raw_text
;
5354 coding
->encoder
= encode_coding_raw_text
;
5355 if (! EQ (eol_type
, Qunix
))
5357 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
5358 if (! VECTORP (eol_type
))
5359 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
5367 /* Return a list of charsets supported by CODING. */
5370 coding_charset_list (coding
)
5371 struct coding_system
*coding
;
5373 Lisp_Object attrs
, charset_list
;
5375 CODING_GET_INFO (coding
, attrs
, charset_list
);
5376 if (EQ (CODING_ATTR_TYPE (attrs
), Qiso_2022
))
5378 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
5380 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
5381 charset_list
= Viso_2022_charset_list
;
5383 else if (EQ (CODING_ATTR_TYPE (attrs
), Qemacs_mule
))
5385 charset_list
= Vemacs_mule_charset_list
;
5387 return charset_list
;
5391 /* Return raw-text or one of its subsidiaries that has the same
5392 eol_type as CODING-SYSTEM. */
5395 raw_text_coding_system (coding_system
)
5396 Lisp_Object coding_system
;
5398 Lisp_Object spec
, attrs
;
5399 Lisp_Object eol_type
, raw_text_eol_type
;
5401 if (NILP (coding_system
))
5403 spec
= CODING_SYSTEM_SPEC (coding_system
);
5404 attrs
= AREF (spec
, 0);
5406 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5407 return coding_system
;
5409 eol_type
= AREF (spec
, 2);
5410 if (VECTORP (eol_type
))
5412 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5413 raw_text_eol_type
= AREF (spec
, 2);
5414 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5415 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5416 : AREF (raw_text_eol_type
, 2));
5420 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5421 does, return one of the subsidiary that has the same eol-spec as
5422 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5423 inherit end-of-line format from the system's setting
5424 (system_eol_type). */
5427 coding_inherit_eol_type (coding_system
, parent
)
5428 Lisp_Object coding_system
, parent
;
5430 Lisp_Object spec
, eol_type
;
5432 if (NILP (coding_system
))
5433 coding_system
= Qraw_text
;
5434 spec
= CODING_SYSTEM_SPEC (coding_system
);
5435 eol_type
= AREF (spec
, 2);
5436 if (VECTORP (eol_type
))
5438 Lisp_Object parent_eol_type
;
5440 if (! NILP (parent
))
5442 Lisp_Object parent_spec
;
5444 parent_spec
= CODING_SYSTEM_SPEC (parent
);
5445 parent_eol_type
= AREF (parent_spec
, 2);
5448 parent_eol_type
= system_eol_type
;
5449 if (EQ (parent_eol_type
, Qunix
))
5450 coding_system
= AREF (eol_type
, 0);
5451 else if (EQ (parent_eol_type
, Qdos
))
5452 coding_system
= AREF (eol_type
, 1);
5453 else if (EQ (parent_eol_type
, Qmac
))
5454 coding_system
= AREF (eol_type
, 2);
5456 return coding_system
;
5459 /* Emacs has a mechanism to automatically detect a coding system if it
5460 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5461 it's impossible to distinguish some coding systems accurately
5462 because they use the same range of codes. So, at first, coding
5463 systems are categorized into 7, those are:
5465 o coding-category-emacs-mule
5467 The category for a coding system which has the same code range
5468 as Emacs' internal format. Assigned the coding-system (Lisp
5469 symbol) `emacs-mule' by default.
5471 o coding-category-sjis
5473 The category for a coding system which has the same code range
5474 as SJIS. Assigned the coding-system (Lisp
5475 symbol) `japanese-shift-jis' by default.
5477 o coding-category-iso-7
5479 The category for a coding system which has the same code range
5480 as ISO2022 of 7-bit environment. This doesn't use any locking
5481 shift and single shift functions. This can encode/decode all
5482 charsets. Assigned the coding-system (Lisp symbol)
5483 `iso-2022-7bit' by default.
5485 o coding-category-iso-7-tight
5487 Same as coding-category-iso-7 except that this can
5488 encode/decode only the specified charsets.
5490 o coding-category-iso-8-1
5492 The category for a coding system which has the same code range
5493 as ISO2022 of 8-bit environment and graphic plane 1 used only
5494 for DIMENSION1 charset. This doesn't use any locking shift
5495 and single shift functions. Assigned the coding-system (Lisp
5496 symbol) `iso-latin-1' by default.
5498 o coding-category-iso-8-2
5500 The category for a coding system which has the same code range
5501 as ISO2022 of 8-bit environment and graphic plane 1 used only
5502 for DIMENSION2 charset. This doesn't use any locking shift
5503 and single shift functions. Assigned the coding-system (Lisp
5504 symbol) `japanese-iso-8bit' by default.
5506 o coding-category-iso-7-else
5508 The category for a coding system which has the same code range
5509 as ISO2022 of 7-bit environemnt but uses locking shift or
5510 single shift functions. Assigned the coding-system (Lisp
5511 symbol) `iso-2022-7bit-lock' by default.
5513 o coding-category-iso-8-else
5515 The category for a coding system which has the same code range
5516 as ISO2022 of 8-bit environemnt but uses locking shift or
5517 single shift functions. Assigned the coding-system (Lisp
5518 symbol) `iso-2022-8bit-ss2' by default.
5520 o coding-category-big5
5522 The category for a coding system which has the same code range
5523 as BIG5. Assigned the coding-system (Lisp symbol)
5524 `cn-big5' by default.
5526 o coding-category-utf-8
5528 The category for a coding system which has the same code range
5529 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5530 symbol) `utf-8' by default.
5532 o coding-category-utf-16-be
5534 The category for a coding system in which a text has an
5535 Unicode signature (cf. Unicode Standard) in the order of BIG
5536 endian at the head. Assigned the coding-system (Lisp symbol)
5537 `utf-16-be' by default.
5539 o coding-category-utf-16-le
5541 The category for a coding system in which a text has an
5542 Unicode signature (cf. Unicode Standard) in the order of
5543 LITTLE endian at the head. Assigned the coding-system (Lisp
5544 symbol) `utf-16-le' by default.
5546 o coding-category-ccl
5548 The category for a coding system of which encoder/decoder is
5549 written in CCL programs. The default value is nil, i.e., no
5550 coding system is assigned.
5552 o coding-category-binary
5554 The category for a coding system not categorized in any of the
5555 above. Assigned the coding-system (Lisp symbol)
5556 `no-conversion' by default.
5558 Each of them is a Lisp symbol and the value is an actual
5559 `coding-system's (this is also a Lisp symbol) assigned by a user.
5560 What Emacs does actually is to detect a category of coding system.
5561 Then, it uses a `coding-system' assigned to it. If Emacs can't
5562 decide only one possible category, it selects a category of the
5563 highest priority. Priorities of categories are also specified by a
5564 user in a Lisp variable `coding-category-list'.
5568 #define EOL_SEEN_NONE 0
5569 #define EOL_SEEN_LF 1
5570 #define EOL_SEEN_CR 2
5571 #define EOL_SEEN_CRLF 4
5573 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5574 SOURCE is encoded. If CATEGORY is one of
5575 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5576 two-byte, else they are encoded by one-byte.
5578 Return one of EOL_SEEN_XXX. */
5580 #define MAX_EOL_CHECK_COUNT 3
5583 detect_eol (source
, src_bytes
, category
)
5584 const unsigned char *source
;
5585 EMACS_INT src_bytes
;
5586 enum coding_category category
;
5588 const unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5591 int eol_seen
= EOL_SEEN_NONE
;
5593 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5597 msb
= category
== (coding_category_utf_16_le
5598 | coding_category_utf_16_le_nosig
);
5601 while (src
+ 1 < src_end
)
5604 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5609 this_eol
= EOL_SEEN_LF
;
5610 else if (src
+ 3 >= src_end
5611 || src
[msb
+ 2] != 0
5612 || src
[lsb
+ 2] != '\n')
5613 this_eol
= EOL_SEEN_CR
;
5615 this_eol
= EOL_SEEN_CRLF
;
5617 if (eol_seen
== EOL_SEEN_NONE
)
5618 /* This is the first end-of-line. */
5619 eol_seen
= this_eol
;
5620 else if (eol_seen
!= this_eol
)
5622 /* The found type is different from what found before. */
5623 eol_seen
= EOL_SEEN_LF
;
5626 if (++total
== MAX_EOL_CHECK_COUNT
)
5634 while (src
< src_end
)
5637 if (c
== '\n' || c
== '\r')
5642 this_eol
= EOL_SEEN_LF
;
5643 else if (src
>= src_end
|| *src
!= '\n')
5644 this_eol
= EOL_SEEN_CR
;
5646 this_eol
= EOL_SEEN_CRLF
, src
++;
5648 if (eol_seen
== EOL_SEEN_NONE
)
5649 /* This is the first end-of-line. */
5650 eol_seen
= this_eol
;
5651 else if (eol_seen
!= this_eol
)
5653 /* The found type is different from what found before. */
5654 eol_seen
= EOL_SEEN_LF
;
5657 if (++total
== MAX_EOL_CHECK_COUNT
)
5667 adjust_coding_eol_type (coding
, eol_seen
)
5668 struct coding_system
*coding
;
5671 Lisp_Object eol_type
;
5673 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5674 if (eol_seen
& EOL_SEEN_LF
)
5676 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5679 else if (eol_seen
& EOL_SEEN_CRLF
)
5681 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5684 else if (eol_seen
& EOL_SEEN_CR
)
5686 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5692 /* Detect how a text specified in CODING is encoded. If a coding
5693 system is detected, update fields of CODING by the detected coding
5697 detect_coding (coding
)
5698 struct coding_system
*coding
;
5700 const unsigned char *src
, *src_end
;
5702 coding
->consumed
= coding
->consumed_char
= 0;
5703 coding
->produced
= coding
->produced_char
= 0;
5704 coding_set_source (coding
);
5706 src_end
= coding
->source
+ coding
->src_bytes
;
5708 /* If we have not yet decided the text encoding type, detect it
5710 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5713 struct coding_detection_info detect_info
;
5714 int null_byte_found
= 0, eight_bit_found
= 0;
5716 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5717 coding
->head_ascii
= -1;
5718 for (src
= coding
->source
; src
< src_end
; src
++)
5723 eight_bit_found
= 1;
5724 if (coding
->head_ascii
< 0)
5725 coding
->head_ascii
= src
- coding
->source
;
5726 if (null_byte_found
)
5731 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
5732 && ! inhibit_iso_escape_detection
5733 && ! detect_info
.checked
)
5735 if (coding
->head_ascii
< 0)
5736 coding
->head_ascii
= src
- coding
->source
;
5737 if (detect_coding_iso_2022 (coding
, &detect_info
))
5739 /* We have scanned the whole data. */
5740 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
5741 /* We didn't find an 8-bit code. We may have
5742 found a null-byte, but it's very rare that
5743 a binary file confirm to ISO-2022. */
5750 null_byte_found
= 1;
5751 if (eight_bit_found
)
5756 if (coding
->head_ascii
< 0)
5757 coding
->head_ascii
= src
- coding
->source
;
5759 if (null_byte_found
|| eight_bit_found
5760 || coding
->head_ascii
< coding
->src_bytes
5761 || detect_info
.found
)
5763 enum coding_category category
;
5764 struct coding_system
*this;
5766 if (coding
->head_ascii
== coding
->src_bytes
)
5767 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5768 for (i
= 0; i
< coding_category_raw_text
; i
++)
5770 category
= coding_priorities
[i
];
5771 this = coding_categories
+ category
;
5772 if (detect_info
.found
& (1 << category
))
5777 if (null_byte_found
)
5779 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
5780 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
5782 for (i
= 0; i
< coding_category_raw_text
; i
++)
5784 category
= coding_priorities
[i
];
5785 this = coding_categories
+ category
;
5788 /* No coding system of this category is defined. */
5789 detect_info
.rejected
|= (1 << category
);
5791 else if (category
>= coding_category_raw_text
)
5793 else if (detect_info
.checked
& (1 << category
))
5795 if (detect_info
.found
& (1 << category
))
5798 else if ((*(this->detector
)) (coding
, &detect_info
)
5799 && detect_info
.found
& (1 << category
))
5801 if (category
== coding_category_utf_16_auto
)
5803 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5804 category
= coding_category_utf_16_le
;
5806 category
= coding_category_utf_16_be
;
5812 if (i
< coding_category_raw_text
)
5813 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5814 else if (null_byte_found
)
5815 setup_coding_system (Qno_conversion
, coding
);
5816 else if ((detect_info
.rejected
& CATEGORY_MASK_ANY
)
5817 == CATEGORY_MASK_ANY
)
5818 setup_coding_system (Qraw_text
, coding
);
5819 else if (detect_info
.rejected
)
5820 for (i
= 0; i
< coding_category_raw_text
; i
++)
5821 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5823 this = coding_categories
+ coding_priorities
[i
];
5824 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5830 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5831 == coding_category_utf_16_auto
)
5833 Lisp_Object coding_systems
;
5834 struct coding_detection_info detect_info
;
5837 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5838 detect_info
.found
= detect_info
.rejected
= 0;
5839 if (CONSP (coding_systems
)
5840 && detect_coding_utf_16 (coding
, &detect_info
))
5842 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5843 setup_coding_system (XCAR (coding_systems
), coding
);
5844 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5845 setup_coding_system (XCDR (coding_systems
), coding
);
5853 struct coding_system
*coding
;
5855 Lisp_Object eol_type
;
5856 unsigned char *p
, *pbeg
, *pend
;
5858 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5859 if (EQ (eol_type
, Qunix
))
5862 if (NILP (coding
->dst_object
))
5863 pbeg
= coding
->destination
;
5865 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5866 pend
= pbeg
+ coding
->produced
;
5868 if (VECTORP (eol_type
))
5870 int eol_seen
= EOL_SEEN_NONE
;
5872 for (p
= pbeg
; p
< pend
; p
++)
5875 eol_seen
|= EOL_SEEN_LF
;
5876 else if (*p
== '\r')
5878 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5880 eol_seen
|= EOL_SEEN_CRLF
;
5884 eol_seen
|= EOL_SEEN_CR
;
5887 if (eol_seen
!= EOL_SEEN_NONE
5888 && eol_seen
!= EOL_SEEN_LF
5889 && eol_seen
!= EOL_SEEN_CRLF
5890 && eol_seen
!= EOL_SEEN_CR
)
5891 eol_seen
= EOL_SEEN_LF
;
5892 if (eol_seen
!= EOL_SEEN_NONE
)
5893 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5896 if (EQ (eol_type
, Qmac
))
5898 for (p
= pbeg
; p
< pend
; p
++)
5902 else if (EQ (eol_type
, Qdos
))
5906 if (NILP (coding
->dst_object
))
5908 /* Start deleting '\r' from the tail to minimize the memory
5910 for (p
= pend
- 2; p
>= pbeg
; p
--)
5913 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5919 int pos_byte
= coding
->dst_pos_byte
;
5920 int pos
= coding
->dst_pos
;
5921 int pos_end
= pos
+ coding
->produced_char
- 1;
5923 while (pos
< pos_end
)
5925 p
= BYTE_POS_ADDR (pos_byte
);
5926 if (*p
== '\r' && p
[1] == '\n')
5928 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5933 if (coding
->dst_multibyte
)
5934 pos_byte
+= BYTES_BY_CHAR_HEAD (*p
);
5939 coding
->produced
-= n
;
5940 coding
->produced_char
-= n
;
5945 /* Return a translation table (or list of them) from coding system
5946 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5947 decoding (ENCODEP is zero). */
5950 get_translation_table (attrs
, encodep
, max_lookup
)
5952 int encodep
, *max_lookup
;
5954 Lisp_Object standard
, translation_table
;
5958 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5959 standard
= Vstandard_translation_table_for_encode
;
5961 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5962 standard
= Vstandard_translation_table_for_decode
;
5963 if (NILP (translation_table
))
5964 translation_table
= standard
;
5967 if (SYMBOLP (translation_table
))
5968 translation_table
= Fget (translation_table
, Qtranslation_table
);
5969 else if (CONSP (translation_table
))
5971 translation_table
= Fcopy_sequence (translation_table
);
5972 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5973 if (SYMBOLP (XCAR (val
)))
5974 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5976 if (CHAR_TABLE_P (standard
))
5978 if (CONSP (translation_table
))
5979 translation_table
= nconc2 (translation_table
,
5980 Fcons (standard
, Qnil
));
5982 translation_table
= Fcons (translation_table
,
5983 Fcons (standard
, Qnil
));
5990 if (CHAR_TABLE_P (translation_table
)
5991 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5993 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5994 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5995 *max_lookup
= XFASTINT (val
);
5997 else if (CONSP (translation_table
))
5999 Lisp_Object tail
, val
;
6001 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
6002 if (CHAR_TABLE_P (XCAR (tail
))
6003 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
6005 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
6006 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
6007 *max_lookup
= XFASTINT (val
);
6011 return translation_table
;
6014 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
6017 if (CHAR_TABLE_P (table)) \
6019 trans = CHAR_TABLE_REF (table, c); \
6020 if (CHARACTERP (trans)) \
6021 c = XFASTINT (trans), trans = Qnil; \
6023 else if (CONSP (table)) \
6027 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
6028 if (CHAR_TABLE_P (XCAR (tail))) \
6030 trans = CHAR_TABLE_REF (XCAR (tail), c); \
6031 if (CHARACTERP (trans)) \
6032 c = XFASTINT (trans), trans = Qnil; \
6033 else if (! NILP (trans)) \
6041 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
6045 int *from_nchars
, *to_nchars
;
6047 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
6051 Lisp_Object from
, tail
;
6054 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
6059 for (i
= 0; i
< len
; i
++)
6061 if (buf
+ i
== buf_end
)
6067 if (XINT (AREF (from
, i
)) != buf
[i
])
6081 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
6089 produce_chars (coding
, translation_table
, last_block
)
6090 struct coding_system
*coding
;
6091 Lisp_Object translation_table
;
6094 unsigned char *dst
= coding
->destination
+ coding
->produced
;
6095 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
6097 EMACS_INT produced_chars
= 0;
6100 if (! coding
->chars_at_source
)
6102 /* Source characters are in coding->charbuf. */
6103 int *buf
= coding
->charbuf
;
6104 int *buf_end
= buf
+ coding
->charbuf_used
;
6106 if (EQ (coding
->src_object
, coding
->dst_object
))
6108 coding_set_source (coding
);
6109 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6112 while (buf
< buf_end
)
6118 int from_nchars
= 1, to_nchars
= 1;
6119 Lisp_Object trans
= Qnil
;
6121 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6124 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
6125 &from_nchars
, &to_nchars
);
6131 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
6133 dst
= alloc_destination (coding
,
6135 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
6137 if (EQ (coding
->src_object
, coding
->dst_object
))
6139 coding_set_source (coding
);
6140 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
6143 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6146 for (i
= 0; i
< to_nchars
; i
++)
6149 c
= XINT (AREF (trans
, i
));
6150 if (coding
->dst_multibyte
6151 || ! CHAR_BYTE8_P (c
))
6152 CHAR_STRING_ADVANCE_NO_UNIFY (c
, dst
);
6154 *dst
++ = CHAR_TO_BYTE8 (c
);
6156 produced_chars
+= to_nchars
;
6158 while (--from_nchars
> 0)
6162 /* This is an annotation datum. (-C) is the length. */
6165 carryover
= buf_end
- buf
;
6169 /* Source characters are at coding->source. */
6170 const unsigned char *src
= coding
->source
;
6171 const unsigned char *src_end
= src
+ coding
->consumed
;
6173 if (EQ (coding
->dst_object
, coding
->src_object
))
6174 dst_end
= (unsigned char *) src
;
6175 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
6177 if (coding
->src_multibyte
)
6180 EMACS_INT consumed_chars
;
6184 const unsigned char *src_base
= src
;
6190 if (EQ (coding
->src_object
, coding
->dst_object
))
6191 dst_end
= (unsigned char *) src
;
6194 EMACS_INT offset
= src
- coding
->source
;
6196 dst
= alloc_destination (coding
, src_end
- src
+ 1,
6198 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6199 coding_set_source (coding
);
6200 src
= coding
->source
+ offset
;
6201 src_end
= coding
->source
+ coding
->src_bytes
;
6202 if (EQ (coding
->src_object
, coding
->dst_object
))
6203 dst_end
= (unsigned char *) src
;
6213 while (src
< src_end
)
6218 if (dst
>= dst_end
- 1)
6220 if (EQ (coding
->src_object
, coding
->dst_object
))
6221 dst_end
= (unsigned char *) src
;
6222 if (dst
>= dst_end
- 1)
6224 EMACS_INT offset
= src
- coding
->source
;
6225 EMACS_INT more_bytes
;
6227 if (EQ (coding
->src_object
, coding
->dst_object
))
6228 more_bytes
= ((src_end
- src
) / 2) + 2;
6230 more_bytes
= src_end
- src
+ 2;
6231 dst
= alloc_destination (coding
, more_bytes
, dst
);
6232 dst_end
= coding
->destination
+ coding
->dst_bytes
;
6233 coding_set_source (coding
);
6234 src
= coding
->source
+ offset
;
6235 src_end
= coding
->source
+ coding
->src_bytes
;
6236 if (EQ (coding
->src_object
, coding
->dst_object
))
6237 dst_end
= (unsigned char *) src
;
6245 if (!EQ (coding
->src_object
, coding
->dst_object
))
6247 EMACS_INT require
= coding
->src_bytes
- coding
->dst_bytes
;
6251 EMACS_INT offset
= src
- coding
->source
;
6253 dst
= alloc_destination (coding
, require
, dst
);
6254 coding_set_source (coding
);
6255 src
= coding
->source
+ offset
;
6256 src_end
= coding
->source
+ coding
->src_bytes
;
6259 produced_chars
= coding
->consumed_char
;
6260 while (src
< src_end
)
6265 produced
= dst
- (coding
->destination
+ coding
->produced
);
6266 if (BUFFERP (coding
->dst_object
) && produced_chars
> 0)
6267 insert_from_gap (produced_chars
, produced
);
6268 coding
->produced
+= produced
;
6269 coding
->produced_char
+= produced_chars
;
6273 /* Compose text in CODING->object according to the annotation data at
6274 CHARBUF. CHARBUF is an array:
6275 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
6279 produce_composition (coding
, charbuf
, pos
)
6280 struct coding_system
*coding
;
6286 enum composition_method method
;
6287 Lisp_Object components
;
6290 to
= pos
+ charbuf
[2];
6293 method
= (enum composition_method
) (charbuf
[3]);
6295 if (method
== COMPOSITION_RELATIVE
)
6297 else if (method
>= COMPOSITION_WITH_RULE
6298 && method
<= COMPOSITION_WITH_RULE_ALTCHARS
)
6300 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
6305 for (i
= 0; i
< len
; i
++)
6307 args
[i
] = make_number (charbuf
[i
]);
6311 components
= (method
== COMPOSITION_WITH_ALTCHARS
6312 ? Fstring (len
, args
) : Fvector (len
, args
));
6316 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
6320 /* Put `charset' property on text in CODING->object according to
6321 the annotation data at CHARBUF. CHARBUF is an array:
6322 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6326 produce_charset (coding
, charbuf
, pos
)
6327 struct coding_system
*coding
;
6331 EMACS_INT from
= pos
- charbuf
[2];
6332 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
6334 Fput_text_property (make_number (from
), make_number (pos
),
6335 Qcharset
, CHARSET_NAME (charset
),
6336 coding
->dst_object
);
6340 #define CHARBUF_SIZE 0x4000
6342 #define ALLOC_CONVERSION_WORK_AREA(coding) \
6344 int size = CHARBUF_SIZE;; \
6346 coding->charbuf = NULL; \
6347 while (size > 1024) \
6349 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6350 if (coding->charbuf) \
6354 if (! coding->charbuf) \
6356 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6357 return coding->result; \
6359 coding->charbuf_size = size; \
6364 produce_annotation (coding
, pos
)
6365 struct coding_system
*coding
;
6368 int *charbuf
= coding
->charbuf
;
6369 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
6371 if (NILP (coding
->dst_object
))
6374 while (charbuf
< charbuf_end
)
6380 int len
= -*charbuf
;
6383 case CODING_ANNOTATE_COMPOSITION_MASK
:
6384 produce_composition (coding
, charbuf
, pos
);
6386 case CODING_ANNOTATE_CHARSET_MASK
:
6387 produce_charset (coding
, charbuf
, pos
);
6397 /* Decode the data at CODING->src_object into CODING->dst_object.
6398 CODING->src_object is a buffer, a string, or nil.
6399 CODING->dst_object is a buffer.
6401 If CODING->src_object is a buffer, it must be the current buffer.
6402 In this case, if CODING->src_pos is positive, it is a position of
6403 the source text in the buffer, otherwise, the source text is in the
6404 gap area of the buffer, and CODING->src_pos specifies the offset of
6405 the text from GPT (which must be the same as PT). If this is the
6406 same buffer as CODING->dst_object, CODING->src_pos must be
6409 If CODING->src_object is a string, CODING->src_pos is an index to
6412 If CODING->src_object is nil, CODING->source must already point to
6413 the non-relocatable memory area. In this case, CODING->src_pos is
6414 an offset from CODING->source.
6416 The decoded data is inserted at the current point of the buffer
6421 decode_coding (coding
)
6422 struct coding_system
*coding
;
6425 Lisp_Object undo_list
;
6426 Lisp_Object translation_table
;
6430 if (BUFFERP (coding
->src_object
)
6431 && coding
->src_pos
> 0
6432 && coding
->src_pos
< GPT
6433 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6434 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6437 if (BUFFERP (coding
->dst_object
))
6439 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6440 set_buffer_internal (XBUFFER (coding
->dst_object
));
6442 move_gap_both (PT
, PT_BYTE
);
6443 undo_list
= current_buffer
->undo_list
;
6444 current_buffer
->undo_list
= Qt
;
6447 coding
->consumed
= coding
->consumed_char
= 0;
6448 coding
->produced
= coding
->produced_char
= 0;
6449 coding
->chars_at_source
= 0;
6450 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6453 ALLOC_CONVERSION_WORK_AREA (coding
);
6455 attrs
= CODING_ID_ATTRS (coding
->id
);
6456 translation_table
= get_translation_table (attrs
, 0, NULL
);
6461 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6463 coding_set_source (coding
);
6464 coding
->annotated
= 0;
6465 coding
->charbuf_used
= carryover
;
6466 (*(coding
->decoder
)) (coding
);
6467 coding_set_destination (coding
);
6468 carryover
= produce_chars (coding
, translation_table
, 0);
6469 if (coding
->annotated
)
6470 produce_annotation (coding
, pos
);
6471 for (i
= 0; i
< carryover
; i
++)
6473 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6475 while (coding
->consumed
< coding
->src_bytes
6476 && (coding
->result
== CODING_RESULT_SUCCESS
6477 || coding
->result
== CODING_RESULT_INVALID_SRC
));
6481 coding_set_destination (coding
);
6482 coding
->charbuf_used
= carryover
;
6483 produce_chars (coding
, translation_table
, 1);
6486 coding
->carryover_bytes
= 0;
6487 if (coding
->consumed
< coding
->src_bytes
)
6489 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6490 const unsigned char *src
;
6492 coding_set_source (coding
);
6493 coding_set_destination (coding
);
6494 src
= coding
->source
+ coding
->consumed
;
6496 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6498 /* Flush out unprocessed data as binary chars. We are sure
6499 that the number of data is less than the size of
6501 coding
->charbuf_used
= 0;
6502 while (nbytes
-- > 0)
6507 c
= BYTE8_TO_CHAR (c
);
6508 coding
->charbuf
[coding
->charbuf_used
++] = c
;
6510 produce_chars (coding
, Qnil
, 1);
6514 /* Record unprocessed bytes in coding->carryover. We are
6515 sure that the number of data is less than the size of
6516 coding->carryover. */
6517 unsigned char *p
= coding
->carryover
;
6519 coding
->carryover_bytes
= nbytes
;
6520 while (nbytes
-- > 0)
6523 coding
->consumed
= coding
->src_bytes
;
6526 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6527 decode_eol (coding
);
6528 if (BUFFERP (coding
->dst_object
))
6530 current_buffer
->undo_list
= undo_list
;
6531 record_insert (coding
->dst_pos
, coding
->produced_char
);
6533 return coding
->result
;
6537 /* Extract an annotation datum from a composition starting at POS and
6538 ending before LIMIT of CODING->src_object (buffer or string), store
6539 the data in BUF, set *STOP to a starting position of the next
6540 composition (if any) or to LIMIT, and return the address of the
6541 next element of BUF.
6543 If such an annotation is not found, set *STOP to a starting
6544 position of a composition after POS (if any) or to LIMIT, and
6548 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6549 EMACS_INT pos
, limit
;
6550 struct coding_system
*coding
;
6554 EMACS_INT start
, end
;
6557 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6560 else if (start
> pos
)
6566 /* We found a composition. Store the corresponding
6567 annotation data in BUF. */
6569 enum composition_method method
= COMPOSITION_METHOD (prop
);
6570 int nchars
= COMPOSITION_LENGTH (prop
);
6572 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6573 if (method
!= COMPOSITION_RELATIVE
)
6575 Lisp_Object components
;
6578 components
= COMPOSITION_COMPONENTS (prop
);
6579 if (VECTORP (components
))
6581 len
= XVECTOR (components
)->size
;
6582 for (i
= 0; i
< len
; i
++)
6583 *buf
++ = XINT (AREF (components
, i
));
6585 else if (STRINGP (components
))
6587 len
= SCHARS (components
);
6591 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6595 else if (INTEGERP (components
))
6598 *buf
++ = XINT (components
);
6600 else if (CONSP (components
))
6602 for (len
= 0; CONSP (components
);
6603 len
++, components
= XCDR (components
))
6604 *buf
++ = XINT (XCAR (components
));
6612 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6623 /* Extract an annotation datum from a text property `charset' at POS of
6624 CODING->src_object (buffer of string), store the data in BUF, set
6625 *STOP to the position where the value of `charset' property changes
6626 (limiting by LIMIT), and return the address of the next element of
6629 If the property value is nil, set *STOP to the position where the
6630 property value is non-nil (limiting by LIMIT), and return BUF. */
6633 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6634 EMACS_INT pos
, limit
;
6635 struct coding_system
*coding
;
6639 Lisp_Object val
, next
;
6642 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6643 if (! NILP (val
) && CHARSETP (val
))
6644 id
= XINT (CHARSET_SYMBOL_ID (val
));
6647 ADD_CHARSET_DATA (buf
, 0, id
);
6648 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6650 make_number (limit
));
6651 *stop
= XINT (next
);
6657 consume_chars (coding
, translation_table
, max_lookup
)
6658 struct coding_system
*coding
;
6659 Lisp_Object translation_table
;
6662 int *buf
= coding
->charbuf
;
6663 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6664 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6665 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6666 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6667 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6668 int multibytep
= coding
->src_multibyte
;
6669 Lisp_Object eol_type
;
6671 EMACS_INT stop
, stop_composition
, stop_charset
;
6672 int *lookup_buf
= NULL
;
6674 if (! NILP (translation_table
))
6675 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6677 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6678 if (VECTORP (eol_type
))
6681 /* Note: composition handling is not yet implemented. */
6682 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6684 if (NILP (coding
->src_object
))
6685 stop
= stop_composition
= stop_charset
= end_pos
;
6688 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6689 stop
= stop_composition
= pos
;
6691 stop
= stop_composition
= end_pos
;
6692 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6693 stop
= stop_charset
= pos
;
6695 stop_charset
= end_pos
;
6698 /* Compensate for CRLF and conversion. */
6699 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6700 while (buf
< buf_end
)
6708 if (pos
== stop_composition
)
6709 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6710 buf
, &stop_composition
);
6711 if (pos
== stop_charset
)
6712 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6713 buf
, &stop_charset
);
6714 stop
= (stop_composition
< stop_charset
6715 ? stop_composition
: stop_charset
);
6722 if (coding
->encoder
== encode_coding_raw_text
)
6724 else if ((bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6725 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
+= bytes
;
6727 c
= BYTE8_TO_CHAR (*src
), src
++, pos
++;
6730 c
= STRING_CHAR_ADVANCE_NO_UNIFY (src
), pos
++;
6731 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6733 if (! EQ (eol_type
, Qunix
))
6737 if (EQ (eol_type
, Qdos
))
6745 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6750 int from_nchars
= 1, to_nchars
= 1;
6751 int *lookup_buf_end
;
6752 const unsigned char *p
= src
;
6756 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6757 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6758 lookup_buf_end
= lookup_buf
+ i
;
6759 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6760 &from_nchars
, &to_nchars
);
6762 || buf
+ to_nchars
> buf_end
)
6764 *buf
++ = *lookup_buf
;
6765 for (i
= 1; i
< to_nchars
; i
++)
6766 *buf
++ = XINT (AREF (trans
, i
));
6767 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6768 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6772 coding
->consumed
= src
- coding
->source
;
6773 coding
->consumed_char
= pos
- coding
->src_pos
;
6774 coding
->charbuf_used
= buf
- coding
->charbuf
;
6775 coding
->chars_at_source
= 0;
6779 /* Encode the text at CODING->src_object into CODING->dst_object.
6780 CODING->src_object is a buffer or a string.
6781 CODING->dst_object is a buffer or nil.
6783 If CODING->src_object is a buffer, it must be the current buffer.
6784 In this case, if CODING->src_pos is positive, it is a position of
6785 the source text in the buffer, otherwise. the source text is in the
6786 gap area of the buffer, and coding->src_pos specifies the offset of
6787 the text from GPT (which must be the same as PT). If this is the
6788 same buffer as CODING->dst_object, CODING->src_pos must be
6789 negative and CODING should not have `pre-write-conversion'.
6791 If CODING->src_object is a string, CODING should not have
6792 `pre-write-conversion'.
6794 If CODING->dst_object is a buffer, the encoded data is inserted at
6795 the current point of that buffer.
6797 If CODING->dst_object is nil, the encoded data is placed at the
6798 memory area specified by CODING->destination. */
6801 encode_coding (coding
)
6802 struct coding_system
*coding
;
6805 Lisp_Object translation_table
;
6808 attrs
= CODING_ID_ATTRS (coding
->id
);
6809 if (coding
->encoder
== encode_coding_raw_text
)
6810 translation_table
= Qnil
, max_lookup
= 0;
6812 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6814 if (BUFFERP (coding
->dst_object
))
6816 set_buffer_internal (XBUFFER (coding
->dst_object
));
6817 coding
->dst_multibyte
6818 = ! NILP (current_buffer
->enable_multibyte_characters
);
6821 coding
->consumed
= coding
->consumed_char
= 0;
6822 coding
->produced
= coding
->produced_char
= 0;
6823 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6826 ALLOC_CONVERSION_WORK_AREA (coding
);
6829 coding_set_source (coding
);
6830 consume_chars (coding
, translation_table
, max_lookup
);
6831 coding_set_destination (coding
);
6832 (*(coding
->encoder
)) (coding
);
6833 } while (coding
->consumed_char
< coding
->src_chars
);
6835 if (BUFFERP (coding
->dst_object
) && coding
->produced_char
> 0)
6836 insert_from_gap (coding
->produced_char
, coding
->produced
);
6838 return (coding
->result
);
6842 /* Name (or base name) of work buffer for code conversion. */
6843 static Lisp_Object Vcode_conversion_workbuf_name
;
6845 /* A working buffer used by the top level conversion. Once it is
6846 created, it is never destroyed. It has the name
6847 Vcode_conversion_workbuf_name. The other working buffers are
6848 destroyed after the use is finished, and their names are modified
6849 versions of Vcode_conversion_workbuf_name. */
6850 static Lisp_Object Vcode_conversion_reused_workbuf
;
6852 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6853 static int reused_workbuf_in_use
;
6856 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6857 multibyteness of returning buffer. */
6860 make_conversion_work_buffer (multibyte
)
6863 Lisp_Object name
, workbuf
;
6864 struct buffer
*current
;
6866 if (reused_workbuf_in_use
++)
6868 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6869 workbuf
= Fget_buffer_create (name
);
6873 name
= Vcode_conversion_workbuf_name
;
6874 workbuf
= Fget_buffer_create (name
);
6875 if (NILP (Vcode_conversion_reused_workbuf
))
6876 Vcode_conversion_reused_workbuf
= workbuf
;
6878 current
= current_buffer
;
6879 set_buffer_internal (XBUFFER (workbuf
));
6881 current_buffer
->undo_list
= Qt
;
6882 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6883 set_buffer_internal (current
);
6889 code_conversion_restore (arg
)
6892 Lisp_Object current
, workbuf
;
6893 struct gcpro gcpro1
;
6896 current
= XCAR (arg
);
6897 workbuf
= XCDR (arg
);
6898 if (! NILP (workbuf
))
6900 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6901 reused_workbuf_in_use
= 0;
6902 else if (! NILP (Fbuffer_live_p (workbuf
)))
6903 Fkill_buffer (workbuf
);
6905 set_buffer_internal (XBUFFER (current
));
6911 code_conversion_save (with_work_buf
, multibyte
)
6912 int with_work_buf
, multibyte
;
6914 Lisp_Object workbuf
= Qnil
;
6917 workbuf
= make_conversion_work_buffer (multibyte
);
6918 record_unwind_protect (code_conversion_restore
,
6919 Fcons (Fcurrent_buffer (), workbuf
));
6924 decode_coding_gap (coding
, chars
, bytes
)
6925 struct coding_system
*coding
;
6926 EMACS_INT chars
, bytes
;
6928 int count
= specpdl_ptr
- specpdl
;
6931 code_conversion_save (0, 0);
6933 coding
->src_object
= Fcurrent_buffer ();
6934 coding
->src_chars
= chars
;
6935 coding
->src_bytes
= bytes
;
6936 coding
->src_pos
= -chars
;
6937 coding
->src_pos_byte
= -bytes
;
6938 coding
->src_multibyte
= chars
< bytes
;
6939 coding
->dst_object
= coding
->src_object
;
6940 coding
->dst_pos
= PT
;
6941 coding
->dst_pos_byte
= PT_BYTE
;
6942 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6944 if (CODING_REQUIRE_DETECTION (coding
))
6945 detect_coding (coding
);
6947 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6948 current_buffer
->text
->inhibit_shrinking
= 1;
6949 decode_coding (coding
);
6950 current_buffer
->text
->inhibit_shrinking
= 0;
6952 attrs
= CODING_ID_ATTRS (coding
->id
);
6953 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6955 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6958 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6959 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6960 make_number (coding
->produced_char
));
6962 coding
->produced_char
+= Z
- prev_Z
;
6963 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6966 unbind_to (count
, Qnil
);
6967 return coding
->result
;
6971 encode_coding_gap (coding
, chars
, bytes
)
6972 struct coding_system
*coding
;
6973 EMACS_INT chars
, bytes
;
6975 int count
= specpdl_ptr
- specpdl
;
6977 code_conversion_save (0, 0);
6979 coding
->src_object
= Fcurrent_buffer ();
6980 coding
->src_chars
= chars
;
6981 coding
->src_bytes
= bytes
;
6982 coding
->src_pos
= -chars
;
6983 coding
->src_pos_byte
= -bytes
;
6984 coding
->src_multibyte
= chars
< bytes
;
6985 coding
->dst_object
= coding
->src_object
;
6986 coding
->dst_pos
= PT
;
6987 coding
->dst_pos_byte
= PT_BYTE
;
6989 encode_coding (coding
);
6991 unbind_to (count
, Qnil
);
6992 return coding
->result
;
6996 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6997 SRC_OBJECT into DST_OBJECT by coding context CODING.
6999 SRC_OBJECT is a buffer, a string, or Qnil.
7001 If it is a buffer, the text is at point of the buffer. FROM and TO
7002 are positions in the buffer.
7004 If it is a string, the text is at the beginning of the string.
7005 FROM and TO are indices to the string.
7007 If it is nil, the text is at coding->source. FROM and TO are
7008 indices to coding->source.
7010 DST_OBJECT is a buffer, Qt, or Qnil.
7012 If it is a buffer, the decoded text is inserted at point of the
7013 buffer. If the buffer is the same as SRC_OBJECT, the source text
7016 If it is Qt, a string is made from the decoded text, and
7017 set in CODING->dst_object.
7019 If it is Qnil, the decoded text is stored at CODING->destination.
7020 The caller must allocate CODING->dst_bytes bytes at
7021 CODING->destination by xmalloc. If the decoded text is longer than
7022 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
7026 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7028 struct coding_system
*coding
;
7029 Lisp_Object src_object
;
7030 EMACS_INT from
, from_byte
, to
, to_byte
;
7031 Lisp_Object dst_object
;
7033 int count
= specpdl_ptr
- specpdl
;
7034 unsigned char *destination
;
7035 EMACS_INT dst_bytes
;
7036 EMACS_INT chars
= to
- from
;
7037 EMACS_INT bytes
= to_byte
- from_byte
;
7039 int saved_pt
= -1, saved_pt_byte
;
7040 int need_marker_adjustment
= 0;
7041 Lisp_Object old_deactivate_mark
;
7043 old_deactivate_mark
= Vdeactivate_mark
;
7045 if (NILP (dst_object
))
7047 destination
= coding
->destination
;
7048 dst_bytes
= coding
->dst_bytes
;
7051 coding
->src_object
= src_object
;
7052 coding
->src_chars
= chars
;
7053 coding
->src_bytes
= bytes
;
7054 coding
->src_multibyte
= chars
< bytes
;
7056 if (STRINGP (src_object
))
7058 coding
->src_pos
= from
;
7059 coding
->src_pos_byte
= from_byte
;
7061 else if (BUFFERP (src_object
))
7063 set_buffer_internal (XBUFFER (src_object
));
7065 move_gap_both (from
, from_byte
);
7066 if (EQ (src_object
, dst_object
))
7068 struct Lisp_Marker
*tail
;
7070 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7072 tail
->need_adjustment
7073 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7074 need_marker_adjustment
|= tail
->need_adjustment
;
7076 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7077 TEMP_SET_PT_BOTH (from
, from_byte
);
7078 current_buffer
->text
->inhibit_shrinking
= 1;
7079 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7080 coding
->src_pos
= -chars
;
7081 coding
->src_pos_byte
= -bytes
;
7085 coding
->src_pos
= from
;
7086 coding
->src_pos_byte
= from_byte
;
7090 if (CODING_REQUIRE_DETECTION (coding
))
7091 detect_coding (coding
);
7092 attrs
= CODING_ID_ATTRS (coding
->id
);
7094 if (EQ (dst_object
, Qt
)
7095 || (! NILP (CODING_ATTR_POST_READ (attrs
))
7096 && NILP (dst_object
)))
7098 coding
->dst_multibyte
= !CODING_FOR_UNIBYTE (coding
);
7099 coding
->dst_object
= code_conversion_save (1, coding
->dst_multibyte
);
7100 coding
->dst_pos
= BEG
;
7101 coding
->dst_pos_byte
= BEG_BYTE
;
7103 else if (BUFFERP (dst_object
))
7105 code_conversion_save (0, 0);
7106 coding
->dst_object
= dst_object
;
7107 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
7108 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
7109 coding
->dst_multibyte
7110 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7114 code_conversion_save (0, 0);
7115 coding
->dst_object
= Qnil
;
7116 /* Most callers presume this will return a multibyte result, and they
7117 won't use `binary' or `raw-text' anyway, so let's not worry about
7118 CODING_FOR_UNIBYTE. */
7119 coding
->dst_multibyte
= 1;
7122 decode_coding (coding
);
7124 if (BUFFERP (coding
->dst_object
))
7125 set_buffer_internal (XBUFFER (coding
->dst_object
));
7127 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
7129 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7130 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
7133 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
7134 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7135 old_deactivate_mark
);
7136 val
= safe_call1 (CODING_ATTR_POST_READ (attrs
),
7137 make_number (coding
->produced_char
));
7140 coding
->produced_char
+= Z
- prev_Z
;
7141 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
7144 if (EQ (dst_object
, Qt
))
7146 coding
->dst_object
= Fbuffer_string ();
7148 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
7150 set_buffer_internal (XBUFFER (coding
->dst_object
));
7151 if (dst_bytes
< coding
->produced
)
7153 destination
= xrealloc (destination
, coding
->produced
);
7156 record_conversion_result (coding
,
7157 CODING_RESULT_INSUFFICIENT_DST
);
7158 unbind_to (count
, Qnil
);
7161 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
7162 move_gap_both (BEGV
, BEGV_BYTE
);
7163 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
7164 coding
->destination
= destination
;
7170 /* This is the case of:
7171 (BUFFERP (src_object) && EQ (src_object, dst_object))
7172 As we have moved PT while replacing the original buffer
7173 contents, we must recover it now. */
7174 set_buffer_internal (XBUFFER (src_object
));
7175 current_buffer
->text
->inhibit_shrinking
= 0;
7176 if (saved_pt
< from
)
7177 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7178 else if (saved_pt
< from
+ chars
)
7179 TEMP_SET_PT_BOTH (from
, from_byte
);
7180 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7181 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7182 saved_pt_byte
+ (coding
->produced
- bytes
));
7184 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7185 saved_pt_byte
+ (coding
->produced
- bytes
));
7187 if (need_marker_adjustment
)
7189 struct Lisp_Marker
*tail
;
7191 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7192 if (tail
->need_adjustment
)
7194 tail
->need_adjustment
= 0;
7195 if (tail
->insertion_type
)
7197 tail
->bytepos
= from_byte
;
7198 tail
->charpos
= from
;
7202 tail
->bytepos
= from_byte
+ coding
->produced
;
7204 = (NILP (current_buffer
->enable_multibyte_characters
)
7205 ? tail
->bytepos
: from
+ coding
->produced_char
);
7211 Vdeactivate_mark
= old_deactivate_mark
;
7212 unbind_to (count
, coding
->dst_object
);
7217 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
7219 struct coding_system
*coding
;
7220 Lisp_Object src_object
;
7221 EMACS_INT from
, from_byte
, to
, to_byte
;
7222 Lisp_Object dst_object
;
7224 int count
= specpdl_ptr
- specpdl
;
7225 EMACS_INT chars
= to
- from
;
7226 EMACS_INT bytes
= to_byte
- from_byte
;
7228 int saved_pt
= -1, saved_pt_byte
;
7229 int need_marker_adjustment
= 0;
7230 int kill_src_buffer
= 0;
7231 Lisp_Object old_deactivate_mark
;
7233 old_deactivate_mark
= Vdeactivate_mark
;
7235 coding
->src_object
= src_object
;
7236 coding
->src_chars
= chars
;
7237 coding
->src_bytes
= bytes
;
7238 coding
->src_multibyte
= chars
< bytes
;
7240 attrs
= CODING_ID_ATTRS (coding
->id
);
7242 if (EQ (src_object
, dst_object
))
7244 struct Lisp_Marker
*tail
;
7246 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7248 tail
->need_adjustment
7249 = tail
->charpos
== (tail
->insertion_type
? from
: to
);
7250 need_marker_adjustment
|= tail
->need_adjustment
;
7254 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
7256 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
7257 set_buffer_internal (XBUFFER (coding
->src_object
));
7258 if (STRINGP (src_object
))
7259 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
7260 else if (BUFFERP (src_object
))
7261 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
7263 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
7265 if (EQ (src_object
, dst_object
))
7267 set_buffer_internal (XBUFFER (src_object
));
7268 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7269 del_range_both (from
, from_byte
, to
, to_byte
, 1);
7270 set_buffer_internal (XBUFFER (coding
->src_object
));
7274 Lisp_Object args
[3];
7275 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
7277 GCPRO5 (coding
->src_object
, coding
->dst_object
, src_object
, dst_object
,
7278 old_deactivate_mark
);
7279 args
[0] = CODING_ATTR_PRE_WRITE (attrs
);
7280 args
[1] = make_number (BEG
);
7281 args
[2] = make_number (Z
);
7282 safe_call (3, args
);
7285 if (XBUFFER (coding
->src_object
) != current_buffer
)
7286 kill_src_buffer
= 1;
7287 coding
->src_object
= Fcurrent_buffer ();
7289 move_gap_both (BEG
, BEG_BYTE
);
7290 coding
->src_chars
= Z
- BEG
;
7291 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
7292 coding
->src_pos
= BEG
;
7293 coding
->src_pos_byte
= BEG_BYTE
;
7294 coding
->src_multibyte
= Z
< Z_BYTE
;
7296 else if (STRINGP (src_object
))
7298 code_conversion_save (0, 0);
7299 coding
->src_pos
= from
;
7300 coding
->src_pos_byte
= from_byte
;
7302 else if (BUFFERP (src_object
))
7304 code_conversion_save (0, 0);
7305 set_buffer_internal (XBUFFER (src_object
));
7306 if (EQ (src_object
, dst_object
))
7308 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
7309 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
7310 coding
->src_pos
= 0;
7311 coding
->src_pos_byte
= 0;
7315 if (from
< GPT
&& to
>= GPT
)
7316 move_gap_both (from
, from_byte
);
7317 coding
->src_pos
= from
;
7318 coding
->src_pos_byte
= from_byte
;
7322 code_conversion_save (0, 0);
7324 if (BUFFERP (dst_object
))
7326 coding
->dst_object
= dst_object
;
7327 if (EQ (src_object
, dst_object
))
7329 coding
->dst_pos
= from
;
7330 coding
->dst_pos_byte
= from_byte
;
7334 struct buffer
*current
= current_buffer
;
7336 set_buffer_temp (XBUFFER (dst_object
));
7337 coding
->dst_pos
= PT
;
7338 coding
->dst_pos_byte
= PT_BYTE
;
7339 move_gap_both (coding
->dst_pos
, coding
->dst_pos_byte
);
7340 set_buffer_temp (current
);
7342 coding
->dst_multibyte
7343 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
7345 else if (EQ (dst_object
, Qt
))
7347 coding
->dst_object
= Qnil
;
7348 coding
->dst_bytes
= coding
->src_chars
;
7349 if (coding
->dst_bytes
== 0)
7350 coding
->dst_bytes
= 1;
7351 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
7352 coding
->dst_multibyte
= 0;
7356 coding
->dst_object
= Qnil
;
7357 coding
->dst_multibyte
= 0;
7360 encode_coding (coding
);
7362 if (EQ (dst_object
, Qt
))
7364 if (BUFFERP (coding
->dst_object
))
7365 coding
->dst_object
= Fbuffer_string ();
7369 = make_unibyte_string ((char *) coding
->destination
,
7371 xfree (coding
->destination
);
7377 /* This is the case of:
7378 (BUFFERP (src_object) && EQ (src_object, dst_object))
7379 As we have moved PT while replacing the original buffer
7380 contents, we must recover it now. */
7381 set_buffer_internal (XBUFFER (src_object
));
7382 if (saved_pt
< from
)
7383 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
7384 else if (saved_pt
< from
+ chars
)
7385 TEMP_SET_PT_BOTH (from
, from_byte
);
7386 else if (! NILP (current_buffer
->enable_multibyte_characters
))
7387 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
7388 saved_pt_byte
+ (coding
->produced
- bytes
));
7390 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
7391 saved_pt_byte
+ (coding
->produced
- bytes
));
7393 if (need_marker_adjustment
)
7395 struct Lisp_Marker
*tail
;
7397 for (tail
= BUF_MARKERS (current_buffer
); tail
; tail
= tail
->next
)
7398 if (tail
->need_adjustment
)
7400 tail
->need_adjustment
= 0;
7401 if (tail
->insertion_type
)
7403 tail
->bytepos
= from_byte
;
7404 tail
->charpos
= from
;
7408 tail
->bytepos
= from_byte
+ coding
->produced
;
7410 = (NILP (current_buffer
->enable_multibyte_characters
)
7411 ? tail
->bytepos
: from
+ coding
->produced_char
);
7417 if (kill_src_buffer
)
7418 Fkill_buffer (coding
->src_object
);
7420 Vdeactivate_mark
= old_deactivate_mark
;
7421 unbind_to (count
, Qnil
);
7426 preferred_coding_system ()
7428 int id
= coding_categories
[coding_priorities
[0]].id
;
7430 return CODING_ID_NAME (id
);
7435 /*** 8. Emacs Lisp library functions ***/
7437 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
7438 doc
: /* Return t if OBJECT is nil or a coding-system.
7439 See the documentation of `define-coding-system' for information
7440 about coding-system objects. */)
7445 || CODING_SYSTEM_ID (object
) >= 0)
7447 if (! SYMBOLP (object
)
7448 || NILP (Fget (object
, Qcoding_system_define_form
)))
7453 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
7454 Sread_non_nil_coding_system
, 1, 1, 0,
7455 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7462 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7463 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
7465 while (SCHARS (val
) == 0);
7466 return (Fintern (val
, Qnil
));
7469 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
7470 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7471 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.
7472 Ignores case when completing coding systems (all Emacs coding systems
7473 are lower-case). */)
7474 (prompt
, default_coding_system
)
7475 Lisp_Object prompt
, default_coding_system
;
7478 int count
= SPECPDL_INDEX ();
7480 if (SYMBOLP (default_coding_system
))
7481 default_coding_system
= SYMBOL_NAME (default_coding_system
);
7482 specbind (Qcompletion_ignore_case
, Qt
);
7483 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
7484 Qt
, Qnil
, Qcoding_system_history
,
7485 default_coding_system
, Qnil
);
7486 unbind_to (count
, Qnil
);
7487 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
7490 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
7492 doc
: /* Check validity of CODING-SYSTEM.
7493 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7494 It is valid if it is nil or a symbol defined as a coding system by the
7495 function `define-coding-system'. */)
7497 Lisp_Object coding_system
;
7499 Lisp_Object define_form
;
7501 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
7502 if (! NILP (define_form
))
7504 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
7505 safe_eval (define_form
);
7507 if (!NILP (Fcoding_system_p (coding_system
)))
7508 return coding_system
;
7509 xsignal1 (Qcoding_system_error
, coding_system
);
7513 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7514 HIGHEST is nonzero, return the coding system of the highest
7515 priority among the detected coding systems. Otherwize return a
7516 list of detected coding systems sorted by their priorities. If
7517 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7518 multibyte form but contains only ASCII and eight-bit chars.
7519 Otherwise, the bytes are raw bytes.
7521 CODING-SYSTEM controls the detection as below:
7523 If it is nil, detect both text-format and eol-format. If the
7524 text-format part of CODING-SYSTEM is already specified
7525 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7526 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7527 detect only text-format. */
7530 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
7532 const unsigned char *src
;
7533 EMACS_INT src_chars
, src_bytes
;
7536 Lisp_Object coding_system
;
7538 const unsigned char *src_end
= src
+ src_bytes
;
7539 Lisp_Object attrs
, eol_type
;
7541 struct coding_system coding
;
7543 struct coding_detection_info detect_info
;
7544 enum coding_category base_category
;
7545 int null_byte_found
= 0, eight_bit_found
= 0;
7547 if (NILP (coding_system
))
7548 coding_system
= Qundecided
;
7549 setup_coding_system (coding_system
, &coding
);
7550 attrs
= CODING_ID_ATTRS (coding
.id
);
7551 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
7552 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7554 coding
.source
= src
;
7555 coding
.src_chars
= src_chars
;
7556 coding
.src_bytes
= src_bytes
;
7557 coding
.src_multibyte
= multibytep
;
7558 coding
.consumed
= 0;
7559 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7561 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7563 /* At first, detect text-format if necessary. */
7564 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7565 if (base_category
== coding_category_undecided
)
7567 enum coding_category category
;
7568 struct coding_system
*this;
7571 coding
.head_ascii
= -1;
7572 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7573 for (; src
< src_end
; src
++)
7578 eight_bit_found
= 1;
7579 if (coding
.head_ascii
< 0)
7580 coding
.head_ascii
= src
- coding
.source
;
7581 if (null_byte_found
)
7586 if ((c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
7587 && ! inhibit_iso_escape_detection
7588 && ! detect_info
.checked
)
7590 if (coding
.head_ascii
< 0)
7591 coding
.head_ascii
= src
- coding
.source
;
7592 if (detect_coding_iso_2022 (&coding
, &detect_info
))
7594 /* We have scanned the whole data. */
7595 if (! (detect_info
.rejected
& CATEGORY_MASK_ISO_7_ELSE
))
7596 /* We didn't find an 8-bit code. We may have
7597 found a null-byte, but it's very rare that
7598 a binary file confirm to ISO-2022. */
7605 null_byte_found
= 1;
7606 if (eight_bit_found
)
7611 if (coding
.head_ascii
< 0)
7612 coding
.head_ascii
= src
- coding
.source
;
7614 if (null_byte_found
|| eight_bit_found
7615 || coding
.head_ascii
< coding
.src_bytes
7616 || detect_info
.found
)
7618 if (coding
.head_ascii
== coding
.src_bytes
)
7619 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7620 for (i
= 0; i
< coding_category_raw_text
; i
++)
7622 category
= coding_priorities
[i
];
7623 this = coding_categories
+ category
;
7624 if (detect_info
.found
& (1 << category
))
7629 if (null_byte_found
)
7631 detect_info
.checked
|= ~CATEGORY_MASK_UTF_16
;
7632 detect_info
.rejected
|= ~CATEGORY_MASK_UTF_16
;
7634 for (i
= 0; i
< coding_category_raw_text
; i
++)
7636 category
= coding_priorities
[i
];
7637 this = coding_categories
+ category
;
7641 /* No coding system of this category is defined. */
7642 detect_info
.rejected
|= (1 << category
);
7644 else if (category
>= coding_category_raw_text
)
7646 else if (detect_info
.checked
& (1 << category
))
7649 && (detect_info
.found
& (1 << category
)))
7652 else if ((*(this->detector
)) (&coding
, &detect_info
)
7654 && (detect_info
.found
& (1 << category
)))
7656 if (category
== coding_category_utf_16_auto
)
7658 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7659 category
= coding_category_utf_16_le
;
7661 category
= coding_category_utf_16_be
;
7669 if ((detect_info
.rejected
& CATEGORY_MASK_ANY
) == CATEGORY_MASK_ANY
)
7671 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7672 id
= coding_categories
[coding_category_raw_text
].id
;
7673 val
= Fcons (make_number (id
), Qnil
);
7675 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7677 detect_info
.found
= CATEGORY_MASK_ANY
;
7678 id
= coding_categories
[coding_category_undecided
].id
;
7679 val
= Fcons (make_number (id
), Qnil
);
7683 if (detect_info
.found
)
7685 detect_info
.found
= 1 << category
;
7686 val
= Fcons (make_number (this->id
), Qnil
);
7689 for (i
= 0; i
< coding_category_raw_text
; i
++)
7690 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7692 detect_info
.found
= 1 << coding_priorities
[i
];
7693 id
= coding_categories
[coding_priorities
[i
]].id
;
7694 val
= Fcons (make_number (id
), Qnil
);
7700 int mask
= detect_info
.rejected
| detect_info
.found
;
7704 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7706 category
= coding_priorities
[i
];
7707 if (! (mask
& (1 << category
)))
7709 found
|= 1 << category
;
7710 id
= coding_categories
[category
].id
;
7712 val
= Fcons (make_number (id
), val
);
7715 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7717 category
= coding_priorities
[i
];
7718 if (detect_info
.found
& (1 << category
))
7720 id
= coding_categories
[category
].id
;
7721 val
= Fcons (make_number (id
), val
);
7724 detect_info
.found
|= found
;
7727 else if (base_category
== coding_category_utf_16_auto
)
7729 if (detect_coding_utf_16 (&coding
, &detect_info
))
7731 struct coding_system
*this;
7733 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7734 this = coding_categories
+ coding_category_utf_16_le
;
7735 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7736 this = coding_categories
+ coding_category_utf_16_be
;
7737 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7738 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7740 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7741 val
= Fcons (make_number (this->id
), Qnil
);
7746 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7747 val
= Fcons (make_number (coding
.id
), Qnil
);
7750 /* Then, detect eol-format if necessary. */
7752 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7755 if (VECTORP (eol_type
))
7757 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7759 if (null_byte_found
)
7760 normal_eol
= EOL_SEEN_LF
;
7762 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7763 coding_category_raw_text
);
7765 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7766 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7767 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7768 coding_category_utf_16_be
);
7769 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7770 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7771 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7772 coding_category_utf_16_le
);
7776 if (EQ (eol_type
, Qunix
))
7777 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7778 else if (EQ (eol_type
, Qdos
))
7779 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7781 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7784 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7786 enum coding_category category
;
7789 id
= XINT (XCAR (tail
));
7790 attrs
= CODING_ID_ATTRS (id
);
7791 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7792 eol_type
= CODING_ID_EOL_TYPE (id
);
7793 if (VECTORP (eol_type
))
7795 if (category
== coding_category_utf_16_be
7796 || category
== coding_category_utf_16_be_nosig
)
7797 this_eol
= utf_16_be_eol
;
7798 else if (category
== coding_category_utf_16_le
7799 || category
== coding_category_utf_16_le_nosig
)
7800 this_eol
= utf_16_le_eol
;
7802 this_eol
= normal_eol
;
7804 if (this_eol
== EOL_SEEN_LF
)
7805 XSETCAR (tail
, AREF (eol_type
, 0));
7806 else if (this_eol
== EOL_SEEN_CRLF
)
7807 XSETCAR (tail
, AREF (eol_type
, 1));
7808 else if (this_eol
== EOL_SEEN_CR
)
7809 XSETCAR (tail
, AREF (eol_type
, 2));
7811 XSETCAR (tail
, CODING_ID_NAME (id
));
7814 XSETCAR (tail
, CODING_ID_NAME (id
));
7818 return (highest
? XCAR (val
) : val
);
7822 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7824 doc
: /* Detect coding system of the text in the region between START and END.
7825 Return a list of possible coding systems ordered by priority.
7827 If only ASCII characters are found (except for such ISO-2022 control
7828 characters as ESC), it returns a list of single element `undecided'
7829 or its subsidiary coding system according to a detected end-of-line
7832 If optional argument HIGHEST is non-nil, return the coding system of
7833 highest priority. */)
7834 (start
, end
, highest
)
7835 Lisp_Object start
, end
, highest
;
7838 int from_byte
, to_byte
;
7840 CHECK_NUMBER_COERCE_MARKER (start
);
7841 CHECK_NUMBER_COERCE_MARKER (end
);
7843 validate_region (&start
, &end
);
7844 from
= XINT (start
), to
= XINT (end
);
7845 from_byte
= CHAR_TO_BYTE (from
);
7846 to_byte
= CHAR_TO_BYTE (to
);
7848 if (from
< GPT
&& to
>= GPT
)
7849 move_gap_both (to
, to_byte
);
7851 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7852 to
- from
, to_byte
- from_byte
,
7854 !NILP (current_buffer
7855 ->enable_multibyte_characters
),
7859 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7861 doc
: /* Detect coding system of the text in STRING.
7862 Return a list of possible coding systems ordered by priority.
7864 If only ASCII characters are found (except for such ISO-2022 control
7865 characters as ESC), it returns a list of single element `undecided'
7866 or its subsidiary coding system according to a detected end-of-line
7869 If optional argument HIGHEST is non-nil, return the coding system of
7870 highest priority. */)
7872 Lisp_Object string
, highest
;
7874 CHECK_STRING (string
);
7876 return detect_coding_system (SDATA (string
),
7877 SCHARS (string
), SBYTES (string
),
7878 !NILP (highest
), STRING_MULTIBYTE (string
),
7884 char_encodable_p (c
, attrs
)
7889 struct charset
*charset
;
7890 Lisp_Object translation_table
;
7892 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7893 if (! NILP (translation_table
))
7894 c
= translate_char (translation_table
, c
);
7895 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7896 CONSP (tail
); tail
= XCDR (tail
))
7898 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7899 if (CHAR_CHARSET_P (c
, charset
))
7902 return (! NILP (tail
));
7906 /* Return a list of coding systems that safely encode the text between
7907 START and END. If EXCLUDE is non-nil, it is a list of coding
7908 systems not to check. The returned list doesn't contain any such
7909 coding systems. In any case, if the text contains only ASCII or is
7910 unibyte, return t. */
7912 DEFUN ("find-coding-systems-region-internal",
7913 Ffind_coding_systems_region_internal
,
7914 Sfind_coding_systems_region_internal
, 2, 3, 0,
7915 doc
: /* Internal use only. */)
7916 (start
, end
, exclude
)
7917 Lisp_Object start
, end
, exclude
;
7919 Lisp_Object coding_attrs_list
, safe_codings
;
7920 EMACS_INT start_byte
, end_byte
;
7921 const unsigned char *p
, *pbeg
, *pend
;
7923 Lisp_Object tail
, elt
;
7925 if (STRINGP (start
))
7927 if (!STRING_MULTIBYTE (start
)
7928 || SCHARS (start
) == SBYTES (start
))
7931 end_byte
= SBYTES (start
);
7935 CHECK_NUMBER_COERCE_MARKER (start
);
7936 CHECK_NUMBER_COERCE_MARKER (end
);
7937 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7938 args_out_of_range (start
, end
);
7939 if (NILP (current_buffer
->enable_multibyte_characters
))
7941 start_byte
= CHAR_TO_BYTE (XINT (start
));
7942 end_byte
= CHAR_TO_BYTE (XINT (end
));
7943 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7946 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7948 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7949 move_gap_both (XINT (start
), start_byte
);
7951 move_gap_both (XINT (end
), end_byte
);
7955 coding_attrs_list
= Qnil
;
7956 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7958 || NILP (Fmemq (XCAR (tail
), exclude
)))
7962 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7963 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7964 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7966 ASET (attrs
, coding_attr_trans_tbl
,
7967 get_translation_table (attrs
, 1, NULL
));
7968 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7972 if (STRINGP (start
))
7973 p
= pbeg
= SDATA (start
);
7975 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7976 pend
= p
+ (end_byte
- start_byte
);
7978 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7979 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7983 if (ASCII_BYTE_P (*p
))
7987 c
= STRING_CHAR_ADVANCE (p
);
7989 charset_map_loaded
= 0;
7990 for (tail
= coding_attrs_list
; CONSP (tail
);)
7995 else if (char_encodable_p (c
, elt
))
7997 else if (CONSP (XCDR (tail
)))
7999 XSETCAR (tail
, XCAR (XCDR (tail
)));
8000 XSETCDR (tail
, XCDR (XCDR (tail
)));
8004 XSETCAR (tail
, Qnil
);
8008 if (charset_map_loaded
)
8010 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8012 if (STRINGP (start
))
8013 pbeg
= SDATA (start
);
8015 pbeg
= BYTE_POS_ADDR (start_byte
);
8016 p
= pbeg
+ p_offset
;
8017 pend
= pbeg
+ pend_offset
;
8022 safe_codings
= list2 (Qraw_text
, Qno_conversion
);
8023 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
8024 if (! NILP (XCAR (tail
)))
8025 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
8027 return safe_codings
;
8031 DEFUN ("unencodable-char-position", Funencodable_char_position
,
8032 Sunencodable_char_position
, 3, 5, 0,
8034 Return position of first un-encodable character in a region.
8035 START and END specify the region and CODING-SYSTEM specifies the
8036 encoding to check. Return nil if CODING-SYSTEM does encode the region.
8038 If optional 4th argument COUNT is non-nil, it specifies at most how
8039 many un-encodable characters to search. In this case, the value is a
8042 If optional 5th argument STRING is non-nil, it is a string to search
8043 for un-encodable characters. In that case, START and END are indexes
8045 (start
, end
, coding_system
, count
, string
)
8046 Lisp_Object start
, end
, coding_system
, count
, string
;
8049 struct coding_system coding
;
8050 Lisp_Object attrs
, charset_list
, translation_table
;
8051 Lisp_Object positions
;
8053 const unsigned char *p
, *stop
, *pend
;
8054 int ascii_compatible
;
8056 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
8057 attrs
= CODING_ID_ATTRS (coding
.id
);
8058 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
8060 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
8061 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8062 translation_table
= get_translation_table (attrs
, 1, NULL
);
8066 validate_region (&start
, &end
);
8067 from
= XINT (start
);
8069 if (NILP (current_buffer
->enable_multibyte_characters
)
8070 || (ascii_compatible
8071 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
8073 p
= CHAR_POS_ADDR (from
);
8074 pend
= CHAR_POS_ADDR (to
);
8075 if (from
< GPT
&& to
>= GPT
)
8082 CHECK_STRING (string
);
8083 CHECK_NATNUM (start
);
8085 from
= XINT (start
);
8088 || to
> SCHARS (string
))
8089 args_out_of_range_3 (string
, start
, end
);
8090 if (! STRING_MULTIBYTE (string
))
8092 p
= SDATA (string
) + string_char_to_byte (string
, from
);
8093 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
8094 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
8102 CHECK_NATNUM (count
);
8111 if (ascii_compatible
)
8112 while (p
< stop
&& ASCII_BYTE_P (*p
))
8122 c
= STRING_CHAR_ADVANCE (p
);
8123 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
8124 && ! char_charset (translate_char (translation_table
, c
),
8125 charset_list
, NULL
))
8127 positions
= Fcons (make_number (from
), positions
);
8136 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
8140 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
8141 Scheck_coding_systems_region
, 3, 3, 0,
8142 doc
: /* Check if the region is encodable by coding systems.
8144 START and END are buffer positions specifying the region.
8145 CODING-SYSTEM-LIST is a list of coding systems to check.
8147 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
8148 CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the
8149 whole region, POS0, POS1, ... are buffer positions where non-encodable
8150 characters are found.
8152 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
8155 START may be a string. In that case, check if the string is
8156 encodable, and the value contains indices to the string instead of
8157 buffer positions. END is ignored. */)
8158 (start
, end
, coding_system_list
)
8159 Lisp_Object start
, end
, coding_system_list
;
8162 EMACS_INT start_byte
, end_byte
;
8164 const unsigned char *p
, *pbeg
, *pend
;
8166 Lisp_Object tail
, elt
, attrs
;
8168 if (STRINGP (start
))
8170 if (!STRING_MULTIBYTE (start
)
8171 && SCHARS (start
) != SBYTES (start
))
8174 end_byte
= SBYTES (start
);
8179 CHECK_NUMBER_COERCE_MARKER (start
);
8180 CHECK_NUMBER_COERCE_MARKER (end
);
8181 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
8182 args_out_of_range (start
, end
);
8183 if (NILP (current_buffer
->enable_multibyte_characters
))
8185 start_byte
= CHAR_TO_BYTE (XINT (start
));
8186 end_byte
= CHAR_TO_BYTE (XINT (end
));
8187 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
8190 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
8192 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
8193 move_gap_both (XINT (start
), start_byte
);
8195 move_gap_both (XINT (end
), end_byte
);
8201 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
8204 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
8205 ASET (attrs
, coding_attr_trans_tbl
,
8206 get_translation_table (attrs
, 1, NULL
));
8207 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
8210 if (STRINGP (start
))
8211 p
= pbeg
= SDATA (start
);
8213 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
8214 pend
= p
+ (end_byte
- start_byte
);
8216 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
8217 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
8221 if (ASCII_BYTE_P (*p
))
8225 c
= STRING_CHAR_ADVANCE (p
);
8227 charset_map_loaded
= 0;
8228 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
8230 elt
= XCDR (XCAR (tail
));
8231 if (! char_encodable_p (c
, XCAR (elt
)))
8232 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
8234 if (charset_map_loaded
)
8236 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
8238 if (STRINGP (start
))
8239 pbeg
= SDATA (start
);
8241 pbeg
= BYTE_POS_ADDR (start_byte
);
8242 p
= pbeg
+ p_offset
;
8243 pend
= pbeg
+ pend_offset
;
8251 for (; CONSP (tail
); tail
= XCDR (tail
))
8254 if (CONSP (XCDR (XCDR (elt
))))
8255 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
8264 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
8265 Lisp_Object start
, end
, coding_system
, dst_object
;
8266 int encodep
, norecord
;
8268 struct coding_system coding
;
8269 EMACS_INT from
, from_byte
, to
, to_byte
;
8270 Lisp_Object src_object
;
8272 CHECK_NUMBER_COERCE_MARKER (start
);
8273 CHECK_NUMBER_COERCE_MARKER (end
);
8274 if (NILP (coding_system
))
8275 coding_system
= Qno_conversion
;
8277 CHECK_CODING_SYSTEM (coding_system
);
8278 src_object
= Fcurrent_buffer ();
8279 if (NILP (dst_object
))
8280 dst_object
= src_object
;
8281 else if (! EQ (dst_object
, Qt
))
8282 CHECK_BUFFER (dst_object
);
8284 validate_region (&start
, &end
);
8285 from
= XFASTINT (start
);
8286 from_byte
= CHAR_TO_BYTE (from
);
8287 to
= XFASTINT (end
);
8288 to_byte
= CHAR_TO_BYTE (to
);
8290 setup_coding_system (coding_system
, &coding
);
8291 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8294 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8297 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
8300 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8302 return (BUFFERP (dst_object
)
8303 ? make_number (coding
.produced_char
)
8304 : coding
.dst_object
);
8308 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
8309 3, 4, "r\nzCoding system: ",
8310 doc
: /* Decode the current region from the specified coding system.
8311 When called from a program, takes four arguments:
8312 START, END, CODING-SYSTEM, and DESTINATION.
8313 START and END are buffer positions.
8315 Optional 4th arguments DESTINATION specifies where the decoded text goes.
8316 If nil, the region between START and END is replaced by the decoded text.
8317 If buffer, the decoded text is inserted in the buffer.
8318 In those cases, the length of the decoded text is returned..
8319 If DESTINATION is t, the decoded text is returned.
8321 This function sets `last-coding-system-used' to the precise coding system
8322 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8323 not fully specified.) */)
8324 (start
, end
, coding_system
, destination
)
8325 Lisp_Object start
, end
, coding_system
, destination
;
8327 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
8330 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
8331 3, 4, "r\nzCoding system: ",
8332 doc
: /* Encode the current region by specified coding system.
8333 When called from a program, takes four arguments:
8334 START, END, CODING-SYSTEM and DESTINATION.
8335 START and END are buffer positions.
8337 Optional 4th arguments DESTINATION specifies where the encoded text goes.
8338 If nil, the region between START and END is replace by the encoded text.
8339 If buffer, the encoded text is inserted in the buffer.
8340 In those cases, the length of the encoded text is returned..
8341 If DESTINATION is t, the encoded text is returned.
8343 This function sets `last-coding-system-used' to the precise coding system
8344 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8345 not fully specified.) */)
8346 (start
, end
, coding_system
, destination
)
8347 Lisp_Object start
, end
, coding_system
, destination
;
8349 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
8353 code_convert_string (string
, coding_system
, dst_object
,
8354 encodep
, nocopy
, norecord
)
8355 Lisp_Object string
, coding_system
, dst_object
;
8356 int encodep
, nocopy
, norecord
;
8358 struct coding_system coding
;
8359 EMACS_INT chars
, bytes
;
8361 CHECK_STRING (string
);
8362 if (NILP (coding_system
))
8365 Vlast_coding_system_used
= Qno_conversion
;
8366 if (NILP (dst_object
))
8367 return (nocopy
? Fcopy_sequence (string
) : string
);
8370 if (NILP (coding_system
))
8371 coding_system
= Qno_conversion
;
8373 CHECK_CODING_SYSTEM (coding_system
);
8374 if (NILP (dst_object
))
8376 else if (! EQ (dst_object
, Qt
))
8377 CHECK_BUFFER (dst_object
);
8379 setup_coding_system (coding_system
, &coding
);
8380 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
8381 chars
= SCHARS (string
);
8382 bytes
= SBYTES (string
);
8384 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8386 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
8388 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
8390 return (BUFFERP (dst_object
)
8391 ? make_number (coding
.produced_char
)
8392 : coding
.dst_object
);
8396 /* Encode or decode STRING according to CODING_SYSTEM.
8397 Do not set Vlast_coding_system_used.
8399 This function is called only from macros DECODE_FILE and
8400 ENCODE_FILE, thus we ignore character composition. */
8403 code_convert_string_norecord (string
, coding_system
, encodep
)
8404 Lisp_Object string
, coding_system
;
8407 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
8411 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
8413 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8415 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8416 if the decoding operation is trivial.
8418 Optional fourth arg BUFFER non-nil means that the decoded text is
8419 inserted in BUFFER instead of returned as a string. In this case,
8420 the return value is the length of the decoded text.
8422 This function sets `last-coding-system-used' to the precise coding system
8423 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8424 not fully specified.) */)
8425 (string
, coding_system
, nocopy
, buffer
)
8426 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8428 return code_convert_string (string
, coding_system
, buffer
,
8429 0, ! NILP (nocopy
), 0);
8432 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
8434 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
8436 Optional third arg NOCOPY non-nil means it is OK to return STRING
8437 itself if the encoding operation is trivial.
8439 Optional fourth arg BUFFER non-nil means that the encoded text is
8440 inserted in BUFFER instead of returned as a string. In this case,
8441 the return value is the length of the encoded.text.
8443 This function sets `last-coding-system-used' to the precise coding system
8444 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8445 not fully specified.) */)
8446 (string
, coding_system
, nocopy
, buffer
)
8447 Lisp_Object string
, coding_system
, nocopy
, buffer
;
8449 return code_convert_string (string
, coding_system
, buffer
,
8450 1, ! NILP (nocopy
), 1);
8454 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
8455 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
8456 Return the corresponding character. */)
8460 Lisp_Object spec
, attrs
, val
;
8461 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
8464 CHECK_NATNUM (code
);
8465 c
= XFASTINT (code
);
8466 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8467 attrs
= AREF (spec
, 0);
8469 if (ASCII_BYTE_P (c
)
8470 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8473 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8474 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8475 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8476 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8479 charset
= charset_roman
;
8480 else if (c
>= 0xA0 && c
< 0xDF)
8482 charset
= charset_kana
;
8487 int s1
= c
>> 8, s2
= c
& 0xFF;
8489 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
8490 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
8491 error ("Invalid code: %d", code
);
8493 charset
= charset_kanji
;
8495 c
= DECODE_CHAR (charset
, c
);
8497 error ("Invalid code: %d", code
);
8498 return make_number (c
);
8502 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
8503 doc
: /* Encode a Japanese character CH to shift_jis encoding.
8504 Return the corresponding code in SJIS. */)
8508 Lisp_Object spec
, attrs
, charset_list
;
8510 struct charset
*charset
;
8513 CHECK_CHARACTER (ch
);
8515 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
8516 attrs
= AREF (spec
, 0);
8518 if (ASCII_CHAR_P (c
)
8519 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8522 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8523 charset
= char_charset (c
, charset_list
, &code
);
8524 if (code
== CHARSET_INVALID_CODE (charset
))
8525 error ("Can't encode by shift_jis encoding: %d", c
);
8528 return make_number (code
);
8531 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
8532 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
8533 Return the corresponding character. */)
8537 Lisp_Object spec
, attrs
, val
;
8538 struct charset
*charset_roman
, *charset_big5
, *charset
;
8541 CHECK_NATNUM (code
);
8542 c
= XFASTINT (code
);
8543 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8544 attrs
= AREF (spec
, 0);
8546 if (ASCII_BYTE_P (c
)
8547 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8550 val
= CODING_ATTR_CHARSET_LIST (attrs
);
8551 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
8552 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
8555 charset
= charset_roman
;
8558 int b1
= c
>> 8, b2
= c
& 0x7F;
8559 if (b1
< 0xA1 || b1
> 0xFE
8560 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
8561 error ("Invalid code: %d", code
);
8562 charset
= charset_big5
;
8564 c
= DECODE_CHAR (charset
, (unsigned )c
);
8566 error ("Invalid code: %d", code
);
8567 return make_number (c
);
8570 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
8571 doc
: /* Encode the Big5 character CH to BIG5 coding system.
8572 Return the corresponding character code in Big5. */)
8576 Lisp_Object spec
, attrs
, charset_list
;
8577 struct charset
*charset
;
8581 CHECK_CHARACTER (ch
);
8583 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
8584 attrs
= AREF (spec
, 0);
8585 if (ASCII_CHAR_P (c
)
8586 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
8589 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
8590 charset
= char_charset (c
, charset_list
, &code
);
8591 if (code
== CHARSET_INVALID_CODE (charset
))
8592 error ("Can't encode by Big5 encoding: %d", c
);
8594 return make_number (code
);
8598 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
8599 Sset_terminal_coding_system_internal
, 1, 2, 0,
8600 doc
: /* Internal use only. */)
8601 (coding_system
, terminal
)
8602 Lisp_Object coding_system
;
8603 Lisp_Object terminal
;
8605 struct coding_system
*terminal_coding
= TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
8606 CHECK_SYMBOL (coding_system
);
8607 setup_coding_system (Fcheck_coding_system (coding_system
), terminal_coding
);
8608 /* We had better not send unsafe characters to terminal. */
8609 terminal_coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
8610 /* Characer composition should be disabled. */
8611 terminal_coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8612 terminal_coding
->src_multibyte
= 1;
8613 terminal_coding
->dst_multibyte
= 0;
8617 DEFUN ("set-safe-terminal-coding-system-internal",
8618 Fset_safe_terminal_coding_system_internal
,
8619 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8620 doc
: /* Internal use only. */)
8622 Lisp_Object coding_system
;
8624 CHECK_SYMBOL (coding_system
);
8625 setup_coding_system (Fcheck_coding_system (coding_system
),
8626 &safe_terminal_coding
);
8627 /* Characer composition should be disabled. */
8628 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8629 safe_terminal_coding
.src_multibyte
= 1;
8630 safe_terminal_coding
.dst_multibyte
= 0;
8634 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
8635 Sterminal_coding_system
, 0, 1, 0,
8636 doc
: /* Return coding system specified for terminal output on the given terminal.
8637 TERMINAL may be a terminal id, a frame, or nil for the selected
8638 frame's terminal device. */)
8640 Lisp_Object terminal
;
8642 struct coding_system
*terminal_coding
8643 = TERMINAL_TERMINAL_CODING (get_terminal (terminal
, 1));
8644 Lisp_Object coding_system
= CODING_ID_NAME (terminal_coding
->id
);
8646 /* For backward compatibility, return nil if it is `undecided'. */
8647 return (! EQ (coding_system
, Qundecided
) ? coding_system
: Qnil
);
8650 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
8651 Sset_keyboard_coding_system_internal
, 1, 2, 0,
8652 doc
: /* Internal use only. */)
8653 (coding_system
, terminal
)
8654 Lisp_Object coding_system
;
8655 Lisp_Object terminal
;
8657 struct terminal
*t
= get_terminal (terminal
, 1);
8658 CHECK_SYMBOL (coding_system
);
8659 setup_coding_system (Fcheck_coding_system (coding_system
),
8660 TERMINAL_KEYBOARD_CODING (t
));
8661 /* Characer composition should be disabled. */
8662 TERMINAL_KEYBOARD_CODING (t
)->common_flags
8663 &= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8667 DEFUN ("keyboard-coding-system",
8668 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 1, 0,
8669 doc
: /* Return coding system specified for decoding keyboard input. */)
8671 Lisp_Object terminal
;
8673 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
8674 (get_terminal (terminal
, 1))->id
);
8678 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8679 Sfind_operation_coding_system
, 1, MANY
, 0,
8680 doc
: /* Choose a coding system for an operation based on the target name.
8681 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8682 DECODING-SYSTEM is the coding system to use for decoding
8683 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8684 for encoding (in case OPERATION does encoding).
8686 The first argument OPERATION specifies an I/O primitive:
8687 For file I/O, `insert-file-contents' or `write-region'.
8688 For process I/O, `call-process', `call-process-region', or `start-process'.
8689 For network I/O, `open-network-stream'.
8691 The remaining arguments should be the same arguments that were passed
8692 to the primitive. Depending on which primitive, one of those arguments
8693 is selected as the TARGET. For example, if OPERATION does file I/O,
8694 whichever argument specifies the file name is TARGET.
8696 TARGET has a meaning which depends on OPERATION:
8697 For file I/O, TARGET is a file name (except for the special case below).
8698 For process I/O, TARGET is a process name.
8699 For network I/O, TARGET is a service name or a port number.
8701 This function looks up what is specified for TARGET in
8702 `file-coding-system-alist', `process-coding-system-alist',
8703 or `network-coding-system-alist' depending on OPERATION.
8704 They may specify a coding system, a cons of coding systems,
8705 or a function symbol to call.
8706 In the last case, we call the function with one argument,
8707 which is a list of all the arguments given to this function.
8708 If the function can't decide a coding system, it can return
8709 `undecided' so that the normal code-detection is performed.
8711 If OPERATION is `insert-file-contents', the argument corresponding to
8712 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8713 file name to look up, and BUFFER is a buffer that contains the file's
8714 contents (not yet decoded). If `file-coding-system-alist' specifies a
8715 function to call for FILENAME, that function should examine the
8716 contents of BUFFER instead of reading the file.
8718 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
8723 Lisp_Object operation
, target_idx
, target
, val
;
8724 register Lisp_Object chain
;
8727 error ("Too few arguments");
8728 operation
= args
[0];
8729 if (!SYMBOLP (operation
)
8730 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8731 error ("Invalid first argument");
8732 if (nargs
< 1 + XINT (target_idx
))
8733 error ("Too few arguments for operation: %s",
8734 SDATA (SYMBOL_NAME (operation
)));
8735 target
= args
[XINT (target_idx
) + 1];
8736 if (!(STRINGP (target
)
8737 || (EQ (operation
, Qinsert_file_contents
) && CONSP (target
)
8738 && STRINGP (XCAR (target
)) && BUFFERP (XCDR (target
)))
8739 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8740 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8742 target
= XCAR (target
);
8744 chain
= ((EQ (operation
, Qinsert_file_contents
)
8745 || EQ (operation
, Qwrite_region
))
8746 ? Vfile_coding_system_alist
8747 : (EQ (operation
, Qopen_network_stream
)
8748 ? Vnetwork_coding_system_alist
8749 : Vprocess_coding_system_alist
));
8753 for (; CONSP (chain
); chain
= XCDR (chain
))
8759 && ((STRINGP (target
)
8760 && STRINGP (XCAR (elt
))
8761 && fast_string_match (XCAR (elt
), target
) >= 0)
8762 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8765 /* Here, if VAL is both a valid coding system and a valid
8766 function symbol, we return VAL as a coding system. */
8769 if (! SYMBOLP (val
))
8771 if (! NILP (Fcoding_system_p (val
)))
8772 return Fcons (val
, val
);
8773 if (! NILP (Ffboundp (val
)))
8775 /* We use call1 rather than safe_call1
8776 so as to get bug reports about functions called here
8777 which don't handle the current interface. */
8778 val
= call1 (val
, Flist (nargs
, args
));
8781 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8782 return Fcons (val
, val
);
8790 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8791 Sset_coding_system_priority
, 0, MANY
, 0,
8792 doc
: /* Assign higher priority to the coding systems given as arguments.
8793 If multiple coding systems belong to the same category,
8794 all but the first one are ignored.
8796 usage: (set-coding-system-priority &rest coding-systems) */)
8802 int changed
[coding_category_max
];
8803 enum coding_category priorities
[coding_category_max
];
8805 bzero (changed
, sizeof changed
);
8807 for (i
= j
= 0; i
< nargs
; i
++)
8809 enum coding_category category
;
8810 Lisp_Object spec
, attrs
;
8812 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8813 attrs
= AREF (spec
, 0);
8814 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8815 if (changed
[category
])
8816 /* Ignore this coding system because a coding system of the
8817 same category already had a higher priority. */
8819 changed
[category
] = 1;
8820 priorities
[j
++] = category
;
8821 if (coding_categories
[category
].id
>= 0
8822 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8823 setup_coding_system (args
[i
], &coding_categories
[category
]);
8824 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8827 /* Now we have decided top J priorities. Reflect the order of the
8828 original priorities to the remaining priorities. */
8830 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8832 while (j
< coding_category_max
8833 && changed
[coding_priorities
[j
]])
8835 if (j
== coding_category_max
)
8837 priorities
[i
] = coding_priorities
[j
];
8840 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8842 /* Update `coding-category-list'. */
8843 Vcoding_category_list
= Qnil
;
8844 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8845 Vcoding_category_list
8846 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8847 Vcoding_category_list
);
8852 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8853 Scoding_system_priority_list
, 0, 1, 0,
8854 doc
: /* Return a list of coding systems ordered by their priorities.
8855 HIGHESTP non-nil means just return the highest priority one. */)
8857 Lisp_Object highestp
;
8862 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8864 enum coding_category category
= coding_priorities
[i
];
8865 int id
= coding_categories
[category
].id
;
8870 attrs
= CODING_ID_ATTRS (id
);
8871 if (! NILP (highestp
))
8872 return CODING_ATTR_BASE_NAME (attrs
);
8873 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8875 return Fnreverse (val
);
8878 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8881 make_subsidiaries (base
)
8884 Lisp_Object subsidiaries
;
8885 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8886 char *buf
= (char *) alloca (base_name_len
+ 6);
8889 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8890 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8891 for (i
= 0; i
< 3; i
++)
8893 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8894 ASET (subsidiaries
, i
, intern (buf
));
8896 return subsidiaries
;
8900 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8901 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8902 doc
: /* For internal use only.
8903 usage: (define-coding-system-internal ...) */)
8909 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8910 Lisp_Object attrs
; /* Vector of attributes. */
8911 Lisp_Object eol_type
;
8912 Lisp_Object aliases
;
8913 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8914 enum coding_category category
;
8915 Lisp_Object tail
, val
;
8916 int max_charset_id
= 0;
8919 if (nargs
< coding_arg_max
)
8922 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8924 name
= args
[coding_arg_name
];
8925 CHECK_SYMBOL (name
);
8926 CODING_ATTR_BASE_NAME (attrs
) = name
;
8928 val
= args
[coding_arg_mnemonic
];
8929 if (! STRINGP (val
))
8930 CHECK_CHARACTER (val
);
8931 CODING_ATTR_MNEMONIC (attrs
) = val
;
8933 coding_type
= args
[coding_arg_coding_type
];
8934 CHECK_SYMBOL (coding_type
);
8935 CODING_ATTR_TYPE (attrs
) = coding_type
;
8937 charset_list
= args
[coding_arg_charset_list
];
8938 if (SYMBOLP (charset_list
))
8940 if (EQ (charset_list
, Qiso_2022
))
8942 if (! EQ (coding_type
, Qiso_2022
))
8943 error ("Invalid charset-list");
8944 charset_list
= Viso_2022_charset_list
;
8946 else if (EQ (charset_list
, Qemacs_mule
))
8948 if (! EQ (coding_type
, Qemacs_mule
))
8949 error ("Invalid charset-list");
8950 charset_list
= Vemacs_mule_charset_list
;
8952 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8953 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8954 max_charset_id
= XFASTINT (XCAR (tail
));
8958 charset_list
= Fcopy_sequence (charset_list
);
8959 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8961 struct charset
*charset
;
8964 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8965 if (EQ (coding_type
, Qiso_2022
)
8966 ? CHARSET_ISO_FINAL (charset
) < 0
8967 : EQ (coding_type
, Qemacs_mule
)
8968 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8970 error ("Can't handle charset `%s'",
8971 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8973 XSETCAR (tail
, make_number (charset
->id
));
8974 if (max_charset_id
< charset
->id
)
8975 max_charset_id
= charset
->id
;
8978 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8980 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8982 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8983 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8984 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8986 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8988 val
= args
[coding_arg_decode_translation_table
];
8989 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8991 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8993 val
= args
[coding_arg_encode_translation_table
];
8994 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8996 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8998 val
= args
[coding_arg_post_read_conversion
];
9000 CODING_ATTR_POST_READ (attrs
) = val
;
9002 val
= args
[coding_arg_pre_write_conversion
];
9004 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9006 val
= args
[coding_arg_default_char
];
9008 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
9011 CHECK_CHARACTER (val
);
9012 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9015 val
= args
[coding_arg_for_unibyte
];
9016 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
9018 val
= args
[coding_arg_plist
];
9020 CODING_ATTR_PLIST (attrs
) = val
;
9022 if (EQ (coding_type
, Qcharset
))
9024 /* Generate a lisp vector of 256 elements. Each element is nil,
9025 integer, or a list of charset IDs.
9027 If Nth element is nil, the byte code N is invalid in this
9030 If Nth element is a number NUM, N is the first byte of a
9031 charset whose ID is NUM.
9033 If Nth element is a list of charset IDs, N is the first byte
9034 of one of them. The list is sorted by dimensions of the
9035 charsets. A charset of smaller dimension comes firtst. */
9036 val
= Fmake_vector (make_number (256), Qnil
);
9038 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
9040 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
9041 int dim
= CHARSET_DIMENSION (charset
);
9042 int idx
= (dim
- 1) * 4;
9044 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9045 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9047 for (i
= charset
->code_space
[idx
];
9048 i
<= charset
->code_space
[idx
+ 1]; i
++)
9050 Lisp_Object tmp
, tmp2
;
9053 tmp
= AREF (val
, i
);
9056 else if (NUMBERP (tmp
))
9058 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
9060 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
9062 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
9066 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
9068 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
9073 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
9076 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
9077 XSETCAR (tmp2
, XCAR (tail
));
9083 ASET (attrs
, coding_attr_charset_valids
, val
);
9084 category
= coding_category_charset
;
9086 else if (EQ (coding_type
, Qccl
))
9090 if (nargs
< coding_arg_ccl_max
)
9093 val
= args
[coding_arg_ccl_decoder
];
9094 CHECK_CCL_PROGRAM (val
);
9096 val
= Fcopy_sequence (val
);
9097 ASET (attrs
, coding_attr_ccl_decoder
, val
);
9099 val
= args
[coding_arg_ccl_encoder
];
9100 CHECK_CCL_PROGRAM (val
);
9102 val
= Fcopy_sequence (val
);
9103 ASET (attrs
, coding_attr_ccl_encoder
, val
);
9105 val
= args
[coding_arg_ccl_valids
];
9106 valids
= Fmake_string (make_number (256), make_number (0));
9107 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
9114 from
= to
= XINT (val
);
9115 if (from
< 0 || from
> 255)
9116 args_out_of_range_3 (val
, make_number (0), make_number (255));
9121 CHECK_NATNUM_CAR (val
);
9122 CHECK_NATNUM_CDR (val
);
9123 from
= XINT (XCAR (val
));
9125 args_out_of_range_3 (XCAR (val
),
9126 make_number (0), make_number (255));
9127 to
= XINT (XCDR (val
));
9128 if (to
< from
|| to
> 255)
9129 args_out_of_range_3 (XCDR (val
),
9130 XCAR (val
), make_number (255));
9132 for (i
= from
; i
<= to
; i
++)
9133 SSET (valids
, i
, 1);
9135 ASET (attrs
, coding_attr_ccl_valids
, valids
);
9137 category
= coding_category_ccl
;
9139 else if (EQ (coding_type
, Qutf_16
))
9141 Lisp_Object bom
, endian
;
9143 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9145 if (nargs
< coding_arg_utf16_max
)
9148 bom
= args
[coding_arg_utf16_bom
];
9149 if (! NILP (bom
) && ! EQ (bom
, Qt
))
9153 CHECK_CODING_SYSTEM (val
);
9155 CHECK_CODING_SYSTEM (val
);
9157 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
9159 endian
= args
[coding_arg_utf16_endian
];
9160 CHECK_SYMBOL (endian
);
9163 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
9164 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
9165 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
9167 category
= (CONSP (bom
)
9168 ? coding_category_utf_16_auto
9170 ? (EQ (endian
, Qbig
)
9171 ? coding_category_utf_16_be_nosig
9172 : coding_category_utf_16_le_nosig
)
9173 : (EQ (endian
, Qbig
)
9174 ? coding_category_utf_16_be
9175 : coding_category_utf_16_le
));
9177 else if (EQ (coding_type
, Qiso_2022
))
9179 Lisp_Object initial
, reg_usage
, request
, flags
;
9182 if (nargs
< coding_arg_iso2022_max
)
9185 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
9186 CHECK_VECTOR (initial
);
9187 for (i
= 0; i
< 4; i
++)
9189 val
= Faref (initial
, make_number (i
));
9192 struct charset
*charset
;
9194 CHECK_CHARSET_GET_CHARSET (val
, charset
);
9195 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
9196 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
9197 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9200 ASET (initial
, i
, make_number (-1));
9203 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
9204 CHECK_CONS (reg_usage
);
9205 CHECK_NUMBER_CAR (reg_usage
);
9206 CHECK_NUMBER_CDR (reg_usage
);
9208 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
9209 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
9217 CHECK_CHARSET_GET_ID (tmp
, id
);
9218 CHECK_NATNUM_CDR (val
);
9219 if (XINT (XCDR (val
)) >= 4)
9220 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
9221 XSETCAR (val
, make_number (id
));
9224 flags
= args
[coding_arg_iso2022_flags
];
9225 CHECK_NATNUM (flags
);
9227 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
9228 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
9230 ASET (attrs
, coding_attr_iso_initial
, initial
);
9231 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
9232 ASET (attrs
, coding_attr_iso_request
, request
);
9233 ASET (attrs
, coding_attr_iso_flags
, flags
);
9234 setup_iso_safe_charsets (attrs
);
9236 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
9237 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
9238 | CODING_ISO_FLAG_SINGLE_SHIFT
))
9239 ? coding_category_iso_7_else
9240 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9241 ? coding_category_iso_7
9242 : coding_category_iso_7_tight
);
9245 int id
= XINT (AREF (initial
, 1));
9247 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
9248 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
9250 ? coding_category_iso_8_else
9251 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
9252 ? coding_category_iso_8_1
9253 : coding_category_iso_8_2
);
9255 if (category
!= coding_category_iso_8_1
9256 && category
!= coding_category_iso_8_2
)
9257 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
9259 else if (EQ (coding_type
, Qemacs_mule
))
9261 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
9262 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
9263 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9264 category
= coding_category_emacs_mule
;
9266 else if (EQ (coding_type
, Qshift_jis
))
9269 struct charset
*charset
;
9271 if (XINT (Flength (charset_list
)) != 3
9272 && XINT (Flength (charset_list
)) != 4)
9273 error ("There should be three or four charsets");
9275 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9276 if (CHARSET_DIMENSION (charset
) != 1)
9277 error ("Dimension of charset %s is not one",
9278 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9279 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9280 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9282 charset_list
= XCDR (charset_list
);
9283 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9284 if (CHARSET_DIMENSION (charset
) != 1)
9285 error ("Dimension of charset %s is not one",
9286 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9288 charset_list
= XCDR (charset_list
);
9289 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9290 if (CHARSET_DIMENSION (charset
) != 2)
9291 error ("Dimension of charset %s is not two",
9292 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9294 charset_list
= XCDR (charset_list
);
9295 if (! NILP (charset_list
))
9297 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9298 if (CHARSET_DIMENSION (charset
) != 2)
9299 error ("Dimension of charset %s is not two",
9300 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9303 category
= coding_category_sjis
;
9304 Vsjis_coding_system
= name
;
9306 else if (EQ (coding_type
, Qbig5
))
9308 struct charset
*charset
;
9310 if (XINT (Flength (charset_list
)) != 2)
9311 error ("There should be just two charsets");
9313 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9314 if (CHARSET_DIMENSION (charset
) != 1)
9315 error ("Dimension of charset %s is not one",
9316 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9317 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
9318 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9320 charset_list
= XCDR (charset_list
);
9321 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
9322 if (CHARSET_DIMENSION (charset
) != 2)
9323 error ("Dimension of charset %s is not two",
9324 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
9326 category
= coding_category_big5
;
9327 Vbig5_coding_system
= name
;
9329 else if (EQ (coding_type
, Qraw_text
))
9331 category
= coding_category_raw_text
;
9332 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9334 else if (EQ (coding_type
, Qutf_8
))
9336 category
= coding_category_utf_8
;
9337 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
9339 else if (EQ (coding_type
, Qundecided
))
9340 category
= coding_category_undecided
;
9342 error ("Invalid coding system type: %s",
9343 SDATA (SYMBOL_NAME (coding_type
)));
9345 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
9346 CODING_ATTR_PLIST (attrs
)
9347 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
9348 CODING_ATTR_PLIST (attrs
)));
9349 CODING_ATTR_PLIST (attrs
)
9350 = Fcons (QCascii_compatible_p
,
9351 Fcons (CODING_ATTR_ASCII_COMPAT (attrs
),
9352 CODING_ATTR_PLIST (attrs
)));
9354 eol_type
= args
[coding_arg_eol_type
];
9355 if (! NILP (eol_type
)
9356 && ! EQ (eol_type
, Qunix
)
9357 && ! EQ (eol_type
, Qdos
)
9358 && ! EQ (eol_type
, Qmac
))
9359 error ("Invalid eol-type");
9361 aliases
= Fcons (name
, Qnil
);
9363 if (NILP (eol_type
))
9365 eol_type
= make_subsidiaries (name
);
9366 for (i
= 0; i
< 3; i
++)
9368 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
9370 this_name
= AREF (eol_type
, i
);
9371 this_aliases
= Fcons (this_name
, Qnil
);
9372 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
9373 this_spec
= Fmake_vector (make_number (3), attrs
);
9374 ASET (this_spec
, 1, this_aliases
);
9375 ASET (this_spec
, 2, this_eol_type
);
9376 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
9377 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
9378 val
= Fassoc (Fsymbol_name (this_name
), Vcoding_system_alist
);
9380 Vcoding_system_alist
9381 = Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
9382 Vcoding_system_alist
);
9386 spec_vec
= Fmake_vector (make_number (3), attrs
);
9387 ASET (spec_vec
, 1, aliases
);
9388 ASET (spec_vec
, 2, eol_type
);
9390 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
9391 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
9392 val
= Fassoc (Fsymbol_name (name
), Vcoding_system_alist
);
9394 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
9395 Vcoding_system_alist
);
9398 int id
= coding_categories
[category
].id
;
9400 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
9401 setup_coding_system (name
, &coding_categories
[category
]);
9407 return Fsignal (Qwrong_number_of_arguments
,
9408 Fcons (intern ("define-coding-system-internal"),
9409 make_number (nargs
)));
9413 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
9415 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9416 (coding_system
, prop
, val
)
9417 Lisp_Object coding_system
, prop
, val
;
9419 Lisp_Object spec
, attrs
;
9421 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9422 attrs
= AREF (spec
, 0);
9423 if (EQ (prop
, QCmnemonic
))
9425 if (! STRINGP (val
))
9426 CHECK_CHARACTER (val
);
9427 CODING_ATTR_MNEMONIC (attrs
) = val
;
9429 else if (EQ (prop
, QCdefalut_char
))
9432 val
= make_number (' ');
9434 CHECK_CHARACTER (val
);
9435 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
9437 else if (EQ (prop
, QCdecode_translation_table
))
9439 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9441 CODING_ATTR_DECODE_TBL (attrs
) = val
;
9443 else if (EQ (prop
, QCencode_translation_table
))
9445 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
9447 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
9449 else if (EQ (prop
, QCpost_read_conversion
))
9452 CODING_ATTR_POST_READ (attrs
) = val
;
9454 else if (EQ (prop
, QCpre_write_conversion
))
9457 CODING_ATTR_PRE_WRITE (attrs
) = val
;
9459 else if (EQ (prop
, QCascii_compatible_p
))
9461 CODING_ATTR_ASCII_COMPAT (attrs
) = val
;
9464 CODING_ATTR_PLIST (attrs
)
9465 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
9470 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
9471 Sdefine_coding_system_alias
, 2, 2, 0,
9472 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9473 (alias
, coding_system
)
9474 Lisp_Object alias
, coding_system
;
9476 Lisp_Object spec
, aliases
, eol_type
, val
;
9478 CHECK_SYMBOL (alias
);
9479 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9480 aliases
= AREF (spec
, 1);
9481 /* ALIASES should be a list of length more than zero, and the first
9482 element is a base coding system. Append ALIAS at the tail of the
9484 while (!NILP (XCDR (aliases
)))
9485 aliases
= XCDR (aliases
);
9486 XSETCDR (aliases
, Fcons (alias
, Qnil
));
9488 eol_type
= AREF (spec
, 2);
9489 if (VECTORP (eol_type
))
9491 Lisp_Object subsidiaries
;
9494 subsidiaries
= make_subsidiaries (alias
);
9495 for (i
= 0; i
< 3; i
++)
9496 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
9497 AREF (eol_type
, i
));
9500 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
9501 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
9502 val
= Fassoc (Fsymbol_name (alias
), Vcoding_system_alist
);
9504 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
9505 Vcoding_system_alist
);
9510 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
9512 doc
: /* Return the base of CODING-SYSTEM.
9513 Any alias or subsidiary coding system is not a base coding system. */)
9515 Lisp_Object coding_system
;
9517 Lisp_Object spec
, attrs
;
9519 if (NILP (coding_system
))
9520 return (Qno_conversion
);
9521 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9522 attrs
= AREF (spec
, 0);
9523 return CODING_ATTR_BASE_NAME (attrs
);
9526 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
9528 doc
: "Return the property list of CODING-SYSTEM.")
9530 Lisp_Object coding_system
;
9532 Lisp_Object spec
, attrs
;
9534 if (NILP (coding_system
))
9535 coding_system
= Qno_conversion
;
9536 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9537 attrs
= AREF (spec
, 0);
9538 return CODING_ATTR_PLIST (attrs
);
9542 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
9544 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
9546 Lisp_Object coding_system
;
9550 if (NILP (coding_system
))
9551 coding_system
= Qno_conversion
;
9552 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
9553 return AREF (spec
, 1);
9556 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
9557 Scoding_system_eol_type
, 1, 1, 0,
9558 doc
: /* Return eol-type of CODING-SYSTEM.
9559 An eol-type is an integer 0, 1, 2, or a vector of coding systems.
9561 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9562 and CR respectively.
9564 A vector value indicates that a format of end-of-line should be
9565 detected automatically. Nth element of the vector is the subsidiary
9566 coding system whose eol-type is N. */)
9568 Lisp_Object coding_system
;
9570 Lisp_Object spec
, eol_type
;
9573 if (NILP (coding_system
))
9574 coding_system
= Qno_conversion
;
9575 if (! CODING_SYSTEM_P (coding_system
))
9577 spec
= CODING_SYSTEM_SPEC (coding_system
);
9578 eol_type
= AREF (spec
, 2);
9579 if (VECTORP (eol_type
))
9580 return Fcopy_sequence (eol_type
);
9581 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
9582 return make_number (n
);
9588 /*** 9. Post-amble ***/
9595 for (i
= 0; i
< coding_category_max
; i
++)
9597 coding_categories
[i
].id
= -1;
9598 coding_priorities
[i
] = i
;
9601 /* ISO2022 specific initialize routine. */
9602 for (i
= 0; i
< 0x20; i
++)
9603 iso_code_class
[i
] = ISO_control_0
;
9604 for (i
= 0x21; i
< 0x7F; i
++)
9605 iso_code_class
[i
] = ISO_graphic_plane_0
;
9606 for (i
= 0x80; i
< 0xA0; i
++)
9607 iso_code_class
[i
] = ISO_control_1
;
9608 for (i
= 0xA1; i
< 0xFF; i
++)
9609 iso_code_class
[i
] = ISO_graphic_plane_1
;
9610 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
9611 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
9612 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
9613 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
9614 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
9615 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
9616 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
9617 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
9618 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
9620 for (i
= 0; i
< 256; i
++)
9622 emacs_mule_bytes
[i
] = 1;
9624 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
9625 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
9626 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
9627 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
9635 staticpro (&Vcoding_system_hash_table
);
9637 Lisp_Object args
[2];
9640 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
9643 staticpro (&Vsjis_coding_system
);
9644 Vsjis_coding_system
= Qnil
;
9646 staticpro (&Vbig5_coding_system
);
9647 Vbig5_coding_system
= Qnil
;
9649 staticpro (&Vcode_conversion_reused_workbuf
);
9650 Vcode_conversion_reused_workbuf
= Qnil
;
9652 staticpro (&Vcode_conversion_workbuf_name
);
9653 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9655 reused_workbuf_in_use
= 0;
9657 DEFSYM (Qcharset
, "charset");
9658 DEFSYM (Qtarget_idx
, "target-idx");
9659 DEFSYM (Qcoding_system_history
, "coding-system-history");
9660 Fset (Qcoding_system_history
, Qnil
);
9662 /* Target FILENAME is the first argument. */
9663 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9664 /* Target FILENAME is the third argument. */
9665 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9667 DEFSYM (Qcall_process
, "call-process");
9668 /* Target PROGRAM is the first argument. */
9669 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9671 DEFSYM (Qcall_process_region
, "call-process-region");
9672 /* Target PROGRAM is the third argument. */
9673 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9675 DEFSYM (Qstart_process
, "start-process");
9676 /* Target PROGRAM is the third argument. */
9677 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9679 DEFSYM (Qopen_network_stream
, "open-network-stream");
9680 /* Target SERVICE is the fourth argument. */
9681 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9683 DEFSYM (Qcoding_system
, "coding-system");
9684 DEFSYM (Qcoding_aliases
, "coding-aliases");
9686 DEFSYM (Qeol_type
, "eol-type");
9687 DEFSYM (Qunix
, "unix");
9688 DEFSYM (Qdos
, "dos");
9690 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9691 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9692 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9693 DEFSYM (Qdefault_char
, "default-char");
9694 DEFSYM (Qundecided
, "undecided");
9695 DEFSYM (Qno_conversion
, "no-conversion");
9696 DEFSYM (Qraw_text
, "raw-text");
9698 DEFSYM (Qiso_2022
, "iso-2022");
9700 DEFSYM (Qutf_8
, "utf-8");
9701 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9703 DEFSYM (Qutf_16
, "utf-16");
9704 DEFSYM (Qbig
, "big");
9705 DEFSYM (Qlittle
, "little");
9707 DEFSYM (Qshift_jis
, "shift-jis");
9708 DEFSYM (Qbig5
, "big5");
9710 DEFSYM (Qcoding_system_p
, "coding-system-p");
9712 DEFSYM (Qcoding_system_error
, "coding-system-error");
9713 Fput (Qcoding_system_error
, Qerror_conditions
,
9714 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9715 Fput (Qcoding_system_error
, Qerror_message
,
9716 build_string ("Invalid coding system"));
9718 /* Intern this now in case it isn't already done.
9719 Setting this variable twice is harmless.
9720 But don't staticpro it here--that is done in alloc.c. */
9721 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9723 DEFSYM (Qtranslation_table
, "translation-table");
9724 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9725 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9726 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9727 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9729 DEFSYM (Qvalid_codes
, "valid-codes");
9731 DEFSYM (Qemacs_mule
, "emacs-mule");
9733 DEFSYM (QCcategory
, ":category");
9734 DEFSYM (QCmnemonic
, ":mnemonic");
9735 DEFSYM (QCdefalut_char
, ":default-char");
9736 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9737 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9738 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9739 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9740 DEFSYM (QCascii_compatible_p
, ":ascii-compatible-p");
9742 Vcoding_category_table
9743 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9744 staticpro (&Vcoding_category_table
);
9745 /* Followings are target of code detection. */
9746 ASET (Vcoding_category_table
, coding_category_iso_7
,
9747 intern ("coding-category-iso-7"));
9748 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9749 intern ("coding-category-iso-7-tight"));
9750 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9751 intern ("coding-category-iso-8-1"));
9752 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9753 intern ("coding-category-iso-8-2"));
9754 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9755 intern ("coding-category-iso-7-else"));
9756 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9757 intern ("coding-category-iso-8-else"));
9758 ASET (Vcoding_category_table
, coding_category_utf_8
,
9759 intern ("coding-category-utf-8"));
9760 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9761 intern ("coding-category-utf-16-be"));
9762 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9763 intern ("coding-category-utf-16-auto"));
9764 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9765 intern ("coding-category-utf-16-le"));
9766 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9767 intern ("coding-category-utf-16-be-nosig"));
9768 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9769 intern ("coding-category-utf-16-le-nosig"));
9770 ASET (Vcoding_category_table
, coding_category_charset
,
9771 intern ("coding-category-charset"));
9772 ASET (Vcoding_category_table
, coding_category_sjis
,
9773 intern ("coding-category-sjis"));
9774 ASET (Vcoding_category_table
, coding_category_big5
,
9775 intern ("coding-category-big5"));
9776 ASET (Vcoding_category_table
, coding_category_ccl
,
9777 intern ("coding-category-ccl"));
9778 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9779 intern ("coding-category-emacs-mule"));
9780 /* Followings are NOT target of code detection. */
9781 ASET (Vcoding_category_table
, coding_category_raw_text
,
9782 intern ("coding-category-raw-text"));
9783 ASET (Vcoding_category_table
, coding_category_undecided
,
9784 intern ("coding-category-undecided"));
9786 DEFSYM (Qinsufficient_source
, "insufficient-source");
9787 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9788 DEFSYM (Qinvalid_source
, "invalid-source");
9789 DEFSYM (Qinterrupted
, "interrupted");
9790 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9791 DEFSYM (Qcoding_system_define_form
, "coding-system-define-form");
9793 defsubr (&Scoding_system_p
);
9794 defsubr (&Sread_coding_system
);
9795 defsubr (&Sread_non_nil_coding_system
);
9796 defsubr (&Scheck_coding_system
);
9797 defsubr (&Sdetect_coding_region
);
9798 defsubr (&Sdetect_coding_string
);
9799 defsubr (&Sfind_coding_systems_region_internal
);
9800 defsubr (&Sunencodable_char_position
);
9801 defsubr (&Scheck_coding_systems_region
);
9802 defsubr (&Sdecode_coding_region
);
9803 defsubr (&Sencode_coding_region
);
9804 defsubr (&Sdecode_coding_string
);
9805 defsubr (&Sencode_coding_string
);
9806 defsubr (&Sdecode_sjis_char
);
9807 defsubr (&Sencode_sjis_char
);
9808 defsubr (&Sdecode_big5_char
);
9809 defsubr (&Sencode_big5_char
);
9810 defsubr (&Sset_terminal_coding_system_internal
);
9811 defsubr (&Sset_safe_terminal_coding_system_internal
);
9812 defsubr (&Sterminal_coding_system
);
9813 defsubr (&Sset_keyboard_coding_system_internal
);
9814 defsubr (&Skeyboard_coding_system
);
9815 defsubr (&Sfind_operation_coding_system
);
9816 defsubr (&Sset_coding_system_priority
);
9817 defsubr (&Sdefine_coding_system_internal
);
9818 defsubr (&Sdefine_coding_system_alias
);
9819 defsubr (&Scoding_system_put
);
9820 defsubr (&Scoding_system_base
);
9821 defsubr (&Scoding_system_plist
);
9822 defsubr (&Scoding_system_aliases
);
9823 defsubr (&Scoding_system_eol_type
);
9824 defsubr (&Scoding_system_priority_list
);
9826 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9827 doc
: /* List of coding systems.
9829 Do not alter the value of this variable manually. This variable should be
9830 updated by the functions `define-coding-system' and
9831 `define-coding-system-alias'. */);
9832 Vcoding_system_list
= Qnil
;
9834 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9835 doc
: /* Alist of coding system names.
9836 Each element is one element list of coding system name.
9837 This variable is given to `completing-read' as TABLE argument.
9839 Do not alter the value of this variable manually. This variable should be
9840 updated by the functions `make-coding-system' and
9841 `define-coding-system-alias'. */);
9842 Vcoding_system_alist
= Qnil
;
9844 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9845 doc
: /* List of coding-categories (symbols) ordered by priority.
9847 On detecting a coding system, Emacs tries code detection algorithms
9848 associated with each coding-category one by one in this order. When
9849 one algorithm agrees with a byte sequence of source text, the coding
9850 system bound to the corresponding coding-category is selected.
9852 Don't modify this variable directly, but use `set-coding-priority'. */);
9856 Vcoding_category_list
= Qnil
;
9857 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9858 Vcoding_category_list
9859 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9860 Vcoding_category_list
);
9863 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9864 doc
: /* Specify the coding system for read operations.
9865 It is useful to bind this variable with `let', but do not set it globally.
9866 If the value is a coding system, it is used for decoding on read operation.
9867 If not, an appropriate element is used from one of the coding system alists:
9868 There are three such tables, `file-coding-system-alist',
9869 `process-coding-system-alist', and `network-coding-system-alist'. */);
9870 Vcoding_system_for_read
= Qnil
;
9872 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9873 doc
: /* Specify the coding system for write operations.
9874 Programs bind this variable with `let', but you should not set it globally.
9875 If the value is a coding system, it is used for encoding of output,
9876 when writing it to a file and when sending it to a file or subprocess.
9878 If this does not specify a coding system, an appropriate element
9879 is used from one of the coding system alists:
9880 There are three such tables, `file-coding-system-alist',
9881 `process-coding-system-alist', and `network-coding-system-alist'.
9882 For output to files, if the above procedure does not specify a coding system,
9883 the value of `buffer-file-coding-system' is used. */);
9884 Vcoding_system_for_write
= Qnil
;
9886 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9888 Coding system used in the latest file or process I/O. */);
9889 Vlast_coding_system_used
= Qnil
;
9891 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9893 Error status of the last code conversion.
9895 When an error was detected in the last code conversion, this variable
9896 is set to one of the following symbols.
9897 `insufficient-source'
9901 `insufficient-memory'
9902 When no error was detected, the value doesn't change. So, to check
9903 the error status of a code conversion by this variable, you must
9904 explicitly set this variable to nil before performing code
9906 Vlast_code_conversion_error
= Qnil
;
9908 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9910 *Non-nil means always inhibit code conversion of end-of-line format.
9911 See info node `Coding Systems' and info node `Text and Binary' concerning
9912 such conversion. */);
9913 inhibit_eol_conversion
= 0;
9915 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9917 Non-nil means process buffer inherits coding system of process output.
9918 Bind it to t if the process output is to be treated as if it were a file
9919 read from some filesystem. */);
9920 inherit_process_coding_system
= 0;
9922 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9924 Alist to decide a coding system to use for a file I/O operation.
9925 The format is ((PATTERN . VAL) ...),
9926 where PATTERN is a regular expression matching a file name,
9927 VAL is a coding system, a cons of coding systems, or a function symbol.
9928 If VAL is a coding system, it is used for both decoding and encoding
9930 If VAL is a cons of coding systems, the car part is used for decoding,
9931 and the cdr part is used for encoding.
9932 If VAL is a function symbol, the function must return a coding system
9933 or a cons of coding systems which are used as above. The function is
9934 called with an argument that is a list of the arguments with which
9935 `find-operation-coding-system' was called. If the function can't decide
9936 a coding system, it can return `undecided' so that the normal
9937 code-detection is performed.
9939 See also the function `find-operation-coding-system'
9940 and the variable `auto-coding-alist'. */);
9941 Vfile_coding_system_alist
= Qnil
;
9943 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9945 Alist to decide a coding system to use for a process I/O operation.
9946 The format is ((PATTERN . VAL) ...),
9947 where PATTERN is a regular expression matching a program name,
9948 VAL is a coding system, a cons of coding systems, or a function symbol.
9949 If VAL is a coding system, it is used for both decoding what received
9950 from the program and encoding what sent to the program.
9951 If VAL is a cons of coding systems, the car part is used for decoding,
9952 and the cdr part is used for encoding.
9953 If VAL is a function symbol, the function must return a coding system
9954 or a cons of coding systems which are used as above.
9956 See also the function `find-operation-coding-system'. */);
9957 Vprocess_coding_system_alist
= Qnil
;
9959 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9961 Alist to decide a coding system to use for a network I/O operation.
9962 The format is ((PATTERN . VAL) ...),
9963 where PATTERN is a regular expression matching a network service name
9964 or is a port number to connect to,
9965 VAL is a coding system, a cons of coding systems, or a function symbol.
9966 If VAL is a coding system, it is used for both decoding what received
9967 from the network stream and encoding what sent to the network stream.
9968 If VAL is a cons of coding systems, the car part is used for decoding,
9969 and the cdr part is used for encoding.
9970 If VAL is a function symbol, the function must return a coding system
9971 or a cons of coding systems which are used as above.
9973 See also the function `find-operation-coding-system'. */);
9974 Vnetwork_coding_system_alist
= Qnil
;
9976 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9977 doc
: /* Coding system to use with system messages.
9978 Also used for decoding keyboard input on X Window system. */);
9979 Vlocale_coding_system
= Qnil
;
9981 /* The eol mnemonics are reset in startup.el system-dependently. */
9982 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9984 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9985 eol_mnemonic_unix
= build_string (":");
9987 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9989 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9990 eol_mnemonic_dos
= build_string ("\\");
9992 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9994 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9995 eol_mnemonic_mac
= build_string ("/");
9997 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9999 *String displayed in mode line when end-of-line format is not yet determined. */);
10000 eol_mnemonic_undecided
= build_string (":");
10002 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
10004 *Non-nil enables character translation while encoding and decoding. */);
10005 Venable_character_translation
= Qt
;
10007 DEFVAR_LISP ("standard-translation-table-for-decode",
10008 &Vstandard_translation_table_for_decode
,
10009 doc
: /* Table for translating characters while decoding. */);
10010 Vstandard_translation_table_for_decode
= Qnil
;
10012 DEFVAR_LISP ("standard-translation-table-for-encode",
10013 &Vstandard_translation_table_for_encode
,
10014 doc
: /* Table for translating characters while encoding. */);
10015 Vstandard_translation_table_for_encode
= Qnil
;
10017 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
10018 doc
: /* Alist of charsets vs revision numbers.
10019 While encoding, if a charset (car part of an element) is found,
10020 designate it with the escape sequence identifying revision (cdr part
10021 of the element). */);
10022 Vcharset_revision_table
= Qnil
;
10024 DEFVAR_LISP ("default-process-coding-system",
10025 &Vdefault_process_coding_system
,
10026 doc
: /* Cons of coding systems used for process I/O by default.
10027 The car part is used for decoding a process output,
10028 the cdr part is used for encoding a text to be sent to a process. */);
10029 Vdefault_process_coding_system
= Qnil
;
10031 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
10033 Table of extra Latin codes in the range 128..159 (inclusive).
10034 This is a vector of length 256.
10035 If Nth element is non-nil, the existence of code N in a file
10036 \(or output of subprocess) doesn't prevent it to be detected as
10037 a coding system of ISO 2022 variant which has a flag
10038 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
10039 or reading output of a subprocess.
10040 Only 128th through 159th elements has a meaning. */);
10041 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
10043 DEFVAR_LISP ("select-safe-coding-system-function",
10044 &Vselect_safe_coding_system_function
,
10046 Function to call to select safe coding system for encoding a text.
10048 If set, this function is called to force a user to select a proper
10049 coding system which can encode the text in the case that a default
10050 coding system used in each operation can't encode the text. The
10051 function should take care that the buffer is not modified while
10052 the coding system is being selected.
10054 The default value is `select-safe-coding-system' (which see). */);
10055 Vselect_safe_coding_system_function
= Qnil
;
10057 DEFVAR_BOOL ("coding-system-require-warning",
10058 &coding_system_require_warning
,
10059 doc
: /* Internal use only.
10060 If non-nil, on writing a file, `select-safe-coding-system-function' is
10061 called even if `coding-system-for-write' is non-nil. The command
10062 `universal-coding-system-argument' binds this variable to t temporarily. */);
10063 coding_system_require_warning
= 0;
10066 DEFVAR_BOOL ("inhibit-iso-escape-detection",
10067 &inhibit_iso_escape_detection
,
10069 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
10071 By default, on reading a file, Emacs tries to detect how the text is
10072 encoded. This code detection is sensitive to escape sequences. If
10073 the sequence is valid as ISO2022, the code is determined as one of
10074 the ISO2022 encodings, and the file is decoded by the corresponding
10075 coding system (e.g. `iso-2022-7bit').
10077 However, there may be a case that you want to read escape sequences in
10078 a file as is. In such a case, you can set this variable to non-nil.
10079 Then, as the code detection ignores any escape sequences, no file is
10080 detected as encoded in some ISO2022 encoding. The result is that all
10081 escape sequences become visible in a buffer.
10083 The default value is nil, and it is strongly recommended not to change
10084 it. That is because many Emacs Lisp source files that contain
10085 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
10086 in Emacs's distribution, and they won't be decoded correctly on
10087 reading if you suppress escape sequence detection.
10089 The other way to read escape sequences in a file without decoding is
10090 to explicitly specify some coding system that doesn't use ISO2022's
10091 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
10092 inhibit_iso_escape_detection
= 0;
10094 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
10095 doc
: /* Char table for translating self-inserting characters.
10096 This is applied to the result of input methods, not their input. See also
10097 `keyboard-translate-table'. */);
10098 Vtranslation_table_for_input
= Qnil
;
10101 Lisp_Object args
[coding_arg_max
];
10102 Lisp_Object plist
[16];
10105 for (i
= 0; i
< coding_arg_max
; i
++)
10108 plist
[0] = intern (":name");
10109 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
10110 plist
[2] = intern (":mnemonic");
10111 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
10112 plist
[4] = intern (":coding-type");
10113 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
10114 plist
[6] = intern (":ascii-compatible-p");
10115 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
10116 plist
[8] = intern (":default-char");
10117 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
10118 plist
[10] = intern (":for-unibyte");
10119 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
10120 plist
[12] = intern (":docstring");
10121 plist
[13] = build_string ("Do no conversion.\n\
10123 When you visit a file with this coding, the file is read into a\n\
10124 unibyte buffer as is, thus each byte of a file is treated as a\n\
10126 plist
[14] = intern (":eol-type");
10127 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
10128 args
[coding_arg_plist
] = Flist (16, plist
);
10129 Fdefine_coding_system_internal (coding_arg_max
, args
);
10131 plist
[1] = args
[coding_arg_name
] = Qundecided
;
10132 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('-');
10133 plist
[5] = args
[coding_arg_coding_type
] = Qundecided
;
10134 /* This is already set.
10135 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
10136 plist
[8] = intern (":charset-list");
10137 plist
[9] = args
[coding_arg_charset_list
] = Fcons (Qascii
, Qnil
);
10138 plist
[11] = args
[coding_arg_for_unibyte
] = Qnil
;
10139 plist
[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
10140 plist
[15] = args
[coding_arg_eol_type
] = Qnil
;
10141 args
[coding_arg_plist
] = Flist (16, plist
);
10142 Fdefine_coding_system_internal (coding_arg_max
, args
);
10145 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
10150 for (i
= 0; i
< coding_category_max
; i
++)
10151 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
10153 #if defined (MSDOS) || defined (WINDOWSNT)
10154 system_eol_type
= Qdos
;
10156 system_eol_type
= Qunix
;
10158 staticpro (&system_eol_type
);
10162 emacs_strerror (error_number
)
10167 synchronize_system_messages_locale ();
10168 str
= strerror (error_number
);
10170 if (! NILP (Vlocale_coding_system
))
10172 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
10173 Vlocale_coding_system
,
10175 str
= (char *) SDATA (dec
);
10183 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
10184 (do not change this comment) */