Merged from emacs@sv.gnu.org. Last-minute emacsclient rewrites be damned!
[bpt/emacs.git] / lisp / international / mule.el
1 ;;; mule.el --- basic commands for mulitilingual environment
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
8
9 ;; Keywords: mule, multilingual, character set, coding system
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (defconst mule-version "5.0 (SAKAKI)" "\
33 Version number and name of this version of MULE (multilingual environment).")
34
35 (defconst mule-version-date "1999.12.7" "\
36 Distribution date of this version of MULE (multilingual environment).")
37
38 (defun load-with-code-conversion (fullname file &optional noerror nomessage)
39 "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
40 The file contents are decoded before evaluation if necessary.
41 If optional third arg NOERROR is non-nil,
42 report no error if FILE doesn't exist.
43 Print messages at start and end of loading unless
44 optional fourth arg NOMESSAGE is non-nil.
45 Return t if file exists."
46 (if (null (file-readable-p fullname))
47 (and (null noerror)
48 (signal 'file-error (list "Cannot open load file" file)))
49 ;; Read file with code conversion, and then eval.
50 (let* ((buffer
51 ;; To avoid any autoloading, set default-major-mode to
52 ;; fundamental-mode.
53 ;; So that we don't get completely screwed if the
54 ;; file is encoded in some complicated character set,
55 ;; read it with real decoding, as a multibyte buffer,
56 ;; even if this is a --unibyte Emacs session.
57 (let ((default-major-mode 'fundamental-mode)
58 (default-enable-multibyte-characters t))
59 ;; We can't use `generate-new-buffer' because files.el
60 ;; is not yet loaded.
61 (get-buffer-create (generate-new-buffer-name " *load*"))))
62 (load-in-progress t)
63 (source (save-match-data (string-match "\\.el\\'" fullname))))
64 (unless nomessage
65 (if source
66 (message "Loading %s (source)..." file)
67 (message "Loading %s..." file)))
68 (when purify-flag
69 (push file preloaded-file-list))
70 (unwind-protect
71 (let ((load-file-name fullname)
72 (set-auto-coding-for-load t)
73 (inhibit-file-name-operation nil))
74 (with-current-buffer buffer
75 ;; Don't let deactivate-mark remain set.
76 (let (deactivate-mark)
77 (insert-file-contents fullname))
78 ;; If the loaded file was inserted with no-conversion or
79 ;; raw-text coding system, make the buffer unibyte.
80 ;; Otherwise, eval-buffer might try to interpret random
81 ;; binary junk as multibyte characters.
82 (if (and enable-multibyte-characters
83 (or (eq (coding-system-type last-coding-system-used) 5)
84 (eq last-coding-system-used 'no-conversion)))
85 (set-buffer-multibyte nil))
86 ;; Make `kill-buffer' quiet.
87 (set-buffer-modified-p nil))
88 ;; Have the original buffer current while we eval.
89 (eval-buffer buffer nil
90 ;; This is compatible with what `load' does.
91 (if purify-flag file fullname)
92 ;; If this Emacs is running with --unibyte,
93 ;; convert multibyte strings to unibyte
94 ;; after reading them.
95 ;; (not default-enable-multibyte-characters)
96 nil t
97 ))
98 (let (kill-buffer-hook kill-buffer-query-functions)
99 (kill-buffer buffer)))
100 (unless purify-flag
101 (do-after-load-evaluation fullname))
102
103 (unless (or nomessage noninteractive)
104 (if source
105 (message "Loading %s (source)...done" file)
106 (message "Loading %s...done" file)))
107 t)))
108
109 ;; API (Application Program Interface) for charsets.
110
111 (defsubst charset-quoted-standard-p (obj)
112 "Return t if OBJ is a quoted symbol, and is the name of a standard charset."
113 (and (listp obj) (eq (car obj) 'quote)
114 (symbolp (car-safe (cdr obj)))
115 (let ((vector (get (car-safe (cdr obj)) 'charset)))
116 (and (vectorp vector)
117 (< (aref vector 0) 160)))))
118
119 (defsubst charsetp (object)
120 "Return t if OBJECT is a charset."
121 (and (symbolp object) (vectorp (get object 'charset))))
122
123 (defsubst charset-info (charset)
124 "Return a vector of information of CHARSET.
125 The elements of the vector are:
126 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
127 LEADING-CODE-BASE, LEADING-CODE-EXT,
128 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
129 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
130 PLIST,
131 where
132 CHARSET-ID (integer) is the identification number of the charset.
133 BYTES (integer) is the length of multi-byte form of a character in
134 the charset: one of 1, 2, 3, and 4.
135 DIMENSION (integer) is the number of bytes to represent a character of
136 the charset: 1 or 2.
137 CHARS (integer) is the number of characters in a dimension: 94 or 96.
138 WIDTH (integer) is the number of columns a character in the charset
139 occupies on the screen: one of 0, 1, and 2.
140 DIRECTION (integer) is the rendering direction of characters in the
141 charset when rendering. If 0, render from left to right, else
142 render from right to left.
143 LEADING-CODE-BASE (integer) is the base leading-code for the
144 charset.
145 LEADING-CODE-EXT (integer) is the extended leading-code for the
146 charset. All charsets of less than 0xA0 has the value 0.
147 ISO-FINAL-CHAR (character) is the final character of the
148 corresponding ISO 2022 charset. If the charset is not assigned
149 any final character, the value is -1.
150 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
151 while encoding to variants of ISO 2022 coding system, one of the
152 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
153 If the charset is not assigned any final character, the value is -1.
154 REVERSE-CHARSET (integer) is the charset which differs only in
155 LEFT-TO-RIGHT value from the charset. If there's no such a
156 charset, the value is -1.
157 SHORT-NAME (string) is the short name to refer to the charset.
158 LONG-NAME (string) is the long name to refer to the charset
159 DESCRIPTION (string) is the description string of the charset.
160 PLIST (property list) may contain any type of information a user
161 want to put and get by functions `put-charset-property' and
162 `get-charset-property' respectively."
163 (get charset 'charset))
164
165 ;; It is better not to use backquote in this file,
166 ;; because that makes a bootstrapping problem
167 ;; if you need to recompile all the Lisp files using interpreted code.
168
169 (defmacro charset-id (charset)
170 "Return charset identification number of CHARSET."
171 (if (charset-quoted-standard-p charset)
172 (aref (charset-info (nth 1 charset)) 0)
173 (list 'aref (list 'charset-info charset) 0)))
174
175 (defmacro charset-bytes (charset)
176 "Return bytes of CHARSET.
177 See the function `charset-info' for more detail."
178 (if (charset-quoted-standard-p charset)
179 (aref (charset-info (nth 1 charset)) 1)
180 (list 'aref (list 'charset-info charset) 1)))
181
182 (defmacro charset-dimension (charset)
183 "Return dimension of CHARSET.
184 See the function `charset-info' for more detail."
185 (if (charset-quoted-standard-p charset)
186 (aref (charset-info (nth 1 charset)) 2)
187 (list 'aref (list 'charset-info charset) 2)))
188
189 (defmacro charset-chars (charset)
190 "Return character numbers contained in a dimension of CHARSET.
191 See the function `charset-info' for more detail."
192 (if (charset-quoted-standard-p charset)
193 (aref (charset-info (nth 1 charset)) 3)
194 (list 'aref (list 'charset-info charset) 3)))
195
196 (defmacro charset-width (charset)
197 "Return width (how many column occupied on a screen) of CHARSET.
198 See the function `charset-info' for more detail."
199 (if (charset-quoted-standard-p charset)
200 (aref (charset-info (nth 1 charset)) 4)
201 (list 'aref (list 'charset-info charset) 4)))
202
203 (defmacro charset-direction (charset)
204 "Return direction of CHARSET.
205 See the function `charset-info' for more detail."
206 (if (charset-quoted-standard-p charset)
207 (aref (charset-info (nth 1 charset)) 5)
208 (list 'aref (list 'charset-info charset) 5)))
209
210 (defmacro charset-iso-final-char (charset)
211 "Return final char of CHARSET.
212 See the function `charset-info' for more detail."
213 (if (charset-quoted-standard-p charset)
214 (aref (charset-info (nth 1 charset)) 8)
215 (list 'aref (list 'charset-info charset) 8)))
216
217 (defmacro charset-iso-graphic-plane (charset)
218 "Return graphic plane of CHARSET.
219 See the function `charset-info' for more detail."
220 (if (charset-quoted-standard-p charset)
221 (aref (charset-info (nth 1 charset)) 9)
222 (list 'aref (list 'charset-info charset) 9)))
223
224 (defmacro charset-reverse-charset (charset)
225 "Return reverse charset of CHARSET.
226 See the function `charset-info' for more detail."
227 (if (charset-quoted-standard-p charset)
228 (aref (charset-info (nth 1 charset)) 10)
229 (list 'aref (list 'charset-info charset) 10)))
230
231 (defmacro charset-short-name (charset)
232 "Return short name of CHARSET.
233 See the function `charset-info' for more detail."
234 (if (charset-quoted-standard-p charset)
235 (aref (charset-info (nth 1 charset)) 11)
236 (list 'aref (list 'charset-info charset) 11)))
237
238 (defmacro charset-long-name (charset)
239 "Return long name of CHARSET.
240 See the function `charset-info' for more detail."
241 (if (charset-quoted-standard-p charset)
242 (aref (charset-info (nth 1 charset)) 12)
243 (list 'aref (list 'charset-info charset) 12)))
244
245 (defmacro charset-description (charset)
246 "Return description of CHARSET.
247 See the function `charset-info' for more detail."
248 (if (charset-quoted-standard-p charset)
249 (aref (charset-info (nth 1 charset)) 13)
250 (list 'aref (list 'charset-info charset) 13)))
251
252 (defmacro charset-plist (charset)
253 "Return list charset property of CHARSET.
254 See the function `charset-info' for more detail."
255 (list 'aref
256 (if (charset-quoted-standard-p charset)
257 (charset-info (nth 1 charset))
258 (list 'charset-info charset))
259 14))
260
261 (defun set-charset-plist (charset plist)
262 "Set CHARSET's property list to PLIST, and return PLIST."
263 (aset (charset-info charset) 14 plist))
264
265 (defun make-char (charset &optional code1 code2)
266 "Return a character of CHARSET whose position codes are CODE1 and CODE2.
267 CODE1 and CODE2 are optional, but if you don't supply
268 sufficient position codes, return a generic character which stands for
269 all characters or group of characters in the character set.
270 A generic character can be used to index a char table (e.g. `syntax-table').
271
272 Such character sets as ascii, eight-bit-control, and eight-bit-graphic
273 don't have corresponding generic characters. If CHARSET is one of
274 them and you don't supply CODE1, return the character of the smallest
275 code in CHARSET.
276
277 If CODE1 or CODE2 are invalid (out of range), this function signals an
278 error. However, the eighth bit of both CODE1 and CODE2 is zeroed
279 before they are used to index CHARSET. Thus you may use, say, the
280 actual ISO 8859 character code rather than subtracting 128, as you
281 would need to index the corresponding Emacs charset."
282 (make-char-internal (charset-id charset) code1 code2))
283
284 (put 'make-char 'byte-compile
285 (lambda (form)
286 (let ((charset (nth 1 form)))
287 (byte-compile-normal-call
288 (cons 'make-char-internal
289 (cons (if (charset-quoted-standard-p charset)
290 (charset-id (nth 1 charset))
291 (list 'charset-id charset))
292 (nthcdr 2 form)))))))
293
294 (defun charset-list ()
295 "Return list of charsets ever defined.
296
297 This function is provided for backward compatibility.
298 Now we have the variable `charset-list'."
299 charset-list)
300
301 (defsubst generic-char-p (char)
302 "Return t if and only if CHAR is a generic character.
303 See also the documentation of `make-char'."
304 (and (>= char 0400)
305 (let ((l (split-char char)))
306 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
307 (not (eq (car l) 'composition))))))
308
309 (defun decode-char (ccs code-point &optional restriction)
310 "Return character specified by coded character set CCS and CODE-POINT in it.
311 Return nil if such a character is not supported.
312 Currently the only supported coded character set is `ucs' (ISO/IEC
313 10646: Universal Multi-Octet Coded Character Set), and the result is
314 translated through the translation-table named
315 `utf-translation-table-for-decode', or through the
316 translation-hash-table named `utf-subst-table-for-decode'
317 \(if `utf-translate-cjk-mode' is non-nil).
318
319 Optional argument RESTRICTION specifies a way to map the pair of CCS
320 and CODE-POINT to a character. Currently not supported and just ignored."
321 (cond
322 ((eq ccs 'ucs)
323 (or (and utf-translate-cjk-mode
324 (utf-lookup-subst-table-for-decode code-point))
325 (let ((c (cond
326 ((< code-point 160)
327 code-point)
328 ((< code-point 256)
329 (make-char 'latin-iso8859-1 code-point))
330 ((< code-point #x2500)
331 (setq code-point (- code-point #x0100))
332 (make-char 'mule-unicode-0100-24ff
333 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
334 ((< code-point #x3400)
335 (setq code-point (- code-point #x2500))
336 (make-char 'mule-unicode-2500-33ff
337 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
338 ((and (>= code-point #xe000) (< code-point #x10000))
339 (setq code-point (- code-point #xe000))
340 (make-char 'mule-unicode-e000-ffff
341 (+ (/ code-point 96) 32)
342 (+ (% code-point 96) 32))))))
343 (when c
344 (or (aref (get 'utf-translation-table-for-decode
345 'translation-table) c)
346 c)))))))
347
348 (defun encode-char (char ccs &optional restriction)
349 "Return code-point in coded character set CCS that corresponds to CHAR.
350 Return nil if CHAR is not included in CCS.
351 Currently the only supported coded character set is `ucs' (ISO/IEC
352 10646: Universal Multi-Octet Coded Character Set), and CHAR is first
353 translated through the translation-table named
354 `utf-translation-table-for-encode', or through the
355 translation-hash-table named `utf-subst-table-for-encode' \(if
356 `utf-translate-cjk-mode' is non-nil).
357
358 CHAR should be in one of these charsets:
359 ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
360 mule-unicode-e000-ffff, eight-bit-control
361 Otherwise, return nil.
362
363 Optional argument RESTRICTION specifies a way to map CHAR to a
364 code-point in CCS. Currently not supported and just ignored."
365 (let* ((split (split-char char))
366 (charset (car split))
367 trans)
368 (cond ((eq ccs 'ucs)
369 (or (and utf-translate-cjk-mode
370 (utf-lookup-subst-table-for-encode char))
371 (let ((table (get 'utf-translation-table-for-encode
372 'translation-table)))
373 (setq trans (aref table char))
374 (if trans
375 (setq split (split-char trans)
376 charset (car split)))
377 (cond ((eq charset 'ascii)
378 (or trans char))
379 ((eq charset 'latin-iso8859-1)
380 (+ (nth 1 split) 128))
381 ((eq charset 'mule-unicode-0100-24ff)
382 (+ #x0100 (+ (* (- (nth 1 split) 32) 96)
383 (- (nth 2 split) 32))))
384 ((eq charset 'mule-unicode-2500-33ff)
385 (+ #x2500 (+ (* (- (nth 1 split) 32) 96)
386 (- (nth 2 split) 32))))
387 ((eq charset 'mule-unicode-e000-ffff)
388 (+ #xe000 (+ (* (- (nth 1 split) 32) 96)
389 (- (nth 2 split) 32))))
390 ((eq charset 'eight-bit-control)
391 char))))))))
392
393 \f
394 ;; Coding system stuff
395
396 ;; Coding system is a symbol that has the property `coding-system'.
397 ;;
398 ;; The value of the property `coding-system' is a vector of the
399 ;; following format:
400 ;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
401 ;; We call this vector as coding-spec. See comments in src/coding.c
402 ;; for more detail.
403
404 (defconst coding-spec-type-idx 0)
405 (defconst coding-spec-mnemonic-idx 1)
406 (defconst coding-spec-doc-string-idx 2)
407 (defconst coding-spec-plist-idx 3)
408 (defconst coding-spec-flags-idx 4)
409
410 ;; PLIST is a property list of a coding system. To share PLIST among
411 ;; alias coding systems, a coding system has PLIST in coding-spec
412 ;; instead of having it in normal property list of Lisp symbol.
413 ;; Here's a list of coding system properties currently being used.
414 ;;
415 ;; o coding-category
416 ;;
417 ;; The value is a coding category the coding system belongs to. The
418 ;; function `make-coding-system' sets this value automatically
419 ;; unless its argument PROPERTIES specifies this property.
420 ;;
421 ;; o alias-coding-systems
422 ;;
423 ;; The value is a list of coding systems of the same alias group. The
424 ;; first element is the coding system made at first, which we call as
425 ;; `base coding system'. The function `make-coding-system' sets this
426 ;; value automatically and `define-coding-system-alias' updates it.
427 ;;
428 ;; See the documentation of make-coding-system for the meanings of the
429 ;; following properties.
430 ;;
431 ;; o post-read-conversion
432 ;; o pre-write-conversion
433 ;; o translation-table-for-decode
434 ;; o translation-table-for-encode
435 ;; o safe-chars
436 ;; o safe-charsets
437 ;; o mime-charset
438 ;; o valid-codes (meaningful only for a coding system based on CCL)
439
440
441 (defsubst coding-system-spec (coding-system)
442 "Return coding-spec of CODING-SYSTEM."
443 (get (check-coding-system coding-system) 'coding-system))
444
445 (defun coding-system-type (coding-system)
446 "Return the coding type of CODING-SYSTEM.
447 A coding type is an integer value indicating the encoding method
448 of CODING-SYSTEM. See the function `make-coding-system' for more detail."
449 (aref (coding-system-spec coding-system) coding-spec-type-idx))
450
451 (defun coding-system-mnemonic (coding-system)
452 "Return the mnemonic character of CODING-SYSTEM.
453 The mnemonic character of a coding system is used in mode line
454 to indicate the coding system. If the arg is nil, return ?-."
455 (let ((spec (coding-system-spec coding-system)))
456 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
457
458 (defun coding-system-doc-string (coding-system)
459 "Return the documentation string for CODING-SYSTEM."
460 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
461
462 (defun coding-system-plist (coding-system)
463 "Return the property list of CODING-SYSTEM."
464 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
465
466 (defun coding-system-flags (coding-system)
467 "Return `flags' of CODING-SYSTEM.
468 A `flags' of a coding system is a vector of length 32 indicating detailed
469 information of a coding system. See the function `make-coding-system'
470 for more detail."
471 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
472
473 (defun coding-system-get (coding-system prop)
474 "Extract a value from CODING-SYSTEM's property list for property PROP."
475 (plist-get (coding-system-plist coding-system) prop))
476
477 (defun coding-system-put (coding-system prop val)
478 "Change value in CODING-SYSTEM's property list PROP to VAL."
479 (let ((plist (coding-system-plist coding-system)))
480 (if plist
481 (plist-put plist prop val)
482 (aset (coding-system-spec coding-system) coding-spec-plist-idx
483 (list prop val)))))
484
485 (defun coding-system-category (coding-system)
486 "Return the coding category of CODING-SYSTEM.
487 See also `coding-category-list'."
488 (coding-system-get coding-system 'coding-category))
489
490 (defun coding-system-base (coding-system)
491 "Return the base coding system of CODING-SYSTEM.
492 A base coding system is what made by `make-coding-system'.
493 Any alias nor subsidiary coding systems are not base coding system."
494 (car (coding-system-get coding-system 'alias-coding-systems)))
495
496 ;; Coding system also has a property `eol-type'.
497 ;;
498 ;; This property indicates how the coding system handles end-of-line
499 ;; format. The value is integer 0, 1, 2, or a vector of three coding
500 ;; systems. Each integer value 0, 1, and 2 indicates the format of
501 ;; end-of-line LF, CRLF, and CR respectively. A vector value
502 ;; indicates that the format of end-of-line should be detected
503 ;; automatically. Nth element of the vector is the subsidiary coding
504 ;; system whose `eol-type' property is N.
505
506 (defun coding-system-eol-type (coding-system)
507 "Return eol-type of CODING-SYSTEM.
508 An eol-type is integer 0, 1, 2, or a vector of coding systems.
509
510 Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
511 CRLF, and CR respectively.
512
513 A vector value indicates that a format of end-of-line should be
514 detected automatically. Nth element of the vector is the subsidiary
515 coding system whose eol-type is N."
516 (get coding-system 'eol-type))
517
518 (defun coding-system-eol-type-mnemonic (coding-system)
519 "Return the string indicating end-of-line format of CODING-SYSTEM."
520 (let* ((eol-type (coding-system-eol-type coding-system))
521 (val (cond ((eq eol-type 0) eol-mnemonic-unix)
522 ((eq eol-type 1) eol-mnemonic-dos)
523 ((eq eol-type 2) eol-mnemonic-mac)
524 (t eol-mnemonic-undecided))))
525 (if (stringp val)
526 val
527 (char-to-string val))))
528
529 (defun coding-system-lessp (x y)
530 (cond ((eq x 'no-conversion) t)
531 ((eq y 'no-conversion) nil)
532 ((eq x 'emacs-mule) t)
533 ((eq y 'emacs-mule) nil)
534 ((eq x 'undecided) t)
535 ((eq y 'undecided) nil)
536 (t (let ((c1 (coding-system-mnemonic x))
537 (c2 (coding-system-mnemonic y)))
538 (or (< (downcase c1) (downcase c2))
539 (and (not (> (downcase c1) (downcase c2)))
540 (< c1 c2)))))))
541
542 (defun coding-system-equal (coding-system-1 coding-system-2)
543 "Return t if and only if CODING-SYSTEM-1 and CODING-SYSTEM-2 are identical.
544 Two coding systems are identical if two symbols are equal
545 or one is an alias of the other."
546 (or (eq coding-system-1 coding-system-2)
547 (and (equal (coding-system-spec coding-system-1)
548 (coding-system-spec coding-system-2))
549 (let ((eol-type-1 (coding-system-eol-type coding-system-1))
550 (eol-type-2 (coding-system-eol-type coding-system-2)))
551 (or (eq eol-type-1 eol-type-2)
552 (and (vectorp eol-type-1) (vectorp eol-type-2)))))))
553
554 (defun add-to-coding-system-list (coding-system)
555 "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
556 (if (or (null coding-system-list)
557 (coding-system-lessp coding-system (car coding-system-list)))
558 (setq coding-system-list (cons coding-system coding-system-list))
559 (let ((len (length coding-system-list))
560 mid (tem coding-system-list))
561 (while (> len 1)
562 (setq mid (nthcdr (/ len 2) tem))
563 (if (coding-system-lessp (car mid) coding-system)
564 (setq tem mid
565 len (- len (/ len 2)))
566 (setq len (/ len 2))))
567 (setcdr tem (cons coding-system (cdr tem))))))
568
569 (defun coding-system-list (&optional base-only)
570 "Return a list of all existing non-subsidiary coding systems.
571 If optional arg BASE-ONLY is non-nil, only base coding systems are listed.
572 The value doesn't include subsidiary coding systems which are what
573 made from bases and aliases automatically for various end-of-line
574 formats (e.g. iso-latin-1-unix, koi8-r-dos)."
575 (let* ((codings (copy-sequence coding-system-list))
576 (tail (cons nil codings)))
577 ;; Remove subsidiary coding systems (eol variants) and alias
578 ;; coding systems (if necessary).
579 (while (cdr tail)
580 (let* ((coding (car (cdr tail)))
581 (aliases (coding-system-get coding 'alias-coding-systems)))
582 (if (or
583 ;; CODING is an eol variant if not in ALIASES.
584 (not (memq coding aliases))
585 ;; CODING is an alias if it is not car of ALIASES.
586 (and base-only (not (eq coding (car aliases)))))
587 (setcdr tail (cdr (cdr tail)))
588 (setq tail (cdr tail)))))
589 codings))
590
591 (defun map-charset-chars (func charset)
592 "Use FUNC to map over all characters in CHARSET for side effects.
593 FUNC is a function of two args, the start and end (inclusive) of a
594 character code range. Thus FUNC should iterate over [START, END]."
595 (let* ((dim (charset-dimension charset))
596 (chars (charset-chars charset))
597 (start (if (= chars 94)
598 33
599 32)))
600 (if (= dim 1)
601 (funcall func
602 (make-char charset start)
603 (make-char charset (+ start chars -1)))
604 (dotimes (i chars)
605 (funcall func
606 (make-char charset (+ i start) start)
607 (make-char charset (+ i start) (+ start chars -1)))))))
608
609 (defalias 'register-char-codings 'ignore "")
610 (make-obsolete 'register-char-codings
611 "it exists just for backward compatibility, and does nothing."
612 "21.3")
613
614 (defconst char-coding-system-table nil
615 "This is an obsolete variable.
616 It exists just for backward compatibility, and the value is always nil.")
617
618 (defun make-subsidiary-coding-system (coding-system)
619 "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
620 (let ((coding-spec (coding-system-spec coding-system))
621 (subsidiaries (vector (intern (format "%s-unix" coding-system))
622 (intern (format "%s-dos" coding-system))
623 (intern (format "%s-mac" coding-system))))
624 elt)
625 (dotimes (i 3)
626 (setq elt (aref subsidiaries i))
627 (put elt 'coding-system coding-spec)
628 (put elt 'eol-type i)
629 (put elt 'coding-system-define-form nil)
630 (add-to-coding-system-list elt)
631 (or (assoc (symbol-name elt) coding-system-alist)
632 (setq coding-system-alist
633 (cons (list (symbol-name elt)) coding-system-alist))))
634 subsidiaries))
635
636 (defun transform-make-coding-system-args (name type &optional doc-string props)
637 "For internal use only.
638 Transform XEmacs style args for `make-coding-system' to Emacs style.
639 Value is a list of transformed arguments."
640 (let ((mnemonic (string-to-char (or (plist-get props 'mnemonic) "?")))
641 (eol-type (plist-get props 'eol-type))
642 properties tmp)
643 (cond
644 ((eq eol-type 'lf) (setq eol-type 'unix))
645 ((eq eol-type 'crlf) (setq eol-type 'dos))
646 ((eq eol-type 'cr) (setq eol-type 'mac)))
647 (if (setq tmp (plist-get props 'post-read-conversion))
648 (setq properties (plist-put properties 'post-read-conversion tmp)))
649 (if (setq tmp (plist-get props 'pre-write-conversion))
650 (setq properties (plist-put properties 'pre-write-conversion tmp)))
651 (cond
652 ((eq type 'shift-jis)
653 `(,name 1 ,mnemonic ,doc-string () ,properties ,eol-type))
654 ((eq type 'iso2022) ; This is not perfect.
655 (if (plist-get props 'escape-quoted)
656 (error "escape-quoted is not supported: %S"
657 `(,name ,type ,doc-string ,props)))
658 (let ((g0 (plist-get props 'charset-g0))
659 (g1 (plist-get props 'charset-g1))
660 (g2 (plist-get props 'charset-g2))
661 (g3 (plist-get props 'charset-g3))
662 (use-roman
663 (and
664 (eq (cadr (assoc 'latin-jisx0201
665 (plist-get props 'input-charset-conversion)))
666 'ascii)
667 (eq (cadr (assoc 'ascii
668 (plist-get props 'output-charset-conversion)))
669 'latin-jisx0201)))
670 (use-oldjis
671 (and
672 (eq (cadr (assoc 'japanese-jisx0208-1978
673 (plist-get props 'input-charset-conversion)))
674 'japanese-jisx0208)
675 (eq (cadr (assoc 'japanese-jisx0208
676 (plist-get props 'output-charset-conversion)))
677 'japanese-jisx0208-1978))))
678 (if (charsetp g0)
679 (if (plist-get props 'force-g0-on-output)
680 (setq g0 `(nil ,g0))
681 (setq g0 `(,g0 t))))
682 (if (charsetp g1)
683 (if (plist-get props 'force-g1-on-output)
684 (setq g1 `(nil ,g1))
685 (setq g1 `(,g1 t))))
686 (if (charsetp g2)
687 (if (plist-get props 'force-g2-on-output)
688 (setq g2 `(nil ,g2))
689 (setq g2 `(,g2 t))))
690 (if (charsetp g3)
691 (if (plist-get props 'force-g3-on-output)
692 (setq g3 `(nil ,g3))
693 (setq g3 `(,g3 t))))
694 `(,name 2 ,mnemonic ,doc-string
695 (,g0 ,g1 ,g2 ,g3
696 ,(plist-get props 'short)
697 ,(not (plist-get props 'no-ascii-eol))
698 ,(not (plist-get props 'no-ascii-cntl))
699 ,(plist-get props 'seven)
700 t
701 ,(not (plist-get props 'lock-shift))
702 ,use-roman
703 ,use-oldjis
704 ,(plist-get props 'no-iso6429)
705 nil nil nil nil)
706 ,properties ,eol-type)))
707 ((eq type 'big5)
708 `(,name 3 ,mnemonic ,doc-string () ,properties ,eol-type))
709 ((eq type 'ccl)
710 `(,name 4 ,mnemonic ,doc-string
711 (,(plist-get props 'decode) . ,(plist-get props 'encode))
712 ,properties ,eol-type))
713 (t
714 (error "unsupported XEmacs style make-coding-style arguments: %S"
715 `(,name ,type ,doc-string ,props))))))
716
717 (defun make-coding-system (coding-system type mnemonic doc-string
718 &optional
719 flags
720 properties
721 eol-type)
722 "Define a new coding system CODING-SYSTEM (symbol).
723 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
724 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
725 in the following format:
726 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
727
728 TYPE is an integer value indicating the type of the coding system as follows:
729 0: Emacs internal format,
730 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PCs,
731 2: ISO-2022 including many variants,
732 3: Big5 used mainly on Chinese PCs,
733 4: private, CCL programs provide encoding/decoding algorithm,
734 5: Raw-text, which means that text contains random 8-bit codes.
735
736 MNEMONIC is a character to be displayed on mode line for the coding system.
737
738 DOC-STRING is a documentation string for the coding system.
739
740 FLAGS specifies more detailed information of the coding system as follows:
741
742 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
743 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
744 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
745 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
746 SAFE, ACCEPT-LATIN-EXTRA-CODE.
747 CHARSETn are character sets initially designated to Gn graphic registers.
748 If CHARSETn is nil, Gn is never used.
749 If CHARSETn is t, Gn can be used but nothing designated initially.
750 If CHARSETn is a list of character sets, those character sets are
751 designated to Gn on output, but nothing designated to Gn initially.
752 But, character set `ascii' can be designated only to G0.
753 SHORT-FORM non-nil means use short designation sequence on output.
754 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
755 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
756 SPACE on output.
757 SEVEN non-nil means use 7-bit code only on output.
758 LOCKING-SHIFT non-nil means use locking-shift.
759 SINGLE-SHIFT non-nil means use single-shift.
760 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
761 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
762 NO-ISO6429 non-nil means not use ISO6429's direction specification.
763 INIT-BOL non-nil means any designation state is assumed to be reset
764 to initial at each beginning of line on output.
765 DESIGNATION-BOL non-nil means designation sequences should be placed
766 at beginning of line on output.
767 SAFE non-nil means convert unsafe characters to `?' on output.
768 Characters not specified in the property `safe-charsets' nor
769 `safe-chars' are unsafe.
770 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
771 a code specified in `latin-extra-code-table' (which see) as a valid
772 code of the coding system.
773
774 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
775 decoding and encoding. CCL programs should be specified by their
776 symbols.
777
778 PROPERTIES is an alist of properties vs the corresponding values. The
779 following properties are recognized:
780
781 o post-read-conversion
782
783 The value is a function to call after some text is inserted and
784 decoded by the coding system itself and before any functions in
785 `after-insert-functions' are called. The argument of this
786 function is the same as for a function in
787 `after-insert-file-functions', i.e. LENGTH of the text inserted,
788 with point at the head of the text to be decoded.
789
790 o pre-write-conversion
791
792 The value is a function to call after all functions in
793 `write-region-annotate-functions' and `buffer-file-format' are
794 called, and before the text is encoded by the coding system itself.
795 The arguments to this function are the same as those of a function
796 in `write-region-annotate-functions', i.e. FROM and TO, specifying
797 a region of text.
798
799 o translation-table-for-decode
800
801 The value is a translation table to be applied on decoding. See
802 the function `make-translation-table' for the format of translation
803 table. This is not applicable to type 4 (CCL-based) coding systems.
804
805 o translation-table-for-encode
806
807 The value is a translation table to be applied on encoding. This is
808 not applicable to type 4 (CCL-based) coding systems.
809
810 o safe-chars
811
812 The value is a char table. If a character has non-nil value in it,
813 the character is safely supported by the coding system. This
814 overrides the specification of safe-charsets.
815
816 o safe-charsets
817
818 The value is a list of charsets safely supported by the coding
819 system. The value t means that all charsets Emacs handles are
820 supported. Even if some charset is not in this list, it doesn't
821 mean that the charset can't be encoded in the coding system;
822 it just means that some other receiver of text encoded
823 in the coding system won't be able to handle that charset.
824
825 o mime-charset
826
827 The value is a symbol whose name is the `MIME-charset' parameter of
828 the coding system.
829
830 o mime-text-unsuitable
831
832 A non-nil value means the `mime-charset' property names a charset
833 which is unsuitable for the top-level media type \"text\".
834
835 o valid-codes (meaningful only for a coding system based on CCL)
836
837 The value is a list to indicate valid byte ranges of the encoded
838 file. Each element of the list is an integer or a cons of integer.
839 In the former case, the integer value is a valid byte code. In the
840 latter case, the integers specify the range of valid byte codes.
841
842 o composition (meaningful only when TYPE is 0 or 2)
843
844 If the value is non-nil, the coding system preserves composition
845 information.
846
847 o ascii-incompatible
848
849 If the value is non-nil, the coding system is not compatible
850 with ASCII, which means it encodes or decodes ASCII character
851 string to the different byte sequence.
852
853 These properties are set in PLIST, a property list. This function
854 also sets properties `coding-category' and `alias-coding-systems'
855 automatically.
856
857 EOL-TYPE specifies the EOL type of the coding-system in one of the
858 following formats:
859
860 o symbol (unix, dos, or mac)
861
862 The symbol `unix' means Unix-like EOL (LF), `dos' means
863 DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).
864
865 o number (0, 1, or 2)
866
867 The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
868 respectively.
869
870 o vector of coding-systems of length 3
871
872 The EOL type is detected automatically for the coding system.
873 And, according to the detected EOL type, one of the coding
874 systems in the vector is selected. Elements of the vector
875 corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
876 in this order.
877
878 Kludgy features for backward compatibility:
879
880 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
881 treated as a compiled CCL code.
882
883 2. If PROPERTIES is just a list of character sets, the list is set as
884 a value of `safe-charsets' in PLIST."
885
886 ;; For compatiblity with XEmacs, we check the type of TYPE. If it
887 ;; is a symbol, perhaps, this function is called with XEmacs-style
888 ;; arguments. Here, try to transform that kind of arguments to
889 ;; Emacs style.
890 (if (symbolp type)
891 (let ((args (transform-make-coding-system-args coding-system type
892 mnemonic doc-string)))
893 (setq coding-system (car args)
894 type (nth 1 args)
895 mnemonic (nth 2 args)
896 doc-string (nth 3 args)
897 flags (nth 4 args)
898 properties (nth 5 args)
899 eol-type (nth 6 args))))
900
901 ;; Set a value of `coding-system' property.
902 (let ((coding-spec (make-vector 5 nil))
903 (no-initial-designation t)
904 (no-alternative-designation t)
905 (accept-latin-extra-code nil)
906 coding-category)
907 (if (or (not (integerp type)) (< type 0) (> type 5))
908 (error "TYPE argument must be 0..5"))
909 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
910 (error "MNEMONIC argument must be an ASCII printable character"))
911 (aset coding-spec coding-spec-type-idx type)
912 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
913 (aset coding-spec coding-spec-doc-string-idx
914 (purecopy (if (stringp doc-string) doc-string "")))
915 (cond ((= type 0)
916 (setq coding-category 'coding-category-emacs-mule))
917 ((= type 1)
918 (setq coding-category 'coding-category-sjis))
919 ((= type 2) ; ISO2022
920 (let ((i 0)
921 (vec (make-vector 32 nil))
922 (g1-designation nil)
923 (fl flags))
924 (while (< i 4)
925 (let ((charset (car fl)))
926 (if (and no-initial-designation
927 (> i 0)
928 (or (charsetp charset)
929 (and (consp charset)
930 (charsetp (car charset)))))
931 (setq no-initial-designation nil))
932 (if (charsetp charset)
933 (if (= i 1) (setq g1-designation charset))
934 (if (consp charset)
935 (let ((tail charset)
936 elt)
937 (while tail
938 (setq elt (car tail))
939 (if (eq elt t)
940 (setq no-alternative-designation nil)
941 (if (and elt (not (charsetp elt)))
942 (error "Invalid charset: %s" elt)))
943 (setq tail (cdr tail)))
944 (setq g1-designation (car charset)))
945 (if charset
946 (if (eq charset t)
947 (setq no-alternative-designation nil)
948 (error "Invalid charset: %s" charset)))))
949 (aset vec i charset))
950 (setq fl (cdr fl) i (1+ i)))
951 (while (and (< i 32) fl)
952 (aset vec i (car fl))
953 (if (and (= i 16) ; ACCEPT-LATIN-EXTRA-CODE
954 (car fl))
955 (setq accept-latin-extra-code t))
956 (setq fl (cdr fl) i (1+ i)))
957 (aset coding-spec 4 vec)
958 (setq coding-category
959 (if (aref vec 8) ; Use locking-shift.
960 (or (and (aref vec 7) 'coding-category-iso-7-else)
961 'coding-category-iso-8-else)
962 (if (aref vec 7) ; 7-bit only.
963 (if (aref vec 9) ; Use single-shift.
964 'coding-category-iso-7-else
965 (if no-alternative-designation
966 'coding-category-iso-7-tight
967 'coding-category-iso-7))
968 (if (or no-initial-designation
969 (not no-alternative-designation))
970 'coding-category-iso-8-else
971 (if (and (charsetp g1-designation)
972 (= (charset-dimension g1-designation) 2))
973 'coding-category-iso-8-2
974 'coding-category-iso-8-1)))))))
975 ((= type 3)
976 (setq coding-category 'coding-category-big5))
977 ((= type 4) ; private
978 (setq coding-category 'coding-category-ccl)
979 (if (not (consp flags))
980 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
981 (let ((decoder (check-ccl-program
982 (car flags)
983 (intern (format "%s-decoder" coding-system))))
984 (encoder (check-ccl-program
985 (cdr flags)
986 (intern (format "%s-encoder" coding-system)))))
987 (if (and decoder encoder)
988 (aset coding-spec 4 (cons decoder encoder))
989 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
990 (t ; i.e. (= type 5)
991 (setq coding-category 'coding-category-raw-text)))
992
993 (let ((plist (list 'coding-category coding-category
994 'alias-coding-systems (list coding-system))))
995 (if no-initial-designation
996 (plist-put plist 'no-initial-designation t))
997 (if (and properties
998 (or (eq properties t)
999 (not (consp (car properties)))))
1000 ;; In the old version, the arg PROPERTIES is a list to be
1001 ;; set in PLIST as a value of property `safe-charsets'.
1002 (setq properties (list (cons 'safe-charsets properties))))
1003 ;; In the current version PROPERTIES is a property list.
1004 ;; Reflect it into PLIST one by one while handling safe-chars
1005 ;; specially.
1006 (let ((safe-charsets (cdr (assq 'safe-charsets properties)))
1007 (safe-chars (cdr (assq 'safe-chars properties)))
1008 (l properties)
1009 prop val)
1010 ;; If only safe-charsets is specified, make a char-table from
1011 ;; it, and store that char-table as the value of `safe-chars'.
1012 (if (and (not safe-chars) safe-charsets)
1013 (let (charset)
1014 (if (eq safe-charsets t)
1015 (setq safe-chars t)
1016 (setq safe-chars (make-char-table 'safe-chars))
1017 (while safe-charsets
1018 (setq charset (car safe-charsets)
1019 safe-charsets (cdr safe-charsets))
1020 (cond ((eq charset 'ascii)) ; just ignore
1021 ((eq charset 'eight-bit-control)
1022 (let ((i 128))
1023 (while (< i 160)
1024 (aset safe-chars i t)
1025 (setq i (1+ i)))))
1026 ((eq charset 'eight-bit-graphic)
1027 (let ((i 160))
1028 (while (< i 256)
1029 (aset safe-chars i t)
1030 (setq i (1+ i)))))
1031 (t
1032 (aset safe-chars (make-char charset) t))))
1033 (if accept-latin-extra-code
1034 (let ((i 128))
1035 (while (< i 160)
1036 (if (aref latin-extra-code-table i)
1037 (aset safe-chars i t))
1038 (setq i (1+ i))))))
1039 (setq l (cons (cons 'safe-chars safe-chars) l))))
1040 (while l
1041 (setq prop (car (car l)) val (cdr (car l)) l (cdr l))
1042 (if (eq prop 'safe-chars)
1043 (progn
1044 (if (and (symbolp val)
1045 (get val 'translation-table))
1046 (setq safe-chars (get val 'translation-table)))
1047 (setq val safe-chars)))
1048 (plist-put plist prop val)))
1049 ;; The property `coding-category' may have been set differently
1050 ;; through PROPERTIES.
1051 (setq coding-category (plist-get plist 'coding-category))
1052 (aset coding-spec coding-spec-plist-idx plist))
1053 (put coding-system 'coding-system coding-spec)
1054 (put coding-system 'coding-system-define-form nil)
1055 (put coding-category 'coding-systems
1056 (cons coding-system (get coding-category 'coding-systems))))
1057
1058 ;; Next, set a value of `eol-type' property.
1059 (if (not eol-type)
1060 ;; If EOL-TYPE is nil, set a vector of subsidiary coding
1061 ;; systems, each corresponds to a coding system for the detected
1062 ;; EOL format.
1063 (setq eol-type (make-subsidiary-coding-system coding-system)))
1064 (setq eol-type
1065 (cond ((or (eq eol-type 'unix) (null eol-type))
1066 0)
1067 ((eq eol-type 'dos)
1068 1)
1069 ((eq eol-type 'mac)
1070 2)
1071 ((or (and (vectorp eol-type)
1072 (= (length eol-type) 3))
1073 (and (numberp eol-type)
1074 (and (>= eol-type 0)
1075 (<= eol-type 2))))
1076 eol-type)
1077 (t
1078 (error "Invalid EOL-TYPE spec:%S" eol-type))))
1079 (put coding-system 'eol-type eol-type)
1080
1081 (define-coding-system-internal coding-system)
1082
1083 ;; At last, register CODING-SYSTEM in `coding-system-list' and
1084 ;; `coding-system-alist'.
1085 (add-to-coding-system-list coding-system)
1086 (or (assoc (symbol-name coding-system) coding-system-alist)
1087 (setq coding-system-alist (cons (list (symbol-name coding-system))
1088 coding-system-alist)))
1089
1090 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
1091 ;; XXX-with-esc variants.
1092 (let ((coding-category (coding-system-category coding-system)))
1093 (if (or (eq coding-category 'coding-category-iso-8-1)
1094 (eq coding-category 'coding-category-iso-8-2))
1095 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
1096 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system))
1097 (safe-charsets (assq 'safe-charsets properties))
1098 (mime-charset (assq 'mime-charset properties)))
1099 (if safe-charsets
1100 (setcdr safe-charsets t)
1101 (setq properties (cons (cons 'safe-charsets t) properties)))
1102 (if mime-charset
1103 (setcdr mime-charset nil))
1104 (make-coding-system esc type mnemonic doc
1105 (if (listp (car flags))
1106 (cons (append (car flags) '(t)) (cdr flags))
1107 (cons (list (car flags) t) (cdr flags)))
1108 properties))))
1109
1110 coding-system)
1111
1112 (put 'safe-chars 'char-table-extra-slots 0)
1113
1114 (defun define-coding-system-alias (alias coding-system)
1115 "Define ALIAS as an alias for coding system CODING-SYSTEM."
1116 (put alias 'coding-system (coding-system-spec coding-system))
1117 (put alias 'coding-system-define-form nil)
1118 (add-to-coding-system-list alias)
1119 (or (assoc (symbol-name alias) coding-system-alist)
1120 (setq coding-system-alist (cons (list (symbol-name alias))
1121 coding-system-alist)))
1122 (let ((eol-type (coding-system-eol-type coding-system)))
1123 (if (vectorp eol-type)
1124 (progn
1125 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
1126 (put alias 'eol-type (make-subsidiary-coding-system alias)))
1127 (put alias 'eol-type eol-type))))
1128
1129 (defun merge-coding-systems (first second)
1130 "Fill in any unspecified aspects of coding system FIRST from SECOND.
1131 Return the resulting coding system."
1132 (let ((base (coding-system-base second))
1133 (eol (coding-system-eol-type second)))
1134 ;; If FIRST doesn't specify text conversion, merge with that of SECOND.
1135 (if (eq (coding-system-base first) 'undecided)
1136 (setq first (coding-system-change-text-conversion first base)))
1137 ;; If FIRST doesn't specify eol conversion, merge with that of SECOND.
1138 (if (and (vectorp (coding-system-eol-type first))
1139 (numberp eol) (>= eol 0) (<= eol 2))
1140 (setq first (coding-system-change-eol-conversion
1141 first eol)))
1142 first))
1143
1144 (defun autoload-coding-system (symbol form)
1145 "Define SYMBOL as a coding-system that is defined on demand.
1146
1147 FROM is a form to evaluate to define the coding-system."
1148 (put symbol 'coding-system-define-form form)
1149 (setq coding-system-alist (cons (list (symbol-name symbol))
1150 coding-system-alist))
1151 (dolist (elt '("-unix" "-dos" "-mac"))
1152 (let ((name (concat (symbol-name symbol) elt)))
1153 (put (intern name) 'coding-system-define-form form)
1154 (setq coding-system-alist (cons (list name) coding-system-alist)))))
1155
1156 (defun set-buffer-file-coding-system (coding-system &optional force nomodify)
1157 "Set the file coding-system of the current buffer to CODING-SYSTEM.
1158 This means that when you save the buffer, it will be converted
1159 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
1160 use \\[list-coding-systems].
1161
1162 If CODING-SYSTEM leaves the text conversion unspecified, or if it
1163 leaves the end-of-line conversion unspecified, FORCE controls what to
1164 do. If FORCE is nil, get the unspecified aspect (or aspects) from the
1165 buffer's previous `buffer-file-coding-system' value (if it is
1166 specified there). Otherwise, leave it unspecified.
1167
1168 This marks the buffer modified so that the succeeding \\[save-buffer]
1169 surely saves the buffer with CODING-SYSTEM. From a program, if you
1170 don't want to mark the buffer modified, specify t for NOMODIFY.
1171 If you know exactly what coding system you want to use,
1172 just set the variable `buffer-file-coding-system' directly."
1173 (interactive "zCoding system for saving file (default nil): \nP")
1174 (check-coding-system coding-system)
1175 (if (and coding-system buffer-file-coding-system (null force))
1176 (setq coding-system
1177 (merge-coding-systems coding-system buffer-file-coding-system)))
1178 (setq buffer-file-coding-system coding-system)
1179 ;; This is in case of an explicit call. Normally, `normal-mode' and
1180 ;; `set-buffer-major-mode-hook' take care of setting the table.
1181 (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
1182 (ucs-set-table-for-input))
1183 (unless nomodify
1184 (set-buffer-modified-p t))
1185 (force-mode-line-update))
1186
1187 (defun revert-buffer-with-coding-system (coding-system &optional force)
1188 "Visit the current buffer's file again using coding system CODING-SYSTEM.
1189 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1190
1191 If CODING-SYSTEM leaves the text conversion unspecified, or if it
1192 leaves the end-of-line conversion unspecified, FORCE controls what to
1193 do. If FORCE is nil, get the unspecified aspect (or aspects) from the
1194 buffer's previous `buffer-file-coding-system' value (if it is
1195 specified there). Otherwise, determine it from the file contents as
1196 usual for visiting a file."
1197 (interactive "zCoding system for visited file (default nil): \nP")
1198 (check-coding-system coding-system)
1199 (if (and coding-system buffer-file-coding-system (null force))
1200 (setq coding-system
1201 (merge-coding-systems coding-system buffer-file-coding-system)))
1202 (let ((coding-system-for-read coding-system))
1203 (revert-buffer)))
1204
1205 (defun set-file-name-coding-system (coding-system)
1206 "Set coding system for decoding and encoding file names to CODING-SYSTEM.
1207 It actually just set the variable `file-name-coding-system' (which
1208 see) to CODING-SYSTEM."
1209 (interactive "zCoding system for file names (default nil): ")
1210 (check-coding-system coding-system)
1211 (if (and coding-system
1212 (coding-system-get coding-system 'ascii-incompatible))
1213 (error "%s is not ASCII-compatible" coding-system))
1214 (setq file-name-coding-system coding-system))
1215
1216 (defvar default-terminal-coding-system nil
1217 "Default value for the terminal coding system.
1218 This is normally set according to the selected language environment.
1219 See also the command `set-terminal-coding-system'.")
1220
1221 (defun set-terminal-coding-system (coding-system &optional display)
1222 "Set coding system of terminal output to CODING-SYSTEM.
1223 All text output to DISPLAY will be encoded
1224 with the specified coding system.
1225
1226 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1227 The default is determined by the selected language environment
1228 or by the previous use of this command.
1229
1230 DISPLAY may be a display id, a frame, or nil for the selected frame's display.
1231 The setting has no effect on graphical displays."
1232 (interactive
1233 (list (let ((default (if (and (not (terminal-coding-system))
1234 default-terminal-coding-system)
1235 default-terminal-coding-system)))
1236 (read-coding-system
1237 (format "Coding system for terminal display (default %s): "
1238 default)
1239 default))))
1240 (if (and (not coding-system)
1241 (not (terminal-coding-system)))
1242 (setq coding-system default-terminal-coding-system))
1243 (if coding-system
1244 (setq default-terminal-coding-system coding-system))
1245 (set-terminal-coding-system-internal coding-system display)
1246 (redraw-frame (selected-frame)))
1247
1248 (defvar default-keyboard-coding-system nil
1249 "Default value of the keyboard coding system.
1250 This is normally set according to the selected language environment.
1251 See also the command `set-keyboard-coding-system'.")
1252
1253 (defun set-keyboard-coding-system (coding-system &optional display)
1254 "Set coding system for keyboard input on DISPLAY to CODING-SYSTEM.
1255 In addition, this command calls `encoded-kbd-setup-display' to set up the
1256 translation of keyboard input events to the specified coding system.
1257
1258 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1259 The default is determined by the selected language environment
1260 or by the previous use of this command.
1261
1262 DISPLAY may be a display id, a frame, or nil for the selected frame's display.
1263 The setting has no effect on graphical displays."
1264 (interactive
1265 (list (let ((default (if (and (not (keyboard-coding-system))
1266 default-keyboard-coding-system)
1267 default-keyboard-coding-system)))
1268 (read-coding-system
1269 (format "Coding system for keyboard input (default %s): "
1270 default)
1271 default))))
1272 (if (and (not coding-system)
1273 (not (keyboard-coding-system)))
1274 (setq coding-system default-keyboard-coding-system))
1275 (if coding-system
1276 (setq default-keyboard-coding-system coding-system))
1277 (if (and coding-system
1278 (coding-system-get coding-system 'ascii-incompatible))
1279 (error "%s is not ASCII-compatible" coding-system))
1280 (set-keyboard-coding-system-internal coding-system display)
1281 (setq keyboard-coding-system coding-system)
1282 (encoded-kbd-setup-display display))
1283
1284 (defcustom keyboard-coding-system nil
1285 "Specify coding system for keyboard input.
1286 If you set this on a terminal which can't distinguish Meta keys from
1287 8-bit characters, you will have to use ESC to type Meta characters.
1288 See Info node `Terminal Coding' and Info node `Unibyte Mode'.
1289
1290 On non-windowing terminals, this is set from the locale by default.
1291
1292 Setting this variable directly does not take effect;
1293 use either \\[customize] or \\[set-keyboard-coding-system]."
1294 :type '(coding-system :tag "Coding system")
1295 :link '(info-link "(emacs)Terminal Coding")
1296 :link '(info-link "(emacs)Unibyte Mode")
1297 :set (lambda (symbol value)
1298 ;; Don't load encoded-kb unnecessarily.
1299 (if (or value (boundp 'encoded-kbd-setup-display))
1300 (set-keyboard-coding-system value)
1301 (set-default 'keyboard-coding-system nil))) ; must initialize
1302 :version "22.1"
1303 :group 'keyboard
1304 :group 'mule)
1305
1306 (defun set-buffer-process-coding-system (decoding encoding)
1307 "Set coding systems for the process associated with the current buffer.
1308 DECODING is the coding system to be used to decode input from the process,
1309 ENCODING is the coding system to be used to encode output to the process.
1310
1311 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
1312 (interactive
1313 "zCoding-system for output from the process: \nzCoding-system for input to the process: ")
1314 (let ((proc (get-buffer-process (current-buffer))))
1315 (if (null proc)
1316 (error "No process")
1317 (check-coding-system decoding)
1318 (check-coding-system encoding)
1319 (set-process-coding-system proc decoding encoding)))
1320 (force-mode-line-update))
1321
1322 (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
1323
1324 (defun set-selection-coding-system (coding-system)
1325 "Make CODING-SYSTEM used for communicating with other X clients.
1326 When sending or receiving text via cut_buffer, selection, and clipboard,
1327 the text is encoded or decoded by CODING-SYSTEM."
1328 (interactive "zCoding system for X selection: ")
1329 (check-coding-system coding-system)
1330 (setq selection-coding-system coding-system))
1331
1332 ;; Coding system lastly specified by the command
1333 ;; set-next-selection-coding-system.
1334 (defvar last-next-selection-coding-system nil)
1335
1336 (defun set-next-selection-coding-system (coding-system)
1337 "Make CODING-SYSTEM used for the next communication with other X clients.
1338 This setting is effective for the next communication only."
1339 (interactive
1340 (list (read-coding-system
1341 (if last-next-selection-coding-system
1342 (format "Coding system for the next X selection (default %S): "
1343 last-next-selection-coding-system)
1344 "Coding system for the next X selection: ")
1345 last-next-selection-coding-system)))
1346 (if coding-system
1347 (setq last-next-selection-coding-system coding-system)
1348 (setq coding-system last-next-selection-coding-system))
1349 (check-coding-system coding-system)
1350
1351 (setq next-selection-coding-system coding-system))
1352
1353 (defun set-coding-priority (arg)
1354 "Set priority of coding categories according to ARG.
1355 ARG is a list of coding categories ordered by priority."
1356 (let ((l arg)
1357 (current-list (copy-sequence coding-category-list)))
1358 ;; Check the validity of ARG while deleting coding categories in
1359 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
1360 ;; contains all coding categories.
1361 (while l
1362 (if (or (null (get (car l) 'coding-category-index))
1363 (null (memq (car l) current-list)))
1364 (error "Invalid or duplicated element in argument: %s" arg))
1365 (setq current-list (delq (car l) current-list))
1366 (setq l (cdr l)))
1367 ;; Update `coding-category-list' and return it.
1368 (setq coding-category-list (append arg current-list))
1369 (set-coding-priority-internal)))
1370
1371 ;;; X selections
1372
1373 (defvar ctext-non-standard-encodings-alist
1374 '(("big5-0" big5 2 (chinese-big5-1 chinese-big5-2))
1375 ("ISO8859-14" iso-8859-14 1 latin-iso8859-14)
1376 ("ISO8859-15" iso-8859-15 1 latin-iso8859-15))
1377 "Alist of non-standard encoding names vs the corresponding usages in CTEXT.
1378
1379 It controls how extended segments of a compound text are handled
1380 by the coding system `compound-text-with-extensions'.
1381
1382 Each element has the form (ENCODING-NAME CODING-SYSTEM N-OCTET CHARSET).
1383
1384 ENCODING-NAME is an encoding name of an \"extended segments\".
1385
1386 CODING-SYSTEM is the coding-system to encode (or decode) the
1387 characters into (or from) the extended segment.
1388
1389 N-OCTET is the number of octets (bytes) that encodes a character
1390 in the segment. It can be 0 (meaning the number of octets per
1391 character is variable), 1, 2, 3, or 4.
1392
1393 CHARSET is a charater set containing characters that are encoded
1394 in the segment. It can be a list of character sets. It can also
1395 be a char-table, in which case characters that have non-nil value
1396 in the char-table are the target.
1397
1398 On decoding CTEXT, all encoding names listed here are recognized.
1399
1400 On encoding CTEXT, encoding names in the variable
1401 `ctext-non-standard-encodings' (which see) and in the information
1402 listed for the current language environment under the key
1403 `ctext-non-standard-encodings' are used.")
1404
1405 (defvar ctext-non-standard-encodings
1406 '("big5-0")
1407 "List of non-standard encoding names used in extended segments of CTEXT.
1408 Each element must be one of the names listed in the variable
1409 `ctext-non-standard-encodings-alist' (which see).")
1410
1411 (defvar ctext-non-standard-encodings-regexp
1412 (string-to-multibyte
1413 (concat
1414 ;; For non-standard encodings.
1415 "\\(\e%/[0-4][\200-\377][\200-\377]\\([^\002]+\\)\002\\)"
1416 "\\|"
1417 ;; For UTF-8 encoding.
1418 "\\(\e%G[^\e]*\e%@\\)")))
1419
1420 ;; Functions to support "Non-Standard Character Set Encodings" defined
1421 ;; by the COMPOUND-TEXT spec. They also support "The UTF-8 encoding"
1422 ;; described in the section 7 of the documentation of COMPOUND-TEXT
1423 ;; distributed with XFree86.
1424
1425 (defun ctext-post-read-conversion (len)
1426 "Decode LEN characters encoded as Compound Text with Extended Segments."
1427 (save-match-data
1428 (save-restriction
1429 (let ((case-fold-search nil)
1430 (in-workbuf (string= (buffer-name) " *code-converting-work*"))
1431 last-coding-system-used
1432 pos bytes)
1433 (or in-workbuf
1434 (narrow-to-region (point) (+ (point) len)))
1435 (if in-workbuf
1436 (set-buffer-multibyte t))
1437 (while (re-search-forward ctext-non-standard-encodings-regexp
1438 nil 'move)
1439 (setq pos (match-beginning 0))
1440 (if (match-beginning 1)
1441 ;; ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
1442 (let* ((M (char-after (+ pos 4)))
1443 (L (char-after (+ pos 5)))
1444 (encoding (match-string 2))
1445 (encoding-info (assoc-string
1446 encoding
1447 ctext-non-standard-encodings-alist t))
1448 (coding (if encoding-info
1449 (nth 1 encoding-info)
1450 (setq encoding (intern (downcase encoding)))
1451 (and (coding-system-p encoding)
1452 encoding))))
1453 (setq bytes (- (+ (* (- M 128) 128) (- L 128))
1454 (- (point) (+ pos 6))))
1455 (when coding
1456 (delete-region pos (point))
1457 (forward-char bytes)
1458 (decode-coding-region (- (point) bytes) (point) coding)))
1459 ;; ESC % G --UTF-8-BYTES-- ESC % @
1460 (delete-char -3)
1461 (delete-region pos (+ pos 3))
1462 (decode-coding-region pos (point) 'utf-8))))
1463 (goto-char (point-min))
1464 (- (point-max) (point)))))
1465
1466 ;; Return a char table of extended segment usage for each character.
1467 ;; Each value of the char table is nil, one of the elements of
1468 ;; `ctext-non-standard-encodings-alist', or the symbol `utf-8'.
1469
1470 (defun ctext-non-standard-encodings-table ()
1471 (let ((table (make-char-table 'translation-table)))
1472 (aset table (make-char 'mule-unicode-0100-24ff) 'utf-8)
1473 (aset table (make-char 'mule-unicode-2500-33ff) 'utf-8)
1474 (aset table (make-char 'mule-unicode-e000-ffff) 'utf-8)
1475 (dolist (encoding (reverse
1476 (append
1477 (get-language-info current-language-environment
1478 'ctext-non-standard-encodings)
1479 ctext-non-standard-encodings)))
1480 (let* ((slot (assoc encoding ctext-non-standard-encodings-alist))
1481 (charset (nth 3 slot)))
1482 (if charset
1483 (cond ((charsetp charset)
1484 (aset table (make-char charset) slot))
1485 ((listp charset)
1486 (dolist (elt charset)
1487 (aset table (make-char elt) slot)))
1488 ((char-table-p charset)
1489 (map-char-table #'(lambda (k v)
1490 (if (and v (> k 128)) (aset table k slot)))
1491 charset))))))
1492 table))
1493
1494 (defun ctext-pre-write-conversion (from to)
1495 "Encode characters between FROM and TO as Compound Text w/Extended Segments.
1496
1497 If FROM is a string, or if the current buffer is not the one set up for us
1498 by encode-coding-string, generate a new temp buffer, insert the
1499 text, and convert it in the temporary buffer. Otherwise, convert in-place."
1500 (save-match-data
1501 (let ((workbuf (get-buffer-create " *code-conversion-work*")))
1502 ;; Setup a working buffer if necessary.
1503 (cond ((stringp from)
1504 (set-buffer workbuf)
1505 (erase-buffer)
1506 (set-buffer-multibyte (multibyte-string-p from))
1507 (insert from))
1508 ((not (eq (current-buffer) workbuf))
1509 (let ((buf (current-buffer))
1510 (multibyte enable-multibyte-characters))
1511 (set-buffer workbuf)
1512 (erase-buffer)
1513 (set-buffer-multibyte multibyte)
1514 (insert-buffer-substring buf from to)))))
1515
1516 ;; Now we can encode the whole buffer.
1517 (let ((encoding-table (ctext-non-standard-encodings-table))
1518 last-coding-system-used
1519 last-pos last-encoding-info
1520 encoding-info end-pos)
1521 (goto-char (setq last-pos (point-min)))
1522 (setq end-pos (point-marker))
1523 (while (re-search-forward "[^\000-\177]+" nil t)
1524 ;; Found a sequence of non-ASCII characters.
1525 (setq last-pos (match-beginning 0)
1526 last-encoding-info (aref encoding-table (char-after last-pos)))
1527 (set-marker end-pos (match-end 0))
1528 (goto-char (1+ last-pos))
1529 (catch 'tag
1530 (while t
1531 (setq encoding-info
1532 (if (< (point) end-pos)
1533 (aref encoding-table (following-char))))
1534 (unless (eq last-encoding-info encoding-info)
1535 (cond ((consp last-encoding-info)
1536 ;; Encode the previous range using an extended
1537 ;; segment.
1538 (let ((encoding-name (car last-encoding-info))
1539 (coding-system (nth 1 last-encoding-info))
1540 (noctets (nth 2 last-encoding-info))
1541 len)
1542 (encode-coding-region last-pos (point) coding-system)
1543 (setq len (+ (length encoding-name) 1
1544 (- (point) last-pos)))
1545 (save-excursion
1546 (goto-char last-pos)
1547 (insert (string-to-multibyte
1548 (format "\e%%/%d%c%c%s\ 2"
1549 noctets
1550 (+ (/ len 128) 128)
1551 (+ (% len 128) 128)
1552 encoding-name))))))
1553 ((eq last-encoding-info 'utf-8)
1554 ;; Encode the previous range using UTF-8 encoding
1555 ;; extention.
1556 (encode-coding-region last-pos (point) 'mule-utf-8)
1557 (save-excursion
1558 (goto-char last-pos)
1559 (insert "\e%G"))
1560 (insert "\e%@")))
1561 (setq last-pos (point)
1562 last-encoding-info encoding-info))
1563 (if (< (point) end-pos)
1564 (forward-char 1)
1565 (throw 'tag nil)))))
1566 (set-marker end-pos nil)
1567 (goto-char (point-min))))
1568 ;; Must return nil, as build_annotations_2 expects that.
1569 nil)
1570
1571 ;;; FILE I/O
1572
1573 (defcustom auto-coding-alist
1574 '(("\\.\\(arc\\|zip\\|lzh\\|lha\\|zoo\\|[jew]ar\\|xpi\\)\\'" . no-conversion)
1575 ("\\.\\(ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\)\\'" . no-conversion)
1576 ("\\.\\(sx[dmicw]\\|odt\\|tar\\|tgz\\)\\'" . no-conversion)
1577 ("\\.\\(gz\\|Z\\|bz\\|bz2\\|gpg\\)\\'" . no-conversion)
1578 ("\\.\\(jpe?g\\|png\\|gif\\|tiff?\\|p[bpgn]m\\)\\'" . no-conversion)
1579 ("/#[^/]+#\\'" . emacs-mule))
1580 "Alist of filename patterns vs corresponding coding systems.
1581 Each element looks like (REGEXP . CODING-SYSTEM).
1582 A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
1583
1584 The settings in this alist take priority over `coding:' tags
1585 in the file (see the function `set-auto-coding')
1586 and the contents of `file-coding-system-alist'."
1587 :group 'files
1588 :group 'mule
1589 :type '(repeat (cons (regexp :tag "File name regexp")
1590 (symbol :tag "Coding system"))))
1591
1592 (defcustom auto-coding-regexp-alist
1593 '(("^BABYL OPTIONS:[ \t]*-\\*-[ \t]*rmail[ \t]*-\\*-" . no-conversion)
1594 ("\\`\xFE\xFF" . utf-16be-with-signature)
1595 ("\\`\xFF\xFE" . utf-16le-with-signature)
1596 ("\\`\xEF\xBB\xBF" . utf-8))
1597 "Alist of patterns vs corresponding coding systems.
1598 Each element looks like (REGEXP . CODING-SYSTEM).
1599 A file whose first bytes match REGEXP is decoded by CODING-SYSTEM on reading.
1600
1601 The settings in this alist take priority over `coding:' tags
1602 in the file (see the function `set-auto-coding')
1603 and the contents of `file-coding-system-alist'."
1604 :group 'files
1605 :group 'mule
1606 :type '(repeat (cons (regexp :tag "Regexp")
1607 (symbol :tag "Coding system"))))
1608
1609 (defun auto-coding-regexp-alist-lookup (from to)
1610 "Lookup `auto-coding-regexp-alist' for the contents of the current buffer.
1611 The value is a coding system is specified for the region FROM and TO,
1612 or nil."
1613 (save-excursion
1614 (goto-char from)
1615 (let ((alist auto-coding-regexp-alist)
1616 coding-system)
1617 (while (and alist (not coding-system))
1618 (let ((regexp (car (car alist))))
1619 (if enable-multibyte-characters
1620 (setq regexp (string-to-multibyte regexp)))
1621 (if (re-search-forward regexp to t)
1622 (setq coding-system (cdr (car alist)))
1623 (setq alist (cdr alist)))))
1624 coding-system)))
1625
1626 ;; See the bottom of this file for built-in auto coding functions.
1627 (defcustom auto-coding-functions '(sgml-xml-auto-coding-function
1628 sgml-html-meta-auto-coding-function)
1629 "A list of functions which attempt to determine a coding system.
1630
1631 Each function in this list should be written to operate on the
1632 current buffer, but should not modify it in any way. The buffer
1633 will contain undecoded text of parts of the file. Each function
1634 should take one argument, SIZE, which says how many
1635 characters (starting from point) it should look at.
1636
1637 If one of these functions succeeds in determining a coding
1638 system, it should return that coding system. Otherwise, it
1639 should return nil.
1640
1641 If a file has a `coding:' tag, that takes precedence over these
1642 functions, so they won't be called at all."
1643 :group 'files
1644 :group 'mule
1645 :type '(repeat function))
1646
1647 (defvar set-auto-coding-for-load nil
1648 "Non-nil means look for `load-coding' property instead of `coding'.
1649 This is used for loading and byte-compiling Emacs Lisp files.")
1650
1651 (defun auto-coding-alist-lookup (filename)
1652 "Return the coding system specified by `auto-coding-alist' for FILENAME."
1653 (let ((alist auto-coding-alist)
1654 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos cygwin)))
1655 coding-system)
1656 (while (and alist (not coding-system))
1657 (if (string-match (car (car alist)) filename)
1658 (setq coding-system (cdr (car alist)))
1659 (setq alist (cdr alist))))
1660 coding-system))
1661
1662 (put 'enable-character-translation 'permanent-local t)
1663 (put 'enable-character-translation 'safe-local-variable 'booleanp)
1664
1665 (defun find-auto-coding (filename size)
1666 "Find a coding system for a file FILENAME of which SIZE bytes follow point.
1667 These bytes should include at least the first 1k of the file
1668 and the last 3k of the file, but the middle may be omitted.
1669
1670 The function checks FILENAME against the variable `auto-coding-alist'.
1671 If FILENAME doesn't match any entries in the variable, it checks the
1672 contents of the current buffer following point against
1673 `auto-coding-regexp-alist'. If no match is found, it checks for a
1674 `coding:' tag in the first one or two lines following point. If no
1675 `coding:' tag is found, it checks any local variables list in the last
1676 3K bytes out of the SIZE bytes. Finally, if none of these methods
1677 succeed, it checks to see if any function in `auto-coding-functions'
1678 gives a match.
1679
1680 If a coding system is specifed, the return value is a
1681 cons (CODING . SOURCE), where CODING is the specified coding
1682 system and SOURCE is a symbol `auto-coding-alist',
1683 `auto-coding-regexp-alist', `coding:', or `auto-coding-functions'
1684 indicating by what CODING is specified. Note that the validity
1685 of CODING is not checked; it's callers responsibility to check it.
1686
1687 If nothing is specified, the return value is nil."
1688 (or (let ((coding-system (auto-coding-alist-lookup filename)))
1689 (if coding-system
1690 (cons coding-system 'auto-coding-alist)))
1691 ;; Try using `auto-coding-regexp-alist'.
1692 (let ((coding-system (auto-coding-regexp-alist-lookup (point)
1693 (+ (point) size))))
1694 (if coding-system
1695 (cons coding-system 'auto-coding-regexp-alist)))
1696 (let* ((case-fold-search t)
1697 (head-start (point))
1698 (head-end (+ head-start (min size 1024)))
1699 (tail-start (+ head-start (max (- size 3072) 0)))
1700 (tail-end (+ head-start size))
1701 coding-system head-found tail-found pos char-trans)
1702 ;; Try a short cut by searching for the string "coding:"
1703 ;; and for "unibyte:" at the head and tail of SIZE bytes.
1704 (setq head-found (or (search-forward "coding:" head-end t)
1705 (search-forward "unibyte:" head-end t)
1706 (search-forward "enable-character-translation:"
1707 head-end t)))
1708 (if (and head-found (> head-found tail-start))
1709 ;; Head and tail are overlapped.
1710 (setq tail-found head-found)
1711 (goto-char tail-start)
1712 (setq tail-found (or (search-forward "coding:" tail-end t)
1713 (search-forward "unibyte:" tail-end t)
1714 (search-forward "enable-character-translation:"
1715 tail-end t))))
1716
1717 ;; At first check the head.
1718 (when head-found
1719 (goto-char head-start)
1720 (setq head-end (set-auto-mode-1))
1721 (setq head-start (point))
1722 (when (and head-end (< head-found head-end))
1723 (goto-char head-start)
1724 (when (and set-auto-coding-for-load
1725 (re-search-forward
1726 "\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
1727 head-end t))
1728 (setq coding-system 'raw-text))
1729 (when (and (not coding-system)
1730 (re-search-forward
1731 "\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
1732 head-end t))
1733 (setq coding-system (intern (match-string 2))))
1734 (when (re-search-forward
1735 "\\(.*;\\)?[ \t]*enable-character-translation:[ \t]*\\([^ ;]+\\)"
1736 head-end t)
1737 (setq char-trans (match-string 2)))))
1738
1739 ;; If no coding: tag in the head, check the tail.
1740 ;; Here we must pay attention to the case that the end-of-line
1741 ;; is just "\r" and we can't use "^" nor "$" in regexp.
1742 (when (and tail-found (or (not coding-system) (not char-trans)))
1743 (goto-char tail-start)
1744 (re-search-forward "[\r\n]\^L" nil t)
1745 (if (re-search-forward
1746 "[\r\n]\\([^[\r\n]*\\)[ \t]*Local Variables:[ \t]*\\([^\r\n]*\\)[\r\n]"
1747 tail-end t)
1748 ;; The prefix is what comes before "local variables:" in its
1749 ;; line. The suffix is what comes after "local variables:"
1750 ;; in its line.
1751 (let* ((prefix (regexp-quote (match-string 1)))
1752 (suffix (regexp-quote (match-string 2)))
1753 (re-coding
1754 (concat
1755 "[\r\n]" prefix
1756 ;; N.B. without the \n below, the regexp can
1757 ;; eat newlines.
1758 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
1759 suffix "[\r\n]"))
1760 (re-unibyte
1761 (concat
1762 "[\r\n]" prefix
1763 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
1764 suffix "[\r\n]"))
1765 (re-char-trans
1766 (concat
1767 "[\r\n]" prefix
1768 "[ \t]*enable-character-translation[ \t]*:[ \t]*\\([^ \t\r\n]+\\)[ \t]*"
1769 suffix "[\r\n]"))
1770 (re-end
1771 (concat "[\r\n]" prefix "[ \t]*End *:[ \t]*" suffix
1772 "[\r\n]?"))
1773 (pos (1- (point))))
1774 (forward-char -1) ; skip back \r or \n.
1775 (re-search-forward re-end tail-end 'move)
1776 (setq tail-end (point))
1777 (goto-char pos)
1778 (when (and set-auto-coding-for-load
1779 (re-search-forward re-unibyte tail-end t))
1780 (setq coding-system 'raw-text))
1781 (when (and (not coding-system)
1782 (re-search-forward re-coding tail-end t))
1783 (setq coding-system (intern (match-string 1))))
1784 (when (and (not char-trans)
1785 (re-search-forward re-char-trans tail-end t))
1786 (setq char-trans (match-string 1))))))
1787 (if coding-system
1788 ;; If the coding-system name ends with "!", remove it and
1789 ;; set char-trans to "nil".
1790 (let ((name (symbol-name coding-system)))
1791 (if (= (aref name (1- (length name))) ?!)
1792 (setq coding-system (intern (substring name 0 -1))
1793 char-trans "nil"))))
1794 (when (and char-trans
1795 (not (setq char-trans (intern char-trans))))
1796 (make-local-variable 'enable-character-translation)
1797 (setq enable-character-translation nil))
1798 (if coding-system
1799 (cons coding-system :coding)))
1800 ;; Finally, try all the `auto-coding-functions'.
1801 (let ((funcs auto-coding-functions)
1802 (coding-system nil))
1803 (while (and funcs (not coding-system))
1804 (setq coding-system (condition-case e
1805 (save-excursion
1806 (goto-char (point-min))
1807 (funcall (pop funcs) size))
1808 (error nil))))
1809 (if coding-system
1810 (cons coding-system 'auto-coding-functions)))))
1811
1812 (defun set-auto-coding (filename size)
1813 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1814 See `find-auto-coding' for how the coding system is found.
1815 Return nil if an invalid coding system is found.
1816
1817 The variable `set-auto-coding-function' (which see) is set to this
1818 function by default."
1819 (let ((found (find-auto-coding filename size)))
1820 (if (and found (coding-system-p (car found)))
1821 (car found))))
1822
1823 (setq set-auto-coding-function 'set-auto-coding)
1824
1825 ;; This variable is set in these two cases:
1826 ;; (1) A file is read by a coding system specified explicitly.
1827 ;; after-insert-file-set-coding sets this value to
1828 ;; coding-system-for-read.
1829 ;; (2) A buffer is saved.
1830 ;; After writing, basic-save-buffer-1 sets this value to
1831 ;; last-coding-system-used.
1832 ;; This variable is used for decoding in revert-buffer.
1833 (defvar buffer-file-coding-system-explicit nil
1834 "The file coding system explicitly specified for the current buffer.
1835 Internal use only.")
1836 (make-variable-buffer-local 'buffer-file-coding-system-explicit)
1837 (put 'buffer-file-coding-system-explicit 'permanent-local t)
1838
1839 (defun after-insert-file-set-coding (inserted &optional visit)
1840 "Set `buffer-file-coding-system' of current buffer after text is inserted.
1841 INSERTED is the number of characters that were inserted, as figured
1842 in the situation before this function. Return the number of characters
1843 inserted, as figured in the situation after. The two numbers can be
1844 different if the buffer has become unibyte.
1845 The optional second arg VISIT non-nil means that we are visiting a file."
1846 (if (and visit
1847 coding-system-for-read
1848 (not (eq coding-system-for-read 'auto-save-coding)))
1849 (setq buffer-file-coding-system-explicit coding-system-for-read))
1850 (if last-coding-system-used
1851 (let ((coding-system
1852 (find-new-buffer-file-coding-system last-coding-system-used))
1853 (modified-p (buffer-modified-p)))
1854 (when coding-system
1855 ;; Tell set-buffer-file-coding-system not to mark the file
1856 ;; as modified; we just read it, and it's supposed to be unmodified.
1857 ;; Marking it modified would try to lock it, which would
1858 ;; check the modtime, and we don't want to do that again now.
1859 (set-buffer-file-coding-system coding-system t t)
1860 (if (and enable-multibyte-characters
1861 (or (eq coding-system 'no-conversion)
1862 (eq (coding-system-type coding-system) 5))
1863 ;; If buffer was unmodified and the size is the
1864 ;; same as INSERTED, we must be visiting it.
1865 (not modified-p)
1866 (= (buffer-size) inserted))
1867 ;; For coding systems no-conversion and raw-text...,
1868 ;; edit the buffer as unibyte.
1869 (let ((pos-marker (copy-marker (+ (point) inserted)))
1870 ;; Prevent locking.
1871 (buffer-file-name nil))
1872 (if visit
1873 ;; If we're doing this for find-file,
1874 ;; don't record undo info; this counts as
1875 ;; part of producing the buffer's initial contents.
1876 (let ((buffer-undo-list t))
1877 (set-buffer-multibyte nil))
1878 (set-buffer-multibyte nil))
1879 (setq inserted (- pos-marker (point)))))
1880 (restore-buffer-modified-p modified-p))))
1881 inserted)
1882
1883 ;; The coding-spec and eol-type of coding-system returned is decided
1884 ;; independently in the following order.
1885 ;; 1. That of buffer-file-coding-system locally bound.
1886 ;; 2. That of CODING.
1887
1888 (defun find-new-buffer-file-coding-system (coding)
1889 "Return a coding system for a buffer when a file of CODING is inserted.
1890 The local variable `buffer-file-coding-system' of the current buffer
1891 is set to the returned value.
1892 Return nil if there's no need to set `buffer-file-coding-system'."
1893 (let (local-coding local-eol
1894 found-coding found-eol
1895 new-coding new-eol)
1896 (if (null coding)
1897 ;; Nothing found about coding.
1898 nil
1899
1900 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1901 ;; and LOCAL-CODING.
1902 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1903 (if (null (numberp local-eol))
1904 ;; But eol-type is not yet set.
1905 (setq local-eol nil))
1906 (if (and buffer-file-coding-system
1907 (not (eq (coding-system-type buffer-file-coding-system) t)))
1908 ;; This is not `undecided'.
1909 (setq local-coding (coding-system-base buffer-file-coding-system)))
1910
1911 (if (and (local-variable-p 'buffer-file-coding-system)
1912 local-eol local-coding)
1913 ;; The current buffer has already set full coding-system, we
1914 ;; had better not change it.
1915 nil
1916
1917 (setq found-eol (coding-system-eol-type coding))
1918 (if (null (numberp found-eol))
1919 ;; But eol-type is not found.
1920 ;; If EOL conversions are inhibited, force unix eol-type.
1921 (setq found-eol (if inhibit-eol-conversion 0)))
1922 (if (eq (coding-system-type coding) t)
1923 (setq found-coding 'undecided)
1924 (setq found-coding (coding-system-base coding)))
1925
1926 (if (and (not found-eol) (eq found-coding 'undecided))
1927 ;; No valid coding information found.
1928 nil
1929
1930 ;; Some coding information (eol or text) found.
1931
1932 ;; The local setting takes precedence over the found one.
1933 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1934 (or local-coding found-coding)
1935 (or found-coding local-coding)))
1936 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1937 (or local-eol found-eol)
1938 (or found-eol local-eol)))
1939
1940 (let ((eol-type (coding-system-eol-type new-coding)))
1941 (if (and (numberp new-eol) (vectorp eol-type))
1942 (aref eol-type new-eol)
1943 new-coding)))))))
1944
1945 (defun modify-coding-system-alist (target-type regexp coding-system)
1946 "Modify one of look up tables for finding a coding system on I/O operation.
1947 There are three of such tables, `file-coding-system-alist',
1948 `process-coding-system-alist', and `network-coding-system-alist'.
1949
1950 TARGET-TYPE specifies which of them to modify.
1951 If it is `file', it affects `file-coding-system-alist' (which see).
1952 If it is `process', it affects `process-coding-system-alist' (which see).
1953 If it is `network', it affects `network-coding-system-alist' (which see).
1954
1955 REGEXP is a regular expression matching a target of I/O operation.
1956 The target is a file name if TARGET-TYPE is `file', a program name if
1957 TARGET-TYPE is `process', or a network service name or a port number
1958 to connect to if TARGET-TYPE is `network'.
1959
1960 CODING-SYSTEM is a coding system to perform code conversion on the I/O
1961 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1962 for decoding and encoding respectively,
1963 or a function symbol which, when called, returns such a cons cell."
1964 (or (memq target-type '(file process network))
1965 (error "Invalid target type: %s" target-type))
1966 (or (stringp regexp)
1967 (and (eq target-type 'network) (integerp regexp))
1968 (error "Invalid regular expression: %s" regexp))
1969 (if (symbolp coding-system)
1970 (if (not (fboundp coding-system))
1971 (progn
1972 (check-coding-system coding-system)
1973 (setq coding-system (cons coding-system coding-system))))
1974 (check-coding-system (car coding-system))
1975 (check-coding-system (cdr coding-system)))
1976 (cond ((eq target-type 'file)
1977 (let ((slot (assoc regexp file-coding-system-alist)))
1978 (if slot
1979 (setcdr slot coding-system)
1980 (setq file-coding-system-alist
1981 (cons (cons regexp coding-system)
1982 file-coding-system-alist)))))
1983 ((eq target-type 'process)
1984 (let ((slot (assoc regexp process-coding-system-alist)))
1985 (if slot
1986 (setcdr slot coding-system)
1987 (setq process-coding-system-alist
1988 (cons (cons regexp coding-system)
1989 process-coding-system-alist)))))
1990 (t
1991 (let ((slot (assoc regexp network-coding-system-alist)))
1992 (if slot
1993 (setcdr slot coding-system)
1994 (setq network-coding-system-alist
1995 (cons (cons regexp coding-system)
1996 network-coding-system-alist)))))))
1997
1998 (defun decode-coding-inserted-region (from to filename
1999 &optional visit beg end replace)
2000 "Decode the region between FROM and TO as if it is read from file FILENAME.
2001 The idea is that the text between FROM and TO was just inserted somehow.
2002 Optional arguments VISIT, BEG, END, and REPLACE are the same as those
2003 of the function `insert-file-contents'.
2004 Part of the job of this function is setting `buffer-undo-list' appropriately."
2005 (save-excursion
2006 (save-restriction
2007 (let ((coding coding-system-for-read)
2008 undo-list-saved)
2009 (if visit
2010 ;; Temporarily turn off undo recording, if we're decoding the
2011 ;; text of a visited file.
2012 (setq buffer-undo-list t)
2013 ;; Otherwise, if we can recognize the undo elt for the insertion,
2014 ;; remove it and get ready to replace it later.
2015 ;; In the mean time, turn off undo recording.
2016 (let ((last (car-safe buffer-undo-list)))
2017 (if (and (consp last) (eql (car last) from) (eql (cdr last) to))
2018 (setq undo-list-saved (cdr buffer-undo-list)
2019 buffer-undo-list t))))
2020 (narrow-to-region from to)
2021 (goto-char (point-min))
2022 (or coding
2023 (setq coding (funcall set-auto-coding-function
2024 filename (- (point-max) (point-min)))))
2025 (or coding
2026 (setq coding (car (find-operation-coding-system
2027 'insert-file-contents
2028 (cons filename (current-buffer))
2029 visit beg end replace))))
2030 (if (coding-system-p coding)
2031 (or enable-multibyte-characters
2032 (setq coding
2033 (coding-system-change-text-conversion coding 'raw-text)))
2034 (setq coding nil))
2035 (if coding
2036 (decode-coding-region (point-min) (point-max) coding)
2037 (setq last-coding-system-used coding))
2038 ;; If we're decoding the text of a visited file,
2039 ;; the undo list should start out empty.
2040 (if visit
2041 (setq buffer-undo-list nil)
2042 ;; If we decided to replace the undo entry for the insertion,
2043 ;; do so now.
2044 (if undo-list-saved
2045 (setq buffer-undo-list
2046 (cons (cons from (point-max)) undo-list-saved))))))))
2047
2048 (defun recode-region (start end new-coding coding)
2049 "Re-decode the region (previously decoded by CODING) by NEW-CODING."
2050 (interactive
2051 (list (region-beginning) (region-end)
2052 (read-coding-system "Text was really in: ")
2053 (let ((coding (or buffer-file-coding-system last-coding-system-used)))
2054 (read-coding-system
2055 (concat "But was interpreted as"
2056 (if coding (format " (default %S): " coding) ": "))
2057 coding))))
2058 (or (and new-coding coding)
2059 (error "Coding system not specified"))
2060 ;; Check it before we encode the region.
2061 (check-coding-system new-coding)
2062 (save-restriction
2063 (narrow-to-region start end)
2064 (encode-coding-region (point-min) (point-max) coding)
2065 (decode-coding-region (point-min) (point-max) new-coding)))
2066
2067 (defun make-translation-table (&rest args)
2068 "Make a translation table from arguments.
2069 A translation table is a char table intended for character
2070 translation in CCL programs.
2071
2072 Each argument is a list of elements of the form (FROM . TO), where FROM
2073 is a character to be translated to TO.
2074
2075 FROM can be a generic character (see `make-char'). In this case, TO is
2076 a generic character containing the same number of characters, or an
2077 ordinary character. If FROM and TO are both generic characters, all
2078 characters belonging to FROM are translated to characters belonging to TO
2079 without changing their position code(s).
2080
2081 The arguments and forms in each argument are processed in the given
2082 order, and if a previous form already translates TO to some other
2083 character, say TO-ALT, FROM is also translated to TO-ALT."
2084 (let ((table (make-char-table 'translation-table))
2085 revlist)
2086 (while args
2087 (let ((elts (car args)))
2088 (while elts
2089 (let* ((from (car (car elts)))
2090 (from-i 0) ; degree of freedom of FROM
2091 (from-rev (nreverse (split-char from)))
2092 (to (cdr (car elts)))
2093 (to-i 0) ; degree of freedom of TO
2094 (to-rev (nreverse (split-char to))))
2095 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
2096 (while (eq (car from-rev) 0)
2097 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
2098 (while (eq (car to-rev) 0)
2099 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
2100 (if (and (/= from-i to-i) (/= to-i 0))
2101 (error "Invalid character pair (%d . %d)" from to))
2102 ;; If we have already translated TO to TO-ALT, FROM should
2103 ;; also be translated to TO-ALT. But, this is only if TO
2104 ;; is a generic character or TO-ALT is not a generic
2105 ;; character.
2106 (let ((to-alt (aref table to)))
2107 (if (and to-alt
2108 (or (> to-i 0) (not (generic-char-p to-alt))))
2109 (setq to to-alt)))
2110 (if (> from-i 0)
2111 (set-char-table-default table from to)
2112 (aset table from to))
2113 ;; If we have already translated some chars to FROM, they
2114 ;; should also be translated to TO.
2115 (let ((l (assq from revlist)))
2116 (if l
2117 (let ((ch (car l)))
2118 (setcar l to)
2119 (setq l (cdr l))
2120 (while l
2121 (aset table ch to)
2122 (setq l (cdr l)) ))))
2123 ;; Now update REVLIST.
2124 (let ((l (assq to revlist)))
2125 (if l
2126 (setcdr l (cons from (cdr l)))
2127 (setq revlist (cons (list to from) revlist)))))
2128 (setq elts (cdr elts))))
2129 (setq args (cdr args)))
2130 ;; Return TABLE just created.
2131 table))
2132
2133 (defun make-translation-table-from-vector (vec)
2134 "Make translation table from decoding vector VEC.
2135 VEC is an array of 256 elements to map unibyte codes to multibyte
2136 characters. Elements may be nil for undefined code points.
2137 See also the variable `nonascii-translation-table'."
2138 (let ((table (make-char-table 'translation-table))
2139 (rev-table (make-char-table 'translation-table))
2140 ch)
2141 (dotimes (i 256)
2142 (setq ch (aref vec i))
2143 (when ch
2144 (aset table i ch)
2145 (if (>= ch 256)
2146 (aset rev-table ch i))))
2147 (set-char-table-extra-slot table 0 rev-table)
2148 table))
2149
2150 (defun define-translation-table (symbol &rest args)
2151 "Define SYMBOL as the name of translation table made by ARGS.
2152 This sets up information so that the table can be used for
2153 translations in a CCL program.
2154
2155 If the first element of ARGS is a char-table whose purpose is
2156 `translation-table', just define SYMBOL to name it. (Note that this
2157 function does not bind SYMBOL.)
2158
2159 Any other ARGS should be suitable as arguments of the function
2160 `make-translation-table' (which see).
2161
2162 This function sets properties `translation-table' and
2163 `translation-table-id' of SYMBOL to the created table itself and the
2164 identification number of the table respectively. It also registers
2165 the table in `translation-table-vector'."
2166 (let ((table (if (and (char-table-p (car args))
2167 (eq (char-table-subtype (car args))
2168 'translation-table))
2169 (car args)
2170 (apply 'make-translation-table args)))
2171 (len (length translation-table-vector))
2172 (id 0)
2173 (done nil))
2174 (put symbol 'translation-table table)
2175 (while (not done)
2176 (if (>= id len)
2177 (setq translation-table-vector
2178 (vconcat translation-table-vector (make-vector len nil))))
2179 (let ((slot (aref translation-table-vector id)))
2180 (if (or (not slot)
2181 (eq (car slot) symbol))
2182 (progn
2183 (aset translation-table-vector id (cons symbol table))
2184 (setq done t))
2185 (setq id (1+ id)))))
2186 (put symbol 'translation-table-id id)
2187 id))
2188
2189 (defun translate-region (start end table)
2190 "From START to END, translate characters according to TABLE.
2191 TABLE is a string or a char-table.
2192 If TABLE is a string, the Nth character in it is the mapping
2193 for the character with code N.
2194 If TABLE is a char-table, the element for character N is the mapping
2195 for the character with code N.
2196 It returns the number of characters changed."
2197 (interactive
2198 (list (region-beginning)
2199 (region-end)
2200 (let (table l)
2201 (dotimes (i (length translation-table-vector))
2202 (if (consp (aref translation-table-vector i))
2203 (push (list (symbol-name
2204 (car (aref translation-table-vector i)))) l)))
2205 (if (not l)
2206 (error "No translation table defined"))
2207 (while (not table)
2208 (setq table (completing-read "Translation table: " l nil t)))
2209 (intern table))))
2210 (if (symbolp table)
2211 (let ((val (get table 'translation-table)))
2212 (or (char-table-p val)
2213 (error "Invalid translation table name: %s" table))
2214 (setq table val)))
2215 (translate-region-internal start end table))
2216
2217 (put 'with-category-table 'lisp-indent-function 1)
2218
2219 (defmacro with-category-table (table &rest body)
2220 "Evaluate BODY with category table of current buffer set to TABLE.
2221 The category table of the current buffer is saved, BODY is evaluated,
2222 then the saved table is restored, even in case of an abnormal exit.
2223 Value is what BODY returns."
2224 (let ((old-table (make-symbol "old-table"))
2225 (old-buffer (make-symbol "old-buffer")))
2226 `(let ((,old-table (category-table))
2227 (,old-buffer (current-buffer)))
2228 (unwind-protect
2229 (progn
2230 (set-category-table ,table)
2231 ,@body)
2232 (with-current-buffer ,old-buffer
2233 (set-category-table ,old-table))))))
2234
2235 (defun define-translation-hash-table (symbol table)
2236 "Define SYMBOL as the name of the hash translation TABLE for use in CCL.
2237
2238 Analogous to `define-translation-table', but updates
2239 `translation-hash-table-vector' and the table is for use in the CCL
2240 `lookup-integer' and `lookup-character' functions."
2241 (unless (and (symbolp symbol)
2242 (hash-table-p table))
2243 (error "Bad args to define-translation-hash-table"))
2244 (let ((len (length translation-hash-table-vector))
2245 (id 0)
2246 done)
2247 (put symbol 'translation-hash-table table)
2248 (while (not done)
2249 (if (>= id len)
2250 (setq translation-hash-table-vector
2251 (vconcat translation-hash-table-vector [nil])))
2252 (let ((slot (aref translation-hash-table-vector id)))
2253 (if (or (not slot)
2254 (eq (car slot) symbol))
2255 (progn
2256 (aset translation-hash-table-vector id (cons symbol table))
2257 (setq done t))
2258 (setq id (1+ id)))))
2259 (put symbol 'translation-hash-table-id id)
2260 id))
2261
2262 ;;; Initialize some variables.
2263
2264 (put 'use-default-ascent 'char-table-extra-slots 0)
2265 (setq use-default-ascent (make-char-table 'use-default-ascent))
2266 (put 'ignore-relative-composition 'char-table-extra-slots 0)
2267 (setq ignore-relative-composition
2268 (make-char-table 'ignore-relative-composition))
2269
2270
2271 ;;; Built-in auto-coding-functions:
2272
2273 (defun sgml-xml-auto-coding-function (size)
2274 "Determine whether the buffer is XML, and if so, its encoding.
2275 This function is intended to be added to `auto-coding-functions'."
2276 (setq size (+ (point) size))
2277 (when (re-search-forward "\\`[[:space:]\n]*<\\?xml" size t)
2278 (let ((end (save-excursion
2279 ;; This is a hack.
2280 (re-search-forward "[\"']\\s-*\\?>" size t))))
2281 (when end
2282 (if (re-search-forward "encoding=[\"']\\(.+?\\)[\"']" end t)
2283 (let* ((match (match-string 1))
2284 (sym (intern (downcase match))))
2285 (if (coding-system-p sym)
2286 sym
2287 (message "Warning: unknown coding system \"%s\"" match)
2288 nil))
2289 'utf-8)))))
2290
2291 (defun sgml-html-meta-auto-coding-function (size)
2292 "If the buffer has an HTML meta tag, use it to determine encoding.
2293 This function is intended to be added to `auto-coding-functions'."
2294 (setq size (min (+ (point) size)
2295 (save-excursion
2296 ;; Limit the search by the end of the HTML header.
2297 (or (search-forward "</head>" size t)
2298 ;; In case of no header, search only 10 lines.
2299 (forward-line 10))
2300 (point))))
2301 (when (re-search-forward "<meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']text/\\sw+;\\s-*charset=\\(.+?\\)[\"']" size t)
2302 (let* ((match (match-string 1))
2303 (sym (intern (downcase match))))
2304 (if (coding-system-p sym)
2305 sym
2306 (message "Warning: unknown coding system \"%s\"" match)
2307 nil))))
2308
2309 ;;;
2310 (provide 'mule)
2311
2312 ;; arch-tag: 9aebaa6e-0e8a-40a9-b857-cb5d04a39e7c
2313 ;;; mule.el ends here