Mercurial > hg > xemacs-beta
annotate src/file-coding.c @ 4839:5f1c6ca4d05e
fix bad change to default_query_method, rewrite conditional for more correctness
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Tue, 12 Jan 2010 23:16:33 -0600 |
parents | a9833e8a32ec |
children | a98ca4640147 |
rev | line source |
---|---|
771 | 1 /* Text encoding conversion functions; coding-system object. |
2 #### rename me to coding-system.c or coding.c | |
428 | 3 Copyright (C) 1991, 1995 Free Software Foundation, Inc. |
4 Copyright (C) 1995 Sun Microsystems, Inc. | |
3025 | 5 Copyright (C) 2000, 2001, 2002, 2003, 2005 Ben Wing. |
428 | 6 |
7 This file is part of XEmacs. | |
8 | |
9 XEmacs is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
11 Free Software Foundation; either version 2, or (at your option) any | |
12 later version. | |
13 | |
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
20 along with XEmacs; see the file COPYING. If not, write to | |
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 Boston, MA 02111-1307, USA. */ | |
23 | |
771 | 24 /* Synched up with: Not in FSF. */ |
25 | |
26 /* Authorship: | |
27 | |
28 Current primary author: Ben Wing <ben@xemacs.org> | |
29 | |
30 Rewritten by Ben Wing <ben@xemacs.org>, based originally on coding.c | |
31 from Mule 2.? but probably does not share one line of code with that | |
32 original source. Rewriting work started around Dec. 1994. or Jan. 1995. | |
33 Proceeded in earnest till Nov. 1995. | |
34 | |
35 Around Feb. 17, 1998, Andy Piper renamed what was then mule-coding.c to | |
36 file-coding.c, with the intention of using it to do end-of-line conversion | |
37 on non-MULE machines (specifically, on Windows machines). He separated | |
38 out the MULE stuff from non-MULE using ifdef's, and searched throughout | |
39 the rest of the source tree looking for coding-system-related code that | |
40 was ifdef MULE but should be ifdef HAVE_CODING_SYSTEMS. | |
41 | |
42 Sept. 4 - 8, 1998, Tomohiko Morioka added the UCS_4 and UTF_8 coding system | |
43 types, providing a primitive means of decoding and encoding externally- | |
44 formatted Unicode/UCS_4 and Unicode/UTF_8 data. | |
45 | |
46 January 25, 2000, Martin Buchholz redid and fleshed out the coding | |
47 system alias handling that was first added in prototype form by | |
48 Hrjove Niksic, April 15, 1999. | |
49 | |
50 April to May 2000, Ben Wing: More major reorganization. Adding features | |
51 needed for MS Windows (multibyte, unicode, unicode-to-multibyte), the | |
52 "chain" coding system for chaining two together, and doing a lot of | |
53 reorganization in preparation for properly abstracting out the different | |
54 coding system types. | |
55 | |
56 June 2001, Ben Wing: Added Unicode support. Eliminated previous | |
57 junky Unicode translation support. | |
58 | |
59 August 2001, Ben Wing: Moved Unicode support to unicode.c. Finished | |
60 abstracting everything except detection, which is hard to abstract (see | |
61 just below). | |
62 | |
63 September 2001, Ben Wing: Moved Mule code to mule-coding.c, Windows code | |
64 to intl-win32.c. Lots more rewriting; very little code is untouched | |
65 from before April 2000. Abstracted the detection code, added multiple | |
66 levels of likelihood to increase the reliability of the algorithm. | |
67 | |
68 October 2001, Ben Wing: HAVE_CODING_SYSTEMS is always now defined. | |
69 Removed the conditionals. | |
70 */ | |
71 | |
428 | 72 #include <config.h> |
73 #include "lisp.h" | |
74 | |
75 #include "buffer.h" | |
76 #include "elhash.h" | |
77 #include "insdel.h" | |
78 #include "lstream.h" | |
440 | 79 #include "opaque.h" |
771 | 80 #include "file-coding.h" |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
81 #include "extents.h" |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
82 #include "rangetab.h" |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
83 #include "chartab.h" |
771 | 84 |
85 #ifdef HAVE_ZLIB | |
86 #include "zlib.h" | |
428 | 87 #endif |
88 | |
89 Lisp_Object Vkeyboard_coding_system; | |
90 Lisp_Object Vterminal_coding_system; | |
91 Lisp_Object Vcoding_system_for_read; | |
92 Lisp_Object Vcoding_system_for_write; | |
93 Lisp_Object Vfile_name_coding_system; | |
94 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
95 Lisp_Object Qaliases, Qcharset_skip_chars_string; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
96 |
771 | 97 #ifdef DEBUG_XEMACS |
98 Lisp_Object Vdebug_coding_detection; | |
440 | 99 #endif |
771 | 100 |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
101 #ifdef MULE |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
102 extern Lisp_Object Vcharset_ascii, Vcharset_control_1, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
103 Vcharset_latin_iso8859_1; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
104 #endif |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
105 |
771 | 106 typedef struct coding_system_type_entry |
107 { | |
108 struct coding_system_methods *meths; | |
109 } coding_system_type_entry; | |
110 | |
111 typedef struct | |
112 { | |
113 Dynarr_declare (coding_system_type_entry); | |
114 } coding_system_type_entry_dynarr; | |
115 | |
116 static coding_system_type_entry_dynarr *the_coding_system_type_entry_dynarr; | |
117 | |
1204 | 118 static const struct memory_description cste_description_1[] = { |
2551 | 119 { XD_BLOCK_PTR, offsetof (coding_system_type_entry, meths), 1, |
120 { &coding_system_methods_description } }, | |
771 | 121 { XD_END } |
122 }; | |
123 | |
1204 | 124 static const struct sized_memory_description cste_description = { |
771 | 125 sizeof (coding_system_type_entry), |
126 cste_description_1 | |
127 }; | |
128 | |
1204 | 129 static const struct memory_description csted_description_1[] = { |
771 | 130 XD_DYNARR_DESC (coding_system_type_entry_dynarr, &cste_description), |
428 | 131 { XD_END } |
132 }; | |
133 | |
1204 | 134 static const struct sized_memory_description csted_description = { |
771 | 135 sizeof (coding_system_type_entry_dynarr), |
136 csted_description_1 | |
137 }; | |
138 | |
139 static Lisp_Object Vcoding_system_type_list; | |
140 | |
141 /* Coding system currently associated with each coding category. */ | |
142 Lisp_Object coding_category_system[MAX_DETECTOR_CATEGORIES]; | |
143 | |
144 /* Table of all coding categories in decreasing order of priority. | |
145 This describes a permutation of the possible coding categories. */ | |
146 int coding_category_by_priority[MAX_DETECTOR_CATEGORIES]; | |
147 | |
148 /* Value used with to give a unique name to nameless coding systems */ | |
149 int coding_system_tick; | |
150 | |
151 int coding_detector_count; | |
152 int coding_detector_category_count; | |
153 | |
154 detector_dynarr *all_coding_detectors; | |
155 | |
1204 | 156 static const struct memory_description struct_detector_category_description_1[] |
771 | 157 = |
158 { | |
159 { XD_LISP_OBJECT, offsetof (struct detector_category, sym) }, | |
160 { XD_END } | |
161 }; | |
162 | |
1204 | 163 static const struct sized_memory_description struct_detector_category_description = |
771 | 164 { |
165 sizeof (struct detector_category), | |
166 struct_detector_category_description_1 | |
428 | 167 }; |
168 | |
1204 | 169 static const struct memory_description detector_category_dynarr_description_1[] = |
771 | 170 { |
171 XD_DYNARR_DESC (detector_category_dynarr, | |
172 &struct_detector_category_description), | |
173 { XD_END } | |
174 }; | |
175 | |
1204 | 176 static const struct sized_memory_description detector_category_dynarr_description = { |
771 | 177 sizeof (detector_category_dynarr), |
178 detector_category_dynarr_description_1 | |
179 }; | |
180 | |
1204 | 181 static const struct memory_description struct_detector_description_1[] |
771 | 182 = |
183 { | |
2367 | 184 { XD_BLOCK_PTR, offsetof (struct detector, cats), 1, |
2551 | 185 { &detector_category_dynarr_description } }, |
771 | 186 { XD_END } |
187 }; | |
188 | |
1204 | 189 static const struct sized_memory_description struct_detector_description = |
771 | 190 { |
191 sizeof (struct detector), | |
192 struct_detector_description_1 | |
193 }; | |
194 | |
1204 | 195 static const struct memory_description detector_dynarr_description_1[] = |
771 | 196 { |
197 XD_DYNARR_DESC (detector_dynarr, &struct_detector_description), | |
198 { XD_END } | |
199 }; | |
200 | |
1204 | 201 static const struct sized_memory_description detector_dynarr_description = { |
771 | 202 sizeof (detector_dynarr), |
203 detector_dynarr_description_1 | |
204 }; | |
428 | 205 |
206 Lisp_Object Qcoding_systemp; | |
207 | |
771 | 208 Lisp_Object Qraw_text; |
428 | 209 |
210 Lisp_Object Qmnemonic, Qeol_type; | |
211 Lisp_Object Qcr, Qcrlf, Qlf; | |
212 Lisp_Object Qeol_cr, Qeol_crlf, Qeol_lf; | |
213 Lisp_Object Qpost_read_conversion; | |
214 Lisp_Object Qpre_write_conversion; | |
215 | |
771 | 216 Lisp_Object Qtranslation_table_for_decode; |
217 Lisp_Object Qtranslation_table_for_encode; | |
218 Lisp_Object Qsafe_chars; | |
219 Lisp_Object Qsafe_charsets; | |
220 Lisp_Object Qmime_charset; | |
221 Lisp_Object Qvalid_codes; | |
222 | |
223 Lisp_Object Qno_conversion; | |
224 Lisp_Object Qconvert_eol; | |
440 | 225 Lisp_Object Qescape_quoted; |
771 | 226 Lisp_Object Qencode, Qdecode; |
227 | |
228 Lisp_Object Qconvert_eol_lf, Qconvert_eol_cr, Qconvert_eol_crlf; | |
229 Lisp_Object Qconvert_eol_autodetect; | |
230 | |
231 Lisp_Object Qnear_certainty, Qquite_probable, Qsomewhat_likely; | |
1494 | 232 Lisp_Object Qslightly_likely; |
771 | 233 Lisp_Object Qas_likely_as_unlikely, Qsomewhat_unlikely, Qquite_improbable; |
234 Lisp_Object Qnearly_impossible; | |
235 | |
236 Lisp_Object Qdo_eol, Qdo_coding; | |
237 | |
238 Lisp_Object Qcanonicalize_after_coding; | |
239 | |
1347 | 240 Lisp_Object QScoding_system_cookie; |
241 | |
4303 | 242 Lisp_Object Qposix_charset_to_coding_system_hash; |
243 | |
771 | 244 /* This is used to convert autodetected coding systems into existing |
245 systems. For example, the chain undecided->convert-eol-autodetect may | |
246 have its separate parts detected as mswindows-multibyte and | |
247 convert-eol-crlf, and the result needs to be mapped to | |
248 mswindows-multibyte-dos. */ | |
249 /* #### It's not clear we need this whole chain-canonicalize mechanism | |
250 any more. */ | |
251 static Lisp_Object Vchain_canonicalize_hash_table; | |
252 | |
253 #ifdef HAVE_ZLIB | |
254 Lisp_Object Qgzip; | |
428 | 255 #endif |
771 | 256 |
2297 | 257 /* Maps symbols (coding system names) to either coding system objects or |
258 (for aliases) other names. */ | |
771 | 259 static Lisp_Object Vcoding_system_hash_table; |
428 | 260 |
261 int enable_multibyte_characters; | |
262 | |
263 EXFUN (Fcopy_coding_system, 2); | |
264 | |
265 | |
266 /************************************************************************/ | |
771 | 267 /* Coding system object methods */ |
428 | 268 /************************************************************************/ |
269 | |
270 static Lisp_Object | |
271 mark_coding_system (Lisp_Object obj) | |
272 { | |
273 Lisp_Coding_System *codesys = XCODING_SYSTEM (obj); | |
274 | |
1204 | 275 #define MARKED_SLOT(x) mark_object (codesys->x); |
276 #include "coding-system-slots.h" | |
771 | 277 |
278 MAYBE_CODESYSMETH (codesys, mark, (obj)); | |
428 | 279 |
1204 | 280 return Qnil; |
428 | 281 } |
282 | |
283 static void | |
771 | 284 print_coding_system_properties (Lisp_Object obj, Lisp_Object printcharfun) |
285 { | |
286 Lisp_Coding_System *c = XCODING_SYSTEM (obj); | |
287 print_internal (c->methods->type, printcharfun, 1); | |
288 MAYBE_CODESYSMETH (c, print, (obj, printcharfun, 1)); | |
289 if (CODING_SYSTEM_EOL_TYPE (c) != EOL_AUTODETECT) | |
290 write_fmt_string_lisp (printcharfun, " eol-type=%s", | |
291 1, Fcoding_system_property (obj, Qeol_type)); | |
292 } | |
293 | |
294 static void | |
428 | 295 print_coding_system (Lisp_Object obj, Lisp_Object printcharfun, |
2286 | 296 int UNUSED (escapeflag)) |
428 | 297 { |
298 Lisp_Coding_System *c = XCODING_SYSTEM (obj); | |
299 if (print_readably) | |
771 | 300 printing_unreadable_object |
301 ("printing unreadable object #<coding-system 0x%x>", c->header.uid); | |
302 | |
303 write_fmt_string_lisp (printcharfun, "#<coding-system %s ", 1, c->name); | |
304 print_coding_system_properties (obj, printcharfun); | |
826 | 305 write_c_string (printcharfun, ">"); |
428 | 306 } |
307 | |
771 | 308 /* Print an abbreviated version of a coding system (but still containing |
309 all the information), for use within a coding system print method. */ | |
310 | |
311 static void | |
312 print_coding_system_in_print_method (Lisp_Object cs, Lisp_Object printcharfun, | |
2286 | 313 int UNUSED (escapeflag)) |
771 | 314 { |
800 | 315 write_fmt_string_lisp (printcharfun, "%s[", 1, XCODING_SYSTEM_NAME (cs)); |
771 | 316 print_coding_system_properties (cs, printcharfun); |
826 | 317 write_c_string (printcharfun, "]"); |
771 | 318 } |
319 | |
3263 | 320 #ifndef NEW_GC |
428 | 321 static void |
322 finalize_coding_system (void *header, int for_disksave) | |
323 { | |
771 | 324 Lisp_Object cs = wrap_coding_system ((Lisp_Coding_System *) header); |
428 | 325 /* Since coding systems never go away, this function is not |
326 necessary. But it would be necessary if we changed things | |
327 so that coding systems could go away. */ | |
328 if (!for_disksave) /* see comment in lstream.c */ | |
771 | 329 MAYBE_XCODESYSMETH (cs, finalize, (cs)); |
330 } | |
3263 | 331 #endif /* not NEW_GC */ |
771 | 332 |
333 static Bytecount | |
334 sizeof_coding_system (const void *header) | |
335 { | |
336 const Lisp_Coding_System *p = (const Lisp_Coding_System *) header; | |
337 return offsetof (Lisp_Coding_System, data) + p->methods->extra_data_size; | |
428 | 338 } |
339 | |
1204 | 340 static const struct memory_description coding_system_methods_description_1[] |
771 | 341 = { |
342 { XD_LISP_OBJECT, | |
343 offsetof (struct coding_system_methods, type) }, | |
344 { XD_LISP_OBJECT, | |
345 offsetof (struct coding_system_methods, predicate_symbol) }, | |
346 { XD_END } | |
347 }; | |
348 | |
1204 | 349 const struct sized_memory_description coding_system_methods_description = { |
771 | 350 sizeof (struct coding_system_methods), |
351 coding_system_methods_description_1 | |
352 }; | |
353 | |
1204 | 354 static const struct sized_memory_description coding_system_extra_description_map[] = |
355 { | |
356 { offsetof (Lisp_Coding_System, methods) }, | |
357 { offsetof (struct coding_system_methods, extra_description) }, | |
358 { -1 }, | |
771 | 359 }; |
360 | |
1204 | 361 static const struct memory_description coding_system_description[] = |
428 | 362 { |
2367 | 363 { XD_BLOCK_PTR, offsetof (Lisp_Coding_System, methods), 1, |
2551 | 364 { &coding_system_methods_description } }, |
1204 | 365 #define MARKED_SLOT(x) { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, x) }, |
366 #define MARKED_SLOT_ARRAY(slot, size) \ | |
367 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Coding_System, slot), size }, | |
368 #include "coding-system-slots.h" | |
2367 | 369 { XD_BLOCK_ARRAY, offsetof (Lisp_Coding_System, data), 1, |
2551 | 370 { coding_system_extra_description_map } }, |
1204 | 371 { XD_END } |
771 | 372 }; |
373 | |
1204 | 374 static const struct memory_description coding_system_empty_extra_description_1[] = |
375 { | |
376 { XD_END } | |
377 }; | |
378 | |
379 const struct sized_memory_description coding_system_empty_extra_description = { | |
380 0, coding_system_empty_extra_description_1 | |
381 }; | |
382 | |
3263 | 383 #ifdef NEW_GC |
384 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("coding-system", coding_system, | |
385 1, /*dumpable-flag*/ | |
386 mark_coding_system, | |
387 print_coding_system, | |
388 0, 0, 0, coding_system_description, | |
389 sizeof_coding_system, | |
390 Lisp_Coding_System); | |
391 #else /* not NEW_GC */ | |
934 | 392 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("coding-system", coding_system, |
393 1, /*dumpable-flag*/ | |
394 mark_coding_system, | |
395 print_coding_system, | |
396 finalize_coding_system, | |
397 0, 0, coding_system_description, | |
398 sizeof_coding_system, | |
399 Lisp_Coding_System); | |
3263 | 400 #endif /* not NEW_GC */ |
771 | 401 |
402 /************************************************************************/ | |
403 /* Creating coding systems */ | |
404 /************************************************************************/ | |
405 | |
406 static struct coding_system_methods * | |
407 decode_coding_system_type (Lisp_Object type, Error_Behavior errb) | |
428 | 408 { |
771 | 409 int i; |
410 | |
411 for (i = 0; i < Dynarr_length (the_coding_system_type_entry_dynarr); i++) | |
428 | 412 { |
771 | 413 if (EQ (type, |
414 Dynarr_at (the_coding_system_type_entry_dynarr, i).meths->type)) | |
415 return Dynarr_at (the_coding_system_type_entry_dynarr, i).meths; | |
428 | 416 } |
771 | 417 |
418 maybe_invalid_constant ("Invalid coding system type", type, | |
419 Qcoding_system, errb); | |
420 | |
421 return 0; | |
428 | 422 } |
423 | |
771 | 424 static int |
425 valid_coding_system_type_p (Lisp_Object type) | |
428 | 426 { |
771 | 427 return decode_coding_system_type (type, ERROR_ME_NOT) != 0; |
428 } | |
429 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
430 #ifdef MULE |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
431 static Lisp_Object Vdefault_query_coding_region_chartab_cache; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
432 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
433 /* Non-static because it's used in INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA. */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
434 Lisp_Object |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
435 default_query_method (Lisp_Object codesys, struct buffer *buf, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
436 Charbpos end, int flags) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
437 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
438 Charbpos pos = BUF_PT (buf), fail_range_start, fail_range_end; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
439 Charbpos pos_byte = BYTE_BUF_PT (buf); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
440 Lisp_Object safe_charsets = XCODING_SYSTEM_SAFE_CHARSETS (codesys); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
441 Lisp_Object safe_chars = XCODING_SYSTEM_SAFE_CHARS (codesys), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
442 result = Qnil; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
443 enum query_coding_failure_reasons failed_reason, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
444 previous_failed_reason = query_coding_succeeded; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
445 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
446 /* safe-charsets of t means the coding system can encode everything. */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
447 if (EQ (Qnil, safe_chars)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
448 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
449 if (EQ (Qt, safe_charsets)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
450 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
451 return Qnil; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
452 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
453 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
454 /* If we've no information on what characters the coding system can |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
455 encode, give up. */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
456 if (EQ (Qnil, safe_charsets) && EQ (Qnil, safe_chars)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
457 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
458 return Qunbound; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
459 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
460 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
461 safe_chars = Fgethash (safe_charsets, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
462 Vdefault_query_coding_region_chartab_cache, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
463 Qnil); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
464 if (NILP (safe_chars)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
465 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
466 safe_chars = Fmake_char_table (Qgeneric); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
467 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
468 EXTERNAL_LIST_LOOP_2 (safe_charset, safe_charsets) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
469 Fput_char_table (safe_charset, Qt, safe_chars); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
470 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
471 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
472 Fputhash (safe_charsets, safe_chars, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
473 Vdefault_query_coding_region_chartab_cache); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
474 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
475 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
476 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
477 if (flags & QUERY_METHOD_HIGHLIGHT && |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
478 /* If we're being called really early, live without highlights getting |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
479 cleared properly: */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
480 !(UNBOUNDP (XSYMBOL (Qquery_coding_clear_highlights)->function))) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
481 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
482 /* It's okay to call Lisp here, the only non-stack object we may have |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
483 allocated up to this point is safe_chars, and that's |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
484 reachable from its entry in |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
485 Vdefault_query_coding_region_chartab_cache */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
486 call3 (Qquery_coding_clear_highlights, make_int (pos), make_int (end), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
487 wrap_buffer (buf)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
488 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
489 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
490 while (pos < end) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
491 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
492 Ichar ch = BYTE_BUF_FETCH_CHAR (buf, pos_byte); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
493 if (!EQ (Qnil, get_char_table (ch, safe_chars))) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
494 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
495 pos++; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
496 INC_BYTEBPOS (buf, pos_byte); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
497 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
498 else |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
499 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
500 fail_range_start = pos; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
501 while ((pos < end) && |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
502 (EQ (Qnil, get_char_table (ch, safe_chars)) |
4839
5f1c6ca4d05e
fix bad change to default_query_method, rewrite conditional for more correctness
Ben Wing <ben@xemacs.org>
parents:
4830
diff
changeset
|
503 && (failed_reason = query_coding_unencodable, |
5f1c6ca4d05e
fix bad change to default_query_method, rewrite conditional for more correctness
Ben Wing <ben@xemacs.org>
parents:
4830
diff
changeset
|
504 (previous_failed_reason == query_coding_succeeded |
5f1c6ca4d05e
fix bad change to default_query_method, rewrite conditional for more correctness
Ben Wing <ben@xemacs.org>
parents:
4830
diff
changeset
|
505 || previous_failed_reason == failed_reason)))) |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
506 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
507 pos++; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
508 INC_BYTEBPOS (buf, pos_byte); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
509 ch = BYTE_BUF_FETCH_CHAR (buf, pos_byte); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
510 previous_failed_reason = failed_reason; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
511 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
512 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
513 if (fail_range_start == pos) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
514 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
515 /* The character can actually be encoded; move on. */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
516 pos++; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
517 INC_BYTEBPOS (buf, pos_byte); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
518 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
519 else |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
520 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
521 assert (previous_failed_reason == query_coding_unencodable); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
522 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
523 if (flags & QUERY_METHOD_ERRORP) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
524 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
525 DECLARE_EISTRING (error_details); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
526 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
527 eicpy_ascii (error_details, "Cannot encode "); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
528 eicat_lstr (error_details, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
529 make_string_from_buffer (buf, fail_range_start, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
530 pos - |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
531 fail_range_start)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
532 eicat_ascii (error_details, " using coding system"); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
533 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
534 signal_error (Qtext_conversion_error, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
535 (const CIbyte *)(eidata (error_details)), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
536 XCODING_SYSTEM_NAME (codesys)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
537 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
538 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
539 if (NILP (result)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
540 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
541 result = Fmake_range_table (Qstart_closed_end_open); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
542 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
543 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
544 fail_range_end = pos; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
545 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
546 Fput_range_table (make_int (fail_range_start), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
547 make_int (fail_range_end), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
548 Qunencodable, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
549 result); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
550 previous_failed_reason = query_coding_succeeded; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
551 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
552 if (flags & QUERY_METHOD_HIGHLIGHT) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
553 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
554 Lisp_Object extent |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
555 = Fmake_extent (make_int (fail_range_start), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
556 make_int (fail_range_end), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
557 wrap_buffer (buf)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
558 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
559 Fset_extent_priority |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
560 (extent, make_int (2 + mouse_highlight_priority)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
561 Fset_extent_face (extent, Qquery_coding_warning_face); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
562 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
563 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
564 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
565 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
566 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
567 return result; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
568 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
569 #else |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
570 Lisp_Object |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
571 default_query_method (Lisp_Object UNUSED (codesys), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
572 struct buffer * UNUSED (buf), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
573 Charbpos UNUSED (end), int UNUSED (flags)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
574 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
575 return Qnil; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
576 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
577 #endif /* defined MULE */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
578 |
771 | 579 DEFUN ("valid-coding-system-type-p", Fvalid_coding_system_type_p, 1, 1, 0, /* |
580 Given a CODING-SYSTEM-TYPE, return non-nil if it is valid. | |
581 Valid types depend on how XEmacs was compiled but may include | |
3025 | 582 `undecided', `chain', `integer', `ccl', `iso2022', `big5', `shift-jis', |
583 `utf-16', `ucs-4', `utf-8', etc. | |
771 | 584 */ |
585 (coding_system_type)) | |
586 { | |
587 return valid_coding_system_type_p (coding_system_type) ? Qt : Qnil; | |
588 } | |
589 | |
590 DEFUN ("coding-system-type-list", Fcoding_system_type_list, 0, 0, 0, /* | |
591 Return a list of valid coding system types. | |
592 */ | |
593 ()) | |
594 { | |
595 return Fcopy_sequence (Vcoding_system_type_list); | |
596 } | |
597 | |
598 void | |
599 add_entry_to_coding_system_type_list (struct coding_system_methods *meths) | |
600 { | |
601 struct coding_system_type_entry entry; | |
602 | |
603 entry.meths = meths; | |
604 Dynarr_add (the_coding_system_type_entry_dynarr, entry); | |
605 Vcoding_system_type_list = Fcons (meths->type, Vcoding_system_type_list); | |
428 | 606 } |
607 | |
608 DEFUN ("coding-system-p", Fcoding_system_p, 1, 1, 0, /* | |
609 Return t if OBJECT is a coding system. | |
610 A coding system is an object that defines how text containing multiple | |
611 character sets is encoded into a stream of (typically 8-bit) bytes. | |
612 The coding system is used to decode the stream into a series of | |
613 characters (which may be from multiple charsets) when the text is read | |
614 from a file or process, and is used to encode the text back into the | |
615 same format when it is written out to a file or process. | |
616 | |
617 For example, many ISO2022-compliant coding systems (such as Compound | |
618 Text, which is used for inter-client data under the X Window System) | |
619 use escape sequences to switch between different charsets -- Japanese | |
620 Kanji, for example, is invoked with "ESC $ ( B"; ASCII is invoked | |
621 with "ESC ( B"; and Cyrillic is invoked with "ESC - L". See | |
622 `make-coding-system' for more information. | |
623 | |
624 Coding systems are normally identified using a symbol, and the | |
625 symbol is accepted in place of the actual coding system object whenever | |
626 a coding system is called for. (This is similar to how faces work.) | |
627 */ | |
628 (object)) | |
629 { | |
630 return CODING_SYSTEMP (object) ? Qt : Qnil; | |
631 } | |
632 | |
4303 | 633 static Lisp_Object |
634 find_coding_system (Lisp_Object coding_system_or_name, | |
635 int do_autoloads) | |
636 { | |
637 Lisp_Object lookup; | |
638 | |
639 if (NILP (coding_system_or_name)) | |
640 coding_system_or_name = Qbinary; | |
641 else if (CODING_SYSTEMP (coding_system_or_name)) | |
642 return coding_system_or_name; | |
643 else | |
644 CHECK_SYMBOL (coding_system_or_name); | |
645 | |
646 while (1) | |
647 { | |
648 lookup = | |
649 Fgethash (coding_system_or_name, Vcoding_system_hash_table, Qnil); | |
650 | |
651 if (CONSP (lookup) && do_autoloads) | |
652 { | |
653 struct gcpro gcpro1; | |
654 int length; | |
655 DECLARE_EISTRING (desired_base); | |
656 DECLARE_EISTRING (warning_info); | |
657 | |
658 eicpy_lstr (desired_base, XSYMBOL_NAME (coding_system_or_name)); | |
659 | |
660 /* Work out the name of the base coding system. */ | |
661 length = eilen (desired_base); | |
662 if (length > (int)(sizeof ("-unix") - 1)) | |
663 { | |
664 if (0 == qxestrcmp ((UAscbyte *)"-unix", (eidata (desired_base)) | |
665 + (length - (sizeof ("-unix") - 1)))) | |
666 { | |
667 eidel (desired_base, length - (sizeof ("-unix") - 1), | |
668 -1, 5, 5); | |
669 } | |
670 } | |
671 else if (length > (int)(sizeof ("-dos") - 1)) | |
672 { | |
673 if ((0 == qxestrcmp ((UAscbyte *)"-dos", (eidata (desired_base)) | |
674 + (length - (sizeof ("-dos") - 1)))) || | |
675 (0 == qxestrcmp ((UAscbyte *)"-mac", (eidata (desired_base)) | |
676 + (length - (sizeof ("-mac") - 1))))) | |
677 { | |
678 eidel (desired_base, length - (sizeof ("-dos") - 1), -1, | |
679 4, 4); | |
680 } | |
681 } | |
682 | |
683 coding_system_or_name = intern_int (eidata (desired_base)); | |
684 | |
685 /* Remove this coding system and its subsidiary coding | |
686 systems from the hash, to avoid calling this code recursively. */ | |
687 Fremhash (coding_system_or_name, Vcoding_system_hash_table); | |
688 Fremhash (add_suffix_to_symbol(coding_system_or_name, "-unix"), | |
689 Vcoding_system_hash_table); | |
690 Fremhash (add_suffix_to_symbol(coding_system_or_name, "-dos"), | |
691 Vcoding_system_hash_table); | |
692 Fremhash (add_suffix_to_symbol(coding_system_or_name, "-mac"), | |
693 Vcoding_system_hash_table); | |
694 | |
695 eicpy_ascii (warning_info, "Error autoloading coding system "); | |
696 eicat_lstr (warning_info, XSYMBOL_NAME (coding_system_or_name)); | |
697 | |
698 /* Keep around the form so it doesn't disappear from under | |
699 #'eval's feet. */ | |
700 GCPRO1 (lookup); | |
701 call1_trapping_problems ((const CIbyte *)eidata (warning_info), | |
702 Qeval, lookup, 0); | |
703 UNGCPRO; | |
704 | |
705 lookup = | |
706 Fgethash (coding_system_or_name, Vcoding_system_hash_table, Qnil); | |
707 } | |
708 | |
709 if (CODING_SYSTEMP (lookup) || NILP (lookup)) | |
710 return lookup; | |
711 | |
712 coding_system_or_name = lookup; | |
713 } | |
714 } | |
715 | |
428 | 716 DEFUN ("find-coding-system", Ffind_coding_system, 1, 1, 0, /* |
717 Retrieve the coding system of the given name. | |
718 | |
719 If CODING-SYSTEM-OR-NAME is a coding-system object, it is simply | |
720 returned. Otherwise, CODING-SYSTEM-OR-NAME should be a symbol. | |
721 If there is no such coding system, nil is returned. Otherwise the | |
722 associated coding system object is returned. | |
723 */ | |
724 (coding_system_or_name)) | |
725 { | |
4303 | 726 return find_coding_system(coding_system_or_name, 1); |
727 } | |
728 | |
729 DEFUN ("autoload-coding-system", Fautoload_coding_system, 2, 2, 0, /* | |
730 Define SYMBOL as a coding-system that is loaded on demand. | |
731 | |
732 FORM is a form to evaluate to define the coding-system. | |
733 */ | |
734 (symbol, form)) | |
735 { | |
736 Lisp_Object lookup; | |
737 | |
738 CHECK_SYMBOL (symbol); | |
739 CHECK_CONS (form); | |
740 | |
741 lookup = find_coding_system (symbol, 0); | |
742 | |
743 if (!NILP (lookup) && | |
744 /* Allow autoloads to be redefined. */ | |
745 !CONSP (lookup)) | |
440 | 746 { |
4303 | 747 invalid_operation ("Cannot redefine existing coding system", |
748 symbol); | |
440 | 749 } |
4303 | 750 |
751 Fputhash (symbol, form, Vcoding_system_hash_table); | |
752 Fputhash (add_suffix_to_symbol(symbol, "-unix"), form, | |
753 Vcoding_system_hash_table); | |
754 Fputhash (add_suffix_to_symbol(symbol, "-dos"), form, | |
755 Vcoding_system_hash_table); | |
756 Fputhash (add_suffix_to_symbol(symbol, "-mac"), form, | |
757 Vcoding_system_hash_table); | |
758 | |
759 /* Tell the POSIX locale infrastructure about this coding system (though | |
760 unfortunately it'll be too late for the startup locale sniffing. */ | |
761 if (!UNBOUNDP (Qposix_charset_to_coding_system_hash)) | |
762 { | |
763 Lisp_Object val = Fsymbol_value (Qposix_charset_to_coding_system_hash); | |
764 DECLARE_EISTRING (minimal_name); | |
765 Ibyte *full_name; | |
766 int len = XSTRING_LENGTH (XSYMBOL_NAME (symbol)), i; | |
767 | |
768 if (!NILP (val)) | |
769 { | |
770 full_name = XSTRING_DATA (XSYMBOL_NAME (symbol)); | |
771 for (i = 0; i < len; ++i) | |
772 { | |
773 if (full_name[i] >= '0' && full_name[i] <= '9') | |
774 { | |
775 eicat_ch (minimal_name, full_name[i]); | |
776 } | |
777 else if (full_name[i] >= 'a' && full_name[i] <= 'z') | |
778 { | |
779 eicat_ch (minimal_name, full_name[i]); | |
780 } | |
781 else if (full_name[i] >= 'A' && full_name[i] <= 'Z') | |
782 { | |
783 eicat_ch (minimal_name, full_name[i] + | |
784 ('a' - 'A')); | |
785 } | |
786 } | |
787 | |
788 if (eilen (minimal_name)) | |
789 { | |
790 CHECK_HASH_TABLE (val); | |
791 Fputhash (eimake_string(minimal_name), symbol, val); | |
792 } | |
793 } | |
794 } | |
795 | |
796 return Qt; | |
428 | 797 } |
798 | |
799 DEFUN ("get-coding-system", Fget_coding_system, 1, 1, 0, /* | |
800 Retrieve the coding system of the given name. | |
801 Same as `find-coding-system' except that if there is no such | |
802 coding system, an error is signaled instead of returning nil. | |
803 */ | |
804 (name)) | |
805 { | |
806 Lisp_Object coding_system = Ffind_coding_system (name); | |
807 | |
808 if (NILP (coding_system)) | |
563 | 809 invalid_argument ("No such coding system", name); |
428 | 810 return coding_system; |
811 } | |
812 | |
771 | 813 int |
814 coding_system_is_binary (Lisp_Object coding_system) | |
815 { | |
816 Lisp_Coding_System *cs = XCODING_SYSTEM (coding_system); | |
817 return | |
818 (EQ (CODING_SYSTEM_TYPE (cs), Qno_conversion) && | |
819 CODING_SYSTEM_EOL_TYPE (cs) == EOL_LF && | |
820 EQ (CODING_SYSTEM_POST_READ_CONVERSION (cs), Qnil) && | |
821 EQ (CODING_SYSTEM_PRE_WRITE_CONVERSION (cs), Qnil)); | |
822 } | |
823 | |
824 static Lisp_Object | |
825 coding_system_real_canonical (Lisp_Object cs) | |
826 { | |
827 if (!NILP (XCODING_SYSTEM_CANONICAL (cs))) | |
828 return XCODING_SYSTEM_CANONICAL (cs); | |
829 return cs; | |
830 } | |
831 | |
832 /* Return true if coding system is of the "standard" type that decodes | |
833 bytes into characters (suitable for decoding a text file). */ | |
834 int | |
835 coding_system_is_for_text_file (Lisp_Object coding_system) | |
836 { | |
837 return (XCODESYSMETH_OR_GIVEN | |
838 (coding_system, conversion_end_type, | |
839 (coding_system_real_canonical (coding_system)), | |
840 DECODES_BYTE_TO_CHARACTER) == | |
841 DECODES_BYTE_TO_CHARACTER); | |
842 } | |
843 | |
844 static int | |
845 decoding_source_sink_type_is_char (Lisp_Object cs, enum source_or_sink sex) | |
846 { | |
847 enum source_sink_type type = | |
848 XCODESYSMETH_OR_GIVEN (cs, conversion_end_type, | |
849 (coding_system_real_canonical (cs)), | |
850 DECODES_BYTE_TO_CHARACTER); | |
851 if (sex == CODING_SOURCE) | |
852 return (type == DECODES_CHARACTER_TO_CHARACTER || | |
853 type == DECODES_CHARACTER_TO_BYTE); | |
854 else | |
855 return (type == DECODES_CHARACTER_TO_CHARACTER || | |
856 type == DECODES_BYTE_TO_CHARACTER); | |
857 } | |
858 | |
859 static int | |
860 encoding_source_sink_type_is_char (Lisp_Object cs, enum source_or_sink sex) | |
861 { | |
862 return decoding_source_sink_type_is_char (cs, | |
863 /* Sex change */ | |
864 sex == CODING_SOURCE ? | |
865 CODING_SINK : CODING_SOURCE); | |
866 } | |
867 | |
868 /* Like Ffind_coding_system() but check that the coding system is of the | |
869 "standard" type that decodes bytes into characters (suitable for | |
870 decoding a text file), and if not, returns an appropriate wrapper that | |
871 does. Also, if EOL_WRAP is non-zero, check whether this coding system | |
872 wants EOL auto-detection, and if so, wrap with a convert-eol coding | |
873 system to do this. */ | |
874 | |
875 Lisp_Object | |
876 find_coding_system_for_text_file (Lisp_Object name, int eol_wrap) | |
877 { | |
878 Lisp_Object coding_system = Ffind_coding_system (name); | |
879 Lisp_Object wrapper = coding_system; | |
880 | |
881 if (NILP (coding_system)) | |
882 return Qnil; | |
883 if (!coding_system_is_for_text_file (coding_system)) | |
884 { | |
885 wrapper = XCODING_SYSTEM_TEXT_FILE_WRAPPER (coding_system); | |
886 if (NILP (wrapper)) | |
887 { | |
888 Lisp_Object chain; | |
889 if (!decoding_source_sink_type_is_char (coding_system, CODING_SINK)) | |
890 chain = list2 (coding_system, Qbinary); | |
891 else | |
892 chain = list1 (coding_system); | |
893 if (decoding_source_sink_type_is_char (coding_system, CODING_SOURCE)) | |
894 chain = Fcons (Qbinary, chain); | |
895 wrapper = | |
896 make_internal_coding_system | |
897 (coding_system, | |
898 "internal-text-file-wrapper", | |
899 Qchain, | |
900 Qunbound, list4 (Qchain, chain, | |
901 Qcanonicalize_after_coding, coding_system)); | |
902 XCODING_SYSTEM_TEXT_FILE_WRAPPER (coding_system) = wrapper; | |
903 } | |
904 } | |
905 | |
906 if (!eol_wrap || XCODING_SYSTEM_EOL_TYPE (coding_system) != EOL_AUTODETECT) | |
907 return wrapper; | |
908 | |
909 coding_system = wrapper; | |
910 wrapper = XCODING_SYSTEM_AUTO_EOL_WRAPPER (coding_system); | |
911 if (!NILP (wrapper)) | |
912 return wrapper; | |
913 wrapper = | |
914 make_internal_coding_system | |
915 (coding_system, | |
916 "internal-auto-eol-wrapper", | |
917 Qundecided, Qunbound, | |
918 list4 (Qcoding_system, coding_system, | |
919 Qdo_eol, Qt)); | |
920 XCODING_SYSTEM_AUTO_EOL_WRAPPER (coding_system) = wrapper; | |
921 return wrapper; | |
922 } | |
923 | |
924 /* Like Fget_coding_system() but verify that the coding system is of the | |
925 "standard" type that decodes bytes into characters (suitable for | |
926 decoding a text file), and if not, returns an appropriate wrapper that | |
927 does. Also, if EOL_WRAP is non-zero, check whether this coding system | |
928 wants EOL auto-detection, and if so, wrap with a convert-eol coding | |
929 system to do this. */ | |
930 | |
931 Lisp_Object | |
932 get_coding_system_for_text_file (Lisp_Object name, int eol_wrap) | |
933 { | |
934 Lisp_Object coding_system = find_coding_system_for_text_file (name, | |
935 eol_wrap); | |
936 if (NILP (coding_system)) | |
937 invalid_argument ("No such coding system", name); | |
938 return coding_system; | |
939 } | |
940 | |
941 /* We store the coding systems in hash tables with the names as the | |
942 key and the actual coding system object as the value. Occasionally | |
943 we need to use them in a list format. These routines provide us | |
944 with that. */ | |
428 | 945 struct coding_system_list_closure |
946 { | |
947 Lisp_Object *coding_system_list; | |
771 | 948 int normal; |
949 int internal; | |
428 | 950 }; |
951 | |
952 static int | |
4303 | 953 add_coding_system_to_list_mapper (Lisp_Object key, Lisp_Object value, |
428 | 954 void *coding_system_list_closure) |
955 { | |
956 /* This function can GC */ | |
957 struct coding_system_list_closure *cscl = | |
958 (struct coding_system_list_closure *) coding_system_list_closure; | |
959 Lisp_Object *coding_system_list = cscl->coding_system_list; | |
960 | |
771 | 961 /* We can't just use VALUE because KEY might be an alias, and we need |
4303 | 962 the real coding system object. |
963 | |
964 Autoloaded coding systems have conses for their values, and can't be | |
965 internal coding systems, or coding system aliases. */ | |
966 if (CONSP (value) || | |
967 (XCODING_SYSTEM (Ffind_coding_system (key))->internal_p ? | |
968 cscl->internal : cscl->normal)) | |
771 | 969 *coding_system_list = Fcons (key, *coding_system_list); |
428 | 970 return 0; |
971 } | |
972 | |
2297 | 973 /* #### should we specify a conventional for "all coding systems"? */ |
771 | 974 DEFUN ("coding-system-list", Fcoding_system_list, 0, 1, 0, /* |
428 | 975 Return a list of the names of all defined coding systems. |
771 | 976 If INTERNAL is nil, only the normal (non-internal) coding systems are |
977 included. (Internal coding systems are created for various internal | |
978 purposes, such as implementing EOL types of CRLF and CR; generally, you do | |
979 not want to see these.) If it is t, only the internal coding systems are | |
980 included. If it is any other non-nil value both normal and internal are | |
981 included. | |
428 | 982 */ |
771 | 983 (internal)) |
428 | 984 { |
985 Lisp_Object coding_system_list = Qnil; | |
986 struct gcpro gcpro1; | |
987 struct coding_system_list_closure coding_system_list_closure; | |
988 | |
989 GCPRO1 (coding_system_list); | |
990 coding_system_list_closure.coding_system_list = &coding_system_list; | |
771 | 991 coding_system_list_closure.normal = !EQ (internal, Qt); |
992 coding_system_list_closure.internal = !NILP (internal); | |
428 | 993 elisp_maphash (add_coding_system_to_list_mapper, Vcoding_system_hash_table, |
994 &coding_system_list_closure); | |
995 UNGCPRO; | |
996 | |
997 return coding_system_list; | |
998 } | |
999 | |
1000 DEFUN ("coding-system-name", Fcoding_system_name, 1, 1, 0, /* | |
1001 Return the name of the given coding system. | |
1002 */ | |
1003 (coding_system)) | |
1004 { | |
1005 coding_system = Fget_coding_system (coding_system); | |
1006 return XCODING_SYSTEM_NAME (coding_system); | |
1007 } | |
1008 | |
1009 static Lisp_Coding_System * | |
771 | 1010 allocate_coding_system (struct coding_system_methods *codesys_meths, |
1011 Bytecount data_size, | |
1012 Lisp_Object name) | |
428 | 1013 { |
771 | 1014 Bytecount total_size = offsetof (Lisp_Coding_System, data) + data_size; |
428 | 1015 Lisp_Coding_System *codesys = |
3024 | 1016 (Lisp_Coding_System *) BASIC_ALLOC_LCRECORD (total_size, |
1204 | 1017 &lrecord_coding_system); |
1018 | |
771 | 1019 codesys->methods = codesys_meths; |
1204 | 1020 #define MARKED_SLOT(x) codesys->x = Qnil; |
1021 #include "coding-system-slots.h" | |
1022 | |
771 | 1023 CODING_SYSTEM_EOL_TYPE (codesys) = EOL_LF; |
1024 CODING_SYSTEM_NAME (codesys) = name; | |
1025 | |
1026 MAYBE_CODESYSMETH (codesys, init, (wrap_coding_system (codesys))); | |
428 | 1027 |
1028 return codesys; | |
1029 } | |
1030 | |
771 | 1031 static enum eol_type |
1032 symbol_to_eol_type (Lisp_Object symbol) | |
1033 { | |
1034 CHECK_SYMBOL (symbol); | |
1035 if (NILP (symbol)) return EOL_AUTODETECT; | |
1036 if (EQ (symbol, Qlf)) return EOL_LF; | |
1037 if (EQ (symbol, Qcrlf)) return EOL_CRLF; | |
1038 if (EQ (symbol, Qcr)) return EOL_CR; | |
1039 | |
1040 invalid_constant ("Unrecognized eol type", symbol); | |
1204 | 1041 RETURN_NOT_REACHED (EOL_AUTODETECT); |
771 | 1042 } |
1043 | |
1044 static Lisp_Object | |
1045 eol_type_to_symbol (enum eol_type type) | |
1046 { | |
1047 switch (type) | |
1048 { | |
2500 | 1049 default: ABORT (); |
771 | 1050 case EOL_LF: return Qlf; |
1051 case EOL_CRLF: return Qcrlf; | |
1052 case EOL_CR: return Qcr; | |
1053 case EOL_AUTODETECT: return Qnil; | |
1054 } | |
1055 } | |
1056 | |
1057 struct subsidiary_type | |
1058 { | |
4528
726060ee587c
First draft of g++ 4.3 warning removal patch. Builds. *Needs ChangeLogs.*
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4522
diff
changeset
|
1059 const Ascbyte *extension; |
726060ee587c
First draft of g++ 4.3 warning removal patch. Builds. *Needs ChangeLogs.*
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4522
diff
changeset
|
1060 const Ascbyte *mnemonic_ext; |
771 | 1061 enum eol_type eol; |
1062 }; | |
1063 | |
1064 static struct subsidiary_type coding_subsidiary_list[] = | |
1065 { { "-unix", "", EOL_LF }, | |
1066 { "-dos", ":T", EOL_CRLF }, | |
1067 { "-mac", ":t", EOL_CR } }; | |
1068 | |
1069 /* kludge */ | |
428 | 1070 static void |
771 | 1071 setup_eol_coding_systems (Lisp_Object codesys) |
428 | 1072 { |
793 | 1073 int len = XSTRING_LENGTH (XSYMBOL (XCODING_SYSTEM_NAME (codesys))->name); |
2367 | 1074 Ibyte *codesys_name = alloca_ibytes (len + 7); |
771 | 1075 int mlen = -1; |
867 | 1076 Ibyte *codesys_mnemonic = 0; |
771 | 1077 Lisp_Object codesys_name_sym, sub_codesys; |
1078 int i; | |
1079 | |
1080 memcpy (codesys_name, | |
793 | 1081 XSTRING_DATA (XSYMBOL (XCODING_SYSTEM_NAME (codesys))->name), len); |
771 | 1082 |
1083 if (STRINGP (XCODING_SYSTEM_MNEMONIC (codesys))) | |
428 | 1084 { |
771 | 1085 mlen = XSTRING_LENGTH (XCODING_SYSTEM_MNEMONIC (codesys)); |
2367 | 1086 codesys_mnemonic = alloca_ibytes (mlen + 7); |
771 | 1087 memcpy (codesys_mnemonic, |
1088 XSTRING_DATA (XCODING_SYSTEM_MNEMONIC (codesys)), mlen); | |
1089 } | |
1090 | |
1091 /* Create three "subsidiary" coding systems, decoding data encoded using | |
1092 each of the three EOL types. We do this for each subsidiary by | |
1093 copying the original coding system, setting the EOL type | |
1094 appropriately, and setting the CANONICAL member of the new coding | |
1095 system to be a chain consisting of the original coding system followed | |
1096 by a convert-eol coding system to do the EOL decoding. For EOL type | |
1097 LF, however, we don't need any decoding, so we skip creating a | |
1098 CANONICAL. | |
1099 | |
1100 If the original coding system is not a text-type coding system | |
1101 (decodes byte->char), we need to coerce it to one by the appropriate | |
1102 wrapping in CANONICAL. */ | |
1103 | |
1104 for (i = 0; i < countof (coding_subsidiary_list); i++) | |
1105 { | |
4528
726060ee587c
First draft of g++ 4.3 warning removal patch. Builds. *Needs ChangeLogs.*
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4522
diff
changeset
|
1106 const Ascbyte *extension = coding_subsidiary_list[i].extension; |
726060ee587c
First draft of g++ 4.3 warning removal patch. Builds. *Needs ChangeLogs.*
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4522
diff
changeset
|
1107 const Ascbyte *mnemonic_ext = coding_subsidiary_list[i].mnemonic_ext; |
771 | 1108 enum eol_type eol = coding_subsidiary_list[i].eol; |
1109 | |
2367 | 1110 qxestrcpy_ascii (codesys_name + len, extension); |
771 | 1111 codesys_name_sym = intern_int (codesys_name); |
1112 if (mlen != -1) | |
2367 | 1113 qxestrcpy_ascii (codesys_mnemonic + mlen, mnemonic_ext); |
771 | 1114 |
1115 sub_codesys = Fcopy_coding_system (codesys, codesys_name_sym); | |
1116 if (mlen != -1) | |
1117 XCODING_SYSTEM_MNEMONIC (sub_codesys) = | |
1118 build_intstring (codesys_mnemonic); | |
1119 | |
1120 if (eol != EOL_LF) | |
1121 { | |
1122 Lisp_Object chain = list2 (get_coding_system_for_text_file | |
1123 (codesys, 0), | |
1124 eol == EOL_CR ? Qconvert_eol_cr : | |
1125 Qconvert_eol_crlf); | |
1126 Lisp_Object canon = | |
1127 make_internal_coding_system | |
1128 (sub_codesys, "internal-subsidiary-eol-wrapper", | |
1129 Qchain, Qunbound, | |
1130 mlen != -1 ? | |
1131 list6 (Qmnemonic, build_intstring (codesys_mnemonic), | |
1132 Qchain, chain, | |
1133 Qcanonicalize_after_coding, sub_codesys) : | |
1134 list4 (Qchain, chain, | |
1135 Qcanonicalize_after_coding, sub_codesys)); | |
1136 XCODING_SYSTEM_CANONICAL (sub_codesys) = canon; | |
1137 } | |
1138 XCODING_SYSTEM_EOL_TYPE (sub_codesys) = eol; | |
1139 XCODING_SYSTEM_SUBSIDIARY_PARENT (sub_codesys) = codesys; | |
1140 XCODING_SYSTEM (codesys)->eol[eol] = sub_codesys; | |
428 | 1141 } |
1142 } | |
1143 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1144 DEFUN ("coding-system-canonical-name-p", Fcoding_system_canonical_name_p, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1145 1, 1, 0, /* |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1146 Return t if OBJECT names a coding system, and is not a coding system alias. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1147 */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1148 (object)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1149 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1150 return CODING_SYSTEMP (Fgethash (object, Vcoding_system_hash_table, Qnil)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1151 ? Qt : Qnil; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1152 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1153 |
771 | 1154 /* Basic function to create new coding systems. For `make-coding-system', |
1155 NAME-OR-EXISTING is the NAME argument, PREFIX is null, and TYPE, | |
1156 DESCRIPTION, and PROPS are the same. All created coding systems are put | |
1157 in a hash table indexed by NAME. | |
1158 | |
1159 If PREFIX is a string, NAME-OR-EXISTING should specify an existing | |
1160 coding system (or nil), and an internal coding system will be created. | |
1161 The name of the coding system will be constructed by combining PREFIX | |
1162 with the name of the existing coding system (if given), and a number | |
1163 will be appended to insure uniqueness. In such a case, if Qunbound is | |
1164 given for DESCRIPTION, the description gets created based on the | |
1165 generated name. Also, if no mnemonic is given in the properties list, a | |
1166 mnemonic is created based on the generated name. | |
1167 | |
1168 For internal coding systems, the coding system is marked as internal | |
1169 (see `coding-system-list'), and no subsidiaries will be created or | |
1170 eol-wrapping will happen. Otherwise: | |
1171 | |
1172 -- if the eol-type property is `lf' or t, the coding system is merely | |
1173 created and returned. (For t, the coding system will be wrapped with | |
1174 an EOL autodetector when it's used to read a file.) | |
1175 | |
1176 -- if eol-type is `crlf' or `cr', after the coding system object is | |
1177 created, it will be wrapped in a chain with the appropriate | |
1178 convert-eol coding system (either `convert-eol-crlf' or | |
1179 `convert-eol-cr'), so that CRLF->LF or CR->LF conversion is done at | |
1180 decoding time, and the opposite at encoding time. The resulting | |
1181 chain becomes the CANONICAL field of the coding system object. | |
1182 | |
1183 -- if eol-type is nil or omitted, "subsidiaries" are generated: Three | |
1184 coding systems where the original coding system (before wrapping with | |
1185 convert-eol-autodetect) is either unwrapped or wrapped with | |
1186 convert-eol-crlf or convert-eol-cr, respectively, so that coding systems | |
1187 to handle LF, CRLF, and CR end-of-line indicators are created. (This | |
1188 crazy crap is based on existing behavior in other Mule versions, | |
1189 including FSF Emacs.) | |
1190 */ | |
428 | 1191 |
1192 static Lisp_Object | |
4528
726060ee587c
First draft of g++ 4.3 warning removal patch. Builds. *Needs ChangeLogs.*
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4522
diff
changeset
|
1193 make_coding_system_1 (Lisp_Object name_or_existing, const Ascbyte *prefix, |
771 | 1194 Lisp_Object type, Lisp_Object description, |
1195 Lisp_Object props) | |
428 | 1196 { |
771 | 1197 Lisp_Coding_System *cs; |
1198 int need_to_setup_eol_systems = 1; | |
1199 enum eol_type eol_wrapper = EOL_AUTODETECT; | |
1200 struct coding_system_methods *meths; | |
1201 Lisp_Object csobj; | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1202 Lisp_Object defmnem = Qnil, aliases = Qnil; |
771 | 1203 |
1204 if (NILP (type)) | |
1205 type = Qundecided; | |
1206 meths = decode_coding_system_type (type, ERROR_ME); | |
1207 | |
1208 if (prefix) | |
428 | 1209 { |
867 | 1210 Ibyte *newname = |
771 | 1211 emacs_sprintf_malloc (NULL, "%s-%s-%d", |
1212 prefix, | |
867 | 1213 NILP (name_or_existing) ? (Ibyte *) "nil" : |
771 | 1214 XSTRING_DATA (Fsymbol_name (XCODING_SYSTEM_NAME |
1215 (name_or_existing))), | |
1216 ++coding_system_tick); | |
1217 name_or_existing = intern_int (newname); | |
1726 | 1218 xfree (newname, Ibyte *); |
771 | 1219 |
1220 if (UNBOUNDP (description)) | |
1221 { | |
1222 newname = | |
1223 emacs_sprintf_malloc | |
1224 (NULL, "For Internal Use (%s)", | |
1225 XSTRING_DATA (Fsymbol_name (name_or_existing))); | |
1226 description = build_intstring (newname); | |
1726 | 1227 xfree (newname, Ibyte *); |
771 | 1228 } |
1229 | |
1230 newname = emacs_sprintf_malloc (NULL, "Int%d", coding_system_tick); | |
1231 defmnem = build_intstring (newname); | |
1726 | 1232 xfree (newname, Ibyte *); |
428 | 1233 } |
771 | 1234 else |
1235 CHECK_SYMBOL (name_or_existing); | |
1236 | |
4303 | 1237 /* See is there an entry for name_or_existing in the defined coding system |
1238 hash table. */ | |
1239 csobj = find_coding_system (name_or_existing, 0); | |
1240 /* Error if it's there and not an autoload form. */ | |
1241 if (!NILP (csobj) && !CONSP (csobj)) | |
771 | 1242 invalid_operation ("Cannot redefine existing coding system", |
4303 | 1243 name_or_existing); |
771 | 1244 |
1245 cs = allocate_coding_system (meths, meths->extra_data_size, | |
1246 name_or_existing); | |
793 | 1247 csobj = wrap_coding_system (cs); |
771 | 1248 |
1249 cs->internal_p = !!prefix; | |
1250 | |
1251 if (NILP (description)) | |
1252 description = build_string (""); | |
1253 else | |
1254 CHECK_STRING (description); | |
1255 CODING_SYSTEM_DESCRIPTION (cs) = description; | |
1256 | |
1257 if (!NILP (defmnem)) | |
1258 CODING_SYSTEM_MNEMONIC (cs) = defmnem; | |
1259 | |
1260 { | |
1261 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, props) | |
1262 { | |
1263 int recognized = 1; | |
1264 | |
1265 if (EQ (key, Qmnemonic)) | |
1266 { | |
1267 if (!NILP (value)) | |
1268 CHECK_STRING (value); | |
1269 CODING_SYSTEM_MNEMONIC (cs) = value; | |
1270 } | |
1271 | |
1272 else if (EQ (key, Qdocumentation)) | |
1273 { | |
1274 if (!NILP (value)) | |
1275 CHECK_STRING (value); | |
1276 CODING_SYSTEM_DOCUMENTATION (cs) = value; | |
1277 } | |
1278 | |
1279 else if (EQ (key, Qeol_type)) | |
1280 { | |
1281 need_to_setup_eol_systems = NILP (value); | |
1282 if (EQ (value, Qt)) | |
1283 value = Qnil; | |
1284 eol_wrapper = symbol_to_eol_type (value); | |
1285 } | |
1286 | |
1287 else if (EQ (key, Qpost_read_conversion)) | |
1288 CODING_SYSTEM_POST_READ_CONVERSION (cs) = value; | |
1289 else if (EQ (key, Qpre_write_conversion)) | |
1290 CODING_SYSTEM_PRE_WRITE_CONVERSION (cs) = value; | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1291 else if (EQ (key, Qaliases)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1292 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1293 EXTERNAL_LIST_LOOP_2 (alias, value) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1294 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1295 CHECK_SYMBOL (alias); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1296 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1297 if (!NILP (Fcoding_system_canonical_name_p (alias))) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1298 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1299 invalid_change ("Symbol is the canonical name of a " |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1300 "coding system and cannot be redefined", |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1301 alias); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1302 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1303 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1304 aliases = value; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1305 } |
771 | 1306 /* FSF compatibility */ |
1307 else if (EQ (key, Qtranslation_table_for_decode)) | |
1308 ; | |
1309 else if (EQ (key, Qtranslation_table_for_encode)) | |
1310 ; | |
1311 else if (EQ (key, Qsafe_chars)) | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1312 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1313 CHECK_CHAR_TABLE (value); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1314 CODING_SYSTEM_SAFE_CHARS (cs) = value; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1315 } |
771 | 1316 else if (EQ (key, Qsafe_charsets)) |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1317 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1318 if (!EQ (Qt, value) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1319 /* Would be nice to actually do this check, but there are |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1320 some order conflicts with japanese.el and |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1321 mule-coding.el */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1322 && 0) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1323 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1324 #ifdef MULE |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1325 EXTERNAL_LIST_LOOP_2 (safe_charset, value) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1326 CHECK_CHARSET (Ffind_charset (safe_charset)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1327 #endif |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1328 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1329 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1330 CODING_SYSTEM_SAFE_CHARSETS (cs) = value; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1331 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1332 else if (EQ (key, Qcategory)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1333 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1334 Fput (name_or_existing, intern ("coding-system-property"), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1335 Fplist_put (Fget (name_or_existing, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1336 intern ("coding-system-property"), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1337 Qnil), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1338 Qcategory, value)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1339 } |
771 | 1340 else if (EQ (key, Qmime_charset)) |
1341 ; | |
1342 else if (EQ (key, Qvalid_codes)) | |
1343 ; | |
1344 else | |
1345 recognized = CODESYSMETH_OR_GIVEN (cs, putprop, | |
1346 (csobj, key, value), 0); | |
1347 | |
1348 if (!recognized) | |
1349 invalid_constant ("Unrecognized property", key); | |
1350 } | |
1351 } | |
1352 | |
1353 { | |
1354 XCODING_SYSTEM_CANONICAL (csobj) = | |
1355 CODESYSMETH_OR_GIVEN (cs, canonicalize, (csobj), Qnil); | |
1356 XCODING_SYSTEM_EOL_TYPE (csobj) = EOL_AUTODETECT; /* for copy-coding-system | |
1357 below */ | |
1358 | |
4303 | 1359 Fputhash (name_or_existing, csobj, Vcoding_system_hash_table); |
1360 | |
771 | 1361 if (need_to_setup_eol_systems && !cs->internal_p) |
1362 setup_eol_coding_systems (csobj); | |
1363 else if (eol_wrapper == EOL_CR || eol_wrapper == EOL_CRLF) | |
1364 { | |
1365 /* If a specific eol-type (other than LF) was specified, we handle | |
1366 this by converting the coding system into a chain that wraps the | |
1367 coding system along with a convert-eol system after it, in | |
1368 exactly that same switcheroo fashion that the normal | |
1369 canonicalize method works -- BUT we will run into a problem if | |
1370 we do it the obvious way, because when `chain' creates its | |
1371 substreams, the substream containing the coding system we're | |
1372 creating will have canonicalization expansion done on it, | |
1373 leading to infinite recursion. So we have to generate a new, | |
1374 internal coding system with the previous value of CANONICAL. */ | |
867 | 1375 Ibyte *newname = |
771 | 1376 emacs_sprintf_malloc |
1377 (NULL, "internal-eol-copy-%s-%d", | |
1378 XSTRING_DATA (Fsymbol_name (name_or_existing)), | |
1379 ++coding_system_tick); | |
1380 Lisp_Object newnamesym = intern_int (newname); | |
1381 Lisp_Object copied = Fcopy_coding_system (csobj, newnamesym); | |
1726 | 1382 xfree (newname, Ibyte *); |
771 | 1383 |
1384 XCODING_SYSTEM_CANONICAL (csobj) = | |
1385 make_internal_coding_system | |
1386 (csobj, | |
1387 "internal-eol-wrapper", | |
1388 Qchain, Qunbound, | |
1389 list4 (Qchain, | |
1390 list2 (copied, | |
1391 eol_wrapper == EOL_CR ? | |
1392 Qconvert_eol_cr : | |
1393 Qconvert_eol_crlf), | |
1394 Qcanonicalize_after_coding, | |
1395 csobj)); | |
1396 } | |
1397 XCODING_SYSTEM_EOL_TYPE (csobj) = eol_wrapper; | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1398 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1399 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1400 EXTERNAL_LIST_LOOP_2 (alias, aliases) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1401 Fdefine_coding_system_alias (alias, csobj); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1402 } |
771 | 1403 } |
1404 | |
1405 return csobj; | |
428 | 1406 } |
1407 | |
771 | 1408 Lisp_Object |
4528
726060ee587c
First draft of g++ 4.3 warning removal patch. Builds. *Needs ChangeLogs.*
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4522
diff
changeset
|
1409 make_internal_coding_system (Lisp_Object existing, const Ascbyte *prefix, |
771 | 1410 Lisp_Object type, Lisp_Object description, |
1411 Lisp_Object props) | |
1412 { | |
1413 return make_coding_system_1 (existing, prefix, type, description, props); | |
1414 } | |
428 | 1415 |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1416 DEFUN ("make-coding-system-internal", Fmake_coding_system_internal, 2, 4, 0, /* |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1417 See `make-coding-system'. This does much of the work of that function. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1418 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1419 Without Mule support, it does all the work of that function, and an alias |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1420 exists, mapping `make-coding-system' to |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1421 `make-coding-system-internal'. You'll need a non-Mule XEmacs to read the |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1422 complete docstring. Or you can just read it in make-coding-system.el; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1423 something like the following should work: |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1424 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
1425 \\[find-function-other-window] find-file RET \\[find-file] mule/make-coding-system.el RET |
771 | 1426 |
428 | 1427 */ |
771 | 1428 (name, type, description, props)) |
428 | 1429 { |
771 | 1430 return make_coding_system_1 (name, 0, type, description, props); |
428 | 1431 } |
1432 | |
1433 DEFUN ("copy-coding-system", Fcopy_coding_system, 2, 2, 0, /* | |
1434 Copy OLD-CODING-SYSTEM to NEW-NAME. | |
1435 If NEW-NAME does not name an existing coding system, a new one will | |
1436 be created. | |
771 | 1437 If you are using this function to create an alias, think again: |
1438 Use `define-coding-system-alias' instead. | |
428 | 1439 */ |
1440 (old_coding_system, new_name)) | |
1441 { | |
1442 Lisp_Object new_coding_system; | |
1443 old_coding_system = Fget_coding_system (old_coding_system); | |
771 | 1444 new_coding_system = |
4303 | 1445 UNBOUNDP (new_name) ? Qnil : find_coding_system (new_name, 0); |
428 | 1446 if (NILP (new_coding_system)) |
1447 { | |
793 | 1448 new_coding_system = |
1449 wrap_coding_system | |
1450 (allocate_coding_system | |
1451 (XCODING_SYSTEM (old_coding_system)->methods, | |
1452 XCODING_SYSTEM (old_coding_system)->methods->extra_data_size, | |
1453 new_name)); | |
771 | 1454 if (!UNBOUNDP (new_name)) |
1455 Fputhash (new_name, new_coding_system, Vcoding_system_hash_table); | |
428 | 1456 } |
771 | 1457 else if (XCODING_SYSTEM (old_coding_system)->methods != |
1458 XCODING_SYSTEM (new_coding_system)->methods) | |
1459 invalid_operation_2 ("Coding systems not same type", | |
1460 old_coding_system, new_coding_system); | |
428 | 1461 |
1462 { | |
1463 Lisp_Coding_System *to = XCODING_SYSTEM (new_coding_system); | |
1464 Lisp_Coding_System *from = XCODING_SYSTEM (old_coding_system); | |
3017 | 1465 COPY_SIZED_LCRECORD (to, from, sizeof_coding_system (from)); |
428 | 1466 to->name = new_name; |
1467 } | |
1468 return new_coding_system; | |
1469 } | |
1470 | |
2297 | 1471 /* #### Shouldn't this really be a find/get pair? */ |
1472 | |
440 | 1473 DEFUN ("coding-system-alias-p", Fcoding_system_alias_p, 1, 1, 0, /* |
1474 Return t if OBJECT is a coding system alias. | |
1475 All coding system aliases are created by `define-coding-system-alias'. | |
1476 */ | |
1477 (object)) | |
428 | 1478 { |
440 | 1479 return SYMBOLP (Fgethash (object, Vcoding_system_hash_table, Qzero)) |
1480 ? Qt : Qnil; | |
1481 } | |
1482 | |
1483 DEFUN ("coding-system-aliasee", Fcoding_system_aliasee, 1, 1, 0, /* | |
1484 Return the coding-system symbol for which symbol ALIAS is an alias. | |
1485 */ | |
1486 (alias)) | |
1487 { | |
1488 Lisp_Object aliasee = Fgethash (alias, Vcoding_system_hash_table, Qnil); | |
1489 if (SYMBOLP (aliasee)) | |
1490 return aliasee; | |
1491 else | |
563 | 1492 invalid_argument ("Symbol is not a coding system alias", alias); |
1204 | 1493 RETURN_NOT_REACHED (Qnil); |
440 | 1494 } |
1495 | |
1496 /* A maphash function, for removing dangling coding system aliases. */ | |
1497 static int | |
2286 | 1498 dangling_coding_system_alias_p (Lisp_Object UNUSED (alias), |
440 | 1499 Lisp_Object aliasee, |
1500 void *dangling_aliases) | |
1501 { | |
1502 if (SYMBOLP (aliasee) | |
1503 && NILP (Fgethash (aliasee, Vcoding_system_hash_table, Qnil))) | |
428 | 1504 { |
440 | 1505 (*(int *) dangling_aliases)++; |
1506 return 1; | |
428 | 1507 } |
440 | 1508 else |
1509 return 0; | |
1510 } | |
1511 | |
1512 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, 2, 2, 0, /* | |
1513 Define symbol ALIAS as an alias for coding system ALIASEE. | |
1514 | |
1515 You can use this function to redefine an alias that has already been defined, | |
1516 but you cannot redefine a name which is the canonical name for a coding system. | |
1517 \(a canonical name of a coding system is what is returned when you call | |
1518 `coding-system-name' on a coding system). | |
1519 | |
1520 ALIASEE itself can be an alias, which allows you to define nested aliases. | |
1521 | |
1522 You are forbidden, however, from creating alias loops or `dangling' aliases. | |
1523 These will be detected, and an error will be signaled if you attempt to do so. | |
1524 | |
1525 If ALIASEE is nil, then ALIAS will simply be undefined. | |
1526 | |
1527 See also `coding-system-alias-p', `coding-system-aliasee', | |
1528 and `coding-system-canonical-name-p'. | |
1529 */ | |
1530 (alias, aliasee)) | |
1531 { | |
2286 | 1532 Lisp_Object probe; |
440 | 1533 |
1534 CHECK_SYMBOL (alias); | |
1535 | |
1536 if (!NILP (Fcoding_system_canonical_name_p (alias))) | |
563 | 1537 invalid_change |
440 | 1538 ("Symbol is the canonical name of a coding system and cannot be redefined", |
1539 alias); | |
1540 | |
1541 if (NILP (aliasee)) | |
1542 { | |
771 | 1543 Lisp_Object subsidiary_unix = add_suffix_to_symbol (alias, "-unix"); |
1544 Lisp_Object subsidiary_dos = add_suffix_to_symbol (alias, "-dos"); | |
1545 Lisp_Object subsidiary_mac = add_suffix_to_symbol (alias, "-mac"); | |
440 | 1546 |
1547 Fremhash (alias, Vcoding_system_hash_table); | |
1548 | |
1549 /* Undefine subsidiary aliases, | |
1550 presumably created by a previous call to this function */ | |
1551 if (! NILP (Fcoding_system_alias_p (subsidiary_unix)) && | |
1552 ! NILP (Fcoding_system_alias_p (subsidiary_dos)) && | |
1553 ! NILP (Fcoding_system_alias_p (subsidiary_mac))) | |
1554 { | |
1555 Fdefine_coding_system_alias (subsidiary_unix, Qnil); | |
1556 Fdefine_coding_system_alias (subsidiary_dos, Qnil); | |
1557 Fdefine_coding_system_alias (subsidiary_mac, Qnil); | |
1558 } | |
1559 | |
1560 /* Undefine dangling coding system aliases. */ | |
1561 { | |
1562 int dangling_aliases; | |
1563 | |
1564 do { | |
1565 dangling_aliases = 0; | |
1566 elisp_map_remhash (dangling_coding_system_alias_p, | |
1567 Vcoding_system_hash_table, | |
1568 &dangling_aliases); | |
1569 } while (dangling_aliases > 0); | |
1570 } | |
1571 | |
1572 return Qnil; | |
1573 } | |
1574 | |
1575 if (CODING_SYSTEMP (aliasee)) | |
1576 aliasee = XCODING_SYSTEM_NAME (aliasee); | |
1577 | |
1578 /* Checks that aliasee names a coding-system */ | |
2286 | 1579 (void) Fget_coding_system (aliasee); |
440 | 1580 |
1581 /* Check for coding system alias loops */ | |
1582 if (EQ (alias, aliasee)) | |
563 | 1583 alias_loop: invalid_operation_2 |
440 | 1584 ("Attempt to create a coding system alias loop", alias, aliasee); |
1585 | |
1586 for (probe = aliasee; | |
1587 SYMBOLP (probe); | |
1588 probe = Fgethash (probe, Vcoding_system_hash_table, Qzero)) | |
1589 { | |
1590 if (EQ (probe, alias)) | |
1591 goto alias_loop; | |
1592 } | |
1593 | |
1594 Fputhash (alias, aliasee, Vcoding_system_hash_table); | |
1595 | |
1596 /* Set up aliases for subsidiaries. | |
2297 | 1597 #### There must be a better way to handle subsidiary coding systems. |
1598 Inquiring Minds Want To Know: shouldn't they always be chains? */ | |
440 | 1599 { |
1600 static const char *suffixes[] = { "-unix", "-dos", "-mac" }; | |
1601 int i; | |
1602 for (i = 0; i < countof (suffixes); i++) | |
1603 { | |
1604 Lisp_Object alias_subsidiary = | |
771 | 1605 add_suffix_to_symbol (alias, suffixes[i]); |
440 | 1606 Lisp_Object aliasee_subsidiary = |
771 | 1607 add_suffix_to_symbol (aliasee, suffixes[i]); |
440 | 1608 |
1609 if (! NILP (Ffind_coding_system (aliasee_subsidiary))) | |
1610 Fdefine_coding_system_alias (alias_subsidiary, aliasee_subsidiary); | |
1611 } | |
1612 } | |
428 | 1613 /* FSF return value is a vector of [ALIAS-unix ALIAS-dos ALIAS-mac], |
1614 but it doesn't look intentional, so I'd rather return something | |
1615 meaningful or nothing at all. */ | |
1616 return Qnil; | |
1617 } | |
1618 | |
1619 static Lisp_Object | |
771 | 1620 subsidiary_coding_system (Lisp_Object coding_system, enum eol_type type) |
428 | 1621 { |
1622 Lisp_Coding_System *cs = XCODING_SYSTEM (coding_system); | |
1623 Lisp_Object new_coding_system; | |
1624 | |
1625 switch (type) | |
1626 { | |
1627 case EOL_AUTODETECT: return coding_system; | |
1628 case EOL_LF: new_coding_system = CODING_SYSTEM_EOL_LF (cs); break; | |
1629 case EOL_CR: new_coding_system = CODING_SYSTEM_EOL_CR (cs); break; | |
1630 case EOL_CRLF: new_coding_system = CODING_SYSTEM_EOL_CRLF (cs); break; | |
2500 | 1631 default: ABORT (); return Qnil; |
428 | 1632 } |
1633 | |
1634 return NILP (new_coding_system) ? coding_system : new_coding_system; | |
1635 } | |
1636 | |
1637 DEFUN ("subsidiary-coding-system", Fsubsidiary_coding_system, 2, 2, 0, /* | |
1638 Return the subsidiary coding system of CODING-SYSTEM with eol type EOL-TYPE. | |
771 | 1639 The logically opposite operation is `coding-system-base'. |
428 | 1640 */ |
1641 (coding_system, eol_type)) | |
1642 { | |
771 | 1643 coding_system = get_coding_system_for_text_file (coding_system, 0); |
428 | 1644 |
1645 return subsidiary_coding_system (coding_system, | |
1646 symbol_to_eol_type (eol_type)); | |
1647 } | |
1648 | |
771 | 1649 DEFUN ("coding-system-base", Fcoding_system_base, |
1650 1, 1, 0, /* | |
1651 Return the base coding system of CODING-SYSTEM. | |
1652 If CODING-SYSTEM is a subsidiary, this returns its parent; otherwise, it | |
1653 returns CODING-SYSTEM. | |
1654 The logically opposite operation is `subsidiary-coding-system'. | |
1655 */ | |
1656 (coding_system)) | |
1657 { | |
1658 Lisp_Object base; | |
1659 | |
1660 coding_system = Fget_coding_system (coding_system); | |
1661 if (EQ (XCODING_SYSTEM_NAME (coding_system), Qbinary)) | |
1662 return Fget_coding_system (Qraw_text); /* hack! */ | |
1663 base = XCODING_SYSTEM_SUBSIDIARY_PARENT (coding_system); | |
1664 if (!NILP (base)) | |
1665 return base; | |
1666 return coding_system; | |
1667 } | |
1668 | |
1669 DEFUN ("coding-system-used-for-io", Fcoding_system_used_for_io, | |
1670 1, 1, 0, /* | |
1671 Return the coding system actually used for I/O. | |
1672 In some cases (e.g. when a particular EOL type is specified) this won't be | |
2297 | 1673 the coding system itself. This can be useful when trying to determine |
1674 precisely how data was decoded. | |
771 | 1675 */ |
1676 (coding_system)) | |
1677 { | |
1678 Lisp_Object canon; | |
1679 | |
1680 coding_system = Fget_coding_system (coding_system); | |
1681 canon = XCODING_SYSTEM_CANONICAL (coding_system); | |
1682 if (!NILP (canon)) | |
1683 return canon; | |
1684 return coding_system; | |
1685 } | |
1686 | |
428 | 1687 |
1688 /************************************************************************/ | |
1689 /* Coding system accessors */ | |
1690 /************************************************************************/ | |
1691 | |
771 | 1692 DEFUN ("coding-system-description", Fcoding_system_description, 1, 1, 0, /* |
1693 Return the description for CODING-SYSTEM. | |
1694 The `description' of a coding system is a short English phrase giving the | |
1695 name rendered according to English punctuation rules, plus possibly some | |
1696 explanatory text (typically in the form of a parenthetical phrase). The | |
1697 description is intended to be short enough that it can appear as a menu item, | |
1698 and clear enough to be recognizable even to someone who is assumed to have | |
1699 some basic familiarity with different encodings but may not know all the | |
1700 technical names; thus, for `cn-gb-2312' is described as "Chinese EUC" and | |
1701 `hz-gb-2312' is described as "Hz/ZW (Chinese)", where the actual name of | |
1702 the encoding is given, followed by a note that this is a Chinese encoding, | |
1703 because the great majority of people encountering this would have no idea | |
1704 what it is, and giving the language indicates whether the encoding should | |
1705 just be ignored or (conceivably) investigated more thoroughly. | |
428 | 1706 */ |
1707 (coding_system)) | |
1708 { | |
1709 coding_system = Fget_coding_system (coding_system); | |
771 | 1710 return XCODING_SYSTEM_DESCRIPTION (coding_system); |
428 | 1711 } |
1712 | |
1713 DEFUN ("coding-system-type", Fcoding_system_type, 1, 1, 0, /* | |
1714 Return the type of CODING-SYSTEM. | |
1715 */ | |
1716 (coding_system)) | |
1717 { | |
771 | 1718 coding_system = Fget_coding_system (coding_system); |
1719 return XCODING_SYSTEM_TYPE (coding_system); | |
428 | 1720 } |
1721 | |
1722 DEFUN ("coding-system-property", Fcoding_system_property, 2, 2, 0, /* | |
1723 Return the PROP property of CODING-SYSTEM. | |
1724 */ | |
1725 (coding_system, prop)) | |
1726 { | |
1727 coding_system = Fget_coding_system (coding_system); | |
1728 CHECK_SYMBOL (prop); | |
1729 | |
1730 if (EQ (prop, Qname)) | |
1731 return XCODING_SYSTEM_NAME (coding_system); | |
1732 else if (EQ (prop, Qtype)) | |
1733 return Fcoding_system_type (coding_system); | |
771 | 1734 else if (EQ (prop, Qdescription)) |
1735 return XCODING_SYSTEM_DESCRIPTION (coding_system); | |
428 | 1736 else if (EQ (prop, Qmnemonic)) |
1737 return XCODING_SYSTEM_MNEMONIC (coding_system); | |
771 | 1738 else if (EQ (prop, Qdocumentation)) |
1739 return XCODING_SYSTEM_DOCUMENTATION (coding_system); | |
428 | 1740 else if (EQ (prop, Qeol_type)) |
771 | 1741 return eol_type_to_symbol (XCODING_SYSTEM_EOL_TYPE |
1742 (coding_system)); | |
428 | 1743 else if (EQ (prop, Qeol_lf)) |
1744 return XCODING_SYSTEM_EOL_LF (coding_system); | |
1745 else if (EQ (prop, Qeol_crlf)) | |
1746 return XCODING_SYSTEM_EOL_CRLF (coding_system); | |
1747 else if (EQ (prop, Qeol_cr)) | |
1748 return XCODING_SYSTEM_EOL_CR (coding_system); | |
1749 else if (EQ (prop, Qpost_read_conversion)) | |
1750 return XCODING_SYSTEM_POST_READ_CONVERSION (coding_system); | |
1751 else if (EQ (prop, Qpre_write_conversion)) | |
1752 return XCODING_SYSTEM_PRE_WRITE_CONVERSION (coding_system); | |
4568
1d74a1d115ee
Add #'query-coding-region tests; do the work necessary to get them running.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4303
diff
changeset
|
1753 else if (EQ (prop, Qsafe_charsets)) |
1d74a1d115ee
Add #'query-coding-region tests; do the work necessary to get them running.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4303
diff
changeset
|
1754 return XCODING_SYSTEM_SAFE_CHARSETS (coding_system); |
1d74a1d115ee
Add #'query-coding-region tests; do the work necessary to get them running.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4303
diff
changeset
|
1755 else if (EQ (prop, Qsafe_chars)) |
1d74a1d115ee
Add #'query-coding-region tests; do the work necessary to get them running.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4303
diff
changeset
|
1756 return XCODING_SYSTEM_SAFE_CHARS (coding_system); |
771 | 1757 else |
1758 { | |
1759 Lisp_Object value = CODESYSMETH_OR_GIVEN (XCODING_SYSTEM (coding_system), | |
1760 getprop, | |
1761 (coding_system, prop), | |
1762 Qunbound); | |
1763 if (UNBOUNDP (value)) | |
1764 invalid_constant ("Unrecognized property", prop); | |
1765 return value; | |
1766 } | |
1767 } | |
1768 | |
1769 | |
1770 /************************************************************************/ | |
1771 /* Coding stream functions */ | |
1772 /************************************************************************/ | |
1773 | |
1774 /* A coding stream is a stream used for encoding or decoding text. The | |
1775 coding-stream object keeps track of the actual coding system, the stream | |
1776 that is at the other end, and data that needs to be persistent across | |
1777 the lifetime of the stream. */ | |
1778 | |
1204 | 1779 extern const struct sized_memory_description chain_coding_stream_description; |
1780 extern const struct sized_memory_description undecided_coding_stream_description; | |
1781 | |
1782 static const struct memory_description coding_stream_data_description_1 []= { | |
2551 | 1783 { XD_BLOCK_PTR, chain_coding_system, 1, |
1784 { &chain_coding_stream_description } }, | |
1785 { XD_BLOCK_PTR, undecided_coding_system, 1, | |
1786 { &undecided_coding_stream_description } }, | |
1204 | 1787 { XD_END } |
1788 }; | |
1789 | |
1790 static const struct sized_memory_description coding_stream_data_description = { | |
1791 sizeof (void *), coding_stream_data_description_1 | |
1792 }; | |
1793 | |
1794 static const struct memory_description coding_lstream_description[] = { | |
1795 { XD_INT, offsetof (struct coding_stream, type) }, | |
1796 { XD_LISP_OBJECT, offsetof (struct coding_stream, orig_codesys) }, | |
1797 { XD_LISP_OBJECT, offsetof (struct coding_stream, codesys) }, | |
1798 { XD_LISP_OBJECT, offsetof (struct coding_stream, other_end) }, | |
1799 { XD_UNION, offsetof (struct coding_stream, data), | |
2551 | 1800 XD_INDIRECT (0, 0), { &coding_stream_data_description } }, |
1204 | 1801 { XD_END } |
1802 }; | |
1803 | |
1804 DEFINE_LSTREAM_IMPLEMENTATION_WITH_DATA ("coding", coding); | |
771 | 1805 |
1806 /* Encoding and decoding are parallel operations, so we create just one | |
1807 stream for both. "Decoding" may involve the extra step of autodetection | |
1808 of the data format, but that's only because of the conventional | |
1809 definition of decoding as converting from external- to | |
1810 internal-formatted data. | |
1811 | |
2297 | 1812 [[ REWRITE ME! ]] |
1813 | |
771 | 1814 #### We really need to abstract out the concept of "data formats" and |
1815 define "converters" that convert from and to specified formats, | |
1816 eliminating the idea of decoding and encoding. When specifying a | |
1817 conversion process, we need to give the data formats themselves, not the | |
1818 conversion processes -- e.g. a coding system called "Unicode->multibyte" | |
1819 converts in both directions, and we could auto-detect the format of data | |
1820 at either end. */ | |
1821 | |
1822 static Bytecount | |
1823 coding_reader (Lstream *stream, unsigned char *data, Bytecount size) | |
1824 { | |
1825 unsigned char *orig_data = data; | |
1826 Bytecount read_size; | |
1827 int error_occurred = 0; | |
1828 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
1829 | |
1830 /* We need to interface to coding_{de,en}code_1(), which expects to take | |
1831 some amount of data and store the result into a Dynarr. We have | |
1832 coding_{de,en}code_1() store into c->runoff, and take data from there | |
1833 as necessary. */ | |
1834 | |
1835 /* We loop until we have enough data, reading chunks from the other | |
1836 end and converting it. */ | |
1837 while (1) | |
1838 { | |
1839 /* Take data from convert_to if we can. Make sure to take at | |
1840 most SIZE bytes, and delete the data from convert_to. */ | |
1841 if (Dynarr_length (str->convert_to) > 0) | |
1842 { | |
1843 Bytecount chunk = | |
1844 min (size, (Bytecount) Dynarr_length (str->convert_to)); | |
1845 memcpy (data, Dynarr_atp (str->convert_to, 0), chunk); | |
1846 Dynarr_delete_many (str->convert_to, 0, chunk); | |
1847 data += chunk; | |
1848 size -= chunk; | |
1849 } | |
1850 | |
1851 if (size == 0) | |
1852 break; /* No more room for data */ | |
1853 | |
1854 if (str->eof) | |
1855 break; | |
1856 | |
1857 { | |
1858 /* Exhausted convert_to, so get some more. Read into convert_from, | |
1859 after existing "rejected" data from the last conversion. */ | |
1860 Bytecount rejected = Dynarr_length (str->convert_from); | |
1861 /* #### 1024 is arbitrary; we really need to separate 0 from EOF, | |
1862 and when we get 0, keep taking more data until we don't get 0 -- | |
1863 we don't know how much data the conversion routine might need | |
2297 | 1864 before it can generate any data of its own (eg, bzip2). */ |
814 | 1865 Bytecount readmore = |
1866 str->one_byte_at_a_time ? (Bytecount) 1 : | |
1867 max (size, (Bytecount) 1024); | |
771 | 1868 |
1869 Dynarr_add_many (str->convert_from, 0, readmore); | |
1870 read_size = Lstream_read (str->other_end, | |
1871 Dynarr_atp (str->convert_from, rejected), | |
1872 readmore); | |
1873 /* Trim size down to how much we actually got */ | |
1874 Dynarr_set_size (str->convert_from, rejected + max (0, read_size)); | |
1875 } | |
1876 | |
1877 if (read_size < 0) /* LSTREAM_ERROR */ | |
1878 { | |
1879 error_occurred = 1; | |
1880 break; | |
1881 } | |
1882 if (read_size == 0) /* LSTREAM_EOF */ | |
1883 /* There might be some more end data produced in the translation, | |
1884 so we set a flag and call the conversion method once more to | |
1885 output any final stuff it may be holding, any "go back to a sane | |
1886 state" escape sequences, etc. The conversion method is free to | |
1887 look at this flag, and we use it above to stop looping. */ | |
1888 str->eof = 1; | |
1889 { | |
1890 Bytecount processed; | |
1891 Bytecount to_process = Dynarr_length (str->convert_from); | |
1892 | |
1893 /* Convert the data, and save any rejected data in convert_from */ | |
1894 processed = | |
1895 XCODESYSMETH (str->codesys, convert, | |
1896 (str, Dynarr_atp (str->convert_from, 0), | |
1897 str->convert_to, to_process)); | |
1898 if (processed < 0) | |
1899 { | |
1900 error_occurred = 1; | |
1901 break; | |
1902 } | |
1903 assert (processed <= to_process); | |
1904 if (processed < to_process) | |
1905 memmove (Dynarr_atp (str->convert_from, 0), | |
1906 Dynarr_atp (str->convert_from, processed), | |
1907 to_process - processed); | |
1908 Dynarr_set_size (str->convert_from, to_process - processed); | |
1909 } | |
1910 } | |
1911 | |
1912 if (data - orig_data == 0) | |
1913 return error_occurred ? -1 : 0; | |
1914 else | |
1915 return data - orig_data; | |
1916 } | |
1917 | |
1918 static Bytecount | |
1919 coding_writer (Lstream *stream, const unsigned char *data, Bytecount size) | |
1920 { | |
1921 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
1922 | |
1923 /* Convert all our data into convert_to, and then attempt to write | |
1924 it all out to the other end. */ | |
1925 Dynarr_reset (str->convert_to); | |
1926 size = XCODESYSMETH (str->codesys, convert, | |
1927 (str, data, str->convert_to, size)); | |
1928 if (Lstream_write (str->other_end, Dynarr_atp (str->convert_to, 0), | |
1929 Dynarr_length (str->convert_to)) < 0) | |
1930 return -1; | |
1931 else | |
1932 /* The return value indicates how much of the incoming data was | |
1933 processed, not how many bytes were written. */ | |
1934 return size; | |
1935 } | |
1936 | |
1937 static int | |
1938 encode_decode_source_sink_type_is_char (Lisp_Object cs, | |
1939 enum source_or_sink sex, | |
1940 enum encode_decode direction) | |
1941 { | |
1942 return (direction == CODING_DECODE ? | |
1943 decoding_source_sink_type_is_char (cs, sex) : | |
1944 encoding_source_sink_type_is_char (cs, sex)); | |
1945 } | |
1946 | |
1947 /* Ensure that the convert methods only get full characters sent to them to | |
1948 convert if the source of that conversion is characters; and that no such | |
1949 full-character checking happens when the source is bytes. Keep in mind | |
1950 that (1) the conversion_end_type return values take the perspective of | |
1951 encoding; (2) the source for decoding is the same as the sink for | |
1952 encoding; (3) when writing, the data is given to us, and we set our own | |
1953 stream to be character mode or not; (4) when reading, the data comes | |
1954 from the other_end stream, and we set that one to be character mode or | |
1955 not. This is consistent with the comment above the prototype for | |
1956 Lstream_set_character_mode(), which lays out rules for who is allowed to | |
1957 modify the character type mode on a stream. | |
1958 | |
814 | 1959 If we're a read stream, we're always setting character mode on the |
1960 source, but we also set it on ourselves consistent with the flag that | |
1961 can disable this (see again the comment above | |
1962 Lstream_set_character_mode()). | |
1963 */ | |
771 | 1964 |
1965 static void | |
1966 set_coding_character_mode (Lstream *stream) | |
1967 { | |
1968 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
1969 Lstream *stream_to_set = | |
1970 stream->flags & LSTREAM_FL_WRITE ? stream : str->other_end; | |
1971 if (encode_decode_source_sink_type_is_char | |
1972 (str->codesys, CODING_SOURCE, str->direction)) | |
1973 Lstream_set_character_mode (stream_to_set); | |
1974 else | |
1975 Lstream_unset_character_mode (stream_to_set); | |
814 | 1976 if (str->set_char_mode_on_us_when_reading && |
1977 (stream->flags & LSTREAM_FL_READ)) | |
1978 { | |
1979 if (encode_decode_source_sink_type_is_char | |
1980 (str->codesys, CODING_SINK, str->direction)) | |
1981 Lstream_set_character_mode (stream); | |
1982 else | |
1983 Lstream_unset_character_mode (stream); | |
1984 } | |
771 | 1985 } |
1986 | |
1987 static Lisp_Object | |
1988 coding_marker (Lisp_Object stream) | |
1989 { | |
1990 struct coding_stream *str = CODING_STREAM_DATA (XLSTREAM (stream)); | |
1991 | |
1992 mark_object (str->orig_codesys); | |
1993 mark_object (str->codesys); | |
1994 MAYBE_XCODESYSMETH (str->codesys, mark_coding_stream, (str)); | |
1995 return wrap_lstream (str->other_end); | |
1996 } | |
1997 | |
1998 static int | |
1999 coding_rewinder (Lstream *stream) | |
2000 { | |
2001 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
2002 MAYBE_XCODESYSMETH (str->codesys, rewind_coding_stream, (str)); | |
2003 | |
2004 str->ch = 0; | |
2005 Dynarr_reset (str->convert_to); | |
2006 Dynarr_reset (str->convert_from); | |
2007 return Lstream_rewind (str->other_end); | |
2008 } | |
2009 | |
2010 static int | |
2011 coding_seekable_p (Lstream *stream) | |
2012 { | |
2013 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
2014 return Lstream_seekable_p (str->other_end); | |
2015 } | |
2016 | |
2017 static int | |
2018 coding_flusher (Lstream *stream) | |
2019 { | |
2020 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
2021 return Lstream_flush (str->other_end); | |
2022 } | |
2023 | |
2024 static int | |
2025 coding_closer (Lstream *stream) | |
2026 { | |
2027 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
2028 if (stream->flags & LSTREAM_FL_WRITE) | |
2029 { | |
2030 str->eof = 1; | |
2031 coding_writer (stream, 0, 0); | |
2032 str->eof = 0; | |
2033 } | |
2034 /* It's safe to free the runoff dynarrs now because they are used only | |
2035 during conversion. We need to keep the type-specific data around, | |
2036 though, because of canonicalize_after_coding. */ | |
2037 if (str->convert_to) | |
2038 { | |
2039 Dynarr_free (str->convert_to); | |
2040 str->convert_to = 0; | |
2041 } | |
2042 if (str->convert_from) | |
428 | 2043 { |
771 | 2044 Dynarr_free (str->convert_from); |
2045 str->convert_from = 0; | |
2046 } | |
2047 | |
800 | 2048 if (str->no_close_other) |
2049 return Lstream_flush (str->other_end); | |
2050 else | |
2051 return Lstream_close (str->other_end); | |
771 | 2052 } |
2053 | |
2054 static void | |
2055 coding_finalizer (Lstream *stream) | |
2056 { | |
2057 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
2058 | |
2059 assert (!str->finalized); | |
2060 MAYBE_XCODESYSMETH (str->codesys, finalize_coding_stream, (str)); | |
2061 if (str->data) | |
2062 { | |
1726 | 2063 xfree (str->data, void *); |
771 | 2064 str->data = 0; |
2065 } | |
2066 str->finalized = 1; | |
2067 } | |
2068 | |
2069 static Lisp_Object | |
2070 coding_stream_canonicalize_after_coding (Lstream *stream) | |
2071 { | |
2072 struct coding_stream *str = CODING_STREAM_DATA (stream); | |
2073 | |
2074 return XCODESYSMETH_OR_GIVEN (str->codesys, canonicalize_after_coding, | |
2075 (str), str->codesys); | |
2076 } | |
2077 | |
2078 Lisp_Object | |
2079 coding_stream_detected_coding_system (Lstream *stream) | |
2080 { | |
2081 Lisp_Object codesys = | |
2082 coding_stream_canonicalize_after_coding (stream); | |
2083 if (NILP (codesys)) | |
2084 return Fget_coding_system (Qidentity); | |
2085 return codesys; | |
2086 } | |
2087 | |
2088 Lisp_Object | |
2089 coding_stream_coding_system (Lstream *stream) | |
2090 { | |
2091 return CODING_STREAM_DATA (stream)->codesys; | |
2092 } | |
2093 | |
2094 /* Change the coding system associated with a stream. */ | |
2095 | |
2096 void | |
2097 set_coding_stream_coding_system (Lstream *lstr, Lisp_Object codesys) | |
2098 { | |
2099 struct coding_stream *str = CODING_STREAM_DATA (lstr); | |
2100 if (EQ (str->orig_codesys, codesys)) | |
2101 return; | |
2102 /* We do the equivalent of closing the stream, destroying it, and | |
2103 reinitializing it. This includes flushing out the data and signalling | |
2104 EOF, if we're a writing stream; we also replace the type-specific data | |
2105 with the data appropriate for the new coding system. */ | |
2106 if (!NILP (str->codesys)) | |
2107 { | |
2108 if (lstr->flags & LSTREAM_FL_WRITE) | |
2109 { | |
2110 Lstream_flush (lstr); | |
2111 str->eof = 1; | |
2112 coding_writer (lstr, 0, 0); | |
2113 str->eof = 0; | |
2114 } | |
2115 MAYBE_XCODESYSMETH (str->codesys, finalize_coding_stream, (str)); | |
2116 } | |
2117 str->orig_codesys = codesys; | |
2118 str->codesys = coding_system_real_canonical (codesys); | |
2119 | |
2120 if (str->data) | |
2121 { | |
1726 | 2122 xfree (str->data, void *); |
771 | 2123 str->data = 0; |
428 | 2124 } |
771 | 2125 if (XCODING_SYSTEM_METHODS (str->codesys)->coding_data_size) |
1204 | 2126 { |
2127 str->data = | |
2128 xmalloc_and_zero (XCODING_SYSTEM_METHODS (str->codesys)-> | |
2129 coding_data_size); | |
2130 str->type = XCODING_SYSTEM_METHODS (str->codesys)->enumtype; | |
2131 } | |
771 | 2132 MAYBE_XCODESYSMETH (str->codesys, init_coding_stream, (str)); |
2133 /* The new coding system may have different ideas regarding whether its | |
2134 ends are characters or bytes. */ | |
2135 set_coding_character_mode (lstr); | |
2136 } | |
2137 | |
2138 /* WARNING WARNING WARNING WARNING!!!!! If you open up a coding | |
2139 stream for writing, no automatic code detection will be performed. | |
2140 The reason for this is that automatic code detection requires a | |
2141 seekable input. Things will also fail if you open a coding | |
2142 stream for reading using a non-fully-specified coding system and | |
2143 a non-seekable input stream. */ | |
2144 | |
2145 static Lisp_Object | |
2146 make_coding_stream_1 (Lstream *stream, Lisp_Object codesys, | |
800 | 2147 const char *mode, enum encode_decode direction, |
802 | 2148 int flags) |
771 | 2149 { |
2150 Lstream *lstr = Lstream_new (lstream_coding, mode); | |
2151 struct coding_stream *str = CODING_STREAM_DATA (lstr); | |
2152 | |
2153 codesys = Fget_coding_system (codesys); | |
2154 xzero (*str); | |
2155 str->codesys = Qnil; | |
2156 str->orig_codesys = Qnil; | |
2157 str->us = lstr; | |
2158 str->other_end = stream; | |
2159 str->convert_to = Dynarr_new (unsigned_char); | |
2160 str->convert_from = Dynarr_new (unsigned_char); | |
2161 str->direction = direction; | |
814 | 2162 if (flags & LSTREAM_FL_NO_CLOSE_OTHER) |
802 | 2163 str->no_close_other = 1; |
814 | 2164 if (flags & LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME) |
802 | 2165 str->one_byte_at_a_time = 1; |
814 | 2166 if (!(flags & LSTREAM_FL_NO_INIT_CHAR_MODE_WHEN_READING)) |
2167 str->set_char_mode_on_us_when_reading = 1; | |
802 | 2168 |
771 | 2169 set_coding_stream_coding_system (lstr, codesys); |
793 | 2170 return wrap_lstream (lstr); |
771 | 2171 } |
2172 | |
814 | 2173 /* FLAGS: |
2174 | |
2175 LSTREAM_FL_NO_CLOSE_OTHER | |
2176 Don't close STREAM (the stream at the other end) when this stream is | |
2177 closed. | |
2178 | |
2179 LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME | |
2180 When reading from STREAM, read and process one byte at a time rather | |
2181 than in large chunks. This is for reading from TTY's, so we don't | |
2182 block. #### We should instead create a non-blocking filedesc stream | |
2183 that emulates the behavior as necessary using select(), when the | |
2184 fcntls don't work. (As seems to be the case on Cygwin.) | |
2185 | |
2186 LSTREAM_FL_NO_INIT_CHAR_MODE_WHEN_READING | |
2187 When reading from STREAM, read and process one byte at a time rather | |
2188 than in large chunks. This is for reading from TTY's, so we don't | |
2189 block. #### We should instead create a non-blocking filedesc stream | |
2190 that emulates the behavior as necessary using select(), when the | |
2191 fcntls don't work. (As seems to be the case on Cygwin.) | |
2192 */ | |
771 | 2193 Lisp_Object |
2194 make_coding_input_stream (Lstream *stream, Lisp_Object codesys, | |
802 | 2195 enum encode_decode direction, int flags) |
771 | 2196 { |
800 | 2197 return make_coding_stream_1 (stream, codesys, "r", direction, |
802 | 2198 flags); |
771 | 2199 } |
2200 | |
814 | 2201 /* FLAGS: |
2202 | |
2203 LSTREAM_FL_NO_CLOSE_OTHER | |
2204 Don't close STREAM (the stream at the other end) when this stream is | |
2205 closed. | |
2206 */ | |
771 | 2207 Lisp_Object |
2208 make_coding_output_stream (Lstream *stream, Lisp_Object codesys, | |
802 | 2209 enum encode_decode direction, int flags) |
771 | 2210 { |
800 | 2211 return make_coding_stream_1 (stream, codesys, "w", direction, |
802 | 2212 flags); |
771 | 2213 } |
2214 | |
2215 static Lisp_Object | |
2216 encode_decode_coding_region (Lisp_Object start, Lisp_Object end, | |
2217 Lisp_Object coding_system, Lisp_Object buffer, | |
2218 enum encode_decode direction) | |
2219 { | |
2220 Charbpos b, e; | |
2221 struct buffer *buf = decode_buffer (buffer, 0); | |
2222 Lisp_Object instream = Qnil, to_outstream = Qnil, outstream = Qnil; | |
2223 Lisp_Object from_outstream = Qnil, auto_outstream = Qnil; | |
2224 Lisp_Object lb_outstream = Qnil; | |
2225 Lisp_Object next; | |
2226 Lstream *istr, *ostr; | |
2227 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; | |
2228 struct gcpro ngcpro1; | |
2229 int source_char, sink_char; | |
2230 | |
2231 get_buffer_range_char (buf, start, end, &b, &e, 0); | |
2232 barf_if_buffer_read_only (buf, b, e); | |
2233 | |
2234 GCPRO5 (instream, to_outstream, outstream, from_outstream, lb_outstream); | |
2235 NGCPRO1 (auto_outstream); | |
2236 | |
2237 coding_system = Fget_coding_system (coding_system); | |
2238 source_char = encode_decode_source_sink_type_is_char (coding_system, | |
2239 CODING_SOURCE, | |
2240 direction); | |
2241 sink_char = encode_decode_source_sink_type_is_char (coding_system, | |
2242 CODING_SINK, | |
2243 direction); | |
2244 | |
2245 /* Order is IN <---> [TO] -> OUT -> [FROM] -> [AUTODETECT-EOL] -> LB */ | |
2246 instream = make_lisp_buffer_input_stream (buf, b, e, 0); | |
2247 next = lb_outstream = make_lisp_buffer_output_stream (buf, b, 0); | |
2248 | |
2249 if (direction == CODING_DECODE && | |
2250 XCODING_SYSTEM_EOL_TYPE (coding_system) == EOL_AUTODETECT) | |
2251 next = auto_outstream = | |
2252 make_coding_output_stream | |
800 | 2253 (XLSTREAM (next), Fget_coding_system (Qconvert_eol_autodetect), |
2254 CODING_DECODE, 0); | |
771 | 2255 |
2256 if (!sink_char) | |
2257 next = from_outstream = | |
800 | 2258 make_coding_output_stream (XLSTREAM (next), Qbinary, CODING_DECODE, 0); |
771 | 2259 outstream = make_coding_output_stream (XLSTREAM (next), coding_system, |
800 | 2260 direction, 0); |
771 | 2261 if (!source_char) |
428 | 2262 { |
771 | 2263 to_outstream = |
2264 make_coding_output_stream (XLSTREAM (outstream), | |
800 | 2265 Qbinary, CODING_ENCODE, 0); |
771 | 2266 ostr = XLSTREAM (to_outstream); |
2267 } | |
2268 else | |
2269 ostr = XLSTREAM (outstream); | |
2270 istr = XLSTREAM (instream); | |
2271 | |
2272 /* The chain of streams looks like this: | |
2273 | |
2297 | 2274 [BUFFER] <----- (( read from/send to loop )) |
771 | 2275 ------> [CHAR->BYTE i.e. ENCODE AS BINARY if source is |
2276 in bytes] | |
2277 ------> [ENCODE/DECODE AS SPECIFIED] | |
2278 ------> [BYTE->CHAR i.e. DECODE AS BINARY | |
2279 if sink is in bytes] | |
2280 ------> [AUTODETECT EOL if | |
2281 we're decoding and | |
2282 coding system calls | |
2283 for this] | |
2284 ------> [BUFFER] | |
2285 */ | |
2367 | 2286 |
2287 /* #### See comment | |
2288 | |
2289 EFFICIENCY OF CODING CONVERSION WITH MULTIPLE COPIES/CHAINS | |
2290 | |
2291 in text.c. | |
2292 */ | |
2293 | |
771 | 2294 while (1) |
2295 { | |
2296 char tempbuf[1024]; /* some random amount */ | |
2297 Charbpos newpos, even_newer_pos; | |
2298 Charbpos oldpos = lisp_buffer_stream_startpos (istr); | |
2299 Bytecount size_in_bytes = | |
2300 Lstream_read (istr, tempbuf, sizeof (tempbuf)); | |
2301 | |
2302 if (!size_in_bytes) | |
2303 break; | |
2304 newpos = lisp_buffer_stream_startpos (istr); | |
2305 Lstream_write (ostr, tempbuf, size_in_bytes); | |
2306 even_newer_pos = lisp_buffer_stream_startpos (istr); | |
2307 buffer_delete_range (buf, even_newer_pos - (newpos - oldpos), | |
2308 even_newer_pos, 0); | |
428 | 2309 } |
771 | 2310 |
2311 { | |
2312 Charcount retlen = | |
2313 lisp_buffer_stream_startpos (XLSTREAM (instream)) - b; | |
2314 Lstream_close (istr); | |
2315 Lstream_close (ostr); | |
2316 NUNGCPRO; | |
2317 UNGCPRO; | |
2318 Lstream_delete (istr); | |
2319 if (!NILP (from_outstream)) | |
2320 Lstream_delete (XLSTREAM (from_outstream)); | |
2321 Lstream_delete (XLSTREAM (outstream)); | |
2322 if (!NILP (to_outstream)) | |
2323 Lstream_delete (XLSTREAM (to_outstream)); | |
2324 if (!NILP (auto_outstream)) | |
2325 Lstream_delete (XLSTREAM (auto_outstream)); | |
2326 Lstream_delete (XLSTREAM (lb_outstream)); | |
2327 return make_int (retlen); | |
2328 } | |
2329 } | |
2330 | |
3302 | 2331 DEFUN ("decode-coding-region", Fdecode_coding_region, 3, 4, |
2332 "*r\nzDecode from coding system: \ni", /* | |
771 | 2333 Decode the text between START and END which is encoded in CODING-SYSTEM. |
2334 This is useful if you've read in encoded text from a file without decoding | |
2335 it (e.g. you read in a JIS-formatted file but used the `binary' or | |
2336 `no-conversion' coding system, so that it shows up as "^[$B!<!+^[(B"). | |
2337 Return length of decoded text. | |
3302 | 2338 BUFFER defaults to the current buffer if unspecified, and when interactive. |
771 | 2339 */ |
2340 (start, end, coding_system, buffer)) | |
2341 { | |
2342 return encode_decode_coding_region (start, end, coding_system, buffer, | |
2343 CODING_DECODE); | |
2344 } | |
2345 | |
3302 | 2346 DEFUN ("encode-coding-region", Fencode_coding_region, 3, 4, |
2347 "*r\nzEncode to coding system: \ni", /* | |
771 | 2348 Encode the text between START and END using CODING-SYSTEM. |
2349 This will, for example, convert Japanese characters into stuff such as | |
3302 | 2350 "^[$B!<!+^[(B" if you use the JIS encoding. Return length of encoded text. |
2351 BUFFER defaults to the current buffer if unspecified, and when interactive. | |
771 | 2352 */ |
2353 (start, end, coding_system, buffer)) | |
2354 { | |
2355 return encode_decode_coding_region (start, end, coding_system, buffer, | |
2356 CODING_ENCODE); | |
428 | 2357 } |
2358 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2359 DEFUN ("query-coding-region", Fquery_coding_region, 3, 7, 0, /* |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2360 Work out whether CODING-SYSTEM can losslessly encode a region. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2361 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2362 START and END are the beginning and end of the region to check. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2363 CODING-SYSTEM is the coding system to try. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2364 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2365 Optional argument BUFFER is the buffer to check, and defaults to the current |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2366 buffer. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2367 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2368 IGNORE-INVALID-SEQUENCESP, also an optional argument, says to treat XEmacs |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2369 characters which have an unambiguous encoded representation, despite being |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2370 undefined in what they represent, as encodable. These chiefly arise with |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2371 variable-length encodings like UTF-8 and UTF-16, where an invalid sequence |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2372 is passed through to XEmacs as a sequence of characters with a defined |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2373 correspondence to the octets on disk, but no non-error semantics; see the |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2374 `invalid-sequence-coding-system' argument to `set-language-info'. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2375 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2376 They can also arise with fixed-length encodings like ISO 8859-7, where |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2377 certain octets on disk have undefined values, and treating them as |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2378 corresponding to the ISO 8859-1 characters with the same numerical values |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2379 may lead to data that is not understood by other applications. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2380 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2381 Optional argument ERRORP says to signal a `text-conversion-error' if some |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2382 character in the region cannot be encoded, and defaults to nil. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2383 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2384 Optional argument HIGHLIGHT says to display unencodable characters in the |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2385 region using `query-coding-warning-face'. It defaults to nil. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2386 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2387 This function can return multiple values; the intention is that callers use |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2388 `multiple-value-bind' or the related CL multiple value functions to deal |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2389 with it. The first result is `t' if the region can be encoded using |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2390 CODING-SYSTEM, or `nil' if not. If the region cannot be encoded using |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2391 CODING-SYSTEM, the second result is a range table describing the positions |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2392 of the unencodable characters. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2393 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2394 Ranges that describe characters that would be ignored were |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2395 IGNORE-INVALID-SEQUENCESP non-nil map to the symbol `invalid-sequence'; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2396 other ranges map to the symbol `unencodable'. If IGNORE-INVALID-SEQUENCESP |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2397 is non-nil, all ranges will map to the symbol `unencodable'. See |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2398 `make-range-table' for more details of range tables. |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2399 */ |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2400 (start, end, coding_system, buffer, ignore_invalid_sequencesp, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2401 errorp, highlight)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2402 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2403 Charbpos b, e; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2404 struct buffer *buf = decode_buffer (buffer, 1); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2405 Lisp_Object result; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2406 int flags = 0, speccount = specpdl_depth (); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2407 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2408 coding_system = Fget_coding_system (coding_system); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2409 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2410 get_buffer_range_char (buf, start, end, &b, &e, 0); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2411 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2412 if (buf != current_buffer) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2413 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2414 record_unwind_protect (save_current_buffer_restore, Fcurrent_buffer ()); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2415 set_buffer_internal (buf); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2416 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2417 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2418 record_unwind_protect (save_excursion_restore, save_excursion_save ()); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2419 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2420 BUF_SET_PT (buf, b); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2421 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2422 if (!NILP (ignore_invalid_sequencesp)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2423 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2424 flags |= QUERY_METHOD_IGNORE_INVALID_SEQUENCES; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2425 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2426 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2427 if (!NILP (errorp)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2428 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2429 flags |= QUERY_METHOD_ERRORP; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2430 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2431 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2432 if (!NILP (highlight)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2433 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2434 flags |= QUERY_METHOD_HIGHLIGHT; |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2435 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2436 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2437 result = XCODESYSMETH_OR_GIVEN (coding_system, query, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2438 (coding_system, buf, e, flags), Qunbound); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2439 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2440 if (UNBOUNDP (result)) |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2441 { |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2442 signal_error (Qtext_conversion_error, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2443 "Coding system doesn't say what it can encode", |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2444 XCODING_SYSTEM_NAME (coding_system)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2445 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2446 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2447 result = (NILP (result)) ? Qt : values2 (Qnil, result); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2448 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2449 return unbind_to_1 (speccount, result); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2450 } |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2451 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
2452 |
428 | 2453 |
2454 /************************************************************************/ | |
771 | 2455 /* Chain methods */ |
428 | 2456 /************************************************************************/ |
2457 | |
771 | 2458 /* #### Need a way to create "opposite-direction" coding systems. */ |
2459 | |
2460 /* Chain two or more coding systems together to make a combination coding | |
2461 system. */ | |
2462 | |
2463 struct chain_coding_system | |
2464 { | |
2465 /* List of coding systems, in decode order */ | |
2466 Lisp_Object *chain; | |
2467 /* Number of coding systems in list */ | |
2468 int count; | |
2469 /* Coding system to return as a result of canonicalize-after-coding */ | |
2470 Lisp_Object canonicalize_after_coding; | |
2471 }; | |
2472 | |
2473 struct chain_coding_stream | |
2474 { | |
2475 int initted; | |
2476 /* Lstreams for chain coding system */ | |
2477 Lisp_Object *lstreams; | |
2478 int lstream_count; | |
2479 }; | |
2480 | |
1204 | 2481 static const struct memory_description chain_coding_system_description[] = { |
2482 { XD_INT, offsetof (struct chain_coding_system, count) }, | |
2367 | 2483 { XD_BLOCK_PTR, offsetof (struct chain_coding_system, chain), |
2551 | 2484 XD_INDIRECT (0, 0), { &lisp_object_description } }, |
1204 | 2485 { XD_LISP_OBJECT, offsetof (struct chain_coding_system, |
2486 canonicalize_after_coding) }, | |
771 | 2487 { XD_END } |
2488 }; | |
2489 | |
1204 | 2490 static const struct memory_description chain_coding_stream_description_1 [] = { |
2491 { XD_INT, offsetof (struct chain_coding_stream, lstream_count) }, | |
2367 | 2492 { XD_BLOCK_PTR, offsetof (struct chain_coding_stream, lstreams), |
2551 | 2493 XD_INDIRECT (0, 0), { &lisp_object_description } }, |
771 | 2494 { XD_END } |
2495 }; | |
2496 | |
1204 | 2497 const struct sized_memory_description chain_coding_stream_description = { |
2498 sizeof (struct chain_coding_stream), chain_coding_stream_description_1 | |
2499 }; | |
2500 | |
2501 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (chain); | |
2502 | |
771 | 2503 static Lisp_Object |
2504 chain_canonicalize (Lisp_Object codesys) | |
2505 { | |
2506 /* We make use of the fact that this method is called at init time, after | |
2507 properties have been parsed. init_method is called too early. */ | |
2508 /* #### It's not clear we need this whole chain-canonicalize mechanism | |
2509 any more. */ | |
2510 Lisp_Object chain = Flist (XCODING_SYSTEM_CHAIN_COUNT (codesys), | |
2511 XCODING_SYSTEM_CHAIN_CHAIN (codesys)); | |
2512 chain = Fcons (XCODING_SYSTEM_PRE_WRITE_CONVERSION (codesys), | |
2513 Fcons (XCODING_SYSTEM_POST_READ_CONVERSION (codesys), | |
2514 chain)); | |
2515 Fputhash (chain, codesys, Vchain_canonicalize_hash_table); | |
2516 return codesys; | |
2517 } | |
2518 | |
2519 static Lisp_Object | |
2520 chain_canonicalize_after_coding (struct coding_stream *str) | |
2521 { | |
2522 Lisp_Object cac = | |
2523 XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (str->codesys); | |
2524 if (!NILP (cac)) | |
2525 return cac; | |
2526 return str->codesys; | |
2527 #if 0 | |
2528 struct chain_coding_stream *data = CODING_STREAM_TYPE_DATA (str, chain); | |
2529 Lisp_Object us = str->codesys, codesys; | |
2530 int i; | |
2531 Lisp_Object chain; | |
2532 Lisp_Object tail; | |
2533 int changed = 0; | |
2534 | |
2535 /* #### It's not clear we need this whole chain-canonicalize mechanism | |
2536 any more. */ | |
2537 if (str->direction == CODING_ENCODE || !data->initted) | |
2538 return us; | |
2539 | |
2540 chain = Flist (XCODING_SYSTEM_CHAIN_COUNT (us), | |
2541 XCODING_SYSTEM_CHAIN_CHAIN (us)); | |
2542 | |
2543 tail = chain; | |
2544 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (us); i++) | |
2545 { | |
2546 codesys = (coding_stream_canonicalize_after_coding | |
2547 (XLSTREAM (data->lstreams[i]))); | |
2548 if (!EQ (codesys, XCAR (tail))) | |
2549 changed = 1; | |
2550 XCAR (tail) = codesys; | |
2551 tail = XCDR (tail); | |
2552 } | |
2553 | |
2554 if (!changed) | |
2555 return us; | |
2556 | |
2557 chain = delq_no_quit (Qnil, chain); | |
2558 | |
2559 if (NILP (XCODING_SYSTEM_PRE_WRITE_CONVERSION (us)) && | |
2560 NILP (XCODING_SYSTEM_POST_READ_CONVERSION (us))) | |
2561 { | |
2562 if (NILP (chain)) | |
2563 return Qnil; | |
2564 if (NILP (XCDR (chain))) | |
2565 return XCAR (chain); | |
2566 } | |
2567 | |
2568 codesys = Fgethash (Fcons (XCODING_SYSTEM_PRE_WRITE_CONVERSION (us), | |
2569 Fcons (XCODING_SYSTEM_POST_READ_CONVERSION (us), | |
2570 chain)), Vchain_canonicalize_hash_table, | |
2571 Qnil); | |
2572 if (!NILP (codesys)) | |
2573 return codesys; | |
2574 return make_internal_coding_system | |
2575 (us, "internal-chain-canonicalizer-wrapper", | |
2576 Qchain, Qunbound, list2 (Qchain, chain)); | |
2577 #endif /* 0 */ | |
2578 } | |
2579 | |
2580 static void | |
2581 chain_init (Lisp_Object codesys) | |
2582 { | |
2583 XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (codesys) = Qnil; | |
2584 } | |
2585 | |
2586 static void | |
2587 chain_mark (Lisp_Object codesys) | |
2588 { | |
2589 int i; | |
2590 | |
2591 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (codesys); i++) | |
2592 mark_object (XCODING_SYSTEM_CHAIN_CHAIN (codesys)[i]); | |
2593 mark_object (XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (codesys)); | |
2594 } | |
2595 | |
2596 static void | |
2597 chain_mark_coding_stream_1 (struct chain_coding_stream *data) | |
2598 { | |
2599 int i; | |
2600 | |
2601 for (i = 0; i < data->lstream_count; i++) | |
2602 mark_object (data->lstreams[i]); | |
2603 } | |
2604 | |
2605 static void | |
2606 chain_mark_coding_stream (struct coding_stream *str) | |
2607 { | |
2608 chain_mark_coding_stream_1 (CODING_STREAM_TYPE_DATA (str, chain)); | |
2609 } | |
2610 | |
2611 static void | |
2612 chain_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag) | |
2613 { | |
2614 int i; | |
2615 | |
826 | 2616 write_c_string (printcharfun, "("); |
771 | 2617 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (cs); i++) |
2618 { | |
826 | 2619 write_c_string (printcharfun, i == 0 ? "" : "->"); |
771 | 2620 print_coding_system_in_print_method (XCODING_SYSTEM_CHAIN_CHAIN (cs)[i], |
2621 printcharfun, escapeflag); | |
2622 } | |
2623 { | |
2624 Lisp_Object cac = XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (cs); | |
2625 if (!NILP (cac)) | |
2626 { | |
2627 if (i > 0) | |
826 | 2628 write_c_string (printcharfun, " "); |
2629 write_c_string (printcharfun, "canonicalize-after-coding="); | |
771 | 2630 print_coding_system_in_print_method (cac, printcharfun, escapeflag); |
2631 } | |
2632 } | |
2633 | |
826 | 2634 write_c_string (printcharfun, ")"); |
771 | 2635 } |
2636 | |
2637 static void | |
2638 chain_rewind_coding_stream_1 (struct chain_coding_stream *data) | |
2639 { | |
2640 /* Each will rewind the next; there is always at least one stream (the | |
2641 dynarr stream at the end) if we're initted */ | |
2642 if (data->initted) | |
2643 Lstream_rewind (XLSTREAM (data->lstreams[0])); | |
2644 } | |
2645 | |
2646 static void | |
2647 chain_rewind_coding_stream (struct coding_stream *str) | |
2648 { | |
2649 chain_rewind_coding_stream_1 (CODING_STREAM_TYPE_DATA (str, chain)); | |
2650 } | |
2651 | |
2652 static void | |
2653 chain_init_coding_streams_1 (struct chain_coding_stream *data, | |
2654 unsigned_char_dynarr *dst, | |
2655 int ncodesys, Lisp_Object *codesys, | |
2656 enum encode_decode direction) | |
2657 { | |
2658 int i; | |
2659 Lisp_Object lstream_out; | |
2660 | |
2661 data->lstream_count = ncodesys + 1; | |
2662 data->lstreams = xnew_array (Lisp_Object, data->lstream_count); | |
2663 | |
2664 lstream_out = make_dynarr_output_stream (dst); | |
2665 Lstream_set_buffering (XLSTREAM (lstream_out), LSTREAM_UNBUFFERED, 0); | |
2666 data->lstreams[data->lstream_count - 1] = lstream_out; | |
2667 | |
2668 for (i = ncodesys - 1; i >= 0; i--) | |
2669 { | |
2670 data->lstreams[i] = | |
2671 make_coding_output_stream | |
2672 (XLSTREAM (lstream_out), | |
2673 codesys[direction == CODING_ENCODE ? ncodesys - (i + 1) : i], | |
800 | 2674 direction, 0); |
771 | 2675 lstream_out = data->lstreams[i]; |
2676 Lstream_set_buffering (XLSTREAM (lstream_out), LSTREAM_UNBUFFERED, | |
2677 0); | |
2678 } | |
2679 data->initted = 1; | |
2680 } | |
2681 | |
2682 static Bytecount | |
2683 chain_convert (struct coding_stream *str, const UExtbyte *src, | |
2684 unsigned_char_dynarr *dst, Bytecount n) | |
2685 { | |
2686 struct chain_coding_stream *data = CODING_STREAM_TYPE_DATA (str, chain); | |
2687 | |
2688 if (str->eof) | |
2689 { | |
2690 /* Each will close the next; there is always at least one stream (the | |
2691 dynarr stream at the end) if we're initted. We need to close now | |
2692 because more data may be generated. */ | |
2693 if (data->initted) | |
2694 Lstream_close (XLSTREAM (data->lstreams[0])); | |
2695 return n; | |
2696 } | |
2697 | |
2698 if (!data->initted) | |
2699 chain_init_coding_streams_1 | |
2700 (data, dst, XCODING_SYSTEM_CHAIN_COUNT (str->codesys), | |
2701 XCODING_SYSTEM_CHAIN_CHAIN (str->codesys), str->direction); | |
2702 | |
2703 if (Lstream_write (XLSTREAM (data->lstreams[0]), src, n) < 0) | |
2704 return -1; | |
2705 return n; | |
2706 } | |
2707 | |
2708 static void | |
2709 chain_finalize_coding_stream_1 (struct chain_coding_stream *data) | |
2710 { | |
2711 if (data->lstreams) | |
2712 { | |
2297 | 2713 /* During GC, these objects are unmarked, and are about to be freed. |
2714 We do NOT want them on the free list, and that will cause lots of | |
2715 nastiness including crashes. Just let them be freed normally. */ | |
771 | 2716 if (!gc_in_progress) |
2717 { | |
2718 int i; | |
2297 | 2719 /* Order of deletion is important here! Delete from the head of |
2720 the chain and work your way towards the tail. In general, | |
2721 when you delete an object, there should be *NO* pointers to it | |
2722 anywhere. Deleting back-to-front would be a problem because | |
2723 there are pointers going forward. If there were pointers in | |
2724 both directions, you'd have to disconnect the pointers to a | |
2725 particular object before deleting it. */ | |
771 | 2726 for (i = 0; i < data->lstream_count; i++) |
2727 Lstream_delete (XLSTREAM ((data->lstreams)[i])); | |
2728 } | |
1726 | 2729 xfree (data->lstreams, Lisp_Object *); |
771 | 2730 } |
2731 } | |
2732 | |
2733 static void | |
2734 chain_finalize_coding_stream (struct coding_stream *str) | |
2735 { | |
2736 chain_finalize_coding_stream_1 (CODING_STREAM_TYPE_DATA (str, chain)); | |
2737 } | |
2738 | |
2739 static void | |
2740 chain_finalize (Lisp_Object c) | |
2741 { | |
2742 if (XCODING_SYSTEM_CHAIN_CHAIN (c)) | |
1726 | 2743 xfree (XCODING_SYSTEM_CHAIN_CHAIN (c), Lisp_Object *); |
771 | 2744 } |
2745 | |
428 | 2746 static int |
771 | 2747 chain_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value) |
2748 { | |
2749 if (EQ (key, Qchain)) | |
2750 { | |
2751 Lisp_Object *cslist; | |
2752 int count = 0; | |
2753 int i; | |
2754 | |
2367 | 2755 { |
2756 EXTERNAL_LIST_LOOP_2 (elt, value) | |
2757 { | |
2758 Fget_coding_system (elt); | |
2759 count++; | |
2760 } | |
2761 } | |
771 | 2762 |
2763 cslist = xnew_array (Lisp_Object, count); | |
2764 XCODING_SYSTEM_CHAIN_CHAIN (codesys) = cslist; | |
2765 | |
2766 count = 0; | |
2367 | 2767 { |
2768 EXTERNAL_LIST_LOOP_2 (elt, value) | |
2769 { | |
2770 cslist[count] = Fget_coding_system (elt); | |
2771 count++; | |
2772 } | |
2773 } | |
771 | 2774 |
2775 XCODING_SYSTEM_CHAIN_COUNT (codesys) = count; | |
2776 | |
2777 for (i = 0; i < count - 1; i++) | |
2778 { | |
2779 if (decoding_source_sink_type_is_char (cslist[i], CODING_SINK) != | |
2780 decoding_source_sink_type_is_char (cslist[i + 1], CODING_SOURCE)) | |
2781 invalid_argument_2 ("Sink of first must match source of second", | |
2782 cslist[i], cslist[i + 1]); | |
2783 } | |
2784 } | |
2785 else if (EQ (key, Qcanonicalize_after_coding)) | |
2786 XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (codesys) = | |
2787 Fget_coding_system (value); | |
2788 else | |
2789 return 0; | |
2790 return 1; | |
2791 } | |
2792 | |
2793 static Lisp_Object | |
2794 chain_getprop (Lisp_Object coding_system, Lisp_Object prop) | |
2795 { | |
2796 if (EQ (prop, Qchain)) | |
2797 { | |
2798 Lisp_Object result = Qnil; | |
2799 int i; | |
2800 | |
2801 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (coding_system); i++) | |
2802 result = Fcons (XCODING_SYSTEM_CHAIN_CHAIN (coding_system)[i], | |
2803 result); | |
2804 | |
2805 return Fnreverse (result); | |
2806 } | |
2807 else if (EQ (prop, Qcanonicalize_after_coding)) | |
2808 return XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (coding_system); | |
2809 else | |
2810 return Qunbound; | |
2811 } | |
2812 | |
2813 static enum source_sink_type | |
2814 chain_conversion_end_type (Lisp_Object codesys) | |
2815 { | |
2816 Lisp_Object *cslist = XCODING_SYSTEM_CHAIN_CHAIN (codesys); | |
2817 int n = XCODING_SYSTEM_CHAIN_COUNT (codesys); | |
2818 int charp_source, charp_sink; | |
2819 | |
2820 if (n == 0) | |
2821 return DECODES_BYTE_TO_BYTE; /* arbitrary */ | |
2822 charp_source = decoding_source_sink_type_is_char (cslist[0], CODING_SOURCE); | |
2823 charp_sink = decoding_source_sink_type_is_char (cslist[n - 1], CODING_SINK); | |
2824 | |
2825 switch (charp_source * 2 + charp_sink) | |
2826 { | |
2827 case 0: return DECODES_BYTE_TO_BYTE; | |
2828 case 1: return DECODES_BYTE_TO_CHARACTER; | |
2829 case 2: return DECODES_CHARACTER_TO_BYTE; | |
2830 case 3: return DECODES_CHARACTER_TO_CHARACTER; | |
2831 } | |
2832 | |
2500 | 2833 ABORT (); |
771 | 2834 return DECODES_BYTE_TO_BYTE; |
2835 } | |
2836 | |
2837 | |
2838 /************************************************************************/ | |
2839 /* No-conversion methods */ | |
2840 /************************************************************************/ | |
2841 | |
2842 /* "No conversion"; used for binary files. We use quotes because there | |
2843 really is some conversion being applied (it does byte<->char | |
2844 conversion), but it appears to the user as if the text is read in | |
2297 | 2845 without conversion. |
2846 | |
2847 #### Shouldn't we _call_ it that, then? And while we're at it, | |
2848 separate it into "to_internal" and "to_external"? */ | |
771 | 2849 DEFINE_CODING_SYSTEM_TYPE (no_conversion); |
2850 | |
2851 /* This is used when reading in "binary" files -- i.e. files that may | |
2852 contain all 256 possible byte values and that are not to be | |
2853 interpreted as being in any particular encoding. */ | |
2854 static Bytecount | |
2855 no_conversion_convert (struct coding_stream *str, | |
2856 const UExtbyte *src, | |
2857 unsigned_char_dynarr *dst, Bytecount n) | |
2858 { | |
2859 UExtbyte c; | |
2860 unsigned int ch = str->ch; | |
2861 Bytecount orign = n; | |
2862 | |
2863 if (str->direction == CODING_DECODE) | |
2864 { | |
2865 while (n--) | |
2866 { | |
2867 c = *src++; | |
2868 | |
2869 DECODE_ADD_BINARY_CHAR (c, dst); | |
2870 } | |
2871 | |
2872 if (str->eof) | |
2873 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst); | |
2874 } | |
2875 else | |
2876 { | |
2877 | |
2878 while (n--) | |
2879 { | |
2880 c = *src++; | |
826 | 2881 if (byte_ascii_p (c)) |
771 | 2882 { |
2883 assert (ch == 0); | |
2884 Dynarr_add (dst, c); | |
2885 } | |
2886 #ifdef MULE | |
867 | 2887 else if (ibyte_leading_byte_p (c)) |
771 | 2888 { |
2889 assert (ch == 0); | |
2890 if (c == LEADING_BYTE_LATIN_ISO8859_1 || | |
2891 c == LEADING_BYTE_CONTROL_1) | |
2892 ch = c; | |
2893 else | |
2297 | 2894 /* #### This is just plain unacceptable. */ |
771 | 2895 Dynarr_add (dst, '~'); /* untranslatable character */ |
2896 } | |
2897 else | |
2898 { | |
2899 if (ch == LEADING_BYTE_LATIN_ISO8859_1) | |
2900 Dynarr_add (dst, c); | |
2901 else if (ch == LEADING_BYTE_CONTROL_1) | |
2902 { | |
2903 assert (c < 0xC0); | |
2904 Dynarr_add (dst, c - 0x20); | |
2905 } | |
2906 /* else it should be the second or third byte of an | |
2907 untranslatable character, so ignore it */ | |
2908 ch = 0; | |
2909 } | |
2910 #endif /* MULE */ | |
2911 | |
2912 } | |
2913 } | |
2914 | |
2915 str->ch = ch; | |
2916 return orign; | |
2917 } | |
2918 | |
2919 DEFINE_DETECTOR (no_conversion); | |
2920 DEFINE_DETECTOR_CATEGORY (no_conversion, no_conversion); | |
2921 | |
2922 struct no_conversion_detector | |
2923 { | |
2924 int dummy; | |
2925 }; | |
2926 | |
2927 static void | |
2286 | 2928 no_conversion_detect (struct detection_state *st, const UExtbyte *UNUSED (src), |
2929 Bytecount UNUSED (n)) | |
771 | 2930 { |
2931 /* Hack until we get better handling of this stuff! */ | |
2932 DET_RESULT (st, no_conversion) = DET_SLIGHTLY_LIKELY; | |
2933 } | |
2934 | |
2935 | |
2936 /************************************************************************/ | |
2937 /* Convert-eol methods */ | |
2938 /************************************************************************/ | |
2939 | |
2940 /* This is used to handle end-of-line (EOL) differences. It is | |
2819 | 2941 character-to-character, and works (when encoding) *BEFORE* sending data to |
2942 the main encoding routine -- thus, that routine must handle different EOL | |
2943 types itself if it does line-oriented type processing. This is unavoidable | |
2944 because we don't know whether the output of the main encoding routine is | |
2945 ASCII compatible (UTF-16 is definitely not, for example). [[ sjt sez this | |
2946 is bogus. There should be _no_ EOL processing (or processing of any kind) | |
2947 after conversion to external. ]] | |
771 | 2948 |
793 | 2949 There is one parameter: `subtype', either `cr', `lf', `crlf', or nil. |
771 | 2950 */ |
2951 | |
2952 struct convert_eol_coding_system | |
2953 { | |
2954 enum eol_type subtype; | |
2132 | 2955 int dummy; /* On some architectures (eg ia64) the portable dumper can |
2956 produce unaligned access errors without this field. Probably | |
2957 because the combined structure of this structure and | |
2958 Lisp_Coding_System is not properly aligned. */ | |
771 | 2959 }; |
2960 | |
2961 #define CODING_SYSTEM_CONVERT_EOL_SUBTYPE(codesys) \ | |
2962 (CODING_SYSTEM_TYPE_DATA (codesys, convert_eol)->subtype) | |
2963 #define XCODING_SYSTEM_CONVERT_EOL_SUBTYPE(codesys) \ | |
2964 (XCODING_SYSTEM_TYPE_DATA (codesys, convert_eol)->subtype) | |
2965 | |
2966 struct convert_eol_coding_stream | |
2967 { | |
2968 enum eol_type actual; | |
2969 }; | |
2970 | |
1204 | 2971 static const struct memory_description |
771 | 2972 convert_eol_coding_system_description[] = { |
2973 { XD_END } | |
2974 }; | |
2975 | |
1204 | 2976 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (convert_eol); |
2977 | |
771 | 2978 static void |
2286 | 2979 convert_eol_print (Lisp_Object cs, Lisp_Object printcharfun, |
2980 int UNUSED (escapeflag)) | |
771 | 2981 { |
2982 struct convert_eol_coding_system *data = | |
2983 XCODING_SYSTEM_TYPE_DATA (cs, convert_eol); | |
2984 | |
2985 write_fmt_string (printcharfun, "(%s)", | |
2986 data->subtype == EOL_LF ? "lf" : | |
2987 data->subtype == EOL_CRLF ? "crlf" : | |
2988 data->subtype == EOL_CR ? "cr" : | |
793 | 2989 data->subtype == EOL_AUTODETECT ? "nil" : |
2500 | 2990 (ABORT(), "")); |
771 | 2991 } |
2992 | |
2993 static enum source_sink_type | |
2286 | 2994 convert_eol_conversion_end_type (Lisp_Object UNUSED (codesys)) |
771 | 2995 { |
2996 return DECODES_CHARACTER_TO_CHARACTER; | |
2997 } | |
2998 | |
2999 static int | |
3000 convert_eol_putprop (Lisp_Object codesys, | |
3001 Lisp_Object key, | |
3002 Lisp_Object value) | |
3003 { | |
3004 struct convert_eol_coding_system *data = | |
3005 XCODING_SYSTEM_TYPE_DATA (codesys, convert_eol); | |
3006 | |
3007 if (EQ (key, Qsubtype)) | |
3008 { | |
3009 if (EQ (value, Qlf) /* || EQ (value, Qunix) */) | |
3010 data->subtype = EOL_LF; | |
3011 else if (EQ (value, Qcrlf) /* || EQ (value, Qdos) */) | |
3012 data->subtype = EOL_CRLF; | |
3013 else if (EQ (value, Qcr) /* || EQ (value, Qmac) */) | |
3014 data->subtype = EOL_CR; | |
793 | 3015 else if (EQ (value, Qnil)) |
771 | 3016 data->subtype = EOL_AUTODETECT; |
3017 else invalid_constant ("Unrecognized eol type", value); | |
3018 } | |
3019 else | |
3020 return 0; | |
3021 return 1; | |
3022 } | |
3023 | |
3024 static Lisp_Object | |
3025 convert_eol_getprop (Lisp_Object coding_system, Lisp_Object prop) | |
3026 { | |
3027 struct convert_eol_coding_system *data = | |
3028 XCODING_SYSTEM_TYPE_DATA (coding_system, convert_eol); | |
3029 | |
3030 if (EQ (prop, Qsubtype)) | |
3031 { | |
3032 switch (data->subtype) | |
3033 { | |
3034 case EOL_LF: return Qlf; | |
3035 case EOL_CRLF: return Qcrlf; | |
3036 case EOL_CR: return Qcr; | |
793 | 3037 case EOL_AUTODETECT: return Qnil; |
2500 | 3038 default: ABORT (); |
771 | 3039 } |
3040 } | |
3041 | |
3042 return Qunbound; | |
3043 } | |
3044 | |
3045 static void | |
3046 convert_eol_init_coding_stream (struct coding_stream *str) | |
3047 { | |
3048 struct convert_eol_coding_stream *data = | |
3049 CODING_STREAM_TYPE_DATA (str, convert_eol); | |
3050 data->actual = XCODING_SYSTEM_CONVERT_EOL_SUBTYPE (str->codesys); | |
3051 } | |
3052 | |
3053 static Bytecount | |
867 | 3054 convert_eol_convert (struct coding_stream *str, const Ibyte *src, |
771 | 3055 unsigned_char_dynarr *dst, Bytecount n) |
3056 { | |
3057 if (str->direction == CODING_DECODE) | |
3058 { | |
3059 struct convert_eol_coding_stream *data = | |
3060 CODING_STREAM_TYPE_DATA (str, convert_eol); | |
3061 | |
3062 if (data->actual == EOL_AUTODETECT) | |
3063 { | |
3064 Bytecount n2 = n; | |
867 | 3065 const Ibyte *src2 = src; |
771 | 3066 |
3067 for (; n2; n2--) | |
3068 { | |
867 | 3069 Ibyte c = *src2++; |
771 | 3070 if (c == '\n') |
3071 { | |
3072 data->actual = EOL_LF; | |
3073 break; | |
3074 } | |
3075 else if (c == '\r') | |
3076 { | |
3077 if (n2 == 1) | |
3078 { | |
3079 /* If we're seeing a '\r' at the end of the data, then | |
3080 reject the '\r' right now so it doesn't become an | |
3081 issue in the code below -- unless we're at the end of | |
3082 the stream, in which case we can't do that (because | |
3083 then the '\r' will never get written out), and in any | |
3084 case we should be recognizing it at EOL_CR format. */ | |
3085 if (str->eof) | |
3086 data->actual = EOL_CR; | |
3087 else | |
3088 n--; | |
3089 break; | |
3090 } | |
3091 else if (*src2 == '\n') | |
3092 data->actual = EOL_CRLF; | |
3093 else | |
3094 data->actual = EOL_CR; | |
3095 break; | |
3096 } | |
3097 } | |
3098 } | |
3099 | |
3100 /* str->eof is set, the caller reached EOF on the other end and has | |
3101 no new data to give us. The only data we get is the data we | |
3102 rejected from last time. */ | |
3103 if (data->actual == EOL_LF || data->actual == EOL_AUTODETECT || | |
3104 (str->eof)) | |
3105 Dynarr_add_many (dst, src, n); | |
3106 else | |
3107 { | |
867 | 3108 const Ibyte *end = src + n; |
771 | 3109 while (1) |
3110 { | |
3111 /* Find the next section with no \r and add it. */ | |
867 | 3112 const Ibyte *runstart = src; |
3113 src = (Ibyte *) memchr (src, '\r', end - src); | |
771 | 3114 if (!src) |
3115 src = end; | |
3116 Dynarr_add_many (dst, runstart, src - runstart); | |
3117 /* Stop if at end ... */ | |
3118 if (src == end) | |
3119 break; | |
3120 /* ... else, translate as necessary. */ | |
3121 src++; | |
3122 if (data->actual == EOL_CR) | |
3123 Dynarr_add (dst, '\n'); | |
3124 /* We need to be careful here with CRLF. If we see a CR at the | |
3125 end of the data, we don't know if it's part of a CRLF, so we | |
3126 reject it. Otherwise: If it's part of a CRLF, eat it and | |
3127 loop; the following LF gets added next time around. If it's | |
3128 not part of a CRLF, add the CR and loop. The following | |
3129 character will be processed in the next loop iteration. This | |
3130 correctly handles a sequence like CR+CR+LF. */ | |
3131 else if (src == end) | |
3132 return n - 1; /* reject the CR at the end; we'll get it again | |
3133 next time the convert method is called */ | |
3134 else if (*src != '\n') | |
3135 Dynarr_add (dst, '\r'); | |
3136 } | |
3137 } | |
3138 | |
3139 return n; | |
3140 } | |
3141 else | |
3142 { | |
3143 enum eol_type subtype = | |
3144 XCODING_SYSTEM_CONVERT_EOL_SUBTYPE (str->codesys); | |
867 | 3145 const Ibyte *end = src + n; |
771 | 3146 |
3147 /* We try to be relatively efficient here. */ | |
3148 if (subtype == EOL_LF) | |
3149 Dynarr_add_many (dst, src, n); | |
3150 else | |
3151 { | |
3152 while (1) | |
3153 { | |
3154 /* Find the next section with no \n and add it. */ | |
867 | 3155 const Ibyte *runstart = src; |
3156 src = (Ibyte *) memchr (src, '\n', end - src); | |
771 | 3157 if (!src) |
3158 src = end; | |
3159 Dynarr_add_many (dst, runstart, src - runstart); | |
3160 /* Stop if at end ... */ | |
3161 if (src == end) | |
3162 break; | |
3163 /* ... else, skip over \n and add its translation. */ | |
3164 src++; | |
3165 Dynarr_add (dst, '\r'); | |
3166 if (subtype == EOL_CRLF) | |
3167 Dynarr_add (dst, '\n'); | |
3168 } | |
3169 } | |
3170 | |
3171 return n; | |
3172 } | |
3173 } | |
3174 | |
3175 static Lisp_Object | |
3176 convert_eol_canonicalize_after_coding (struct coding_stream *str) | |
3177 { | |
3178 struct convert_eol_coding_stream *data = | |
3179 CODING_STREAM_TYPE_DATA (str, convert_eol); | |
3180 | |
3181 if (str->direction == CODING_ENCODE) | |
3182 return str->codesys; | |
3183 | |
3184 switch (data->actual) | |
3185 { | |
3186 case EOL_LF: return Fget_coding_system (Qconvert_eol_lf); | |
3187 case EOL_CRLF: return Fget_coding_system (Qconvert_eol_crlf); | |
3188 case EOL_CR: return Fget_coding_system (Qconvert_eol_cr); | |
3189 case EOL_AUTODETECT: return str->codesys; | |
2500 | 3190 default: ABORT (); return Qnil; |
771 | 3191 } |
3192 } | |
3193 | |
3194 | |
3195 /************************************************************************/ | |
3196 /* Undecided methods */ | |
3197 /************************************************************************/ | |
3198 | |
3199 /* Do autodetection. We can autodetect the EOL type only, the coding | |
3200 system only, or both. We only do autodetection when decoding; when | |
3201 encoding, we just pass the data through. | |
3202 | |
3203 When doing just EOL detection, a coding system can be specified; if so, | |
3204 we will decode this data through the coding system before doing EOL | |
3205 detection. The reason for specifying this is so that | |
3206 canonicalize-after-coding works: We will canonicalize the specified | |
3207 coding system into the appropriate EOL type. When doing both coding and | |
3208 EOL detection, we do similar canonicalization, and also catch situations | |
3209 where the EOL type is overspecified, i.e. the detected coding system | |
3210 specifies an EOL type, and either switch to the equivalent | |
3211 non-EOL-processing coding system (if possible), or terminate EOL | |
3212 detection and use the specified EOL type. This prevents data from being | |
3213 EOL-processed twice. | |
3214 */ | |
3215 | |
3216 struct undecided_coding_system | |
3217 { | |
3218 int do_eol, do_coding; | |
3219 Lisp_Object cs; | |
3220 }; | |
3221 | |
3222 struct undecided_coding_stream | |
3223 { | |
3224 Lisp_Object actual; | |
3225 /* Either 2 or 3 lstreams here; see undecided_convert */ | |
3226 struct chain_coding_stream c; | |
3227 | |
3228 struct detection_state *st; | |
3229 }; | |
3230 | |
1204 | 3231 static const struct memory_description undecided_coding_system_description[] = { |
3232 { XD_LISP_OBJECT, offsetof (struct undecided_coding_system, cs) }, | |
771 | 3233 { XD_END } |
3234 }; | |
3235 | |
1204 | 3236 static const struct memory_description undecided_coding_stream_description_1 [] = { |
3237 { XD_LISP_OBJECT, offsetof (struct undecided_coding_stream, actual) }, | |
2367 | 3238 { XD_BLOCK_ARRAY, offsetof (struct undecided_coding_stream, c), |
2551 | 3239 1, { &chain_coding_stream_description } }, |
1204 | 3240 { XD_END } |
3241 }; | |
3242 | |
3243 const struct sized_memory_description undecided_coding_stream_description = { | |
3244 sizeof (struct undecided_coding_stream), undecided_coding_stream_description_1 | |
3245 }; | |
3246 | |
3247 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (undecided); | |
3248 | |
771 | 3249 static void |
3250 undecided_init (Lisp_Object codesys) | |
3251 { | |
3252 struct undecided_coding_system *data = | |
3253 XCODING_SYSTEM_TYPE_DATA (codesys, undecided); | |
3254 | |
3255 data->cs = Qnil; | |
3256 } | |
3257 | |
3258 static void | |
3259 undecided_mark (Lisp_Object codesys) | |
3260 { | |
3261 struct undecided_coding_system *data = | |
3262 XCODING_SYSTEM_TYPE_DATA (codesys, undecided); | |
3263 | |
3264 mark_object (data->cs); | |
3265 } | |
3266 | |
3267 static void | |
3268 undecided_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag) | |
3269 { | |
3270 struct undecided_coding_system *data = | |
3271 XCODING_SYSTEM_TYPE_DATA (cs, undecided); | |
3272 int need_space = 0; | |
3273 | |
826 | 3274 write_c_string (printcharfun, "("); |
771 | 3275 if (data->do_eol) |
3276 { | |
826 | 3277 write_c_string (printcharfun, "do-eol"); |
771 | 3278 need_space = 1; |
3279 } | |
3280 if (data->do_coding) | |
3281 { | |
3282 if (need_space) | |
826 | 3283 write_c_string (printcharfun, " "); |
3284 write_c_string (printcharfun, "do-coding"); | |
771 | 3285 need_space = 1; |
3286 } | |
3287 if (!NILP (data->cs)) | |
3288 { | |
3289 if (need_space) | |
826 | 3290 write_c_string (printcharfun, " "); |
3291 write_c_string (printcharfun, "coding-system="); | |
771 | 3292 print_coding_system_in_print_method (data->cs, printcharfun, escapeflag); |
3293 } | |
826 | 3294 write_c_string (printcharfun, ")"); |
771 | 3295 } |
3296 | |
3297 static void | |
3298 undecided_mark_coding_stream (struct coding_stream *str) | |
3299 { | |
1204 | 3300 mark_object (CODING_STREAM_TYPE_DATA (str, undecided)->actual); |
771 | 3301 chain_mark_coding_stream_1 (&CODING_STREAM_TYPE_DATA (str, undecided)->c); |
3302 } | |
3303 | |
3304 static int | |
3305 undecided_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value) | |
3306 { | |
3307 struct undecided_coding_system *data = | |
3308 XCODING_SYSTEM_TYPE_DATA (codesys, undecided); | |
3309 | |
3310 if (EQ (key, Qdo_eol)) | |
3311 data->do_eol = 1; | |
3312 else if (EQ (key, Qdo_coding)) | |
3313 data->do_coding = 1; | |
3314 else if (EQ (key, Qcoding_system)) | |
3315 data->cs = get_coding_system_for_text_file (value, 0); | |
3316 else | |
3317 return 0; | |
3318 return 1; | |
3319 } | |
3320 | |
3321 static Lisp_Object | |
3322 undecided_getprop (Lisp_Object codesys, Lisp_Object prop) | |
3323 { | |
3324 struct undecided_coding_system *data = | |
3325 XCODING_SYSTEM_TYPE_DATA (codesys, undecided); | |
3326 | |
3327 if (EQ (prop, Qdo_eol)) | |
3328 return data->do_eol ? Qt : Qnil; | |
3329 if (EQ (prop, Qdo_coding)) | |
3330 return data->do_coding ? Qt : Qnil; | |
3331 if (EQ (prop, Qcoding_system)) | |
3332 return data->cs; | |
3333 return Qunbound; | |
3334 } | |
3335 | |
3336 static struct detection_state * | |
3337 allocate_detection_state (void) | |
3338 { | |
3339 int i; | |
3340 Bytecount size = MAX_ALIGN_SIZE (sizeof (struct detection_state)); | |
3341 struct detection_state *block; | |
3342 | |
3343 for (i = 0; i < coding_detector_count; i++) | |
3344 size += MAX_ALIGN_SIZE (Dynarr_at (all_coding_detectors, i).data_size); | |
3345 | |
3346 block = (struct detection_state *) xmalloc_and_zero (size); | |
3347 | |
3348 size = MAX_ALIGN_SIZE (sizeof (struct detection_state)); | |
3349 for (i = 0; i < coding_detector_count; i++) | |
3350 { | |
3351 block->data_offset[i] = size; | |
3352 size += MAX_ALIGN_SIZE (Dynarr_at (all_coding_detectors, i).data_size); | |
3353 } | |
3354 | |
3355 return block; | |
3356 } | |
3357 | |
3358 static void | |
3359 free_detection_state (struct detection_state *st) | |
3360 { | |
3361 int i; | |
3362 | |
3363 for (i = 0; i < coding_detector_count; i++) | |
3364 { | |
3365 if (Dynarr_at (all_coding_detectors, i).finalize_detection_state_method) | |
3366 Dynarr_at (all_coding_detectors, i).finalize_detection_state_method | |
3367 (st); | |
3368 } | |
3369 | |
1726 | 3370 xfree (st, struct detection_state *); |
771 | 3371 } |
3372 | |
3373 static int | |
3374 coding_category_symbol_to_id (Lisp_Object symbol) | |
428 | 3375 { |
3376 int i; | |
3377 | |
3378 CHECK_SYMBOL (symbol); | |
771 | 3379 for (i = 0; i < coding_detector_count; i++) |
3380 { | |
3381 detector_category_dynarr *cats = | |
3382 Dynarr_at (all_coding_detectors, i).cats; | |
3383 int j; | |
3384 | |
3385 for (j = 0; j < Dynarr_length (cats); j++) | |
3386 if (EQ (Dynarr_at (cats, j).sym, symbol)) | |
3387 return Dynarr_at (cats, j).id; | |
3388 } | |
3389 | |
563 | 3390 invalid_constant ("Unrecognized coding category", symbol); |
1204 | 3391 RETURN_NOT_REACHED (0); |
428 | 3392 } |
3393 | |
771 | 3394 static Lisp_Object |
3395 coding_category_id_to_symbol (int id) | |
428 | 3396 { |
3397 int i; | |
771 | 3398 |
3399 for (i = 0; i < coding_detector_count; i++) | |
3400 { | |
3401 detector_category_dynarr *cats = | |
3402 Dynarr_at (all_coding_detectors, i).cats; | |
3403 int j; | |
3404 | |
3405 for (j = 0; j < Dynarr_length (cats); j++) | |
3406 if (id == Dynarr_at (cats, j).id) | |
3407 return Dynarr_at (cats, j).sym; | |
3408 } | |
3409 | |
2500 | 3410 ABORT (); |
771 | 3411 return Qnil; /* (usually) not reached */ |
428 | 3412 } |
3413 | |
771 | 3414 static Lisp_Object |
3415 detection_result_number_to_symbol (enum detection_result result) | |
428 | 3416 { |
1494 | 3417 /* let compiler warn if not all enumerators are handled */ |
3418 switch (result) { | |
3419 #define FROB(sym, num) case num: return (sym) | |
771 | 3420 FROB (Qnear_certainty, DET_NEAR_CERTAINTY); |
3421 FROB (Qquite_probable, DET_QUITE_PROBABLE); | |
3422 FROB (Qsomewhat_likely, DET_SOMEWHAT_LIKELY); | |
1494 | 3423 FROB (Qslightly_likely, DET_SLIGHTLY_LIKELY); |
771 | 3424 FROB (Qas_likely_as_unlikely, DET_AS_LIKELY_AS_UNLIKELY); |
3425 FROB (Qsomewhat_unlikely, DET_SOMEWHAT_UNLIKELY); | |
3426 FROB (Qquite_improbable, DET_QUITE_IMPROBABLE); | |
3427 FROB (Qnearly_impossible, DET_NEARLY_IMPOSSIBLE); | |
3428 #undef FROB | |
1494 | 3429 } |
771 | 3430 |
2500 | 3431 ABORT (); |
771 | 3432 return Qnil; /* (usually) not reached */ |
3433 } | |
3434 | |
778 | 3435 #if 0 /* not used */ |
771 | 3436 static enum detection_result |
3437 detection_result_symbol_to_number (Lisp_Object symbol) | |
3438 { | |
1494 | 3439 /* using switch here would be bad style, and doesn't help */ |
771 | 3440 #define FROB(sym, num) if (EQ (symbol, sym)) return (num) |
3441 FROB (Qnear_certainty, DET_NEAR_CERTAINTY); | |
3442 FROB (Qquite_probable, DET_QUITE_PROBABLE); | |
3443 FROB (Qsomewhat_likely, DET_SOMEWHAT_LIKELY); | |
1494 | 3444 FROB (Qslightly_likely, DET_SLIGHTLY_LIKELY); |
771 | 3445 FROB (Qas_likely_as_unlikely, DET_AS_LIKELY_AS_UNLIKELY); |
3446 FROB (Qsomewhat_unlikely, DET_SOMEWHAT_UNLIKELY); | |
3447 FROB (Qquite_improbable, DET_QUITE_IMPROBABLE); | |
3448 FROB (Qnearly_impossible, DET_NEARLY_IMPOSSIBLE); | |
3449 #undef FROB | |
3450 | |
3451 invalid_constant ("Unrecognized detection result", symbol); | |
3452 return ((enum detection_result) 0); /* not reached */ | |
3453 } | |
778 | 3454 #endif /* 0 */ |
771 | 3455 |
3456 /* Set all detection results for a given detector to a specified value. */ | |
3457 void | |
3458 set_detection_results (struct detection_state *st, int detector, int given) | |
3459 { | |
3460 detector_category_dynarr *cats = | |
3461 Dynarr_at (all_coding_detectors, detector).cats; | |
3462 int i; | |
3463 | |
3464 for (i = 0; i < Dynarr_length (cats); i++) | |
3465 st->categories[Dynarr_at (cats, i).id] = given; | |
3466 } | |
428 | 3467 |
3468 static int | |
3469 acceptable_control_char_p (int c) | |
3470 { | |
3471 switch (c) | |
3472 { | |
3473 /* Allow and ignore control characters that you might | |
3474 reasonably see in a text file */ | |
3475 case '\r': | |
3476 case '\n': | |
3477 case '\t': | |
3478 case 7: /* bell */ | |
3479 case 8: /* backspace */ | |
3480 case 11: /* vertical tab */ | |
3481 case 12: /* form feed */ | |
3482 case 26: /* MS-DOS C-z junk */ | |
3483 case 31: /* '^_' -- for info */ | |
3484 return 1; | |
3485 default: | |
3486 return 0; | |
3487 } | |
3488 } | |
3489 | |
771 | 3490 #ifdef DEBUG_XEMACS |
3491 | |
3492 static UExtbyte | |
3493 hex_digit_to_char (int digit) | |
428 | 3494 { |
771 | 3495 if (digit < 10) |
3496 return digit + '0'; | |
3497 else | |
3498 return digit - 10 + 'A'; | |
428 | 3499 } |
3500 | |
771 | 3501 static void |
3502 output_bytes_in_ascii_and_hex (const UExtbyte *src, Bytecount n) | |
428 | 3503 { |
3425 | 3504 Extbyte *ascii = alloca_array (Extbyte, n + 1); |
3505 Extbyte *hex = alloca_array (Extbyte, 3 * n + 1); | |
771 | 3506 int i; |
3413 | 3507 DECLARE_EISTRING (eistr_ascii); |
3508 DECLARE_EISTRING (eistr_hex); | |
771 | 3509 |
3510 for (i = 0; i < n; i++) | |
428 | 3511 { |
3425 | 3512 Extbyte c = src[i]; |
771 | 3513 if (c < 0x20) |
3514 ascii[i] = '.'; | |
428 | 3515 else |
771 | 3516 ascii[i] = c; |
3517 hex[3 * i] = hex_digit_to_char (c >> 4); | |
3518 hex[3 * i + 1] = hex_digit_to_char (c & 0xF); | |
3519 hex[3 * i + 2] = ' '; | |
428 | 3520 } |
771 | 3521 ascii[i] = '\0'; |
3522 hex[3 * i - 1] = '\0'; | |
3413 | 3523 |
3524 eicpy_ext(eistr_hex, hex, Qbinary); | |
3525 eicpy_ext(eistr_ascii, ascii, Qbinary); | |
3526 | |
3425 | 3527 stderr_out ("%s %s", eidata(eistr_ascii), eidata(eistr_hex)); |
428 | 3528 } |
3529 | |
771 | 3530 #endif /* DEBUG_XEMACS */ |
3531 | |
3532 /* Attempt to determine the encoding of the given text. Before calling | |
3533 this function for the first time, you must zero out the detection state. | |
428 | 3534 |
3535 Returns: | |
3536 | |
771 | 3537 0 == keep going |
3538 1 == stop | |
428 | 3539 */ |
3540 | |
3541 static int | |
771 | 3542 detect_coding_type (struct detection_state *st, const UExtbyte *src, |
3543 Bytecount n) | |
428 | 3544 { |
771 | 3545 Bytecount n2 = n; |
3546 const UExtbyte *src2 = src; | |
3547 int i; | |
3548 | |
3549 #ifdef DEBUG_XEMACS | |
3550 if (!NILP (Vdebug_coding_detection)) | |
3551 { | |
3552 int bytes = min (16, n); | |
3553 stderr_out ("detect_coding_type: processing %ld bytes\n", n); | |
3554 stderr_out ("First %d: ", bytes); | |
3555 output_bytes_in_ascii_and_hex (src, bytes); | |
3556 stderr_out ("\nLast %d: ", bytes); | |
3557 output_bytes_in_ascii_and_hex (src + n - bytes, bytes); | |
3558 stderr_out ("\n"); | |
3559 } | |
3560 #endif /* DEBUG_XEMACS */ | |
428 | 3561 if (!st->seen_non_ascii) |
3562 { | |
771 | 3563 for (; n2; n2--, src2++) |
428 | 3564 { |
771 | 3565 UExtbyte c = *src2; |
428 | 3566 if ((c < 0x20 && !acceptable_control_char_p (c)) || c >= 0x80) |
3567 { | |
3568 st->seen_non_ascii = 1; | |
3569 break; | |
3570 } | |
3571 } | |
3572 } | |
3573 | |
771 | 3574 for (i = 0; i < coding_detector_count; i++) |
3575 Dynarr_at (all_coding_detectors, i).detect_method (st, src, n); | |
3576 | |
3577 st->bytes_seen += n; | |
3578 | |
3579 #ifdef DEBUG_XEMACS | |
3580 if (!NILP (Vdebug_coding_detection)) | |
3581 { | |
3582 stderr_out ("seen_non_ascii: %d\n", st->seen_non_ascii); | |
1494 | 3583 if (coding_detector_category_count <= 0) |
3584 stderr_out ("found %d detector categories\n", | |
3585 coding_detector_category_count); | |
771 | 3586 for (i = 0; i < coding_detector_category_count; i++) |
3587 stderr_out_lisp | |
3588 ("%s: %s\n", | |
3589 2, | |
3590 coding_category_id_to_symbol (i), | |
3591 detection_result_number_to_symbol ((enum detection_result) | |
3592 st->categories[i])); | |
3593 } | |
3594 #endif /* DEBUG_XEMACS */ | |
3595 | |
3596 { | |
3597 int not_unlikely = 0; | |
3598 int retval; | |
3599 | |
3600 for (i = 0; i < coding_detector_category_count; i++) | |
3601 if (st->categories[i] >= 0) | |
3602 not_unlikely++; | |
3603 | |
3604 retval = (not_unlikely <= 1 | |
3605 #if 0 /* this is bogus */ | |
3606 || st->bytes_seen >= MAX_BYTES_PROCESSED_FOR_DETECTION | |
428 | 3607 #endif |
771 | 3608 ); |
3609 | |
3610 #ifdef DEBUG_XEMACS | |
3611 if (!NILP (Vdebug_coding_detection)) | |
3612 stderr_out ("detect_coding_type: returning %d (%s)\n", | |
3613 retval, retval ? "stop" : "keep going"); | |
3614 #endif /* DEBUG_XEMACS */ | |
3615 | |
3616 return retval; | |
428 | 3617 } |
3618 } | |
3619 | |
3620 static Lisp_Object | |
771 | 3621 detected_coding_system (struct detection_state *st) |
428 | 3622 { |
771 | 3623 int i; |
3624 int even = 1; | |
3625 | |
3626 if (st->seen_non_ascii) | |
3627 { | |
3628 for (i = 0; i < coding_detector_category_count; i++) | |
3629 if (st->categories[i] != DET_AS_LIKELY_AS_UNLIKELY) | |
3630 { | |
3631 even = 0; | |
3632 break; | |
3633 } | |
3634 } | |
3635 | |
3636 /* #### Here we are ignoring the results of detection when it's all | |
3637 ASCII. This is obviously a bad thing. But we need to fix up the | |
3638 existing detection methods somewhat before we can switch. */ | |
3639 if (even) | |
428 | 3640 { |
3641 /* If the file was entirely or basically ASCII, use the | |
3642 default value of `buffer-file-coding-system'. */ | |
3643 Lisp_Object retval = | |
3644 XBUFFER (Vbuffer_defaults)->buffer_file_coding_system; | |
3645 if (!NILP (retval)) | |
3646 { | |
771 | 3647 retval = find_coding_system_for_text_file (retval, 0); |
428 | 3648 if (NILP (retval)) |
3649 { | |
3650 warn_when_safe | |
3651 (Qbad_variable, Qwarning, | |
3652 "Invalid `default-buffer-file-coding-system', set to nil"); | |
3653 XBUFFER (Vbuffer_defaults)->buffer_file_coding_system = Qnil; | |
3654 } | |
3655 } | |
3656 if (NILP (retval)) | |
4100 | 3657 retval = Fget_coding_system (Qbinary); |
428 | 3658 return retval; |
3659 } | |
3660 else | |
3661 { | |
771 | 3662 int likelihood; |
3663 Lisp_Object retval = Qnil; | |
3664 | |
3665 /* Look through the coding categories first by likelihood and then by | |
3666 priority and find the first one that is allowed. */ | |
3667 | |
3668 for (likelihood = DET_HIGHEST; likelihood >= DET_LOWEST; likelihood--) | |
428 | 3669 { |
771 | 3670 for (i = 0; i < coding_detector_category_count; i++) |
3671 { | |
3672 int cat = coding_category_by_priority[i]; | |
3673 if (st->categories[cat] == likelihood && | |
3674 !NILP (coding_category_system[cat])) | |
3675 { | |
3676 retval = (get_coding_system_for_text_file | |
3677 (coding_category_system[cat], 0)); | |
3678 if (likelihood < DET_AS_LIKELY_AS_UNLIKELY) | |
3679 warn_when_safe_lispobj | |
3680 (intern ("detection"), | |
793 | 3681 Qwarning, |
771 | 3682 emacs_sprintf_string_lisp |
3683 ( | |
3684 "Detected coding %s is unlikely to be correct (likelihood == `%s')", | |
3685 Qnil, 2, XCODING_SYSTEM_NAME (retval), | |
3686 detection_result_number_to_symbol | |
3687 ((enum detection_result) likelihood))); | |
3688 return retval; | |
3689 } | |
3690 } | |
428 | 3691 } |
771 | 3692 |
3693 return Fget_coding_system (Qraw_text); | |
428 | 3694 } |
3695 } | |
3696 | |
1347 | 3697 /* Look for a coding system in the string (skipping over leading |
3698 blanks). If found, return it, otherwise nil. */ | |
3699 | |
3700 static Lisp_Object | |
2531 | 3701 snarf_coding_system (const UExtbyte *p, Bytecount len) |
1347 | 3702 { |
3703 Bytecount n; | |
2531 | 3704 UExtbyte *name; |
1347 | 3705 |
3706 while (*p == ' ' || *p == '\t') p++, len--; | |
3707 len = min (len, 1000); | |
3708 name = alloca_ibytes (len + 1); | |
3709 memcpy (name, p, len); | |
3710 name[len] = '\0'; | |
3711 | |
3712 /* Get coding system name */ | |
3713 /* Characters valid in a MIME charset name (rfc 1521), | |
3714 and in a Lisp symbol name. */ | |
3715 n = qxestrspn (name, | |
3716 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
3717 "abcdefghijklmnopqrstuvwxyz" | |
3718 "0123456789" | |
3719 "!$%&*+-.^_{|}~"); | |
3720 if (n > 0) | |
3721 { | |
3722 name[n] = '\0'; | |
2531 | 3723 /* This call to intern_int() is OK because we already verified that |
3724 there are only ASCII characters in the string */ | |
3725 return find_coding_system_for_text_file (intern_int ((Ibyte *) name), 0); | |
1347 | 3726 } |
3727 | |
3728 return Qnil; | |
3729 } | |
3730 | |
428 | 3731 /* Given a seekable read stream and potential coding system and EOL type |
3732 as specified, do any autodetection that is called for. If the | |
3733 coding system and/or EOL type are not `autodetect', they will be left | |
3734 alone; but this function will never return an autodetect coding system | |
3735 or EOL type. | |
3736 | |
3737 This function does not automatically fetch subsidiary coding systems; | |
3738 that should be unnecessary with the explicit eol-type argument. */ | |
3739 | |
3740 #define LENGTH(string_constant) (sizeof (string_constant) - 1) | |
3741 | |
771 | 3742 static Lisp_Object |
3743 unwind_free_detection_state (Lisp_Object opaque) | |
3744 { | |
3745 struct detection_state *st = | |
3746 (struct detection_state *) get_opaque_ptr (opaque); | |
3747 free_detection_state (st); | |
3748 free_opaque_ptr (opaque); | |
3749 return Qnil; | |
3750 } | |
3751 | |
1347 | 3752 /* #### This duplicates code in `find-coding-system-magic-cookie-in-file' |
3753 in files.el. Look into combining them. */ | |
3754 | |
771 | 3755 static Lisp_Object |
3756 look_for_coding_system_magic_cookie (const UExtbyte *data, Bytecount len) | |
428 | 3757 { |
771 | 3758 const UExtbyte *p; |
3759 const UExtbyte *scan_end; | |
2531 | 3760 Bytecount cookie_len; |
771 | 3761 |
3762 /* Look for initial "-*-"; mode line prefix */ | |
3763 for (p = data, | |
3764 scan_end = data + len - LENGTH ("-*-coding:?-*-"); | |
3765 p <= scan_end | |
3766 && *p != '\n' | |
3767 && *p != '\r'; | |
3768 p++) | |
3769 if (*p == '-' && *(p+1) == '*' && *(p+2) == '-') | |
3770 { | |
3771 const UExtbyte *local_vars_beg = p + 3; | |
3772 /* Look for final "-*-"; mode line suffix */ | |
3773 for (p = local_vars_beg, | |
3774 scan_end = data + len - LENGTH ("-*-"); | |
3775 p <= scan_end | |
428 | 3776 && *p != '\n' |
3777 && *p != '\r'; | |
771 | 3778 p++) |
3779 if (*p == '-' && *(p+1) == '*' && *(p+2) == '-') | |
3780 { | |
3781 const UExtbyte *suffix = p; | |
3782 /* Look for "coding:" */ | |
3783 for (p = local_vars_beg, | |
3784 scan_end = suffix - LENGTH ("coding:?"); | |
3785 p <= scan_end; | |
3786 p++) | |
3787 if (memcmp ("coding:", p, LENGTH ("coding:")) == 0 | |
3788 && (p == local_vars_beg | |
3789 || (*(p-1) == ' ' || | |
3790 *(p-1) == '\t' || | |
3791 *(p-1) == ';'))) | |
3792 { | |
3793 p += LENGTH ("coding:"); | |
1347 | 3794 return snarf_coding_system (p, suffix - p); |
771 | 3795 break; |
3796 } | |
3797 break; | |
3798 } | |
3799 break; | |
3800 } | |
3801 | |
2531 | 3802 /* Look for ;;;###coding system */ |
3803 | |
3804 cookie_len = LENGTH (";;;###coding system: "); | |
3805 | |
3806 for (p = data, | |
3807 scan_end = data + len - cookie_len; | |
3808 p <= scan_end; | |
3809 p++) | |
1347 | 3810 { |
2531 | 3811 if (*p == ';' && !memcmp (p, ";;;###coding system: ", cookie_len)) |
3812 { | |
3813 const UExtbyte *suffix; | |
3814 | |
3815 p += cookie_len; | |
3816 suffix = p; | |
3817 while (suffix < scan_end && !isspace (*suffix)) | |
3818 suffix++; | |
3819 return snarf_coding_system (p, suffix - p); | |
3820 } | |
1347 | 3821 } |
3822 | |
3823 return Qnil; | |
771 | 3824 } |
3825 | |
3826 static Lisp_Object | |
3827 determine_real_coding_system (Lstream *stream) | |
3828 { | |
3829 struct detection_state *st = allocate_detection_state (); | |
3830 int depth = record_unwind_protect (unwind_free_detection_state, | |
3831 make_opaque_ptr (st)); | |
3832 UExtbyte buf[4096]; | |
3833 Bytecount nread = Lstream_read (stream, buf, sizeof (buf)); | |
3834 Lisp_Object coding_system = look_for_coding_system_magic_cookie (buf, nread); | |
3835 | |
3836 if (NILP (coding_system)) | |
3837 { | |
3838 while (1) | |
3839 { | |
3840 if (detect_coding_type (st, buf, nread)) | |
428 | 3841 break; |
771 | 3842 nread = Lstream_read (stream, buf, sizeof (buf)); |
3843 if (nread == 0) | |
3844 break; | |
428 | 3845 } |
771 | 3846 |
3847 coding_system = detected_coding_system (st); | |
428 | 3848 } |
3849 | |
3850 Lstream_rewind (stream); | |
771 | 3851 |
3852 unbind_to (depth); | |
3853 return coding_system; | |
3854 } | |
3855 | |
3856 static void | |
3857 undecided_init_coding_stream (struct coding_stream *str) | |
3858 { | |
3859 struct undecided_coding_stream *data = | |
3860 CODING_STREAM_TYPE_DATA (str, undecided); | |
3861 struct undecided_coding_system *csdata = | |
3862 XCODING_SYSTEM_TYPE_DATA (str->codesys, undecided); | |
3863 | |
3864 data->actual = Qnil; | |
3865 | |
3866 if (str->direction == CODING_DECODE) | |
3867 { | |
3868 Lstream *lst = str->other_end; | |
3869 | |
3870 if ((lst->flags & LSTREAM_FL_READ) && | |
3871 Lstream_seekable_p (lst) && | |
3872 csdata->do_coding) | |
3873 /* We can determine the coding system now. */ | |
3874 data->actual = determine_real_coding_system (lst); | |
3875 } | |
1494 | 3876 |
3877 #ifdef DEBUG_XEMACS | |
3878 if (!NILP (Vdebug_coding_detection)) | |
3879 stderr_out_lisp ("detected coding system: %s\n", 1, data->actual); | |
3880 #endif /* DEBUG_XEMACS */ | |
771 | 3881 } |
3882 | |
3883 static void | |
3884 undecided_rewind_coding_stream (struct coding_stream *str) | |
3885 { | |
3886 chain_rewind_coding_stream_1 (&CODING_STREAM_TYPE_DATA (str, undecided)->c); | |
3887 } | |
3888 | |
3889 static void | |
3890 undecided_finalize_coding_stream (struct coding_stream *str) | |
3891 { | |
3892 struct undecided_coding_stream *data = | |
3893 CODING_STREAM_TYPE_DATA (str, undecided); | |
3894 | |
3895 chain_finalize_coding_stream_1 | |
3896 (&CODING_STREAM_TYPE_DATA (str, undecided)->c); | |
3897 if (data->st) | |
3898 free_detection_state (data->st); | |
3899 } | |
3900 | |
3901 static Lisp_Object | |
3902 undecided_canonicalize (Lisp_Object codesys) | |
3903 { | |
3904 struct undecided_coding_system *csdata = | |
3905 XCODING_SYSTEM_TYPE_DATA (codesys, undecided); | |
3906 if (!csdata->do_eol && !csdata->do_coding) | |
3907 return NILP (csdata->cs) ? Fget_coding_system (Qbinary) : csdata->cs; | |
3908 if (csdata->do_eol && !csdata->do_coding && NILP (csdata->cs)) | |
3909 return Fget_coding_system (Qconvert_eol_autodetect); | |
3910 return codesys; | |
3911 } | |
3912 | |
3913 static Bytecount | |
3914 undecided_convert (struct coding_stream *str, const UExtbyte *src, | |
3915 unsigned_char_dynarr *dst, Bytecount n) | |
3916 { | |
3917 int first_time = 0; | |
3918 | |
3919 if (str->direction == CODING_DECODE) | |
3920 { | |
3921 /* At this point, we have only the following possibilities: | |
3922 | |
3923 do_eol && do_coding | |
3924 do_coding only | |
3925 do_eol only and a coding system was specified | |
3926 | |
3927 Other possibilities are removed during undecided_canonicalize. | |
3928 | |
3929 Therefore, our substreams are either | |
3930 | |
3931 lstream_coding -> lstream_dynarr, or | |
3932 lstream_coding -> lstream_eol -> lstream_dynarr. | |
3933 */ | |
3934 struct undecided_coding_system *csdata = | |
3935 XCODING_SYSTEM_TYPE_DATA (str->codesys, undecided); | |
3936 struct undecided_coding_stream *data = | |
3937 CODING_STREAM_TYPE_DATA (str, undecided); | |
3938 | |
3939 if (str->eof) | |
3940 { | |
3941 /* Each will close the next. We need to close now because more | |
3942 data may be generated. */ | |
3943 if (data->c.initted) | |
3944 Lstream_close (XLSTREAM (data->c.lstreams[0])); | |
3945 return n; | |
3946 } | |
3947 | |
3948 if (!data->c.initted) | |
3949 { | |
3950 data->c.lstream_count = csdata->do_eol ? 3 : 2; | |
3951 data->c.lstreams = xnew_array (Lisp_Object, data->c.lstream_count); | |
3952 | |
3953 data->c.lstreams[data->c.lstream_count - 1] = | |
3954 make_dynarr_output_stream (dst); | |
3955 Lstream_set_buffering | |
3956 (XLSTREAM (data->c.lstreams[data->c.lstream_count - 1]), | |
3957 LSTREAM_UNBUFFERED, 0); | |
3958 if (csdata->do_eol) | |
3959 { | |
3960 data->c.lstreams[1] = | |
3961 make_coding_output_stream | |
3962 (XLSTREAM (data->c.lstreams[data->c.lstream_count - 1]), | |
3963 Fget_coding_system (Qconvert_eol_autodetect), | |
800 | 3964 CODING_DECODE, 0); |
771 | 3965 Lstream_set_buffering |
3966 (XLSTREAM (data->c.lstreams[1]), | |
3967 LSTREAM_UNBUFFERED, 0); | |
3968 } | |
3969 | |
3970 data->c.lstreams[0] = | |
3971 make_coding_output_stream | |
3972 (XLSTREAM (data->c.lstreams[1]), | |
3973 /* Substitute binary if we need to detect the encoding */ | |
3974 csdata->do_coding ? Qbinary : csdata->cs, | |
800 | 3975 CODING_DECODE, 0); |
771 | 3976 Lstream_set_buffering (XLSTREAM (data->c.lstreams[0]), |
3977 LSTREAM_UNBUFFERED, 0); | |
3978 | |
3979 first_time = 1; | |
3980 data->c.initted = 1; | |
3981 } | |
3982 | |
3983 /* If necessary, do encoding-detection now. We do this when we're a | |
3984 writing stream or a non-seekable reading stream, meaning that we | |
3985 can't just process the whole input, rewind, and start over. */ | |
3986 | |
3987 if (csdata->do_coding) | |
3988 { | |
3989 int actual_was_nil = NILP (data->actual); | |
3990 if (NILP (data->actual)) | |
3991 { | |
3992 if (!data->st) | |
3993 data->st = allocate_detection_state (); | |
3994 if (first_time) | |
3995 /* #### This is cheesy. What we really ought to do is buffer | |
3996 up a certain minimum amount of data to get a better result. | |
3997 */ | |
3998 data->actual = look_for_coding_system_magic_cookie (src, n); | |
3999 if (NILP (data->actual)) | |
4000 { | |
4001 /* #### This is cheesy. What we really ought to do is buffer | |
4002 up a certain minimum amount of data so as to get a less | |
4003 random result when doing subprocess detection. */ | |
4004 detect_coding_type (data->st, src, n); | |
4005 data->actual = detected_coding_system (data->st); | |
4100 | 4006 /* kludge to prevent infinite recursion */ |
4007 if (XCODING_SYSTEM(data->actual)->methods->enumtype == undecided_coding_system) | |
4008 data->actual = Fget_coding_system (Qbinary); | |
771 | 4009 } |
4010 } | |
4011 /* We need to set the detected coding system if we actually have | |
4012 such a coding system but didn't before. That is the case | |
4013 either when we just detected it in the previous code or when | |
4014 it was detected during undecided_init_coding_stream(). We | |
4015 can check for that using first_time. */ | |
4016 if (!NILP (data->actual) && (actual_was_nil || first_time)) | |
4017 { | |
4018 /* If the detected coding system doesn't allow for EOL | |
4019 autodetection, try to get the equivalent that does; | |
4020 otherwise, disable EOL detection (overriding whatever | |
4021 may already have been detected). */ | |
4022 if (XCODING_SYSTEM_EOL_TYPE (data->actual) != EOL_AUTODETECT) | |
4023 { | |
4024 if (!NILP (XCODING_SYSTEM_SUBSIDIARY_PARENT (data->actual))) | |
4025 data->actual = | |
4026 XCODING_SYSTEM_SUBSIDIARY_PARENT (data->actual); | |
4027 else if (data->c.lstream_count == 3) | |
4028 set_coding_stream_coding_system | |
4029 (XLSTREAM (data->c.lstreams[1]), | |
4030 Fget_coding_system (Qidentity)); | |
4031 } | |
4032 set_coding_stream_coding_system | |
4033 (XLSTREAM (data->c.lstreams[0]), data->actual); | |
4034 } | |
4035 } | |
4036 | |
4037 if (Lstream_write (XLSTREAM (data->c.lstreams[0]), src, n) < 0) | |
4038 return -1; | |
4039 return n; | |
4040 } | |
4041 else | |
4042 return no_conversion_convert (str, src, dst, n); | |
4043 } | |
4044 | |
4045 static Lisp_Object | |
4046 undecided_canonicalize_after_coding (struct coding_stream *str) | |
4047 { | |
4048 struct undecided_coding_stream *data = | |
4049 CODING_STREAM_TYPE_DATA (str, undecided); | |
4050 Lisp_Object ret, eolret; | |
4051 | |
4052 if (str->direction == CODING_ENCODE) | |
4053 return str->codesys; | |
4054 | |
4055 if (!data->c.initted) | |
4647
e4ed58cb0e5b
Fix bugs with #'find-file, 0-length files, & coding-system-for-read specified.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4569
diff
changeset
|
4056 return str->codesys; |
771 | 4057 |
4058 ret = coding_stream_canonicalize_after_coding | |
4059 (XLSTREAM (data->c.lstreams[0])); | |
4060 if (NILP (ret)) | |
4647
e4ed58cb0e5b
Fix bugs with #'find-file, 0-length files, & coding-system-for-read specified.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4569
diff
changeset
|
4061 ret = str->codesys; |
771 | 4062 if (XCODING_SYSTEM_EOL_TYPE (ret) != EOL_AUTODETECT) |
4063 return ret; | |
4064 eolret = coding_stream_canonicalize_after_coding | |
4065 (XLSTREAM (data->c.lstreams[1])); | |
4066 if (!EQ (XCODING_SYSTEM_TYPE (eolret), Qconvert_eol)) | |
4067 return ret; | |
4068 return | |
4069 Fsubsidiary_coding_system (ret, Fcoding_system_property (eolret, | |
4070 Qsubtype)); | |
4071 } | |
4072 | |
4073 | |
4074 /************************************************************************/ | |
4075 /* Lisp interface: Coding category functions and detection */ | |
4076 /************************************************************************/ | |
4077 | |
4078 DEFUN ("coding-category-list", Fcoding_category_list, 0, 0, 0, /* | |
4079 Return a list of all recognized coding categories. | |
4080 */ | |
4081 ()) | |
4082 { | |
4083 int i; | |
4084 Lisp_Object list = Qnil; | |
4085 | |
4086 for (i = 0; i < coding_detector_count; i++) | |
4087 { | |
4088 detector_category_dynarr *cats = | |
4089 Dynarr_at (all_coding_detectors, i).cats; | |
4090 int j; | |
4091 | |
4092 for (j = 0; j < Dynarr_length (cats); j++) | |
4093 list = Fcons (Dynarr_at (cats, j).sym, list); | |
4094 } | |
4095 | |
4096 return Fnreverse (list); | |
4097 } | |
4098 | |
4099 DEFUN ("set-coding-priority-list", Fset_coding_priority_list, 1, 1, 0, /* | |
4100 Change the priority order of the coding categories. | |
4101 LIST should be list of coding categories, in descending order of | |
4102 priority. Unspecified coding categories will be lower in priority | |
4103 than all specified ones, in the same relative order they were in | |
4104 previously. | |
4105 */ | |
4106 (list)) | |
4107 { | |
4108 int *category_to_priority = | |
4109 alloca_array (int, coding_detector_category_count); | |
4110 int i, j; | |
4111 | |
4112 /* First generate a list that maps coding categories to priorities. */ | |
4113 | |
4114 for (i = 0; i < coding_detector_category_count; i++) | |
4115 category_to_priority[i] = -1; | |
4116 | |
4117 /* Highest priority comes from the specified list. */ | |
4118 i = 0; | |
2367 | 4119 { |
4120 EXTERNAL_LIST_LOOP_2 (elt, list) | |
4121 { | |
4122 int cat = coding_category_symbol_to_id (elt); | |
4123 | |
4124 if (category_to_priority[cat] >= 0) | |
4125 sferror ("Duplicate coding category in list", elt); | |
4126 category_to_priority[cat] = i++; | |
4127 } | |
4128 } | |
771 | 4129 |
4130 /* Now go through the existing categories by priority to retrieve | |
4131 the categories not yet specified and preserve their priority | |
4132 order. */ | |
4133 for (j = 0; j < coding_detector_category_count; j++) | |
4134 { | |
4135 int cat = coding_category_by_priority[j]; | |
4136 if (category_to_priority[cat] < 0) | |
4137 category_to_priority[cat] = i++; | |
4138 } | |
4139 | |
4140 /* Now we need to construct the inverse of the mapping we just | |
4141 constructed. */ | |
4142 | |
4143 for (i = 0; i < coding_detector_category_count; i++) | |
4144 coding_category_by_priority[category_to_priority[i]] = i; | |
4145 | |
4146 /* Phew! That was confusing. */ | |
4147 return Qnil; | |
4148 } | |
4149 | |
4150 DEFUN ("coding-priority-list", Fcoding_priority_list, 0, 0, 0, /* | |
4151 Return a list of coding categories in descending order of priority. | |
4152 */ | |
4153 ()) | |
4154 { | |
4155 int i; | |
4156 Lisp_Object list = Qnil; | |
4157 | |
4158 for (i = 0; i < coding_detector_category_count; i++) | |
4159 list = | |
4160 Fcons (coding_category_id_to_symbol (coding_category_by_priority[i]), | |
4161 list); | |
4162 return Fnreverse (list); | |
4163 } | |
4164 | |
4165 DEFUN ("set-coding-category-system", Fset_coding_category_system, 2, 2, 0, /* | |
4166 Change the coding system associated with a coding category. | |
4167 */ | |
4168 (coding_category, coding_system)) | |
4169 { | |
4170 coding_category_system[coding_category_symbol_to_id (coding_category)] = | |
4171 Fget_coding_system (coding_system); | |
4172 return Qnil; | |
4173 } | |
4174 | |
4175 DEFUN ("coding-category-system", Fcoding_category_system, 1, 1, 0, /* | |
4176 Return the coding system associated with a coding category. | |
4177 */ | |
4178 (coding_category)) | |
4179 { | |
4180 Lisp_Object sys = | |
4181 coding_category_system[coding_category_symbol_to_id (coding_category)]; | |
4182 | |
4183 if (!NILP (sys)) | |
4184 return XCODING_SYSTEM_NAME (sys); | |
4185 return Qnil; | |
4186 } | |
4187 | |
800 | 4188 /* Detect the encoding of STREAM. Assumes stream is at the begnning and will |
4189 read through to the end of STREAM, leaving it there but open. */ | |
4190 | |
771 | 4191 Lisp_Object |
4192 detect_coding_stream (Lisp_Object stream) | |
4193 { | |
4194 Lisp_Object val = Qnil; | |
4195 struct gcpro gcpro1, gcpro2, gcpro3; | |
4196 UExtbyte random_buffer[65536]; | |
4197 Lisp_Object binary_instream = | |
4198 make_coding_input_stream | |
4199 (XLSTREAM (stream), Qbinary, | |
814 | 4200 CODING_ENCODE, LSTREAM_FL_NO_CLOSE_OTHER); |
771 | 4201 Lisp_Object decstream = |
4202 make_coding_input_stream | |
4203 (XLSTREAM (binary_instream), | |
800 | 4204 Qundecided, CODING_DECODE, 0); |
771 | 4205 Lstream *decstr = XLSTREAM (decstream); |
4206 | |
4207 GCPRO3 (decstream, stream, binary_instream); | |
4208 /* Read and discard all data; detection happens as a side effect of this, | |
4209 and we examine what was detected afterwards. */ | |
4210 while (Lstream_read (decstr, random_buffer, sizeof (random_buffer)) > 0) | |
4211 ; | |
4212 | |
4213 val = coding_stream_detected_coding_system (decstr); | |
4214 Lstream_close (decstr); | |
4215 Lstream_delete (decstr); | |
4216 Lstream_delete (XLSTREAM (binary_instream)); | |
4217 UNGCPRO; | |
4218 return val; | |
428 | 4219 } |
4220 | |
4221 DEFUN ("detect-coding-region", Fdetect_coding_region, 2, 3, 0, /* | |
4222 Detect coding system of the text in the region between START and END. | |
444 | 4223 Return a list of possible coding systems ordered by priority. |
3025 | 4224 If only ASCII characters are found, return `undecided' or one of |
428 | 4225 its subsidiary coding systems according to a detected end-of-line |
4226 type. Optional arg BUFFER defaults to the current buffer. | |
4227 */ | |
4228 (start, end, buffer)) | |
4229 { | |
4230 Lisp_Object val = Qnil; | |
4231 struct buffer *buf = decode_buffer (buffer, 0); | |
665 | 4232 Charbpos b, e; |
771 | 4233 Lisp_Object lb_instream; |
428 | 4234 |
4235 get_buffer_range_char (buf, start, end, &b, &e, 0); | |
4236 lb_instream = make_lisp_buffer_input_stream (buf, b, e, 0); | |
771 | 4237 |
4238 val = detect_coding_stream (lb_instream); | |
4239 Lstream_delete (XLSTREAM (lb_instream)); | |
428 | 4240 return val; |
4241 } | |
4242 | |
4243 | |
771 | 4244 |
4245 #ifdef DEBUG_XEMACS | |
4246 | |
428 | 4247 /************************************************************************/ |
771 | 4248 /* Internal methods */ |
4249 /************************************************************************/ | |
4250 | |
4251 /* Raw (internally-formatted) data. */ | |
4252 DEFINE_CODING_SYSTEM_TYPE (internal); | |
428 | 4253 |
665 | 4254 static Bytecount |
2286 | 4255 internal_convert (struct coding_stream *UNUSED (str), const UExtbyte *src, |
771 | 4256 unsigned_char_dynarr *dst, Bytecount n) |
4257 { | |
4258 Bytecount orign = n; | |
4259 Dynarr_add_many (dst, src, n); | |
4260 return orign; | |
4261 } | |
4262 | |
4263 #endif /* DEBUG_XEMACS */ | |
4264 | |
4265 | |
4266 | |
4267 #ifdef HAVE_ZLIB | |
4268 | |
4269 /************************************************************************/ | |
4270 /* Gzip methods */ | |
4271 /************************************************************************/ | |
4272 | |
4273 struct gzip_coding_system | |
428 | 4274 { |
771 | 4275 int level; /* 0 through 9, or -1 for default */ |
4276 }; | |
4277 | |
4278 #define CODING_SYSTEM_GZIP_LEVEL(codesys) \ | |
4279 (CODING_SYSTEM_TYPE_DATA (codesys, gzip)->level) | |
4280 #define XCODING_SYSTEM_GZIP_LEVEL(codesys) \ | |
4281 (XCODING_SYSTEM_TYPE_DATA (codesys, gzip)->level) | |
4282 | |
4283 struct gzip_coding_stream | |
428 | 4284 { |
771 | 4285 z_stream stream; |
4286 int stream_initted; | |
4287 int reached_eof; /* #### this should be handled by the caller, once we | |
4288 return LSTREAM_EOF */ | |
4289 }; | |
4290 | |
1204 | 4291 static const struct memory_description |
771 | 4292 gzip_coding_system_description[] = { |
4293 { XD_END } | |
4294 }; | |
4295 | |
1204 | 4296 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (gzip); |
4297 | |
771 | 4298 enum source_sink_type |
4299 gzip_conversion_end_type (Lisp_Object codesys) | |
4300 { | |
4301 return DECODES_BYTE_TO_BYTE; | |
428 | 4302 } |
4303 | |
4304 static void | |
771 | 4305 gzip_init (Lisp_Object codesys) |
4306 { | |
4307 struct gzip_coding_system *data = XCODING_SYSTEM_TYPE_DATA (codesys, gzip); | |
4308 data->level = -1; | |
4309 } | |
4310 | |
4311 static void | |
4312 gzip_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag) | |
428 | 4313 { |
771 | 4314 struct gzip_coding_system *data = XCODING_SYSTEM_TYPE_DATA (cs, gzip); |
4315 | |
826 | 4316 write_c_string (printcharfun, "("); |
771 | 4317 if (data->level == -1) |
826 | 4318 write_c_string (printcharfun, "default"); |
771 | 4319 else |
4320 print_internal (make_int (data->level), printcharfun, 0); | |
826 | 4321 write_c_string (printcharfun, ")"); |
428 | 4322 } |
4323 | |
4324 static int | |
771 | 4325 gzip_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value) |
428 | 4326 { |
771 | 4327 struct gzip_coding_system *data = XCODING_SYSTEM_TYPE_DATA (codesys, gzip); |
4328 | |
4329 if (EQ (key, Qlevel)) | |
428 | 4330 { |
771 | 4331 if (EQ (value, Qdefault)) |
4332 data->level = -1; | |
4333 else | |
428 | 4334 { |
771 | 4335 CHECK_INT (value); |
4336 check_int_range (XINT (value), 0, 9); | |
4337 data->level = XINT (value); | |
428 | 4338 } |
4339 } | |
4340 else | |
771 | 4341 return 0; |
4342 return 1; | |
428 | 4343 } |
4344 | |
4345 static Lisp_Object | |
771 | 4346 gzip_getprop (Lisp_Object coding_system, Lisp_Object prop) |
428 | 4347 { |
771 | 4348 struct gzip_coding_system *data = |
4349 XCODING_SYSTEM_TYPE_DATA (coding_system, gzip); | |
4350 | |
4351 if (EQ (prop, Qlevel)) | |
428 | 4352 { |
771 | 4353 if (data->level == -1) |
4354 return Qdefault; | |
4355 return make_int (data->level); | |
428 | 4356 } |
771 | 4357 |
4358 return Qunbound; | |
428 | 4359 } |
4360 | |
4361 static void | |
771 | 4362 gzip_init_coding_stream (struct coding_stream *str) |
428 | 4363 { |
771 | 4364 struct gzip_coding_stream *data = CODING_STREAM_TYPE_DATA (str, gzip); |
4365 if (data->stream_initted) | |
428 | 4366 { |
771 | 4367 if (str->direction == CODING_DECODE) |
4368 inflateEnd (&data->stream); | |
4369 else | |
4370 deflateEnd (&data->stream); | |
4371 data->stream_initted = 0; | |
428 | 4372 } |
771 | 4373 data->reached_eof = 0; |
428 | 4374 } |
4375 | |
4376 static void | |
771 | 4377 gzip_rewind_coding_stream (struct coding_stream *str) |
428 | 4378 { |
771 | 4379 gzip_init_coding_stream (str); |
428 | 4380 } |
4381 | |
771 | 4382 static Bytecount |
4383 gzip_convert (struct coding_stream *str, | |
4384 const UExtbyte *src, | |
4385 unsigned_char_dynarr *dst, Bytecount n) | |
428 | 4386 { |
771 | 4387 struct gzip_coding_stream *data = CODING_STREAM_TYPE_DATA (str, gzip); |
4388 int zerr; | |
4389 if (str->direction == CODING_DECODE) | |
428 | 4390 { |
771 | 4391 if (data->reached_eof) |
4392 return n; /* eat the data */ | |
4393 | |
4394 if (!data->stream_initted) | |
428 | 4395 { |
771 | 4396 xzero (data->stream); |
4397 if (inflateInit (&data->stream) != Z_OK) | |
4398 return LSTREAM_ERROR; | |
4399 data->stream_initted = 1; | |
428 | 4400 } |
771 | 4401 |
4402 data->stream.next_in = (Bytef *) src; | |
4403 data->stream.avail_in = n; | |
4404 | |
4405 /* Normally we stop when we've fed all data to the decompressor; but | |
4406 if we're at the end of the input, and the decompressor hasn't | |
4407 reported EOF, we need to keep going, as there might be more output | |
4408 to generate. Z_OK from the decompressor means input was processed | |
4409 or output was generated; if neither, we break out of the loop. | |
4410 Other return values are: | |
4411 | |
4412 Z_STREAM_END EOF from decompressor | |
4413 Z_DATA_ERROR Corrupted data | |
4414 Z_BUF_ERROR No progress possible (this should happen if | |
4415 we try to feed it an incomplete file) | |
4416 Z_MEM_ERROR Out of memory | |
4417 Z_STREAM_ERROR (should never happen) | |
4418 Z_NEED_DICT (#### when will this happen?) | |
4419 */ | |
4420 while (data->stream.avail_in > 0 || str->eof) | |
4421 { | |
4422 /* Reserve an output buffer of the same size as the input buffer; | |
4423 if that's not enough, we keep reserving the same size. */ | |
4424 Bytecount reserved = n; | |
4425 Dynarr_add_many (dst, 0, reserved); | |
4426 /* Careful here! Don't retrieve the pointer until after | |
4427 reserving the space, or it might be bogus */ | |
4428 data->stream.next_out = | |
4429 Dynarr_atp (dst, Dynarr_length (dst) - reserved); | |
4430 data->stream.avail_out = reserved; | |
4431 zerr = inflate (&data->stream, Z_NO_FLUSH); | |
4432 /* Lop off the unused portion */ | |
4433 Dynarr_set_size (dst, Dynarr_length (dst) - data->stream.avail_out); | |
4434 if (zerr != Z_OK) | |
4435 break; | |
4436 } | |
4437 | |
4438 if (zerr == Z_STREAM_END) | |
4439 data->reached_eof = 1; | |
4440 | |
4441 if ((Bytecount) data->stream.avail_in < n) | |
4442 return n - data->stream.avail_in; | |
4443 | |
4444 if (zerr == Z_OK || zerr == Z_STREAM_END) | |
4445 return 0; | |
4446 | |
4447 return LSTREAM_ERROR; | |
428 | 4448 } |
4449 else | |
4450 { | |
771 | 4451 if (!data->stream_initted) |
4452 { | |
4453 int level = XCODING_SYSTEM_GZIP_LEVEL (str->codesys); | |
4454 xzero (data->stream); | |
4455 if (deflateInit (&data->stream, | |
4456 level == -1 ? Z_DEFAULT_COMPRESSION : level) != | |
4457 Z_OK) | |
4458 return LSTREAM_ERROR; | |
4459 data->stream_initted = 1; | |
428 | 4460 } |
771 | 4461 |
4462 data->stream.next_in = (Bytef *) src; | |
4463 data->stream.avail_in = n; | |
4464 | |
4465 /* Normally we stop when we've fed all data to the compressor; but if | |
4466 we're at the end of the input, and the compressor hasn't reported | |
4467 EOF, we need to keep going, as there might be more output to | |
4468 generate. (To signal EOF on our end, we set the FLUSH parameter | |
4469 to Z_FINISH; when all data is output, Z_STREAM_END will be | |
4470 returned.) Z_OK from the compressor means input was processed or | |
4471 output was generated; if neither, we break out of the loop. Other | |
4472 return values are: | |
4473 | |
4474 Z_STREAM_END EOF from compressor | |
4475 Z_BUF_ERROR No progress possible (should never happen) | |
4476 Z_STREAM_ERROR (should never happen) | |
4477 */ | |
4478 while (data->stream.avail_in > 0 || str->eof) | |
4479 { | |
4480 /* Reserve an output buffer of the same size as the input buffer; | |
4481 if that's not enough, we keep reserving the same size. */ | |
4482 Bytecount reserved = n; | |
4483 Dynarr_add_many (dst, 0, reserved); | |
4484 /* Careful here! Don't retrieve the pointer until after | |
4485 reserving the space, or it might be bogus */ | |
4486 data->stream.next_out = | |
4487 Dynarr_atp (dst, Dynarr_length (dst) - reserved); | |
4488 data->stream.avail_out = reserved; | |
4489 zerr = | |
4490 deflate (&data->stream, | |
4491 str->eof ? Z_FINISH : Z_NO_FLUSH); | |
4492 /* Lop off the unused portion */ | |
4493 Dynarr_set_size (dst, Dynarr_length (dst) - data->stream.avail_out); | |
4494 if (zerr != Z_OK) | |
4495 break; | |
4496 } | |
4497 | |
4498 if ((Bytecount) data->stream.avail_in < n) | |
4499 return n - data->stream.avail_in; | |
4500 | |
4501 if (zerr == Z_OK || zerr == Z_STREAM_END) | |
4502 return 0; | |
4503 | |
4504 return LSTREAM_ERROR; | |
428 | 4505 } |
4506 } | |
4507 | |
771 | 4508 #endif /* HAVE_ZLIB */ |
428 | 4509 |
4510 | |
4511 /************************************************************************/ | |
4512 /* Initialization */ | |
4513 /************************************************************************/ | |
4514 | |
4515 void | |
4516 syms_of_file_coding (void) | |
4517 { | |
442 | 4518 INIT_LRECORD_IMPLEMENTATION (coding_system); |
4519 | |
771 | 4520 DEFSUBR (Fvalid_coding_system_type_p); |
4521 DEFSUBR (Fcoding_system_type_list); | |
428 | 4522 DEFSUBR (Fcoding_system_p); |
4303 | 4523 DEFSUBR (Fautoload_coding_system); |
428 | 4524 DEFSUBR (Ffind_coding_system); |
4525 DEFSUBR (Fget_coding_system); | |
4526 DEFSUBR (Fcoding_system_list); | |
4527 DEFSUBR (Fcoding_system_name); | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4528 DEFSUBR (Fmake_coding_system_internal); |
428 | 4529 DEFSUBR (Fcopy_coding_system); |
440 | 4530 DEFSUBR (Fcoding_system_canonical_name_p); |
4531 DEFSUBR (Fcoding_system_alias_p); | |
4532 DEFSUBR (Fcoding_system_aliasee); | |
428 | 4533 DEFSUBR (Fdefine_coding_system_alias); |
4534 DEFSUBR (Fsubsidiary_coding_system); | |
771 | 4535 DEFSUBR (Fcoding_system_base); |
4536 DEFSUBR (Fcoding_system_used_for_io); | |
428 | 4537 |
4538 DEFSUBR (Fcoding_system_type); | |
771 | 4539 DEFSUBR (Fcoding_system_description); |
428 | 4540 DEFSUBR (Fcoding_system_property); |
4541 | |
4542 DEFSUBR (Fcoding_category_list); | |
4543 DEFSUBR (Fset_coding_priority_list); | |
4544 DEFSUBR (Fcoding_priority_list); | |
4545 DEFSUBR (Fset_coding_category_system); | |
4546 DEFSUBR (Fcoding_category_system); | |
4547 | |
4548 DEFSUBR (Fdetect_coding_region); | |
4549 DEFSUBR (Fdecode_coding_region); | |
4550 DEFSUBR (Fencode_coding_region); | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4551 DEFSUBR (Fquery_coding_region); |
563 | 4552 DEFSYMBOL_MULTIWORD_PREDICATE (Qcoding_systemp); |
4553 DEFSYMBOL (Qno_conversion); | |
771 | 4554 DEFSYMBOL (Qconvert_eol); |
4555 DEFSYMBOL (Qconvert_eol_autodetect); | |
4556 DEFSYMBOL (Qconvert_eol_lf); | |
4557 DEFSYMBOL (Qconvert_eol_cr); | |
4558 DEFSYMBOL (Qconvert_eol_crlf); | |
563 | 4559 DEFSYMBOL (Qraw_text); |
771 | 4560 |
563 | 4561 DEFSYMBOL (Qmnemonic); |
4562 DEFSYMBOL (Qeol_type); | |
4563 DEFSYMBOL (Qpost_read_conversion); | |
4564 DEFSYMBOL (Qpre_write_conversion); | |
4565 | |
771 | 4566 DEFSYMBOL (Qtranslation_table_for_decode); |
4567 DEFSYMBOL (Qtranslation_table_for_encode); | |
4568 DEFSYMBOL (Qsafe_chars); | |
4569 DEFSYMBOL (Qsafe_charsets); | |
4570 DEFSYMBOL (Qmime_charset); | |
4571 DEFSYMBOL (Qvalid_codes); | |
4572 | |
563 | 4573 DEFSYMBOL (Qcr); |
4574 DEFSYMBOL (Qlf); | |
4575 DEFSYMBOL (Qcrlf); | |
4576 DEFSYMBOL (Qeol_cr); | |
4577 DEFSYMBOL (Qeol_lf); | |
4578 DEFSYMBOL (Qeol_crlf); | |
4579 DEFSYMBOL (Qencode); | |
4580 DEFSYMBOL (Qdecode); | |
428 | 4581 |
771 | 4582 DEFSYMBOL (Qnear_certainty); |
4583 DEFSYMBOL (Qquite_probable); | |
4584 DEFSYMBOL (Qsomewhat_likely); | |
1494 | 4585 DEFSYMBOL (Qslightly_likely); |
771 | 4586 DEFSYMBOL (Qas_likely_as_unlikely); |
4587 DEFSYMBOL (Qsomewhat_unlikely); | |
4588 DEFSYMBOL (Qquite_improbable); | |
4589 DEFSYMBOL (Qnearly_impossible); | |
4590 | |
4591 DEFSYMBOL (Qdo_eol); | |
4592 DEFSYMBOL (Qdo_coding); | |
4593 | |
4594 DEFSYMBOL (Qcanonicalize_after_coding); | |
4595 | |
4303 | 4596 DEFSYMBOL (Qposix_charset_to_coding_system_hash); |
4597 | |
771 | 4598 DEFSYMBOL (Qescape_quoted); |
4599 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4600 DEFSYMBOL (Qquery_coding_warning_face); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4601 DEFSYMBOL (Qaliases); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4602 DEFSYMBOL (Qcharset_skip_chars_string); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4603 |
771 | 4604 #ifdef HAVE_ZLIB |
4605 DEFSYMBOL (Qgzip); | |
4606 #endif | |
4607 | |
428 | 4608 } |
4609 | |
4610 void | |
4611 lstream_type_create_file_coding (void) | |
4612 { | |
771 | 4613 LSTREAM_HAS_METHOD (coding, reader); |
4614 LSTREAM_HAS_METHOD (coding, writer); | |
4615 LSTREAM_HAS_METHOD (coding, rewinder); | |
4616 LSTREAM_HAS_METHOD (coding, seekable_p); | |
4617 LSTREAM_HAS_METHOD (coding, marker); | |
4618 LSTREAM_HAS_METHOD (coding, flusher); | |
4619 LSTREAM_HAS_METHOD (coding, closer); | |
4620 LSTREAM_HAS_METHOD (coding, finalizer); | |
4621 } | |
4622 | |
4623 void | |
4624 coding_system_type_create (void) | |
4625 { | |
4626 int i; | |
4627 | |
4628 staticpro (&Vcoding_system_hash_table); | |
4629 Vcoding_system_hash_table = | |
4630 make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ); | |
4631 | |
4632 the_coding_system_type_entry_dynarr = Dynarr_new (coding_system_type_entry); | |
2367 | 4633 dump_add_root_block_ptr (&the_coding_system_type_entry_dynarr, |
771 | 4634 &csted_description); |
4635 | |
4636 Vcoding_system_type_list = Qnil; | |
4637 staticpro (&Vcoding_system_type_list); | |
4638 | |
4639 /* Initialize to something reasonable ... */ | |
4640 for (i = 0; i < MAX_DETECTOR_CATEGORIES; i++) | |
4641 { | |
4642 coding_category_system[i] = Qnil; | |
1204 | 4643 dump_add_root_lisp_object (&coding_category_system[i]); |
771 | 4644 coding_category_by_priority[i] = i; |
4645 } | |
4646 | |
4647 dump_add_opaque (coding_category_by_priority, | |
4648 sizeof (coding_category_by_priority)); | |
4649 | |
4650 all_coding_detectors = Dynarr_new2 (detector_dynarr, struct detector); | |
2367 | 4651 dump_add_root_block_ptr (&all_coding_detectors, |
771 | 4652 &detector_dynarr_description); |
4653 | |
4654 dump_add_opaque_int (&coding_system_tick); | |
4655 dump_add_opaque_int (&coding_detector_count); | |
4656 dump_add_opaque_int (&coding_detector_category_count); | |
4657 | |
4658 INITIALIZE_CODING_SYSTEM_TYPE (no_conversion, | |
4659 "no-conversion-coding-system-p"); | |
4660 CODING_SYSTEM_HAS_METHOD (no_conversion, convert); | |
4661 | |
4662 INITIALIZE_DETECTOR (no_conversion); | |
4663 DETECTOR_HAS_METHOD (no_conversion, detect); | |
4664 INITIALIZE_DETECTOR_CATEGORY (no_conversion, no_conversion); | |
4665 | |
4666 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (convert_eol, | |
4667 "convert-eol-coding-system-p"); | |
4668 CODING_SYSTEM_HAS_METHOD (convert_eol, print); | |
4669 CODING_SYSTEM_HAS_METHOD (convert_eol, convert); | |
4670 CODING_SYSTEM_HAS_METHOD (convert_eol, getprop); | |
4671 CODING_SYSTEM_HAS_METHOD (convert_eol, putprop); | |
4672 CODING_SYSTEM_HAS_METHOD (convert_eol, conversion_end_type); | |
4673 CODING_SYSTEM_HAS_METHOD (convert_eol, canonicalize_after_coding); | |
4674 CODING_SYSTEM_HAS_METHOD (convert_eol, init_coding_stream); | |
4675 | |
4676 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (undecided, | |
4677 "undecided-coding-system-p"); | |
4678 CODING_SYSTEM_HAS_METHOD (undecided, init); | |
4679 CODING_SYSTEM_HAS_METHOD (undecided, mark); | |
4680 CODING_SYSTEM_HAS_METHOD (undecided, print); | |
4681 CODING_SYSTEM_HAS_METHOD (undecided, convert); | |
4682 CODING_SYSTEM_HAS_METHOD (undecided, putprop); | |
4683 CODING_SYSTEM_HAS_METHOD (undecided, getprop); | |
4684 CODING_SYSTEM_HAS_METHOD (undecided, init_coding_stream); | |
4685 CODING_SYSTEM_HAS_METHOD (undecided, rewind_coding_stream); | |
4686 CODING_SYSTEM_HAS_METHOD (undecided, finalize_coding_stream); | |
4687 CODING_SYSTEM_HAS_METHOD (undecided, mark_coding_stream); | |
4688 CODING_SYSTEM_HAS_METHOD (undecided, canonicalize); | |
4689 CODING_SYSTEM_HAS_METHOD (undecided, canonicalize_after_coding); | |
4690 | |
4691 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (chain, "chain-coding-system-p"); | |
4692 | |
4693 CODING_SYSTEM_HAS_METHOD (chain, print); | |
4694 CODING_SYSTEM_HAS_METHOD (chain, canonicalize); | |
4695 CODING_SYSTEM_HAS_METHOD (chain, init); | |
4696 CODING_SYSTEM_HAS_METHOD (chain, mark); | |
4697 CODING_SYSTEM_HAS_METHOD (chain, mark_coding_stream); | |
4698 CODING_SYSTEM_HAS_METHOD (chain, convert); | |
4699 CODING_SYSTEM_HAS_METHOD (chain, rewind_coding_stream); | |
4700 CODING_SYSTEM_HAS_METHOD (chain, finalize_coding_stream); | |
4701 CODING_SYSTEM_HAS_METHOD (chain, finalize); | |
4702 CODING_SYSTEM_HAS_METHOD (chain, putprop); | |
4703 CODING_SYSTEM_HAS_METHOD (chain, getprop); | |
4704 CODING_SYSTEM_HAS_METHOD (chain, conversion_end_type); | |
4705 CODING_SYSTEM_HAS_METHOD (chain, canonicalize_after_coding); | |
4706 | |
4707 #ifdef DEBUG_XEMACS | |
4708 INITIALIZE_CODING_SYSTEM_TYPE (internal, "internal-coding-system-p"); | |
4709 CODING_SYSTEM_HAS_METHOD (internal, convert); | |
4710 #endif | |
4711 | |
4712 #ifdef HAVE_ZLIB | |
4713 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (gzip, "gzip-coding-system-p"); | |
4714 CODING_SYSTEM_HAS_METHOD (gzip, conversion_end_type); | |
4715 CODING_SYSTEM_HAS_METHOD (gzip, convert); | |
4716 CODING_SYSTEM_HAS_METHOD (gzip, init); | |
4717 CODING_SYSTEM_HAS_METHOD (gzip, print); | |
4718 CODING_SYSTEM_HAS_METHOD (gzip, init_coding_stream); | |
4719 CODING_SYSTEM_HAS_METHOD (gzip, rewind_coding_stream); | |
4720 CODING_SYSTEM_HAS_METHOD (gzip, putprop); | |
4721 CODING_SYSTEM_HAS_METHOD (gzip, getprop); | |
4722 #endif | |
4723 } | |
4724 | |
4725 void | |
4726 reinit_coding_system_type_create (void) | |
4727 { | |
4728 REINITIALIZE_CODING_SYSTEM_TYPE (no_conversion); | |
4729 REINITIALIZE_CODING_SYSTEM_TYPE (convert_eol); | |
4730 REINITIALIZE_CODING_SYSTEM_TYPE (undecided); | |
4731 REINITIALIZE_CODING_SYSTEM_TYPE (chain); | |
4732 #if 0 | |
4733 REINITIALIZE_CODING_SYSTEM_TYPE (text_file_wrapper); | |
4734 #endif /* 0 */ | |
4735 #ifdef DEBUG_XEMACS | |
4736 REINITIALIZE_CODING_SYSTEM_TYPE (internal); | |
4737 #endif | |
4738 #ifdef HAVE_ZLIB | |
4739 REINITIALIZE_CODING_SYSTEM_TYPE (gzip); | |
4740 #endif | |
4741 } | |
4742 | |
4743 void | |
4744 reinit_vars_of_file_coding (void) | |
4745 { | |
428 | 4746 } |
4747 | |
4748 void | |
4749 vars_of_file_coding (void) | |
4750 { | |
771 | 4751 /* We always have file-coding support */ |
428 | 4752 Fprovide (intern ("file-coding")); |
4753 | |
1347 | 4754 QScoding_system_cookie = build_string (";;;###coding system: "); |
4755 staticpro (&QScoding_system_cookie); | |
4756 | |
1242 | 4757 #ifdef HAVE_DEFAULT_EOL_DETECTION |
2297 | 4758 /* #### Find a more appropriate place for this comment. |
4759 WARNING: The existing categories are intimately tied to the function | |
1242 | 4760 `coding-system-category' in coding.el. If you change a category, or |
4761 change the layout of any coding system associated with a category, you | |
4762 need to check that function and make sure it's written properly. */ | |
4763 | |
4764 Fprovide (intern ("unix-default-eol-detection")); | |
4765 #endif | |
4766 | |
428 | 4767 DEFVAR_LISP ("keyboard-coding-system", &Vkeyboard_coding_system /* |
3142 | 4768 Default coding system used for TTY and X11 keyboard input. |
4769 Under X11, used only to interpet the character for a key event when that | |
4770 event has a KeySym of NoSymbol but does have an associated string keysym, | |
4771 something that's seen with input methods. | |
4772 | |
4773 If you need to set these things to different coding systems, call the | |
4774 function `set-console-tty-coding-system' for the TTY and use this variable | |
4775 for X11. | |
428 | 4776 */ ); |
4777 Vkeyboard_coding_system = Qnil; | |
4778 | |
4779 DEFVAR_LISP ("terminal-coding-system", &Vterminal_coding_system /* | |
4780 Coding system used for TTY display output. | |
4781 Not used under a windowing system. | |
4782 */ ); | |
4783 Vterminal_coding_system = Qnil; | |
4784 | |
4785 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read /* | |
440 | 4786 Overriding coding system used when reading from a file or process. |
4787 You should bind this variable with `let', but do not set it globally. | |
4788 If this is non-nil, it specifies the coding system that will be used | |
4789 to decode input on read operations, such as from a file or process. | |
4790 It overrides `buffer-file-coding-system-for-read', | |
428 | 4791 `insert-file-contents-pre-hook', etc. Use those variables instead of |
440 | 4792 this one for permanent changes to the environment. */ ); |
428 | 4793 Vcoding_system_for_read = Qnil; |
4794 | |
4795 DEFVAR_LISP ("coding-system-for-write", | |
4796 &Vcoding_system_for_write /* | |
440 | 4797 Overriding coding system used when writing to a file or process. |
4798 You should bind this variable with `let', but do not set it globally. | |
4799 If this is non-nil, it specifies the coding system that will be used | |
4800 to encode output for write operations, such as to a file or process. | |
4801 It overrides `buffer-file-coding-system', `write-region-pre-hook', etc. | |
4802 Use those variables instead of this one for permanent changes to the | |
4803 environment. */ ); | |
428 | 4804 Vcoding_system_for_write = Qnil; |
4805 | |
4806 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system /* | |
4807 Coding system used to convert pathnames when accessing files. | |
4808 */ ); | |
4809 Vfile_name_coding_system = Qnil; | |
4810 | |
4811 DEFVAR_BOOL ("enable-multibyte-characters", &enable_multibyte_characters /* | |
771 | 4812 Setting this has no effect. It is purely for FSF compatibility. |
428 | 4813 */ ); |
4814 enable_multibyte_characters = 1; | |
771 | 4815 |
4816 Vchain_canonicalize_hash_table = | |
4817 make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL); | |
4818 staticpro (&Vchain_canonicalize_hash_table); | |
4819 | |
4820 #ifdef DEBUG_XEMACS | |
4821 DEFVAR_LISP ("debug-coding-detection", &Vdebug_coding_detection /* | |
4822 If non-nil, display debug information about detection operations in progress. | |
4823 Information is displayed on stderr. | |
4824 */ ); | |
4825 Vdebug_coding_detection = Qnil; | |
4826 #endif | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4827 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4828 #ifdef MULE |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4829 Vdefault_query_coding_region_chartab_cache |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4830 = make_lisp_hash_table (25, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4831 staticpro (&Vdefault_query_coding_region_chartab_cache); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4832 #endif |
428 | 4833 } |
4834 | |
2297 | 4835 /* #### reformat this for consistent appearance? */ |
4836 | |
428 | 4837 void |
4838 complex_vars_of_file_coding (void) | |
4839 { | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4840 Fmake_coding_system_internal |
771 | 4841 (Qconvert_eol_cr, Qconvert_eol, |
4842 build_msg_string ("Convert CR to LF"), | |
4843 nconc2 (list6 (Qdocumentation, | |
4844 build_msg_string ( | |
4845 "Converts CR (used to mark the end of a line on Macintosh systems) to LF\n" | |
4846 "(used internally and under Unix to mark the end of a line)."), | |
4847 Qmnemonic, build_string ("CR->LF"), | |
4848 Qsubtype, Qcr), | |
4849 /* VERY IMPORTANT! Tell make-coding-system not to generate | |
4850 subsidiaries -- it needs the coding systems we're creating | |
4851 to do so! */ | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4852 list4 (Qeol_type, Qlf, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4853 Qsafe_charsets, Qt))); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4854 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4855 Fmake_coding_system_internal |
771 | 4856 (Qconvert_eol_lf, Qconvert_eol, |
4857 build_msg_string ("Convert LF to LF (do nothing)"), | |
4858 nconc2 (list6 (Qdocumentation, | |
4859 build_msg_string ( | |
4860 "Do nothing."), | |
4861 Qmnemonic, build_string ("LF->LF"), | |
4862 Qsubtype, Qlf), | |
4863 /* VERY IMPORTANT! Tell make-coding-system not to generate | |
4864 subsidiaries -- it needs the coding systems we're creating | |
4865 to do so! */ | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4866 list4 (Qeol_type, Qlf, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4867 Qsafe_charsets, Qt))); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4868 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4869 Fmake_coding_system_internal |
771 | 4870 (Qconvert_eol_crlf, Qconvert_eol, |
4871 build_msg_string ("Convert CRLF to LF"), | |
4872 nconc2 (list6 (Qdocumentation, | |
4873 build_msg_string ( | |
4874 "Converts CR+LF (used to mark the end of a line on Macintosh systems) to LF\n" | |
4875 "(used internally and under Unix to mark the end of a line)."), | |
4876 Qmnemonic, build_string ("CRLF->LF"), | |
4877 Qsubtype, Qcrlf), | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4878 |
771 | 4879 /* VERY IMPORTANT! Tell make-coding-system not to generate |
4880 subsidiaries -- it needs the coding systems we're creating | |
4881 to do so! */ | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4882 list4 (Qeol_type, Qlf, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4883 Qsafe_charsets, Qt))); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4884 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4885 Fmake_coding_system_internal |
771 | 4886 (Qconvert_eol_autodetect, Qconvert_eol, |
4887 build_msg_string ("Autodetect EOL type"), | |
4888 nconc2 (list6 (Qdocumentation, | |
4889 build_msg_string ( | |
4890 "Autodetect the end-of-line type."), | |
4891 Qmnemonic, build_string ("Auto-EOL"), | |
793 | 4892 Qsubtype, Qnil), |
771 | 4893 /* VERY IMPORTANT! Tell make-coding-system not to generate |
4894 subsidiaries -- it needs the coding systems we're creating | |
4895 to do so! */ | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4896 list4 (Qeol_type, Qlf, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4897 Qsafe_charsets, Qt))); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4898 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4899 Fmake_coding_system_internal |
771 | 4900 (Qundecided, Qundecided, |
4901 build_msg_string ("Undecided (auto-detect)"), | |
4902 nconc2 (list4 (Qdocumentation, | |
4903 build_msg_string | |
4904 ("Automatically detects the correct encoding."), | |
4905 Qmnemonic, build_string ("Auto")), | |
4906 list6 (Qdo_eol, Qt, Qdo_coding, Qt, | |
4907 /* We do EOL detection ourselves so we don't need to be | |
4908 wrapped in an EOL detector. (It doesn't actually hurt, | |
4909 though, I don't think.) */ | |
4910 Qeol_type, Qlf))); | |
4911 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4912 Fmake_coding_system_internal |
771 | 4913 (intern ("undecided-dos"), Qundecided, |
4914 build_msg_string ("Undecided (auto-detect) (CRLF)"), | |
4915 nconc2 (list4 (Qdocumentation, | |
4916 build_msg_string | |
4917 ("Automatically detects the correct encoding; EOL type of CRLF forced."), | |
4918 Qmnemonic, build_string ("Auto")), | |
4919 list4 (Qdo_coding, Qt, | |
4920 Qeol_type, Qcrlf))); | |
4921 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4922 Fmake_coding_system_internal |
771 | 4923 (intern ("undecided-unix"), Qundecided, |
4924 build_msg_string ("Undecided (auto-detect) (LF)"), | |
4925 nconc2 (list4 (Qdocumentation, | |
4926 build_msg_string | |
4927 ("Automatically detects the correct encoding; EOL type of LF forced."), | |
4928 Qmnemonic, build_string ("Auto")), | |
4929 list4 (Qdo_coding, Qt, | |
4930 Qeol_type, Qlf))); | |
4931 | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4932 Fmake_coding_system_internal |
771 | 4933 (intern ("undecided-mac"), Qundecided, |
4934 build_msg_string ("Undecided (auto-detect) (CR)"), | |
4935 nconc2 (list4 (Qdocumentation, | |
4936 build_msg_string | |
4937 ("Automatically detects the correct encoding; EOL type of CR forced."), | |
4938 Qmnemonic, build_string ("Auto")), | |
4939 list4 (Qdo_coding, Qt, | |
4940 Qeol_type, Qcr))); | |
4941 | |
428 | 4942 /* Need to create this here or we're really screwed. */ |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4943 Fmake_coding_system_internal |
428 | 4944 (Qraw_text, Qno_conversion, |
771 | 4945 build_msg_string ("Raw Text"), |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4946 nconc2 (list4 (Qdocumentation, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4947 build_msg_string ("Raw text converts only line-break " |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4948 "codes, and acts otherwise like " |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4949 "`binary'."), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4950 Qmnemonic, build_string ("Raw")), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4951 #ifdef MULE |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4952 list2 (Qsafe_charsets, list3 (Vcharset_ascii, Vcharset_control_1, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4953 Vcharset_latin_iso8859_1)))); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4954 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4955 #else |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4956 Qnil)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4957 #endif |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4958 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4959 Fmake_coding_system_internal |
428 | 4960 (Qbinary, Qno_conversion, |
771 | 4961 build_msg_string ("Binary"), |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4962 nconc2 (list6 (Qdocumentation, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4963 build_msg_string ( |
771 | 4964 "This coding system is as close as it comes to doing no conversion.\n" |
4965 "On input, each byte is converted directly into the character\n" | |
4966 "with the corresponding code -- i.e. from the `ascii', `control-1',\n" | |
4967 "or `latin-1' character sets. On output, these characters are\n" | |
4968 "converted back to the corresponding bytes, and other characters\n" | |
4969 "are converted to the default character, i.e. `~'."), | |
4690
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4970 Qeol_type, Qlf, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4971 Qmnemonic, build_string ("Binary")), |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4972 #ifdef MULE |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4973 list2 (Qsafe_charsets, list3 (Vcharset_ascii, Vcharset_control_1, |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4974 Vcharset_latin_iso8859_1)))); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4975 |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4976 #else |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4977 Qnil)); |
257b468bf2ca
Move the #'query-coding-region implementation to C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4647
diff
changeset
|
4978 #endif |
428 | 4979 |
771 | 4980 /* Formerly aliased to raw-text! Completely bogus and not even the same |
4981 as FSF Emacs. */ | |
4982 Fdefine_coding_system_alias (Qno_conversion, Qbinary); | |
4983 Fdefine_coding_system_alias (intern ("no-conversion-unix"), | |
4984 intern ("raw-text-unix")); | |
4985 Fdefine_coding_system_alias (intern ("no-conversion-dos"), | |
4986 intern ("raw-text-dos")); | |
4987 Fdefine_coding_system_alias (intern ("no-conversion-mac"), | |
4988 intern ("raw-text-mac")); | |
4989 | |
1318 | 4990 /* These three below will get their defaults set correctly |
4991 in code-init.el. We init them now so we can handle stuff at dump | |
771 | 4992 time before we get to code-init.el. */ |
1318 | 4993 Fdefine_coding_system_alias (Qnative, Qbinary); |
440 | 4994 Fdefine_coding_system_alias (Qterminal, Qbinary); |
4995 Fdefine_coding_system_alias (Qkeyboard, Qbinary); | |
4996 | |
1318 | 4997 Fdefine_coding_system_alias (Qfile_name, Qnative); |
771 | 4998 Fdefine_coding_system_alias (Qidentity, Qconvert_eol_lf); |
4999 | |
428 | 5000 /* Need this for bootstrapping */ |
771 | 5001 coding_category_system[detector_category_no_conversion] = |
428 | 5002 Fget_coding_system (Qraw_text); |
5003 } |