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