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