*** empty log message ***
[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
35554641 27(defconst mule-version "5.0 (AOI)" "\
4ed46869
KH
28Version number and name of this version of MULE (multilingual environment).")
29
35554641 30(defconst mule-version-date "1999.12.7" "\
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)
7d276780
EZ
72 ;; If the loaded file was inserted with no-conversion or
73 ;; raw-text coding system, make the buffer unibyte.
74 ;; Otherwise, eval-buffer might try to interpret random
75 ;; binary junk as multibyte characters.
76 (if (and enable-multibyte-characters
77 (or (eq (coding-system-type last-coding-system-used) 5)
78 (eq last-coding-system-used 'no-conversion)))
79 (set-buffer-multibyte nil))
4ed46869
KH
80 ;; Make `kill-buffer' quiet.
81 (set-buffer-modified-p nil))
0f69cb38 82 ;; Have the original buffer current while we eval.
88162676
RS
83 (eval-buffer buffer nil file
84 ;; If this Emacs is running with --unibyte,
85 ;; convert multibyte strings to unibyte
86 ;; after reading them.
ba74e833 87;; (not default-enable-multibyte-characters)
8dd08b5b 88 nil t
ba74e833 89 ))
cfc70cdf
RS
90 (let (kill-buffer-hook kill-buffer-query-functions)
91 (kill-buffer buffer)))
4ed46869 92 (let ((hook (assoc file after-load-alist)))
db5cae4b
SM
93 (when hook
94 (mapcar (function eval) (cdr hook))))
95 (unless (or nomessage noninteractive)
96 (if source
97 (message "Loading %s (source)...done" file)
98 (message "Loading %s...done" file)))
4ed46869
KH
99 t)))
100
101;; API (Application Program Interface) for charsets.
102
2e02a76f
RS
103;; Return t if OBJ is a quoted symbol
104;; and the symbol is the name of a standard charset.
105(defsubst charset-quoted-standard-p (obj)
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
141 corresponding ISO 2022 charset.
142ISO-GRAPHIC-PLANE (integer) is the graphic plane to be invoked
143 while encoding to variants of ISO 2022 coding system, one of the
144 following: 0/graphic-plane-left(GL), 1/graphic-plane-right(GR).
145REVERSE-CHARSET (integer) is the charset which differs only in
146 LEFT-TO-RIGHT value from the charset. If there's no such a
147 charset, the value is -1.
148SHORT-NAME (string) is the short name to refer to the charset.
149LONG-NAME (string) is the long name to refer to the charset
150DESCRIPTION (string) is the description string of the charset.
151PLIST (property list) may contain any type of information a user
152 want to put and get by functions `put-charset-property' and
153 `get-charset-property' respectively."
154 (get charset 'charset))
155
40c81f74
PE
156;; It is better not to use backquote in this file,
157;; because that makes a bootstrapping problem
158;; if you need to recompile all the Lisp files using interpreted code.
159
4ed46869
KH
160(defmacro charset-id (charset)
161 "Return charset identification number of CHARSET."
2e02a76f 162 (if (charset-quoted-standard-p charset)
4ed46869 163 (aref (charset-info (nth 1 charset)) 0)
40c81f74 164 (list 'aref (list 'charset-info charset) 0)))
4ed46869
KH
165
166(defmacro charset-bytes (charset)
900dc6e3
KH
167 "Return bytes of CHARSET.
168See the function `charset-info' for more detail."
2e02a76f 169 (if (charset-quoted-standard-p charset)
4ed46869 170 (aref (charset-info (nth 1 charset)) 1)
40c81f74 171 (list 'aref (list 'charset-info charset) 1)))
4ed46869
KH
172
173(defmacro charset-dimension (charset)
900dc6e3
KH
174 "Return dimension of CHARSET.
175See the function `charset-info' for more detail."
2e02a76f 176 (if (charset-quoted-standard-p charset)
4ed46869 177 (aref (charset-info (nth 1 charset)) 2)
40c81f74 178 (list 'aref (list 'charset-info charset) 2)))
4ed46869
KH
179
180(defmacro charset-chars (charset)
900dc6e3
KH
181 "Return character numbers contained in a dimension of CHARSET.
182See the function `charset-info' for more detail."
2e02a76f 183 (if (charset-quoted-standard-p charset)
4ed46869 184 (aref (charset-info (nth 1 charset)) 3)
40c81f74 185 (list 'aref (list 'charset-info charset) 3)))
4ed46869
KH
186
187(defmacro charset-width (charset)
900dc6e3
KH
188 "Return width (how many column occupied on a screen) of CHARSET.
189See the function `charset-info' for more detail."
2e02a76f 190 (if (charset-quoted-standard-p charset)
4ed46869 191 (aref (charset-info (nth 1 charset)) 4)
40c81f74 192 (list 'aref (list 'charset-info charset) 4)))
4ed46869
KH
193
194(defmacro charset-direction (charset)
900dc6e3
KH
195 "Return direction of CHARSET.
196See the function `charset-info' for more detail."
2e02a76f 197 (if (charset-quoted-standard-p charset)
4ed46869 198 (aref (charset-info (nth 1 charset)) 5)
40c81f74 199 (list 'aref (list 'charset-info charset) 5)))
4ed46869
KH
200
201(defmacro charset-iso-final-char (charset)
900dc6e3
KH
202 "Return final char of CHARSET.
203See the function `charset-info' for more detail."
2e02a76f 204 (if (charset-quoted-standard-p charset)
4ed46869 205 (aref (charset-info (nth 1 charset)) 8)
40c81f74 206 (list 'aref (list 'charset-info charset) 8)))
4ed46869
KH
207
208(defmacro charset-iso-graphic-plane (charset)
900dc6e3
KH
209 "Return graphic plane of CHARSET.
210See the function `charset-info' for more detail."
2e02a76f 211 (if (charset-quoted-standard-p charset)
4ed46869 212 (aref (charset-info (nth 1 charset)) 9)
40c81f74 213 (list 'aref (list 'charset-info charset) 9)))
4ed46869
KH
214
215(defmacro charset-reverse-charset (charset)
900dc6e3
KH
216 "Return reverse charset of CHARSET.
217See the function `charset-info' for more detail."
2e02a76f 218 (if (charset-quoted-standard-p charset)
4ed46869 219 (aref (charset-info (nth 1 charset)) 10)
40c81f74 220 (list 'aref (list 'charset-info charset) 10)))
4ed46869
KH
221
222(defmacro charset-short-name (charset)
900dc6e3
KH
223 "Return short name of CHARSET.
224See the function `charset-info' for more detail."
2e02a76f 225 (if (charset-quoted-standard-p charset)
4ed46869 226 (aref (charset-info (nth 1 charset)) 11)
40c81f74 227 (list 'aref (list 'charset-info charset) 11)))
4ed46869
KH
228
229(defmacro charset-long-name (charset)
900dc6e3
KH
230 "Return long name of CHARSET.
231See the function `charset-info' for more detail."
2e02a76f 232 (if (charset-quoted-standard-p charset)
4ed46869 233 (aref (charset-info (nth 1 charset)) 12)
40c81f74 234 (list 'aref (list 'charset-info charset) 12)))
4ed46869
KH
235
236(defmacro charset-description (charset)
0611934b 237 "Return description of CHARSET.
900dc6e3 238See the function `charset-info' for more detail."
2e02a76f 239 (if (charset-quoted-standard-p charset)
4ed46869 240 (aref (charset-info (nth 1 charset)) 13)
40c81f74 241 (list 'aref (list 'charset-info charset) 13)))
4ed46869
KH
242
243(defmacro charset-plist (charset)
900dc6e3
KH
244 "Return list charset property of CHARSET.
245See the function `charset-info' for more detail."
40c81f74
PE
246 (list 'aref
247 (if (charset-quoted-standard-p charset)
248 (charset-info (nth 1 charset))
249 (list 'charset-info charset))
250 14))
4ed46869
KH
251
252(defun set-charset-plist (charset plist)
e8dd0160 253 "Set CHARSET's property list to PLIST, and return PLIST."
4ed46869
KH
254 (aset (charset-info charset) 14 plist))
255
a6acd8a2 256(defun make-char (charset &optional c1 c2)
a9fb0b58 257 "Return a character of CHARSET and position codes CODE1 and CODE2.
f98e2797 258CODE1 and CODE2 are optional, but if you don't supply
a9fb0b58
KH
259sufficient position codes, return a generic character which stands for
260all characters or group of characters in the character set.
a73a8c89 261A generic character can be used to index a char table (e.g. syntax-table)."
a6acd8a2
KH
262 (make-char-internal (charset-id charset) c1 c2))
263
264(put 'make-char 'byte-compile
265 (function
266 (lambda (form)
267 (let ((charset (nth 1 form)))
268 (if (charset-quoted-standard-p charset)
269 (byte-compile-normal-call
270 (cons 'make-char-internal
271 (cons (charset-id (nth 1 charset)) (nthcdr 2 form))))
272 (byte-compile-normal-call
273 (cons 'make-char-internal
274 (cons (list 'charset-id charset) (nthcdr 2 form)))))))))
4ed46869 275
d3675a42 276(defun charset-list ()
900dc6e3
KH
277 "Return list of charsets ever defined.
278
d3675a42 279This function is provided for backward compatibility.
900dc6e3 280Now we have the variable `charset-list'."
d3675a42
KH
281 charset-list)
282
13d5617d
KH
283(defsubst generic-char-p (char)
284 "Return t if and only if CHAR is a generic character.
285See also the documentation of make-char."
1d935669
KH
286 (and (>= char 0400)
287 (let ((l (split-char char)))
288 (and (or (= (nth 1 l) 0) (eq (nth 2 l) 0))
289 (not (eq (car l) 'composition))))))
69eba008 290
0269ddfb 291\f
8057896b 292;; Coding system staffs
4ed46869 293
8057896b 294;; Coding system is a symbol that has the property `coding-system'.
4ed46869 295;;
8057896b
KH
296;; The value of the property `coding-system' is a vector of the
297;; following format:
d3675a42 298;; [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
8057896b 299;; We call this vector as coding-spec. See comments in src/coding.c
d3675a42 300;; for more detail.
8057896b
KH
301
302(defconst coding-spec-type-idx 0)
303(defconst coding-spec-mnemonic-idx 1)
304(defconst coding-spec-doc-string-idx 2)
0269ddfb 305(defconst coding-spec-plist-idx 3)
8057896b
KH
306(defconst coding-spec-flags-idx 4)
307
a6acd8a2
KH
308;; PLIST is a property list of a coding system. To share PLIST among
309;; alias coding systems, a coding system has PLIST in coding-spec
310;; instead of having it in normal property list of Lisp symbol.
311;; Here's a list of coding system properties currently being used.
0269ddfb
KH
312;;
313;; o coding-category
314;;
315;; The value is a coding category the coding system belongs to. The
1b46a680
KH
316;; function `make-coding-system' sets this value automatically
317;; unless its argument PROPERTIES specifies this property.
4ed46869 318;;
0269ddfb 319;; o alias-coding-systems
4ed46869 320;;
0269ddfb
KH
321;; The value is a list of coding systems of the same alias group. The
322;; first element is the coding system made at first, which we call as
1b46a680
KH
323;; `base coding system'. The function `make-coding-system' sets this
324;; value automatically and `define-coding-system-alias' updates it.
0269ddfb
KH
325;;
326;; o post-read-conversion
327;;
328;; The value is a function to call after some text is inserted and
329;; decoded by the coding system itself and before any functions in
330;; `after-insert-functions' are called. The arguments to this
331;; function is the same as those of a function in
4ed46869
KH
332;; `after-insert-functions', i.e. LENGTH of a text while putting point
333;; at the head of the text to be decoded
334;;
0269ddfb
KH
335;; o pre-write-conversion
336;;
337;; The value is a function to call after all functions in
338;; `write-region-annotate-functions' and `buffer-file-format' are
339;; called, and before the text is encoded by the coding system itself.
340;; The arguments to this function is the same as those of a function
341;; in `write-region-annotate-functions', i.e. FROM and TO specifying
342;; region of a text.
343;;
f967223b 344;; o translation-table-for-decode
0269ddfb 345;;
f967223b
KH
346;; The value is a translation table to be applied on decoding. See
347;; the function `make-translation-table' for the format of translation
348;; table.
0269ddfb 349;;
f967223b 350;; o translation-table-for-encode
0269ddfb 351;;
f967223b 352;; The value is a translation table to be applied on encoding.
a6acd8a2
KH
353;;
354;; o safe-charsets
355;;
356;; The value is a list of charsets safely supported by the coding
357;; system. The value t means that all charsets Emacs handles are
358;; supported. Even if some charset is not in this list, it doesn't
359;; mean that the charset can't be encoded in the coding system,
360;; instead, it just means that some other receiver of a text encoded
361;; in the coding system won't be able to handle that charset.
b25eef20
KH
362;;
363;; o mime-charset
364;;
365;; The value is a symbol of which name is `MIME-charset' parameter of
366;; the coding system.
c76b5c99 367;;
1322c8ca
KH
368;; o charset-origin-alist
369;;
370;; The value is a list of this form:
371;; (CHARSET EXTERNAL-CHARSET-NAME ENCODING-FUNCTION).
372;; ENCODING-FUNCTION is a function to encode a character in CHARSET
373;; to the code in EXTERNAL-CHARSET-NAME. The command what-cursor-position
374;; uses this information of the buffer-file-coding-system.
caa85ad9
KH
375;; ENCODING-FUNCTION may be a translation table or a symbol whose
376;; property `translation-table' is a translation table. In these case,
377;; the translation table is used to encode the character.
1322c8ca 378;;
c76b5c99
KH
379;; o valid-codes (meaningful only for a coding system based on CCL)
380;;
381;; The value is a list to indicate valid byte ranges of the encoded
382;; file. Each element of the list is an integer or a cons of integer.
383;; In the former case, the integer value is a valid byte code. In the
e8dd0160 384;; latter case, the integers specifies the range of valid byte codes.
c76b5c99 385
0269ddfb
KH
386
387;; Return coding-spec of CODING-SYSTEM
388(defsubst coding-system-spec (coding-system)
389 (get (check-coding-system coding-system) 'coding-system))
4ed46869 390
4ed46869 391(defun coding-system-type (coding-system)
0269ddfb
KH
392 "Return the coding type of CODING-SYSTEM.
393A coding type is an integer value indicating the encoding method
394of CODING-SYSTEM. See the function `make-coding-system' for more detail."
395 (aref (coding-system-spec coding-system) coding-spec-type-idx))
4ed46869 396
4ed46869 397(defun coding-system-mnemonic (coding-system)
0269ddfb 398 "Return the mnemonic character of CODING-SYSTEM.
6e2c8840
KH
399The mnemonic character of a coding system is used in mode line
400to indicate the coding system. If the arg is nil, return ?-."
401 (let ((spec (coding-system-spec coding-system)))
402 (if spec (aref spec coding-spec-mnemonic-idx) ?-)))
4ed46869 403
8057896b 404(defun coding-system-doc-string (coding-system)
0269ddfb
KH
405 "Return the documentation string for CODING-SYSTEM."
406 (aref (coding-system-spec coding-system) coding-spec-doc-string-idx))
4ed46869 407
d3675a42 408(defun coding-system-plist (coding-system)
0269ddfb
KH
409 "Return the property list of CODING-SYSTEM."
410 (aref (coding-system-spec coding-system) coding-spec-plist-idx))
d3675a42 411
4ed46869 412(defun coding-system-flags (coding-system)
0269ddfb
KH
413 "Return `flags' of CODING-SYSTEM.
414A `flags' of a coding system is a vector of length 32 indicating detailed
415information of a coding system. See the function `make-coding-system'
416for more detail."
417 (aref (coding-system-spec coding-system) coding-spec-flags-idx))
418
419(defun coding-system-get (coding-system prop)
420 "Extract a value from CODING-SYSTEM's property list for property PROP."
421 (plist-get (coding-system-plist coding-system) prop))
422
423(defun coding-system-put (coding-system prop val)
424 "Change value in CODING-SYSTEM's property list PROP to VAL."
425 (let ((plist (coding-system-plist coding-system)))
426 (if plist
427 (plist-put plist prop val)
428 (aset (coding-system-spec coding-system) coding-spec-plist-idx
429 (list prop val)))))
430
431(defun coding-system-category (coding-system)
432 "Return the coding category of CODING-SYSTEM."
433 (coding-system-get coding-system 'coding-category))
434
435(defun coding-system-base (coding-system)
436 "Return the base coding system of CODING-SYSTEM.
d9e3229d
KH
437A base coding system is what made by `make-coding-system'.
438Any alias nor subsidiary coding systems are not base coding system."
0269ddfb
KH
439 (car (coding-system-get coding-system 'alias-coding-systems)))
440
441(defalias 'coding-system-parent 'coding-system-base)
2598a293 442(make-obsolete 'coding-system-parent 'coding-system-base "20.3")
0269ddfb
KH
443
444;; Coding system also has a property `eol-type'.
445;;
446;; This property indicates how the coding system handles end-of-line
447;; format. The value is integer 0, 1, 2, or a vector of three coding
448;; systems. Each integer value 0, 1, and 2 indicates the format of
449;; end-of-line LF, CRLF, and CR respectively. A vector value
450;; indicates that the format of end-of-line should be detected
451;; automatically. Nth element of the vector is the subsidiary coding
452;; system whose `eol-type' property is N.
4ed46869 453
8057896b 454(defun coding-system-eol-type (coding-system)
0269ddfb
KH
455 "Return eol-type of CODING-SYSTEM.
456An eol-type is integer 0, 1, 2, or a vector of coding systems.
4ed46869 457
0269ddfb
KH
458Integer values 0, 1, and 2 indicate a format of end-of-line; LF,
459CRLF, and CR respectively.
460
461A vector value indicates that a format of end-of-line should be
462detected automatically. Nth element of the vector is the subsidiary
463coding system whose eol-type is N."
464 (get coding-system 'eol-type))
bd882697 465
857ea15c
AS
466(defun coding-system-lessp (x y)
467 (cond ((eq x 'no-conversion) t)
468 ((eq y 'no-conversion) nil)
469 ((eq x 'emacs-mule) t)
470 ((eq y 'emacs-mule) nil)
471 ((eq x 'undecided) t)
472 ((eq y 'undecided) nil)
473 (t (let ((c1 (coding-system-mnemonic x))
474 (c2 (coding-system-mnemonic y)))
475 (or (< (downcase c1) (downcase c2))
476 (and (not (> (downcase c1) (downcase c2)))
477 (< c1 c2)))))))
478
479;; Add CODING-SYSTEM to coding-system-list while keeping it sorted.
480(defun add-to-coding-system-list (coding-system)
481 (if (or (null coding-system-list)
482 (coding-system-lessp coding-system (car coding-system-list)))
483 (setq coding-system-list (cons coding-system coding-system-list))
484 (let ((len (length coding-system-list))
485 mid (tem coding-system-list))
486 (while (> len 1)
487 (setq mid (nthcdr (/ len 2) tem))
488 (if (coding-system-lessp (car mid) coding-system)
489 (setq tem mid
490 len (- len (/ len 2)))
491 (setq len (/ len 2))))
492 (setcdr tem (cons coding-system (cdr tem))))))
493
80a7463d
KH
494(defun coding-system-list (&optional base-only)
495 "Return a list of all existing coding systems.
496If optional arg BASE-ONLY is non-nil, only base coding systems are listed."
497 (let* ((codings (copy-sequence coding-system-list))
498 (tail (cons nil codings)))
499 ;; Remove subsidiary coding systems (eol variants) and alias
500 ;; coding systems (if necessary).
501 (while (cdr tail)
502 (let* ((coding (car (cdr tail)))
503 (aliases (coding-system-get coding 'alias-coding-systems)))
504 (if (or
505 ;; CODING is an eol variant if not in ALIASES.
506 (not (memq coding aliases))
507 ;; CODING is an alias if it is not car of ALIASES.
508 (and base-only (not (eq coding (car aliases)))))
509 (setcdr tail (cdr (cdr tail)))
510 (setq tail (cdr tail)))))
511 codings))
512
6e9722b0
KH
513;; Make subsidiary coding systems (eol-type variants) of CODING-SYSTEM.
514(defun make-subsidiary-coding-system (coding-system)
0269ddfb
KH
515 (let ((coding-spec (coding-system-spec coding-system))
516 (subsidiaries (vector (intern (format "%s-unix" coding-system))
8057896b
KH
517 (intern (format "%s-dos" coding-system))
518 (intern (format "%s-mac" coding-system))))
0269ddfb
KH
519 (i 0)
520 temp)
8057896b 521 (while (< i 3)
0269ddfb 522 (put (aref subsidiaries i) 'coding-system coding-spec)
8057896b 523 (put (aref subsidiaries i) 'eol-type i)
857ea15c 524 (add-to-coding-system-list (aref subsidiaries i))
0269ddfb
KH
525 (setq coding-system-alist
526 (cons (list (symbol-name (aref subsidiaries i)))
527 coding-system-alist))
8057896b
KH
528 (setq i (1+ i)))
529 subsidiaries))
4ed46869 530
8057896b 531(defun make-coding-system (coding-system type mnemonic doc-string
1b46a680
KH
532 &optional
533 flags
534 properties
535 eol-type)
3bb1accb 536 "Define a new coding system CODING-SYSTEM (symbol).
a6acd8a2 537Remaining arguments are TYPE, MNEMONIC, DOC-STRING, FLAGS (optional),
d9e3229d 538and PROPERTIES (optional) which construct a coding-spec of CODING-SYSTEM
a6acd8a2 539in the following format:
0269ddfb 540 [TYPE MNEMONIC DOC-STRING PLIST FLAGS]
3bb1accb
KH
541
542TYPE is an integer value indicating the type of the coding system as follows:
4ed46869
KH
543 0: Emacs internal format,
544 1: Shift-JIS (or MS-Kanji) used mainly on Japanese PC,
545 2: ISO-2022 including many variants,
546 3: Big5 used mainly on Chinese PC,
cd9c3177
KH
547 4: private, CCL programs provide encoding/decoding algorithm,
548 5: Raw-text, which means that text contains random 8-bit codes.
0269ddfb 549
3bb1accb 550MNEMONIC is a character to be displayed on mode line for the coding system.
0269ddfb 551
3bb1accb 552DOC-STRING is a documentation string for the coding system.
0269ddfb 553
3bb1accb 554FLAGS specifies more detailed information of the coding system as follows:
d6d6d592 555
3bb1accb 556 If TYPE is 2 (ISO-2022), FLAGS is a list of these elements:
4ed46869
KH
557 CHARSET0, CHARSET1, CHARSET2, CHARSET3, SHORT-FORM,
558 ASCII-EOL, ASCII-CNTL, SEVEN, LOCKING-SHIFT, SINGLE-SHIFT,
d6d6d592 559 USE-ROMAN, USE-OLDJIS, NO-ISO6429, INIT-BOL, DESIGNATION-BOL,
850101ed 560 SAFE, ACCEPT-LATIN-EXTRA-CODE.
4ed46869
KH
561 CHARSETn are character sets initially designated to Gn graphic registers.
562 If CHARSETn is nil, Gn is never used.
563 If CHARSETn is t, Gn can be used but nothing designated initially.
564 If CHARSETn is a list of character sets, those character sets are
565 designated to Gn on output, but nothing designated to Gn initially.
421e3b4e 566 But, character set `ascii' can be designated only to G0.
4ed46869
KH
567 SHORT-FORM non-nil means use short designation sequence on output.
568 ASCII-EOL non-nil means designate ASCII to g0 at end of line on output.
569 ASCII-CNTL non-nil means designate ASCII to g0 before control codes and
570 SPACE on output.
571 SEVEN non-nil means use 7-bit code only on output.
572 LOCKING-SHIFT non-nil means use locking-shift.
573 SINGLE-SHIFT non-nil means use single-shift.
574 USE-ROMAN non-nil means designate JIS0201-1976-Roman instead of ASCII.
575 USE-OLDJIS non-nil means designate JIS0208-1976 instead of JIS0208-1983.
576 NO-ISO6429 non-nil means not use ISO6429's direction specification.
69eba008
KH
577 INIT-BOL non-nil means any designation state is assumed to be reset
578 to initial at each beginning of line on output.
579 DESIGNATION-BOL non-nil means designation sequences should be placed
580 at beginning of line on output.
a6acd8a2
KH
581 SAFE non-nil means convert unsafe characters to `?' on output.
582 Unsafe characters are what not specified in SAFE-CHARSET.
850101ed
RS
583 ACCEPT-LATIN-EXTRA-CODE non-nil means code-detection routine accepts
584 a code specified in `latin-extra-code-table' (which see) as a valid
585 code of the coding system.
d6d6d592 586
3bb1accb
KH
587 If TYPE is 4 (private), FLAGS should be a cons of CCL programs, for
588 decoding and encoding. CCL programs should be specified by their
589 symbols.
d9e3229d
KH
590
591PROPERTIES is an alist of properties vs the corresponding values.
592These properties are set in PLIST, a property list. This function
593also sets properties `coding-category' and `alias-coding-systems'
594automatically.
4ed46869 595
1b46a680
KH
596EOL-TYPE specifies the EOL type of the coding-system in one of the
597following formats:
598
599 o symbol (unix, dos, or mac)
600
601 The symbol `unix' means Unix-like EOL (LF), `dos' means
602 DOS-like EOL (CRLF), and `mac' means MAC-like EOL (CR).
603
604 o number (0, 1, or 2)
605
606 The number 0, 1, and 2 mean UNIX, DOS, and MAC-like EOL
607 respectively.
608
609 o vector of coding-systems of length 3
610
611 The EOL type is detected automatically for the coding system.
612 And, according to the detected EOL type, one of the coding
613 systems in the vector is selected. Elements of the vector
614 corresponds to Unix-liek EOL, DOS-like EOL, and Mac-like EOL
615 in this order.
616
3bb1accb
KH
617Kludgy features for backward compatibility:
618
6191. If TYPE is 4 and car or cdr of FLAGS is a vector, the vector is
620treated as a compiled CCL code.
621
6222. If PROPERTIES is just a list of character sets, the list is set as
623a value of `safe-charsets' in PLIST."
0269ddfb 624 ;; Set a value of `coding-system' property.
6e9722b0 625 (let ((coding-spec (make-vector 5 nil))
d9e3229d
KH
626 (no-initial-designation t)
627 (no-alternative-designation t)
6e9722b0 628 coding-category)
cd9c3177 629 (if (or (not (integerp type)) (< type 0) (> type 5))
0269ddfb 630 (error "TYPE argument must be 0..5"))
8057896b 631 (if (or (not (integerp mnemonic)) (<= mnemonic ? ) (> mnemonic 127))
e8dd0160 632 (error "MNEMONIC argument must be an ASCII printable character."))
0269ddfb
KH
633 (aset coding-spec coding-spec-type-idx type)
634 (aset coding-spec coding-spec-mnemonic-idx mnemonic)
635 (aset coding-spec coding-spec-doc-string-idx
e127a722 636 (purecopy (if (stringp doc-string) doc-string "")))
6e9722b0
KH
637 (cond ((= type 0)
638 (setq coding-category 'coding-category-emacs-mule))
639 ((= type 1)
640 (setq coding-category 'coding-category-sjis))
641 ((= type 2) ; ISO2022
4ed46869 642 (let ((i 0)
6e9722b0 643 (vec (make-vector 32 nil))
05134257
KH
644 (g1-designation nil)
645 (fl flags))
4ed46869 646 (while (< i 4)
05134257 647 (let ((charset (car fl)))
6e9722b0
KH
648 (if (and no-initial-designation
649 (> i 0)
650 (or (charsetp charset)
651 (and (consp charset)
652 (charsetp (car charset)))))
653 (setq no-initial-designation nil))
654 (if (charsetp charset)
655 (if (= i 1) (setq g1-designation charset))
656 (if (consp charset)
657 (let ((tail charset)
658 elt)
659 (while tail
660 (setq elt (car tail))
d9e3229d
KH
661 (if (eq elt t)
662 (setq no-alternative-designation nil)
663 (if (and elt (not (charsetp elt)))
664 (error "Invalid charset: %s" elt)))
6e9722b0
KH
665 (setq tail (cdr tail)))
666 (setq g1-designation (car charset)))
d9e3229d
KH
667 (if charset
668 (if (eq charset t)
669 (setq no-alternative-designation nil)
670 (error "Invalid charset: %s" charset)))))
4ed46869 671 (aset vec i charset))
05134257
KH
672 (setq fl (cdr fl) i (1+ i)))
673 (while (and (< i 32) fl)
674 (aset vec i (car fl))
675 (setq fl (cdr fl) i (1+ i)))
6e9722b0 676 (aset coding-spec 4 vec)
6e9722b0
KH
677 (setq coding-category
678 (if (aref vec 8) ; Use locking-shift.
dc64cd19
KH
679 (or (and (aref vec 7) 'coding-category-iso-7-else)
680 'coding-category-iso-8-else)
6e9722b0
KH
681 (if (aref vec 7) ; 7-bit only.
682 (if (aref vec 9) ; Use single-shift.
dc64cd19 683 'coding-category-iso-7-else
d9e3229d
KH
684 (if no-alternative-designation
685 'coding-category-iso-7-tight
686 'coding-category-iso-7))
3ad911d8
KH
687 (if (or no-initial-designation
688 (not no-alternative-designation))
dc64cd19 689 'coding-category-iso-8-else
6e9722b0
KH
690 (if (and (charsetp g1-designation)
691 (= (charset-dimension g1-designation) 2))
692 'coding-category-iso-8-2
693 'coding-category-iso-8-1)))))))
694 ((= type 3)
695 (setq coding-category 'coding-category-big5))
696 ((= type 4) ; private
c76b5c99 697 (setq coding-category 'coding-category-ccl)
3bb1accb
KH
698 (if (not (consp flags))
699 (error "Invalid FLAGS argument for TYPE 4 (CCL)")
700 (let ((decoder (check-ccl-program
701 (car flags)
702 (intern (format "%s-decoder" coding-system))))
703 (encoder (check-ccl-program
704 (cdr flags)
705 (intern (format "%s-encoder" coding-system)))))
706 (if (and decoder encoder)
707 (aset coding-spec 4 (cons decoder encoder))
708 (error "Invalid FLAGS argument for TYPE 4 (CCL)")))))
cd9c3177
KH
709 (t ; i.e. (= type 5)
710 (setq coding-category 'coding-category-raw-text)))
0269ddfb
KH
711
712 (let ((plist (list 'coding-category coding-category
d9e3229d 713 'alias-coding-systems (list coding-system))))
0269ddfb 714 (if no-initial-designation
d9e3229d
KH
715 (plist-put plist 'no-initial-designation t))
716 (if (and properties
717 (or (eq properties t)
718 (not (consp (car properties)))))
719 ;; In the old version, the arg PROPERTIES is a list to be
720 ;; set in PLIST as a value of property `safe-charsets'.
721 (plist-put plist 'safe-charsets properties)
1b46a680
KH
722 ;; In the current version PROPERTIES is a property list.
723 ;; Reflect it into PLIST one by one.
05134257
KH
724 (let ((l properties))
725 (while l
726 (plist-put plist (car (car l)) (cdr (car l)))
727 (setq l (cdr l)))))
1b46a680
KH
728 ;; The property `coding-category' may have been set differently
729 ;; through PROPERTIES.
730 (setq coding-category (plist-get plist 'coding-category))
0269ddfb 731 (aset coding-spec coding-spec-plist-idx plist))
6e9722b0 732 (put coding-system 'coding-system coding-spec)
6e9722b0
KH
733 (put coding-category 'coding-systems
734 (cons coding-system (get coding-category 'coding-systems))))
4ed46869 735
1b46a680
KH
736 ;; Next, set a value of `eol-type' property.
737 (if (and (not eol-type)
738 (or (<= type 3) (= type 5)))
739 ;; If EOL-TYPE is nil, set a vector of subsidiary coding
740 ;; systems, each corresponds to a coding system for the detected
741 ;; EOL format.
742 (setq eol-type (make-subsidiary-coding-system coding-system)))
743 (setq eol-type
744 (cond ((or (eq eol-type 'unix) (null eol-type))
745 0)
746 ((eq eol-type 'dos)
747 1)
748 ((eq eol-type 'mac)
749 2)
750 ((or (and (vectorp eol-type)
751 (= (length eol-type) 3))
752 (and (numberp eol-type)
753 (and (>= eol-type 0)
754 (<= eol-type 2))))
755 eol-type)
756 (t
757 (error "Invalid EOL-TYPE spec:%S" eol-type))))
758 (put coding-system 'eol-type eol-type)
0269ddfb
KH
759
760 ;; At last, register CODING-SYSTEM in `coding-system-list' and
761 ;; `coding-system-alist'.
857ea15c 762 (add-to-coding-system-list coding-system)
0269ddfb 763 (setq coding-system-alist (cons (list (symbol-name coding-system))
d9e3229d 764 coding-system-alist))
05134257
KH
765
766 ;; For a coding system of cateogory iso-8-1 and iso-8-2, create
767 ;; XXX-with-esc variants.
768 (let ((coding-category (coding-system-category coding-system)))
769 (if (or (eq coding-category 'coding-category-iso-8-1)
770 (eq coding-category 'coding-category-iso-8-2))
771 (let ((esc (intern (concat (symbol-name coding-system) "-with-esc")))
772 (doc (format "Same as %s but can handle any charsets by ISO's escape sequences." coding-system)))
773 (make-coding-system esc type mnemonic doc
774 (if (listp (car flags))
775 (cons (append (car flags) '(t)) (cdr flags))
776 (cons (list (car flags) t) (cdr flags)))
777 properties)
d00742a1 778 (coding-system-put esc 'mime-charset nil)
05134257
KH
779 (coding-system-put esc 'safe-charsets t))))
780
d9e3229d 781 coding-system)
8057896b 782
a42763dc 783(defun define-coding-system-alias (alias coding-system)
358d28fb 784 "Define ALIAS as an alias for coding system CODING-SYSTEM."
0269ddfb
KH
785 (put alias 'coding-system (coding-system-spec coding-system))
786 (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
857ea15c 787 (add-to-coding-system-list alias)
0269ddfb
KH
788 (setq coding-system-alist (cons (list (symbol-name alias))
789 coding-system-alist))
790 (let ((eol-type (coding-system-eol-type coding-system)))
791 (if (vectorp eol-type)
792 (put alias 'eol-type (make-subsidiary-coding-system alias))
793 (put alias 'eol-type eol-type))))
4ed46869
KH
794
795(defun set-buffer-file-coding-system (coding-system &optional force)
358d28fb
RS
796 "Set the file coding-system of the current buffer to CODING-SYSTEM.
797This means that when you save the buffer, it will be converted
798according to CODING-SYSTEM. For a list of possible values of CODING-SYSTEM,
799use \\[list-coding-systems].
800
801If the buffer's previous file coding-system value specifies end-of-line
802conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
803merged with the already-specified end-of-line conversion.
b839fdcc
KH
804
805If the buffer's previous file coding-system value specifies text
806conversion, and CODING-SYSTEM does not specify one, CODING-SYSTEM is
807merged with the already-specified text conversion.
808
809However, if the optional prefix argument FORCE is non-nil, then
810CODING-SYSTEM is used exactly as specified.
aeef8f07
KH
811
812This marks the buffer modified so that the succeeding \\[save-buffer]
813surely saves the buffer with CODING-SYSTEM. From a program, if you
814don't want to mark the buffer modified, just set the variable
815`buffer-file-coding-system' directly."
d9e3229d 816 (interactive "zCoding system for visited file (default, nil): \nP")
4ed46869 817 (check-coding-system coding-system)
36d455c4 818 (if (and coding-system buffer-file-coding-system (null force))
8057896b
KH
819 (let ((x (coding-system-eol-type buffer-file-coding-system))
820 (y (coding-system-eol-type coding-system)))
4ed46869 821 (if (and (numberp x) (>= x 0) (<= x 2) (vectorp y))
b839fdcc
KH
822 (setq coding-system (aref y x))
823 (if (and (eq (coding-system-base coding-system) 'undecided)
824 (numberp y))
825 (setq coding-system (coding-system-change-eol-conversion
826 buffer-file-coding-system y))))))
4ed46869
KH
827 (setq buffer-file-coding-system coding-system)
828 (set-buffer-modified-p t)
829 (force-mode-line-update))
830
358d28fb
RS
831(defvar default-terminal-coding-system nil
832 "Default value for the terminal coding system.
833This is normally set according to the selected language environment.
834See also the command `set-terminal-coding-system'.")
835
df100398
KH
836(defun set-terminal-coding-system (coding-system)
837 "Set coding system of your terminal to CODING-SYSTEM.
358d28fb
RS
838All text output to the terminal will be encoded
839with the specified coding system.
840For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
841The default is determined by the selected language environment
842or by the previous use of this command."
843 (interactive
2e02a76f
RS
844 (list (let ((default (if (and (not (terminal-coding-system))
845 default-terminal-coding-system)
846 default-terminal-coding-system)))
847 (read-coding-system
848 (format "Coding system for terminal display (default, %s): "
849 default)
850 default))))
358d28fb
RS
851 (if (and (not coding-system)
852 (not (terminal-coding-system)))
853 (setq coding-system default-terminal-coding-system))
854 (if coding-system
855 (setq default-terminal-coding-system coding-system))
df100398
KH
856 (set-terminal-coding-system-internal coding-system)
857 (redraw-frame (selected-frame)))
858
358d28fb
RS
859(defvar default-keyboard-coding-system nil
860 "Default value of the keyboard coding system.
861This is normally set according to the selected language environment.
862See also the command `set-keyboard-coding-system'.")
863
df100398 864(defun set-keyboard-coding-system (coding-system)
358d28fb
RS
865 "Set coding system for keyboard input to CODING-SYSTEM.
866In addition, this command enables Encoded-kbd minor mode.
26471f06 867\(If CODING-SYSTEM is nil, Encoded-kbd mode is turned off.)
358d28fb
RS
868For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems].
869The default is determined by the selected language environment
870or by the previous use of this command."
871 (interactive
2e02a76f
RS
872 (list (let ((default (if (and (not (keyboard-coding-system))
873 default-keyboard-coding-system)
874 default-keyboard-coding-system)))
875 (read-coding-system
876 (format "Coding system for keyboard input (default, %s): "
877 default)
878 default))))
358d28fb
RS
879 (if (and (not coding-system)
880 (not (keyboard-coding-system)))
881 (setq coding-system default-keyboard-coding-system))
882 (if coding-system
883 (setq default-keyboard-coding-system coding-system))
df100398
KH
884 (set-keyboard-coding-system-internal coding-system)
885 (encoded-kbd-mode (if coding-system 1 0)))
886
887(defun set-buffer-process-coding-system (decoding encoding)
358d28fb 888 "Set coding systems for the process associated with the current buffer.
df100398 889DECODING is the coding system to be used to decode input from the process,
358d28fb
RS
890ENCODING is the coding system to be used to encode output to the process.
891
892For a list of possible values of CODING-SYSTEM, use \\[list-coding-systems]."
4ed46869
KH
893 (interactive
894 "zCoding-system for process input: \nzCoding-system for process output: ")
895 (let ((proc (get-buffer-process (current-buffer))))
896 (if (null proc)
897 (error "no process")
df100398
KH
898 (check-coding-system decoding)
899 (check-coding-system encoding)
900 (set-process-coding-system proc decoding encoding)))
4ed46869
KH
901 (force-mode-line-update))
902
d0b99881
RS
903(defalias 'set-clipboard-coding-system 'set-selection-coding-system)
904
14915c37 905(defun set-selection-coding-system (coding-system)
b25eef20
KH
906 "Make CODING-SYSTEM used for communicating with other X clients .
907When sending or receiving text via cut_buffer, selection, and clipboard,
908the text is encoded or decoded by CODING-SYSTEM."
a03b3ce1 909 (interactive "zCoding system for X selection: ")
b25eef20 910 (check-coding-system coding-system)
14915c37 911 (setq selection-coding-system coding-system))
b25eef20 912
e8dd0160 913;; Coding system lastly specified by the command
a03b3ce1
KH
914;; set-next-selection-coding-system.
915(defvar last-next-selection-coding-system nil)
916
917(defun set-next-selection-coding-system (coding-system)
918 "Make CODING-SYSTEM used for the next communication with other X clients.
919This setting is effective for the next communication only."
920 (interactive
921 (list (read-coding-system
922 (if last-next-selection-coding-system
923 (format "Coding system for the next X selection (default, %S): "
924 last-next-selection-coding-system)
925 "Coding system for the next X selection: ")
926 last-next-selection-coding-system)))
927 (if coding-system
928 (setq last-next-selection-coding-system coding-system)
929 (setq coding-system last-next-selection-coding-system))
930 (check-coding-system coding-system)
931
932 (setq next-selection-coding-system coding-system))
933
4ed46869 934(defun set-coding-priority (arg)
d9e3229d
KH
935 "Set priority of coding categories according to LIST.
936LIST is a list of coding categories ordered by priority."
937 (let ((l arg)
938 (current-list (copy-sequence coding-category-list)))
e8dd0160 939 ;; Check the validity of ARG while deleting coding categories in
d9e3229d
KH
940 ;; ARG from CURRENT-LIST. We assume that CODING-CATEGORY-LIST
941 ;; contains all coding categories.
942 (while l
943 (if (or (null (get (car l) 'coding-category-index))
944 (null (memq (car l) current-list)))
945 (error "Invalid or duplicated element in argument: %s" arg))
946 (setq current-list (delq (car l) current-list))
947 (setq l (cdr l)))
4ed46869 948 ;; Update `coding-category-list' and return it.
2feaf204
KH
949 (setq coding-category-list (append arg current-list))
950 (set-coding-priority-internal)))
4ed46869
KH
951
952;;; FILE I/O
953
835f49b8 954(defvar auto-coding-alist
0735296c
DL
955 '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
956 ("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\|TAR\\|TGZ\\)\\'" . no-conversion))
835f49b8
KH
957 "Alist of filename patterns vs corresponding coding systems.
958Each element looks like (REGEXP . CODING-SYSTEM).
558b0c86 959A file whose name matches REGEXP is decoded by CODING-SYSTEM on reading.
835f49b8 960
7fed493a
RS
961The settings in this alist take priority over `coding:' tags
962in the file (see the function `set-auto-coding')
963and the contents of `file-coding-system-alist'.")
835f49b8 964
1c4cc63a
KH
965(defvar set-auto-coding-for-load nil
966 "Non-nil means look for `load-coding' property instead of `coding'.
967This is used for loading and byte-compiling Emacs Lisp files.")
968
8a592131
RS
969(defun auto-coding-alist-lookup (filename)
970 "Return the coding system specified by `auto-coding-alist' for FILENAME."
971 (let ((alist auto-coding-alist)
ca128d75 972 (case-fold-search (memq system-type '(vax-vms windows-nt ms-dos)))
8a592131
RS
973 coding-system)
974 (while (and alist (not coding-system))
975 (if (string-match (car (car alist)) filename)
976 (setq coding-system (cdr (car alist)))
977 (setq alist (cdr alist))))
978 coding-system))
979
835f49b8
KH
980(defun set-auto-coding (filename size)
981 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1c4cc63a
KH
982These bytes should include at least the first 1k of the file
983and the last 3k of the file, but the middle may be omitted.
63561304 984
835f49b8
KH
985It checks FILENAME against the variable `auto-coding-alist'.
986If FILENAME doesn't match any entries in the variable,
987it checks for a `coding:' tag in the first one or two lines following
e8dd0160 988point. If no `coding:' tag is found, it checks for local variables
1c4cc63a 989list in the last 3K bytes out of the SIZE bytes.
63561304
KH
990
991The return value is the specified coding system,
992or nil if nothing specified.
87aba788 993
ba74e833 994The variable `set-auto-coding-function' (which see) is set to this
87aba788 995function by default."
8a592131 996 (let ((coding-system (auto-coding-alist-lookup filename)))
835f49b8
KH
997
998 (or coding-system
999 (let* ((case-fold-search t)
1000 (head-start (point))
1001 (head-end (+ head-start (min size 1024)))
1002 (tail-start (+ head-start (max (- size 3072) 0)))
1003 (tail-end (+ head-start size))
1004 coding-system head-found tail-found pos)
1005 ;; Try a short cut by searching for the string "coding:"
e8dd0160 1006 ;; and for "unibyte:" at the head and tail of SIZE bytes.
835f49b8
KH
1007 (setq head-found (or (search-forward "coding:" head-end t)
1008 (search-forward "unibyte:" head-end t)))
1009 (if (and head-found (> head-found tail-start))
1010 ;; Head and tail are overlapped.
1011 (setq tail-found head-found)
1012 (goto-char tail-start)
1013 (setq tail-found (or (search-forward "coding:" tail-end t)
1014 (search-forward "unibyte:" tail-end t))))
1015
1016 ;; At first check the head.
1017 (when head-found
1018 (goto-char head-start)
1019 (setq pos (re-search-forward "[\n\r]" head-end t))
1020 (if (and pos
1021 (= (char-after head-start) ?#)
1022 (= (char-after (1+ head-start)) ?!))
1023 ;; If the file begins with "#!" (exec interpreter magic),
1024 ;; look for coding frobs in the first two lines. You cannot
1025 ;; necessarily put them in the first line of such a file
1026 ;; without screwing up the interpreter invocation.
1027 (setq pos (search-forward "\n" head-end t)))
1028 (if pos (setq head-end pos))
1029 (when (< head-found head-end)
1030 (goto-char head-start)
1031 (when (and set-auto-coding-for-load
1032 (re-search-forward
1033 "-\\*-\\(.*;\\)?[ \t]*unibyte:[ \t]*\\([^ ;]+\\)"
1034 head-end t))
1035 (setq coding-system 'raw-text))
1036 (when (and (not coding-system)
1037 (re-search-forward
1038 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)"
1039 head-end t))
1040 (setq coding-system (intern (match-string 2)))
1041 (or (coding-system-p coding-system)
1042 (setq coding-system nil)))))
1043
1044 ;; If no coding: tag in the head, check the tail.
1045 (when (and tail-found (not coding-system))
1046 (goto-char tail-start)
1047 (search-forward "\n\^L" nil t)
1048 (if (re-search-forward
1049 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
1050 ;; The prefix is what comes before "local variables:" in its
1051 ;; line. The suffix is what comes after "local variables:"
1052 ;; in its line.
1053 (let* ((prefix (regexp-quote (match-string 1)))
1054 (suffix (regexp-quote (match-string 2)))
d318fcf4
EZ
1055 (re-coding
1056 (concat
1057 "^" prefix
1058 "[ \t]*coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1059 suffix "$"))
1060 (re-unibyte
1061 (concat
1062 "^" prefix
1063 "[ \t]*unibyte[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
1064 suffix "$"))
1065 (re-end
1066 (concat "^" prefix "[ \t]*end *:[ \t]*" suffix "$"))
835f49b8
KH
1067 (pos (point)))
1068 (re-search-forward re-end tail-end 'move)
1069 (setq tail-end (point))
1070 (goto-char pos)
1071 (when (and set-auto-coding-for-load
1072 (re-search-forward re-unibyte tail-end t))
1073 (setq coding-system 'raw-text))
1074 (when (and (not coding-system)
1075 (re-search-forward re-coding tail-end t))
1076 (setq coding-system (intern (match-string 1)))
1077 (or (coding-system-p coding-system)
1078 (setq coding-system nil))))))
1079 coding-system))))
63561304
KH
1080
1081(setq set-auto-coding-function 'set-auto-coding)
87aba788 1082
4ed46869
KH
1083;; Set buffer-file-coding-system of the current buffer after some text
1084;; is inserted.
1085(defun after-insert-file-set-buffer-file-coding-system (inserted)
1086 (if last-coding-system-used
1087 (let ((coding-system
1088 (find-new-buffer-file-coding-system last-coding-system-used))
1089 (modified-p (buffer-modified-p)))
0269ddfb
KH
1090 (when coding-system
1091 (set-buffer-file-coding-system coding-system)
d0c26c63
KH
1092 (if (and enable-multibyte-characters
1093 (or (eq coding-system 'no-conversion)
ccb77b4e 1094 (eq (coding-system-type coding-system) 5))
136e48e4
KH
1095 ;; If buffer was unmodified and the size is the
1096 ;; same as INSERTED, we must be visiting it.
1097 (not modified-p)
1098 (= (buffer-size) inserted))
ccb77b4e
RS
1099 ;; For coding systems no-conversion and raw-text...,
1100 ;; edit the buffer as unibyte.
d0c26c63
KH
1101 (let ((pos-byte (position-bytes (+ (point) inserted))))
1102 (set-buffer-multibyte nil)
1103 (setq inserted (- pos-byte (position-bytes (point))))))
0269ddfb 1104 (set-buffer-modified-p modified-p))))
d0c26c63 1105 inserted)
4ed46869 1106
84c9d215
KH
1107(add-hook 'after-insert-file-functions
1108 'after-insert-file-set-buffer-file-coding-system)
4ed46869 1109
8057896b 1110;; The coding-spec and eol-type of coding-system returned is decided
4ed46869
KH
1111;; independently in the following order.
1112;; 1. That of buffer-file-coding-system locally bound.
1113;; 2. That of CODING.
1114
1115(defun find-new-buffer-file-coding-system (coding)
1116 "Return a coding system for a buffer when a file of CODING is inserted.
a73a8c89
KH
1117The local variable `buffer-file-coding-system' of the current buffer
1118is set to the returned value.
509064c5 1119Return nil if there's no need to set `buffer-file-coding-system'."
4ed46869 1120 (let (local-coding local-eol
b685f8d6 1121 found-coding found-eol
4ed46869
KH
1122 new-coding new-eol)
1123 (if (null coding)
1124 ;; Nothing found about coding.
1125 nil
1126
b685f8d6
RS
1127 ;; Get information of `buffer-file-coding-system' in LOCAL-EOL
1128 ;; and LOCAL-CODING.
1129 (setq local-eol (coding-system-eol-type buffer-file-coding-system))
1130 (if (null (numberp local-eol))
1131 ;; But eol-type is not yet set.
1132 (setq local-eol nil))
0269ddfb
KH
1133 (if (and buffer-file-coding-system
1134 (not (eq (coding-system-type buffer-file-coding-system) t)))
1135 ;; This is not `undecided'.
1136 (setq local-coding (coding-system-base buffer-file-coding-system)))
b685f8d6
RS
1137
1138 (if (and (local-variable-p 'buffer-file-coding-system)
1139 local-eol local-coding)
4ed46869
KH
1140 ;; The current buffer has already set full coding-system, we
1141 ;; had better not change it.
1142 nil
1143
8057896b 1144 (setq found-eol (coding-system-eol-type coding))
4ed46869 1145 (if (null (numberp found-eol))
be02cd54
EZ
1146 ;; But eol-type is not found.
1147 ;; If EOL conversions are inhibited, force unix eol-type.
1148 (setq found-eol (if inhibit-eol-conversion 0)))
c76b5c99
KH
1149 (if (eq (coding-system-type coding) t)
1150 (setq found-coding 'undecided)
1151 (setq found-coding (coding-system-base coding)))
1152
1153 (if (and (not found-eol) (eq found-coding 'undecided))
1154 ;; No valid coding information found.
1155 nil
1156
1157 ;; Some coding information (eol or text) found.
1158
1159 ;; The local setting takes precedence over the found one.
1160 (setq new-coding (if (local-variable-p 'buffer-file-coding-system)
1161 (or local-coding found-coding)
1162 (or found-coding local-coding)))
1163 (setq new-eol (if (local-variable-p 'buffer-file-coding-system)
1164 (or local-eol found-eol)
1165 (or found-eol local-eol)))
1166
1167 (let ((eol-type (coding-system-eol-type new-coding)))
1168 (if (and (numberp new-eol) (vectorp eol-type))
1169 (aref eol-type new-eol)
1170 new-coding)))))))
4ed46869 1171
fe831d33
GV
1172(defun modify-coding-system-alist (target-type regexp coding-system)
1173 "Modify one of look up tables for finding a coding system on I/O operation.
8c453b46
RS
1174There are three of such tables, `file-coding-system-alist',
1175`process-coding-system-alist', and `network-coding-system-alist'.
fe831d33
GV
1176
1177TARGET-TYPE specifies which of them to modify.
8c453b46
RS
1178If it is `file', it affects `file-coding-system-alist' (which see).
1179If it is `process', it affects `process-coding-system-alist' (which see).
e8dd0160 1180If it is `network', it affects `network-coding-system-alist' (which see).
fe831d33
GV
1181
1182REGEXP is a regular expression matching a target of I/O operation.
1183The target is a file name if TARGET-TYPE is `file', a program name if
1184TARGET-TYPE is `process', or a network service name or a port number
1185to connect to if TARGET-TYPE is `network'.
1186
1187CODING-SYSTEM is a coding system to perform code conversion on the I/O
8c453b46
RS
1188operation, or a cons cell (DECODING . ENCODING) specifying the coding systems
1189for decoding and encoding respectively,
1190or a function symbol which, when called, returns such a cons cell."
fe831d33
GV
1191 (or (memq target-type '(file process network))
1192 (error "Invalid target type: %s" target-type))
1193 (or (stringp regexp)
1194 (and (eq target-type 'network) (integerp regexp))
1195 (error "Invalid regular expression: %s" regexp))
1196 (if (symbolp coding-system)
1197 (if (not (fboundp coding-system))
1198 (progn
1199 (check-coding-system coding-system)
1200 (setq coding-system (cons coding-system coding-system))))
1201 (check-coding-system (car coding-system))
1202 (check-coding-system (cdr coding-system)))
1203 (cond ((eq target-type 'file)
1204 (let ((slot (assoc regexp file-coding-system-alist)))
1205 (if slot
1206 (setcdr slot coding-system)
1207 (setq file-coding-system-alist
1208 (cons (cons regexp coding-system)
1209 file-coding-system-alist)))))
1210 ((eq target-type 'process)
1211 (let ((slot (assoc regexp process-coding-system-alist)))
1212 (if slot
1213 (setcdr slot coding-system)
1214 (setq process-coding-system-alist
1215 (cons (cons regexp coding-system)
1216 process-coding-system-alist)))))
1217 (t
1218 (let ((slot (assoc regexp network-coding-system-alist)))
1219 (if slot
1220 (setcdr slot coding-system)
1221 (setq network-coding-system-alist
1222 (cons (cons regexp coding-system)
1223 network-coding-system-alist)))))))
1224
b25eef20 1225(defun make-translation-table (&rest args)
f967223b 1226 "Make a translation table (char table) from arguments.
13d5617d 1227Each argument is a list of the form (FROM . TO),
b25eef20 1228where FROM is a character to be translated to TO.
13d5617d 1229
452fdb31
RS
1230FROM can be a generic character (see `make-char'). In this case, TO is
1231a generic character containing the same number of characters, or a
1232ordinary character. If FROM and TO are both generic characters, all
b25eef20 1233characters belonging to FROM are translated to characters belonging to TO
13d5617d 1234without changing their position code(s)."
f967223b 1235 (let ((table (make-char-table 'translation-table))
a73a8c89
KH
1236 revlist)
1237 (while args
1238 (let ((elts (car args)))
1239 (while elts
13d5617d
KH
1240 (let* ((from (car (car elts)))
1241 (from-i 0) ; degree of freedom of FROM
1242 (from-rev (nreverse (split-char from)))
1243 (to (cdr (car elts)))
1244 (to-i 0) ; degree of freedom of TO
1245 (to-rev (nreverse (split-char to))))
1246 ;; Check numbers of heading 0s in FROM-REV and TO-REV.
1247 (while (eq (car from-rev) 0)
1248 (setq from-i (1+ from-i) from-rev (cdr from-rev)))
1249 (while (eq (car to-rev) 0)
1250 (setq to-i (1+ to-i) to-rev (cdr to-rev)))
1251 (if (and (/= from-i to-i) (/= to-i 0))
1252 (error "Invalid character pair (%d . %d)" from to))
b25eef20
KH
1253 ;; If we have already translated TO to TO-ALT, FROM should
1254 ;; also be translated to TO-ALT. But, this is only if TO
1255 ;; is a generic character or TO-ALT is not a generic
13d5617d
KH
1256 ;; character.
1257 (let ((to-alt (aref table to)))
1258 (if (and to-alt
1259 (or (> to-i 0) (not (generic-char-p to-alt))))
1260 (setq to to-alt)))
1261 (if (> from-i 0)
1262 (set-char-table-default table from to)
1263 (aset table from to))
b25eef20
KH
1264 ;; If we have already translated some chars to FROM, they
1265 ;; should also be translated to TO.
a73a8c89
KH
1266 (let ((l (assq from revlist)))
1267 (if l
1268 (let ((ch (car l)))
1269 (setcar l to)
1270 (setq l (cdr l))
1271 (while l
1272 (aset table ch to)
1273 (setq l (cdr l)) ))))
1274 ;; Now update REVLIST.
1275 (let ((l (assq to revlist)))
1276 (if l
1277 (setcdr l (cons from (cdr l)))
1278 (setq revlist (cons (list to from) revlist)))))
1279 (setq elts (cdr elts))))
1280 (setq args (cdr args)))
1281 ;; Return TABLE just created.
1282 table))
1283
c76b5c99
KH
1284(defun make-translation-table-from-vector (vec)
1285 "Make translation table from decoding vector VEC.
1286VEC is an array of 256 elements to map unibyte codes to multibyte characters.
1287See also the variable `nonascii-translation-table'."
1288 (let ((table (make-char-table 'translation-table))
1289 (rev-table (make-char-table 'translation-table))
1290 (i 0)
1291 ch)
1292 (while (< i 256)
1293 (setq ch (aref vec i))
1294 (aset table i ch)
1295 (if (>= ch 256)
1296 (aset rev-table ch i))
1297 (setq i (1+ i)))
1298 (set-char-table-extra-slot table 0 rev-table)
1299 table))
1300
f967223b 1301(defun define-translation-table (symbol &rest args)
e8dd0160 1302 "Define SYMBOL as a name of translation table made by ARGS.
b25eef20 1303
007c79c8
KH
1304If the first element of ARGS is a char-table of which purpose is
1305translation-table, just define SYMBOL as the name of it.
1306
1307In the other case, ARGS are the same as arguments to the function
1308`make-translation-table' (which see).
b25eef20 1309
452fdb31
RS
1310This function sets properties `translation-table' and
1311`translation-table-id' of SYMBOL to the created table itself and
f967223b 1312identification number of the table respectively."
007c79c8
KH
1313 (let ((table (if (and (char-table-p (car args))
1314 (eq (char-table-subtype (car args))
1315 'translation-table))
1316 (car args)
1317 (apply 'make-translation-table args)))
f967223b 1318 (len (length translation-table-vector))
d9e3229d 1319 (id 0)
b25eef20 1320 (done nil))
f967223b 1321 (put symbol 'translation-table table)
b25eef20
KH
1322 (while (not done)
1323 (if (>= id len)
f967223b
KH
1324 (setq translation-table-vector
1325 (vconcat translation-table-vector (make-vector len nil))))
1326 (let ((slot (aref translation-table-vector id)))
b25eef20
KH
1327 (if (or (not slot)
1328 (eq (car slot) symbol))
1329 (progn
f967223b 1330 (aset translation-table-vector id (cons symbol table))
007c79c8
KH
1331 (setq done t))
1332 (setq id (1+ id)))))
f967223b 1333 (put symbol 'translation-table-id id)
d9e3229d
KH
1334 id))
1335
35554641
KH
1336(put 'with-category-table 'lisp-indent-function 1)
1337
1338(defmacro with-category-table (category-table &rest body)
1339 `(let ((current-category-table (category-table)))
1340 (set-category-table ,category-table)
1341 (unwind-protect
1342 (progn ,@body)
1343 (set-category-table current-category-table))))
1344
69eba008
KH
1345;;; Initialize some variables.
1346
1347(put 'use-default-ascent 'char-table-extra-slots 0)
1348(setq use-default-ascent (make-char-table 'use-default-ascent))
d6d6d592
KH
1349(put 'ignore-relative-composition 'char-table-extra-slots 0)
1350(setq ignore-relative-composition
1351 (make-char-table 'ignore-relative-composition))
69eba008
KH
1352
1353;;;
4ed46869
KH
1354(provide 'mule)
1355
1356;;; mule.el ends here