]> code.delx.au - gnu-emacs/blob - lisp/international/mule.el
Avoid non-ASCII characters to prevent loadup failure.
[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 "4.0 (HANANOEN)" "\
28 Version number and name of this version of MULE (multilingual environment).")
29
30 (defconst mule-version-date "1998.7.1" "\
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 is 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' and `define-coding-system-alias' sets
317 ;; this value automatically.
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' and
324 ;; `define-coding-system-alias' set this value automatically.
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)
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 flags properties)
533 "Define a new coding system CODING-SYSTEM (symbol).
534 Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
535 and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
536 in the following format:
537 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
538
539 TYPE is an integer value indicating the type of the coding system as follows:
540 0: Emacs internal format,
541 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
542 2: ISO-2022 including many variants,
543 3: Big5 used mainly on Chinese PC,
544 4: private, CCL programs provide encoding/decoding algorithm,
545 5: Raw-text, which means that text contains random 8-bit codes.
546
547 MNEMONIC is a character to be displayed on mode line for the coding system.
548
549 DOC-STRING is a documentation string for the coding system.
550
551 FLAGS specifies more detailed information of the coding system as follows:
552
553 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
554 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
555 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
556 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
557 SAFE, ACCEPT-LATIN-EXTRA-CODE.
558 CHARSETn are character sets initially designated to Gn graphic registers.
559 If CHARSETn is nil, Gn is never used.
560 If CHARSETn is t, Gn can be used but nothing designated initially.
561 If CHARSETn is a list of character sets, those character sets are
562 designated to Gn on output, but nothing designated to Gn initially.
563 But, character set `ascii' can be designated only to G0.
564 SHORT-FORM non-nil means use short designation sequence on output.
565 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
566 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
567 SPACE on output.
568 SEVEN non-nil means use 7-bit code only on output.
569 LOCKING-SHIFT non-nil means use locking-shift.
570 SINGLE-SHIFT non-nil means use single-shift.
571 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
572 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
573 NO-ISO6429 non-nil means not use ISO6429's direction specification.
574 INIT-BOL non-nil means any designation state is assumed to be reset
575 to initial at each beginning of line on output.
576 DESIGNATION-BOL non-nil means designation sequences should be placed
577 at beginning of line on output.
578 SAFE non-nil means convert unsafe characters to `?' on output.
579 Unsafe characters are what not specified in SAFE-CHARSET.
580 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
581 a code specified in `latin-extra-code-table' (which see) as a valid
582 code of the coding system.
583
584 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
585 decoding and encoding. CCL programs should be specified by their
586 symbols.
587
588 PROPERTIES is an alist of properties vs the corresponding values.
589 These properties are set in PLIST, a property list. This function
590 also sets properties `coding-category' and `alias-coding-systems'
591 automatically.
592
593 Kludgy features for backward compatibility:
594
595 1. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
596 treated as a compiled CCL code.
597
598 2. If PROPERTIES is just a list of character sets, the list is set as
599 a value of `safe-charsets' in PLIST."
600 (if (memq coding-system coding-system-list)
601 (error "Coding system %s already exists" coding-system))
602
603 ;; Set a value of `coding-system' property.
604 (let ((coding-spec (make-vector 5 nil))
605 (no-initial-designation t)
606 (no-alternative-designation t)
607 coding-category)
608 (if (or (not (integerp type)) (< type 0) (> type 5))
609 (error "TYPE argument must be 0..5"))
610 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
611 (error "MNEMONIC argument must be an ASCII printable character."))
612 (aset coding-spec coding-spec-type-idx type)
613 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
614 (aset coding-spec coding-spec-doc-string-idx
615 (if (stringp doc-string) doc-string ""))
616 (cond ((= type 0)
617 (setq coding-category 'coding-category-emacs-mule))
618 ((= type 1)
619 (setq coding-category 'coding-category-sjis))
620 ((= type 2) ; ISO2022
621 (let ((i 0)
622 (vec (make-vector 32 nil))
623 (g1-designation nil)
624 (fl flags))
625 (while (< i 4)
626 (let ((charset (car fl)))
627 (if (and no-initial-designation
628 (> i 0)
629 (or (charsetp charset)
630 (and (consp charset)
631 (charsetp (car charset)))))
632 (setq no-initial-designation nil))
633 (if (charsetp charset)
634 (if (= i 1) (setq g1-designation charset))
635 (if (consp charset)
636 (let ((tail charset)
637 elt)
638 (while tail
639 (setq elt (car tail))
640 (if (eq elt t)
641 (setq no-alternative-designation nil)
642 (if (and elt (not (charsetp elt)))
643 (error "Invalid charset: %s" elt)))
644 (setq tail (cdr tail)))
645 (setq g1-designation (car charset)))
646 (if charset
647 (if (eq charset t)
648 (setq no-alternative-designation nil)
649 (error "Invalid charset: %s" charset)))))
650 (aset vec i charset))
651 (setq fl (cdr fl) i (1+ i)))
652 (while (and (< i 32) fl)
653 (aset vec i (car fl))
654 (setq fl (cdr fl) i (1+ i)))
655 (aset coding-spec 4 vec)
656 (setq coding-category
657 (if (aref vec 8) ; Use locking-shift.
658 (or (and (aref vec 7) 'coding-category-iso-7-else)
659 'coding-category-iso-8-else)
660 (if (aref vec 7) ; 7-bit only.
661 (if (aref vec 9) ; Use single-shift.
662 'coding-category-iso-7-else
663 (if no-alternative-designation
664 'coding-category-iso-7-tight
665 'coding-category-iso-7))
666 (if (or no-initial-designation
667 (not no-alternative-designation))
668 'coding-category-iso-8-else
669 (if (and (charsetp g1-designation)
670 (= (charset-dimension g1-designation) 2))
671 'coding-category-iso-8-2
672 'coding-category-iso-8-1)))))))
673 ((= type 3)
674 (setq coding-category 'coding-category-big5))
675 ((= type 4) ; private
676 (setq coding-category 'coding-category-ccl)
677 (if (not (consp flags))
678 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
679 (let ((decoder (check-ccl-program
680 (car flags)
681 (intern (format "%s-decoder" coding-system))))
682 (encoder (check-ccl-program
683 (cdr flags)
684 (intern (format "%s-encoder" coding-system)))))
685 (if (and decoder encoder)
686 (aset coding-spec 4 (cons decoder encoder))
687 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
688 (t ; i.e. (= type 5)
689 (setq coding-category 'coding-category-raw-text)))
690
691 (let ((plist (list 'coding-category coding-category
692 'alias-coding-systems (list coding-system))))
693 (if no-initial-designation
694 (plist-put plist 'no-initial-designation t))
695 (if (and properties
696 (or (eq properties t)
697 (not (consp (car properties)))))
698 ;; In the old version, the arg PROPERTIES is a list to be
699 ;; set in PLIST as a value of property `safe-charsets'.
700 (plist-put plist 'safe-charsets properties)
701 (let ((l properties))
702 (while l
703 (plist-put plist (car (car l)) (cdr (car l)))
704 (setq l (cdr l)))))
705 (aset coding-spec coding-spec-plist-idx plist))
706 (put coding-system 'coding-system coding-spec)
707 (put coding-category 'coding-systems
708 (cons coding-system (get coding-category 'coding-systems))))
709
710 ;; Next, set a value of `eol-type' property. The value is a vector
711 ;; of subsidiary coding systems, each corresponds to a coding system
712 ;; for the detected end-of-line format.
713 (put coding-system 'eol-type
714 (if (or (<= type 3) (= type 5))
715 (make-subsidiary-coding-system coding-system)
716 0))
717
718 ;; At last, register CODING-SYSTEM in `coding-system-list' and
719 ;; `coding-system-alist'.
720 (add-to-coding-system-list coding-system)
721 (setq coding-system-alist (cons (list (symbol-name coding-system))
722 coding-system-alist))
723
724 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
725 ;; XXX-with-esc variants.
726 (let ((coding-category (coding-system-category coding-system)))
727 (if (or (eq coding-category 'coding-category-iso-8-1)
728 (eq coding-category 'coding-category-iso-8-2))
729 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
730 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system)))
731 (make-coding-system esc type mnemonic doc
732 (if (listp (car flags))
733 (cons (append (car flags) '(t)) (cdr flags))
734 (cons (list (car flags) t) (cdr flags)))
735 properties)
736 (coding-system-put esc 'mime-charset nil)
737 (coding-system-put esc 'safe-charsets t))))
738
739 coding-system)
740
741 (defun define-coding-system-alias (alias coding-system)
742 "Define ALIAS as an alias for coding system CODING-SYSTEM."
743 (put alias 'coding-system (coding-system-spec coding-system))
744 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
745 (add-to-coding-system-list alias)
746 (setq coding-system-alist (cons (list (symbol-name alias))
747 coding-system-alist))
748 (let ((eol-type (coding-system-eol-type coding-system)))
749 (if (vectorp eol-type)
750 (put alias 'eol-type (make-subsidiary-coding-system alias))
751 (put alias 'eol-type eol-type))))
752
753 (defun set-buffer-file-coding-system (coding-system &optional force)
754 "Set the file coding-system of the current buffer to CODING-SYSTEM.
755 This means that when you save the buffer, it will be converted
756 according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
757 use \\[list-coding-systems].
758
759 If the buffer's previous file coding-system value specifies end-of-line
760 conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
761 merged with the already-specified end-of-line conversion.
762 However, if the optional prefix argument FORCE is non-nil,
763 then CODING-SYSTEM is used exactly as specified."
764 (interactive "zCoding system for visited file (default, nil): \nP")
765 (check-coding-system coding-system)
766 (if (null force)
767 (let ((x (coding-system-eol-type buffer-file-coding-system))
768 (y (coding-system-eol-type coding-system)))
769 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
770 (setq coding-system (aref y x)))))
771 (setq buffer-file-coding-system coding-system)
772 (set-buffer-modified-p t)
773 (force-mode-line-update))
774
775 (defvar default-terminal-coding-system nil
776 "Default value for the terminal coding system.
777 This is normally set according to the selected language environment.
778 See also the command `set-terminal-coding-system'.")
779
780 (defun set-terminal-coding-system (coding-system)
781 "Set coding system of your terminal to CODING-SYSTEM.
782 All text output to the terminal will be encoded
783 with the specified coding system.
784 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
785 The default is determined by the selected language environment
786 or by the previous use of this command."
787 (interactive
788 (list (let ((default (if (and (not (terminal-coding-system))
789 default-terminal-coding-system)
790 default-terminal-coding-system)))
791 (read-coding-system
792 (format "Coding system for terminal display (default, %s): "
793 default)
794 default))))
795 (if (and (not coding-system)
796 (not (terminal-coding-system)))
797 (setq coding-system default-terminal-coding-system))
798 (if coding-system
799 (setq default-terminal-coding-system coding-system))
800 (set-terminal-coding-system-internal coding-system)
801 (redraw-frame (selected-frame)))
802
803 (defvar default-keyboard-coding-system nil
804 "Default value of the keyboard coding system.
805 This is normally set according to the selected language environment.
806 See also the command `set-keyboard-coding-system'.")
807
808 (defun set-keyboard-coding-system (coding-system)
809 "Set coding system for keyboard input to CODING-SYSTEM.
810 In addition, this command enables Encoded-kbd minor mode.
811 \(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
812 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
813 The default is determined by the selected language environment
814 or by the previous use of this command."
815 (interactive
816 (list (let ((default (if (and (not (keyboard-coding-system))
817 default-keyboard-coding-system)
818 default-keyboard-coding-system)))
819 (read-coding-system
820 (format "Coding system for keyboard input (default, %s): "
821 default)
822 default))))
823 (if (and (not coding-system)
824 (not (keyboard-coding-system)))
825 (setq coding-system default-keyboard-coding-system))
826 (if coding-system
827 (setq default-keyboard-coding-system coding-system))
828 (set-keyboard-coding-system-internal coding-system)
829 (encoded-kbd-mode (if coding-system 1 0)))
830
831 (defun set-buffer-process-coding-system (decoding encoding)
832 "Set coding systems for the process associated with the current buffer.
833 DECODING is the coding system to be used to decode input from the process,
834 ENCODING is the coding system to be used to encode output to the process.
835
836 For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
837 (interactive
838 "zCoding-system for process input: \nzCoding-system for process output: ")
839 (let ((proc (get-buffer-process (current-buffer))))
840 (if (null proc)
841 (error "no process")
842 (check-coding-system decoding)
843 (check-coding-system encoding)
844 (set-process-coding-system proc decoding encoding)))
845 (force-mode-line-update))
846
847 (defalias 'set-clipboard-coding-system 'set-selection-coding-system)
848
849 (defun set-selection-coding-system (coding-system)
850 "Make CODING-SYSTEM used for communicating with other X clients .
851 When sending or receiving text via cut_buffer, selection, and clipboard,
852 the text is encoded or decoded by CODING-SYSTEM."
853 (interactive "zCoding system for X selection: ")
854 (check-coding-system coding-system)
855 (setq selection-coding-system coding-system))
856
857 ;; Coding system lastly specified by the command
858 ;; set-next-selection-coding-system.
859 (defvar last-next-selection-coding-system nil)
860
861 (defun set-next-selection-coding-system (coding-system)
862 "Make CODING-SYSTEM used for the next communication with other X clients.
863 This setting is effective for the next communication only."
864 (interactive
865 (list (read-coding-system
866 (if last-next-selection-coding-system
867 (format "Coding system for the next X selection (default, %S): "
868 last-next-selection-coding-system)
869 "Coding system for the next X selection: ")
870 last-next-selection-coding-system)))
871 (if coding-system
872 (setq last-next-selection-coding-system coding-system)
873 (setq coding-system last-next-selection-coding-system))
874 (check-coding-system coding-system)
875
876 (setq next-selection-coding-system coding-system))
877
878 (defun set-coding-priority (arg)
879 "Set priority of coding categories according to LIST.
880 LIST is a list of coding categories ordered by priority."
881 (let ((l arg)
882 (current-list (copy-sequence coding-category-list)))
883 ;; Check the validity of ARG while deleting coding categories in
884 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
885 ;; contains all coding categories.
886 (while l
887 (if (or (null (get (car l) 'coding-category-index))
888 (null (memq (car l) current-list)))
889 (error "Invalid or duplicated element in argument: %s" arg))
890 (setq current-list (delq (car l) current-list))
891 (setq l (cdr l)))
892 ;; Update `coding-category-list' and return it.
893 (setq coding-category-list (append arg current-list))
894 (set-coding-priority-internal)))
895
896 ;;; FILE I/O
897
898 (defvar auto-coding-alist
899 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
900 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\|TGZ\\)\\'" . no-conversion))
901 "Alist of filename patterns vs corresponding coding systems.
902 Each element looks like (REGEXP . CODING-SYSTEM).
903 A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
904
905 The settings in this alist take priority over `coding:' tags
906 in the file (see the function `set-auto-coding')
907 and the contents of `file-coding-system-alist'.")
908
909 (defvar set-auto-coding-for-load nil
910 "Non-nil means look for `load-coding' property instead of `coding'.
911 This is used for loading and byte-compiling Emacs Lisp files.")
912
913 (defun auto-coding-alist-lookup (filename)
914 "Return the coding system specified by `auto-coding-alist' for FILENAME."
915 (let ((alist auto-coding-alist)
916 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
917 coding-system)
918 (while (and alist (not coding-system))
919 (if (string-match (car (car alist)) filename)
920 (setq coding-system (cdr (car alist)))
921 (setq alist (cdr alist))))
922 coding-system))
923
924 (defun set-auto-coding (filename size)
925 "Return coding system for a file FILENAME of which SIZE bytes follow point.
926 These bytes should include at least the first 1k of the file
927 and the last 3k of the file, but the middle may be omitted.
928
929 It checks FILENAME against the variable `auto-coding-alist'.
930 If FILENAME doesn't match any entries in the variable,
931 it checks for a `coding:' tag in the first one or two lines following
932 point. If no `coding:' tag is found, it checks for local variables
933 list in the last 3K bytes out of the SIZE bytes.
934
935 The return value is the specified coding system,
936 or nil if nothing specified.
937
938 The variable `set-auto-coding-function' (which see) is set to this
939 function by default."
940 (let ((coding-system (auto-coding-alist-lookup filename)))
941
942 (or coding-system
943 (let* ((case-fold-search t)
944 (head-start (point))
945 (head-end (+ head-start (min size 1024)))
946 (tail-start (+ head-start (max (- size 3072) 0)))
947 (tail-end (+ head-start size))
948 coding-system head-found tail-found pos)
949 ;; Try a short cut by searching for the string "coding:"
950 ;; and for "unibyte:" at the head and tail of SIZE bytes.
951 (setq head-found (or (search-forward "coding:" head-end t)
952 (search-forward "unibyte:" head-end t)))
953 (if (and head-found (> head-found tail-start))
954 ;; Head and tail are overlapped.
955 (setq tail-found head-found)
956 (goto-char tail-start)
957 (setq tail-found (or (search-forward "coding:" tail-end t)
958 (search-forward "unibyte:" tail-end t))))
959
960 ;; At first check the head.
961 (when head-found
962 (goto-char head-start)
963 (setq pos (re-search-forward "[\n\r]" head-end t))
964 (if (and pos
965 (= (char-after head-start) ?#)
966 (= (char-after (1+ head-start)) ?!))
967 ;; If the file begins with "#!" (exec interpreter magic),
968 ;; look for coding frobs in the first two lines. You cannot
969 ;; necessarily put them in the first line of such a file
970 ;; without screwing up the interpreter invocation.
971 (setq pos (search-forward "\n" head-end t)))
972 (if pos (setq head-end pos))
973 (when (< head-found head-end)
974 (goto-char head-start)
975 (when (and set-auto-coding-for-load
976 (re-search-forward
977 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
978 head-end t))
979 (setq coding-system 'raw-text))
980 (when (and (not coding-system)
981 (re-search-forward
982 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
983 head-end t))
984 (setq coding-system (intern (match-string 2)))
985 (or (coding-system-p coding-system)
986 (setq coding-system nil)))))
987
988 ;; If no coding: tag in the head, check the tail.
989 (when (and tail-found (not coding-system))
990 (goto-char tail-start)
991 (search-forward "\n\^L" nil t)
992 (if (re-search-forward
993 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
994 ;; The prefix is what comes before "local variables:" in its
995 ;; line. The suffix is what comes after "local variables:"
996 ;; in its line.
997 (let* ((prefix (regexp-quote (match-string 1)))
998 (suffix (regexp-quote (match-string 2)))
999 (re-coding
1000 (concat
1001 "^" prefix
1002 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1003 suffix "$"))
1004 (re-unibyte
1005 (concat
1006 "^" prefix
1007 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1008 suffix "$"))
1009 (re-end
1010 (concat "^" prefix "[ \t]*end *:[ \t]*" suffix "$"))
1011 (pos (point)))
1012 (re-search-forward re-end tail-end 'move)
1013 (setq tail-end (point))
1014 (goto-char pos)
1015 (when (and set-auto-coding-for-load
1016 (re-search-forward re-unibyte tail-end t))
1017 (setq coding-system 'raw-text))
1018 (when (and (not coding-system)
1019 (re-search-forward re-coding tail-end t))
1020 (setq coding-system (intern (match-string 1)))
1021 (or (coding-system-p coding-system)
1022 (setq coding-system nil))))))
1023 coding-system))))
1024
1025 (setq set-auto-coding-function 'set-auto-coding)
1026
1027 ;; Set buffer-file-coding-system of the current buffer after some text
1028 ;; is inserted.
1029 (defun after-insert-file-set-buffer-file-coding-system (inserted)
1030 (if last-coding-system-used
1031 (let ((coding-system
1032 (find-new-buffer-file-coding-system last-coding-system-used))
1033 (modified-p (buffer-modified-p)))
1034 (when coding-system
1035 (set-buffer-file-coding-system coding-system)
1036 (if (and enable-multibyte-characters
1037 (or (eq coding-system 'no-conversion)
1038 (eq (coding-system-type coding-system) 5))
1039 ;; If buffer was unmodified, we must be visiting it.
1040 (not modified-p))
1041 ;; For coding systems no-conversion and raw-text...,
1042 ;; edit the buffer as unibyte.
1043 (let ((pos-byte (position-bytes (+ (point) inserted))))
1044 (set-buffer-multibyte nil)
1045 (setq inserted (- pos-byte (position-bytes (point))))))
1046 (set-buffer-modified-p modified-p))))
1047 inserted)
1048
1049 (add-hook 'after-insert-file-functions
1050 'after-insert-file-set-buffer-file-coding-system)
1051
1052 ;; The coding-spec and eol-type of coding-system returned is decided
1053 ;; independently in the following order.
1054 ;; 1. That of buffer-file-coding-system locally bound.
1055 ;; 2. That of CODING.
1056
1057 (defun find-new-buffer-file-coding-system (coding)
1058 "Return a coding system for a buffer when a file of CODING is inserted.
1059 The local variable `buffer-file-coding-system' of the current buffer
1060 is set to the returned value.
1061 Return nil if there's no need to set `buffer-file-coding-system'."
1062 (let (local-coding local-eol
1063 found-coding found-eol
1064 new-coding new-eol)
1065 (if (null coding)
1066 ;; Nothing found about coding.
1067 nil
1068
1069 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1070 ;; and LOCAL-CODING.
1071 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1072 (if (null (numberp local-eol))
1073 ;; But eol-type is not yet set.
1074 (setq local-eol nil))
1075 (if (and buffer-file-coding-system
1076 (not (eq (coding-system-type buffer-file-coding-system) t)))
1077 ;; This is not `undecided'.
1078 (setq local-coding (coding-system-base buffer-file-coding-system)))
1079
1080 (if (and (local-variable-p 'buffer-file-coding-system)
1081 local-eol local-coding)
1082 ;; The current buffer has already set full coding-system, we
1083 ;; had better not change it.
1084 nil
1085
1086 (setq found-eol (coding-system-eol-type coding))
1087 (if (null (numberp found-eol))
1088 ;; But eol-type is not found.
1089 ;; If EOL conversions are inhibited, force unix eol-type.
1090 (setq found-eol (if inhibit-eol-conversion 0)))
1091 (if (eq (coding-system-type coding) t)
1092 (setq found-coding 'undecided)
1093 (setq found-coding (coding-system-base coding)))
1094
1095 (if (and (not found-eol) (eq found-coding 'undecided))
1096 ;; No valid coding information found.
1097 nil
1098
1099 ;; Some coding information (eol or text) found.
1100
1101 ;; The local setting takes precedence over the found one.
1102 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1103 (or local-coding found-coding)
1104 (or found-coding local-coding)))
1105 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1106 (or local-eol found-eol)
1107 (or found-eol local-eol)))
1108
1109 (let ((eol-type (coding-system-eol-type new-coding)))
1110 (if (and (numberp new-eol) (vectorp eol-type))
1111 (aref eol-type new-eol)
1112 new-coding)))))))
1113
1114 (defun modify-coding-system-alist (target-type regexp coding-system)
1115 "Modify one of look up tables for finding a coding system on I/O operation.
1116 There are three of such tables, `file-coding-system-alist',
1117 `process-coding-system-alist', and `network-coding-system-alist'.
1118
1119 TARGET-TYPE specifies which of them to modify.
1120 If it is `file', it affects `file-coding-system-alist' (which see).
1121 If it is `process', it affects `process-coding-system-alist' (which see).
1122 If it is `network', it affects `network-coding-system-alist' (which see).
1123
1124 REGEXP is a regular expression matching a target of I/O operation.
1125 The target is a file name if TARGET-TYPE is `file', a program name if
1126 TARGET-TYPE is `process', or a network service name or a port number
1127 to connect to if TARGET-TYPE is `network'.
1128
1129 CODING-SYSTEM is a coding system to perform code conversion on the I/O
1130 operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1131 for decoding and encoding respectively,
1132 or a function symbol which, when called, returns such a cons cell."
1133 (or (memq target-type '(file process network))
1134 (error "Invalid target type: %s" target-type))
1135 (or (stringp regexp)
1136 (and (eq target-type 'network) (integerp regexp))
1137 (error "Invalid regular expression: %s" regexp))
1138 (if (symbolp coding-system)
1139 (if (not (fboundp coding-system))
1140 (progn
1141 (check-coding-system coding-system)
1142 (setq coding-system (cons coding-system coding-system))))
1143 (check-coding-system (car coding-system))
1144 (check-coding-system (cdr coding-system)))
1145 (cond ((eq target-type 'file)
1146 (let ((slot (assoc regexp file-coding-system-alist)))
1147 (if slot
1148 (setcdr slot coding-system)
1149 (setq file-coding-system-alist
1150 (cons (cons regexp coding-system)
1151 file-coding-system-alist)))))
1152 ((eq target-type 'process)
1153 (let ((slot (assoc regexp process-coding-system-alist)))
1154 (if slot
1155 (setcdr slot coding-system)
1156 (setq process-coding-system-alist
1157 (cons (cons regexp coding-system)
1158 process-coding-system-alist)))))
1159 (t
1160 (let ((slot (assoc regexp network-coding-system-alist)))
1161 (if slot
1162 (setcdr slot coding-system)
1163 (setq network-coding-system-alist
1164 (cons (cons regexp coding-system)
1165 network-coding-system-alist)))))))
1166
1167 (defun make-translation-table (&rest args)
1168 "Make a translation table (char table) from arguments.
1169 Each argument is a list of the form (FROM . TO),
1170 where FROM is a character to be translated to TO.
1171
1172 FROM can be a generic character (see `make-char'). In this case, TO is
1173 a generic character containing the same number of characters, or a
1174 ordinary character. If FROM and TO are both generic characters, all
1175 characters belonging to FROM are translated to characters belonging to TO
1176 without changing their position code(s)."
1177 (let ((table (make-char-table 'translation-table))
1178 revlist)
1179 (while args
1180 (let ((elts (car args)))
1181 (while elts
1182 (let* ((from (car (car elts)))
1183 (from-i 0) ; degree of freedom of FROM
1184 (from-rev (nreverse (split-char from)))
1185 (to (cdr (car elts)))
1186 (to-i 0) ; degree of freedom of TO
1187 (to-rev (nreverse (split-char to))))
1188 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1189 (while (eq (car from-rev) 0)
1190 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1191 (while (eq (car to-rev) 0)
1192 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1193 (if (and (/= from-i to-i) (/= to-i 0))
1194 (error "Invalid character pair (%d . %d)" from to))
1195 ;; If we have already translated TO to TO-ALT, FROM should
1196 ;; also be translated to TO-ALT. But, this is only if TO
1197 ;; is a generic character or TO-ALT is not a generic
1198 ;; character.
1199 (let ((to-alt (aref table to)))
1200 (if (and to-alt
1201 (or (> to-i 0) (not (generic-char-p to-alt))))
1202 (setq to to-alt)))
1203 (if (> from-i 0)
1204 (set-char-table-default table from to)
1205 (aset table from to))
1206 ;; If we have already translated some chars to FROM, they
1207 ;; should also be translated to TO.
1208 (let ((l (assq from revlist)))
1209 (if l
1210 (let ((ch (car l)))
1211 (setcar l to)
1212 (setq l (cdr l))
1213 (while l
1214 (aset table ch to)
1215 (setq l (cdr l)) ))))
1216 ;; Now update REVLIST.
1217 (let ((l (assq to revlist)))
1218 (if l
1219 (setcdr l (cons from (cdr l)))
1220 (setq revlist (cons (list to from) revlist)))))
1221 (setq elts (cdr elts))))
1222 (setq args (cdr args)))
1223 ;; Return TABLE just created.
1224 table))
1225
1226 (defun make-translation-table-from-vector (vec)
1227 "Make translation table from decoding vector VEC.
1228 VEC is an array of 256 elements to map unibyte codes to multibyte characters.
1229 See also the variable `nonascii-translation-table'."
1230 (let ((table (make-char-table 'translation-table))
1231 (rev-table (make-char-table 'translation-table))
1232 (i 0)
1233 ch)
1234 (while (< i 256)
1235 (setq ch (aref vec i))
1236 (aset table i ch)
1237 (if (>= ch 256)
1238 (aset rev-table ch i))
1239 (setq i (1+ i)))
1240 (set-char-table-extra-slot table 0 rev-table)
1241 table))
1242
1243 (defun define-translation-table (symbol &rest args)
1244 "Define SYMBOL as a name of translation table made by ARGS.
1245
1246 If the first element of ARGS is a char-table of which purpose is
1247 translation-table, just define SYMBOL as the name of it.
1248
1249 In the other case, ARGS are the same as arguments to the function
1250 `make-translation-table' (which see).
1251
1252 This function sets properties `translation-table' and
1253 `translation-table-id' of SYMBOL to the created table itself and
1254 identification number of the table respectively."
1255 (let ((table (if (and (char-table-p (car args))
1256 (eq (char-table-subtype (car args))
1257 'translation-table))
1258 (car args)
1259 (apply 'make-translation-table args)))
1260 (len (length translation-table-vector))
1261 (id 0)
1262 (done nil))
1263 (put symbol 'translation-table table)
1264 (while (not done)
1265 (if (>= id len)
1266 (setq translation-table-vector
1267 (vconcat translation-table-vector (make-vector len nil))))
1268 (let ((slot (aref translation-table-vector id)))
1269 (if (or (not slot)
1270 (eq (car slot) symbol))
1271 (progn
1272 (aset translation-table-vector id (cons symbol table))
1273 (setq done t))
1274 (setq id (1+ id)))))
1275 (put symbol 'translation-table-id id)
1276 id))
1277
1278 ;;; Initialize some variables.
1279
1280 (put 'use-default-ascent 'char-table-extra-slots 0)
1281 (setq use-default-ascent (make-char-table 'use-default-ascent))
1282 (put 'ignore-relative-composition 'char-table-extra-slots 0)
1283 (setq ignore-relative-composition
1284 (make-char-table 'ignore-relative-composition))
1285
1286 ;;;
1287 (provide 'mule)
1288
1289 ;;; mule.el ends here