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