Fix Next and Prev pointers in @node directives, so
[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.
41cd7d67 4;; Copyright (C) 2001 Free Software Foundation, Inc.
58cfed09 5;; Licensed to the Free Software Foundation.
4ed46869
KH
6
7;; Keywords: mule, multilingual, character set, coding system
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
369314dc
KH
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
4ed46869
KH
25
26;;; Code:
27
d2542b42 28(defconst mule-version "5.0 (SAKAKI)" "\
4ed46869
KH
29Version number and name of this version of MULE (multilingual environment).")
30
35554641 31(defconst mule-version-date "1999.12.7" "\
4ed46869
KH
32Distribution date of this version of MULE (multilingual environment).")
33
34(defun load-with-code-conversion (fullname file &optional noerror nomessage)
0f69cb38
KH
35 "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
36The file contents are decoded before evaluation if necessary.
4ed46869
KH
37If optional second arg NOERROR is non-nil,
38 report no error if FILE doesn't exist.
39Print messages at start and end of loading unless
40 optional third arg NOMESSAGE is non-nil.
41Return t if file exists."
42 (if (null (file-readable-p fullname))
43 (and (null noerror)
44 (signal 'file-error (list "Cannot open load file" file)))
45 ;; Read file with code conversion, and then eval.
46 (let* ((buffer
47 ;; To avoid any autoloading, set default-major-mode to
48 ;; fundamental-mode.
88162676
RS
49 ;; So that we don't get completely screwed if the
50 ;; file is encoded in some complicated character set,
51 ;; read it with real decoding, as a multibyte buffer,
52 ;; even if this is a --unibyte Emacs session.
53 (let ((default-major-mode 'fundamental-mode)
54 (default-enable-multibyte-characters t))
4ed46869
KH
55 ;; We can't use `generate-new-buffer' because files.el
56 ;; is not yet loaded.
57 (get-buffer-create (generate-new-buffer-name " *load*"))))
db5cae4b
SM
58 (load-in-progress t)
59 (source (save-match-data (string-match "\\.el\\'" fullname))))
60 (unless nomessage
61 (if source
62 (message "Loading %s (source)..." file)
63 (message "Loading %s..." file)))
64 (when purify-flag
65 (setq preloaded-file-list (cons file preloaded-file-list)))
4ed46869 66 (unwind-protect
a6acd8a2 67 (let ((load-file-name fullname)
1c4cc63a 68 (set-auto-coding-for-load t)
a6acd8a2 69 (inhibit-file-name-operation nil))
4ed46869
KH
70 (save-excursion
71 (set-buffer buffer)
72 (insert-file-contents fullname)
7d276780
EZ
73 ;; If the loaded file was inserted with no-conversion or
74 ;; raw-text coding system, make the buffer unibyte.
75 ;; Otherwise, eval-buffer might try to interpret random
76 ;; binary junk as multibyte characters.
77 (if (and enable-multibyte-characters
78 (or (eq (coding-system-type last-coding-system-used) 5)
79 (eq last-coding-system-used 'no-conversion)))
80 (set-buffer-multibyte nil))
4ed46869
KH
81 ;; Make `kill-buffer' quiet.
82 (set-buffer-modified-p nil))
0f69cb38 83 ;; Have the original buffer current while we eval.
88162676
RS
84 (eval-buffer buffer nil file
85 ;; If this Emacs is running with --unibyte,
86 ;; convert multibyte strings to unibyte
87 ;; after reading them.
ba74e833 88;; (not default-enable-multibyte-characters)
8dd08b5b 89 nil t
ba74e833 90 ))
cfc70cdf
RS
91 (let (kill-buffer-hook kill-buffer-query-functions)
92 (kill-buffer buffer)))
4ed46869 93 (let ((hook (assoc file after-load-alist)))
db5cae4b
SM
94 (when hook
95 (mapcar (function eval) (cdr hook))))
96 (unless (or nomessage noninteractive)
97 (if source
98 (message "Loading %s (source)...done" file)
99 (message "Loading %s...done" file)))
4ed46869
KH
100 t)))
101
102;; API (Application Program Interface) for charsets.
103
2e02a76f 104(defsubst charset-quoted-standard-p (obj)
521d4010 105 "Return t if OBJ is a quoted symbol, and is the name of a standard charset."
2e02a76f
RS
106 (and (listp obj) (eq (car obj) 'quote)
107 (symbolp (car-safe (cdr obj)))
108 (let ((vector (get (car-safe (cdr obj)) 'charset)))
109 (and (vectorp vector)
110 (< (aref vector 0) 160)))))
4ed46869
KH
111
112(defsubst charsetp (object)
e4a7fdfc 113 "T if OBJECT is a charset."
4ed46869
KH
114 (and (symbolp object) (vectorp (get object 'charset))))
115
116(defsubst charset-info (charset)
117 "Return a vector of information of CHARSET.
118The elements of the vector are:
119 CHARSET-ID, BYTES, DIMENSION, CHARS, WIDTH, DIRECTION,
120 LEADING-CODE-BASE, LEADING-CODE-EXT,
121 ISO-FINAL-CHAR, ISO-GRAPHIC-PLANE,
122 REVERSE-CHARSET, SHORT-NAME, LONG-NAME, DESCRIPTION,
123 PLIST,
124where
125CHARSET-ID (integer) is the identification number of the charset.
aa8bb645
KH
126BYTES (integer) is the length of multi-byte form of a character in
127 the charset: one of 1, 2, 3, and 4.
4ed46869
KH
128DIMENSION (integer) is the number of bytes to represent a character of
129the charset: 1 or 2.
130CHARS (integer) is the number of characters in a dimension: 94 or 96.
4ed46869
KH
131WIDTH (integer) is the number of columns a character in the charset
132 occupies on the screen: one of 0, 1, and 2.
133DIRECTION (integer) is the rendering direction of characters in the
08b4ace5
KH
134 charset when rendering. If 0, render from left to right, else
135 render from right to left.
4ed46869
KH
136LEADING-CODE-BASE (integer) is the base leading-code for the
137 charset.
138LEADING-CODE-EXT (integer) is the extended leading-code for the
139 charset. All charsets of less than 0xA0 has the value 0.
140ISO-FINAL-CHAR (character) is the final character of the
7dd4c92d
KH
141 corresponding ISO 2022 charset. If the charset is not assigned
142 any final character, the value is -1.
4ed46869
KH
143ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
144 while encoding to variants of ISO 2022 coding system, one of the
145 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
7dd4c92d 146 If the charset is not assigned any final character, the value is -1.
4ed46869
KH
147REVERSE-CHARSET (integer) is the charset which differs only in
148 LEFT-TO-RIGHT value from the charset. If there's no such a
149 charset, the value is -1.
150SHORT-NAME (string) is the short name to refer to the charset.
151LONG-NAME (string) is the long name to refer to the charset
152DESCRIPTION (string) is the description string of the charset.
153PLIST (property list) may contain any type of information a user
154 want to put and get by functions `put-charset-property' and
155 `get-charset-property' respectively."
156 (get charset 'charset))
157
40c81f74
PE
158;; It is better not to use backquote in this file,
159;; because that makes a bootstrapping problem
160;; if you need to recompile all the Lisp files using interpreted code.
161
4ed46869
KH
162(defmacro charset-id (charset)
163 "Return charset identification number of CHARSET."
2e02a76f 164 (if (charset-quoted-standard-p charset)
4ed46869 165 (aref (charset-info (nth 1 charset)) 0)
40c81f74 166 (list 'aref (list 'charset-info charset) 0)))
4ed46869
KH
167
168(defmacro charset-bytes (charset)
900dc6e3
KH
169 "Return bytes of CHARSET.
170See the function `charset-info' for more detail."
2e02a76f 171 (if (charset-quoted-standard-p charset)
4ed46869 172 (aref (charset-info (nth 1 charset)) 1)
40c81f74 173 (list 'aref (list 'charset-info charset) 1)))
4ed46869
KH
174
175(defmacro charset-dimension (charset)
900dc6e3
KH
176 "Return dimension of CHARSET.
177See the function `charset-info' for more detail."
2e02a76f 178 (if (charset-quoted-standard-p charset)
4ed46869 179 (aref (charset-info (nth 1 charset)) 2)
40c81f74 180 (list 'aref (list 'charset-info charset) 2)))
4ed46869
KH
181
182(defmacro charset-chars (charset)
900dc6e3
KH
183 "Return character numbers contained in a dimension of CHARSET.
184See the function `charset-info' for more detail."
2e02a76f 185 (if (charset-quoted-standard-p charset)
4ed46869 186 (aref (charset-info (nth 1 charset)) 3)
40c81f74 187 (list 'aref (list 'charset-info charset) 3)))
4ed46869
KH
188
189(defmacro charset-width (charset)
900dc6e3
KH
190 "Return width (how many column occupied on a screen) of CHARSET.
191See the function `charset-info' for more detail."
2e02a76f 192 (if (charset-quoted-standard-p charset)
4ed46869 193 (aref (charset-info (nth 1 charset)) 4)
40c81f74 194 (list 'aref (list 'charset-info charset) 4)))
4ed46869
KH
195
196(defmacro charset-direction (charset)
900dc6e3
KH
197 "Return direction of CHARSET.
198See the function `charset-info' for more detail."
2e02a76f 199 (if (charset-quoted-standard-p charset)
4ed46869 200 (aref (charset-info (nth 1 charset)) 5)
40c81f74 201 (list 'aref (list 'charset-info charset) 5)))
4ed46869
KH
202
203(defmacro charset-iso-final-char (charset)
900dc6e3
KH
204 "Return final char of CHARSET.
205See the function `charset-info' for more detail."
2e02a76f 206 (if (charset-quoted-standard-p charset)
4ed46869 207 (aref (charset-info (nth 1 charset)) 8)
40c81f74 208 (list 'aref (list 'charset-info charset) 8)))
4ed46869
KH
209
210(defmacro charset-iso-graphic-plane (charset)
900dc6e3
KH
211 "Return graphic plane of CHARSET.
212See the function `charset-info' for more detail."
2e02a76f 213 (if (charset-quoted-standard-p charset)
4ed46869 214 (aref (charset-info (nth 1 charset)) 9)
40c81f74 215 (list 'aref (list 'charset-info charset) 9)))
4ed46869
KH
216
217(defmacro charset-reverse-charset (charset)
900dc6e3
KH
218 "Return reverse charset of CHARSET.
219See the function `charset-info' for more detail."
2e02a76f 220 (if (charset-quoted-standard-p charset)
4ed46869 221 (aref (charset-info (nth 1 charset)) 10)
40c81f74 222 (list 'aref (list 'charset-info charset) 10)))
4ed46869
KH
223
224(defmacro charset-short-name (charset)
900dc6e3
KH
225 "Return short name of CHARSET.
226See the function `charset-info' for more detail."
2e02a76f 227 (if (charset-quoted-standard-p charset)
4ed46869 228 (aref (charset-info (nth 1 charset)) 11)
40c81f74 229 (list 'aref (list 'charset-info charset) 11)))
4ed46869
KH
230
231(defmacro charset-long-name (charset)
900dc6e3
KH
232 "Return long name of CHARSET.
233See the function `charset-info' for more detail."
2e02a76f 234 (if (charset-quoted-standard-p charset)
4ed46869 235 (aref (charset-info (nth 1 charset)) 12)
40c81f74 236 (list 'aref (list 'charset-info charset) 12)))
4ed46869
KH
237
238(defmacro charset-description (charset)
0611934b 239 "Return description of CHARSET.
900dc6e3 240See the function `charset-info' for more detail."
2e02a76f 241 (if (charset-quoted-standard-p charset)
4ed46869 242 (aref (charset-info (nth 1 charset)) 13)
40c81f74 243 (list 'aref (list 'charset-info charset) 13)))
4ed46869
KH
244
245(defmacro charset-plist (charset)
900dc6e3
KH
246 "Return list charset property of CHARSET.
247See the function `charset-info' for more detail."
40c81f74
PE
248 (list 'aref
249 (if (charset-quoted-standard-p charset)
250 (charset-info (nth 1 charset))
251 (list 'charset-info charset))
252 14))
4ed46869
KH
253
254(defun set-charset-plist (charset plist)
e8dd0160 255 "Set CHARSET's property list to PLIST, and return PLIST."
4ed46869
KH
256 (aset (charset-info charset) 14 plist))
257
d3e7e7cf
EZ
258(defun make-char (charset &optional code1 code2)
259 "Return a character of CHARSET whose position codes are CODE1 and CODE2.
f98e2797 260CODE1 and CODE2 are optional, but if you don't supply
a9fb0b58
KH
261sufficient position codes, return a generic character which stands for
262all characters or group of characters in the character set.
d3e7e7cf 263A generic character can be used to index a char table (e.g. syntax-table).
9ff05eae
KH
264
265Such character sets as ascii, eight-bit-control, and eight-bit-graphic
266don't have corresponding generic characters. If CHARSET is one of
267them and you don't supply CODE1, return the character of the smallest
268code in CHARSET.
269
d3e7e7cf 270If CODE1 or CODE2 are invalid (out of range), this function signals an error."
636799f2 271 (make-char-internal (charset-id charset) code1 code2))
a6acd8a2
KH
272
273(put 'make-char 'byte-compile
521d4010 274 (function
a6acd8a2
KH
275 (lambda (form)
276 (let ((charset (nth 1 form)))
277 (if (charset-quoted-standard-p charset)
278 (byte-compile-normal-call
279 (cons 'make-char-internal
280 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
281 (byte-compile-normal-call
282 (cons 'make-char-internal
283 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
4ed46869 284
d3675a42 285(defun charset-list ()
900dc6e3
KH
286 "Return list of charsets ever defined.
287
d3675a42 288This function is provided for backward compatibility.
900dc6e3 289Now we have the variable `charset-list'."
d3675a42
KH
290 charset-list)
291
13d5617d
KH
292(defsubst generic-char-p (char)
293 "Return t if and only if CHAR is a generic character.
521d4010 294See also the documentation of `make-char'."
1d935669
KH
295 (and (>= char 0400)
296 (let ((l (split-char char)))
297 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
298 (not (eq (car l) 'composition))))))
69eba008 299
cc57cc54 300(defun decode-char (ccs code-point &optional restriction)
7f341749 301 "Return character specified by coded character set CCS and CODE-POINT in it.
cc57cc54 302Return nil if such a character is not supported.
e76938e7
DL
303Currently the only supported coded character set is `ucs' (ISO/IEC
30410646: Universal Multi-Octet Coded Character Set).
cc57cc54
KH
305
306Optional argument RESTRICTION specifies a way to map the pair of CCS
f7492bc7 307and CODE-POINT to a character. Currently not supported and just ignored."
cc57cc54 308 (cond ((eq ccs 'ucs)
6ba9b20b 309 (cond ((< code-point 160)
cc57cc54
KH
310 code-point)
311 ((< code-point 256)
312 (make-char 'latin-iso8859-1 code-point))
313 ((< code-point #x2500)
314 (setq code-point (- code-point #x0100))
521d4010 315 (make-char 'mule-unicode-0100-24ff
cc57cc54 316 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
6ba9b20b 317 ((< code-point #x3400)
cc57cc54
KH
318 (setq code-point (- code-point #x2500))
319 (make-char 'mule-unicode-2500-33ff
320 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
321 ((and (>= code-point #xe000) (< code-point #x10000))
322 (setq code-point (- code-point #xe000))
323 (make-char 'mule-unicode-e000-ffff
324 (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
325 ))))
326
327(defun encode-char (char ccs &optional restriction)
7f341749 328 "Return code-point in coded character set CCS that corresponds to CHAR.
cc57cc54 329Return nil if CHAR is not included in CCS.
e76938e7
DL
330Currently the only supported coded character set is `ucs' (ISO/IEC
33110646: Universal Multi-Octet Coded Character Set).
7f341749
KH
332
333CHAR should be in one of these charsets:
cc57cc54 334 ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
6ba9b20b 335 mule-unicode-e000-ffff, eight-bit-control
cc57cc54
KH
336Otherwise, return nil.
337
338Optional argument RESTRICTION specifies a way to map CHAR to a
339code-point in CCS. Currently not supported and just ignored."
340 (let* ((split (split-char char))
341 (charset (car split)))
342 (cond ((eq ccs 'ucs)
343 (cond ((eq charset 'ascii)
344 char)
345 ((eq charset 'latin-iso8859-1)
346 (+ (nth 1 split) 128))
347 ((eq charset 'mule-unicode-0100-24ff)
348 (+ #x0100 (+ (* (- (nth 1 split) 32) 96)
349 (- (nth 2 split) 32))))
350 ((eq charset 'mule-unicode-2500-33ff)
351 (+ #x2500 (+ (* (- (nth 1 split) 32) 96)
352 (- (nth 2 split) 32))))
353 ((eq charset 'mule-unicode-e000-ffff)
354 (+ #xe000 (+ (* (- (nth 1 split) 32) 96)
6ba9b20b
KH
355 (- (nth 2 split) 32))))
356 ((eq charset 'eight-bit-control)
357 char))))))
cc57cc54 358
0269ddfb 359\f
e76938e7 360;; Coding system stuff
4ed46869 361
8057896b 362;; Coding system is a symbol that has the property `coding-system'.
4ed46869 363;;
8057896b
KH
364;; The value of the property `coding-system' is a vector of the
365;; following format:
d3675a42 366;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
8057896b 367;; We call this vector as coding-spec. See comments in src/coding.c
521d4010 368;; for more detail.
8057896b
KH
369
370(defconst coding-spec-type-idx 0)
371(defconst coding-spec-mnemonic-idx 1)
372(defconst coding-spec-doc-string-idx 2)
0269ddfb 373(defconst coding-spec-plist-idx 3)
8057896b
KH
374(defconst coding-spec-flags-idx 4)
375
a6acd8a2
KH
376;; PLIST is a property list of a coding system. To share PLIST among
377;; alias coding systems, a coding system has PLIST in coding-spec
378;; instead of having it in normal property list of Lisp symbol.
379;; Here's a list of coding system properties currently being used.
0269ddfb
KH
380;;
381;; o coding-category
382;;
383;; The value is a coding category the coding system belongs to. The
521d4010 384;; function `make-coding-system' sets this value automatically
1b46a680 385;; unless its argument PROPERTIES specifies this property.
4ed46869 386;;
0269ddfb 387;; o alias-coding-systems
4ed46869 388;;
0269ddfb
KH
389;; The value is a list of coding systems of the same alias group. The
390;; first element is the coding system made at first, which we call as
1b46a680
KH
391;; `base coding system'. The function `make-coding-system' sets this
392;; value automatically and `define-coding-system-alias' updates it.
0269ddfb 393;;
3c948dde
KH
394;; See the documentation of make-coding-system for the meanings of the
395;; following properties.
4ed46869 396;;
3c948dde 397;; o post-read-conversion
0269ddfb 398;; o pre-write-conversion
f967223b 399;; o translation-table-for-decode
f967223b 400;; o translation-table-for-encode
c11a8f77 401;; o safe-chars
a6acd8a2 402;; o safe-charsets
b25eef20 403;; o mime-charset
c76b5c99 404;; o valid-codes (meaningful only for a coding system based on CCL)
c76b5c99 405
0269ddfb 406
0269ddfb 407(defsubst coding-system-spec (coding-system)
521d4010 408 "Return coding-spec of CODING-SYSTEM."
0269ddfb 409 (get (check-coding-system coding-system) 'coding-system))
4ed46869 410
4ed46869 411(defun coding-system-type (coding-system)
0269ddfb
KH
412 "Return the coding type of CODING-SYSTEM.
413A coding type is an integer value indicating the encoding method
414of CODING-SYSTEM. See the function `make-coding-system' for more detail."
415 (aref (coding-system-spec coding-system) coding-spec-type-idx))
4ed46869 416
4ed46869 417(defun coding-system-mnemonic (coding-system)
0269ddfb 418 "Return the mnemonic character of CODING-SYSTEM.
6e2c8840
KH
419The mnemonic character of a coding system is used in mode line
420to indicate the coding system. If the arg is nil, return ?-."
421 (let ((spec (coding-system-spec coding-system)))
422 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
4ed46869 423
8057896b 424(defun coding-system-doc-string (coding-system)
0269ddfb
KH
425 "Return the documentation string for CODING-SYSTEM."
426 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
4ed46869 427
d3675a42 428(defun coding-system-plist (coding-system)
0269ddfb
KH
429 "Return the property list of CODING-SYSTEM."
430 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
d3675a42 431
4ed46869 432(defun coding-system-flags (coding-system)
0269ddfb
KH
433 "Return `flags' of CODING-SYSTEM.
434A `flags' of a coding system is a vector of length 32 indicating detailed
435information of a coding system. See the function `make-coding-system'
436for more detail."
437 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
438
439(defun coding-system-get (coding-system prop)
440 "Extract a value from CODING-SYSTEM's property list for property PROP."
441 (plist-get (coding-system-plist coding-system) prop))
442
443(defun coding-system-put (coding-system prop val)
444 "Change value in CODING-SYSTEM's property list PROP to VAL."
445 (let ((plist (coding-system-plist coding-system)))
446 (if plist
447 (plist-put plist prop val)
448 (aset (coding-system-spec coding-system) coding-spec-plist-idx
449 (list prop val)))))
450
451(defun coding-system-category (coding-system)
72f73748
DL
452 "Return the coding category of CODING-SYSTEM.
453See also `coding-category-list'."
0269ddfb
KH
454 (coding-system-get coding-system 'coding-category))
455
456(defun coding-system-base (coding-system)
457 "Return the base coding system of CODING-SYSTEM.
d9e3229d
KH
458A base coding system is what made by `make-coding-system'.
459Any alias nor subsidiary coding systems are not base coding system."
0269ddfb
KH
460 (car (coding-system-get coding-system 'alias-coding-systems)))
461
462(defalias 'coding-system-parent 'coding-system-base)
2598a293 463(make-obsolete 'coding-system-parent 'coding-system-base "20.3")
0269ddfb
KH
464
465;; Coding system also has a property `eol-type'.
466;;
467;; This property indicates how the coding system handles end-of-line
468;; format. The value is integer 0, 1, 2, or a vector of three coding
469;; systems. Each integer value 0, 1, and 2 indicates the format of
470;; end-of-line LF, CRLF, and CR respectively. A vector value
471;; indicates that the format of end-of-line should be detected
472;; automatically. Nth element of the vector is the subsidiary coding
473;; system whose `eol-type' property is N.
4ed46869 474
8057896b 475(defun coding-system-eol-type (coding-system)
0269ddfb
KH
476 "Return eol-type of CODING-SYSTEM.
477An eol-type is integer 0, 1, 2, or a vector of coding systems.
4ed46869 478
0269ddfb
KH
479Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
480CRLF, and CR respectively.
481
482A vector value indicates that a format of end-of-line should be
483detected automatically. Nth element of the vector is the subsidiary
484coding system whose eol-type is N."
485 (get coding-system 'eol-type))
bd882697 486
857ea15c
AS
487(defun coding-system-lessp (x y)
488 (cond ((eq x 'no-conversion) t)
489 ((eq y 'no-conversion) nil)
490 ((eq x 'emacs-mule) t)
491 ((eq y 'emacs-mule) nil)
492 ((eq x 'undecided) t)
493 ((eq y 'undecided) nil)
494 (t (let ((c1 (coding-system-mnemonic x))
495 (c2 (coding-system-mnemonic y)))
496 (or (< (downcase c1) (downcase c2))
497 (and (not (> (downcase c1) (downcase c2)))
498 (< c1 c2)))))))
499
857ea15c 500(defun add-to-coding-system-list (coding-system)
521d4010 501 "Add CODING-SYSTEM to `coding-system-list' while keeping it sorted."
857ea15c
AS
502 (if (or (null coding-system-list)
503 (coding-system-lessp coding-system (car coding-system-list)))
504 (setq coding-system-list (cons coding-system coding-system-list))
505 (let ((len (length coding-system-list))
506 mid (tem coding-system-list))
507 (while (> len 1)
508 (setq mid (nthcdr (/ len 2) tem))
509 (if (coding-system-lessp (car mid) coding-system)
510 (setq tem mid
511 len (- len (/ len 2)))
512 (setq len (/ len 2))))
513 (setcdr tem (cons coding-system (cdr tem))))))
514
80a7463d 515(defun coding-system-list (&optional base-only)
c11a8f77
KH
516 "Return a list of all existing non-subsidiary coding systems.
517If optional arg BASE-ONLY is non-nil, only base coding systems are listed.
518The value doesn't include subsidiary coding systems which are what
519made from bases and aliases automatically for various end-of-line
520formats (e.g. iso-latin-1-unix, koi8-r-dos)."
80a7463d
KH
521 (let* ((codings (copy-sequence coding-system-list))
522 (tail (cons nil codings)))
523 ;; Remove subsidiary coding systems (eol variants) and alias
524 ;; coding systems (if necessary).
525 (while (cdr tail)
526 (let* ((coding (car (cdr tail)))
527 (aliases (coding-system-get coding 'alias-coding-systems)))
528 (if (or
529 ;; CODING is an eol variant if not in ALIASES.
530 (not (memq coding aliases))
531 ;; CODING is an alias if it is not car of ALIASES.
532 (and base-only (not (eq coding (car aliases)))))
533 (setcdr tail (cdr (cdr tail)))
534 (setq tail (cdr tail)))))
535 codings))
536
c11a8f77
KH
537(defun register-char-codings (coding-system safe-chars)
538 (let ((general (char-table-extra-slot char-coding-system-table 0)))
539 (if (eq safe-chars t)
540 (or (memq coding-system general)
541 (set-char-table-extra-slot char-coding-system-table 0
542 (cons coding-system general)))
543 (map-char-table
544 (function
545 (lambda (key val)
546 (if (and (>= key 128) val)
547 (let ((codings (aref char-coding-system-table key)))
548 (or (memq coding-system codings)
549 (aset char-coding-system-table key
550 (cons coding-system codings)))))))
551 safe-chars))))
552
553
6e9722b0 554(defun make-subsidiary-coding-system (coding-system)
521d4010 555 "Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM."
0269ddfb
KH
556 (let ((coding-spec (coding-system-spec coding-system))
557 (subsidiaries (vector (intern (format "%s-unix" coding-system))
8057896b
KH
558 (intern (format "%s-dos" coding-system))
559 (intern (format "%s-mac" coding-system))))
0269ddfb
KH
560 (i 0)
561 temp)
8057896b 562 (while (< i 3)
0269ddfb 563 (put (aref subsidiaries i) 'coding-system coding-spec)
8057896b 564 (put (aref subsidiaries i) 'eol-type i)
857ea15c 565 (add-to-coding-system-list (aref subsidiaries i))
0269ddfb
KH
566 (setq coding-system-alist
567 (cons (list (symbol-name (aref subsidiaries i)))
568 coding-system-alist))
8057896b
KH
569 (setq i (1+ i)))
570 subsidiaries))
4ed46869 571
50c29104
KH
572(defun transform-make-coding-system-args (name type &optional doc-string props)
573 "For internal use only.
574Transform XEmacs style args for `make-coding-system' to Emacs style.
575Value is a list of transformed arguments."
576 (let ((mnemonic (string-to-char (or (plist-get props 'mnemonic) "?")))
577 (eol-type (plist-get props 'eol-type))
578 properties tmp)
579 (cond
580 ((eq eol-type 'lf) (setq eol-type 'unix))
581 ((eq eol-type 'crlf) (setq eol-type 'dos))
582 ((eq eol-type 'cr) (setq eol-type 'mac)))
583 (if (setq tmp (plist-get props 'post-read-conversion))
584 (setq properties (plist-put properties 'post-read-conversion tmp)))
585 (if (setq tmp (plist-get props 'pre-write-conversion))
586 (setq properties (plist-put properties 'pre-write-conversion tmp)))
587 (cond
f4a012a6
KH
588 ((eq type 'shift-jis)
589 `(,name 1 ,mnemonic ,doc-string () ,properties ,eol-type))
590 ((eq type 'iso2022) ; This is not perfect.
591 (if (plist-get props 'escape-quoted)
592 (error "escape-quoted is not supported: %S"
593 `(,name ,type ,doc-string ,props)))
594 (let ((g0 (plist-get props 'charset-g0))
595 (g1 (plist-get props 'charset-g1))
596 (g2 (plist-get props 'charset-g2))
597 (g3 (plist-get props 'charset-g3))
598 (use-roman
599 (and
600 (eq (cadr (assoc 'latin-jisx0201
601 (plist-get props 'input-charset-conversion)))
602 'ascii)
603 (eq (cadr (assoc 'ascii
604 (plist-get props 'output-charset-conversion)))
605 'latin-jisx0201)))
606 (use-oldjis
607 (and
608 (eq (cadr (assoc 'japanese-jisx0208-1978
609 (plist-get props 'input-charset-conversion)))
610 'japanese-jisx0208)
611 (eq (cadr (assoc 'japanese-jisx0208
612 (plist-get props 'output-charset-conversion)))
613 'japanese-jisx0208-1978))))
614 (if (charsetp g0)
615 (if (plist-get props 'force-g0-on-output)
616 (setq g0 `(nil ,g0))
617 (setq g0 `(,g0 t))))
618 (if (charsetp g1)
619 (if (plist-get props 'force-g1-on-output)
620 (setq g1 `(nil ,g1))
621 (setq g1 `(,g1 t))))
622 (if (charsetp g2)
623 (if (plist-get props 'force-g2-on-output)
624 (setq g2 `(nil ,g2))
625 (setq g2 `(,g2 t))))
626 (if (charsetp g3)
627 (if (plist-get props 'force-g3-on-output)
628 (setq g3 `(nil ,g3))
629 (setq g3 `(,g3 t))))
630 `(,name 2 ,mnemonic ,doc-string
631 (,g0 ,g1 ,g2 ,g3
632 ,(plist-get props 'short)
633 ,(not (plist-get props 'no-ascii-eol))
634 ,(not (plist-get props 'no-ascii-cntl))
635 ,(plist-get props 'seven)
636 t
637 ,(not (plist-get props 'lock-shift))
638 ,use-roman
639 ,use-oldjis
640 ,(plist-get props 'no-iso6429)
641 nil nil nil nil)
642 ,properties ,eol-type)))
643 ((eq type 'big5)
644 `(,name 3 ,mnemonic ,doc-string () ,properties ,eol-type))
50c29104 645 ((eq type 'ccl)
f4a012a6 646 `(,name 4 ,mnemonic ,doc-string
50c29104 647 (,(plist-get props 'decode) . ,(plist-get props 'encode))
f4a012a6 648 ,properties ,eol-type))
50c29104 649 (t
f4a012a6 650 (error "unsupported XEmacs style make-coding-style arguments: %S"
50c29104
KH
651 `(,name ,type ,doc-string ,props))))))
652
8057896b 653(defun make-coding-system (coding-system type mnemonic doc-string
1b46a680
KH
654 &optional
655 flags
656 properties
657 eol-type)
3bb1accb 658 "Define a new coding system CODING-SYSTEM (symbol).
521d4010 659Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
d9e3229d 660and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
a6acd8a2 661in the following format:
0269ddfb 662 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
3bb1accb
KH
663
664TYPE is an integer value indicating the type of the coding system as follows:
4ed46869
KH
665 0: Emacs internal format,
666 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
667 2: ISO-2022 including many variants,
668 3: Big5 used mainly on Chinese PC,
cd9c3177 669 4: private, CCL programs provide encoding/decoding algorithm,
521d4010 670 5: Raw-text, which means that text contains random 8-bit codes.
0269ddfb 671
3bb1accb 672MNEMONIC is a character to be displayed on mode line for the coding system.
0269ddfb 673
3bb1accb 674DOC-STRING is a documentation string for the coding system.
0269ddfb 675
3bb1accb 676FLAGS specifies more detailed information of the coding system as follows:
d6d6d592 677
3bb1accb 678 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
4ed46869
KH
679 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
680 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
d6d6d592 681 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
850101ed 682 SAFE, ACCEPT-LATIN-EXTRA-CODE.
4ed46869
KH
683 CHARSETn are character sets initially designated to Gn graphic registers.
684 If CHARSETn is nil, Gn is never used.
685 If CHARSETn is t, Gn can be used but nothing designated initially.
686 If CHARSETn is a list of character sets, those character sets are
687 designated to Gn on output, but nothing designated to Gn initially.
421e3b4e 688 But, character set `ascii' can be designated only to G0.
4ed46869
KH
689 SHORT-FORM non-nil means use short designation sequence on output.
690 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
691 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
692 SPACE on output.
693 SEVEN non-nil means use 7-bit code only on output.
694 LOCKING-SHIFT non-nil means use locking-shift.
695 SINGLE-SHIFT non-nil means use single-shift.
696 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
697 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
698 NO-ISO6429 non-nil means not use ISO6429's direction specification.
69eba008
KH
699 INIT-BOL non-nil means any designation state is assumed to be reset
700 to initial at each beginning of line on output.
701 DESIGNATION-BOL non-nil means designation sequences should be placed
702 at beginning of line on output.
a6acd8a2 703 SAFE non-nil means convert unsafe characters to `?' on output.
c11a8f77
KH
704 Characters not specified in the property `safe-charsets' nor
705 `safe-chars' are unsafe.
850101ed
RS
706 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
707 a code specified in `latin-extra-code-table' (which see) as a valid
708 code of the coding system.
d6d6d592 709
3bb1accb
KH
710 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
711 decoding and encoding. CCL programs should be specified by their
712 symbols.
d9e3229d 713
a2852e45
KH
714PROPERTIES is an alist of properties vs the corresponding values. The
715following properties are recognized:
716
717 o post-read-conversion
718
719 The value is a function to call after some text is inserted and
720 decoded by the coding system itself and before any functions in
521d4010
DL
721 `after-insert-functions' are called. The argument of this
722 function is the same as for a function in
723 `after-insert-file-functions', i.e. LENGTH of the text inserted,
724 with point at the head of the text to be decoded.
a2852e45
KH
725
726 o pre-write-conversion
727
728 The value is a function to call after all functions in
729 `write-region-annotate-functions' and `buffer-file-format' are
730 called, and before the text is encoded by the coding system itself.
a284eea3
DL
731 The arguments to this function are the same as those of a function
732 in `write-region-annotate-functions', i.e. FROM and TO, specifying
521d4010 733 a region of text.
a2852e45
KH
734
735 o translation-table-for-decode
736
737 The value is a translation table to be applied on decoding. See
738 the function `make-translation-table' for the format of translation
521d4010 739 table. This is not applicable to type 4 (CCL-based) coding systems.
a2852e45
KH
740
741 o translation-table-for-encode
742
521d4010
DL
743 The value is a translation table to be applied on encoding. This is
744 not applicable to type 4 (CCL-based) coding systems.
a2852e45
KH
745
746 o safe-chars
747
748 The value is a char table. If a character has non-nil value in it,
749 the character is safely supported by the coding system. This
750 overrides the specification of safe-charsets.
751
752 o safe-charsets
521d4010 753
a2852e45
KH
754 The value is a list of charsets safely supported by the coding
755 system. The value t means that all charsets Emacs handles are
756 supported. Even if some charset is not in this list, it doesn't
521d4010
DL
757 mean that the charset can't be encoded in the coding system;
758 it just means that some other receiver of text encoded
a2852e45
KH
759 in the coding system won't be able to handle that charset.
760
761 o mime-charset
762
763 The value is a symbol of which name is `MIME-charset' parameter of
764 the coding system.
765
766 o valid-codes (meaningful only for a coding system based on CCL)
767
768 The value is a list to indicate valid byte ranges of the encoded
769 file. Each element of the list is an integer or a cons of integer.
770 In the former case, the integer value is a valid byte code. In the
771 latter case, the integers specifies the range of valid byte codes.
772
d9e3229d
KH
773These properties are set in PLIST, a property list. This function
774also sets properties `coding-category' and `alias-coding-systems'
775automatically.
4ed46869 776
1b46a680
KH
777EOL-TYPE specifies the EOL type of the coding-system in one of the
778following formats:
779
780 o symbol (unix, dos, or mac)
781
782 The symbol `unix' means Unix-like EOL (LF), `dos' means
783 DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).
784
785 o number (0, 1, or 2)
786
787 The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
788 respectively.
789
790 o vector of coding-systems of length 3
791
792 The EOL type is detected automatically for the coding system.
793 And, according to the detected EOL type, one of the coding
794 systems in the vector is selected. Elements of the vector
4d3a9228 795 corresponds to Unix-like EOL, DOS-like EOL, and Mac-like EOL
1b46a680
KH
796 in this order.
797
3bb1accb
KH
798Kludgy features for backward compatibility:
799
8001. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
801treated as a compiled CCL code.
802
8032. If PROPERTIES is just a list of character sets, the list is set as
804a value of `safe-charsets' in PLIST."
50c29104
KH
805
806 ;; For compatiblity with XEmacs, we check the type of TYPE. If it
c3d0ee51
EZ
807 ;; is a symbol, perhaps, this function is called with XEmacs-style
808 ;; arguments. Here, try to transform that kind of arguments to
50c29104
KH
809 ;; Emacs style.
810 (if (symbolp type)
811 (let ((args (transform-make-coding-system-args coding-system type
812 mnemonic doc-string)))
813 (setq coding-system (car args)
1053cc93 814 type (nth 1 args)
50c29104
KH
815 mnemonic (nth 2 args)
816 doc-string (nth 3 args)
817 flags (nth 4 args)
818 properties (nth 5 args)
819 eol-type (nth 6 args))))
820
0269ddfb 821 ;; Set a value of `coding-system' property.
6e9722b0 822 (let ((coding-spec (make-vector 5 nil))
d9e3229d
KH
823 (no-initial-designation t)
824 (no-alternative-designation t)
e6cddb1a 825 (accept-latin-extra-code nil)
6e9722b0 826 coding-category)
cd9c3177 827 (if (or (not (integerp type)) (< type 0) (> type 5))
0269ddfb 828 (error "TYPE argument must be 0..5"))
8057896b 829 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
521d4010 830 (error "MNEMONIC argument must be an ASCII printable character"))
0269ddfb
KH
831 (aset coding-spec coding-spec-type-idx type)
832 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
833 (aset coding-spec coding-spec-doc-string-idx
e127a722 834 (purecopy (if (stringp doc-string) doc-string "")))
6e9722b0
KH
835 (cond ((= type 0)
836 (setq coding-category 'coding-category-emacs-mule))
837 ((= type 1)
838 (setq coding-category 'coding-category-sjis))
839 ((= type 2) ; ISO2022
4ed46869 840 (let ((i 0)
6e9722b0 841 (vec (make-vector 32 nil))
05134257
KH
842 (g1-designation nil)
843 (fl flags))
4ed46869 844 (while (< i 4)
05134257 845 (let ((charset (car fl)))
6e9722b0
KH
846 (if (and no-initial-designation
847 (> i 0)
848 (or (charsetp charset)
849 (and (consp charset)
850 (charsetp (car charset)))))
851 (setq no-initial-designation nil))
852 (if (charsetp charset)
853 (if (= i 1) (setq g1-designation charset))
854 (if (consp charset)
855 (let ((tail charset)
856 elt)
857 (while tail
858 (setq elt (car tail))
d9e3229d
KH
859 (if (eq elt t)
860 (setq no-alternative-designation nil)
861 (if (and elt (not (charsetp elt)))
862 (error "Invalid charset: %s" elt)))
6e9722b0
KH
863 (setq tail (cdr tail)))
864 (setq g1-designation (car charset)))
d9e3229d
KH
865 (if charset
866 (if (eq charset t)
867 (setq no-alternative-designation nil)
868 (error "Invalid charset: %s" charset)))))
4ed46869 869 (aset vec i charset))
05134257
KH
870 (setq fl (cdr fl) i (1+ i)))
871 (while (and (< i 32) fl)
872 (aset vec i (car fl))
e6cddb1a
KH
873 (if (and (= i 16) ; ACCEPT-LATIN-EXTRA-CODE
874 (car fl))
875 (setq accept-latin-extra-code t))
05134257 876 (setq fl (cdr fl) i (1+ i)))
6e9722b0 877 (aset coding-spec 4 vec)
6e9722b0
KH
878 (setq coding-category
879 (if (aref vec 8) ; Use locking-shift.
dc64cd19
KH
880 (or (and (aref vec 7) 'coding-category-iso-7-else)
881 'coding-category-iso-8-else)
6e9722b0
KH
882 (if (aref vec 7) ; 7-bit only.
883 (if (aref vec 9) ; Use single-shift.
dc64cd19 884 'coding-category-iso-7-else
d9e3229d
KH
885 (if no-alternative-designation
886 'coding-category-iso-7-tight
887 'coding-category-iso-7))
3ad911d8
KH
888 (if (or no-initial-designation
889 (not no-alternative-designation))
dc64cd19 890 'coding-category-iso-8-else
6e9722b0
KH
891 (if (and (charsetp g1-designation)
892 (= (charset-dimension g1-designation) 2))
893 'coding-category-iso-8-2
894 'coding-category-iso-8-1)))))))
895 ((= type 3)
896 (setq coding-category 'coding-category-big5))
897 ((= type 4) ; private
c76b5c99 898 (setq coding-category 'coding-category-ccl)
3bb1accb
KH
899 (if (not (consp flags))
900 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
901 (let ((decoder (check-ccl-program
902 (car flags)
903 (intern (format "%s-decoder" coding-system))))
904 (encoder (check-ccl-program
905 (cdr flags)
906 (intern (format "%s-encoder" coding-system)))))
907 (if (and decoder encoder)
908 (aset coding-spec 4 (cons decoder encoder))
909 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
cd9c3177
KH
910 (t ; i.e. (= type 5)
911 (setq coding-category 'coding-category-raw-text)))
0269ddfb
KH
912
913 (let ((plist (list 'coding-category coding-category
d9e3229d 914 'alias-coding-systems (list coding-system))))
0269ddfb 915 (if no-initial-designation
d9e3229d
KH
916 (plist-put plist 'no-initial-designation t))
917 (if (and properties
918 (or (eq properties t)
919 (not (consp (car properties)))))
920 ;; In the old version, the arg PROPERTIES is a list to be
921 ;; set in PLIST as a value of property `safe-charsets'.
c11a8f77
KH
922 (setq properties (list (cons 'safe-charsets properties))))
923 ;; In the current version PROPERTIES is a property list.
924 ;; Reflect it into PLIST one by one while handling safe-chars
925 ;; specially.
926 (let ((safe-charsets (cdr (assq 'safe-charsets properties)))
927 (safe-chars (cdr (assq 'safe-chars properties)))
928 (l properties)
929 prop val)
930 ;; If only safe-charsets is specified, make a char-table from
931 ;; it, and store that char-table as the value of `safe-chars'.
932 (if (and (not safe-chars) safe-charsets)
933 (let (charset)
934 (if (eq safe-charsets t)
935 (setq safe-chars t)
936 (setq safe-chars (make-char-table 'safe-chars))
937 (while safe-charsets
938 (setq charset (car safe-charsets)
939 safe-charsets (cdr safe-charsets))
940 (cond ((eq charset 'ascii)) ; just ignore
941 ((eq charset 'eight-bit-control)
942 (let ((i 128))
943 (while (< i 160)
944 (aset safe-chars i t)
945 (setq i (1+ i)))))
946 ((eq charset 'eight-bit-graphic)
947 (let ((i 160))
948 (while (< i 256)
949 (aset safe-chars i t)
950 (setq i (1+ i)))))
951 (t
e6cddb1a
KH
952 (aset safe-chars (make-char charset) t))))
953 (if accept-latin-extra-code
954 (let ((i 128))
955 (while (< i 160)
956 (if (aref latin-extra-code-table i)
957 (aset safe-chars i t))
958 (setq i (1+ i))))))
c11a8f77
KH
959 (setq l (cons (cons 'safe-chars safe-chars) l))))
960 (while l
961 (setq prop (car (car l)) val (cdr (car l)) l (cdr l))
962 (if (eq prop 'safe-chars)
963 (progn
cc57cc54
KH
964 (if (and (symbolp val)
965 (get val 'translation-table))
966 (setq safe-chars (get val 'translation-table)))
967 (register-char-codings coding-system safe-chars)
968 (setq val safe-chars)))
c11a8f77 969 (plist-put plist prop val)))
1b46a680
KH
970 ;; The property `coding-category' may have been set differently
971 ;; through PROPERTIES.
972 (setq coding-category (plist-get plist 'coding-category))
0269ddfb 973 (aset coding-spec coding-spec-plist-idx plist))
6e9722b0 974 (put coding-system 'coding-system coding-spec)
6e9722b0
KH
975 (put coding-category 'coding-systems
976 (cons coding-system (get coding-category 'coding-systems))))
4ed46869 977
1b46a680 978 ;; Next, set a value of `eol-type' property.
af873cd1 979 (if (not eol-type)
1b46a680
KH
980 ;; If EOL-TYPE is nil, set a vector of subsidiary coding
981 ;; systems, each corresponds to a coding system for the detected
982 ;; EOL format.
983 (setq eol-type (make-subsidiary-coding-system coding-system)))
984 (setq eol-type
985 (cond ((or (eq eol-type 'unix) (null eol-type))
986 0)
987 ((eq eol-type 'dos)
988 1)
989 ((eq eol-type 'mac)
990 2)
991 ((or (and (vectorp eol-type)
992 (= (length eol-type) 3))
993 (and (numberp eol-type)
994 (and (>= eol-type 0)
995 (<= eol-type 2))))
996 eol-type)
997 (t
998 (error "Invalid EOL-TYPE spec:%S" eol-type))))
999 (put coding-system 'eol-type eol-type)
0269ddfb
KH
1000
1001 ;; At last, register CODING-SYSTEM in `coding-system-list' and
1002 ;; `coding-system-alist'.
857ea15c 1003 (add-to-coding-system-list coding-system)
0269ddfb 1004 (setq coding-system-alist (cons (list (symbol-name coding-system))
d9e3229d 1005 coding-system-alist))
05134257
KH
1006
1007 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
1008 ;; XXX-with-esc variants.
1009 (let ((coding-category (coding-system-category coding-system)))
1010 (if (or (eq coding-category 'coding-category-iso-8-1)
1011 (eq coding-category 'coding-category-iso-8-2))
1012 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
c11a8f77
KH
1013 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system))
1014 (safe-charsets (assq 'safe-charsets properties))
1015 (mime-charset (assq 'mime-charset properties)))
1016 (if safe-charsets
1017 (setcdr safe-charsets t)
1018 (setq properties (cons (cons 'safe-charsets t) properties)))
1019 (if mime-charset
1020 (setcdr mime-charset nil))
05134257
KH
1021 (make-coding-system esc type mnemonic doc
1022 (if (listp (car flags))
1023 (cons (append (car flags) '(t)) (cdr flags))
1024 (cons (list (car flags) t) (cdr flags)))
c11a8f77 1025 properties))))
05134257 1026
d9e3229d 1027 coding-system)
8057896b 1028
a42763dc 1029(defun define-coding-system-alias (alias coding-system)
358d28fb 1030 "Define ALIAS as an alias for coding system CODING-SYSTEM."
0269ddfb
KH
1031 (put alias 'coding-system (coding-system-spec coding-system))
1032 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
857ea15c 1033 (add-to-coding-system-list alias)
0269ddfb
KH
1034 (setq coding-system-alist (cons (list (symbol-name alias))
1035 coding-system-alist))
1036 (let ((eol-type (coding-system-eol-type coding-system)))
1037 (if (vectorp eol-type)
1038 (put alias 'eol-type (make-subsidiary-coding-system alias))
1039 (put alias 'eol-type eol-type))))
4ed46869
KH
1040
1041(defun set-buffer-file-coding-system (coding-system &optional force)
358d28fb
RS
1042 "Set the file coding-system of the current buffer to CODING-SYSTEM.
1043This means that when you save the buffer, it will be converted
1044according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
1045use \\[list-coding-systems].
1046
1047If the buffer's previous file coding-system value specifies end-of-line
1048conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
1049merged with the already-specified end-of-line conversion.
b839fdcc
KH
1050
1051If the buffer's previous file coding-system value specifies text
1052conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
1053merged with the already-specified text conversion.
1054
1055However, if the optional prefix argument FORCE is non-nil, then
1056CODING-SYSTEM is used exactly as specified.
aeef8f07
KH
1057
1058This marks the buffer modified so that the succeeding \\[save-buffer]
1059surely saves the buffer with CODING-SYSTEM. From a program, if you
1060don't want to mark the buffer modified, just set the variable
1061`buffer-file-coding-system' directly."
d9e3229d 1062 (interactive "zCoding system for visited file (default, nil): \nP")
4ed46869 1063 (check-coding-system coding-system)
36d455c4 1064 (if (and coding-system buffer-file-coding-system (null force))
8dd735c1
KH
1065 (let ((base (coding-system-base buffer-file-coding-system))
1066 (eol (coding-system-eol-type buffer-file-coding-system)))
1067 ;; If CODING-SYSTEM doesn't specify text conversion, merge
1068 ;; with that of buffer-file-coding-system.
1069 (if (eq (coding-system-base coding-system) 'undecided)
1070 (setq coding-system (coding-system-change-text-conversion
1071 coding-system base)))
1072 ;; If CODING-SYSTEM doesn't specify eol conversion, merge with
1073 ;; that of buffer-file-coding-system.
1074 (if (and (vectorp (coding-system-eol-type coding-system))
1075 (numberp eol) (>= eol 0) (<= eol 2))
1076 (setq coding-system (coding-system-change-eol-conversion
1077 coding-system eol)))))
4ed46869
KH
1078 (setq buffer-file-coding-system coding-system)
1079 (set-buffer-modified-p t)
1080 (force-mode-line-update))
1081
358d28fb
RS
1082(defvar default-terminal-coding-system nil
1083 "Default value for the terminal coding system.
1084This is normally set according to the selected language environment.
1085See also the command `set-terminal-coding-system'.")
1086
df100398
KH
1087(defun set-terminal-coding-system (coding-system)
1088 "Set coding system of your terminal to CODING-SYSTEM.
358d28fb
RS
1089All text output to the terminal will be encoded
1090with the specified coding system.
1091For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1092The default is determined by the selected language environment
1093or by the previous use of this command."
1094 (interactive
2e02a76f
RS
1095 (list (let ((default (if (and (not (terminal-coding-system))
1096 default-terminal-coding-system)
1097 default-terminal-coding-system)))
1098 (read-coding-system
1099 (format "Coding system for terminal display (default, %s): "
1100 default)
1101 default))))
358d28fb
RS
1102 (if (and (not coding-system)
1103 (not (terminal-coding-system)))
1104 (setq coding-system default-terminal-coding-system))
1105 (if coding-system
521d4010 1106 (setq default-terminal-coding-system coding-system))
df100398
KH
1107 (set-terminal-coding-system-internal coding-system)
1108 (redraw-frame (selected-frame)))
1109
358d28fb
RS
1110(defvar default-keyboard-coding-system nil
1111 "Default value of the keyboard coding system.
1112This is normally set according to the selected language environment.
1113See also the command `set-keyboard-coding-system'.")
1114
df100398 1115(defun set-keyboard-coding-system (coding-system)
358d28fb
RS
1116 "Set coding system for keyboard input to CODING-SYSTEM.
1117In addition, this command enables Encoded-kbd minor mode.
6d34f495
DL
1118\(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off -- see
1119`encoded-kbd-mode'.)
358d28fb
RS
1120For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
1121The default is determined by the selected language environment
1122or by the previous use of this command."
1123 (interactive
2e02a76f
RS
1124 (list (let ((default (if (and (not (keyboard-coding-system))
1125 default-keyboard-coding-system)
1126 default-keyboard-coding-system)))
1127 (read-coding-system
1128 (format "Coding system for keyboard input (default, %s): "
1129 default)
1130 default))))
358d28fb
RS
1131 (if (and (not coding-system)
1132 (not (keyboard-coding-system)))
1133 (setq coding-system default-keyboard-coding-system))
1134 (if coding-system
1135 (setq default-keyboard-coding-system coding-system))
df100398
KH
1136 (set-keyboard-coding-system-internal coding-system)
1137 (encoded-kbd-mode (if coding-system 1 0)))
1138
6d34f495
DL
1139(defcustom keyboard-coding-system nil
1140 "Specify coding system for keyboard input.
1141If you set this on a terminal which can't distinguish Meta keys from
11428-bit characters, you will have to use ESC to type Meta characters.
1143See Info node `Specify Coding' and Info node `Single-Byte Character Support'.
1144
1145Setting this variable directly does not take effect;
1146use either M-x customize or \\[set-keyboard-coding-system]."
1147 :type '(coding-system :tag "Coding system")
1148 :link '(info-link "(emacs)Specify Coding")
1149 :link '(info-link "(emacs)Single-Byte Character Support")
1150 :set (lambda (symbol value)
1151 ;; Don't load encoded-kbd-mode unnecessarily.
1152 (if (or value (boundp 'encoded-kbd-mode))
1153 (set-keyboard-coding-system value)
1154 (set-default 'keyboard-coding-system nil))) ; must initialize
1155 :version "21.1"
1156 :group 'keyboard
1157 :group 'mule)
1158
df100398 1159(defun set-buffer-process-coding-system (decoding encoding)
358d28fb 1160 "Set coding systems for the process associated with the current buffer.
df100398 1161DECODING is the coding system to be used to decode input from the process,
358d28fb
RS
1162ENCODING is the coding system to be used to encode output to the process.
1163
1164For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
4ed46869 1165 (interactive
83911021 1166 "zCoding-system for output from the process: \nzCoding-system for input to the process: ")
4ed46869
KH
1167 (let ((proc (get-buffer-process (current-buffer))))
1168 (if (null proc)
521d4010 1169 (error "No process")
df100398
KH
1170 (check-coding-system decoding)
1171 (check-coding-system encoding)
1172 (set-process-coding-system proc decoding encoding)))
4ed46869
KH
1173 (force-mode-line-update))
1174
d0b99881
RS
1175(defalias 'set-clipboard-coding-system 'set-selection-coding-system)
1176
14915c37 1177(defun set-selection-coding-system (coding-system)
b25eef20
KH
1178 "Make CODING-SYSTEM used for communicating with other X clients .
1179When sending or receiving text via cut_buffer, selection, and clipboard,
1180the text is encoded or decoded by CODING-SYSTEM."
a03b3ce1 1181 (interactive "zCoding system for X selection: ")
b25eef20 1182 (check-coding-system coding-system)
14915c37 1183 (setq selection-coding-system coding-system))
b25eef20 1184
e8dd0160 1185;; Coding system lastly specified by the command
a03b3ce1
KH
1186;; set-next-selection-coding-system.
1187(defvar last-next-selection-coding-system nil)
1188
1189(defun set-next-selection-coding-system (coding-system)
1190 "Make CODING-SYSTEM used for the next communication with other X clients.
1191This setting is effective for the next communication only."
1192 (interactive
1193 (list (read-coding-system
1194 (if last-next-selection-coding-system
1195 (format "Coding system for the next X selection (default, %S): "
1196 last-next-selection-coding-system)
1197 "Coding system for the next X selection: ")
1198 last-next-selection-coding-system)))
1199 (if coding-system
1200 (setq last-next-selection-coding-system coding-system)
1201 (setq coding-system last-next-selection-coding-system))
1202 (check-coding-system coding-system)
1203
1204 (setq next-selection-coding-system coding-system))
1205
4ed46869 1206(defun set-coding-priority (arg)
521d4010
DL
1207 "Set priority of coding categories according to ARG.
1208ARG is a list of coding categories ordered by priority."
d9e3229d
KH
1209 (let ((l arg)
1210 (current-list (copy-sequence coding-category-list)))
e8dd0160 1211 ;; Check the validity of ARG while deleting coding categories in
d9e3229d
KH
1212 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
1213 ;; contains all coding categories.
1214 (while l
1215 (if (or (null (get (car l) 'coding-category-index))
1216 (null (memq (car l) current-list)))
1217 (error "Invalid or duplicated element in argument: %s" arg))
1218 (setq current-list (delq (car l) current-list))
1219 (setq l (cdr l)))
4ed46869 1220 ;; Update `coding-category-list' and return it.
2feaf204
KH
1221 (setq coding-category-list (append arg current-list))
1222 (set-coding-priority-internal)))
4ed46869
KH
1223
1224;;; FILE I/O
1225
e76938e7 1226(defcustom auto-coding-alist
0735296c
DL
1227 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
1228 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\|TGZ\\)\\'" . no-conversion))
835f49b8
KH
1229 "Alist of filename patterns vs corresponding coding systems.
1230Each element looks like (REGEXP . CODING-SYSTEM).
558b0c86 1231A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
835f49b8 1232
7fed493a
RS
1233The settings in this alist take priority over `coding:' tags
1234in the file (see the function `set-auto-coding')
e76938e7
DL
1235and the contents of `file-coding-system-alist'."
1236 :group 'files
1237 :group 'mule
1238 :type '(repeat (cons (regexp :tag "File name regexp")
1239 (symbol :tag "Coding system"))))
835f49b8 1240
502522b2
GM
1241(defcustom auto-coding-regexp-alist
1242 '(("^BABYL OPTIONS:[ \t]*-\\*-[ \t]*rmail[ \t]*-\\*-" . no-conversion))
1243 "Alist of patterns vs corresponding coding systems.
1244Each element looks like (REGEXP . CODING-SYSTEM).
1245A file whose first bytes match REGEXP is decoded by CODING-SYSTEM on reading.
1246
1247The settings in this alist take priority over `coding:' tags
1248in the file (see the function `set-auto-coding')
1249and the contents of `file-coding-system-alist'."
1250 :group 'files
1251 :group 'mule
1252 :type '(repeat (cons (regexp :tag "Regexp")
1253 (symbol :tag "Coding system"))))
1254
1c4cc63a
KH
1255(defvar set-auto-coding-for-load nil
1256 "Non-nil means look for `load-coding' property instead of `coding'.
1257This is used for loading and byte-compiling Emacs Lisp files.")
1258
8a592131
RS
1259(defun auto-coding-alist-lookup (filename)
1260 "Return the coding system specified by `auto-coding-alist' for FILENAME."
1261 (let ((alist auto-coding-alist)
ca128d75 1262 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
8a592131
RS
1263 coding-system)
1264 (while (and alist (not coding-system))
1265 (if (string-match (car (car alist)) filename)
1266 (setq coding-system (cdr (car alist)))
1267 (setq alist (cdr alist))))
1268 coding-system))
1269
502522b2
GM
1270
1271(defun auto-coding-from-file-contents (size)
1272 "Determine a coding system from the contents of the current buffer.
1273The current buffer contains SIZE bytes starting at point.
1274Value is either a coding system or nil."
1275 (save-excursion
1276 (let ((alist auto-coding-regexp-alist)
1277 coding-system)
1278 (while (and alist (not coding-system))
1279 (let ((regexp (car (car alist))))
1280 (when (re-search-forward regexp (+ (point) size) t)
1281 (setq coding-system (cdr (car alist)))))
1282 (setq alist (cdr alist)))
1283 coding-system)))
1284
1285
835f49b8
KH
1286(defun set-auto-coding (filename size)
1287 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1c4cc63a
KH
1288These bytes should include at least the first 1k of the file
1289and the last 3k of the file, but the middle may be omitted.
63561304 1290
502522b2
GM
1291It checks FILENAME against the variable `auto-coding-alist'. If
1292FILENAME doesn't match any entries in the variable, it checks the
1293contents of the current buffer following point against
1294`auto-coding-regexp-alist'. If no match is found, it checks for a
1295`coding:' tag in the first one or two lines following point. If no
1296`coding:' tag is found, it checks for local variables list in the last
12973K bytes out of the SIZE bytes.
63561304
KH
1298
1299The return value is the specified coding system,
1300or nil if nothing specified.
87aba788 1301
ba74e833 1302The variable `set-auto-coding-function' (which see) is set to this
87aba788 1303function by default."
502522b2
GM
1304 (or (auto-coding-alist-lookup filename)
1305 (auto-coding-from-file-contents size)
1306 (let* ((case-fold-search t)
1307 (head-start (point))
1308 (head-end (+ head-start (min size 1024)))
1309 (tail-start (+ head-start (max (- size 3072) 0)))
1310 (tail-end (+ head-start size))
1311 coding-system head-found tail-found pos)
1312 ;; Try a short cut by searching for the string "coding:"
1313 ;; and for "unibyte:" at the head and tail of SIZE bytes.
1314 (setq head-found (or (search-forward "coding:" head-end t)
1315 (search-forward "unibyte:" head-end t)))
1316 (if (and head-found (> head-found tail-start))
1317 ;; Head and tail are overlapped.
1318 (setq tail-found head-found)
1319 (goto-char tail-start)
1320 (setq tail-found (or (search-forward "coding:" tail-end t)
1321 (search-forward "unibyte:" tail-end t))))
1322
1323 ;; At first check the head.
1324 (when head-found
1325 (goto-char head-start)
1326 (setq pos (re-search-forward "[\n\r]" head-end t))
1327 (if (and pos
1328 (= (char-after head-start) ?#)
1329 (= (char-after (1+ head-start)) ?!))
1330 ;; If the file begins with "#!" (exec interpreter magic),
1331 ;; look for coding frobs in the first two lines. You cannot
1332 ;; necessarily put them in the first line of such a file
1333 ;; without screwing up the interpreter invocation.
1334 (setq pos (search-forward "\n" head-end t)))
1335 (if pos (setq head-end pos))
1336 (when (< head-found head-end)
835f49b8 1337 (goto-char head-start)
502522b2
GM
1338 (when (and set-auto-coding-for-load
1339 (re-search-forward
1340 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
1341 head-end t))
1342 (setq coding-system 'raw-text))
1343 (when (and (not coding-system)
1344 (re-search-forward
1345 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
1346 head-end t))
1347 (setq coding-system (intern (match-string 2)))
1348 (or (coding-system-p coding-system)
1349 (setq coding-system nil)))))
1350
1351 ;; If no coding: tag in the head, check the tail.
1352 (when (and tail-found (not coding-system))
1353 (goto-char tail-start)
1354 (search-forward "\n\^L" nil t)
1355 (if (re-search-forward
1356 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
1357 ;; The prefix is what comes before "local variables:" in its
1358 ;; line. The suffix is what comes after "local variables:"
1359 ;; in its line.
1360 (let* ((prefix (regexp-quote (match-string 1)))
1361 (suffix (regexp-quote (match-string 2)))
1362 (re-coding
1363 (concat
1364 "^" prefix
1365 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1366 suffix "$"))
1367 (re-unibyte
1368 (concat
1369 "^" prefix
1370 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1371 suffix "$"))
1372 (re-end
1373 (concat "^" prefix "[ \t]*end *:[ \t]*" suffix "$"))
1374 (pos (point)))
1375 (re-search-forward re-end tail-end 'move)
1376 (setq tail-end (point))
1377 (goto-char pos)
1378 (when (and set-auto-coding-for-load
1379 (re-search-forward re-unibyte tail-end t))
1380 (setq coding-system 'raw-text))
1381 (when (and (not coding-system)
1382 (re-search-forward re-coding tail-end t))
1383 (setq coding-system (intern (match-string 1)))
1384 (or (coding-system-p coding-system)
1385 (setq coding-system nil))))))
1386 coding-system)))
63561304
KH
1387
1388(setq set-auto-coding-function 'set-auto-coding)
87aba788 1389
4ed46869 1390(defun after-insert-file-set-buffer-file-coding-system (inserted)
521d4010 1391 "Set `buffer-file-coding-system' of current buffer after text is inserted."
4ed46869
KH
1392 (if last-coding-system-used
1393 (let ((coding-system
1394 (find-new-buffer-file-coding-system last-coding-system-used))
1395 (modified-p (buffer-modified-p)))
0269ddfb 1396 (when coding-system
71983219 1397 (set-buffer-file-coding-system coding-system t)
d0c26c63
KH
1398 (if (and enable-multibyte-characters
1399 (or (eq coding-system 'no-conversion)
ccb77b4e 1400 (eq (coding-system-type coding-system) 5))
136e48e4
KH
1401 ;; If buffer was unmodified and the size is the
1402 ;; same as INSERTED, we must be visiting it.
1403 (not modified-p)
1404 (= (buffer-size) inserted))
ccb77b4e
RS
1405 ;; For coding systems no-conversion and raw-text...,
1406 ;; edit the buffer as unibyte.
d0c26c63
KH
1407 (let ((pos-byte (position-bytes (+ (point) inserted))))
1408 (set-buffer-multibyte nil)
1409 (setq inserted (- pos-byte (position-bytes (point))))))
0269ddfb 1410 (set-buffer-modified-p modified-p))))
d0c26c63 1411 inserted)
4ed46869 1412
84c9d215
KH
1413(add-hook 'after-insert-file-functions
1414 'after-insert-file-set-buffer-file-coding-system)
4ed46869 1415
8057896b 1416;; The coding-spec and eol-type of coding-system returned is decided
4ed46869
KH
1417;; independently in the following order.
1418;; 1. That of buffer-file-coding-system locally bound.
1419;; 2. That of CODING.
1420
1421(defun find-new-buffer-file-coding-system (coding)
1422 "Return a coding system for a buffer when a file of CODING is inserted.
a73a8c89
KH
1423The local variable `buffer-file-coding-system' of the current buffer
1424is set to the returned value.
509064c5 1425Return nil if there's no need to set `buffer-file-coding-system'."
4ed46869 1426 (let (local-coding local-eol
b685f8d6 1427 found-coding found-eol
4ed46869
KH
1428 new-coding new-eol)
1429 (if (null coding)
1430 ;; Nothing found about coding.
1431 nil
1432
b685f8d6
RS
1433 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1434 ;; and LOCAL-CODING.
1435 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1436 (if (null (numberp local-eol))
1437 ;; But eol-type is not yet set.
1438 (setq local-eol nil))
0269ddfb
KH
1439 (if (and buffer-file-coding-system
1440 (not (eq (coding-system-type buffer-file-coding-system) t)))
1441 ;; This is not `undecided'.
1442 (setq local-coding (coding-system-base buffer-file-coding-system)))
b685f8d6
RS
1443
1444 (if (and (local-variable-p 'buffer-file-coding-system)
1445 local-eol local-coding)
4ed46869
KH
1446 ;; The current buffer has already set full coding-system, we
1447 ;; had better not change it.
1448 nil
1449
8057896b 1450 (setq found-eol (coding-system-eol-type coding))
4ed46869 1451 (if (null (numberp found-eol))
be02cd54
EZ
1452 ;; But eol-type is not found.
1453 ;; If EOL conversions are inhibited, force unix eol-type.
1454 (setq found-eol (if inhibit-eol-conversion 0)))
c76b5c99
KH
1455 (if (eq (coding-system-type coding) t)
1456 (setq found-coding 'undecided)
1457 (setq found-coding (coding-system-base coding)))
1458
1459 (if (and (not found-eol) (eq found-coding 'undecided))
1460 ;; No valid coding information found.
1461 nil
1462
1463 ;; Some coding information (eol or text) found.
1464
1465 ;; The local setting takes precedence over the found one.
1466 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1467 (or local-coding found-coding)
1468 (or found-coding local-coding)))
1469 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1470 (or local-eol found-eol)
1471 (or found-eol local-eol)))
1472
1473 (let ((eol-type (coding-system-eol-type new-coding)))
1474 (if (and (numberp new-eol) (vectorp eol-type))
1475 (aref eol-type new-eol)
1476 new-coding)))))))
4ed46869 1477
fe831d33
GV
1478(defun modify-coding-system-alist (target-type regexp coding-system)
1479 "Modify one of look up tables for finding a coding system on I/O operation.
8c453b46
RS
1480There are three of such tables, `file-coding-system-alist',
1481`process-coding-system-alist', and `network-coding-system-alist'.
fe831d33
GV
1482
1483TARGET-TYPE specifies which of them to modify.
8c453b46
RS
1484If it is `file', it affects `file-coding-system-alist' (which see).
1485If it is `process', it affects `process-coding-system-alist' (which see).
e8dd0160 1486If it is `network', it affects `network-coding-system-alist' (which see).
fe831d33
GV
1487
1488REGEXP is a regular expression matching a target of I/O operation.
1489The target is a file name if TARGET-TYPE is `file', a program name if
1490TARGET-TYPE is `process', or a network service name or a port number
1491to connect to if TARGET-TYPE is `network'.
1492
1493CODING-SYSTEM is a coding system to perform code conversion on the I/O
8c453b46
RS
1494operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1495for decoding and encoding respectively,
1496or a function symbol which, when called, returns such a cons cell."
fe831d33
GV
1497 (or (memq target-type '(file process network))
1498 (error "Invalid target type: %s" target-type))
1499 (or (stringp regexp)
1500 (and (eq target-type 'network) (integerp regexp))
1501 (error "Invalid regular expression: %s" regexp))
1502 (if (symbolp coding-system)
1503 (if (not (fboundp coding-system))
1504 (progn
1505 (check-coding-system coding-system)
1506 (setq coding-system (cons coding-system coding-system))))
1507 (check-coding-system (car coding-system))
1508 (check-coding-system (cdr coding-system)))
1509 (cond ((eq target-type 'file)
1510 (let ((slot (assoc regexp file-coding-system-alist)))
1511 (if slot
1512 (setcdr slot coding-system)
1513 (setq file-coding-system-alist
1514 (cons (cons regexp coding-system)
1515 file-coding-system-alist)))))
1516 ((eq target-type 'process)
1517 (let ((slot (assoc regexp process-coding-system-alist)))
1518 (if slot
1519 (setcdr slot coding-system)
1520 (setq process-coding-system-alist
1521 (cons (cons regexp coding-system)
1522 process-coding-system-alist)))))
1523 (t
1524 (let ((slot (assoc regexp network-coding-system-alist)))
1525 (if slot
1526 (setcdr slot coding-system)
1527 (setq network-coding-system-alist
1528 (cons (cons regexp coding-system)
1529 network-coding-system-alist)))))))
1530
b25eef20 1531(defun make-translation-table (&rest args)
a284eea3
DL
1532 "Make a translation table from arguments.
1533A translation table is a char table intended for for character
1534translation in CCL programs.
1535
1536Each argument is a list of elemnts of the form (FROM . TO), where FROM
1537is a character to be translated to TO.
13d5617d 1538
452fdb31
RS
1539FROM can be a generic character (see `make-char'). In this case, TO is
1540a generic character containing the same number of characters, or a
1541ordinary character. If FROM and TO are both generic characters, all
b25eef20 1542characters belonging to FROM are translated to characters belonging to TO
4e003d37
KH
1543without changing their position code(s).
1544
1545The arguments and forms in each argument are processed in the given
1546order, and if a previous form already translates TO to some other
1547character, say TO-ALT, FROM is also translated to TO-ALT."
f967223b 1548 (let ((table (make-char-table 'translation-table))
a73a8c89
KH
1549 revlist)
1550 (while args
1551 (let ((elts (car args)))
1552 (while elts
13d5617d
KH
1553 (let* ((from (car (car elts)))
1554 (from-i 0) ; degree of freedom of FROM
1555 (from-rev (nreverse (split-char from)))
1556 (to (cdr (car elts)))
1557 (to-i 0) ; degree of freedom of TO
1558 (to-rev (nreverse (split-char to))))
1559 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1560 (while (eq (car from-rev) 0)
1561 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1562 (while (eq (car to-rev) 0)
1563 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1564 (if (and (/= from-i to-i) (/= to-i 0))
1565 (error "Invalid character pair (%d . %d)" from to))
b25eef20
KH
1566 ;; If we have already translated TO to TO-ALT, FROM should
1567 ;; also be translated to TO-ALT. But, this is only if TO
1568 ;; is a generic character or TO-ALT is not a generic
13d5617d
KH
1569 ;; character.
1570 (let ((to-alt (aref table to)))
1571 (if (and to-alt
1572 (or (> to-i 0) (not (generic-char-p to-alt))))
1573 (setq to to-alt)))
1574 (if (> from-i 0)
1575 (set-char-table-default table from to)
1576 (aset table from to))
b25eef20
KH
1577 ;; If we have already translated some chars to FROM, they
1578 ;; should also be translated to TO.
a73a8c89
KH
1579 (let ((l (assq from revlist)))
1580 (if l
1581 (let ((ch (car l)))
1582 (setcar l to)
1583 (setq l (cdr l))
1584 (while l
1585 (aset table ch to)
1586 (setq l (cdr l)) ))))
1587 ;; Now update REVLIST.
1588 (let ((l (assq to revlist)))
1589 (if l
1590 (setcdr l (cons from (cdr l)))
1591 (setq revlist (cons (list to from) revlist)))))
1592 (setq elts (cdr elts))))
1593 (setq args (cdr args)))
1594 ;; Return TABLE just created.
1595 table))
1596
c76b5c99
KH
1597(defun make-translation-table-from-vector (vec)
1598 "Make translation table from decoding vector VEC.
1599VEC is an array of 256 elements to map unibyte codes to multibyte characters.
1600See also the variable `nonascii-translation-table'."
1601 (let ((table (make-char-table 'translation-table))
1602 (rev-table (make-char-table 'translation-table))
1603 (i 0)
1604 ch)
1605 (while (< i 256)
1606 (setq ch (aref vec i))
1607 (aset table i ch)
1608 (if (>= ch 256)
1609 (aset rev-table ch i))
1610 (setq i (1+ i)))
1611 (set-char-table-extra-slot table 0 rev-table)
1612 table))
1613
f967223b 1614(defun define-translation-table (symbol &rest args)
a284eea3
DL
1615 "Define SYMBOL as the name of translation table made by ARGS.
1616This sets up information so that the table can be used for
1617translations in a CCL program.
b25eef20 1618
a284eea3
DL
1619If the first element of ARGS is a char-table whose purpose is
1620`translation-table', just define SYMBOL to name it. (Note that this
1621function does not bind SYMBOL.)
007c79c8 1622
a284eea3 1623Any other ARGS should be suitable as arguments of the function
007c79c8 1624`make-translation-table' (which see).
b25eef20 1625
452fdb31 1626This function sets properties `translation-table' and
521d4010
DL
1627`translation-table-id' of SYMBOL to the created table itself and the
1628identification number of the table respectively. It also registers
1629the table in `translation-table-vector'."
007c79c8
KH
1630 (let ((table (if (and (char-table-p (car args))
1631 (eq (char-table-subtype (car args))
1632 'translation-table))
1633 (car args)
1634 (apply 'make-translation-table args)))
f967223b 1635 (len (length translation-table-vector))
d9e3229d 1636 (id 0)
b25eef20 1637 (done nil))
f967223b 1638 (put symbol 'translation-table table)
b25eef20
KH
1639 (while (not done)
1640 (if (>= id len)
f967223b
KH
1641 (setq translation-table-vector
1642 (vconcat translation-table-vector (make-vector len nil))))
1643 (let ((slot (aref translation-table-vector id)))
b25eef20
KH
1644 (if (or (not slot)
1645 (eq (car slot) symbol))
1646 (progn
f967223b 1647 (aset translation-table-vector id (cons symbol table))
007c79c8
KH
1648 (setq done t))
1649 (setq id (1+ id)))))
f967223b 1650 (put symbol 'translation-table-id id)
d9e3229d
KH
1651 id))
1652
35554641
KH
1653(put 'with-category-table 'lisp-indent-function 1)
1654
1655(defmacro with-category-table (category-table &rest body)
1656 `(let ((current-category-table (category-table)))
1657 (set-category-table ,category-table)
1658 (unwind-protect
1659 (progn ,@body)
1660 (set-category-table current-category-table))))
1661
69eba008
KH
1662;;; Initialize some variables.
1663
1664(put 'use-default-ascent 'char-table-extra-slots 0)
1665(setq use-default-ascent (make-char-table 'use-default-ascent))
d6d6d592
KH
1666(put 'ignore-relative-composition 'char-table-extra-slots 0)
1667(setq ignore-relative-composition
1668 (make-char-table 'ignore-relative-composition))
69eba008
KH
1669
1670;;;
4ed46869
KH
1671(provide 'mule)
1672
1673;;; mule.el ends here