[AIXHFT] (hft_init, hft_reset): Add declarations.
[bpt/emacs.git] / lisp / international / mule.el
CommitLineData
4ed46869
KH
1;;; mule.el --- basic commands for mulitilingual environment
2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
58cfed09 4;; Licensed to the Free Software Foundation.
4ed46869
KH
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
369314dc
KH
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.
4ed46869
KH
24
25;;; Code:
26
eb9386bb 27(defconst mule-version "4.0 (HANANOEN)" "\
4ed46869
KH
28Version number and name of this version of MULE (multilingual environment).")
29
eb9386bb 30(defconst mule-version-date "1998.7.1" "\
4ed46869
KH
31Distribution date of this version of MULE (multilingual environment).")
32
33(defun load-with-code-conversion (fullname file &optional noerror nomessage)
0f69cb38
KH
34 "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
35The file contents are decoded before evaluation if necessary.
4ed46869
KH
36If optional second arg NOERROR is non-nil,
37 report no error if FILE doesn't exist.
38Print messages at start and end of loading unless
39 optional third arg NOMESSAGE is non-nil.
40Return 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.
88162676
RS
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))
4ed46869
KH
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*"))))
db5cae4b
SM
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)))
4ed46869 65 (unwind-protect
a6acd8a2 66 (let ((load-file-name fullname)
1c4cc63a 67 (set-auto-coding-for-load t)
a6acd8a2 68 (inhibit-file-name-operation nil))
4ed46869
KH
69 (save-excursion
70 (set-buffer buffer)
71 (insert-file-contents fullname)
4ed46869
KH
72 ;; Make `kill-buffer' quiet.
73 (set-buffer-modified-p nil))
0f69cb38 74 ;; Have the original buffer current while we eval.
88162676
RS
75 (eval-buffer buffer nil file
76 ;; If this Emacs is running with --unibyte,
77 ;; convert multibyte strings to unibyte
78 ;; after reading them.
ba74e833
RS
79;; (not default-enable-multibyte-characters)
80 ))
cfc70cdf
RS
81 (let (kill-buffer-hook kill-buffer-query-functions)
82 (kill-buffer buffer)))
4ed46869 83 (let ((hook (assoc file after-load-alist)))
db5cae4b
SM
84 (when hook
85 (mapcar (function eval) (cdr hook))))
86 (unless (or nomessage noninteractive)
87 (if source
88 (message "Loading %s (source)...done" file)
89 (message "Loading %s...done" file)))
4ed46869
KH
90 t)))
91
92;; API (Application Program Interface) for charsets.
93
2e02a76f
RS
94;; Return t if OBJ is a quoted symbol
95;; and the symbol is the name of a standard charset.
96(defsubst charset-quoted-standard-p (obj)
97 (and (listp obj) (eq (car obj) 'quote)
98 (symbolp (car-safe (cdr obj)))
99 (let ((vector (get (car-safe (cdr obj)) 'charset)))
100 (and (vectorp vector)
101 (< (aref vector 0) 160)))))
4ed46869
KH
102
103(defsubst charsetp (object)
104 "T is OBJECT is a charset."
105 (and (symbolp object) (vectorp (get object 'charset))))
106
107(defsubst charset-info (charset)
108 "Return a vector of information of CHARSET.
109The elements of the vector are:
110 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
111 LEADING-CODE-BASE, LEADING-CODE-EXT,
112 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
113 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
114 PLIST,
115where
116CHARSET-ID (integer) is the identification number of the charset.
117DIMENSION (integer) is the number of bytes to represent a character of
118the charset: 1 or 2.
119CHARS (integer) is the number of characters in a dimension: 94 or 96.
120BYTE (integer) is the length of multi-byte form of a character in
121 the charset: one of 1, 2, 3, and 4.
122WIDTH (integer) is the number of columns a character in the charset
123 occupies on the screen: one of 0, 1, and 2.
124DIRECTION (integer) is the rendering direction of characters in the
125 charset when rendering. If 0, render from right to left, else
126 render from left to right.
127LEADING-CODE-BASE (integer) is the base leading-code for the
128 charset.
129LEADING-CODE-EXT (integer) is the extended leading-code for the
130 charset. All charsets of less than 0xA0 has the value 0.
131ISO-FINAL-CHAR (character) is the final character of the
132 corresponding ISO 2022 charset.
133ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
134 while encoding to variants of ISO 2022 coding system, one of the
135 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
136REVERSE-CHARSET (integer) is the charset which differs only in
137 LEFT-TO-RIGHT value from the charset. If there's no such a
138 charset, the value is -1.
139SHORT-NAME (string) is the short name to refer to the charset.
140LONG-NAME (string) is the long name to refer to the charset
141DESCRIPTION (string) is the description string of the charset.
142PLIST (property list) may contain any type of information a user
143 want to put and get by functions `put-charset-property' and
144 `get-charset-property' respectively."
145 (get charset 'charset))
146
147(defmacro charset-id (charset)
148 "Return charset identification number of CHARSET."
2e02a76f 149 (if (charset-quoted-standard-p charset)
4ed46869
KH
150 (aref (charset-info (nth 1 charset)) 0)
151 `(aref (charset-info ,charset) 0)))
152
153(defmacro charset-bytes (charset)
900dc6e3
KH
154 "Return bytes of CHARSET.
155See the function `charset-info' for more detail."
2e02a76f 156 (if (charset-quoted-standard-p charset)
4ed46869
KH
157 (aref (charset-info (nth 1 charset)) 1)
158 `(aref (charset-info ,charset) 1)))
159
160(defmacro charset-dimension (charset)
900dc6e3
KH
161 "Return dimension of CHARSET.
162See the function `charset-info' for more detail."
2e02a76f 163 (if (charset-quoted-standard-p charset)
4ed46869
KH
164 (aref (charset-info (nth 1 charset)) 2)
165 `(aref (charset-info ,charset) 2)))
166
167(defmacro charset-chars (charset)
900dc6e3
KH
168 "Return character numbers contained in a dimension of CHARSET.
169See the function `charset-info' for more detail."
2e02a76f 170 (if (charset-quoted-standard-p charset)
4ed46869
KH
171 (aref (charset-info (nth 1 charset)) 3)
172 `(aref (charset-info ,charset) 3)))
173
174(defmacro charset-width (charset)
900dc6e3
KH
175 "Return width (how many column occupied on a screen) of CHARSET.
176See the function `charset-info' for more detail."
2e02a76f 177 (if (charset-quoted-standard-p charset)
4ed46869
KH
178 (aref (charset-info (nth 1 charset)) 4)
179 `(aref (charset-info ,charset) 4)))
180
181(defmacro charset-direction (charset)
900dc6e3
KH
182 "Return direction of CHARSET.
183See the function `charset-info' for more detail."
2e02a76f 184 (if (charset-quoted-standard-p charset)
4ed46869
KH
185 (aref (charset-info (nth 1 charset)) 5)
186 `(aref (charset-info ,charset) 5)))
187
188(defmacro charset-iso-final-char (charset)
900dc6e3
KH
189 "Return final char of CHARSET.
190See the function `charset-info' for more detail."
2e02a76f 191 (if (charset-quoted-standard-p charset)
4ed46869
KH
192 (aref (charset-info (nth 1 charset)) 8)
193 `(aref (charset-info ,charset) 8)))
194
195(defmacro charset-iso-graphic-plane (charset)
900dc6e3
KH
196 "Return graphic plane of CHARSET.
197See the function `charset-info' for more detail."
2e02a76f 198 (if (charset-quoted-standard-p charset)
4ed46869
KH
199 (aref (charset-info (nth 1 charset)) 9)
200 `(aref (charset-info ,charset) 9)))
201
202(defmacro charset-reverse-charset (charset)
900dc6e3
KH
203 "Return reverse charset of CHARSET.
204See the function `charset-info' for more detail."
2e02a76f 205 (if (charset-quoted-standard-p charset)
4ed46869
KH
206 (aref (charset-info (nth 1 charset)) 10)
207 `(aref (charset-info ,charset) 10)))
208
209(defmacro charset-short-name (charset)
900dc6e3
KH
210 "Return short name of CHARSET.
211See the function `charset-info' for more detail."
2e02a76f 212 (if (charset-quoted-standard-p charset)
4ed46869
KH
213 (aref (charset-info (nth 1 charset)) 11)
214 `(aref (charset-info ,charset) 11)))
215
216(defmacro charset-long-name (charset)
900dc6e3
KH
217 "Return long name of CHARSET.
218See the function `charset-info' for more detail."
2e02a76f 219 (if (charset-quoted-standard-p charset)
4ed46869
KH
220 (aref (charset-info (nth 1 charset)) 12)
221 `(aref (charset-info ,charset) 12)))
222
223(defmacro charset-description (charset)
900dc6e3
KH
224 "Return descriptoin of CHARSET.
225See the function `charset-info' for more detail."
2e02a76f 226 (if (charset-quoted-standard-p charset)
4ed46869
KH
227 (aref (charset-info (nth 1 charset)) 13)
228 `(aref (charset-info ,charset) 13)))
229
230(defmacro charset-plist (charset)
900dc6e3
KH
231 "Return list charset property of CHARSET.
232See the function `charset-info' for more detail."
2e02a76f 233 (if (charset-quoted-standard-p charset)
f98e2797 234 `(aref ,(charset-info (nth 1 charset)) 14)
4ed46869
KH
235 `(aref (charset-info ,charset) 14)))
236
237(defun set-charset-plist (charset plist)
900dc6e3 238 "Set CHARSET's property list to PLIST, and retrun PLIST."
4ed46869
KH
239 (aset (charset-info charset) 14 plist))
240
a6acd8a2 241(defun make-char (charset &optional c1 c2)
f98e2797
KH
242 "Return a character of CHARSET and position-codes CODE1 and CODE2.
243CODE1 and CODE2 are optional, but if you don't supply
900dc6e3 244sufficient position-codes, return a generic character which stands for
f98e2797 245all characters or group of characters in the character sets.
a73a8c89 246A generic character can be used to index a char table (e.g. syntax-table)."
a6acd8a2
KH
247 (make-char-internal (charset-id charset) c1 c2))
248
249(put 'make-char 'byte-compile
250 (function
251 (lambda (form)
252 (let ((charset (nth 1 form)))
253 (if (charset-quoted-standard-p charset)
254 (byte-compile-normal-call
255 (cons 'make-char-internal
256 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
257 (byte-compile-normal-call
258 (cons 'make-char-internal
259 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
4ed46869 260
d3675a42 261(defun charset-list ()
900dc6e3
KH
262 "Return list of charsets ever defined.
263
d3675a42 264This function is provided for backward compatibility.
900dc6e3 265Now we have the variable `charset-list'."
d3675a42
KH
266 charset-list)
267
13d5617d
KH
268(defsubst generic-char-p (char)
269 "Return t if and only if CHAR is a generic character.
270See also the documentation of make-char."
1d935669
KH
271 (and (>= char 0400)
272 (let ((l (split-char char)))
273 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
274 (not (eq (car l) 'composition))))))
69eba008 275
0269ddfb 276\f
8057896b 277;; Coding system staffs
4ed46869 278
8057896b 279;; Coding system is a symbol that has the property `coding-system'.
4ed46869 280;;
8057896b
KH
281;; The value of the property `coding-system' is a vector of the
282;; following format:
d3675a42 283;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
8057896b 284;; We call this vector as coding-spec. See comments in src/coding.c
d3675a42 285;; for more detail.
8057896b
KH
286
287(defconst coding-spec-type-idx 0)
288(defconst coding-spec-mnemonic-idx 1)
289(defconst coding-spec-doc-string-idx 2)
0269ddfb 290(defconst coding-spec-plist-idx 3)
8057896b
KH
291(defconst coding-spec-flags-idx 4)
292
a6acd8a2
KH
293;; PLIST is a property list of a coding system. To share PLIST among
294;; alias coding systems, a coding system has PLIST in coding-spec
295;; instead of having it in normal property list of Lisp symbol.
296;; Here's a list of coding system properties currently being used.
0269ddfb
KH
297;;
298;; o coding-category
299;;
300;; The value is a coding category the coding system belongs to. The
301;; function `make-coding-system' and `define-coding-system-alias' sets
302;; this value automatically.
4ed46869 303;;
0269ddfb 304;; o alias-coding-systems
4ed46869 305;;
0269ddfb
KH
306;; The value is a list of coding systems of the same alias group. The
307;; first element is the coding system made at first, which we call as
308;; `base coding system'. The function `make-coding-system' and
309;; `define-coding-system-alias' set this value automatically.
310;;
311;; o post-read-conversion
312;;
313;; The value is a function to call after some text is inserted and
314;; decoded by the coding system itself and before any functions in
315;; `after-insert-functions' are called. The arguments to this
316;; function is the same as those of a function in
4ed46869
KH
317;; `after-insert-functions', i.e. LENGTH of a text while putting point
318;; at the head of the text to be decoded
319;;
0269ddfb
KH
320;; o pre-write-conversion
321;;
322;; The value is a function to call after all functions in
323;; `write-region-annotate-functions' and `buffer-file-format' are
324;; called, and before the text is encoded by the coding system itself.
325;; The arguments to this function is the same as those of a function
326;; in `write-region-annotate-functions', i.e. FROM and TO specifying
327;; region of a text.
328;;
f967223b 329;; o translation-table-for-decode
0269ddfb 330;;
f967223b
KH
331;; The value is a translation table to be applied on decoding. See
332;; the function `make-translation-table' for the format of translation
333;; table.
0269ddfb 334;;
f967223b 335;; o translation-table-for-encode
0269ddfb 336;;
f967223b 337;; The value is a translation table to be applied on encoding.
a6acd8a2
KH
338;;
339;; o safe-charsets
340;;
341;; The value is a list of charsets safely supported by the coding
342;; system. The value t means that all charsets Emacs handles are
343;; supported. Even if some charset is not in this list, it doesn't
344;; mean that the charset can't be encoded in the coding system,
345;; instead, it just means that some other receiver of a text encoded
346;; in the coding system won't be able to handle that charset.
b25eef20
KH
347;;
348;; o mime-charset
349;;
350;; The value is a symbol of which name is `MIME-charset' parameter of
351;; the coding system.
0269ddfb
KH
352
353;; Return coding-spec of CODING-SYSTEM
354(defsubst coding-system-spec (coding-system)
355 (get (check-coding-system coding-system) 'coding-system))
4ed46869 356
4ed46869 357(defun coding-system-type (coding-system)
0269ddfb
KH
358 "Return the coding type of CODING-SYSTEM.
359A coding type is an integer value indicating the encoding method
360of CODING-SYSTEM. See the function `make-coding-system' for more detail."
361 (aref (coding-system-spec coding-system) coding-spec-type-idx))
4ed46869 362
4ed46869 363(defun coding-system-mnemonic (coding-system)
0269ddfb 364 "Return the mnemonic character of CODING-SYSTEM.
6e2c8840
KH
365The mnemonic character of a coding system is used in mode line
366to indicate the coding system. If the arg is nil, return ?-."
367 (let ((spec (coding-system-spec coding-system)))
368 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
4ed46869 369
8057896b 370(defun coding-system-doc-string (coding-system)
0269ddfb
KH
371 "Return the documentation string for CODING-SYSTEM."
372 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
4ed46869 373
d3675a42 374(defun coding-system-plist (coding-system)
0269ddfb
KH
375 "Return the property list of CODING-SYSTEM."
376 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
d3675a42 377
4ed46869 378(defun coding-system-flags (coding-system)
0269ddfb
KH
379 "Return `flags' of CODING-SYSTEM.
380A `flags' of a coding system is a vector of length 32 indicating detailed
381information of a coding system. See the function `make-coding-system'
382for more detail."
383 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
384
385(defun coding-system-get (coding-system prop)
386 "Extract a value from CODING-SYSTEM's property list for property PROP."
387 (plist-get (coding-system-plist coding-system) prop))
388
389(defun coding-system-put (coding-system prop val)
390 "Change value in CODING-SYSTEM's property list PROP to VAL."
391 (let ((plist (coding-system-plist coding-system)))
392 (if plist
393 (plist-put plist prop val)
394 (aset (coding-system-spec coding-system) coding-spec-plist-idx
395 (list prop val)))))
396
397(defun coding-system-category (coding-system)
398 "Return the coding category of CODING-SYSTEM."
399 (coding-system-get coding-system 'coding-category))
400
401(defun coding-system-base (coding-system)
402 "Return the base coding system of CODING-SYSTEM.
d9e3229d
KH
403A base coding system is what made by `make-coding-system'.
404Any alias nor subsidiary coding systems are not base coding system."
0269ddfb
KH
405 (car (coding-system-get coding-system 'alias-coding-systems)))
406
407(defalias 'coding-system-parent 'coding-system-base)
408(make-obsolete 'coding-system-parent 'coding-system-base)
409
410;; Coding system also has a property `eol-type'.
411;;
412;; This property indicates how the coding system handles end-of-line
413;; format. The value is integer 0, 1, 2, or a vector of three coding
414;; systems. Each integer value 0, 1, and 2 indicates the format of
415;; end-of-line LF, CRLF, and CR respectively. A vector value
416;; indicates that the format of end-of-line should be detected
417;; automatically. Nth element of the vector is the subsidiary coding
418;; system whose `eol-type' property is N.
4ed46869 419
8057896b 420(defun coding-system-eol-type (coding-system)
0269ddfb
KH
421 "Return eol-type of CODING-SYSTEM.
422An eol-type is integer 0, 1, 2, or a vector of coding systems.
4ed46869 423
0269ddfb
KH
424Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
425CRLF, and CR respectively.
426
427A vector value indicates that a format of end-of-line should be
428detected automatically. Nth element of the vector is the subsidiary
429coding system whose eol-type is N."
430 (get coding-system 'eol-type))
bd882697 431
6e9722b0
KH
432;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
433(defun make-subsidiary-coding-system (coding-system)
0269ddfb
KH
434 (let ((coding-spec (coding-system-spec coding-system))
435 (subsidiaries (vector (intern (format "%s-unix" coding-system))
8057896b
KH
436 (intern (format "%s-dos" coding-system))
437 (intern (format "%s-mac" coding-system))))
0269ddfb
KH
438 (i 0)
439 temp)
8057896b 440 (while (< i 3)
0269ddfb 441 (put (aref subsidiaries i) 'coding-system coding-spec)
8057896b 442 (put (aref subsidiaries i) 'eol-type i)
0269ddfb
KH
443 (setq coding-system-list
444 (cons (aref subsidiaries i) coding-system-list))
445 (setq coding-system-alist
446 (cons (list (symbol-name (aref subsidiaries i)))
447 coding-system-alist))
8057896b
KH
448 (setq i (1+ i)))
449 subsidiaries))
4ed46869 450
8057896b 451(defun make-coding-system (coding-system type mnemonic doc-string
d9e3229d 452 &optional flags properties)
3bb1accb 453 "Define a new coding system CODING-SYSTEM (symbol).
a6acd8a2 454Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
d9e3229d 455and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
a6acd8a2 456in the following format:
0269ddfb 457 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
3bb1accb
KH
458
459TYPE is an integer value indicating the type of the coding system as follows:
4ed46869
KH
460 0: Emacs internal format,
461 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
462 2: ISO-2022 including many variants,
463 3: Big5 used mainly on Chinese PC,
cd9c3177
KH
464 4: private, CCL programs provide encoding/decoding algorithm,
465 5: Raw-text, which means that text contains random 8-bit codes.
0269ddfb 466
3bb1accb 467MNEMONIC is a character to be displayed on mode line for the coding system.
0269ddfb 468
3bb1accb 469DOC-STRING is a documentation string for the coding system.
0269ddfb 470
3bb1accb 471FLAGS specifies more detailed information of the coding system as follows:
d6d6d592 472
3bb1accb 473 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
4ed46869
KH
474 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
475 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
d6d6d592 476 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
850101ed 477 SAFE, ACCEPT-LATIN-EXTRA-CODE.
4ed46869
KH
478 CHARSETn are character sets initially designated to Gn graphic registers.
479 If CHARSETn is nil, Gn is never used.
480 If CHARSETn is t, Gn can be used but nothing designated initially.
481 If CHARSETn is a list of character sets, those character sets are
482 designated to Gn on output, but nothing designated to Gn initially.
483 SHORT-FORM non-nil means use short designation sequence on output.
484 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
485 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
486 SPACE on output.
487 SEVEN non-nil means use 7-bit code only on output.
488 LOCKING-SHIFT non-nil means use locking-shift.
489 SINGLE-SHIFT non-nil means use single-shift.
490 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
491 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
492 NO-ISO6429 non-nil means not use ISO6429's direction specification.
69eba008
KH
493 INIT-BOL non-nil means any designation state is assumed to be reset
494 to initial at each beginning of line on output.
495 DESIGNATION-BOL non-nil means designation sequences should be placed
496 at beginning of line on output.
a6acd8a2
KH
497 SAFE non-nil means convert unsafe characters to `?' on output.
498 Unsafe characters are what not specified in SAFE-CHARSET.
850101ed
RS
499 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
500 a code specified in `latin-extra-code-table' (which see) as a valid
501 code of the coding system.
d6d6d592 502
3bb1accb
KH
503 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
504 decoding and encoding. CCL programs should be specified by their
505 symbols.
d9e3229d
KH
506
507PROPERTIES is an alist of properties vs the corresponding values.
508These properties are set in PLIST, a property list. This function
509also sets properties `coding-category' and `alias-coding-systems'
510automatically.
4ed46869 511
3bb1accb
KH
512Kludgy features for backward compatibility:
513
5141. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
515treated as a compiled CCL code.
516
5172. If PROPERTIES is just a list of character sets, the list is set as
518a value of `safe-charsets' in PLIST."
0269ddfb 519 (if (memq coding-system coding-system-list)
d9e3229d 520 (error "Coding system %s already exists" coding-system))
0269ddfb
KH
521
522 ;; Set a value of `coding-system' property.
6e9722b0 523 (let ((coding-spec (make-vector 5 nil))
d9e3229d
KH
524 (no-initial-designation t)
525 (no-alternative-designation t)
6e9722b0 526 coding-category)
cd9c3177 527 (if (or (not (integerp type)) (< type 0) (> type 5))
0269ddfb 528 (error "TYPE argument must be 0..5"))
8057896b 529 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
0269ddfb
KH
530 (error "MNEMONIC arguemnt must be an ASCII printable character."))
531 (aset coding-spec coding-spec-type-idx type)
532 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
533 (aset coding-spec coding-spec-doc-string-idx
534 (if (stringp doc-string) doc-string ""))
6e9722b0
KH
535 (cond ((= type 0)
536 (setq coding-category 'coding-category-emacs-mule))
537 ((= type 1)
538 (setq coding-category 'coding-category-sjis))
539 ((= type 2) ; ISO2022
4ed46869 540 (let ((i 0)
6e9722b0 541 (vec (make-vector 32 nil))
6e9722b0 542 (g1-designation nil))
4ed46869
KH
543 (while (< i 4)
544 (let ((charset (car flags)))
6e9722b0
KH
545 (if (and no-initial-designation
546 (> i 0)
547 (or (charsetp charset)
548 (and (consp charset)
549 (charsetp (car charset)))))
550 (setq no-initial-designation nil))
551 (if (charsetp charset)
552 (if (= i 1) (setq g1-designation charset))
553 (if (consp charset)
554 (let ((tail charset)
555 elt)
556 (while tail
557 (setq elt (car tail))
d9e3229d
KH
558 (if (eq elt t)
559 (setq no-alternative-designation nil)
560 (if (and elt (not (charsetp elt)))
561 (error "Invalid charset: %s" elt)))
6e9722b0
KH
562 (setq tail (cdr tail)))
563 (setq g1-designation (car charset)))
d9e3229d
KH
564 (if charset
565 (if (eq charset t)
566 (setq no-alternative-designation nil)
567 (error "Invalid charset: %s" charset)))))
4ed46869
KH
568 (aset vec i charset))
569 (setq flags (cdr flags) i (1+ i)))
570 (while (and (< i 32) flags)
571 (aset vec i (car flags))
572 (setq flags (cdr flags) i (1+ i)))
6e9722b0 573 (aset coding-spec 4 vec)
6e9722b0
KH
574 (setq coding-category
575 (if (aref vec 8) ; Use locking-shift.
dc64cd19
KH
576 (or (and (aref vec 7) 'coding-category-iso-7-else)
577 'coding-category-iso-8-else)
6e9722b0
KH
578 (if (aref vec 7) ; 7-bit only.
579 (if (aref vec 9) ; Use single-shift.
dc64cd19 580 'coding-category-iso-7-else
d9e3229d
KH
581 (if no-alternative-designation
582 'coding-category-iso-7-tight
583 'coding-category-iso-7))
3ad911d8
KH
584 (if (or no-initial-designation
585 (not no-alternative-designation))
dc64cd19 586 'coding-category-iso-8-else
6e9722b0
KH
587 (if (and (charsetp g1-designation)
588 (= (charset-dimension g1-designation) 2))
589 'coding-category-iso-8-2
590 'coding-category-iso-8-1)))))))
591 ((= type 3)
592 (setq coding-category 'coding-category-big5))
593 ((= type 4) ; private
594 (setq coding-category 'coding-category-binary)
3bb1accb
KH
595 (if (not (consp flags))
596 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
597 (let ((decoder (check-ccl-program
598 (car flags)
599 (intern (format "%s-decoder" coding-system))))
600 (encoder (check-ccl-program
601 (cdr flags)
602 (intern (format "%s-encoder" coding-system)))))
603 (if (and decoder encoder)
604 (aset coding-spec 4 (cons decoder encoder))
605 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
cd9c3177
KH
606 (t ; i.e. (= type 5)
607 (setq coding-category 'coding-category-raw-text)))
0269ddfb
KH
608
609 (let ((plist (list 'coding-category coding-category
d9e3229d 610 'alias-coding-systems (list coding-system))))
0269ddfb 611 (if no-initial-designation
d9e3229d
KH
612 (plist-put plist 'no-initial-designation t))
613 (if (and properties
614 (or (eq properties t)
615 (not (consp (car properties)))))
616 ;; In the old version, the arg PROPERTIES is a list to be
617 ;; set in PLIST as a value of property `safe-charsets'.
618 (plist-put plist 'safe-charsets properties)
619 (while properties
620 (plist-put plist (car (car properties)) (cdr (car properties)))
621 (setq properties (cdr properties))))
0269ddfb 622 (aset coding-spec coding-spec-plist-idx plist))
6e9722b0 623 (put coding-system 'coding-system coding-spec)
6e9722b0
KH
624 (put coding-category 'coding-systems
625 (cons coding-system (get coding-category 'coding-systems))))
4ed46869
KH
626
627 ;; Next, set a value of `eol-type' property. The value is a vector
6e9722b0 628 ;; of subsidiary coding systems, each corresponds to a coding system
4ed46869 629 ;; for the detected end-of-line format.
8057896b 630 (put coding-system 'eol-type
cd9c3177 631 (if (or (<= type 3) (= type 5))
6e9722b0 632 (make-subsidiary-coding-system coding-system)
0269ddfb
KH
633 0))
634
635 ;; At last, register CODING-SYSTEM in `coding-system-list' and
636 ;; `coding-system-alist'.
637 (setq coding-system-list (cons coding-system coding-system-list))
638 (setq coding-system-alist (cons (list (symbol-name coding-system))
d9e3229d
KH
639 coding-system-alist))
640 coding-system)
8057896b 641
a42763dc 642(defun define-coding-system-alias (alias coding-system)
358d28fb 643 "Define ALIAS as an alias for coding system CODING-SYSTEM."
0269ddfb
KH
644 (put alias 'coding-system (coding-system-spec coding-system))
645 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
646 (setq coding-system-list (cons alias coding-system-list))
647 (setq coding-system-alist (cons (list (symbol-name alias))
648 coding-system-alist))
649 (let ((eol-type (coding-system-eol-type coding-system)))
650 (if (vectorp eol-type)
651 (put alias 'eol-type (make-subsidiary-coding-system alias))
652 (put alias 'eol-type eol-type))))
4ed46869
KH
653
654(defun set-buffer-file-coding-system (coding-system &optional force)
358d28fb
RS
655 "Set the file coding-system of the current buffer to CODING-SYSTEM.
656This means that when you save the buffer, it will be converted
657according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
658use \\[list-coding-systems].
659
660If the buffer's previous file coding-system value specifies end-of-line
661conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
662merged with the already-specified end-of-line conversion.
663However, if the optional prefix argument FORCE is non-nil,
0269ddfb 664then CODING-SYSTEM is used exactly as specified."
d9e3229d 665 (interactive "zCoding system for visited file (default, nil): \nP")
4ed46869
KH
666 (check-coding-system coding-system)
667 (if (null force)
8057896b
KH
668 (let ((x (coding-system-eol-type buffer-file-coding-system))
669 (y (coding-system-eol-type coding-system)))
4ed46869
KH
670 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
671 (setq coding-system (aref y x)))))
672 (setq buffer-file-coding-system coding-system)
673 (set-buffer-modified-p t)
674 (force-mode-line-update))
675
358d28fb
RS
676(defvar default-terminal-coding-system nil
677 "Default value for the terminal coding system.
678This is normally set according to the selected language environment.
679See also the command `set-terminal-coding-system'.")
680
df100398
KH
681(defun set-terminal-coding-system (coding-system)
682 "Set coding system of your terminal to CODING-SYSTEM.
358d28fb
RS
683All text output to the terminal will be encoded
684with the specified coding system.
685For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
686The default is determined by the selected language environment
687or by the previous use of this command."
688 (interactive
2e02a76f
RS
689 (list (let ((default (if (and (not (terminal-coding-system))
690 default-terminal-coding-system)
691 default-terminal-coding-system)))
692 (read-coding-system
693 (format "Coding system for terminal display (default, %s): "
694 default)
695 default))))
358d28fb
RS
696 (if (and (not coding-system)
697 (not (terminal-coding-system)))
698 (setq coding-system default-terminal-coding-system))
699 (if coding-system
700 (setq default-terminal-coding-system coding-system))
df100398
KH
701 (set-terminal-coding-system-internal coding-system)
702 (redraw-frame (selected-frame)))
703
358d28fb
RS
704(defvar default-keyboard-coding-system nil
705 "Default value of the keyboard coding system.
706This is normally set according to the selected language environment.
707See also the command `set-keyboard-coding-system'.")
708
df100398 709(defun set-keyboard-coding-system (coding-system)
358d28fb
RS
710 "Set coding system for keyboard input to CODING-SYSTEM.
711In addition, this command enables Encoded-kbd minor mode.
26471f06 712\(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
358d28fb
RS
713For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
714The default is determined by the selected language environment
715or by the previous use of this command."
716 (interactive
2e02a76f
RS
717 (list (let ((default (if (and (not (keyboard-coding-system))
718 default-keyboard-coding-system)
719 default-keyboard-coding-system)))
720 (read-coding-system
721 (format "Coding system for keyboard input (default, %s): "
722 default)
723 default))))
358d28fb
RS
724 (if (and (not coding-system)
725 (not (keyboard-coding-system)))
726 (setq coding-system default-keyboard-coding-system))
727 (if coding-system
728 (setq default-keyboard-coding-system coding-system))
df100398
KH
729 (set-keyboard-coding-system-internal coding-system)
730 (encoded-kbd-mode (if coding-system 1 0)))
731
732(defun set-buffer-process-coding-system (decoding encoding)
358d28fb 733 "Set coding systems for the process associated with the current buffer.
df100398 734DECODING is the coding system to be used to decode input from the process,
358d28fb
RS
735ENCODING is the coding system to be used to encode output to the process.
736
737For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
4ed46869
KH
738 (interactive
739 "zCoding-system for process input: \nzCoding-system for process output: ")
740 (let ((proc (get-buffer-process (current-buffer))))
741 (if (null proc)
742 (error "no process")
df100398
KH
743 (check-coding-system decoding)
744 (check-coding-system encoding)
745 (set-process-coding-system proc decoding encoding)))
4ed46869
KH
746 (force-mode-line-update))
747
14915c37 748(defun set-selection-coding-system (coding-system)
b25eef20
KH
749 "Make CODING-SYSTEM used for communicating with other X clients .
750When sending or receiving text via cut_buffer, selection, and clipboard,
751the text is encoded or decoded by CODING-SYSTEM."
752 (check-coding-system coding-system)
14915c37 753 (setq selection-coding-system coding-system))
b25eef20 754
4ed46869 755(defun set-coding-priority (arg)
d9e3229d
KH
756 "Set priority of coding categories according to LIST.
757LIST is a list of coding categories ordered by priority."
758 (let ((l arg)
759 (current-list (copy-sequence coding-category-list)))
760 ;; Check the varidity of ARG while deleting coding categories in
761 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
762 ;; contains all coding categories.
763 (while l
764 (if (or (null (get (car l) 'coding-category-index))
765 (null (memq (car l) current-list)))
766 (error "Invalid or duplicated element in argument: %s" arg))
767 (setq current-list (delq (car l) current-list))
768 (setq l (cdr l)))
4ed46869 769 ;; Update `coding-category-list' and return it.
2feaf204
KH
770 (setq coding-category-list (append arg current-list))
771 (set-coding-priority-internal)))
4ed46869
KH
772
773;;; FILE I/O
774
835f49b8
KH
775(defvar auto-coding-alist
776 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\)\\'" . no-conversion)
777 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\)\\'" . no-conversion))
778 "Alist of filename patterns vs corresponding coding systems.
779Each element looks like (REGEXP . CODING-SYSTEM).
558b0c86 780A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
835f49b8
KH
781
782The settings in this variable have higher priority than `coding:' tag
783in the file contents (see the function `set-auto-coding')
784and the variable `file-coding-system-alist'.")
785
1c4cc63a
KH
786(defvar set-auto-coding-for-load nil
787 "Non-nil means look for `load-coding' property instead of `coding'.
788This is used for loading and byte-compiling Emacs Lisp files.")
789
835f49b8
KH
790(defun set-auto-coding (filename size)
791 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1c4cc63a
KH
792These bytes should include at least the first 1k of the file
793and the last 3k of the file, but the middle may be omitted.
63561304 794
835f49b8
KH
795It checks FILENAME against the variable `auto-coding-alist'.
796If FILENAME doesn't match any entries in the variable,
797it checks for a `coding:' tag in the first one or two lines following
1c4cc63a
KH
798point. If no `coding:' tag is found, it checks for alocal variables
799list in the last 3K bytes out of the SIZE bytes.
63561304
KH
800
801The return value is the specified coding system,
802or nil if nothing specified.
87aba788 803
ba74e833 804The variable `set-auto-coding-function' (which see) is set to this
87aba788 805function by default."
835f49b8
KH
806 (let ((alist auto-coding-alist)
807 (case-fold-search (memq system-type '(vax-vms windows-nt)))
808 coding-system)
809 (while (and alist (not coding-system))
810 (if (string-match (car (car alist)) filename)
811 (setq coding-system (cdr (car alist)))
812 (setq alist (cdr alist))))
813
814 (or coding-system
815 (let* ((case-fold-search t)
816 (head-start (point))
817 (head-end (+ head-start (min size 1024)))
818 (tail-start (+ head-start (max (- size 3072) 0)))
819 (tail-end (+ head-start size))
820 coding-system head-found tail-found pos)
821 ;; Try a short cut by searching for the string "coding:"
822 ;; and for "unibyte:" at th ehead and tail of SIZE bytes.
823 (setq head-found (or (search-forward "coding:" head-end t)
824 (search-forward "unibyte:" head-end t)))
825 (if (and head-found (> head-found tail-start))
826 ;; Head and tail are overlapped.
827 (setq tail-found head-found)
828 (goto-char tail-start)
829 (setq tail-found (or (search-forward "coding:" tail-end t)
830 (search-forward "unibyte:" tail-end t))))
831
832 ;; At first check the head.
833 (when head-found
834 (goto-char head-start)
835 (setq pos (re-search-forward "[\n\r]" head-end t))
836 (if (and pos
837 (= (char-after head-start) ?#)
838 (= (char-after (1+ head-start)) ?!))
839 ;; If the file begins with "#!" (exec interpreter magic),
840 ;; look for coding frobs in the first two lines. You cannot
841 ;; necessarily put them in the first line of such a file
842 ;; without screwing up the interpreter invocation.
843 (setq pos (search-forward "\n" head-end t)))
844 (if pos (setq head-end pos))
845 (when (< head-found head-end)
846 (goto-char head-start)
847 (when (and set-auto-coding-for-load
848 (re-search-forward
849 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
850 head-end t))
851 (setq coding-system 'raw-text))
852 (when (and (not coding-system)
853 (re-search-forward
854 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
855 head-end t))
856 (setq coding-system (intern (match-string 2)))
857 (or (coding-system-p coding-system)
858 (setq coding-system nil)))))
859
860 ;; If no coding: tag in the head, check the tail.
861 (when (and tail-found (not coding-system))
862 (goto-char tail-start)
863 (search-forward "\n\^L" nil t)
864 (if (re-search-forward
865 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
866 ;; The prefix is what comes before "local variables:" in its
867 ;; line. The suffix is what comes after "local variables:"
868 ;; in its line.
869 (let* ((prefix (regexp-quote (match-string 1)))
870 (suffix (regexp-quote (match-string 2)))
871 (re-coding (concat
872 "^" prefix
873 "coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
874 suffix "$"))
875 (re-unibyte (concat
876 "^" prefix
877 "unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
878 suffix "$"))
879 (re-end (concat
880 "^" prefix "end *:[ \t]*" suffix "$"))
881 (pos (point)))
882 (re-search-forward re-end tail-end 'move)
883 (setq tail-end (point))
884 (goto-char pos)
885 (when (and set-auto-coding-for-load
886 (re-search-forward re-unibyte tail-end t))
887 (setq coding-system 'raw-text))
888 (when (and (not coding-system)
889 (re-search-forward re-coding tail-end t))
890 (setq coding-system (intern (match-string 1)))
891 (or (coding-system-p coding-system)
892 (setq coding-system nil))))))
893 coding-system))))
63561304
KH
894
895(setq set-auto-coding-function 'set-auto-coding)
87aba788 896
4ed46869
KH
897;; Set buffer-file-coding-system of the current buffer after some text
898;; is inserted.
899(defun after-insert-file-set-buffer-file-coding-system (inserted)
900 (if last-coding-system-used
901 (let ((coding-system
902 (find-new-buffer-file-coding-system last-coding-system-used))
903 (modified-p (buffer-modified-p)))
0269ddfb
KH
904 (when coding-system
905 (set-buffer-file-coding-system coding-system)
ccb77b4e
RS
906 (if (and (or (eq coding-system 'no-conversion)
907 (eq (coding-system-type coding-system) 5))
908 ;; If buffer was unmodified, we must be visiting it.
909 (not modified-p))
910 ;; For coding systems no-conversion and raw-text...,
911 ;; edit the buffer as unibyte.
a2b4abc1 912 (set-buffer-multibyte nil))
0269ddfb 913 (set-buffer-modified-p modified-p))))
4ed46869
KH
914 nil)
915
84c9d215
KH
916(add-hook 'after-insert-file-functions
917 'after-insert-file-set-buffer-file-coding-system)
4ed46869 918
8057896b 919;; The coding-spec and eol-type of coding-system returned is decided
4ed46869
KH
920;; independently in the following order.
921;; 1. That of buffer-file-coding-system locally bound.
922;; 2. That of CODING.
923
924(defun find-new-buffer-file-coding-system (coding)
925 "Return a coding system for a buffer when a file of CODING is inserted.
a73a8c89
KH
926The local variable `buffer-file-coding-system' of the current buffer
927is set to the returned value.
df100398 928Return nil if there's no need of setting new buffer-file-coding-system."
4ed46869 929 (let (local-coding local-eol
b685f8d6 930 found-coding found-eol
4ed46869
KH
931 new-coding new-eol)
932 (if (null coding)
933 ;; Nothing found about coding.
934 nil
935
b685f8d6
RS
936 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
937 ;; and LOCAL-CODING.
938 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
939 (if (null (numberp local-eol))
940 ;; But eol-type is not yet set.
941 (setq local-eol nil))
0269ddfb
KH
942 (if (and buffer-file-coding-system
943 (not (eq (coding-system-type buffer-file-coding-system) t)))
944 ;; This is not `undecided'.
945 (setq local-coding (coding-system-base buffer-file-coding-system)))
b685f8d6
RS
946
947 (if (and (local-variable-p 'buffer-file-coding-system)
948 local-eol local-coding)
4ed46869
KH
949 ;; The current buffer has already set full coding-system, we
950 ;; had better not change it.
951 nil
952
8057896b 953 (setq found-eol (coding-system-eol-type coding))
4ed46869
KH
954 (if (null (numberp found-eol))
955 ;; But eol-type is not found.
956 (setq found-eol nil))
0269ddfb
KH
957 (if (not (eq (coding-system-type coding) t))
958 ;; This is not `undecided'.
959 (setq found-coding (coding-system-base coding)))
4ed46869
KH
960
961 ;; The local setting takes precedence over the found one.
b685f8d6
RS
962 (setq new-coding (or (and (local-variable-p 'buffer-file-coding-system)
963 local-coding)
964 found-coding
965 local-coding))
966 (setq new-eol (or (and (local-variable-p 'buffer-file-coding-system)
967 local-eol)
968 found-eol
969 local-eol))
970 (when (numberp new-eol)
971 (or new-coding
972 (setq new-coding 'undecided))
973 (if (vectorp (coding-system-eol-type new-coding))
974 (setq new-coding
975 (aref (coding-system-eol-type new-coding) new-eol))))
976 ;; Return a new coding system only when it is different from
977 ;; the current one.
978 (if (not (eq buffer-file-coding-system new-coding))
979 new-coding)))))
4ed46869 980
fe831d33
GV
981(defun modify-coding-system-alist (target-type regexp coding-system)
982 "Modify one of look up tables for finding a coding system on I/O operation.
8c453b46
RS
983There are three of such tables, `file-coding-system-alist',
984`process-coding-system-alist', and `network-coding-system-alist'.
fe831d33
GV
985
986TARGET-TYPE specifies which of them to modify.
8c453b46
RS
987If it is `file', it affects `file-coding-system-alist' (which see).
988If it is `process', it affects `process-coding-system-alist' (which see).
989If it is `network', it affects `network-codign-system-alist' (which see).
fe831d33
GV
990
991REGEXP is a regular expression matching a target of I/O operation.
992The target is a file name if TARGET-TYPE is `file', a program name if
993TARGET-TYPE is `process', or a network service name or a port number
994to connect to if TARGET-TYPE is `network'.
995
996CODING-SYSTEM is a coding system to perform code conversion on the I/O
8c453b46
RS
997operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
998for decoding and encoding respectively,
999or a function symbol which, when called, returns such a cons cell."
fe831d33
GV
1000 (or (memq target-type '(file process network))
1001 (error "Invalid target type: %s" target-type))
1002 (or (stringp regexp)
1003 (and (eq target-type 'network) (integerp regexp))
1004 (error "Invalid regular expression: %s" regexp))
1005 (if (symbolp coding-system)
1006 (if (not (fboundp coding-system))
1007 (progn
1008 (check-coding-system coding-system)
1009 (setq coding-system (cons coding-system coding-system))))
1010 (check-coding-system (car coding-system))
1011 (check-coding-system (cdr coding-system)))
1012 (cond ((eq target-type 'file)
1013 (let ((slot (assoc regexp file-coding-system-alist)))
1014 (if slot
1015 (setcdr slot coding-system)
1016 (setq file-coding-system-alist
1017 (cons (cons regexp coding-system)
1018 file-coding-system-alist)))))
1019 ((eq target-type 'process)
1020 (let ((slot (assoc regexp process-coding-system-alist)))
1021 (if slot
1022 (setcdr slot coding-system)
1023 (setq process-coding-system-alist
1024 (cons (cons regexp coding-system)
1025 process-coding-system-alist)))))
1026 (t
1027 (let ((slot (assoc regexp network-coding-system-alist)))
1028 (if slot
1029 (setcdr slot coding-system)
1030 (setq network-coding-system-alist
1031 (cons (cons regexp coding-system)
1032 network-coding-system-alist)))))))
1033
b25eef20 1034(defun make-translation-table (&rest args)
f967223b 1035 "Make a translation table (char table) from arguments.
13d5617d 1036Each argument is a list of the form (FROM . TO),
b25eef20 1037where FROM is a character to be translated to TO.
13d5617d 1038
452fdb31
RS
1039FROM can be a generic character (see `make-char'). In this case, TO is
1040a generic character containing the same number of characters, or a
1041ordinary character. If FROM and TO are both generic characters, all
b25eef20 1042characters belonging to FROM are translated to characters belonging to TO
13d5617d 1043without changing their position code(s)."
f967223b 1044 (let ((table (make-char-table 'translation-table))
a73a8c89
KH
1045 revlist)
1046 (while args
1047 (let ((elts (car args)))
1048 (while elts
13d5617d
KH
1049 (let* ((from (car (car elts)))
1050 (from-i 0) ; degree of freedom of FROM
1051 (from-rev (nreverse (split-char from)))
1052 (to (cdr (car elts)))
1053 (to-i 0) ; degree of freedom of TO
1054 (to-rev (nreverse (split-char to))))
1055 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1056 (while (eq (car from-rev) 0)
1057 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1058 (while (eq (car to-rev) 0)
1059 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1060 (if (and (/= from-i to-i) (/= to-i 0))
1061 (error "Invalid character pair (%d . %d)" from to))
b25eef20
KH
1062 ;; If we have already translated TO to TO-ALT, FROM should
1063 ;; also be translated to TO-ALT. But, this is only if TO
1064 ;; is a generic character or TO-ALT is not a generic
13d5617d
KH
1065 ;; character.
1066 (let ((to-alt (aref table to)))
1067 (if (and to-alt
1068 (or (> to-i 0) (not (generic-char-p to-alt))))
1069 (setq to to-alt)))
1070 (if (> from-i 0)
1071 (set-char-table-default table from to)
1072 (aset table from to))
b25eef20
KH
1073 ;; If we have already translated some chars to FROM, they
1074 ;; should also be translated to TO.
a73a8c89
KH
1075 (let ((l (assq from revlist)))
1076 (if l
1077 (let ((ch (car l)))
1078 (setcar l to)
1079 (setq l (cdr l))
1080 (while l
1081 (aset table ch to)
1082 (setq l (cdr l)) ))))
1083 ;; Now update REVLIST.
1084 (let ((l (assq to revlist)))
1085 (if l
1086 (setcdr l (cons from (cdr l)))
1087 (setq revlist (cons (list to from) revlist)))))
1088 (setq elts (cdr elts))))
1089 (setq args (cdr args)))
1090 ;; Return TABLE just created.
1091 table))
1092
f967223b
KH
1093(defun define-translation-table (symbol &rest args)
1094 "Define SYMBOL as a name of translation table makde by ARGS.
b25eef20
KH
1095
1096See the documentation of the function `make-translation-table' for the
1097meaning of ARGS.
1098
452fdb31
RS
1099This function sets properties `translation-table' and
1100`translation-table-id' of SYMBOL to the created table itself and
f967223b 1101identification number of the table respectively."
b25eef20 1102 (let ((table (apply 'make-translation-table args))
f967223b 1103 (len (length translation-table-vector))
d9e3229d 1104 (id 0)
b25eef20 1105 (done nil))
f967223b 1106 (put symbol 'translation-table table)
b25eef20
KH
1107 (while (not done)
1108 (if (>= id len)
f967223b
KH
1109 (setq translation-table-vector
1110 (vconcat translation-table-vector (make-vector len nil))))
1111 (let ((slot (aref translation-table-vector id)))
b25eef20
KH
1112 (if (or (not slot)
1113 (eq (car slot) symbol))
1114 (progn
f967223b 1115 (aset translation-table-vector id (cons symbol table))
b25eef20 1116 (setq done t))))
d9e3229d 1117 (setq id (1+ id)))
f967223b 1118 (put symbol 'translation-table-id id)
d9e3229d
KH
1119 id))
1120
69eba008
KH
1121;;; Initialize some variables.
1122
1123(put 'use-default-ascent 'char-table-extra-slots 0)
1124(setq use-default-ascent (make-char-table 'use-default-ascent))
d6d6d592
KH
1125(put 'ignore-relative-composition 'char-table-extra-slots 0)
1126(setq ignore-relative-composition
1127 (make-char-table 'ignore-relative-composition))
69eba008
KH
1128
1129;;;
4ed46869
KH
1130(provide 'mule)
1131
1132;;; mule.el ends here