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