(Fcall_interactively): Pass new arg to Fread_coding_system.
[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,
cd9c3177
KH
355 4: private, CCL programs provide encoding/decoding algorithm,
356 5: Raw-text, which means that text contains random 8-bit codes.
4ed46869 357MNEMONIC is a character to be displayed on mode line for the coding-system.
8057896b 358DOC-STRING is a documentation string for the coding-system.
4ed46869 359FLAGS specifies more precise information of each TYPE.
d6d6d592 360
4ed46869
KH
361 If TYPE is 2 (ISO-2022), FLAGS should be a list of:
362 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
363 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
d6d6d592 364 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
850101ed 365 SAFE, ACCEPT-LATIN-EXTRA-CODE.
4ed46869
KH
366 CHARSETn are character sets initially designated to Gn graphic registers.
367 If CHARSETn is nil, Gn is never used.
368 If CHARSETn is t, Gn can be used but nothing designated initially.
369 If CHARSETn is a list of character sets, those character sets are
370 designated to Gn on output, but nothing designated to Gn initially.
371 SHORT-FORM non-nil means use short designation sequence on output.
372 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
373 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
374 SPACE on output.
375 SEVEN non-nil means use 7-bit code only on output.
376 LOCKING-SHIFT non-nil means use locking-shift.
377 SINGLE-SHIFT non-nil means use single-shift.
378 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
379 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
380 NO-ISO6429 non-nil means not use ISO6429's direction specification.
69eba008
KH
381 INIT-BOL non-nil means any designation state is assumed to be reset
382 to initial at each beginning of line on output.
383 DESIGNATION-BOL non-nil means designation sequences should be placed
384 at beginning of line on output.
d6d6d592
KH
385 SAFE non-nil means convert unexpected characters to `?' on output.
386 Unexpected characters are what not specified in CHARSETn directly.
850101ed
RS
387 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
388 a code specified in `latin-extra-code-table' (which see) as a valid
389 code of the coding system.
d6d6d592 390
4ed46869 391 If TYPE is 4 (private), FLAGS should be a cons of CCL programs,
87aba788 392 for decoding and encoding. See the documentation of CCL for more detail."
4ed46869
KH
393
394 ;; At first, set a value of `coding-system' property.
6e9722b0
KH
395 (let ((coding-spec (make-vector 5 nil))
396 coding-category)
cd9c3177 397 (if (or (not (integerp type)) (< type 0) (> type 5))
8057896b
KH
398 (error "TYPE argument must be 0..4"))
399 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
400 (error "MNEMONIC arguemnt must be a printable character."))
401 (aset coding-spec 0 type)
402 (aset coding-spec 1 mnemonic)
403 (aset coding-spec 2 (if (stringp doc-string) doc-string ""))
404 (aset coding-spec 3 nil) ; obsolete element
6e9722b0
KH
405 (cond ((= type 0)
406 (setq coding-category 'coding-category-emacs-mule))
407 ((= type 1)
408 (setq coding-category 'coding-category-sjis))
409 ((= type 2) ; ISO2022
4ed46869 410 (let ((i 0)
6e9722b0
KH
411 (vec (make-vector 32 nil))
412 (no-initial-designation t)
413 (g1-designation nil))
4ed46869
KH
414 (while (< i 4)
415 (let ((charset (car flags)))
6e9722b0
KH
416 (if (and no-initial-designation
417 (> i 0)
418 (or (charsetp charset)
419 (and (consp charset)
420 (charsetp (car charset)))))
421 (setq no-initial-designation nil))
422 (if (charsetp charset)
423 (if (= i 1) (setq g1-designation charset))
424 (if (consp charset)
425 (let ((tail charset)
426 elt)
427 (while tail
428 (setq elt (car tail))
69eba008
KH
429 (or (not elt) (eq elt t) (charsetp elt)
430 (error "Invalid charset: %s" elt))
6e9722b0
KH
431 (setq tail (cdr tail)))
432 (setq g1-designation (car charset)))
433 (if (and charset (not (eq charset t)))
434 (error "Invalid charset: %s" charset))))
4ed46869
KH
435 (aset vec i charset))
436 (setq flags (cdr flags) i (1+ i)))
437 (while (and (< i 32) flags)
438 (aset vec i (car flags))
439 (setq flags (cdr flags) i (1+ i)))
6e9722b0
KH
440 (aset coding-spec 4 vec)
441 (if no-initial-designation
442 (put coding-system 'no-initial-designation t))
443 (setq coding-category
444 (if (aref vec 8) ; Use locking-shift.
dc64cd19
KH
445 (or (and (aref vec 7) 'coding-category-iso-7-else)
446 'coding-category-iso-8-else)
6e9722b0
KH
447 (if (aref vec 7) ; 7-bit only.
448 (if (aref vec 9) ; Use single-shift.
dc64cd19 449 'coding-category-iso-7-else
6e9722b0
KH
450 'coding-category-iso-7)
451 (if no-initial-designation
dc64cd19 452 'coding-category-iso-8-else
6e9722b0
KH
453 (if (and (charsetp g1-designation)
454 (= (charset-dimension g1-designation) 2))
455 'coding-category-iso-8-2
456 'coding-category-iso-8-1)))))))
457 ((= type 3)
458 (setq coding-category 'coding-category-big5))
459 ((= type 4) ; private
460 (setq coding-category 'coding-category-binary)
4ed46869
KH
461 (if (and (consp flags)
462 (vectorp (car flags))
463 (vectorp (cdr flags)))
8057896b 464 (aset coding-spec 4 flags)
cd9c3177
KH
465 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))
466 (t ; i.e. (= type 5)
467 (setq coding-category 'coding-category-raw-text)))
6e9722b0
KH
468 (put coding-system 'coding-system coding-spec)
469 (put coding-system 'coding-category coding-category)
470 (put coding-category 'coding-systems
471 (cons coding-system (get coding-category 'coding-systems))))
4ed46869
KH
472
473 ;; Next, set a value of `eol-type' property. The value is a vector
6e9722b0 474 ;; of subsidiary coding systems, each corresponds to a coding system
4ed46869 475 ;; for the detected end-of-line format.
8057896b 476 (put coding-system 'eol-type
cd9c3177 477 (if (or (<= type 3) (= type 5))
6e9722b0 478 (make-subsidiary-coding-system coding-system)
8057896b
KH
479 0)))
480
a42763dc 481(defun define-coding-system-alias (alias coding-system)
358d28fb 482 "Define ALIAS as an alias for coding system CODING-SYSTEM."
8057896b 483 (check-coding-system coding-system)
6e9722b0
KH
484 (let ((parent (coding-system-parent coding-system)))
485 (if parent
486 (setq coding-system parent)))
8057896b 487 (put alias 'coding-system coding-system)
6e9722b0
KH
488 (put alias 'parent-coding-system coding-system)
489 (put coding-system 'alias-coding-systems
490 (cons alias (get coding-system 'alias-coding-systems)))
491 (let ((eol-variants (coding-system-eol-type coding-system))
492 subsidiaries)
493 (if (vectorp eol-variants)
494 (let ((i 0))
495 (setq subsidiaries (make-subsidiary-coding-system alias))
496 (while (< i 3)
497 (put (aref subsidiaries i) 'parent-coding-system
498 (aref eol-variants i))
499 (put (aref eol-variants i) 'alias-coding-systems
500 (cons (aref subsidiaries i) (get (aref eol-variants i)
501 'alias-coding-systems)))
502 (setq i (1+ i)))))))
4ed46869
KH
503
504(defun set-buffer-file-coding-system (coding-system &optional force)
358d28fb
RS
505 "Set the file coding-system of the current buffer to CODING-SYSTEM.
506This means that when you save the buffer, it will be converted
507according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
508use \\[list-coding-systems].
509
510If the buffer's previous file coding-system value specifies end-of-line
511conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
512merged with the already-specified end-of-line conversion.
513However, if the optional prefix argument FORCE is non-nil,
514them CODING-SYSTEM is used exactly as specified."
ea8b56b3 515 (interactive "zCoding system for visited file: \nP")
4ed46869
KH
516 (check-coding-system coding-system)
517 (if (null force)
8057896b
KH
518 (let ((x (coding-system-eol-type buffer-file-coding-system))
519 (y (coding-system-eol-type coding-system)))
4ed46869
KH
520 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
521 (setq coding-system (aref y x)))))
522 (setq buffer-file-coding-system coding-system)
523 (set-buffer-modified-p t)
524 (force-mode-line-update))
525
358d28fb
RS
526(defvar default-terminal-coding-system nil
527 "Default value for the terminal coding system.
528This is normally set according to the selected language environment.
529See also the command `set-terminal-coding-system'.")
530
df100398
KH
531(defun set-terminal-coding-system (coding-system)
532 "Set coding system of your terminal to CODING-SYSTEM.
358d28fb
RS
533All text output to the terminal will be encoded
534with the specified coding system.
535For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
536The default is determined by the selected language environment
537or by the previous use of this command."
538 (interactive
539 (list (read-coding-system
540 (format "Coding system for terminal display (default, %s): "
541 (if (and (not (terminal-coding-system))
542 default-terminal-coding-system)
543 default-terminal-coding-system)))))
544 (if (and (not coding-system)
545 (not (terminal-coding-system)))
546 (setq coding-system default-terminal-coding-system))
547 (if coding-system
548 (setq default-terminal-coding-system coding-system))
df100398
KH
549 (set-terminal-coding-system-internal coding-system)
550 (redraw-frame (selected-frame)))
551
358d28fb
RS
552(defvar default-keyboard-coding-system nil
553 "Default value of the keyboard coding system.
554This is normally set according to the selected language environment.
555See also the command `set-keyboard-coding-system'.")
556
df100398 557(defun set-keyboard-coding-system (coding-system)
358d28fb
RS
558 "Set coding system for keyboard input to CODING-SYSTEM.
559In addition, this command enables Encoded-kbd minor mode.
560\(If CODING-SYSTEM is nil, Encoded-bkd mode is turned off.)
561For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
562The default is determined by the selected language environment
563or by the previous use of this command."
564 (interactive
565 (list (read-coding-system
566 (format "Coding system for keyboard input (default, %s): "
567 (if (and (not (keyboard-coding-system))
568 default-keyboard-coding-system)
569 default-keyboard-coding-system)))))
570 (if (and (not coding-system)
571 (not (keyboard-coding-system)))
572 (setq coding-system default-keyboard-coding-system))
573 (if coding-system
574 (setq default-keyboard-coding-system coding-system))
df100398
KH
575 (set-keyboard-coding-system-internal coding-system)
576 (encoded-kbd-mode (if coding-system 1 0)))
577
578(defun set-buffer-process-coding-system (decoding encoding)
358d28fb 579 "Set coding systems for the process associated with the current buffer.
df100398 580DECODING is the coding system to be used to decode input from the process,
358d28fb
RS
581ENCODING is the coding system to be used to encode output to the process.
582
583For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
4ed46869
KH
584 (interactive
585 "zCoding-system for process input: \nzCoding-system for process output: ")
586 (let ((proc (get-buffer-process (current-buffer))))
587 (if (null proc)
588 (error "no process")
df100398
KH
589 (check-coding-system decoding)
590 (check-coding-system encoding)
591 (set-process-coding-system proc decoding encoding)))
4ed46869
KH
592 (force-mode-line-update))
593
4ed46869
KH
594(defun set-coding-priority (arg)
595 "Set priority of coding-category according to LIST.
596LIST is a list of coding-categories ordered by priority."
597 (let (l)
598 ;; Put coding-categories listed in ARG to L while checking the
599 ;; validity. We assume that `coding-category-list' contains whole
600 ;; coding-categories.
601 (while arg
602 (if (null (memq (car arg) coding-category-list))
603 (error "Invalid element in argument: %s" (car arg)))
604 (setq l (cons (car arg) l))
605 (setq arg (cdr arg)))
606 ;; Put coding-category not listed in ARG to L.
607 (while coding-category-list
608 (if (null (memq (car coding-category-list) l))
609 (setq l (cons (car coding-category-list) l)))
610 (setq coding-category-list (cdr coding-category-list)))
611 ;; Update `coding-category-list' and return it.
612 (setq coding-category-list (nreverse l))))
613
614;;; FILE I/O
615
63561304
KH
616(defun set-auto-coding (string)
617 "Return coding system for a file which has STRING at the head and tail.
618STRING is a concatination of the first 1K-byte and
619 the last 3K-byte of the file.
620
621It checks for a -*- coding: tag in the first one or two lines of STRING.
622If there's no coding: tag in the head, it checks local variables spec
623in the tailing 3K-byte oof STRING.
624
625The return value is the specified coding system,
626or nil if nothing specified.
87aba788
KH
627
628The variable `auto-file-coding-system' (which see) is set to this
629function by default."
63561304
KH
630 (condition-case nil
631 (let ((case-fold-search t)
632 (len (length string))
633 (limit (string-match "\n" string))
634 (coding-system nil))
635
636 ;; At first check the head.
637 (if limit
638 (when (string-match "^#!" string)
639 ;; If the file begins with "#!" (exec interpreter
640 ;; magic), look for coding frobs in the first two lines.
641 ;; You cannot necessarily put them in the first line of
642 ;; such a file without screwing up the interpreter
643 ;; invocation.
644 (setq limit (string-match "\n" string limit))
645 (or limit
646 (setq limit len)))
647 (setq limit len))
35ce8cd7
RS
648 (when (and (string-match "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" string)
649 (< (match-beginning 2) limit))
63561304 650 (setq coding-system
35ce8cd7 651 (intern (substring string (match-beginning 2) (match-end 2))))
63561304
KH
652 (if (not (coding-system-p coding-system))
653 (setq coding-system nil)))
654
655 ;; If no coding system is specified in the head, check the tail.
656 (when (and (not coding-system)
657 (let ((idx (if (> len 3000) (- len 3000) 0))
658 start)
659 (while (setq start (string-match "\n\^L" string idx))
660 (setq idx (+ start 2)))
661 (string-match
662 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$"
663 string idx)))
664 ;; The prefix is what comes before "local variables:" in its line.
665 ;; The suffix is what comes after "local variables:" in its line.
666 (let* ((idx (1+ (match-end 0)))
667 (prefix (regexp-quote
668 (substring string
669 (match-beginning 1) (match-end 1))))
670 (suffix (regexp-quote
671 (substring string
672 (match-beginning 2) (match-end 2))))
673 (re-coding (concat "^" prefix
674 "coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
675 suffix "$"))
676 (re-end (concat "^" prefix "end *:[ \t]*" suffix "$"))
677 (limit (or (string-match re-end string idx) len)))
678 (when (and (setq idx (string-match re-coding string idx))
679 (< idx limit))
680 (setq coding-system
681 (intern (substring string
682 (match-beginning 1) (match-end 1))))
683 (or (coding-system-p coding-system)
684 (setq coding-system nil)))))
685
686 coding-system)
687 (error nil)))
688
689(setq set-auto-coding-function 'set-auto-coding)
87aba788 690
4ed46869
KH
691;; Set buffer-file-coding-system of the current buffer after some text
692;; is inserted.
693(defun after-insert-file-set-buffer-file-coding-system (inserted)
694 (if last-coding-system-used
695 (let ((coding-system
696 (find-new-buffer-file-coding-system last-coding-system-used))
697 (modified-p (buffer-modified-p)))
698 (if coding-system
699 (set-buffer-file-coding-system coding-system))
cd9c3177
KH
700 (if (or (eq coding-system 'no-conversion)
701 (eq (coding-system-type coding-system) 5))
702 ;; It seems that random 8-bit codes are read. We had
703 ;; better edit this buffer without multibyte character
704 ;; facility.
705 (setq enable-multibyte-characters nil))
4ed46869
KH
706 (set-buffer-modified-p modified-p)))
707 nil)
708
709(setq after-insert-file-functions
710 (cons 'after-insert-file-set-buffer-file-coding-system
711 after-insert-file-functions))
712
8057896b 713;; The coding-spec and eol-type of coding-system returned is decided
4ed46869
KH
714;; independently in the following order.
715;; 1. That of buffer-file-coding-system locally bound.
716;; 2. That of CODING.
717
718(defun find-new-buffer-file-coding-system (coding)
719 "Return a coding system for a buffer when a file of CODING is inserted.
a73a8c89
KH
720The local variable `buffer-file-coding-system' of the current buffer
721is set to the returned value.
df100398 722Return nil if there's no need of setting new buffer-file-coding-system."
4ed46869
KH
723 (let (local-coding local-eol
724 found-eol
725 new-coding new-eol)
726 (if (null coding)
727 ;; Nothing found about coding.
728 nil
729
730 ;; Get information of the current local value of
731 ;; `buffer-file-coding-system' in LOCAL-EOL and LOCAL-CODING.
732 (if (local-variable-p 'buffer-file-coding-system)
733 ;; Something already set locally.
734 (progn
8057896b 735 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
4ed46869
KH
736 (if (null (numberp local-eol))
737 ;; But eol-type is not yet set.
738 (setq local-eol nil))
739 (if (null (eq (coding-system-type buffer-file-coding-system) t))
13d5617d 740 ;; This is not `undecided'.
4ed46869
KH
741 (progn
742 (setq local-coding buffer-file-coding-system)
743 (while (symbolp (get local-coding 'coding-system))
744 (setq local-coding (get local-coding 'coding-system))))
745 )))
746
747 (if (and local-eol local-coding)
748 ;; The current buffer has already set full coding-system, we
749 ;; had better not change it.
750 nil
751
8057896b 752 (setq found-eol (coding-system-eol-type coding))
4ed46869
KH
753 (if (null (numberp found-eol))
754 ;; But eol-type is not found.
755 (setq found-eol nil))
4ed46869
KH
756
757 ;; The local setting takes precedence over the found one.
758 (setq new-coding (or local-coding coding))
759 (setq new-eol (or local-eol found-eol))
760 (if (and (numberp new-eol)
8057896b 761 (vectorp (coding-system-eol-type new-coding)))
4ed46869 762 (setq new-coding
8057896b 763 (aref (coding-system-eol-type new-coding) new-eol)))
4ed46869
KH
764 new-coding))))
765
fe831d33
GV
766(defun modify-coding-system-alist (target-type regexp coding-system)
767 "Modify one of look up tables for finding a coding system on I/O operation.
8c453b46
RS
768There are three of such tables, `file-coding-system-alist',
769`process-coding-system-alist', and `network-coding-system-alist'.
fe831d33
GV
770
771TARGET-TYPE specifies which of them to modify.
8c453b46
RS
772If it is `file', it affects `file-coding-system-alist' (which see).
773If it is `process', it affects `process-coding-system-alist' (which see).
774If it is `network', it affects `network-codign-system-alist' (which see).
fe831d33
GV
775
776REGEXP is a regular expression matching a target of I/O operation.
777The target is a file name if TARGET-TYPE is `file', a program name if
778TARGET-TYPE is `process', or a network service name or a port number
779to connect to if TARGET-TYPE is `network'.
780
781CODING-SYSTEM is a coding system to perform code conversion on the I/O
8c453b46
RS
782operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
783for decoding and encoding respectively,
784or a function symbol which, when called, returns such a cons cell."
fe831d33
GV
785 (or (memq target-type '(file process network))
786 (error "Invalid target type: %s" target-type))
787 (or (stringp regexp)
788 (and (eq target-type 'network) (integerp regexp))
789 (error "Invalid regular expression: %s" regexp))
790 (if (symbolp coding-system)
791 (if (not (fboundp coding-system))
792 (progn
793 (check-coding-system coding-system)
794 (setq coding-system (cons coding-system coding-system))))
795 (check-coding-system (car coding-system))
796 (check-coding-system (cdr coding-system)))
797 (cond ((eq target-type 'file)
798 (let ((slot (assoc regexp file-coding-system-alist)))
799 (if slot
800 (setcdr slot coding-system)
801 (setq file-coding-system-alist
802 (cons (cons regexp coding-system)
803 file-coding-system-alist)))))
804 ((eq target-type 'process)
805 (let ((slot (assoc regexp process-coding-system-alist)))
806 (if slot
807 (setcdr slot coding-system)
808 (setq process-coding-system-alist
809 (cons (cons regexp coding-system)
810 process-coding-system-alist)))))
811 (t
812 (let ((slot (assoc regexp network-coding-system-alist)))
813 (if slot
814 (setcdr slot coding-system)
815 (setq network-coding-system-alist
816 (cons (cons regexp coding-system)
817 network-coding-system-alist)))))))
818
a73a8c89
KH
819(defun make-unification-table (&rest args)
820 "Make a unification table (char table) from arguments.
13d5617d
KH
821Each argument is a list of the form (FROM . TO),
822where FROM is a character to be unified to TO.
823
824FROM can be a generic character (see make-char). In this case, TO is
825a generic character containing the same number of charcters or a
826oridinal character. If FROM and TO are both generic characters, all
827characters belonging to FROM are unified to characters belonging to TO
828without changing their position code(s)."
a73a8c89
KH
829 (let ((table (make-char-table 'character-unification-table))
830 revlist)
831 (while args
832 (let ((elts (car args)))
833 (while elts
13d5617d
KH
834 (let* ((from (car (car elts)))
835 (from-i 0) ; degree of freedom of FROM
836 (from-rev (nreverse (split-char from)))
837 (to (cdr (car elts)))
838 (to-i 0) ; degree of freedom of TO
839 (to-rev (nreverse (split-char to))))
840 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
841 (while (eq (car from-rev) 0)
842 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
843 (while (eq (car to-rev) 0)
844 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
845 (if (and (/= from-i to-i) (/= to-i 0))
846 (error "Invalid character pair (%d . %d)" from to))
847 ;; If we have already unified TO to TO-ALT, FROM should
848 ;; also be unified to TO-ALT. But, this is only if TO is
849 ;; a generic character or TO-ALT is not a generic
850 ;; character.
851 (let ((to-alt (aref table to)))
852 (if (and to-alt
853 (or (> to-i 0) (not (generic-char-p to-alt))))
854 (setq to to-alt)))
855 (if (> from-i 0)
856 (set-char-table-default table from to)
857 (aset table from to))
a73a8c89
KH
858 ;; If we have already unified some chars to FROM, they
859 ;; should also be unified to TO.
860 (let ((l (assq from revlist)))
861 (if l
862 (let ((ch (car l)))
863 (setcar l to)
864 (setq l (cdr l))
865 (while l
866 (aset table ch to)
867 (setq l (cdr l)) ))))
868 ;; Now update REVLIST.
869 (let ((l (assq to revlist)))
870 (if l
871 (setcdr l (cons from (cdr l)))
872 (setq revlist (cons (list to from) revlist)))))
873 (setq elts (cdr elts))))
874 (setq args (cdr args)))
875 ;; Return TABLE just created.
876 table))
877
69eba008
KH
878;;; Initialize some variables.
879
880(put 'use-default-ascent 'char-table-extra-slots 0)
881(setq use-default-ascent (make-char-table 'use-default-ascent))
d6d6d592
KH
882(put 'ignore-relative-composition 'char-table-extra-slots 0)
883(setq ignore-relative-composition
884 (make-char-table 'ignore-relative-composition))
69eba008
KH
885
886;;;
4ed46869
KH
887(provide 'mule)
888
889;;; mule.el ends here