]> code.delx.au - gnu-emacs/blob - lisp/international/mule.el
(after-insert-file-set-buffer-file-coding-system): If the buffer
[gnu-emacs] / lisp / international / mule.el
1 ;;; mule.el --- basic commands for mulitilingual environment
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual, character set, coding system
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (defconst mule-version "5.0 (AOI)" "\
28 Version number and name of this version of MULE (multilingual environment).")
29
30 (defconst mule-version-date "1999.12.7" "\
31 Distribution date of this version of MULE (multilingual environment).")
32
33 (defun load-with-code-conversion (fullname file &optional noerror nomessage)
34 "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
35 The file contents are decoded before evaluation if necessary.
36 If optional second arg NOERROR is non-nil,
37 report no error if FILE doesn't exist.
38 Print messages at start and end of loading unless
39 optional third arg NOMESSAGE is non-nil.
40 Return t if file exists."
41 (if (null (file-readable-p fullname))
42 (and (null noerror)
43 (signal 'file-error (list "Cannot open load file" file)))
44 ;; Read file with code conversion, and then eval.
45 (let* ((buffer
46 ;; To avoid any autoloading, set default-major-mode to
47 ;; fundamental-mode.
48 ;; So that we don't get completely screwed if the
49 ;; file is encoded in some complicated character set,
50 ;; read it with real decoding, as a multibyte buffer,
51 ;; even if this is a --unibyte Emacs session.
52 (let ((default-major-mode 'fundamental-mode)
53 (default-enable-multibyte-characters t))
54 ;; We can't use `generate-new-buffer' because files.el
55 ;; is not yet loaded.
56 (get-buffer-create (generate-new-buffer-name " *load*"))))
57 (load-in-progress t)
58 (source (save-match-data (string-match "\\.el\\'" fullname))))
59 (unless nomessage
60 (if source
61 (message "Loading %s (source)..." file)
62 (message "Loading %s..." file)))
63 (when purify-flag
64 (setq preloaded-file-list (cons file preloaded-file-list)))
65 (unwind-protect
66 (let ((load-file-name fullname)
67 (set-auto-coding-for-load t)
68 (inhibit-file-name-operation nil))
69 (save-excursion
70 (set-buffer buffer)
71 (insert-file-contents fullname)
72 ;; If the loaded file was inserted with no-conversion or
73 ;; raw-text coding system, make the buffer unibyte.
74 ;; Otherwise, eval-buffer might try to interpret random
75 ;; binary junk as multibyte characters.
76 (if (and enable-multibyte-characters
77 (or (eq (coding-system-type last-coding-system-used) 5)
78 (eq last-coding-system-used 'no-conversion)))
79 (set-buffer-multibyte nil))
80 ;; Make `kill-buffer' quiet.
81 (set-buffer-modified-p nil))
82 ;; Have the original buffer current while we eval.
83 (eval-buffer buffer nil file
84 ;; If this Emacs is running with --unibyte,
85 ;; convert multibyte strings to unibyte
86 ;; after reading them.
87 ;; (not default-enable-multibyte-characters)
88 nil t
89 ))
90 (let (kill-buffer-hook kill-buffer-query-functions)
91 (kill-buffer buffer)))
92 (let ((hook (assoc file after-load-alist)))
93 (when hook
94 (mapcar (function eval) (cdr hook))))
95 (unless (or nomessage noninteractive)
96 (if source
97 (message "Loading %s (source)...done" file)
98 (message "Loading %s...done" file)))
99 t)))
100
101 ;; API (Application Program Interface) for charsets.
102
103 ;; Return t if OBJ is a quoted symbol
104 ;; and the symbol is the name of a standard charset.
105 (defsubst charset-quoted-standard-p (obj)
106 (and (listp obj) (eq (car obj) 'quote)
107 (symbolp (car-safe (cdr obj)))
108 (let ((vector (get (car-safe (cdr obj)) 'charset)))
109 (and (vectorp vector)
110 (< (aref vector 0) 160)))))
111
112 (defsubst charsetp (object)
113 "T if OBJECT is a charset."
114 (and (symbolp object) (vectorp (get object 'charset))))
115
116 (defsubst charset-info (charset)
117 "Return a vector of information of CHARSET.
118 The elements of the vector are:
119 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
120 LEADING-CODE-BASE, LEADING-CODE-EXT,
121 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
122 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
123 PLIST,
124 where
125 CHARSET-ID (integer) is the identification number of the charset.
126 BYTES (integer) is the length of multi-byte form of a character in
127 the charset: one of 1, 2, 3, and 4.
128 DIMENSION (integer) is the number of bytes to represent a character of
129 the charset: 1 or 2.
130 CHARS (integer) is the number of characters in a dimension: 94 or 96.
131 WIDTH (integer) is the number of columns a character in the charset
132 occupies on the screen: one of 0, 1, and 2.
133 DIRECTION (integer) is the rendering direction of characters in the
134 charset when rendering. If 0, render from left to right, else
135 render from right to left.
136 LEADING-CODE-BASE (integer) is the base leading-code for the
137 charset.
138 LEADING-CODE-EXT (integer) is the extended leading-code for the
139 charset. All charsets of less than 0xA0 has the value 0.
140 ISO-FINAL-CHAR (character) is the final character of the
141 corresponding ISO 2022 charset.
142 ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
143 while encoding to variants of ISO 2022 coding system, one of the
144 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
145 REVERSE-CHARSET (integer) is the charset which differs only in
146 LEFT-TO-RIGHT value from the charset. If there's no such a
147 charset, the value is -1.
148 SHORT-NAME (string) is the short name to refer to the charset.
149 LONG-NAME (string) is the long name to refer to the charset
150 DESCRIPTION (string) is the description string of the charset.
151 PLIST (property list) may contain any type of information a user
152 want to put and get by functions `put-charset-property' and
153 `get-charset-property' respectively."
154 (get charset 'charset))
155
156 ;; It is better not to use backquote in this file,
157 ;; because that makes a bootstrapping problem
158 ;; if you need to recompile all the Lisp files using interpreted code.
159
160 (defmacro charset-id (charset)
161 "Return charset identification number of CHARSET."
162 (if (charset-quoted-standard-p charset)
163 (aref (charset-info (nth 1 charset)) 0)
164 (list 'aref (list 'charset-info charset) 0)))
165
166 (defmacro charset-bytes (charset)
167 "Return bytes of CHARSET.
168 See the function `charset-info' for more detail."
169 (if (charset-quoted-standard-p charset)
170 (aref (charset-info (nth 1 charset)) 1)
171 (list 'aref (list 'charset-info charset) 1)))
172
173 (defmacro charset-dimension (charset)
174 "Return dimension of CHARSET.
175 See the function `charset-info' for more detail."
176 (if (charset-quoted-standard-p charset)
177 (aref (charset-info (nth 1 charset)) 2)
178 (list 'aref (list 'charset-info charset) 2)))
179
180 (defmacro charset-chars (charset)
181 "Return character numbers contained in a dimension of CHARSET.
182 See the function `charset-info' for more detail."
183 (if (charset-quoted-standard-p charset)
184 (aref (charset-info (nth 1 charset)) 3)
185 (list 'aref (list 'charset-info charset) 3)))
186
187 (defmacro charset-width (charset)
188 "Return width (how many column occupied on a screen) of CHARSET.
189 See the function `charset-info' for more detail."
190 (if (charset-quoted-standard-p charset)
191 (aref (charset-info (nth 1 charset)) 4)
192 (list 'aref (list 'charset-info charset) 4)))
193
194 (defmacro charset-direction (charset)
195 "Return direction of CHARSET.
196 See the function `charset-info' for more detail."
197 (if (charset-quoted-standard-p charset)
198 (aref (charset-info (nth 1 charset)) 5)
199 (list 'aref (list 'charset-info charset) 5)))
200
201 (defmacro charset-iso-final-char (charset)
202 "Return final char of CHARSET.
203 See the function `charset-info' for more detail."
204 (if (charset-quoted-standard-p charset)
205 (aref (charset-info (nth 1 charset)) 8)
206 (list 'aref (list 'charset-info charset) 8)))
207
208 (defmacro charset-iso-graphic-plane (charset)
209 "Return graphic plane of CHARSET.
210 See the function `charset-info' for more detail."
211 (if (charset-quoted-standard-p charset)
212 (aref (charset-info (nth 1 charset)) 9)
213 (list 'aref (list 'charset-info charset) 9)))
214
215 (defmacro charset-reverse-charset (charset)
216 "Return reverse charset of CHARSET.
217 See the function `charset-info' for more detail."
218 (if (charset-quoted-standard-p charset)
219 (aref (charset-info (nth 1 charset)) 10)
220 (list 'aref (list 'charset-info charset) 10)))
221
222 (defmacro charset-short-name (charset)
223 "Return short name of CHARSET.
224 See the function `charset-info' for more detail."
225 (if (charset-quoted-standard-p charset)
226 (aref (charset-info (nth 1 charset)) 11)
227 (list 'aref (list 'charset-info charset) 11)))
228
229 (defmacro charset-long-name (charset)
230 "Return long name of CHARSET.
231 See the function `charset-info' for more detail."
232 (if (charset-quoted-standard-p charset)
233 (aref (charset-info (nth 1 charset)) 12)
234 (list 'aref (list 'charset-info charset) 12)))
235
236 (defmacro charset-description (charset)
237 "Return description of CHARSET.
238 See the function `charset-info' for more detail."
239 (if (charset-quoted-standard-p charset)
240 (aref (charset-info (nth 1 charset)) 13)
241 (list 'aref (list 'charset-info charset) 13)))
242
243 (defmacro charset-plist (charset)
244 "Return list charset property of CHARSET.
245 See the function `charset-info' for more detail."
246 (list 'aref
247 (if (charset-quoted-standard-p charset)
248 (charset-info (nth 1 charset))
249 (list 'charset-info charset))
250 14))
251
252 (defun set-charset-plist (charset plist)
253 "Set CHARSET's property list to PLIST, and return PLIST."
254 (aset (charset-info charset) 14 plist))
255
256 (defun make-char (charset &optional c1 c2)
257 "Return a character of CHARSET and position codes CODE1 and CODE2.
258 CODE1 and CODE2 are optional, but if you don't supply
259 sufficient position codes, return a generic character which stands for
260 all characters or group of characters in the character set.
261 A generic character can be used to index a char table (e.g. syntax-table)."
262 (make-char-internal (charset-id charset) c1 c2))
263
264 (put 'make-char 'byte-compile
265 (function
266 (lambda (form)
267 (let ((charset (nth 1 form)))
268 (if (charset-quoted-standard-p charset)
269 (byte-compile-normal-call
270 (cons 'make-char-internal
271 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
272 (byte-compile-normal-call
273 (cons 'make-char-internal
274 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
275
276 (defun charset-list ()
277 "Return list of charsets ever defined.
278
279 This function is provided for backward compatibility.
280 Now we have the variable `charset-list'."
281 charset-list)
282
283 (defsubst generic-char-p (char)
284 "Return t if and only if CHAR is a generic character.
285 See also the documentation of make-char."
286 (and (>= char 0400)
287 (let ((l (split-char char)))
288 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
289 (not (eq (car l) 'composition))))))
290
291 \f
292 ;; Coding system staffs
293
294 ;; Coding system is a symbol that has the property `coding-system'.
295 ;;
296 ;; The value of the property `coding-system' is a vector of the
297 ;; following format:
298 ;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
299 ;; We call this vector as coding-spec. See comments in src/coding.c
300 ;; for more detail.
301
302 (defconst coding-spec-type-idx 0)
303 (defconst coding-spec-mnemonic-idx 1)
304 (defconst coding-spec-doc-string-idx 2)
305 (defconst coding-spec-plist-idx 3)
306 (defconst coding-spec-flags-idx 4)
307
308 ;; PLIST is a property list of a coding system. To share PLIST among
309 ;; alias coding systems, a coding system has PLIST in coding-spec
310 ;; instead of having it in normal property list of Lisp symbol.
311 ;; Here's a list of coding system properties currently being used.
312 ;;
313 ;; o coding-category
314 ;;
315 ;; The value is a coding category the coding system belongs to. The
316 ;; function `make-coding-system' sets this value automatically
317 ;; unless its argument PROPERTIES specifies this property.
318 ;;
319 ;; o alias-coding-systems
320 ;;
321 ;; The value is a list of coding systems of the same alias group. The
322 ;; first element is the coding system made at first, which we call as
323 ;; `base coding system'. The function `make-coding-system' sets this
324 ;; value automatically and `define-coding-system-alias' updates it.
325 ;;
326 ;; o post-read-conversion
327 ;;
328 ;; The value is a function to call after some text is inserted and
329 ;; decoded by the coding system itself and before any functions in
330 ;; `after-insert-functions' are called. The arguments to this
331 ;; function is the same as those of a function in
332 ;; `after-insert-functions', i.e. LENGTH of a text while putting point
333 ;; at the head of the text to be decoded
334 ;;
335 ;; o pre-write-conversion
336 ;;
337 ;; The value is a function to call after all functions in
338 ;; `write-region-annotate-functions' and `buffer-file-format' are
339 ;; called, and before the text is encoded by the coding system itself.
340 ;; The arguments to this function is the same as those of a function
341 ;; in `write-region-annotate-functions', i.e. FROM and TO specifying
342 ;; region of a text.
343 ;;
344 ;; o translation-table-for-decode
345 ;;
346 ;; The value is a translation table to be applied on decoding. See
347 ;; the function `make-translation-table' for the format of translation
348 ;; table.
349 ;;
350 ;; o translation-table-for-encode
351 ;;
352 ;; The value is a translation table to be applied on encoding.
353 ;;
354 ;; o safe-charsets
355 ;;
356 ;; The value is a list of charsets safely supported by the coding
357 ;; system. The value t means that all charsets Emacs handles are
358 ;; supported. Even if some charset is not in this list, it doesn't
359 ;; mean that the charset can't be encoded in the coding system,
360 ;; instead, it just means that some other receiver of a text encoded
361 ;; in the coding system won't be able to handle that charset.
362 ;;
363 ;; o mime-charset
364 ;;
365 ;; The value is a symbol of which name is `MIME-charset' parameter of
366 ;; the coding system.
367 ;;
368 ;; o charset-origin-alist
369 ;;
370 ;; The value is a list of this form:
371 ;; (CHARSET EXTERNAL-CHARSET-NAME ENCODING-FUNCTION).
372 ;; ENCODING-FUNCTION is a function to encode a character in CHARSET
373 ;; to the code in EXTERNAL-CHARSET-NAME. The command what-cursor-position
374 ;; uses this information of the buffer-file-coding-system.
375 ;; ENCODING-FUNCTION may be a translation table or a symbol whose
376 ;; property `translation-table' is a translation table. In these case,
377 ;; the translation table is used to encode the character.
378 ;;
379 ;; o valid-codes (meaningful only for a coding system based on CCL)
380 ;;
381 ;; The value is a list to indicate valid byte ranges of the encoded
382 ;; file. Each element of the list is an integer or a cons of integer.
383 ;; In the former case, the integer value is a valid byte code. In the
384 ;; latter case, the integers specifies the range of valid byte codes.
385
386
387 ;; Return coding-spec of CODING-SYSTEM
388 (defsubst coding-system-spec (coding-system)
389 (get (check-coding-system coding-system) 'coding-system))
390
391 (defun coding-system-type (coding-system)
392 "Return the coding type of CODING-SYSTEM.
393 A coding type is an integer value indicating the encoding method
394 of CODING-SYSTEM. See the function `make-coding-system' for more detail."
395 (aref (coding-system-spec coding-system) coding-spec-type-idx))
396
397 (defun coding-system-mnemonic (coding-system)
398 "Return the mnemonic character of CODING-SYSTEM.
399 The mnemonic character of a coding system is used in mode line
400 to indicate the coding system. If the arg is nil, return ?-."
401 (let ((spec (coding-system-spec coding-system)))
402 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
403
404 (defun coding-system-doc-string (coding-system)
405 "Return the documentation string for CODING-SYSTEM."
406 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
407
408 (defun coding-system-plist (coding-system)
409 "Return the property list of CODING-SYSTEM."
410 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
411
412 (defun coding-system-flags (coding-system)
413 "Return `flags' of CODING-SYSTEM.
414 A `flags' of a coding system is a vector of length 32 indicating detailed
415 information of a coding system. See the function `make-coding-system'
416 for more detail."
417 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
418
419 (defun coding-system-get (coding-system prop)
420 "Extract a value from CODING-SYSTEM's property list for property PROP."
421 (plist-get (coding-system-plist coding-system) prop))
422
423 (defun coding-system-put (coding-system prop val)
424 "Change value in CODING-SYSTEM's property list PROP to VAL."
425 (let ((plist (coding-system-plist coding-system)))
426 (if plist
427 (plist-put plist prop val)
428 (aset (coding-system-spec coding-system) coding-spec-plist-idx
429 (list prop val)))))
430
431 (defun coding-system-category (coding-system)
432 "Return the coding category of CODING-SYSTEM."
433 (coding-system-get coding-system 'coding-category))
434
435 (defun coding-system-base (coding-system)
436 "Return the base coding system of CODING-SYSTEM.
437 A base coding system is what made by `make-coding-system'.
438 Any alias nor subsidiary coding systems are not base coding system."
439 (car (coding-system-get coding-system 'alias-coding-systems)))
440
441 (defalias 'coding-system-parent 'coding-system-base)
442 (make-obsolete 'coding-system-parent 'coding-system-base "20.3")
443
444 ;; Coding system also has a property `eol-type'.
445 ;;
446 ;; This property indicates how the coding system handles end-of-line
447 ;; format. The value is integer 0, 1, 2, or a vector of three coding
448 ;; systems. Each integer value 0, 1, and 2 indicates the format of
449 ;; end-of-line LF, CRLF, and CR respectively. A vector value
450 ;; indicates that the format of end-of-line should be detected
451 ;; automatically. Nth element of the vector is the subsidiary coding
452 ;; system whose `eol-type' property is N.
453
454 (defun coding-system-eol-type (coding-system)
455 "Return eol-type of CODING-SYSTEM.
456 An eol-type is integer 0, 1, 2, or a vector of coding systems.
457
458 Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
459 CRLF, and CR respectively.
460
461 A vector value indicates that a format of end-of-line should be
462 detected automatically. Nth element of the vector is the subsidiary
463 coding system whose eol-type is N."
464 (get coding-system 'eol-type))
465
466 (defun coding-system-lessp (x y)
467 (cond ((eq x 'no-conversion) t)
468 ((eq y 'no-conversion) nil)
469 ((eq x 'emacs-mule) t)
470 ((eq y 'emacs-mule) nil)
471 ((eq x 'undecided) t)
472 ((eq y 'undecided) nil)
473 (t (let ((c1 (coding-system-mnemonic x))
474 (c2 (coding-system-mnemonic y)))
475 (or (< (downcase c1) (downcase c2))
476 (and (not (> (downcase c1) (downcase c2)))
477 (< c1 c2)))))))
478
479 ;; Add CODING-SYSTEM to coding-system-list while keeping it sorted.
480 (defun add-to-coding-system-list (coding-system)
481 (if (or (null coding-system-list)
482 (coding-system-lessp coding-system (car coding-system-list)))
483 (setq coding-system-list (cons coding-system coding-system-list))
484 (let ((len (length coding-system-list))
485 mid (tem coding-system-list))
486 (while (> len 1)
487 (setq mid (nthcdr (/ len 2) tem))
488 (if (coding-system-lessp (car mid) coding-system)
489 (setq tem mid
490 len (- len (/ len 2)))
491 (setq len (/ len 2))))
492 (setcdr tem (cons coding-system (cdr tem))))))
493
494 (defun coding-system-list (&optional base-only)
495 "Return a list of all existing coding systems.
496 If optional arg BASE-ONLY is non-nil, only base coding systems are listed."
497 (let* ((codings (copy-sequence coding-system-list))
498 (tail (cons nil codings)))
499 ;; Remove subsidiary coding systems (eol variants) and alias
500 ;; coding systems (if necessary).
501 (while (cdr tail)
502 (let* ((coding (car (cdr tail)))
503 (aliases (coding-system-get coding 'alias-coding-systems)))
504 (if (or
505 ;; CODING is an eol variant if not in ALIASES.
506 (not (memq coding aliases))
507 ;; CODING is an alias if it is not car of ALIASES.
508 (and base-only (not (eq coding (car aliases)))))
509 (setcdr tail (cdr (cdr tail)))
510 (setq tail (cdr tail)))))
511 codings))
512
513 ;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
514 (defun make-subsidiary-coding-system (coding-system)
515 (let ((coding-spec (coding-system-spec coding-system))
516 (subsidiaries (vector (intern (format "%s-unix" coding-system))
517 (intern (format "%s-dos" coding-system))
518 (intern (format "%s-mac" coding-system))))
519 (i 0)
520 temp)
521 (while (< i 3)
522 (put (aref subsidiaries i) 'coding-system coding-spec)
523 (put (aref subsidiaries i) 'eol-type i)
524 (add-to-coding-system-list (aref subsidiaries i))
525 (setq coding-system-alist
526 (cons (list (symbol-name (aref subsidiaries i)))
527 coding-system-alist))
528 (setq i (1+ i)))
529 subsidiaries))
530
531 (defun make-coding-system (coding-system type mnemonic doc-string
532 &optional
533 flags
534 properties
535 eol-type)
536 "Define a new coding system CODING-SYSTEM (symbol).
537 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
538 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
539 in the following format:
540 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
541
542 TYPE is an integer value indicating the type of the coding system as follows:
543 0: Emacs internal format,
544 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
545 2: ISO-2022 including many variants,
546 3: Big5 used mainly on Chinese PC,
547 4: private, CCL programs provide encoding/decoding algorithm,
548 5: Raw-text, which means that text contains random 8-bit codes.
549
550 MNEMONIC is a character to be displayed on mode line for the coding system.
551
552 DOC-STRING is a documentation string for the coding system.
553
554 FLAGS specifies more detailed information of the coding system as follows:
555
556 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
557 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
558 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
559 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
560 SAFE, ACCEPT-LATIN-EXTRA-CODE.
561 CHARSETn are character sets initially designated to Gn graphic registers.
562 If CHARSETn is nil, Gn is never used.
563 If CHARSETn is t, Gn can be used but nothing designated initially.
564 If CHARSETn is a list of character sets, those character sets are
565 designated to Gn on output, but nothing designated to Gn initially.
566 But, character set `ascii' can be designated only to G0.
567 SHORT-FORM non-nil means use short designation sequence on output.
568 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
569 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
570 SPACE on output.
571 SEVEN non-nil means use 7-bit code only on output.
572 LOCKING-SHIFT non-nil means use locking-shift.
573 SINGLE-SHIFT non-nil means use single-shift.
574 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
575 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
576 NO-ISO6429 non-nil means not use ISO6429's direction specification.
577 INIT-BOL non-nil means any designation state is assumed to be reset
578 to initial at each beginning of line on output.
579 DESIGNATION-BOL non-nil means designation sequences should be placed
580 at beginning of line on output.
581 SAFE non-nil means convert unsafe characters to `?' on output.
582 Unsafe characters are what not specified in SAFE-CHARSET.
583 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
584 a code specified in `latin-extra-code-table' (which see) as a valid
585 code of the coding system.
586
587 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
588 decoding and encoding. CCL programs should be specified by their
589 symbols.
590
591 PROPERTIES is an alist of properties vs the corresponding values.
592 These properties are set in PLIST, a property list. This function
593 also sets properties `coding-category' and `alias-coding-systems'
594 automatically.
595
596 EOL-TYPE specifies the EOL type of the coding-system in one of the
597 following formats:
598
599 o symbol (unix, dos, or mac)
600
601 The symbol `unix' means Unix-like EOL (LF), `dos' means
602 DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).
603
604 o number (0, 1, or 2)
605
606 The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
607 respectively.
608
609 o vector of coding-systems of length 3
610
611 The EOL type is detected automatically for the coding system.
612 And, according to the detected EOL type, one of the coding
613 systems in the vector is selected. Elements of the vector
614 corresponds to Unix-liek EOL, DOS-like EOL, and Mac-like EOL
615 in this order.
616
617 Kludgy features for backward compatibility:
618
619 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
620 treated as a compiled CCL code.
621
622 2. If PROPERTIES is just a list of character sets, the list is set as
623 a value of `safe-charsets' in PLIST."
624 ;; Set a value of `coding-system' property.
625 (let ((coding-spec (make-vector 5 nil))
626 (no-initial-designation t)
627 (no-alternative-designation t)
628 coding-category)
629 (if (or (not (integerp type)) (< type 0) (> type 5))
630 (error "TYPE argument must be 0..5"))
631 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
632 (error "MNEMONIC argument must be an ASCII printable character."))
633 (aset coding-spec coding-spec-type-idx type)
634 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
635 (aset coding-spec coding-spec-doc-string-idx
636 (purecopy (if (stringp doc-string) doc-string "")))
637 (cond ((= type 0)
638 (setq coding-category 'coding-category-emacs-mule))
639 ((= type 1)
640 (setq coding-category 'coding-category-sjis))
641 ((= type 2) ; ISO2022
642 (let ((i 0)
643 (vec (make-vector 32 nil))
644 (g1-designation nil)
645 (fl flags))
646 (while (< i 4)
647 (let ((charset (car fl)))
648 (if (and no-initial-designation
649 (> i 0)
650 (or (charsetp charset)
651 (and (consp charset)
652 (charsetp (car charset)))))
653 (setq no-initial-designation nil))
654 (if (charsetp charset)
655 (if (= i 1) (setq g1-designation charset))
656 (if (consp charset)
657 (let ((tail charset)
658 elt)
659 (while tail
660 (setq elt (car tail))
661 (if (eq elt t)
662 (setq no-alternative-designation nil)
663 (if (and elt (not (charsetp elt)))
664 (error "Invalid charset: %s" elt)))
665 (setq tail (cdr tail)))
666 (setq g1-designation (car charset)))
667 (if charset
668 (if (eq charset t)
669 (setq no-alternative-designation nil)
670 (error "Invalid charset: %s" charset)))))
671 (aset vec i charset))
672 (setq fl (cdr fl) i (1+ i)))
673 (while (and (< i 32) fl)
674 (aset vec i (car fl))
675 (setq fl (cdr fl) i (1+ i)))
676 (aset coding-spec 4 vec)
677 (setq coding-category
678 (if (aref vec 8) ; Use locking-shift.
679 (or (and (aref vec 7) 'coding-category-iso-7-else)
680 'coding-category-iso-8-else)
681 (if (aref vec 7) ; 7-bit only.
682 (if (aref vec 9) ; Use single-shift.
683 'coding-category-iso-7-else
684 (if no-alternative-designation
685 'coding-category-iso-7-tight
686 'coding-category-iso-7))
687 (if (or no-initial-designation
688 (not no-alternative-designation))
689 'coding-category-iso-8-else
690 (if (and (charsetp g1-designation)
691 (= (charset-dimension g1-designation) 2))
692 'coding-category-iso-8-2
693 'coding-category-iso-8-1)))))))
694 ((= type 3)
695 (setq coding-category 'coding-category-big5))
696 ((= type 4) ; private
697 (setq coding-category 'coding-category-ccl)
698 (if (not (consp flags))
699 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
700 (let ((decoder (check-ccl-program
701 (car flags)
702 (intern (format "%s-decoder" coding-system))))
703 (encoder (check-ccl-program
704 (cdr flags)
705 (intern (format "%s-encoder" coding-system)))))
706 (if (and decoder encoder)
707 (aset coding-spec 4 (cons decoder encoder))
708 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
709 (t ; i.e. (= type 5)
710 (setq coding-category 'coding-category-raw-text)))
711
712 (let ((plist (list 'coding-category coding-category
713 'alias-coding-systems (list coding-system))))
714 (if no-initial-designation
715 (plist-put plist 'no-initial-designation t))
716 (if (and properties
717 (or (eq properties t)
718 (not (consp (car properties)))))
719 ;; In the old version, the arg PROPERTIES is a list to be
720 ;; set in PLIST as a value of property `safe-charsets'.
721 (plist-put plist 'safe-charsets properties)
722 ;; In the current version PROPERTIES is a property list.
723 ;; Reflect it into PLIST one by one.
724 (let ((l properties))
725 (while l
726 (plist-put plist (car (car l)) (cdr (car l)))
727 (setq l (cdr l)))))
728 ;; The property `coding-category' may have been set differently
729 ;; through PROPERTIES.
730 (setq coding-category (plist-get plist 'coding-category))
731 (aset coding-spec coding-spec-plist-idx plist))
732 (put coding-system 'coding-system coding-spec)
733 (put coding-category 'coding-systems
734 (cons coding-system (get coding-category 'coding-systems))))
735
736 ;; Next, set a value of `eol-type' property.
737 (if (and (not eol-type)
738 (or (<= type 3) (= type 5)))
739 ;; If EOL-TYPE is nil, set a vector of subsidiary coding
740 ;; systems, each corresponds to a coding system for the detected
741 ;; EOL format.
742 (setq eol-type (make-subsidiary-coding-system coding-system)))
743 (setq eol-type
744 (cond ((or (eq eol-type 'unix) (null eol-type))
745 0)
746 ((eq eol-type 'dos)
747 1)
748 ((eq eol-type 'mac)
749 2)
750 ((or (and (vectorp eol-type)
751 (= (length eol-type) 3))
752 (and (numberp eol-type)
753 (and (>= eol-type 0)
754 (<= eol-type 2))))
755 eol-type)
756 (t
757 (error "Invalid EOL-TYPE spec:%S" eol-type))))
758 (put coding-system 'eol-type eol-type)
759
760 ;; At last, register CODING-SYSTEM in `coding-system-list' and
761 ;; `coding-system-alist'.
762 (add-to-coding-system-list coding-system)
763 (setq coding-system-alist (cons (list (symbol-name coding-system))
764 coding-system-alist))
765
766 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
767 ;; XXX-with-esc variants.
768 (let ((coding-category (coding-system-category coding-system)))
769 (if (or (eq coding-category 'coding-category-iso-8-1)
770 (eq coding-category 'coding-category-iso-8-2))
771 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
772 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system)))
773 (make-coding-system esc type mnemonic doc
774 (if (listp (car flags))
775 (cons (append (car flags) '(t)) (cdr flags))
776 (cons (list (car flags) t) (cdr flags)))
777 properties)
778 (coding-system-put esc 'mime-charset nil)
779 (coding-system-put esc 'safe-charsets t))))
780
781 coding-system)
782
783 (defun define-coding-system-alias (alias coding-system)
784 "Define ALIAS as an alias for coding system CODING-SYSTEM."
785 (put alias 'coding-system (coding-system-spec coding-system))
786 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
787 (add-to-coding-system-list alias)
788 (setq coding-system-alist (cons (list (symbol-name alias))
789 coding-system-alist))
790 (let ((eol-type (coding-system-eol-type coding-system)))
791 (if (vectorp eol-type)
792 (put alias 'eol-type (make-subsidiary-coding-system alias))
793 (put alias 'eol-type eol-type))))
794
795 (defun set-buffer-file-coding-system (coding-system &optional force)
796 "Set the file coding-system of the current buffer to CODING-SYSTEM.
797 This means that when you save the buffer, it will be converted
798 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
799 use \\[list-coding-systems].
800
801 If the buffer's previous file coding-system value specifies end-of-line
802 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
803 merged with the already-specified end-of-line conversion.
804 However, if the optional prefix argument FORCE is non-nil,
805 then CODING-SYSTEM is used exactly as specified.
806
807 This marks the buffer modified so that the succeeding \\[save-buffer]
808 surely saves the buffer with CODING-SYSTEM. From a program, if you
809 don't want to mark the buffer modified, just set the variable
810 `buffer-file-coding-system' directly."
811 (interactive "zCoding system for visited file (default, nil): \nP")
812 (check-coding-system coding-system)
813 (if (null force)
814 (let ((x (coding-system-eol-type buffer-file-coding-system))
815 (y (coding-system-eol-type coding-system)))
816 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
817 (setq coding-system (aref y x)))))
818 (setq buffer-file-coding-system coding-system)
819 (set-buffer-modified-p t)
820 (force-mode-line-update))
821
822 (defvar default-terminal-coding-system nil
823 "Default value for the terminal coding system.
824 This is normally set according to the selected language environment.
825 See also the command `set-terminal-coding-system'.")
826
827 (defun set-terminal-coding-system (coding-system)
828 "Set coding system of your terminal to CODING-SYSTEM.
829 All text output to the terminal will be encoded
830 with the specified coding system.
831 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
832 The default is determined by the selected language environment
833 or by the previous use of this command."
834 (interactive
835 (list (let ((default (if (and (not (terminal-coding-system))
836 default-terminal-coding-system)
837 default-terminal-coding-system)))
838 (read-coding-system
839 (format "Coding system for terminal display (default, %s): "
840 default)
841 default))))
842 (if (and (not coding-system)
843 (not (terminal-coding-system)))
844 (setq coding-system default-terminal-coding-system))
845 (if coding-system
846 (setq default-terminal-coding-system coding-system))
847 (set-terminal-coding-system-internal coding-system)
848 (redraw-frame (selected-frame)))
849
850 (defvar default-keyboard-coding-system nil
851 "Default value of the keyboard coding system.
852 This is normally set according to the selected language environment.
853 See also the command `set-keyboard-coding-system'.")
854
855 (defun set-keyboard-coding-system (coding-system)
856 "Set coding system for keyboard input to CODING-SYSTEM.
857 In addition, this command enables Encoded-kbd minor mode.
858 \(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
859 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
860 The default is determined by the selected language environment
861 or by the previous use of this command."
862 (interactive
863 (list (let ((default (if (and (not (keyboard-coding-system))
864 default-keyboard-coding-system)
865 default-keyboard-coding-system)))
866 (read-coding-system
867 (format "Coding system for keyboard input (default, %s): "
868 default)
869 default))))
870 (if (and (not coding-system)
871 (not (keyboard-coding-system)))
872 (setq coding-system default-keyboard-coding-system))
873 (if coding-system
874 (setq default-keyboard-coding-system coding-system))
875 (set-keyboard-coding-system-internal coding-system)
876 (encoded-kbd-mode (if coding-system 1 0)))
877
878 (defun set-buffer-process-coding-system (decoding encoding)
879 "Set coding systems for the process associated with the current buffer.
880 DECODING is the coding system to be used to decode input from the process,
881 ENCODING is the coding system to be used to encode output to the process.
882
883 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
884 (interactive
885 "zCoding-system for process input: \nzCoding-system for process output: ")
886 (let ((proc (get-buffer-process (current-buffer))))
887 (if (null proc)
888 (error "no process")
889 (check-coding-system decoding)
890 (check-coding-system encoding)
891 (set-process-coding-system proc decoding encoding)))
892 (force-mode-line-update))
893
894 (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
895
896 (defun set-selection-coding-system (coding-system)
897 "Make CODING-SYSTEM used for communicating with other X clients .
898 When sending or receiving text via cut_buffer, selection, and clipboard,
899 the text is encoded or decoded by CODING-SYSTEM."
900 (interactive "zCoding system for X selection: ")
901 (check-coding-system coding-system)
902 (setq selection-coding-system coding-system))
903
904 ;; Coding system lastly specified by the command
905 ;; set-next-selection-coding-system.
906 (defvar last-next-selection-coding-system nil)
907
908 (defun set-next-selection-coding-system (coding-system)
909 "Make CODING-SYSTEM used for the next communication with other X clients.
910 This setting is effective for the next communication only."
911 (interactive
912 (list (read-coding-system
913 (if last-next-selection-coding-system
914 (format "Coding system for the next X selection (default, %S): "
915 last-next-selection-coding-system)
916 "Coding system for the next X selection: ")
917 last-next-selection-coding-system)))
918 (if coding-system
919 (setq last-next-selection-coding-system coding-system)
920 (setq coding-system last-next-selection-coding-system))
921 (check-coding-system coding-system)
922
923 (setq next-selection-coding-system coding-system))
924
925 (defun set-coding-priority (arg)
926 "Set priority of coding categories according to LIST.
927 LIST is a list of coding categories ordered by priority."
928 (let ((l arg)
929 (current-list (copy-sequence coding-category-list)))
930 ;; Check the validity of ARG while deleting coding categories in
931 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
932 ;; contains all coding categories.
933 (while l
934 (if (or (null (get (car l) 'coding-category-index))
935 (null (memq (car l) current-list)))
936 (error "Invalid or duplicated element in argument: %s" arg))
937 (setq current-list (delq (car l) current-list))
938 (setq l (cdr l)))
939 ;; Update `coding-category-list' and return it.
940 (setq coding-category-list (append arg current-list))
941 (set-coding-priority-internal)))
942
943 ;;; FILE I/O
944
945 (defvar auto-coding-alist
946 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
947 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\|TGZ\\)\\'" . no-conversion))
948 "Alist of filename patterns vs corresponding coding systems.
949 Each element looks like (REGEXP . CODING-SYSTEM).
950 A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
951
952 The settings in this alist take priority over `coding:' tags
953 in the file (see the function `set-auto-coding')
954 and the contents of `file-coding-system-alist'.")
955
956 (defvar set-auto-coding-for-load nil
957 "Non-nil means look for `load-coding' property instead of `coding'.
958 This is used for loading and byte-compiling Emacs Lisp files.")
959
960 (defun auto-coding-alist-lookup (filename)
961 "Return the coding system specified by `auto-coding-alist' for FILENAME."
962 (let ((alist auto-coding-alist)
963 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
964 coding-system)
965 (while (and alist (not coding-system))
966 (if (string-match (car (car alist)) filename)
967 (setq coding-system (cdr (car alist)))
968 (setq alist (cdr alist))))
969 coding-system))
970
971 (defun set-auto-coding (filename size)
972 "Return coding system for a file FILENAME of which SIZE bytes follow point.
973 These bytes should include at least the first 1k of the file
974 and the last 3k of the file, but the middle may be omitted.
975
976 It checks FILENAME against the variable `auto-coding-alist'.
977 If FILENAME doesn't match any entries in the variable,
978 it checks for a `coding:' tag in the first one or two lines following
979 point. If no `coding:' tag is found, it checks for local variables
980 list in the last 3K bytes out of the SIZE bytes.
981
982 The return value is the specified coding system,
983 or nil if nothing specified.
984
985 The variable `set-auto-coding-function' (which see) is set to this
986 function by default."
987 (let ((coding-system (auto-coding-alist-lookup filename)))
988
989 (or coding-system
990 (let* ((case-fold-search t)
991 (head-start (point))
992 (head-end (+ head-start (min size 1024)))
993 (tail-start (+ head-start (max (- size 3072) 0)))
994 (tail-end (+ head-start size))
995 coding-system head-found tail-found pos)
996 ;; Try a short cut by searching for the string "coding:"
997 ;; and for "unibyte:" at the head and tail of SIZE bytes.
998 (setq head-found (or (search-forward "coding:" head-end t)
999 (search-forward "unibyte:" head-end t)))
1000 (if (and head-found (> head-found tail-start))
1001 ;; Head and tail are overlapped.
1002 (setq tail-found head-found)
1003 (goto-char tail-start)
1004 (setq tail-found (or (search-forward "coding:" tail-end t)
1005 (search-forward "unibyte:" tail-end t))))
1006
1007 ;; At first check the head.
1008 (when head-found
1009 (goto-char head-start)
1010 (setq pos (re-search-forward "[\n\r]" head-end t))
1011 (if (and pos
1012 (= (char-after head-start) ?#)
1013 (= (char-after (1+ head-start)) ?!))
1014 ;; If the file begins with "#!" (exec interpreter magic),
1015 ;; look for coding frobs in the first two lines. You cannot
1016 ;; necessarily put them in the first line of such a file
1017 ;; without screwing up the interpreter invocation.
1018 (setq pos (search-forward "\n" head-end t)))
1019 (if pos (setq head-end pos))
1020 (when (< head-found head-end)
1021 (goto-char head-start)
1022 (when (and set-auto-coding-for-load
1023 (re-search-forward
1024 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
1025 head-end t))
1026 (setq coding-system 'raw-text))
1027 (when (and (not coding-system)
1028 (re-search-forward
1029 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
1030 head-end t))
1031 (setq coding-system (intern (match-string 2)))
1032 (or (coding-system-p coding-system)
1033 (setq coding-system nil)))))
1034
1035 ;; If no coding: tag in the head, check the tail.
1036 (when (and tail-found (not coding-system))
1037 (goto-char tail-start)
1038 (search-forward "\n\^L" nil t)
1039 (if (re-search-forward
1040 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
1041 ;; The prefix is what comes before "local variables:" in its
1042 ;; line. The suffix is what comes after "local variables:"
1043 ;; in its line.
1044 (let* ((prefix (regexp-quote (match-string 1)))
1045 (suffix (regexp-quote (match-string 2)))
1046 (re-coding
1047 (concat
1048 "^" prefix
1049 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1050 suffix "$"))
1051 (re-unibyte
1052 (concat
1053 "^" prefix
1054 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1055 suffix "$"))
1056 (re-end
1057 (concat "^" prefix "[ \t]*end *:[ \t]*" suffix "$"))
1058 (pos (point)))
1059 (re-search-forward re-end tail-end 'move)
1060 (setq tail-end (point))
1061 (goto-char pos)
1062 (when (and set-auto-coding-for-load
1063 (re-search-forward re-unibyte tail-end t))
1064 (setq coding-system 'raw-text))
1065 (when (and (not coding-system)
1066 (re-search-forward re-coding tail-end t))
1067 (setq coding-system (intern (match-string 1)))
1068 (or (coding-system-p coding-system)
1069 (setq coding-system nil))))))
1070 coding-system))))
1071
1072 (setq set-auto-coding-function 'set-auto-coding)
1073
1074 ;; Set buffer-file-coding-system of the current buffer after some text
1075 ;; is inserted.
1076 (defun after-insert-file-set-buffer-file-coding-system (inserted)
1077 (if last-coding-system-used
1078 (let ((coding-system
1079 (find-new-buffer-file-coding-system last-coding-system-used))
1080 (modified-p (buffer-modified-p)))
1081 (when coding-system
1082 (set-buffer-file-coding-system coding-system)
1083 (if (and enable-multibyte-characters
1084 (or (eq coding-system 'no-conversion)
1085 (eq (coding-system-type coding-system) 5))
1086 ;; If buffer was unmodified and the size is the
1087 ;; same as INSERTED, we must be visiting it.
1088 (not modified-p)
1089 (= (buffer-size) inserted))
1090 ;; For coding systems no-conversion and raw-text...,
1091 ;; edit the buffer as unibyte.
1092 (let ((pos-byte (position-bytes (+ (point) inserted))))
1093 (set-buffer-multibyte nil)
1094 (setq inserted (- pos-byte (position-bytes (point))))))
1095 (set-buffer-modified-p modified-p))))
1096 inserted)
1097
1098 (add-hook 'after-insert-file-functions
1099 'after-insert-file-set-buffer-file-coding-system)
1100
1101 ;; The coding-spec and eol-type of coding-system returned is decided
1102 ;; independently in the following order.
1103 ;; 1. That of buffer-file-coding-system locally bound.
1104 ;; 2. That of CODING.
1105
1106 (defun find-new-buffer-file-coding-system (coding)
1107 "Return a coding system for a buffer when a file of CODING is inserted.
1108 The local variable `buffer-file-coding-system' of the current buffer
1109 is set to the returned value.
1110 Return nil if there's no need to set `buffer-file-coding-system'."
1111 (let (local-coding local-eol
1112 found-coding found-eol
1113 new-coding new-eol)
1114 (if (null coding)
1115 ;; Nothing found about coding.
1116 nil
1117
1118 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1119 ;; and LOCAL-CODING.
1120 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1121 (if (null (numberp local-eol))
1122 ;; But eol-type is not yet set.
1123 (setq local-eol nil))
1124 (if (and buffer-file-coding-system
1125 (not (eq (coding-system-type buffer-file-coding-system) t)))
1126 ;; This is not `undecided'.
1127 (setq local-coding (coding-system-base buffer-file-coding-system)))
1128
1129 (if (and (local-variable-p 'buffer-file-coding-system)
1130 local-eol local-coding)
1131 ;; The current buffer has already set full coding-system, we
1132 ;; had better not change it.
1133 nil
1134
1135 (setq found-eol (coding-system-eol-type coding))
1136 (if (null (numberp found-eol))
1137 ;; But eol-type is not found.
1138 ;; If EOL conversions are inhibited, force unix eol-type.
1139 (setq found-eol (if inhibit-eol-conversion 0)))
1140 (if (eq (coding-system-type coding) t)
1141 (setq found-coding 'undecided)
1142 (setq found-coding (coding-system-base coding)))
1143
1144 (if (and (not found-eol) (eq found-coding 'undecided))
1145 ;; No valid coding information found.
1146 nil
1147
1148 ;; Some coding information (eol or text) found.
1149
1150 ;; The local setting takes precedence over the found one.
1151 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1152 (or local-coding found-coding)
1153 (or found-coding local-coding)))
1154 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1155 (or local-eol found-eol)
1156 (or found-eol local-eol)))
1157
1158 (let ((eol-type (coding-system-eol-type new-coding)))
1159 (if (and (numberp new-eol) (vectorp eol-type))
1160 (aref eol-type new-eol)
1161 new-coding)))))))
1162
1163 (defun modify-coding-system-alist (target-type regexp coding-system)
1164 "Modify one of look up tables for finding a coding system on I/O operation.
1165 There are three of such tables, `file-coding-system-alist',
1166 `process-coding-system-alist', and `network-coding-system-alist'.
1167
1168 TARGET-TYPE specifies which of them to modify.
1169 If it is `file', it affects `file-coding-system-alist' (which see).
1170 If it is `process', it affects `process-coding-system-alist' (which see).
1171 If it is `network', it affects `network-coding-system-alist' (which see).
1172
1173 REGEXP is a regular expression matching a target of I/O operation.
1174 The target is a file name if TARGET-TYPE is `file', a program name if
1175 TARGET-TYPE is `process', or a network service name or a port number
1176 to connect to if TARGET-TYPE is `network'.
1177
1178 CODING-SYSTEM is a coding system to perform code conversion on the I/O
1179 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1180 for decoding and encoding respectively,
1181 or a function symbol which, when called, returns such a cons cell."
1182 (or (memq target-type '(file process network))
1183 (error "Invalid target type: %s" target-type))
1184 (or (stringp regexp)
1185 (and (eq target-type 'network) (integerp regexp))
1186 (error "Invalid regular expression: %s" regexp))
1187 (if (symbolp coding-system)
1188 (if (not (fboundp coding-system))
1189 (progn
1190 (check-coding-system coding-system)
1191 (setq coding-system (cons coding-system coding-system))))
1192 (check-coding-system (car coding-system))
1193 (check-coding-system (cdr coding-system)))
1194 (cond ((eq target-type 'file)
1195 (let ((slot (assoc regexp file-coding-system-alist)))
1196 (if slot
1197 (setcdr slot coding-system)
1198 (setq file-coding-system-alist
1199 (cons (cons regexp coding-system)
1200 file-coding-system-alist)))))
1201 ((eq target-type 'process)
1202 (let ((slot (assoc regexp process-coding-system-alist)))
1203 (if slot
1204 (setcdr slot coding-system)
1205 (setq process-coding-system-alist
1206 (cons (cons regexp coding-system)
1207 process-coding-system-alist)))))
1208 (t
1209 (let ((slot (assoc regexp network-coding-system-alist)))
1210 (if slot
1211 (setcdr slot coding-system)
1212 (setq network-coding-system-alist
1213 (cons (cons regexp coding-system)
1214 network-coding-system-alist)))))))
1215
1216 (defun make-translation-table (&rest args)
1217 "Make a translation table (char table) from arguments.
1218 Each argument is a list of the form (FROM . TO),
1219 where FROM is a character to be translated to TO.
1220
1221 FROM can be a generic character (see `make-char'). In this case, TO is
1222 a generic character containing the same number of characters, or a
1223 ordinary character. If FROM and TO are both generic characters, all
1224 characters belonging to FROM are translated to characters belonging to TO
1225 without changing their position code(s)."
1226 (let ((table (make-char-table 'translation-table))
1227 revlist)
1228 (while args
1229 (let ((elts (car args)))
1230 (while elts
1231 (let* ((from (car (car elts)))
1232 (from-i 0) ; degree of freedom of FROM
1233 (from-rev (nreverse (split-char from)))
1234 (to (cdr (car elts)))
1235 (to-i 0) ; degree of freedom of TO
1236 (to-rev (nreverse (split-char to))))
1237 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1238 (while (eq (car from-rev) 0)
1239 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1240 (while (eq (car to-rev) 0)
1241 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1242 (if (and (/= from-i to-i) (/= to-i 0))
1243 (error "Invalid character pair (%d . %d)" from to))
1244 ;; If we have already translated TO to TO-ALT, FROM should
1245 ;; also be translated to TO-ALT. But, this is only if TO
1246 ;; is a generic character or TO-ALT is not a generic
1247 ;; character.
1248 (let ((to-alt (aref table to)))
1249 (if (and to-alt
1250 (or (> to-i 0) (not (generic-char-p to-alt))))
1251 (setq to to-alt)))
1252 (if (> from-i 0)
1253 (set-char-table-default table from to)
1254 (aset table from to))
1255 ;; If we have already translated some chars to FROM, they
1256 ;; should also be translated to TO.
1257 (let ((l (assq from revlist)))
1258 (if l
1259 (let ((ch (car l)))
1260 (setcar l to)
1261 (setq l (cdr l))
1262 (while l
1263 (aset table ch to)
1264 (setq l (cdr l)) ))))
1265 ;; Now update REVLIST.
1266 (let ((l (assq to revlist)))
1267 (if l
1268 (setcdr l (cons from (cdr l)))
1269 (setq revlist (cons (list to from) revlist)))))
1270 (setq elts (cdr elts))))
1271 (setq args (cdr args)))
1272 ;; Return TABLE just created.
1273 table))
1274
1275 (defun make-translation-table-from-vector (vec)
1276 "Make translation table from decoding vector VEC.
1277 VEC is an array of 256 elements to map unibyte codes to multibyte characters.
1278 See also the variable `nonascii-translation-table'."
1279 (let ((table (make-char-table 'translation-table))
1280 (rev-table (make-char-table 'translation-table))
1281 (i 0)
1282 ch)
1283 (while (< i 256)
1284 (setq ch (aref vec i))
1285 (aset table i ch)
1286 (if (>= ch 256)
1287 (aset rev-table ch i))
1288 (setq i (1+ i)))
1289 (set-char-table-extra-slot table 0 rev-table)
1290 table))
1291
1292 (defun define-translation-table (symbol &rest args)
1293 "Define SYMBOL as a name of translation table made by ARGS.
1294
1295 If the first element of ARGS is a char-table of which purpose is
1296 translation-table, just define SYMBOL as the name of it.
1297
1298 In the other case, ARGS are the same as arguments to the function
1299 `make-translation-table' (which see).
1300
1301 This function sets properties `translation-table' and
1302 `translation-table-id' of SYMBOL to the created table itself and
1303 identification number of the table respectively."
1304 (let ((table (if (and (char-table-p (car args))
1305 (eq (char-table-subtype (car args))
1306 'translation-table))
1307 (car args)
1308 (apply 'make-translation-table args)))
1309 (len (length translation-table-vector))
1310 (id 0)
1311 (done nil))
1312 (put symbol 'translation-table table)
1313 (while (not done)
1314 (if (>= id len)
1315 (setq translation-table-vector
1316 (vconcat translation-table-vector (make-vector len nil))))
1317 (let ((slot (aref translation-table-vector id)))
1318 (if (or (not slot)
1319 (eq (car slot) symbol))
1320 (progn
1321 (aset translation-table-vector id (cons symbol table))
1322 (setq done t))
1323 (setq id (1+ id)))))
1324 (put symbol 'translation-table-id id)
1325 id))
1326
1327 (put 'with-category-table 'lisp-indent-function 1)
1328
1329 (defmacro with-category-table (category-table &rest body)
1330 `(let ((current-category-table (category-table)))
1331 (set-category-table ,category-table)
1332 (unwind-protect
1333 (progn ,@body)
1334 (set-category-table current-category-table))))
1335
1336 ;;; Initialize some variables.
1337
1338 (put 'use-default-ascent 'char-table-extra-slots 0)
1339 (setq use-default-ascent (make-char-table 'use-default-ascent))
1340 (put 'ignore-relative-composition 'char-table-extra-slots 0)
1341 (setq ignore-relative-composition
1342 (make-char-table 'ignore-relative-composition))
1343
1344 ;;;
1345 (provide 'mule)
1346
1347 ;;; mule.el ends here