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