1 /* Coding system handler (conversion, detection, etc).
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
9 This file is part of GNU Emacs.
11 GNU Emacs is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 GNU Emacs is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GNU Emacs; see the file COPYING. If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
26 /*** TABLE OF CONTENTS ***
30 2. Emacs' internal format (emacs-utf-8) handlers
33 5. Charset-base coding systems handlers
34 6. emacs-mule (old Emacs' internal format) handlers
36 8. Shift-JIS and BIG5 handlers
38 10. C library functions
39 11. Emacs Lisp library functions
44 /*** 0. General comments ***
49 A coding system is an object for an encoding mechanism that contains
50 information about how to convert byte sequences to character
51 sequences and vice versa. When we say "decode", it means converting
52 a byte sequence of a specific coding system into a character
53 sequence that is represented by Emacs' internal coding system
54 `emacs-utf-8', and when we say "encode", it means converting a
55 character sequence of emacs-utf-8 to a byte sequence of a specific
58 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
59 C level, a coding system is represented by a vector of attributes
60 stored in the hash table Vcharset_hash_table. The conversion from
61 coding system symbol to attributes vector is done by looking up
62 Vcharset_hash_table by the symbol.
64 Coding systems are classified into the following types depending on
65 the encoding mechanism. Here's a brief description of the types.
71 o Charset-base coding system
73 A coding system defined by one or more (coded) character sets.
74 Decoding and encoding are done by a code converter defined for each
77 o Old Emacs internal format (emacs-mule)
79 The coding system adopted by old versions of Emacs (20 and 21).
81 o ISO2022-base coding system
83 The most famous coding system for multiple character sets. X's
84 Compound Text, various EUCs (Extended Unix Code), and coding systems
85 used in the Internet communication such as ISO-2022-JP are all
88 o SJIS (or Shift-JIS or MS-Kanji-Code)
90 A coding system to encode character sets: ASCII, JISX0201, and
91 JISX0208. Widely used for PC's in Japan. Details are described in
96 A coding system to encode character sets: ASCII and Big5. Widely
97 used for Chinese (mainly in Taiwan and Hong Kong). Details are
98 described in section 8. In this file, when we write "big5" (all
99 lowercase), we mean the coding system, and when we write "Big5"
100 (capitalized), we mean the character set.
104 If a user wants to decode/encode text encoded in a coding system
105 not listed above, he can supply a decoder and an encoder for it in
106 CCL (Code Conversion Language) programs. Emacs executes the CCL
107 program while decoding/encoding.
111 A coding system for text containing raw eight-bit data. Emacs
112 treats each byte of source text as a character (except for
113 end-of-line conversion).
117 Like raw text, but don't do end-of-line conversion.
122 How text end-of-line is encoded depends on operating system. For
123 instance, Unix's format is just one byte of LF (line-feed) code,
124 whereas DOS's format is two-byte sequence of `carriage-return' and
125 `line-feed' codes. MacOS's format is usually one byte of
128 Since text character encoding and end-of-line encoding are
129 independent, any coding system described above can take any format
130 of end-of-line (except for no-conversion).
134 Before using a coding system for code conversion (i.e. decoding and
135 encoding), we setup a structure of type `struct coding_system'.
136 This structure keeps various information about a specific code
137 conversion (e.g. the location of source and destination data).
144 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
146 These functions check if a byte sequence specified as a source in
147 CODING conforms to the format of XXX, and update the members of
150 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
152 Below is the template of these functions. */
156 detect_coding_XXX (coding
, detect_info
)
157 struct coding_system
*coding
;
158 struct coding_detection_info
*detect_info
;
160 unsigned char *src
= coding
->source
;
161 unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
162 int multibytep
= coding
->src_multibyte
;
163 int consumed_chars
= 0;
169 /* Get one byte from the source. If the souce is exausted, jump
170 to no_more_source:. */
173 if (! __C_conforms_to_XXX___ (c
))
175 if (! __C_strongly_suggests_XXX__ (c
))
176 found
= CATEGORY_MASK_XXX
;
178 /* The byte sequence is invalid for XXX. */
179 detect_info
->rejected
|= CATEGORY_MASK_XXX
;
183 /* The source exausted successfully. */
184 detect_info
->found
|= found
;
189 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
191 These functions decode a byte sequence specified as a source by
192 CODING. The resulting multibyte text goes to a place pointed to by
193 CODING->charbuf, the length of which should not exceed
194 CODING->charbuf_size;
196 These functions set the information of original and decoded texts in
197 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
198 They also set CODING->result to one of CODING_RESULT_XXX indicating
199 how the decoding is finished.
201 Below is the template of these functions. */
205 decode_coding_XXXX (coding
)
206 struct coding_system
*coding
;
208 unsigned char *src
= coding
->source
+ coding
->consumed
;
209 unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
210 /* SRC_BASE remembers the start position in source in each loop.
211 The loop will be exited when there's not enough source code, or
212 when there's no room in CHARBUF for a decoded character. */
213 unsigned char *src_base
;
214 /* A buffer to produce decoded characters. */
215 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
216 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
217 int multibytep
= coding
->src_multibyte
;
222 if (charbuf
< charbuf_end
)
223 /* No more room to produce a decoded character. */
230 if (src_base
< src_end
231 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
232 /* If the source ends by partial bytes to construct a character,
233 treat them as eight-bit raw data. */
234 while (src_base
< src_end
&& charbuf
< charbuf_end
)
235 *charbuf
++ = *src_base
++;
236 /* Remember how many bytes and characters we consumed. If the
237 source is multibyte, the bytes and chars are not identical. */
238 coding
->consumed
= coding
->consumed_char
= src_base
- coding
->source
;
239 /* Remember how many characters we produced. */
240 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
244 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
246 These functions encode SRC_BYTES length text at SOURCE of Emacs'
247 internal multibyte format by CODING. The resulting byte sequence
248 goes to a place pointed to by DESTINATION, the length of which
249 should not exceed DST_BYTES.
251 These functions set the information of original and encoded texts in
252 the members produced, produced_char, consumed, and consumed_char of
253 the structure *CODING. They also set the member result to one of
254 CODING_RESULT_XXX indicating how the encoding finished.
256 DST_BYTES zero means that source area and destination area are
257 overlapped, which means that we can produce a encoded text until it
258 reaches at the head of not-yet-encoded source text.
260 Below is a template of these functions. */
263 encode_coding_XXX (coding
)
264 struct coding_system
*coding
;
266 int multibytep
= coding
->dst_multibyte
;
267 int *charbuf
= coding
->charbuf
;
268 int *charbuf_end
= charbuf
->charbuf
+ coding
->charbuf_used
;
269 unsigned char *dst
= coding
->destination
+ coding
->produced
;
270 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
271 unsigned char *adjusted_dst_end
= dst_end
- _MAX_BYTES_PRODUCED_IN_LOOP_
;
272 int produced_chars
= 0;
274 for (; charbuf
< charbuf_end
&& dst
< adjusted_dst_end
; charbuf
++)
277 /* Encode C into DST, and increment DST. */
279 label_no_more_destination
:
280 /* How many chars and bytes we produced. */
281 coding
->produced_char
+= produced_chars
;
282 coding
->produced
= dst
- coding
->destination
;
287 /*** 1. Preamble ***/
294 #include "character.h"
297 #include "composite.h"
301 Lisp_Object Vcoding_system_hash_table
;
303 Lisp_Object Qcoding_system
, Qcoding_aliases
, Qeol_type
;
304 Lisp_Object Qunix
, Qdos
;
305 extern Lisp_Object Qmac
; /* frame.c */
306 Lisp_Object Qbuffer_file_coding_system
;
307 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
308 Lisp_Object Qdefault_char
;
309 Lisp_Object Qno_conversion
, Qundecided
;
310 Lisp_Object Qcharset
, Qiso_2022
, Qutf_8
, Qutf_16
, Qshift_jis
, Qbig5
;
311 Lisp_Object Qbig
, Qlittle
;
312 Lisp_Object Qcoding_system_history
;
313 Lisp_Object Qvalid_codes
;
314 Lisp_Object QCcategory
, QCmnemonic
, QCdefalut_char
;
315 Lisp_Object QCdecode_translation_table
, QCencode_translation_table
;
316 Lisp_Object QCpost_read_conversion
, QCpre_write_conversion
;
318 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
319 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
320 Lisp_Object Qstart_process
, Qopen_network_stream
;
321 Lisp_Object Qtarget_idx
;
323 Lisp_Object Qinsufficient_source
, Qinconsistent_eol
, Qinvalid_source
;
324 Lisp_Object Qinterrupted
, Qinsufficient_memory
;
326 int coding_system_require_warning
;
328 Lisp_Object Vselect_safe_coding_system_function
;
330 /* Mnemonic string for each format of end-of-line. */
331 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
332 /* Mnemonic string to indicate format of end-of-line is not yet
334 Lisp_Object eol_mnemonic_undecided
;
338 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
340 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
342 /* Coding system emacs-mule and raw-text are for converting only
343 end-of-line format. */
344 Lisp_Object Qemacs_mule
, Qraw_text
;
345 Lisp_Object Qutf_8_emacs
;
347 /* Coding-systems are handed between Emacs Lisp programs and C internal
348 routines by the following three variables. */
349 /* Coding-system for reading files and receiving data from process. */
350 Lisp_Object Vcoding_system_for_read
;
351 /* Coding-system for writing files and sending data to process. */
352 Lisp_Object Vcoding_system_for_write
;
353 /* Coding-system actually used in the latest I/O. */
354 Lisp_Object Vlast_coding_system_used
;
355 /* Set to non-nil when an error is detected while code conversion. */
356 Lisp_Object Vlast_code_conversion_error
;
357 /* A vector of length 256 which contains information about special
358 Latin codes (especially for dealing with Microsoft codes). */
359 Lisp_Object Vlatin_extra_code_table
;
361 /* Flag to inhibit code conversion of end-of-line format. */
362 int inhibit_eol_conversion
;
364 /* Flag to inhibit ISO2022 escape sequence detection. */
365 int inhibit_iso_escape_detection
;
367 /* Flag to make buffer-file-coding-system inherit from process-coding. */
368 int inherit_process_coding_system
;
370 /* Coding system to be used to encode text for terminal display. */
371 struct coding_system terminal_coding
;
373 /* Coding system to be used to encode text for terminal display when
374 terminal coding system is nil. */
375 struct coding_system safe_terminal_coding
;
377 /* Coding system of what is sent from terminal keyboard. */
378 struct coding_system keyboard_coding
;
380 Lisp_Object Vfile_coding_system_alist
;
381 Lisp_Object Vprocess_coding_system_alist
;
382 Lisp_Object Vnetwork_coding_system_alist
;
384 Lisp_Object Vlocale_coding_system
;
388 /* Flag to tell if we look up translation table on character code
390 Lisp_Object Venable_character_translation
;
391 /* Standard translation table to look up on decoding (reading). */
392 Lisp_Object Vstandard_translation_table_for_decode
;
393 /* Standard translation table to look up on encoding (writing). */
394 Lisp_Object Vstandard_translation_table_for_encode
;
396 Lisp_Object Qtranslation_table
;
397 Lisp_Object Qtranslation_table_id
;
398 Lisp_Object Qtranslation_table_for_decode
;
399 Lisp_Object Qtranslation_table_for_encode
;
401 /* Alist of charsets vs revision number. */
402 static Lisp_Object Vcharset_revision_table
;
404 /* Default coding systems used for process I/O. */
405 Lisp_Object Vdefault_process_coding_system
;
407 /* Char table for translating Quail and self-inserting input. */
408 Lisp_Object Vtranslation_table_for_input
;
410 /* Two special coding systems. */
411 Lisp_Object Vsjis_coding_system
;
412 Lisp_Object Vbig5_coding_system
;
414 static void record_conversion_result (struct coding_system
*coding
,
415 enum coding_result_code result
);
416 static int detect_coding_utf_8
P_ ((struct coding_system
*,
417 struct coding_detection_info
*info
));
418 static void decode_coding_utf_8
P_ ((struct coding_system
*));
419 static int encode_coding_utf_8
P_ ((struct coding_system
*));
421 static int detect_coding_utf_16
P_ ((struct coding_system
*,
422 struct coding_detection_info
*info
));
423 static void decode_coding_utf_16
P_ ((struct coding_system
*));
424 static int encode_coding_utf_16
P_ ((struct coding_system
*));
426 static int detect_coding_iso_2022
P_ ((struct coding_system
*,
427 struct coding_detection_info
*info
));
428 static void decode_coding_iso_2022
P_ ((struct coding_system
*));
429 static int encode_coding_iso_2022
P_ ((struct coding_system
*));
431 static int detect_coding_emacs_mule
P_ ((struct coding_system
*,
432 struct coding_detection_info
*info
));
433 static void decode_coding_emacs_mule
P_ ((struct coding_system
*));
434 static int encode_coding_emacs_mule
P_ ((struct coding_system
*));
436 static int detect_coding_sjis
P_ ((struct coding_system
*,
437 struct coding_detection_info
*info
));
438 static void decode_coding_sjis
P_ ((struct coding_system
*));
439 static int encode_coding_sjis
P_ ((struct coding_system
*));
441 static int detect_coding_big5
P_ ((struct coding_system
*,
442 struct coding_detection_info
*info
));
443 static void decode_coding_big5
P_ ((struct coding_system
*));
444 static int encode_coding_big5
P_ ((struct coding_system
*));
446 static int detect_coding_ccl
P_ ((struct coding_system
*,
447 struct coding_detection_info
*info
));
448 static void decode_coding_ccl
P_ ((struct coding_system
*));
449 static int encode_coding_ccl
P_ ((struct coding_system
*));
451 static void decode_coding_raw_text
P_ ((struct coding_system
*));
452 static int encode_coding_raw_text
P_ ((struct coding_system
*));
455 /* ISO2022 section */
457 #define CODING_ISO_INITIAL(coding, reg) \
458 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
459 coding_attr_iso_initial), \
463 #define CODING_ISO_REQUEST(coding, charset_id) \
464 ((charset_id <= (coding)->max_charset_id \
465 ? (coding)->safe_charsets[charset_id] \
469 #define CODING_ISO_FLAGS(coding) \
470 ((coding)->spec.iso_2022.flags)
471 #define CODING_ISO_DESIGNATION(coding, reg) \
472 ((coding)->spec.iso_2022.current_designation[reg])
473 #define CODING_ISO_INVOCATION(coding, plane) \
474 ((coding)->spec.iso_2022.current_invocation[plane])
475 #define CODING_ISO_SINGLE_SHIFTING(coding) \
476 ((coding)->spec.iso_2022.single_shifting)
477 #define CODING_ISO_BOL(coding) \
478 ((coding)->spec.iso_2022.bol)
479 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
480 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
482 /* Control characters of ISO2022. */
483 /* code */ /* function */
484 #define ISO_CODE_LF 0x0A /* line-feed */
485 #define ISO_CODE_CR 0x0D /* carriage-return */
486 #define ISO_CODE_SO 0x0E /* shift-out */
487 #define ISO_CODE_SI 0x0F /* shift-in */
488 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
489 #define ISO_CODE_ESC 0x1B /* escape */
490 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
491 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
492 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
494 /* All code (1-byte) of ISO2022 is classified into one of the
496 enum iso_code_class_type
498 ISO_control_0
, /* Control codes in the range
499 0x00..0x1F and 0x7F, except for the
500 following 5 codes. */
501 ISO_shift_out
, /* ISO_CODE_SO (0x0E) */
502 ISO_shift_in
, /* ISO_CODE_SI (0x0F) */
503 ISO_single_shift_2_7
, /* ISO_CODE_SS2_7 (0x19) */
504 ISO_escape
, /* ISO_CODE_SO (0x1B) */
505 ISO_control_1
, /* Control codes in the range
506 0x80..0x9F, except for the
507 following 3 codes. */
508 ISO_single_shift_2
, /* ISO_CODE_SS2 (0x8E) */
509 ISO_single_shift_3
, /* ISO_CODE_SS3 (0x8F) */
510 ISO_control_sequence_introducer
, /* ISO_CODE_CSI (0x9B) */
511 ISO_0x20_or_0x7F
, /* Codes of the values 0x20 or 0x7F. */
512 ISO_graphic_plane_0
, /* Graphic codes in the range 0x21..0x7E. */
513 ISO_0xA0_or_0xFF
, /* Codes of the values 0xA0 or 0xFF. */
514 ISO_graphic_plane_1
/* Graphic codes in the range 0xA1..0xFE. */
517 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
518 `iso-flags' attribute of an iso2022 coding system. */
520 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
521 instead of the correct short-form sequence (e.g. ESC $ A). */
522 #define CODING_ISO_FLAG_LONG_FORM 0x0001
524 /* If set, reset graphic planes and registers at end-of-line to the
526 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
528 /* If set, reset graphic planes and registers before any control
529 characters to the initial state. */
530 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
532 /* If set, encode by 7-bit environment. */
533 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
535 /* If set, use locking-shift function. */
536 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
538 /* If set, use single-shift function. Overwrite
539 CODING_ISO_FLAG_LOCKING_SHIFT. */
540 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
542 /* If set, use designation escape sequence. */
543 #define CODING_ISO_FLAG_DESIGNATION 0x0040
545 /* If set, produce revision number sequence. */
546 #define CODING_ISO_FLAG_REVISION 0x0080
548 /* If set, produce ISO6429's direction specifying sequence. */
549 #define CODING_ISO_FLAG_DIRECTION 0x0100
551 /* If set, assume designation states are reset at beginning of line on
553 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
555 /* If set, designation sequence should be placed at beginning of line
557 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
559 /* If set, do not encode unsafe charactes on output. */
560 #define CODING_ISO_FLAG_SAFE 0x0800
562 /* If set, extra latin codes (128..159) are accepted as a valid code
564 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
566 #define CODING_ISO_FLAG_COMPOSITION 0x2000
568 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
570 #define CODING_ISO_FLAG_USE_ROMAN 0x8000
572 #define CODING_ISO_FLAG_USE_OLDJIS 0x10000
574 #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
576 /* A character to be produced on output if encoding of the original
577 character is prohibited by CODING_ISO_FLAG_SAFE. */
578 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
582 #define CODING_UTF_16_BOM(coding) \
583 ((coding)->spec.utf_16.bom)
585 #define CODING_UTF_16_ENDIAN(coding) \
586 ((coding)->spec.utf_16.endian)
588 #define CODING_UTF_16_SURROGATE(coding) \
589 ((coding)->spec.utf_16.surrogate)
593 #define CODING_CCL_DECODER(coding) \
594 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
595 #define CODING_CCL_ENCODER(coding) \
596 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
597 #define CODING_CCL_VALIDS(coding) \
598 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
600 /* Index for each coding category in `coding_categories' */
604 coding_category_iso_7
,
605 coding_category_iso_7_tight
,
606 coding_category_iso_8_1
,
607 coding_category_iso_8_2
,
608 coding_category_iso_7_else
,
609 coding_category_iso_8_else
,
610 coding_category_utf_8
,
611 coding_category_utf_16_auto
,
612 coding_category_utf_16_be
,
613 coding_category_utf_16_le
,
614 coding_category_utf_16_be_nosig
,
615 coding_category_utf_16_le_nosig
,
616 coding_category_charset
,
617 coding_category_sjis
,
618 coding_category_big5
,
620 coding_category_emacs_mule
,
621 /* All above are targets of code detection. */
622 coding_category_raw_text
,
623 coding_category_undecided
,
627 /* Definitions of flag bits used in detect_coding_XXXX. */
628 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
629 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
630 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
631 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
632 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
633 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
634 #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
635 #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
636 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
637 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
638 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
639 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
640 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
641 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
642 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
643 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
644 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
645 #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
647 /* This value is returned if detect_coding_mask () find nothing other
648 than ASCII characters. */
649 #define CATEGORY_MASK_ANY \
650 (CATEGORY_MASK_ISO_7 \
651 | CATEGORY_MASK_ISO_7_TIGHT \
652 | CATEGORY_MASK_ISO_8_1 \
653 | CATEGORY_MASK_ISO_8_2 \
654 | CATEGORY_MASK_ISO_7_ELSE \
655 | CATEGORY_MASK_ISO_8_ELSE \
656 | CATEGORY_MASK_UTF_8 \
657 | CATEGORY_MASK_UTF_16_BE \
658 | CATEGORY_MASK_UTF_16_LE \
659 | CATEGORY_MASK_UTF_16_BE_NOSIG \
660 | CATEGORY_MASK_UTF_16_LE_NOSIG \
661 | CATEGORY_MASK_CHARSET \
662 | CATEGORY_MASK_SJIS \
663 | CATEGORY_MASK_BIG5 \
664 | CATEGORY_MASK_CCL \
665 | CATEGORY_MASK_EMACS_MULE)
668 #define CATEGORY_MASK_ISO_7BIT \
669 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
671 #define CATEGORY_MASK_ISO_8BIT \
672 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
674 #define CATEGORY_MASK_ISO_ELSE \
675 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
677 #define CATEGORY_MASK_ISO_ESCAPE \
678 (CATEGORY_MASK_ISO_7 \
679 | CATEGORY_MASK_ISO_7_TIGHT \
680 | CATEGORY_MASK_ISO_7_ELSE \
681 | CATEGORY_MASK_ISO_8_ELSE)
683 #define CATEGORY_MASK_ISO \
684 ( CATEGORY_MASK_ISO_7BIT \
685 | CATEGORY_MASK_ISO_8BIT \
686 | CATEGORY_MASK_ISO_ELSE)
688 #define CATEGORY_MASK_UTF_16 \
689 (CATEGORY_MASK_UTF_16_BE \
690 | CATEGORY_MASK_UTF_16_LE \
691 | CATEGORY_MASK_UTF_16_BE_NOSIG \
692 | CATEGORY_MASK_UTF_16_LE_NOSIG)
695 /* List of symbols `coding-category-xxx' ordered by priority. This
696 variable is exposed to Emacs Lisp. */
697 static Lisp_Object Vcoding_category_list
;
699 /* Table of coding categories (Lisp symbols). This variable is for
701 static Lisp_Object Vcoding_category_table
;
703 /* Table of coding-categories ordered by priority. */
704 static enum coding_category coding_priorities
[coding_category_max
];
706 /* Nth element is a coding context for the coding system bound to the
707 Nth coding category. */
708 static struct coding_system coding_categories
[coding_category_max
];
710 /*** Commonly used macros and functions ***/
713 #define min(a, b) ((a) < (b) ? (a) : (b))
716 #define max(a, b) ((a) > (b) ? (a) : (b))
719 #define CODING_GET_INFO(coding, attrs, charset_list) \
721 (attrs) = CODING_ID_ATTRS ((coding)->id); \
722 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
726 /* Safely get one byte from the source text pointed by SRC which ends
727 at SRC_END, and set C to that byte. If there are not enough bytes
728 in the source, it jumps to `no_more_source'. If multibytep is
729 nonzero, and a multibyte character is found at SRC, set C to the
730 negative value of the character code. The caller should declare
731 and set these variables appropriately in advance:
732 src, src_end, multibytep */
734 #define ONE_MORE_BYTE(c) \
736 if (src == src_end) \
738 if (src_base < src) \
739 record_conversion_result \
740 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
741 goto no_more_source; \
744 if (multibytep && (c & 0x80)) \
746 if ((c & 0xFE) == 0xC0) \
747 c = ((c & 1) << 6) | *src++; \
750 c = - string_char (--src, &src, NULL); \
751 record_conversion_result \
752 (coding, CODING_RESULT_INVALID_SRC); \
759 #define ONE_MORE_BYTE_NO_CHECK(c) \
762 if (multibytep && (c & 0x80)) \
764 if ((c & 0xFE) == 0xC0) \
765 c = ((c & 1) << 6) | *src++; \
768 c = - string_char (--src, &src, NULL); \
769 record_conversion_result \
770 (coding, CODING_RESULT_INVALID_SRC); \
777 /* Store a byte C in the place pointed by DST and increment DST to the
778 next free point, and increment PRODUCED_CHARS. The caller should
779 assure that C is 0..127, and declare and set the variable `dst'
780 appropriately in advance.
784 #define EMIT_ONE_ASCII_BYTE(c) \
791 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
793 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
795 produced_chars += 2; \
796 *dst++ = (c1), *dst++ = (c2); \
800 /* Store a byte C in the place pointed by DST and increment DST to the
801 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
802 nonzero, store in an appropriate multibyte from. The caller should
803 declare and set the variables `dst' and `multibytep' appropriately
806 #define EMIT_ONE_BYTE(c) \
813 ch = BYTE8_TO_CHAR (ch); \
814 CHAR_STRING_ADVANCE (ch, dst); \
821 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
823 #define EMIT_TWO_BYTES(c1, c2) \
825 produced_chars += 2; \
832 ch = BYTE8_TO_CHAR (ch); \
833 CHAR_STRING_ADVANCE (ch, dst); \
836 ch = BYTE8_TO_CHAR (ch); \
837 CHAR_STRING_ADVANCE (ch, dst); \
847 #define EMIT_THREE_BYTES(c1, c2, c3) \
849 EMIT_ONE_BYTE (c1); \
850 EMIT_TWO_BYTES (c2, c3); \
854 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
856 EMIT_TWO_BYTES (c1, c2); \
857 EMIT_TWO_BYTES (c3, c4); \
862 record_conversion_result (struct coding_system
*coding
,
863 enum coding_result_code result
)
865 coding
->result
= result
;
868 case CODING_RESULT_INSUFFICIENT_SRC
:
869 Vlast_code_conversion_error
= Qinsufficient_source
;
871 case CODING_RESULT_INCONSISTENT_EOL
:
872 Vlast_code_conversion_error
= Qinconsistent_eol
;
874 case CODING_RESULT_INVALID_SRC
:
875 Vlast_code_conversion_error
= Qinvalid_source
;
877 case CODING_RESULT_INTERRUPT
:
878 Vlast_code_conversion_error
= Qinterrupted
;
880 case CODING_RESULT_INSUFFICIENT_MEM
:
881 Vlast_code_conversion_error
= Qinsufficient_memory
;
886 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
888 charset_map_loaded = 0; \
889 c = DECODE_CHAR (charset, code); \
890 if (charset_map_loaded) \
892 const unsigned char *orig = coding->source; \
895 coding_set_source (coding); \
896 offset = coding->source - orig; \
898 src_base += offset; \
904 #define ASSURE_DESTINATION(bytes) \
906 if (dst + (bytes) >= dst_end) \
908 int more_bytes = charbuf_end - charbuf + (bytes); \
910 dst = alloc_destination (coding, more_bytes, dst); \
911 dst_end = coding->destination + coding->dst_bytes; \
918 coding_set_source (coding
)
919 struct coding_system
*coding
;
921 if (BUFFERP (coding
->src_object
))
923 struct buffer
*buf
= XBUFFER (coding
->src_object
);
925 if (coding
->src_pos
< 0)
926 coding
->source
= BUF_GAP_END_ADDR (buf
) + coding
->src_pos_byte
;
928 coding
->source
= BUF_BYTE_ADDRESS (buf
, coding
->src_pos_byte
);
930 else if (STRINGP (coding
->src_object
))
932 coding
->source
= SDATA (coding
->src_object
) + coding
->src_pos_byte
;
935 /* Otherwise, the source is C string and is never relocated
936 automatically. Thus we don't have to update anything. */
941 coding_set_destination (coding
)
942 struct coding_system
*coding
;
944 if (BUFFERP (coding
->dst_object
))
946 if (coding
->src_pos
< 0)
948 coding
->destination
= BEG_ADDR
+ coding
->dst_pos_byte
- 1;
949 coding
->dst_bytes
= (GAP_END_ADDR
950 - (coding
->src_bytes
- coding
->consumed
)
951 - coding
->destination
);
955 /* We are sure that coding->dst_pos_byte is before the gap
957 coding
->destination
= (BUF_BEG_ADDR (XBUFFER (coding
->dst_object
))
958 + coding
->dst_pos_byte
- 1);
959 coding
->dst_bytes
= (BUF_GAP_END_ADDR (XBUFFER (coding
->dst_object
))
960 - coding
->destination
);
964 /* Otherwise, the destination is C string and is never relocated
965 automatically. Thus we don't have to update anything. */
971 coding_alloc_by_realloc (coding
, bytes
)
972 struct coding_system
*coding
;
975 coding
->destination
= (unsigned char *) xrealloc (coding
->destination
,
976 coding
->dst_bytes
+ bytes
);
977 coding
->dst_bytes
+= bytes
;
981 coding_alloc_by_making_gap (coding
, bytes
)
982 struct coding_system
*coding
;
985 if (BUFFERP (coding
->dst_object
)
986 && EQ (coding
->src_object
, coding
->dst_object
))
988 EMACS_INT add
= coding
->src_bytes
- coding
->consumed
;
990 GAP_SIZE
-= add
; ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
992 GAP_SIZE
+= add
; ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
996 Lisp_Object this_buffer
;
998 this_buffer
= Fcurrent_buffer ();
999 set_buffer_internal (XBUFFER (coding
->dst_object
));
1001 set_buffer_internal (XBUFFER (this_buffer
));
1006 static unsigned char *
1007 alloc_destination (coding
, nbytes
, dst
)
1008 struct coding_system
*coding
;
1012 EMACS_INT offset
= dst
- coding
->destination
;
1014 if (BUFFERP (coding
->dst_object
))
1015 coding_alloc_by_making_gap (coding
, nbytes
);
1017 coding_alloc_by_realloc (coding
, nbytes
);
1018 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1019 coding_set_destination (coding
);
1020 dst
= coding
->destination
+ offset
;
1024 /** Macros for annotations. */
1026 /* Maximum length of annotation data (sum of annotations for
1027 composition and charset). */
1028 #define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1030 /* An annotation data is stored in the array coding->charbuf in this
1032 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1033 LENGTH is the number of elements in the annotation.
1034 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1035 NCHARS is the number of characters in the text annotated.
1037 The format of the following elements depend on ANNOTATION_MASK.
1039 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1041 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1042 METHOD is one of enum composition_method.
1043 Optionnal COMPOSITION-COMPONENTS are characters and composition
1046 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1049 #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1051 *(buf)++ = -(len); \
1052 *(buf)++ = (mask); \
1053 *(buf)++ = (nchars); \
1054 coding->annotated = 1; \
1057 #define ADD_COMPOSITION_DATA(buf, nchars, method) \
1059 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1064 #define ADD_CHARSET_DATA(buf, nchars, id) \
1066 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1071 /*** 2. Emacs' internal format (emacs-utf-8) ***/
1078 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1079 Check if a text is encoded in UTF-8. If it is, return 1, else
1082 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1083 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1084 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1085 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1086 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1087 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1090 detect_coding_utf_8 (coding
, detect_info
)
1091 struct coding_system
*coding
;
1092 struct coding_detection_info
*detect_info
;
1094 const unsigned char *src
= coding
->source
, *src_base
;
1095 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1096 int multibytep
= coding
->src_multibyte
;
1097 int consumed_chars
= 0;
1100 detect_info
->checked
|= CATEGORY_MASK_UTF_8
;
1101 /* A coding system of this category is always ASCII compatible. */
1102 src
+= coding
->head_ascii
;
1106 int c
, c1
, c2
, c3
, c4
;
1110 if (c
< 0 || UTF_8_1_OCTET_P (c
))
1113 if (c1
< 0 || ! UTF_8_EXTRA_OCTET_P (c1
))
1115 if (UTF_8_2_OCTET_LEADING_P (c
))
1117 found
= CATEGORY_MASK_UTF_8
;
1121 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1123 if (UTF_8_3_OCTET_LEADING_P (c
))
1125 found
= CATEGORY_MASK_UTF_8
;
1129 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1131 if (UTF_8_4_OCTET_LEADING_P (c
))
1133 found
= CATEGORY_MASK_UTF_8
;
1137 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1139 if (UTF_8_5_OCTET_LEADING_P (c
))
1141 found
= CATEGORY_MASK_UTF_8
;
1146 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1150 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1152 detect_info
->rejected
|= CATEGORY_MASK_UTF_8
;
1155 detect_info
->found
|= found
;
1161 decode_coding_utf_8 (coding
)
1162 struct coding_system
*coding
;
1164 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1165 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1166 const unsigned char *src_base
;
1167 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1168 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1169 int consumed_chars
= 0, consumed_chars_base
;
1170 int multibytep
= coding
->src_multibyte
;
1171 Lisp_Object attr
, charset_list
;
1173 CODING_GET_INFO (coding
, attr
, charset_list
);
1177 int c
, c1
, c2
, c3
, c4
, c5
;
1180 consumed_chars_base
= consumed_chars
;
1182 if (charbuf
>= charbuf_end
)
1190 else if (UTF_8_1_OCTET_P(c1
))
1197 if (c2
< 0 || ! UTF_8_EXTRA_OCTET_P (c2
))
1199 if (UTF_8_2_OCTET_LEADING_P (c1
))
1201 c
= ((c1
& 0x1F) << 6) | (c2
& 0x3F);
1202 /* Reject overlong sequences here and below. Encoders
1203 producing them are incorrect, they can be misleading,
1204 and they mess up read/write invariance. */
1211 if (c3
< 0 || ! UTF_8_EXTRA_OCTET_P (c3
))
1213 if (UTF_8_3_OCTET_LEADING_P (c1
))
1215 c
= (((c1
& 0xF) << 12)
1216 | ((c2
& 0x3F) << 6) | (c3
& 0x3F));
1218 || (c
>= 0xd800 && c
< 0xe000)) /* surrogates (invalid) */
1224 if (c4
< 0 || ! UTF_8_EXTRA_OCTET_P (c4
))
1226 if (UTF_8_4_OCTET_LEADING_P (c1
))
1228 c
= (((c1
& 0x7) << 18) | ((c2
& 0x3F) << 12)
1229 | ((c3
& 0x3F) << 6) | (c4
& 0x3F));
1236 if (c5
< 0 || ! UTF_8_EXTRA_OCTET_P (c5
))
1238 if (UTF_8_5_OCTET_LEADING_P (c1
))
1240 c
= (((c1
& 0x3) << 24) | ((c2
& 0x3F) << 18)
1241 | ((c3
& 0x3F) << 12) | ((c4
& 0x3F) << 6)
1243 if ((c
> MAX_CHAR
) || (c
< 0x200000))
1258 consumed_chars
= consumed_chars_base
;
1260 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
1265 coding
->consumed_char
+= consumed_chars_base
;
1266 coding
->consumed
= src_base
- coding
->source
;
1267 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1272 encode_coding_utf_8 (coding
)
1273 struct coding_system
*coding
;
1275 int multibytep
= coding
->dst_multibyte
;
1276 int *charbuf
= coding
->charbuf
;
1277 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1278 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1279 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1280 int produced_chars
= 0;
1285 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
1287 while (charbuf
< charbuf_end
)
1289 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p
, *pend
= str
;
1291 ASSURE_DESTINATION (safe_room
);
1293 if (CHAR_BYTE8_P (c
))
1295 c
= CHAR_TO_BYTE8 (c
);
1300 CHAR_STRING_ADVANCE (c
, pend
);
1301 for (p
= str
; p
< pend
; p
++)
1308 int safe_room
= MAX_MULTIBYTE_LENGTH
;
1310 while (charbuf
< charbuf_end
)
1312 ASSURE_DESTINATION (safe_room
);
1314 dst
+= CHAR_STRING (c
, dst
);
1318 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1319 coding
->produced_char
+= produced_chars
;
1320 coding
->produced
= dst
- coding
->destination
;
1325 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1326 Check if a text is encoded in one of UTF-16 based coding systems.
1327 If it is, return 1, else return 0. */
1329 #define UTF_16_HIGH_SURROGATE_P(val) \
1330 (((val) & 0xFC00) == 0xD800)
1332 #define UTF_16_LOW_SURROGATE_P(val) \
1333 (((val) & 0xFC00) == 0xDC00)
1335 #define UTF_16_INVALID_P(val) \
1336 (((val) == 0xFFFE) \
1337 || ((val) == 0xFFFF) \
1338 || UTF_16_LOW_SURROGATE_P (val))
1342 detect_coding_utf_16 (coding
, detect_info
)
1343 struct coding_system
*coding
;
1344 struct coding_detection_info
*detect_info
;
1346 const unsigned char *src
= coding
->source
, *src_base
= src
;
1347 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1348 int multibytep
= coding
->src_multibyte
;
1349 int consumed_chars
= 0;
1352 detect_info
->checked
|= CATEGORY_MASK_UTF_16
;
1353 if (coding
->mode
& CODING_MODE_LAST_BLOCK
1354 && (coding
->src_chars
& 1))
1356 detect_info
->rejected
|= CATEGORY_MASK_UTF_16
;
1362 if ((c1
== 0xFF) && (c2
== 0xFE))
1364 detect_info
->found
|= (CATEGORY_MASK_UTF_16_LE
1365 | CATEGORY_MASK_UTF_16_AUTO
);
1366 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_BE
1367 | CATEGORY_MASK_UTF_16_BE_NOSIG
1368 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1370 else if ((c1
== 0xFE) && (c2
== 0xFF))
1372 detect_info
->found
|= (CATEGORY_MASK_UTF_16_BE
1373 | CATEGORY_MASK_UTF_16_AUTO
);
1374 detect_info
->rejected
|= (CATEGORY_MASK_UTF_16_LE
1375 | CATEGORY_MASK_UTF_16_BE_NOSIG
1376 | CATEGORY_MASK_UTF_16_LE_NOSIG
);
1378 else if (c1
>= 0 && c2
>= 0)
1380 unsigned char b1
[256], b2
[256];
1381 int b1_variants
= 1, b2_variants
= 1;
1384 bzero (b1
, 256), bzero (b2
, 256);
1386 for (n
= 0; n
< 256 && src
< src_end
; n
++)
1391 if (c1
< 0 || c2
< 0)
1393 if (! b1
[c1
++]) b1_variants
++;
1394 if (! b2
[c2
++]) b2_variants
++;
1396 if (b1_variants
< b2_variants
)
1397 detect_info
->found
|= CATEGORY_MASK_UTF_16_BE_NOSIG
;
1399 detect_info
->found
|= CATEGORY_MASK_UTF_16_LE_NOSIG
;
1400 detect_info
->rejected
1401 |= (CATEGORY_MASK_UTF_16_BE
| CATEGORY_MASK_UTF_16_LE
);
1408 decode_coding_utf_16 (coding
)
1409 struct coding_system
*coding
;
1411 const unsigned char *src
= coding
->source
+ coding
->consumed
;
1412 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1413 const unsigned char *src_base
;
1414 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
1415 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
1416 int consumed_chars
= 0, consumed_chars_base
;
1417 int multibytep
= coding
->src_multibyte
;
1418 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1419 enum utf_16_endian_type endian
= CODING_UTF_16_ENDIAN (coding
);
1420 int surrogate
= CODING_UTF_16_SURROGATE (coding
);
1421 Lisp_Object attr
, charset_list
;
1423 CODING_GET_INFO (coding
, attr
, charset_list
);
1425 if (bom
== utf_16_with_bom
)
1434 if (endian
== utf_16_big_endian
1435 ? c
!= 0xFEFF : c
!= 0xFFFE)
1437 /* The first two bytes are not BOM. Treat them as bytes
1438 for a normal character. */
1442 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1444 else if (bom
== utf_16_detect_bom
)
1446 /* We have already tried to detect BOM and failed in
1448 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1456 consumed_chars_base
= consumed_chars
;
1458 if (charbuf
+ 2 >= charbuf_end
)
1470 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
1474 c
= (endian
== utf_16_big_endian
1475 ? ((c1
<< 8) | c2
) : ((c2
<< 8) | c1
));
1478 if (! UTF_16_LOW_SURROGATE_P (c
))
1480 if (endian
== utf_16_big_endian
)
1481 c1
= surrogate
>> 8, c2
= surrogate
& 0xFF;
1483 c1
= surrogate
& 0xFF, c2
= surrogate
>> 8;
1487 if (UTF_16_HIGH_SURROGATE_P (c
))
1488 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1494 c
= ((surrogate
- 0xD800) << 10) | (c
- 0xDC00);
1495 CODING_UTF_16_SURROGATE (coding
) = surrogate
= 0;
1496 *charbuf
++ = 0x10000 + c
;
1501 if (UTF_16_HIGH_SURROGATE_P (c
))
1502 CODING_UTF_16_SURROGATE (coding
) = surrogate
= c
;
1509 coding
->consumed_char
+= consumed_chars_base
;
1510 coding
->consumed
= src_base
- coding
->source
;
1511 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
1515 encode_coding_utf_16 (coding
)
1516 struct coding_system
*coding
;
1518 int multibytep
= coding
->dst_multibyte
;
1519 int *charbuf
= coding
->charbuf
;
1520 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
1521 unsigned char *dst
= coding
->destination
+ coding
->produced
;
1522 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
1524 enum utf_16_bom_type bom
= CODING_UTF_16_BOM (coding
);
1525 int big_endian
= CODING_UTF_16_ENDIAN (coding
) == utf_16_big_endian
;
1526 int produced_chars
= 0;
1527 Lisp_Object attrs
, charset_list
;
1530 CODING_GET_INFO (coding
, attrs
, charset_list
);
1532 if (bom
!= utf_16_without_bom
)
1534 ASSURE_DESTINATION (safe_room
);
1536 EMIT_TWO_BYTES (0xFE, 0xFF);
1538 EMIT_TWO_BYTES (0xFF, 0xFE);
1539 CODING_UTF_16_BOM (coding
) = utf_16_without_bom
;
1542 while (charbuf
< charbuf_end
)
1544 ASSURE_DESTINATION (safe_room
);
1546 if (c
>= MAX_UNICODE_CHAR
)
1547 c
= coding
->default_char
;
1552 EMIT_TWO_BYTES (c
>> 8, c
& 0xFF);
1554 EMIT_TWO_BYTES (c
& 0xFF, c
>> 8);
1561 c1
= (c
>> 10) + 0xD800;
1562 c2
= (c
& 0x3FF) + 0xDC00;
1564 EMIT_FOUR_BYTES (c1
>> 8, c1
& 0xFF, c2
>> 8, c2
& 0xFF);
1566 EMIT_FOUR_BYTES (c1
& 0xFF, c1
>> 8, c2
& 0xFF, c2
>> 8);
1569 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
1570 coding
->produced
= dst
- coding
->destination
;
1571 coding
->produced_char
+= produced_chars
;
1576 /*** 6. Old Emacs' internal format (emacs-mule) ***/
1578 /* Emacs' internal format for representation of multiple character
1579 sets is a kind of multi-byte encoding, i.e. characters are
1580 represented by variable-length sequences of one-byte codes.
1582 ASCII characters and control characters (e.g. `tab', `newline') are
1583 represented by one-byte sequences which are their ASCII codes, in
1584 the range 0x00 through 0x7F.
1586 8-bit characters of the range 0x80..0x9F are represented by
1587 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1590 8-bit characters of the range 0xA0..0xFF are represented by
1591 one-byte sequences which are their 8-bit code.
1593 The other characters are represented by a sequence of `base
1594 leading-code', optional `extended leading-code', and one or two
1595 `position-code's. The length of the sequence is determined by the
1596 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1597 whereas extended leading-code and position-code take the range 0xA0
1598 through 0xFF. See `charset.h' for more details about leading-code
1601 --- CODE RANGE of Emacs' internal format ---
1605 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1606 eight-bit-graphic 0xA0..0xBF
1607 ELSE 0x81..0x9D + [0xA0..0xFF]+
1608 ---------------------------------------------
1610 As this is the internal character representation, the format is
1611 usually not used externally (i.e. in a file or in a data sent to a
1612 process). But, it is possible to have a text externally in this
1613 format (i.e. by encoding by the coding system `emacs-mule').
1615 In that case, a sequence of one-byte codes has a slightly different
1618 At first, all characters in eight-bit-control are represented by
1619 one-byte sequences which are their 8-bit code.
1621 Next, character composition data are represented by the byte
1622 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1624 METHOD is 0xF0 plus one of composition method (enum
1625 composition_method),
1627 BYTES is 0xA0 plus a byte length of this composition data,
1629 CHARS is 0x20 plus a number of characters composed by this
1632 COMPONENTs are characters of multibye form or composition
1633 rules encoded by two-byte of ASCII codes.
1635 In addition, for backward compatibility, the following formats are
1636 also recognized as composition data on decoding.
1639 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1642 MSEQ is a multibyte form but in these special format:
1643 ASCII: 0xA0 ASCII_CODE+0x80,
1644 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1645 RULE is a one byte code of the range 0xA0..0xF0 that
1646 represents a composition rule.
1649 char emacs_mule_bytes
[256];
1652 emacs_mule_char (coding
, src
, nbytes
, nchars
, id
)
1653 struct coding_system
*coding
;
1654 const unsigned char *src
;
1655 int *nbytes
, *nchars
, *id
;
1657 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1658 const unsigned char *src_base
= src
;
1659 int multibytep
= coding
->src_multibyte
;
1660 struct charset
*charset
;
1663 int consumed_chars
= 0;
1669 charset
= emacs_mule_charset
[0];
1673 switch (emacs_mule_bytes
[c
])
1676 if (! (charset
= emacs_mule_charset
[c
]))
1685 if (c
== EMACS_MULE_LEADING_CODE_PRIVATE_11
1686 || c
== EMACS_MULE_LEADING_CODE_PRIVATE_12
)
1689 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
1698 if (! (charset
= emacs_mule_charset
[c
]))
1703 code
= (c
& 0x7F) << 8;
1713 if (c
< 0 || ! (charset
= emacs_mule_charset
[c
]))
1718 code
= (c
& 0x7F) << 8;
1727 charset
= CHARSET_FROM_ID (ASCII_BYTE_P (code
)
1728 ? charset_ascii
: charset_eight_bit
);
1734 c
= DECODE_CHAR (charset
, code
);
1738 *nbytes
= src
- src_base
;
1739 *nchars
= consumed_chars
;
1752 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1753 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1757 detect_coding_emacs_mule (coding
, detect_info
)
1758 struct coding_system
*coding
;
1759 struct coding_detection_info
*detect_info
;
1761 const unsigned char *src
= coding
->source
, *src_base
;
1762 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
1763 int multibytep
= coding
->src_multibyte
;
1764 int consumed_chars
= 0;
1768 detect_info
->checked
|= CATEGORY_MASK_EMACS_MULE
;
1769 /* A coding system of this category is always ASCII compatible. */
1770 src
+= coding
->head_ascii
;
1780 /* Perhaps the start of composite character. We simple skip
1781 it because analyzing it is too heavy for detecting. But,
1782 at least, we check that the composite character
1783 constitues of more than 4 bytes. */
1784 const unsigned char *src_base
;
1794 if (src
- src_base
<= 4)
1796 found
= CATEGORY_MASK_EMACS_MULE
;
1804 && (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
1809 int more_bytes
= emacs_mule_bytes
[*src_base
] - 1;
1811 while (more_bytes
> 0)
1816 src
--; /* Unread the last byte. */
1821 if (more_bytes
!= 0)
1823 found
= CATEGORY_MASK_EMACS_MULE
;
1826 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1830 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
1832 detect_info
->rejected
|= CATEGORY_MASK_EMACS_MULE
;
1835 detect_info
->found
|= found
;
1840 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1842 /* Decode a character represented as a component of composition
1843 sequence of Emacs 20/21 style at SRC. Set C to that character and
1844 update SRC to the head of next character (or an encoded composition
1845 rule). If SRC doesn't points a composition component, set C to -1.
1846 If SRC points an invalid byte sequence, global exit by a return
1849 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
1853 int nbytes, nchars; \
1855 if (src == src_end) \
1857 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
1862 goto invalid_code; \
1866 consumed_chars += nchars; \
1871 /* Decode a composition rule represented as a component of composition
1872 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
1873 and increment BUF. If SRC points an invalid byte sequence, set C
1876 #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
1878 int c, gref, nref; \
1880 if (src >= src_end) \
1881 goto invalid_code; \
1882 ONE_MORE_BYTE_NO_CHECK (c); \
1884 if (c < 0 || c >= 81) \
1885 goto invalid_code; \
1887 gref = c / 9, nref = c % 9; \
1888 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1892 /* Decode a composition rule represented as a component of composition
1893 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
1894 and increment BUF. If SRC points an invalid byte sequence, set C
1897 #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
1901 if (src + 1>= src_end) \
1902 goto invalid_code; \
1903 ONE_MORE_BYTE_NO_CHECK (gref); \
1905 ONE_MORE_BYTE_NO_CHECK (nref); \
1907 if (gref < 0 || gref >= 81 \
1908 || nref < 0 || nref >= 81) \
1909 goto invalid_code; \
1910 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1914 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
1916 /* Emacs 21 style format. The first three bytes at SRC are \
1917 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
1918 the byte length of this composition information, CHARS is the \
1919 number of characters composed by this composition. */ \
1920 enum composition_method method = c - 0xF2; \
1921 int *charbuf_base = charbuf; \
1922 int consumed_chars_limit; \
1923 int nbytes, nchars; \
1925 ONE_MORE_BYTE (c); \
1927 goto invalid_code; \
1928 nbytes = c - 0xA0; \
1930 goto invalid_code; \
1931 ONE_MORE_BYTE (c); \
1933 goto invalid_code; \
1934 nchars = c - 0xA0; \
1935 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
1936 consumed_chars_limit = consumed_chars_base + nbytes; \
1937 if (method != COMPOSITION_RELATIVE) \
1940 while (consumed_chars < consumed_chars_limit) \
1942 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
1943 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
1945 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
1948 if (consumed_chars < consumed_chars_limit) \
1949 goto invalid_code; \
1950 charbuf_base[0] -= i; \
1955 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
1957 /* Emacs 20 style format for relative composition. */ \
1958 /* Store multibyte form of characters to be composed. */ \
1959 enum composition_method method = COMPOSITION_RELATIVE; \
1960 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
1961 int *buf = components; \
1965 ONE_MORE_BYTE (c); /* skip 0x80 */ \
1966 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
1967 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
1969 goto invalid_code; \
1970 ADD_COMPOSITION_DATA (charbuf, i, method); \
1971 for (j = 0; j < i; j++) \
1972 *charbuf++ = components[j]; \
1976 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
1978 /* Emacs 20 style format for rule-base composition. */ \
1979 /* Store multibyte form of characters to be composed. */ \
1980 enum composition_method method = COMPOSITION_WITH_RULE; \
1981 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
1982 int *buf = components; \
1985 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
1986 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
1988 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
1989 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
1991 if (i < 1 || (buf - components) % 2 == 0) \
1992 goto invalid_code; \
1993 if (charbuf + i + (i / 2) + 1 < charbuf_end) \
1994 goto no_more_source; \
1995 ADD_COMPOSITION_DATA (buf, i, method); \
1996 for (j = 0; j < i; j++) \
1997 *charbuf++ = components[j]; \
1998 for (j = 0; j < i; j += 2) \
1999 *charbuf++ = components[j]; \
2004 decode_coding_emacs_mule (coding
)
2005 struct coding_system
*coding
;
2007 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2008 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2009 const unsigned char *src_base
;
2010 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2012 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
2013 int consumed_chars
= 0, consumed_chars_base
;
2014 int multibytep
= coding
->src_multibyte
;
2015 Lisp_Object attrs
, charset_list
;
2016 int char_offset
= coding
->produced_char
;
2017 int last_offset
= char_offset
;
2018 int last_id
= charset_ascii
;
2020 CODING_GET_INFO (coding
, attrs
, charset_list
);
2027 consumed_chars_base
= consumed_chars
;
2029 if (charbuf
>= charbuf_end
)
2048 if (c
- 0xF2 >= COMPOSITION_RELATIVE
2049 && c
- 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS
)
2050 DECODE_EMACS_MULE_21_COMPOSITION (c
);
2052 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c
);
2054 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c
);
2058 else if (c
< 0xA0 && emacs_mule_bytes
[c
] > 1)
2064 consumed_chars
= consumed_chars_base
;
2065 c
= emacs_mule_char (coding
, src
, &nbytes
, &nchars
, &id
);
2074 if (last_id
!= charset_ascii
)
2075 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2077 last_offset
= char_offset
;
2081 consumed_chars
+= nchars
;
2088 consumed_chars
= consumed_chars_base
;
2090 *charbuf
++ = ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
2096 if (last_id
!= charset_ascii
)
2097 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
2098 coding
->consumed_char
+= consumed_chars_base
;
2099 coding
->consumed
= src_base
- coding
->source
;
2100 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
2104 #define EMACS_MULE_LEADING_CODES(id, codes) \
2107 codes[0] = id, codes[1] = 0; \
2108 else if (id < 0xE0) \
2109 codes[0] = 0x9A, codes[1] = id; \
2110 else if (id < 0xF0) \
2111 codes[0] = 0x9B, codes[1] = id; \
2112 else if (id < 0xF5) \
2113 codes[0] = 0x9C, codes[1] = id; \
2115 codes[0] = 0x9D, codes[1] = id; \
2120 encode_coding_emacs_mule (coding
)
2121 struct coding_system
*coding
;
2123 int multibytep
= coding
->dst_multibyte
;
2124 int *charbuf
= coding
->charbuf
;
2125 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
2126 unsigned char *dst
= coding
->destination
+ coding
->produced
;
2127 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
2129 int produced_chars
= 0;
2130 Lisp_Object attrs
, charset_list
;
2132 int preferred_charset_id
= -1;
2134 CODING_GET_INFO (coding
, attrs
, charset_list
);
2135 if (! EQ (charset_list
, Vemacs_mule_charset_list
))
2137 CODING_ATTR_CHARSET_LIST (attrs
)
2138 = charset_list
= Vemacs_mule_charset_list
;
2141 while (charbuf
< charbuf_end
)
2143 ASSURE_DESTINATION (safe_room
);
2148 /* Handle an annotation. */
2151 case CODING_ANNOTATE_COMPOSITION_MASK
:
2152 /* Not yet implemented. */
2154 case CODING_ANNOTATE_CHARSET_MASK
:
2155 preferred_charset_id
= charbuf
[3];
2156 if (preferred_charset_id
>= 0
2157 && NILP (Fmemq (make_number (preferred_charset_id
),
2159 preferred_charset_id
= -1;
2168 if (ASCII_CHAR_P (c
))
2169 EMIT_ONE_ASCII_BYTE (c
);
2170 else if (CHAR_BYTE8_P (c
))
2172 c
= CHAR_TO_BYTE8 (c
);
2177 struct charset
*charset
;
2181 unsigned char leading_codes
[2];
2183 if (preferred_charset_id
>= 0)
2185 charset
= CHARSET_FROM_ID (preferred_charset_id
);
2186 if (! CHAR_CHARSET_P (c
, charset
))
2187 charset
= char_charset (c
, charset_list
, NULL
);
2190 charset
= char_charset (c
, charset_list
, &code
);
2193 c
= coding
->default_char
;
2194 if (ASCII_CHAR_P (c
))
2196 EMIT_ONE_ASCII_BYTE (c
);
2199 charset
= char_charset (c
, charset_list
, &code
);
2201 dimension
= CHARSET_DIMENSION (charset
);
2202 emacs_mule_id
= CHARSET_EMACS_MULE_ID (charset
);
2203 EMACS_MULE_LEADING_CODES (emacs_mule_id
, leading_codes
);
2204 EMIT_ONE_BYTE (leading_codes
[0]);
2205 if (leading_codes
[1])
2206 EMIT_ONE_BYTE (leading_codes
[1]);
2208 EMIT_ONE_BYTE (code
| 0x80);
2212 EMIT_ONE_BYTE (code
>> 8);
2213 EMIT_ONE_BYTE (code
& 0xFF);
2217 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
2218 coding
->produced_char
+= produced_chars
;
2219 coding
->produced
= dst
- coding
->destination
;
2224 /*** 7. ISO2022 handlers ***/
2226 /* The following note describes the coding system ISO2022 briefly.
2227 Since the intention of this note is to help understand the
2228 functions in this file, some parts are NOT ACCURATE or are OVERLY
2229 SIMPLIFIED. For thorough understanding, please refer to the
2230 original document of ISO2022. This is equivalent to the standard
2231 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
2233 ISO2022 provides many mechanisms to encode several character sets
2234 in 7-bit and 8-bit environments. For 7-bit environments, all text
2235 is encoded using bytes less than 128. This may make the encoded
2236 text a little bit longer, but the text passes more easily through
2237 several types of gateway, some of which strip off the MSB (Most
2240 There are two kinds of character sets: control character sets and
2241 graphic character sets. The former contain control characters such
2242 as `newline' and `escape' to provide control functions (control
2243 functions are also provided by escape sequences). The latter
2244 contain graphic characters such as 'A' and '-'. Emacs recognizes
2245 two control character sets and many graphic character sets.
2247 Graphic character sets are classified into one of the following
2248 four classes, according to the number of bytes (DIMENSION) and
2249 number of characters in one dimension (CHARS) of the set:
2250 - DIMENSION1_CHARS94
2251 - DIMENSION1_CHARS96
2252 - DIMENSION2_CHARS94
2253 - DIMENSION2_CHARS96
2255 In addition, each character set is assigned an identification tag,
2256 unique for each set, called the "final character" (denoted as <F>
2257 hereafter). The <F> of each character set is decided by ECMA(*)
2258 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2259 (0x30..0x3F are for private use only).
2261 Note (*): ECMA = European Computer Manufacturers Association
2263 Here are examples of graphic character sets [NAME(<F>)]:
2264 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2265 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2266 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2267 o DIMENSION2_CHARS96 -- none for the moment
2269 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
2270 C0 [0x00..0x1F] -- control character plane 0
2271 GL [0x20..0x7F] -- graphic character plane 0
2272 C1 [0x80..0x9F] -- control character plane 1
2273 GR [0xA0..0xFF] -- graphic character plane 1
2275 A control character set is directly designated and invoked to C0 or
2276 C1 by an escape sequence. The most common case is that:
2277 - ISO646's control character set is designated/invoked to C0, and
2278 - ISO6429's control character set is designated/invoked to C1,
2279 and usually these designations/invocations are omitted in encoded
2280 text. In a 7-bit environment, only C0 can be used, and a control
2281 character for C1 is encoded by an appropriate escape sequence to
2282 fit into the environment. All control characters for C1 are
2283 defined to have corresponding escape sequences.
2285 A graphic character set is at first designated to one of four
2286 graphic registers (G0 through G3), then these graphic registers are
2287 invoked to GL or GR. These designations and invocations can be
2288 done independently. The most common case is that G0 is invoked to
2289 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2290 these invocations and designations are omitted in encoded text.
2291 In a 7-bit environment, only GL can be used.
2293 When a graphic character set of CHARS94 is invoked to GL, codes
2294 0x20 and 0x7F of the GL area work as control characters SPACE and
2295 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2298 There are two ways of invocation: locking-shift and single-shift.
2299 With locking-shift, the invocation lasts until the next different
2300 invocation, whereas with single-shift, the invocation affects the
2301 following character only and doesn't affect the locking-shift
2302 state. Invocations are done by the following control characters or
2305 ----------------------------------------------------------------------
2306 abbrev function cntrl escape seq description
2307 ----------------------------------------------------------------------
2308 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2309 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2310 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2311 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2312 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2313 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2314 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2315 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2316 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
2317 ----------------------------------------------------------------------
2318 (*) These are not used by any known coding system.
2320 Control characters for these functions are defined by macros
2321 ISO_CODE_XXX in `coding.h'.
2323 Designations are done by the following escape sequences:
2324 ----------------------------------------------------------------------
2325 escape sequence description
2326 ----------------------------------------------------------------------
2327 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2328 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2329 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2330 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2331 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2332 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2333 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2334 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2335 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2336 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2337 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2338 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2339 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2340 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2341 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2342 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2343 ----------------------------------------------------------------------
2345 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
2346 of dimension 1, chars 94, and final character <F>, etc...
2348 Note (*): Although these designations are not allowed in ISO2022,
2349 Emacs accepts them on decoding, and produces them on encoding
2350 CHARS96 character sets in a coding system which is characterized as
2351 7-bit environment, non-locking-shift, and non-single-shift.
2353 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
2354 '(' must be omitted. We refer to this as "short-form" hereafter.
2356 Now you may notice that there are a lot of ways of encoding the
2357 same multilingual text in ISO2022. Actually, there exist many
2358 coding systems such as Compound Text (used in X11's inter client
2359 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2360 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
2361 localized platforms), and all of these are variants of ISO2022.
2363 In addition to the above, Emacs handles two more kinds of escape
2364 sequences: ISO6429's direction specification and Emacs' private
2365 sequence for specifying character composition.
2367 ISO6429's direction specification takes the following form:
2368 o CSI ']' -- end of the current direction
2369 o CSI '0' ']' -- end of the current direction
2370 o CSI '1' ']' -- start of left-to-right text
2371 o CSI '2' ']' -- start of right-to-left text
2372 The control character CSI (0x9B: control sequence introducer) is
2373 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2375 Character composition specification takes the following form:
2376 o ESC '0' -- start relative composition
2377 o ESC '1' -- end composition
2378 o ESC '2' -- start rule-base composition (*)
2379 o ESC '3' -- start relative composition with alternate chars (**)
2380 o ESC '4' -- start rule-base composition with alternate chars (**)
2381 Since these are not standard escape sequences of any ISO standard,
2382 the use of them with these meanings is restricted to Emacs only.
2384 (*) This form is used only in Emacs 20.7 and older versions,
2385 but newer versions can safely decode it.
2386 (**) This form is used only in Emacs 21.1 and newer versions,
2387 and older versions can't decode it.
2389 Here's a list of example usages of these composition escape
2390 sequences (categorized by `enum composition_method').
2392 COMPOSITION_RELATIVE:
2393 ESC 0 CHAR [ CHAR ] ESC 1
2394 COMPOSITION_WITH_RULE:
2395 ESC 2 CHAR [ RULE CHAR ] ESC 1
2396 COMPOSITION_WITH_ALTCHARS:
2397 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
2398 COMPOSITION_WITH_RULE_ALTCHARS:
2399 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
2401 enum iso_code_class_type iso_code_class
[256];
2403 #define SAFE_CHARSET_P(coding, id) \
2404 ((id) <= (coding)->max_charset_id \
2405 && (coding)->safe_charsets[id] >= 0)
2408 #define SHIFT_OUT_OK(category) \
2409 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2412 setup_iso_safe_charsets (attrs
)
2415 Lisp_Object charset_list
, safe_charsets
;
2416 Lisp_Object request
;
2417 Lisp_Object reg_usage
;
2420 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
2423 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
2424 if ((flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
2425 && ! EQ (charset_list
, Viso_2022_charset_list
))
2427 CODING_ATTR_CHARSET_LIST (attrs
)
2428 = charset_list
= Viso_2022_charset_list
;
2429 ASET (attrs
, coding_attr_safe_charsets
, Qnil
);
2432 if (STRINGP (AREF (attrs
, coding_attr_safe_charsets
)))
2436 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2438 int id
= XINT (XCAR (tail
));
2439 if (max_charset_id
< id
)
2440 max_charset_id
= id
;
2443 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
2445 request
= AREF (attrs
, coding_attr_iso_request
);
2446 reg_usage
= AREF (attrs
, coding_attr_iso_usage
);
2447 reg94
= XINT (XCAR (reg_usage
));
2448 reg96
= XINT (XCDR (reg_usage
));
2450 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
2454 struct charset
*charset
;
2457 charset
= CHARSET_FROM_ID (XINT (id
));
2458 reg
= Fcdr (Fassq (id
, request
));
2460 SSET (safe_charsets
, XINT (id
), XINT (reg
));
2461 else if (charset
->iso_chars_96
)
2464 SSET (safe_charsets
, XINT (id
), reg96
);
2469 SSET (safe_charsets
, XINT (id
), reg94
);
2472 ASET (attrs
, coding_attr_safe_charsets
, safe_charsets
);
2476 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2477 Check if a text is encoded in one of ISO-2022 based codig systems.
2478 If it is, return 1, else return 0. */
2481 detect_coding_iso_2022 (coding
, detect_info
)
2482 struct coding_system
*coding
;
2483 struct coding_detection_info
*detect_info
;
2485 const unsigned char *src
= coding
->source
, *src_base
= src
;
2486 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2487 int multibytep
= coding
->src_multibyte
;
2488 int single_shifting
= 0;
2491 int consumed_chars
= 0;
2496 detect_info
->checked
|= CATEGORY_MASK_ISO
;
2498 for (i
= coding_category_iso_7
; i
<= coding_category_iso_8_else
; i
++)
2500 struct coding_system
*this = &(coding_categories
[i
]);
2501 Lisp_Object attrs
, val
;
2503 attrs
= CODING_ID_ATTRS (this->id
);
2504 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2505 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs
), Viso_2022_charset_list
))
2506 setup_iso_safe_charsets (attrs
);
2507 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
2508 this->max_charset_id
= SCHARS (val
) - 1;
2509 this->safe_charsets
= (char *) SDATA (val
);
2512 /* A coding system of this category is always ASCII compatible. */
2513 src
+= coding
->head_ascii
;
2515 while (rejected
!= CATEGORY_MASK_ISO
)
2522 if (inhibit_iso_escape_detection
)
2524 single_shifting
= 0;
2526 if (c
>= '(' && c
<= '/')
2528 /* Designation sequence for a charset of dimension 1. */
2530 if (c1
< ' ' || c1
>= 0x80
2531 || (id
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
2532 /* Invalid designation sequence. Just ignore. */
2537 /* Designation sequence for a charset of dimension 2. */
2539 if (c
>= '@' && c
<= 'B')
2540 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
2541 id
= iso_charset_table
[1][0][c
];
2542 else if (c
>= '(' && c
<= '/')
2545 if (c1
< ' ' || c1
>= 0x80
2546 || (id
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
2547 /* Invalid designation sequence. Just ignore. */
2551 /* Invalid designation sequence. Just ignore it. */
2554 else if (c
== 'N' || c
== 'O')
2556 /* ESC <Fe> for SS2 or SS3. */
2557 single_shifting
= 1;
2558 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2561 else if (c
>= '0' && c
<= '4')
2563 /* ESC <Fp> for start/end composition. */
2564 found
|= CATEGORY_MASK_ISO
;
2569 /* Invalid escape sequence. Just ignore it. */
2573 /* We found a valid designation sequence for CHARSET. */
2574 rejected
|= CATEGORY_MASK_ISO_8BIT
;
2575 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7
],
2577 found
|= CATEGORY_MASK_ISO_7
;
2579 rejected
|= CATEGORY_MASK_ISO_7
;
2580 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_tight
],
2582 found
|= CATEGORY_MASK_ISO_7_TIGHT
;
2584 rejected
|= CATEGORY_MASK_ISO_7_TIGHT
;
2585 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_7_else
],
2587 found
|= CATEGORY_MASK_ISO_7_ELSE
;
2589 rejected
|= CATEGORY_MASK_ISO_7_ELSE
;
2590 if (SAFE_CHARSET_P (&coding_categories
[coding_category_iso_8_else
],
2592 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2594 rejected
|= CATEGORY_MASK_ISO_8_ELSE
;
2599 /* Locking shift out/in. */
2600 if (inhibit_iso_escape_detection
)
2602 single_shifting
= 0;
2603 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_8BIT
;
2604 found
|= CATEGORY_MASK_ISO_ELSE
;
2608 /* Control sequence introducer. */
2609 single_shifting
= 0;
2610 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2611 found
|= CATEGORY_MASK_ISO_8_ELSE
;
2612 goto check_extra_latin
;
2617 if (inhibit_iso_escape_detection
)
2619 single_shifting
= 0;
2620 rejected
|= CATEGORY_MASK_ISO_7BIT
;
2621 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2622 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2623 found
|= CATEGORY_MASK_ISO_8_1
, single_shifting
= 1;
2624 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_2
])
2625 & CODING_ISO_FLAG_SINGLE_SHIFT
)
2626 found
|= CATEGORY_MASK_ISO_8_2
, single_shifting
= 1;
2627 if (single_shifting
)
2629 goto check_extra_latin
;
2636 single_shifting
= 0;
2641 rejected
|= CATEGORY_MASK_ISO_7BIT
| CATEGORY_MASK_ISO_7_ELSE
;
2642 found
|= CATEGORY_MASK_ISO_8_1
;
2643 /* Check the length of succeeding codes of the range
2644 0xA0..0FF. If the byte length is even, we include
2645 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2646 only when we are not single shifting. */
2647 if (! single_shifting
2648 && ! (rejected
& CATEGORY_MASK_ISO_8_2
))
2651 while (src
< src_end
)
2659 if (i
& 1 && src
< src_end
)
2660 rejected
|= CATEGORY_MASK_ISO_8_2
;
2662 found
|= CATEGORY_MASK_ISO_8_2
;
2667 single_shifting
= 0;
2668 if (! VECTORP (Vlatin_extra_code_table
)
2669 || NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
2671 rejected
= CATEGORY_MASK_ISO
;
2674 if (CODING_ISO_FLAGS (&coding_categories
[coding_category_iso_8_1
])
2675 & CODING_ISO_FLAG_LATIN_EXTRA
)
2676 found
|= CATEGORY_MASK_ISO_8_1
;
2678 rejected
|= CATEGORY_MASK_ISO_8_1
;
2679 rejected
|= CATEGORY_MASK_ISO_8_2
;
2682 detect_info
->rejected
|= CATEGORY_MASK_ISO
;
2686 detect_info
->rejected
|= rejected
;
2687 detect_info
->found
|= (found
& ~rejected
);
2692 /* Set designation state into CODING. */
2693 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2697 if (final < '0' || final >= 128 \
2698 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2699 || !SAFE_CHARSET_P (coding, id)) \
2701 CODING_ISO_DESIGNATION (coding, reg) = -2; \
2702 goto invalid_code; \
2704 prev = CODING_ISO_DESIGNATION (coding, reg); \
2705 if (id == charset_jisx0201_roman) \
2707 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2708 id = charset_ascii; \
2710 else if (id == charset_jisx0208_1978) \
2712 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2713 id = charset_jisx0208; \
2715 CODING_ISO_DESIGNATION (coding, reg) = id; \
2716 /* If there was an invalid designation to REG previously, and this \
2717 designation is ASCII to REG, we should keep this designation \
2719 if (prev == -2 && id == charset_ascii) \
2720 goto invalid_code; \
2724 #define MAYBE_FINISH_COMPOSITION() \
2727 if (composition_state == COMPOSING_NO) \
2729 /* It is assured that we have enough room for producing \
2730 characters stored in the table `components'. */ \
2731 if (charbuf + component_idx > charbuf_end) \
2732 goto no_more_source; \
2733 composition_state = COMPOSING_NO; \
2734 if (method == COMPOSITION_RELATIVE \
2735 || method == COMPOSITION_WITH_ALTCHARS) \
2737 for (i = 0; i < component_idx; i++) \
2738 *charbuf++ = components[i]; \
2739 char_offset += component_idx; \
2743 for (i = 0; i < component_idx; i += 2) \
2744 *charbuf++ = components[i]; \
2745 char_offset += (component_idx / 2) + 1; \
2750 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2751 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2752 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
2753 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2754 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
2757 #define DECODE_COMPOSITION_START(c1) \
2760 && composition_state == COMPOSING_COMPONENT_RULE) \
2762 component_len = component_idx; \
2763 composition_state = COMPOSING_CHAR; \
2767 const unsigned char *p; \
2769 MAYBE_FINISH_COMPOSITION (); \
2770 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2771 goto no_more_source; \
2772 for (p = src; p < src_end - 1; p++) \
2773 if (*p == ISO_CODE_ESC && p[1] == '1') \
2775 if (p == src_end - 1) \
2777 if (coding->mode & CODING_MODE_LAST_BLOCK) \
2778 goto invalid_code; \
2779 goto no_more_source; \
2782 /* This is surely the start of a composition. */ \
2783 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2784 : c1 == '2' ? COMPOSITION_WITH_RULE \
2785 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2786 : COMPOSITION_WITH_RULE_ALTCHARS); \
2787 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2788 : COMPOSING_COMPONENT_CHAR); \
2789 component_idx = component_len = 0; \
2794 /* Handle compositoin end sequence ESC 1. */
2796 #define DECODE_COMPOSITION_END() \
2798 int nchars = (component_len > 0 ? component_idx - component_len \
2799 : method == COMPOSITION_RELATIVE ? component_idx \
2800 : (component_idx + 1) / 2); \
2802 int *saved_charbuf = charbuf; \
2804 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2805 if (method != COMPOSITION_RELATIVE) \
2807 if (component_len == 0) \
2808 for (i = 0; i < component_idx; i++) \
2809 *charbuf++ = components[i]; \
2811 for (i = 0; i < component_len; i++) \
2812 *charbuf++ = components[i]; \
2813 *saved_charbuf = saved_charbuf - charbuf; \
2815 if (method == COMPOSITION_WITH_RULE) \
2816 for (i = 0; i < component_idx; i += 2, char_offset++) \
2817 *charbuf++ = components[i]; \
2819 for (i = component_len; i < component_idx; i++, char_offset++) \
2820 *charbuf++ = components[i]; \
2821 coding->annotated = 1; \
2822 composition_state = COMPOSING_NO; \
2826 /* Decode a composition rule from the byte C1 (and maybe one more byte
2827 from SRC) and store one encoded composition rule in
2828 coding->cmp_data. */
2830 #define DECODE_COMPOSITION_RULE(c1) \
2833 if (c1 < 81) /* old format (before ver.21) */ \
2835 int gref = (c1) / 9; \
2836 int nref = (c1) % 9; \
2837 if (gref == 4) gref = 10; \
2838 if (nref == 4) nref = 10; \
2839 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
2841 else if (c1 < 93) /* new format (after ver.21) */ \
2843 ONE_MORE_BYTE (c2); \
2844 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
2851 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2854 decode_coding_iso_2022 (coding
)
2855 struct coding_system
*coding
;
2857 const unsigned char *src
= coding
->source
+ coding
->consumed
;
2858 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
2859 const unsigned char *src_base
;
2860 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
2862 = coding
->charbuf
+ coding
->charbuf_size
- 4 - MAX_ANNOTATION_LENGTH
;
2863 int consumed_chars
= 0, consumed_chars_base
;
2864 int multibytep
= coding
->src_multibyte
;
2865 /* Charsets invoked to graphic plane 0 and 1 respectively. */
2866 int charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2867 int charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
2868 struct charset
*charset
;
2870 /* For handling composition sequence. */
2871 #define COMPOSING_NO 0
2872 #define COMPOSING_CHAR 1
2873 #define COMPOSING_RULE 2
2874 #define COMPOSING_COMPONENT_CHAR 3
2875 #define COMPOSING_COMPONENT_RULE 4
2877 int composition_state
= COMPOSING_NO
;
2878 enum composition_method method
;
2879 int components
[MAX_COMPOSITION_COMPONENTS
* 2 + 1];
2882 Lisp_Object attrs
, charset_list
;
2883 int char_offset
= coding
->produced_char
;
2884 int last_offset
= char_offset
;
2885 int last_id
= charset_ascii
;
2887 CODING_GET_INFO (coding
, attrs
, charset_list
);
2888 setup_iso_safe_charsets (attrs
);
2895 consumed_chars_base
= consumed_chars
;
2897 if (charbuf
>= charbuf_end
)
2904 /* We produce at most one character. */
2905 switch (iso_code_class
[c1
])
2907 case ISO_0x20_or_0x7F
:
2908 if (composition_state
!= COMPOSING_NO
)
2910 if (composition_state
== COMPOSING_RULE
2911 || composition_state
== COMPOSING_COMPONENT_RULE
)
2913 DECODE_COMPOSITION_RULE (c1
);
2914 components
[component_idx
++] = c1
;
2915 composition_state
--;
2919 if (charset_id_0
< 0
2920 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0
)))
2921 /* This is SPACE or DEL. */
2922 charset
= CHARSET_FROM_ID (charset_ascii
);
2924 charset
= CHARSET_FROM_ID (charset_id_0
);
2927 case ISO_graphic_plane_0
:
2928 if (composition_state
!= COMPOSING_NO
)
2930 if (composition_state
== COMPOSING_RULE
2931 || composition_state
== COMPOSING_COMPONENT_RULE
)
2933 DECODE_COMPOSITION_RULE (c1
);
2934 components
[component_idx
++] = c1
;
2935 composition_state
--;
2939 charset
= CHARSET_FROM_ID (charset_id_0
);
2942 case ISO_0xA0_or_0xFF
:
2943 if (charset_id_1
< 0
2944 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1
))
2945 || CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SEVEN_BITS
)
2947 /* This is a graphic character, we fall down ... */
2949 case ISO_graphic_plane_1
:
2950 if (charset_id_1
< 0)
2952 charset
= CHARSET_FROM_ID (charset_id_1
);
2956 MAYBE_FINISH_COMPOSITION ();
2957 charset
= CHARSET_FROM_ID (charset_ascii
);
2961 MAYBE_FINISH_COMPOSITION ();
2965 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
2966 || CODING_ISO_DESIGNATION (coding
, 1) < 0)
2968 CODING_ISO_INVOCATION (coding
, 0) = 1;
2969 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2973 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
))
2975 CODING_ISO_INVOCATION (coding
, 0) = 0;
2976 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
2979 case ISO_single_shift_2_7
:
2980 case ISO_single_shift_2
:
2981 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
2983 /* SS2 is handled as an escape sequence of ESC 'N' */
2985 goto label_escape_sequence
;
2987 case ISO_single_shift_3
:
2988 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
))
2990 /* SS2 is handled as an escape sequence of ESC 'O' */
2992 goto label_escape_sequence
;
2994 case ISO_control_sequence_introducer
:
2995 /* CSI is handled as an escape sequence of ESC '[' ... */
2997 goto label_escape_sequence
;
3001 label_escape_sequence
:
3002 /* Escape sequences handled here are invocation,
3003 designation, direction specification, and character
3004 composition specification. */
3007 case '&': /* revision of following character set */
3009 if (!(c1
>= '@' && c1
<= '~'))
3012 if (c1
!= ISO_CODE_ESC
)
3015 goto label_escape_sequence
;
3017 case '$': /* designation of 2-byte character set */
3018 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3021 if (c1
>= '@' && c1
<= 'B')
3022 { /* designation of JISX0208.1978, GB2312.1980,
3024 DECODE_DESIGNATION (0, 2, 0, c1
);
3026 else if (c1
>= 0x28 && c1
<= 0x2B)
3027 { /* designation of DIMENSION2_CHARS94 character set */
3029 DECODE_DESIGNATION (c1
- 0x28, 2, 0, c2
);
3031 else if (c1
>= 0x2C && c1
<= 0x2F)
3032 { /* designation of DIMENSION2_CHARS96 character set */
3034 DECODE_DESIGNATION (c1
- 0x2C, 2, 1, c2
);
3038 /* We must update these variables now. */
3039 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3040 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3043 case 'n': /* invocation of locking-shift-2 */
3044 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3045 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3047 CODING_ISO_INVOCATION (coding
, 0) = 2;
3048 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3051 case 'o': /* invocation of locking-shift-3 */
3052 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_LOCKING_SHIFT
)
3053 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3055 CODING_ISO_INVOCATION (coding
, 0) = 3;
3056 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3059 case 'N': /* invocation of single-shift-2 */
3060 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3061 || CODING_ISO_DESIGNATION (coding
, 2) < 0)
3063 charset
= CHARSET_FROM_ID (CODING_ISO_DESIGNATION (coding
, 2));
3065 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3069 case 'O': /* invocation of single-shift-3 */
3070 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3071 || CODING_ISO_DESIGNATION (coding
, 3) < 0)
3073 charset
= CHARSET_FROM_ID (CODING_ISO_DESIGNATION (coding
, 3));
3075 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
3079 case '0': case '2': case '3': case '4': /* start composition */
3080 if (! (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
))
3082 DECODE_COMPOSITION_START (c1
);
3085 case '1': /* end composition */
3086 if (composition_state
== COMPOSING_NO
)
3088 DECODE_COMPOSITION_END ();
3091 case '[': /* specification of direction */
3092 if (! CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DIRECTION
)
3094 /* For the moment, nested direction is not supported.
3095 So, `coding->mode & CODING_MODE_DIRECTION' zero means
3096 left-to-right, and nozero means right-to-left. */
3100 case ']': /* end of the current direction */
3101 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3103 case '0': /* end of the current direction */
3104 case '1': /* start of left-to-right direction */
3107 coding
->mode
&= ~CODING_MODE_DIRECTION
;
3112 case '2': /* start of right-to-left direction */
3115 coding
->mode
|= CODING_MODE_DIRECTION
;
3129 /* CTEXT extended segment:
3130 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3131 We keep these bytes as is for the moment.
3132 They may be decoded by post-read-conversion. */
3136 ONE_MORE_BYTE (dim
);
3139 size
= ((M
- 128) * 128) + (L
- 128);
3140 if (charbuf
+ 8 + size
> charbuf_end
)
3142 *charbuf
++ = ISO_CODE_ESC
;
3146 *charbuf
++ = BYTE8_TO_CHAR (M
);
3147 *charbuf
++ = BYTE8_TO_CHAR (L
);
3151 *charbuf
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3156 /* XFree86 extension for embedding UTF-8 in CTEXT:
3157 ESC % G --UTF-8-BYTES-- ESC % @
3158 We keep these bytes as is for the moment.
3159 They may be decoded by post-read-conversion. */
3162 if (p
+ 6 > charbuf_end
)
3164 *p
++ = ISO_CODE_ESC
;
3167 while (p
< charbuf_end
)
3170 if (c1
== ISO_CODE_ESC
3171 && src
+ 1 < src_end
3175 *p
++ = ASCII_BYTE_P (c1
) ? c1
: BYTE8_TO_CHAR (c1
);
3177 if (p
+ 3 > charbuf_end
)
3179 *p
++ = ISO_CODE_ESC
;
3190 if (! (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATION
))
3192 if (c1
>= 0x28 && c1
<= 0x2B)
3193 { /* designation of DIMENSION1_CHARS94 character set */
3195 DECODE_DESIGNATION (c1
- 0x28, 1, 0, c2
);
3197 else if (c1
>= 0x2C && c1
<= 0x2F)
3198 { /* designation of DIMENSION1_CHARS96 character set */
3200 DECODE_DESIGNATION (c1
- 0x2C, 1, 1, c2
);
3204 /* We must update these variables now. */
3205 charset_id_0
= CODING_ISO_INVOKED_CHARSET (coding
, 0);
3206 charset_id_1
= CODING_ISO_INVOKED_CHARSET (coding
, 1);
3211 if (charset
->id
!= charset_ascii
3212 && last_id
!= charset
->id
)
3214 if (last_id
!= charset_ascii
)
3215 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3216 last_id
= charset
->id
;
3217 last_offset
= char_offset
;
3220 /* Now we know CHARSET and 1st position code C1 of a character.
3221 Produce a decoded character while getting 2nd position code
3224 if (CHARSET_DIMENSION (charset
) > 1)
3227 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3228 /* C2 is not in a valid range. */
3230 c1
= (c1
<< 8) | (c2
& 0x7F);
3231 if (CHARSET_DIMENSION (charset
) > 2)
3234 if (c2
< 0x20 || (c2
>= 0x80 && c2
< 0xA0))
3235 /* C2 is not in a valid range. */
3237 c1
= (c1
<< 8) | (c2
& 0x7F);
3241 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c1
, c
);
3244 MAYBE_FINISH_COMPOSITION ();
3245 for (; src_base
< src
; src_base
++, char_offset
++)
3247 if (ASCII_BYTE_P (*src_base
))
3248 *charbuf
++ = *src_base
;
3250 *charbuf
++ = BYTE8_TO_CHAR (*src_base
);
3253 else if (composition_state
== COMPOSING_NO
)
3260 components
[component_idx
++] = c
;
3261 if (method
== COMPOSITION_WITH_RULE
3262 || (method
== COMPOSITION_WITH_RULE_ALTCHARS
3263 && composition_state
== COMPOSING_COMPONENT_CHAR
))
3264 composition_state
++;
3269 MAYBE_FINISH_COMPOSITION ();
3271 consumed_chars
= consumed_chars_base
;
3273 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
3283 if (last_id
!= charset_ascii
)
3284 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
3285 coding
->consumed_char
+= consumed_chars_base
;
3286 coding
->consumed
= src_base
- coding
->source
;
3287 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
3291 /* ISO2022 encoding stuff. */
3294 It is not enough to say just "ISO2022" on encoding, we have to
3295 specify more details. In Emacs, each coding system of ISO2022
3296 variant has the following specifications:
3297 1. Initial designation to G0 thru G3.
3298 2. Allows short-form designation?
3299 3. ASCII should be designated to G0 before control characters?
3300 4. ASCII should be designated to G0 at end of line?
3301 5. 7-bit environment or 8-bit environment?
3302 6. Use locking-shift?
3303 7. Use Single-shift?
3304 And the following two are only for Japanese:
3305 8. Use ASCII in place of JIS0201-1976-Roman?
3306 9. Use JISX0208-1983 in place of JISX0208-1978?
3307 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3308 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
3312 /* Produce codes (escape sequence) for designating CHARSET to graphic
3313 register REG at DST, and increment DST. If <final-char> of CHARSET is
3314 '@', 'A', or 'B' and the coding system CODING allows, produce
3315 designation sequence of short-form. */
3317 #define ENCODE_DESIGNATION(charset, reg, coding) \
3319 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
3320 char *intermediate_char_94 = "()*+"; \
3321 char *intermediate_char_96 = ",-./"; \
3322 int revision = -1; \
3325 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3326 revision = CHARSET_ISO_REVISION (charset); \
3328 if (revision >= 0) \
3330 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3331 EMIT_ONE_BYTE ('@' + revision); \
3333 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
3334 if (CHARSET_DIMENSION (charset) == 1) \
3336 if (! CHARSET_ISO_CHARS_96 (charset)) \
3337 c = intermediate_char_94[reg]; \
3339 c = intermediate_char_96[reg]; \
3340 EMIT_ONE_ASCII_BYTE (c); \
3344 EMIT_ONE_ASCII_BYTE ('$'); \
3345 if (! CHARSET_ISO_CHARS_96 (charset)) \
3347 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
3349 || final_char < '@' || final_char > 'B') \
3350 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
3353 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
3355 EMIT_ONE_ASCII_BYTE (final_char); \
3357 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
3361 /* The following two macros produce codes (control character or escape
3362 sequence) for ISO2022 single-shift functions (single-shift-2 and
3365 #define ENCODE_SINGLE_SHIFT_2 \
3367 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3368 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3370 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3371 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3375 #define ENCODE_SINGLE_SHIFT_3 \
3377 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3378 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3380 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3381 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
3385 /* The following four macros produce codes (control character or
3386 escape sequence) for ISO2022 locking-shift functions (shift-in,
3387 shift-out, locking-shift-2, and locking-shift-3). */
3389 #define ENCODE_SHIFT_IN \
3391 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3392 CODING_ISO_INVOCATION (coding, 0) = 0; \
3396 #define ENCODE_SHIFT_OUT \
3398 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3399 CODING_ISO_INVOCATION (coding, 0) = 1; \
3403 #define ENCODE_LOCKING_SHIFT_2 \
3405 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3406 CODING_ISO_INVOCATION (coding, 0) = 2; \
3410 #define ENCODE_LOCKING_SHIFT_3 \
3412 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3413 CODING_ISO_INVOCATION (coding, 0) = 3; \
3417 /* Produce codes for a DIMENSION1 character whose character set is
3418 CHARSET and whose position-code is C1. Designation and invocation
3419 sequences are also produced in advance if necessary. */
3421 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3423 int id = CHARSET_ID (charset); \
3425 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3426 && id == charset_ascii) \
3428 id = charset_jisx0201_roman; \
3429 charset = CHARSET_FROM_ID (id); \
3432 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3434 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3435 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3437 EMIT_ONE_BYTE (c1 | 0x80); \
3438 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3441 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3443 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
3446 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3448 EMIT_ONE_BYTE (c1 | 0x80); \
3452 /* Since CHARSET is not yet invoked to any graphic planes, we \
3453 must invoke it, or, at first, designate it to some graphic \
3454 register. Then repeat the loop to actually produce the \
3456 dst = encode_invocation_designation (charset, coding, dst, \
3461 /* Produce codes for a DIMENSION2 character whose character set is
3462 CHARSET and whose position-codes are C1 and C2. Designation and
3463 invocation codes are also produced in advance if necessary. */
3465 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3467 int id = CHARSET_ID (charset); \
3469 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3470 && id == charset_jisx0208) \
3472 id = charset_jisx0208_1978; \
3473 charset = CHARSET_FROM_ID (id); \
3476 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
3478 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3479 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3481 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3482 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
3485 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
3487 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
3490 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
3492 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3496 /* Since CHARSET is not yet invoked to any graphic planes, we \
3497 must invoke it, or, at first, designate it to some graphic \
3498 register. Then repeat the loop to actually produce the \
3500 dst = encode_invocation_designation (charset, coding, dst, \
3505 #define ENCODE_ISO_CHARACTER(charset, c) \
3507 int code = ENCODE_CHAR ((charset),(c)); \
3509 if (CHARSET_DIMENSION (charset) == 1) \
3510 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3512 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
3516 /* Produce designation and invocation codes at a place pointed by DST
3517 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
3521 encode_invocation_designation (charset
, coding
, dst
, p_nchars
)
3522 struct charset
*charset
;
3523 struct coding_system
*coding
;
3527 int multibytep
= coding
->dst_multibyte
;
3528 int produced_chars
= *p_nchars
;
3529 int reg
; /* graphic register number */
3530 int id
= CHARSET_ID (charset
);
3532 /* At first, check designations. */
3533 for (reg
= 0; reg
< 4; reg
++)
3534 if (id
== CODING_ISO_DESIGNATION (coding
, reg
))
3539 /* CHARSET is not yet designated to any graphic registers. */
3540 /* At first check the requested designation. */
3541 reg
= CODING_ISO_REQUEST (coding
, id
);
3543 /* Since CHARSET requests no special designation, designate it
3544 to graphic register 0. */
3547 ENCODE_DESIGNATION (charset
, reg
, coding
);
3550 if (CODING_ISO_INVOCATION (coding
, 0) != reg
3551 && CODING_ISO_INVOCATION (coding
, 1) != reg
)
3553 /* Since the graphic register REG is not invoked to any graphic
3554 planes, invoke it to graphic plane 0. */
3557 case 0: /* graphic register 0 */
3561 case 1: /* graphic register 1 */
3565 case 2: /* graphic register 2 */
3566 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3567 ENCODE_SINGLE_SHIFT_2
;
3569 ENCODE_LOCKING_SHIFT_2
;
3572 case 3: /* graphic register 3 */
3573 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_SINGLE_SHIFT
)
3574 ENCODE_SINGLE_SHIFT_3
;
3576 ENCODE_LOCKING_SHIFT_3
;
3581 *p_nchars
= produced_chars
;
3585 /* The following three macros produce codes for indicating direction
3587 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
3589 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3590 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
3592 EMIT_ONE_BYTE (ISO_CODE_CSI); \
3596 #define ENCODE_DIRECTION_R2L() \
3598 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3599 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3603 #define ENCODE_DIRECTION_L2R() \
3605 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3606 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3610 /* Produce codes for designation and invocation to reset the graphic
3611 planes and registers to initial state. */
3612 #define ENCODE_RESET_PLANE_AND_REGISTER() \
3615 struct charset *charset; \
3617 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3619 for (reg = 0; reg < 4; reg++) \
3620 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3621 && (CODING_ISO_DESIGNATION (coding, reg) \
3622 != CODING_ISO_INITIAL (coding, reg))) \
3624 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3625 ENCODE_DESIGNATION (charset, reg, coding); \
3630 /* Produce designation sequences of charsets in the line started from
3631 SRC to a place pointed by DST, and return updated DST.
3633 If the current block ends before any end-of-line, we may fail to
3634 find all the necessary designations. */
3636 static unsigned char *
3637 encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
)
3638 struct coding_system
*coding
;
3639 int *charbuf
, *charbuf_end
;
3642 struct charset
*charset
;
3643 /* Table of charsets to be designated to each graphic register. */
3645 int c
, found
= 0, reg
;
3646 int produced_chars
= 0;
3647 int multibytep
= coding
->dst_multibyte
;
3649 Lisp_Object charset_list
;
3651 attrs
= CODING_ID_ATTRS (coding
->id
);
3652 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
3653 if (EQ (charset_list
, Qiso_2022
))
3654 charset_list
= Viso_2022_charset_list
;
3656 for (reg
= 0; reg
< 4; reg
++)
3666 charset
= char_charset (c
, charset_list
, NULL
);
3667 id
= CHARSET_ID (charset
);
3668 reg
= CODING_ISO_REQUEST (coding
, id
);
3669 if (reg
>= 0 && r
[reg
] < 0)
3678 for (reg
= 0; reg
< 4; reg
++)
3680 && CODING_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
3681 ENCODE_DESIGNATION (CHARSET_FROM_ID (r
[reg
]), reg
, coding
);
3687 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3690 encode_coding_iso_2022 (coding
)
3691 struct coding_system
*coding
;
3693 int multibytep
= coding
->dst_multibyte
;
3694 int *charbuf
= coding
->charbuf
;
3695 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
3696 unsigned char *dst
= coding
->destination
+ coding
->produced
;
3697 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
3700 = (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3701 && CODING_ISO_BOL (coding
));
3702 int produced_chars
= 0;
3703 Lisp_Object attrs
, eol_type
, charset_list
;
3704 int ascii_compatible
;
3706 int preferred_charset_id
= -1;
3708 CODING_GET_INFO (coding
, attrs
, charset_list
);
3709 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
3710 if (VECTORP (eol_type
))
3713 setup_iso_safe_charsets (attrs
);
3714 /* Charset list may have been changed. */
3715 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
); \
3716 coding
->safe_charsets
= (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs
));
3718 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
3720 while (charbuf
< charbuf_end
)
3722 ASSURE_DESTINATION (safe_room
);
3724 if (bol_designation
)
3726 unsigned char *dst_prev
= dst
;
3728 /* We have to produce designation sequences if any now. */
3729 dst
= encode_designation_at_bol (coding
, charbuf
, charbuf_end
, dst
);
3730 bol_designation
= 0;
3731 /* We are sure that designation sequences are all ASCII bytes. */
3732 produced_chars
+= dst
- dst_prev
;
3739 /* Handle an annotation. */
3742 case CODING_ANNOTATE_COMPOSITION_MASK
:
3743 /* Not yet implemented. */
3745 case CODING_ANNOTATE_CHARSET_MASK
:
3746 preferred_charset_id
= charbuf
[3];
3747 if (preferred_charset_id
>= 0
3748 && NILP (Fmemq (make_number (preferred_charset_id
),
3750 preferred_charset_id
= -1;
3759 /* Now encode the character C. */
3760 if (c
< 0x20 || c
== 0x7F)
3763 || (c
== '\r' && EQ (eol_type
, Qmac
)))
3765 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3766 ENCODE_RESET_PLANE_AND_REGISTER ();
3767 if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_INIT_AT_BOL
)
3771 for (i
= 0; i
< 4; i
++)
3772 CODING_ISO_DESIGNATION (coding
, i
)
3773 = CODING_ISO_INITIAL (coding
, i
);
3776 = CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
;
3778 else if (CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_CNTL
)
3779 ENCODE_RESET_PLANE_AND_REGISTER ();
3780 EMIT_ONE_ASCII_BYTE (c
);
3782 else if (ASCII_CHAR_P (c
))
3784 if (ascii_compatible
)
3785 EMIT_ONE_ASCII_BYTE (c
);
3788 struct charset
*charset
= CHARSET_FROM_ID (charset_ascii
);
3789 ENCODE_ISO_CHARACTER (charset
, c
);
3792 else if (CHAR_BYTE8_P (c
))
3794 c
= CHAR_TO_BYTE8 (c
);
3799 struct charset
*charset
;
3801 if (preferred_charset_id
>= 0)
3803 charset
= CHARSET_FROM_ID (preferred_charset_id
);
3804 if (! CHAR_CHARSET_P (c
, charset
))
3805 charset
= char_charset (c
, charset_list
, NULL
);
3808 charset
= char_charset (c
, charset_list
, NULL
);
3811 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
3813 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
3814 charset
= CHARSET_FROM_ID (charset_ascii
);
3818 c
= coding
->default_char
;
3819 charset
= char_charset (c
, charset_list
, NULL
);
3822 ENCODE_ISO_CHARACTER (charset
, c
);
3826 if (coding
->mode
& CODING_MODE_LAST_BLOCK
3827 && CODING_ISO_FLAGS (coding
) & CODING_ISO_FLAG_RESET_AT_EOL
)
3829 ASSURE_DESTINATION (safe_room
);
3830 ENCODE_RESET_PLANE_AND_REGISTER ();
3832 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
3833 CODING_ISO_BOL (coding
) = bol_designation
;
3834 coding
->produced_char
+= produced_chars
;
3835 coding
->produced
= dst
- coding
->destination
;
3840 /*** 8,9. SJIS and BIG5 handlers ***/
3842 /* Although SJIS and BIG5 are not ISO's coding system, they are used
3843 quite widely. So, for the moment, Emacs supports them in the bare
3844 C code. But, in the future, they may be supported only by CCL. */
3846 /* SJIS is a coding system encoding three character sets: ASCII, right
3847 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3848 as is. A character of charset katakana-jisx0201 is encoded by
3849 "position-code + 0x80". A character of charset japanese-jisx0208
3850 is encoded in 2-byte but two position-codes are divided and shifted
3851 so that it fit in the range below.
3853 --- CODE RANGE of SJIS ---
3854 (character set) (range)
3856 KATAKANA-JISX0201 0xA0 .. 0xDF
3857 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
3858 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
3859 -------------------------------
3863 /* BIG5 is a coding system encoding two character sets: ASCII and
3864 Big5. An ASCII character is encoded as is. Big5 is a two-byte
3865 character set and is encoded in two-byte.
3867 --- CODE RANGE of BIG5 ---
3868 (character set) (range)
3870 Big5 (1st byte) 0xA1 .. 0xFE
3871 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3872 --------------------------
3876 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3877 Check if a text is encoded in SJIS. If it is, return
3878 CATEGORY_MASK_SJIS, else return 0. */
3881 detect_coding_sjis (coding
, detect_info
)
3882 struct coding_system
*coding
;
3883 struct coding_detection_info
*detect_info
;
3885 const unsigned char *src
= coding
->source
, *src_base
;
3886 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3887 int multibytep
= coding
->src_multibyte
;
3888 int consumed_chars
= 0;
3892 detect_info
->checked
|= CATEGORY_MASK_SJIS
;
3893 /* A coding system of this category is always ASCII compatible. */
3894 src
+= coding
->head_ascii
;
3902 if ((c
>= 0x81 && c
<= 0x9F) || (c
>= 0xE0 && c
<= 0xEF))
3905 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
3907 found
= CATEGORY_MASK_SJIS
;
3909 else if (c
>= 0xA0 && c
< 0xE0)
3910 found
= CATEGORY_MASK_SJIS
;
3914 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
3918 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
3920 detect_info
->rejected
|= CATEGORY_MASK_SJIS
;
3923 detect_info
->found
|= found
;
3927 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3928 Check if a text is encoded in BIG5. If it is, return
3929 CATEGORY_MASK_BIG5, else return 0. */
3932 detect_coding_big5 (coding
, detect_info
)
3933 struct coding_system
*coding
;
3934 struct coding_detection_info
*detect_info
;
3936 const unsigned char *src
= coding
->source
, *src_base
;
3937 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3938 int multibytep
= coding
->src_multibyte
;
3939 int consumed_chars
= 0;
3943 detect_info
->checked
|= CATEGORY_MASK_BIG5
;
3944 /* A coding system of this category is always ASCII compatible. */
3945 src
+= coding
->head_ascii
;
3956 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
3958 found
= CATEGORY_MASK_BIG5
;
3963 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
3967 if (src_base
< src
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
3969 detect_info
->rejected
|= CATEGORY_MASK_BIG5
;
3972 detect_info
->found
|= found
;
3976 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
3977 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
3980 decode_coding_sjis (coding
)
3981 struct coding_system
*coding
;
3983 const unsigned char *src
= coding
->source
+ coding
->consumed
;
3984 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
3985 const unsigned char *src_base
;
3986 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
3988 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
3989 int consumed_chars
= 0, consumed_chars_base
;
3990 int multibytep
= coding
->src_multibyte
;
3991 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
3992 struct charset
*charset_kanji2
;
3993 Lisp_Object attrs
, charset_list
, val
;
3994 int char_offset
= coding
->produced_char
;
3995 int last_offset
= char_offset
;
3996 int last_id
= charset_ascii
;
3998 CODING_GET_INFO (coding
, attrs
, charset_list
);
4001 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4002 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4003 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4004 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4009 struct charset
*charset
;
4012 consumed_chars_base
= consumed_chars
;
4014 if (charbuf
>= charbuf_end
)
4021 charset
= charset_roman
;
4022 else if (c
== 0x80 || c
== 0xA0)
4024 else if (c
>= 0xA1 && c
<= 0xDF)
4026 /* SJIS -> JISX0201-Kana */
4028 charset
= charset_kana
;
4032 /* SJIS -> JISX0208 */
4034 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4038 charset
= charset_kanji
;
4040 else if (c
<= 0xFC && charset_kanji2
)
4042 /* SJIS -> JISX0213-2 */
4044 if (c1
< 0x40 || c1
== 0x7F || c1
> 0xFC)
4048 charset
= charset_kanji2
;
4052 if (charset
->id
!= charset_ascii
4053 && last_id
!= charset
->id
)
4055 if (last_id
!= charset_ascii
)
4056 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4057 last_id
= charset
->id
;
4058 last_offset
= char_offset
;
4060 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4067 consumed_chars
= consumed_chars_base
;
4069 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4075 if (last_id
!= charset_ascii
)
4076 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4077 coding
->consumed_char
+= consumed_chars_base
;
4078 coding
->consumed
= src_base
- coding
->source
;
4079 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4083 decode_coding_big5 (coding
)
4084 struct coding_system
*coding
;
4086 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4087 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4088 const unsigned char *src_base
;
4089 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4091 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4092 int consumed_chars
= 0, consumed_chars_base
;
4093 int multibytep
= coding
->src_multibyte
;
4094 struct charset
*charset_roman
, *charset_big5
;
4095 Lisp_Object attrs
, charset_list
, val
;
4096 int char_offset
= coding
->produced_char
;
4097 int last_offset
= char_offset
;
4098 int last_id
= charset_ascii
;
4100 CODING_GET_INFO (coding
, attrs
, charset_list
);
4102 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4103 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4108 struct charset
*charset
;
4111 consumed_chars_base
= consumed_chars
;
4113 if (charbuf
>= charbuf_end
)
4121 charset
= charset_roman
;
4125 if (c
< 0xA1 || c
> 0xFE)
4128 if (c1
< 0x40 || (c1
> 0x7E && c1
< 0xA1) || c1
> 0xFE)
4131 charset
= charset_big5
;
4133 if (charset
->id
!= charset_ascii
4134 && last_id
!= charset
->id
)
4136 if (last_id
!= charset_ascii
)
4137 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4138 last_id
= charset
->id
;
4139 last_offset
= char_offset
;
4141 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
, charset
, c
, c
);
4148 consumed_chars
= consumed_chars_base
;
4150 *charbuf
++ = c
< 0 ? -c
: BYTE8_TO_CHAR (c
);
4156 if (last_id
!= charset_ascii
)
4157 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4158 coding
->consumed_char
+= consumed_chars_base
;
4159 coding
->consumed
= src_base
- coding
->source
;
4160 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4163 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
4164 This function can encode charsets `ascii', `katakana-jisx0201',
4165 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4166 are sure that all these charsets are registered as official charset
4167 (i.e. do not have extended leading-codes). Characters of other
4168 charsets are produced without any encoding. If SJIS_P is 1, encode
4169 SJIS text, else encode BIG5 text. */
4172 encode_coding_sjis (coding
)
4173 struct coding_system
*coding
;
4175 int multibytep
= coding
->dst_multibyte
;
4176 int *charbuf
= coding
->charbuf
;
4177 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4178 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4179 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4181 int produced_chars
= 0;
4182 Lisp_Object attrs
, charset_list
, val
;
4183 int ascii_compatible
;
4184 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
;
4185 struct charset
*charset_kanji2
;
4188 CODING_GET_INFO (coding
, attrs
, charset_list
);
4190 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4191 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4192 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4193 charset_kanji2
= NILP (val
) ? NULL
: CHARSET_FROM_ID (XINT (XCAR (val
)));
4195 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4197 while (charbuf
< charbuf_end
)
4199 ASSURE_DESTINATION (safe_room
);
4201 /* Now encode the character C. */
4202 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4203 EMIT_ONE_ASCII_BYTE (c
);
4204 else if (CHAR_BYTE8_P (c
))
4206 c
= CHAR_TO_BYTE8 (c
);
4212 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4216 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4218 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4219 charset
= CHARSET_FROM_ID (charset_ascii
);
4223 c
= coding
->default_char
;
4224 charset
= char_charset (c
, charset_list
, &code
);
4227 if (code
== CHARSET_INVALID_CODE (charset
))
4229 if (charset
== charset_kanji
)
4233 c1
= code
>> 8, c2
= code
& 0xFF;
4234 EMIT_TWO_BYTES (c1
, c2
);
4236 else if (charset
== charset_kana
)
4237 EMIT_ONE_BYTE (code
| 0x80);
4238 else if (charset_kanji2
&& charset
== charset_kanji2
)
4243 if (c1
== 0x21 || (c1
>= 0x23 && c1
< 0x25)
4244 || (c1
>= 0x2C && c1
<= 0x2F) || c1
>= 0x6E)
4246 JIS_TO_SJIS2 (code
);
4247 c1
= code
>> 8, c2
= code
& 0xFF;
4248 EMIT_TWO_BYTES (c1
, c2
);
4251 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4254 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4257 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4258 coding
->produced_char
+= produced_chars
;
4259 coding
->produced
= dst
- coding
->destination
;
4264 encode_coding_big5 (coding
)
4265 struct coding_system
*coding
;
4267 int multibytep
= coding
->dst_multibyte
;
4268 int *charbuf
= coding
->charbuf
;
4269 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4270 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4271 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4273 int produced_chars
= 0;
4274 Lisp_Object attrs
, charset_list
, val
;
4275 int ascii_compatible
;
4276 struct charset
*charset_roman
, *charset_big5
;
4279 CODING_GET_INFO (coding
, attrs
, charset_list
);
4281 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
4282 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
4283 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4285 while (charbuf
< charbuf_end
)
4287 ASSURE_DESTINATION (safe_room
);
4289 /* Now encode the character C. */
4290 if (ASCII_CHAR_P (c
) && ascii_compatible
)
4291 EMIT_ONE_ASCII_BYTE (c
);
4292 else if (CHAR_BYTE8_P (c
))
4294 c
= CHAR_TO_BYTE8 (c
);
4300 struct charset
*charset
= char_charset (c
, charset_list
, &code
);
4304 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4306 code
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4307 charset
= CHARSET_FROM_ID (charset_ascii
);
4311 c
= coding
->default_char
;
4312 charset
= char_charset (c
, charset_list
, &code
);
4315 if (code
== CHARSET_INVALID_CODE (charset
))
4317 if (charset
== charset_big5
)
4321 c1
= code
>> 8, c2
= code
& 0xFF;
4322 EMIT_TWO_BYTES (c1
, c2
);
4325 EMIT_ONE_ASCII_BYTE (code
& 0x7F);
4328 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4329 coding
->produced_char
+= produced_chars
;
4330 coding
->produced
= dst
- coding
->destination
;
4335 /*** 10. CCL handlers ***/
4337 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4338 Check if a text is encoded in a coding system of which
4339 encoder/decoder are written in CCL program. If it is, return
4340 CATEGORY_MASK_CCL, else return 0. */
4343 detect_coding_ccl (coding
, detect_info
)
4344 struct coding_system
*coding
;
4345 struct coding_detection_info
*detect_info
;
4347 const unsigned char *src
= coding
->source
, *src_base
;
4348 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4349 int multibytep
= coding
->src_multibyte
;
4350 int consumed_chars
= 0;
4352 unsigned char *valids
;
4353 int head_ascii
= coding
->head_ascii
;
4356 detect_info
->checked
|= CATEGORY_MASK_CCL
;
4358 coding
= &coding_categories
[coding_category_ccl
];
4359 valids
= CODING_CCL_VALIDS (coding
);
4360 attrs
= CODING_ID_ATTRS (coding
->id
);
4361 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4370 if (c
< 0 || ! valids
[c
])
4372 if ((valids
[c
] > 1))
4373 found
= CATEGORY_MASK_CCL
;
4375 detect_info
->rejected
|= CATEGORY_MASK_CCL
;
4379 detect_info
->found
|= found
;
4384 decode_coding_ccl (coding
)
4385 struct coding_system
*coding
;
4387 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4388 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4389 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4390 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_size
;
4391 int consumed_chars
= 0;
4392 int multibytep
= coding
->src_multibyte
;
4393 struct ccl_program ccl
;
4394 int source_charbuf
[1024];
4395 int source_byteidx
[1024];
4396 Lisp_Object attrs
, charset_list
;
4398 CODING_GET_INFO (coding
, attrs
, charset_list
);
4399 setup_ccl_program (&ccl
, CODING_CCL_DECODER (coding
));
4401 while (src
< src_end
)
4403 const unsigned char *p
= src
;
4404 int *source
, *source_end
;
4408 while (i
< 1024 && p
< src_end
)
4410 source_byteidx
[i
] = p
- src
;
4411 source_charbuf
[i
++] = STRING_CHAR_ADVANCE (p
);
4414 while (i
< 1024 && p
< src_end
)
4415 source_charbuf
[i
++] = *p
++;
4417 if (p
== src_end
&& coding
->mode
& CODING_MODE_LAST_BLOCK
)
4420 source
= source_charbuf
;
4421 source_end
= source
+ i
;
4422 while (source
< source_end
)
4424 ccl_driver (&ccl
, source
, charbuf
,
4425 source_end
- source
, charbuf_end
- charbuf
,
4427 source
+= ccl
.consumed
;
4428 charbuf
+= ccl
.produced
;
4429 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_DST
)
4432 if (source
< source_end
)
4433 src
+= source_byteidx
[source
- source_charbuf
];
4436 consumed_chars
+= source
- source_charbuf
;
4438 if (ccl
.status
!= CCL_STAT_SUSPEND_BY_SRC
4439 && ccl
.status
!= CODING_RESULT_INSUFFICIENT_SRC
)
4445 case CCL_STAT_SUSPEND_BY_SRC
:
4446 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4448 case CCL_STAT_SUSPEND_BY_DST
:
4451 case CCL_STAT_INVALID_CMD
:
4452 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4455 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4458 coding
->consumed_char
+= consumed_chars
;
4459 coding
->consumed
= src
- coding
->source
;
4460 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4464 encode_coding_ccl (coding
)
4465 struct coding_system
*coding
;
4467 struct ccl_program ccl
;
4468 int multibytep
= coding
->dst_multibyte
;
4469 int *charbuf
= coding
->charbuf
;
4470 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4471 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4472 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4473 unsigned char *adjusted_dst_end
= dst_end
- 1;
4474 int destination_charbuf
[1024];
4475 int i
, produced_chars
= 0;
4476 Lisp_Object attrs
, charset_list
;
4478 CODING_GET_INFO (coding
, attrs
, charset_list
);
4479 setup_ccl_program (&ccl
, CODING_CCL_ENCODER (coding
));
4481 ccl
.last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4482 ccl
.dst_multibyte
= coding
->dst_multibyte
;
4484 while (charbuf
< charbuf_end
&& dst
< adjusted_dst_end
)
4486 int dst_bytes
= dst_end
- dst
;
4487 if (dst_bytes
> 1024)
4490 ccl_driver (&ccl
, charbuf
, destination_charbuf
,
4491 charbuf_end
- charbuf
, dst_bytes
, charset_list
);
4492 charbuf
+= ccl
.consumed
;
4494 for (i
= 0; i
< ccl
.produced
; i
++)
4495 EMIT_ONE_BYTE (destination_charbuf
[i
] & 0xFF);
4498 for (i
= 0; i
< ccl
.produced
; i
++)
4499 *dst
++ = destination_charbuf
[i
] & 0xFF;
4500 produced_chars
+= ccl
.produced
;
4506 case CCL_STAT_SUSPEND_BY_SRC
:
4507 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
4509 case CCL_STAT_SUSPEND_BY_DST
:
4510 record_conversion_result (coding
, CODING_RESULT_INSUFFICIENT_DST
);
4513 case CCL_STAT_INVALID_CMD
:
4514 record_conversion_result (coding
, CODING_RESULT_INTERRUPT
);
4517 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4521 coding
->produced_char
+= produced_chars
;
4522 coding
->produced
= dst
- coding
->destination
;
4528 /*** 10, 11. no-conversion handlers ***/
4530 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4533 decode_coding_raw_text (coding
)
4534 struct coding_system
*coding
;
4536 coding
->chars_at_source
= 1;
4537 coding
->consumed_char
= 0;
4538 coding
->consumed
= 0;
4539 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4543 encode_coding_raw_text (coding
)
4544 struct coding_system
*coding
;
4546 int multibytep
= coding
->dst_multibyte
;
4547 int *charbuf
= coding
->charbuf
;
4548 int *charbuf_end
= coding
->charbuf
+ coding
->charbuf_used
;
4549 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4550 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4551 int produced_chars
= 0;
4556 int safe_room
= MAX_MULTIBYTE_LENGTH
* 2;
4558 if (coding
->src_multibyte
)
4559 while (charbuf
< charbuf_end
)
4561 ASSURE_DESTINATION (safe_room
);
4563 if (ASCII_CHAR_P (c
))
4564 EMIT_ONE_ASCII_BYTE (c
);
4565 else if (CHAR_BYTE8_P (c
))
4567 c
= CHAR_TO_BYTE8 (c
);
4572 unsigned char str
[MAX_MULTIBYTE_LENGTH
], *p0
= str
, *p1
= str
;
4574 CHAR_STRING_ADVANCE (c
, p1
);
4577 EMIT_ONE_BYTE (*p0
);
4583 while (charbuf
< charbuf_end
)
4585 ASSURE_DESTINATION (safe_room
);
4592 if (coding
->src_multibyte
)
4594 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4596 while (charbuf
< charbuf_end
)
4598 ASSURE_DESTINATION (safe_room
);
4600 if (ASCII_CHAR_P (c
))
4602 else if (CHAR_BYTE8_P (c
))
4603 *dst
++ = CHAR_TO_BYTE8 (c
);
4605 CHAR_STRING_ADVANCE (c
, dst
);
4611 ASSURE_DESTINATION (charbuf_end
- charbuf
);
4612 while (charbuf
< charbuf_end
&& dst
< dst_end
)
4613 *dst
++ = *charbuf
++;
4614 produced_chars
= dst
- (coding
->destination
+ coding
->dst_bytes
);
4617 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4618 coding
->produced_char
+= produced_chars
;
4619 coding
->produced
= dst
- coding
->destination
;
4623 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4624 Check if a text is encoded in a charset-based coding system. If it
4625 is, return 1, else return 0. */
4628 detect_coding_charset (coding
, detect_info
)
4629 struct coding_system
*coding
;
4630 struct coding_detection_info
*detect_info
;
4632 const unsigned char *src
= coding
->source
, *src_base
;
4633 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4634 int multibytep
= coding
->src_multibyte
;
4635 int consumed_chars
= 0;
4636 Lisp_Object attrs
, valids
;
4639 detect_info
->checked
|= CATEGORY_MASK_CHARSET
;
4641 coding
= &coding_categories
[coding_category_charset
];
4642 attrs
= CODING_ID_ATTRS (coding
->id
);
4643 valids
= AREF (attrs
, coding_attr_charset_valids
);
4645 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
4646 src
+= coding
->head_ascii
;
4656 if (NILP (AREF (valids
, c
)))
4659 found
= CATEGORY_MASK_CHARSET
;
4661 detect_info
->rejected
|= CATEGORY_MASK_CHARSET
;
4665 detect_info
->found
|= found
;
4670 decode_coding_charset (coding
)
4671 struct coding_system
*coding
;
4673 const unsigned char *src
= coding
->source
+ coding
->consumed
;
4674 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
4675 const unsigned char *src_base
;
4676 int *charbuf
= coding
->charbuf
+ coding
->charbuf_used
;
4678 = coding
->charbuf
+ coding
->charbuf_size
- MAX_ANNOTATION_LENGTH
;
4679 int consumed_chars
= 0, consumed_chars_base
;
4680 int multibytep
= coding
->src_multibyte
;
4681 Lisp_Object attrs
, charset_list
, valids
;
4682 int char_offset
= coding
->produced_char
;
4683 int last_offset
= char_offset
;
4684 int last_id
= charset_ascii
;
4686 CODING_GET_INFO (coding
, attrs
, charset_list
);
4687 valids
= AREF (attrs
, coding_attr_charset_valids
);
4693 struct charset
*charset
;
4699 consumed_chars_base
= consumed_chars
;
4701 if (charbuf
>= charbuf_end
)
4709 val
= AREF (valids
, c
);
4714 charset
= CHARSET_FROM_ID (XFASTINT (val
));
4715 dim
= CHARSET_DIMENSION (charset
);
4719 code
= (code
<< 8) | c
;
4722 CODING_DECODE_CHAR (coding
, src
, src_base
, src_end
,
4727 /* VAL is a list of charset IDs. It is assured that the
4728 list is sorted by charset dimensions (smaller one
4732 charset
= CHARSET_FROM_ID (XFASTINT (XCAR (val
)));
4733 dim
= CHARSET_DIMENSION (charset
);
4737 code
= (code
<< 8) | c
;
4740 CODING_DECODE_CHAR (coding
, src
, src_base
,
4741 src_end
, charset
, code
, c
);
4749 if (charset
->id
!= charset_ascii
4750 && last_id
!= charset
->id
)
4752 if (last_id
!= charset_ascii
)
4753 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4754 last_id
= charset
->id
;
4755 last_offset
= char_offset
;
4764 consumed_chars
= consumed_chars_base
;
4766 *charbuf
++ = c
< 0 ? -c
: ASCII_BYTE_P (c
) ? c
: BYTE8_TO_CHAR (c
);
4772 if (last_id
!= charset_ascii
)
4773 ADD_CHARSET_DATA (charbuf
, char_offset
- last_offset
, last_id
);
4774 coding
->consumed_char
+= consumed_chars_base
;
4775 coding
->consumed
= src_base
- coding
->source
;
4776 coding
->charbuf_used
= charbuf
- coding
->charbuf
;
4780 encode_coding_charset (coding
)
4781 struct coding_system
*coding
;
4783 int multibytep
= coding
->dst_multibyte
;
4784 int *charbuf
= coding
->charbuf
;
4785 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
4786 unsigned char *dst
= coding
->destination
+ coding
->produced
;
4787 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
4788 int safe_room
= MAX_MULTIBYTE_LENGTH
;
4789 int produced_chars
= 0;
4790 Lisp_Object attrs
, charset_list
;
4791 int ascii_compatible
;
4794 CODING_GET_INFO (coding
, attrs
, charset_list
);
4795 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
4797 while (charbuf
< charbuf_end
)
4799 struct charset
*charset
;
4802 ASSURE_DESTINATION (safe_room
);
4804 if (ascii_compatible
&& ASCII_CHAR_P (c
))
4805 EMIT_ONE_ASCII_BYTE (c
);
4806 else if (CHAR_BYTE8_P (c
))
4808 c
= CHAR_TO_BYTE8 (c
);
4813 charset
= char_charset (c
, charset_list
, &code
);
4816 if (CHARSET_DIMENSION (charset
) == 1)
4817 EMIT_ONE_BYTE (code
);
4818 else if (CHARSET_DIMENSION (charset
) == 2)
4819 EMIT_TWO_BYTES (code
>> 8, code
& 0xFF);
4820 else if (CHARSET_DIMENSION (charset
) == 3)
4821 EMIT_THREE_BYTES (code
>> 16, (code
>> 8) & 0xFF, code
& 0xFF);
4823 EMIT_FOUR_BYTES (code
>> 24, (code
>> 16) & 0xFF,
4824 (code
>> 8) & 0xFF, code
& 0xFF);
4828 if (coding
->mode
& CODING_MODE_SAFE_ENCODING
)
4829 c
= CODING_INHIBIT_CHARACTER_SUBSTITUTION
;
4831 c
= coding
->default_char
;
4837 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
4838 coding
->produced_char
+= produced_chars
;
4839 coding
->produced
= dst
- coding
->destination
;
4844 /*** 7. C library functions ***/
4846 /* Setup coding context CODING from information about CODING_SYSTEM.
4847 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4848 CODING_SYSTEM is invalid, signal an error. */
4851 setup_coding_system (coding_system
, coding
)
4852 Lisp_Object coding_system
;
4853 struct coding_system
*coding
;
4856 Lisp_Object eol_type
;
4857 Lisp_Object coding_type
;
4860 if (NILP (coding_system
))
4861 coding_system
= Qno_conversion
;
4863 CHECK_CODING_SYSTEM_GET_ID (coding_system
, coding
->id
);
4865 attrs
= CODING_ID_ATTRS (coding
->id
);
4866 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
4869 coding
->head_ascii
= -1;
4870 coding
->common_flags
4871 = (VECTORP (eol_type
) ? CODING_REQUIRE_DETECTION_MASK
: 0);
4872 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
4873 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
4874 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
4875 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
4876 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs
)))
4877 coding
->common_flags
|= CODING_FOR_UNIBYTE_MASK
;
4879 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
4880 coding
->max_charset_id
= SCHARS (val
) - 1;
4881 coding
->safe_charsets
= (char *) SDATA (val
);
4882 coding
->default_char
= XINT (CODING_ATTR_DEFAULT_CHAR (attrs
));
4884 coding_type
= CODING_ATTR_TYPE (attrs
);
4885 if (EQ (coding_type
, Qundecided
))
4887 coding
->detector
= NULL
;
4888 coding
->decoder
= decode_coding_raw_text
;
4889 coding
->encoder
= encode_coding_raw_text
;
4890 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
4892 else if (EQ (coding_type
, Qiso_2022
))
4895 int flags
= XINT (AREF (attrs
, coding_attr_iso_flags
));
4897 /* Invoke graphic register 0 to plane 0. */
4898 CODING_ISO_INVOCATION (coding
, 0) = 0;
4899 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
4900 CODING_ISO_INVOCATION (coding
, 1)
4901 = (flags
& CODING_ISO_FLAG_SEVEN_BITS
? -1 : 1);
4902 /* Setup the initial status of designation. */
4903 for (i
= 0; i
< 4; i
++)
4904 CODING_ISO_DESIGNATION (coding
, i
) = CODING_ISO_INITIAL (coding
, i
);
4905 /* Not single shifting initially. */
4906 CODING_ISO_SINGLE_SHIFTING (coding
) = 0;
4907 /* Beginning of buffer should also be regarded as bol. */
4908 CODING_ISO_BOL (coding
) = 1;
4909 coding
->detector
= detect_coding_iso_2022
;
4910 coding
->decoder
= decode_coding_iso_2022
;
4911 coding
->encoder
= encode_coding_iso_2022
;
4912 if (flags
& CODING_ISO_FLAG_SAFE
)
4913 coding
->mode
|= CODING_MODE_SAFE_ENCODING
;
4914 coding
->common_flags
4915 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
4916 | CODING_REQUIRE_FLUSHING_MASK
);
4917 if (flags
& CODING_ISO_FLAG_COMPOSITION
)
4918 coding
->common_flags
|= CODING_ANNOTATE_COMPOSITION_MASK
;
4919 if (flags
& CODING_ISO_FLAG_DESIGNATION
)
4920 coding
->common_flags
|= CODING_ANNOTATE_CHARSET_MASK
;
4921 if (flags
& CODING_ISO_FLAG_FULL_SUPPORT
)
4923 setup_iso_safe_charsets (attrs
);
4924 val
= CODING_ATTR_SAFE_CHARSETS (attrs
);
4925 coding
->max_charset_id
= SCHARS (val
) - 1;
4926 coding
->safe_charsets
= (char *) SDATA (val
);
4928 CODING_ISO_FLAGS (coding
) = flags
;
4930 else if (EQ (coding_type
, Qcharset
))
4932 coding
->detector
= detect_coding_charset
;
4933 coding
->decoder
= decode_coding_charset
;
4934 coding
->encoder
= encode_coding_charset
;
4935 coding
->common_flags
4936 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
4938 else if (EQ (coding_type
, Qutf_8
))
4940 coding
->detector
= detect_coding_utf_8
;
4941 coding
->decoder
= decode_coding_utf_8
;
4942 coding
->encoder
= encode_coding_utf_8
;
4943 coding
->common_flags
4944 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
4946 else if (EQ (coding_type
, Qutf_16
))
4948 val
= AREF (attrs
, coding_attr_utf_16_bom
);
4949 CODING_UTF_16_BOM (coding
) = (CONSP (val
) ? utf_16_detect_bom
4950 : EQ (val
, Qt
) ? utf_16_with_bom
4951 : utf_16_without_bom
);
4952 val
= AREF (attrs
, coding_attr_utf_16_endian
);
4953 CODING_UTF_16_ENDIAN (coding
) = (EQ (val
, Qbig
) ? utf_16_big_endian
4954 : utf_16_little_endian
);
4955 CODING_UTF_16_SURROGATE (coding
) = 0;
4956 coding
->detector
= detect_coding_utf_16
;
4957 coding
->decoder
= decode_coding_utf_16
;
4958 coding
->encoder
= encode_coding_utf_16
;
4959 coding
->common_flags
4960 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
4961 if (CODING_UTF_16_BOM (coding
) == utf_16_detect_bom
)
4962 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
4964 else if (EQ (coding_type
, Qccl
))
4966 coding
->detector
= detect_coding_ccl
;
4967 coding
->decoder
= decode_coding_ccl
;
4968 coding
->encoder
= encode_coding_ccl
;
4969 coding
->common_flags
4970 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
4971 | CODING_REQUIRE_FLUSHING_MASK
);
4973 else if (EQ (coding_type
, Qemacs_mule
))
4975 coding
->detector
= detect_coding_emacs_mule
;
4976 coding
->decoder
= decode_coding_emacs_mule
;
4977 coding
->encoder
= encode_coding_emacs_mule
;
4978 coding
->common_flags
4979 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
4980 if (! NILP (AREF (attrs
, coding_attr_emacs_mule_full
))
4981 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs
), Vemacs_mule_charset_list
))
4983 Lisp_Object tail
, safe_charsets
;
4984 int max_charset_id
= 0;
4986 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
4988 if (max_charset_id
< XFASTINT (XCAR (tail
)))
4989 max_charset_id
= XFASTINT (XCAR (tail
));
4990 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
4992 for (tail
= Vemacs_mule_charset_list
; CONSP (tail
);
4994 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
4995 coding
->max_charset_id
= max_charset_id
;
4996 coding
->safe_charsets
= (char *) SDATA (safe_charsets
);
4999 else if (EQ (coding_type
, Qshift_jis
))
5001 coding
->detector
= detect_coding_sjis
;
5002 coding
->decoder
= decode_coding_sjis
;
5003 coding
->encoder
= encode_coding_sjis
;
5004 coding
->common_flags
5005 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5007 else if (EQ (coding_type
, Qbig5
))
5009 coding
->detector
= detect_coding_big5
;
5010 coding
->decoder
= decode_coding_big5
;
5011 coding
->encoder
= encode_coding_big5
;
5012 coding
->common_flags
5013 |= (CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
);
5015 else /* EQ (coding_type, Qraw_text) */
5017 coding
->detector
= NULL
;
5018 coding
->decoder
= decode_coding_raw_text
;
5019 coding
->encoder
= encode_coding_raw_text
;
5025 /* Return raw-text or one of its subsidiaries that has the same
5026 eol_type as CODING-SYSTEM. */
5029 raw_text_coding_system (coding_system
)
5030 Lisp_Object coding_system
;
5032 Lisp_Object spec
, attrs
;
5033 Lisp_Object eol_type
, raw_text_eol_type
;
5035 if (NILP (coding_system
))
5037 spec
= CODING_SYSTEM_SPEC (coding_system
);
5038 attrs
= AREF (spec
, 0);
5040 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
5041 return coding_system
;
5043 eol_type
= AREF (spec
, 2);
5044 if (VECTORP (eol_type
))
5046 spec
= CODING_SYSTEM_SPEC (Qraw_text
);
5047 raw_text_eol_type
= AREF (spec
, 2);
5048 return (EQ (eol_type
, Qunix
) ? AREF (raw_text_eol_type
, 0)
5049 : EQ (eol_type
, Qdos
) ? AREF (raw_text_eol_type
, 1)
5050 : AREF (raw_text_eol_type
, 2));
5054 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5055 does, return one of the subsidiary that has the same eol-spec as
5056 PARENT. Otherwise, return CODING_SYSTEM. */
5059 coding_inherit_eol_type (coding_system
, parent
)
5060 Lisp_Object coding_system
, parent
;
5062 Lisp_Object spec
, eol_type
;
5064 if (NILP (coding_system
))
5065 coding_system
= Qraw_text
;
5066 spec
= CODING_SYSTEM_SPEC (coding_system
);
5067 eol_type
= AREF (spec
, 2);
5068 if (VECTORP (eol_type
)
5071 Lisp_Object parent_spec
;
5072 Lisp_Object parent_eol_type
;
5075 = CODING_SYSTEM_SPEC (buffer_defaults
.buffer_file_coding_system
);
5076 parent_eol_type
= AREF (parent_spec
, 2);
5077 if (EQ (parent_eol_type
, Qunix
))
5078 coding_system
= AREF (eol_type
, 0);
5079 else if (EQ (parent_eol_type
, Qdos
))
5080 coding_system
= AREF (eol_type
, 1);
5081 else if (EQ (parent_eol_type
, Qmac
))
5082 coding_system
= AREF (eol_type
, 2);
5084 return coding_system
;
5087 /* Emacs has a mechanism to automatically detect a coding system if it
5088 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5089 it's impossible to distinguish some coding systems accurately
5090 because they use the same range of codes. So, at first, coding
5091 systems are categorized into 7, those are:
5093 o coding-category-emacs-mule
5095 The category for a coding system which has the same code range
5096 as Emacs' internal format. Assigned the coding-system (Lisp
5097 symbol) `emacs-mule' by default.
5099 o coding-category-sjis
5101 The category for a coding system which has the same code range
5102 as SJIS. Assigned the coding-system (Lisp
5103 symbol) `japanese-shift-jis' by default.
5105 o coding-category-iso-7
5107 The category for a coding system which has the same code range
5108 as ISO2022 of 7-bit environment. This doesn't use any locking
5109 shift and single shift functions. This can encode/decode all
5110 charsets. Assigned the coding-system (Lisp symbol)
5111 `iso-2022-7bit' by default.
5113 o coding-category-iso-7-tight
5115 Same as coding-category-iso-7 except that this can
5116 encode/decode only the specified charsets.
5118 o coding-category-iso-8-1
5120 The category for a coding system which has the same code range
5121 as ISO2022 of 8-bit environment and graphic plane 1 used only
5122 for DIMENSION1 charset. This doesn't use any locking shift
5123 and single shift functions. Assigned the coding-system (Lisp
5124 symbol) `iso-latin-1' by default.
5126 o coding-category-iso-8-2
5128 The category for a coding system which has the same code range
5129 as ISO2022 of 8-bit environment and graphic plane 1 used only
5130 for DIMENSION2 charset. This doesn't use any locking shift
5131 and single shift functions. Assigned the coding-system (Lisp
5132 symbol) `japanese-iso-8bit' by default.
5134 o coding-category-iso-7-else
5136 The category for a coding system which has the same code range
5137 as ISO2022 of 7-bit environemnt but uses locking shift or
5138 single shift functions. Assigned the coding-system (Lisp
5139 symbol) `iso-2022-7bit-lock' by default.
5141 o coding-category-iso-8-else
5143 The category for a coding system which has the same code range
5144 as ISO2022 of 8-bit environemnt but uses locking shift or
5145 single shift functions. Assigned the coding-system (Lisp
5146 symbol) `iso-2022-8bit-ss2' by default.
5148 o coding-category-big5
5150 The category for a coding system which has the same code range
5151 as BIG5. Assigned the coding-system (Lisp symbol)
5152 `cn-big5' by default.
5154 o coding-category-utf-8
5156 The category for a coding system which has the same code range
5157 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
5158 symbol) `utf-8' by default.
5160 o coding-category-utf-16-be
5162 The category for a coding system in which a text has an
5163 Unicode signature (cf. Unicode Standard) in the order of BIG
5164 endian at the head. Assigned the coding-system (Lisp symbol)
5165 `utf-16-be' by default.
5167 o coding-category-utf-16-le
5169 The category for a coding system in which a text has an
5170 Unicode signature (cf. Unicode Standard) in the order of
5171 LITTLE endian at the head. Assigned the coding-system (Lisp
5172 symbol) `utf-16-le' by default.
5174 o coding-category-ccl
5176 The category for a coding system of which encoder/decoder is
5177 written in CCL programs. The default value is nil, i.e., no
5178 coding system is assigned.
5180 o coding-category-binary
5182 The category for a coding system not categorized in any of the
5183 above. Assigned the coding-system (Lisp symbol)
5184 `no-conversion' by default.
5186 Each of them is a Lisp symbol and the value is an actual
5187 `coding-system's (this is also a Lisp symbol) assigned by a user.
5188 What Emacs does actually is to detect a category of coding system.
5189 Then, it uses a `coding-system' assigned to it. If Emacs can't
5190 decide only one possible category, it selects a category of the
5191 highest priority. Priorities of categories are also specified by a
5192 user in a Lisp variable `coding-category-list'.
5196 #define EOL_SEEN_NONE 0
5197 #define EOL_SEEN_LF 1
5198 #define EOL_SEEN_CR 2
5199 #define EOL_SEEN_CRLF 4
5201 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
5202 SOURCE is encoded. If CATEGORY is one of
5203 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5204 two-byte, else they are encoded by one-byte.
5206 Return one of EOL_SEEN_XXX. */
5208 #define MAX_EOL_CHECK_COUNT 3
5211 detect_eol (source
, src_bytes
, category
)
5212 unsigned char *source
;
5213 EMACS_INT src_bytes
;
5214 enum coding_category category
;
5216 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
5219 int eol_seen
= EOL_SEEN_NONE
;
5221 if ((1 << category
) & CATEGORY_MASK_UTF_16
)
5225 msb
= category
== (coding_category_utf_16_le
5226 | coding_category_utf_16_le_nosig
);
5229 while (src
+ 1 < src_end
)
5232 if (src
[msb
] == 0 && (c
== '\n' || c
== '\r'))
5237 this_eol
= EOL_SEEN_LF
;
5238 else if (src
+ 3 >= src_end
5239 || src
[msb
+ 2] != 0
5240 || src
[lsb
+ 2] != '\n')
5241 this_eol
= EOL_SEEN_CR
;
5243 this_eol
= EOL_SEEN_CRLF
;
5245 if (eol_seen
== EOL_SEEN_NONE
)
5246 /* This is the first end-of-line. */
5247 eol_seen
= this_eol
;
5248 else if (eol_seen
!= this_eol
)
5250 /* The found type is different from what found before. */
5251 eol_seen
= EOL_SEEN_LF
;
5254 if (++total
== MAX_EOL_CHECK_COUNT
)
5262 while (src
< src_end
)
5265 if (c
== '\n' || c
== '\r')
5270 this_eol
= EOL_SEEN_LF
;
5271 else if (src
>= src_end
|| *src
!= '\n')
5272 this_eol
= EOL_SEEN_CR
;
5274 this_eol
= EOL_SEEN_CRLF
, src
++;
5276 if (eol_seen
== EOL_SEEN_NONE
)
5277 /* This is the first end-of-line. */
5278 eol_seen
= this_eol
;
5279 else if (eol_seen
!= this_eol
)
5281 /* The found type is different from what found before. */
5282 eol_seen
= EOL_SEEN_LF
;
5285 if (++total
== MAX_EOL_CHECK_COUNT
)
5295 adjust_coding_eol_type (coding
, eol_seen
)
5296 struct coding_system
*coding
;
5299 Lisp_Object eol_type
;
5301 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5302 if (eol_seen
& EOL_SEEN_LF
)
5304 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 0));
5307 else if (eol_seen
& EOL_SEEN_CRLF
)
5309 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 1));
5312 else if (eol_seen
& EOL_SEEN_CR
)
5314 coding
->id
= CODING_SYSTEM_ID (AREF (eol_type
, 2));
5320 /* Detect how a text specified in CODING is encoded. If a coding
5321 system is detected, update fields of CODING by the detected coding
5325 detect_coding (coding
)
5326 struct coding_system
*coding
;
5328 const unsigned char *src
, *src_end
;
5329 Lisp_Object attrs
, coding_type
;
5331 coding
->consumed
= coding
->consumed_char
= 0;
5332 coding
->produced
= coding
->produced_char
= 0;
5333 coding_set_source (coding
);
5335 src_end
= coding
->source
+ coding
->src_bytes
;
5337 /* If we have not yet decided the text encoding type, detect it
5339 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding
->id
)), Qundecided
))
5343 for (i
= 0, src
= coding
->source
; src
< src_end
; i
++, src
++)
5346 if (c
& 0x80 || (c
< 0x20 && (c
== ISO_CODE_ESC
5348 || c
== ISO_CODE_SO
)))
5351 coding
->head_ascii
= src
- (coding
->source
+ coding
->consumed
);
5353 if (coding
->head_ascii
< coding
->src_bytes
)
5355 struct coding_detection_info detect_info
;
5356 enum coding_category category
;
5357 struct coding_system
*this;
5359 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
5360 for (i
= 0; i
< coding_category_raw_text
; i
++)
5362 category
= coding_priorities
[i
];
5363 this = coding_categories
+ category
;
5366 /* No coding system of this category is defined. */
5367 detect_info
.rejected
|= (1 << category
);
5369 else if (category
>= coding_category_raw_text
)
5371 else if (detect_info
.checked
& (1 << category
))
5373 if (detect_info
.found
& (1 << category
))
5376 else if ((*(this->detector
)) (coding
, &detect_info
)
5377 && detect_info
.found
& (1 << category
))
5379 if (category
== coding_category_utf_16_auto
)
5381 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5382 category
= coding_category_utf_16_le
;
5384 category
= coding_category_utf_16_be
;
5389 if (i
< coding_category_raw_text
)
5390 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5391 else if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
5392 setup_coding_system (Qraw_text
, coding
);
5393 else if (detect_info
.rejected
)
5394 for (i
= 0; i
< coding_category_raw_text
; i
++)
5395 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
5397 this = coding_categories
+ coding_priorities
[i
];
5398 setup_coding_system (CODING_ID_NAME (this->id
), coding
);
5403 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding
->id
)))
5404 == coding_category_utf_16_auto
)
5406 Lisp_Object coding_systems
;
5407 struct coding_detection_info detect_info
;
5410 = AREF (CODING_ID_ATTRS (coding
->id
), coding_attr_utf_16_bom
);
5411 detect_info
.found
= detect_info
.rejected
= 0;
5412 if (CONSP (coding_systems
)
5413 && detect_coding_utf_16 (coding
, &detect_info
))
5415 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
5416 setup_coding_system (XCAR (coding_systems
), coding
);
5417 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
5418 setup_coding_system (XCDR (coding_systems
), coding
);
5426 struct coding_system
*coding
;
5428 Lisp_Object eol_type
;
5429 unsigned char *p
, *pbeg
, *pend
;
5431 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5432 if (EQ (eol_type
, Qunix
))
5435 if (NILP (coding
->dst_object
))
5436 pbeg
= coding
->destination
;
5438 pbeg
= BYTE_POS_ADDR (coding
->dst_pos_byte
);
5439 pend
= pbeg
+ coding
->produced
;
5441 if (VECTORP (eol_type
))
5443 int eol_seen
= EOL_SEEN_NONE
;
5445 for (p
= pbeg
; p
< pend
; p
++)
5448 eol_seen
|= EOL_SEEN_LF
;
5449 else if (*p
== '\r')
5451 if (p
+ 1 < pend
&& *(p
+ 1) == '\n')
5453 eol_seen
|= EOL_SEEN_CRLF
;
5457 eol_seen
|= EOL_SEEN_CR
;
5460 if (eol_seen
!= EOL_SEEN_NONE
5461 && eol_seen
!= EOL_SEEN_LF
5462 && eol_seen
!= EOL_SEEN_CRLF
5463 && eol_seen
!= EOL_SEEN_CR
)
5464 eol_seen
= EOL_SEEN_LF
;
5465 if (eol_seen
!= EOL_SEEN_NONE
)
5466 eol_type
= adjust_coding_eol_type (coding
, eol_seen
);
5469 if (EQ (eol_type
, Qmac
))
5471 for (p
= pbeg
; p
< pend
; p
++)
5475 else if (EQ (eol_type
, Qdos
))
5479 if (NILP (coding
->dst_object
))
5481 for (p
= pend
- 2; p
>= pbeg
; p
--)
5484 safe_bcopy ((char *) (p
+ 1), (char *) p
, pend
-- - p
- 1);
5490 for (p
= pend
- 2; p
>= pbeg
; p
--)
5493 int pos_byte
= coding
->dst_pos_byte
+ (p
- pbeg
);
5494 int pos
= BYTE_TO_CHAR (pos_byte
);
5496 del_range_2 (pos
, pos_byte
, pos
+ 1, pos_byte
+ 1, 0);
5500 coding
->produced
-= n
;
5501 coding
->produced_char
-= n
;
5506 /* Return a translation table (or list of them) from coding system
5507 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5508 decoding (ENCODEP is zero). */
5511 get_translation_table (attrs
, encodep
, max_lookup
)
5513 int encodep
, *max_lookup
;
5515 Lisp_Object standard
, translation_table
;
5519 translation_table
= CODING_ATTR_ENCODE_TBL (attrs
),
5520 standard
= Vstandard_translation_table_for_encode
;
5522 translation_table
= CODING_ATTR_DECODE_TBL (attrs
),
5523 standard
= Vstandard_translation_table_for_decode
;
5524 if (NILP (translation_table
))
5525 translation_table
= standard
;
5528 if (SYMBOLP (translation_table
))
5529 translation_table
= Fget (translation_table
, Qtranslation_table
);
5530 else if (CONSP (translation_table
))
5532 translation_table
= Fcopy_sequence (translation_table
);
5533 for (val
= translation_table
; CONSP (val
); val
= XCDR (val
))
5534 if (SYMBOLP (XCAR (val
)))
5535 XSETCAR (val
, Fget (XCAR (val
), Qtranslation_table
));
5537 if (CHAR_TABLE_P (standard
))
5539 if (CONSP (translation_table
))
5540 translation_table
= nconc2 (translation_table
,
5541 Fcons (standard
, Qnil
));
5543 translation_table
= Fcons (translation_table
,
5544 Fcons (standard
, Qnil
));
5548 if (CHAR_TABLE_P (translation_table
)
5549 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table
)) > 1)
5551 val
= XCHAR_TABLE (translation_table
)->extras
[1];
5552 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5553 *max_lookup
= XFASTINT (val
);
5555 else if (CONSP (translation_table
))
5557 Lisp_Object tail
, val
;
5559 for (tail
= translation_table
; CONSP (tail
); tail
= XCDR (tail
))
5560 if (CHAR_TABLE_P (XCAR (tail
))
5561 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail
))) > 1)
5563 val
= XCHAR_TABLE (XCAR (tail
))->extras
[1];
5564 if (NATNUMP (val
) && *max_lookup
< XFASTINT (val
))
5565 *max_lookup
= XFASTINT (val
);
5568 return translation_table
;
5571 #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5574 if (CHAR_TABLE_P (table)) \
5576 trans = CHAR_TABLE_REF (table, c); \
5577 if (CHARACTERP (trans)) \
5578 c = XFASTINT (trans), trans = Qnil; \
5580 else if (CONSP (table)) \
5584 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5585 if (CHAR_TABLE_P (XCAR (tail))) \
5587 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5588 if (CHARACTERP (trans)) \
5589 c = XFASTINT (trans), trans = Qnil; \
5590 else if (! NILP (trans)) \
5598 get_translation (val
, buf
, buf_end
, last_block
, from_nchars
, to_nchars
)
5602 int *from_nchars
, *to_nchars
;
5604 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5608 Lisp_Object from
, tail
;
5611 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
5616 for (i
= 0; i
< len
; i
++)
5618 if (buf
+ i
== buf_end
)
5624 if (XINT (AREF (from
, i
)) != buf
[i
])
5638 *buf
= XINT (AREF (val
, 0)), *to_nchars
= ASIZE (val
);
5646 produce_chars (coding
, translation_table
, last_block
)
5647 struct coding_system
*coding
;
5648 Lisp_Object translation_table
;
5651 unsigned char *dst
= coding
->destination
+ coding
->produced
;
5652 unsigned char *dst_end
= coding
->destination
+ coding
->dst_bytes
;
5654 int produced_chars
= 0;
5657 if (! coding
->chars_at_source
)
5659 /* Characters are in coding->charbuf. */
5660 int *buf
= coding
->charbuf
;
5661 int *buf_end
= buf
+ coding
->charbuf_used
;
5663 if (BUFFERP (coding
->src_object
)
5664 && EQ (coding
->src_object
, coding
->dst_object
))
5665 dst_end
= ((unsigned char *) coding
->source
) + coding
->consumed
;
5667 while (buf
< buf_end
)
5673 int from_nchars
= 1, to_nchars
= 1;
5674 Lisp_Object trans
= Qnil
;
5676 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
5679 trans
= get_translation (trans
, buf
, buf_end
, last_block
,
5680 &from_nchars
, &to_nchars
);
5686 if (dst
+ MAX_MULTIBYTE_LENGTH
* to_nchars
> dst_end
)
5688 dst
= alloc_destination (coding
,
5690 + MAX_MULTIBYTE_LENGTH
* to_nchars
,
5692 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5695 for (i
= 0; i
< to_nchars
; i
++)
5698 c
= XINT (AREF (trans
, i
));
5699 if (coding
->dst_multibyte
5700 || ! CHAR_BYTE8_P (c
))
5701 CHAR_STRING_ADVANCE (c
, dst
);
5703 *dst
++ = CHAR_TO_BYTE8 (c
);
5705 produced_chars
+= to_nchars
;
5707 while (--from_nchars
> 0)
5711 /* This is an annotation datum. (-C) is the length. */
5714 carryover
= buf_end
- buf
;
5718 const unsigned char *src
= coding
->source
;
5719 const unsigned char *src_end
= src
+ coding
->src_bytes
;
5720 Lisp_Object eol_type
;
5722 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
5724 if (coding
->src_multibyte
!= coding
->dst_multibyte
)
5726 if (coding
->src_multibyte
)
5733 const unsigned char *src_base
= src
;
5739 if (EQ (eol_type
, Qdos
))
5743 record_conversion_result
5744 (coding
, CODING_RESULT_INSUFFICIENT_SRC
);
5745 goto no_more_source
;
5750 else if (EQ (eol_type
, Qmac
))
5755 coding
->consumed
= src
- coding
->source
;
5757 if (EQ (coding
->src_object
, coding
->dst_object
))
5758 dst_end
= (unsigned char *) src
;
5761 dst
= alloc_destination (coding
, src_end
- src
+ 1,
5763 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5764 coding_set_source (coding
);
5765 src
= coding
->source
+ coding
->consumed
;
5766 src_end
= coding
->source
+ coding
->src_bytes
;
5776 while (src
< src_end
)
5783 if (EQ (eol_type
, Qdos
))
5789 else if (EQ (eol_type
, Qmac
))
5792 if (dst
>= dst_end
- 1)
5794 coding
->consumed
= src
- coding
->source
;
5796 if (EQ (coding
->src_object
, coding
->dst_object
))
5797 dst_end
= (unsigned char *) src
;
5798 if (dst
>= dst_end
- 1)
5800 dst
= alloc_destination (coding
, src_end
- src
+ 2,
5802 dst_end
= coding
->destination
+ coding
->dst_bytes
;
5803 coding_set_source (coding
);
5804 src
= coding
->source
+ coding
->consumed
;
5805 src_end
= coding
->source
+ coding
->src_bytes
;
5813 if (!EQ (coding
->src_object
, coding
->dst_object
))
5815 int require
= coding
->src_bytes
- coding
->dst_bytes
;
5819 EMACS_INT offset
= src
- coding
->source
;
5821 dst
= alloc_destination (coding
, require
, dst
);
5822 coding_set_source (coding
);
5823 src
= coding
->source
+ offset
;
5824 src_end
= coding
->source
+ coding
->src_bytes
;
5827 produced_chars
= coding
->src_chars
;
5828 while (src
< src_end
)
5834 if (EQ (eol_type
, Qdos
))
5841 else if (EQ (eol_type
, Qmac
))
5847 coding
->consumed
= coding
->src_bytes
;
5848 coding
->consumed_char
= coding
->src_chars
;
5851 produced
= dst
- (coding
->destination
+ coding
->produced
);
5852 if (BUFFERP (coding
->dst_object
))
5853 insert_from_gap (produced_chars
, produced
);
5854 coding
->produced
+= produced
;
5855 coding
->produced_char
+= produced_chars
;
5859 /* Compose text in CODING->object according to the annotation data at
5860 CHARBUF. CHARBUF is an array:
5861 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
5865 produce_composition (coding
, charbuf
, pos
)
5866 struct coding_system
*coding
;
5872 enum composition_method method
;
5873 Lisp_Object components
;
5876 to
= pos
+ charbuf
[2];
5877 method
= (enum composition_method
) (charbuf
[3]);
5879 if (method
== COMPOSITION_RELATIVE
)
5883 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
5888 for (i
= 0; i
< len
; i
++)
5889 args
[i
] = make_number (charbuf
[i
]);
5890 components
= (method
== COMPOSITION_WITH_ALTCHARS
5891 ? Fstring (len
, args
) : Fvector (len
, args
));
5893 compose_text (pos
, to
, components
, Qnil
, coding
->dst_object
);
5897 /* Put `charset' property on text in CODING->object according to
5898 the annotation data at CHARBUF. CHARBUF is an array:
5899 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
5903 produce_charset (coding
, charbuf
, pos
)
5904 struct coding_system
*coding
;
5908 EMACS_INT from
= pos
- charbuf
[2];
5909 struct charset
*charset
= CHARSET_FROM_ID (charbuf
[3]);
5911 Fput_text_property (make_number (from
), make_number (pos
),
5912 Qcharset
, CHARSET_NAME (charset
),
5913 coding
->dst_object
);
5917 #define CHARBUF_SIZE 0x4000
5919 #define ALLOC_CONVERSION_WORK_AREA(coding) \
5921 int size = CHARBUF_SIZE;; \
5923 coding->charbuf = NULL; \
5924 while (size > 1024) \
5926 coding->charbuf = (int *) alloca (sizeof (int) * size); \
5927 if (coding->charbuf) \
5931 if (! coding->charbuf) \
5933 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
5934 return coding->result; \
5936 coding->charbuf_size = size; \
5941 produce_annotation (coding
, pos
)
5942 struct coding_system
*coding
;
5945 int *charbuf
= coding
->charbuf
;
5946 int *charbuf_end
= charbuf
+ coding
->charbuf_used
;
5948 if (NILP (coding
->dst_object
))
5951 while (charbuf
< charbuf_end
)
5957 int len
= -*charbuf
;
5960 case CODING_ANNOTATE_COMPOSITION_MASK
:
5961 produce_composition (coding
, charbuf
, pos
);
5963 case CODING_ANNOTATE_CHARSET_MASK
:
5964 produce_charset (coding
, charbuf
, pos
);
5974 /* Decode the data at CODING->src_object into CODING->dst_object.
5975 CODING->src_object is a buffer, a string, or nil.
5976 CODING->dst_object is a buffer.
5978 If CODING->src_object is a buffer, it must be the current buffer.
5979 In this case, if CODING->src_pos is positive, it is a position of
5980 the source text in the buffer, otherwise, the source text is in the
5981 gap area of the buffer, and CODING->src_pos specifies the offset of
5982 the text from GPT (which must be the same as PT). If this is the
5983 same buffer as CODING->dst_object, CODING->src_pos must be
5986 If CODING->src_object is a string, CODING->src_pos in an index to
5989 If CODING->src_object is nil, CODING->source must already point to
5990 the non-relocatable memory area. In this case, CODING->src_pos is
5991 an offset from CODING->source.
5993 The decoded data is inserted at the current point of the buffer
5998 decode_coding (coding
)
5999 struct coding_system
*coding
;
6002 Lisp_Object undo_list
;
6003 Lisp_Object translation_table
;
6008 if (BUFFERP (coding
->src_object
)
6009 && coding
->src_pos
> 0
6010 && coding
->src_pos
< GPT
6011 && coding
->src_pos
+ coding
->src_chars
> GPT
)
6012 move_gap_both (coding
->src_pos
, coding
->src_pos_byte
);
6015 if (BUFFERP (coding
->dst_object
))
6017 if (current_buffer
!= XBUFFER (coding
->dst_object
))
6018 set_buffer_internal (XBUFFER (coding
->dst_object
));
6020 move_gap_both (PT
, PT_BYTE
);
6021 undo_list
= current_buffer
->undo_list
;
6022 current_buffer
->undo_list
= Qt
;
6025 coding
->consumed
= coding
->consumed_char
= 0;
6026 coding
->produced
= coding
->produced_char
= 0;
6027 coding
->chars_at_source
= 0;
6028 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6031 ALLOC_CONVERSION_WORK_AREA (coding
);
6033 attrs
= CODING_ID_ATTRS (coding
->id
);
6034 translation_table
= get_translation_table (attrs
, 0, &max_lookup
);
6039 EMACS_INT pos
= coding
->dst_pos
+ coding
->produced_char
;
6041 coding_set_source (coding
);
6042 coding
->annotated
= 0;
6043 coding
->charbuf_used
= carryover
;
6044 (*(coding
->decoder
)) (coding
);
6045 coding_set_destination (coding
);
6046 carryover
= produce_chars (coding
, translation_table
, 0);
6047 if (coding
->annotated
)
6048 produce_annotation (coding
, pos
);
6049 for (i
= 0; i
< carryover
; i
++)
6051 = coding
->charbuf
[coding
->charbuf_used
- carryover
+ i
];
6053 while (coding
->consumed
< coding
->src_bytes
6054 && ! coding
->result
);
6058 coding_set_destination (coding
);
6059 coding
->charbuf_used
= carryover
;
6060 produce_chars (coding
, translation_table
, 1);
6063 coding
->carryover_bytes
= 0;
6064 if (coding
->consumed
< coding
->src_bytes
)
6066 int nbytes
= coding
->src_bytes
- coding
->consumed
;
6067 const unsigned char *src
;
6069 coding_set_source (coding
);
6070 coding_set_destination (coding
);
6071 src
= coding
->source
+ coding
->consumed
;
6073 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
6075 /* Flush out unprocessed data as binary chars. We are sure
6076 that the number of data is less than the size of
6078 coding
->charbuf_used
= 0;
6079 while (nbytes
-- > 0)
6083 coding
->charbuf
[coding
->charbuf_used
++] = (c
& 0x80 ? - c
: c
);
6085 produce_chars (coding
);
6089 /* Record unprocessed bytes in coding->carryover. We are
6090 sure that the number of data is less than the size of
6091 coding->carryover. */
6092 unsigned char *p
= coding
->carryover
;
6094 coding
->carryover_bytes
= nbytes
;
6095 while (nbytes
-- > 0)
6098 coding
->consumed
= coding
->src_bytes
;
6101 if (BUFFERP (coding
->dst_object
))
6103 current_buffer
->undo_list
= undo_list
;
6104 record_insert (coding
->dst_pos
, coding
->produced_char
);
6106 if (! EQ (CODING_ID_EOL_TYPE (coding
->id
), Qunix
))
6107 decode_eol (coding
);
6108 return coding
->result
;
6112 /* Extract an annotation datum from a composition starting at POS and
6113 ending before LIMIT of CODING->src_object (buffer or string), store
6114 the data in BUF, set *STOP to a starting position of the next
6115 composition (if any) or to LIMIT, and return the address of the
6116 next element of BUF.
6118 If such an annotation is not found, set *STOP to a starting
6119 position of a composition after POS (if any) or to LIMIT, and
6123 handle_composition_annotation (pos
, limit
, coding
, buf
, stop
)
6124 EMACS_INT pos
, limit
;
6125 struct coding_system
*coding
;
6129 EMACS_INT start
, end
;
6132 if (! find_composition (pos
, limit
, &start
, &end
, &prop
, coding
->src_object
)
6135 else if (start
> pos
)
6141 /* We found a composition. Store the corresponding
6142 annotation data in BUF. */
6144 enum composition_method method
= COMPOSITION_METHOD (prop
);
6145 int nchars
= COMPOSITION_LENGTH (prop
);
6147 ADD_COMPOSITION_DATA (buf
, nchars
, method
);
6148 if (method
!= COMPOSITION_RELATIVE
)
6150 Lisp_Object components
;
6153 components
= COMPOSITION_COMPONENTS (prop
);
6154 if (VECTORP (components
))
6156 len
= XVECTOR (components
)->size
;
6157 for (i
= 0; i
< len
; i
++)
6158 *buf
++ = XINT (AREF (components
, i
));
6160 else if (STRINGP (components
))
6162 len
= SCHARS (components
);
6166 FETCH_STRING_CHAR_ADVANCE (*buf
, components
, i
, i_byte
);
6170 else if (INTEGERP (components
))
6173 *buf
++ = XINT (components
);
6175 else if (CONSP (components
))
6177 for (len
= 0; CONSP (components
);
6178 len
++, components
= XCDR (components
))
6179 *buf
++ = XINT (XCAR (components
));
6187 if (find_composition (end
, limit
, &start
, &end
, &prop
,
6198 /* Extract an annotation datum from a text property `charset' at POS of
6199 CODING->src_object (buffer of string), store the data in BUF, set
6200 *STOP to the position where the value of `charset' property changes
6201 (limiting by LIMIT), and return the address of the next element of
6204 If the property value is nil, set *STOP to the position where the
6205 property value is non-nil (limiting by LIMIT), and return BUF. */
6208 handle_charset_annotation (pos
, limit
, coding
, buf
, stop
)
6209 EMACS_INT pos
, limit
;
6210 struct coding_system
*coding
;
6214 Lisp_Object val
, next
;
6217 val
= Fget_text_property (make_number (pos
), Qcharset
, coding
->src_object
);
6218 if (! NILP (val
) && CHARSETP (val
))
6219 id
= XINT (CHARSET_SYMBOL_ID (val
));
6222 ADD_CHARSET_DATA (buf
, 0, id
);
6223 next
= Fnext_single_property_change (make_number (pos
), Qcharset
,
6225 make_number (limit
));
6226 *stop
= XINT (next
);
6232 consume_chars (coding
, translation_table
, max_lookup
)
6233 struct coding_system
*coding
;
6234 Lisp_Object translation_table
;
6237 int *buf
= coding
->charbuf
;
6238 int *buf_end
= coding
->charbuf
+ coding
->charbuf_size
;
6239 const unsigned char *src
= coding
->source
+ coding
->consumed
;
6240 const unsigned char *src_end
= coding
->source
+ coding
->src_bytes
;
6241 EMACS_INT pos
= coding
->src_pos
+ coding
->consumed_char
;
6242 EMACS_INT end_pos
= coding
->src_pos
+ coding
->src_chars
;
6243 int multibytep
= coding
->src_multibyte
;
6244 Lisp_Object eol_type
;
6246 EMACS_INT stop
, stop_composition
, stop_charset
;
6247 int *lookup_buf
= NULL
;
6249 if (! NILP (translation_table
))
6250 lookup_buf
= alloca (sizeof (int) * max_lookup
);
6252 eol_type
= CODING_ID_EOL_TYPE (coding
->id
);
6253 if (VECTORP (eol_type
))
6256 /* Note: composition handling is not yet implemented. */
6257 coding
->common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
6259 if (NILP (coding
->src_object
))
6260 stop
= stop_composition
= stop_charset
= end_pos
;
6263 if (coding
->common_flags
& CODING_ANNOTATE_COMPOSITION_MASK
)
6264 stop
= stop_composition
= pos
;
6266 stop
= stop_composition
= end_pos
;
6267 if (coding
->common_flags
& CODING_ANNOTATE_CHARSET_MASK
)
6268 stop
= stop_charset
= pos
;
6270 stop_charset
= end_pos
;
6273 /* Compensate for CRLF and conversion. */
6274 buf_end
-= 1 + MAX_ANNOTATION_LENGTH
;
6275 while (buf
< buf_end
)
6283 if (pos
== stop_composition
)
6284 buf
= handle_composition_annotation (pos
, end_pos
, coding
,
6285 buf
, &stop_composition
);
6286 if (pos
== stop_charset
)
6287 buf
= handle_charset_annotation (pos
, end_pos
, coding
,
6288 buf
, &stop_charset
);
6289 stop
= (stop_composition
< stop_charset
6290 ? stop_composition
: stop_charset
);
6297 if (! CODING_FOR_UNIBYTE (coding
)
6298 && (bytes
= MULTIBYTE_LENGTH (src
, src_end
)) > 0)
6299 c
= STRING_CHAR_ADVANCE (src
), pos
+= bytes
;
6304 c
= STRING_CHAR_ADVANCE (src
), pos
++;
6305 if ((c
== '\r') && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
6307 if (! EQ (eol_type
, Qunix
))
6311 if (EQ (eol_type
, Qdos
))
6319 LOOKUP_TRANSLATION_TABLE (translation_table
, c
, trans
);
6324 int from_nchars
= 1, to_nchars
= 1;
6325 int *lookup_buf_end
;
6326 const unsigned char *p
= src
;
6330 for (i
= 1; i
< max_lookup
&& p
< src_end
; i
++)
6331 lookup_buf
[i
] = STRING_CHAR_ADVANCE (p
);
6332 lookup_buf_end
= lookup_buf
+ i
;
6333 trans
= get_translation (trans
, lookup_buf
, lookup_buf_end
, 1,
6334 &from_nchars
, &to_nchars
);
6336 || buf
+ to_nchars
> buf_end
)
6338 *buf
++ = *lookup_buf
;
6339 for (i
= 1; i
< to_nchars
; i
++)
6340 *buf
++ = XINT (AREF (trans
, i
));
6341 for (i
= 1; i
< from_nchars
; i
++, pos
++)
6342 src
+= MULTIBYTE_LENGTH_NO_CHECK (src
);
6346 coding
->consumed
= src
- coding
->source
;
6347 coding
->consumed_char
= pos
- coding
->src_pos
;
6348 coding
->charbuf_used
= buf
- coding
->charbuf
;
6349 coding
->chars_at_source
= 0;
6353 /* Encode the text at CODING->src_object into CODING->dst_object.
6354 CODING->src_object is a buffer or a string.
6355 CODING->dst_object is a buffer or nil.
6357 If CODING->src_object is a buffer, it must be the current buffer.
6358 In this case, if CODING->src_pos is positive, it is a position of
6359 the source text in the buffer, otherwise. the source text is in the
6360 gap area of the buffer, and coding->src_pos specifies the offset of
6361 the text from GPT (which must be the same as PT). If this is the
6362 same buffer as CODING->dst_object, CODING->src_pos must be
6363 negative and CODING should not have `pre-write-conversion'.
6365 If CODING->src_object is a string, CODING should not have
6366 `pre-write-conversion'.
6368 If CODING->dst_object is a buffer, the encoded data is inserted at
6369 the current point of that buffer.
6371 If CODING->dst_object is nil, the encoded data is placed at the
6372 memory area specified by CODING->destination. */
6375 encode_coding (coding
)
6376 struct coding_system
*coding
;
6379 Lisp_Object translation_table
;
6382 attrs
= CODING_ID_ATTRS (coding
->id
);
6383 translation_table
= get_translation_table (attrs
, 1, &max_lookup
);
6385 if (BUFFERP (coding
->dst_object
))
6387 set_buffer_internal (XBUFFER (coding
->dst_object
));
6388 coding
->dst_multibyte
6389 = ! NILP (current_buffer
->enable_multibyte_characters
);
6392 coding
->consumed
= coding
->consumed_char
= 0;
6393 coding
->produced
= coding
->produced_char
= 0;
6394 record_conversion_result (coding
, CODING_RESULT_SUCCESS
);
6397 ALLOC_CONVERSION_WORK_AREA (coding
);
6400 coding_set_source (coding
);
6401 consume_chars (coding
, translation_table
, max_lookup
);
6402 coding_set_destination (coding
);
6403 (*(coding
->encoder
)) (coding
);
6404 } while (coding
->consumed_char
< coding
->src_chars
);
6406 if (BUFFERP (coding
->dst_object
))
6407 insert_from_gap (coding
->produced_char
, coding
->produced
);
6409 return (coding
->result
);
6413 /* Name (or base name) of work buffer for code conversion. */
6414 static Lisp_Object Vcode_conversion_workbuf_name
;
6416 /* A working buffer used by the top level conversion. Once it is
6417 created, it is never destroyed. It has the name
6418 Vcode_conversion_workbuf_name. The other working buffers are
6419 destroyed after the use is finished, and their names are modified
6420 versions of Vcode_conversion_workbuf_name. */
6421 static Lisp_Object Vcode_conversion_reused_workbuf
;
6423 /* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6424 static int reused_workbuf_in_use
;
6427 /* Return a working buffer of code convesion. MULTIBYTE specifies the
6428 multibyteness of returning buffer. */
6431 make_conversion_work_buffer (multibyte
)
6433 Lisp_Object name
, workbuf
;
6434 struct buffer
*current
;
6436 if (reused_workbuf_in_use
++)
6438 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6439 workbuf
= Fget_buffer_create (name
);
6443 name
= Vcode_conversion_workbuf_name
;
6444 workbuf
= Fget_buffer_create (name
);
6445 if (NILP (Vcode_conversion_reused_workbuf
))
6446 Vcode_conversion_reused_workbuf
= workbuf
;
6448 current
= current_buffer
;
6449 set_buffer_internal (XBUFFER (workbuf
));
6451 current_buffer
->undo_list
= Qt
;
6452 current_buffer
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6453 set_buffer_internal (current
);
6459 code_conversion_restore (arg
)
6462 Lisp_Object current
, workbuf
;
6464 current
= XCAR (arg
);
6465 workbuf
= XCDR (arg
);
6466 if (! NILP (workbuf
))
6468 if (EQ (workbuf
, Vcode_conversion_reused_workbuf
))
6469 reused_workbuf_in_use
= 0;
6470 else if (! NILP (Fbuffer_live_p (workbuf
)))
6471 Fkill_buffer (workbuf
);
6473 set_buffer_internal (XBUFFER (current
));
6478 code_conversion_save (with_work_buf
, multibyte
)
6479 int with_work_buf
, multibyte
;
6481 Lisp_Object workbuf
= Qnil
;
6484 workbuf
= make_conversion_work_buffer (multibyte
);
6485 record_unwind_protect (code_conversion_restore
,
6486 Fcons (Fcurrent_buffer (), workbuf
));
6491 decode_coding_gap (coding
, chars
, bytes
)
6492 struct coding_system
*coding
;
6493 EMACS_INT chars
, bytes
;
6495 int count
= specpdl_ptr
- specpdl
;
6498 code_conversion_save (0, 0);
6500 coding
->src_object
= Fcurrent_buffer ();
6501 coding
->src_chars
= chars
;
6502 coding
->src_bytes
= bytes
;
6503 coding
->src_pos
= -chars
;
6504 coding
->src_pos_byte
= -bytes
;
6505 coding
->src_multibyte
= chars
< bytes
;
6506 coding
->dst_object
= coding
->src_object
;
6507 coding
->dst_pos
= PT
;
6508 coding
->dst_pos_byte
= PT_BYTE
;
6509 coding
->dst_multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
6510 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
6512 if (CODING_REQUIRE_DETECTION (coding
))
6513 detect_coding (coding
);
6515 decode_coding (coding
);
6517 attrs
= CODING_ID_ATTRS (coding
->id
);
6518 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6520 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6523 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6524 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6525 make_number (coding
->produced_char
));
6527 coding
->produced_char
+= Z
- prev_Z
;
6528 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6531 unbind_to (count
, Qnil
);
6532 return coding
->result
;
6536 encode_coding_gap (coding
, chars
, bytes
)
6537 struct coding_system
*coding
;
6538 EMACS_INT chars
, bytes
;
6540 int count
= specpdl_ptr
- specpdl
;
6542 code_conversion_save (0, 0);
6544 coding
->src_object
= Fcurrent_buffer ();
6545 coding
->src_chars
= chars
;
6546 coding
->src_bytes
= bytes
;
6547 coding
->src_pos
= -chars
;
6548 coding
->src_pos_byte
= -bytes
;
6549 coding
->src_multibyte
= chars
< bytes
;
6550 coding
->dst_object
= coding
->src_object
;
6551 coding
->dst_pos
= PT
;
6552 coding
->dst_pos_byte
= PT_BYTE
;
6554 encode_coding (coding
);
6556 unbind_to (count
, Qnil
);
6557 return coding
->result
;
6561 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6562 SRC_OBJECT into DST_OBJECT by coding context CODING.
6564 SRC_OBJECT is a buffer, a string, or Qnil.
6566 If it is a buffer, the text is at point of the buffer. FROM and TO
6567 are positions in the buffer.
6569 If it is a string, the text is at the beginning of the string.
6570 FROM and TO are indices to the string.
6572 If it is nil, the text is at coding->source. FROM and TO are
6573 indices to coding->source.
6575 DST_OBJECT is a buffer, Qt, or Qnil.
6577 If it is a buffer, the decoded text is inserted at point of the
6578 buffer. If the buffer is the same as SRC_OBJECT, the source text
6581 If it is Qt, a string is made from the decoded text, and
6582 set in CODING->dst_object.
6584 If it is Qnil, the decoded text is stored at CODING->destination.
6585 The caller must allocate CODING->dst_bytes bytes at
6586 CODING->destination by xmalloc. If the decoded text is longer than
6587 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6591 decode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6593 struct coding_system
*coding
;
6594 Lisp_Object src_object
;
6595 EMACS_INT from
, from_byte
, to
, to_byte
;
6596 Lisp_Object dst_object
;
6598 int count
= specpdl_ptr
- specpdl
;
6599 unsigned char *destination
;
6600 EMACS_INT dst_bytes
;
6601 EMACS_INT chars
= to
- from
;
6602 EMACS_INT bytes
= to_byte
- from_byte
;
6605 int saved_pt
= -1, saved_pt_byte
;
6607 buffer
= Fcurrent_buffer ();
6609 if (NILP (dst_object
))
6611 destination
= coding
->destination
;
6612 dst_bytes
= coding
->dst_bytes
;
6615 coding
->src_object
= src_object
;
6616 coding
->src_chars
= chars
;
6617 coding
->src_bytes
= bytes
;
6618 coding
->src_multibyte
= chars
< bytes
;
6620 if (STRINGP (src_object
))
6622 coding
->src_pos
= from
;
6623 coding
->src_pos_byte
= from_byte
;
6625 else if (BUFFERP (src_object
))
6627 set_buffer_internal (XBUFFER (src_object
));
6629 move_gap_both (from
, from_byte
);
6630 if (EQ (src_object
, dst_object
))
6632 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6633 TEMP_SET_PT_BOTH (from
, from_byte
);
6634 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6635 coding
->src_pos
= -chars
;
6636 coding
->src_pos_byte
= -bytes
;
6640 coding
->src_pos
= from
;
6641 coding
->src_pos_byte
= from_byte
;
6645 if (CODING_REQUIRE_DETECTION (coding
))
6646 detect_coding (coding
);
6647 attrs
= CODING_ID_ATTRS (coding
->id
);
6649 if (EQ (dst_object
, Qt
)
6650 || (! NILP (CODING_ATTR_POST_READ (attrs
))
6651 && NILP (dst_object
)))
6653 coding
->dst_object
= code_conversion_save (1, 1);
6654 coding
->dst_pos
= BEG
;
6655 coding
->dst_pos_byte
= BEG_BYTE
;
6656 coding
->dst_multibyte
= 1;
6658 else if (BUFFERP (dst_object
))
6660 code_conversion_save (0, 0);
6661 coding
->dst_object
= dst_object
;
6662 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6663 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6664 coding
->dst_multibyte
6665 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6669 code_conversion_save (0, 0);
6670 coding
->dst_object
= Qnil
;
6671 coding
->dst_multibyte
= 1;
6674 decode_coding (coding
);
6676 if (BUFFERP (coding
->dst_object
))
6677 set_buffer_internal (XBUFFER (coding
->dst_object
));
6679 if (! NILP (CODING_ATTR_POST_READ (attrs
)))
6681 struct gcpro gcpro1
, gcpro2
;
6682 EMACS_INT prev_Z
= Z
, prev_Z_BYTE
= Z_BYTE
;
6685 TEMP_SET_PT_BOTH (coding
->dst_pos
, coding
->dst_pos_byte
);
6686 GCPRO2 (coding
->src_object
, coding
->dst_object
);
6687 val
= call1 (CODING_ATTR_POST_READ (attrs
),
6688 make_number (coding
->produced_char
));
6691 coding
->produced_char
+= Z
- prev_Z
;
6692 coding
->produced
+= Z_BYTE
- prev_Z_BYTE
;
6695 if (EQ (dst_object
, Qt
))
6697 coding
->dst_object
= Fbuffer_string ();
6699 else if (NILP (dst_object
) && BUFFERP (coding
->dst_object
))
6701 set_buffer_internal (XBUFFER (coding
->dst_object
));
6702 if (dst_bytes
< coding
->produced
)
6705 = (unsigned char *) xrealloc (destination
, coding
->produced
);
6708 record_conversion_result (coding
,
6709 CODING_RESULT_INSUFFICIENT_DST
);
6710 unbind_to (count
, Qnil
);
6713 if (BEGV
< GPT
&& GPT
< BEGV
+ coding
->produced_char
)
6714 move_gap_both (BEGV
, BEGV_BYTE
);
6715 bcopy (BEGV_ADDR
, destination
, coding
->produced
);
6716 coding
->destination
= destination
;
6722 /* This is the case of:
6723 (BUFFERP (src_object) && EQ (src_object, dst_object))
6724 As we have moved PT while replacing the original buffer
6725 contents, we must recover it now. */
6726 set_buffer_internal (XBUFFER (src_object
));
6727 if (saved_pt
< from
)
6728 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
6729 else if (saved_pt
< from
+ chars
)
6730 TEMP_SET_PT_BOTH (from
, from_byte
);
6731 else if (! NILP (current_buffer
->enable_multibyte_characters
))
6732 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
6733 saved_pt_byte
+ (coding
->produced
- bytes
));
6735 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
6736 saved_pt_byte
+ (coding
->produced
- bytes
));
6739 unbind_to (count
, coding
->dst_object
);
6744 encode_coding_object (coding
, src_object
, from
, from_byte
, to
, to_byte
,
6746 struct coding_system
*coding
;
6747 Lisp_Object src_object
;
6748 EMACS_INT from
, from_byte
, to
, to_byte
;
6749 Lisp_Object dst_object
;
6751 int count
= specpdl_ptr
- specpdl
;
6752 EMACS_INT chars
= to
- from
;
6753 EMACS_INT bytes
= to_byte
- from_byte
;
6756 int saved_pt
= -1, saved_pt_byte
;
6758 buffer
= Fcurrent_buffer ();
6760 coding
->src_object
= src_object
;
6761 coding
->src_chars
= chars
;
6762 coding
->src_bytes
= bytes
;
6763 coding
->src_multibyte
= chars
< bytes
;
6765 attrs
= CODING_ID_ATTRS (coding
->id
);
6767 if (! NILP (CODING_ATTR_PRE_WRITE (attrs
)))
6769 coding
->src_object
= code_conversion_save (1, coding
->src_multibyte
);
6770 set_buffer_internal (XBUFFER (coding
->src_object
));
6771 if (STRINGP (src_object
))
6772 insert_from_string (src_object
, from
, from_byte
, chars
, bytes
, 0);
6773 else if (BUFFERP (src_object
))
6774 insert_from_buffer (XBUFFER (src_object
), from
, chars
, 0);
6776 insert_1_both (coding
->source
+ from
, chars
, bytes
, 0, 0, 0);
6778 if (EQ (src_object
, dst_object
))
6780 set_buffer_internal (XBUFFER (src_object
));
6781 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6782 del_range_both (from
, from_byte
, to
, to_byte
, 1);
6783 set_buffer_internal (XBUFFER (coding
->src_object
));
6786 call2 (CODING_ATTR_PRE_WRITE (attrs
),
6787 make_number (BEG
), make_number (Z
));
6788 coding
->src_object
= Fcurrent_buffer ();
6790 move_gap_both (BEG
, BEG_BYTE
);
6791 coding
->src_chars
= Z
- BEG
;
6792 coding
->src_bytes
= Z_BYTE
- BEG_BYTE
;
6793 coding
->src_pos
= BEG
;
6794 coding
->src_pos_byte
= BEG_BYTE
;
6795 coding
->src_multibyte
= Z
< Z_BYTE
;
6797 else if (STRINGP (src_object
))
6799 code_conversion_save (0, 0);
6800 coding
->src_pos
= from
;
6801 coding
->src_pos_byte
= from_byte
;
6803 else if (BUFFERP (src_object
))
6805 code_conversion_save (0, 0);
6806 set_buffer_internal (XBUFFER (src_object
));
6807 if (EQ (src_object
, dst_object
))
6809 saved_pt
= PT
, saved_pt_byte
= PT_BYTE
;
6810 coding
->src_object
= del_range_1 (from
, to
, 1, 1);
6811 coding
->src_pos
= 0;
6812 coding
->src_pos_byte
= 0;
6816 if (from
< GPT
&& to
>= GPT
)
6817 move_gap_both (from
, from_byte
);
6818 coding
->src_pos
= from
;
6819 coding
->src_pos_byte
= from_byte
;
6823 code_conversion_save (0, 0);
6825 if (BUFFERP (dst_object
))
6827 coding
->dst_object
= dst_object
;
6828 if (EQ (src_object
, dst_object
))
6830 coding
->dst_pos
= from
;
6831 coding
->dst_pos_byte
= from_byte
;
6835 coding
->dst_pos
= BUF_PT (XBUFFER (dst_object
));
6836 coding
->dst_pos_byte
= BUF_PT_BYTE (XBUFFER (dst_object
));
6838 coding
->dst_multibyte
6839 = ! NILP (XBUFFER (dst_object
)->enable_multibyte_characters
);
6841 else if (EQ (dst_object
, Qt
))
6843 coding
->dst_object
= Qnil
;
6844 coding
->dst_bytes
= coding
->src_chars
;
6845 if (coding
->dst_bytes
== 0)
6846 coding
->dst_bytes
= 1;
6847 coding
->destination
= (unsigned char *) xmalloc (coding
->dst_bytes
);
6848 coding
->dst_multibyte
= 0;
6852 coding
->dst_object
= Qnil
;
6853 coding
->dst_multibyte
= 0;
6856 encode_coding (coding
);
6858 if (EQ (dst_object
, Qt
))
6860 if (BUFFERP (coding
->dst_object
))
6861 coding
->dst_object
= Fbuffer_string ();
6865 = make_unibyte_string ((char *) coding
->destination
,
6867 xfree (coding
->destination
);
6873 /* This is the case of:
6874 (BUFFERP (src_object) && EQ (src_object, dst_object))
6875 As we have moved PT while replacing the original buffer
6876 contents, we must recover it now. */
6877 set_buffer_internal (XBUFFER (src_object
));
6878 if (saved_pt
< from
)
6879 TEMP_SET_PT_BOTH (saved_pt
, saved_pt_byte
);
6880 else if (saved_pt
< from
+ chars
)
6881 TEMP_SET_PT_BOTH (from
, from_byte
);
6882 else if (! NILP (current_buffer
->enable_multibyte_characters
))
6883 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced_char
- chars
),
6884 saved_pt_byte
+ (coding
->produced
- bytes
));
6886 TEMP_SET_PT_BOTH (saved_pt
+ (coding
->produced
- bytes
),
6887 saved_pt_byte
+ (coding
->produced
- bytes
));
6890 unbind_to (count
, Qnil
);
6895 preferred_coding_system ()
6897 int id
= coding_categories
[coding_priorities
[0]].id
;
6899 return CODING_ID_NAME (id
);
6904 /*** 8. Emacs Lisp library functions ***/
6906 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
6907 doc
: /* Return t if OBJECT is nil or a coding-system.
6908 See the documentation of `define-coding-system' for information
6909 about coding-system objects. */)
6913 return ((NILP (obj
) || CODING_SYSTEM_P (obj
)) ? Qt
: Qnil
);
6916 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
6917 Sread_non_nil_coding_system
, 1, 1, 0,
6918 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
6925 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
6926 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
6928 while (SCHARS (val
) == 0);
6929 return (Fintern (val
, Qnil
));
6932 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
6933 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
6934 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
6935 (prompt
, default_coding_system
)
6936 Lisp_Object prompt
, default_coding_system
;
6939 if (SYMBOLP (default_coding_system
))
6940 XSETSTRING (default_coding_system
, XPNTR (SYMBOL_NAME (default_coding_system
)));
6941 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
6942 Qt
, Qnil
, Qcoding_system_history
,
6943 default_coding_system
, Qnil
);
6944 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
6947 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
6949 doc
: /* Check validity of CODING-SYSTEM.
6950 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. */)
6952 Lisp_Object coding_system
;
6954 CHECK_SYMBOL (coding_system
);
6955 if (!NILP (Fcoding_system_p (coding_system
)))
6956 return coding_system
;
6958 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
6962 /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
6963 HIGHEST is nonzero, return the coding system of the highest
6964 priority among the detected coding systems. Otherwize return a
6965 list of detected coding systems sorted by their priorities. If
6966 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
6967 multibyte form but contains only ASCII and eight-bit chars.
6968 Otherwise, the bytes are raw bytes.
6970 CODING-SYSTEM controls the detection as below:
6972 If it is nil, detect both text-format and eol-format. If the
6973 text-format part of CODING-SYSTEM is already specified
6974 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
6975 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
6976 detect only text-format. */
6979 detect_coding_system (src
, src_chars
, src_bytes
, highest
, multibytep
,
6981 const unsigned char *src
;
6982 int src_chars
, src_bytes
, highest
;
6984 Lisp_Object coding_system
;
6986 const unsigned char *src_end
= src
+ src_bytes
;
6987 Lisp_Object attrs
, eol_type
;
6989 struct coding_system coding
;
6991 struct coding_detection_info detect_info
;
6992 enum coding_category base_category
;
6994 if (NILP (coding_system
))
6995 coding_system
= Qundecided
;
6996 setup_coding_system (coding_system
, &coding
);
6997 attrs
= CODING_ID_ATTRS (coding
.id
);
6998 eol_type
= CODING_ID_EOL_TYPE (coding
.id
);
6999 coding_system
= CODING_ATTR_BASE_NAME (attrs
);
7001 coding
.source
= src
;
7002 coding
.src_chars
= src_chars
;
7003 coding
.src_bytes
= src_bytes
;
7004 coding
.src_multibyte
= multibytep
;
7005 coding
.consumed
= 0;
7006 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7008 detect_info
.checked
= detect_info
.found
= detect_info
.rejected
= 0;
7010 /* At first, detect text-format if necessary. */
7011 base_category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7012 if (base_category
== coding_category_undecided
)
7014 enum coding_category category
;
7015 struct coding_system
*this;
7018 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7019 for (i
= 0; src
< src_end
; i
++, src
++)
7022 if (c
& 0x80 || (c
< 0x20 && (c
== ISO_CODE_ESC
7024 || c
== ISO_CODE_SO
)))
7027 coding
.head_ascii
= src
- coding
.source
;
7030 for (i
= 0; i
< coding_category_raw_text
; i
++)
7032 category
= coding_priorities
[i
];
7033 this = coding_categories
+ category
;
7037 /* No coding system of this category is defined. */
7038 detect_info
.rejected
|= (1 << category
);
7040 else if (category
>= coding_category_raw_text
)
7042 else if (detect_info
.checked
& (1 << category
))
7045 && (detect_info
.found
& (1 << category
)))
7050 if ((*(this->detector
)) (&coding
, &detect_info
)
7052 && (detect_info
.found
& (1 << category
)))
7054 if (category
== coding_category_utf_16_auto
)
7056 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7057 category
= coding_category_utf_16_le
;
7059 category
= coding_category_utf_16_be
;
7066 if (detect_info
.rejected
== CATEGORY_MASK_ANY
)
7068 detect_info
.found
= CATEGORY_MASK_RAW_TEXT
;
7069 id
= coding_categories
[coding_category_raw_text
].id
;
7070 val
= Fcons (make_number (id
), Qnil
);
7072 else if (! detect_info
.rejected
&& ! detect_info
.found
)
7074 detect_info
.found
= CATEGORY_MASK_ANY
;
7075 id
= coding_categories
[coding_category_undecided
].id
;
7076 val
= Fcons (make_number (id
), Qnil
);
7080 if (detect_info
.found
)
7082 detect_info
.found
= 1 << category
;
7083 val
= Fcons (make_number (this->id
), Qnil
);
7086 for (i
= 0; i
< coding_category_raw_text
; i
++)
7087 if (! (detect_info
.rejected
& (1 << coding_priorities
[i
])))
7089 detect_info
.found
= 1 << coding_priorities
[i
];
7090 id
= coding_categories
[coding_priorities
[i
]].id
;
7091 val
= Fcons (make_number (id
), Qnil
);
7097 int mask
= detect_info
.rejected
| detect_info
.found
;
7101 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7103 category
= coding_priorities
[i
];
7104 if (! (mask
& (1 << category
)))
7106 found
|= 1 << category
;
7107 id
= coding_categories
[category
].id
;
7108 val
= Fcons (make_number (id
), val
);
7111 for (i
= coding_category_raw_text
- 1; i
>= 0; i
--)
7113 category
= coding_priorities
[i
];
7114 if (detect_info
.found
& (1 << category
))
7116 id
= coding_categories
[category
].id
;
7117 val
= Fcons (make_number (id
), val
);
7120 detect_info
.found
|= found
;
7123 else if (base_category
== coding_category_utf_16_auto
)
7125 if (detect_coding_utf_16 (&coding
, &detect_info
))
7127 enum coding_category category
;
7128 struct coding_system
*this;
7130 if (detect_info
.found
& CATEGORY_MASK_UTF_16_LE
)
7131 this = coding_categories
+ coding_category_utf_16_le
;
7132 else if (detect_info
.found
& CATEGORY_MASK_UTF_16_BE
)
7133 this = coding_categories
+ coding_category_utf_16_be
;
7134 else if (detect_info
.rejected
& CATEGORY_MASK_UTF_16_LE_NOSIG
)
7135 this = coding_categories
+ coding_category_utf_16_be_nosig
;
7137 this = coding_categories
+ coding_category_utf_16_le_nosig
;
7138 val
= Fcons (make_number (this->id
), Qnil
);
7143 detect_info
.found
= 1 << XINT (CODING_ATTR_CATEGORY (attrs
));
7144 val
= Fcons (make_number (coding
.id
), Qnil
);
7147 /* Then, detect eol-format if necessary. */
7149 int normal_eol
= -1, utf_16_be_eol
= -1, utf_16_le_eol
;
7152 if (VECTORP (eol_type
))
7154 if (detect_info
.found
& ~CATEGORY_MASK_UTF_16
)
7155 normal_eol
= detect_eol (coding
.source
, src_bytes
,
7156 coding_category_raw_text
);
7157 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_BE
7158 | CATEGORY_MASK_UTF_16_BE_NOSIG
))
7159 utf_16_be_eol
= detect_eol (coding
.source
, src_bytes
,
7160 coding_category_utf_16_be
);
7161 if (detect_info
.found
& (CATEGORY_MASK_UTF_16_LE
7162 | CATEGORY_MASK_UTF_16_LE_NOSIG
))
7163 utf_16_le_eol
= detect_eol (coding
.source
, src_bytes
,
7164 coding_category_utf_16_le
);
7168 if (EQ (eol_type
, Qunix
))
7169 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_LF
;
7170 else if (EQ (eol_type
, Qdos
))
7171 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CRLF
;
7173 normal_eol
= utf_16_be_eol
= utf_16_le_eol
= EOL_SEEN_CR
;
7176 for (tail
= val
; CONSP (tail
); tail
= XCDR (tail
))
7178 enum coding_category category
;
7181 id
= XINT (XCAR (tail
));
7182 attrs
= CODING_ID_ATTRS (id
);
7183 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
7184 eol_type
= CODING_ID_EOL_TYPE (id
);
7185 if (VECTORP (eol_type
))
7187 if (category
== coding_category_utf_16_be
7188 || category
== coding_category_utf_16_be_nosig
)
7189 this_eol
= utf_16_be_eol
;
7190 else if (category
== coding_category_utf_16_le
7191 || category
== coding_category_utf_16_le_nosig
)
7192 this_eol
= utf_16_le_eol
;
7194 this_eol
= normal_eol
;
7196 if (this_eol
== EOL_SEEN_LF
)
7197 XSETCAR (tail
, AREF (eol_type
, 0));
7198 else if (this_eol
== EOL_SEEN_CRLF
)
7199 XSETCAR (tail
, AREF (eol_type
, 1));
7200 else if (this_eol
== EOL_SEEN_CR
)
7201 XSETCAR (tail
, AREF (eol_type
, 2));
7203 XSETCAR (tail
, CODING_ID_NAME (id
));
7206 XSETCAR (tail
, CODING_ID_NAME (id
));
7210 return (highest
? XCAR (val
) : val
);
7214 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
7216 doc
: /* Detect coding system of the text in the region between START and END.
7217 Return a list of possible coding systems ordered by priority.
7219 If only ASCII characters are found, it returns a list of single element
7220 `undecided' or its subsidiary coding system according to a detected
7223 If optional argument HIGHEST is non-nil, return the coding system of
7224 highest priority. */)
7225 (start
, end
, highest
)
7226 Lisp_Object start
, end
, highest
;
7229 int from_byte
, to_byte
;
7231 CHECK_NUMBER_COERCE_MARKER (start
);
7232 CHECK_NUMBER_COERCE_MARKER (end
);
7234 validate_region (&start
, &end
);
7235 from
= XINT (start
), to
= XINT (end
);
7236 from_byte
= CHAR_TO_BYTE (from
);
7237 to_byte
= CHAR_TO_BYTE (to
);
7239 if (from
< GPT
&& to
>= GPT
)
7240 move_gap_both (to
, to_byte
);
7242 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
7243 to
- from
, to_byte
- from_byte
,
7245 !NILP (current_buffer
7246 ->enable_multibyte_characters
),
7250 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
7252 doc
: /* Detect coding system of the text in STRING.
7253 Return a list of possible coding systems ordered by priority.
7255 If only ASCII characters are found, it returns a list of single element
7256 `undecided' or its subsidiary coding system according to a detected
7259 If optional argument HIGHEST is non-nil, return the coding system of
7260 highest priority. */)
7262 Lisp_Object string
, highest
;
7264 CHECK_STRING (string
);
7266 return detect_coding_system (SDATA (string
),
7267 SCHARS (string
), SBYTES (string
),
7268 !NILP (highest
), STRING_MULTIBYTE (string
),
7274 char_encodable_p (c
, attrs
)
7279 struct charset
*charset
;
7280 Lisp_Object translation_table
;
7282 translation_table
= CODING_ATTR_TRANS_TBL (attrs
);
7283 if (! NILP (translation_table
))
7284 c
= translate_char (translation_table
, c
);
7285 for (tail
= CODING_ATTR_CHARSET_LIST (attrs
);
7286 CONSP (tail
); tail
= XCDR (tail
))
7288 charset
= CHARSET_FROM_ID (XINT (XCAR (tail
)));
7289 if (CHAR_CHARSET_P (c
, charset
))
7292 return (! NILP (tail
));
7296 /* Return a list of coding systems that safely encode the text between
7297 START and END. If EXCLUDE is non-nil, it is a list of coding
7298 systems not to check. The returned list doesn't contain any such
7299 coding systems. In any case, if the text contains only ASCII or is
7300 unibyte, return t. */
7302 DEFUN ("find-coding-systems-region-internal",
7303 Ffind_coding_systems_region_internal
,
7304 Sfind_coding_systems_region_internal
, 2, 3, 0,
7305 doc
: /* Internal use only. */)
7306 (start
, end
, exclude
)
7307 Lisp_Object start
, end
, exclude
;
7309 Lisp_Object coding_attrs_list
, safe_codings
;
7310 EMACS_INT start_byte
, end_byte
;
7311 const unsigned char *p
, *pbeg
, *pend
;
7313 Lisp_Object tail
, elt
;
7315 if (STRINGP (start
))
7317 if (!STRING_MULTIBYTE (start
)
7318 || SCHARS (start
) == SBYTES (start
))
7321 end_byte
= SBYTES (start
);
7325 CHECK_NUMBER_COERCE_MARKER (start
);
7326 CHECK_NUMBER_COERCE_MARKER (end
);
7327 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7328 args_out_of_range (start
, end
);
7329 if (NILP (current_buffer
->enable_multibyte_characters
))
7331 start_byte
= CHAR_TO_BYTE (XINT (start
));
7332 end_byte
= CHAR_TO_BYTE (XINT (end
));
7333 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7336 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7338 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7339 move_gap_both (XINT (start
), start_byte
);
7341 move_gap_both (XINT (end
), end_byte
);
7345 coding_attrs_list
= Qnil
;
7346 for (tail
= Vcoding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7348 || NILP (Fmemq (XCAR (tail
), exclude
)))
7352 attrs
= AREF (CODING_SYSTEM_SPEC (XCAR (tail
)), 0);
7353 if (EQ (XCAR (tail
), CODING_ATTR_BASE_NAME (attrs
))
7354 && ! EQ (CODING_ATTR_TYPE (attrs
), Qundecided
))
7356 ASET (attrs
, coding_attr_trans_tbl
,
7357 get_translation_table (attrs
, 1));
7358 coding_attrs_list
= Fcons (attrs
, coding_attrs_list
);
7362 if (STRINGP (start
))
7363 p
= pbeg
= SDATA (start
);
7365 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7366 pend
= p
+ (end_byte
- start_byte
);
7368 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++;
7369 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7373 if (ASCII_BYTE_P (*p
))
7377 c
= STRING_CHAR_ADVANCE (p
);
7379 charset_map_loaded
= 0;
7380 for (tail
= coding_attrs_list
; CONSP (tail
);)
7385 else if (char_encodable_p (c
, elt
))
7387 else if (CONSP (XCDR (tail
)))
7389 XSETCAR (tail
, XCAR (XCDR (tail
)));
7390 XSETCDR (tail
, XCDR (XCDR (tail
)));
7394 XSETCAR (tail
, Qnil
);
7398 if (charset_map_loaded
)
7400 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7402 if (STRINGP (start
))
7403 pbeg
= SDATA (start
);
7405 pbeg
= BYTE_POS_ADDR (start_byte
);
7406 p
= pbeg
+ p_offset
;
7407 pend
= pbeg
+ pend_offset
;
7412 safe_codings
= Qnil
;
7413 for (tail
= coding_attrs_list
; CONSP (tail
); tail
= XCDR (tail
))
7414 if (! NILP (XCAR (tail
)))
7415 safe_codings
= Fcons (CODING_ATTR_BASE_NAME (XCAR (tail
)), safe_codings
);
7417 return safe_codings
;
7421 DEFUN ("unencodable-char-position", Funencodable_char_position
,
7422 Sunencodable_char_position
, 3, 5, 0,
7424 Return position of first un-encodable character in a region.
7425 START and END specfiy the region and CODING-SYSTEM specifies the
7426 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7428 If optional 4th argument COUNT is non-nil, it specifies at most how
7429 many un-encodable characters to search. In this case, the value is a
7432 If optional 5th argument STRING is non-nil, it is a string to search
7433 for un-encodable characters. In that case, START and END are indexes
7435 (start
, end
, coding_system
, count
, string
)
7436 Lisp_Object start
, end
, coding_system
, count
, string
;
7439 struct coding_system coding
;
7440 Lisp_Object attrs
, charset_list
, translation_table
;
7441 Lisp_Object positions
;
7443 const unsigned char *p
, *stop
, *pend
;
7444 int ascii_compatible
;
7446 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
7447 attrs
= CODING_ID_ATTRS (coding
.id
);
7448 if (EQ (CODING_ATTR_TYPE (attrs
), Qraw_text
))
7450 ascii_compatible
= ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
));
7451 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7452 translation_table
= get_translation_table (attrs
, 1);
7456 validate_region (&start
, &end
);
7457 from
= XINT (start
);
7459 if (NILP (current_buffer
->enable_multibyte_characters
)
7460 || (ascii_compatible
7461 && (to
- from
) == (CHAR_TO_BYTE (to
) - (CHAR_TO_BYTE (from
)))))
7463 p
= CHAR_POS_ADDR (from
);
7464 pend
= CHAR_POS_ADDR (to
);
7465 if (from
< GPT
&& to
>= GPT
)
7472 CHECK_STRING (string
);
7473 CHECK_NATNUM (start
);
7475 from
= XINT (start
);
7478 || to
> SCHARS (string
))
7479 args_out_of_range_3 (string
, start
, end
);
7480 if (! STRING_MULTIBYTE (string
))
7482 p
= SDATA (string
) + string_char_to_byte (string
, from
);
7483 stop
= pend
= SDATA (string
) + string_char_to_byte (string
, to
);
7484 if (ascii_compatible
&& (to
- from
) == (pend
- p
))
7492 CHECK_NATNUM (count
);
7501 if (ascii_compatible
)
7502 while (p
< stop
&& ASCII_BYTE_P (*p
))
7512 c
= STRING_CHAR_ADVANCE (p
);
7513 if (! (ASCII_CHAR_P (c
) && ascii_compatible
)
7514 && ! char_charset (translate_char (translation_table
, c
),
7515 charset_list
, NULL
))
7517 positions
= Fcons (make_number (from
), positions
);
7526 return (NILP (count
) ? Fcar (positions
) : Fnreverse (positions
));
7530 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region
,
7531 Scheck_coding_systems_region
, 3, 3, 0,
7532 doc
: /* Check if the region is encodable by coding systems.
7534 START and END are buffer positions specifying the region.
7535 CODING-SYSTEM-LIST is a list of coding systems to check.
7537 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7538 CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7539 whole region, POS0, POS1, ... are buffer positions where non-encodable
7540 characters are found.
7542 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7545 START may be a string. In that case, check if the string is
7546 encodable, and the value contains indices to the string instead of
7547 buffer positions. END is ignored. */)
7548 (start
, end
, coding_system_list
)
7549 Lisp_Object start
, end
, coding_system_list
;
7552 EMACS_INT start_byte
, end_byte
;
7554 const unsigned char *p
, *pbeg
, *pend
;
7556 Lisp_Object tail
, elt
, attrs
;
7558 if (STRINGP (start
))
7560 if (!STRING_MULTIBYTE (start
)
7561 && SCHARS (start
) != SBYTES (start
))
7564 end_byte
= SBYTES (start
);
7569 CHECK_NUMBER_COERCE_MARKER (start
);
7570 CHECK_NUMBER_COERCE_MARKER (end
);
7571 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
7572 args_out_of_range (start
, end
);
7573 if (NILP (current_buffer
->enable_multibyte_characters
))
7575 start_byte
= CHAR_TO_BYTE (XINT (start
));
7576 end_byte
= CHAR_TO_BYTE (XINT (end
));
7577 if (XINT (end
) - XINT (start
) == end_byte
- start_byte
)
7580 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
7582 if ((GPT
- XINT (start
)) < (XINT (end
) - GPT
))
7583 move_gap_both (XINT (start
), start_byte
);
7585 move_gap_both (XINT (end
), end_byte
);
7591 for (tail
= coding_system_list
; CONSP (tail
); tail
= XCDR (tail
))
7594 attrs
= AREF (CODING_SYSTEM_SPEC (elt
), 0);
7595 ASET (attrs
, coding_attr_trans_tbl
, get_translation_table (attrs
, 1));
7596 list
= Fcons (Fcons (elt
, Fcons (attrs
, Qnil
)), list
);
7599 if (STRINGP (start
))
7600 p
= pbeg
= SDATA (start
);
7602 p
= pbeg
= BYTE_POS_ADDR (start_byte
);
7603 pend
= p
+ (end_byte
- start_byte
);
7605 while (p
< pend
&& ASCII_BYTE_P (*p
)) p
++, pos
++;
7606 while (p
< pend
&& ASCII_BYTE_P (*(pend
- 1))) pend
--;
7610 if (ASCII_BYTE_P (*p
))
7614 c
= STRING_CHAR_ADVANCE (p
);
7616 charset_map_loaded
= 0;
7617 for (tail
= list
; CONSP (tail
); tail
= XCDR (tail
))
7619 elt
= XCDR (XCAR (tail
));
7620 if (! char_encodable_p (c
, XCAR (elt
)))
7621 XSETCDR (elt
, Fcons (make_number (pos
), XCDR (elt
)));
7623 if (charset_map_loaded
)
7625 EMACS_INT p_offset
= p
- pbeg
, pend_offset
= pend
- pbeg
;
7627 if (STRINGP (start
))
7628 pbeg
= SDATA (start
);
7630 pbeg
= BYTE_POS_ADDR (start_byte
);
7631 p
= pbeg
+ p_offset
;
7632 pend
= pbeg
+ pend_offset
;
7640 for (; CONSP (tail
); tail
= XCDR (tail
))
7643 if (CONSP (XCDR (XCDR (elt
))))
7644 list
= Fcons (Fcons (XCAR (elt
), Fnreverse (XCDR (XCDR (elt
)))),
7653 code_convert_region (start
, end
, coding_system
, dst_object
, encodep
, norecord
)
7654 Lisp_Object start
, end
, coding_system
, dst_object
;
7655 int encodep
, norecord
;
7657 struct coding_system coding
;
7658 EMACS_INT from
, from_byte
, to
, to_byte
;
7659 Lisp_Object src_object
;
7661 CHECK_NUMBER_COERCE_MARKER (start
);
7662 CHECK_NUMBER_COERCE_MARKER (end
);
7663 if (NILP (coding_system
))
7664 coding_system
= Qno_conversion
;
7666 CHECK_CODING_SYSTEM (coding_system
);
7667 src_object
= Fcurrent_buffer ();
7668 if (NILP (dst_object
))
7669 dst_object
= src_object
;
7670 else if (! EQ (dst_object
, Qt
))
7671 CHECK_BUFFER (dst_object
);
7673 validate_region (&start
, &end
);
7674 from
= XFASTINT (start
);
7675 from_byte
= CHAR_TO_BYTE (from
);
7676 to
= XFASTINT (end
);
7677 to_byte
= CHAR_TO_BYTE (to
);
7679 setup_coding_system (coding_system
, &coding
);
7680 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7683 encode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7686 decode_coding_object (&coding
, src_object
, from
, from_byte
, to
, to_byte
,
7689 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7691 return (BUFFERP (dst_object
)
7692 ? make_number (coding
.produced_char
)
7693 : coding
.dst_object
);
7697 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
7698 3, 4, "r\nzCoding system: ",
7699 doc
: /* Decode the current region from the specified coding system.
7700 When called from a program, takes four arguments:
7701 START, END, CODING-SYSTEM, and DESTINATION.
7702 START and END are buffer positions.
7704 Optional 4th arguments DESTINATION specifies where the decoded text goes.
7705 If nil, the region between START and END is replace by the decoded text.
7706 If buffer, the decoded text is inserted in the buffer.
7707 If t, the decoded text is returned.
7709 This function sets `last-coding-system-used' to the precise coding system
7710 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7711 not fully specified.)
7712 It returns the length of the decoded text. */)
7713 (start
, end
, coding_system
, destination
)
7714 Lisp_Object start
, end
, coding_system
, destination
;
7716 return code_convert_region (start
, end
, coding_system
, destination
, 0, 0);
7719 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
7720 3, 4, "r\nzCoding system: ",
7721 doc
: /* Encode the current region by specified coding system.
7722 When called from a program, takes three arguments:
7723 START, END, and CODING-SYSTEM. START and END are buffer positions.
7725 Optional 4th arguments DESTINATION specifies where the encoded text goes.
7726 If nil, the region between START and END is replace by the encoded text.
7727 If buffer, the encoded text is inserted in the buffer.
7728 If t, the encoded text is returned.
7730 This function sets `last-coding-system-used' to the precise coding system
7731 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7732 not fully specified.)
7733 It returns the length of the encoded text. */)
7734 (start
, end
, coding_system
, destination
)
7735 Lisp_Object start
, end
, coding_system
, destination
;
7737 return code_convert_region (start
, end
, coding_system
, destination
, 1, 0);
7741 code_convert_string (string
, coding_system
, dst_object
,
7742 encodep
, nocopy
, norecord
)
7743 Lisp_Object string
, coding_system
, dst_object
;
7744 int encodep
, nocopy
, norecord
;
7746 struct coding_system coding
;
7747 EMACS_INT chars
, bytes
;
7749 CHECK_STRING (string
);
7750 if (NILP (coding_system
))
7753 Vlast_coding_system_used
= Qno_conversion
;
7754 if (NILP (dst_object
))
7755 return (nocopy
? Fcopy_sequence (string
) : string
);
7758 if (NILP (coding_system
))
7759 coding_system
= Qno_conversion
;
7761 CHECK_CODING_SYSTEM (coding_system
);
7762 if (NILP (dst_object
))
7764 else if (! EQ (dst_object
, Qt
))
7765 CHECK_BUFFER (dst_object
);
7767 setup_coding_system (coding_system
, &coding
);
7768 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7769 chars
= SCHARS (string
);
7770 bytes
= SBYTES (string
);
7772 encode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
7774 decode_coding_object (&coding
, string
, 0, 0, chars
, bytes
, dst_object
);
7776 Vlast_coding_system_used
= CODING_ID_NAME (coding
.id
);
7778 return (BUFFERP (dst_object
)
7779 ? make_number (coding
.produced_char
)
7780 : coding
.dst_object
);
7784 /* Encode or decode STRING according to CODING_SYSTEM.
7785 Do not set Vlast_coding_system_used.
7787 This function is called only from macros DECODE_FILE and
7788 ENCODE_FILE, thus we ignore character composition. */
7791 code_convert_string_norecord (string
, coding_system
, encodep
)
7792 Lisp_Object string
, coding_system
;
7795 return code_convert_string (string
, coding_system
, Qt
, encodep
, 0, 1);
7799 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
7801 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
7803 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
7804 if the decoding operation is trivial.
7806 Optional fourth arg BUFFER non-nil meant that the decoded text is
7807 inserted in BUFFER instead of returned as a string. In this case,
7808 the return value is BUFFER.
7810 This function sets `last-coding-system-used' to the precise coding system
7811 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7812 not fully specified. */)
7813 (string
, coding_system
, nocopy
, buffer
)
7814 Lisp_Object string
, coding_system
, nocopy
, buffer
;
7816 return code_convert_string (string
, coding_system
, buffer
,
7817 0, ! NILP (nocopy
), 0);
7820 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
7822 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
7824 Optional third arg NOCOPY non-nil means it is OK to return STRING
7825 itself if the encoding operation is trivial.
7827 Optional fourth arg BUFFER non-nil meant that the encoded text is
7828 inserted in BUFFER instead of returned as a string. In this case,
7829 the return value is BUFFER.
7831 This function sets `last-coding-system-used' to the precise coding system
7832 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7833 not fully specified.) */)
7834 (string
, coding_system
, nocopy
, buffer
)
7835 Lisp_Object string
, coding_system
, nocopy
, buffer
;
7837 return code_convert_string (string
, coding_system
, buffer
,
7838 1, ! NILP (nocopy
), 1);
7842 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
7843 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
7844 Return the corresponding character. */)
7848 Lisp_Object spec
, attrs
, val
;
7849 struct charset
*charset_roman
, *charset_kanji
, *charset_kana
, *charset
;
7852 CHECK_NATNUM (code
);
7853 c
= XFASTINT (code
);
7854 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
7855 attrs
= AREF (spec
, 0);
7857 if (ASCII_BYTE_P (c
)
7858 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
7861 val
= CODING_ATTR_CHARSET_LIST (attrs
);
7862 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
7863 charset_kana
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
7864 charset_kanji
= CHARSET_FROM_ID (XINT (XCAR (val
)));
7867 charset
= charset_roman
;
7868 else if (c
>= 0xA0 && c
< 0xDF)
7870 charset
= charset_kana
;
7875 int s1
= c
>> 8, s2
= c
& 0xFF;
7877 if (s1
< 0x81 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF
7878 || s2
< 0x40 || s2
== 0x7F || s2
> 0xFC)
7879 error ("Invalid code: %d", code
);
7881 charset
= charset_kanji
;
7883 c
= DECODE_CHAR (charset
, c
);
7885 error ("Invalid code: %d", code
);
7886 return make_number (c
);
7890 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
7891 doc
: /* Encode a Japanese character CHAR to shift_jis encoding.
7892 Return the corresponding code in SJIS. */)
7896 Lisp_Object spec
, attrs
, charset_list
;
7898 struct charset
*charset
;
7901 CHECK_CHARACTER (ch
);
7903 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system
, spec
);
7904 attrs
= AREF (spec
, 0);
7906 if (ASCII_CHAR_P (c
)
7907 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
7910 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7911 charset
= char_charset (c
, charset_list
, &code
);
7912 if (code
== CHARSET_INVALID_CODE (charset
))
7913 error ("Can't encode by shift_jis encoding: %d", c
);
7916 return make_number (code
);
7919 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
7920 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
7921 Return the corresponding character. */)
7925 Lisp_Object spec
, attrs
, val
;
7926 struct charset
*charset_roman
, *charset_big5
, *charset
;
7929 CHECK_NATNUM (code
);
7930 c
= XFASTINT (code
);
7931 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
7932 attrs
= AREF (spec
, 0);
7934 if (ASCII_BYTE_P (c
)
7935 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
7938 val
= CODING_ATTR_CHARSET_LIST (attrs
);
7939 charset_roman
= CHARSET_FROM_ID (XINT (XCAR (val
))), val
= XCDR (val
);
7940 charset_big5
= CHARSET_FROM_ID (XINT (XCAR (val
)));
7943 charset
= charset_roman
;
7946 int b1
= c
>> 8, b2
= c
& 0x7F;
7947 if (b1
< 0xA1 || b1
> 0xFE
7948 || b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE)
7949 error ("Invalid code: %d", code
);
7950 charset
= charset_big5
;
7952 c
= DECODE_CHAR (charset
, (unsigned )c
);
7954 error ("Invalid code: %d", code
);
7955 return make_number (c
);
7958 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
7959 doc
: /* Encode the Big5 character CHAR to BIG5 coding system.
7960 Return the corresponding character code in Big5. */)
7964 Lisp_Object spec
, attrs
, charset_list
;
7965 struct charset
*charset
;
7969 CHECK_CHARACTER (ch
);
7971 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system
, spec
);
7972 attrs
= AREF (spec
, 0);
7973 if (ASCII_CHAR_P (c
)
7974 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs
)))
7977 charset_list
= CODING_ATTR_CHARSET_LIST (attrs
);
7978 charset
= char_charset (c
, charset_list
, &code
);
7979 if (code
== CHARSET_INVALID_CODE (charset
))
7980 error ("Can't encode by Big5 encoding: %d", c
);
7982 return make_number (code
);
7986 DEFUN ("set-terminal-coding-system-internal",
7987 Fset_terminal_coding_system_internal
,
7988 Sset_terminal_coding_system_internal
, 1, 1, 0,
7989 doc
: /* Internal use only. */)
7991 Lisp_Object coding_system
;
7993 CHECK_SYMBOL (coding_system
);
7994 setup_coding_system (Fcheck_coding_system (coding_system
),
7997 /* We had better not send unsafe characters to terminal. */
7998 terminal_coding
.mode
|= CODING_MODE_SAFE_ENCODING
;
7999 /* Characer composition should be disabled. */
8000 terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8001 terminal_coding
.src_multibyte
= 1;
8002 terminal_coding
.dst_multibyte
= 0;
8006 DEFUN ("set-safe-terminal-coding-system-internal",
8007 Fset_safe_terminal_coding_system_internal
,
8008 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
8009 doc
: /* Internal use only. */)
8011 Lisp_Object coding_system
;
8013 CHECK_SYMBOL (coding_system
);
8014 setup_coding_system (Fcheck_coding_system (coding_system
),
8015 &safe_terminal_coding
);
8016 /* Characer composition should be disabled. */
8017 safe_terminal_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8018 safe_terminal_coding
.src_multibyte
= 1;
8019 safe_terminal_coding
.dst_multibyte
= 0;
8023 DEFUN ("terminal-coding-system",
8024 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
8025 doc
: /* Return coding system specified for terminal output. */)
8028 return CODING_ID_NAME (terminal_coding
.id
);
8031 DEFUN ("set-keyboard-coding-system-internal",
8032 Fset_keyboard_coding_system_internal
,
8033 Sset_keyboard_coding_system_internal
, 1, 1, 0,
8034 doc
: /* Internal use only. */)
8036 Lisp_Object coding_system
;
8038 CHECK_SYMBOL (coding_system
);
8039 setup_coding_system (Fcheck_coding_system (coding_system
),
8041 /* Characer composition should be disabled. */
8042 keyboard_coding
.common_flags
&= ~CODING_ANNOTATE_COMPOSITION_MASK
;
8046 DEFUN ("keyboard-coding-system",
8047 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
8048 doc
: /* Return coding system specified for decoding keyboard input. */)
8051 return CODING_ID_NAME (keyboard_coding
.id
);
8055 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
8056 Sfind_operation_coding_system
, 1, MANY
, 0,
8057 doc
: /* Choose a coding system for an operation based on the target name.
8058 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8059 DECODING-SYSTEM is the coding system to use for decoding
8060 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8061 for encoding (in case OPERATION does encoding).
8063 The first argument OPERATION specifies an I/O primitive:
8064 For file I/O, `insert-file-contents' or `write-region'.
8065 For process I/O, `call-process', `call-process-region', or `start-process'.
8066 For network I/O, `open-network-stream'.
8068 The remaining arguments should be the same arguments that were passed
8069 to the primitive. Depending on which primitive, one of those arguments
8070 is selected as the TARGET. For example, if OPERATION does file I/O,
8071 whichever argument specifies the file name is TARGET.
8073 TARGET has a meaning which depends on OPERATION:
8074 For file I/O, TARGET is a file name.
8075 For process I/O, TARGET is a process name.
8076 For network I/O, TARGET is a service name or a port number
8078 This function looks up what specified for TARGET in,
8079 `file-coding-system-alist', `process-coding-system-alist',
8080 or `network-coding-system-alist' depending on OPERATION.
8081 They may specify a coding system, a cons of coding systems,
8082 or a function symbol to call.
8083 In the last case, we call the function with one argument,
8084 which is a list of all the arguments given to this function.
8086 usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
8091 Lisp_Object operation
, target_idx
, target
, val
;
8092 register Lisp_Object chain
;
8095 error ("Too few arguments");
8096 operation
= args
[0];
8097 if (!SYMBOLP (operation
)
8098 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
8099 error ("Invalid first arguement");
8100 if (nargs
< 1 + XINT (target_idx
))
8101 error ("Too few arguments for operation: %s",
8102 SDATA (SYMBOL_NAME (operation
)));
8103 target
= args
[XINT (target_idx
) + 1];
8104 if (!(STRINGP (target
)
8105 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
8106 error ("Invalid %dth argument", XINT (target_idx
) + 1);
8108 chain
= ((EQ (operation
, Qinsert_file_contents
)
8109 || EQ (operation
, Qwrite_region
))
8110 ? Vfile_coding_system_alist
8111 : (EQ (operation
, Qopen_network_stream
)
8112 ? Vnetwork_coding_system_alist
8113 : Vprocess_coding_system_alist
));
8117 for (; CONSP (chain
); chain
= XCDR (chain
))
8123 && ((STRINGP (target
)
8124 && STRINGP (XCAR (elt
))
8125 && fast_string_match (XCAR (elt
), target
) >= 0)
8126 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
8129 /* Here, if VAL is both a valid coding system and a valid
8130 function symbol, we return VAL as a coding system. */
8133 if (! SYMBOLP (val
))
8135 if (! NILP (Fcoding_system_p (val
)))
8136 return Fcons (val
, val
);
8137 if (! NILP (Ffboundp (val
)))
8139 val
= call1 (val
, Flist (nargs
, args
));
8142 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
8143 return Fcons (val
, val
);
8151 DEFUN ("set-coding-system-priority", Fset_coding_system_priority
,
8152 Sset_coding_system_priority
, 0, MANY
, 0,
8153 doc
: /* Assign higher priority to the coding systems given as arguments.
8154 If multiple coding systems belongs to the same category,
8155 all but the first one are ignored.
8157 usage: (set-coding-system-priority ...) */)
8163 int changed
[coding_category_max
];
8164 enum coding_category priorities
[coding_category_max
];
8166 bzero (changed
, sizeof changed
);
8168 for (i
= j
= 0; i
< nargs
; i
++)
8170 enum coding_category category
;
8171 Lisp_Object spec
, attrs
;
8173 CHECK_CODING_SYSTEM_GET_SPEC (args
[i
], spec
);
8174 attrs
= AREF (spec
, 0);
8175 category
= XINT (CODING_ATTR_CATEGORY (attrs
));
8176 if (changed
[category
])
8177 /* Ignore this coding system because a coding system of the
8178 same category already had a higher priority. */
8180 changed
[category
] = 1;
8181 priorities
[j
++] = category
;
8182 if (coding_categories
[category
].id
>= 0
8183 && ! EQ (args
[i
], CODING_ID_NAME (coding_categories
[category
].id
)))
8184 setup_coding_system (args
[i
], &coding_categories
[category
]);
8185 Fset (AREF (Vcoding_category_table
, category
), args
[i
]);
8188 /* Now we have decided top J priorities. Reflect the order of the
8189 original priorities to the remaining priorities. */
8191 for (i
= j
, j
= 0; i
< coding_category_max
; i
++, j
++)
8193 while (j
< coding_category_max
8194 && changed
[coding_priorities
[j
]])
8196 if (j
== coding_category_max
)
8198 priorities
[i
] = coding_priorities
[j
];
8201 bcopy (priorities
, coding_priorities
, sizeof priorities
);
8203 /* Update `coding-category-list'. */
8204 Vcoding_category_list
= Qnil
;
8205 for (i
= coding_category_max
- 1; i
>= 0; i
--)
8206 Vcoding_category_list
8207 = Fcons (AREF (Vcoding_category_table
, priorities
[i
]),
8208 Vcoding_category_list
);
8213 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list
,
8214 Scoding_system_priority_list
, 0, 1, 0,
8215 doc
: /* Return a list of coding systems ordered by their priorities.
8216 HIGHESTP non-nil means just return the highest priority one. */)
8218 Lisp_Object highestp
;
8223 for (i
= 0, val
= Qnil
; i
< coding_category_max
; i
++)
8225 enum coding_category category
= coding_priorities
[i
];
8226 int id
= coding_categories
[category
].id
;
8231 attrs
= CODING_ID_ATTRS (id
);
8232 if (! NILP (highestp
))
8233 return CODING_ATTR_BASE_NAME (attrs
);
8234 val
= Fcons (CODING_ATTR_BASE_NAME (attrs
), val
);
8236 return Fnreverse (val
);
8239 static char *suffixes
[] = { "-unix", "-dos", "-mac" };
8242 make_subsidiaries (base
)
8245 Lisp_Object subsidiaries
;
8246 int base_name_len
= SBYTES (SYMBOL_NAME (base
));
8247 char *buf
= (char *) alloca (base_name_len
+ 6);
8250 bcopy (SDATA (SYMBOL_NAME (base
)), buf
, base_name_len
);
8251 subsidiaries
= Fmake_vector (make_number (3), Qnil
);
8252 for (i
= 0; i
< 3; i
++)
8254 bcopy (suffixes
[i
], buf
+ base_name_len
, strlen (suffixes
[i
]) + 1);
8255 ASET (subsidiaries
, i
, intern (buf
));
8257 return subsidiaries
;
8261 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
8262 Sdefine_coding_system_internal
, coding_arg_max
, MANY
, 0,
8263 doc
: /* For internal use only.
8264 usage: (define-coding-system-internal ...) */)
8270 Lisp_Object spec_vec
; /* [ ATTRS ALIASE EOL_TYPE ] */
8271 Lisp_Object attrs
; /* Vector of attributes. */
8272 Lisp_Object eol_type
;
8273 Lisp_Object aliases
;
8274 Lisp_Object coding_type
, charset_list
, safe_charsets
;
8275 enum coding_category category
;
8276 Lisp_Object tail
, val
;
8277 int max_charset_id
= 0;
8280 if (nargs
< coding_arg_max
)
8283 attrs
= Fmake_vector (make_number (coding_attr_last_index
), Qnil
);
8285 name
= args
[coding_arg_name
];
8286 CHECK_SYMBOL (name
);
8287 CODING_ATTR_BASE_NAME (attrs
) = name
;
8289 val
= args
[coding_arg_mnemonic
];
8290 if (! STRINGP (val
))
8291 CHECK_CHARACTER (val
);
8292 CODING_ATTR_MNEMONIC (attrs
) = val
;
8294 coding_type
= args
[coding_arg_coding_type
];
8295 CHECK_SYMBOL (coding_type
);
8296 CODING_ATTR_TYPE (attrs
) = coding_type
;
8298 charset_list
= args
[coding_arg_charset_list
];
8299 if (SYMBOLP (charset_list
))
8301 if (EQ (charset_list
, Qiso_2022
))
8303 if (! EQ (coding_type
, Qiso_2022
))
8304 error ("Invalid charset-list");
8305 charset_list
= Viso_2022_charset_list
;
8307 else if (EQ (charset_list
, Qemacs_mule
))
8309 if (! EQ (coding_type
, Qemacs_mule
))
8310 error ("Invalid charset-list");
8311 charset_list
= Vemacs_mule_charset_list
;
8313 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8314 if (max_charset_id
< XFASTINT (XCAR (tail
)))
8315 max_charset_id
= XFASTINT (XCAR (tail
));
8319 charset_list
= Fcopy_sequence (charset_list
);
8320 for (tail
= charset_list
; !NILP (tail
); tail
= Fcdr (tail
))
8322 struct charset
*charset
;
8325 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8326 if (EQ (coding_type
, Qiso_2022
)
8327 ? CHARSET_ISO_FINAL (charset
) < 0
8328 : EQ (coding_type
, Qemacs_mule
)
8329 ? CHARSET_EMACS_MULE_ID (charset
) < 0
8331 error ("Can't handle charset `%s'",
8332 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8334 XSETCAR (tail
, make_number (charset
->id
));
8335 if (max_charset_id
< charset
->id
)
8336 max_charset_id
= charset
->id
;
8339 CODING_ATTR_CHARSET_LIST (attrs
) = charset_list
;
8341 safe_charsets
= Fmake_string (make_number (max_charset_id
+ 1),
8343 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8344 SSET (safe_charsets
, XFASTINT (XCAR (tail
)), 0);
8345 CODING_ATTR_SAFE_CHARSETS (attrs
) = safe_charsets
;
8347 CODING_ATTR_ASCII_COMPAT (attrs
) = args
[coding_arg_ascii_compatible_p
];
8349 val
= args
[coding_arg_decode_translation_table
];
8350 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8352 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8354 val
= args
[coding_arg_encode_translation_table
];
8355 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8357 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8359 val
= args
[coding_arg_post_read_conversion
];
8361 CODING_ATTR_POST_READ (attrs
) = val
;
8363 val
= args
[coding_arg_pre_write_conversion
];
8365 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8367 val
= args
[coding_arg_default_char
];
8369 CODING_ATTR_DEFAULT_CHAR (attrs
) = make_number (' ');
8372 CHECK_CHARACTER (val
);
8373 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8376 val
= args
[coding_arg_for_unibyte
];
8377 CODING_ATTR_FOR_UNIBYTE (attrs
) = NILP (val
) ? Qnil
: Qt
;
8379 val
= args
[coding_arg_plist
];
8381 CODING_ATTR_PLIST (attrs
) = val
;
8383 if (EQ (coding_type
, Qcharset
))
8385 /* Generate a lisp vector of 256 elements. Each element is nil,
8386 integer, or a list of charset IDs.
8388 If Nth element is nil, the byte code N is invalid in this
8391 If Nth element is a number NUM, N is the first byte of a
8392 charset whose ID is NUM.
8394 If Nth element is a list of charset IDs, N is the first byte
8395 of one of them. The list is sorted by dimensions of the
8396 charsets. A charset of smaller dimension comes firtst. */
8397 val
= Fmake_vector (make_number (256), Qnil
);
8399 for (tail
= charset_list
; CONSP (tail
); tail
= XCDR (tail
))
8401 struct charset
*charset
= CHARSET_FROM_ID (XFASTINT (XCAR (tail
)));
8402 int dim
= CHARSET_DIMENSION (charset
);
8403 int idx
= (dim
- 1) * 4;
8405 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8406 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8408 for (i
= charset
->code_space
[idx
];
8409 i
<= charset
->code_space
[idx
+ 1]; i
++)
8411 Lisp_Object tmp
, tmp2
;
8414 tmp
= AREF (val
, i
);
8417 else if (NUMBERP (tmp
))
8419 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp
)));
8421 tmp
= Fcons (XCAR (tail
), Fcons (tmp
, Qnil
));
8423 tmp
= Fcons (tmp
, Fcons (XCAR (tail
), Qnil
));
8427 for (tmp2
= tmp
; CONSP (tmp2
); tmp2
= XCDR (tmp2
))
8429 dim2
= CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2
))));
8434 tmp
= nconc2 (tmp
, Fcons (XCAR (tail
), Qnil
));
8437 XSETCDR (tmp2
, Fcons (XCAR (tmp2
), XCDR (tmp2
)));
8438 XSETCAR (tmp2
, XCAR (tail
));
8444 ASET (attrs
, coding_attr_charset_valids
, val
);
8445 category
= coding_category_charset
;
8447 else if (EQ (coding_type
, Qccl
))
8451 if (nargs
< coding_arg_ccl_max
)
8454 val
= args
[coding_arg_ccl_decoder
];
8455 CHECK_CCL_PROGRAM (val
);
8457 val
= Fcopy_sequence (val
);
8458 ASET (attrs
, coding_attr_ccl_decoder
, val
);
8460 val
= args
[coding_arg_ccl_encoder
];
8461 CHECK_CCL_PROGRAM (val
);
8463 val
= Fcopy_sequence (val
);
8464 ASET (attrs
, coding_attr_ccl_encoder
, val
);
8466 val
= args
[coding_arg_ccl_valids
];
8467 valids
= Fmake_string (make_number (256), make_number (0));
8468 for (tail
= val
; !NILP (tail
); tail
= Fcdr (tail
))
8475 from
= to
= XINT (val
);
8476 if (from
< 0 || from
> 255)
8477 args_out_of_range_3 (val
, make_number (0), make_number (255));
8482 CHECK_NATNUM_CAR (val
);
8483 CHECK_NATNUM_CDR (val
);
8484 from
= XINT (XCAR (val
));
8486 args_out_of_range_3 (XCAR (val
),
8487 make_number (0), make_number (255));
8488 to
= XINT (XCDR (val
));
8489 if (to
< from
|| to
> 255)
8490 args_out_of_range_3 (XCDR (val
),
8491 XCAR (val
), make_number (255));
8493 for (i
= from
; i
<= to
; i
++)
8494 SSET (valids
, i
, 1);
8496 ASET (attrs
, coding_attr_ccl_valids
, valids
);
8498 category
= coding_category_ccl
;
8500 else if (EQ (coding_type
, Qutf_16
))
8502 Lisp_Object bom
, endian
;
8504 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8506 if (nargs
< coding_arg_utf16_max
)
8509 bom
= args
[coding_arg_utf16_bom
];
8510 if (! NILP (bom
) && ! EQ (bom
, Qt
))
8514 CHECK_CODING_SYSTEM (val
);
8516 CHECK_CODING_SYSTEM (val
);
8518 ASET (attrs
, coding_attr_utf_16_bom
, bom
);
8520 endian
= args
[coding_arg_utf16_endian
];
8521 CHECK_SYMBOL (endian
);
8524 else if (! EQ (endian
, Qbig
) && ! EQ (endian
, Qlittle
))
8525 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian
)));
8526 ASET (attrs
, coding_attr_utf_16_endian
, endian
);
8528 category
= (CONSP (bom
)
8529 ? coding_category_utf_16_auto
8531 ? (EQ (endian
, Qbig
)
8532 ? coding_category_utf_16_be_nosig
8533 : coding_category_utf_16_le_nosig
)
8534 : (EQ (endian
, Qbig
)
8535 ? coding_category_utf_16_be
8536 : coding_category_utf_16_le
));
8538 else if (EQ (coding_type
, Qiso_2022
))
8540 Lisp_Object initial
, reg_usage
, request
, flags
;
8543 if (nargs
< coding_arg_iso2022_max
)
8546 initial
= Fcopy_sequence (args
[coding_arg_iso2022_initial
]);
8547 CHECK_VECTOR (initial
);
8548 for (i
= 0; i
< 4; i
++)
8550 val
= Faref (initial
, make_number (i
));
8553 struct charset
*charset
;
8555 CHECK_CHARSET_GET_CHARSET (val
, charset
);
8556 ASET (initial
, i
, make_number (CHARSET_ID (charset
)));
8557 if (i
== 0 && CHARSET_ASCII_COMPATIBLE_P (charset
))
8558 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8561 ASET (initial
, i
, make_number (-1));
8564 reg_usage
= args
[coding_arg_iso2022_reg_usage
];
8565 CHECK_CONS (reg_usage
);
8566 CHECK_NUMBER_CAR (reg_usage
);
8567 CHECK_NUMBER_CDR (reg_usage
);
8569 request
= Fcopy_sequence (args
[coding_arg_iso2022_request
]);
8570 for (tail
= request
; ! NILP (tail
); tail
= Fcdr (tail
))
8578 CHECK_CHARSET_GET_ID (tmp
, id
);
8579 CHECK_NATNUM_CDR (val
);
8580 if (XINT (XCDR (val
)) >= 4)
8581 error ("Invalid graphic register number: %d", XINT (XCDR (val
)));
8582 XSETCAR (val
, make_number (id
));
8585 flags
= args
[coding_arg_iso2022_flags
];
8586 CHECK_NATNUM (flags
);
8588 if (EQ (args
[coding_arg_charset_list
], Qiso_2022
))
8589 flags
= make_number (i
| CODING_ISO_FLAG_FULL_SUPPORT
);
8591 ASET (attrs
, coding_attr_iso_initial
, initial
);
8592 ASET (attrs
, coding_attr_iso_usage
, reg_usage
);
8593 ASET (attrs
, coding_attr_iso_request
, request
);
8594 ASET (attrs
, coding_attr_iso_flags
, flags
);
8595 setup_iso_safe_charsets (attrs
);
8597 if (i
& CODING_ISO_FLAG_SEVEN_BITS
)
8598 category
= ((i
& (CODING_ISO_FLAG_LOCKING_SHIFT
8599 | CODING_ISO_FLAG_SINGLE_SHIFT
))
8600 ? coding_category_iso_7_else
8601 : EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8602 ? coding_category_iso_7
8603 : coding_category_iso_7_tight
);
8606 int id
= XINT (AREF (initial
, 1));
8608 category
= (((i
& CODING_ISO_FLAG_LOCKING_SHIFT
)
8609 || EQ (args
[coding_arg_charset_list
], Qiso_2022
)
8611 ? coding_category_iso_8_else
8612 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id
)) == 1)
8613 ? coding_category_iso_8_1
8614 : coding_category_iso_8_2
);
8616 if (category
!= coding_category_iso_8_1
8617 && category
!= coding_category_iso_8_2
)
8618 CODING_ATTR_ASCII_COMPAT (attrs
) = Qnil
;
8620 else if (EQ (coding_type
, Qemacs_mule
))
8622 if (EQ (args
[coding_arg_charset_list
], Qemacs_mule
))
8623 ASET (attrs
, coding_attr_emacs_mule_full
, Qt
);
8624 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8625 category
= coding_category_emacs_mule
;
8627 else if (EQ (coding_type
, Qshift_jis
))
8630 struct charset
*charset
;
8632 if (XINT (Flength (charset_list
)) != 3
8633 && XINT (Flength (charset_list
)) != 4)
8634 error ("There should be three or four charsets");
8636 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8637 if (CHARSET_DIMENSION (charset
) != 1)
8638 error ("Dimension of charset %s is not one",
8639 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8640 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8641 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8643 charset_list
= XCDR (charset_list
);
8644 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8645 if (CHARSET_DIMENSION (charset
) != 1)
8646 error ("Dimension of charset %s is not one",
8647 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8649 charset_list
= XCDR (charset_list
);
8650 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8651 if (CHARSET_DIMENSION (charset
) != 2)
8652 error ("Dimension of charset %s is not two",
8653 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8655 charset_list
= XCDR (charset_list
);
8656 if (! NILP (charset_list
))
8658 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8659 if (CHARSET_DIMENSION (charset
) != 2)
8660 error ("Dimension of charset %s is not two",
8661 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8664 category
= coding_category_sjis
;
8665 Vsjis_coding_system
= name
;
8667 else if (EQ (coding_type
, Qbig5
))
8669 struct charset
*charset
;
8671 if (XINT (Flength (charset_list
)) != 2)
8672 error ("There should be just two charsets");
8674 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8675 if (CHARSET_DIMENSION (charset
) != 1)
8676 error ("Dimension of charset %s is not one",
8677 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8678 if (CHARSET_ASCII_COMPATIBLE_P (charset
))
8679 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8681 charset_list
= XCDR (charset_list
);
8682 charset
= CHARSET_FROM_ID (XINT (XCAR (charset_list
)));
8683 if (CHARSET_DIMENSION (charset
) != 2)
8684 error ("Dimension of charset %s is not two",
8685 SDATA (SYMBOL_NAME (CHARSET_NAME (charset
))));
8687 category
= coding_category_big5
;
8688 Vbig5_coding_system
= name
;
8690 else if (EQ (coding_type
, Qraw_text
))
8692 category
= coding_category_raw_text
;
8693 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8695 else if (EQ (coding_type
, Qutf_8
))
8697 category
= coding_category_utf_8
;
8698 CODING_ATTR_ASCII_COMPAT (attrs
) = Qt
;
8700 else if (EQ (coding_type
, Qundecided
))
8701 category
= coding_category_undecided
;
8703 error ("Invalid coding system type: %s",
8704 SDATA (SYMBOL_NAME (coding_type
)));
8706 CODING_ATTR_CATEGORY (attrs
) = make_number (category
);
8707 CODING_ATTR_PLIST (attrs
)
8708 = Fcons (QCcategory
, Fcons (AREF (Vcoding_category_table
, category
),
8709 CODING_ATTR_PLIST (attrs
)));
8711 eol_type
= args
[coding_arg_eol_type
];
8712 if (! NILP (eol_type
)
8713 && ! EQ (eol_type
, Qunix
)
8714 && ! EQ (eol_type
, Qdos
)
8715 && ! EQ (eol_type
, Qmac
))
8716 error ("Invalid eol-type");
8718 aliases
= Fcons (name
, Qnil
);
8720 if (NILP (eol_type
))
8722 eol_type
= make_subsidiaries (name
);
8723 for (i
= 0; i
< 3; i
++)
8725 Lisp_Object this_spec
, this_name
, this_aliases
, this_eol_type
;
8727 this_name
= AREF (eol_type
, i
);
8728 this_aliases
= Fcons (this_name
, Qnil
);
8729 this_eol_type
= (i
== 0 ? Qunix
: i
== 1 ? Qdos
: Qmac
);
8730 this_spec
= Fmake_vector (make_number (3), attrs
);
8731 ASET (this_spec
, 1, this_aliases
);
8732 ASET (this_spec
, 2, this_eol_type
);
8733 Fputhash (this_name
, this_spec
, Vcoding_system_hash_table
);
8734 Vcoding_system_list
= Fcons (this_name
, Vcoding_system_list
);
8735 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (this_name
), Qnil
),
8736 Vcoding_system_alist
);
8740 spec_vec
= Fmake_vector (make_number (3), attrs
);
8741 ASET (spec_vec
, 1, aliases
);
8742 ASET (spec_vec
, 2, eol_type
);
8744 Fputhash (name
, spec_vec
, Vcoding_system_hash_table
);
8745 Vcoding_system_list
= Fcons (name
, Vcoding_system_list
);
8746 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (name
), Qnil
),
8747 Vcoding_system_alist
);
8750 int id
= coding_categories
[category
].id
;
8752 if (id
< 0 || EQ (name
, CODING_ID_NAME (id
)))
8753 setup_coding_system (name
, &coding_categories
[category
]);
8759 return Fsignal (Qwrong_number_of_arguments
,
8760 Fcons (intern ("define-coding-system-internal"),
8761 make_number (nargs
)));
8765 DEFUN ("coding-system-put", Fcoding_system_put
, Scoding_system_put
,
8767 doc
: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
8768 (coding_system
, prop
, val
)
8769 Lisp_Object coding_system
, prop
, val
;
8771 Lisp_Object spec
, attrs
, plist
;
8773 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8774 attrs
= AREF (spec
, 0);
8775 if (EQ (prop
, QCmnemonic
))
8777 if (! STRINGP (val
))
8778 CHECK_CHARACTER (val
);
8779 CODING_ATTR_MNEMONIC (attrs
) = val
;
8781 else if (EQ (prop
, QCdefalut_char
))
8784 val
= make_number (' ');
8786 CHECK_CHARACTER (val
);
8787 CODING_ATTR_DEFAULT_CHAR (attrs
) = val
;
8789 else if (EQ (prop
, QCdecode_translation_table
))
8791 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8793 CODING_ATTR_DECODE_TBL (attrs
) = val
;
8795 else if (EQ (prop
, QCencode_translation_table
))
8797 if (! CHAR_TABLE_P (val
) && ! CONSP (val
))
8799 CODING_ATTR_ENCODE_TBL (attrs
) = val
;
8801 else if (EQ (prop
, QCpost_read_conversion
))
8804 CODING_ATTR_POST_READ (attrs
) = val
;
8806 else if (EQ (prop
, QCpre_write_conversion
))
8809 CODING_ATTR_PRE_WRITE (attrs
) = val
;
8812 CODING_ATTR_PLIST (attrs
)
8813 = Fplist_put (CODING_ATTR_PLIST (attrs
), prop
, val
);
8818 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias
,
8819 Sdefine_coding_system_alias
, 2, 2, 0,
8820 doc
: /* Define ALIAS as an alias for CODING-SYSTEM. */)
8821 (alias
, coding_system
)
8822 Lisp_Object alias
, coding_system
;
8824 Lisp_Object spec
, aliases
, eol_type
;
8826 CHECK_SYMBOL (alias
);
8827 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8828 aliases
= AREF (spec
, 1);
8829 /* ALISES should be a list of length more than zero, and the first
8830 element is a base coding system. Append ALIAS at the tail of the
8832 while (!NILP (XCDR (aliases
)))
8833 aliases
= XCDR (aliases
);
8834 XSETCDR (aliases
, Fcons (alias
, Qnil
));
8836 eol_type
= AREF (spec
, 2);
8837 if (VECTORP (eol_type
))
8839 Lisp_Object subsidiaries
;
8842 subsidiaries
= make_subsidiaries (alias
);
8843 for (i
= 0; i
< 3; i
++)
8844 Fdefine_coding_system_alias (AREF (subsidiaries
, i
),
8845 AREF (eol_type
, i
));
8848 Fputhash (alias
, spec
, Vcoding_system_hash_table
);
8849 Vcoding_system_list
= Fcons (alias
, Vcoding_system_list
);
8850 Vcoding_system_alist
= Fcons (Fcons (Fsymbol_name (alias
), Qnil
),
8851 Vcoding_system_alist
);
8856 DEFUN ("coding-system-base", Fcoding_system_base
, Scoding_system_base
,
8858 doc
: /* Return the base of CODING-SYSTEM.
8859 Any alias or subsidiary coding system is not a base coding system. */)
8861 Lisp_Object coding_system
;
8863 Lisp_Object spec
, attrs
;
8865 if (NILP (coding_system
))
8866 return (Qno_conversion
);
8867 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8868 attrs
= AREF (spec
, 0);
8869 return CODING_ATTR_BASE_NAME (attrs
);
8872 DEFUN ("coding-system-plist", Fcoding_system_plist
, Scoding_system_plist
,
8874 doc
: "Return the property list of CODING-SYSTEM.")
8876 Lisp_Object coding_system
;
8878 Lisp_Object spec
, attrs
;
8880 if (NILP (coding_system
))
8881 coding_system
= Qno_conversion
;
8882 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8883 attrs
= AREF (spec
, 0);
8884 return CODING_ATTR_PLIST (attrs
);
8888 DEFUN ("coding-system-aliases", Fcoding_system_aliases
, Scoding_system_aliases
,
8890 doc
: /* Return the list of aliases of CODING-SYSTEM. */)
8892 Lisp_Object coding_system
;
8896 if (NILP (coding_system
))
8897 coding_system
= Qno_conversion
;
8898 CHECK_CODING_SYSTEM_GET_SPEC (coding_system
, spec
);
8899 return AREF (spec
, 1);
8902 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type
,
8903 Scoding_system_eol_type
, 1, 1, 0,
8904 doc
: /* Return eol-type of CODING-SYSTEM.
8905 An eol-type is integer 0, 1, 2, or a vector of coding systems.
8907 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
8908 and CR respectively.
8910 A vector value indicates that a format of end-of-line should be
8911 detected automatically. Nth element of the vector is the subsidiary
8912 coding system whose eol-type is N. */)
8914 Lisp_Object coding_system
;
8916 Lisp_Object spec
, eol_type
;
8919 if (NILP (coding_system
))
8920 coding_system
= Qno_conversion
;
8921 if (! CODING_SYSTEM_P (coding_system
))
8923 spec
= CODING_SYSTEM_SPEC (coding_system
);
8924 eol_type
= AREF (spec
, 2);
8925 if (VECTORP (eol_type
))
8926 return Fcopy_sequence (eol_type
);
8927 n
= EQ (eol_type
, Qunix
) ? 0 : EQ (eol_type
, Qdos
) ? 1 : 2;
8928 return make_number (n
);
8934 /*** 9. Post-amble ***/
8941 for (i
= 0; i
< coding_category_max
; i
++)
8943 coding_categories
[i
].id
= -1;
8944 coding_priorities
[i
] = i
;
8947 /* ISO2022 specific initialize routine. */
8948 for (i
= 0; i
< 0x20; i
++)
8949 iso_code_class
[i
] = ISO_control_0
;
8950 for (i
= 0x21; i
< 0x7F; i
++)
8951 iso_code_class
[i
] = ISO_graphic_plane_0
;
8952 for (i
= 0x80; i
< 0xA0; i
++)
8953 iso_code_class
[i
] = ISO_control_1
;
8954 for (i
= 0xA1; i
< 0xFF; i
++)
8955 iso_code_class
[i
] = ISO_graphic_plane_1
;
8956 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
8957 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
8958 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
8959 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
8960 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
8961 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
8962 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
8963 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
8964 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
8966 for (i
= 0; i
< 256; i
++)
8968 emacs_mule_bytes
[i
] = 1;
8970 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_11
] = 3;
8971 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_12
] = 3;
8972 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_21
] = 4;
8973 emacs_mule_bytes
[EMACS_MULE_LEADING_CODE_PRIVATE_22
] = 4;
8981 staticpro (&Vcoding_system_hash_table
);
8983 Lisp_Object args
[2];
8986 Vcoding_system_hash_table
= Fmake_hash_table (2, args
);
8989 staticpro (&Vsjis_coding_system
);
8990 Vsjis_coding_system
= Qnil
;
8992 staticpro (&Vbig5_coding_system
);
8993 Vbig5_coding_system
= Qnil
;
8995 staticpro (&Vcode_conversion_reused_workbuf
);
8996 Vcode_conversion_reused_workbuf
= Qnil
;
8998 staticpro (&Vcode_conversion_workbuf_name
);
8999 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
9001 reused_workbuf_in_use
= 0;
9003 DEFSYM (Qcharset
, "charset");
9004 DEFSYM (Qtarget_idx
, "target-idx");
9005 DEFSYM (Qcoding_system_history
, "coding-system-history");
9006 Fset (Qcoding_system_history
, Qnil
);
9008 /* Target FILENAME is the first argument. */
9009 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
9010 /* Target FILENAME is the third argument. */
9011 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
9013 DEFSYM (Qcall_process
, "call-process");
9014 /* Target PROGRAM is the first argument. */
9015 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
9017 DEFSYM (Qcall_process_region
, "call-process-region");
9018 /* Target PROGRAM is the third argument. */
9019 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
9021 DEFSYM (Qstart_process
, "start-process");
9022 /* Target PROGRAM is the third argument. */
9023 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
9025 DEFSYM (Qopen_network_stream
, "open-network-stream");
9026 /* Target SERVICE is the fourth argument. */
9027 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
9029 DEFSYM (Qcoding_system
, "coding-system");
9030 DEFSYM (Qcoding_aliases
, "coding-aliases");
9032 DEFSYM (Qeol_type
, "eol-type");
9033 DEFSYM (Qunix
, "unix");
9034 DEFSYM (Qdos
, "dos");
9036 DEFSYM (Qbuffer_file_coding_system
, "buffer-file-coding-system");
9037 DEFSYM (Qpost_read_conversion
, "post-read-conversion");
9038 DEFSYM (Qpre_write_conversion
, "pre-write-conversion");
9039 DEFSYM (Qdefault_char
, "default-char");
9040 DEFSYM (Qundecided
, "undecided");
9041 DEFSYM (Qno_conversion
, "no-conversion");
9042 DEFSYM (Qraw_text
, "raw-text");
9044 DEFSYM (Qiso_2022
, "iso-2022");
9046 DEFSYM (Qutf_8
, "utf-8");
9047 DEFSYM (Qutf_8_emacs
, "utf-8-emacs");
9049 DEFSYM (Qutf_16
, "utf-16");
9050 DEFSYM (Qbig
, "big");
9051 DEFSYM (Qlittle
, "little");
9053 DEFSYM (Qshift_jis
, "shift-jis");
9054 DEFSYM (Qbig5
, "big5");
9056 DEFSYM (Qcoding_system_p
, "coding-system-p");
9058 DEFSYM (Qcoding_system_error
, "coding-system-error");
9059 Fput (Qcoding_system_error
, Qerror_conditions
,
9060 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
9061 Fput (Qcoding_system_error
, Qerror_message
,
9062 build_string ("Invalid coding system"));
9064 /* Intern this now in case it isn't already done.
9065 Setting this variable twice is harmless.
9066 But don't staticpro it here--that is done in alloc.c. */
9067 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
9069 DEFSYM (Qtranslation_table
, "translation-table");
9070 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
9071 DEFSYM (Qtranslation_table_id
, "translation-table-id");
9072 DEFSYM (Qtranslation_table_for_decode
, "translation-table-for-decode");
9073 DEFSYM (Qtranslation_table_for_encode
, "translation-table-for-encode");
9075 DEFSYM (Qvalid_codes
, "valid-codes");
9077 DEFSYM (Qemacs_mule
, "emacs-mule");
9079 DEFSYM (QCcategory
, ":category");
9080 DEFSYM (QCmnemonic
, ":mnemonic");
9081 DEFSYM (QCdefalut_char
, ":default-char");
9082 DEFSYM (QCdecode_translation_table
, ":decode-translation-table");
9083 DEFSYM (QCencode_translation_table
, ":encode-translation-table");
9084 DEFSYM (QCpost_read_conversion
, ":post-read-conversion");
9085 DEFSYM (QCpre_write_conversion
, ":pre-write-conversion");
9087 Vcoding_category_table
9088 = Fmake_vector (make_number (coding_category_max
), Qnil
);
9089 staticpro (&Vcoding_category_table
);
9090 /* Followings are target of code detection. */
9091 ASET (Vcoding_category_table
, coding_category_iso_7
,
9092 intern ("coding-category-iso-7"));
9093 ASET (Vcoding_category_table
, coding_category_iso_7_tight
,
9094 intern ("coding-category-iso-7-tight"));
9095 ASET (Vcoding_category_table
, coding_category_iso_8_1
,
9096 intern ("coding-category-iso-8-1"));
9097 ASET (Vcoding_category_table
, coding_category_iso_8_2
,
9098 intern ("coding-category-iso-8-2"));
9099 ASET (Vcoding_category_table
, coding_category_iso_7_else
,
9100 intern ("coding-category-iso-7-else"));
9101 ASET (Vcoding_category_table
, coding_category_iso_8_else
,
9102 intern ("coding-category-iso-8-else"));
9103 ASET (Vcoding_category_table
, coding_category_utf_8
,
9104 intern ("coding-category-utf-8"));
9105 ASET (Vcoding_category_table
, coding_category_utf_16_be
,
9106 intern ("coding-category-utf-16-be"));
9107 ASET (Vcoding_category_table
, coding_category_utf_16_auto
,
9108 intern ("coding-category-utf-16-auto"));
9109 ASET (Vcoding_category_table
, coding_category_utf_16_le
,
9110 intern ("coding-category-utf-16-le"));
9111 ASET (Vcoding_category_table
, coding_category_utf_16_be_nosig
,
9112 intern ("coding-category-utf-16-be-nosig"));
9113 ASET (Vcoding_category_table
, coding_category_utf_16_le_nosig
,
9114 intern ("coding-category-utf-16-le-nosig"));
9115 ASET (Vcoding_category_table
, coding_category_charset
,
9116 intern ("coding-category-charset"));
9117 ASET (Vcoding_category_table
, coding_category_sjis
,
9118 intern ("coding-category-sjis"));
9119 ASET (Vcoding_category_table
, coding_category_big5
,
9120 intern ("coding-category-big5"));
9121 ASET (Vcoding_category_table
, coding_category_ccl
,
9122 intern ("coding-category-ccl"));
9123 ASET (Vcoding_category_table
, coding_category_emacs_mule
,
9124 intern ("coding-category-emacs-mule"));
9125 /* Followings are NOT target of code detection. */
9126 ASET (Vcoding_category_table
, coding_category_raw_text
,
9127 intern ("coding-category-raw-text"));
9128 ASET (Vcoding_category_table
, coding_category_undecided
,
9129 intern ("coding-category-undecided"));
9131 DEFSYM (Qinsufficient_source
, "insufficient-source");
9132 DEFSYM (Qinconsistent_eol
, "inconsistent-eol");
9133 DEFSYM (Qinvalid_source
, "invalid-source");
9134 DEFSYM (Qinterrupted
, "interrupted");
9135 DEFSYM (Qinsufficient_memory
, "insufficient-memory");
9137 defsubr (&Scoding_system_p
);
9138 defsubr (&Sread_coding_system
);
9139 defsubr (&Sread_non_nil_coding_system
);
9140 defsubr (&Scheck_coding_system
);
9141 defsubr (&Sdetect_coding_region
);
9142 defsubr (&Sdetect_coding_string
);
9143 defsubr (&Sfind_coding_systems_region_internal
);
9144 defsubr (&Sunencodable_char_position
);
9145 defsubr (&Scheck_coding_systems_region
);
9146 defsubr (&Sdecode_coding_region
);
9147 defsubr (&Sencode_coding_region
);
9148 defsubr (&Sdecode_coding_string
);
9149 defsubr (&Sencode_coding_string
);
9150 defsubr (&Sdecode_sjis_char
);
9151 defsubr (&Sencode_sjis_char
);
9152 defsubr (&Sdecode_big5_char
);
9153 defsubr (&Sencode_big5_char
);
9154 defsubr (&Sset_terminal_coding_system_internal
);
9155 defsubr (&Sset_safe_terminal_coding_system_internal
);
9156 defsubr (&Sterminal_coding_system
);
9157 defsubr (&Sset_keyboard_coding_system_internal
);
9158 defsubr (&Skeyboard_coding_system
);
9159 defsubr (&Sfind_operation_coding_system
);
9160 defsubr (&Sset_coding_system_priority
);
9161 defsubr (&Sdefine_coding_system_internal
);
9162 defsubr (&Sdefine_coding_system_alias
);
9163 defsubr (&Scoding_system_put
);
9164 defsubr (&Scoding_system_base
);
9165 defsubr (&Scoding_system_plist
);
9166 defsubr (&Scoding_system_aliases
);
9167 defsubr (&Scoding_system_eol_type
);
9168 defsubr (&Scoding_system_priority_list
);
9170 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
9171 doc
: /* List of coding systems.
9173 Do not alter the value of this variable manually. This variable should be
9174 updated by the functions `define-coding-system' and
9175 `define-coding-system-alias'. */);
9176 Vcoding_system_list
= Qnil
;
9178 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
9179 doc
: /* Alist of coding system names.
9180 Each element is one element list of coding system name.
9181 This variable is given to `completing-read' as TABLE argument.
9183 Do not alter the value of this variable manually. This variable should be
9184 updated by the functions `make-coding-system' and
9185 `define-coding-system-alias'. */);
9186 Vcoding_system_alist
= Qnil
;
9188 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
9189 doc
: /* List of coding-categories (symbols) ordered by priority.
9191 On detecting a coding system, Emacs tries code detection algorithms
9192 associated with each coding-category one by one in this order. When
9193 one algorithm agrees with a byte sequence of source text, the coding
9194 system bound to the corresponding coding-category is selected. */);
9198 Vcoding_category_list
= Qnil
;
9199 for (i
= coding_category_max
- 1; i
>= 0; i
--)
9200 Vcoding_category_list
9201 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
9202 Vcoding_category_list
);
9205 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
9206 doc
: /* Specify the coding system for read operations.
9207 It is useful to bind this variable with `let', but do not set it globally.
9208 If the value is a coding system, it is used for decoding on read operation.
9209 If not, an appropriate element is used from one of the coding system alists:
9210 There are three such tables, `file-coding-system-alist',
9211 `process-coding-system-alist', and `network-coding-system-alist'. */);
9212 Vcoding_system_for_read
= Qnil
;
9214 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
9215 doc
: /* Specify the coding system for write operations.
9216 Programs bind this variable with `let', but you should not set it globally.
9217 If the value is a coding system, it is used for encoding of output,
9218 when writing it to a file and when sending it to a file or subprocess.
9220 If this does not specify a coding system, an appropriate element
9221 is used from one of the coding system alists:
9222 There are three such tables, `file-coding-system-alist',
9223 `process-coding-system-alist', and `network-coding-system-alist'.
9224 For output to files, if the above procedure does not specify a coding system,
9225 the value of `buffer-file-coding-system' is used. */);
9226 Vcoding_system_for_write
= Qnil
;
9228 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
9230 Coding system used in the latest file or process I/O. */);
9231 Vlast_coding_system_used
= Qnil
;
9233 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error
,
9235 Error status of the last code conversion.
9237 When an error was detected in the last code conversion, this variable
9238 is set to one of the following symbols.
9239 `insufficient-source'
9243 `insufficient-memory'
9244 When no error was detected, the value doesn't change. So, to check
9245 the error status of a code conversion by this variable, you must
9246 explicitly set this variable to nil before performing code
9248 Vlast_code_conversion_error
= Qnil
;
9250 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
9252 *Non-nil means always inhibit code conversion of end-of-line format.
9253 See info node `Coding Systems' and info node `Text and Binary' concerning
9254 such conversion. */);
9255 inhibit_eol_conversion
= 0;
9257 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
9259 Non-nil means process buffer inherits coding system of process output.
9260 Bind it to t if the process output is to be treated as if it were a file
9261 read from some filesystem. */);
9262 inherit_process_coding_system
= 0;
9264 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
9266 Alist to decide a coding system to use for a file I/O operation.
9267 The format is ((PATTERN . VAL) ...),
9268 where PATTERN is a regular expression matching a file name,
9269 VAL is a coding system, a cons of coding systems, or a function symbol.
9270 If VAL is a coding system, it is used for both decoding and encoding
9272 If VAL is a cons of coding systems, the car part is used for decoding,
9273 and the cdr part is used for encoding.
9274 If VAL is a function symbol, the function must return a coding system
9275 or a cons of coding systems which are used as above. The function gets
9276 the arguments with which `find-operation-coding-systems' was called.
9278 See also the function `find-operation-coding-system'
9279 and the variable `auto-coding-alist'. */);
9280 Vfile_coding_system_alist
= Qnil
;
9282 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
9284 Alist to decide a coding system to use for a process I/O operation.
9285 The format is ((PATTERN . VAL) ...),
9286 where PATTERN is a regular expression matching a program name,
9287 VAL is a coding system, a cons of coding systems, or a function symbol.
9288 If VAL is a coding system, it is used for both decoding what received
9289 from the program and encoding what sent to the program.
9290 If VAL is a cons of coding systems, the car part is used for decoding,
9291 and the cdr part is used for encoding.
9292 If VAL is a function symbol, the function must return a coding system
9293 or a cons of coding systems which are used as above.
9295 See also the function `find-operation-coding-system'. */);
9296 Vprocess_coding_system_alist
= Qnil
;
9298 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
9300 Alist to decide a coding system to use for a network I/O operation.
9301 The format is ((PATTERN . VAL) ...),
9302 where PATTERN is a regular expression matching a network service name
9303 or is a port number to connect to,
9304 VAL is a coding system, a cons of coding systems, or a function symbol.
9305 If VAL is a coding system, it is used for both decoding what received
9306 from the network stream and encoding what sent to the network stream.
9307 If VAL is a cons of coding systems, the car part is used for decoding,
9308 and the cdr part is used for encoding.
9309 If VAL is a function symbol, the function must return a coding system
9310 or a cons of coding systems which are used as above.
9312 See also the function `find-operation-coding-system'. */);
9313 Vnetwork_coding_system_alist
= Qnil
;
9315 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
9316 doc
: /* Coding system to use with system messages.
9317 Also used for decoding keyboard input on X Window system. */);
9318 Vlocale_coding_system
= Qnil
;
9320 /* The eol mnemonics are reset in startup.el system-dependently. */
9321 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
9323 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
9324 eol_mnemonic_unix
= build_string (":");
9326 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
9328 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
9329 eol_mnemonic_dos
= build_string ("\\");
9331 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
9333 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
9334 eol_mnemonic_mac
= build_string ("/");
9336 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
9338 *String displayed in mode line when end-of-line format is not yet determined. */);
9339 eol_mnemonic_undecided
= build_string (":");
9341 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
9343 *Non-nil enables character translation while encoding and decoding. */);
9344 Venable_character_translation
= Qt
;
9346 DEFVAR_LISP ("standard-translation-table-for-decode",
9347 &Vstandard_translation_table_for_decode
,
9348 doc
: /* Table for translating characters while decoding. */);
9349 Vstandard_translation_table_for_decode
= Qnil
;
9351 DEFVAR_LISP ("standard-translation-table-for-encode",
9352 &Vstandard_translation_table_for_encode
,
9353 doc
: /* Table for translating characters while encoding. */);
9354 Vstandard_translation_table_for_encode
= Qnil
;
9356 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table
,
9357 doc
: /* Alist of charsets vs revision numbers.
9358 While encoding, if a charset (car part of an element) is found,
9359 designate it with the escape sequence identifying revision (cdr part
9360 of the element). */);
9361 Vcharset_revision_table
= Qnil
;
9363 DEFVAR_LISP ("default-process-coding-system",
9364 &Vdefault_process_coding_system
,
9365 doc
: /* Cons of coding systems used for process I/O by default.
9366 The car part is used for decoding a process output,
9367 the cdr part is used for encoding a text to be sent to a process. */);
9368 Vdefault_process_coding_system
= Qnil
;
9370 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
9372 Table of extra Latin codes in the range 128..159 (inclusive).
9373 This is a vector of length 256.
9374 If Nth element is non-nil, the existence of code N in a file
9375 \(or output of subprocess) doesn't prevent it to be detected as
9376 a coding system of ISO 2022 variant which has a flag
9377 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9378 or reading output of a subprocess.
9379 Only 128th through 159th elements has a meaning. */);
9380 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
9382 DEFVAR_LISP ("select-safe-coding-system-function",
9383 &Vselect_safe_coding_system_function
,
9385 Function to call to select safe coding system for encoding a text.
9387 If set, this function is called to force a user to select a proper
9388 coding system which can encode the text in the case that a default
9389 coding system used in each operation can't encode the text.
9391 The default value is `select-safe-coding-system' (which see). */);
9392 Vselect_safe_coding_system_function
= Qnil
;
9394 DEFVAR_BOOL ("coding-system-require-warning",
9395 &coding_system_require_warning
,
9396 doc
: /* Internal use only.
9397 If non-nil, on writing a file, `select-safe-coding-system-function' is
9398 called even if `coding-system-for-write' is non-nil. The command
9399 `universal-coding-system-argument' binds this variable to t temporarily. */);
9400 coding_system_require_warning
= 0;
9403 DEFVAR_BOOL ("inhibit-iso-escape-detection",
9404 &inhibit_iso_escape_detection
,
9406 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
9408 By default, on reading a file, Emacs tries to detect how the text is
9409 encoded. This code detection is sensitive to escape sequences. If
9410 the sequence is valid as ISO2022, the code is determined as one of
9411 the ISO2022 encodings, and the file is decoded by the corresponding
9412 coding system (e.g. `iso-2022-7bit').
9414 However, there may be a case that you want to read escape sequences in
9415 a file as is. In such a case, you can set this variable to non-nil.
9416 Then, as the code detection ignores any escape sequences, no file is
9417 detected as encoded in some ISO2022 encoding. The result is that all
9418 escape sequences become visible in a buffer.
9420 The default value is nil, and it is strongly recommended not to change
9421 it. That is because many Emacs Lisp source files that contain
9422 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9423 in Emacs's distribution, and they won't be decoded correctly on
9424 reading if you suppress escape sequence detection.
9426 The other way to read escape sequences in a file without decoding is
9427 to explicitly specify some coding system that doesn't use ISO2022's
9428 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
9429 inhibit_iso_escape_detection
= 0;
9431 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
9432 doc
: /* Char table for translating self-inserting characters.
9433 This is applied to the result of input methods, not their input. See also
9434 `keyboard-translate-table'. */);
9435 Vtranslation_table_for_input
= Qnil
;
9438 Lisp_Object args
[coding_arg_max
];
9439 Lisp_Object plist
[16];
9442 for (i
= 0; i
< coding_arg_max
; i
++)
9445 plist
[0] = intern (":name");
9446 plist
[1] = args
[coding_arg_name
] = Qno_conversion
;
9447 plist
[2] = intern (":mnemonic");
9448 plist
[3] = args
[coding_arg_mnemonic
] = make_number ('=');
9449 plist
[4] = intern (":coding-type");
9450 plist
[5] = args
[coding_arg_coding_type
] = Qraw_text
;
9451 plist
[6] = intern (":ascii-compatible-p");
9452 plist
[7] = args
[coding_arg_ascii_compatible_p
] = Qt
;
9453 plist
[8] = intern (":default-char");
9454 plist
[9] = args
[coding_arg_default_char
] = make_number (0);
9455 plist
[10] = intern (":for-unibyte");
9456 plist
[11] = args
[coding_arg_for_unibyte
] = Qt
;
9457 plist
[12] = intern (":docstring");
9458 plist
[13] = build_string ("Do no conversion.\n\
9460 When you visit a file with this coding, the file is read into a\n\
9461 unibyte buffer as is, thus each byte of a file is treated as a\n\
9463 plist
[14] = intern (":eol-type");
9464 plist
[15] = args
[coding_arg_eol_type
] = Qunix
;
9465 args
[coding_arg_plist
] = Flist (16, plist
);
9466 Fdefine_coding_system_internal (coding_arg_max
, args
);
9469 setup_coding_system (Qno_conversion
, &keyboard_coding
);
9470 setup_coding_system (Qno_conversion
, &terminal_coding
);
9471 setup_coding_system (Qno_conversion
, &safe_terminal_coding
);
9476 for (i
= 0; i
< coding_category_max
; i
++)
9477 Fset (AREF (Vcoding_category_table
, i
), Qno_conversion
);
9482 emacs_strerror (error_number
)
9487 synchronize_system_messages_locale ();
9488 str
= strerror (error_number
);
9490 if (! NILP (Vlocale_coding_system
))
9492 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
9493 Vlocale_coding_system
,
9495 str
= (char *) SDATA (dec
);