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