Merge from emacs--devo--0
[bpt/emacs.git] / src / coding.c
1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
8
9 This file is part of GNU Emacs.
10
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 3, or (at your option)
14 any later version.
15
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.
20
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., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
25
26 /*** TABLE OF CONTENTS ***
27
28 0. General comments
29 1. Preamble
30 2. Emacs' internal format (emacs-mule) handlers
31 3. ISO2022 handlers
32 4. Shift-JIS and BIG5 handlers
33 5. CCL handlers
34 6. End-of-line handlers
35 7. C library functions
36 8. Emacs Lisp library functions
37 9. Post-amble
38
39 */
40
41 /*** 0. General comments ***/
42
43
44 /*** GENERAL NOTE on CODING SYSTEMS ***
45
46 A coding system is an encoding mechanism for one or more character
47 sets. Here's a list of coding systems which Emacs can handle. When
48 we say "decode", it means converting some other coding system to
49 Emacs' internal format (emacs-mule), and when we say "encode",
50 it means converting the coding system emacs-mule to some other
51 coding system.
52
53 0. Emacs' internal format (emacs-mule)
54
55 Emacs itself holds a multi-lingual character in buffers and strings
56 in a special format. Details are described in section 2.
57
58 1. ISO2022
59
60 The most famous coding system for multiple character sets. X's
61 Compound Text, various EUCs (Extended Unix Code), and coding
62 systems used in Internet communication such as ISO-2022-JP are
63 all variants of ISO2022. Details are described in section 3.
64
65 2. SJIS (or Shift-JIS or MS-Kanji-Code)
66
67 A coding system to encode character sets: ASCII, JISX0201, and
68 JISX0208. Widely used for PC's in Japan. Details are described in
69 section 4.
70
71 3. BIG5
72
73 A coding system to encode the character sets ASCII and Big5. Widely
74 used for Chinese (mainly in Taiwan and Hong Kong). Details are
75 described in section 4. In this file, when we write "BIG5"
76 (all uppercase), we mean the coding system, and when we write
77 "Big5" (capitalized), we mean the character set.
78
79 4. Raw text
80
81 A coding system for text containing random 8-bit code. Emacs does
82 no code conversion on such text except for end-of-line format.
83
84 5. Other
85
86 If a user wants to read/write text encoded in a coding system not
87 listed above, he can supply a decoder and an encoder for it as CCL
88 (Code Conversion Language) programs. Emacs executes the CCL program
89 while reading/writing.
90
91 Emacs represents a coding system by a Lisp symbol that has a property
92 `coding-system'. But, before actually using the coding system, the
93 information about it is set in a structure of type `struct
94 coding_system' for rapid processing. See section 6 for more details.
95
96 */
97
98 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
99
100 How end-of-line of text is encoded depends on the operating system.
101 For instance, Unix's format is just one byte of `line-feed' code,
102 whereas DOS's format is two-byte sequence of `carriage-return' and
103 `line-feed' codes. MacOS's format is usually one byte of
104 `carriage-return'.
105
106 Since text character encoding and end-of-line encoding are
107 independent, any coding system described above can have any
108 end-of-line format. So Emacs has information about end-of-line
109 format in each coding-system. See section 6 for more details.
110
111 */
112
113 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
114
115 These functions check if a text between SRC and SRC_END is encoded
116 in the coding system category XXX. Each returns an integer value in
117 which appropriate flag bits for the category XXX are set. The flag
118 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
119 template for these functions. If MULTIBYTEP is nonzero, 8-bit codes
120 of the range 0x80..0x9F are in multibyte form. */
121 #if 0
122 int
123 detect_coding_emacs_mule (src, src_end, multibytep)
124 unsigned char *src, *src_end;
125 int multibytep;
126 {
127 ...
128 }
129 #endif
130
131 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
132
133 These functions decode SRC_BYTES length of unibyte text at SOURCE
134 encoded in CODING to Emacs' internal format. The resulting
135 multibyte text goes to a place pointed to by DESTINATION, the length
136 of which should not exceed DST_BYTES.
137
138 These functions set the information about original and decoded texts
139 in the members `produced', `produced_char', `consumed', and
140 `consumed_char' of the structure *CODING. They also set the member
141 `result' to one of CODING_FINISH_XXX indicating how the decoding
142 finished.
143
144 DST_BYTES zero means that the source area and destination area are
145 overlapped, which means that we can produce a decoded text until it
146 reaches the head of the not-yet-decoded source text.
147
148 Below is a template for these functions. */
149 #if 0
150 static void
151 decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
152 struct coding_system *coding;
153 const unsigned char *source;
154 unsigned char *destination;
155 int src_bytes, dst_bytes;
156 {
157 ...
158 }
159 #endif
160
161 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
162
163 These functions encode SRC_BYTES length text at SOURCE from Emacs'
164 internal multibyte format to CODING. The resulting unibyte text
165 goes to a place pointed to by DESTINATION, the length of which
166 should not exceed DST_BYTES.
167
168 These functions set the information about original and encoded texts
169 in the members `produced', `produced_char', `consumed', and
170 `consumed_char' of the structure *CODING. They also set the member
171 `result' to one of CODING_FINISH_XXX indicating how the encoding
172 finished.
173
174 DST_BYTES zero means that the source area and destination area are
175 overlapped, which means that we can produce encoded text until it
176 reaches at the head of the not-yet-encoded source text.
177
178 Below is a template for these functions. */
179 #if 0
180 static void
181 encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
182 struct coding_system *coding;
183 unsigned char *source, *destination;
184 int src_bytes, dst_bytes;
185 {
186 ...
187 }
188 #endif
189
190 /*** COMMONLY USED MACROS ***/
191
192 /* The following two macros ONE_MORE_BYTE and TWO_MORE_BYTES safely
193 get one, two, and three bytes from the source text respectively.
194 If there are not enough bytes in the source, they jump to
195 `label_end_of_loop'. The caller should set variables `coding',
196 `src' and `src_end' to appropriate pointer in advance. These
197 macros are called from decoding routines `decode_coding_XXX', thus
198 it is assumed that the source text is unibyte. */
199
200 #define ONE_MORE_BYTE(c1) \
201 do { \
202 if (src >= src_end) \
203 { \
204 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
205 goto label_end_of_loop; \
206 } \
207 c1 = *src++; \
208 } while (0)
209
210 #define TWO_MORE_BYTES(c1, c2) \
211 do { \
212 if (src + 1 >= src_end) \
213 { \
214 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
215 goto label_end_of_loop; \
216 } \
217 c1 = *src++; \
218 c2 = *src++; \
219 } while (0)
220
221
222 /* Like ONE_MORE_BYTE, but 8-bit bytes of data at SRC are in multibyte
223 form if MULTIBYTEP is nonzero. In addition, if SRC is not less
224 than SRC_END, return with RET. */
225
226 #define ONE_MORE_BYTE_CHECK_MULTIBYTE(c1, multibytep, ret) \
227 do { \
228 if (src >= src_end) \
229 { \
230 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
231 return ret; \
232 } \
233 c1 = *src++; \
234 if (multibytep && c1 == LEADING_CODE_8_BIT_CONTROL) \
235 c1 = *src++ - 0x20; \
236 } while (0)
237
238 /* Set C to the next character at the source text pointed by `src'.
239 If there are not enough characters in the source, jump to
240 `label_end_of_loop'. The caller should set variables `coding'
241 `src', `src_end', and `translation_table' to appropriate pointers
242 in advance. This macro is used in encoding routines
243 `encode_coding_XXX', thus it assumes that the source text is in
244 multibyte form except for 8-bit characters. 8-bit characters are
245 in multibyte form if coding->src_multibyte is nonzero, else they
246 are represented by a single byte. */
247
248 #define ONE_MORE_CHAR(c) \
249 do { \
250 int len = src_end - src; \
251 int bytes; \
252 if (len <= 0) \
253 { \
254 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
255 goto label_end_of_loop; \
256 } \
257 if (coding->src_multibyte \
258 || UNIBYTE_STR_AS_MULTIBYTE_P (src, len, bytes)) \
259 c = STRING_CHAR_AND_LENGTH (src, len, bytes); \
260 else \
261 c = *src, bytes = 1; \
262 if (!NILP (translation_table)) \
263 c = translate_char (translation_table, c, -1, 0, 0); \
264 src += bytes; \
265 } while (0)
266
267
268 /* Produce a multibyte form of character C to `dst'. Jump to
269 `label_end_of_loop' if there's not enough space at `dst'.
270
271 If we are now in the middle of a composition sequence, the decoded
272 character may be ALTCHAR (for the current composition). In that
273 case, the character goes to coding->cmp_data->data instead of
274 `dst'.
275
276 This macro is used in decoding routines. */
277
278 #define EMIT_CHAR(c) \
279 do { \
280 if (! COMPOSING_P (coding) \
281 || coding->composing == COMPOSITION_RELATIVE \
282 || coding->composing == COMPOSITION_WITH_RULE) \
283 { \
284 int bytes = CHAR_BYTES (c); \
285 if ((dst + bytes) > (dst_bytes ? dst_end : src)) \
286 { \
287 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
288 goto label_end_of_loop; \
289 } \
290 dst += CHAR_STRING (c, dst); \
291 coding->produced_char++; \
292 } \
293 \
294 if (COMPOSING_P (coding) \
295 && coding->composing != COMPOSITION_RELATIVE) \
296 { \
297 CODING_ADD_COMPOSITION_COMPONENT (coding, c); \
298 coding->composition_rule_follows \
299 = coding->composing != COMPOSITION_WITH_ALTCHARS; \
300 } \
301 } while (0)
302
303
304 #define EMIT_ONE_BYTE(c) \
305 do { \
306 if (dst >= (dst_bytes ? dst_end : src)) \
307 { \
308 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
309 goto label_end_of_loop; \
310 } \
311 *dst++ = c; \
312 } while (0)
313
314 #define EMIT_TWO_BYTES(c1, c2) \
315 do { \
316 if (dst + 2 > (dst_bytes ? dst_end : src)) \
317 { \
318 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
319 goto label_end_of_loop; \
320 } \
321 *dst++ = c1, *dst++ = c2; \
322 } while (0)
323
324 #define EMIT_BYTES(from, to) \
325 do { \
326 if (dst + (to - from) > (dst_bytes ? dst_end : src)) \
327 { \
328 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
329 goto label_end_of_loop; \
330 } \
331 while (from < to) \
332 *dst++ = *from++; \
333 } while (0)
334
335 \f
336 /*** 1. Preamble ***/
337
338 #ifdef emacs
339 #include <config.h>
340 #endif
341
342 #include <stdio.h>
343
344 #ifdef emacs
345
346 #include "lisp.h"
347 #include "buffer.h"
348 #include "charset.h"
349 #include "composite.h"
350 #include "ccl.h"
351 #include "coding.h"
352 #include "window.h"
353 #include "intervals.h"
354 #include "frame.h"
355 #include "termhooks.h"
356
357 #else /* not emacs */
358
359 #include "mulelib.h"
360
361 #endif /* not emacs */
362
363 Lisp_Object Qcoding_system, Qeol_type;
364 Lisp_Object Qbuffer_file_coding_system;
365 Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
366 Lisp_Object Qno_conversion, Qundecided;
367 Lisp_Object Qcoding_system_history;
368 Lisp_Object Qsafe_chars;
369 Lisp_Object Qvalid_codes;
370 Lisp_Object Qascii_incompatible;
371
372 extern Lisp_Object Qinsert_file_contents, Qwrite_region;
373 Lisp_Object Qcall_process, Qcall_process_region;
374 Lisp_Object Qstart_process, Qopen_network_stream;
375 Lisp_Object Qtarget_idx;
376
377 /* If a symbol has this property, evaluate the value to define the
378 symbol as a coding system. */
379 Lisp_Object Qcoding_system_define_form;
380
381 Lisp_Object Vselect_safe_coding_system_function;
382
383 int coding_system_require_warning;
384
385 /* Mnemonic string for each format of end-of-line. */
386 Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
387 /* Mnemonic string to indicate format of end-of-line is not yet
388 decided. */
389 Lisp_Object eol_mnemonic_undecided;
390
391 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
392 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac.
393 This has an effect only for external encoding (i.e. for output to
394 file and process), not for in-buffer or Lisp string encoding. */
395 int system_eol_type;
396
397 #ifdef emacs
398
399 /* Information about which coding system is safe for which chars.
400 The value has the form (GENERIC-LIST . NON-GENERIC-ALIST).
401
402 GENERIC-LIST is a list of generic coding systems which can encode
403 any characters.
404
405 NON-GENERIC-ALIST is an alist of non generic coding systems vs the
406 corresponding char table that contains safe chars. */
407 Lisp_Object Vcoding_system_safe_chars;
408
409 Lisp_Object Vcoding_system_list, Vcoding_system_alist;
410
411 Lisp_Object Qcoding_system_p, Qcoding_system_error;
412
413 /* Coding system emacs-mule and raw-text are for converting only
414 end-of-line format. */
415 Lisp_Object Qemacs_mule, Qraw_text;
416
417 Lisp_Object Qutf_8;
418
419 /* Coding-systems are handed between Emacs Lisp programs and C internal
420 routines by the following three variables. */
421 /* Coding-system for reading files and receiving data from process. */
422 Lisp_Object Vcoding_system_for_read;
423 /* Coding-system for writing files and sending data to process. */
424 Lisp_Object Vcoding_system_for_write;
425 /* Coding-system actually used in the latest I/O. */
426 Lisp_Object Vlast_coding_system_used;
427
428 /* A vector of length 256 which contains information about special
429 Latin codes (especially for dealing with Microsoft codes). */
430 Lisp_Object Vlatin_extra_code_table;
431
432 /* Flag to inhibit code conversion of end-of-line format. */
433 int inhibit_eol_conversion;
434
435 /* Flag to inhibit ISO2022 escape sequence detection. */
436 int inhibit_iso_escape_detection;
437
438 /* Flag to make buffer-file-coding-system inherit from process-coding. */
439 int inherit_process_coding_system;
440
441 /* Coding system to be used to encode text for terminal display when
442 terminal coding system is nil. */
443 struct coding_system safe_terminal_coding;
444
445 /* Default coding system to be used to write a file. */
446 struct coding_system default_buffer_file_coding;
447
448 Lisp_Object Vfile_coding_system_alist;
449 Lisp_Object Vprocess_coding_system_alist;
450 Lisp_Object Vnetwork_coding_system_alist;
451
452 Lisp_Object Vlocale_coding_system;
453
454 #endif /* emacs */
455
456 Lisp_Object Qcoding_category, Qcoding_category_index;
457
458 /* List of symbols `coding-category-xxx' ordered by priority. */
459 Lisp_Object Vcoding_category_list;
460
461 /* Table of coding categories (Lisp symbols). */
462 Lisp_Object Vcoding_category_table;
463
464 /* Table of names of symbol for each coding-category. */
465 char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
466 "coding-category-emacs-mule",
467 "coding-category-sjis",
468 "coding-category-iso-7",
469 "coding-category-iso-7-tight",
470 "coding-category-iso-8-1",
471 "coding-category-iso-8-2",
472 "coding-category-iso-7-else",
473 "coding-category-iso-8-else",
474 "coding-category-ccl",
475 "coding-category-big5",
476 "coding-category-utf-8",
477 "coding-category-utf-16-be",
478 "coding-category-utf-16-le",
479 "coding-category-raw-text",
480 "coding-category-binary"
481 };
482
483 /* Table of pointers to coding systems corresponding to each coding
484 categories. */
485 struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX];
486
487 /* Table of coding category masks. Nth element is a mask for a coding
488 category of which priority is Nth. */
489 static
490 int coding_priorities[CODING_CATEGORY_IDX_MAX];
491
492 /* Flag to tell if we look up translation table on character code
493 conversion. */
494 Lisp_Object Venable_character_translation;
495 /* Standard translation table to look up on decoding (reading). */
496 Lisp_Object Vstandard_translation_table_for_decode;
497 /* Standard translation table to look up on encoding (writing). */
498 Lisp_Object Vstandard_translation_table_for_encode;
499
500 Lisp_Object Qtranslation_table;
501 Lisp_Object Qtranslation_table_id;
502 Lisp_Object Qtranslation_table_for_decode;
503 Lisp_Object Qtranslation_table_for_encode;
504
505 /* Alist of charsets vs revision number. */
506 Lisp_Object Vcharset_revision_alist;
507
508 /* Default coding systems used for process I/O. */
509 Lisp_Object Vdefault_process_coding_system;
510
511 /* Char table for translating Quail and self-inserting input. */
512 Lisp_Object Vtranslation_table_for_input;
513
514 /* Global flag to tell that we can't call post-read-conversion and
515 pre-write-conversion functions. Usually the value is zero, but it
516 is set to 1 temporarily while such functions are running. This is
517 to avoid infinite recursive call. */
518 static int inhibit_pre_post_conversion;
519
520 Lisp_Object Qchar_coding_system;
521
522 /* Return `safe-chars' property of CODING_SYSTEM (symbol). Don't check
523 its validity. */
524
525 Lisp_Object
526 coding_safe_chars (coding_system)
527 Lisp_Object coding_system;
528 {
529 Lisp_Object coding_spec, plist, safe_chars;
530
531 coding_spec = Fget (coding_system, Qcoding_system);
532 plist = XVECTOR (coding_spec)->contents[3];
533 safe_chars = Fplist_get (XVECTOR (coding_spec)->contents[3], Qsafe_chars);
534 return (CHAR_TABLE_P (safe_chars) ? safe_chars : Qt);
535 }
536
537 #define CODING_SAFE_CHAR_P(safe_chars, c) \
538 (EQ (safe_chars, Qt) || !NILP (CHAR_TABLE_REF (safe_chars, c)))
539
540 \f
541 /*** 2. Emacs internal format (emacs-mule) handlers ***/
542
543 /* Emacs' internal format for representation of multiple character
544 sets is a kind of multi-byte encoding, i.e. characters are
545 represented by variable-length sequences of one-byte codes.
546
547 ASCII characters and control characters (e.g. `tab', `newline') are
548 represented by one-byte sequences which are their ASCII codes, in
549 the range 0x00 through 0x7F.
550
551 8-bit characters of the range 0x80..0x9F are represented by
552 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
553 code + 0x20).
554
555 8-bit characters of the range 0xA0..0xFF are represented by
556 one-byte sequences which are their 8-bit code.
557
558 The other characters are represented by a sequence of `base
559 leading-code', optional `extended leading-code', and one or two
560 `position-code's. The length of the sequence is determined by the
561 base leading-code. Leading-code takes the range 0x81 through 0x9D,
562 whereas extended leading-code and position-code take the range 0xA0
563 through 0xFF. See `charset.h' for more details about leading-code
564 and position-code.
565
566 --- CODE RANGE of Emacs' internal format ---
567 character set range
568 ------------- -----
569 ascii 0x00..0x7F
570 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
571 eight-bit-graphic 0xA0..0xBF
572 ELSE 0x81..0x9D + [0xA0..0xFF]+
573 ---------------------------------------------
574
575 As this is the internal character representation, the format is
576 usually not used externally (i.e. in a file or in a data sent to a
577 process). But, it is possible to have a text externally in this
578 format (i.e. by encoding by the coding system `emacs-mule').
579
580 In that case, a sequence of one-byte codes has a slightly different
581 form.
582
583 Firstly, all characters in eight-bit-control are represented by
584 one-byte sequences which are their 8-bit code.
585
586 Next, character composition data are represented by the byte
587 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
588 where,
589 METHOD is 0xF0 plus one of composition method (enum
590 composition_method),
591
592 BYTES is 0xA0 plus the byte length of these composition data,
593
594 CHARS is 0xA0 plus the number of characters composed by these
595 data,
596
597 COMPONENTs are characters of multibyte form or composition
598 rules encoded by two-byte of ASCII codes.
599
600 In addition, for backward compatibility, the following formats are
601 also recognized as composition data on decoding.
602
603 0x80 MSEQ ...
604 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
605
606 Here,
607 MSEQ is a multibyte form but in these special format:
608 ASCII: 0xA0 ASCII_CODE+0x80,
609 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
610 RULE is a one byte code of the range 0xA0..0xF0 that
611 represents a composition rule.
612 */
613
614 enum emacs_code_class_type emacs_code_class[256];
615
616 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
617 Check if a text is encoded in Emacs' internal format. If it is,
618 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
619
620 static int
621 detect_coding_emacs_mule (src, src_end, multibytep)
622 unsigned char *src, *src_end;
623 int multibytep;
624 {
625 unsigned char c;
626 int composing = 0;
627 /* Dummy for ONE_MORE_BYTE. */
628 struct coding_system dummy_coding;
629 struct coding_system *coding = &dummy_coding;
630
631 while (1)
632 {
633 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep,
634 CODING_CATEGORY_MASK_EMACS_MULE);
635 if (composing)
636 {
637 if (c < 0xA0)
638 composing = 0;
639 else if (c == 0xA0)
640 {
641 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0);
642 c &= 0x7F;
643 }
644 else
645 c -= 0x20;
646 }
647
648 if (c < 0x20)
649 {
650 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
651 return 0;
652 }
653 else if (c >= 0x80 && c < 0xA0)
654 {
655 if (c == 0x80)
656 /* Old leading code for a composite character. */
657 composing = 1;
658 else
659 {
660 unsigned char *src_base = src - 1;
661 int bytes;
662
663 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src_base, src_end - src_base,
664 bytes))
665 return 0;
666 src = src_base + bytes;
667 }
668 }
669 }
670 }
671
672
673 /* Record the starting position START and METHOD of one composition. */
674
675 #define CODING_ADD_COMPOSITION_START(coding, start, method) \
676 do { \
677 struct composition_data *cmp_data = coding->cmp_data; \
678 int *data = cmp_data->data + cmp_data->used; \
679 coding->cmp_data_start = cmp_data->used; \
680 data[0] = -1; \
681 data[1] = cmp_data->char_offset + start; \
682 data[3] = (int) method; \
683 cmp_data->used += 4; \
684 } while (0)
685
686 /* Record the ending position END of the current composition. */
687
688 #define CODING_ADD_COMPOSITION_END(coding, end) \
689 do { \
690 struct composition_data *cmp_data = coding->cmp_data; \
691 int *data = cmp_data->data + coding->cmp_data_start; \
692 data[0] = cmp_data->used - coding->cmp_data_start; \
693 data[2] = cmp_data->char_offset + end; \
694 } while (0)
695
696 /* Record one COMPONENT (alternate character or composition rule). */
697
698 #define CODING_ADD_COMPOSITION_COMPONENT(coding, component) \
699 do { \
700 coding->cmp_data->data[coding->cmp_data->used++] = component; \
701 if (coding->cmp_data->used - coding->cmp_data_start \
702 == COMPOSITION_DATA_MAX_BUNCH_LENGTH) \
703 { \
704 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \
705 coding->composing = COMPOSITION_NO; \
706 } \
707 } while (0)
708
709
710 /* Get one byte from a data pointed by SRC and increment SRC. If SRC
711 is not less than SRC_END, return -1 without incrementing Src. */
712
713 #define SAFE_ONE_MORE_BYTE() (src >= src_end ? -1 : *src++)
714
715
716 /* Decode a character represented as a component of composition
717 sequence of Emacs 20 style at SRC. Set C to that character, store
718 its multibyte form sequence at P, and set P to the end of that
719 sequence. If no valid character is found, set C to -1. */
720
721 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(c, p) \
722 do { \
723 int bytes; \
724 \
725 c = SAFE_ONE_MORE_BYTE (); \
726 if (c < 0) \
727 break; \
728 if (CHAR_HEAD_P (c)) \
729 c = -1; \
730 else if (c == 0xA0) \
731 { \
732 c = SAFE_ONE_MORE_BYTE (); \
733 if (c < 0xA0) \
734 c = -1; \
735 else \
736 { \
737 c -= 0x80; \
738 *p++ = c; \
739 } \
740 } \
741 else if (BASE_LEADING_CODE_P (c - 0x20)) \
742 { \
743 unsigned char *p0 = p; \
744 \
745 c -= 0x20; \
746 *p++ = c; \
747 bytes = BYTES_BY_CHAR_HEAD (c); \
748 while (--bytes) \
749 { \
750 c = SAFE_ONE_MORE_BYTE (); \
751 if (c < 0) \
752 break; \
753 *p++ = c; \
754 } \
755 if (UNIBYTE_STR_AS_MULTIBYTE_P (p0, p - p0, bytes) \
756 || (coding->flags /* We are recovering a file. */ \
757 && p0[0] == LEADING_CODE_8_BIT_CONTROL \
758 && ! CHAR_HEAD_P (p0[1]))) \
759 c = STRING_CHAR (p0, bytes); \
760 else \
761 c = -1; \
762 } \
763 else \
764 c = -1; \
765 } while (0)
766
767
768 /* Decode a composition rule represented as a component of composition
769 sequence of Emacs 20 style at SRC. Set C to the rule. If not
770 valid rule is found, set C to -1. */
771
772 #define DECODE_EMACS_MULE_COMPOSITION_RULE(c) \
773 do { \
774 c = SAFE_ONE_MORE_BYTE (); \
775 c -= 0xA0; \
776 if (c < 0 || c >= 81) \
777 c = -1; \
778 else \
779 { \
780 gref = c / 9, nref = c % 9; \
781 c = COMPOSITION_ENCODE_RULE (gref, nref); \
782 } \
783 } while (0)
784
785
786 /* Decode composition sequence encoded by `emacs-mule' at the source
787 pointed by SRC. SRC_END is the end of source. Store information
788 of the composition in CODING->cmp_data.
789
790 For backward compatibility, decode also a composition sequence of
791 Emacs 20 style. In that case, the composition sequence contains
792 characters that should be extracted into a buffer or string. Store
793 those characters at *DESTINATION in multibyte form.
794
795 If we encounter an invalid byte sequence, return 0.
796 If we encounter an insufficient source or destination, or
797 insufficient space in CODING->cmp_data, return 1.
798 Otherwise, return consumed bytes in the source.
799
800 */
801 static INLINE int
802 decode_composition_emacs_mule (coding, src, src_end,
803 destination, dst_end, dst_bytes)
804 struct coding_system *coding;
805 const unsigned char *src, *src_end;
806 unsigned char **destination, *dst_end;
807 int dst_bytes;
808 {
809 unsigned char *dst = *destination;
810 int method, data_len, nchars;
811 const unsigned char *src_base = src++;
812 /* Store components of composition. */
813 int component[COMPOSITION_DATA_MAX_BUNCH_LENGTH];
814 int ncomponent;
815 /* Store multibyte form of characters to be composed. This is for
816 Emacs 20 style composition sequence. */
817 unsigned char buf[MAX_COMPOSITION_COMPONENTS * MAX_MULTIBYTE_LENGTH];
818 unsigned char *bufp = buf;
819 int c, i, gref, nref;
820
821 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
822 >= COMPOSITION_DATA_SIZE)
823 {
824 coding->result = CODING_FINISH_INSUFFICIENT_CMP;
825 return -1;
826 }
827
828 ONE_MORE_BYTE (c);
829 if (c - 0xF0 >= COMPOSITION_RELATIVE
830 && c - 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS)
831 {
832 int with_rule;
833
834 method = c - 0xF0;
835 with_rule = (method == COMPOSITION_WITH_RULE
836 || method == COMPOSITION_WITH_RULE_ALTCHARS);
837 ONE_MORE_BYTE (c);
838 data_len = c - 0xA0;
839 if (data_len < 4
840 || src_base + data_len > src_end)
841 return 0;
842 ONE_MORE_BYTE (c);
843 nchars = c - 0xA0;
844 if (c < 1)
845 return 0;
846 for (ncomponent = 0; src < src_base + data_len; ncomponent++)
847 {
848 /* If it is longer than this, it can't be valid. */
849 if (ncomponent >= COMPOSITION_DATA_MAX_BUNCH_LENGTH)
850 return 0;
851
852 if (ncomponent % 2 && with_rule)
853 {
854 ONE_MORE_BYTE (gref);
855 gref -= 32;
856 ONE_MORE_BYTE (nref);
857 nref -= 32;
858 c = COMPOSITION_ENCODE_RULE (gref, nref);
859 }
860 else
861 {
862 int bytes;
863 if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes)
864 || (coding->flags /* We are recovering a file. */
865 && src[0] == LEADING_CODE_8_BIT_CONTROL
866 && ! CHAR_HEAD_P (src[1])))
867 c = STRING_CHAR (src, bytes);
868 else
869 c = *src, bytes = 1;
870 src += bytes;
871 }
872 component[ncomponent] = c;
873 }
874 }
875 else if (c >= 0x80)
876 {
877 /* This may be an old Emacs 20 style format. See the comment at
878 the section 2 of this file. */
879 while (src < src_end && !CHAR_HEAD_P (*src)) src++;
880 if (src == src_end
881 && !(coding->mode & CODING_MODE_LAST_BLOCK))
882 goto label_end_of_loop;
883
884 src_end = src;
885 src = src_base + 1;
886 if (c < 0xC0)
887 {
888 method = COMPOSITION_RELATIVE;
889 for (ncomponent = 0; ncomponent < MAX_COMPOSITION_COMPONENTS;)
890 {
891 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
892 if (c < 0)
893 break;
894 component[ncomponent++] = c;
895 }
896 if (ncomponent < 2)
897 return 0;
898 nchars = ncomponent;
899 }
900 else if (c == 0xFF)
901 {
902 method = COMPOSITION_WITH_RULE;
903 src++;
904 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
905 if (c < 0)
906 return 0;
907 component[0] = c;
908 for (ncomponent = 1;
909 ncomponent < MAX_COMPOSITION_COMPONENTS * 2 - 1;)
910 {
911 DECODE_EMACS_MULE_COMPOSITION_RULE (c);
912 if (c < 0)
913 break;
914 component[ncomponent++] = c;
915 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
916 if (c < 0)
917 break;
918 component[ncomponent++] = c;
919 }
920 if (ncomponent < 3)
921 return 0;
922 nchars = (ncomponent + 1) / 2;
923 }
924 else
925 return 0;
926 }
927 else
928 return 0;
929
930 if (buf == bufp || dst + (bufp - buf) <= (dst_bytes ? dst_end : src))
931 {
932 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, method);
933 for (i = 0; i < ncomponent; i++)
934 CODING_ADD_COMPOSITION_COMPONENT (coding, component[i]);
935 CODING_ADD_COMPOSITION_END (coding, coding->produced_char + nchars);
936 if (buf < bufp)
937 {
938 unsigned char *p = buf;
939 EMIT_BYTES (p, bufp);
940 *destination += bufp - buf;
941 coding->produced_char += nchars;
942 }
943 return (src - src_base);
944 }
945 label_end_of_loop:
946 return -1;
947 }
948
949 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
950
951 static void
952 decode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
953 struct coding_system *coding;
954 const unsigned char *source;
955 unsigned char *destination;
956 int src_bytes, dst_bytes;
957 {
958 const unsigned char *src = source;
959 const unsigned char *src_end = source + src_bytes;
960 unsigned char *dst = destination;
961 unsigned char *dst_end = destination + dst_bytes;
962 /* SRC_BASE remembers the start position in source in each loop.
963 The loop will be exited when there's not enough source code, or
964 when there's not enough destination area to produce a
965 character. */
966 const unsigned char *src_base;
967
968 coding->produced_char = 0;
969 while ((src_base = src) < src_end)
970 {
971 unsigned char tmp[MAX_MULTIBYTE_LENGTH];
972 const unsigned char *p;
973 int bytes;
974
975 if (*src == '\r')
976 {
977 int c = *src++;
978
979 if (coding->eol_type == CODING_EOL_CR)
980 c = '\n';
981 else if (coding->eol_type == CODING_EOL_CRLF)
982 {
983 ONE_MORE_BYTE (c);
984 if (c != '\n')
985 {
986 src--;
987 c = '\r';
988 }
989 }
990 *dst++ = c;
991 coding->produced_char++;
992 continue;
993 }
994 else if (*src == '\n')
995 {
996 if ((coding->eol_type == CODING_EOL_CR
997 || coding->eol_type == CODING_EOL_CRLF)
998 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
999 {
1000 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1001 goto label_end_of_loop;
1002 }
1003 *dst++ = *src++;
1004 coding->produced_char++;
1005 continue;
1006 }
1007 else if (*src == 0x80 && coding->cmp_data)
1008 {
1009 /* Start of composition data. */
1010 int consumed = decode_composition_emacs_mule (coding, src, src_end,
1011 &dst, dst_end,
1012 dst_bytes);
1013 if (consumed < 0)
1014 goto label_end_of_loop;
1015 else if (consumed > 0)
1016 {
1017 src += consumed;
1018 continue;
1019 }
1020 bytes = CHAR_STRING (*src, tmp);
1021 p = tmp;
1022 src++;
1023 }
1024 else if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes)
1025 || (coding->flags /* We are recovering a file. */
1026 && src[0] == LEADING_CODE_8_BIT_CONTROL
1027 && ! CHAR_HEAD_P (src[1])))
1028 {
1029 p = src;
1030 src += bytes;
1031 }
1032 else
1033 {
1034 int i, c;
1035
1036 bytes = BYTES_BY_CHAR_HEAD (*src);
1037 src++;
1038 for (i = 1; i < bytes; i++)
1039 {
1040 ONE_MORE_BYTE (c);
1041 if (CHAR_HEAD_P (c))
1042 break;
1043 }
1044 if (i < bytes)
1045 {
1046 bytes = CHAR_STRING (*src_base, tmp);
1047 p = tmp;
1048 src = src_base + 1;
1049 }
1050 else
1051 {
1052 p = src_base;
1053 }
1054 }
1055 if (dst + bytes >= (dst_bytes ? dst_end : src))
1056 {
1057 coding->result = CODING_FINISH_INSUFFICIENT_DST;
1058 break;
1059 }
1060 while (bytes--) *dst++ = *p++;
1061 coding->produced_char++;
1062 }
1063 label_end_of_loop:
1064 coding->consumed = coding->consumed_char = src_base - source;
1065 coding->produced = dst - destination;
1066 }
1067
1068
1069 /* Encode composition data stored at DATA into a special byte sequence
1070 starting by 0x80. Update CODING->cmp_data_start and maybe
1071 CODING->cmp_data for the next call. */
1072
1073 #define ENCODE_COMPOSITION_EMACS_MULE(coding, data) \
1074 do { \
1075 unsigned char buf[1024], *p0 = buf, *p; \
1076 int len = data[0]; \
1077 int i; \
1078 \
1079 buf[0] = 0x80; \
1080 buf[1] = 0xF0 + data[3]; /* METHOD */ \
1081 buf[3] = 0xA0 + (data[2] - data[1]); /* COMPOSED-CHARS */ \
1082 p = buf + 4; \
1083 if (data[3] == COMPOSITION_WITH_RULE \
1084 || data[3] == COMPOSITION_WITH_RULE_ALTCHARS) \
1085 { \
1086 p += CHAR_STRING (data[4], p); \
1087 for (i = 5; i < len; i += 2) \
1088 { \
1089 int gref, nref; \
1090 COMPOSITION_DECODE_RULE (data[i], gref, nref); \
1091 *p++ = 0x20 + gref; \
1092 *p++ = 0x20 + nref; \
1093 p += CHAR_STRING (data[i + 1], p); \
1094 } \
1095 } \
1096 else \
1097 { \
1098 for (i = 4; i < len; i++) \
1099 p += CHAR_STRING (data[i], p); \
1100 } \
1101 buf[2] = 0xA0 + (p - buf); /* COMPONENTS-BYTES */ \
1102 \
1103 if (dst + (p - buf) + 4 > (dst_bytes ? dst_end : src)) \
1104 { \
1105 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
1106 goto label_end_of_loop; \
1107 } \
1108 while (p0 < p) \
1109 *dst++ = *p0++; \
1110 coding->cmp_data_start += data[0]; \
1111 if (coding->cmp_data_start == coding->cmp_data->used \
1112 && coding->cmp_data->next) \
1113 { \
1114 coding->cmp_data = coding->cmp_data->next; \
1115 coding->cmp_data_start = 0; \
1116 } \
1117 } while (0)
1118
1119
1120 static void encode_eol P_ ((struct coding_system *, const unsigned char *,
1121 unsigned char *, int, int));
1122
1123 static void
1124 encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1125 struct coding_system *coding;
1126 const unsigned char *source;
1127 unsigned char *destination;
1128 int src_bytes, dst_bytes;
1129 {
1130 const unsigned char *src = source;
1131 const unsigned char *src_end = source + src_bytes;
1132 unsigned char *dst = destination;
1133 unsigned char *dst_end = destination + dst_bytes;
1134 const unsigned char *src_base;
1135 int c;
1136 int char_offset;
1137 int *data;
1138
1139 Lisp_Object translation_table;
1140
1141 translation_table = Qnil;
1142
1143 /* Optimization for the case that there's no composition. */
1144 if (!coding->cmp_data || coding->cmp_data->used == 0)
1145 {
1146 encode_eol (coding, source, destination, src_bytes, dst_bytes);
1147 return;
1148 }
1149
1150 char_offset = coding->cmp_data->char_offset;
1151 data = coding->cmp_data->data + coding->cmp_data_start;
1152 while (1)
1153 {
1154 src_base = src;
1155
1156 /* If SRC starts a composition, encode the information about the
1157 composition in advance. */
1158 if (coding->cmp_data_start < coding->cmp_data->used
1159 && char_offset + coding->consumed_char == data[1])
1160 {
1161 ENCODE_COMPOSITION_EMACS_MULE (coding, data);
1162 char_offset = coding->cmp_data->char_offset;
1163 data = coding->cmp_data->data + coding->cmp_data_start;
1164 }
1165
1166 ONE_MORE_CHAR (c);
1167 if (c == '\n' && (coding->eol_type == CODING_EOL_CRLF
1168 || coding->eol_type == CODING_EOL_CR))
1169 {
1170 if (coding->eol_type == CODING_EOL_CRLF)
1171 EMIT_TWO_BYTES ('\r', c);
1172 else
1173 EMIT_ONE_BYTE ('\r');
1174 }
1175 else if (SINGLE_BYTE_CHAR_P (c))
1176 {
1177 if (coding->flags && ! ASCII_BYTE_P (c))
1178 {
1179 /* As we are auto saving, retain the multibyte form for
1180 8-bit chars. */
1181 unsigned char buf[MAX_MULTIBYTE_LENGTH];
1182 int bytes = CHAR_STRING (c, buf);
1183
1184 if (bytes == 1)
1185 EMIT_ONE_BYTE (buf[0]);
1186 else
1187 EMIT_TWO_BYTES (buf[0], buf[1]);
1188 }
1189 else
1190 EMIT_ONE_BYTE (c);
1191 }
1192 else
1193 EMIT_BYTES (src_base, src);
1194 coding->consumed_char++;
1195 }
1196 label_end_of_loop:
1197 coding->consumed = src_base - source;
1198 coding->produced = coding->produced_char = dst - destination;
1199 return;
1200 }
1201
1202 \f
1203 /*** 3. ISO2022 handlers ***/
1204
1205 /* The following note describes the coding system ISO2022 briefly.
1206 Since the intention of this note is to help understand the
1207 functions in this file, some parts are NOT ACCURATE or are OVERLY
1208 SIMPLIFIED. For thorough understanding, please refer to the
1209 original document of ISO2022. This is equivalent to the standard
1210 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
1211
1212 ISO2022 provides many mechanisms to encode several character sets
1213 in 7-bit and 8-bit environments. For 7-bit environments, all text
1214 is encoded using bytes less than 128. This may make the encoded
1215 text a little bit longer, but the text passes more easily through
1216 several types of gateway, some of which strip off the MSB (Most
1217 Significant Bit).
1218
1219 There are two kinds of character sets: control character sets and
1220 graphic character sets. The former contain control characters such
1221 as `newline' and `escape' to provide control functions (control
1222 functions are also provided by escape sequences). The latter
1223 contain graphic characters such as 'A' and '-'. Emacs recognizes
1224 two control character sets and many graphic character sets.
1225
1226 Graphic character sets are classified into one of the following
1227 four classes, according to the number of bytes (DIMENSION) and
1228 number of characters in one dimension (CHARS) of the set:
1229 - DIMENSION1_CHARS94
1230 - DIMENSION1_CHARS96
1231 - DIMENSION2_CHARS94
1232 - DIMENSION2_CHARS96
1233
1234 In addition, each character set is assigned an identification tag,
1235 unique for each set, called the "final character" (denoted as <F>
1236 hereafter). The <F> of each character set is decided by ECMA(*)
1237 when it is registered in ISO. The code range of <F> is 0x30..0x7F
1238 (0x30..0x3F are for private use only).
1239
1240 Note (*): ECMA = European Computer Manufacturers Association
1241
1242 Here are examples of graphic character sets [NAME(<F>)]:
1243 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
1244 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
1245 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
1246 o DIMENSION2_CHARS96 -- none for the moment
1247
1248 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
1249 C0 [0x00..0x1F] -- control character plane 0
1250 GL [0x20..0x7F] -- graphic character plane 0
1251 C1 [0x80..0x9F] -- control character plane 1
1252 GR [0xA0..0xFF] -- graphic character plane 1
1253
1254 A control character set is directly designated and invoked to C0 or
1255 C1 by an escape sequence. The most common case is that:
1256 - ISO646's control character set is designated/invoked to C0, and
1257 - ISO6429's control character set is designated/invoked to C1,
1258 and usually these designations/invocations are omitted in encoded
1259 text. In a 7-bit environment, only C0 can be used, and a control
1260 character for C1 is encoded by an appropriate escape sequence to
1261 fit into the environment. All control characters for C1 are
1262 defined to have corresponding escape sequences.
1263
1264 A graphic character set is at first designated to one of four
1265 graphic registers (G0 through G3), then these graphic registers are
1266 invoked to GL or GR. These designations and invocations can be
1267 done independently. The most common case is that G0 is invoked to
1268 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
1269 these invocations and designations are omitted in encoded text.
1270 In a 7-bit environment, only GL can be used.
1271
1272 When a graphic character set of CHARS94 is invoked to GL, codes
1273 0x20 and 0x7F of the GL area work as control characters SPACE and
1274 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
1275 be used.
1276
1277 There are two ways of invocation: locking-shift and single-shift.
1278 With locking-shift, the invocation lasts until the next different
1279 invocation, whereas with single-shift, the invocation affects the
1280 following character only and doesn't affect the locking-shift
1281 state. Invocations are done by the following control characters or
1282 escape sequences:
1283
1284 ----------------------------------------------------------------------
1285 abbrev function cntrl escape seq description
1286 ----------------------------------------------------------------------
1287 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
1288 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
1289 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
1290 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
1291 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
1292 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
1293 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
1294 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
1295 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
1296 ----------------------------------------------------------------------
1297 (*) These are not used by any known coding system.
1298
1299 Control characters for these functions are defined by macros
1300 ISO_CODE_XXX in `coding.h'.
1301
1302 Designations are done by the following escape sequences:
1303 ----------------------------------------------------------------------
1304 escape sequence description
1305 ----------------------------------------------------------------------
1306 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
1307 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
1308 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
1309 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
1310 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
1311 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
1312 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
1313 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
1314 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
1315 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
1316 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
1317 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
1318 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
1319 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
1320 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
1321 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
1322 ----------------------------------------------------------------------
1323
1324 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
1325 of dimension 1, chars 94, and final character <F>, etc...
1326
1327 Note (*): Although these designations are not allowed in ISO2022,
1328 Emacs accepts them on decoding, and produces them on encoding
1329 CHARS96 character sets in a coding system which is characterized as
1330 7-bit environment, non-locking-shift, and non-single-shift.
1331
1332 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
1333 '(' can be omitted. We refer to this as "short-form" hereafter.
1334
1335 Now you may notice that there are a lot of ways of encoding the
1336 same multilingual text in ISO2022. Actually, there exist many
1337 coding systems such as Compound Text (used in X11's inter client
1338 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
1339 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
1340 localized platforms), and all of these are variants of ISO2022.
1341
1342 In addition to the above, Emacs handles two more kinds of escape
1343 sequences: ISO6429's direction specification and Emacs' private
1344 sequence for specifying character composition.
1345
1346 ISO6429's direction specification takes the following form:
1347 o CSI ']' -- end of the current direction
1348 o CSI '0' ']' -- end of the current direction
1349 o CSI '1' ']' -- start of left-to-right text
1350 o CSI '2' ']' -- start of right-to-left text
1351 The control character CSI (0x9B: control sequence introducer) is
1352 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
1353
1354 Character composition specification takes the following form:
1355 o ESC '0' -- start relative composition
1356 o ESC '1' -- end composition
1357 o ESC '2' -- start rule-base composition (*)
1358 o ESC '3' -- start relative composition with alternate chars (**)
1359 o ESC '4' -- start rule-base composition with alternate chars (**)
1360 Since these are not standard escape sequences of any ISO standard,
1361 the use of them with these meanings is restricted to Emacs only.
1362
1363 (*) This form is used only in Emacs 20.5 and older versions,
1364 but the newer versions can safely decode it.
1365 (**) This form is used only in Emacs 21.1 and newer versions,
1366 and the older versions can't decode it.
1367
1368 Here's a list of example usages of these composition escape
1369 sequences (categorized by `enum composition_method').
1370
1371 COMPOSITION_RELATIVE:
1372 ESC 0 CHAR [ CHAR ] ESC 1
1373 COMPOSITION_WITH_RULE:
1374 ESC 2 CHAR [ RULE CHAR ] ESC 1
1375 COMPOSITION_WITH_ALTCHARS:
1376 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
1377 COMPOSITION_WITH_RULE_ALTCHARS:
1378 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
1379
1380 enum iso_code_class_type iso_code_class[256];
1381
1382 #define CHARSET_OK(idx, charset, c) \
1383 (coding_system_table[idx] \
1384 && (charset == CHARSET_ASCII \
1385 || (safe_chars = coding_safe_chars (coding_system_table[idx]->symbol), \
1386 CODING_SAFE_CHAR_P (safe_chars, c))) \
1387 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding_system_table[idx], \
1388 charset) \
1389 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
1390
1391 #define SHIFT_OUT_OK(idx) \
1392 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
1393
1394 #define COMPOSITION_OK(idx) \
1395 (coding_system_table[idx]->composing != COMPOSITION_DISABLED)
1396
1397 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1398 Check if a text is encoded in ISO2022. If it is, return an
1399 integer in which appropriate flag bits any of:
1400 CODING_CATEGORY_MASK_ISO_7
1401 CODING_CATEGORY_MASK_ISO_7_TIGHT
1402 CODING_CATEGORY_MASK_ISO_8_1
1403 CODING_CATEGORY_MASK_ISO_8_2
1404 CODING_CATEGORY_MASK_ISO_7_ELSE
1405 CODING_CATEGORY_MASK_ISO_8_ELSE
1406 are set. If a code which should never appear in ISO2022 is found,
1407 returns 0. */
1408
1409 static int
1410 detect_coding_iso2022 (src, src_end, multibytep)
1411 unsigned char *src, *src_end;
1412 int multibytep;
1413 {
1414 int mask = CODING_CATEGORY_MASK_ISO;
1415 int mask_found = 0;
1416 int reg[4], shift_out = 0, single_shifting = 0;
1417 int c, c1, charset;
1418 /* Dummy for ONE_MORE_BYTE. */
1419 struct coding_system dummy_coding;
1420 struct coding_system *coding = &dummy_coding;
1421 Lisp_Object safe_chars;
1422
1423 reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1;
1424 while (mask)
1425 {
1426 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, mask & mask_found);
1427 retry:
1428 switch (c)
1429 {
1430 case ISO_CODE_ESC:
1431 if (inhibit_iso_escape_detection)
1432 break;
1433 single_shifting = 0;
1434 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, mask & mask_found);
1435 if (c >= '(' && c <= '/')
1436 {
1437 /* Designation sequence for a charset of dimension 1. */
1438 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep, mask & mask_found);
1439 if (c1 < ' ' || c1 >= 0x80
1440 || (charset = iso_charset_table[0][c >= ','][c1]) < 0)
1441 /* Invalid designation sequence. Just ignore. */
1442 break;
1443 reg[(c - '(') % 4] = charset;
1444 }
1445 else if (c == '$')
1446 {
1447 /* Designation sequence for a charset of dimension 2. */
1448 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, mask & mask_found);
1449 if (c >= '@' && c <= 'B')
1450 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
1451 reg[0] = charset = iso_charset_table[1][0][c];
1452 else if (c >= '(' && c <= '/')
1453 {
1454 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep,
1455 mask & mask_found);
1456 if (c1 < ' ' || c1 >= 0x80
1457 || (charset = iso_charset_table[1][c >= ','][c1]) < 0)
1458 /* Invalid designation sequence. Just ignore. */
1459 break;
1460 reg[(c - '(') % 4] = charset;
1461 }
1462 else
1463 /* Invalid designation sequence. Just ignore. */
1464 break;
1465 }
1466 else if (c == 'N' || c == 'O')
1467 {
1468 /* ESC <Fe> for SS2 or SS3. */
1469 mask &= CODING_CATEGORY_MASK_ISO_7_ELSE;
1470 break;
1471 }
1472 else if (c >= '0' && c <= '4')
1473 {
1474 /* ESC <Fp> for start/end composition. */
1475 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7))
1476 mask_found |= CODING_CATEGORY_MASK_ISO_7;
1477 else
1478 mask &= ~CODING_CATEGORY_MASK_ISO_7;
1479 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT))
1480 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT;
1481 else
1482 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT;
1483 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_1))
1484 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
1485 else
1486 mask &= ~CODING_CATEGORY_MASK_ISO_8_1;
1487 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_2))
1488 mask_found |= CODING_CATEGORY_MASK_ISO_8_2;
1489 else
1490 mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
1491 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7_ELSE))
1492 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE;
1493 else
1494 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE;
1495 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_ELSE))
1496 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE;
1497 else
1498 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE;
1499 break;
1500 }
1501 else
1502 /* Invalid escape sequence. Just ignore. */
1503 break;
1504
1505 /* We found a valid designation sequence for CHARSET. */
1506 mask &= ~CODING_CATEGORY_MASK_ISO_8BIT;
1507 c = MAKE_CHAR (charset, 0, 0);
1508 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset, c))
1509 mask_found |= CODING_CATEGORY_MASK_ISO_7;
1510 else
1511 mask &= ~CODING_CATEGORY_MASK_ISO_7;
1512 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset, c))
1513 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT;
1514 else
1515 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT;
1516 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset, c))
1517 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE;
1518 else
1519 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE;
1520 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset, c))
1521 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE;
1522 else
1523 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE;
1524 break;
1525
1526 case ISO_CODE_SO:
1527 if (inhibit_iso_escape_detection)
1528 break;
1529 single_shifting = 0;
1530 if (shift_out == 0
1531 && (reg[1] >= 0
1532 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE)
1533 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE)))
1534 {
1535 /* Locking shift out. */
1536 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1537 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1538 }
1539 break;
1540
1541 case ISO_CODE_SI:
1542 if (inhibit_iso_escape_detection)
1543 break;
1544 single_shifting = 0;
1545 if (shift_out == 1)
1546 {
1547 /* Locking shift in. */
1548 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1549 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1550 }
1551 break;
1552
1553 case ISO_CODE_CSI:
1554 single_shifting = 0;
1555 case ISO_CODE_SS2:
1556 case ISO_CODE_SS3:
1557 {
1558 int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE;
1559
1560 if (inhibit_iso_escape_detection)
1561 break;
1562 if (c != ISO_CODE_CSI)
1563 {
1564 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1565 & CODING_FLAG_ISO_SINGLE_SHIFT)
1566 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1567 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1568 & CODING_FLAG_ISO_SINGLE_SHIFT)
1569 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1570 single_shifting = 1;
1571 }
1572 if (VECTORP (Vlatin_extra_code_table)
1573 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1574 {
1575 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1576 & CODING_FLAG_ISO_LATIN_EXTRA)
1577 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1578 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1579 & CODING_FLAG_ISO_LATIN_EXTRA)
1580 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1581 }
1582 mask &= newmask;
1583 mask_found |= newmask;
1584 }
1585 break;
1586
1587 default:
1588 if (c < 0x80)
1589 {
1590 single_shifting = 0;
1591 break;
1592 }
1593 else if (c < 0xA0)
1594 {
1595 single_shifting = 0;
1596 if (VECTORP (Vlatin_extra_code_table)
1597 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1598 {
1599 int newmask = 0;
1600
1601 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1602 & CODING_FLAG_ISO_LATIN_EXTRA)
1603 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1604 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1605 & CODING_FLAG_ISO_LATIN_EXTRA)
1606 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1607 mask &= newmask;
1608 mask_found |= newmask;
1609 }
1610 else
1611 return 0;
1612 }
1613 else
1614 {
1615 mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT
1616 | CODING_CATEGORY_MASK_ISO_7_ELSE);
1617 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
1618 /* Check the length of succeeding codes of the range
1619 0xA0..0FF. If the byte length is odd, we exclude
1620 CODING_CATEGORY_MASK_ISO_8_2. We can check this only
1621 when we are not single shifting. */
1622 if (!single_shifting
1623 && mask & CODING_CATEGORY_MASK_ISO_8_2)
1624 {
1625 int i = 1;
1626
1627 c = -1;
1628 while (src < src_end)
1629 {
1630 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep,
1631 mask & mask_found);
1632 if (c < 0xA0)
1633 break;
1634 i++;
1635 }
1636
1637 if (i & 1 && src < src_end)
1638 mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
1639 else
1640 mask_found |= CODING_CATEGORY_MASK_ISO_8_2;
1641 if (c >= 0)
1642 /* This means that we have read one extra byte. */
1643 goto retry;
1644 }
1645 }
1646 break;
1647 }
1648 }
1649 return (mask & mask_found);
1650 }
1651
1652 /* Decode a character of which charset is CHARSET, the 1st position
1653 code is C1, the 2nd position code is C2, and return the decoded
1654 character code. If the variable `translation_table' is non-nil,
1655 returned the translated code. */
1656
1657 #define DECODE_ISO_CHARACTER(charset, c1, c2) \
1658 (NILP (translation_table) \
1659 ? MAKE_CHAR (charset, c1, c2) \
1660 : translate_char (translation_table, -1, charset, c1, c2))
1661
1662 /* Set designation state into CODING. */
1663 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
1664 do { \
1665 int charset, c; \
1666 \
1667 if (final_char < '0' || final_char >= 128) \
1668 goto label_invalid_code; \
1669 charset = ISO_CHARSET_TABLE (make_number (dimension), \
1670 make_number (chars), \
1671 make_number (final_char)); \
1672 c = MAKE_CHAR (charset, 0, 0); \
1673 if (charset >= 0 \
1674 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
1675 || CODING_SAFE_CHAR_P (safe_chars, c))) \
1676 { \
1677 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
1678 && reg == 0 \
1679 && charset == CHARSET_ASCII) \
1680 { \
1681 /* We should insert this designation sequence as is so \
1682 that it is surely written back to a file. */ \
1683 coding->spec.iso2022.last_invalid_designation_register = -1; \
1684 goto label_invalid_code; \
1685 } \
1686 coding->spec.iso2022.last_invalid_designation_register = -1; \
1687 if ((coding->mode & CODING_MODE_DIRECTION) \
1688 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
1689 charset = CHARSET_REVERSE_CHARSET (charset); \
1690 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1691 } \
1692 else \
1693 { \
1694 coding->spec.iso2022.last_invalid_designation_register = reg; \
1695 goto label_invalid_code; \
1696 } \
1697 } while (0)
1698
1699 /* Allocate a memory block for storing information about compositions.
1700 The block is chained to the already allocated blocks. */
1701
1702 void
1703 coding_allocate_composition_data (coding, char_offset)
1704 struct coding_system *coding;
1705 int char_offset;
1706 {
1707 struct composition_data *cmp_data
1708 = (struct composition_data *) xmalloc (sizeof *cmp_data);
1709
1710 cmp_data->char_offset = char_offset;
1711 cmp_data->used = 0;
1712 cmp_data->prev = coding->cmp_data;
1713 cmp_data->next = NULL;
1714 if (coding->cmp_data)
1715 coding->cmp_data->next = cmp_data;
1716 coding->cmp_data = cmp_data;
1717 coding->cmp_data_start = 0;
1718 coding->composing = COMPOSITION_NO;
1719 }
1720
1721 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
1722 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
1723 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
1724 ESC 3 : altchar composition : ESC 3 ALT ... ESC 0 CHAR ... ESC 1
1725 ESC 4 : alt&rule composition : ESC 4 ALT RULE .. ALT ESC 0 CHAR ... ESC 1
1726 */
1727
1728 #define DECODE_COMPOSITION_START(c1) \
1729 do { \
1730 if (coding->composing == COMPOSITION_DISABLED) \
1731 { \
1732 *dst++ = ISO_CODE_ESC; \
1733 *dst++ = c1 & 0x7f; \
1734 coding->produced_char += 2; \
1735 } \
1736 else if (!COMPOSING_P (coding)) \
1737 { \
1738 /* This is surely the start of a composition. We must be sure \
1739 that coding->cmp_data has enough space to store the \
1740 information about the composition. If not, terminate the \
1741 current decoding loop, allocate one more memory block for \
1742 coding->cmp_data in the caller, then start the decoding \
1743 loop again. We can't allocate memory here directly because \
1744 it may cause buffer/string relocation. */ \
1745 if (!coding->cmp_data \
1746 || (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH \
1747 >= COMPOSITION_DATA_SIZE)) \
1748 { \
1749 coding->result = CODING_FINISH_INSUFFICIENT_CMP; \
1750 goto label_end_of_loop; \
1751 } \
1752 coding->composing = (c1 == '0' ? COMPOSITION_RELATIVE \
1753 : c1 == '2' ? COMPOSITION_WITH_RULE \
1754 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
1755 : COMPOSITION_WITH_RULE_ALTCHARS); \
1756 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, \
1757 coding->composing); \
1758 coding->composition_rule_follows = 0; \
1759 } \
1760 else \
1761 { \
1762 /* We are already handling a composition. If the method is \
1763 the following two, the codes following the current escape \
1764 sequence are actual characters stored in a buffer. */ \
1765 if (coding->composing == COMPOSITION_WITH_ALTCHARS \
1766 || coding->composing == COMPOSITION_WITH_RULE_ALTCHARS) \
1767 { \
1768 coding->composing = COMPOSITION_RELATIVE; \
1769 coding->composition_rule_follows = 0; \
1770 } \
1771 } \
1772 } while (0)
1773
1774 /* Handle composition end sequence ESC 1. */
1775
1776 #define DECODE_COMPOSITION_END(c1) \
1777 do { \
1778 if (! COMPOSING_P (coding)) \
1779 { \
1780 *dst++ = ISO_CODE_ESC; \
1781 *dst++ = c1; \
1782 coding->produced_char += 2; \
1783 } \
1784 else \
1785 { \
1786 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \
1787 coding->composing = COMPOSITION_NO; \
1788 } \
1789 } while (0)
1790
1791 /* Decode a composition rule from the byte C1 (and maybe one more byte
1792 from SRC) and store one encoded composition rule in
1793 coding->cmp_data. */
1794
1795 #define DECODE_COMPOSITION_RULE(c1) \
1796 do { \
1797 int rule = 0; \
1798 (c1) -= 32; \
1799 if (c1 < 81) /* old format (before ver.21) */ \
1800 { \
1801 int gref = (c1) / 9; \
1802 int nref = (c1) % 9; \
1803 if (gref == 4) gref = 10; \
1804 if (nref == 4) nref = 10; \
1805 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
1806 } \
1807 else if (c1 < 93) /* new format (after ver.21) */ \
1808 { \
1809 ONE_MORE_BYTE (c2); \
1810 rule = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
1811 } \
1812 CODING_ADD_COMPOSITION_COMPONENT (coding, rule); \
1813 coding->composition_rule_follows = 0; \
1814 } while (0)
1815
1816
1817 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1818
1819 static void
1820 decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
1821 struct coding_system *coding;
1822 const unsigned char *source;
1823 unsigned char *destination;
1824 int src_bytes, dst_bytes;
1825 {
1826 const unsigned char *src = source;
1827 const unsigned char *src_end = source + src_bytes;
1828 unsigned char *dst = destination;
1829 unsigned char *dst_end = destination + dst_bytes;
1830 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1831 int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1832 int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
1833 /* SRC_BASE remembers the start position in source in each loop.
1834 The loop will be exited when there's not enough source code
1835 (within macro ONE_MORE_BYTE), or when there's not enough
1836 destination area to produce a character (within macro
1837 EMIT_CHAR). */
1838 const unsigned char *src_base;
1839 int c, charset;
1840 Lisp_Object translation_table;
1841 Lisp_Object safe_chars;
1842
1843 safe_chars = coding_safe_chars (coding->symbol);
1844
1845 if (NILP (Venable_character_translation))
1846 translation_table = Qnil;
1847 else
1848 {
1849 translation_table = coding->translation_table_for_decode;
1850 if (NILP (translation_table))
1851 translation_table = Vstandard_translation_table_for_decode;
1852 }
1853
1854 coding->result = CODING_FINISH_NORMAL;
1855
1856 while (1)
1857 {
1858 int c1, c2 = 0;
1859
1860 src_base = src;
1861 ONE_MORE_BYTE (c1);
1862
1863 /* We produce no character or one character. */
1864 switch (iso_code_class [c1])
1865 {
1866 case ISO_0x20_or_0x7F:
1867 if (COMPOSING_P (coding) && coding->composition_rule_follows)
1868 {
1869 DECODE_COMPOSITION_RULE (c1);
1870 continue;
1871 }
1872 if (charset0 < 0 || CHARSET_CHARS (charset0) == 94)
1873 {
1874 /* This is SPACE or DEL. */
1875 charset = CHARSET_ASCII;
1876 break;
1877 }
1878 /* This is a graphic character, we fall down ... */
1879
1880 case ISO_graphic_plane_0:
1881 if (COMPOSING_P (coding) && coding->composition_rule_follows)
1882 {
1883 DECODE_COMPOSITION_RULE (c1);
1884 continue;
1885 }
1886 charset = charset0;
1887 break;
1888
1889 case ISO_0xA0_or_0xFF:
1890 if (charset1 < 0 || CHARSET_CHARS (charset1) == 94
1891 || coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
1892 goto label_invalid_code;
1893 /* This is a graphic character, we fall down ... */
1894
1895 case ISO_graphic_plane_1:
1896 if (charset1 < 0)
1897 goto label_invalid_code;
1898 charset = charset1;
1899 break;
1900
1901 case ISO_control_0:
1902 if (COMPOSING_P (coding))
1903 DECODE_COMPOSITION_END ('1');
1904
1905 /* All ISO2022 control characters in this class have the
1906 same representation in Emacs internal format. */
1907 if (c1 == '\n'
1908 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1909 && (coding->eol_type == CODING_EOL_CR
1910 || coding->eol_type == CODING_EOL_CRLF))
1911 {
1912 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1913 goto label_end_of_loop;
1914 }
1915 charset = CHARSET_ASCII;
1916 break;
1917
1918 case ISO_control_1:
1919 if (COMPOSING_P (coding))
1920 DECODE_COMPOSITION_END ('1');
1921 goto label_invalid_code;
1922
1923 case ISO_carriage_return:
1924 if (COMPOSING_P (coding))
1925 DECODE_COMPOSITION_END ('1');
1926
1927 if (coding->eol_type == CODING_EOL_CR)
1928 c1 = '\n';
1929 else if (coding->eol_type == CODING_EOL_CRLF)
1930 {
1931 ONE_MORE_BYTE (c1);
1932 if (c1 != ISO_CODE_LF)
1933 {
1934 src--;
1935 c1 = '\r';
1936 }
1937 }
1938 charset = CHARSET_ASCII;
1939 break;
1940
1941 case ISO_shift_out:
1942 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1943 || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0)
1944 goto label_invalid_code;
1945 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1;
1946 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1947 continue;
1948
1949 case ISO_shift_in:
1950 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
1951 goto label_invalid_code;
1952 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
1953 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1954 continue;
1955
1956 case ISO_single_shift_2_7:
1957 case ISO_single_shift_2:
1958 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1959 goto label_invalid_code;
1960 /* SS2 is handled as an escape sequence of ESC 'N' */
1961 c1 = 'N';
1962 goto label_escape_sequence;
1963
1964 case ISO_single_shift_3:
1965 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1966 goto label_invalid_code;
1967 /* SS2 is handled as an escape sequence of ESC 'O' */
1968 c1 = 'O';
1969 goto label_escape_sequence;
1970
1971 case ISO_control_sequence_introducer:
1972 /* CSI is handled as an escape sequence of ESC '[' ... */
1973 c1 = '[';
1974 goto label_escape_sequence;
1975
1976 case ISO_escape:
1977 ONE_MORE_BYTE (c1);
1978 label_escape_sequence:
1979 /* Escape sequences handled by Emacs are invocation,
1980 designation, direction specification, and character
1981 composition specification. */
1982 switch (c1)
1983 {
1984 case '&': /* revision of following character set */
1985 ONE_MORE_BYTE (c1);
1986 if (!(c1 >= '@' && c1 <= '~'))
1987 goto label_invalid_code;
1988 ONE_MORE_BYTE (c1);
1989 if (c1 != ISO_CODE_ESC)
1990 goto label_invalid_code;
1991 ONE_MORE_BYTE (c1);
1992 goto label_escape_sequence;
1993
1994 case '$': /* designation of 2-byte character set */
1995 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1996 goto label_invalid_code;
1997 ONE_MORE_BYTE (c1);
1998 if (c1 >= '@' && c1 <= 'B')
1999 { /* designation of JISX0208.1978, GB2312.1980,
2000 or JISX0208.1980 */
2001 DECODE_DESIGNATION (0, 2, 94, c1);
2002 }
2003 else if (c1 >= 0x28 && c1 <= 0x2B)
2004 { /* designation of DIMENSION2_CHARS94 character set */
2005 ONE_MORE_BYTE (c2);
2006 DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2);
2007 }
2008 else if (c1 >= 0x2C && c1 <= 0x2F)
2009 { /* designation of DIMENSION2_CHARS96 character set */
2010 ONE_MORE_BYTE (c2);
2011 DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2);
2012 }
2013 else
2014 goto label_invalid_code;
2015 /* We must update these variables now. */
2016 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
2017 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
2018 continue;
2019
2020 case 'n': /* invocation of locking-shift-2 */
2021 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
2022 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
2023 goto label_invalid_code;
2024 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2;
2025 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
2026 continue;
2027
2028 case 'o': /* invocation of locking-shift-3 */
2029 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
2030 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
2031 goto label_invalid_code;
2032 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3;
2033 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
2034 continue;
2035
2036 case 'N': /* invocation of single-shift-2 */
2037 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2038 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
2039 goto label_invalid_code;
2040 charset = CODING_SPEC_ISO_DESIGNATION (coding, 2);
2041 ONE_MORE_BYTE (c1);
2042 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
2043 goto label_invalid_code;
2044 break;
2045
2046 case 'O': /* invocation of single-shift-3 */
2047 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2048 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
2049 goto label_invalid_code;
2050 charset = CODING_SPEC_ISO_DESIGNATION (coding, 3);
2051 ONE_MORE_BYTE (c1);
2052 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
2053 goto label_invalid_code;
2054 break;
2055
2056 case '0': case '2': case '3': case '4': /* start composition */
2057 DECODE_COMPOSITION_START (c1);
2058 continue;
2059
2060 case '1': /* end composition */
2061 DECODE_COMPOSITION_END (c1);
2062 continue;
2063
2064 case '[': /* specification of direction */
2065 if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION)
2066 goto label_invalid_code;
2067 /* For the moment, nested direction is not supported.
2068 So, `coding->mode & CODING_MODE_DIRECTION' zero means
2069 left-to-right, and nonzero means right-to-left. */
2070 ONE_MORE_BYTE (c1);
2071 switch (c1)
2072 {
2073 case ']': /* end of the current direction */
2074 coding->mode &= ~CODING_MODE_DIRECTION;
2075
2076 case '0': /* end of the current direction */
2077 case '1': /* start of left-to-right direction */
2078 ONE_MORE_BYTE (c1);
2079 if (c1 == ']')
2080 coding->mode &= ~CODING_MODE_DIRECTION;
2081 else
2082 goto label_invalid_code;
2083 break;
2084
2085 case '2': /* start of right-to-left direction */
2086 ONE_MORE_BYTE (c1);
2087 if (c1 == ']')
2088 coding->mode |= CODING_MODE_DIRECTION;
2089 else
2090 goto label_invalid_code;
2091 break;
2092
2093 default:
2094 goto label_invalid_code;
2095 }
2096 continue;
2097
2098 case '%':
2099 if (COMPOSING_P (coding))
2100 DECODE_COMPOSITION_END ('1');
2101 ONE_MORE_BYTE (c1);
2102 if (c1 == '/')
2103 {
2104 /* CTEXT extended segment:
2105 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
2106 We keep these bytes as is for the moment.
2107 They may be decoded by post-read-conversion. */
2108 int dim, M, L;
2109 int size, required;
2110 int produced_chars;
2111
2112 ONE_MORE_BYTE (dim);
2113 ONE_MORE_BYTE (M);
2114 ONE_MORE_BYTE (L);
2115 size = ((M - 128) * 128) + (L - 128);
2116 required = 8 + size * 2;
2117 if (dst + required > (dst_bytes ? dst_end : src))
2118 goto label_end_of_loop;
2119 *dst++ = ISO_CODE_ESC;
2120 *dst++ = '%';
2121 *dst++ = '/';
2122 *dst++ = dim;
2123 produced_chars = 4;
2124 dst += CHAR_STRING (M, dst), produced_chars++;
2125 dst += CHAR_STRING (L, dst), produced_chars++;
2126 while (size-- > 0)
2127 {
2128 ONE_MORE_BYTE (c1);
2129 dst += CHAR_STRING (c1, dst), produced_chars++;
2130 }
2131 coding->produced_char += produced_chars;
2132 }
2133 else if (c1 == 'G')
2134 {
2135 unsigned char *d = dst;
2136 int produced_chars;
2137
2138 /* XFree86 extension for embedding UTF-8 in CTEXT:
2139 ESC % G --UTF-8-BYTES-- ESC % @
2140 We keep these bytes as is for the moment.
2141 They may be decoded by post-read-conversion. */
2142 if (d + 6 > (dst_bytes ? dst_end : src))
2143 goto label_end_of_loop;
2144 *d++ = ISO_CODE_ESC;
2145 *d++ = '%';
2146 *d++ = 'G';
2147 produced_chars = 3;
2148 while (d + 1 < (dst_bytes ? dst_end : src))
2149 {
2150 ONE_MORE_BYTE (c1);
2151 if (c1 == ISO_CODE_ESC
2152 && src + 1 < src_end
2153 && src[0] == '%'
2154 && src[1] == '@')
2155 {
2156 src += 2;
2157 break;
2158 }
2159 d += CHAR_STRING (c1, d), produced_chars++;
2160 }
2161 if (d + 3 > (dst_bytes ? dst_end : src))
2162 goto label_end_of_loop;
2163 *d++ = ISO_CODE_ESC;
2164 *d++ = '%';
2165 *d++ = '@';
2166 dst = d;
2167 coding->produced_char += produced_chars + 3;
2168 }
2169 else
2170 goto label_invalid_code;
2171 continue;
2172
2173 default:
2174 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
2175 goto label_invalid_code;
2176 if (c1 >= 0x28 && c1 <= 0x2B)
2177 { /* designation of DIMENSION1_CHARS94 character set */
2178 ONE_MORE_BYTE (c2);
2179 DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2);
2180 }
2181 else if (c1 >= 0x2C && c1 <= 0x2F)
2182 { /* designation of DIMENSION1_CHARS96 character set */
2183 ONE_MORE_BYTE (c2);
2184 DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2);
2185 }
2186 else
2187 goto label_invalid_code;
2188 /* We must update these variables now. */
2189 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
2190 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
2191 continue;
2192 }
2193 }
2194
2195 /* Now we know CHARSET and 1st position code C1 of a character.
2196 Produce a multibyte sequence for that character while getting
2197 2nd position code C2 if necessary. */
2198 if (CHARSET_DIMENSION (charset) == 2)
2199 {
2200 ONE_MORE_BYTE (c2);
2201 if (c1 < 0x80 ? c2 < 0x20 || c2 >= 0x80 : c2 < 0xA0)
2202 /* C2 is not in a valid range. */
2203 goto label_invalid_code;
2204 }
2205 c = DECODE_ISO_CHARACTER (charset, c1, c2);
2206 EMIT_CHAR (c);
2207 continue;
2208
2209 label_invalid_code:
2210 coding->errors++;
2211 if (COMPOSING_P (coding))
2212 DECODE_COMPOSITION_END ('1');
2213 src = src_base;
2214 c = *src++;
2215 if (! NILP (translation_table))
2216 c = translate_char (translation_table, c, 0, 0, 0);
2217 EMIT_CHAR (c);
2218 }
2219
2220 label_end_of_loop:
2221 coding->consumed = coding->consumed_char = src_base - source;
2222 coding->produced = dst - destination;
2223 return;
2224 }
2225
2226
2227 /* ISO2022 encoding stuff. */
2228
2229 /*
2230 It is not enough to say just "ISO2022" on encoding, we have to
2231 specify more details. In Emacs, each ISO2022 coding system
2232 variant has the following specifications:
2233 1. Initial designation to G0 through G3.
2234 2. Allows short-form designation?
2235 3. ASCII should be designated to G0 before control characters?
2236 4. ASCII should be designated to G0 at end of line?
2237 5. 7-bit environment or 8-bit environment?
2238 6. Use locking-shift?
2239 7. Use Single-shift?
2240 And the following two are only for Japanese:
2241 8. Use ASCII in place of JIS0201-1976-Roman?
2242 9. Use JISX0208-1983 in place of JISX0208-1978?
2243 These specifications are encoded in `coding->flags' as flag bits
2244 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
2245 details.
2246 */
2247
2248 /* Produce codes (escape sequence) for designating CHARSET to graphic
2249 register REG at DST, and increment DST. If <final-char> of CHARSET is
2250 '@', 'A', or 'B' and the coding system CODING allows, produce
2251 designation sequence of short-form. */
2252
2253 #define ENCODE_DESIGNATION(charset, reg, coding) \
2254 do { \
2255 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
2256 char *intermediate_char_94 = "()*+"; \
2257 char *intermediate_char_96 = ",-./"; \
2258 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
2259 \
2260 if (revision < 255) \
2261 { \
2262 *dst++ = ISO_CODE_ESC; \
2263 *dst++ = '&'; \
2264 *dst++ = '@' + revision; \
2265 } \
2266 *dst++ = ISO_CODE_ESC; \
2267 if (CHARSET_DIMENSION (charset) == 1) \
2268 { \
2269 if (CHARSET_CHARS (charset) == 94) \
2270 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2271 else \
2272 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2273 } \
2274 else \
2275 { \
2276 *dst++ = '$'; \
2277 if (CHARSET_CHARS (charset) == 94) \
2278 { \
2279 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
2280 || reg != 0 \
2281 || final_char < '@' || final_char > 'B') \
2282 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2283 } \
2284 else \
2285 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2286 } \
2287 *dst++ = final_char; \
2288 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
2289 } while (0)
2290
2291 /* The following two macros produce codes (control character or escape
2292 sequence) for ISO2022 single-shift functions (single-shift-2 and
2293 single-shift-3). */
2294
2295 #define ENCODE_SINGLE_SHIFT_2 \
2296 do { \
2297 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2298 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
2299 else \
2300 *dst++ = ISO_CODE_SS2; \
2301 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2302 } while (0)
2303
2304 #define ENCODE_SINGLE_SHIFT_3 \
2305 do { \
2306 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2307 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
2308 else \
2309 *dst++ = ISO_CODE_SS3; \
2310 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2311 } while (0)
2312
2313 /* The following four macros produce codes (control character or
2314 escape sequence) for ISO2022 locking-shift functions (shift-in,
2315 shift-out, locking-shift-2, and locking-shift-3). */
2316
2317 #define ENCODE_SHIFT_IN \
2318 do { \
2319 *dst++ = ISO_CODE_SI; \
2320 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
2321 } while (0)
2322
2323 #define ENCODE_SHIFT_OUT \
2324 do { \
2325 *dst++ = ISO_CODE_SO; \
2326 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
2327 } while (0)
2328
2329 #define ENCODE_LOCKING_SHIFT_2 \
2330 do { \
2331 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
2332 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
2333 } while (0)
2334
2335 #define ENCODE_LOCKING_SHIFT_3 \
2336 do { \
2337 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
2338 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
2339 } while (0)
2340
2341 /* Produce codes for a DIMENSION1 character whose character set is
2342 CHARSET and whose position-code is C1. Designation and invocation
2343 sequences are also produced in advance if necessary. */
2344
2345 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
2346 do { \
2347 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2348 { \
2349 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2350 *dst++ = c1 & 0x7F; \
2351 else \
2352 *dst++ = c1 | 0x80; \
2353 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2354 break; \
2355 } \
2356 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2357 { \
2358 *dst++ = c1 & 0x7F; \
2359 break; \
2360 } \
2361 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2362 { \
2363 *dst++ = c1 | 0x80; \
2364 break; \
2365 } \
2366 else \
2367 /* Since CHARSET is not yet invoked to any graphic planes, we \
2368 must invoke it, or, at first, designate it to some graphic \
2369 register. Then repeat the loop to actually produce the \
2370 character. */ \
2371 dst = encode_invocation_designation (charset, coding, dst); \
2372 } while (1)
2373
2374 /* Produce codes for a DIMENSION2 character whose character set is
2375 CHARSET and whose position-codes are C1 and C2. Designation and
2376 invocation codes are also produced in advance if necessary. */
2377
2378 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
2379 do { \
2380 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2381 { \
2382 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2383 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
2384 else \
2385 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
2386 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2387 break; \
2388 } \
2389 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2390 { \
2391 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
2392 break; \
2393 } \
2394 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2395 { \
2396 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
2397 break; \
2398 } \
2399 else \
2400 /* Since CHARSET is not yet invoked to any graphic planes, we \
2401 must invoke it, or, at first, designate it to some graphic \
2402 register. Then repeat the loop to actually produce the \
2403 character. */ \
2404 dst = encode_invocation_designation (charset, coding, dst); \
2405 } while (1)
2406
2407 #define ENCODE_ISO_CHARACTER(c) \
2408 do { \
2409 int charset, c1, c2; \
2410 \
2411 SPLIT_CHAR (c, charset, c1, c2); \
2412 if (CHARSET_DEFINED_P (charset)) \
2413 { \
2414 if (CHARSET_DIMENSION (charset) == 1) \
2415 { \
2416 if (charset == CHARSET_ASCII \
2417 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
2418 charset = charset_latin_jisx0201; \
2419 ENCODE_ISO_CHARACTER_DIMENSION1 (charset, c1); \
2420 } \
2421 else \
2422 { \
2423 if (charset == charset_jisx0208 \
2424 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
2425 charset = charset_jisx0208_1978; \
2426 ENCODE_ISO_CHARACTER_DIMENSION2 (charset, c1, c2); \
2427 } \
2428 } \
2429 else \
2430 { \
2431 *dst++ = c1; \
2432 if (c2 >= 0) \
2433 *dst++ = c2; \
2434 } \
2435 } while (0)
2436
2437
2438 /* Instead of encoding character C, produce one or two `?'s. */
2439
2440 #define ENCODE_UNSAFE_CHARACTER(c) \
2441 do { \
2442 ENCODE_ISO_CHARACTER (CODING_REPLACEMENT_CHARACTER); \
2443 if (CHARSET_WIDTH (CHAR_CHARSET (c)) > 1) \
2444 ENCODE_ISO_CHARACTER (CODING_REPLACEMENT_CHARACTER); \
2445 } while (0)
2446
2447
2448 /* Produce designation and invocation codes at a place pointed by DST
2449 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
2450 Return new DST. */
2451
2452 unsigned char *
2453 encode_invocation_designation (charset, coding, dst)
2454 int charset;
2455 struct coding_system *coding;
2456 unsigned char *dst;
2457 {
2458 int reg; /* graphic register number */
2459
2460 /* At first, check designations. */
2461 for (reg = 0; reg < 4; reg++)
2462 if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg))
2463 break;
2464
2465 if (reg >= 4)
2466 {
2467 /* CHARSET is not yet designated to any graphic registers. */
2468 /* At first check the requested designation. */
2469 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
2470 if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)
2471 /* Since CHARSET requests no special designation, designate it
2472 to graphic register 0. */
2473 reg = 0;
2474
2475 ENCODE_DESIGNATION (charset, reg, coding);
2476 }
2477
2478 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg
2479 && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg)
2480 {
2481 /* Since the graphic register REG is not invoked to any graphic
2482 planes, invoke it to graphic plane 0. */
2483 switch (reg)
2484 {
2485 case 0: /* graphic register 0 */
2486 ENCODE_SHIFT_IN;
2487 break;
2488
2489 case 1: /* graphic register 1 */
2490 ENCODE_SHIFT_OUT;
2491 break;
2492
2493 case 2: /* graphic register 2 */
2494 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2495 ENCODE_SINGLE_SHIFT_2;
2496 else
2497 ENCODE_LOCKING_SHIFT_2;
2498 break;
2499
2500 case 3: /* graphic register 3 */
2501 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2502 ENCODE_SINGLE_SHIFT_3;
2503 else
2504 ENCODE_LOCKING_SHIFT_3;
2505 break;
2506 }
2507 }
2508
2509 return dst;
2510 }
2511
2512 /* Produce 2-byte codes for encoded composition rule RULE. */
2513
2514 #define ENCODE_COMPOSITION_RULE(rule) \
2515 do { \
2516 int gref, nref; \
2517 COMPOSITION_DECODE_RULE (rule, gref, nref); \
2518 *dst++ = 32 + 81 + gref; \
2519 *dst++ = 32 + nref; \
2520 } while (0)
2521
2522 /* Produce codes for indicating the start of a composition sequence
2523 (ESC 0, ESC 3, or ESC 4). DATA points to an array of integers
2524 which specify information about the composition. See the comment
2525 in coding.h for the format of DATA. */
2526
2527 #define ENCODE_COMPOSITION_START(coding, data) \
2528 do { \
2529 coding->composing = data[3]; \
2530 *dst++ = ISO_CODE_ESC; \
2531 if (coding->composing == COMPOSITION_RELATIVE) \
2532 *dst++ = '0'; \
2533 else \
2534 { \
2535 *dst++ = (coding->composing == COMPOSITION_WITH_ALTCHARS \
2536 ? '3' : '4'); \
2537 coding->cmp_data_index = coding->cmp_data_start + 4; \
2538 coding->composition_rule_follows = 0; \
2539 } \
2540 } while (0)
2541
2542 /* Produce codes for indicating the end of the current composition. */
2543
2544 #define ENCODE_COMPOSITION_END(coding, data) \
2545 do { \
2546 *dst++ = ISO_CODE_ESC; \
2547 *dst++ = '1'; \
2548 coding->cmp_data_start += data[0]; \
2549 coding->composing = COMPOSITION_NO; \
2550 if (coding->cmp_data_start == coding->cmp_data->used \
2551 && coding->cmp_data->next) \
2552 { \
2553 coding->cmp_data = coding->cmp_data->next; \
2554 coding->cmp_data_start = 0; \
2555 } \
2556 } while (0)
2557
2558 /* Produce composition start sequence ESC 0. Here, this sequence
2559 doesn't mean the start of a new composition but means that we have
2560 just produced components (alternate chars and composition rules) of
2561 the composition and the actual text follows in SRC. */
2562
2563 #define ENCODE_COMPOSITION_FAKE_START(coding) \
2564 do { \
2565 *dst++ = ISO_CODE_ESC; \
2566 *dst++ = '0'; \
2567 coding->composing = COMPOSITION_RELATIVE; \
2568 } while (0)
2569
2570 /* The following three macros produce codes for indicating direction
2571 of text. */
2572 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2573 do { \
2574 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
2575 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
2576 else \
2577 *dst++ = ISO_CODE_CSI; \
2578 } while (0)
2579
2580 #define ENCODE_DIRECTION_R2L \
2581 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '2', *dst++ = ']'
2582
2583 #define ENCODE_DIRECTION_L2R \
2584 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '0', *dst++ = ']'
2585
2586 /* Produce codes for designation and invocation to reset the graphic
2587 planes and registers to initial state. */
2588 #define ENCODE_RESET_PLANE_AND_REGISTER \
2589 do { \
2590 int reg; \
2591 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
2592 ENCODE_SHIFT_IN; \
2593 for (reg = 0; reg < 4; reg++) \
2594 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
2595 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
2596 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
2597 ENCODE_DESIGNATION \
2598 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
2599 } while (0)
2600
2601 /* Produce designation sequences of charsets in the line started from
2602 SRC to a place pointed by DST, and return updated DST.
2603
2604 If the current block ends before any end-of-line, we may fail to
2605 find all the necessary designations. */
2606
2607 static unsigned char *
2608 encode_designation_at_bol (coding, translation_table, src, src_end, dst)
2609 struct coding_system *coding;
2610 Lisp_Object translation_table;
2611 const unsigned char *src, *src_end;
2612 unsigned char *dst;
2613 {
2614 int charset, c, found = 0, reg;
2615 /* Table of charsets to be designated to each graphic register. */
2616 int r[4];
2617
2618 for (reg = 0; reg < 4; reg++)
2619 r[reg] = -1;
2620
2621 while (found < 4)
2622 {
2623 ONE_MORE_CHAR (c);
2624 if (c == '\n')
2625 break;
2626
2627 charset = CHAR_CHARSET (c);
2628 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
2629 if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0)
2630 {
2631 found++;
2632 r[reg] = charset;
2633 }
2634 }
2635
2636 label_end_of_loop:
2637 if (found)
2638 {
2639 for (reg = 0; reg < 4; reg++)
2640 if (r[reg] >= 0
2641 && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg])
2642 ENCODE_DESIGNATION (r[reg], reg, coding);
2643 }
2644
2645 return dst;
2646 }
2647
2648 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
2649
2650 static void
2651 encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2652 struct coding_system *coding;
2653 const unsigned char *source;
2654 unsigned char *destination;
2655 int src_bytes, dst_bytes;
2656 {
2657 const unsigned char *src = source;
2658 const unsigned char *src_end = source + src_bytes;
2659 unsigned char *dst = destination;
2660 unsigned char *dst_end = destination + dst_bytes;
2661 /* Since the maximum bytes produced by each loop is 20, we subtract 19
2662 from DST_END to assure overflow checking is necessary only at the
2663 head of loop. */
2664 unsigned char *adjusted_dst_end = dst_end - 19;
2665 /* SRC_BASE remembers the start position in source in each loop.
2666 The loop will be exited when there's not enough source text to
2667 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
2668 there's not enough destination area to produce encoded codes
2669 (within macro EMIT_BYTES). */
2670 const unsigned char *src_base;
2671 int c;
2672 Lisp_Object translation_table;
2673 Lisp_Object safe_chars;
2674
2675 if (coding->flags & CODING_FLAG_ISO_SAFE)
2676 coding->mode |= CODING_MODE_INHIBIT_UNENCODABLE_CHAR;
2677
2678 safe_chars = coding_safe_chars (coding->symbol);
2679
2680 if (NILP (Venable_character_translation))
2681 translation_table = Qnil;
2682 else
2683 {
2684 translation_table = coding->translation_table_for_encode;
2685 if (NILP (translation_table))
2686 translation_table = Vstandard_translation_table_for_encode;
2687 }
2688
2689 coding->consumed_char = 0;
2690 coding->errors = 0;
2691 while (1)
2692 {
2693 src_base = src;
2694
2695 if (dst >= (dst_bytes ? adjusted_dst_end : (src - 19)))
2696 {
2697 coding->result = CODING_FINISH_INSUFFICIENT_DST;
2698 break;
2699 }
2700
2701 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL
2702 && CODING_SPEC_ISO_BOL (coding))
2703 {
2704 /* We have to produce designation sequences if any now. */
2705 dst = encode_designation_at_bol (coding, translation_table,
2706 src, src_end, dst);
2707 CODING_SPEC_ISO_BOL (coding) = 0;
2708 }
2709
2710 /* Check composition start and end. */
2711 if (coding->composing != COMPOSITION_DISABLED
2712 && coding->cmp_data_start < coding->cmp_data->used)
2713 {
2714 struct composition_data *cmp_data = coding->cmp_data;
2715 int *data = cmp_data->data + coding->cmp_data_start;
2716 int this_pos = cmp_data->char_offset + coding->consumed_char;
2717
2718 if (coding->composing == COMPOSITION_RELATIVE)
2719 {
2720 if (this_pos == data[2])
2721 {
2722 ENCODE_COMPOSITION_END (coding, data);
2723 cmp_data = coding->cmp_data;
2724 data = cmp_data->data + coding->cmp_data_start;
2725 }
2726 }
2727 else if (COMPOSING_P (coding))
2728 {
2729 /* COMPOSITION_WITH_ALTCHARS or COMPOSITION_WITH_RULE_ALTCHAR */
2730 if (coding->cmp_data_index == coding->cmp_data_start + data[0])
2731 /* We have consumed components of the composition.
2732 What follows in SRC is the composition's base
2733 text. */
2734 ENCODE_COMPOSITION_FAKE_START (coding);
2735 else
2736 {
2737 int c = cmp_data->data[coding->cmp_data_index++];
2738 if (coding->composition_rule_follows)
2739 {
2740 ENCODE_COMPOSITION_RULE (c);
2741 coding->composition_rule_follows = 0;
2742 }
2743 else
2744 {
2745 if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR
2746 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2747 ENCODE_UNSAFE_CHARACTER (c);
2748 else
2749 ENCODE_ISO_CHARACTER (c);
2750 if (coding->composing == COMPOSITION_WITH_RULE_ALTCHARS)
2751 coding->composition_rule_follows = 1;
2752 }
2753 continue;
2754 }
2755 }
2756 if (!COMPOSING_P (coding))
2757 {
2758 if (this_pos == data[1])
2759 {
2760 ENCODE_COMPOSITION_START (coding, data);
2761 continue;
2762 }
2763 }
2764 }
2765
2766 ONE_MORE_CHAR (c);
2767
2768 /* Now encode the character C. */
2769 if (c < 0x20 || c == 0x7F)
2770 {
2771 if (c == '\r')
2772 {
2773 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
2774 {
2775 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
2776 ENCODE_RESET_PLANE_AND_REGISTER;
2777 *dst++ = c;
2778 continue;
2779 }
2780 /* fall down to treat '\r' as '\n' ... */
2781 c = '\n';
2782 }
2783 if (c == '\n')
2784 {
2785 if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL)
2786 ENCODE_RESET_PLANE_AND_REGISTER;
2787 if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL)
2788 bcopy (coding->spec.iso2022.initial_designation,
2789 coding->spec.iso2022.current_designation,
2790 sizeof coding->spec.iso2022.initial_designation);
2791 if (coding->eol_type == CODING_EOL_LF
2792 || coding->eol_type == CODING_EOL_UNDECIDED)
2793 *dst++ = ISO_CODE_LF;
2794 else if (coding->eol_type == CODING_EOL_CRLF)
2795 *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF;
2796 else
2797 *dst++ = ISO_CODE_CR;
2798 CODING_SPEC_ISO_BOL (coding) = 1;
2799 }
2800 else
2801 {
2802 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
2803 ENCODE_RESET_PLANE_AND_REGISTER;
2804 *dst++ = c;
2805 }
2806 }
2807 else if (ASCII_BYTE_P (c))
2808 ENCODE_ISO_CHARACTER (c);
2809 else if (SINGLE_BYTE_CHAR_P (c))
2810 {
2811 *dst++ = c;
2812 coding->errors++;
2813 }
2814 else if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR
2815 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2816 ENCODE_UNSAFE_CHARACTER (c);
2817 else
2818 ENCODE_ISO_CHARACTER (c);
2819
2820 coding->consumed_char++;
2821 }
2822
2823 label_end_of_loop:
2824 coding->consumed = src_base - source;
2825 coding->produced = coding->produced_char = dst - destination;
2826 }
2827
2828 \f
2829 /*** 4. SJIS and BIG5 handlers ***/
2830
2831 /* Although SJIS and BIG5 are not ISO coding systems, they are used
2832 quite widely. So, for the moment, Emacs supports them in the bare
2833 C code. But, in the future, they may be supported only by CCL. */
2834
2835 /* SJIS is a coding system encoding three character sets: ASCII, right
2836 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
2837 as is. A character of charset katakana-jisx0201 is encoded by
2838 "position-code + 0x80". A character of charset japanese-jisx0208
2839 is encoded in 2-byte but two position-codes are divided and shifted
2840 so that it fits in the range below.
2841
2842 --- CODE RANGE of SJIS ---
2843 (character set) (range)
2844 ASCII 0x00 .. 0x7F
2845 KATAKANA-JISX0201 0xA1 .. 0xDF
2846 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
2847 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
2848 -------------------------------
2849
2850 */
2851
2852 /* BIG5 is a coding system encoding two character sets: ASCII and
2853 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2854 character set and is encoded in two bytes.
2855
2856 --- CODE RANGE of BIG5 ---
2857 (character set) (range)
2858 ASCII 0x00 .. 0x7F
2859 Big5 (1st byte) 0xA1 .. 0xFE
2860 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2861 --------------------------
2862
2863 Since the number of characters in Big5 is larger than maximum
2864 characters in Emacs' charset (96x96), it can't be handled as one
2865 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2866 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2867 contains frequently used characters and the latter contains less
2868 frequently used characters. */
2869
2870 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2871 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2872 C1 and C2 are the 1st and 2nd position-codes of Emacs' internal
2873 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2874
2875 /* Number of Big5 characters which have the same code in 1st byte. */
2876 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2877
2878 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2879 do { \
2880 unsigned int temp \
2881 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2882 if (b1 < 0xC9) \
2883 charset = charset_big5_1; \
2884 else \
2885 { \
2886 charset = charset_big5_2; \
2887 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2888 } \
2889 c1 = temp / (0xFF - 0xA1) + 0x21; \
2890 c2 = temp % (0xFF - 0xA1) + 0x21; \
2891 } while (0)
2892
2893 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2894 do { \
2895 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2896 if (charset == charset_big5_2) \
2897 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2898 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2899 b2 = temp % BIG5_SAME_ROW; \
2900 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2901 } while (0)
2902
2903 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2904 Check if a text is encoded in SJIS. If it is, return
2905 CODING_CATEGORY_MASK_SJIS, else return 0. */
2906
2907 static int
2908 detect_coding_sjis (src, src_end, multibytep)
2909 unsigned char *src, *src_end;
2910 int multibytep;
2911 {
2912 int c;
2913 /* Dummy for ONE_MORE_BYTE. */
2914 struct coding_system dummy_coding;
2915 struct coding_system *coding = &dummy_coding;
2916
2917 while (1)
2918 {
2919 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_SJIS);
2920 if (c < 0x80)
2921 continue;
2922 if (c == 0x80 || c == 0xA0 || c > 0xEF)
2923 return 0;
2924 if (c <= 0x9F || c >= 0xE0)
2925 {
2926 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0);
2927 if (c < 0x40 || c == 0x7F || c > 0xFC)
2928 return 0;
2929 }
2930 }
2931 }
2932
2933 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2934 Check if a text is encoded in BIG5. If it is, return
2935 CODING_CATEGORY_MASK_BIG5, else return 0. */
2936
2937 static int
2938 detect_coding_big5 (src, src_end, multibytep)
2939 unsigned char *src, *src_end;
2940 int multibytep;
2941 {
2942 int c;
2943 /* Dummy for ONE_MORE_BYTE. */
2944 struct coding_system dummy_coding;
2945 struct coding_system *coding = &dummy_coding;
2946
2947 while (1)
2948 {
2949 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_BIG5);
2950 if (c < 0x80)
2951 continue;
2952 if (c < 0xA1 || c > 0xFE)
2953 return 0;
2954 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0);
2955 if (c < 0x40 || (c > 0x7F && c < 0xA1) || c > 0xFE)
2956 return 0;
2957 }
2958 }
2959
2960 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2961 Check if a text is encoded in UTF-8. If it is, return
2962 CODING_CATEGORY_MASK_UTF_8, else return 0. */
2963
2964 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
2965 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
2966 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
2967 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
2968 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
2969 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
2970 #define UTF_8_6_OCTET_LEADING_P(c) (((c) & 0xFE) == 0xFC)
2971
2972 static int
2973 detect_coding_utf_8 (src, src_end, multibytep)
2974 unsigned char *src, *src_end;
2975 int multibytep;
2976 {
2977 unsigned char c;
2978 int seq_maybe_bytes;
2979 /* Dummy for ONE_MORE_BYTE. */
2980 struct coding_system dummy_coding;
2981 struct coding_system *coding = &dummy_coding;
2982
2983 while (1)
2984 {
2985 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_UTF_8);
2986 if (UTF_8_1_OCTET_P (c))
2987 continue;
2988 else if (UTF_8_2_OCTET_LEADING_P (c))
2989 seq_maybe_bytes = 1;
2990 else if (UTF_8_3_OCTET_LEADING_P (c))
2991 seq_maybe_bytes = 2;
2992 else if (UTF_8_4_OCTET_LEADING_P (c))
2993 seq_maybe_bytes = 3;
2994 else if (UTF_8_5_OCTET_LEADING_P (c))
2995 seq_maybe_bytes = 4;
2996 else if (UTF_8_6_OCTET_LEADING_P (c))
2997 seq_maybe_bytes = 5;
2998 else
2999 return 0;
3000
3001 do
3002 {
3003 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0);
3004 if (!UTF_8_EXTRA_OCTET_P (c))
3005 return 0;
3006 seq_maybe_bytes--;
3007 }
3008 while (seq_maybe_bytes > 0);
3009 }
3010 }
3011
3012 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3013 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
3014 Little Endian (otherwise). If it is, return
3015 CODING_CATEGORY_MASK_UTF_16_BE or CODING_CATEGORY_MASK_UTF_16_LE,
3016 else return 0. */
3017
3018 #define UTF_16_INVALID_P(val) \
3019 (((val) == 0xFFFE) \
3020 || ((val) == 0xFFFF))
3021
3022 #define UTF_16_HIGH_SURROGATE_P(val) \
3023 (((val) & 0xD800) == 0xD800)
3024
3025 #define UTF_16_LOW_SURROGATE_P(val) \
3026 (((val) & 0xDC00) == 0xDC00)
3027
3028 static int
3029 detect_coding_utf_16 (src, src_end, multibytep)
3030 unsigned char *src, *src_end;
3031 int multibytep;
3032 {
3033 unsigned char c1, c2;
3034 /* Dummy for ONE_MORE_BYTE_CHECK_MULTIBYTE. */
3035 struct coding_system dummy_coding;
3036 struct coding_system *coding = &dummy_coding;
3037
3038 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep, 0);
3039 ONE_MORE_BYTE_CHECK_MULTIBYTE (c2, multibytep, 0);
3040
3041 if ((c1 == 0xFF) && (c2 == 0xFE))
3042 return CODING_CATEGORY_MASK_UTF_16_LE;
3043 else if ((c1 == 0xFE) && (c2 == 0xFF))
3044 return CODING_CATEGORY_MASK_UTF_16_BE;
3045 return 0;
3046 }
3047
3048 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
3049 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
3050
3051 static void
3052 decode_coding_sjis_big5 (coding, source, destination,
3053 src_bytes, dst_bytes, sjis_p)
3054 struct coding_system *coding;
3055 const unsigned char *source;
3056 unsigned char *destination;
3057 int src_bytes, dst_bytes;
3058 int sjis_p;
3059 {
3060 const unsigned char *src = source;
3061 const unsigned char *src_end = source + src_bytes;
3062 unsigned char *dst = destination;
3063 unsigned char *dst_end = destination + dst_bytes;
3064 /* SRC_BASE remembers the start position in source in each loop.
3065 The loop will be exited when there's not enough source code
3066 (within macro ONE_MORE_BYTE), or when there's not enough
3067 destination area to produce a character (within macro
3068 EMIT_CHAR). */
3069 const unsigned char *src_base;
3070 Lisp_Object translation_table;
3071
3072 if (NILP (Venable_character_translation))
3073 translation_table = Qnil;
3074 else
3075 {
3076 translation_table = coding->translation_table_for_decode;
3077 if (NILP (translation_table))
3078 translation_table = Vstandard_translation_table_for_decode;
3079 }
3080
3081 coding->produced_char = 0;
3082 while (1)
3083 {
3084 int c, charset, c1, c2 = 0;
3085
3086 src_base = src;
3087 ONE_MORE_BYTE (c1);
3088
3089 if (c1 < 0x80)
3090 {
3091 charset = CHARSET_ASCII;
3092 if (c1 < 0x20)
3093 {
3094 if (c1 == '\r')
3095 {
3096 if (coding->eol_type == CODING_EOL_CRLF)
3097 {
3098 ONE_MORE_BYTE (c2);
3099 if (c2 == '\n')
3100 c1 = c2;
3101 else
3102 /* To process C2 again, SRC is subtracted by 1. */
3103 src--;
3104 }
3105 else if (coding->eol_type == CODING_EOL_CR)
3106 c1 = '\n';
3107 }
3108 else if (c1 == '\n'
3109 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3110 && (coding->eol_type == CODING_EOL_CR
3111 || coding->eol_type == CODING_EOL_CRLF))
3112 {
3113 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3114 goto label_end_of_loop;
3115 }
3116 }
3117 }
3118 else
3119 {
3120 if (sjis_p)
3121 {
3122 if (c1 == 0x80 || c1 == 0xA0 || c1 > 0xEF)
3123 goto label_invalid_code;
3124 if (c1 <= 0x9F || c1 >= 0xE0)
3125 {
3126 /* SJIS -> JISX0208 */
3127 ONE_MORE_BYTE (c2);
3128 if (c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
3129 goto label_invalid_code;
3130 DECODE_SJIS (c1, c2, c1, c2);
3131 charset = charset_jisx0208;
3132 }
3133 else
3134 /* SJIS -> JISX0201-Kana */
3135 charset = charset_katakana_jisx0201;
3136 }
3137 else
3138 {
3139 /* BIG5 -> Big5 */
3140 if (c1 < 0xA0 || c1 > 0xFE)
3141 goto label_invalid_code;
3142 ONE_MORE_BYTE (c2);
3143 if (c2 < 0x40 || (c2 > 0x7E && c2 < 0xA1) || c2 > 0xFE)
3144 goto label_invalid_code;
3145 DECODE_BIG5 (c1, c2, charset, c1, c2);
3146 }
3147 }
3148
3149 c = DECODE_ISO_CHARACTER (charset, c1, c2);
3150 EMIT_CHAR (c);
3151 continue;
3152
3153 label_invalid_code:
3154 coding->errors++;
3155 src = src_base;
3156 c = *src++;
3157 EMIT_CHAR (c);
3158 }
3159
3160 label_end_of_loop:
3161 coding->consumed = coding->consumed_char = src_base - source;
3162 coding->produced = dst - destination;
3163 return;
3164 }
3165
3166 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
3167 This function can encode charsets `ascii', `katakana-jisx0201',
3168 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
3169 are sure that all these charsets are registered as official charset
3170 (i.e. do not have extended leading-codes). Characters of other
3171 charsets are produced without any encoding. If SJIS_P is 1, encode
3172 SJIS text, else encode BIG5 text. */
3173
3174 static void
3175 encode_coding_sjis_big5 (coding, source, destination,
3176 src_bytes, dst_bytes, sjis_p)
3177 struct coding_system *coding;
3178 unsigned char *source, *destination;
3179 int src_bytes, dst_bytes;
3180 int sjis_p;
3181 {
3182 unsigned char *src = source;
3183 unsigned char *src_end = source + src_bytes;
3184 unsigned char *dst = destination;
3185 unsigned char *dst_end = destination + dst_bytes;
3186 /* SRC_BASE remembers the start position in source in each loop.
3187 The loop will be exited when there's not enough source text to
3188 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3189 there's not enough destination area to produce encoded codes
3190 (within macro EMIT_BYTES). */
3191 unsigned char *src_base;
3192 Lisp_Object translation_table;
3193
3194 if (NILP (Venable_character_translation))
3195 translation_table = Qnil;
3196 else
3197 {
3198 translation_table = coding->translation_table_for_encode;
3199 if (NILP (translation_table))
3200 translation_table = Vstandard_translation_table_for_encode;
3201 }
3202
3203 while (1)
3204 {
3205 int c, charset, c1, c2;
3206
3207 src_base = src;
3208 ONE_MORE_CHAR (c);
3209
3210 /* Now encode the character C. */
3211 if (SINGLE_BYTE_CHAR_P (c))
3212 {
3213 switch (c)
3214 {
3215 case '\r':
3216 if (!(coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
3217 {
3218 EMIT_ONE_BYTE (c);
3219 break;
3220 }
3221 c = '\n';
3222 case '\n':
3223 if (coding->eol_type == CODING_EOL_CRLF)
3224 {
3225 EMIT_TWO_BYTES ('\r', c);
3226 break;
3227 }
3228 else if (coding->eol_type == CODING_EOL_CR)
3229 c = '\r';
3230 default:
3231 EMIT_ONE_BYTE (c);
3232 }
3233 }
3234 else
3235 {
3236 SPLIT_CHAR (c, charset, c1, c2);
3237 if (sjis_p)
3238 {
3239 if (charset == charset_jisx0208
3240 || charset == charset_jisx0208_1978)
3241 {
3242 ENCODE_SJIS (c1, c2, c1, c2);
3243 EMIT_TWO_BYTES (c1, c2);
3244 }
3245 else if (charset == charset_katakana_jisx0201)
3246 EMIT_ONE_BYTE (c1 | 0x80);
3247 else if (charset == charset_latin_jisx0201)
3248 EMIT_ONE_BYTE (c1);
3249 else if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR)
3250 {
3251 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3252 if (CHARSET_WIDTH (charset) > 1)
3253 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3254 }
3255 else
3256 /* There's no way other than producing the internal
3257 codes as is. */
3258 EMIT_BYTES (src_base, src);
3259 }
3260 else
3261 {
3262 if (charset == charset_big5_1 || charset == charset_big5_2)
3263 {
3264 ENCODE_BIG5 (charset, c1, c2, c1, c2);
3265 EMIT_TWO_BYTES (c1, c2);
3266 }
3267 else if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR)
3268 {
3269 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3270 if (CHARSET_WIDTH (charset) > 1)
3271 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3272 }
3273 else
3274 /* There's no way other than producing the internal
3275 codes as is. */
3276 EMIT_BYTES (src_base, src);
3277 }
3278 }
3279 coding->consumed_char++;
3280 }
3281
3282 label_end_of_loop:
3283 coding->consumed = src_base - source;
3284 coding->produced = coding->produced_char = dst - destination;
3285 }
3286
3287 \f
3288 /*** 5. CCL handlers ***/
3289
3290 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3291 Check if a text is encoded in a coding system of which
3292 encoder/decoder are written in CCL program. If it is, return
3293 CODING_CATEGORY_MASK_CCL, else return 0. */
3294
3295 static int
3296 detect_coding_ccl (src, src_end, multibytep)
3297 unsigned char *src, *src_end;
3298 int multibytep;
3299 {
3300 unsigned char *valid;
3301 int c;
3302 /* Dummy for ONE_MORE_BYTE. */
3303 struct coding_system dummy_coding;
3304 struct coding_system *coding = &dummy_coding;
3305
3306 /* No coding system is assigned to coding-category-ccl. */
3307 if (!coding_system_table[CODING_CATEGORY_IDX_CCL])
3308 return 0;
3309
3310 valid = coding_system_table[CODING_CATEGORY_IDX_CCL]->spec.ccl.valid_codes;
3311 while (1)
3312 {
3313 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_CCL);
3314 if (! valid[c])
3315 return 0;
3316 }
3317 }
3318
3319 \f
3320 /*** 6. End-of-line handlers ***/
3321
3322 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3323
3324 static void
3325 decode_eol (coding, source, destination, src_bytes, dst_bytes)
3326 struct coding_system *coding;
3327 const unsigned char *source;
3328 unsigned char *destination;
3329 int src_bytes, dst_bytes;
3330 {
3331 const unsigned char *src = source;
3332 unsigned char *dst = destination;
3333 const unsigned char *src_end = src + src_bytes;
3334 unsigned char *dst_end = dst + dst_bytes;
3335 Lisp_Object translation_table;
3336 /* SRC_BASE remembers the start position in source in each loop.
3337 The loop will be exited when there's not enough source code
3338 (within macro ONE_MORE_BYTE), or when there's not enough
3339 destination area to produce a character (within macro
3340 EMIT_CHAR). */
3341 const unsigned char *src_base;
3342 int c;
3343
3344 translation_table = Qnil;
3345 switch (coding->eol_type)
3346 {
3347 case CODING_EOL_CRLF:
3348 while (1)
3349 {
3350 src_base = src;
3351 ONE_MORE_BYTE (c);
3352 if (c == '\r')
3353 {
3354 ONE_MORE_BYTE (c);
3355 if (c != '\n')
3356 {
3357 src--;
3358 c = '\r';
3359 }
3360 }
3361 else if (c == '\n'
3362 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL))
3363 {
3364 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3365 goto label_end_of_loop;
3366 }
3367 EMIT_CHAR (c);
3368 }
3369 break;
3370
3371 case CODING_EOL_CR:
3372 while (1)
3373 {
3374 src_base = src;
3375 ONE_MORE_BYTE (c);
3376 if (c == '\n')
3377 {
3378 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3379 {
3380 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3381 goto label_end_of_loop;
3382 }
3383 }
3384 else if (c == '\r')
3385 c = '\n';
3386 EMIT_CHAR (c);
3387 }
3388 break;
3389
3390 default: /* no need for EOL handling */
3391 while (1)
3392 {
3393 src_base = src;
3394 ONE_MORE_BYTE (c);
3395 EMIT_CHAR (c);
3396 }
3397 }
3398
3399 label_end_of_loop:
3400 coding->consumed = coding->consumed_char = src_base - source;
3401 coding->produced = dst - destination;
3402 return;
3403 }
3404
3405 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
3406 format of end-of-line according to `coding->eol_type'. It also
3407 convert multibyte form 8-bit characters to unibyte if
3408 CODING->src_multibyte is nonzero. If `coding->mode &
3409 CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code '\r' in source text
3410 also means end-of-line. */
3411
3412 static void
3413 encode_eol (coding, source, destination, src_bytes, dst_bytes)
3414 struct coding_system *coding;
3415 const unsigned char *source;
3416 unsigned char *destination;
3417 int src_bytes, dst_bytes;
3418 {
3419 const unsigned char *src = source;
3420 unsigned char *dst = destination;
3421 const unsigned char *src_end = src + src_bytes;
3422 unsigned char *dst_end = dst + dst_bytes;
3423 Lisp_Object translation_table;
3424 /* SRC_BASE remembers the start position in source in each loop.
3425 The loop will be exited when there's not enough source text to
3426 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3427 there's not enough destination area to produce encoded codes
3428 (within macro EMIT_BYTES). */
3429 const unsigned char *src_base;
3430 unsigned char *tmp;
3431 int c;
3432 int selective_display = coding->mode & CODING_MODE_SELECTIVE_DISPLAY;
3433
3434 translation_table = Qnil;
3435 if (coding->src_multibyte
3436 && *(src_end - 1) == LEADING_CODE_8_BIT_CONTROL)
3437 {
3438 src_end--;
3439 src_bytes--;
3440 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
3441 }
3442
3443 if (coding->eol_type == CODING_EOL_CRLF)
3444 {
3445 while (src < src_end)
3446 {
3447 src_base = src;
3448 c = *src++;
3449 if (c >= 0x20)
3450 EMIT_ONE_BYTE (c);
3451 else if (c == '\n' || (c == '\r' && selective_display))
3452 EMIT_TWO_BYTES ('\r', '\n');
3453 else
3454 EMIT_ONE_BYTE (c);
3455 }
3456 src_base = src;
3457 label_end_of_loop:
3458 ;
3459 }
3460 else
3461 {
3462 if (!dst_bytes || src_bytes <= dst_bytes)
3463 {
3464 safe_bcopy (src, dst, src_bytes);
3465 src_base = src_end;
3466 dst += src_bytes;
3467 }
3468 else
3469 {
3470 if (coding->src_multibyte
3471 && *(src + dst_bytes - 1) == LEADING_CODE_8_BIT_CONTROL)
3472 dst_bytes--;
3473 safe_bcopy (src, dst, dst_bytes);
3474 src_base = src + dst_bytes;
3475 dst = destination + dst_bytes;
3476 coding->result = CODING_FINISH_INSUFFICIENT_DST;
3477 }
3478 if (coding->eol_type == CODING_EOL_CR)
3479 {
3480 for (tmp = destination; tmp < dst; tmp++)
3481 if (*tmp == '\n') *tmp = '\r';
3482 }
3483 else if (selective_display)
3484 {
3485 for (tmp = destination; tmp < dst; tmp++)
3486 if (*tmp == '\r') *tmp = '\n';
3487 }
3488 }
3489 if (coding->src_multibyte)
3490 dst = destination + str_as_unibyte (destination, dst - destination);
3491
3492 coding->consumed = src_base - source;
3493 coding->produced = dst - destination;
3494 coding->produced_char = coding->produced;
3495 }
3496
3497 \f
3498 /*** 7. C library functions ***/
3499
3500 /* In Emacs Lisp, a coding system is represented by a Lisp symbol which
3501 has a property `coding-system'. The value of this property is a
3502 vector of length 5 (called the coding-vector). Among elements of
3503 this vector, the first (element[0]) and the fifth (element[4])
3504 carry important information for decoding/encoding. Before
3505 decoding/encoding, this information should be set in fields of a
3506 structure of type `coding_system'.
3507
3508 The value of the property `coding-system' can be a symbol of another
3509 subsidiary coding-system. In that case, Emacs gets coding-vector
3510 from that symbol.
3511
3512 `element[0]' contains information to be set in `coding->type'. The
3513 value and its meaning is as follows:
3514
3515 0 -- coding_type_emacs_mule
3516 1 -- coding_type_sjis
3517 2 -- coding_type_iso2022
3518 3 -- coding_type_big5
3519 4 -- coding_type_ccl encoder/decoder written in CCL
3520 nil -- coding_type_no_conversion
3521 t -- coding_type_undecided (automatic conversion on decoding,
3522 no-conversion on encoding)
3523
3524 `element[4]' contains information to be set in `coding->flags' and
3525 `coding->spec'. The meaning varies by `coding->type'.
3526
3527 If `coding->type' is `coding_type_iso2022', element[4] is a vector
3528 of length 32 (of which the first 13 sub-elements are used now).
3529 Meanings of these sub-elements are:
3530
3531 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
3532 If the value is an integer of valid charset, the charset is
3533 assumed to be designated to graphic register N initially.
3534
3535 If the value is minus, it is a minus value of charset which
3536 reserves graphic register N, which means that the charset is
3537 not designated initially but should be designated to graphic
3538 register N just before encoding a character in that charset.
3539
3540 If the value is nil, graphic register N is never used on
3541 encoding.
3542
3543 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
3544 Each value takes t or nil. See the section ISO2022 of
3545 `coding.h' for more information.
3546
3547 If `coding->type' is `coding_type_big5', element[4] is t to denote
3548 BIG5-ETen or nil to denote BIG5-HKU.
3549
3550 If `coding->type' takes the other value, element[4] is ignored.
3551
3552 Emacs Lisp's coding systems also carry information about format of
3553 end-of-line in a value of property `eol-type'. If the value is
3554 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
3555 means CODING_EOL_CR. If it is not integer, it should be a vector
3556 of subsidiary coding systems of which property `eol-type' has one
3557 of the above values.
3558
3559 */
3560
3561 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
3562 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
3563 is setup so that no conversion is necessary and return -1, else
3564 return 0. */
3565
3566 int
3567 setup_coding_system (coding_system, coding)
3568 Lisp_Object coding_system;
3569 struct coding_system *coding;
3570 {
3571 Lisp_Object coding_spec, coding_type, eol_type, plist;
3572 Lisp_Object val;
3573
3574 /* At first, zero clear all members. */
3575 bzero (coding, sizeof (struct coding_system));
3576
3577 /* Initialize some fields required for all kinds of coding systems. */
3578 coding->symbol = coding_system;
3579 coding->heading_ascii = -1;
3580 coding->post_read_conversion = coding->pre_write_conversion = Qnil;
3581 coding->composing = COMPOSITION_DISABLED;
3582 coding->cmp_data = NULL;
3583
3584 if (NILP (coding_system))
3585 goto label_invalid_coding_system;
3586
3587 coding_spec = Fget (coding_system, Qcoding_system);
3588
3589 if (!VECTORP (coding_spec)
3590 || XVECTOR (coding_spec)->size != 5
3591 || !CONSP (XVECTOR (coding_spec)->contents[3]))
3592 goto label_invalid_coding_system;
3593
3594 eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type);
3595 if (VECTORP (eol_type))
3596 {
3597 coding->eol_type = CODING_EOL_UNDECIDED;
3598 coding->common_flags = CODING_REQUIRE_DETECTION_MASK;
3599 if (system_eol_type != CODING_EOL_LF)
3600 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
3601 }
3602 else if (XFASTINT (eol_type) == 1)
3603 {
3604 coding->eol_type = CODING_EOL_CRLF;
3605 coding->common_flags
3606 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3607 }
3608 else if (XFASTINT (eol_type) == 2)
3609 {
3610 coding->eol_type = CODING_EOL_CR;
3611 coding->common_flags
3612 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3613 }
3614 else
3615 {
3616 coding->common_flags = 0;
3617 coding->eol_type = CODING_EOL_LF;
3618 }
3619
3620 coding_type = XVECTOR (coding_spec)->contents[0];
3621 /* Try short cut. */
3622 if (SYMBOLP (coding_type))
3623 {
3624 if (EQ (coding_type, Qt))
3625 {
3626 coding->type = coding_type_undecided;
3627 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
3628 }
3629 else
3630 coding->type = coding_type_no_conversion;
3631 /* Initialize this member. Any thing other than
3632 CODING_CATEGORY_IDX_UTF_16_BE and
3633 CODING_CATEGORY_IDX_UTF_16_LE are ok because they have
3634 special treatment in detect_eol. */
3635 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
3636
3637 return 0;
3638 }
3639
3640 /* Get values of coding system properties:
3641 `post-read-conversion', `pre-write-conversion',
3642 `translation-table-for-decode', `translation-table-for-encode'. */
3643 plist = XVECTOR (coding_spec)->contents[3];
3644 /* Pre & post conversion functions should be disabled if
3645 inhibit_eol_conversion is nonzero. This is the case that a code
3646 conversion function is called while those functions are running. */
3647 if (! inhibit_pre_post_conversion)
3648 {
3649 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
3650 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
3651 }
3652 val = Fplist_get (plist, Qtranslation_table_for_decode);
3653 if (SYMBOLP (val))
3654 val = Fget (val, Qtranslation_table_for_decode);
3655 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil;
3656 val = Fplist_get (plist, Qtranslation_table_for_encode);
3657 if (SYMBOLP (val))
3658 val = Fget (val, Qtranslation_table_for_encode);
3659 coding->translation_table_for_encode = CHAR_TABLE_P (val) ? val : Qnil;
3660 val = Fplist_get (plist, Qcoding_category);
3661 if (!NILP (val))
3662 {
3663 val = Fget (val, Qcoding_category_index);
3664 if (INTEGERP (val))
3665 coding->category_idx = XINT (val);
3666 else
3667 goto label_invalid_coding_system;
3668 }
3669 else
3670 goto label_invalid_coding_system;
3671
3672 /* If the coding system has non-nil `composition' property, enable
3673 composition handling. */
3674 val = Fplist_get (plist, Qcomposition);
3675 if (!NILP (val))
3676 coding->composing = COMPOSITION_NO;
3677
3678 /* If the coding system is ascii-incompatible, record it in
3679 common_flags. */
3680 val = Fplist_get (plist, Qascii_incompatible);
3681 if (! NILP (val))
3682 coding->common_flags |= CODING_ASCII_INCOMPATIBLE_MASK;
3683
3684 switch (XFASTINT (coding_type))
3685 {
3686 case 0:
3687 coding->type = coding_type_emacs_mule;
3688 coding->common_flags
3689 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3690 if (!NILP (coding->post_read_conversion))
3691 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
3692 if (!NILP (coding->pre_write_conversion))
3693 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
3694 break;
3695
3696 case 1:
3697 coding->type = coding_type_sjis;
3698 coding->common_flags
3699 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3700 break;
3701
3702 case 2:
3703 coding->type = coding_type_iso2022;
3704 coding->common_flags
3705 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3706 {
3707 Lisp_Object val, temp;
3708 Lisp_Object *flags;
3709 int i, charset, reg_bits = 0;
3710
3711 val = XVECTOR (coding_spec)->contents[4];
3712
3713 if (!VECTORP (val) || XVECTOR (val)->size != 32)
3714 goto label_invalid_coding_system;
3715
3716 flags = XVECTOR (val)->contents;
3717 coding->flags
3718 = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM)
3719 | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL)
3720 | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL)
3721 | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS)
3722 | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT)
3723 | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT)
3724 | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN)
3725 | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS)
3726 | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION)
3727 | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL)
3728 | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL)
3729 | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE)
3730 | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA)
3731 );
3732
3733 /* Invoke graphic register 0 to plane 0. */
3734 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
3735 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
3736 CODING_SPEC_ISO_INVOCATION (coding, 1)
3737 = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1);
3738 /* Not single shifting at first. */
3739 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0;
3740 /* Beginning of buffer should also be regarded as bol. */
3741 CODING_SPEC_ISO_BOL (coding) = 1;
3742
3743 for (charset = 0; charset <= MAX_CHARSET; charset++)
3744 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255;
3745 val = Vcharset_revision_alist;
3746 while (CONSP (val))
3747 {
3748 charset = get_charset_id (Fcar_safe (XCAR (val)));
3749 if (charset >= 0
3750 && (temp = Fcdr_safe (XCAR (val)), INTEGERP (temp))
3751 && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
3752 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
3753 val = XCDR (val);
3754 }
3755
3756 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
3757 FLAGS[REG] can be one of below:
3758 integer CHARSET: CHARSET occupies register I,
3759 t: designate nothing to REG initially, but can be used
3760 by any charsets,
3761 list of integer, nil, or t: designate the first
3762 element (if integer) to REG initially, the remaining
3763 elements (if integer) is designated to REG on request,
3764 if an element is t, REG can be used by any charsets,
3765 nil: REG is never used. */
3766 for (charset = 0; charset <= MAX_CHARSET; charset++)
3767 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3768 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION;
3769 for (i = 0; i < 4; i++)
3770 {
3771 if ((INTEGERP (flags[i])
3772 && (charset = XINT (flags[i]), CHARSET_VALID_P (charset)))
3773 || (charset = get_charset_id (flags[i])) >= 0)
3774 {
3775 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3776 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i;
3777 }
3778 else if (EQ (flags[i], Qt))
3779 {
3780 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3781 reg_bits |= 1 << i;
3782 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3783 }
3784 else if (CONSP (flags[i]))
3785 {
3786 Lisp_Object tail;
3787 tail = flags[i];
3788
3789 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3790 if ((INTEGERP (XCAR (tail))
3791 && (charset = XINT (XCAR (tail)),
3792 CHARSET_VALID_P (charset)))
3793 || (charset = get_charset_id (XCAR (tail))) >= 0)
3794 {
3795 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3796 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
3797 }
3798 else
3799 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3800 tail = XCDR (tail);
3801 while (CONSP (tail))
3802 {
3803 if ((INTEGERP (XCAR (tail))
3804 && (charset = XINT (XCAR (tail)),
3805 CHARSET_VALID_P (charset)))
3806 || (charset = get_charset_id (XCAR (tail))) >= 0)
3807 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3808 = i;
3809 else if (EQ (XCAR (tail), Qt))
3810 reg_bits |= 1 << i;
3811 tail = XCDR (tail);
3812 }
3813 }
3814 else
3815 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3816
3817 CODING_SPEC_ISO_DESIGNATION (coding, i)
3818 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i);
3819 }
3820
3821 if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
3822 {
3823 /* REG 1 can be used only by locking shift in 7-bit env. */
3824 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
3825 reg_bits &= ~2;
3826 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
3827 /* Without any shifting, only REG 0 and 1 can be used. */
3828 reg_bits &= 3;
3829 }
3830
3831 if (reg_bits)
3832 for (charset = 0; charset <= MAX_CHARSET; charset++)
3833 {
3834 if (CHARSET_DEFINED_P (charset)
3835 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3836 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
3837 {
3838 /* There exist some default graphic registers to be
3839 used by CHARSET. */
3840
3841 /* We had better avoid designating a charset of
3842 CHARS96 to REG 0 as far as possible. */
3843 if (CHARSET_CHARS (charset) == 96)
3844 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3845 = (reg_bits & 2
3846 ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0)));
3847 else
3848 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3849 = (reg_bits & 1
3850 ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3)));
3851 }
3852 }
3853 }
3854 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3855 coding->spec.iso2022.last_invalid_designation_register = -1;
3856 break;
3857
3858 case 3:
3859 coding->type = coding_type_big5;
3860 coding->common_flags
3861 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3862 coding->flags
3863 = (NILP (XVECTOR (coding_spec)->contents[4])
3864 ? CODING_FLAG_BIG5_HKU
3865 : CODING_FLAG_BIG5_ETEN);
3866 break;
3867
3868 case 4:
3869 coding->type = coding_type_ccl;
3870 coding->common_flags
3871 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3872 {
3873 val = XVECTOR (coding_spec)->contents[4];
3874 if (! CONSP (val)
3875 || setup_ccl_program (&(coding->spec.ccl.decoder),
3876 XCAR (val)) < 0
3877 || setup_ccl_program (&(coding->spec.ccl.encoder),
3878 XCDR (val)) < 0)
3879 goto label_invalid_coding_system;
3880
3881 bzero (coding->spec.ccl.valid_codes, 256);
3882 val = Fplist_get (plist, Qvalid_codes);
3883 if (CONSP (val))
3884 {
3885 Lisp_Object this;
3886
3887 for (; CONSP (val); val = XCDR (val))
3888 {
3889 this = XCAR (val);
3890 if (INTEGERP (this)
3891 && XINT (this) >= 0 && XINT (this) < 256)
3892 coding->spec.ccl.valid_codes[XINT (this)] = 1;
3893 else if (CONSP (this)
3894 && INTEGERP (XCAR (this))
3895 && INTEGERP (XCDR (this)))
3896 {
3897 int start = XINT (XCAR (this));
3898 int end = XINT (XCDR (this));
3899
3900 if (start >= 0 && start <= end && end < 256)
3901 while (start <= end)
3902 coding->spec.ccl.valid_codes[start++] = 1;
3903 }
3904 }
3905 }
3906 }
3907 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3908 coding->spec.ccl.cr_carryover = 0;
3909 coding->spec.ccl.eight_bit_carryover[0] = 0;
3910 break;
3911
3912 case 5:
3913 coding->type = coding_type_raw_text;
3914 break;
3915
3916 default:
3917 goto label_invalid_coding_system;
3918 }
3919 return 0;
3920
3921 label_invalid_coding_system:
3922 coding->type = coding_type_no_conversion;
3923 coding->category_idx = CODING_CATEGORY_IDX_BINARY;
3924 coding->common_flags = 0;
3925 coding->eol_type = CODING_EOL_UNDECIDED;
3926 coding->pre_write_conversion = coding->post_read_conversion = Qnil;
3927 return NILP (coding_system) ? 0 : -1;
3928 }
3929
3930 /* Free memory blocks allocated for storing composition information. */
3931
3932 void
3933 coding_free_composition_data (coding)
3934 struct coding_system *coding;
3935 {
3936 struct composition_data *cmp_data = coding->cmp_data, *next;
3937
3938 if (!cmp_data)
3939 return;
3940 /* Memory blocks are chained. At first, rewind to the first, then,
3941 free blocks one by one. */
3942 while (cmp_data->prev)
3943 cmp_data = cmp_data->prev;
3944 while (cmp_data)
3945 {
3946 next = cmp_data->next;
3947 xfree (cmp_data);
3948 cmp_data = next;
3949 }
3950 coding->cmp_data = NULL;
3951 }
3952
3953 /* Set `char_offset' member of all memory blocks pointed by
3954 coding->cmp_data to POS. */
3955
3956 void
3957 coding_adjust_composition_offset (coding, pos)
3958 struct coding_system *coding;
3959 int pos;
3960 {
3961 struct composition_data *cmp_data;
3962
3963 for (cmp_data = coding->cmp_data; cmp_data; cmp_data = cmp_data->next)
3964 cmp_data->char_offset = pos;
3965 }
3966
3967 /* Setup raw-text or one of its subsidiaries in the structure
3968 coding_system CODING according to the already setup value eol_type
3969 in CODING. CODING should be setup for some coding system in
3970 advance. */
3971
3972 void
3973 setup_raw_text_coding_system (coding)
3974 struct coding_system *coding;
3975 {
3976 if (coding->type != coding_type_raw_text)
3977 {
3978 coding->symbol = Qraw_text;
3979 coding->type = coding_type_raw_text;
3980 if (coding->eol_type != CODING_EOL_UNDECIDED)
3981 {
3982 Lisp_Object subsidiaries;
3983 subsidiaries = Fget (Qraw_text, Qeol_type);
3984
3985 if (VECTORP (subsidiaries)
3986 && XVECTOR (subsidiaries)->size == 3)
3987 coding->symbol
3988 = XVECTOR (subsidiaries)->contents[coding->eol_type];
3989 }
3990 setup_coding_system (coding->symbol, coding);
3991 }
3992 return;
3993 }
3994
3995 /* Emacs has a mechanism to automatically detect a coding system if it
3996 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3997 it's impossible to distinguish some coding systems accurately
3998 because they use the same range of codes. So, at first, coding
3999 systems are categorized into 7, those are:
4000
4001 o coding-category-emacs-mule
4002
4003 The category for a coding system which has the same code range
4004 as Emacs' internal format. Assigned the coding-system (Lisp
4005 symbol) `emacs-mule' by default.
4006
4007 o coding-category-sjis
4008
4009 The category for a coding system which has the same code range
4010 as SJIS. Assigned the coding-system (Lisp
4011 symbol) `japanese-shift-jis' by default.
4012
4013 o coding-category-iso-7
4014
4015 The category for a coding system which has the same code range
4016 as ISO2022 of 7-bit environment. This doesn't use any locking
4017 shift and single shift functions. This can encode/decode all
4018 charsets. Assigned the coding-system (Lisp symbol)
4019 `iso-2022-7bit' by default.
4020
4021 o coding-category-iso-7-tight
4022
4023 Same as coding-category-iso-7 except that this can
4024 encode/decode only the specified charsets.
4025
4026 o coding-category-iso-8-1
4027
4028 The category for a coding system which has the same code range
4029 as ISO2022 of 8-bit environment and graphic plane 1 used only
4030 for DIMENSION1 charset. This doesn't use any locking shift
4031 and single shift functions. Assigned the coding-system (Lisp
4032 symbol) `iso-latin-1' by default.
4033
4034 o coding-category-iso-8-2
4035
4036 The category for a coding system which has the same code range
4037 as ISO2022 of 8-bit environment and graphic plane 1 used only
4038 for DIMENSION2 charset. This doesn't use any locking shift
4039 and single shift functions. Assigned the coding-system (Lisp
4040 symbol) `japanese-iso-8bit' by default.
4041
4042 o coding-category-iso-7-else
4043
4044 The category for a coding system which has the same code range
4045 as ISO2022 of 7-bit environment but uses locking shift or
4046 single shift functions. Assigned the coding-system (Lisp
4047 symbol) `iso-2022-7bit-lock' by default.
4048
4049 o coding-category-iso-8-else
4050
4051 The category for a coding system which has the same code range
4052 as ISO2022 of 8-bit environment but uses locking shift or
4053 single shift functions. Assigned the coding-system (Lisp
4054 symbol) `iso-2022-8bit-ss2' by default.
4055
4056 o coding-category-big5
4057
4058 The category for a coding system which has the same code range
4059 as BIG5. Assigned the coding-system (Lisp symbol)
4060 `cn-big5' by default.
4061
4062 o coding-category-utf-8
4063
4064 The category for a coding system which has the same code range
4065 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
4066 symbol) `utf-8' by default.
4067
4068 o coding-category-utf-16-be
4069
4070 The category for a coding system in which a text has an
4071 Unicode signature (cf. Unicode Standard) in the order of BIG
4072 endian at the head. Assigned the coding-system (Lisp symbol)
4073 `utf-16-be' by default.
4074
4075 o coding-category-utf-16-le
4076
4077 The category for a coding system in which a text has an
4078 Unicode signature (cf. Unicode Standard) in the order of
4079 LITTLE endian at the head. Assigned the coding-system (Lisp
4080 symbol) `utf-16-le' by default.
4081
4082 o coding-category-ccl
4083
4084 The category for a coding system of which encoder/decoder is
4085 written in CCL programs. The default value is nil, i.e., no
4086 coding system is assigned.
4087
4088 o coding-category-binary
4089
4090 The category for a coding system not categorized in any of the
4091 above. Assigned the coding-system (Lisp symbol)
4092 `no-conversion' by default.
4093
4094 Each of them is a Lisp symbol and the value is an actual
4095 `coding-system' (this is also a Lisp symbol) assigned by a user.
4096 What Emacs does actually is to detect a category of coding system.
4097 Then, it uses a `coding-system' assigned to it. If Emacs can't
4098 decide a single possible category, it selects a category of the
4099 highest priority. Priorities of categories are also specified by a
4100 user in a Lisp variable `coding-category-list'.
4101
4102 */
4103
4104 static
4105 int ascii_skip_code[256];
4106
4107 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
4108 If it detects possible coding systems, return an integer in which
4109 appropriate flag bits are set. Flag bits are defined by macros
4110 CODING_CATEGORY_MASK_XXX in `coding.h'. If PRIORITIES is non-NULL,
4111 it should point the table `coding_priorities'. In that case, only
4112 the flag bit for a coding system of the highest priority is set in
4113 the returned value. If MULTIBYTEP is nonzero, 8-bit codes of the
4114 range 0x80..0x9F are in multibyte form.
4115
4116 How many ASCII characters are at the head is returned as *SKIP. */
4117
4118 static int
4119 detect_coding_mask (source, src_bytes, priorities, skip, multibytep)
4120 unsigned char *source;
4121 int src_bytes, *priorities, *skip;
4122 int multibytep;
4123 {
4124 register unsigned char c;
4125 unsigned char *src = source, *src_end = source + src_bytes;
4126 unsigned int mask, utf16_examined_p, iso2022_examined_p;
4127 int i;
4128
4129 /* At first, skip all ASCII characters and control characters except
4130 for three ISO2022 specific control characters. */
4131 ascii_skip_code[ISO_CODE_SO] = 0;
4132 ascii_skip_code[ISO_CODE_SI] = 0;
4133 ascii_skip_code[ISO_CODE_ESC] = 0;
4134
4135 label_loop_detect_coding:
4136 while (src < src_end && ascii_skip_code[*src]) src++;
4137 *skip = src - source;
4138
4139 if (src >= src_end)
4140 /* We found nothing other than ASCII. There's nothing to do. */
4141 return 0;
4142
4143 c = *src;
4144 /* The text seems to be encoded in some multilingual coding system.
4145 Now, try to find in which coding system the text is encoded. */
4146 if (c < 0x80)
4147 {
4148 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
4149 /* C is an ISO2022 specific control code of C0. */
4150 mask = detect_coding_iso2022 (src, src_end, multibytep);
4151 if (mask == 0)
4152 {
4153 /* No valid ISO2022 code follows C. Try again. */
4154 src++;
4155 if (c == ISO_CODE_ESC)
4156 ascii_skip_code[ISO_CODE_ESC] = 1;
4157 else
4158 ascii_skip_code[ISO_CODE_SO] = ascii_skip_code[ISO_CODE_SI] = 1;
4159 goto label_loop_detect_coding;
4160 }
4161 if (priorities)
4162 {
4163 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4164 {
4165 if (mask & priorities[i])
4166 return priorities[i];
4167 }
4168 return CODING_CATEGORY_MASK_RAW_TEXT;
4169 }
4170 }
4171 else
4172 {
4173 int try;
4174
4175 if (multibytep && c == LEADING_CODE_8_BIT_CONTROL)
4176 c = src[1] - 0x20;
4177
4178 if (c < 0xA0)
4179 {
4180 /* C is the first byte of SJIS character code,
4181 or a leading-code of Emacs' internal format (emacs-mule),
4182 or the first byte of UTF-16. */
4183 try = (CODING_CATEGORY_MASK_SJIS
4184 | CODING_CATEGORY_MASK_EMACS_MULE
4185 | CODING_CATEGORY_MASK_UTF_16_BE
4186 | CODING_CATEGORY_MASK_UTF_16_LE);
4187
4188 /* Or, if C is a special latin extra code,
4189 or is an ISO2022 specific control code of C1 (SS2 or SS3),
4190 or is an ISO2022 control-sequence-introducer (CSI),
4191 we should also consider the possibility of ISO2022 codings. */
4192 if ((VECTORP (Vlatin_extra_code_table)
4193 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
4194 || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
4195 || (c == ISO_CODE_CSI
4196 && (src < src_end
4197 && (*src == ']'
4198 || ((*src == '0' || *src == '1' || *src == '2')
4199 && src + 1 < src_end
4200 && src[1] == ']')))))
4201 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
4202 | CODING_CATEGORY_MASK_ISO_8BIT);
4203 }
4204 else
4205 /* C is a character of ISO2022 in graphic plane right,
4206 or a SJIS's 1-byte character code (i.e. JISX0201),
4207 or the first byte of BIG5's 2-byte code,
4208 or the first byte of UTF-8/16. */
4209 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
4210 | CODING_CATEGORY_MASK_ISO_8BIT
4211 | CODING_CATEGORY_MASK_SJIS
4212 | CODING_CATEGORY_MASK_BIG5
4213 | CODING_CATEGORY_MASK_UTF_8
4214 | CODING_CATEGORY_MASK_UTF_16_BE
4215 | CODING_CATEGORY_MASK_UTF_16_LE);
4216
4217 /* Or, we may have to consider the possibility of CCL. */
4218 if (coding_system_table[CODING_CATEGORY_IDX_CCL]
4219 && (coding_system_table[CODING_CATEGORY_IDX_CCL]
4220 ->spec.ccl.valid_codes)[c])
4221 try |= CODING_CATEGORY_MASK_CCL;
4222
4223 mask = 0;
4224 utf16_examined_p = iso2022_examined_p = 0;
4225 if (priorities)
4226 {
4227 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4228 {
4229 if (!iso2022_examined_p
4230 && (priorities[i] & try & CODING_CATEGORY_MASK_ISO))
4231 {
4232 mask |= detect_coding_iso2022 (src, src_end, multibytep);
4233 iso2022_examined_p = 1;
4234 }
4235 else if (priorities[i] & try & CODING_CATEGORY_MASK_SJIS)
4236 mask |= detect_coding_sjis (src, src_end, multibytep);
4237 else if (priorities[i] & try & CODING_CATEGORY_MASK_UTF_8)
4238 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4239 else if (!utf16_examined_p
4240 && (priorities[i] & try &
4241 CODING_CATEGORY_MASK_UTF_16_BE_LE))
4242 {
4243 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4244 utf16_examined_p = 1;
4245 }
4246 else if (priorities[i] & try & CODING_CATEGORY_MASK_BIG5)
4247 mask |= detect_coding_big5 (src, src_end, multibytep);
4248 else if (priorities[i] & try & CODING_CATEGORY_MASK_EMACS_MULE)
4249 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4250 else if (priorities[i] & try & CODING_CATEGORY_MASK_CCL)
4251 mask |= detect_coding_ccl (src, src_end, multibytep);
4252 else if (priorities[i] & CODING_CATEGORY_MASK_RAW_TEXT)
4253 mask |= CODING_CATEGORY_MASK_RAW_TEXT;
4254 else if (priorities[i] & CODING_CATEGORY_MASK_BINARY)
4255 mask |= CODING_CATEGORY_MASK_BINARY;
4256 if (mask & priorities[i])
4257 return priorities[i];
4258 }
4259 return CODING_CATEGORY_MASK_RAW_TEXT;
4260 }
4261 if (try & CODING_CATEGORY_MASK_ISO)
4262 mask |= detect_coding_iso2022 (src, src_end, multibytep);
4263 if (try & CODING_CATEGORY_MASK_SJIS)
4264 mask |= detect_coding_sjis (src, src_end, multibytep);
4265 if (try & CODING_CATEGORY_MASK_BIG5)
4266 mask |= detect_coding_big5 (src, src_end, multibytep);
4267 if (try & CODING_CATEGORY_MASK_UTF_8)
4268 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4269 if (try & CODING_CATEGORY_MASK_UTF_16_BE_LE)
4270 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4271 if (try & CODING_CATEGORY_MASK_EMACS_MULE)
4272 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4273 if (try & CODING_CATEGORY_MASK_CCL)
4274 mask |= detect_coding_ccl (src, src_end, multibytep);
4275 }
4276 return (mask | CODING_CATEGORY_MASK_RAW_TEXT | CODING_CATEGORY_MASK_BINARY);
4277 }
4278
4279 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
4280 The information of the detected coding system is set in CODING. */
4281
4282 void
4283 detect_coding (coding, src, src_bytes)
4284 struct coding_system *coding;
4285 const unsigned char *src;
4286 int src_bytes;
4287 {
4288 unsigned int idx;
4289 int skip, mask;
4290 Lisp_Object val;
4291
4292 val = Vcoding_category_list;
4293 mask = detect_coding_mask (src, src_bytes, coding_priorities, &skip,
4294 coding->src_multibyte);
4295 coding->heading_ascii = skip;
4296
4297 if (!mask) return;
4298
4299 /* We found a single coding system of the highest priority in MASK. */
4300 idx = 0;
4301 while (mask && ! (mask & 1)) mask >>= 1, idx++;
4302 if (! mask)
4303 idx = CODING_CATEGORY_IDX_RAW_TEXT;
4304
4305 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[idx]);
4306
4307 if (coding->eol_type != CODING_EOL_UNDECIDED)
4308 {
4309 Lisp_Object tmp;
4310
4311 tmp = Fget (val, Qeol_type);
4312 if (VECTORP (tmp))
4313 val = XVECTOR (tmp)->contents[coding->eol_type];
4314 }
4315
4316 /* Setup this new coding system while preserving some slots. */
4317 {
4318 int src_multibyte = coding->src_multibyte;
4319 int dst_multibyte = coding->dst_multibyte;
4320
4321 setup_coding_system (val, coding);
4322 coding->src_multibyte = src_multibyte;
4323 coding->dst_multibyte = dst_multibyte;
4324 coding->heading_ascii = skip;
4325 }
4326 }
4327
4328 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
4329 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
4330 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
4331
4332 How many non-eol characters are at the head is returned as *SKIP. */
4333
4334 #define MAX_EOL_CHECK_COUNT 3
4335
4336 static int
4337 detect_eol_type (source, src_bytes, skip)
4338 unsigned char *source;
4339 int src_bytes, *skip;
4340 {
4341 unsigned char *src = source, *src_end = src + src_bytes;
4342 unsigned char c;
4343 int total = 0; /* How many end-of-lines are found so far. */
4344 int eol_type = CODING_EOL_UNDECIDED;
4345 int this_eol_type;
4346
4347 *skip = 0;
4348
4349 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
4350 {
4351 c = *src++;
4352 if (c == '\n' || c == '\r')
4353 {
4354 if (*skip == 0)
4355 *skip = src - 1 - source;
4356 total++;
4357 if (c == '\n')
4358 this_eol_type = CODING_EOL_LF;
4359 else if (src >= src_end || *src != '\n')
4360 this_eol_type = CODING_EOL_CR;
4361 else
4362 this_eol_type = CODING_EOL_CRLF, src++;
4363
4364 if (eol_type == CODING_EOL_UNDECIDED)
4365 /* This is the first end-of-line. */
4366 eol_type = this_eol_type;
4367 else if (eol_type != this_eol_type)
4368 {
4369 /* The found type is different from what found before. */
4370 eol_type = CODING_EOL_INCONSISTENT;
4371 break;
4372 }
4373 }
4374 }
4375
4376 if (*skip == 0)
4377 *skip = src_end - source;
4378 return eol_type;
4379 }
4380
4381 /* Like detect_eol_type, but detect EOL type in 2-octet
4382 big-endian/little-endian format for coding systems utf-16-be and
4383 utf-16-le. */
4384
4385 static int
4386 detect_eol_type_in_2_octet_form (source, src_bytes, skip, big_endian_p)
4387 unsigned char *source;
4388 int src_bytes, *skip, big_endian_p;
4389 {
4390 unsigned char *src = source, *src_end = src + src_bytes;
4391 unsigned int c1, c2;
4392 int total = 0; /* How many end-of-lines are found so far. */
4393 int eol_type = CODING_EOL_UNDECIDED;
4394 int this_eol_type;
4395 int msb, lsb;
4396
4397 if (big_endian_p)
4398 msb = 0, lsb = 1;
4399 else
4400 msb = 1, lsb = 0;
4401
4402 *skip = 0;
4403
4404 while ((src + 1) < src_end && total < MAX_EOL_CHECK_COUNT)
4405 {
4406 c1 = (src[msb] << 8) | (src[lsb]);
4407 src += 2;
4408
4409 if (c1 == '\n' || c1 == '\r')
4410 {
4411 if (*skip == 0)
4412 *skip = src - 2 - source;
4413 total++;
4414 if (c1 == '\n')
4415 {
4416 this_eol_type = CODING_EOL_LF;
4417 }
4418 else
4419 {
4420 if ((src + 1) >= src_end)
4421 {
4422 this_eol_type = CODING_EOL_CR;
4423 }
4424 else
4425 {
4426 c2 = (src[msb] << 8) | (src[lsb]);
4427 if (c2 == '\n')
4428 this_eol_type = CODING_EOL_CRLF, src += 2;
4429 else
4430 this_eol_type = CODING_EOL_CR;
4431 }
4432 }
4433
4434 if (eol_type == CODING_EOL_UNDECIDED)
4435 /* This is the first end-of-line. */
4436 eol_type = this_eol_type;
4437 else if (eol_type != this_eol_type)
4438 {
4439 /* The found type is different from what found before. */
4440 eol_type = CODING_EOL_INCONSISTENT;
4441 break;
4442 }
4443 }
4444 }
4445
4446 if (*skip == 0)
4447 *skip = src_end - source;
4448 return eol_type;
4449 }
4450
4451 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
4452 is encoded. If it detects an appropriate format of end-of-line, it
4453 sets the information in *CODING. */
4454
4455 void
4456 detect_eol (coding, src, src_bytes)
4457 struct coding_system *coding;
4458 const unsigned char *src;
4459 int src_bytes;
4460 {
4461 Lisp_Object val;
4462 int skip;
4463 int eol_type;
4464
4465 switch (coding->category_idx)
4466 {
4467 case CODING_CATEGORY_IDX_UTF_16_BE:
4468 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 1);
4469 break;
4470 case CODING_CATEGORY_IDX_UTF_16_LE:
4471 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 0);
4472 break;
4473 default:
4474 eol_type = detect_eol_type (src, src_bytes, &skip);
4475 break;
4476 }
4477
4478 if (coding->heading_ascii > skip)
4479 coding->heading_ascii = skip;
4480 else
4481 skip = coding->heading_ascii;
4482
4483 if (eol_type == CODING_EOL_UNDECIDED)
4484 return;
4485 if (eol_type == CODING_EOL_INCONSISTENT)
4486 {
4487 #if 0
4488 /* This code is suppressed until we find a better way to
4489 distinguish raw text file and binary file. */
4490
4491 /* If we have already detected that the coding is raw-text, the
4492 coding should actually be no-conversion. */
4493 if (coding->type == coding_type_raw_text)
4494 {
4495 setup_coding_system (Qno_conversion, coding);
4496 return;
4497 }
4498 /* Else, let's decode only text code anyway. */
4499 #endif /* 0 */
4500 eol_type = CODING_EOL_LF;
4501 }
4502
4503 val = Fget (coding->symbol, Qeol_type);
4504 if (VECTORP (val) && XVECTOR (val)->size == 3)
4505 {
4506 int src_multibyte = coding->src_multibyte;
4507 int dst_multibyte = coding->dst_multibyte;
4508 struct composition_data *cmp_data = coding->cmp_data;
4509
4510 setup_coding_system (XVECTOR (val)->contents[eol_type], coding);
4511 coding->src_multibyte = src_multibyte;
4512 coding->dst_multibyte = dst_multibyte;
4513 coding->heading_ascii = skip;
4514 coding->cmp_data = cmp_data;
4515 }
4516 }
4517
4518 #define CONVERSION_BUFFER_EXTRA_ROOM 256
4519
4520 #define DECODING_BUFFER_MAG(coding) \
4521 (coding->type == coding_type_iso2022 \
4522 ? 3 \
4523 : (coding->type == coding_type_ccl \
4524 ? coding->spec.ccl.decoder.buf_magnification \
4525 : 2))
4526
4527 /* Return maximum size (bytes) of a buffer enough for decoding
4528 SRC_BYTES of text encoded in CODING. */
4529
4530 int
4531 decoding_buffer_size (coding, src_bytes)
4532 struct coding_system *coding;
4533 int src_bytes;
4534 {
4535 return (src_bytes * DECODING_BUFFER_MAG (coding)
4536 + CONVERSION_BUFFER_EXTRA_ROOM);
4537 }
4538
4539 /* Return maximum size (bytes) of a buffer enough for encoding
4540 SRC_BYTES of text to CODING. */
4541
4542 int
4543 encoding_buffer_size (coding, src_bytes)
4544 struct coding_system *coding;
4545 int src_bytes;
4546 {
4547 int magnification;
4548
4549 if (coding->type == coding_type_ccl)
4550 {
4551 magnification = coding->spec.ccl.encoder.buf_magnification;
4552 if (coding->eol_type == CODING_EOL_CRLF)
4553 magnification *= 2;
4554 }
4555 else if (CODING_REQUIRE_ENCODING (coding))
4556 magnification = 3;
4557 else
4558 magnification = 1;
4559
4560 return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM);
4561 }
4562
4563 /* Working buffer for code conversion. */
4564 struct conversion_buffer
4565 {
4566 int size; /* size of data. */
4567 int on_stack; /* 1 if allocated by alloca. */
4568 unsigned char *data;
4569 };
4570
4571 /* Allocate LEN bytes of memory for BUF (struct conversion_buffer). */
4572 #define allocate_conversion_buffer(buf, len) \
4573 do { \
4574 if (len < MAX_ALLOCA) \
4575 { \
4576 buf.data = (unsigned char *) alloca (len); \
4577 buf.on_stack = 1; \
4578 } \
4579 else \
4580 { \
4581 buf.data = (unsigned char *) xmalloc (len); \
4582 buf.on_stack = 0; \
4583 } \
4584 buf.size = len; \
4585 } while (0)
4586
4587 /* Double the allocated memory for *BUF. */
4588 static void
4589 extend_conversion_buffer (buf)
4590 struct conversion_buffer *buf;
4591 {
4592 if (buf->on_stack)
4593 {
4594 unsigned char *save = buf->data;
4595 buf->data = (unsigned char *) xmalloc (buf->size * 2);
4596 bcopy (save, buf->data, buf->size);
4597 buf->on_stack = 0;
4598 }
4599 else
4600 {
4601 buf->data = (unsigned char *) xrealloc (buf->data, buf->size * 2);
4602 }
4603 buf->size *= 2;
4604 }
4605
4606 /* Free the allocated memory for BUF if it is not on stack. */
4607 static void
4608 free_conversion_buffer (buf)
4609 struct conversion_buffer *buf;
4610 {
4611 if (!buf->on_stack)
4612 xfree (buf->data);
4613 }
4614
4615 int
4616 ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep)
4617 struct coding_system *coding;
4618 unsigned char *source, *destination;
4619 int src_bytes, dst_bytes, encodep;
4620 {
4621 struct ccl_program *ccl
4622 = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder;
4623 unsigned char *dst = destination;
4624
4625 ccl->suppress_error = coding->suppress_error;
4626 ccl->last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4627 if (encodep)
4628 {
4629 /* On encoding, EOL format is converted within ccl_driver. For
4630 that, setup proper information in the structure CCL. */
4631 ccl->eol_type = coding->eol_type;
4632 if (ccl->eol_type ==CODING_EOL_UNDECIDED)
4633 ccl->eol_type = CODING_EOL_LF;
4634 ccl->cr_consumed = coding->spec.ccl.cr_carryover;
4635 ccl->eight_bit_control = coding->dst_multibyte;
4636 }
4637 else
4638 ccl->eight_bit_control = 1;
4639 ccl->multibyte = coding->src_multibyte;
4640 if (coding->spec.ccl.eight_bit_carryover[0] != 0)
4641 {
4642 /* Move carryover bytes to DESTINATION. */
4643 unsigned char *p = coding->spec.ccl.eight_bit_carryover;
4644 while (*p)
4645 *dst++ = *p++;
4646 coding->spec.ccl.eight_bit_carryover[0] = 0;
4647 if (dst_bytes)
4648 dst_bytes -= dst - destination;
4649 }
4650
4651 coding->produced = (ccl_driver (ccl, source, dst, src_bytes, dst_bytes,
4652 &(coding->consumed))
4653 + dst - destination);
4654
4655 if (encodep)
4656 {
4657 coding->produced_char = coding->produced;
4658 coding->spec.ccl.cr_carryover = ccl->cr_consumed;
4659 }
4660 else if (!ccl->eight_bit_control)
4661 {
4662 /* The produced bytes forms a valid multibyte sequence. */
4663 coding->produced_char
4664 = multibyte_chars_in_text (destination, coding->produced);
4665 coding->spec.ccl.eight_bit_carryover[0] = 0;
4666 }
4667 else
4668 {
4669 /* On decoding, the destination should always multibyte. But,
4670 CCL program might have been generated an invalid multibyte
4671 sequence. Here we make such a sequence valid as
4672 multibyte. */
4673 int bytes
4674 = dst_bytes ? dst_bytes : source + coding->consumed - destination;
4675
4676 if ((coding->consumed < src_bytes
4677 || !ccl->last_block)
4678 && coding->produced >= 1
4679 && destination[coding->produced - 1] >= 0x80)
4680 {
4681 /* We should not convert the tailing 8-bit codes to
4682 multibyte form even if they doesn't form a valid
4683 multibyte sequence. They may form a valid sequence in
4684 the next call. */
4685 int carryover = 0;
4686
4687 if (destination[coding->produced - 1] < 0xA0)
4688 carryover = 1;
4689 else if (coding->produced >= 2)
4690 {
4691 if (destination[coding->produced - 2] >= 0x80)
4692 {
4693 if (destination[coding->produced - 2] < 0xA0)
4694 carryover = 2;
4695 else if (coding->produced >= 3
4696 && destination[coding->produced - 3] >= 0x80
4697 && destination[coding->produced - 3] < 0xA0)
4698 carryover = 3;
4699 }
4700 }
4701 if (carryover > 0)
4702 {
4703 BCOPY_SHORT (destination + coding->produced - carryover,
4704 coding->spec.ccl.eight_bit_carryover,
4705 carryover);
4706 coding->spec.ccl.eight_bit_carryover[carryover] = 0;
4707 coding->produced -= carryover;
4708 }
4709 }
4710 coding->produced = str_as_multibyte (destination, bytes,
4711 coding->produced,
4712 &(coding->produced_char));
4713 }
4714
4715 switch (ccl->status)
4716 {
4717 case CCL_STAT_SUSPEND_BY_SRC:
4718 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
4719 break;
4720 case CCL_STAT_SUSPEND_BY_DST:
4721 coding->result = CODING_FINISH_INSUFFICIENT_DST;
4722 break;
4723 case CCL_STAT_QUIT:
4724 case CCL_STAT_INVALID_CMD:
4725 coding->result = CODING_FINISH_INTERRUPT;
4726 break;
4727 default:
4728 coding->result = CODING_FINISH_NORMAL;
4729 break;
4730 }
4731 return coding->result;
4732 }
4733
4734 /* Decode EOL format of the text at PTR of BYTES length destructively
4735 according to CODING->eol_type. This is called after the CCL
4736 program produced a decoded text at PTR. If we do CRLF->LF
4737 conversion, update CODING->produced and CODING->produced_char. */
4738
4739 static void
4740 decode_eol_post_ccl (coding, ptr, bytes)
4741 struct coding_system *coding;
4742 unsigned char *ptr;
4743 int bytes;
4744 {
4745 Lisp_Object val, saved_coding_symbol;
4746 unsigned char *pend = ptr + bytes;
4747 int dummy;
4748
4749 /* Remember the current coding system symbol. We set it back when
4750 an inconsistent EOL is found so that `last-coding-system-used' is
4751 set to the coding system that doesn't specify EOL conversion. */
4752 saved_coding_symbol = coding->symbol;
4753
4754 coding->spec.ccl.cr_carryover = 0;
4755 if (coding->eol_type == CODING_EOL_UNDECIDED)
4756 {
4757 /* Here, to avoid the call of setup_coding_system, we directly
4758 call detect_eol_type. */
4759 coding->eol_type = detect_eol_type (ptr, bytes, &dummy);
4760 if (coding->eol_type == CODING_EOL_INCONSISTENT)
4761 coding->eol_type = CODING_EOL_LF;
4762 if (coding->eol_type != CODING_EOL_UNDECIDED)
4763 {
4764 val = Fget (coding->symbol, Qeol_type);
4765 if (VECTORP (val) && XVECTOR (val)->size == 3)
4766 coding->symbol = XVECTOR (val)->contents[coding->eol_type];
4767 }
4768 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4769 }
4770
4771 if (coding->eol_type == CODING_EOL_LF
4772 || coding->eol_type == CODING_EOL_UNDECIDED)
4773 {
4774 /* We have nothing to do. */
4775 ptr = pend;
4776 }
4777 else if (coding->eol_type == CODING_EOL_CRLF)
4778 {
4779 unsigned char *pstart = ptr, *p = ptr;
4780
4781 if (! (coding->mode & CODING_MODE_LAST_BLOCK)
4782 && *(pend - 1) == '\r')
4783 {
4784 /* If the last character is CR, we can't handle it here
4785 because LF will be in the not-yet-decoded source text.
4786 Record that the CR is not yet processed. */
4787 coding->spec.ccl.cr_carryover = 1;
4788 coding->produced--;
4789 coding->produced_char--;
4790 pend--;
4791 }
4792 while (ptr < pend)
4793 {
4794 if (*ptr == '\r')
4795 {
4796 if (ptr + 1 < pend && *(ptr + 1) == '\n')
4797 {
4798 *p++ = '\n';
4799 ptr += 2;
4800 }
4801 else
4802 {
4803 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4804 goto undo_eol_conversion;
4805 *p++ = *ptr++;
4806 }
4807 }
4808 else if (*ptr == '\n'
4809 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4810 goto undo_eol_conversion;
4811 else
4812 *p++ = *ptr++;
4813 continue;
4814
4815 undo_eol_conversion:
4816 /* We have faced with inconsistent EOL format at PTR.
4817 Convert all LFs before PTR back to CRLFs. */
4818 for (p--, ptr--; p >= pstart; p--)
4819 {
4820 if (*p == '\n')
4821 *ptr-- = '\n', *ptr-- = '\r';
4822 else
4823 *ptr-- = *p;
4824 }
4825 /* If carryover is recorded, cancel it because we don't
4826 convert CRLF anymore. */
4827 if (coding->spec.ccl.cr_carryover)
4828 {
4829 coding->spec.ccl.cr_carryover = 0;
4830 coding->produced++;
4831 coding->produced_char++;
4832 pend++;
4833 }
4834 p = ptr = pend;
4835 coding->eol_type = CODING_EOL_LF;
4836 coding->symbol = saved_coding_symbol;
4837 }
4838 if (p < pend)
4839 {
4840 /* As each two-byte sequence CRLF was converted to LF, (PEND
4841 - P) is the number of deleted characters. */
4842 coding->produced -= pend - p;
4843 coding->produced_char -= pend - p;
4844 }
4845 }
4846 else /* i.e. coding->eol_type == CODING_EOL_CR */
4847 {
4848 unsigned char *p = ptr;
4849
4850 for (; ptr < pend; ptr++)
4851 {
4852 if (*ptr == '\r')
4853 *ptr = '\n';
4854 else if (*ptr == '\n'
4855 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4856 {
4857 for (; p < ptr; p++)
4858 {
4859 if (*p == '\n')
4860 *p = '\r';
4861 }
4862 ptr = pend;
4863 coding->eol_type = CODING_EOL_LF;
4864 coding->symbol = saved_coding_symbol;
4865 }
4866 }
4867 }
4868 }
4869
4870 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
4871 decoding, it may detect coding system and format of end-of-line if
4872 those are not yet decided. The source should be unibyte, the
4873 result is multibyte if CODING->dst_multibyte is nonzero, else
4874 unibyte. */
4875
4876 int
4877 decode_coding (coding, source, destination, src_bytes, dst_bytes)
4878 struct coding_system *coding;
4879 const unsigned char *source;
4880 unsigned char *destination;
4881 int src_bytes, dst_bytes;
4882 {
4883 int extra = 0;
4884
4885 if (coding->type == coding_type_undecided)
4886 detect_coding (coding, source, src_bytes);
4887
4888 if (coding->eol_type == CODING_EOL_UNDECIDED
4889 && coding->type != coding_type_ccl)
4890 {
4891 detect_eol (coding, source, src_bytes);
4892 /* We had better recover the original eol format if we
4893 encounter an inconsistent eol format while decoding. */
4894 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4895 }
4896
4897 coding->produced = coding->produced_char = 0;
4898 coding->consumed = coding->consumed_char = 0;
4899 coding->errors = 0;
4900 coding->result = CODING_FINISH_NORMAL;
4901
4902 switch (coding->type)
4903 {
4904 case coding_type_sjis:
4905 decode_coding_sjis_big5 (coding, source, destination,
4906 src_bytes, dst_bytes, 1);
4907 break;
4908
4909 case coding_type_iso2022:
4910 decode_coding_iso2022 (coding, source, destination,
4911 src_bytes, dst_bytes);
4912 break;
4913
4914 case coding_type_big5:
4915 decode_coding_sjis_big5 (coding, source, destination,
4916 src_bytes, dst_bytes, 0);
4917 break;
4918
4919 case coding_type_emacs_mule:
4920 decode_coding_emacs_mule (coding, source, destination,
4921 src_bytes, dst_bytes);
4922 break;
4923
4924 case coding_type_ccl:
4925 if (coding->spec.ccl.cr_carryover)
4926 {
4927 /* Put the CR which was not processed by the previous call
4928 of decode_eol_post_ccl in DESTINATION. It will be
4929 decoded together with the following LF by the call to
4930 decode_eol_post_ccl below. */
4931 *destination = '\r';
4932 coding->produced++;
4933 coding->produced_char++;
4934 dst_bytes--;
4935 extra = coding->spec.ccl.cr_carryover;
4936 }
4937 ccl_coding_driver (coding, source, destination + extra,
4938 src_bytes, dst_bytes, 0);
4939 if (coding->eol_type != CODING_EOL_LF)
4940 {
4941 coding->produced += extra;
4942 coding->produced_char += extra;
4943 decode_eol_post_ccl (coding, destination, coding->produced);
4944 }
4945 break;
4946
4947 default:
4948 decode_eol (coding, source, destination, src_bytes, dst_bytes);
4949 }
4950
4951 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
4952 && coding->mode & CODING_MODE_LAST_BLOCK
4953 && coding->consumed == src_bytes)
4954 coding->result = CODING_FINISH_NORMAL;
4955
4956 if (coding->mode & CODING_MODE_LAST_BLOCK
4957 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
4958 {
4959 const unsigned char *src = source + coding->consumed;
4960 unsigned char *dst = destination + coding->produced;
4961
4962 src_bytes -= coding->consumed;
4963 coding->errors++;
4964 if (COMPOSING_P (coding))
4965 DECODE_COMPOSITION_END ('1');
4966 while (src_bytes--)
4967 {
4968 int c = *src++;
4969 dst += CHAR_STRING (c, dst);
4970 coding->produced_char++;
4971 }
4972 coding->consumed = coding->consumed_char = src - source;
4973 coding->produced = dst - destination;
4974 coding->result = CODING_FINISH_NORMAL;
4975 }
4976
4977 if (!coding->dst_multibyte)
4978 {
4979 coding->produced = str_as_unibyte (destination, coding->produced);
4980 coding->produced_char = coding->produced;
4981 }
4982
4983 return coding->result;
4984 }
4985
4986 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". The
4987 multibyteness of the source is CODING->src_multibyte, the
4988 multibyteness of the result is always unibyte. */
4989
4990 int
4991 encode_coding (coding, source, destination, src_bytes, dst_bytes)
4992 struct coding_system *coding;
4993 const unsigned char *source;
4994 unsigned char *destination;
4995 int src_bytes, dst_bytes;
4996 {
4997 coding->produced = coding->produced_char = 0;
4998 coding->consumed = coding->consumed_char = 0;
4999 coding->errors = 0;
5000 coding->result = CODING_FINISH_NORMAL;
5001 if (coding->eol_type == CODING_EOL_UNDECIDED)
5002 coding->eol_type = CODING_EOL_LF;
5003
5004 switch (coding->type)
5005 {
5006 case coding_type_sjis:
5007 encode_coding_sjis_big5 (coding, source, destination,
5008 src_bytes, dst_bytes, 1);
5009 break;
5010
5011 case coding_type_iso2022:
5012 encode_coding_iso2022 (coding, source, destination,
5013 src_bytes, dst_bytes);
5014 break;
5015
5016 case coding_type_big5:
5017 encode_coding_sjis_big5 (coding, source, destination,
5018 src_bytes, dst_bytes, 0);
5019 break;
5020
5021 case coding_type_emacs_mule:
5022 encode_coding_emacs_mule (coding, source, destination,
5023 src_bytes, dst_bytes);
5024 break;
5025
5026 case coding_type_ccl:
5027 ccl_coding_driver (coding, source, destination,
5028 src_bytes, dst_bytes, 1);
5029 break;
5030
5031 default:
5032 encode_eol (coding, source, destination, src_bytes, dst_bytes);
5033 }
5034
5035 if (coding->mode & CODING_MODE_LAST_BLOCK
5036 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
5037 {
5038 const unsigned char *src = source + coding->consumed;
5039 unsigned char *dst = destination + coding->produced;
5040
5041 if (coding->type == coding_type_iso2022)
5042 ENCODE_RESET_PLANE_AND_REGISTER;
5043 if (COMPOSING_P (coding))
5044 *dst++ = ISO_CODE_ESC, *dst++ = '1';
5045 if (coding->consumed < src_bytes)
5046 {
5047 int len = src_bytes - coding->consumed;
5048
5049 BCOPY_SHORT (src, dst, len);
5050 if (coding->src_multibyte)
5051 len = str_as_unibyte (dst, len);
5052 dst += len;
5053 coding->consumed = src_bytes;
5054 }
5055 coding->produced = coding->produced_char = dst - destination;
5056 coding->result = CODING_FINISH_NORMAL;
5057 }
5058
5059 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
5060 && coding->consumed == src_bytes)
5061 coding->result = CODING_FINISH_NORMAL;
5062
5063 return coding->result;
5064 }
5065
5066 /* Scan text in the region between *BEG and *END (byte positions),
5067 skip characters which we don't have to decode by coding system
5068 CODING at the head and tail, then set *BEG and *END to the region
5069 of the text we actually have to convert. The caller should move
5070 the gap out of the region in advance if the region is from a
5071 buffer.
5072
5073 If STR is not NULL, *BEG and *END are indices into STR. */
5074
5075 static void
5076 shrink_decoding_region (beg, end, coding, str)
5077 int *beg, *end;
5078 struct coding_system *coding;
5079 unsigned char *str;
5080 {
5081 unsigned char *begp_orig, *begp, *endp_orig, *endp, c;
5082 int eol_conversion;
5083 Lisp_Object translation_table;
5084
5085 if (coding->type == coding_type_ccl
5086 || coding->type == coding_type_undecided
5087 || coding->eol_type != CODING_EOL_LF
5088 || !NILP (coding->post_read_conversion)
5089 || coding->composing != COMPOSITION_DISABLED)
5090 {
5091 /* We can't skip any data. */
5092 return;
5093 }
5094 if (coding->type == coding_type_no_conversion
5095 || coding->type == coding_type_raw_text
5096 || coding->type == coding_type_emacs_mule)
5097 {
5098 /* We need no conversion, but don't have to skip any data here.
5099 Decoding routine handles them effectively anyway. */
5100 return;
5101 }
5102
5103 translation_table = coding->translation_table_for_decode;
5104 if (NILP (translation_table) && !NILP (Venable_character_translation))
5105 translation_table = Vstandard_translation_table_for_decode;
5106 if (CHAR_TABLE_P (translation_table))
5107 {
5108 int i;
5109 for (i = 0; i < 128; i++)
5110 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
5111 break;
5112 if (i < 128)
5113 /* Some ASCII character should be translated. We give up
5114 shrinking. */
5115 return;
5116 }
5117
5118 if (coding->heading_ascii >= 0)
5119 /* Detection routine has already found how much we can skip at the
5120 head. */
5121 *beg += coding->heading_ascii;
5122
5123 if (str)
5124 {
5125 begp_orig = begp = str + *beg;
5126 endp_orig = endp = str + *end;
5127 }
5128 else
5129 {
5130 begp_orig = begp = BYTE_POS_ADDR (*beg);
5131 endp_orig = endp = begp + *end - *beg;
5132 }
5133
5134 eol_conversion = (coding->eol_type == CODING_EOL_CR
5135 || coding->eol_type == CODING_EOL_CRLF);
5136
5137 switch (coding->type)
5138 {
5139 case coding_type_sjis:
5140 case coding_type_big5:
5141 /* We can skip all ASCII characters at the head. */
5142 if (coding->heading_ascii < 0)
5143 {
5144 if (eol_conversion)
5145 while (begp < endp && *begp < 0x80 && *begp != '\r') begp++;
5146 else
5147 while (begp < endp && *begp < 0x80) begp++;
5148 }
5149 /* We can skip all ASCII characters at the tail except for the
5150 second byte of SJIS or BIG5 code. */
5151 if (eol_conversion)
5152 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--;
5153 else
5154 while (begp < endp && endp[-1] < 0x80) endp--;
5155 /* Do not consider LF as ascii if preceded by CR, since that
5156 confuses eol decoding. */
5157 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
5158 endp++;
5159 if (begp < endp && endp < endp_orig && endp[-1] >= 0x80)
5160 endp++;
5161 break;
5162
5163 case coding_type_iso2022:
5164 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
5165 /* We can't skip any data. */
5166 break;
5167 if (coding->heading_ascii < 0)
5168 {
5169 /* We can skip all ASCII characters at the head except for a
5170 few control codes. */
5171 while (begp < endp && (c = *begp) < 0x80
5172 && c != ISO_CODE_CR && c != ISO_CODE_SO
5173 && c != ISO_CODE_SI && c != ISO_CODE_ESC
5174 && (!eol_conversion || c != ISO_CODE_LF))
5175 begp++;
5176 }
5177 switch (coding->category_idx)
5178 {
5179 case CODING_CATEGORY_IDX_ISO_8_1:
5180 case CODING_CATEGORY_IDX_ISO_8_2:
5181 /* We can skip all ASCII characters at the tail. */
5182 if (eol_conversion)
5183 while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--;
5184 else
5185 while (begp < endp && endp[-1] < 0x80) endp--;
5186 /* Do not consider LF as ascii if preceded by CR, since that
5187 confuses eol decoding. */
5188 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
5189 endp++;
5190 break;
5191
5192 case CODING_CATEGORY_IDX_ISO_7:
5193 case CODING_CATEGORY_IDX_ISO_7_TIGHT:
5194 {
5195 /* We can skip all characters at the tail except for 8-bit
5196 codes and ESC and the following 2-byte at the tail. */
5197 unsigned char *eight_bit = NULL;
5198
5199 if (eol_conversion)
5200 while (begp < endp
5201 && (c = endp[-1]) != ISO_CODE_ESC && c != '\r')
5202 {
5203 if (!eight_bit && c & 0x80) eight_bit = endp;
5204 endp--;
5205 }
5206 else
5207 while (begp < endp
5208 && (c = endp[-1]) != ISO_CODE_ESC)
5209 {
5210 if (!eight_bit && c & 0x80) eight_bit = endp;
5211 endp--;
5212 }
5213 /* Do not consider LF as ascii if preceded by CR, since that
5214 confuses eol decoding. */
5215 if (begp < endp && endp < endp_orig
5216 && endp[-1] == '\r' && endp[0] == '\n')
5217 endp++;
5218 if (begp < endp && endp[-1] == ISO_CODE_ESC)
5219 {
5220 if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B')
5221 /* This is an ASCII designation sequence. We can
5222 surely skip the tail. But, if we have
5223 encountered an 8-bit code, skip only the codes
5224 after that. */
5225 endp = eight_bit ? eight_bit : endp + 2;
5226 else
5227 /* Hmmm, we can't skip the tail. */
5228 endp = endp_orig;
5229 }
5230 else if (eight_bit)
5231 endp = eight_bit;
5232 }
5233 }
5234 break;
5235
5236 default:
5237 abort ();
5238 }
5239 *beg += begp - begp_orig;
5240 *end += endp - endp_orig;
5241 return;
5242 }
5243
5244 /* Like shrink_decoding_region but for encoding. */
5245
5246 static void
5247 shrink_encoding_region (beg, end, coding, str)
5248 int *beg, *end;
5249 struct coding_system *coding;
5250 unsigned char *str;
5251 {
5252 unsigned char *begp_orig, *begp, *endp_orig, *endp;
5253 int eol_conversion;
5254 Lisp_Object translation_table;
5255
5256 if (coding->type == coding_type_ccl
5257 || coding->eol_type == CODING_EOL_CRLF
5258 || coding->eol_type == CODING_EOL_CR
5259 || (coding->cmp_data && coding->cmp_data->used > 0))
5260 {
5261 /* We can't skip any data. */
5262 return;
5263 }
5264 if (coding->type == coding_type_no_conversion
5265 || coding->type == coding_type_raw_text
5266 || coding->type == coding_type_emacs_mule
5267 || coding->type == coding_type_undecided)
5268 {
5269 /* We need no conversion, but don't have to skip any data here.
5270 Encoding routine handles them effectively anyway. */
5271 return;
5272 }
5273
5274 translation_table = coding->translation_table_for_encode;
5275 if (NILP (translation_table) && !NILP (Venable_character_translation))
5276 translation_table = Vstandard_translation_table_for_encode;
5277 if (CHAR_TABLE_P (translation_table))
5278 {
5279 int i;
5280 for (i = 0; i < 128; i++)
5281 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
5282 break;
5283 if (i < 128)
5284 /* Some ASCII character should be translated. We give up
5285 shrinking. */
5286 return;
5287 }
5288
5289 if (str)
5290 {
5291 begp_orig = begp = str + *beg;
5292 endp_orig = endp = str + *end;
5293 }
5294 else
5295 {
5296 begp_orig = begp = BYTE_POS_ADDR (*beg);
5297 endp_orig = endp = begp + *end - *beg;
5298 }
5299
5300 eol_conversion = (coding->eol_type == CODING_EOL_CR
5301 || coding->eol_type == CODING_EOL_CRLF);
5302
5303 /* Here, we don't have to check coding->pre_write_conversion because
5304 the caller is expected to have handled it already. */
5305 switch (coding->type)
5306 {
5307 case coding_type_iso2022:
5308 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
5309 /* We can't skip any data. */
5310 break;
5311 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL)
5312 {
5313 unsigned char *bol = begp;
5314 while (begp < endp && *begp < 0x80)
5315 {
5316 begp++;
5317 if (begp[-1] == '\n')
5318 bol = begp;
5319 }
5320 begp = bol;
5321 goto label_skip_tail;
5322 }
5323 /* fall down ... */
5324
5325 case coding_type_sjis:
5326 case coding_type_big5:
5327 /* We can skip all ASCII characters at the head and tail. */
5328 if (eol_conversion)
5329 while (begp < endp && *begp < 0x80 && *begp != '\n') begp++;
5330 else
5331 while (begp < endp && *begp < 0x80) begp++;
5332 label_skip_tail:
5333 if (eol_conversion)
5334 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--;
5335 else
5336 while (begp < endp && *(endp - 1) < 0x80) endp--;
5337 break;
5338
5339 default:
5340 abort ();
5341 }
5342
5343 *beg += begp - begp_orig;
5344 *end += endp - endp_orig;
5345 return;
5346 }
5347
5348 /* As shrinking conversion region requires some overhead, we don't try
5349 shrinking if the length of conversion region is less than this
5350 value. */
5351 static int shrink_conversion_region_threshhold = 1024;
5352
5353 #define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \
5354 do { \
5355 if (*(end) - *(beg) > shrink_conversion_region_threshhold) \
5356 { \
5357 if (encodep) shrink_encoding_region (beg, end, coding, str); \
5358 else shrink_decoding_region (beg, end, coding, str); \
5359 } \
5360 } while (0)
5361
5362 /* ARG is (CODING BUFFER ...) where CODING is what to be set in
5363 Vlast_coding_system_used and the remaining elements are buffers to
5364 kill. */
5365 static Lisp_Object
5366 code_convert_region_unwind (arg)
5367 Lisp_Object arg;
5368 {
5369 struct gcpro gcpro1;
5370 GCPRO1 (arg);
5371
5372 inhibit_pre_post_conversion = 0;
5373 Vlast_coding_system_used = XCAR (arg);
5374 for (arg = XCDR (arg); ! NILP (arg); arg = XCDR (arg))
5375 Fkill_buffer (XCAR (arg));
5376
5377 UNGCPRO;
5378 return Qnil;
5379 }
5380
5381 /* Store information about all compositions in the range FROM and TO
5382 of OBJ in memory blocks pointed by CODING->cmp_data. OBJ is a
5383 buffer or a string, defaults to the current buffer. */
5384
5385 void
5386 coding_save_composition (coding, from, to, obj)
5387 struct coding_system *coding;
5388 int from, to;
5389 Lisp_Object obj;
5390 {
5391 Lisp_Object prop;
5392 int start, end;
5393
5394 if (coding->composing == COMPOSITION_DISABLED)
5395 return;
5396 if (!coding->cmp_data)
5397 coding_allocate_composition_data (coding, from);
5398 if (!find_composition (from, to, &start, &end, &prop, obj)
5399 || end > to)
5400 return;
5401 if (start < from
5402 && (!find_composition (end, to, &start, &end, &prop, obj)
5403 || end > to))
5404 return;
5405 coding->composing = COMPOSITION_NO;
5406 do
5407 {
5408 if (COMPOSITION_VALID_P (start, end, prop))
5409 {
5410 enum composition_method method = COMPOSITION_METHOD (prop);
5411 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
5412 >= COMPOSITION_DATA_SIZE)
5413 coding_allocate_composition_data (coding, from);
5414 /* For relative composition, we remember start and end
5415 positions, for the other compositions, we also remember
5416 components. */
5417 CODING_ADD_COMPOSITION_START (coding, start - from, method);
5418 if (method != COMPOSITION_RELATIVE)
5419 {
5420 /* We must store a*/
5421 Lisp_Object val, ch;
5422
5423 val = COMPOSITION_COMPONENTS (prop);
5424 if (CONSP (val))
5425 while (CONSP (val))
5426 {
5427 ch = XCAR (val), val = XCDR (val);
5428 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5429 }
5430 else if (VECTORP (val) || STRINGP (val))
5431 {
5432 int len = (VECTORP (val)
5433 ? XVECTOR (val)->size : SCHARS (val));
5434 int i;
5435 for (i = 0; i < len; i++)
5436 {
5437 ch = (STRINGP (val)
5438 ? Faref (val, make_number (i))
5439 : XVECTOR (val)->contents[i]);
5440 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5441 }
5442 }
5443 else /* INTEGERP (val) */
5444 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (val));
5445 }
5446 CODING_ADD_COMPOSITION_END (coding, end - from);
5447 }
5448 start = end;
5449 }
5450 while (start < to
5451 && find_composition (start, to, &start, &end, &prop, obj)
5452 && end <= to);
5453
5454 /* Make coding->cmp_data point to the first memory block. */
5455 while (coding->cmp_data->prev)
5456 coding->cmp_data = coding->cmp_data->prev;
5457 coding->cmp_data_start = 0;
5458 }
5459
5460 /* Reflect the saved information about compositions to OBJ.
5461 CODING->cmp_data points to a memory block for the information. OBJ
5462 is a buffer or a string, defaults to the current buffer. */
5463
5464 void
5465 coding_restore_composition (coding, obj)
5466 struct coding_system *coding;
5467 Lisp_Object obj;
5468 {
5469 struct composition_data *cmp_data = coding->cmp_data;
5470
5471 if (!cmp_data)
5472 return;
5473
5474 while (cmp_data->prev)
5475 cmp_data = cmp_data->prev;
5476
5477 while (cmp_data)
5478 {
5479 int i;
5480
5481 for (i = 0; i < cmp_data->used && cmp_data->data[i] > 0;
5482 i += cmp_data->data[i])
5483 {
5484 int *data = cmp_data->data + i;
5485 enum composition_method method = (enum composition_method) data[3];
5486 Lisp_Object components;
5487
5488 if (data[0] < 0 || i + data[0] > cmp_data->used)
5489 /* Invalid composition data. */
5490 break;
5491
5492 if (method == COMPOSITION_RELATIVE)
5493 components = Qnil;
5494 else
5495 {
5496 int len = data[0] - 4, j;
5497 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
5498
5499 if (method == COMPOSITION_WITH_RULE_ALTCHARS
5500 && len % 2 == 0)
5501 len --;
5502 if (len < 1)
5503 /* Invalid composition data. */
5504 break;
5505 for (j = 0; j < len; j++)
5506 args[j] = make_number (data[4 + j]);
5507 components = (method == COMPOSITION_WITH_ALTCHARS
5508 ? Fstring (len, args)
5509 : Fvector (len, args));
5510 }
5511 compose_text (data[1], data[2], components, Qnil, obj);
5512 }
5513 cmp_data = cmp_data->next;
5514 }
5515 }
5516
5517 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
5518 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
5519 coding system CODING, and return the status code of code conversion
5520 (currently, this value has no meaning).
5521
5522 How many characters (and bytes) are converted to how many
5523 characters (and bytes) are recorded in members of the structure
5524 CODING.
5525
5526 If REPLACE is nonzero, we do various things as if the original text
5527 is deleted and a new text is inserted. See the comments in
5528 replace_range (insdel.c) to know what we are doing.
5529
5530 If REPLACE is zero, it is assumed that the source text is unibyte.
5531 Otherwise, it is assumed that the source text is multibyte. */
5532
5533 int
5534 code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
5535 int from, from_byte, to, to_byte, encodep, replace;
5536 struct coding_system *coding;
5537 {
5538 int len = to - from, len_byte = to_byte - from_byte;
5539 int nchars_del = 0, nbytes_del = 0;
5540 int require, inserted, inserted_byte;
5541 int head_skip, tail_skip, total_skip = 0;
5542 Lisp_Object saved_coding_symbol;
5543 int first = 1;
5544 unsigned char *src, *dst;
5545 Lisp_Object deletion;
5546 int orig_point = PT, orig_len = len;
5547 int prev_Z;
5548 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5549
5550 deletion = Qnil;
5551 saved_coding_symbol = coding->symbol;
5552
5553 if (from < PT && PT < to)
5554 {
5555 TEMP_SET_PT_BOTH (from, from_byte);
5556 orig_point = from;
5557 }
5558
5559 if (replace)
5560 {
5561 int saved_from = from;
5562 int saved_inhibit_modification_hooks;
5563
5564 prepare_to_modify_buffer (from, to, &from);
5565 if (saved_from != from)
5566 {
5567 to = from + len;
5568 from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to);
5569 len_byte = to_byte - from_byte;
5570 }
5571
5572 /* The code conversion routine can not preserve text properties
5573 for now. So, we must remove all text properties in the
5574 region. Here, we must suppress all modification hooks. */
5575 saved_inhibit_modification_hooks = inhibit_modification_hooks;
5576 inhibit_modification_hooks = 1;
5577 Fset_text_properties (make_number (from), make_number (to), Qnil, Qnil);
5578 inhibit_modification_hooks = saved_inhibit_modification_hooks;
5579 }
5580
5581 coding->heading_ascii = 0;
5582
5583 if (! encodep && CODING_REQUIRE_DETECTION (coding))
5584 {
5585 /* We must detect encoding of text and eol format. */
5586
5587 if (from < GPT && to > GPT)
5588 move_gap_both (from, from_byte);
5589 if (coding->type == coding_type_undecided)
5590 {
5591 detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte);
5592 if (coding->type == coding_type_undecided)
5593 {
5594 /* It seems that the text contains only ASCII, but we
5595 should not leave it undecided because the deeper
5596 decoding routine (decode_coding) tries to detect the
5597 encodings again in vain. */
5598 coding->type = coding_type_emacs_mule;
5599 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5600 /* As emacs-mule decoder will handle composition, we
5601 need this setting to allocate coding->cmp_data
5602 later. */
5603 coding->composing = COMPOSITION_NO;
5604 }
5605 }
5606 if (coding->eol_type == CODING_EOL_UNDECIDED
5607 && coding->type != coding_type_ccl)
5608 {
5609 detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte);
5610 if (coding->eol_type == CODING_EOL_UNDECIDED)
5611 coding->eol_type = CODING_EOL_LF;
5612 /* We had better recover the original eol format if we
5613 encounter an inconsistent eol format while decoding. */
5614 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5615 }
5616 }
5617
5618 /* Now we convert the text. */
5619
5620 /* For encoding, we must process pre-write-conversion in advance. */
5621 if (! inhibit_pre_post_conversion
5622 && encodep
5623 && SYMBOLP (coding->pre_write_conversion)
5624 && ! NILP (Ffboundp (coding->pre_write_conversion)))
5625 {
5626 /* The function in pre-write-conversion may put a new text in a
5627 new buffer. */
5628 struct buffer *prev = current_buffer;
5629 Lisp_Object new;
5630
5631 record_unwind_protect (code_convert_region_unwind,
5632 Fcons (Vlast_coding_system_used, Qnil));
5633 /* We should not call any more pre-write/post-read-conversion
5634 functions while this pre-write-conversion is running. */
5635 inhibit_pre_post_conversion = 1;
5636 call2 (coding->pre_write_conversion,
5637 make_number (from), make_number (to));
5638 inhibit_pre_post_conversion = 0;
5639 /* Discard the unwind protect. */
5640 specpdl_ptr--;
5641
5642 if (current_buffer != prev)
5643 {
5644 len = ZV - BEGV;
5645 new = Fcurrent_buffer ();
5646 set_buffer_internal_1 (prev);
5647 del_range_2 (from, from_byte, to, to_byte, 0);
5648 TEMP_SET_PT_BOTH (from, from_byte);
5649 insert_from_buffer (XBUFFER (new), 1, len, 0);
5650 Fkill_buffer (new);
5651 if (orig_point >= to)
5652 orig_point += len - orig_len;
5653 else if (orig_point > from)
5654 orig_point = from;
5655 orig_len = len;
5656 to = from + len;
5657 from_byte = CHAR_TO_BYTE (from);
5658 to_byte = CHAR_TO_BYTE (to);
5659 len_byte = to_byte - from_byte;
5660 TEMP_SET_PT_BOTH (from, from_byte);
5661 }
5662 }
5663
5664 if (replace)
5665 {
5666 if (! EQ (current_buffer->undo_list, Qt))
5667 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
5668 else
5669 {
5670 nchars_del = to - from;
5671 nbytes_del = to_byte - from_byte;
5672 }
5673 }
5674
5675 if (coding->composing != COMPOSITION_DISABLED)
5676 {
5677 if (encodep)
5678 coding_save_composition (coding, from, to, Fcurrent_buffer ());
5679 else
5680 coding_allocate_composition_data (coding, from);
5681 }
5682
5683 /* Try to skip the heading and tailing ASCIIs. We can't skip them
5684 if we must run CCL program or there are compositions to
5685 encode. */
5686 if (coding->type != coding_type_ccl
5687 && (! coding->cmp_data || coding->cmp_data->used == 0))
5688 {
5689 int from_byte_orig = from_byte, to_byte_orig = to_byte;
5690
5691 if (from < GPT && GPT < to)
5692 move_gap_both (from, from_byte);
5693 SHRINK_CONVERSION_REGION (&from_byte, &to_byte, coding, NULL, encodep);
5694 if (from_byte == to_byte
5695 && (encodep || NILP (coding->post_read_conversion))
5696 && ! CODING_REQUIRE_FLUSHING (coding))
5697 {
5698 coding->produced = len_byte;
5699 coding->produced_char = len;
5700 if (!replace)
5701 /* We must record and adjust for this new text now. */
5702 adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len);
5703 coding_free_composition_data (coding);
5704 return 0;
5705 }
5706
5707 head_skip = from_byte - from_byte_orig;
5708 tail_skip = to_byte_orig - to_byte;
5709 total_skip = head_skip + tail_skip;
5710 from += head_skip;
5711 to -= tail_skip;
5712 len -= total_skip; len_byte -= total_skip;
5713 }
5714
5715 /* For conversion, we must put the gap before the text in addition to
5716 making the gap larger for efficient decoding. The required gap
5717 size starts from 2000 which is the magic number used in make_gap.
5718 But, after one batch of conversion, it will be incremented if we
5719 find that it is not enough . */
5720 require = 2000;
5721
5722 if (GAP_SIZE < require)
5723 make_gap (require - GAP_SIZE);
5724 move_gap_both (from, from_byte);
5725
5726 inserted = inserted_byte = 0;
5727
5728 GAP_SIZE += len_byte;
5729 ZV -= len;
5730 Z -= len;
5731 ZV_BYTE -= len_byte;
5732 Z_BYTE -= len_byte;
5733
5734 if (GPT - BEG < BEG_UNCHANGED)
5735 BEG_UNCHANGED = GPT - BEG;
5736 if (Z - GPT < END_UNCHANGED)
5737 END_UNCHANGED = Z - GPT;
5738
5739 if (!encodep && coding->src_multibyte)
5740 {
5741 /* Decoding routines expects that the source text is unibyte.
5742 We must convert 8-bit characters of multibyte form to
5743 unibyte. */
5744 int len_byte_orig = len_byte;
5745 len_byte = str_as_unibyte (GAP_END_ADDR - len_byte, len_byte);
5746 if (len_byte < len_byte_orig)
5747 safe_bcopy (GAP_END_ADDR - len_byte_orig, GAP_END_ADDR - len_byte,
5748 len_byte);
5749 coding->src_multibyte = 0;
5750 }
5751
5752 for (;;)
5753 {
5754 int result;
5755
5756 /* The buffer memory is now:
5757 +--------+converted-text+---------+-------original-text-------+---+
5758 |<-from->|<--inserted-->|---------|<--------len_byte--------->|---|
5759 |<---------------------- GAP ----------------------->| */
5760 src = GAP_END_ADDR - len_byte;
5761 dst = GPT_ADDR + inserted_byte;
5762
5763 if (encodep)
5764 result = encode_coding (coding, src, dst, len_byte, 0);
5765 else
5766 {
5767 if (coding->composing != COMPOSITION_DISABLED)
5768 coding->cmp_data->char_offset = from + inserted;
5769 result = decode_coding (coding, src, dst, len_byte, 0);
5770 }
5771
5772 /* The buffer memory is now:
5773 +--------+-------converted-text----+--+------original-text----+---+
5774 |<-from->|<-inserted->|<-produced->|--|<-(len_byte-consumed)->|---|
5775 |<---------------------- GAP ----------------------->| */
5776
5777 inserted += coding->produced_char;
5778 inserted_byte += coding->produced;
5779 len_byte -= coding->consumed;
5780
5781 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5782 {
5783 coding_allocate_composition_data (coding, from + inserted);
5784 continue;
5785 }
5786
5787 src += coding->consumed;
5788 dst += coding->produced;
5789
5790 if (result == CODING_FINISH_NORMAL)
5791 {
5792 src += len_byte;
5793 break;
5794 }
5795 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
5796 {
5797 unsigned char *pend = dst, *p = pend - inserted_byte;
5798 Lisp_Object eol_type;
5799
5800 /* Encode LFs back to the original eol format (CR or CRLF). */
5801 if (coding->eol_type == CODING_EOL_CR)
5802 {
5803 while (p < pend) if (*p++ == '\n') p[-1] = '\r';
5804 }
5805 else
5806 {
5807 int count = 0;
5808
5809 while (p < pend) if (*p++ == '\n') count++;
5810 if (src - dst < count)
5811 {
5812 /* We don't have sufficient room for encoding LFs
5813 back to CRLF. We must record converted and
5814 not-yet-converted text back to the buffer
5815 content, enlarge the gap, then record them out of
5816 the buffer contents again. */
5817 int add = len_byte + inserted_byte;
5818
5819 GAP_SIZE -= add;
5820 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5821 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5822 make_gap (count - GAP_SIZE);
5823 GAP_SIZE += add;
5824 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5825 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5826 /* Don't forget to update SRC, DST, and PEND. */
5827 src = GAP_END_ADDR - len_byte;
5828 dst = GPT_ADDR + inserted_byte;
5829 pend = dst;
5830 }
5831 inserted += count;
5832 inserted_byte += count;
5833 coding->produced += count;
5834 p = dst = pend + count;
5835 while (count)
5836 {
5837 *--p = *--pend;
5838 if (*p == '\n') count--, *--p = '\r';
5839 }
5840 }
5841
5842 /* Suppress eol-format conversion in the further conversion. */
5843 coding->eol_type = CODING_EOL_LF;
5844
5845 /* Set the coding system symbol to that for Unix-like EOL. */
5846 eol_type = Fget (saved_coding_symbol, Qeol_type);
5847 if (VECTORP (eol_type)
5848 && XVECTOR (eol_type)->size == 3
5849 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5850 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5851 else
5852 coding->symbol = saved_coding_symbol;
5853
5854 continue;
5855 }
5856 if (len_byte <= 0)
5857 {
5858 if (coding->type != coding_type_ccl
5859 || coding->mode & CODING_MODE_LAST_BLOCK)
5860 break;
5861 coding->mode |= CODING_MODE_LAST_BLOCK;
5862 continue;
5863 }
5864 if (result == CODING_FINISH_INSUFFICIENT_SRC)
5865 {
5866 /* The source text ends in invalid codes. Let's just
5867 make them valid buffer contents, and finish conversion. */
5868 if (multibyte_p)
5869 {
5870 unsigned char *start = dst;
5871
5872 inserted += len_byte;
5873 while (len_byte--)
5874 {
5875 int c = *src++;
5876 dst += CHAR_STRING (c, dst);
5877 }
5878
5879 inserted_byte += dst - start;
5880 }
5881 else
5882 {
5883 inserted += len_byte;
5884 inserted_byte += len_byte;
5885 while (len_byte--)
5886 *dst++ = *src++;
5887 }
5888 break;
5889 }
5890 if (result == CODING_FINISH_INTERRUPT)
5891 {
5892 /* The conversion procedure was interrupted by a user. */
5893 break;
5894 }
5895 /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */
5896 if (coding->consumed < 1)
5897 {
5898 /* It's quite strange to require more memory without
5899 consuming any bytes. Perhaps CCL program bug. */
5900 break;
5901 }
5902 if (first)
5903 {
5904 /* We have just done the first batch of conversion which was
5905 stopped because of insufficient gap. Let's reconsider the
5906 required gap size (i.e. SRT - DST) now.
5907
5908 We have converted ORIG bytes (== coding->consumed) into
5909 NEW bytes (coding->produced). To convert the remaining
5910 LEN bytes, we may need REQUIRE bytes of gap, where:
5911 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
5912 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
5913 Here, we are sure that NEW >= ORIG. */
5914
5915 if (coding->produced <= coding->consumed)
5916 {
5917 /* This happens because of CCL-based coding system with
5918 eol-type CRLF. */
5919 require = 0;
5920 }
5921 else
5922 {
5923 float ratio = coding->produced - coding->consumed;
5924 ratio /= coding->consumed;
5925 require = len_byte * ratio;
5926 }
5927 first = 0;
5928 }
5929 if ((src - dst) < (require + 2000))
5930 {
5931 /* See the comment above the previous call of make_gap. */
5932 int add = len_byte + inserted_byte;
5933
5934 GAP_SIZE -= add;
5935 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5936 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5937 make_gap (require + 2000);
5938 GAP_SIZE += add;
5939 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5940 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5941 }
5942 }
5943 if (src - dst > 0) *dst = 0; /* Put an anchor. */
5944
5945 if (encodep && coding->dst_multibyte)
5946 {
5947 /* The output is unibyte. We must convert 8-bit characters to
5948 multibyte form. */
5949 if (inserted_byte * 2 > GAP_SIZE)
5950 {
5951 GAP_SIZE -= inserted_byte;
5952 ZV += inserted_byte; Z += inserted_byte;
5953 ZV_BYTE += inserted_byte; Z_BYTE += inserted_byte;
5954 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5955 make_gap (inserted_byte - GAP_SIZE);
5956 GAP_SIZE += inserted_byte;
5957 ZV -= inserted_byte; Z -= inserted_byte;
5958 ZV_BYTE -= inserted_byte; Z_BYTE -= inserted_byte;
5959 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5960 }
5961 inserted_byte = str_to_multibyte (GPT_ADDR, GAP_SIZE, inserted_byte);
5962 }
5963
5964 /* If we shrank the conversion area, adjust it now. */
5965 if (total_skip > 0)
5966 {
5967 if (tail_skip > 0)
5968 safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip);
5969 inserted += total_skip; inserted_byte += total_skip;
5970 GAP_SIZE += total_skip;
5971 GPT -= head_skip; GPT_BYTE -= head_skip;
5972 ZV -= total_skip; ZV_BYTE -= total_skip;
5973 Z -= total_skip; Z_BYTE -= total_skip;
5974 from -= head_skip; from_byte -= head_skip;
5975 to += tail_skip; to_byte += tail_skip;
5976 }
5977
5978 prev_Z = Z;
5979 if (! EQ (current_buffer->undo_list, Qt))
5980 adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte);
5981 else
5982 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del,
5983 inserted, inserted_byte);
5984 inserted = Z - prev_Z;
5985
5986 if (!encodep && coding->cmp_data && coding->cmp_data->used)
5987 coding_restore_composition (coding, Fcurrent_buffer ());
5988 coding_free_composition_data (coding);
5989
5990 if (! inhibit_pre_post_conversion
5991 && ! encodep && ! NILP (coding->post_read_conversion))
5992 {
5993 Lisp_Object val;
5994 Lisp_Object saved_coding_system;
5995
5996 if (from != PT)
5997 TEMP_SET_PT_BOTH (from, from_byte);
5998 prev_Z = Z;
5999 record_unwind_protect (code_convert_region_unwind,
6000 Fcons (Vlast_coding_system_used, Qnil));
6001 saved_coding_system = Vlast_coding_system_used;
6002 Vlast_coding_system_used = coding->symbol;
6003 /* We should not call any more pre-write/post-read-conversion
6004 functions while this post-read-conversion is running. */
6005 inhibit_pre_post_conversion = 1;
6006 val = call1 (coding->post_read_conversion, make_number (inserted));
6007 inhibit_pre_post_conversion = 0;
6008 coding->symbol = Vlast_coding_system_used;
6009 Vlast_coding_system_used = saved_coding_system;
6010 /* Discard the unwind protect. */
6011 specpdl_ptr--;
6012 CHECK_NUMBER (val);
6013 inserted += Z - prev_Z;
6014 }
6015
6016 if (orig_point >= from)
6017 {
6018 if (orig_point >= from + orig_len)
6019 orig_point += inserted - orig_len;
6020 else
6021 orig_point = from;
6022 TEMP_SET_PT (orig_point);
6023 }
6024
6025 if (replace)
6026 {
6027 signal_after_change (from, to - from, inserted);
6028 update_compositions (from, from + inserted, CHECK_BORDER);
6029 }
6030
6031 {
6032 coding->consumed = to_byte - from_byte;
6033 coding->consumed_char = to - from;
6034 coding->produced = inserted_byte;
6035 coding->produced_char = inserted;
6036 }
6037
6038 return 0;
6039 }
6040
6041 /* Name (or base name) of work buffer for code conversion. */
6042 static Lisp_Object Vcode_conversion_workbuf_name;
6043
6044 /* Set the current buffer to the working buffer prepared for
6045 code-conversion. MULTIBYTE specifies the multibyteness of the
6046 buffer. Return the buffer we set if it must be killed after use.
6047 Otherwise return Qnil. */
6048
6049 static Lisp_Object
6050 set_conversion_work_buffer (multibyte)
6051 int multibyte;
6052 {
6053 Lisp_Object buffer, buffer_to_kill;
6054 struct buffer *buf;
6055
6056 buffer = Fget_buffer_create (Vcode_conversion_workbuf_name);
6057 buf = XBUFFER (buffer);
6058 if (buf == current_buffer)
6059 {
6060 /* As we are already in the work buffer, we must generate a new
6061 buffer for the work. */
6062 Lisp_Object name;
6063
6064 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
6065 buffer = buffer_to_kill = Fget_buffer_create (name);
6066 buf = XBUFFER (buffer);
6067 }
6068 else
6069 buffer_to_kill = Qnil;
6070
6071 delete_all_overlays (buf);
6072 buf->directory = current_buffer->directory;
6073 buf->read_only = Qnil;
6074 buf->filename = Qnil;
6075 buf->undo_list = Qt;
6076 eassert (buf->overlays_before == NULL);
6077 eassert (buf->overlays_after == NULL);
6078 set_buffer_internal (buf);
6079 if (BEG != BEGV || Z != ZV)
6080 Fwiden ();
6081 del_range_2 (BEG, BEG_BYTE, Z, Z_BYTE, 0);
6082 buf->enable_multibyte_characters = multibyte ? Qt : Qnil;
6083 return buffer_to_kill;
6084 }
6085
6086 Lisp_Object
6087 run_pre_post_conversion_on_str (str, coding, encodep)
6088 Lisp_Object str;
6089 struct coding_system *coding;
6090 int encodep;
6091 {
6092 int count = SPECPDL_INDEX ();
6093 struct gcpro gcpro1, gcpro2;
6094 int multibyte = STRING_MULTIBYTE (str);
6095 Lisp_Object old_deactivate_mark;
6096 Lisp_Object buffer_to_kill;
6097 Lisp_Object unwind_arg;
6098
6099 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6100 /* It is not crucial to specbind this. */
6101 old_deactivate_mark = Vdeactivate_mark;
6102 GCPRO2 (str, old_deactivate_mark);
6103
6104 /* We must insert the contents of STR as is without
6105 unibyte<->multibyte conversion. For that, we adjust the
6106 multibyteness of the working buffer to that of STR. */
6107 buffer_to_kill = set_conversion_work_buffer (multibyte);
6108 if (NILP (buffer_to_kill))
6109 unwind_arg = Fcons (Vlast_coding_system_used, Qnil);
6110 else
6111 unwind_arg = list2 (Vlast_coding_system_used, buffer_to_kill);
6112 record_unwind_protect (code_convert_region_unwind, unwind_arg);
6113
6114 insert_from_string (str, 0, 0,
6115 SCHARS (str), SBYTES (str), 0);
6116 UNGCPRO;
6117 inhibit_pre_post_conversion = 1;
6118 if (encodep)
6119 {
6120 struct buffer *prev = current_buffer;
6121
6122 call2 (coding->pre_write_conversion, make_number (BEG), make_number (Z));
6123 if (prev != current_buffer)
6124 /* We must kill the current buffer too. */
6125 Fsetcdr (unwind_arg, Fcons (Fcurrent_buffer (), XCDR (unwind_arg)));
6126 }
6127 else
6128 {
6129 Vlast_coding_system_used = coding->symbol;
6130 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6131 call1 (coding->post_read_conversion, make_number (Z - BEG));
6132 coding->symbol = Vlast_coding_system_used;
6133 }
6134 inhibit_pre_post_conversion = 0;
6135 Vdeactivate_mark = old_deactivate_mark;
6136 str = make_buffer_string (BEG, Z, 1);
6137 return unbind_to (count, str);
6138 }
6139
6140
6141 /* Run pre-write-conversion function of CODING on NCHARS/NBYTES
6142 text in *STR. *SIZE is the allocated bytes for STR. As it
6143 is intended that this function is called from encode_terminal_code,
6144 the pre-write-conversion function is run by safe_call and thus
6145 "Error during redisplay: ..." is logged when an error occurs.
6146
6147 Store the resulting text in *STR and set CODING->produced_char and
6148 CODING->produced to the number of characters and bytes
6149 respectively. If the size of *STR is too small, enlarge it by
6150 xrealloc and update *STR and *SIZE. */
6151
6152 void
6153 run_pre_write_conversin_on_c_str (str, size, nchars, nbytes, coding)
6154 unsigned char **str;
6155 int *size, nchars, nbytes;
6156 struct coding_system *coding;
6157 {
6158 struct gcpro gcpro1, gcpro2;
6159 struct buffer *cur = current_buffer;
6160 struct buffer *prev;
6161 Lisp_Object old_deactivate_mark, old_last_coding_system_used;
6162 Lisp_Object args[3];
6163 Lisp_Object buffer_to_kill;
6164
6165 /* It is not crucial to specbind this. */
6166 old_deactivate_mark = Vdeactivate_mark;
6167 old_last_coding_system_used = Vlast_coding_system_used;
6168 GCPRO2 (old_deactivate_mark, old_last_coding_system_used);
6169
6170 /* We must insert the contents of STR as is without
6171 unibyte<->multibyte conversion. For that, we adjust the
6172 multibyteness of the working buffer to that of STR. */
6173 buffer_to_kill = set_conversion_work_buffer (coding->src_multibyte);
6174 insert_1_both (*str, nchars, nbytes, 0, 0, 0);
6175 UNGCPRO;
6176 inhibit_pre_post_conversion = 1;
6177 prev = current_buffer;
6178 args[0] = coding->pre_write_conversion;
6179 args[1] = make_number (BEG);
6180 args[2] = make_number (Z);
6181 safe_call (3, args);
6182 inhibit_pre_post_conversion = 0;
6183 Vdeactivate_mark = old_deactivate_mark;
6184 Vlast_coding_system_used = old_last_coding_system_used;
6185 coding->produced_char = Z - BEG;
6186 coding->produced = Z_BYTE - BEG_BYTE;
6187 if (coding->produced > *size)
6188 {
6189 *size = coding->produced;
6190 *str = xrealloc (*str, *size);
6191 }
6192 if (BEG < GPT && GPT < Z)
6193 move_gap (BEG);
6194 bcopy (BEG_ADDR, *str, coding->produced);
6195 coding->src_multibyte
6196 = ! NILP (current_buffer->enable_multibyte_characters);
6197 if (prev != current_buffer)
6198 Fkill_buffer (Fcurrent_buffer ());
6199 set_buffer_internal (cur);
6200 if (! NILP (buffer_to_kill))
6201 Fkill_buffer (buffer_to_kill);
6202 }
6203
6204
6205 Lisp_Object
6206 decode_coding_string (str, coding, nocopy)
6207 Lisp_Object str;
6208 struct coding_system *coding;
6209 int nocopy;
6210 {
6211 int len;
6212 struct conversion_buffer buf;
6213 int from, to_byte;
6214 Lisp_Object saved_coding_symbol;
6215 int result;
6216 int require_decoding;
6217 int shrinked_bytes = 0;
6218 Lisp_Object newstr;
6219 int consumed, consumed_char, produced, produced_char;
6220
6221 from = 0;
6222 to_byte = SBYTES (str);
6223
6224 saved_coding_symbol = coding->symbol;
6225 coding->src_multibyte = STRING_MULTIBYTE (str);
6226 coding->dst_multibyte = 1;
6227 coding->heading_ascii = 0;
6228
6229 if (CODING_REQUIRE_DETECTION (coding))
6230 {
6231 /* See the comments in code_convert_region. */
6232 if (coding->type == coding_type_undecided)
6233 {
6234 detect_coding (coding, SDATA (str), to_byte);
6235 if (coding->type == coding_type_undecided)
6236 {
6237 coding->type = coding_type_emacs_mule;
6238 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
6239 /* As emacs-mule decoder will handle composition, we
6240 need this setting to allocate coding->cmp_data
6241 later. */
6242 coding->composing = COMPOSITION_NO;
6243 }
6244 }
6245 if (coding->eol_type == CODING_EOL_UNDECIDED
6246 && coding->type != coding_type_ccl)
6247 {
6248 saved_coding_symbol = coding->symbol;
6249 detect_eol (coding, SDATA (str), to_byte);
6250 if (coding->eol_type == CODING_EOL_UNDECIDED)
6251 coding->eol_type = CODING_EOL_LF;
6252 /* We had better recover the original eol format if we
6253 encounter an inconsistent eol format while decoding. */
6254 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
6255 }
6256 }
6257
6258 if (coding->type == coding_type_no_conversion
6259 || coding->type == coding_type_raw_text)
6260 coding->dst_multibyte = 0;
6261
6262 require_decoding = CODING_REQUIRE_DECODING (coding);
6263
6264 if (STRING_MULTIBYTE (str))
6265 {
6266 /* Decoding routines expect the source text to be unibyte. */
6267 str = Fstring_as_unibyte (str);
6268 to_byte = SBYTES (str);
6269 nocopy = 1;
6270 coding->src_multibyte = 0;
6271 }
6272
6273 /* Try to skip the heading and tailing ASCIIs. */
6274 if (require_decoding && coding->type != coding_type_ccl)
6275 {
6276 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, SDATA (str),
6277 0);
6278 if (from == to_byte)
6279 require_decoding = 0;
6280 shrinked_bytes = from + (SBYTES (str) - to_byte);
6281 }
6282
6283 if (!require_decoding
6284 && !(SYMBOLP (coding->post_read_conversion)
6285 && !NILP (Ffboundp (coding->post_read_conversion))))
6286 {
6287 coding->consumed = SBYTES (str);
6288 coding->consumed_char = SCHARS (str);
6289 if (coding->dst_multibyte)
6290 {
6291 str = Fstring_as_multibyte (str);
6292 nocopy = 1;
6293 }
6294 coding->produced = SBYTES (str);
6295 coding->produced_char = SCHARS (str);
6296 return (nocopy ? str : Fcopy_sequence (str));
6297 }
6298
6299 if (coding->composing != COMPOSITION_DISABLED)
6300 coding_allocate_composition_data (coding, from);
6301 len = decoding_buffer_size (coding, to_byte - from);
6302 allocate_conversion_buffer (buf, len);
6303
6304 consumed = consumed_char = produced = produced_char = 0;
6305 while (1)
6306 {
6307 result = decode_coding (coding, SDATA (str) + from + consumed,
6308 buf.data + produced, to_byte - from - consumed,
6309 buf.size - produced);
6310 consumed += coding->consumed;
6311 consumed_char += coding->consumed_char;
6312 produced += coding->produced;
6313 produced_char += coding->produced_char;
6314 if (result == CODING_FINISH_NORMAL
6315 || result == CODING_FINISH_INTERRUPT
6316 || (result == CODING_FINISH_INSUFFICIENT_SRC
6317 && coding->consumed == 0))
6318 break;
6319 if (result == CODING_FINISH_INSUFFICIENT_CMP)
6320 coding_allocate_composition_data (coding, from + produced_char);
6321 else if (result == CODING_FINISH_INSUFFICIENT_DST)
6322 extend_conversion_buffer (&buf);
6323 else if (result == CODING_FINISH_INCONSISTENT_EOL)
6324 {
6325 Lisp_Object eol_type;
6326
6327 /* Recover the original EOL format. */
6328 if (coding->eol_type == CODING_EOL_CR)
6329 {
6330 unsigned char *p;
6331 for (p = buf.data; p < buf.data + produced; p++)
6332 if (*p == '\n') *p = '\r';
6333 }
6334 else if (coding->eol_type == CODING_EOL_CRLF)
6335 {
6336 int num_eol = 0;
6337 unsigned char *p0, *p1;
6338 for (p0 = buf.data, p1 = p0 + produced; p0 < p1; p0++)
6339 if (*p0 == '\n') num_eol++;
6340 if (produced + num_eol >= buf.size)
6341 extend_conversion_buffer (&buf);
6342 for (p0 = buf.data + produced, p1 = p0 + num_eol; p0 > buf.data;)
6343 {
6344 *--p1 = *--p0;
6345 if (*p0 == '\n') *--p1 = '\r';
6346 }
6347 produced += num_eol;
6348 produced_char += num_eol;
6349 }
6350 /* Suppress eol-format conversion in the further conversion. */
6351 coding->eol_type = CODING_EOL_LF;
6352
6353 /* Set the coding system symbol to that for Unix-like EOL. */
6354 eol_type = Fget (saved_coding_symbol, Qeol_type);
6355 if (VECTORP (eol_type)
6356 && XVECTOR (eol_type)->size == 3
6357 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
6358 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
6359 else
6360 coding->symbol = saved_coding_symbol;
6361
6362
6363 }
6364 }
6365
6366 coding->consumed = consumed;
6367 coding->consumed_char = consumed_char;
6368 coding->produced = produced;
6369 coding->produced_char = produced_char;
6370
6371 if (coding->dst_multibyte)
6372 newstr = make_uninit_multibyte_string (produced_char + shrinked_bytes,
6373 produced + shrinked_bytes);
6374 else
6375 newstr = make_uninit_string (produced + shrinked_bytes);
6376 if (from > 0)
6377 STRING_COPYIN (newstr, 0, SDATA (str), from);
6378 STRING_COPYIN (newstr, from, buf.data, produced);
6379 if (shrinked_bytes > from)
6380 STRING_COPYIN (newstr, from + produced,
6381 SDATA (str) + to_byte,
6382 shrinked_bytes - from);
6383 free_conversion_buffer (&buf);
6384
6385 coding->consumed += shrinked_bytes;
6386 coding->consumed_char += shrinked_bytes;
6387 coding->produced += shrinked_bytes;
6388 coding->produced_char += shrinked_bytes;
6389
6390 if (coding->cmp_data && coding->cmp_data->used)
6391 coding_restore_composition (coding, newstr);
6392 coding_free_composition_data (coding);
6393
6394 if (SYMBOLP (coding->post_read_conversion)
6395 && !NILP (Ffboundp (coding->post_read_conversion)))
6396 newstr = run_pre_post_conversion_on_str (newstr, coding, 0);
6397
6398 return newstr;
6399 }
6400
6401 Lisp_Object
6402 encode_coding_string (str, coding, nocopy)
6403 Lisp_Object str;
6404 struct coding_system *coding;
6405 int nocopy;
6406 {
6407 int len;
6408 struct conversion_buffer buf;
6409 int from, to, to_byte;
6410 int result;
6411 int shrinked_bytes = 0;
6412 Lisp_Object newstr;
6413 int consumed, consumed_char, produced, produced_char;
6414
6415 if (SYMBOLP (coding->pre_write_conversion)
6416 && !NILP (Ffboundp (coding->pre_write_conversion)))
6417 {
6418 str = run_pre_post_conversion_on_str (str, coding, 1);
6419 /* As STR is just newly generated, we don't have to copy it
6420 anymore. */
6421 nocopy = 1;
6422 }
6423
6424 from = 0;
6425 to = SCHARS (str);
6426 to_byte = SBYTES (str);
6427
6428 /* Encoding routines determine the multibyteness of the source text
6429 by coding->src_multibyte. */
6430 coding->src_multibyte = SCHARS (str) < SBYTES (str);
6431 coding->dst_multibyte = 0;
6432 if (! CODING_REQUIRE_ENCODING (coding))
6433 goto no_need_of_encoding;
6434
6435 if (coding->composing != COMPOSITION_DISABLED)
6436 coding_save_composition (coding, from, to, str);
6437
6438 /* Try to skip the heading and tailing ASCIIs. We can't skip them
6439 if we must run CCL program or there are compositions to
6440 encode. */
6441 coding->heading_ascii = 0;
6442 if (coding->type != coding_type_ccl
6443 && (! coding->cmp_data || coding->cmp_data->used == 0))
6444 {
6445 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, SDATA (str),
6446 1);
6447 if (from == to_byte)
6448 {
6449 coding_free_composition_data (coding);
6450 goto no_need_of_encoding;
6451 }
6452 shrinked_bytes = from + (SBYTES (str) - to_byte);
6453 }
6454
6455 len = encoding_buffer_size (coding, to_byte - from);
6456 allocate_conversion_buffer (buf, len);
6457
6458 consumed = consumed_char = produced = produced_char = 0;
6459 while (1)
6460 {
6461 result = encode_coding (coding, SDATA (str) + from + consumed,
6462 buf.data + produced, to_byte - from - consumed,
6463 buf.size - produced);
6464 consumed += coding->consumed;
6465 consumed_char += coding->consumed_char;
6466 produced += coding->produced;
6467 produced_char += coding->produced_char;
6468 if (result == CODING_FINISH_NORMAL
6469 || result == CODING_FINISH_INTERRUPT
6470 || (result == CODING_FINISH_INSUFFICIENT_SRC
6471 && coding->consumed == 0))
6472 break;
6473 /* Now result should be CODING_FINISH_INSUFFICIENT_DST. */
6474 extend_conversion_buffer (&buf);
6475 }
6476
6477 coding->consumed = consumed;
6478 coding->consumed_char = consumed_char;
6479 coding->produced = produced;
6480 coding->produced_char = produced_char;
6481
6482 newstr = make_uninit_string (produced + shrinked_bytes);
6483 if (from > 0)
6484 STRING_COPYIN (newstr, 0, SDATA (str), from);
6485 STRING_COPYIN (newstr, from, buf.data, produced);
6486 if (shrinked_bytes > from)
6487 STRING_COPYIN (newstr, from + produced,
6488 SDATA (str) + to_byte,
6489 shrinked_bytes - from);
6490
6491 free_conversion_buffer (&buf);
6492 coding_free_composition_data (coding);
6493
6494 return newstr;
6495
6496 no_need_of_encoding:
6497 coding->consumed = SBYTES (str);
6498 coding->consumed_char = SCHARS (str);
6499 if (STRING_MULTIBYTE (str))
6500 {
6501 if (nocopy)
6502 /* We are sure that STR doesn't contain a multibyte
6503 character. */
6504 STRING_SET_UNIBYTE (str);
6505 else
6506 {
6507 str = Fstring_as_unibyte (str);
6508 nocopy = 1;
6509 }
6510 }
6511 coding->produced = SBYTES (str);
6512 coding->produced_char = SCHARS (str);
6513 return (nocopy ? str : Fcopy_sequence (str));
6514 }
6515
6516 \f
6517 #ifdef emacs
6518 /*** 8. Emacs Lisp library functions ***/
6519
6520 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
6521 doc: /* Return t if OBJECT is nil or a coding-system.
6522 See the documentation of `make-coding-system' for information
6523 about coding-system objects. */)
6524 (obj)
6525 Lisp_Object obj;
6526 {
6527 if (NILP (obj))
6528 return Qt;
6529 if (!SYMBOLP (obj))
6530 return Qnil;
6531 if (! NILP (Fget (obj, Qcoding_system_define_form)))
6532 return Qt;
6533 /* Get coding-spec vector for OBJ. */
6534 obj = Fget (obj, Qcoding_system);
6535 return ((VECTORP (obj) && XVECTOR (obj)->size == 5)
6536 ? Qt : Qnil);
6537 }
6538
6539 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
6540 Sread_non_nil_coding_system, 1, 1, 0,
6541 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
6542 (prompt)
6543 Lisp_Object prompt;
6544 {
6545 Lisp_Object val;
6546 do
6547 {
6548 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
6549 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
6550 }
6551 while (SCHARS (val) == 0);
6552 return (Fintern (val, Qnil));
6553 }
6554
6555 DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
6556 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
6557 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
6558 (prompt, default_coding_system)
6559 Lisp_Object prompt, default_coding_system;
6560 {
6561 Lisp_Object val;
6562 if (SYMBOLP (default_coding_system))
6563 default_coding_system = SYMBOL_NAME (default_coding_system);
6564 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
6565 Qt, Qnil, Qcoding_system_history,
6566 default_coding_system, Qnil);
6567 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
6568 }
6569
6570 DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
6571 1, 1, 0,
6572 doc: /* Check validity of CODING-SYSTEM.
6573 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
6574 It is valid if it is nil or a symbol with a non-nil `coding-system' property.
6575 The value of this property should be a vector of length 5. */)
6576 (coding_system)
6577 Lisp_Object coding_system;
6578 {
6579 Lisp_Object define_form;
6580
6581 define_form = Fget (coding_system, Qcoding_system_define_form);
6582 if (! NILP (define_form))
6583 {
6584 Fput (coding_system, Qcoding_system_define_form, Qnil);
6585 safe_eval (define_form);
6586 }
6587 if (!NILP (Fcoding_system_p (coding_system)))
6588 return coding_system;
6589 xsignal1 (Qcoding_system_error, coding_system);
6590 }
6591 \f
6592 Lisp_Object
6593 detect_coding_system (src, src_bytes, highest, multibytep)
6594 const unsigned char *src;
6595 int src_bytes, highest;
6596 int multibytep;
6597 {
6598 int coding_mask, eol_type;
6599 Lisp_Object val, tmp;
6600 int dummy;
6601
6602 coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy, multibytep);
6603 eol_type = detect_eol_type (src, src_bytes, &dummy);
6604 if (eol_type == CODING_EOL_INCONSISTENT)
6605 eol_type = CODING_EOL_UNDECIDED;
6606
6607 if (!coding_mask)
6608 {
6609 val = Qundecided;
6610 if (eol_type != CODING_EOL_UNDECIDED)
6611 {
6612 Lisp_Object val2;
6613 val2 = Fget (Qundecided, Qeol_type);
6614 if (VECTORP (val2))
6615 val = XVECTOR (val2)->contents[eol_type];
6616 }
6617 return (highest ? val : Fcons (val, Qnil));
6618 }
6619
6620 /* At first, gather possible coding systems in VAL. */
6621 val = Qnil;
6622 for (tmp = Vcoding_category_list; CONSP (tmp); tmp = XCDR (tmp))
6623 {
6624 Lisp_Object category_val, category_index;
6625
6626 category_index = Fget (XCAR (tmp), Qcoding_category_index);
6627 category_val = Fsymbol_value (XCAR (tmp));
6628 if (!NILP (category_val)
6629 && NATNUMP (category_index)
6630 && (coding_mask & (1 << XFASTINT (category_index))))
6631 {
6632 val = Fcons (category_val, val);
6633 if (highest)
6634 break;
6635 }
6636 }
6637 if (!highest)
6638 val = Fnreverse (val);
6639
6640 /* Then, replace the elements with subsidiary coding systems. */
6641 for (tmp = val; CONSP (tmp); tmp = XCDR (tmp))
6642 {
6643 if (eol_type != CODING_EOL_UNDECIDED
6644 && eol_type != CODING_EOL_INCONSISTENT)
6645 {
6646 Lisp_Object eol;
6647 eol = Fget (XCAR (tmp), Qeol_type);
6648 if (VECTORP (eol))
6649 XSETCAR (tmp, XVECTOR (eol)->contents[eol_type]);
6650 }
6651 }
6652 return (highest ? XCAR (val) : val);
6653 }
6654
6655 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
6656 2, 3, 0,
6657 doc: /* Detect how the byte sequence in the region is encoded.
6658 Return a list of possible coding systems used on decoding a byte
6659 sequence containing the bytes in the region between START and END when
6660 the coding system `undecided' is specified. The list is ordered by
6661 priority decided in the current language environment.
6662
6663 If only ASCII characters are found (except for such ISO-2022 control
6664 characters ISO-2022 as ESC), it returns a list of single element
6665 `undecided' or its subsidiary coding system according to a detected
6666 end-of-line format.
6667
6668 If optional argument HIGHEST is non-nil, return the coding system of
6669 highest priority. */)
6670 (start, end, highest)
6671 Lisp_Object start, end, highest;
6672 {
6673 int from, to;
6674 int from_byte, to_byte;
6675 int include_anchor_byte = 0;
6676
6677 CHECK_NUMBER_COERCE_MARKER (start);
6678 CHECK_NUMBER_COERCE_MARKER (end);
6679
6680 validate_region (&start, &end);
6681 from = XINT (start), to = XINT (end);
6682 from_byte = CHAR_TO_BYTE (from);
6683 to_byte = CHAR_TO_BYTE (to);
6684
6685 if (from < GPT && to >= GPT)
6686 move_gap_both (to, to_byte);
6687 /* If we an anchor byte `\0' follows the region, we include it in
6688 the detecting source. Then code detectors can handle the tailing
6689 byte sequence more accurately.
6690
6691 Fix me: This is not a perfect solution. It is better that we
6692 add one more argument, say LAST_BLOCK, to all detect_coding_XXX.
6693 */
6694 if (to == Z || (to == GPT && GAP_SIZE > 0))
6695 include_anchor_byte = 1;
6696 return detect_coding_system (BYTE_POS_ADDR (from_byte),
6697 to_byte - from_byte + include_anchor_byte,
6698 !NILP (highest),
6699 !NILP (current_buffer
6700 ->enable_multibyte_characters));
6701 }
6702
6703 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
6704 1, 2, 0,
6705 doc: /* Detect how the byte sequence in STRING is encoded.
6706 Return a list of possible coding systems used on decoding a byte
6707 sequence containing the bytes in STRING when the coding system
6708 `undecided' is specified. The list is ordered by priority decided in
6709 the current language environment.
6710
6711 If only ASCII characters are found (except for such ISO-2022 control
6712 characters ISO-2022 as ESC), it returns a list of single element
6713 `undecided' or its subsidiary coding system according to a detected
6714 end-of-line format.
6715
6716 If optional argument HIGHEST is non-nil, return the coding system of
6717 highest priority. */)
6718 (string, highest)
6719 Lisp_Object string, highest;
6720 {
6721 CHECK_STRING (string);
6722
6723 return detect_coding_system (SDATA (string),
6724 /* "+ 1" is to include the anchor byte
6725 `\0'. With this, code detectors can
6726 handle the tailing bytes more
6727 accurately. */
6728 SBYTES (string) + 1,
6729 !NILP (highest),
6730 STRING_MULTIBYTE (string));
6731 }
6732
6733 /* Subroutine for Ffind_coding_systems_region_internal.
6734
6735 Return a list of coding systems that safely encode the multibyte
6736 text between P and PEND. SAFE_CODINGS, if non-nil, is an alist of
6737 possible coding systems. If it is nil, it means that we have not
6738 yet found any coding systems.
6739
6740 WORK_TABLE a char-table of which element is set to t once the
6741 element is looked up.
6742
6743 If a non-ASCII single byte char is found, set
6744 *single_byte_char_found to 1. */
6745
6746 static Lisp_Object
6747 find_safe_codings (p, pend, safe_codings, work_table, single_byte_char_found)
6748 unsigned char *p, *pend;
6749 Lisp_Object safe_codings, work_table;
6750 int *single_byte_char_found;
6751 {
6752 int c, len;
6753 Lisp_Object val, ch;
6754 Lisp_Object prev, tail;
6755
6756 if (NILP (safe_codings))
6757 goto done_safe_codings;
6758 while (p < pend)
6759 {
6760 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
6761 p += len;
6762 if (ASCII_BYTE_P (c))
6763 /* We can ignore ASCII characters here. */
6764 continue;
6765 if (SINGLE_BYTE_CHAR_P (c))
6766 *single_byte_char_found = 1;
6767 /* Check the safe coding systems for C. */
6768 ch = make_number (c);
6769 val = Faref (work_table, ch);
6770 if (EQ (val, Qt))
6771 /* This element was already checked. Ignore it. */
6772 continue;
6773 /* Remember that we checked this element. */
6774 Faset (work_table, ch, Qt);
6775
6776 for (prev = tail = safe_codings; CONSP (tail); tail = XCDR (tail))
6777 {
6778 Lisp_Object elt, translation_table, hash_table, accept_latin_extra;
6779 int encodable;
6780
6781 elt = XCAR (tail);
6782 if (CONSP (XCDR (elt)))
6783 {
6784 /* This entry has this format now:
6785 ( CODING SAFE-CHARS TRANSLATION-TABLE HASH-TABLE
6786 ACCEPT-LATIN-EXTRA ) */
6787 val = XCDR (elt);
6788 encodable = ! NILP (Faref (XCAR (val), ch));
6789 if (! encodable)
6790 {
6791 val = XCDR (val);
6792 translation_table = XCAR (val);
6793 hash_table = XCAR (XCDR (val));
6794 accept_latin_extra = XCAR (XCDR (XCDR (val)));
6795 }
6796 }
6797 else
6798 {
6799 /* This entry has this format now: ( CODING . SAFE-CHARS) */
6800 encodable = ! NILP (Faref (XCDR (elt), ch));
6801 if (! encodable)
6802 {
6803 /* Transform the format to:
6804 ( CODING SAFE-CHARS TRANSLATION-TABLE HASH-TABLE
6805 ACCEPT-LATIN-EXTRA ) */
6806 val = Fget (XCAR (elt), Qcoding_system);
6807 translation_table
6808 = Fplist_get (AREF (val, 3),
6809 Qtranslation_table_for_encode);
6810 if (SYMBOLP (translation_table))
6811 translation_table = Fget (translation_table,
6812 Qtranslation_table);
6813 hash_table
6814 = (CHAR_TABLE_P (translation_table)
6815 ? XCHAR_TABLE (translation_table)->extras[1]
6816 : Qnil);
6817 accept_latin_extra
6818 = ((EQ (AREF (val, 0), make_number (2))
6819 && VECTORP (AREF (val, 4)))
6820 ? AREF (AREF (val, 4), 16)
6821 : Qnil);
6822 XSETCAR (tail, list5 (XCAR (elt), XCDR (elt),
6823 translation_table, hash_table,
6824 accept_latin_extra));
6825 }
6826 }
6827
6828 if (! encodable
6829 && ((CHAR_TABLE_P (translation_table)
6830 && ! NILP (Faref (translation_table, ch)))
6831 || (HASH_TABLE_P (hash_table)
6832 && ! NILP (Fgethash (ch, hash_table, Qnil)))
6833 || (SINGLE_BYTE_CHAR_P (c)
6834 && ! NILP (accept_latin_extra)
6835 && VECTORP (Vlatin_extra_code_table)
6836 && ! NILP (AREF (Vlatin_extra_code_table, c)))))
6837 encodable = 1;
6838 if (encodable)
6839 prev = tail;
6840 else
6841 {
6842 /* Exclude this coding system from SAFE_CODINGS. */
6843 if (EQ (tail, safe_codings))
6844 {
6845 safe_codings = XCDR (safe_codings);
6846 if (NILP (safe_codings))
6847 goto done_safe_codings;
6848 }
6849 else
6850 XSETCDR (prev, XCDR (tail));
6851 }
6852 }
6853 }
6854
6855 done_safe_codings:
6856 /* If the above loop was terminated before P reaches PEND, it means
6857 SAFE_CODINGS was set to nil. If we have not yet found an
6858 non-ASCII single-byte char, check it now. */
6859 if (! *single_byte_char_found)
6860 while (p < pend)
6861 {
6862 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
6863 p += len;
6864 if (! ASCII_BYTE_P (c)
6865 && SINGLE_BYTE_CHAR_P (c))
6866 {
6867 *single_byte_char_found = 1;
6868 break;
6869 }
6870 }
6871 return safe_codings;
6872 }
6873
6874 DEFUN ("find-coding-systems-region-internal",
6875 Ffind_coding_systems_region_internal,
6876 Sfind_coding_systems_region_internal, 2, 2, 0,
6877 doc: /* Internal use only. */)
6878 (start, end)
6879 Lisp_Object start, end;
6880 {
6881 Lisp_Object work_table, safe_codings;
6882 int non_ascii_p = 0;
6883 int single_byte_char_found = 0;
6884 const unsigned char *p1, *p1end, *p2, *p2end, *p;
6885
6886 if (STRINGP (start))
6887 {
6888 if (!STRING_MULTIBYTE (start))
6889 return Qt;
6890 p1 = SDATA (start), p1end = p1 + SBYTES (start);
6891 p2 = p2end = p1end;
6892 if (SCHARS (start) != SBYTES (start))
6893 non_ascii_p = 1;
6894 }
6895 else
6896 {
6897 int from, to, stop;
6898
6899 CHECK_NUMBER_COERCE_MARKER (start);
6900 CHECK_NUMBER_COERCE_MARKER (end);
6901 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
6902 args_out_of_range (start, end);
6903 if (NILP (current_buffer->enable_multibyte_characters))
6904 return Qt;
6905 from = CHAR_TO_BYTE (XINT (start));
6906 to = CHAR_TO_BYTE (XINT (end));
6907 stop = from < GPT_BYTE && GPT_BYTE < to ? GPT_BYTE : to;
6908 p1 = BYTE_POS_ADDR (from), p1end = p1 + (stop - from);
6909 if (stop == to)
6910 p2 = p2end = p1end;
6911 else
6912 p2 = BYTE_POS_ADDR (stop), p2end = p2 + (to - stop);
6913 if (XINT (end) - XINT (start) != to - from)
6914 non_ascii_p = 1;
6915 }
6916
6917 if (!non_ascii_p)
6918 {
6919 /* We are sure that the text contains no multibyte character.
6920 Check if it contains eight-bit-graphic. */
6921 p = p1;
6922 for (p = p1; p < p1end && ASCII_BYTE_P (*p); p++);
6923 if (p == p1end)
6924 {
6925 for (p = p2; p < p2end && ASCII_BYTE_P (*p); p++);
6926 if (p == p2end)
6927 return Qt;
6928 }
6929 }
6930
6931 /* The text contains non-ASCII characters. */
6932
6933 work_table = Fmake_char_table (Qchar_coding_system, Qnil);
6934 safe_codings = Fcopy_sequence (XCDR (Vcoding_system_safe_chars));
6935
6936 safe_codings = find_safe_codings (p1, p1end, safe_codings, work_table,
6937 &single_byte_char_found);
6938 if (p2 < p2end)
6939 safe_codings = find_safe_codings (p2, p2end, safe_codings, work_table,
6940 &single_byte_char_found);
6941 if (EQ (safe_codings, XCDR (Vcoding_system_safe_chars)))
6942 safe_codings = Qt;
6943 else
6944 {
6945 /* Turn safe_codings to a list of coding systems... */
6946 Lisp_Object val;
6947
6948 if (single_byte_char_found)
6949 /* ... and append these for eight-bit chars. */
6950 val = Fcons (Qraw_text,
6951 Fcons (Qemacs_mule, Fcons (Qno_conversion, Qnil)));
6952 else
6953 /* ... and append generic coding systems. */
6954 val = Fcopy_sequence (XCAR (Vcoding_system_safe_chars));
6955
6956 for (; CONSP (safe_codings); safe_codings = XCDR (safe_codings))
6957 val = Fcons (XCAR (XCAR (safe_codings)), val);
6958 safe_codings = val;
6959 }
6960
6961 return safe_codings;
6962 }
6963
6964
6965 /* Search from position POS for such characters that are unencodable
6966 accoding to SAFE_CHARS, and return a list of their positions. P
6967 points where in the memory the character at POS exists. Limit the
6968 search at PEND or when Nth unencodable characters are found.
6969
6970 If SAFE_CHARS is a char table, an element for an unencodable
6971 character is nil.
6972
6973 If SAFE_CHARS is nil, all non-ASCII characters are unencodable.
6974
6975 Otherwise, SAFE_CHARS is t, and only eight-bit-contrl and
6976 eight-bit-graphic characters are unencodable. */
6977
6978 static Lisp_Object
6979 unencodable_char_position (safe_chars, pos, p, pend, n)
6980 Lisp_Object safe_chars;
6981 int pos;
6982 unsigned char *p, *pend;
6983 int n;
6984 {
6985 Lisp_Object pos_list;
6986
6987 pos_list = Qnil;
6988 while (p < pend)
6989 {
6990 int len;
6991 int c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, len);
6992
6993 if (c >= 128
6994 && (CHAR_TABLE_P (safe_chars)
6995 ? NILP (CHAR_TABLE_REF (safe_chars, c))
6996 : (NILP (safe_chars) || c < 256)))
6997 {
6998 pos_list = Fcons (make_number (pos), pos_list);
6999 if (--n <= 0)
7000 break;
7001 }
7002 pos++;
7003 p += len;
7004 }
7005 return Fnreverse (pos_list);
7006 }
7007
7008
7009 DEFUN ("unencodable-char-position", Funencodable_char_position,
7010 Sunencodable_char_position, 3, 5, 0,
7011 doc: /*
7012 Return position of first un-encodable character in a region.
7013 START and END specfiy the region and CODING-SYSTEM specifies the
7014 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7015
7016 If optional 4th argument COUNT is non-nil, it specifies at most how
7017 many un-encodable characters to search. In this case, the value is a
7018 list of positions.
7019
7020 If optional 5th argument STRING is non-nil, it is a string to search
7021 for un-encodable characters. In that case, START and END are indexes
7022 to the string. */)
7023 (start, end, coding_system, count, string)
7024 Lisp_Object start, end, coding_system, count, string;
7025 {
7026 int n;
7027 Lisp_Object safe_chars;
7028 struct coding_system coding;
7029 Lisp_Object positions;
7030 int from, to;
7031 unsigned char *p, *pend;
7032
7033 if (NILP (string))
7034 {
7035 validate_region (&start, &end);
7036 from = XINT (start);
7037 to = XINT (end);
7038 if (NILP (current_buffer->enable_multibyte_characters))
7039 return Qnil;
7040 p = CHAR_POS_ADDR (from);
7041 if (to == GPT)
7042 pend = GPT_ADDR;
7043 else
7044 pend = CHAR_POS_ADDR (to);
7045 }
7046 else
7047 {
7048 CHECK_STRING (string);
7049 CHECK_NATNUM (start);
7050 CHECK_NATNUM (end);
7051 from = XINT (start);
7052 to = XINT (end);
7053 if (from > to
7054 || to > SCHARS (string))
7055 args_out_of_range_3 (string, start, end);
7056 if (! STRING_MULTIBYTE (string))
7057 return Qnil;
7058 p = SDATA (string) + string_char_to_byte (string, from);
7059 pend = SDATA (string) + string_char_to_byte (string, to);
7060 }
7061
7062 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
7063
7064 if (NILP (count))
7065 n = 1;
7066 else
7067 {
7068 CHECK_NATNUM (count);
7069 n = XINT (count);
7070 }
7071
7072 if (coding.type == coding_type_no_conversion
7073 || coding.type == coding_type_raw_text)
7074 return Qnil;
7075
7076 if (coding.type == coding_type_undecided)
7077 safe_chars = Qnil;
7078 else
7079 safe_chars = coding_safe_chars (coding_system);
7080
7081 if (STRINGP (string)
7082 || from >= GPT || to <= GPT)
7083 positions = unencodable_char_position (safe_chars, from, p, pend, n);
7084 else
7085 {
7086 Lisp_Object args[2];
7087
7088 args[0] = unencodable_char_position (safe_chars, from, p, GPT_ADDR, n);
7089 n -= XINT (Flength (args[0]));
7090 if (n <= 0)
7091 positions = args[0];
7092 else
7093 {
7094 args[1] = unencodable_char_position (safe_chars, GPT, GAP_END_ADDR,
7095 pend, n);
7096 positions = Fappend (2, args);
7097 }
7098 }
7099
7100 return (NILP (count) ? Fcar (positions) : positions);
7101 }
7102
7103
7104 Lisp_Object
7105 code_convert_region1 (start, end, coding_system, encodep)
7106 Lisp_Object start, end, coding_system;
7107 int encodep;
7108 {
7109 struct coding_system coding;
7110 int from, to;
7111
7112 CHECK_NUMBER_COERCE_MARKER (start);
7113 CHECK_NUMBER_COERCE_MARKER (end);
7114 CHECK_SYMBOL (coding_system);
7115
7116 validate_region (&start, &end);
7117 from = XFASTINT (start);
7118 to = XFASTINT (end);
7119
7120 if (NILP (coding_system))
7121 return make_number (to - from);
7122
7123 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
7124 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
7125
7126 coding.mode |= CODING_MODE_LAST_BLOCK;
7127 coding.src_multibyte = coding.dst_multibyte
7128 = !NILP (current_buffer->enable_multibyte_characters);
7129 code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to),
7130 &coding, encodep, 1);
7131 Vlast_coding_system_used = coding.symbol;
7132 return make_number (coding.produced_char);
7133 }
7134
7135 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
7136 3, 3, "r\nzCoding system: ",
7137 doc: /* Decode the current region from the specified coding system.
7138 When called from a program, takes three arguments:
7139 START, END, and CODING-SYSTEM. START and END are buffer positions.
7140 This function sets `last-coding-system-used' to the precise coding system
7141 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7142 not fully specified.)
7143 It returns the length of the decoded text. */)
7144 (start, end, coding_system)
7145 Lisp_Object start, end, coding_system;
7146 {
7147 return code_convert_region1 (start, end, coding_system, 0);
7148 }
7149
7150 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
7151 3, 3, "r\nzCoding system: ",
7152 doc: /* Encode the current region into the specified coding system.
7153 When called from a program, takes three arguments:
7154 START, END, and CODING-SYSTEM. START and END are buffer positions.
7155 This function sets `last-coding-system-used' to the precise coding system
7156 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7157 not fully specified.)
7158 It returns the length of the encoded text. */)
7159 (start, end, coding_system)
7160 Lisp_Object start, end, coding_system;
7161 {
7162 return code_convert_region1 (start, end, coding_system, 1);
7163 }
7164
7165 Lisp_Object
7166 code_convert_string1 (string, coding_system, nocopy, encodep)
7167 Lisp_Object string, coding_system, nocopy;
7168 int encodep;
7169 {
7170 struct coding_system coding;
7171
7172 CHECK_STRING (string);
7173 CHECK_SYMBOL (coding_system);
7174
7175 if (NILP (coding_system))
7176 return (NILP (nocopy) ? Fcopy_sequence (string) : string);
7177
7178 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
7179 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
7180
7181 coding.mode |= CODING_MODE_LAST_BLOCK;
7182 string = (encodep
7183 ? encode_coding_string (string, &coding, !NILP (nocopy))
7184 : decode_coding_string (string, &coding, !NILP (nocopy)));
7185 Vlast_coding_system_used = coding.symbol;
7186
7187 return string;
7188 }
7189
7190 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
7191 2, 3, 0,
7192 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
7193 Optional arg NOCOPY non-nil means it is OK to return STRING itself
7194 if the decoding operation is trivial.
7195 This function sets `last-coding-system-used' to the precise coding system
7196 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7197 not fully specified.) */)
7198 (string, coding_system, nocopy)
7199 Lisp_Object string, coding_system, nocopy;
7200 {
7201 return code_convert_string1 (string, coding_system, nocopy, 0);
7202 }
7203
7204 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
7205 2, 3, 0,
7206 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
7207 Optional arg NOCOPY non-nil means it is OK to return STRING itself
7208 if the encoding operation is trivial.
7209 This function sets `last-coding-system-used' to the precise coding system
7210 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7211 not fully specified.) */)
7212 (string, coding_system, nocopy)
7213 Lisp_Object string, coding_system, nocopy;
7214 {
7215 return code_convert_string1 (string, coding_system, nocopy, 1);
7216 }
7217
7218 /* Encode or decode STRING according to CODING_SYSTEM.
7219 Do not set Vlast_coding_system_used.
7220
7221 This function is called only from macros DECODE_FILE and
7222 ENCODE_FILE, thus we ignore character composition. */
7223
7224 Lisp_Object
7225 code_convert_string_norecord (string, coding_system, encodep)
7226 Lisp_Object string, coding_system;
7227 int encodep;
7228 {
7229 struct coding_system coding;
7230
7231 CHECK_STRING (string);
7232 CHECK_SYMBOL (coding_system);
7233
7234 if (NILP (coding_system))
7235 return string;
7236
7237 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
7238 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
7239
7240 coding.composing = COMPOSITION_DISABLED;
7241 coding.mode |= CODING_MODE_LAST_BLOCK;
7242 return (encodep
7243 ? encode_coding_string (string, &coding, 1)
7244 : decode_coding_string (string, &coding, 1));
7245 }
7246 \f
7247 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
7248 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
7249 Return the corresponding character. */)
7250 (code)
7251 Lisp_Object code;
7252 {
7253 unsigned char c1, c2, s1, s2;
7254 Lisp_Object val;
7255
7256 CHECK_NUMBER (code);
7257 s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF;
7258 if (s1 == 0)
7259 {
7260 if (s2 < 0x80)
7261 XSETFASTINT (val, s2);
7262 else if (s2 >= 0xA0 || s2 <= 0xDF)
7263 XSETFASTINT (val, MAKE_CHAR (charset_katakana_jisx0201, s2, 0));
7264 else
7265 error ("Invalid Shift JIS code: %x", XFASTINT (code));
7266 }
7267 else
7268 {
7269 if ((s1 < 0x80 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF)
7270 || (s2 < 0x40 || s2 == 0x7F || s2 > 0xFC))
7271 error ("Invalid Shift JIS code: %x", XFASTINT (code));
7272 DECODE_SJIS (s1, s2, c1, c2);
7273 XSETFASTINT (val, MAKE_CHAR (charset_jisx0208, c1, c2));
7274 }
7275 return val;
7276 }
7277
7278 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
7279 doc: /* Encode a Japanese character CH to shift_jis encoding.
7280 Return the corresponding code in SJIS. */)
7281 (ch)
7282 Lisp_Object ch;
7283 {
7284 int charset, c1, c2, s1, s2;
7285 Lisp_Object val;
7286
7287 CHECK_NUMBER (ch);
7288 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
7289 if (charset == CHARSET_ASCII)
7290 {
7291 val = ch;
7292 }
7293 else if (charset == charset_jisx0208
7294 && c1 > 0x20 && c1 < 0x7F && c2 > 0x20 && c2 < 0x7F)
7295 {
7296 ENCODE_SJIS (c1, c2, s1, s2);
7297 XSETFASTINT (val, (s1 << 8) | s2);
7298 }
7299 else if (charset == charset_katakana_jisx0201
7300 && c1 > 0x20 && c2 < 0xE0)
7301 {
7302 XSETFASTINT (val, c1 | 0x80);
7303 }
7304 else
7305 error ("Can't encode to shift_jis: %d", XFASTINT (ch));
7306 return val;
7307 }
7308
7309 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
7310 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
7311 Return the corresponding character. */)
7312 (code)
7313 Lisp_Object code;
7314 {
7315 int charset;
7316 unsigned char b1, b2, c1, c2;
7317 Lisp_Object val;
7318
7319 CHECK_NUMBER (code);
7320 b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF;
7321 if (b1 == 0)
7322 {
7323 if (b2 >= 0x80)
7324 error ("Invalid BIG5 code: %x", XFASTINT (code));
7325 val = code;
7326 }
7327 else
7328 {
7329 if ((b1 < 0xA1 || b1 > 0xFE)
7330 || (b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE))
7331 error ("Invalid BIG5 code: %x", XFASTINT (code));
7332 DECODE_BIG5 (b1, b2, charset, c1, c2);
7333 XSETFASTINT (val, MAKE_CHAR (charset, c1, c2));
7334 }
7335 return val;
7336 }
7337
7338 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
7339 doc: /* Encode the Big5 character CH to BIG5 coding system.
7340 Return the corresponding character code in Big5. */)
7341 (ch)
7342 Lisp_Object ch;
7343 {
7344 int charset, c1, c2, b1, b2;
7345 Lisp_Object val;
7346
7347 CHECK_NUMBER (ch);
7348 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
7349 if (charset == CHARSET_ASCII)
7350 {
7351 val = ch;
7352 }
7353 else if ((charset == charset_big5_1
7354 && (XFASTINT (ch) >= 0x250a1 && XFASTINT (ch) <= 0x271ec))
7355 || (charset == charset_big5_2
7356 && XFASTINT (ch) >= 0x290a1 && XFASTINT (ch) <= 0x2bdb2))
7357 {
7358 ENCODE_BIG5 (charset, c1, c2, b1, b2);
7359 XSETFASTINT (val, (b1 << 8) | b2);
7360 }
7361 else
7362 error ("Can't encode to Big5: %d", XFASTINT (ch));
7363 return val;
7364 }
7365 \f
7366 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
7367 Sset_terminal_coding_system_internal, 1, 2, 0,
7368 doc: /* Internal use only. */)
7369 (coding_system, terminal)
7370 Lisp_Object coding_system;
7371 Lisp_Object terminal;
7372 {
7373 struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
7374 CHECK_SYMBOL (coding_system);
7375 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
7376 /* We had better not send unsafe characters to terminal. */
7377 terminal_coding->mode |= CODING_MODE_INHIBIT_UNENCODABLE_CHAR;
7378 /* Character composition should be disabled. */
7379 terminal_coding->composing = COMPOSITION_DISABLED;
7380 /* Error notification should be suppressed. */
7381 terminal_coding->suppress_error = 1;
7382 terminal_coding->src_multibyte = 1;
7383 terminal_coding->dst_multibyte = 0;
7384 return Qnil;
7385 }
7386
7387 DEFUN ("set-safe-terminal-coding-system-internal", Fset_safe_terminal_coding_system_internal,
7388 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
7389 doc: /* Internal use only. */)
7390 (coding_system)
7391 Lisp_Object coding_system;
7392 {
7393 CHECK_SYMBOL (coding_system);
7394 setup_coding_system (Fcheck_coding_system (coding_system),
7395 &safe_terminal_coding);
7396 /* Character composition should be disabled. */
7397 safe_terminal_coding.composing = COMPOSITION_DISABLED;
7398 /* Error notification should be suppressed. */
7399 safe_terminal_coding.suppress_error = 1;
7400 safe_terminal_coding.src_multibyte = 1;
7401 safe_terminal_coding.dst_multibyte = 0;
7402 return Qnil;
7403 }
7404
7405 DEFUN ("terminal-coding-system", Fterminal_coding_system,
7406 Sterminal_coding_system, 0, 1, 0,
7407 doc: /* Return coding system specified for terminal output on the given terminal.
7408 TERMINAL may be a terminal id, a frame, or nil for the selected
7409 frame's terminal device. */)
7410 (terminal)
7411 Lisp_Object terminal;
7412 {
7413 return TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1))->symbol;
7414 }
7415
7416 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
7417 Sset_keyboard_coding_system_internal, 1, 2, 0,
7418 doc: /* Internal use only. */)
7419 (coding_system, terminal)
7420 Lisp_Object coding_system;
7421 Lisp_Object terminal;
7422 {
7423 struct terminal *t = get_terminal (terminal, 1);
7424 CHECK_SYMBOL (coding_system);
7425
7426 setup_coding_system (Fcheck_coding_system (coding_system),
7427 TERMINAL_KEYBOARD_CODING (t));
7428 /* Character composition should be disabled. */
7429 TERMINAL_KEYBOARD_CODING (t)->composing = COMPOSITION_DISABLED;
7430 return Qnil;
7431 }
7432
7433 DEFUN ("keyboard-coding-system", Fkeyboard_coding_system,
7434 Skeyboard_coding_system, 0, 1, 0,
7435 doc: /* Return coding system for decoding keyboard input on TERMINAL.
7436 TERMINAL may be a terminal id, a frame, or nil for the selected
7437 frame's terminal device. */)
7438 (terminal)
7439 Lisp_Object terminal;
7440 {
7441 return TERMINAL_KEYBOARD_CODING (get_terminal (terminal, 1))->symbol;
7442 }
7443
7444 \f
7445 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
7446 Sfind_operation_coding_system, 1, MANY, 0,
7447 doc: /* Choose a coding system for an operation based on the target name.
7448 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
7449 DECODING-SYSTEM is the coding system to use for decoding
7450 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
7451 for encoding (in case OPERATION does encoding).
7452
7453 The first argument OPERATION specifies an I/O primitive:
7454 For file I/O, `insert-file-contents' or `write-region'.
7455 For process I/O, `call-process', `call-process-region', or `start-process'.
7456 For network I/O, `open-network-stream'.
7457
7458 The remaining arguments should be the same arguments that were passed
7459 to the primitive. Depending on which primitive, one of those arguments
7460 is selected as the TARGET. For example, if OPERATION does file I/O,
7461 whichever argument specifies the file name is TARGET.
7462
7463 TARGET has a meaning which depends on OPERATION:
7464 For file I/O, TARGET is a file name (except for the special case below).
7465 For process I/O, TARGET is a process name.
7466 For network I/O, TARGET is a service name or a port number
7467
7468 This function looks up what specified for TARGET in,
7469 `file-coding-system-alist', `process-coding-system-alist',
7470 or `network-coding-system-alist' depending on OPERATION.
7471 They may specify a coding system, a cons of coding systems,
7472 or a function symbol to call.
7473 In the last case, we call the function with one argument,
7474 which is a list of all the arguments given to this function.
7475 If the function can't decide a coding system, it can return
7476 `undecided' so that the normal code-detection is performed.
7477
7478 If OPERATION is `insert-file-contents', the argument corresponding to
7479 TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
7480 file name to look up, and BUFFER is a buffer that contains the file's
7481 contents (not yet decoded). If `file-coding-system-alist' specifies a
7482 function to call for FILENAME, that function should examine the
7483 contents of BUFFER instead of reading the file.
7484
7485 usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
7486 (nargs, args)
7487 int nargs;
7488 Lisp_Object *args;
7489 {
7490 Lisp_Object operation, target_idx, target, val;
7491 register Lisp_Object chain;
7492
7493 if (nargs < 2)
7494 error ("Too few arguments");
7495 operation = args[0];
7496 if (!SYMBOLP (operation)
7497 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
7498 error ("Invalid first argument");
7499 if (nargs < 1 + XINT (target_idx))
7500 error ("Too few arguments for operation: %s",
7501 SDATA (SYMBOL_NAME (operation)));
7502 /* For write-region, if the 6th argument (i.e. VISIT, the 5th
7503 argument to write-region) is string, it must be treated as a
7504 target file name. */
7505 if (EQ (operation, Qwrite_region)
7506 && nargs > 5
7507 && STRINGP (args[5]))
7508 target_idx = make_number (4);
7509 target = args[XINT (target_idx) + 1];
7510 if (!(STRINGP (target)
7511 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
7512 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
7513 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
7514 error ("Invalid argument %d", XINT (target_idx) + 1);
7515 if (CONSP (target))
7516 target = XCAR (target);
7517
7518 chain = ((EQ (operation, Qinsert_file_contents)
7519 || EQ (operation, Qwrite_region))
7520 ? Vfile_coding_system_alist
7521 : (EQ (operation, Qopen_network_stream)
7522 ? Vnetwork_coding_system_alist
7523 : Vprocess_coding_system_alist));
7524 if (NILP (chain))
7525 return Qnil;
7526
7527 for (; CONSP (chain); chain = XCDR (chain))
7528 {
7529 Lisp_Object elt;
7530 elt = XCAR (chain);
7531
7532 if (CONSP (elt)
7533 && ((STRINGP (target)
7534 && STRINGP (XCAR (elt))
7535 && fast_string_match (XCAR (elt), target) >= 0)
7536 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
7537 {
7538 val = XCDR (elt);
7539 /* Here, if VAL is both a valid coding system and a valid
7540 function symbol, we return VAL as a coding system. */
7541 if (CONSP (val))
7542 return val;
7543 if (! SYMBOLP (val))
7544 return Qnil;
7545 if (! NILP (Fcoding_system_p (val)))
7546 return Fcons (val, val);
7547 if (! NILP (Ffboundp (val)))
7548 {
7549 /* We use call1 rather than safe_call1
7550 so as to get bug reports about functions called here
7551 which don't handle the current interface. */
7552 val = call1 (val, Flist (nargs, args));
7553 if (CONSP (val))
7554 return val;
7555 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
7556 return Fcons (val, val);
7557 }
7558 return Qnil;
7559 }
7560 }
7561 return Qnil;
7562 }
7563
7564 DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal,
7565 Supdate_coding_systems_internal, 0, 0, 0,
7566 doc: /* Update internal database for ISO2022 and CCL based coding systems.
7567 When values of any coding categories are changed, you must
7568 call this function. */)
7569 ()
7570 {
7571 int i;
7572
7573 for (i = CODING_CATEGORY_IDX_EMACS_MULE; i < CODING_CATEGORY_IDX_MAX; i++)
7574 {
7575 Lisp_Object val;
7576
7577 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[i]);
7578 if (!NILP (val))
7579 {
7580 if (! coding_system_table[i])
7581 coding_system_table[i] = ((struct coding_system *)
7582 xmalloc (sizeof (struct coding_system)));
7583 setup_coding_system (val, coding_system_table[i]);
7584 }
7585 else if (coding_system_table[i])
7586 {
7587 xfree (coding_system_table[i]);
7588 coding_system_table[i] = NULL;
7589 }
7590 }
7591
7592 return Qnil;
7593 }
7594
7595 DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal,
7596 Sset_coding_priority_internal, 0, 0, 0,
7597 doc: /* Update internal database for the current value of `coding-category-list'.
7598 This function is internal use only. */)
7599 ()
7600 {
7601 int i = 0, idx;
7602 Lisp_Object val;
7603
7604 val = Vcoding_category_list;
7605
7606 while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX)
7607 {
7608 if (! SYMBOLP (XCAR (val)))
7609 break;
7610 idx = XFASTINT (Fget (XCAR (val), Qcoding_category_index));
7611 if (idx >= CODING_CATEGORY_IDX_MAX)
7612 break;
7613 coding_priorities[i++] = (1 << idx);
7614 val = XCDR (val);
7615 }
7616 /* If coding-category-list is valid and contains all coding
7617 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
7618 the following code saves Emacs from crashing. */
7619 while (i < CODING_CATEGORY_IDX_MAX)
7620 coding_priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT;
7621
7622 return Qnil;
7623 }
7624
7625 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
7626 Sdefine_coding_system_internal, 1, 1, 0,
7627 doc: /* Register CODING-SYSTEM as a base coding system.
7628 This function is internal use only. */)
7629 (coding_system)
7630 Lisp_Object coding_system;
7631 {
7632 Lisp_Object safe_chars, slot;
7633
7634 if (NILP (Fcheck_coding_system (coding_system)))
7635 xsignal1 (Qcoding_system_error, coding_system);
7636
7637 safe_chars = coding_safe_chars (coding_system);
7638 if (! EQ (safe_chars, Qt) && ! CHAR_TABLE_P (safe_chars))
7639 error ("No valid safe-chars property for %s",
7640 SDATA (SYMBOL_NAME (coding_system)));
7641
7642 if (EQ (safe_chars, Qt))
7643 {
7644 if (NILP (Fmemq (coding_system, XCAR (Vcoding_system_safe_chars))))
7645 XSETCAR (Vcoding_system_safe_chars,
7646 Fcons (coding_system, XCAR (Vcoding_system_safe_chars)));
7647 }
7648 else
7649 {
7650 slot = Fassq (coding_system, XCDR (Vcoding_system_safe_chars));
7651 if (NILP (slot))
7652 XSETCDR (Vcoding_system_safe_chars,
7653 nconc2 (XCDR (Vcoding_system_safe_chars),
7654 Fcons (Fcons (coding_system, safe_chars), Qnil)));
7655 else
7656 XSETCDR (slot, safe_chars);
7657 }
7658 return Qnil;
7659 }
7660
7661 #endif /* emacs */
7662
7663 \f
7664 /*** 9. Post-amble ***/
7665
7666 void
7667 init_coding_once ()
7668 {
7669 int i;
7670
7671 /* Emacs' internal format specific initialize routine. */
7672 for (i = 0; i <= 0x20; i++)
7673 emacs_code_class[i] = EMACS_control_code;
7674 emacs_code_class[0x0A] = EMACS_linefeed_code;
7675 emacs_code_class[0x0D] = EMACS_carriage_return_code;
7676 for (i = 0x21 ; i < 0x7F; i++)
7677 emacs_code_class[i] = EMACS_ascii_code;
7678 emacs_code_class[0x7F] = EMACS_control_code;
7679 for (i = 0x80; i < 0xFF; i++)
7680 emacs_code_class[i] = EMACS_invalid_code;
7681 emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3;
7682 emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3;
7683 emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4;
7684 emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4;
7685
7686 /* ISO2022 specific initialize routine. */
7687 for (i = 0; i < 0x20; i++)
7688 iso_code_class[i] = ISO_control_0;
7689 for (i = 0x21; i < 0x7F; i++)
7690 iso_code_class[i] = ISO_graphic_plane_0;
7691 for (i = 0x80; i < 0xA0; i++)
7692 iso_code_class[i] = ISO_control_1;
7693 for (i = 0xA1; i < 0xFF; i++)
7694 iso_code_class[i] = ISO_graphic_plane_1;
7695 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
7696 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
7697 iso_code_class[ISO_CODE_CR] = ISO_carriage_return;
7698 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
7699 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
7700 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
7701 iso_code_class[ISO_CODE_ESC] = ISO_escape;
7702 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
7703 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
7704 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
7705
7706 setup_coding_system (Qnil, &safe_terminal_coding);
7707 setup_coding_system (Qnil, &default_buffer_file_coding);
7708
7709 bzero (coding_system_table, sizeof coding_system_table);
7710
7711 bzero (ascii_skip_code, sizeof ascii_skip_code);
7712 for (i = 0; i < 128; i++)
7713 ascii_skip_code[i] = 1;
7714
7715 #if defined (MSDOS) || defined (WINDOWSNT)
7716 system_eol_type = CODING_EOL_CRLF;
7717 #else
7718 system_eol_type = CODING_EOL_LF;
7719 #endif
7720
7721 inhibit_pre_post_conversion = 0;
7722 }
7723
7724 #ifdef emacs
7725
7726 void
7727 syms_of_coding ()
7728 {
7729 staticpro (&Vcode_conversion_workbuf_name);
7730 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*");
7731
7732 Qtarget_idx = intern ("target-idx");
7733 staticpro (&Qtarget_idx);
7734
7735 Qcoding_system_history = intern ("coding-system-history");
7736 staticpro (&Qcoding_system_history);
7737 Fset (Qcoding_system_history, Qnil);
7738
7739 /* Target FILENAME is the first argument. */
7740 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
7741 /* Target FILENAME is the third argument. */
7742 Fput (Qwrite_region, Qtarget_idx, make_number (2));
7743
7744 Qcall_process = intern ("call-process");
7745 staticpro (&Qcall_process);
7746 /* Target PROGRAM is the first argument. */
7747 Fput (Qcall_process, Qtarget_idx, make_number (0));
7748
7749 Qcall_process_region = intern ("call-process-region");
7750 staticpro (&Qcall_process_region);
7751 /* Target PROGRAM is the third argument. */
7752 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
7753
7754 Qstart_process = intern ("start-process");
7755 staticpro (&Qstart_process);
7756 /* Target PROGRAM is the third argument. */
7757 Fput (Qstart_process, Qtarget_idx, make_number (2));
7758
7759 Qopen_network_stream = intern ("open-network-stream");
7760 staticpro (&Qopen_network_stream);
7761 /* Target SERVICE is the fourth argument. */
7762 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
7763
7764 Qcoding_system = intern ("coding-system");
7765 staticpro (&Qcoding_system);
7766
7767 Qeol_type = intern ("eol-type");
7768 staticpro (&Qeol_type);
7769
7770 Qbuffer_file_coding_system = intern ("buffer-file-coding-system");
7771 staticpro (&Qbuffer_file_coding_system);
7772
7773 Qpost_read_conversion = intern ("post-read-conversion");
7774 staticpro (&Qpost_read_conversion);
7775
7776 Qpre_write_conversion = intern ("pre-write-conversion");
7777 staticpro (&Qpre_write_conversion);
7778
7779 Qno_conversion = intern ("no-conversion");
7780 staticpro (&Qno_conversion);
7781
7782 Qundecided = intern ("undecided");
7783 staticpro (&Qundecided);
7784
7785 Qcoding_system_p = intern ("coding-system-p");
7786 staticpro (&Qcoding_system_p);
7787
7788 Qcoding_system_error = intern ("coding-system-error");
7789 staticpro (&Qcoding_system_error);
7790
7791 Fput (Qcoding_system_error, Qerror_conditions,
7792 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
7793 Fput (Qcoding_system_error, Qerror_message,
7794 build_string ("Invalid coding system"));
7795
7796 Qcoding_category = intern ("coding-category");
7797 staticpro (&Qcoding_category);
7798 Qcoding_category_index = intern ("coding-category-index");
7799 staticpro (&Qcoding_category_index);
7800
7801 Vcoding_category_table
7802 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil);
7803 staticpro (&Vcoding_category_table);
7804 {
7805 int i;
7806 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
7807 {
7808 XVECTOR (Vcoding_category_table)->contents[i]
7809 = intern (coding_category_name[i]);
7810 Fput (XVECTOR (Vcoding_category_table)->contents[i],
7811 Qcoding_category_index, make_number (i));
7812 }
7813 }
7814
7815 Vcoding_system_safe_chars = Fcons (Qnil, Qnil);
7816 staticpro (&Vcoding_system_safe_chars);
7817
7818 Qtranslation_table = intern ("translation-table");
7819 staticpro (&Qtranslation_table);
7820 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
7821
7822 Qtranslation_table_id = intern ("translation-table-id");
7823 staticpro (&Qtranslation_table_id);
7824
7825 Qtranslation_table_for_decode = intern ("translation-table-for-decode");
7826 staticpro (&Qtranslation_table_for_decode);
7827
7828 Qtranslation_table_for_encode = intern ("translation-table-for-encode");
7829 staticpro (&Qtranslation_table_for_encode);
7830
7831 Qsafe_chars = intern ("safe-chars");
7832 staticpro (&Qsafe_chars);
7833
7834 Qchar_coding_system = intern ("char-coding-system");
7835 staticpro (&Qchar_coding_system);
7836
7837 /* Intern this now in case it isn't already done.
7838 Setting this variable twice is harmless.
7839 But don't staticpro it here--that is done in alloc.c. */
7840 Qchar_table_extra_slots = intern ("char-table-extra-slots");
7841 Fput (Qsafe_chars, Qchar_table_extra_slots, make_number (0));
7842 Fput (Qchar_coding_system, Qchar_table_extra_slots, make_number (0));
7843
7844 Qvalid_codes = intern ("valid-codes");
7845 staticpro (&Qvalid_codes);
7846
7847 Qascii_incompatible = intern ("ascii-incompatible");
7848 staticpro (&Qascii_incompatible);
7849
7850 Qemacs_mule = intern ("emacs-mule");
7851 staticpro (&Qemacs_mule);
7852
7853 Qraw_text = intern ("raw-text");
7854 staticpro (&Qraw_text);
7855
7856 Qutf_8 = intern ("utf-8");
7857 staticpro (&Qutf_8);
7858
7859 Qcoding_system_define_form = intern ("coding-system-define-form");
7860 staticpro (&Qcoding_system_define_form);
7861
7862 defsubr (&Scoding_system_p);
7863 defsubr (&Sread_coding_system);
7864 defsubr (&Sread_non_nil_coding_system);
7865 defsubr (&Scheck_coding_system);
7866 defsubr (&Sdetect_coding_region);
7867 defsubr (&Sdetect_coding_string);
7868 defsubr (&Sfind_coding_systems_region_internal);
7869 defsubr (&Sunencodable_char_position);
7870 defsubr (&Sdecode_coding_region);
7871 defsubr (&Sencode_coding_region);
7872 defsubr (&Sdecode_coding_string);
7873 defsubr (&Sencode_coding_string);
7874 defsubr (&Sdecode_sjis_char);
7875 defsubr (&Sencode_sjis_char);
7876 defsubr (&Sdecode_big5_char);
7877 defsubr (&Sencode_big5_char);
7878 defsubr (&Sset_terminal_coding_system_internal);
7879 defsubr (&Sset_safe_terminal_coding_system_internal);
7880 defsubr (&Sterminal_coding_system);
7881 defsubr (&Sset_keyboard_coding_system_internal);
7882 defsubr (&Skeyboard_coding_system);
7883 defsubr (&Sfind_operation_coding_system);
7884 defsubr (&Supdate_coding_systems_internal);
7885 defsubr (&Sset_coding_priority_internal);
7886 defsubr (&Sdefine_coding_system_internal);
7887
7888 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
7889 doc: /* List of coding systems.
7890
7891 Do not alter the value of this variable manually. This variable should be
7892 updated by the functions `make-coding-system' and
7893 `define-coding-system-alias'. */);
7894 Vcoding_system_list = Qnil;
7895
7896 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
7897 doc: /* Alist of coding system names.
7898 Each element is one element list of coding system name.
7899 This variable is given to `completing-read' as TABLE argument.
7900
7901 Do not alter the value of this variable manually. This variable should be
7902 updated by the functions `make-coding-system' and
7903 `define-coding-system-alias'. */);
7904 Vcoding_system_alist = Qnil;
7905
7906 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
7907 doc: /* List of coding-categories (symbols) ordered by priority.
7908
7909 On detecting a coding system, Emacs tries code detection algorithms
7910 associated with each coding-category one by one in this order. When
7911 one algorithm agrees with a byte sequence of source text, the coding
7912 system bound to the corresponding coding-category is selected.
7913
7914 Don't modify this variable directly, but use `set-coding-priority'. */);
7915 {
7916 int i;
7917
7918 Vcoding_category_list = Qnil;
7919 for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--)
7920 Vcoding_category_list
7921 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
7922 Vcoding_category_list);
7923 }
7924
7925 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
7926 doc: /* Specify the coding system for read operations.
7927 It is useful to bind this variable with `let', but do not set it globally.
7928 If the value is a coding system, it is used for decoding on read operation.
7929 If not, an appropriate element is used from one of the coding system alists:
7930 There are three such tables, `file-coding-system-alist',
7931 `process-coding-system-alist', and `network-coding-system-alist'. */);
7932 Vcoding_system_for_read = Qnil;
7933
7934 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
7935 doc: /* Specify the coding system for write operations.
7936 Programs bind this variable with `let', but you should not set it globally.
7937 If the value is a coding system, it is used for encoding of output,
7938 when writing it to a file and when sending it to a file or subprocess.
7939
7940 If this does not specify a coding system, an appropriate element
7941 is used from one of the coding system alists:
7942 There are three such tables, `file-coding-system-alist',
7943 `process-coding-system-alist', and `network-coding-system-alist'.
7944 For output to files, if the above procedure does not specify a coding system,
7945 the value of `buffer-file-coding-system' is used. */);
7946 Vcoding_system_for_write = Qnil;
7947
7948 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
7949 doc: /* Coding system used in the latest file or process I/O.
7950 Also set by `encode-coding-region', `decode-coding-region',
7951 `encode-coding-string' and `decode-coding-string'. */);
7952 Vlast_coding_system_used = Qnil;
7953
7954 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
7955 doc: /* *Non-nil means always inhibit code conversion of end-of-line format.
7956 See info node `Coding Systems' and info node `Text and Binary' concerning
7957 such conversion. */);
7958 inhibit_eol_conversion = 0;
7959
7960 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
7961 doc: /* Non-nil means process buffer inherits coding system of process output.
7962 Bind it to t if the process output is to be treated as if it were a file
7963 read from some filesystem. */);
7964 inherit_process_coding_system = 0;
7965
7966 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
7967 doc: /* Alist to decide a coding system to use for a file I/O operation.
7968 The format is ((PATTERN . VAL) ...),
7969 where PATTERN is a regular expression matching a file name,
7970 VAL is a coding system, a cons of coding systems, or a function symbol.
7971 If VAL is a coding system, it is used for both decoding and encoding
7972 the file contents.
7973 If VAL is a cons of coding systems, the car part is used for decoding,
7974 and the cdr part is used for encoding.
7975 If VAL is a function symbol, the function must return a coding system
7976 or a cons of coding systems which are used as above. The function is
7977 called with an argument that is a list of the arguments with which
7978 `find-operation-coding-system' was called. If the function can't decide
7979 a coding system, it can return `undecided' so that the normal
7980 code-detection is performed.
7981
7982 See also the function `find-operation-coding-system'
7983 and the variable `auto-coding-alist'. */);
7984 Vfile_coding_system_alist = Qnil;
7985
7986 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
7987 doc: /* Alist to decide a coding system to use for a process I/O operation.
7988 The format is ((PATTERN . VAL) ...),
7989 where PATTERN is a regular expression matching a program name,
7990 VAL is a coding system, a cons of coding systems, or a function symbol.
7991 If VAL is a coding system, it is used for both decoding what received
7992 from the program and encoding what sent to the program.
7993 If VAL is a cons of coding systems, the car part is used for decoding,
7994 and the cdr part is used for encoding.
7995 If VAL is a function symbol, the function must return a coding system
7996 or a cons of coding systems which are used as above.
7997
7998 See also the function `find-operation-coding-system'. */);
7999 Vprocess_coding_system_alist = Qnil;
8000
8001 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
8002 doc: /* Alist to decide a coding system to use for a network I/O operation.
8003 The format is ((PATTERN . VAL) ...),
8004 where PATTERN is a regular expression matching a network service name
8005 or is a port number to connect to,
8006 VAL is a coding system, a cons of coding systems, or a function symbol.
8007 If VAL is a coding system, it is used for both decoding what received
8008 from the network stream and encoding what sent to the network stream.
8009 If VAL is a cons of coding systems, the car part is used for decoding,
8010 and the cdr part is used for encoding.
8011 If VAL is a function symbol, the function must return a coding system
8012 or a cons of coding systems which are used as above.
8013
8014 See also the function `find-operation-coding-system'. */);
8015 Vnetwork_coding_system_alist = Qnil;
8016
8017 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system,
8018 doc: /* Coding system to use with system messages.
8019 Also used for decoding keyboard input on X Window system. */);
8020 Vlocale_coding_system = Qnil;
8021
8022 /* The eol mnemonics are reset in startup.el system-dependently. */
8023 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
8024 doc: /* *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
8025 eol_mnemonic_unix = build_string (":");
8026
8027 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
8028 doc: /* *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
8029 eol_mnemonic_dos = build_string ("\\");
8030
8031 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
8032 doc: /* *String displayed in mode line for MAC-like (CR) end-of-line format. */);
8033 eol_mnemonic_mac = build_string ("/");
8034
8035 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
8036 doc: /* *String displayed in mode line when end-of-line format is not yet determined. */);
8037 eol_mnemonic_undecided = build_string (":");
8038
8039 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
8040 doc: /* *Non-nil enables character translation while encoding and decoding. */);
8041 Venable_character_translation = Qt;
8042
8043 DEFVAR_LISP ("standard-translation-table-for-decode",
8044 &Vstandard_translation_table_for_decode,
8045 doc: /* Table for translating characters while decoding. */);
8046 Vstandard_translation_table_for_decode = Qnil;
8047
8048 DEFVAR_LISP ("standard-translation-table-for-encode",
8049 &Vstandard_translation_table_for_encode,
8050 doc: /* Table for translating characters while encoding. */);
8051 Vstandard_translation_table_for_encode = Qnil;
8052
8053 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist,
8054 doc: /* Alist of charsets vs revision numbers.
8055 While encoding, if a charset (car part of an element) is found,
8056 designate it with the escape sequence identifying revision (cdr part of the element). */);
8057 Vcharset_revision_alist = Qnil;
8058
8059 DEFVAR_LISP ("default-process-coding-system",
8060 &Vdefault_process_coding_system,
8061 doc: /* Cons of coding systems used for process I/O by default.
8062 The car part is used for decoding a process output,
8063 the cdr part is used for encoding a text to be sent to a process. */);
8064 Vdefault_process_coding_system = Qnil;
8065
8066 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
8067 doc: /* Table of extra Latin codes in the range 128..159 (inclusive).
8068 This is a vector of length 256.
8069 If Nth element is non-nil, the existence of code N in a file
8070 \(or output of subprocess) doesn't prevent it to be detected as
8071 a coding system of ISO 2022 variant which has a flag
8072 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
8073 or reading output of a subprocess.
8074 Only 128th through 159th elements has a meaning. */);
8075 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
8076
8077 DEFVAR_LISP ("select-safe-coding-system-function",
8078 &Vselect_safe_coding_system_function,
8079 doc: /* Function to call to select safe coding system for encoding a text.
8080
8081 If set, this function is called to force a user to select a proper
8082 coding system which can encode the text in the case that a default
8083 coding system used in each operation can't encode the text.
8084
8085 The default value is `select-safe-coding-system' (which see). */);
8086 Vselect_safe_coding_system_function = Qnil;
8087
8088 DEFVAR_BOOL ("coding-system-require-warning",
8089 &coding_system_require_warning,
8090 doc: /* Internal use only.
8091 If non-nil, on writing a file, `select-safe-coding-system-function' is
8092 called even if `coding-system-for-write' is non-nil. The command
8093 `universal-coding-system-argument' binds this variable to t temporarily. */);
8094 coding_system_require_warning = 0;
8095
8096
8097 DEFVAR_BOOL ("inhibit-iso-escape-detection",
8098 &inhibit_iso_escape_detection,
8099 doc: /* If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
8100
8101 By default, on reading a file, Emacs tries to detect how the text is
8102 encoded. This code detection is sensitive to escape sequences. If
8103 the sequence is valid as ISO2022, the code is determined as one of
8104 the ISO2022 encodings, and the file is decoded by the corresponding
8105 coding system (e.g. `iso-2022-7bit').
8106
8107 However, there may be a case that you want to read escape sequences in
8108 a file as is. In such a case, you can set this variable to non-nil.
8109 Then, as the code detection ignores any escape sequences, no file is
8110 detected as encoded in some ISO2022 encoding. The result is that all
8111 escape sequences become visible in a buffer.
8112
8113 The default value is nil, and it is strongly recommended not to change
8114 it. That is because many Emacs Lisp source files that contain
8115 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
8116 in Emacs's distribution, and they won't be decoded correctly on
8117 reading if you suppress escape sequence detection.
8118
8119 The other way to read escape sequences in a file without decoding is
8120 to explicitly specify some coding system that doesn't use ISO2022's
8121 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
8122 inhibit_iso_escape_detection = 0;
8123
8124 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input,
8125 doc: /* Char table for translating self-inserting characters.
8126 This is applied to the result of input methods, not their input. See also
8127 `keyboard-translate-table'. */);
8128 Vtranslation_table_for_input = Qnil;
8129 }
8130
8131 char *
8132 emacs_strerror (error_number)
8133 int error_number;
8134 {
8135 char *str;
8136
8137 synchronize_system_messages_locale ();
8138 str = strerror (error_number);
8139
8140 if (! NILP (Vlocale_coding_system))
8141 {
8142 Lisp_Object dec = code_convert_string_norecord (build_string (str),
8143 Vlocale_coding_system,
8144 0);
8145 str = (char *) SDATA (dec);
8146 }
8147
8148 return str;
8149 }
8150
8151 #endif /* emacs */
8152
8153 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
8154 (do not change this comment) */