(quail-terminate-translation): Run
[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
27(defconst mule-version "3.0 (MOMIJINOGA)" "\
28Version number and name of this version of MULE (multilingual environment).")
29
30(defconst mule-version-date "1998.1.1" "\
31Distribution 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 path is FULLNAME.
35The FILE is decoded before evaluation if necessary.
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.
48 (let ((default-major-mode 'fundamental-mode))
49 ;; We can't use `generate-new-buffer' because files.el
50 ;; is not yet loaded.
51 (get-buffer-create (generate-new-buffer-name " *load*"))))
850101ed 52 (enable-multibyte-characters t)
4ed46869
KH
53 (load-in-progress t))
54 (or nomessage (message "Loading %s..." file))
55 (unwind-protect
56 (progn
57 (save-excursion
58 (set-buffer buffer)
59 (insert-file-contents fullname)
60 ;; We must set `buffer-file-name' for `eval-buffer' and
61 ;; `load-history'.
62 (setq buffer-file-name file)
63 ;; Make `kill-buffer' quiet.
64 (set-buffer-modified-p nil))
65 ;; Eval in the original buffer.
66 (eval-buffer buffer))
cfc70cdf
RS
67 (let (kill-buffer-hook kill-buffer-query-functions)
68 (kill-buffer buffer)))
4ed46869
KH
69 (let ((hook (assoc file after-load-alist)))
70 (if hook
71 (mapcar (function eval) (cdr hook))))
72 (or nomessage noninteractive
73 (message "Loading %s...done" file))
74 t)))
75
76;; API (Application Program Interface) for charsets.
77
78;; Return t if OBJ is a quoted symbol.
79(defsubst quoted-symbol-p (obj)
80 (and (listp obj) (eq (car obj) 'quote)))
81
82(defsubst charsetp (object)
83 "T is OBJECT is a charset."
84 (and (symbolp object) (vectorp (get object 'charset))))
85
86(defsubst charset-info (charset)
87 "Return a vector of information of CHARSET.
88The elements of the vector are:
89 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
90 LEADING-CODE-BASE, LEADING-CODE-EXT,
91 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
92 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
93 PLIST,
94where
95CHARSET-ID (integer) is the identification number of the charset.
96DIMENSION (integer) is the number of bytes to represent a character of
97the charset: 1 or 2.
98CHARS (integer) is the number of characters in a dimension: 94 or 96.
99BYTE (integer) is the length of multi-byte form of a character in
100 the charset: one of 1, 2, 3, and 4.
101WIDTH (integer) is the number of columns a character in the charset
102 occupies on the screen: one of 0, 1, and 2.
103DIRECTION (integer) is the rendering direction of characters in the
104 charset when rendering. If 0, render from right to left, else
105 render from left to right.
106LEADING-CODE-BASE (integer) is the base leading-code for the
107 charset.
108LEADING-CODE-EXT (integer) is the extended leading-code for the
109 charset. All charsets of less than 0xA0 has the value 0.
110ISO-FINAL-CHAR (character) is the final character of the
111 corresponding ISO 2022 charset.
112ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
113 while encoding to variants of ISO 2022 coding system, one of the
114 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
115REVERSE-CHARSET (integer) is the charset which differs only in
116 LEFT-TO-RIGHT value from the charset. If there's no such a
117 charset, the value is -1.
118SHORT-NAME (string) is the short name to refer to the charset.
119LONG-NAME (string) is the long name to refer to the charset
120DESCRIPTION (string) is the description string of the charset.
121PLIST (property list) may contain any type of information a user
122 want to put and get by functions `put-charset-property' and
123 `get-charset-property' respectively."
124 (get charset 'charset))
125
126(defmacro charset-id (charset)
127 "Return charset identification number of CHARSET."
128 (if (and (listp charset) (eq (car charset) 'quote))
129 (aref (charset-info (nth 1 charset)) 0)
130 `(aref (charset-info ,charset) 0)))
131
132(defmacro charset-bytes (charset)
900dc6e3
KH
133 "Return bytes of CHARSET.
134See the function `charset-info' for more detail."
4ed46869
KH
135 (if (quoted-symbol-p charset)
136 (aref (charset-info (nth 1 charset)) 1)
137 `(aref (charset-info ,charset) 1)))
138
139(defmacro charset-dimension (charset)
900dc6e3
KH
140 "Return dimension of CHARSET.
141See the function `charset-info' for more detail."
4ed46869
KH
142 (if (quoted-symbol-p charset)
143 (aref (charset-info (nth 1 charset)) 2)
144 `(aref (charset-info ,charset) 2)))
145
146(defmacro charset-chars (charset)
900dc6e3
KH
147 "Return character numbers contained in a dimension of CHARSET.
148See the function `charset-info' for more detail."
4ed46869
KH
149 (if (quoted-symbol-p charset)
150 (aref (charset-info (nth 1 charset)) 3)
151 `(aref (charset-info ,charset) 3)))
152
153(defmacro charset-width (charset)
900dc6e3
KH
154 "Return width (how many column occupied on a screen) of CHARSET.
155See the function `charset-info' for more detail."
4ed46869
KH
156 (if (quoted-symbol-p charset)
157 (aref (charset-info (nth 1 charset)) 4)
158 `(aref (charset-info ,charset) 4)))
159
160(defmacro charset-direction (charset)
900dc6e3
KH
161 "Return direction of CHARSET.
162See the function `charset-info' for more detail."
4ed46869
KH
163 (if (quoted-symbol-p charset)
164 (aref (charset-info (nth 1 charset)) 5)
165 `(aref (charset-info ,charset) 5)))
166
167(defmacro charset-iso-final-char (charset)
900dc6e3
KH
168 "Return final char of CHARSET.
169See the function `charset-info' for more detail."
4ed46869
KH
170 (if (quoted-symbol-p charset)
171 (aref (charset-info (nth 1 charset)) 8)
172 `(aref (charset-info ,charset) 8)))
173
174(defmacro charset-iso-graphic-plane (charset)
900dc6e3
KH
175 "Return graphic plane of CHARSET.
176See the function `charset-info' for more detail."
4ed46869
KH
177 (if (quoted-symbol-p charset)
178 (aref (charset-info (nth 1 charset)) 9)
179 `(aref (charset-info ,charset) 9)))
180
181(defmacro charset-reverse-charset (charset)
900dc6e3
KH
182 "Return reverse charset of CHARSET.
183See the function `charset-info' for more detail."
4ed46869
KH
184 (if (quoted-symbol-p charset)
185 (aref (charset-info (nth 1 charset)) 10)
186 `(aref (charset-info ,charset) 10)))
187
188(defmacro charset-short-name (charset)
900dc6e3
KH
189 "Return short name of CHARSET.
190See the function `charset-info' for more detail."
4ed46869
KH
191 (if (quoted-symbol-p charset)
192 (aref (charset-info (nth 1 charset)) 11)
193 `(aref (charset-info ,charset) 11)))
194
195(defmacro charset-long-name (charset)
900dc6e3
KH
196 "Return long name of CHARSET.
197See the function `charset-info' for more detail."
4ed46869
KH
198 (if (quoted-symbol-p charset)
199 (aref (charset-info (nth 1 charset)) 12)
200 `(aref (charset-info ,charset) 12)))
201
202(defmacro charset-description (charset)
900dc6e3
KH
203 "Return descriptoin of CHARSET.
204See the function `charset-info' for more detail."
4ed46869
KH
205 (if (quoted-symbol-p charset)
206 (aref (charset-info (nth 1 charset)) 13)
207 `(aref (charset-info ,charset) 13)))
208
209(defmacro charset-plist (charset)
900dc6e3
KH
210 "Return list charset property of CHARSET.
211See the function `charset-info' for more detail."
4ed46869 212 (if (quoted-symbol-p charset)
f98e2797 213 `(aref ,(charset-info (nth 1 charset)) 14)
4ed46869
KH
214 `(aref (charset-info ,charset) 14)))
215
216(defun set-charset-plist (charset plist)
900dc6e3 217 "Set CHARSET's property list to PLIST, and retrun PLIST."
4ed46869
KH
218 (aset (charset-info charset) 14 plist))
219
220(defmacro make-char (charset &optional c1 c2)
f98e2797
KH
221 "Return a character of CHARSET and position-codes CODE1 and CODE2.
222CODE1 and CODE2 are optional, but if you don't supply
900dc6e3 223sufficient position-codes, return a generic character which stands for
f98e2797 224all characters or group of characters in the character sets.
a73a8c89 225A generic character can be used to index a char table (e.g. syntax-table)."
4ed46869
KH
226 (if (quoted-symbol-p charset)
227 `(make-char-internal ,(charset-id (nth 1 charset)) ,c1 ,c2)
228 `(make-char-internal (charset-id ,charset) ,c1 ,c2)))
229
69eba008 230(defmacro charset-list ()
900dc6e3
KH
231 "Return list of charsets ever defined.
232
233This macro is provided for backward compatibility.
234Now we have the variable `charset-list'."
13d5617d
KH
235 'charset-list)
236
237(defsubst generic-char-p (char)
238 "Return t if and only if CHAR is a generic character.
239See also the documentation of make-char."
240 (let ((l (split-char char)))
241 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
242 (not (eq (car l) 'composition)))))
69eba008 243
8057896b 244;; Coding system staffs
4ed46869 245
8057896b 246;; Coding system is a symbol that has the property `coding-system'.
4ed46869 247;;
8057896b
KH
248;; The value of the property `coding-system' is a vector of the
249;; following format:
250;; [TYPE MNEMONIC DOC-STRING NOT-USED-NOW FLAGS]
251;; We call this vector as coding-spec. See comments in src/coding.c
252;; for more detail. The property value may be another coding system,
253;; in which case, the coding-spec should be taken from that
254;; coding-system. The 4th element NOT-USED-NOW is kept just for
255;; backward compatibility with old version of Mule.
256
257(defconst coding-spec-type-idx 0)
258(defconst coding-spec-mnemonic-idx 1)
259(defconst coding-spec-doc-string-idx 2)
260(defconst coding-spec-flags-idx 4)
261
262;; Coding system may have proerpty `eol-type'. The value of the
263;; property `eol-type' is integer 0..2 or a vector of three coding
264;; systems. The integer value 0, 1, and 2 indicate the format of
4ed46869
KH
265;; end-of-line LF, CRLF, and CR respectively. The vector value
266;; indicates that the format of end-of-line should be detected
8057896b
KH
267;; automatically. Nth element of the vector is the subsidiary coding
268;; system whose `eol-type' property is N.
4ed46869 269;;
8057896b
KH
270;; Coding system may also have properties `post-read-conversion' and
271;; `pre-write-conversion. Values of these properties are functions.
4ed46869
KH
272;;
273;; The function in `post-read-conversion' is called after some text is
8057896b 274;; inserted and decoded along the coding system and before any
4ed46869
KH
275;; functions in `after-insert-functions' are called. The arguments to
276;; this function is the same as those of a function in
277;; `after-insert-functions', i.e. LENGTH of a text while putting point
278;; at the head of the text to be decoded
279;;
280;; The function in `pre-write-conversion' is called after all
281;; functions in `write-region-annotate-functions' and
282;; `buffer-file-format' are called, and before the text is encoded by
8057896b 283;; the coding system. The arguments to this function is the same as
4ed46869
KH
284;; those of a function in `write-region-annotate-functions', i.e. FROM
285;; and TO specifying region of a text.
286
8057896b
KH
287;; Return Nth element of coding-spec of CODING-SYSTEM.
288(defun coding-system-spec-ref (coding-system n)
289 (check-coding-system coding-system)
290 (let ((vec (coding-system-spec coding-system)))
291 (and vec (aref vec n))))
4ed46869 292
4ed46869 293(defun coding-system-type (coding-system)
6e9722b0 294 "Return TYPE element in coding-spec of CODING-SYSTEM."
8057896b 295 (coding-system-spec-ref coding-system coding-spec-type-idx))
4ed46869 296
4ed46869 297(defun coding-system-mnemonic (coding-system)
8057896b
KH
298 "Return MNEMONIC element in coding-spec of CODING-SYSTEM."
299 (or (coding-system-spec-ref coding-system coding-spec-mnemonic-idx)
300 ?-))
4ed46869 301
8057896b
KH
302(defun coding-system-doc-string (coding-system)
303 "Return DOC-STRING element in coding-spec of CODING-SYSTEM."
304 (coding-system-spec-ref coding-system coding-spec-doc-string-idx))
4ed46869 305
4ed46869 306(defun coding-system-flags (coding-system)
8057896b
KH
307 "Return FLAGS element in coding-spec of CODING-SYSTEM."
308 (coding-system-spec-ref coding-system coding-spec-flags-idx))
4ed46869 309
8057896b
KH
310(defun coding-system-eol-type (coding-system)
311 "Return eol-type property of CODING-SYSTEM."
69eba008 312 (check-coding-system coding-system)
4ed46869
KH
313 (and coding-system
314 (or (get coding-system 'eol-type)
8057896b 315 (coding-system-eol-type (get coding-system 'coding-system)))))
4ed46869 316
6e9722b0
KH
317(defun coding-system-category (coding-system)
318 "Return coding category of CODING-SYSTEM."
319 (and coding-system
320 (symbolp coding-system)
321 (or (get coding-system 'coding-category)
322 (coding-system-category (get coding-system 'coding-system)))))
323
bd882697
KH
324(defun coding-system-parent (coding-system)
325 "Return parent of CODING-SYSTEM."
326 (let ((parent (get coding-system 'parent-coding-system)))
327 (and parent
328 (or (coding-system-parent parent)
329 parent))))
330
6e9722b0
KH
331;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
332(defun make-subsidiary-coding-system (coding-system)
8057896b
KH
333 (let ((subsidiaries (vector (intern (format "%s-unix" coding-system))
334 (intern (format "%s-dos" coding-system))
335 (intern (format "%s-mac" coding-system))))
336 (i 0))
337 (while (< i 3)
6e9722b0 338 (put (aref subsidiaries i) 'coding-system coding-system)
8057896b
KH
339 (put (aref subsidiaries i) 'eol-type i)
340 (put (aref subsidiaries i) 'eol-variant t)
341 (setq i (1+ i)))
342 subsidiaries))
4ed46869 343
8057896b
KH
344(defun make-coding-system (coding-system type mnemonic doc-string
345 &optional flags)
4ed46869 346 "Define a new CODING-SYSTEM (symbol).
8057896b
KH
347Remaining arguments are TYPE, MNEMONIC, DOC-STRING, and FLAGS (optional) which
348construct a coding-spec of CODING-SYSTEM in the following format:
349 [TYPE MNEMONIC DOC-STRING nil FLAGS]
4ed46869
KH
350TYPE is an integer value indicating the type of coding-system as follows:
351 0: Emacs internal format,
352 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
353 2: ISO-2022 including many variants,
354 3: Big5 used mainly on Chinese PC,
355 4: private, CCL programs provide encoding/decoding algorithm.
356MNEMONIC is a character to be displayed on mode line for the coding-system.
8057896b 357DOC-STRING is a documentation string for the coding-system.
4ed46869 358FLAGS specifies more precise information of each TYPE.
d6d6d592 359
4ed46869
KH
360 If TYPE is 2 (ISO-2022), FLAGS should be a list of:
361 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
362 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
d6d6d592 363 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
850101ed 364 SAFE, ACCEPT-LATIN-EXTRA-CODE.
4ed46869
KH
365 CHARSETn are character sets initially designated to Gn graphic registers.
366 If CHARSETn is nil, Gn is never used.
367 If CHARSETn is t, Gn can be used but nothing designated initially.
368 If CHARSETn is a list of character sets, those character sets are
369 designated to Gn on output, but nothing designated to Gn initially.
370 SHORT-FORM non-nil means use short designation sequence on output.
371 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
372 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
373 SPACE on output.
374 SEVEN non-nil means use 7-bit code only on output.
375 LOCKING-SHIFT non-nil means use locking-shift.
376 SINGLE-SHIFT non-nil means use single-shift.
377 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
378 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
379 NO-ISO6429 non-nil means not use ISO6429's direction specification.
69eba008
KH
380 INIT-BOL non-nil means any designation state is assumed to be reset
381 to initial at each beginning of line on output.
382 DESIGNATION-BOL non-nil means designation sequences should be placed
383 at beginning of line on output.
d6d6d592
KH
384 SAFE non-nil means convert unexpected characters to `?' on output.
385 Unexpected characters are what not specified in CHARSETn directly.
850101ed
RS
386 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
387 a code specified in `latin-extra-code-table' (which see) as a valid
388 code of the coding system.
d6d6d592 389
4ed46869
KH
390 If TYPE is 4 (private), FLAGS should be a cons of CCL programs,
391 for encoding and decoding. See the documentation of CCL for more detail."
392
393 ;; At first, set a value of `coding-system' property.
6e9722b0
KH
394 (let ((coding-spec (make-vector 5 nil))
395 coding-category)
8057896b
KH
396 (if (or (not (integerp type)) (< type 0) (> type 4))
397 (error "TYPE argument must be 0..4"))
398 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
399 (error "MNEMONIC arguemnt must be a printable character."))
400 (aset coding-spec 0 type)
401 (aset coding-spec 1 mnemonic)
402 (aset coding-spec 2 (if (stringp doc-string) doc-string ""))
403 (aset coding-spec 3 nil) ; obsolete element
6e9722b0
KH
404 (cond ((= type 0)
405 (setq coding-category 'coding-category-emacs-mule))
406 ((= type 1)
407 (setq coding-category 'coding-category-sjis))
408 ((= type 2) ; ISO2022
4ed46869 409 (let ((i 0)
6e9722b0
KH
410 (vec (make-vector 32 nil))
411 (no-initial-designation t)
412 (g1-designation nil))
4ed46869
KH
413 (while (< i 4)
414 (let ((charset (car flags)))
6e9722b0
KH
415 (if (and no-initial-designation
416 (> i 0)
417 (or (charsetp charset)
418 (and (consp charset)
419 (charsetp (car charset)))))
420 (setq no-initial-designation nil))
421 (if (charsetp charset)
422 (if (= i 1) (setq g1-designation charset))
423 (if (consp charset)
424 (let ((tail charset)
425 elt)
426 (while tail
427 (setq elt (car tail))
69eba008
KH
428 (or (not elt) (eq elt t) (charsetp elt)
429 (error "Invalid charset: %s" elt))
6e9722b0
KH
430 (setq tail (cdr tail)))
431 (setq g1-designation (car charset)))
432 (if (and charset (not (eq charset t)))
433 (error "Invalid charset: %s" charset))))
4ed46869
KH
434 (aset vec i charset))
435 (setq flags (cdr flags) i (1+ i)))
436 (while (and (< i 32) flags)
437 (aset vec i (car flags))
438 (setq flags (cdr flags) i (1+ i)))
6e9722b0
KH
439 (aset coding-spec 4 vec)
440 (if no-initial-designation
441 (put coding-system 'no-initial-designation t))
442 (setq coding-category
443 (if (aref vec 8) ; Use locking-shift.
dc64cd19
KH
444 (or (and (aref vec 7) 'coding-category-iso-7-else)
445 'coding-category-iso-8-else)
6e9722b0
KH
446 (if (aref vec 7) ; 7-bit only.
447 (if (aref vec 9) ; Use single-shift.
dc64cd19 448 'coding-category-iso-7-else
6e9722b0
KH
449 'coding-category-iso-7)
450 (if no-initial-designation
dc64cd19 451 'coding-category-iso-8-else
6e9722b0
KH
452 (if (and (charsetp g1-designation)
453 (= (charset-dimension g1-designation) 2))
454 'coding-category-iso-8-2
455 'coding-category-iso-8-1)))))))
456 ((= type 3)
457 (setq coding-category 'coding-category-big5))
458 ((= type 4) ; private
459 (setq coding-category 'coding-category-binary)
4ed46869
KH
460 (if (and (consp flags)
461 (vectorp (car flags))
462 (vectorp (cdr flags)))
8057896b 463 (aset coding-spec 4 flags)
6e9722b0
KH
464 (error "Invalid FLAGS argument for TYPE 4 (CCL)"))))
465 (put coding-system 'coding-system coding-spec)
466 (put coding-system 'coding-category coding-category)
467 (put coding-category 'coding-systems
468 (cons coding-system (get coding-category 'coding-systems))))
4ed46869
KH
469
470 ;; Next, set a value of `eol-type' property. The value is a vector
6e9722b0 471 ;; of subsidiary coding systems, each corresponds to a coding system
4ed46869 472 ;; for the detected end-of-line format.
8057896b
KH
473 (put coding-system 'eol-type
474 (if (<= type 3)
6e9722b0 475 (make-subsidiary-coding-system coding-system)
8057896b
KH
476 0)))
477
a42763dc 478(defun define-coding-system-alias (alias coding-system)
358d28fb 479 "Define ALIAS as an alias for coding system CODING-SYSTEM."
8057896b 480 (check-coding-system coding-system)
6e9722b0
KH
481 (let ((parent (coding-system-parent coding-system)))
482 (if parent
483 (setq coding-system parent)))
8057896b 484 (put alias 'coding-system coding-system)
6e9722b0
KH
485 (put alias 'parent-coding-system coding-system)
486 (put coding-system 'alias-coding-systems
487 (cons alias (get coding-system 'alias-coding-systems)))
488 (let ((eol-variants (coding-system-eol-type coding-system))
489 subsidiaries)
490 (if (vectorp eol-variants)
491 (let ((i 0))
492 (setq subsidiaries (make-subsidiary-coding-system alias))
493 (while (< i 3)
494 (put (aref subsidiaries i) 'parent-coding-system
495 (aref eol-variants i))
496 (put (aref eol-variants i) 'alias-coding-systems
497 (cons (aref subsidiaries i) (get (aref eol-variants i)
498 'alias-coding-systems)))
499 (setq i (1+ i)))))))
4ed46869
KH
500
501(defun set-buffer-file-coding-system (coding-system &optional force)
358d28fb
RS
502 "Set the file coding-system of the current buffer to CODING-SYSTEM.
503This means that when you save the buffer, it will be converted
504according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
505use \\[list-coding-systems].
506
507If the buffer's previous file coding-system value specifies end-of-line
508conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
509merged with the already-specified end-of-line conversion.
510However, if the optional prefix argument FORCE is non-nil,
511them CODING-SYSTEM is used exactly as specified."
4ed46869
KH
512 (interactive "zBuffer-file-coding-system: \nP")
513 (check-coding-system coding-system)
514 (if (null force)
8057896b
KH
515 (let ((x (coding-system-eol-type buffer-file-coding-system))
516 (y (coding-system-eol-type coding-system)))
4ed46869
KH
517 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
518 (setq coding-system (aref y x)))))
519 (setq buffer-file-coding-system coding-system)
520 (set-buffer-modified-p t)
521 (force-mode-line-update))
522
358d28fb
RS
523(defvar default-terminal-coding-system nil
524 "Default value for the terminal coding system.
525This is normally set according to the selected language environment.
526See also the command `set-terminal-coding-system'.")
527
df100398
KH
528(defun set-terminal-coding-system (coding-system)
529 "Set coding system of your terminal to CODING-SYSTEM.
358d28fb
RS
530All text output to the terminal will be encoded
531with the specified coding system.
532For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
533The default is determined by the selected language environment
534or by the previous use of this command."
535 (interactive
536 (list (read-coding-system
537 (format "Coding system for terminal display (default, %s): "
538 (if (and (not (terminal-coding-system))
539 default-terminal-coding-system)
540 default-terminal-coding-system)))))
541 (if (and (not coding-system)
542 (not (terminal-coding-system)))
543 (setq coding-system default-terminal-coding-system))
544 (if coding-system
545 (setq default-terminal-coding-system coding-system))
df100398
KH
546 (set-terminal-coding-system-internal coding-system)
547 (redraw-frame (selected-frame)))
548
358d28fb
RS
549(defvar default-keyboard-coding-system nil
550 "Default value of the keyboard coding system.
551This is normally set according to the selected language environment.
552See also the command `set-keyboard-coding-system'.")
553
df100398 554(defun set-keyboard-coding-system (coding-system)
358d28fb
RS
555 "Set coding system for keyboard input to CODING-SYSTEM.
556In addition, this command enables Encoded-kbd minor mode.
557\(If CODING-SYSTEM is nil, Encoded-bkd mode is turned off.)
558For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
559The default is determined by the selected language environment
560or by the previous use of this command."
561 (interactive
562 (list (read-coding-system
563 (format "Coding system for keyboard input (default, %s): "
564 (if (and (not (keyboard-coding-system))
565 default-keyboard-coding-system)
566 default-keyboard-coding-system)))))
567 (if (and (not coding-system)
568 (not (keyboard-coding-system)))
569 (setq coding-system default-keyboard-coding-system))
570 (if coding-system
571 (setq default-keyboard-coding-system coding-system))
df100398
KH
572 (set-keyboard-coding-system-internal coding-system)
573 (encoded-kbd-mode (if coding-system 1 0)))
574
575(defun set-buffer-process-coding-system (decoding encoding)
358d28fb 576 "Set coding systems for the process associated with the current buffer.
df100398 577DECODING is the coding system to be used to decode input from the process,
358d28fb
RS
578ENCODING is the coding system to be used to encode output to the process.
579
580For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
4ed46869
KH
581 (interactive
582 "zCoding-system for process input: \nzCoding-system for process output: ")
583 (let ((proc (get-buffer-process (current-buffer))))
584 (if (null proc)
585 (error "no process")
df100398
KH
586 (check-coding-system decoding)
587 (check-coding-system encoding)
588 (set-process-coding-system proc decoding encoding)))
4ed46869
KH
589 (force-mode-line-update))
590
4ed46869
KH
591(defun set-coding-priority (arg)
592 "Set priority of coding-category according to LIST.
593LIST is a list of coding-categories ordered by priority."
594 (let (l)
595 ;; Put coding-categories listed in ARG to L while checking the
596 ;; validity. We assume that `coding-category-list' contains whole
597 ;; coding-categories.
598 (while arg
599 (if (null (memq (car arg) coding-category-list))
600 (error "Invalid element in argument: %s" (car arg)))
601 (setq l (cons (car arg) l))
602 (setq arg (cdr arg)))
603 ;; Put coding-category not listed in ARG to L.
604 (while coding-category-list
605 (if (null (memq (car coding-category-list) l))
606 (setq l (cons (car coding-category-list) l)))
607 (setq coding-category-list (cdr coding-category-list)))
608 ;; Update `coding-category-list' and return it.
609 (setq coding-category-list (nreverse l))))
610
611;;; FILE I/O
612
613;; Set buffer-file-coding-system of the current buffer after some text
614;; is inserted.
615(defun after-insert-file-set-buffer-file-coding-system (inserted)
616 (if last-coding-system-used
617 (let ((coding-system
618 (find-new-buffer-file-coding-system last-coding-system-used))
619 (modified-p (buffer-modified-p)))
620 (if coding-system
621 (set-buffer-file-coding-system coding-system))
622 (set-buffer-modified-p modified-p)))
623 nil)
624
625(setq after-insert-file-functions
626 (cons 'after-insert-file-set-buffer-file-coding-system
627 after-insert-file-functions))
628
8057896b 629;; The coding-spec and eol-type of coding-system returned is decided
4ed46869
KH
630;; independently in the following order.
631;; 1. That of buffer-file-coding-system locally bound.
632;; 2. That of CODING.
633
634(defun find-new-buffer-file-coding-system (coding)
635 "Return a coding system for a buffer when a file of CODING is inserted.
a73a8c89
KH
636The local variable `buffer-file-coding-system' of the current buffer
637is set to the returned value.
df100398 638Return nil if there's no need of setting new buffer-file-coding-system."
4ed46869
KH
639 (let (local-coding local-eol
640 found-eol
641 new-coding new-eol)
642 (if (null coding)
643 ;; Nothing found about coding.
644 nil
645
646 ;; Get information of the current local value of
647 ;; `buffer-file-coding-system' in LOCAL-EOL and LOCAL-CODING.
648 (if (local-variable-p 'buffer-file-coding-system)
649 ;; Something already set locally.
650 (progn
8057896b 651 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
4ed46869
KH
652 (if (null (numberp local-eol))
653 ;; But eol-type is not yet set.
654 (setq local-eol nil))
655 (if (null (eq (coding-system-type buffer-file-coding-system) t))
13d5617d 656 ;; This is not `undecided'.
4ed46869
KH
657 (progn
658 (setq local-coding buffer-file-coding-system)
659 (while (symbolp (get local-coding 'coding-system))
660 (setq local-coding (get local-coding 'coding-system))))
661 )))
662
663 (if (and local-eol local-coding)
664 ;; The current buffer has already set full coding-system, we
665 ;; had better not change it.
666 nil
667
8057896b 668 (setq found-eol (coding-system-eol-type coding))
4ed46869
KH
669 (if (null (numberp found-eol))
670 ;; But eol-type is not found.
671 (setq found-eol nil))
4ed46869
KH
672
673 ;; The local setting takes precedence over the found one.
674 (setq new-coding (or local-coding coding))
675 (setq new-eol (or local-eol found-eol))
676 (if (and (numberp new-eol)
8057896b 677 (vectorp (coding-system-eol-type new-coding)))
4ed46869 678 (setq new-coding
8057896b 679 (aref (coding-system-eol-type new-coding) new-eol)))
4ed46869
KH
680 new-coding))))
681
fe831d33
GV
682(defun modify-coding-system-alist (target-type regexp coding-system)
683 "Modify one of look up tables for finding a coding system on I/O operation.
8c453b46
RS
684There are three of such tables, `file-coding-system-alist',
685`process-coding-system-alist', and `network-coding-system-alist'.
fe831d33
GV
686
687TARGET-TYPE specifies which of them to modify.
8c453b46
RS
688If it is `file', it affects `file-coding-system-alist' (which see).
689If it is `process', it affects `process-coding-system-alist' (which see).
690If it is `network', it affects `network-codign-system-alist' (which see).
fe831d33
GV
691
692REGEXP is a regular expression matching a target of I/O operation.
693The target is a file name if TARGET-TYPE is `file', a program name if
694TARGET-TYPE is `process', or a network service name or a port number
695to connect to if TARGET-TYPE is `network'.
696
697CODING-SYSTEM is a coding system to perform code conversion on the I/O
8c453b46
RS
698operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
699for decoding and encoding respectively,
700or a function symbol which, when called, returns such a cons cell."
fe831d33
GV
701 (or (memq target-type '(file process network))
702 (error "Invalid target type: %s" target-type))
703 (or (stringp regexp)
704 (and (eq target-type 'network) (integerp regexp))
705 (error "Invalid regular expression: %s" regexp))
706 (if (symbolp coding-system)
707 (if (not (fboundp coding-system))
708 (progn
709 (check-coding-system coding-system)
710 (setq coding-system (cons coding-system coding-system))))
711 (check-coding-system (car coding-system))
712 (check-coding-system (cdr coding-system)))
713 (cond ((eq target-type 'file)
714 (let ((slot (assoc regexp file-coding-system-alist)))
715 (if slot
716 (setcdr slot coding-system)
717 (setq file-coding-system-alist
718 (cons (cons regexp coding-system)
719 file-coding-system-alist)))))
720 ((eq target-type 'process)
721 (let ((slot (assoc regexp process-coding-system-alist)))
722 (if slot
723 (setcdr slot coding-system)
724 (setq process-coding-system-alist
725 (cons (cons regexp coding-system)
726 process-coding-system-alist)))))
727 (t
728 (let ((slot (assoc regexp network-coding-system-alist)))
729 (if slot
730 (setcdr slot coding-system)
731 (setq network-coding-system-alist
732 (cons (cons regexp coding-system)
733 network-coding-system-alist)))))))
734
a73a8c89
KH
735(defun make-unification-table (&rest args)
736 "Make a unification table (char table) from arguments.
13d5617d
KH
737Each argument is a list of the form (FROM . TO),
738where FROM is a character to be unified to TO.
739
740FROM can be a generic character (see make-char). In this case, TO is
741a generic character containing the same number of charcters or a
742oridinal character. If FROM and TO are both generic characters, all
743characters belonging to FROM are unified to characters belonging to TO
744without changing their position code(s)."
a73a8c89
KH
745 (let ((table (make-char-table 'character-unification-table))
746 revlist)
747 (while args
748 (let ((elts (car args)))
749 (while elts
13d5617d
KH
750 (let* ((from (car (car elts)))
751 (from-i 0) ; degree of freedom of FROM
752 (from-rev (nreverse (split-char from)))
753 (to (cdr (car elts)))
754 (to-i 0) ; degree of freedom of TO
755 (to-rev (nreverse (split-char to))))
756 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
757 (while (eq (car from-rev) 0)
758 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
759 (while (eq (car to-rev) 0)
760 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
761 (if (and (/= from-i to-i) (/= to-i 0))
762 (error "Invalid character pair (%d . %d)" from to))
763 ;; If we have already unified TO to TO-ALT, FROM should
764 ;; also be unified to TO-ALT. But, this is only if TO is
765 ;; a generic character or TO-ALT is not a generic
766 ;; character.
767 (let ((to-alt (aref table to)))
768 (if (and to-alt
769 (or (> to-i 0) (not (generic-char-p to-alt))))
770 (setq to to-alt)))
771 (if (> from-i 0)
772 (set-char-table-default table from to)
773 (aset table from to))
a73a8c89
KH
774 ;; If we have already unified some chars to FROM, they
775 ;; should also be unified to TO.
776 (let ((l (assq from revlist)))
777 (if l
778 (let ((ch (car l)))
779 (setcar l to)
780 (setq l (cdr l))
781 (while l
782 (aset table ch to)
783 (setq l (cdr l)) ))))
784 ;; Now update REVLIST.
785 (let ((l (assq to revlist)))
786 (if l
787 (setcdr l (cons from (cdr l)))
788 (setq revlist (cons (list to from) revlist)))))
789 (setq elts (cdr elts))))
790 (setq args (cdr args)))
791 ;; Return TABLE just created.
792 table))
793
69eba008
KH
794;;; Initialize some variables.
795
796(put 'use-default-ascent 'char-table-extra-slots 0)
797(setq use-default-ascent (make-char-table 'use-default-ascent))
d6d6d592
KH
798(put 'ignore-relative-composition 'char-table-extra-slots 0)
799(setq ignore-relative-composition
800 (make-char-table 'ignore-relative-composition))
69eba008
KH
801
802;;;
4ed46869
KH
803(provide 'mule)
804
805;;; mule.el ends here